summaryrefslogtreecommitdiffstats
path: root/test/ioctl/discovery.c
blob: f5f6f5160fae2a35e07ed35223baaa8683ad41c3 (plain)
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
// SPDX-License-Identifier: LGPL-2.1-or-later

#include <libnvme.h>

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <ccan/array_size/array_size.h>
#include <ccan/endian/endian.h>

#include "../../src/nvme/private.h"
#include "mock.h"
#include "util.h"

#define TEST_FD 0xFD
#define HEADER_LEN 20

static void arbitrary_ascii_string(size_t max_len, char *str, char *log_str)
{
	size_t len;
	size_t i;

	len = arbitrary_range(max_len + 1);
	for (i = 0; i < len; i++) {
		/*
		 * ASCII strings shall contain only code values 20h through 7Eh.
		 * Exclude 20h (space) because it ends the string.
		 */
		str[i] = log_str[i] = arbitrary_range(0x7E - 0x20) + 0x20 + 1;
	}
	for (i = len; i < max_len; i++) {
		str[i] = '\0';
		log_str[i] = ' ';
	}
}

static void arbitrary_entry(struct nvmf_disc_log_entry *entry,
                            struct nvmf_disc_log_entry *log_entry)
{
	arbitrary(entry, sizeof(*entry));
	memcpy(log_entry, entry, sizeof(*entry));
	arbitrary_ascii_string(
		sizeof(entry->trsvcid), entry->trsvcid, log_entry->trsvcid);
	arbitrary_ascii_string(
		sizeof(entry->traddr), entry->traddr, log_entry->traddr);
}

static void arbitrary_entries(size_t len,
                              struct nvmf_disc_log_entry *entries,
                              struct nvmf_disc_log_entry *log_entries)
{
	size_t i;

	for (i = 0; i < len; i++)
		arbitrary_entry(&entries[i], &log_entries[i]);
}

static void test_no_entries(nvme_ctrl_t c)
{
	struct nvmf_discovery_log header = {};
	/* No entries to fetch after fetching the header */
	struct mock_cmd mock_admin_cmds[] = {
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = HEADER_LEN,
			.cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.out_data = &header,
		},
	};
	struct nvmf_discovery_log *log = NULL;

	set_mock_admin_cmds(mock_admin_cmds, ARRAY_SIZE(mock_admin_cmds));
	check(nvmf_get_discovery_log(c, &log, 1) == 0, "discovery failed: %m");
	end_mock_cmds();
	cmp(log, &header, sizeof(header), "incorrect header");
	free(log);
}

static void test_four_entries(nvme_ctrl_t c)
{
	size_t num_entries = 4;
	struct nvmf_disc_log_entry entries[num_entries];
	struct nvmf_disc_log_entry log_entries[num_entries];
	struct nvmf_discovery_log header = {.numrec = cpu_to_le64(num_entries)};
	/*
	 * All 4 entries should be fetched at once
	 * followed by the header again (to ensure genctr hasn't changed)
	 */
	struct mock_cmd mock_admin_cmds[] = {
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = HEADER_LEN,
			.cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.out_data = &header,
		},
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = sizeof(entries),
			.cdw10 = (sizeof(entries) / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.cdw12 = sizeof(header), /* LPOL */
			.out_data = log_entries,
		},
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = HEADER_LEN,
			.cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.out_data = &header,
		},
	};
	struct nvmf_discovery_log *log = NULL;

	arbitrary_entries(num_entries, entries, log_entries);
	set_mock_admin_cmds(mock_admin_cmds, ARRAY_SIZE(mock_admin_cmds));
	check(nvmf_get_discovery_log(c, &log, 1) == 0, "discovery failed: %m");
	end_mock_cmds();
	cmp(log, &header, sizeof(header), "incorrect header");
	cmp(log->entries, entries, sizeof(entries), "incorrect entries");
	free(log);
}

static void test_five_entries(nvme_ctrl_t c)
{
	size_t num_entries = 5;
	struct nvmf_disc_log_entry entries[num_entries];
	struct nvmf_disc_log_entry log_entries[num_entries];
	size_t first_entries = 4;
	size_t first_data_len = first_entries * sizeof(*entries);
	size_t second_entries = num_entries - first_entries;
	size_t second_data_len = second_entries * sizeof(*entries);
	struct nvmf_discovery_log header = {.numrec = cpu_to_le64(num_entries)};
	/*
	 * The first 4 entries (4 KB) are fetched together,
	 * followed by last entry separately.
	 * Finally, the header is fetched again to check genctr.
	 */
	struct mock_cmd mock_admin_cmds[] = {
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = HEADER_LEN,
			.cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.out_data = &header,
		},
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = first_data_len,
			.cdw10 = (first_data_len / 4 - 1) << 16 /* NUMDL */
			       | 1 << 15 /* RAE */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.cdw12 = sizeof(header), /* LPOL */
			.out_data = log_entries,
		},
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = second_data_len,
			.cdw10 = (second_data_len / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.cdw12 = sizeof(header) + first_data_len, /* LPOL */
			.out_data = log_entries + first_entries,
		},
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = HEADER_LEN,
			.cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.out_data = &header,
		},
	};
	struct nvmf_discovery_log *log = NULL;

	arbitrary_entries(num_entries, entries, log_entries);
	set_mock_admin_cmds(mock_admin_cmds, ARRAY_SIZE(mock_admin_cmds));
	check(nvmf_get_discovery_log(c, &log, 1) == 0, "discovery failed: %m");
	end_mock_cmds();
	cmp(log, &header, sizeof(header), "incorrect header");
	cmp(log->entries, entries, sizeof(entries), "incorrect entries");
	free(log);
}

static void test_genctr_change(nvme_ctrl_t c)
{
	struct nvmf_disc_log_entry entries1[1];
	struct nvmf_discovery_log header1 = {
		.numrec = cpu_to_le64(ARRAY_SIZE(entries1)),
	};
	size_t num_entries2 = 2;
	struct nvmf_disc_log_entry entries2[num_entries2];
	struct nvmf_disc_log_entry log_entries2[num_entries2];
	struct nvmf_discovery_log header2 = {
		.genctr = cpu_to_le64(1),
		.numrec = cpu_to_le64(num_entries2),
	};
	/*
	 * genctr changes after the entries are fetched the first time,
	 * so the log page entries are refetched
	 */
	struct mock_cmd mock_admin_cmds[] = {
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = HEADER_LEN,
			.cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.out_data = &header1,
		},
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = sizeof(entries1),
			.cdw10 = (sizeof(entries1) / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* NUMDL */
			.cdw12 = sizeof(header1), /* LPOL */
			.out_data = entries1,
		},
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = HEADER_LEN,
			.cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.out_data = &header2,
		},
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = sizeof(entries2),
			.cdw10 = (sizeof(entries2) / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.cdw12 = sizeof(header2), /* LPOL */
			.out_data = log_entries2,
		},
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = HEADER_LEN,
			.cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.out_data = &header2,
		},
	};
	struct nvmf_discovery_log *log = NULL;

	arbitrary(entries1, sizeof(entries1));
	arbitrary_entries(num_entries2, entries2, log_entries2);
	set_mock_admin_cmds(mock_admin_cmds, ARRAY_SIZE(mock_admin_cmds));
	check(nvmf_get_discovery_log(c, &log, 2) == 0, "discovery failed: %m");
	end_mock_cmds();
	cmp(log, &header2, sizeof(header2), "incorrect header");
	cmp(log->entries, entries2, sizeof(entries2), "incorrect entries");
	free(log);
}

static void test_max_retries(nvme_ctrl_t c)
{
	struct nvmf_disc_log_entry entry;
	struct nvmf_discovery_log header1 = {.numrec = cpu_to_le64(1)};
	struct nvmf_discovery_log header2 = {
		.genctr = cpu_to_le64(1),
		.numrec = cpu_to_le64(1),
	};
	struct nvmf_discovery_log header3 = {
		.genctr = cpu_to_le64(2),
		.numrec = cpu_to_le64(1),
	};
	/* genctr changes in both attempts, hitting the max retries (2) */
	struct mock_cmd mock_admin_cmds[] = {
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = HEADER_LEN,
			.cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.out_data = &header1,
		},
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = sizeof(entry),
			.cdw10 = (sizeof(entry) / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.cdw12 = sizeof(header1), /* LPOL */
			.out_data = &entry,
		},
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = HEADER_LEN,
			.cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.out_data = &header2,
		},
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = sizeof(entry),
			.cdw10 = (sizeof(entry) / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.cdw12 = sizeof(header2), /* LPOL */
			.out_data = &entry,
		},
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = HEADER_LEN,
			.cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.out_data = &header3,
		},
	};
	struct nvmf_discovery_log *log = NULL;

	arbitrary(&entry, sizeof(entry));
	set_mock_admin_cmds(mock_admin_cmds, ARRAY_SIZE(mock_admin_cmds));
	check(nvmf_get_discovery_log(c, &log, 2) == -1, "discovery succeeded");
	end_mock_cmds();
	check(errno == EAGAIN, "discovery failed: %m");
	check(!log, "unexpected log page returned");
}

static void test_header_error(nvme_ctrl_t c)
{
	/* Stop after an error in fetching the header the first time */
	struct mock_cmd mock_admin_cmds[] = {
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = HEADER_LEN,
			.cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.err = NVME_SC_INVALID_OPCODE,
		},
	};
	struct nvmf_discovery_log *log = NULL;

	set_mock_admin_cmds(mock_admin_cmds, ARRAY_SIZE(mock_admin_cmds));
	check(nvmf_get_discovery_log(c, &log, 1) == -1, "discovery succeeded");
	end_mock_cmds();
	check(!log, "unexpected log page returned");
}

static void test_entries_error(nvme_ctrl_t c)
{
	struct nvmf_discovery_log header = {.numrec = cpu_to_le64(1)};
	size_t entry_size = sizeof(struct nvmf_disc_log_entry);
	/* Stop after an error in fetching the entries */
	struct mock_cmd mock_admin_cmds[] = {
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = HEADER_LEN,
			.cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.out_data = &header,
		},
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = entry_size,
			.cdw10 = (entry_size / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.cdw12 = sizeof(header), /* LPOL */
			.err = -EIO,
		},
	};
	struct nvmf_discovery_log *log = NULL;

	set_mock_admin_cmds(mock_admin_cmds, ARRAY_SIZE(mock_admin_cmds));
	check(nvmf_get_discovery_log(c, &log, 1) == -1, "discovery succeeded");
	end_mock_cmds();
	check(errno == EIO, "discovery failed: %m");
	check(!log, "unexpected log page returned");
}

static void test_genctr_error(nvme_ctrl_t c)
{
	struct nvmf_disc_log_entry entry;
	struct nvmf_discovery_log header = {.numrec = cpu_to_le64(1)};
	/* Stop after an error in refetching the header */
	struct mock_cmd mock_admin_cmds[] = {
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = HEADER_LEN,
			.cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.out_data = &header,
		},
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = sizeof(entry),
			.cdw10 = (sizeof(entry) / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.cdw12 = sizeof(header), /* LPOL */
			.out_data = &entry,
		},
		{
			.opcode = nvme_admin_get_log_page,
			.data_len = HEADER_LEN,
			.cdw10 = (HEADER_LEN / 4 - 1) << 16 /* NUMDL */
			       | NVME_LOG_LID_DISCOVER, /* LID */
			.err = NVME_SC_INTERNAL,
		},
	};
	struct nvmf_discovery_log *log = NULL;

	arbitrary(&entry, sizeof(entry));
	set_mock_admin_cmds(mock_admin_cmds, ARRAY_SIZE(mock_admin_cmds));
	check(nvmf_get_discovery_log(c, &log, 1) == -1, "discovery succeeded");
	end_mock_cmds();
	check(!log, "unexpected log page returned");
}

static void run_test(const char *test_name, void (*test_fn)(nvme_ctrl_t))
{
	struct nvme_ctrl c = {.fd = TEST_FD};

	printf("Running test %s...", test_name);
	fflush(stdout);
	check(asprintf(&c.name, "%s_ctrl", test_name) >= 0, "asprintf() failed");
	test_fn(&c);
	free(c.name);
	puts(" OK");
}

#define RUN_TEST(name) run_test(#name, test_ ## name)

int main(void)
{
	set_mock_fd(TEST_FD);
	RUN_TEST(no_entries);
	RUN_TEST(four_entries);
	RUN_TEST(five_entries);
	RUN_TEST(genctr_change);
	RUN_TEST(max_retries);
	RUN_TEST(header_error);
	RUN_TEST(entries_error);
	RUN_TEST(genctr_error);
}