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 rds_remove_bound(rs); 76 77 rds_send_drop_to(rs, NULL); 78 rds_rdma_drop_keys(rs); 79 rds_notify_queue_get(rs, NULL); 80 rds_notify_msg_zcopy_purge(&rs->rs_zcookie_queue); 81 82 spin_lock_bh(&rds_sock_lock); 83 list_del_init(&rs->rs_item); 84 rds_sock_count--; 85 spin_unlock_bh(&rds_sock_lock); 86 87 rds_trans_put(rs->rs_transport); 88 89 sock->sk = NULL; 90 sock_put(sk); 91 out: 92 return 0; 93 } 94 95 /* 96 * Careful not to race with rds_release -> sock_orphan which clears sk_sleep. 97 * _bh() isn't OK here, we're called from interrupt handlers. It's probably OK 98 * to wake the waitqueue after sk_sleep is clear as we hold a sock ref, but 99 * this seems more conservative. 100 * NB - normally, one would use sk_callback_lock for this, but we can 101 * get here from interrupts, whereas the network code grabs sk_callback_lock 102 * with _lock_bh only - so relying on sk_callback_lock introduces livelocks. 103 */ 104 void rds_wake_sk_sleep(struct rds_sock *rs) 105 { 106 unsigned long flags; 107 108 read_lock_irqsave(&rs->rs_recv_lock, flags); 109 __rds_wake_sk_sleep(rds_rs_to_sk(rs)); 110 read_unlock_irqrestore(&rs->rs_recv_lock, flags); 111 } 112 113 static int rds_getname(struct socket *sock, struct sockaddr *uaddr, 114 int peer) 115 { 116 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr; 117 struct rds_sock *rs = rds_sk_to_rs(sock->sk); 118 119 memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 120 121 /* racey, don't care */ 122 if (peer) { 123 if (!rs->rs_conn_addr) 124 return -ENOTCONN; 125 126 sin->sin_port = rs->rs_conn_port; 127 sin->sin_addr.s_addr = rs->rs_conn_addr; 128 } else { 129 sin->sin_port = rs->rs_bound_port; 130 sin->sin_addr.s_addr = rs->rs_bound_addr; 131 } 132 133 sin->sin_family = AF_INET; 134 135 return sizeof(*sin); 136 } 137 138 /* 139 * RDS' poll is without a doubt the least intuitive part of the interface, 140 * as EPOLLIN and EPOLLOUT do not behave entirely as you would expect from 141 * a network protocol. 142 * 143 * EPOLLIN is asserted if 144 * - there is data on the receive queue. 145 * - to signal that a previously congested destination may have become 146 * uncongested 147 * - A notification has been queued to the socket (this can be a congestion 148 * update, or a RDMA completion, or a MSG_ZEROCOPY completion). 149 * 150 * EPOLLOUT is asserted if there is room on the send queue. This does not mean 151 * however, that the next sendmsg() call will succeed. If the application tries 152 * to send to a congested destination, the system call may still fail (and 153 * return ENOBUFS). 154 */ 155 static __poll_t rds_poll(struct file *file, struct socket *sock, 156 poll_table *wait) 157 { 158 struct sock *sk = sock->sk; 159 struct rds_sock *rs = rds_sk_to_rs(sk); 160 __poll_t mask = 0; 161 unsigned long flags; 162 163 poll_wait(file, sk_sleep(sk), wait); 164 165 if (rs->rs_seen_congestion) 166 poll_wait(file, &rds_poll_waitq, wait); 167 168 read_lock_irqsave(&rs->rs_recv_lock, flags); 169 if (!rs->rs_cong_monitor) { 170 /* When a congestion map was updated, we signal EPOLLIN for 171 * "historical" reasons. Applications can also poll for 172 * WRBAND instead. */ 173 if (rds_cong_updated_since(&rs->rs_cong_track)) 174 mask |= (EPOLLIN | EPOLLRDNORM | EPOLLWRBAND); 175 } else { 176 spin_lock(&rs->rs_lock); 177 if (rs->rs_cong_notify) 178 mask |= (EPOLLIN | EPOLLRDNORM); 179 spin_unlock(&rs->rs_lock); 180 } 181 if (!list_empty(&rs->rs_recv_queue) || 182 !list_empty(&rs->rs_notify_queue) || 183 !list_empty(&rs->rs_zcookie_queue.zcookie_head)) 184 mask |= (EPOLLIN | EPOLLRDNORM); 185 if (rs->rs_snd_bytes < rds_sk_sndbuf(rs)) 186 mask |= (EPOLLOUT | EPOLLWRNORM); 187 if (sk->sk_err || !skb_queue_empty(&sk->sk_error_queue)) 188 mask |= POLLERR; 189 read_unlock_irqrestore(&rs->rs_recv_lock, flags); 190 191 /* clear state any time we wake a seen-congested socket */ 192 if (mask) 193 rs->rs_seen_congestion = 0; 194 195 return mask; 196 } 197 198 static int rds_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 199 { 200 return -ENOIOCTLCMD; 201 } 202 203 static int rds_cancel_sent_to(struct rds_sock *rs, char __user *optval, 204 int len) 205 { 206 struct sockaddr_in sin; 207 int ret = 0; 208 209 /* racing with another thread binding seems ok here */ 210 if (rs->rs_bound_addr == 0) { 211 ret = -ENOTCONN; /* XXX not a great errno */ 212 goto out; 213 } 214 215 if (len < sizeof(struct sockaddr_in)) { 216 ret = -EINVAL; 217 goto out; 218 } 219 220 if (copy_from_user(&sin, optval, sizeof(sin))) { 221 ret = -EFAULT; 222 goto out; 223 } 224 225 rds_send_drop_to(rs, &sin); 226 out: 227 return ret; 228 } 229 230 static int rds_set_bool_option(unsigned char *optvar, char __user *optval, 231 int optlen) 232 { 233 int value; 234 235 if (optlen < sizeof(int)) 236 return -EINVAL; 237 if (get_user(value, (int __user *) optval)) 238 return -EFAULT; 239 *optvar = !!value; 240 return 0; 241 } 242 243 static int rds_cong_monitor(struct rds_sock *rs, char __user *optval, 244 int optlen) 245 { 246 int ret; 247 248 ret = rds_set_bool_option(&rs->rs_cong_monitor, optval, optlen); 249 if (ret == 0) { 250 if (rs->rs_cong_monitor) { 251 rds_cong_add_socket(rs); 252 } else { 253 rds_cong_remove_socket(rs); 254 rs->rs_cong_mask = 0; 255 rs->rs_cong_notify = 0; 256 } 257 } 258 return ret; 259 } 260 261 static int rds_set_transport(struct rds_sock *rs, char __user *optval, 262 int optlen) 263 { 264 int t_type; 265 266 if (rs->rs_transport) 267 return -EOPNOTSUPP; /* previously attached to transport */ 268 269 if (optlen != sizeof(int)) 270 return -EINVAL; 271 272 if (copy_from_user(&t_type, (int __user *)optval, sizeof(t_type))) 273 return -EFAULT; 274 275 if (t_type < 0 || t_type >= RDS_TRANS_COUNT) 276 return -EINVAL; 277 278 rs->rs_transport = rds_trans_get(t_type); 279 280 return rs->rs_transport ? 0 : -ENOPROTOOPT; 281 } 282 283 static int rds_enable_recvtstamp(struct sock *sk, char __user *optval, 284 int optlen) 285 { 286 int val, valbool; 287 288 if (optlen != sizeof(int)) 289 return -EFAULT; 290 291 if (get_user(val, (int __user *)optval)) 292 return -EFAULT; 293 294 valbool = val ? 1 : 0; 295 296 if (valbool) 297 sock_set_flag(sk, SOCK_RCVTSTAMP); 298 else 299 sock_reset_flag(sk, SOCK_RCVTSTAMP); 300 301 return 0; 302 } 303 304 static int rds_recv_track_latency(struct rds_sock *rs, char __user *optval, 305 int optlen) 306 { 307 struct rds_rx_trace_so trace; 308 int i; 309 310 if (optlen != sizeof(struct rds_rx_trace_so)) 311 return -EFAULT; 312 313 if (copy_from_user(&trace, optval, sizeof(trace))) 314 return -EFAULT; 315 316 if (trace.rx_traces > RDS_MSG_RX_DGRAM_TRACE_MAX) 317 return -EFAULT; 318 319 rs->rs_rx_traces = trace.rx_traces; 320 for (i = 0; i < rs->rs_rx_traces; i++) { 321 if (trace.rx_trace_pos[i] > RDS_MSG_RX_DGRAM_TRACE_MAX) { 322 rs->rs_rx_traces = 0; 323 return -EFAULT; 324 } 325 rs->rs_rx_trace[i] = trace.rx_trace_pos[i]; 326 } 327 328 return 0; 329 } 330 331 static int rds_setsockopt(struct socket *sock, int level, int optname, 332 char __user *optval, unsigned int optlen) 333 { 334 struct rds_sock *rs = rds_sk_to_rs(sock->sk); 335 int ret; 336 337 if (level != SOL_RDS) { 338 ret = -ENOPROTOOPT; 339 goto out; 340 } 341 342 switch (optname) { 343 case RDS_CANCEL_SENT_TO: 344 ret = rds_cancel_sent_to(rs, optval, optlen); 345 break; 346 case RDS_GET_MR: 347 ret = rds_get_mr(rs, optval, optlen); 348 break; 349 case RDS_GET_MR_FOR_DEST: 350 ret = rds_get_mr_for_dest(rs, optval, optlen); 351 break; 352 case RDS_FREE_MR: 353 ret = rds_free_mr(rs, optval, optlen); 354 break; 355 case RDS_RECVERR: 356 ret = rds_set_bool_option(&rs->rs_recverr, optval, optlen); 357 break; 358 case RDS_CONG_MONITOR: 359 ret = rds_cong_monitor(rs, optval, optlen); 360 break; 361 case SO_RDS_TRANSPORT: 362 lock_sock(sock->sk); 363 ret = rds_set_transport(rs, optval, optlen); 364 release_sock(sock->sk); 365 break; 366 case SO_TIMESTAMP: 367 lock_sock(sock->sk); 368 ret = rds_enable_recvtstamp(sock->sk, optval, optlen); 369 release_sock(sock->sk); 370 break; 371 case SO_RDS_MSG_RXPATH_LATENCY: 372 ret = rds_recv_track_latency(rs, optval, optlen); 373 break; 374 default: 375 ret = -ENOPROTOOPT; 376 } 377 out: 378 return ret; 379 } 380 381 static int rds_getsockopt(struct socket *sock, int level, int optname, 382 char __user *optval, int __user *optlen) 383 { 384 struct rds_sock *rs = rds_sk_to_rs(sock->sk); 385 int ret = -ENOPROTOOPT, len; 386 int trans; 387 388 if (level != SOL_RDS) 389 goto out; 390 391 if (get_user(len, optlen)) { 392 ret = -EFAULT; 393 goto out; 394 } 395 396 switch (optname) { 397 case RDS_INFO_FIRST ... RDS_INFO_LAST: 398 ret = rds_info_getsockopt(sock, optname, optval, 399 optlen); 400 break; 401 402 case RDS_RECVERR: 403 if (len < sizeof(int)) 404 ret = -EINVAL; 405 else 406 if (put_user(rs->rs_recverr, (int __user *) optval) || 407 put_user(sizeof(int), optlen)) 408 ret = -EFAULT; 409 else 410 ret = 0; 411 break; 412 case SO_RDS_TRANSPORT: 413 if (len < sizeof(int)) { 414 ret = -EINVAL; 415 break; 416 } 417 trans = (rs->rs_transport ? rs->rs_transport->t_type : 418 RDS_TRANS_NONE); /* unbound */ 419 if (put_user(trans, (int __user *)optval) || 420 put_user(sizeof(int), optlen)) 421 ret = -EFAULT; 422 else 423 ret = 0; 424 break; 425 default: 426 break; 427 } 428 429 out: 430 return ret; 431 432 } 433 434 static int rds_connect(struct socket *sock, struct sockaddr *uaddr, 435 int addr_len, int flags) 436 { 437 struct sock *sk = sock->sk; 438 struct sockaddr_in *sin = (struct sockaddr_in *)uaddr; 439 struct rds_sock *rs = rds_sk_to_rs(sk); 440 int ret = 0; 441 442 lock_sock(sk); 443 444 if (addr_len != sizeof(struct sockaddr_in)) { 445 ret = -EINVAL; 446 goto out; 447 } 448 449 if (sin->sin_family != AF_INET) { 450 ret = -EAFNOSUPPORT; 451 goto out; 452 } 453 454 if (sin->sin_addr.s_addr == htonl(INADDR_ANY)) { 455 ret = -EDESTADDRREQ; 456 goto out; 457 } 458 459 rs->rs_conn_addr = sin->sin_addr.s_addr; 460 rs->rs_conn_port = sin->sin_port; 461 462 out: 463 release_sock(sk); 464 return ret; 465 } 466 467 static struct proto rds_proto = { 468 .name = "RDS", 469 .owner = THIS_MODULE, 470 .obj_size = sizeof(struct rds_sock), 471 }; 472 473 static const struct proto_ops rds_proto_ops = { 474 .family = AF_RDS, 475 .owner = THIS_MODULE, 476 .release = rds_release, 477 .bind = rds_bind, 478 .connect = rds_connect, 479 .socketpair = sock_no_socketpair, 480 .accept = sock_no_accept, 481 .getname = rds_getname, 482 .poll = rds_poll, 483 .ioctl = rds_ioctl, 484 .listen = sock_no_listen, 485 .shutdown = sock_no_shutdown, 486 .setsockopt = rds_setsockopt, 487 .getsockopt = rds_getsockopt, 488 .sendmsg = rds_sendmsg, 489 .recvmsg = rds_recvmsg, 490 .mmap = sock_no_mmap, 491 .sendpage = sock_no_sendpage, 492 }; 493 494 static void rds_sock_destruct(struct sock *sk) 495 { 496 struct rds_sock *rs = rds_sk_to_rs(sk); 497 498 WARN_ON((&rs->rs_item != rs->rs_item.next || 499 &rs->rs_item != rs->rs_item.prev)); 500 } 501 502 static int __rds_create(struct socket *sock, struct sock *sk, int protocol) 503 { 504 struct rds_sock *rs; 505 506 sock_init_data(sock, sk); 507 sock->ops = &rds_proto_ops; 508 sk->sk_protocol = protocol; 509 sk->sk_destruct = rds_sock_destruct; 510 511 rs = rds_sk_to_rs(sk); 512 spin_lock_init(&rs->rs_lock); 513 rwlock_init(&rs->rs_recv_lock); 514 INIT_LIST_HEAD(&rs->rs_send_queue); 515 INIT_LIST_HEAD(&rs->rs_recv_queue); 516 INIT_LIST_HEAD(&rs->rs_notify_queue); 517 INIT_LIST_HEAD(&rs->rs_cong_list); 518 rds_message_zcopy_queue_init(&rs->rs_zcookie_queue); 519 spin_lock_init(&rs->rs_rdma_lock); 520 rs->rs_rdma_keys = RB_ROOT; 521 rs->rs_rx_traces = 0; 522 523 spin_lock_bh(&rds_sock_lock); 524 list_add_tail(&rs->rs_item, &rds_sock_list); 525 rds_sock_count++; 526 spin_unlock_bh(&rds_sock_lock); 527 528 return 0; 529 } 530 531 static int rds_create(struct net *net, struct socket *sock, int protocol, 532 int kern) 533 { 534 struct sock *sk; 535 536 if (sock->type != SOCK_SEQPACKET || protocol) 537 return -ESOCKTNOSUPPORT; 538 539 sk = sk_alloc(net, AF_RDS, GFP_ATOMIC, &rds_proto, kern); 540 if (!sk) 541 return -ENOMEM; 542 543 return __rds_create(sock, sk, protocol); 544 } 545 546 void rds_sock_addref(struct rds_sock *rs) 547 { 548 sock_hold(rds_rs_to_sk(rs)); 549 } 550 551 void rds_sock_put(struct rds_sock *rs) 552 { 553 sock_put(rds_rs_to_sk(rs)); 554 } 555 556 static const struct net_proto_family rds_family_ops = { 557 .family = AF_RDS, 558 .create = rds_create, 559 .owner = THIS_MODULE, 560 }; 561 562 static void rds_sock_inc_info(struct socket *sock, unsigned int len, 563 struct rds_info_iterator *iter, 564 struct rds_info_lengths *lens) 565 { 566 struct rds_sock *rs; 567 struct rds_incoming *inc; 568 unsigned int total = 0; 569 570 len /= sizeof(struct rds_info_message); 571 572 spin_lock_bh(&rds_sock_lock); 573 574 list_for_each_entry(rs, &rds_sock_list, rs_item) { 575 read_lock(&rs->rs_recv_lock); 576 577 /* XXX too lazy to maintain counts.. */ 578 list_for_each_entry(inc, &rs->rs_recv_queue, i_item) { 579 total++; 580 if (total <= len) 581 rds_inc_info_copy(inc, iter, inc->i_saddr, 582 rs->rs_bound_addr, 1); 583 } 584 585 read_unlock(&rs->rs_recv_lock); 586 } 587 588 spin_unlock_bh(&rds_sock_lock); 589 590 lens->nr = total; 591 lens->each = sizeof(struct rds_info_message); 592 } 593 594 static void rds_sock_info(struct socket *sock, unsigned int len, 595 struct rds_info_iterator *iter, 596 struct rds_info_lengths *lens) 597 { 598 struct rds_info_socket sinfo; 599 struct rds_sock *rs; 600 601 len /= sizeof(struct rds_info_socket); 602 603 spin_lock_bh(&rds_sock_lock); 604 605 if (len < rds_sock_count) 606 goto out; 607 608 list_for_each_entry(rs, &rds_sock_list, rs_item) { 609 sinfo.sndbuf = rds_sk_sndbuf(rs); 610 sinfo.rcvbuf = rds_sk_rcvbuf(rs); 611 sinfo.bound_addr = rs->rs_bound_addr; 612 sinfo.connected_addr = rs->rs_conn_addr; 613 sinfo.bound_port = rs->rs_bound_port; 614 sinfo.connected_port = rs->rs_conn_port; 615 sinfo.inum = sock_i_ino(rds_rs_to_sk(rs)); 616 617 rds_info_copy(iter, &sinfo, sizeof(sinfo)); 618 } 619 620 out: 621 lens->nr = rds_sock_count; 622 lens->each = sizeof(struct rds_info_socket); 623 624 spin_unlock_bh(&rds_sock_lock); 625 } 626 627 static void rds_exit(void) 628 { 629 sock_unregister(rds_family_ops.family); 630 proto_unregister(&rds_proto); 631 rds_conn_exit(); 632 rds_cong_exit(); 633 rds_sysctl_exit(); 634 rds_threads_exit(); 635 rds_stats_exit(); 636 rds_page_exit(); 637 rds_bind_lock_destroy(); 638 rds_info_deregister_func(RDS_INFO_SOCKETS, rds_sock_info); 639 rds_info_deregister_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info); 640 } 641 module_exit(rds_exit); 642 643 u32 rds_gen_num; 644 645 static int rds_init(void) 646 { 647 int ret; 648 649 net_get_random_once(&rds_gen_num, sizeof(rds_gen_num)); 650 651 ret = rds_bind_lock_init(); 652 if (ret) 653 goto out; 654 655 ret = rds_conn_init(); 656 if (ret) 657 goto out_bind; 658 659 ret = rds_threads_init(); 660 if (ret) 661 goto out_conn; 662 ret = rds_sysctl_init(); 663 if (ret) 664 goto out_threads; 665 ret = rds_stats_init(); 666 if (ret) 667 goto out_sysctl; 668 ret = proto_register(&rds_proto, 1); 669 if (ret) 670 goto out_stats; 671 ret = sock_register(&rds_family_ops); 672 if (ret) 673 goto out_proto; 674 675 rds_info_register_func(RDS_INFO_SOCKETS, rds_sock_info); 676 rds_info_register_func(RDS_INFO_RECV_MESSAGES, rds_sock_inc_info); 677 678 goto out; 679 680 out_proto: 681 proto_unregister(&rds_proto); 682 out_stats: 683 rds_stats_exit(); 684 out_sysctl: 685 rds_sysctl_exit(); 686 out_threads: 687 rds_threads_exit(); 688 out_conn: 689 rds_conn_exit(); 690 rds_cong_exit(); 691 rds_page_exit(); 692 out_bind: 693 rds_bind_lock_destroy(); 694 out: 695 return ret; 696 } 697 module_init(rds_init); 698 699 #define DRV_VERSION "4.0" 700 #define DRV_RELDATE "Feb 12, 2009" 701 702 MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>"); 703 MODULE_DESCRIPTION("RDS: Reliable Datagram Sockets" 704 " v" DRV_VERSION " (" DRV_RELDATE ")"); 705 MODULE_VERSION(DRV_VERSION); 706 MODULE_LICENSE("Dual BSD/GPL"); 707 MODULE_ALIAS_NETPROTO(PF_RDS); 708