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