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 : 216038 : fuzz_fill_random_bytes(char *character_repr, size_t len, unsigned int *rand_seed)
85 : : {
86 : : size_t i;
87 : :
88 [ + + ]: 10585862 : for (i = 0; i < len; i++) {
89 : 10369824 : character_repr[i] = rand_r(rand_seed) % UINT8_MAX;
90 : : }
91 : 216038 : }
92 : :
93 : : static char *
94 : 216038 : 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 : 216038 : total_size = spdk_base64_get_encoded_strlen(len) + 1;
102 : :
103 : 216038 : value_string = calloc(1, total_size);
104 [ - + ]: 216038 : if (value_string == NULL) {
105 : 0 : return NULL;
106 : : }
107 : :
108 : 216038 : rc = spdk_base64_encode(value_string, item, len);
109 [ - + ]: 216038 : if (rc < 0) {
110 : 0 : free(value_string);
111 : 0 : return NULL;
112 : : }
113 : :
114 : 216038 : 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 : 432082 : iscsi_put_pdu(struct spdk_iscsi_pdu *pdu)
132 : : {
133 [ - + ]: 432082 : if (!pdu) {
134 : 0 : return;
135 : : }
136 : :
137 : 432082 : pdu->ref--;
138 [ - + ]: 432082 : if (pdu->ref < 0) {
139 : 0 : pdu->ref = 0;
140 : : }
141 : :
142 [ + - ]: 432082 : if (pdu->ref == 0) {
143 [ + + ]: 432082 : if (pdu->data) {
144 : 205254 : free(pdu->data);
145 : : }
146 : 432082 : free(pdu);
147 : : }
148 : : }
149 : :
150 : : struct spdk_iscsi_pdu *
151 : 432082 : iscsi_get_pdu(struct spdk_iscsi_conn *conn)
152 : : {
153 : : struct spdk_iscsi_pdu *pdu;
154 : :
155 : 432082 : pdu = calloc(1, sizeof(*pdu));
156 [ - + ]: 432082 : if (!pdu) {
157 : 0 : return NULL;
158 : : }
159 : :
160 : 432082 : pdu->ref = 1;
161 : 432082 : pdu->conn = conn;
162 : :
163 : 432082 : 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 : 216038 : dump_iscsi_cmd(void *ctx, const void *data, size_t size)
220 : : {
221 [ - + - + ]: 216038 : fprintf(stderr, "%s\n", (const char *)data);
222 : 216038 : return 0;
223 : : }
224 : :
225 : : static void
226 : 216038 : 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 : 216038 : 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 : 216038 : spdk_json_write_named_uint32(w, "opcode", io_ctx->req.bhs->opcode);
234 : 216038 : spdk_json_write_named_uint32(w, "immediate", io_ctx->req.bhs->immediate);
235 : 216038 : spdk_json_write_named_uint32(w, "reserved", io_ctx->req.bhs->reserved);
236 : 216038 : spdk_json_write_named_uint32(w, "total_ahs_len", io_ctx->req.bhs->total_ahs_len);
237 : 216038 : spdk_json_write_named_string(w, "data_segment_len", data_segment_len);
238 : 216038 : spdk_json_write_named_uint32(w, "itt", io_ctx->req.bhs->itt);
239 : 216038 : spdk_json_write_named_uint32(w, "exp_stat_sn", io_ctx->req.bhs->exp_stat_sn);
240 : :
241 : 216038 : free(data_segment_len);
242 : 216038 : }
243 : :
244 : : static void
245 : 216038 : 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 : 216038 : w = spdk_json_write_begin(dump_iscsi_cmd, NULL, SPDK_JSON_WRITE_FLAG_FORMATTED);
250 : 216038 : spdk_json_write_named_object_begin(w, "bhs");
251 : 216038 : print_scsi_io_data(w, io_ctx);
252 : 216038 : spdk_json_write_object_end(w);
253 : 216038 : spdk_json_write_end(w);
254 : 216038 : }
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 : 216038 : prep_iscsi_pdu_bhs_opcode_cmd(struct fuzz_iscsi_dev_ctx *dev_ctx, struct fuzz_iscsi_io_ctx *io_ctx)
291 : : {
292 : 216038 : io_ctx->iov_ctx.iov_req.iov_len = sizeof(struct iscsi_bhs);
293 : 216038 : fuzz_fill_random_bytes((char *)io_ctx->req.bhs, sizeof(struct iscsi_bhs),
294 : : &dev_ctx->random_seed);
295 : 216038 : }
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 : 216039 : iscsi_fuzz_pdu_hdr_handle(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
306 : : {
307 : : int opcode;
308 : 216039 : int rc = 0;
309 : :
310 : 216039 : opcode = pdu->bhs.opcode;
311 [ + + ]: 216039 : if (opcode == ISCSI_OP_LOGIN_RSP) {
312 : 2 : return iscsi_pdu_hdr_op_login_rsp(conn, pdu);
313 : : }
314 : :
315 [ - + + - ]: 216037 : 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 : 14273 : 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 [ - + - + ]: 14273 : fprintf(stderr, "Received hdr_handle response opcode from Target is 0x%x.\n", pdu->bhs.opcode);
331 : 14273 : break;
332 : 201764 : case ISCSI_OP_REJECT:
333 [ - + - + ]: 201764 : fprintf(stderr, "Received rejected hdr_handle response opcode(0x3f) from Target.\n");
334 : 201764 : break;
335 : 0 : default:
336 : 0 : rc = -1;
337 : 0 : break;
338 : : }
339 : :
340 : 216037 : 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 : 216038 : iscsi_fuzz_pdu_payload_handle(struct spdk_iscsi_conn *conn, struct spdk_iscsi_pdu *pdu)
366 : : {
367 : : int opcode;
368 : 216038 : int rc = 0;
369 : :
370 : 216038 : opcode = pdu->bhs.opcode;
371 [ - + - + ]: 216038 : fprintf(stderr, "Received payload_handle response opcode from Target is 0x%x.\n", opcode);
372 : :
373 [ + + - ]: 216038 : switch (opcode) {
374 : 2 : case ISCSI_OP_LOGIN_RSP:
375 : 2 : rc = iscsi_pdu_payload_op_login_rsp(conn, pdu);
376 : 2 : break;
377 : 216036 : 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 : 216036 : break;
389 : 0 : default:
390 : 0 : rc = -1;
391 : 0 : break;
392 : : }
393 : :
394 : 216038 : return rc;
395 : : }
396 : :
397 : : static int
398 : 439222 : 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 : 871301 : prev_state = conn->pdu_recv_state;
407 : 871301 : pdu = conn->pdu_in_progress;
408 : :
409 [ + + + + : 871301 : switch (conn->pdu_recv_state) {
- ]
410 : 216040 : case ISCSI_PDU_RECV_STATE_AWAIT_PDU_READY:
411 [ - + ]: 216040 : assert(conn->pdu_in_progress == NULL);
412 : :
413 : 216040 : conn->pdu_in_progress = iscsi_get_pdu(conn);
414 [ - + ]: 216040 : if (conn->pdu_in_progress == NULL) {
415 : 0 : return SPDK_ISCSI_CONNECTION_FATAL;
416 : : }
417 : 216040 : TAILQ_INSERT_TAIL(&g_get_pdu_list, conn->pdu_in_progress, tailq);
418 : 216040 : conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_AWAIT_PDU_HDR;
419 : 216040 : break;
420 : 439222 : case ISCSI_PDU_RECV_STATE_AWAIT_PDU_HDR:
421 [ + - ]: 439222 : if (pdu->bhs_valid_bytes < ISCSI_BHS_LEN) {
422 : 1317666 : rc = iscsi_conn_read_data(conn,
423 : 439222 : ISCSI_BHS_LEN - pdu->bhs_valid_bytes,
424 : 439222 : (uint8_t *)&pdu->bhs + pdu->bhs_valid_bytes);
425 [ - + ]: 439222 : if (rc < 0) {
426 : 0 : conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
427 : 0 : break;
428 : : }
429 : 439222 : pdu->bhs_valid_bytes += rc;
430 [ + + ]: 439222 : if (pdu->bhs_valid_bytes < ISCSI_BHS_LEN) {
431 : 223183 : return 0;
432 : : }
433 : : }
434 : :
435 : 216039 : pdu->data_segment_len = ISCSI_ALIGN(DGET24(pdu->bhs.data_segment_len));
436 : :
437 : 216039 : rc = iscsi_fuzz_pdu_hdr_handle(conn, pdu);
438 [ - + ]: 216039 : 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 [ - + + + ]: 216039 : if (conn->is_logged_out) {
445 : 1 : printf("pdu received after logout\n");
446 : 1 : conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
447 : 1 : break;
448 : : }
449 : :
450 : 216038 : conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD;
451 : 216038 : break;
452 : 216038 : case ISCSI_PDU_RECV_STATE_AWAIT_PDU_PAYLOAD:
453 : 216038 : data_len = pdu->data_segment_len;
454 [ + + + - ]: 216038 : if (data_len != 0 && pdu->data == NULL) {
455 : 205252 : pdu->data = calloc(1, data_len);
456 [ - + ]: 205252 : if (pdu->data == NULL) {
457 : 0 : return 0;
458 : : }
459 : : }
460 : :
461 : : /* copy the actual data into local buffer */
462 [ + + ]: 216038 : if (pdu->data_valid_bytes < data_len) {
463 : 205252 : rc = iscsi_conn_read_data_segment(conn, pdu,
464 : : pdu->data_valid_bytes,
465 : 205252 : data_len - pdu->data_valid_bytes);
466 [ - + ]: 205252 : if (rc < 0) {
467 : 0 : conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
468 : 0 : break;
469 : : }
470 : 205252 : pdu->data_valid_bytes += rc;
471 [ - + ]: 205252 : if (pdu->data_valid_bytes < data_len) {
472 : 0 : return 0;
473 : : }
474 : : }
475 : :
476 [ - + + - ]: 216038 : if (!pdu->is_rejected) {
477 : 216038 : rc = iscsi_fuzz_pdu_payload_handle(conn, pdu);
478 : : } else {
479 : 0 : rc = 0;
480 : : }
481 [ + - ]: 216038 : if (rc == 0) {
482 [ + - - + ]: 216038 : spdk_trace_record(TRACE_ISCSI_TASK_EXECUTED, 0, 0, (uintptr_t)pdu);
483 : 216038 : conn->pdu_in_progress = NULL;
484 : 216038 : conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_AWAIT_PDU_READY;
485 : 216038 : return 1;
486 : : } else {
487 : 0 : conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_ERROR;
488 : : }
489 : 0 : break;
490 : 1 : case ISCSI_PDU_RECV_STATE_ERROR:
491 : 1 : return SPDK_ISCSI_CONNECTION_FATAL;
492 : 0 : default:
493 : 0 : assert(false);
494 : : printf("code should not come here\n");
495 : : break;
496 : : }
497 [ + - ]: 432079 : } while (prev_state != conn->pdu_recv_state);
498 : :
499 : 0 : return 0;
500 : : }
501 : :
502 : : #define GET_PDU_LOOP_COUNT 16
503 : :
504 : : static int
505 : 223284 : fuzz_iscsi_handle_incoming_pdus(struct spdk_iscsi_conn *conn)
506 : : {
507 : : int i, rc;
508 : :
509 : : /* Read new PDUs from network */
510 [ + + ]: 439322 : for (i = 0; i < GET_PDU_LOOP_COUNT; i++) {
511 : 439222 : rc = iscsi_fuzz_read_pdu(conn);
512 [ + + ]: 439222 : if (rc == 0) {
513 : 223183 : break;
514 [ + + ]: 216039 : } else if (rc < 0) {
515 : 1 : return rc;
516 : : }
517 : : }
518 : :
519 : 223283 : return i;
520 : : }
521 : :
522 : : static void
523 : 2 : fuzz_iscsi_send_login_request(struct fuzz_iscsi_dev_ctx *dev_ctx, uint8_t session_type)
524 : : {
525 : 2 : struct fuzz_iscsi_io_ctx *io_ctx = NULL;
526 : : struct spdk_iscsi_pdu *req_pdu;
527 : : struct iscsi_bhs_login_req *login_req;
528 : 2 : struct spdk_iscsi_conn *conn = dev_ctx->conn;
529 : :
530 : 2 : req_pdu = iscsi_get_pdu(conn);
531 : 2 : req_pdu->writev_offset = 0;
532 : 2 : req_pdu->hdigest_valid_bytes = 0;
533 : 2 : req_pdu->ahs_valid_bytes = 0;
534 : 2 : req_pdu->data_buf_len = 8192;
535 : 2 : req_pdu->data = calloc(1, 8192);
536 [ - + ]: 2 : assert(req_pdu->data != NULL);
537 : 2 : req_pdu->data_segment_len = 0;
538 : :
539 : 2 : login_req = (struct iscsi_bhs_login_req *)&req_pdu->bhs;
540 : 2 : io_ctx = &dev_ctx->io_ctx;
541 : 2 : io_ctx->req.login_req = login_req;
542 : 2 : io_ctx->req.login_req->version_min = 0;
543 : : /* a new session */
544 : 2 : io_ctx->req.login_req->tsih = 0;
545 : :
546 : 2 : req_pdu->bhs.opcode = ISCSI_OP_LOGIN;
547 : 2 : req_pdu->bhs.immediate = 1;
548 : 2 : req_pdu->bhs.reserved = 0;
549 : 2 : req_pdu->bhs_valid_bytes = ISCSI_BHS_LEN;
550 : 2 : req_pdu->bhs.total_ahs_len = 0;
551 : :
552 : : /* An initiator that chooses to operate without iSCSI security and with
553 : : * all the operational parameters taking the default values issues the
554 : : * Login with the T bit set to 1, the CSG set to
555 : : * LoginOperationalNegotiation, and the NSG set to FullFeaturePhase.
556 : : *
557 : : * Byte / 0 | 1 | 2 | 3 |
558 : : * |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|
559 : : * +---------------+---------------+---------------+---------------+
560 : : * 0|.|1| 0x03 |T|C|.|.|CSG|NSG| Version-max | Version-min |
561 : : */
562 : 2 : req_pdu->bhs.flags = ISCSI_LOGIN_TRANSIT | (ISCSI_OPERATIONAL_NEGOTIATION_PHASE << 2) |
563 : : ISCSI_FULL_FEATURE_PHASE;
564 : :
565 : 2 : req_pdu->data_segment_len = iscsi_append_text("InitiatorName", g_init_name,
566 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
567 : 2 : req_pdu->data_segment_len = iscsi_append_text("HeaderDigest", "None",
568 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
569 : 2 : req_pdu->data_segment_len = iscsi_append_text("DataDigest", "None",
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("DefaultTime2Wait", "2",
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("DefaultTime2Retain", "0",
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("IFMarker", "No",
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("OFMarker", "No",
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("ErrorRecoveryLevel", "0",
580 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
581 : :
582 [ - + ]: 2 : if (session_type == SESSION_TYPE_DISCOVERY) {
583 : : /* Discovery PDU */
584 : 0 : conn->sess->session_type = SESSION_TYPE_DISCOVERY;
585 : 0 : req_pdu->data_segment_len = iscsi_append_text("SessionType", "Discovery",
586 : 0 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
587 : 0 : req_pdu->data_segment_len = iscsi_append_text("MaxRecvDataSegmentLength", "32768",
588 : 0 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
589 : : } else {
590 : : /* Login PDU */
591 : 2 : conn->sess->session_type = SESSION_TYPE_NORMAL;
592 : 2 : req_pdu->data_segment_len = iscsi_append_text("SessionType", "Normal",
593 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
594 : 2 : req_pdu->data_segment_len = iscsi_append_text("TargetName", g_tgt_name,
595 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
596 : 2 : req_pdu->data_segment_len = iscsi_append_text("InitialR2T", "No",
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("ImmediateData", "Yes",
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("MaxBurstLength", "16776192",
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("FirstBurstLength", "262144",
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("MaxOutstandingR2T", "1",
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("MaxConnections", "1",
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("DataPDUInOrder", "Yes",
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("DataSequenceInOrder", "Yes",
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("MaxRecvDataSegmentLength", "262144",
613 : 2 : req_pdu->data, req_pdu->data_buf_len, req_pdu->data_segment_len);
614 : : }
615 : :
616 : 2 : DSET24(req_pdu->bhs.data_segment_len, req_pdu->data_segment_len);
617 : 2 : iscsi_conn_write_pdu(conn, req_pdu, iscsi_conn_pdu_generic_complete, NULL);
618 : 2 : }
619 : :
620 : : static void
621 : 2 : fuzz_iscsi_send_logout_request(struct fuzz_iscsi_dev_ctx *dev_ctx)
622 : : {
623 : 2 : struct fuzz_iscsi_io_ctx *io_ctx = NULL;
624 : : struct spdk_iscsi_pdu *req_pdu;
625 : : struct iscsi_bhs_logout_req *logout_req;
626 : 2 : struct spdk_iscsi_conn *conn = dev_ctx->conn;
627 : :
628 : 2 : conn->is_logged_out = true;
629 : :
630 : 2 : req_pdu = iscsi_get_pdu(conn);
631 : 2 : req_pdu->writev_offset = 0;
632 : 2 : req_pdu->hdigest_valid_bytes = 0;
633 : 2 : req_pdu->ahs_valid_bytes = 0;
634 : 2 : req_pdu->data_buf_len = 0;
635 : :
636 : 2 : logout_req = (struct iscsi_bhs_logout_req *)&req_pdu->bhs;
637 : 2 : io_ctx = &dev_ctx->io_ctx;
638 : 2 : io_ctx->req.logout_req = logout_req;
639 : :
640 : 2 : req_pdu->bhs.opcode = ISCSI_OP_LOGOUT;
641 : 2 : req_pdu->bhs.immediate = 1;
642 : 2 : req_pdu->bhs.reserved = 0;
643 : 2 : req_pdu->bhs_valid_bytes = ISCSI_BHS_LEN;
644 : 2 : req_pdu->bhs.total_ahs_len = 0;
645 : 2 : req_pdu->bhs.flags = 0;
646 : :
647 : 2 : DSET24(req_pdu->bhs.data_segment_len, 0);
648 : 2 : iscsi_conn_write_pdu(conn, req_pdu, iscsi_conn_pdu_generic_complete, conn);
649 : 2 : }
650 : :
651 : : static void
652 : 2 : iscsi_fuzz_conn_reset(struct spdk_iscsi_conn *conn, struct spdk_iscsi_sess *sess)
653 : : {
654 : 2 : conn->sess = sess;
655 : 2 : conn->data_in_cnt = 0;
656 : 2 : conn->params = NULL;
657 : 2 : conn->header_digest = true;
658 : 2 : conn->data_digest = false;
659 : 2 : conn->header_digest = 0;
660 : 2 : conn->MaxRecvDataSegmentLength = 8192;
661 : 2 : conn->full_feature = 0;
662 : 2 : conn->pdu_recv_state = ISCSI_PDU_RECV_STATE_AWAIT_PDU_READY;
663 : 2 : conn->pdu_in_progress = NULL;
664 : 2 : conn->is_logged_out = 0;
665 : 2 : }
666 : :
667 : : static void
668 : 2 : iscsi_fuzz_sock_connect(struct spdk_iscsi_conn *conn)
669 : : {
670 : 2 : const char *host = g_tgt_ip;
671 : 2 : const char *port = g_tgt_port;
672 : 0 : char saddr[INET6_ADDRSTRLEN], caddr[INET6_ADDRSTRLEN];
673 : 0 : uint16_t cport, sport;
674 : 2 : int rc = 0;
675 : :
676 : 2 : conn->sock = spdk_sock_connect(host, spdk_strtol(port, 10), NULL);
677 [ - + ]: 2 : if (conn->sock == NULL) {
678 [ # # ]: 0 : fprintf(stderr, "connect error(%d): %s\n", errno, spdk_strerror(errno));
679 : 0 : spdk_sock_close(&conn->sock);
680 : 0 : return;
681 : : }
682 [ - + ]: 2 : fprintf(stderr, "\nConnecting to the server on %s:%s\n", host, port);
683 : :
684 : 2 : rc = spdk_sock_getaddr(conn->sock, saddr, sizeof(saddr), &sport, caddr, sizeof(caddr), &cport);
685 [ - + ]: 2 : if (rc < 0) {
686 [ # # ]: 0 : fprintf(stderr, "Cannot get connection addresses\n");
687 : 0 : spdk_sock_close(&conn->sock);
688 : 0 : return;
689 : : }
690 : :
691 [ - + ]: 2 : fprintf(stderr, "Connection accepted from (%s, %hu) to (%s, %hu)\n", caddr, cport, saddr, sport);
692 : :
693 : : }
694 : :
695 : : static void
696 : 216038 : check_successful_op(struct fuzz_iscsi_dev_ctx *dev_ctx, struct fuzz_iscsi_io_ctx *io_ctx)
697 : : {
698 [ - + + + ]: 216038 : if (g_is_valid_opcode) {
699 [ - + - + ]: 21324 : fprintf(stderr, "Sent a valid opcode PDU.\n");
700 : 21324 : dev_ctx->num_valid_pdus++;
701 : : } else {
702 [ - + - + ]: 194714 : fprintf(stderr, "Sent an invalid opcode PDU.\n");
703 : : }
704 : 216038 : }
705 : :
706 : : /* submit requests begin */
707 : : static void
708 : 223194 : dev_submit_requests(struct fuzz_iscsi_dev_ctx *dev_ctx)
709 : : {
710 : 223194 : struct fuzz_iscsi_io_ctx *io_ctx = NULL;
711 : : uint8_t opcode;
712 : : struct spdk_iscsi_pdu *req_pdu;
713 : : struct iscsi_bhs *bhs;
714 : : struct iscsi_bhs_nop_out *nop_out_req;
715 : : struct iscsi_bhs_scsi_req *scsi_req;
716 : : struct iscsi_bhs_task_req *task_req;
717 : : struct iscsi_bhs_text_req *text_req;
718 : : struct iscsi_bhs_data_out *data_out_req;
719 : : struct iscsi_bhs_snack_req *snack_req;
720 : 0 : unsigned int rand_seed;
721 : : bool is_p99;
722 : :
723 : 223194 : g_is_valid_opcode = true;
724 : :
725 : : /* Random PDU */
726 : 223194 : opcode = rand() % 0x3f;
727 [ - + - + ]: 223194 : fprintf(stderr, "Random request bhs.opcode of Initiator is 0x%x.\n", opcode);
728 : :
729 [ + + + + ]: 223194 : if ((opcode == ISCSI_OP_LOGIN) || (opcode == ISCSI_OP_LOGOUT)) {
730 : : /* only need send next */
731 [ - + - + ]: 7156 : fprintf(stderr, "LOGIN and LOGOUT opcodes are ignored here.\n");
732 : 7156 : return;
733 : : }
734 : :
735 : 216038 : req_pdu = iscsi_get_pdu(dev_ctx->conn);
736 : 216038 : req_pdu->writev_offset = 0;
737 : 216038 : req_pdu->hdigest_valid_bytes = 0;
738 : 216038 : req_pdu->ahs_valid_bytes = 0;
739 : 216038 : req_pdu->data_buf_len = 0;
740 : :
741 : 216038 : dev_ctx->conn->sess->session_type = SESSION_TYPE_NORMAL;
742 : :
743 : 216038 : io_ctx = &dev_ctx->io_ctx;
744 : :
745 [ + + + + : 216038 : switch (opcode) {
+ + + ]
746 : 3575 : case ISCSI_OP_NOPOUT:
747 : 3575 : nop_out_req = (struct iscsi_bhs_nop_out *)&req_pdu->bhs;
748 : 3575 : io_ctx->req.nop_out_req = nop_out_req;
749 : 3575 : break;
750 : 3506 : case ISCSI_OP_SCSI:
751 : 3506 : scsi_req = (struct iscsi_bhs_scsi_req *)&req_pdu->bhs;
752 : 3506 : io_ctx->req.scsi_req = scsi_req;
753 : 3506 : break;
754 : 3641 : case ISCSI_OP_TASK:
755 : 3641 : task_req = (struct iscsi_bhs_task_req *)&req_pdu->bhs;
756 : 3641 : io_ctx->req.task_req = task_req;
757 : 3641 : break;
758 : 3557 : case ISCSI_OP_TEXT:
759 : 3557 : text_req = (struct iscsi_bhs_text_req *)&req_pdu->bhs;
760 : 3557 : io_ctx->req.text_req = text_req;
761 : 3557 : break;
762 : 3539 : case ISCSI_OP_SCSI_DATAOUT:
763 : 3539 : data_out_req = (struct iscsi_bhs_data_out *)&req_pdu->bhs;
764 : 3539 : io_ctx->req.data_out_req = data_out_req;
765 : 3539 : break;
766 : 3506 : case ISCSI_OP_SNACK:
767 : 3506 : snack_req = (struct iscsi_bhs_snack_req *)&req_pdu->bhs;
768 : 3506 : io_ctx->req.snack_req = snack_req;
769 : 3506 : break;
770 : 194714 : default:
771 : 194714 : bhs = (struct iscsi_bhs *)&req_pdu->bhs;
772 : 194714 : io_ctx->req.bhs = bhs;
773 : 194714 : g_is_valid_opcode = false;
774 : 194714 : break;
775 : : }
776 : :
777 : 216038 : prep_iscsi_pdu_bhs_opcode_cmd(dev_ctx, io_ctx);
778 : 216038 : io_ctx->req.bhs->opcode = opcode;
779 : 216038 : req_pdu->bhs.opcode = opcode;
780 : 216038 : req_pdu->bhs.immediate = 1;
781 : 216038 : req_pdu->bhs.reserved = 0;
782 : 216038 : req_pdu->bhs_valid_bytes = ISCSI_BHS_LEN;
783 : 216038 : req_pdu->bhs.total_ahs_len = 0;
784 : 216038 : req_pdu->bhs.stat_sn = 0;
785 : 216038 : DSET24(req_pdu->bhs.data_segment_len, 0);
786 : :
787 [ + + ]: 216038 : if (opcode <= ISCSI_OP_TEXT) {
788 : 14279 : rand_seed = time(NULL);
789 : 14279 : is_p99 = rand_r(&rand_seed) % 100 == 0 ? true : false;
790 [ + + ]: 14279 : if (!is_p99) { /* Remaining 1% */
791 [ + + + + : 13646 : switch (opcode) {
- ]
792 : 3423 : case ISCSI_OP_NOPOUT:
793 [ + - ]: 3423 : if (req_pdu->bhs.immediate) {
794 : 3423 : io_ctx->req.nop_out_req->cmd_sn = dev_ctx->current_cmd_sn;
795 : : } else {
796 : 0 : io_ctx->req.nop_out_req->cmd_sn = dev_ctx->current_cmd_sn++;
797 : : }
798 : 3423 : break;
799 : 3355 : case ISCSI_OP_SCSI:
800 [ + - ]: 3355 : if (req_pdu->bhs.immediate) {
801 : 3355 : io_ctx->req.scsi_req->cmd_sn = dev_ctx->current_cmd_sn;
802 : : } else {
803 : 0 : io_ctx->req.scsi_req->cmd_sn = dev_ctx->current_cmd_sn++;
804 : : }
805 : 3355 : break;
806 : 3479 : case ISCSI_OP_TASK:
807 [ + - ]: 3479 : if (req_pdu->bhs.immediate) {
808 : 3479 : io_ctx->req.task_req->cmd_sn = dev_ctx->current_cmd_sn;
809 : : } else {
810 : 0 : io_ctx->req.task_req->cmd_sn = dev_ctx->current_cmd_sn++;
811 : : }
812 : 3479 : break;
813 : 3389 : case ISCSI_OP_TEXT:
814 [ + - ]: 3389 : if (req_pdu->bhs.immediate) {
815 : 3389 : io_ctx->req.text_req->cmd_sn = dev_ctx->current_cmd_sn;
816 : : } else {
817 : 0 : io_ctx->req.text_req->cmd_sn = dev_ctx->current_cmd_sn++;
818 : : }
819 : 3389 : break;
820 : 0 : default:
821 : 0 : break;
822 : : }
823 : 0 : }
824 : : }
825 : :
826 [ + + ]: 216038 : if (opcode == ISCSI_OP_SCSI) {
827 : : /* avoid ((R_bit != 0) && (W_bit != 0)) is true */
828 : 3506 : io_ctx->req.scsi_req->read_bit = 0;
829 : 3506 : io_ctx->req.scsi_req->write_bit = 0;
830 : : }
831 : :
832 [ + + ]: 216038 : if (opcode == ISCSI_OP_TEXT) {
833 : : /* avoid: (F_bit && C_bit) is true */
834 : 3557 : io_ctx->req.text_req->flags = 0;
835 : : /* avoid: correct itt is not equal to the current itt */
836 : 3557 : io_ctx->req.text_req->itt = 0;
837 : : }
838 : :
839 [ - + - + ]: 216038 : fprintf(stderr, "Dumping this request bhs contents now.\n");
840 : 216038 : print_req_obj(dev_ctx, io_ctx);
841 : :
842 : 216038 : check_successful_op(dev_ctx, io_ctx);
843 : 216038 : dev_ctx->num_sent_pdus++;
844 : :
845 : 216038 : iscsi_conn_write_pdu(dev_ctx->conn, req_pdu,
846 : : iscsi_conn_pdu_generic_complete, NULL);
847 : : }
848 : : /* submit requests end */
849 : :
850 : : static int
851 : 223284 : poll_dev(void *ctx)
852 : : {
853 : 223284 : struct fuzz_iscsi_dev_ctx *dev_ctx = ctx;
854 : 223284 : struct spdk_iscsi_conn *conn = dev_ctx->conn;
855 : : uint64_t current_ticks;
856 : : struct spdk_iscsi_pdu *pdu, *tmp;
857 : :
858 : 223284 : current_ticks = spdk_get_ticks();
859 [ + + ]: 223284 : if (current_ticks > g_runtime_ticks) {
860 : 2 : g_run = false;
861 : : }
862 : :
863 [ - + + + ]: 223284 : if (!g_run) {
864 : : /* Logout PDU */
865 : 2 : fuzz_iscsi_send_logout_request(dev_ctx);
866 : 2 : fuzz_iscsi_handle_incoming_pdus(conn);
867 : :
868 [ + + ]: 216042 : TAILQ_FOREACH_SAFE(pdu, &g_get_pdu_list, tailq, tmp) {
869 [ + + ]: 216040 : TAILQ_REMOVE(&g_get_pdu_list, pdu, tailq);
870 : 216040 : iscsi_put_pdu(pdu);
871 : : }
872 : :
873 : 2 : spdk_sock_close(&conn->sock);
874 : :
875 [ - + ]: 2 : TAILQ_FOREACH_SAFE(pdu, &conn->write_pdu_list, tailq, tmp) {
876 [ # # ]: 0 : TAILQ_REMOVE(&conn->write_pdu_list, pdu, tailq);
877 : 0 : iscsi_put_pdu(pdu);
878 : : }
879 : :
880 : 2 : free(conn);
881 : :
882 : 2 : spdk_poller_unregister(&dev_ctx->poller);
883 : 2 : __sync_sub_and_fetch(&g_num_active_threads, 1);
884 : :
885 : 2 : return -1;
886 : : }
887 : :
888 [ - + - + ]: 223282 : if (conn->is_logged_out) {
889 : 0 : spdk_sock_close(&conn->sock);
890 : 0 : iscsi_fuzz_conn_reset(conn, &dev_ctx->sess);
891 : 0 : iscsi_fuzz_sock_connect(conn);
892 : 0 : usleep(1000);
893 : :
894 : : /* Login PDU */
895 : 0 : fuzz_iscsi_send_login_request(dev_ctx, SESSION_TYPE_NORMAL);
896 [ + + ]: 223282 : } else if (conn->full_feature == 1) {
897 : 223194 : dev_submit_requests(dev_ctx);
898 : : }
899 : :
900 : 223282 : spdk_sock_flush(conn->sock);
901 : :
902 : 223282 : fuzz_iscsi_handle_incoming_pdus(conn);
903 : :
904 : 223282 : return 0;
905 : : }
906 : :
907 : : static void
908 : 2 : start_io(void *ctx)
909 : : {
910 : 2 : struct fuzz_iscsi_dev_ctx *dev_ctx = ctx;
911 : :
912 : 2 : dev_ctx->sess.ExpCmdSN = 0;
913 : 2 : dev_ctx->sess.MaxCmdSN = 64;
914 : 2 : dev_ctx->sess.MaxBurstLength = SPDK_ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH;
915 : 2 : dev_ctx->sess.MaxOutstandingR2T = 1;
916 : 2 : dev_ctx->sess.tag = 1;
917 : 2 : dev_ctx->sess.tsih = 256;
918 : :
919 : 2 : dev_ctx->conn = calloc(1, sizeof(*dev_ctx->conn));
920 [ - + ]: 2 : assert(dev_ctx->conn != NULL);
921 : 2 : TAILQ_INIT(&dev_ctx->conn->write_pdu_list);
922 : :
923 : 2 : iscsi_fuzz_conn_reset(dev_ctx->conn, &dev_ctx->sess);
924 : 2 : iscsi_fuzz_sock_connect(dev_ctx->conn);
925 : 2 : usleep(1000);
926 : :
927 : : /* Login PDU */
928 : 2 : fuzz_iscsi_send_login_request(dev_ctx, SESSION_TYPE_NORMAL);
929 : :
930 [ - + ]: 2 : if (g_random_seed) {
931 : 0 : dev_ctx->random_seed = g_random_seed;
932 : : } else {
933 : 2 : dev_ctx->random_seed = spdk_get_ticks();
934 : : }
935 : :
936 : 2 : dev_ctx->poller = SPDK_POLLER_REGISTER(poll_dev, dev_ctx, 0);
937 [ - + ]: 2 : if (dev_ctx->poller == NULL) {
938 : 0 : return;
939 : : }
940 : : }
941 : :
942 : : static int
943 : 60 : check_app_completion(void *ctx)
944 : : {
945 [ + + ]: 60 : if (g_num_active_threads == 0) {
946 : 2 : spdk_poller_unregister(&g_app_completion_poller);
947 [ - + ]: 2 : printf("Fuzzing completed. Shutting down the fuzz application.\n\n");
948 : 2 : cleanup();
949 : 2 : spdk_app_stop(0);
950 : : }
951 : 60 : return 0;
952 : : }
953 : :
954 : : static void
955 : 2 : begin_iscsi_fuzz(void *ctx)
956 : : {
957 : : struct fuzz_iscsi_dev_ctx *dev_ctx;
958 : : int rc;
959 : :
960 : 2 : g_runtime_ticks = spdk_get_ticks() + g_runtime * spdk_get_ticks_hz();
961 : :
962 : 2 : g_valid_buffer = spdk_malloc(0x1000, 0x200, NULL, SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_SHARE);
963 [ - + ]: 2 : if (g_valid_buffer == NULL) {
964 [ # # # # ]: 0 : fprintf(stderr, "Failed to allocate a valid buffer for random PDUs\n");
965 : 0 : goto out;
966 : : }
967 : :
968 : 2 : rc = fuzz_iscsi_dev_init();
969 [ - + ]: 2 : if (rc) {
970 [ # # # # ]: 0 : fprintf(stderr, "fuzz_iscsi_dev_init() failed.\n");
971 : 0 : goto out;
972 : : }
973 : :
974 [ + + ]: 4 : TAILQ_FOREACH(dev_ctx, &g_dev_list, link) {
975 [ - + ]: 2 : assert(dev_ctx->thread != NULL);
976 : 2 : spdk_thread_send_msg(dev_ctx->thread, start_io, dev_ctx);
977 : 2 : __sync_add_and_fetch(&g_num_active_threads, 1);
978 : : }
979 : :
980 : 2 : g_app_completion_poller = SPDK_POLLER_REGISTER(check_app_completion, NULL, 1000000);
981 [ - + ]: 2 : if (g_app_completion_poller == NULL) {
982 [ # # # # ]: 0 : fprintf(stderr, "Failed to register a poller for test completion checking.\n");
983 : 0 : goto out;
984 : : }
985 : :
986 : 2 : return;
987 : 0 : out:
988 : 0 : cleanup();
989 : 0 : spdk_app_stop(0);
990 : : }
991 : :
992 : : static void
993 : 0 : iscsi_fuzz_usage(void)
994 : : {
995 [ # # # # ]: 0 : fprintf(stderr, " -T <path> iSCSI Target IP address.\n");
996 [ # # # # ]: 0 : fprintf(stderr, " -S <integer> Seed value for test.\n");
997 [ # # # # ]: 0 : fprintf(stderr,
998 : : " -t <integer> Time in seconds to run the fuzz test. Only valid if -j is not specified.\n");
999 : 0 : }
1000 : :
1001 : : static int
1002 : 4 : iscsi_fuzz_parse(int ch, char *arg)
1003 : : {
1004 : : int64_t error_test;
1005 : :
1006 [ + - + - ]: 4 : switch (ch) {
1007 : 2 : case 'T':
1008 : 2 : g_tgt_ip = optarg;
1009 : 2 : break;
1010 : 0 : case 'S':
1011 : 0 : error_test = spdk_strtol(arg, 10);
1012 [ # # ]: 0 : if (error_test < 0) {
1013 [ # # # # ]: 0 : fprintf(stderr, "Invalid value supplied for the random seed.\n");
1014 : 0 : return -1;
1015 : : } else {
1016 : 0 : g_random_seed = error_test;
1017 : : }
1018 : 0 : break;
1019 : 2 : case 't':
1020 : 2 : g_runtime = spdk_strtol(optarg, 10);
1021 [ + - - + ]: 2 : if (g_runtime <= 0 || g_runtime > MAX_RUNTIME_S) {
1022 [ # # # # ]: 0 : fprintf(stderr, "You must supply a positive runtime value less than %d.\n", MAX_RUNTIME_S);
1023 : 0 : return -1;
1024 : : }
1025 : 2 : break;
1026 : 0 : case '?':
1027 : : default:
1028 : 0 : iscsi_fuzz_usage();
1029 : 0 : return -EINVAL;
1030 : : }
1031 : :
1032 : 4 : return 0;
1033 : : }
1034 : :
1035 : : int
1036 : 2 : main(int argc, char **argv)
1037 : : {
1038 : 2 : struct spdk_app_opts opts = {};
1039 : : int rc;
1040 : :
1041 : 2 : g_runtime = DEFAULT_RUNTIME;
1042 : 2 : srand((unsigned)time(0));
1043 : :
1044 : 2 : TAILQ_INIT(&g_get_pdu_list);
1045 : :
1046 : 2 : spdk_app_opts_init(&opts, sizeof(opts));
1047 : 2 : opts.name = "iscsi_fuzz";
1048 : 2 : opts.rpc_addr = NULL;
1049 : :
1050 : 2 : if ((rc = spdk_app_parse_args(argc, argv, &opts, "T:S:t:", NULL, iscsi_fuzz_parse,
1051 [ - + ]: 2 : iscsi_fuzz_usage) != SPDK_APP_PARSE_ARGS_SUCCESS)) {
1052 : 0 : return rc;
1053 : : }
1054 : :
1055 : 2 : rc = spdk_app_start(&opts, begin_iscsi_fuzz, NULL);
1056 : :
1057 : 2 : spdk_app_fini();
1058 : 2 : return rc;
1059 : : }
|