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