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