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_connected_t icl_soft_conn_connected;
102 static kobj_method_t icl_soft_methods[] = {
103 KOBJMETHOD(icl_conn_new_pdu, icl_soft_conn_new_pdu),
104 KOBJMETHOD(icl_conn_pdu_free, icl_soft_conn_pdu_free),
105 KOBJMETHOD(icl_conn_pdu_data_segment_length,
106 icl_soft_conn_pdu_data_segment_length),
107 KOBJMETHOD(icl_conn_pdu_append_data, icl_soft_conn_pdu_append_data),
108 KOBJMETHOD(icl_conn_pdu_get_data, icl_soft_conn_pdu_get_data),
109 KOBJMETHOD(icl_conn_pdu_queue, icl_soft_conn_pdu_queue),
110 KOBJMETHOD(icl_conn_handoff, icl_soft_conn_handoff),
111 KOBJMETHOD(icl_conn_free, icl_soft_conn_free),
112 KOBJMETHOD(icl_conn_close, icl_soft_conn_close),
113 KOBJMETHOD(icl_conn_connected, icl_soft_conn_connected),
117 DEFINE_CLASS(icl_soft, icl_soft_methods, sizeof(struct icl_conn));
120 icl_conn_fail(struct icl_conn *ic)
122 if (ic->ic_socket == NULL)
128 ic->ic_socket->so_error = EDOOFUS;
133 icl_conn_receive(struct icl_conn *ic, size_t len)
142 memset(&uio, 0, sizeof(uio));
145 flags = MSG_DONTWAIT;
146 error = soreceive(so, NULL, &uio, &m, NULL, &flags);
148 ICL_DEBUG("soreceive error %d", error);
151 if (uio.uio_resid != 0) {
153 ICL_DEBUG("short read");
160 static struct icl_pdu *
161 icl_pdu_new_empty(struct icl_conn *ic, int flags)
166 refcount_acquire(&ic->ic_outstanding_pdus);
168 ip = uma_zalloc(icl_pdu_zone, flags | M_ZERO);
170 ICL_WARN("failed to allocate %zd bytes", sizeof(*ip));
172 refcount_release(&ic->ic_outstanding_pdus);
183 icl_pdu_free(struct icl_pdu *ip)
189 m_freem(ip->ip_bhs_mbuf);
190 m_freem(ip->ip_ahs_mbuf);
191 m_freem(ip->ip_data_mbuf);
192 uma_zfree(icl_pdu_zone, ip);
194 refcount_release(&ic->ic_outstanding_pdus);
199 icl_soft_conn_pdu_free(struct icl_conn *ic, struct icl_pdu *ip)
206 * Allocate icl_pdu with empty BHS to fill up by the caller.
209 icl_soft_conn_new_pdu(struct icl_conn *ic, int flags)
213 ip = icl_pdu_new_empty(ic, flags);
217 ip->ip_bhs_mbuf = m_getm2(NULL, sizeof(struct iscsi_bhs),
218 flags, MT_DATA, M_PKTHDR);
219 if (ip->ip_bhs_mbuf == NULL) {
220 ICL_WARN("failed to allocate %zd bytes", sizeof(*ip));
224 ip->ip_bhs = mtod(ip->ip_bhs_mbuf, struct iscsi_bhs *);
225 memset(ip->ip_bhs, 0, sizeof(struct iscsi_bhs));
226 ip->ip_bhs_mbuf->m_len = sizeof(struct iscsi_bhs);
232 icl_pdu_ahs_length(const struct icl_pdu *request)
235 return (request->ip_bhs->bhs_total_ahs_len * 4);
239 icl_pdu_data_segment_length(const struct icl_pdu *request)
243 len += request->ip_bhs->bhs_data_segment_len[0];
245 len += request->ip_bhs->bhs_data_segment_len[1];
247 len += request->ip_bhs->bhs_data_segment_len[2];
253 icl_soft_conn_pdu_data_segment_length(struct icl_conn *ic,
254 const struct icl_pdu *request)
257 return (icl_pdu_data_segment_length(request));
261 icl_pdu_set_data_segment_length(struct icl_pdu *response, uint32_t len)
264 response->ip_bhs->bhs_data_segment_len[2] = len;
265 response->ip_bhs->bhs_data_segment_len[1] = len >> 8;
266 response->ip_bhs->bhs_data_segment_len[0] = len >> 16;
270 icl_pdu_padding(const struct icl_pdu *ip)
273 if ((ip->ip_data_len % 4) != 0)
274 return (4 - (ip->ip_data_len % 4));
280 icl_pdu_size(const struct icl_pdu *response)
284 KASSERT(response->ip_ahs_len == 0, ("responding with AHS"));
286 len = sizeof(struct iscsi_bhs) + response->ip_data_len +
287 icl_pdu_padding(response);
288 if (response->ip_conn->ic_header_crc32c)
289 len += ISCSI_HEADER_DIGEST_SIZE;
290 if (response->ip_data_len != 0 && response->ip_conn->ic_data_crc32c)
291 len += ISCSI_DATA_DIGEST_SIZE;
297 icl_pdu_receive_bhs(struct icl_pdu *request, size_t *availablep)
301 m = icl_conn_receive(request->ip_conn, sizeof(struct iscsi_bhs));
303 ICL_DEBUG("failed to receive BHS");
307 request->ip_bhs_mbuf = m_pullup(m, sizeof(struct iscsi_bhs));
308 if (request->ip_bhs_mbuf == NULL) {
309 ICL_WARN("m_pullup failed");
312 request->ip_bhs = mtod(request->ip_bhs_mbuf, struct iscsi_bhs *);
315 * XXX: For architectures with strict alignment requirements
316 * we may need to allocate ip_bhs and copy the data into it.
317 * For some reason, though, not doing this doesn't seem
318 * to cause problems; tested on sparc64.
321 *availablep -= sizeof(struct iscsi_bhs);
326 icl_pdu_receive_ahs(struct icl_pdu *request, size_t *availablep)
329 request->ip_ahs_len = icl_pdu_ahs_length(request);
330 if (request->ip_ahs_len == 0)
333 request->ip_ahs_mbuf = icl_conn_receive(request->ip_conn,
334 request->ip_ahs_len);
335 if (request->ip_ahs_mbuf == NULL) {
336 ICL_DEBUG("failed to receive AHS");
340 *availablep -= request->ip_ahs_len;
345 icl_mbuf_to_crc32c(const struct mbuf *m0)
347 uint32_t digest = 0xffffffff;
348 const struct mbuf *m;
350 for (m = m0; m != NULL; m = m->m_next)
351 digest = calculate_crc32c(digest,
352 mtod(m, const void *), m->m_len);
354 digest = digest ^ 0xffffffff;
360 icl_pdu_check_header_digest(struct icl_pdu *request, size_t *availablep)
363 uint32_t received_digest, valid_digest;
365 if (request->ip_conn->ic_header_crc32c == false)
368 m = icl_conn_receive(request->ip_conn, ISCSI_HEADER_DIGEST_SIZE);
370 ICL_DEBUG("failed to receive header digest");
374 CTASSERT(sizeof(received_digest) == ISCSI_HEADER_DIGEST_SIZE);
375 m_copydata(m, 0, ISCSI_HEADER_DIGEST_SIZE, (void *)&received_digest);
378 *availablep -= ISCSI_HEADER_DIGEST_SIZE;
383 valid_digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
384 if (received_digest != valid_digest) {
385 ICL_WARN("header digest check failed; got 0x%x, "
386 "should be 0x%x", received_digest, valid_digest);
394 * Return the number of bytes that should be waiting in the receive socket
395 * before icl_pdu_receive_data_segment() gets called.
398 icl_pdu_data_segment_receive_len(const struct icl_pdu *request)
402 len = icl_pdu_data_segment_length(request);
407 * Account for the parts of data segment already read from
410 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
411 len -= request->ip_data_len;
414 * Don't always wait for the full data segment to be delivered
415 * to the socket; this might badly affect performance due to
416 * TCP window scaling.
418 if (len > partial_receive_len) {
420 ICL_DEBUG("need %zd bytes of data, limiting to %zd",
421 len, partial_receive_len));
423 len = partial_receive_len;
429 * Account for padding. Note that due to the way code is written,
430 * the icl_pdu_receive_data_segment() must always receive padding
431 * along with the last part of data segment, because it would be
432 * impossible to tell whether we've already received the full data
433 * segment including padding, or without it.
436 len += 4 - (len % 4);
439 ICL_DEBUG("need %zd bytes of data", len));
446 icl_pdu_receive_data_segment(struct icl_pdu *request,
447 size_t *availablep, bool *more_neededp)
450 size_t len, padding = 0;
453 ic = request->ip_conn;
455 *more_neededp = false;
456 ic->ic_receive_len = 0;
458 len = icl_pdu_data_segment_length(request);
463 padding = 4 - (len % 4);
466 * Account for already received parts of data segment.
468 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
469 len -= request->ip_data_len;
471 if (len + padding > *availablep) {
473 * Not enough data in the socket buffer. Receive as much
474 * as we can. Don't receive padding, since, obviously, it's
475 * not the end of data segment yet.
478 ICL_DEBUG("limited from %zd to %zd",
479 len + padding, *availablep - padding));
481 len = *availablep - padding;
482 *more_neededp = true;
487 * Must not try to receive padding without at least one byte
488 * of actual data segment.
491 m = icl_conn_receive(request->ip_conn, len + padding);
493 ICL_DEBUG("failed to receive data segment");
497 if (request->ip_data_mbuf == NULL)
498 request->ip_data_mbuf = m;
500 m_cat(request->ip_data_mbuf, m);
502 request->ip_data_len += len;
503 *availablep -= len + padding;
509 icl_pdu_data_segment_receive_len(request);
515 icl_pdu_check_data_digest(struct icl_pdu *request, size_t *availablep)
518 uint32_t received_digest, valid_digest;
520 if (request->ip_conn->ic_data_crc32c == false)
523 if (request->ip_data_len == 0)
526 m = icl_conn_receive(request->ip_conn, ISCSI_DATA_DIGEST_SIZE);
528 ICL_DEBUG("failed to receive data digest");
532 CTASSERT(sizeof(received_digest) == ISCSI_DATA_DIGEST_SIZE);
533 m_copydata(m, 0, ISCSI_DATA_DIGEST_SIZE, (void *)&received_digest);
536 *availablep -= ISCSI_DATA_DIGEST_SIZE;
539 * Note that ip_data_mbuf also contains padding; since digest
540 * calculation is supposed to include that, we iterate over
541 * the entire ip_data_mbuf chain, not just ip_data_len bytes of it.
543 valid_digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
544 if (received_digest != valid_digest) {
545 ICL_WARN("data digest check failed; got 0x%x, "
546 "should be 0x%x", received_digest, valid_digest);
554 * Somewhat contrary to the name, this attempts to receive only one
555 * "part" of PDU at a time; call it repeatedly until it returns non-NULL.
557 static struct icl_pdu *
558 icl_conn_receive_pdu(struct icl_conn *ic, size_t *availablep)
560 struct icl_pdu *request;
568 if (ic->ic_receive_state == ICL_CONN_STATE_BHS) {
569 KASSERT(ic->ic_receive_pdu == NULL,
570 ("ic->ic_receive_pdu != NULL"));
571 request = icl_pdu_new_empty(ic, M_NOWAIT);
572 if (request == NULL) {
573 ICL_DEBUG("failed to allocate PDU; "
574 "dropping connection");
578 ic->ic_receive_pdu = request;
580 KASSERT(ic->ic_receive_pdu != NULL,
581 ("ic->ic_receive_pdu == NULL"));
582 request = ic->ic_receive_pdu;
585 if (*availablep < ic->ic_receive_len) {
587 ICL_DEBUG("not enough data; need %zd, "
588 "have %zd", ic->ic_receive_len, *availablep);
593 switch (ic->ic_receive_state) {
594 case ICL_CONN_STATE_BHS:
595 //ICL_DEBUG("receiving BHS");
596 error = icl_pdu_receive_bhs(request, availablep);
598 ICL_DEBUG("failed to receive BHS; "
599 "dropping connection");
604 * We don't enforce any limit for AHS length;
605 * its length is stored in 8 bit field.
608 len = icl_pdu_data_segment_length(request);
609 if (len > ic->ic_max_data_segment_length) {
610 ICL_WARN("received data segment "
611 "length %zd is larger than negotiated "
612 "MaxDataSegmentLength %zd; "
613 "dropping connection",
614 len, ic->ic_max_data_segment_length);
619 ic->ic_receive_state = ICL_CONN_STATE_AHS;
620 ic->ic_receive_len = icl_pdu_ahs_length(request);
623 case ICL_CONN_STATE_AHS:
624 //ICL_DEBUG("receiving AHS");
625 error = icl_pdu_receive_ahs(request, availablep);
627 ICL_DEBUG("failed to receive AHS; "
628 "dropping connection");
631 ic->ic_receive_state = ICL_CONN_STATE_HEADER_DIGEST;
632 if (ic->ic_header_crc32c == false)
633 ic->ic_receive_len = 0;
635 ic->ic_receive_len = ISCSI_HEADER_DIGEST_SIZE;
638 case ICL_CONN_STATE_HEADER_DIGEST:
639 //ICL_DEBUG("receiving header digest");
640 error = icl_pdu_check_header_digest(request, availablep);
642 ICL_DEBUG("header digest failed; "
643 "dropping connection");
647 ic->ic_receive_state = ICL_CONN_STATE_DATA;
649 icl_pdu_data_segment_receive_len(request);
652 case ICL_CONN_STATE_DATA:
653 //ICL_DEBUG("receiving data segment");
654 error = icl_pdu_receive_data_segment(request, availablep,
657 ICL_DEBUG("failed to receive data segment;"
658 "dropping connection");
665 ic->ic_receive_state = ICL_CONN_STATE_DATA_DIGEST;
666 if (request->ip_data_len == 0 || ic->ic_data_crc32c == false)
667 ic->ic_receive_len = 0;
669 ic->ic_receive_len = ISCSI_DATA_DIGEST_SIZE;
672 case ICL_CONN_STATE_DATA_DIGEST:
673 //ICL_DEBUG("receiving data digest");
674 error = icl_pdu_check_data_digest(request, availablep);
676 ICL_DEBUG("data digest failed; "
677 "dropping connection");
682 * We've received complete PDU; reset the receive state machine
683 * and return the PDU.
685 ic->ic_receive_state = ICL_CONN_STATE_BHS;
686 ic->ic_receive_len = sizeof(struct iscsi_bhs);
687 ic->ic_receive_pdu = NULL;
691 panic("invalid ic_receive_state %d\n", ic->ic_receive_state);
696 * Don't free the PDU; it's pointed to by ic->ic_receive_pdu
697 * and will get freed in icl_soft_conn_close().
706 icl_conn_receive_pdus(struct icl_conn *ic, size_t available)
708 struct icl_pdu *response;
714 * This can never happen; we're careful to only mess with ic->ic_socket
715 * pointer when the send/receive threads are not running.
717 KASSERT(so != NULL, ("NULL socket"));
720 if (ic->ic_disconnecting)
723 if (so->so_error != 0) {
724 ICL_DEBUG("connection error %d; "
725 "dropping connection", so->so_error);
731 * Loop until we have a complete PDU or there is not enough
732 * data in the socket buffer.
734 if (available < ic->ic_receive_len) {
736 ICL_DEBUG("not enough data; have %zd, "
737 "need %zd", available,
743 response = icl_conn_receive_pdu(ic, &available);
744 if (response == NULL)
747 if (response->ip_ahs_len > 0) {
748 ICL_WARN("received PDU with unsupported "
749 "AHS; opcode 0x%x; dropping connection",
750 response->ip_bhs->bhs_opcode);
751 icl_pdu_free(response);
756 (ic->ic_receive)(response);
761 icl_receive_thread(void *arg)
771 ic->ic_receive_running = true;
775 if (ic->ic_disconnecting) {
776 //ICL_DEBUG("terminating");
781 * Set the low watermark, to be checked by
782 * soreadable() in icl_soupcall_receive()
783 * to avoid unneccessary wakeups until there
784 * is enough data received to read the PDU.
786 SOCKBUF_LOCK(&so->so_rcv);
787 available = sbavail(&so->so_rcv);
788 if (available < ic->ic_receive_len) {
789 so->so_rcv.sb_lowat = ic->ic_receive_len;
790 cv_wait(&ic->ic_receive_cv, &so->so_rcv.sb_mtx);
792 so->so_rcv.sb_lowat = so->so_rcv.sb_hiwat + 1;
793 SOCKBUF_UNLOCK(&so->so_rcv);
795 icl_conn_receive_pdus(ic, available);
799 ic->ic_receive_running = false;
800 cv_signal(&ic->ic_send_cv);
806 icl_soupcall_receive(struct socket *so, void *arg, int waitflag)
814 cv_signal(&ic->ic_receive_cv);
819 icl_pdu_finalize(struct icl_pdu *request)
821 size_t padding, pdu_len;
822 uint32_t digest, zero = 0;
826 ic = request->ip_conn;
828 icl_pdu_set_data_segment_length(request, request->ip_data_len);
830 pdu_len = icl_pdu_size(request);
832 if (ic->ic_header_crc32c) {
833 digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
834 ok = m_append(request->ip_bhs_mbuf, sizeof(digest),
837 ICL_WARN("failed to append header digest");
842 if (request->ip_data_len != 0) {
843 padding = icl_pdu_padding(request);
845 ok = m_append(request->ip_data_mbuf, padding,
848 ICL_WARN("failed to append padding");
853 if (ic->ic_data_crc32c) {
854 digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
856 ok = m_append(request->ip_data_mbuf, sizeof(digest),
859 ICL_WARN("failed to append data digest");
864 m_cat(request->ip_bhs_mbuf, request->ip_data_mbuf);
865 request->ip_data_mbuf = NULL;
868 request->ip_bhs_mbuf->m_pkthdr.len = pdu_len;
874 icl_conn_send_pdus(struct icl_conn *ic, struct icl_pdu_stailq *queue)
876 struct icl_pdu *request, *request2;
878 size_t available, size, size2;
879 int coalesced, error;
881 ICL_CONN_LOCK_ASSERT_NOT(ic);
885 SOCKBUF_LOCK(&so->so_snd);
887 * Check how much space do we have for transmit. We can't just
888 * call sosend() and retry when we get EWOULDBLOCK or EMSGSIZE,
889 * as it always frees the mbuf chain passed to it, even in case
892 available = sbspace(&so->so_snd);
895 * Notify the socket upcall that we don't need wakeups
896 * for the time being.
898 so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1;
899 SOCKBUF_UNLOCK(&so->so_snd);
901 while (!STAILQ_EMPTY(queue)) {
902 request = STAILQ_FIRST(queue);
903 size = icl_pdu_size(request);
904 if (available < size) {
907 * Set the low watermark, to be checked by
908 * sowriteable() in icl_soupcall_send()
909 * to avoid unneccessary wakeups until there
910 * is enough space for the PDU to fit.
912 SOCKBUF_LOCK(&so->so_snd);
913 available = sbspace(&so->so_snd);
914 if (available < size) {
916 ICL_DEBUG("no space to send; "
917 "have %zd, need %zd",
920 so->so_snd.sb_lowat = size;
921 SOCKBUF_UNLOCK(&so->so_snd);
924 SOCKBUF_UNLOCK(&so->so_snd);
926 STAILQ_REMOVE_HEAD(queue, ip_next);
927 error = icl_pdu_finalize(request);
929 ICL_DEBUG("failed to finalize PDU; "
930 "dropping connection");
932 icl_pdu_free(request);
938 request2 = STAILQ_FIRST(queue);
939 if (request2 == NULL)
941 size2 = icl_pdu_size(request2);
942 if (available < size + size2)
944 STAILQ_REMOVE_HEAD(queue, ip_next);
945 error = icl_pdu_finalize(request2);
947 ICL_DEBUG("failed to finalize PDU; "
948 "dropping connection");
950 icl_pdu_free(request);
951 icl_pdu_free(request2);
954 m_cat(request->ip_bhs_mbuf, request2->ip_bhs_mbuf);
955 request2->ip_bhs_mbuf = NULL;
956 request->ip_bhs_mbuf->m_pkthdr.len += size2;
958 STAILQ_REMOVE_AFTER(queue, request, ip_next);
959 icl_pdu_free(request2);
964 ICL_DEBUG("coalesced %d PDUs into %zd bytes",
970 error = sosend(so, NULL, NULL, request->ip_bhs_mbuf,
971 NULL, MSG_DONTWAIT, curthread);
972 request->ip_bhs_mbuf = NULL; /* Sosend consumes the mbuf. */
974 ICL_DEBUG("failed to send PDU, error %d; "
975 "dropping connection", error);
977 icl_pdu_free(request);
980 icl_pdu_free(request);
985 icl_send_thread(void *arg)
988 struct icl_pdu_stailq queue;
995 ic->ic_send_running = true;
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);
1274 error = kthread_add(icl_send_thread, ic, NULL, NULL, 0, 0, "%stx",
1277 ICL_WARN("kthread_add(9) failed with error %d", error);
1278 icl_soft_conn_close(ic);
1282 error = kthread_add(icl_receive_thread, ic, NULL, NULL, 0, 0, "%srx",
1285 ICL_WARN("kthread_add(9) failed with error %d", error);
1286 icl_soft_conn_close(ic);
1291 * Register socket upcall, to get notified about incoming PDUs
1292 * and free space to send outgoing ones.
1294 SOCKBUF_LOCK(&ic->ic_socket->so_snd);
1295 soupcall_set(ic->ic_socket, SO_SND, icl_soupcall_send, ic);
1296 SOCKBUF_UNLOCK(&ic->ic_socket->so_snd);
1297 SOCKBUF_LOCK(&ic->ic_socket->so_rcv);
1298 soupcall_set(ic->ic_socket, SO_RCV, icl_soupcall_receive, ic);
1299 SOCKBUF_UNLOCK(&ic->ic_socket->so_rcv);
1305 icl_soft_conn_handoff(struct icl_conn *ic, int fd)
1309 cap_rights_t rights;
1312 ICL_CONN_LOCK_ASSERT_NOT(ic);
1315 * Steal the socket from userland.
1317 error = fget(curthread, fd,
1318 cap_rights_init(&rights, CAP_SOCK_CLIENT), &fp);
1321 if (fp->f_type != DTYPE_SOCKET) {
1322 fdrop(fp, curthread);
1326 if (so->so_type != SOCK_STREAM) {
1327 fdrop(fp, curthread);
1333 if (ic->ic_socket != NULL) {
1334 ICL_CONN_UNLOCK(ic);
1335 fdrop(fp, curthread);
1339 ic->ic_socket = fp->f_data;
1340 fp->f_ops = &badfileops;
1342 fdrop(fp, curthread);
1343 ICL_CONN_UNLOCK(ic);
1345 error = icl_conn_start(ic);
1351 icl_soft_conn_close(struct icl_conn *ic)
1353 struct icl_pdu *pdu;
1355 ICL_CONN_LOCK_ASSERT_NOT(ic);
1358 if (ic->ic_socket == NULL) {
1359 ICL_CONN_UNLOCK(ic);
1364 * Deregister socket upcalls.
1366 ICL_CONN_UNLOCK(ic);
1367 SOCKBUF_LOCK(&ic->ic_socket->so_snd);
1368 if (ic->ic_socket->so_snd.sb_upcall != NULL)
1369 soupcall_clear(ic->ic_socket, SO_SND);
1370 SOCKBUF_UNLOCK(&ic->ic_socket->so_snd);
1371 SOCKBUF_LOCK(&ic->ic_socket->so_rcv);
1372 if (ic->ic_socket->so_rcv.sb_upcall != NULL)
1373 soupcall_clear(ic->ic_socket, SO_RCV);
1374 SOCKBUF_UNLOCK(&ic->ic_socket->so_rcv);
1377 ic->ic_disconnecting = true;
1380 * Wake up the threads, so they can properly terminate.
1382 while (ic->ic_receive_running || ic->ic_send_running) {
1383 //ICL_DEBUG("waiting for send/receive threads to terminate");
1384 cv_signal(&ic->ic_receive_cv);
1385 cv_signal(&ic->ic_send_cv);
1386 cv_wait(&ic->ic_send_cv, ic->ic_lock);
1388 //ICL_DEBUG("send/receive threads terminated");
1390 ICL_CONN_UNLOCK(ic);
1391 soclose(ic->ic_socket);
1393 ic->ic_socket = NULL;
1395 if (ic->ic_receive_pdu != NULL) {
1396 //ICL_DEBUG("freeing partially received PDU");
1397 icl_pdu_free(ic->ic_receive_pdu);
1398 ic->ic_receive_pdu = NULL;
1402 * Remove any outstanding PDUs from the send queue.
1404 while (!STAILQ_EMPTY(&ic->ic_to_send)) {
1405 pdu = STAILQ_FIRST(&ic->ic_to_send);
1406 STAILQ_REMOVE_HEAD(&ic->ic_to_send, ip_next);
1410 KASSERT(STAILQ_EMPTY(&ic->ic_to_send),
1411 ("destroying session with non-empty send queue"));
1413 KASSERT(ic->ic_outstanding_pdus == 0,
1414 ("destroying session with %d outstanding PDUs",
1415 ic->ic_outstanding_pdus));
1417 ICL_CONN_UNLOCK(ic);
1421 icl_soft_conn_connected(struct icl_conn *ic)
1423 ICL_CONN_LOCK_ASSERT_NOT(ic);
1426 if (ic->ic_socket == NULL) {
1427 ICL_CONN_UNLOCK(ic);
1430 if (ic->ic_socket->so_error != 0) {
1431 ICL_CONN_UNLOCK(ic);
1434 ICL_CONN_UNLOCK(ic);
1439 icl_soft_limits(size_t *limitp)
1442 *limitp = 128 * 1024;
1447 #ifdef ICL_KERNEL_PROXY
1449 icl_conn_handoff_sock(struct icl_conn *ic, struct socket *so)
1453 ICL_CONN_LOCK_ASSERT_NOT(ic);
1455 if (so->so_type != SOCK_STREAM)
1459 if (ic->ic_socket != NULL) {
1460 ICL_CONN_UNLOCK(ic);
1464 ICL_CONN_UNLOCK(ic);
1466 error = icl_conn_start(ic);
1470 #endif /* ICL_KERNEL_PROXY */
1477 icl_pdu_zone = uma_zcreate("icl_pdu",
1478 sizeof(struct icl_pdu), NULL, NULL, NULL, NULL,
1480 refcount_init(&icl_ncons, 0);
1483 * The reason we call this "none" is that to the user,
1484 * it's known as "offload driver"; "offload driver: soft"
1485 * doesn't make much sense.
1487 error = icl_register("none", 0, icl_soft_limits, icl_soft_new_conn);
1488 KASSERT(error == 0, ("failed to register"));
1494 icl_soft_unload(void)
1500 icl_unregister("none");
1502 uma_zdestroy(icl_pdu_zone);
1508 icl_soft_modevent(module_t mod, int what, void *arg)
1513 return (icl_soft_load());
1515 return (icl_soft_unload());
1521 moduledata_t icl_soft_data = {
1527 DECLARE_MODULE(icl_soft, icl_soft_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1528 MODULE_DEPEND(icl_soft, icl, 1, 1, 1);
1529 MODULE_VERSION(icl_soft, 1);