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>
45 /* only for info exporting */
46 static DEFINE_SPINLOCK(rds_tcp_tc_list_lock
);
47 static LIST_HEAD(rds_tcp_tc_list
);
48 static unsigned int rds_tcp_tc_count
;
50 /* Track rds_tcp_connection structs so they can be cleaned up */
51 static DEFINE_SPINLOCK(rds_tcp_conn_lock
);
52 static LIST_HEAD(rds_tcp_conn_list
);
54 static struct kmem_cache
*rds_tcp_conn_slab
;
56 #define RDS_TCP_DEFAULT_BUFSIZE (128 * 1024)
58 /* doing it this way avoids calling tcp_sk() */
59 void rds_tcp_nonagle(struct socket
*sock
)
61 mm_segment_t oldfs
= get_fs();
65 sock
->ops
->setsockopt(sock
, SOL_TCP
, TCP_NODELAY
, (char __user
*)&val
,
70 /* All module specific customizations to the RDS-TCP socket should be done in
71 * rds_tcp_tune() and applied after socket creation. In general these
72 * customizations should be tunable via module_param()
74 void rds_tcp_tune(struct socket
*sock
)
76 rds_tcp_nonagle(sock
);
79 u32
rds_tcp_snd_nxt(struct rds_tcp_connection
*tc
)
81 return tcp_sk(tc
->t_sock
->sk
)->snd_nxt
;
84 u32
rds_tcp_snd_una(struct rds_tcp_connection
*tc
)
86 return tcp_sk(tc
->t_sock
->sk
)->snd_una
;
89 void rds_tcp_restore_callbacks(struct socket
*sock
,
90 struct rds_tcp_connection
*tc
)
92 rdsdebug("restoring sock %p callbacks from tc %p\n", sock
, tc
);
93 write_lock_bh(&sock
->sk
->sk_callback_lock
);
95 /* done under the callback_lock to serialize with write_space */
96 spin_lock(&rds_tcp_tc_list_lock
);
97 list_del_init(&tc
->t_list_item
);
99 spin_unlock(&rds_tcp_tc_list_lock
);
103 sock
->sk
->sk_write_space
= tc
->t_orig_write_space
;
104 sock
->sk
->sk_data_ready
= tc
->t_orig_data_ready
;
105 sock
->sk
->sk_state_change
= tc
->t_orig_state_change
;
106 sock
->sk
->sk_user_data
= NULL
;
108 write_unlock_bh(&sock
->sk
->sk_callback_lock
);
112 * This is the only path that sets tc->t_sock. Send and receive trust that
113 * it is set. The RDS_CONN_CONNECTED bit protects those paths from being
114 * called while it isn't set.
116 void rds_tcp_set_callbacks(struct socket
*sock
, struct rds_connection
*conn
)
118 struct rds_tcp_connection
*tc
= conn
->c_transport_data
;
120 rdsdebug("setting sock %p callbacks to tc %p\n", sock
, tc
);
121 write_lock_bh(&sock
->sk
->sk_callback_lock
);
123 /* done under the callback_lock to serialize with write_space */
124 spin_lock(&rds_tcp_tc_list_lock
);
125 list_add_tail(&tc
->t_list_item
, &rds_tcp_tc_list
);
127 spin_unlock(&rds_tcp_tc_list_lock
);
129 /* accepted sockets need our listen data ready undone */
130 if (sock
->sk
->sk_data_ready
== rds_tcp_listen_data_ready
)
131 sock
->sk
->sk_data_ready
= sock
->sk
->sk_user_data
;
135 tc
->t_orig_data_ready
= sock
->sk
->sk_data_ready
;
136 tc
->t_orig_write_space
= sock
->sk
->sk_write_space
;
137 tc
->t_orig_state_change
= sock
->sk
->sk_state_change
;
139 sock
->sk
->sk_user_data
= conn
;
140 sock
->sk
->sk_data_ready
= rds_tcp_data_ready
;
141 sock
->sk
->sk_write_space
= rds_tcp_write_space
;
142 sock
->sk
->sk_state_change
= rds_tcp_state_change
;
144 write_unlock_bh(&sock
->sk
->sk_callback_lock
);
147 static void rds_tcp_tc_info(struct socket
*sock
, unsigned int len
,
148 struct rds_info_iterator
*iter
,
149 struct rds_info_lengths
*lens
)
151 struct rds_info_tcp_socket tsinfo
;
152 struct rds_tcp_connection
*tc
;
154 struct sockaddr_in sin
;
157 spin_lock_irqsave(&rds_tcp_tc_list_lock
, flags
);
159 if (len
/ sizeof(tsinfo
) < rds_tcp_tc_count
)
162 list_for_each_entry(tc
, &rds_tcp_tc_list
, t_list_item
) {
164 sock
->ops
->getname(sock
, (struct sockaddr
*)&sin
, &sinlen
, 0);
165 tsinfo
.local_addr
= sin
.sin_addr
.s_addr
;
166 tsinfo
.local_port
= sin
.sin_port
;
167 sock
->ops
->getname(sock
, (struct sockaddr
*)&sin
, &sinlen
, 1);
168 tsinfo
.peer_addr
= sin
.sin_addr
.s_addr
;
169 tsinfo
.peer_port
= sin
.sin_port
;
171 tsinfo
.hdr_rem
= tc
->t_tinc_hdr_rem
;
172 tsinfo
.data_rem
= tc
->t_tinc_data_rem
;
173 tsinfo
.last_sent_nxt
= tc
->t_last_sent_nxt
;
174 tsinfo
.last_expected_una
= tc
->t_last_expected_una
;
175 tsinfo
.last_seen_una
= tc
->t_last_seen_una
;
177 rds_info_copy(iter
, &tsinfo
, sizeof(tsinfo
));
181 lens
->nr
= rds_tcp_tc_count
;
182 lens
->each
= sizeof(tsinfo
);
184 spin_unlock_irqrestore(&rds_tcp_tc_list_lock
, flags
);
187 static int rds_tcp_laddr_check(struct net
*net
, __be32 addr
)
189 if (inet_addr_type(net
, addr
) == RTN_LOCAL
)
191 return -EADDRNOTAVAIL
;
194 static int rds_tcp_conn_alloc(struct rds_connection
*conn
, gfp_t gfp
)
196 struct rds_tcp_connection
*tc
;
198 tc
= kmem_cache_alloc(rds_tcp_conn_slab
, gfp
);
204 tc
->t_tinc_hdr_rem
= sizeof(struct rds_header
);
205 tc
->t_tinc_data_rem
= 0;
207 conn
->c_transport_data
= tc
;
209 spin_lock_irq(&rds_tcp_conn_lock
);
210 list_add_tail(&tc
->t_tcp_node
, &rds_tcp_conn_list
);
211 spin_unlock_irq(&rds_tcp_conn_lock
);
213 rdsdebug("alloced tc %p\n", conn
->c_transport_data
);
217 static void rds_tcp_conn_free(void *arg
)
219 struct rds_tcp_connection
*tc
= arg
;
221 rdsdebug("freeing tc %p\n", tc
);
223 spin_lock_irqsave(&rds_tcp_conn_lock
, flags
);
224 list_del(&tc
->t_tcp_node
);
225 spin_unlock_irqrestore(&rds_tcp_conn_lock
, flags
);
227 kmem_cache_free(rds_tcp_conn_slab
, tc
);
230 static void rds_tcp_destroy_conns(void)
232 struct rds_tcp_connection
*tc
, *_tc
;
235 /* avoid calling conn_destroy with irqs off */
236 spin_lock_irq(&rds_tcp_conn_lock
);
237 list_splice(&rds_tcp_conn_list
, &tmp_list
);
238 INIT_LIST_HEAD(&rds_tcp_conn_list
);
239 spin_unlock_irq(&rds_tcp_conn_lock
);
241 list_for_each_entry_safe(tc
, _tc
, &tmp_list
, t_tcp_node
) {
242 if (tc
->conn
->c_passive
)
243 rds_conn_destroy(tc
->conn
->c_passive
);
244 rds_conn_destroy(tc
->conn
);
248 static void rds_tcp_exit(void);
250 struct rds_transport rds_tcp_transport
= {
251 .laddr_check
= rds_tcp_laddr_check
,
252 .xmit_prepare
= rds_tcp_xmit_prepare
,
253 .xmit_complete
= rds_tcp_xmit_complete
,
254 .xmit
= rds_tcp_xmit
,
255 .recv
= rds_tcp_recv
,
256 .conn_alloc
= rds_tcp_conn_alloc
,
257 .conn_free
= rds_tcp_conn_free
,
258 .conn_connect
= rds_tcp_conn_connect
,
259 .conn_shutdown
= rds_tcp_conn_shutdown
,
260 .inc_copy_to_user
= rds_tcp_inc_copy_to_user
,
261 .inc_free
= rds_tcp_inc_free
,
262 .stats_info_copy
= rds_tcp_stats_info_copy
,
263 .exit
= rds_tcp_exit
,
264 .t_owner
= THIS_MODULE
,
266 .t_type
= RDS_TRANS_TCP
,
267 .t_prefer_loopback
= 1,
270 static int rds_tcp_netid
;
272 /* per-network namespace private data for this module */
274 struct socket
*rds_tcp_listen_sock
;
275 struct work_struct rds_tcp_accept_w
;
278 static void rds_tcp_accept_worker(struct work_struct
*work
)
280 struct rds_tcp_net
*rtn
= container_of(work
,
284 while (rds_tcp_accept_one(rtn
->rds_tcp_listen_sock
) == 0)
288 void rds_tcp_accept_work(struct sock
*sk
)
290 struct net
*net
= sock_net(sk
);
291 struct rds_tcp_net
*rtn
= net_generic(net
, rds_tcp_netid
);
293 queue_work(rds_wq
, &rtn
->rds_tcp_accept_w
);
296 static __net_init
int rds_tcp_init_net(struct net
*net
)
298 struct rds_tcp_net
*rtn
= net_generic(net
, rds_tcp_netid
);
300 rtn
->rds_tcp_listen_sock
= rds_tcp_listen_init(net
);
301 if (!rtn
->rds_tcp_listen_sock
) {
302 pr_warn("could not set up listen sock\n");
303 return -EAFNOSUPPORT
;
305 INIT_WORK(&rtn
->rds_tcp_accept_w
, rds_tcp_accept_worker
);
309 static void __net_exit
rds_tcp_exit_net(struct net
*net
)
311 struct rds_tcp_net
*rtn
= net_generic(net
, rds_tcp_netid
);
313 /* If rds_tcp_exit_net() is called as a result of netns deletion,
314 * the rds_tcp_kill_sock() device notifier would already have cleaned
315 * up the listen socket, thus there is no work to do in this function.
317 * If rds_tcp_exit_net() is called as a result of module unload,
318 * i.e., due to rds_tcp_exit() -> unregister_pernet_subsys(), then
319 * we do need to clean up the listen socket here.
321 if (rtn
->rds_tcp_listen_sock
) {
322 rds_tcp_listen_stop(rtn
->rds_tcp_listen_sock
);
323 rtn
->rds_tcp_listen_sock
= NULL
;
324 flush_work(&rtn
->rds_tcp_accept_w
);
328 static struct pernet_operations rds_tcp_net_ops
= {
329 .init
= rds_tcp_init_net
,
330 .exit
= rds_tcp_exit_net
,
331 .id
= &rds_tcp_netid
,
332 .size
= sizeof(struct rds_tcp_net
),
335 static void rds_tcp_kill_sock(struct net
*net
)
337 struct rds_tcp_connection
*tc
, *_tc
;
340 struct rds_tcp_net
*rtn
= net_generic(net
, rds_tcp_netid
);
342 rds_tcp_listen_stop(rtn
->rds_tcp_listen_sock
);
343 rtn
->rds_tcp_listen_sock
= NULL
;
344 flush_work(&rtn
->rds_tcp_accept_w
);
345 spin_lock_irq(&rds_tcp_conn_lock
);
346 list_for_each_entry_safe(tc
, _tc
, &rds_tcp_conn_list
, t_tcp_node
) {
347 struct net
*c_net
= read_pnet(&tc
->conn
->c_net
);
349 if (net
!= c_net
|| !tc
->t_sock
)
351 list_move_tail(&tc
->t_tcp_node
, &tmp_list
);
353 spin_unlock_irq(&rds_tcp_conn_lock
);
354 list_for_each_entry_safe(tc
, _tc
, &tmp_list
, t_tcp_node
) {
356 sk
->sk_prot
->disconnect(sk
, 0);
358 if (tc
->conn
->c_passive
)
359 rds_conn_destroy(tc
->conn
->c_passive
);
360 rds_conn_destroy(tc
->conn
);
364 static int rds_tcp_dev_event(struct notifier_block
*this,
365 unsigned long event
, void *ptr
)
367 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
369 /* rds-tcp registers as a pernet subys, so the ->exit will only
370 * get invoked after network acitivity has quiesced. We need to
371 * clean up all sockets to quiesce network activity, and use
372 * the unregistration of the per-net loopback device as a trigger
373 * to start that cleanup.
375 if (event
== NETDEV_UNREGISTER_FINAL
&&
376 dev
->ifindex
== LOOPBACK_IFINDEX
)
377 rds_tcp_kill_sock(dev_net(dev
));
382 static struct notifier_block rds_tcp_dev_notifier
= {
383 .notifier_call
= rds_tcp_dev_event
,
384 .priority
= -10, /* must be called after other network notifiers */
387 static void rds_tcp_exit(void)
389 rds_info_deregister_func(RDS_INFO_TCP_SOCKETS
, rds_tcp_tc_info
);
390 unregister_pernet_subsys(&rds_tcp_net_ops
);
391 if (unregister_netdevice_notifier(&rds_tcp_dev_notifier
))
392 pr_warn("could not unregister rds_tcp_dev_notifier\n");
393 rds_tcp_destroy_conns();
394 rds_trans_unregister(&rds_tcp_transport
);
396 kmem_cache_destroy(rds_tcp_conn_slab
);
398 module_exit(rds_tcp_exit
);
400 static int rds_tcp_init(void)
404 rds_tcp_conn_slab
= kmem_cache_create("rds_tcp_connection",
405 sizeof(struct rds_tcp_connection
),
407 if (!rds_tcp_conn_slab
) {
412 ret
= register_netdevice_notifier(&rds_tcp_dev_notifier
);
414 pr_warn("could not register rds_tcp_dev_notifier\n");
418 ret
= register_pernet_subsys(&rds_tcp_net_ops
);
422 ret
= rds_tcp_recv_init();
426 ret
= rds_trans_register(&rds_tcp_transport
);
430 rds_info_register_func(RDS_INFO_TCP_SOCKETS
, rds_tcp_tc_info
);
437 unregister_pernet_subsys(&rds_tcp_net_ops
);
438 kmem_cache_destroy(rds_tcp_conn_slab
);
442 module_init(rds_tcp_init
);
444 MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
445 MODULE_DESCRIPTION("RDS: TCP transport");
446 MODULE_LICENSE("Dual BSD/GPL");