blob: 6b12b68541ae96fb8be76e72cb8d0e6f8c89abee [file] [log] [blame]
Andy Grover13796bf2009-02-24 15:30:24 +00001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Andy Grover13796bf2009-02-24 15:30:24 +000035#include <linux/in.h>
36
37#include "rds.h"
38#include "loop.h"
39
40static DEFINE_SPINLOCK(loop_conns_lock);
41static LIST_HEAD(loop_conns);
42
43/*
44 * This 'loopback' transport is a special case for flows that originate
45 * and terminate on the same machine.
46 *
47 * Connection build-up notices if the destination address is thought of
48 * as a local address by a transport. At that time it decides to use the
49 * loopback transport instead of the bound transport of the sending socket.
50 *
51 * The loopback transport's sending path just hands the sent rds_message
52 * straight to the receiving path via an embedded rds_incoming.
53 */
54
55/*
56 * Usually a message transits both the sender and receiver's conns as it
57 * flows to the receiver. In the loopback case, though, the receive path
58 * is handed the sending conn so the sense of the addresses is reversed.
59 */
60static int rds_loop_xmit(struct rds_connection *conn, struct rds_message *rm,
61 unsigned int hdr_off, unsigned int sg,
62 unsigned int off)
63{
Neil Horman60946282011-03-02 06:28:22 +000064 struct scatterlist *sgp = &rm->data.op_sg[sg];
65 int ret = sizeof(struct rds_header) +
66 be32_to_cpu(rm->m_inc.i_hdr.h_len);
67
Andy Grover77dd5502010-03-22 15:22:04 -070068 /* Do not send cong updates to loopback */
69 if (rm->m_inc.i_hdr.h_flags & RDS_FLAG_CONG_BITMAP) {
70 rds_cong_map_updated(conn->c_fcong, ~(u64) 0);
Neil Horman60946282011-03-02 06:28:22 +000071 ret = min_t(int, ret, sgp->length - conn->c_xmit_data_off);
72 goto out;
Andy Grover77dd5502010-03-22 15:22:04 -070073 }
74
Andy Grover13796bf2009-02-24 15:30:24 +000075 BUG_ON(hdr_off || sg || off);
76
77 rds_inc_init(&rm->m_inc, conn, conn->c_laddr);
Andy Groverd37c9352010-01-19 18:14:56 -080078 /* For the embedded inc. Matching put is in loop_inc_free() */
79 rds_message_addref(rm);
Andy Grover13796bf2009-02-24 15:30:24 +000080
81 rds_recv_incoming(conn, conn->c_laddr, conn->c_faddr, &rm->m_inc,
Cong Wang6114eab2011-11-25 23:14:40 +080082 GFP_KERNEL);
Andy Grover13796bf2009-02-24 15:30:24 +000083
84 rds_send_drop_acked(conn, be64_to_cpu(rm->m_inc.i_hdr.h_sequence),
85 NULL);
86
87 rds_inc_put(&rm->m_inc);
Neil Horman60946282011-03-02 06:28:22 +000088out:
89 return ret;
Andy Grover13796bf2009-02-24 15:30:24 +000090}
91
Andy Groverd37c9352010-01-19 18:14:56 -080092/*
93 * See rds_loop_xmit(). Since our inc is embedded in the rm, we
94 * make sure the rm lives at least until the inc is done.
95 */
96static void rds_loop_inc_free(struct rds_incoming *inc)
97{
98 struct rds_message *rm = container_of(inc, struct rds_message, m_inc);
99 rds_message_put(rm);
100}
101
Andy Grover13796bf2009-02-24 15:30:24 +0000102/* we need to at least give the thread something to succeed */
103static int rds_loop_recv(struct rds_connection *conn)
104{
105 return 0;
106}
107
108struct rds_loop_connection {
109 struct list_head loop_node;
110 struct rds_connection *conn;
111};
112
113/*
114 * Even the loopback transport needs to keep track of its connections,
115 * so it can call rds_conn_destroy() on them on exit. N.B. there are
116 * 1+ loopback addresses (127.*.*.*) so it's not a bug to have
117 * multiple loopback conns allocated, although rather useless.
118 */
119static int rds_loop_conn_alloc(struct rds_connection *conn, gfp_t gfp)
120{
121 struct rds_loop_connection *lc;
122 unsigned long flags;
123
Dan Carpenterf0229ea2012-03-21 20:44:09 +0000124 lc = kzalloc(sizeof(struct rds_loop_connection), gfp);
Andy Grover8690bfa2010-01-12 11:56:44 -0800125 if (!lc)
Andy Grover13796bf2009-02-24 15:30:24 +0000126 return -ENOMEM;
127
128 INIT_LIST_HEAD(&lc->loop_node);
129 lc->conn = conn;
130 conn->c_transport_data = lc;
131
132 spin_lock_irqsave(&loop_conns_lock, flags);
133 list_add_tail(&lc->loop_node, &loop_conns);
134 spin_unlock_irqrestore(&loop_conns_lock, flags);
135
136 return 0;
137}
138
139static void rds_loop_conn_free(void *arg)
140{
141 struct rds_loop_connection *lc = arg;
Pavel Emelyanov58c490b2010-11-02 01:52:05 +0000142 unsigned long flags;
143
Andy Grover13796bf2009-02-24 15:30:24 +0000144 rdsdebug("lc %p\n", lc);
Pavel Emelyanov58c490b2010-11-02 01:52:05 +0000145 spin_lock_irqsave(&loop_conns_lock, flags);
Andy Grover13796bf2009-02-24 15:30:24 +0000146 list_del(&lc->loop_node);
Pavel Emelyanov58c490b2010-11-02 01:52:05 +0000147 spin_unlock_irqrestore(&loop_conns_lock, flags);
Andy Grover13796bf2009-02-24 15:30:24 +0000148 kfree(lc);
149}
150
151static int rds_loop_conn_connect(struct rds_connection *conn)
152{
153 rds_connect_complete(conn);
154 return 0;
155}
156
157static void rds_loop_conn_shutdown(struct rds_connection *conn)
158{
159}
160
161void rds_loop_exit(void)
162{
163 struct rds_loop_connection *lc, *_lc;
164 LIST_HEAD(tmp_list);
165
166 /* avoid calling conn_destroy with irqs off */
167 spin_lock_irq(&loop_conns_lock);
168 list_splice(&loop_conns, &tmp_list);
169 INIT_LIST_HEAD(&loop_conns);
170 spin_unlock_irq(&loop_conns_lock);
171
172 list_for_each_entry_safe(lc, _lc, &tmp_list, loop_node) {
173 WARN_ON(lc->conn->c_passive);
174 rds_conn_destroy(lc->conn);
175 }
176}
177
178/*
179 * This is missing .xmit_* because loop doesn't go through generic
180 * rds_send_xmit() and doesn't call rds_recv_incoming(). .listen_stop and
181 * .laddr_check are missing because transport.c doesn't iterate over
182 * rds_loop_transport.
183 */
184struct rds_transport rds_loop_transport = {
185 .xmit = rds_loop_xmit,
Andy Grover13796bf2009-02-24 15:30:24 +0000186 .recv = rds_loop_recv,
187 .conn_alloc = rds_loop_conn_alloc,
188 .conn_free = rds_loop_conn_free,
189 .conn_connect = rds_loop_conn_connect,
190 .conn_shutdown = rds_loop_conn_shutdown,
191 .inc_copy_to_user = rds_message_inc_copy_to_user,
Andy Groverd37c9352010-01-19 18:14:56 -0800192 .inc_free = rds_loop_inc_free,
Andy Grover13796bf2009-02-24 15:30:24 +0000193 .t_name = "loopback",
194};