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