Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / net / rds / tcp.c
blob61ed2a8764ba4c7a66788d7843cef18abb9e904a
1 /*
2 * Copyright (c) 2006 Oracle. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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.
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.
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/in.h>
36 #include <linux/module.h>
37 #include <net/tcp.h>
38 #include <net/net_namespace.h>
39 #include <net/netns/generic.h>
41 #include "rds.h"
42 #include "tcp.h"
44 /* only for info exporting */
45 static DEFINE_SPINLOCK(rds_tcp_tc_list_lock);
46 static LIST_HEAD(rds_tcp_tc_list);
47 static unsigned int rds_tcp_tc_count;
49 /* Track rds_tcp_connection structs so they can be cleaned up */
50 static DEFINE_SPINLOCK(rds_tcp_conn_lock);
51 static LIST_HEAD(rds_tcp_conn_list);
53 static struct kmem_cache *rds_tcp_conn_slab;
55 static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write,
56 void __user *buffer, size_t *lenp,
57 loff_t *fpos);
59 int rds_tcp_min_sndbuf = SOCK_MIN_SNDBUF;
60 int rds_tcp_min_rcvbuf = SOCK_MIN_RCVBUF;
62 static struct ctl_table rds_tcp_sysctl_table[] = {
63 #define RDS_TCP_SNDBUF 0
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,
72 #define RDS_TCP_RCVBUF 1
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,
81 { }
84 /* doing it this way avoids calling tcp_sk() */
85 void rds_tcp_nonagle(struct socket *sock)
87 mm_segment_t oldfs = get_fs();
88 int val = 1;
90 set_fs(KERNEL_DS);
91 sock->ops->setsockopt(sock, SOL_TCP, TCP_NODELAY, (char __user *)&val,
92 sizeof(val));
93 set_fs(oldfs);
96 u32 rds_tcp_snd_nxt(struct rds_tcp_connection *tc)
98 return tcp_sk(tc->t_sock->sk)->snd_nxt;
101 u32 rds_tcp_snd_una(struct rds_tcp_connection *tc)
103 return tcp_sk(tc->t_sock->sk)->snd_una;
106 void rds_tcp_restore_callbacks(struct socket *sock,
107 struct rds_tcp_connection *tc)
109 rdsdebug("restoring sock %p callbacks from tc %p\n", sock, tc);
110 write_lock_bh(&sock->sk->sk_callback_lock);
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);
118 tc->t_sock = NULL;
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;
125 write_unlock_bh(&sock->sk->sk_callback_lock);
129 * This is the only path that sets tc->t_sock. Send and receive trust that
130 * it is set. The RDS_CONN_CONNECTED bit protects those paths from being
131 * called while it isn't set.
133 void rds_tcp_set_callbacks(struct socket *sock, struct rds_connection *conn)
135 struct rds_tcp_connection *tc = conn->c_transport_data;
137 rdsdebug("setting sock %p callbacks to tc %p\n", sock, tc);
138 write_lock_bh(&sock->sk->sk_callback_lock);
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);
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;
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;
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;
161 write_unlock_bh(&sock->sk->sk_callback_lock);
164 static void rds_tcp_tc_info(struct socket *sock, unsigned int len,
165 struct rds_info_iterator *iter,
166 struct rds_info_lengths *lens)
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;
174 spin_lock_irqsave(&rds_tcp_tc_list_lock, flags);
176 if (len / sizeof(tsinfo) < rds_tcp_tc_count)
177 goto out;
179 list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) {
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;
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;
194 rds_info_copy(iter, &tsinfo, sizeof(tsinfo));
197 out:
198 lens->nr = rds_tcp_tc_count;
199 lens->each = sizeof(tsinfo);
201 spin_unlock_irqrestore(&rds_tcp_tc_list_lock, flags);
204 static int rds_tcp_laddr_check(struct net *net, __be32 addr)
206 if (inet_addr_type(net, addr) == RTN_LOCAL)
207 return 0;
208 return -EADDRNOTAVAIL;
211 static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp)
213 struct rds_tcp_connection *tc;
215 tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp);
216 if (!tc)
217 return -ENOMEM;
219 tc->t_sock = NULL;
220 tc->t_tinc = NULL;
221 tc->t_tinc_hdr_rem = sizeof(struct rds_header);
222 tc->t_tinc_data_rem = 0;
224 conn->c_transport_data = tc;
226 spin_lock_irq(&rds_tcp_conn_lock);
227 list_add_tail(&tc->t_tcp_node, &rds_tcp_conn_list);
228 spin_unlock_irq(&rds_tcp_conn_lock);
230 rdsdebug("alloced tc %p\n", conn->c_transport_data);
231 return 0;
234 static void rds_tcp_conn_free(void *arg)
236 struct rds_tcp_connection *tc = arg;
237 unsigned long flags;
238 rdsdebug("freeing tc %p\n", tc);
240 spin_lock_irqsave(&rds_tcp_conn_lock, flags);
241 list_del(&tc->t_tcp_node);
242 spin_unlock_irqrestore(&rds_tcp_conn_lock, flags);
244 kmem_cache_free(rds_tcp_conn_slab, tc);
247 static void rds_tcp_destroy_conns(void)
249 struct rds_tcp_connection *tc, *_tc;
250 LIST_HEAD(tmp_list);
252 /* avoid calling conn_destroy with irqs off */
253 spin_lock_irq(&rds_tcp_conn_lock);
254 list_splice(&rds_tcp_conn_list, &tmp_list);
255 INIT_LIST_HEAD(&rds_tcp_conn_list);
256 spin_unlock_irq(&rds_tcp_conn_lock);
258 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node) {
259 if (tc->conn->c_passive)
260 rds_conn_destroy(tc->conn->c_passive);
261 rds_conn_destroy(tc->conn);
265 static void rds_tcp_exit(void);
267 struct rds_transport rds_tcp_transport = {
268 .laddr_check = rds_tcp_laddr_check,
269 .xmit_prepare = rds_tcp_xmit_prepare,
270 .xmit_complete = rds_tcp_xmit_complete,
271 .xmit = rds_tcp_xmit,
272 .recv = rds_tcp_recv,
273 .conn_alloc = rds_tcp_conn_alloc,
274 .conn_free = rds_tcp_conn_free,
275 .conn_connect = rds_tcp_conn_connect,
276 .conn_shutdown = rds_tcp_conn_shutdown,
277 .inc_copy_to_user = rds_tcp_inc_copy_to_user,
278 .inc_free = rds_tcp_inc_free,
279 .stats_info_copy = rds_tcp_stats_info_copy,
280 .exit = rds_tcp_exit,
281 .t_owner = THIS_MODULE,
282 .t_name = "tcp",
283 .t_type = RDS_TRANS_TCP,
284 .t_prefer_loopback = 1,
287 static int rds_tcp_netid;
289 /* per-network namespace private data for this module */
290 struct rds_tcp_net {
291 struct socket *rds_tcp_listen_sock;
292 struct work_struct rds_tcp_accept_w;
293 struct ctl_table_header *rds_tcp_sysctl;
294 struct ctl_table *ctl_table;
295 int sndbuf_size;
296 int rcvbuf_size;
299 /* All module specific customizations to the RDS-TCP socket should be done in
300 * rds_tcp_tune() and applied after socket creation.
302 void rds_tcp_tune(struct socket *sock)
304 struct sock *sk = sock->sk;
305 struct net *net = sock_net(sk);
306 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
308 rds_tcp_nonagle(sock);
309 lock_sock(sk);
310 if (rtn->sndbuf_size > 0) {
311 sk->sk_sndbuf = rtn->sndbuf_size;
312 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
314 if (rtn->rcvbuf_size > 0) {
315 sk->sk_sndbuf = rtn->rcvbuf_size;
316 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
318 release_sock(sk);
321 static void rds_tcp_accept_worker(struct work_struct *work)
323 struct rds_tcp_net *rtn = container_of(work,
324 struct rds_tcp_net,
325 rds_tcp_accept_w);
327 while (rds_tcp_accept_one(rtn->rds_tcp_listen_sock) == 0)
328 cond_resched();
331 void rds_tcp_accept_work(struct sock *sk)
333 struct net *net = sock_net(sk);
334 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
336 queue_work(rds_wq, &rtn->rds_tcp_accept_w);
339 static __net_init int rds_tcp_init_net(struct net *net)
341 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
342 struct ctl_table *tbl;
343 int err = 0;
345 memset(rtn, 0, sizeof(*rtn));
347 /* {snd, rcv}buf_size default to 0, which implies we let the
348 * stack pick the value, and permit auto-tuning of buffer size.
350 if (net == &init_net) {
351 tbl = rds_tcp_sysctl_table;
352 } else {
353 tbl = kmemdup(rds_tcp_sysctl_table,
354 sizeof(rds_tcp_sysctl_table), GFP_KERNEL);
355 if (!tbl) {
356 pr_warn("could not set allocate syctl table\n");
357 return -ENOMEM;
359 rtn->ctl_table = tbl;
361 tbl[RDS_TCP_SNDBUF].data = &rtn->sndbuf_size;
362 tbl[RDS_TCP_RCVBUF].data = &rtn->rcvbuf_size;
363 rtn->rds_tcp_sysctl = register_net_sysctl(net, "net/rds/tcp", tbl);
364 if (!rtn->rds_tcp_sysctl) {
365 pr_warn("could not register sysctl\n");
366 err = -ENOMEM;
367 goto fail;
369 rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net);
370 if (!rtn->rds_tcp_listen_sock) {
371 pr_warn("could not set up listen sock\n");
372 unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
373 rtn->rds_tcp_sysctl = NULL;
374 err = -EAFNOSUPPORT;
375 goto fail;
377 INIT_WORK(&rtn->rds_tcp_accept_w, rds_tcp_accept_worker);
378 return 0;
380 fail:
381 if (net != &init_net)
382 kfree(tbl);
383 return err;
386 static void __net_exit rds_tcp_exit_net(struct net *net)
388 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
390 if (rtn->rds_tcp_sysctl)
391 unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
393 if (net != &init_net && rtn->ctl_table)
394 kfree(rtn->ctl_table);
396 /* If rds_tcp_exit_net() is called as a result of netns deletion,
397 * the rds_tcp_kill_sock() device notifier would already have cleaned
398 * up the listen socket, thus there is no work to do in this function.
400 * If rds_tcp_exit_net() is called as a result of module unload,
401 * i.e., due to rds_tcp_exit() -> unregister_pernet_subsys(), then
402 * we do need to clean up the listen socket here.
404 if (rtn->rds_tcp_listen_sock) {
405 rds_tcp_listen_stop(rtn->rds_tcp_listen_sock);
406 rtn->rds_tcp_listen_sock = NULL;
407 flush_work(&rtn->rds_tcp_accept_w);
411 static struct pernet_operations rds_tcp_net_ops = {
412 .init = rds_tcp_init_net,
413 .exit = rds_tcp_exit_net,
414 .id = &rds_tcp_netid,
415 .size = sizeof(struct rds_tcp_net),
418 static void rds_tcp_kill_sock(struct net *net)
420 struct rds_tcp_connection *tc, *_tc;
421 struct sock *sk;
422 LIST_HEAD(tmp_list);
423 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
425 rds_tcp_listen_stop(rtn->rds_tcp_listen_sock);
426 rtn->rds_tcp_listen_sock = NULL;
427 flush_work(&rtn->rds_tcp_accept_w);
428 spin_lock_irq(&rds_tcp_conn_lock);
429 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
430 struct net *c_net = read_pnet(&tc->conn->c_net);
432 if (net != c_net || !tc->t_sock)
433 continue;
434 list_move_tail(&tc->t_tcp_node, &tmp_list);
436 spin_unlock_irq(&rds_tcp_conn_lock);
437 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node) {
438 sk = tc->t_sock->sk;
439 sk->sk_prot->disconnect(sk, 0);
440 tcp_done(sk);
441 if (tc->conn->c_passive)
442 rds_conn_destroy(tc->conn->c_passive);
443 rds_conn_destroy(tc->conn);
447 static int rds_tcp_dev_event(struct notifier_block *this,
448 unsigned long event, void *ptr)
450 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
452 /* rds-tcp registers as a pernet subys, so the ->exit will only
453 * get invoked after network acitivity has quiesced. We need to
454 * clean up all sockets to quiesce network activity, and use
455 * the unregistration of the per-net loopback device as a trigger
456 * to start that cleanup.
458 if (event == NETDEV_UNREGISTER_FINAL &&
459 dev->ifindex == LOOPBACK_IFINDEX)
460 rds_tcp_kill_sock(dev_net(dev));
462 return NOTIFY_DONE;
465 static struct notifier_block rds_tcp_dev_notifier = {
466 .notifier_call = rds_tcp_dev_event,
467 .priority = -10, /* must be called after other network notifiers */
470 /* when sysctl is used to modify some kernel socket parameters,this
471 * function resets the RDS connections in that netns so that we can
472 * restart with new parameters. The assumption is that such reset
473 * events are few and far-between.
475 static void rds_tcp_sysctl_reset(struct net *net)
477 struct rds_tcp_connection *tc, *_tc;
479 spin_lock_irq(&rds_tcp_conn_lock);
480 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
481 struct net *c_net = read_pnet(&tc->conn->c_net);
483 if (net != c_net || !tc->t_sock)
484 continue;
486 rds_conn_drop(tc->conn); /* reconnect with new parameters */
488 spin_unlock_irq(&rds_tcp_conn_lock);
491 static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write,
492 void __user *buffer, size_t *lenp,
493 loff_t *fpos)
495 struct net *net = current->nsproxy->net_ns;
496 int err;
498 err = proc_dointvec_minmax(ctl, write, buffer, lenp, fpos);
499 if (err < 0) {
500 pr_warn("Invalid input. Must be >= %d\n",
501 *(int *)(ctl->extra1));
502 return err;
504 if (write)
505 rds_tcp_sysctl_reset(net);
506 return 0;
509 static void rds_tcp_exit(void)
511 rds_info_deregister_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
512 unregister_pernet_subsys(&rds_tcp_net_ops);
513 if (unregister_netdevice_notifier(&rds_tcp_dev_notifier))
514 pr_warn("could not unregister rds_tcp_dev_notifier\n");
515 rds_tcp_destroy_conns();
516 rds_trans_unregister(&rds_tcp_transport);
517 rds_tcp_recv_exit();
518 kmem_cache_destroy(rds_tcp_conn_slab);
520 module_exit(rds_tcp_exit);
522 static int rds_tcp_init(void)
524 int ret;
526 rds_tcp_conn_slab = kmem_cache_create("rds_tcp_connection",
527 sizeof(struct rds_tcp_connection),
528 0, 0, NULL);
529 if (!rds_tcp_conn_slab) {
530 ret = -ENOMEM;
531 goto out;
534 ret = register_netdevice_notifier(&rds_tcp_dev_notifier);
535 if (ret) {
536 pr_warn("could not register rds_tcp_dev_notifier\n");
537 goto out;
540 ret = register_pernet_subsys(&rds_tcp_net_ops);
541 if (ret)
542 goto out_slab;
544 ret = rds_tcp_recv_init();
545 if (ret)
546 goto out_slab;
548 ret = rds_trans_register(&rds_tcp_transport);
549 if (ret)
550 goto out_recv;
552 rds_info_register_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
554 goto out;
556 out_recv:
557 rds_tcp_recv_exit();
558 out_slab:
559 unregister_pernet_subsys(&rds_tcp_net_ops);
560 kmem_cache_destroy(rds_tcp_conn_slab);
561 out:
562 return ret;
564 module_init(rds_tcp_init);
566 MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
567 MODULE_DESCRIPTION("RDS: TCP transport");
568 MODULE_LICENSE("Dual BSD/GPL");