xref: /linux/net/rds/tcp.c (revision a34b0e4e21d6be3c3d620aa7f9dfbf0e9550c19e)
1 /*
2  * Copyright (c) 2006, 2018 Oracle and/or its affiliates. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  *
32  */
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/in.h>
36 #include <linux/module.h>
37 #include <net/tcp.h>
38 #include <net/net_namespace.h>
39 #include <net/netns/generic.h>
40 #include <net/addrconf.h>
41 
42 #include "rds.h"
43 #include "tcp.h"
44 
45 /* only for info exporting */
46 static DEFINE_SPINLOCK(rds_tcp_tc_list_lock);
47 static LIST_HEAD(rds_tcp_tc_list);
48 
49 /* rds_tcp_tc_count counts only IPv4 connections.
50  * rds6_tcp_tc_count counts both IPv4 and IPv6 connections.
51  */
52 static unsigned int rds_tcp_tc_count;
53 #if IS_ENABLED(CONFIG_IPV6)
54 static unsigned int rds6_tcp_tc_count;
55 #endif
56 
57 /* Track rds_tcp_connection structs so they can be cleaned up */
58 static DEFINE_SPINLOCK(rds_tcp_conn_lock);
59 static LIST_HEAD(rds_tcp_conn_list);
60 static atomic_t rds_tcp_unloading = ATOMIC_INIT(0);
61 
62 static struct kmem_cache *rds_tcp_conn_slab;
63 
64 static int rds_tcp_sndbuf_handler(const struct ctl_table *ctl, int write,
65 				  void *buffer, size_t *lenp, loff_t *fpos);
66 static int rds_tcp_rcvbuf_handler(const struct ctl_table *ctl, int write,
67 				  void *buffer, size_t *lenp, loff_t *fpos);
68 
69 static int rds_tcp_min_sndbuf = SOCK_MIN_SNDBUF;
70 static int rds_tcp_min_rcvbuf = SOCK_MIN_RCVBUF;
71 
72 static struct ctl_table rds_tcp_sysctl_table[] = {
73 #define	RDS_TCP_SNDBUF	0
74 	{
75 		.procname       = "rds_tcp_sndbuf",
76 		/* data is per-net pointer */
77 		.maxlen         = sizeof(int),
78 		.mode           = 0644,
79 		.proc_handler   = rds_tcp_sndbuf_handler,
80 		.extra1		= &rds_tcp_min_sndbuf,
81 	},
82 #define	RDS_TCP_RCVBUF	1
83 	{
84 		.procname       = "rds_tcp_rcvbuf",
85 		/* data is per-net pointer */
86 		.maxlen         = sizeof(int),
87 		.mode           = 0644,
88 		.proc_handler   = rds_tcp_rcvbuf_handler,
89 		.extra1		= &rds_tcp_min_rcvbuf,
90 	},
91 };
92 
93 u32 rds_tcp_write_seq(struct rds_tcp_connection *tc)
94 {
95 	/* seq# of the last byte of data in tcp send buffer */
96 	return tcp_sk(tc->t_sock->sk)->write_seq;
97 }
98 
99 u32 rds_tcp_snd_una(struct rds_tcp_connection *tc)
100 {
101 	return tcp_sk(tc->t_sock->sk)->snd_una;
102 }
103 
104 void rds_tcp_restore_callbacks(struct socket *sock,
105 			       struct rds_tcp_connection *tc)
106 {
107 	rdsdebug("restoring sock %p callbacks from tc %p\n", sock, tc);
108 	write_lock_bh(&sock->sk->sk_callback_lock);
109 
110 	/* done under the callback_lock to serialize with write_space */
111 	spin_lock(&rds_tcp_tc_list_lock);
112 	list_del_init(&tc->t_list_item);
113 #if IS_ENABLED(CONFIG_IPV6)
114 	rds6_tcp_tc_count--;
115 #endif
116 	if (!tc->t_cpath->cp_conn->c_isv6)
117 		rds_tcp_tc_count--;
118 	spin_unlock(&rds_tcp_tc_list_lock);
119 
120 	tc->t_sock = NULL;
121 
122 	sock->sk->sk_write_space = tc->t_orig_write_space;
123 	sock->sk->sk_data_ready = tc->t_orig_data_ready;
124 	sock->sk->sk_state_change = tc->t_orig_state_change;
125 	sock->sk->sk_user_data = NULL;
126 
127 	write_unlock_bh(&sock->sk->sk_callback_lock);
128 }
129 
130 /*
131  * rds_tcp_reset_callbacks() switches the to the new sock and
132  * returns the existing tc->t_sock.
133  *
134  * The only functions that set tc->t_sock are rds_tcp_set_callbacks
135  * and rds_tcp_reset_callbacks.  Send and receive trust that
136  * it is set.  The absence of RDS_CONN_UP bit protects those paths
137  * from being called while it isn't set.
138  */
139 void rds_tcp_reset_callbacks(struct socket *sock,
140 			     struct rds_conn_path *cp)
141 {
142 	struct rds_tcp_connection *tc = cp->cp_transport_data;
143 	struct socket *osock = tc->t_sock;
144 
145 	if (!osock)
146 		goto newsock;
147 
148 	/* Need to resolve a duelling SYN between peers.
149 	 * We have an outstanding SYN to this peer, which may
150 	 * potentially have transitioned to the RDS_CONN_UP state,
151 	 * so we must quiesce any send threads before resetting
152 	 * cp_transport_data. We quiesce these threads by setting
153 	 * cp_state to something other than RDS_CONN_UP, and then
154 	 * waiting for any existing threads in rds_send_xmit to
155 	 * complete release_in_xmit(). (Subsequent threads entering
156 	 * rds_send_xmit() will bail on !rds_conn_up().
157 	 *
158 	 * However an incoming syn-ack at this point would end up
159 	 * marking the conn as RDS_CONN_UP, and would again permit
160 	 * rds_send_xmi() threads through, so ideally we would
161 	 * synchronize on RDS_CONN_UP after lock_sock(), but cannot
162 	 * do that: waiting on !RDS_IN_XMIT after lock_sock() may
163 	 * end up deadlocking with tcp_sendmsg(), and the RDS_IN_XMIT
164 	 * would not get set. As a result, we set c_state to
165 	 * RDS_CONN_RESETTTING, to ensure that rds_tcp_state_change
166 	 * cannot mark rds_conn_path_up() in the window before lock_sock()
167 	 */
168 	atomic_set(&cp->cp_state, RDS_CONN_RESETTING);
169 	wait_event(cp->cp_waitq, !test_bit(RDS_IN_XMIT, &cp->cp_flags));
170 	/* reset receive side state for rds_tcp_data_recv() for osock  */
171 	cancel_delayed_work_sync(&cp->cp_send_w);
172 	cancel_delayed_work_sync(&cp->cp_recv_w);
173 	lock_sock(osock->sk);
174 	if (tc->t_tinc) {
175 		rds_inc_put(&tc->t_tinc->ti_inc);
176 		tc->t_tinc = NULL;
177 	}
178 	tc->t_tinc_hdr_rem = sizeof(struct rds_header);
179 	tc->t_tinc_data_rem = 0;
180 	rds_tcp_restore_callbacks(osock, tc);
181 	release_sock(osock->sk);
182 	sock_release(osock);
183 newsock:
184 	rds_send_path_reset(cp);
185 	lock_sock(sock->sk);
186 	rds_tcp_set_callbacks(sock, cp);
187 	release_sock(sock->sk);
188 }
189 
190 /* Add tc to rds_tcp_tc_list and set tc->t_sock. See comments
191  * above rds_tcp_reset_callbacks for notes about synchronization
192  * with data path
193  */
194 void rds_tcp_set_callbacks(struct socket *sock, struct rds_conn_path *cp)
195 {
196 	struct rds_tcp_connection *tc = cp->cp_transport_data;
197 
198 	rdsdebug("setting sock %p callbacks to tc %p\n", sock, tc);
199 	write_lock_bh(&sock->sk->sk_callback_lock);
200 
201 	/* done under the callback_lock to serialize with write_space */
202 	spin_lock(&rds_tcp_tc_list_lock);
203 	list_add_tail(&tc->t_list_item, &rds_tcp_tc_list);
204 #if IS_ENABLED(CONFIG_IPV6)
205 	rds6_tcp_tc_count++;
206 #endif
207 	if (!tc->t_cpath->cp_conn->c_isv6)
208 		rds_tcp_tc_count++;
209 	spin_unlock(&rds_tcp_tc_list_lock);
210 
211 	/* accepted sockets need our listen data ready undone */
212 	if (sock->sk->sk_data_ready == rds_tcp_listen_data_ready)
213 		sock->sk->sk_data_ready = sock->sk->sk_user_data;
214 
215 	tc->t_sock = sock;
216 	if (!tc->t_rtn)
217 		tc->t_rtn = net_generic(sock_net(sock->sk), rds_tcp_netid);
218 	tc->t_cpath = cp;
219 	tc->t_orig_data_ready = sock->sk->sk_data_ready;
220 	tc->t_orig_write_space = sock->sk->sk_write_space;
221 	tc->t_orig_state_change = sock->sk->sk_state_change;
222 
223 	sock->sk->sk_user_data = cp;
224 	sock->sk->sk_data_ready = rds_tcp_data_ready;
225 	sock->sk->sk_write_space = rds_tcp_write_space;
226 	sock->sk->sk_state_change = rds_tcp_state_change;
227 
228 	write_unlock_bh(&sock->sk->sk_callback_lock);
229 }
230 
231 /* Handle RDS_INFO_TCP_SOCKETS socket option.  It only returns IPv4
232  * connections for backward compatibility.
233  */
234 static void rds_tcp_tc_info(struct socket *rds_sock, unsigned int len,
235 			    struct rds_info_iterator *iter,
236 			    struct rds_info_lengths *lens)
237 {
238 	struct rds_info_tcp_socket tsinfo;
239 	struct rds_tcp_connection *tc;
240 	unsigned long flags;
241 
242 	spin_lock_irqsave(&rds_tcp_tc_list_lock, flags);
243 
244 	if (len / sizeof(tsinfo) < rds_tcp_tc_count)
245 		goto out;
246 
247 	list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) {
248 		struct inet_sock *inet = inet_sk(tc->t_sock->sk);
249 
250 		if (tc->t_cpath->cp_conn->c_isv6)
251 			continue;
252 
253 		tsinfo.local_addr = inet->inet_saddr;
254 		tsinfo.local_port = inet->inet_sport;
255 		tsinfo.peer_addr = inet->inet_daddr;
256 		tsinfo.peer_port = inet->inet_dport;
257 
258 		tsinfo.hdr_rem = tc->t_tinc_hdr_rem;
259 		tsinfo.data_rem = tc->t_tinc_data_rem;
260 		tsinfo.last_sent_nxt = tc->t_last_sent_nxt;
261 		tsinfo.last_expected_una = tc->t_last_expected_una;
262 		tsinfo.last_seen_una = tc->t_last_seen_una;
263 		tsinfo.tos = tc->t_cpath->cp_conn->c_tos;
264 
265 		rds_info_copy(iter, &tsinfo, sizeof(tsinfo));
266 	}
267 
268 out:
269 	lens->nr = rds_tcp_tc_count;
270 	lens->each = sizeof(tsinfo);
271 
272 	spin_unlock_irqrestore(&rds_tcp_tc_list_lock, flags);
273 }
274 
275 #if IS_ENABLED(CONFIG_IPV6)
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 #endif
318 
319 int rds_tcp_laddr_check(struct net *net, const struct in6_addr *addr,
320 			__u32 scope_id)
321 {
322 	struct net_device *dev = NULL;
323 #if IS_ENABLED(CONFIG_IPV6)
324 	int ret;
325 #endif
326 
327 	if (ipv6_addr_v4mapped(addr)) {
328 		if (inet_addr_type(net, addr->s6_addr32[3]) == RTN_LOCAL)
329 			return 0;
330 		return -EADDRNOTAVAIL;
331 	}
332 
333 	/* If the scope_id is specified, check only those addresses
334 	 * hosted on the specified interface.
335 	 */
336 	if (scope_id != 0) {
337 		rcu_read_lock();
338 		dev = dev_get_by_index_rcu(net, scope_id);
339 		/* scope_id is not valid... */
340 		if (!dev) {
341 			rcu_read_unlock();
342 			return -EADDRNOTAVAIL;
343 		}
344 		rcu_read_unlock();
345 	}
346 #if IS_ENABLED(CONFIG_IPV6)
347 	ret = ipv6_chk_addr(net, addr, dev, 0);
348 	if (ret)
349 		return 0;
350 #endif
351 	return -EADDRNOTAVAIL;
352 }
353 
354 static void rds_tcp_conn_free(void *arg)
355 {
356 	struct rds_tcp_connection *tc = arg;
357 	unsigned long flags;
358 
359 	rdsdebug("freeing tc %p\n", tc);
360 
361 	spin_lock_irqsave(&rds_tcp_conn_lock, flags);
362 	if (!tc->t_tcp_node_detached)
363 		list_del(&tc->t_tcp_node);
364 	spin_unlock_irqrestore(&rds_tcp_conn_lock, flags);
365 
366 	kmem_cache_free(rds_tcp_conn_slab, tc);
367 }
368 
369 static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp)
370 {
371 	struct rds_tcp_connection *tc;
372 	int i, j;
373 	int ret = 0;
374 
375 	for (i = 0; i < RDS_MPATH_WORKERS; i++) {
376 		tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp);
377 		if (!tc) {
378 			ret = -ENOMEM;
379 			goto fail;
380 		}
381 		mutex_init(&tc->t_conn_path_lock);
382 		tc->t_sock = NULL;
383 		tc->t_rtn = NULL;
384 		tc->t_tinc = NULL;
385 		tc->t_tinc_hdr_rem = sizeof(struct rds_header);
386 		tc->t_tinc_data_rem = 0;
387 
388 		conn->c_path[i].cp_transport_data = tc;
389 		tc->t_cpath = &conn->c_path[i];
390 		tc->t_tcp_node_detached = true;
391 
392 		rdsdebug("rds_conn_path [%d] tc %p\n", i,
393 			 conn->c_path[i].cp_transport_data);
394 	}
395 	spin_lock_irq(&rds_tcp_conn_lock);
396 	for (i = 0; i < RDS_MPATH_WORKERS; i++) {
397 		tc = conn->c_path[i].cp_transport_data;
398 		tc->t_tcp_node_detached = false;
399 		list_add_tail(&tc->t_tcp_node, &rds_tcp_conn_list);
400 	}
401 	spin_unlock_irq(&rds_tcp_conn_lock);
402 fail:
403 	if (ret) {
404 		for (j = 0; j < i; j++)
405 			rds_tcp_conn_free(conn->c_path[j].cp_transport_data);
406 	}
407 	return ret;
408 }
409 
410 static bool list_has_conn(struct list_head *list, struct rds_connection *conn)
411 {
412 	struct rds_tcp_connection *tc, *_tc;
413 
414 	list_for_each_entry_safe(tc, _tc, list, t_tcp_node) {
415 		if (tc->t_cpath->cp_conn == conn)
416 			return true;
417 	}
418 	return false;
419 }
420 
421 static void rds_tcp_set_unloading(void)
422 {
423 	atomic_set(&rds_tcp_unloading, 1);
424 }
425 
426 static bool rds_tcp_is_unloading(struct rds_connection *conn)
427 {
428 	return atomic_read(&rds_tcp_unloading) != 0;
429 }
430 
431 static void rds_tcp_destroy_conns(void)
432 {
433 	struct rds_tcp_connection *tc, *_tc;
434 	LIST_HEAD(tmp_list);
435 
436 	/* avoid calling conn_destroy with irqs off */
437 	spin_lock_irq(&rds_tcp_conn_lock);
438 	list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
439 		if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn))
440 			list_move_tail(&tc->t_tcp_node, &tmp_list);
441 	}
442 	spin_unlock_irq(&rds_tcp_conn_lock);
443 
444 	list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node)
445 		rds_conn_destroy(tc->t_cpath->cp_conn);
446 }
447 
448 static void rds_tcp_exit(void);
449 
450 static u8 rds_tcp_get_tos_map(u8 tos)
451 {
452 	/* all user tos mapped to default 0 for TCP transport */
453 	return 0;
454 }
455 
456 struct rds_transport rds_tcp_transport = {
457 	.laddr_check		= rds_tcp_laddr_check,
458 	.xmit_path_prepare	= rds_tcp_xmit_path_prepare,
459 	.xmit_path_complete	= rds_tcp_xmit_path_complete,
460 	.xmit			= rds_tcp_xmit,
461 	.recv_path		= rds_tcp_recv_path,
462 	.conn_alloc		= rds_tcp_conn_alloc,
463 	.conn_free		= rds_tcp_conn_free,
464 	.conn_slots_available	= rds_tcp_conn_slots_available,
465 	.conn_path_connect	= rds_tcp_conn_path_connect,
466 	.conn_path_shutdown	= rds_tcp_conn_path_shutdown,
467 	.inc_copy_to_user	= rds_tcp_inc_copy_to_user,
468 	.inc_free		= rds_tcp_inc_free,
469 	.stats_info_copy	= rds_tcp_stats_info_copy,
470 	.exit			= rds_tcp_exit,
471 	.get_tos_map		= rds_tcp_get_tos_map,
472 	.t_owner		= THIS_MODULE,
473 	.t_name			= "tcp",
474 	.t_type			= RDS_TRANS_TCP,
475 	.t_prefer_loopback	= 1,
476 	.t_mp_capable		= 1,
477 	.t_unloading		= rds_tcp_is_unloading,
478 };
479 
480 int rds_tcp_netid;
481 
482 /* All module specific customizations to the RDS-TCP socket should be done in
483  * rds_tcp_tune() and applied after socket creation.
484  */
485 bool rds_tcp_tune(struct socket *sock)
486 {
487 	struct sock *sk = sock->sk;
488 	struct net *net = sock_net(sk);
489 	struct rds_tcp_net *rtn;
490 
491 	tcp_sock_set_nodelay(sock->sk);
492 	lock_sock(sk);
493 	/* TCP timer functions might access net namespace even after
494 	 * a process which created this net namespace terminated.
495 	 */
496 	if (!sk->sk_net_refcnt) {
497 		if (!maybe_get_net(net)) {
498 			release_sock(sk);
499 			return false;
500 		}
501 		sk_net_refcnt_upgrade(sk);
502 		put_net(net);
503 	}
504 	rtn = net_generic(net, rds_tcp_netid);
505 	if (rtn->sndbuf_size > 0) {
506 		sk->sk_sndbuf = rtn->sndbuf_size;
507 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
508 	}
509 	if (rtn->rcvbuf_size > 0) {
510 		sk->sk_rcvbuf = rtn->rcvbuf_size;
511 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
512 	}
513 	release_sock(sk);
514 	return true;
515 }
516 
517 static void rds_tcp_accept_worker(struct work_struct *work)
518 {
519 	struct rds_tcp_net *rtn = container_of(work,
520 					       struct rds_tcp_net,
521 					       rds_tcp_accept_w);
522 
523 	while (rds_tcp_accept_one(rtn) == 0)
524 		cond_resched();
525 }
526 
527 void rds_tcp_accept_work(struct rds_tcp_net *rtn)
528 {
529 	queue_work(rds_wq, &rtn->rds_tcp_accept_w);
530 }
531 
532 static __net_init int rds_tcp_init_net(struct net *net)
533 {
534 	struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
535 	struct ctl_table *tbl;
536 	int err = 0;
537 
538 	memset(rtn, 0, sizeof(*rtn));
539 
540 	mutex_init(&rtn->rds_tcp_accept_lock);
541 
542 	/* {snd, rcv}buf_size default to 0, which implies we let the
543 	 * stack pick the value, and permit auto-tuning of buffer size.
544 	 */
545 	if (net == &init_net) {
546 		tbl = rds_tcp_sysctl_table;
547 	} else {
548 		tbl = kmemdup(rds_tcp_sysctl_table,
549 			      sizeof(rds_tcp_sysctl_table), GFP_KERNEL);
550 		if (!tbl) {
551 			pr_warn("could not set allocate sysctl table\n");
552 			return -ENOMEM;
553 		}
554 		rtn->ctl_table = tbl;
555 	}
556 	tbl[RDS_TCP_SNDBUF].data = &rtn->sndbuf_size;
557 	tbl[RDS_TCP_RCVBUF].data = &rtn->rcvbuf_size;
558 	rtn->rds_tcp_sysctl = register_net_sysctl_sz(net, "net/rds/tcp", tbl,
559 						     ARRAY_SIZE(rds_tcp_sysctl_table));
560 	if (!rtn->rds_tcp_sysctl) {
561 		pr_warn("could not register sysctl\n");
562 		err = -ENOMEM;
563 		goto fail;
564 	}
565 
566 #if IS_ENABLED(CONFIG_IPV6)
567 	rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net, true);
568 #else
569 	rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net, false);
570 #endif
571 	if (!rtn->rds_tcp_listen_sock) {
572 		pr_warn("could not set up IPv6 listen sock\n");
573 
574 #if IS_ENABLED(CONFIG_IPV6)
575 		/* Try IPv4 as some systems disable IPv6 */
576 		rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net, false);
577 		if (!rtn->rds_tcp_listen_sock) {
578 #endif
579 			unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
580 			rtn->rds_tcp_sysctl = NULL;
581 			err = -EAFNOSUPPORT;
582 			goto fail;
583 #if IS_ENABLED(CONFIG_IPV6)
584 		}
585 #endif
586 	}
587 	INIT_WORK(&rtn->rds_tcp_accept_w, rds_tcp_accept_worker);
588 	return 0;
589 
590 fail:
591 	if (net != &init_net)
592 		kfree(tbl);
593 	return err;
594 }
595 
596 static void rds_tcp_kill_sock(struct net *net)
597 {
598 	struct rds_tcp_connection *tc, *_tc;
599 	LIST_HEAD(tmp_list);
600 	struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
601 	struct socket *lsock = rtn->rds_tcp_listen_sock;
602 
603 	rtn->rds_tcp_listen_sock = NULL;
604 	rds_tcp_listen_stop(lsock, &rtn->rds_tcp_accept_w);
605 	if (rtn->rds_tcp_accepted_sock)
606 		sock_release(rtn->rds_tcp_accepted_sock);
607 	spin_lock_irq(&rds_tcp_conn_lock);
608 	list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
609 		struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net);
610 
611 		if (net != c_net)
612 			continue;
613 		if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn)) {
614 			list_move_tail(&tc->t_tcp_node, &tmp_list);
615 		} else {
616 			list_del(&tc->t_tcp_node);
617 			tc->t_tcp_node_detached = true;
618 		}
619 	}
620 	spin_unlock_irq(&rds_tcp_conn_lock);
621 	list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node)
622 		rds_conn_destroy(tc->t_cpath->cp_conn);
623 }
624 
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 
634 	if (net != &init_net)
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),
643 };
644 
645 void *rds_tcp_listen_sock_def_readable(struct net *net)
646 {
647 	struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
648 	struct socket *lsock = rtn->rds_tcp_listen_sock;
649 
650 	if (!lsock)
651 		return NULL;
652 
653 	return lsock->sk->sk_user_data;
654 }
655 
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 
665 	spin_lock_irq(&rds_tcp_conn_lock);
666 	list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
667 		struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net);
668 
669 		if (net != c_net || !tc->t_sock)
670 			continue;
671 
672 		/* reconnect with new parameters */
673 		rds_conn_path_drop(tc->t_cpath, false);
674 	}
675 	spin_unlock_irq(&rds_tcp_conn_lock);
676 }
677 
678 static int rds_tcp_skbuf_handler(struct rds_tcp_net *rtn,
679 				 const struct ctl_table *ctl, int write,
680 				 void *buffer, size_t *lenp, loff_t *fpos)
681 {
682 	int err;
683 
684 	err = proc_dointvec_minmax(ctl, write, buffer, lenp, fpos);
685 	if (err < 0) {
686 		pr_warn("Invalid input. Must be >= %d\n",
687 			*(int *)(ctl->extra1));
688 		return err;
689 	}
690 
691 	if (write && rtn->rds_tcp_listen_sock && rtn->rds_tcp_listen_sock->sk) {
692 		struct net *net = sock_net(rtn->rds_tcp_listen_sock->sk);
693 
694 		rds_tcp_sysctl_reset(net);
695 	}
696 
697 	return 0;
698 }
699 
700 static int rds_tcp_sndbuf_handler(const struct ctl_table *ctl, int write,
701 				  void *buffer, size_t *lenp, loff_t *fpos)
702 {
703 	struct rds_tcp_net *rtn = container_of(ctl->data, struct rds_tcp_net,
704 					       sndbuf_size);
705 
706 	return rds_tcp_skbuf_handler(rtn, ctl, write, buffer, lenp, fpos);
707 }
708 
709 static int rds_tcp_rcvbuf_handler(const struct ctl_table *ctl, int write,
710 				  void *buffer, size_t *lenp, loff_t *fpos)
711 {
712 	struct rds_tcp_net *rtn = container_of(ctl->data, struct rds_tcp_net,
713 					       rcvbuf_size);
714 
715 	return rds_tcp_skbuf_handler(rtn, ctl, write, buffer, lenp, fpos);
716 }
717 
718 static void rds_tcp_exit(void)
719 {
720 	rds_tcp_set_unloading();
721 	synchronize_rcu();
722 	rds_info_deregister_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
723 #if IS_ENABLED(CONFIG_IPV6)
724 	rds_info_deregister_func(RDS6_INFO_TCP_SOCKETS, rds6_tcp_tc_info);
725 #endif
726 	unregister_pernet_device(&rds_tcp_net_ops);
727 	rds_tcp_destroy_conns();
728 	rds_trans_unregister(&rds_tcp_transport);
729 	rds_tcp_recv_exit();
730 	kmem_cache_destroy(rds_tcp_conn_slab);
731 }
732 module_exit(rds_tcp_exit);
733 
734 static int __init rds_tcp_init(void)
735 {
736 	int ret;
737 
738 	rds_tcp_conn_slab = KMEM_CACHE(rds_tcp_connection, 0);
739 	if (!rds_tcp_conn_slab) {
740 		ret = -ENOMEM;
741 		goto out;
742 	}
743 
744 	ret = rds_tcp_recv_init();
745 	if (ret)
746 		goto out_slab;
747 
748 	ret = register_pernet_device(&rds_tcp_net_ops);
749 	if (ret)
750 		goto out_recv;
751 
752 	rds_trans_register(&rds_tcp_transport);
753 
754 	rds_info_register_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
755 #if IS_ENABLED(CONFIG_IPV6)
756 	rds_info_register_func(RDS6_INFO_TCP_SOCKETS, rds6_tcp_tc_info);
757 #endif
758 
759 	goto out;
760 out_recv:
761 	rds_tcp_recv_exit();
762 out_slab:
763 	kmem_cache_destroy(rds_tcp_conn_slab);
764 out:
765 	return ret;
766 }
767 module_init(rds_tcp_init);
768 
769 MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
770 MODULE_DESCRIPTION("RDS: TCP transport");
771 MODULE_LICENSE("Dual BSD/GPL");
772