icl.h revision 264109
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: head/sys/dev/iscsi/icl.h 264109 2014-04-04 08:43:23Z trasz $
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
40struct icl_conn;
41
42struct 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
60struct icl_pdu		*icl_pdu_new_bhs(struct icl_conn *ic, int flags);
61size_t			icl_pdu_data_segment_length(const struct icl_pdu *ip);
62int			icl_pdu_append_data(struct icl_pdu *ip, const void *addr, size_t len, int flags);
63void			icl_pdu_get_data(struct icl_pdu *ip, size_t off, void *addr, size_t len);
64void			icl_pdu_queue(struct icl_pdu *ip);
65void			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
76struct 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	size_t			ic_receive_len;
84	int			ic_receive_state;
85	struct icl_pdu		*ic_receive_pdu;
86	struct cv		ic_send_cv;
87	struct cv		ic_receive_cv;
88	bool			ic_header_crc32c;
89	bool			ic_data_crc32c;
90	bool			ic_send_running;
91	bool			ic_receive_running;
92	size_t			ic_max_data_segment_length;
93	bool			ic_disconnecting;
94	bool			ic_iser;
95	const char		*ic_name;
96
97	void			(*ic_receive)(struct icl_pdu *);
98	void			(*ic_error)(struct icl_conn *);
99
100	/*
101	 * User (initiator or provider) private fields.
102	 */
103	void			*ic_prv0;
104};
105
106struct icl_conn		*icl_conn_new(const char *name, struct mtx *lock);
107void			icl_conn_free(struct icl_conn *ic);
108int			icl_conn_handoff(struct icl_conn *ic, int fd);
109void			icl_conn_shutdown(struct icl_conn *ic);
110void			icl_conn_close(struct icl_conn *ic);
111bool			icl_conn_connected(struct icl_conn *ic);
112
113#ifdef ICL_KERNEL_PROXY
114
115struct sockaddr;
116struct icl_listen;
117
118struct 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};
125
126struct icl_listen	{
127	TAILQ_HEAD(, icl_listen_sock)	il_sockets;
128	struct sx			il_lock;
129	void				(*il_accept)(struct socket *);
130};
131
132/*
133 * Initiator part.
134 */
135int			icl_conn_connect(struct icl_conn *ic, bool rdma,
136			    int domain, int socktype, int protocol,
137			    struct sockaddr *from_sa, struct sockaddr *to_sa);
138/*
139 * Target part.
140 */
141struct icl_listen	*icl_listen_new(void (*accept_cb)(struct socket *));
142void			icl_listen_free(struct icl_listen *il);
143int			icl_listen_add(struct icl_listen *il, bool rdma, int domain,
144    int socktype, int protocol, struct sockaddr *sa);
145int			icl_listen_remove(struct icl_listen *il, struct sockaddr *sa);
146
147/*
148 * This one is not a public API; only to be used by icl_proxy.c.
149 */
150int			icl_conn_handoff_sock(struct icl_conn *ic, struct socket *so);
151
152#endif /* ICL_KERNEL_PROXY */
153
154#endif /* !ICL_H */
155