xref: /freebsd/sys/dev/iscsi/icl_soft_proxy.c (revision 6bfca4dcab07dad45a805879d954876b353c0810)
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/param.h>
68 #include <sys/capsicum.h>
69 #include <sys/condvar.h>
70 #include <sys/conf.h>
71 #include <sys/lock.h>
72 #include <sys/kernel.h>
73 #include <sys/kthread.h>
74 #include <sys/malloc.h>
75 #include <sys/mutex.h>
76 #include <sys/proc.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/sx.h>
80 #include <sys/systm.h>
81 #include <netinet/in.h>
82 #include <netinet/tcp.h>
83 
84 #include <dev/iscsi/icl.h>
85 
86 struct icl_listen_sock {
87 	TAILQ_ENTRY(icl_listen_sock)	ils_next;
88 	struct icl_listen		*ils_listen;
89 	struct socket			*ils_socket;
90 	bool				ils_running;
91 	int				ils_id;
92 };
93 
94 struct icl_listen	{
95 	TAILQ_HEAD(, icl_listen_sock)	il_sockets;
96 	struct sx			il_lock;
97 	void				(*il_accept)(struct socket *,
98 					    struct sockaddr *, int);
99 };
100 
101 static MALLOC_DEFINE(M_ICL_PROXY, "ICL_PROXY", "iSCSI common layer proxy");
102 
103 int
104 icl_soft_proxy_connect(struct icl_conn *ic, int domain, int socktype,
105     int protocol, struct sockaddr *from_sa, struct sockaddr *to_sa)
106 {
107 	struct socket *so;
108 	int error;
109 
110 	error = socreate(domain, &so, socktype, protocol,
111 	    curthread->td_ucred, curthread);
112 	if (error != 0)
113 		return (error);
114 
115 	if (from_sa != NULL) {
116 		error = sobind(so, from_sa, curthread);
117 		if (error != 0) {
118 			soclose(so);
119 			return (error);
120 		}
121 	}
122 
123 	error = soconnect(so, to_sa, curthread);
124 	if (error != 0) {
125 		soclose(so);
126 		return (error);
127 	}
128 
129 	SOCK_LOCK(so);
130 	while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
131 		error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
132 		    "icl_connect", 0);
133 		if (error)
134 			break;
135 	}
136 	if (error == 0) {
137 		error = so->so_error;
138 		so->so_error = 0;
139 	}
140 	SOCK_UNLOCK(so);
141 
142 	if (error != 0) {
143 		soclose(so);
144 		return (error);
145 	}
146 
147 	error = icl_soft_handoff_sock(ic, so);
148 	if (error != 0)
149 		soclose(so);
150 
151 	return (error);
152 }
153 
154 struct icl_listen *
155 icl_listen_new(void (*accept_cb)(struct socket *, struct sockaddr *, int))
156 {
157 	struct icl_listen *il;
158 
159 	il = malloc(sizeof(*il), M_ICL_PROXY, M_ZERO | M_WAITOK);
160 	TAILQ_INIT(&il->il_sockets);
161 	sx_init(&il->il_lock, "icl_listen");
162 	il->il_accept = accept_cb;
163 
164 	return (il);
165 }
166 
167 void
168 icl_listen_free(struct icl_listen *il)
169 {
170 	struct icl_listen_sock *ils;
171 	sbintime_t sbt, pr;
172 
173 	sx_xlock(&il->il_lock);
174 	while (!TAILQ_EMPTY(&il->il_sockets)) {
175 		ils = TAILQ_FIRST(&il->il_sockets);
176 		while (ils->ils_running) {
177 			ICL_DEBUG("waiting for accept thread to terminate");
178 			sx_xunlock(&il->il_lock);
179 			SOLISTEN_LOCK(ils->ils_socket);
180 			ils->ils_socket->so_error = ENOTCONN;
181 			SOLISTEN_UNLOCK(ils->ils_socket);
182 			wakeup(&ils->ils_socket->so_timeo);
183 			sbt = mstosbt(995);
184 			pr = mstosbt(10);
185 			pause_sbt("icl_unlisten", sbt, pr, 0);
186 			sx_xlock(&il->il_lock);
187 		}
188 
189 		TAILQ_REMOVE(&il->il_sockets, ils, ils_next);
190 		soclose(ils->ils_socket);
191 		free(ils, M_ICL_PROXY);
192 	}
193 	sx_xunlock(&il->il_lock);
194 
195 	free(il, M_ICL_PROXY);
196 }
197 
198 /*
199  * XXX: Doing accept in a separate thread in each socket might not be the
200  * best way to do stuff, but it's pretty clean and debuggable - and you
201  * probably won't have hundreds of listening sockets anyway.
202  */
203 static void
204 icl_accept_thread(void *arg)
205 {
206 	struct icl_listen_sock *ils;
207 	struct socket *head, *so;
208 	struct sockaddr *sa;
209 	int error;
210 
211 	ils = arg;
212 	head = ils->ils_socket;
213 
214 	ils->ils_running = true;
215 
216 	for (;;) {
217 		SOLISTEN_LOCK(head);
218 		error = solisten_dequeue(head, &so, 0);
219 		if (error == ENOTCONN) {
220 			/*
221 			 * XXXGL: ENOTCONN is our mark from icl_listen_free().
222 			 * Neither socket code, nor msleep(9) may return it.
223 			 */
224 			ICL_DEBUG("terminating");
225 			ils->ils_running = false;
226 			kthread_exit();
227 			return;
228 		}
229 		if (error) {
230 			ICL_WARN("solisten_dequeue error %d", error);
231 			continue;
232 		}
233 
234 		sa = NULL;
235 		error = soaccept(so, &sa);
236 		if (error != 0) {
237 			ICL_WARN("soaccept error %d", error);
238 			if (sa != NULL)
239 				free(sa, M_SONAME);
240 			soclose(so);
241 			continue;
242 		}
243 
244 		(ils->ils_listen->il_accept)(so, sa, ils->ils_id);
245 	}
246 }
247 
248 static int
249 icl_listen_add_tcp(struct icl_listen *il, int domain, int socktype,
250     int protocol, struct sockaddr *sa, int portal_id)
251 {
252 	struct icl_listen_sock *ils;
253 	struct socket *so;
254 	struct sockopt sopt;
255 	int error, one = 1;
256 
257 	error = socreate(domain, &so, socktype, protocol,
258 	    curthread->td_ucred, curthread);
259 	if (error != 0) {
260 		ICL_WARN("socreate failed with error %d", error);
261 		return (error);
262 	}
263 
264 	sopt.sopt_dir = SOPT_SET;
265 	sopt.sopt_level = SOL_SOCKET;
266 	sopt.sopt_name = SO_REUSEADDR;
267 	sopt.sopt_val = &one;
268 	sopt.sopt_valsize = sizeof(one);
269 	sopt.sopt_td = NULL;
270 	error = sosetopt(so, &sopt);
271 	if (error != 0) {
272 		ICL_WARN("failed to set SO_REUSEADDR with error %d", error);
273 		soclose(so);
274 		return (error);
275 	}
276 
277 	error = sobind(so, sa, curthread);
278 	if (error != 0) {
279 		ICL_WARN("sobind failed with error %d", error);
280 		soclose(so);
281 		return (error);
282 	}
283 
284 	error = solisten(so, -1, curthread);
285 	if (error != 0) {
286 		ICL_WARN("solisten failed with error %d", error);
287 		soclose(so);
288 		return (error);
289 	}
290 
291 	ils = malloc(sizeof(*ils), M_ICL_PROXY, M_ZERO | M_WAITOK);
292 	ils->ils_listen = il;
293 	ils->ils_socket = so;
294 	ils->ils_id = portal_id;
295 
296 	error = kthread_add(icl_accept_thread, ils, NULL, NULL, 0, 0, "iclacc");
297 	if (error != 0) {
298 		ICL_WARN("kthread_add failed with error %d", error);
299 		soclose(so);
300 		free(ils, M_ICL_PROXY);
301 
302 		return (error);
303 	}
304 
305 	sx_xlock(&il->il_lock);
306 	TAILQ_INSERT_TAIL(&il->il_sockets, ils, ils_next);
307 	sx_xunlock(&il->il_lock);
308 
309 	return (0);
310 }
311 
312 int
313 icl_listen_add(struct icl_listen *il, bool rdma, int domain, int socktype,
314     int protocol, struct sockaddr *sa, int portal_id)
315 {
316 
317 	if (rdma) {
318 		ICL_DEBUG("RDMA not supported");
319 		return (EOPNOTSUPP);
320 	}
321 
322 	return (icl_listen_add_tcp(il, domain, socktype, protocol, sa,
323 	    portal_id));
324 }
325 
326 int
327 icl_listen_remove(struct icl_listen *il, struct sockaddr *sa)
328 {
329 
330 	/*
331 	 * XXX
332 	 */
333 
334 	return (EOPNOTSUPP);
335 }
336 
337 #endif /* ICL_KERNEL_PROXY */
338