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/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
55 #include <netinet/in.h>
56 #include <netinet/tcp.h>
59 #include "iscsi_proto.h"
61 SYSCTL_NODE(_kern, OID_AUTO, icl, CTLFLAG_RD, 0, "iSCSI Common Layer");
63 TUNABLE_INT("kern.icl.debug", &debug);
64 SYSCTL_INT(_kern_icl, OID_AUTO, debug, CTLFLAG_RW,
65 &debug, 1, "Enable debug messages");
66 static int partial_receive_len = 1 * 1024; /* XXX: More? */
67 TUNABLE_INT("kern.icl.partial_receive_len", &partial_receive_len);
68 SYSCTL_INT(_kern_icl, OID_AUTO, partial_receive_len, CTLFLAG_RW,
69 &partial_receive_len, 1 * 1024, "Minimum read size for partially received "
72 static uma_zone_t icl_conn_zone;
73 static uma_zone_t icl_pdu_zone;
75 static volatile u_int icl_ncons;
77 #define ICL_DEBUG(X, ...) \
79 printf("%s: " X "\n", __func__, ## __VA_ARGS__);\
82 #define ICL_WARN(X, ...) \
84 printf("WARNING: %s: " X "\n", \
85 __func__, ## __VA_ARGS__); \
88 #define ICL_CONN_LOCK(X) mtx_lock(&X->ic_lock)
89 #define ICL_CONN_UNLOCK(X) mtx_unlock(&X->ic_lock)
90 #define ICL_CONN_LOCK_ASSERT(X) mtx_assert(&X->ic_lock, MA_OWNED)
93 icl_conn_fail(struct icl_conn *ic)
95 if (ic->ic_socket == NULL)
101 ic->ic_socket->so_error = EDOOFUS;
106 icl_conn_receive(struct icl_conn *ic, size_t len)
115 memset(&uio, 0, sizeof(uio));
118 flags = MSG_DONTWAIT;
119 error = soreceive(so, NULL, &uio, &m, NULL, &flags);
121 ICL_DEBUG("soreceive error %d", error);
124 if (uio.uio_resid != 0) {
126 ICL_DEBUG("short read");
133 static struct icl_pdu *
134 icl_pdu_new(struct icl_conn *ic, int flags)
138 refcount_acquire(&ic->ic_outstanding_pdus);
139 ip = uma_zalloc(icl_pdu_zone, flags | M_ZERO);
141 ICL_WARN("failed to allocate %zd bytes", sizeof(*ip));
142 refcount_release(&ic->ic_outstanding_pdus);
152 icl_pdu_free(struct icl_pdu *ip)
158 m_freem(ip->ip_bhs_mbuf);
159 m_freem(ip->ip_ahs_mbuf);
160 m_freem(ip->ip_data_mbuf);
161 uma_zfree(icl_pdu_zone, ip);
162 refcount_release(&ic->ic_outstanding_pdus);
166 * Allocate icl_pdu with empty BHS to fill up by the caller.
169 icl_pdu_new_bhs(struct icl_conn *ic, int flags)
173 ip = icl_pdu_new(ic, flags);
177 ip->ip_bhs_mbuf = m_getm2(NULL, sizeof(struct iscsi_bhs),
178 flags, MT_DATA, M_PKTHDR);
179 if (ip->ip_bhs_mbuf == NULL) {
180 ICL_WARN("failed to allocate %zd bytes", sizeof(*ip));
184 ip->ip_bhs = mtod(ip->ip_bhs_mbuf, struct iscsi_bhs *);
185 memset(ip->ip_bhs, 0, sizeof(struct iscsi_bhs));
186 ip->ip_bhs_mbuf->m_len = sizeof(struct iscsi_bhs);
192 icl_pdu_ahs_length(const struct icl_pdu *request)
195 return (request->ip_bhs->bhs_total_ahs_len * 4);
199 icl_pdu_data_segment_length(const struct icl_pdu *request)
203 len += request->ip_bhs->bhs_data_segment_len[0];
205 len += request->ip_bhs->bhs_data_segment_len[1];
207 len += request->ip_bhs->bhs_data_segment_len[2];
213 icl_pdu_set_data_segment_length(struct icl_pdu *response, uint32_t len)
216 response->ip_bhs->bhs_data_segment_len[2] = len;
217 response->ip_bhs->bhs_data_segment_len[1] = len >> 8;
218 response->ip_bhs->bhs_data_segment_len[0] = len >> 16;
222 icl_pdu_padding(const struct icl_pdu *ip)
225 if ((ip->ip_data_len % 4) != 0)
226 return (4 - (ip->ip_data_len % 4));
232 icl_pdu_size(const struct icl_pdu *response)
236 KASSERT(response->ip_ahs_len == 0, ("responding with AHS"));
238 len = sizeof(struct iscsi_bhs) + response->ip_data_len +
239 icl_pdu_padding(response);
240 if (response->ip_conn->ic_header_crc32c)
241 len += ISCSI_HEADER_DIGEST_SIZE;
242 if (response->ip_data_len != 0 && response->ip_conn->ic_data_crc32c)
243 len += ISCSI_DATA_DIGEST_SIZE;
249 icl_pdu_receive_bhs(struct icl_pdu *request, size_t *availablep)
253 m = icl_conn_receive(request->ip_conn, sizeof(struct iscsi_bhs));
255 ICL_DEBUG("failed to receive BHS");
259 request->ip_bhs_mbuf = m_pullup(m, sizeof(struct iscsi_bhs));
260 if (request->ip_bhs_mbuf == NULL) {
261 ICL_WARN("m_pullup failed");
264 request->ip_bhs = mtod(request->ip_bhs_mbuf, struct iscsi_bhs *);
267 * XXX: For architectures with strict alignment requirements
268 * we may need to allocate ip_bhs and copy the data into it.
269 * For some reason, though, not doing this doesn't seem
270 * to cause problems; tested on sparc64.
273 *availablep -= sizeof(struct iscsi_bhs);
278 icl_pdu_receive_ahs(struct icl_pdu *request, size_t *availablep)
281 request->ip_ahs_len = icl_pdu_ahs_length(request);
282 if (request->ip_ahs_len == 0)
285 request->ip_ahs_mbuf = icl_conn_receive(request->ip_conn,
286 request->ip_ahs_len);
287 if (request->ip_ahs_mbuf == NULL) {
288 ICL_DEBUG("failed to receive AHS");
292 *availablep -= request->ip_ahs_len;
297 icl_mbuf_to_crc32c(const struct mbuf *m0)
299 uint32_t digest = 0xffffffff;
300 const struct mbuf *m;
302 for (m = m0; m != NULL; m = m->m_next)
303 digest = calculate_crc32c(digest,
304 mtod(m, const void *), m->m_len);
306 digest = digest ^ 0xffffffff;
312 icl_pdu_check_header_digest(struct icl_pdu *request, size_t *availablep)
315 uint32_t received_digest, valid_digest;
317 if (request->ip_conn->ic_header_crc32c == false)
320 m = icl_conn_receive(request->ip_conn, ISCSI_HEADER_DIGEST_SIZE);
322 ICL_DEBUG("failed to receive header digest");
326 CTASSERT(sizeof(received_digest) == ISCSI_HEADER_DIGEST_SIZE);
327 m_copydata(m, 0, ISCSI_HEADER_DIGEST_SIZE, (void *)&received_digest);
330 *availablep -= ISCSI_HEADER_DIGEST_SIZE;
335 valid_digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
336 if (received_digest != valid_digest) {
337 ICL_WARN("header digest check failed; got 0x%x, "
338 "should be 0x%x", received_digest, valid_digest);
346 * Return the number of bytes that should be waiting in the receive socket
347 * before icl_pdu_receive_data_segment() gets called.
350 icl_pdu_data_segment_receive_len(const struct icl_pdu *request)
354 len = icl_pdu_data_segment_length(request);
359 * Account for the parts of data segment already read from
362 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
363 len -= request->ip_data_len;
366 * Don't always wait for the full data segment to be delivered
367 * to the socket; this might badly affect performance due to
368 * TCP window scaling.
370 if (len > partial_receive_len) {
372 ICL_DEBUG("need %zd bytes of data, limiting to %zd",
373 len, partial_receive_len));
375 len = partial_receive_len;
381 * Account for padding. Note that due to the way code is written,
382 * the icl_pdu_receive_data_segment() must always receive padding
383 * along with the last part of data segment, because it would be
384 * impossible to tell whether we've already received the full data
385 * segment including padding, or without it.
388 len += 4 - (len % 4);
391 ICL_DEBUG("need %zd bytes of data", len));
398 icl_pdu_receive_data_segment(struct icl_pdu *request,
399 size_t *availablep, bool *more_neededp)
402 size_t len, padding = 0;
405 ic = request->ip_conn;
407 *more_neededp = false;
408 ic->ic_receive_len = 0;
410 len = icl_pdu_data_segment_length(request);
415 padding = 4 - (len % 4);
418 * Account for already received parts of data segment.
420 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
421 len -= request->ip_data_len;
423 if (len + padding > *availablep) {
425 * Not enough data in the socket buffer. Receive as much
426 * as we can. Don't receive padding, since, obviously, it's
427 * not the end of data segment yet.
430 ICL_DEBUG("limited from %zd to %zd",
431 len + padding, *availablep - padding));
433 len = *availablep - padding;
434 *more_neededp = true;
439 * Must not try to receive padding without at least one byte
440 * of actual data segment.
443 m = icl_conn_receive(request->ip_conn, len + padding);
445 ICL_DEBUG("failed to receive data segment");
449 if (request->ip_data_mbuf == NULL)
450 request->ip_data_mbuf = m;
452 m_cat(request->ip_data_mbuf, m);
454 request->ip_data_len += len;
455 *availablep -= len + padding;
461 icl_pdu_data_segment_receive_len(request);
467 icl_pdu_check_data_digest(struct icl_pdu *request, size_t *availablep)
470 uint32_t received_digest, valid_digest;
472 if (request->ip_conn->ic_data_crc32c == false)
475 if (request->ip_data_len == 0)
478 m = icl_conn_receive(request->ip_conn, ISCSI_DATA_DIGEST_SIZE);
480 ICL_DEBUG("failed to receive data digest");
484 CTASSERT(sizeof(received_digest) == ISCSI_DATA_DIGEST_SIZE);
485 m_copydata(m, 0, ISCSI_DATA_DIGEST_SIZE, (void *)&received_digest);
488 *availablep -= ISCSI_DATA_DIGEST_SIZE;
491 * Note that ip_data_mbuf also contains padding; since digest
492 * calculation is supposed to include that, we iterate over
493 * the entire ip_data_mbuf chain, not just ip_data_len bytes of it.
495 valid_digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
496 if (received_digest != valid_digest) {
497 ICL_WARN("data digest check failed; got 0x%x, "
498 "should be 0x%x", received_digest, valid_digest);
506 * Somewhat contrary to the name, this attempts to receive only one
507 * "part" of PDU at a time; call it repeatedly until it returns non-NULL.
509 static struct icl_pdu *
510 icl_conn_receive_pdu(struct icl_conn *ic, size_t *availablep)
512 struct icl_pdu *request;
520 if (ic->ic_receive_state == ICL_CONN_STATE_BHS) {
521 KASSERT(ic->ic_receive_pdu == NULL,
522 ("ic->ic_receive_pdu != NULL"));
523 request = icl_pdu_new(ic, M_NOWAIT);
524 if (request == NULL) {
525 ICL_DEBUG("failed to allocate PDU; "
526 "dropping connection");
530 ic->ic_receive_pdu = request;
532 KASSERT(ic->ic_receive_pdu != NULL,
533 ("ic->ic_receive_pdu == NULL"));
534 request = ic->ic_receive_pdu;
537 if (*availablep < ic->ic_receive_len) {
539 ICL_DEBUG("not enough data; need %zd, "
540 "have %zd", ic->ic_receive_len, *availablep);
545 switch (ic->ic_receive_state) {
546 case ICL_CONN_STATE_BHS:
547 //ICL_DEBUG("receiving BHS");
548 error = icl_pdu_receive_bhs(request, availablep);
550 ICL_DEBUG("failed to receive BHS; "
551 "dropping connection");
556 * We don't enforce any limit for AHS length;
557 * its length is stored in 8 bit field.
560 len = icl_pdu_data_segment_length(request);
561 if (len > ic->ic_max_data_segment_length) {
562 ICL_WARN("received data segment "
563 "length %zd is larger than negotiated "
564 "MaxDataSegmentLength %zd; "
565 "dropping connection",
566 len, ic->ic_max_data_segment_length);
571 ic->ic_receive_state = ICL_CONN_STATE_AHS;
572 ic->ic_receive_len = icl_pdu_ahs_length(request);
575 case ICL_CONN_STATE_AHS:
576 //ICL_DEBUG("receiving AHS");
577 error = icl_pdu_receive_ahs(request, availablep);
579 ICL_DEBUG("failed to receive AHS; "
580 "dropping connection");
583 ic->ic_receive_state = ICL_CONN_STATE_HEADER_DIGEST;
584 if (ic->ic_header_crc32c == false)
585 ic->ic_receive_len = 0;
587 ic->ic_receive_len = ISCSI_HEADER_DIGEST_SIZE;
590 case ICL_CONN_STATE_HEADER_DIGEST:
591 //ICL_DEBUG("receiving header digest");
592 error = icl_pdu_check_header_digest(request, availablep);
594 ICL_DEBUG("header digest failed; "
595 "dropping connection");
599 ic->ic_receive_state = ICL_CONN_STATE_DATA;
601 icl_pdu_data_segment_receive_len(request);
604 case ICL_CONN_STATE_DATA:
605 //ICL_DEBUG("receiving data segment");
606 error = icl_pdu_receive_data_segment(request, availablep,
609 ICL_DEBUG("failed to receive data segment;"
610 "dropping connection");
617 ic->ic_receive_state = ICL_CONN_STATE_DATA_DIGEST;
618 if (request->ip_data_len == 0 || ic->ic_data_crc32c == false)
619 ic->ic_receive_len = 0;
621 ic->ic_receive_len = ISCSI_DATA_DIGEST_SIZE;
624 case ICL_CONN_STATE_DATA_DIGEST:
625 //ICL_DEBUG("receiving data digest");
626 error = icl_pdu_check_data_digest(request, availablep);
628 ICL_DEBUG("data digest failed; "
629 "dropping connection");
634 * We've received complete PDU; reset the receive state machine
635 * and return the PDU.
637 ic->ic_receive_state = ICL_CONN_STATE_BHS;
638 ic->ic_receive_len = sizeof(struct iscsi_bhs);
639 ic->ic_receive_pdu = NULL;
643 panic("invalid ic_receive_state %d\n", ic->ic_receive_state);
647 icl_pdu_free(request);
655 icl_conn_receive_pdus(struct icl_conn *ic, size_t available)
657 struct icl_pdu *response;
663 * This can never happen; we're careful to only mess with ic->ic_socket
664 * pointer when the send/receive threads are not running.
666 KASSERT(so != NULL, ("NULL socket"));
669 if (ic->ic_disconnecting)
672 if (so->so_error != 0) {
673 ICL_DEBUG("connection error %d; "
674 "dropping connection", so->so_error);
680 * Loop until we have a complete PDU or there is not enough
681 * data in the socket buffer.
683 if (available < ic->ic_receive_len) {
685 ICL_DEBUG("not enough data; have %zd, "
686 "need %zd", available,
692 response = icl_conn_receive_pdu(ic, &available);
693 if (response == NULL)
696 if (response->ip_ahs_len > 0) {
697 ICL_WARN("received PDU with unsupported "
698 "AHS; opcode 0x%x; dropping connection",
699 response->ip_bhs->bhs_opcode);
700 icl_pdu_free(response);
705 (ic->ic_receive)(response);
710 icl_receive_thread(void *arg)
720 ic->ic_receive_running = true;
724 if (ic->ic_disconnecting) {
725 //ICL_DEBUG("terminating");
729 SOCKBUF_LOCK(&so->so_rcv);
730 available = so->so_rcv.sb_cc;
731 if (available < ic->ic_receive_len) {
732 so->so_rcv.sb_lowat = ic->ic_receive_len;
733 cv_wait(&ic->ic_receive_cv, &so->so_rcv.sb_mtx);
735 SOCKBUF_UNLOCK(&so->so_rcv);
737 icl_conn_receive_pdus(ic, available);
741 ic->ic_receive_running = false;
747 icl_soupcall_receive(struct socket *so, void *arg, int waitflag)
752 cv_signal(&ic->ic_receive_cv);
757 icl_pdu_send(struct icl_pdu *request)
759 size_t padding, pdu_len;
760 uint32_t digest, zero = 0;
765 ic = request->ip_conn;
766 so = request->ip_conn->ic_socket;
768 ICL_CONN_LOCK_ASSERT(ic);
770 icl_pdu_set_data_segment_length(request, request->ip_data_len);
772 pdu_len = icl_pdu_size(request);
774 if (ic->ic_header_crc32c) {
775 digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
776 ok = m_append(request->ip_bhs_mbuf, sizeof(digest),
779 ICL_WARN("failed to append header digest");
784 if (request->ip_data_len != 0) {
785 padding = icl_pdu_padding(request);
787 ok = m_append(request->ip_data_mbuf, padding,
790 ICL_WARN("failed to append padding");
795 if (ic->ic_data_crc32c) {
796 digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
798 ok = m_append(request->ip_data_mbuf, sizeof(digest),
801 ICL_WARN("failed to append header digest");
806 m_cat(request->ip_bhs_mbuf, request->ip_data_mbuf);
807 request->ip_data_mbuf = NULL;
810 request->ip_bhs_mbuf->m_pkthdr.len = pdu_len;
812 error = sosend(so, NULL, NULL, request->ip_bhs_mbuf,
813 NULL, MSG_DONTWAIT, curthread);
814 request->ip_bhs_mbuf = NULL; /* Sosend consumes the mbuf. */
816 ICL_DEBUG("sosend error %d", error);
824 icl_conn_send_pdus(struct icl_conn *ic)
826 struct icl_pdu *request;
828 size_t available, size;
831 ICL_CONN_LOCK_ASSERT(ic);
835 SOCKBUF_LOCK(&so->so_snd);
836 available = sbspace(&so->so_snd);
837 SOCKBUF_UNLOCK(&so->so_snd);
839 while (!TAILQ_EMPTY(&ic->ic_to_send)) {
840 if (ic->ic_disconnecting)
843 request = TAILQ_FIRST(&ic->ic_to_send);
844 size = icl_pdu_size(request);
845 if (available < size) {
847 * Set the low watermark on the socket,
848 * to avoid waking up until there is enough
851 SOCKBUF_LOCK(&so->so_snd);
852 so->so_snd.sb_lowat = size;
853 SOCKBUF_UNLOCK(&so->so_snd);
855 ICL_DEBUG("no space to send; "
856 "have %zd, need %zd",
862 TAILQ_REMOVE(&ic->ic_to_send, request, ip_next);
863 error = icl_pdu_send(request);
865 ICL_DEBUG("failed to send PDU; "
866 "dropping connection");
870 icl_pdu_free(request);
875 icl_send_thread(void *arg)
882 ic->ic_send_running = true;
885 if (ic->ic_disconnecting) {
886 //ICL_DEBUG("terminating");
889 icl_conn_send_pdus(ic);
890 cv_wait(&ic->ic_send_cv, &ic->ic_lock);
893 ic->ic_send_running = false;
899 icl_soupcall_send(struct socket *so, void *arg, int waitflag)
904 cv_signal(&ic->ic_send_cv);
909 icl_pdu_append_data(struct icl_pdu *request, const void *addr, size_t len, int flags)
911 struct mbuf *mb, *newmb;
912 size_t copylen, off = 0;
914 KASSERT(len > 0, ("len == 0"));
916 newmb = m_getm2(NULL, len, flags, MT_DATA, M_PKTHDR);
918 ICL_WARN("failed to allocate mbuf for %zd bytes", len);
922 for (mb = newmb; mb != NULL; mb = mb->m_next) {
923 copylen = min(M_TRAILINGSPACE(mb), len - off);
924 memcpy(mtod(mb, char *), (const char *)addr + off, copylen);
928 KASSERT(off == len, ("%s: off != len", __func__));
930 if (request->ip_data_mbuf == NULL) {
931 request->ip_data_mbuf = newmb;
932 request->ip_data_len = len;
934 m_cat(request->ip_data_mbuf, newmb);
935 request->ip_data_len += len;
942 icl_pdu_get_data(struct icl_pdu *ip, size_t off, void *addr, size_t len)
945 m_copydata(ip->ip_data_mbuf, off, len, addr);
949 icl_pdu_queue(struct icl_pdu *ip)
956 if (ic->ic_disconnecting || ic->ic_socket == NULL) {
957 ICL_DEBUG("icl_pdu_queue on closed connection");
962 TAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next);
964 cv_signal(&ic->ic_send_cv);
972 refcount_acquire(&icl_ncons);
974 ic = uma_zalloc(icl_conn_zone, M_WAITOK | M_ZERO);
976 TAILQ_INIT(&ic->ic_to_send);
977 mtx_init(&ic->ic_lock, "icl_lock", NULL, MTX_DEF);
978 cv_init(&ic->ic_send_cv, "icl_tx");
979 cv_init(&ic->ic_receive_cv, "icl_rx");
980 refcount_init(&ic->ic_outstanding_pdus, 0);
981 ic->ic_max_data_segment_length = ICL_MAX_DATA_SEGMENT_LENGTH;
987 icl_conn_free(struct icl_conn *ic)
990 mtx_destroy(&ic->ic_lock);
991 cv_destroy(&ic->ic_send_cv);
992 cv_destroy(&ic->ic_receive_cv);
993 uma_zfree(icl_conn_zone, ic);
994 refcount_release(&icl_ncons);
998 icl_conn_start(struct icl_conn *ic)
1009 if (ic->ic_socket == NULL) {
1010 ICL_CONN_UNLOCK(ic);
1014 ic->ic_receive_state = ICL_CONN_STATE_BHS;
1015 ic->ic_receive_len = sizeof(struct iscsi_bhs);
1016 ic->ic_disconnecting = false;
1018 ICL_CONN_UNLOCK(ic);
1021 * Use max available sockbuf size for sending. Do it manually
1022 * instead of sbreserve(9) to work around resource limits.
1024 * XXX: This kind of sucks. On one hand, we don't currently support
1025 * sending a part of data segment; we always do it in one piece,
1026 * so we have to make sure it can fit in the socket buffer.
1027 * Once I've implemented partial send, we'll get rid of this
1028 * and use autoscaling.
1030 bufsize = (sizeof(struct iscsi_bhs) +
1031 ic->ic_max_data_segment_length) * 8;
1032 error = soreserve(ic->ic_socket, bufsize, bufsize);
1034 ICL_WARN("soreserve failed with error %d", error);
1042 bzero(&opt, sizeof(opt));
1043 opt.sopt_dir = SOPT_SET;
1044 opt.sopt_level = IPPROTO_TCP;
1045 opt.sopt_name = TCP_NODELAY;
1046 opt.sopt_val = &one;
1047 opt.sopt_valsize = sizeof(one);
1048 error = sosetopt(ic->ic_socket, &opt);
1050 ICL_WARN("disabling TCP_NODELAY failed with error %d", error);
1058 error = kthread_add(icl_send_thread, ic, NULL, NULL, 0, 0, "icltx");
1060 ICL_WARN("kthread_add(9) failed with error %d", error);
1065 error = kthread_add(icl_receive_thread, ic, NULL, NULL, 0, 0, "iclrx");
1067 ICL_WARN("kthread_add(9) failed with error %d", error);
1073 * Register socket upcall, to get notified about incoming PDUs
1074 * and free space to send outgoing ones.
1076 SOCKBUF_LOCK(&ic->ic_socket->so_snd);
1077 soupcall_set(ic->ic_socket, SO_SND, icl_soupcall_send, ic);
1078 SOCKBUF_UNLOCK(&ic->ic_socket->so_snd);
1079 SOCKBUF_LOCK(&ic->ic_socket->so_rcv);
1080 soupcall_set(ic->ic_socket, SO_RCV, icl_soupcall_receive, ic);
1081 SOCKBUF_UNLOCK(&ic->ic_socket->so_rcv);
1087 icl_conn_handoff(struct icl_conn *ic, int fd)
1091 cap_rights_t rights;
1095 * Steal the socket from userland.
1097 error = fget(curthread, fd,
1098 cap_rights_init(&rights, CAP_SOCK_CLIENT), &fp);
1101 if (fp->f_type != DTYPE_SOCKET) {
1102 fdrop(fp, curthread);
1106 if (so->so_type != SOCK_STREAM) {
1107 fdrop(fp, curthread);
1113 if (ic->ic_socket != NULL) {
1114 ICL_CONN_UNLOCK(ic);
1115 fdrop(fp, curthread);
1119 ic->ic_socket = fp->f_data;
1120 fp->f_ops = &badfileops;
1122 fdrop(fp, curthread);
1123 ICL_CONN_UNLOCK(ic);
1125 error = icl_conn_start(ic);
1131 icl_conn_shutdown(struct icl_conn *ic)
1135 if (ic->ic_socket == NULL) {
1136 ICL_CONN_UNLOCK(ic);
1139 ICL_CONN_UNLOCK(ic);
1141 soshutdown(ic->ic_socket, SHUT_RDWR);
1145 icl_conn_close(struct icl_conn *ic)
1147 struct icl_pdu *pdu;
1150 if (ic->ic_socket == NULL) {
1151 ICL_CONN_UNLOCK(ic);
1155 ic->ic_disconnecting = true;
1158 * Wake up the threads, so they can properly terminate.
1160 cv_signal(&ic->ic_receive_cv);
1161 cv_signal(&ic->ic_send_cv);
1162 while (ic->ic_receive_running || ic->ic_send_running) {
1163 //ICL_DEBUG("waiting for send/receive threads to terminate");
1164 ICL_CONN_UNLOCK(ic);
1165 cv_signal(&ic->ic_receive_cv);
1166 cv_signal(&ic->ic_send_cv);
1167 pause("icl_close", 1 * hz);
1170 //ICL_DEBUG("send/receive threads terminated");
1172 soclose(ic->ic_socket);
1173 ic->ic_socket = NULL;
1175 if (ic->ic_receive_pdu != NULL) {
1176 //ICL_DEBUG("freeing partially received PDU");
1177 icl_pdu_free(ic->ic_receive_pdu);
1178 ic->ic_receive_pdu = NULL;
1182 * Remove any outstanding PDUs from the send queue.
1184 while (!TAILQ_EMPTY(&ic->ic_to_send)) {
1185 pdu = TAILQ_FIRST(&ic->ic_to_send);
1186 TAILQ_REMOVE(&ic->ic_to_send, pdu, ip_next);
1190 KASSERT(TAILQ_EMPTY(&ic->ic_to_send),
1191 ("destroying session with non-empty send queue"));
1196 KASSERT(ic->ic_outstanding_pdus == 0,
1197 ("destroying session with %d outstanding PDUs",
1198 ic->ic_outstanding_pdus));
1200 ICL_CONN_UNLOCK(ic);
1204 icl_conn_connected(struct icl_conn *ic)
1208 if (ic->ic_socket == NULL) {
1209 ICL_CONN_UNLOCK(ic);
1212 if (ic->ic_socket->so_error != 0) {
1213 ICL_CONN_UNLOCK(ic);
1216 ICL_CONN_UNLOCK(ic);
1220 #ifdef ICL_KERNEL_PROXY
1222 icl_conn_handoff_sock(struct icl_conn *ic, struct socket *so)
1226 if (so->so_type != SOCK_STREAM)
1230 if (ic->ic_socket != NULL) {
1231 ICL_CONN_UNLOCK(ic);
1235 ICL_CONN_UNLOCK(ic);
1237 error = icl_conn_start(ic);
1241 #endif /* ICL_KERNEL_PROXY */
1250 uma_zdestroy(icl_conn_zone);
1251 uma_zdestroy(icl_pdu_zone);
1260 icl_conn_zone = uma_zcreate("icl_conn",
1261 sizeof(struct icl_conn), NULL, NULL, NULL, NULL,
1263 icl_pdu_zone = uma_zcreate("icl_pdu",
1264 sizeof(struct icl_pdu), NULL, NULL, NULL, NULL,
1267 refcount_init(&icl_ncons, 0);
1271 icl_modevent(module_t mod, int what, void *arg)
1279 return (icl_unload());
1285 moduledata_t icl_data = {
1291 DECLARE_MODULE(icl, icl_data, SI_SUB_DRIVERS, SI_ORDER_FIRST);
1292 MODULE_VERSION(icl, 1);