blob: 7028d6e51947149324bf2976ac75ed5ae2fb0d06 [file] [log] [blame]
Andy Grover70041082009-08-21 12:28:31 +00001/*
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -07002 * Copyright (c) 2006, 2018 Oracle and/or its affiliates. All rights reserved.
Andy Grover70041082009-08-21 12:28:31 +00003 *
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 Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Andy Grover70041082009-08-21 12:28:31 +000035#include <linux/in.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040036#include <linux/module.h>
Andy Grover70041082009-08-21 12:28:31 +000037#include <net/tcp.h>
Sowmini Varadhan467fa152015-08-05 01:43:26 -040038#include <net/net_namespace.h>
39#include <net/netns/generic.h>
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -070040#include <net/tcp.h>
41#include <net/addrconf.h>
Andy Grover70041082009-08-21 12:28:31 +000042
43#include "rds.h"
44#include "tcp.h"
45
46/* only for info exporting */
47static DEFINE_SPINLOCK(rds_tcp_tc_list_lock);
48static LIST_HEAD(rds_tcp_tc_list);
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -070049
50/* rds_tcp_tc_count counts only IPv4 connections.
51 * rds6_tcp_tc_count counts both IPv4 and IPv6 connections.
52 */
stephen hemmingerff51bf82010-10-19 08:08:33 +000053static unsigned int rds_tcp_tc_count;
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -070054static unsigned int rds6_tcp_tc_count;
Andy Grover70041082009-08-21 12:28:31 +000055
56/* Track rds_tcp_connection structs so they can be cleaned up */
57static DEFINE_SPINLOCK(rds_tcp_conn_lock);
58static LIST_HEAD(rds_tcp_conn_list);
Sowmini Varadhanebeeb1a2018-02-03 04:26:51 -080059static atomic_t rds_tcp_unloading = ATOMIC_INIT(0);
Andy Grover70041082009-08-21 12:28:31 +000060
61static struct kmem_cache *rds_tcp_conn_slab;
62
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -070063static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write,
64 void __user *buffer, size_t *lenp,
65 loff_t *fpos);
66
Wei Yongjunaf73e722016-06-17 18:12:46 +000067static int rds_tcp_min_sndbuf = SOCK_MIN_SNDBUF;
68static int rds_tcp_min_rcvbuf = SOCK_MIN_RCVBUF;
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -070069
70static 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 Grover70041082009-08-21 12:28:31 +000092/* doing it this way avoids calling tcp_sk() */
93void rds_tcp_nonagle(struct socket *sock)
94{
Andy Grover70041082009-08-21 12:28:31 +000095 int val = 1;
96
Al Viroe73a67f2017-03-18 21:20:27 -040097 kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (void *)&val,
Andy Grover70041082009-08-21 12:28:31 +000098 sizeof(val));
Andy Grover70041082009-08-21 12:28:31 +000099}
100
Sowmini Varadhanb5895132018-01-18 13:11:07 -0800101u32 rds_tcp_write_seq(struct rds_tcp_connection *tc)
Andy Grover70041082009-08-21 12:28:31 +0000102{
Sowmini Varadhanb5895132018-01-18 13:11:07 -0800103 /* seq# of the last byte of data in tcp send buffer */
104 return tcp_sk(tc->t_sock->sk)->write_seq;
Andy Grover70041082009-08-21 12:28:31 +0000105}
106
107u32 rds_tcp_snd_una(struct rds_tcp_connection *tc)
108{
109 return tcp_sk(tc->t_sock->sk)->snd_una;
110}
111
112void 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 Poon1e2b44e2018-07-23 20:51:22 -0700121 rds6_tcp_tc_count--;
122 if (!tc->t_cpath->cp_conn->c_isv6)
123 rds_tcp_tc_count--;
Andy Grover70041082009-08-21 12:28:31 +0000124 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 Varadhan335b48d2016-06-04 13:59:58 -0700137 * 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 */
145void rds_tcp_reset_callbacks(struct socket *sock,
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700146 struct rds_conn_path *cp)
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700147{
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700148 struct rds_tcp_connection *tc = cp->cp_transport_data;
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700149 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 Varadhanea3b1ea2016-06-30 16:11:14 -0700158 * cp_transport_data. We quiesce these threads by setting
159 * cp_state to something other than RDS_CONN_UP, and then
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700160 * 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 Varadhan9c794402016-06-04 14:00:00 -0700163 *
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 Varadhan335b48d2016-06-04 13:59:58 -0700173 */
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700174 atomic_set(&cp->cp_state, RDS_CONN_RESETTING);
175 wait_event(cp->cp_waitq, !test_bit(RDS_IN_XMIT, &cp->cp_flags));
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700176 lock_sock(osock->sk);
177 /* reset receive side state for rds_tcp_data_recv() for osock */
Sowmini Varadhanac3615e2016-07-14 03:51:02 -0700178 cancel_delayed_work_sync(&cp->cp_send_w);
179 cancel_delayed_work_sync(&cp->cp_recv_w);
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700180 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 Varadhanac3615e2016-07-14 03:51:02 -0700186 rds_tcp_restore_callbacks(osock, tc);
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700187 release_sock(osock->sk);
188 sock_release(osock);
189newsock:
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700190 rds_send_path_reset(cp);
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700191 lock_sock(sock->sk);
Sowmini Varadhanac3615e2016-07-14 03:51:02 -0700192 rds_tcp_set_callbacks(sock, cp);
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700193 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 Grover70041082009-08-21 12:28:31 +0000199 */
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700200void rds_tcp_set_callbacks(struct socket *sock, struct rds_conn_path *cp)
Andy Grover70041082009-08-21 12:28:31 +0000201{
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700202 struct rds_tcp_connection *tc = cp->cp_transport_data;
Andy Grover70041082009-08-21 12:28:31 +0000203
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 Poon1e2b44e2018-07-23 20:51:22 -0700210 rds6_tcp_tc_count++;
211 if (!tc->t_cpath->cp_conn->c_isv6)
212 rds_tcp_tc_count++;
Andy Grover70041082009-08-21 12:28:31 +0000213 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 Varadhanea3b1ea2016-06-30 16:11:14 -0700220 tc->t_cpath = cp;
Andy Grover70041082009-08-21 12:28:31 +0000221 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 Varadhanea3b1ea2016-06-30 16:11:14 -0700225 sock->sk->sk_user_data = cp;
Andy Grover70041082009-08-21 12:28:31 +0000226 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 Poon1e2b44e2018-07-23 20:51:22 -0700233/* Handle RDS_INFO_TCP_SOCKETS socket option. It only returns IPv4
234 * connections for backward compatibility.
235 */
Sowmini Varadhan1ac507d2016-11-04 10:04:11 -0700236static void rds_tcp_tc_info(struct socket *rds_sock, unsigned int len,
Andy Grover70041082009-08-21 12:28:31 +0000237 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 Grover70041082009-08-21 12:28:31 +0000243
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 Poon1e2b44e2018-07-23 20:51:22 -0700250 struct inet_sock *inet = inet_sk(tc->t_sock->sk);
Andy Grover70041082009-08-21 12:28:31 +0000251
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -0700252 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 Grover70041082009-08-21 12:28:31 +0000259
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
269out:
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 Poonb7ff8b12018-07-23 20:51:23 -0700276/* 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 */
280static 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
311out:
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 Pooneee2fa62018-07-23 20:51:21 -0700318static int rds_tcp_laddr_check(struct net *net, const struct in6_addr *addr,
319 __u32 scope_id)
Andy Grover70041082009-08-21 12:28:31 +0000320{
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700321 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 Grover70041082009-08-21 12:28:31 +0000345 return 0;
346 return -EADDRNOTAVAIL;
347}
348
Sowmini Varadhan66261da2017-12-22 09:39:01 -0800349static void rds_tcp_conn_free(void *arg)
350{
351 struct rds_tcp_connection *tc = arg;
Sowmini Varadhan53d0e832018-03-15 03:54:26 -0700352 unsigned long flags;
Sowmini Varadhan66261da2017-12-22 09:39:01 -0800353
354 rdsdebug("freeing tc %p\n", tc);
355
Sowmini Varadhan53d0e832018-03-15 03:54:26 -0700356 spin_lock_irqsave(&rds_tcp_conn_lock, flags);
Sowmini Varadhan66261da2017-12-22 09:39:01 -0800357 if (!tc->t_tcp_node_detached)
358 list_del(&tc->t_tcp_node);
Sowmini Varadhan53d0e832018-03-15 03:54:26 -0700359 spin_unlock_irqrestore(&rds_tcp_conn_lock, flags);
Sowmini Varadhan66261da2017-12-22 09:39:01 -0800360
361 kmem_cache_free(rds_tcp_conn_slab, tc);
362}
363
Andy Grover70041082009-08-21 12:28:31 +0000364static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp)
365{
366 struct rds_tcp_connection *tc;
Sowmini Varadhan66261da2017-12-22 09:39:01 -0800367 int i, j;
368 int ret = 0;
Andy Grover70041082009-08-21 12:28:31 +0000369
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700370 for (i = 0; i < RDS_MPATH_WORKERS; i++) {
371 tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp);
Sowmini Varadhan66261da2017-12-22 09:39:01 -0800372 if (!tc) {
373 ret = -ENOMEM;
Sowmini Varadhanebeeb1a2018-02-03 04:26:51 -0800374 goto fail;
Sowmini Varadhan66261da2017-12-22 09:39:01 -0800375 }
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700376 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 Grover70041082009-08-21 12:28:31 +0000381
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700382 conn->c_path[i].cp_transport_data = tc;
383 tc->t_cpath = &conn->c_path[i];
Sowmini Varadhanebeeb1a2018-02-03 04:26:51 -0800384 tc->t_tcp_node_detached = true;
Andy Grover70041082009-08-21 12:28:31 +0000385
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700386 rdsdebug("rds_conn_path [%d] tc %p\n", i,
387 conn->c_path[i].cp_transport_data);
388 }
Sowmini Varadhan53d0e832018-03-15 03:54:26 -0700389 spin_lock_irq(&rds_tcp_conn_lock);
Sowmini Varadhanebeeb1a2018-02-03 04:26:51 -0800390 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 Varadhan53d0e832018-03-15 03:54:26 -0700395 spin_unlock_irq(&rds_tcp_conn_lock);
Sowmini Varadhanebeeb1a2018-02-03 04:26:51 -0800396fail:
Sowmini Varadhan66261da2017-12-22 09:39:01 -0800397 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 Grover70041082009-08-21 12:28:31 +0000402}
403
Sowmini Varadhanafb41642016-06-30 16:11:13 -0700404static 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 Varadhanebeeb1a2018-02-03 04:26:51 -0800415static void rds_tcp_set_unloading(void)
416{
417 atomic_set(&rds_tcp_unloading, 1);
418}
419
420static bool rds_tcp_is_unloading(struct rds_connection *conn)
421{
422 return atomic_read(&rds_tcp_unloading) != 0;
423}
424
Andy Grover70041082009-08-21 12:28:31 +0000425static 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 Varadhanafb41642016-06-30 16:11:13 -0700432 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 Grover70041082009-08-21 12:28:31 +0000436 spin_unlock_irq(&rds_tcp_conn_lock);
437
Sowmini Varadhan26e4e6b2016-06-30 16:11:11 -0700438 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node)
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700439 rds_conn_destroy(tc->t_cpath->cp_conn);
Andy Grover70041082009-08-21 12:28:31 +0000440}
441
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400442static void rds_tcp_exit(void);
Andy Grover70041082009-08-21 12:28:31 +0000443
444struct rds_transport rds_tcp_transport = {
445 .laddr_check = rds_tcp_laddr_check,
Sowmini Varadhan226f7a72016-06-30 16:11:10 -0700446 .xmit_path_prepare = rds_tcp_xmit_path_prepare,
447 .xmit_path_complete = rds_tcp_xmit_path_complete,
Andy Grover70041082009-08-21 12:28:31 +0000448 .xmit = rds_tcp_xmit,
Sowmini Varadhan2da43c42016-06-30 16:11:15 -0700449 .recv_path = rds_tcp_recv_path,
Andy Grover70041082009-08-21 12:28:31 +0000450 .conn_alloc = rds_tcp_conn_alloc,
451 .conn_free = rds_tcp_conn_free,
Sowmini Varadhanb04e8552016-06-30 16:11:16 -0700452 .conn_path_connect = rds_tcp_conn_path_connect,
Sowmini Varadhan226f7a72016-06-30 16:11:10 -0700453 .conn_path_shutdown = rds_tcp_conn_path_shutdown,
Andy Grover70041082009-08-21 12:28:31 +0000454 .inc_copy_to_user = rds_tcp_inc_copy_to_user,
Andy Grover70041082009-08-21 12:28:31 +0000455 .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 Grover335776b2009-08-21 12:28:34 +0000460 .t_type = RDS_TRANS_TCP,
Andy Grover70041082009-08-21 12:28:31 +0000461 .t_prefer_loopback = 1,
Sowmini Varadhan5916e2c2016-07-14 03:51:03 -0700462 .t_mp_capable = 1,
Sowmini Varadhanebeeb1a2018-02-03 04:26:51 -0800463 .t_unloading = rds_tcp_is_unloading,
Andy Grover70041082009-08-21 12:28:31 +0000464};
465
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300466static unsigned int rds_tcp_netid;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400467
468/* per-network namespace private data for this module */
469struct rds_tcp_net {
470 struct socket *rds_tcp_listen_sock;
471 struct work_struct rds_tcp_accept_w;
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700472 struct ctl_table_header *rds_tcp_sysctl;
473 struct ctl_table *ctl_table;
474 int sndbuf_size;
475 int rcvbuf_size;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400476};
477
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700478/* All module specific customizations to the RDS-TCP socket should be done in
479 * rds_tcp_tune() and applied after socket creation.
480 */
481void 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 Varadhan467fa152015-08-05 01:43:26 -0400500static 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
510void 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
518static __net_init int rds_tcp_init_net(struct net *net)
519{
520 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700521 struct ctl_table *tbl;
522 int err = 0;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400523
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700524 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 Poon1e2b44e2018-07-23 20:51:22 -0700548 rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net, true);
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400549 if (!rtn->rds_tcp_listen_sock) {
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -0700550 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 Varadhan467fa152015-08-05 01:43:26 -0400560 }
561 INIT_WORK(&rtn->rds_tcp_accept_w, rds_tcp_accept_worker);
562 return 0;
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700563
564fail:
565 if (net != &init_net)
566 kfree(tbl);
567 return err;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400568}
569
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400570static void rds_tcp_kill_sock(struct net *net)
571{
572 struct rds_tcp_connection *tc, *_tc;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400573 LIST_HEAD(tmp_list);
574 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
Sowmini Varadhanb21dd452017-03-04 08:57:35 -0800575 struct socket *lsock = rtn->rds_tcp_listen_sock;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400576
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400577 rtn->rds_tcp_listen_sock = NULL;
Sowmini Varadhanb21dd452017-03-04 08:57:35 -0800578 rds_tcp_listen_stop(lsock, &rtn->rds_tcp_accept_w);
Sowmini Varadhan53d0e832018-03-15 03:54:26 -0700579 spin_lock_irq(&rds_tcp_conn_lock);
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400580 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
Sowmini Varadhan681648e2017-11-30 11:11:28 -0800581 struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net);
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400582
583 if (net != c_net || !tc->t_sock)
584 continue;
Sowmini Varadhanf10b4cf2017-11-30 11:11:29 -0800585 if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn)) {
Sowmini Varadhanafb41642016-06-30 16:11:13 -0700586 list_move_tail(&tc->t_tcp_node, &tmp_list);
Sowmini Varadhanf10b4cf2017-11-30 11:11:29 -0800587 } else {
588 list_del(&tc->t_tcp_node);
589 tc->t_tcp_node_detached = true;
590 }
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400591 }
Sowmini Varadhan53d0e832018-03-15 03:54:26 -0700592 spin_unlock_irq(&rds_tcp_conn_lock);
Sowmini Varadhan2d746c92017-11-30 11:11:27 -0800593 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node)
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700594 rds_conn_destroy(tc->t_cpath->cp_conn);
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400595}
596
Sowmini Varadhanbdf5bd72018-03-19 06:52:48 -0700597static 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
610static 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 Varadhanbdf5bd72018-03-19 06:52:48 -0700615};
616
Sowmini Varadhana93d01f2016-07-14 03:51:01 -0700617void *rds_tcp_listen_sock_def_readable(struct net *net)
618{
619 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
Sowmini Varadhanb21dd452017-03-04 08:57:35 -0800620 struct socket *lsock = rtn->rds_tcp_listen_sock;
Sowmini Varadhana93d01f2016-07-14 03:51:01 -0700621
Sowmini Varadhanb21dd452017-03-04 08:57:35 -0800622 if (!lsock)
623 return NULL;
624
625 return lsock->sk->sk_user_data;
Sowmini Varadhana93d01f2016-07-14 03:51:01 -0700626}
627
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700628/* 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 */
633static void rds_tcp_sysctl_reset(struct net *net)
634{
635 struct rds_tcp_connection *tc, *_tc;
636
Sowmini Varadhan53d0e832018-03-15 03:54:26 -0700637 spin_lock_irq(&rds_tcp_conn_lock);
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700638 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
Sowmini Varadhan681648e2017-11-30 11:11:28 -0800639 struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net);
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700640
641 if (net != c_net || !tc->t_sock)
642 continue;
643
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700644 /* reconnect with new parameters */
Sowmini Varadhanaed20a52017-07-16 16:43:46 -0700645 rds_conn_path_drop(tc->t_cpath, false);
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700646 }
Sowmini Varadhan53d0e832018-03-15 03:54:26 -0700647 spin_unlock_irq(&rds_tcp_conn_lock);
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700648}
649
650static 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 Varadhan467fa152015-08-05 01:43:26 -0400668static void rds_tcp_exit(void)
669{
Sowmini Varadhanebeeb1a2018-02-03 04:26:51 -0800670 rds_tcp_set_unloading();
671 synchronize_rcu();
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400672 rds_info_deregister_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
Ka-Cheong Poonb7ff8b12018-07-23 20:51:23 -0700673 rds_info_deregister_func(RDS6_INFO_TCP_SOCKETS, rds6_tcp_tc_info);
Sowmini Varadhanbdf5bd72018-03-19 06:52:48 -0700674 unregister_pernet_device(&rds_tcp_net_ops);
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400675 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}
680module_exit(rds_tcp_exit);
681
stephen hemmingerff51bf82010-10-19 08:08:33 +0000682static int rds_tcp_init(void)
Andy Grover70041082009-08-21 12:28:31 +0000683{
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 Grover8690bfa2010-01-12 11:56:44 -0800689 if (!rds_tcp_conn_slab) {
Andy Grover70041082009-08-21 12:28:31 +0000690 ret = -ENOMEM;
691 goto out;
692 }
693
Sowmini Varadhan16c09b12017-03-04 08:57:34 -0800694 ret = rds_tcp_recv_init();
695 if (ret)
Zhu Yanjun3b5923f2017-02-24 04:28:01 -0500696 goto out_slab;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400697
Sowmini Varadhanbdf5bd72018-03-19 06:52:48 -0700698 ret = register_pernet_device(&rds_tcp_net_ops);
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400699 if (ret)
Sowmini Varadhan16c09b12017-03-04 08:57:34 -0800700 goto out_recv;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400701
Zhu Yanjuna8d63a52017-03-03 00:44:26 -0500702 rds_trans_register(&rds_tcp_transport);
Andy Grover70041082009-08-21 12:28:31 +0000703
Andy Grover70041082009-08-21 12:28:31 +0000704 rds_info_register_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
Ka-Cheong Poonb7ff8b12018-07-23 20:51:23 -0700705 rds_info_register_func(RDS6_INFO_TCP_SOCKETS, rds6_tcp_tc_info);
Andy Grover70041082009-08-21 12:28:31 +0000706
707 goto out;
Sowmini Varadhan16c09b12017-03-04 08:57:34 -0800708out_recv:
709 rds_tcp_recv_exit();
Zhu Yanjun3b5923f2017-02-24 04:28:01 -0500710out_slab:
Andy Grover70041082009-08-21 12:28:31 +0000711 kmem_cache_destroy(rds_tcp_conn_slab);
712out:
713 return ret;
714}
715module_init(rds_tcp_init);
716
717MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
718MODULE_DESCRIPTION("RDS: TCP transport");
719MODULE_LICENSE("Dual BSD/GPL");
720