Linux Audio

Check our new training course

Linux kernel drivers training

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