1 /*- 2 * Copyright (c) 2012 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Edward Tomasz Napierala under sponsorship 6 * from the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #ifndef ICL_H 33 #define ICL_H 34 35 /* 36 * iSCSI Common Layer. It's used by both the initiator and target to send 37 * and receive iSCSI PDUs. 38 */ 39 40 struct icl_conn; 41 42 struct icl_pdu { 43 TAILQ_ENTRY(icl_pdu) ip_next; 44 struct icl_conn *ip_conn; 45 struct iscsi_bhs *ip_bhs; 46 struct mbuf *ip_bhs_mbuf; 47 size_t ip_ahs_len; 48 struct mbuf *ip_ahs_mbuf; 49 size_t ip_data_len; 50 struct mbuf *ip_data_mbuf; 51 52 /* 53 * User (initiator or provider) private fields. 54 */ 55 uint32_t ip_prv0; 56 uint32_t ip_prv1; 57 uint32_t ip_prv2; 58 }; 59 60 struct icl_pdu *icl_pdu_new_bhs(struct icl_conn *ic, int flags); 61 size_t icl_pdu_data_segment_length(const struct icl_pdu *ip); 62 int icl_pdu_append_data(struct icl_pdu *ip, const void *addr, size_t len, int flags); 63 void icl_pdu_get_data(struct icl_pdu *ip, size_t off, void *addr, size_t len); 64 void icl_pdu_queue(struct icl_pdu *ip); 65 void icl_pdu_free(struct icl_pdu *ip); 66 67 #define ICL_CONN_STATE_INVALID 0 68 #define ICL_CONN_STATE_BHS 1 69 #define ICL_CONN_STATE_AHS 2 70 #define ICL_CONN_STATE_HEADER_DIGEST 3 71 #define ICL_CONN_STATE_DATA 4 72 #define ICL_CONN_STATE_DATA_DIGEST 5 73 74 #define ICL_MAX_DATA_SEGMENT_LENGTH (128 * 1024) 75 76 struct icl_conn { 77 struct mtx ic_lock; 78 struct socket *ic_socket; 79 volatile u_int ic_outstanding_pdus; 80 TAILQ_HEAD(, icl_pdu) ic_to_send; 81 size_t ic_receive_len; 82 int ic_receive_state; 83 struct icl_pdu *ic_receive_pdu; 84 struct cv ic_send_cv; 85 struct cv ic_receive_cv; 86 bool ic_header_crc32c; 87 bool ic_data_crc32c; 88 bool ic_send_running; 89 bool ic_receive_running; 90 size_t ic_max_data_segment_length; 91 bool ic_disconnecting; 92 bool ic_iser; 93 94 void (*ic_receive)(struct icl_pdu *); 95 void (*ic_error)(struct icl_conn *); 96 97 /* 98 * User (initiator or provider) private fields. 99 */ 100 void *ic_prv0; 101 }; 102 103 struct icl_conn *icl_conn_new(void); 104 void icl_conn_free(struct icl_conn *ic); 105 int icl_conn_handoff(struct icl_conn *ic, int fd); 106 void icl_conn_shutdown(struct icl_conn *ic); 107 void icl_conn_close(struct icl_conn *ic); 108 bool icl_conn_connected(struct icl_conn *ic); 109 110 #ifdef ICL_KERNEL_PROXY 111 112 struct sockaddr; 113 struct icl_listen; 114 115 struct icl_listen_sock { 116 TAILQ_ENTRY(icl_listen_sock) ils_next; 117 struct icl_listen *ils_listen; 118 struct socket *ils_socket; 119 bool ils_running; 120 bool ils_disconnecting; 121 }; 122 123 struct icl_listen { 124 TAILQ_HEAD(, icl_listen_sock) il_sockets; 125 struct sx il_lock; 126 void (*il_accept)(struct socket *); 127 }; 128 129 /* 130 * Initiator part. 131 */ 132 int icl_conn_connect(struct icl_conn *ic, bool rdma, 133 int domain, int socktype, int protocol, 134 struct sockaddr *from_sa, struct sockaddr *to_sa); 135 /* 136 * Target part. 137 */ 138 struct icl_listen *icl_listen_new(void (*accept_cb)(struct socket *)); 139 void icl_listen_free(struct icl_listen *il); 140 int icl_listen_add(struct icl_listen *il, bool rdma, int domain, 141 int socktype, int protocol, struct sockaddr *sa); 142 int icl_listen_remove(struct icl_listen *il, struct sockaddr *sa); 143 144 /* 145 * This one is not a public API; only to be used by icl_proxy.c. 146 */ 147 int icl_conn_handoff_sock(struct icl_conn *ic, struct socket *so); 148 149 #endif /* ICL_KERNEL_PROXY */ 150 151 #endif /* !ICL_H */ 152