blob: 1d73ad79c847bb7d918ba4ec358a4920a8e55c1c [file] [log] [blame]
Andy Grover13796bf2009-02-24 15:30:24 +00001/*
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -07002 * Copyright (c) 2006, 2017 Oracle and/or its affiliates. All rights reserved.
Andy Grover13796bf2009-02-24 15:30:24 +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 Grover13796bf2009-02-24 15:30:24 +000035#include <linux/in.h>
Sowmini Varadhanc809195f2018-06-25 06:41:25 -070036#include <net/net_namespace.h>
37#include <net/netns/generic.h>
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -070038#include <linux/ipv6.h>
Andy Grover13796bf2009-02-24 15:30:24 +000039
Sowmini Varadhan0cb43962016-06-13 09:44:26 -070040#include "rds_single_path.h"
Andy Grover13796bf2009-02-24 15:30:24 +000041#include "rds.h"
42#include "loop.h"
43
44static DEFINE_SPINLOCK(loop_conns_lock);
45static LIST_HEAD(loop_conns);
Sowmini Varadhanc809195f2018-06-25 06:41:25 -070046static atomic_t rds_loop_unloading = ATOMIC_INIT(0);
47
48static void rds_loop_set_unloading(void)
49{
50 atomic_set(&rds_loop_unloading, 1);
51}
52
53static bool rds_loop_is_unloading(struct rds_connection *conn)
54{
55 return atomic_read(&rds_loop_unloading) != 0;
56}
Andy Grover13796bf2009-02-24 15:30:24 +000057
58/*
59 * This 'loopback' transport is a special case for flows that originate
60 * and terminate on the same machine.
61 *
62 * Connection build-up notices if the destination address is thought of
63 * as a local address by a transport. At that time it decides to use the
64 * loopback transport instead of the bound transport of the sending socket.
65 *
66 * The loopback transport's sending path just hands the sent rds_message
67 * straight to the receiving path via an embedded rds_incoming.
68 */
69
70/*
71 * Usually a message transits both the sender and receiver's conns as it
72 * flows to the receiver. In the loopback case, though, the receive path
73 * is handed the sending conn so the sense of the addresses is reversed.
74 */
75static int rds_loop_xmit(struct rds_connection *conn, struct rds_message *rm,
76 unsigned int hdr_off, unsigned int sg,
77 unsigned int off)
78{
Neil Horman60946282011-03-02 06:28:22 +000079 struct scatterlist *sgp = &rm->data.op_sg[sg];
80 int ret = sizeof(struct rds_header) +
81 be32_to_cpu(rm->m_inc.i_hdr.h_len);
82
Andy Grover77dd5502010-03-22 15:22:04 -070083 /* Do not send cong updates to loopback */
84 if (rm->m_inc.i_hdr.h_flags & RDS_FLAG_CONG_BITMAP) {
85 rds_cong_map_updated(conn->c_fcong, ~(u64) 0);
Neil Horman60946282011-03-02 06:28:22 +000086 ret = min_t(int, ret, sgp->length - conn->c_xmit_data_off);
87 goto out;
Andy Grover77dd5502010-03-22 15:22:04 -070088 }
89
Andy Grover13796bf2009-02-24 15:30:24 +000090 BUG_ON(hdr_off || sg || off);
91
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -070092 rds_inc_init(&rm->m_inc, conn, &conn->c_laddr);
Andy Groverd37c9352010-01-19 18:14:56 -080093 /* For the embedded inc. Matching put is in loop_inc_free() */
94 rds_message_addref(rm);
Andy Grover13796bf2009-02-24 15:30:24 +000095
Ka-Cheong Pooneee2fa62018-07-23 20:51:21 -070096 rds_recv_incoming(conn, &conn->c_laddr, &conn->c_faddr, &rm->m_inc,
Cong Wang6114eab2011-11-25 23:14:40 +080097 GFP_KERNEL);
Andy Grover13796bf2009-02-24 15:30:24 +000098
99 rds_send_drop_acked(conn, be64_to_cpu(rm->m_inc.i_hdr.h_sequence),
100 NULL);
101
102 rds_inc_put(&rm->m_inc);
Neil Horman60946282011-03-02 06:28:22 +0000103out:
104 return ret;
Andy Grover13796bf2009-02-24 15:30:24 +0000105}
106
Andy Groverd37c9352010-01-19 18:14:56 -0800107/*
108 * See rds_loop_xmit(). Since our inc is embedded in the rm, we
109 * make sure the rm lives at least until the inc is done.
110 */
111static void rds_loop_inc_free(struct rds_incoming *inc)
112{
Joshua Houghton5c3da572016-06-18 15:46:31 +0000113 struct rds_message *rm = container_of(inc, struct rds_message, m_inc);
114
115 rds_message_put(rm);
Andy Groverd37c9352010-01-19 18:14:56 -0800116}
117
Andy Grover13796bf2009-02-24 15:30:24 +0000118/* we need to at least give the thread something to succeed */
Sowmini Varadhan2da43c42016-06-30 16:11:15 -0700119static int rds_loop_recv_path(struct rds_conn_path *cp)
Andy Grover13796bf2009-02-24 15:30:24 +0000120{
121 return 0;
122}
123
124struct rds_loop_connection {
125 struct list_head loop_node;
126 struct rds_connection *conn;
127};
128
129/*
130 * Even the loopback transport needs to keep track of its connections,
131 * so it can call rds_conn_destroy() on them on exit. N.B. there are
132 * 1+ loopback addresses (127.*.*.*) so it's not a bug to have
133 * multiple loopback conns allocated, although rather useless.
134 */
135static int rds_loop_conn_alloc(struct rds_connection *conn, gfp_t gfp)
136{
137 struct rds_loop_connection *lc;
138 unsigned long flags;
139
Dan Carpenterf0229ea2012-03-21 20:44:09 +0000140 lc = kzalloc(sizeof(struct rds_loop_connection), gfp);
Andy Grover8690bfa2010-01-12 11:56:44 -0800141 if (!lc)
Andy Grover13796bf2009-02-24 15:30:24 +0000142 return -ENOMEM;
143
144 INIT_LIST_HEAD(&lc->loop_node);
145 lc->conn = conn;
146 conn->c_transport_data = lc;
147
148 spin_lock_irqsave(&loop_conns_lock, flags);
149 list_add_tail(&lc->loop_node, &loop_conns);
150 spin_unlock_irqrestore(&loop_conns_lock, flags);
151
152 return 0;
153}
154
155static void rds_loop_conn_free(void *arg)
156{
157 struct rds_loop_connection *lc = arg;
Pavel Emelyanov58c490b2010-11-02 01:52:05 +0000158 unsigned long flags;
159
Andy Grover13796bf2009-02-24 15:30:24 +0000160 rdsdebug("lc %p\n", lc);
Pavel Emelyanov58c490b2010-11-02 01:52:05 +0000161 spin_lock_irqsave(&loop_conns_lock, flags);
Andy Grover13796bf2009-02-24 15:30:24 +0000162 list_del(&lc->loop_node);
Pavel Emelyanov58c490b2010-11-02 01:52:05 +0000163 spin_unlock_irqrestore(&loop_conns_lock, flags);
Andy Grover13796bf2009-02-24 15:30:24 +0000164 kfree(lc);
165}
166
Sowmini Varadhanb04e8552016-06-30 16:11:16 -0700167static int rds_loop_conn_path_connect(struct rds_conn_path *cp)
Andy Grover13796bf2009-02-24 15:30:24 +0000168{
Sowmini Varadhanb04e8552016-06-30 16:11:16 -0700169 rds_connect_complete(cp->cp_conn);
Andy Grover13796bf2009-02-24 15:30:24 +0000170 return 0;
171}
172
Sowmini Varadhan226f7a72016-06-30 16:11:10 -0700173static void rds_loop_conn_path_shutdown(struct rds_conn_path *cp)
Andy Grover13796bf2009-02-24 15:30:24 +0000174{
175}
176
177void rds_loop_exit(void)
178{
179 struct rds_loop_connection *lc, *_lc;
180 LIST_HEAD(tmp_list);
181
Sowmini Varadhanc809195f2018-06-25 06:41:25 -0700182 rds_loop_set_unloading();
183 synchronize_rcu();
Andy Grover13796bf2009-02-24 15:30:24 +0000184 /* avoid calling conn_destroy with irqs off */
185 spin_lock_irq(&loop_conns_lock);
186 list_splice(&loop_conns, &tmp_list);
187 INIT_LIST_HEAD(&loop_conns);
188 spin_unlock_irq(&loop_conns_lock);
189
190 list_for_each_entry_safe(lc, _lc, &tmp_list, loop_node) {
191 WARN_ON(lc->conn->c_passive);
192 rds_conn_destroy(lc->conn);
193 }
194}
195
Sowmini Varadhanc809195f2018-06-25 06:41:25 -0700196static void rds_loop_kill_conns(struct net *net)
197{
198 struct rds_loop_connection *lc, *_lc;
199 LIST_HEAD(tmp_list);
200
201 spin_lock_irq(&loop_conns_lock);
202 list_for_each_entry_safe(lc, _lc, &loop_conns, loop_node) {
203 struct net *c_net = read_pnet(&lc->conn->c_net);
204
205 if (net != c_net)
206 continue;
207 list_move_tail(&lc->loop_node, &tmp_list);
208 }
209 spin_unlock_irq(&loop_conns_lock);
210
211 list_for_each_entry_safe(lc, _lc, &tmp_list, loop_node) {
212 WARN_ON(lc->conn->c_passive);
213 rds_conn_destroy(lc->conn);
214 }
215}
216
217static void __net_exit rds_loop_exit_net(struct net *net)
218{
219 rds_loop_kill_conns(net);
220}
221
222static struct pernet_operations rds_loop_net_ops = {
223 .exit = rds_loop_exit_net,
224};
225
226int rds_loop_net_init(void)
227{
228 return register_pernet_device(&rds_loop_net_ops);
229}
230
231void rds_loop_net_exit(void)
232{
233 unregister_pernet_device(&rds_loop_net_ops);
234}
235
Andy Grover13796bf2009-02-24 15:30:24 +0000236/*
237 * This is missing .xmit_* because loop doesn't go through generic
238 * rds_send_xmit() and doesn't call rds_recv_incoming(). .listen_stop and
239 * .laddr_check are missing because transport.c doesn't iterate over
240 * rds_loop_transport.
241 */
242struct rds_transport rds_loop_transport = {
243 .xmit = rds_loop_xmit,
Sowmini Varadhan2da43c42016-06-30 16:11:15 -0700244 .recv_path = rds_loop_recv_path,
Andy Grover13796bf2009-02-24 15:30:24 +0000245 .conn_alloc = rds_loop_conn_alloc,
246 .conn_free = rds_loop_conn_free,
Sowmini Varadhanb04e8552016-06-30 16:11:16 -0700247 .conn_path_connect = rds_loop_conn_path_connect,
Sowmini Varadhan226f7a72016-06-30 16:11:10 -0700248 .conn_path_shutdown = rds_loop_conn_path_shutdown,
Andy Grover13796bf2009-02-24 15:30:24 +0000249 .inc_copy_to_user = rds_message_inc_copy_to_user,
Andy Groverd37c9352010-01-19 18:14:56 -0800250 .inc_free = rds_loop_inc_free,
Andy Grover13796bf2009-02-24 15:30:24 +0000251 .t_name = "loopback",
Santosh Shilimkarf1693c62018-06-14 11:52:34 -0700252 .t_type = RDS_TRANS_LOOP,
Sowmini Varadhanc809195f2018-06-25 06:41:25 -0700253 .t_unloading = rds_loop_is_unloading,
Andy Grover13796bf2009-02-24 15:30:24 +0000254};