]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme: add json output format for list_crl
authorGollu Appalanaidu <anaidu.gollu@samsung.com>
Sun, 16 May 2021 07:20:15 +0000 (12:50 +0530)
committerKeith Busch <kbusch@kernel.org>
Tue, 25 May 2021 13:58:22 +0000 (07:58 -0600)
Signed-off-by: Gollu Appalanaidu <anaidu.gollu@samsung.com>
Documentation/nvme-list-ctrl.1
Documentation/nvme-list-ctrl.html
Documentation/nvme-list-ctrl.txt
nvme-print.c
nvme-print.h
nvme.c

index ce644712f8e909c6bad6dce11b4ec7580204ccd5..3ab756e10b240a7722cdf29a01b37ae054f4ba75 100644 (file)
@@ -1,13 +1,13 @@
 '\" t
 .\"     Title: nvme-id-ns
-.\"    Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
-.\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
-.\"      Date: 10/20/2020
+.\"    Author: [FIXME: author] [see http://docbook.sf.net/el/author]
+.\" Generator: DocBook XSL Stylesheets v1.79.1 <http://docbook.sf.net/>
+.\"      Date: 05/16/2021
 .\"    Manual: NVMe Manual
 .\"    Source: NVMe
 .\"  Language: English
 .\"
-.TH "NVME\-ID\-NS" "1" "10/20/2020" "NVMe" "NVMe Manual"
+.TH "NVME\-ID\-NS" "1" "05/16/2021" "NVMe" "NVMe Manual"
 .\" -----------------------------------------------------------------
 .\" * Define some portability stuff
 .\" -----------------------------------------------------------------
@@ -33,7 +33,7 @@ nvme-list-ctrl \- Send NVMe Identify List Controllers, return result and structu
 .sp
 .nf
 \fInvme list\-ctrl\fR <device> [\-\-cntid=<cntid> | \-c <cntid>]
-                        [\-\-namespace\-id=<nsid> | \-n <nsid>]
+                        [\-\-namespace\-id=<nsid> | \-n <nsid>] [\-o <fmt> | \-\-output\-format=<fmt>]
 DESCRIPTION
 .fi
 .sp
@@ -62,6 +62,14 @@ Retrieve the identify list structure starting with the given controller id\&.
 .RS 4
 If provided, will request the controllers attached to the specified namespace\&. If no namespace is given, or set to 0, the command requests the controller list for the entire subsystem, whether or not they are attached to namespace(s)\&.
 .RE
+.PP
+\-o <format>, \-\-output\-format=<format>
+.RS 4
+Set the reporting format to
+\fInormal\fR
+or
+\fIjson\fR\&. Only one output format can be used at a time\&.
+.RE
 .SH "EXAMPLES"
 .sp
 No examples yet\&.
index c3aa941ffa96c1e1e18260e8194f096e51d22b9a..efde99ee2da6772ff548a81efedc11a47d8301c7 100644 (file)
@@ -1,9 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"\r
     "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">\r
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">\r
 <head>\r
 <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />\r
-<meta name="generator" content="AsciiDoc 8.6.8" />\r
+<meta name="generator" content="AsciiDoc 8.6.10" />\r
 <title>nvme-id-ns(1)</title>\r
 <style type="text/css">\r
 /* Shared CSS for AsciiDoc xhtml11 and html5 backends */\r
@@ -94,7 +95,9 @@ ul > li > * { color: black; }
   padding: 0;\r
   margin: 0;\r
 }\r
-\r
+pre {\r
+  white-space: pre-wrap;\r
+}\r
 \r
 #author {\r
   color: #527bbd;\r
@@ -223,7 +226,7 @@ div.exampleblock > div.content {
 }\r
 \r
 div.imageblock div.content { padding-left: 0; }\r
-span.image img { border-style: none; }\r
+span.image img { border-style: none; vertical-align: text-bottom; }\r
 a.image:visited { color: white; }\r
 \r
 dl {\r
@@ -433,7 +436,7 @@ thead, p.table.header {
 p.table {\r
   margin-top: 0;\r
 }\r
-/* Because the table frame attribute is overridden by CSS in most browsers. */\r
+/* Because the table frame attribute is overriden by CSS in most browsers. */\r
 div.tableblock > table[frame="void"] {\r
   border-style: none;\r
 }\r
@@ -747,7 +750,7 @@ nvme-id-ns(1) Manual Page
 <div class="sectionbody">\r
 <div class="verseblock">\r
 <pre class="content"><em>nvme list-ctrl</em> &lt;device&gt; [--cntid=&lt;cntid&gt; | -c &lt;cntid&gt;]\r
-                        [--namespace-id=&lt;nsid&gt; | -n &lt;nsid&gt;]\r
+                        [--namespace-id=&lt;nsid&gt; | -n &lt;nsid&gt;] [-o &lt;fmt&gt; | --output-format=&lt;fmt&gt;]\r
 DESCRIPTION</pre>\r
 <div class="attribution">\r
 </div></div>\r
@@ -793,6 +796,18 @@ OPTIONS</code></pre>
         whether or not they are attached to namespace(s).\r
 </p>\r
 </dd>\r
+<dt class="hdlist1">\r
+-o &lt;format&gt;\r
+</dt>\r
+<dt class="hdlist1">\r
+--output-format=&lt;format&gt;\r
+</dt>\r
+<dd>\r
+<p>\r
+        Set the reporting format to <em>normal</em> or <em>json</em>. Only one output\r
+        format can be used at a time.\r
+</p>\r
+</dd>\r
 </dl></div>\r
 </div>\r
 </div>\r
@@ -812,7 +827,8 @@ OPTIONS</code></pre>
 <div id="footnotes"><hr /></div>\r
 <div id="footer">\r
 <div id="footer-text">\r
-Last updated 2016-05-16 12:47:42 EDT\r
+Last updated\r
+ 2021-05-16 13:00:43 IST\r
 </div>\r
 </div>\r
 </body>\r
index b60afb77090335e0405d56535dca646ef0638a50..45707e737ad7d9bd76271172540eda4225318afb 100644 (file)
@@ -9,7 +9,7 @@ SYNOPSIS
 --------
 [verse]
 'nvme list-ctrl' <device> [--cntid=<cntid> | -c <cntid>]
-                       [--namespace-id=<nsid> | -n <nsid>]
+                       [--namespace-id=<nsid> | -n <nsid>] [-o <fmt> | --output-format=<fmt>]
 DESCRIPTION
 -----------
 For the NVMe device given, sends an identify command for controller list
@@ -37,6 +37,11 @@ OPTIONS
        command requests the controller list for the entire subsystem,
        whether or not they are attached to namespace(s).
 
+-o <format>::
+--output-format=<format>::
+       Set the reporting format to 'normal' or 'json'. Only one output
+       format can be used at a time.
+
 EXAMPLES
 --------
 No examples yet.
index 9dde48ada87c7e8ab8111878fb317fc4d562edc2..09a4dcbc1a85203855d95c9de7ab369a1dae36ee 100755 (executable)
@@ -4165,6 +4165,48 @@ void nvme_show_zns_report_zones(void *report, __u32 descs,
        }
 }
 
+static void json_nvme_list_ctrl(struct nvme_controller_list *ctrl_list, __u16 num)
+{
+       struct json_object *root;
+       struct json_object *valid_attrs;
+       struct json_object *valid;
+       int i;
+
+       root = json_create_object();
+       valid = json_create_array();
+
+       json_object_add_value_uint(root, "num_ctrl",
+               le16_to_cpu(ctrl_list->num));
+
+       for (i = 0; i < min(num, 2047); i++) {
+
+               valid_attrs = json_create_object();
+               json_object_add_value_uint(valid_attrs, "ctrl_id",
+                       le16_to_cpu(ctrl_list->identifier[i]));
+               json_array_add_value_object(valid, valid_attrs);
+       }
+
+       json_object_add_value_array(root, "ctrl_list", valid);
+       json_print_object(root, NULL);
+       printf("\n");
+       json_free_object(root);
+}
+
+void nvme_show_list_ctrl(struct nvme_controller_list *ctrl_list,
+       enum nvme_print_flags flags)
+{
+       int i;
+       __u16 num = le16_to_cpu(ctrl_list->num);
+
+       if (flags & JSON)
+               return json_nvme_list_ctrl(ctrl_list, num);
+
+       printf("num of ctrls present: %u\n", num);
+       for (i = 0; i < min(num, 2047); i++) {
+               printf("[%4u]:%#x\n", i, le16_to_cpu(ctrl_list->identifier[i]));
+       }
+}
+
 static void json_nvme_id_nvmset(struct nvme_id_nvmset *nvmset)
 {
        __u32 nent = nvmset->nid;
index 2d8ed4debd8933f3469ecfed97a9f909c3c6d93b..3eb9c519a3d86948d73e624a396cccfc767158e4 100644 (file)
@@ -84,6 +84,8 @@ void nvme_show_id_ns_granularity_list(const struct nvme_id_ns_granularity_list *
 void nvme_show_id_uuid_list(const struct nvme_id_uuid_list *uuid_list,
        enum nvme_print_flags flags);
 void nvme_show_id_iocs(struct nvme_id_iocs *iocs);
+void nvme_show_list_ctrl(struct nvme_controller_list *ctrl_list,
+       enum nvme_print_flags flags);
 
 void nvme_feature_show_fields(enum nvme_feat fid, unsigned int result, unsigned char *buf);
 void nvme_directive_show(__u8 type, __u8 oper, __u16 spec, __u32 nsid, __u32 result,
diff --git a/nvme.c b/nvme.c
index 94015710917f408b1855d6f271f1c4720bed727c..cc854fda82e37642aeb5a8466475669a2e1cb381 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -1377,21 +1377,25 @@ static int list_ctrl(int argc, char **argv, struct command *cmd, struct plugin *
                "given device is part of, or optionally controllers attached to a specific namespace.";
        const char *controller = "controller to display";
        const char *namespace_id = "optional namespace attached to controller";
-       int err, i, fd;
+       int err, fd;
        struct nvme_controller_list *cntlist;
+       enum nvme_print_flags flags;
 
        struct config {
                __u16 cntid;
                __u32 namespace_id;
+               char *output_format;
        };
 
        struct config cfg = {
                .cntid = 0,
+               .output_format = "normal",
        };
 
        OPT_ARGS(opts) = {
-               OPT_SHRT("cntid",       'c', &cfg.cntid,        controller),
-               OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id),
+               OPT_SHRT("cntid",        'c', &cfg.cntid,         controller),
+               OPT_UINT("namespace-id", 'n', &cfg.namespace_id,  namespace_id),
+               OPT_FMT("output-format", 'o', &cfg.output_format, output_format),
                OPT_END()
        };
 
@@ -1399,6 +1403,14 @@ static int list_ctrl(int argc, char **argv, struct command *cmd, struct plugin *
        if (fd < 0)
                goto ret;
 
+       err = flags = validate_output_format(cfg.output_format);
+       if (flags < 0)
+               goto close_fd;
+       if (flags != JSON && flags != NORMAL) {
+               err = -EINVAL;
+               goto close_fd;
+       }
+
        if (posix_memalign((void *)&cntlist, getpagesize(), 0x1000)) {
                fprintf(stderr, "can not allocate controller list payload\n");
                err = -ENOMEM;
@@ -1406,12 +1418,9 @@ static int list_ctrl(int argc, char **argv, struct command *cmd, struct plugin *
        }
 
        err = nvme_identify_ctrl_list(fd, cfg.namespace_id, cfg.cntid, cntlist);
-       if (!err) {
-               __u16 num = le16_to_cpu(cntlist->num);
-
-               for (i = 0; i < (min(num, 2048)); i++)
-                       printf("[%4u]:%#x\n", i, le16_to_cpu(cntlist->identifier[i]));
-       } else if (err > 0)
+       if (!err)
+               nvme_show_list_ctrl(cntlist, flags);
+       else if (err > 0)
                nvme_show_status(err);
        else
                perror("id controller list");