113796bf9SAndy Grover /*
2*eee2fa6aSKa-Cheong Poon * Copyright (c) 2006, 2017 Oracle and/or its affiliates. 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>
36c809195fSSowmini Varadhan #include <net/net_namespace.h>
37c809195fSSowmini Varadhan #include <net/netns/generic.h>
38*eee2fa6aSKa-Cheong Poon #include <linux/ipv6.h>
3913796bf9SAndy Grover
400cb43965SSowmini Varadhan #include "rds_single_path.h"
4113796bf9SAndy Grover #include "rds.h"
4213796bf9SAndy Grover #include "loop.h"
4313796bf9SAndy Grover
4413796bf9SAndy Grover static DEFINE_SPINLOCK(loop_conns_lock);
4513796bf9SAndy Grover static LIST_HEAD(loop_conns);
46c809195fSSowmini Varadhan static atomic_t rds_loop_unloading = ATOMIC_INIT(0);
47c809195fSSowmini Varadhan
rds_loop_set_unloading(void)48c809195fSSowmini Varadhan static void rds_loop_set_unloading(void)
49c809195fSSowmini Varadhan {
50c809195fSSowmini Varadhan atomic_set(&rds_loop_unloading, 1);
51c809195fSSowmini Varadhan }
52c809195fSSowmini Varadhan
rds_loop_is_unloading(struct rds_connection * conn)53c809195fSSowmini Varadhan static bool rds_loop_is_unloading(struct rds_connection *conn)
54c809195fSSowmini Varadhan {
55c809195fSSowmini Varadhan return atomic_read(&rds_loop_unloading) != 0;
56c809195fSSowmini Varadhan }
5713796bf9SAndy Grover
5813796bf9SAndy Grover /*
5913796bf9SAndy Grover * This 'loopback' transport is a special case for flows that originate
6013796bf9SAndy Grover * and terminate on the same machine.
6113796bf9SAndy Grover *
6213796bf9SAndy Grover * Connection build-up notices if the destination address is thought of
6313796bf9SAndy Grover * as a local address by a transport. At that time it decides to use the
6413796bf9SAndy Grover * loopback transport instead of the bound transport of the sending socket.
6513796bf9SAndy Grover *
6613796bf9SAndy Grover * The loopback transport's sending path just hands the sent rds_message
6713796bf9SAndy Grover * straight to the receiving path via an embedded rds_incoming.
6813796bf9SAndy Grover */
6913796bf9SAndy Grover
7013796bf9SAndy Grover /*
7113796bf9SAndy Grover * Usually a message transits both the sender and receiver's conns as it
7213796bf9SAndy Grover * flows to the receiver. In the loopback case, though, the receive path
7313796bf9SAndy Grover * is handed the sending conn so the sense of the addresses is reversed.
7413796bf9SAndy Grover */
rds_loop_xmit(struct rds_connection * conn,struct rds_message * rm,unsigned int hdr_off,unsigned int sg,unsigned int off)7513796bf9SAndy Grover static int rds_loop_xmit(struct rds_connection *conn, struct rds_message *rm,
7613796bf9SAndy Grover unsigned int hdr_off, unsigned int sg,
7713796bf9SAndy Grover unsigned int off)
7813796bf9SAndy Grover {
796094628bSNeil Horman struct scatterlist *sgp = &rm->data.op_sg[sg];
806094628bSNeil Horman int ret = sizeof(struct rds_header) +
816094628bSNeil Horman be32_to_cpu(rm->m_inc.i_hdr.h_len);
826094628bSNeil Horman
8377dd550eSAndy Grover /* Do not send cong updates to loopback */
8477dd550eSAndy Grover if (rm->m_inc.i_hdr.h_flags & RDS_FLAG_CONG_BITMAP) {
8577dd550eSAndy Grover rds_cong_map_updated(conn->c_fcong, ~(u64) 0);
866094628bSNeil Horman ret = min_t(int, ret, sgp->length - conn->c_xmit_data_off);
876094628bSNeil Horman goto out;
8877dd550eSAndy Grover }
8977dd550eSAndy Grover
9013796bf9SAndy Grover BUG_ON(hdr_off || sg || off);
9113796bf9SAndy Grover
92*eee2fa6aSKa-Cheong Poon rds_inc_init(&rm->m_inc, conn, &conn->c_laddr);
93d37c9359SAndy Grover /* For the embedded inc. Matching put is in loop_inc_free() */
94d37c9359SAndy Grover rds_message_addref(rm);
9513796bf9SAndy Grover
96*eee2fa6aSKa-Cheong Poon rds_recv_incoming(conn, &conn->c_laddr, &conn->c_faddr, &rm->m_inc,
976114eab5SCong Wang GFP_KERNEL);
9813796bf9SAndy Grover
9913796bf9SAndy Grover rds_send_drop_acked(conn, be64_to_cpu(rm->m_inc.i_hdr.h_sequence),
10013796bf9SAndy Grover NULL);
10113796bf9SAndy Grover
10213796bf9SAndy Grover rds_inc_put(&rm->m_inc);
1036094628bSNeil Horman out:
1046094628bSNeil Horman return ret;
10513796bf9SAndy Grover }
10613796bf9SAndy Grover
107d37c9359SAndy Grover /*
108d37c9359SAndy Grover * See rds_loop_xmit(). Since our inc is embedded in the rm, we
109d37c9359SAndy Grover * make sure the rm lives at least until the inc is done.
110d37c9359SAndy Grover */
rds_loop_inc_free(struct rds_incoming * inc)111d37c9359SAndy Grover static void rds_loop_inc_free(struct rds_incoming *inc)
112d37c9359SAndy Grover {
113d37c9359SAndy Grover struct rds_message *rm = container_of(inc, struct rds_message, m_inc);
1145c3da57dSJoshua Houghton
115d37c9359SAndy Grover rds_message_put(rm);
116d37c9359SAndy Grover }
117d37c9359SAndy Grover
11813796bf9SAndy Grover /* we need to at least give the thread something to succeed */
rds_loop_recv_path(struct rds_conn_path * cp)1192da43c4aSSowmini Varadhan static int rds_loop_recv_path(struct rds_conn_path *cp)
12013796bf9SAndy Grover {
12113796bf9SAndy Grover return 0;
12213796bf9SAndy Grover }
12313796bf9SAndy Grover
12413796bf9SAndy Grover struct rds_loop_connection {
12513796bf9SAndy Grover struct list_head loop_node;
12613796bf9SAndy Grover struct rds_connection *conn;
12713796bf9SAndy Grover };
12813796bf9SAndy Grover
12913796bf9SAndy Grover /*
13013796bf9SAndy Grover * Even the loopback transport needs to keep track of its connections,
13113796bf9SAndy Grover * so it can call rds_conn_destroy() on them on exit. N.B. there are
13213796bf9SAndy Grover * 1+ loopback addresses (127.*.*.*) so it's not a bug to have
13313796bf9SAndy Grover * multiple loopback conns allocated, although rather useless.
13413796bf9SAndy Grover */
rds_loop_conn_alloc(struct rds_connection * conn,gfp_t gfp)13513796bf9SAndy Grover static int rds_loop_conn_alloc(struct rds_connection *conn, gfp_t gfp)
13613796bf9SAndy Grover {
13713796bf9SAndy Grover struct rds_loop_connection *lc;
13813796bf9SAndy Grover unsigned long flags;
13913796bf9SAndy Grover
140f0229eaaSDan Carpenter lc = kzalloc(sizeof(struct rds_loop_connection), gfp);
1418690bfa1SAndy Grover if (!lc)
14213796bf9SAndy Grover return -ENOMEM;
14313796bf9SAndy Grover
14413796bf9SAndy Grover INIT_LIST_HEAD(&lc->loop_node);
14513796bf9SAndy Grover lc->conn = conn;
14613796bf9SAndy Grover conn->c_transport_data = lc;
14713796bf9SAndy Grover
14813796bf9SAndy Grover spin_lock_irqsave(&loop_conns_lock, flags);
14913796bf9SAndy Grover list_add_tail(&lc->loop_node, &loop_conns);
15013796bf9SAndy Grover spin_unlock_irqrestore(&loop_conns_lock, flags);
15113796bf9SAndy Grover
15213796bf9SAndy Grover return 0;
15313796bf9SAndy Grover }
15413796bf9SAndy Grover
rds_loop_conn_free(void * arg)15513796bf9SAndy Grover static void rds_loop_conn_free(void *arg)
15613796bf9SAndy Grover {
15713796bf9SAndy Grover struct rds_loop_connection *lc = arg;
15858c490baSPavel Emelyanov unsigned long flags;
15958c490baSPavel Emelyanov
16013796bf9SAndy Grover rdsdebug("lc %p\n", lc);
16158c490baSPavel Emelyanov spin_lock_irqsave(&loop_conns_lock, flags);
16213796bf9SAndy Grover list_del(&lc->loop_node);
16358c490baSPavel Emelyanov spin_unlock_irqrestore(&loop_conns_lock, flags);
16413796bf9SAndy Grover kfree(lc);
16513796bf9SAndy Grover }
16613796bf9SAndy Grover
rds_loop_conn_path_connect(struct rds_conn_path * cp)167b04e8554SSowmini Varadhan static int rds_loop_conn_path_connect(struct rds_conn_path *cp)
16813796bf9SAndy Grover {
169b04e8554SSowmini Varadhan rds_connect_complete(cp->cp_conn);
17013796bf9SAndy Grover return 0;
17113796bf9SAndy Grover }
17213796bf9SAndy Grover
rds_loop_conn_path_shutdown(struct rds_conn_path * cp)173226f7a7dSSowmini Varadhan static void rds_loop_conn_path_shutdown(struct rds_conn_path *cp)
17413796bf9SAndy Grover {
17513796bf9SAndy Grover }
17613796bf9SAndy Grover
rds_loop_exit(void)17713796bf9SAndy Grover void rds_loop_exit(void)
17813796bf9SAndy Grover {
17913796bf9SAndy Grover struct rds_loop_connection *lc, *_lc;
18013796bf9SAndy Grover LIST_HEAD(tmp_list);
18113796bf9SAndy Grover
182c809195fSSowmini Varadhan rds_loop_set_unloading();
183c809195fSSowmini Varadhan synchronize_rcu();
18413796bf9SAndy Grover /* avoid calling conn_destroy with irqs off */
18513796bf9SAndy Grover spin_lock_irq(&loop_conns_lock);
18613796bf9SAndy Grover list_splice(&loop_conns, &tmp_list);
18713796bf9SAndy Grover INIT_LIST_HEAD(&loop_conns);
18813796bf9SAndy Grover spin_unlock_irq(&loop_conns_lock);
18913796bf9SAndy Grover
19013796bf9SAndy Grover list_for_each_entry_safe(lc, _lc, &tmp_list, loop_node) {
19113796bf9SAndy Grover WARN_ON(lc->conn->c_passive);
19213796bf9SAndy Grover rds_conn_destroy(lc->conn);
19313796bf9SAndy Grover }
19413796bf9SAndy Grover }
19513796bf9SAndy Grover
rds_loop_kill_conns(struct net * net)196c809195fSSowmini Varadhan static void rds_loop_kill_conns(struct net *net)
197c809195fSSowmini Varadhan {
198c809195fSSowmini Varadhan struct rds_loop_connection *lc, *_lc;
199c809195fSSowmini Varadhan LIST_HEAD(tmp_list);
200c809195fSSowmini Varadhan
201c809195fSSowmini Varadhan spin_lock_irq(&loop_conns_lock);
202c809195fSSowmini Varadhan list_for_each_entry_safe(lc, _lc, &loop_conns, loop_node) {
203c809195fSSowmini Varadhan struct net *c_net = read_pnet(&lc->conn->c_net);
204c809195fSSowmini Varadhan
205c809195fSSowmini Varadhan if (net != c_net)
206c809195fSSowmini Varadhan continue;
207c809195fSSowmini Varadhan list_move_tail(&lc->loop_node, &tmp_list);
208c809195fSSowmini Varadhan }
209c809195fSSowmini Varadhan spin_unlock_irq(&loop_conns_lock);
210c809195fSSowmini Varadhan
211c809195fSSowmini Varadhan list_for_each_entry_safe(lc, _lc, &tmp_list, loop_node) {
212c809195fSSowmini Varadhan WARN_ON(lc->conn->c_passive);
213c809195fSSowmini Varadhan rds_conn_destroy(lc->conn);
214c809195fSSowmini Varadhan }
215c809195fSSowmini Varadhan }
216c809195fSSowmini Varadhan
rds_loop_exit_net(struct net * net)217c809195fSSowmini Varadhan static void __net_exit rds_loop_exit_net(struct net *net)
218c809195fSSowmini Varadhan {
219c809195fSSowmini Varadhan rds_loop_kill_conns(net);
220c809195fSSowmini Varadhan }
221c809195fSSowmini Varadhan
222c809195fSSowmini Varadhan static struct pernet_operations rds_loop_net_ops = {
223c809195fSSowmini Varadhan .exit = rds_loop_exit_net,
224c809195fSSowmini Varadhan };
225c809195fSSowmini Varadhan
rds_loop_net_init(void)226c809195fSSowmini Varadhan int rds_loop_net_init(void)
227c809195fSSowmini Varadhan {
228c809195fSSowmini Varadhan return register_pernet_device(&rds_loop_net_ops);
229c809195fSSowmini Varadhan }
230c809195fSSowmini Varadhan
rds_loop_net_exit(void)231c809195fSSowmini Varadhan void rds_loop_net_exit(void)
232c809195fSSowmini Varadhan {
233c809195fSSowmini Varadhan unregister_pernet_device(&rds_loop_net_ops);
234c809195fSSowmini Varadhan }
235c809195fSSowmini Varadhan
23613796bf9SAndy Grover /*
23713796bf9SAndy Grover * This is missing .xmit_* because loop doesn't go through generic
23813796bf9SAndy Grover * rds_send_xmit() and doesn't call rds_recv_incoming(). .listen_stop and
23913796bf9SAndy Grover * .laddr_check are missing because transport.c doesn't iterate over
24013796bf9SAndy Grover * rds_loop_transport.
24113796bf9SAndy Grover */
24213796bf9SAndy Grover struct rds_transport rds_loop_transport = {
24313796bf9SAndy Grover .xmit = rds_loop_xmit,
2442da43c4aSSowmini Varadhan .recv_path = rds_loop_recv_path,
24513796bf9SAndy Grover .conn_alloc = rds_loop_conn_alloc,
24613796bf9SAndy Grover .conn_free = rds_loop_conn_free,
247b04e8554SSowmini Varadhan .conn_path_connect = rds_loop_conn_path_connect,
248226f7a7dSSowmini Varadhan .conn_path_shutdown = rds_loop_conn_path_shutdown,
24913796bf9SAndy Grover .inc_copy_to_user = rds_message_inc_copy_to_user,
250d37c9359SAndy Grover .inc_free = rds_loop_inc_free,
25113796bf9SAndy Grover .t_name = "loopback",
252f1693c63SSantosh Shilimkar .t_type = RDS_TRANS_LOOP,
253c809195fSSowmini Varadhan .t_unloading = rds_loop_is_unloading,
25413796bf9SAndy Grover };
255