2 * Copyright (c) 2012 The FreeBSD Foundation
5 * This software was developed by Edward Tomasz Napierala under sponsorship
6 * from the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * Software implementation of iSCSI Common Layer kobj(9) interface.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/capsicum.h>
40 #include <sys/condvar.h>
43 #include <sys/kernel.h>
44 #include <sys/kthread.h>
47 #include <sys/mutex.h>
48 #include <sys/module.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53 #include <sys/systm.h>
57 #include <netinet/in.h>
58 #include <netinet/tcp.h>
60 #include <dev/iscsi/icl.h>
61 #include <dev/iscsi/iscsi_proto.h>
62 #include <icl_conn_if.h>
64 static int coalesce = 1;
65 SYSCTL_INT(_kern_icl, OID_AUTO, coalesce, CTLFLAG_RWTUN,
66 &coalesce, 0, "Try to coalesce PDUs before sending");
67 static int partial_receive_len = 128 * 1024;
68 SYSCTL_INT(_kern_icl, OID_AUTO, partial_receive_len, CTLFLAG_RWTUN,
69 &partial_receive_len, 0, "Minimum read size for partially received "
71 static int sendspace = 1048576;
72 SYSCTL_INT(_kern_icl, OID_AUTO, sendspace, CTLFLAG_RWTUN,
73 &sendspace, 0, "Default send socket buffer size");
74 static int recvspace = 1048576;
75 SYSCTL_INT(_kern_icl, OID_AUTO, recvspace, CTLFLAG_RWTUN,
76 &recvspace, 0, "Default receive socket buffer size");
78 static MALLOC_DEFINE(M_ICL_SOFT, "icl_soft", "iSCSI software backend");
79 static uma_zone_t icl_pdu_zone;
81 static volatile u_int icl_ncons;
83 #define ICL_CONN_LOCK(X) mtx_lock(X->ic_lock)
84 #define ICL_CONN_UNLOCK(X) mtx_unlock(X->ic_lock)
85 #define ICL_CONN_LOCK_ASSERT(X) mtx_assert(X->ic_lock, MA_OWNED)
86 #define ICL_CONN_LOCK_ASSERT_NOT(X) mtx_assert(X->ic_lock, MA_NOTOWNED)
88 STAILQ_HEAD(icl_pdu_stailq, icl_pdu);
90 static icl_conn_new_pdu_t icl_soft_conn_new_pdu;
91 static icl_conn_pdu_free_t icl_soft_conn_pdu_free;
92 static icl_conn_pdu_data_segment_length_t
93 icl_soft_conn_pdu_data_segment_length;
94 static icl_conn_pdu_append_data_t icl_soft_conn_pdu_append_data;
95 static icl_conn_pdu_get_data_t icl_soft_conn_pdu_get_data;
96 static icl_conn_pdu_queue_t icl_soft_conn_pdu_queue;
97 static icl_conn_handoff_t icl_soft_conn_handoff;
98 static icl_conn_free_t icl_soft_conn_free;
99 static icl_conn_close_t icl_soft_conn_close;
100 static icl_conn_task_setup_t icl_soft_conn_task_setup;
101 static icl_conn_task_done_t icl_soft_conn_task_done;
102 static icl_conn_transfer_setup_t icl_soft_conn_transfer_setup;
103 static icl_conn_transfer_done_t icl_soft_conn_transfer_done;
105 static kobj_method_t icl_soft_methods[] = {
106 KOBJMETHOD(icl_conn_new_pdu, icl_soft_conn_new_pdu),
107 KOBJMETHOD(icl_conn_pdu_free, icl_soft_conn_pdu_free),
108 KOBJMETHOD(icl_conn_pdu_data_segment_length,
109 icl_soft_conn_pdu_data_segment_length),
110 KOBJMETHOD(icl_conn_pdu_append_data, icl_soft_conn_pdu_append_data),
111 KOBJMETHOD(icl_conn_pdu_get_data, icl_soft_conn_pdu_get_data),
112 KOBJMETHOD(icl_conn_pdu_queue, icl_soft_conn_pdu_queue),
113 KOBJMETHOD(icl_conn_handoff, icl_soft_conn_handoff),
114 KOBJMETHOD(icl_conn_free, icl_soft_conn_free),
115 KOBJMETHOD(icl_conn_close, icl_soft_conn_close),
116 KOBJMETHOD(icl_conn_task_setup, icl_soft_conn_task_setup),
117 KOBJMETHOD(icl_conn_task_done, icl_soft_conn_task_done),
118 KOBJMETHOD(icl_conn_transfer_setup, icl_soft_conn_transfer_setup),
119 KOBJMETHOD(icl_conn_transfer_done, icl_soft_conn_transfer_done),
123 DEFINE_CLASS(icl_soft, icl_soft_methods, sizeof(struct icl_conn));
126 icl_conn_fail(struct icl_conn *ic)
128 if (ic->ic_socket == NULL)
134 ic->ic_socket->so_error = EDOOFUS;
139 icl_conn_receive(struct icl_conn *ic, size_t len)
148 memset(&uio, 0, sizeof(uio));
151 flags = MSG_DONTWAIT;
152 error = soreceive(so, NULL, &uio, &m, NULL, &flags);
154 ICL_DEBUG("soreceive error %d", error);
157 if (uio.uio_resid != 0) {
159 ICL_DEBUG("short read");
166 static struct icl_pdu *
167 icl_pdu_new_empty(struct icl_conn *ic, int flags)
172 refcount_acquire(&ic->ic_outstanding_pdus);
174 ip = uma_zalloc(icl_pdu_zone, flags | M_ZERO);
176 ICL_WARN("failed to allocate %zd bytes", sizeof(*ip));
178 refcount_release(&ic->ic_outstanding_pdus);
189 icl_pdu_free(struct icl_pdu *ip)
195 m_freem(ip->ip_bhs_mbuf);
196 m_freem(ip->ip_ahs_mbuf);
197 m_freem(ip->ip_data_mbuf);
198 uma_zfree(icl_pdu_zone, ip);
200 refcount_release(&ic->ic_outstanding_pdus);
205 icl_soft_conn_pdu_free(struct icl_conn *ic, struct icl_pdu *ip)
212 * Allocate icl_pdu with empty BHS to fill up by the caller.
215 icl_soft_conn_new_pdu(struct icl_conn *ic, int flags)
219 ip = icl_pdu_new_empty(ic, flags);
223 ip->ip_bhs_mbuf = m_getm2(NULL, sizeof(struct iscsi_bhs),
224 flags, MT_DATA, M_PKTHDR);
225 if (ip->ip_bhs_mbuf == NULL) {
226 ICL_WARN("failed to allocate %zd bytes", sizeof(*ip));
230 ip->ip_bhs = mtod(ip->ip_bhs_mbuf, struct iscsi_bhs *);
231 memset(ip->ip_bhs, 0, sizeof(struct iscsi_bhs));
232 ip->ip_bhs_mbuf->m_len = sizeof(struct iscsi_bhs);
238 icl_pdu_ahs_length(const struct icl_pdu *request)
241 return (request->ip_bhs->bhs_total_ahs_len * 4);
245 icl_pdu_data_segment_length(const struct icl_pdu *request)
249 len += request->ip_bhs->bhs_data_segment_len[0];
251 len += request->ip_bhs->bhs_data_segment_len[1];
253 len += request->ip_bhs->bhs_data_segment_len[2];
259 icl_soft_conn_pdu_data_segment_length(struct icl_conn *ic,
260 const struct icl_pdu *request)
263 return (icl_pdu_data_segment_length(request));
267 icl_pdu_set_data_segment_length(struct icl_pdu *response, uint32_t len)
270 response->ip_bhs->bhs_data_segment_len[2] = len;
271 response->ip_bhs->bhs_data_segment_len[1] = len >> 8;
272 response->ip_bhs->bhs_data_segment_len[0] = len >> 16;
276 icl_pdu_padding(const struct icl_pdu *ip)
279 if ((ip->ip_data_len % 4) != 0)
280 return (4 - (ip->ip_data_len % 4));
286 icl_pdu_size(const struct icl_pdu *response)
290 KASSERT(response->ip_ahs_len == 0, ("responding with AHS"));
292 len = sizeof(struct iscsi_bhs) + response->ip_data_len +
293 icl_pdu_padding(response);
294 if (response->ip_conn->ic_header_crc32c)
295 len += ISCSI_HEADER_DIGEST_SIZE;
296 if (response->ip_data_len != 0 && response->ip_conn->ic_data_crc32c)
297 len += ISCSI_DATA_DIGEST_SIZE;
303 icl_pdu_receive_bhs(struct icl_pdu *request, size_t *availablep)
307 m = icl_conn_receive(request->ip_conn, sizeof(struct iscsi_bhs));
309 ICL_DEBUG("failed to receive BHS");
313 request->ip_bhs_mbuf = m_pullup(m, sizeof(struct iscsi_bhs));
314 if (request->ip_bhs_mbuf == NULL) {
315 ICL_WARN("m_pullup failed");
318 request->ip_bhs = mtod(request->ip_bhs_mbuf, struct iscsi_bhs *);
321 * XXX: For architectures with strict alignment requirements
322 * we may need to allocate ip_bhs and copy the data into it.
323 * For some reason, though, not doing this doesn't seem
324 * to cause problems; tested on sparc64.
327 *availablep -= sizeof(struct iscsi_bhs);
332 icl_pdu_receive_ahs(struct icl_pdu *request, size_t *availablep)
335 request->ip_ahs_len = icl_pdu_ahs_length(request);
336 if (request->ip_ahs_len == 0)
339 request->ip_ahs_mbuf = icl_conn_receive(request->ip_conn,
340 request->ip_ahs_len);
341 if (request->ip_ahs_mbuf == NULL) {
342 ICL_DEBUG("failed to receive AHS");
346 *availablep -= request->ip_ahs_len;
351 icl_mbuf_to_crc32c(const struct mbuf *m0)
353 uint32_t digest = 0xffffffff;
354 const struct mbuf *m;
356 for (m = m0; m != NULL; m = m->m_next)
357 digest = calculate_crc32c(digest,
358 mtod(m, const void *), m->m_len);
360 digest = digest ^ 0xffffffff;
366 icl_pdu_check_header_digest(struct icl_pdu *request, size_t *availablep)
369 uint32_t received_digest, valid_digest;
371 if (request->ip_conn->ic_header_crc32c == false)
374 m = icl_conn_receive(request->ip_conn, ISCSI_HEADER_DIGEST_SIZE);
376 ICL_DEBUG("failed to receive header digest");
380 CTASSERT(sizeof(received_digest) == ISCSI_HEADER_DIGEST_SIZE);
381 m_copydata(m, 0, ISCSI_HEADER_DIGEST_SIZE, (void *)&received_digest);
384 *availablep -= ISCSI_HEADER_DIGEST_SIZE;
389 valid_digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
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)
524 uint32_t received_digest, valid_digest;
526 if (request->ip_conn->ic_data_crc32c == false)
529 if (request->ip_data_len == 0)
532 m = icl_conn_receive(request->ip_conn, ISCSI_DATA_DIGEST_SIZE);
534 ICL_DEBUG("failed to receive data digest");
538 CTASSERT(sizeof(received_digest) == ISCSI_DATA_DIGEST_SIZE);
539 m_copydata(m, 0, ISCSI_DATA_DIGEST_SIZE, (void *)&received_digest);
542 *availablep -= ISCSI_DATA_DIGEST_SIZE;
545 * Note that ip_data_mbuf also contains padding; since digest
546 * calculation is supposed to include that, we iterate over
547 * the entire ip_data_mbuf chain, not just ip_data_len bytes of it.
549 valid_digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
550 if (received_digest != valid_digest) {
551 ICL_WARN("data digest check failed; got 0x%x, "
552 "should be 0x%x", received_digest, valid_digest);
560 * Somewhat contrary to the name, this attempts to receive only one
561 * "part" of PDU at a time; call it repeatedly until it returns non-NULL.
563 static struct icl_pdu *
564 icl_conn_receive_pdu(struct icl_conn *ic, size_t *availablep)
566 struct icl_pdu *request;
574 if (ic->ic_receive_state == ICL_CONN_STATE_BHS) {
575 KASSERT(ic->ic_receive_pdu == NULL,
576 ("ic->ic_receive_pdu != NULL"));
577 request = icl_pdu_new_empty(ic, M_NOWAIT);
578 if (request == NULL) {
579 ICL_DEBUG("failed to allocate PDU; "
580 "dropping connection");
584 ic->ic_receive_pdu = request;
586 KASSERT(ic->ic_receive_pdu != NULL,
587 ("ic->ic_receive_pdu == NULL"));
588 request = ic->ic_receive_pdu;
591 if (*availablep < ic->ic_receive_len) {
593 ICL_DEBUG("not enough data; need %zd, "
594 "have %zd", ic->ic_receive_len, *availablep);
599 switch (ic->ic_receive_state) {
600 case ICL_CONN_STATE_BHS:
601 //ICL_DEBUG("receiving BHS");
602 error = icl_pdu_receive_bhs(request, availablep);
604 ICL_DEBUG("failed to receive BHS; "
605 "dropping connection");
610 * We don't enforce any limit for AHS length;
611 * its length is stored in 8 bit field.
614 len = icl_pdu_data_segment_length(request);
615 if (len > ic->ic_max_data_segment_length) {
616 ICL_WARN("received data segment "
617 "length %zd is larger than negotiated "
618 "MaxDataSegmentLength %zd; "
619 "dropping connection",
620 len, ic->ic_max_data_segment_length);
625 ic->ic_receive_state = ICL_CONN_STATE_AHS;
626 ic->ic_receive_len = icl_pdu_ahs_length(request);
629 case ICL_CONN_STATE_AHS:
630 //ICL_DEBUG("receiving AHS");
631 error = icl_pdu_receive_ahs(request, availablep);
633 ICL_DEBUG("failed to receive AHS; "
634 "dropping connection");
637 ic->ic_receive_state = ICL_CONN_STATE_HEADER_DIGEST;
638 if (ic->ic_header_crc32c == false)
639 ic->ic_receive_len = 0;
641 ic->ic_receive_len = ISCSI_HEADER_DIGEST_SIZE;
644 case ICL_CONN_STATE_HEADER_DIGEST:
645 //ICL_DEBUG("receiving header digest");
646 error = icl_pdu_check_header_digest(request, availablep);
648 ICL_DEBUG("header digest failed; "
649 "dropping connection");
653 ic->ic_receive_state = ICL_CONN_STATE_DATA;
655 icl_pdu_data_segment_receive_len(request);
658 case ICL_CONN_STATE_DATA:
659 //ICL_DEBUG("receiving data segment");
660 error = icl_pdu_receive_data_segment(request, availablep,
663 ICL_DEBUG("failed to receive data segment;"
664 "dropping connection");
671 ic->ic_receive_state = ICL_CONN_STATE_DATA_DIGEST;
672 if (request->ip_data_len == 0 || ic->ic_data_crc32c == false)
673 ic->ic_receive_len = 0;
675 ic->ic_receive_len = ISCSI_DATA_DIGEST_SIZE;
678 case ICL_CONN_STATE_DATA_DIGEST:
679 //ICL_DEBUG("receiving data digest");
680 error = icl_pdu_check_data_digest(request, availablep);
682 ICL_DEBUG("data digest failed; "
683 "dropping connection");
688 * We've received complete PDU; reset the receive state machine
689 * and return the PDU.
691 ic->ic_receive_state = ICL_CONN_STATE_BHS;
692 ic->ic_receive_len = sizeof(struct iscsi_bhs);
693 ic->ic_receive_pdu = NULL;
697 panic("invalid ic_receive_state %d\n", ic->ic_receive_state);
702 * Don't free the PDU; it's pointed to by ic->ic_receive_pdu
703 * and will get freed in icl_soft_conn_close().
712 icl_conn_receive_pdus(struct icl_conn *ic, size_t available)
714 struct icl_pdu *response;
720 * This can never happen; we're careful to only mess with ic->ic_socket
721 * pointer when the send/receive threads are not running.
723 KASSERT(so != NULL, ("NULL socket"));
726 if (ic->ic_disconnecting)
729 if (so->so_error != 0) {
730 ICL_DEBUG("connection error %d; "
731 "dropping connection", so->so_error);
737 * Loop until we have a complete PDU or there is not enough
738 * data in the socket buffer.
740 if (available < ic->ic_receive_len) {
742 ICL_DEBUG("not enough data; have %zd, "
743 "need %zd", available,
749 response = icl_conn_receive_pdu(ic, &available);
750 if (response == NULL)
753 if (response->ip_ahs_len > 0) {
754 ICL_WARN("received PDU with unsupported "
755 "AHS; opcode 0x%x; dropping connection",
756 response->ip_bhs->bhs_opcode);
757 icl_pdu_free(response);
762 (ic->ic_receive)(response);
767 icl_receive_thread(void *arg)
777 if (ic->ic_disconnecting) {
778 //ICL_DEBUG("terminating");
783 * Set the low watermark, to be checked by
784 * soreadable() in icl_soupcall_receive()
785 * to avoid unneccessary wakeups until there
786 * is enough data received to read the PDU.
788 SOCKBUF_LOCK(&so->so_rcv);
789 available = sbavail(&so->so_rcv);
790 if (available < ic->ic_receive_len) {
791 so->so_rcv.sb_lowat = ic->ic_receive_len;
792 cv_wait(&ic->ic_receive_cv, &so->so_rcv.sb_mtx);
794 so->so_rcv.sb_lowat = so->so_rcv.sb_hiwat + 1;
795 SOCKBUF_UNLOCK(&so->so_rcv);
797 icl_conn_receive_pdus(ic, available);
801 ic->ic_receive_running = false;
802 cv_signal(&ic->ic_send_cv);
808 icl_soupcall_receive(struct socket *so, void *arg, int waitflag)
816 cv_signal(&ic->ic_receive_cv);
821 icl_pdu_finalize(struct icl_pdu *request)
823 size_t padding, pdu_len;
824 uint32_t digest, zero = 0;
828 ic = request->ip_conn;
830 icl_pdu_set_data_segment_length(request, request->ip_data_len);
832 pdu_len = icl_pdu_size(request);
834 if (ic->ic_header_crc32c) {
835 digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
836 ok = m_append(request->ip_bhs_mbuf, sizeof(digest),
839 ICL_WARN("failed to append header digest");
844 if (request->ip_data_len != 0) {
845 padding = icl_pdu_padding(request);
847 ok = m_append(request->ip_data_mbuf, padding,
850 ICL_WARN("failed to append padding");
855 if (ic->ic_data_crc32c) {
856 digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
858 ok = m_append(request->ip_data_mbuf, sizeof(digest),
861 ICL_WARN("failed to append data digest");
866 m_cat(request->ip_bhs_mbuf, request->ip_data_mbuf);
867 request->ip_data_mbuf = NULL;
870 request->ip_bhs_mbuf->m_pkthdr.len = pdu_len;
876 icl_conn_send_pdus(struct icl_conn *ic, struct icl_pdu_stailq *queue)
878 struct icl_pdu *request, *request2;
880 size_t available, size, size2;
881 int coalesced, error;
883 ICL_CONN_LOCK_ASSERT_NOT(ic);
887 SOCKBUF_LOCK(&so->so_snd);
889 * Check how much space do we have for transmit. We can't just
890 * call sosend() and retry when we get EWOULDBLOCK or EMSGSIZE,
891 * as it always frees the mbuf chain passed to it, even in case
894 available = sbspace(&so->so_snd);
897 * Notify the socket upcall that we don't need wakeups
898 * for the time being.
900 so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1;
901 SOCKBUF_UNLOCK(&so->so_snd);
903 while (!STAILQ_EMPTY(queue)) {
904 request = STAILQ_FIRST(queue);
905 size = icl_pdu_size(request);
906 if (available < size) {
909 * Set the low watermark, to be checked by
910 * sowriteable() in icl_soupcall_send()
911 * to avoid unneccessary wakeups until there
912 * is enough space for the PDU to fit.
914 SOCKBUF_LOCK(&so->so_snd);
915 available = sbspace(&so->so_snd);
916 if (available < size) {
918 ICL_DEBUG("no space to send; "
919 "have %zd, need %zd",
922 so->so_snd.sb_lowat = size;
923 SOCKBUF_UNLOCK(&so->so_snd);
926 SOCKBUF_UNLOCK(&so->so_snd);
928 STAILQ_REMOVE_HEAD(queue, ip_next);
929 error = icl_pdu_finalize(request);
931 ICL_DEBUG("failed to finalize PDU; "
932 "dropping connection");
934 icl_pdu_free(request);
940 request2 = STAILQ_FIRST(queue);
941 if (request2 == NULL)
943 size2 = icl_pdu_size(request2);
944 if (available < size + size2)
946 STAILQ_REMOVE_HEAD(queue, ip_next);
947 error = icl_pdu_finalize(request2);
949 ICL_DEBUG("failed to finalize PDU; "
950 "dropping connection");
952 icl_pdu_free(request);
953 icl_pdu_free(request2);
956 m_cat(request->ip_bhs_mbuf, request2->ip_bhs_mbuf);
957 request2->ip_bhs_mbuf = NULL;
958 request->ip_bhs_mbuf->m_pkthdr.len += size2;
960 STAILQ_REMOVE_AFTER(queue, request, ip_next);
961 icl_pdu_free(request2);
966 ICL_DEBUG("coalesced %d PDUs into %zd bytes",
972 error = sosend(so, NULL, NULL, request->ip_bhs_mbuf,
973 NULL, MSG_DONTWAIT, curthread);
974 request->ip_bhs_mbuf = NULL; /* Sosend consumes the mbuf. */
976 ICL_DEBUG("failed to send PDU, error %d; "
977 "dropping connection", error);
979 icl_pdu_free(request);
982 icl_pdu_free(request);
987 icl_send_thread(void *arg)
990 struct icl_pdu_stailq queue;
1000 * If the local queue is empty, populate it from
1001 * the main one. This way the icl_conn_send_pdus()
1002 * can go through all the queued PDUs without holding
1005 if (STAILQ_EMPTY(&queue))
1006 STAILQ_SWAP(&ic->ic_to_send, &queue, icl_pdu);
1008 ic->ic_check_send_space = false;
1009 ICL_CONN_UNLOCK(ic);
1010 icl_conn_send_pdus(ic, &queue);
1014 * The icl_soupcall_send() was called since the last
1015 * call to sbspace(); go around;
1017 if (ic->ic_check_send_space)
1021 * Local queue is empty, but we still have PDUs
1022 * in the main one; go around.
1024 if (STAILQ_EMPTY(&queue) &&
1025 !STAILQ_EMPTY(&ic->ic_to_send))
1029 * There might be some stuff in the local queue,
1030 * which didn't get sent due to not having enough send
1031 * space. Wait for socket upcall.
1036 if (ic->ic_disconnecting) {
1037 //ICL_DEBUG("terminating");
1041 cv_wait(&ic->ic_send_cv, ic->ic_lock);
1045 * We're exiting; move PDUs back to the main queue, so they can
1046 * get freed properly. At this point ordering doesn't matter.
1048 STAILQ_CONCAT(&ic->ic_to_send, &queue);
1050 ic->ic_send_running = false;
1051 cv_signal(&ic->ic_send_cv);
1052 ICL_CONN_UNLOCK(ic);
1057 icl_soupcall_send(struct socket *so, void *arg, int waitflag)
1059 struct icl_conn *ic;
1061 if (!sowriteable(so))
1067 ic->ic_check_send_space = true;
1068 ICL_CONN_UNLOCK(ic);
1070 cv_signal(&ic->ic_send_cv);
1076 icl_pdu_append_data(struct icl_pdu *request, const void *addr, size_t len,
1079 struct mbuf *mb, *newmb;
1080 size_t copylen, off = 0;
1082 KASSERT(len > 0, ("len == 0"));
1084 newmb = m_getm2(NULL, len, flags, MT_DATA, M_PKTHDR);
1085 if (newmb == NULL) {
1086 ICL_WARN("failed to allocate mbuf for %zd bytes", len);
1090 for (mb = newmb; mb != NULL; mb = mb->m_next) {
1091 copylen = min(M_TRAILINGSPACE(mb), len - off);
1092 memcpy(mtod(mb, char *), (const char *)addr + off, copylen);
1093 mb->m_len = copylen;
1096 KASSERT(off == len, ("%s: off != len", __func__));
1098 if (request->ip_data_mbuf == NULL) {
1099 request->ip_data_mbuf = newmb;
1100 request->ip_data_len = len;
1102 m_cat(request->ip_data_mbuf, newmb);
1103 request->ip_data_len += len;
1110 icl_soft_conn_pdu_append_data(struct icl_conn *ic, struct icl_pdu *request,
1111 const void *addr, size_t len, int flags)
1114 return (icl_pdu_append_data(request, addr, len, flags));
1118 icl_pdu_get_data(struct icl_pdu *ip, size_t off, void *addr, size_t len)
1121 m_copydata(ip->ip_data_mbuf, off, len, addr);
1125 icl_soft_conn_pdu_get_data(struct icl_conn *ic, struct icl_pdu *ip,
1126 size_t off, void *addr, size_t len)
1129 return (icl_pdu_get_data(ip, off, addr, len));
1133 icl_pdu_queue(struct icl_pdu *ip)
1135 struct icl_conn *ic;
1139 ICL_CONN_LOCK_ASSERT(ic);
1141 if (ic->ic_disconnecting || ic->ic_socket == NULL) {
1142 ICL_DEBUG("icl_pdu_queue on closed connection");
1147 if (!STAILQ_EMPTY(&ic->ic_to_send)) {
1148 STAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next);
1150 * If the queue is not empty, someone else had already
1151 * signaled the send thread; no need to do that again,
1157 STAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next);
1158 cv_signal(&ic->ic_send_cv);
1162 icl_soft_conn_pdu_queue(struct icl_conn *ic, struct icl_pdu *ip)
1168 static struct icl_conn *
1169 icl_soft_new_conn(const char *name, struct mtx *lock)
1171 struct icl_conn *ic;
1173 refcount_acquire(&icl_ncons);
1175 ic = (struct icl_conn *)kobj_create(&icl_soft_class, M_ICL_SOFT, M_WAITOK | M_ZERO);
1177 STAILQ_INIT(&ic->ic_to_send);
1179 cv_init(&ic->ic_send_cv, "icl_tx");
1180 cv_init(&ic->ic_receive_cv, "icl_rx");
1182 refcount_init(&ic->ic_outstanding_pdus, 0);
1184 ic->ic_max_data_segment_length = ICL_MAX_DATA_SEGMENT_LENGTH;
1186 ic->ic_offload = "None";
1192 icl_soft_conn_free(struct icl_conn *ic)
1195 cv_destroy(&ic->ic_send_cv);
1196 cv_destroy(&ic->ic_receive_cv);
1197 kobj_delete((struct kobj *)ic, M_ICL_SOFT);
1198 refcount_release(&icl_ncons);
1202 icl_conn_start(struct icl_conn *ic)
1213 if (ic->ic_socket == NULL) {
1214 ICL_CONN_UNLOCK(ic);
1218 ic->ic_receive_state = ICL_CONN_STATE_BHS;
1219 ic->ic_receive_len = sizeof(struct iscsi_bhs);
1220 ic->ic_disconnecting = false;
1222 ICL_CONN_UNLOCK(ic);
1225 * For sendspace, this is required because the current code cannot
1226 * send a PDU in pieces; thus, the minimum buffer size is equal
1227 * to the maximum PDU size. "+4" is to account for possible padding.
1229 * What we should actually do here is to use autoscaling, but set
1230 * some minimal buffer size to "minspace". I don't know a way to do
1233 minspace = sizeof(struct iscsi_bhs) + ic->ic_max_data_segment_length +
1234 ISCSI_HEADER_DIGEST_SIZE + ISCSI_DATA_DIGEST_SIZE + 4;
1235 if (sendspace < minspace) {
1236 ICL_WARN("kern.icl.sendspace too low; must be at least %zd",
1238 sendspace = minspace;
1240 if (recvspace < minspace) {
1241 ICL_WARN("kern.icl.recvspace too low; must be at least %zd",
1243 recvspace = minspace;
1246 error = soreserve(ic->ic_socket, sendspace, recvspace);
1248 ICL_WARN("soreserve failed with error %d", error);
1249 icl_soft_conn_close(ic);
1252 ic->ic_socket->so_snd.sb_flags |= SB_AUTOSIZE;
1253 ic->ic_socket->so_rcv.sb_flags |= SB_AUTOSIZE;
1258 bzero(&opt, sizeof(opt));
1259 opt.sopt_dir = SOPT_SET;
1260 opt.sopt_level = IPPROTO_TCP;
1261 opt.sopt_name = TCP_NODELAY;
1262 opt.sopt_val = &one;
1263 opt.sopt_valsize = sizeof(one);
1264 error = sosetopt(ic->ic_socket, &opt);
1266 ICL_WARN("disabling TCP_NODELAY failed with error %d", error);
1267 icl_soft_conn_close(ic);
1272 * Register socket upcall, to get notified about incoming PDUs
1273 * and free space to send outgoing ones.
1275 SOCKBUF_LOCK(&ic->ic_socket->so_snd);
1276 soupcall_set(ic->ic_socket, SO_SND, icl_soupcall_send, ic);
1277 SOCKBUF_UNLOCK(&ic->ic_socket->so_snd);
1278 SOCKBUF_LOCK(&ic->ic_socket->so_rcv);
1279 soupcall_set(ic->ic_socket, SO_RCV, icl_soupcall_receive, ic);
1280 SOCKBUF_UNLOCK(&ic->ic_socket->so_rcv);
1286 ic->ic_send_running = ic->ic_receive_running = true;
1287 ICL_CONN_UNLOCK(ic);
1288 error = kthread_add(icl_send_thread, ic, NULL, NULL, 0, 0, "%stx",
1291 ICL_WARN("kthread_add(9) failed with error %d", error);
1293 ic->ic_send_running = ic->ic_receive_running = false;
1294 cv_signal(&ic->ic_send_cv);
1295 ICL_CONN_UNLOCK(ic);
1296 icl_soft_conn_close(ic);
1299 error = kthread_add(icl_receive_thread, ic, NULL, NULL, 0, 0, "%srx",
1302 ICL_WARN("kthread_add(9) failed with error %d", error);
1304 ic->ic_receive_running = false;
1305 cv_signal(&ic->ic_send_cv);
1306 ICL_CONN_UNLOCK(ic);
1307 icl_soft_conn_close(ic);
1315 icl_soft_conn_handoff(struct icl_conn *ic, int fd)
1319 cap_rights_t rights;
1322 ICL_CONN_LOCK_ASSERT_NOT(ic);
1325 * Steal the socket from userland.
1327 error = fget(curthread, fd,
1328 cap_rights_init(&rights, CAP_SOCK_CLIENT), &fp);
1331 if (fp->f_type != DTYPE_SOCKET) {
1332 fdrop(fp, curthread);
1336 if (so->so_type != SOCK_STREAM) {
1337 fdrop(fp, curthread);
1343 if (ic->ic_socket != NULL) {
1344 ICL_CONN_UNLOCK(ic);
1345 fdrop(fp, curthread);
1349 ic->ic_socket = fp->f_data;
1350 fp->f_ops = &badfileops;
1352 fdrop(fp, curthread);
1353 ICL_CONN_UNLOCK(ic);
1355 error = icl_conn_start(ic);
1361 icl_soft_conn_close(struct icl_conn *ic)
1363 struct icl_pdu *pdu;
1369 * Wake up the threads, so they can properly terminate.
1371 ic->ic_disconnecting = true;
1372 while (ic->ic_receive_running || ic->ic_send_running) {
1373 cv_signal(&ic->ic_receive_cv);
1374 cv_signal(&ic->ic_send_cv);
1375 cv_wait(&ic->ic_send_cv, ic->ic_lock);
1378 /* Some other thread could close the connection same time. */
1381 ICL_CONN_UNLOCK(ic);
1384 ic->ic_socket = NULL;
1387 * Deregister socket upcalls.
1389 ICL_CONN_UNLOCK(ic);
1390 SOCKBUF_LOCK(&so->so_snd);
1391 if (so->so_snd.sb_upcall != NULL)
1392 soupcall_clear(so, SO_SND);
1393 SOCKBUF_UNLOCK(&so->so_snd);
1394 SOCKBUF_LOCK(&so->so_rcv);
1395 if (so->so_rcv.sb_upcall != NULL)
1396 soupcall_clear(so, SO_RCV);
1397 SOCKBUF_UNLOCK(&so->so_rcv);
1401 if (ic->ic_receive_pdu != NULL) {
1402 //ICL_DEBUG("freeing partially received PDU");
1403 icl_pdu_free(ic->ic_receive_pdu);
1404 ic->ic_receive_pdu = NULL;
1408 * Remove any outstanding PDUs from the send queue.
1410 while (!STAILQ_EMPTY(&ic->ic_to_send)) {
1411 pdu = STAILQ_FIRST(&ic->ic_to_send);
1412 STAILQ_REMOVE_HEAD(&ic->ic_to_send, ip_next);
1416 KASSERT(STAILQ_EMPTY(&ic->ic_to_send),
1417 ("destroying session with non-empty send queue"));
1419 KASSERT(ic->ic_outstanding_pdus == 0,
1420 ("destroying session with %d outstanding PDUs",
1421 ic->ic_outstanding_pdus));
1423 ICL_CONN_UNLOCK(ic);
1427 icl_soft_conn_task_setup(struct icl_conn *ic, struct ccb_scsiio *csio,
1428 uint32_t *task_tagp, void **prvp)
1435 icl_soft_conn_task_done(struct icl_conn *ic, void *prv)
1440 icl_soft_conn_transfer_setup(struct icl_conn *ic, union ctl_io *io,
1441 uint32_t *transfer_tag, void **prvp)
1448 icl_soft_conn_transfer_done(struct icl_conn *ic, void *prv)
1453 icl_soft_limits(size_t *limitp)
1456 *limitp = 128 * 1024;
1461 #ifdef ICL_KERNEL_PROXY
1463 icl_conn_handoff_sock(struct icl_conn *ic, struct socket *so)
1467 ICL_CONN_LOCK_ASSERT_NOT(ic);
1469 if (so->so_type != SOCK_STREAM)
1473 if (ic->ic_socket != NULL) {
1474 ICL_CONN_UNLOCK(ic);
1478 ICL_CONN_UNLOCK(ic);
1480 error = icl_conn_start(ic);
1484 #endif /* ICL_KERNEL_PROXY */
1491 icl_pdu_zone = uma_zcreate("icl_pdu",
1492 sizeof(struct icl_pdu), NULL, NULL, NULL, NULL,
1494 refcount_init(&icl_ncons, 0);
1497 * The reason we call this "none" is that to the user,
1498 * it's known as "offload driver"; "offload driver: soft"
1499 * doesn't make much sense.
1501 error = icl_register("none", 0, icl_soft_limits, icl_soft_new_conn);
1502 KASSERT(error == 0, ("failed to register"));
1508 icl_soft_unload(void)
1514 icl_unregister("none");
1516 uma_zdestroy(icl_pdu_zone);
1522 icl_soft_modevent(module_t mod, int what, void *arg)
1527 return (icl_soft_load());
1529 return (icl_soft_unload());
1535 moduledata_t icl_soft_data = {
1541 DECLARE_MODULE(icl_soft, icl_soft_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1542 MODULE_DEPEND(icl_soft, icl, 1, 1, 1);
1543 MODULE_VERSION(icl_soft, 1);