Debian Codegraph @

test/tree.c

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /**
3 * This file is part of libnvme.
4 * Copyright (c) 2023 Daniel Wagner, SUSE LLC
5 */
6
7 #include <assert.h>
8 #include <string.h>
9 #include <stdbool.h>
10 #include <stdlib.h>
11 #include <arpa/inet.h>
12
13 #include <ccan/array_size/array_size.h>
14
15 #include <libnvme.h>
16 #include <nvme/private.h>
17
18 struct test_data {
19 /* input data */
20 const char *subsysname;
21 const char *subsysnqn;
22 const char *transport;
23 const char *traddr;
24 const char *host_traddr;
25 const char *host_iface;
26 const char *trsvcid;
27
28 /* track controller generated by input data */
29 nvme_subsystem_t s;
30 nvme_ctrl_t c;
31 int ctrl_id;
32 };
33
34 #define DEFAULT_SUBSYSNAME "subsysname"
35 #define DEFAULT_SUBSYSNQN "subsysnqn"
36 #define SRC_ADDR4 "192.168.56.100"
37 #define SRC_ADDR6 "1234:5678:abcd:EF01:1234:5678:abcd:EF01"
38
39 struct test_data test_data[] = {
40 { DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN, "tcp", "192.168.1.1", "192.168.1.20", NULL, "4420" },
41 { DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN, "tcp", "192.168.1.1", "192.168.1.20", NULL, "4421" },
42 { DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN, "tcp", "192.168.1.2", "192.168.1.20", "eth1", "4420" },
43 { DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN, "tcp", "192.168.1.2", "192.168.1.20", "eth1", "4421" },
44 { DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN, "rdma", "192.168.1.3", "192.168.1.20", NULL, NULL },
45 { DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN, "rdma", "192.168.1.4", "192.168.1.20", NULL, NULL },
46 { DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN, "fc",
47 "nn-0x201700a09890f5bf:pn-0x201900a09890f5bf",
48 "nn-0x200000109b579ef3:pn-0x100000109b579ef3"
49 },
50 { DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN, "fc",
51 "nn-0x201700a09890f5bf:pn-0x201900a09890f5bf",
52 "nn-0x200000109b579ef6:pn-0x100000109b579ef6",
53 },
54 };
55
56 static struct test_data *find_test_data(nvme_ctrl_t c)
57 {
58 for (int i = 0; i < ARRAY_SIZE(test_data); i++) {
59 struct test_data *d = &test_data[i];
60
61 if (d->c == c)
62 return d;
63 }
64
65 return NULL;
66 }
67
68 static void show_ctrl(nvme_ctrl_t c)
69 {
70 struct test_data *d = find_test_data(c);
71
72 if (d)
73 printf("ctrl%d ", d->ctrl_id);
74 else
75 printf(" ");
76
77 printf("0x%p: %s %s %s %s %s ",
78 c,
79 nvme_ctrl_get_transport(c),
80 nvme_ctrl_get_traddr(c),
81 nvme_ctrl_get_host_traddr(c),
82 nvme_ctrl_get_host_iface(c),
83 nvme_ctrl_get_trsvcid(c));
84 }
85
86 static bool match_ctrl(struct test_data *d, nvme_ctrl_t c)
87 {
88 bool pass = true;
89 const char *trsvid, *host_traddr, *host_iface;
90
91 if (d->c != c)
92 pass = false;
93
94 if (strcmp(d->transport, nvme_ctrl_get_transport(d->c)))
95 pass = false;
96
97 if (strcmp(d->traddr, nvme_ctrl_get_traddr(d->c)))
98 pass = false;
99
100
101 host_traddr = nvme_ctrl_get_host_traddr(c);
102 if (d->host_traddr &&
103 (!host_traddr || strcmp(d->host_traddr, host_traddr)))
104 pass = false;
105
106 host_iface = nvme_ctrl_get_host_iface(c);
107 if (d->host_iface &&
108 (!host_iface || strcmp(d->host_iface, host_iface)))
109 pass = false;
110
111 trsvid = nvme_ctrl_get_trsvcid(c);
112 if (d->trsvcid &&
113 (!trsvid || strcmp(d->trsvcid, trsvid)))
114 pass = false;
115
116 printf("[%s]", pass? "PASS" : "FAILED");
117
118 return pass;
119 }
120
121 static nvme_root_t create_tree()
122 {
123 nvme_root_t r;
124 nvme_host_t h;
125
126 r = nvme_create_root(stdout, LOG_DEBUG);
127 assert(r);
128 h = nvme_default_host(r);
129 assert(h);
130
131 printf(" ctrls created:\n");
132 for (int i = 0; i < ARRAY_SIZE(test_data); i++) {
133 struct test_data *d = &test_data[i];
134
135 d->s = nvme_lookup_subsystem(h, d->subsysname, d->subsysnqn);
136 assert(d->s);
137 d->c = nvme_lookup_ctrl(d->s, d->transport, d->traddr,
138 d->host_traddr, d->host_iface,
139 d->trsvcid, NULL);
140 assert(d->c);
141 d->ctrl_id = i;
142
143 printf(" ");
144 show_ctrl(d->c);
145 match_ctrl(d, d->c);
146 printf("\n");
147 }
148 printf("\n");
149
150 return r;
151 }
152
153 static unsigned int count_entries(nvme_root_t r)
154 {
155 nvme_host_t h;
156 nvme_subsystem_t s;
157 nvme_ctrl_t c;
158 unsigned int i = 0;
159
160 nvme_for_each_host(r, h)
161 nvme_for_each_subsystem(h, s)
162 nvme_subsystem_for_each_ctrl(s, c)
163 i++;
164
165 return i;
166 }
167
168 static bool tcp_ctrl_lookup(nvme_subsystem_t s, struct test_data *d)
169 {
170 nvme_ctrl_t c;
171 bool pass = true;
172
173 c = nvme_lookup_ctrl(s, d->transport, d->traddr, NULL,
174 NULL, d->trsvcid, NULL);
175 printf("%10s %12s %10s -> ", d->trsvcid, "", "");
176 show_ctrl(c);
177 pass &= match_ctrl(d, c);
178 printf("\n");
179
180 if (d->host_traddr) {
181 c = nvme_lookup_ctrl(s, d->transport, d->traddr, d->host_traddr,
182 NULL, d->trsvcid, NULL);
183 printf("%10s %12s %10s -> ", d->trsvcid, d->host_traddr, "");
184 show_ctrl(c);
185 pass &= match_ctrl(d, c);
186 printf("\n");
187 }
188
189 if (d->host_iface) {
190 c = nvme_lookup_ctrl(s, d->transport, d->traddr, NULL,
191 d->host_iface, d->trsvcid, NULL);
192 printf("%10s %12s %10s -> ", d->trsvcid, "", d->host_iface);
193 show_ctrl(c);
194 pass &= match_ctrl(d, c);
195 printf("\n");
196 }
197
198 if (d->host_iface && d->traddr) {
199 c = nvme_lookup_ctrl(s, d->transport, d->traddr, d->host_traddr,
200 d->host_iface, d->trsvcid, NULL);
201 printf("%10s %12s %10s -> ", d->trsvcid, d->host_traddr, d->host_iface);
202 show_ctrl(c);
203 pass &= match_ctrl(d, c);
204 printf("\n");
205 }
206
207 return pass;
208 }
209
210 static bool default_ctrl_lookup(nvme_subsystem_t s, struct test_data *d)
211 {
212 nvme_ctrl_t c;
213 bool pass = true;
214
215 c = nvme_lookup_ctrl(s, d->transport, d->traddr, d->host_traddr,
216 NULL, NULL, NULL);
217 printf("%10s %12s %10s -> ", "", "", "");
218 show_ctrl(c);
219 pass &= match_ctrl(d, c);
220 printf("\n");
221
222 return pass;
223 }
224
225 static bool ctrl_lookups(nvme_root_t r)
226 {
227 nvme_host_t h;
228 nvme_subsystem_t s;
229 bool pass = true;
230
231 h = nvme_first_host(r);
232 s = nvme_lookup_subsystem(h, DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN);
233
234 printf(" lookup controller:\n");
235 for (int i = 0; i < ARRAY_SIZE(test_data); i++) {
236 struct test_data *d = &test_data[i];
237
238 printf("%10s %12s %10s ", "", "", "");
239 show_ctrl(d->c);
240 printf("\n");
241
242 if (!strcmp("tcp", d->transport))
243 pass &= tcp_ctrl_lookup(s, d);
244 else
245 pass &= default_ctrl_lookup(s, d);
246
247 printf("\n");
248 }
249
250 return pass;
251 }
252
253 static bool test_lookup(void)
254 {
255 nvme_root_t r;
256 bool pass;
257
258 printf("test_lookup:\n");
259
260 r = create_tree();
261 pass = count_entries(r) == ARRAY_SIZE(test_data);
262 pass &= ctrl_lookups(r);
263
264 nvme_free_tree(r);
265
266 return pass;
267 }
268
269 static bool test_src_addr()
270 {
271 bool pass = true;
272 nvme_root_t r;
273 nvme_host_t h;
274 nvme_ctrl_t c;
275 nvme_subsystem_t s;
276 char *src_addr, buffer[100]; /* big enough for IPv6 max length */
277
278 printf("\n"
279 "test_src_addr:\n");
280
281 r = nvme_create_root(stdout, LOG_DEBUG);
282 assert(r);
283
284 h = nvme_default_host(r);
285 assert(h);
286
287 s = nvme_lookup_subsystem(h, DEFAULT_SUBSYSNAME, DEFAULT_SUBSYSNQN);
288 assert(s);
289
290 c = nvme_lookup_ctrl(s, "tcp", "192.168.56.1", NULL, NULL, "8009", NULL);
291 assert(c);
292
293 c->address = NULL;
294 printf(" - Test c->address = NULL : src_addr = NULL ");
295 src_addr = nvme_ctrl_get_src_addr(c, buffer, sizeof(buffer));
296 if (src_addr != NULL) {
297 printf("[FAIL]\n");
298 fprintf(stderr,
299 "nvme_ctrl_get_src_addr() c->address=NULL should return src_addr=NULL\n");
300 pass = false;
301 } else {
302 printf("[PASS]\n");
303 }
304
305 c->address = "";
306 printf(" - Test c->address = \"\" : src_addr = NULL ");
307 src_addr = nvme_ctrl_get_src_addr(c, buffer, sizeof(buffer));
308 if (src_addr != NULL) {
309 printf("[FAIL]\n");
310 fprintf(stderr,
311 "nvme_ctrl_get_src_addr() c->address="" should return src_addr=NULL\n");
312 pass = false;
313 } else {
314 printf("[PASS]\n");
315 }
316
317 c->address = "traddr=192.168.56.1,trsvcid=8009";
318 printf(" - Test c->address = \"%s\" : src_addr = NULL ", c->address);
319 src_addr = nvme_ctrl_get_src_addr(c, buffer, sizeof(buffer));
320 if (src_addr != NULL) {
321 printf("[FAIL]\n");
322 fprintf(stderr,
323 "nvme_ctrl_get_src_addr() c->address=%s should return src_addr=NULL\n",
324 c->address);
325 pass = false;
326 } else {
327 printf("[PASS]\n");
328 }
329
330 c->address = "traddr=192.168.56.1,trsvcid=8009,src_addr=" SRC_ADDR4;
331 printf(" - Test c->address = \"%s\" : src_addr = \"" SRC_ADDR4 "\" ", c->address);
332 src_addr = nvme_ctrl_get_src_addr(c, buffer, sizeof(buffer));
333 if (!src_addr || strcmp(src_addr, SRC_ADDR4)) {
334 printf("[FAIL]\n");
335 fprintf(stderr,
336 "nvme_ctrl_get_src_addr() c->address=%s should return src_addr=" SRC_ADDR4 "\n",
337 c->address);
338 pass = false;
339 } else {
340 printf("[PASS]\n");
341 }
342
343 c->address = "traddr=192.168.56.1,src_addr=" SRC_ADDR4 ",trsvcid=8009";
344 printf(" - Test c->address = \"%s\" : src_addr = \"" SRC_ADDR4 "\" ", c->address);
345 src_addr = nvme_ctrl_get_src_addr(c, buffer, sizeof(buffer));
346 if (!src_addr || strcmp(src_addr, SRC_ADDR4)) {
347 printf("[FAIL]\n");
348 fprintf(stderr,
349 "nvme_ctrl_get_src_addr() c->address=%s should return src_addr=" SRC_ADDR4 "\n",
350 c->address);
351 pass = false;
352 } else {
353 printf("[PASS]\n");
354 }
355
356 c->address = "traddr=1234::abcd,trsvcid=8009,src_addr=" SRC_ADDR6;
357 printf(" - Test c->address = \"%s\" : src_addr = \"" SRC_ADDR6 "\" ", c->address);
358 src_addr = nvme_ctrl_get_src_addr(c, buffer, sizeof(buffer));
359 if (!src_addr || strcmp(src_addr, SRC_ADDR6)) {
360 printf("[FAIL]\n");
361 fprintf(stderr,
362 "nvme_ctrl_get_src_addr() c->address=%s should return src_addr=" SRC_ADDR6 "\n",
363 c->address);
364 pass = false;
365 } else {
366 printf("[PASS]\n");
367 }
368
369 c->address = "traddr=1234::abcd,src_addr=" SRC_ADDR6 ",trsvcid=8009";
370 printf(" - Test c->address = \"%s\" : src_addr = \"" SRC_ADDR6 "\" ", c->address);
371 src_addr = nvme_ctrl_get_src_addr(c, buffer, sizeof(buffer));
372 if (!src_addr || strcmp(src_addr, SRC_ADDR6)) {
373 printf("[FAIL]\n");
374 fprintf(stderr,
375 "nvme_ctrl_get_src_addr() c->address=%s should return src_addr=" SRC_ADDR6 "\n",
376 c->address);
377 pass = false;
378 } else {
379 printf("[PASS]\n");
380 }
381
382 c->address = "traddr=1234::abcd,trsvcid=8009,src_addr=" SRC_ADDR6 "%scope";
383 printf(" - Test c->address = \"%s\" : src_addr = \"" SRC_ADDR6 "\" ", c->address);
384 src_addr = nvme_ctrl_get_src_addr(c, buffer, sizeof(buffer));
385 if (!src_addr || strcmp(src_addr, SRC_ADDR6)) {
386 printf("[FAIL]\n");
387 fprintf(stderr,
388 "nvme_ctrl_get_src_addr() c->address=%s should return src_addr=" SRC_ADDR6 "\n",
389 c->address);
390 pass = false;
391 } else {
392 printf("[PASS]\n");
393 }
394
395 c->address = "traddr=1234::abcd,src_addr=" SRC_ADDR6 "%scope,trsvcid=8009";
396 printf(" - Test c->address = \"%s\" : src_addr = \"" SRC_ADDR6 "\" ", c->address);
397 src_addr = nvme_ctrl_get_src_addr(c, buffer, sizeof(buffer));
398 if (!src_addr || strcmp(src_addr, SRC_ADDR6)) {
399 printf("[FAIL]\n");
400 fprintf(stderr,
401 "nvme_ctrl_get_src_addr() c->address=%s should return src_addr=" SRC_ADDR6 "\n",
402 c->address);
403 pass = false;
404 } else {
405 printf("[PASS]\n");
406 }
407
408 c->address = NULL; /* Needed to avoid freeing non-malloced memory (see above) */
409
410 nvme_free_tree(r);
411
412 return pass;
413 }
414
415 struct ctrl_args {
416 const char *transport;
417 const char *traddr;
418 const char *trsvcid;
419 const char *host_traddr;
420 const char *host_iface;
421 const char *address;
422 const char *subsysnqn;
423 };
424
425 static void set_ctrl_args(struct ctrl_args *args,
426 const char *transport,
427 const char *traddr,
428 const char *trsvcid,
429 const char *host_traddr,
430 const char *host_iface,
431 const char *address,
432 const char *subsysnqn)
433 {
434 args->transport = transport;
435 args->traddr = traddr;
436 args->trsvcid = trsvcid;
437 args->host_traddr = host_traddr;
438 args->host_iface = host_iface;
439 args->address = address;
440 args->subsysnqn = subsysnqn;
441 }
442
443 static bool ctrl_match(const char *tag,
444 int reference_id,
445 int candidate_id,
446 struct ctrl_args *reference,
447 struct ctrl_args *candidate,
448 bool should_match)
449 {
450 nvme_root_t r;
451 nvme_host_t h;
452 nvme_ctrl_t reference_ctrl; /* Existing controller (from sysfs) */
453 nvme_ctrl_t candidate_ctrl;
454 nvme_ctrl_t found_ctrl;
455 nvme_subsystem_t s;
456
457 r = nvme_create_root(stdout, LOG_INFO);
458 assert(r);
459
460 h = nvme_default_host(r);
461 assert(h);
462
463 s = nvme_lookup_subsystem(h, DEFAULT_SUBSYSNAME, reference->subsysnqn ? reference->subsysnqn : DEFAULT_SUBSYSNQN);
464 assert(s);
465
466 reference_ctrl = nvme_lookup_ctrl(s, reference->transport, reference->traddr,
467 reference->host_traddr, reference->host_iface,
468 reference->trsvcid, NULL);
469 assert(reference_ctrl);
470 reference_ctrl->name = "nvme1"; /* fake the device name */
471 if (reference->address) {
472 reference_ctrl->address = (char *)reference->address;
473 }
474
475 /* nvme_ctrl_find() MUST BE RUN BEFORE nvme_lookup_ctrl() */
476 found_ctrl = nvme_ctrl_find(s, candidate->transport, candidate->traddr,
477 candidate->trsvcid, candidate->subsysnqn,
478 candidate->host_traddr,
479 candidate->host_iface);
480
481 candidate_ctrl = nvme_lookup_ctrl(s, candidate->transport, candidate->traddr,
482 candidate->host_traddr, candidate->host_iface,
483 candidate->trsvcid, NULL);
484
485 if (should_match) {
486 if (candidate_ctrl != reference_ctrl) {
487 printf("%s-%d-%d: Candidate (%s, %s, %s, %s, %s, %s) failed to match (%s, %s, %s, %s, %s, %s, %s)\n",
488 tag, reference_id, candidate_id,
489 candidate->transport, candidate->traddr, candidate->trsvcid,
490 candidate->subsysnqn, candidate->host_traddr, candidate->host_iface,
491 reference->transport, reference->traddr, reference->trsvcid, reference->subsysnqn,
492 reference->host_traddr, reference->host_iface, reference->address);
493 return false;
494 }
495
496 if (!found_ctrl) {
497 printf("%s-%d-%d: Candidate (%s, %s, %s, %s, %s, %s) failed to find controller\n",
498 tag, reference_id, candidate_id,
499 candidate->transport, candidate->traddr, candidate->trsvcid,
500 candidate->subsysnqn, candidate->host_traddr, candidate->host_iface);
501 return false;
502 }
503 } else {
504 if (candidate_ctrl == reference_ctrl) {
505 printf("%s-%d-%d: Candidate (%s, %s, %s, %s, %s, %s) should not match (%s, %s, %s, %s, %s, %s, %s)\n",
506 tag, reference_id, candidate_id,
507 candidate->transport, candidate->traddr, candidate->trsvcid,
508 candidate->subsysnqn, candidate->host_traddr, candidate->host_iface,
509 reference->transport, reference->traddr, reference->trsvcid, reference->subsysnqn,
510 reference->host_traddr, reference->host_iface, reference->address);
511 return false;
512 }
513
514 if (found_ctrl) {
515 printf("%s-%d-%d: Candidate (%s, %s, %s, %s, %s, %s) should not have found controller. found_ctrl=%p reference=%p\n",
516 tag, reference_id, candidate_id,
517 candidate->transport, candidate->traddr, candidate->trsvcid, candidate->subsysnqn,
518 candidate->host_traddr, candidate->host_iface, found_ctrl, reference_ctrl);
519 return false;
520 }
521 }
522
523 /* Set the faked data back to NULL before freeing the tree */
524 reference_ctrl->name = NULL;
525 reference_ctrl->address = NULL;
526
527 nvme_free_tree(r);
528
529 return true;
530 }
531
532 /**
533 * test_ctrl_match_fc - Test that we can look up FC controllers
534 *
535 * @return true when all tests have passed. false otherwise.
536 */
537 static bool test_ctrl_match_fc(void)
538 {
539 bool pass = true;
540 struct ctrl_args reference = {0};
541 struct ctrl_args candidate = {0};
542
543 printf("test_ctrl_match_fc:\n");
544
545 /*******************************************************************/
546 /* Reference ID 1 */
547 set_ctrl_args(&reference, "fc", "21:00:00:e0:8b:05:05:01", "4420", "21:00:00:e0:8b:05:05:20", NULL, NULL, NULL);
548
549 set_ctrl_args(&candidate, "fc", "21:00:00:e0:8b:05:05:01", "4420", "21:00:00:e0:8b:05:05:20", NULL, NULL, NULL);
550 pass &= ctrl_match("FC", 1, 0, &reference, &candidate, true);
551
552 set_ctrl_args(&candidate, "fc", "21:00:00:e0:8b:05:05:01", "4420", NULL, NULL, NULL, NULL);
553 pass &= ctrl_match("FC", 1, 1, &reference, &candidate, true);
554
555 set_ctrl_args(&candidate, "fc", "21:00:00:e0:8b:05:05:01", "4420", NULL, "eth0", NULL, NULL);
556 pass &= ctrl_match("FC", 1, 2, &reference, &candidate, true);
557
558 set_ctrl_args(&candidate, "fc", "192.168.2.2", "4420", NULL, "eth0", NULL, NULL);
559 pass &= ctrl_match("FC", 1, 3, &reference, &candidate, false);
560
561 set_ctrl_args(&candidate, "fc", "21:00:00:e0:8b:05:05:01", "8009", NULL, NULL, NULL, NULL);
562 pass &= ctrl_match("FC", 1, 4, &reference, &candidate, false);
563
564 set_ctrl_args(&candidate, "fc", "21:00:00:e0:8b:05:05:01", "4420", "21:00:00:e0:8b:05:05:21", NULL, NULL, NULL);
565 pass &= ctrl_match("FC", 1, 5, &reference, &candidate, false);
566
567 set_ctrl_args(&candidate, "fc", "21:00:00:e0:8b:05:05:01", "8009", NULL, "", NULL, NULL);
568 pass &= ctrl_match("FC", 1, 6, &reference, &candidate, false);
569
570
571 /*******************************************************************/
572 /* Reference ID 2 */
573 set_ctrl_args(&reference, "fc", "21:00:00:e0:8b:05:05:01", "4420", NULL, NULL, NULL, NULL);
574
575 set_ctrl_args(&candidate, "fc", "21:00:00:e0:8b:05:05:01", "4420", "21:00:00:e0:8b:05:05:20", NULL, NULL, NULL);
576 pass &= ctrl_match("FC", 2, 0, &reference, &candidate, true);
577
578 set_ctrl_args(&candidate, "fc", "21:00:00:e0:8b:05:05:01", "4420", NULL, NULL, NULL, NULL);
579 pass &= ctrl_match("FC", 2, 1, &reference, &candidate, true);
580
581 set_ctrl_args(&candidate, "fc", "21:00:00:e0:8b:05:05:01", "4420", NULL, "eth0", NULL, NULL);
582 pass &= ctrl_match("FC", 2, 2, &reference, &candidate, true);
583
584 set_ctrl_args(&candidate, "fc", "192.168.2.2", "4420", NULL, "eth0", NULL, NULL);
585 pass &= ctrl_match("FC", 2, 3, &reference, &candidate, false);
586
587 set_ctrl_args(&candidate, "fc", "21:00:00:e0:8b:05:05:01", "8009", NULL, NULL, NULL, NULL);
588 pass &= ctrl_match("FC", 2, 4, &reference, &candidate, false);
589
590 set_ctrl_args(&candidate, "fc", "21:00:00:e0:8b:05:05:01", "8009", NULL, "", NULL, NULL);
591 pass &= ctrl_match("FC", 2, 5, &reference, &candidate, false);
592
593
594 /*******************************************************************/
595 /* Reference ID 3 */
596 set_ctrl_args(&reference, "fc", "21:00:00:e0:8b:05:05:01", "4420", NULL, "eth0", NULL, NULL);
597
598 set_ctrl_args(&candidate, "fc", "21:00:00:e0:8b:05:05:01", "4420", "21:00:00:e0:8b:05:05:20", NULL, NULL, NULL);
599 pass &= ctrl_match("FC", 3, 0, &reference, &candidate, true);
600
601 set_ctrl_args(&candidate, "fc", "21:00:00:e0:8b:05:05:01", "4420", NULL, NULL, NULL, NULL);
602 pass &= ctrl_match("FC", 3, 1, &reference, &candidate, true);
603
604 set_ctrl_args(&candidate, "fc", "21:00:00:e0:8b:05:05:01", "4420", NULL, "eth0", NULL, NULL);
605 pass &= ctrl_match("FC", 3, 2, &reference, &candidate, true);
606
607 set_ctrl_args(&candidate, "fc", "192.168.2.2", "4420", NULL, "eth0", NULL, NULL);
608 pass &= ctrl_match("FC", 3, 3, &reference, &candidate, false);
609
610 set_ctrl_args(&candidate, "fc", "21:00:00:e0:8b:05:05:01", "8009", NULL, NULL, NULL, NULL);
611 pass &= ctrl_match("FC", 3, 4, &reference, &candidate, false);
612
613 set_ctrl_args(&candidate, "fc", "21:00:00:e0:8b:05:05:01", "8009", NULL, "", NULL, NULL);
614 pass &= ctrl_match("FC", 3, 5, &reference, &candidate, false);
615
616 return pass;
617 }
618
619 /**
620 * test_ctrl_match_rdma - Test that we can look up RDMA controllers
621 *
622 * @return true when all tests have passed. false otherwise.
623 */
624 static bool test_ctrl_match_rdma(void)
625 {
626 bool pass = true;
627 struct ctrl_args reference = {0};
628 struct ctrl_args candidate = {0};
629
630 printf("test_ctrl_match_rdma:\n");
631
632 /*******************************************************************/
633 /* Reference ID 1 */
634 set_ctrl_args(&reference, "rdma", "192.168.2.1", "4420", "192.168.1.20", NULL, NULL, NULL);
635
636 set_ctrl_args(&candidate, "rdma", "192.168.2.1", "4420", "192.168.1.20", NULL, NULL, NULL);
637 pass &= ctrl_match("RDMA", 1, 0, &reference, &candidate, true);
638
639 set_ctrl_args(&candidate, "rdma", "192.168.2.1", "4420", NULL, NULL, NULL, NULL);
640 pass &= ctrl_match("RDMA", 1, 1, &reference, &candidate, true);
641
642 set_ctrl_args(&candidate, "rdma", "192.168.2.1", "4420", NULL, "eth0", NULL, NULL);
643 pass &= ctrl_match("RDMA", 1, 2, &reference, &candidate, true);
644
645 set_ctrl_args(&candidate, "rdma", "192.168.2.2", "4420", NULL, "eth0", NULL, NULL);
646 pass &= ctrl_match("RDMA", 1, 3, &reference, &candidate, false);
647
648 set_ctrl_args(&candidate, "rdma", "192.168.2.1", "8009", NULL, NULL, NULL, NULL);
649 pass &= ctrl_match("RDMA", 1, 4, &reference, &candidate, false);
650
651 set_ctrl_args(&candidate, "rdma", "192.168.2.1", "4420", "192.168.1.21", NULL, NULL, NULL);
652 pass &= ctrl_match("RDMA", 1, 5, &reference, &candidate, false);
653
654 set_ctrl_args(&candidate, "rdma", "192.168.2.1", "8009", NULL, "", NULL, NULL);
655 pass &= ctrl_match("RDMA", 1, 6, &reference, &candidate, false);
656
657
658 /*******************************************************************/
659 /* Reference ID 2 */
660 set_ctrl_args(&reference, "rdma", "192.168.2.1", "4420", NULL, NULL, NULL, NULL);
661
662 set_ctrl_args(&candidate, "rdma", "192.168.2.1", "4420", "192.168.1.20", NULL, NULL, NULL);
663 pass &= ctrl_match("RDMA", 2, 0, &reference, &candidate, true);
664
665 set_ctrl_args(&candidate, "rdma", "192.168.2.1", "4420", NULL, NULL, NULL, NULL);
666 pass &= ctrl_match("RDMA", 2, 1, &reference, &candidate, true);
667
668 set_ctrl_args(&candidate, "rdma", "192.168.2.1", "4420", NULL, "eth0", NULL, NULL);
669 pass &= ctrl_match("RDMA", 2, 2, &reference, &candidate, true);
670
671 set_ctrl_args(&candidate, "rdma", "192.168.2.2", "4420", NULL, "eth0", NULL, NULL);
672 pass &= ctrl_match("RDMA", 2, 3, &reference, &candidate, false);
673
674 set_ctrl_args(&candidate, "rdma", "192.168.2.1", "8009", NULL, NULL, NULL, NULL);
675 pass &= ctrl_match("RDMA", 2, 4, &reference, &candidate, false);
676
677 set_ctrl_args(&candidate, "rdma", "192.168.2.1", "8009", NULL, "", NULL, NULL);
678 pass &= ctrl_match("RDMA", 2, 5, &reference, &candidate, false);
679
680
681 /*******************************************************************/
682 /* Reference ID 3 */
683 set_ctrl_args(&reference, "rdma", "192.168.2.1", "4420", NULL, "eth0", NULL, NULL);
684
685 set_ctrl_args(&candidate, "rdma", "192.168.2.1", "4420", "192.168.1.20", NULL, NULL, NULL);
686 pass &= ctrl_match("RDMA", 3, 0, &reference, &candidate, true);
687
688 set_ctrl_args(&candidate, "rdma", "192.168.2.1", "4420", NULL, NULL, NULL, NULL);
689 pass &= ctrl_match("RDMA", 3, 1, &reference, &candidate, true);
690
691 set_ctrl_args(&candidate, "rdma", "192.168.2.1", "4420", NULL, "eth0", NULL, NULL);
692 pass &= ctrl_match("RDMA", 3, 2, &reference, &candidate, true);
693
694 set_ctrl_args(&candidate, "rdma", "192.168.2.2", "4420", NULL, "eth0", NULL, NULL);
695 pass &= ctrl_match("RDMA", 3, 3, &reference, &candidate, false);
696
697 set_ctrl_args(&candidate, "rdma", "192.168.2.1", "8009", NULL, NULL, NULL, NULL);
698 pass &= ctrl_match("RDMA", 3, 4, &reference, &candidate, false);
699
700 set_ctrl_args(&candidate, "rdma", "192.168.2.1", "8009", NULL, "", NULL, NULL);
701 pass &= ctrl_match("RDMA", 3, 5, &reference, &candidate, false);
702
703 return pass;
704 }
705
706 /**
707 * test_ctrl_match_tcp - Test that we can look up TCP controllers
708 *
709 * @note: The mocked getifaddrs() returns 2 interface entries with the
710 * following addresses. Therefore the tests must use IP addresses
711 * that match these.
712 *
713 * eth0
714 * \_ 192.168.1.20
715 * \_ fe80::dead:beef
716 *
717 * lo
718 * \_ 127.0.0.1
719 * \_ ::1
720 *
721 * @return true when all tests have passed. false otherwise.
722 */
723 static bool test_ctrl_match_tcp()
724 {
725 bool pass = true;
726 struct ctrl_args reference = {0};
727 struct ctrl_args candidate = {0};
728
729 printf("\n"
730 "test_ctrl_match_tcp:\n");
731
732 /*******************************************************************/
733 /* IPv4: Reference ID 1 */
734 set_ctrl_args(&reference, "tcp", "123.123.123.123", "8009", NULL, NULL, NULL, NULL);
735
736 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, NULL, NULL, NULL);
737 pass &= ctrl_match("IPv4", 1, 0, &reference, &candidate, true);
738 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", NULL, NULL, NULL);
739 pass &= ctrl_match("IPv4", 1, 1, &reference, &candidate, true);
740 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, "eth0", NULL, NULL);
741 pass &= ctrl_match("IPv4", 1, 2, &reference, &candidate, true);
742 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", "eth0", NULL, NULL);
743 pass &= ctrl_match("IPv4", 1, 3, &reference, &candidate, true);
744 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", NULL, NULL, NULL);
745 pass &= ctrl_match("IPv4", 1, 4, &reference, &candidate, true);
746 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", "eth0", NULL, NULL);
747 pass &= ctrl_match("IPv4", 1, 5, &reference, &candidate, true);
748 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, "lo", NULL, NULL);
749 pass &= ctrl_match("IPv4", 1, 6, &reference, &candidate, true);
750 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", "lo", NULL, NULL);
751 pass &= ctrl_match("IPv4", 1, 7, &reference, &candidate, true);
752 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", "lo", NULL, NULL);
753 pass &= ctrl_match("IPv4", 1, 8, &reference, &candidate, true);
754
755 /*******************************************************************/
756 /* IPv4: Reference ID 2 */
757 set_ctrl_args(&reference, "tcp", "123.123.123.123", "8009", "192.168.1.20", NULL, NULL, NULL);
758
759 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, NULL, NULL, NULL);
760 pass &= ctrl_match("IPv4", 2, 0, &reference, &candidate, true);
761 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", NULL, NULL, NULL);
762 pass &= ctrl_match("IPv4", 2, 1, &reference, &candidate, true);
763 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, "eth0", NULL, NULL);
764 pass &= ctrl_match("IPv4", 2, 2, &reference, &candidate, true);
765 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", "eth0", NULL, NULL);
766 pass &= ctrl_match("IPv4", 2, 3, &reference, &candidate, true);
767 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", NULL, NULL, NULL);
768 pass &= ctrl_match("IPv4", 2, 4, &reference, &candidate, false);
769 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", "eth0", NULL, NULL);
770 pass &= ctrl_match("IPv4", 2, 5, &reference, &candidate, false);
771 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, "lo", NULL, NULL);
772 pass &= ctrl_match("IPv4", 2, 6, &reference, &candidate, false);
773 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", "lo", NULL, NULL);
774 pass &= ctrl_match("IPv4", 2, 7, &reference, &candidate, false);
775 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", "lo", NULL, NULL);
776 pass &= ctrl_match("IPv4", 2, 8, &reference, &candidate, false);
777
778 /*******************************************************************/
779 /* IPv4: Reference ID 3 */
780 set_ctrl_args(&reference, "tcp", "123.123.123.123", "8009", NULL, "eth0", NULL, NULL);
781
782 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, NULL, NULL, NULL);
783 pass &= ctrl_match("IPv4", 3, 0, &reference, &candidate, true);
784 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", NULL, NULL, NULL);
785 pass &= ctrl_match("IPv4", 3, 1, &reference, &candidate, true);
786 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, "eth0", NULL, NULL);
787 pass &= ctrl_match("IPv4", 3, 2, &reference, &candidate, true);
788 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", "eth0", NULL, NULL);
789 pass &= ctrl_match("IPv4", 3, 3, &reference, &candidate, true);
790 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", NULL, NULL, NULL);
791 pass &= ctrl_match("IPv4", 3, 4, &reference, &candidate, false);
792 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", "eth0", NULL, NULL);
793 pass &= ctrl_match("IPv4", 3, 5, &reference, &candidate, false);
794 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, "lo", NULL, NULL);
795 pass &= ctrl_match("IPv4", 3, 6, &reference, &candidate, false);
796 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", "lo", NULL, NULL);
797 pass &= ctrl_match("IPv4", 3, 7, &reference, &candidate, false);
798 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", "lo", NULL, NULL);
799 pass &= ctrl_match("IPv4", 3, 8, &reference, &candidate, false);
800
801 /*******************************************************************/
802 /* IPv4: Reference ID 4 */
803 set_ctrl_args(&reference, "tcp", "123.123.123.123", "8009", "192.168.1.20", "eth0", NULL, NULL);
804
805 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, NULL, NULL, NULL);
806 pass &= ctrl_match("IPv4", 4, 0, &reference, &candidate, true);
807 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", NULL, NULL, NULL);
808 pass &= ctrl_match("IPv4", 4, 1, &reference, &candidate, true);
809 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, "eth0", NULL, NULL);
810 pass &= ctrl_match("IPv4", 4, 2, &reference, &candidate, true);
811 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", "eth0", NULL, NULL);
812 pass &= ctrl_match("IPv4", 4, 3, &reference, &candidate, true);
813 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", NULL, NULL, NULL);
814 pass &= ctrl_match("IPv4", 4, 4, &reference, &candidate, false);
815 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", "eth0", NULL, NULL);
816 pass &= ctrl_match("IPv4", 4, 5, &reference, &candidate, false);
817 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, "lo", NULL, NULL);
818 pass &= ctrl_match("IPv4", 4, 6, &reference, &candidate, false);
819 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", "lo", NULL, NULL);
820 pass &= ctrl_match("IPv4", 4, 7, &reference, &candidate, false);
821 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", "lo", NULL, NULL);
822 pass &= ctrl_match("IPv4", 4, 8, &reference, &candidate, false);
823
824 /*******************************************************************/
825 /* IPv4: Reference ID 5 */
826 set_ctrl_args(&reference, "tcp", "123.123.123.123", "8009", "192.168.1.21", NULL, NULL, NULL);
827
828 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, NULL, NULL, NULL);
829 pass &= ctrl_match("IPv4", 5, 0, &reference, &candidate, true);
830 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", NULL, NULL, NULL);
831 pass &= ctrl_match("IPv4", 5, 1, &reference, &candidate, false);
832 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, "eth0", NULL, NULL);
833 pass &= ctrl_match("IPv4", 5, 2, &reference, &candidate, false);
834 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", "eth0", NULL, NULL);
835 pass &= ctrl_match("IPv4", 5, 3, &reference, &candidate, false);
836 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", NULL, NULL, NULL);
837 pass &= ctrl_match("IPv4", 5, 4, &reference, &candidate, true);
838 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", "eth0", NULL, NULL);
839 pass &= ctrl_match("IPv4", 5, 5, &reference, &candidate, false);
840 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, "lo", NULL, NULL);
841 pass &= ctrl_match("IPv4", 5, 6, &reference, &candidate, false);
842 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", "lo", NULL, NULL);
843 pass &= ctrl_match("IPv4", 5, 7, &reference, &candidate, false);
844 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", "lo", NULL, NULL);
845 pass &= ctrl_match("IPv4", 5, 8, &reference, &candidate, false);
846
847 /*******************************************************************/
848 /* IPv4: Reference ID 6 */
849 set_ctrl_args(&reference, "tcp", "123.123.123.123", "8009", NULL, NULL, "traddr=123.123.123.123,trsvcid=8009,src_addr=192.168.1.20", NULL);
850
851 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, NULL, NULL, NULL);
852 pass &= ctrl_match("IPv4", 6, 0, &reference, &candidate, true);
853 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", NULL, NULL, NULL);
854 pass &= ctrl_match("IPv4", 6, 1, &reference, &candidate, true);
855 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, "eth0", NULL, NULL);
856 pass &= ctrl_match("IPv4", 6, 2, &reference, &candidate, true);
857 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", "eth0", NULL, NULL);
858 pass &= ctrl_match("IPv4", 6, 3, &reference, &candidate, true);
859 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", NULL, NULL, NULL);
860 pass &= ctrl_match("IPv4", 6, 4, &reference, &candidate, false);
861 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", "eth0", NULL, NULL);
862 pass &= ctrl_match("IPv4", 6, 5, &reference, &candidate, false);
863 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, "lo", NULL, NULL);
864 pass &= ctrl_match("IPv4", 6, 6, &reference, &candidate, false);
865 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", "lo", NULL, NULL);
866 pass &= ctrl_match("IPv4", 6, 7, &reference, &candidate, false);
867 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", "lo", NULL, NULL);
868 pass &= ctrl_match("IPv4", 6, 8, &reference, &candidate, false);
869
870 /*******************************************************************/
871 /* IPv4: Reference ID 7 */
872 set_ctrl_args(&reference, "tcp", "123.123.123.123", "8009", NULL, NULL, "traddr=123.123.123.123,trsvcid=8009,src_addr=127.0.0.1", NULL);
873
874 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, NULL, NULL, NULL);
875 pass &= ctrl_match("IPv4", 7, 0, &reference, &candidate, true);
876 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", NULL, NULL, NULL);
877 pass &= ctrl_match("IPv4", 7, 1, &reference, &candidate, false);
878 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, "eth0", NULL, NULL);
879 pass &= ctrl_match("IPv4", 7, 2, &reference, &candidate, false);
880 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", "eth0", NULL, NULL);
881 pass &= ctrl_match("IPv4", 7, 3, &reference, &candidate, false);
882 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", NULL, NULL, NULL);
883 pass &= ctrl_match("IPv4", 7, 4, &reference, &candidate, false);
884 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", "eth0", NULL, NULL);
885 pass &= ctrl_match("IPv4", 7, 5, &reference, &candidate, false);
886 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, "lo", NULL, NULL);
887 pass &= ctrl_match("IPv4", 7, 6, &reference, &candidate, true);
888 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", "lo", NULL, NULL);
889 pass &= ctrl_match("IPv4", 7, 7, &reference, &candidate, false);
890 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", "lo", NULL, NULL);
891 pass &= ctrl_match("IPv4", 7, 8, &reference, &candidate, false);
892
893 /*******************************************************************/
894 /* IPv6: Reference ID 1 */
895 set_ctrl_args(&reference, "tcp", "aaaa::bbbb", "8009", NULL, NULL, NULL, NULL);
896
897 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, NULL, NULL, NULL);
898 pass &= ctrl_match("IPv6", 1, 0, &reference, &candidate, true);
899 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", NULL, NULL, NULL);
900 pass &= ctrl_match("IPv6", 1, 1, &reference, &candidate, true);
901 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, "eth0", NULL, NULL);
902 pass &= ctrl_match("IPv6", 1, 2, &reference, &candidate, true);
903 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", "eth0", NULL, NULL);
904 pass &= ctrl_match("IPv6", 1, 3, &reference, &candidate, true);
905 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", NULL, NULL, NULL);
906 pass &= ctrl_match("IPv6", 1, 4, &reference, &candidate, true);
907 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", "eth0", NULL, NULL);
908 pass &= ctrl_match("IPv6", 1, 5, &reference, &candidate, true);
909 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, "lo", NULL, NULL);
910 pass &= ctrl_match("IPv6", 1, 6, &reference, &candidate, true);
911 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", "lo", NULL, NULL);
912 pass &= ctrl_match("IPv6", 1, 7, &reference, &candidate, true);
913 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", "lo", NULL, NULL);
914 pass &= ctrl_match("IPv6", 1, 8, &reference, &candidate, true);
915
916 /*******************************************************************/
917 /* IPv6: Reference ID 2 */
918 set_ctrl_args(&reference, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", NULL, NULL, NULL);
919
920 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, NULL, NULL, NULL);
921 pass &= ctrl_match("IPv6", 2, 0, &reference, &candidate, true);
922 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", NULL, NULL, NULL);
923 pass &= ctrl_match("IPv6", 2, 1, &reference, &candidate, true);
924 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, "eth0", NULL, NULL);
925 pass &= ctrl_match("IPv6", 2, 2, &reference, &candidate, true);
926 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", "eth0", NULL, NULL);
927 pass &= ctrl_match("IPv6", 2, 3, &reference, &candidate, true);
928 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", NULL, NULL, NULL);
929 pass &= ctrl_match("IPv6", 2, 4, &reference, &candidate, false);
930 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", "eth0", NULL, NULL);
931 pass &= ctrl_match("IPv6", 2, 5, &reference, &candidate, false);
932 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, "lo", NULL, NULL);
933 pass &= ctrl_match("IPv6", 2, 6, &reference, &candidate, false);
934 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", "lo", NULL, NULL);
935 pass &= ctrl_match("IPv6", 2, 7, &reference, &candidate, false);
936 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", "lo", NULL, NULL);
937 pass &= ctrl_match("IPv6", 2, 8, &reference, &candidate, false);
938
939 /*******************************************************************/
940 /* IPv6: Reference ID 3 */
941 set_ctrl_args(&reference, "tcp", "aaaa::bbbb", "8009", NULL, "eth0", NULL, NULL);
942
943 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, NULL, NULL, NULL);
944 pass &= ctrl_match("IPv6", 3, 0, &reference, &candidate, true);
945 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", NULL, NULL, NULL);
946 pass &= ctrl_match("IPv6", 3, 1, &reference, &candidate, true);
947 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, "eth0", NULL, NULL);
948 pass &= ctrl_match("IPv6", 3, 2, &reference, &candidate, true);
949 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", "eth0", NULL, NULL);
950 pass &= ctrl_match("IPv6", 3, 3, &reference, &candidate, true);
951 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", NULL, NULL, NULL);
952 pass &= ctrl_match("IPv6", 3, 4, &reference, &candidate, false);
953 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", "eth0", NULL, NULL);
954 pass &= ctrl_match("IPv6", 3, 5, &reference, &candidate, false);
955 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, "lo", NULL, NULL);
956 pass &= ctrl_match("IPv6", 3, 6, &reference, &candidate, false);
957 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", "lo", NULL, NULL);
958 pass &= ctrl_match("IPv6", 3, 7, &reference, &candidate, false);
959 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", "lo", NULL, NULL);
960 pass &= ctrl_match("IPv6", 3, 8, &reference, &candidate, false);
961
962 /*******************************************************************/
963 /* IPv6: Reference ID 4 */
964 set_ctrl_args(&reference, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", "eth0", NULL, NULL);
965
966 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, NULL, NULL, NULL);
967 pass &= ctrl_match("IPv6", 4, 0, &reference, &candidate, true);
968 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", NULL, NULL, NULL);
969 pass &= ctrl_match("IPv6", 4, 1, &reference, &candidate, true);
970 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, "eth0", NULL, NULL);
971 pass &= ctrl_match("IPv6", 4, 2, &reference, &candidate, true);
972 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", "eth0", NULL, NULL);
973 pass &= ctrl_match("IPv6", 4, 3, &reference, &candidate, true);
974 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", NULL, NULL, NULL);
975 pass &= ctrl_match("IPv6", 4, 4, &reference, &candidate, false);
976 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", "eth0", NULL, NULL);
977 pass &= ctrl_match("IPv6", 4, 5, &reference, &candidate, false);
978 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, "lo", NULL, NULL);
979 pass &= ctrl_match("IPv6", 4, 6, &reference, &candidate, false);
980 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", "lo", NULL, NULL);
981 pass &= ctrl_match("IPv6", 4, 7, &reference, &candidate, false);
982 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", "lo", NULL, NULL);
983 pass &= ctrl_match("IPv6", 4, 8, &reference, &candidate, false);
984
985 /*******************************************************************/
986 /* IPv6: Reference ID 5 */
987 set_ctrl_args(&reference, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", NULL, NULL, NULL);
988
989 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, NULL, NULL, NULL);
990 pass &= ctrl_match("IPv6", 5, 0, &reference, &candidate, true);
991 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", NULL, NULL, NULL);
992 pass &= ctrl_match("IPv6", 5, 1, &reference, &candidate, false);
993 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, "eth0", NULL, NULL);
994 pass &= ctrl_match("IPv6", 5, 2, &reference, &candidate, false);
995 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", "eth0", NULL, NULL);
996 pass &= ctrl_match("IPv6", 5, 3, &reference, &candidate, false);
997 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", NULL, NULL, NULL);
998 pass &= ctrl_match("IPv6", 5, 4, &reference, &candidate, true);
999 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", "eth0", NULL, NULL);
1000 pass &= ctrl_match("IPv6", 5, 5, &reference, &candidate, false);
1001 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, "lo", NULL, NULL);
1002 pass &= ctrl_match("IPv6", 5, 6, &reference, &candidate, false);
1003 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", "lo", NULL, NULL);
1004 pass &= ctrl_match("IPv6", 5, 7, &reference, &candidate, false);
1005 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", "lo", NULL, NULL);
1006 pass &= ctrl_match("IPv6", 5, 8, &reference, &candidate, false);
1007
1008 /*******************************************************************/
1009 /* IPv6: Reference ID 6 */
1010 set_ctrl_args(&reference, "tcp", "aaaa::bbbb", "8009", NULL, NULL, "traddr=aaaa::bbbb,trsvcid=8009,src_addr=fe80::dead:beef", NULL);
1011
1012 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, NULL, NULL, NULL);
1013 pass &= ctrl_match("IPv6", 6, 0, &reference, &candidate, true);
1014 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", NULL, NULL, NULL);
1015 pass &= ctrl_match("IPv6", 6, 1, &reference, &candidate, true);
1016 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, "eth0", NULL, NULL);
1017 pass &= ctrl_match("IPv6", 6, 2, &reference, &candidate, true);
1018 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", "eth0", NULL, NULL);
1019 pass &= ctrl_match("IPv6", 6, 3, &reference, &candidate, true);
1020 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", NULL, NULL, NULL);
1021 pass &= ctrl_match("IPv6", 6, 4, &reference, &candidate, false);
1022 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", "eth0", NULL, NULL);
1023 pass &= ctrl_match("IPv6", 6, 5, &reference, &candidate, false);
1024 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, "lo", NULL, NULL);
1025 pass &= ctrl_match("IPv6", 6, 6, &reference, &candidate, false);
1026 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", "lo", NULL, NULL);
1027 pass &= ctrl_match("IPv6", 6, 7, &reference, &candidate, false);
1028 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", "lo", NULL, NULL);
1029 pass &= ctrl_match("IPv6", 6, 8, &reference, &candidate, false);
1030
1031 /*******************************************************************/
1032 /* IPv6: Reference ID 7 */
1033 set_ctrl_args(&reference, "tcp", "aaaa::bbbb", "8009", NULL, NULL, "traddr=aaaa::bbbb,trsvcid=8009,src_addr=::1", NULL);
1034
1035 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, NULL, NULL, NULL);
1036 pass &= ctrl_match("IPv6", 7, 0, &reference, &candidate, true);
1037 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", NULL, NULL, NULL);
1038 pass &= ctrl_match("IPv6", 7, 1, &reference, &candidate, false);
1039 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, "eth0", NULL, NULL);
1040 pass &= ctrl_match("IPv6", 7, 2, &reference, &candidate, false);
1041 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", "eth0", NULL, NULL);
1042 pass &= ctrl_match("IPv6", 7, 3, &reference, &candidate, false);
1043 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", NULL, NULL, NULL);
1044 pass &= ctrl_match("IPv6", 7, 4, &reference, &candidate, false);
1045 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", "eth0", NULL, NULL);
1046 pass &= ctrl_match("IPv6", 7, 5, &reference, &candidate, false);
1047 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", NULL, "lo", NULL, NULL);
1048 pass &= ctrl_match("IPv6", 7, 6, &reference, &candidate, true);
1049 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:beef", "lo", NULL, NULL);
1050 pass &= ctrl_match("IPv6", 7, 7, &reference, &candidate, false);
1051 set_ctrl_args(&candidate, "tcp", "aaaa::bbbb", "8009", "fe80::dead:cafe", "lo", NULL, NULL);
1052 pass &= ctrl_match("IPv6", 7, 8, &reference, &candidate, false);
1053
1054 return pass;
1055 }
1056
1057 static bool ctrl_config_match(const char *tag,
1058 int reference_id,
1059 int candidate_id,
1060 struct ctrl_args *reference,
1061 struct ctrl_args *candidate,
1062 bool should_match)
1063 {
1064 bool match;
1065 nvme_root_t r;
1066 nvme_host_t h;
1067 nvme_ctrl_t reference_ctrl; /* Existing controller (from sysfs) */
1068 nvme_subsystem_t s;
1069
1070 r = nvme_create_root(stdout, LOG_INFO);
1071 assert(r);
1072
1073 h = nvme_default_host(r);
1074 assert(h);
1075
1076 s = nvme_lookup_subsystem(h, DEFAULT_SUBSYSNAME, reference->subsysnqn ? reference->subsysnqn : DEFAULT_SUBSYSNQN);
1077 assert(s);
1078
1079 reference_ctrl = nvme_lookup_ctrl(s, reference->transport, reference->traddr,
1080 reference->host_traddr, reference->host_iface,
1081 reference->trsvcid, NULL);
1082 assert(reference_ctrl);
1083 reference_ctrl->name = "nvme1"; /* fake the device name */
1084 if (reference->address) {
1085 reference_ctrl->address = (char *)reference->address;
1086 }
1087
1088 match = nvme_ctrl_config_match(reference_ctrl, candidate->transport, candidate->traddr,
1089 candidate->trsvcid, candidate->subsysnqn,
1090 candidate->host_traddr, candidate->host_iface);
1091
1092 if (should_match) {
1093 if (!match) {
1094 printf("%s-%d-%d: Failed to match config for Candidate (%s, %s, %s, %s, %s, %s)\n",
1095 tag, reference_id, candidate_id,
1096 candidate->transport, candidate->traddr, candidate->trsvcid,
1097 candidate->subsysnqn, candidate->host_traddr, candidate->host_iface);
1098 return false;
1099 }
1100 } else {
1101 if (match) {
1102 printf("%s-%d-%d: Config should not have matched for Candidate (%s, %s, %s, %s, %s, %s)\n",
1103 tag, reference_id, candidate_id,
1104 candidate->transport, candidate->traddr, candidate->trsvcid,
1105 candidate->subsysnqn, candidate->host_traddr, candidate->host_iface);
1106 return false;
1107 }
1108 }
1109
1110 /* Set the faked data back to NULL before freeing the tree */
1111 reference_ctrl->name = NULL;
1112 reference_ctrl->address = NULL;
1113
1114 nvme_free_tree(r);
1115
1116 return true;
1117 }
1118
1119 static bool test_ctrl_config_match()
1120 {
1121 bool pass = true;
1122 struct ctrl_args reference = {0};
1123 struct ctrl_args candidate = {0};
1124
1125 printf("\n"
1126 "test_ctrl_config_match:\n");
1127
1128 set_ctrl_args(&reference, "tcp", "123.123.123.123", "8009", NULL, NULL, NULL, NULL);
1129
1130 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, NULL, NULL, NULL);
1131 pass &= ctrl_config_match("IPv4", 1, 0, &reference, &candidate, true);
1132 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", NULL, NULL, NULL);
1133 pass &= ctrl_config_match("IPv4", 1, 1, &reference, &candidate, true);
1134 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, "eth0", NULL, NULL);
1135 pass &= ctrl_config_match("IPv4", 1, 2, &reference, &candidate, true);
1136 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", "eth0", NULL, NULL);
1137 pass &= ctrl_config_match("IPv4", 1, 3, &reference, &candidate, true);
1138 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", NULL, NULL, NULL);
1139 pass &= ctrl_config_match("IPv4", 1, 4, &reference, &candidate, true);
1140 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", "eth0", NULL, NULL);
1141 pass &= ctrl_config_match("IPv4", 1, 5, &reference, &candidate, true);
1142 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", NULL, "lo", NULL, NULL);
1143 pass &= ctrl_config_match("IPv4", 1, 6, &reference, &candidate, true);
1144 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.20", "lo", NULL, NULL);
1145 pass &= ctrl_config_match("IPv4", 1, 7, &reference, &candidate, true);
1146 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", "lo", NULL, NULL);
1147 pass &= ctrl_config_match("IPv4", 1, 8, &reference, &candidate, true);
1148 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", "lo", NULL, "hello");
1149 pass &= ctrl_config_match("IPv4", 1, 9, &reference, &candidate, false);
1150 set_ctrl_args(&candidate, "tcp", "123.123.123.123", "8009", "192.168.1.21", "lo", NULL, DEFAULT_SUBSYSNQN);
1151 pass &= ctrl_config_match("IPv4", 1, 9, &reference, &candidate, true);
1152
1153 return pass;
1154 }
1155
1156
1157 /**
1158 * This test module uses a mocked ifaddrs library (mock-ifaddrs.c)
1159 * such that there are 2 fake interfaces (eth0 and lo) with the
1160 * following IP addresses:
1161 *
1162 * - eth0
1163 * \_ IPv4: 192.168.1.20
1164 * \_ IPv6: fe80::dead:beef
1165 *
1166 * - lo
1167 * \_ IPv4: 127.0.0.1
1168 * \_ IPv6: ::1
1169 */
1170 int main(int argc, char *argv[])
1171 {
1172 bool pass = true;
1173
1174 pass &= test_lookup();
1175 pass &= test_src_addr();
1176 pass &= test_ctrl_match_fc();
1177 pass &= test_ctrl_match_rdma();
1178 pass &= test_ctrl_match_tcp();
1179 pass &= test_ctrl_config_match();
1180
1181 fflush(stdout);
1182
1183 exit(pass ? EXIT_SUCCESS : EXIT_FAILURE);
1184 }