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