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