Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (C) 2019 Intel Corporation.
3 : : * All rights reserved.
4 : : */
5 : :
6 : : #include "spdk/stdinc.h"
7 : : #include "spdk/event.h"
8 : : #include "spdk/util.h"
9 : : #include "spdk/string.h"
10 : : #include "spdk/likely.h"
11 : : #include "spdk/json.h"
12 : : #include "spdk/endian.h"
13 : : #include "spdk/bdev.h"
14 : : #include "spdk/notify.h"
15 : : #include "spdk/scsi.h"
16 : : #include "spdk_internal/mock.h"
17 : : #include "spdk/scsi_spec.h"
18 : : #include "iscsi/conn.h"
19 : : #include "iscsi/iscsi.c"
20 : : #include "scsi/scsi_internal.h"
21 : : #include "spdk/sock.h"
22 : :
23 : : #define GET_PDU_LOOP_COUNT 16
24 : : #define DEFAULT_RUNTIME 30 /* seconds */
25 : : #define MAX_RUNTIME_S 86400 /* 24 hours */
26 : :
27 : : /* Global run state */
28 : : uint64_t g_runtime_ticks;
29 : : int g_runtime;
30 : : int g_num_active_threads;
31 : : bool g_run = true;
32 : : bool g_is_valid_opcode = true;
33 : :
34 : 2 : SPDK_LOG_REGISTER_COMPONENT(iscsi)
35 : :
36 : : /* Global resources */
37 : : TAILQ_HEAD(, spdk_iscsi_pdu) g_get_pdu_list;
38 : : TAILQ_HEAD(, fuzz_iscsi_dev_ctx) g_dev_list = TAILQ_HEAD_INITIALIZER(g_dev_list);
39 : : struct spdk_poller *g_app_completion_poller;
40 : : void *g_valid_buffer;
41 : : unsigned int g_random_seed;
42 : : char *g_tgt_ip = "127.0.0.1";
43 : : char *g_tgt_port = "3260";
44 : : /* TBD: Discovery login to get target information. We use fixed IQN for target for now. */
45 : : char *g_tgt_name = "iqn.2016-06.io.spdk:disk1";
46 : : char *g_init_name = "iqn.2016-06.io.spdk:fuzzinit";
47 : :
48 : : struct fuzz_iscsi_iov_ctx {
49 : : struct iovec iov_req;
50 : : struct iovec iov_data;
51 : : struct iovec iov_resp;
52 : : };
53 : :
54 : : struct fuzz_iscsi_io_ctx {
55 : : struct fuzz_iscsi_iov_ctx iov_ctx;
56 : : union {
57 : : struct iscsi_bhs *bhs;
58 : : struct iscsi_bhs_nop_out *nop_out_req;
59 : : struct iscsi_bhs_scsi_req *scsi_req;
60 : : struct iscsi_bhs_task_req *task_req;
61 : : struct iscsi_bhs_login_req *login_req;
62 : : struct iscsi_bhs_text_req *text_req;
63 : : struct iscsi_bhs_data_out *data_out_req;
64 : : struct iscsi_bhs_logout_req *logout_req;
65 : : struct iscsi_bhs_snack_req *snack_req;
66 : : } req;
67 : : };
68 : :
69 : : struct fuzz_iscsi_dev_ctx {
70 : : struct spdk_iscsi_sess sess;
71 : : struct spdk_iscsi_conn *conn;
72 : : struct fuzz_iscsi_io_ctx io_ctx;
73 : :
74 : : struct spdk_thread *thread;
75 : : struct spdk_poller *poller;
76 : : unsigned int random_seed, current_cmd_sn;
77 : : uint64_t num_sent_pdus;
78 : : uint64_t num_valid_pdus;
79 : :
80 : : TAILQ_ENTRY(fuzz_iscsi_dev_ctx) link;
81 : : };
82 : :
83 : : static void
84 : 229212 : fuzz_fill_random_bytes(char *character_repr, size_t len, unsigned int *rand_seed)
85 : : {
86 : : size_t i;
87 : :
88 [ + + ]: 11231388 : for (i = 0; i < len; i++) {
89 : 11002176 : character_repr[i] = rand_r(rand_seed) % UINT8_MAX;
90 : : }
91 : 229212 : }
92 : :
93 : : static char *
94 : 229212 : fuzz_get_value_base_64_buffer(void *item, size_t len)
95 : : {
96 : : char *value_string;
97 : : size_t total_size;
98 : : int rc;
99 : :
100 : : /* Null pointer */
101 : 229212 : total_size = spdk_base64_get_encoded_strlen(len) + 1;
102 : :
103 : 229212 : value_string = calloc(1, total_size);
104 [ - + ]: 229212 : if (value_string == NULL) {
105 : 0 : return NULL;
106 : : }
107 : :
108 : 229212 : rc = spdk_base64_encode(value_string, item, len);
109 [ - + ]: 229212 : if (rc < 0) {
110 : 0 : free(value_string);
111 : 0 : return NULL;
112 : : }
113 : :
114 : 229212 : return value_string;
115 : : }
116 : :
117 : : int
118 : 0 : iscsi_chap_get_authinfo(struct iscsi_chap_auth *auth, const char *authuser,
119 : : int ag_tag)
120 : : {
121 : 0 : return 0;
122 : : }
123 : :
124 : : void
125 : 0 : shutdown_iscsi_conns_done(void)
126 : : {
127 : 0 : return;
128 : : }
129 : :
130 : : void
131 : 458431 : iscsi_put_pdu(struct spdk_iscsi_pdu *pdu)
132 : : {
133 [ - + ]: 458431 : if (!pdu) {
134 : 0 : return;
135 : : }
136 : :
137 : 458431 : pdu->ref--;
138 [ - + ]: 458431 : if (pdu->ref < 0) {
139 : 0 : pdu->ref = 0;
140 : : }
141 : :
142 [ + - ]: 458431 : if (pdu->ref == 0) {
143 [ + + ]: 458431 : if (pdu->data) {
144 : 218141 : free(pdu->data);
145 : : }
146 : 458431 : free(pdu);
147 : : }
148 : : }
149 : :
150 : : struct spdk_iscsi_pdu *
151 : 458431 : iscsi_get_pdu(struct spdk_iscsi_conn *conn)
152 : : {
153 : : struct spdk_iscsi_pdu *pdu;
154 : :
155 : 458431 : pdu = calloc(1, sizeof(*pdu));
156 [ - + ]: 458431 : if (!pdu) {
157 : 0 : return NULL;
158 : : }
159 : :
160 : 458431 : pdu->ref = 1;
161 : 458431 : pdu->conn = conn;
162 : :
163 : 458431 : return pdu;
164 : : }
165 : :
166 : : static void
167 : 0 : iscsi_task_free(struct spdk_scsi_task *scsi_task)
168 : : {
169 : 0 : struct spdk_iscsi_task *task = iscsi_task_from_scsi_task(scsi_task);
170 : :
171 [ # # ]: 0 : assert(task->parent == NULL);
172 : :
173 : 0 : iscsi_task_disassociate_pdu(task);
174 [ # # ]: 0 : assert(task->conn->pending_task_cnt > 0);
175 : 0 : task->conn->pending_task_cnt--;
176 : 0 : free(task);
177 : 0 : }
178 : :
179 : : struct spdk_iscsi_task *
180 : 0 : iscsi_task_get(struct spdk_iscsi_conn *conn, struct spdk_iscsi_task *parent,
181 : : spdk_scsi_task_cpl cpl_fn)
182 : : {
183 : : struct spdk_iscsi_task *task;
184 : :
185 : : /* iSCSI subtask is not necessary for now. */
186 [ # # ]: 0 : assert(parent == NULL);
187 : :
188 : 0 : task = calloc(1, sizeof(*task));
189 [ # # ]: 0 : if (!task) {
190 [ # # ]: 0 : printf("Unable to get task\n");
191 : 0 : abort();
192 : : }
193 : :
194 : 0 : task->conn = conn;
195 [ # # ]: 0 : assert(conn->pending_task_cnt < UINT32_MAX);
196 : 0 : conn->pending_task_cnt++;
197 : 0 : spdk_scsi_task_construct(&task->scsi, cpl_fn, iscsi_task_free);
198 : :
199 : 0 : return task;
200 : : }
201 : :
202 : : static void
203 : 2 : cleanup(void)
204 : : {
205 : : struct fuzz_iscsi_dev_ctx *dev_ctx, *tmp;
206 : :
207 [ + + ]: 4 : TAILQ_FOREACH_SAFE(dev_ctx, &g_dev_list, link, tmp) {
208 [ - + ]: 2 : printf("device %p stats: Sent %" PRIu64 " valid opcode PDUs, %" PRIu64 " invalid opcode PDUs.\n",
209 : : dev_ctx, dev_ctx->num_valid_pdus,
210 : 2 : dev_ctx->num_sent_pdus - dev_ctx->num_valid_pdus);
211 : 2 : free(dev_ctx);
212 : : }
213 : :
214 : 2 : spdk_free(g_valid_buffer);
215 : 2 : }
216 : :
217 : : /* data dumping functions begin */
218 : : static int
219 : 229212 : dump_iscsi_cmd(void *ctx, const void *data, size_t size)
220 : : {
221 [ - + - + ]: 229212 : fprintf(stderr, "%s\n", (const char *)data);
222 : 229212 : return 0;
223 : : }
224 : :
225 : : static void
226 : 229212 : print_scsi_io_data(struct spdk_json_write_ctx *w, struct fuzz_iscsi_io_ctx *io_ctx)
227 : : {
228 : : char *data_segment_len;
229 : :
230 : 229212 : data_segment_len = fuzz_get_value_base_64_buffer((void *)io_ctx->req.bhs->data_segment_len,
231 : : sizeof(io_ctx->req.bhs->data_segment_len));
232 : :
233 : 229212 : spdk_json_write_named_uint32(w, "opcode", io_ctx->req.bhs->opcode);
234 : 229212 : spdk_json_write_named_uint32(w, "immediate", io_ctx->req.bhs->immediate);
235 : 229212 : spdk_json_write_named_uint32(w, "reserved", io_ctx->req.bhs->reserved);
236 : 229212 : spdk_json_write_named_uint32(w, "total_ahs_len", io_ctx->req.bhs->total_ahs_len);
237 : 229212 : spdk_json_write_named_string(w, "data_segment_len", data_segment_len);
238 : 229212 : spdk_json_write_named_uint32(w, "itt", io_ctx->req.bhs->itt);
239 : 229212 : spdk_json_write_named_uint32(w, "exp_stat_sn", io_ctx->req.bhs->exp_stat_sn);
240 : :
241 : 229212 : free(data_segment_len);
242 : 229212 : }
243 : :
244 : : static void
245 : 229212 : print_req_obj(struct fuzz_iscsi_dev_ctx *dev_ctx, struct fuzz_iscsi_io_ctx *io_ctx)
246 : : {
247 : : struct spdk_json_write_ctx *w;
248 : :
249 : 229212 : w = spdk_json_write_begin(dump_iscsi_cmd, NULL, SPDK_JSON_WRITE_FLAG_FORMATTED);
250 : 229212 : spdk_json_write_named_object_begin(w, "bhs");
251 : 229212 : print_scsi_io_data(w, io_ctx);
252 : 229212 : spdk_json_write_object_end(w);
253 : 229212 : spdk_json_write_end(w);
254 : 229212 : }
255 : :
256 : : /* data dumping functions end */
257 : :
258 : : /* dev initialization begin */
259 : : static int
260 : 2 : fuzz_iscsi_dev_init(void)
261 : : {
262 : : struct fuzz_iscsi_dev_ctx *dev_ctx;
263 : 2 : int rc = 0;
264 : :
265 : 2 : dev_ctx = calloc(1, sizeof(*dev_ctx));
266 [ - + ]: 2 : if (dev_ctx == NULL) {
267 : 0 : return -ENOMEM;
268 : : }
269 : :
270 : 2 : dev_ctx->thread = spdk_get_thread();
271 [ - + ]: 2 : if (dev_ctx->thread == NULL) {
272 [ # # # # ]: 0 : fprintf(stderr, "Unable to get a thread for a fuzz device.\n");
273 : 0 : rc = -EINVAL;
274 : 0 : goto error_out;
275 : : }
276 : :
277 : 2 : dev_ctx->current_cmd_sn = 0;
278 : :
279 : 2 : TAILQ_INSERT_TAIL(&g_dev_list, dev_ctx, link);
280 : 2 : return 0;
281 : :
282 : 0 : error_out:
283 : 0 : free(dev_ctx);
284 : 0 : return rc;
285 : : }
286 : : /* dev initialization end */
287 : :
288 : : /* build requests begin */
289 : : static void
290 : 229212 : prep_iscsi_pdu_bhs_opcode_cmd(struct fuzz_iscsi_dev_ctx *dev_ctx, struct fuzz_iscsi_io_ctx *io_ctx)
291 : : {
292 : 229212 : io_ctx->iov_ctx.iov_req.iov_len = sizeof(struct iscsi_bhs);
293 : 229212 : fuzz_fill_random_bytes((char *)io_ctx->req.bhs, sizeof(struct iscsi_bhs),
294 : : &dev_ctx->random_seed);
295 : 229212 : }
296 : : /* build requests end */
297 : :
298 : : static int
299 : 2 : iscsi_pdu_hdr_op_login_rsp(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
300 : : {
301 : 2 : return 0;
302 : : }
303 : :
304 : : static int
305 : 229213 : iscsi_fuzz_pdu_hdr_handle(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
306 : : {
307 : : int opcode;
308 : 229213 : int rc = 0;
309 : :
310 : 229213 : opcode = pdu->bhs.opcode;
311 [ + + ]: 229213 : if (opcode == ISCSI_OP_LOGIN_RSP) {
312 : 2 : return iscsi_pdu_hdr_op_login_rsp(conn, pdu);
313 : : }
314 : :
315 [ - + + - ]: 229211 : switch (opcode) {
316 : 0 : case ISCSI_OP_LOGOUT_RSP:
317 [ # # # # ]: 0 : fprintf(stderr, "Received logout hdr_handle response opcode(0x26) from Target.\n");
318 : 0 : conn->is_logged_out = true;
319 : 0 : break;
320 : 14833 : case ISCSI_OP_NOPIN:
321 : : case ISCSI_OP_SCSI_RSP:
322 : : case ISCSI_OP_TASK_RSP:
323 : : case ISCSI_OP_TEXT_RSP:
324 : : case ISCSI_OP_SCSI_DATAIN:
325 : : case ISCSI_OP_R2T:
326 : : case ISCSI_OP_ASYNC:
327 : : case ISCSI_OP_VENDOR_3C:
328 : : case ISCSI_OP_VENDOR_3D:
329 : : case ISCSI_OP_VENDOR_3E:
330 [ - + - + ]: 14833 : fprintf(stderr, "Received hdr_handle response opcode from Target is 0x%x.\n", pdu->bhs.opcode);
331 : 14833 : break;
332 : 214378 : case ISCSI_OP_REJECT:
333 [ - + - + ]: 214378 : fprintf(stderr, "Received rejected hdr_handle response opcode(0x3f) from Target.\n");
334 : 214378 : break;
335 : 0 : default:
336 : 0 : rc = -1;
337 : 0 : break;
338 : : }
339 : :
340 : 229211 : return rc;
341 : : }
342 : :
343 : : static int
344 : 2 : iscsi_pdu_payload_op_login_rsp(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
345 : : {
346 : : struct iscsi_bhs_login_rsp *rsph;
347 : :
348 : 2 : rsph = (struct iscsi_bhs_login_rsp *)&pdu->bhs;
349 [ - + ]: 2 : if (rsph == NULL) {
350 : 0 : return -1;
351 : : }
352 : :
353 [ - + ]: 2 : assert(rsph->tsih != 0);
354 [ - + ]: 2 : assert(rsph->status_class == 0);
355 [ - + ]: 2 : assert(ISCSI_BHS_LOGIN_GET_TBIT(rsph->flags));
356 [ - + ]: 2 : assert(!(rsph->flags & ISCSI_LOGIN_CONTINUE));
357 [ - + ]: 2 : assert((rsph->flags & ISCSI_LOGIN_NEXT_STAGE_MASK) == ISCSI_LOGIN_NEXT_STAGE_3);
358 : :
359 : : /* We got the Login Final Response and move to Full-Feature Phase. */
360 : 2 : conn->full_feature = 1;
361 : 2 : return 0;
362 : : }
363 : :
364 : : static int
365 : 229213 : iscsi_fuzz_pdu_payload_handle(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
366 : : {
367 : : int opcode;
368 : 229213 : int rc = 0;
369 : :
370 : 229213 : opcode = pdu->bhs.opcode;
371 [ - + - + ]: 229213 : fprintf(stderr, "Received payload_handle response opcode from Target is 0x%x.\n", opcode);
372 : :
373 [ + + - ]: 229213 : switch (opcode) {
374 : 2 : case ISCSI_OP_LOGIN_RSP:
375 : 2 : rc = iscsi_pdu_payload_op_login_rsp(conn, pdu);
376 : 2 : break;
377 : 229211 : case ISCSI_OP_NOPIN:
378 : : case ISCSI_OP_SCSI_RSP:
379 : : case ISCSI_OP_TASK_RSP:
380 : : case ISCSI_OP_TEXT_RSP:
381 : : case ISCSI_OP_SCSI_DATAIN:
382 : : case ISCSI_OP_R2T:
383 : : case ISCSI_OP_ASYNC:
384 : : case ISCSI_OP_VENDOR_3C:
385 : : case ISCSI_OP_VENDOR_3D:
386 : : case ISCSI_OP_VENDOR_3E:
387 : : case ISCSI_OP_REJECT:
388 : 229211 : break;
389 : 0 : default:
390 : 0 : rc = -1;
391 : 0 : break;
392 : : }
393 : :
394 : 229213 : return rc;
395 : : }
396 : :
397 : : static int
398 : 466032 : iscsi_fuzz_read_pdu(struct spdk_iscsi_conn *conn)
399 : : {
400 : : enum iscsi_pdu_recv_state prev_state;
401 : : struct spdk_iscsi_pdu *pdu;
402 : : uint32_t data_len;
403 : : int rc;
404 : :
405 : : do {
406 : 924460 : prev_state = conn->pdu_recv_state;
407 : 924460 : pdu = conn->pdu_in_progress;
408 : :
409 [ + + + - : 924460 : switch (conn->pdu_recv_state) {
- ]
410 : 229215 : case ISCSI_PDU_RECV_STATE_AWAIT_PDU_READY:
411 [ - + ]: 229215 : assert(conn->pdu_in_progress == NULL);
412 : :
413 : 229215 : conn->pdu_in_progress = iscsi_get_pdu(conn);
414 [ - + ]: 229215 : if (conn->pdu_in_progress == NULL) {
415 : 0 : return SPDK_ISCSI_CONNECTION_FATAL;
416 : : }
417 : 229215 : TAILQ_INSERT_TAIL(&g_get_pdu_list, conn->pdu_in_progress, tailq);
418 : 229215 : conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_AWAIT_PDU_HDR;
419 : 229215 : break;
420 : 466032 : case ISCSI_PDU_RECV_STATE_AWAIT_PDU_HDR:
421 [ + - ]: 466032 : if (pdu->bhs_valid_bytes < ISCSI_BHS_LEN) {
422 : 1398096 : rc = iscsi_conn_read_data(conn,
423 : 466032 : ISCSI_BHS_LEN - pdu->bhs_valid_bytes,
424 : 466032 : (uint8_t *)&pdu->bhs + pdu->bhs_valid_bytes);
425 [ - + ]: 466032 : if (rc < 0) {
426 : 0 : conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
427 : 0 : break;
428 : : }
429 : 466032 : pdu->bhs_valid_bytes += rc;
430 [ + + ]: 466032 : if (pdu->bhs_valid_bytes < ISCSI_BHS_LEN) {
431 : 236819 : return 0;
432 : : }
433 : : }
434 : :
435 : 229213 : pdu->data_segment_len = ISCSI_ALIGN(DGET24(pdu->bhs.data_segment_len));
436 : :
437 : 229213 : rc = iscsi_fuzz_pdu_hdr_handle(conn, pdu);
438 [ - + ]: 229213 : if (rc < 0) {
439 [ # # ]: 0 : printf("Critical error is detected. Close the connection\n");
440 : 0 : conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
441 : 0 : break;
442 : : }
443 : :
444 [ - + - + ]: 229213 : if (conn->is_logged_out) {
445 [ # # ]: 0 : printf("pdu received after logout\n");
446 : 0 : conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
447 : 0 : break;
448 : : }
449 : :
450 : 229213 : conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD;
451 : 229213 : break;
452 : 229213 : case ISCSI_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD:
453 : 229213 : data_len = pdu->data_segment_len;
454 [ + + + - ]: 229213 : if (data_len != 0 && pdu->data == NULL) {
455 : 218139 : pdu->data = calloc(1, data_len);
456 [ - + ]: 218139 : if (pdu->data == NULL) {
457 : 0 : return 0;
458 : : }
459 : : }
460 : :
461 : : /* copy the actual data into local buffer */
462 [ + + ]: 229213 : if (pdu->data_valid_bytes < data_len) {
463 : 218139 : rc = iscsi_conn_read_data_segment(conn, pdu,
464 : : pdu->data_valid_bytes,
465 : 218139 : data_len - pdu->data_valid_bytes);
466 [ - + ]: 218139 : if (rc < 0) {
467 : 0 : conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
468 : 0 : break;
469 : : }
470 : 218139 : pdu->data_valid_bytes += rc;
471 [ - + ]: 218139 : if (pdu->data_valid_bytes < data_len) {
472 : 0 : return 0;
473 : : }
474 : : }
475 : :
476 : : /* All data for this PDU has now been read from the socket. */
477 [ + - - + ]: 229213 : spdk_trace_record(TRACE_ISCSI_READ_PDU, conn->id, pdu->data_valid_bytes,
478 : : (uintptr_t)pdu, pdu->bhs.opcode);
479 : :
480 [ - + + - ]: 229213 : if (!pdu->is_rejected) {
481 : 229213 : rc = iscsi_fuzz_pdu_payload_handle(conn, pdu);
482 : : } else {
483 : 0 : rc = 0;
484 : : }
485 [ + - ]: 229213 : if (rc == 0) {
486 [ + - - + ]: 229213 : spdk_trace_record(TRACE_ISCSI_TASK_EXECUTED, 0, 0, (uintptr_t)pdu);
487 : 229213 : conn->pdu_in_progress = NULL;
488 : 229213 : conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_AWAIT_PDU_READY;
489 : 229213 : return 1;
490 : : } else {
491 : 0 : conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
492 : : }
493 : 0 : break;
494 : 0 : case ISCSI_PDU_RECV_STATE_ERROR:
495 : 0 : return SPDK_ISCSI_CONNECTION_FATAL;
496 : 0 : default:
497 : 0 : assert(false);
498 : : printf("code should not come here\n");
499 : : break;
500 : : }
501 [ + - ]: 458428 : } while (prev_state != conn->pdu_recv_state);
502 : :
503 : 0 : return 0;
504 : : }
505 : :
506 : : #define GET_PDU_LOOP_COUNT 16
507 : :
508 : : static int
509 : 236819 : fuzz_iscsi_handle_incoming_pdus(struct spdk_iscsi_conn *conn)
510 : : {
511 : : int i, rc;
512 : :
513 : : /* Read new PDUs from network */
514 [ + - ]: 466032 : for (i = 0; i < GET_PDU_LOOP_COUNT; i++) {
515 : 466032 : rc = iscsi_fuzz_read_pdu(conn);
516 [ + + ]: 466032 : if (rc == 0) {
517 : 236819 : break;
518 [ - + ]: 229213 : } else if (rc < 0) {
519 : 0 : return rc;
520 : : }
521 : : }
522 : :
523 : 236819 : return i;
524 : : }
525 : :
526 : : static void
527 : 2 : fuzz_iscsi_send_login_request(struct fuzz_iscsi_dev_ctx *dev_ctx, uint8_t session_type)
528 : : {
529 : 2 : struct fuzz_iscsi_io_ctx *io_ctx = NULL;
530 : : struct spdk_iscsi_pdu *req_pdu;
531 : : struct iscsi_bhs_login_req *login_req;
532 : 2 : struct spdk_iscsi_conn *conn = dev_ctx->conn;
533 : :
534 : 2 : req_pdu = iscsi_get_pdu(conn);
535 : 2 : req_pdu->writev_offset = 0;
536 : 2 : req_pdu->hdigest_valid_bytes = 0;
537 : 2 : req_pdu->ahs_valid_bytes = 0;
538 : 2 : req_pdu->data_buf_len = 8192;
539 : 2 : req_pdu->data = calloc(1, 8192);
540 [ - + ]: 2 : assert(req_pdu->data != NULL);
541 : 2 : req_pdu->data_segment_len = 0;
542 : :
543 : 2 : login_req = (struct iscsi_bhs_login_req *)&req_pdu->bhs;
544 : 2 : io_ctx = &dev_ctx->io_ctx;
545 : 2 : io_ctx->req.login_req = login_req;
546 : 2 : io_ctx->req.login_req->version_min = 0;
547 : : /* a new session */
548 : 2 : io_ctx->req.login_req->tsih = 0;
549 : :
550 : 2 : req_pdu->bhs.opcode = ISCSI_OP_LOGIN;
551 : 2 : req_pdu->bhs.immediate = 1;
552 : 2 : req_pdu->bhs.reserved = 0;
553 : 2 : req_pdu->bhs_valid_bytes = ISCSI_BHS_LEN;
554 : 2 : req_pdu->bhs.total_ahs_len = 0;
555 : :
556 : : /* An initiator that chooses to operate without iSCSI security and with
557 : : * all the operational parameters taking the default values issues the
558 : : * Login with the T bit set to 1, the CSG set to
559 : : * LoginOperationalNegotiation, and the NSG set to FullFeaturePhase.
560 : : *
561 : : * Byte / 0 | 1 | 2 | 3 |
562 : : * |0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
563 : : * +---------------+---------------+---------------+---------------+
564 : : * 0|.|1| 0x03 |T|C|.|.|CSG|NSG| Version-max | Version-min |
565 : : */
566 : 2 : req_pdu->bhs.flags = ISCSI_LOGIN_TRANSIT | (ISCSI_OPERATIONAL_NEGOTIATION_PHASE << 2) |
567 : : ISCSI_FULL_FEATURE_PHASE;
568 : :
569 : 2 : req_pdu->data_segment_len = iscsi_append_text("InitiatorName", g_init_name,
570 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
571 : 2 : req_pdu->data_segment_len = iscsi_append_text("HeaderDigest", "None",
572 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
573 : 2 : req_pdu->data_segment_len = iscsi_append_text("DataDigest", "None",
574 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
575 : 2 : req_pdu->data_segment_len = iscsi_append_text("DefaultTime2Wait", "2",
576 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
577 : 2 : req_pdu->data_segment_len = iscsi_append_text("DefaultTime2Retain", "0",
578 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
579 : 2 : req_pdu->data_segment_len = iscsi_append_text("IFMarker", "No",
580 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
581 : 2 : req_pdu->data_segment_len = iscsi_append_text("OFMarker", "No",
582 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
583 : 2 : req_pdu->data_segment_len = iscsi_append_text("ErrorRecoveryLevel", "0",
584 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
585 : :
586 [ - + ]: 2 : if (session_type == SESSION_TYPE_DISCOVERY) {
587 : : /* Discovery PDU */
588 : 0 : conn->sess->session_type = SESSION_TYPE_DISCOVERY;
589 : 0 : req_pdu->data_segment_len = iscsi_append_text("SessionType", "Discovery",
590 : 0 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
591 : 0 : req_pdu->data_segment_len = iscsi_append_text("MaxRecvDataSegmentLength", "32768",
592 : 0 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
593 : : } else {
594 : : /* Login PDU */
595 : 2 : conn->sess->session_type = SESSION_TYPE_NORMAL;
596 : 2 : req_pdu->data_segment_len = iscsi_append_text("SessionType", "Normal",
597 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
598 : 2 : req_pdu->data_segment_len = iscsi_append_text("TargetName", g_tgt_name,
599 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
600 : 2 : req_pdu->data_segment_len = iscsi_append_text("InitialR2T", "No",
601 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
602 : 2 : req_pdu->data_segment_len = iscsi_append_text("ImmediateData", "Yes",
603 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
604 : 2 : req_pdu->data_segment_len = iscsi_append_text("MaxBurstLength", "16776192",
605 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
606 : 2 : req_pdu->data_segment_len = iscsi_append_text("FirstBurstLength", "262144",
607 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
608 : 2 : req_pdu->data_segment_len = iscsi_append_text("MaxOutstandingR2T", "1",
609 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
610 : 2 : req_pdu->data_segment_len = iscsi_append_text("MaxConnections", "1",
611 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
612 : 2 : req_pdu->data_segment_len = iscsi_append_text("DataPDUInOrder", "Yes",
613 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
614 : 2 : req_pdu->data_segment_len = iscsi_append_text("DataSequenceInOrder", "Yes",
615 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
616 : 2 : req_pdu->data_segment_len = iscsi_append_text("MaxRecvDataSegmentLength", "262144",
617 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
618 : : }
619 : :
620 : 2 : DSET24(req_pdu->bhs.data_segment_len, req_pdu->data_segment_len);
621 : 2 : iscsi_conn_write_pdu(conn, req_pdu, iscsi_conn_pdu_generic_complete, NULL);
622 : 2 : }
623 : :
624 : : static void
625 : 2 : fuzz_iscsi_send_logout_request(struct fuzz_iscsi_dev_ctx *dev_ctx)
626 : : {
627 : 2 : struct fuzz_iscsi_io_ctx *io_ctx = NULL;
628 : : struct spdk_iscsi_pdu *req_pdu;
629 : : struct iscsi_bhs_logout_req *logout_req;
630 : 2 : struct spdk_iscsi_conn *conn = dev_ctx->conn;
631 : :
632 : 2 : conn->is_logged_out = true;
633 : :
634 : 2 : req_pdu = iscsi_get_pdu(conn);
635 : 2 : req_pdu->writev_offset = 0;
636 : 2 : req_pdu->hdigest_valid_bytes = 0;
637 : 2 : req_pdu->ahs_valid_bytes = 0;
638 : 2 : req_pdu->data_buf_len = 0;
639 : :
640 : 2 : logout_req = (struct iscsi_bhs_logout_req *)&req_pdu->bhs;
641 : 2 : io_ctx = &dev_ctx->io_ctx;
642 : 2 : io_ctx->req.logout_req = logout_req;
643 : :
644 : 2 : req_pdu->bhs.opcode = ISCSI_OP_LOGOUT;
645 : 2 : req_pdu->bhs.immediate = 1;
646 : 2 : req_pdu->bhs.reserved = 0;
647 : 2 : req_pdu->bhs_valid_bytes = ISCSI_BHS_LEN;
648 : 2 : req_pdu->bhs.total_ahs_len = 0;
649 : 2 : req_pdu->bhs.flags = 0;
650 : :
651 : 2 : DSET24(req_pdu->bhs.data_segment_len, 0);
652 : 2 : iscsi_conn_write_pdu(conn, req_pdu, iscsi_conn_pdu_generic_complete, conn);
653 : 2 : }
654 : :
655 : : static void
656 : 2 : iscsi_fuzz_conn_reset(struct spdk_iscsi_conn *conn, struct spdk_iscsi_sess *sess)
657 : : {
658 : 2 : conn->sess = sess;
659 : 2 : conn->data_in_cnt = 0;
660 : 2 : conn->params = NULL;
661 : 2 : conn->header_digest = true;
662 : 2 : conn->data_digest = false;
663 : 2 : conn->header_digest = 0;
664 : 2 : conn->MaxRecvDataSegmentLength = 8192;
665 : 2 : conn->full_feature = 0;
666 : 2 : conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_AWAIT_PDU_READY;
667 : 2 : conn->pdu_in_progress = NULL;
668 : 2 : conn->is_logged_out = 0;
669 : 2 : }
670 : :
671 : : static void
672 : 2 : iscsi_fuzz_sock_connect(struct spdk_iscsi_conn *conn)
673 : : {
674 : 2 : const char *host = g_tgt_ip;
675 : 2 : const char *port = g_tgt_port;
676 : 0 : char saddr[INET6_ADDRSTRLEN], caddr[INET6_ADDRSTRLEN];
677 : 0 : uint16_t cport, sport;
678 : 2 : int rc = 0;
679 : :
680 : 2 : conn->sock = spdk_sock_connect(host, spdk_strtol(port, 10), NULL);
681 [ - + ]: 2 : if (conn->sock == NULL) {
682 [ # # ]: 0 : fprintf(stderr, "connect error(%d): %s\n", errno, spdk_strerror(errno));
683 : 0 : spdk_sock_close(&conn->sock);
684 : 0 : return;
685 : : }
686 [ - + ]: 2 : fprintf(stderr, "\nConnecting to the server on %s:%s\n", host, port);
687 : :
688 : 2 : rc = spdk_sock_getaddr(conn->sock, saddr, sizeof(saddr), &sport, caddr, sizeof(caddr), &cport);
689 [ - + ]: 2 : if (rc < 0) {
690 [ # # ]: 0 : fprintf(stderr, "Cannot get connection addresses\n");
691 : 0 : spdk_sock_close(&conn->sock);
692 : 0 : return;
693 : : }
694 : :
695 [ - + ]: 2 : fprintf(stderr, "Connection accepted from (%s, %hu) to (%s, %hu)\n", caddr, cport, saddr, sport);
696 : :
697 : : }
698 : :
699 : : static void
700 : 229212 : check_successful_op(struct fuzz_iscsi_dev_ctx *dev_ctx, struct fuzz_iscsi_io_ctx *io_ctx)
701 : : {
702 [ - + + + ]: 229212 : if (g_is_valid_opcode) {
703 [ - + - + ]: 22344 : fprintf(stderr, "Sent a valid opcode PDU.\n");
704 : 22344 : dev_ctx->num_valid_pdus++;
705 : : } else {
706 [ - + - + ]: 206868 : fprintf(stderr, "Sent an invalid opcode PDU.\n");
707 : : }
708 : 229212 : }
709 : :
710 : : /* submit requests begin */
711 : : static void
712 : 236754 : dev_submit_requests(struct fuzz_iscsi_dev_ctx *dev_ctx)
713 : : {
714 : 236754 : struct fuzz_iscsi_io_ctx *io_ctx = NULL;
715 : : uint8_t opcode;
716 : : struct spdk_iscsi_pdu *req_pdu;
717 : : struct iscsi_bhs *bhs;
718 : : struct iscsi_bhs_nop_out *nop_out_req;
719 : : struct iscsi_bhs_scsi_req *scsi_req;
720 : : struct iscsi_bhs_task_req *task_req;
721 : : struct iscsi_bhs_text_req *text_req;
722 : : struct iscsi_bhs_data_out *data_out_req;
723 : : struct iscsi_bhs_snack_req *snack_req;
724 : 0 : unsigned int rand_seed;
725 : : bool is_p99;
726 : :
727 : 236754 : g_is_valid_opcode = true;
728 : :
729 : : /* Random PDU */
730 : 236754 : opcode = rand() % 0x3f;
731 [ - + - + ]: 236754 : fprintf(stderr, "Random request bhs.opcode of Initiator is 0x%x.\n", opcode);
732 : :
733 [ + + + + ]: 236754 : if ((opcode == ISCSI_OP_LOGIN) || (opcode == ISCSI_OP_LOGOUT)) {
734 : : /* only need send next */
735 [ - + - + ]: 7542 : fprintf(stderr, "LOGIN and LOGOUT opcodes are ignored here.\n");
736 : 7542 : return;
737 : : }
738 : :
739 : 229212 : req_pdu = iscsi_get_pdu(dev_ctx->conn);
740 : 229212 : req_pdu->writev_offset = 0;
741 : 229212 : req_pdu->hdigest_valid_bytes = 0;
742 : 229212 : req_pdu->ahs_valid_bytes = 0;
743 : 229212 : req_pdu->data_buf_len = 0;
744 : :
745 : 229212 : dev_ctx->conn->sess->session_type = SESSION_TYPE_NORMAL;
746 : :
747 : 229212 : io_ctx = &dev_ctx->io_ctx;
748 : :
749 [ + + + + : 229212 : switch (opcode) {
+ + + ]
750 : 3687 : case ISCSI_OP_NOPOUT:
751 : 3687 : nop_out_req = (struct iscsi_bhs_nop_out *)&req_pdu->bhs;
752 : 3687 : io_ctx->req.nop_out_req = nop_out_req;
753 : 3687 : break;
754 : 3775 : case ISCSI_OP_SCSI:
755 : 3775 : scsi_req = (struct iscsi_bhs_scsi_req *)&req_pdu->bhs;
756 : 3775 : io_ctx->req.scsi_req = scsi_req;
757 : 3775 : break;
758 : 3699 : case ISCSI_OP_TASK:
759 : 3699 : task_req = (struct iscsi_bhs_task_req *)&req_pdu->bhs;
760 : 3699 : io_ctx->req.task_req = task_req;
761 : 3699 : break;
762 : 3674 : case ISCSI_OP_TEXT:
763 : 3674 : text_req = (struct iscsi_bhs_text_req *)&req_pdu->bhs;
764 : 3674 : io_ctx->req.text_req = text_req;
765 : 3674 : break;
766 : 3780 : case ISCSI_OP_SCSI_DATAOUT:
767 : 3780 : data_out_req = (struct iscsi_bhs_data_out *)&req_pdu->bhs;
768 : 3780 : io_ctx->req.data_out_req = data_out_req;
769 : 3780 : break;
770 : 3729 : case ISCSI_OP_SNACK:
771 : 3729 : snack_req = (struct iscsi_bhs_snack_req *)&req_pdu->bhs;
772 : 3729 : io_ctx->req.snack_req = snack_req;
773 : 3729 : break;
774 : 206868 : default:
775 : 206868 : bhs = (struct iscsi_bhs *)&req_pdu->bhs;
776 : 206868 : io_ctx->req.bhs = bhs;
777 : 206868 : g_is_valid_opcode = false;
778 : 206868 : break;
779 : : }
780 : :
781 : 229212 : prep_iscsi_pdu_bhs_opcode_cmd(dev_ctx, io_ctx);
782 : 229212 : io_ctx->req.bhs->opcode = opcode;
783 : 229212 : req_pdu->bhs.opcode = opcode;
784 : 229212 : req_pdu->bhs.immediate = 1;
785 : 229212 : req_pdu->bhs.reserved = 0;
786 : 229212 : req_pdu->bhs_valid_bytes = ISCSI_BHS_LEN;
787 : 229212 : req_pdu->bhs.total_ahs_len = 0;
788 : 229212 : req_pdu->bhs.stat_sn = 0;
789 : 229212 : DSET24(req_pdu->bhs.data_segment_len, 0);
790 : :
791 [ + + ]: 229212 : if (opcode <= ISCSI_OP_TEXT) {
792 : 14835 : rand_seed = time(NULL);
793 : 14835 : is_p99 = rand_r(&rand_seed) % 100 == 0 ? true : false;
794 [ + - ]: 14835 : if (!is_p99) { /* Remaining 1% */
795 [ + + + + : 14835 : switch (opcode) {
- ]
796 : 3687 : case ISCSI_OP_NOPOUT:
797 [ + - ]: 3687 : if (req_pdu->bhs.immediate) {
798 : 3687 : io_ctx->req.nop_out_req->cmd_sn = dev_ctx->current_cmd_sn;
799 : : } else {
800 : 0 : io_ctx->req.nop_out_req->cmd_sn = dev_ctx->current_cmd_sn++;
801 : : }
802 : 3687 : break;
803 : 3775 : case ISCSI_OP_SCSI:
804 [ + - ]: 3775 : if (req_pdu->bhs.immediate) {
805 : 3775 : io_ctx->req.scsi_req->cmd_sn = dev_ctx->current_cmd_sn;
806 : : } else {
807 : 0 : io_ctx->req.scsi_req->cmd_sn = dev_ctx->current_cmd_sn++;
808 : : }
809 : 3775 : break;
810 : 3699 : case ISCSI_OP_TASK:
811 [ + - ]: 3699 : if (req_pdu->bhs.immediate) {
812 : 3699 : io_ctx->req.task_req->cmd_sn = dev_ctx->current_cmd_sn;
813 : : } else {
814 : 0 : io_ctx->req.task_req->cmd_sn = dev_ctx->current_cmd_sn++;
815 : : }
816 : 3699 : break;
817 : 3674 : case ISCSI_OP_TEXT:
818 [ + - ]: 3674 : if (req_pdu->bhs.immediate) {
819 : 3674 : io_ctx->req.text_req->cmd_sn = dev_ctx->current_cmd_sn;
820 : : } else {
821 : 0 : io_ctx->req.text_req->cmd_sn = dev_ctx->current_cmd_sn++;
822 : : }
823 : 3674 : break;
824 : 0 : default:
825 : 0 : break;
826 : : }
827 : 0 : }
828 : : }
829 : :
830 [ + + ]: 229212 : if (opcode == ISCSI_OP_SCSI) {
831 : : /* avoid ((R_bit != 0) && (W_bit != 0)) is true */
832 : 3775 : io_ctx->req.scsi_req->read_bit = 0;
833 : 3775 : io_ctx->req.scsi_req->write_bit = 0;
834 : : }
835 : :
836 [ + + ]: 229212 : if (opcode == ISCSI_OP_TEXT) {
837 : : /* avoid: (F_bit && C_bit) is true */
838 : 3674 : io_ctx->req.text_req->flags = 0;
839 : : /* avoid: correct itt is not equal to the current itt */
840 : 3674 : io_ctx->req.text_req->itt = 0;
841 : : }
842 : :
843 [ - + - + ]: 229212 : fprintf(stderr, "Dumping this request bhs contents now.\n");
844 : 229212 : print_req_obj(dev_ctx, io_ctx);
845 : :
846 : 229212 : check_successful_op(dev_ctx, io_ctx);
847 : 229212 : dev_ctx->num_sent_pdus++;
848 : :
849 : 229212 : iscsi_conn_write_pdu(dev_ctx->conn, req_pdu,
850 : : iscsi_conn_pdu_generic_complete, NULL);
851 : : }
852 : : /* submit requests end */
853 : :
854 : : static int
855 : 236819 : poll_dev(void *ctx)
856 : : {
857 : 236819 : struct fuzz_iscsi_dev_ctx *dev_ctx = ctx;
858 : 236819 : struct spdk_iscsi_conn *conn = dev_ctx->conn;
859 : : uint64_t current_ticks;
860 : : struct spdk_iscsi_pdu *pdu, *tmp;
861 : :
862 : 236819 : current_ticks = spdk_get_ticks();
863 [ + + ]: 236819 : if (current_ticks > g_runtime_ticks) {
864 : 2 : g_run = false;
865 : : }
866 : :
867 [ - + + + ]: 236819 : if (!g_run) {
868 : : /* Logout PDU */
869 : 2 : fuzz_iscsi_send_logout_request(dev_ctx);
870 : 2 : fuzz_iscsi_handle_incoming_pdus(conn);
871 : :
872 [ + + ]: 229217 : TAILQ_FOREACH_SAFE(pdu, &g_get_pdu_list, tailq, tmp) {
873 [ + + ]: 229215 : TAILQ_REMOVE(&g_get_pdu_list, pdu, tailq);
874 : 229215 : iscsi_put_pdu(pdu);
875 : : }
876 : :
877 : 2 : spdk_sock_close(&conn->sock);
878 : :
879 [ - + ]: 2 : TAILQ_FOREACH_SAFE(pdu, &conn->write_pdu_list, tailq, tmp) {
880 [ # # ]: 0 : TAILQ_REMOVE(&conn->write_pdu_list, pdu, tailq);
881 : 0 : iscsi_put_pdu(pdu);
882 : : }
883 : :
884 : 2 : free(conn);
885 : :
886 : 2 : spdk_poller_unregister(&dev_ctx->poller);
887 : 2 : __sync_sub_and_fetch(&g_num_active_threads, 1);
888 : :
889 : 2 : return -1;
890 : : }
891 : :
892 [ - + - + ]: 236817 : if (conn->is_logged_out) {
893 : 0 : spdk_sock_close(&conn->sock);
894 : 0 : iscsi_fuzz_conn_reset(conn, &dev_ctx->sess);
895 : 0 : iscsi_fuzz_sock_connect(conn);
896 : 0 : usleep(1000);
897 : :
898 : : /* Login PDU */
899 : 0 : fuzz_iscsi_send_login_request(dev_ctx, SESSION_TYPE_NORMAL);
900 [ + + ]: 236817 : } else if (conn->full_feature == 1) {
901 : 236754 : dev_submit_requests(dev_ctx);
902 : : }
903 : :
904 : 236817 : spdk_sock_flush(conn->sock);
905 : :
906 : 236817 : fuzz_iscsi_handle_incoming_pdus(conn);
907 : :
908 : 236817 : return 0;
909 : : }
910 : :
911 : : static void
912 : 2 : start_io(void *ctx)
913 : : {
914 : 2 : struct fuzz_iscsi_dev_ctx *dev_ctx = ctx;
915 : :
916 : 2 : dev_ctx->sess.ExpCmdSN = 0;
917 : 2 : dev_ctx->sess.MaxCmdSN = 64;
918 : 2 : dev_ctx->sess.MaxBurstLength = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
919 : 2 : dev_ctx->sess.MaxOutstandingR2T = 1;
920 : 2 : dev_ctx->sess.tag = 1;
921 : 2 : dev_ctx->sess.tsih = 256;
922 : :
923 : 2 : dev_ctx->conn = calloc(1, sizeof(*dev_ctx->conn));
924 [ - + ]: 2 : assert(dev_ctx->conn != NULL);
925 : 2 : TAILQ_INIT(&dev_ctx->conn->write_pdu_list);
926 : :
927 : 2 : iscsi_fuzz_conn_reset(dev_ctx->conn, &dev_ctx->sess);
928 : 2 : iscsi_fuzz_sock_connect(dev_ctx->conn);
929 : 2 : usleep(1000);
930 : :
931 : : /* Login PDU */
932 : 2 : fuzz_iscsi_send_login_request(dev_ctx, SESSION_TYPE_NORMAL);
933 : :
934 [ - + ]: 2 : if (g_random_seed) {
935 : 0 : dev_ctx->random_seed = g_random_seed;
936 : : } else {
937 : 2 : dev_ctx->random_seed = spdk_get_ticks();
938 : : }
939 : :
940 : 2 : dev_ctx->poller = SPDK_POLLER_REGISTER(poll_dev, dev_ctx, 0);
941 [ - + ]: 2 : if (dev_ctx->poller == NULL) {
942 : 0 : return;
943 : : }
944 : : }
945 : :
946 : : static int
947 : 60 : check_app_completion(void *ctx)
948 : : {
949 [ + + ]: 60 : if (g_num_active_threads == 0) {
950 : 2 : spdk_poller_unregister(&g_app_completion_poller);
951 [ - + ]: 2 : printf("Fuzzing completed. Shutting down the fuzz application.\n\n");
952 : 2 : cleanup();
953 : 2 : spdk_app_stop(0);
954 : : }
955 : 60 : return 0;
956 : : }
957 : :
958 : : static void
959 : 2 : begin_iscsi_fuzz(void *ctx)
960 : : {
961 : : struct fuzz_iscsi_dev_ctx *dev_ctx;
962 : : int rc;
963 : :
964 : 2 : g_runtime_ticks = spdk_get_ticks() + g_runtime * spdk_get_ticks_hz();
965 : :
966 : 2 : g_valid_buffer = spdk_malloc(0x1000, 0x200, NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_SHARE);
967 [ - + ]: 2 : if (g_valid_buffer == NULL) {
968 [ # # # # ]: 0 : fprintf(stderr, "Failed to allocate a valid buffer for random PDUs\n");
969 : 0 : goto out;
970 : : }
971 : :
972 : 2 : rc = fuzz_iscsi_dev_init();
973 [ - + ]: 2 : if (rc) {
974 [ # # # # ]: 0 : fprintf(stderr, "fuzz_iscsi_dev_init() failed.\n");
975 : 0 : goto out;
976 : : }
977 : :
978 [ + + ]: 4 : TAILQ_FOREACH(dev_ctx, &g_dev_list, link) {
979 [ - + ]: 2 : assert(dev_ctx->thread != NULL);
980 : 2 : spdk_thread_send_msg(dev_ctx->thread, start_io, dev_ctx);
981 : 2 : __sync_add_and_fetch(&g_num_active_threads, 1);
982 : : }
983 : :
984 : 2 : g_app_completion_poller = SPDK_POLLER_REGISTER(check_app_completion, NULL, 1000000);
985 [ - + ]: 2 : if (g_app_completion_poller == NULL) {
986 [ # # # # ]: 0 : fprintf(stderr, "Failed to register a poller for test completion checking.\n");
987 : 0 : goto out;
988 : : }
989 : :
990 : 2 : return;
991 : 0 : out:
992 : 0 : cleanup();
993 : 0 : spdk_app_stop(0);
994 : : }
995 : :
996 : : static void
997 : 0 : iscsi_fuzz_usage(void)
998 : : {
999 [ # # # # ]: 0 : fprintf(stderr, " -T <path> iSCSI Target IP address.\n");
1000 [ # # # # ]: 0 : fprintf(stderr, " -S <integer> Seed value for test.\n");
1001 [ # # # # ]: 0 : fprintf(stderr,
1002 : : " -t <integer> Time in seconds to run the fuzz test. Only valid if -j is not specified.\n");
1003 : 0 : }
1004 : :
1005 : : static int
1006 : 4 : iscsi_fuzz_parse(int ch, char *arg)
1007 : : {
1008 : : int64_t error_test;
1009 : :
1010 [ + - + - ]: 4 : switch (ch) {
1011 : 2 : case 'T':
1012 : 2 : g_tgt_ip = optarg;
1013 : 2 : break;
1014 : 0 : case 'S':
1015 : 0 : error_test = spdk_strtol(arg, 10);
1016 [ # # ]: 0 : if (error_test < 0) {
1017 [ # # # # ]: 0 : fprintf(stderr, "Invalid value supplied for the random seed.\n");
1018 : 0 : return -1;
1019 : : } else {
1020 : 0 : g_random_seed = error_test;
1021 : : }
1022 : 0 : break;
1023 : 2 : case 't':
1024 : 2 : g_runtime = spdk_strtol(optarg, 10);
1025 [ + - - + ]: 2 : if (g_runtime <= 0 || g_runtime > MAX_RUNTIME_S) {
1026 [ # # # # ]: 0 : fprintf(stderr, "You must supply a positive runtime value less than %d.\n", MAX_RUNTIME_S);
1027 : 0 : return -1;
1028 : : }
1029 : 2 : break;
1030 : 0 : case '?':
1031 : : default:
1032 : 0 : iscsi_fuzz_usage();
1033 : 0 : return -EINVAL;
1034 : : }
1035 : :
1036 : 4 : return 0;
1037 : : }
1038 : :
1039 : : int
1040 : 2 : main(int argc, char **argv)
1041 : : {
1042 : 2 : struct spdk_app_opts opts = {};
1043 : : int rc;
1044 : :
1045 : 2 : g_runtime = DEFAULT_RUNTIME;
1046 : 2 : srand((unsigned)time(0));
1047 : :
1048 : 2 : TAILQ_INIT(&g_get_pdu_list);
1049 : :
1050 : 2 : spdk_app_opts_init(&opts, sizeof(opts));
1051 : 2 : opts.name = "iscsi_fuzz";
1052 : :
1053 : 2 : if ((rc = spdk_app_parse_args(argc, argv, &opts, "T:S:t:", NULL, iscsi_fuzz_parse,
1054 [ - + ]: 2 : iscsi_fuzz_usage) != SPDK_APP_PARSE_ARGS_SUCCESS)) {
1055 : 0 : return rc;
1056 : : }
1057 : :
1058 : 2 : rc = spdk_app_start(&opts, begin_iscsi_fuzz, NULL);
1059 : :
1060 : 2 : spdk_app_fini();
1061 : 2 : return rc;
1062 : : }
|