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
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
36 #include <linux/module.h>
38 #include <net/net_namespace.h>
39 #include <net/netns/generic.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,
59 static int rds_tcp_min_sndbuf = SOCK_MIN_SNDBUF;
60 static 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),
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),
78 .proc_handler = rds_tcp_skbuf_handler,
79 .extra1 = &rds_tcp_min_rcvbuf,
84 /* doing it this way avoids calling tcp_sk() */
85 void rds_tcp_nonagle(struct socket *sock)
89 kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (void *)&val,
93 u32 rds_tcp_write_seq(struct rds_tcp_connection *tc)
95 /* seq# of the last byte of data in tcp send buffer */
96 return tcp_sk(tc->t_sock->sk)->write_seq;
99 u32 rds_tcp_snd_una(struct rds_tcp_connection *tc)
101 return tcp_sk(tc->t_sock->sk)->snd_una;
104 void rds_tcp_restore_callbacks(struct socket *sock,
105 struct rds_tcp_connection *tc)
107 rdsdebug("restoring sock %p callbacks from tc %p\n", sock, tc);
108 write_lock_bh(&sock->sk->sk_callback_lock);
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);
114 spin_unlock(&rds_tcp_tc_list_lock);
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;
123 write_unlock_bh(&sock->sk->sk_callback_lock);
127 * rds_tcp_reset_callbacks() switches the to the new sock and
128 * returns the existing tc->t_sock.
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.
135 void rds_tcp_reset_callbacks(struct socket *sock,
136 struct rds_conn_path *cp)
138 struct rds_tcp_connection *tc = cp->cp_transport_data;
139 struct socket *osock = tc->t_sock;
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
148 * cp_transport_data. We quiesce these threads by setting
149 * cp_state to something other than RDS_CONN_UP, and then
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().
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()
164 atomic_set(&cp->cp_state, RDS_CONN_RESETTING);
165 wait_event(cp->cp_waitq, !test_bit(RDS_IN_XMIT, &cp->cp_flags));
166 lock_sock(osock->sk);
167 /* reset receive side state for rds_tcp_data_recv() for osock */
168 cancel_delayed_work_sync(&cp->cp_send_w);
169 cancel_delayed_work_sync(&cp->cp_recv_w);
171 rds_inc_put(&tc->t_tinc->ti_inc);
174 tc->t_tinc_hdr_rem = sizeof(struct rds_header);
175 tc->t_tinc_data_rem = 0;
176 rds_tcp_restore_callbacks(osock, tc);
177 release_sock(osock->sk);
180 rds_send_path_reset(cp);
182 rds_tcp_set_callbacks(sock, cp);
183 release_sock(sock->sk);
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
190 void rds_tcp_set_callbacks(struct socket *sock, struct rds_conn_path *cp)
192 struct rds_tcp_connection *tc = cp->cp_transport_data;
194 rdsdebug("setting sock %p callbacks to tc %p\n", sock, tc);
195 write_lock_bh(&sock->sk->sk_callback_lock);
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);
201 spin_unlock(&rds_tcp_tc_list_lock);
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;
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;
213 sock->sk->sk_user_data = cp;
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;
218 write_unlock_bh(&sock->sk->sk_callback_lock);
221 static void rds_tcp_tc_info(struct socket *rds_sock, unsigned int len,
222 struct rds_info_iterator *iter,
223 struct rds_info_lengths *lens)
225 struct rds_info_tcp_socket tsinfo;
226 struct rds_tcp_connection *tc;
228 struct sockaddr_in sin;
232 spin_lock_irqsave(&rds_tcp_tc_list_lock, flags);
234 if (len / sizeof(tsinfo) < rds_tcp_tc_count)
237 list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) {
241 sock->ops->getname(sock, (struct sockaddr *)&sin,
243 tsinfo.local_addr = sin.sin_addr.s_addr;
244 tsinfo.local_port = sin.sin_port;
245 sock->ops->getname(sock, (struct sockaddr *)&sin,
247 tsinfo.peer_addr = sin.sin_addr.s_addr;
248 tsinfo.peer_port = sin.sin_port;
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;
257 rds_info_copy(iter, &tsinfo, sizeof(tsinfo));
261 lens->nr = rds_tcp_tc_count;
262 lens->each = sizeof(tsinfo);
264 spin_unlock_irqrestore(&rds_tcp_tc_list_lock, flags);
267 static int rds_tcp_laddr_check(struct net *net, __be32 addr)
269 if (inet_addr_type(net, addr) == RTN_LOCAL)
271 return -EADDRNOTAVAIL;
274 static void rds_tcp_conn_free(void *arg)
276 struct rds_tcp_connection *tc = arg;
279 rdsdebug("freeing tc %p\n", tc);
281 spin_lock_irqsave(&rds_tcp_conn_lock, flags);
282 if (!tc->t_tcp_node_detached)
283 list_del(&tc->t_tcp_node);
284 spin_unlock_irqrestore(&rds_tcp_conn_lock, flags);
286 kmem_cache_free(rds_tcp_conn_slab, tc);
289 static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp)
291 struct rds_tcp_connection *tc;
295 for (i = 0; i < RDS_MPATH_WORKERS; i++) {
296 tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp);
301 mutex_init(&tc->t_conn_path_lock);
304 tc->t_tinc_hdr_rem = sizeof(struct rds_header);
305 tc->t_tinc_data_rem = 0;
307 conn->c_path[i].cp_transport_data = tc;
308 tc->t_cpath = &conn->c_path[i];
310 spin_lock_irq(&rds_tcp_conn_lock);
311 tc->t_tcp_node_detached = false;
312 list_add_tail(&tc->t_tcp_node, &rds_tcp_conn_list);
313 spin_unlock_irq(&rds_tcp_conn_lock);
314 rdsdebug("rds_conn_path [%d] tc %p\n", i,
315 conn->c_path[i].cp_transport_data);
318 for (j = 0; j < i; j++)
319 rds_tcp_conn_free(conn->c_path[j].cp_transport_data);
324 static bool list_has_conn(struct list_head *list, struct rds_connection *conn)
326 struct rds_tcp_connection *tc, *_tc;
328 list_for_each_entry_safe(tc, _tc, list, t_tcp_node) {
329 if (tc->t_cpath->cp_conn == conn)
335 static void rds_tcp_destroy_conns(void)
337 struct rds_tcp_connection *tc, *_tc;
340 /* avoid calling conn_destroy with irqs off */
341 spin_lock_irq(&rds_tcp_conn_lock);
342 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
343 if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn))
344 list_move_tail(&tc->t_tcp_node, &tmp_list);
346 spin_unlock_irq(&rds_tcp_conn_lock);
348 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node)
349 rds_conn_destroy(tc->t_cpath->cp_conn);
352 static void rds_tcp_exit(void);
354 struct rds_transport rds_tcp_transport = {
355 .laddr_check = rds_tcp_laddr_check,
356 .xmit_path_prepare = rds_tcp_xmit_path_prepare,
357 .xmit_path_complete = rds_tcp_xmit_path_complete,
358 .xmit = rds_tcp_xmit,
359 .recv_path = rds_tcp_recv_path,
360 .conn_alloc = rds_tcp_conn_alloc,
361 .conn_free = rds_tcp_conn_free,
362 .conn_path_connect = rds_tcp_conn_path_connect,
363 .conn_path_shutdown = rds_tcp_conn_path_shutdown,
364 .inc_copy_to_user = rds_tcp_inc_copy_to_user,
365 .inc_free = rds_tcp_inc_free,
366 .stats_info_copy = rds_tcp_stats_info_copy,
367 .exit = rds_tcp_exit,
368 .t_owner = THIS_MODULE,
370 .t_type = RDS_TRANS_TCP,
371 .t_prefer_loopback = 1,
375 static unsigned int rds_tcp_netid;
377 /* per-network namespace private data for this module */
379 struct socket *rds_tcp_listen_sock;
380 struct work_struct rds_tcp_accept_w;
381 struct ctl_table_header *rds_tcp_sysctl;
382 struct ctl_table *ctl_table;
387 /* All module specific customizations to the RDS-TCP socket should be done in
388 * rds_tcp_tune() and applied after socket creation.
390 void rds_tcp_tune(struct socket *sock)
392 struct sock *sk = sock->sk;
393 struct net *net = sock_net(sk);
394 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
396 rds_tcp_nonagle(sock);
398 if (rtn->sndbuf_size > 0) {
399 sk->sk_sndbuf = rtn->sndbuf_size;
400 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
402 if (rtn->rcvbuf_size > 0) {
403 sk->sk_sndbuf = rtn->rcvbuf_size;
404 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
409 static void rds_tcp_accept_worker(struct work_struct *work)
411 struct rds_tcp_net *rtn = container_of(work,
415 while (rds_tcp_accept_one(rtn->rds_tcp_listen_sock) == 0)
419 void rds_tcp_accept_work(struct sock *sk)
421 struct net *net = sock_net(sk);
422 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
424 queue_work(rds_wq, &rtn->rds_tcp_accept_w);
427 static __net_init int rds_tcp_init_net(struct net *net)
429 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
430 struct ctl_table *tbl;
433 memset(rtn, 0, sizeof(*rtn));
435 /* {snd, rcv}buf_size default to 0, which implies we let the
436 * stack pick the value, and permit auto-tuning of buffer size.
438 if (net == &init_net) {
439 tbl = rds_tcp_sysctl_table;
441 tbl = kmemdup(rds_tcp_sysctl_table,
442 sizeof(rds_tcp_sysctl_table), GFP_KERNEL);
444 pr_warn("could not set allocate syctl table\n");
447 rtn->ctl_table = tbl;
449 tbl[RDS_TCP_SNDBUF].data = &rtn->sndbuf_size;
450 tbl[RDS_TCP_RCVBUF].data = &rtn->rcvbuf_size;
451 rtn->rds_tcp_sysctl = register_net_sysctl(net, "net/rds/tcp", tbl);
452 if (!rtn->rds_tcp_sysctl) {
453 pr_warn("could not register sysctl\n");
457 rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net);
458 if (!rtn->rds_tcp_listen_sock) {
459 pr_warn("could not set up listen sock\n");
460 unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
461 rtn->rds_tcp_sysctl = NULL;
465 INIT_WORK(&rtn->rds_tcp_accept_w, rds_tcp_accept_worker);
469 if (net != &init_net)
474 static void __net_exit rds_tcp_exit_net(struct net *net)
476 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
478 if (rtn->rds_tcp_sysctl)
479 unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
481 if (net != &init_net && rtn->ctl_table)
482 kfree(rtn->ctl_table);
484 /* If rds_tcp_exit_net() is called as a result of netns deletion,
485 * the rds_tcp_kill_sock() device notifier would already have cleaned
486 * up the listen socket, thus there is no work to do in this function.
488 * If rds_tcp_exit_net() is called as a result of module unload,
489 * i.e., due to rds_tcp_exit() -> unregister_pernet_subsys(), then
490 * we do need to clean up the listen socket here.
492 if (rtn->rds_tcp_listen_sock) {
493 struct socket *lsock = rtn->rds_tcp_listen_sock;
495 rtn->rds_tcp_listen_sock = NULL;
496 rds_tcp_listen_stop(lsock, &rtn->rds_tcp_accept_w);
500 static struct pernet_operations rds_tcp_net_ops = {
501 .init = rds_tcp_init_net,
502 .exit = rds_tcp_exit_net,
503 .id = &rds_tcp_netid,
504 .size = sizeof(struct rds_tcp_net),
507 static void rds_tcp_kill_sock(struct net *net)
509 struct rds_tcp_connection *tc, *_tc;
511 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
512 struct socket *lsock = rtn->rds_tcp_listen_sock;
514 rtn->rds_tcp_listen_sock = NULL;
515 rds_tcp_listen_stop(lsock, &rtn->rds_tcp_accept_w);
516 spin_lock_irq(&rds_tcp_conn_lock);
517 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
518 struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net);
520 if (net != c_net || !tc->t_sock)
522 if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn)) {
523 list_move_tail(&tc->t_tcp_node, &tmp_list);
525 list_del(&tc->t_tcp_node);
526 tc->t_tcp_node_detached = true;
529 spin_unlock_irq(&rds_tcp_conn_lock);
530 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node)
531 rds_conn_destroy(tc->t_cpath->cp_conn);
534 void *rds_tcp_listen_sock_def_readable(struct net *net)
536 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
537 struct socket *lsock = rtn->rds_tcp_listen_sock;
542 return lsock->sk->sk_user_data;
545 static int rds_tcp_dev_event(struct notifier_block *this,
546 unsigned long event, void *ptr)
548 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
550 /* rds-tcp registers as a pernet subys, so the ->exit will only
551 * get invoked after network acitivity has quiesced. We need to
552 * clean up all sockets to quiesce network activity, and use
553 * the unregistration of the per-net loopback device as a trigger
554 * to start that cleanup.
556 if (event == NETDEV_UNREGISTER_FINAL &&
557 dev->ifindex == LOOPBACK_IFINDEX)
558 rds_tcp_kill_sock(dev_net(dev));
563 static struct notifier_block rds_tcp_dev_notifier = {
564 .notifier_call = rds_tcp_dev_event,
565 .priority = -10, /* must be called after other network notifiers */
568 /* when sysctl is used to modify some kernel socket parameters,this
569 * function resets the RDS connections in that netns so that we can
570 * restart with new parameters. The assumption is that such reset
571 * events are few and far-between.
573 static void rds_tcp_sysctl_reset(struct net *net)
575 struct rds_tcp_connection *tc, *_tc;
577 spin_lock_irq(&rds_tcp_conn_lock);
578 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
579 struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net);
581 if (net != c_net || !tc->t_sock)
584 /* reconnect with new parameters */
585 rds_conn_path_drop(tc->t_cpath, false);
587 spin_unlock_irq(&rds_tcp_conn_lock);
590 static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write,
591 void __user *buffer, size_t *lenp,
594 struct net *net = current->nsproxy->net_ns;
597 err = proc_dointvec_minmax(ctl, write, buffer, lenp, fpos);
599 pr_warn("Invalid input. Must be >= %d\n",
600 *(int *)(ctl->extra1));
604 rds_tcp_sysctl_reset(net);
608 static void rds_tcp_exit(void)
610 rds_info_deregister_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
611 unregister_pernet_subsys(&rds_tcp_net_ops);
612 if (unregister_netdevice_notifier(&rds_tcp_dev_notifier))
613 pr_warn("could not unregister rds_tcp_dev_notifier\n");
614 rds_tcp_destroy_conns();
615 rds_trans_unregister(&rds_tcp_transport);
617 kmem_cache_destroy(rds_tcp_conn_slab);
619 module_exit(rds_tcp_exit);
621 static int rds_tcp_init(void)
625 rds_tcp_conn_slab = kmem_cache_create("rds_tcp_connection",
626 sizeof(struct rds_tcp_connection),
628 if (!rds_tcp_conn_slab) {
633 ret = rds_tcp_recv_init();
637 ret = register_pernet_subsys(&rds_tcp_net_ops);
641 ret = register_netdevice_notifier(&rds_tcp_dev_notifier);
643 pr_warn("could not register rds_tcp_dev_notifier\n");
647 rds_trans_register(&rds_tcp_transport);
649 rds_info_register_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
654 unregister_pernet_subsys(&rds_tcp_net_ops);
658 kmem_cache_destroy(rds_tcp_conn_slab);
662 module_init(rds_tcp_init);
664 MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
665 MODULE_DESCRIPTION("RDS: TCP transport");
666 MODULE_LICENSE("Dual BSD/GPL");