diff --git a/include/spdk/conf.h b/include/spdk/conf.h index efbea13a2..e79e831e3 100644 --- a/include/spdk/conf.h +++ b/include/spdk/conf.h @@ -63,6 +63,7 @@ char *spdk_conf_section_get_nmval(struct spdk_conf_section *sp, const char *key, char *spdk_conf_section_get_nval(struct spdk_conf_section *sp, const char *key, int idx); char *spdk_conf_section_get_val(struct spdk_conf_section *sp, const char *key); int spdk_conf_section_get_intval(struct spdk_conf_section *sp, const char *key); +bool spdk_conf_section_get_boolval(struct spdk_conf_section *sp, const char *key, bool default_val); void spdk_conf_set_as_default(struct spdk_conf *cp); diff --git a/lib/bdev/nvme/blockdev_nvme.c b/lib/bdev/nvme/blockdev_nvme.c index f8b55bbd4..35b565327 100644 --- a/lib/bdev/nvme/blockdev_nvme.c +++ b/lib/bdev/nvme/blockdev_nvme.c @@ -112,7 +112,7 @@ struct nvme_probe_ctx { }; static int g_hot_insert_nvme_controller_index = 0; -static int g_reset_controller_on_timeout = 0; +static bool g_reset_controller_on_timeout = false; static int g_timeout = 0; static int g_nvme_adminq_poll_timeout_us = 0; static int g_nvme_hotplug_poll_timeout_us = 0; @@ -768,12 +768,8 @@ bdev_nvme_library_init(void) probe_ctx.count++; } - val = spdk_conf_section_get_val(sp, "ResetControllerOnTimeout"); - if (val != NULL) { - if (!strcmp(val, "Yes")) { - g_reset_controller_on_timeout = 1; - } - } + g_reset_controller_on_timeout = + spdk_conf_section_get_boolval(sp, "ResetControllerOnTimeout", false); if ((g_timeout = spdk_conf_section_get_intval(sp, "NvmeTimeoutValue")) < 0) { g_timeout = 0; diff --git a/lib/conf/conf.c b/lib/conf/conf.c index 8cbeeddca..63719eac5 100644 --- a/lib/conf/conf.c +++ b/lib/conf/conf.c @@ -427,6 +427,27 @@ spdk_conf_section_get_intval(struct spdk_conf_section *sp, const char *key) return value; } +bool +spdk_conf_section_get_boolval(struct spdk_conf_section *sp, const char *key, bool default_val) +{ + const char *v; + + v = spdk_conf_section_get_nval(sp, key, 0); + if (v == NULL) { + return default_val; + } + + if (!strcasecmp(v, "Yes") || !strcasecmp(v, "Y") || !strcasecmp(v, "True")) { + return true; + } + + if (!strcasecmp(v, "No") || !strcasecmp(v, "N") || !strcasecmp(v, "False")) { + return false; + } + + return default_val; +} + static int parse_line(struct spdk_conf *cp, char *lp) { diff --git a/lib/copy/ioat/copy_engine_ioat.c b/lib/copy/ioat/copy_engine_ioat.c index f3563b812..1227c7c60 100644 --- a/lib/copy/ioat/copy_engine_ioat.c +++ b/lib/copy/ioat/copy_engine_ioat.c @@ -277,18 +277,16 @@ static int copy_engine_ioat_init(void) { struct spdk_conf_section *sp = spdk_conf_find_section(NULL, "Ioat"); - const char *val, *pci_bdf; + const char *pci_bdf; int i; struct ioat_probe_ctx probe_ctx = {}; if (sp != NULL) { - val = spdk_conf_section_get_val(sp, "Disable"); - if (val != NULL) { + if (spdk_conf_section_get_boolval(sp, "Disable", false)) { /* Disable Ioat */ - if (!strcmp(val, "Yes")) { - return 0; - } + return 0; } + /*Init the whitelist*/ for (i = 0; i < IOAT_MAX_CHANNELS; i++) { pci_bdf = spdk_conf_section_get_nmval(sp, "Whitelist", i, 0); diff --git a/lib/rpc/rpc.c b/lib/rpc/rpc.c index 5604f795f..684abb796 100644 --- a/lib/rpc/rpc.c +++ b/lib/rpc/rpc.c @@ -83,23 +83,13 @@ static int enable_rpc(void) { struct spdk_conf_section *sp; - char *val; sp = spdk_conf_find_section(NULL, "Rpc"); if (sp == NULL) { return 0; } - val = spdk_conf_section_get_val(sp, "Enable"); - if (val == NULL) { - return 0; - } - - if (!strcmp(val, "Yes")) { - return 1; - } - - return 0; + return spdk_conf_section_get_boolval(sp, "Enable", false); } static const char * diff --git a/lib/scsi/scsi.c b/lib/scsi/scsi.c index 246c17e80..368be82a8 100644 --- a/lib/scsi/scsi.c +++ b/lib/scsi/scsi.c @@ -88,17 +88,7 @@ spdk_read_config_scsi_parameters(void) g_spdk_scsi.scsi_params.unmap_granularity_alignment = (val == NULL) ? DEFAULT_UNMAP_GRANULARITY_ALIGNMENT : strtoul(val, NULL, 10); - val = spdk_conf_section_get_val(sp, "Ugavalid"); - if (val == NULL) { - g_spdk_scsi.scsi_params.ugavalid = DEFAULT_UGAVALID; - } else if (strcasecmp(val, "Yes") == 0) { - g_spdk_scsi.scsi_params.ugavalid = 1; - } else if (strcasecmp(val, "No") == 0) { - g_spdk_scsi.scsi_params.ugavalid = 0; - } else { - SPDK_ERRLOG("unknown value %s\n", val); - return -1; - } + g_spdk_scsi.scsi_params.ugavalid = spdk_conf_section_get_boolval(sp, "Ugavalid", DEFAULT_UGAVALID); val = spdk_conf_section_get_val(sp, "MaxWriteSameLength"); g_spdk_scsi.scsi_params.max_write_same_length = (val == NULL) ? diff --git a/test/lib/scsi/init/init_ut.c b/test/lib/scsi/init/init_ut.c index 3acfed93d..8d8402d28 100644 --- a/test/lib/scsi/init/init_ut.c +++ b/test/lib/scsi/init/init_ut.c @@ -274,6 +274,7 @@ scsi_init_ugavalid_no(void) static void scsi_init_ugavalid_unknown_value_failure(void) { + struct spdk_scsi_parameters params; int rc; struct spdk_conf *config; @@ -282,10 +283,13 @@ scsi_init_ugavalid_unknown_value_failure(void) config = spdk_config_init_scsi_params("Ugavalid", "unknown value"); spdk_conf_set_as_default(config); rc = spdk_scsi_subsystem_init(); + CU_ASSERT_EQUAL(rc, 0); - /* returns -1 since scsi_params.ugavalid is set to - * 'unknown value' */ - CU_ASSERT_TRUE(rc < 0); + /* Assert the scsi_params.ugavalid == DEFAULT_UGAVALID and + * assert the rest of the params are set to their default values */ + set_default_scsi_params(¶ms); + params.ugavalid = DEFAULT_UGAVALID; + CU_ASSERT(memcmp(&g_spdk_scsi.scsi_params, ¶ms, sizeof(params)) == 0); spdk_conf_free(config); }