2 * Copyright (c) 2012 The FreeBSD Foundation
4 * This software was developed by Edward Tomasz Napierala under sponsorship
5 * from the FreeBSD Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 * Copyright (c) 1982, 1986, 1989, 1990, 1993
31 * The Regents of the University of California. All rights reserved.
33 * sendfile(2) and related extensions:
34 * Copyright (c) 1998, David Greenman. All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
60 * @(#)uipc_syscalls.c 8.4 (Berkeley) 2/21/94
64 * iSCSI Common Layer, kernel proxy part.
67 #ifdef ICL_KERNEL_PROXY
69 #include <sys/cdefs.h>
70 __FBSDID("$FreeBSD$");
72 #include <sys/param.h>
73 #include <sys/capsicum.h>
74 #include <sys/condvar.h>
77 #include <sys/kernel.h>
78 #include <sys/kthread.h>
79 #include <sys/malloc.h>
80 #include <sys/mutex.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
85 #include <sys/systm.h>
86 #include <netinet/in.h>
87 #include <netinet/tcp.h>
89 #include <dev/iscsi/icl.h>
91 struct icl_listen_sock {
92 TAILQ_ENTRY(icl_listen_sock) ils_next;
93 struct icl_listen *ils_listen;
94 struct socket *ils_socket;
100 TAILQ_HEAD(, icl_listen_sock) il_sockets;
102 void (*il_accept)(struct socket *,
103 struct sockaddr *, int);
106 static MALLOC_DEFINE(M_ICL_PROXY, "ICL_PROXY", "iSCSI common layer proxy");
109 icl_soft_proxy_connect(struct icl_conn *ic, int domain, int socktype,
110 int protocol, struct sockaddr *from_sa, struct sockaddr *to_sa)
116 error = socreate(domain, &so, socktype, protocol,
117 curthread->td_ucred, curthread);
121 if (from_sa != NULL) {
122 error = sobind(so, from_sa, curthread);
129 error = soconnect(so, to_sa, curthread);
136 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
137 error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
140 if (error == EINTR || error == ERESTART)
146 error = so->so_error;
156 error = icl_soft_handoff_sock(ic, so);
164 icl_listen_new(void (*accept_cb)(struct socket *, struct sockaddr *, int))
166 struct icl_listen *il;
168 il = malloc(sizeof(*il), M_ICL_PROXY, M_ZERO | M_WAITOK);
169 TAILQ_INIT(&il->il_sockets);
170 sx_init(&il->il_lock, "icl_listen");
171 il->il_accept = accept_cb;
177 icl_listen_free(struct icl_listen *il)
179 struct icl_listen_sock *ils;
181 sx_xlock(&il->il_lock);
182 while (!TAILQ_EMPTY(&il->il_sockets)) {
183 ils = TAILQ_FIRST(&il->il_sockets);
184 while (ils->ils_running) {
185 ICL_DEBUG("waiting for accept thread to terminate");
186 sx_xunlock(&il->il_lock);
187 SOLISTEN_LOCK(ils->ils_socket);
188 ils->ils_socket->so_error = ENOTCONN;
189 SOLISTEN_UNLOCK(ils->ils_socket);
190 wakeup(&ils->ils_socket->so_timeo);
191 pause("icl_unlisten", 1 * hz);
192 sx_xlock(&il->il_lock);
195 TAILQ_REMOVE(&il->il_sockets, ils, ils_next);
196 soclose(ils->ils_socket);
197 free(ils, M_ICL_PROXY);
199 sx_xunlock(&il->il_lock);
201 free(il, M_ICL_PROXY);
205 * XXX: Doing accept in a separate thread in each socket might not be the
206 * best way to do stuff, but it's pretty clean and debuggable - and you
207 * probably won't have hundreds of listening sockets anyway.
210 icl_accept_thread(void *arg)
212 struct icl_listen_sock *ils;
213 struct socket *head, *so;
218 head = ils->ils_socket;
220 ils->ils_running = true;
224 error = solisten_dequeue(head, &so, 0);
225 if (error == ENOTCONN) {
227 * XXXGL: ENOTCONN is our mark from icl_listen_free().
228 * Neither socket code, nor msleep(9) may return it.
230 ICL_DEBUG("terminating");
231 ils->ils_running = false;
236 ICL_WARN("solisten_dequeue error %d", error);
241 error = soaccept(so, &sa);
243 ICL_WARN("soaccept error %d", error);
250 (ils->ils_listen->il_accept)(so, sa, ils->ils_id);
255 icl_listen_add_tcp(struct icl_listen *il, int domain, int socktype,
256 int protocol, struct sockaddr *sa, int portal_id)
258 struct icl_listen_sock *ils;
263 error = socreate(domain, &so, socktype, protocol,
264 curthread->td_ucred, curthread);
266 ICL_WARN("socreate failed with error %d", error);
270 sopt.sopt_dir = SOPT_SET;
271 sopt.sopt_level = SOL_SOCKET;
272 sopt.sopt_name = SO_REUSEADDR;
273 sopt.sopt_val = &one;
274 sopt.sopt_valsize = sizeof(one);
276 error = sosetopt(so, &sopt);
278 ICL_WARN("failed to set SO_REUSEADDR with error %d", error);
283 error = sobind(so, sa, curthread);
285 ICL_WARN("sobind failed with error %d", error);
290 error = solisten(so, -1, curthread);
292 ICL_WARN("solisten failed with error %d", error);
297 ils = malloc(sizeof(*ils), M_ICL_PROXY, M_ZERO | M_WAITOK);
298 ils->ils_listen = il;
299 ils->ils_socket = so;
300 ils->ils_id = portal_id;
302 error = kthread_add(icl_accept_thread, ils, NULL, NULL, 0, 0, "iclacc");
304 ICL_WARN("kthread_add failed with error %d", error);
306 free(ils, M_ICL_PROXY);
311 sx_xlock(&il->il_lock);
312 TAILQ_INSERT_TAIL(&il->il_sockets, ils, ils_next);
313 sx_xunlock(&il->il_lock);
319 icl_listen_add(struct icl_listen *il, bool rdma, int domain, int socktype,
320 int protocol, struct sockaddr *sa, int portal_id)
324 ICL_DEBUG("RDMA not supported");
328 return (icl_listen_add_tcp(il, domain, socktype, protocol, sa,
333 icl_listen_remove(struct icl_listen *il, struct sockaddr *sa)
343 #endif /* ICL_KERNEL_PROXY */