blob: 5327d130c4b5691e788bbbcb990a349d714ad8d4 [file] [log] [blame]
Andy Grover70041082009-08-21 12:28:31 +00001/*
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -07002 * Copyright (c) 2006, 2018 Oracle and/or its affiliates. All rights reserved.
Andy Grover70041082009-08-21 12:28:31 +00003 *
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Andy Grover70041082009-08-21 12:28:31 +000035#include <linux/in.h>
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040036#include <linux/module.h>
Andy Grover70041082009-08-21 12:28:31 +000037#include <net/tcp.h>
Sowmini Varadhan467fa152015-08-05 01:43:26 -040038#include <net/net_namespace.h>
39#include <net/netns/generic.h>
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -070040#include <net/addrconf.h>
Andy Grover70041082009-08-21 12:28:31 +000041
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);
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -070048
49/* rds_tcp_tc_count counts only IPv4 connections.
50 * rds6_tcp_tc_count counts both IPv4 and IPv6 connections.
51 */
stephen hemmingerff51bf82010-10-19 08:08:33 +000052static unsigned int rds_tcp_tc_count;
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -070053#if IS_ENABLED(CONFIG_IPV6)
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -070054static unsigned int rds6_tcp_tc_count;
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -070055#endif
Andy Grover70041082009-08-21 12:28:31 +000056
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);
Sowmini Varadhanebeeb1a2018-02-03 04:26:51 -080060static atomic_t rds_tcp_unloading = ATOMIC_INIT(0);
Andy Grover70041082009-08-21 12:28:31 +000061
62static struct kmem_cache *rds_tcp_conn_slab;
63
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -070064static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +020065 void *buffer, size_t *lenp, loff_t *fpos);
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -070066
Wei Yongjunaf73e722016-06-17 18:12:46 +000067static int rds_tcp_min_sndbuf = SOCK_MIN_SNDBUF;
68static int rds_tcp_min_rcvbuf = SOCK_MIN_RCVBUF;
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -070069
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
Sowmini Varadhanb5895132018-01-18 13:11:07 -080092u32 rds_tcp_write_seq(struct rds_tcp_connection *tc)
Andy Grover70041082009-08-21 12:28:31 +000093{
Sowmini Varadhanb5895132018-01-18 13:11:07 -080094 /* seq# of the last byte of data in tcp send buffer */
95 return tcp_sk(tc->t_sock->sk)->write_seq;
Andy Grover70041082009-08-21 12:28:31 +000096}
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);
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700112#if IS_ENABLED(CONFIG_IPV6)
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -0700113 rds6_tcp_tc_count--;
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700114#endif
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -0700115 if (!tc->t_cpath->cp_conn->c_isv6)
116 rds_tcp_tc_count--;
Andy Grover70041082009-08-21 12:28:31 +0000117 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/*
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700130 * 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,
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700139 struct rds_conn_path *cp)
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700140{
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700141 struct rds_tcp_connection *tc = cp->cp_transport_data;
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700142 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
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700151 * cp_transport_data. We quiesce these threads by setting
152 * cp_state to something other than RDS_CONN_UP, and then
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700153 * 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().
Sowmini Varadhan9c794402016-06-04 14:00:00 -0700156 *
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()
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700166 */
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700167 atomic_set(&cp->cp_state, RDS_CONN_RESETTING);
168 wait_event(cp->cp_waitq, !test_bit(RDS_IN_XMIT, &cp->cp_flags));
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700169 lock_sock(osock->sk);
170 /* reset receive side state for rds_tcp_data_recv() for osock */
Sowmini Varadhanac3615e2016-07-14 03:51:02 -0700171 cancel_delayed_work_sync(&cp->cp_send_w);
172 cancel_delayed_work_sync(&cp->cp_recv_w);
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700173 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;
Sowmini Varadhanac3615e2016-07-14 03:51:02 -0700179 rds_tcp_restore_callbacks(osock, tc);
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700180 release_sock(osock->sk);
181 sock_release(osock);
182newsock:
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700183 rds_send_path_reset(cp);
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700184 lock_sock(sock->sk);
Sowmini Varadhanac3615e2016-07-14 03:51:02 -0700185 rds_tcp_set_callbacks(sock, cp);
Sowmini Varadhan335b48d2016-06-04 13:59:58 -0700186 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
Andy Grover70041082009-08-21 12:28:31 +0000192 */
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700193void rds_tcp_set_callbacks(struct socket *sock, struct rds_conn_path *cp)
Andy Grover70041082009-08-21 12:28:31 +0000194{
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700195 struct rds_tcp_connection *tc = cp->cp_transport_data;
Andy Grover70041082009-08-21 12:28:31 +0000196
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);
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700203#if IS_ENABLED(CONFIG_IPV6)
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -0700204 rds6_tcp_tc_count++;
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700205#endif
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -0700206 if (!tc->t_cpath->cp_conn->c_isv6)
207 rds_tcp_tc_count++;
Andy Grover70041082009-08-21 12:28:31 +0000208 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;
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700215 tc->t_cpath = cp;
Andy Grover70041082009-08-21 12:28:31 +0000216 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
Sowmini Varadhanea3b1ea2016-06-30 16:11:14 -0700220 sock->sk->sk_user_data = cp;
Andy Grover70041082009-08-21 12:28:31 +0000221 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
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -0700228/* Handle RDS_INFO_TCP_SOCKETS socket option. It only returns IPv4
229 * connections for backward compatibility.
230 */
Sowmini Varadhan1ac507d2016-11-04 10:04:11 -0700231static void rds_tcp_tc_info(struct socket *rds_sock, unsigned int len,
Andy Grover70041082009-08-21 12:28:31 +0000232 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;
Andy Grover70041082009-08-21 12:28:31 +0000238
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) {
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -0700245 struct inet_sock *inet = inet_sk(tc->t_sock->sk);
Andy Grover70041082009-08-21 12:28:31 +0000246
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -0700247 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;
Andy Grover70041082009-08-21 12:28:31 +0000254
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;
Santosh Shilimkar3eb45032018-10-23 23:21:14 -0400260 tsinfo.tos = tc->t_cpath->cp_conn->c_tos;
Andy Grover70041082009-08-21 12:28:31 +0000261
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
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700272#if IS_ENABLED(CONFIG_IPV6)
Ka-Cheong Poonb7ff8b12018-07-23 20:51:23 -0700273/* 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}
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700314#endif
Ka-Cheong Poonb7ff8b12018-07-23 20:51:23 -0700315
Rao Shoaibaced3ce52021-05-21 11:08:06 -0700316int rds_tcp_laddr_check(struct net *net, const struct in6_addr *addr,
317 __u32 scope_id)
Andy Grover70041082009-08-21 12:28:31 +0000318{
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700319 struct net_device *dev = NULL;
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700320#if IS_ENABLED(CONFIG_IPV6)
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700321 int ret;
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700322#endif
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700323
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 }
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700343#if IS_ENABLED(CONFIG_IPV6)
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -0700344 ret = ipv6_chk_addr(net, addr, dev, 0);
345 if (ret)
Andy Grover70041082009-08-21 12:28:31 +0000346 return 0;
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700347#endif
Andy Grover70041082009-08-21 12:28:31 +0000348 return -EADDRNOTAVAIL;
349}
350
Sowmini Varadhan66261da2017-12-22 09:39:01 -0800351static void rds_tcp_conn_free(void *arg)
352{
353 struct rds_tcp_connection *tc = arg;
Sowmini Varadhan53d0e832018-03-15 03:54:26 -0700354 unsigned long flags;
Sowmini Varadhan66261da2017-12-22 09:39:01 -0800355
356 rdsdebug("freeing tc %p\n", tc);
357
Sowmini Varadhan53d0e832018-03-15 03:54:26 -0700358 spin_lock_irqsave(&rds_tcp_conn_lock, flags);
Sowmini Varadhan66261da2017-12-22 09:39:01 -0800359 if (!tc->t_tcp_node_detached)
360 list_del(&tc->t_tcp_node);
Sowmini Varadhan53d0e832018-03-15 03:54:26 -0700361 spin_unlock_irqrestore(&rds_tcp_conn_lock, flags);
Sowmini Varadhan66261da2017-12-22 09:39:01 -0800362
363 kmem_cache_free(rds_tcp_conn_slab, tc);
364}
365
Andy Grover70041082009-08-21 12:28:31 +0000366static int rds_tcp_conn_alloc(struct rds_connection *conn, gfp_t gfp)
367{
368 struct rds_tcp_connection *tc;
Sowmini Varadhan66261da2017-12-22 09:39:01 -0800369 int i, j;
370 int ret = 0;
Andy Grover70041082009-08-21 12:28:31 +0000371
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700372 for (i = 0; i < RDS_MPATH_WORKERS; i++) {
373 tc = kmem_cache_alloc(rds_tcp_conn_slab, gfp);
Sowmini Varadhan66261da2017-12-22 09:39:01 -0800374 if (!tc) {
375 ret = -ENOMEM;
Sowmini Varadhanebeeb1a2018-02-03 04:26:51 -0800376 goto fail;
Sowmini Varadhan66261da2017-12-22 09:39:01 -0800377 }
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700378 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;
Andy Grover70041082009-08-21 12:28:31 +0000383
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700384 conn->c_path[i].cp_transport_data = tc;
385 tc->t_cpath = &conn->c_path[i];
Sowmini Varadhanebeeb1a2018-02-03 04:26:51 -0800386 tc->t_tcp_node_detached = true;
Andy Grover70041082009-08-21 12:28:31 +0000387
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700388 rdsdebug("rds_conn_path [%d] tc %p\n", i,
389 conn->c_path[i].cp_transport_data);
390 }
Sowmini Varadhan53d0e832018-03-15 03:54:26 -0700391 spin_lock_irq(&rds_tcp_conn_lock);
Sowmini Varadhanebeeb1a2018-02-03 04:26:51 -0800392 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 }
Sowmini Varadhan53d0e832018-03-15 03:54:26 -0700397 spin_unlock_irq(&rds_tcp_conn_lock);
Sowmini Varadhanebeeb1a2018-02-03 04:26:51 -0800398fail:
Sowmini Varadhan66261da2017-12-22 09:39:01 -0800399 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;
Andy Grover70041082009-08-21 12:28:31 +0000404}
405
Sowmini Varadhanafb41642016-06-30 16:11:13 -0700406static 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
Sowmini Varadhanebeeb1a2018-02-03 04:26:51 -0800417static 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
Andy Grover70041082009-08-21 12:28:31 +0000427static 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);
Sowmini Varadhanafb41642016-06-30 16:11:13 -0700434 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 }
Andy Grover70041082009-08-21 12:28:31 +0000438 spin_unlock_irq(&rds_tcp_conn_lock);
439
Sowmini Varadhan26e4e6b2016-06-30 16:11:11 -0700440 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node)
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700441 rds_conn_destroy(tc->t_cpath->cp_conn);
Andy Grover70041082009-08-21 12:28:31 +0000442}
443
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400444static void rds_tcp_exit(void);
Andy Grover70041082009-08-21 12:28:31 +0000445
Santosh Shilimkar56dc8bc2018-10-13 21:36:49 +0800446static 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
Andy Grover70041082009-08-21 12:28:31 +0000452struct rds_transport rds_tcp_transport = {
453 .laddr_check = rds_tcp_laddr_check,
Sowmini Varadhan226f7a72016-06-30 16:11:10 -0700454 .xmit_path_prepare = rds_tcp_xmit_path_prepare,
455 .xmit_path_complete = rds_tcp_xmit_path_complete,
Andy Grover70041082009-08-21 12:28:31 +0000456 .xmit = rds_tcp_xmit,
Sowmini Varadhan2da43c42016-06-30 16:11:15 -0700457 .recv_path = rds_tcp_recv_path,
Andy Grover70041082009-08-21 12:28:31 +0000458 .conn_alloc = rds_tcp_conn_alloc,
459 .conn_free = rds_tcp_conn_free,
Sowmini Varadhanb04e8552016-06-30 16:11:16 -0700460 .conn_path_connect = rds_tcp_conn_path_connect,
Sowmini Varadhan226f7a72016-06-30 16:11:10 -0700461 .conn_path_shutdown = rds_tcp_conn_path_shutdown,
Andy Grover70041082009-08-21 12:28:31 +0000462 .inc_copy_to_user = rds_tcp_inc_copy_to_user,
Andy Grover70041082009-08-21 12:28:31 +0000463 .inc_free = rds_tcp_inc_free,
464 .stats_info_copy = rds_tcp_stats_info_copy,
465 .exit = rds_tcp_exit,
Santosh Shilimkar56dc8bc2018-10-13 21:36:49 +0800466 .get_tos_map = rds_tcp_get_tos_map,
Andy Grover70041082009-08-21 12:28:31 +0000467 .t_owner = THIS_MODULE,
468 .t_name = "tcp",
Andy Grover335776b2009-08-21 12:28:34 +0000469 .t_type = RDS_TRANS_TCP,
Andy Grover70041082009-08-21 12:28:31 +0000470 .t_prefer_loopback = 1,
Sowmini Varadhan5916e2c2016-07-14 03:51:03 -0700471 .t_mp_capable = 1,
Sowmini Varadhanebeeb1a2018-02-03 04:26:51 -0800472 .t_unloading = rds_tcp_is_unloading,
Andy Grover70041082009-08-21 12:28:31 +0000473};
474
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +0300475static unsigned int rds_tcp_netid;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400476
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;
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700481 struct ctl_table_header *rds_tcp_sysctl;
482 struct ctl_table *ctl_table;
483 int sndbuf_size;
484 int rcvbuf_size;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400485};
486
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700487/* All module specific customizations to the RDS-TCP socket should be done in
488 * rds_tcp_tune() and applied after socket creation.
489 */
490void rds_tcp_tune(struct socket *sock)
491{
492 struct sock *sk = sock->sk;
493 struct net *net = sock_net(sk);
494 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
495
Christoph Hellwig12abc5e2020-05-28 07:12:19 +0200496 tcp_sock_set_nodelay(sock->sk);
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700497 lock_sock(sk);
498 if (rtn->sndbuf_size > 0) {
499 sk->sk_sndbuf = rtn->sndbuf_size;
500 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
501 }
502 if (rtn->rcvbuf_size > 0) {
William Kucharski19f36ed2021-12-01 07:45:22 -0700503 sk->sk_rcvbuf = rtn->rcvbuf_size;
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700504 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
505 }
506 release_sock(sk);
507}
508
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400509static void rds_tcp_accept_worker(struct work_struct *work)
510{
511 struct rds_tcp_net *rtn = container_of(work,
512 struct rds_tcp_net,
513 rds_tcp_accept_w);
514
515 while (rds_tcp_accept_one(rtn->rds_tcp_listen_sock) == 0)
516 cond_resched();
517}
518
519void rds_tcp_accept_work(struct sock *sk)
520{
521 struct net *net = sock_net(sk);
522 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
523
524 queue_work(rds_wq, &rtn->rds_tcp_accept_w);
525}
526
527static __net_init int rds_tcp_init_net(struct net *net)
528{
529 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700530 struct ctl_table *tbl;
531 int err = 0;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400532
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700533 memset(rtn, 0, sizeof(*rtn));
534
535 /* {snd, rcv}buf_size default to 0, which implies we let the
536 * stack pick the value, and permit auto-tuning of buffer size.
537 */
538 if (net == &init_net) {
539 tbl = rds_tcp_sysctl_table;
540 } else {
541 tbl = kmemdup(rds_tcp_sysctl_table,
542 sizeof(rds_tcp_sysctl_table), GFP_KERNEL);
543 if (!tbl) {
Colin Ian Kingd14a1082019-05-03 13:10:17 +0100544 pr_warn("could not set allocate sysctl table\n");
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700545 return -ENOMEM;
546 }
547 rtn->ctl_table = tbl;
548 }
549 tbl[RDS_TCP_SNDBUF].data = &rtn->sndbuf_size;
550 tbl[RDS_TCP_RCVBUF].data = &rtn->rcvbuf_size;
551 rtn->rds_tcp_sysctl = register_net_sysctl(net, "net/rds/tcp", tbl);
552 if (!rtn->rds_tcp_sysctl) {
553 pr_warn("could not register sysctl\n");
554 err = -ENOMEM;
555 goto fail;
556 }
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700557
558#if IS_ENABLED(CONFIG_IPV6)
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -0700559 rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net, true);
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700560#else
561 rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net, false);
562#endif
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400563 if (!rtn->rds_tcp_listen_sock) {
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -0700564 pr_warn("could not set up IPv6 listen sock\n");
565
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700566#if IS_ENABLED(CONFIG_IPV6)
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -0700567 /* Try IPv4 as some systems disable IPv6 */
568 rtn->rds_tcp_listen_sock = rds_tcp_listen_init(net, false);
569 if (!rtn->rds_tcp_listen_sock) {
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700570#endif
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -0700571 unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
572 rtn->rds_tcp_sysctl = NULL;
573 err = -EAFNOSUPPORT;
574 goto fail;
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700575#if IS_ENABLED(CONFIG_IPV6)
Ka-Cheong Poon1e2b44e2018-07-23 20:51:22 -0700576 }
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700577#endif
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400578 }
579 INIT_WORK(&rtn->rds_tcp_accept_w, rds_tcp_accept_worker);
580 return 0;
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700581
582fail:
583 if (net != &init_net)
584 kfree(tbl);
585 return err;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400586}
587
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400588static void rds_tcp_kill_sock(struct net *net)
589{
590 struct rds_tcp_connection *tc, *_tc;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400591 LIST_HEAD(tmp_list);
592 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
Sowmini Varadhanb21dd452017-03-04 08:57:35 -0800593 struct socket *lsock = rtn->rds_tcp_listen_sock;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400594
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400595 rtn->rds_tcp_listen_sock = NULL;
Sowmini Varadhanb21dd452017-03-04 08:57:35 -0800596 rds_tcp_listen_stop(lsock, &rtn->rds_tcp_accept_w);
Sowmini Varadhan53d0e832018-03-15 03:54:26 -0700597 spin_lock_irq(&rds_tcp_conn_lock);
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400598 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
Sowmini Varadhan681648e2017-11-30 11:11:28 -0800599 struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net);
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400600
Mao Wenancb66ddd12019-03-28 17:10:56 +0800601 if (net != c_net)
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400602 continue;
Sowmini Varadhanf10b4cf2017-11-30 11:11:29 -0800603 if (!list_has_conn(&tmp_list, tc->t_cpath->cp_conn)) {
Sowmini Varadhanafb41642016-06-30 16:11:13 -0700604 list_move_tail(&tc->t_tcp_node, &tmp_list);
Sowmini Varadhanf10b4cf2017-11-30 11:11:29 -0800605 } else {
606 list_del(&tc->t_tcp_node);
607 tc->t_tcp_node_detached = true;
608 }
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400609 }
Sowmini Varadhan53d0e832018-03-15 03:54:26 -0700610 spin_unlock_irq(&rds_tcp_conn_lock);
Sowmini Varadhan2d746c92017-11-30 11:11:27 -0800611 list_for_each_entry_safe(tc, _tc, &tmp_list, t_tcp_node)
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700612 rds_conn_destroy(tc->t_cpath->cp_conn);
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400613}
614
Sowmini Varadhanbdf5bd72018-03-19 06:52:48 -0700615static void __net_exit rds_tcp_exit_net(struct net *net)
616{
617 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
618
619 rds_tcp_kill_sock(net);
620
621 if (rtn->rds_tcp_sysctl)
622 unregister_net_sysctl_table(rtn->rds_tcp_sysctl);
623
Zhu Yanjun4087d2b2018-12-30 23:24:11 +0800624 if (net != &init_net)
Sowmini Varadhanbdf5bd72018-03-19 06:52:48 -0700625 kfree(rtn->ctl_table);
626}
627
628static struct pernet_operations rds_tcp_net_ops = {
629 .init = rds_tcp_init_net,
630 .exit = rds_tcp_exit_net,
631 .id = &rds_tcp_netid,
632 .size = sizeof(struct rds_tcp_net),
Sowmini Varadhanbdf5bd72018-03-19 06:52:48 -0700633};
634
Sowmini Varadhana93d01f2016-07-14 03:51:01 -0700635void *rds_tcp_listen_sock_def_readable(struct net *net)
636{
637 struct rds_tcp_net *rtn = net_generic(net, rds_tcp_netid);
Sowmini Varadhanb21dd452017-03-04 08:57:35 -0800638 struct socket *lsock = rtn->rds_tcp_listen_sock;
Sowmini Varadhana93d01f2016-07-14 03:51:01 -0700639
Sowmini Varadhanb21dd452017-03-04 08:57:35 -0800640 if (!lsock)
641 return NULL;
642
643 return lsock->sk->sk_user_data;
Sowmini Varadhana93d01f2016-07-14 03:51:01 -0700644}
645
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700646/* when sysctl is used to modify some kernel socket parameters,this
647 * function resets the RDS connections in that netns so that we can
648 * restart with new parameters. The assumption is that such reset
649 * events are few and far-between.
650 */
651static void rds_tcp_sysctl_reset(struct net *net)
652{
653 struct rds_tcp_connection *tc, *_tc;
654
Sowmini Varadhan53d0e832018-03-15 03:54:26 -0700655 spin_lock_irq(&rds_tcp_conn_lock);
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700656 list_for_each_entry_safe(tc, _tc, &rds_tcp_conn_list, t_tcp_node) {
Sowmini Varadhan681648e2017-11-30 11:11:28 -0800657 struct net *c_net = read_pnet(&tc->t_cpath->cp_conn->c_net);
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700658
659 if (net != c_net || !tc->t_sock)
660 continue;
661
Sowmini Varadhan02105b22016-06-30 16:11:12 -0700662 /* reconnect with new parameters */
Sowmini Varadhanaed20a52017-07-16 16:43:46 -0700663 rds_conn_path_drop(tc->t_cpath, false);
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700664 }
Sowmini Varadhan53d0e832018-03-15 03:54:26 -0700665 spin_unlock_irq(&rds_tcp_conn_lock);
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700666}
667
668static int rds_tcp_skbuf_handler(struct ctl_table *ctl, int write,
Christoph Hellwig32927392020-04-24 08:43:38 +0200669 void *buffer, size_t *lenp, loff_t *fpos)
Sowmini Varadhanc6a58ff2016-03-16 11:38:12 -0700670{
671 struct net *net = current->nsproxy->net_ns;
672 int err;
673
674 err = proc_dointvec_minmax(ctl, write, buffer, lenp, fpos);
675 if (err < 0) {
676 pr_warn("Invalid input. Must be >= %d\n",
677 *(int *)(ctl->extra1));
678 return err;
679 }
680 if (write)
681 rds_tcp_sysctl_reset(net);
682 return 0;
683}
684
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400685static void rds_tcp_exit(void)
686{
Sowmini Varadhanebeeb1a2018-02-03 04:26:51 -0800687 rds_tcp_set_unloading();
688 synchronize_rcu();
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400689 rds_info_deregister_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700690#if IS_ENABLED(CONFIG_IPV6)
Ka-Cheong Poonb7ff8b12018-07-23 20:51:23 -0700691 rds_info_deregister_func(RDS6_INFO_TCP_SOCKETS, rds6_tcp_tc_info);
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700692#endif
Sowmini Varadhanbdf5bd72018-03-19 06:52:48 -0700693 unregister_pernet_device(&rds_tcp_net_ops);
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400694 rds_tcp_destroy_conns();
695 rds_trans_unregister(&rds_tcp_transport);
696 rds_tcp_recv_exit();
697 kmem_cache_destroy(rds_tcp_conn_slab);
698}
699module_exit(rds_tcp_exit);
700
stephen hemmingerff51bf82010-10-19 08:08:33 +0000701static int rds_tcp_init(void)
Andy Grover70041082009-08-21 12:28:31 +0000702{
703 int ret;
704
705 rds_tcp_conn_slab = kmem_cache_create("rds_tcp_connection",
706 sizeof(struct rds_tcp_connection),
707 0, 0, NULL);
Andy Grover8690bfa2010-01-12 11:56:44 -0800708 if (!rds_tcp_conn_slab) {
Andy Grover70041082009-08-21 12:28:31 +0000709 ret = -ENOMEM;
710 goto out;
711 }
712
Sowmini Varadhan16c09b12017-03-04 08:57:34 -0800713 ret = rds_tcp_recv_init();
714 if (ret)
Zhu Yanjun3b5923f2017-02-24 04:28:01 -0500715 goto out_slab;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400716
Sowmini Varadhanbdf5bd72018-03-19 06:52:48 -0700717 ret = register_pernet_device(&rds_tcp_net_ops);
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400718 if (ret)
Sowmini Varadhan16c09b12017-03-04 08:57:34 -0800719 goto out_recv;
Sowmini Varadhan467fa152015-08-05 01:43:26 -0400720
Zhu Yanjuna8d63a52017-03-03 00:44:26 -0500721 rds_trans_register(&rds_tcp_transport);
Andy Grover70041082009-08-21 12:28:31 +0000722
Andy Grover70041082009-08-21 12:28:31 +0000723 rds_info_register_func(RDS_INFO_TCP_SOCKETS, rds_tcp_tc_info);
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700724#if IS_ENABLED(CONFIG_IPV6)
Ka-Cheong Poonb7ff8b12018-07-23 20:51:23 -0700725 rds_info_register_func(RDS6_INFO_TCP_SOCKETS, rds6_tcp_tc_info);
Ka-Cheong Poone65d4d92018-07-30 22:48:42 -0700726#endif
Andy Grover70041082009-08-21 12:28:31 +0000727
728 goto out;
Sowmini Varadhan16c09b12017-03-04 08:57:34 -0800729out_recv:
730 rds_tcp_recv_exit();
Zhu Yanjun3b5923f2017-02-24 04:28:01 -0500731out_slab:
Andy Grover70041082009-08-21 12:28:31 +0000732 kmem_cache_destroy(rds_tcp_conn_slab);
733out:
734 return ret;
735}
736module_init(rds_tcp_init);
737
738MODULE_AUTHOR("Oracle Corporation <rds-devel@oss.oracle.com>");
739MODULE_DESCRIPTION("RDS: TCP transport");
740MODULE_LICENSE("Dual BSD/GPL");