-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsnvme.c
662 lines (554 loc) · 14.7 KB
/
lsnvme.c
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <inttypes.h>
#include <ctype.h>
#include <getopt.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <fcntl.h>
#include <mntent.h>
#include <libgen.h>
#include <libudev.h>
// these are moving around
#include <linux/nvme.h>
//#include <uapi/linux/nvme_ioctl.h>
#define TAB " "
#define TEE "├─"
#define ELB "└─"
static const char NVME[] = "nvme";
static const char *SYS = "/sys";
static const char *DEV = "/dev";
static struct udev *udev;
enum {
SZ_B,
SZ_KB,
SZ_MB,
SZ_GB,
SZ_TB,
SZ_AUTO,
};
/* opts and default values */
static struct options {
int sz;
int verbose;
bool disp_ctrl;
bool disp_devs;
bool disp_tree;
bool disp_machine;
int headers;
} opts = {
SZ_AUTO, /* determine size */
0, /* verbosity */
false, /* display controllers */
true, /* display block devs */
false, /* display as tree */
false, /* machine readable output */
0, /* print headers */
};
static struct size_spec {
unsigned long int div;
char suffix;
} disk_sizes[] = {
[SZ_B] = { 1, 'B' },
[SZ_KB] = { 1024, 'K' },
[SZ_MB] = { 1024 * 1000, 'M' },
[SZ_GB] = { 1024 * 1000 * 1000, 'G' },
[SZ_TB] = { 0xee6b280000ULL, 'T' },
[SZ_AUTO] = { 0, 0 },
};
/*
* TODO: make this char buffer the correct size
*/
static char *read_str(const char *path)
{
static char read_str[32] = {0};
int fd = open(path, O_RDONLY|O_NONBLOCK);
if (fd < 0)
return NULL;
if (read(fd, read_str, 32) < 0) {
close(fd);
return NULL;
}
close(fd);
return read_str;
}
// search parents until grandfather for driver
static const char *find_driver(struct udev_device *node)
{
unsigned int count = 3;
const char *p = udev_device_get_driver(node);
struct udev_device *parent = node;
while (p == NULL && count > 0) {
parent = udev_device_get_parent(parent);
p = udev_device_get_driver(parent);
--count;
}
return p;
}
// given absolute path, return udev_device or null
// mostly borrows from udevadm implementation
static struct udev_device *find_device(char *path)
{
if (!path)
return NULL;
if (strncmp(path, DEV, strlen(DEV)) == 0) {
struct stat statbuf;
char type;
if (stat(path, &statbuf) < 0)
return NULL;
if (S_ISBLK(statbuf.st_mode))
type = 'b';
else if (S_ISCHR(statbuf.st_mode))
type = 'c';
else
return NULL;
return udev_device_new_from_devnum(udev, type, statbuf.st_rdev);
} else if (strncmp(path, SYS, strlen(SYS)) == 0) {
size_t sz = strlen(path);
while (sz > 0 && path[sz-1] == '/') {
path[sz-1] = 0;
--sz;
}
return udev_device_new_from_syspath(udev, path);
} else {
return NULL;
}
}
void lsnvme_printss_header(const char *tab)
{
printf("%s[dev:ns]\tdevtype\tvendor\tmodel\trevision\tdev\tsize\n",
tab);
}
static int find_sz(unsigned long long total)
{
int s = SZ_AUTO-1;
for (; s >= 0 && disk_sizes[s].div > total; --s) {}
return s;
}
/*
* Get size or return "-"
* Max size supported right now is 36000 TB
* TODO: support Terabyte size
*/
static char *bd_size(struct udev_device *dev)
{
static char size_str[32];
char path[128];
char *nptr, *endptr;
unsigned long long int sectors;
double total;
int ret;
strncpy(path, udev_device_get_syspath(dev), 128);
strcat(path, "/size");
nptr = read_str(path);
if (!nptr)
return "-";
sectors = strtoull(nptr, &endptr, 10);
// no valid digits || overflow
if ((sectors == 0 && nptr == endptr ) ||
(sectors == ULLONG_MAX && errno == ERANGE))
return "-";
// too big: ~1 >> 55
if (sectors > 0x7fffffffffffffULL)
return "-";
sectors <<= 9;
if (opts.sz == SZ_AUTO)
opts.sz = find_sz(sectors);
if (opts.sz == SZ_B) {
ret = snprintf(size_str, 32, "%llu", sectors);
} else {
total = (double)(sectors) / disk_sizes[opts.sz].div;
ret = snprintf(size_str, 32, "%.2f%c",
total, disk_sizes[opts.sz].suffix);
}
if (ret >= 32)
return "-";
return size_str;
}
static int lsnvme_identify_ns(struct udev_device *dev, struct nvme_id_ns *ptr)
{
const char *devpath = udev_device_get_devnode(dev);
uint32_t ns = atoi(udev_device_get_sysnum(dev));
int fd = open(devpath, O_RDONLY|O_NONBLOCK);
if (fd < 0) {
perror(devpath);
return errno;
}
struct nvme_admin_cmd cmd = {
.opcode = nvme_admin_identify,
.nsid = ns,
.addr = (uint64_t) ptr,
.data_len = 4096,
.cdw10 = 0,
};
return ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd);
}
static const char *lsnvme_query_hwdb(struct udev_device *dev,
const char *key)
{
static struct udev_hwdb *hwdb = NULL;
struct udev_list_entry *list, *current;
const char *value = NULL;
const char *modalias = NULL;
// check if value is cached in dev object
value = udev_device_get_property_value(dev, key);
if (value)
return value;
modalias = udev_device_get_property_value(dev, "MODALIAS");
if (!modalias)
return "-";
if (hwdb == NULL)
hwdb = udev_hwdb_new(udev);
list = udev_hwdb_get_properties_list_entry(hwdb, modalias, 0);
current = udev_list_entry_get_by_name(list, key);
value = udev_list_entry_get_value(current);
return value ? strdup(value) : "-";
}
static int lsnvme_identify_ctrl(struct udev_device *dev,
struct nvme_id_ctrl *ptr)
{
const char *devpath = udev_device_get_devnode(dev);
int fd = open(devpath, O_RDONLY|O_NONBLOCK);
if (fd < 0) {
perror(devpath);
return errno;
}
struct nvme_admin_cmd cmd = {
.opcode = nvme_admin_identify,
.addr = (uint64_t) ptr,
.data_len = 4096,
.cdw10 = 1,
};
return ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd);
}
void lsnvme_printctrl_id(struct nvme_id_ctrl *id)
{
printf("%sPCI Vendor ID: %x\n", TAB, id->vid);
printf("%sPCI Subsystem Vendor ID: %x\n", TAB, id->ssvid);
printf("%sSerial Number: %.20s\n", TAB, id->sn);
printf("%sModel Number: %.20s\n", TAB, id->mn);
printf("%sFirmware Revision: %.20s\n", TAB, id->fr);
printf("%sIEEE OUI Identifier: %02x%02x%02x\n",
TAB, id->ieee[2], id->ieee[1], id->ieee[0]);
printf("%sController ID: %x\n", TAB, id->cntlid);
printf("%sVersion: %x\n", TAB, id->ver);
printf("%sNumber of Namespaces: %d\n", TAB, id->nn);
}
void lsnvme_printctrl_ns(struct nvme_id_ns *ns)
{
printf("%sNamespace Size: %"PRIu64"\n",
TAB, (uint64_t)le64toh(ns->nsze));
printf("%sNamespace Capacity: %"PRIu64"\n",
TAB, (uint64_t)le64toh(ns->ncap));
printf("%sNamespace Utilization: %"PRIu64"\n",
TAB, (uint64_t)le64toh(ns->nuse));
printf("%sNVM Capacity: %"PRIu64"\n",
TAB, (uint64_t)le64toh(ns->nvmcap));
}
/*
* [dev:ns] device_file devtype size <vendor model revision>
*/
void lsnvme_printbd(struct udev_device *dev, const char *tab)
{
printf("[%s:%s]\t%s\t%s\t%s\t%s\t%s\t%s\n",
udev_device_get_sysnum(udev_device_get_parent(dev)),
udev_device_get_sysnum(dev),
udev_device_get_devnode(dev),
udev_device_get_devtype(dev),
bd_size(dev),
lsnvme_query_hwdb(dev, "ID_VENDOR"),
lsnvme_query_hwdb(dev, "ID_MODEL"),
lsnvme_query_hwdb(dev, "ID_REVISION")
);
if (opts.verbose) {
struct nvme_id_ns ns;
if(lsnvme_identify_ns(dev, &ns))
fprintf(stderr, "%sioctl failed on: %s\n",
TAB, udev_device_get_devnode(dev));
else
lsnvme_printctrl_ns(&ns);
}
}
/*
* [dev:ns:pn] device_file devtype size (part type?)
*/
void lsnvme_printpart(struct udev_device *dev, const char *tab)
{
struct udev_device *parent = udev_device_get_parent(dev);
printf("[%s:%s:%s]\t%s\t%s\t%s\n",
udev_device_get_sysnum(udev_device_get_parent(parent)),
udev_device_get_sysnum(udev_device_get_parent(dev)),
udev_device_get_sysnum(dev),
udev_device_get_devnode(dev),
udev_device_get_devtype(dev),
bd_size(dev)
);
}
/*
* [dev] device_file vendor model bus driver (transport?)
*/
void lsnvme_printctrl(struct udev_device *dev)
{
struct udev_device *pdev = udev_device_get_parent(dev);
printf("[%s]\t%s\t%s\t%s\t%s\t%s\n",
udev_device_get_sysnum(dev),
udev_device_get_devnode(dev),
lsnvme_query_hwdb(pdev, "ID_VENDOR_FROM_DATABASE"),
lsnvme_query_hwdb(pdev, "ID_MODEL_FROM_DATABASE"),
udev_device_get_subsystem(pdev),
find_driver(dev)
);
if (opts.verbose) {
struct nvme_id_ctrl id;
if(lsnvme_identify_ctrl(dev, &id))
fprintf(stderr, "%sioctl failed on: %s\n",
TAB, udev_device_get_devnode(dev));
else
lsnvme_printctrl_id(&id);
}
}
static int lsnvme_ls(char *path)
{
struct udev_device *dev = find_device(path);
const char *dt;
if (dev == NULL)
return EXIT_FAILURE;
dt = udev_device_get_devtype(dev);
if (strcmp(udev_device_get_subsystem(dev), NVME) == 0)
lsnvme_printctrl(dev);
else if (dt && strcmp(dt, "partition"))
lsnvme_printbd(dev, "");
else
lsnvme_printpart(dev, "");
return EXIT_SUCCESS;
}
static int lsnvme_enum_devs(struct udev_device *parent)
{
struct udev_enumerate *enum_children = udev_enumerate_new(udev);
struct udev_list_entry *devices, *dev_list_entry;
struct udev_device *cdev;
const char *path;
udev_enumerate_add_match_parent(enum_children, parent);
udev_enumerate_scan_devices(enum_children);
devices = udev_enumerate_get_list_entry(enum_children);
udev_list_entry_foreach(dev_list_entry, devices) {
path = udev_list_entry_get_name(dev_list_entry);
cdev = udev_device_new_from_syspath(udev, path);
const char *dt = udev_device_get_devtype(cdev);
/* skip parent */
if (udev_device_get_devnum(cdev) ==
udev_device_get_devnum(parent))
continue;
if (dt && strcmp(dt, "partition")) {
lsnvme_printbd(cdev, opts.disp_ctrl ? TAB : "");
} else {
lsnvme_printpart(cdev, "");
}
udev_device_unref(cdev);
}
udev_enumerate_unref(enum_children);
return EXIT_SUCCESS;
}
static int lsnvme_enum_ctrl(void)
{
struct udev_enumerate *enum_parents;
struct udev_list_entry *devices, *dev_list_entry;
struct udev_device *dev;
const char *path;
enum_parents = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(enum_parents, NVME);
udev_enumerate_scan_devices(enum_parents);
devices = udev_enumerate_get_list_entry(enum_parents);
udev_list_entry_foreach(dev_list_entry, devices) {
path = udev_list_entry_get_name(dev_list_entry);
dev = udev_device_new_from_syspath(udev, path);
if (opts.disp_ctrl)
lsnvme_printctrl(dev);
if (opts.disp_devs)
lsnvme_enum_devs(dev);
udev_device_unref(dev);
}
udev_enumerate_unref(enum_parents);
return EXIT_SUCCESS;
}
// TODO: don't allocate mem if mount points are equal?
static void lsnvme_get_mount_paths(void)
{
FILE *fp;
struct mntent *fs;
const char *mounts = "/proc/mounts";
fp = setmntent(mounts, "r");
if (fp == NULL) {
fprintf(stderr, "Could not open: %s\n", mounts);
return;
}
while ((fs = getmntent(fp)) != NULL)
if (strcmp(fs->mnt_type, "sysfs") == 0)
SYS = strdup(fs->mnt_dir);
else if (strcmp(fs->mnt_type, "devtmpfs") == 0)
DEV = strdup(fs->mnt_dir);
endmntent(fp);
}
static int version(const char *progr)
{
fprintf(stdout, "%s: 0.2\n", progr);
return EXIT_SUCCESS;
}
static void set_size(char size_spec)
{
switch (size_spec) {
case 'b':
case 'B':
opts.sz = SZ_B;
break;
case 'k':
case 'K':
opts.sz = SZ_KB;
break;
case 'M':
case 'm':
opts.sz = SZ_MB;
break;
case 'G':
case 'g':
opts.sz = SZ_GB;
break;
case 'T':
case 't':
opts.sz = SZ_TB;
break;
default:
opts.sz = SZ_AUTO;
break;
}
}
static struct option long_options[] = {
{"size", required_argument, 0, 's'},
{"host", optional_argument, 0, 'H'},
{"tree", no_argument, 0, 't'},
{"targets", no_argument, 0, 'T'},
{"discover", no_argument, 0, 'D'},
{"m", no_argument, 0, 'm'},
{"headers", no_argument, &opts.headers, 1},
{"version", no_argument, 0, 'V'},
{"verbose", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
{0,0,0,0}
};
static const char *help_strings[][2] = {
{"SIZE", "\tspecific size from [TGMKB], default: auto"},
{"", "\tdisplay host(s) attached to this target system"},
{"", "\tdisplay tree-like diagram if possible"},
{"", "\tlist targets attached to this host (WIP)"},
{"", "Go 'discover' resources available for a host (WIP)"},
{"", "\tmachine readable output"},
{"", "\tprint descriptive headers"},
{"", "\tdisplay version and exit"},
{"", "\tincrease verbosity level"},
{"", "\tdisplay this help and exit"},
{"", ""}
};
static int usage(const char *progr)
{
const struct option *ptr = long_options;
printf("\nUsage: %s [<switches>] [ devices.. ]\n", progr);
for (int i = 0; ptr->name != NULL; ++i, ptr = &long_options[i])
if (ptr->has_arg == required_argument)
printf(" -%c, --%s=%s%s\n", ptr->val, ptr->name,
help_strings[i][0], help_strings[i][1]);
else if (ptr->flag && ptr->val)
printf(" --%s\t%s\n", ptr->name,
help_strings[i][1]);
else
printf(" -%c, --%s\t%s\n", ptr->val, ptr->name,
help_strings[i][1]);
printf("\n");
return EXIT_SUCCESS;
}
int main(int argc, char *argv[])
{
int opt, option_index, ret = EXIT_SUCCESS;
while ((opt = getopt_long(argc, argv, "s:DHTtmVvh",
long_options, &option_index)) != -1) {
switch (opt) {
case 0: /* longopt only, may not be used */
printf("option: %s\n", long_options[option_index].name);
printf("%d\n", opts.headers);
break;
case 's':
set_size(optarg[0]);
break;
case 'H':
opts.disp_ctrl = true;
opts.disp_devs = false;
break;
case 't':
opts.disp_tree = true;
break;
case 'm':
opts.disp_machine = true;
break;
case 'V':
return version(argv[0]);
case 'v':
++opts.verbose;
break;
case 'D':
printf("lsnvme WIP...\"Go discover NVMe resources ");
printf("on this target system that\n");
printf("are available for a host/initiator.\"\n");
printf("OR\n");
printf("\"Go discover NVMe resources \'out there\' ");
printf("on the network for\nthe host/initiator.\"\n");
return ret;
case 'T':
printf("lsnvme WIP...\"Go list the NVMe subsystem ");
printf("targets and the allocated cntlid's\n");
printf("in each attached subsystem to ");
printf("this host/initiator.\"\n");
return ret;
case 'h':
case '?':
default:
return usage(argv[0]);
}
}
udev = udev_new();
if (udev == NULL) {
fprintf(stderr, "failed to initialize udev library, exiting\n");
return EXIT_FAILURE;
} else {
lsnvme_get_mount_paths();
}
/* if given a list of devices, print them, otherwise
* print all controllers */
if (optind < argc) {
while (optind < argc)
if(lsnvme_ls(argv[optind++]))
fprintf(stderr,
"%s: unable to get info for: %s\n",
argv[0], argv[optind-1]);
} else {
ret = lsnvme_enum_ctrl();
}
udev_unref(udev);
return ret;
}