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