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 	/* Do not send cong updates to loopback */
89 	if (rm->m_inc.i_hdr.h_flags & RDSV3_FLAG_CONG_BITMAP) {
90 		rdsv3_cong_map_updated(conn->c_fcong, ~(uint64_t)0);
91 		return (sizeof (struct rdsv3_header) + RDSV3_CONG_MAP_BYTES);
92 	}
93 	ASSERT(!(hdr_off || sg || off));
94 
95 	RDSV3_DPRINTF4("rdsv3_loop_xmit", "Enter(conn: %p, rm: %p)", conn, rm);
96 
97 	rdsv3_inc_init(&rm->m_inc, conn, conn->c_laddr);
98 	/* For the embedded inc. Matching put is in loop_inc_free() */
99 	rdsv3_message_addref(rm);
100 
101 	rdsv3_recv_incoming(conn, conn->c_laddr, conn->c_faddr, &rm->m_inc,
102 	    KM_NOSLEEP);
103 
104 	rdsv3_send_drop_acked(conn, ntohll(rm->m_inc.i_hdr.h_sequence),
105 	    NULL);
106 
107 	rdsv3_inc_put(&rm->m_inc);
108 
109 	RDSV3_DPRINTF4("rdsv3_loop_xmit", "Return(conn: %p, rm: %p)", conn, rm);
110 
111 	return (sizeof (struct rdsv3_header) +
112 	    ntohl(rm->m_inc.i_hdr.h_len));
113 }
114 
115 /*
116  * See rds_loop_xmit(). Since our inc is embedded in the rm, we
117  * make sure the rm lives at least until the inc is done.
118  */
119 static void
120 rdsv3_loop_inc_free(struct rdsv3_incoming *inc)
121 {
122 	struct rdsv3_message *rm = container_of(inc, struct rdsv3_message,
123 	    m_inc);
124 	rdsv3_message_put(rm);
125 }
126 
127 static int
128 rdsv3_loop_xmit_cong_map(struct rdsv3_connection *conn,
129     struct rdsv3_cong_map *map,
130     unsigned long offset)
131 {
132 	RDSV3_DPRINTF4("rdsv3_loop_xmit_cong_map", "Enter(conn: %p)", conn);
133 
134 	ASSERT(!offset);
135 	ASSERT(map == conn->c_lcong);
136 
137 	rdsv3_cong_map_updated(conn->c_fcong, ~(uint64_t)0);
138 
139 	RDSV3_DPRINTF4("rdsv3_loop_xmit_cong_map", "Return(conn: %p)", conn);
140 
141 	return (sizeof (struct rdsv3_header) + RDSV3_CONG_MAP_BYTES);
142 }
143 
144 /* we need to at least give the thread something to succeed */
145 /* ARGSUSED */
146 static int
147 rdsv3_loop_recv(struct rdsv3_connection *conn)
148 {
149 	return (0);
150 }
151 
152 struct rdsv3_loop_connection {
153 	struct list_node loop_node;
154 	struct rdsv3_connection *conn;
155 };
156 
157 /*
158  * Even the loopback transport needs to keep track of its connections,
159  * so it can call rdsv3_conn_destroy() on them on exit. N.B. there are
160  * 1+ loopback addresses (127.*.*.*) so it's not a bug to have
161  * multiple loopback conns allocated, although rather useless.
162  */
163 /* ARGSUSED */
164 static int
165 rdsv3_loop_conn_alloc(struct rdsv3_connection *conn, int gfp)
166 {
167 	struct rdsv3_loop_connection *lc;
168 
169 	RDSV3_DPRINTF4("rdsv3_loop_conn_alloc", "Enter(conn: %p)", conn);
170 
171 	lc = kmem_zalloc(sizeof (struct rdsv3_loop_connection), KM_NOSLEEP);
172 	if (!lc)
173 		return (-ENOMEM);
174 
175 	list_link_init(&lc->loop_node);
176 	lc->conn = conn;
177 	conn->c_transport_data = lc;
178 
179 	mutex_enter(&loop_conns_lock);
180 	list_insert_tail(&loop_conns, lc);
181 	mutex_exit(&loop_conns_lock);
182 
183 	RDSV3_DPRINTF4("rdsv3_loop_conn_alloc", "Return(conn: %p)", conn);
184 
185 	return (0);
186 }
187 
188 static void
189 rdsv3_loop_conn_free(void *arg)
190 {
191 	struct rdsv3_loop_connection *lc = arg;
192 	RDSV3_DPRINTF5("rdsv3_loop_conn_free", "lc %p\n", lc);
193 	list_remove_node(&lc->loop_node);
194 	kmem_free(lc, sizeof (struct rdsv3_loop_connection));
195 }
196 
197 static int
198 rdsv3_loop_conn_connect(struct rdsv3_connection *conn)
199 {
200 	rdsv3_connect_complete(conn);
201 	return (0);
202 }
203 
204 /* ARGSUSED */
205 static void
206 rdsv3_loop_conn_shutdown(struct rdsv3_connection *conn)
207 {
208 }
209 
210 void
211 rdsv3_loop_exit(void)
212 {
213 	struct rdsv3_loop_connection *lc, *_lc;
214 	list_t tmp_list;
215 
216 	RDSV3_DPRINTF4("rdsv3_loop_exit", "Enter");
217 
218 	list_create(&tmp_list, sizeof (struct rdsv3_loop_connection),
219 	    offsetof(struct rdsv3_loop_connection, loop_node));
220 
221 	/* avoid calling conn_destroy with irqs off */
222 	mutex_enter(&loop_conns_lock);
223 	list_splice(&loop_conns, &tmp_list);
224 	mutex_exit(&loop_conns_lock);
225 
226 	RDSV3_FOR_EACH_LIST_NODE_SAFE(lc, _lc, &tmp_list, loop_node) {
227 		ASSERT(!lc->conn->c_passive);
228 		rdsv3_conn_destroy(lc->conn);
229 	}
230 
231 	list_destroy(&loop_conns);
232 	mutex_destroy(&loop_conns_lock);
233 
234 	RDSV3_DPRINTF4("rdsv3_loop_exit", "Return");
235 }
236 
237 /*
238  * This is missing .xmit_* because loop doesn't go through generic
239  * rdsv3_send_xmit() and doesn't call rdsv3_recv_incoming().  .listen_stop and
240  * .laddr_check are missing because transport.c doesn't iterate over
241  * rdsv3_loop_transport.
242  */
243 #ifndef __lock_lint
244 struct rdsv3_transport rdsv3_loop_transport = {
245 	.xmit			= rdsv3_loop_xmit,
246 	.xmit_cong_map		= rdsv3_loop_xmit_cong_map,
247 	.recv			= rdsv3_loop_recv,
248 	.conn_alloc		= rdsv3_loop_conn_alloc,
249 	.conn_free		= rdsv3_loop_conn_free,
250 	.conn_connect		= rdsv3_loop_conn_connect,
251 	.conn_shutdown		= rdsv3_loop_conn_shutdown,
252 	.inc_copy_to_user	= rdsv3_message_inc_copy_to_user,
253 	.inc_free		= rdsv3_loop_inc_free,
254 	.t_name			= "loopback",
255 };
256 #else
257 struct rdsv3_transport rdsv3_loop_transport;
258 #endif
259