xref: /illumos-gate/usr/src/uts/common/io/ib/clients/rdsv3/loop.c (revision 02b4e56ca3a4e4a4fe9e52fca9c2972101f0e57f)
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 {
136 	struct list_node loop_node;
137 	struct rdsv3_connection *conn;
138 };
139 
140 /*
141  * Even the loopback transport needs to keep track of its connections,
142  * so it can call rdsv3_conn_destroy() on them on exit. N.B. there are
143  * 1+ loopback addresses (127.*.*.*) so it's not a bug to have
144  * multiple loopback conns allocated, although rather useless.
145  */
146 /* ARGSUSED */
147 static int
148 rdsv3_loop_conn_alloc(struct rdsv3_connection *conn, int gfp)
149 {
150 	struct rdsv3_loop_connection *lc;
151 
152 	RDSV3_DPRINTF4("rdsv3_loop_conn_alloc", "Enter(conn: %p)", conn);
153 
154 	lc = kmem_zalloc(sizeof (struct rdsv3_loop_connection), KM_NOSLEEP);
155 	if (lc == NULL)
156 		return (-ENOMEM);
157 
158 	list_link_init(&lc->loop_node);
159 	lc->conn = conn;
160 	conn->c_transport_data = lc;
161 
162 	mutex_enter(&loop_conns_lock);
163 	list_insert_tail(&loop_conns, lc);
164 	mutex_exit(&loop_conns_lock);
165 
166 	RDSV3_DPRINTF4("rdsv3_loop_conn_alloc", "Return(conn: %p)", conn);
167 
168 	return (0);
169 }
170 
171 static void
172 rdsv3_loop_conn_free(void *arg)
173 {
174 	struct rdsv3_loop_connection *lc = arg;
175 	RDSV3_DPRINTF5("rdsv3_loop_conn_free", "lc %p\n", lc);
176 	list_remove_node(&lc->loop_node);
177 	kmem_free(lc, sizeof (struct rdsv3_loop_connection));
178 }
179 
180 static int
181 rdsv3_loop_conn_connect(struct rdsv3_connection *conn)
182 {
183 	rdsv3_connect_complete(conn);
184 	return (0);
185 }
186 
187 /* ARGSUSED */
188 static void
189 rdsv3_loop_conn_shutdown(struct rdsv3_connection *conn)
190 {
191 }
192 
193 void
194 rdsv3_loop_exit(void)
195 {
196 	struct rdsv3_loop_connection *lc, *_lc;
197 	list_t tmp_list;
198 
199 	RDSV3_DPRINTF4("rdsv3_loop_exit", "Enter");
200 
201 	list_create(&tmp_list, sizeof (struct rdsv3_loop_connection),
202 	    offsetof(struct rdsv3_loop_connection, loop_node));
203 
204 	/* avoid calling conn_destroy with irqs off */
205 	mutex_enter(&loop_conns_lock);
206 	list_splice(&loop_conns, &tmp_list);
207 	mutex_exit(&loop_conns_lock);
208 
209 	RDSV3_FOR_EACH_LIST_NODE_SAFE(lc, _lc, &tmp_list, loop_node) {
210 		ASSERT(!lc->conn->c_passive);
211 		rdsv3_conn_destroy(lc->conn);
212 	}
213 
214 	list_destroy(&loop_conns);
215 	mutex_destroy(&loop_conns_lock);
216 
217 	RDSV3_DPRINTF4("rdsv3_loop_exit", "Return");
218 }
219 
220 /*
221  * This is missing .xmit_* because loop doesn't go through generic
222  * rdsv3_send_xmit() and doesn't call rdsv3_recv_incoming().  .listen_stop and
223  * .laddr_check are missing because transport.c doesn't iterate over
224  * rdsv3_loop_transport.
225  */
226 #ifndef __lock_lint
227 struct rdsv3_transport rdsv3_loop_transport = {
228 	.xmit			= rdsv3_loop_xmit,
229 	.xmit_cong_map		= rdsv3_loop_xmit_cong_map,
230 	.recv			= rdsv3_loop_recv,
231 	.conn_alloc		= rdsv3_loop_conn_alloc,
232 	.conn_free		= rdsv3_loop_conn_free,
233 	.conn_connect		= rdsv3_loop_conn_connect,
234 	.conn_shutdown		= rdsv3_loop_conn_shutdown,
235 	.inc_copy_to_user	= rdsv3_message_inc_copy_to_user,
236 	.inc_purge		= rdsv3_message_inc_purge,
237 	.inc_free		= rdsv3_message_inc_free,
238 	.t_name			= "loopback",
239 };
240 #else
241 struct rdsv3_transport rdsv3_loop_transport;
242 #endif
243