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>
66 #define ICL_CONN_STATE_BHS 1
67 #define ICL_CONN_STATE_AHS 2
68 #define ICL_CONN_STATE_HEADER_DIGEST 3
69 #define ICL_CONN_STATE_DATA 4
70 #define ICL_CONN_STATE_DATA_DIGEST 5
72 struct icl_soft_conn {
75 /* soft specific stuff goes here. */
76 STAILQ_HEAD(, icl_pdu) to_send;
79 struct icl_pdu *receive_pdu;
83 bool check_send_space;
90 /* soft specific stuff goes here. */
96 SYSCTL_NODE(_kern_icl, OID_AUTO, soft, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
98 static int coalesce = 1;
99 SYSCTL_INT(_kern_icl_soft, OID_AUTO, coalesce, CTLFLAG_RWTUN,
100 &coalesce, 0, "Try to coalesce PDUs before sending");
101 static int partial_receive_len = 256 * 1024;
102 SYSCTL_INT(_kern_icl_soft, OID_AUTO, partial_receive_len, CTLFLAG_RWTUN,
103 &partial_receive_len, 0, "Minimum read size for partially received "
105 static int max_data_segment_length = 256 * 1024;
106 SYSCTL_INT(_kern_icl_soft, OID_AUTO, max_data_segment_length, CTLFLAG_RWTUN,
107 &max_data_segment_length, 0, "Maximum data segment length");
108 static int first_burst_length = 1024 * 1024;
109 SYSCTL_INT(_kern_icl_soft, OID_AUTO, first_burst_length, CTLFLAG_RWTUN,
110 &first_burst_length, 0, "First burst length");
111 static int max_burst_length = 1024 * 1024;
112 SYSCTL_INT(_kern_icl_soft, OID_AUTO, max_burst_length, CTLFLAG_RWTUN,
113 &max_burst_length, 0, "Maximum burst length");
114 static int sendspace = 1536 * 1024;
115 SYSCTL_INT(_kern_icl_soft, OID_AUTO, sendspace, CTLFLAG_RWTUN,
116 &sendspace, 0, "Default send socket buffer size");
117 static int recvspace = 1536 * 1024;
118 SYSCTL_INT(_kern_icl_soft, OID_AUTO, recvspace, CTLFLAG_RWTUN,
119 &recvspace, 0, "Default receive socket buffer size");
121 static MALLOC_DEFINE(M_ICL_SOFT, "icl_soft", "iSCSI software backend");
122 static uma_zone_t icl_soft_pdu_zone;
124 static volatile u_int icl_ncons;
126 #define ICL_CONN_LOCK(X) mtx_lock(X->ic_lock)
127 #define ICL_CONN_UNLOCK(X) mtx_unlock(X->ic_lock)
128 #define ICL_CONN_LOCK_ASSERT(X) mtx_assert(X->ic_lock, MA_OWNED)
129 #define ICL_CONN_LOCK_ASSERT_NOT(X) mtx_assert(X->ic_lock, MA_NOTOWNED)
131 STAILQ_HEAD(icl_pdu_stailq, icl_pdu);
133 static icl_conn_new_pdu_t icl_soft_conn_new_pdu;
134 static icl_conn_pdu_free_t icl_soft_conn_pdu_free;
135 static icl_conn_pdu_data_segment_length_t
136 icl_soft_conn_pdu_data_segment_length;
137 static icl_conn_pdu_append_data_t icl_soft_conn_pdu_append_data;
138 static icl_conn_pdu_get_data_t icl_soft_conn_pdu_get_data;
139 static icl_conn_pdu_queue_t icl_soft_conn_pdu_queue;
140 static icl_conn_pdu_queue_cb_t icl_soft_conn_pdu_queue_cb;
141 static icl_conn_handoff_t icl_soft_conn_handoff;
142 static icl_conn_free_t icl_soft_conn_free;
143 static icl_conn_close_t icl_soft_conn_close;
144 static icl_conn_task_setup_t icl_soft_conn_task_setup;
145 static icl_conn_task_done_t icl_soft_conn_task_done;
146 static icl_conn_transfer_setup_t icl_soft_conn_transfer_setup;
147 static icl_conn_transfer_done_t icl_soft_conn_transfer_done;
148 #ifdef ICL_KERNEL_PROXY
149 static icl_conn_connect_t icl_soft_conn_connect;
152 static kobj_method_t icl_soft_methods[] = {
153 KOBJMETHOD(icl_conn_new_pdu, icl_soft_conn_new_pdu),
154 KOBJMETHOD(icl_conn_pdu_free, icl_soft_conn_pdu_free),
155 KOBJMETHOD(icl_conn_pdu_data_segment_length,
156 icl_soft_conn_pdu_data_segment_length),
157 KOBJMETHOD(icl_conn_pdu_append_data, icl_soft_conn_pdu_append_data),
158 KOBJMETHOD(icl_conn_pdu_get_data, icl_soft_conn_pdu_get_data),
159 KOBJMETHOD(icl_conn_pdu_queue, icl_soft_conn_pdu_queue),
160 KOBJMETHOD(icl_conn_pdu_queue_cb, icl_soft_conn_pdu_queue_cb),
161 KOBJMETHOD(icl_conn_handoff, icl_soft_conn_handoff),
162 KOBJMETHOD(icl_conn_free, icl_soft_conn_free),
163 KOBJMETHOD(icl_conn_close, icl_soft_conn_close),
164 KOBJMETHOD(icl_conn_task_setup, icl_soft_conn_task_setup),
165 KOBJMETHOD(icl_conn_task_done, icl_soft_conn_task_done),
166 KOBJMETHOD(icl_conn_transfer_setup, icl_soft_conn_transfer_setup),
167 KOBJMETHOD(icl_conn_transfer_done, icl_soft_conn_transfer_done),
168 #ifdef ICL_KERNEL_PROXY
169 KOBJMETHOD(icl_conn_connect, icl_soft_conn_connect),
174 DEFINE_CLASS(icl_soft, icl_soft_methods, sizeof(struct icl_soft_conn));
177 icl_conn_fail(struct icl_conn *ic)
179 if (ic->ic_socket == NULL)
185 ic->ic_socket->so_error = EDOOFUS;
190 icl_soft_conn_pdu_free(struct icl_conn *ic, struct icl_pdu *ip)
192 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip;
194 KASSERT(isp->ref_cnt == 0, ("freeing active PDU"));
195 m_freem(ip->ip_bhs_mbuf);
196 m_freem(ip->ip_ahs_mbuf);
197 m_freem(ip->ip_data_mbuf);
198 uma_zfree(icl_soft_pdu_zone, isp);
200 refcount_release(&ic->ic_outstanding_pdus);
205 icl_soft_pdu_call_cb(struct icl_pdu *ip)
207 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip;
210 isp->cb(ip, isp->error);
212 refcount_release(&ip->ip_conn->ic_outstanding_pdus);
214 uma_zfree(icl_soft_pdu_zone, isp);
218 icl_soft_pdu_done(struct icl_pdu *ip, int error)
220 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip;
225 m_freem(ip->ip_bhs_mbuf);
226 ip->ip_bhs_mbuf = NULL;
227 m_freem(ip->ip_ahs_mbuf);
228 ip->ip_ahs_mbuf = NULL;
229 m_freem(ip->ip_data_mbuf);
230 ip->ip_data_mbuf = NULL;
232 if (atomic_fetchadd_int(&isp->ref_cnt, -1) == 1)
233 icl_soft_pdu_call_cb(ip);
237 icl_soft_mbuf_done(struct mbuf *mb)
239 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)mb->m_ext.ext_arg1;
241 icl_soft_pdu_call_cb(&isp->ip);
245 * Allocate icl_pdu with empty BHS to fill up by the caller.
248 icl_soft_conn_new_pdu(struct icl_conn *ic, int flags)
250 struct icl_soft_pdu *isp;
254 refcount_acquire(&ic->ic_outstanding_pdus);
256 isp = uma_zalloc(icl_soft_pdu_zone, flags | M_ZERO);
258 ICL_WARN("failed to allocate soft PDU");
260 refcount_release(&ic->ic_outstanding_pdus);
267 CTASSERT(sizeof(struct iscsi_bhs) <= MHLEN);
268 ip->ip_bhs_mbuf = m_gethdr(flags, MT_DATA);
269 if (ip->ip_bhs_mbuf == NULL) {
270 ICL_WARN("failed to allocate BHS mbuf");
271 icl_soft_conn_pdu_free(ic, ip);
274 ip->ip_bhs = mtod(ip->ip_bhs_mbuf, struct iscsi_bhs *);
275 memset(ip->ip_bhs, 0, sizeof(struct iscsi_bhs));
276 ip->ip_bhs_mbuf->m_len = sizeof(struct iscsi_bhs);
282 icl_pdu_ahs_length(const struct icl_pdu *request)
285 return (request->ip_bhs->bhs_total_ahs_len * 4);
289 icl_pdu_data_segment_length(const struct icl_pdu *request)
293 len += request->ip_bhs->bhs_data_segment_len[0];
295 len += request->ip_bhs->bhs_data_segment_len[1];
297 len += request->ip_bhs->bhs_data_segment_len[2];
303 icl_soft_conn_pdu_data_segment_length(struct icl_conn *ic,
304 const struct icl_pdu *request)
307 return (icl_pdu_data_segment_length(request));
311 icl_pdu_set_data_segment_length(struct icl_pdu *response, uint32_t len)
314 response->ip_bhs->bhs_data_segment_len[2] = len;
315 response->ip_bhs->bhs_data_segment_len[1] = len >> 8;
316 response->ip_bhs->bhs_data_segment_len[0] = len >> 16;
320 icl_pdu_padding(const struct icl_pdu *ip)
323 if ((ip->ip_data_len % 4) != 0)
324 return (4 - (ip->ip_data_len % 4));
330 icl_pdu_size(const struct icl_pdu *response)
334 KASSERT(response->ip_ahs_len == 0, ("responding with AHS"));
336 len = sizeof(struct iscsi_bhs) + response->ip_data_len +
337 icl_pdu_padding(response);
338 if (response->ip_conn->ic_header_crc32c)
339 len += ISCSI_HEADER_DIGEST_SIZE;
340 if (response->ip_data_len != 0 && response->ip_conn->ic_data_crc32c)
341 len += ISCSI_DATA_DIGEST_SIZE;
347 icl_soft_receive_buf(struct mbuf **r, size_t *rs, void *buf, size_t s)
350 m_copydata(*r, 0, s, buf);
352 while ((*r) != NULL && (*r)->m_len == 0)
358 icl_pdu_receive_ahs(struct icl_pdu *request, struct mbuf **r, size_t *rs)
361 request->ip_ahs_len = icl_pdu_ahs_length(request);
362 if (request->ip_ahs_len == 0)
365 request->ip_ahs_mbuf = *r;
366 *r = m_split(request->ip_ahs_mbuf, request->ip_ahs_len, M_WAITOK);
367 *rs -= request->ip_ahs_len;
371 icl_mbuf_to_crc32c(const struct mbuf *m0)
373 uint32_t digest = 0xffffffff;
374 const struct mbuf *m;
376 for (m = m0; m != NULL; m = m->m_next)
377 digest = calculate_crc32c(digest,
378 mtod(m, const void *), m->m_len);
380 digest = digest ^ 0xffffffff;
386 icl_pdu_check_header_digest(struct icl_pdu *request, struct mbuf **r, size_t *rs)
388 uint32_t received_digest, valid_digest;
390 if (request->ip_conn->ic_header_crc32c == false)
393 CTASSERT(sizeof(received_digest) == ISCSI_HEADER_DIGEST_SIZE);
394 icl_soft_receive_buf(r, rs, &received_digest, ISCSI_HEADER_DIGEST_SIZE);
396 /* Temporary attach AHS to BHS to calculate header digest. */
397 request->ip_bhs_mbuf->m_next = request->ip_ahs_mbuf;
398 valid_digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
399 request->ip_bhs_mbuf->m_next = NULL;
400 if (received_digest != valid_digest) {
401 ICL_WARN("header digest check failed; got 0x%x, "
402 "should be 0x%x", received_digest, valid_digest);
410 * Return the number of bytes that should be waiting in the receive socket
411 * before icl_pdu_receive_data_segment() gets called.
414 icl_pdu_data_segment_receive_len(const struct icl_pdu *request)
418 len = icl_pdu_data_segment_length(request);
423 * Account for the parts of data segment already read from
426 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
427 len -= request->ip_data_len;
430 * Don't always wait for the full data segment to be delivered
431 * to the socket; this might badly affect performance due to
432 * TCP window scaling.
434 if (len > partial_receive_len) {
436 ICL_DEBUG("need %zd bytes of data, limiting to %zd",
437 len, partial_receive_len));
439 len = partial_receive_len;
445 * Account for padding. Note that due to the way code is written,
446 * the icl_pdu_receive_data_segment() must always receive padding
447 * along with the last part of data segment, because it would be
448 * impossible to tell whether we've already received the full data
449 * segment including padding, or without it.
452 len += 4 - (len % 4);
455 ICL_DEBUG("need %zd bytes of data", len));
462 icl_pdu_receive_data_segment(struct icl_pdu *request, struct mbuf **r,
463 size_t *rs, bool *more_neededp)
465 struct icl_soft_conn *isc;
466 size_t len, padding = 0;
469 isc = (struct icl_soft_conn *)request->ip_conn;
471 *more_neededp = false;
472 isc->receive_len = 0;
474 len = icl_pdu_data_segment_length(request);
479 padding = 4 - (len % 4);
482 * Account for already received parts of data segment.
484 KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len"));
485 len -= request->ip_data_len;
487 if (len + padding > *rs) {
489 * Not enough data in the socket buffer. Receive as much
490 * as we can. Don't receive padding, since, obviously, it's
491 * not the end of data segment yet.
494 ICL_DEBUG("limited from %zd to %zd",
495 len + padding, *rs - padding));
498 *more_neededp = true;
503 * Must not try to receive padding without at least one byte
504 * of actual data segment.
508 *r = m_split(m, len + padding, M_WAITOK);
509 *rs -= len + padding;
511 if (request->ip_data_mbuf == NULL)
512 request->ip_data_mbuf = m;
514 m_cat(request->ip_data_mbuf, m);
516 request->ip_data_len += len;
521 isc->receive_len = icl_pdu_data_segment_receive_len(request);
527 icl_pdu_check_data_digest(struct icl_pdu *request, struct mbuf **r, size_t *rs)
529 uint32_t received_digest, valid_digest;
531 if (request->ip_conn->ic_data_crc32c == false)
534 if (request->ip_data_len == 0)
537 CTASSERT(sizeof(received_digest) == ISCSI_DATA_DIGEST_SIZE);
538 icl_soft_receive_buf(r, rs, &received_digest, ISCSI_DATA_DIGEST_SIZE);
541 * Note that ip_data_mbuf also contains padding; since digest
542 * calculation is supposed to include that, we iterate over
543 * the entire ip_data_mbuf chain, not just ip_data_len bytes of it.
545 valid_digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
546 if (received_digest != valid_digest) {
547 ICL_WARN("data digest check failed; got 0x%x, "
548 "should be 0x%x", received_digest, valid_digest);
556 * Somewhat contrary to the name, this attempts to receive only one
557 * "part" of PDU at a time; call it repeatedly until it returns non-NULL.
559 static struct icl_pdu *
560 icl_conn_receive_pdu(struct icl_soft_conn *isc, struct mbuf **r, size_t *rs)
562 struct icl_conn *ic = &isc->ic;
563 struct icl_pdu *request;
568 if (isc->receive_state == ICL_CONN_STATE_BHS) {
569 KASSERT(isc->receive_pdu == NULL,
570 ("isc->receive_pdu != NULL"));
571 request = icl_soft_conn_new_pdu(ic, M_NOWAIT);
572 if (request == NULL) {
573 ICL_DEBUG("failed to allocate PDU; "
574 "dropping connection");
578 isc->receive_pdu = request;
580 KASSERT(isc->receive_pdu != NULL,
581 ("isc->receive_pdu == NULL"));
582 request = isc->receive_pdu;
585 switch (isc->receive_state) {
586 case ICL_CONN_STATE_BHS:
587 //ICL_DEBUG("receiving BHS");
588 icl_soft_receive_buf(r, rs, request->ip_bhs,
589 sizeof(struct iscsi_bhs));
592 * We don't enforce any limit for AHS length;
593 * its length is stored in 8 bit field.
596 len = icl_pdu_data_segment_length(request);
597 if (len > ic->ic_max_recv_data_segment_length) {
598 ICL_WARN("received data segment "
599 "length %zd is larger than negotiated; "
600 "dropping connection", len);
605 isc->receive_state = ICL_CONN_STATE_AHS;
606 isc->receive_len = icl_pdu_ahs_length(request);
609 case ICL_CONN_STATE_AHS:
610 //ICL_DEBUG("receiving AHS");
611 icl_pdu_receive_ahs(request, r, rs);
612 isc->receive_state = ICL_CONN_STATE_HEADER_DIGEST;
613 if (ic->ic_header_crc32c == false)
614 isc->receive_len = 0;
616 isc->receive_len = ISCSI_HEADER_DIGEST_SIZE;
619 case ICL_CONN_STATE_HEADER_DIGEST:
620 //ICL_DEBUG("receiving header digest");
621 error = icl_pdu_check_header_digest(request, r, rs);
623 ICL_DEBUG("header digest failed; "
624 "dropping connection");
628 isc->receive_state = ICL_CONN_STATE_DATA;
629 isc->receive_len = icl_pdu_data_segment_receive_len(request);
632 case ICL_CONN_STATE_DATA:
633 //ICL_DEBUG("receiving data segment");
634 error = icl_pdu_receive_data_segment(request, r, rs,
637 ICL_DEBUG("failed to receive data segment;"
638 "dropping connection");
645 isc->receive_state = ICL_CONN_STATE_DATA_DIGEST;
646 if (request->ip_data_len == 0 || ic->ic_data_crc32c == false)
647 isc->receive_len = 0;
649 isc->receive_len = ISCSI_DATA_DIGEST_SIZE;
652 case ICL_CONN_STATE_DATA_DIGEST:
653 //ICL_DEBUG("receiving data digest");
654 error = icl_pdu_check_data_digest(request, r, rs);
656 ICL_DEBUG("data digest failed; "
657 "dropping connection");
662 * We've received complete PDU; reset the receive state machine
663 * and return the PDU.
665 isc->receive_state = ICL_CONN_STATE_BHS;
666 isc->receive_len = sizeof(struct iscsi_bhs);
667 isc->receive_pdu = NULL;
671 panic("invalid receive_state %d\n", isc->receive_state);
676 * Don't free the PDU; it's pointed to by isc->receive_pdu
677 * and will get freed in icl_soft_conn_close().
686 icl_conn_receive_pdus(struct icl_soft_conn *isc, struct mbuf **r, size_t *rs)
688 struct icl_conn *ic = &isc->ic;
689 struct icl_pdu *response;
692 if (ic->ic_disconnecting)
696 * Loop until we have a complete PDU or there is not enough
697 * data in the socket buffer.
699 if (*rs < isc->receive_len) {
701 ICL_DEBUG("not enough data; have %zd, need %zd",
702 *rs, isc->receive_len);
707 response = icl_conn_receive_pdu(isc, r, rs);
708 if (response == NULL)
711 if (response->ip_ahs_len > 0) {
712 ICL_WARN("received PDU with unsupported "
713 "AHS; opcode 0x%x; dropping connection",
714 response->ip_bhs->bhs_opcode);
715 icl_soft_conn_pdu_free(ic, response);
720 (ic->ic_receive)(response);
725 icl_receive_thread(void *arg)
727 struct icl_soft_conn *isc = arg;
728 struct icl_conn *ic = &isc->ic;
729 size_t available, read = 0;
731 struct mbuf *m, *r = NULL;
738 SOCKBUF_LOCK(&so->so_rcv);
739 if (ic->ic_disconnecting) {
740 SOCKBUF_UNLOCK(&so->so_rcv);
745 * Set the low watermark, to be checked by
746 * soreadable() in icl_soupcall_receive()
747 * to avoid unnecessary wakeups until there
748 * is enough data received to read the PDU.
750 available = sbavail(&so->so_rcv);
751 if (read + available < isc->receive_len) {
752 so->so_rcv.sb_lowat = isc->receive_len - read;
753 cv_wait(&isc->receive_cv, SOCKBUF_MTX(&so->so_rcv));
754 so->so_rcv.sb_lowat = so->so_rcv.sb_hiwat + 1;
755 available = sbavail(&so->so_rcv);
757 SOCKBUF_UNLOCK(&so->so_rcv);
759 if (available == 0) {
760 if (so->so_error != 0) {
761 ICL_DEBUG("connection error %d; "
762 "dropping connection", so->so_error);
769 memset(&uio, 0, sizeof(uio));
770 uio.uio_resid = available;
771 flags = MSG_DONTWAIT;
772 error = soreceive(so, NULL, &uio, &m, NULL, &flags);
774 ICL_DEBUG("soreceive error %d", error);
777 if (uio.uio_resid != 0) {
779 ICL_DEBUG("short read");
788 icl_conn_receive_pdus(isc, &r, &read);
795 isc->receive_running = false;
796 cv_signal(&isc->send_cv);
802 icl_soupcall_receive(struct socket *so, void *arg, int waitflag)
804 struct icl_soft_conn *isc;
810 cv_signal(&isc->receive_cv);
815 icl_pdu_finalize(struct icl_pdu *request)
817 size_t padding, pdu_len;
818 uint32_t digest, zero = 0;
822 ic = request->ip_conn;
824 icl_pdu_set_data_segment_length(request, request->ip_data_len);
826 pdu_len = icl_pdu_size(request);
828 if (ic->ic_header_crc32c) {
829 digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf);
830 ok = m_append(request->ip_bhs_mbuf, sizeof(digest),
833 ICL_WARN("failed to append header digest");
838 if (request->ip_data_len != 0) {
839 padding = icl_pdu_padding(request);
841 ok = m_append(request->ip_data_mbuf, padding,
844 ICL_WARN("failed to append padding");
849 if (ic->ic_data_crc32c) {
850 digest = icl_mbuf_to_crc32c(request->ip_data_mbuf);
852 ok = m_append(request->ip_data_mbuf, sizeof(digest),
855 ICL_WARN("failed to append data digest");
860 m_cat(request->ip_bhs_mbuf, request->ip_data_mbuf);
861 request->ip_data_mbuf = NULL;
864 request->ip_bhs_mbuf->m_pkthdr.len = pdu_len;
870 icl_conn_send_pdus(struct icl_soft_conn *isc, struct icl_pdu_stailq *queue)
872 struct icl_conn *ic = &isc->ic;
873 struct icl_pdu *request, *request2;
876 long available, size, size2;
877 int coalesced, error;
879 ICL_CONN_LOCK_ASSERT_NOT(ic);
883 SOCKBUF_LOCK(&so->so_snd);
885 * Check how much space do we have for transmit. We can't just
886 * call sosend() and retry when we get EWOULDBLOCK or EMSGSIZE,
887 * as it always frees the mbuf chain passed to it, even in case
890 available = sbspace(&so->so_snd);
891 isc->check_send_space = false;
894 * Notify the socket upcall that we don't need wakeups
895 * for the time being.
897 so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1;
898 SOCKBUF_UNLOCK(&so->so_snd);
900 while (!STAILQ_EMPTY(queue)) {
901 request = STAILQ_FIRST(queue);
902 size = icl_pdu_size(request);
903 if (available < size) {
905 * Set the low watermark, to be checked by
906 * sowriteable() in icl_soupcall_send()
907 * to avoid unnecessary wakeups until there
908 * is enough space for the PDU to fit.
910 SOCKBUF_LOCK(&so->so_snd);
911 available = sbspace(&so->so_snd);
912 if (available < size) {
914 ICL_DEBUG("no space to send; "
915 "have %ld, need %ld",
918 so->so_snd.sb_lowat = max(size,
919 so->so_snd.sb_hiwat / 8);
920 SOCKBUF_UNLOCK(&so->so_snd);
923 SOCKBUF_UNLOCK(&so->so_snd);
925 STAILQ_REMOVE_HEAD(queue, ip_next);
926 error = icl_pdu_finalize(request);
928 ICL_DEBUG("failed to finalize PDU; "
929 "dropping connection");
930 icl_soft_pdu_done(request, EIO);
935 m = request->ip_bhs_mbuf;
936 for (coalesced = 1; ; coalesced++) {
937 request2 = STAILQ_FIRST(queue);
938 if (request2 == NULL)
940 size2 = icl_pdu_size(request2);
941 if (available < size + size2)
943 STAILQ_REMOVE_HEAD(queue, ip_next);
944 error = icl_pdu_finalize(request2);
946 ICL_DEBUG("failed to finalize PDU; "
947 "dropping connection");
948 icl_soft_pdu_done(request, EIO);
949 icl_soft_pdu_done(request2, EIO);
955 m_cat(m, request2->ip_bhs_mbuf);
956 request2->ip_bhs_mbuf = NULL;
957 request->ip_bhs_mbuf->m_pkthdr.len += size2;
959 icl_soft_pdu_done(request2, 0);
963 ICL_DEBUG("coalesced %d PDUs into %ld bytes",
969 error = sosend(so, NULL, NULL, request->ip_bhs_mbuf,
970 NULL, MSG_DONTWAIT, curthread);
971 request->ip_bhs_mbuf = NULL; /* Sosend consumes the mbuf. */
973 ICL_DEBUG("failed to send PDU, error %d; "
974 "dropping connection", error);
975 icl_soft_pdu_done(request, error);
979 icl_soft_pdu_done(request, 0);
984 icl_send_thread(void *arg)
986 struct icl_soft_conn *isc;
988 struct icl_pdu_stailq queue;
999 * Populate the local queue from the main one.
1000 * This way the icl_conn_send_pdus() can go through
1001 * all the queued PDUs without holding any locks.
1003 if (STAILQ_EMPTY(&queue) || isc->check_send_space)
1004 STAILQ_CONCAT(&queue, &isc->to_send);
1006 ICL_CONN_UNLOCK(ic);
1007 icl_conn_send_pdus(isc, &queue);
1011 * The icl_soupcall_send() was called since the last
1012 * call to sbspace(); go around;
1014 if (isc->check_send_space)
1018 * Local queue is empty, but we still have PDUs
1019 * in the main one; go around.
1021 if (STAILQ_EMPTY(&queue) &&
1022 !STAILQ_EMPTY(&isc->to_send))
1026 * There might be some stuff in the local queue,
1027 * which didn't get sent due to not having enough send
1028 * space. Wait for socket upcall.
1033 if (ic->ic_disconnecting) {
1034 //ICL_DEBUG("terminating");
1038 cv_wait(&isc->send_cv, ic->ic_lock);
1042 * We're exiting; move PDUs back to the main queue, so they can
1043 * get freed properly. At this point ordering doesn't matter.
1045 STAILQ_CONCAT(&isc->to_send, &queue);
1047 isc->send_running = false;
1048 cv_signal(&isc->send_cv);
1049 ICL_CONN_UNLOCK(ic);
1054 icl_soupcall_send(struct socket *so, void *arg, int waitflag)
1056 struct icl_soft_conn *isc;
1057 struct icl_conn *ic;
1059 if (!sowriteable(so))
1066 isc->check_send_space = true;
1067 ICL_CONN_UNLOCK(ic);
1069 cv_signal(&isc->send_cv);
1075 icl_soft_conn_pdu_append_data(struct icl_conn *ic, struct icl_pdu *request,
1076 const void *addr, size_t len, int flags)
1078 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)request;
1079 struct mbuf *mb, *newmb;
1080 size_t copylen, off = 0;
1082 KASSERT(len > 0, ("len == 0"));
1084 if (flags & ICL_NOCOPY) {
1085 newmb = m_get(flags & ~ICL_NOCOPY, MT_DATA);
1086 if (newmb == NULL) {
1087 ICL_WARN("failed to allocate mbuf");
1091 newmb->m_flags |= M_RDONLY;
1092 m_extaddref(newmb, __DECONST(char *, addr), len, &isp->ref_cnt,
1093 icl_soft_mbuf_done, isp, NULL);
1096 newmb = m_getm2(NULL, len, flags, MT_DATA, 0);
1097 if (newmb == NULL) {
1098 ICL_WARN("failed to allocate mbuf for %zd bytes", len);
1102 for (mb = newmb; mb != NULL; mb = mb->m_next) {
1103 copylen = min(M_TRAILINGSPACE(mb), len - off);
1104 memcpy(mtod(mb, char *), (const char *)addr + off, copylen);
1105 mb->m_len = copylen;
1108 KASSERT(off == len, ("%s: off != len", __func__));
1111 if (request->ip_data_mbuf == NULL) {
1112 request->ip_data_mbuf = newmb;
1113 request->ip_data_len = len;
1115 m_cat(request->ip_data_mbuf, newmb);
1116 request->ip_data_len += len;
1123 icl_soft_conn_pdu_get_data(struct icl_conn *ic, struct icl_pdu *ip,
1124 size_t off, void *addr, size_t len)
1127 m_copydata(ip->ip_data_mbuf, off, len, addr);
1131 icl_soft_conn_pdu_queue(struct icl_conn *ic, struct icl_pdu *ip)
1134 icl_soft_conn_pdu_queue_cb(ic, ip, NULL);
1138 icl_soft_conn_pdu_queue_cb(struct icl_conn *ic, struct icl_pdu *ip,
1141 struct icl_soft_conn *isc = (struct icl_soft_conn *)ic;
1142 struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip;
1144 ICL_CONN_LOCK_ASSERT(ic);
1148 if (ic->ic_disconnecting || ic->ic_socket == NULL) {
1149 ICL_DEBUG("icl_pdu_queue on closed connection");
1150 icl_soft_pdu_done(ip, ENOTCONN);
1154 if (!STAILQ_EMPTY(&isc->to_send)) {
1155 STAILQ_INSERT_TAIL(&isc->to_send, ip, ip_next);
1157 * If the queue is not empty, someone else had already
1158 * signaled the send thread; no need to do that again,
1164 STAILQ_INSERT_TAIL(&isc->to_send, ip, ip_next);
1165 cv_signal(&isc->send_cv);
1168 static struct icl_conn *
1169 icl_soft_new_conn(const char *name, struct mtx *lock)
1171 struct icl_soft_conn *isc;
1172 struct icl_conn *ic;
1174 refcount_acquire(&icl_ncons);
1176 isc = (struct icl_soft_conn *)kobj_create(&icl_soft_class, M_ICL_SOFT,
1179 STAILQ_INIT(&isc->to_send);
1180 cv_init(&isc->send_cv, "icl_tx");
1181 cv_init(&isc->receive_cv, "icl_rx");
1186 refcount_init(&ic->ic_outstanding_pdus, 0);
1189 ic->ic_offload = "None";
1190 ic->ic_unmapped = false;
1196 icl_soft_conn_free(struct icl_conn *ic)
1198 struct icl_soft_conn *isc = (struct icl_soft_conn *)ic;
1201 KASSERT(ic->ic_outstanding_pdus == 0,
1202 ("destroying session with %d outstanding PDUs",
1203 ic->ic_outstanding_pdus));
1205 cv_destroy(&isc->send_cv);
1206 cv_destroy(&isc->receive_cv);
1207 kobj_delete((struct kobj *)isc, M_ICL_SOFT);
1208 refcount_release(&icl_ncons);
1212 icl_conn_start(struct icl_conn *ic)
1214 struct icl_soft_conn *isc = (struct icl_soft_conn *)ic;
1224 if (ic->ic_socket == NULL) {
1225 ICL_CONN_UNLOCK(ic);
1229 isc->receive_state = ICL_CONN_STATE_BHS;
1230 isc->receive_len = sizeof(struct iscsi_bhs);
1231 ic->ic_disconnecting = false;
1233 ICL_CONN_UNLOCK(ic);
1236 * For sendspace, this is required because the current code cannot
1237 * send a PDU in pieces; thus, the minimum buffer size is equal
1238 * to the maximum PDU size. "+4" is to account for possible padding.
1240 minspace = sizeof(struct iscsi_bhs) +
1241 ic->ic_max_send_data_segment_length +
1242 ISCSI_HEADER_DIGEST_SIZE + ISCSI_DATA_DIGEST_SIZE + 4;
1243 if (sendspace < minspace) {
1244 ICL_WARN("kern.icl.sendspace too low; must be at least %zd",
1246 sendspace = minspace;
1248 minspace = sizeof(struct iscsi_bhs) +
1249 ic->ic_max_recv_data_segment_length +
1250 ISCSI_HEADER_DIGEST_SIZE + ISCSI_DATA_DIGEST_SIZE + 4;
1251 if (recvspace < minspace) {
1252 ICL_WARN("kern.icl.recvspace too low; must be at least %zd",
1254 recvspace = minspace;
1257 error = soreserve(ic->ic_socket, sendspace, recvspace);
1259 ICL_WARN("soreserve failed with error %d", error);
1260 icl_soft_conn_close(ic);
1263 ic->ic_socket->so_snd.sb_flags |= SB_AUTOSIZE;
1264 ic->ic_socket->so_rcv.sb_flags |= SB_AUTOSIZE;
1269 bzero(&opt, sizeof(opt));
1270 opt.sopt_dir = SOPT_SET;
1271 opt.sopt_level = IPPROTO_TCP;
1272 opt.sopt_name = TCP_NODELAY;
1273 opt.sopt_val = &one;
1274 opt.sopt_valsize = sizeof(one);
1275 error = sosetopt(ic->ic_socket, &opt);
1277 ICL_WARN("disabling TCP_NODELAY failed with error %d", error);
1278 icl_soft_conn_close(ic);
1283 * Register socket upcall, to get notified about incoming PDUs
1284 * and free space to send outgoing ones.
1286 SOCKBUF_LOCK(&ic->ic_socket->so_snd);
1287 soupcall_set(ic->ic_socket, SO_SND, icl_soupcall_send, isc);
1288 SOCKBUF_UNLOCK(&ic->ic_socket->so_snd);
1289 SOCKBUF_LOCK(&ic->ic_socket->so_rcv);
1290 soupcall_set(ic->ic_socket, SO_RCV, icl_soupcall_receive, isc);
1291 SOCKBUF_UNLOCK(&ic->ic_socket->so_rcv);
1297 isc->send_running = isc->receive_running = true;
1298 ICL_CONN_UNLOCK(ic);
1299 error = kthread_add(icl_send_thread, ic, NULL, NULL, 0, 0, "%stx",
1302 ICL_WARN("kthread_add(9) failed with error %d", error);
1304 isc->send_running = isc->receive_running = false;
1305 cv_signal(&isc->send_cv);
1306 ICL_CONN_UNLOCK(ic);
1307 icl_soft_conn_close(ic);
1310 error = kthread_add(icl_receive_thread, ic, NULL, NULL, 0, 0, "%srx",
1313 ICL_WARN("kthread_add(9) failed with error %d", error);
1315 isc->receive_running = false;
1316 cv_signal(&isc->send_cv);
1317 ICL_CONN_UNLOCK(ic);
1318 icl_soft_conn_close(ic);
1326 icl_soft_conn_handoff(struct icl_conn *ic, int fd)
1330 cap_rights_t rights;
1333 ICL_CONN_LOCK_ASSERT_NOT(ic);
1335 #ifdef ICL_KERNEL_PROXY
1337 * We're transitioning to Full Feature phase, and we don't
1342 if (ic->ic_socket == NULL) {
1343 ICL_CONN_UNLOCK(ic);
1344 ICL_WARN("proxy handoff without connect");
1347 ICL_CONN_UNLOCK(ic);
1353 * Steal the socket from userland.
1355 error = fget(curthread, fd,
1356 cap_rights_init_one(&rights, CAP_SOCK_CLIENT), &fp);
1359 if (fp->f_type != DTYPE_SOCKET) {
1360 fdrop(fp, curthread);
1364 if (so->so_type != SOCK_STREAM) {
1365 fdrop(fp, curthread);
1371 if (ic->ic_socket != NULL) {
1372 ICL_CONN_UNLOCK(ic);
1373 fdrop(fp, curthread);
1377 ic->ic_socket = fp->f_data;
1378 fp->f_ops = &badfileops;
1380 fdrop(fp, curthread);
1381 ICL_CONN_UNLOCK(ic);
1383 error = icl_conn_start(ic);
1389 icl_soft_conn_close(struct icl_conn *ic)
1391 struct icl_soft_conn *isc = (struct icl_soft_conn *)ic;
1392 struct icl_pdu *pdu;
1396 * Wake up the threads, so they can properly terminate.
1397 * Receive thread sleeps on so->so_rcv lock, send on ic->ic_lock.
1400 if (!ic->ic_disconnecting) {
1403 SOCKBUF_LOCK(&so->so_rcv);
1404 ic->ic_disconnecting = true;
1406 SOCKBUF_UNLOCK(&so->so_rcv);
1408 while (isc->receive_running || isc->send_running) {
1409 cv_signal(&isc->receive_cv);
1410 cv_signal(&isc->send_cv);
1411 cv_wait(&isc->send_cv, ic->ic_lock);
1414 /* Some other thread could close the connection same time. */
1417 ICL_CONN_UNLOCK(ic);
1420 ic->ic_socket = NULL;
1423 * Deregister socket upcalls.
1425 ICL_CONN_UNLOCK(ic);
1426 SOCKBUF_LOCK(&so->so_snd);
1427 if (so->so_snd.sb_upcall != NULL)
1428 soupcall_clear(so, SO_SND);
1429 SOCKBUF_UNLOCK(&so->so_snd);
1430 SOCKBUF_LOCK(&so->so_rcv);
1431 if (so->so_rcv.sb_upcall != NULL)
1432 soupcall_clear(so, SO_RCV);
1433 SOCKBUF_UNLOCK(&so->so_rcv);
1437 if (isc->receive_pdu != NULL) {
1438 //ICL_DEBUG("freeing partially received PDU");
1439 icl_soft_conn_pdu_free(ic, isc->receive_pdu);
1440 isc->receive_pdu = NULL;
1444 * Remove any outstanding PDUs from the send queue.
1446 while (!STAILQ_EMPTY(&isc->to_send)) {
1447 pdu = STAILQ_FIRST(&isc->to_send);
1448 STAILQ_REMOVE_HEAD(&isc->to_send, ip_next);
1449 icl_soft_pdu_done(pdu, ENOTCONN);
1452 KASSERT(STAILQ_EMPTY(&isc->to_send),
1453 ("destroying session with non-empty send queue"));
1454 ICL_CONN_UNLOCK(ic);
1458 icl_soft_conn_task_setup(struct icl_conn *ic, struct icl_pdu *ip,
1459 struct ccb_scsiio *csio, uint32_t *task_tagp, void **prvp)
1466 icl_soft_conn_task_done(struct icl_conn *ic, void *prv)
1471 icl_soft_conn_transfer_setup(struct icl_conn *ic, union ctl_io *io,
1472 uint32_t *transfer_tag, void **prvp)
1479 icl_soft_conn_transfer_done(struct icl_conn *ic, void *prv)
1484 icl_soft_limits(struct icl_drv_limits *idl)
1487 idl->idl_max_recv_data_segment_length = max_data_segment_length;
1488 idl->idl_max_send_data_segment_length = max_data_segment_length;
1489 idl->idl_max_burst_length = max_burst_length;
1490 idl->idl_first_burst_length = first_burst_length;
1495 #ifdef ICL_KERNEL_PROXY
1497 icl_soft_conn_connect(struct icl_conn *ic, int domain, int socktype,
1498 int protocol, struct sockaddr *from_sa, struct sockaddr *to_sa)
1501 return (icl_soft_proxy_connect(ic, domain, socktype, protocol,
1506 icl_soft_handoff_sock(struct icl_conn *ic, struct socket *so)
1510 ICL_CONN_LOCK_ASSERT_NOT(ic);
1512 if (so->so_type != SOCK_STREAM)
1516 if (ic->ic_socket != NULL) {
1517 ICL_CONN_UNLOCK(ic);
1521 ICL_CONN_UNLOCK(ic);
1523 error = icl_conn_start(ic);
1527 #endif /* ICL_KERNEL_PROXY */
1534 icl_soft_pdu_zone = uma_zcreate("icl_soft_pdu",
1535 sizeof(struct icl_soft_pdu), NULL, NULL, NULL, NULL,
1537 refcount_init(&icl_ncons, 0);
1540 * The reason we call this "none" is that to the user,
1541 * it's known as "offload driver"; "offload driver: soft"
1542 * doesn't make much sense.
1544 error = icl_register("none", false, 0,
1545 icl_soft_limits, icl_soft_new_conn);
1546 KASSERT(error == 0, ("failed to register"));
1548 #if defined(ICL_KERNEL_PROXY) && 0
1550 * Debugging aid for kernel proxy functionality.
1552 error = icl_register("proxytest", true, 0,
1553 icl_soft_limits, icl_soft_new_conn);
1554 KASSERT(error == 0, ("failed to register"));
1561 icl_soft_unload(void)
1567 icl_unregister("none", false);
1568 #if defined(ICL_KERNEL_PROXY) && 0
1569 icl_unregister("proxytest", true);
1572 uma_zdestroy(icl_soft_pdu_zone);
1578 icl_soft_modevent(module_t mod, int what, void *arg)
1583 return (icl_soft_load());
1585 return (icl_soft_unload());
1591 moduledata_t icl_soft_data = {
1597 DECLARE_MODULE(icl_soft, icl_soft_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1598 MODULE_DEPEND(icl_soft, icl, 1, 1, 1);
1599 MODULE_VERSION(icl_soft, 1);