1321b17ecSEdward Tomasz Napierala /*- 243ee6e9dSEdward Tomasz Napierala * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 343ee6e9dSEdward Tomasz Napierala * 4321b17ecSEdward Tomasz Napierala * Copyright (c) 2012 The FreeBSD Foundation 5321b17ecSEdward Tomasz Napierala * 6321b17ecSEdward Tomasz Napierala * This software was developed by Edward Tomasz Napierala under sponsorship 7321b17ecSEdward Tomasz Napierala * from the FreeBSD Foundation. 8321b17ecSEdward Tomasz Napierala * 9321b17ecSEdward Tomasz Napierala * Redistribution and use in source and binary forms, with or without 10321b17ecSEdward Tomasz Napierala * modification, are permitted provided that the following conditions 11321b17ecSEdward Tomasz Napierala * are met: 12321b17ecSEdward Tomasz Napierala * 1. Redistributions of source code must retain the above copyright 13321b17ecSEdward Tomasz Napierala * notice, this list of conditions and the following disclaimer. 14321b17ecSEdward Tomasz Napierala * 2. Redistributions in binary form must reproduce the above copyright 15321b17ecSEdward Tomasz Napierala * notice, this list of conditions and the following disclaimer in the 16321b17ecSEdward Tomasz Napierala * documentation and/or other materials provided with the distribution. 17321b17ecSEdward Tomasz Napierala * 18321b17ecSEdward Tomasz Napierala * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19321b17ecSEdward Tomasz Napierala * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20321b17ecSEdward Tomasz Napierala * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21321b17ecSEdward Tomasz Napierala * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22321b17ecSEdward Tomasz Napierala * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23321b17ecSEdward Tomasz Napierala * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24321b17ecSEdward Tomasz Napierala * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25321b17ecSEdward Tomasz Napierala * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26321b17ecSEdward Tomasz Napierala * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27321b17ecSEdward Tomasz Napierala * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28321b17ecSEdward Tomasz Napierala * SUCH DAMAGE. 29321b17ecSEdward Tomasz Napierala * 30321b17ecSEdward Tomasz Napierala */ 31321b17ecSEdward Tomasz Napierala 32321b17ecSEdward Tomasz Napierala /* 335aabcd7cSEdward Tomasz Napierala * Software implementation of iSCSI Common Layer kobj(9) interface. 34321b17ecSEdward Tomasz Napierala */ 35321b17ecSEdward Tomasz Napierala 36321b17ecSEdward Tomasz Napierala #include <sys/cdefs.h> 37321b17ecSEdward Tomasz Napierala __FBSDID("$FreeBSD$"); 38321b17ecSEdward Tomasz Napierala 39321b17ecSEdward Tomasz Napierala #include <sys/param.h> 40321b17ecSEdward Tomasz Napierala #include <sys/capsicum.h> 41321b17ecSEdward Tomasz Napierala #include <sys/condvar.h> 42321b17ecSEdward Tomasz Napierala #include <sys/conf.h> 43f89d2072SXin LI #include <sys/gsb_crc32.h> 44321b17ecSEdward Tomasz Napierala #include <sys/file.h> 45321b17ecSEdward Tomasz Napierala #include <sys/kernel.h> 46321b17ecSEdward Tomasz Napierala #include <sys/kthread.h> 47321b17ecSEdward Tomasz Napierala #include <sys/lock.h> 48321b17ecSEdward Tomasz Napierala #include <sys/mbuf.h> 49321b17ecSEdward Tomasz Napierala #include <sys/mutex.h> 50321b17ecSEdward Tomasz Napierala #include <sys/module.h> 51321b17ecSEdward Tomasz Napierala #include <sys/protosw.h> 52321b17ecSEdward Tomasz Napierala #include <sys/socket.h> 53321b17ecSEdward Tomasz Napierala #include <sys/socketvar.h> 54321b17ecSEdward Tomasz Napierala #include <sys/sysctl.h> 55321b17ecSEdward Tomasz Napierala #include <sys/systm.h> 56321b17ecSEdward Tomasz Napierala #include <sys/sx.h> 57321b17ecSEdward Tomasz Napierala #include <sys/uio.h> 58321b17ecSEdward Tomasz Napierala #include <vm/uma.h> 59321b17ecSEdward Tomasz Napierala #include <netinet/in.h> 60321b17ecSEdward Tomasz Napierala #include <netinet/tcp.h> 61321b17ecSEdward Tomasz Napierala 62321b17ecSEdward Tomasz Napierala #include <dev/iscsi/icl.h> 63321b17ecSEdward Tomasz Napierala #include <dev/iscsi/iscsi_proto.h> 64321b17ecSEdward Tomasz Napierala #include <icl_conn_if.h> 65321b17ecSEdward Tomasz Napierala 6687322a90SJohn Baldwin #define ICL_CONN_STATE_BHS 1 6787322a90SJohn Baldwin #define ICL_CONN_STATE_AHS 2 6887322a90SJohn Baldwin #define ICL_CONN_STATE_HEADER_DIGEST 3 6987322a90SJohn Baldwin #define ICL_CONN_STATE_DATA 4 7087322a90SJohn Baldwin #define ICL_CONN_STATE_DATA_DIGEST 5 7187322a90SJohn Baldwin 7287322a90SJohn Baldwin struct icl_soft_conn { 7387322a90SJohn Baldwin struct icl_conn ic; 7487322a90SJohn Baldwin 7587322a90SJohn Baldwin /* soft specific stuff goes here. */ 7687322a90SJohn Baldwin STAILQ_HEAD(, icl_pdu) to_send; 7787322a90SJohn Baldwin struct cv send_cv; 7887322a90SJohn Baldwin struct cv receive_cv; 7987322a90SJohn Baldwin struct icl_pdu *receive_pdu; 8087322a90SJohn Baldwin size_t receive_len; 8187322a90SJohn Baldwin int receive_state; 8287322a90SJohn Baldwin bool receive_running; 8387322a90SJohn Baldwin bool check_send_space; 8487322a90SJohn Baldwin bool send_running; 8587322a90SJohn Baldwin }; 8687322a90SJohn Baldwin 879a4510acSAlexander Motin struct icl_soft_pdu { 889a4510acSAlexander Motin struct icl_pdu ip; 899a4510acSAlexander Motin 909a4510acSAlexander Motin /* soft specific stuff goes here. */ 919a4510acSAlexander Motin u_int ref_cnt; 929a4510acSAlexander Motin icl_pdu_cb cb; 939a4510acSAlexander Motin int error; 949a4510acSAlexander Motin }; 959a4510acSAlexander Motin 96b75168edSAlexander Motin SYSCTL_NODE(_kern_icl, OID_AUTO, soft, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 97b75168edSAlexander Motin "Software iSCSI"); 98321b17ecSEdward Tomasz Napierala static int coalesce = 1; 99b75168edSAlexander Motin SYSCTL_INT(_kern_icl_soft, OID_AUTO, coalesce, CTLFLAG_RWTUN, 100321b17ecSEdward Tomasz Napierala &coalesce, 0, "Try to coalesce PDUs before sending"); 101b75168edSAlexander Motin static int partial_receive_len = 256 * 1024; 102b75168edSAlexander Motin SYSCTL_INT(_kern_icl_soft, OID_AUTO, partial_receive_len, CTLFLAG_RWTUN, 103321b17ecSEdward Tomasz Napierala &partial_receive_len, 0, "Minimum read size for partially received " 104321b17ecSEdward Tomasz Napierala "data segment"); 105b75168edSAlexander Motin static int max_data_segment_length = 256 * 1024; 106b75168edSAlexander Motin SYSCTL_INT(_kern_icl_soft, OID_AUTO, max_data_segment_length, CTLFLAG_RWTUN, 107b75168edSAlexander Motin &max_data_segment_length, 0, "Maximum data segment length"); 108b75168edSAlexander Motin static int first_burst_length = 1024 * 1024; 109b75168edSAlexander Motin SYSCTL_INT(_kern_icl_soft, OID_AUTO, first_burst_length, CTLFLAG_RWTUN, 110b75168edSAlexander Motin &first_burst_length, 0, "First burst length"); 111b75168edSAlexander Motin static int max_burst_length = 1024 * 1024; 112b75168edSAlexander Motin SYSCTL_INT(_kern_icl_soft, OID_AUTO, max_burst_length, CTLFLAG_RWTUN, 113b75168edSAlexander Motin &max_burst_length, 0, "Maximum burst length"); 114b75168edSAlexander Motin static int sendspace = 1536 * 1024; 115b75168edSAlexander Motin SYSCTL_INT(_kern_icl_soft, OID_AUTO, sendspace, CTLFLAG_RWTUN, 116321b17ecSEdward Tomasz Napierala &sendspace, 0, "Default send socket buffer size"); 117b75168edSAlexander Motin static int recvspace = 1536 * 1024; 118b75168edSAlexander Motin SYSCTL_INT(_kern_icl_soft, OID_AUTO, recvspace, CTLFLAG_RWTUN, 119321b17ecSEdward Tomasz Napierala &recvspace, 0, "Default receive socket buffer size"); 120321b17ecSEdward Tomasz Napierala 121321b17ecSEdward Tomasz Napierala static MALLOC_DEFINE(M_ICL_SOFT, "icl_soft", "iSCSI software backend"); 1229a4510acSAlexander Motin static uma_zone_t icl_soft_pdu_zone; 123321b17ecSEdward Tomasz Napierala 124321b17ecSEdward Tomasz Napierala static volatile u_int icl_ncons; 125321b17ecSEdward Tomasz Napierala 126321b17ecSEdward Tomasz Napierala STAILQ_HEAD(icl_pdu_stailq, icl_pdu); 127321b17ecSEdward Tomasz Napierala 128321b17ecSEdward Tomasz Napierala static icl_conn_new_pdu_t icl_soft_conn_new_pdu; 129321b17ecSEdward Tomasz Napierala static icl_conn_pdu_free_t icl_soft_conn_pdu_free; 130321b17ecSEdward Tomasz Napierala static icl_conn_pdu_data_segment_length_t 131321b17ecSEdward Tomasz Napierala icl_soft_conn_pdu_data_segment_length; 132321b17ecSEdward Tomasz Napierala static icl_conn_pdu_append_data_t icl_soft_conn_pdu_append_data; 133321b17ecSEdward Tomasz Napierala static icl_conn_pdu_get_data_t icl_soft_conn_pdu_get_data; 134321b17ecSEdward Tomasz Napierala static icl_conn_pdu_queue_t icl_soft_conn_pdu_queue; 1359a4510acSAlexander Motin static icl_conn_pdu_queue_cb_t icl_soft_conn_pdu_queue_cb; 136321b17ecSEdward Tomasz Napierala static icl_conn_handoff_t icl_soft_conn_handoff; 137321b17ecSEdward Tomasz Napierala static icl_conn_free_t icl_soft_conn_free; 138321b17ecSEdward Tomasz Napierala static icl_conn_close_t icl_soft_conn_close; 1397a03d007SEdward Tomasz Napierala static icl_conn_task_setup_t icl_soft_conn_task_setup; 1407a03d007SEdward Tomasz Napierala static icl_conn_task_done_t icl_soft_conn_task_done; 1417a03d007SEdward Tomasz Napierala static icl_conn_transfer_setup_t icl_soft_conn_transfer_setup; 1427a03d007SEdward Tomasz Napierala static icl_conn_transfer_done_t icl_soft_conn_transfer_done; 143f41492b0SEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 144f41492b0SEdward Tomasz Napierala static icl_conn_connect_t icl_soft_conn_connect; 145f41492b0SEdward Tomasz Napierala #endif 146321b17ecSEdward Tomasz Napierala 147321b17ecSEdward Tomasz Napierala static kobj_method_t icl_soft_methods[] = { 148321b17ecSEdward Tomasz Napierala KOBJMETHOD(icl_conn_new_pdu, icl_soft_conn_new_pdu), 149321b17ecSEdward Tomasz Napierala KOBJMETHOD(icl_conn_pdu_free, icl_soft_conn_pdu_free), 150321b17ecSEdward Tomasz Napierala KOBJMETHOD(icl_conn_pdu_data_segment_length, 151321b17ecSEdward Tomasz Napierala icl_soft_conn_pdu_data_segment_length), 152321b17ecSEdward Tomasz Napierala KOBJMETHOD(icl_conn_pdu_append_data, icl_soft_conn_pdu_append_data), 153321b17ecSEdward Tomasz Napierala KOBJMETHOD(icl_conn_pdu_get_data, icl_soft_conn_pdu_get_data), 154321b17ecSEdward Tomasz Napierala KOBJMETHOD(icl_conn_pdu_queue, icl_soft_conn_pdu_queue), 1559a4510acSAlexander Motin KOBJMETHOD(icl_conn_pdu_queue_cb, icl_soft_conn_pdu_queue_cb), 156321b17ecSEdward Tomasz Napierala KOBJMETHOD(icl_conn_handoff, icl_soft_conn_handoff), 157321b17ecSEdward Tomasz Napierala KOBJMETHOD(icl_conn_free, icl_soft_conn_free), 158321b17ecSEdward Tomasz Napierala KOBJMETHOD(icl_conn_close, icl_soft_conn_close), 1597a03d007SEdward Tomasz Napierala KOBJMETHOD(icl_conn_task_setup, icl_soft_conn_task_setup), 1607a03d007SEdward Tomasz Napierala KOBJMETHOD(icl_conn_task_done, icl_soft_conn_task_done), 1617a03d007SEdward Tomasz Napierala KOBJMETHOD(icl_conn_transfer_setup, icl_soft_conn_transfer_setup), 1627a03d007SEdward Tomasz Napierala KOBJMETHOD(icl_conn_transfer_done, icl_soft_conn_transfer_done), 163f41492b0SEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 164f41492b0SEdward Tomasz Napierala KOBJMETHOD(icl_conn_connect, icl_soft_conn_connect), 165f41492b0SEdward Tomasz Napierala #endif 166321b17ecSEdward Tomasz Napierala { 0, 0 } 167321b17ecSEdward Tomasz Napierala }; 168321b17ecSEdward Tomasz Napierala 16987322a90SJohn Baldwin DEFINE_CLASS(icl_soft, icl_soft_methods, sizeof(struct icl_soft_conn)); 170321b17ecSEdward Tomasz Napierala 171321b17ecSEdward Tomasz Napierala static void 172321b17ecSEdward Tomasz Napierala icl_conn_fail(struct icl_conn *ic) 173321b17ecSEdward Tomasz Napierala { 174321b17ecSEdward Tomasz Napierala if (ic->ic_socket == NULL) 175321b17ecSEdward Tomasz Napierala return; 176321b17ecSEdward Tomasz Napierala 177321b17ecSEdward Tomasz Napierala /* 178321b17ecSEdward Tomasz Napierala * XXX 179321b17ecSEdward Tomasz Napierala */ 180321b17ecSEdward Tomasz Napierala ic->ic_socket->so_error = EDOOFUS; 181321b17ecSEdward Tomasz Napierala (ic->ic_error)(ic); 182321b17ecSEdward Tomasz Napierala } 183321b17ecSEdward Tomasz Napierala 18482f7fa7aSAlexander Motin static void 18582f7fa7aSAlexander Motin icl_soft_conn_pdu_free(struct icl_conn *ic, struct icl_pdu *ip) 18682f7fa7aSAlexander Motin { 1879a4510acSAlexander Motin struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip; 18882f7fa7aSAlexander Motin 1899a4510acSAlexander Motin KASSERT(isp->ref_cnt == 0, ("freeing active PDU")); 19082f7fa7aSAlexander Motin m_freem(ip->ip_bhs_mbuf); 19182f7fa7aSAlexander Motin m_freem(ip->ip_ahs_mbuf); 19282f7fa7aSAlexander Motin m_freem(ip->ip_data_mbuf); 1939a4510acSAlexander Motin uma_zfree(icl_soft_pdu_zone, isp); 19482f7fa7aSAlexander Motin #ifdef DIAGNOSTIC 19582f7fa7aSAlexander Motin refcount_release(&ic->ic_outstanding_pdus); 19682f7fa7aSAlexander Motin #endif 19782f7fa7aSAlexander Motin } 19882f7fa7aSAlexander Motin 1999a4510acSAlexander Motin static void 2009a4510acSAlexander Motin icl_soft_pdu_call_cb(struct icl_pdu *ip) 2019a4510acSAlexander Motin { 2029a4510acSAlexander Motin struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip; 2039a4510acSAlexander Motin 2049a4510acSAlexander Motin if (isp->cb != NULL) 2059a4510acSAlexander Motin isp->cb(ip, isp->error); 2069a4510acSAlexander Motin #ifdef DIAGNOSTIC 2079a4510acSAlexander Motin refcount_release(&ip->ip_conn->ic_outstanding_pdus); 2089a4510acSAlexander Motin #endif 2099a4510acSAlexander Motin uma_zfree(icl_soft_pdu_zone, isp); 2109a4510acSAlexander Motin } 2119a4510acSAlexander Motin 2129a4510acSAlexander Motin static void 2139a4510acSAlexander Motin icl_soft_pdu_done(struct icl_pdu *ip, int error) 2149a4510acSAlexander Motin { 2159a4510acSAlexander Motin struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip; 2169a4510acSAlexander Motin 2179a4510acSAlexander Motin if (error != 0) 2189a4510acSAlexander Motin isp->error = error; 2199a4510acSAlexander Motin 2209a4510acSAlexander Motin m_freem(ip->ip_bhs_mbuf); 2219a4510acSAlexander Motin ip->ip_bhs_mbuf = NULL; 2229a4510acSAlexander Motin m_freem(ip->ip_ahs_mbuf); 2239a4510acSAlexander Motin ip->ip_ahs_mbuf = NULL; 2249a4510acSAlexander Motin m_freem(ip->ip_data_mbuf); 2259a4510acSAlexander Motin ip->ip_data_mbuf = NULL; 2269a4510acSAlexander Motin 2279a4510acSAlexander Motin if (atomic_fetchadd_int(&isp->ref_cnt, -1) == 1) 2289a4510acSAlexander Motin icl_soft_pdu_call_cb(ip); 2299a4510acSAlexander Motin } 2309a4510acSAlexander Motin 2319a4510acSAlexander Motin static void 2329a4510acSAlexander Motin icl_soft_mbuf_done(struct mbuf *mb) 2339a4510acSAlexander Motin { 2349a4510acSAlexander Motin struct icl_soft_pdu *isp = (struct icl_soft_pdu *)mb->m_ext.ext_arg1; 2359a4510acSAlexander Motin 2369a4510acSAlexander Motin icl_soft_pdu_call_cb(&isp->ip); 2379a4510acSAlexander Motin } 2389a4510acSAlexander Motin 23982f7fa7aSAlexander Motin /* 24082f7fa7aSAlexander Motin * Allocate icl_pdu with empty BHS to fill up by the caller. 24182f7fa7aSAlexander Motin */ 24282f7fa7aSAlexander Motin struct icl_pdu * 24382f7fa7aSAlexander Motin icl_soft_conn_new_pdu(struct icl_conn *ic, int flags) 244321b17ecSEdward Tomasz Napierala { 2459a4510acSAlexander Motin struct icl_soft_pdu *isp; 246321b17ecSEdward Tomasz Napierala struct icl_pdu *ip; 247321b17ecSEdward Tomasz Napierala 248321b17ecSEdward Tomasz Napierala #ifdef DIAGNOSTIC 249321b17ecSEdward Tomasz Napierala refcount_acquire(&ic->ic_outstanding_pdus); 250321b17ecSEdward Tomasz Napierala #endif 2519a4510acSAlexander Motin isp = uma_zalloc(icl_soft_pdu_zone, flags | M_ZERO); 2529a4510acSAlexander Motin if (isp == NULL) { 2539a4510acSAlexander Motin ICL_WARN("failed to allocate soft PDU"); 254321b17ecSEdward Tomasz Napierala #ifdef DIAGNOSTIC 255321b17ecSEdward Tomasz Napierala refcount_release(&ic->ic_outstanding_pdus); 256321b17ecSEdward Tomasz Napierala #endif 257321b17ecSEdward Tomasz Napierala return (NULL); 258321b17ecSEdward Tomasz Napierala } 2599a4510acSAlexander Motin ip = &isp->ip; 260321b17ecSEdward Tomasz Napierala ip->ip_conn = ic; 261321b17ecSEdward Tomasz Napierala 26233d9db92SAlexander Motin CTASSERT(sizeof(struct iscsi_bhs) <= MHLEN); 26333d9db92SAlexander Motin ip->ip_bhs_mbuf = m_gethdr(flags, MT_DATA); 264321b17ecSEdward Tomasz Napierala if (ip->ip_bhs_mbuf == NULL) { 265d0d587c7SAlexander Motin ICL_WARN("failed to allocate BHS mbuf"); 26682f7fa7aSAlexander Motin icl_soft_conn_pdu_free(ic, ip); 267321b17ecSEdward Tomasz Napierala return (NULL); 268321b17ecSEdward Tomasz Napierala } 269321b17ecSEdward Tomasz Napierala ip->ip_bhs = mtod(ip->ip_bhs_mbuf, struct iscsi_bhs *); 270321b17ecSEdward Tomasz Napierala memset(ip->ip_bhs, 0, sizeof(struct iscsi_bhs)); 271321b17ecSEdward Tomasz Napierala ip->ip_bhs_mbuf->m_len = sizeof(struct iscsi_bhs); 272321b17ecSEdward Tomasz Napierala 273321b17ecSEdward Tomasz Napierala return (ip); 274321b17ecSEdward Tomasz Napierala } 275321b17ecSEdward Tomasz Napierala 276321b17ecSEdward Tomasz Napierala static int 277321b17ecSEdward Tomasz Napierala icl_pdu_ahs_length(const struct icl_pdu *request) 278321b17ecSEdward Tomasz Napierala { 279321b17ecSEdward Tomasz Napierala 280321b17ecSEdward Tomasz Napierala return (request->ip_bhs->bhs_total_ahs_len * 4); 281321b17ecSEdward Tomasz Napierala } 282321b17ecSEdward Tomasz Napierala 283321b17ecSEdward Tomasz Napierala static size_t 284321b17ecSEdward Tomasz Napierala icl_pdu_data_segment_length(const struct icl_pdu *request) 285321b17ecSEdward Tomasz Napierala { 286321b17ecSEdward Tomasz Napierala uint32_t len = 0; 287321b17ecSEdward Tomasz Napierala 288321b17ecSEdward Tomasz Napierala len += request->ip_bhs->bhs_data_segment_len[0]; 289321b17ecSEdward Tomasz Napierala len <<= 8; 290321b17ecSEdward Tomasz Napierala len += request->ip_bhs->bhs_data_segment_len[1]; 291321b17ecSEdward Tomasz Napierala len <<= 8; 292321b17ecSEdward Tomasz Napierala len += request->ip_bhs->bhs_data_segment_len[2]; 293321b17ecSEdward Tomasz Napierala 294321b17ecSEdward Tomasz Napierala return (len); 295321b17ecSEdward Tomasz Napierala } 296321b17ecSEdward Tomasz Napierala 297321b17ecSEdward Tomasz Napierala size_t 298321b17ecSEdward Tomasz Napierala icl_soft_conn_pdu_data_segment_length(struct icl_conn *ic, 299321b17ecSEdward Tomasz Napierala const struct icl_pdu *request) 300321b17ecSEdward Tomasz Napierala { 301321b17ecSEdward Tomasz Napierala 302321b17ecSEdward Tomasz Napierala return (icl_pdu_data_segment_length(request)); 303321b17ecSEdward Tomasz Napierala } 304321b17ecSEdward Tomasz Napierala 305321b17ecSEdward Tomasz Napierala static void 306321b17ecSEdward Tomasz Napierala icl_pdu_set_data_segment_length(struct icl_pdu *response, uint32_t len) 307321b17ecSEdward Tomasz Napierala { 308321b17ecSEdward Tomasz Napierala 309321b17ecSEdward Tomasz Napierala response->ip_bhs->bhs_data_segment_len[2] = len; 310321b17ecSEdward Tomasz Napierala response->ip_bhs->bhs_data_segment_len[1] = len >> 8; 311321b17ecSEdward Tomasz Napierala response->ip_bhs->bhs_data_segment_len[0] = len >> 16; 312321b17ecSEdward Tomasz Napierala } 313321b17ecSEdward Tomasz Napierala 314321b17ecSEdward Tomasz Napierala static size_t 315321b17ecSEdward Tomasz Napierala icl_pdu_padding(const struct icl_pdu *ip) 316321b17ecSEdward Tomasz Napierala { 317321b17ecSEdward Tomasz Napierala 318321b17ecSEdward Tomasz Napierala if ((ip->ip_data_len % 4) != 0) 319321b17ecSEdward Tomasz Napierala return (4 - (ip->ip_data_len % 4)); 320321b17ecSEdward Tomasz Napierala 321321b17ecSEdward Tomasz Napierala return (0); 322321b17ecSEdward Tomasz Napierala } 323321b17ecSEdward Tomasz Napierala 324321b17ecSEdward Tomasz Napierala static size_t 325321b17ecSEdward Tomasz Napierala icl_pdu_size(const struct icl_pdu *response) 326321b17ecSEdward Tomasz Napierala { 327321b17ecSEdward Tomasz Napierala size_t len; 328321b17ecSEdward Tomasz Napierala 329321b17ecSEdward Tomasz Napierala KASSERT(response->ip_ahs_len == 0, ("responding with AHS")); 330321b17ecSEdward Tomasz Napierala 331321b17ecSEdward Tomasz Napierala len = sizeof(struct iscsi_bhs) + response->ip_data_len + 332321b17ecSEdward Tomasz Napierala icl_pdu_padding(response); 333321b17ecSEdward Tomasz Napierala if (response->ip_conn->ic_header_crc32c) 334321b17ecSEdward Tomasz Napierala len += ISCSI_HEADER_DIGEST_SIZE; 335321b17ecSEdward Tomasz Napierala if (response->ip_data_len != 0 && response->ip_conn->ic_data_crc32c) 336321b17ecSEdward Tomasz Napierala len += ISCSI_DATA_DIGEST_SIZE; 337321b17ecSEdward Tomasz Napierala 338321b17ecSEdward Tomasz Napierala return (len); 339321b17ecSEdward Tomasz Napierala } 340321b17ecSEdward Tomasz Napierala 3416895f89fSAlexander Motin static void 3426895f89fSAlexander Motin icl_soft_receive_buf(struct mbuf **r, size_t *rs, void *buf, size_t s) 343321b17ecSEdward Tomasz Napierala { 344321b17ecSEdward Tomasz Napierala 3456895f89fSAlexander Motin m_copydata(*r, 0, s, buf); 3466895f89fSAlexander Motin m_adj(*r, s); 3476895f89fSAlexander Motin while ((*r) != NULL && (*r)->m_len == 0) 3486895f89fSAlexander Motin *r = m_free(*r); 3496895f89fSAlexander Motin *rs -= s; 350321b17ecSEdward Tomasz Napierala } 351321b17ecSEdward Tomasz Napierala 3526895f89fSAlexander Motin static void 3536895f89fSAlexander Motin icl_pdu_receive_ahs(struct icl_pdu *request, struct mbuf **r, size_t *rs) 354321b17ecSEdward Tomasz Napierala { 355321b17ecSEdward Tomasz Napierala 356321b17ecSEdward Tomasz Napierala request->ip_ahs_len = icl_pdu_ahs_length(request); 357321b17ecSEdward Tomasz Napierala if (request->ip_ahs_len == 0) 3586895f89fSAlexander Motin return; 359321b17ecSEdward Tomasz Napierala 3606895f89fSAlexander Motin request->ip_ahs_mbuf = *r; 3616895f89fSAlexander Motin *r = m_split(request->ip_ahs_mbuf, request->ip_ahs_len, M_WAITOK); 3626895f89fSAlexander Motin *rs -= request->ip_ahs_len; 363321b17ecSEdward Tomasz Napierala } 364321b17ecSEdward Tomasz Napierala 365321b17ecSEdward Tomasz Napierala static uint32_t 366321b17ecSEdward Tomasz Napierala icl_mbuf_to_crc32c(const struct mbuf *m0) 367321b17ecSEdward Tomasz Napierala { 368321b17ecSEdward Tomasz Napierala uint32_t digest = 0xffffffff; 369321b17ecSEdward Tomasz Napierala const struct mbuf *m; 370321b17ecSEdward Tomasz Napierala 371321b17ecSEdward Tomasz Napierala for (m = m0; m != NULL; m = m->m_next) 372321b17ecSEdward Tomasz Napierala digest = calculate_crc32c(digest, 373321b17ecSEdward Tomasz Napierala mtod(m, const void *), m->m_len); 374321b17ecSEdward Tomasz Napierala 375321b17ecSEdward Tomasz Napierala digest = digest ^ 0xffffffff; 376321b17ecSEdward Tomasz Napierala 377321b17ecSEdward Tomasz Napierala return (digest); 378321b17ecSEdward Tomasz Napierala } 379321b17ecSEdward Tomasz Napierala 380321b17ecSEdward Tomasz Napierala static int 3816895f89fSAlexander Motin icl_pdu_check_header_digest(struct icl_pdu *request, struct mbuf **r, size_t *rs) 382321b17ecSEdward Tomasz Napierala { 383321b17ecSEdward Tomasz Napierala uint32_t received_digest, valid_digest; 384321b17ecSEdward Tomasz Napierala 385321b17ecSEdward Tomasz Napierala if (request->ip_conn->ic_header_crc32c == false) 386321b17ecSEdward Tomasz Napierala return (0); 387321b17ecSEdward Tomasz Napierala 388d0d587c7SAlexander Motin CTASSERT(sizeof(received_digest) == ISCSI_HEADER_DIGEST_SIZE); 3896895f89fSAlexander Motin icl_soft_receive_buf(r, rs, &received_digest, ISCSI_HEADER_DIGEST_SIZE); 390321b17ecSEdward Tomasz Napierala 391875ac6cfSAlexander Motin /* Temporary attach AHS to BHS to calculate header digest. */ 392875ac6cfSAlexander Motin request->ip_bhs_mbuf->m_next = request->ip_ahs_mbuf; 393321b17ecSEdward Tomasz Napierala valid_digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf); 394875ac6cfSAlexander Motin request->ip_bhs_mbuf->m_next = NULL; 395321b17ecSEdward Tomasz Napierala if (received_digest != valid_digest) { 396321b17ecSEdward Tomasz Napierala ICL_WARN("header digest check failed; got 0x%x, " 397321b17ecSEdward Tomasz Napierala "should be 0x%x", received_digest, valid_digest); 398321b17ecSEdward Tomasz Napierala return (-1); 399321b17ecSEdward Tomasz Napierala } 400321b17ecSEdward Tomasz Napierala 401321b17ecSEdward Tomasz Napierala return (0); 402321b17ecSEdward Tomasz Napierala } 403321b17ecSEdward Tomasz Napierala 404321b17ecSEdward Tomasz Napierala /* 405321b17ecSEdward Tomasz Napierala * Return the number of bytes that should be waiting in the receive socket 406321b17ecSEdward Tomasz Napierala * before icl_pdu_receive_data_segment() gets called. 407321b17ecSEdward Tomasz Napierala */ 408321b17ecSEdward Tomasz Napierala static size_t 409321b17ecSEdward Tomasz Napierala icl_pdu_data_segment_receive_len(const struct icl_pdu *request) 410321b17ecSEdward Tomasz Napierala { 411321b17ecSEdward Tomasz Napierala size_t len; 412321b17ecSEdward Tomasz Napierala 413321b17ecSEdward Tomasz Napierala len = icl_pdu_data_segment_length(request); 414321b17ecSEdward Tomasz Napierala if (len == 0) 415321b17ecSEdward Tomasz Napierala return (0); 416321b17ecSEdward Tomasz Napierala 417321b17ecSEdward Tomasz Napierala /* 418321b17ecSEdward Tomasz Napierala * Account for the parts of data segment already read from 419321b17ecSEdward Tomasz Napierala * the socket buffer. 420321b17ecSEdward Tomasz Napierala */ 421321b17ecSEdward Tomasz Napierala KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len")); 422321b17ecSEdward Tomasz Napierala len -= request->ip_data_len; 423321b17ecSEdward Tomasz Napierala 424321b17ecSEdward Tomasz Napierala /* 425321b17ecSEdward Tomasz Napierala * Don't always wait for the full data segment to be delivered 426321b17ecSEdward Tomasz Napierala * to the socket; this might badly affect performance due to 427321b17ecSEdward Tomasz Napierala * TCP window scaling. 428321b17ecSEdward Tomasz Napierala */ 429321b17ecSEdward Tomasz Napierala if (len > partial_receive_len) { 430321b17ecSEdward Tomasz Napierala #if 0 431321b17ecSEdward Tomasz Napierala ICL_DEBUG("need %zd bytes of data, limiting to %zd", 432321b17ecSEdward Tomasz Napierala len, partial_receive_len)); 433321b17ecSEdward Tomasz Napierala #endif 434321b17ecSEdward Tomasz Napierala len = partial_receive_len; 435321b17ecSEdward Tomasz Napierala 436321b17ecSEdward Tomasz Napierala return (len); 437321b17ecSEdward Tomasz Napierala } 438321b17ecSEdward Tomasz Napierala 439321b17ecSEdward Tomasz Napierala /* 440321b17ecSEdward Tomasz Napierala * Account for padding. Note that due to the way code is written, 441321b17ecSEdward Tomasz Napierala * the icl_pdu_receive_data_segment() must always receive padding 442321b17ecSEdward Tomasz Napierala * along with the last part of data segment, because it would be 443321b17ecSEdward Tomasz Napierala * impossible to tell whether we've already received the full data 444321b17ecSEdward Tomasz Napierala * segment including padding, or without it. 445321b17ecSEdward Tomasz Napierala */ 446321b17ecSEdward Tomasz Napierala if ((len % 4) != 0) 447321b17ecSEdward Tomasz Napierala len += 4 - (len % 4); 448321b17ecSEdward Tomasz Napierala 449321b17ecSEdward Tomasz Napierala #if 0 450321b17ecSEdward Tomasz Napierala ICL_DEBUG("need %zd bytes of data", len)); 451321b17ecSEdward Tomasz Napierala #endif 452321b17ecSEdward Tomasz Napierala 453321b17ecSEdward Tomasz Napierala return (len); 454321b17ecSEdward Tomasz Napierala } 455321b17ecSEdward Tomasz Napierala 456321b17ecSEdward Tomasz Napierala static int 4576895f89fSAlexander Motin icl_pdu_receive_data_segment(struct icl_pdu *request, struct mbuf **r, 4586895f89fSAlexander Motin size_t *rs, bool *more_neededp) 459321b17ecSEdward Tomasz Napierala { 46087322a90SJohn Baldwin struct icl_soft_conn *isc; 461321b17ecSEdward Tomasz Napierala size_t len, padding = 0; 462321b17ecSEdward Tomasz Napierala struct mbuf *m; 463321b17ecSEdward Tomasz Napierala 46487322a90SJohn Baldwin isc = (struct icl_soft_conn *)request->ip_conn; 465321b17ecSEdward Tomasz Napierala 466321b17ecSEdward Tomasz Napierala *more_neededp = false; 46787322a90SJohn Baldwin isc->receive_len = 0; 468321b17ecSEdward Tomasz Napierala 469321b17ecSEdward Tomasz Napierala len = icl_pdu_data_segment_length(request); 470321b17ecSEdward Tomasz Napierala if (len == 0) 471321b17ecSEdward Tomasz Napierala return (0); 472321b17ecSEdward Tomasz Napierala 473321b17ecSEdward Tomasz Napierala if ((len % 4) != 0) 474321b17ecSEdward Tomasz Napierala padding = 4 - (len % 4); 475321b17ecSEdward Tomasz Napierala 476321b17ecSEdward Tomasz Napierala /* 477321b17ecSEdward Tomasz Napierala * Account for already received parts of data segment. 478321b17ecSEdward Tomasz Napierala */ 479321b17ecSEdward Tomasz Napierala KASSERT(len > request->ip_data_len, ("len <= request->ip_data_len")); 480321b17ecSEdward Tomasz Napierala len -= request->ip_data_len; 481321b17ecSEdward Tomasz Napierala 4826895f89fSAlexander Motin if (len + padding > *rs) { 483321b17ecSEdward Tomasz Napierala /* 484321b17ecSEdward Tomasz Napierala * Not enough data in the socket buffer. Receive as much 485321b17ecSEdward Tomasz Napierala * as we can. Don't receive padding, since, obviously, it's 486321b17ecSEdward Tomasz Napierala * not the end of data segment yet. 487321b17ecSEdward Tomasz Napierala */ 488321b17ecSEdward Tomasz Napierala #if 0 489321b17ecSEdward Tomasz Napierala ICL_DEBUG("limited from %zd to %zd", 4906895f89fSAlexander Motin len + padding, *rs - padding)); 491321b17ecSEdward Tomasz Napierala #endif 4926895f89fSAlexander Motin len = *rs - padding; 493321b17ecSEdward Tomasz Napierala *more_neededp = true; 494321b17ecSEdward Tomasz Napierala padding = 0; 495321b17ecSEdward Tomasz Napierala } 496321b17ecSEdward Tomasz Napierala 497321b17ecSEdward Tomasz Napierala /* 498321b17ecSEdward Tomasz Napierala * Must not try to receive padding without at least one byte 499321b17ecSEdward Tomasz Napierala * of actual data segment. 500321b17ecSEdward Tomasz Napierala */ 501321b17ecSEdward Tomasz Napierala if (len > 0) { 5026895f89fSAlexander Motin m = *r; 5036895f89fSAlexander Motin *r = m_split(m, len + padding, M_WAITOK); 5046895f89fSAlexander Motin *rs -= len + padding; 505321b17ecSEdward Tomasz Napierala 506321b17ecSEdward Tomasz Napierala if (request->ip_data_mbuf == NULL) 507321b17ecSEdward Tomasz Napierala request->ip_data_mbuf = m; 508321b17ecSEdward Tomasz Napierala else 509321b17ecSEdward Tomasz Napierala m_cat(request->ip_data_mbuf, m); 510321b17ecSEdward Tomasz Napierala 511321b17ecSEdward Tomasz Napierala request->ip_data_len += len; 512321b17ecSEdward Tomasz Napierala } else 513321b17ecSEdward Tomasz Napierala ICL_DEBUG("len 0"); 514321b17ecSEdward Tomasz Napierala 515321b17ecSEdward Tomasz Napierala if (*more_neededp) 51687322a90SJohn Baldwin isc->receive_len = icl_pdu_data_segment_receive_len(request); 517321b17ecSEdward Tomasz Napierala 518321b17ecSEdward Tomasz Napierala return (0); 519321b17ecSEdward Tomasz Napierala } 520321b17ecSEdward Tomasz Napierala 521321b17ecSEdward Tomasz Napierala static int 5226895f89fSAlexander Motin icl_pdu_check_data_digest(struct icl_pdu *request, struct mbuf **r, size_t *rs) 523321b17ecSEdward Tomasz Napierala { 524321b17ecSEdward Tomasz Napierala uint32_t received_digest, valid_digest; 525321b17ecSEdward Tomasz Napierala 526321b17ecSEdward Tomasz Napierala if (request->ip_conn->ic_data_crc32c == false) 527321b17ecSEdward Tomasz Napierala return (0); 528321b17ecSEdward Tomasz Napierala 529321b17ecSEdward Tomasz Napierala if (request->ip_data_len == 0) 530321b17ecSEdward Tomasz Napierala return (0); 531321b17ecSEdward Tomasz Napierala 532d0d587c7SAlexander Motin CTASSERT(sizeof(received_digest) == ISCSI_DATA_DIGEST_SIZE); 5336895f89fSAlexander Motin icl_soft_receive_buf(r, rs, &received_digest, ISCSI_DATA_DIGEST_SIZE); 534321b17ecSEdward Tomasz Napierala 535321b17ecSEdward Tomasz Napierala /* 536321b17ecSEdward Tomasz Napierala * Note that ip_data_mbuf also contains padding; since digest 537321b17ecSEdward Tomasz Napierala * calculation is supposed to include that, we iterate over 538321b17ecSEdward Tomasz Napierala * the entire ip_data_mbuf chain, not just ip_data_len bytes of it. 539321b17ecSEdward Tomasz Napierala */ 540321b17ecSEdward Tomasz Napierala valid_digest = icl_mbuf_to_crc32c(request->ip_data_mbuf); 541321b17ecSEdward Tomasz Napierala if (received_digest != valid_digest) { 542321b17ecSEdward Tomasz Napierala ICL_WARN("data digest check failed; got 0x%x, " 543321b17ecSEdward Tomasz Napierala "should be 0x%x", received_digest, valid_digest); 544321b17ecSEdward Tomasz Napierala return (-1); 545321b17ecSEdward Tomasz Napierala } 546321b17ecSEdward Tomasz Napierala 547321b17ecSEdward Tomasz Napierala return (0); 548321b17ecSEdward Tomasz Napierala } 549321b17ecSEdward Tomasz Napierala 550321b17ecSEdward Tomasz Napierala /* 551321b17ecSEdward Tomasz Napierala * Somewhat contrary to the name, this attempts to receive only one 552321b17ecSEdward Tomasz Napierala * "part" of PDU at a time; call it repeatedly until it returns non-NULL. 553321b17ecSEdward Tomasz Napierala */ 554321b17ecSEdward Tomasz Napierala static struct icl_pdu * 55587322a90SJohn Baldwin icl_conn_receive_pdu(struct icl_soft_conn *isc, struct mbuf **r, size_t *rs) 556321b17ecSEdward Tomasz Napierala { 55787322a90SJohn Baldwin struct icl_conn *ic = &isc->ic; 558321b17ecSEdward Tomasz Napierala struct icl_pdu *request; 559321b17ecSEdward Tomasz Napierala size_t len; 5606895f89fSAlexander Motin int error = 0; 561321b17ecSEdward Tomasz Napierala bool more_needed; 562321b17ecSEdward Tomasz Napierala 56387322a90SJohn Baldwin if (isc->receive_state == ICL_CONN_STATE_BHS) { 56487322a90SJohn Baldwin KASSERT(isc->receive_pdu == NULL, 56587322a90SJohn Baldwin ("isc->receive_pdu != NULL")); 566d0d587c7SAlexander Motin request = icl_soft_conn_new_pdu(ic, M_NOWAIT); 567321b17ecSEdward Tomasz Napierala if (request == NULL) { 568321b17ecSEdward Tomasz Napierala ICL_DEBUG("failed to allocate PDU; " 569321b17ecSEdward Tomasz Napierala "dropping connection"); 570321b17ecSEdward Tomasz Napierala icl_conn_fail(ic); 571321b17ecSEdward Tomasz Napierala return (NULL); 572321b17ecSEdward Tomasz Napierala } 57387322a90SJohn Baldwin isc->receive_pdu = request; 574321b17ecSEdward Tomasz Napierala } else { 57587322a90SJohn Baldwin KASSERT(isc->receive_pdu != NULL, 57687322a90SJohn Baldwin ("isc->receive_pdu == NULL")); 57787322a90SJohn Baldwin request = isc->receive_pdu; 578321b17ecSEdward Tomasz Napierala } 579321b17ecSEdward Tomasz Napierala 58087322a90SJohn Baldwin switch (isc->receive_state) { 581321b17ecSEdward Tomasz Napierala case ICL_CONN_STATE_BHS: 582321b17ecSEdward Tomasz Napierala //ICL_DEBUG("receiving BHS"); 5836895f89fSAlexander Motin icl_soft_receive_buf(r, rs, request->ip_bhs, 5846895f89fSAlexander Motin sizeof(struct iscsi_bhs)); 585321b17ecSEdward Tomasz Napierala 586321b17ecSEdward Tomasz Napierala /* 587321b17ecSEdward Tomasz Napierala * We don't enforce any limit for AHS length; 588321b17ecSEdward Tomasz Napierala * its length is stored in 8 bit field. 589321b17ecSEdward Tomasz Napierala */ 590321b17ecSEdward Tomasz Napierala 591321b17ecSEdward Tomasz Napierala len = icl_pdu_data_segment_length(request); 5920cc7d64aSJohn Baldwin if (len > ic->ic_max_recv_data_segment_length) { 593321b17ecSEdward Tomasz Napierala ICL_WARN("received data segment " 594b75168edSAlexander Motin "length %zd is larger than negotiated; " 595b75168edSAlexander Motin "dropping connection", len); 596321b17ecSEdward Tomasz Napierala error = EINVAL; 597321b17ecSEdward Tomasz Napierala break; 598321b17ecSEdward Tomasz Napierala } 599321b17ecSEdward Tomasz Napierala 60087322a90SJohn Baldwin isc->receive_state = ICL_CONN_STATE_AHS; 60187322a90SJohn Baldwin isc->receive_len = icl_pdu_ahs_length(request); 602321b17ecSEdward Tomasz Napierala break; 603321b17ecSEdward Tomasz Napierala 604321b17ecSEdward Tomasz Napierala case ICL_CONN_STATE_AHS: 605321b17ecSEdward Tomasz Napierala //ICL_DEBUG("receiving AHS"); 6066895f89fSAlexander Motin icl_pdu_receive_ahs(request, r, rs); 60787322a90SJohn Baldwin isc->receive_state = ICL_CONN_STATE_HEADER_DIGEST; 608321b17ecSEdward Tomasz Napierala if (ic->ic_header_crc32c == false) 60987322a90SJohn Baldwin isc->receive_len = 0; 610321b17ecSEdward Tomasz Napierala else 61187322a90SJohn Baldwin isc->receive_len = ISCSI_HEADER_DIGEST_SIZE; 612321b17ecSEdward Tomasz Napierala break; 613321b17ecSEdward Tomasz Napierala 614321b17ecSEdward Tomasz Napierala case ICL_CONN_STATE_HEADER_DIGEST: 615321b17ecSEdward Tomasz Napierala //ICL_DEBUG("receiving header digest"); 6166895f89fSAlexander Motin error = icl_pdu_check_header_digest(request, r, rs); 617321b17ecSEdward Tomasz Napierala if (error != 0) { 618321b17ecSEdward Tomasz Napierala ICL_DEBUG("header digest failed; " 619321b17ecSEdward Tomasz Napierala "dropping connection"); 620321b17ecSEdward Tomasz Napierala break; 621321b17ecSEdward Tomasz Napierala } 622321b17ecSEdward Tomasz Napierala 62387322a90SJohn Baldwin isc->receive_state = ICL_CONN_STATE_DATA; 62487322a90SJohn Baldwin isc->receive_len = icl_pdu_data_segment_receive_len(request); 625321b17ecSEdward Tomasz Napierala break; 626321b17ecSEdward Tomasz Napierala 627321b17ecSEdward Tomasz Napierala case ICL_CONN_STATE_DATA: 628321b17ecSEdward Tomasz Napierala //ICL_DEBUG("receiving data segment"); 6296895f89fSAlexander Motin error = icl_pdu_receive_data_segment(request, r, rs, 630321b17ecSEdward Tomasz Napierala &more_needed); 631321b17ecSEdward Tomasz Napierala if (error != 0) { 632321b17ecSEdward Tomasz Napierala ICL_DEBUG("failed to receive data segment;" 633321b17ecSEdward Tomasz Napierala "dropping connection"); 634321b17ecSEdward Tomasz Napierala break; 635321b17ecSEdward Tomasz Napierala } 636321b17ecSEdward Tomasz Napierala 637321b17ecSEdward Tomasz Napierala if (more_needed) 638321b17ecSEdward Tomasz Napierala break; 639321b17ecSEdward Tomasz Napierala 64087322a90SJohn Baldwin isc->receive_state = ICL_CONN_STATE_DATA_DIGEST; 641321b17ecSEdward Tomasz Napierala if (request->ip_data_len == 0 || ic->ic_data_crc32c == false) 64287322a90SJohn Baldwin isc->receive_len = 0; 643321b17ecSEdward Tomasz Napierala else 64487322a90SJohn Baldwin isc->receive_len = ISCSI_DATA_DIGEST_SIZE; 645321b17ecSEdward Tomasz Napierala break; 646321b17ecSEdward Tomasz Napierala 647321b17ecSEdward Tomasz Napierala case ICL_CONN_STATE_DATA_DIGEST: 648321b17ecSEdward Tomasz Napierala //ICL_DEBUG("receiving data digest"); 6496895f89fSAlexander Motin error = icl_pdu_check_data_digest(request, r, rs); 650321b17ecSEdward Tomasz Napierala if (error != 0) { 651321b17ecSEdward Tomasz Napierala ICL_DEBUG("data digest failed; " 652321b17ecSEdward Tomasz Napierala "dropping connection"); 653321b17ecSEdward Tomasz Napierala break; 654321b17ecSEdward Tomasz Napierala } 655321b17ecSEdward Tomasz Napierala 656321b17ecSEdward Tomasz Napierala /* 657321b17ecSEdward Tomasz Napierala * We've received complete PDU; reset the receive state machine 658321b17ecSEdward Tomasz Napierala * and return the PDU. 659321b17ecSEdward Tomasz Napierala */ 66087322a90SJohn Baldwin isc->receive_state = ICL_CONN_STATE_BHS; 66187322a90SJohn Baldwin isc->receive_len = sizeof(struct iscsi_bhs); 66287322a90SJohn Baldwin isc->receive_pdu = NULL; 663321b17ecSEdward Tomasz Napierala return (request); 664321b17ecSEdward Tomasz Napierala 665321b17ecSEdward Tomasz Napierala default: 66687322a90SJohn Baldwin panic("invalid receive_state %d\n", isc->receive_state); 667321b17ecSEdward Tomasz Napierala } 668321b17ecSEdward Tomasz Napierala 669321b17ecSEdward Tomasz Napierala if (error != 0) { 670321b17ecSEdward Tomasz Napierala /* 67187322a90SJohn Baldwin * Don't free the PDU; it's pointed to by isc->receive_pdu 6725aabcd7cSEdward Tomasz Napierala * and will get freed in icl_soft_conn_close(). 673321b17ecSEdward Tomasz Napierala */ 674321b17ecSEdward Tomasz Napierala icl_conn_fail(ic); 675321b17ecSEdward Tomasz Napierala } 676321b17ecSEdward Tomasz Napierala 677321b17ecSEdward Tomasz Napierala return (NULL); 678321b17ecSEdward Tomasz Napierala } 679321b17ecSEdward Tomasz Napierala 680321b17ecSEdward Tomasz Napierala static void 68187322a90SJohn Baldwin icl_conn_receive_pdus(struct icl_soft_conn *isc, struct mbuf **r, size_t *rs) 682321b17ecSEdward Tomasz Napierala { 68387322a90SJohn Baldwin struct icl_conn *ic = &isc->ic; 684321b17ecSEdward Tomasz Napierala struct icl_pdu *response; 685321b17ecSEdward Tomasz Napierala 686321b17ecSEdward Tomasz Napierala for (;;) { 687321b17ecSEdward Tomasz Napierala if (ic->ic_disconnecting) 688321b17ecSEdward Tomasz Napierala return; 689321b17ecSEdward Tomasz Napierala 690321b17ecSEdward Tomasz Napierala /* 691321b17ecSEdward Tomasz Napierala * Loop until we have a complete PDU or there is not enough 692321b17ecSEdward Tomasz Napierala * data in the socket buffer. 693321b17ecSEdward Tomasz Napierala */ 69487322a90SJohn Baldwin if (*rs < isc->receive_len) { 695321b17ecSEdward Tomasz Napierala #if 0 6966895f89fSAlexander Motin ICL_DEBUG("not enough data; have %zd, need %zd", 69787322a90SJohn Baldwin *rs, isc->receive_len); 698321b17ecSEdward Tomasz Napierala #endif 699321b17ecSEdward Tomasz Napierala return; 700321b17ecSEdward Tomasz Napierala } 701321b17ecSEdward Tomasz Napierala 70287322a90SJohn Baldwin response = icl_conn_receive_pdu(isc, r, rs); 703321b17ecSEdward Tomasz Napierala if (response == NULL) 704321b17ecSEdward Tomasz Napierala continue; 705321b17ecSEdward Tomasz Napierala 706321b17ecSEdward Tomasz Napierala if (response->ip_ahs_len > 0) { 707321b17ecSEdward Tomasz Napierala ICL_WARN("received PDU with unsupported " 708321b17ecSEdward Tomasz Napierala "AHS; opcode 0x%x; dropping connection", 709321b17ecSEdward Tomasz Napierala response->ip_bhs->bhs_opcode); 71082f7fa7aSAlexander Motin icl_soft_conn_pdu_free(ic, response); 711321b17ecSEdward Tomasz Napierala icl_conn_fail(ic); 712321b17ecSEdward Tomasz Napierala return; 713321b17ecSEdward Tomasz Napierala } 714321b17ecSEdward Tomasz Napierala 715321b17ecSEdward Tomasz Napierala (ic->ic_receive)(response); 716321b17ecSEdward Tomasz Napierala } 717321b17ecSEdward Tomasz Napierala } 718321b17ecSEdward Tomasz Napierala 719321b17ecSEdward Tomasz Napierala static void 720321b17ecSEdward Tomasz Napierala icl_receive_thread(void *arg) 721321b17ecSEdward Tomasz Napierala { 72287322a90SJohn Baldwin struct icl_soft_conn *isc = arg; 72387322a90SJohn Baldwin struct icl_conn *ic = &isc->ic; 7246895f89fSAlexander Motin size_t available, read = 0; 725321b17ecSEdward Tomasz Napierala struct socket *so; 7266895f89fSAlexander Motin struct mbuf *m, *r = NULL; 7276895f89fSAlexander Motin struct uio uio; 7286895f89fSAlexander Motin int error, flags; 729321b17ecSEdward Tomasz Napierala 730321b17ecSEdward Tomasz Napierala so = ic->ic_socket; 731321b17ecSEdward Tomasz Napierala 732321b17ecSEdward Tomasz Napierala for (;;) { 7336895f89fSAlexander Motin SOCKBUF_LOCK(&so->so_rcv); 734321b17ecSEdward Tomasz Napierala if (ic->ic_disconnecting) { 7356895f89fSAlexander Motin SOCKBUF_UNLOCK(&so->so_rcv); 736321b17ecSEdward Tomasz Napierala break; 737321b17ecSEdward Tomasz Napierala } 738321b17ecSEdward Tomasz Napierala 739321b17ecSEdward Tomasz Napierala /* 740321b17ecSEdward Tomasz Napierala * Set the low watermark, to be checked by 741321b17ecSEdward Tomasz Napierala * soreadable() in icl_soupcall_receive() 742266078c6SPedro F. Giffuni * to avoid unnecessary wakeups until there 743321b17ecSEdward Tomasz Napierala * is enough data received to read the PDU. 744321b17ecSEdward Tomasz Napierala */ 745321b17ecSEdward Tomasz Napierala available = sbavail(&so->so_rcv); 74687322a90SJohn Baldwin if (read + available < isc->receive_len) { 74787322a90SJohn Baldwin so->so_rcv.sb_lowat = isc->receive_len - read; 74887322a90SJohn Baldwin cv_wait(&isc->receive_cv, SOCKBUF_MTX(&so->so_rcv)); 749321b17ecSEdward Tomasz Napierala so->so_rcv.sb_lowat = so->so_rcv.sb_hiwat + 1; 7506895f89fSAlexander Motin available = sbavail(&so->so_rcv); 7516895f89fSAlexander Motin } 752321b17ecSEdward Tomasz Napierala SOCKBUF_UNLOCK(&so->so_rcv); 753321b17ecSEdward Tomasz Napierala 7546895f89fSAlexander Motin if (available == 0) { 7556895f89fSAlexander Motin if (so->so_error != 0) { 7566895f89fSAlexander Motin ICL_DEBUG("connection error %d; " 7576895f89fSAlexander Motin "dropping connection", so->so_error); 7586895f89fSAlexander Motin icl_conn_fail(ic); 7596895f89fSAlexander Motin break; 760321b17ecSEdward Tomasz Napierala } 7616895f89fSAlexander Motin continue; 7626895f89fSAlexander Motin } 7636895f89fSAlexander Motin 7646895f89fSAlexander Motin memset(&uio, 0, sizeof(uio)); 7656895f89fSAlexander Motin uio.uio_resid = available; 7666895f89fSAlexander Motin flags = MSG_DONTWAIT; 7676895f89fSAlexander Motin error = soreceive(so, NULL, &uio, &m, NULL, &flags); 7686895f89fSAlexander Motin if (error != 0) { 7696895f89fSAlexander Motin ICL_DEBUG("soreceive error %d", error); 7706895f89fSAlexander Motin break; 7716895f89fSAlexander Motin } 7726895f89fSAlexander Motin if (uio.uio_resid != 0) { 7736895f89fSAlexander Motin m_freem(m); 7746895f89fSAlexander Motin ICL_DEBUG("short read"); 7756895f89fSAlexander Motin break; 7766895f89fSAlexander Motin } 7776895f89fSAlexander Motin if (r) 7786895f89fSAlexander Motin m_cat(r, m); 7796895f89fSAlexander Motin else 7806895f89fSAlexander Motin r = m; 7816895f89fSAlexander Motin read += available; 7826895f89fSAlexander Motin 78387322a90SJohn Baldwin icl_conn_receive_pdus(isc, &r, &read); 7846895f89fSAlexander Motin } 7856895f89fSAlexander Motin 7866895f89fSAlexander Motin if (r) 7876895f89fSAlexander Motin m_freem(r); 788321b17ecSEdward Tomasz Napierala 789321b17ecSEdward Tomasz Napierala ICL_CONN_LOCK(ic); 79087322a90SJohn Baldwin isc->receive_running = false; 79187322a90SJohn Baldwin cv_signal(&isc->send_cv); 792321b17ecSEdward Tomasz Napierala ICL_CONN_UNLOCK(ic); 793321b17ecSEdward Tomasz Napierala kthread_exit(); 794321b17ecSEdward Tomasz Napierala } 795321b17ecSEdward Tomasz Napierala 796321b17ecSEdward Tomasz Napierala static int 797321b17ecSEdward Tomasz Napierala icl_soupcall_receive(struct socket *so, void *arg, int waitflag) 798321b17ecSEdward Tomasz Napierala { 79987322a90SJohn Baldwin struct icl_soft_conn *isc; 800321b17ecSEdward Tomasz Napierala 801321b17ecSEdward Tomasz Napierala if (!soreadable(so)) 802321b17ecSEdward Tomasz Napierala return (SU_OK); 803321b17ecSEdward Tomasz Napierala 80487322a90SJohn Baldwin isc = arg; 80587322a90SJohn Baldwin cv_signal(&isc->receive_cv); 806321b17ecSEdward Tomasz Napierala return (SU_OK); 807321b17ecSEdward Tomasz Napierala } 808321b17ecSEdward Tomasz Napierala 809321b17ecSEdward Tomasz Napierala static int 810321b17ecSEdward Tomasz Napierala icl_pdu_finalize(struct icl_pdu *request) 811321b17ecSEdward Tomasz Napierala { 812321b17ecSEdward Tomasz Napierala size_t padding, pdu_len; 813321b17ecSEdward Tomasz Napierala uint32_t digest, zero = 0; 814321b17ecSEdward Tomasz Napierala int ok; 815321b17ecSEdward Tomasz Napierala struct icl_conn *ic; 816321b17ecSEdward Tomasz Napierala 817321b17ecSEdward Tomasz Napierala ic = request->ip_conn; 818321b17ecSEdward Tomasz Napierala 819321b17ecSEdward Tomasz Napierala icl_pdu_set_data_segment_length(request, request->ip_data_len); 820321b17ecSEdward Tomasz Napierala 821321b17ecSEdward Tomasz Napierala pdu_len = icl_pdu_size(request); 822321b17ecSEdward Tomasz Napierala 823321b17ecSEdward Tomasz Napierala if (ic->ic_header_crc32c) { 824321b17ecSEdward Tomasz Napierala digest = icl_mbuf_to_crc32c(request->ip_bhs_mbuf); 825321b17ecSEdward Tomasz Napierala ok = m_append(request->ip_bhs_mbuf, sizeof(digest), 826321b17ecSEdward Tomasz Napierala (void *)&digest); 827321b17ecSEdward Tomasz Napierala if (ok != 1) { 828321b17ecSEdward Tomasz Napierala ICL_WARN("failed to append header digest"); 829321b17ecSEdward Tomasz Napierala return (1); 830321b17ecSEdward Tomasz Napierala } 831321b17ecSEdward Tomasz Napierala } 832321b17ecSEdward Tomasz Napierala 833321b17ecSEdward Tomasz Napierala if (request->ip_data_len != 0) { 834321b17ecSEdward Tomasz Napierala padding = icl_pdu_padding(request); 835321b17ecSEdward Tomasz Napierala if (padding > 0) { 836321b17ecSEdward Tomasz Napierala ok = m_append(request->ip_data_mbuf, padding, 837321b17ecSEdward Tomasz Napierala (void *)&zero); 838321b17ecSEdward Tomasz Napierala if (ok != 1) { 839321b17ecSEdward Tomasz Napierala ICL_WARN("failed to append padding"); 840321b17ecSEdward Tomasz Napierala return (1); 841321b17ecSEdward Tomasz Napierala } 842321b17ecSEdward Tomasz Napierala } 843321b17ecSEdward Tomasz Napierala 844321b17ecSEdward Tomasz Napierala if (ic->ic_data_crc32c) { 845321b17ecSEdward Tomasz Napierala digest = icl_mbuf_to_crc32c(request->ip_data_mbuf); 846321b17ecSEdward Tomasz Napierala 847321b17ecSEdward Tomasz Napierala ok = m_append(request->ip_data_mbuf, sizeof(digest), 848321b17ecSEdward Tomasz Napierala (void *)&digest); 849321b17ecSEdward Tomasz Napierala if (ok != 1) { 850321b17ecSEdward Tomasz Napierala ICL_WARN("failed to append data digest"); 851321b17ecSEdward Tomasz Napierala return (1); 852321b17ecSEdward Tomasz Napierala } 853321b17ecSEdward Tomasz Napierala } 854321b17ecSEdward Tomasz Napierala 855321b17ecSEdward Tomasz Napierala m_cat(request->ip_bhs_mbuf, request->ip_data_mbuf); 856321b17ecSEdward Tomasz Napierala request->ip_data_mbuf = NULL; 857321b17ecSEdward Tomasz Napierala } 858321b17ecSEdward Tomasz Napierala 859321b17ecSEdward Tomasz Napierala request->ip_bhs_mbuf->m_pkthdr.len = pdu_len; 860321b17ecSEdward Tomasz Napierala 861321b17ecSEdward Tomasz Napierala return (0); 862321b17ecSEdward Tomasz Napierala } 863321b17ecSEdward Tomasz Napierala 864321b17ecSEdward Tomasz Napierala static void 86587322a90SJohn Baldwin icl_conn_send_pdus(struct icl_soft_conn *isc, struct icl_pdu_stailq *queue) 866321b17ecSEdward Tomasz Napierala { 86787322a90SJohn Baldwin struct icl_conn *ic = &isc->ic; 868321b17ecSEdward Tomasz Napierala struct icl_pdu *request, *request2; 869b85a67f5SAlexander Motin struct mbuf *m; 870321b17ecSEdward Tomasz Napierala struct socket *so; 871605703b5SAlexander Motin long available, size, size2; 872321b17ecSEdward Tomasz Napierala int coalesced, error; 873321b17ecSEdward Tomasz Napierala 874321b17ecSEdward Tomasz Napierala ICL_CONN_LOCK_ASSERT_NOT(ic); 875321b17ecSEdward Tomasz Napierala 876321b17ecSEdward Tomasz Napierala so = ic->ic_socket; 877321b17ecSEdward Tomasz Napierala 878321b17ecSEdward Tomasz Napierala SOCKBUF_LOCK(&so->so_snd); 879321b17ecSEdward Tomasz Napierala /* 880321b17ecSEdward Tomasz Napierala * Check how much space do we have for transmit. We can't just 881321b17ecSEdward Tomasz Napierala * call sosend() and retry when we get EWOULDBLOCK or EMSGSIZE, 882321b17ecSEdward Tomasz Napierala * as it always frees the mbuf chain passed to it, even in case 883321b17ecSEdward Tomasz Napierala * of error. 884321b17ecSEdward Tomasz Napierala */ 885321b17ecSEdward Tomasz Napierala available = sbspace(&so->so_snd); 88687322a90SJohn Baldwin isc->check_send_space = false; 887321b17ecSEdward Tomasz Napierala 888321b17ecSEdward Tomasz Napierala /* 889321b17ecSEdward Tomasz Napierala * Notify the socket upcall that we don't need wakeups 890321b17ecSEdward Tomasz Napierala * for the time being. 891321b17ecSEdward Tomasz Napierala */ 892321b17ecSEdward Tomasz Napierala so->so_snd.sb_lowat = so->so_snd.sb_hiwat + 1; 893321b17ecSEdward Tomasz Napierala SOCKBUF_UNLOCK(&so->so_snd); 894321b17ecSEdward Tomasz Napierala 895321b17ecSEdward Tomasz Napierala while (!STAILQ_EMPTY(queue)) { 896321b17ecSEdward Tomasz Napierala request = STAILQ_FIRST(queue); 897321b17ecSEdward Tomasz Napierala size = icl_pdu_size(request); 898321b17ecSEdward Tomasz Napierala if (available < size) { 899321b17ecSEdward Tomasz Napierala /* 900321b17ecSEdward Tomasz Napierala * Set the low watermark, to be checked by 901321b17ecSEdward Tomasz Napierala * sowriteable() in icl_soupcall_send() 902266078c6SPedro F. Giffuni * to avoid unnecessary wakeups until there 903321b17ecSEdward Tomasz Napierala * is enough space for the PDU to fit. 904321b17ecSEdward Tomasz Napierala */ 905321b17ecSEdward Tomasz Napierala SOCKBUF_LOCK(&so->so_snd); 906321b17ecSEdward Tomasz Napierala available = sbspace(&so->so_snd); 907321b17ecSEdward Tomasz Napierala if (available < size) { 908321b17ecSEdward Tomasz Napierala #if 1 909321b17ecSEdward Tomasz Napierala ICL_DEBUG("no space to send; " 910605703b5SAlexander Motin "have %ld, need %ld", 911321b17ecSEdward Tomasz Napierala available, size); 912321b17ecSEdward Tomasz Napierala #endif 9131f29b46cSAlexander Motin so->so_snd.sb_lowat = max(size, 9141f29b46cSAlexander Motin so->so_snd.sb_hiwat / 8); 915321b17ecSEdward Tomasz Napierala SOCKBUF_UNLOCK(&so->so_snd); 916321b17ecSEdward Tomasz Napierala return; 917321b17ecSEdward Tomasz Napierala } 918321b17ecSEdward Tomasz Napierala SOCKBUF_UNLOCK(&so->so_snd); 919321b17ecSEdward Tomasz Napierala } 920321b17ecSEdward Tomasz Napierala STAILQ_REMOVE_HEAD(queue, ip_next); 921321b17ecSEdward Tomasz Napierala error = icl_pdu_finalize(request); 922321b17ecSEdward Tomasz Napierala if (error != 0) { 923321b17ecSEdward Tomasz Napierala ICL_DEBUG("failed to finalize PDU; " 924321b17ecSEdward Tomasz Napierala "dropping connection"); 9259a4510acSAlexander Motin icl_soft_pdu_done(request, EIO); 926321b17ecSEdward Tomasz Napierala icl_conn_fail(ic); 927321b17ecSEdward Tomasz Napierala return; 928321b17ecSEdward Tomasz Napierala } 929321b17ecSEdward Tomasz Napierala if (coalesce) { 930b85a67f5SAlexander Motin m = request->ip_bhs_mbuf; 931b85a67f5SAlexander Motin for (coalesced = 1; ; coalesced++) { 932321b17ecSEdward Tomasz Napierala request2 = STAILQ_FIRST(queue); 933321b17ecSEdward Tomasz Napierala if (request2 == NULL) 934321b17ecSEdward Tomasz Napierala break; 935321b17ecSEdward Tomasz Napierala size2 = icl_pdu_size(request2); 936321b17ecSEdward Tomasz Napierala if (available < size + size2) 937321b17ecSEdward Tomasz Napierala break; 938321b17ecSEdward Tomasz Napierala STAILQ_REMOVE_HEAD(queue, ip_next); 939321b17ecSEdward Tomasz Napierala error = icl_pdu_finalize(request2); 940321b17ecSEdward Tomasz Napierala if (error != 0) { 941321b17ecSEdward Tomasz Napierala ICL_DEBUG("failed to finalize PDU; " 942321b17ecSEdward Tomasz Napierala "dropping connection"); 9439a4510acSAlexander Motin icl_soft_pdu_done(request, EIO); 9449a4510acSAlexander Motin icl_soft_pdu_done(request2, EIO); 945321b17ecSEdward Tomasz Napierala icl_conn_fail(ic); 946321b17ecSEdward Tomasz Napierala return; 947321b17ecSEdward Tomasz Napierala } 948b85a67f5SAlexander Motin while (m->m_next) 949b85a67f5SAlexander Motin m = m->m_next; 950b85a67f5SAlexander Motin m_cat(m, request2->ip_bhs_mbuf); 951321b17ecSEdward Tomasz Napierala request2->ip_bhs_mbuf = NULL; 952321b17ecSEdward Tomasz Napierala request->ip_bhs_mbuf->m_pkthdr.len += size2; 953321b17ecSEdward Tomasz Napierala size += size2; 9549a4510acSAlexander Motin icl_soft_pdu_done(request2, 0); 955321b17ecSEdward Tomasz Napierala } 956321b17ecSEdward Tomasz Napierala #if 0 957321b17ecSEdward Tomasz Napierala if (coalesced > 1) { 958605703b5SAlexander Motin ICL_DEBUG("coalesced %d PDUs into %ld bytes", 959321b17ecSEdward Tomasz Napierala coalesced, size); 960321b17ecSEdward Tomasz Napierala } 961321b17ecSEdward Tomasz Napierala #endif 962321b17ecSEdward Tomasz Napierala } 963321b17ecSEdward Tomasz Napierala available -= size; 964321b17ecSEdward Tomasz Napierala error = sosend(so, NULL, NULL, request->ip_bhs_mbuf, 965321b17ecSEdward Tomasz Napierala NULL, MSG_DONTWAIT, curthread); 966321b17ecSEdward Tomasz Napierala request->ip_bhs_mbuf = NULL; /* Sosend consumes the mbuf. */ 967321b17ecSEdward Tomasz Napierala if (error != 0) { 968321b17ecSEdward Tomasz Napierala ICL_DEBUG("failed to send PDU, error %d; " 969321b17ecSEdward Tomasz Napierala "dropping connection", error); 9709a4510acSAlexander Motin icl_soft_pdu_done(request, error); 971321b17ecSEdward Tomasz Napierala icl_conn_fail(ic); 972321b17ecSEdward Tomasz Napierala return; 973321b17ecSEdward Tomasz Napierala } 9749a4510acSAlexander Motin icl_soft_pdu_done(request, 0); 975321b17ecSEdward Tomasz Napierala } 976321b17ecSEdward Tomasz Napierala } 977321b17ecSEdward Tomasz Napierala 978321b17ecSEdward Tomasz Napierala static void 979321b17ecSEdward Tomasz Napierala icl_send_thread(void *arg) 980321b17ecSEdward Tomasz Napierala { 98187322a90SJohn Baldwin struct icl_soft_conn *isc; 982321b17ecSEdward Tomasz Napierala struct icl_conn *ic; 983321b17ecSEdward Tomasz Napierala struct icl_pdu_stailq queue; 984321b17ecSEdward Tomasz Napierala 98587322a90SJohn Baldwin isc = arg; 98687322a90SJohn Baldwin ic = &isc->ic; 987321b17ecSEdward Tomasz Napierala 988321b17ecSEdward Tomasz Napierala STAILQ_INIT(&queue); 989321b17ecSEdward Tomasz Napierala 990321b17ecSEdward Tomasz Napierala ICL_CONN_LOCK(ic); 991321b17ecSEdward Tomasz Napierala for (;;) { 992321b17ecSEdward Tomasz Napierala for (;;) { 993321b17ecSEdward Tomasz Napierala /* 994df3747c6SAlexander Motin * Populate the local queue from the main one. 995df3747c6SAlexander Motin * This way the icl_conn_send_pdus() can go through 996df3747c6SAlexander Motin * all the queued PDUs without holding any locks. 997321b17ecSEdward Tomasz Napierala */ 99887322a90SJohn Baldwin if (STAILQ_EMPTY(&queue) || isc->check_send_space) 99987322a90SJohn Baldwin STAILQ_CONCAT(&queue, &isc->to_send); 1000321b17ecSEdward Tomasz Napierala 1001321b17ecSEdward Tomasz Napierala ICL_CONN_UNLOCK(ic); 100287322a90SJohn Baldwin icl_conn_send_pdus(isc, &queue); 1003321b17ecSEdward Tomasz Napierala ICL_CONN_LOCK(ic); 1004321b17ecSEdward Tomasz Napierala 1005321b17ecSEdward Tomasz Napierala /* 1006321b17ecSEdward Tomasz Napierala * The icl_soupcall_send() was called since the last 1007321b17ecSEdward Tomasz Napierala * call to sbspace(); go around; 1008321b17ecSEdward Tomasz Napierala */ 100987322a90SJohn Baldwin if (isc->check_send_space) 1010321b17ecSEdward Tomasz Napierala continue; 1011321b17ecSEdward Tomasz Napierala 1012321b17ecSEdward Tomasz Napierala /* 1013321b17ecSEdward Tomasz Napierala * Local queue is empty, but we still have PDUs 1014321b17ecSEdward Tomasz Napierala * in the main one; go around. 1015321b17ecSEdward Tomasz Napierala */ 1016321b17ecSEdward Tomasz Napierala if (STAILQ_EMPTY(&queue) && 101787322a90SJohn Baldwin !STAILQ_EMPTY(&isc->to_send)) 1018321b17ecSEdward Tomasz Napierala continue; 1019321b17ecSEdward Tomasz Napierala 1020321b17ecSEdward Tomasz Napierala /* 1021321b17ecSEdward Tomasz Napierala * There might be some stuff in the local queue, 1022321b17ecSEdward Tomasz Napierala * which didn't get sent due to not having enough send 1023321b17ecSEdward Tomasz Napierala * space. Wait for socket upcall. 1024321b17ecSEdward Tomasz Napierala */ 1025321b17ecSEdward Tomasz Napierala break; 1026321b17ecSEdward Tomasz Napierala } 1027321b17ecSEdward Tomasz Napierala 1028321b17ecSEdward Tomasz Napierala if (ic->ic_disconnecting) { 1029321b17ecSEdward Tomasz Napierala //ICL_DEBUG("terminating"); 1030321b17ecSEdward Tomasz Napierala break; 1031321b17ecSEdward Tomasz Napierala } 1032321b17ecSEdward Tomasz Napierala 103387322a90SJohn Baldwin cv_wait(&isc->send_cv, ic->ic_lock); 1034321b17ecSEdward Tomasz Napierala } 1035321b17ecSEdward Tomasz Napierala 1036321b17ecSEdward Tomasz Napierala /* 1037321b17ecSEdward Tomasz Napierala * We're exiting; move PDUs back to the main queue, so they can 1038321b17ecSEdward Tomasz Napierala * get freed properly. At this point ordering doesn't matter. 1039321b17ecSEdward Tomasz Napierala */ 104087322a90SJohn Baldwin STAILQ_CONCAT(&isc->to_send, &queue); 1041321b17ecSEdward Tomasz Napierala 104287322a90SJohn Baldwin isc->send_running = false; 104387322a90SJohn Baldwin cv_signal(&isc->send_cv); 1044321b17ecSEdward Tomasz Napierala ICL_CONN_UNLOCK(ic); 1045321b17ecSEdward Tomasz Napierala kthread_exit(); 1046321b17ecSEdward Tomasz Napierala } 1047321b17ecSEdward Tomasz Napierala 1048321b17ecSEdward Tomasz Napierala static int 1049321b17ecSEdward Tomasz Napierala icl_soupcall_send(struct socket *so, void *arg, int waitflag) 1050321b17ecSEdward Tomasz Napierala { 105187322a90SJohn Baldwin struct icl_soft_conn *isc; 1052321b17ecSEdward Tomasz Napierala struct icl_conn *ic; 1053321b17ecSEdward Tomasz Napierala 1054321b17ecSEdward Tomasz Napierala if (!sowriteable(so)) 1055321b17ecSEdward Tomasz Napierala return (SU_OK); 1056321b17ecSEdward Tomasz Napierala 105787322a90SJohn Baldwin isc = arg; 105887322a90SJohn Baldwin ic = &isc->ic; 1059321b17ecSEdward Tomasz Napierala 1060321b17ecSEdward Tomasz Napierala ICL_CONN_LOCK(ic); 106187322a90SJohn Baldwin isc->check_send_space = true; 1062321b17ecSEdward Tomasz Napierala ICL_CONN_UNLOCK(ic); 1063321b17ecSEdward Tomasz Napierala 106487322a90SJohn Baldwin cv_signal(&isc->send_cv); 1065321b17ecSEdward Tomasz Napierala 1066321b17ecSEdward Tomasz Napierala return (SU_OK); 1067321b17ecSEdward Tomasz Napierala } 1068321b17ecSEdward Tomasz Napierala 1069321b17ecSEdward Tomasz Napierala static int 107082f7fa7aSAlexander Motin icl_soft_conn_pdu_append_data(struct icl_conn *ic, struct icl_pdu *request, 107182f7fa7aSAlexander Motin const void *addr, size_t len, int flags) 1072321b17ecSEdward Tomasz Napierala { 10739a4510acSAlexander Motin struct icl_soft_pdu *isp = (struct icl_soft_pdu *)request; 1074321b17ecSEdward Tomasz Napierala struct mbuf *mb, *newmb; 1075321b17ecSEdward Tomasz Napierala size_t copylen, off = 0; 1076321b17ecSEdward Tomasz Napierala 1077321b17ecSEdward Tomasz Napierala KASSERT(len > 0, ("len == 0")); 1078321b17ecSEdward Tomasz Napierala 10799a4510acSAlexander Motin if (flags & ICL_NOCOPY) { 10809a4510acSAlexander Motin newmb = m_get(flags & ~ICL_NOCOPY, MT_DATA); 10819a4510acSAlexander Motin if (newmb == NULL) { 10829a4510acSAlexander Motin ICL_WARN("failed to allocate mbuf"); 10839a4510acSAlexander Motin return (ENOMEM); 10849a4510acSAlexander Motin } 10859a4510acSAlexander Motin 10869a4510acSAlexander Motin newmb->m_flags |= M_RDONLY; 10879a4510acSAlexander Motin m_extaddref(newmb, __DECONST(char *, addr), len, &isp->ref_cnt, 10889a4510acSAlexander Motin icl_soft_mbuf_done, isp, NULL); 10899a4510acSAlexander Motin newmb->m_len = len; 10909a4510acSAlexander Motin } else { 1091898fd11fSAlexander Motin newmb = m_getm2(NULL, len, flags, MT_DATA, 0); 1092321b17ecSEdward Tomasz Napierala if (newmb == NULL) { 1093321b17ecSEdward Tomasz Napierala ICL_WARN("failed to allocate mbuf for %zd bytes", len); 1094321b17ecSEdward Tomasz Napierala return (ENOMEM); 1095321b17ecSEdward Tomasz Napierala } 1096321b17ecSEdward Tomasz Napierala 1097321b17ecSEdward Tomasz Napierala for (mb = newmb; mb != NULL; mb = mb->m_next) { 1098321b17ecSEdward Tomasz Napierala copylen = min(M_TRAILINGSPACE(mb), len - off); 1099321b17ecSEdward Tomasz Napierala memcpy(mtod(mb, char *), (const char *)addr + off, copylen); 1100321b17ecSEdward Tomasz Napierala mb->m_len = copylen; 1101321b17ecSEdward Tomasz Napierala off += copylen; 1102321b17ecSEdward Tomasz Napierala } 1103321b17ecSEdward Tomasz Napierala KASSERT(off == len, ("%s: off != len", __func__)); 11049a4510acSAlexander Motin } 1105321b17ecSEdward Tomasz Napierala 1106321b17ecSEdward Tomasz Napierala if (request->ip_data_mbuf == NULL) { 1107321b17ecSEdward Tomasz Napierala request->ip_data_mbuf = newmb; 1108321b17ecSEdward Tomasz Napierala request->ip_data_len = len; 1109321b17ecSEdward Tomasz Napierala } else { 1110321b17ecSEdward Tomasz Napierala m_cat(request->ip_data_mbuf, newmb); 1111321b17ecSEdward Tomasz Napierala request->ip_data_len += len; 1112321b17ecSEdward Tomasz Napierala } 1113321b17ecSEdward Tomasz Napierala 1114321b17ecSEdward Tomasz Napierala return (0); 1115321b17ecSEdward Tomasz Napierala } 1116321b17ecSEdward Tomasz Napierala 1117321b17ecSEdward Tomasz Napierala void 1118321b17ecSEdward Tomasz Napierala icl_soft_conn_pdu_get_data(struct icl_conn *ic, struct icl_pdu *ip, 1119321b17ecSEdward Tomasz Napierala size_t off, void *addr, size_t len) 1120321b17ecSEdward Tomasz Napierala { 1121321b17ecSEdward Tomasz Napierala 112282f7fa7aSAlexander Motin m_copydata(ip->ip_data_mbuf, off, len, addr); 1123321b17ecSEdward Tomasz Napierala } 1124321b17ecSEdward Tomasz Napierala 1125321b17ecSEdward Tomasz Napierala static void 11269a4510acSAlexander Motin icl_soft_conn_pdu_queue(struct icl_conn *ic, struct icl_pdu *ip) 1127321b17ecSEdward Tomasz Napierala { 1128321b17ecSEdward Tomasz Napierala 11299a4510acSAlexander Motin icl_soft_conn_pdu_queue_cb(ic, ip, NULL); 11309a4510acSAlexander Motin } 11319a4510acSAlexander Motin 11329a4510acSAlexander Motin static void 11339a4510acSAlexander Motin icl_soft_conn_pdu_queue_cb(struct icl_conn *ic, struct icl_pdu *ip, 11349a4510acSAlexander Motin icl_pdu_cb cb) 11359a4510acSAlexander Motin { 113687322a90SJohn Baldwin struct icl_soft_conn *isc = (struct icl_soft_conn *)ic; 11379a4510acSAlexander Motin struct icl_soft_pdu *isp = (struct icl_soft_pdu *)ip; 1138321b17ecSEdward Tomasz Napierala 1139321b17ecSEdward Tomasz Napierala ICL_CONN_LOCK_ASSERT(ic); 11409a4510acSAlexander Motin isp->ref_cnt++; 11419a4510acSAlexander Motin isp->cb = cb; 1142321b17ecSEdward Tomasz Napierala 1143321b17ecSEdward Tomasz Napierala if (ic->ic_disconnecting || ic->ic_socket == NULL) { 1144321b17ecSEdward Tomasz Napierala ICL_DEBUG("icl_pdu_queue on closed connection"); 11459a4510acSAlexander Motin icl_soft_pdu_done(ip, ENOTCONN); 1146321b17ecSEdward Tomasz Napierala return; 1147321b17ecSEdward Tomasz Napierala } 1148321b17ecSEdward Tomasz Napierala 114987322a90SJohn Baldwin if (!STAILQ_EMPTY(&isc->to_send)) { 115087322a90SJohn Baldwin STAILQ_INSERT_TAIL(&isc->to_send, ip, ip_next); 1151321b17ecSEdward Tomasz Napierala /* 1152321b17ecSEdward Tomasz Napierala * If the queue is not empty, someone else had already 1153321b17ecSEdward Tomasz Napierala * signaled the send thread; no need to do that again, 1154321b17ecSEdward Tomasz Napierala * just return. 1155321b17ecSEdward Tomasz Napierala */ 1156321b17ecSEdward Tomasz Napierala return; 1157321b17ecSEdward Tomasz Napierala } 1158321b17ecSEdward Tomasz Napierala 115987322a90SJohn Baldwin STAILQ_INSERT_TAIL(&isc->to_send, ip, ip_next); 116087322a90SJohn Baldwin cv_signal(&isc->send_cv); 1161321b17ecSEdward Tomasz Napierala } 1162321b17ecSEdward Tomasz Napierala 1163321b17ecSEdward Tomasz Napierala static struct icl_conn * 1164321b17ecSEdward Tomasz Napierala icl_soft_new_conn(const char *name, struct mtx *lock) 1165321b17ecSEdward Tomasz Napierala { 116687322a90SJohn Baldwin struct icl_soft_conn *isc; 1167321b17ecSEdward Tomasz Napierala struct icl_conn *ic; 1168321b17ecSEdward Tomasz Napierala 1169321b17ecSEdward Tomasz Napierala refcount_acquire(&icl_ncons); 1170321b17ecSEdward Tomasz Napierala 117187322a90SJohn Baldwin isc = (struct icl_soft_conn *)kobj_create(&icl_soft_class, M_ICL_SOFT, 117287322a90SJohn Baldwin M_WAITOK | M_ZERO); 1173321b17ecSEdward Tomasz Napierala 117487322a90SJohn Baldwin STAILQ_INIT(&isc->to_send); 117587322a90SJohn Baldwin cv_init(&isc->send_cv, "icl_tx"); 117687322a90SJohn Baldwin cv_init(&isc->receive_cv, "icl_rx"); 117787322a90SJohn Baldwin 117887322a90SJohn Baldwin ic = &isc->ic; 1179321b17ecSEdward Tomasz Napierala ic->ic_lock = lock; 1180321b17ecSEdward Tomasz Napierala #ifdef DIAGNOSTIC 1181321b17ecSEdward Tomasz Napierala refcount_init(&ic->ic_outstanding_pdus, 0); 1182321b17ecSEdward Tomasz Napierala #endif 1183321b17ecSEdward Tomasz Napierala ic->ic_name = name; 1184d4b195d3SEdward Tomasz Napierala ic->ic_offload = "None"; 11857deb68abSEdward Tomasz Napierala ic->ic_unmapped = false; 1186321b17ecSEdward Tomasz Napierala 1187321b17ecSEdward Tomasz Napierala return (ic); 1188321b17ecSEdward Tomasz Napierala } 1189321b17ecSEdward Tomasz Napierala 1190321b17ecSEdward Tomasz Napierala void 1191321b17ecSEdward Tomasz Napierala icl_soft_conn_free(struct icl_conn *ic) 1192321b17ecSEdward Tomasz Napierala { 119387322a90SJohn Baldwin struct icl_soft_conn *isc = (struct icl_soft_conn *)ic; 1194321b17ecSEdward Tomasz Napierala 119522d3bb26SEdward Tomasz Napierala #ifdef DIAGNOSTIC 119622d3bb26SEdward Tomasz Napierala KASSERT(ic->ic_outstanding_pdus == 0, 119722d3bb26SEdward Tomasz Napierala ("destroying session with %d outstanding PDUs", 119822d3bb26SEdward Tomasz Napierala ic->ic_outstanding_pdus)); 119922d3bb26SEdward Tomasz Napierala #endif 120087322a90SJohn Baldwin cv_destroy(&isc->send_cv); 120187322a90SJohn Baldwin cv_destroy(&isc->receive_cv); 120287322a90SJohn Baldwin kobj_delete((struct kobj *)isc, M_ICL_SOFT); 1203321b17ecSEdward Tomasz Napierala refcount_release(&icl_ncons); 1204321b17ecSEdward Tomasz Napierala } 1205321b17ecSEdward Tomasz Napierala 1206321b17ecSEdward Tomasz Napierala static int 1207321b17ecSEdward Tomasz Napierala icl_conn_start(struct icl_conn *ic) 1208321b17ecSEdward Tomasz Napierala { 120987322a90SJohn Baldwin struct icl_soft_conn *isc = (struct icl_soft_conn *)ic; 1210321b17ecSEdward Tomasz Napierala size_t minspace; 1211321b17ecSEdward Tomasz Napierala struct sockopt opt; 1212321b17ecSEdward Tomasz Napierala int error, one = 1; 1213321b17ecSEdward Tomasz Napierala 1214321b17ecSEdward Tomasz Napierala ICL_CONN_LOCK(ic); 1215321b17ecSEdward Tomasz Napierala 1216321b17ecSEdward Tomasz Napierala /* 1217321b17ecSEdward Tomasz Napierala * XXX: Ugly hack. 1218321b17ecSEdward Tomasz Napierala */ 1219321b17ecSEdward Tomasz Napierala if (ic->ic_socket == NULL) { 1220321b17ecSEdward Tomasz Napierala ICL_CONN_UNLOCK(ic); 1221321b17ecSEdward Tomasz Napierala return (EINVAL); 1222321b17ecSEdward Tomasz Napierala } 1223321b17ecSEdward Tomasz Napierala 122487322a90SJohn Baldwin isc->receive_state = ICL_CONN_STATE_BHS; 122587322a90SJohn Baldwin isc->receive_len = sizeof(struct iscsi_bhs); 1226321b17ecSEdward Tomasz Napierala ic->ic_disconnecting = false; 1227321b17ecSEdward Tomasz Napierala 1228321b17ecSEdward Tomasz Napierala ICL_CONN_UNLOCK(ic); 1229321b17ecSEdward Tomasz Napierala 1230321b17ecSEdward Tomasz Napierala /* 1231321b17ecSEdward Tomasz Napierala * For sendspace, this is required because the current code cannot 1232321b17ecSEdward Tomasz Napierala * send a PDU in pieces; thus, the minimum buffer size is equal 1233321b17ecSEdward Tomasz Napierala * to the maximum PDU size. "+4" is to account for possible padding. 1234321b17ecSEdward Tomasz Napierala */ 12350cc7d64aSJohn Baldwin minspace = sizeof(struct iscsi_bhs) + 12360cc7d64aSJohn Baldwin ic->ic_max_send_data_segment_length + 1237321b17ecSEdward Tomasz Napierala ISCSI_HEADER_DIGEST_SIZE + ISCSI_DATA_DIGEST_SIZE + 4; 1238321b17ecSEdward Tomasz Napierala if (sendspace < minspace) { 1239321b17ecSEdward Tomasz Napierala ICL_WARN("kern.icl.sendspace too low; must be at least %zd", 1240321b17ecSEdward Tomasz Napierala minspace); 1241321b17ecSEdward Tomasz Napierala sendspace = minspace; 1242321b17ecSEdward Tomasz Napierala } 12430cc7d64aSJohn Baldwin minspace = sizeof(struct iscsi_bhs) + 12440cc7d64aSJohn Baldwin ic->ic_max_recv_data_segment_length + 12450cc7d64aSJohn Baldwin ISCSI_HEADER_DIGEST_SIZE + ISCSI_DATA_DIGEST_SIZE + 4; 1246321b17ecSEdward Tomasz Napierala if (recvspace < minspace) { 1247321b17ecSEdward Tomasz Napierala ICL_WARN("kern.icl.recvspace too low; must be at least %zd", 1248321b17ecSEdward Tomasz Napierala minspace); 1249321b17ecSEdward Tomasz Napierala recvspace = minspace; 1250321b17ecSEdward Tomasz Napierala } 1251321b17ecSEdward Tomasz Napierala 1252321b17ecSEdward Tomasz Napierala error = soreserve(ic->ic_socket, sendspace, recvspace); 1253321b17ecSEdward Tomasz Napierala if (error != 0) { 1254321b17ecSEdward Tomasz Napierala ICL_WARN("soreserve failed with error %d", error); 12555aabcd7cSEdward Tomasz Napierala icl_soft_conn_close(ic); 1256321b17ecSEdward Tomasz Napierala return (error); 1257321b17ecSEdward Tomasz Napierala } 1258321b17ecSEdward Tomasz Napierala ic->ic_socket->so_snd.sb_flags |= SB_AUTOSIZE; 1259321b17ecSEdward Tomasz Napierala ic->ic_socket->so_rcv.sb_flags |= SB_AUTOSIZE; 1260321b17ecSEdward Tomasz Napierala 1261321b17ecSEdward Tomasz Napierala /* 1262321b17ecSEdward Tomasz Napierala * Disable Nagle. 1263321b17ecSEdward Tomasz Napierala */ 1264321b17ecSEdward Tomasz Napierala bzero(&opt, sizeof(opt)); 1265321b17ecSEdward Tomasz Napierala opt.sopt_dir = SOPT_SET; 1266321b17ecSEdward Tomasz Napierala opt.sopt_level = IPPROTO_TCP; 1267321b17ecSEdward Tomasz Napierala opt.sopt_name = TCP_NODELAY; 1268321b17ecSEdward Tomasz Napierala opt.sopt_val = &one; 1269321b17ecSEdward Tomasz Napierala opt.sopt_valsize = sizeof(one); 1270321b17ecSEdward Tomasz Napierala error = sosetopt(ic->ic_socket, &opt); 1271321b17ecSEdward Tomasz Napierala if (error != 0) { 1272321b17ecSEdward Tomasz Napierala ICL_WARN("disabling TCP_NODELAY failed with error %d", error); 12735aabcd7cSEdward Tomasz Napierala icl_soft_conn_close(ic); 1274321b17ecSEdward Tomasz Napierala return (error); 1275321b17ecSEdward Tomasz Napierala } 1276321b17ecSEdward Tomasz Napierala 1277321b17ecSEdward Tomasz Napierala /* 1278321b17ecSEdward Tomasz Napierala * Register socket upcall, to get notified about incoming PDUs 1279321b17ecSEdward Tomasz Napierala * and free space to send outgoing ones. 1280321b17ecSEdward Tomasz Napierala */ 1281321b17ecSEdward Tomasz Napierala SOCKBUF_LOCK(&ic->ic_socket->so_snd); 128287322a90SJohn Baldwin soupcall_set(ic->ic_socket, SO_SND, icl_soupcall_send, isc); 1283321b17ecSEdward Tomasz Napierala SOCKBUF_UNLOCK(&ic->ic_socket->so_snd); 1284321b17ecSEdward Tomasz Napierala SOCKBUF_LOCK(&ic->ic_socket->so_rcv); 128587322a90SJohn Baldwin soupcall_set(ic->ic_socket, SO_RCV, icl_soupcall_receive, isc); 1286321b17ecSEdward Tomasz Napierala SOCKBUF_UNLOCK(&ic->ic_socket->so_rcv); 1287321b17ecSEdward Tomasz Napierala 12885b157f21SAlexander Motin /* 12895b157f21SAlexander Motin * Start threads. 12905b157f21SAlexander Motin */ 12915b157f21SAlexander Motin ICL_CONN_LOCK(ic); 129287322a90SJohn Baldwin isc->send_running = isc->receive_running = true; 12935b157f21SAlexander Motin ICL_CONN_UNLOCK(ic); 12945b157f21SAlexander Motin error = kthread_add(icl_send_thread, ic, NULL, NULL, 0, 0, "%stx", 12955b157f21SAlexander Motin ic->ic_name); 12965b157f21SAlexander Motin if (error != 0) { 12975b157f21SAlexander Motin ICL_WARN("kthread_add(9) failed with error %d", error); 12985b157f21SAlexander Motin ICL_CONN_LOCK(ic); 129987322a90SJohn Baldwin isc->send_running = isc->receive_running = false; 130087322a90SJohn Baldwin cv_signal(&isc->send_cv); 13015b157f21SAlexander Motin ICL_CONN_UNLOCK(ic); 13025b157f21SAlexander Motin icl_soft_conn_close(ic); 13035b157f21SAlexander Motin return (error); 13045b157f21SAlexander Motin } 13055b157f21SAlexander Motin error = kthread_add(icl_receive_thread, ic, NULL, NULL, 0, 0, "%srx", 13065b157f21SAlexander Motin ic->ic_name); 13075b157f21SAlexander Motin if (error != 0) { 13085b157f21SAlexander Motin ICL_WARN("kthread_add(9) failed with error %d", error); 13095b157f21SAlexander Motin ICL_CONN_LOCK(ic); 131087322a90SJohn Baldwin isc->receive_running = false; 131187322a90SJohn Baldwin cv_signal(&isc->send_cv); 13125b157f21SAlexander Motin ICL_CONN_UNLOCK(ic); 13135b157f21SAlexander Motin icl_soft_conn_close(ic); 13145b157f21SAlexander Motin return (error); 13155b157f21SAlexander Motin } 13165b157f21SAlexander Motin 1317321b17ecSEdward Tomasz Napierala return (0); 1318321b17ecSEdward Tomasz Napierala } 1319321b17ecSEdward Tomasz Napierala 1320321b17ecSEdward Tomasz Napierala int 1321321b17ecSEdward Tomasz Napierala icl_soft_conn_handoff(struct icl_conn *ic, int fd) 1322321b17ecSEdward Tomasz Napierala { 1323321b17ecSEdward Tomasz Napierala struct file *fp; 1324321b17ecSEdward Tomasz Napierala struct socket *so; 1325321b17ecSEdward Tomasz Napierala cap_rights_t rights; 1326321b17ecSEdward Tomasz Napierala int error; 1327321b17ecSEdward Tomasz Napierala 1328321b17ecSEdward Tomasz Napierala ICL_CONN_LOCK_ASSERT_NOT(ic); 1329321b17ecSEdward Tomasz Napierala 1330906a424bSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 1331906a424bSEdward Tomasz Napierala /* 1332906a424bSEdward Tomasz Napierala * We're transitioning to Full Feature phase, and we don't 1333906a424bSEdward Tomasz Napierala * really care. 1334906a424bSEdward Tomasz Napierala */ 1335906a424bSEdward Tomasz Napierala if (fd == 0) { 1336906a424bSEdward Tomasz Napierala ICL_CONN_LOCK(ic); 1337906a424bSEdward Tomasz Napierala if (ic->ic_socket == NULL) { 1338906a424bSEdward Tomasz Napierala ICL_CONN_UNLOCK(ic); 1339906a424bSEdward Tomasz Napierala ICL_WARN("proxy handoff without connect"); 1340906a424bSEdward Tomasz Napierala return (EINVAL); 1341906a424bSEdward Tomasz Napierala } 1342906a424bSEdward Tomasz Napierala ICL_CONN_UNLOCK(ic); 1343906a424bSEdward Tomasz Napierala return (0); 1344906a424bSEdward Tomasz Napierala } 1345906a424bSEdward Tomasz Napierala #endif 1346906a424bSEdward Tomasz Napierala 1347321b17ecSEdward Tomasz Napierala /* 1348321b17ecSEdward Tomasz Napierala * Steal the socket from userland. 1349321b17ecSEdward Tomasz Napierala */ 1350321b17ecSEdward Tomasz Napierala error = fget(curthread, fd, 13516b3a9a0fSMateusz Guzik cap_rights_init_one(&rights, CAP_SOCK_CLIENT), &fp); 1352321b17ecSEdward Tomasz Napierala if (error != 0) 1353321b17ecSEdward Tomasz Napierala return (error); 1354321b17ecSEdward Tomasz Napierala if (fp->f_type != DTYPE_SOCKET) { 1355321b17ecSEdward Tomasz Napierala fdrop(fp, curthread); 1356321b17ecSEdward Tomasz Napierala return (EINVAL); 1357321b17ecSEdward Tomasz Napierala } 1358321b17ecSEdward Tomasz Napierala so = fp->f_data; 1359321b17ecSEdward Tomasz Napierala if (so->so_type != SOCK_STREAM) { 1360321b17ecSEdward Tomasz Napierala fdrop(fp, curthread); 1361321b17ecSEdward Tomasz Napierala return (EINVAL); 1362321b17ecSEdward Tomasz Napierala } 1363321b17ecSEdward Tomasz Napierala 1364321b17ecSEdward Tomasz Napierala ICL_CONN_LOCK(ic); 1365321b17ecSEdward Tomasz Napierala 1366321b17ecSEdward Tomasz Napierala if (ic->ic_socket != NULL) { 1367321b17ecSEdward Tomasz Napierala ICL_CONN_UNLOCK(ic); 1368321b17ecSEdward Tomasz Napierala fdrop(fp, curthread); 1369321b17ecSEdward Tomasz Napierala return (EBUSY); 1370321b17ecSEdward Tomasz Napierala } 1371321b17ecSEdward Tomasz Napierala 1372321b17ecSEdward Tomasz Napierala ic->ic_socket = fp->f_data; 1373321b17ecSEdward Tomasz Napierala fp->f_ops = &badfileops; 1374321b17ecSEdward Tomasz Napierala fp->f_data = NULL; 1375321b17ecSEdward Tomasz Napierala fdrop(fp, curthread); 1376321b17ecSEdward Tomasz Napierala ICL_CONN_UNLOCK(ic); 1377321b17ecSEdward Tomasz Napierala 1378321b17ecSEdward Tomasz Napierala error = icl_conn_start(ic); 1379321b17ecSEdward Tomasz Napierala 1380321b17ecSEdward Tomasz Napierala return (error); 1381321b17ecSEdward Tomasz Napierala } 1382321b17ecSEdward Tomasz Napierala 1383321b17ecSEdward Tomasz Napierala void 13845aabcd7cSEdward Tomasz Napierala icl_soft_conn_close(struct icl_conn *ic) 1385321b17ecSEdward Tomasz Napierala { 138687322a90SJohn Baldwin struct icl_soft_conn *isc = (struct icl_soft_conn *)ic; 1387321b17ecSEdward Tomasz Napierala struct icl_pdu *pdu; 13885b157f21SAlexander Motin struct socket *so; 1389321b17ecSEdward Tomasz Napierala 13905b157f21SAlexander Motin /* 13915b157f21SAlexander Motin * Wake up the threads, so they can properly terminate. 13926895f89fSAlexander Motin * Receive thread sleeps on so->so_rcv lock, send on ic->ic_lock. 13935b157f21SAlexander Motin */ 13946895f89fSAlexander Motin ICL_CONN_LOCK(ic); 13956895f89fSAlexander Motin if (!ic->ic_disconnecting) { 13966895f89fSAlexander Motin so = ic->ic_socket; 139706e9c710SAlexander Motin if (so) 13986895f89fSAlexander Motin SOCKBUF_LOCK(&so->so_rcv); 13995b157f21SAlexander Motin ic->ic_disconnecting = true; 140006e9c710SAlexander Motin if (so) 14016895f89fSAlexander Motin SOCKBUF_UNLOCK(&so->so_rcv); 14026895f89fSAlexander Motin } 140387322a90SJohn Baldwin while (isc->receive_running || isc->send_running) { 140487322a90SJohn Baldwin cv_signal(&isc->receive_cv); 140587322a90SJohn Baldwin cv_signal(&isc->send_cv); 140687322a90SJohn Baldwin cv_wait(&isc->send_cv, ic->ic_lock); 14075b157f21SAlexander Motin } 14085b157f21SAlexander Motin 14095b157f21SAlexander Motin /* Some other thread could close the connection same time. */ 14105b157f21SAlexander Motin so = ic->ic_socket; 14115b157f21SAlexander Motin if (so == NULL) { 1412321b17ecSEdward Tomasz Napierala ICL_CONN_UNLOCK(ic); 1413321b17ecSEdward Tomasz Napierala return; 1414321b17ecSEdward Tomasz Napierala } 14155b157f21SAlexander Motin ic->ic_socket = NULL; 1416321b17ecSEdward Tomasz Napierala 1417321b17ecSEdward Tomasz Napierala /* 1418321b17ecSEdward Tomasz Napierala * Deregister socket upcalls. 1419321b17ecSEdward Tomasz Napierala */ 1420321b17ecSEdward Tomasz Napierala ICL_CONN_UNLOCK(ic); 14215b157f21SAlexander Motin SOCKBUF_LOCK(&so->so_snd); 14225b157f21SAlexander Motin if (so->so_snd.sb_upcall != NULL) 14235b157f21SAlexander Motin soupcall_clear(so, SO_SND); 14245b157f21SAlexander Motin SOCKBUF_UNLOCK(&so->so_snd); 14255b157f21SAlexander Motin SOCKBUF_LOCK(&so->so_rcv); 14265b157f21SAlexander Motin if (so->so_rcv.sb_upcall != NULL) 14275b157f21SAlexander Motin soupcall_clear(so, SO_RCV); 14285b157f21SAlexander Motin SOCKBUF_UNLOCK(&so->so_rcv); 14295b157f21SAlexander Motin soclose(so); 1430321b17ecSEdward Tomasz Napierala ICL_CONN_LOCK(ic); 1431321b17ecSEdward Tomasz Napierala 143287322a90SJohn Baldwin if (isc->receive_pdu != NULL) { 1433321b17ecSEdward Tomasz Napierala //ICL_DEBUG("freeing partially received PDU"); 143487322a90SJohn Baldwin icl_soft_conn_pdu_free(ic, isc->receive_pdu); 143587322a90SJohn Baldwin isc->receive_pdu = NULL; 1436321b17ecSEdward Tomasz Napierala } 1437321b17ecSEdward Tomasz Napierala 1438321b17ecSEdward Tomasz Napierala /* 1439321b17ecSEdward Tomasz Napierala * Remove any outstanding PDUs from the send queue. 1440321b17ecSEdward Tomasz Napierala */ 144187322a90SJohn Baldwin while (!STAILQ_EMPTY(&isc->to_send)) { 144287322a90SJohn Baldwin pdu = STAILQ_FIRST(&isc->to_send); 144387322a90SJohn Baldwin STAILQ_REMOVE_HEAD(&isc->to_send, ip_next); 14449a4510acSAlexander Motin icl_soft_pdu_done(pdu, ENOTCONN); 1445321b17ecSEdward Tomasz Napierala } 1446321b17ecSEdward Tomasz Napierala 144787322a90SJohn Baldwin KASSERT(STAILQ_EMPTY(&isc->to_send), 1448321b17ecSEdward Tomasz Napierala ("destroying session with non-empty send queue")); 1449321b17ecSEdward Tomasz Napierala ICL_CONN_UNLOCK(ic); 1450321b17ecSEdward Tomasz Napierala } 1451321b17ecSEdward Tomasz Napierala 14527a03d007SEdward Tomasz Napierala int 1453604c023fSEdward Tomasz Napierala icl_soft_conn_task_setup(struct icl_conn *ic, struct icl_pdu *ip, 1454604c023fSEdward Tomasz Napierala struct ccb_scsiio *csio, uint32_t *task_tagp, void **prvp) 14557a03d007SEdward Tomasz Napierala { 14567a03d007SEdward Tomasz Napierala 14577a03d007SEdward Tomasz Napierala return (0); 14587a03d007SEdward Tomasz Napierala } 14597a03d007SEdward Tomasz Napierala 14607a03d007SEdward Tomasz Napierala void 14617a03d007SEdward Tomasz Napierala icl_soft_conn_task_done(struct icl_conn *ic, void *prv) 14627a03d007SEdward Tomasz Napierala { 14637a03d007SEdward Tomasz Napierala } 14647a03d007SEdward Tomasz Napierala 14657a03d007SEdward Tomasz Napierala int 1466*8903d8e3SJohn Baldwin icl_soft_conn_transfer_setup(struct icl_conn *ic, struct icl_pdu *ip, 1467*8903d8e3SJohn Baldwin union ctl_io *io, uint32_t *transfer_tag, void **prvp) 14687a03d007SEdward Tomasz Napierala { 14697a03d007SEdward Tomasz Napierala 14707a03d007SEdward Tomasz Napierala return (0); 14717a03d007SEdward Tomasz Napierala } 14727a03d007SEdward Tomasz Napierala 14737a03d007SEdward Tomasz Napierala void 14747a03d007SEdward Tomasz Napierala icl_soft_conn_transfer_done(struct icl_conn *ic, void *prv) 14757a03d007SEdward Tomasz Napierala { 14767a03d007SEdward Tomasz Napierala } 14777a03d007SEdward Tomasz Napierala 1478321b17ecSEdward Tomasz Napierala static int 147997b84d34SNavdeep Parhar icl_soft_limits(struct icl_drv_limits *idl) 1480321b17ecSEdward Tomasz Napierala { 1481321b17ecSEdward Tomasz Napierala 1482b75168edSAlexander Motin idl->idl_max_recv_data_segment_length = max_data_segment_length; 1483b75168edSAlexander Motin idl->idl_max_send_data_segment_length = max_data_segment_length; 1484b75168edSAlexander Motin idl->idl_max_burst_length = max_burst_length; 1485b75168edSAlexander Motin idl->idl_first_burst_length = first_burst_length; 1486321b17ecSEdward Tomasz Napierala 1487321b17ecSEdward Tomasz Napierala return (0); 1488321b17ecSEdward Tomasz Napierala } 1489321b17ecSEdward Tomasz Napierala 1490321b17ecSEdward Tomasz Napierala #ifdef ICL_KERNEL_PROXY 1491321b17ecSEdward Tomasz Napierala int 1492f41492b0SEdward Tomasz Napierala icl_soft_conn_connect(struct icl_conn *ic, int domain, int socktype, 1493f41492b0SEdward Tomasz Napierala int protocol, struct sockaddr *from_sa, struct sockaddr *to_sa) 1494f41492b0SEdward Tomasz Napierala { 1495f41492b0SEdward Tomasz Napierala 1496f41492b0SEdward Tomasz Napierala return (icl_soft_proxy_connect(ic, domain, socktype, protocol, 1497f41492b0SEdward Tomasz Napierala from_sa, to_sa)); 1498f41492b0SEdward Tomasz Napierala } 1499f41492b0SEdward Tomasz Napierala 1500f41492b0SEdward Tomasz Napierala int 1501f41492b0SEdward Tomasz Napierala icl_soft_handoff_sock(struct icl_conn *ic, struct socket *so) 1502321b17ecSEdward Tomasz Napierala { 1503321b17ecSEdward Tomasz Napierala int error; 1504321b17ecSEdward Tomasz Napierala 1505321b17ecSEdward Tomasz Napierala ICL_CONN_LOCK_ASSERT_NOT(ic); 1506321b17ecSEdward Tomasz Napierala 1507321b17ecSEdward Tomasz Napierala if (so->so_type != SOCK_STREAM) 1508321b17ecSEdward Tomasz Napierala return (EINVAL); 1509321b17ecSEdward Tomasz Napierala 1510321b17ecSEdward Tomasz Napierala ICL_CONN_LOCK(ic); 1511321b17ecSEdward Tomasz Napierala if (ic->ic_socket != NULL) { 1512321b17ecSEdward Tomasz Napierala ICL_CONN_UNLOCK(ic); 1513321b17ecSEdward Tomasz Napierala return (EBUSY); 1514321b17ecSEdward Tomasz Napierala } 1515321b17ecSEdward Tomasz Napierala ic->ic_socket = so; 1516321b17ecSEdward Tomasz Napierala ICL_CONN_UNLOCK(ic); 1517321b17ecSEdward Tomasz Napierala 1518321b17ecSEdward Tomasz Napierala error = icl_conn_start(ic); 1519321b17ecSEdward Tomasz Napierala 1520321b17ecSEdward Tomasz Napierala return (error); 1521321b17ecSEdward Tomasz Napierala } 1522321b17ecSEdward Tomasz Napierala #endif /* ICL_KERNEL_PROXY */ 1523321b17ecSEdward Tomasz Napierala 1524321b17ecSEdward Tomasz Napierala static int 1525321b17ecSEdward Tomasz Napierala icl_soft_load(void) 1526321b17ecSEdward Tomasz Napierala { 1527321b17ecSEdward Tomasz Napierala int error; 1528321b17ecSEdward Tomasz Napierala 15299a4510acSAlexander Motin icl_soft_pdu_zone = uma_zcreate("icl_soft_pdu", 15309a4510acSAlexander Motin sizeof(struct icl_soft_pdu), NULL, NULL, NULL, NULL, 1531321b17ecSEdward Tomasz Napierala UMA_ALIGN_PTR, 0); 1532321b17ecSEdward Tomasz Napierala refcount_init(&icl_ncons, 0); 1533321b17ecSEdward Tomasz Napierala 1534321b17ecSEdward Tomasz Napierala /* 1535321b17ecSEdward Tomasz Napierala * The reason we call this "none" is that to the user, 1536321b17ecSEdward Tomasz Napierala * it's known as "offload driver"; "offload driver: soft" 1537321b17ecSEdward Tomasz Napierala * doesn't make much sense. 1538321b17ecSEdward Tomasz Napierala */ 1539b8911594SEdward Tomasz Napierala error = icl_register("none", false, 0, 1540b8911594SEdward Tomasz Napierala icl_soft_limits, icl_soft_new_conn); 1541321b17ecSEdward Tomasz Napierala KASSERT(error == 0, ("failed to register")); 1542321b17ecSEdward Tomasz Napierala 1543b8911594SEdward Tomasz Napierala #if defined(ICL_KERNEL_PROXY) && 0 1544b8911594SEdward Tomasz Napierala /* 1545b8911594SEdward Tomasz Napierala * Debugging aid for kernel proxy functionality. 1546b8911594SEdward Tomasz Napierala */ 1547b8911594SEdward Tomasz Napierala error = icl_register("proxytest", true, 0, 1548b8911594SEdward Tomasz Napierala icl_soft_limits, icl_soft_new_conn); 1549b8911594SEdward Tomasz Napierala KASSERT(error == 0, ("failed to register")); 1550b8911594SEdward Tomasz Napierala #endif 1551b8911594SEdward Tomasz Napierala 1552321b17ecSEdward Tomasz Napierala return (error); 1553321b17ecSEdward Tomasz Napierala } 1554321b17ecSEdward Tomasz Napierala 1555321b17ecSEdward Tomasz Napierala static int 1556321b17ecSEdward Tomasz Napierala icl_soft_unload(void) 1557321b17ecSEdward Tomasz Napierala { 1558321b17ecSEdward Tomasz Napierala 1559321b17ecSEdward Tomasz Napierala if (icl_ncons != 0) 1560321b17ecSEdward Tomasz Napierala return (EBUSY); 1561321b17ecSEdward Tomasz Napierala 1562b8911594SEdward Tomasz Napierala icl_unregister("none", false); 1563b8911594SEdward Tomasz Napierala #if defined(ICL_KERNEL_PROXY) && 0 1564b8911594SEdward Tomasz Napierala icl_unregister("proxytest", true); 1565b8911594SEdward Tomasz Napierala #endif 1566321b17ecSEdward Tomasz Napierala 15679a4510acSAlexander Motin uma_zdestroy(icl_soft_pdu_zone); 1568321b17ecSEdward Tomasz Napierala 1569321b17ecSEdward Tomasz Napierala return (0); 1570321b17ecSEdward Tomasz Napierala } 1571321b17ecSEdward Tomasz Napierala 1572321b17ecSEdward Tomasz Napierala static int 1573321b17ecSEdward Tomasz Napierala icl_soft_modevent(module_t mod, int what, void *arg) 1574321b17ecSEdward Tomasz Napierala { 1575321b17ecSEdward Tomasz Napierala 1576321b17ecSEdward Tomasz Napierala switch (what) { 1577321b17ecSEdward Tomasz Napierala case MOD_LOAD: 1578321b17ecSEdward Tomasz Napierala return (icl_soft_load()); 1579321b17ecSEdward Tomasz Napierala case MOD_UNLOAD: 1580321b17ecSEdward Tomasz Napierala return (icl_soft_unload()); 1581321b17ecSEdward Tomasz Napierala default: 1582321b17ecSEdward Tomasz Napierala return (EINVAL); 1583321b17ecSEdward Tomasz Napierala } 1584321b17ecSEdward Tomasz Napierala } 1585321b17ecSEdward Tomasz Napierala 1586321b17ecSEdward Tomasz Napierala moduledata_t icl_soft_data = { 1587321b17ecSEdward Tomasz Napierala "icl_soft", 1588321b17ecSEdward Tomasz Napierala icl_soft_modevent, 1589321b17ecSEdward Tomasz Napierala 0 1590321b17ecSEdward Tomasz Napierala }; 1591321b17ecSEdward Tomasz Napierala 1592321b17ecSEdward Tomasz Napierala DECLARE_MODULE(icl_soft, icl_soft_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); 1593321b17ecSEdward Tomasz Napierala MODULE_DEPEND(icl_soft, icl, 1, 1, 1); 1594872d2d92SEdward Tomasz Napierala MODULE_VERSION(icl_soft, 1); 1595