icl.h revision 265495
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: stable/10/sys/dev/iscsi/icl.h 265495 2014-05-07 06:29:01Z 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	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
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	TAILQ_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
96	void			(*ic_receive)(struct icl_pdu *);
97	void			(*ic_error)(struct icl_conn *);
98
99	/*
100	 * User (initiator or provider) private fields.
101	 */
102	void			*ic_prv0;
103};
104
105struct icl_conn		*icl_conn_new(struct mtx *lock);
106void			icl_conn_free(struct icl_conn *ic);
107int			icl_conn_handoff(struct icl_conn *ic, int fd);
108void			icl_conn_shutdown(struct icl_conn *ic);
109void			icl_conn_close(struct icl_conn *ic);
110bool			icl_conn_connected(struct icl_conn *ic);
111
112#ifdef ICL_KERNEL_PROXY
113
114struct sockaddr;
115struct icl_listen;
116
117struct icl_listen_sock {
118	TAILQ_ENTRY(icl_listen_sock)	ils_next;
119	struct icl_listen	*ils_listen;
120	struct socket		*ils_socket;
121	bool			ils_running;
122	bool			ils_disconnecting;
123};
124
125struct icl_listen	{
126	TAILQ_HEAD(, icl_listen_sock)	il_sockets;
127	struct sx			il_lock;
128	void				(*il_accept)(struct socket *);
129};
130
131/*
132 * Initiator part.
133 */
134int			icl_conn_connect(struct icl_conn *ic, bool rdma,
135			    int domain, int socktype, int protocol,
136			    struct sockaddr *from_sa, struct sockaddr *to_sa);
137/*
138 * Target part.
139 */
140struct icl_listen	*icl_listen_new(void (*accept_cb)(struct socket *));
141void			icl_listen_free(struct icl_listen *il);
142int			icl_listen_add(struct icl_listen *il, bool rdma, int domain,
143    int socktype, int protocol, struct sockaddr *sa);
144int			icl_listen_remove(struct icl_listen *il, struct sockaddr *sa);
145
146/*
147 * This one is not a public API; only to be used by icl_proxy.c.
148 */
149int			icl_conn_handoff_sock(struct icl_conn *ic, struct socket *so);
150
151#endif /* ICL_KERNEL_PROXY */
152
153#endif /* !ICL_H */
154