Debian Codegraph @

src/nvme/json.c

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * This file is part of libnvme.
4 * Copyright (c) 2021 SUSE Software Solutions
5 *
6 * Authors: Hannes Reinecke <[email protected]>
7 */
8
9 #include <stdio.h>
10 #include <errno.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <fcntl.h>
14
15 #include <json.h>
16
17 #include "cleanup.h"
18 #include "fabrics.h"
19 #include "log.h"
20 #include "private.h"
21 #include "linux.h"
22
23 #define JSON_UPDATE_INT_OPTION(c, k, a, o) \
24 if (!strcmp(# a, k ) && !c->a) c->a = json_object_get_int(o);
25 #define JSON_UPDATE_BOOL_OPTION(c, k, a, o) \
26 if (!strcmp(# a, k ) && !c->a) c->a = json_object_get_boolean(o);
27
28 static void json_update_attributes(nvme_ctrl_t c,
29 struct json_object *ctrl_obj)
30 {
31 struct nvme_fabrics_config *cfg = nvme_ctrl_get_config(c);
32
33 json_object_object_foreach(ctrl_obj, key_str, val_obj) {
34 JSON_UPDATE_INT_OPTION(cfg, key_str,
35 nr_io_queues, val_obj);
36 JSON_UPDATE_INT_OPTION(cfg, key_str,
37 nr_write_queues, val_obj);
38 JSON_UPDATE_INT_OPTION(cfg, key_str,
39 nr_poll_queues, val_obj);
40 JSON_UPDATE_INT_OPTION(cfg, key_str,
41 queue_size, val_obj);
42 JSON_UPDATE_INT_OPTION(cfg, key_str,
43 keep_alive_tmo, val_obj);
44 JSON_UPDATE_INT_OPTION(cfg, key_str,
45 reconnect_delay, val_obj);
46 if (!strcmp("ctrl_loss_tmo", key_str) &&
47 cfg->ctrl_loss_tmo != NVMF_DEF_CTRL_LOSS_TMO)
48 cfg->ctrl_loss_tmo = json_object_get_int(val_obj);
49 JSON_UPDATE_INT_OPTION(cfg, key_str,
50 fast_io_fail_tmo, val_obj);
51 if (!strcmp("tos", key_str) && cfg->tos != -1)
52 cfg->tos = json_object_get_int(val_obj);
53 JSON_UPDATE_BOOL_OPTION(cfg, key_str,
54 duplicate_connect, val_obj);
55 JSON_UPDATE_BOOL_OPTION(cfg, key_str,
56 disable_sqflow, val_obj);
57 JSON_UPDATE_BOOL_OPTION(cfg, key_str,
58 hdr_digest, val_obj);
59 JSON_UPDATE_BOOL_OPTION(cfg, key_str,
60 data_digest, val_obj);
61 JSON_UPDATE_BOOL_OPTION(cfg, key_str,
62 tls, val_obj);
63 JSON_UPDATE_BOOL_OPTION(cfg, key_str,
64 concat, val_obj);
65 if (!strcmp("persistent", key_str) &&
66 !nvme_ctrl_is_persistent(c))
67 nvme_ctrl_set_persistent(c, true);
68 if (!strcmp("discovery", key_str) &&
69 !nvme_ctrl_is_discovery_ctrl(c))
70 nvme_ctrl_set_discovery_ctrl(c, true);
71 if (!strcmp("keyring", key_str))
72 nvme_ctrl_set_keyring(c,
73 json_object_get_string(val_obj));
74 if (!strcmp("tls_key_identity", key_str)) {
75 nvme_ctrl_set_tls_key_identity(c,
76 json_object_get_string(val_obj));
77 }
78 if (!strcmp("tls_key", key_str)) {
79 nvme_ctrl_set_tls_key(c,
80 json_object_get_string(val_obj));
81 }
82 }
83 }
84
85 static void json_parse_port(nvme_subsystem_t s, struct json_object *port_obj)
86 {
87 nvme_ctrl_t c;
88 struct json_object *attr_obj;
89 const char *transport, *traddr = NULL;
90 const char *host_traddr = NULL, *host_iface = NULL, *trsvcid = NULL;
91
92 attr_obj = json_object_object_get(port_obj, "transport");
93 if (!attr_obj)
94 return;
95 transport = json_object_get_string(attr_obj);
96 attr_obj = json_object_object_get(port_obj, "traddr");
97 if (attr_obj)
98 traddr = json_object_get_string(attr_obj);
99 attr_obj = json_object_object_get(port_obj, "host_traddr");
100 if (attr_obj)
101 host_traddr = json_object_get_string(attr_obj);
102 attr_obj = json_object_object_get(port_obj, "host_iface");
103 if (attr_obj)
104 host_iface = json_object_get_string(attr_obj);
105 attr_obj = json_object_object_get(port_obj, "trsvcid");
106 if (attr_obj)
107 trsvcid = json_object_get_string(attr_obj);
108 c = nvme_lookup_ctrl(s, transport, traddr, host_traddr,
109 host_iface, trsvcid, NULL);
110 if (!c)
111 return;
112 json_update_attributes(c, port_obj);
113 attr_obj = json_object_object_get(port_obj, "dhchap_key");
114 if (attr_obj)
115 nvme_ctrl_set_dhchap_host_key(c, json_object_get_string(attr_obj));
116 attr_obj = json_object_object_get(port_obj, "dhchap_ctrl_key");
117 if (attr_obj)
118 nvme_ctrl_set_dhchap_key(c, json_object_get_string(attr_obj));
119 attr_obj = json_object_object_get(port_obj, "keyring");
120 if (attr_obj)
121 nvme_ctrl_set_keyring(c, json_object_get_string(attr_obj));
122 attr_obj = json_object_object_get(port_obj, "tls_key_identity");
123 if (attr_obj) {
124 nvme_ctrl_set_tls_key_identity(c,
125 json_object_get_string(attr_obj));
126 }
127 attr_obj = json_object_object_get(port_obj, "tls_key");
128 if (attr_obj) {
129 nvme_ctrl_set_tls_key(c,
130 json_object_get_string(attr_obj));
131 }
132 }
133
134 static void json_parse_subsys(nvme_host_t h, struct json_object *subsys_obj)
135 {
136 struct json_object *nqn_obj, *app_obj, *port_array;
137 nvme_subsystem_t s;
138 const char *nqn;
139 int p;
140
141 nqn_obj = json_object_object_get(subsys_obj, "nqn");
142 if (!nqn_obj)
143 return;
144 nqn = json_object_get_string(nqn_obj);
145 s = nvme_lookup_subsystem(h, NULL, nqn);
146 if (!s)
147 return;
148 app_obj = json_object_object_get(subsys_obj, "application");
149 if (app_obj)
150 nvme_subsystem_set_application(s, json_object_get_string(app_obj));
151
152 port_array = json_object_object_get(subsys_obj, "ports");
153 if (!port_array)
154 return;
155 for (p = 0; p < json_object_array_length(port_array); p++) {
156 struct json_object *port_obj;
157
158 port_obj = json_object_array_get_idx(port_array, p);
159 if (port_obj)
160 json_parse_port(s, port_obj);
161 }
162 }
163
164 static void json_parse_host(nvme_root_t r, struct json_object *host_obj)
165 {
166 struct json_object *attr_obj, *subsys_array, *subsys_obj;
167 nvme_host_t h;
168 const char *hostnqn, *hostid = NULL;
169 int s;
170
171 attr_obj = json_object_object_get(host_obj, "hostnqn");
172 if (!attr_obj)
173 return;
174 hostnqn = json_object_get_string(attr_obj);
175 attr_obj = json_object_object_get(host_obj, "hostid");
176 if (attr_obj)
177 hostid = json_object_get_string(attr_obj);
178 h = nvme_lookup_host(r, hostnqn, hostid);
179 attr_obj = json_object_object_get(host_obj, "dhchap_key");
180 if (attr_obj)
181 nvme_host_set_dhchap_key(h, json_object_get_string(attr_obj));
182 attr_obj = json_object_object_get(host_obj, "hostsymname");
183 if (attr_obj)
184 nvme_host_set_hostsymname(h, json_object_get_string(attr_obj));
185 attr_obj = json_object_object_get(host_obj, "persistent_discovery_ctrl");
186 if (attr_obj)
187 nvme_host_set_pdc_enabled(h, json_object_get_boolean(attr_obj));
188 subsys_array = json_object_object_get(host_obj, "subsystems");
189 if (!subsys_array)
190 return;
191 for (s = 0; s < json_object_array_length(subsys_array); s++) {
192 subsys_obj = json_object_array_get_idx(subsys_array, s);
193 if (subsys_obj)
194 json_parse_subsys(h, subsys_obj);
195 }
196 }
197
198 static DEFINE_CLEANUP_FUNC(cleanup_tokener, json_tokener *, json_tokener_free)
199 #define _cleanup_tokener_ __cleanup__(cleanup_tokener)
200
201 static struct json_object *parse_json(nvme_root_t r, int fd)
202 {
203 char buf[JSON_FILE_BUF_SIZE];
204 struct json_object *obj;
205 char *str = NULL;
206 _cleanup_tokener_ json_tokener *tok = NULL;
207 int ret;
208 _cleanup_free_ void *ptr = NULL;
209 int len = 0;
210
211 while ((ret = read(fd, buf, JSON_FILE_BUF_SIZE)) > 0) {
212 str = realloc(ptr, len + ret);
213 if (!str)
214 return NULL;
215 memcpy(&str[len], buf, ret);
216 len += ret;
217 ptr = str;
218 }
219
220 if (ret < 0 || !len)
221 return NULL;
222
223 tok = json_tokener_new_ex(JSON_TOKENER_DEFAULT_DEPTH);
224 if (!tok)
225 return NULL;
226
227 /* Enforce correctly formatted JSON */
228 tok->flags = JSON_TOKENER_STRICT;
229
230 obj = json_tokener_parse_ex(tok, str, len);
231 if (!obj)
232 nvme_msg(r, LOG_DEBUG, "JSON parsing failed: %s\n",
233 json_util_get_last_err());
234
235 return obj;
236 }
237
238 int json_read_config(nvme_root_t r, const char *config_file)
239 {
240 struct json_object *json_root, *host_obj;
241 int fd, h;
242
243 fd = open(config_file, O_RDONLY);
244 if (fd < 0) {
245 nvme_msg(r, LOG_DEBUG, "Error opening %s, %s\n",
246 config_file, strerror(errno));
247 return fd;
248 }
249 json_root = parse_json(r, fd);
250 close(fd);
251 if (!json_root) {
252 errno = EPROTO;
253 return -1;
254 }
255 if (!json_object_is_type(json_root, json_type_array)) {
256 nvme_msg(r, LOG_DEBUG, "Wrong format, expected array\n");
257 json_object_put(json_root);
258 errno = EPROTO;
259 return -1;
260 }
261 for (h = 0; h < json_object_array_length(json_root); h++) {
262 host_obj = json_object_array_get_idx(json_root, h);
263 if (host_obj)
264 json_parse_host(r, host_obj);
265 }
266 json_object_put(json_root);
267 return 0;
268 }
269
270 #define JSON_STRING_OPTION(c, p, o) \
271 if ((c)->o && strcmp((c)->o, "none")) \
272 json_object_object_add((p), # o , \
273 json_object_new_string((c)->o))
274 #define JSON_INT_OPTION(c, p, o, d) \
275 if ((c)->o != d) \
276 json_object_object_add((p), # o , \
277 json_object_new_int((c)->o))
278 #define JSON_BOOL_OPTION(c, p, o) \
279 if ((c)->o) \
280 json_object_object_add((p), # o , \
281 json_object_new_boolean((c)->o))
282
283 static void json_update_port(struct json_object *ctrl_array, nvme_ctrl_t c)
284 {
285 struct nvme_fabrics_config *cfg = nvme_ctrl_get_config(c);
286 struct json_object *port_obj = json_object_new_object();
287 const char *transport, *value;
288
289 transport = nvme_ctrl_get_transport(c);
290 if (!strcmp(transport, "pcie")) {
291 json_object_put(port_obj);
292 return;
293 }
294
295 json_object_object_add(port_obj, "transport",
296 json_object_new_string(transport));
297 value = nvme_ctrl_get_traddr(c);
298 if (value)
299 json_object_object_add(port_obj, "traddr",
300 json_object_new_string(value));
301 value = nvme_ctrl_get_host_traddr(c);
302 if (value)
303 json_object_object_add(port_obj, "host_traddr",
304 json_object_new_string(value));
305 value = nvme_ctrl_get_host_iface(c);
306 if (value)
307 json_object_object_add(port_obj, "host_iface",
308 json_object_new_string(value));
309 value = nvme_ctrl_get_trsvcid(c);
310 if (value)
311 json_object_object_add(port_obj, "trsvcid",
312 json_object_new_string(value));
313 value = nvme_ctrl_get_dhchap_host_key(c);
314 if (value)
315 json_object_object_add(port_obj, "dhchap_key",
316 json_object_new_string(value));
317 value = nvme_ctrl_get_dhchap_key(c);
318 if (value)
319 json_object_object_add(port_obj, "dhchap_ctrl_key",
320 json_object_new_string(value));
321 JSON_BOOL_OPTION(cfg, port_obj, tls);
322 value = nvme_ctrl_get_keyring(c);
323 if (value)
324 json_object_object_add(port_obj, "keyring",
325 json_object_new_string(value));
326 value = nvme_ctrl_get_tls_key_identity(c);
327 if (value)
328 json_object_object_add(port_obj, "tls_key_identity",
329 json_object_new_string(value));
330 value = nvme_ctrl_get_tls_key(c);
331 if (value)
332 json_object_object_add(port_obj, "tls_key",
333 json_object_new_string(value));
334 JSON_INT_OPTION(cfg, port_obj, nr_io_queues, 0);
335 JSON_INT_OPTION(cfg, port_obj, nr_write_queues, 0);
336 JSON_INT_OPTION(cfg, port_obj, nr_poll_queues, 0);
337 JSON_INT_OPTION(cfg, port_obj, queue_size, 0);
338 JSON_INT_OPTION(cfg, port_obj, keep_alive_tmo, 0);
339 JSON_INT_OPTION(cfg, port_obj, reconnect_delay, 0);
340 if (strcmp(transport, "loop")) {
341 JSON_INT_OPTION(cfg, port_obj, ctrl_loss_tmo,
342 NVMF_DEF_CTRL_LOSS_TMO);
343 JSON_INT_OPTION(cfg, port_obj, fast_io_fail_tmo, 0);
344 }
345 JSON_INT_OPTION(cfg, port_obj, tos, -1);
346 JSON_BOOL_OPTION(cfg, port_obj, duplicate_connect);
347 JSON_BOOL_OPTION(cfg, port_obj, disable_sqflow);
348 JSON_BOOL_OPTION(cfg, port_obj, hdr_digest);
349 JSON_BOOL_OPTION(cfg, port_obj, data_digest);
350 JSON_BOOL_OPTION(cfg, port_obj, concat);
351 if (nvme_ctrl_is_persistent(c))
352 json_object_object_add(port_obj, "persistent",
353 json_object_new_boolean(true));
354 if (nvme_ctrl_is_discovery_ctrl(c))
355 json_object_object_add(port_obj, "discovery",
356 json_object_new_boolean(true));
357
358 json_object_array_add(ctrl_array, port_obj);
359 }
360
361 static void json_update_subsys(struct json_object *subsys_array,
362 nvme_subsystem_t s)
363 {
364 nvme_ctrl_t c;
365 const char *subsysnqn = nvme_subsystem_get_nqn(s), *app;
366 struct json_object *subsys_obj = json_object_new_object();
367 struct json_object *port_array;
368
369 /* Skip discovery subsystems as the nqn is not unique */
370 if (!strcmp(subsysnqn, NVME_DISC_SUBSYS_NAME))
371 return;
372
373 json_object_object_add(subsys_obj, "nqn",
374 json_object_new_string(subsysnqn));
375 app = nvme_subsystem_get_application(s);
376 if (app)
377 json_object_object_add(subsys_obj, "application",
378 json_object_new_string(app));
379 port_array = json_object_new_array();
380 nvme_subsystem_for_each_ctrl(s, c) {
381 json_update_port(port_array, c);
382 }
383 if (json_object_array_length(port_array)) {
384 json_object_object_add(subsys_obj, "ports", port_array);
385 json_object_array_add(subsys_array, subsys_obj);
386 } else {
387 json_object_put(port_array);
388 json_object_put(subsys_obj);
389 }
390 }
391
392 int json_update_config(nvme_root_t r, const char *config_file)
393 {
394 nvme_host_t h;
395 struct json_object *json_root, *host_obj;
396 struct json_object *subsys_array;
397 int ret = 0;
398
399 json_root = json_object_new_array();
400 nvme_for_each_host(r, h) {
401 nvme_subsystem_t s;
402 const char *hostnqn, *hostid, *dhchap_key, *hostsymname;
403
404 host_obj = json_object_new_object();
405 if (!host_obj)
406 continue;
407 hostnqn = nvme_host_get_hostnqn(h);
408 json_object_object_add(host_obj, "hostnqn",
409 json_object_new_string(hostnqn));
410 hostid = nvme_host_get_hostid(h);
411 if (hostid)
412 json_object_object_add(host_obj, "hostid",
413 json_object_new_string(hostid));
414 dhchap_key = nvme_host_get_dhchap_key(h);
415 if (dhchap_key)
416 json_object_object_add(host_obj, "dhchap_key",
417 json_object_new_string(dhchap_key));
418 hostsymname = nvme_host_get_hostsymname(h);
419 if (hostsymname)
420 json_object_object_add(host_obj, "hostsymname",
421 json_object_new_string(hostsymname));
422 if (h->pdc_enabled_valid)
423 json_object_object_add(host_obj, "persistent_discovery_ctrl",
424 json_object_new_boolean(h->pdc_enabled));
425 subsys_array = json_object_new_array();
426 nvme_for_each_subsystem(h, s) {
427 json_update_subsys(subsys_array, s);
428 }
429 if (json_object_array_length(subsys_array)) {
430 json_object_object_add(host_obj, "subsystems",
431 subsys_array);
432 json_object_array_add(json_root, host_obj);
433 } else {
434 json_object_put(subsys_array);
435 json_object_put(host_obj);
436 }
437 }
438 if (!config_file) {
439 ret = json_object_to_fd(1, json_root,
440 JSON_C_TO_STRING_PRETTY |
441 JSON_C_TO_STRING_NOSLASHESCAPE);
442 printf("\n");
443 } else
444 ret = json_object_to_file_ext(config_file, json_root,
445 JSON_C_TO_STRING_PRETTY |
446 JSON_C_TO_STRING_NOSLASHESCAPE);
447 if (ret < 0) {
448 nvme_msg(r, LOG_ERR, "Failed to write to %s, %s\n",
449 config_file ? "stdout" : config_file,
450 json_util_get_last_err());
451 ret = -1;
452 errno = EIO;
453 }
454 json_object_put(json_root);
455
456 return ret;
457 }
458
459 static void json_dump_ctrl(struct json_object *ctrl_array, nvme_ctrl_t c)
460 {
461 struct nvme_fabrics_config *cfg = nvme_ctrl_get_config(c);
462 struct json_object *ctrl_obj = json_object_new_object();
463 const char *name, *transport, *value;
464
465 name = nvme_ctrl_get_name(c);
466 if (name && strlen(name))
467 json_object_object_add(ctrl_obj, "name",
468 json_object_new_string(name));
469 transport = nvme_ctrl_get_transport(c);
470 json_object_object_add(ctrl_obj, "transport",
471 json_object_new_string(transport));
472 value = nvme_ctrl_get_traddr(c);
473 if (value)
474 json_object_object_add(ctrl_obj, "traddr",
475 json_object_new_string(value));
476 value = nvme_ctrl_get_host_traddr(c);
477 if (value)
478 json_object_object_add(ctrl_obj, "host_traddr",
479 json_object_new_string(value));
480 value = nvme_ctrl_get_host_iface(c);
481 if (value)
482 json_object_object_add(ctrl_obj, "host_iface",
483 json_object_new_string(value));
484 value = nvme_ctrl_get_trsvcid(c);
485 if (value)
486 json_object_object_add(ctrl_obj, "trsvcid",
487 json_object_new_string(value));
488 value = nvme_ctrl_get_dhchap_host_key(c);
489 if (value)
490 json_object_object_add(ctrl_obj, "dhchap_key",
491 json_object_new_string(value));
492 value = nvme_ctrl_get_dhchap_key(c);
493 if (value)
494 json_object_object_add(ctrl_obj, "dhchap_ctrl_key",
495 json_object_new_string(value));
496 JSON_INT_OPTION(cfg, ctrl_obj, nr_io_queues, 0);
497 JSON_INT_OPTION(cfg, ctrl_obj, nr_write_queues, 0);
498 JSON_INT_OPTION(cfg, ctrl_obj, nr_poll_queues, 0);
499 JSON_INT_OPTION(cfg, ctrl_obj, queue_size, 0);
500 JSON_INT_OPTION(cfg, ctrl_obj, keep_alive_tmo, 0);
501 JSON_INT_OPTION(cfg, ctrl_obj, reconnect_delay, 0);
502 if (strcmp(transport, "loop")) {
503 JSON_INT_OPTION(cfg, ctrl_obj, ctrl_loss_tmo,
504 NVMF_DEF_CTRL_LOSS_TMO);
505 JSON_INT_OPTION(cfg, ctrl_obj, fast_io_fail_tmo, 0);
506 }
507 JSON_INT_OPTION(cfg, ctrl_obj, tos, -1);
508 JSON_BOOL_OPTION(cfg, ctrl_obj, duplicate_connect);
509 JSON_BOOL_OPTION(cfg, ctrl_obj, disable_sqflow);
510 JSON_BOOL_OPTION(cfg, ctrl_obj, hdr_digest);
511 JSON_BOOL_OPTION(cfg, ctrl_obj, data_digest);
512 if (!strcmp(transport, "tcp")) {
513 JSON_BOOL_OPTION(cfg, ctrl_obj, tls);
514
515 value = nvme_ctrl_get_keyring(c);
516 if (value)
517 json_object_object_add(ctrl_obj, "keyring",
518 json_object_new_string(value));
519 value = nvme_ctrl_get_tls_key_identity(c);
520 if (value)
521 json_object_object_add(ctrl_obj, "tls_key_identity",
522 json_object_new_string(value));
523 value = nvme_ctrl_get_tls_key(c);
524 if (value)
525 json_object_object_add(ctrl_obj, "tls_key",
526 json_object_new_string(value));
527 }
528 JSON_BOOL_OPTION(cfg, ctrl_obj, concat);
529 if (nvme_ctrl_is_persistent(c))
530 json_object_object_add(ctrl_obj, "persistent",
531 json_object_new_boolean(true));
532 if (nvme_ctrl_is_discovery_ctrl(c))
533 json_object_object_add(ctrl_obj, "discovery",
534 json_object_new_boolean(true));
535 json_object_array_add(ctrl_array, ctrl_obj);
536 }
537
538 static unsigned int json_dump_subsys_multipath(nvme_subsystem_t s,
539 struct json_object *ns_array)
540 {
541 nvme_ns_t n;
542 nvme_path_t p;
543 unsigned int i = 0;
544
545 nvme_subsystem_for_each_ns(s, n) {
546 struct json_object *ns_obj;
547 struct json_object *path_array;
548
549 ns_obj = json_object_new_object();
550 json_object_object_add(ns_obj, "nsid",
551 json_object_new_int(nvme_ns_get_nsid(n)));
552 json_object_object_add(ns_obj, "name",
553 json_object_new_string(nvme_ns_get_name(n)));
554
555 path_array = json_object_new_array();
556 nvme_namespace_for_each_path(n, p) {
557 struct json_object *path_obj;
558 struct json_object *ctrl_array;
559 nvme_ctrl_t c;
560
561 path_obj = json_object_new_object();
562 json_object_object_add(path_obj, "path",
563 json_object_new_string(nvme_path_get_name(p)));
564 json_object_object_add(path_obj, "ANAState",
565 json_object_new_string(nvme_path_get_ana_state(p)));
566 json_object_object_add(path_obj, "NUMANodes",
567 json_object_new_string(nvme_path_get_numa_nodes(p)));
568 json_object_object_add(path_obj, "qdepth",
569 json_object_new_int(nvme_path_get_queue_depth(p)));
570
571 c = nvme_path_get_ctrl(p);
572 ctrl_array = json_object_new_array();
573 json_dump_ctrl(ctrl_array, c);
574 json_object_object_add(path_obj, "controller", ctrl_array);
575 json_object_array_add(path_array, path_obj);
576 }
577 json_object_object_add(ns_obj, "paths", path_array);
578 json_object_array_add(ns_array, ns_obj);
579 i++;
580 }
581 return i;
582 }
583
584 static void json_dump_subsys_non_multipath(nvme_subsystem_t s,
585 struct json_object *ns_array)
586 {
587 nvme_ctrl_t c;
588 nvme_ns_t n;
589
590 nvme_subsystem_for_each_ctrl(s, c) {
591 nvme_ctrl_for_each_ns(c, n) {
592 struct json_object *ctrl_array;
593 struct json_object *ns_obj;
594
595 ns_obj = json_object_new_object();
596 json_object_object_add(ns_obj, "nsid",
597 json_object_new_int(nvme_ns_get_nsid(n)));
598 json_object_object_add(ns_obj, "name",
599 json_object_new_string(nvme_ns_get_name(n)));
600
601 ctrl_array = json_object_new_array();
602 json_dump_ctrl(ctrl_array, c);
603 json_object_object_add(ns_obj, "controller", ctrl_array);
604
605 json_object_array_add(ns_array, ns_obj);
606 }
607 }
608 }
609
610 static void json_dump_subsys(struct json_object *subsys_array,
611 nvme_subsystem_t s)
612 {
613 struct json_object *subsys_obj = json_object_new_object();
614 struct json_object *ns_array;
615
616 json_object_object_add(subsys_obj, "name",
617 json_object_new_string(nvme_subsystem_get_name(s)));
618 json_object_object_add(subsys_obj, "nqn",
619 json_object_new_string(nvme_subsystem_get_nqn(s)));
620
621 ns_array = json_object_new_array();
622 if (!json_dump_subsys_multipath(s, ns_array))
623 json_dump_subsys_non_multipath(s, ns_array);
624
625 if (json_object_array_length(ns_array))
626 json_object_object_add(subsys_obj, "namespaces", ns_array);
627 else
628 json_object_put(ns_array);
629 json_object_array_add(subsys_array, subsys_obj);
630 }
631
632 int json_dump_tree(nvme_root_t r)
633 {
634 nvme_host_t h;
635 struct json_object *json_root, *host_obj;
636 struct json_object *host_array, *subsys_array;
637 int ret = 0;
638
639 json_root = json_object_new_object();
640 host_array = json_object_new_array();
641 nvme_for_each_host(r, h) {
642 nvme_subsystem_t s;
643 const char *hostid, *dhchap_key;
644
645 host_obj = json_object_new_object();
646 json_object_object_add(host_obj, "hostnqn",
647 json_object_new_string(nvme_host_get_hostnqn(h)));
648 hostid = nvme_host_get_hostid(h);
649 if (hostid)
650 json_object_object_add(host_obj, "hostid",
651 json_object_new_string(hostid));
652 dhchap_key = nvme_host_get_dhchap_key(h);
653 if (dhchap_key)
654 json_object_object_add(host_obj, "dhchap_key",
655 json_object_new_string(dhchap_key));
656 if (h->pdc_enabled_valid)
657 json_object_object_add(host_obj, "persistent_discovery_ctrl",
658 json_object_new_boolean(h->pdc_enabled));
659 subsys_array = json_object_new_array();
660 nvme_for_each_subsystem(h, s) {
661 json_dump_subsys(subsys_array, s);
662 }
663 if (json_object_array_length(subsys_array))
664 json_object_object_add(host_obj, "subsystems",
665 subsys_array);
666 else
667 json_object_put(subsys_array);
668 json_object_array_add(host_array, host_obj);
669 }
670 json_object_object_add(json_root, "hosts", host_array);
671
672 ret = json_object_to_fd(r->log.fd, json_root,
673 JSON_C_TO_STRING_PRETTY |
674 JSON_C_TO_STRING_NOSLASHESCAPE);
675 if (ret < 0) {
676 nvme_msg(r, LOG_ERR, "Failed to write, %s\n",
677 json_util_get_last_err());
678 ret = -1;
679 errno = EIO;
680 }
681 json_object_put(json_root);
682
683 return ret;
684 }