2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
7 * This software was developed by Edward Tomasz Napierala under sponsorship
8 * from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Software implementation of iSCSI Common Layer kobj(9) interface.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/capsicum.h>
42 #include <sys/condvar.h>
45 #include <sys/kernel.h>
46 #include <sys/kthread.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>
59 #include <netinet/in.h>
60 #include <netinet/tcp.h>
62 #include <dev/iscsi/icl.h>
63 #include <dev/iscsi/iscsi_proto.h>
64 #include <icl_conn_if.h>
66 static int coalesce = 1;
67 SYSCTL_INT(_kern_icl, OID_AUTO, coalesce, CTLFLAG_RWTUN,
68 &coalesce, 0, "Try to coalesce PDUs before sending");
69 static int partial_receive_len = 128 * 1024;
70 SYSCTL_INT(_kern_icl, OID_AUTO, partial_receive_len, CTLFLAG_RWTUN,
71 &partial_receive_len, 0, "Minimum read size for partially received "
73 static int sendspace = 1048576;
74 SYSCTL_INT(_kern_icl, OID_AUTO, sendspace, CTLFLAG_RWTUN,
75 &sendspace, 0, "Default send socket buffer size");
76 static int recvspace = 1048576;
77 SYSCTL_INT(_kern_icl, OID_AUTO, recvspace, CTLFLAG_RWTUN,
78 &recvspace, 0, "Default receive socket buffer size");
80 static MALLOC_DEFINE(M_ICL_SOFT, "icl_soft", "iSCSI software backend");
81 static uma_zone_t icl_pdu_zone;
83 static volatile u_int icl_ncons;
85 #define ICL_CONN_LOCK(X) mtx_lock(X->ic_lock)
86 #define ICL_CONN_UNLOCK(X) mtx_unlock(X->ic_lock)
87 #define ICL_CONN_LOCK_ASSERT(X) mtx_assert(X->ic_lock, MA_OWNED)
88 #define ICL_CONN_LOCK_ASSERT_NOT(X) mtx_assert(X->ic_lock, MA_NOTOWNED)
90 STAILQ_HEAD(icl_pdu_stailq, icl_pdu);
92 static icl_conn_new_pdu_t icl_soft_conn_new_pdu;
93 static icl_conn_pdu_free_t icl_soft_conn_pdu_free;
94 static icl_conn_pdu_data_segment_length_t
95 icl_soft_conn_pdu_data_segment_length;
96 static icl_conn_pdu_append_data_t icl_soft_conn_pdu_append_data;
97 static icl_conn_pdu_get_data_t icl_soft_conn_pdu_get_data;
98 static icl_conn_pdu_queue_t icl_soft_conn_pdu_queue;
99 static icl_conn_handoff_t icl_soft_conn_handoff;
100 static icl_conn_free_t icl_soft_conn_free;
101 static icl_conn_close_t icl_soft_conn_close;
102 static icl_conn_task_setup_t icl_soft_conn_task_setup;
103 static icl_conn_task_done_t icl_soft_conn_task_done;
104 static icl_conn_transfer_setup_t icl_soft_conn_transfer_setup;
105 static icl_conn_transfer_done_t icl_soft_conn_transfer_done;
106 #ifdef ICL_KERNEL_PROXY
107 static icl_conn_connect_t icl_soft_conn_connect;
110 static kobj_method_t icl_soft_methods[] = {
111 KOBJMETHOD(icl_conn_new_pdu, icl_soft_conn_new_pdu),
112 KOBJMETHOD(icl_conn_pdu_free, icl_soft_conn_pdu_free),
113 KOBJMETHOD(icl_conn_pdu_data_segment_length,
114 icl_soft_conn_pdu_data_segment_length),
115 KOBJMETHOD(icl_conn_pdu_append_data, icl_soft_conn_pdu_append_data),
116 KOBJMETHOD(icl_conn_pdu_get_data, icl_soft_conn_pdu_get_data),
117 KOBJMETHOD(icl_conn_pdu_queue, icl_soft_conn_pdu_queue),
118 KOBJMETHOD(icl_conn_handoff, icl_soft_conn_handoff),
119 KOBJMETHOD(icl_conn_free, icl_soft_conn_free),
120 KOBJMETHOD(icl_conn_close, icl_soft_conn_close),
121 KOBJMETHOD(icl_conn_task_setup, icl_soft_conn_task_setup),
122 KOBJMETHOD(icl_conn_task_done, icl_soft_conn_task_done),
123 KOBJMETHOD(icl_conn_transfer_setup, icl_soft_conn_transfer_setup),
124 KOBJMETHOD(icl_conn_transfer_done, icl_soft_conn_transfer_done),
125 #ifdef ICL_KERNEL_PROXY
126 KOBJMETHOD(icl_conn_connect, icl_soft_conn_connect),
131 DEFINE_CLASS(icl_soft, icl_soft_methods, sizeof(struct icl_conn));
134 icl_conn_fail(struct icl_conn *ic)
136 if (ic->ic_socket == NULL)
142 ic->ic_socket->so_error = EDOOFUS;
147 icl_conn_receive(struct icl_conn *ic, size_t len)
156 memset(&uio, 0, sizeof(uio));
159 flags = MSG_DONTWAIT;
160 error = soreceive(so, NULL, &uio, &m, NULL, &flags);
162 ICL_DEBUG("soreceive error %d", error);
165 if (uio.uio_resid != 0) {
167 ICL_DEBUG("short read");
175 icl_conn_receive_buf(struct icl_conn *ic, void *buf, size_t len)
184 memset(&uio, 0, sizeof(uio));
185 iov[0].iov_base = buf;
186 iov[0].iov_len = len;
191 uio.uio_segflg = UIO_SYSSPACE;
192 uio.uio_rw = UIO_READ;
194 flags = MSG_DONTWAIT;
195 error = soreceive(so, NULL, &uio, NULL, NULL, &flags);
197 ICL_DEBUG("soreceive error %d", error);
200 if (uio.uio_resid != 0) {
201 ICL_DEBUG("short read");
209 icl_soft_conn_pdu_free(struct icl_conn *ic, struct icl_pdu *ip)
212 m_freem(ip->ip_bhs_mbuf);
213 m_freem(ip->ip_ahs_mbuf);
214 m_freem(ip->ip_data_mbuf);
215 uma_zfree(icl_pdu_zone, ip);
217 refcount_release(&ic->ic_outstanding_pdus);
222 * Allocate icl_pdu with empty BHS to fill up by the caller.
225 icl_soft_conn_new_pdu(struct icl_conn *ic, int flags)
230 refcount_acquire(&ic->ic_outstanding_pdus);
232 ip = uma_zalloc(icl_pdu_zone, flags | M_ZERO);
234 ICL_WARN("failed to allocate %zd bytes", sizeof(*ip));
236 refcount_release(&ic->ic_outstanding_pdus);
242 CTASSERT(sizeof(struct iscsi_bhs) <= MHLEN);
243 ip->ip_bhs_mbuf = m_gethdr(flags, MT_DATA);
244 if (ip->ip_bhs_mbuf == NULL) {
245 ICL_WARN("failed to allocate BHS mbuf");
246 icl_soft_conn_pdu_free(ic, ip);
249 ip->ip_bhs = mtod(ip->ip_bhs_mbuf, struct iscsi_bhs *);
250 memset(ip->ip_bhs, 0, sizeof(struct iscsi_bhs));
251 ip->ip_bhs_mbuf->m_len = sizeof(struct iscsi_bhs);
257 icl_pdu_ahs_length(const struct icl_pdu *request)
260 return (request->ip_bhs->bhs_total_ahs_len * 4);
264 icl_pdu_data_segment_length(const struct icl_pdu *request)
268 len += request->ip_bhs->bhs_data_segment_len[0];
270 len += request->ip_bhs->bhs_data_segment_len[1];
272 len += request->ip_bhs->bhs_data_segment_len[2];
278 icl_soft_conn_pdu_data_segment_length(struct icl_conn *ic,
279 const struct icl_pdu *request)
282 return (icl_pdu_data_segment_length(request));
286 icl_pdu_set_data_segment_length(struct icl_pdu *response, uint32_t len)
289 response->ip_bhs->bhs_data_segment_len[2] = len;
290 response->ip_bhs->bhs_data_segment_len[1] = len >> 8;
291 response->ip_bhs->bhs_data_segment_len[0] = len >> 16;
295 icl_pdu_padding(const struct icl_pdu *ip)
298 if ((ip->ip_data_len % 4) != 0)
299 return (4 - (ip->ip_data_len % 4));
305 icl_pdu_size(const struct icl_pdu *response)
309 KASSERT(response->ip_ahs_len == 0, ("responding with AHS"));
311 len = sizeof(struct iscsi_bhs) + response->ip_data_len +
312 icl_pdu_padding(response);
313 if (response->ip_conn->ic_header_crc32c)
314 len += ISCSI_HEADER_DIGEST_SIZE;
315 if (response->ip_data_len != 0 && response->ip_conn->ic_data_crc32c)
316 len += ISCSI_DATA_DIGEST_SIZE;
322 icl_pdu_receive_bhs(struct icl_pdu *request, size_t *availablep)
325 if (icl_conn_receive_buf(request->ip_conn,
326 request->ip_bhs, sizeof(struct iscsi_bhs))) {
327 ICL_DEBUG("failed to receive BHS");
331 *availablep -= sizeof(struct iscsi_bhs);
336 icl_pdu_receive_ahs(struct icl_pdu *request, size_t *availablep)
339 request->ip_ahs_len = icl_pdu_ahs_length(request);
340 if (request->ip_ahs_len == 0)
343 request->ip_ahs_mbuf = icl_conn_receive(request->ip_conn,
344 request->ip_ahs_len);
345 if (request->ip_ahs_mbuf == NULL) {
346 ICL_DEBUG("failed to receive AHS");
350 *availablep -= request->ip_ahs_len;
355 icl_mbuf_to_crc32c(const struct mbuf *m0)
357 uint32_t digest = 0xffffffff;
358 const struct mbuf *m;
360 for (m = m0; m != NULL; m = m->m_next)
361 digest = calculate_crc32c(digest,
362 mtod(m, const void *), m->m_len);
364 digest = digest ^ 0xffffffff;
370 icl_pdu_check_header_digest(struct icl_pdu *request, size_t *availablep)
372 uint32_t received_digest, valid_digest;
374 if (request->ip_conn->ic_header_crc32c == false)
377 CTASSERT(sizeof(received_digest) == ISCSI_HEADER_DIGEST_SIZE);
378 if (icl_conn_receive_buf(request->ip_conn,
379 &received_digest, ISCSI_HEADER_DIGEST_SIZE)) {
380 ICL_DEBUG("failed to receive header digest");
383 *availablep -= ISCSI_HEADER_DIGEST_SIZE;
385 /* Temporary attach AHS to BHS to calculate header digest. */
386 request->ip_bhs_mbuf->m_next = request->ip_ahs_mbuf;
387 valid_digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
388 request->ip_bhs_mbuf->m_next = NULL;
389 if (received_digest != valid_digest) {
390 ICL_WARN("header digest check failed; got 0x%x, "
391 "should be 0x%x", received_digest, valid_digest);
399 * Return the number of bytes that should be waiting in the receive socket
400 * before icl_pdu_receive_data_segment() gets called.
403 icl_pdu_data_segment_receive_len(const struct icl_pdu *request)
407 len = icl_pdu_data_segment_length(request);
412 * Account for the parts of data segment already read from
415 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
416 len -= request->ip_data_len;
419 * Don't always wait for the full data segment to be delivered
420 * to the socket; this might badly affect performance due to
421 * TCP window scaling.
423 if (len > partial_receive_len) {
425 ICL_DEBUG("need %zd bytes of data, limiting to %zd",
426 len, partial_receive_len));
428 len = partial_receive_len;
434 * Account for padding. Note that due to the way code is written,
435 * the icl_pdu_receive_data_segment() must always receive padding
436 * along with the last part of data segment, because it would be
437 * impossible to tell whether we've already received the full data
438 * segment including padding, or without it.
441 len += 4 - (len % 4);
444 ICL_DEBUG("need %zd bytes of data", len));
451 icl_pdu_receive_data_segment(struct icl_pdu *request,
452 size_t *availablep, bool *more_neededp)
455 size_t len, padding = 0;
458 ic = request->ip_conn;
460 *more_neededp = false;
461 ic->ic_receive_len = 0;
463 len = icl_pdu_data_segment_length(request);
468 padding = 4 - (len % 4);
471 * Account for already received parts of data segment.
473 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
474 len -= request->ip_data_len;
476 if (len + padding > *availablep) {
478 * Not enough data in the socket buffer. Receive as much
479 * as we can. Don't receive padding, since, obviously, it's
480 * not the end of data segment yet.
483 ICL_DEBUG("limited from %zd to %zd",
484 len + padding, *availablep - padding));
486 len = *availablep - padding;
487 *more_neededp = true;
492 * Must not try to receive padding without at least one byte
493 * of actual data segment.
496 m = icl_conn_receive(request->ip_conn, len + padding);
498 ICL_DEBUG("failed to receive data segment");
502 if (request->ip_data_mbuf == NULL)
503 request->ip_data_mbuf = m;
505 m_cat(request->ip_data_mbuf, m);
507 request->ip_data_len += len;
508 *availablep -= len + padding;
514 icl_pdu_data_segment_receive_len(request);
520 icl_pdu_check_data_digest(struct icl_pdu *request, size_t *availablep)
522 uint32_t received_digest, valid_digest;
524 if (request->ip_conn->ic_data_crc32c == false)
527 if (request->ip_data_len == 0)
530 CTASSERT(sizeof(received_digest) == ISCSI_DATA_DIGEST_SIZE);
531 if (icl_conn_receive_buf(request->ip_conn,
532 &received_digest, ISCSI_DATA_DIGEST_SIZE)) {
533 ICL_DEBUG("failed to receive data digest");
536 *availablep -= ISCSI_DATA_DIGEST_SIZE;
539 * Note that ip_data_mbuf also contains padding; since digest
540 * calculation is supposed to include that, we iterate over
541 * the entire ip_data_mbuf chain, not just ip_data_len bytes of it.
543 valid_digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
544 if (received_digest != valid_digest) {
545 ICL_WARN("data digest check failed; got 0x%x, "
546 "should be 0x%x", received_digest, valid_digest);
554 * Somewhat contrary to the name, this attempts to receive only one
555 * "part" of PDU at a time; call it repeatedly until it returns non-NULL.
557 static struct icl_pdu *
558 icl_conn_receive_pdu(struct icl_conn *ic, size_t *availablep)
560 struct icl_pdu *request;
568 if (ic->ic_receive_state == ICL_CONN_STATE_BHS) {
569 KASSERT(ic->ic_receive_pdu == NULL,
570 ("ic->ic_receive_pdu != NULL"));
571 request = icl_soft_conn_new_pdu(ic, M_NOWAIT);
572 if (request == NULL) {
573 ICL_DEBUG("failed to allocate PDU; "
574 "dropping connection");
578 ic->ic_receive_pdu = request;
580 KASSERT(ic->ic_receive_pdu != NULL,
581 ("ic->ic_receive_pdu == NULL"));
582 request = ic->ic_receive_pdu;
585 if (*availablep < ic->ic_receive_len) {
587 ICL_DEBUG("not enough data; need %zd, "
588 "have %zd", ic->ic_receive_len, *availablep);
593 switch (ic->ic_receive_state) {
594 case ICL_CONN_STATE_BHS:
595 //ICL_DEBUG("receiving BHS");
596 error = icl_pdu_receive_bhs(request, availablep);
598 ICL_DEBUG("failed to receive BHS; "
599 "dropping connection");
604 * We don't enforce any limit for AHS length;
605 * its length is stored in 8 bit field.
608 len = icl_pdu_data_segment_length(request);
609 if (len > ic->ic_max_data_segment_length) {
610 ICL_WARN("received data segment "
611 "length %zd is larger than negotiated "
612 "MaxDataSegmentLength %zd; "
613 "dropping connection",
614 len, ic->ic_max_data_segment_length);
619 ic->ic_receive_state = ICL_CONN_STATE_AHS;
620 ic->ic_receive_len = icl_pdu_ahs_length(request);
623 case ICL_CONN_STATE_AHS:
624 //ICL_DEBUG("receiving AHS");
625 error = icl_pdu_receive_ahs(request, availablep);
627 ICL_DEBUG("failed to receive AHS; "
628 "dropping connection");
631 ic->ic_receive_state = ICL_CONN_STATE_HEADER_DIGEST;
632 if (ic->ic_header_crc32c == false)
633 ic->ic_receive_len = 0;
635 ic->ic_receive_len = ISCSI_HEADER_DIGEST_SIZE;
638 case ICL_CONN_STATE_HEADER_DIGEST:
639 //ICL_DEBUG("receiving header digest");
640 error = icl_pdu_check_header_digest(request, availablep);
642 ICL_DEBUG("header digest failed; "
643 "dropping connection");
647 ic->ic_receive_state = ICL_CONN_STATE_DATA;
649 icl_pdu_data_segment_receive_len(request);
652 case ICL_CONN_STATE_DATA:
653 //ICL_DEBUG("receiving data segment");
654 error = icl_pdu_receive_data_segment(request, availablep,
657 ICL_DEBUG("failed to receive data segment;"
658 "dropping connection");
665 ic->ic_receive_state = ICL_CONN_STATE_DATA_DIGEST;
666 if (request->ip_data_len == 0 || ic->ic_data_crc32c == false)
667 ic->ic_receive_len = 0;
669 ic->ic_receive_len = ISCSI_DATA_DIGEST_SIZE;
672 case ICL_CONN_STATE_DATA_DIGEST:
673 //ICL_DEBUG("receiving data digest");
674 error = icl_pdu_check_data_digest(request, availablep);
676 ICL_DEBUG("data digest failed; "
677 "dropping connection");
682 * We've received complete PDU; reset the receive state machine
683 * and return the PDU.
685 ic->ic_receive_state = ICL_CONN_STATE_BHS;
686 ic->ic_receive_len = sizeof(struct iscsi_bhs);
687 ic->ic_receive_pdu = NULL;
691 panic("invalid ic_receive_state %d\n", ic->ic_receive_state);
696 * Don't free the PDU; it's pointed to by ic->ic_receive_pdu
697 * and will get freed in icl_soft_conn_close().
706 icl_conn_receive_pdus(struct icl_conn *ic, size_t available)
708 struct icl_pdu *response;
714 * This can never happen; we're careful to only mess with ic->ic_socket
715 * pointer when the send/receive threads are not running.
717 KASSERT(so != NULL, ("NULL socket"));
720 if (ic->ic_disconnecting)
723 if (so->so_error != 0) {
724 ICL_DEBUG("connection error %d; "
725 "dropping connection", so->so_error);
731 * Loop until we have a complete PDU or there is not enough
732 * data in the socket buffer.
734 if (available < ic->ic_receive_len) {
736 ICL_DEBUG("not enough data; have %zd, "
737 "need %zd", available,
743 response = icl_conn_receive_pdu(ic, &available);
744 if (response == NULL)
747 if (response->ip_ahs_len > 0) {
748 ICL_WARN("received PDU with unsupported "
749 "AHS; opcode 0x%x; dropping connection",
750 response->ip_bhs->bhs_opcode);
751 icl_soft_conn_pdu_free(ic, response);
756 (ic->ic_receive)(response);
761 icl_receive_thread(void *arg)
771 if (ic->ic_disconnecting) {
772 //ICL_DEBUG("terminating");
777 * Set the low watermark, to be checked by
778 * soreadable() in icl_soupcall_receive()
779 * to avoid unnecessary wakeups until there
780 * is enough data received to read the PDU.
782 SOCKBUF_LOCK(&so->so_rcv);
783 available = sbavail(&so->so_rcv);
784 if (available < ic->ic_receive_len) {
785 so->so_rcv.sb_lowat = ic->ic_receive_len;
786 cv_wait(&ic->ic_receive_cv, &so->so_rcv.sb_mtx);
788 so->so_rcv.sb_lowat = so->so_rcv.sb_hiwat + 1;
789 SOCKBUF_UNLOCK(&so->so_rcv);
791 icl_conn_receive_pdus(ic, available);
795 ic->ic_receive_running = false;
796 cv_signal(&ic->ic_send_cv);
802 icl_soupcall_receive(struct socket *so, void *arg, int waitflag)
810 cv_signal(&ic->ic_receive_cv);
815 icl_pdu_finalize(struct icl_pdu *request)
817 size_t padding, pdu_len;
818 uint32_t digest, zero = 0;
822 ic = request->ip_conn;
824 icl_pdu_set_data_segment_length(request, request->ip_data_len);
826 pdu_len = icl_pdu_size(request);
828 if (ic->ic_header_crc32c) {
829 digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
830 ok = m_append(request->ip_bhs_mbuf, sizeof(digest),
833 ICL_WARN("failed to append header digest");
838 if (request->ip_data_len != 0) {
839 padding = icl_pdu_padding(request);
841 ok = m_append(request->ip_data_mbuf, padding,
844 ICL_WARN("failed to append padding");
849 if (ic->ic_data_crc32c) {
850 digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
852 ok = m_append(request->ip_data_mbuf, sizeof(digest),
855 ICL_WARN("failed to append data digest");
860 m_cat(request->ip_bhs_mbuf, request->ip_data_mbuf);
861 request->ip_data_mbuf = NULL;
864 request->ip_bhs_mbuf->m_pkthdr.len = pdu_len;
870 icl_conn_send_pdus(struct icl_conn *ic, struct icl_pdu_stailq *queue)
872 struct icl_pdu *request, *request2;
874 long available, size, size2;
875 int coalesced, error;
877 ICL_CONN_LOCK_ASSERT_NOT(ic);
881 SOCKBUF_LOCK(&so->so_snd);
883 * Check how much space do we have for transmit. We can't just
884 * call sosend() and retry when we get EWOULDBLOCK or EMSGSIZE,
885 * as it always frees the mbuf chain passed to it, even in case
888 available = sbspace(&so->so_snd);
891 * Notify the socket upcall that we don't need wakeups
892 * for the time being.
894 so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1;
895 SOCKBUF_UNLOCK(&so->so_snd);
897 while (!STAILQ_EMPTY(queue)) {
898 request = STAILQ_FIRST(queue);
899 size = icl_pdu_size(request);
900 if (available < size) {
903 * Set the low watermark, to be checked by
904 * sowriteable() in icl_soupcall_send()
905 * to avoid unnecessary wakeups until there
906 * is enough space for the PDU to fit.
908 SOCKBUF_LOCK(&so->so_snd);
909 available = sbspace(&so->so_snd);
910 if (available < size) {
912 ICL_DEBUG("no space to send; "
913 "have %ld, need %ld",
916 so->so_snd.sb_lowat = size;
917 SOCKBUF_UNLOCK(&so->so_snd);
920 SOCKBUF_UNLOCK(&so->so_snd);
922 STAILQ_REMOVE_HEAD(queue, ip_next);
923 error = icl_pdu_finalize(request);
925 ICL_DEBUG("failed to finalize PDU; "
926 "dropping connection");
927 icl_soft_conn_pdu_free(ic, request);
934 request2 = STAILQ_FIRST(queue);
935 if (request2 == NULL)
937 size2 = icl_pdu_size(request2);
938 if (available < size + size2)
940 STAILQ_REMOVE_HEAD(queue, ip_next);
941 error = icl_pdu_finalize(request2);
943 ICL_DEBUG("failed to finalize PDU; "
944 "dropping connection");
945 icl_soft_conn_pdu_free(ic, request);
946 icl_soft_conn_pdu_free(ic, request2);
950 m_cat(request->ip_bhs_mbuf, request2->ip_bhs_mbuf);
951 request2->ip_bhs_mbuf = NULL;
952 request->ip_bhs_mbuf->m_pkthdr.len += size2;
954 STAILQ_REMOVE_AFTER(queue, request, ip_next);
955 icl_soft_conn_pdu_free(ic, request2);
960 ICL_DEBUG("coalesced %d PDUs into %ld bytes",
966 error = sosend(so, NULL, NULL, request->ip_bhs_mbuf,
967 NULL, MSG_DONTWAIT, curthread);
968 request->ip_bhs_mbuf = NULL; /* Sosend consumes the mbuf. */
970 ICL_DEBUG("failed to send PDU, error %d; "
971 "dropping connection", error);
972 icl_soft_conn_pdu_free(ic, request);
976 icl_soft_conn_pdu_free(ic, request);
981 icl_send_thread(void *arg)
984 struct icl_pdu_stailq queue;
994 * If the local queue is empty, populate it from
995 * the main one. This way the icl_conn_send_pdus()
996 * can go through all the queued PDUs without holding
999 if (STAILQ_EMPTY(&queue))
1000 STAILQ_SWAP(&ic->ic_to_send, &queue, icl_pdu);
1002 ic->ic_check_send_space = false;
1003 ICL_CONN_UNLOCK(ic);
1004 icl_conn_send_pdus(ic, &queue);
1008 * The icl_soupcall_send() was called since the last
1009 * call to sbspace(); go around;
1011 if (ic->ic_check_send_space)
1015 * Local queue is empty, but we still have PDUs
1016 * in the main one; go around.
1018 if (STAILQ_EMPTY(&queue) &&
1019 !STAILQ_EMPTY(&ic->ic_to_send))
1023 * There might be some stuff in the local queue,
1024 * which didn't get sent due to not having enough send
1025 * space. Wait for socket upcall.
1030 if (ic->ic_disconnecting) {
1031 //ICL_DEBUG("terminating");
1035 cv_wait(&ic->ic_send_cv, ic->ic_lock);
1039 * We're exiting; move PDUs back to the main queue, so they can
1040 * get freed properly. At this point ordering doesn't matter.
1042 STAILQ_CONCAT(&ic->ic_to_send, &queue);
1044 ic->ic_send_running = false;
1045 cv_signal(&ic->ic_send_cv);
1046 ICL_CONN_UNLOCK(ic);
1051 icl_soupcall_send(struct socket *so, void *arg, int waitflag)
1053 struct icl_conn *ic;
1055 if (!sowriteable(so))
1061 ic->ic_check_send_space = true;
1062 ICL_CONN_UNLOCK(ic);
1064 cv_signal(&ic->ic_send_cv);
1070 icl_soft_conn_pdu_append_data(struct icl_conn *ic, struct icl_pdu *request,
1071 const void *addr, size_t len, int flags)
1073 struct mbuf *mb, *newmb;
1074 size_t copylen, off = 0;
1076 KASSERT(len > 0, ("len == 0"));
1078 newmb = m_getm2(NULL, len, flags, MT_DATA, 0);
1079 if (newmb == NULL) {
1080 ICL_WARN("failed to allocate mbuf for %zd bytes", len);
1084 for (mb = newmb; mb != NULL; mb = mb->m_next) {
1085 copylen = min(M_TRAILINGSPACE(mb), len - off);
1086 memcpy(mtod(mb, char *), (const char *)addr + off, copylen);
1087 mb->m_len = copylen;
1090 KASSERT(off == len, ("%s: off != len", __func__));
1092 if (request->ip_data_mbuf == NULL) {
1093 request->ip_data_mbuf = newmb;
1094 request->ip_data_len = len;
1096 m_cat(request->ip_data_mbuf, newmb);
1097 request->ip_data_len += len;
1104 icl_soft_conn_pdu_get_data(struct icl_conn *ic, struct icl_pdu *ip,
1105 size_t off, void *addr, size_t len)
1108 m_copydata(ip->ip_data_mbuf, off, len, addr);
1112 icl_pdu_queue(struct icl_pdu *ip)
1114 struct icl_conn *ic;
1118 ICL_CONN_LOCK_ASSERT(ic);
1120 if (ic->ic_disconnecting || ic->ic_socket == NULL) {
1121 ICL_DEBUG("icl_pdu_queue on closed connection");
1122 icl_soft_conn_pdu_free(ic, ip);
1126 if (!STAILQ_EMPTY(&ic->ic_to_send)) {
1127 STAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next);
1129 * If the queue is not empty, someone else had already
1130 * signaled the send thread; no need to do that again,
1136 STAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next);
1137 cv_signal(&ic->ic_send_cv);
1141 icl_soft_conn_pdu_queue(struct icl_conn *ic, struct icl_pdu *ip)
1147 static struct icl_conn *
1148 icl_soft_new_conn(const char *name, struct mtx *lock)
1150 struct icl_conn *ic;
1152 refcount_acquire(&icl_ncons);
1154 ic = (struct icl_conn *)kobj_create(&icl_soft_class, M_ICL_SOFT, M_WAITOK | M_ZERO);
1156 STAILQ_INIT(&ic->ic_to_send);
1158 cv_init(&ic->ic_send_cv, "icl_tx");
1159 cv_init(&ic->ic_receive_cv, "icl_rx");
1161 refcount_init(&ic->ic_outstanding_pdus, 0);
1163 ic->ic_max_data_segment_length = ICL_MAX_DATA_SEGMENT_LENGTH;
1165 ic->ic_offload = "None";
1166 ic->ic_unmapped = false;
1172 icl_soft_conn_free(struct icl_conn *ic)
1176 KASSERT(ic->ic_outstanding_pdus == 0,
1177 ("destroying session with %d outstanding PDUs",
1178 ic->ic_outstanding_pdus));
1180 cv_destroy(&ic->ic_send_cv);
1181 cv_destroy(&ic->ic_receive_cv);
1182 kobj_delete((struct kobj *)ic, M_ICL_SOFT);
1183 refcount_release(&icl_ncons);
1187 icl_conn_start(struct icl_conn *ic)
1198 if (ic->ic_socket == NULL) {
1199 ICL_CONN_UNLOCK(ic);
1203 ic->ic_receive_state = ICL_CONN_STATE_BHS;
1204 ic->ic_receive_len = sizeof(struct iscsi_bhs);
1205 ic->ic_disconnecting = false;
1207 ICL_CONN_UNLOCK(ic);
1210 * For sendspace, this is required because the current code cannot
1211 * send a PDU in pieces; thus, the minimum buffer size is equal
1212 * to the maximum PDU size. "+4" is to account for possible padding.
1214 * What we should actually do here is to use autoscaling, but set
1215 * some minimal buffer size to "minspace". I don't know a way to do
1218 minspace = sizeof(struct iscsi_bhs) + ic->ic_max_data_segment_length +
1219 ISCSI_HEADER_DIGEST_SIZE + ISCSI_DATA_DIGEST_SIZE + 4;
1220 if (sendspace < minspace) {
1221 ICL_WARN("kern.icl.sendspace too low; must be at least %zd",
1223 sendspace = minspace;
1225 if (recvspace < minspace) {
1226 ICL_WARN("kern.icl.recvspace too low; must be at least %zd",
1228 recvspace = minspace;
1231 error = soreserve(ic->ic_socket, sendspace, recvspace);
1233 ICL_WARN("soreserve failed with error %d", error);
1234 icl_soft_conn_close(ic);
1237 ic->ic_socket->so_snd.sb_flags |= SB_AUTOSIZE;
1238 ic->ic_socket->so_rcv.sb_flags |= SB_AUTOSIZE;
1243 bzero(&opt, sizeof(opt));
1244 opt.sopt_dir = SOPT_SET;
1245 opt.sopt_level = IPPROTO_TCP;
1246 opt.sopt_name = TCP_NODELAY;
1247 opt.sopt_val = &one;
1248 opt.sopt_valsize = sizeof(one);
1249 error = sosetopt(ic->ic_socket, &opt);
1251 ICL_WARN("disabling TCP_NODELAY failed with error %d", error);
1252 icl_soft_conn_close(ic);
1257 * Register socket upcall, to get notified about incoming PDUs
1258 * and free space to send outgoing ones.
1260 SOCKBUF_LOCK(&ic->ic_socket->so_snd);
1261 soupcall_set(ic->ic_socket, SO_SND, icl_soupcall_send, ic);
1262 SOCKBUF_UNLOCK(&ic->ic_socket->so_snd);
1263 SOCKBUF_LOCK(&ic->ic_socket->so_rcv);
1264 soupcall_set(ic->ic_socket, SO_RCV, icl_soupcall_receive, ic);
1265 SOCKBUF_UNLOCK(&ic->ic_socket->so_rcv);
1271 ic->ic_send_running = ic->ic_receive_running = true;
1272 ICL_CONN_UNLOCK(ic);
1273 error = kthread_add(icl_send_thread, ic, NULL, NULL, 0, 0, "%stx",
1276 ICL_WARN("kthread_add(9) failed with error %d", error);
1278 ic->ic_send_running = ic->ic_receive_running = false;
1279 cv_signal(&ic->ic_send_cv);
1280 ICL_CONN_UNLOCK(ic);
1281 icl_soft_conn_close(ic);
1284 error = kthread_add(icl_receive_thread, ic, NULL, NULL, 0, 0, "%srx",
1287 ICL_WARN("kthread_add(9) failed with error %d", error);
1289 ic->ic_receive_running = false;
1290 cv_signal(&ic->ic_send_cv);
1291 ICL_CONN_UNLOCK(ic);
1292 icl_soft_conn_close(ic);
1300 icl_soft_conn_handoff(struct icl_conn *ic, int fd)
1304 cap_rights_t rights;
1307 ICL_CONN_LOCK_ASSERT_NOT(ic);
1309 #ifdef ICL_KERNEL_PROXY
1311 * We're transitioning to Full Feature phase, and we don't
1316 if (ic->ic_socket == NULL) {
1317 ICL_CONN_UNLOCK(ic);
1318 ICL_WARN("proxy handoff without connect");
1321 ICL_CONN_UNLOCK(ic);
1327 * Steal the socket from userland.
1329 error = fget(curthread, fd,
1330 cap_rights_init(&rights, CAP_SOCK_CLIENT), &fp);
1333 if (fp->f_type != DTYPE_SOCKET) {
1334 fdrop(fp, curthread);
1338 if (so->so_type != SOCK_STREAM) {
1339 fdrop(fp, curthread);
1345 if (ic->ic_socket != NULL) {
1346 ICL_CONN_UNLOCK(ic);
1347 fdrop(fp, curthread);
1351 ic->ic_socket = fp->f_data;
1352 fp->f_ops = &badfileops;
1354 fdrop(fp, curthread);
1355 ICL_CONN_UNLOCK(ic);
1357 error = icl_conn_start(ic);
1363 icl_soft_conn_close(struct icl_conn *ic)
1365 struct icl_pdu *pdu;
1371 * Wake up the threads, so they can properly terminate.
1373 ic->ic_disconnecting = true;
1374 while (ic->ic_receive_running || ic->ic_send_running) {
1375 cv_signal(&ic->ic_receive_cv);
1376 cv_signal(&ic->ic_send_cv);
1377 cv_wait(&ic->ic_send_cv, ic->ic_lock);
1380 /* Some other thread could close the connection same time. */
1383 ICL_CONN_UNLOCK(ic);
1386 ic->ic_socket = NULL;
1389 * Deregister socket upcalls.
1391 ICL_CONN_UNLOCK(ic);
1392 SOCKBUF_LOCK(&so->so_snd);
1393 if (so->so_snd.sb_upcall != NULL)
1394 soupcall_clear(so, SO_SND);
1395 SOCKBUF_UNLOCK(&so->so_snd);
1396 SOCKBUF_LOCK(&so->so_rcv);
1397 if (so->so_rcv.sb_upcall != NULL)
1398 soupcall_clear(so, SO_RCV);
1399 SOCKBUF_UNLOCK(&so->so_rcv);
1403 if (ic->ic_receive_pdu != NULL) {
1404 //ICL_DEBUG("freeing partially received PDU");
1405 icl_soft_conn_pdu_free(ic, ic->ic_receive_pdu);
1406 ic->ic_receive_pdu = NULL;
1410 * Remove any outstanding PDUs from the send queue.
1412 while (!STAILQ_EMPTY(&ic->ic_to_send)) {
1413 pdu = STAILQ_FIRST(&ic->ic_to_send);
1414 STAILQ_REMOVE_HEAD(&ic->ic_to_send, ip_next);
1415 icl_soft_conn_pdu_free(ic, pdu);
1418 KASSERT(STAILQ_EMPTY(&ic->ic_to_send),
1419 ("destroying session with non-empty send queue"));
1420 ICL_CONN_UNLOCK(ic);
1424 icl_soft_conn_task_setup(struct icl_conn *ic, struct icl_pdu *ip,
1425 struct ccb_scsiio *csio, uint32_t *task_tagp, void **prvp)
1432 icl_soft_conn_task_done(struct icl_conn *ic, void *prv)
1437 icl_soft_conn_transfer_setup(struct icl_conn *ic, union ctl_io *io,
1438 uint32_t *transfer_tag, void **prvp)
1445 icl_soft_conn_transfer_done(struct icl_conn *ic, void *prv)
1450 icl_soft_limits(struct icl_drv_limits *idl)
1453 idl->idl_max_recv_data_segment_length = 128 * 1024;
1454 idl->idl_max_send_data_segment_length = 128 * 1024;
1455 idl->idl_max_burst_length = 262144;
1456 idl->idl_first_burst_length = 65536;
1461 #ifdef ICL_KERNEL_PROXY
1463 icl_soft_conn_connect(struct icl_conn *ic, int domain, int socktype,
1464 int protocol, struct sockaddr *from_sa, struct sockaddr *to_sa)
1467 return (icl_soft_proxy_connect(ic, domain, socktype, protocol,
1472 icl_soft_handoff_sock(struct icl_conn *ic, struct socket *so)
1476 ICL_CONN_LOCK_ASSERT_NOT(ic);
1478 if (so->so_type != SOCK_STREAM)
1482 if (ic->ic_socket != NULL) {
1483 ICL_CONN_UNLOCK(ic);
1487 ICL_CONN_UNLOCK(ic);
1489 error = icl_conn_start(ic);
1493 #endif /* ICL_KERNEL_PROXY */
1500 icl_pdu_zone = uma_zcreate("icl_pdu",
1501 sizeof(struct icl_pdu), NULL, NULL, NULL, NULL,
1503 refcount_init(&icl_ncons, 0);
1506 * The reason we call this "none" is that to the user,
1507 * it's known as "offload driver"; "offload driver: soft"
1508 * doesn't make much sense.
1510 error = icl_register("none", false, 0,
1511 icl_soft_limits, icl_soft_new_conn);
1512 KASSERT(error == 0, ("failed to register"));
1514 #if defined(ICL_KERNEL_PROXY) && 0
1516 * Debugging aid for kernel proxy functionality.
1518 error = icl_register("proxytest", true, 0,
1519 icl_soft_limits, icl_soft_new_conn);
1520 KASSERT(error == 0, ("failed to register"));
1527 icl_soft_unload(void)
1533 icl_unregister("none", false);
1534 #if defined(ICL_KERNEL_PROXY) && 0
1535 icl_unregister("proxytest", true);
1538 uma_zdestroy(icl_pdu_zone);
1544 icl_soft_modevent(module_t mod, int what, void *arg)
1549 return (icl_soft_load());
1551 return (icl_soft_unload());
1557 moduledata_t icl_soft_data = {
1563 DECLARE_MODULE(icl_soft, icl_soft_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1564 MODULE_DEPEND(icl_soft, icl, 1, 1, 1);
1565 MODULE_VERSION(icl_soft, 1);