1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
|
/*-
* BSD LICENSE
*
* Copyright (c) Intel Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "spdk/stdinc.h"
#include "spdk_cunit.h"
#include "scsi/task.c"
#include "scsi/lun.c"
#include "spdk_internal/mock.h"
/* These unit tests aren't multithreads, but we need to allocate threads since
* the lun.c code will register pollers.
*/
#include "common/lib/ut_multithread.c"
/* Unit test bdev mockup */
struct spdk_bdev {
int x;
};
SPDK_LOG_REGISTER_COMPONENT("scsi", SPDK_LOG_SCSI)
struct spdk_scsi_globals g_spdk_scsi;
static bool g_lun_execute_fail = false;
static int g_lun_execute_status = SPDK_SCSI_TASK_PENDING;
static uint32_t g_task_count = 0;
struct spdk_trace_histories *g_trace_histories;
DEFINE_STUB_V(_spdk_trace_record,
(uint64_t tsc, uint16_t tpoint_id, uint16_t poller_id,
uint32_t size, uint64_t object_id, uint64_t arg1));
static void
spdk_lun_ut_cpl_task(struct spdk_scsi_task *task)
{
SPDK_CU_ASSERT_FATAL(g_task_count > 0);
g_task_count--;
}
static void
spdk_lun_ut_free_task(struct spdk_scsi_task *task)
{
}
static void
ut_init_task(struct spdk_scsi_task *task)
{
memset(task, 0, sizeof(*task));
spdk_scsi_task_construct(task, spdk_lun_ut_cpl_task,
spdk_lun_ut_free_task);
g_task_count++;
}
void
spdk_bdev_free_io(struct spdk_bdev_io *bdev_io)
{
CU_ASSERT(0);
}
DEFINE_STUB(spdk_bdev_open, int,
(struct spdk_bdev *bdev, bool write, spdk_bdev_remove_cb_t remove_cb,
void *remove_ctx, struct spdk_bdev_desc **desc),
0);
DEFINE_STUB_V(spdk_bdev_close, (struct spdk_bdev_desc *desc));
DEFINE_STUB(spdk_bdev_get_name, const char *,
(const struct spdk_bdev *bdev), "test");
DEFINE_STUB_V(spdk_scsi_dev_queue_mgmt_task,
(struct spdk_scsi_dev *dev, struct spdk_scsi_task *task));
DEFINE_STUB_V(spdk_scsi_dev_delete_lun,
(struct spdk_scsi_dev *dev, struct spdk_scsi_lun *lun));
DEFINE_STUB(scsi_pr_check, int, (struct spdk_scsi_task *task), 0);
DEFINE_STUB(scsi2_reserve_check, int, (struct spdk_scsi_task *task), 0);
void
bdev_scsi_reset(struct spdk_scsi_task *task)
{
task->status = SPDK_SCSI_STATUS_GOOD;
task->response = SPDK_SCSI_TASK_MGMT_RESP_SUCCESS;
scsi_lun_complete_reset_task(task->lun, task);
}
int
bdev_scsi_execute(struct spdk_scsi_task *task)
{
if (g_lun_execute_fail) {
return -EINVAL;
} else {
task->status = SPDK_SCSI_STATUS_GOOD;
if (g_lun_execute_status == SPDK_SCSI_TASK_PENDING) {
return g_lun_execute_status;
} else if (g_lun_execute_status == SPDK_SCSI_TASK_COMPLETE) {
return g_lun_execute_status;
} else {
return 0;
}
}
}
DEFINE_STUB(spdk_bdev_get_io_channel, struct spdk_io_channel *,
(struct spdk_bdev_desc *desc), NULL);
static struct spdk_scsi_lun *lun_construct(void)
{
struct spdk_scsi_lun *lun;
struct spdk_bdev bdev;
lun = scsi_lun_construct(&bdev, NULL, NULL);
SPDK_CU_ASSERT_FATAL(lun != NULL);
return lun;
}
static void
lun_destruct(struct spdk_scsi_lun *lun)
{
/* LUN will defer its removal if there are any unfinished tasks */
SPDK_CU_ASSERT_FATAL(TAILQ_EMPTY(&lun->tasks));
scsi_lun_destruct(lun);
}
static void
lun_task_mgmt_execute_abort_task_not_supported(void)
{
struct spdk_scsi_lun *lun;
struct spdk_scsi_task task = { 0 };
struct spdk_scsi_task mgmt_task = { 0 };
struct spdk_scsi_port initiator_port = { 0 };
struct spdk_scsi_dev dev = { 0 };
uint8_t cdb[6] = { 0 };
lun = lun_construct();
lun->dev = &dev;
ut_init_task(&mgmt_task);
mgmt_task.lun = lun;
mgmt_task.initiator_port = &initiator_port;
mgmt_task.function = SPDK_SCSI_TASK_FUNC_ABORT_TASK;
/* Params to add regular task to the lun->tasks */
ut_init_task(&task);
task.lun = lun;
task.cdb = cdb;
scsi_lun_execute_task(lun, &task);
/* task should now be on the tasks list */
CU_ASSERT(!TAILQ_EMPTY(&lun->tasks));
scsi_lun_execute_mgmt_task(lun, &mgmt_task);
/* task abort is not supported */
CU_ASSERT(mgmt_task.response == SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED);
/* task is still on the tasks list */
CU_ASSERT_EQUAL(g_task_count, 1);
scsi_lun_complete_task(lun, &task);
CU_ASSERT_EQUAL(g_task_count, 0);
lun_destruct(lun);
}
static void
lun_task_mgmt_execute_abort_task_all_not_supported(void)
{
struct spdk_scsi_lun *lun;
struct spdk_scsi_task task = { 0 };
struct spdk_scsi_task mgmt_task = { 0 };
struct spdk_scsi_port initiator_port = { 0 };
struct spdk_scsi_dev dev = { 0 };
uint8_t cdb[6] = { 0 };
lun = lun_construct();
lun->dev = &dev;
ut_init_task(&mgmt_task);
mgmt_task.lun = lun;
mgmt_task.initiator_port = &initiator_port;
mgmt_task.function = SPDK_SCSI_TASK_FUNC_ABORT_TASK_SET;
/* Params to add regular task to the lun->tasks */
ut_init_task(&task);
task.initiator_port = &initiator_port;
task.lun = lun;
task.cdb = cdb;
scsi_lun_execute_task(lun, &task);
/* task should now be on the tasks list */
CU_ASSERT(!TAILQ_EMPTY(&lun->tasks));
scsi_lun_execute_mgmt_task(lun, &mgmt_task);
/* task abort is not supported */
CU_ASSERT(mgmt_task.response == SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED);
/* task is still on the tasks list */
CU_ASSERT_EQUAL(g_task_count, 1);
scsi_lun_complete_task(lun, &task);
CU_ASSERT_EQUAL(g_task_count, 0);
lun_destruct(lun);
}
static void
lun_task_mgmt_execute_lun_reset(void)
{
struct spdk_scsi_lun *lun;
struct spdk_scsi_task mgmt_task = { 0 };
struct spdk_scsi_dev dev = { 0 };
lun = lun_construct();
lun->dev = &dev;
ut_init_task(&mgmt_task);
mgmt_task.lun = lun;
mgmt_task.function = SPDK_SCSI_TASK_FUNC_LUN_RESET;
scsi_lun_execute_mgmt_task(lun, &mgmt_task);
/* Returns success */
CU_ASSERT_EQUAL(mgmt_task.status, SPDK_SCSI_STATUS_GOOD);
CU_ASSERT_EQUAL(mgmt_task.response, SPDK_SCSI_TASK_MGMT_RESP_SUCCESS);
lun_destruct(lun);
CU_ASSERT_EQUAL(g_task_count, 0);
}
static void
lun_task_mgmt_execute_invalid_case(void)
{
struct spdk_scsi_lun *lun;
struct spdk_scsi_task mgmt_task = { 0 };
struct spdk_scsi_dev dev = { 0 };
lun = lun_construct();
lun->dev = &dev;
ut_init_task(&mgmt_task);
mgmt_task.function = 5;
/* Pass an invalid value to the switch statement */
scsi_lun_execute_mgmt_task(lun, &mgmt_task);
/* function code is invalid */
CU_ASSERT_EQUAL(mgmt_task.response, SPDK_SCSI_TASK_MGMT_RESP_REJECT_FUNC_NOT_SUPPORTED);
lun_destruct(lun);
CU_ASSERT_EQUAL(g_task_count, 0);
}
static void
lun_append_task_null_lun_task_cdb_spc_inquiry(void)
{
struct spdk_scsi_task task = { 0 };
uint8_t cdb[6] = { 0 };
ut_init_task(&task);
task.cdb = cdb;
task.cdb[0] = SPDK_SPC_INQUIRY;
/* alloc_len >= 4096 */
task.cdb[3] = 0xFF;
task.cdb[4] = 0xFF;
task.lun = NULL;
spdk_scsi_task_process_null_lun(&task);
CU_ASSERT_EQUAL(task.status, SPDK_SCSI_STATUS_GOOD);
spdk_scsi_task_put(&task);
/* spdk_scsi_task_process_null_lun() does not call cpl_fn */
CU_ASSERT_EQUAL(g_task_count, 1);
g_task_count = 0;
}
static void
lun_append_task_null_lun_alloc_len_lt_4096(void)
{
struct spdk_scsi_task task = { 0 };
uint8_t cdb[6] = { 0 };
ut_init_task(&task);
task.cdb = cdb;
task.cdb[0] = SPDK_SPC_INQUIRY;
/* alloc_len < 4096 */
task.cdb[3] = 0;
task.cdb[4] = 0;
/* alloc_len is set to a minimal value of 4096
* Hence, buf of size 4096 is allocated */
spdk_scsi_task_process_null_lun(&task);
CU_ASSERT_EQUAL(task.status, SPDK_SCSI_STATUS_GOOD);
spdk_scsi_task_put(&task);
/* spdk_scsi_task_process_null_lun() does not call cpl_fn */
CU_ASSERT_EQUAL(g_task_count, 1);
g_task_count = 0;
}
static void
lun_append_task_null_lun_not_supported(void)
{
struct spdk_scsi_task task = { 0 };
uint8_t cdb[6] = { 0 };
ut_init_task(&task);
task.cdb = cdb;
task.lun = NULL;
spdk_scsi_task_process_null_lun(&task);
CU_ASSERT_EQUAL(task.status, SPDK_SCSI_STATUS_CHECK_CONDITION);
/* LUN not supported; task's data transferred should be 0 */
CU_ASSERT_EQUAL(task.data_transferred, 0);
/* spdk_scsi_task_process_null_lun() does not call cpl_fn */
CU_ASSERT_EQUAL(g_task_count, 1);
g_task_count = 0;
}
static void
lun_execute_scsi_task_pending(void)
{
struct spdk_scsi_lun *lun;
struct spdk_scsi_task task = { 0 };
struct spdk_scsi_dev dev = { 0 };
lun = lun_construct();
ut_init_task(&task);
task.lun = lun;
lun->dev = &dev;
g_lun_execute_fail = false;
g_lun_execute_status = SPDK_SCSI_TASK_PENDING;
/* the tasks list should still be empty since it has not been
executed yet
*/
CU_ASSERT(TAILQ_EMPTY(&lun->tasks));
scsi_lun_execute_task(lun, &task);
/* Assert the task has been successfully added to the tasks queue */
CU_ASSERT(!TAILQ_EMPTY(&lun->tasks));
/* task is still on the tasks list */
CU_ASSERT_EQUAL(g_task_count, 1);
/* Need to complete task so LUN might be removed right now */
scsi_lun_complete_task(lun, &task);
CU_ASSERT_EQUAL(g_task_count, 0);
lun_destruct(lun);
}
static void
lun_execute_scsi_task_complete(void)
{
struct spdk_scsi_lun *lun;
struct spdk_scsi_task task = { 0 };
struct spdk_scsi_dev dev = { 0 };
lun = lun_construct();
ut_init_task(&task);
task.lun = lun;
lun->dev = &dev;
g_lun_execute_fail = false;
g_lun_execute_status = SPDK_SCSI_TASK_COMPLETE;
/* the tasks list should still be empty since it has not been
executed yet
*/
CU_ASSERT(TAILQ_EMPTY(&lun->tasks));
scsi_lun_execute_task(lun, &task);
/* Assert the task has not been added to the tasks queue */
CU_ASSERT(TAILQ_EMPTY(&lun->tasks));
lun_destruct(lun);
CU_ASSERT_EQUAL(g_task_count, 0);
}
static void
lun_destruct_success(void)
{
struct spdk_scsi_lun *lun;
lun = lun_construct();
scsi_lun_destruct(lun);
CU_ASSERT_EQUAL(g_task_count, 0);
}
static void
lun_construct_null_ctx(void)
{
struct spdk_scsi_lun *lun;
lun = scsi_lun_construct(NULL, NULL, NULL);
/* lun should be NULL since we passed NULL for the ctx pointer. */
CU_ASSERT(lun == NULL);
CU_ASSERT_EQUAL(g_task_count, 0);
}
static void
lun_construct_success(void)
{
struct spdk_scsi_lun *lun = lun_construct();
lun_destruct(lun);
CU_ASSERT_EQUAL(g_task_count, 0);
}
static void
lun_reset_task_wait_scsi_task_complete(void)
{
struct spdk_scsi_lun *lun;
struct spdk_scsi_task task = { 0 };
struct spdk_scsi_task mgmt_task = { 0 };
struct spdk_scsi_dev dev = { 0 };
lun = lun_construct();
lun->dev = &dev;
ut_init_task(&task);
task.lun = lun;
g_lun_execute_fail = false;
g_lun_execute_status = SPDK_SCSI_TASK_PENDING;
ut_init_task(&mgmt_task);
mgmt_task.lun = lun;
mgmt_task.function = SPDK_SCSI_TASK_FUNC_LUN_RESET;
/* Execute the task but it is still in the task list. */
scsi_lun_execute_task(lun, &task);
CU_ASSERT(TAILQ_EMPTY(&lun->pending_tasks));
CU_ASSERT(!TAILQ_EMPTY(&lun->tasks));
/* Execute the reset task */
scsi_lun_execute_mgmt_task(lun, &mgmt_task);
/* The reset task should be on the submitted mgmt task list and
* a poller is created because the task prior to the reset task is pending.
*/
CU_ASSERT(!TAILQ_EMPTY(&lun->mgmt_tasks));
CU_ASSERT(lun->reset_poller != NULL);
/* Execute the poller to check if the task prior to the reset task complete. */
scsi_lun_reset_check_outstanding_tasks(&mgmt_task);
CU_ASSERT(!TAILQ_EMPTY(&lun->mgmt_tasks));
CU_ASSERT(lun->reset_poller != NULL);
/* Complete the task. */
scsi_lun_complete_task(lun, &task);
CU_ASSERT(TAILQ_EMPTY(&lun->tasks));
/* Execute the poller to check if the task prior to the reset task complete. */
scsi_lun_reset_check_outstanding_tasks(&mgmt_task);
CU_ASSERT(TAILQ_EMPTY(&lun->mgmt_tasks));
CU_ASSERT(lun->reset_poller == NULL);
CU_ASSERT_EQUAL(mgmt_task.status, SPDK_SCSI_STATUS_GOOD);
CU_ASSERT_EQUAL(mgmt_task.response, SPDK_SCSI_TASK_MGMT_RESP_SUCCESS);
lun_destruct(lun);
CU_ASSERT_EQUAL(g_task_count, 0);
}
static void
lun_reset_task_suspend_scsi_task(void)
{
struct spdk_scsi_lun *lun;
struct spdk_scsi_task task = { 0 };
struct spdk_scsi_task mgmt_task = { 0 };
struct spdk_scsi_dev dev = { 0 };
lun = lun_construct();
lun->dev = &dev;
ut_init_task(&task);
task.lun = lun;
g_lun_execute_fail = false;
g_lun_execute_status = SPDK_SCSI_TASK_COMPLETE;
ut_init_task(&mgmt_task);
mgmt_task.lun = lun;
mgmt_task.function = SPDK_SCSI_TASK_FUNC_LUN_RESET;
/* Append a reset task to the pending mgmt task list. */
scsi_lun_append_mgmt_task(lun, &mgmt_task);
CU_ASSERT(!TAILQ_EMPTY(&lun->pending_mgmt_tasks));
/* Execute the task but it is on the pending task list. */
scsi_lun_execute_task(lun, &task);
CU_ASSERT(!TAILQ_EMPTY(&lun->pending_tasks));
/* Execute the reset task. The task will be executed then. */
_scsi_lun_execute_mgmt_task(lun);
CU_ASSERT(TAILQ_EMPTY(&lun->mgmt_tasks));
CU_ASSERT(lun->reset_poller == NULL);
CU_ASSERT_EQUAL(mgmt_task.status, SPDK_SCSI_STATUS_GOOD);
CU_ASSERT_EQUAL(mgmt_task.response, SPDK_SCSI_TASK_MGMT_RESP_SUCCESS);
CU_ASSERT(TAILQ_EMPTY(&lun->pending_tasks));
CU_ASSERT(TAILQ_EMPTY(&lun->tasks));
lun_destruct(lun);
CU_ASSERT_EQUAL(g_task_count, 0);
}
static void
lun_check_pending_tasks_only_for_specific_initiator(void)
{
struct spdk_bdev bdev = {};
struct spdk_scsi_lun *lun;
struct spdk_scsi_task task1 = {};
struct spdk_scsi_task task2 = {};
struct spdk_scsi_port initiator_port1 = {};
struct spdk_scsi_port initiator_port2 = {};
struct spdk_scsi_port initiator_port3 = {};
lun = scsi_lun_construct(&bdev, NULL, NULL);
task1.initiator_port = &initiator_port1;
task2.initiator_port = &initiator_port2;
TAILQ_INSERT_TAIL(&lun->tasks, &task1, scsi_link);
TAILQ_INSERT_TAIL(&lun->tasks, &task2, scsi_link);
CU_ASSERT(scsi_lun_has_outstanding_tasks(lun) == true);
CU_ASSERT(_scsi_lun_has_pending_tasks(lun) == false);
CU_ASSERT(scsi_lun_has_pending_tasks(lun, NULL) == true);
CU_ASSERT(scsi_lun_has_pending_tasks(lun, &initiator_port1) == true);
CU_ASSERT(scsi_lun_has_pending_tasks(lun, &initiator_port2) == true);
CU_ASSERT(scsi_lun_has_pending_tasks(lun, &initiator_port3) == false);
TAILQ_REMOVE(&lun->tasks, &task1, scsi_link);
TAILQ_REMOVE(&lun->tasks, &task2, scsi_link);
CU_ASSERT(_scsi_lun_has_pending_tasks(lun) == false);
CU_ASSERT(scsi_lun_has_pending_tasks(lun, NULL) == false);
TAILQ_INSERT_TAIL(&lun->pending_tasks, &task1, scsi_link);
TAILQ_INSERT_TAIL(&lun->pending_tasks, &task2, scsi_link);
CU_ASSERT(scsi_lun_has_outstanding_tasks(lun) == false);
CU_ASSERT(_scsi_lun_has_pending_tasks(lun) == true);
CU_ASSERT(scsi_lun_has_pending_tasks(lun, NULL) == true);
CU_ASSERT(scsi_lun_has_pending_tasks(lun, &initiator_port1) == true);
CU_ASSERT(scsi_lun_has_pending_tasks(lun, &initiator_port2) == true);
CU_ASSERT(scsi_lun_has_pending_tasks(lun, &initiator_port3) == false);
TAILQ_REMOVE(&lun->pending_tasks, &task1, scsi_link);
TAILQ_REMOVE(&lun->pending_tasks, &task2, scsi_link);
CU_ASSERT(_scsi_lun_has_pending_tasks(lun) == false);
CU_ASSERT(scsi_lun_has_pending_tasks(lun, NULL) == false);
TAILQ_INSERT_TAIL(&lun->mgmt_tasks, &task1, scsi_link);
TAILQ_INSERT_TAIL(&lun->mgmt_tasks, &task2, scsi_link);
CU_ASSERT(scsi_lun_has_outstanding_mgmt_tasks(lun) == true);
CU_ASSERT(_scsi_lun_has_pending_mgmt_tasks(lun) == false);
CU_ASSERT(scsi_lun_has_pending_mgmt_tasks(lun, NULL) == true);
CU_ASSERT(scsi_lun_has_pending_mgmt_tasks(lun, &initiator_port1) == true);
CU_ASSERT(scsi_lun_has_pending_mgmt_tasks(lun, &initiator_port2) == true);
CU_ASSERT(scsi_lun_has_pending_mgmt_tasks(lun, &initiator_port3) == false);
TAILQ_REMOVE(&lun->mgmt_tasks, &task1, scsi_link);
TAILQ_REMOVE(&lun->mgmt_tasks, &task2, scsi_link);
CU_ASSERT(_scsi_lun_has_pending_mgmt_tasks(lun) == false);
CU_ASSERT(scsi_lun_has_pending_mgmt_tasks(lun, NULL) == false);
TAILQ_INSERT_TAIL(&lun->pending_mgmt_tasks, &task1, scsi_link);
TAILQ_INSERT_TAIL(&lun->pending_mgmt_tasks, &task2, scsi_link);
CU_ASSERT(_scsi_lun_has_pending_mgmt_tasks(lun) == true);
CU_ASSERT(scsi_lun_has_pending_mgmt_tasks(lun, NULL) == true);
CU_ASSERT(scsi_lun_has_pending_mgmt_tasks(lun, &initiator_port1) == true);
CU_ASSERT(scsi_lun_has_pending_mgmt_tasks(lun, &initiator_port2) == true);
CU_ASSERT(scsi_lun_has_pending_mgmt_tasks(lun, &initiator_port3) == false);
TAILQ_REMOVE(&lun->pending_mgmt_tasks, &task1, scsi_link);
TAILQ_REMOVE(&lun->pending_mgmt_tasks, &task2, scsi_link);
CU_ASSERT(_scsi_lun_has_pending_mgmt_tasks(lun) == false);
CU_ASSERT(scsi_lun_has_pending_mgmt_tasks(lun, NULL) == false);
scsi_lun_remove(lun);
}
static void
abort_pending_mgmt_tasks_when_lun_is_removed(void)
{
struct spdk_bdev bdev = {};
struct spdk_scsi_lun *lun;
struct spdk_scsi_task task1, task2, task3;
lun = scsi_lun_construct(&bdev, NULL, NULL);
/* Normal case */
ut_init_task(&task1);
ut_init_task(&task2);
ut_init_task(&task3);
task1.lun = lun;
task2.lun = lun;
task3.lun = lun;
task1.function = SPDK_SCSI_TASK_FUNC_LUN_RESET;
task2.function = SPDK_SCSI_TASK_FUNC_LUN_RESET;
task3.function = SPDK_SCSI_TASK_FUNC_LUN_RESET;
CU_ASSERT(g_task_count == 3);
scsi_lun_append_mgmt_task(lun, &task1);
scsi_lun_append_mgmt_task(lun, &task2);
scsi_lun_append_mgmt_task(lun, &task3);
CU_ASSERT(!TAILQ_EMPTY(&lun->pending_mgmt_tasks));
_scsi_lun_execute_mgmt_task(lun);
CU_ASSERT(TAILQ_EMPTY(&lun->pending_mgmt_tasks));
CU_ASSERT(TAILQ_EMPTY(&lun->mgmt_tasks));
CU_ASSERT(g_task_count == 0);
CU_ASSERT(task1.response == SPDK_SCSI_TASK_MGMT_RESP_SUCCESS);
CU_ASSERT(task2.response == SPDK_SCSI_TASK_MGMT_RESP_SUCCESS);
CU_ASSERT(task3.response == SPDK_SCSI_TASK_MGMT_RESP_SUCCESS);
/* LUN hotplug case */
ut_init_task(&task1);
ut_init_task(&task2);
ut_init_task(&task3);
task1.function = SPDK_SCSI_TASK_FUNC_LUN_RESET;
task2.function = SPDK_SCSI_TASK_FUNC_LUN_RESET;
task3.function = SPDK_SCSI_TASK_FUNC_LUN_RESET;
CU_ASSERT(g_task_count == 3);
scsi_lun_append_mgmt_task(lun, &task1);
scsi_lun_append_mgmt_task(lun, &task2);
scsi_lun_append_mgmt_task(lun, &task3);
CU_ASSERT(!TAILQ_EMPTY(&lun->pending_mgmt_tasks));
lun->removed = true;
_scsi_lun_execute_mgmt_task(lun);
CU_ASSERT(TAILQ_EMPTY(&lun->pending_mgmt_tasks));
CU_ASSERT(TAILQ_EMPTY(&lun->mgmt_tasks));
CU_ASSERT(g_task_count == 0);
CU_ASSERT(task1.response == SPDK_SCSI_TASK_MGMT_RESP_INVALID_LUN);
CU_ASSERT(task2.response == SPDK_SCSI_TASK_MGMT_RESP_INVALID_LUN);
CU_ASSERT(task3.response == SPDK_SCSI_TASK_MGMT_RESP_INVALID_LUN);
scsi_lun_remove(lun);
}
int
main(int argc, char **argv)
{
CU_pSuite suite = NULL;
unsigned int num_failures;
CU_set_error_action(CUEA_ABORT);
CU_initialize_registry();
suite = CU_add_suite("lun_suite", NULL, NULL);
CU_ADD_TEST(suite, lun_task_mgmt_execute_abort_task_not_supported);
CU_ADD_TEST(suite, lun_task_mgmt_execute_abort_task_all_not_supported);
CU_ADD_TEST(suite, lun_task_mgmt_execute_lun_reset);
CU_ADD_TEST(suite, lun_task_mgmt_execute_invalid_case);
CU_ADD_TEST(suite, lun_append_task_null_lun_task_cdb_spc_inquiry);
CU_ADD_TEST(suite, lun_append_task_null_lun_alloc_len_lt_4096);
CU_ADD_TEST(suite, lun_append_task_null_lun_not_supported);
CU_ADD_TEST(suite, lun_execute_scsi_task_pending);
CU_ADD_TEST(suite, lun_execute_scsi_task_complete);
CU_ADD_TEST(suite, lun_destruct_success);
CU_ADD_TEST(suite, lun_construct_null_ctx);
CU_ADD_TEST(suite, lun_construct_success);
CU_ADD_TEST(suite, lun_reset_task_wait_scsi_task_complete);
CU_ADD_TEST(suite, lun_reset_task_suspend_scsi_task);
CU_ADD_TEST(suite, lun_check_pending_tasks_only_for_specific_initiator);
CU_ADD_TEST(suite, abort_pending_mgmt_tasks_when_lun_is_removed);
CU_basic_set_mode(CU_BRM_VERBOSE);
allocate_threads(1);
set_thread(0);
CU_basic_run_tests();
free_threads();
num_failures = CU_get_number_of_failures();
CU_cleanup_registry();
return num_failures;
}
|