1 /*
2 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 */
4
5 /*
6 * This file contains code imported from the OFED rds source file loop.c
7 * Oracle elects to have and use the contents of loop.c under and governed
8 * by the OpenIB.org BSD license (see below for full license text). However,
9 * the following notice accompanied the original version of this file:
10 */
11
12 /*
13 * Copyright (c) 2006 Oracle. All rights reserved.
14 *
15 * This software is available to you under a choice of one of two
16 * licenses. You may choose to be licensed under the terms of the GNU
17 * General Public License (GPL) Version 2, available from the file
18 * COPYING in the main directory of this source tree, or the
19 * OpenIB.org BSD license below:
20 *
21 * Redistribution and use in source and binary forms, with or
22 * without modification, are permitted provided that the following
23 * conditions are met:
24 *
25 * - Redistributions of source code must retain the above
26 * copyright notice, this list of conditions and the following
27 * disclaimer.
28 *
29 * - Redistributions in binary form must reproduce the above
30 * copyright notice, this list of conditions and the following
31 * disclaimer in the documentation and/or other materials
32 * provided with the distribution.
33 *
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
35 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
36 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
37 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
38 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
39 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
40 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41 * SOFTWARE.
42 *
43 */
44 #include <sys/rds.h>
45
46 #include <sys/ib/clients/rdsv3/rdsv3.h>
47 #include <sys/ib/clients/rdsv3/loop.h>
48 #include <sys/ib/clients/rdsv3/rdsv3_debug.h>
49
50 kmutex_t loop_conns_lock;
51 list_t loop_conns;
52
53 /*
54 * This 'loopback' transport is a special case for flows that originate
55 * and terminate on the same machine.
56 *
57 * Connection build-up notices if the destination address is thought of
58 * as a local address by a transport. At that time it decides to use the
59 * loopback transport instead of the bound transport of the sending socket.
60 *
61 * The loopback transport's sending path just hands the sent rds_message
62 * straight to the receiving path via an embedded rds_incoming.
63 */
64
65 /*
66 * Usually a message transits both the sender and receiver's conns as it
67 * flows to the receiver. In the loopback case, though, the receive path
68 * is handed the sending conn so the sense of the addresses is reversed.
69 */
70 static int
rdsv3_loop_xmit(struct rdsv3_connection * conn,struct rdsv3_message * rm,unsigned int hdr_off,unsigned int sg,unsigned int off)71 rdsv3_loop_xmit(struct rdsv3_connection *conn, struct rdsv3_message *rm,
72 unsigned int hdr_off, unsigned int sg,
73 unsigned int off)
74 {
75 /* Do not send cong updates to loopback */
76 if (rm->m_inc.i_hdr.h_flags & RDSV3_FLAG_CONG_BITMAP) {
77 rdsv3_cong_map_updated(conn->c_fcong, ~(uint64_t)0);
78 return (sizeof (struct rdsv3_header) + RDSV3_CONG_MAP_BYTES);
79 }
80 ASSERT(!(hdr_off || sg || off));
81
82 RDSV3_DPRINTF4("rdsv3_loop_xmit", "Enter(conn: %p, rm: %p)", conn, rm);
83
84 rdsv3_inc_init(&rm->m_inc, conn, conn->c_laddr);
85 /* For the embedded inc. Matching put is in loop_inc_free() */
86 rdsv3_message_addref(rm);
87
88 rdsv3_recv_incoming(conn, conn->c_laddr, conn->c_faddr, &rm->m_inc,
89 KM_NOSLEEP);
90
91 rdsv3_send_drop_acked(conn, ntohll(rm->m_inc.i_hdr.h_sequence),
92 NULL);
93
94 rdsv3_inc_put(&rm->m_inc);
95
96 RDSV3_DPRINTF4("rdsv3_loop_xmit", "Return(conn: %p, rm: %p)", conn, rm);
97
98 return (sizeof (struct rdsv3_header) +
99 ntohl(rm->m_inc.i_hdr.h_len));
100 }
101
102 /*
103 * See rds_loop_xmit(). Since our inc is embedded in the rm, we
104 * make sure the rm lives at least until the inc is done.
105 */
106 static void
rdsv3_loop_inc_free(struct rdsv3_incoming * inc)107 rdsv3_loop_inc_free(struct rdsv3_incoming *inc)
108 {
109 struct rdsv3_message *rm = container_of(inc, struct rdsv3_message,
110 m_inc);
111 rdsv3_message_put(rm);
112 }
113
114 static int
rdsv3_loop_xmit_cong_map(struct rdsv3_connection * conn,struct rdsv3_cong_map * map,unsigned long offset)115 rdsv3_loop_xmit_cong_map(struct rdsv3_connection *conn,
116 struct rdsv3_cong_map *map,
117 unsigned long offset)
118 {
119 RDSV3_DPRINTF4("rdsv3_loop_xmit_cong_map", "Enter(conn: %p)", conn);
120
121 ASSERT(!offset);
122 ASSERT(map == conn->c_lcong);
123
124 rdsv3_cong_map_updated(conn->c_fcong, ~(uint64_t)0);
125
126 RDSV3_DPRINTF4("rdsv3_loop_xmit_cong_map", "Return(conn: %p)", conn);
127
128 return (sizeof (struct rdsv3_header) + RDSV3_CONG_MAP_BYTES);
129 }
130
131 /* we need to at least give the thread something to succeed */
132 /* ARGSUSED */
133 static int
rdsv3_loop_recv(struct rdsv3_connection * conn)134 rdsv3_loop_recv(struct rdsv3_connection *conn)
135 {
136 return (0);
137 }
138
139 struct rdsv3_loop_connection {
140 struct list_node loop_node;
141 struct rdsv3_connection *conn;
142 };
143
144 /*
145 * Even the loopback transport needs to keep track of its connections,
146 * so it can call rdsv3_conn_destroy() on them on exit. N.B. there are
147 * 1+ loopback addresses (127.*.*.*) so it's not a bug to have
148 * multiple loopback conns allocated, although rather useless.
149 */
150 /* ARGSUSED */
151 static int
rdsv3_loop_conn_alloc(struct rdsv3_connection * conn,int gfp)152 rdsv3_loop_conn_alloc(struct rdsv3_connection *conn, int gfp)
153 {
154 struct rdsv3_loop_connection *lc;
155
156 RDSV3_DPRINTF4("rdsv3_loop_conn_alloc", "Enter(conn: %p)", conn);
157
158 lc = kmem_zalloc(sizeof (struct rdsv3_loop_connection), KM_NOSLEEP);
159 if (!lc)
160 return (-ENOMEM);
161
162 list_link_init(&lc->loop_node);
163 lc->conn = conn;
164 conn->c_transport_data = lc;
165
166 mutex_enter(&loop_conns_lock);
167 list_insert_tail(&loop_conns, lc);
168 mutex_exit(&loop_conns_lock);
169
170 RDSV3_DPRINTF4("rdsv3_loop_conn_alloc", "Return(conn: %p)", conn);
171
172 return (0);
173 }
174
175 static void
rdsv3_loop_conn_free(void * arg)176 rdsv3_loop_conn_free(void *arg)
177 {
178 struct rdsv3_loop_connection *lc = arg;
179 RDSV3_DPRINTF5("rdsv3_loop_conn_free", "lc %p\n", lc);
180 list_remove_node(&lc->loop_node);
181 kmem_free(lc, sizeof (struct rdsv3_loop_connection));
182 }
183
184 static int
rdsv3_loop_conn_connect(struct rdsv3_connection * conn)185 rdsv3_loop_conn_connect(struct rdsv3_connection *conn)
186 {
187 rdsv3_connect_complete(conn);
188 return (0);
189 }
190
191 /* ARGSUSED */
192 static void
rdsv3_loop_conn_shutdown(struct rdsv3_connection * conn)193 rdsv3_loop_conn_shutdown(struct rdsv3_connection *conn)
194 {
195 }
196
197 void
rdsv3_loop_exit(void)198 rdsv3_loop_exit(void)
199 {
200 struct rdsv3_loop_connection *lc, *_lc;
201 list_t tmp_list;
202
203 RDSV3_DPRINTF4("rdsv3_loop_exit", "Enter");
204
205 list_create(&tmp_list, sizeof (struct rdsv3_loop_connection),
206 offsetof(struct rdsv3_loop_connection, loop_node));
207
208 /* avoid calling conn_destroy with irqs off */
209 mutex_enter(&loop_conns_lock);
210 list_splice(&loop_conns, &tmp_list);
211 mutex_exit(&loop_conns_lock);
212
213 RDSV3_FOR_EACH_LIST_NODE_SAFE(lc, _lc, &tmp_list, loop_node) {
214 ASSERT(!lc->conn->c_passive);
215 rdsv3_conn_destroy(lc->conn);
216 }
217
218 list_destroy(&loop_conns);
219 mutex_destroy(&loop_conns_lock);
220
221 RDSV3_DPRINTF4("rdsv3_loop_exit", "Return");
222 }
223
224 /*
225 * This is missing .xmit_* because loop doesn't go through generic
226 * rdsv3_send_xmit() and doesn't call rdsv3_recv_incoming(). .listen_stop and
227 * .laddr_check are missing because transport.c doesn't iterate over
228 * rdsv3_loop_transport.
229 */
230 #ifndef __lock_lint
231 struct rdsv3_transport rdsv3_loop_transport = {
232 .xmit = rdsv3_loop_xmit,
233 .xmit_cong_map = rdsv3_loop_xmit_cong_map,
234 .recv = rdsv3_loop_recv,
235 .conn_alloc = rdsv3_loop_conn_alloc,
236 .conn_free = rdsv3_loop_conn_free,
237 .conn_connect = rdsv3_loop_conn_connect,
238 .conn_shutdown = rdsv3_loop_conn_shutdown,
239 .inc_copy_to_user = rdsv3_message_inc_copy_to_user,
240 .inc_free = rdsv3_loop_inc_free,
241 .t_name = "loopback",
242 };
243 #else
244 struct rdsv3_transport rdsv3_loop_transport;
245 #endif
246