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