RDS: TCP: Synchronize accept() and connect() paths on t_conn_lock.
[deliverable/linux.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
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
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/*
129 * This is the only path that sets tc->t_sock. Send and receive trust that
eb192840 130 * it is set. The RDS_CONN_UP bit protects those paths from being
70041088
AG
131 * called while it isn't set.
132 */
133void rds_tcp_set_callbacks(struct socket *sock, struct rds_connection *conn)
134{
135 struct rds_tcp_connection *tc = conn->c_transport_data;
136
137 rdsdebug("setting sock %p callbacks to tc %p\n", sock, tc);
138 write_lock_bh(&sock->sk->sk_callback_lock);
139
140 /* done under the callback_lock to serialize with write_space */
141 spin_lock(&rds_tcp_tc_list_lock);
142 list_add_tail(&tc->t_list_item, &rds_tcp_tc_list);
143 rds_tcp_tc_count++;
144 spin_unlock(&rds_tcp_tc_list_lock);
145
146 /* accepted sockets need our listen data ready undone */
147 if (sock->sk->sk_data_ready == rds_tcp_listen_data_ready)
148 sock->sk->sk_data_ready = sock->sk->sk_user_data;
149
150 tc->t_sock = sock;
151 tc->conn = conn;
152 tc->t_orig_data_ready = sock->sk->sk_data_ready;
153 tc->t_orig_write_space = sock->sk->sk_write_space;
154 tc->t_orig_state_change = sock->sk->sk_state_change;
155
156 sock->sk->sk_user_data = conn;
157 sock->sk->sk_data_ready = rds_tcp_data_ready;
158 sock->sk->sk_write_space = rds_tcp_write_space;
159 sock->sk->sk_state_change = rds_tcp_state_change;
160
161 write_unlock_bh(&sock->sk->sk_callback_lock);
162}
163
164static void rds_tcp_tc_info(struct socket *sock, unsigned int len,
165 struct rds_info_iterator *iter,
166 struct rds_info_lengths *lens)
167{
168 struct rds_info_tcp_socket tsinfo;
169 struct rds_tcp_connection *tc;
170 unsigned long flags;
171 struct sockaddr_in sin;
172 int sinlen;
173
174 spin_lock_irqsave(&rds_tcp_tc_list_lock, flags);
175
176 if (len / sizeof(tsinfo) < rds_tcp_tc_count)
177 goto out;
178
179 list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) {
180
181 sock->ops->getname(sock, (struct sockaddr *)&sin, &sinlen, 0);
182 tsinfo.local_addr = sin.sin_addr.s_addr;
183 tsinfo.local_port = sin.sin_port;
184 sock->ops->getname(sock, (struct sockaddr *)&sin, &sinlen, 1);
185 tsinfo.peer_addr = sin.sin_addr.s_addr;
186 tsinfo.peer_port = sin.sin_port;
187
188 tsinfo.hdr_rem = tc->t_tinc_hdr_rem;
189 tsinfo.data_rem = tc->t_tinc_data_rem;
190 tsinfo.last_sent_nxt = tc->t_last_sent_nxt;
191 tsinfo.last_expected_una = tc->t_last_expected_una;
192 tsinfo.last_seen_una = tc->t_last_seen_una;
193
194 rds_info_copy(iter, &tsinfo, sizeof(tsinfo));
195 }
196
197out:
198 lens->nr = rds_tcp_tc_count;
199 lens->each = sizeof(tsinfo);
200
201 spin_unlock_irqrestore(&rds_tcp_tc_list_lock, flags);
202}
203
d5a8ac28 204static int rds_tcp_laddr_check(struct net *net, __be32 addr)
70041088 205{
d5a8ac28 206 if (inet_addr_type(net, addr) == RTN_LOCAL)
70041088
AG
207 return 0;
208 return -EADDRNOTAVAIL;
209}
210
211static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp)
212{
213 struct rds_tcp_connection *tc;
214
215 tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp);
8690bfa1 216 if (!tc)
70041088
AG
217 return -ENOMEM;
218
bd7c5f98 219 mutex_init(&tc->t_conn_lock);
70041088
AG
220 tc->t_sock = NULL;
221 tc->t_tinc = NULL;
222 tc->t_tinc_hdr_rem = sizeof(struct rds_header);
223 tc->t_tinc_data_rem = 0;
224
225 conn->c_transport_data = tc;
226
227 spin_lock_irq(&rds_tcp_conn_lock);
228 list_add_tail(&tc->t_tcp_node, &rds_tcp_conn_list);
229 spin_unlock_irq(&rds_tcp_conn_lock);
230
231 rdsdebug("alloced tc %p\n", conn->c_transport_data);
232 return 0;
233}
234
235static void rds_tcp_conn_free(void *arg)
236{
237 struct rds_tcp_connection *tc = arg;
8200a59f 238 unsigned long flags;
70041088 239 rdsdebug("freeing tc %p\n", tc);
8200a59f
PE
240
241 spin_lock_irqsave(&rds_tcp_conn_lock, flags);
242 list_del(&tc->t_tcp_node);
243 spin_unlock_irqrestore(&rds_tcp_conn_lock, flags);
244
70041088
AG
245 kmem_cache_free(rds_tcp_conn_slab, tc);
246}
247
248static void rds_tcp_destroy_conns(void)
249{
250 struct rds_tcp_connection *tc, *_tc;
251 LIST_HEAD(tmp_list);
252
253 /* avoid calling conn_destroy with irqs off */
254 spin_lock_irq(&rds_tcp_conn_lock);
255 list_splice(&rds_tcp_conn_list, &tmp_list);
256 INIT_LIST_HEAD(&rds_tcp_conn_list);
257 spin_unlock_irq(&rds_tcp_conn_lock);
258
259 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node) {
260 if (tc->conn->c_passive)
261 rds_conn_destroy(tc->conn->c_passive);
262 rds_conn_destroy(tc->conn);
263 }
264}
265
467fa153 266static void rds_tcp_exit(void);
70041088
AG
267
268struct rds_transport rds_tcp_transport = {
269 .laddr_check = rds_tcp_laddr_check,
270 .xmit_prepare = rds_tcp_xmit_prepare,
271 .xmit_complete = rds_tcp_xmit_complete,
70041088
AG
272 .xmit = rds_tcp_xmit,
273 .recv = rds_tcp_recv,
274 .conn_alloc = rds_tcp_conn_alloc,
275 .conn_free = rds_tcp_conn_free,
276 .conn_connect = rds_tcp_conn_connect,
277 .conn_shutdown = rds_tcp_conn_shutdown,
278 .inc_copy_to_user = rds_tcp_inc_copy_to_user,
70041088
AG
279 .inc_free = rds_tcp_inc_free,
280 .stats_info_copy = rds_tcp_stats_info_copy,
281 .exit = rds_tcp_exit,
282 .t_owner = THIS_MODULE,
283 .t_name = "tcp",
335776bd 284 .t_type = RDS_TRANS_TCP,
70041088
AG
285 .t_prefer_loopback = 1,
286};
287
467fa153
SV
288static int rds_tcp_netid;
289
290/* per-network namespace private data for this module */
291struct rds_tcp_net {
292 struct socket *rds_tcp_listen_sock;
293 struct work_struct rds_tcp_accept_w;
c6a58ffe
SV
294 struct ctl_table_header *rds_tcp_sysctl;
295 struct ctl_table *ctl_table;
296 int sndbuf_size;
297 int rcvbuf_size;
467fa153
SV
298};
299
c6a58ffe
SV
300/* All module specific customizations to the RDS-TCP socket should be done in
301 * rds_tcp_tune() and applied after socket creation.
302 */
303void rds_tcp_tune(struct socket *sock)
304{
305 struct sock *sk = sock->sk;
306 struct net *net = sock_net(sk);
307 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
308
309 rds_tcp_nonagle(sock);
310 lock_sock(sk);
311 if (rtn->sndbuf_size > 0) {
312 sk->sk_sndbuf = rtn->sndbuf_size;
313 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
314 }
315 if (rtn->rcvbuf_size > 0) {
316 sk->sk_sndbuf = rtn->rcvbuf_size;
317 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
318 }
319 release_sock(sk);
320}
321
467fa153
SV
322static void rds_tcp_accept_worker(struct work_struct *work)
323{
324 struct rds_tcp_net *rtn = container_of(work,
325 struct rds_tcp_net,
326 rds_tcp_accept_w);
327
328 while (rds_tcp_accept_one(rtn->rds_tcp_listen_sock) == 0)
329 cond_resched();
330}
331
332void rds_tcp_accept_work(struct sock *sk)
333{
334 struct net *net = sock_net(sk);
335 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
336
337 queue_work(rds_wq, &rtn->rds_tcp_accept_w);
338}
339
340static __net_init int rds_tcp_init_net(struct net *net)
341{
342 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
c6a58ffe
SV
343 struct ctl_table *tbl;
344 int err = 0;
467fa153 345
c6a58ffe
SV
346 memset(rtn, 0, sizeof(*rtn));
347
348 /* {snd, rcv}buf_size default to 0, which implies we let the
349 * stack pick the value, and permit auto-tuning of buffer size.
350 */
351 if (net == &init_net) {
352 tbl = rds_tcp_sysctl_table;
353 } else {
354 tbl = kmemdup(rds_tcp_sysctl_table,
355 sizeof(rds_tcp_sysctl_table), GFP_KERNEL);
356 if (!tbl) {
357 pr_warn("could not set allocate syctl table\n");
358 return -ENOMEM;
359 }
360 rtn->ctl_table = tbl;
361 }
362 tbl[RDS_TCP_SNDBUF].data = &rtn->sndbuf_size;
363 tbl[RDS_TCP_RCVBUF].data = &rtn->rcvbuf_size;
364 rtn->rds_tcp_sysctl = register_net_sysctl(net, "net/rds/tcp", tbl);
365 if (!rtn->rds_tcp_sysctl) {
366 pr_warn("could not register sysctl\n");
367 err = -ENOMEM;
368 goto fail;
369 }
467fa153
SV
370 rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net);
371 if (!rtn->rds_tcp_listen_sock) {
372 pr_warn("could not set up listen sock\n");
c6a58ffe
SV
373 unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
374 rtn->rds_tcp_sysctl = NULL;
375 err = -EAFNOSUPPORT;
376 goto fail;
467fa153
SV
377 }
378 INIT_WORK(&rtn->rds_tcp_accept_w, rds_tcp_accept_worker);
379 return 0;
c6a58ffe
SV
380
381fail:
382 if (net != &init_net)
383 kfree(tbl);
384 return err;
467fa153
SV
385}
386
387static void __net_exit rds_tcp_exit_net(struct net *net)
388{
389 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
390
c6a58ffe
SV
391 if (rtn->rds_tcp_sysctl)
392 unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
393
394 if (net != &init_net && rtn->ctl_table)
395 kfree(rtn->ctl_table);
396
467fa153
SV
397 /* If rds_tcp_exit_net() is called as a result of netns deletion,
398 * the rds_tcp_kill_sock() device notifier would already have cleaned
399 * up the listen socket, thus there is no work to do in this function.
400 *
401 * If rds_tcp_exit_net() is called as a result of module unload,
402 * i.e., due to rds_tcp_exit() -> unregister_pernet_subsys(), then
403 * we do need to clean up the listen socket here.
404 */
405 if (rtn->rds_tcp_listen_sock) {
406 rds_tcp_listen_stop(rtn->rds_tcp_listen_sock);
407 rtn->rds_tcp_listen_sock = NULL;
408 flush_work(&rtn->rds_tcp_accept_w);
409 }
410}
411
412static struct pernet_operations rds_tcp_net_ops = {
413 .init = rds_tcp_init_net,
414 .exit = rds_tcp_exit_net,
415 .id = &rds_tcp_netid,
416 .size = sizeof(struct rds_tcp_net),
417};
418
419static void rds_tcp_kill_sock(struct net *net)
420{
421 struct rds_tcp_connection *tc, *_tc;
422 struct sock *sk;
423 LIST_HEAD(tmp_list);
424 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
425
426 rds_tcp_listen_stop(rtn->rds_tcp_listen_sock);
427 rtn->rds_tcp_listen_sock = NULL;
428 flush_work(&rtn->rds_tcp_accept_w);
429 spin_lock_irq(&rds_tcp_conn_lock);
430 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
431 struct net *c_net = read_pnet(&tc->conn->c_net);
432
433 if (net != c_net || !tc->t_sock)
434 continue;
435 list_move_tail(&tc->t_tcp_node, &tmp_list);
436 }
437 spin_unlock_irq(&rds_tcp_conn_lock);
438 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node) {
439 sk = tc->t_sock->sk;
440 sk->sk_prot->disconnect(sk, 0);
441 tcp_done(sk);
442 if (tc->conn->c_passive)
443 rds_conn_destroy(tc->conn->c_passive);
444 rds_conn_destroy(tc->conn);
445 }
446}
447
448static int rds_tcp_dev_event(struct notifier_block *this,
449 unsigned long event, void *ptr)
450{
451 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
452
453 /* rds-tcp registers as a pernet subys, so the ->exit will only
454 * get invoked after network acitivity has quiesced. We need to
455 * clean up all sockets to quiesce network activity, and use
456 * the unregistration of the per-net loopback device as a trigger
457 * to start that cleanup.
458 */
459 if (event == NETDEV_UNREGISTER_FINAL &&
460 dev->ifindex == LOOPBACK_IFINDEX)
461 rds_tcp_kill_sock(dev_net(dev));
462
463 return NOTIFY_DONE;
464}
465
466static struct notifier_block rds_tcp_dev_notifier = {
467 .notifier_call = rds_tcp_dev_event,
468 .priority = -10, /* must be called after other network notifiers */
469};
470
c6a58ffe
SV
471/* when sysctl is used to modify some kernel socket parameters,this
472 * function resets the RDS connections in that netns so that we can
473 * restart with new parameters. The assumption is that such reset
474 * events are few and far-between.
475 */
476static void rds_tcp_sysctl_reset(struct net *net)
477{
478 struct rds_tcp_connection *tc, *_tc;
479
480 spin_lock_irq(&rds_tcp_conn_lock);
481 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
482 struct net *c_net = read_pnet(&tc->conn->c_net);
483
484 if (net != c_net || !tc->t_sock)
485 continue;
486
487 rds_conn_drop(tc->conn); /* reconnect with new parameters */
488 }
489 spin_unlock_irq(&rds_tcp_conn_lock);
490}
491
492static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write,
493 void __user *buffer, size_t *lenp,
494 loff_t *fpos)
495{
496 struct net *net = current->nsproxy->net_ns;
497 int err;
498
499 err = proc_dointvec_minmax(ctl, write, buffer, lenp, fpos);
500 if (err < 0) {
501 pr_warn("Invalid input. Must be >= %d\n",
502 *(int *)(ctl->extra1));
503 return err;
504 }
505 if (write)
506 rds_tcp_sysctl_reset(net);
507 return 0;
508}
509
467fa153
SV
510static void rds_tcp_exit(void)
511{
512 rds_info_deregister_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
513 unregister_pernet_subsys(&rds_tcp_net_ops);
514 if (unregister_netdevice_notifier(&rds_tcp_dev_notifier))
515 pr_warn("could not unregister rds_tcp_dev_notifier\n");
516 rds_tcp_destroy_conns();
517 rds_trans_unregister(&rds_tcp_transport);
518 rds_tcp_recv_exit();
519 kmem_cache_destroy(rds_tcp_conn_slab);
520}
521module_exit(rds_tcp_exit);
522
ff51bf84 523static int rds_tcp_init(void)
70041088
AG
524{
525 int ret;
526
527 rds_tcp_conn_slab = kmem_cache_create("rds_tcp_connection",
528 sizeof(struct rds_tcp_connection),
529 0, 0, NULL);
8690bfa1 530 if (!rds_tcp_conn_slab) {
70041088
AG
531 ret = -ENOMEM;
532 goto out;
533 }
534
467fa153
SV
535 ret = register_netdevice_notifier(&rds_tcp_dev_notifier);
536 if (ret) {
537 pr_warn("could not register rds_tcp_dev_notifier\n");
538 goto out;
539 }
540
541 ret = register_pernet_subsys(&rds_tcp_net_ops);
542 if (ret)
543 goto out_slab;
544
70041088
AG
545 ret = rds_tcp_recv_init();
546 if (ret)
547 goto out_slab;
548
549 ret = rds_trans_register(&rds_tcp_transport);
550 if (ret)
551 goto out_recv;
552
70041088
AG
553 rds_info_register_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
554
555 goto out;
556
70041088
AG
557out_recv:
558 rds_tcp_recv_exit();
559out_slab:
467fa153 560 unregister_pernet_subsys(&rds_tcp_net_ops);
70041088
AG
561 kmem_cache_destroy(rds_tcp_conn_slab);
562out:
563 return ret;
564}
565module_init(rds_tcp_init);
566
567MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
568MODULE_DESCRIPTION("RDS: TCP transport");
569MODULE_LICENSE("Dual BSD/GPL");
570
This page took 0.419344 seconds and 5 git commands to generate.