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
|
/*-
* 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_cunit.h"
#include "nvme/nvme_poll_group.c"
#include "common/lib/test_env.c"
struct spdk_nvme_transport {
const char name[32];
TAILQ_ENTRY(spdk_nvme_transport) link;
};
struct spdk_nvme_transport t1 = {
.name = "transport1",
};
struct spdk_nvme_transport t2 = {
.name = "transport2",
};
struct spdk_nvme_transport t3 = {
.name = "transport3",
};
struct spdk_nvme_transport t4 = {
.name = "transport4",
};
int64_t g_process_completions_return_value = 0;
int g_destroy_return_value = 0;
TAILQ_HEAD(nvme_transport_list, spdk_nvme_transport) g_spdk_nvme_transports =
TAILQ_HEAD_INITIALIZER(g_spdk_nvme_transports);
static void
unit_test_disconnected_qpair_cb(struct spdk_nvme_qpair *qpair, void *poll_group_ctx)
{
}
const struct spdk_nvme_transport *
nvme_get_first_transport(void)
{
return TAILQ_FIRST(&g_spdk_nvme_transports);
}
const struct spdk_nvme_transport *
nvme_get_next_transport(const struct spdk_nvme_transport *transport)
{
return TAILQ_NEXT(transport, link);
}
int
nvme_transport_poll_group_disconnect_qpair(struct spdk_nvme_qpair *qpair)
{
struct spdk_nvme_transport_poll_group *tgroup;
struct spdk_nvme_qpair *iter_qp, *tmp_iter_qp;
tgroup = qpair->poll_group;
STAILQ_FOREACH_SAFE(iter_qp, &tgroup->connected_qpairs, poll_group_stailq, tmp_iter_qp) {
if (qpair == iter_qp) {
STAILQ_REMOVE(&tgroup->connected_qpairs, qpair, spdk_nvme_qpair, poll_group_stailq);
STAILQ_INSERT_TAIL(&tgroup->disconnected_qpairs, qpair, poll_group_stailq);
return 0;
}
}
STAILQ_FOREACH(iter_qp, &tgroup->disconnected_qpairs, poll_group_stailq) {
if (qpair == iter_qp) {
return 0;
}
}
return -EINVAL;
}
int
nvme_transport_poll_group_connect_qpair(struct spdk_nvme_qpair *qpair)
{
struct spdk_nvme_transport_poll_group *tgroup;
struct spdk_nvme_qpair *iter_qp, *tmp_iter_qp;
tgroup = qpair->poll_group;
STAILQ_FOREACH_SAFE(iter_qp, &tgroup->disconnected_qpairs, poll_group_stailq, tmp_iter_qp) {
if (qpair == iter_qp) {
STAILQ_REMOVE(&tgroup->disconnected_qpairs, qpair, spdk_nvme_qpair, poll_group_stailq);
STAILQ_INSERT_TAIL(&tgroup->connected_qpairs, qpair, poll_group_stailq);
return 0;
}
}
STAILQ_FOREACH(iter_qp, &tgroup->connected_qpairs, poll_group_stailq) {
if (qpair == iter_qp) {
return 0;
}
}
return -EINVAL;
}
struct spdk_nvme_transport_poll_group *
nvme_transport_poll_group_create(const struct spdk_nvme_transport *transport)
{
struct spdk_nvme_transport_poll_group *group = NULL;
/* TODO: separate this transport function table from the transport specific one. */
group = calloc(1, sizeof(*group));
if (group) {
group->transport = transport;
STAILQ_INIT(&group->connected_qpairs);
STAILQ_INIT(&group->disconnected_qpairs);
}
return group;
}
int
nvme_transport_poll_group_destroy(struct spdk_nvme_transport_poll_group *tgroup)
{
return g_destroy_return_value;
}
int
nvme_transport_poll_group_add(struct spdk_nvme_transport_poll_group *tgroup,
struct spdk_nvme_qpair *qpair)
{
STAILQ_INSERT_TAIL(&tgroup->connected_qpairs, qpair, poll_group_stailq);
qpair->poll_group = tgroup;
return 0;
}
int
nvme_transport_poll_group_remove(struct spdk_nvme_transport_poll_group *tgroup,
struct spdk_nvme_qpair *qpair)
{
struct spdk_nvme_qpair *iter_qp, *tmp_iter_qp;
STAILQ_FOREACH_SAFE(iter_qp, &tgroup->connected_qpairs, poll_group_stailq, tmp_iter_qp) {
if (qpair == iter_qp) {
STAILQ_REMOVE(&tgroup->connected_qpairs, qpair, spdk_nvme_qpair, poll_group_stailq);
return 0;
}
}
STAILQ_FOREACH_SAFE(iter_qp, &tgroup->disconnected_qpairs, poll_group_stailq, tmp_iter_qp) {
if (qpair == iter_qp) {
STAILQ_REMOVE(&tgroup->disconnected_qpairs, qpair, spdk_nvme_qpair, poll_group_stailq);
return 0;
}
}
return -ENODEV;
}
int64_t
nvme_transport_poll_group_process_completions(struct spdk_nvme_transport_poll_group *group,
uint32_t completions_per_qpair, spdk_nvme_disconnected_qpair_cb disconnected_qpair_cb)
{
return g_process_completions_return_value;
}
static void
test_spdk_nvme_poll_group_create(void)
{
struct spdk_nvme_poll_group *group;
/* basic case - create a poll group with no internal transport poll groups. */
group = spdk_nvme_poll_group_create(NULL);
SPDK_CU_ASSERT_FATAL(group != NULL);
CU_ASSERT(STAILQ_EMPTY(&group->tgroups));
SPDK_CU_ASSERT_FATAL(spdk_nvme_poll_group_destroy(group) == 0);
TAILQ_INSERT_TAIL(&g_spdk_nvme_transports, &t1, link);
TAILQ_INSERT_TAIL(&g_spdk_nvme_transports, &t2, link);
TAILQ_INSERT_TAIL(&g_spdk_nvme_transports, &t3, link);
/* advanced case - create a poll group with three internal poll groups. */
group = spdk_nvme_poll_group_create(NULL);
CU_ASSERT(STAILQ_EMPTY(&group->tgroups));
SPDK_CU_ASSERT_FATAL(spdk_nvme_poll_group_destroy(group) == 0);
/* Failing case - failed to allocate a poll group. */
MOCK_SET(calloc, NULL);
group = spdk_nvme_poll_group_create(NULL);
CU_ASSERT(group == NULL);
MOCK_CLEAR(calloc);
TAILQ_REMOVE(&g_spdk_nvme_transports, &t1, link);
TAILQ_REMOVE(&g_spdk_nvme_transports, &t2, link);
TAILQ_REMOVE(&g_spdk_nvme_transports, &t3, link);
}
static void
test_spdk_nvme_poll_group_add_remove(void)
{
struct spdk_nvme_poll_group *group;
struct spdk_nvme_transport_poll_group *tgroup = NULL, *tmp_tgroup, *tgroup_1 = NULL,
*tgroup_2 = NULL,
*tgroup_4 = NULL;
struct spdk_nvme_qpair *qpair;
struct spdk_nvme_qpair qpair1_1 = {0};
struct spdk_nvme_qpair qpair1_2 = {0};
struct spdk_nvme_qpair qpair2_1 = {0};
struct spdk_nvme_qpair qpair2_2 = {0};
struct spdk_nvme_qpair qpair4_1 = {0};
struct spdk_nvme_qpair qpair4_2 = {0};
int i = 0;
TAILQ_INSERT_TAIL(&g_spdk_nvme_transports, &t1, link);
TAILQ_INSERT_TAIL(&g_spdk_nvme_transports, &t2, link);
TAILQ_INSERT_TAIL(&g_spdk_nvme_transports, &t3, link);
group = spdk_nvme_poll_group_create(NULL);
SPDK_CU_ASSERT_FATAL(group != NULL);
CU_ASSERT(STAILQ_EMPTY(&group->tgroups));
/* Add qpairs to a single transport. */
qpair1_1.transport = &t1;
qpair1_1.state = NVME_QPAIR_DISCONNECTED;
qpair1_2.transport = &t1;
qpair1_2.state = NVME_QPAIR_ENABLED;
CU_ASSERT(spdk_nvme_poll_group_add(group, &qpair1_1) == 0);
CU_ASSERT(spdk_nvme_poll_group_add(group, &qpair1_2) == -EINVAL);
STAILQ_FOREACH(tmp_tgroup, &group->tgroups, link) {
if (tmp_tgroup->transport == &t1) {
tgroup = tmp_tgroup;
} else {
CU_ASSERT(STAILQ_EMPTY(&tmp_tgroup->connected_qpairs));
}
i++;
}
CU_ASSERT(i == 1);
SPDK_CU_ASSERT_FATAL(tgroup != NULL);
qpair = STAILQ_FIRST(&tgroup->connected_qpairs);
SPDK_CU_ASSERT_FATAL(qpair == &qpair1_1);
qpair = STAILQ_NEXT(qpair, poll_group_stailq);
CU_ASSERT(qpair == NULL);
/* Add qpairs to a second transport. */
qpair2_1.transport = &t2;
qpair2_2.transport = &t2;
CU_ASSERT(spdk_nvme_poll_group_add(group, &qpair2_1) == 0);
CU_ASSERT(spdk_nvme_poll_group_add(group, &qpair2_2) == 0);
qpair4_1.transport = &t4;
qpair4_2.transport = &t4;
/* Add qpairs for a transport that doesn't exist. */
CU_ASSERT(spdk_nvme_poll_group_add(group, &qpair4_1) == -ENODEV);
CU_ASSERT(spdk_nvme_poll_group_add(group, &qpair4_2) == -ENODEV);
i = 0;
STAILQ_FOREACH(tmp_tgroup, &group->tgroups, link) {
if (tmp_tgroup->transport == &t1) {
tgroup_1 = tmp_tgroup;
} else if (tmp_tgroup->transport == &t2) {
tgroup_2 = tmp_tgroup;
} else {
CU_ASSERT(STAILQ_EMPTY(&tmp_tgroup->connected_qpairs));
}
i++;
}
CU_ASSERT(i == 2);
SPDK_CU_ASSERT_FATAL(tgroup_1 != NULL);
qpair = STAILQ_FIRST(&tgroup_1->connected_qpairs);
SPDK_CU_ASSERT_FATAL(qpair == &qpair1_1);
qpair = STAILQ_NEXT(qpair, poll_group_stailq);
CU_ASSERT(qpair == NULL);
SPDK_CU_ASSERT_FATAL(tgroup_2 != NULL);
qpair = STAILQ_FIRST(&tgroup_2->connected_qpairs);
SPDK_CU_ASSERT_FATAL(qpair == &qpair2_1);
qpair = STAILQ_NEXT(qpair, poll_group_stailq);
SPDK_CU_ASSERT_FATAL(qpair == &qpair2_2);
qpair = STAILQ_NEXT(qpair, poll_group_stailq);
CU_ASSERT(qpair == NULL);
/* Try removing a qpair that belongs to a transport not in our poll group. */
CU_ASSERT(spdk_nvme_poll_group_remove(group, &qpair4_1) == -ENODEV);
TAILQ_INSERT_TAIL(&g_spdk_nvme_transports, &t4, link);
CU_ASSERT(spdk_nvme_poll_group_add(group, &qpair4_1) == 0);
CU_ASSERT(spdk_nvme_poll_group_add(group, &qpair4_2) == 0);
STAILQ_FOREACH(tmp_tgroup, &group->tgroups, link) {
if (tmp_tgroup->transport == &t1) {
tgroup_1 = tmp_tgroup;
} else if (tmp_tgroup->transport == &t2) {
tgroup_2 = tmp_tgroup;
} else if (tmp_tgroup->transport == &t4) {
tgroup_4 = tmp_tgroup;
} else {
CU_ASSERT(STAILQ_EMPTY(&tmp_tgroup->connected_qpairs));
}
}
SPDK_CU_ASSERT_FATAL(tgroup_1 != NULL);
qpair = STAILQ_FIRST(&tgroup_1->connected_qpairs);
SPDK_CU_ASSERT_FATAL(qpair == &qpair1_1);
qpair = STAILQ_NEXT(qpair, poll_group_stailq);
CU_ASSERT(qpair == NULL);
SPDK_CU_ASSERT_FATAL(tgroup_2 != NULL);
qpair = STAILQ_FIRST(&tgroup_2->connected_qpairs);
SPDK_CU_ASSERT_FATAL(qpair == &qpair2_1);
qpair = STAILQ_NEXT(qpair, poll_group_stailq);
SPDK_CU_ASSERT_FATAL(qpair == &qpair2_2);
qpair = STAILQ_NEXT(qpair, poll_group_stailq);
CU_ASSERT(qpair == NULL);
SPDK_CU_ASSERT_FATAL(tgroup_4 != NULL);
qpair = STAILQ_FIRST(&tgroup_4->connected_qpairs);
SPDK_CU_ASSERT_FATAL(qpair == &qpair4_1);
qpair = STAILQ_NEXT(qpair, poll_group_stailq);
SPDK_CU_ASSERT_FATAL(qpair == &qpair4_2);
qpair = STAILQ_NEXT(qpair, poll_group_stailq);
CU_ASSERT(qpair == NULL);
/* remove all qpairs */
CU_ASSERT(spdk_nvme_poll_group_remove(group, &qpair1_1) == 0);
CU_ASSERT(spdk_nvme_poll_group_remove(group, &qpair2_1) == 0);
CU_ASSERT(spdk_nvme_poll_group_remove(group, &qpair2_2) == 0);
CU_ASSERT(spdk_nvme_poll_group_remove(group, &qpair4_1) == 0);
CU_ASSERT(spdk_nvme_poll_group_remove(group, &qpair4_2) == 0);
/* Confirm the fourth transport group was created. */
i = 0;
STAILQ_FOREACH_SAFE(tgroup, &group->tgroups, link, tmp_tgroup) {
CU_ASSERT(STAILQ_EMPTY(&tgroup->connected_qpairs));
STAILQ_REMOVE(&group->tgroups, tgroup, spdk_nvme_transport_poll_group, link);
free(tgroup);
i++;
}
CU_ASSERT(i == 3);
SPDK_CU_ASSERT_FATAL(spdk_nvme_poll_group_destroy(group) == 0);
TAILQ_REMOVE(&g_spdk_nvme_transports, &t1, link);
TAILQ_REMOVE(&g_spdk_nvme_transports, &t2, link);
TAILQ_REMOVE(&g_spdk_nvme_transports, &t3, link);
TAILQ_REMOVE(&g_spdk_nvme_transports, &t4, link);
}
static void
test_spdk_nvme_poll_group_process_completions(void)
{
struct spdk_nvme_poll_group *group;
struct spdk_nvme_transport_poll_group *tgroup, *tmp_tgroup;
struct spdk_nvme_qpair qpair1_1 = {0};
group = spdk_nvme_poll_group_create(NULL);
SPDK_CU_ASSERT_FATAL(group != NULL);
/* If we don't have any transport poll groups, we shouldn't get any completions. */
g_process_completions_return_value = 32;
CU_ASSERT(spdk_nvme_poll_group_process_completions(group, 128,
unit_test_disconnected_qpair_cb) == 0);
SPDK_CU_ASSERT_FATAL(spdk_nvme_poll_group_destroy(group) == 0);
TAILQ_INSERT_TAIL(&g_spdk_nvme_transports, &t1, link);
TAILQ_INSERT_TAIL(&g_spdk_nvme_transports, &t2, link);
TAILQ_INSERT_TAIL(&g_spdk_nvme_transports, &t3, link);
/* try it with three transport poll groups. */
group = spdk_nvme_poll_group_create(NULL);
SPDK_CU_ASSERT_FATAL(group != NULL);
qpair1_1.state = NVME_QPAIR_DISCONNECTED;
qpair1_1.transport = &t1;
CU_ASSERT(spdk_nvme_poll_group_add(group, &qpair1_1) == 0);
qpair1_1.state = NVME_QPAIR_ENABLED;
CU_ASSERT(nvme_poll_group_connect_qpair(&qpair1_1) == 0);
CU_ASSERT(spdk_nvme_poll_group_process_completions(group, 128,
unit_test_disconnected_qpair_cb) == 32);
CU_ASSERT(spdk_nvme_poll_group_remove(group, &qpair1_1) == 0);
STAILQ_FOREACH_SAFE(tgroup, &group->tgroups, link, tmp_tgroup) {
CU_ASSERT(STAILQ_EMPTY(&tgroup->connected_qpairs));
STAILQ_REMOVE(&group->tgroups, tgroup, spdk_nvme_transport_poll_group, link);
free(tgroup);
}
SPDK_CU_ASSERT_FATAL(spdk_nvme_poll_group_destroy(group) == 0);
TAILQ_REMOVE(&g_spdk_nvme_transports, &t1, link);
TAILQ_REMOVE(&g_spdk_nvme_transports, &t2, link);
TAILQ_REMOVE(&g_spdk_nvme_transports, &t3, link);
}
static void
test_spdk_nvme_poll_group_destroy(void)
{
struct spdk_nvme_poll_group *group;
struct spdk_nvme_transport_poll_group *tgroup, *tgroup_1, *tgroup_2;
struct spdk_nvme_qpair qpair1_1 = {0};
int num_tgroups = 0;
/* Simple destruction of empty poll group. */
group = spdk_nvme_poll_group_create(NULL);
SPDK_CU_ASSERT_FATAL(group != NULL);
SPDK_CU_ASSERT_FATAL(spdk_nvme_poll_group_destroy(group) == 0);
TAILQ_INSERT_TAIL(&g_spdk_nvme_transports, &t1, link);
TAILQ_INSERT_TAIL(&g_spdk_nvme_transports, &t2, link);
TAILQ_INSERT_TAIL(&g_spdk_nvme_transports, &t3, link);
group = spdk_nvme_poll_group_create(NULL);
SPDK_CU_ASSERT_FATAL(group != NULL);
qpair1_1.transport = &t1;
CU_ASSERT(spdk_nvme_poll_group_add(group, &qpair1_1) == 0);
/* Don't remove busy poll groups. */
g_destroy_return_value = -EBUSY;
SPDK_CU_ASSERT_FATAL(spdk_nvme_poll_group_destroy(group) == -EBUSY);
STAILQ_FOREACH(tgroup, &group->tgroups, link) {
num_tgroups++;
}
CU_ASSERT(num_tgroups == 1);
/* destroy poll group with internal poll groups. */
g_destroy_return_value = 0;
tgroup_1 = STAILQ_FIRST(&group->tgroups);
tgroup_2 = STAILQ_NEXT(tgroup_1, link);
CU_ASSERT(tgroup_2 == NULL)
SPDK_CU_ASSERT_FATAL(spdk_nvme_poll_group_destroy(group) == 0);
free(tgroup_1);
}
int
main(int argc, char **argv)
{
CU_pSuite suite = NULL;
unsigned int num_failures;
if (CU_initialize_registry() != CUE_SUCCESS) {
return CU_get_error();
}
suite = CU_add_suite("nvme_ns_cmd", NULL, NULL);
if (suite == NULL) {
CU_cleanup_registry();
return CU_get_error();
}
if (
CU_add_test(suite, "nvme_poll_group_create_test", test_spdk_nvme_poll_group_create) == NULL ||
CU_add_test(suite, "nvme_poll_group_add_remove_test",
test_spdk_nvme_poll_group_add_remove) == NULL ||
CU_add_test(suite, "nvme_poll_group_process_completions",
test_spdk_nvme_poll_group_process_completions) == NULL ||
CU_add_test(suite, "nvme_poll_group_destroy_test", test_spdk_nvme_poll_group_destroy) == NULL
) {
CU_cleanup_registry();
return CU_get_error();
}
CU_basic_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
num_failures = CU_get_number_of_failures();
CU_cleanup_registry();
return num_failures;
}
|