blob: 22569007677357ba40347ee46e6584c683de2597 [file] [log] [blame]
Andy Grover70041082009-08-21 12:28:31 +00001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Andy Grover70041082009-08-21 12:28:31 +000035#include <linux/in.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040036#include <linux/module.h>
Andy Grover70041082009-08-21 12:28:31 +000037#include <net/tcp.h>
Sowmini Varadhan467fa152015-08-05 01:43:26 -040038#include <net/net_namespace.h>
39#include <net/netns/generic.h>
Andy Grover70041082009-08-21 12:28:31 +000040
41#include "rds.h"
42#include "tcp.h"
43
44/* only for info exporting */
45static DEFINE_SPINLOCK(rds_tcp_tc_list_lock);
46static LIST_HEAD(rds_tcp_tc_list);
stephen hemmingerff51bf82010-10-19 08:08:33 +000047static unsigned int rds_tcp_tc_count;
Andy Grover70041082009-08-21 12:28:31 +000048
49/* Track rds_tcp_connection structs so they can be cleaned up */
50static DEFINE_SPINLOCK(rds_tcp_conn_lock);
51static LIST_HEAD(rds_tcp_conn_list);
52
53static struct kmem_cache *rds_tcp_conn_slab;
54
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -070055static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write,
56 void __user *buffer, size_t *lenp,
57 loff_t *fpos);
58
Wei Yongjunaf73e722016-06-17 18:12:46 +000059static int rds_tcp_min_sndbuf = SOCK_MIN_SNDBUF;
60static int rds_tcp_min_rcvbuf = SOCK_MIN_RCVBUF;
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -070061
62static struct ctl_table rds_tcp_sysctl_table[] = {
63#define RDS_TCP_SNDBUF 0
64 {
65 .procname = "rds_tcp_sndbuf",
66 /* data is per-net pointer */
67 .maxlen = sizeof(int),
68 .mode = 0644,
69 .proc_handler = rds_tcp_skbuf_handler,
70 .extra1 = &rds_tcp_min_sndbuf,
71 },
72#define RDS_TCP_RCVBUF 1
73 {
74 .procname = "rds_tcp_rcvbuf",
75 /* data is per-net pointer */
76 .maxlen = sizeof(int),
77 .mode = 0644,
78 .proc_handler = rds_tcp_skbuf_handler,
79 .extra1 = &rds_tcp_min_rcvbuf,
80 },
81 { }
82};
83
Andy Grover70041082009-08-21 12:28:31 +000084/* doing it this way avoids calling tcp_sk() */
85void rds_tcp_nonagle(struct socket *sock)
86{
87 mm_segment_t oldfs = get_fs();
88 int val = 1;
89
90 set_fs(KERNEL_DS);
91 sock->ops->setsockopt(sock, SOL_TCP, TCP_NODELAY, (char __user *)&val,
92 sizeof(val));
93 set_fs(oldfs);
94}
95
Andy Grover70041082009-08-21 12:28:31 +000096u32 rds_tcp_snd_nxt(struct rds_tcp_connection *tc)
97{
98 return tcp_sk(tc->t_sock->sk)->snd_nxt;
99}
100
101u32 rds_tcp_snd_una(struct rds_tcp_connection *tc)
102{
103 return tcp_sk(tc->t_sock->sk)->snd_una;
104}
105
106void rds_tcp_restore_callbacks(struct socket *sock,
107 struct rds_tcp_connection *tc)
108{
109 rdsdebug("restoring sock %p callbacks from tc %p\n", sock, tc);
110 write_lock_bh(&sock->sk->sk_callback_lock);
111
112 /* done under the callback_lock to serialize with write_space */
113 spin_lock(&rds_tcp_tc_list_lock);
114 list_del_init(&tc->t_list_item);
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/*
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700129 * 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 */
137void rds_tcp_reset_callbacks(struct socket *sock,
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700138 struct rds_conn_path *cp)
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700139{
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700140 struct rds_tcp_connection *tc = cp->cp_transport_data;
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700141 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
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700150 * cp_transport_data. We quiesce these threads by setting
151 * cp_state to something other than RDS_CONN_UP, and then
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700152 * 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().
Sowmini Varadhan9c794402016-06-04 14:00:00 -0700155 *
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()
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700165 */
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700166 atomic_set(&cp->cp_state, RDS_CONN_RESETTING);
167 wait_event(cp->cp_waitq, !test_bit(RDS_IN_XMIT, &cp->cp_flags));
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700168 lock_sock(osock->sk);
169 /* reset receive side state for rds_tcp_data_recv() for osock */
Sowmini Varadhanac3615e2016-07-14 03:51:02 -0700170 cancel_delayed_work_sync(&cp->cp_send_w);
171 cancel_delayed_work_sync(&cp->cp_recv_w);
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700172 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;
Sowmini Varadhanac3615e2016-07-14 03:51:02 -0700178 rds_tcp_restore_callbacks(osock, tc);
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700179 release_sock(osock->sk);
180 sock_release(osock);
181newsock:
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700182 rds_send_path_reset(cp);
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700183 lock_sock(sock->sk);
Sowmini Varadhanac3615e2016-07-14 03:51:02 -0700184 rds_tcp_set_callbacks(sock, cp);
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700185 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
Andy Grover70041082009-08-21 12:28:31 +0000191 */
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700192void rds_tcp_set_callbacks(struct socket *sock, struct rds_conn_path *cp)
Andy Grover70041082009-08-21 12:28:31 +0000193{
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700194 struct rds_tcp_connection *tc = cp->cp_transport_data;
Andy Grover70041082009-08-21 12:28:31 +0000195
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 rds_tcp_tc_count++;
203 spin_unlock(&rds_tcp_tc_list_lock);
204
205 /* accepted sockets need our listen data ready undone */
206 if (sock->sk->sk_data_ready == rds_tcp_listen_data_ready)
207 sock->sk->sk_data_ready = sock->sk->sk_user_data;
208
209 tc->t_sock = sock;
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700210 tc->t_cpath = cp;
Andy Grover70041082009-08-21 12:28:31 +0000211 tc->t_orig_data_ready = sock->sk->sk_data_ready;
212 tc->t_orig_write_space = sock->sk->sk_write_space;
213 tc->t_orig_state_change = sock->sk->sk_state_change;
214
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700215 sock->sk->sk_user_data = cp;
Andy Grover70041082009-08-21 12:28:31 +0000216 sock->sk->sk_data_ready = rds_tcp_data_ready;
217 sock->sk->sk_write_space = rds_tcp_write_space;
218 sock->sk->sk_state_change = rds_tcp_state_change;
219
220 write_unlock_bh(&sock->sk->sk_callback_lock);
221}
222
Sowmini Varadhan1ac507d2016-11-04 10:04:11 -0700223static void rds_tcp_tc_info(struct socket *rds_sock, unsigned int len,
Andy Grover70041082009-08-21 12:28:31 +0000224 struct rds_info_iterator *iter,
225 struct rds_info_lengths *lens)
226{
227 struct rds_info_tcp_socket tsinfo;
228 struct rds_tcp_connection *tc;
229 unsigned long flags;
230 struct sockaddr_in sin;
231 int sinlen;
Sowmini Varadhan1ac507d2016-11-04 10:04:11 -0700232 struct socket *sock;
Andy Grover70041082009-08-21 12:28:31 +0000233
234 spin_lock_irqsave(&rds_tcp_tc_list_lock, flags);
235
236 if (len / sizeof(tsinfo) < rds_tcp_tc_count)
237 goto out;
238
239 list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) {
240
Sowmini Varadhan1ac507d2016-11-04 10:04:11 -0700241 sock = tc->t_sock;
242 if (sock) {
243 sock->ops->getname(sock, (struct sockaddr *)&sin,
244 &sinlen, 0);
245 tsinfo.local_addr = sin.sin_addr.s_addr;
246 tsinfo.local_port = sin.sin_port;
247 sock->ops->getname(sock, (struct sockaddr *)&sin,
248 &sinlen, 1);
249 tsinfo.peer_addr = sin.sin_addr.s_addr;
250 tsinfo.peer_port = sin.sin_port;
251 }
Andy Grover70041082009-08-21 12:28:31 +0000252
253 tsinfo.hdr_rem = tc->t_tinc_hdr_rem;
254 tsinfo.data_rem = tc->t_tinc_data_rem;
255 tsinfo.last_sent_nxt = tc->t_last_sent_nxt;
256 tsinfo.last_expected_una = tc->t_last_expected_una;
257 tsinfo.last_seen_una = tc->t_last_seen_una;
258
259 rds_info_copy(iter, &tsinfo, sizeof(tsinfo));
260 }
261
262out:
263 lens->nr = rds_tcp_tc_count;
264 lens->each = sizeof(tsinfo);
265
266 spin_unlock_irqrestore(&rds_tcp_tc_list_lock, flags);
267}
268
Sowmini Varadhand5a8ac22015-08-05 01:43:25 -0400269static int rds_tcp_laddr_check(struct net *net, __be32 addr)
Andy Grover70041082009-08-21 12:28:31 +0000270{
Sowmini Varadhand5a8ac22015-08-05 01:43:25 -0400271 if (inet_addr_type(net, addr) == RTN_LOCAL)
Andy Grover70041082009-08-21 12:28:31 +0000272 return 0;
273 return -EADDRNOTAVAIL;
274}
275
276static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp)
277{
278 struct rds_tcp_connection *tc;
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700279 int i;
Andy Grover70041082009-08-21 12:28:31 +0000280
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700281 for (i = 0; i < RDS_MPATH_WORKERS; i++) {
282 tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp);
283 if (!tc)
284 return -ENOMEM;
Andy Grover70041082009-08-21 12:28:31 +0000285
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700286 mutex_init(&tc->t_conn_path_lock);
287 tc->t_sock = NULL;
288 tc->t_tinc = NULL;
289 tc->t_tinc_hdr_rem = sizeof(struct rds_header);
290 tc->t_tinc_data_rem = 0;
Andy Grover70041082009-08-21 12:28:31 +0000291
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700292 conn->c_path[i].cp_transport_data = tc;
293 tc->t_cpath = &conn->c_path[i];
Andy Grover70041082009-08-21 12:28:31 +0000294
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700295 spin_lock_irq(&rds_tcp_conn_lock);
296 list_add_tail(&tc->t_tcp_node, &rds_tcp_conn_list);
297 spin_unlock_irq(&rds_tcp_conn_lock);
298 rdsdebug("rds_conn_path [%d] tc %p\n", i,
299 conn->c_path[i].cp_transport_data);
300 }
Andy Grover70041082009-08-21 12:28:31 +0000301
Andy Grover70041082009-08-21 12:28:31 +0000302 return 0;
303}
304
305static void rds_tcp_conn_free(void *arg)
306{
307 struct rds_tcp_connection *tc = arg;
Pavel Emelyanov8200a592010-11-02 01:54:01 +0000308 unsigned long flags;
Andy Grover70041082009-08-21 12:28:31 +0000309 rdsdebug("freeing tc %p\n", tc);
Pavel Emelyanov8200a592010-11-02 01:54:01 +0000310
311 spin_lock_irqsave(&rds_tcp_conn_lock, flags);
312 list_del(&tc->t_tcp_node);
313 spin_unlock_irqrestore(&rds_tcp_conn_lock, flags);
314
Andy Grover70041082009-08-21 12:28:31 +0000315 kmem_cache_free(rds_tcp_conn_slab, tc);
316}
317
Sowmini Varadhanafb41642016-06-30 16:11:13 -0700318static bool list_has_conn(struct list_head *list, struct rds_connection *conn)
319{
320 struct rds_tcp_connection *tc, *_tc;
321
322 list_for_each_entry_safe(tc, _tc, list, t_tcp_node) {
323 if (tc->t_cpath->cp_conn == conn)
324 return true;
325 }
326 return false;
327}
328
Andy Grover70041082009-08-21 12:28:31 +0000329static void rds_tcp_destroy_conns(void)
330{
331 struct rds_tcp_connection *tc, *_tc;
332 LIST_HEAD(tmp_list);
333
334 /* avoid calling conn_destroy with irqs off */
335 spin_lock_irq(&rds_tcp_conn_lock);
Sowmini Varadhanafb41642016-06-30 16:11:13 -0700336 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
337 if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn))
338 list_move_tail(&tc->t_tcp_node, &tmp_list);
339 }
Andy Grover70041082009-08-21 12:28:31 +0000340 spin_unlock_irq(&rds_tcp_conn_lock);
341
Sowmini Varadhan26e4e6b2016-06-30 16:11:11 -0700342 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node)
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700343 rds_conn_destroy(tc->t_cpath->cp_conn);
Andy Grover70041082009-08-21 12:28:31 +0000344}
345
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400346static void rds_tcp_exit(void);
Andy Grover70041082009-08-21 12:28:31 +0000347
348struct rds_transport rds_tcp_transport = {
349 .laddr_check = rds_tcp_laddr_check,
Sowmini Varadhan226f7a72016-06-30 16:11:10 -0700350 .xmit_path_prepare = rds_tcp_xmit_path_prepare,
351 .xmit_path_complete = rds_tcp_xmit_path_complete,
Andy Grover70041082009-08-21 12:28:31 +0000352 .xmit = rds_tcp_xmit,
Sowmini Varadhan2da43c42016-06-30 16:11:15 -0700353 .recv_path = rds_tcp_recv_path,
Andy Grover70041082009-08-21 12:28:31 +0000354 .conn_alloc = rds_tcp_conn_alloc,
355 .conn_free = rds_tcp_conn_free,
Sowmini Varadhanb04e8552016-06-30 16:11:16 -0700356 .conn_path_connect = rds_tcp_conn_path_connect,
Sowmini Varadhan226f7a72016-06-30 16:11:10 -0700357 .conn_path_shutdown = rds_tcp_conn_path_shutdown,
Andy Grover70041082009-08-21 12:28:31 +0000358 .inc_copy_to_user = rds_tcp_inc_copy_to_user,
Andy Grover70041082009-08-21 12:28:31 +0000359 .inc_free = rds_tcp_inc_free,
360 .stats_info_copy = rds_tcp_stats_info_copy,
361 .exit = rds_tcp_exit,
362 .t_owner = THIS_MODULE,
363 .t_name = "tcp",
Andy Grover335776bd2009-08-21 12:28:34 +0000364 .t_type = RDS_TRANS_TCP,
Andy Grover70041082009-08-21 12:28:31 +0000365 .t_prefer_loopback = 1,
Sowmini Varadhan5916e2c2016-07-14 03:51:03 -0700366 .t_mp_capable = 1,
Andy Grover70041082009-08-21 12:28:31 +0000367};
368
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300369static unsigned int rds_tcp_netid;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400370
371/* per-network namespace private data for this module */
372struct rds_tcp_net {
373 struct socket *rds_tcp_listen_sock;
374 struct work_struct rds_tcp_accept_w;
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700375 struct ctl_table_header *rds_tcp_sysctl;
376 struct ctl_table *ctl_table;
377 int sndbuf_size;
378 int rcvbuf_size;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400379};
380
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700381/* All module specific customizations to the RDS-TCP socket should be done in
382 * rds_tcp_tune() and applied after socket creation.
383 */
384void rds_tcp_tune(struct socket *sock)
385{
386 struct sock *sk = sock->sk;
387 struct net *net = sock_net(sk);
388 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
389
390 rds_tcp_nonagle(sock);
391 lock_sock(sk);
392 if (rtn->sndbuf_size > 0) {
393 sk->sk_sndbuf = rtn->sndbuf_size;
394 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
395 }
396 if (rtn->rcvbuf_size > 0) {
397 sk->sk_sndbuf = rtn->rcvbuf_size;
398 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
399 }
400 release_sock(sk);
401}
402
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400403static void rds_tcp_accept_worker(struct work_struct *work)
404{
405 struct rds_tcp_net *rtn = container_of(work,
406 struct rds_tcp_net,
407 rds_tcp_accept_w);
408
409 while (rds_tcp_accept_one(rtn->rds_tcp_listen_sock) == 0)
410 cond_resched();
411}
412
413void rds_tcp_accept_work(struct sock *sk)
414{
415 struct net *net = sock_net(sk);
416 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
417
418 queue_work(rds_wq, &rtn->rds_tcp_accept_w);
419}
420
421static __net_init int rds_tcp_init_net(struct net *net)
422{
423 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700424 struct ctl_table *tbl;
425 int err = 0;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400426
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700427 memset(rtn, 0, sizeof(*rtn));
428
429 /* {snd, rcv}buf_size default to 0, which implies we let the
430 * stack pick the value, and permit auto-tuning of buffer size.
431 */
432 if (net == &init_net) {
433 tbl = rds_tcp_sysctl_table;
434 } else {
435 tbl = kmemdup(rds_tcp_sysctl_table,
436 sizeof(rds_tcp_sysctl_table), GFP_KERNEL);
437 if (!tbl) {
438 pr_warn("could not set allocate syctl table\n");
439 return -ENOMEM;
440 }
441 rtn->ctl_table = tbl;
442 }
443 tbl[RDS_TCP_SNDBUF].data = &rtn->sndbuf_size;
444 tbl[RDS_TCP_RCVBUF].data = &rtn->rcvbuf_size;
445 rtn->rds_tcp_sysctl = register_net_sysctl(net, "net/rds/tcp", tbl);
446 if (!rtn->rds_tcp_sysctl) {
447 pr_warn("could not register sysctl\n");
448 err = -ENOMEM;
449 goto fail;
450 }
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400451 rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net);
452 if (!rtn->rds_tcp_listen_sock) {
453 pr_warn("could not set up listen sock\n");
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700454 unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
455 rtn->rds_tcp_sysctl = NULL;
456 err = -EAFNOSUPPORT;
457 goto fail;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400458 }
459 INIT_WORK(&rtn->rds_tcp_accept_w, rds_tcp_accept_worker);
460 return 0;
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700461
462fail:
463 if (net != &init_net)
464 kfree(tbl);
465 return err;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400466}
467
468static void __net_exit rds_tcp_exit_net(struct net *net)
469{
470 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
471
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700472 if (rtn->rds_tcp_sysctl)
473 unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
474
475 if (net != &init_net && rtn->ctl_table)
476 kfree(rtn->ctl_table);
477
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400478 /* If rds_tcp_exit_net() is called as a result of netns deletion,
479 * the rds_tcp_kill_sock() device notifier would already have cleaned
480 * up the listen socket, thus there is no work to do in this function.
481 *
482 * If rds_tcp_exit_net() is called as a result of module unload,
483 * i.e., due to rds_tcp_exit() -> unregister_pernet_subsys(), then
484 * we do need to clean up the listen socket here.
485 */
486 if (rtn->rds_tcp_listen_sock) {
Sowmini Varadhanb21dd452017-03-04 08:57:35 -0800487 struct socket *lsock = rtn->rds_tcp_listen_sock;
488
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400489 rtn->rds_tcp_listen_sock = NULL;
Sowmini Varadhanb21dd452017-03-04 08:57:35 -0800490 rds_tcp_listen_stop(lsock, &rtn->rds_tcp_accept_w);
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400491 }
492}
493
494static struct pernet_operations rds_tcp_net_ops = {
495 .init = rds_tcp_init_net,
496 .exit = rds_tcp_exit_net,
497 .id = &rds_tcp_netid,
498 .size = sizeof(struct rds_tcp_net),
499};
500
Sowmini Varadhanafb41642016-06-30 16:11:13 -0700501/* explicitly send a RST on each socket, thereby releasing any socket refcnts
502 * that may otherwise hold up netns deletion.
503 */
504static void rds_tcp_conn_paths_destroy(struct rds_connection *conn)
505{
506 struct rds_conn_path *cp;
507 struct rds_tcp_connection *tc;
508 int i;
509 struct sock *sk;
510
511 for (i = 0; i < RDS_MPATH_WORKERS; i++) {
512 cp = &conn->c_path[i];
513 tc = cp->cp_transport_data;
514 if (!tc->t_sock)
515 continue;
516 sk = tc->t_sock->sk;
517 sk->sk_prot->disconnect(sk, 0);
518 tcp_done(sk);
519 }
520}
521
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400522static void rds_tcp_kill_sock(struct net *net)
523{
524 struct rds_tcp_connection *tc, *_tc;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400525 LIST_HEAD(tmp_list);
526 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
Sowmini Varadhanb21dd452017-03-04 08:57:35 -0800527 struct socket *lsock = rtn->rds_tcp_listen_sock;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400528
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400529 rtn->rds_tcp_listen_sock = NULL;
Sowmini Varadhanb21dd452017-03-04 08:57:35 -0800530 rds_tcp_listen_stop(lsock, &rtn->rds_tcp_accept_w);
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400531 spin_lock_irq(&rds_tcp_conn_lock);
532 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
Sowmini Varadhan8edc3af2017-03-04 08:57:33 -0800533 struct net *c_net = tc->t_cpath->cp_conn->c_net;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400534
535 if (net != c_net || !tc->t_sock)
536 continue;
Sowmini Varadhanafb41642016-06-30 16:11:13 -0700537 if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn))
538 list_move_tail(&tc->t_tcp_node, &tmp_list);
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400539 }
540 spin_unlock_irq(&rds_tcp_conn_lock);
541 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node) {
Sowmini Varadhanafb41642016-06-30 16:11:13 -0700542 rds_tcp_conn_paths_destroy(tc->t_cpath->cp_conn);
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700543 rds_conn_destroy(tc->t_cpath->cp_conn);
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400544 }
545}
546
Sowmini Varadhana93d01f2016-07-14 03:51:01 -0700547void *rds_tcp_listen_sock_def_readable(struct net *net)
548{
549 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
Sowmini Varadhanb21dd452017-03-04 08:57:35 -0800550 struct socket *lsock = rtn->rds_tcp_listen_sock;
Sowmini Varadhana93d01f2016-07-14 03:51:01 -0700551
Sowmini Varadhanb21dd452017-03-04 08:57:35 -0800552 if (!lsock)
553 return NULL;
554
555 return lsock->sk->sk_user_data;
Sowmini Varadhana93d01f2016-07-14 03:51:01 -0700556}
557
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400558static int rds_tcp_dev_event(struct notifier_block *this,
559 unsigned long event, void *ptr)
560{
561 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
562
563 /* rds-tcp registers as a pernet subys, so the ->exit will only
564 * get invoked after network acitivity has quiesced. We need to
565 * clean up all sockets to quiesce network activity, and use
566 * the unregistration of the per-net loopback device as a trigger
567 * to start that cleanup.
568 */
569 if (event == NETDEV_UNREGISTER_FINAL &&
570 dev->ifindex == LOOPBACK_IFINDEX)
571 rds_tcp_kill_sock(dev_net(dev));
572
573 return NOTIFY_DONE;
574}
575
576static struct notifier_block rds_tcp_dev_notifier = {
577 .notifier_call = rds_tcp_dev_event,
578 .priority = -10, /* must be called after other network notifiers */
579};
580
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700581/* when sysctl is used to modify some kernel socket parameters,this
582 * function resets the RDS connections in that netns so that we can
583 * restart with new parameters. The assumption is that such reset
584 * events are few and far-between.
585 */
586static void rds_tcp_sysctl_reset(struct net *net)
587{
588 struct rds_tcp_connection *tc, *_tc;
589
590 spin_lock_irq(&rds_tcp_conn_lock);
591 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
Sowmini Varadhan8edc3af2017-03-04 08:57:33 -0800592 struct net *c_net = tc->t_cpath->cp_conn->c_net;
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700593
594 if (net != c_net || !tc->t_sock)
595 continue;
596
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700597 /* reconnect with new parameters */
598 rds_conn_path_drop(tc->t_cpath);
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700599 }
600 spin_unlock_irq(&rds_tcp_conn_lock);
601}
602
603static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write,
604 void __user *buffer, size_t *lenp,
605 loff_t *fpos)
606{
607 struct net *net = current->nsproxy->net_ns;
608 int err;
609
610 err = proc_dointvec_minmax(ctl, write, buffer, lenp, fpos);
611 if (err < 0) {
612 pr_warn("Invalid input. Must be >= %d\n",
613 *(int *)(ctl->extra1));
614 return err;
615 }
616 if (write)
617 rds_tcp_sysctl_reset(net);
618 return 0;
619}
620
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400621static void rds_tcp_exit(void)
622{
623 rds_info_deregister_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
624 unregister_pernet_subsys(&rds_tcp_net_ops);
625 if (unregister_netdevice_notifier(&rds_tcp_dev_notifier))
626 pr_warn("could not unregister rds_tcp_dev_notifier\n");
627 rds_tcp_destroy_conns();
628 rds_trans_unregister(&rds_tcp_transport);
629 rds_tcp_recv_exit();
630 kmem_cache_destroy(rds_tcp_conn_slab);
631}
632module_exit(rds_tcp_exit);
633
stephen hemmingerff51bf82010-10-19 08:08:33 +0000634static int rds_tcp_init(void)
Andy Grover70041082009-08-21 12:28:31 +0000635{
636 int ret;
637
638 rds_tcp_conn_slab = kmem_cache_create("rds_tcp_connection",
639 sizeof(struct rds_tcp_connection),
640 0, 0, NULL);
Andy Grover8690bfa2010-01-12 11:56:44 -0800641 if (!rds_tcp_conn_slab) {
Andy Grover70041082009-08-21 12:28:31 +0000642 ret = -ENOMEM;
643 goto out;
644 }
645
Sowmini Varadhan16c09b12017-03-04 08:57:34 -0800646 ret = rds_tcp_recv_init();
647 if (ret)
Zhu Yanjun3b5923f2017-02-24 04:28:01 -0500648 goto out_slab;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400649
650 ret = register_pernet_subsys(&rds_tcp_net_ops);
651 if (ret)
Sowmini Varadhan16c09b12017-03-04 08:57:34 -0800652 goto out_recv;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400653
Sowmini Varadhan16c09b12017-03-04 08:57:34 -0800654 ret = register_netdevice_notifier(&rds_tcp_dev_notifier);
655 if (ret) {
656 pr_warn("could not register rds_tcp_dev_notifier\n");
Vegard Nossum3dad5422016-07-03 10:54:54 +0200657 goto out_pernet;
Sowmini Varadhan16c09b12017-03-04 08:57:34 -0800658 }
Andy Grover70041082009-08-21 12:28:31 +0000659
Zhu Yanjuna8d63a52017-03-03 00:44:26 -0500660 rds_trans_register(&rds_tcp_transport);
Andy Grover70041082009-08-21 12:28:31 +0000661
Andy Grover70041082009-08-21 12:28:31 +0000662 rds_info_register_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
663
664 goto out;
665
Vegard Nossum3dad5422016-07-03 10:54:54 +0200666out_pernet:
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400667 unregister_pernet_subsys(&rds_tcp_net_ops);
Sowmini Varadhan16c09b12017-03-04 08:57:34 -0800668out_recv:
669 rds_tcp_recv_exit();
Zhu Yanjun3b5923f2017-02-24 04:28:01 -0500670out_slab:
Andy Grover70041082009-08-21 12:28:31 +0000671 kmem_cache_destroy(rds_tcp_conn_slab);
672out:
673 return ret;
674}
675module_init(rds_tcp_init);
676
677MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
678MODULE_DESCRIPTION("RDS: TCP transport");
679MODULE_LICENSE("Dual BSD/GPL");
680