2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 The FreeBSD Foundation
6 * This software was developed by Edward Tomasz Napierala under sponsorship
7 * from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Software implementation of iSCSI Common Layer kobj(9) interface.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/capsicum.h>
41 #include <sys/condvar.h>
43 #include <sys/gsb_crc32.h>
45 #include <sys/kernel.h>
46 #include <sys/kthread.h>
49 #include <sys/mutex.h>
50 #include <sys/module.h>
51 #include <sys/protosw.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/systm.h>
59 #include <netinet/in.h>
60 #include <netinet/tcp.h>
62 #include <dev/iscsi/icl.h>
63 #include <dev/iscsi/iscsi_proto.h>
64 #include <icl_conn_if.h>
69 /* soft specific stuff goes here. */
75 SYSCTL_NODE(_kern_icl, OID_AUTO, soft, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
77 static int coalesce = 1;
78 SYSCTL_INT(_kern_icl_soft, OID_AUTO, coalesce, CTLFLAG_RWTUN,
79 &coalesce, 0, "Try to coalesce PDUs before sending");
80 static int partial_receive_len = 256 * 1024;
81 SYSCTL_INT(_kern_icl_soft, OID_AUTO, partial_receive_len, CTLFLAG_RWTUN,
82 &partial_receive_len, 0, "Minimum read size for partially received "
84 static int max_data_segment_length = 256 * 1024;
85 SYSCTL_INT(_kern_icl_soft, OID_AUTO, max_data_segment_length, CTLFLAG_RWTUN,
86 &max_data_segment_length, 0, "Maximum data segment length");
87 static int first_burst_length = 1024 * 1024;
88 SYSCTL_INT(_kern_icl_soft, OID_AUTO, first_burst_length, CTLFLAG_RWTUN,
89 &first_burst_length, 0, "First burst length");
90 static int max_burst_length = 1024 * 1024;
91 SYSCTL_INT(_kern_icl_soft, OID_AUTO, max_burst_length, CTLFLAG_RWTUN,
92 &max_burst_length, 0, "Maximum burst length");
93 static int sendspace = 1536 * 1024;
94 SYSCTL_INT(_kern_icl_soft, OID_AUTO, sendspace, CTLFLAG_RWTUN,
95 &sendspace, 0, "Default send socket buffer size");
96 static int recvspace = 1536 * 1024;
97 SYSCTL_INT(_kern_icl_soft, OID_AUTO, recvspace, CTLFLAG_RWTUN,
98 &recvspace, 0, "Default receive socket buffer size");
100 static MALLOC_DEFINE(M_ICL_SOFT, "icl_soft", "iSCSI software backend");
101 static uma_zone_t icl_soft_pdu_zone;
103 static volatile u_int icl_ncons;
105 #define ICL_CONN_LOCK(X) mtx_lock(X->ic_lock)
106 #define ICL_CONN_UNLOCK(X) mtx_unlock(X->ic_lock)
107 #define ICL_CONN_LOCK_ASSERT(X) mtx_assert(X->ic_lock, MA_OWNED)
108 #define ICL_CONN_LOCK_ASSERT_NOT(X) mtx_assert(X->ic_lock, MA_NOTOWNED)
110 STAILQ_HEAD(icl_pdu_stailq, icl_pdu);
112 static icl_conn_new_pdu_t icl_soft_conn_new_pdu;
113 static icl_conn_pdu_free_t icl_soft_conn_pdu_free;
114 static icl_conn_pdu_data_segment_length_t
115 icl_soft_conn_pdu_data_segment_length;
116 static icl_conn_pdu_append_data_t icl_soft_conn_pdu_append_data;
117 static icl_conn_pdu_get_data_t icl_soft_conn_pdu_get_data;
118 static icl_conn_pdu_queue_t icl_soft_conn_pdu_queue;
119 static icl_conn_pdu_queue_cb_t icl_soft_conn_pdu_queue_cb;
120 static icl_conn_handoff_t icl_soft_conn_handoff;
121 static icl_conn_free_t icl_soft_conn_free;
122 static icl_conn_close_t icl_soft_conn_close;
123 static icl_conn_task_setup_t icl_soft_conn_task_setup;
124 static icl_conn_task_done_t icl_soft_conn_task_done;
125 static icl_conn_transfer_setup_t icl_soft_conn_transfer_setup;
126 static icl_conn_transfer_done_t icl_soft_conn_transfer_done;
127 #ifdef ICL_KERNEL_PROXY
128 static icl_conn_connect_t icl_soft_conn_connect;
131 static kobj_method_t icl_soft_methods[] = {
132 KOBJMETHOD(icl_conn_new_pdu, icl_soft_conn_new_pdu),
133 KOBJMETHOD(icl_conn_pdu_free, icl_soft_conn_pdu_free),
134 KOBJMETHOD(icl_conn_pdu_data_segment_length,
135 icl_soft_conn_pdu_data_segment_length),
136 KOBJMETHOD(icl_conn_pdu_append_data, icl_soft_conn_pdu_append_data),
137 KOBJMETHOD(icl_conn_pdu_get_data, icl_soft_conn_pdu_get_data),
138 KOBJMETHOD(icl_conn_pdu_queue, icl_soft_conn_pdu_queue),
139 KOBJMETHOD(icl_conn_pdu_queue_cb, icl_soft_conn_pdu_queue_cb),
140 KOBJMETHOD(icl_conn_handoff, icl_soft_conn_handoff),
141 KOBJMETHOD(icl_conn_free, icl_soft_conn_free),
142 KOBJMETHOD(icl_conn_close, icl_soft_conn_close),
143 KOBJMETHOD(icl_conn_task_setup, icl_soft_conn_task_setup),
144 KOBJMETHOD(icl_conn_task_done, icl_soft_conn_task_done),
145 KOBJMETHOD(icl_conn_transfer_setup, icl_soft_conn_transfer_setup),
146 KOBJMETHOD(icl_conn_transfer_done, icl_soft_conn_transfer_done),
147 #ifdef ICL_KERNEL_PROXY
148 KOBJMETHOD(icl_conn_connect, icl_soft_conn_connect),
153 DEFINE_CLASS(icl_soft, icl_soft_methods, sizeof(struct icl_conn));
156 icl_conn_fail(struct icl_conn *ic)
158 if (ic->ic_socket == NULL)
164 ic->ic_socket->so_error = EDOOFUS;
169 icl_soft_conn_pdu_free(struct icl_conn *ic, struct icl_pdu *ip)
171 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip;
173 KASSERT(isp->ref_cnt == 0, ("freeing active PDU"));
174 m_freem(ip->ip_bhs_mbuf);
175 m_freem(ip->ip_ahs_mbuf);
176 m_freem(ip->ip_data_mbuf);
177 uma_zfree(icl_soft_pdu_zone, isp);
179 refcount_release(&ic->ic_outstanding_pdus);
184 icl_soft_pdu_call_cb(struct icl_pdu *ip)
186 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip;
189 isp->cb(ip, isp->error);
191 refcount_release(&ip->ip_conn->ic_outstanding_pdus);
193 uma_zfree(icl_soft_pdu_zone, isp);
197 icl_soft_pdu_done(struct icl_pdu *ip, int error)
199 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip;
204 m_freem(ip->ip_bhs_mbuf);
205 ip->ip_bhs_mbuf = NULL;
206 m_freem(ip->ip_ahs_mbuf);
207 ip->ip_ahs_mbuf = NULL;
208 m_freem(ip->ip_data_mbuf);
209 ip->ip_data_mbuf = NULL;
211 if (atomic_fetchadd_int(&isp->ref_cnt, -1) == 1)
212 icl_soft_pdu_call_cb(ip);
216 icl_soft_mbuf_done(struct mbuf *mb)
218 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)mb->m_ext.ext_arg1;
220 icl_soft_pdu_call_cb(&isp->ip);
224 * Allocate icl_pdu with empty BHS to fill up by the caller.
227 icl_soft_conn_new_pdu(struct icl_conn *ic, int flags)
229 struct icl_soft_pdu *isp;
233 refcount_acquire(&ic->ic_outstanding_pdus);
235 isp = uma_zalloc(icl_soft_pdu_zone, flags | M_ZERO);
237 ICL_WARN("failed to allocate soft PDU");
239 refcount_release(&ic->ic_outstanding_pdus);
246 CTASSERT(sizeof(struct iscsi_bhs) <= MHLEN);
247 ip->ip_bhs_mbuf = m_gethdr(flags, MT_DATA);
248 if (ip->ip_bhs_mbuf == NULL) {
249 ICL_WARN("failed to allocate BHS mbuf");
250 icl_soft_conn_pdu_free(ic, ip);
253 ip->ip_bhs = mtod(ip->ip_bhs_mbuf, struct iscsi_bhs *);
254 memset(ip->ip_bhs, 0, sizeof(struct iscsi_bhs));
255 ip->ip_bhs_mbuf->m_len = sizeof(struct iscsi_bhs);
261 icl_pdu_ahs_length(const struct icl_pdu *request)
264 return (request->ip_bhs->bhs_total_ahs_len * 4);
268 icl_pdu_data_segment_length(const struct icl_pdu *request)
272 len += request->ip_bhs->bhs_data_segment_len[0];
274 len += request->ip_bhs->bhs_data_segment_len[1];
276 len += request->ip_bhs->bhs_data_segment_len[2];
282 icl_soft_conn_pdu_data_segment_length(struct icl_conn *ic,
283 const struct icl_pdu *request)
286 return (icl_pdu_data_segment_length(request));
290 icl_pdu_set_data_segment_length(struct icl_pdu *response, uint32_t len)
293 response->ip_bhs->bhs_data_segment_len[2] = len;
294 response->ip_bhs->bhs_data_segment_len[1] = len >> 8;
295 response->ip_bhs->bhs_data_segment_len[0] = len >> 16;
299 icl_pdu_padding(const struct icl_pdu *ip)
302 if ((ip->ip_data_len % 4) != 0)
303 return (4 - (ip->ip_data_len % 4));
309 icl_pdu_size(const struct icl_pdu *response)
313 KASSERT(response->ip_ahs_len == 0, ("responding with AHS"));
315 len = sizeof(struct iscsi_bhs) + response->ip_data_len +
316 icl_pdu_padding(response);
317 if (response->ip_conn->ic_header_crc32c)
318 len += ISCSI_HEADER_DIGEST_SIZE;
319 if (response->ip_data_len != 0 && response->ip_conn->ic_data_crc32c)
320 len += ISCSI_DATA_DIGEST_SIZE;
326 icl_soft_receive_buf(struct mbuf **r, size_t *rs, void *buf, size_t s)
329 m_copydata(*r, 0, s, buf);
331 while ((*r) != NULL && (*r)->m_len == 0)
337 icl_pdu_receive_ahs(struct icl_pdu *request, struct mbuf **r, size_t *rs)
340 request->ip_ahs_len = icl_pdu_ahs_length(request);
341 if (request->ip_ahs_len == 0)
344 request->ip_ahs_mbuf = *r;
345 *r = m_split(request->ip_ahs_mbuf, request->ip_ahs_len, M_WAITOK);
346 *rs -= request->ip_ahs_len;
350 icl_mbuf_to_crc32c(const struct mbuf *m0)
352 uint32_t digest = 0xffffffff;
353 const struct mbuf *m;
355 for (m = m0; m != NULL; m = m->m_next)
356 digest = calculate_crc32c(digest,
357 mtod(m, const void *), m->m_len);
359 digest = digest ^ 0xffffffff;
365 icl_pdu_check_header_digest(struct icl_pdu *request, struct mbuf **r, size_t *rs)
367 uint32_t received_digest, valid_digest;
369 if (request->ip_conn->ic_header_crc32c == false)
372 CTASSERT(sizeof(received_digest) == ISCSI_HEADER_DIGEST_SIZE);
373 icl_soft_receive_buf(r, rs, &received_digest, ISCSI_HEADER_DIGEST_SIZE);
375 /* Temporary attach AHS to BHS to calculate header digest. */
376 request->ip_bhs_mbuf->m_next = request->ip_ahs_mbuf;
377 valid_digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
378 request->ip_bhs_mbuf->m_next = NULL;
379 if (received_digest != valid_digest) {
380 ICL_WARN("header digest check failed; got 0x%x, "
381 "should be 0x%x", received_digest, valid_digest);
389 * Return the number of bytes that should be waiting in the receive socket
390 * before icl_pdu_receive_data_segment() gets called.
393 icl_pdu_data_segment_receive_len(const struct icl_pdu *request)
397 len = icl_pdu_data_segment_length(request);
402 * Account for the parts of data segment already read from
405 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
406 len -= request->ip_data_len;
409 * Don't always wait for the full data segment to be delivered
410 * to the socket; this might badly affect performance due to
411 * TCP window scaling.
413 if (len > partial_receive_len) {
415 ICL_DEBUG("need %zd bytes of data, limiting to %zd",
416 len, partial_receive_len));
418 len = partial_receive_len;
424 * Account for padding. Note that due to the way code is written,
425 * the icl_pdu_receive_data_segment() must always receive padding
426 * along with the last part of data segment, because it would be
427 * impossible to tell whether we've already received the full data
428 * segment including padding, or without it.
431 len += 4 - (len % 4);
434 ICL_DEBUG("need %zd bytes of data", len));
441 icl_pdu_receive_data_segment(struct icl_pdu *request, struct mbuf **r,
442 size_t *rs, bool *more_neededp)
445 size_t len, padding = 0;
448 ic = request->ip_conn;
450 *more_neededp = false;
451 ic->ic_receive_len = 0;
453 len = icl_pdu_data_segment_length(request);
458 padding = 4 - (len % 4);
461 * Account for already received parts of data segment.
463 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
464 len -= request->ip_data_len;
466 if (len + padding > *rs) {
468 * Not enough data in the socket buffer. Receive as much
469 * as we can. Don't receive padding, since, obviously, it's
470 * not the end of data segment yet.
473 ICL_DEBUG("limited from %zd to %zd",
474 len + padding, *rs - padding));
477 *more_neededp = true;
482 * Must not try to receive padding without at least one byte
483 * of actual data segment.
487 *r = m_split(m, len + padding, M_WAITOK);
488 *rs -= len + padding;
490 if (request->ip_data_mbuf == NULL)
491 request->ip_data_mbuf = m;
493 m_cat(request->ip_data_mbuf, m);
495 request->ip_data_len += len;
501 icl_pdu_data_segment_receive_len(request);
507 icl_pdu_check_data_digest(struct icl_pdu *request, struct mbuf **r, size_t *rs)
509 uint32_t received_digest, valid_digest;
511 if (request->ip_conn->ic_data_crc32c == false)
514 if (request->ip_data_len == 0)
517 CTASSERT(sizeof(received_digest) == ISCSI_DATA_DIGEST_SIZE);
518 icl_soft_receive_buf(r, rs, &received_digest, ISCSI_DATA_DIGEST_SIZE);
521 * Note that ip_data_mbuf also contains padding; since digest
522 * calculation is supposed to include that, we iterate over
523 * the entire ip_data_mbuf chain, not just ip_data_len bytes of it.
525 valid_digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
526 if (received_digest != valid_digest) {
527 ICL_WARN("data digest check failed; got 0x%x, "
528 "should be 0x%x", received_digest, valid_digest);
536 * Somewhat contrary to the name, this attempts to receive only one
537 * "part" of PDU at a time; call it repeatedly until it returns non-NULL.
539 static struct icl_pdu *
540 icl_conn_receive_pdu(struct icl_conn *ic, struct mbuf **r, size_t *rs)
542 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_soft_conn_new_pdu(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 switch (ic->ic_receive_state) {
565 case ICL_CONN_STATE_BHS:
566 //ICL_DEBUG("receiving BHS");
567 icl_soft_receive_buf(r, rs, request->ip_bhs,
568 sizeof(struct iscsi_bhs));
571 * We don't enforce any limit for AHS length;
572 * its length is stored in 8 bit field.
575 len = icl_pdu_data_segment_length(request);
576 if (len > ic->ic_max_recv_data_segment_length) {
577 ICL_WARN("received data segment "
578 "length %zd is larger than negotiated; "
579 "dropping connection", len);
584 ic->ic_receive_state = ICL_CONN_STATE_AHS;
585 ic->ic_receive_len = icl_pdu_ahs_length(request);
588 case ICL_CONN_STATE_AHS:
589 //ICL_DEBUG("receiving AHS");
590 icl_pdu_receive_ahs(request, r, rs);
591 ic->ic_receive_state = ICL_CONN_STATE_HEADER_DIGEST;
592 if (ic->ic_header_crc32c == false)
593 ic->ic_receive_len = 0;
595 ic->ic_receive_len = ISCSI_HEADER_DIGEST_SIZE;
598 case ICL_CONN_STATE_HEADER_DIGEST:
599 //ICL_DEBUG("receiving header digest");
600 error = icl_pdu_check_header_digest(request, r, rs);
602 ICL_DEBUG("header digest failed; "
603 "dropping connection");
607 ic->ic_receive_state = ICL_CONN_STATE_DATA;
609 icl_pdu_data_segment_receive_len(request);
612 case ICL_CONN_STATE_DATA:
613 //ICL_DEBUG("receiving data segment");
614 error = icl_pdu_receive_data_segment(request, r, rs,
617 ICL_DEBUG("failed to receive data segment;"
618 "dropping connection");
625 ic->ic_receive_state = ICL_CONN_STATE_DATA_DIGEST;
626 if (request->ip_data_len == 0 || ic->ic_data_crc32c == false)
627 ic->ic_receive_len = 0;
629 ic->ic_receive_len = ISCSI_DATA_DIGEST_SIZE;
632 case ICL_CONN_STATE_DATA_DIGEST:
633 //ICL_DEBUG("receiving data digest");
634 error = icl_pdu_check_data_digest(request, r, rs);
636 ICL_DEBUG("data digest failed; "
637 "dropping connection");
642 * We've received complete PDU; reset the receive state machine
643 * and return the PDU.
645 ic->ic_receive_state = ICL_CONN_STATE_BHS;
646 ic->ic_receive_len = sizeof(struct iscsi_bhs);
647 ic->ic_receive_pdu = NULL;
651 panic("invalid ic_receive_state %d\n", ic->ic_receive_state);
656 * Don't free the PDU; it's pointed to by ic->ic_receive_pdu
657 * and will get freed in icl_soft_conn_close().
666 icl_conn_receive_pdus(struct icl_conn *ic, struct mbuf **r, size_t *rs)
668 struct icl_pdu *response;
671 if (ic->ic_disconnecting)
675 * Loop until we have a complete PDU or there is not enough
676 * data in the socket buffer.
678 if (*rs < ic->ic_receive_len) {
680 ICL_DEBUG("not enough data; have %zd, need %zd",
681 *rs, ic->ic_receive_len);
686 response = icl_conn_receive_pdu(ic, r, rs);
687 if (response == NULL)
690 if (response->ip_ahs_len > 0) {
691 ICL_WARN("received PDU with unsupported "
692 "AHS; opcode 0x%x; dropping connection",
693 response->ip_bhs->bhs_opcode);
694 icl_soft_conn_pdu_free(ic, response);
699 (ic->ic_receive)(response);
704 icl_receive_thread(void *arg)
707 size_t available, read = 0;
709 struct mbuf *m, *r = NULL;
717 SOCKBUF_LOCK(&so->so_rcv);
718 if (ic->ic_disconnecting) {
719 SOCKBUF_UNLOCK(&so->so_rcv);
724 * Set the low watermark, to be checked by
725 * soreadable() in icl_soupcall_receive()
726 * to avoid unnecessary wakeups until there
727 * is enough data received to read the PDU.
729 available = sbavail(&so->so_rcv);
730 if (read + available < ic->ic_receive_len) {
731 so->so_rcv.sb_lowat = ic->ic_receive_len - read;
732 cv_wait(&ic->ic_receive_cv, &so->so_rcv.sb_mtx);
733 so->so_rcv.sb_lowat = so->so_rcv.sb_hiwat + 1;
734 available = sbavail(&so->so_rcv);
736 SOCKBUF_UNLOCK(&so->so_rcv);
738 if (available == 0) {
739 if (so->so_error != 0) {
740 ICL_DEBUG("connection error %d; "
741 "dropping connection", so->so_error);
748 memset(&uio, 0, sizeof(uio));
749 uio.uio_resid = available;
750 flags = MSG_DONTWAIT;
751 error = soreceive(so, NULL, &uio, &m, NULL, &flags);
753 ICL_DEBUG("soreceive error %d", error);
756 if (uio.uio_resid != 0) {
758 ICL_DEBUG("short read");
767 icl_conn_receive_pdus(ic, &r, &read);
774 ic->ic_receive_running = false;
775 cv_signal(&ic->ic_send_cv);
781 icl_soupcall_receive(struct socket *so, void *arg, int waitflag)
789 cv_signal(&ic->ic_receive_cv);
794 icl_pdu_finalize(struct icl_pdu *request)
796 size_t padding, pdu_len;
797 uint32_t digest, zero = 0;
801 ic = request->ip_conn;
803 icl_pdu_set_data_segment_length(request, request->ip_data_len);
805 pdu_len = icl_pdu_size(request);
807 if (ic->ic_header_crc32c) {
808 digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
809 ok = m_append(request->ip_bhs_mbuf, sizeof(digest),
812 ICL_WARN("failed to append header digest");
817 if (request->ip_data_len != 0) {
818 padding = icl_pdu_padding(request);
820 ok = m_append(request->ip_data_mbuf, padding,
823 ICL_WARN("failed to append padding");
828 if (ic->ic_data_crc32c) {
829 digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
831 ok = m_append(request->ip_data_mbuf, sizeof(digest),
834 ICL_WARN("failed to append data digest");
839 m_cat(request->ip_bhs_mbuf, request->ip_data_mbuf);
840 request->ip_data_mbuf = NULL;
843 request->ip_bhs_mbuf->m_pkthdr.len = pdu_len;
849 icl_conn_send_pdus(struct icl_conn *ic, struct icl_pdu_stailq *queue)
851 struct icl_pdu *request, *request2;
854 long 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);
869 ic->ic_check_send_space = false;
872 * Notify the socket upcall that we don't need wakeups
873 * for the time being.
875 so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1;
876 SOCKBUF_UNLOCK(&so->so_snd);
878 while (!STAILQ_EMPTY(queue)) {
879 request = STAILQ_FIRST(queue);
880 size = icl_pdu_size(request);
881 if (available < size) {
883 * Set the low watermark, to be checked by
884 * sowriteable() in icl_soupcall_send()
885 * to avoid unnecessary wakeups until there
886 * is enough space for the PDU to fit.
888 SOCKBUF_LOCK(&so->so_snd);
889 available = sbspace(&so->so_snd);
890 if (available < size) {
892 ICL_DEBUG("no space to send; "
893 "have %ld, need %ld",
896 so->so_snd.sb_lowat = max(size,
897 so->so_snd.sb_hiwat / 8);
898 SOCKBUF_UNLOCK(&so->so_snd);
901 SOCKBUF_UNLOCK(&so->so_snd);
903 STAILQ_REMOVE_HEAD(queue, ip_next);
904 error = icl_pdu_finalize(request);
906 ICL_DEBUG("failed to finalize PDU; "
907 "dropping connection");
908 icl_soft_pdu_done(request, EIO);
913 m = request->ip_bhs_mbuf;
914 for (coalesced = 1; ; coalesced++) {
915 request2 = STAILQ_FIRST(queue);
916 if (request2 == NULL)
918 size2 = icl_pdu_size(request2);
919 if (available < size + size2)
921 STAILQ_REMOVE_HEAD(queue, ip_next);
922 error = icl_pdu_finalize(request2);
924 ICL_DEBUG("failed to finalize PDU; "
925 "dropping connection");
926 icl_soft_pdu_done(request, EIO);
927 icl_soft_pdu_done(request2, EIO);
933 m_cat(m, request2->ip_bhs_mbuf);
934 request2->ip_bhs_mbuf = NULL;
935 request->ip_bhs_mbuf->m_pkthdr.len += size2;
937 icl_soft_pdu_done(request2, 0);
941 ICL_DEBUG("coalesced %d PDUs into %ld bytes",
947 error = sosend(so, NULL, NULL, request->ip_bhs_mbuf,
948 NULL, MSG_DONTWAIT, curthread);
949 request->ip_bhs_mbuf = NULL; /* Sosend consumes the mbuf. */
951 ICL_DEBUG("failed to send PDU, error %d; "
952 "dropping connection", error);
953 icl_soft_pdu_done(request, error);
957 icl_soft_pdu_done(request, 0);
962 icl_send_thread(void *arg)
965 struct icl_pdu_stailq queue;
975 * Populate the local queue from the main one.
976 * This way the icl_conn_send_pdus() can go through
977 * all the queued PDUs without holding any locks.
979 if (STAILQ_EMPTY(&queue) || ic->ic_check_send_space)
980 STAILQ_CONCAT(&queue, &ic->ic_to_send);
983 icl_conn_send_pdus(ic, &queue);
987 * The icl_soupcall_send() was called since the last
988 * call to sbspace(); go around;
990 if (ic->ic_check_send_space)
994 * Local queue is empty, but we still have PDUs
995 * in the main one; go around.
997 if (STAILQ_EMPTY(&queue) &&
998 !STAILQ_EMPTY(&ic->ic_to_send))
1002 * There might be some stuff in the local queue,
1003 * which didn't get sent due to not having enough send
1004 * space. Wait for socket upcall.
1009 if (ic->ic_disconnecting) {
1010 //ICL_DEBUG("terminating");
1014 cv_wait(&ic->ic_send_cv, ic->ic_lock);
1018 * We're exiting; move PDUs back to the main queue, so they can
1019 * get freed properly. At this point ordering doesn't matter.
1021 STAILQ_CONCAT(&ic->ic_to_send, &queue);
1023 ic->ic_send_running = false;
1024 cv_signal(&ic->ic_send_cv);
1025 ICL_CONN_UNLOCK(ic);
1030 icl_soupcall_send(struct socket *so, void *arg, int waitflag)
1032 struct icl_conn *ic;
1034 if (!sowriteable(so))
1040 ic->ic_check_send_space = true;
1041 ICL_CONN_UNLOCK(ic);
1043 cv_signal(&ic->ic_send_cv);
1049 icl_soft_conn_pdu_append_data(struct icl_conn *ic, struct icl_pdu *request,
1050 const void *addr, size_t len, int flags)
1052 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)request;
1053 struct mbuf *mb, *newmb;
1054 size_t copylen, off = 0;
1056 KASSERT(len > 0, ("len == 0"));
1058 if (flags & ICL_NOCOPY) {
1059 newmb = m_get(flags & ~ICL_NOCOPY, MT_DATA);
1060 if (newmb == NULL) {
1061 ICL_WARN("failed to allocate mbuf");
1065 newmb->m_flags |= M_RDONLY;
1066 m_extaddref(newmb, __DECONST(char *, addr), len, &isp->ref_cnt,
1067 icl_soft_mbuf_done, isp, NULL);
1070 newmb = m_getm2(NULL, len, flags, MT_DATA, 0);
1071 if (newmb == NULL) {
1072 ICL_WARN("failed to allocate mbuf for %zd bytes", len);
1076 for (mb = newmb; mb != NULL; mb = mb->m_next) {
1077 copylen = min(M_TRAILINGSPACE(mb), len - off);
1078 memcpy(mtod(mb, char *), (const char *)addr + off, copylen);
1079 mb->m_len = copylen;
1082 KASSERT(off == len, ("%s: off != len", __func__));
1085 if (request->ip_data_mbuf == NULL) {
1086 request->ip_data_mbuf = newmb;
1087 request->ip_data_len = len;
1089 m_cat(request->ip_data_mbuf, newmb);
1090 request->ip_data_len += len;
1097 icl_soft_conn_pdu_get_data(struct icl_conn *ic, struct icl_pdu *ip,
1098 size_t off, void *addr, size_t len)
1101 m_copydata(ip->ip_data_mbuf, off, len, addr);
1105 icl_soft_conn_pdu_queue(struct icl_conn *ic, struct icl_pdu *ip)
1108 icl_soft_conn_pdu_queue_cb(ic, ip, NULL);
1112 icl_soft_conn_pdu_queue_cb(struct icl_conn *ic, struct icl_pdu *ip,
1115 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip;
1117 ICL_CONN_LOCK_ASSERT(ic);
1121 if (ic->ic_disconnecting || ic->ic_socket == NULL) {
1122 ICL_DEBUG("icl_pdu_queue on closed connection");
1123 icl_soft_pdu_done(ip, ENOTCONN);
1127 if (!STAILQ_EMPTY(&ic->ic_to_send)) {
1128 STAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next);
1130 * If the queue is not empty, someone else had already
1131 * signaled the send thread; no need to do that again,
1137 STAILQ_INSERT_TAIL(&ic->ic_to_send, ip, ip_next);
1138 cv_signal(&ic->ic_send_cv);
1141 static struct icl_conn *
1142 icl_soft_new_conn(const char *name, struct mtx *lock)
1144 struct icl_conn *ic;
1146 refcount_acquire(&icl_ncons);
1148 ic = (struct icl_conn *)kobj_create(&icl_soft_class, M_ICL_SOFT, M_WAITOK | M_ZERO);
1150 STAILQ_INIT(&ic->ic_to_send);
1152 cv_init(&ic->ic_send_cv, "icl_tx");
1153 cv_init(&ic->ic_receive_cv, "icl_rx");
1155 refcount_init(&ic->ic_outstanding_pdus, 0);
1158 ic->ic_offload = "None";
1159 ic->ic_unmapped = false;
1165 icl_soft_conn_free(struct icl_conn *ic)
1169 KASSERT(ic->ic_outstanding_pdus == 0,
1170 ("destroying session with %d outstanding PDUs",
1171 ic->ic_outstanding_pdus));
1173 cv_destroy(&ic->ic_send_cv);
1174 cv_destroy(&ic->ic_receive_cv);
1175 kobj_delete((struct kobj *)ic, M_ICL_SOFT);
1176 refcount_release(&icl_ncons);
1180 icl_conn_start(struct icl_conn *ic)
1191 if (ic->ic_socket == NULL) {
1192 ICL_CONN_UNLOCK(ic);
1196 ic->ic_receive_state = ICL_CONN_STATE_BHS;
1197 ic->ic_receive_len = sizeof(struct iscsi_bhs);
1198 ic->ic_disconnecting = false;
1200 ICL_CONN_UNLOCK(ic);
1203 * For sendspace, this is required because the current code cannot
1204 * send a PDU in pieces; thus, the minimum buffer size is equal
1205 * to the maximum PDU size. "+4" is to account for possible padding.
1207 minspace = sizeof(struct iscsi_bhs) +
1208 ic->ic_max_send_data_segment_length +
1209 ISCSI_HEADER_DIGEST_SIZE + ISCSI_DATA_DIGEST_SIZE + 4;
1210 if (sendspace < minspace) {
1211 ICL_WARN("kern.icl.sendspace too low; must be at least %zd",
1213 sendspace = minspace;
1215 minspace = sizeof(struct iscsi_bhs) +
1216 ic->ic_max_recv_data_segment_length +
1217 ISCSI_HEADER_DIGEST_SIZE + ISCSI_DATA_DIGEST_SIZE + 4;
1218 if (recvspace < minspace) {
1219 ICL_WARN("kern.icl.recvspace too low; must be at least %zd",
1221 recvspace = minspace;
1224 error = soreserve(ic->ic_socket, sendspace, recvspace);
1226 ICL_WARN("soreserve failed with error %d", error);
1227 icl_soft_conn_close(ic);
1230 ic->ic_socket->so_snd.sb_flags |= SB_AUTOSIZE;
1231 ic->ic_socket->so_rcv.sb_flags |= SB_AUTOSIZE;
1236 bzero(&opt, sizeof(opt));
1237 opt.sopt_dir = SOPT_SET;
1238 opt.sopt_level = IPPROTO_TCP;
1239 opt.sopt_name = TCP_NODELAY;
1240 opt.sopt_val = &one;
1241 opt.sopt_valsize = sizeof(one);
1242 error = sosetopt(ic->ic_socket, &opt);
1244 ICL_WARN("disabling TCP_NODELAY failed with error %d", error);
1245 icl_soft_conn_close(ic);
1250 * Register socket upcall, to get notified about incoming PDUs
1251 * and free space to send outgoing ones.
1253 SOCKBUF_LOCK(&ic->ic_socket->so_snd);
1254 soupcall_set(ic->ic_socket, SO_SND, icl_soupcall_send, ic);
1255 SOCKBUF_UNLOCK(&ic->ic_socket->so_snd);
1256 SOCKBUF_LOCK(&ic->ic_socket->so_rcv);
1257 soupcall_set(ic->ic_socket, SO_RCV, icl_soupcall_receive, ic);
1258 SOCKBUF_UNLOCK(&ic->ic_socket->so_rcv);
1264 ic->ic_send_running = ic->ic_receive_running = true;
1265 ICL_CONN_UNLOCK(ic);
1266 error = kthread_add(icl_send_thread, ic, NULL, NULL, 0, 0, "%stx",
1269 ICL_WARN("kthread_add(9) failed with error %d", error);
1271 ic->ic_send_running = ic->ic_receive_running = false;
1272 cv_signal(&ic->ic_send_cv);
1273 ICL_CONN_UNLOCK(ic);
1274 icl_soft_conn_close(ic);
1277 error = kthread_add(icl_receive_thread, ic, NULL, NULL, 0, 0, "%srx",
1280 ICL_WARN("kthread_add(9) failed with error %d", error);
1282 ic->ic_receive_running = false;
1283 cv_signal(&ic->ic_send_cv);
1284 ICL_CONN_UNLOCK(ic);
1285 icl_soft_conn_close(ic);
1293 icl_soft_conn_handoff(struct icl_conn *ic, int fd)
1297 cap_rights_t rights;
1300 ICL_CONN_LOCK_ASSERT_NOT(ic);
1302 #ifdef ICL_KERNEL_PROXY
1304 * We're transitioning to Full Feature phase, and we don't
1309 if (ic->ic_socket == NULL) {
1310 ICL_CONN_UNLOCK(ic);
1311 ICL_WARN("proxy handoff without connect");
1314 ICL_CONN_UNLOCK(ic);
1320 * Steal the socket from userland.
1322 error = fget(curthread, fd,
1323 cap_rights_init_one(&rights, CAP_SOCK_CLIENT), &fp);
1326 if (fp->f_type != DTYPE_SOCKET) {
1327 fdrop(fp, curthread);
1331 if (so->so_type != SOCK_STREAM) {
1332 fdrop(fp, curthread);
1338 if (ic->ic_socket != NULL) {
1339 ICL_CONN_UNLOCK(ic);
1340 fdrop(fp, curthread);
1344 ic->ic_socket = fp->f_data;
1345 fp->f_ops = &badfileops;
1347 fdrop(fp, curthread);
1348 ICL_CONN_UNLOCK(ic);
1350 error = icl_conn_start(ic);
1356 icl_soft_conn_close(struct icl_conn *ic)
1358 struct icl_pdu *pdu;
1362 * Wake up the threads, so they can properly terminate.
1363 * Receive thread sleeps on so->so_rcv lock, send on ic->ic_lock.
1366 if (!ic->ic_disconnecting) {
1369 SOCKBUF_LOCK(&so->so_rcv);
1370 ic->ic_disconnecting = true;
1372 SOCKBUF_UNLOCK(&so->so_rcv);
1374 while (ic->ic_receive_running || ic->ic_send_running) {
1375 cv_signal(&ic->ic_receive_cv);
1376 cv_signal(&ic->ic_send_cv);
1377 cv_wait(&ic->ic_send_cv, ic->ic_lock);
1380 /* Some other thread could close the connection same time. */
1383 ICL_CONN_UNLOCK(ic);
1386 ic->ic_socket = NULL;
1389 * Deregister socket upcalls.
1391 ICL_CONN_UNLOCK(ic);
1392 SOCKBUF_LOCK(&so->so_snd);
1393 if (so->so_snd.sb_upcall != NULL)
1394 soupcall_clear(so, SO_SND);
1395 SOCKBUF_UNLOCK(&so->so_snd);
1396 SOCKBUF_LOCK(&so->so_rcv);
1397 if (so->so_rcv.sb_upcall != NULL)
1398 soupcall_clear(so, SO_RCV);
1399 SOCKBUF_UNLOCK(&so->so_rcv);
1403 if (ic->ic_receive_pdu != NULL) {
1404 //ICL_DEBUG("freeing partially received PDU");
1405 icl_soft_conn_pdu_free(ic, ic->ic_receive_pdu);
1406 ic->ic_receive_pdu = NULL;
1410 * Remove any outstanding PDUs from the send queue.
1412 while (!STAILQ_EMPTY(&ic->ic_to_send)) {
1413 pdu = STAILQ_FIRST(&ic->ic_to_send);
1414 STAILQ_REMOVE_HEAD(&ic->ic_to_send, ip_next);
1415 icl_soft_pdu_done(pdu, ENOTCONN);
1418 KASSERT(STAILQ_EMPTY(&ic->ic_to_send),
1419 ("destroying session with non-empty send queue"));
1420 ICL_CONN_UNLOCK(ic);
1424 icl_soft_conn_task_setup(struct icl_conn *ic, struct icl_pdu *ip,
1425 struct ccb_scsiio *csio, uint32_t *task_tagp, void **prvp)
1432 icl_soft_conn_task_done(struct icl_conn *ic, void *prv)
1437 icl_soft_conn_transfer_setup(struct icl_conn *ic, union ctl_io *io,
1438 uint32_t *transfer_tag, void **prvp)
1445 icl_soft_conn_transfer_done(struct icl_conn *ic, void *prv)
1450 icl_soft_limits(struct icl_drv_limits *idl)
1453 idl->idl_max_recv_data_segment_length = max_data_segment_length;
1454 idl->idl_max_send_data_segment_length = max_data_segment_length;
1455 idl->idl_max_burst_length = max_burst_length;
1456 idl->idl_first_burst_length = first_burst_length;
1461 #ifdef ICL_KERNEL_PROXY
1463 icl_soft_conn_connect(struct icl_conn *ic, int domain, int socktype,
1464 int protocol, struct sockaddr *from_sa, struct sockaddr *to_sa)
1467 return (icl_soft_proxy_connect(ic, domain, socktype, protocol,
1472 icl_soft_handoff_sock(struct icl_conn *ic, struct socket *so)
1476 ICL_CONN_LOCK_ASSERT_NOT(ic);
1478 if (so->so_type != SOCK_STREAM)
1482 if (ic->ic_socket != NULL) {
1483 ICL_CONN_UNLOCK(ic);
1487 ICL_CONN_UNLOCK(ic);
1489 error = icl_conn_start(ic);
1493 #endif /* ICL_KERNEL_PROXY */
1500 icl_soft_pdu_zone = uma_zcreate("icl_soft_pdu",
1501 sizeof(struct icl_soft_pdu), NULL, NULL, NULL, NULL,
1503 refcount_init(&icl_ncons, 0);
1506 * The reason we call this "none" is that to the user,
1507 * it's known as "offload driver"; "offload driver: soft"
1508 * doesn't make much sense.
1510 error = icl_register("none", false, 0,
1511 icl_soft_limits, icl_soft_new_conn);
1512 KASSERT(error == 0, ("failed to register"));
1514 #if defined(ICL_KERNEL_PROXY) && 0
1516 * Debugging aid for kernel proxy functionality.
1518 error = icl_register("proxytest", true, 0,
1519 icl_soft_limits, icl_soft_new_conn);
1520 KASSERT(error == 0, ("failed to register"));
1527 icl_soft_unload(void)
1533 icl_unregister("none", false);
1534 #if defined(ICL_KERNEL_PROXY) && 0
1535 icl_unregister("proxytest", true);
1538 uma_zdestroy(icl_soft_pdu_zone);
1544 icl_soft_modevent(module_t mod, int what, void *arg)
1549 return (icl_soft_load());
1551 return (icl_soft_unload());
1557 moduledata_t icl_soft_data = {
1563 DECLARE_MODULE(icl_soft, icl_soft_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1564 MODULE_DEPEND(icl_soft, icl, 1, 1, 1);
1565 MODULE_VERSION(icl_soft, 1);