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