Merge branch 'for-6.2/hyperv' into for-linus
[linux-block.git] / net / rds / tcp.c
CommitLineData
70041088 1/*
1e2b44e7 2 * Copyright (c) 2006, 2018 Oracle and/or its affiliates. All rights reserved.
70041088
AG
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>
eee2fa6a 40#include <net/addrconf.h>
70041088
AG
41
42#include "rds.h"
43#include "tcp.h"
44
45/* only for info exporting */
46static DEFINE_SPINLOCK(rds_tcp_tc_list_lock);
47static LIST_HEAD(rds_tcp_tc_list);
1e2b44e7
KCP
48
49/* rds_tcp_tc_count counts only IPv4 connections.
50 * rds6_tcp_tc_count counts both IPv4 and IPv6 connections.
51 */
ff51bf84 52static unsigned int rds_tcp_tc_count;
e65d4d96 53#if IS_ENABLED(CONFIG_IPV6)
1e2b44e7 54static unsigned int rds6_tcp_tc_count;
e65d4d96 55#endif
70041088
AG
56
57/* Track rds_tcp_connection structs so they can be cleaned up */
58static DEFINE_SPINLOCK(rds_tcp_conn_lock);
59static LIST_HEAD(rds_tcp_conn_list);
ebeeb1ad 60static atomic_t rds_tcp_unloading = ATOMIC_INIT(0);
70041088
AG
61
62static struct kmem_cache *rds_tcp_conn_slab;
63
c6a58ffe 64static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write,
32927393 65 void *buffer, size_t *lenp, loff_t *fpos);
c6a58ffe 66
af73e72d
WY
67static int rds_tcp_min_sndbuf = SOCK_MIN_SNDBUF;
68static int rds_tcp_min_rcvbuf = SOCK_MIN_RCVBUF;
c6a58ffe
SV
69
70static struct ctl_table rds_tcp_sysctl_table[] = {
71#define RDS_TCP_SNDBUF 0
72 {
73 .procname = "rds_tcp_sndbuf",
74 /* data is per-net pointer */
75 .maxlen = sizeof(int),
76 .mode = 0644,
77 .proc_handler = rds_tcp_skbuf_handler,
78 .extra1 = &rds_tcp_min_sndbuf,
79 },
80#define RDS_TCP_RCVBUF 1
81 {
82 .procname = "rds_tcp_rcvbuf",
83 /* data is per-net pointer */
84 .maxlen = sizeof(int),
85 .mode = 0644,
86 .proc_handler = rds_tcp_skbuf_handler,
87 .extra1 = &rds_tcp_min_rcvbuf,
88 },
89 { }
90};
91
b589513e 92u32 rds_tcp_write_seq(struct rds_tcp_connection *tc)
70041088 93{
b589513e
SV
94 /* seq# of the last byte of data in tcp send buffer */
95 return tcp_sk(tc->t_sock->sk)->write_seq;
70041088
AG
96}
97
98u32 rds_tcp_snd_una(struct rds_tcp_connection *tc)
99{
100 return tcp_sk(tc->t_sock->sk)->snd_una;
101}
102
103void rds_tcp_restore_callbacks(struct socket *sock,
104 struct rds_tcp_connection *tc)
105{
106 rdsdebug("restoring sock %p callbacks from tc %p\n", sock, tc);
107 write_lock_bh(&sock->sk->sk_callback_lock);
108
109 /* done under the callback_lock to serialize with write_space */
110 spin_lock(&rds_tcp_tc_list_lock);
111 list_del_init(&tc->t_list_item);
e65d4d96 112#if IS_ENABLED(CONFIG_IPV6)
1e2b44e7 113 rds6_tcp_tc_count--;
e65d4d96 114#endif
1e2b44e7
KCP
115 if (!tc->t_cpath->cp_conn->c_isv6)
116 rds_tcp_tc_count--;
70041088
AG
117 spin_unlock(&rds_tcp_tc_list_lock);
118
119 tc->t_sock = NULL;
120
121 sock->sk->sk_write_space = tc->t_orig_write_space;
122 sock->sk->sk_data_ready = tc->t_orig_data_ready;
123 sock->sk->sk_state_change = tc->t_orig_state_change;
124 sock->sk->sk_user_data = NULL;
125
126 write_unlock_bh(&sock->sk->sk_callback_lock);
127}
128
129/*
335b48d9
SV
130 * rds_tcp_reset_callbacks() switches the to the new sock and
131 * returns the existing tc->t_sock.
132 *
133 * The only functions that set tc->t_sock are rds_tcp_set_callbacks
134 * and rds_tcp_reset_callbacks. Send and receive trust that
135 * it is set. The absence of RDS_CONN_UP bit protects those paths
136 * from being called while it isn't set.
137 */
138void rds_tcp_reset_callbacks(struct socket *sock,
ea3b1ea5 139 struct rds_conn_path *cp)
335b48d9 140{
ea3b1ea5 141 struct rds_tcp_connection *tc = cp->cp_transport_data;
335b48d9
SV
142 struct socket *osock = tc->t_sock;
143
144 if (!osock)
145 goto newsock;
146
147 /* Need to resolve a duelling SYN between peers.
148 * We have an outstanding SYN to this peer, which may
149 * potentially have transitioned to the RDS_CONN_UP state,
150 * so we must quiesce any send threads before resetting
ea3b1ea5
SV
151 * cp_transport_data. We quiesce these threads by setting
152 * cp_state to something other than RDS_CONN_UP, and then
335b48d9
SV
153 * waiting for any existing threads in rds_send_xmit to
154 * complete release_in_xmit(). (Subsequent threads entering
155 * rds_send_xmit() will bail on !rds_conn_up().
9c79440e
SV
156 *
157 * However an incoming syn-ack at this point would end up
158 * marking the conn as RDS_CONN_UP, and would again permit
159 * rds_send_xmi() threads through, so ideally we would
160 * synchronize on RDS_CONN_UP after lock_sock(), but cannot
161 * do that: waiting on !RDS_IN_XMIT after lock_sock() may
162 * end up deadlocking with tcp_sendmsg(), and the RDS_IN_XMIT
163 * would not get set. As a result, we set c_state to
164 * RDS_CONN_RESETTTING, to ensure that rds_tcp_state_change
165 * cannot mark rds_conn_path_up() in the window before lock_sock()
335b48d9 166 */
ea3b1ea5
SV
167 atomic_set(&cp->cp_state, RDS_CONN_RESETTING);
168 wait_event(cp->cp_waitq, !test_bit(RDS_IN_XMIT, &cp->cp_flags));
335b48d9 169 /* reset receive side state for rds_tcp_data_recv() for osock */
ac3615e7
SV
170 cancel_delayed_work_sync(&cp->cp_send_w);
171 cancel_delayed_work_sync(&cp->cp_recv_w);
a91b750f 172 lock_sock(osock->sk);
335b48d9
SV
173 if (tc->t_tinc) {
174 rds_inc_put(&tc->t_tinc->ti_inc);
175 tc->t_tinc = NULL;
176 }
177 tc->t_tinc_hdr_rem = sizeof(struct rds_header);
178 tc->t_tinc_data_rem = 0;
ac3615e7 179 rds_tcp_restore_callbacks(osock, tc);
335b48d9
SV
180 release_sock(osock->sk);
181 sock_release(osock);
182newsock:
ea3b1ea5 183 rds_send_path_reset(cp);
335b48d9 184 lock_sock(sock->sk);
ac3615e7 185 rds_tcp_set_callbacks(sock, cp);
335b48d9
SV
186 release_sock(sock->sk);
187}
188
189/* Add tc to rds_tcp_tc_list and set tc->t_sock. See comments
190 * above rds_tcp_reset_callbacks for notes about synchronization
191 * with data path
70041088 192 */
ea3b1ea5 193void rds_tcp_set_callbacks(struct socket *sock, struct rds_conn_path *cp)
70041088 194{
ea3b1ea5 195 struct rds_tcp_connection *tc = cp->cp_transport_data;
70041088
AG
196
197 rdsdebug("setting sock %p callbacks to tc %p\n", sock, tc);
198 write_lock_bh(&sock->sk->sk_callback_lock);
199
200 /* done under the callback_lock to serialize with write_space */
201 spin_lock(&rds_tcp_tc_list_lock);
202 list_add_tail(&tc->t_list_item, &rds_tcp_tc_list);
e65d4d96 203#if IS_ENABLED(CONFIG_IPV6)
1e2b44e7 204 rds6_tcp_tc_count++;
e65d4d96 205#endif
1e2b44e7
KCP
206 if (!tc->t_cpath->cp_conn->c_isv6)
207 rds_tcp_tc_count++;
70041088
AG
208 spin_unlock(&rds_tcp_tc_list_lock);
209
210 /* accepted sockets need our listen data ready undone */
211 if (sock->sk->sk_data_ready == rds_tcp_listen_data_ready)
212 sock->sk->sk_data_ready = sock->sk->sk_user_data;
213
214 tc->t_sock = sock;
ea3b1ea5 215 tc->t_cpath = cp;
70041088
AG
216 tc->t_orig_data_ready = sock->sk->sk_data_ready;
217 tc->t_orig_write_space = sock->sk->sk_write_space;
218 tc->t_orig_state_change = sock->sk->sk_state_change;
219
ea3b1ea5 220 sock->sk->sk_user_data = cp;
70041088
AG
221 sock->sk->sk_data_ready = rds_tcp_data_ready;
222 sock->sk->sk_write_space = rds_tcp_write_space;
223 sock->sk->sk_state_change = rds_tcp_state_change;
224
225 write_unlock_bh(&sock->sk->sk_callback_lock);
226}
227
1e2b44e7
KCP
228/* Handle RDS_INFO_TCP_SOCKETS socket option. It only returns IPv4
229 * connections for backward compatibility.
230 */
1ac507d4 231static void rds_tcp_tc_info(struct socket *rds_sock, unsigned int len,
70041088
AG
232 struct rds_info_iterator *iter,
233 struct rds_info_lengths *lens)
234{
235 struct rds_info_tcp_socket tsinfo;
236 struct rds_tcp_connection *tc;
237 unsigned long flags;
70041088
AG
238
239 spin_lock_irqsave(&rds_tcp_tc_list_lock, flags);
240
241 if (len / sizeof(tsinfo) < rds_tcp_tc_count)
242 goto out;
243
244 list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) {
1e2b44e7 245 struct inet_sock *inet = inet_sk(tc->t_sock->sk);
70041088 246
1e2b44e7
KCP
247 if (tc->t_cpath->cp_conn->c_isv6)
248 continue;
249
250 tsinfo.local_addr = inet->inet_saddr;
251 tsinfo.local_port = inet->inet_sport;
252 tsinfo.peer_addr = inet->inet_daddr;
253 tsinfo.peer_port = inet->inet_dport;
70041088
AG
254
255 tsinfo.hdr_rem = tc->t_tinc_hdr_rem;
256 tsinfo.data_rem = tc->t_tinc_data_rem;
257 tsinfo.last_sent_nxt = tc->t_last_sent_nxt;
258 tsinfo.last_expected_una = tc->t_last_expected_una;
259 tsinfo.last_seen_una = tc->t_last_seen_una;
3eb45036 260 tsinfo.tos = tc->t_cpath->cp_conn->c_tos;
70041088
AG
261
262 rds_info_copy(iter, &tsinfo, sizeof(tsinfo));
263 }
264
265out:
266 lens->nr = rds_tcp_tc_count;
267 lens->each = sizeof(tsinfo);
268
269 spin_unlock_irqrestore(&rds_tcp_tc_list_lock, flags);
270}
271
e65d4d96 272#if IS_ENABLED(CONFIG_IPV6)
b7ff8b10
KCP
273/* Handle RDS6_INFO_TCP_SOCKETS socket option. It returns both IPv4 and
274 * IPv6 connections. IPv4 connection address is returned in an IPv4 mapped
275 * address.
276 */
277static void rds6_tcp_tc_info(struct socket *sock, unsigned int len,
278 struct rds_info_iterator *iter,
279 struct rds_info_lengths *lens)
280{
281 struct rds6_info_tcp_socket tsinfo6;
282 struct rds_tcp_connection *tc;
283 unsigned long flags;
284
285 spin_lock_irqsave(&rds_tcp_tc_list_lock, flags);
286
287 if (len / sizeof(tsinfo6) < rds6_tcp_tc_count)
288 goto out;
289
290 list_for_each_entry(tc, &rds_tcp_tc_list, t_list_item) {
291 struct sock *sk = tc->t_sock->sk;
292 struct inet_sock *inet = inet_sk(sk);
293
294 tsinfo6.local_addr = sk->sk_v6_rcv_saddr;
295 tsinfo6.local_port = inet->inet_sport;
296 tsinfo6.peer_addr = sk->sk_v6_daddr;
297 tsinfo6.peer_port = inet->inet_dport;
298
299 tsinfo6.hdr_rem = tc->t_tinc_hdr_rem;
300 tsinfo6.data_rem = tc->t_tinc_data_rem;
301 tsinfo6.last_sent_nxt = tc->t_last_sent_nxt;
302 tsinfo6.last_expected_una = tc->t_last_expected_una;
303 tsinfo6.last_seen_una = tc->t_last_seen_una;
304
305 rds_info_copy(iter, &tsinfo6, sizeof(tsinfo6));
306 }
307
308out:
309 lens->nr = rds6_tcp_tc_count;
310 lens->each = sizeof(tsinfo6);
311
312 spin_unlock_irqrestore(&rds_tcp_tc_list_lock, flags);
313}
e65d4d96 314#endif
b7ff8b10 315
aced3ce5
RS
316int rds_tcp_laddr_check(struct net *net, const struct in6_addr *addr,
317 __u32 scope_id)
70041088 318{
eee2fa6a 319 struct net_device *dev = NULL;
e65d4d96 320#if IS_ENABLED(CONFIG_IPV6)
eee2fa6a 321 int ret;
e65d4d96 322#endif
eee2fa6a
KCP
323
324 if (ipv6_addr_v4mapped(addr)) {
325 if (inet_addr_type(net, addr->s6_addr32[3]) == RTN_LOCAL)
326 return 0;
327 return -EADDRNOTAVAIL;
328 }
329
330 /* If the scope_id is specified, check only those addresses
331 * hosted on the specified interface.
332 */
333 if (scope_id != 0) {
334 rcu_read_lock();
335 dev = dev_get_by_index_rcu(net, scope_id);
336 /* scope_id is not valid... */
337 if (!dev) {
338 rcu_read_unlock();
339 return -EADDRNOTAVAIL;
340 }
341 rcu_read_unlock();
342 }
e65d4d96 343#if IS_ENABLED(CONFIG_IPV6)
eee2fa6a
KCP
344 ret = ipv6_chk_addr(net, addr, dev, 0);
345 if (ret)
70041088 346 return 0;
e65d4d96 347#endif
70041088
AG
348 return -EADDRNOTAVAIL;
349}
350
66261da1
SV
351static void rds_tcp_conn_free(void *arg)
352{
353 struct rds_tcp_connection *tc = arg;
53d0e83f 354 unsigned long flags;
66261da1
SV
355
356 rdsdebug("freeing tc %p\n", tc);
357
53d0e83f 358 spin_lock_irqsave(&rds_tcp_conn_lock, flags);
66261da1
SV
359 if (!tc->t_tcp_node_detached)
360 list_del(&tc->t_tcp_node);
53d0e83f 361 spin_unlock_irqrestore(&rds_tcp_conn_lock, flags);
66261da1
SV
362
363 kmem_cache_free(rds_tcp_conn_slab, tc);
364}
365
70041088
AG
366static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp)
367{
368 struct rds_tcp_connection *tc;
66261da1
SV
369 int i, j;
370 int ret = 0;
70041088 371
02105b2c
SV
372 for (i = 0; i < RDS_MPATH_WORKERS; i++) {
373 tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp);
66261da1
SV
374 if (!tc) {
375 ret = -ENOMEM;
ebeeb1ad 376 goto fail;
66261da1 377 }
02105b2c
SV
378 mutex_init(&tc->t_conn_path_lock);
379 tc->t_sock = NULL;
380 tc->t_tinc = NULL;
381 tc->t_tinc_hdr_rem = sizeof(struct rds_header);
382 tc->t_tinc_data_rem = 0;
70041088 383
02105b2c
SV
384 conn->c_path[i].cp_transport_data = tc;
385 tc->t_cpath = &conn->c_path[i];
ebeeb1ad 386 tc->t_tcp_node_detached = true;
70041088 387
02105b2c
SV
388 rdsdebug("rds_conn_path [%d] tc %p\n", i,
389 conn->c_path[i].cp_transport_data);
390 }
53d0e83f 391 spin_lock_irq(&rds_tcp_conn_lock);
ebeeb1ad
SV
392 for (i = 0; i < RDS_MPATH_WORKERS; i++) {
393 tc = conn->c_path[i].cp_transport_data;
394 tc->t_tcp_node_detached = false;
395 list_add_tail(&tc->t_tcp_node, &rds_tcp_conn_list);
396 }
53d0e83f 397 spin_unlock_irq(&rds_tcp_conn_lock);
ebeeb1ad 398fail:
66261da1
SV
399 if (ret) {
400 for (j = 0; j < i; j++)
401 rds_tcp_conn_free(conn->c_path[j].cp_transport_data);
402 }
403 return ret;
70041088
AG
404}
405
afb4164d
SV
406static bool list_has_conn(struct list_head *list, struct rds_connection *conn)
407{
408 struct rds_tcp_connection *tc, *_tc;
409
410 list_for_each_entry_safe(tc, _tc, list, t_tcp_node) {
411 if (tc->t_cpath->cp_conn == conn)
412 return true;
413 }
414 return false;
415}
416
ebeeb1ad
SV
417static void rds_tcp_set_unloading(void)
418{
419 atomic_set(&rds_tcp_unloading, 1);
420}
421
422static bool rds_tcp_is_unloading(struct rds_connection *conn)
423{
424 return atomic_read(&rds_tcp_unloading) != 0;
425}
426
70041088
AG
427static void rds_tcp_destroy_conns(void)
428{
429 struct rds_tcp_connection *tc, *_tc;
430 LIST_HEAD(tmp_list);
431
432 /* avoid calling conn_destroy with irqs off */
433 spin_lock_irq(&rds_tcp_conn_lock);
afb4164d
SV
434 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
435 if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn))
436 list_move_tail(&tc->t_tcp_node, &tmp_list);
437 }
70041088
AG
438 spin_unlock_irq(&rds_tcp_conn_lock);
439
26e4e6bb 440 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node)
02105b2c 441 rds_conn_destroy(tc->t_cpath->cp_conn);
70041088
AG
442}
443
467fa153 444static void rds_tcp_exit(void);
70041088 445
56dc8bce
SS
446static u8 rds_tcp_get_tos_map(u8 tos)
447{
448 /* all user tos mapped to default 0 for TCP transport */
449 return 0;
450}
451
70041088
AG
452struct rds_transport rds_tcp_transport = {
453 .laddr_check = rds_tcp_laddr_check,
226f7a7d
SV
454 .xmit_path_prepare = rds_tcp_xmit_path_prepare,
455 .xmit_path_complete = rds_tcp_xmit_path_complete,
70041088 456 .xmit = rds_tcp_xmit,
2da43c4a 457 .recv_path = rds_tcp_recv_path,
70041088
AG
458 .conn_alloc = rds_tcp_conn_alloc,
459 .conn_free = rds_tcp_conn_free,
b04e8554 460 .conn_path_connect = rds_tcp_conn_path_connect,
226f7a7d 461 .conn_path_shutdown = rds_tcp_conn_path_shutdown,
70041088 462 .inc_copy_to_user = rds_tcp_inc_copy_to_user,
70041088
AG
463 .inc_free = rds_tcp_inc_free,
464 .stats_info_copy = rds_tcp_stats_info_copy,
465 .exit = rds_tcp_exit,
56dc8bce 466 .get_tos_map = rds_tcp_get_tos_map,
70041088
AG
467 .t_owner = THIS_MODULE,
468 .t_name = "tcp",
335776bd 469 .t_type = RDS_TRANS_TCP,
70041088 470 .t_prefer_loopback = 1,
5916e2c1 471 .t_mp_capable = 1,
ebeeb1ad 472 .t_unloading = rds_tcp_is_unloading,
70041088
AG
473};
474
c7d03a00 475static unsigned int rds_tcp_netid;
467fa153
SV
476
477/* per-network namespace private data for this module */
478struct rds_tcp_net {
479 struct socket *rds_tcp_listen_sock;
480 struct work_struct rds_tcp_accept_w;
c6a58ffe
SV
481 struct ctl_table_header *rds_tcp_sysctl;
482 struct ctl_table *ctl_table;
483 int sndbuf_size;
484 int rcvbuf_size;
467fa153
SV
485};
486
c6a58ffe
SV
487/* All module specific customizations to the RDS-TCP socket should be done in
488 * rds_tcp_tune() and applied after socket creation.
489 */
6997fbd7 490bool rds_tcp_tune(struct socket *sock)
c6a58ffe
SV
491{
492 struct sock *sk = sock->sk;
493 struct net *net = sock_net(sk);
6997fbd7 494 struct rds_tcp_net *rtn;
c6a58ffe 495
12abc5ee 496 tcp_sock_set_nodelay(sock->sk);
c6a58ffe 497 lock_sock(sk);
3a58f13a
TH
498 /* TCP timer functions might access net namespace even after
499 * a process which created this net namespace terminated.
500 */
501 if (!sk->sk_net_refcnt) {
6997fbd7
TH
502 if (!maybe_get_net(net)) {
503 release_sock(sk);
504 return false;
505 }
3a58f13a 506 sk->sk_net_refcnt = 1;
6997fbd7 507 netns_tracker_alloc(net, &sk->ns_tracker, GFP_KERNEL);
3a58f13a
TH
508 sock_inuse_add(net, 1);
509 }
6997fbd7 510 rtn = net_generic(net, rds_tcp_netid);
c6a58ffe
SV
511 if (rtn->sndbuf_size > 0) {
512 sk->sk_sndbuf = rtn->sndbuf_size;
513 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
514 }
515 if (rtn->rcvbuf_size > 0) {
19f36edf 516 sk->sk_rcvbuf = rtn->rcvbuf_size;
c6a58ffe
SV
517 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
518 }
519 release_sock(sk);
6997fbd7 520 return true;
c6a58ffe
SV
521}
522
467fa153
SV
523static void rds_tcp_accept_worker(struct work_struct *work)
524{
525 struct rds_tcp_net *rtn = container_of(work,
526 struct rds_tcp_net,
527 rds_tcp_accept_w);
528
529 while (rds_tcp_accept_one(rtn->rds_tcp_listen_sock) == 0)
530 cond_resched();
531}
532
533void rds_tcp_accept_work(struct sock *sk)
534{
535 struct net *net = sock_net(sk);
536 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
537
538 queue_work(rds_wq, &rtn->rds_tcp_accept_w);
539}
540
541static __net_init int rds_tcp_init_net(struct net *net)
542{
543 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
c6a58ffe
SV
544 struct ctl_table *tbl;
545 int err = 0;
467fa153 546
c6a58ffe
SV
547 memset(rtn, 0, sizeof(*rtn));
548
549 /* {snd, rcv}buf_size default to 0, which implies we let the
550 * stack pick the value, and permit auto-tuning of buffer size.
551 */
552 if (net == &init_net) {
553 tbl = rds_tcp_sysctl_table;
554 } else {
555 tbl = kmemdup(rds_tcp_sysctl_table,
556 sizeof(rds_tcp_sysctl_table), GFP_KERNEL);
557 if (!tbl) {
d14a108d 558 pr_warn("could not set allocate sysctl table\n");
c6a58ffe
SV
559 return -ENOMEM;
560 }
561 rtn->ctl_table = tbl;
562 }
563 tbl[RDS_TCP_SNDBUF].data = &rtn->sndbuf_size;
564 tbl[RDS_TCP_RCVBUF].data = &rtn->rcvbuf_size;
565 rtn->rds_tcp_sysctl = register_net_sysctl(net, "net/rds/tcp", tbl);
566 if (!rtn->rds_tcp_sysctl) {
567 pr_warn("could not register sysctl\n");
568 err = -ENOMEM;
569 goto fail;
570 }
e65d4d96
KCP
571
572#if IS_ENABLED(CONFIG_IPV6)
1e2b44e7 573 rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net, true);
e65d4d96
KCP
574#else
575 rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net, false);
576#endif
467fa153 577 if (!rtn->rds_tcp_listen_sock) {
1e2b44e7
KCP
578 pr_warn("could not set up IPv6 listen sock\n");
579
e65d4d96 580#if IS_ENABLED(CONFIG_IPV6)
1e2b44e7
KCP
581 /* Try IPv4 as some systems disable IPv6 */
582 rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net, false);
583 if (!rtn->rds_tcp_listen_sock) {
e65d4d96 584#endif
1e2b44e7
KCP
585 unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
586 rtn->rds_tcp_sysctl = NULL;
587 err = -EAFNOSUPPORT;
588 goto fail;
e65d4d96 589#if IS_ENABLED(CONFIG_IPV6)
1e2b44e7 590 }
e65d4d96 591#endif
467fa153
SV
592 }
593 INIT_WORK(&rtn->rds_tcp_accept_w, rds_tcp_accept_worker);
594 return 0;
c6a58ffe
SV
595
596fail:
597 if (net != &init_net)
598 kfree(tbl);
599 return err;
467fa153
SV
600}
601
467fa153
SV
602static void rds_tcp_kill_sock(struct net *net)
603{
604 struct rds_tcp_connection *tc, *_tc;
467fa153
SV
605 LIST_HEAD(tmp_list);
606 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
b21dd450 607 struct socket *lsock = rtn->rds_tcp_listen_sock;
467fa153 608
467fa153 609 rtn->rds_tcp_listen_sock = NULL;
b21dd450 610 rds_tcp_listen_stop(lsock, &rtn->rds_tcp_accept_w);
53d0e83f 611 spin_lock_irq(&rds_tcp_conn_lock);
467fa153 612 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
681648e6 613 struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net);
467fa153 614
cb66ddd1 615 if (net != c_net)
467fa153 616 continue;
f10b4cff 617 if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn)) {
afb4164d 618 list_move_tail(&tc->t_tcp_node, &tmp_list);
f10b4cff
SV
619 } else {
620 list_del(&tc->t_tcp_node);
621 tc->t_tcp_node_detached = true;
622 }
467fa153 623 }
53d0e83f 624 spin_unlock_irq(&rds_tcp_conn_lock);
2d746c93 625 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node)
02105b2c 626 rds_conn_destroy(tc->t_cpath->cp_conn);
467fa153
SV
627}
628
bdf5bd7f 629static void __net_exit rds_tcp_exit_net(struct net *net)
a93d01f5
SV
630{
631 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
b21dd450 632
bdf5bd7f 633 rds_tcp_kill_sock(net);
a93d01f5 634
bdf5bd7f
SV
635 if (rtn->rds_tcp_sysctl)
636 unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
637
4087d2bc 638 if (net != &init_net)
bdf5bd7f 639 kfree(rtn->ctl_table);
a93d01f5
SV
640}
641
bdf5bd7f
SV
642static struct pernet_operations rds_tcp_net_ops = {
643 .init = rds_tcp_init_net,
644 .exit = rds_tcp_exit_net,
645 .id = &rds_tcp_netid,
646 .size = sizeof(struct rds_tcp_net),
bdf5bd7f
SV
647};
648
649void *rds_tcp_listen_sock_def_readable(struct net *net)
467fa153 650{
bdf5bd7f
SV
651 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
652 struct socket *lsock = rtn->rds_tcp_listen_sock;
467fa153 653
bdf5bd7f
SV
654 if (!lsock)
655 return NULL;
467fa153 656
bdf5bd7f 657 return lsock->sk->sk_user_data;
467fa153
SV
658}
659
c6a58ffe
SV
660/* when sysctl is used to modify some kernel socket parameters,this
661 * function resets the RDS connections in that netns so that we can
662 * restart with new parameters. The assumption is that such reset
663 * events are few and far-between.
664 */
665static void rds_tcp_sysctl_reset(struct net *net)
666{
667 struct rds_tcp_connection *tc, *_tc;
668
53d0e83f 669 spin_lock_irq(&rds_tcp_conn_lock);
c6a58ffe 670 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
681648e6 671 struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net);
c6a58ffe
SV
672
673 if (net != c_net || !tc->t_sock)
674 continue;
675
02105b2c 676 /* reconnect with new parameters */
aed20a53 677 rds_conn_path_drop(tc->t_cpath, false);
c6a58ffe 678 }
53d0e83f 679 spin_unlock_irq(&rds_tcp_conn_lock);
c6a58ffe
SV
680}
681
682static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write,
32927393 683 void *buffer, size_t *lenp, loff_t *fpos)
c6a58ffe
SV
684{
685 struct net *net = current->nsproxy->net_ns;
686 int err;
687
688 err = proc_dointvec_minmax(ctl, write, buffer, lenp, fpos);
689 if (err < 0) {
690 pr_warn("Invalid input. Must be >= %d\n",
691 *(int *)(ctl->extra1));
692 return err;
693 }
694 if (write)
695 rds_tcp_sysctl_reset(net);
696 return 0;
697}
698
467fa153
SV
699static void rds_tcp_exit(void)
700{
ebeeb1ad
SV
701 rds_tcp_set_unloading();
702 synchronize_rcu();
467fa153 703 rds_info_deregister_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
e65d4d96 704#if IS_ENABLED(CONFIG_IPV6)
b7ff8b10 705 rds_info_deregister_func(RDS6_INFO_TCP_SOCKETS, rds6_tcp_tc_info);
e65d4d96 706#endif
bdf5bd7f 707 unregister_pernet_device(&rds_tcp_net_ops);
467fa153
SV
708 rds_tcp_destroy_conns();
709 rds_trans_unregister(&rds_tcp_transport);
710 rds_tcp_recv_exit();
711 kmem_cache_destroy(rds_tcp_conn_slab);
712}
713module_exit(rds_tcp_exit);
714
f0bd32c8 715static int __init rds_tcp_init(void)
70041088
AG
716{
717 int ret;
718
719 rds_tcp_conn_slab = kmem_cache_create("rds_tcp_connection",
720 sizeof(struct rds_tcp_connection),
721 0, 0, NULL);
8690bfa1 722 if (!rds_tcp_conn_slab) {
70041088
AG
723 ret = -ENOMEM;
724 goto out;
725 }
726
16c09b1c
SV
727 ret = rds_tcp_recv_init();
728 if (ret)
3b5923f0 729 goto out_slab;
467fa153 730
bdf5bd7f 731 ret = register_pernet_device(&rds_tcp_net_ops);
467fa153 732 if (ret)
16c09b1c 733 goto out_recv;
467fa153 734
a8d63a53 735 rds_trans_register(&rds_tcp_transport);
70041088 736
70041088 737 rds_info_register_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
e65d4d96 738#if IS_ENABLED(CONFIG_IPV6)
b7ff8b10 739 rds_info_register_func(RDS6_INFO_TCP_SOCKETS, rds6_tcp_tc_info);
e65d4d96 740#endif
70041088
AG
741
742 goto out;
16c09b1c
SV
743out_recv:
744 rds_tcp_recv_exit();
3b5923f0 745out_slab:
70041088
AG
746 kmem_cache_destroy(rds_tcp_conn_slab);
747out:
748 return ret;
749}
750module_init(rds_tcp_init);
751
752MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
753MODULE_DESCRIPTION("RDS: TCP transport");
754MODULE_LICENSE("Dual BSD/GPL");