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>
70 /* soft specific stuff goes here. */
76 static int coalesce = 1;
77 SYSCTL_INT(_kern_icl, OID_AUTO, coalesce, CTLFLAG_RWTUN,
78 &coalesce, 0, "Try to coalesce PDUs before sending");
79 static int partial_receive_len = 128 * 1024;
80 SYSCTL_INT(_kern_icl, OID_AUTO, partial_receive_len, CTLFLAG_RWTUN,
81 &partial_receive_len, 0, "Minimum read size for partially received "
83 static int sendspace = 1048576;
84 SYSCTL_INT(_kern_icl, OID_AUTO, sendspace, CTLFLAG_RWTUN,
85 &sendspace, 0, "Default send socket buffer size");
86 static int recvspace = 1048576;
87 SYSCTL_INT(_kern_icl, OID_AUTO, recvspace, CTLFLAG_RWTUN,
88 &recvspace, 0, "Default receive socket buffer size");
90 static MALLOC_DEFINE(M_ICL_SOFT, "icl_soft", "iSCSI software backend");
91 static uma_zone_t icl_soft_pdu_zone;
93 static volatile u_int icl_ncons;
95 #define ICL_CONN_LOCK(X) mtx_lock(X->ic_lock)
96 #define ICL_CONN_UNLOCK(X) mtx_unlock(X->ic_lock)
97 #define ICL_CONN_LOCK_ASSERT(X) mtx_assert(X->ic_lock, MA_OWNED)
98 #define ICL_CONN_LOCK_ASSERT_NOT(X) mtx_assert(X->ic_lock, MA_NOTOWNED)
100 STAILQ_HEAD(icl_pdu_stailq, icl_pdu);
102 static icl_conn_new_pdu_t icl_soft_conn_new_pdu;
103 static icl_conn_pdu_free_t icl_soft_conn_pdu_free;
104 static icl_conn_pdu_data_segment_length_t
105 icl_soft_conn_pdu_data_segment_length;
106 static icl_conn_pdu_append_data_t icl_soft_conn_pdu_append_data;
107 static icl_conn_pdu_get_data_t icl_soft_conn_pdu_get_data;
108 static icl_conn_pdu_queue_t icl_soft_conn_pdu_queue;
109 static icl_conn_pdu_queue_cb_t icl_soft_conn_pdu_queue_cb;
110 static icl_conn_handoff_t icl_soft_conn_handoff;
111 static icl_conn_free_t icl_soft_conn_free;
112 static icl_conn_close_t icl_soft_conn_close;
113 static icl_conn_task_setup_t icl_soft_conn_task_setup;
114 static icl_conn_task_done_t icl_soft_conn_task_done;
115 static icl_conn_transfer_setup_t icl_soft_conn_transfer_setup;
116 static icl_conn_transfer_done_t icl_soft_conn_transfer_done;
117 #ifdef ICL_KERNEL_PROXY
118 static icl_conn_connect_t icl_soft_conn_connect;
121 static kobj_method_t icl_soft_methods[] = {
122 KOBJMETHOD(icl_conn_new_pdu, icl_soft_conn_new_pdu),
123 KOBJMETHOD(icl_conn_pdu_free, icl_soft_conn_pdu_free),
124 KOBJMETHOD(icl_conn_pdu_data_segment_length,
125 icl_soft_conn_pdu_data_segment_length),
126 KOBJMETHOD(icl_conn_pdu_append_data, icl_soft_conn_pdu_append_data),
127 KOBJMETHOD(icl_conn_pdu_get_data, icl_soft_conn_pdu_get_data),
128 KOBJMETHOD(icl_conn_pdu_queue, icl_soft_conn_pdu_queue),
129 KOBJMETHOD(icl_conn_pdu_queue_cb, icl_soft_conn_pdu_queue_cb),
130 KOBJMETHOD(icl_conn_handoff, icl_soft_conn_handoff),
131 KOBJMETHOD(icl_conn_free, icl_soft_conn_free),
132 KOBJMETHOD(icl_conn_close, icl_soft_conn_close),
133 KOBJMETHOD(icl_conn_task_setup, icl_soft_conn_task_setup),
134 KOBJMETHOD(icl_conn_task_done, icl_soft_conn_task_done),
135 KOBJMETHOD(icl_conn_transfer_setup, icl_soft_conn_transfer_setup),
136 KOBJMETHOD(icl_conn_transfer_done, icl_soft_conn_transfer_done),
137 #ifdef ICL_KERNEL_PROXY
138 KOBJMETHOD(icl_conn_connect, icl_soft_conn_connect),
143 DEFINE_CLASS(icl_soft, icl_soft_methods, sizeof(struct icl_conn));
146 icl_conn_fail(struct icl_conn *ic)
148 if (ic->ic_socket == NULL)
154 ic->ic_socket->so_error = EDOOFUS;
159 icl_conn_receive(struct icl_conn *ic, size_t len)
168 memset(&uio, 0, sizeof(uio));
171 flags = MSG_DONTWAIT;
172 error = soreceive(so, NULL, &uio, &m, NULL, &flags);
174 ICL_DEBUG("soreceive error %d", error);
177 if (uio.uio_resid != 0) {
179 ICL_DEBUG("short read");
187 icl_conn_receive_buf(struct icl_conn *ic, void *buf, size_t len)
196 memset(&uio, 0, sizeof(uio));
197 iov[0].iov_base = buf;
198 iov[0].iov_len = len;
203 uio.uio_segflg = UIO_SYSSPACE;
204 uio.uio_rw = UIO_READ;
206 flags = MSG_DONTWAIT;
207 error = soreceive(so, NULL, &uio, NULL, NULL, &flags);
209 ICL_DEBUG("soreceive error %d", error);
212 if (uio.uio_resid != 0) {
213 ICL_DEBUG("short read");
221 icl_soft_conn_pdu_free(struct icl_conn *ic, struct icl_pdu *ip)
223 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip;
225 KASSERT(isp->ref_cnt == 0, ("freeing active PDU"));
226 m_freem(ip->ip_bhs_mbuf);
227 m_freem(ip->ip_ahs_mbuf);
228 m_freem(ip->ip_data_mbuf);
229 uma_zfree(icl_soft_pdu_zone, isp);
231 refcount_release(&ic->ic_outstanding_pdus);
236 icl_soft_pdu_call_cb(struct icl_pdu *ip)
238 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip;
241 isp->cb(ip, isp->error);
243 refcount_release(&ip->ip_conn->ic_outstanding_pdus);
245 uma_zfree(icl_soft_pdu_zone, isp);
249 icl_soft_pdu_done(struct icl_pdu *ip, int error)
251 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip;
256 m_freem(ip->ip_bhs_mbuf);
257 ip->ip_bhs_mbuf = NULL;
258 m_freem(ip->ip_ahs_mbuf);
259 ip->ip_ahs_mbuf = NULL;
260 m_freem(ip->ip_data_mbuf);
261 ip->ip_data_mbuf = NULL;
263 if (atomic_fetchadd_int(&isp->ref_cnt, -1) == 1)
264 icl_soft_pdu_call_cb(ip);
268 icl_soft_mbuf_done(struct mbuf *mb)
270 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)mb->m_ext.ext_arg1;
272 icl_soft_pdu_call_cb(&isp->ip);
276 * Allocate icl_pdu with empty BHS to fill up by the caller.
279 icl_soft_conn_new_pdu(struct icl_conn *ic, int flags)
281 struct icl_soft_pdu *isp;
285 refcount_acquire(&ic->ic_outstanding_pdus);
287 isp = uma_zalloc(icl_soft_pdu_zone, flags | M_ZERO);
289 ICL_WARN("failed to allocate soft PDU");
291 refcount_release(&ic->ic_outstanding_pdus);
298 CTASSERT(sizeof(struct iscsi_bhs) <= MHLEN);
299 ip->ip_bhs_mbuf = m_gethdr(flags, MT_DATA);
300 if (ip->ip_bhs_mbuf == NULL) {
301 ICL_WARN("failed to allocate BHS mbuf");
302 icl_soft_conn_pdu_free(ic, ip);
305 ip->ip_bhs = mtod(ip->ip_bhs_mbuf, struct iscsi_bhs *);
306 memset(ip->ip_bhs, 0, sizeof(struct iscsi_bhs));
307 ip->ip_bhs_mbuf->m_len = sizeof(struct iscsi_bhs);
313 icl_pdu_ahs_length(const struct icl_pdu *request)
316 return (request->ip_bhs->bhs_total_ahs_len * 4);
320 icl_pdu_data_segment_length(const struct icl_pdu *request)
324 len += request->ip_bhs->bhs_data_segment_len[0];
326 len += request->ip_bhs->bhs_data_segment_len[1];
328 len += request->ip_bhs->bhs_data_segment_len[2];
334 icl_soft_conn_pdu_data_segment_length(struct icl_conn *ic,
335 const struct icl_pdu *request)
338 return (icl_pdu_data_segment_length(request));
342 icl_pdu_set_data_segment_length(struct icl_pdu *response, uint32_t len)
345 response->ip_bhs->bhs_data_segment_len[2] = len;
346 response->ip_bhs->bhs_data_segment_len[1] = len >> 8;
347 response->ip_bhs->bhs_data_segment_len[0] = len >> 16;
351 icl_pdu_padding(const struct icl_pdu *ip)
354 if ((ip->ip_data_len % 4) != 0)
355 return (4 - (ip->ip_data_len % 4));
361 icl_pdu_size(const struct icl_pdu *response)
365 KASSERT(response->ip_ahs_len == 0, ("responding with AHS"));
367 len = sizeof(struct iscsi_bhs) + response->ip_data_len +
368 icl_pdu_padding(response);
369 if (response->ip_conn->ic_header_crc32c)
370 len += ISCSI_HEADER_DIGEST_SIZE;
371 if (response->ip_data_len != 0 && response->ip_conn->ic_data_crc32c)
372 len += ISCSI_DATA_DIGEST_SIZE;
378 icl_pdu_receive_bhs(struct icl_pdu *request, size_t *availablep)
381 if (icl_conn_receive_buf(request->ip_conn,
382 request->ip_bhs, sizeof(struct iscsi_bhs))) {
383 ICL_DEBUG("failed to receive BHS");
387 *availablep -= sizeof(struct iscsi_bhs);
392 icl_pdu_receive_ahs(struct icl_pdu *request, size_t *availablep)
395 request->ip_ahs_len = icl_pdu_ahs_length(request);
396 if (request->ip_ahs_len == 0)
399 request->ip_ahs_mbuf = icl_conn_receive(request->ip_conn,
400 request->ip_ahs_len);
401 if (request->ip_ahs_mbuf == NULL) {
402 ICL_DEBUG("failed to receive AHS");
406 *availablep -= request->ip_ahs_len;
411 icl_mbuf_to_crc32c(const struct mbuf *m0)
413 uint32_t digest = 0xffffffff;
414 const struct mbuf *m;
416 for (m = m0; m != NULL; m = m->m_next)
417 digest = calculate_crc32c(digest,
418 mtod(m, const void *), m->m_len);
420 digest = digest ^ 0xffffffff;
426 icl_pdu_check_header_digest(struct icl_pdu *request, size_t *availablep)
428 uint32_t received_digest, valid_digest;
430 if (request->ip_conn->ic_header_crc32c == false)
433 CTASSERT(sizeof(received_digest) == ISCSI_HEADER_DIGEST_SIZE);
434 if (icl_conn_receive_buf(request->ip_conn,
435 &received_digest, ISCSI_HEADER_DIGEST_SIZE)) {
436 ICL_DEBUG("failed to receive header digest");
439 *availablep -= ISCSI_HEADER_DIGEST_SIZE;
441 /* Temporary attach AHS to BHS to calculate header digest. */
442 request->ip_bhs_mbuf->m_next = request->ip_ahs_mbuf;
443 valid_digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
444 request->ip_bhs_mbuf->m_next = NULL;
445 if (received_digest != valid_digest) {
446 ICL_WARN("header digest check failed; got 0x%x, "
447 "should be 0x%x", received_digest, valid_digest);
455 * Return the number of bytes that should be waiting in the receive socket
456 * before icl_pdu_receive_data_segment() gets called.
459 icl_pdu_data_segment_receive_len(const struct icl_pdu *request)
463 len = icl_pdu_data_segment_length(request);
468 * Account for the parts of data segment already read from
471 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
472 len -= request->ip_data_len;
475 * Don't always wait for the full data segment to be delivered
476 * to the socket; this might badly affect performance due to
477 * TCP window scaling.
479 if (len > partial_receive_len) {
481 ICL_DEBUG("need %zd bytes of data, limiting to %zd",
482 len, partial_receive_len));
484 len = partial_receive_len;
490 * Account for padding. Note that due to the way code is written,
491 * the icl_pdu_receive_data_segment() must always receive padding
492 * along with the last part of data segment, because it would be
493 * impossible to tell whether we've already received the full data
494 * segment including padding, or without it.
497 len += 4 - (len % 4);
500 ICL_DEBUG("need %zd bytes of data", len));
507 icl_pdu_receive_data_segment(struct icl_pdu *request,
508 size_t *availablep, bool *more_neededp)
511 size_t len, padding = 0;
514 ic = request->ip_conn;
516 *more_neededp = false;
517 ic->ic_receive_len = 0;
519 len = icl_pdu_data_segment_length(request);
524 padding = 4 - (len % 4);
527 * Account for already received parts of data segment.
529 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
530 len -= request->ip_data_len;
532 if (len + padding > *availablep) {
534 * Not enough data in the socket buffer. Receive as much
535 * as we can. Don't receive padding, since, obviously, it's
536 * not the end of data segment yet.
539 ICL_DEBUG("limited from %zd to %zd",
540 len + padding, *availablep - padding));
542 len = *availablep - padding;
543 *more_neededp = true;
548 * Must not try to receive padding without at least one byte
549 * of actual data segment.
552 m = icl_conn_receive(request->ip_conn, len + padding);
554 ICL_DEBUG("failed to receive data segment");
558 if (request->ip_data_mbuf == NULL)
559 request->ip_data_mbuf = m;
561 m_cat(request->ip_data_mbuf, m);
563 request->ip_data_len += len;
564 *availablep -= len + padding;
570 icl_pdu_data_segment_receive_len(request);
576 icl_pdu_check_data_digest(struct icl_pdu *request, size_t *availablep)
578 uint32_t received_digest, valid_digest;
580 if (request->ip_conn->ic_data_crc32c == false)
583 if (request->ip_data_len == 0)
586 CTASSERT(sizeof(received_digest) == ISCSI_DATA_DIGEST_SIZE);
587 if (icl_conn_receive_buf(request->ip_conn,
588 &received_digest, ISCSI_DATA_DIGEST_SIZE)) {
589 ICL_DEBUG("failed to receive data digest");
592 *availablep -= ISCSI_DATA_DIGEST_SIZE;
595 * Note that ip_data_mbuf also contains padding; since digest
596 * calculation is supposed to include that, we iterate over
597 * the entire ip_data_mbuf chain, not just ip_data_len bytes of it.
599 valid_digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
600 if (received_digest != valid_digest) {
601 ICL_WARN("data digest check failed; got 0x%x, "
602 "should be 0x%x", received_digest, valid_digest);
610 * Somewhat contrary to the name, this attempts to receive only one
611 * "part" of PDU at a time; call it repeatedly until it returns non-NULL.
613 static struct icl_pdu *
614 icl_conn_receive_pdu(struct icl_conn *ic, size_t *availablep)
616 struct icl_pdu *request;
624 if (ic->ic_receive_state == ICL_CONN_STATE_BHS) {
625 KASSERT(ic->ic_receive_pdu == NULL,
626 ("ic->ic_receive_pdu != NULL"));
627 request = icl_soft_conn_new_pdu(ic, M_NOWAIT);
628 if (request == NULL) {
629 ICL_DEBUG("failed to allocate PDU; "
630 "dropping connection");
634 ic->ic_receive_pdu = request;
636 KASSERT(ic->ic_receive_pdu != NULL,
637 ("ic->ic_receive_pdu == NULL"));
638 request = ic->ic_receive_pdu;
641 if (*availablep < ic->ic_receive_len) {
643 ICL_DEBUG("not enough data; need %zd, "
644 "have %zd", ic->ic_receive_len, *availablep);
649 switch (ic->ic_receive_state) {
650 case ICL_CONN_STATE_BHS:
651 //ICL_DEBUG("receiving BHS");
652 error = icl_pdu_receive_bhs(request, availablep);
654 ICL_DEBUG("failed to receive BHS; "
655 "dropping connection");
660 * We don't enforce any limit for AHS length;
661 * its length is stored in 8 bit field.
664 len = icl_pdu_data_segment_length(request);
665 if (len > ic->ic_max_data_segment_length) {
666 ICL_WARN("received data segment "
667 "length %zd is larger than negotiated "
668 "MaxDataSegmentLength %zd; "
669 "dropping connection",
670 len, ic->ic_max_data_segment_length);
675 ic->ic_receive_state = ICL_CONN_STATE_AHS;
676 ic->ic_receive_len = icl_pdu_ahs_length(request);
679 case ICL_CONN_STATE_AHS:
680 //ICL_DEBUG("receiving AHS");
681 error = icl_pdu_receive_ahs(request, availablep);
683 ICL_DEBUG("failed to receive AHS; "
684 "dropping connection");
687 ic->ic_receive_state = ICL_CONN_STATE_HEADER_DIGEST;
688 if (ic->ic_header_crc32c == false)
689 ic->ic_receive_len = 0;
691 ic->ic_receive_len = ISCSI_HEADER_DIGEST_SIZE;
694 case ICL_CONN_STATE_HEADER_DIGEST:
695 //ICL_DEBUG("receiving header digest");
696 error = icl_pdu_check_header_digest(request, availablep);
698 ICL_DEBUG("header digest failed; "
699 "dropping connection");
703 ic->ic_receive_state = ICL_CONN_STATE_DATA;
705 icl_pdu_data_segment_receive_len(request);
708 case ICL_CONN_STATE_DATA:
709 //ICL_DEBUG("receiving data segment");
710 error = icl_pdu_receive_data_segment(request, availablep,
713 ICL_DEBUG("failed to receive data segment;"
714 "dropping connection");
721 ic->ic_receive_state = ICL_CONN_STATE_DATA_DIGEST;
722 if (request->ip_data_len == 0 || ic->ic_data_crc32c == false)
723 ic->ic_receive_len = 0;
725 ic->ic_receive_len = ISCSI_DATA_DIGEST_SIZE;
728 case ICL_CONN_STATE_DATA_DIGEST:
729 //ICL_DEBUG("receiving data digest");
730 error = icl_pdu_check_data_digest(request, availablep);
732 ICL_DEBUG("data digest failed; "
733 "dropping connection");
738 * We've received complete PDU; reset the receive state machine
739 * and return the PDU.
741 ic->ic_receive_state = ICL_CONN_STATE_BHS;
742 ic->ic_receive_len = sizeof(struct iscsi_bhs);
743 ic->ic_receive_pdu = NULL;
747 panic("invalid ic_receive_state %d\n", ic->ic_receive_state);
752 * Don't free the PDU; it's pointed to by ic->ic_receive_pdu
753 * and will get freed in icl_soft_conn_close().
762 icl_conn_receive_pdus(struct icl_conn *ic, size_t available)
764 struct icl_pdu *response;
770 * This can never happen; we're careful to only mess with ic->ic_socket
771 * pointer when the send/receive threads are not running.
773 KASSERT(so != NULL, ("NULL socket"));
776 if (ic->ic_disconnecting)
779 if (so->so_error != 0) {
780 ICL_DEBUG("connection error %d; "
781 "dropping connection", so->so_error);
787 * Loop until we have a complete PDU or there is not enough
788 * data in the socket buffer.
790 if (available < ic->ic_receive_len) {
792 ICL_DEBUG("not enough data; have %zd, "
793 "need %zd", available,
799 response = icl_conn_receive_pdu(ic, &available);
800 if (response == NULL)
803 if (response->ip_ahs_len > 0) {
804 ICL_WARN("received PDU with unsupported "
805 "AHS; opcode 0x%x; dropping connection",
806 response->ip_bhs->bhs_opcode);
807 icl_soft_conn_pdu_free(ic, response);
812 (ic->ic_receive)(response);
817 icl_receive_thread(void *arg)
827 if (ic->ic_disconnecting) {
828 //ICL_DEBUG("terminating");
833 * Set the low watermark, to be checked by
834 * soreadable() in icl_soupcall_receive()
835 * to avoid unnecessary wakeups until there
836 * is enough data received to read the PDU.
838 SOCKBUF_LOCK(&so->so_rcv);
839 available = sbavail(&so->so_rcv);
840 if (available < ic->ic_receive_len) {
841 so->so_rcv.sb_lowat = ic->ic_receive_len;
842 cv_wait(&ic->ic_receive_cv, &so->so_rcv.sb_mtx);
844 so->so_rcv.sb_lowat = so->so_rcv.sb_hiwat + 1;
845 SOCKBUF_UNLOCK(&so->so_rcv);
847 icl_conn_receive_pdus(ic, available);
851 ic->ic_receive_running = false;
852 cv_signal(&ic->ic_send_cv);
858 icl_soupcall_receive(struct socket *so, void *arg, int waitflag)
866 cv_signal(&ic->ic_receive_cv);
871 icl_pdu_finalize(struct icl_pdu *request)
873 size_t padding, pdu_len;
874 uint32_t digest, zero = 0;
878 ic = request->ip_conn;
880 icl_pdu_set_data_segment_length(request, request->ip_data_len);
882 pdu_len = icl_pdu_size(request);
884 if (ic->ic_header_crc32c) {
885 digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
886 ok = m_append(request->ip_bhs_mbuf, sizeof(digest),
889 ICL_WARN("failed to append header digest");
894 if (request->ip_data_len != 0) {
895 padding = icl_pdu_padding(request);
897 ok = m_append(request->ip_data_mbuf, padding,
900 ICL_WARN("failed to append padding");
905 if (ic->ic_data_crc32c) {
906 digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
908 ok = m_append(request->ip_data_mbuf, sizeof(digest),
911 ICL_WARN("failed to append data digest");
916 m_cat(request->ip_bhs_mbuf, request->ip_data_mbuf);
917 request->ip_data_mbuf = NULL;
920 request->ip_bhs_mbuf->m_pkthdr.len = pdu_len;
926 icl_conn_send_pdus(struct icl_conn *ic, struct icl_pdu_stailq *queue)
928 struct icl_pdu *request, *request2;
930 long available, size, size2;
931 int coalesced, error;
933 ICL_CONN_LOCK_ASSERT_NOT(ic);
937 SOCKBUF_LOCK(&so->so_snd);
939 * Check how much space do we have for transmit. We can't just
940 * call sosend() and retry when we get EWOULDBLOCK or EMSGSIZE,
941 * as it always frees the mbuf chain passed to it, even in case
944 available = sbspace(&so->so_snd);
947 * Notify the socket upcall that we don't need wakeups
948 * for the time being.
950 so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1;
951 SOCKBUF_UNLOCK(&so->so_snd);
953 while (!STAILQ_EMPTY(queue)) {
954 request = STAILQ_FIRST(queue);
955 size = icl_pdu_size(request);
956 if (available < size) {
959 * Set the low watermark, to be checked by
960 * sowriteable() in icl_soupcall_send()
961 * to avoid unnecessary wakeups until there
962 * is enough space for the PDU to fit.
964 SOCKBUF_LOCK(&so->so_snd);
965 available = sbspace(&so->so_snd);
966 if (available < size) {
968 ICL_DEBUG("no space to send; "
969 "have %ld, need %ld",
972 so->so_snd.sb_lowat = max(size,
973 so->so_snd.sb_hiwat / 8);
974 SOCKBUF_UNLOCK(&so->so_snd);
977 SOCKBUF_UNLOCK(&so->so_snd);
979 STAILQ_REMOVE_HEAD(queue, ip_next);
980 error = icl_pdu_finalize(request);
982 ICL_DEBUG("failed to finalize PDU; "
983 "dropping connection");
984 icl_soft_pdu_done(request, EIO);
991 request2 = STAILQ_FIRST(queue);
992 if (request2 == NULL)
994 size2 = icl_pdu_size(request2);
995 if (available < size + size2)
997 STAILQ_REMOVE_HEAD(queue, ip_next);
998 error = icl_pdu_finalize(request2);
1000 ICL_DEBUG("failed to finalize PDU; "
1001 "dropping connection");
1002 icl_soft_pdu_done(request, EIO);
1003 icl_soft_pdu_done(request2, EIO);
1007 m_cat(request->ip_bhs_mbuf, request2->ip_bhs_mbuf);
1008 request2->ip_bhs_mbuf = NULL;
1009 request->ip_bhs_mbuf->m_pkthdr.len += size2;
1011 STAILQ_REMOVE_AFTER(queue, request, ip_next);
1012 icl_soft_pdu_done(request2, 0);
1016 if (coalesced > 1) {
1017 ICL_DEBUG("coalesced %d PDUs into %ld bytes",
1023 error = sosend(so, NULL, NULL, request->ip_bhs_mbuf,
1024 NULL, MSG_DONTWAIT, curthread);
1025 request->ip_bhs_mbuf = NULL; /* Sosend consumes the mbuf. */
1027 ICL_DEBUG("failed to send PDU, error %d; "
1028 "dropping connection", error);
1029 icl_soft_pdu_done(request, error);
1033 icl_soft_pdu_done(request, 0);
1038 icl_send_thread(void *arg)
1040 struct icl_conn *ic;
1041 struct icl_pdu_stailq queue;
1045 STAILQ_INIT(&queue);
1051 * If the local queue is empty, populate it from
1052 * the main one. This way the icl_conn_send_pdus()
1053 * can go through all the queued PDUs without holding
1056 if (STAILQ_EMPTY(&queue))
1057 STAILQ_SWAP(&ic->ic_to_send, &queue, icl_pdu);
1059 ic->ic_check_send_space = false;
1060 ICL_CONN_UNLOCK(ic);
1061 icl_conn_send_pdus(ic, &queue);
1065 * The icl_soupcall_send() was called since the last
1066 * call to sbspace(); go around;
1068 if (ic->ic_check_send_space)
1072 * Local queue is empty, but we still have PDUs
1073 * in the main one; go around.
1075 if (STAILQ_EMPTY(&queue) &&
1076 !STAILQ_EMPTY(&ic->ic_to_send))
1080 * There might be some stuff in the local queue,
1081 * which didn't get sent due to not having enough send
1082 * space. Wait for socket upcall.
1087 if (ic->ic_disconnecting) {
1088 //ICL_DEBUG("terminating");
1092 cv_wait(&ic->ic_send_cv, ic->ic_lock);
1096 * We're exiting; move PDUs back to the main queue, so they can
1097 * get freed properly. At this point ordering doesn't matter.
1099 STAILQ_CONCAT(&ic->ic_to_send, &queue);
1101 ic->ic_send_running = false;
1102 cv_signal(&ic->ic_send_cv);
1103 ICL_CONN_UNLOCK(ic);
1108 icl_soupcall_send(struct socket *so, void *arg, int waitflag)
1110 struct icl_conn *ic;
1112 if (!sowriteable(so))
1118 ic->ic_check_send_space = true;
1119 ICL_CONN_UNLOCK(ic);
1121 cv_signal(&ic->ic_send_cv);
1127 icl_soft_conn_pdu_append_data(struct icl_conn *ic, struct icl_pdu *request,
1128 const void *addr, size_t len, int flags)
1130 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)request;
1131 struct mbuf *mb, *newmb;
1132 size_t copylen, off = 0;
1134 KASSERT(len > 0, ("len == 0"));
1136 if (flags & ICL_NOCOPY) {
1137 newmb = m_get(flags & ~ICL_NOCOPY, MT_DATA);
1138 if (newmb == NULL) {
1139 ICL_WARN("failed to allocate mbuf");
1143 newmb->m_flags |= M_RDONLY;
1144 m_extaddref(newmb, __DECONST(char *, addr), len, &isp->ref_cnt,
1145 icl_soft_mbuf_done, isp, NULL);
1148 newmb = m_getm2(NULL, len, flags, MT_DATA, 0);
1149 if (newmb == NULL) {
1150 ICL_WARN("failed to allocate mbuf for %zd bytes", len);
1154 for (mb = newmb; mb != NULL; mb = mb->m_next) {
1155 copylen = min(M_TRAILINGSPACE(mb), len - off);
1156 memcpy(mtod(mb, char *), (const char *)addr + off, copylen);
1157 mb->m_len = copylen;
1160 KASSERT(off == len, ("%s: off != len", __func__));
1163 if (request->ip_data_mbuf == NULL) {
1164 request->ip_data_mbuf = newmb;
1165 request->ip_data_len = len;
1167 m_cat(request->ip_data_mbuf, newmb);
1168 request->ip_data_len += len;
1175 icl_soft_conn_pdu_get_data(struct icl_conn *ic, struct icl_pdu *ip,
1176 size_t off, void *addr, size_t len)
1179 m_copydata(ip->ip_data_mbuf, off, len, addr);
1183 icl_soft_conn_pdu_queue(struct icl_conn *ic, struct icl_pdu *ip)
1186 icl_soft_conn_pdu_queue_cb(ic, ip, NULL);
1190 icl_soft_conn_pdu_queue_cb(struct icl_conn *ic, struct icl_pdu *ip,
1193 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip;
1195 ICL_CONN_LOCK_ASSERT(ic);
1199 if (ic->ic_disconnecting || ic->ic_socket == NULL) {
1200 ICL_DEBUG("icl_pdu_queue on closed connection");
1201 icl_soft_pdu_done(ip, ENOTCONN);
1205 if (!STAILQ_EMPTY(&ic->ic_to_send)) {
1206 STAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next);
1208 * If the queue is not empty, someone else had already
1209 * signaled the send thread; no need to do that again,
1215 STAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next);
1216 cv_signal(&ic->ic_send_cv);
1219 static struct icl_conn *
1220 icl_soft_new_conn(const char *name, struct mtx *lock)
1222 struct icl_conn *ic;
1224 refcount_acquire(&icl_ncons);
1226 ic = (struct icl_conn *)kobj_create(&icl_soft_class, M_ICL_SOFT, M_WAITOK | M_ZERO);
1228 STAILQ_INIT(&ic->ic_to_send);
1230 cv_init(&ic->ic_send_cv, "icl_tx");
1231 cv_init(&ic->ic_receive_cv, "icl_rx");
1233 refcount_init(&ic->ic_outstanding_pdus, 0);
1235 ic->ic_max_data_segment_length = ICL_MAX_DATA_SEGMENT_LENGTH;
1237 ic->ic_offload = "None";
1238 ic->ic_unmapped = false;
1244 icl_soft_conn_free(struct icl_conn *ic)
1248 KASSERT(ic->ic_outstanding_pdus == 0,
1249 ("destroying session with %d outstanding PDUs",
1250 ic->ic_outstanding_pdus));
1252 cv_destroy(&ic->ic_send_cv);
1253 cv_destroy(&ic->ic_receive_cv);
1254 kobj_delete((struct kobj *)ic, M_ICL_SOFT);
1255 refcount_release(&icl_ncons);
1259 icl_conn_start(struct icl_conn *ic)
1270 if (ic->ic_socket == NULL) {
1271 ICL_CONN_UNLOCK(ic);
1275 ic->ic_receive_state = ICL_CONN_STATE_BHS;
1276 ic->ic_receive_len = sizeof(struct iscsi_bhs);
1277 ic->ic_disconnecting = false;
1279 ICL_CONN_UNLOCK(ic);
1282 * For sendspace, this is required because the current code cannot
1283 * send a PDU in pieces; thus, the minimum buffer size is equal
1284 * to the maximum PDU size. "+4" is to account for possible padding.
1286 * What we should actually do here is to use autoscaling, but set
1287 * some minimal buffer size to "minspace". I don't know a way to do
1290 minspace = sizeof(struct iscsi_bhs) + ic->ic_max_data_segment_length +
1291 ISCSI_HEADER_DIGEST_SIZE + ISCSI_DATA_DIGEST_SIZE + 4;
1292 if (sendspace < minspace) {
1293 ICL_WARN("kern.icl.sendspace too low; must be at least %zd",
1295 sendspace = minspace;
1297 if (recvspace < minspace) {
1298 ICL_WARN("kern.icl.recvspace too low; must be at least %zd",
1300 recvspace = minspace;
1303 error = soreserve(ic->ic_socket, sendspace, recvspace);
1305 ICL_WARN("soreserve failed with error %d", error);
1306 icl_soft_conn_close(ic);
1309 ic->ic_socket->so_snd.sb_flags |= SB_AUTOSIZE;
1310 ic->ic_socket->so_rcv.sb_flags |= SB_AUTOSIZE;
1315 bzero(&opt, sizeof(opt));
1316 opt.sopt_dir = SOPT_SET;
1317 opt.sopt_level = IPPROTO_TCP;
1318 opt.sopt_name = TCP_NODELAY;
1319 opt.sopt_val = &one;
1320 opt.sopt_valsize = sizeof(one);
1321 error = sosetopt(ic->ic_socket, &opt);
1323 ICL_WARN("disabling TCP_NODELAY failed with error %d", error);
1324 icl_soft_conn_close(ic);
1329 * Register socket upcall, to get notified about incoming PDUs
1330 * and free space to send outgoing ones.
1332 SOCKBUF_LOCK(&ic->ic_socket->so_snd);
1333 soupcall_set(ic->ic_socket, SO_SND, icl_soupcall_send, ic);
1334 SOCKBUF_UNLOCK(&ic->ic_socket->so_snd);
1335 SOCKBUF_LOCK(&ic->ic_socket->so_rcv);
1336 soupcall_set(ic->ic_socket, SO_RCV, icl_soupcall_receive, ic);
1337 SOCKBUF_UNLOCK(&ic->ic_socket->so_rcv);
1343 ic->ic_send_running = ic->ic_receive_running = true;
1344 ICL_CONN_UNLOCK(ic);
1345 error = kthread_add(icl_send_thread, ic, NULL, NULL, 0, 0, "%stx",
1348 ICL_WARN("kthread_add(9) failed with error %d", error);
1350 ic->ic_send_running = ic->ic_receive_running = false;
1351 cv_signal(&ic->ic_send_cv);
1352 ICL_CONN_UNLOCK(ic);
1353 icl_soft_conn_close(ic);
1356 error = kthread_add(icl_receive_thread, ic, NULL, NULL, 0, 0, "%srx",
1359 ICL_WARN("kthread_add(9) failed with error %d", error);
1361 ic->ic_receive_running = false;
1362 cv_signal(&ic->ic_send_cv);
1363 ICL_CONN_UNLOCK(ic);
1364 icl_soft_conn_close(ic);
1372 icl_soft_conn_handoff(struct icl_conn *ic, int fd)
1376 cap_rights_t rights;
1379 ICL_CONN_LOCK_ASSERT_NOT(ic);
1381 #ifdef ICL_KERNEL_PROXY
1383 * We're transitioning to Full Feature phase, and we don't
1388 if (ic->ic_socket == NULL) {
1389 ICL_CONN_UNLOCK(ic);
1390 ICL_WARN("proxy handoff without connect");
1393 ICL_CONN_UNLOCK(ic);
1399 * Steal the socket from userland.
1401 error = fget(curthread, fd,
1402 cap_rights_init(&rights, CAP_SOCK_CLIENT), &fp);
1405 if (fp->f_type != DTYPE_SOCKET) {
1406 fdrop(fp, curthread);
1410 if (so->so_type != SOCK_STREAM) {
1411 fdrop(fp, curthread);
1417 if (ic->ic_socket != NULL) {
1418 ICL_CONN_UNLOCK(ic);
1419 fdrop(fp, curthread);
1423 ic->ic_socket = fp->f_data;
1424 fp->f_ops = &badfileops;
1426 fdrop(fp, curthread);
1427 ICL_CONN_UNLOCK(ic);
1429 error = icl_conn_start(ic);
1435 icl_soft_conn_close(struct icl_conn *ic)
1437 struct icl_pdu *pdu;
1443 * Wake up the threads, so they can properly terminate.
1445 ic->ic_disconnecting = true;
1446 while (ic->ic_receive_running || ic->ic_send_running) {
1447 cv_signal(&ic->ic_receive_cv);
1448 cv_signal(&ic->ic_send_cv);
1449 cv_wait(&ic->ic_send_cv, ic->ic_lock);
1452 /* Some other thread could close the connection same time. */
1455 ICL_CONN_UNLOCK(ic);
1458 ic->ic_socket = NULL;
1461 * Deregister socket upcalls.
1463 ICL_CONN_UNLOCK(ic);
1464 SOCKBUF_LOCK(&so->so_snd);
1465 if (so->so_snd.sb_upcall != NULL)
1466 soupcall_clear(so, SO_SND);
1467 SOCKBUF_UNLOCK(&so->so_snd);
1468 SOCKBUF_LOCK(&so->so_rcv);
1469 if (so->so_rcv.sb_upcall != NULL)
1470 soupcall_clear(so, SO_RCV);
1471 SOCKBUF_UNLOCK(&so->so_rcv);
1475 if (ic->ic_receive_pdu != NULL) {
1476 //ICL_DEBUG("freeing partially received PDU");
1477 icl_soft_conn_pdu_free(ic, ic->ic_receive_pdu);
1478 ic->ic_receive_pdu = NULL;
1482 * Remove any outstanding PDUs from the send queue.
1484 while (!STAILQ_EMPTY(&ic->ic_to_send)) {
1485 pdu = STAILQ_FIRST(&ic->ic_to_send);
1486 STAILQ_REMOVE_HEAD(&ic->ic_to_send, ip_next);
1487 icl_soft_pdu_done(pdu, ENOTCONN);
1490 KASSERT(STAILQ_EMPTY(&ic->ic_to_send),
1491 ("destroying session with non-empty send queue"));
1492 ICL_CONN_UNLOCK(ic);
1496 icl_soft_conn_task_setup(struct icl_conn *ic, struct icl_pdu *ip,
1497 struct ccb_scsiio *csio, uint32_t *task_tagp, void **prvp)
1504 icl_soft_conn_task_done(struct icl_conn *ic, void *prv)
1509 icl_soft_conn_transfer_setup(struct icl_conn *ic, union ctl_io *io,
1510 uint32_t *transfer_tag, void **prvp)
1517 icl_soft_conn_transfer_done(struct icl_conn *ic, void *prv)
1522 icl_soft_limits(struct icl_drv_limits *idl)
1525 idl->idl_max_recv_data_segment_length = 128 * 1024;
1526 idl->idl_max_send_data_segment_length = 128 * 1024;
1527 idl->idl_max_burst_length = 262144;
1528 idl->idl_first_burst_length = 65536;
1533 #ifdef ICL_KERNEL_PROXY
1535 icl_soft_conn_connect(struct icl_conn *ic, int domain, int socktype,
1536 int protocol, struct sockaddr *from_sa, struct sockaddr *to_sa)
1539 return (icl_soft_proxy_connect(ic, domain, socktype, protocol,
1544 icl_soft_handoff_sock(struct icl_conn *ic, struct socket *so)
1548 ICL_CONN_LOCK_ASSERT_NOT(ic);
1550 if (so->so_type != SOCK_STREAM)
1554 if (ic->ic_socket != NULL) {
1555 ICL_CONN_UNLOCK(ic);
1559 ICL_CONN_UNLOCK(ic);
1561 error = icl_conn_start(ic);
1565 #endif /* ICL_KERNEL_PROXY */
1572 icl_soft_pdu_zone = uma_zcreate("icl_soft_pdu",
1573 sizeof(struct icl_soft_pdu), NULL, NULL, NULL, NULL,
1575 refcount_init(&icl_ncons, 0);
1578 * The reason we call this "none" is that to the user,
1579 * it's known as "offload driver"; "offload driver: soft"
1580 * doesn't make much sense.
1582 error = icl_register("none", false, 0,
1583 icl_soft_limits, icl_soft_new_conn);
1584 KASSERT(error == 0, ("failed to register"));
1586 #if defined(ICL_KERNEL_PROXY) && 0
1588 * Debugging aid for kernel proxy functionality.
1590 error = icl_register("proxytest", true, 0,
1591 icl_soft_limits, icl_soft_new_conn);
1592 KASSERT(error == 0, ("failed to register"));
1599 icl_soft_unload(void)
1605 icl_unregister("none", false);
1606 #if defined(ICL_KERNEL_PROXY) && 0
1607 icl_unregister("proxytest", true);
1610 uma_zdestroy(icl_soft_pdu_zone);
1616 icl_soft_modevent(module_t mod, int what, void *arg)
1621 return (icl_soft_load());
1623 return (icl_soft_unload());
1629 moduledata_t icl_soft_data = {
1635 DECLARE_MODULE(icl_soft, icl_soft_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1636 MODULE_DEPEND(icl_soft, icl, 1, 1, 1);
1637 MODULE_VERSION(icl_soft, 1);