]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - net/rds/tcp.c
Merge tag 'batadv-net-for-davem-20161119' of git://git.open-mesh.org/linux-merge
[mirror_ubuntu-bionic-kernel.git] / net / rds / tcp.c
CommitLineData
70041088
AG
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>
5a0e3ad6 34#include <linux/slab.h>
70041088 35#include <linux/in.h>
3a9a231d 36#include <linux/module.h>
70041088 37#include <net/tcp.h>
467fa153
SV
38#include <net/net_namespace.h>
39#include <net/netns/generic.h>
70041088
AG
40
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);
ff51bf84 47static unsigned int rds_tcp_tc_count;
70041088
AG
48
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
c6a58ffe
SV
55static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write,
56 void __user *buffer, size_t *lenp,
57 loff_t *fpos);
58
af73e72d
WY
59static int rds_tcp_min_sndbuf = SOCK_MIN_SNDBUF;
60static int rds_tcp_min_rcvbuf = SOCK_MIN_RCVBUF;
c6a58ffe
SV
61
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
70041088
AG
84/* 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
70041088
AG
96u32 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/*
335b48d9
SV
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 */
137void rds_tcp_reset_callbacks(struct socket *sock,
ea3b1ea5 138 struct rds_conn_path *cp)
335b48d9 139{
ea3b1ea5 140 struct rds_tcp_connection *tc = cp->cp_transport_data;
335b48d9
SV
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
ea3b1ea5
SV
150 * cp_transport_data. We quiesce these threads by setting
151 * cp_state to something other than RDS_CONN_UP, and then
335b48d9
SV
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().
9c79440e
SV
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()
335b48d9 165 */
ea3b1ea5
SV
166 atomic_set(&cp->cp_state, RDS_CONN_RESETTING);
167 wait_event(cp->cp_waitq, !test_bit(RDS_IN_XMIT, &cp->cp_flags));
335b48d9
SV
168 lock_sock(osock->sk);
169 /* reset receive side state for rds_tcp_data_recv() for osock */
ac3615e7
SV
170 cancel_delayed_work_sync(&cp->cp_send_w);
171 cancel_delayed_work_sync(&cp->cp_recv_w);
335b48d9
SV
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;
ac3615e7 178 rds_tcp_restore_callbacks(osock, tc);
335b48d9
SV
179 release_sock(osock->sk);
180 sock_release(osock);
181newsock:
ea3b1ea5 182 rds_send_path_reset(cp);
335b48d9 183 lock_sock(sock->sk);
ac3615e7 184 rds_tcp_set_callbacks(sock, cp);
335b48d9
SV
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
70041088 191 */
ea3b1ea5 192void rds_tcp_set_callbacks(struct socket *sock, struct rds_conn_path *cp)
70041088 193{
ea3b1ea5 194 struct rds_tcp_connection *tc = cp->cp_transport_data;
70041088
AG
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 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;
ea3b1ea5 210 tc->t_cpath = cp;
70041088
AG
211 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
ea3b1ea5 215 sock->sk->sk_user_data = cp;
70041088
AG
216 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
223static void rds_tcp_tc_info(struct socket *sock, unsigned int len,
224 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;
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->ops->getname(sock, (struct sockaddr *)&sin, &sinlen, 0);
241 tsinfo.local_addr = sin.sin_addr.s_addr;
242 tsinfo.local_port = sin.sin_port;
243 sock->ops->getname(sock, (struct sockaddr *)&sin, &sinlen, 1);
244 tsinfo.peer_addr = sin.sin_addr.s_addr;
245 tsinfo.peer_port = sin.sin_port;
246
247 tsinfo.hdr_rem = tc->t_tinc_hdr_rem;
248 tsinfo.data_rem = tc->t_tinc_data_rem;
249 tsinfo.last_sent_nxt = tc->t_last_sent_nxt;
250 tsinfo.last_expected_una = tc->t_last_expected_una;
251 tsinfo.last_seen_una = tc->t_last_seen_una;
252
253 rds_info_copy(iter, &tsinfo, sizeof(tsinfo));
254 }
255
256out:
257 lens->nr = rds_tcp_tc_count;
258 lens->each = sizeof(tsinfo);
259
260 spin_unlock_irqrestore(&rds_tcp_tc_list_lock, flags);
261}
262
d5a8ac28 263static int rds_tcp_laddr_check(struct net *net, __be32 addr)
70041088 264{
d5a8ac28 265 if (inet_addr_type(net, addr) == RTN_LOCAL)
70041088
AG
266 return 0;
267 return -EADDRNOTAVAIL;
268}
269
270static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp)
271{
272 struct rds_tcp_connection *tc;
02105b2c 273 int i;
70041088 274
02105b2c
SV
275 for (i = 0; i < RDS_MPATH_WORKERS; i++) {
276 tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp);
277 if (!tc)
278 return -ENOMEM;
70041088 279
02105b2c
SV
280 mutex_init(&tc->t_conn_path_lock);
281 tc->t_sock = NULL;
282 tc->t_tinc = NULL;
283 tc->t_tinc_hdr_rem = sizeof(struct rds_header);
284 tc->t_tinc_data_rem = 0;
70041088 285
02105b2c
SV
286 conn->c_path[i].cp_transport_data = tc;
287 tc->t_cpath = &conn->c_path[i];
70041088 288
02105b2c
SV
289 spin_lock_irq(&rds_tcp_conn_lock);
290 list_add_tail(&tc->t_tcp_node, &rds_tcp_conn_list);
291 spin_unlock_irq(&rds_tcp_conn_lock);
292 rdsdebug("rds_conn_path [%d] tc %p\n", i,
293 conn->c_path[i].cp_transport_data);
294 }
70041088 295
70041088
AG
296 return 0;
297}
298
299static void rds_tcp_conn_free(void *arg)
300{
301 struct rds_tcp_connection *tc = arg;
8200a59f 302 unsigned long flags;
70041088 303 rdsdebug("freeing tc %p\n", tc);
8200a59f
PE
304
305 spin_lock_irqsave(&rds_tcp_conn_lock, flags);
306 list_del(&tc->t_tcp_node);
307 spin_unlock_irqrestore(&rds_tcp_conn_lock, flags);
308
70041088
AG
309 kmem_cache_free(rds_tcp_conn_slab, tc);
310}
311
afb4164d
SV
312static bool list_has_conn(struct list_head *list, struct rds_connection *conn)
313{
314 struct rds_tcp_connection *tc, *_tc;
315
316 list_for_each_entry_safe(tc, _tc, list, t_tcp_node) {
317 if (tc->t_cpath->cp_conn == conn)
318 return true;
319 }
320 return false;
321}
322
70041088
AG
323static void rds_tcp_destroy_conns(void)
324{
325 struct rds_tcp_connection *tc, *_tc;
326 LIST_HEAD(tmp_list);
327
328 /* avoid calling conn_destroy with irqs off */
329 spin_lock_irq(&rds_tcp_conn_lock);
afb4164d
SV
330 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
331 if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn))
332 list_move_tail(&tc->t_tcp_node, &tmp_list);
333 }
70041088
AG
334 spin_unlock_irq(&rds_tcp_conn_lock);
335
26e4e6bb 336 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node)
02105b2c 337 rds_conn_destroy(tc->t_cpath->cp_conn);
70041088
AG
338}
339
467fa153 340static void rds_tcp_exit(void);
70041088
AG
341
342struct rds_transport rds_tcp_transport = {
343 .laddr_check = rds_tcp_laddr_check,
226f7a7d
SV
344 .xmit_path_prepare = rds_tcp_xmit_path_prepare,
345 .xmit_path_complete = rds_tcp_xmit_path_complete,
70041088 346 .xmit = rds_tcp_xmit,
2da43c4a 347 .recv_path = rds_tcp_recv_path,
70041088
AG
348 .conn_alloc = rds_tcp_conn_alloc,
349 .conn_free = rds_tcp_conn_free,
b04e8554 350 .conn_path_connect = rds_tcp_conn_path_connect,
226f7a7d 351 .conn_path_shutdown = rds_tcp_conn_path_shutdown,
70041088 352 .inc_copy_to_user = rds_tcp_inc_copy_to_user,
70041088
AG
353 .inc_free = rds_tcp_inc_free,
354 .stats_info_copy = rds_tcp_stats_info_copy,
355 .exit = rds_tcp_exit,
356 .t_owner = THIS_MODULE,
357 .t_name = "tcp",
335776bd 358 .t_type = RDS_TRANS_TCP,
70041088 359 .t_prefer_loopback = 1,
5916e2c1 360 .t_mp_capable = 1,
70041088
AG
361};
362
467fa153
SV
363static int rds_tcp_netid;
364
365/* per-network namespace private data for this module */
366struct rds_tcp_net {
367 struct socket *rds_tcp_listen_sock;
368 struct work_struct rds_tcp_accept_w;
c6a58ffe
SV
369 struct ctl_table_header *rds_tcp_sysctl;
370 struct ctl_table *ctl_table;
371 int sndbuf_size;
372 int rcvbuf_size;
467fa153
SV
373};
374
c6a58ffe
SV
375/* All module specific customizations to the RDS-TCP socket should be done in
376 * rds_tcp_tune() and applied after socket creation.
377 */
378void rds_tcp_tune(struct socket *sock)
379{
380 struct sock *sk = sock->sk;
381 struct net *net = sock_net(sk);
382 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
383
384 rds_tcp_nonagle(sock);
385 lock_sock(sk);
386 if (rtn->sndbuf_size > 0) {
387 sk->sk_sndbuf = rtn->sndbuf_size;
388 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
389 }
390 if (rtn->rcvbuf_size > 0) {
391 sk->sk_sndbuf = rtn->rcvbuf_size;
392 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
393 }
394 release_sock(sk);
395}
396
467fa153
SV
397static void rds_tcp_accept_worker(struct work_struct *work)
398{
399 struct rds_tcp_net *rtn = container_of(work,
400 struct rds_tcp_net,
401 rds_tcp_accept_w);
402
403 while (rds_tcp_accept_one(rtn->rds_tcp_listen_sock) == 0)
404 cond_resched();
405}
406
407void rds_tcp_accept_work(struct sock *sk)
408{
409 struct net *net = sock_net(sk);
410 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
411
412 queue_work(rds_wq, &rtn->rds_tcp_accept_w);
413}
414
415static __net_init int rds_tcp_init_net(struct net *net)
416{
417 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
c6a58ffe
SV
418 struct ctl_table *tbl;
419 int err = 0;
467fa153 420
c6a58ffe
SV
421 memset(rtn, 0, sizeof(*rtn));
422
423 /* {snd, rcv}buf_size default to 0, which implies we let the
424 * stack pick the value, and permit auto-tuning of buffer size.
425 */
426 if (net == &init_net) {
427 tbl = rds_tcp_sysctl_table;
428 } else {
429 tbl = kmemdup(rds_tcp_sysctl_table,
430 sizeof(rds_tcp_sysctl_table), GFP_KERNEL);
431 if (!tbl) {
432 pr_warn("could not set allocate syctl table\n");
433 return -ENOMEM;
434 }
435 rtn->ctl_table = tbl;
436 }
437 tbl[RDS_TCP_SNDBUF].data = &rtn->sndbuf_size;
438 tbl[RDS_TCP_RCVBUF].data = &rtn->rcvbuf_size;
439 rtn->rds_tcp_sysctl = register_net_sysctl(net, "net/rds/tcp", tbl);
440 if (!rtn->rds_tcp_sysctl) {
441 pr_warn("could not register sysctl\n");
442 err = -ENOMEM;
443 goto fail;
444 }
467fa153
SV
445 rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net);
446 if (!rtn->rds_tcp_listen_sock) {
447 pr_warn("could not set up listen sock\n");
c6a58ffe
SV
448 unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
449 rtn->rds_tcp_sysctl = NULL;
450 err = -EAFNOSUPPORT;
451 goto fail;
467fa153
SV
452 }
453 INIT_WORK(&rtn->rds_tcp_accept_w, rds_tcp_accept_worker);
454 return 0;
c6a58ffe
SV
455
456fail:
457 if (net != &init_net)
458 kfree(tbl);
459 return err;
467fa153
SV
460}
461
462static void __net_exit rds_tcp_exit_net(struct net *net)
463{
464 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
465
c6a58ffe
SV
466 if (rtn->rds_tcp_sysctl)
467 unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
468
469 if (net != &init_net && rtn->ctl_table)
470 kfree(rtn->ctl_table);
471
467fa153
SV
472 /* If rds_tcp_exit_net() is called as a result of netns deletion,
473 * the rds_tcp_kill_sock() device notifier would already have cleaned
474 * up the listen socket, thus there is no work to do in this function.
475 *
476 * If rds_tcp_exit_net() is called as a result of module unload,
477 * i.e., due to rds_tcp_exit() -> unregister_pernet_subsys(), then
478 * we do need to clean up the listen socket here.
479 */
480 if (rtn->rds_tcp_listen_sock) {
481 rds_tcp_listen_stop(rtn->rds_tcp_listen_sock);
482 rtn->rds_tcp_listen_sock = NULL;
483 flush_work(&rtn->rds_tcp_accept_w);
484 }
485}
486
487static struct pernet_operations rds_tcp_net_ops = {
488 .init = rds_tcp_init_net,
489 .exit = rds_tcp_exit_net,
490 .id = &rds_tcp_netid,
491 .size = sizeof(struct rds_tcp_net),
492};
493
afb4164d
SV
494/* explicitly send a RST on each socket, thereby releasing any socket refcnts
495 * that may otherwise hold up netns deletion.
496 */
497static void rds_tcp_conn_paths_destroy(struct rds_connection *conn)
498{
499 struct rds_conn_path *cp;
500 struct rds_tcp_connection *tc;
501 int i;
502 struct sock *sk;
503
504 for (i = 0; i < RDS_MPATH_WORKERS; i++) {
505 cp = &conn->c_path[i];
506 tc = cp->cp_transport_data;
507 if (!tc->t_sock)
508 continue;
509 sk = tc->t_sock->sk;
510 sk->sk_prot->disconnect(sk, 0);
511 tcp_done(sk);
512 }
513}
514
467fa153
SV
515static void rds_tcp_kill_sock(struct net *net)
516{
517 struct rds_tcp_connection *tc, *_tc;
467fa153
SV
518 LIST_HEAD(tmp_list);
519 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
520
521 rds_tcp_listen_stop(rtn->rds_tcp_listen_sock);
522 rtn->rds_tcp_listen_sock = NULL;
523 flush_work(&rtn->rds_tcp_accept_w);
524 spin_lock_irq(&rds_tcp_conn_lock);
525 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
02105b2c 526 struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net);
467fa153
SV
527
528 if (net != c_net || !tc->t_sock)
529 continue;
afb4164d
SV
530 if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn))
531 list_move_tail(&tc->t_tcp_node, &tmp_list);
467fa153
SV
532 }
533 spin_unlock_irq(&rds_tcp_conn_lock);
534 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node) {
afb4164d 535 rds_tcp_conn_paths_destroy(tc->t_cpath->cp_conn);
02105b2c 536 rds_conn_destroy(tc->t_cpath->cp_conn);
467fa153
SV
537 }
538}
539
a93d01f5
SV
540void *rds_tcp_listen_sock_def_readable(struct net *net)
541{
542 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
543
544 return rtn->rds_tcp_listen_sock->sk->sk_user_data;
545}
546
467fa153
SV
547static int rds_tcp_dev_event(struct notifier_block *this,
548 unsigned long event, void *ptr)
549{
550 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
551
552 /* rds-tcp registers as a pernet subys, so the ->exit will only
553 * get invoked after network acitivity has quiesced. We need to
554 * clean up all sockets to quiesce network activity, and use
555 * the unregistration of the per-net loopback device as a trigger
556 * to start that cleanup.
557 */
558 if (event == NETDEV_UNREGISTER_FINAL &&
559 dev->ifindex == LOOPBACK_IFINDEX)
560 rds_tcp_kill_sock(dev_net(dev));
561
562 return NOTIFY_DONE;
563}
564
565static struct notifier_block rds_tcp_dev_notifier = {
566 .notifier_call = rds_tcp_dev_event,
567 .priority = -10, /* must be called after other network notifiers */
568};
569
c6a58ffe
SV
570/* when sysctl is used to modify some kernel socket parameters,this
571 * function resets the RDS connections in that netns so that we can
572 * restart with new parameters. The assumption is that such reset
573 * events are few and far-between.
574 */
575static void rds_tcp_sysctl_reset(struct net *net)
576{
577 struct rds_tcp_connection *tc, *_tc;
578
579 spin_lock_irq(&rds_tcp_conn_lock);
580 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
02105b2c 581 struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net);
c6a58ffe
SV
582
583 if (net != c_net || !tc->t_sock)
584 continue;
585
02105b2c
SV
586 /* reconnect with new parameters */
587 rds_conn_path_drop(tc->t_cpath);
c6a58ffe
SV
588 }
589 spin_unlock_irq(&rds_tcp_conn_lock);
590}
591
592static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write,
593 void __user *buffer, size_t *lenp,
594 loff_t *fpos)
595{
596 struct net *net = current->nsproxy->net_ns;
597 int err;
598
599 err = proc_dointvec_minmax(ctl, write, buffer, lenp, fpos);
600 if (err < 0) {
601 pr_warn("Invalid input. Must be >= %d\n",
602 *(int *)(ctl->extra1));
603 return err;
604 }
605 if (write)
606 rds_tcp_sysctl_reset(net);
607 return 0;
608}
609
467fa153
SV
610static void rds_tcp_exit(void)
611{
612 rds_info_deregister_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
613 unregister_pernet_subsys(&rds_tcp_net_ops);
614 if (unregister_netdevice_notifier(&rds_tcp_dev_notifier))
615 pr_warn("could not unregister rds_tcp_dev_notifier\n");
616 rds_tcp_destroy_conns();
617 rds_trans_unregister(&rds_tcp_transport);
618 rds_tcp_recv_exit();
619 kmem_cache_destroy(rds_tcp_conn_slab);
620}
621module_exit(rds_tcp_exit);
622
ff51bf84 623static int rds_tcp_init(void)
70041088
AG
624{
625 int ret;
626
627 rds_tcp_conn_slab = kmem_cache_create("rds_tcp_connection",
628 sizeof(struct rds_tcp_connection),
629 0, 0, NULL);
8690bfa1 630 if (!rds_tcp_conn_slab) {
70041088
AG
631 ret = -ENOMEM;
632 goto out;
633 }
634
467fa153
SV
635 ret = register_netdevice_notifier(&rds_tcp_dev_notifier);
636 if (ret) {
637 pr_warn("could not register rds_tcp_dev_notifier\n");
638 goto out;
639 }
640
641 ret = register_pernet_subsys(&rds_tcp_net_ops);
642 if (ret)
643 goto out_slab;
644
70041088
AG
645 ret = rds_tcp_recv_init();
646 if (ret)
3dad5424 647 goto out_pernet;
70041088
AG
648
649 ret = rds_trans_register(&rds_tcp_transport);
650 if (ret)
651 goto out_recv;
652
70041088
AG
653 rds_info_register_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
654
655 goto out;
656
70041088
AG
657out_recv:
658 rds_tcp_recv_exit();
3dad5424 659out_pernet:
467fa153 660 unregister_pernet_subsys(&rds_tcp_net_ops);
3dad5424 661out_slab:
70041088
AG
662 kmem_cache_destroy(rds_tcp_conn_slab);
663out:
664 return ret;
665}
666module_init(rds_tcp_init);
667
668MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
669MODULE_DESCRIPTION("RDS: TCP transport");
670MODULE_LICENSE("Dual BSD/GPL");
671