diff --git a/include/spdk/nvme.h b/include/spdk/nvme.h index 9e755d4612..8d43314c68 100644 --- a/include/spdk/nvme.h +++ b/include/spdk/nvme.h @@ -213,6 +213,22 @@ int spdk_nvme_transport_id_parse(struct spdk_nvme_transport_id *trid, const char */ int spdk_nvme_transport_id_parse_trtype(enum spdk_nvme_transport_type *trtype, const char *str); +/** + * Look up the string representation of a transport ID transport type. + * + * \param trtype Transport type to convert. + * \return Static string constant describing trtype, or NULL if trtype not found. + */ +const char *spdk_nvme_transport_id_trtype_str(enum spdk_nvme_transport_type trtype); + +/** + * Look up the string representation of a transport ID address family. + * + * \param adrfam Address family to convert. + * \return Static string constant describing adrfam, or NULL if adrmfam not found. + */ +const char *spdk_nvme_transport_id_adrfam_str(enum spdk_nvmf_adrfam adrfam); + /** * Parse the string representation of a tranport ID address family. * diff --git a/lib/bdev/nvme/blockdev_nvme.c b/lib/bdev/nvme/blockdev_nvme.c index e3a3161673..3541f1ab6b 100644 --- a/lib/bdev/nvme/blockdev_nvme.c +++ b/lib/bdev/nvme/blockdev_nvme.c @@ -460,6 +460,8 @@ bdev_nvme_dump_config_json(void *ctx, struct spdk_json_write_ctx *w) struct spdk_nvme_ns *ns; union spdk_nvme_vs_register vs; union spdk_nvme_csts_register csts; + const char *trtype_str; + const char *adrfam_str; char buf[128]; cdata = spdk_nvme_ctrlr_get_data(nvme_bdev->nvme_ctrlr->ctrlr); @@ -478,13 +480,16 @@ bdev_nvme_dump_config_json(void *ctx, struct spdk_json_write_ctx *w) spdk_json_write_name(w, "trid"); spdk_json_write_object_begin(w); - spdk_json_write_name(w, "trtype"); - if (nvme_ctrlr->trid.trtype == SPDK_NVME_TRANSPORT_PCIE) { - spdk_json_write_string(w, "PCIe"); - } else if (nvme_ctrlr->trid.trtype == SPDK_NVME_TRANSPORT_RDMA) { - spdk_json_write_string(w, "RDMA"); - } else { - spdk_json_write_string(w, "Unknown"); + trtype_str = spdk_nvme_transport_id_trtype_str(nvme_ctrlr->trid.trtype); + if (trtype_str) { + spdk_json_write_name(w, "trtype"); + spdk_json_write_string(w, trtype_str); + } + + adrfam_str = spdk_nvme_transport_id_adrfam_str(nvme_ctrlr->trid.adrfam); + if (adrfam_str) { + spdk_json_write_name(w, "adrfam"); + spdk_json_write_string(w, adrfam_str); } if (nvme_ctrlr->trid.traddr[0] != '\0') { diff --git a/lib/nvme/nvme.c b/lib/nvme/nvme.c index 94d1c34dd0..3602fb0f96 100644 --- a/lib/nvme/nvme.c +++ b/lib/nvme/nvme.c @@ -467,6 +467,19 @@ spdk_nvme_transport_id_parse_trtype(enum spdk_nvme_transport_type *trtype, const return 0; } +const char * +spdk_nvme_transport_id_trtype_str(enum spdk_nvme_transport_type trtype) +{ + switch (trtype) { + case SPDK_NVME_TRANSPORT_PCIE: + return "PCIe"; + case SPDK_NVME_TRANSPORT_RDMA: + return "RDMA"; + default: + return NULL; + } +} + int spdk_nvme_transport_id_parse_adrfam(enum spdk_nvmf_adrfam *adrfam, const char *str) { @@ -488,6 +501,23 @@ spdk_nvme_transport_id_parse_adrfam(enum spdk_nvmf_adrfam *adrfam, const char *s return 0; } +const char * +spdk_nvme_transport_id_adrfam_str(enum spdk_nvmf_adrfam adrfam) +{ + switch (adrfam) { + case SPDK_NVMF_ADRFAM_IPV4: + return "IPv4"; + case SPDK_NVMF_ADRFAM_IPV6: + return "IPv6"; + case SPDK_NVMF_ADRFAM_IB: + return "IB"; + case SPDK_NVMF_ADRFAM_FC: + return "FC"; + default: + return NULL; + } +} + int spdk_nvme_transport_id_parse(struct spdk_nvme_transport_id *trid, const char *str) { diff --git a/test/unit/lib/nvme/nvme.c/nvme_ut.c b/test/unit/lib/nvme/nvme.c/nvme_ut.c index 1b4e9ff753..059a86d91d 100644 --- a/test/unit/lib/nvme/nvme.c/nvme_ut.c +++ b/test/unit/lib/nvme/nvme.c/nvme_ut.c @@ -381,6 +381,48 @@ test_spdk_nvme_transport_id_parse_adrfam(void) } +static void +test_trid_trtype_str(void) +{ + const char *s; + + s = spdk_nvme_transport_id_trtype_str(-5); + CU_ASSERT(s == NULL); + + s = spdk_nvme_transport_id_trtype_str(SPDK_NVME_TRANSPORT_PCIE); + SPDK_CU_ASSERT_FATAL(s != NULL); + CU_ASSERT(strcmp(s, "PCIe") == 0); + + s = spdk_nvme_transport_id_trtype_str(SPDK_NVME_TRANSPORT_RDMA); + SPDK_CU_ASSERT_FATAL(s != NULL); + CU_ASSERT(strcmp(s, "RDMA") == 0); +} + +static void +test_trid_adrfam_str(void) +{ + const char *s; + + s = spdk_nvme_transport_id_adrfam_str(-5); + CU_ASSERT(s == NULL); + + s = spdk_nvme_transport_id_adrfam_str(SPDK_NVMF_ADRFAM_IPV4); + SPDK_CU_ASSERT_FATAL(s != NULL); + CU_ASSERT(strcmp(s, "IPv4") == 0); + + s = spdk_nvme_transport_id_adrfam_str(SPDK_NVMF_ADRFAM_IPV6); + SPDK_CU_ASSERT_FATAL(s != NULL); + CU_ASSERT(strcmp(s, "IPv6") == 0); + + s = spdk_nvme_transport_id_adrfam_str(SPDK_NVMF_ADRFAM_IB); + SPDK_CU_ASSERT_FATAL(s != NULL); + CU_ASSERT(strcmp(s, "IB") == 0); + + s = spdk_nvme_transport_id_adrfam_str(SPDK_NVMF_ADRFAM_FC); + SPDK_CU_ASSERT_FATAL(s != NULL); + CU_ASSERT(strcmp(s, "FC") == 0); +} + int main(int argc, char **argv) { CU_pSuite suite = NULL; @@ -405,6 +447,8 @@ int main(int argc, char **argv) test_spdk_nvme_transport_id_parse_adrfam) == NULL || CU_add_test(suite, "test_trid_parse_and_compare", test_trid_parse_and_compare) == NULL || + CU_add_test(suite, "test_trid_trtype_str", test_trid_trtype_str) == NULL || + CU_add_test(suite, "test_trid_adrfam_str", test_trid_adrfam_str) == NULL || CU_add_test(suite, "test_nvme_robust_mutex_init_shared", test_nvme_robust_mutex_init_shared) == NULL ) {