@@ -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+ }
0 commit comments