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 * iSCSI Common Layer. It's used by both the initiator and target to send
33 * and receive iSCSI PDUs.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/capsicum.h>
41 #include <sys/condvar.h>
44 #include <sys/kernel.h>
45 #include <sys/kthread.h>
48 #include <sys/mutex.h>
49 #include <sys/module.h>
50 #include <sys/protosw.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
58 #include <netinet/in.h>
59 #include <netinet/tcp.h>
61 #include <dev/iscsi/icl.h>
62 #include <dev/iscsi/iscsi_proto.h>
64 SYSCTL_NODE(_kern, OID_AUTO, icl, CTLFLAG_RD, 0, "iSCSI Common Layer");
66 SYSCTL_INT(_kern_icl, OID_AUTO, debug, CTLFLAG_RWTUN,
67 &debug, 0, "Enable debug messages");
68 static int coalesce = 1;
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 SYSCTL_INT(_kern_icl, OID_AUTO, partial_receive_len, CTLFLAG_RWTUN,
73 &partial_receive_len, 0, "Minimum read size for partially received "
75 static int sendspace = 1048576;
76 SYSCTL_INT(_kern_icl, OID_AUTO, sendspace, CTLFLAG_RWTUN,
77 &sendspace, 0, "Default send socket buffer size");
78 static int recvspace = 1048576;
79 SYSCTL_INT(_kern_icl, OID_AUTO, recvspace, CTLFLAG_RWTUN,
80 &recvspace, 0, "Default receive socket buffer size");
82 static uma_zone_t icl_conn_zone;
83 static uma_zone_t icl_pdu_zone;
85 static volatile u_int icl_ncons;
87 #define ICL_DEBUG(X, ...) \
90 printf("%s: " X "\n", __func__, ## __VA_ARGS__);\
93 #define ICL_WARN(X, ...) \
96 printf("WARNING: %s: " X "\n", \
97 __func__, ## __VA_ARGS__); \
101 #define ICL_CONN_LOCK(X) mtx_lock(X->ic_lock)
102 #define ICL_CONN_UNLOCK(X) mtx_unlock(X->ic_lock)
103 #define ICL_CONN_LOCK_ASSERT(X) mtx_assert(X->ic_lock, MA_OWNED)
104 #define ICL_CONN_LOCK_ASSERT_NOT(X) mtx_assert(X->ic_lock, MA_NOTOWNED)
106 STAILQ_HEAD(icl_pdu_stailq, icl_pdu);
109 icl_conn_fail(struct icl_conn *ic)
111 if (ic->ic_socket == NULL)
117 ic->ic_socket->so_error = EDOOFUS;
122 icl_conn_receive(struct icl_conn *ic, size_t len)
131 memset(&uio, 0, sizeof(uio));
134 flags = MSG_DONTWAIT;
135 error = soreceive(so, NULL, &uio, &m, NULL, &flags);
137 ICL_DEBUG("soreceive error %d", error);
140 if (uio.uio_resid != 0) {
142 ICL_DEBUG("short read");
149 static struct icl_pdu *
150 icl_pdu_new(struct icl_conn *ic, int flags)
155 refcount_acquire(&ic->ic_outstanding_pdus);
157 ip = uma_zalloc(icl_pdu_zone, flags | M_ZERO);
159 ICL_WARN("failed to allocate %zd bytes", sizeof(*ip));
161 refcount_release(&ic->ic_outstanding_pdus);
172 icl_pdu_free(struct icl_pdu *ip)
178 m_freem(ip->ip_bhs_mbuf);
179 m_freem(ip->ip_ahs_mbuf);
180 m_freem(ip->ip_data_mbuf);
181 uma_zfree(icl_pdu_zone, ip);
183 refcount_release(&ic->ic_outstanding_pdus);
188 * Allocate icl_pdu with empty BHS to fill up by the caller.
191 icl_pdu_new_bhs(struct icl_conn *ic, int flags)
195 ip = icl_pdu_new(ic, flags);
199 ip->ip_bhs_mbuf = m_getm2(NULL, sizeof(struct iscsi_bhs),
200 flags, MT_DATA, M_PKTHDR);
201 if (ip->ip_bhs_mbuf == NULL) {
202 ICL_WARN("failed to allocate %zd bytes", sizeof(*ip));
206 ip->ip_bhs = mtod(ip->ip_bhs_mbuf, struct iscsi_bhs *);
207 memset(ip->ip_bhs, 0, sizeof(struct iscsi_bhs));
208 ip->ip_bhs_mbuf->m_len = sizeof(struct iscsi_bhs);
214 icl_pdu_ahs_length(const struct icl_pdu *request)
217 return (request->ip_bhs->bhs_total_ahs_len * 4);
221 icl_pdu_data_segment_length(const struct icl_pdu *request)
225 len += request->ip_bhs->bhs_data_segment_len[0];
227 len += request->ip_bhs->bhs_data_segment_len[1];
229 len += request->ip_bhs->bhs_data_segment_len[2];
235 icl_pdu_set_data_segment_length(struct icl_pdu *response, uint32_t len)
238 response->ip_bhs->bhs_data_segment_len[2] = len;
239 response->ip_bhs->bhs_data_segment_len[1] = len >> 8;
240 response->ip_bhs->bhs_data_segment_len[0] = len >> 16;
244 icl_pdu_padding(const struct icl_pdu *ip)
247 if ((ip->ip_data_len % 4) != 0)
248 return (4 - (ip->ip_data_len % 4));
254 icl_pdu_size(const struct icl_pdu *response)
258 KASSERT(response->ip_ahs_len == 0, ("responding with AHS"));
260 len = sizeof(struct iscsi_bhs) + response->ip_data_len +
261 icl_pdu_padding(response);
262 if (response->ip_conn->ic_header_crc32c)
263 len += ISCSI_HEADER_DIGEST_SIZE;
264 if (response->ip_data_len != 0 && response->ip_conn->ic_data_crc32c)
265 len += ISCSI_DATA_DIGEST_SIZE;
271 icl_pdu_receive_bhs(struct icl_pdu *request, size_t *availablep)
275 m = icl_conn_receive(request->ip_conn, sizeof(struct iscsi_bhs));
277 ICL_DEBUG("failed to receive BHS");
281 request->ip_bhs_mbuf = m_pullup(m, sizeof(struct iscsi_bhs));
282 if (request->ip_bhs_mbuf == NULL) {
283 ICL_WARN("m_pullup failed");
286 request->ip_bhs = mtod(request->ip_bhs_mbuf, struct iscsi_bhs *);
289 * XXX: For architectures with strict alignment requirements
290 * we may need to allocate ip_bhs and copy the data into it.
291 * For some reason, though, not doing this doesn't seem
292 * to cause problems; tested on sparc64.
295 *availablep -= sizeof(struct iscsi_bhs);
300 icl_pdu_receive_ahs(struct icl_pdu *request, size_t *availablep)
303 request->ip_ahs_len = icl_pdu_ahs_length(request);
304 if (request->ip_ahs_len == 0)
307 request->ip_ahs_mbuf = icl_conn_receive(request->ip_conn,
308 request->ip_ahs_len);
309 if (request->ip_ahs_mbuf == NULL) {
310 ICL_DEBUG("failed to receive AHS");
314 *availablep -= request->ip_ahs_len;
319 icl_mbuf_to_crc32c(const struct mbuf *m0)
321 uint32_t digest = 0xffffffff;
322 const struct mbuf *m;
324 for (m = m0; m != NULL; m = m->m_next)
325 digest = calculate_crc32c(digest,
326 mtod(m, const void *), m->m_len);
328 digest = digest ^ 0xffffffff;
334 icl_pdu_check_header_digest(struct icl_pdu *request, size_t *availablep)
337 uint32_t received_digest, valid_digest;
339 if (request->ip_conn->ic_header_crc32c == false)
342 m = icl_conn_receive(request->ip_conn, ISCSI_HEADER_DIGEST_SIZE);
344 ICL_DEBUG("failed to receive header digest");
348 CTASSERT(sizeof(received_digest) == ISCSI_HEADER_DIGEST_SIZE);
349 m_copydata(m, 0, ISCSI_HEADER_DIGEST_SIZE, (void *)&received_digest);
352 *availablep -= ISCSI_HEADER_DIGEST_SIZE;
357 valid_digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
358 if (received_digest != valid_digest) {
359 ICL_WARN("header digest check failed; got 0x%x, "
360 "should be 0x%x", received_digest, valid_digest);
368 * Return the number of bytes that should be waiting in the receive socket
369 * before icl_pdu_receive_data_segment() gets called.
372 icl_pdu_data_segment_receive_len(const struct icl_pdu *request)
376 len = icl_pdu_data_segment_length(request);
381 * Account for the parts of data segment already read from
384 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
385 len -= request->ip_data_len;
388 * Don't always wait for the full data segment to be delivered
389 * to the socket; this might badly affect performance due to
390 * TCP window scaling.
392 if (len > partial_receive_len) {
394 ICL_DEBUG("need %zd bytes of data, limiting to %zd",
395 len, partial_receive_len));
397 len = partial_receive_len;
403 * Account for padding. Note that due to the way code is written,
404 * the icl_pdu_receive_data_segment() must always receive padding
405 * along with the last part of data segment, because it would be
406 * impossible to tell whether we've already received the full data
407 * segment including padding, or without it.
410 len += 4 - (len % 4);
413 ICL_DEBUG("need %zd bytes of data", len));
420 icl_pdu_receive_data_segment(struct icl_pdu *request,
421 size_t *availablep, bool *more_neededp)
424 size_t len, padding = 0;
427 ic = request->ip_conn;
429 *more_neededp = false;
430 ic->ic_receive_len = 0;
432 len = icl_pdu_data_segment_length(request);
437 padding = 4 - (len % 4);
440 * Account for already received parts of data segment.
442 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
443 len -= request->ip_data_len;
445 if (len + padding > *availablep) {
447 * Not enough data in the socket buffer. Receive as much
448 * as we can. Don't receive padding, since, obviously, it's
449 * not the end of data segment yet.
452 ICL_DEBUG("limited from %zd to %zd",
453 len + padding, *availablep - padding));
455 len = *availablep - padding;
456 *more_neededp = true;
461 * Must not try to receive padding without at least one byte
462 * of actual data segment.
465 m = icl_conn_receive(request->ip_conn, len + padding);
467 ICL_DEBUG("failed to receive data segment");
471 if (request->ip_data_mbuf == NULL)
472 request->ip_data_mbuf = m;
474 m_cat(request->ip_data_mbuf, m);
476 request->ip_data_len += len;
477 *availablep -= len + padding;
483 icl_pdu_data_segment_receive_len(request);
489 icl_pdu_check_data_digest(struct icl_pdu *request, size_t *availablep)
492 uint32_t received_digest, valid_digest;
494 if (request->ip_conn->ic_data_crc32c == false)
497 if (request->ip_data_len == 0)
500 m = icl_conn_receive(request->ip_conn, ISCSI_DATA_DIGEST_SIZE);
502 ICL_DEBUG("failed to receive data digest");
506 CTASSERT(sizeof(received_digest) == ISCSI_DATA_DIGEST_SIZE);
507 m_copydata(m, 0, ISCSI_DATA_DIGEST_SIZE, (void *)&received_digest);
510 *availablep -= ISCSI_DATA_DIGEST_SIZE;
513 * Note that ip_data_mbuf also contains padding; since digest
514 * calculation is supposed to include that, we iterate over
515 * the entire ip_data_mbuf chain, not just ip_data_len bytes of it.
517 valid_digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
518 if (received_digest != valid_digest) {
519 ICL_WARN("data digest check failed; got 0x%x, "
520 "should be 0x%x", received_digest, valid_digest);
528 * Somewhat contrary to the name, this attempts to receive only one
529 * "part" of PDU at a time; call it repeatedly until it returns non-NULL.
531 static struct icl_pdu *
532 icl_conn_receive_pdu(struct icl_conn *ic, size_t *availablep)
534 struct icl_pdu *request;
542 if (ic->ic_receive_state == ICL_CONN_STATE_BHS) {
543 KASSERT(ic->ic_receive_pdu == NULL,
544 ("ic->ic_receive_pdu != NULL"));
545 request = icl_pdu_new(ic, M_NOWAIT);
546 if (request == NULL) {
547 ICL_DEBUG("failed to allocate PDU; "
548 "dropping connection");
552 ic->ic_receive_pdu = request;
554 KASSERT(ic->ic_receive_pdu != NULL,
555 ("ic->ic_receive_pdu == NULL"));
556 request = ic->ic_receive_pdu;
559 if (*availablep < ic->ic_receive_len) {
561 ICL_DEBUG("not enough data; need %zd, "
562 "have %zd", ic->ic_receive_len, *availablep);
567 switch (ic->ic_receive_state) {
568 case ICL_CONN_STATE_BHS:
569 //ICL_DEBUG("receiving BHS");
570 error = icl_pdu_receive_bhs(request, availablep);
572 ICL_DEBUG("failed to receive BHS; "
573 "dropping connection");
578 * We don't enforce any limit for AHS length;
579 * its length is stored in 8 bit field.
582 len = icl_pdu_data_segment_length(request);
583 if (len > ic->ic_max_data_segment_length) {
584 ICL_WARN("received data segment "
585 "length %zd is larger than negotiated "
586 "MaxDataSegmentLength %zd; "
587 "dropping connection",
588 len, ic->ic_max_data_segment_length);
593 ic->ic_receive_state = ICL_CONN_STATE_AHS;
594 ic->ic_receive_len = icl_pdu_ahs_length(request);
597 case ICL_CONN_STATE_AHS:
598 //ICL_DEBUG("receiving AHS");
599 error = icl_pdu_receive_ahs(request, availablep);
601 ICL_DEBUG("failed to receive AHS; "
602 "dropping connection");
605 ic->ic_receive_state = ICL_CONN_STATE_HEADER_DIGEST;
606 if (ic->ic_header_crc32c == false)
607 ic->ic_receive_len = 0;
609 ic->ic_receive_len = ISCSI_HEADER_DIGEST_SIZE;
612 case ICL_CONN_STATE_HEADER_DIGEST:
613 //ICL_DEBUG("receiving header digest");
614 error = icl_pdu_check_header_digest(request, availablep);
616 ICL_DEBUG("header digest failed; "
617 "dropping connection");
621 ic->ic_receive_state = ICL_CONN_STATE_DATA;
623 icl_pdu_data_segment_receive_len(request);
626 case ICL_CONN_STATE_DATA:
627 //ICL_DEBUG("receiving data segment");
628 error = icl_pdu_receive_data_segment(request, availablep,
631 ICL_DEBUG("failed to receive data segment;"
632 "dropping connection");
639 ic->ic_receive_state = ICL_CONN_STATE_DATA_DIGEST;
640 if (request->ip_data_len == 0 || ic->ic_data_crc32c == false)
641 ic->ic_receive_len = 0;
643 ic->ic_receive_len = ISCSI_DATA_DIGEST_SIZE;
646 case ICL_CONN_STATE_DATA_DIGEST:
647 //ICL_DEBUG("receiving data digest");
648 error = icl_pdu_check_data_digest(request, availablep);
650 ICL_DEBUG("data digest failed; "
651 "dropping connection");
656 * We've received complete PDU; reset the receive state machine
657 * and return the PDU.
659 ic->ic_receive_state = ICL_CONN_STATE_BHS;
660 ic->ic_receive_len = sizeof(struct iscsi_bhs);
661 ic->ic_receive_pdu = NULL;
665 panic("invalid ic_receive_state %d\n", ic->ic_receive_state);
670 * Don't free the PDU; it's pointed to by ic->ic_receive_pdu
671 * and will get freed in icl_conn_close().
680 icl_conn_receive_pdus(struct icl_conn *ic, size_t available)
682 struct icl_pdu *response;
688 * This can never happen; we're careful to only mess with ic->ic_socket
689 * pointer when the send/receive threads are not running.
691 KASSERT(so != NULL, ("NULL socket"));
694 if (ic->ic_disconnecting)
697 if (so->so_error != 0) {
698 ICL_DEBUG("connection error %d; "
699 "dropping connection", so->so_error);
705 * Loop until we have a complete PDU or there is not enough
706 * data in the socket buffer.
708 if (available < ic->ic_receive_len) {
710 ICL_DEBUG("not enough data; have %zd, "
711 "need %zd", available,
717 response = icl_conn_receive_pdu(ic, &available);
718 if (response == NULL)
721 if (response->ip_ahs_len > 0) {
722 ICL_WARN("received PDU with unsupported "
723 "AHS; opcode 0x%x; dropping connection",
724 response->ip_bhs->bhs_opcode);
725 icl_pdu_free(response);
730 (ic->ic_receive)(response);
735 icl_receive_thread(void *arg)
745 ic->ic_receive_running = true;
749 if (ic->ic_disconnecting) {
750 //ICL_DEBUG("terminating");
755 * Set the low watermark, to be checked by
756 * soreadable() in icl_soupcall_receive()
757 * to avoid unneccessary wakeups until there
758 * is enough data received to read the PDU.
760 SOCKBUF_LOCK(&so->so_rcv);
761 available = so->so_rcv.sb_cc;
762 if (available < ic->ic_receive_len) {
763 so->so_rcv.sb_lowat = ic->ic_receive_len;
764 cv_wait(&ic->ic_receive_cv, &so->so_rcv.sb_mtx);
766 so->so_rcv.sb_lowat = so->so_rcv.sb_hiwat + 1;
767 SOCKBUF_UNLOCK(&so->so_rcv);
769 icl_conn_receive_pdus(ic, available);
773 ic->ic_receive_running = false;
779 icl_soupcall_receive(struct socket *so, void *arg, int waitflag)
787 cv_signal(&ic->ic_receive_cv);
792 icl_pdu_finalize(struct icl_pdu *request)
794 size_t padding, pdu_len;
795 uint32_t digest, zero = 0;
799 ic = request->ip_conn;
801 icl_pdu_set_data_segment_length(request, request->ip_data_len);
803 pdu_len = icl_pdu_size(request);
805 if (ic->ic_header_crc32c) {
806 digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
807 ok = m_append(request->ip_bhs_mbuf, sizeof(digest),
810 ICL_WARN("failed to append header digest");
815 if (request->ip_data_len != 0) {
816 padding = icl_pdu_padding(request);
818 ok = m_append(request->ip_data_mbuf, padding,
821 ICL_WARN("failed to append padding");
826 if (ic->ic_data_crc32c) {
827 digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
829 ok = m_append(request->ip_data_mbuf, sizeof(digest),
832 ICL_WARN("failed to append data digest");
837 m_cat(request->ip_bhs_mbuf, request->ip_data_mbuf);
838 request->ip_data_mbuf = NULL;
841 request->ip_bhs_mbuf->m_pkthdr.len = pdu_len;
847 icl_conn_send_pdus(struct icl_conn *ic, struct icl_pdu_stailq *queue)
849 struct icl_pdu *request, *request2;
851 size_t available, size, size2;
852 int coalesced, error;
854 ICL_CONN_LOCK_ASSERT_NOT(ic);
858 SOCKBUF_LOCK(&so->so_snd);
860 * Check how much space do we have for transmit. We can't just
861 * call sosend() and retry when we get EWOULDBLOCK or EMSGSIZE,
862 * as it always frees the mbuf chain passed to it, even in case
865 available = sbspace(&so->so_snd);
868 * Notify the socket upcall that we don't need wakeups
869 * for the time being.
871 so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1;
872 SOCKBUF_UNLOCK(&so->so_snd);
874 while (!STAILQ_EMPTY(queue)) {
875 if (ic->ic_disconnecting)
877 request = STAILQ_FIRST(queue);
878 size = icl_pdu_size(request);
879 if (available < size) {
882 * Set the low watermark, to be checked by
883 * sowriteable() in icl_soupcall_send()
884 * to avoid unneccessary wakeups until there
885 * is enough space for the PDU to fit.
887 SOCKBUF_LOCK(&so->so_snd);
888 available = sbspace(&so->so_snd);
889 if (available < size) {
891 ICL_DEBUG("no space to send; "
892 "have %zd, need %zd",
895 so->so_snd.sb_lowat = size;
896 SOCKBUF_UNLOCK(&so->so_snd);
899 SOCKBUF_UNLOCK(&so->so_snd);
901 STAILQ_REMOVE_HEAD(queue, ip_next);
902 error = icl_pdu_finalize(request);
904 ICL_DEBUG("failed to finalize PDU; "
905 "dropping connection");
907 icl_pdu_free(request);
913 request2 = STAILQ_FIRST(queue);
914 if (request2 == NULL)
916 size2 = icl_pdu_size(request2);
917 if (available < size + size2)
919 STAILQ_REMOVE_HEAD(queue, ip_next);
920 error = icl_pdu_finalize(request2);
922 ICL_DEBUG("failed to finalize PDU; "
923 "dropping connection");
925 icl_pdu_free(request);
926 icl_pdu_free(request2);
929 m_cat(request->ip_bhs_mbuf, request2->ip_bhs_mbuf);
930 request2->ip_bhs_mbuf = NULL;
931 request->ip_bhs_mbuf->m_pkthdr.len += size2;
933 STAILQ_REMOVE_AFTER(queue, request, ip_next);
934 icl_pdu_free(request2);
939 ICL_DEBUG("coalesced %d PDUs into %zd bytes",
945 error = sosend(so, NULL, NULL, request->ip_bhs_mbuf,
946 NULL, MSG_DONTWAIT, curthread);
947 request->ip_bhs_mbuf = NULL; /* Sosend consumes the mbuf. */
949 ICL_DEBUG("failed to send PDU, error %d; "
950 "dropping connection", error);
952 icl_pdu_free(request);
955 icl_pdu_free(request);
960 icl_send_thread(void *arg)
963 struct icl_pdu_stailq queue;
970 ic->ic_send_running = true;
973 if (ic->ic_disconnecting) {
974 //ICL_DEBUG("terminating");
980 * If the local queue is empty, populate it from
981 * the main one. This way the icl_conn_send_pdus()
982 * can go through all the queued PDUs without holding
985 if (STAILQ_EMPTY(&queue))
986 STAILQ_SWAP(&ic->ic_to_send, &queue, icl_pdu);
988 ic->ic_check_send_space = false;
990 icl_conn_send_pdus(ic, &queue);
994 * The icl_soupcall_send() was called since the last
995 * call to sbspace(); go around;
997 if (ic->ic_check_send_space)
1001 * Local queue is empty, but we still have PDUs
1002 * in the main one; go around.
1004 if (STAILQ_EMPTY(&queue) &&
1005 !STAILQ_EMPTY(&ic->ic_to_send))
1009 * There might be some stuff in the local queue,
1010 * which didn't get sent due to not having enough send
1011 * space. Wait for socket upcall.
1016 cv_wait(&ic->ic_send_cv, ic->ic_lock);
1020 * We're exiting; move PDUs back to the main queue, so they can
1021 * get freed properly. At this point ordering doesn't matter.
1023 STAILQ_CONCAT(&ic->ic_to_send, &queue);
1025 ic->ic_send_running = false;
1026 ICL_CONN_UNLOCK(ic);
1031 icl_soupcall_send(struct socket *so, void *arg, int waitflag)
1033 struct icl_conn *ic;
1035 if (!sowriteable(so))
1041 ic->ic_check_send_space = true;
1042 ICL_CONN_UNLOCK(ic);
1044 cv_signal(&ic->ic_send_cv);
1050 icl_pdu_append_data(struct icl_pdu *request, const void *addr, size_t len,
1053 struct mbuf *mb, *newmb;
1054 size_t copylen, off = 0;
1056 KASSERT(len > 0, ("len == 0"));
1058 newmb = m_getm2(NULL, len, flags, MT_DATA, M_PKTHDR);
1059 if (newmb == NULL) {
1060 ICL_WARN("failed to allocate mbuf for %zd bytes", len);
1064 for (mb = newmb; mb != NULL; mb = mb->m_next) {
1065 copylen = min(M_TRAILINGSPACE(mb), len - off);
1066 memcpy(mtod(mb, char *), (const char *)addr + off, copylen);
1067 mb->m_len = copylen;
1070 KASSERT(off == len, ("%s: off != len", __func__));
1072 if (request->ip_data_mbuf == NULL) {
1073 request->ip_data_mbuf = newmb;
1074 request->ip_data_len = len;
1076 m_cat(request->ip_data_mbuf, newmb);
1077 request->ip_data_len += len;
1084 icl_pdu_get_data(struct icl_pdu *ip, size_t off, void *addr, size_t len)
1087 m_copydata(ip->ip_data_mbuf, off, len, addr);
1091 icl_pdu_queue(struct icl_pdu *ip)
1093 struct icl_conn *ic;
1097 ICL_CONN_LOCK_ASSERT(ic);
1099 if (ic->ic_disconnecting || ic->ic_socket == NULL) {
1100 ICL_DEBUG("icl_pdu_queue on closed connection");
1105 if (!STAILQ_EMPTY(&ic->ic_to_send)) {
1106 STAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next);
1108 * If the queue is not empty, someone else had already
1109 * signaled the send thread; no need to do that again,
1115 STAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next);
1116 cv_signal(&ic->ic_send_cv);
1120 icl_conn_new(const char *name, struct mtx *lock)
1122 struct icl_conn *ic;
1124 refcount_acquire(&icl_ncons);
1126 ic = uma_zalloc(icl_conn_zone, M_WAITOK | M_ZERO);
1128 STAILQ_INIT(&ic->ic_to_send);
1130 cv_init(&ic->ic_send_cv, "icl_tx");
1131 cv_init(&ic->ic_receive_cv, "icl_rx");
1133 refcount_init(&ic->ic_outstanding_pdus, 0);
1135 ic->ic_max_data_segment_length = ICL_MAX_DATA_SEGMENT_LENGTH;
1142 icl_conn_free(struct icl_conn *ic)
1145 cv_destroy(&ic->ic_send_cv);
1146 cv_destroy(&ic->ic_receive_cv);
1147 uma_zfree(icl_conn_zone, ic);
1148 refcount_release(&icl_ncons);
1152 icl_conn_start(struct icl_conn *ic)
1163 if (ic->ic_socket == NULL) {
1164 ICL_CONN_UNLOCK(ic);
1168 ic->ic_receive_state = ICL_CONN_STATE_BHS;
1169 ic->ic_receive_len = sizeof(struct iscsi_bhs);
1170 ic->ic_disconnecting = false;
1172 ICL_CONN_UNLOCK(ic);
1175 * For sendspace, this is required because the current code cannot
1176 * send a PDU in pieces; thus, the minimum buffer size is equal
1177 * to the maximum PDU size. "+4" is to account for possible padding.
1179 * What we should actually do here is to use autoscaling, but set
1180 * some minimal buffer size to "minspace". I don't know a way to do
1183 minspace = sizeof(struct iscsi_bhs) + ic->ic_max_data_segment_length +
1184 ISCSI_HEADER_DIGEST_SIZE + ISCSI_DATA_DIGEST_SIZE + 4;
1185 if (sendspace < minspace) {
1186 ICL_WARN("kern.icl.sendspace too low; must be at least %zd",
1188 sendspace = minspace;
1190 if (recvspace < minspace) {
1191 ICL_WARN("kern.icl.recvspace too low; must be at least %zd",
1193 recvspace = minspace;
1196 error = soreserve(ic->ic_socket, sendspace, recvspace);
1198 ICL_WARN("soreserve failed with error %d", error);
1206 bzero(&opt, sizeof(opt));
1207 opt.sopt_dir = SOPT_SET;
1208 opt.sopt_level = IPPROTO_TCP;
1209 opt.sopt_name = TCP_NODELAY;
1210 opt.sopt_val = &one;
1211 opt.sopt_valsize = sizeof(one);
1212 error = sosetopt(ic->ic_socket, &opt);
1214 ICL_WARN("disabling TCP_NODELAY failed with error %d", error);
1222 error = kthread_add(icl_send_thread, ic, NULL, NULL, 0, 0, "%stx",
1225 ICL_WARN("kthread_add(9) failed with error %d", error);
1230 error = kthread_add(icl_receive_thread, ic, NULL, NULL, 0, 0, "%srx",
1233 ICL_WARN("kthread_add(9) failed with error %d", error);
1239 * Register socket upcall, to get notified about incoming PDUs
1240 * and free space to send outgoing ones.
1242 SOCKBUF_LOCK(&ic->ic_socket->so_snd);
1243 soupcall_set(ic->ic_socket, SO_SND, icl_soupcall_send, ic);
1244 SOCKBUF_UNLOCK(&ic->ic_socket->so_snd);
1245 SOCKBUF_LOCK(&ic->ic_socket->so_rcv);
1246 soupcall_set(ic->ic_socket, SO_RCV, icl_soupcall_receive, ic);
1247 SOCKBUF_UNLOCK(&ic->ic_socket->so_rcv);
1253 icl_conn_handoff(struct icl_conn *ic, int fd)
1257 cap_rights_t rights;
1260 ICL_CONN_LOCK_ASSERT_NOT(ic);
1263 * Steal the socket from userland.
1265 error = fget(curthread, fd,
1266 cap_rights_init(&rights, CAP_SOCK_CLIENT), &fp);
1269 if (fp->f_type != DTYPE_SOCKET) {
1270 fdrop(fp, curthread);
1274 if (so->so_type != SOCK_STREAM) {
1275 fdrop(fp, curthread);
1281 if (ic->ic_socket != NULL) {
1282 ICL_CONN_UNLOCK(ic);
1283 fdrop(fp, curthread);
1287 ic->ic_socket = fp->f_data;
1288 fp->f_ops = &badfileops;
1290 fdrop(fp, curthread);
1291 ICL_CONN_UNLOCK(ic);
1293 error = icl_conn_start(ic);
1299 icl_conn_shutdown(struct icl_conn *ic)
1301 ICL_CONN_LOCK_ASSERT_NOT(ic);
1304 if (ic->ic_socket == NULL) {
1305 ICL_CONN_UNLOCK(ic);
1308 ICL_CONN_UNLOCK(ic);
1310 soshutdown(ic->ic_socket, SHUT_RDWR);
1314 icl_conn_close(struct icl_conn *ic)
1316 struct icl_pdu *pdu;
1318 ICL_CONN_LOCK_ASSERT_NOT(ic);
1321 if (ic->ic_socket == NULL) {
1322 ICL_CONN_UNLOCK(ic);
1327 * Deregister socket upcalls.
1329 ICL_CONN_UNLOCK(ic);
1330 SOCKBUF_LOCK(&ic->ic_socket->so_snd);
1331 if (ic->ic_socket->so_snd.sb_upcall != NULL)
1332 soupcall_clear(ic->ic_socket, SO_SND);
1333 SOCKBUF_UNLOCK(&ic->ic_socket->so_snd);
1334 SOCKBUF_LOCK(&ic->ic_socket->so_rcv);
1335 if (ic->ic_socket->so_rcv.sb_upcall != NULL)
1336 soupcall_clear(ic->ic_socket, SO_RCV);
1337 SOCKBUF_UNLOCK(&ic->ic_socket->so_rcv);
1340 ic->ic_disconnecting = true;
1343 * Wake up the threads, so they can properly terminate.
1345 cv_signal(&ic->ic_receive_cv);
1346 cv_signal(&ic->ic_send_cv);
1347 while (ic->ic_receive_running || ic->ic_send_running) {
1348 //ICL_DEBUG("waiting for send/receive threads to terminate");
1349 ICL_CONN_UNLOCK(ic);
1350 cv_signal(&ic->ic_receive_cv);
1351 cv_signal(&ic->ic_send_cv);
1352 pause("icl_close", 1 * hz);
1355 //ICL_DEBUG("send/receive threads terminated");
1357 ICL_CONN_UNLOCK(ic);
1358 soclose(ic->ic_socket);
1360 ic->ic_socket = NULL;
1362 if (ic->ic_receive_pdu != NULL) {
1363 //ICL_DEBUG("freeing partially received PDU");
1364 icl_pdu_free(ic->ic_receive_pdu);
1365 ic->ic_receive_pdu = NULL;
1369 * Remove any outstanding PDUs from the send queue.
1371 while (!STAILQ_EMPTY(&ic->ic_to_send)) {
1372 pdu = STAILQ_FIRST(&ic->ic_to_send);
1373 STAILQ_REMOVE_HEAD(&ic->ic_to_send, ip_next);
1377 KASSERT(STAILQ_EMPTY(&ic->ic_to_send),
1378 ("destroying session with non-empty send queue"));
1380 KASSERT(ic->ic_outstanding_pdus == 0,
1381 ("destroying session with %d outstanding PDUs",
1382 ic->ic_outstanding_pdus));
1384 ICL_CONN_UNLOCK(ic);
1388 icl_conn_connected(struct icl_conn *ic)
1390 ICL_CONN_LOCK_ASSERT_NOT(ic);
1393 if (ic->ic_socket == NULL) {
1394 ICL_CONN_UNLOCK(ic);
1397 if (ic->ic_socket->so_error != 0) {
1398 ICL_CONN_UNLOCK(ic);
1401 ICL_CONN_UNLOCK(ic);
1405 #ifdef ICL_KERNEL_PROXY
1407 icl_conn_handoff_sock(struct icl_conn *ic, struct socket *so)
1411 ICL_CONN_LOCK_ASSERT_NOT(ic);
1413 if (so->so_type != SOCK_STREAM)
1417 if (ic->ic_socket != NULL) {
1418 ICL_CONN_UNLOCK(ic);
1422 ICL_CONN_UNLOCK(ic);
1424 error = icl_conn_start(ic);
1428 #endif /* ICL_KERNEL_PROXY */
1437 uma_zdestroy(icl_conn_zone);
1438 uma_zdestroy(icl_pdu_zone);
1447 icl_conn_zone = uma_zcreate("icl_conn",
1448 sizeof(struct icl_conn), NULL, NULL, NULL, NULL,
1450 icl_pdu_zone = uma_zcreate("icl_pdu",
1451 sizeof(struct icl_pdu), NULL, NULL, NULL, NULL,
1454 refcount_init(&icl_ncons, 0);
1458 icl_modevent(module_t mod, int what, void *arg)
1466 return (icl_unload());
1472 moduledata_t icl_data = {
1478 DECLARE_MODULE(icl, icl_data, SI_SUB_DRIVERS, SI_ORDER_FIRST);
1479 MODULE_VERSION(icl, 1);