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
33 * iSCSI Common Layer. It's used by both the initiator and target to send
34 * and receive iSCSI PDUs.
37 #include <sys/param.h>
38 #include <sys/capability.h>
39 #include <sys/condvar.h>
42 #include <sys/kernel.h>
43 #include <sys/kthread.h>
46 #include <sys/mutex.h>
47 #include <sys/module.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/systm.h>
56 #include <netinet/in.h>
57 #include <netinet/tcp.h>
60 #include "iscsi_proto.h"
62 SYSCTL_NODE(_kern, OID_AUTO, icl, CTLFLAG_RD, 0, "iSCSI Common Layer");
64 TUNABLE_INT("kern.icl.debug", &debug);
65 SYSCTL_INT(_kern_icl, OID_AUTO, debug, CTLFLAG_RWTUN,
66 &debug, 0, "Enable debug messages");
67 static int coalesce = 1;
68 TUNABLE_INT("kern.icl.coalesce", &coalesce);
69 SYSCTL_INT(_kern_icl, OID_AUTO, coalesce, CTLFLAG_RWTUN,
70 &coalesce, 0, "Try to coalesce PDUs before sending");
71 static int partial_receive_len = 128 * 1024;
72 TUNABLE_INT("kern.icl.partial_receive_len", &partial_receive_len);
73 SYSCTL_INT(_kern_icl, OID_AUTO, partial_receive_len, CTLFLAG_RWTUN,
74 &partial_receive_len, 0, "Minimum read size for partially received "
76 static int sendspace = 1048576;
77 TUNABLE_INT("kern.icl.sendspace", &sendspace);
78 SYSCTL_INT(_kern_icl, OID_AUTO, sendspace, CTLFLAG_RWTUN,
79 &sendspace, 0, "Default send socket buffer size");
80 static int recvspace = 1048576;
81 TUNABLE_INT("kern.icl.recvspace", &recvspace);
82 SYSCTL_INT(_kern_icl, OID_AUTO, recvspace, CTLFLAG_RWTUN,
83 &recvspace, 0, "Default receive socket buffer size");
85 static uma_zone_t icl_conn_zone;
86 static uma_zone_t icl_pdu_zone;
88 static volatile u_int icl_ncons;
90 #define ICL_DEBUG(X, ...) \
93 printf("%s: " X "\n", __func__, ## __VA_ARGS__);\
96 #define ICL_WARN(X, ...) \
99 printf("WARNING: %s: " X "\n", \
100 __func__, ## __VA_ARGS__); \
104 #define ICL_CONN_LOCK(X) mtx_lock(X->ic_lock)
105 #define ICL_CONN_UNLOCK(X) mtx_unlock(X->ic_lock)
106 #define ICL_CONN_LOCK_ASSERT(X) mtx_assert(X->ic_lock, MA_OWNED)
107 #define ICL_CONN_LOCK_ASSERT_NOT(X) mtx_assert(X->ic_lock, MA_NOTOWNED)
109 STAILQ_HEAD(icl_pdu_stailq, icl_pdu);
112 icl_conn_fail(struct icl_conn *ic)
114 if (ic->ic_socket == NULL)
120 ic->ic_socket->so_error = EDOOFUS;
125 icl_conn_receive(struct icl_conn *ic, size_t len)
134 memset(&uio, 0, sizeof(uio));
137 flags = MSG_DONTWAIT;
138 error = soreceive(so, NULL, &uio, &m, NULL, &flags);
140 ICL_DEBUG("soreceive error %d", error);
143 if (uio.uio_resid != 0) {
145 ICL_DEBUG("short read");
152 static struct icl_pdu *
153 icl_pdu_new(struct icl_conn *ic, int flags)
158 refcount_acquire(&ic->ic_outstanding_pdus);
160 ip = uma_zalloc(icl_pdu_zone, flags | M_ZERO);
162 ICL_WARN("failed to allocate %zd bytes", sizeof(*ip));
164 refcount_release(&ic->ic_outstanding_pdus);
175 icl_pdu_free(struct icl_pdu *ip)
181 m_freem(ip->ip_bhs_mbuf);
182 m_freem(ip->ip_ahs_mbuf);
183 m_freem(ip->ip_data_mbuf);
184 uma_zfree(icl_pdu_zone, ip);
186 refcount_release(&ic->ic_outstanding_pdus);
191 * Allocate icl_pdu with empty BHS to fill up by the caller.
194 icl_pdu_new_bhs(struct icl_conn *ic, int flags)
198 ip = icl_pdu_new(ic, flags);
202 ip->ip_bhs_mbuf = m_getm2(NULL, sizeof(struct iscsi_bhs),
203 flags, MT_DATA, M_PKTHDR);
204 if (ip->ip_bhs_mbuf == NULL) {
205 ICL_WARN("failed to allocate %zd bytes", sizeof(*ip));
209 ip->ip_bhs = mtod(ip->ip_bhs_mbuf, struct iscsi_bhs *);
210 memset(ip->ip_bhs, 0, sizeof(struct iscsi_bhs));
211 ip->ip_bhs_mbuf->m_len = sizeof(struct iscsi_bhs);
217 icl_pdu_ahs_length(const struct icl_pdu *request)
220 return (request->ip_bhs->bhs_total_ahs_len * 4);
224 icl_pdu_data_segment_length(const struct icl_pdu *request)
228 len += request->ip_bhs->bhs_data_segment_len[0];
230 len += request->ip_bhs->bhs_data_segment_len[1];
232 len += request->ip_bhs->bhs_data_segment_len[2];
238 icl_pdu_set_data_segment_length(struct icl_pdu *response, uint32_t len)
241 response->ip_bhs->bhs_data_segment_len[2] = len;
242 response->ip_bhs->bhs_data_segment_len[1] = len >> 8;
243 response->ip_bhs->bhs_data_segment_len[0] = len >> 16;
247 icl_pdu_padding(const struct icl_pdu *ip)
250 if ((ip->ip_data_len % 4) != 0)
251 return (4 - (ip->ip_data_len % 4));
257 icl_pdu_size(const struct icl_pdu *response)
261 KASSERT(response->ip_ahs_len == 0, ("responding with AHS"));
263 len = sizeof(struct iscsi_bhs) + response->ip_data_len +
264 icl_pdu_padding(response);
265 if (response->ip_conn->ic_header_crc32c)
266 len += ISCSI_HEADER_DIGEST_SIZE;
267 if (response->ip_data_len != 0 && response->ip_conn->ic_data_crc32c)
268 len += ISCSI_DATA_DIGEST_SIZE;
274 icl_pdu_receive_bhs(struct icl_pdu *request, size_t *availablep)
278 m = icl_conn_receive(request->ip_conn, sizeof(struct iscsi_bhs));
280 ICL_DEBUG("failed to receive BHS");
284 request->ip_bhs_mbuf = m_pullup(m, sizeof(struct iscsi_bhs));
285 if (request->ip_bhs_mbuf == NULL) {
286 ICL_WARN("m_pullup failed");
289 request->ip_bhs = mtod(request->ip_bhs_mbuf, struct iscsi_bhs *);
292 * XXX: For architectures with strict alignment requirements
293 * we may need to allocate ip_bhs and copy the data into it.
294 * For some reason, though, not doing this doesn't seem
295 * to cause problems; tested on sparc64.
298 *availablep -= sizeof(struct iscsi_bhs);
303 icl_pdu_receive_ahs(struct icl_pdu *request, size_t *availablep)
306 request->ip_ahs_len = icl_pdu_ahs_length(request);
307 if (request->ip_ahs_len == 0)
310 request->ip_ahs_mbuf = icl_conn_receive(request->ip_conn,
311 request->ip_ahs_len);
312 if (request->ip_ahs_mbuf == NULL) {
313 ICL_DEBUG("failed to receive AHS");
317 *availablep -= request->ip_ahs_len;
322 icl_mbuf_to_crc32c(const struct mbuf *m0)
324 uint32_t digest = 0xffffffff;
325 const struct mbuf *m;
327 for (m = m0; m != NULL; m = m->m_next)
328 digest = calculate_crc32c(digest,
329 mtod(m, const void *), m->m_len);
331 digest = digest ^ 0xffffffff;
337 icl_pdu_check_header_digest(struct icl_pdu *request, size_t *availablep)
340 uint32_t received_digest, valid_digest;
342 if (request->ip_conn->ic_header_crc32c == false)
345 m = icl_conn_receive(request->ip_conn, ISCSI_HEADER_DIGEST_SIZE);
347 ICL_DEBUG("failed to receive header digest");
351 CTASSERT(sizeof(received_digest) == ISCSI_HEADER_DIGEST_SIZE);
352 m_copydata(m, 0, ISCSI_HEADER_DIGEST_SIZE, (void *)&received_digest);
355 *availablep -= ISCSI_HEADER_DIGEST_SIZE;
360 valid_digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
361 if (received_digest != valid_digest) {
362 ICL_WARN("header digest check failed; got 0x%x, "
363 "should be 0x%x", received_digest, valid_digest);
371 * Return the number of bytes that should be waiting in the receive socket
372 * before icl_pdu_receive_data_segment() gets called.
375 icl_pdu_data_segment_receive_len(const struct icl_pdu *request)
379 len = icl_pdu_data_segment_length(request);
384 * Account for the parts of data segment already read from
387 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
388 len -= request->ip_data_len;
391 * Don't always wait for the full data segment to be delivered
392 * to the socket; this might badly affect performance due to
393 * TCP window scaling.
395 if (len > partial_receive_len) {
397 ICL_DEBUG("need %zd bytes of data, limiting to %zd",
398 len, partial_receive_len));
400 len = partial_receive_len;
406 * Account for padding. Note that due to the way code is written,
407 * the icl_pdu_receive_data_segment() must always receive padding
408 * along with the last part of data segment, because it would be
409 * impossible to tell whether we've already received the full data
410 * segment including padding, or without it.
413 len += 4 - (len % 4);
416 ICL_DEBUG("need %zd bytes of data", len));
423 icl_pdu_receive_data_segment(struct icl_pdu *request,
424 size_t *availablep, bool *more_neededp)
427 size_t len, padding = 0;
430 ic = request->ip_conn;
432 *more_neededp = false;
433 ic->ic_receive_len = 0;
435 len = icl_pdu_data_segment_length(request);
440 padding = 4 - (len % 4);
443 * Account for already received parts of data segment.
445 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
446 len -= request->ip_data_len;
448 if (len + padding > *availablep) {
450 * Not enough data in the socket buffer. Receive as much
451 * as we can. Don't receive padding, since, obviously, it's
452 * not the end of data segment yet.
455 ICL_DEBUG("limited from %zd to %zd",
456 len + padding, *availablep - padding));
458 len = *availablep - padding;
459 *more_neededp = true;
464 * Must not try to receive padding without at least one byte
465 * of actual data segment.
468 m = icl_conn_receive(request->ip_conn, len + padding);
470 ICL_DEBUG("failed to receive data segment");
474 if (request->ip_data_mbuf == NULL)
475 request->ip_data_mbuf = m;
477 m_cat(request->ip_data_mbuf, m);
479 request->ip_data_len += len;
480 *availablep -= len + padding;
486 icl_pdu_data_segment_receive_len(request);
492 icl_pdu_check_data_digest(struct icl_pdu *request, size_t *availablep)
495 uint32_t received_digest, valid_digest;
497 if (request->ip_conn->ic_data_crc32c == false)
500 if (request->ip_data_len == 0)
503 m = icl_conn_receive(request->ip_conn, ISCSI_DATA_DIGEST_SIZE);
505 ICL_DEBUG("failed to receive data digest");
509 CTASSERT(sizeof(received_digest) == ISCSI_DATA_DIGEST_SIZE);
510 m_copydata(m, 0, ISCSI_DATA_DIGEST_SIZE, (void *)&received_digest);
513 *availablep -= ISCSI_DATA_DIGEST_SIZE;
516 * Note that ip_data_mbuf also contains padding; since digest
517 * calculation is supposed to include that, we iterate over
518 * the entire ip_data_mbuf chain, not just ip_data_len bytes of it.
520 valid_digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
521 if (received_digest != valid_digest) {
522 ICL_WARN("data digest check failed; got 0x%x, "
523 "should be 0x%x", received_digest, valid_digest);
531 * Somewhat contrary to the name, this attempts to receive only one
532 * "part" of PDU at a time; call it repeatedly until it returns non-NULL.
534 static struct icl_pdu *
535 icl_conn_receive_pdu(struct icl_conn *ic, size_t *availablep)
537 struct icl_pdu *request;
545 if (ic->ic_receive_state == ICL_CONN_STATE_BHS) {
546 KASSERT(ic->ic_receive_pdu == NULL,
547 ("ic->ic_receive_pdu != NULL"));
548 request = icl_pdu_new(ic, M_NOWAIT);
549 if (request == NULL) {
550 ICL_DEBUG("failed to allocate PDU; "
551 "dropping connection");
555 ic->ic_receive_pdu = request;
557 KASSERT(ic->ic_receive_pdu != NULL,
558 ("ic->ic_receive_pdu == NULL"));
559 request = ic->ic_receive_pdu;
562 if (*availablep < ic->ic_receive_len) {
564 ICL_DEBUG("not enough data; need %zd, "
565 "have %zd", ic->ic_receive_len, *availablep);
570 switch (ic->ic_receive_state) {
571 case ICL_CONN_STATE_BHS:
572 //ICL_DEBUG("receiving BHS");
573 error = icl_pdu_receive_bhs(request, availablep);
575 ICL_DEBUG("failed to receive BHS; "
576 "dropping connection");
581 * We don't enforce any limit for AHS length;
582 * its length is stored in 8 bit field.
585 len = icl_pdu_data_segment_length(request);
586 if (len > ic->ic_max_data_segment_length) {
587 ICL_WARN("received data segment "
588 "length %zd is larger than negotiated "
589 "MaxDataSegmentLength %zd; "
590 "dropping connection",
591 len, ic->ic_max_data_segment_length);
596 ic->ic_receive_state = ICL_CONN_STATE_AHS;
597 ic->ic_receive_len = icl_pdu_ahs_length(request);
600 case ICL_CONN_STATE_AHS:
601 //ICL_DEBUG("receiving AHS");
602 error = icl_pdu_receive_ahs(request, availablep);
604 ICL_DEBUG("failed to receive AHS; "
605 "dropping connection");
608 ic->ic_receive_state = ICL_CONN_STATE_HEADER_DIGEST;
609 if (ic->ic_header_crc32c == false)
610 ic->ic_receive_len = 0;
612 ic->ic_receive_len = ISCSI_HEADER_DIGEST_SIZE;
615 case ICL_CONN_STATE_HEADER_DIGEST:
616 //ICL_DEBUG("receiving header digest");
617 error = icl_pdu_check_header_digest(request, availablep);
619 ICL_DEBUG("header digest failed; "
620 "dropping connection");
624 ic->ic_receive_state = ICL_CONN_STATE_DATA;
626 icl_pdu_data_segment_receive_len(request);
629 case ICL_CONN_STATE_DATA:
630 //ICL_DEBUG("receiving data segment");
631 error = icl_pdu_receive_data_segment(request, availablep,
634 ICL_DEBUG("failed to receive data segment;"
635 "dropping connection");
642 ic->ic_receive_state = ICL_CONN_STATE_DATA_DIGEST;
643 if (request->ip_data_len == 0 || ic->ic_data_crc32c == false)
644 ic->ic_receive_len = 0;
646 ic->ic_receive_len = ISCSI_DATA_DIGEST_SIZE;
649 case ICL_CONN_STATE_DATA_DIGEST:
650 //ICL_DEBUG("receiving data digest");
651 error = icl_pdu_check_data_digest(request, availablep);
653 ICL_DEBUG("data digest failed; "
654 "dropping connection");
659 * We've received complete PDU; reset the receive state machine
660 * and return the PDU.
662 ic->ic_receive_state = ICL_CONN_STATE_BHS;
663 ic->ic_receive_len = sizeof(struct iscsi_bhs);
664 ic->ic_receive_pdu = NULL;
668 panic("invalid ic_receive_state %d\n", ic->ic_receive_state);
673 * Don't free the PDU; it's pointed to by ic->ic_receive_pdu
674 * and will get freed in icl_conn_close().
683 icl_conn_receive_pdus(struct icl_conn *ic, size_t available)
685 struct icl_pdu *response;
691 * This can never happen; we're careful to only mess with ic->ic_socket
692 * pointer when the send/receive threads are not running.
694 KASSERT(so != NULL, ("NULL socket"));
697 if (ic->ic_disconnecting)
700 if (so->so_error != 0) {
701 ICL_DEBUG("connection error %d; "
702 "dropping connection", so->so_error);
708 * Loop until we have a complete PDU or there is not enough
709 * data in the socket buffer.
711 if (available < ic->ic_receive_len) {
713 ICL_DEBUG("not enough data; have %zd, "
714 "need %zd", available,
720 response = icl_conn_receive_pdu(ic, &available);
721 if (response == NULL)
724 if (response->ip_ahs_len > 0) {
725 ICL_WARN("received PDU with unsupported "
726 "AHS; opcode 0x%x; dropping connection",
727 response->ip_bhs->bhs_opcode);
728 icl_pdu_free(response);
733 (ic->ic_receive)(response);
738 icl_receive_thread(void *arg)
748 ic->ic_receive_running = true;
752 if (ic->ic_disconnecting) {
753 //ICL_DEBUG("terminating");
758 * Set the low watermark, to be checked by
759 * soreadable() in icl_soupcall_receive()
760 * to avoid unneccessary wakeups until there
761 * is enough data received to read the PDU.
763 SOCKBUF_LOCK(&so->so_rcv);
764 available = so->so_rcv.sb_cc;
765 if (available < ic->ic_receive_len) {
766 so->so_rcv.sb_lowat = ic->ic_receive_len;
767 cv_wait(&ic->ic_receive_cv, &so->so_rcv.sb_mtx);
769 so->so_rcv.sb_lowat = so->so_rcv.sb_hiwat + 1;
770 SOCKBUF_UNLOCK(&so->so_rcv);
772 icl_conn_receive_pdus(ic, available);
776 ic->ic_receive_running = false;
782 icl_soupcall_receive(struct socket *so, void *arg, int waitflag)
790 cv_signal(&ic->ic_receive_cv);
795 icl_pdu_finalize(struct icl_pdu *request)
797 size_t padding, pdu_len;
798 uint32_t digest, zero = 0;
802 ic = request->ip_conn;
804 icl_pdu_set_data_segment_length(request, request->ip_data_len);
806 pdu_len = icl_pdu_size(request);
808 if (ic->ic_header_crc32c) {
809 digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
810 ok = m_append(request->ip_bhs_mbuf, sizeof(digest),
813 ICL_WARN("failed to append header digest");
818 if (request->ip_data_len != 0) {
819 padding = icl_pdu_padding(request);
821 ok = m_append(request->ip_data_mbuf, padding,
824 ICL_WARN("failed to append padding");
829 if (ic->ic_data_crc32c) {
830 digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
832 ok = m_append(request->ip_data_mbuf, sizeof(digest),
835 ICL_WARN("failed to append data digest");
840 m_cat(request->ip_bhs_mbuf, request->ip_data_mbuf);
841 request->ip_data_mbuf = NULL;
844 request->ip_bhs_mbuf->m_pkthdr.len = pdu_len;
850 icl_conn_send_pdus(struct icl_conn *ic, struct icl_pdu_stailq *queue)
852 struct icl_pdu *request, *request2;
854 size_t available, size, size2;
855 int coalesced, error;
857 ICL_CONN_LOCK_ASSERT_NOT(ic);
861 SOCKBUF_LOCK(&so->so_snd);
863 * Check how much space do we have for transmit. We can't just
864 * call sosend() and retry when we get EWOULDBLOCK or EMSGSIZE,
865 * as it always frees the mbuf chain passed to it, even in case
868 available = sbspace(&so->so_snd);
871 * Notify the socket upcall that we don't need wakeups
872 * for the time being.
874 so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1;
875 SOCKBUF_UNLOCK(&so->so_snd);
877 while (!STAILQ_EMPTY(queue)) {
878 if (ic->ic_disconnecting)
880 request = STAILQ_FIRST(queue);
881 size = icl_pdu_size(request);
882 if (available < size) {
885 * Set the low watermark, to be checked by
886 * sowriteable() in icl_soupcall_send()
887 * to avoid unneccessary wakeups until there
888 * is enough space for the PDU to fit.
890 SOCKBUF_LOCK(&so->so_snd);
891 available = sbspace(&so->so_snd);
892 if (available < size) {
894 ICL_DEBUG("no space to send; "
895 "have %zd, need %zd",
898 so->so_snd.sb_lowat = size;
899 SOCKBUF_UNLOCK(&so->so_snd);
902 SOCKBUF_UNLOCK(&so->so_snd);
904 STAILQ_REMOVE_HEAD(queue, ip_next);
905 error = icl_pdu_finalize(request);
907 ICL_DEBUG("failed to finalize PDU; "
908 "dropping connection");
910 icl_pdu_free(request);
916 request2 = STAILQ_FIRST(queue);
917 if (request2 == NULL)
919 size2 = icl_pdu_size(request2);
920 if (available < size + size2)
922 STAILQ_REMOVE_HEAD(queue, ip_next);
923 error = icl_pdu_finalize(request2);
925 ICL_DEBUG("failed to finalize PDU; "
926 "dropping connection");
928 icl_pdu_free(request);
929 icl_pdu_free(request2);
932 m_cat(request->ip_bhs_mbuf, request2->ip_bhs_mbuf);
933 request2->ip_bhs_mbuf = NULL;
934 request->ip_bhs_mbuf->m_pkthdr.len += size2;
936 STAILQ_REMOVE_AFTER(queue, request, ip_next);
937 icl_pdu_free(request2);
942 ICL_DEBUG("coalesced %d PDUs into %zd bytes",
948 error = sosend(so, NULL, NULL, request->ip_bhs_mbuf,
949 NULL, MSG_DONTWAIT, curthread);
950 request->ip_bhs_mbuf = NULL; /* Sosend consumes the mbuf. */
952 ICL_DEBUG("failed to send PDU, error %d; "
953 "dropping connection", error);
955 icl_pdu_free(request);
958 icl_pdu_free(request);
963 icl_send_thread(void *arg)
966 struct icl_pdu_stailq queue;
973 ic->ic_send_running = true;
976 if (ic->ic_disconnecting) {
977 //ICL_DEBUG("terminating");
983 * If the local queue is empty, populate it from
984 * the main one. This way the icl_conn_send_pdus()
985 * can go through all the queued PDUs without holding
988 if (STAILQ_EMPTY(&queue))
989 STAILQ_SWAP(&ic->ic_to_send, &queue, icl_pdu);
991 ic->ic_check_send_space = false;
993 icl_conn_send_pdus(ic, &queue);
997 * The icl_soupcall_send() was called since the last
998 * call to sbspace(); go around;
1000 if (ic->ic_check_send_space)
1004 * Local queue is empty, but we still have PDUs
1005 * in the main one; go around.
1007 if (STAILQ_EMPTY(&queue) &&
1008 !STAILQ_EMPTY(&ic->ic_to_send))
1012 * There might be some stuff in the local queue,
1013 * which didn't get sent due to not having enough send
1014 * space. Wait for socket upcall.
1019 cv_wait(&ic->ic_send_cv, ic->ic_lock);
1023 * We're exiting; move PDUs back to the main queue, so they can
1024 * get freed properly. At this point ordering doesn't matter.
1026 STAILQ_CONCAT(&ic->ic_to_send, &queue);
1028 ic->ic_send_running = false;
1029 ICL_CONN_UNLOCK(ic);
1034 icl_soupcall_send(struct socket *so, void *arg, int waitflag)
1036 struct icl_conn *ic;
1038 if (!sowriteable(so))
1044 ic->ic_check_send_space = true;
1045 ICL_CONN_UNLOCK(ic);
1047 cv_signal(&ic->ic_send_cv);
1053 icl_pdu_append_data(struct icl_pdu *request, const void *addr, size_t len,
1056 struct mbuf *mb, *newmb;
1057 size_t copylen, off = 0;
1059 KASSERT(len > 0, ("len == 0"));
1061 newmb = m_getm2(NULL, len, flags, MT_DATA, M_PKTHDR);
1062 if (newmb == NULL) {
1063 ICL_WARN("failed to allocate mbuf for %zd bytes", len);
1067 for (mb = newmb; mb != NULL; mb = mb->m_next) {
1068 copylen = min(M_TRAILINGSPACE(mb), len - off);
1069 memcpy(mtod(mb, char *), (const char *)addr + off, copylen);
1070 mb->m_len = copylen;
1073 KASSERT(off == len, ("%s: off != len", __func__));
1075 if (request->ip_data_mbuf == NULL) {
1076 request->ip_data_mbuf = newmb;
1077 request->ip_data_len = len;
1079 m_cat(request->ip_data_mbuf, newmb);
1080 request->ip_data_len += len;
1087 icl_pdu_get_data(struct icl_pdu *ip, size_t off, void *addr, size_t len)
1090 m_copydata(ip->ip_data_mbuf, off, len, addr);
1094 icl_pdu_queue(struct icl_pdu *ip)
1096 struct icl_conn *ic;
1100 ICL_CONN_LOCK_ASSERT(ic);
1102 if (ic->ic_disconnecting || ic->ic_socket == NULL) {
1103 ICL_DEBUG("icl_pdu_queue on closed connection");
1108 if (!STAILQ_EMPTY(&ic->ic_to_send)) {
1109 STAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next);
1111 * If the queue is not empty, someone else had already
1112 * signaled the send thread; no need to do that again,
1118 STAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next);
1119 cv_signal(&ic->ic_send_cv);
1123 icl_conn_new(const char *name, struct mtx *lock)
1125 struct icl_conn *ic;
1127 refcount_acquire(&icl_ncons);
1129 ic = uma_zalloc(icl_conn_zone, M_WAITOK | M_ZERO);
1131 STAILQ_INIT(&ic->ic_to_send);
1133 cv_init(&ic->ic_send_cv, "icl_tx");
1134 cv_init(&ic->ic_receive_cv, "icl_rx");
1136 refcount_init(&ic->ic_outstanding_pdus, 0);
1138 ic->ic_max_data_segment_length = ICL_MAX_DATA_SEGMENT_LENGTH;
1145 icl_conn_free(struct icl_conn *ic)
1148 cv_destroy(&ic->ic_send_cv);
1149 cv_destroy(&ic->ic_receive_cv);
1150 uma_zfree(icl_conn_zone, ic);
1151 refcount_release(&icl_ncons);
1155 icl_conn_start(struct icl_conn *ic)
1166 if (ic->ic_socket == NULL) {
1167 ICL_CONN_UNLOCK(ic);
1171 ic->ic_receive_state = ICL_CONN_STATE_BHS;
1172 ic->ic_receive_len = sizeof(struct iscsi_bhs);
1173 ic->ic_disconnecting = false;
1175 ICL_CONN_UNLOCK(ic);
1178 * For sendspace, this is required because the current code cannot
1179 * send a PDU in pieces; thus, the minimum buffer size is equal
1180 * to the maximum PDU size. "+4" is to account for possible padding.
1182 * What we should actually do here is to use autoscaling, but set
1183 * some minimal buffer size to "minspace". I don't know a way to do
1186 minspace = sizeof(struct iscsi_bhs) + ic->ic_max_data_segment_length +
1187 ISCSI_HEADER_DIGEST_SIZE + ISCSI_DATA_DIGEST_SIZE + 4;
1188 if (sendspace < minspace) {
1189 ICL_WARN("kern.icl.sendspace too low; must be at least %zd",
1191 sendspace = minspace;
1193 if (recvspace < minspace) {
1194 ICL_WARN("kern.icl.recvspace too low; must be at least %zd",
1196 recvspace = minspace;
1199 error = soreserve(ic->ic_socket, sendspace, recvspace);
1201 ICL_WARN("soreserve failed with error %d", error);
1209 bzero(&opt, sizeof(opt));
1210 opt.sopt_dir = SOPT_SET;
1211 opt.sopt_level = IPPROTO_TCP;
1212 opt.sopt_name = TCP_NODELAY;
1213 opt.sopt_val = &one;
1214 opt.sopt_valsize = sizeof(one);
1215 error = sosetopt(ic->ic_socket, &opt);
1217 ICL_WARN("disabling TCP_NODELAY failed with error %d", error);
1225 error = kthread_add(icl_send_thread, ic, NULL, NULL, 0, 0, "%stx",
1228 ICL_WARN("kthread_add(9) failed with error %d", error);
1233 error = kthread_add(icl_receive_thread, ic, NULL, NULL, 0, 0, "%srx",
1236 ICL_WARN("kthread_add(9) failed with error %d", error);
1242 * Register socket upcall, to get notified about incoming PDUs
1243 * and free space to send outgoing ones.
1245 SOCKBUF_LOCK(&ic->ic_socket->so_snd);
1246 soupcall_set(ic->ic_socket, SO_SND, icl_soupcall_send, ic);
1247 SOCKBUF_UNLOCK(&ic->ic_socket->so_snd);
1248 SOCKBUF_LOCK(&ic->ic_socket->so_rcv);
1249 soupcall_set(ic->ic_socket, SO_RCV, icl_soupcall_receive, ic);
1250 SOCKBUF_UNLOCK(&ic->ic_socket->so_rcv);
1256 icl_conn_handoff(struct icl_conn *ic, int fd)
1260 cap_rights_t rights;
1263 ICL_CONN_LOCK_ASSERT_NOT(ic);
1266 * Steal the socket from userland.
1268 error = fget(curthread, fd,
1269 cap_rights_init(&rights, CAP_SOCK_CLIENT), &fp);
1272 if (fp->f_type != DTYPE_SOCKET) {
1273 fdrop(fp, curthread);
1277 if (so->so_type != SOCK_STREAM) {
1278 fdrop(fp, curthread);
1284 if (ic->ic_socket != NULL) {
1285 ICL_CONN_UNLOCK(ic);
1286 fdrop(fp, curthread);
1290 ic->ic_socket = fp->f_data;
1291 fp->f_ops = &badfileops;
1293 fdrop(fp, curthread);
1294 ICL_CONN_UNLOCK(ic);
1296 error = icl_conn_start(ic);
1302 icl_conn_shutdown(struct icl_conn *ic)
1304 ICL_CONN_LOCK_ASSERT_NOT(ic);
1307 if (ic->ic_socket == NULL) {
1308 ICL_CONN_UNLOCK(ic);
1311 ICL_CONN_UNLOCK(ic);
1313 soshutdown(ic->ic_socket, SHUT_RDWR);
1317 icl_conn_close(struct icl_conn *ic)
1319 struct icl_pdu *pdu;
1321 ICL_CONN_LOCK_ASSERT_NOT(ic);
1324 if (ic->ic_socket == NULL) {
1325 ICL_CONN_UNLOCK(ic);
1330 * Deregister socket upcalls.
1332 ICL_CONN_UNLOCK(ic);
1333 SOCKBUF_LOCK(&ic->ic_socket->so_snd);
1334 if (ic->ic_socket->so_snd.sb_upcall != NULL)
1335 soupcall_clear(ic->ic_socket, SO_SND);
1336 SOCKBUF_UNLOCK(&ic->ic_socket->so_snd);
1337 SOCKBUF_LOCK(&ic->ic_socket->so_rcv);
1338 if (ic->ic_socket->so_rcv.sb_upcall != NULL)
1339 soupcall_clear(ic->ic_socket, SO_RCV);
1340 SOCKBUF_UNLOCK(&ic->ic_socket->so_rcv);
1343 ic->ic_disconnecting = true;
1346 * Wake up the threads, so they can properly terminate.
1348 cv_signal(&ic->ic_receive_cv);
1349 cv_signal(&ic->ic_send_cv);
1350 while (ic->ic_receive_running || ic->ic_send_running) {
1351 //ICL_DEBUG("waiting for send/receive threads to terminate");
1352 ICL_CONN_UNLOCK(ic);
1353 cv_signal(&ic->ic_receive_cv);
1354 cv_signal(&ic->ic_send_cv);
1355 pause("icl_close", 1 * hz);
1358 //ICL_DEBUG("send/receive threads terminated");
1360 ICL_CONN_UNLOCK(ic);
1361 soclose(ic->ic_socket);
1363 ic->ic_socket = NULL;
1365 if (ic->ic_receive_pdu != NULL) {
1366 //ICL_DEBUG("freeing partially received PDU");
1367 icl_pdu_free(ic->ic_receive_pdu);
1368 ic->ic_receive_pdu = NULL;
1372 * Remove any outstanding PDUs from the send queue.
1374 while (!STAILQ_EMPTY(&ic->ic_to_send)) {
1375 pdu = STAILQ_FIRST(&ic->ic_to_send);
1376 STAILQ_REMOVE_HEAD(&ic->ic_to_send, ip_next);
1380 KASSERT(STAILQ_EMPTY(&ic->ic_to_send),
1381 ("destroying session with non-empty send queue"));
1383 KASSERT(ic->ic_outstanding_pdus == 0,
1384 ("destroying session with %d outstanding PDUs",
1385 ic->ic_outstanding_pdus));
1387 ICL_CONN_UNLOCK(ic);
1391 icl_conn_connected(struct icl_conn *ic)
1393 ICL_CONN_LOCK_ASSERT_NOT(ic);
1396 if (ic->ic_socket == NULL) {
1397 ICL_CONN_UNLOCK(ic);
1400 if (ic->ic_socket->so_error != 0) {
1401 ICL_CONN_UNLOCK(ic);
1404 ICL_CONN_UNLOCK(ic);
1408 #ifdef ICL_KERNEL_PROXY
1410 icl_conn_handoff_sock(struct icl_conn *ic, struct socket *so)
1414 ICL_CONN_LOCK_ASSERT_NOT(ic);
1416 if (so->so_type != SOCK_STREAM)
1420 if (ic->ic_socket != NULL) {
1421 ICL_CONN_UNLOCK(ic);
1425 ICL_CONN_UNLOCK(ic);
1427 error = icl_conn_start(ic);
1431 #endif /* ICL_KERNEL_PROXY */
1440 uma_zdestroy(icl_conn_zone);
1441 uma_zdestroy(icl_pdu_zone);
1450 icl_conn_zone = uma_zcreate("icl_conn",
1451 sizeof(struct icl_conn), NULL, NULL, NULL, NULL,
1453 icl_pdu_zone = uma_zcreate("icl_pdu",
1454 sizeof(struct icl_pdu), NULL, NULL, NULL, NULL,
1457 refcount_init(&icl_ncons, 0);
1461 icl_modevent(module_t mod, int what, void *arg)
1469 return (icl_unload());
1475 moduledata_t icl_data = {
1481 DECLARE_MODULE(icl, icl_data, SI_SUB_DRIVERS, SI_ORDER_FIRST);
1482 MODULE_VERSION(icl, 1);