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