xref: /openbmc/linux/net/rds/loop.c (revision 6114eab535ab49239e0a6ce08eb9243664aef993)
113796bf9SAndy Grover /*
213796bf9SAndy Grover  * Copyright (c) 2006 Oracle.  All rights reserved.
313796bf9SAndy Grover  *
413796bf9SAndy Grover  * This software is available to you under a choice of one of two
513796bf9SAndy Grover  * licenses.  You may choose to be licensed under the terms of the GNU
613796bf9SAndy Grover  * General Public License (GPL) Version 2, available from the file
713796bf9SAndy Grover  * COPYING in the main directory of this source tree, or the
813796bf9SAndy Grover  * OpenIB.org BSD license below:
913796bf9SAndy Grover  *
1013796bf9SAndy Grover  *     Redistribution and use in source and binary forms, with or
1113796bf9SAndy Grover  *     without modification, are permitted provided that the following
1213796bf9SAndy Grover  *     conditions are met:
1313796bf9SAndy Grover  *
1413796bf9SAndy Grover  *      - Redistributions of source code must retain the above
1513796bf9SAndy Grover  *        copyright notice, this list of conditions and the following
1613796bf9SAndy Grover  *        disclaimer.
1713796bf9SAndy Grover  *
1813796bf9SAndy Grover  *      - Redistributions in binary form must reproduce the above
1913796bf9SAndy Grover  *        copyright notice, this list of conditions and the following
2013796bf9SAndy Grover  *        disclaimer in the documentation and/or other materials
2113796bf9SAndy Grover  *        provided with the distribution.
2213796bf9SAndy Grover  *
2313796bf9SAndy Grover  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2413796bf9SAndy Grover  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2513796bf9SAndy Grover  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2613796bf9SAndy Grover  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2713796bf9SAndy Grover  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2813796bf9SAndy Grover  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2913796bf9SAndy Grover  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3013796bf9SAndy Grover  * SOFTWARE.
3113796bf9SAndy Grover  *
3213796bf9SAndy Grover  */
3313796bf9SAndy Grover #include <linux/kernel.h>
345a0e3ad6STejun Heo #include <linux/slab.h>
3513796bf9SAndy Grover #include <linux/in.h>
3613796bf9SAndy Grover 
3713796bf9SAndy Grover #include "rds.h"
3813796bf9SAndy Grover #include "loop.h"
3913796bf9SAndy Grover 
4013796bf9SAndy Grover static DEFINE_SPINLOCK(loop_conns_lock);
4113796bf9SAndy Grover static LIST_HEAD(loop_conns);
4213796bf9SAndy Grover 
4313796bf9SAndy Grover /*
4413796bf9SAndy Grover  * This 'loopback' transport is a special case for flows that originate
4513796bf9SAndy Grover  * and terminate on the same machine.
4613796bf9SAndy Grover  *
4713796bf9SAndy Grover  * Connection build-up notices if the destination address is thought of
4813796bf9SAndy Grover  * as a local address by a transport.  At that time it decides to use the
4913796bf9SAndy Grover  * loopback transport instead of the bound transport of the sending socket.
5013796bf9SAndy Grover  *
5113796bf9SAndy Grover  * The loopback transport's sending path just hands the sent rds_message
5213796bf9SAndy Grover  * straight to the receiving path via an embedded rds_incoming.
5313796bf9SAndy Grover  */
5413796bf9SAndy Grover 
5513796bf9SAndy Grover /*
5613796bf9SAndy Grover  * Usually a message transits both the sender and receiver's conns as it
5713796bf9SAndy Grover  * flows to the receiver.  In the loopback case, though, the receive path
5813796bf9SAndy Grover  * is handed the sending conn so the sense of the addresses is reversed.
5913796bf9SAndy Grover  */
6013796bf9SAndy Grover static int rds_loop_xmit(struct rds_connection *conn, struct rds_message *rm,
6113796bf9SAndy Grover 			 unsigned int hdr_off, unsigned int sg,
6213796bf9SAndy Grover 			 unsigned int off)
6313796bf9SAndy Grover {
646094628bSNeil Horman 	struct scatterlist *sgp = &rm->data.op_sg[sg];
656094628bSNeil Horman 	int ret = sizeof(struct rds_header) +
666094628bSNeil Horman 			be32_to_cpu(rm->m_inc.i_hdr.h_len);
676094628bSNeil Horman 
6877dd550eSAndy Grover 	/* Do not send cong updates to loopback */
6977dd550eSAndy Grover 	if (rm->m_inc.i_hdr.h_flags & RDS_FLAG_CONG_BITMAP) {
7077dd550eSAndy Grover 		rds_cong_map_updated(conn->c_fcong, ~(u64) 0);
716094628bSNeil Horman 		ret = min_t(int, ret, sgp->length - conn->c_xmit_data_off);
726094628bSNeil Horman 		goto out;
7377dd550eSAndy Grover 	}
7477dd550eSAndy Grover 
7513796bf9SAndy Grover 	BUG_ON(hdr_off || sg || off);
7613796bf9SAndy Grover 
7713796bf9SAndy Grover 	rds_inc_init(&rm->m_inc, conn, conn->c_laddr);
78d37c9359SAndy Grover 	/* For the embedded inc. Matching put is in loop_inc_free() */
79d37c9359SAndy Grover 	rds_message_addref(rm);
8013796bf9SAndy Grover 
8113796bf9SAndy Grover 	rds_recv_incoming(conn, conn->c_laddr, conn->c_faddr, &rm->m_inc,
82*6114eab5SCong Wang 			  GFP_KERNEL);
8313796bf9SAndy Grover 
8413796bf9SAndy Grover 	rds_send_drop_acked(conn, be64_to_cpu(rm->m_inc.i_hdr.h_sequence),
8513796bf9SAndy Grover 			    NULL);
8613796bf9SAndy Grover 
8713796bf9SAndy Grover 	rds_inc_put(&rm->m_inc);
886094628bSNeil Horman out:
896094628bSNeil Horman 	return ret;
9013796bf9SAndy Grover }
9113796bf9SAndy Grover 
92d37c9359SAndy Grover /*
93d37c9359SAndy Grover  * See rds_loop_xmit(). Since our inc is embedded in the rm, we
94d37c9359SAndy Grover  * make sure the rm lives at least until the inc is done.
95d37c9359SAndy Grover  */
96d37c9359SAndy Grover static void rds_loop_inc_free(struct rds_incoming *inc)
97d37c9359SAndy Grover {
98d37c9359SAndy Grover         struct rds_message *rm = container_of(inc, struct rds_message, m_inc);
99d37c9359SAndy Grover         rds_message_put(rm);
100d37c9359SAndy Grover }
101d37c9359SAndy Grover 
10213796bf9SAndy Grover /* we need to at least give the thread something to succeed */
10313796bf9SAndy Grover static int rds_loop_recv(struct rds_connection *conn)
10413796bf9SAndy Grover {
10513796bf9SAndy Grover 	return 0;
10613796bf9SAndy Grover }
10713796bf9SAndy Grover 
10813796bf9SAndy Grover struct rds_loop_connection {
10913796bf9SAndy Grover 	struct list_head loop_node;
11013796bf9SAndy Grover 	struct rds_connection *conn;
11113796bf9SAndy Grover };
11213796bf9SAndy Grover 
11313796bf9SAndy Grover /*
11413796bf9SAndy Grover  * Even the loopback transport needs to keep track of its connections,
11513796bf9SAndy Grover  * so it can call rds_conn_destroy() on them on exit. N.B. there are
11613796bf9SAndy Grover  * 1+ loopback addresses (127.*.*.*) so it's not a bug to have
11713796bf9SAndy Grover  * multiple loopback conns allocated, although rather useless.
11813796bf9SAndy Grover  */
11913796bf9SAndy Grover static int rds_loop_conn_alloc(struct rds_connection *conn, gfp_t gfp)
12013796bf9SAndy Grover {
12113796bf9SAndy Grover 	struct rds_loop_connection *lc;
12213796bf9SAndy Grover 	unsigned long flags;
12313796bf9SAndy Grover 
12413796bf9SAndy Grover 	lc = kzalloc(sizeof(struct rds_loop_connection), GFP_KERNEL);
1258690bfa1SAndy Grover 	if (!lc)
12613796bf9SAndy Grover 		return -ENOMEM;
12713796bf9SAndy Grover 
12813796bf9SAndy Grover 	INIT_LIST_HEAD(&lc->loop_node);
12913796bf9SAndy Grover 	lc->conn = conn;
13013796bf9SAndy Grover 	conn->c_transport_data = lc;
13113796bf9SAndy Grover 
13213796bf9SAndy Grover 	spin_lock_irqsave(&loop_conns_lock, flags);
13313796bf9SAndy Grover 	list_add_tail(&lc->loop_node, &loop_conns);
13413796bf9SAndy Grover 	spin_unlock_irqrestore(&loop_conns_lock, flags);
13513796bf9SAndy Grover 
13613796bf9SAndy Grover 	return 0;
13713796bf9SAndy Grover }
13813796bf9SAndy Grover 
13913796bf9SAndy Grover static void rds_loop_conn_free(void *arg)
14013796bf9SAndy Grover {
14113796bf9SAndy Grover 	struct rds_loop_connection *lc = arg;
14258c490baSPavel Emelyanov 	unsigned long flags;
14358c490baSPavel Emelyanov 
14413796bf9SAndy Grover 	rdsdebug("lc %p\n", lc);
14558c490baSPavel Emelyanov 	spin_lock_irqsave(&loop_conns_lock, flags);
14613796bf9SAndy Grover 	list_del(&lc->loop_node);
14758c490baSPavel Emelyanov 	spin_unlock_irqrestore(&loop_conns_lock, flags);
14813796bf9SAndy Grover 	kfree(lc);
14913796bf9SAndy Grover }
15013796bf9SAndy Grover 
15113796bf9SAndy Grover static int rds_loop_conn_connect(struct rds_connection *conn)
15213796bf9SAndy Grover {
15313796bf9SAndy Grover 	rds_connect_complete(conn);
15413796bf9SAndy Grover 	return 0;
15513796bf9SAndy Grover }
15613796bf9SAndy Grover 
15713796bf9SAndy Grover static void rds_loop_conn_shutdown(struct rds_connection *conn)
15813796bf9SAndy Grover {
15913796bf9SAndy Grover }
16013796bf9SAndy Grover 
16113796bf9SAndy Grover void rds_loop_exit(void)
16213796bf9SAndy Grover {
16313796bf9SAndy Grover 	struct rds_loop_connection *lc, *_lc;
16413796bf9SAndy Grover 	LIST_HEAD(tmp_list);
16513796bf9SAndy Grover 
16613796bf9SAndy Grover 	/* avoid calling conn_destroy with irqs off */
16713796bf9SAndy Grover 	spin_lock_irq(&loop_conns_lock);
16813796bf9SAndy Grover 	list_splice(&loop_conns, &tmp_list);
16913796bf9SAndy Grover 	INIT_LIST_HEAD(&loop_conns);
17013796bf9SAndy Grover 	spin_unlock_irq(&loop_conns_lock);
17113796bf9SAndy Grover 
17213796bf9SAndy Grover 	list_for_each_entry_safe(lc, _lc, &tmp_list, loop_node) {
17313796bf9SAndy Grover 		WARN_ON(lc->conn->c_passive);
17413796bf9SAndy Grover 		rds_conn_destroy(lc->conn);
17513796bf9SAndy Grover 	}
17613796bf9SAndy Grover }
17713796bf9SAndy Grover 
17813796bf9SAndy Grover /*
17913796bf9SAndy Grover  * This is missing .xmit_* because loop doesn't go through generic
18013796bf9SAndy Grover  * rds_send_xmit() and doesn't call rds_recv_incoming().  .listen_stop and
18113796bf9SAndy Grover  * .laddr_check are missing because transport.c doesn't iterate over
18213796bf9SAndy Grover  * rds_loop_transport.
18313796bf9SAndy Grover  */
18413796bf9SAndy Grover struct rds_transport rds_loop_transport = {
18513796bf9SAndy Grover 	.xmit			= rds_loop_xmit,
18613796bf9SAndy Grover 	.recv			= rds_loop_recv,
18713796bf9SAndy Grover 	.conn_alloc		= rds_loop_conn_alloc,
18813796bf9SAndy Grover 	.conn_free		= rds_loop_conn_free,
18913796bf9SAndy Grover 	.conn_connect		= rds_loop_conn_connect,
19013796bf9SAndy Grover 	.conn_shutdown		= rds_loop_conn_shutdown,
19113796bf9SAndy Grover 	.inc_copy_to_user	= rds_message_inc_copy_to_user,
192d37c9359SAndy Grover 	.inc_free		= rds_loop_inc_free,
19313796bf9SAndy Grover 	.t_name			= "loopback",
19413796bf9SAndy Grover };
195