18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2006, 2017 Oracle and/or its affiliates. All rights reserved.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two
58c2ecf20Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
68c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
78c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the
88c2ecf20Sopenharmony_ci * OpenIB.org BSD license below:
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
118c2ecf20Sopenharmony_ci *     without modification, are permitted provided that the following
128c2ecf20Sopenharmony_ci *     conditions are met:
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *      - Redistributions of source code must retain the above
158c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
168c2ecf20Sopenharmony_ci *        disclaimer.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
198c2ecf20Sopenharmony_ci *        copyright notice, this list of conditions and the following
208c2ecf20Sopenharmony_ci *        disclaimer in the documentation and/or other materials
218c2ecf20Sopenharmony_ci *        provided with the distribution.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
248c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
258c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
268c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
278c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
288c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
298c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
308c2ecf20Sopenharmony_ci * SOFTWARE.
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci#include <linux/kernel.h>
348c2ecf20Sopenharmony_ci#include <linux/slab.h>
358c2ecf20Sopenharmony_ci#include <linux/in.h>
368c2ecf20Sopenharmony_ci#include <net/net_namespace.h>
378c2ecf20Sopenharmony_ci#include <net/netns/generic.h>
388c2ecf20Sopenharmony_ci#include <linux/ipv6.h>
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#include "rds_single_path.h"
418c2ecf20Sopenharmony_ci#include "rds.h"
428c2ecf20Sopenharmony_ci#include "loop.h"
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(loop_conns_lock);
458c2ecf20Sopenharmony_cistatic LIST_HEAD(loop_conns);
468c2ecf20Sopenharmony_cistatic atomic_t rds_loop_unloading = ATOMIC_INIT(0);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistatic void rds_loop_set_unloading(void)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	atomic_set(&rds_loop_unloading, 1);
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic bool rds_loop_is_unloading(struct rds_connection *conn)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	return atomic_read(&rds_loop_unloading) != 0;
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/*
598c2ecf20Sopenharmony_ci * This 'loopback' transport is a special case for flows that originate
608c2ecf20Sopenharmony_ci * and terminate on the same machine.
618c2ecf20Sopenharmony_ci *
628c2ecf20Sopenharmony_ci * Connection build-up notices if the destination address is thought of
638c2ecf20Sopenharmony_ci * as a local address by a transport.  At that time it decides to use the
648c2ecf20Sopenharmony_ci * loopback transport instead of the bound transport of the sending socket.
658c2ecf20Sopenharmony_ci *
668c2ecf20Sopenharmony_ci * The loopback transport's sending path just hands the sent rds_message
678c2ecf20Sopenharmony_ci * straight to the receiving path via an embedded rds_incoming.
688c2ecf20Sopenharmony_ci */
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/*
718c2ecf20Sopenharmony_ci * Usually a message transits both the sender and receiver's conns as it
728c2ecf20Sopenharmony_ci * flows to the receiver.  In the loopback case, though, the receive path
738c2ecf20Sopenharmony_ci * is handed the sending conn so the sense of the addresses is reversed.
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_cistatic int rds_loop_xmit(struct rds_connection *conn, struct rds_message *rm,
768c2ecf20Sopenharmony_ci			 unsigned int hdr_off, unsigned int sg,
778c2ecf20Sopenharmony_ci			 unsigned int off)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	struct scatterlist *sgp = &rm->data.op_sg[sg];
808c2ecf20Sopenharmony_ci	int ret = sizeof(struct rds_header) +
818c2ecf20Sopenharmony_ci			be32_to_cpu(rm->m_inc.i_hdr.h_len);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	/* Do not send cong updates to loopback */
848c2ecf20Sopenharmony_ci	if (rm->m_inc.i_hdr.h_flags & RDS_FLAG_CONG_BITMAP) {
858c2ecf20Sopenharmony_ci		rds_cong_map_updated(conn->c_fcong, ~(u64) 0);
868c2ecf20Sopenharmony_ci		ret = min_t(int, ret, sgp->length - conn->c_xmit_data_off);
878c2ecf20Sopenharmony_ci		goto out;
888c2ecf20Sopenharmony_ci	}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	BUG_ON(hdr_off || sg || off);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	rds_inc_init(&rm->m_inc, conn, &conn->c_laddr);
938c2ecf20Sopenharmony_ci	/* For the embedded inc. Matching put is in loop_inc_free() */
948c2ecf20Sopenharmony_ci	rds_message_addref(rm);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	rds_recv_incoming(conn, &conn->c_laddr, &conn->c_faddr, &rm->m_inc,
978c2ecf20Sopenharmony_ci			  GFP_KERNEL);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	rds_send_drop_acked(conn, be64_to_cpu(rm->m_inc.i_hdr.h_sequence),
1008c2ecf20Sopenharmony_ci			    NULL);
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	rds_inc_put(&rm->m_inc);
1038c2ecf20Sopenharmony_ciout:
1048c2ecf20Sopenharmony_ci	return ret;
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci/*
1088c2ecf20Sopenharmony_ci * See rds_loop_xmit(). Since our inc is embedded in the rm, we
1098c2ecf20Sopenharmony_ci * make sure the rm lives at least until the inc is done.
1108c2ecf20Sopenharmony_ci */
1118c2ecf20Sopenharmony_cistatic void rds_loop_inc_free(struct rds_incoming *inc)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	struct rds_message *rm = container_of(inc, struct rds_message, m_inc);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	rds_message_put(rm);
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci/* we need to at least give the thread something to succeed */
1198c2ecf20Sopenharmony_cistatic int rds_loop_recv_path(struct rds_conn_path *cp)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	return 0;
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistruct rds_loop_connection {
1258c2ecf20Sopenharmony_ci	struct list_head loop_node;
1268c2ecf20Sopenharmony_ci	struct rds_connection *conn;
1278c2ecf20Sopenharmony_ci};
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/*
1308c2ecf20Sopenharmony_ci * Even the loopback transport needs to keep track of its connections,
1318c2ecf20Sopenharmony_ci * so it can call rds_conn_destroy() on them on exit. N.B. there are
1328c2ecf20Sopenharmony_ci * 1+ loopback addresses (127.*.*.*) so it's not a bug to have
1338c2ecf20Sopenharmony_ci * multiple loopback conns allocated, although rather useless.
1348c2ecf20Sopenharmony_ci */
1358c2ecf20Sopenharmony_cistatic int rds_loop_conn_alloc(struct rds_connection *conn, gfp_t gfp)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	struct rds_loop_connection *lc;
1388c2ecf20Sopenharmony_ci	unsigned long flags;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	lc = kzalloc(sizeof(struct rds_loop_connection), gfp);
1418c2ecf20Sopenharmony_ci	if (!lc)
1428c2ecf20Sopenharmony_ci		return -ENOMEM;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&lc->loop_node);
1458c2ecf20Sopenharmony_ci	lc->conn = conn;
1468c2ecf20Sopenharmony_ci	conn->c_transport_data = lc;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	spin_lock_irqsave(&loop_conns_lock, flags);
1498c2ecf20Sopenharmony_ci	list_add_tail(&lc->loop_node, &loop_conns);
1508c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&loop_conns_lock, flags);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	return 0;
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic void rds_loop_conn_free(void *arg)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	struct rds_loop_connection *lc = arg;
1588c2ecf20Sopenharmony_ci	unsigned long flags;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	rdsdebug("lc %p\n", lc);
1618c2ecf20Sopenharmony_ci	spin_lock_irqsave(&loop_conns_lock, flags);
1628c2ecf20Sopenharmony_ci	list_del(&lc->loop_node);
1638c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&loop_conns_lock, flags);
1648c2ecf20Sopenharmony_ci	kfree(lc);
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic int rds_loop_conn_path_connect(struct rds_conn_path *cp)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	rds_connect_complete(cp->cp_conn);
1708c2ecf20Sopenharmony_ci	return 0;
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic void rds_loop_conn_path_shutdown(struct rds_conn_path *cp)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_civoid rds_loop_exit(void)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	struct rds_loop_connection *lc, *_lc;
1808c2ecf20Sopenharmony_ci	LIST_HEAD(tmp_list);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	rds_loop_set_unloading();
1838c2ecf20Sopenharmony_ci	synchronize_rcu();
1848c2ecf20Sopenharmony_ci	/* avoid calling conn_destroy with irqs off */
1858c2ecf20Sopenharmony_ci	spin_lock_irq(&loop_conns_lock);
1868c2ecf20Sopenharmony_ci	list_splice(&loop_conns, &tmp_list);
1878c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&loop_conns);
1888c2ecf20Sopenharmony_ci	spin_unlock_irq(&loop_conns_lock);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	list_for_each_entry_safe(lc, _lc, &tmp_list, loop_node) {
1918c2ecf20Sopenharmony_ci		WARN_ON(lc->conn->c_passive);
1928c2ecf20Sopenharmony_ci		rds_conn_destroy(lc->conn);
1938c2ecf20Sopenharmony_ci	}
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic void rds_loop_kill_conns(struct net *net)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	struct rds_loop_connection *lc, *_lc;
1998c2ecf20Sopenharmony_ci	LIST_HEAD(tmp_list);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	spin_lock_irq(&loop_conns_lock);
2028c2ecf20Sopenharmony_ci	list_for_each_entry_safe(lc, _lc, &loop_conns, loop_node)  {
2038c2ecf20Sopenharmony_ci		struct net *c_net = read_pnet(&lc->conn->c_net);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci		if (net != c_net)
2068c2ecf20Sopenharmony_ci			continue;
2078c2ecf20Sopenharmony_ci		list_move_tail(&lc->loop_node, &tmp_list);
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci	spin_unlock_irq(&loop_conns_lock);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	list_for_each_entry_safe(lc, _lc, &tmp_list, loop_node) {
2128c2ecf20Sopenharmony_ci		WARN_ON(lc->conn->c_passive);
2138c2ecf20Sopenharmony_ci		rds_conn_destroy(lc->conn);
2148c2ecf20Sopenharmony_ci	}
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_cistatic void __net_exit rds_loop_exit_net(struct net *net)
2188c2ecf20Sopenharmony_ci{
2198c2ecf20Sopenharmony_ci	rds_loop_kill_conns(net);
2208c2ecf20Sopenharmony_ci}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistatic struct pernet_operations rds_loop_net_ops = {
2238c2ecf20Sopenharmony_ci	.exit = rds_loop_exit_net,
2248c2ecf20Sopenharmony_ci};
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ciint rds_loop_net_init(void)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	return register_pernet_device(&rds_loop_net_ops);
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_civoid rds_loop_net_exit(void)
2328c2ecf20Sopenharmony_ci{
2338c2ecf20Sopenharmony_ci	unregister_pernet_device(&rds_loop_net_ops);
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci/*
2378c2ecf20Sopenharmony_ci * This is missing .xmit_* because loop doesn't go through generic
2388c2ecf20Sopenharmony_ci * rds_send_xmit() and doesn't call rds_recv_incoming().  .listen_stop and
2398c2ecf20Sopenharmony_ci * .laddr_check are missing because transport.c doesn't iterate over
2408c2ecf20Sopenharmony_ci * rds_loop_transport.
2418c2ecf20Sopenharmony_ci */
2428c2ecf20Sopenharmony_cistruct rds_transport rds_loop_transport = {
2438c2ecf20Sopenharmony_ci	.xmit			= rds_loop_xmit,
2448c2ecf20Sopenharmony_ci	.recv_path		= rds_loop_recv_path,
2458c2ecf20Sopenharmony_ci	.conn_alloc		= rds_loop_conn_alloc,
2468c2ecf20Sopenharmony_ci	.conn_free		= rds_loop_conn_free,
2478c2ecf20Sopenharmony_ci	.conn_path_connect	= rds_loop_conn_path_connect,
2488c2ecf20Sopenharmony_ci	.conn_path_shutdown	= rds_loop_conn_path_shutdown,
2498c2ecf20Sopenharmony_ci	.inc_copy_to_user	= rds_message_inc_copy_to_user,
2508c2ecf20Sopenharmony_ci	.inc_free		= rds_loop_inc_free,
2518c2ecf20Sopenharmony_ci	.t_name			= "loopback",
2528c2ecf20Sopenharmony_ci	.t_type			= RDS_TRANS_LOOP,
2538c2ecf20Sopenharmony_ci	.t_unloading		= rds_loop_is_unloading,
2548c2ecf20Sopenharmony_ci};
255