icl.h revision 264526
1255570Strasz/*-
2255570Strasz * Copyright (c) 2012 The FreeBSD Foundation
3255570Strasz * All rights reserved.
4255570Strasz *
5255570Strasz * This software was developed by Edward Tomasz Napierala under sponsorship
6255570Strasz * from the FreeBSD Foundation.
7255570Strasz *
8255570Strasz * Redistribution and use in source and binary forms, with or without
9255570Strasz * modification, are permitted provided that the following conditions
10255570Strasz * are met:
11255570Strasz * 1. Redistributions of source code must retain the above copyright
12255570Strasz *    notice, this list of conditions and the following disclaimer.
13255570Strasz * 2. Redistributions in binary form must reproduce the above copyright
14255570Strasz *    notice, this list of conditions and the following disclaimer in the
15255570Strasz *    documentation and/or other materials provided with the distribution.
16255570Strasz *
17255570Strasz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18255570Strasz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19255570Strasz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20255570Strasz * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21255570Strasz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22255570Strasz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23255570Strasz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24255570Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25255570Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26255570Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27255570Strasz * SUCH DAMAGE.
28255570Strasz *
29255570Strasz * $FreeBSD: head/sys/dev/iscsi/icl.h 264526 2014-04-16 10:29:34Z trasz $
30255570Strasz */
31255570Strasz
32255570Strasz#ifndef ICL_H
33255570Strasz#define	ICL_H
34255570Strasz
35255570Strasz/*
36255570Strasz * iSCSI Common Layer.  It's used by both the initiator and target to send
37255570Strasz * and receive iSCSI PDUs.
38255570Strasz */
39255570Strasz
40255570Straszstruct icl_conn;
41255570Strasz
42255570Straszstruct icl_pdu {
43264109Strasz	STAILQ_ENTRY(icl_pdu)	ip_next;
44255570Strasz	struct icl_conn		*ip_conn;
45255570Strasz	struct iscsi_bhs	*ip_bhs;
46255570Strasz	struct mbuf		*ip_bhs_mbuf;
47255570Strasz	size_t			ip_ahs_len;
48255570Strasz	struct mbuf		*ip_ahs_mbuf;
49255570Strasz	size_t			ip_data_len;
50255570Strasz	struct mbuf		*ip_data_mbuf;
51255570Strasz
52255570Strasz	/*
53255570Strasz	 * User (initiator or provider) private fields.
54255570Strasz	 */
55255570Strasz	uint32_t		ip_prv0;
56255570Strasz	uint32_t		ip_prv1;
57255570Strasz	uint32_t		ip_prv2;
58255570Strasz};
59255570Strasz
60255570Straszstruct icl_pdu		*icl_pdu_new_bhs(struct icl_conn *ic, int flags);
61255570Straszsize_t			icl_pdu_data_segment_length(const struct icl_pdu *ip);
62255570Straszint			icl_pdu_append_data(struct icl_pdu *ip, const void *addr, size_t len, int flags);
63255570Straszvoid			icl_pdu_get_data(struct icl_pdu *ip, size_t off, void *addr, size_t len);
64255570Straszvoid			icl_pdu_queue(struct icl_pdu *ip);
65255570Straszvoid			icl_pdu_free(struct icl_pdu *ip);
66255570Strasz
67255570Strasz#define ICL_CONN_STATE_INVALID		0
68255570Strasz#define ICL_CONN_STATE_BHS		1
69255570Strasz#define ICL_CONN_STATE_AHS		2
70255570Strasz#define ICL_CONN_STATE_HEADER_DIGEST	3
71255570Strasz#define ICL_CONN_STATE_DATA		4
72255570Strasz#define ICL_CONN_STATE_DATA_DIGEST	5
73255570Strasz
74255570Strasz#define	ICL_MAX_DATA_SEGMENT_LENGTH	(128 * 1024)
75255570Strasz
76255570Straszstruct icl_conn {
77264022Strasz	struct mtx		*ic_lock;
78255570Strasz	struct socket		*ic_socket;
79263743Strasz#ifdef DIAGNOSTIC
80255570Strasz	volatile u_int		ic_outstanding_pdus;
81263743Strasz#endif
82264109Strasz	STAILQ_HEAD(, icl_pdu)	ic_to_send;
83264122Strasz	bool			ic_check_send_space;
84255570Strasz	size_t			ic_receive_len;
85255570Strasz	int			ic_receive_state;
86255570Strasz	struct icl_pdu		*ic_receive_pdu;
87255570Strasz	struct cv		ic_send_cv;
88255570Strasz	struct cv		ic_receive_cv;
89255570Strasz	bool			ic_header_crc32c;
90255570Strasz	bool			ic_data_crc32c;
91255570Strasz	bool			ic_send_running;
92255570Strasz	bool			ic_receive_running;
93255570Strasz	size_t			ic_max_data_segment_length;
94255570Strasz	bool			ic_disconnecting;
95255570Strasz	bool			ic_iser;
96264023Strasz	const char		*ic_name;
97255570Strasz
98255570Strasz	void			(*ic_receive)(struct icl_pdu *);
99255570Strasz	void			(*ic_error)(struct icl_conn *);
100255570Strasz
101255570Strasz	/*
102255570Strasz	 * User (initiator or provider) private fields.
103255570Strasz	 */
104255570Strasz	void			*ic_prv0;
105255570Strasz};
106255570Strasz
107264023Straszstruct icl_conn		*icl_conn_new(const char *name, struct mtx *lock);
108255570Straszvoid			icl_conn_free(struct icl_conn *ic);
109255570Straszint			icl_conn_handoff(struct icl_conn *ic, int fd);
110255570Straszvoid			icl_conn_shutdown(struct icl_conn *ic);
111255570Straszvoid			icl_conn_close(struct icl_conn *ic);
112255570Straszbool			icl_conn_connected(struct icl_conn *ic);
113255570Strasz
114255570Strasz#ifdef ICL_KERNEL_PROXY
115255570Strasz
116255570Straszstruct sockaddr;
117255570Straszstruct icl_listen;
118255570Strasz
119255570Straszstruct icl_listen_sock {
120255570Strasz	TAILQ_ENTRY(icl_listen_sock)	ils_next;
121264526Strasz	struct icl_listen		*ils_listen;
122264526Strasz	struct socket			*ils_socket;
123264526Strasz	bool				ils_running;
124264526Strasz	bool				ils_disconnecting;
125264526Strasz	int				ils_id;
126255570Strasz};
127255570Strasz
128255570Straszstruct icl_listen	{
129255570Strasz	TAILQ_HEAD(, icl_listen_sock)	il_sockets;
130255570Strasz	struct sx			il_lock;
131264526Strasz	void				(*il_accept)(struct socket *, int);
132255570Strasz};
133255570Strasz
134255570Strasz/*
135255570Strasz * Initiator part.
136255570Strasz */
137255570Straszint			icl_conn_connect(struct icl_conn *ic, bool rdma,
138255570Strasz			    int domain, int socktype, int protocol,
139255570Strasz			    struct sockaddr *from_sa, struct sockaddr *to_sa);
140255570Strasz/*
141255570Strasz * Target part.
142255570Strasz */
143264526Straszstruct icl_listen	*icl_listen_new(void (*accept_cb)(struct socket *, int));
144255570Straszvoid			icl_listen_free(struct icl_listen *il);
145264526Straszint			icl_listen_add(struct icl_listen *il, bool rdma,
146264526Strasz			    int domain, int socktype, int protocol,
147264526Strasz			    struct sockaddr *sa, int portal_id);
148255570Straszint			icl_listen_remove(struct icl_listen *il, struct sockaddr *sa);
149255570Strasz
150255570Strasz/*
151255570Strasz * This one is not a public API; only to be used by icl_proxy.c.
152255570Strasz */
153255570Straszint			icl_conn_handoff_sock(struct icl_conn *ic, struct socket *so);
154255570Strasz
155255570Strasz#endif /* ICL_KERNEL_PROXY */
156255570Strasz
157255570Strasz#endif /* !ICL_H */
158