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_storage ss = { .ss_len = sizeof(ss) }; 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 error = soaccept(so, (struct sockaddr *)&ss); 235 if (error != 0) { 236 ICL_WARN("soaccept error %d", error); 237 soclose(so); 238 continue; 239 } 240 241 (ils->ils_listen->il_accept)(so, (struct sockaddr *)&ss, 242 ils->ils_id); 243 } 244 } 245 246 static int 247 icl_listen_add_tcp(struct icl_listen *il, int domain, int socktype, 248 int protocol, struct sockaddr *sa, int portal_id) 249 { 250 struct icl_listen_sock *ils; 251 struct socket *so; 252 struct sockopt sopt; 253 int error, one = 1; 254 255 error = socreate(domain, &so, socktype, protocol, 256 curthread->td_ucred, curthread); 257 if (error != 0) { 258 ICL_WARN("socreate failed with error %d", error); 259 return (error); 260 } 261 262 sopt.sopt_dir = SOPT_SET; 263 sopt.sopt_level = SOL_SOCKET; 264 sopt.sopt_name = SO_REUSEADDR; 265 sopt.sopt_val = &one; 266 sopt.sopt_valsize = sizeof(one); 267 sopt.sopt_td = NULL; 268 error = sosetopt(so, &sopt); 269 if (error != 0) { 270 ICL_WARN("failed to set SO_REUSEADDR with error %d", error); 271 soclose(so); 272 return (error); 273 } 274 275 error = sobind(so, sa, curthread); 276 if (error != 0) { 277 ICL_WARN("sobind failed with error %d", error); 278 soclose(so); 279 return (error); 280 } 281 282 error = solisten(so, -1, curthread); 283 if (error != 0) { 284 ICL_WARN("solisten failed with error %d", error); 285 soclose(so); 286 return (error); 287 } 288 289 ils = malloc(sizeof(*ils), M_ICL_PROXY, M_ZERO | M_WAITOK); 290 ils->ils_listen = il; 291 ils->ils_socket = so; 292 ils->ils_id = portal_id; 293 294 error = kthread_add(icl_accept_thread, ils, NULL, NULL, 0, 0, "iclacc"); 295 if (error != 0) { 296 ICL_WARN("kthread_add failed with error %d", error); 297 soclose(so); 298 free(ils, M_ICL_PROXY); 299 300 return (error); 301 } 302 303 sx_xlock(&il->il_lock); 304 TAILQ_INSERT_TAIL(&il->il_sockets, ils, ils_next); 305 sx_xunlock(&il->il_lock); 306 307 return (0); 308 } 309 310 int 311 icl_listen_add(struct icl_listen *il, bool rdma, int domain, int socktype, 312 int protocol, struct sockaddr *sa, int portal_id) 313 { 314 315 if (rdma) { 316 ICL_DEBUG("RDMA not supported"); 317 return (EOPNOTSUPP); 318 } 319 320 return (icl_listen_add_tcp(il, domain, socktype, protocol, sa, 321 portal_id)); 322 } 323 324 int 325 icl_listen_remove(struct icl_listen *il, struct sockaddr *sa) 326 { 327 328 /* 329 * XXX 330 */ 331 332 return (EOPNOTSUPP); 333 } 334 335 #endif /* ICL_KERNEL_PROXY */ 336