Skip to content

Commit c79bd6a

Browse files
hreineckedwsuse
authored andcommitted
tree: add nvme_dump_tree()
Dump the internal tree in JSON format to stdout for debugging. Signed-off-by: Hannes Reinecke <[email protected]> [dwagner: dropped json wrapper definition usage] Signed-off-by: Daniel Wagner <[email protected]>
1 parent 984f9ae commit c79bd6a

4 files changed

Lines changed: 153 additions & 0 deletions

File tree

src/nvme/json.c

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,133 @@ int json_update_config(nvme_root_t r, const char *config_file)
331331

332332
return ret;
333333
}
334+
335+
static void json_dump_ctrl(struct json_object *ctrl_array, nvme_ctrl_t c)
336+
{
337+
struct nvme_fabrics_config *cfg = nvme_ctrl_get_config(c);
338+
struct json_object *ctrl_obj = json_object_new_object();
339+
const char *name, *transport, *value;
340+
341+
name = nvme_ctrl_get_name(c);
342+
if (name && strlen(name))
343+
json_object_object_add(ctrl_obj, "name",
344+
json_object_new_string(name));
345+
transport = nvme_ctrl_get_transport(c);
346+
json_object_object_add(ctrl_obj, "transport",
347+
json_object_new_string(transport));
348+
value = nvme_ctrl_get_traddr(c);
349+
if (value)
350+
json_object_object_add(ctrl_obj, "traddr",
351+
json_object_new_string(value));
352+
value = nvme_ctrl_get_host_traddr(c);
353+
if (value)
354+
json_object_object_add(ctrl_obj, "host_traddr",
355+
json_object_new_string(value));
356+
value = nvme_ctrl_get_host_iface(c);
357+
if (value)
358+
json_object_object_add(ctrl_obj, "host_iface",
359+
json_object_new_string(value));
360+
value = nvme_ctrl_get_trsvcid(c);
361+
if (value)
362+
json_object_object_add(ctrl_obj, "trsvcid",
363+
json_object_new_string(value));
364+
value = nvme_ctrl_get_dhchap_key(c);
365+
if (value)
366+
json_object_object_add(ctrl_obj, "dhchap_key",
367+
json_object_new_string(value));
368+
JSON_INT_OPTION(cfg, ctrl_obj, nr_io_queues, 0);
369+
JSON_INT_OPTION(cfg, ctrl_obj, nr_write_queues, 0);
370+
JSON_INT_OPTION(cfg, ctrl_obj, nr_poll_queues, 0);
371+
JSON_INT_OPTION(cfg, ctrl_obj, queue_size, 0);
372+
JSON_INT_OPTION(cfg, ctrl_obj, keep_alive_tmo, 0);
373+
JSON_INT_OPTION(cfg, ctrl_obj, reconnect_delay, 0);
374+
if (strcmp(transport, "loop")) {
375+
JSON_INT_OPTION(cfg, ctrl_obj, ctrl_loss_tmo,
376+
NVMF_DEF_CTRL_LOSS_TMO);
377+
JSON_INT_OPTION(cfg, ctrl_obj, fast_io_fail_tmo, 0);
378+
}
379+
JSON_INT_OPTION(cfg, ctrl_obj, tos, -1);
380+
JSON_BOOL_OPTION(cfg, ctrl_obj, duplicate_connect);
381+
JSON_BOOL_OPTION(cfg, ctrl_obj, disable_sqflow);
382+
JSON_BOOL_OPTION(cfg, ctrl_obj, hdr_digest);
383+
JSON_BOOL_OPTION(cfg, ctrl_obj, data_digest);
384+
JSON_BOOL_OPTION(cfg, ctrl_obj, tls);
385+
if (nvme_ctrl_is_persistent(c))
386+
json_object_object_add(ctrl_obj, "persistent",
387+
json_object_new_boolean(true));
388+
if (nvme_ctrl_is_discovery_ctrl(c))
389+
json_object_object_add(ctrl_obj, "discovery",
390+
json_object_new_boolean(true));
391+
json_object_array_add(ctrl_array, ctrl_obj);
392+
}
393+
394+
static void json_dump_subsys(struct json_object *subsys_array,
395+
nvme_subsystem_t s)
396+
{
397+
nvme_ctrl_t c;
398+
struct json_object *subsys_obj = json_object_new_object();
399+
struct json_object *ctrl_array;
400+
401+
json_object_object_add(subsys_obj, "name",
402+
json_object_new_string(nvme_subsystem_get_name(s)));
403+
json_object_object_add(subsys_obj, "nqn",
404+
json_object_new_string(nvme_subsystem_get_nqn(s)));
405+
ctrl_array = json_object_new_array();
406+
nvme_subsystem_for_each_ctrl(s, c) {
407+
json_dump_ctrl(ctrl_array, c);
408+
}
409+
if (json_object_array_length(ctrl_array))
410+
json_object_object_add(subsys_obj, "controllers", ctrl_array);
411+
else
412+
json_object_put(ctrl_array);
413+
json_object_array_add(subsys_array, subsys_obj);
414+
}
415+
416+
int json_dump_tree(nvme_root_t r)
417+
{
418+
nvme_host_t h;
419+
struct json_object *json_root, *host_obj;
420+
struct json_object *host_array, *subsys_array;
421+
int ret = 0;
422+
423+
json_root = json_object_new_object();
424+
host_array = json_object_new_array();
425+
nvme_for_each_host(r, h) {
426+
nvme_subsystem_t s;
427+
const char *hostid, *dhchap_key;
428+
429+
host_obj = json_object_new_object();
430+
json_object_object_add(host_obj, "hostnqn",
431+
json_object_new_string(nvme_host_get_hostnqn(h)));
432+
hostid = nvme_host_get_hostid(h);
433+
if (hostid)
434+
json_object_object_add(host_obj, "hostid",
435+
json_object_new_string(hostid));
436+
dhchap_key = nvme_host_get_dhchap_key(h);
437+
if (dhchap_key)
438+
json_object_object_add(host_obj, "dhchap_key",
439+
json_object_new_string(dhchap_key));
440+
subsys_array = json_object_new_array();
441+
nvme_for_each_subsystem(h, s) {
442+
json_dump_subsys(subsys_array, s);
443+
}
444+
if (json_object_array_length(subsys_array))
445+
json_object_object_add(host_obj, "subsystems",
446+
subsys_array);
447+
else
448+
json_object_put(subsys_array);
449+
json_object_array_add(host_array, host_obj);
450+
}
451+
json_object_object_add(json_root, "hosts", host_array);
452+
453+
ret = json_object_to_fd(1, json_root, JSON_C_TO_STRING_PRETTY);
454+
if (ret < 0) {
455+
nvme_msg(r, LOG_ERR, "Failed to write to stdout, %s\n",
456+
json_util_get_last_err());
457+
ret = -1;
458+
errno = EIO;
459+
}
460+
json_object_put(json_root);
461+
462+
return ret;
463+
}

src/nvme/private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ int json_read_config(nvme_root_t r, const char *config_file);
127127

128128
int json_update_config(nvme_root_t r, const char *config_file);
129129

130+
int json_dump_tree(nvme_root_t r);
131+
130132
#if (LOG_FUNCNAME == 1)
131133
#define __nvme_log_func __func__
132134
#else

src/nvme/tree.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ int nvme_dump_config(nvme_root_t r)
189189
#endif
190190
}
191191

192+
int nvme_dump_tree(nvme_root_t r)
193+
{
194+
#ifdef CONFIG_JSONC
195+
return json_dump_tree(r);
196+
#else
197+
errno = ENOTSUP;
198+
return -1;
199+
#endif
200+
}
201+
192202
nvme_host_t nvme_first_host(nvme_root_t r)
193203
{
194204
return list_top(&r->hosts, struct nvme_host, entry);

src/nvme/tree.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,17 @@ int nvme_update_config(nvme_root_t r);
10941094
*/
10951095
int nvme_dump_config(nvme_root_t r);
10961096

1097+
/**
1098+
* nvme_dump_tree() - Dump internal object tree
1099+
* @r: nvme_root_t object
1100+
*
1101+
* Prints the internal object tree in JSON format
1102+
* to stdout.
1103+
*
1104+
* Return: 0 on success, -1 on failure.
1105+
*/
1106+
int nvme_dump_tree(nvme_root_t r);
1107+
10971108
/**
10981109
* nvme_get_attr() - Read sysfs attribute
10991110
* @d: sysfs directory

0 commit comments

Comments
 (0)