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