1 /* 2 * Copyright (c) 2006 Oracle. All rights reserved. 3 * 4 * This software is available to you under a choice of one of two 5 * licenses. You may choose to be licensed under the terms of the GNU 6 * General Public License (GPL) Version 2, available from the file 7 * COPYING in the main directory of this source tree, or the 8 * OpenIB.org BSD license below: 9 * 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted provided that the following 12 * conditions are met: 13 * 14 * - Redistributions of source code must retain the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer. 17 * 18 * - Redistributions in binary form must reproduce the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer in the documentation and/or other materials 21 * provided with the distribution. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * SOFTWARE. 31 * 32 */ 33 #include <linux/module.h> 34 #include <linux/errno.h> 35 #include <linux/kernel.h> 36 #include <linux/gfp.h> 37 #include <linux/in.h> 38 #include <linux/poll.h> 39 #include <net/sock.h> 40 41 #include "rds.h" 42 43 /* this is just used for stats gathering :/ */ 44 static DEFINE_SPINLOCK(rds_sock_lock); 45 static unsigned long rds_sock_count; 46 static LIST_HEAD(rds_sock_list); 47 DECLARE_WAIT_QUEUE_HEAD(rds_poll_waitq); 48 49 /* 50 * This is called as the final descriptor referencing this socket is closed. 51 * We have to unbind the socket so that another socket can be bound to the 52 * address it was using. 53 * 54 * We have to be careful about racing with the incoming path. sock_orphan() 55 * sets SOCK_DEAD and we use that as an indicator to the rx path that new 56 * messages shouldn't be queued. 57 */ 58 static int rds_release(struct socket *sock) 59 { 60 struct sock *sk = sock->sk; 61 struct rds_sock *rs; 62 63 if (!sk) 64 goto out; 65 66 rs = rds_sk_to_rs(sk); 67 68 sock_orphan(sk); 69 /* Note - rds_clear_recv_queue grabs rs_recv_lock, so 70 * that ensures the recv path has completed messing 71 * with the socket. */ 72 rds_clear_recv_queue(rs); 73 rds_cong_remove_socket(rs); 74 75 /* 76 * the binding lookup hash uses rcu, we need to 77 * make sure we synchronize_rcu before we free our 78 * entry 79 */ 80 rds_remove_bound(rs); 81 synchronize_rcu(); 82 83 rds_send_drop_to(rs, NULL); 84 rds_rdma_drop_keys(rs); 85 rds_notify_queue_get(rs, NULL); 86 87 spin_lock_bh(&rds_sock_lock); 88 list_del_init(&rs->rs_item); 89 rds_sock_count--; 90 spin_unlock_bh(&rds_sock_lock); 91 92 rds_trans_put(rs->rs_transport); 93 94 sock->sk = NULL; 95 sock_put(sk); 96 out: 97 return 0; 98 } 99 100 /* 101 * Careful not to race with rds_release -> sock_orphan which clears sk_sleep. 102 * _bh() isn't OK here, we're called from interrupt handlers. It's probably OK 103 * to wake the waitqueue after sk_sleep is clear as we hold a sock ref, but 104 * this seems more conservative. 105 * NB - normally, one would use sk_callback_lock for this, but we can 106 * get here from interrupts, whereas the network code grabs sk_callback_lock 107 * with _lock_bh only - so relying on sk_callback_lock introduces livelocks. 108 */ 109 void rds_wake_sk_sleep(struct rds_sock *rs) 110 { 111 unsigned long flags; 112 113 read_lock_irqsave(&rs->rs_recv_lock, flags); 114 __rds_wake_sk_sleep(rds_rs_to_sk(rs)); 115 read_unlock_irqrestore(&rs->rs_recv_lock, flags); 116 } 117 118 static int rds_getname(struct socket *sock, struct sockaddr *uaddr, 119 int *uaddr_len, int peer) 120 { 121 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr; 122 struct rds_sock *rs = rds_sk_to_rs(sock->sk); 123 124 memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 125 126 /* racey, don't care */ 127 if (peer) { 128 if (!rs->rs_conn_addr) 129 return -ENOTCONN; 130 131 sin->sin_port = rs->rs_conn_port; 132 sin->sin_addr.s_addr = rs->rs_conn_addr; 133 } else { 134 sin->sin_port = rs->rs_bound_port; 135 sin->sin_addr.s_addr = rs->rs_bound_addr; 136 } 137 138 sin->sin_family = AF_INET; 139 140 *uaddr_len = sizeof(*sin); 141 return 0; 142 } 143 144 /* 145 * RDS' poll is without a doubt the least intuitive part of the interface, 146 * as POLLIN and POLLOUT do not behave entirely as you would expect from 147 * a network protocol. 148 * 149 * POLLIN is asserted if 150 * - there is data on the receive queue. 151 * - to signal that a previously congested destination may have become 152 * uncongested 153 * - A notification has been queued to the socket (this can be a congestion 154 * update, or a RDMA completion). 155 * 156 * POLLOUT is asserted if there is room on the send queue. This does not mean 157 * however, that the next sendmsg() call will succeed. If the application tries 158 * to send to a congested destination, the system call may still fail (and 159 * return ENOBUFS). 160 */ 161 static unsigned int rds_poll(struct file *file, struct socket *sock, 162 poll_table *wait) 163 { 164 struct sock *sk = sock->sk; 165 struct rds_sock *rs = rds_sk_to_rs(sk); 166 unsigned int mask = 0; 167 unsigned long flags; 168 169 poll_wait(file, sk_sleep(sk), wait); 170 171 if (rs->rs_seen_congestion) 172 poll_wait(file, &rds_poll_waitq, wait); 173 174 read_lock_irqsave(&rs->rs_recv_lock, flags); 175 if (!rs->rs_cong_monitor) { 176 /* When a congestion map was updated, we signal POLLIN for 177 * "historical" reasons. Applications can also poll for 178 * WRBAND instead. */ 179 if (rds_cong_updated_since(&rs->rs_cong_track)) 180 mask |= (POLLIN | POLLRDNORM | POLLWRBAND); 181 } else { 182 spin_lock(&rs->rs_lock); 183 if (rs->rs_cong_notify) 184 mask |= (POLLIN | POLLRDNORM); 185 spin_unlock(&rs->rs_lock); 186 } 187 if (!list_empty(&rs->rs_recv_queue) || 188 !list_empty(&rs->rs_notify_queue)) 189 mask |= (POLLIN | POLLRDNORM); 190 if (rs->rs_snd_bytes < rds_sk_sndbuf(rs)) 191 mask |= (POLLOUT | POLLWRNORM); 192 read_unlock_irqrestore(&rs->rs_recv_lock, flags); 193 194 /* clear state any time we wake a seen-congested socket */ 195 if (mask) 196 rs->rs_seen_congestion = 0; 197 198 return mask; 199 } 200 201 static int rds_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 202 { 203 return -ENOIOCTLCMD; 204 } 205 206 static int rds_cancel_sent_to(struct rds_sock *rs, char __user *optval, 207 int len) 208 { 209 struct sockaddr_in sin; 210 int ret = 0; 211 212 /* racing with another thread binding seems ok here */ 213 if (rs->rs_bound_addr == 0) { 214 ret = -ENOTCONN; /* XXX not a great errno */ 215 goto out; 216 } 217 218 if (len < sizeof(struct sockaddr_in)) { 219 ret = -EINVAL; 220 goto out; 221 } 222 223 if (copy_from_user(&sin, optval, sizeof(sin))) { 224 ret = -EFAULT; 225 goto out; 226 } 227 228 rds_send_drop_to(rs, &sin); 229 out: 230 return ret; 231 } 232 233 static int rds_set_bool_option(unsigned char *optvar, char __user *optval, 234 int optlen) 235 { 236 int value; 237 238 if (optlen < sizeof(int)) 239 return -EINVAL; 240 if (get_user(value, (int __user *) optval)) 241 return -EFAULT; 242 *optvar = !!value; 243 return 0; 244 } 245 246 static int rds_cong_monitor(struct rds_sock *rs, char __user *optval, 247 int optlen) 248 { 249 int ret; 250 251 ret = rds_set_bool_option(&rs->rs_cong_monitor, optval, optlen); 252 if (ret == 0) { 253 if (rs->rs_cong_monitor) { 254 rds_cong_add_socket(rs); 255 } else { 256 rds_cong_remove_socket(rs); 257 rs->rs_cong_mask = 0; 258 rs->rs_cong_notify = 0; 259 } 260 } 261 return ret; 262 } 263 264 static int rds_set_transport(struct rds_sock *rs, char __user *optval, 265 int optlen) 266 { 267 int t_type; 268 269 if (rs->rs_transport) 270 return -EOPNOTSUPP; /* previously attached to transport */ 271 272 if (optlen != sizeof(int)) 273 return -EINVAL; 274 275 if (copy_from_user(&t_type, (int __user *)optval, sizeof(t_type))) 276 return -EFAULT; 277 278 if (t_type < 0 || t_type >= RDS_TRANS_COUNT) 279 return -EINVAL; 280 281 rs->rs_transport = rds_trans_get(t_type); 282 283 return rs->rs_transport ? 0 : -ENOPROTOOPT; 284 } 285 286 static int rds_setsockopt(struct socket *sock, int level, int optname, 287 char __user *optval, unsigned int optlen) 288 { 289 struct rds_sock *rs = rds_sk_to_rs(sock->sk); 290 int ret; 291 292 if (level != SOL_RDS) { 293 ret = -ENOPROTOOPT; 294 goto out; 295 } 296 297 switch (optname) { 298 case RDS_CANCEL_SENT_TO: 299 ret = rds_cancel_sent_to(rs, optval, optlen); 300 break; 301 case RDS_GET_MR: 302 ret = rds_get_mr(rs, optval, optlen); 303 break; 304 case RDS_GET_MR_FOR_DEST: 305 ret = rds_get_mr_for_dest(rs, optval, optlen); 306 break; 307 case RDS_FREE_MR: 308 ret = rds_free_mr(rs, optval, optlen); 309 break; 310 case RDS_RECVERR: 311 ret = rds_set_bool_option(&rs->rs_recverr, optval, optlen); 312 break; 313 case RDS_CONG_MONITOR: 314 ret = rds_cong_monitor(rs, optval, optlen); 315 break; 316 case SO_RDS_TRANSPORT: 317 lock_sock(sock->sk); 318 ret = rds_set_transport(rs, optval, optlen); 319 release_sock(sock->sk); 320 break; 321 default: 322 ret = -ENOPROTOOPT; 323 } 324 out: 325 return ret; 326 } 327 328 static int rds_getsockopt(struct socket *sock, int level, int optname, 329 char __user *optval, int __user *optlen) 330 { 331 struct rds_sock *rs = rds_sk_to_rs(sock->sk); 332 int ret = -ENOPROTOOPT, len; 333 int trans; 334 335 if (level != SOL_RDS) 336 goto out; 337 338 if (get_user(len, optlen)) { 339 ret = -EFAULT; 340 goto out; 341 } 342 343 switch (optname) { 344 case RDS_INFO_FIRST ... RDS_INFO_LAST: 345 ret = rds_info_getsockopt(sock, optname, optval, 346 optlen); 347 break; 348 349 case RDS_RECVERR: 350 if (len < sizeof(int)) 351 ret = -EINVAL; 352 else 353 if (put_user(rs->rs_recverr, (int __user *) optval) || 354 put_user(sizeof(int), optlen)) 355 ret = -EFAULT; 356 else 357 ret = 0; 358 break; 359 case SO_RDS_TRANSPORT: 360 if (len < sizeof(int)) { 361 ret = -EINVAL; 362 break; 363 } 364 trans = (rs->rs_transport ? rs->rs_transport->t_type : 365 RDS_TRANS_NONE); /* unbound */ 366 if (put_user(trans, (int __user *)optval) || 367 put_user(sizeof(int), optlen)) 368 ret = -EFAULT; 369 else 370 ret = 0; 371 break; 372 default: 373 break; 374 } 375 376 out: 377 return ret; 378 379 } 380 381 static int rds_connect(struct socket *sock, struct sockaddr *uaddr, 382 int addr_len, int flags) 383 { 384 struct sock *sk = sock->sk; 385 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr; 386 struct rds_sock *rs = rds_sk_to_rs(sk); 387 int ret = 0; 388 389 lock_sock(sk); 390 391 if (addr_len != sizeof(struct sockaddr_in)) { 392 ret = -EINVAL; 393 goto out; 394 } 395 396 if (sin->sin_family != AF_INET) { 397 ret = -EAFNOSUPPORT; 398 goto out; 399 } 400 401 if (sin->sin_addr.s_addr == htonl(INADDR_ANY)) { 402 ret = -EDESTADDRREQ; 403 goto out; 404 } 405 406 rs->rs_conn_addr = sin->sin_addr.s_addr; 407 rs->rs_conn_port = sin->sin_port; 408 409 out: 410 release_sock(sk); 411 return ret; 412 } 413 414 static struct proto rds_proto = { 415 .name = "RDS", 416 .owner = THIS_MODULE, 417 .obj_size = sizeof(struct rds_sock), 418 }; 419 420 static const struct proto_ops rds_proto_ops = { 421 .family = AF_RDS, 422 .owner = THIS_MODULE, 423 .release = rds_release, 424 .bind = rds_bind, 425 .connect = rds_connect, 426 .socketpair = sock_no_socketpair, 427 .accept = sock_no_accept, 428 .getname = rds_getname, 429 .poll = rds_poll, 430 .ioctl = rds_ioctl, 431 .listen = sock_no_listen, 432 .shutdown = sock_no_shutdown, 433 .setsockopt = rds_setsockopt, 434 .getsockopt = rds_getsockopt, 435 .sendmsg = rds_sendmsg, 436 .recvmsg = rds_recvmsg, 437 .mmap = sock_no_mmap, 438 .sendpage = sock_no_sendpage, 439 }; 440 441 static void rds_sock_destruct(struct sock *sk) 442 { 443 struct rds_sock *rs = rds_sk_to_rs(sk); 444 445 WARN_ON((&rs->rs_item != rs->rs_item.next || 446 &rs->rs_item != rs->rs_item.prev)); 447 } 448 449 static int __rds_create(struct socket *sock, struct sock *sk, int protocol) 450 { 451 struct rds_sock *rs; 452 453 sock_init_data(sock, sk); 454 sock->ops = &rds_proto_ops; 455 sk->sk_protocol = protocol; 456 sk->sk_destruct = rds_sock_destruct; 457 458 rs = rds_sk_to_rs(sk); 459 spin_lock_init(&rs->rs_lock); 460 rwlock_init(&rs->rs_recv_lock); 461 INIT_LIST_HEAD(&rs->rs_send_queue); 462 INIT_LIST_HEAD(&rs->rs_recv_queue); 463 INIT_LIST_HEAD(&rs->rs_notify_queue); 464 INIT_LIST_HEAD(&rs->rs_cong_list); 465 spin_lock_init(&rs->rs_rdma_lock); 466 rs->rs_rdma_keys = RB_ROOT; 467 468 spin_lock_bh(&rds_sock_lock); 469 list_add_tail(&rs->rs_item, &rds_sock_list); 470 rds_sock_count++; 471 spin_unlock_bh(&rds_sock_lock); 472 473 return 0; 474 } 475 476 static int rds_create(struct net *net, struct socket *sock, int protocol, 477 int kern) 478 { 479 struct sock *sk; 480 481 if (sock->type != SOCK_SEQPACKET || protocol) 482 return -ESOCKTNOSUPPORT; 483 484 sk = sk_alloc(net, AF_RDS, GFP_ATOMIC, &rds_proto, kern); 485 if (!sk) 486 return -ENOMEM; 487 488 return __rds_create(sock, sk, protocol); 489 } 490 491 void rds_sock_addref(struct rds_sock *rs) 492 { 493 sock_hold(rds_rs_to_sk(rs)); 494 } 495 496 void rds_sock_put(struct rds_sock *rs) 497 { 498 sock_put(rds_rs_to_sk(rs)); 499 } 500 501 static const struct net_proto_family rds_family_ops = { 502 .family = AF_RDS, 503 .create = rds_create, 504 .owner = THIS_MODULE, 505 }; 506 507 static void rds_sock_inc_info(struct socket *sock, unsigned int len, 508 struct rds_info_iterator *iter, 509 struct rds_info_lengths *lens) 510 { 511 struct rds_sock *rs; 512 struct rds_incoming *inc; 513 unsigned int total = 0; 514 515 len /= sizeof(struct rds_info_message); 516 517 spin_lock_bh(&rds_sock_lock); 518 519 list_for_each_entry(rs, &rds_sock_list, rs_item) { 520 read_lock(&rs->rs_recv_lock); 521 522 /* XXX too lazy to maintain counts.. */ 523 list_for_each_entry(inc, &rs->rs_recv_queue, i_item) { 524 total++; 525 if (total <= len) 526 rds_inc_info_copy(inc, iter, inc->i_saddr, 527 rs->rs_bound_addr, 1); 528 } 529 530 read_unlock(&rs->rs_recv_lock); 531 } 532 533 spin_unlock_bh(&rds_sock_lock); 534 535 lens->nr = total; 536 lens->each = sizeof(struct rds_info_message); 537 } 538 539 static void rds_sock_info(struct socket *sock, unsigned int len, 540 struct rds_info_iterator *iter, 541 struct rds_info_lengths *lens) 542 { 543 struct rds_info_socket sinfo; 544 struct rds_sock *rs; 545 546 len /= sizeof(struct rds_info_socket); 547 548 spin_lock_bh(&rds_sock_lock); 549 550 if (len < rds_sock_count) 551 goto out; 552 553 list_for_each_entry(rs, &rds_sock_list, rs_item) { 554 sinfo.sndbuf = rds_sk_sndbuf(rs); 555 sinfo.rcvbuf = rds_sk_rcvbuf(rs); 556 sinfo.bound_addr = rs->rs_bound_addr; 557 sinfo.connected_addr = rs->rs_conn_addr; 558 sinfo.bound_port = rs->rs_bound_port; 559 sinfo.connected_port = rs->rs_conn_port; 560 sinfo.inum = sock_i_ino(rds_rs_to_sk(rs)); 561 562 rds_info_copy(iter, &sinfo, sizeof(sinfo)); 563 } 564 565 out: 566 lens->nr = rds_sock_count; 567 lens->each = sizeof(struct rds_info_socket); 568 569 spin_unlock_bh(&rds_sock_lock); 570 } 571 572 static void rds_exit(void) 573 { 574 sock_unregister(rds_family_ops.family); 575 proto_unregister(&rds_proto); 576 rds_conn_exit(); 577 rds_cong_exit(); 578 rds_sysctl_exit(); 579 rds_threads_exit(); 580 rds_stats_exit(); 581 rds_page_exit(); 582 rds_info_deregister_func(RDS_INFO_SOCKETS, rds_sock_info); 583 rds_info_deregister_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info); 584 } 585 module_exit(rds_exit); 586 587 static int rds_init(void) 588 { 589 int ret; 590 591 ret = rds_conn_init(); 592 if (ret) 593 goto out; 594 ret = rds_threads_init(); 595 if (ret) 596 goto out_conn; 597 ret = rds_sysctl_init(); 598 if (ret) 599 goto out_threads; 600 ret = rds_stats_init(); 601 if (ret) 602 goto out_sysctl; 603 ret = proto_register(&rds_proto, 1); 604 if (ret) 605 goto out_stats; 606 ret = sock_register(&rds_family_ops); 607 if (ret) 608 goto out_proto; 609 610 rds_info_register_func(RDS_INFO_SOCKETS, rds_sock_info); 611 rds_info_register_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info); 612 613 goto out; 614 615 out_proto: 616 proto_unregister(&rds_proto); 617 out_stats: 618 rds_stats_exit(); 619 out_sysctl: 620 rds_sysctl_exit(); 621 out_threads: 622 rds_threads_exit(); 623 out_conn: 624 rds_conn_exit(); 625 rds_cong_exit(); 626 rds_page_exit(); 627 out: 628 return ret; 629 } 630 module_init(rds_init); 631 632 #define DRV_VERSION "4.0" 633 #define DRV_RELDATE "Feb 12, 2009" 634 635 MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>"); 636 MODULE_DESCRIPTION("RDS: Reliable Datagram Sockets" 637 " v" DRV_VERSION " (" DRV_RELDATE ")"); 638 MODULE_VERSION(DRV_VERSION); 639 MODULE_LICENSE("Dual BSD/GPL"); 640 MODULE_ALIAS_NETPROTO(PF_RDS); 641