]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/lib/iscsi/iscsi.h
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / spdk / lib / iscsi / iscsi.h
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_H
36 #define SPDK_ISCSI_H
37
38 #include "spdk/stdinc.h"
39
40 #include "spdk/bdev.h"
41 #include "spdk/iscsi_spec.h"
42 #include "spdk/event.h"
43 #include "spdk/thread.h"
44
45 #include "iscsi/param.h"
46 #include "iscsi/tgt_node.h"
47
48 #include "spdk/assert.h"
49 #include "spdk/util.h"
50
51 #define SPDK_ISCSI_DEFAULT_NODEBASE "iqn.2016-06.io.spdk"
52
53 #define DEFAULT_MAXR2T 4
54 #define MAX_INITIATOR_NAME 256
55 #define MAX_TARGET_NAME 256
56
57 #define MAX_PORTAL 1024
58 #define MAX_INITIATOR 256
59 #define MAX_NETMASK 256
60 #define MAX_SESSIONS 1024
61 #define MAX_ISCSI_CONNECTIONS MAX_SESSIONS
62 #define MAX_FIRSTBURSTLENGTH 16777215
63
64 #define DEFAULT_PORT 3260
65 #define DEFAULT_MAX_SESSIONS 128
66 #define DEFAULT_MAX_CONNECTIONS_PER_SESSION 2
67 #define DEFAULT_MAXOUTSTANDINGR2T 1
68 #define DEFAULT_DEFAULTTIME2WAIT 2
69 #define DEFAULT_DEFAULTTIME2RETAIN 20
70 #define DEFAULT_FIRSTBURSTLENGTH 8192
71 #define DEFAULT_INITIALR2T true
72 #define DEFAULT_IMMEDIATEDATA true
73 #define DEFAULT_DATAPDUINORDER true
74 #define DEFAULT_DATASEQUENCEINORDER true
75 #define DEFAULT_ERRORRECOVERYLEVEL 0
76 #define DEFAULT_TIMEOUT 60
77 #define MAX_NOPININTERVAL 60
78 #define DEFAULT_NOPININTERVAL 30
79 #define DEFAULT_CONNECTIONS_PER_LCORE 4
80
81 /*
82 * SPDK iSCSI target currently only supports 64KB as the maximum data segment length
83 * it can receive from initiators. Other values may work, but no guarantees.
84 */
85 #define SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH 65536
86
87 /*
88 * SPDK iSCSI target will only send a maximum of SPDK_BDEV_LARGE_BUF_MAX_SIZE data segments, even if the
89 * connection can support more.
90 */
91 #define SPDK_ISCSI_MAX_SEND_DATA_SEGMENT_LENGTH SPDK_BDEV_LARGE_BUF_MAX_SIZE
92
93 /*
94 * Defines maximum number of data out buffers each connection can have in
95 * use at any given time.
96 */
97 #define MAX_DATA_OUT_PER_CONNECTION 16
98
99 /*
100 * Defines maximum number of data in buffers each connection can have in
101 * use at any given time. So this limit does not affect I/O smaller than
102 * SPDK_BDEV_SMALL_BUF_MAX_SIZE.
103 */
104 #define MAX_LARGE_DATAIN_PER_CONNECTION 64
105
106 /*
107 * Defines default maximum queue depth per connection and this can be
108 * changed by configuration file.
109 */
110 #define DEFAULT_MAX_QUEUE_DEPTH 64
111
112 #define SPDK_ISCSI_MAX_BURST_LENGTH \
113 (SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH * MAX_DATA_OUT_PER_CONNECTION)
114
115 /*
116 * Defines default maximum amount in bytes of unsolicited data the iSCSI
117 * initiator may send to the SPDK iSCSI target during the execution of
118 * a single SCSI command. And it is smaller than the MaxBurstLength.
119 */
120 #define SPDK_ISCSI_FIRST_BURST_LENGTH 8192
121
122 /*
123 * Defines minimum amount in bytes of unsolicited data the iSCSI initiator
124 * may send to the SPDK iSCSI target during the execution of a single
125 * SCSI command.
126 */
127 #define SPDK_ISCSI_MIN_FIRST_BURST_LENGTH 512
128
129 /** Defines how long we should wait for a TCP close after responding to a
130 * logout request, before terminating the connection ourselves.
131 */
132 #define ISCSI_LOGOUT_TIMEOUT 5 /* in seconds */
133
134 /* according to RFC1982 */
135 #define SN32_CMPMAX (((uint32_t)1U) << (32 - 1))
136 #define SN32_LT(S1,S2) \
137 (((uint32_t)(S1) != (uint32_t)(S2)) \
138 && (((uint32_t)(S1) < (uint32_t)(S2) \
139 && ((uint32_t)(S2) - (uint32_t)(S1) < SN32_CMPMAX)) \
140 || ((uint32_t)(S1) > (uint32_t)(S2) \
141 && ((uint32_t)(S1) - (uint32_t)(S2) > SN32_CMPMAX))))
142 #define SN32_GT(S1,S2) \
143 (((uint32_t)(S1) != (uint32_t)(S2)) \
144 && (((uint32_t)(S1) < (uint32_t)(S2) \
145 && ((uint32_t)(S2) - (uint32_t)(S1) > SN32_CMPMAX)) \
146 || ((uint32_t)(S1) > (uint32_t)(S2) \
147 && ((uint32_t)(S1) - (uint32_t)(S2) < SN32_CMPMAX))))
148
149 /* For spdk_iscsi_login_in related function use, we need to avoid the conflict
150 * with other errors
151 * */
152 #define SPDK_ISCSI_LOGIN_ERROR_RESPONSE -1000
153 #define SPDK_ISCSI_LOGIN_ERROR_PARAMETER -1001
154 #define SPDK_ISCSI_PARAMETER_EXCHANGE_NOT_ONCE -1002
155
156 #define ISCSI_AHS_LEN 60
157
158 struct spdk_mobj {
159 struct spdk_mempool *mp;
160 void *buf;
161 size_t len;
162 uint64_t reserved; /* do not use */
163 };
164
165 struct spdk_iscsi_pdu {
166 struct iscsi_bhs bhs;
167 struct spdk_mobj *mobj;
168 uint8_t *data_buf;
169 uint8_t *data;
170 uint8_t header_digest[ISCSI_DIGEST_LEN];
171 uint8_t data_digest[ISCSI_DIGEST_LEN];
172 size_t data_segment_len;
173 int bhs_valid_bytes;
174 int ahs_valid_bytes;
175 int data_valid_bytes;
176 int hdigest_valid_bytes;
177 int ddigest_valid_bytes;
178 int ref;
179 bool data_from_mempool; /* indicate whether the data buffer is allocated from mempool */
180 struct spdk_iscsi_task *task; /* data tied to a task buffer */
181 uint32_t cmd_sn;
182 uint32_t writev_offset;
183 TAILQ_ENTRY(spdk_iscsi_pdu) tailq;
184
185
186 /*
187 * 60 bytes of AHS should suffice for now.
188 * This should always be at the end of PDU data structure.
189 * we need to not zero this out when doing memory clear.
190 */
191 uint8_t ahs[ISCSI_AHS_LEN];
192
193 struct {
194 uint16_t length; /* iSCSI SenseLength (big-endian) */
195 uint8_t data[32];
196 } sense;
197 };
198
199 enum iscsi_connection_state {
200 ISCSI_CONN_STATE_INVALID = 0,
201 ISCSI_CONN_STATE_RUNNING = 1,
202 ISCSI_CONN_STATE_LOGGED_OUT = 2,
203 ISCSI_CONN_STATE_EXITING = 3,
204 ISCSI_CONN_STATE_EXITED = 4,
205 };
206
207 enum iscsi_chap_phase {
208 ISCSI_CHAP_PHASE_NONE = 0,
209 ISCSI_CHAP_PHASE_WAIT_A = 1,
210 ISCSI_CHAP_PHASE_WAIT_NR = 2,
211 ISCSI_CHAP_PHASE_END = 3,
212 };
213
214 enum session_type {
215 SESSION_TYPE_INVALID = 0,
216 SESSION_TYPE_NORMAL = 1,
217 SESSION_TYPE_DISCOVERY = 2,
218 };
219
220 #define ISCSI_CHAP_CHALLENGE_LEN 1024
221 #define ISCSI_CHAP_MAX_USER_LEN 255
222 #define ISCSI_CHAP_MAX_SECRET_LEN 255
223
224 struct iscsi_chap_auth {
225 enum iscsi_chap_phase chap_phase;
226
227 char user[ISCSI_CHAP_MAX_USER_LEN + 1];
228 char secret[ISCSI_CHAP_MAX_SECRET_LEN + 1];
229 char muser[ISCSI_CHAP_MAX_USER_LEN + 1];
230 char msecret[ISCSI_CHAP_MAX_SECRET_LEN + 1];
231
232 uint8_t chap_id[1];
233 uint8_t chap_mid[1];
234 int chap_challenge_len;
235 uint8_t chap_challenge[ISCSI_CHAP_CHALLENGE_LEN];
236 int chap_mchallenge_len;
237 uint8_t chap_mchallenge[ISCSI_CHAP_CHALLENGE_LEN];
238 };
239
240 struct spdk_iscsi_auth_secret {
241 char user[ISCSI_CHAP_MAX_USER_LEN + 1];
242 char secret[ISCSI_CHAP_MAX_SECRET_LEN + 1];
243 char muser[ISCSI_CHAP_MAX_USER_LEN + 1];
244 char msecret[ISCSI_CHAP_MAX_SECRET_LEN + 1];
245 TAILQ_ENTRY(spdk_iscsi_auth_secret) tailq;
246 };
247
248 struct spdk_iscsi_auth_group {
249 int32_t tag;
250 TAILQ_HEAD(, spdk_iscsi_auth_secret) secret_head;
251 TAILQ_ENTRY(spdk_iscsi_auth_group) tailq;
252 };
253
254 struct spdk_iscsi_sess {
255 uint32_t connections;
256 struct spdk_iscsi_conn **conns;
257
258 struct spdk_scsi_port *initiator_port;
259 int tag;
260
261 uint64_t isid;
262 uint16_t tsih;
263 struct spdk_iscsi_tgt_node *target;
264 int queue_depth;
265
266 struct iscsi_param *params;
267
268 enum session_type session_type;
269 uint32_t MaxConnections;
270 uint32_t MaxOutstandingR2T;
271 uint32_t DefaultTime2Wait;
272 uint32_t DefaultTime2Retain;
273 uint32_t FirstBurstLength;
274 uint32_t MaxBurstLength;
275 bool InitialR2T;
276 bool ImmediateData;
277 bool DataPDUInOrder;
278 bool DataSequenceInOrder;
279 uint32_t ErrorRecoveryLevel;
280
281 uint32_t ExpCmdSN;
282 uint32_t MaxCmdSN;
283
284 uint32_t current_text_itt;
285 };
286
287 struct spdk_iscsi_poll_group {
288 uint32_t core;
289 struct spdk_poller *poller;
290 struct spdk_poller *nop_poller;
291 STAILQ_HEAD(connections, spdk_iscsi_conn) connections;
292 struct spdk_sock_group *sock_group;
293 };
294
295 struct spdk_iscsi_opts {
296 char *authfile;
297 char *nodebase;
298 int32_t timeout;
299 int32_t nopininterval;
300 bool disable_chap;
301 bool require_chap;
302 bool mutual_chap;
303 int32_t chap_group;
304 uint32_t MaxSessions;
305 uint32_t MaxConnectionsPerSession;
306 uint32_t MaxConnections;
307 uint32_t MaxQueueDepth;
308 uint32_t DefaultTime2Wait;
309 uint32_t DefaultTime2Retain;
310 uint32_t FirstBurstLength;
311 bool ImmediateData;
312 uint32_t ErrorRecoveryLevel;
313 bool AllowDuplicateIsid;
314 uint32_t min_connections_per_core;
315 };
316
317 struct spdk_iscsi_globals {
318 char *authfile;
319 char *nodebase;
320 pthread_mutex_t mutex;
321 TAILQ_HEAD(, spdk_iscsi_portal) portal_head;
322 TAILQ_HEAD(, spdk_iscsi_portal_grp) pg_head;
323 TAILQ_HEAD(, spdk_iscsi_init_grp) ig_head;
324 TAILQ_HEAD(, spdk_iscsi_tgt_node) target_head;
325 TAILQ_HEAD(, spdk_iscsi_auth_group) auth_group_head;
326
327 int32_t timeout;
328 int32_t nopininterval;
329 bool disable_chap;
330 bool require_chap;
331 bool mutual_chap;
332 int32_t chap_group;
333
334 uint32_t MaxSessions;
335 uint32_t MaxConnectionsPerSession;
336 uint32_t MaxConnections;
337 uint32_t MaxQueueDepth;
338 uint32_t DefaultTime2Wait;
339 uint32_t DefaultTime2Retain;
340 uint32_t FirstBurstLength;
341 bool ImmediateData;
342 uint32_t ErrorRecoveryLevel;
343 bool AllowDuplicateIsid;
344
345 struct spdk_mempool *pdu_pool;
346 struct spdk_mempool *pdu_immediate_data_pool;
347 struct spdk_mempool *pdu_data_out_pool;
348 struct spdk_mempool *session_pool;
349 struct spdk_mempool *task_pool;
350
351 struct spdk_iscsi_sess **session;
352 struct spdk_iscsi_poll_group *poll_group;
353 };
354
355 #define ISCSI_SECURITY_NEGOTIATION_PHASE 0
356 #define ISCSI_OPERATIONAL_NEGOTIATION_PHASE 1
357 #define ISCSI_NSG_RESERVED_CODE 2
358 #define ISCSI_FULL_FEATURE_PHASE 3
359
360 enum spdk_error_codes {
361 SPDK_SUCCESS = 0,
362 SPDK_ISCSI_CONNECTION_FATAL = -1,
363 SPDK_PDU_FATAL = -2,
364 };
365
366 #define DGET24(B) \
367 ((( (uint32_t) *((uint8_t *)(B)+0)) << 16) \
368 | (((uint32_t) *((uint8_t *)(B)+1)) << 8) \
369 | (((uint32_t) *((uint8_t *)(B)+2)) << 0))
370
371 #define DSET24(B,D) \
372 (((*((uint8_t *)(B)+0)) = (uint8_t)((uint32_t)(D) >> 16)), \
373 ((*((uint8_t *)(B)+1)) = (uint8_t)((uint32_t)(D) >> 8)), \
374 ((*((uint8_t *)(B)+2)) = (uint8_t)((uint32_t)(D) >> 0)))
375
376 #define xstrdup(s) (s ? strdup(s) : (char *)NULL)
377
378 extern struct spdk_iscsi_globals g_spdk_iscsi;
379 extern struct spdk_iscsi_opts *g_spdk_iscsi_opts;
380
381 struct spdk_iscsi_task;
382 struct spdk_json_write_ctx;
383
384 typedef void (*spdk_iscsi_init_cb)(void *cb_arg, int rc);
385
386 void spdk_iscsi_init(spdk_iscsi_init_cb cb_fn, void *cb_arg);
387 typedef void (*spdk_iscsi_fini_cb)(void *arg);
388 void spdk_iscsi_fini(spdk_iscsi_fini_cb cb_fn, void *cb_arg);
389 void spdk_shutdown_iscsi_conns_done(void);
390 void spdk_iscsi_config_text(FILE *fp);
391 void spdk_iscsi_config_json(struct spdk_json_write_ctx *w);
392
393 struct spdk_iscsi_opts *spdk_iscsi_opts_alloc(void);
394 void spdk_iscsi_opts_free(struct spdk_iscsi_opts *opts);
395 struct spdk_iscsi_opts *spdk_iscsi_opts_copy(struct spdk_iscsi_opts *src);
396 void spdk_iscsi_opts_info_json(struct spdk_json_write_ctx *w);
397 int spdk_iscsi_set_discovery_auth(bool disable_chap, bool require_chap,
398 bool mutual_chap, int32_t chap_group);
399 int spdk_iscsi_chap_get_authinfo(struct iscsi_chap_auth *auth, const char *authuser,
400 int ag_tag);
401 int spdk_iscsi_add_auth_group(int32_t tag, struct spdk_iscsi_auth_group **_group);
402 struct spdk_iscsi_auth_group *spdk_iscsi_find_auth_group_by_tag(int32_t tag);
403 void spdk_iscsi_delete_auth_group(struct spdk_iscsi_auth_group *group);
404 int spdk_iscsi_auth_group_add_secret(struct spdk_iscsi_auth_group *group,
405 const char *user, const char *secret,
406 const char *muser, const char *msecret);
407 int spdk_iscsi_auth_group_delete_secret(struct spdk_iscsi_auth_group *group,
408 const char *user);
409 void spdk_iscsi_auth_groups_info_json(struct spdk_json_write_ctx *w);
410
411 void spdk_iscsi_send_nopin(struct spdk_iscsi_conn *conn);
412 void spdk_iscsi_task_response(struct spdk_iscsi_conn *conn,
413 struct spdk_iscsi_task *task);
414 int spdk_iscsi_execute(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu);
415 int spdk_iscsi_build_iovecs(struct spdk_iscsi_conn *conn,
416 struct iovec *iovec, struct spdk_iscsi_pdu *pdu);
417 int
418 spdk_iscsi_read_pdu(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu **_pdu);
419 void spdk_iscsi_task_mgmt_response(struct spdk_iscsi_conn *conn,
420 struct spdk_iscsi_task *task);
421
422 int spdk_iscsi_conn_params_init(struct iscsi_param **params);
423 int spdk_iscsi_sess_params_init(struct iscsi_param **params);
424
425 void spdk_free_sess(struct spdk_iscsi_sess *sess);
426 void spdk_clear_all_transfer_task(struct spdk_iscsi_conn *conn,
427 struct spdk_scsi_lun *lun);
428 void spdk_del_transfer_task(struct spdk_iscsi_conn *conn, uint32_t CmdSN);
429 bool spdk_iscsi_is_deferred_free_pdu(struct spdk_iscsi_pdu *pdu);
430
431 int spdk_iscsi_negotiate_params(struct spdk_iscsi_conn *conn,
432 struct iscsi_param **params_p, uint8_t *data,
433 int alloc_len, int data_len);
434 int spdk_iscsi_copy_param2var(struct spdk_iscsi_conn *conn);
435
436 void spdk_iscsi_task_cpl(struct spdk_scsi_task *scsi_task);
437 void spdk_iscsi_task_mgmt_cpl(struct spdk_scsi_task *scsi_task);
438
439 /* Memory management */
440 void spdk_put_pdu(struct spdk_iscsi_pdu *pdu);
441 struct spdk_iscsi_pdu *spdk_get_pdu(void);
442 int spdk_iscsi_conn_handle_queued_datain_tasks(struct spdk_iscsi_conn *conn);
443
444 static inline int
445 spdk_get_immediate_data_buffer_size(void)
446 {
447 /*
448 * Specify enough extra space in addition to FirstBurstLength to
449 * account for a header digest, data digest and additional header
450 * segments (AHS). These are not normally used but they do not
451 * take up much space and we need to make sure the worst-case scenario
452 * can be satisified by the size returned here.
453 */
454 return g_spdk_iscsi.FirstBurstLength +
455 ISCSI_DIGEST_LEN + /* data digest */
456 ISCSI_DIGEST_LEN + /* header digest */
457 8 + /* bidirectional AHS */
458 52; /* extended CDB AHS (for a 64-byte CDB) */
459 }
460
461 static inline int
462 spdk_get_data_out_buffer_size(void)
463 {
464 return SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
465 }
466
467 #endif /* SPDK_ISCSI_H */