xref: /freebsd/sys/dev/iscsi/icl.h (revision 5bd73b51076b5cb5a2c9810f76c1d7ed20c4460e)
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 	STAILQ_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 #ifdef DIAGNOSTIC
80 	volatile u_int		ic_outstanding_pdus;
81 #endif
82 	STAILQ_HEAD(, icl_pdu)	ic_to_send;
83 	bool			ic_check_send_space;
84 	size_t			ic_receive_len;
85 	int			ic_receive_state;
86 	struct icl_pdu		*ic_receive_pdu;
87 	struct cv		ic_send_cv;
88 	struct cv		ic_receive_cv;
89 	bool			ic_header_crc32c;
90 	bool			ic_data_crc32c;
91 	bool			ic_send_running;
92 	bool			ic_receive_running;
93 	size_t			ic_max_data_segment_length;
94 	bool			ic_disconnecting;
95 	bool			ic_iser;
96 	const char		*ic_name;
97 
98 	void			(*ic_receive)(struct icl_pdu *);
99 	void			(*ic_error)(struct icl_conn *);
100 
101 	/*
102 	 * User (initiator or provider) private fields.
103 	 */
104 	void			*ic_prv0;
105 };
106 
107 struct icl_conn		*icl_conn_new(const char *name, struct mtx *lock);
108 void			icl_conn_free(struct icl_conn *ic);
109 int			icl_conn_handoff(struct icl_conn *ic, int fd);
110 void			icl_conn_close(struct icl_conn *ic);
111 bool			icl_conn_connected(struct icl_conn *ic);
112 
113 #ifdef ICL_KERNEL_PROXY
114 
115 struct sockaddr;
116 struct icl_listen;
117 
118 struct icl_listen_sock {
119 	TAILQ_ENTRY(icl_listen_sock)	ils_next;
120 	struct icl_listen		*ils_listen;
121 	struct socket			*ils_socket;
122 	bool				ils_running;
123 	bool				ils_disconnecting;
124 	int				ils_id;
125 };
126 
127 struct icl_listen	{
128 	TAILQ_HEAD(, icl_listen_sock)	il_sockets;
129 	struct sx			il_lock;
130 	void				(*il_accept)(struct socket *,
131 					    struct sockaddr *, int);
132 };
133 
134 /*
135  * Initiator part.
136  */
137 int			icl_conn_connect(struct icl_conn *ic, bool rdma,
138 			    int domain, int socktype, int protocol,
139 			    struct sockaddr *from_sa, struct sockaddr *to_sa);
140 /*
141  * Target part.
142  */
143 struct icl_listen	*icl_listen_new(void (*accept_cb)(struct socket *,
144 			    struct sockaddr *, int));
145 void			icl_listen_free(struct icl_listen *il);
146 int			icl_listen_add(struct icl_listen *il, bool rdma,
147 			    int domain, int socktype, int protocol,
148 			    struct sockaddr *sa, int portal_id);
149 int			icl_listen_remove(struct icl_listen *il, struct sockaddr *sa);
150 
151 /*
152  * This one is not a public API; only to be used by icl_proxy.c.
153  */
154 int			icl_conn_handoff_sock(struct icl_conn *ic, struct socket *so);
155 
156 #endif /* ICL_KERNEL_PROXY */
157 
158 #endif /* !ICL_H */
159