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 /* Track rds_tcp_connection structs so they can be cleaned up */ 50 static DEFINE_SPINLOCK(rds_tcp_conn_lock); 51 static LIST_HEAD(rds_tcp_conn_list); 52 static atomic_t rds_tcp_unloading = ATOMIC_INIT(0); 53 54 static struct kmem_cache *rds_tcp_conn_slab; 55 56 static int rds_tcp_sndbuf_handler(const struct ctl_table *ctl, int write, 57 void *buffer, size_t *lenp, loff_t *fpos); 58 static int rds_tcp_rcvbuf_handler(const struct ctl_table *ctl, int write, 59 void *buffer, size_t *lenp, loff_t *fpos); 60 61 static int rds_tcp_min_sndbuf = SOCK_MIN_SNDBUF; 62 static int rds_tcp_min_rcvbuf = SOCK_MIN_RCVBUF; 63 64 static struct ctl_table rds_tcp_sysctl_table[] = { 65 #define RDS_TCP_SNDBUF 0 66 { 67 .procname = "rds_tcp_sndbuf", 68 /* data is per-net pointer */ 69 .maxlen = sizeof(int), 70 .mode = 0644, 71 .proc_handler = rds_tcp_sndbuf_handler, 72 .extra1 = &rds_tcp_min_sndbuf, 73 }, 74 #define RDS_TCP_RCVBUF 1 75 { 76 .procname = "rds_tcp_rcvbuf", 77 /* data is per-net pointer */ 78 .maxlen = sizeof(int), 79 .mode = 0644, 80 .proc_handler = rds_tcp_rcvbuf_handler, 81 .extra1 = &rds_tcp_min_rcvbuf, 82 }, 83 }; 84 85 u32 rds_tcp_write_seq(struct rds_tcp_connection *tc) 86 { 87 /* seq# of the last byte of data in tcp send buffer */ 88 return tcp_sk(tc->t_sock->sk)->write_seq; 89 } 90 91 u32 rds_tcp_snd_una(struct rds_tcp_connection *tc) 92 { 93 return tcp_sk(tc->t_sock->sk)->snd_una; 94 } 95 96 void rds_tcp_restore_callbacks(struct socket *sock, 97 struct rds_tcp_connection *tc) 98 { 99 rdsdebug("restoring sock %p callbacks from tc %p\n", sock, tc); 100 write_lock_bh(&sock->sk->sk_callback_lock); 101 102 /* done under the callback_lock to serialize with write_space */ 103 spin_lock(&rds_tcp_tc_list_lock); 104 list_del_init(&tc->t_list_item); 105 spin_unlock(&rds_tcp_tc_list_lock); 106 107 tc->t_sock = NULL; 108 109 sock->sk->sk_write_space = tc->t_orig_write_space; 110 sock->sk->sk_data_ready = tc->t_orig_data_ready; 111 sock->sk->sk_state_change = tc->t_orig_state_change; 112 sock->sk->sk_user_data = NULL; 113 114 write_unlock_bh(&sock->sk->sk_callback_lock); 115 } 116 117 /* 118 * rds_tcp_reset_callbacks() switches a path to a new socket and 119 * releases the old one it finds in tc->t_sock, resolving a duelling 120 * SYN. 121 * 122 * tc->t_sock is set by rds_tcp_set_callbacks() and cleared by 123 * rds_tcp_restore_callbacks(). Four paths write it: the active 124 * connect in rds_tcp_conn_path_connect(), which sets it and clears it 125 * again on failure; the accept path in rds_tcp_accept_one(), which 126 * sets it for a path with no socket yet; the teardown in 127 * rds_tcp_conn_path_shutdown(), which clears it; and the swap done 128 * here, which does both. The connect and accept paths are serialized 129 * against each other by t_conn_path_lock. Send and receive trust 130 * that it is set: the absence of RDS_CONN_UP protects those paths 131 * from being called while it isn't, and the swap done here runs under 132 * RDS_IN_XMIT so that it cannot interleave with a sender already 133 * inside rds_send_xmit(). 134 */ 135 void rds_tcp_reset_callbacks(struct socket *sock, 136 struct rds_conn_path *cp) 137 { 138 struct rds_tcp_connection *tc = cp->cp_transport_data; 139 struct socket *osock; 140 141 /* Need to resolve a duelling SYN between peers. 142 * We have an outstanding SYN to this peer, which may 143 * potentially have transitioned to the RDS_CONN_UP state, 144 * so we must quiesce any send threads before resetting 145 * cp_transport_data. Setting cp_state to something other 146 * than RDS_CONN_UP stops new senders, and owning RDS_IN_XMIT 147 * excludes any thread already inside rds_send_xmit() - or a 148 * teardown in rds_conn_shutdown(), which holds the same lock 149 * for the duration of the transport shutdown - for the whole 150 * socket swap and the rds_send_path_reset() below. 151 * 152 * An incoming syn-ack at this point would end up marking the 153 * conn as RDS_CONN_UP, and would again permit rds_send_xmit() 154 * threads through, so ideally we would synchronize on 155 * RDS_CONN_UP after lock_sock(), but cannot do that: acquiring 156 * RDS_IN_XMIT after lock_sock() may end up deadlocking with 157 * tcp_sendmsg(), which takes the socket lock while holding 158 * RDS_IN_XMIT. As a result, we set c_state to 159 * RDS_CONN_RESETTING, to ensure that rds_tcp_state_change 160 * cannot mark rds_conn_path_up() in the window before 161 * lock_sock(). 162 * 163 * Only make that transition if the path is still connecting 164 * (or already resetting from an earlier duel). A path in any 165 * other state - typically RDS_CONN_DISCONNECTING or 166 * RDS_CONN_ERROR with a shutdown in flight - is dropped 167 * instead. That still replaces its state, with RDS_CONN_ERROR, 168 * and, unless a pending destroy is about to reap the whole 169 * connection anyway, queues one more shutdown pass. A shutdown 170 * already in flight leaves that RDS_CONN_ERROR alone when it 171 * finishes; the queued pass then completes the transition to 172 * RDS_CONN_DOWN and tears down anything that attached to the 173 * path in the meantime. 174 */ 175 if (!rds_conn_path_transition(cp, RDS_CONN_CONNECTING, 176 RDS_CONN_RESETTING) && 177 !rds_conn_path_transition(cp, RDS_CONN_RESETTING, 178 RDS_CONN_RESETTING)) 179 rds_conn_path_drop(cp, 0); 180 wait_event(cp->cp_waitq, 181 !test_and_set_bit_lock(RDS_IN_XMIT, &cp->cp_flags)); 182 183 /* Read t_sock only while owning RDS_IN_XMIT, never before the 184 * wait: the teardown in rds_conn_shutdown() releases the old 185 * socket and clears t_sock, so a pointer sampled earlier can 186 * be stale by the time we wake up. The teardown holds the 187 * same lock while it does so, so what we read here cannot 188 * change under us until we release it. 189 */ 190 osock = tc->t_sock; 191 if (!osock) 192 goto newsock; 193 194 /* reset receive side state for rds_tcp_data_recv() for osock. 195 * 196 * The sync cancels while owning RDS_IN_XMIT rely on cp_wq 197 * being ordered: a teardown blocked on the bit occupies 198 * cp_wq's only execution slot, so cp_send_w and cp_recv_w are 199 * pending at most and the cancels never flush. Nothing here 200 * may flush or wait on cp_wq itself. 201 */ 202 cancel_delayed_work_sync(&cp->cp_send_w); 203 cancel_delayed_work_sync(&cp->cp_recv_w); 204 lock_sock(osock->sk); 205 if (tc->t_tinc) { 206 rds_inc_put(&tc->t_tinc->ti_inc); 207 tc->t_tinc = NULL; 208 } 209 tc->t_tinc_hdr_rem = sizeof(struct rds_header); 210 tc->t_tinc_data_rem = 0; 211 rds_tcp_restore_callbacks(osock, tc); 212 release_sock(osock->sk); 213 sock_release(osock); 214 newsock: 215 rds_send_path_reset(cp); 216 lock_sock(sock->sk); 217 rds_tcp_set_callbacks(sock, cp); 218 release_sock(sock->sk); 219 220 clear_bit_unlock(RDS_IN_XMIT, &cp->cp_flags); 221 wake_up_all(&cp->cp_waitq); 222 } 223 224 /* Add tc to rds_tcp_tc_list and set tc->t_sock. See comments 225 * above rds_tcp_reset_callbacks for notes about synchronization 226 * with data path 227 */ 228 void rds_tcp_set_callbacks(struct socket *sock, struct rds_conn_path *cp) 229 { 230 struct rds_tcp_connection *tc = cp->cp_transport_data; 231 232 rdsdebug("setting sock %p callbacks to tc %p\n", sock, tc); 233 write_lock_bh(&sock->sk->sk_callback_lock); 234 235 /* done under the callback_lock to serialize with write_space. 236 * Set t_sock inside rds_tcp_tc_list_lock so readers walking 237 * rds_tcp_tc_list under the same lock cannot observe an 238 * entry whose t_sock is NULL. 239 */ 240 spin_lock(&rds_tcp_tc_list_lock); 241 tc->t_sock = sock; 242 list_add_tail(&tc->t_list_item, &rds_tcp_tc_list); 243 spin_unlock(&rds_tcp_tc_list_lock); 244 245 /* accepted sockets need our listen data ready undone */ 246 if (sock->sk->sk_data_ready == rds_tcp_listen_data_ready) 247 sock->sk->sk_data_ready = sock->sk->sk_user_data; 248 if (!tc->t_rtn) 249 tc->t_rtn = net_generic(sock_net(sock->sk), rds_tcp_netid); 250 tc->t_cpath = cp; 251 tc->t_orig_data_ready = sock->sk->sk_data_ready; 252 tc->t_orig_write_space = sock->sk->sk_write_space; 253 tc->t_orig_state_change = sock->sk->sk_state_change; 254 255 sock->sk->sk_user_data = cp; 256 sock->sk->sk_data_ready = rds_tcp_data_ready; 257 sock->sk->sk_write_space = rds_tcp_write_space; 258 sock->sk->sk_state_change = rds_tcp_state_change; 259 260 write_unlock_bh(&sock->sk->sk_callback_lock); 261 } 262 263 /* Handle RDS_INFO_TCP_SOCKETS socket option. It only returns IPv4 264 * connections for backward compatibility. 265 */ 266 static void rds_tcp_tc_info(struct socket *rds_sock, unsigned int len, 267 struct rds_info_iterator *iter, 268 struct rds_info_lengths *lens) 269 { 270 struct net *net = sock_net(rds_sock->sk); 271 struct rds_info_tcp_socket tsinfo; 272 struct rds_tcp_connection *tc; 273 unsigned int copied = 0; 274 unsigned int cnt = 0; 275 unsigned long flags; 276 277 spin_lock_irqsave(&rds_tcp_tc_list_lock, flags); 278 279 /* First pass: count entries visible in the caller's netns. */ 280 list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) { 281 if (tc->t_cpath->cp_conn->c_isv6) 282 continue; 283 if (!net_eq(rds_conn_net(tc->t_cpath->cp_conn), net)) 284 continue; 285 cnt++; 286 } 287 288 if (len / sizeof(tsinfo) < cnt) 289 goto out; 290 291 list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) { 292 struct inet_sock *inet = inet_sk(tc->t_sock->sk); 293 294 if (copied >= cnt) 295 break; 296 if (tc->t_cpath->cp_conn->c_isv6) 297 continue; 298 /* Only show connections in the caller's netns. */ 299 if (!net_eq(rds_conn_net(tc->t_cpath->cp_conn), net)) 300 continue; 301 302 tsinfo.local_addr = inet->inet_saddr; 303 tsinfo.local_port = inet->inet_sport; 304 tsinfo.peer_addr = inet->inet_daddr; 305 tsinfo.peer_port = inet->inet_dport; 306 307 tsinfo.hdr_rem = tc->t_tinc_hdr_rem; 308 tsinfo.data_rem = tc->t_tinc_data_rem; 309 tsinfo.last_sent_nxt = tc->t_last_sent_nxt; 310 tsinfo.last_expected_una = tc->t_last_expected_una; 311 tsinfo.last_seen_una = tc->t_last_seen_una; 312 tsinfo.tos = tc->t_cpath->cp_conn->c_tos; 313 314 rds_info_copy(iter, &tsinfo, sizeof(tsinfo)); 315 copied++; 316 } 317 cnt = copied; 318 319 out: 320 lens->nr = cnt; 321 lens->each = sizeof(tsinfo); 322 323 spin_unlock_irqrestore(&rds_tcp_tc_list_lock, flags); 324 } 325 326 #if IS_ENABLED(CONFIG_IPV6) 327 /* Handle RDS6_INFO_TCP_SOCKETS socket option. It returns both IPv4 and 328 * IPv6 connections. IPv4 connection address is returned in an IPv4 mapped 329 * address. 330 */ 331 static void rds6_tcp_tc_info(struct socket *sock, unsigned int len, 332 struct rds_info_iterator *iter, 333 struct rds_info_lengths *lens) 334 { 335 struct net *net = sock_net(sock->sk); 336 struct rds6_info_tcp_socket tsinfo6; 337 struct rds_tcp_connection *tc; 338 unsigned int copied = 0; 339 unsigned int cnt = 0; 340 unsigned long flags; 341 342 spin_lock_irqsave(&rds_tcp_tc_list_lock, flags); 343 344 /* First pass: count entries visible in the caller's netns. */ 345 list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) { 346 if (!net_eq(rds_conn_net(tc->t_cpath->cp_conn), net)) 347 continue; 348 cnt++; 349 } 350 351 if (len / sizeof(tsinfo6) < cnt) 352 goto out; 353 354 list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) { 355 struct sock *sk = tc->t_sock->sk; 356 struct inet_sock *inet = inet_sk(sk); 357 358 if (copied >= cnt) 359 break; 360 /* Only show connections in the caller's netns. */ 361 if (!net_eq(rds_conn_net(tc->t_cpath->cp_conn), net)) 362 continue; 363 364 tsinfo6.local_addr = sk->sk_v6_rcv_saddr; 365 tsinfo6.local_port = inet->inet_sport; 366 tsinfo6.peer_addr = sk->sk_v6_daddr; 367 tsinfo6.peer_port = inet->inet_dport; 368 369 tsinfo6.hdr_rem = tc->t_tinc_hdr_rem; 370 tsinfo6.data_rem = tc->t_tinc_data_rem; 371 tsinfo6.last_sent_nxt = tc->t_last_sent_nxt; 372 tsinfo6.last_expected_una = tc->t_last_expected_una; 373 tsinfo6.last_seen_una = tc->t_last_seen_una; 374 375 rds_info_copy(iter, &tsinfo6, sizeof(tsinfo6)); 376 copied++; 377 } 378 cnt = copied; 379 380 out: 381 lens->nr = cnt; 382 lens->each = sizeof(tsinfo6); 383 384 spin_unlock_irqrestore(&rds_tcp_tc_list_lock, flags); 385 } 386 #endif 387 388 int rds_tcp_laddr_check(struct net *net, const struct in6_addr *addr, 389 __u32 scope_id) 390 { 391 struct net_device *dev = NULL; 392 #if IS_ENABLED(CONFIG_IPV6) 393 int ret; 394 #endif 395 396 if (ipv6_addr_v4mapped(addr)) { 397 if (inet_addr_type(net, addr->s6_addr32[3]) == RTN_LOCAL) 398 return 0; 399 return -EADDRNOTAVAIL; 400 } 401 402 /* If the scope_id is specified, check only those addresses 403 * hosted on the specified interface. 404 */ 405 rcu_read_lock(); 406 if (scope_id != 0) { 407 dev = dev_get_by_index_rcu(net, scope_id); 408 /* scope_id is not valid... */ 409 if (!dev) { 410 rcu_read_unlock(); 411 return -EADDRNOTAVAIL; 412 } 413 } 414 #if IS_ENABLED(CONFIG_IPV6) 415 if (ipv6_mod_enabled()) { 416 ret = ipv6_chk_addr(net, addr, dev, 0); 417 if (ret) { 418 rcu_read_unlock(); 419 return 0; 420 } 421 } 422 #endif 423 rcu_read_unlock(); 424 return -EADDRNOTAVAIL; 425 } 426 427 static void rds_tcp_conn_free(void *arg) 428 { 429 struct rds_tcp_connection *tc = arg; 430 unsigned long flags; 431 432 rdsdebug("freeing tc %p\n", tc); 433 434 spin_lock_irqsave(&rds_tcp_conn_lock, flags); 435 if (!tc->t_tcp_node_detached) 436 list_del(&tc->t_tcp_node); 437 spin_unlock_irqrestore(&rds_tcp_conn_lock, flags); 438 439 kmem_cache_free(rds_tcp_conn_slab, tc); 440 } 441 442 static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp) 443 { 444 struct rds_tcp_connection *tc; 445 int i, j; 446 int ret = 0; 447 448 for (i = 0; i < RDS_MPATH_WORKERS; i++) { 449 tc = kmem_cache_zalloc(rds_tcp_conn_slab, gfp); 450 if (!tc) { 451 ret = -ENOMEM; 452 goto fail; 453 } 454 mutex_init(&tc->t_conn_path_lock); 455 tc->t_sock = NULL; 456 tc->t_rtn = NULL; 457 tc->t_tinc = NULL; 458 tc->t_tinc_hdr_rem = sizeof(struct rds_header); 459 tc->t_tinc_data_rem = 0; 460 init_waitqueue_head(&tc->t_recv_done_waitq); 461 462 conn->c_path[i].cp_transport_data = tc; 463 tc->t_cpath = &conn->c_path[i]; 464 tc->t_tcp_node_detached = true; 465 466 rdsdebug("rds_conn_path [%d] tc %p\n", i, 467 conn->c_path[i].cp_transport_data); 468 } 469 spin_lock_irq(&rds_tcp_conn_lock); 470 for (i = 0; i < RDS_MPATH_WORKERS; i++) { 471 tc = conn->c_path[i].cp_transport_data; 472 tc->t_tcp_node_detached = false; 473 list_add_tail(&tc->t_tcp_node, &rds_tcp_conn_list); 474 } 475 spin_unlock_irq(&rds_tcp_conn_lock); 476 fail: 477 if (ret) { 478 for (j = 0; j < i; j++) 479 rds_tcp_conn_free(conn->c_path[j].cp_transport_data); 480 } 481 return ret; 482 } 483 484 static bool list_has_conn(struct list_head *list, struct rds_connection *conn) 485 { 486 struct rds_tcp_connection *tc, *_tc; 487 488 list_for_each_entry_safe(tc, _tc, list, t_tcp_node) { 489 if (tc->t_cpath->cp_conn == conn) 490 return true; 491 } 492 return false; 493 } 494 495 static void rds_tcp_set_unloading(void) 496 { 497 atomic_set(&rds_tcp_unloading, 1); 498 } 499 500 static bool rds_tcp_is_unloading(struct rds_connection *conn) 501 { 502 return atomic_read(&rds_tcp_unloading) != 0; 503 } 504 505 static void rds_tcp_destroy_conns(void) 506 { 507 struct rds_tcp_connection *tc, *_tc; 508 LIST_HEAD(tmp_list); 509 510 /* avoid calling conn_destroy with irqs off */ 511 spin_lock_irq(&rds_tcp_conn_lock); 512 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) { 513 if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn)) 514 list_move_tail(&tc->t_tcp_node, &tmp_list); 515 } 516 spin_unlock_irq(&rds_tcp_conn_lock); 517 518 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node) 519 rds_conn_destroy(tc->t_cpath->cp_conn); 520 } 521 522 static void rds_tcp_exit(void); 523 524 static u8 rds_tcp_get_tos_map(u8 tos) 525 { 526 /* all user tos mapped to default 0 for TCP transport */ 527 return 0; 528 } 529 530 struct rds_transport rds_tcp_transport = { 531 .laddr_check = rds_tcp_laddr_check, 532 .xmit_path_prepare = rds_tcp_xmit_path_prepare, 533 .xmit_path_complete = rds_tcp_xmit_path_complete, 534 .xmit = rds_tcp_xmit, 535 .recv_path = rds_tcp_recv_path, 536 .conn_alloc = rds_tcp_conn_alloc, 537 .conn_free = rds_tcp_conn_free, 538 .conn_slots_available = rds_tcp_conn_slots_available, 539 .conn_path_connect = rds_tcp_conn_path_connect, 540 .conn_path_shutdown = rds_tcp_conn_path_shutdown, 541 .inc_copy_to_user = rds_tcp_inc_copy_to_user, 542 .inc_free = rds_tcp_inc_free, 543 .stats_info_copy = rds_tcp_stats_info_copy, 544 .exit = rds_tcp_exit, 545 .get_tos_map = rds_tcp_get_tos_map, 546 .t_owner = THIS_MODULE, 547 .t_name = "tcp", 548 .t_type = RDS_TRANS_TCP, 549 .t_prefer_loopback = 1, 550 .t_mp_capable = 1, 551 .t_unloading = rds_tcp_is_unloading, 552 }; 553 554 int rds_tcp_netid; 555 556 /* All module specific customizations to the RDS-TCP socket should be done in 557 * rds_tcp_tune() and applied after socket creation. 558 */ 559 bool rds_tcp_tune(struct socket *sock) 560 { 561 struct sock *sk = sock->sk; 562 struct net *net = sock_net(sk); 563 struct rds_tcp_net *rtn; 564 565 tcp_sock_set_nodelay(sock->sk); 566 /* TCP timer functions might access net namespace even after 567 * a process which created this net namespace terminated. 568 */ 569 if (!sk->sk_net_refcnt) { 570 if (!maybe_get_net(net)) 571 return false; 572 /* 573 * sk_net_refcnt_upgrade() must be called before lock_sock() 574 * because it does a GFP_KERNEL allocation, which can trigger 575 * fs_reclaim and create a circular lock dependency with the 576 * socket lock. The fields it modifies (sk_net_refcnt, 577 * ns_tracker) are not accessed by any concurrent code path 578 * at this point. 579 */ 580 sk_net_refcnt_upgrade(sk); 581 put_net(net); 582 } 583 lock_sock(sk); 584 rtn = net_generic(net, rds_tcp_netid); 585 if (rtn->sndbuf_size > 0) { 586 sk->sk_sndbuf = rtn->sndbuf_size; 587 sk->sk_userlocks |= SOCK_SNDBUF_LOCK; 588 } 589 if (rtn->rcvbuf_size > 0) { 590 sk->sk_rcvbuf = rtn->rcvbuf_size; 591 sk->sk_userlocks |= SOCK_RCVBUF_LOCK; 592 } 593 release_sock(sk); 594 return true; 595 } 596 597 static void rds_tcp_accept_worker(struct work_struct *work) 598 { 599 struct rds_tcp_net *rtn = container_of(work, 600 struct rds_tcp_net, 601 rds_tcp_accept_w); 602 603 while (rds_tcp_accept_one(rtn) == 0) 604 cond_resched(); 605 } 606 607 void rds_tcp_accept_work(struct rds_tcp_net *rtn) 608 { 609 queue_work(rds_wq, &rtn->rds_tcp_accept_w); 610 } 611 612 static __net_init int rds_tcp_init_net(struct net *net) 613 { 614 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid); 615 struct ctl_table *tbl; 616 int err = 0; 617 618 memset(rtn, 0, sizeof(*rtn)); 619 620 mutex_init(&rtn->rds_tcp_accept_lock); 621 622 /* {snd, rcv}buf_size default to 0, which implies we let the 623 * stack pick the value, and permit auto-tuning of buffer size. 624 */ 625 if (net == &init_net) { 626 tbl = rds_tcp_sysctl_table; 627 } else { 628 tbl = kmemdup(rds_tcp_sysctl_table, 629 sizeof(rds_tcp_sysctl_table), GFP_KERNEL); 630 if (!tbl) { 631 pr_warn("could not set allocate sysctl table\n"); 632 return -ENOMEM; 633 } 634 rtn->ctl_table = tbl; 635 } 636 tbl[RDS_TCP_SNDBUF].data = &rtn->sndbuf_size; 637 tbl[RDS_TCP_RCVBUF].data = &rtn->rcvbuf_size; 638 rtn->rds_tcp_sysctl = register_net_sysctl_sz(net, "net/rds/tcp", tbl, 639 ARRAY_SIZE(rds_tcp_sysctl_table)); 640 if (!rtn->rds_tcp_sysctl) { 641 pr_warn("could not register sysctl\n"); 642 err = -ENOMEM; 643 goto fail; 644 } 645 646 #if IS_ENABLED(CONFIG_IPV6) 647 rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net, true); 648 #else 649 rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net, false); 650 #endif 651 if (!rtn->rds_tcp_listen_sock) { 652 pr_warn("could not set up IPv6 listen sock\n"); 653 654 #if IS_ENABLED(CONFIG_IPV6) 655 /* Try IPv4 as some systems disable IPv6 */ 656 rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net, false); 657 if (!rtn->rds_tcp_listen_sock) { 658 #endif 659 unregister_net_sysctl_table(rtn->rds_tcp_sysctl); 660 rtn->rds_tcp_sysctl = NULL; 661 err = -EAFNOSUPPORT; 662 goto fail; 663 #if IS_ENABLED(CONFIG_IPV6) 664 } 665 #endif 666 } 667 INIT_WORK(&rtn->rds_tcp_accept_w, rds_tcp_accept_worker); 668 return 0; 669 670 fail: 671 if (net != &init_net) 672 kfree(tbl); 673 return err; 674 } 675 676 static void rds_tcp_kill_sock(struct net *net) 677 { 678 struct rds_tcp_connection *tc, *_tc; 679 LIST_HEAD(tmp_list); 680 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid); 681 struct socket *lsock = rtn->rds_tcp_listen_sock; 682 683 rtn->rds_tcp_listen_sock = NULL; 684 rds_tcp_listen_stop(lsock, &rtn->rds_tcp_accept_w); 685 if (rtn->rds_tcp_accepted_sock) 686 sock_release(rtn->rds_tcp_accepted_sock); 687 spin_lock_irq(&rds_tcp_conn_lock); 688 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) { 689 struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net); 690 691 if (net != c_net) 692 continue; 693 if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn)) { 694 list_move_tail(&tc->t_tcp_node, &tmp_list); 695 } else { 696 list_del(&tc->t_tcp_node); 697 tc->t_tcp_node_detached = true; 698 } 699 } 700 spin_unlock_irq(&rds_tcp_conn_lock); 701 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node) 702 rds_conn_destroy(tc->t_cpath->cp_conn); 703 } 704 705 static void __net_exit rds_tcp_exit_net(struct net *net) 706 { 707 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid); 708 709 if (rtn->rds_tcp_sysctl) 710 unregister_net_sysctl_table(rtn->rds_tcp_sysctl); 711 712 if (net != &init_net) 713 kfree(rtn->ctl_table); 714 715 rds_tcp_kill_sock(net); 716 } 717 718 static struct pernet_operations rds_tcp_net_ops = { 719 .init = rds_tcp_init_net, 720 .exit = rds_tcp_exit_net, 721 .id = &rds_tcp_netid, 722 .size = sizeof(struct rds_tcp_net), 723 }; 724 725 void *rds_tcp_listen_sock_def_readable(struct net *net) 726 { 727 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid); 728 struct socket *lsock = rtn->rds_tcp_listen_sock; 729 730 if (!lsock) 731 return NULL; 732 733 return lsock->sk->sk_user_data; 734 } 735 736 /* when sysctl is used to modify some kernel socket parameters,this 737 * function resets the RDS connections in that netns so that we can 738 * restart with new parameters. The assumption is that such reset 739 * events are few and far-between. 740 */ 741 static void rds_tcp_sysctl_reset(struct net *net) 742 { 743 struct rds_tcp_connection *tc, *_tc; 744 745 spin_lock_irq(&rds_tcp_conn_lock); 746 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) { 747 struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net); 748 749 if (net != c_net || !tc->t_sock) 750 continue; 751 752 /* reconnect with new parameters */ 753 rds_conn_path_drop(tc->t_cpath, false); 754 } 755 spin_unlock_irq(&rds_tcp_conn_lock); 756 } 757 758 static int rds_tcp_skbuf_handler(struct rds_tcp_net *rtn, 759 const struct ctl_table *ctl, int write, 760 void *buffer, size_t *lenp, loff_t *fpos) 761 { 762 int err; 763 764 err = proc_dointvec_minmax(ctl, write, buffer, lenp, fpos); 765 if (err < 0) { 766 pr_warn("Invalid input. Must be >= %d\n", 767 *(int *)(ctl->extra1)); 768 return err; 769 } 770 771 if (write && rtn->rds_tcp_listen_sock && rtn->rds_tcp_listen_sock->sk) { 772 struct net *net = sock_net(rtn->rds_tcp_listen_sock->sk); 773 774 rds_tcp_sysctl_reset(net); 775 } 776 777 return 0; 778 } 779 780 static int rds_tcp_sndbuf_handler(const struct ctl_table *ctl, int write, 781 void *buffer, size_t *lenp, loff_t *fpos) 782 { 783 struct rds_tcp_net *rtn = container_of(ctl->data, struct rds_tcp_net, 784 sndbuf_size); 785 786 return rds_tcp_skbuf_handler(rtn, ctl, write, buffer, lenp, fpos); 787 } 788 789 static int rds_tcp_rcvbuf_handler(const struct ctl_table *ctl, int write, 790 void *buffer, size_t *lenp, loff_t *fpos) 791 { 792 struct rds_tcp_net *rtn = container_of(ctl->data, struct rds_tcp_net, 793 rcvbuf_size); 794 795 return rds_tcp_skbuf_handler(rtn, ctl, write, buffer, lenp, fpos); 796 } 797 798 static void rds_tcp_exit(void) 799 { 800 rds_tcp_set_unloading(); 801 synchronize_rcu(); 802 rds_info_deregister_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info); 803 #if IS_ENABLED(CONFIG_IPV6) 804 rds_info_deregister_func(RDS6_INFO_TCP_SOCKETS, rds6_tcp_tc_info); 805 #endif 806 unregister_pernet_device(&rds_tcp_net_ops); 807 rds_tcp_destroy_conns(); 808 rds_trans_unregister(&rds_tcp_transport); 809 rds_tcp_recv_exit(); 810 kmem_cache_destroy(rds_tcp_conn_slab); 811 } 812 module_exit(rds_tcp_exit); 813 814 static int __init rds_tcp_init(void) 815 { 816 int ret; 817 818 rds_tcp_conn_slab = KMEM_CACHE(rds_tcp_connection, 0); 819 if (!rds_tcp_conn_slab) { 820 ret = -ENOMEM; 821 goto out; 822 } 823 824 ret = rds_tcp_recv_init(); 825 if (ret) 826 goto out_slab; 827 828 ret = register_pernet_device(&rds_tcp_net_ops); 829 if (ret) 830 goto out_recv; 831 832 rds_trans_register(&rds_tcp_transport); 833 834 rds_info_register_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info); 835 #if IS_ENABLED(CONFIG_IPV6) 836 rds_info_register_func(RDS6_INFO_TCP_SOCKETS, rds6_tcp_tc_info); 837 #endif 838 839 goto out; 840 out_recv: 841 rds_tcp_recv_exit(); 842 out_slab: 843 kmem_cache_destroy(rds_tcp_conn_slab); 844 out: 845 return ret; 846 } 847 module_init(rds_tcp_init); 848 849 MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>"); 850 MODULE_DESCRIPTION("RDS: TCP transport"); 851 MODULE_LICENSE("Dual BSD/GPL"); 852