forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvme.c
5866 lines (5141 loc) · 152 KB
/
nvme.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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* nvme.c -- NVM-Express command line utility.
*
* Copyright (c) 2014-2015, Intel Corporation.
*
* Written by Keith Busch <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* This program uses NVMe IOCTLs to run native nvme commands to a device.
*/
#include "nvme/tree.h"
#include "nvme/types.h"
#include <errno.h>
#include <getopt.h>
#include <fcntl.h>
#include <inttypes.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <dirent.h>
#include <libgen.h>
#ifdef LIBHUGETLBFS
#include <hugetlbfs.h>
#endif
#include <linux/fs.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "common.h"
#include "nvme.h"
#include "libnvme.h"
#include "nvme-print.h"
#include "plugin.h"
#include "argconfig.h"
#include "fabrics.h"
#define CREATE_CMD
#include "nvme-builtin.h"
static struct stat nvme_stat;
const char *devicename;
static const char nvme_version_string[] = NVME_VERSION;
static struct plugin builtin = {
.commands = commands,
.name = NULL,
.desc = NULL,
.next = NULL,
.tail = &builtin,
};
static struct program nvme = {
.name = "nvme",
.version = nvme_version_string,
.usage = "<command> [<device>] [<args>]",
.desc = "The '<device>' may be either an NVMe character "\
"device (ex: /dev/nvme0) or an nvme block device "\
"(ex: /dev/nvme0n1).",
.extensions = &builtin,
};
const char *output_format = "Output format: normal|json|binary";
static const char *output_format_no_binary = "Output format: normal|json";
static void *mmap_registers(nvme_root_t r, const char *dev);
static void *__nvme_alloc(size_t len, bool *huge) {
void *p;
if (!posix_memalign(&p, getpagesize(), len)) {
*huge = false;
memset(p, 0, len);
return p;
}
return NULL;
}
#define HUGE_MIN 0x80000
#ifdef LIBHUGETLBFS
void nvme_free(void *p, bool huge)
{
if (huge) {
if (p)
free_hugepage_region(p);
}
else
free(p);
}
void *nvme_alloc(size_t len, bool *huge)
{
void *p;
if (len < HUGE_MIN)
return __nvme_alloc(len, huge);
p = get_hugepage_region(len, GHR_DEFAULT);
if (!p)
return __nvme_alloc(len, huge);
*huge = true;
return p;
}
#else
void nvme_free(void *p, bool huge)
{
free(p);
}
void *nvme_alloc(size_t len, bool *huge)
{
return __nvme_alloc(len, huge);
}
#endif
static bool is_chardev(void)
{
return S_ISCHR(nvme_stat.st_mode);
}
static bool is_blkdev(void)
{
return S_ISBLK(nvme_stat.st_mode);
}
static int open_dev(char *dev)
{
int err, fd;
devicename = basename(dev);
err = open(dev, O_RDONLY);
if (err < 0)
goto perror;
fd = err;
err = fstat(fd, &nvme_stat);
if (err < 0) {
close(fd);
goto perror;
}
if (!is_chardev() && !is_blkdev()) {
fprintf(stderr, "%s is not a block or character device\n", dev);
close(fd);
return -ENODEV;
}
return fd;
perror:
perror(dev);
return err;
}
static int check_arg_dev(int argc, char **argv)
{
if (optind >= argc) {
errno = EINVAL;
perror(argv[0]);
return -EINVAL;
}
return 0;
}
static int get_dev(int argc, char **argv)
{
int ret;
ret = check_arg_dev(argc, argv);
if (ret)
return ret;
return open_dev(argv[optind]);
}
int parse_and_open(int argc, char **argv, const char *desc,
const struct argconfig_commandline_options *opts)
{
int ret;
ret = argconfig_parse(argc, argv, desc, opts);
if (ret)
return ret;
ret = get_dev(argc, argv);
if (ret < 0)
argconfig_print_help(desc, opts);
return ret;
}
enum nvme_print_flags validate_output_format(const char *format)
{
if (!format)
return -EINVAL;
if (!strcmp(format, "normal"))
return NORMAL;
if (!strcmp(format, "json"))
return JSON;
if (!strcmp(format, "binary"))
return BINARY;
return -EINVAL;
}
static int get_smart_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
struct nvme_smart_log smart_log;
const char *desc = "Retrieve SMART log for the given device "\
"(or optionally a namespace) in either decoded format "\
"(default) or binary.";
const char *namespace = "(optional) desired namespace";
const char *raw = "output in binary format";
const char *human_readable = "show info in readable format";
enum nvme_print_flags flags;
int err = -1, fd;
struct config {
__u32 namespace_id;
int raw_binary;
char *output_format;
int human_readable;
};
struct config cfg = {
.namespace_id = NVME_NSID_ALL,
.output_format = "normal",
};
OPT_ARGS(opts) = {
OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace),
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw),
OPT_FLAG("human-readable", 'H', &cfg.human_readable, human_readable),
OPT_END()
};
err = fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
goto ret;
err = flags = validate_output_format(cfg.output_format);
if (flags < 0)
goto close_fd;
if (cfg.raw_binary)
flags = BINARY;
if (cfg.human_readable)
flags |= VERBOSE;
err = nvme_get_log_smart(fd, cfg.namespace_id, true, &smart_log);
if (!err)
nvme_show_smart_log(&smart_log, cfg.namespace_id, devicename,
flags);
else if (err > 0)
nvme_show_status(err);
else
perror("smart log");
close_fd:
close(fd);
ret:
return nvme_status_to_errno(err, false);
}
static int get_ana_log(int argc, char **argv, struct command *cmd,
struct plugin *plugin)
{
const char *desc = "Retrieve ANA log for the given device in " \
"decoded format (default), json or binary.";
void *ana_log;
int err = -1, fd;
int groups = 0; /* Right now get all the per ANA group NSIDS */
size_t ana_log_len;
struct nvme_id_ctrl ctrl;
enum nvme_print_flags flags;
enum nvme_log_ana_lsp lsp;
struct config {
char *output_format;
};
struct config cfg = {
.output_format = "normal",
};
OPT_ARGS(opts) = {
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_END()
};
err = fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
goto ret;
err = flags = validate_output_format(cfg.output_format);
if (flags < 0)
goto close_fd;
err = nvme_identify_ctrl(fd, &ctrl);
if (err) {
fprintf(stderr, "ERROR : nvme_identify_ctrl() failed 0x%x\n",
err);
goto close_fd;
}
ana_log_len = sizeof(struct nvme_ana_log) +
le32_to_cpu(ctrl.nanagrpid) * sizeof(struct nvme_ana_group_desc);
if (!(ctrl.anacap & (1 << 6)))
ana_log_len += le32_to_cpu(ctrl.mnan) * sizeof(__le32);
ana_log = malloc(ana_log_len);
if (!ana_log) {
perror("malloc");
err = -ENOMEM;
goto close_fd;
}
lsp = groups ? NVME_LOG_ANA_LSP_RGO_GROUPS_ONLY :
NVME_LOG_ANA_LSP_RGO_NAMESPACES;
err = nvme_get_log_ana(fd, lsp, true, 0, ana_log_len, ana_log);
if (!err) {
nvme_show_ana_log(ana_log, devicename, flags, ana_log_len);
} else if (err > 0)
nvme_show_status(err);
else
perror("ana-log");
free(ana_log);
close_fd:
close(fd);
ret:
return nvme_status_to_errno(err, false);
}
static int get_telemetry_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve telemetry log and write to binary file";
const char *fname = "File name to save raw binary, includes header";
const char *hgen = "Have the host tell the controller to generate the report";
const char *cgen = "Gather report generated by the controller.";
const char *dgen = "Pick which telemetry data area to report. Default is all. Valid options are 1, 2, 3.";
struct nvme_telemetry_log *log;
int err = 0, fd, output;
unsigned total_size;
struct config {
char *file_name;
__u32 host_gen;
int ctrl_init;
int data_area;
};
struct config cfg = {
.file_name = NULL,
.host_gen = 1,
.ctrl_init = 0,
.data_area = 3,
};
OPT_ARGS(opts) = {
OPT_FILE("output-file", 'o', &cfg.file_name, fname),
OPT_UINT("host-generate", 'g', &cfg.host_gen, hgen),
OPT_FLAG("controller-init", 'c', &cfg.ctrl_init, cgen),
OPT_UINT("data-area", 'd', &cfg.data_area, dgen),
OPT_END()
};
err = fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
goto ret;
if (!cfg.file_name) {
fprintf(stderr, "Please provide an output file!\n");
err = -EINVAL;
goto close_fd;
}
cfg.host_gen = !!cfg.host_gen;
output = open(cfg.file_name, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (output < 0) {
fprintf(stderr, "Failed to open output file %s: %s!\n",
cfg.file_name, strerror(errno));
err = output;
goto free_mem;
}
if (cfg.ctrl_init)
err = nvme_get_ctrl_telemetry(fd, true, &log);
else if (cfg.host_gen)
err = nvme_get_new_host_telemetry(fd, &log);
else
err = nvme_get_host_telemetry(fd, &log);
if (err < 0)
perror("get-telemetry-log");
else if (err > 0) {
nvme_show_status(err);
fprintf(stderr, "Failed to acquire telemetry log %d!\n", err);
goto close_output;
}
switch (cfg.data_area) {
case 1:
total_size = (le16_to_cpu(log->dalb1) + 1) * NVME_LOG_TELEM_BLOCK_SIZE;
break;
case 2:
total_size = (le16_to_cpu(log->dalb2) + 1) * NVME_LOG_TELEM_BLOCK_SIZE;
break;
case 3:
default:
total_size = (le16_to_cpu(log->dalb3) + 1) * NVME_LOG_TELEM_BLOCK_SIZE;
break;
}
err = write(output, (void *)log, total_size);
if (err != total_size) {
fprintf(stderr, "Failed to flush all data to file!\n");
goto close_output;
}
close_output:
close(output);
free_mem:
free(log);
close_fd:
close(fd);
ret:
return nvme_status_to_errno(err, false);
}
static int get_endurance_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
struct nvme_endurance_group_log endurance_log;
const char *desc = "Retrieves endurance groups log page and prints the log.";
const char *group_id = "The endurance group identifier";
enum nvme_print_flags flags;
int err, fd;
struct config {
char *output_format;
__u16 group_id;
};
struct config cfg = {
.output_format = "normal",
.group_id = 0,
};
OPT_ARGS(opts) = {
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_SHRT("group-id", 'g', &cfg.group_id, group_id),
OPT_END()
};
err = fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
goto ret;
err = flags = validate_output_format(cfg.output_format);
if (flags < 0)
goto close_fd;
err = nvme_get_log_endurance_group(fd, cfg.group_id, &endurance_log);
if (!err)
nvme_show_endurance_log(&endurance_log, cfg.group_id, devicename, flags);
else if (err > 0)
nvme_show_status(err);
else
perror("endurance log");
close_fd:
close(fd);
ret:
return nvme_status_to_errno(err, false);
}
static int collect_effects_log(int fd, enum nvme_csi csi, nvme_effects_log_node_t **node, int flags)
{
int err;
nvme_effects_log_node_t *n;
n = malloc(sizeof(nvme_effects_log_node_t));
if (!n) {
perror("Failed to allocate memory");
return -1;
}
n->csi = csi;
err = nvme_get_log_cmd_effects(fd, csi, &n->effects);
if (!err) {
*node = n;
return 0;
} else if (err > 0)
nvme_show_status(err);
else
perror("effects log page");
free(n);
return -1;
}
static int get_effects_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve command effects log page and print the table.";
const char *raw = "show log in binary format";
const char *human_readable = "show log in readable format";
const char *csi = "";
nvme_effects_log_node_t *nodes[3] = { NULL };
int idx = 0;
void *bar = NULL;
int err = -1, fd;
enum nvme_print_flags flags;
struct config {
int raw_binary;
int human_readable;
char *output_format;
int csi;
};
struct config cfg = {
.output_format = "normal",
.csi = -1,
};
OPT_ARGS(opts) = {
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_FLAG("human-readable",'H', &cfg.human_readable, human_readable),
OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw),
OPT_INT("csi", 'c', &cfg.csi, csi),
OPT_END()
};
err = fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
goto ret;
err = flags = validate_output_format(cfg.output_format);
if (flags < 0)
goto close_fd;
if (cfg.raw_binary)
flags = BINARY;
if (cfg.human_readable)
flags |= VERBOSE;
if (cfg.csi < 0) {
nvme_root_t nvme_root;
uint64_t cap_value;
int nvme_command_set_supported;
int other_command_sets_supported;
nvme_root = nvme_scan(NULL);
bar = mmap_registers(nvme_root, devicename);
nvme_free_tree(nvme_root);
if (!bar) {
goto close_fd;
}
cap_value = nvme_mmio_read64(bar + NVME_REG_CAP);
munmap(bar, getpagesize());
nvme_command_set_supported = (cap_value & (1UL << 37)) != 0;
other_command_sets_supported = (cap_value & (1UL << (37+6))) != 0;
if (nvme_command_set_supported) {
if (collect_effects_log(fd, NVME_CSI_NVM, &nodes[idx], flags) == 0)
idx++;
}
if (other_command_sets_supported) {
collect_effects_log(fd, NVME_CSI_ZNS, &nodes[idx], flags);
}
nvme_print_effects_log_pages(nodes, flags);
}
else {
collect_effects_log(fd, cfg.csi, &nodes[0], flags);
nvme_print_effects_log_pages(nodes, flags);
}
for (idx = 0; nodes[idx]; idx++)
free(nodes[idx]);
close_fd:
close(fd);
ret:
return nvme_status_to_errno(err, false);
}
static int get_error_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve specified number of "\
"error log entries from a given device "\
"in either decoded format (default) or binary.";
const char *log_entries = "number of entries to retrieve";
const char *raw = "dump in binary format";
struct nvme_error_log_page *err_log;
struct nvme_id_ctrl ctrl;
enum nvme_print_flags flags;
int err = -1, fd;
struct config {
__u32 log_entries;
int raw_binary;
char *output_format;
};
struct config cfg = {
.log_entries = 64,
.output_format = "normal",
};
OPT_ARGS(opts) = {
OPT_UINT("log-entries", 'e', &cfg.log_entries, log_entries),
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw),
OPT_END()
};
err = fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
goto ret;
err = flags = validate_output_format(cfg.output_format);
if (flags < 0)
goto close_fd;
if (cfg.raw_binary)
flags = BINARY;
if (!cfg.log_entries) {
fprintf(stderr, "non-zero log-entries is required param\n");
errno = EINVAL;
err = -1;
goto close_fd;
}
err = nvme_identify_ctrl(fd, &ctrl);
if (err < 0) {
perror("identify controller");
goto close_fd;
} else if (err) {
fprintf(stderr, "could not identify controller\n");
errno = ENODEV;
err = -1;
goto close_fd;
}
cfg.log_entries = min(cfg.log_entries, ctrl.elpe + 1);
err_log = calloc(cfg.log_entries, sizeof(struct nvme_error_log_page));
if (!err_log) {
perror("could not alloc buffer for error log\n");
errno = ENOMEM;
err = -1;
goto close_fd;
}
err = nvme_get_log_error(fd, cfg.log_entries, true, err_log);
if (!err)
nvme_show_error_log(err_log, cfg.log_entries, devicename, flags);
else if (err > 0)
nvme_show_status(err);
else
perror("error log");
free(err_log);
close_fd:
close(fd);
ret:
return nvme_status_to_errno(err, false);
}
static int get_fw_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve the firmware log for the "\
"specified device in either decoded format (default) or binary.";
const char *raw = "use binary output";
struct nvme_firmware_slot fw_log;
enum nvme_print_flags flags;
int err, fd;
struct config {
int raw_binary;
char *output_format;
};
struct config cfg = {
.output_format = "normal",
};
OPT_ARGS(opts) = {
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw),
OPT_END()
};
err = fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
goto ret;
err = flags = validate_output_format(cfg.output_format);
if (flags < 0)
goto close_fd;
if (cfg.raw_binary)
flags = BINARY;
err = nvme_get_log_fw_slot(fd, true, &fw_log);
if (!err)
nvme_show_fw_log(&fw_log, devicename, flags);
else if (err > 0)
nvme_show_status(err);
else
perror("fw log");
close_fd:
close(fd);
ret:
return nvme_status_to_errno(err, false);
}
static int get_changed_ns_list_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve Changed Namespaces log for the given device "\
"in either decoded format "\
"(default) or binary.";
const char *raw = "output in binary format";
struct nvme_ns_list changed_ns_list_log;
enum nvme_print_flags flags;
int err, fd;
struct config {
int raw_binary;
char *output_format;
};
struct config cfg = {
.output_format = "normal",
};
OPT_ARGS(opts) = {
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw),
OPT_END()
};
err = fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
goto ret;
err = flags = validate_output_format(cfg.output_format);
if (flags < 0)
goto close_fd;
if (cfg.raw_binary)
flags = BINARY;
err = nvme_get_log_changed_ns_list(fd, true, &changed_ns_list_log);
if (!err)
nvme_show_changed_ns_list_log(&changed_ns_list_log, devicename,
flags);
else if (err > 0)
nvme_show_status(err);
else
perror("changed ns list log");
close_fd:
close(fd);
ret:
return nvme_status_to_errno(err, false);
}
static int get_pred_lat_per_nvmset_log(int argc, char **argv,
struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve Predictable latency per nvm set log "\
"page and prints it for the given device in either decoded " \
"format(default),json or binary.";
const char *nvmset_id = "NVM Set Identifier";
const char *raw = "use binary output";
struct nvme_nvmset_predictable_lat_log plpns_log;
enum nvme_print_flags flags;
int err, fd;
struct config {
__u16 nvmset_id;
char *output_format;
int raw_binary;
};
struct config cfg = {
.nvmset_id = 1,
.output_format = "normal",
};
OPT_ARGS(opts) = {
OPT_SHRT("nvmset-id", 'i', &cfg.nvmset_id, nvmset_id),
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw),
OPT_END()
};
err = fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
goto ret;
err = flags = validate_output_format(cfg.output_format);
if (flags < 0)
goto close_fd;
if (cfg.raw_binary)
flags = BINARY;
err = nvme_get_log_predictable_lat_nvmset(fd, cfg.nvmset_id, &plpns_log);
if (!err)
nvme_show_predictable_latency_per_nvmset(&plpns_log,
cfg.nvmset_id, devicename, flags);
else if (err > 0)
nvme_show_status(err);
else
perror("predictable latency per nvm set");
close_fd:
close(fd);
ret:
return nvme_status_to_errno(err, false);
}
static int get_pred_lat_event_agg_log(int argc, char **argv,
struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve Predictable Latency Event" \
"Aggregate Log page and prints it, for the given" \
"device in either decoded format(default)," \
"json or binary.";
const char *log_entries = "Number of pending NVM Set" \
"log Entries list";
const char *rae = "Retain an Asynchronous Event";
const char *raw = "use binary output";
enum nvme_print_flags flags;
struct nvme_id_ctrl ctrl;
__u32 log_size;
void *pea_log;
int err, fd;
struct config {
__u64 log_entries;
bool rae;
char *output_format;
int raw_binary;
};
struct config cfg = {
.log_entries = 2044,
.rae = false,
.output_format = "normal",
};
OPT_ARGS(opts) = {
OPT_UINT("log-entries", 'e', &cfg.log_entries, log_entries),
OPT_FLAG("rae", 'r', &cfg.rae, rae),
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw),
OPT_END()
};
err = fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
goto ret;
err = flags = validate_output_format(cfg.output_format);
if (flags < 0)
goto close_fd;
if (cfg.raw_binary)
flags = BINARY;
if (!cfg.log_entries) {
fprintf(stderr, "non-zero log-entries is required param\n");
err = -EINVAL;
goto close_fd;
}
err = nvme_identify_ctrl(fd, &ctrl);
if (err < 0) {
perror("identify controller");
goto close_fd;
} else if (err) {
nvme_show_status(err);
goto close_fd;
}
cfg.log_entries = min(cfg.log_entries, le32_to_cpu(ctrl.nsetidmax));
log_size = sizeof(__u64) + cfg.log_entries * sizeof(__u16);
pea_log = calloc(log_size, 1);
if (!pea_log) {
perror("could not alloc buffer for predictable " \
"latency event agggregate log entries\n");
err = -ENOMEM;
goto close_fd;
}
err = nvme_get_log_predictable_lat_event(fd, cfg.rae, 0, log_size, pea_log);
if (!err)
nvme_show_predictable_latency_event_agg_log(pea_log, cfg.log_entries,
log_size, devicename, flags);
else if (err > 0)
nvme_show_status(err);
else
perror("predictable latency event gggregate log page");
free(pea_log);
close_fd:
close(fd);
ret:
return nvme_status_to_errno(err, false);
}
static int get_persistent_event_log(int argc, char **argv,
struct command *cmd, struct plugin *plugin)
{
const char *desc = "Retrieve Persistent Event log info for"\
" the given device in either decoded format(default),"\
" json or binary.";
const char *action = "action the controller shall take during"\
" processing this persistent log page command.";
const char *log_len = "number of bytes to retrieve";
const char *raw = "use binary output";
struct nvme_persistent_event_log *pevent;
enum nvme_print_flags flags;
void *pevent_log_info;
int err, fd;
bool huge;
struct config {
__u8 action;
__u32 log_len;
int raw_binary;
char *output_format;
};
struct config cfg = {
.action = 0xff,
.log_len = 0,
.output_format = "normal",
};
OPT_ARGS(opts) = {
OPT_BYTE("action", 'a', &cfg.action, action),
OPT_UINT("log_len", 'l', &cfg.log_len, log_len),
OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
OPT_FLAG("raw-binary", 'b', &cfg.raw_binary, raw),
OPT_END()
};
err = fd = parse_and_open(argc, argv, desc, opts);
if (fd < 0)
goto ret;
err = flags = validate_output_format(cfg.output_format);
if (flags < 0)
goto close_fd;
if (cfg.raw_binary)
flags = BINARY;
pevent = calloc(sizeof(*pevent), 1);
if (!pevent) {
perror("could not alloc buffer for persistent " \
"event log header\n");
err = -ENOMEM;
goto close_fd;
}
err = nvme_get_log_persistent_event(fd, cfg.action,
sizeof(*pevent), pevent);
if (err < 0) {
perror("persistent event log");
goto close_fd;
} else if (err) {
nvme_show_status(err);
goto close_fd;
}
if (cfg.action == NVME_PEVENT_LOG_RELEASE_CTX) {
printf("Releasing Persistent Event Log Context\n");
goto close_fd;
}