xref: /illumos-gate/usr/src/uts/common/io/ib/clients/rdsv3/loop.c (revision 8459c777fc1aaabb2f7dad05de1313aa169417cd)
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 #include <sys/containerof.h>
46 
47 #include <sys/ib/clients/rdsv3/rdsv3.h>
48 #include <sys/ib/clients/rdsv3/loop.h>
49 #include <sys/ib/clients/rdsv3/rdsv3_debug.h>
50 
51 kmutex_t loop_conns_lock;
52 list_t loop_conns;
53 
54 /*
55  * This 'loopback' transport is a special case for flows that originate
56  * and terminate on the same machine.
57  *
58  * Connection build-up notices if the destination address is thought of
59  * as a local address by a transport.  At that time it decides to use the
60  * loopback transport instead of the bound transport of the sending socket.
61  *
62  * The loopback transport's sending path just hands the sent rds_message
63  * straight to the receiving path via an embedded rds_incoming.
64  */
65 
66 /*
67  * Usually a message transits both the sender and receiver's conns as it
68  * flows to the receiver.  In the loopback case, though, the receive path
69  * is handed the sending conn so the sense of the addresses is reversed.
70  */
71 static int
72 rdsv3_loop_xmit(struct rdsv3_connection *conn, struct rdsv3_message *rm,
73     unsigned int hdr_off, unsigned int sg,
74     unsigned int off)
75 {
76 	/* Do not send cong updates to loopback */
77 	if (rm->m_inc.i_hdr.h_flags & RDSV3_FLAG_CONG_BITMAP) {
78 		rdsv3_cong_map_updated(conn->c_fcong, ~(uint64_t)0);
79 		return (sizeof (struct rdsv3_header) + RDSV3_CONG_MAP_BYTES);
80 	}
81 	ASSERT(!(hdr_off || sg || off));
82 
83 	RDSV3_DPRINTF4("rdsv3_loop_xmit", "Enter(conn: %p, rm: %p)", conn, rm);
84 
85 	rdsv3_inc_init(&rm->m_inc, conn, conn->c_laddr);
86 	/* For the embedded inc. Matching put is in loop_inc_free() */
87 	rdsv3_message_addref(rm);
88 
89 	rdsv3_recv_incoming(conn, conn->c_laddr, conn->c_faddr, &rm->m_inc,
90 	    KM_NOSLEEP);
91 
92 	rdsv3_send_drop_acked(conn, ntohll(rm->m_inc.i_hdr.h_sequence),
93 	    NULL);
94 
95 	rdsv3_inc_put(&rm->m_inc);
96 
97 	RDSV3_DPRINTF4("rdsv3_loop_xmit", "Return(conn: %p, rm: %p)", conn, rm);
98 
99 	return (sizeof (struct rdsv3_header) +
100 	    ntohl(rm->m_inc.i_hdr.h_len));
101 }
102 
103 /*
104  * See rds_loop_xmit(). Since our inc is embedded in the rm, we
105  * make sure the rm lives at least until the inc is done.
106  */
107 static void
108 rdsv3_loop_inc_free(struct rdsv3_incoming *inc)
109 {
110 	struct rdsv3_message *rm = __containerof(inc, struct rdsv3_message,
111 	    m_inc);
112 	rdsv3_message_put(rm);
113 }
114 
115 static int
116 rdsv3_loop_xmit_cong_map(struct rdsv3_connection *conn,
117     struct rdsv3_cong_map *map,
118     unsigned long offset)
119 {
120 	RDSV3_DPRINTF4("rdsv3_loop_xmit_cong_map", "Enter(conn: %p)", conn);
121 
122 	ASSERT(!offset);
123 	ASSERT(map == conn->c_lcong);
124 
125 	rdsv3_cong_map_updated(conn->c_fcong, ~(uint64_t)0);
126 
127 	RDSV3_DPRINTF4("rdsv3_loop_xmit_cong_map", "Return(conn: %p)", conn);
128 
129 	return (sizeof (struct rdsv3_header) + RDSV3_CONG_MAP_BYTES);
130 }
131 
132 /* we need to at least give the thread something to succeed */
133 /* ARGSUSED */
134 static int
135 rdsv3_loop_recv(struct rdsv3_connection *conn)
136 {
137 	return (0);
138 }
139 
140 struct rdsv3_loop_connection {
141 	struct list_node loop_node;
142 	struct rdsv3_connection *conn;
143 };
144 
145 /*
146  * Even the loopback transport needs to keep track of its connections,
147  * so it can call rdsv3_conn_destroy() on them on exit. N.B. there are
148  * 1+ loopback addresses (127.*.*.*) so it's not a bug to have
149  * multiple loopback conns allocated, although rather useless.
150  */
151 /* ARGSUSED */
152 static int
153 rdsv3_loop_conn_alloc(struct rdsv3_connection *conn, int gfp)
154 {
155 	struct rdsv3_loop_connection *lc;
156 
157 	RDSV3_DPRINTF4("rdsv3_loop_conn_alloc", "Enter(conn: %p)", conn);
158 
159 	lc = kmem_zalloc(sizeof (struct rdsv3_loop_connection), KM_NOSLEEP);
160 	if (!lc)
161 		return (-ENOMEM);
162 
163 	list_link_init(&lc->loop_node);
164 	lc->conn = conn;
165 	conn->c_transport_data = lc;
166 
167 	mutex_enter(&loop_conns_lock);
168 	list_insert_tail(&loop_conns, lc);
169 	mutex_exit(&loop_conns_lock);
170 
171 	RDSV3_DPRINTF4("rdsv3_loop_conn_alloc", "Return(conn: %p)", conn);
172 
173 	return (0);
174 }
175 
176 static void
177 rdsv3_loop_conn_free(void *arg)
178 {
179 	struct rdsv3_loop_connection *lc = arg;
180 	RDSV3_DPRINTF5("rdsv3_loop_conn_free", "lc %p\n", lc);
181 	list_remove_node(&lc->loop_node);
182 	kmem_free(lc, sizeof (struct rdsv3_loop_connection));
183 }
184 
185 static int
186 rdsv3_loop_conn_connect(struct rdsv3_connection *conn)
187 {
188 	rdsv3_connect_complete(conn);
189 	return (0);
190 }
191 
192 /* ARGSUSED */
193 static void
194 rdsv3_loop_conn_shutdown(struct rdsv3_connection *conn)
195 {
196 }
197 
198 void
199 rdsv3_loop_exit(void)
200 {
201 	struct rdsv3_loop_connection *lc, *_lc;
202 	list_t tmp_list;
203 
204 	RDSV3_DPRINTF4("rdsv3_loop_exit", "Enter");
205 
206 	list_create(&tmp_list, sizeof (struct rdsv3_loop_connection),
207 	    offsetof(struct rdsv3_loop_connection, loop_node));
208 
209 	/* avoid calling conn_destroy with irqs off */
210 	mutex_enter(&loop_conns_lock);
211 	list_splice(&loop_conns, &tmp_list);
212 	mutex_exit(&loop_conns_lock);
213 
214 	RDSV3_FOR_EACH_LIST_NODE_SAFE(lc, _lc, &tmp_list, loop_node) {
215 		ASSERT(!lc->conn->c_passive);
216 		rdsv3_conn_destroy(lc->conn);
217 	}
218 
219 	list_destroy(&loop_conns);
220 	mutex_destroy(&loop_conns_lock);
221 
222 	RDSV3_DPRINTF4("rdsv3_loop_exit", "Return");
223 }
224 
225 /*
226  * This is missing .xmit_* because loop doesn't go through generic
227  * rdsv3_send_xmit() and doesn't call rdsv3_recv_incoming().  .listen_stop and
228  * .laddr_check are missing because transport.c doesn't iterate over
229  * rdsv3_loop_transport.
230  */
231 #ifndef __lock_lint
232 struct rdsv3_transport rdsv3_loop_transport = {
233 	.xmit			= rdsv3_loop_xmit,
234 	.xmit_cong_map		= rdsv3_loop_xmit_cong_map,
235 	.recv			= rdsv3_loop_recv,
236 	.conn_alloc		= rdsv3_loop_conn_alloc,
237 	.conn_free		= rdsv3_loop_conn_free,
238 	.conn_connect		= rdsv3_loop_conn_connect,
239 	.conn_shutdown		= rdsv3_loop_conn_shutdown,
240 	.inc_copy_to_user	= rdsv3_message_inc_copy_to_user,
241 	.inc_free		= rdsv3_loop_inc_free,
242 	.t_name			= "loopback",
243 };
244 #else
245 struct rdsv3_transport rdsv3_loop_transport;
246 #endif
247