1 /* 2 * Copyright (c) 2006, 2018 Oracle and/or its affiliates. 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/kernel.h> 34 #include <linux/slab.h> 35 #include <linux/in.h> 36 #include <linux/module.h> 37 #include <net/tcp.h> 38 #include <net/net_namespace.h> 39 #include <net/netns/generic.h> 40 #include <net/tcp.h> 41 #include <net/addrconf.h> 42 43 #include "rds.h" 44 #include "tcp.h" 45 46 /* only for info exporting */ 47 static DEFINE_SPINLOCK(rds_tcp_tc_list_lock); 48 static LIST_HEAD(rds_tcp_tc_list); 49 50 /* rds_tcp_tc_count counts only IPv4 connections. 51 * rds6_tcp_tc_count counts both IPv4 and IPv6 connections. 52 */ 53 static unsigned int rds_tcp_tc_count; 54 static unsigned int rds6_tcp_tc_count; 55 56 /* Track rds_tcp_connection structs so they can be cleaned up */ 57 static DEFINE_SPINLOCK(rds_tcp_conn_lock); 58 static LIST_HEAD(rds_tcp_conn_list); 59 static atomic_t rds_tcp_unloading = ATOMIC_INIT(0); 60 61 static struct kmem_cache *rds_tcp_conn_slab; 62 63 static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write, 64 void __user *buffer, size_t *lenp, 65 loff_t *fpos); 66 67 static int rds_tcp_min_sndbuf = SOCK_MIN_SNDBUF; 68 static int rds_tcp_min_rcvbuf = SOCK_MIN_RCVBUF; 69 70 static struct ctl_table rds_tcp_sysctl_table[] = { 71 #define RDS_TCP_SNDBUF 0 72 { 73 .procname = "rds_tcp_sndbuf", 74 /* data is per-net pointer */ 75 .maxlen = sizeof(int), 76 .mode = 0644, 77 .proc_handler = rds_tcp_skbuf_handler, 78 .extra1 = &rds_tcp_min_sndbuf, 79 }, 80 #define RDS_TCP_RCVBUF 1 81 { 82 .procname = "rds_tcp_rcvbuf", 83 /* data is per-net pointer */ 84 .maxlen = sizeof(int), 85 .mode = 0644, 86 .proc_handler = rds_tcp_skbuf_handler, 87 .extra1 = &rds_tcp_min_rcvbuf, 88 }, 89 { } 90 }; 91 92 /* doing it this way avoids calling tcp_sk() */ 93 void rds_tcp_nonagle(struct socket *sock) 94 { 95 int val = 1; 96 97 kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (void *)&val, 98 sizeof(val)); 99 } 100 101 u32 rds_tcp_write_seq(struct rds_tcp_connection *tc) 102 { 103 /* seq# of the last byte of data in tcp send buffer */ 104 return tcp_sk(tc->t_sock->sk)->write_seq; 105 } 106 107 u32 rds_tcp_snd_una(struct rds_tcp_connection *tc) 108 { 109 return tcp_sk(tc->t_sock->sk)->snd_una; 110 } 111 112 void rds_tcp_restore_callbacks(struct socket *sock, 113 struct rds_tcp_connection *tc) 114 { 115 rdsdebug("restoring sock %p callbacks from tc %p\n", sock, tc); 116 write_lock_bh(&sock->sk->sk_callback_lock); 117 118 /* done under the callback_lock to serialize with write_space */ 119 spin_lock(&rds_tcp_tc_list_lock); 120 list_del_init(&tc->t_list_item); 121 rds6_tcp_tc_count--; 122 if (!tc->t_cpath->cp_conn->c_isv6) 123 rds_tcp_tc_count--; 124 spin_unlock(&rds_tcp_tc_list_lock); 125 126 tc->t_sock = NULL; 127 128 sock->sk->sk_write_space = tc->t_orig_write_space; 129 sock->sk->sk_data_ready = tc->t_orig_data_ready; 130 sock->sk->sk_state_change = tc->t_orig_state_change; 131 sock->sk->sk_user_data = NULL; 132 133 write_unlock_bh(&sock->sk->sk_callback_lock); 134 } 135 136 /* 137 * rds_tcp_reset_callbacks() switches the to the new sock and 138 * returns the existing tc->t_sock. 139 * 140 * The only functions that set tc->t_sock are rds_tcp_set_callbacks 141 * and rds_tcp_reset_callbacks. Send and receive trust that 142 * it is set. The absence of RDS_CONN_UP bit protects those paths 143 * from being called while it isn't set. 144 */ 145 void rds_tcp_reset_callbacks(struct socket *sock, 146 struct rds_conn_path *cp) 147 { 148 struct rds_tcp_connection *tc = cp->cp_transport_data; 149 struct socket *osock = tc->t_sock; 150 151 if (!osock) 152 goto newsock; 153 154 /* Need to resolve a duelling SYN between peers. 155 * We have an outstanding SYN to this peer, which may 156 * potentially have transitioned to the RDS_CONN_UP state, 157 * so we must quiesce any send threads before resetting 158 * cp_transport_data. We quiesce these threads by setting 159 * cp_state to something other than RDS_CONN_UP, and then 160 * waiting for any existing threads in rds_send_xmit to 161 * complete release_in_xmit(). (Subsequent threads entering 162 * rds_send_xmit() will bail on !rds_conn_up(). 163 * 164 * However an incoming syn-ack at this point would end up 165 * marking the conn as RDS_CONN_UP, and would again permit 166 * rds_send_xmi() threads through, so ideally we would 167 * synchronize on RDS_CONN_UP after lock_sock(), but cannot 168 * do that: waiting on !RDS_IN_XMIT after lock_sock() may 169 * end up deadlocking with tcp_sendmsg(), and the RDS_IN_XMIT 170 * would not get set. As a result, we set c_state to 171 * RDS_CONN_RESETTTING, to ensure that rds_tcp_state_change 172 * cannot mark rds_conn_path_up() in the window before lock_sock() 173 */ 174 atomic_set(&cp->cp_state, RDS_CONN_RESETTING); 175 wait_event(cp->cp_waitq, !test_bit(RDS_IN_XMIT, &cp->cp_flags)); 176 lock_sock(osock->sk); 177 /* reset receive side state for rds_tcp_data_recv() for osock */ 178 cancel_delayed_work_sync(&cp->cp_send_w); 179 cancel_delayed_work_sync(&cp->cp_recv_w); 180 if (tc->t_tinc) { 181 rds_inc_put(&tc->t_tinc->ti_inc); 182 tc->t_tinc = NULL; 183 } 184 tc->t_tinc_hdr_rem = sizeof(struct rds_header); 185 tc->t_tinc_data_rem = 0; 186 rds_tcp_restore_callbacks(osock, tc); 187 release_sock(osock->sk); 188 sock_release(osock); 189 newsock: 190 rds_send_path_reset(cp); 191 lock_sock(sock->sk); 192 rds_tcp_set_callbacks(sock, cp); 193 release_sock(sock->sk); 194 } 195 196 /* Add tc to rds_tcp_tc_list and set tc->t_sock. See comments 197 * above rds_tcp_reset_callbacks for notes about synchronization 198 * with data path 199 */ 200 void rds_tcp_set_callbacks(struct socket *sock, struct rds_conn_path *cp) 201 { 202 struct rds_tcp_connection *tc = cp->cp_transport_data; 203 204 rdsdebug("setting sock %p callbacks to tc %p\n", sock, tc); 205 write_lock_bh(&sock->sk->sk_callback_lock); 206 207 /* done under the callback_lock to serialize with write_space */ 208 spin_lock(&rds_tcp_tc_list_lock); 209 list_add_tail(&tc->t_list_item, &rds_tcp_tc_list); 210 rds6_tcp_tc_count++; 211 if (!tc->t_cpath->cp_conn->c_isv6) 212 rds_tcp_tc_count++; 213 spin_unlock(&rds_tcp_tc_list_lock); 214 215 /* accepted sockets need our listen data ready undone */ 216 if (sock->sk->sk_data_ready == rds_tcp_listen_data_ready) 217 sock->sk->sk_data_ready = sock->sk->sk_user_data; 218 219 tc->t_sock = sock; 220 tc->t_cpath = cp; 221 tc->t_orig_data_ready = sock->sk->sk_data_ready; 222 tc->t_orig_write_space = sock->sk->sk_write_space; 223 tc->t_orig_state_change = sock->sk->sk_state_change; 224 225 sock->sk->sk_user_data = cp; 226 sock->sk->sk_data_ready = rds_tcp_data_ready; 227 sock->sk->sk_write_space = rds_tcp_write_space; 228 sock->sk->sk_state_change = rds_tcp_state_change; 229 230 write_unlock_bh(&sock->sk->sk_callback_lock); 231 } 232 233 /* Handle RDS_INFO_TCP_SOCKETS socket option. It only returns IPv4 234 * connections for backward compatibility. 235 */ 236 static void rds_tcp_tc_info(struct socket *rds_sock, unsigned int len, 237 struct rds_info_iterator *iter, 238 struct rds_info_lengths *lens) 239 { 240 struct rds_info_tcp_socket tsinfo; 241 struct rds_tcp_connection *tc; 242 unsigned long flags; 243 244 spin_lock_irqsave(&rds_tcp_tc_list_lock, flags); 245 246 if (len / sizeof(tsinfo) < rds_tcp_tc_count) 247 goto out; 248 249 list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) { 250 struct inet_sock *inet = inet_sk(tc->t_sock->sk); 251 252 if (tc->t_cpath->cp_conn->c_isv6) 253 continue; 254 255 tsinfo.local_addr = inet->inet_saddr; 256 tsinfo.local_port = inet->inet_sport; 257 tsinfo.peer_addr = inet->inet_daddr; 258 tsinfo.peer_port = inet->inet_dport; 259 260 tsinfo.hdr_rem = tc->t_tinc_hdr_rem; 261 tsinfo.data_rem = tc->t_tinc_data_rem; 262 tsinfo.last_sent_nxt = tc->t_last_sent_nxt; 263 tsinfo.last_expected_una = tc->t_last_expected_una; 264 tsinfo.last_seen_una = tc->t_last_seen_una; 265 266 rds_info_copy(iter, &tsinfo, sizeof(tsinfo)); 267 } 268 269 out: 270 lens->nr = rds_tcp_tc_count; 271 lens->each = sizeof(tsinfo); 272 273 spin_unlock_irqrestore(&rds_tcp_tc_list_lock, flags); 274 } 275 276 /* Handle RDS6_INFO_TCP_SOCKETS socket option. It returns both IPv4 and 277 * IPv6 connections. IPv4 connection address is returned in an IPv4 mapped 278 * address. 279 */ 280 static void rds6_tcp_tc_info(struct socket *sock, unsigned int len, 281 struct rds_info_iterator *iter, 282 struct rds_info_lengths *lens) 283 { 284 struct rds6_info_tcp_socket tsinfo6; 285 struct rds_tcp_connection *tc; 286 unsigned long flags; 287 288 spin_lock_irqsave(&rds_tcp_tc_list_lock, flags); 289 290 if (len / sizeof(tsinfo6) < rds6_tcp_tc_count) 291 goto out; 292 293 list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) { 294 struct sock *sk = tc->t_sock->sk; 295 struct inet_sock *inet = inet_sk(sk); 296 297 tsinfo6.local_addr = sk->sk_v6_rcv_saddr; 298 tsinfo6.local_port = inet->inet_sport; 299 tsinfo6.peer_addr = sk->sk_v6_daddr; 300 tsinfo6.peer_port = inet->inet_dport; 301 302 tsinfo6.hdr_rem = tc->t_tinc_hdr_rem; 303 tsinfo6.data_rem = tc->t_tinc_data_rem; 304 tsinfo6.last_sent_nxt = tc->t_last_sent_nxt; 305 tsinfo6.last_expected_una = tc->t_last_expected_una; 306 tsinfo6.last_seen_una = tc->t_last_seen_una; 307 308 rds_info_copy(iter, &tsinfo6, sizeof(tsinfo6)); 309 } 310 311 out: 312 lens->nr = rds6_tcp_tc_count; 313 lens->each = sizeof(tsinfo6); 314 315 spin_unlock_irqrestore(&rds_tcp_tc_list_lock, flags); 316 } 317 318 static int rds_tcp_laddr_check(struct net *net, const struct in6_addr *addr, 319 __u32 scope_id) 320 { 321 struct net_device *dev = NULL; 322 int ret; 323 324 if (ipv6_addr_v4mapped(addr)) { 325 if (inet_addr_type(net, addr->s6_addr32[3]) == RTN_LOCAL) 326 return 0; 327 return -EADDRNOTAVAIL; 328 } 329 330 /* If the scope_id is specified, check only those addresses 331 * hosted on the specified interface. 332 */ 333 if (scope_id != 0) { 334 rcu_read_lock(); 335 dev = dev_get_by_index_rcu(net, scope_id); 336 /* scope_id is not valid... */ 337 if (!dev) { 338 rcu_read_unlock(); 339 return -EADDRNOTAVAIL; 340 } 341 rcu_read_unlock(); 342 } 343 ret = ipv6_chk_addr(net, addr, dev, 0); 344 if (ret) 345 return 0; 346 return -EADDRNOTAVAIL; 347 } 348 349 static void rds_tcp_conn_free(void *arg) 350 { 351 struct rds_tcp_connection *tc = arg; 352 unsigned long flags; 353 354 rdsdebug("freeing tc %p\n", tc); 355 356 spin_lock_irqsave(&rds_tcp_conn_lock, flags); 357 if (!tc->t_tcp_node_detached) 358 list_del(&tc->t_tcp_node); 359 spin_unlock_irqrestore(&rds_tcp_conn_lock, flags); 360 361 kmem_cache_free(rds_tcp_conn_slab, tc); 362 } 363 364 static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp) 365 { 366 struct rds_tcp_connection *tc; 367 int i, j; 368 int ret = 0; 369 370 for (i = 0; i < RDS_MPATH_WORKERS; i++) { 371 tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp); 372 if (!tc) { 373 ret = -ENOMEM; 374 goto fail; 375 } 376 mutex_init(&tc->t_conn_path_lock); 377 tc->t_sock = NULL; 378 tc->t_tinc = NULL; 379 tc->t_tinc_hdr_rem = sizeof(struct rds_header); 380 tc->t_tinc_data_rem = 0; 381 382 conn->c_path[i].cp_transport_data = tc; 383 tc->t_cpath = &conn->c_path[i]; 384 tc->t_tcp_node_detached = true; 385 386 rdsdebug("rds_conn_path [%d] tc %p\n", i, 387 conn->c_path[i].cp_transport_data); 388 } 389 spin_lock_irq(&rds_tcp_conn_lock); 390 for (i = 0; i < RDS_MPATH_WORKERS; i++) { 391 tc = conn->c_path[i].cp_transport_data; 392 tc->t_tcp_node_detached = false; 393 list_add_tail(&tc->t_tcp_node, &rds_tcp_conn_list); 394 } 395 spin_unlock_irq(&rds_tcp_conn_lock); 396 fail: 397 if (ret) { 398 for (j = 0; j < i; j++) 399 rds_tcp_conn_free(conn->c_path[j].cp_transport_data); 400 } 401 return ret; 402 } 403 404 static bool list_has_conn(struct list_head *list, struct rds_connection *conn) 405 { 406 struct rds_tcp_connection *tc, *_tc; 407 408 list_for_each_entry_safe(tc, _tc, list, t_tcp_node) { 409 if (tc->t_cpath->cp_conn == conn) 410 return true; 411 } 412 return false; 413 } 414 415 static void rds_tcp_set_unloading(void) 416 { 417 atomic_set(&rds_tcp_unloading, 1); 418 } 419 420 static bool rds_tcp_is_unloading(struct rds_connection *conn) 421 { 422 return atomic_read(&rds_tcp_unloading) != 0; 423 } 424 425 static void rds_tcp_destroy_conns(void) 426 { 427 struct rds_tcp_connection *tc, *_tc; 428 LIST_HEAD(tmp_list); 429 430 /* avoid calling conn_destroy with irqs off */ 431 spin_lock_irq(&rds_tcp_conn_lock); 432 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) { 433 if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn)) 434 list_move_tail(&tc->t_tcp_node, &tmp_list); 435 } 436 spin_unlock_irq(&rds_tcp_conn_lock); 437 438 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node) 439 rds_conn_destroy(tc->t_cpath->cp_conn); 440 } 441 442 static void rds_tcp_exit(void); 443 444 struct rds_transport rds_tcp_transport = { 445 .laddr_check = rds_tcp_laddr_check, 446 .xmit_path_prepare = rds_tcp_xmit_path_prepare, 447 .xmit_path_complete = rds_tcp_xmit_path_complete, 448 .xmit = rds_tcp_xmit, 449 .recv_path = rds_tcp_recv_path, 450 .conn_alloc = rds_tcp_conn_alloc, 451 .conn_free = rds_tcp_conn_free, 452 .conn_path_connect = rds_tcp_conn_path_connect, 453 .conn_path_shutdown = rds_tcp_conn_path_shutdown, 454 .inc_copy_to_user = rds_tcp_inc_copy_to_user, 455 .inc_free = rds_tcp_inc_free, 456 .stats_info_copy = rds_tcp_stats_info_copy, 457 .exit = rds_tcp_exit, 458 .t_owner = THIS_MODULE, 459 .t_name = "tcp", 460 .t_type = RDS_TRANS_TCP, 461 .t_prefer_loopback = 1, 462 .t_mp_capable = 1, 463 .t_unloading = rds_tcp_is_unloading, 464 }; 465 466 static unsigned int rds_tcp_netid; 467 468 /* per-network namespace private data for this module */ 469 struct rds_tcp_net { 470 struct socket *rds_tcp_listen_sock; 471 struct work_struct rds_tcp_accept_w; 472 struct ctl_table_header *rds_tcp_sysctl; 473 struct ctl_table *ctl_table; 474 int sndbuf_size; 475 int rcvbuf_size; 476 }; 477 478 /* All module specific customizations to the RDS-TCP socket should be done in 479 * rds_tcp_tune() and applied after socket creation. 480 */ 481 void rds_tcp_tune(struct socket *sock) 482 { 483 struct sock *sk = sock->sk; 484 struct net *net = sock_net(sk); 485 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid); 486 487 rds_tcp_nonagle(sock); 488 lock_sock(sk); 489 if (rtn->sndbuf_size > 0) { 490 sk->sk_sndbuf = rtn->sndbuf_size; 491 sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 492 } 493 if (rtn->rcvbuf_size > 0) { 494 sk->sk_sndbuf = rtn->rcvbuf_size; 495 sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 496 } 497 release_sock(sk); 498 } 499 500 static void rds_tcp_accept_worker(struct work_struct *work) 501 { 502 struct rds_tcp_net *rtn = container_of(work, 503 struct rds_tcp_net, 504 rds_tcp_accept_w); 505 506 while (rds_tcp_accept_one(rtn->rds_tcp_listen_sock) == 0) 507 cond_resched(); 508 } 509 510 void rds_tcp_accept_work(struct sock *sk) 511 { 512 struct net *net = sock_net(sk); 513 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid); 514 515 queue_work(rds_wq, &rtn->rds_tcp_accept_w); 516 } 517 518 static __net_init int rds_tcp_init_net(struct net *net) 519 { 520 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid); 521 struct ctl_table *tbl; 522 int err = 0; 523 524 memset(rtn, 0, sizeof(*rtn)); 525 526 /* {snd, rcv}buf_size default to 0, which implies we let the 527 * stack pick the value, and permit auto-tuning of buffer size. 528 */ 529 if (net == &init_net) { 530 tbl = rds_tcp_sysctl_table; 531 } else { 532 tbl = kmemdup(rds_tcp_sysctl_table, 533 sizeof(rds_tcp_sysctl_table), GFP_KERNEL); 534 if (!tbl) { 535 pr_warn("could not set allocate syctl table\n"); 536 return -ENOMEM; 537 } 538 rtn->ctl_table = tbl; 539 } 540 tbl[RDS_TCP_SNDBUF].data = &rtn->sndbuf_size; 541 tbl[RDS_TCP_RCVBUF].data = &rtn->rcvbuf_size; 542 rtn->rds_tcp_sysctl = register_net_sysctl(net, "net/rds/tcp", tbl); 543 if (!rtn->rds_tcp_sysctl) { 544 pr_warn("could not register sysctl\n"); 545 err = -ENOMEM; 546 goto fail; 547 } 548 rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net, true); 549 if (!rtn->rds_tcp_listen_sock) { 550 pr_warn("could not set up IPv6 listen sock\n"); 551 552 /* Try IPv4 as some systems disable IPv6 */ 553 rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net, false); 554 if (!rtn->rds_tcp_listen_sock) { 555 unregister_net_sysctl_table(rtn->rds_tcp_sysctl); 556 rtn->rds_tcp_sysctl = NULL; 557 err = -EAFNOSUPPORT; 558 goto fail; 559 } 560 } 561 INIT_WORK(&rtn->rds_tcp_accept_w, rds_tcp_accept_worker); 562 return 0; 563 564 fail: 565 if (net != &init_net) 566 kfree(tbl); 567 return err; 568 } 569 570 static void rds_tcp_kill_sock(struct net *net) 571 { 572 struct rds_tcp_connection *tc, *_tc; 573 LIST_HEAD(tmp_list); 574 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid); 575 struct socket *lsock = rtn->rds_tcp_listen_sock; 576 577 rtn->rds_tcp_listen_sock = NULL; 578 rds_tcp_listen_stop(lsock, &rtn->rds_tcp_accept_w); 579 spin_lock_irq(&rds_tcp_conn_lock); 580 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) { 581 struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net); 582 583 if (net != c_net || !tc->t_sock) 584 continue; 585 if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn)) { 586 list_move_tail(&tc->t_tcp_node, &tmp_list); 587 } else { 588 list_del(&tc->t_tcp_node); 589 tc->t_tcp_node_detached = true; 590 } 591 } 592 spin_unlock_irq(&rds_tcp_conn_lock); 593 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node) 594 rds_conn_destroy(tc->t_cpath->cp_conn); 595 } 596 597 static void __net_exit rds_tcp_exit_net(struct net *net) 598 { 599 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid); 600 601 rds_tcp_kill_sock(net); 602 603 if (rtn->rds_tcp_sysctl) 604 unregister_net_sysctl_table(rtn->rds_tcp_sysctl); 605 606 if (net != &init_net && rtn->ctl_table) 607 kfree(rtn->ctl_table); 608 } 609 610 static struct pernet_operations rds_tcp_net_ops = { 611 .init = rds_tcp_init_net, 612 .exit = rds_tcp_exit_net, 613 .id = &rds_tcp_netid, 614 .size = sizeof(struct rds_tcp_net), 615 }; 616 617 void *rds_tcp_listen_sock_def_readable(struct net *net) 618 { 619 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid); 620 struct socket *lsock = rtn->rds_tcp_listen_sock; 621 622 if (!lsock) 623 return NULL; 624 625 return lsock->sk->sk_user_data; 626 } 627 628 /* when sysctl is used to modify some kernel socket parameters,this 629 * function resets the RDS connections in that netns so that we can 630 * restart with new parameters. The assumption is that such reset 631 * events are few and far-between. 632 */ 633 static void rds_tcp_sysctl_reset(struct net *net) 634 { 635 struct rds_tcp_connection *tc, *_tc; 636 637 spin_lock_irq(&rds_tcp_conn_lock); 638 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) { 639 struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net); 640 641 if (net != c_net || !tc->t_sock) 642 continue; 643 644 /* reconnect with new parameters */ 645 rds_conn_path_drop(tc->t_cpath, false); 646 } 647 spin_unlock_irq(&rds_tcp_conn_lock); 648 } 649 650 static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write, 651 void __user *buffer, size_t *lenp, 652 loff_t *fpos) 653 { 654 struct net *net = current->nsproxy->net_ns; 655 int err; 656 657 err = proc_dointvec_minmax(ctl, write, buffer, lenp, fpos); 658 if (err < 0) { 659 pr_warn("Invalid input. Must be >= %d\n", 660 *(int *)(ctl->extra1)); 661 return err; 662 } 663 if (write) 664 rds_tcp_sysctl_reset(net); 665 return 0; 666 } 667 668 static void rds_tcp_exit(void) 669 { 670 rds_tcp_set_unloading(); 671 synchronize_rcu(); 672 rds_info_deregister_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info); 673 rds_info_deregister_func(RDS6_INFO_TCP_SOCKETS, rds6_tcp_tc_info); 674 unregister_pernet_device(&rds_tcp_net_ops); 675 rds_tcp_destroy_conns(); 676 rds_trans_unregister(&rds_tcp_transport); 677 rds_tcp_recv_exit(); 678 kmem_cache_destroy(rds_tcp_conn_slab); 679 } 680 module_exit(rds_tcp_exit); 681 682 static int rds_tcp_init(void) 683 { 684 int ret; 685 686 rds_tcp_conn_slab = kmem_cache_create("rds_tcp_connection", 687 sizeof(struct rds_tcp_connection), 688 0, 0, NULL); 689 if (!rds_tcp_conn_slab) { 690 ret = -ENOMEM; 691 goto out; 692 } 693 694 ret = rds_tcp_recv_init(); 695 if (ret) 696 goto out_slab; 697 698 ret = register_pernet_device(&rds_tcp_net_ops); 699 if (ret) 700 goto out_recv; 701 702 rds_trans_register(&rds_tcp_transport); 703 704 rds_info_register_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info); 705 rds_info_register_func(RDS6_INFO_TCP_SOCKETS, rds6_tcp_tc_info); 706 707 goto out; 708 out_recv: 709 rds_tcp_recv_exit(); 710 out_slab: 711 kmem_cache_destroy(rds_tcp_conn_slab); 712 out: 713 return ret; 714 } 715 module_init(rds_tcp_init); 716 717 MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>"); 718 MODULE_DESCRIPTION("RDS: TCP transport"); 719 MODULE_LICENSE("Dual BSD/GPL"); 720