Skip to content

Commit 34a081d

Browse files
committed
Add lm-print* files, making json support optional
1 parent 37848d2 commit 34a081d

File tree

8 files changed

+364
-231
lines changed

8 files changed

+364
-231
lines changed

plugins/lm/lm-nvme.c

Lines changed: 6 additions & 230 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#define CREATE_CMD
3636
#include "lm-nvme.h"
3737

38+
#include "lm-print.h"
39+
3840
static inline const char *arg_str(const char * const *strings,
3941
size_t array_size, size_t idx)
4042
{
@@ -417,206 +419,6 @@ static int lm_migration_send(int argc, char **argv, struct command *command, str
417419
return err;
418420
}
419421

420-
static void json_controller_state_data(struct nvme_lm_controller_state_data *data, size_t len)
421-
{
422-
struct json_object *root = json_create_object();
423-
struct json_object *nvmecs = json_create_object();
424-
struct json_object *iosqs = json_create_array();
425-
struct json_object *iocqs = json_create_array();
426-
427-
json_object_add_value_uint(root, "version",
428-
le16_to_cpu(data->hdr.ver));
429-
json_object_add_value_uint(root, "controller state attributes",
430-
data->hdr.csattr);
431-
json_object_add_value_uint128(root, "nvme controller state size",
432-
le128_to_cpu(data->hdr.nvmecss));
433-
json_object_add_value_uint128(root, "vendor specific size",
434-
le128_to_cpu(data->hdr.vss));
435-
436-
json_object_add_value_object(root, "nvme controller state", nvmecs);
437-
438-
json_object_add_value_uint(nvmecs, "version",
439-
le16_to_cpu(data->data.hdr.ver));
440-
json_object_add_value_uint(nvmecs, "number of io submission queues",
441-
le16_to_cpu(data->data.hdr.niosq));
442-
json_object_add_value_uint(nvmecs, "number of io completion queues",
443-
le16_to_cpu(data->data.hdr.niocq));
444-
445-
json_object_add_value_array(nvmecs, "io submission queue list", iosqs);
446-
447-
for (int i = 0; i < data->data.hdr.niosq; i++) {
448-
struct nvme_lm_io_submission_queue_data *sq = &data->data.sqs[i];
449-
struct json_object *sq_obj = json_create_object();
450-
451-
json_object_add_value_uint64(sq_obj, "io submission prp entry 1",
452-
le64_to_cpu(sq->iosqprp1));
453-
json_object_add_value_uint(sq_obj, "io submission queue size",
454-
le16_to_cpu(sq->iosqqsize));
455-
json_object_add_value_uint(sq_obj, "io submission queue identifier",
456-
le16_to_cpu(sq->iosqqid));
457-
json_object_add_value_uint(sq_obj, "io completion queue identifier",
458-
le16_to_cpu(sq->iosqcqid));
459-
json_object_add_value_uint(sq_obj, "io submission queue attributes",
460-
le16_to_cpu(sq->iosqa));
461-
json_object_add_value_uint(sq_obj, "io submission queue head pointer",
462-
le16_to_cpu(sq->iosqhp));
463-
json_object_add_value_uint(sq_obj, "io submission queue tail pointer",
464-
le16_to_cpu(sq->iosqtp));
465-
466-
json_array_add_value_object(iosqs, sq_obj);
467-
}
468-
469-
json_object_add_value_array(nvmecs, "io completion queue list", iocqs);
470-
471-
for (int i = 0; i < data->data.hdr.niocq; i++) {
472-
struct nvme_lm_io_completion_queue_data *cq = &data->data.cqs[i];
473-
struct json_object *cq_obj = json_create_object();
474-
475-
json_object_add_value_uint64(cq_obj, "io completion prp entry 1",
476-
le64_to_cpu(cq->iocqprp1));
477-
json_object_add_value_uint(cq_obj, "io completion queue size",
478-
le16_to_cpu(cq->iocqqsize));
479-
json_object_add_value_uint(cq_obj, "io completion queue identifier",
480-
le16_to_cpu(cq->iocqqid));
481-
json_object_add_value_uint(cq_obj, "io completion queue head pointer",
482-
le16_to_cpu(cq->iocqhp));
483-
json_object_add_value_uint(cq_obj, "io completion queue tail pointer",
484-
le16_to_cpu(cq->iocqtp));
485-
json_object_add_value_uint(cq_obj, "io completion queue attributes",
486-
le32_to_cpu(cq->iocqa));
487-
488-
json_array_add_value_object(iocqs, cq_obj);
489-
}
490-
491-
json_print_object(root, NULL);
492-
printf("\n");
493-
json_free_object(root);
494-
}
495-
496-
static void show_controller_state_data(struct nvme_lm_controller_state_data *data, size_t len,
497-
__u32 offset, enum nvme_print_flags flags)
498-
{
499-
if (flags & BINARY)
500-
return d_raw((unsigned char *)data, len);
501-
502-
if (offset) {
503-
nvme_show_error("Cannot parse non-zero offset");
504-
return;
505-
}
506-
507-
if (flags & JSON)
508-
return json_controller_state_data(data, len);
509-
510-
int human = flags & VERBOSE;
511-
512-
if (sizeof(struct nvme_lm_controller_state_data_header) <= len) {
513-
printf("Header:\n");
514-
printf("%-45s: 0x%x\n", "Version (VER)", data->hdr.ver);
515-
printf("%-45s: 0x%x\n", "Controller State Attributes (CSATTR)", data->hdr.csattr);
516-
if (human)
517-
printf(" [0:0] : 0x%x Controller %sSuspended\n",
518-
data->hdr.csattr & 1, data->hdr.csattr & 1 ? "" : "NOT ");
519-
printf("%-45s: %s\n", "NVMe Controller State Size (NVMECSS)",
520-
uint128_t_to_string(le128_to_cpu(data->hdr.nvmecss)));
521-
printf("%-45s: %s\n", "Vendor Specific Size (VSS)",
522-
uint128_t_to_string(le128_to_cpu(data->hdr.vss)));
523-
524-
len -= sizeof(struct nvme_lm_controller_state_data_header);
525-
} else {
526-
fprintf(stderr, "WARNING: Header truncated\n");
527-
len = 0;
528-
}
529-
530-
if (!len)
531-
return;
532-
533-
if (sizeof(struct nvme_lm_nvme_controller_state_data_header) <= len) {
534-
int niosq = data->data.hdr.niosq;
535-
int niocq = data->data.hdr.niocq;
536-
537-
printf("\nNVMe Controller State Data Structure:\n");
538-
printf("%-45s: 0x%x\n", "Version (VER)",
539-
le16_to_cpu(data->data.hdr.ver));
540-
printf("%-45s: %d\n", "Number of I/O Submission Queues (NIOSQ)",
541-
le16_to_cpu(niosq));
542-
printf("%-45s: %d\n", "Number of I/O Completion Queues (NIOCQ)",
543-
le16_to_cpu(niocq));
544-
545-
len -= sizeof(struct nvme_lm_nvme_controller_state_data_header);
546-
547-
if (len < niosq * sizeof(struct nvme_lm_io_submission_queue_data)) {
548-
fprintf(stderr, "WARNING: I/O Submission Queues truncated\n");
549-
niosq = len / sizeof(struct nvme_lm_io_submission_queue_data);
550-
}
551-
552-
for (int i = 0; i < niosq; ++i) {
553-
struct nvme_lm_io_submission_queue_data *sq = &(data->data.sqs[i]);
554-
__u16 iosqa = le16_to_cpu(sq->iosqa);
555-
556-
printf("\nNVMe I/O Submission Queue Data [%d]:\n", i);
557-
printf("%-45s: 0x%lx\n", "PRP Entry 1 (IOSQPRP1)",
558-
le64_to_cpu(sq->iosqprp1));
559-
printf("%-45s: 0x%x\n", "Queue Size (IOSQQSIZE)",
560-
le16_to_cpu(sq->iosqqsize));
561-
printf("%-45s: 0x%x\n", "Identifier (IOSQQID)",
562-
le16_to_cpu(sq->iosqqid));
563-
printf("%-45s: 0x%x\n", "Completion Queue Identifier (IOSQCQID)",
564-
le16_to_cpu(sq->iosqcqid));
565-
printf("%-45s: 0x%x\n", "Attributes (IOSQA)", iosqa);
566-
if (human) {
567-
printf(" [2:1] : 0x%x Queue Priority (IOSQQPRIO)\n",
568-
NVME_GET(iosqa, LM_IOSQPRIO));
569-
printf(" [0:0] : 0x%x Queue %sPhysically Contiguous (IOSQPC)\n",
570-
NVME_GET(iosqa, LM_IOSQPC),
571-
NVME_GET(iosqa, LM_IOSQPC) ? "" : "NOT ");
572-
}
573-
printf("%-45s: 0x%x\n", "I/O Submission Queue Head Pointer (IOSQHP)",
574-
le16_to_cpu(sq->iosqhp));
575-
printf("%-45s: 0x%x\n", "I/O Submission Queue Tail Pointer (IOSQTP)",
576-
le16_to_cpu(sq->iosqtp));
577-
}
578-
579-
len -= niosq * sizeof(struct nvme_lm_io_submission_queue_data);
580-
581-
if (len < niocq * sizeof(struct nvme_lm_io_completion_queue_data)) {
582-
fprintf(stderr, "WARNING: I/O Completion Queues truncated\n");
583-
niocq = len / sizeof(struct nvme_lm_io_completion_queue_data);
584-
}
585-
586-
for (int i = 0; i < niocq; ++i) {
587-
struct nvme_lm_io_completion_queue_data *cq = &data->data.cqs[niosq + i];
588-
__u32 iocqa = le32_to_cpu(cq->iocqa);
589-
590-
printf("\nNVMe I/O Completion Queue Data [%d]:\n", i);
591-
printf("%-45s: 0x%lx\n", "I/O Completion PRP Entry 1 (IOCQPRP1)",
592-
le64_to_cpu(cq->iocqprp1));
593-
printf("%-45s: 0x%x\n", "I/O Completion Queue Size (IOCQQSIZE)",
594-
le16_to_cpu(cq->iocqqsize));
595-
printf("%-45s: 0x%x\n", "I/O Completion Queue Identifier (IOCQQID)",
596-
le16_to_cpu(cq->iocqqid));
597-
printf("%-45s: 0x%x\n", "I/O Completion Queue Head Pointer (IOSQHP)",
598-
le16_to_cpu(cq->iocqhp));
599-
printf("%-45s: 0x%x\n", "I/O Completion Queue Tail Pointer (IOSQTP)",
600-
le16_to_cpu(cq->iocqtp));
601-
printf("%-45s: 0x%x\n", "I/O Completion Queue Attributes (IOCQA)", iocqa);
602-
if (human) {
603-
printf(" [31:16] : 0x%x I/O Completion Queue Interrupt Vector "
604-
"(IOCQIV)\n",
605-
NVME_GET(iocqa, LM_IOCQIEN));
606-
printf(" [2:2] : 0x%x Slot 0 Phase Tag (S0PT)\n",
607-
NVME_GET(iocqa, LM_S0PT));
608-
printf(" [1:1] : 0x%x Interrupts %sEnabled (IOCQIEN)\n",
609-
NVME_GET(iocqa, LM_IOCQIEN),
610-
NVME_GET(iocqa, LM_IOCQIEN) ? "" : "NOT ");
611-
printf(" [0:0] : 0x%x Queue %sPhysically Contiguous (IOCQPC)\n",
612-
NVME_GET(iocqa, LM_IOCQPC),
613-
NVME_GET(iocqa, LM_IOCQPC) ? "" : "NOT ");
614-
}
615-
}
616-
} else
617-
fprintf(stderr, "WARNING: NVMe Controller State Data Structure truncated\n");
618-
}
619-
620422
static int lm_migration_recv(int argc, char **argv, struct command *command, struct plugin *plugin)
621423
{
622424
const char *desc = "Migration Receive command is used to obtain information used to manage "
@@ -636,7 +438,7 @@ static int lm_migration_recv(int argc, char **argv, struct command *command, str
636438
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
637439
_cleanup_file_ FILE *fd = NULL;
638440
_cleanup_huge_ struct nvme_mem_huge mh = { 0, };
639-
enum nvme_print_flags flags;
441+
nvme_print_flags_t flags;
640442
void *data = NULL;
641443
int err = -1;
642444

@@ -741,8 +543,8 @@ static int lm_migration_recv(int argc, char **argv, struct command *command, str
741543
err = -errno;
742544
}
743545
} else {
744-
show_controller_state_data((struct nvme_lm_controller_state_data *)data,
745-
(cfg.numd + 1) << 2, cfg.offset, flags);
546+
lm_show_controller_state_data((struct nvme_lm_controller_state_data *)data,
547+
(cfg.numd + 1) << 2, cfg.offset, flags);
746548
}
747549
}
748550

@@ -809,40 +611,14 @@ static int lm_set_cdq(int argc, char **argv, struct command *command, struct plu
809611
return err;
810612
}
811613

812-
static void json_lm_controller_data_queue(struct nvme_lm_ctrl_data_queue_fid_data *data)
813-
{
814-
struct json_object *root = json_create_object();
815-
816-
json_object_add_value_uint(root, "head_pointer", le32_to_cpu(data->hp));
817-
json_object_add_value_uint(root, "tail_pointer_trigger", le32_to_cpu(data->tpt));
818-
819-
json_print_object(root, NULL);
820-
printf("\n");
821-
json_free_object(root);
822-
}
823-
824-
static void lm_show_controller_data_queue(struct nvme_lm_ctrl_data_queue_fid_data *data,
825-
enum nvme_print_flags flags)
826-
{
827-
if (flags & JSON)
828-
return json_lm_controller_data_queue(data);
829-
830-
if (flags & BINARY)
831-
return d_raw((unsigned char *)data,
832-
sizeof(struct nvme_lm_ctrl_data_queue_fid_data));
833-
834-
printf("Head Pointer: 0x%x\n", le32_to_cpu(data->hp));
835-
printf("Tail Pointer Trigger: 0x%x\n", le32_to_cpu(data->tpt));
836-
}
837-
838614
static int lm_get_cdq(int argc, char **argv, struct command *command, struct plugin *plugin)
839615
{
840616
const char *desc = "This Feature allows a host to retrieve the status of the head pointer "
841617
"of a CDQ and specify the configuration of a CDQ Tail event.";
842618
const char *cdqid = "Controller Data Queue ID";
843619

844620
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
845-
enum nvme_print_flags flags;
621+
nvme_print_flags_t flags;
846622
int err = -1;
847623

848624
struct config {

plugins/lm/lm-print-binary.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
3+
#include "lm-print.h"
4+
5+
static void binary_controller_state_data(struct nvme_lm_controller_state_data *data, size_t len,
6+
__u32 offset)
7+
{
8+
d_raw((unsigned char *)data, len);
9+
}
10+
11+
static void binary_controller_data_queue(struct nvme_lm_ctrl_data_queue_fid_data *data)
12+
{
13+
d_raw((unsigned char *)data, sizeof(*data));
14+
}
15+
16+
static struct lm_print_ops binary_print_ops = {
17+
.controller_state_data = binary_controller_state_data,
18+
.controller_data_queue = binary_controller_data_queue,
19+
};
20+
21+
struct lm_print_ops *lm_get_binary_print_ops(nvme_print_flags_t flags)
22+
{
23+
binary_print_ops.flags = flags;
24+
return &binary_print_ops;
25+
}

0 commit comments

Comments
 (0)