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 TUNABLE_INT("kern.icl.debug", &debug);
67 SYSCTL_INT(_kern_icl, OID_AUTO, debug, CTLFLAG_RWTUN,
68 &debug, 0, "Enable debug messages");
69 static int coalesce = 1;
70 TUNABLE_INT("kern.icl.coalesce", &coalesce);
71 SYSCTL_INT(_kern_icl, OID_AUTO, coalesce, CTLFLAG_RWTUN,
72 &coalesce, 0, "Try to coalesce PDUs before sending");
73 static int partial_receive_len = 128 * 1024;
74 TUNABLE_INT("kern.icl.partial_receive_len", &partial_receive_len);
75 SYSCTL_INT(_kern_icl, OID_AUTO, partial_receive_len, CTLFLAG_RWTUN,
76 &partial_receive_len, 0, "Minimum read size for partially received "
78 static int sendspace = 1048576;
79 TUNABLE_INT("kern.icl.sendspace", &sendspace);
80 SYSCTL_INT(_kern_icl, OID_AUTO, sendspace, CTLFLAG_RWTUN,
81 &sendspace, 0, "Default send socket buffer size");
82 static int recvspace = 1048576;
83 TUNABLE_INT("kern.icl.recvspace", &recvspace);
84 SYSCTL_INT(_kern_icl, OID_AUTO, recvspace, CTLFLAG_RWTUN,
85 &recvspace, 0, "Default receive socket buffer size");
87 static uma_zone_t icl_conn_zone;
88 static uma_zone_t icl_pdu_zone;
90 static volatile u_int icl_ncons;
92 #define ICL_DEBUG(X, ...) \
95 printf("%s: " X "\n", __func__, ## __VA_ARGS__);\
98 #define ICL_WARN(X, ...) \
101 printf("WARNING: %s: " X "\n", \
102 __func__, ## __VA_ARGS__); \
106 #define ICL_CONN_LOCK(X) mtx_lock(X->ic_lock)
107 #define ICL_CONN_UNLOCK(X) mtx_unlock(X->ic_lock)
108 #define ICL_CONN_LOCK_ASSERT(X) mtx_assert(X->ic_lock, MA_OWNED)
109 #define ICL_CONN_LOCK_ASSERT_NOT(X) mtx_assert(X->ic_lock, MA_NOTOWNED)
111 STAILQ_HEAD(icl_pdu_stailq, icl_pdu);
114 icl_conn_fail(struct icl_conn *ic)
116 if (ic->ic_socket == NULL)
122 ic->ic_socket->so_error = EDOOFUS;
127 icl_conn_receive(struct icl_conn *ic, size_t len)
136 memset(&uio, 0, sizeof(uio));
139 flags = MSG_DONTWAIT;
140 error = soreceive(so, NULL, &uio, &m, NULL, &flags);
142 ICL_DEBUG("soreceive error %d", error);
145 if (uio.uio_resid != 0) {
147 ICL_DEBUG("short read");
154 static struct icl_pdu *
155 icl_pdu_new_empty(struct icl_conn *ic, int flags)
160 refcount_acquire(&ic->ic_outstanding_pdus);
162 ip = uma_zalloc(icl_pdu_zone, flags | M_ZERO);
164 ICL_WARN("failed to allocate %zd bytes", sizeof(*ip));
166 refcount_release(&ic->ic_outstanding_pdus);
177 icl_pdu_free(struct icl_pdu *ip)
183 m_freem(ip->ip_bhs_mbuf);
184 m_freem(ip->ip_ahs_mbuf);
185 m_freem(ip->ip_data_mbuf);
186 uma_zfree(icl_pdu_zone, ip);
188 refcount_release(&ic->ic_outstanding_pdus);
193 * Allocate icl_pdu with empty BHS to fill up by the caller.
196 icl_pdu_new(struct icl_conn *ic, int flags)
200 ip = icl_pdu_new_empty(ic, flags);
204 ip->ip_bhs_mbuf = m_getm2(NULL, sizeof(struct iscsi_bhs),
205 flags, MT_DATA, M_PKTHDR);
206 if (ip->ip_bhs_mbuf == NULL) {
207 ICL_WARN("failed to allocate %zd bytes", sizeof(*ip));
211 ip->ip_bhs = mtod(ip->ip_bhs_mbuf, struct iscsi_bhs *);
212 memset(ip->ip_bhs, 0, sizeof(struct iscsi_bhs));
213 ip->ip_bhs_mbuf->m_len = sizeof(struct iscsi_bhs);
219 icl_pdu_ahs_length(const struct icl_pdu *request)
222 return (request->ip_bhs->bhs_total_ahs_len * 4);
226 icl_pdu_data_segment_length(const struct icl_pdu *request)
230 len += request->ip_bhs->bhs_data_segment_len[0];
232 len += request->ip_bhs->bhs_data_segment_len[1];
234 len += request->ip_bhs->bhs_data_segment_len[2];
240 icl_pdu_set_data_segment_length(struct icl_pdu *response, uint32_t len)
243 response->ip_bhs->bhs_data_segment_len[2] = len;
244 response->ip_bhs->bhs_data_segment_len[1] = len >> 8;
245 response->ip_bhs->bhs_data_segment_len[0] = len >> 16;
249 icl_pdu_padding(const struct icl_pdu *ip)
252 if ((ip->ip_data_len % 4) != 0)
253 return (4 - (ip->ip_data_len % 4));
259 icl_pdu_size(const struct icl_pdu *response)
263 KASSERT(response->ip_ahs_len == 0, ("responding with AHS"));
265 len = sizeof(struct iscsi_bhs) + response->ip_data_len +
266 icl_pdu_padding(response);
267 if (response->ip_conn->ic_header_crc32c)
268 len += ISCSI_HEADER_DIGEST_SIZE;
269 if (response->ip_data_len != 0 && response->ip_conn->ic_data_crc32c)
270 len += ISCSI_DATA_DIGEST_SIZE;
276 icl_pdu_receive_bhs(struct icl_pdu *request, size_t *availablep)
280 m = icl_conn_receive(request->ip_conn, sizeof(struct iscsi_bhs));
282 ICL_DEBUG("failed to receive BHS");
286 request->ip_bhs_mbuf = m_pullup(m, sizeof(struct iscsi_bhs));
287 if (request->ip_bhs_mbuf == NULL) {
288 ICL_WARN("m_pullup failed");
291 request->ip_bhs = mtod(request->ip_bhs_mbuf, struct iscsi_bhs *);
294 * XXX: For architectures with strict alignment requirements
295 * we may need to allocate ip_bhs and copy the data into it.
296 * For some reason, though, not doing this doesn't seem
297 * to cause problems; tested on sparc64.
300 *availablep -= sizeof(struct iscsi_bhs);
305 icl_pdu_receive_ahs(struct icl_pdu *request, size_t *availablep)
308 request->ip_ahs_len = icl_pdu_ahs_length(request);
309 if (request->ip_ahs_len == 0)
312 request->ip_ahs_mbuf = icl_conn_receive(request->ip_conn,
313 request->ip_ahs_len);
314 if (request->ip_ahs_mbuf == NULL) {
315 ICL_DEBUG("failed to receive AHS");
319 *availablep -= request->ip_ahs_len;
324 icl_mbuf_to_crc32c(const struct mbuf *m0)
326 uint32_t digest = 0xffffffff;
327 const struct mbuf *m;
329 for (m = m0; m != NULL; m = m->m_next)
330 digest = calculate_crc32c(digest,
331 mtod(m, const void *), m->m_len);
333 digest = digest ^ 0xffffffff;
339 icl_pdu_check_header_digest(struct icl_pdu *request, size_t *availablep)
342 uint32_t received_digest, valid_digest;
344 if (request->ip_conn->ic_header_crc32c == false)
347 m = icl_conn_receive(request->ip_conn, ISCSI_HEADER_DIGEST_SIZE);
349 ICL_DEBUG("failed to receive header digest");
353 CTASSERT(sizeof(received_digest) == ISCSI_HEADER_DIGEST_SIZE);
354 m_copydata(m, 0, ISCSI_HEADER_DIGEST_SIZE, (void *)&received_digest);
357 *availablep -= ISCSI_HEADER_DIGEST_SIZE;
362 valid_digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
363 if (received_digest != valid_digest) {
364 ICL_WARN("header digest check failed; got 0x%x, "
365 "should be 0x%x", received_digest, valid_digest);
373 * Return the number of bytes that should be waiting in the receive socket
374 * before icl_pdu_receive_data_segment() gets called.
377 icl_pdu_data_segment_receive_len(const struct icl_pdu *request)
381 len = icl_pdu_data_segment_length(request);
386 * Account for the parts of data segment already read from
389 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
390 len -= request->ip_data_len;
393 * Don't always wait for the full data segment to be delivered
394 * to the socket; this might badly affect performance due to
395 * TCP window scaling.
397 if (len > partial_receive_len) {
399 ICL_DEBUG("need %zd bytes of data, limiting to %zd",
400 len, partial_receive_len));
402 len = partial_receive_len;
408 * Account for padding. Note that due to the way code is written,
409 * the icl_pdu_receive_data_segment() must always receive padding
410 * along with the last part of data segment, because it would be
411 * impossible to tell whether we've already received the full data
412 * segment including padding, or without it.
415 len += 4 - (len % 4);
418 ICL_DEBUG("need %zd bytes of data", len));
425 icl_pdu_receive_data_segment(struct icl_pdu *request,
426 size_t *availablep, bool *more_neededp)
429 size_t len, padding = 0;
432 ic = request->ip_conn;
434 *more_neededp = false;
435 ic->ic_receive_len = 0;
437 len = icl_pdu_data_segment_length(request);
442 padding = 4 - (len % 4);
445 * Account for already received parts of data segment.
447 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
448 len -= request->ip_data_len;
450 if (len + padding > *availablep) {
452 * Not enough data in the socket buffer. Receive as much
453 * as we can. Don't receive padding, since, obviously, it's
454 * not the end of data segment yet.
457 ICL_DEBUG("limited from %zd to %zd",
458 len + padding, *availablep - padding));
460 len = *availablep - padding;
461 *more_neededp = true;
466 * Must not try to receive padding without at least one byte
467 * of actual data segment.
470 m = icl_conn_receive(request->ip_conn, len + padding);
472 ICL_DEBUG("failed to receive data segment");
476 if (request->ip_data_mbuf == NULL)
477 request->ip_data_mbuf = m;
479 m_cat(request->ip_data_mbuf, m);
481 request->ip_data_len += len;
482 *availablep -= len + padding;
488 icl_pdu_data_segment_receive_len(request);
494 icl_pdu_check_data_digest(struct icl_pdu *request, size_t *availablep)
497 uint32_t received_digest, valid_digest;
499 if (request->ip_conn->ic_data_crc32c == false)
502 if (request->ip_data_len == 0)
505 m = icl_conn_receive(request->ip_conn, ISCSI_DATA_DIGEST_SIZE);
507 ICL_DEBUG("failed to receive data digest");
511 CTASSERT(sizeof(received_digest) == ISCSI_DATA_DIGEST_SIZE);
512 m_copydata(m, 0, ISCSI_DATA_DIGEST_SIZE, (void *)&received_digest);
515 *availablep -= ISCSI_DATA_DIGEST_SIZE;
518 * Note that ip_data_mbuf also contains padding; since digest
519 * calculation is supposed to include that, we iterate over
520 * the entire ip_data_mbuf chain, not just ip_data_len bytes of it.
522 valid_digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
523 if (received_digest != valid_digest) {
524 ICL_WARN("data digest check failed; got 0x%x, "
525 "should be 0x%x", received_digest, valid_digest);
533 * Somewhat contrary to the name, this attempts to receive only one
534 * "part" of PDU at a time; call it repeatedly until it returns non-NULL.
536 static struct icl_pdu *
537 icl_conn_receive_pdu(struct icl_conn *ic, size_t *availablep)
539 struct icl_pdu *request;
547 if (ic->ic_receive_state == ICL_CONN_STATE_BHS) {
548 KASSERT(ic->ic_receive_pdu == NULL,
549 ("ic->ic_receive_pdu != NULL"));
550 request = icl_pdu_new_empty(ic, M_NOWAIT);
551 if (request == NULL) {
552 ICL_DEBUG("failed to allocate PDU; "
553 "dropping connection");
557 ic->ic_receive_pdu = request;
559 KASSERT(ic->ic_receive_pdu != NULL,
560 ("ic->ic_receive_pdu == NULL"));
561 request = ic->ic_receive_pdu;
564 if (*availablep < ic->ic_receive_len) {
566 ICL_DEBUG("not enough data; need %zd, "
567 "have %zd", ic->ic_receive_len, *availablep);
572 switch (ic->ic_receive_state) {
573 case ICL_CONN_STATE_BHS:
574 //ICL_DEBUG("receiving BHS");
575 error = icl_pdu_receive_bhs(request, availablep);
577 ICL_DEBUG("failed to receive BHS; "
578 "dropping connection");
583 * We don't enforce any limit for AHS length;
584 * its length is stored in 8 bit field.
587 len = icl_pdu_data_segment_length(request);
588 if (len > ic->ic_max_data_segment_length) {
589 ICL_WARN("received data segment "
590 "length %zd is larger than negotiated "
591 "MaxDataSegmentLength %zd; "
592 "dropping connection",
593 len, ic->ic_max_data_segment_length);
598 ic->ic_receive_state = ICL_CONN_STATE_AHS;
599 ic->ic_receive_len = icl_pdu_ahs_length(request);
602 case ICL_CONN_STATE_AHS:
603 //ICL_DEBUG("receiving AHS");
604 error = icl_pdu_receive_ahs(request, availablep);
606 ICL_DEBUG("failed to receive AHS; "
607 "dropping connection");
610 ic->ic_receive_state = ICL_CONN_STATE_HEADER_DIGEST;
611 if (ic->ic_header_crc32c == false)
612 ic->ic_receive_len = 0;
614 ic->ic_receive_len = ISCSI_HEADER_DIGEST_SIZE;
617 case ICL_CONN_STATE_HEADER_DIGEST:
618 //ICL_DEBUG("receiving header digest");
619 error = icl_pdu_check_header_digest(request, availablep);
621 ICL_DEBUG("header digest failed; "
622 "dropping connection");
626 ic->ic_receive_state = ICL_CONN_STATE_DATA;
628 icl_pdu_data_segment_receive_len(request);
631 case ICL_CONN_STATE_DATA:
632 //ICL_DEBUG("receiving data segment");
633 error = icl_pdu_receive_data_segment(request, availablep,
636 ICL_DEBUG("failed to receive data segment;"
637 "dropping connection");
644 ic->ic_receive_state = ICL_CONN_STATE_DATA_DIGEST;
645 if (request->ip_data_len == 0 || ic->ic_data_crc32c == false)
646 ic->ic_receive_len = 0;
648 ic->ic_receive_len = ISCSI_DATA_DIGEST_SIZE;
651 case ICL_CONN_STATE_DATA_DIGEST:
652 //ICL_DEBUG("receiving data digest");
653 error = icl_pdu_check_data_digest(request, availablep);
655 ICL_DEBUG("data digest failed; "
656 "dropping connection");
661 * We've received complete PDU; reset the receive state machine
662 * and return the PDU.
664 ic->ic_receive_state = ICL_CONN_STATE_BHS;
665 ic->ic_receive_len = sizeof(struct iscsi_bhs);
666 ic->ic_receive_pdu = NULL;
670 panic("invalid ic_receive_state %d\n", ic->ic_receive_state);
675 * Don't free the PDU; it's pointed to by ic->ic_receive_pdu
676 * and will get freed in icl_conn_close().
685 icl_conn_receive_pdus(struct icl_conn *ic, size_t available)
687 struct icl_pdu *response;
693 * This can never happen; we're careful to only mess with ic->ic_socket
694 * pointer when the send/receive threads are not running.
696 KASSERT(so != NULL, ("NULL socket"));
699 if (ic->ic_disconnecting)
702 if (so->so_error != 0) {
703 ICL_DEBUG("connection error %d; "
704 "dropping connection", so->so_error);
710 * Loop until we have a complete PDU or there is not enough
711 * data in the socket buffer.
713 if (available < ic->ic_receive_len) {
715 ICL_DEBUG("not enough data; have %zd, "
716 "need %zd", available,
722 response = icl_conn_receive_pdu(ic, &available);
723 if (response == NULL)
726 if (response->ip_ahs_len > 0) {
727 ICL_WARN("received PDU with unsupported "
728 "AHS; opcode 0x%x; dropping connection",
729 response->ip_bhs->bhs_opcode);
730 icl_pdu_free(response);
735 (ic->ic_receive)(response);
740 icl_receive_thread(void *arg)
750 ic->ic_receive_running = true;
754 if (ic->ic_disconnecting) {
755 //ICL_DEBUG("terminating");
760 * Set the low watermark, to be checked by
761 * soreadable() in icl_soupcall_receive()
762 * to avoid unneccessary wakeups until there
763 * is enough data received to read the PDU.
765 SOCKBUF_LOCK(&so->so_rcv);
766 available = so->so_rcv.sb_cc;
767 if (available < ic->ic_receive_len) {
768 so->so_rcv.sb_lowat = ic->ic_receive_len;
769 cv_wait(&ic->ic_receive_cv, &so->so_rcv.sb_mtx);
771 so->so_rcv.sb_lowat = so->so_rcv.sb_hiwat + 1;
772 SOCKBUF_UNLOCK(&so->so_rcv);
774 icl_conn_receive_pdus(ic, available);
778 ic->ic_receive_running = false;
779 cv_signal(&ic->ic_send_cv);
785 icl_soupcall_receive(struct socket *so, void *arg, int waitflag)
793 cv_signal(&ic->ic_receive_cv);
798 icl_pdu_finalize(struct icl_pdu *request)
800 size_t padding, pdu_len;
801 uint32_t digest, zero = 0;
805 ic = request->ip_conn;
807 icl_pdu_set_data_segment_length(request, request->ip_data_len);
809 pdu_len = icl_pdu_size(request);
811 if (ic->ic_header_crc32c) {
812 digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
813 ok = m_append(request->ip_bhs_mbuf, sizeof(digest),
816 ICL_WARN("failed to append header digest");
821 if (request->ip_data_len != 0) {
822 padding = icl_pdu_padding(request);
824 ok = m_append(request->ip_data_mbuf, padding,
827 ICL_WARN("failed to append padding");
832 if (ic->ic_data_crc32c) {
833 digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
835 ok = m_append(request->ip_data_mbuf, sizeof(digest),
838 ICL_WARN("failed to append data digest");
843 m_cat(request->ip_bhs_mbuf, request->ip_data_mbuf);
844 request->ip_data_mbuf = NULL;
847 request->ip_bhs_mbuf->m_pkthdr.len = pdu_len;
853 icl_conn_send_pdus(struct icl_conn *ic, struct icl_pdu_stailq *queue)
855 struct icl_pdu *request, *request2;
857 size_t available, size, size2;
858 int coalesced, error;
860 ICL_CONN_LOCK_ASSERT_NOT(ic);
864 SOCKBUF_LOCK(&so->so_snd);
866 * Check how much space do we have for transmit. We can't just
867 * call sosend() and retry when we get EWOULDBLOCK or EMSGSIZE,
868 * as it always frees the mbuf chain passed to it, even in case
871 available = sbspace(&so->so_snd);
874 * Notify the socket upcall that we don't need wakeups
875 * for the time being.
877 so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1;
878 SOCKBUF_UNLOCK(&so->so_snd);
880 while (!STAILQ_EMPTY(queue)) {
881 request = STAILQ_FIRST(queue);
882 size = icl_pdu_size(request);
883 if (available < size) {
886 * Set the low watermark, to be checked by
887 * sowriteable() in icl_soupcall_send()
888 * to avoid unneccessary wakeups until there
889 * is enough space for the PDU to fit.
891 SOCKBUF_LOCK(&so->so_snd);
892 available = sbspace(&so->so_snd);
893 if (available < size) {
895 ICL_DEBUG("no space to send; "
896 "have %zd, need %zd",
899 so->so_snd.sb_lowat = size;
900 SOCKBUF_UNLOCK(&so->so_snd);
903 SOCKBUF_UNLOCK(&so->so_snd);
905 STAILQ_REMOVE_HEAD(queue, ip_next);
906 error = icl_pdu_finalize(request);
908 ICL_DEBUG("failed to finalize PDU; "
909 "dropping connection");
911 icl_pdu_free(request);
917 request2 = STAILQ_FIRST(queue);
918 if (request2 == NULL)
920 size2 = icl_pdu_size(request2);
921 if (available < size + size2)
923 STAILQ_REMOVE_HEAD(queue, ip_next);
924 error = icl_pdu_finalize(request2);
926 ICL_DEBUG("failed to finalize PDU; "
927 "dropping connection");
929 icl_pdu_free(request);
930 icl_pdu_free(request2);
933 m_cat(request->ip_bhs_mbuf, request2->ip_bhs_mbuf);
934 request2->ip_bhs_mbuf = NULL;
935 request->ip_bhs_mbuf->m_pkthdr.len += size2;
937 STAILQ_REMOVE_AFTER(queue, request, ip_next);
938 icl_pdu_free(request2);
943 ICL_DEBUG("coalesced %d PDUs into %zd bytes",
949 error = sosend(so, NULL, NULL, request->ip_bhs_mbuf,
950 NULL, MSG_DONTWAIT, curthread);
951 request->ip_bhs_mbuf = NULL; /* Sosend consumes the mbuf. */
953 ICL_DEBUG("failed to send PDU, error %d; "
954 "dropping connection", error);
956 icl_pdu_free(request);
959 icl_pdu_free(request);
964 icl_send_thread(void *arg)
967 struct icl_pdu_stailq queue;
974 ic->ic_send_running = true;
979 * If the local queue is empty, populate it from
980 * the main one. This way the icl_conn_send_pdus()
981 * can go through all the queued PDUs without holding
984 if (STAILQ_EMPTY(&queue))
985 STAILQ_SWAP(&ic->ic_to_send, &queue, icl_pdu);
987 ic->ic_check_send_space = false;
989 icl_conn_send_pdus(ic, &queue);
993 * The icl_soupcall_send() was called since the last
994 * call to sbspace(); go around;
996 if (ic->ic_check_send_space)
1000 * Local queue is empty, but we still have PDUs
1001 * in the main one; go around.
1003 if (STAILQ_EMPTY(&queue) &&
1004 !STAILQ_EMPTY(&ic->ic_to_send))
1008 * There might be some stuff in the local queue,
1009 * which didn't get sent due to not having enough send
1010 * space. Wait for socket upcall.
1015 if (ic->ic_disconnecting) {
1016 //ICL_DEBUG("terminating");
1020 cv_wait(&ic->ic_send_cv, ic->ic_lock);
1024 * We're exiting; move PDUs back to the main queue, so they can
1025 * get freed properly. At this point ordering doesn't matter.
1027 STAILQ_CONCAT(&ic->ic_to_send, &queue);
1029 ic->ic_send_running = false;
1030 cv_signal(&ic->ic_send_cv);
1031 ICL_CONN_UNLOCK(ic);
1036 icl_soupcall_send(struct socket *so, void *arg, int waitflag)
1038 struct icl_conn *ic;
1040 if (!sowriteable(so))
1046 ic->ic_check_send_space = true;
1047 ICL_CONN_UNLOCK(ic);
1049 cv_signal(&ic->ic_send_cv);
1055 icl_pdu_append_data(struct icl_pdu *request, const void *addr, size_t len,
1058 struct mbuf *mb, *newmb;
1059 size_t copylen, off = 0;
1061 KASSERT(len > 0, ("len == 0"));
1063 newmb = m_getm2(NULL, len, flags, MT_DATA, M_PKTHDR);
1064 if (newmb == NULL) {
1065 ICL_WARN("failed to allocate mbuf for %zd bytes", len);
1069 for (mb = newmb; mb != NULL; mb = mb->m_next) {
1070 copylen = min(M_TRAILINGSPACE(mb), len - off);
1071 memcpy(mtod(mb, char *), (const char *)addr + off, copylen);
1072 mb->m_len = copylen;
1075 KASSERT(off == len, ("%s: off != len", __func__));
1077 if (request->ip_data_mbuf == NULL) {
1078 request->ip_data_mbuf = newmb;
1079 request->ip_data_len = len;
1081 m_cat(request->ip_data_mbuf, newmb);
1082 request->ip_data_len += len;
1089 icl_pdu_get_data(struct icl_pdu *ip, size_t off, void *addr, size_t len)
1092 m_copydata(ip->ip_data_mbuf, off, len, addr);
1096 icl_pdu_queue(struct icl_pdu *ip)
1098 struct icl_conn *ic;
1102 ICL_CONN_LOCK_ASSERT(ic);
1104 if (ic->ic_disconnecting || ic->ic_socket == NULL) {
1105 ICL_DEBUG("icl_pdu_queue on closed connection");
1110 if (!STAILQ_EMPTY(&ic->ic_to_send)) {
1111 STAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next);
1113 * If the queue is not empty, someone else had already
1114 * signaled the send thread; no need to do that again,
1120 STAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next);
1121 cv_signal(&ic->ic_send_cv);
1125 icl_conn_new(const char *name, struct mtx *lock)
1127 struct icl_conn *ic;
1129 refcount_acquire(&icl_ncons);
1131 ic = uma_zalloc(icl_conn_zone, M_WAITOK | M_ZERO);
1133 STAILQ_INIT(&ic->ic_to_send);
1135 cv_init(&ic->ic_send_cv, "icl_tx");
1136 cv_init(&ic->ic_receive_cv, "icl_rx");
1138 refcount_init(&ic->ic_outstanding_pdus, 0);
1140 ic->ic_max_data_segment_length = ICL_MAX_DATA_SEGMENT_LENGTH;
1147 icl_conn_free(struct icl_conn *ic)
1150 cv_destroy(&ic->ic_send_cv);
1151 cv_destroy(&ic->ic_receive_cv);
1152 uma_zfree(icl_conn_zone, ic);
1153 refcount_release(&icl_ncons);
1157 icl_conn_start(struct icl_conn *ic)
1168 if (ic->ic_socket == NULL) {
1169 ICL_CONN_UNLOCK(ic);
1173 ic->ic_receive_state = ICL_CONN_STATE_BHS;
1174 ic->ic_receive_len = sizeof(struct iscsi_bhs);
1175 ic->ic_disconnecting = false;
1177 ICL_CONN_UNLOCK(ic);
1180 * For sendspace, this is required because the current code cannot
1181 * send a PDU in pieces; thus, the minimum buffer size is equal
1182 * to the maximum PDU size. "+4" is to account for possible padding.
1184 * What we should actually do here is to use autoscaling, but set
1185 * some minimal buffer size to "minspace". I don't know a way to do
1188 minspace = sizeof(struct iscsi_bhs) + ic->ic_max_data_segment_length +
1189 ISCSI_HEADER_DIGEST_SIZE + ISCSI_DATA_DIGEST_SIZE + 4;
1190 if (sendspace < minspace) {
1191 ICL_WARN("kern.icl.sendspace too low; must be at least %zd",
1193 sendspace = minspace;
1195 if (recvspace < minspace) {
1196 ICL_WARN("kern.icl.recvspace too low; must be at least %zd",
1198 recvspace = minspace;
1201 error = soreserve(ic->ic_socket, sendspace, recvspace);
1203 ICL_WARN("soreserve failed with error %d", error);
1207 ic->ic_socket->so_snd.sb_flags |= SB_AUTOSIZE;
1208 ic->ic_socket->so_rcv.sb_flags |= SB_AUTOSIZE;
1213 bzero(&opt, sizeof(opt));
1214 opt.sopt_dir = SOPT_SET;
1215 opt.sopt_level = IPPROTO_TCP;
1216 opt.sopt_name = TCP_NODELAY;
1217 opt.sopt_val = &one;
1218 opt.sopt_valsize = sizeof(one);
1219 error = sosetopt(ic->ic_socket, &opt);
1221 ICL_WARN("disabling TCP_NODELAY failed with error %d", error);
1229 error = kthread_add(icl_send_thread, ic, NULL, NULL, 0, 0, "%stx",
1232 ICL_WARN("kthread_add(9) failed with error %d", error);
1237 error = kthread_add(icl_receive_thread, ic, NULL, NULL, 0, 0, "%srx",
1240 ICL_WARN("kthread_add(9) failed with error %d", error);
1246 * Register socket upcall, to get notified about incoming PDUs
1247 * and free space to send outgoing ones.
1249 SOCKBUF_LOCK(&ic->ic_socket->so_snd);
1250 soupcall_set(ic->ic_socket, SO_SND, icl_soupcall_send, ic);
1251 SOCKBUF_UNLOCK(&ic->ic_socket->so_snd);
1252 SOCKBUF_LOCK(&ic->ic_socket->so_rcv);
1253 soupcall_set(ic->ic_socket, SO_RCV, icl_soupcall_receive, ic);
1254 SOCKBUF_UNLOCK(&ic->ic_socket->so_rcv);
1260 icl_conn_handoff(struct icl_conn *ic, int fd)
1264 cap_rights_t rights;
1267 ICL_CONN_LOCK_ASSERT_NOT(ic);
1270 * Steal the socket from userland.
1272 error = fget(curthread, fd,
1273 cap_rights_init(&rights, CAP_SOCK_CLIENT), &fp);
1276 if (fp->f_type != DTYPE_SOCKET) {
1277 fdrop(fp, curthread);
1281 if (so->so_type != SOCK_STREAM) {
1282 fdrop(fp, curthread);
1288 if (ic->ic_socket != NULL) {
1289 ICL_CONN_UNLOCK(ic);
1290 fdrop(fp, curthread);
1294 ic->ic_socket = fp->f_data;
1295 fp->f_ops = &badfileops;
1297 fdrop(fp, curthread);
1298 ICL_CONN_UNLOCK(ic);
1300 error = icl_conn_start(ic);
1306 icl_conn_close(struct icl_conn *ic)
1308 struct icl_pdu *pdu;
1310 ICL_CONN_LOCK_ASSERT_NOT(ic);
1313 if (ic->ic_socket == NULL) {
1314 ICL_CONN_UNLOCK(ic);
1319 * Deregister socket upcalls.
1321 ICL_CONN_UNLOCK(ic);
1322 SOCKBUF_LOCK(&ic->ic_socket->so_snd);
1323 if (ic->ic_socket->so_snd.sb_upcall != NULL)
1324 soupcall_clear(ic->ic_socket, SO_SND);
1325 SOCKBUF_UNLOCK(&ic->ic_socket->so_snd);
1326 SOCKBUF_LOCK(&ic->ic_socket->so_rcv);
1327 if (ic->ic_socket->so_rcv.sb_upcall != NULL)
1328 soupcall_clear(ic->ic_socket, SO_RCV);
1329 SOCKBUF_UNLOCK(&ic->ic_socket->so_rcv);
1332 ic->ic_disconnecting = true;
1335 * Wake up the threads, so they can properly terminate.
1337 while (ic->ic_receive_running || ic->ic_send_running) {
1338 //ICL_DEBUG("waiting for send/receive threads to terminate");
1339 cv_signal(&ic->ic_receive_cv);
1340 cv_signal(&ic->ic_send_cv);
1341 cv_wait(&ic->ic_send_cv, ic->ic_lock);
1343 //ICL_DEBUG("send/receive threads terminated");
1345 ICL_CONN_UNLOCK(ic);
1346 soclose(ic->ic_socket);
1348 ic->ic_socket = NULL;
1350 if (ic->ic_receive_pdu != NULL) {
1351 //ICL_DEBUG("freeing partially received PDU");
1352 icl_pdu_free(ic->ic_receive_pdu);
1353 ic->ic_receive_pdu = NULL;
1357 * Remove any outstanding PDUs from the send queue.
1359 while (!STAILQ_EMPTY(&ic->ic_to_send)) {
1360 pdu = STAILQ_FIRST(&ic->ic_to_send);
1361 STAILQ_REMOVE_HEAD(&ic->ic_to_send, ip_next);
1365 KASSERT(STAILQ_EMPTY(&ic->ic_to_send),
1366 ("destroying session with non-empty send queue"));
1368 KASSERT(ic->ic_outstanding_pdus == 0,
1369 ("destroying session with %d outstanding PDUs",
1370 ic->ic_outstanding_pdus));
1372 ICL_CONN_UNLOCK(ic);
1376 icl_conn_connected(struct icl_conn *ic)
1378 ICL_CONN_LOCK_ASSERT_NOT(ic);
1381 if (ic->ic_socket == NULL) {
1382 ICL_CONN_UNLOCK(ic);
1385 if (ic->ic_socket->so_error != 0) {
1386 ICL_CONN_UNLOCK(ic);
1389 ICL_CONN_UNLOCK(ic);
1393 #ifdef ICL_KERNEL_PROXY
1395 icl_conn_handoff_sock(struct icl_conn *ic, struct socket *so)
1399 ICL_CONN_LOCK_ASSERT_NOT(ic);
1401 if (so->so_type != SOCK_STREAM)
1405 if (ic->ic_socket != NULL) {
1406 ICL_CONN_UNLOCK(ic);
1410 ICL_CONN_UNLOCK(ic);
1412 error = icl_conn_start(ic);
1416 #endif /* ICL_KERNEL_PROXY */
1425 uma_zdestroy(icl_conn_zone);
1426 uma_zdestroy(icl_pdu_zone);
1435 icl_conn_zone = uma_zcreate("icl_conn",
1436 sizeof(struct icl_conn), NULL, NULL, NULL, NULL,
1438 icl_pdu_zone = uma_zcreate("icl_pdu",
1439 sizeof(struct icl_pdu), NULL, NULL, NULL, NULL,
1442 refcount_init(&icl_ncons, 0);
1446 icl_modevent(module_t mod, int what, void *arg)
1454 return (icl_unload());
1460 moduledata_t icl_data = {
1466 DECLARE_MODULE(icl, icl_data, SI_SUB_DRIVERS, SI_ORDER_FIRST);
1467 MODULE_VERSION(icl, 1);