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>
44 #include <sys/gsb_crc32.h>
46 #include <sys/kernel.h>
47 #include <sys/kthread.h>
50 #include <sys/mutex.h>
51 #include <sys/module.h>
52 #include <sys/protosw.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/sysctl.h>
56 #include <sys/systm.h>
60 #include <netinet/in.h>
61 #include <netinet/tcp.h>
63 #include <dev/iscsi/icl.h>
64 #include <dev/iscsi/iscsi_proto.h>
65 #include <icl_conn_if.h>
67 static int coalesce = 1;
68 SYSCTL_INT(_kern_icl, OID_AUTO, coalesce, CTLFLAG_RWTUN,
69 &coalesce, 0, "Try to coalesce PDUs before sending");
70 static int partial_receive_len = 128 * 1024;
71 SYSCTL_INT(_kern_icl, OID_AUTO, partial_receive_len, CTLFLAG_RWTUN,
72 &partial_receive_len, 0, "Minimum read size for partially received "
74 static int sendspace = 1048576;
75 SYSCTL_INT(_kern_icl, OID_AUTO, sendspace, CTLFLAG_RWTUN,
76 &sendspace, 0, "Default send socket buffer size");
77 static int recvspace = 1048576;
78 SYSCTL_INT(_kern_icl, OID_AUTO, recvspace, CTLFLAG_RWTUN,
79 &recvspace, 0, "Default receive socket buffer size");
81 static MALLOC_DEFINE(M_ICL_SOFT, "icl_soft", "iSCSI software backend");
82 static uma_zone_t icl_pdu_zone;
84 static volatile u_int icl_ncons;
86 #define ICL_CONN_LOCK(X) mtx_lock(X->ic_lock)
87 #define ICL_CONN_UNLOCK(X) mtx_unlock(X->ic_lock)
88 #define ICL_CONN_LOCK_ASSERT(X) mtx_assert(X->ic_lock, MA_OWNED)
89 #define ICL_CONN_LOCK_ASSERT_NOT(X) mtx_assert(X->ic_lock, MA_NOTOWNED)
91 STAILQ_HEAD(icl_pdu_stailq, icl_pdu);
93 static icl_conn_new_pdu_t icl_soft_conn_new_pdu;
94 static icl_conn_pdu_free_t icl_soft_conn_pdu_free;
95 static icl_conn_pdu_data_segment_length_t
96 icl_soft_conn_pdu_data_segment_length;
97 static icl_conn_pdu_append_data_t icl_soft_conn_pdu_append_data;
98 static icl_conn_pdu_get_data_t icl_soft_conn_pdu_get_data;
99 static icl_conn_pdu_queue_t icl_soft_conn_pdu_queue;
100 static icl_conn_handoff_t icl_soft_conn_handoff;
101 static icl_conn_free_t icl_soft_conn_free;
102 static icl_conn_close_t icl_soft_conn_close;
103 static icl_conn_task_setup_t icl_soft_conn_task_setup;
104 static icl_conn_task_done_t icl_soft_conn_task_done;
105 static icl_conn_transfer_setup_t icl_soft_conn_transfer_setup;
106 static icl_conn_transfer_done_t icl_soft_conn_transfer_done;
107 #ifdef ICL_KERNEL_PROXY
108 static icl_conn_connect_t icl_soft_conn_connect;
111 static kobj_method_t icl_soft_methods[] = {
112 KOBJMETHOD(icl_conn_new_pdu, icl_soft_conn_new_pdu),
113 KOBJMETHOD(icl_conn_pdu_free, icl_soft_conn_pdu_free),
114 KOBJMETHOD(icl_conn_pdu_data_segment_length,
115 icl_soft_conn_pdu_data_segment_length),
116 KOBJMETHOD(icl_conn_pdu_append_data, icl_soft_conn_pdu_append_data),
117 KOBJMETHOD(icl_conn_pdu_get_data, icl_soft_conn_pdu_get_data),
118 KOBJMETHOD(icl_conn_pdu_queue, icl_soft_conn_pdu_queue),
119 KOBJMETHOD(icl_conn_handoff, icl_soft_conn_handoff),
120 KOBJMETHOD(icl_conn_free, icl_soft_conn_free),
121 KOBJMETHOD(icl_conn_close, icl_soft_conn_close),
122 KOBJMETHOD(icl_conn_task_setup, icl_soft_conn_task_setup),
123 KOBJMETHOD(icl_conn_task_done, icl_soft_conn_task_done),
124 KOBJMETHOD(icl_conn_transfer_setup, icl_soft_conn_transfer_setup),
125 KOBJMETHOD(icl_conn_transfer_done, icl_soft_conn_transfer_done),
126 #ifdef ICL_KERNEL_PROXY
127 KOBJMETHOD(icl_conn_connect, icl_soft_conn_connect),
132 DEFINE_CLASS(icl_soft, icl_soft_methods, sizeof(struct icl_conn));
135 icl_conn_fail(struct icl_conn *ic)
137 if (ic->ic_socket == NULL)
143 ic->ic_socket->so_error = EDOOFUS;
148 icl_conn_receive(struct icl_conn *ic, size_t len)
157 memset(&uio, 0, sizeof(uio));
160 flags = MSG_DONTWAIT;
161 error = soreceive(so, NULL, &uio, &m, NULL, &flags);
163 ICL_DEBUG("soreceive error %d", error);
166 if (uio.uio_resid != 0) {
168 ICL_DEBUG("short read");
176 icl_conn_receive_buf(struct icl_conn *ic, void *buf, size_t len)
185 memset(&uio, 0, sizeof(uio));
186 iov[0].iov_base = buf;
187 iov[0].iov_len = len;
192 uio.uio_segflg = UIO_SYSSPACE;
193 uio.uio_rw = UIO_READ;
195 flags = MSG_DONTWAIT;
196 error = soreceive(so, NULL, &uio, NULL, NULL, &flags);
198 ICL_DEBUG("soreceive error %d", error);
201 if (uio.uio_resid != 0) {
202 ICL_DEBUG("short read");
210 icl_soft_conn_pdu_free(struct icl_conn *ic, struct icl_pdu *ip)
213 m_freem(ip->ip_bhs_mbuf);
214 m_freem(ip->ip_ahs_mbuf);
215 m_freem(ip->ip_data_mbuf);
216 uma_zfree(icl_pdu_zone, ip);
218 refcount_release(&ic->ic_outstanding_pdus);
223 * Allocate icl_pdu with empty BHS to fill up by the caller.
226 icl_soft_conn_new_pdu(struct icl_conn *ic, int flags)
231 refcount_acquire(&ic->ic_outstanding_pdus);
233 ip = uma_zalloc(icl_pdu_zone, flags | M_ZERO);
235 ICL_WARN("failed to allocate %zd bytes", sizeof(*ip));
237 refcount_release(&ic->ic_outstanding_pdus);
243 CTASSERT(sizeof(struct iscsi_bhs) <= MHLEN);
244 ip->ip_bhs_mbuf = m_gethdr(flags, MT_DATA);
245 if (ip->ip_bhs_mbuf == NULL) {
246 ICL_WARN("failed to allocate BHS mbuf");
247 icl_soft_conn_pdu_free(ic, ip);
250 ip->ip_bhs = mtod(ip->ip_bhs_mbuf, struct iscsi_bhs *);
251 memset(ip->ip_bhs, 0, sizeof(struct iscsi_bhs));
252 ip->ip_bhs_mbuf->m_len = sizeof(struct iscsi_bhs);
258 icl_pdu_ahs_length(const struct icl_pdu *request)
261 return (request->ip_bhs->bhs_total_ahs_len * 4);
265 icl_pdu_data_segment_length(const struct icl_pdu *request)
269 len += request->ip_bhs->bhs_data_segment_len[0];
271 len += request->ip_bhs->bhs_data_segment_len[1];
273 len += request->ip_bhs->bhs_data_segment_len[2];
279 icl_soft_conn_pdu_data_segment_length(struct icl_conn *ic,
280 const struct icl_pdu *request)
283 return (icl_pdu_data_segment_length(request));
287 icl_pdu_set_data_segment_length(struct icl_pdu *response, uint32_t len)
290 response->ip_bhs->bhs_data_segment_len[2] = len;
291 response->ip_bhs->bhs_data_segment_len[1] = len >> 8;
292 response->ip_bhs->bhs_data_segment_len[0] = len >> 16;
296 icl_pdu_padding(const struct icl_pdu *ip)
299 if ((ip->ip_data_len % 4) != 0)
300 return (4 - (ip->ip_data_len % 4));
306 icl_pdu_size(const struct icl_pdu *response)
310 KASSERT(response->ip_ahs_len == 0, ("responding with AHS"));
312 len = sizeof(struct iscsi_bhs) + response->ip_data_len +
313 icl_pdu_padding(response);
314 if (response->ip_conn->ic_header_crc32c)
315 len += ISCSI_HEADER_DIGEST_SIZE;
316 if (response->ip_data_len != 0 && response->ip_conn->ic_data_crc32c)
317 len += ISCSI_DATA_DIGEST_SIZE;
323 icl_pdu_receive_bhs(struct icl_pdu *request, size_t *availablep)
326 if (icl_conn_receive_buf(request->ip_conn,
327 request->ip_bhs, sizeof(struct iscsi_bhs))) {
328 ICL_DEBUG("failed to receive BHS");
332 *availablep -= sizeof(struct iscsi_bhs);
337 icl_pdu_receive_ahs(struct icl_pdu *request, size_t *availablep)
340 request->ip_ahs_len = icl_pdu_ahs_length(request);
341 if (request->ip_ahs_len == 0)
344 request->ip_ahs_mbuf = icl_conn_receive(request->ip_conn,
345 request->ip_ahs_len);
346 if (request->ip_ahs_mbuf == NULL) {
347 ICL_DEBUG("failed to receive AHS");
351 *availablep -= request->ip_ahs_len;
356 icl_mbuf_to_crc32c(const struct mbuf *m0)
358 uint32_t digest = 0xffffffff;
359 const struct mbuf *m;
361 for (m = m0; m != NULL; m = m->m_next)
362 digest = calculate_crc32c(digest,
363 mtod(m, const void *), m->m_len);
365 digest = digest ^ 0xffffffff;
371 icl_pdu_check_header_digest(struct icl_pdu *request, size_t *availablep)
373 uint32_t received_digest, valid_digest;
375 if (request->ip_conn->ic_header_crc32c == false)
378 CTASSERT(sizeof(received_digest) == ISCSI_HEADER_DIGEST_SIZE);
379 if (icl_conn_receive_buf(request->ip_conn,
380 &received_digest, ISCSI_HEADER_DIGEST_SIZE)) {
381 ICL_DEBUG("failed to receive header digest");
384 *availablep -= ISCSI_HEADER_DIGEST_SIZE;
386 /* Temporary attach AHS to BHS to calculate header digest. */
387 request->ip_bhs_mbuf->m_next = request->ip_ahs_mbuf;
388 valid_digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
389 request->ip_bhs_mbuf->m_next = NULL;
390 if (received_digest != valid_digest) {
391 ICL_WARN("header digest check failed; got 0x%x, "
392 "should be 0x%x", received_digest, valid_digest);
400 * Return the number of bytes that should be waiting in the receive socket
401 * before icl_pdu_receive_data_segment() gets called.
404 icl_pdu_data_segment_receive_len(const struct icl_pdu *request)
408 len = icl_pdu_data_segment_length(request);
413 * Account for the parts of data segment already read from
416 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
417 len -= request->ip_data_len;
420 * Don't always wait for the full data segment to be delivered
421 * to the socket; this might badly affect performance due to
422 * TCP window scaling.
424 if (len > partial_receive_len) {
426 ICL_DEBUG("need %zd bytes of data, limiting to %zd",
427 len, partial_receive_len));
429 len = partial_receive_len;
435 * Account for padding. Note that due to the way code is written,
436 * the icl_pdu_receive_data_segment() must always receive padding
437 * along with the last part of data segment, because it would be
438 * impossible to tell whether we've already received the full data
439 * segment including padding, or without it.
442 len += 4 - (len % 4);
445 ICL_DEBUG("need %zd bytes of data", len));
452 icl_pdu_receive_data_segment(struct icl_pdu *request,
453 size_t *availablep, bool *more_neededp)
456 size_t len, padding = 0;
459 ic = request->ip_conn;
461 *more_neededp = false;
462 ic->ic_receive_len = 0;
464 len = icl_pdu_data_segment_length(request);
469 padding = 4 - (len % 4);
472 * Account for already received parts of data segment.
474 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
475 len -= request->ip_data_len;
477 if (len + padding > *availablep) {
479 * Not enough data in the socket buffer. Receive as much
480 * as we can. Don't receive padding, since, obviously, it's
481 * not the end of data segment yet.
484 ICL_DEBUG("limited from %zd to %zd",
485 len + padding, *availablep - padding));
487 len = *availablep - padding;
488 *more_neededp = true;
493 * Must not try to receive padding without at least one byte
494 * of actual data segment.
497 m = icl_conn_receive(request->ip_conn, len + padding);
499 ICL_DEBUG("failed to receive data segment");
503 if (request->ip_data_mbuf == NULL)
504 request->ip_data_mbuf = m;
506 m_cat(request->ip_data_mbuf, m);
508 request->ip_data_len += len;
509 *availablep -= len + padding;
515 icl_pdu_data_segment_receive_len(request);
521 icl_pdu_check_data_digest(struct icl_pdu *request, size_t *availablep)
523 uint32_t received_digest, valid_digest;
525 if (request->ip_conn->ic_data_crc32c == false)
528 if (request->ip_data_len == 0)
531 CTASSERT(sizeof(received_digest) == ISCSI_DATA_DIGEST_SIZE);
532 if (icl_conn_receive_buf(request->ip_conn,
533 &received_digest, ISCSI_DATA_DIGEST_SIZE)) {
534 ICL_DEBUG("failed to receive data digest");
537 *availablep -= ISCSI_DATA_DIGEST_SIZE;
540 * Note that ip_data_mbuf also contains padding; since digest
541 * calculation is supposed to include that, we iterate over
542 * the entire ip_data_mbuf chain, not just ip_data_len bytes of it.
544 valid_digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
545 if (received_digest != valid_digest) {
546 ICL_WARN("data digest check failed; got 0x%x, "
547 "should be 0x%x", received_digest, valid_digest);
555 * Somewhat contrary to the name, this attempts to receive only one
556 * "part" of PDU at a time; call it repeatedly until it returns non-NULL.
558 static struct icl_pdu *
559 icl_conn_receive_pdu(struct icl_conn *ic, size_t *availablep)
561 struct icl_pdu *request;
569 if (ic->ic_receive_state == ICL_CONN_STATE_BHS) {
570 KASSERT(ic->ic_receive_pdu == NULL,
571 ("ic->ic_receive_pdu != NULL"));
572 request = icl_soft_conn_new_pdu(ic, M_NOWAIT);
573 if (request == NULL) {
574 ICL_DEBUG("failed to allocate PDU; "
575 "dropping connection");
579 ic->ic_receive_pdu = request;
581 KASSERT(ic->ic_receive_pdu != NULL,
582 ("ic->ic_receive_pdu == NULL"));
583 request = ic->ic_receive_pdu;
586 if (*availablep < ic->ic_receive_len) {
588 ICL_DEBUG("not enough data; need %zd, "
589 "have %zd", ic->ic_receive_len, *availablep);
594 switch (ic->ic_receive_state) {
595 case ICL_CONN_STATE_BHS:
596 //ICL_DEBUG("receiving BHS");
597 error = icl_pdu_receive_bhs(request, availablep);
599 ICL_DEBUG("failed to receive BHS; "
600 "dropping connection");
605 * We don't enforce any limit for AHS length;
606 * its length is stored in 8 bit field.
609 len = icl_pdu_data_segment_length(request);
610 if (len > ic->ic_max_data_segment_length) {
611 ICL_WARN("received data segment "
612 "length %zd is larger than negotiated "
613 "MaxDataSegmentLength %zd; "
614 "dropping connection",
615 len, ic->ic_max_data_segment_length);
620 ic->ic_receive_state = ICL_CONN_STATE_AHS;
621 ic->ic_receive_len = icl_pdu_ahs_length(request);
624 case ICL_CONN_STATE_AHS:
625 //ICL_DEBUG("receiving AHS");
626 error = icl_pdu_receive_ahs(request, availablep);
628 ICL_DEBUG("failed to receive AHS; "
629 "dropping connection");
632 ic->ic_receive_state = ICL_CONN_STATE_HEADER_DIGEST;
633 if (ic->ic_header_crc32c == false)
634 ic->ic_receive_len = 0;
636 ic->ic_receive_len = ISCSI_HEADER_DIGEST_SIZE;
639 case ICL_CONN_STATE_HEADER_DIGEST:
640 //ICL_DEBUG("receiving header digest");
641 error = icl_pdu_check_header_digest(request, availablep);
643 ICL_DEBUG("header digest failed; "
644 "dropping connection");
648 ic->ic_receive_state = ICL_CONN_STATE_DATA;
650 icl_pdu_data_segment_receive_len(request);
653 case ICL_CONN_STATE_DATA:
654 //ICL_DEBUG("receiving data segment");
655 error = icl_pdu_receive_data_segment(request, availablep,
658 ICL_DEBUG("failed to receive data segment;"
659 "dropping connection");
666 ic->ic_receive_state = ICL_CONN_STATE_DATA_DIGEST;
667 if (request->ip_data_len == 0 || ic->ic_data_crc32c == false)
668 ic->ic_receive_len = 0;
670 ic->ic_receive_len = ISCSI_DATA_DIGEST_SIZE;
673 case ICL_CONN_STATE_DATA_DIGEST:
674 //ICL_DEBUG("receiving data digest");
675 error = icl_pdu_check_data_digest(request, availablep);
677 ICL_DEBUG("data digest failed; "
678 "dropping connection");
683 * We've received complete PDU; reset the receive state machine
684 * and return the PDU.
686 ic->ic_receive_state = ICL_CONN_STATE_BHS;
687 ic->ic_receive_len = sizeof(struct iscsi_bhs);
688 ic->ic_receive_pdu = NULL;
692 panic("invalid ic_receive_state %d\n", ic->ic_receive_state);
697 * Don't free the PDU; it's pointed to by ic->ic_receive_pdu
698 * and will get freed in icl_soft_conn_close().
707 icl_conn_receive_pdus(struct icl_conn *ic, size_t available)
709 struct icl_pdu *response;
715 * This can never happen; we're careful to only mess with ic->ic_socket
716 * pointer when the send/receive threads are not running.
718 KASSERT(so != NULL, ("NULL socket"));
721 if (ic->ic_disconnecting)
724 if (so->so_error != 0) {
725 ICL_DEBUG("connection error %d; "
726 "dropping connection", so->so_error);
732 * Loop until we have a complete PDU or there is not enough
733 * data in the socket buffer.
735 if (available < ic->ic_receive_len) {
737 ICL_DEBUG("not enough data; have %zd, "
738 "need %zd", available,
744 response = icl_conn_receive_pdu(ic, &available);
745 if (response == NULL)
748 if (response->ip_ahs_len > 0) {
749 ICL_WARN("received PDU with unsupported "
750 "AHS; opcode 0x%x; dropping connection",
751 response->ip_bhs->bhs_opcode);
752 icl_soft_conn_pdu_free(ic, response);
757 (ic->ic_receive)(response);
762 icl_receive_thread(void *arg)
772 if (ic->ic_disconnecting) {
773 //ICL_DEBUG("terminating");
778 * Set the low watermark, to be checked by
779 * soreadable() in icl_soupcall_receive()
780 * to avoid unnecessary wakeups until there
781 * is enough data received to read the PDU.
783 SOCKBUF_LOCK(&so->so_rcv);
784 available = sbavail(&so->so_rcv);
785 if (available < ic->ic_receive_len) {
786 so->so_rcv.sb_lowat = ic->ic_receive_len;
787 cv_wait(&ic->ic_receive_cv, &so->so_rcv.sb_mtx);
789 so->so_rcv.sb_lowat = so->so_rcv.sb_hiwat + 1;
790 SOCKBUF_UNLOCK(&so->so_rcv);
792 icl_conn_receive_pdus(ic, available);
796 ic->ic_receive_running = false;
797 cv_signal(&ic->ic_send_cv);
803 icl_soupcall_receive(struct socket *so, void *arg, int waitflag)
811 cv_signal(&ic->ic_receive_cv);
816 icl_pdu_finalize(struct icl_pdu *request)
818 size_t padding, pdu_len;
819 uint32_t digest, zero = 0;
823 ic = request->ip_conn;
825 icl_pdu_set_data_segment_length(request, request->ip_data_len);
827 pdu_len = icl_pdu_size(request);
829 if (ic->ic_header_crc32c) {
830 digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
831 ok = m_append(request->ip_bhs_mbuf, sizeof(digest),
834 ICL_WARN("failed to append header digest");
839 if (request->ip_data_len != 0) {
840 padding = icl_pdu_padding(request);
842 ok = m_append(request->ip_data_mbuf, padding,
845 ICL_WARN("failed to append padding");
850 if (ic->ic_data_crc32c) {
851 digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
853 ok = m_append(request->ip_data_mbuf, sizeof(digest),
856 ICL_WARN("failed to append data digest");
861 m_cat(request->ip_bhs_mbuf, request->ip_data_mbuf);
862 request->ip_data_mbuf = NULL;
865 request->ip_bhs_mbuf->m_pkthdr.len = pdu_len;
871 icl_conn_send_pdus(struct icl_conn *ic, struct icl_pdu_stailq *queue)
873 struct icl_pdu *request, *request2;
875 long available, size, size2;
876 int coalesced, error;
878 ICL_CONN_LOCK_ASSERT_NOT(ic);
882 SOCKBUF_LOCK(&so->so_snd);
884 * Check how much space do we have for transmit. We can't just
885 * call sosend() and retry when we get EWOULDBLOCK or EMSGSIZE,
886 * as it always frees the mbuf chain passed to it, even in case
889 available = sbspace(&so->so_snd);
892 * Notify the socket upcall that we don't need wakeups
893 * for the time being.
895 so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1;
896 SOCKBUF_UNLOCK(&so->so_snd);
898 while (!STAILQ_EMPTY(queue)) {
899 request = STAILQ_FIRST(queue);
900 size = icl_pdu_size(request);
901 if (available < size) {
904 * Set the low watermark, to be checked by
905 * sowriteable() in icl_soupcall_send()
906 * to avoid unnecessary wakeups until there
907 * is enough space for the PDU to fit.
909 SOCKBUF_LOCK(&so->so_snd);
910 available = sbspace(&so->so_snd);
911 if (available < size) {
913 ICL_DEBUG("no space to send; "
914 "have %ld, need %ld",
917 so->so_snd.sb_lowat = size;
918 SOCKBUF_UNLOCK(&so->so_snd);
921 SOCKBUF_UNLOCK(&so->so_snd);
923 STAILQ_REMOVE_HEAD(queue, ip_next);
924 error = icl_pdu_finalize(request);
926 ICL_DEBUG("failed to finalize PDU; "
927 "dropping connection");
928 icl_soft_conn_pdu_free(ic, request);
935 request2 = STAILQ_FIRST(queue);
936 if (request2 == NULL)
938 size2 = icl_pdu_size(request2);
939 if (available < size + size2)
941 STAILQ_REMOVE_HEAD(queue, ip_next);
942 error = icl_pdu_finalize(request2);
944 ICL_DEBUG("failed to finalize PDU; "
945 "dropping connection");
946 icl_soft_conn_pdu_free(ic, request);
947 icl_soft_conn_pdu_free(ic, request2);
951 m_cat(request->ip_bhs_mbuf, request2->ip_bhs_mbuf);
952 request2->ip_bhs_mbuf = NULL;
953 request->ip_bhs_mbuf->m_pkthdr.len += size2;
955 STAILQ_REMOVE_AFTER(queue, request, ip_next);
956 icl_soft_conn_pdu_free(ic, request2);
961 ICL_DEBUG("coalesced %d PDUs into %ld bytes",
967 error = sosend(so, NULL, NULL, request->ip_bhs_mbuf,
968 NULL, MSG_DONTWAIT, curthread);
969 request->ip_bhs_mbuf = NULL; /* Sosend consumes the mbuf. */
971 ICL_DEBUG("failed to send PDU, error %d; "
972 "dropping connection", error);
973 icl_soft_conn_pdu_free(ic, request);
977 icl_soft_conn_pdu_free(ic, request);
982 icl_send_thread(void *arg)
985 struct icl_pdu_stailq queue;
995 * If the local queue is empty, populate it from
996 * the main one. This way the icl_conn_send_pdus()
997 * can go through all the queued PDUs without holding
1000 if (STAILQ_EMPTY(&queue))
1001 STAILQ_SWAP(&ic->ic_to_send, &queue, icl_pdu);
1003 ic->ic_check_send_space = false;
1004 ICL_CONN_UNLOCK(ic);
1005 icl_conn_send_pdus(ic, &queue);
1009 * The icl_soupcall_send() was called since the last
1010 * call to sbspace(); go around;
1012 if (ic->ic_check_send_space)
1016 * Local queue is empty, but we still have PDUs
1017 * in the main one; go around.
1019 if (STAILQ_EMPTY(&queue) &&
1020 !STAILQ_EMPTY(&ic->ic_to_send))
1024 * There might be some stuff in the local queue,
1025 * which didn't get sent due to not having enough send
1026 * space. Wait for socket upcall.
1031 if (ic->ic_disconnecting) {
1032 //ICL_DEBUG("terminating");
1036 cv_wait(&ic->ic_send_cv, ic->ic_lock);
1040 * We're exiting; move PDUs back to the main queue, so they can
1041 * get freed properly. At this point ordering doesn't matter.
1043 STAILQ_CONCAT(&ic->ic_to_send, &queue);
1045 ic->ic_send_running = false;
1046 cv_signal(&ic->ic_send_cv);
1047 ICL_CONN_UNLOCK(ic);
1052 icl_soupcall_send(struct socket *so, void *arg, int waitflag)
1054 struct icl_conn *ic;
1056 if (!sowriteable(so))
1062 ic->ic_check_send_space = true;
1063 ICL_CONN_UNLOCK(ic);
1065 cv_signal(&ic->ic_send_cv);
1071 icl_soft_conn_pdu_append_data(struct icl_conn *ic, struct icl_pdu *request,
1072 const void *addr, size_t len, int flags)
1074 struct mbuf *mb, *newmb;
1075 size_t copylen, off = 0;
1077 KASSERT(len > 0, ("len == 0"));
1079 newmb = m_getm2(NULL, len, flags, MT_DATA, 0);
1080 if (newmb == NULL) {
1081 ICL_WARN("failed to allocate mbuf for %zd bytes", len);
1085 for (mb = newmb; mb != NULL; mb = mb->m_next) {
1086 copylen = min(M_TRAILINGSPACE(mb), len - off);
1087 memcpy(mtod(mb, char *), (const char *)addr + off, copylen);
1088 mb->m_len = copylen;
1091 KASSERT(off == len, ("%s: off != len", __func__));
1093 if (request->ip_data_mbuf == NULL) {
1094 request->ip_data_mbuf = newmb;
1095 request->ip_data_len = len;
1097 m_cat(request->ip_data_mbuf, newmb);
1098 request->ip_data_len += len;
1105 icl_soft_conn_pdu_get_data(struct icl_conn *ic, struct icl_pdu *ip,
1106 size_t off, void *addr, size_t len)
1109 m_copydata(ip->ip_data_mbuf, off, len, addr);
1113 icl_pdu_queue(struct icl_pdu *ip)
1115 struct icl_conn *ic;
1119 ICL_CONN_LOCK_ASSERT(ic);
1121 if (ic->ic_disconnecting || ic->ic_socket == NULL) {
1122 ICL_DEBUG("icl_pdu_queue on closed connection");
1123 icl_soft_conn_pdu_free(ic, ip);
1127 if (!STAILQ_EMPTY(&ic->ic_to_send)) {
1128 STAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next);
1130 * If the queue is not empty, someone else had already
1131 * signaled the send thread; no need to do that again,
1137 STAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next);
1138 cv_signal(&ic->ic_send_cv);
1142 icl_soft_conn_pdu_queue(struct icl_conn *ic, struct icl_pdu *ip)
1148 static struct icl_conn *
1149 icl_soft_new_conn(const char *name, struct mtx *lock)
1151 struct icl_conn *ic;
1153 refcount_acquire(&icl_ncons);
1155 ic = (struct icl_conn *)kobj_create(&icl_soft_class, M_ICL_SOFT, M_WAITOK | M_ZERO);
1157 STAILQ_INIT(&ic->ic_to_send);
1159 cv_init(&ic->ic_send_cv, "icl_tx");
1160 cv_init(&ic->ic_receive_cv, "icl_rx");
1162 refcount_init(&ic->ic_outstanding_pdus, 0);
1164 ic->ic_max_data_segment_length = ICL_MAX_DATA_SEGMENT_LENGTH;
1166 ic->ic_offload = "None";
1167 ic->ic_unmapped = false;
1173 icl_soft_conn_free(struct icl_conn *ic)
1177 KASSERT(ic->ic_outstanding_pdus == 0,
1178 ("destroying session with %d outstanding PDUs",
1179 ic->ic_outstanding_pdus));
1181 cv_destroy(&ic->ic_send_cv);
1182 cv_destroy(&ic->ic_receive_cv);
1183 kobj_delete((struct kobj *)ic, M_ICL_SOFT);
1184 refcount_release(&icl_ncons);
1188 icl_conn_start(struct icl_conn *ic)
1199 if (ic->ic_socket == NULL) {
1200 ICL_CONN_UNLOCK(ic);
1204 ic->ic_receive_state = ICL_CONN_STATE_BHS;
1205 ic->ic_receive_len = sizeof(struct iscsi_bhs);
1206 ic->ic_disconnecting = false;
1208 ICL_CONN_UNLOCK(ic);
1211 * For sendspace, this is required because the current code cannot
1212 * send a PDU in pieces; thus, the minimum buffer size is equal
1213 * to the maximum PDU size. "+4" is to account for possible padding.
1215 * What we should actually do here is to use autoscaling, but set
1216 * some minimal buffer size to "minspace". I don't know a way to do
1219 minspace = sizeof(struct iscsi_bhs) + ic->ic_max_data_segment_length +
1220 ISCSI_HEADER_DIGEST_SIZE + ISCSI_DATA_DIGEST_SIZE + 4;
1221 if (sendspace < minspace) {
1222 ICL_WARN("kern.icl.sendspace too low; must be at least %zd",
1224 sendspace = minspace;
1226 if (recvspace < minspace) {
1227 ICL_WARN("kern.icl.recvspace too low; must be at least %zd",
1229 recvspace = minspace;
1232 error = soreserve(ic->ic_socket, sendspace, recvspace);
1234 ICL_WARN("soreserve failed with error %d", error);
1235 icl_soft_conn_close(ic);
1238 ic->ic_socket->so_snd.sb_flags |= SB_AUTOSIZE;
1239 ic->ic_socket->so_rcv.sb_flags |= SB_AUTOSIZE;
1244 bzero(&opt, sizeof(opt));
1245 opt.sopt_dir = SOPT_SET;
1246 opt.sopt_level = IPPROTO_TCP;
1247 opt.sopt_name = TCP_NODELAY;
1248 opt.sopt_val = &one;
1249 opt.sopt_valsize = sizeof(one);
1250 error = sosetopt(ic->ic_socket, &opt);
1252 ICL_WARN("disabling TCP_NODELAY failed with error %d", error);
1253 icl_soft_conn_close(ic);
1258 * Register socket upcall, to get notified about incoming PDUs
1259 * and free space to send outgoing ones.
1261 SOCKBUF_LOCK(&ic->ic_socket->so_snd);
1262 soupcall_set(ic->ic_socket, SO_SND, icl_soupcall_send, ic);
1263 SOCKBUF_UNLOCK(&ic->ic_socket->so_snd);
1264 SOCKBUF_LOCK(&ic->ic_socket->so_rcv);
1265 soupcall_set(ic->ic_socket, SO_RCV, icl_soupcall_receive, ic);
1266 SOCKBUF_UNLOCK(&ic->ic_socket->so_rcv);
1272 ic->ic_send_running = ic->ic_receive_running = true;
1273 ICL_CONN_UNLOCK(ic);
1274 error = kthread_add(icl_send_thread, ic, NULL, NULL, 0, 0, "%stx",
1277 ICL_WARN("kthread_add(9) failed with error %d", error);
1279 ic->ic_send_running = ic->ic_receive_running = false;
1280 cv_signal(&ic->ic_send_cv);
1281 ICL_CONN_UNLOCK(ic);
1282 icl_soft_conn_close(ic);
1285 error = kthread_add(icl_receive_thread, ic, NULL, NULL, 0, 0, "%srx",
1288 ICL_WARN("kthread_add(9) failed with error %d", error);
1290 ic->ic_receive_running = false;
1291 cv_signal(&ic->ic_send_cv);
1292 ICL_CONN_UNLOCK(ic);
1293 icl_soft_conn_close(ic);
1301 icl_soft_conn_handoff(struct icl_conn *ic, int fd)
1305 cap_rights_t rights;
1308 ICL_CONN_LOCK_ASSERT_NOT(ic);
1310 #ifdef ICL_KERNEL_PROXY
1312 * We're transitioning to Full Feature phase, and we don't
1317 if (ic->ic_socket == NULL) {
1318 ICL_CONN_UNLOCK(ic);
1319 ICL_WARN("proxy handoff without connect");
1322 ICL_CONN_UNLOCK(ic);
1328 * Steal the socket from userland.
1330 error = fget(curthread, fd,
1331 cap_rights_init(&rights, CAP_SOCK_CLIENT), &fp);
1334 if (fp->f_type != DTYPE_SOCKET) {
1335 fdrop(fp, curthread);
1339 if (so->so_type != SOCK_STREAM) {
1340 fdrop(fp, curthread);
1346 if (ic->ic_socket != NULL) {
1347 ICL_CONN_UNLOCK(ic);
1348 fdrop(fp, curthread);
1352 ic->ic_socket = fp->f_data;
1353 fp->f_ops = &badfileops;
1355 fdrop(fp, curthread);
1356 ICL_CONN_UNLOCK(ic);
1358 error = icl_conn_start(ic);
1364 icl_soft_conn_close(struct icl_conn *ic)
1366 struct icl_pdu *pdu;
1372 * Wake up the threads, so they can properly terminate.
1374 ic->ic_disconnecting = true;
1375 while (ic->ic_receive_running || ic->ic_send_running) {
1376 cv_signal(&ic->ic_receive_cv);
1377 cv_signal(&ic->ic_send_cv);
1378 cv_wait(&ic->ic_send_cv, ic->ic_lock);
1381 /* Some other thread could close the connection same time. */
1384 ICL_CONN_UNLOCK(ic);
1387 ic->ic_socket = NULL;
1390 * Deregister socket upcalls.
1392 ICL_CONN_UNLOCK(ic);
1393 SOCKBUF_LOCK(&so->so_snd);
1394 if (so->so_snd.sb_upcall != NULL)
1395 soupcall_clear(so, SO_SND);
1396 SOCKBUF_UNLOCK(&so->so_snd);
1397 SOCKBUF_LOCK(&so->so_rcv);
1398 if (so->so_rcv.sb_upcall != NULL)
1399 soupcall_clear(so, SO_RCV);
1400 SOCKBUF_UNLOCK(&so->so_rcv);
1404 if (ic->ic_receive_pdu != NULL) {
1405 //ICL_DEBUG("freeing partially received PDU");
1406 icl_soft_conn_pdu_free(ic, ic->ic_receive_pdu);
1407 ic->ic_receive_pdu = NULL;
1411 * Remove any outstanding PDUs from the send queue.
1413 while (!STAILQ_EMPTY(&ic->ic_to_send)) {
1414 pdu = STAILQ_FIRST(&ic->ic_to_send);
1415 STAILQ_REMOVE_HEAD(&ic->ic_to_send, ip_next);
1416 icl_soft_conn_pdu_free(ic, pdu);
1419 KASSERT(STAILQ_EMPTY(&ic->ic_to_send),
1420 ("destroying session with non-empty send queue"));
1421 ICL_CONN_UNLOCK(ic);
1425 icl_soft_conn_task_setup(struct icl_conn *ic, struct icl_pdu *ip,
1426 struct ccb_scsiio *csio, uint32_t *task_tagp, void **prvp)
1433 icl_soft_conn_task_done(struct icl_conn *ic, void *prv)
1438 icl_soft_conn_transfer_setup(struct icl_conn *ic, union ctl_io *io,
1439 uint32_t *transfer_tag, void **prvp)
1446 icl_soft_conn_transfer_done(struct icl_conn *ic, void *prv)
1451 icl_soft_limits(struct icl_drv_limits *idl)
1454 idl->idl_max_recv_data_segment_length = 128 * 1024;
1455 idl->idl_max_send_data_segment_length = 128 * 1024;
1456 idl->idl_max_burst_length = 262144;
1457 idl->idl_first_burst_length = 65536;
1462 #ifdef ICL_KERNEL_PROXY
1464 icl_soft_conn_connect(struct icl_conn *ic, int domain, int socktype,
1465 int protocol, struct sockaddr *from_sa, struct sockaddr *to_sa)
1468 return (icl_soft_proxy_connect(ic, domain, socktype, protocol,
1473 icl_soft_handoff_sock(struct icl_conn *ic, struct socket *so)
1477 ICL_CONN_LOCK_ASSERT_NOT(ic);
1479 if (so->so_type != SOCK_STREAM)
1483 if (ic->ic_socket != NULL) {
1484 ICL_CONN_UNLOCK(ic);
1488 ICL_CONN_UNLOCK(ic);
1490 error = icl_conn_start(ic);
1494 #endif /* ICL_KERNEL_PROXY */
1501 icl_pdu_zone = uma_zcreate("icl_pdu",
1502 sizeof(struct icl_pdu), NULL, NULL, NULL, NULL,
1504 refcount_init(&icl_ncons, 0);
1507 * The reason we call this "none" is that to the user,
1508 * it's known as "offload driver"; "offload driver: soft"
1509 * doesn't make much sense.
1511 error = icl_register("none", false, 0,
1512 icl_soft_limits, icl_soft_new_conn);
1513 KASSERT(error == 0, ("failed to register"));
1515 #if defined(ICL_KERNEL_PROXY) && 0
1517 * Debugging aid for kernel proxy functionality.
1519 error = icl_register("proxytest", true, 0,
1520 icl_soft_limits, icl_soft_new_conn);
1521 KASSERT(error == 0, ("failed to register"));
1528 icl_soft_unload(void)
1534 icl_unregister("none", false);
1535 #if defined(ICL_KERNEL_PROXY) && 0
1536 icl_unregister("proxytest", true);
1539 uma_zdestroy(icl_pdu_zone);
1545 icl_soft_modevent(module_t mod, int what, void *arg)
1550 return (icl_soft_load());
1552 return (icl_soft_unload());
1558 moduledata_t icl_soft_data = {
1564 DECLARE_MODULE(icl_soft, icl_soft_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1565 MODULE_DEPEND(icl_soft, icl, 1, 1, 1);
1566 MODULE_VERSION(icl_soft, 1);