@@ -301,21 +301,24 @@ static void save_discovery_log(char *raw, struct nvmf_discovery_log *log)
301301 close (fd );
302302}
303303
304- struct cb_discovery_log_data {
304+ struct cb_discovery_data {
305+ struct nvme_fabrics_config * defcfg ;
305306 nvme_print_flags_t flags ;
306307 char * raw ;
308+ char * * argv ;
309+ FILE * f ;
307310};
308311
309312static void cb_discovery_log (struct nvmf_discovery_ctx * dctx ,
310313 bool connect , struct nvmf_discovery_log * log ,
311314 uint64_t numrec , void * user_data )
312315{
313- struct cb_discovery_log_data * dld = user_data ;
316+ struct cb_discovery_data * cdd = user_data ;
314317
315- if (dld -> raw )
316- save_discovery_log (dld -> raw , log );
318+ if (cdd -> raw )
319+ save_discovery_log (cdd -> raw , log );
317320 else if (!connect )
318- nvme_show_discovery_log (log , numrec , dld -> flags );
321+ nvme_show_discovery_log (log , numrec , cdd -> flags );
319322}
320323
321324static void already_connected (struct nvme_host * host ,
@@ -344,15 +347,15 @@ static bool nvmf_decide_retry(struct nvmf_discovery_ctx *dctx, int err,
344347static void nvmf_connected (struct nvmf_discovery_ctx * dctx ,
345348 struct nvme_ctrl * c , void * user_data )
346349{
347- struct cb_discovery_log_data * dld = user_data ;
350+ struct cb_discovery_data * cdd = user_data ;
348351
349- if (dld -> flags == NORMAL ) {
352+ if (cdd -> flags == NORMAL ) {
350353 printf ("device: %s\n" , nvme_ctrl_get_name (c ));
351354 return ;
352355 }
353356
354357#ifdef CONFIG_JSONC
355- if (dld -> flags == JSON ) {
358+ if (cdd -> flags == JSON ) {
356359 struct json_object * root ;
357360
358361 root = json_create_object ();
@@ -367,6 +370,131 @@ static void nvmf_connected(struct nvmf_discovery_ctx *dctx,
367370#endif
368371}
369372
373+ static int parser_init (struct nvmf_discovery_ctx * dctx , void * user_data )
374+ {
375+ struct cb_discovery_data * cdd = user_data ;
376+
377+ cdd -> f = fopen (PATH_NVMF_DISC , "r" );
378+ if (cdd -> f == NULL ) {
379+ fprintf (stderr , "No params given and no %s\n" , PATH_NVMF_DISC );
380+ return - ENOENT ;
381+ }
382+
383+ cdd -> argv = calloc (MAX_DISC_ARGS , sizeof (char * ));
384+ if (!cdd -> argv )
385+ return -1 ;
386+
387+ cdd -> argv [0 ] = "discover" ;
388+
389+ return 0 ;
390+ }
391+
392+ static void parser_cleanup (struct nvmf_discovery_ctx * dctx , void * user_data )
393+ {
394+ struct cb_discovery_data * cdd = user_data ;
395+
396+ free (cdd -> argv );
397+ fclose (cdd -> f );
398+ }
399+
400+ static int parser_next_line (struct nvmf_discovery_ctx * dctx , void * user_data )
401+ {
402+ struct cb_discovery_data * cdd = user_data ;
403+ struct nvme_fabrics_config cfg ;
404+ struct tr_config trcfg = {};
405+ char * ptr , * p , line [4096 ];
406+ int argc , ret = 0 ;
407+ bool force = false;
408+
409+ NVMF_ARGS (opts , trcfg , cfg ,
410+ OPT_FLAG ("persistent" , 'p' , & persistent , "persistent discovery connection" ),
411+ OPT_FLAG ("force" , 0 , & force , "Force persistent discovery controller creation" ));
412+
413+ memcpy (& cfg , cdd -> defcfg , sizeof (cfg ));
414+ next :
415+ if (fgets (line , sizeof (line ), cdd -> f ) == NULL )
416+ return - EOF ;
417+
418+ if (line [0 ] == '#' || line [0 ] == '\n' )
419+ goto next ;
420+
421+ argc = 1 ;
422+ p = line ;
423+ while ((ptr = strsep (& p , " =\n" )) != NULL )
424+ cdd -> argv [argc ++ ] = ptr ;
425+ cdd -> argv [argc ] = NULL ;
426+
427+ trcfg .subsysnqn = NVME_DISC_SUBSYS_NAME ;
428+ ret = argconfig_parse (argc , cdd -> argv , "config" , opts );
429+ if (ret )
430+ goto next ;
431+ if (!trcfg .transport && !trcfg .traddr )
432+ goto next ;
433+
434+ if (!trcfg .trsvcid )
435+ trcfg .trsvcid = nvmf_get_default_trsvcid (trcfg .transport , true);
436+
437+ ret = nvmf_discovery_ctx_subsysnqn_set (dctx , trcfg .subsysnqn );
438+ if (ret )
439+ return ret ;
440+
441+ ret = nvmf_discovery_ctx_transport_set (dctx , trcfg .transport );
442+ if (ret )
443+ return ret ;
444+
445+ ret = nvmf_discovery_ctx_traddr_set (dctx , trcfg .traddr );
446+ if (ret )
447+ return ret ;
448+
449+ ret = nvmf_discovery_ctx_host_traddr_set (dctx , trcfg .host_traddr );
450+ if (ret )
451+ return ret ;
452+
453+ ret = nvmf_discovery_ctx_host_iface_set (dctx , trcfg .host_iface );
454+ if (ret )
455+ return ret ;
456+
457+ ret = nvmf_discovery_ctx_trsvcid_set (dctx , trcfg .trsvcid );
458+ if (ret )
459+ return ret ;
460+
461+ ret = nvmf_discovery_ctx_hostnqn_set (dctx , trcfg .hostnqn );
462+ if (ret )
463+ return ret ;
464+
465+ ret = nvmf_discovery_ctx_hostid_set (dctx , trcfg .hostid );
466+ if (ret )
467+ return ret ;
468+
469+ ret = nvmf_discovery_ctx_hostkey_set (dctx , trcfg .hostkey );
470+ if (ret )
471+ return ret ;
472+
473+ ret = nvmf_discovery_ctx_ctrlkey_set (dctx , trcfg .ctrlkey );
474+ if (ret )
475+ return ret ;
476+
477+ ret = nvmf_discovery_ctx_keyring_set (dctx , trcfg .keyring );
478+ if (ret )
479+ return ret ;
480+
481+ ret = nvmf_discovery_ctx_tls_key_set (dctx , trcfg .tls_key );
482+ if (ret )
483+ return ret ;
484+
485+ ret = nvmf_discovery_ctx_tls_key_identity_set (dctx ,
486+ trcfg .tls_key_identity );
487+ if (ret )
488+ return ret ;
489+
490+ ret = nvmf_discovery_ctx_default_fabrics_config_set (dctx , & cfg );
491+ if (ret )
492+ return ret ;
493+
494+ return 0 ;
495+ }
496+
497+
370498static int create_discovery_log_ctx (struct nvme_global_ctx * ctx ,
371499 bool persistent , struct tr_config * trcfg ,
372500 struct nvme_fabrics_config * defcfg ,
@@ -403,6 +531,18 @@ static int create_discovery_log_ctx(struct nvme_global_ctx *ctx,
403531 if (err )
404532 goto err ;
405533
534+ err = nvmf_discovery_ctx_parser_init_set (dctx , parser_init );
535+ if (err )
536+ goto err ;
537+
538+ err = nvmf_discovery_ctx_parser_cleanup_set (dctx , parser_cleanup );
539+ if (err )
540+ goto err ;
541+
542+ err = nvmf_discovery_ctx_parser_next_line_set (dctx , parser_next_line );
543+ if (err )
544+ goto err ;
545+
406546 err = nvmf_discovery_ctx_persistent_set (dctx , persistent );
407547 if (err )
408548 goto err ;
@@ -427,107 +567,6 @@ static int create_discovery_log_ctx(struct nvme_global_ctx *ctx,
427567 return err ;
428568}
429569
430- static int discover_from_conf_file (struct nvme_global_ctx * ctx , nvme_host_t h ,
431- const char * desc , bool connect ,
432- const struct nvme_fabrics_config * defcfg )
433- {
434- _cleanup_free_ struct nvmf_discovery_ctx * dctx = NULL ;
435- char * ptr , * * argv , * p , line [4096 ];
436- int argc , ret = 0 ;
437- unsigned int verbose = 0 ;
438- _cleanup_file_ FILE * f = NULL ;
439- nvme_print_flags_t flags ;
440- char * format = "normal" ;
441- struct nvme_fabrics_config cfg ;
442- struct tr_config trcfg ;
443- bool force = false;
444- NVMF_ARGS (opts , trcfg , cfg ,
445- OPT_FMT ("output-format" , 'o' , & format , output_format ),
446- OPT_FILE ("raw" , 'r' , & raw , "save raw output to file" ),
447- OPT_FLAG ("persistent" , 'p' , & persistent , "persistent discovery connection" ),
448- OPT_FLAG ("quiet" , 0 , & quiet , "suppress already connected errors" ),
449- OPT_INCR ("verbose" , 'v' , & verbose , "Increase logging verbosity" ),
450- OPT_FLAG ("force" , 0 , & force , "Force persistent discovery controller creation" ));
451-
452- nvmf_default_config (& cfg );
453-
454- ret = validate_output_format (format , & flags );
455- if (ret < 0 ) {
456- nvme_show_error ("Invalid output format" );
457- return ret ;
458- }
459-
460- f = fopen (PATH_NVMF_DISC , "r" );
461- if (f == NULL ) {
462- fprintf (stderr , "No params given and no %s\n" , PATH_NVMF_DISC );
463- return - ENOENT ;
464- }
465-
466- argv = calloc (MAX_DISC_ARGS , sizeof (char * ));
467- if (!argv )
468- return -1 ;
469-
470- argv [0 ] = "discover" ;
471- memset (line , 0 , sizeof (line ));
472- while (fgets (line , sizeof (line ), f ) != NULL ) {
473- nvme_ctrl_t c ;
474-
475- if (line [0 ] == '#' || line [0 ] == '\n' )
476- continue ;
477-
478- argc = 1 ;
479- p = line ;
480- while ((ptr = strsep (& p , " =\n" )) != NULL )
481- argv [argc ++ ] = ptr ;
482- argv [argc ] = NULL ;
483-
484- memcpy (& cfg , defcfg , sizeof (cfg ));
485- trcfg .subsysnqn = NVME_DISC_SUBSYS_NAME ;
486- ret = argconfig_parse (argc , argv , desc , opts );
487- if (ret )
488- goto next ;
489- if (!trcfg .transport && !trcfg .traddr )
490- goto next ;
491-
492- if (!trcfg .trsvcid )
493- trcfg .trsvcid =
494- nvmf_get_default_trsvcid (trcfg .transport , true);
495-
496- struct cb_discovery_log_data dld = {
497- .flags = flags ,
498- .raw = raw ,
499- };
500- ret = create_discovery_log_ctx (ctx , true, & trcfg , & cfg ,
501- & dld , & dctx );
502- if (ret )
503- return ret ;
504-
505- if (!force ) {
506- c = lookup_ctrl (h , & trcfg );
507- if (c ) {
508- nvmf_discovery (ctx , dctx , connect , c );
509- goto next ;
510- }
511- }
512-
513- ret = nvmf_create_discover_ctrl (ctx , h , & cfg , & trcfg , & c );
514- if (ret )
515- goto next ;
516-
517- nvmf_discovery_ctx_persistent_set (dctx , persistent );
518- nvmf_discovery (ctx , dctx , connect , c );
519- if (!(persistent || is_persistent_discovery_ctrl (h , c )))
520- ret = nvme_disconnect_ctrl (c );
521- nvme_free_ctrl (c );
522-
523- next :
524- memset (& cfg , 0 , sizeof (cfg ));
525- }
526- free (argv );
527-
528- return ret ;
529- }
530-
531570static int nvme_read_volatile_config (struct nvme_global_ctx * ctx )
532571{
533572 char * filename , * ext ;
@@ -669,7 +708,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
669708 if (trcfg .hostkey )
670709 nvme_host_set_dhchap_key (h , trcfg .hostkey );
671710
672- struct cb_discovery_log_data dld = {
711+ struct cb_discovery_data dld = {
673712 .flags = flags ,
674713 .raw = raw ,
675714 };
@@ -692,7 +731,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
692731 if (ret || access (PATH_NVMF_DISC , F_OK ))
693732 goto out_free ;
694733
695- ret = discover_from_conf_file (ctx , h , desc , connect , & cfg );
734+ ret = nvmf_discovery_config_file (ctx , dctx , h , connect , force );
696735 goto out_free ;
697736 }
698737
0 commit comments