blob: 8faa0b1ae39dd8bbd2f5fe44610ae1ebe4d1c64c [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
59int rds_tcp_min_sndbuf = SOCK_MIN_SNDBUF;
60int rds_tcp_min_rcvbuf = SOCK_MIN_RCVBUF;
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
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,
138 struct rds_connection *conn)
139{
140 struct rds_tcp_connection *tc = conn->c_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 * c_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 lock_sock(osock->sk);
157 /* reset receive side state for rds_tcp_data_recv() for osock */
158 if (tc->t_tinc) {
159 rds_inc_put(&tc->t_tinc->ti_inc);
160 tc->t_tinc = NULL;
161 }
162 tc->t_tinc_hdr_rem = sizeof(struct rds_header);
163 tc->t_tinc_data_rem = 0;
164 tc->t_sock = NULL;
165
166 write_lock_bh(&osock->sk->sk_callback_lock);
167
168 osock->sk->sk_user_data = NULL;
169 osock->sk->sk_data_ready = tc->t_orig_data_ready;
170 osock->sk->sk_write_space = tc->t_orig_write_space;
171 osock->sk->sk_state_change = tc->t_orig_state_change;
172 write_unlock_bh(&osock->sk->sk_callback_lock);
173 release_sock(osock->sk);
174 sock_release(osock);
175newsock:
176 lock_sock(sock->sk);
177 write_lock_bh(&sock->sk->sk_callback_lock);
178 tc->t_sock = sock;
179 sock->sk->sk_user_data = conn;
180 sock->sk->sk_data_ready = rds_tcp_data_ready;
181 sock->sk->sk_write_space = rds_tcp_write_space;
182 sock->sk->sk_state_change = rds_tcp_state_change;
183
184 write_unlock_bh(&sock->sk->sk_callback_lock);
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
Andy Grover70041082009-08-21 12:28:31 +0000191 */
192void rds_tcp_set_callbacks(struct socket *sock, struct rds_connection *conn)
193{
194 struct rds_tcp_connection *tc = conn->c_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 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;
210 tc->conn = conn;
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
215 sock->sk->sk_user_data = conn;
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
Sowmini Varadhand5a8ac22015-08-05 01:43:25 -0400263static int rds_tcp_laddr_check(struct net *net, __be32 addr)
Andy Grover70041082009-08-21 12:28:31 +0000264{
Sowmini Varadhand5a8ac22015-08-05 01:43:25 -0400265 if (inet_addr_type(net, addr) == RTN_LOCAL)
Andy Grover70041082009-08-21 12:28:31 +0000266 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;
273
274 tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp);
Andy Grover8690bfa2010-01-12 11:56:44 -0800275 if (!tc)
Andy Grover70041082009-08-21 12:28:31 +0000276 return -ENOMEM;
277
Sowmini Varadhanbd7c5f92016-05-02 11:24:52 -0700278 mutex_init(&tc->t_conn_lock);
Andy Grover70041082009-08-21 12:28:31 +0000279 tc->t_sock = NULL;
280 tc->t_tinc = NULL;
281 tc->t_tinc_hdr_rem = sizeof(struct rds_header);
282 tc->t_tinc_data_rem = 0;
283
284 conn->c_transport_data = tc;
285
286 spin_lock_irq(&rds_tcp_conn_lock);
287 list_add_tail(&tc->t_tcp_node, &rds_tcp_conn_list);
288 spin_unlock_irq(&rds_tcp_conn_lock);
289
290 rdsdebug("alloced tc %p\n", conn->c_transport_data);
291 return 0;
292}
293
294static void rds_tcp_conn_free(void *arg)
295{
296 struct rds_tcp_connection *tc = arg;
Pavel Emelyanov8200a592010-11-02 01:54:01 +0000297 unsigned long flags;
Andy Grover70041082009-08-21 12:28:31 +0000298 rdsdebug("freeing tc %p\n", tc);
Pavel Emelyanov8200a592010-11-02 01:54:01 +0000299
300 spin_lock_irqsave(&rds_tcp_conn_lock, flags);
301 list_del(&tc->t_tcp_node);
302 spin_unlock_irqrestore(&rds_tcp_conn_lock, flags);
303
Andy Grover70041082009-08-21 12:28:31 +0000304 kmem_cache_free(rds_tcp_conn_slab, tc);
305}
306
307static void rds_tcp_destroy_conns(void)
308{
309 struct rds_tcp_connection *tc, *_tc;
310 LIST_HEAD(tmp_list);
311
312 /* avoid calling conn_destroy with irqs off */
313 spin_lock_irq(&rds_tcp_conn_lock);
314 list_splice(&rds_tcp_conn_list, &tmp_list);
315 INIT_LIST_HEAD(&rds_tcp_conn_list);
316 spin_unlock_irq(&rds_tcp_conn_lock);
317
318 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node) {
319 if (tc->conn->c_passive)
320 rds_conn_destroy(tc->conn->c_passive);
321 rds_conn_destroy(tc->conn);
322 }
323}
324
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400325static void rds_tcp_exit(void);
Andy Grover70041082009-08-21 12:28:31 +0000326
327struct rds_transport rds_tcp_transport = {
328 .laddr_check = rds_tcp_laddr_check,
329 .xmit_prepare = rds_tcp_xmit_prepare,
330 .xmit_complete = rds_tcp_xmit_complete,
Andy Grover70041082009-08-21 12:28:31 +0000331 .xmit = rds_tcp_xmit,
332 .recv = rds_tcp_recv,
333 .conn_alloc = rds_tcp_conn_alloc,
334 .conn_free = rds_tcp_conn_free,
335 .conn_connect = rds_tcp_conn_connect,
336 .conn_shutdown = rds_tcp_conn_shutdown,
337 .inc_copy_to_user = rds_tcp_inc_copy_to_user,
Andy Grover70041082009-08-21 12:28:31 +0000338 .inc_free = rds_tcp_inc_free,
339 .stats_info_copy = rds_tcp_stats_info_copy,
340 .exit = rds_tcp_exit,
341 .t_owner = THIS_MODULE,
342 .t_name = "tcp",
Andy Grover335776b2009-08-21 12:28:34 +0000343 .t_type = RDS_TRANS_TCP,
Andy Grover70041082009-08-21 12:28:31 +0000344 .t_prefer_loopback = 1,
345};
346
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400347static int rds_tcp_netid;
348
349/* per-network namespace private data for this module */
350struct rds_tcp_net {
351 struct socket *rds_tcp_listen_sock;
352 struct work_struct rds_tcp_accept_w;
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700353 struct ctl_table_header *rds_tcp_sysctl;
354 struct ctl_table *ctl_table;
355 int sndbuf_size;
356 int rcvbuf_size;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400357};
358
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700359/* All module specific customizations to the RDS-TCP socket should be done in
360 * rds_tcp_tune() and applied after socket creation.
361 */
362void rds_tcp_tune(struct socket *sock)
363{
364 struct sock *sk = sock->sk;
365 struct net *net = sock_net(sk);
366 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
367
368 rds_tcp_nonagle(sock);
369 lock_sock(sk);
370 if (rtn->sndbuf_size > 0) {
371 sk->sk_sndbuf = rtn->sndbuf_size;
372 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
373 }
374 if (rtn->rcvbuf_size > 0) {
375 sk->sk_sndbuf = rtn->rcvbuf_size;
376 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
377 }
378 release_sock(sk);
379}
380
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400381static void rds_tcp_accept_worker(struct work_struct *work)
382{
383 struct rds_tcp_net *rtn = container_of(work,
384 struct rds_tcp_net,
385 rds_tcp_accept_w);
386
387 while (rds_tcp_accept_one(rtn->rds_tcp_listen_sock) == 0)
388 cond_resched();
389}
390
391void rds_tcp_accept_work(struct sock *sk)
392{
393 struct net *net = sock_net(sk);
394 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
395
396 queue_work(rds_wq, &rtn->rds_tcp_accept_w);
397}
398
399static __net_init int rds_tcp_init_net(struct net *net)
400{
401 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700402 struct ctl_table *tbl;
403 int err = 0;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400404
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700405 memset(rtn, 0, sizeof(*rtn));
406
407 /* {snd, rcv}buf_size default to 0, which implies we let the
408 * stack pick the value, and permit auto-tuning of buffer size.
409 */
410 if (net == &init_net) {
411 tbl = rds_tcp_sysctl_table;
412 } else {
413 tbl = kmemdup(rds_tcp_sysctl_table,
414 sizeof(rds_tcp_sysctl_table), GFP_KERNEL);
415 if (!tbl) {
416 pr_warn("could not set allocate syctl table\n");
417 return -ENOMEM;
418 }
419 rtn->ctl_table = tbl;
420 }
421 tbl[RDS_TCP_SNDBUF].data = &rtn->sndbuf_size;
422 tbl[RDS_TCP_RCVBUF].data = &rtn->rcvbuf_size;
423 rtn->rds_tcp_sysctl = register_net_sysctl(net, "net/rds/tcp", tbl);
424 if (!rtn->rds_tcp_sysctl) {
425 pr_warn("could not register sysctl\n");
426 err = -ENOMEM;
427 goto fail;
428 }
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400429 rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net);
430 if (!rtn->rds_tcp_listen_sock) {
431 pr_warn("could not set up listen sock\n");
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700432 unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
433 rtn->rds_tcp_sysctl = NULL;
434 err = -EAFNOSUPPORT;
435 goto fail;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400436 }
437 INIT_WORK(&rtn->rds_tcp_accept_w, rds_tcp_accept_worker);
438 return 0;
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700439
440fail:
441 if (net != &init_net)
442 kfree(tbl);
443 return err;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400444}
445
446static void __net_exit rds_tcp_exit_net(struct net *net)
447{
448 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
449
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700450 if (rtn->rds_tcp_sysctl)
451 unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
452
453 if (net != &init_net && rtn->ctl_table)
454 kfree(rtn->ctl_table);
455
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400456 /* If rds_tcp_exit_net() is called as a result of netns deletion,
457 * the rds_tcp_kill_sock() device notifier would already have cleaned
458 * up the listen socket, thus there is no work to do in this function.
459 *
460 * If rds_tcp_exit_net() is called as a result of module unload,
461 * i.e., due to rds_tcp_exit() -> unregister_pernet_subsys(), then
462 * we do need to clean up the listen socket here.
463 */
464 if (rtn->rds_tcp_listen_sock) {
465 rds_tcp_listen_stop(rtn->rds_tcp_listen_sock);
466 rtn->rds_tcp_listen_sock = NULL;
467 flush_work(&rtn->rds_tcp_accept_w);
468 }
469}
470
471static struct pernet_operations rds_tcp_net_ops = {
472 .init = rds_tcp_init_net,
473 .exit = rds_tcp_exit_net,
474 .id = &rds_tcp_netid,
475 .size = sizeof(struct rds_tcp_net),
476};
477
478static void rds_tcp_kill_sock(struct net *net)
479{
480 struct rds_tcp_connection *tc, *_tc;
481 struct sock *sk;
482 LIST_HEAD(tmp_list);
483 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
484
485 rds_tcp_listen_stop(rtn->rds_tcp_listen_sock);
486 rtn->rds_tcp_listen_sock = NULL;
487 flush_work(&rtn->rds_tcp_accept_w);
488 spin_lock_irq(&rds_tcp_conn_lock);
489 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
490 struct net *c_net = read_pnet(&tc->conn->c_net);
491
492 if (net != c_net || !tc->t_sock)
493 continue;
494 list_move_tail(&tc->t_tcp_node, &tmp_list);
495 }
496 spin_unlock_irq(&rds_tcp_conn_lock);
497 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node) {
498 sk = tc->t_sock->sk;
499 sk->sk_prot->disconnect(sk, 0);
500 tcp_done(sk);
501 if (tc->conn->c_passive)
502 rds_conn_destroy(tc->conn->c_passive);
503 rds_conn_destroy(tc->conn);
504 }
505}
506
507static int rds_tcp_dev_event(struct notifier_block *this,
508 unsigned long event, void *ptr)
509{
510 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
511
512 /* rds-tcp registers as a pernet subys, so the ->exit will only
513 * get invoked after network acitivity has quiesced. We need to
514 * clean up all sockets to quiesce network activity, and use
515 * the unregistration of the per-net loopback device as a trigger
516 * to start that cleanup.
517 */
518 if (event == NETDEV_UNREGISTER_FINAL &&
519 dev->ifindex == LOOPBACK_IFINDEX)
520 rds_tcp_kill_sock(dev_net(dev));
521
522 return NOTIFY_DONE;
523}
524
525static struct notifier_block rds_tcp_dev_notifier = {
526 .notifier_call = rds_tcp_dev_event,
527 .priority = -10, /* must be called after other network notifiers */
528};
529
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700530/* when sysctl is used to modify some kernel socket parameters,this
531 * function resets the RDS connections in that netns so that we can
532 * restart with new parameters. The assumption is that such reset
533 * events are few and far-between.
534 */
535static void rds_tcp_sysctl_reset(struct net *net)
536{
537 struct rds_tcp_connection *tc, *_tc;
538
539 spin_lock_irq(&rds_tcp_conn_lock);
540 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
541 struct net *c_net = read_pnet(&tc->conn->c_net);
542
543 if (net != c_net || !tc->t_sock)
544 continue;
545
546 rds_conn_drop(tc->conn); /* reconnect with new parameters */
547 }
548 spin_unlock_irq(&rds_tcp_conn_lock);
549}
550
551static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write,
552 void __user *buffer, size_t *lenp,
553 loff_t *fpos)
554{
555 struct net *net = current->nsproxy->net_ns;
556 int err;
557
558 err = proc_dointvec_minmax(ctl, write, buffer, lenp, fpos);
559 if (err < 0) {
560 pr_warn("Invalid input. Must be >= %d\n",
561 *(int *)(ctl->extra1));
562 return err;
563 }
564 if (write)
565 rds_tcp_sysctl_reset(net);
566 return 0;
567}
568
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400569static void rds_tcp_exit(void)
570{
571 rds_info_deregister_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
572 unregister_pernet_subsys(&rds_tcp_net_ops);
573 if (unregister_netdevice_notifier(&rds_tcp_dev_notifier))
574 pr_warn("could not unregister rds_tcp_dev_notifier\n");
575 rds_tcp_destroy_conns();
576 rds_trans_unregister(&rds_tcp_transport);
577 rds_tcp_recv_exit();
578 kmem_cache_destroy(rds_tcp_conn_slab);
579}
580module_exit(rds_tcp_exit);
581
stephen hemmingerff51bf82010-10-19 08:08:33 +0000582static int rds_tcp_init(void)
Andy Grover70041082009-08-21 12:28:31 +0000583{
584 int ret;
585
586 rds_tcp_conn_slab = kmem_cache_create("rds_tcp_connection",
587 sizeof(struct rds_tcp_connection),
588 0, 0, NULL);
Andy Grover8690bfa2010-01-12 11:56:44 -0800589 if (!rds_tcp_conn_slab) {
Andy Grover70041082009-08-21 12:28:31 +0000590 ret = -ENOMEM;
591 goto out;
592 }
593
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400594 ret = register_netdevice_notifier(&rds_tcp_dev_notifier);
595 if (ret) {
596 pr_warn("could not register rds_tcp_dev_notifier\n");
597 goto out;
598 }
599
600 ret = register_pernet_subsys(&rds_tcp_net_ops);
601 if (ret)
602 goto out_slab;
603
Andy Grover70041082009-08-21 12:28:31 +0000604 ret = rds_tcp_recv_init();
605 if (ret)
606 goto out_slab;
607
608 ret = rds_trans_register(&rds_tcp_transport);
609 if (ret)
610 goto out_recv;
611
Andy Grover70041082009-08-21 12:28:31 +0000612 rds_info_register_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
613
614 goto out;
615
Andy Grover70041082009-08-21 12:28:31 +0000616out_recv:
617 rds_tcp_recv_exit();
618out_slab:
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400619 unregister_pernet_subsys(&rds_tcp_net_ops);
Andy Grover70041082009-08-21 12:28:31 +0000620 kmem_cache_destroy(rds_tcp_conn_slab);
621out:
622 return ret;
623}
624module_init(rds_tcp_init);
625
626MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
627MODULE_DESCRIPTION("RDS: TCP transport");
628MODULE_LICENSE("Dual BSD/GPL");
629