xref: /spdk/lib/iscsi/conn.h (revision cc6920a4763d4b9a43aa40583c8397d8f14fa100)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (C) 2008-2012 Daisuke Aoyama <aoyama@peach.ne.jp>.
5  *   Copyright (c) Intel Corporation.
6  *   All rights reserved.
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  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #ifndef SPDK_ISCSI_CONN_H
36 #define SPDK_ISCSI_CONN_H
37 
38 #include "spdk/stdinc.h"
39 
40 #include "iscsi/iscsi.h"
41 #include "spdk/queue.h"
42 #include "spdk/cpuset.h"
43 #include "spdk/scsi.h"
44 
45 #include "spdk_internal/trace_defs.h"
46 
47 /*
48  * MAX_CONNECTION_PARAMS: The numbers of the params in conn_param_table
49  * MAX_SESSION_PARAMS: The numbers of the params in sess_param_table
50  */
51 #define MAX_CONNECTION_PARAMS 14
52 #define MAX_SESSION_PARAMS 19
53 
54 #define MAX_ADDRBUF 64
55 #define MAX_INITIATOR_ADDR (MAX_ADDRBUF)
56 #define MAX_TARGET_ADDR (MAX_ADDRBUF)
57 
58 enum iscsi_pdu_recv_state {
59 	/* Ready to wait for PDU */
60 	ISCSI_PDU_RECV_STATE_AWAIT_PDU_READY,
61 
62 	/* Active connection waiting for any PDU header */
63 	ISCSI_PDU_RECV_STATE_AWAIT_PDU_HDR,
64 
65 	/* Active connection waiting for payload */
66 	ISCSI_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD,
67 
68 	/* Active connection does not wait for payload */
69 	ISCSI_PDU_RECV_STATE_ERROR,
70 };
71 
72 struct spdk_poller;
73 struct spdk_iscsi_conn;
74 
75 struct spdk_iscsi_lun {
76 	struct spdk_iscsi_conn		*conn;
77 	struct spdk_scsi_lun		*lun;
78 	struct spdk_scsi_lun_desc	*desc;
79 	struct spdk_poller		*remove_poller;
80 	TAILQ_ENTRY(spdk_iscsi_lun)	tailq;
81 };
82 
83 struct spdk_iscsi_conn {
84 	int				id;
85 	int				is_valid;
86 	/*
87 	 * All fields below this point are reinitialized each time the
88 	 *  connection object is allocated.  Make sure to update the
89 	 *  SPDK_ISCSI_CONNECTION_MEMSET() macro if changing which fields
90 	 *  are initialized when allocated.
91 	 */
92 	struct spdk_iscsi_portal	*portal;
93 	int				pg_tag;
94 	char				portal_host[MAX_PORTAL_ADDR + 1];
95 	char				portal_port[MAX_PORTAL_ADDR + 1];
96 	struct spdk_iscsi_poll_group	*pg;
97 	struct spdk_sock		*sock;
98 	struct spdk_iscsi_sess		*sess;
99 
100 	enum iscsi_connection_state	state;
101 	int				login_phase;
102 	bool				is_logged_out;
103 	struct spdk_iscsi_pdu		*login_rsp_pdu;
104 
105 	uint64_t	last_flush;
106 	uint64_t	last_fill;
107 	uint64_t	last_nopin;
108 
109 	/* Timer used to destroy connection after requesting logout if
110 	 *  initiator does not send logout request.
111 	 */
112 	struct spdk_poller *logout_request_timer;
113 
114 	/* Timer used to destroy connection after logout if initiator does
115 	 *  not close the connection.
116 	 */
117 	struct spdk_poller *logout_timer;
118 
119 	/* Timer used to wait for connection to close
120 	 */
121 	struct spdk_poller *shutdown_timer;
122 
123 	/* Timer used to destroy connection after creating this connection
124 	 *  if login process does not complete.
125 	 */
126 	struct spdk_poller *login_timer;
127 
128 	struct spdk_iscsi_pdu *pdu_in_progress;
129 	enum iscsi_pdu_recv_state pdu_recv_state;
130 
131 	TAILQ_HEAD(, spdk_iscsi_pdu) write_pdu_list;
132 	TAILQ_HEAD(, spdk_iscsi_pdu) snack_pdu_list;
133 
134 	uint32_t pending_r2t;
135 
136 	uint16_t cid;
137 
138 	/* IP address */
139 	char initiator_addr[MAX_INITIATOR_ADDR];
140 	char target_addr[MAX_TARGET_ADDR];
141 
142 	/* Initiator/Target port binds */
143 	char				initiator_name[MAX_INITIATOR_NAME];
144 	struct spdk_scsi_port		*initiator_port;
145 	char				target_short_name[MAX_TARGET_NAME];
146 	struct spdk_scsi_port		*target_port;
147 	struct spdk_iscsi_tgt_node	*target;
148 	struct spdk_scsi_dev		*dev;
149 
150 	/* To handle the case that SendTargets response is split into
151 	 * multiple PDUs due to very small MaxRecvDataSegmentLength.
152 	 */
153 	uint32_t			send_tgt_completed_size;
154 	struct iscsi_param		*params_text;
155 
156 	/* for fast access */
157 	int header_digest;
158 	int data_digest;
159 	int full_feature;
160 
161 	struct iscsi_param *params;
162 	bool sess_param_state_negotiated[MAX_SESSION_PARAMS];
163 	bool conn_param_state_negotiated[MAX_CONNECTION_PARAMS];
164 	struct iscsi_chap_auth auth;
165 	bool authenticated;
166 	bool disable_chap;
167 	bool require_chap;
168 	bool mutual_chap;
169 	int32_t chap_group;
170 	uint32_t pending_task_cnt;
171 	uint32_t data_out_cnt;
172 	uint32_t data_in_cnt;
173 
174 	uint64_t timeout;
175 	uint64_t nopininterval;
176 	bool nop_outstanding;
177 
178 	/*
179 	 * This is the maximum data segment length that iscsi target can send
180 	 *  to the initiator on this connection.  Not to be confused with the
181 	 *  maximum data segment length that initiators can send to iscsi target, which
182 	 *  is statically defined as SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH.
183 	 */
184 	int MaxRecvDataSegmentLength;
185 
186 	uint32_t StatSN;
187 	uint32_t exp_statsn;
188 	uint32_t ttt; /* target transfer tag */
189 	char *partial_text_parameter;
190 
191 	STAILQ_ENTRY(spdk_iscsi_conn) pg_link;
192 	bool			is_stopped;  /* Set true when connection is stopped for migration */
193 	TAILQ_HEAD(queued_r2t_tasks, spdk_iscsi_task)	queued_r2t_tasks;
194 	TAILQ_HEAD(active_r2t_tasks, spdk_iscsi_task)	active_r2t_tasks;
195 	TAILQ_HEAD(queued_datain_tasks, spdk_iscsi_task)	queued_datain_tasks;
196 
197 	TAILQ_HEAD(, spdk_iscsi_lun)	luns;
198 
199 	TAILQ_ENTRY(spdk_iscsi_conn)	conn_link;
200 };
201 
202 void iscsi_task_cpl(struct spdk_scsi_task *scsi_task);
203 void iscsi_task_mgmt_cpl(struct spdk_scsi_task *scsi_task);
204 
205 int initialize_iscsi_conns(void);
206 void shutdown_iscsi_conns(void);
207 void iscsi_conns_request_logout(struct spdk_iscsi_tgt_node *target, int pg_tag);
208 int iscsi_get_active_conns(struct spdk_iscsi_tgt_node *target);
209 
210 int iscsi_conn_construct(struct spdk_iscsi_portal *portal, struct spdk_sock *sock);
211 void iscsi_conn_destruct(struct spdk_iscsi_conn *conn);
212 void iscsi_conn_handle_nop(struct spdk_iscsi_conn *conn);
213 void iscsi_conn_schedule(struct spdk_iscsi_conn *conn);
214 void iscsi_conn_logout(struct spdk_iscsi_conn *conn);
215 int iscsi_drop_conns(struct spdk_iscsi_conn *conn,
216 		     const char *conn_match, int drop_all);
217 int iscsi_conn_handle_queued_datain_tasks(struct spdk_iscsi_conn *conn);
218 int iscsi_conn_abort_queued_datain_task(struct spdk_iscsi_conn *conn,
219 					uint32_t ref_task_tag);
220 int iscsi_conn_abort_queued_datain_tasks(struct spdk_iscsi_conn *conn,
221 		struct spdk_scsi_lun *lun,
222 		struct spdk_iscsi_pdu *pdu);
223 
224 int iscsi_conn_read_data(struct spdk_iscsi_conn *conn, int len, void *buf);
225 int iscsi_conn_readv_data(struct spdk_iscsi_conn *conn,
226 			  struct iovec *iov, int iovcnt);
227 void iscsi_conn_write_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu,
228 			  iscsi_conn_xfer_complete_cb cb_fn,
229 			  void *cb_arg);
230 
231 void iscsi_conn_free_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu);
232 
233 void iscsi_conn_info_json(struct spdk_json_write_ctx *w, struct spdk_iscsi_conn *conn);
234 void iscsi_conn_pdu_generic_complete(void *cb_arg);
235 #endif /* SPDK_ISCSI_CONN_H */
236