Debian Codegraph @

examples/discover-loop.c

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /**
3 * This file is part of libnvme.
4 * Copyright (c) 2020 Western Digital Corporation or its affiliates.
5 *
6 * Authors: Keith Busch <[email protected]>
7 */
8
9 /**
10 * discover-loop: Use fabrics commands to discover any loop targets and print
11 * those records. You must have at least one configured nvme loop target on the
12 * system (no existing connection required). The output will look more
13 * interesting with more targets.
14 */
15 #define __SANE_USERSPACE_TYPES__
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <libnvme.h>
22
23 #include <ccan/endian/endian.h>
24
25 static void print_discover_log(struct nvmf_discovery_log *log)
26 {
27 int i, numrec = le64_to_cpu(log->numrec);
28
29 printf(".\n");
30 printf("|-- genctr:%llx\n", log->genctr);
31 printf("|-- numrec:%x\n", numrec);
32 printf("`-- recfmt:%x\n", log->recfmt);
33
34 for (i = 0; i < numrec; i++) {
35 struct nvmf_disc_log_entry *e = &log->entries[i];
36
37 printf(" %c-- Entry:%d\n", (i < numrec - 1) ? '|' : '`', i);
38 printf(" %c |-- trtype:%x\n", (i < numrec - 1) ? '|' : ' ', e->trtype);
39 printf(" %c |-- adrfam:%x\n", (i < numrec - 1) ? '|' : ' ', e->adrfam);
40 printf(" %c |-- subtype:%x\n", (i < numrec - 1) ? '|' : ' ', e->subtype);
41 printf(" %c |-- treq:%x\n", (i < numrec - 1) ? '|' : ' ', e->treq);
42 printf(" %c |-- portid:%x\n", (i < numrec - 1) ? '|' : ' ', e->portid);
43 printf(" %c |-- cntlid:%x\n", (i < numrec - 1) ? '|' : ' ', e->cntlid);
44 printf(" %c |-- asqsz:%x\n", (i < numrec - 1) ? '|' : ' ', e->asqsz);
45 printf(" %c |-- trsvcid:%s\n", (i < numrec - 1) ? '|' : ' ', e->trsvcid);
46 printf(" %c |-- subnqn:%s\n", (i < numrec - 1) ? '|' : ' ', e->subnqn);
47 printf(" %c `-- traddr:%s\n", (i < numrec - 1) ? '|' : ' ', e->traddr);
48 }
49 }
50
51 int main()
52 {
53 struct nvmf_discovery_log *log = NULL;
54 nvme_root_t r;
55 nvme_host_t h;
56 nvme_ctrl_t c;
57 int ret;
58 struct nvme_fabrics_config cfg;
59
60 nvmf_default_config(&cfg);
61
62 r = nvme_scan(NULL);
63 h = nvme_default_host(r);
64 if (!h) {
65 fprintf(stderr, "Failed to allocated memory\n");
66 return ENOMEM;
67 }
68 c = nvme_create_ctrl(r, NVME_DISC_SUBSYS_NAME, "loop",
69 NULL, NULL, NULL, NULL);
70 if (!c) {
71 fprintf(stderr, "Failed to allocate memory\n");
72 return ENOMEM;
73 }
74 ret = nvmf_add_ctrl(h, c, &cfg);
75 if (ret < 0) {
76 fprintf(stderr, "no controller found\n");
77 return errno;
78 }
79
80 ret = nvmf_get_discovery_log(c, &log, 4);
81 nvme_disconnect_ctrl(c);
82 nvme_free_ctrl(c);
83
84 if (ret)
85 fprintf(stderr, "nvmf-discover-log:%x\n", ret);
86 else
87 print_discover_log(log);
88
89 nvme_free_tree(r);
90 free(log);
91 return 0;
92 }