xref: /freebsd/sys/dev/iscsi/icl_soft_proxy.c (revision a2f733abcff64628b7771a47089628b7327a88bd)
1 /*-
2  * Copyright (c) 2012 The FreeBSD Foundation
3  *
4  * This software was developed by Edward Tomasz Napierala under sponsorship
5  * from the FreeBSD Foundation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 /*-
30  * Copyright (c) 1982, 1986, 1989, 1990, 1993
31  *	The Regents of the University of California.  All rights reserved.
32  *
33  * sendfile(2) and related extensions:
34  * Copyright (c) 1998, David Greenman. All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  */
60 
61 /*
62  * iSCSI Common Layer, kernel proxy part.
63  */
64 
65 #ifdef ICL_KERNEL_PROXY
66 
67 #include <sys/cdefs.h>
68 #include <sys/param.h>
69 #include <sys/capsicum.h>
70 #include <sys/condvar.h>
71 #include <sys/conf.h>
72 #include <sys/lock.h>
73 #include <sys/kernel.h>
74 #include <sys/kthread.h>
75 #include <sys/malloc.h>
76 #include <sys/mutex.h>
77 #include <sys/proc.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/sx.h>
81 #include <sys/systm.h>
82 #include <netinet/in.h>
83 #include <netinet/tcp.h>
84 
85 #include <dev/iscsi/icl.h>
86 
87 struct icl_listen_sock {
88 	TAILQ_ENTRY(icl_listen_sock)	ils_next;
89 	struct icl_listen		*ils_listen;
90 	struct socket			*ils_socket;
91 	bool				ils_running;
92 	int				ils_id;
93 };
94 
95 struct icl_listen	{
96 	TAILQ_HEAD(, icl_listen_sock)	il_sockets;
97 	struct sx			il_lock;
98 	void				(*il_accept)(struct socket *,
99 					    struct sockaddr *, int);
100 };
101 
102 static MALLOC_DEFINE(M_ICL_PROXY, "ICL_PROXY", "iSCSI common layer proxy");
103 
104 int
105 icl_soft_proxy_connect(struct icl_conn *ic, int domain, int socktype,
106     int protocol, struct sockaddr *from_sa, struct sockaddr *to_sa)
107 {
108 	struct socket *so;
109 	int error;
110 
111 	error = socreate(domain, &so, socktype, protocol,
112 	    curthread->td_ucred, curthread);
113 	if (error != 0)
114 		return (error);
115 
116 	if (from_sa != NULL) {
117 		error = sobind(so, from_sa, curthread);
118 		if (error != 0) {
119 			soclose(so);
120 			return (error);
121 		}
122 	}
123 
124 	error = soconnect(so, to_sa, curthread);
125 	if (error != 0) {
126 		soclose(so);
127 		return (error);
128 	}
129 
130 	SOCK_LOCK(so);
131 	while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
132 		error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
133 		    "icl_connect", 0);
134 		if (error)
135 			break;
136 	}
137 	if (error == 0) {
138 		error = so->so_error;
139 		so->so_error = 0;
140 	}
141 	SOCK_UNLOCK(so);
142 
143 	if (error != 0) {
144 		soclose(so);
145 		return (error);
146 	}
147 
148 	error = icl_soft_handoff_sock(ic, so);
149 	if (error != 0)
150 		soclose(so);
151 
152 	return (error);
153 }
154 
155 struct icl_listen *
156 icl_listen_new(void (*accept_cb)(struct socket *, struct sockaddr *, int))
157 {
158 	struct icl_listen *il;
159 
160 	il = malloc(sizeof(*il), M_ICL_PROXY, M_ZERO | M_WAITOK);
161 	TAILQ_INIT(&il->il_sockets);
162 	sx_init(&il->il_lock, "icl_listen");
163 	il->il_accept = accept_cb;
164 
165 	return (il);
166 }
167 
168 void
169 icl_listen_free(struct icl_listen *il)
170 {
171 	struct icl_listen_sock *ils;
172 	sbintime_t sbt, pr;
173 
174 	sx_xlock(&il->il_lock);
175 	while (!TAILQ_EMPTY(&il->il_sockets)) {
176 		ils = TAILQ_FIRST(&il->il_sockets);
177 		while (ils->ils_running) {
178 			ICL_DEBUG("waiting for accept thread to terminate");
179 			sx_xunlock(&il->il_lock);
180 			SOLISTEN_LOCK(ils->ils_socket);
181 			ils->ils_socket->so_error = ENOTCONN;
182 			SOLISTEN_UNLOCK(ils->ils_socket);
183 			wakeup(&ils->ils_socket->so_timeo);
184 			sbt = mstosbt(995);
185 			pr = mstosbt(10);
186 			pause_sbt("icl_unlisten", sbt, pr, 0);
187 			sx_xlock(&il->il_lock);
188 		}
189 
190 		TAILQ_REMOVE(&il->il_sockets, ils, ils_next);
191 		soclose(ils->ils_socket);
192 		free(ils, M_ICL_PROXY);
193 	}
194 	sx_xunlock(&il->il_lock);
195 
196 	free(il, M_ICL_PROXY);
197 }
198 
199 /*
200  * XXX: Doing accept in a separate thread in each socket might not be the
201  * best way to do stuff, but it's pretty clean and debuggable - and you
202  * probably won't have hundreds of listening sockets anyway.
203  */
204 static void
205 icl_accept_thread(void *arg)
206 {
207 	struct icl_listen_sock *ils;
208 	struct socket *head, *so;
209 	struct sockaddr *sa;
210 	int error;
211 
212 	ils = arg;
213 	head = ils->ils_socket;
214 
215 	ils->ils_running = true;
216 
217 	for (;;) {
218 		SOLISTEN_LOCK(head);
219 		error = solisten_dequeue(head, &so, 0);
220 		if (error == ENOTCONN) {
221 			/*
222 			 * XXXGL: ENOTCONN is our mark from icl_listen_free().
223 			 * Neither socket code, nor msleep(9) may return it.
224 			 */
225 			ICL_DEBUG("terminating");
226 			ils->ils_running = false;
227 			kthread_exit();
228 			return;
229 		}
230 		if (error) {
231 			ICL_WARN("solisten_dequeue error %d", error);
232 			continue;
233 		}
234 
235 		sa = NULL;
236 		error = soaccept(so, &sa);
237 		if (error != 0) {
238 			ICL_WARN("soaccept error %d", error);
239 			if (sa != NULL)
240 				free(sa, M_SONAME);
241 			soclose(so);
242 			continue;
243 		}
244 
245 		(ils->ils_listen->il_accept)(so, sa, ils->ils_id);
246 	}
247 }
248 
249 static int
250 icl_listen_add_tcp(struct icl_listen *il, int domain, int socktype,
251     int protocol, struct sockaddr *sa, int portal_id)
252 {
253 	struct icl_listen_sock *ils;
254 	struct socket *so;
255 	struct sockopt sopt;
256 	int error, one = 1;
257 
258 	error = socreate(domain, &so, socktype, protocol,
259 	    curthread->td_ucred, curthread);
260 	if (error != 0) {
261 		ICL_WARN("socreate failed with error %d", error);
262 		return (error);
263 	}
264 
265 	sopt.sopt_dir = SOPT_SET;
266 	sopt.sopt_level = SOL_SOCKET;
267 	sopt.sopt_name = SO_REUSEADDR;
268 	sopt.sopt_val = &one;
269 	sopt.sopt_valsize = sizeof(one);
270 	sopt.sopt_td = NULL;
271 	error = sosetopt(so, &sopt);
272 	if (error != 0) {
273 		ICL_WARN("failed to set SO_REUSEADDR with error %d", error);
274 		soclose(so);
275 		return (error);
276 	}
277 
278 	error = sobind(so, sa, curthread);
279 	if (error != 0) {
280 		ICL_WARN("sobind failed with error %d", error);
281 		soclose(so);
282 		return (error);
283 	}
284 
285 	error = solisten(so, -1, curthread);
286 	if (error != 0) {
287 		ICL_WARN("solisten failed with error %d", error);
288 		soclose(so);
289 		return (error);
290 	}
291 
292 	ils = malloc(sizeof(*ils), M_ICL_PROXY, M_ZERO | M_WAITOK);
293 	ils->ils_listen = il;
294 	ils->ils_socket = so;
295 	ils->ils_id = portal_id;
296 
297 	error = kthread_add(icl_accept_thread, ils, NULL, NULL, 0, 0, "iclacc");
298 	if (error != 0) {
299 		ICL_WARN("kthread_add failed with error %d", error);
300 		soclose(so);
301 		free(ils, M_ICL_PROXY);
302 
303 		return (error);
304 	}
305 
306 	sx_xlock(&il->il_lock);
307 	TAILQ_INSERT_TAIL(&il->il_sockets, ils, ils_next);
308 	sx_xunlock(&il->il_lock);
309 
310 	return (0);
311 }
312 
313 int
314 icl_listen_add(struct icl_listen *il, bool rdma, int domain, int socktype,
315     int protocol, struct sockaddr *sa, int portal_id)
316 {
317 
318 	if (rdma) {
319 		ICL_DEBUG("RDMA not supported");
320 		return (EOPNOTSUPP);
321 	}
322 
323 	return (icl_listen_add_tcp(il, domain, socktype, protocol, sa,
324 	    portal_id));
325 }
326 
327 int
328 icl_listen_remove(struct icl_listen *il, struct sockaddr *sa)
329 {
330 
331 	/*
332 	 * XXX
333 	 */
334 
335 	return (EOPNOTSUPP);
336 }
337 
338 #endif /* ICL_KERNEL_PROXY */
339