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