1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2012 The FreeBSD Foundation 5 * 6 * This software was developed by Edward Tomasz Napierala under sponsorship 7 * from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 32 /* 33 * Software implementation of iSCSI Common Layer kobj(9) interface. 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include <sys/param.h> 40 #include <sys/capsicum.h> 41 #include <sys/condvar.h> 42 #include <sys/conf.h> 43 #include <sys/gsb_crc32.h> 44 #include <sys/file.h> 45 #include <sys/kernel.h> 46 #include <sys/kthread.h> 47 #include <sys/lock.h> 48 #include <sys/mbuf.h> 49 #include <sys/mutex.h> 50 #include <sys/module.h> 51 #include <sys/protosw.h> 52 #include <sys/socket.h> 53 #include <sys/socketvar.h> 54 #include <sys/sysctl.h> 55 #include <sys/systm.h> 56 #include <sys/sx.h> 57 #include <sys/uio.h> 58 #include <vm/uma.h> 59 #include <netinet/in.h> 60 #include <netinet/tcp.h> 61 62 #include <dev/iscsi/icl.h> 63 #include <dev/iscsi/iscsi_proto.h> 64 #include <icl_conn_if.h> 65 66 struct icl_soft_pdu { 67 struct icl_pdu ip; 68 69 /* soft specific stuff goes here. */ 70 u_int ref_cnt; 71 icl_pdu_cb cb; 72 int error; 73 }; 74 75 SYSCTL_NODE(_kern_icl, OID_AUTO, soft, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 76 "Software iSCSI"); 77 static int coalesce = 1; 78 SYSCTL_INT(_kern_icl_soft, OID_AUTO, coalesce, CTLFLAG_RWTUN, 79 &coalesce, 0, "Try to coalesce PDUs before sending"); 80 static int partial_receive_len = 256 * 1024; 81 SYSCTL_INT(_kern_icl_soft, OID_AUTO, partial_receive_len, CTLFLAG_RWTUN, 82 &partial_receive_len, 0, "Minimum read size for partially received " 83 "data segment"); 84 static int max_data_segment_length = 256 * 1024; 85 SYSCTL_INT(_kern_icl_soft, OID_AUTO, max_data_segment_length, CTLFLAG_RWTUN, 86 &max_data_segment_length, 0, "Maximum data segment length"); 87 static int first_burst_length = 1024 * 1024; 88 SYSCTL_INT(_kern_icl_soft, OID_AUTO, first_burst_length, CTLFLAG_RWTUN, 89 &first_burst_length, 0, "First burst length"); 90 static int max_burst_length = 1024 * 1024; 91 SYSCTL_INT(_kern_icl_soft, OID_AUTO, max_burst_length, CTLFLAG_RWTUN, 92 &max_burst_length, 0, "Maximum burst length"); 93 static int sendspace = 1536 * 1024; 94 SYSCTL_INT(_kern_icl_soft, OID_AUTO, sendspace, CTLFLAG_RWTUN, 95 &sendspace, 0, "Default send socket buffer size"); 96 static int recvspace = 1536 * 1024; 97 SYSCTL_INT(_kern_icl_soft, OID_AUTO, recvspace, CTLFLAG_RWTUN, 98 &recvspace, 0, "Default receive socket buffer size"); 99 100 static MALLOC_DEFINE(M_ICL_SOFT, "icl_soft", "iSCSI software backend"); 101 static uma_zone_t icl_soft_pdu_zone; 102 103 static volatile u_int icl_ncons; 104 105 #define ICL_CONN_LOCK(X) mtx_lock(X->ic_lock) 106 #define ICL_CONN_UNLOCK(X) mtx_unlock(X->ic_lock) 107 #define ICL_CONN_LOCK_ASSERT(X) mtx_assert(X->ic_lock, MA_OWNED) 108 #define ICL_CONN_LOCK_ASSERT_NOT(X) mtx_assert(X->ic_lock, MA_NOTOWNED) 109 110 STAILQ_HEAD(icl_pdu_stailq, icl_pdu); 111 112 static icl_conn_new_pdu_t icl_soft_conn_new_pdu; 113 static icl_conn_pdu_free_t icl_soft_conn_pdu_free; 114 static icl_conn_pdu_data_segment_length_t 115 icl_soft_conn_pdu_data_segment_length; 116 static icl_conn_pdu_append_data_t icl_soft_conn_pdu_append_data; 117 static icl_conn_pdu_get_data_t icl_soft_conn_pdu_get_data; 118 static icl_conn_pdu_queue_t icl_soft_conn_pdu_queue; 119 static icl_conn_pdu_queue_cb_t icl_soft_conn_pdu_queue_cb; 120 static icl_conn_handoff_t icl_soft_conn_handoff; 121 static icl_conn_free_t icl_soft_conn_free; 122 static icl_conn_close_t icl_soft_conn_close; 123 static icl_conn_task_setup_t icl_soft_conn_task_setup; 124 static icl_conn_task_done_t icl_soft_conn_task_done; 125 static icl_conn_transfer_setup_t icl_soft_conn_transfer_setup; 126 static icl_conn_transfer_done_t icl_soft_conn_transfer_done; 127 #ifdef ICL_KERNEL_PROXY 128 static icl_conn_connect_t icl_soft_conn_connect; 129 #endif 130 131 static kobj_method_t icl_soft_methods[] = { 132 KOBJMETHOD(icl_conn_new_pdu, icl_soft_conn_new_pdu), 133 KOBJMETHOD(icl_conn_pdu_free, icl_soft_conn_pdu_free), 134 KOBJMETHOD(icl_conn_pdu_data_segment_length, 135 icl_soft_conn_pdu_data_segment_length), 136 KOBJMETHOD(icl_conn_pdu_append_data, icl_soft_conn_pdu_append_data), 137 KOBJMETHOD(icl_conn_pdu_get_data, icl_soft_conn_pdu_get_data), 138 KOBJMETHOD(icl_conn_pdu_queue, icl_soft_conn_pdu_queue), 139 KOBJMETHOD(icl_conn_pdu_queue_cb, icl_soft_conn_pdu_queue_cb), 140 KOBJMETHOD(icl_conn_handoff, icl_soft_conn_handoff), 141 KOBJMETHOD(icl_conn_free, icl_soft_conn_free), 142 KOBJMETHOD(icl_conn_close, icl_soft_conn_close), 143 KOBJMETHOD(icl_conn_task_setup, icl_soft_conn_task_setup), 144 KOBJMETHOD(icl_conn_task_done, icl_soft_conn_task_done), 145 KOBJMETHOD(icl_conn_transfer_setup, icl_soft_conn_transfer_setup), 146 KOBJMETHOD(icl_conn_transfer_done, icl_soft_conn_transfer_done), 147 #ifdef ICL_KERNEL_PROXY 148 KOBJMETHOD(icl_conn_connect, icl_soft_conn_connect), 149 #endif 150 { 0, 0 } 151 }; 152 153 DEFINE_CLASS(icl_soft, icl_soft_methods, sizeof(struct icl_conn)); 154 155 static void 156 icl_conn_fail(struct icl_conn *ic) 157 { 158 if (ic->ic_socket == NULL) 159 return; 160 161 /* 162 * XXX 163 */ 164 ic->ic_socket->so_error = EDOOFUS; 165 (ic->ic_error)(ic); 166 } 167 168 static void 169 icl_soft_conn_pdu_free(struct icl_conn *ic, struct icl_pdu *ip) 170 { 171 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip; 172 173 KASSERT(isp->ref_cnt == 0, ("freeing active PDU")); 174 m_freem(ip->ip_bhs_mbuf); 175 m_freem(ip->ip_ahs_mbuf); 176 m_freem(ip->ip_data_mbuf); 177 uma_zfree(icl_soft_pdu_zone, isp); 178 #ifdef DIAGNOSTIC 179 refcount_release(&ic->ic_outstanding_pdus); 180 #endif 181 } 182 183 static void 184 icl_soft_pdu_call_cb(struct icl_pdu *ip) 185 { 186 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip; 187 188 if (isp->cb != NULL) 189 isp->cb(ip, isp->error); 190 #ifdef DIAGNOSTIC 191 refcount_release(&ip->ip_conn->ic_outstanding_pdus); 192 #endif 193 uma_zfree(icl_soft_pdu_zone, isp); 194 } 195 196 static void 197 icl_soft_pdu_done(struct icl_pdu *ip, int error) 198 { 199 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip; 200 201 if (error != 0) 202 isp->error = error; 203 204 m_freem(ip->ip_bhs_mbuf); 205 ip->ip_bhs_mbuf = NULL; 206 m_freem(ip->ip_ahs_mbuf); 207 ip->ip_ahs_mbuf = NULL; 208 m_freem(ip->ip_data_mbuf); 209 ip->ip_data_mbuf = NULL; 210 211 if (atomic_fetchadd_int(&isp->ref_cnt, -1) == 1) 212 icl_soft_pdu_call_cb(ip); 213 } 214 215 static void 216 icl_soft_mbuf_done(struct mbuf *mb) 217 { 218 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)mb->m_ext.ext_arg1; 219 220 icl_soft_pdu_call_cb(&isp->ip); 221 } 222 223 /* 224 * Allocate icl_pdu with empty BHS to fill up by the caller. 225 */ 226 struct icl_pdu * 227 icl_soft_conn_new_pdu(struct icl_conn *ic, int flags) 228 { 229 struct icl_soft_pdu *isp; 230 struct icl_pdu *ip; 231 232 #ifdef DIAGNOSTIC 233 refcount_acquire(&ic->ic_outstanding_pdus); 234 #endif 235 isp = uma_zalloc(icl_soft_pdu_zone, flags | M_ZERO); 236 if (isp == NULL) { 237 ICL_WARN("failed to allocate soft PDU"); 238 #ifdef DIAGNOSTIC 239 refcount_release(&ic->ic_outstanding_pdus); 240 #endif 241 return (NULL); 242 } 243 ip = &isp->ip; 244 ip->ip_conn = ic; 245 246 CTASSERT(sizeof(struct iscsi_bhs) <= MHLEN); 247 ip->ip_bhs_mbuf = m_gethdr(flags, MT_DATA); 248 if (ip->ip_bhs_mbuf == NULL) { 249 ICL_WARN("failed to allocate BHS mbuf"); 250 icl_soft_conn_pdu_free(ic, ip); 251 return (NULL); 252 } 253 ip->ip_bhs = mtod(ip->ip_bhs_mbuf, struct iscsi_bhs *); 254 memset(ip->ip_bhs, 0, sizeof(struct iscsi_bhs)); 255 ip->ip_bhs_mbuf->m_len = sizeof(struct iscsi_bhs); 256 257 return (ip); 258 } 259 260 static int 261 icl_pdu_ahs_length(const struct icl_pdu *request) 262 { 263 264 return (request->ip_bhs->bhs_total_ahs_len * 4); 265 } 266 267 static size_t 268 icl_pdu_data_segment_length(const struct icl_pdu *request) 269 { 270 uint32_t len = 0; 271 272 len += request->ip_bhs->bhs_data_segment_len[0]; 273 len <<= 8; 274 len += request->ip_bhs->bhs_data_segment_len[1]; 275 len <<= 8; 276 len += request->ip_bhs->bhs_data_segment_len[2]; 277 278 return (len); 279 } 280 281 size_t 282 icl_soft_conn_pdu_data_segment_length(struct icl_conn *ic, 283 const struct icl_pdu *request) 284 { 285 286 return (icl_pdu_data_segment_length(request)); 287 } 288 289 static void 290 icl_pdu_set_data_segment_length(struct icl_pdu *response, uint32_t len) 291 { 292 293 response->ip_bhs->bhs_data_segment_len[2] = len; 294 response->ip_bhs->bhs_data_segment_len[1] = len >> 8; 295 response->ip_bhs->bhs_data_segment_len[0] = len >> 16; 296 } 297 298 static size_t 299 icl_pdu_padding(const struct icl_pdu *ip) 300 { 301 302 if ((ip->ip_data_len % 4) != 0) 303 return (4 - (ip->ip_data_len % 4)); 304 305 return (0); 306 } 307 308 static size_t 309 icl_pdu_size(const struct icl_pdu *response) 310 { 311 size_t len; 312 313 KASSERT(response->ip_ahs_len == 0, ("responding with AHS")); 314 315 len = sizeof(struct iscsi_bhs) + response->ip_data_len + 316 icl_pdu_padding(response); 317 if (response->ip_conn->ic_header_crc32c) 318 len += ISCSI_HEADER_DIGEST_SIZE; 319 if (response->ip_data_len != 0 && response->ip_conn->ic_data_crc32c) 320 len += ISCSI_DATA_DIGEST_SIZE; 321 322 return (len); 323 } 324 325 static void 326 icl_soft_receive_buf(struct mbuf **r, size_t *rs, void *buf, size_t s) 327 { 328 329 m_copydata(*r, 0, s, buf); 330 m_adj(*r, s); 331 while ((*r) != NULL && (*r)->m_len == 0) 332 *r = m_free(*r); 333 *rs -= s; 334 } 335 336 static void 337 icl_pdu_receive_ahs(struct icl_pdu *request, struct mbuf **r, size_t *rs) 338 { 339 340 request->ip_ahs_len = icl_pdu_ahs_length(request); 341 if (request->ip_ahs_len == 0) 342 return; 343 344 request->ip_ahs_mbuf = *r; 345 *r = m_split(request->ip_ahs_mbuf, request->ip_ahs_len, M_WAITOK); 346 *rs -= request->ip_ahs_len; 347 } 348 349 static uint32_t 350 icl_mbuf_to_crc32c(const struct mbuf *m0) 351 { 352 uint32_t digest = 0xffffffff; 353 const struct mbuf *m; 354 355 for (m = m0; m != NULL; m = m->m_next) 356 digest = calculate_crc32c(digest, 357 mtod(m, const void *), m->m_len); 358 359 digest = digest ^ 0xffffffff; 360 361 return (digest); 362 } 363 364 static int 365 icl_pdu_check_header_digest(struct icl_pdu *request, struct mbuf **r, size_t *rs) 366 { 367 uint32_t received_digest, valid_digest; 368 369 if (request->ip_conn->ic_header_crc32c == false) 370 return (0); 371 372 CTASSERT(sizeof(received_digest) == ISCSI_HEADER_DIGEST_SIZE); 373 icl_soft_receive_buf(r, rs, &received_digest, ISCSI_HEADER_DIGEST_SIZE); 374 375 /* Temporary attach AHS to BHS to calculate header digest. */ 376 request->ip_bhs_mbuf->m_next = request->ip_ahs_mbuf; 377 valid_digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf); 378 request->ip_bhs_mbuf->m_next = NULL; 379 if (received_digest != valid_digest) { 380 ICL_WARN("header digest check failed; got 0x%x, " 381 "should be 0x%x", received_digest, valid_digest); 382 return (-1); 383 } 384 385 return (0); 386 } 387 388 /* 389 * Return the number of bytes that should be waiting in the receive socket 390 * before icl_pdu_receive_data_segment() gets called. 391 */ 392 static size_t 393 icl_pdu_data_segment_receive_len(const struct icl_pdu *request) 394 { 395 size_t len; 396 397 len = icl_pdu_data_segment_length(request); 398 if (len == 0) 399 return (0); 400 401 /* 402 * Account for the parts of data segment already read from 403 * the socket buffer. 404 */ 405 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len")); 406 len -= request->ip_data_len; 407 408 /* 409 * Don't always wait for the full data segment to be delivered 410 * to the socket; this might badly affect performance due to 411 * TCP window scaling. 412 */ 413 if (len > partial_receive_len) { 414 #if 0 415 ICL_DEBUG("need %zd bytes of data, limiting to %zd", 416 len, partial_receive_len)); 417 #endif 418 len = partial_receive_len; 419 420 return (len); 421 } 422 423 /* 424 * Account for padding. Note that due to the way code is written, 425 * the icl_pdu_receive_data_segment() must always receive padding 426 * along with the last part of data segment, because it would be 427 * impossible to tell whether we've already received the full data 428 * segment including padding, or without it. 429 */ 430 if ((len % 4) != 0) 431 len += 4 - (len % 4); 432 433 #if 0 434 ICL_DEBUG("need %zd bytes of data", len)); 435 #endif 436 437 return (len); 438 } 439 440 static int 441 icl_pdu_receive_data_segment(struct icl_pdu *request, struct mbuf **r, 442 size_t *rs, bool *more_neededp) 443 { 444 struct icl_conn *ic; 445 size_t len, padding = 0; 446 struct mbuf *m; 447 448 ic = request->ip_conn; 449 450 *more_neededp = false; 451 ic->ic_receive_len = 0; 452 453 len = icl_pdu_data_segment_length(request); 454 if (len == 0) 455 return (0); 456 457 if ((len % 4) != 0) 458 padding = 4 - (len % 4); 459 460 /* 461 * Account for already received parts of data segment. 462 */ 463 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len")); 464 len -= request->ip_data_len; 465 466 if (len + padding > *rs) { 467 /* 468 * Not enough data in the socket buffer. Receive as much 469 * as we can. Don't receive padding, since, obviously, it's 470 * not the end of data segment yet. 471 */ 472 #if 0 473 ICL_DEBUG("limited from %zd to %zd", 474 len + padding, *rs - padding)); 475 #endif 476 len = *rs - padding; 477 *more_neededp = true; 478 padding = 0; 479 } 480 481 /* 482 * Must not try to receive padding without at least one byte 483 * of actual data segment. 484 */ 485 if (len > 0) { 486 m = *r; 487 *r = m_split(m, len + padding, M_WAITOK); 488 *rs -= len + padding; 489 490 if (request->ip_data_mbuf == NULL) 491 request->ip_data_mbuf = m; 492 else 493 m_cat(request->ip_data_mbuf, m); 494 495 request->ip_data_len += len; 496 } else 497 ICL_DEBUG("len 0"); 498 499 if (*more_neededp) 500 ic->ic_receive_len = 501 icl_pdu_data_segment_receive_len(request); 502 503 return (0); 504 } 505 506 static int 507 icl_pdu_check_data_digest(struct icl_pdu *request, struct mbuf **r, size_t *rs) 508 { 509 uint32_t received_digest, valid_digest; 510 511 if (request->ip_conn->ic_data_crc32c == false) 512 return (0); 513 514 if (request->ip_data_len == 0) 515 return (0); 516 517 CTASSERT(sizeof(received_digest) == ISCSI_DATA_DIGEST_SIZE); 518 icl_soft_receive_buf(r, rs, &received_digest, ISCSI_DATA_DIGEST_SIZE); 519 520 /* 521 * Note that ip_data_mbuf also contains padding; since digest 522 * calculation is supposed to include that, we iterate over 523 * the entire ip_data_mbuf chain, not just ip_data_len bytes of it. 524 */ 525 valid_digest = icl_mbuf_to_crc32c(request->ip_data_mbuf); 526 if (received_digest != valid_digest) { 527 ICL_WARN("data digest check failed; got 0x%x, " 528 "should be 0x%x", received_digest, valid_digest); 529 return (-1); 530 } 531 532 return (0); 533 } 534 535 /* 536 * Somewhat contrary to the name, this attempts to receive only one 537 * "part" of PDU at a time; call it repeatedly until it returns non-NULL. 538 */ 539 static struct icl_pdu * 540 icl_conn_receive_pdu(struct icl_conn *ic, struct mbuf **r, size_t *rs) 541 { 542 struct icl_pdu *request; 543 size_t len; 544 int error = 0; 545 bool more_needed; 546 547 if (ic->ic_receive_state == ICL_CONN_STATE_BHS) { 548 KASSERT(ic->ic_receive_pdu == NULL, 549 ("ic->ic_receive_pdu != NULL")); 550 request = icl_soft_conn_new_pdu(ic, M_NOWAIT); 551 if (request == NULL) { 552 ICL_DEBUG("failed to allocate PDU; " 553 "dropping connection"); 554 icl_conn_fail(ic); 555 return (NULL); 556 } 557 ic->ic_receive_pdu = request; 558 } else { 559 KASSERT(ic->ic_receive_pdu != NULL, 560 ("ic->ic_receive_pdu == NULL")); 561 request = ic->ic_receive_pdu; 562 } 563 564 switch (ic->ic_receive_state) { 565 case ICL_CONN_STATE_BHS: 566 //ICL_DEBUG("receiving BHS"); 567 icl_soft_receive_buf(r, rs, request->ip_bhs, 568 sizeof(struct iscsi_bhs)); 569 570 /* 571 * We don't enforce any limit for AHS length; 572 * its length is stored in 8 bit field. 573 */ 574 575 len = icl_pdu_data_segment_length(request); 576 if (len > ic->ic_max_data_segment_length) { 577 ICL_WARN("received data segment " 578 "length %zd is larger than negotiated; " 579 "dropping connection", len); 580 error = EINVAL; 581 break; 582 } 583 584 ic->ic_receive_state = ICL_CONN_STATE_AHS; 585 ic->ic_receive_len = icl_pdu_ahs_length(request); 586 break; 587 588 case ICL_CONN_STATE_AHS: 589 //ICL_DEBUG("receiving AHS"); 590 icl_pdu_receive_ahs(request, r, rs); 591 ic->ic_receive_state = ICL_CONN_STATE_HEADER_DIGEST; 592 if (ic->ic_header_crc32c == false) 593 ic->ic_receive_len = 0; 594 else 595 ic->ic_receive_len = ISCSI_HEADER_DIGEST_SIZE; 596 break; 597 598 case ICL_CONN_STATE_HEADER_DIGEST: 599 //ICL_DEBUG("receiving header digest"); 600 error = icl_pdu_check_header_digest(request, r, rs); 601 if (error != 0) { 602 ICL_DEBUG("header digest failed; " 603 "dropping connection"); 604 break; 605 } 606 607 ic->ic_receive_state = ICL_CONN_STATE_DATA; 608 ic->ic_receive_len = 609 icl_pdu_data_segment_receive_len(request); 610 break; 611 612 case ICL_CONN_STATE_DATA: 613 //ICL_DEBUG("receiving data segment"); 614 error = icl_pdu_receive_data_segment(request, r, rs, 615 &more_needed); 616 if (error != 0) { 617 ICL_DEBUG("failed to receive data segment;" 618 "dropping connection"); 619 break; 620 } 621 622 if (more_needed) 623 break; 624 625 ic->ic_receive_state = ICL_CONN_STATE_DATA_DIGEST; 626 if (request->ip_data_len == 0 || ic->ic_data_crc32c == false) 627 ic->ic_receive_len = 0; 628 else 629 ic->ic_receive_len = ISCSI_DATA_DIGEST_SIZE; 630 break; 631 632 case ICL_CONN_STATE_DATA_DIGEST: 633 //ICL_DEBUG("receiving data digest"); 634 error = icl_pdu_check_data_digest(request, r, rs); 635 if (error != 0) { 636 ICL_DEBUG("data digest failed; " 637 "dropping connection"); 638 break; 639 } 640 641 /* 642 * We've received complete PDU; reset the receive state machine 643 * and return the PDU. 644 */ 645 ic->ic_receive_state = ICL_CONN_STATE_BHS; 646 ic->ic_receive_len = sizeof(struct iscsi_bhs); 647 ic->ic_receive_pdu = NULL; 648 return (request); 649 650 default: 651 panic("invalid ic_receive_state %d\n", ic->ic_receive_state); 652 } 653 654 if (error != 0) { 655 /* 656 * Don't free the PDU; it's pointed to by ic->ic_receive_pdu 657 * and will get freed in icl_soft_conn_close(). 658 */ 659 icl_conn_fail(ic); 660 } 661 662 return (NULL); 663 } 664 665 static void 666 icl_conn_receive_pdus(struct icl_conn *ic, struct mbuf **r, size_t *rs) 667 { 668 struct icl_pdu *response; 669 670 for (;;) { 671 if (ic->ic_disconnecting) 672 return; 673 674 /* 675 * Loop until we have a complete PDU or there is not enough 676 * data in the socket buffer. 677 */ 678 if (*rs < ic->ic_receive_len) { 679 #if 0 680 ICL_DEBUG("not enough data; have %zd, need %zd", 681 *rs, ic->ic_receive_len); 682 #endif 683 return; 684 } 685 686 response = icl_conn_receive_pdu(ic, r, rs); 687 if (response == NULL) 688 continue; 689 690 if (response->ip_ahs_len > 0) { 691 ICL_WARN("received PDU with unsupported " 692 "AHS; opcode 0x%x; dropping connection", 693 response->ip_bhs->bhs_opcode); 694 icl_soft_conn_pdu_free(ic, response); 695 icl_conn_fail(ic); 696 return; 697 } 698 699 (ic->ic_receive)(response); 700 } 701 } 702 703 static void 704 icl_receive_thread(void *arg) 705 { 706 struct icl_conn *ic; 707 size_t available, read = 0; 708 struct socket *so; 709 struct mbuf *m, *r = NULL; 710 struct uio uio; 711 int error, flags; 712 713 ic = arg; 714 so = ic->ic_socket; 715 716 for (;;) { 717 SOCKBUF_LOCK(&so->so_rcv); 718 if (ic->ic_disconnecting) { 719 SOCKBUF_UNLOCK(&so->so_rcv); 720 break; 721 } 722 723 /* 724 * Set the low watermark, to be checked by 725 * soreadable() in icl_soupcall_receive() 726 * to avoid unnecessary wakeups until there 727 * is enough data received to read the PDU. 728 */ 729 available = sbavail(&so->so_rcv); 730 if (read + available < ic->ic_receive_len) { 731 so->so_rcv.sb_lowat = ic->ic_receive_len - read; 732 cv_wait(&ic->ic_receive_cv, &so->so_rcv.sb_mtx); 733 so->so_rcv.sb_lowat = so->so_rcv.sb_hiwat + 1; 734 available = sbavail(&so->so_rcv); 735 } 736 SOCKBUF_UNLOCK(&so->so_rcv); 737 738 if (available == 0) { 739 if (so->so_error != 0) { 740 ICL_DEBUG("connection error %d; " 741 "dropping connection", so->so_error); 742 icl_conn_fail(ic); 743 break; 744 } 745 continue; 746 } 747 748 memset(&uio, 0, sizeof(uio)); 749 uio.uio_resid = available; 750 flags = MSG_DONTWAIT; 751 error = soreceive(so, NULL, &uio, &m, NULL, &flags); 752 if (error != 0) { 753 ICL_DEBUG("soreceive error %d", error); 754 break; 755 } 756 if (uio.uio_resid != 0) { 757 m_freem(m); 758 ICL_DEBUG("short read"); 759 break; 760 } 761 if (r) 762 m_cat(r, m); 763 else 764 r = m; 765 read += available; 766 767 icl_conn_receive_pdus(ic, &r, &read); 768 } 769 770 if (r) 771 m_freem(r); 772 773 ICL_CONN_LOCK(ic); 774 ic->ic_receive_running = false; 775 cv_signal(&ic->ic_send_cv); 776 ICL_CONN_UNLOCK(ic); 777 kthread_exit(); 778 } 779 780 static int 781 icl_soupcall_receive(struct socket *so, void *arg, int waitflag) 782 { 783 struct icl_conn *ic; 784 785 if (!soreadable(so)) 786 return (SU_OK); 787 788 ic = arg; 789 cv_signal(&ic->ic_receive_cv); 790 return (SU_OK); 791 } 792 793 static int 794 icl_pdu_finalize(struct icl_pdu *request) 795 { 796 size_t padding, pdu_len; 797 uint32_t digest, zero = 0; 798 int ok; 799 struct icl_conn *ic; 800 801 ic = request->ip_conn; 802 803 icl_pdu_set_data_segment_length(request, request->ip_data_len); 804 805 pdu_len = icl_pdu_size(request); 806 807 if (ic->ic_header_crc32c) { 808 digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf); 809 ok = m_append(request->ip_bhs_mbuf, sizeof(digest), 810 (void *)&digest); 811 if (ok != 1) { 812 ICL_WARN("failed to append header digest"); 813 return (1); 814 } 815 } 816 817 if (request->ip_data_len != 0) { 818 padding = icl_pdu_padding(request); 819 if (padding > 0) { 820 ok = m_append(request->ip_data_mbuf, padding, 821 (void *)&zero); 822 if (ok != 1) { 823 ICL_WARN("failed to append padding"); 824 return (1); 825 } 826 } 827 828 if (ic->ic_data_crc32c) { 829 digest = icl_mbuf_to_crc32c(request->ip_data_mbuf); 830 831 ok = m_append(request->ip_data_mbuf, sizeof(digest), 832 (void *)&digest); 833 if (ok != 1) { 834 ICL_WARN("failed to append data digest"); 835 return (1); 836 } 837 } 838 839 m_cat(request->ip_bhs_mbuf, request->ip_data_mbuf); 840 request->ip_data_mbuf = NULL; 841 } 842 843 request->ip_bhs_mbuf->m_pkthdr.len = pdu_len; 844 845 return (0); 846 } 847 848 static void 849 icl_conn_send_pdus(struct icl_conn *ic, struct icl_pdu_stailq *queue) 850 { 851 struct icl_pdu *request, *request2; 852 struct mbuf *m; 853 struct socket *so; 854 long available, size, size2; 855 int coalesced, error; 856 857 ICL_CONN_LOCK_ASSERT_NOT(ic); 858 859 so = ic->ic_socket; 860 861 SOCKBUF_LOCK(&so->so_snd); 862 /* 863 * Check how much space do we have for transmit. We can't just 864 * call sosend() and retry when we get EWOULDBLOCK or EMSGSIZE, 865 * as it always frees the mbuf chain passed to it, even in case 866 * of error. 867 */ 868 available = sbspace(&so->so_snd); 869 870 /* 871 * Notify the socket upcall that we don't need wakeups 872 * for the time being. 873 */ 874 so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1; 875 SOCKBUF_UNLOCK(&so->so_snd); 876 877 while (!STAILQ_EMPTY(queue)) { 878 request = STAILQ_FIRST(queue); 879 size = icl_pdu_size(request); 880 if (available < size) { 881 /* 882 * Set the low watermark, to be checked by 883 * sowriteable() in icl_soupcall_send() 884 * to avoid unnecessary wakeups until there 885 * is enough space for the PDU to fit. 886 */ 887 SOCKBUF_LOCK(&so->so_snd); 888 available = sbspace(&so->so_snd); 889 if (available < size) { 890 #if 1 891 ICL_DEBUG("no space to send; " 892 "have %ld, need %ld", 893 available, size); 894 #endif 895 so->so_snd.sb_lowat = max(size, 896 so->so_snd.sb_hiwat / 8); 897 SOCKBUF_UNLOCK(&so->so_snd); 898 return; 899 } 900 SOCKBUF_UNLOCK(&so->so_snd); 901 } 902 STAILQ_REMOVE_HEAD(queue, ip_next); 903 error = icl_pdu_finalize(request); 904 if (error != 0) { 905 ICL_DEBUG("failed to finalize PDU; " 906 "dropping connection"); 907 icl_soft_pdu_done(request, EIO); 908 icl_conn_fail(ic); 909 return; 910 } 911 if (coalesce) { 912 m = request->ip_bhs_mbuf; 913 for (coalesced = 1; ; coalesced++) { 914 request2 = STAILQ_FIRST(queue); 915 if (request2 == NULL) 916 break; 917 size2 = icl_pdu_size(request2); 918 if (available < size + size2) 919 break; 920 STAILQ_REMOVE_HEAD(queue, ip_next); 921 error = icl_pdu_finalize(request2); 922 if (error != 0) { 923 ICL_DEBUG("failed to finalize PDU; " 924 "dropping connection"); 925 icl_soft_pdu_done(request, EIO); 926 icl_soft_pdu_done(request2, EIO); 927 icl_conn_fail(ic); 928 return; 929 } 930 while (m->m_next) 931 m = m->m_next; 932 m_cat(m, request2->ip_bhs_mbuf); 933 request2->ip_bhs_mbuf = NULL; 934 request->ip_bhs_mbuf->m_pkthdr.len += size2; 935 size += size2; 936 icl_soft_pdu_done(request2, 0); 937 } 938 #if 0 939 if (coalesced > 1) { 940 ICL_DEBUG("coalesced %d PDUs into %ld bytes", 941 coalesced, size); 942 } 943 #endif 944 } 945 available -= size; 946 error = sosend(so, NULL, NULL, request->ip_bhs_mbuf, 947 NULL, MSG_DONTWAIT, curthread); 948 request->ip_bhs_mbuf = NULL; /* Sosend consumes the mbuf. */ 949 if (error != 0) { 950 ICL_DEBUG("failed to send PDU, error %d; " 951 "dropping connection", error); 952 icl_soft_pdu_done(request, error); 953 icl_conn_fail(ic); 954 return; 955 } 956 icl_soft_pdu_done(request, 0); 957 } 958 } 959 960 static void 961 icl_send_thread(void *arg) 962 { 963 struct icl_conn *ic; 964 struct icl_pdu_stailq queue; 965 966 ic = arg; 967 968 STAILQ_INIT(&queue); 969 970 ICL_CONN_LOCK(ic); 971 for (;;) { 972 for (;;) { 973 /* 974 * Populate the local queue from the main one. 975 * This way the icl_conn_send_pdus() can go through 976 * all the queued PDUs without holding any locks. 977 */ 978 STAILQ_CONCAT(&queue, &ic->ic_to_send); 979 980 ic->ic_check_send_space = false; 981 ICL_CONN_UNLOCK(ic); 982 icl_conn_send_pdus(ic, &queue); 983 ICL_CONN_LOCK(ic); 984 985 /* 986 * The icl_soupcall_send() was called since the last 987 * call to sbspace(); go around; 988 */ 989 if (ic->ic_check_send_space) 990 continue; 991 992 /* 993 * Local queue is empty, but we still have PDUs 994 * in the main one; go around. 995 */ 996 if (STAILQ_EMPTY(&queue) && 997 !STAILQ_EMPTY(&ic->ic_to_send)) 998 continue; 999 1000 /* 1001 * There might be some stuff in the local queue, 1002 * which didn't get sent due to not having enough send 1003 * space. Wait for socket upcall. 1004 */ 1005 break; 1006 } 1007 1008 if (ic->ic_disconnecting) { 1009 //ICL_DEBUG("terminating"); 1010 break; 1011 } 1012 1013 cv_wait(&ic->ic_send_cv, ic->ic_lock); 1014 } 1015 1016 /* 1017 * We're exiting; move PDUs back to the main queue, so they can 1018 * get freed properly. At this point ordering doesn't matter. 1019 */ 1020 STAILQ_CONCAT(&ic->ic_to_send, &queue); 1021 1022 ic->ic_send_running = false; 1023 cv_signal(&ic->ic_send_cv); 1024 ICL_CONN_UNLOCK(ic); 1025 kthread_exit(); 1026 } 1027 1028 static int 1029 icl_soupcall_send(struct socket *so, void *arg, int waitflag) 1030 { 1031 struct icl_conn *ic; 1032 1033 if (!sowriteable(so)) 1034 return (SU_OK); 1035 1036 ic = arg; 1037 1038 ICL_CONN_LOCK(ic); 1039 ic->ic_check_send_space = true; 1040 ICL_CONN_UNLOCK(ic); 1041 1042 cv_signal(&ic->ic_send_cv); 1043 1044 return (SU_OK); 1045 } 1046 1047 static int 1048 icl_soft_conn_pdu_append_data(struct icl_conn *ic, struct icl_pdu *request, 1049 const void *addr, size_t len, int flags) 1050 { 1051 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)request; 1052 struct mbuf *mb, *newmb; 1053 size_t copylen, off = 0; 1054 1055 KASSERT(len > 0, ("len == 0")); 1056 1057 if (flags & ICL_NOCOPY) { 1058 newmb = m_get(flags & ~ICL_NOCOPY, MT_DATA); 1059 if (newmb == NULL) { 1060 ICL_WARN("failed to allocate mbuf"); 1061 return (ENOMEM); 1062 } 1063 1064 newmb->m_flags |= M_RDONLY; 1065 m_extaddref(newmb, __DECONST(char *, addr), len, &isp->ref_cnt, 1066 icl_soft_mbuf_done, isp, NULL); 1067 newmb->m_len = len; 1068 } else { 1069 newmb = m_getm2(NULL, len, flags, MT_DATA, 0); 1070 if (newmb == NULL) { 1071 ICL_WARN("failed to allocate mbuf for %zd bytes", len); 1072 return (ENOMEM); 1073 } 1074 1075 for (mb = newmb; mb != NULL; mb = mb->m_next) { 1076 copylen = min(M_TRAILINGSPACE(mb), len - off); 1077 memcpy(mtod(mb, char *), (const char *)addr + off, copylen); 1078 mb->m_len = copylen; 1079 off += copylen; 1080 } 1081 KASSERT(off == len, ("%s: off != len", __func__)); 1082 } 1083 1084 if (request->ip_data_mbuf == NULL) { 1085 request->ip_data_mbuf = newmb; 1086 request->ip_data_len = len; 1087 } else { 1088 m_cat(request->ip_data_mbuf, newmb); 1089 request->ip_data_len += len; 1090 } 1091 1092 return (0); 1093 } 1094 1095 void 1096 icl_soft_conn_pdu_get_data(struct icl_conn *ic, struct icl_pdu *ip, 1097 size_t off, void *addr, size_t len) 1098 { 1099 1100 m_copydata(ip->ip_data_mbuf, off, len, addr); 1101 } 1102 1103 static void 1104 icl_soft_conn_pdu_queue(struct icl_conn *ic, struct icl_pdu *ip) 1105 { 1106 1107 icl_soft_conn_pdu_queue_cb(ic, ip, NULL); 1108 } 1109 1110 static void 1111 icl_soft_conn_pdu_queue_cb(struct icl_conn *ic, struct icl_pdu *ip, 1112 icl_pdu_cb cb) 1113 { 1114 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip; 1115 1116 ICL_CONN_LOCK_ASSERT(ic); 1117 isp->ref_cnt++; 1118 isp->cb = cb; 1119 1120 if (ic->ic_disconnecting || ic->ic_socket == NULL) { 1121 ICL_DEBUG("icl_pdu_queue on closed connection"); 1122 icl_soft_pdu_done(ip, ENOTCONN); 1123 return; 1124 } 1125 1126 if (!STAILQ_EMPTY(&ic->ic_to_send)) { 1127 STAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next); 1128 /* 1129 * If the queue is not empty, someone else had already 1130 * signaled the send thread; no need to do that again, 1131 * just return. 1132 */ 1133 return; 1134 } 1135 1136 STAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next); 1137 cv_signal(&ic->ic_send_cv); 1138 } 1139 1140 static struct icl_conn * 1141 icl_soft_new_conn(const char *name, struct mtx *lock) 1142 { 1143 struct icl_conn *ic; 1144 1145 refcount_acquire(&icl_ncons); 1146 1147 ic = (struct icl_conn *)kobj_create(&icl_soft_class, M_ICL_SOFT, M_WAITOK | M_ZERO); 1148 1149 STAILQ_INIT(&ic->ic_to_send); 1150 ic->ic_lock = lock; 1151 cv_init(&ic->ic_send_cv, "icl_tx"); 1152 cv_init(&ic->ic_receive_cv, "icl_rx"); 1153 #ifdef DIAGNOSTIC 1154 refcount_init(&ic->ic_outstanding_pdus, 0); 1155 #endif 1156 ic->ic_max_data_segment_length = max_data_segment_length; 1157 ic->ic_name = name; 1158 ic->ic_offload = "None"; 1159 ic->ic_unmapped = false; 1160 1161 return (ic); 1162 } 1163 1164 void 1165 icl_soft_conn_free(struct icl_conn *ic) 1166 { 1167 1168 #ifdef DIAGNOSTIC 1169 KASSERT(ic->ic_outstanding_pdus == 0, 1170 ("destroying session with %d outstanding PDUs", 1171 ic->ic_outstanding_pdus)); 1172 #endif 1173 cv_destroy(&ic->ic_send_cv); 1174 cv_destroy(&ic->ic_receive_cv); 1175 kobj_delete((struct kobj *)ic, M_ICL_SOFT); 1176 refcount_release(&icl_ncons); 1177 } 1178 1179 static int 1180 icl_conn_start(struct icl_conn *ic) 1181 { 1182 size_t minspace; 1183 struct sockopt opt; 1184 int error, one = 1; 1185 1186 ICL_CONN_LOCK(ic); 1187 1188 /* 1189 * XXX: Ugly hack. 1190 */ 1191 if (ic->ic_socket == NULL) { 1192 ICL_CONN_UNLOCK(ic); 1193 return (EINVAL); 1194 } 1195 1196 ic->ic_receive_state = ICL_CONN_STATE_BHS; 1197 ic->ic_receive_len = sizeof(struct iscsi_bhs); 1198 ic->ic_disconnecting = false; 1199 1200 ICL_CONN_UNLOCK(ic); 1201 1202 /* 1203 * For sendspace, this is required because the current code cannot 1204 * send a PDU in pieces; thus, the minimum buffer size is equal 1205 * to the maximum PDU size. "+4" is to account for possible padding. 1206 */ 1207 minspace = sizeof(struct iscsi_bhs) + ic->ic_max_data_segment_length + 1208 ISCSI_HEADER_DIGEST_SIZE + ISCSI_DATA_DIGEST_SIZE + 4; 1209 if (sendspace < minspace) { 1210 ICL_WARN("kern.icl.sendspace too low; must be at least %zd", 1211 minspace); 1212 sendspace = minspace; 1213 } 1214 if (recvspace < minspace) { 1215 ICL_WARN("kern.icl.recvspace too low; must be at least %zd", 1216 minspace); 1217 recvspace = minspace; 1218 } 1219 1220 error = soreserve(ic->ic_socket, sendspace, recvspace); 1221 if (error != 0) { 1222 ICL_WARN("soreserve failed with error %d", error); 1223 icl_soft_conn_close(ic); 1224 return (error); 1225 } 1226 ic->ic_socket->so_snd.sb_flags |= SB_AUTOSIZE; 1227 ic->ic_socket->so_rcv.sb_flags |= SB_AUTOSIZE; 1228 1229 /* 1230 * Disable Nagle. 1231 */ 1232 bzero(&opt, sizeof(opt)); 1233 opt.sopt_dir = SOPT_SET; 1234 opt.sopt_level = IPPROTO_TCP; 1235 opt.sopt_name = TCP_NODELAY; 1236 opt.sopt_val = &one; 1237 opt.sopt_valsize = sizeof(one); 1238 error = sosetopt(ic->ic_socket, &opt); 1239 if (error != 0) { 1240 ICL_WARN("disabling TCP_NODELAY failed with error %d", error); 1241 icl_soft_conn_close(ic); 1242 return (error); 1243 } 1244 1245 /* 1246 * Register socket upcall, to get notified about incoming PDUs 1247 * and free space to send outgoing ones. 1248 */ 1249 SOCKBUF_LOCK(&ic->ic_socket->so_snd); 1250 soupcall_set(ic->ic_socket, SO_SND, icl_soupcall_send, ic); 1251 SOCKBUF_UNLOCK(&ic->ic_socket->so_snd); 1252 SOCKBUF_LOCK(&ic->ic_socket->so_rcv); 1253 soupcall_set(ic->ic_socket, SO_RCV, icl_soupcall_receive, ic); 1254 SOCKBUF_UNLOCK(&ic->ic_socket->so_rcv); 1255 1256 /* 1257 * Start threads. 1258 */ 1259 ICL_CONN_LOCK(ic); 1260 ic->ic_send_running = ic->ic_receive_running = true; 1261 ICL_CONN_UNLOCK(ic); 1262 error = kthread_add(icl_send_thread, ic, NULL, NULL, 0, 0, "%stx", 1263 ic->ic_name); 1264 if (error != 0) { 1265 ICL_WARN("kthread_add(9) failed with error %d", error); 1266 ICL_CONN_LOCK(ic); 1267 ic->ic_send_running = ic->ic_receive_running = false; 1268 cv_signal(&ic->ic_send_cv); 1269 ICL_CONN_UNLOCK(ic); 1270 icl_soft_conn_close(ic); 1271 return (error); 1272 } 1273 error = kthread_add(icl_receive_thread, ic, NULL, NULL, 0, 0, "%srx", 1274 ic->ic_name); 1275 if (error != 0) { 1276 ICL_WARN("kthread_add(9) failed with error %d", error); 1277 ICL_CONN_LOCK(ic); 1278 ic->ic_receive_running = false; 1279 cv_signal(&ic->ic_send_cv); 1280 ICL_CONN_UNLOCK(ic); 1281 icl_soft_conn_close(ic); 1282 return (error); 1283 } 1284 1285 return (0); 1286 } 1287 1288 int 1289 icl_soft_conn_handoff(struct icl_conn *ic, int fd) 1290 { 1291 struct file *fp; 1292 struct socket *so; 1293 cap_rights_t rights; 1294 int error; 1295 1296 ICL_CONN_LOCK_ASSERT_NOT(ic); 1297 1298 #ifdef ICL_KERNEL_PROXY 1299 /* 1300 * We're transitioning to Full Feature phase, and we don't 1301 * really care. 1302 */ 1303 if (fd == 0) { 1304 ICL_CONN_LOCK(ic); 1305 if (ic->ic_socket == NULL) { 1306 ICL_CONN_UNLOCK(ic); 1307 ICL_WARN("proxy handoff without connect"); 1308 return (EINVAL); 1309 } 1310 ICL_CONN_UNLOCK(ic); 1311 return (0); 1312 } 1313 #endif 1314 1315 /* 1316 * Steal the socket from userland. 1317 */ 1318 error = fget(curthread, fd, 1319 cap_rights_init_one(&rights, CAP_SOCK_CLIENT), &fp); 1320 if (error != 0) 1321 return (error); 1322 if (fp->f_type != DTYPE_SOCKET) { 1323 fdrop(fp, curthread); 1324 return (EINVAL); 1325 } 1326 so = fp->f_data; 1327 if (so->so_type != SOCK_STREAM) { 1328 fdrop(fp, curthread); 1329 return (EINVAL); 1330 } 1331 1332 ICL_CONN_LOCK(ic); 1333 1334 if (ic->ic_socket != NULL) { 1335 ICL_CONN_UNLOCK(ic); 1336 fdrop(fp, curthread); 1337 return (EBUSY); 1338 } 1339 1340 ic->ic_socket = fp->f_data; 1341 fp->f_ops = &badfileops; 1342 fp->f_data = NULL; 1343 fdrop(fp, curthread); 1344 ICL_CONN_UNLOCK(ic); 1345 1346 error = icl_conn_start(ic); 1347 1348 return (error); 1349 } 1350 1351 void 1352 icl_soft_conn_close(struct icl_conn *ic) 1353 { 1354 struct icl_pdu *pdu; 1355 struct socket *so; 1356 1357 /* 1358 * Wake up the threads, so they can properly terminate. 1359 * Receive thread sleeps on so->so_rcv lock, send on ic->ic_lock. 1360 */ 1361 ICL_CONN_LOCK(ic); 1362 if (!ic->ic_disconnecting) { 1363 so = ic->ic_socket; 1364 if (so) 1365 SOCKBUF_LOCK(&so->so_rcv); 1366 ic->ic_disconnecting = true; 1367 if (so) 1368 SOCKBUF_UNLOCK(&so->so_rcv); 1369 } 1370 while (ic->ic_receive_running || ic->ic_send_running) { 1371 cv_signal(&ic->ic_receive_cv); 1372 cv_signal(&ic->ic_send_cv); 1373 cv_wait(&ic->ic_send_cv, ic->ic_lock); 1374 } 1375 1376 /* Some other thread could close the connection same time. */ 1377 so = ic->ic_socket; 1378 if (so == NULL) { 1379 ICL_CONN_UNLOCK(ic); 1380 return; 1381 } 1382 ic->ic_socket = NULL; 1383 1384 /* 1385 * Deregister socket upcalls. 1386 */ 1387 ICL_CONN_UNLOCK(ic); 1388 SOCKBUF_LOCK(&so->so_snd); 1389 if (so->so_snd.sb_upcall != NULL) 1390 soupcall_clear(so, SO_SND); 1391 SOCKBUF_UNLOCK(&so->so_snd); 1392 SOCKBUF_LOCK(&so->so_rcv); 1393 if (so->so_rcv.sb_upcall != NULL) 1394 soupcall_clear(so, SO_RCV); 1395 SOCKBUF_UNLOCK(&so->so_rcv); 1396 soclose(so); 1397 ICL_CONN_LOCK(ic); 1398 1399 if (ic->ic_receive_pdu != NULL) { 1400 //ICL_DEBUG("freeing partially received PDU"); 1401 icl_soft_conn_pdu_free(ic, ic->ic_receive_pdu); 1402 ic->ic_receive_pdu = NULL; 1403 } 1404 1405 /* 1406 * Remove any outstanding PDUs from the send queue. 1407 */ 1408 while (!STAILQ_EMPTY(&ic->ic_to_send)) { 1409 pdu = STAILQ_FIRST(&ic->ic_to_send); 1410 STAILQ_REMOVE_HEAD(&ic->ic_to_send, ip_next); 1411 icl_soft_pdu_done(pdu, ENOTCONN); 1412 } 1413 1414 KASSERT(STAILQ_EMPTY(&ic->ic_to_send), 1415 ("destroying session with non-empty send queue")); 1416 ICL_CONN_UNLOCK(ic); 1417 } 1418 1419 int 1420 icl_soft_conn_task_setup(struct icl_conn *ic, struct icl_pdu *ip, 1421 struct ccb_scsiio *csio, uint32_t *task_tagp, void **prvp) 1422 { 1423 1424 return (0); 1425 } 1426 1427 void 1428 icl_soft_conn_task_done(struct icl_conn *ic, void *prv) 1429 { 1430 } 1431 1432 int 1433 icl_soft_conn_transfer_setup(struct icl_conn *ic, union ctl_io *io, 1434 uint32_t *transfer_tag, void **prvp) 1435 { 1436 1437 return (0); 1438 } 1439 1440 void 1441 icl_soft_conn_transfer_done(struct icl_conn *ic, void *prv) 1442 { 1443 } 1444 1445 static int 1446 icl_soft_limits(struct icl_drv_limits *idl) 1447 { 1448 1449 idl->idl_max_recv_data_segment_length = max_data_segment_length; 1450 idl->idl_max_send_data_segment_length = max_data_segment_length; 1451 idl->idl_max_burst_length = max_burst_length; 1452 idl->idl_first_burst_length = first_burst_length; 1453 1454 return (0); 1455 } 1456 1457 #ifdef ICL_KERNEL_PROXY 1458 int 1459 icl_soft_conn_connect(struct icl_conn *ic, int domain, int socktype, 1460 int protocol, struct sockaddr *from_sa, struct sockaddr *to_sa) 1461 { 1462 1463 return (icl_soft_proxy_connect(ic, domain, socktype, protocol, 1464 from_sa, to_sa)); 1465 } 1466 1467 int 1468 icl_soft_handoff_sock(struct icl_conn *ic, struct socket *so) 1469 { 1470 int error; 1471 1472 ICL_CONN_LOCK_ASSERT_NOT(ic); 1473 1474 if (so->so_type != SOCK_STREAM) 1475 return (EINVAL); 1476 1477 ICL_CONN_LOCK(ic); 1478 if (ic->ic_socket != NULL) { 1479 ICL_CONN_UNLOCK(ic); 1480 return (EBUSY); 1481 } 1482 ic->ic_socket = so; 1483 ICL_CONN_UNLOCK(ic); 1484 1485 error = icl_conn_start(ic); 1486 1487 return (error); 1488 } 1489 #endif /* ICL_KERNEL_PROXY */ 1490 1491 static int 1492 icl_soft_load(void) 1493 { 1494 int error; 1495 1496 icl_soft_pdu_zone = uma_zcreate("icl_soft_pdu", 1497 sizeof(struct icl_soft_pdu), NULL, NULL, NULL, NULL, 1498 UMA_ALIGN_PTR, 0); 1499 refcount_init(&icl_ncons, 0); 1500 1501 /* 1502 * The reason we call this "none" is that to the user, 1503 * it's known as "offload driver"; "offload driver: soft" 1504 * doesn't make much sense. 1505 */ 1506 error = icl_register("none", false, 0, 1507 icl_soft_limits, icl_soft_new_conn); 1508 KASSERT(error == 0, ("failed to register")); 1509 1510 #if defined(ICL_KERNEL_PROXY) && 0 1511 /* 1512 * Debugging aid for kernel proxy functionality. 1513 */ 1514 error = icl_register("proxytest", true, 0, 1515 icl_soft_limits, icl_soft_new_conn); 1516 KASSERT(error == 0, ("failed to register")); 1517 #endif 1518 1519 return (error); 1520 } 1521 1522 static int 1523 icl_soft_unload(void) 1524 { 1525 1526 if (icl_ncons != 0) 1527 return (EBUSY); 1528 1529 icl_unregister("none", false); 1530 #if defined(ICL_KERNEL_PROXY) && 0 1531 icl_unregister("proxytest", true); 1532 #endif 1533 1534 uma_zdestroy(icl_soft_pdu_zone); 1535 1536 return (0); 1537 } 1538 1539 static int 1540 icl_soft_modevent(module_t mod, int what, void *arg) 1541 { 1542 1543 switch (what) { 1544 case MOD_LOAD: 1545 return (icl_soft_load()); 1546 case MOD_UNLOAD: 1547 return (icl_soft_unload()); 1548 default: 1549 return (EINVAL); 1550 } 1551 } 1552 1553 moduledata_t icl_soft_data = { 1554 "icl_soft", 1555 icl_soft_modevent, 1556 0 1557 }; 1558 1559 DECLARE_MODULE(icl_soft, icl_soft_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); 1560 MODULE_DEPEND(icl_soft, icl, 1, 1, 1); 1561 MODULE_VERSION(icl_soft, 1); 1562