Debian Codegraph @

test/ioctl/discovery.c

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 #include <errno.h>
4 #include <stdlib.h>
5 #include <string.h>
6
7 #include <ccan/array_size/array_size.h>
8 #include <ccan/endian/endian.h>
9
10 #include <libnvme.h>
11 #include <nvme/private.h>
12
13 #include "mock.h"
14 #include "util.h"
15
16 #define TEST_FD 0xFD
17 #define HEADER_LEN 20
18
19 static void arbitrary_ascii_string(size_t max_len, char *str, char *log_str)
20 {
21 size_t len;
22 size_t i;
23
24 len = arbitrary_range(max_len + 1);
25 for (i = 0; i < len; i++) {
26 /*
27 * ASCII strings shall contain only code values 20h through 7Eh.
28 * Exclude 20h (space) because it ends the string.
29 */
30 str[i] = log_str[i] = arbitrary_range(0x7E - 0x20) + 0x20 + 1;
31 }
32 for (i = len; i < max_len; i++) {
33 str[i] = '\0';
34 log_str[i] = ' ';
35 }
36 }
37
38 static void arbitrary_entry(struct nvmf_disc_log_entry *entry,
39 struct nvmf_disc_log_entry *log_entry)
40 {
41 arbitrary(entry, sizeof(*entry));
42 memcpy(log_entry, entry, sizeof(*entry));
43 arbitrary_ascii_string(
44 sizeof(entry->trsvcid), entry->trsvcid, log_entry->trsvcid);
45 arbitrary_ascii_string(
46 sizeof(entry->traddr), entry->traddr, log_entry->traddr);
47 }
48
49 static void arbitrary_entries(size_t len,
50 struct nvmf_disc_log_entry *entries,
51 struct nvmf_disc_log_entry *log_entries)
52 {
53 size_t i;
54
55 for (i = 0; i < len; i++)
56 arbitrary_entry(&entries[i], &log_entries[i]);
57 }
58
59 static void test_no_entries(nvme_ctrl_t c)
60 {
61 struct nvmf_discovery_log header = {};
62 /* No entries to fetch after fetching the header */
63 struct mock_cmd mock_admin_cmds[] = {
64 {
65 .opcode = nvme_admin_get_log_page,
66 .data_len = HEADER_LEN,
67 .cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
68 | NVME_LOG_LID_DISCOVER, /* LID */
69 .out_data = &header,
70 },
71 };
72 struct nvmf_discovery_log *log = NULL;
73
74 set_mock_admin_cmds(mock_admin_cmds, ARRAY_SIZE(mock_admin_cmds));
75 check(nvmf_get_discovery_log(c, &log, 1) == 0, "discovery failed: %m");
76 end_mock_cmds();
77 cmp(log, &header, sizeof(header), "incorrect header");
78 free(log);
79 }
80
81 static void test_four_entries(nvme_ctrl_t c)
82 {
83 size_t num_entries = 4;
84 struct nvmf_disc_log_entry entries[num_entries];
85 struct nvmf_disc_log_entry log_entries[num_entries];
86 struct nvmf_discovery_log header = {.numrec = cpu_to_le64(num_entries)};
87 /*
88 * All 4 entries should be fetched at once
89 * followed by the header again (to ensure genctr hasn't changed)
90 */
91 struct mock_cmd mock_admin_cmds[] = {
92 {
93 .opcode = nvme_admin_get_log_page,
94 .data_len = HEADER_LEN,
95 .cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
96 | NVME_LOG_LID_DISCOVER, /* LID */
97 .out_data = &header,
98 },
99 {
100 .opcode = nvme_admin_get_log_page,
101 .data_len = sizeof(entries),
102 .cdw10 = (sizeof(entries) / 4 - 1) << 16 /* NUMDL */
103 | NVME_LOG_LID_DISCOVER, /* LID */
104 .cdw12 = sizeof(header), /* LPOL */
105 .out_data = log_entries,
106 },
107 {
108 .opcode = nvme_admin_get_log_page,
109 .data_len = HEADER_LEN,
110 .cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
111 | NVME_LOG_LID_DISCOVER, /* LID */
112 .out_data = &header,
113 },
114 };
115 struct nvmf_discovery_log *log = NULL;
116
117 arbitrary_entries(num_entries, entries, log_entries);
118 set_mock_admin_cmds(mock_admin_cmds, ARRAY_SIZE(mock_admin_cmds));
119 check(nvmf_get_discovery_log(c, &log, 1) == 0, "discovery failed: %m");
120 end_mock_cmds();
121 cmp(log, &header, sizeof(header), "incorrect header");
122 cmp(log->entries, entries, sizeof(entries), "incorrect entries");
123 free(log);
124 }
125
126 static void test_five_entries(nvme_ctrl_t c)
127 {
128 size_t num_entries = 5;
129 struct nvmf_disc_log_entry entries[num_entries];
130 struct nvmf_disc_log_entry log_entries[num_entries];
131 size_t first_entries = 4;
132 size_t first_data_len = first_entries * sizeof(*entries);
133 size_t second_entries = num_entries - first_entries;
134 size_t second_data_len = second_entries * sizeof(*entries);
135 struct nvmf_discovery_log header = {.numrec = cpu_to_le64(num_entries)};
136 /*
137 * The first 4 entries (4 KB) are fetched together,
138 * followed by last entry separately.
139 * Finally, the header is fetched again to check genctr.
140 */
141 struct mock_cmd mock_admin_cmds[] = {
142 {
143 .opcode = nvme_admin_get_log_page,
144 .data_len = HEADER_LEN,
145 .cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
146 | NVME_LOG_LID_DISCOVER, /* LID */
147 .out_data = &header,
148 },
149 {
150 .opcode = nvme_admin_get_log_page,
151 .data_len = first_data_len,
152 .cdw10 = (first_data_len / 4 - 1) << 16 /* NUMDL */
153 | 1 << 15 /* RAE */
154 | NVME_LOG_LID_DISCOVER, /* LID */
155 .cdw12 = sizeof(header), /* LPOL */
156 .out_data = log_entries,
157 },
158 {
159 .opcode = nvme_admin_get_log_page,
160 .data_len = second_data_len,
161 .cdw10 = (second_data_len / 4 - 1) << 16 /* NUMDL */
162 | NVME_LOG_LID_DISCOVER, /* LID */
163 .cdw12 = sizeof(header) + first_data_len, /* LPOL */
164 .out_data = log_entries + first_entries,
165 },
166 {
167 .opcode = nvme_admin_get_log_page,
168 .data_len = HEADER_LEN,
169 .cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
170 | NVME_LOG_LID_DISCOVER, /* LID */
171 .out_data = &header,
172 },
173 };
174 struct nvmf_discovery_log *log = NULL;
175
176 arbitrary_entries(num_entries, entries, log_entries);
177 set_mock_admin_cmds(mock_admin_cmds, ARRAY_SIZE(mock_admin_cmds));
178 check(nvmf_get_discovery_log(c, &log, 1) == 0, "discovery failed: %m");
179 end_mock_cmds();
180 cmp(log, &header, sizeof(header), "incorrect header");
181 cmp(log->entries, entries, sizeof(entries), "incorrect entries");
182 free(log);
183 }
184
185 static void test_genctr_change(nvme_ctrl_t c)
186 {
187 struct nvmf_disc_log_entry entries1[1];
188 struct nvmf_discovery_log header1 = {
189 .numrec = cpu_to_le64(ARRAY_SIZE(entries1)),
190 };
191 size_t num_entries2 = 2;
192 struct nvmf_disc_log_entry entries2[num_entries2];
193 struct nvmf_disc_log_entry log_entries2[num_entries2];
194 struct nvmf_discovery_log header2 = {
195 .genctr = cpu_to_le64(1),
196 .numrec = cpu_to_le64(num_entries2),
197 };
198 /*
199 * genctr changes after the entries are fetched the first time,
200 * so the log page entries are refetched
201 */
202 struct mock_cmd mock_admin_cmds[] = {
203 {
204 .opcode = nvme_admin_get_log_page,
205 .data_len = HEADER_LEN,
206 .cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
207 | NVME_LOG_LID_DISCOVER, /* LID */
208 .out_data = &header1,
209 },
210 {
211 .opcode = nvme_admin_get_log_page,
212 .data_len = sizeof(entries1),
213 .cdw10 = (sizeof(entries1) / 4 - 1) << 16 /* NUMDL */
214 | NVME_LOG_LID_DISCOVER, /* NUMDL */
215 .cdw12 = sizeof(header1), /* LPOL */
216 .out_data = entries1,
217 },
218 {
219 .opcode = nvme_admin_get_log_page,
220 .data_len = HEADER_LEN,
221 .cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
222 | NVME_LOG_LID_DISCOVER, /* LID */
223 .out_data = &header2,
224 },
225 {
226 .opcode = nvme_admin_get_log_page,
227 .data_len = sizeof(entries2),
228 .cdw10 = (sizeof(entries2) / 4 - 1) << 16 /* NUMDL */
229 | NVME_LOG_LID_DISCOVER, /* LID */
230 .cdw12 = sizeof(header2), /* LPOL */
231 .out_data = log_entries2,
232 },
233 {
234 .opcode = nvme_admin_get_log_page,
235 .data_len = HEADER_LEN,
236 .cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
237 | NVME_LOG_LID_DISCOVER, /* LID */
238 .out_data = &header2,
239 },
240 };
241 struct nvmf_discovery_log *log = NULL;
242
243 arbitrary(entries1, sizeof(entries1));
244 arbitrary_entries(num_entries2, entries2, log_entries2);
245 set_mock_admin_cmds(mock_admin_cmds, ARRAY_SIZE(mock_admin_cmds));
246 check(nvmf_get_discovery_log(c, &log, 2) == 0, "discovery failed: %m");
247 end_mock_cmds();
248 cmp(log, &header2, sizeof(header2), "incorrect header");
249 cmp(log->entries, entries2, sizeof(entries2), "incorrect entries");
250 free(log);
251 }
252
253 static void test_max_retries(nvme_ctrl_t c)
254 {
255 struct nvmf_disc_log_entry entry;
256 struct nvmf_discovery_log header1 = {.numrec = cpu_to_le64(1)};
257 struct nvmf_discovery_log header2 = {
258 .genctr = cpu_to_le64(1),
259 .numrec = cpu_to_le64(1),
260 };
261 struct nvmf_discovery_log header3 = {
262 .genctr = cpu_to_le64(2),
263 .numrec = cpu_to_le64(1),
264 };
265 /* genctr changes in both attempts, hitting the max retries (2) */
266 struct mock_cmd mock_admin_cmds[] = {
267 {
268 .opcode = nvme_admin_get_log_page,
269 .data_len = HEADER_LEN,
270 .cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
271 | NVME_LOG_LID_DISCOVER, /* LID */
272 .out_data = &header1,
273 },
274 {
275 .opcode = nvme_admin_get_log_page,
276 .data_len = sizeof(entry),
277 .cdw10 = (sizeof(entry) / 4 - 1) << 16 /* NUMDL */
278 | NVME_LOG_LID_DISCOVER, /* LID */
279 .cdw12 = sizeof(header1), /* LPOL */
280 .out_data = &entry,
281 },
282 {
283 .opcode = nvme_admin_get_log_page,
284 .data_len = HEADER_LEN,
285 .cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
286 | NVME_LOG_LID_DISCOVER, /* LID */
287 .out_data = &header2,
288 },
289 {
290 .opcode = nvme_admin_get_log_page,
291 .data_len = sizeof(entry),
292 .cdw10 = (sizeof(entry) / 4 - 1) << 16 /* NUMDL */
293 | NVME_LOG_LID_DISCOVER, /* LID */
294 .cdw12 = sizeof(header2), /* LPOL */
295 .out_data = &entry,
296 },
297 {
298 .opcode = nvme_admin_get_log_page,
299 .data_len = HEADER_LEN,
300 .cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
301 | NVME_LOG_LID_DISCOVER, /* LID */
302 .out_data = &header3,
303 },
304 };
305 struct nvmf_discovery_log *log = NULL;
306
307 arbitrary(&entry, sizeof(entry));
308 set_mock_admin_cmds(mock_admin_cmds, ARRAY_SIZE(mock_admin_cmds));
309 check(nvmf_get_discovery_log(c, &log, 2) == -1, "discovery succeeded");
310 end_mock_cmds();
311 check(errno == EAGAIN, "discovery failed: %m");
312 check(!log, "unexpected log page returned");
313 }
314
315 static void test_header_error(nvme_ctrl_t c)
316 {
317 /* Stop after an error in fetching the header the first time */
318 struct mock_cmd mock_admin_cmds[] = {
319 {
320 .opcode = nvme_admin_get_log_page,
321 .data_len = HEADER_LEN,
322 .cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
323 | NVME_LOG_LID_DISCOVER, /* LID */
324 .err = NVME_SC_INVALID_OPCODE,
325 },
326 };
327 struct nvmf_discovery_log *log = NULL;
328
329 set_mock_admin_cmds(mock_admin_cmds, ARRAY_SIZE(mock_admin_cmds));
330 check(nvmf_get_discovery_log(c, &log, 1) == -1, "discovery succeeded");
331 end_mock_cmds();
332 check(!log, "unexpected log page returned");
333 }
334
335 static void test_entries_error(nvme_ctrl_t c)
336 {
337 struct nvmf_discovery_log header = {.numrec = cpu_to_le64(1)};
338 size_t entry_size = sizeof(struct nvmf_disc_log_entry);
339 /* Stop after an error in fetching the entries */
340 struct mock_cmd mock_admin_cmds[] = {
341 {
342 .opcode = nvme_admin_get_log_page,
343 .data_len = HEADER_LEN,
344 .cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
345 | NVME_LOG_LID_DISCOVER, /* LID */
346 .out_data = &header,
347 },
348 {
349 .opcode = nvme_admin_get_log_page,
350 .data_len = entry_size,
351 .cdw10 = (entry_size / 4 - 1) << 16 /* NUMDL */
352 | NVME_LOG_LID_DISCOVER, /* LID */
353 .cdw12 = sizeof(header), /* LPOL */
354 .err = -EIO,
355 },
356 };
357 struct nvmf_discovery_log *log = NULL;
358
359 set_mock_admin_cmds(mock_admin_cmds, ARRAY_SIZE(mock_admin_cmds));
360 check(nvmf_get_discovery_log(c, &log, 1) == -1, "discovery succeeded");
361 end_mock_cmds();
362 check(errno == EIO, "discovery failed: %m");
363 check(!log, "unexpected log page returned");
364 }
365
366 static void test_genctr_error(nvme_ctrl_t c)
367 {
368 struct nvmf_disc_log_entry entry;
369 struct nvmf_discovery_log header = {.numrec = cpu_to_le64(1)};
370 /* Stop after an error in refetching the header */
371 struct mock_cmd mock_admin_cmds[] = {
372 {
373 .opcode = nvme_admin_get_log_page,
374 .data_len = HEADER_LEN,
375 .cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
376 | NVME_LOG_LID_DISCOVER, /* LID */
377 .out_data = &header,
378 },
379 {
380 .opcode = nvme_admin_get_log_page,
381 .data_len = sizeof(entry),
382 .cdw10 = (sizeof(entry) / 4 - 1) << 16 /* NUMDL */
383 | NVME_LOG_LID_DISCOVER, /* LID */
384 .cdw12 = sizeof(header), /* LPOL */
385 .out_data = &entry,
386 },
387 {
388 .opcode = nvme_admin_get_log_page,
389 .data_len = HEADER_LEN,
390 .cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
391 | NVME_LOG_LID_DISCOVER, /* LID */
392 .err = NVME_SC_INTERNAL,
393 },
394 };
395 struct nvmf_discovery_log *log = NULL;
396
397 arbitrary(&entry, sizeof(entry));
398 set_mock_admin_cmds(mock_admin_cmds, ARRAY_SIZE(mock_admin_cmds));
399 check(nvmf_get_discovery_log(c, &log, 1) == -1, "discovery succeeded");
400 end_mock_cmds();
401 check(!log, "unexpected log page returned");
402 }
403
404 static void run_test(const char *test_name, void (*test_fn)(nvme_ctrl_t))
405 {
406 struct nvme_ctrl c = {.fd = TEST_FD};
407
408 printf("Running test %s...", test_name);
409 fflush(stdout);
410 check(asprintf(&c.name, "%s_ctrl", test_name) >= 0, "asprintf() failed");
411 test_fn(&c);
412 free(c.name);
413 puts(" OK");
414 }
415
416 #define RUN_TEST(name) run_test(#name, test_ ## name)
417
418 int main(void)
419 {
420 set_mock_fd(TEST_FD);
421 RUN_TEST(no_entries);
422 RUN_TEST(four_entries);
423 RUN_TEST(five_entries);
424 RUN_TEST(genctr_change);
425 RUN_TEST(max_retries);
426 RUN_TEST(header_error);
427 RUN_TEST(entries_error);
428 RUN_TEST(genctr_error);
429 }