xref: /freebsd/sys/dev/iscsi/icl_soft_proxy.c (revision 257cbe34)
1257cbe34SEdward Tomasz Napierala /*-
2257cbe34SEdward Tomasz Napierala  * Copyright (c) 2012 The FreeBSD Foundation
3257cbe34SEdward Tomasz Napierala  * All rights reserved.
4257cbe34SEdward Tomasz Napierala  *
5257cbe34SEdward Tomasz Napierala  * This software was developed by Edward Tomasz Napierala under sponsorship
6257cbe34SEdward Tomasz Napierala  * from the FreeBSD Foundation.
7257cbe34SEdward Tomasz Napierala  *
8257cbe34SEdward Tomasz Napierala  * Redistribution and use in source and binary forms, with or without
9257cbe34SEdward Tomasz Napierala  * modification, are permitted provided that the following conditions
10257cbe34SEdward Tomasz Napierala  * are met:
11257cbe34SEdward Tomasz Napierala  * 1. Redistributions of source code must retain the above copyright
12257cbe34SEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer.
13257cbe34SEdward Tomasz Napierala  * 2. Redistributions in binary form must reproduce the above copyright
14257cbe34SEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer in the
15257cbe34SEdward Tomasz Napierala  *    documentation and/or other materials provided with the distribution.
16257cbe34SEdward Tomasz Napierala  *
17257cbe34SEdward Tomasz Napierala  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18257cbe34SEdward Tomasz Napierala  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19257cbe34SEdward Tomasz Napierala  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20257cbe34SEdward Tomasz Napierala  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21257cbe34SEdward Tomasz Napierala  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22257cbe34SEdward Tomasz Napierala  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23257cbe34SEdward Tomasz Napierala  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24257cbe34SEdward Tomasz Napierala  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25257cbe34SEdward Tomasz Napierala  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26257cbe34SEdward Tomasz Napierala  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27257cbe34SEdward Tomasz Napierala  * SUCH DAMAGE.
28257cbe34SEdward Tomasz Napierala  *
29257cbe34SEdward Tomasz Napierala  */
30257cbe34SEdward Tomasz Napierala /*-
31257cbe34SEdward Tomasz Napierala  * Copyright (c) 1982, 1986, 1989, 1990, 1993
32257cbe34SEdward Tomasz Napierala  *	The Regents of the University of California.  All rights reserved.
33257cbe34SEdward Tomasz Napierala  *
34257cbe34SEdward Tomasz Napierala  * sendfile(2) and related extensions:
35257cbe34SEdward Tomasz Napierala  * Copyright (c) 1998, David Greenman. All rights reserved.
36257cbe34SEdward Tomasz Napierala  *
37257cbe34SEdward Tomasz Napierala  * Redistribution and use in source and binary forms, with or without
38257cbe34SEdward Tomasz Napierala  * modification, are permitted provided that the following conditions
39257cbe34SEdward Tomasz Napierala  * are met:
40257cbe34SEdward Tomasz Napierala  * 1. Redistributions of source code must retain the above copyright
41257cbe34SEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer.
42257cbe34SEdward Tomasz Napierala  * 2. Redistributions in binary form must reproduce the above copyright
43257cbe34SEdward Tomasz Napierala  *    notice, this list of conditions and the following disclaimer in the
44257cbe34SEdward Tomasz Napierala  *    documentation and/or other materials provided with the distribution.
45257cbe34SEdward Tomasz Napierala  * 4. Neither the name of the University nor the names of its contributors
46257cbe34SEdward Tomasz Napierala  *    may be used to endorse or promote products derived from this software
47257cbe34SEdward Tomasz Napierala  *    without specific prior written permission.
48257cbe34SEdward Tomasz Napierala  *
49257cbe34SEdward Tomasz Napierala  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50257cbe34SEdward Tomasz Napierala  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51257cbe34SEdward Tomasz Napierala  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52257cbe34SEdward Tomasz Napierala  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53257cbe34SEdward Tomasz Napierala  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54257cbe34SEdward Tomasz Napierala  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55257cbe34SEdward Tomasz Napierala  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56257cbe34SEdward Tomasz Napierala  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57257cbe34SEdward Tomasz Napierala  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58257cbe34SEdward Tomasz Napierala  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59257cbe34SEdward Tomasz Napierala  * SUCH DAMAGE.
60257cbe34SEdward Tomasz Napierala  *
61257cbe34SEdward Tomasz Napierala  *	@(#)uipc_syscalls.c	8.4 (Berkeley) 2/21/94
62257cbe34SEdward Tomasz Napierala  */
63257cbe34SEdward Tomasz Napierala 
64257cbe34SEdward Tomasz Napierala /*
65257cbe34SEdward Tomasz Napierala  * iSCSI Common Layer, kernel proxy part.
66257cbe34SEdward Tomasz Napierala  */
67257cbe34SEdward Tomasz Napierala 
68257cbe34SEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY
69257cbe34SEdward Tomasz Napierala 
70257cbe34SEdward Tomasz Napierala #include <sys/cdefs.h>
71257cbe34SEdward Tomasz Napierala __FBSDID("$FreeBSD$");
72257cbe34SEdward Tomasz Napierala 
73257cbe34SEdward Tomasz Napierala #include <sys/param.h>
74257cbe34SEdward Tomasz Napierala #include <sys/capsicum.h>
75257cbe34SEdward Tomasz Napierala #include <sys/condvar.h>
76257cbe34SEdward Tomasz Napierala #include <sys/conf.h>
77257cbe34SEdward Tomasz Napierala #include <sys/kernel.h>
78257cbe34SEdward Tomasz Napierala #include <sys/kthread.h>
79257cbe34SEdward Tomasz Napierala #include <sys/malloc.h>
80257cbe34SEdward Tomasz Napierala #include <sys/proc.h>
81257cbe34SEdward Tomasz Napierala #include <sys/socket.h>
82257cbe34SEdward Tomasz Napierala #include <sys/socketvar.h>
83257cbe34SEdward Tomasz Napierala #include <sys/sx.h>
84257cbe34SEdward Tomasz Napierala #include <sys/systm.h>
85257cbe34SEdward Tomasz Napierala #include <netinet/in.h>
86257cbe34SEdward Tomasz Napierala #include <netinet/tcp.h>
87257cbe34SEdward Tomasz Napierala 
88257cbe34SEdward Tomasz Napierala #include <dev/iscsi/icl.h>
89257cbe34SEdward Tomasz Napierala 
90257cbe34SEdward Tomasz Napierala struct icl_listen_sock {
91257cbe34SEdward Tomasz Napierala 	TAILQ_ENTRY(icl_listen_sock)	ils_next;
92257cbe34SEdward Tomasz Napierala 	struct icl_listen		*ils_listen;
93257cbe34SEdward Tomasz Napierala 	struct socket			*ils_socket;
94257cbe34SEdward Tomasz Napierala 	bool				ils_running;
95257cbe34SEdward Tomasz Napierala 	bool				ils_disconnecting;
96257cbe34SEdward Tomasz Napierala 	int				ils_id;
97257cbe34SEdward Tomasz Napierala };
98257cbe34SEdward Tomasz Napierala 
99257cbe34SEdward Tomasz Napierala struct icl_listen	{
100257cbe34SEdward Tomasz Napierala 	TAILQ_HEAD(, icl_listen_sock)	il_sockets;
101257cbe34SEdward Tomasz Napierala 	struct sx			il_lock;
102257cbe34SEdward Tomasz Napierala 	void				(*il_accept)(struct socket *,
103257cbe34SEdward Tomasz Napierala 					    struct sockaddr *, int);
104257cbe34SEdward Tomasz Napierala };
105257cbe34SEdward Tomasz Napierala 
106257cbe34SEdward Tomasz Napierala static MALLOC_DEFINE(M_ICL_PROXY, "ICL_PROXY", "iSCSI common layer proxy");
107257cbe34SEdward Tomasz Napierala 
108257cbe34SEdward Tomasz Napierala int
109257cbe34SEdward Tomasz Napierala icl_soft_proxy_connect(struct icl_conn *ic, int domain, int socktype,
110257cbe34SEdward Tomasz Napierala     int protocol, struct sockaddr *from_sa, struct sockaddr *to_sa)
111257cbe34SEdward Tomasz Napierala {
112257cbe34SEdward Tomasz Napierala 	struct socket *so;
113257cbe34SEdward Tomasz Napierala 	int error;
114257cbe34SEdward Tomasz Napierala 	int interrupted = 0;
115257cbe34SEdward Tomasz Napierala 
116257cbe34SEdward Tomasz Napierala 	error = socreate(domain, &so, socktype, protocol,
117257cbe34SEdward Tomasz Napierala 	    curthread->td_ucred, curthread);
118257cbe34SEdward Tomasz Napierala 	if (error != 0)
119257cbe34SEdward Tomasz Napierala 		return (error);
120257cbe34SEdward Tomasz Napierala 
121257cbe34SEdward Tomasz Napierala 	if (from_sa != NULL) {
122257cbe34SEdward Tomasz Napierala 		error = sobind(so, from_sa, curthread);
123257cbe34SEdward Tomasz Napierala 		if (error != 0) {
124257cbe34SEdward Tomasz Napierala 			soclose(so);
125257cbe34SEdward Tomasz Napierala 			return (error);
126257cbe34SEdward Tomasz Napierala 		}
127257cbe34SEdward Tomasz Napierala 	}
128257cbe34SEdward Tomasz Napierala 
129257cbe34SEdward Tomasz Napierala 	error = soconnect(so, to_sa, curthread);
130257cbe34SEdward Tomasz Napierala 	if (error != 0) {
131257cbe34SEdward Tomasz Napierala 		soclose(so);
132257cbe34SEdward Tomasz Napierala 		return (error);
133257cbe34SEdward Tomasz Napierala 	}
134257cbe34SEdward Tomasz Napierala 
135257cbe34SEdward Tomasz Napierala 	SOCK_LOCK(so);
136257cbe34SEdward Tomasz Napierala 	while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
137257cbe34SEdward Tomasz Napierala 		error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
138257cbe34SEdward Tomasz Napierala 		    "icl_connect", 0);
139257cbe34SEdward Tomasz Napierala 		if (error) {
140257cbe34SEdward Tomasz Napierala 			if (error == EINTR || error == ERESTART)
141257cbe34SEdward Tomasz Napierala 				interrupted = 1;
142257cbe34SEdward Tomasz Napierala 			break;
143257cbe34SEdward Tomasz Napierala 		}
144257cbe34SEdward Tomasz Napierala 	}
145257cbe34SEdward Tomasz Napierala 	if (error == 0) {
146257cbe34SEdward Tomasz Napierala 		error = so->so_error;
147257cbe34SEdward Tomasz Napierala 		so->so_error = 0;
148257cbe34SEdward Tomasz Napierala 	}
149257cbe34SEdward Tomasz Napierala 	SOCK_UNLOCK(so);
150257cbe34SEdward Tomasz Napierala 
151257cbe34SEdward Tomasz Napierala 	if (error != 0) {
152257cbe34SEdward Tomasz Napierala 		soclose(so);
153257cbe34SEdward Tomasz Napierala 		return (error);
154257cbe34SEdward Tomasz Napierala 	}
155257cbe34SEdward Tomasz Napierala 
156257cbe34SEdward Tomasz Napierala 	error = icl_soft_handoff_sock(ic, so);
157257cbe34SEdward Tomasz Napierala 	if (error != 0)
158257cbe34SEdward Tomasz Napierala 		soclose(so);
159257cbe34SEdward Tomasz Napierala 
160257cbe34SEdward Tomasz Napierala 	return (error);
161257cbe34SEdward Tomasz Napierala }
162257cbe34SEdward Tomasz Napierala 
163257cbe34SEdward Tomasz Napierala struct icl_listen *
164257cbe34SEdward Tomasz Napierala icl_listen_new(void (*accept_cb)(struct socket *, struct sockaddr *, int))
165257cbe34SEdward Tomasz Napierala {
166257cbe34SEdward Tomasz Napierala 	struct icl_listen *il;
167257cbe34SEdward Tomasz Napierala 
168257cbe34SEdward Tomasz Napierala 	il = malloc(sizeof(*il), M_ICL_PROXY, M_ZERO | M_WAITOK);
169257cbe34SEdward Tomasz Napierala 	TAILQ_INIT(&il->il_sockets);
170257cbe34SEdward Tomasz Napierala 	sx_init(&il->il_lock, "icl_listen");
171257cbe34SEdward Tomasz Napierala 	il->il_accept = accept_cb;
172257cbe34SEdward Tomasz Napierala 
173257cbe34SEdward Tomasz Napierala 	return (il);
174257cbe34SEdward Tomasz Napierala }
175257cbe34SEdward Tomasz Napierala 
176257cbe34SEdward Tomasz Napierala void
177257cbe34SEdward Tomasz Napierala icl_listen_free(struct icl_listen *il)
178257cbe34SEdward Tomasz Napierala {
179257cbe34SEdward Tomasz Napierala 	struct icl_listen_sock *ils;
180257cbe34SEdward Tomasz Napierala 
181257cbe34SEdward Tomasz Napierala 	sx_xlock(&il->il_lock);
182257cbe34SEdward Tomasz Napierala 	while (!TAILQ_EMPTY(&il->il_sockets)) {
183257cbe34SEdward Tomasz Napierala 		ils = TAILQ_FIRST(&il->il_sockets);
184257cbe34SEdward Tomasz Napierala 		while (ils->ils_running) {
185257cbe34SEdward Tomasz Napierala 			ICL_DEBUG("waiting for accept thread to terminate");
186257cbe34SEdward Tomasz Napierala 			sx_xunlock(&il->il_lock);
187257cbe34SEdward Tomasz Napierala 			ils->ils_disconnecting = true;
188257cbe34SEdward Tomasz Napierala 			wakeup(&ils->ils_socket->so_timeo);
189257cbe34SEdward Tomasz Napierala 			pause("icl_unlisten", 1 * hz);
190257cbe34SEdward Tomasz Napierala 			sx_xlock(&il->il_lock);
191257cbe34SEdward Tomasz Napierala 		}
192257cbe34SEdward Tomasz Napierala 
193257cbe34SEdward Tomasz Napierala 		TAILQ_REMOVE(&il->il_sockets, ils, ils_next);
194257cbe34SEdward Tomasz Napierala 		soclose(ils->ils_socket);
195257cbe34SEdward Tomasz Napierala 		free(ils, M_ICL_PROXY);
196257cbe34SEdward Tomasz Napierala 	}
197257cbe34SEdward Tomasz Napierala 	sx_xunlock(&il->il_lock);
198257cbe34SEdward Tomasz Napierala 
199257cbe34SEdward Tomasz Napierala 	free(il, M_ICL_PROXY);
200257cbe34SEdward Tomasz Napierala }
201257cbe34SEdward Tomasz Napierala 
202257cbe34SEdward Tomasz Napierala /*
203257cbe34SEdward Tomasz Napierala  * XXX: Doing accept in a separate thread in each socket might not be the best way
204257cbe34SEdward Tomasz Napierala  * 	to do stuff, but it's pretty clean and debuggable - and you probably won't
205257cbe34SEdward Tomasz Napierala  * 	have hundreds of listening sockets anyway.
206257cbe34SEdward Tomasz Napierala  */
207257cbe34SEdward Tomasz Napierala static void
208257cbe34SEdward Tomasz Napierala icl_accept_thread(void *arg)
209257cbe34SEdward Tomasz Napierala {
210257cbe34SEdward Tomasz Napierala 	struct icl_listen_sock *ils;
211257cbe34SEdward Tomasz Napierala 	struct socket *head, *so;
212257cbe34SEdward Tomasz Napierala 	struct sockaddr *sa;
213257cbe34SEdward Tomasz Napierala 	int error;
214257cbe34SEdward Tomasz Napierala 
215257cbe34SEdward Tomasz Napierala 	ils = arg;
216257cbe34SEdward Tomasz Napierala 	head = ils->ils_socket;
217257cbe34SEdward Tomasz Napierala 
218257cbe34SEdward Tomasz Napierala 	ils->ils_running = true;
219257cbe34SEdward Tomasz Napierala 
220257cbe34SEdward Tomasz Napierala 	for (;;) {
221257cbe34SEdward Tomasz Napierala 		ACCEPT_LOCK();
222257cbe34SEdward Tomasz Napierala 		while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0 && ils->ils_disconnecting == false) {
223257cbe34SEdward Tomasz Napierala 			if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
224257cbe34SEdward Tomasz Napierala 				head->so_error = ECONNABORTED;
225257cbe34SEdward Tomasz Napierala 				break;
226257cbe34SEdward Tomasz Napierala 			}
227257cbe34SEdward Tomasz Napierala 			error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
228257cbe34SEdward Tomasz Napierala 			    "accept", 0);
229257cbe34SEdward Tomasz Napierala 			if (error) {
230257cbe34SEdward Tomasz Napierala 				ACCEPT_UNLOCK();
231257cbe34SEdward Tomasz Napierala 				ICL_WARN("msleep failed with error %d", error);
232257cbe34SEdward Tomasz Napierala 				continue;
233257cbe34SEdward Tomasz Napierala 			}
234257cbe34SEdward Tomasz Napierala 			if (ils->ils_disconnecting) {
235257cbe34SEdward Tomasz Napierala 				ACCEPT_UNLOCK();
236257cbe34SEdward Tomasz Napierala 				ICL_DEBUG("terminating");
237257cbe34SEdward Tomasz Napierala 				ils->ils_running = false;
238257cbe34SEdward Tomasz Napierala 				kthread_exit();
239257cbe34SEdward Tomasz Napierala 				return;
240257cbe34SEdward Tomasz Napierala 			}
241257cbe34SEdward Tomasz Napierala 		}
242257cbe34SEdward Tomasz Napierala 		if (head->so_error) {
243257cbe34SEdward Tomasz Napierala 			error = head->so_error;
244257cbe34SEdward Tomasz Napierala 			head->so_error = 0;
245257cbe34SEdward Tomasz Napierala 			ACCEPT_UNLOCK();
246257cbe34SEdward Tomasz Napierala 			ICL_WARN("socket error %d", error);
247257cbe34SEdward Tomasz Napierala 			continue;
248257cbe34SEdward Tomasz Napierala 		}
249257cbe34SEdward Tomasz Napierala 		so = TAILQ_FIRST(&head->so_comp);
250257cbe34SEdward Tomasz Napierala 		KASSERT(so != NULL, ("NULL so"));
251257cbe34SEdward Tomasz Napierala 		KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
252257cbe34SEdward Tomasz Napierala 		KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
253257cbe34SEdward Tomasz Napierala 
254257cbe34SEdward Tomasz Napierala 		/*
255257cbe34SEdward Tomasz Napierala 		 * Before changing the flags on the socket, we have to bump the
256257cbe34SEdward Tomasz Napierala 		 * reference count.  Otherwise, if the protocol calls sofree(),
257257cbe34SEdward Tomasz Napierala 		 * the socket will be released due to a zero refcount.
258257cbe34SEdward Tomasz Napierala 		 */
259257cbe34SEdward Tomasz Napierala 		SOCK_LOCK(so);			/* soref() and so_state update */
260257cbe34SEdward Tomasz Napierala 		soref(so);			/* file descriptor reference */
261257cbe34SEdward Tomasz Napierala 
262257cbe34SEdward Tomasz Napierala 		TAILQ_REMOVE(&head->so_comp, so, so_list);
263257cbe34SEdward Tomasz Napierala 		head->so_qlen--;
264257cbe34SEdward Tomasz Napierala 		so->so_state |= (head->so_state & SS_NBIO);
265257cbe34SEdward Tomasz Napierala 		so->so_qstate &= ~SQ_COMP;
266257cbe34SEdward Tomasz Napierala 		so->so_head = NULL;
267257cbe34SEdward Tomasz Napierala 
268257cbe34SEdward Tomasz Napierala 		SOCK_UNLOCK(so);
269257cbe34SEdward Tomasz Napierala 		ACCEPT_UNLOCK();
270257cbe34SEdward Tomasz Napierala 
271257cbe34SEdward Tomasz Napierala 		sa = NULL;
272257cbe34SEdward Tomasz Napierala 		error = soaccept(so, &sa);
273257cbe34SEdward Tomasz Napierala 		if (error != 0) {
274257cbe34SEdward Tomasz Napierala 			ICL_WARN("soaccept error %d", error);
275257cbe34SEdward Tomasz Napierala 			if (sa != NULL)
276257cbe34SEdward Tomasz Napierala 				free(sa, M_SONAME);
277257cbe34SEdward Tomasz Napierala 			soclose(so);
278257cbe34SEdward Tomasz Napierala 			continue;
279257cbe34SEdward Tomasz Napierala 		}
280257cbe34SEdward Tomasz Napierala 
281257cbe34SEdward Tomasz Napierala 		(ils->ils_listen->il_accept)(so, sa, ils->ils_id);
282257cbe34SEdward Tomasz Napierala 	}
283257cbe34SEdward Tomasz Napierala }
284257cbe34SEdward Tomasz Napierala 
285257cbe34SEdward Tomasz Napierala static int
286257cbe34SEdward Tomasz Napierala icl_listen_add_tcp(struct icl_listen *il, int domain, int socktype,
287257cbe34SEdward Tomasz Napierala     int protocol, struct sockaddr *sa, int portal_id)
288257cbe34SEdward Tomasz Napierala {
289257cbe34SEdward Tomasz Napierala 	struct icl_listen_sock *ils;
290257cbe34SEdward Tomasz Napierala 	struct socket *so;
291257cbe34SEdward Tomasz Napierala 	struct sockopt sopt;
292257cbe34SEdward Tomasz Napierala 	int error, one = 1;
293257cbe34SEdward Tomasz Napierala 
294257cbe34SEdward Tomasz Napierala 	error = socreate(domain, &so, socktype, protocol,
295257cbe34SEdward Tomasz Napierala 	    curthread->td_ucred, curthread);
296257cbe34SEdward Tomasz Napierala 	if (error != 0) {
297257cbe34SEdward Tomasz Napierala 		ICL_WARN("socreate failed with error %d", error);
298257cbe34SEdward Tomasz Napierala 		return (error);
299257cbe34SEdward Tomasz Napierala 	}
300257cbe34SEdward Tomasz Napierala 
301257cbe34SEdward Tomasz Napierala 	sopt.sopt_dir = SOPT_SET;
302257cbe34SEdward Tomasz Napierala 	sopt.sopt_level = SOL_SOCKET;
303257cbe34SEdward Tomasz Napierala 	sopt.sopt_name = SO_REUSEADDR;
304257cbe34SEdward Tomasz Napierala 	sopt.sopt_val = &one;
305257cbe34SEdward Tomasz Napierala 	sopt.sopt_valsize = sizeof(one);
306257cbe34SEdward Tomasz Napierala 	sopt.sopt_td = NULL;
307257cbe34SEdward Tomasz Napierala 	error = sosetopt(so, &sopt);
308257cbe34SEdward Tomasz Napierala 	if (error != 0) {
309257cbe34SEdward Tomasz Napierala 		ICL_WARN("failed to set SO_REUSEADDR with error %d", error);
310257cbe34SEdward Tomasz Napierala 		soclose(so);
311257cbe34SEdward Tomasz Napierala 		return (error);
312257cbe34SEdward Tomasz Napierala 	}
313257cbe34SEdward Tomasz Napierala 
314257cbe34SEdward Tomasz Napierala 	error = sobind(so, sa, curthread);
315257cbe34SEdward Tomasz Napierala 	if (error != 0) {
316257cbe34SEdward Tomasz Napierala 		ICL_WARN("sobind failed with error %d", error);
317257cbe34SEdward Tomasz Napierala 		soclose(so);
318257cbe34SEdward Tomasz Napierala 		return (error);
319257cbe34SEdward Tomasz Napierala 	}
320257cbe34SEdward Tomasz Napierala 
321257cbe34SEdward Tomasz Napierala 	error = solisten(so, -1, curthread);
322257cbe34SEdward Tomasz Napierala 	if (error != 0) {
323257cbe34SEdward Tomasz Napierala 		ICL_WARN("solisten failed with error %d", error);
324257cbe34SEdward Tomasz Napierala 		soclose(so);
325257cbe34SEdward Tomasz Napierala 		return (error);
326257cbe34SEdward Tomasz Napierala 	}
327257cbe34SEdward Tomasz Napierala 
328257cbe34SEdward Tomasz Napierala 	ils = malloc(sizeof(*ils), M_ICL_PROXY, M_ZERO | M_WAITOK);
329257cbe34SEdward Tomasz Napierala 	ils->ils_listen = il;
330257cbe34SEdward Tomasz Napierala 	ils->ils_socket = so;
331257cbe34SEdward Tomasz Napierala 	ils->ils_id = portal_id;
332257cbe34SEdward Tomasz Napierala 
333257cbe34SEdward Tomasz Napierala 	error = kthread_add(icl_accept_thread, ils, NULL, NULL, 0, 0, "iclacc");
334257cbe34SEdward Tomasz Napierala 	if (error != 0) {
335257cbe34SEdward Tomasz Napierala 		ICL_WARN("kthread_add failed with error %d", error);
336257cbe34SEdward Tomasz Napierala 		soclose(so);
337257cbe34SEdward Tomasz Napierala 		free(ils, M_ICL_PROXY);
338257cbe34SEdward Tomasz Napierala 
339257cbe34SEdward Tomasz Napierala 		return (error);
340257cbe34SEdward Tomasz Napierala 	}
341257cbe34SEdward Tomasz Napierala 
342257cbe34SEdward Tomasz Napierala 	sx_xlock(&il->il_lock);
343257cbe34SEdward Tomasz Napierala 	TAILQ_INSERT_TAIL(&il->il_sockets, ils, ils_next);
344257cbe34SEdward Tomasz Napierala 	sx_xunlock(&il->il_lock);
345257cbe34SEdward Tomasz Napierala 
346257cbe34SEdward Tomasz Napierala 	return (0);
347257cbe34SEdward Tomasz Napierala }
348257cbe34SEdward Tomasz Napierala 
349257cbe34SEdward Tomasz Napierala int
350257cbe34SEdward Tomasz Napierala icl_listen_add(struct icl_listen *il, bool rdma, int domain, int socktype,
351257cbe34SEdward Tomasz Napierala     int protocol, struct sockaddr *sa, int portal_id)
352257cbe34SEdward Tomasz Napierala {
353257cbe34SEdward Tomasz Napierala 
354257cbe34SEdward Tomasz Napierala 	if (rdma) {
355257cbe34SEdward Tomasz Napierala 		ICL_DEBUG("RDMA not supported");
356257cbe34SEdward Tomasz Napierala 		return (EOPNOTSUPP);
357257cbe34SEdward Tomasz Napierala 	}
358257cbe34SEdward Tomasz Napierala 
359257cbe34SEdward Tomasz Napierala 
360257cbe34SEdward Tomasz Napierala 	return (icl_listen_add_tcp(il, domain, socktype, protocol, sa,
361257cbe34SEdward Tomasz Napierala 	    portal_id));
362257cbe34SEdward Tomasz Napierala }
363257cbe34SEdward Tomasz Napierala 
364257cbe34SEdward Tomasz Napierala int
365257cbe34SEdward Tomasz Napierala icl_listen_remove(struct icl_listen *il, struct sockaddr *sa)
366257cbe34SEdward Tomasz Napierala {
367257cbe34SEdward Tomasz Napierala 
368257cbe34SEdward Tomasz Napierala 	/*
369257cbe34SEdward Tomasz Napierala 	 * XXX
370257cbe34SEdward Tomasz Napierala 	 */
371257cbe34SEdward Tomasz Napierala 
372257cbe34SEdward Tomasz Napierala 	return (EOPNOTSUPP);
373257cbe34SEdward Tomasz Napierala }
374257cbe34SEdward Tomasz Napierala 
375257cbe34SEdward Tomasz Napierala #endif /* ICL_KERNEL_PROXY */
376