@@ -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 ,
@@ -345,15 +348,15 @@ static bool nvmf_decide_retry(struct nvmf_discovery_ctx *dctx, int err,
345348static void nvmf_connected (struct nvmf_discovery_ctx * dctx ,
346349 struct nvme_ctrl * c , void * user_data )
347350{
348- struct cb_discovery_log_data * dld = user_data ;
351+ struct cb_discovery_data * cdd = user_data ;
349352
350- if (dld -> flags == NORMAL ) {
353+ if (cdd -> flags == NORMAL ) {
351354 printf ("device: %s\n" , nvme_ctrl_get_name (c ));
352355 return ;
353356 }
354357
355358#ifdef CONFIG_JSONC
356- if (dld -> flags == JSON ) {
359+ if (cdd -> flags == JSON ) {
357360 struct json_object * root ;
358361
359362 root = json_create_object ();
@@ -368,6 +371,131 @@ static void nvmf_connected(struct nvmf_discovery_ctx *dctx,
368371#endif
369372}
370373
374+ static int parser_init (struct nvmf_discovery_ctx * dctx , void * user_data )
375+ {
376+ struct cb_discovery_data * cdd = user_data ;
377+
378+ cdd -> f = fopen (PATH_NVMF_DISC , "r" );
379+ if (cdd -> f == NULL ) {
380+ fprintf (stderr , "No params given and no %s\n" , PATH_NVMF_DISC );
381+ return - ENOENT ;
382+ }
383+
384+ cdd -> argv = calloc (MAX_DISC_ARGS , sizeof (char * ));
385+ if (!cdd -> argv )
386+ return -1 ;
387+
388+ cdd -> argv [0 ] = "discover" ;
389+
390+ return 0 ;
391+ }
392+
393+ static void parser_cleanup (struct nvmf_discovery_ctx * dctx , void * user_data )
394+ {
395+ struct cb_discovery_data * cdd = user_data ;
396+
397+ free (cdd -> argv );
398+ fclose (cdd -> f );
399+ }
400+
401+ static int parser_next_line (struct nvmf_discovery_ctx * dctx , void * user_data )
402+ {
403+ struct cb_discovery_data * cdd = user_data ;
404+ struct nvme_fabrics_config cfg ;
405+ struct tr_config trcfg = {};
406+ char * ptr , * p , line [4096 ];
407+ int argc , ret = 0 ;
408+ bool force = false;
409+
410+ NVMF_ARGS (opts , trcfg , cfg ,
411+ OPT_FLAG ("persistent" , 'p' , & persistent , "persistent discovery connection" ),
412+ OPT_FLAG ("force" , 0 , & force , "Force persistent discovery controller creation" ));
413+
414+ memcpy (& cfg , cdd -> defcfg , sizeof (cfg ));
415+ next :
416+ if (fgets (line , sizeof (line ), cdd -> f ) == NULL )
417+ return - EOF ;
418+
419+ if (line [0 ] == '#' || line [0 ] == '\n' )
420+ goto next ;
421+
422+ argc = 1 ;
423+ p = line ;
424+ while ((ptr = strsep (& p , " =\n" )) != NULL )
425+ cdd -> argv [argc ++ ] = ptr ;
426+ cdd -> argv [argc ] = NULL ;
427+
428+ trcfg .subsysnqn = NVME_DISC_SUBSYS_NAME ;
429+ ret = argconfig_parse (argc , cdd -> argv , "config" , opts );
430+ if (ret )
431+ goto next ;
432+ if (!trcfg .transport && !trcfg .traddr )
433+ goto next ;
434+
435+ if (!trcfg .trsvcid )
436+ trcfg .trsvcid = nvmf_get_default_trsvcid (trcfg .transport , true);
437+
438+ ret = nvmf_discovery_ctx_subsysnqn_set (dctx , trcfg .subsysnqn );
439+ if (ret )
440+ return ret ;
441+
442+ ret = nvmf_discovery_ctx_transport_set (dctx , trcfg .transport );
443+ if (ret )
444+ return ret ;
445+
446+ ret = nvmf_discovery_ctx_traddr_set (dctx , trcfg .traddr );
447+ if (ret )
448+ return ret ;
449+
450+ ret = nvmf_discovery_ctx_host_traddr_set (dctx , trcfg .host_traddr );
451+ if (ret )
452+ return ret ;
453+
454+ ret = nvmf_discovery_ctx_host_iface_set (dctx , trcfg .host_iface );
455+ if (ret )
456+ return ret ;
457+
458+ ret = nvmf_discovery_ctx_trsvcid_set (dctx , trcfg .trsvcid );
459+ if (ret )
460+ return ret ;
461+
462+ ret = nvmf_discovery_ctx_hostnqn_set (dctx , trcfg .hostnqn );
463+ if (ret )
464+ return ret ;
465+
466+ ret = nvmf_discovery_ctx_hostid_set (dctx , trcfg .hostid );
467+ if (ret )
468+ return ret ;
469+
470+ ret = nvmf_discovery_ctx_hostkey_set (dctx , trcfg .hostkey );
471+ if (ret )
472+ return ret ;
473+
474+ ret = nvmf_discovery_ctx_ctrlkey_set (dctx , trcfg .ctrlkey );
475+ if (ret )
476+ return ret ;
477+
478+ ret = nvmf_discovery_ctx_keyring_set (dctx , trcfg .keyring );
479+ if (ret )
480+ return ret ;
481+
482+ ret = nvmf_discovery_ctx_tls_key_set (dctx , trcfg .tls_key );
483+ if (ret )
484+ return ret ;
485+
486+ ret = nvmf_discovery_ctx_tls_key_identity_set (dctx ,
487+ trcfg .tls_key_identity );
488+ if (ret )
489+ return ret ;
490+
491+ ret = nvmf_discovery_ctx_default_fabrics_config_set (dctx , & cfg );
492+ if (ret )
493+ return ret ;
494+
495+ return 0 ;
496+ }
497+
498+
371499static int create_discovery_log_ctx (struct nvme_global_ctx * ctx ,
372500 bool persistent , struct tr_config * trcfg ,
373501 struct nvme_fabrics_config * defcfg ,
@@ -404,6 +532,18 @@ static int create_discovery_log_ctx(struct nvme_global_ctx *ctx,
404532 if (err )
405533 goto err ;
406534
535+ err = nvmf_discovery_ctx_parser_init_set (dctx , parser_init );
536+ if (err )
537+ goto err ;
538+
539+ err = nvmf_discovery_ctx_parser_cleanup_set (dctx , parser_cleanup );
540+ if (err )
541+ goto err ;
542+
543+ err = nvmf_discovery_ctx_parser_next_line_set (dctx , parser_next_line );
544+ if (err )
545+ goto err ;
546+
407547 err = nvmf_discovery_ctx_persistent_set (dctx , persistent );
408548 if (err )
409549 goto err ;
@@ -428,107 +568,6 @@ static int create_discovery_log_ctx(struct nvme_global_ctx *ctx,
428568 return err ;
429569}
430570
431- static int discover_from_conf_file (struct nvme_global_ctx * ctx , nvme_host_t h ,
432- const char * desc , bool connect ,
433- const struct nvme_fabrics_config * defcfg )
434- {
435- _cleanup_free_ struct nvmf_discovery_ctx * dctx = NULL ;
436- char * ptr , * * argv , * p , line [4096 ];
437- int argc , ret = 0 ;
438- unsigned int verbose = 0 ;
439- _cleanup_file_ FILE * f = NULL ;
440- nvme_print_flags_t flags ;
441- char * format = "normal" ;
442- struct nvme_fabrics_config cfg ;
443- struct tr_config trcfg ;
444- bool force = false;
445- NVMF_ARGS (opts , trcfg , cfg ,
446- OPT_FMT ("output-format" , 'o' , & format , output_format ),
447- OPT_FILE ("raw" , 'r' , & raw , "save raw output to file" ),
448- OPT_FLAG ("persistent" , 'p' , & persistent , "persistent discovery connection" ),
449- OPT_FLAG ("quiet" , 0 , & quiet , "suppress already connected errors" ),
450- OPT_INCR ("verbose" , 'v' , & verbose , "Increase logging verbosity" ),
451- OPT_FLAG ("force" , 0 , & force , "Force persistent discovery controller creation" ));
452-
453- nvmf_default_config (& cfg );
454-
455- ret = validate_output_format (format , & flags );
456- if (ret < 0 ) {
457- nvme_show_error ("Invalid output format" );
458- return ret ;
459- }
460-
461- f = fopen (PATH_NVMF_DISC , "r" );
462- if (f == NULL ) {
463- fprintf (stderr , "No params given and no %s\n" , PATH_NVMF_DISC );
464- return - ENOENT ;
465- }
466-
467- argv = calloc (MAX_DISC_ARGS , sizeof (char * ));
468- if (!argv )
469- return -1 ;
470-
471- argv [0 ] = "discover" ;
472- memset (line , 0 , sizeof (line ));
473- while (fgets (line , sizeof (line ), f ) != NULL ) {
474- nvme_ctrl_t c ;
475-
476- if (line [0 ] == '#' || line [0 ] == '\n' )
477- continue ;
478-
479- argc = 1 ;
480- p = line ;
481- while ((ptr = strsep (& p , " =\n" )) != NULL )
482- argv [argc ++ ] = ptr ;
483- argv [argc ] = NULL ;
484-
485- memcpy (& cfg , defcfg , sizeof (cfg ));
486- trcfg .subsysnqn = NVME_DISC_SUBSYS_NAME ;
487- ret = argconfig_parse (argc , argv , desc , opts );
488- if (ret )
489- goto next ;
490- if (!trcfg .transport && !trcfg .traddr )
491- goto next ;
492-
493- if (!trcfg .trsvcid )
494- trcfg .trsvcid =
495- nvmf_get_default_trsvcid (trcfg .transport , true);
496-
497- struct cb_discovery_log_data dld = {
498- .flags = flags ,
499- .raw = raw ,
500- };
501- ret = create_discovery_log_ctx (ctx , true, & trcfg , & cfg ,
502- & dld , & dctx );
503- if (ret )
504- return ret ;
505-
506- if (!force ) {
507- c = lookup_ctrl (h , & trcfg );
508- if (c ) {
509- nvmf_discovery (ctx , dctx , connect , c );
510- goto next ;
511- }
512- }
513-
514- ret = nvmf_create_discover_ctrl (ctx , h , & cfg , & trcfg , & c );
515- if (ret )
516- goto next ;
517-
518- nvmf_discovery_ctx_persistent_set (dctx , persistent );
519- nvmf_discovery (ctx , dctx , connect , c );
520- if (!(persistent || is_persistent_discovery_ctrl (h , c )))
521- ret = nvme_disconnect_ctrl (c );
522- nvme_free_ctrl (c );
523-
524- next :
525- memset (& cfg , 0 , sizeof (cfg ));
526- }
527- free (argv );
528-
529- return ret ;
530- }
531-
532571static int nvme_read_volatile_config (struct nvme_global_ctx * ctx )
533572{
534573 char * filename , * ext ;
@@ -670,7 +709,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
670709 if (trcfg .hostkey )
671710 nvme_host_set_dhchap_key (h , trcfg .hostkey );
672711
673- struct cb_discovery_log_data dld = {
712+ struct cb_discovery_data dld = {
674713 .flags = flags ,
675714 .raw = raw ,
676715 };
@@ -693,7 +732,7 @@ int nvmf_discover(const char *desc, int argc, char **argv, bool connect)
693732 if (ret || access (PATH_NVMF_DISC , F_OK ))
694733 goto out_free ;
695734
696- ret = discover_from_conf_file (ctx , h , desc , connect , & cfg );
735+ ret = nvmf_discovery_config_file (ctx , dctx , h , connect , force );
697736 goto out_free ;
698737 }
699738
0 commit comments