diff --git a/app/nvmf_tgt/nvmf_rpc.c b/app/nvmf_tgt/nvmf_rpc.c index e1d1e5a3a..edcf0a273 100644 --- a/app/nvmf_tgt/nvmf_rpc.c +++ b/app/nvmf_tgt/nvmf_rpc.c @@ -39,23 +39,10 @@ #include "spdk/env.h" #include "spdk/nvme.h" #include "spdk/nvmf.h" +#include "spdk/string.h" #include "spdk/util.h" #include "nvmf_tgt.h" - -static bool -all_zero(const void *data, size_t size) -{ - const uint8_t *buf = data; - - while (size--) { - if (*buf++ != 0) { - return false; - } - } - return true; -} - static int json_write_hex_str(struct spdk_json_write_ctx *w, const void *data, size_t size) { @@ -267,12 +254,12 @@ dump_nvmf_subsystem(struct spdk_json_write_ctx *w, struct spdk_nvmf_subsystem *s spdk_json_write_name(w, "name"); spdk_json_write_string(w, spdk_bdev_get_name(spdk_nvmf_ns_get_bdev(ns))); - if (!all_zero(ns_opts.nguid, sizeof(ns_opts.nguid))) { + if (!spdk_mem_all_zero(ns_opts.nguid, sizeof(ns_opts.nguid))) { spdk_json_write_name(w, "nguid"); json_write_hex_str(w, ns_opts.nguid, sizeof(ns_opts.nguid)); } - if (!all_zero(ns_opts.eui64, sizeof(ns_opts.eui64))) { + if (!spdk_mem_all_zero(ns_opts.eui64, sizeof(ns_opts.eui64))) { spdk_json_write_name(w, "eui64"); json_write_hex_str(w, ns_opts.eui64, sizeof(ns_opts.eui64)); } diff --git a/examples/nvme/identify/identify.c b/examples/nvme/identify/identify.c index e7acc3989..c2b2b8a25 100644 --- a/examples/nvme/identify/identify.c +++ b/examples/nvme/identify/identify.c @@ -40,6 +40,7 @@ #include "spdk/nvme_intel.h" #include "spdk/nvmf_spec.h" #include "spdk/pci_ids.h" +#include "spdk/string.h" #include "spdk/util.h" #define MAX_DISCOVERY_LOG_ENTRIES ((uint64_t)1000) @@ -127,19 +128,6 @@ hex_dump(const void *data, size_t size) } } -static bool -all_zero(const void *data, size_t size) -{ - const uint8_t *buf = data; - - while (size--) { - if (*buf++ != 0) { - return false; - } - } - return true; -} - static void get_feature_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl) { @@ -521,12 +509,12 @@ print_namespace(struct spdk_nvme_ns *ns) if (nsdata->noiob) { printf("Optimal I/O Boundary: %u blocks\n", nsdata->noiob); } - if (!all_zero(nsdata->nguid, sizeof(nsdata->nguid))) { + if (!spdk_mem_all_zero(nsdata->nguid, sizeof(nsdata->nguid))) { printf("NGUID: "); print_hex_be(nsdata->nguid, sizeof(nsdata->nguid)); printf("\n"); } - if (!all_zero(&nsdata->eui64, sizeof(nsdata->eui64))) { + if (!spdk_mem_all_zero(&nsdata->eui64, sizeof(nsdata->eui64))) { printf("EUI64: "); print_hex_be(&nsdata->eui64, sizeof(nsdata->eui64)); printf("\n"); diff --git a/include/spdk/string.h b/include/spdk/string.h index e6e3f9334..3e6aecdb3 100644 --- a/include/spdk/string.h +++ b/include/spdk/string.h @@ -171,6 +171,15 @@ int spdk_parse_ip_addr(char *ip, char **host, char **port); */ int spdk_parse_capacity(const char *cap_str, uint64_t *cap, bool *has_prefix); +/** + * Check if a buffer is all zero (0x00) bytes or not. + * + * \param data Buffer to check. + * \param size Size of data in bytes. + * \return true if data consists entirely of zeroes, or false if any byte in data is not zero. + */ +bool spdk_mem_all_zero(const void *data, size_t size); + #ifdef __cplusplus } #endif diff --git a/lib/nvme/nvme_ctrlr.c b/lib/nvme/nvme_ctrlr.c index 7aa4b9deb..593c43430 100644 --- a/lib/nvme/nvme_ctrlr.c +++ b/lib/nvme/nvme_ctrlr.c @@ -36,6 +36,7 @@ #include "nvme_internal.h" #include "spdk/env.h" +#include "spdk/string.h" #include @@ -934,10 +935,8 @@ static int nvme_ctrlr_set_host_id(struct spdk_nvme_ctrlr *ctrlr) { struct nvme_completion_poll_status status; - bool all_zeroes; uint8_t *host_id; uint32_t host_id_size; - uint32_t i; int rc; if (ctrlr->trid.trtype != SPDK_NVME_TRANSPORT_PCIE) { @@ -960,15 +959,7 @@ nvme_ctrlr_set_host_id(struct spdk_nvme_ctrlr *ctrlr) } /* If the user specified an all-zeroes host identifier, don't send the command. */ - all_zeroes = true; - for (i = 0; i < host_id_size; i++) { - if (host_id[i] != 0) { - all_zeroes = false; - break; - } - } - - if (all_zeroes) { + if (spdk_mem_all_zero(host_id, host_id_size)) { SPDK_DEBUGLOG(SPDK_LOG_NVME, "User did not specify host ID - not sending Set Features - Host ID\n"); return 0; diff --git a/lib/util/string.c b/lib/util/string.c index 54f2abf9c..24bb1254c 100644 --- a/lib/util/string.c +++ b/lib/util/string.c @@ -390,3 +390,17 @@ spdk_parse_capacity(const char *cap_str, uint64_t *cap, bool *has_prefix) return 0; } + +bool +spdk_mem_all_zero(const void *data, size_t size) +{ + const uint8_t *buf = data; + + while (size--) { + if (*buf++ != 0) { + return false; + } + } + + return true; +} diff --git a/test/unit/lib/nvmf/ctrlr_discovery.c/ctrlr_discovery_ut.c b/test/unit/lib/nvmf/ctrlr_discovery.c/ctrlr_discovery_ut.c index 372fe1040..94481a812 100644 --- a/test/unit/lib/nvmf/ctrlr_discovery.c/ctrlr_discovery_ut.c +++ b/test/unit/lib/nvmf/ctrlr_discovery.c/ctrlr_discovery_ut.c @@ -174,21 +174,6 @@ spdk_nvmf_poll_group_resume_subsystem(struct spdk_nvmf_poll_group *group, return 0; } -static bool -all_zero(const void *buf, size_t size) -{ - const uint8_t *b = buf; - - while (size--) { - if (*b != 0) { - return false; - } - b++; - } - - return true; -} - static void test_discovery_log(void) { @@ -239,8 +224,8 @@ test_discovery_log(void) CU_ASSERT(disc_log->genctr != 0); CU_ASSERT(disc_log->numrec == 1); CU_ASSERT(disc_log->entries[0].trtype == 42); - CU_ASSERT(all_zero(buffer + sizeof(*disc_log) + sizeof(disc_log->entries[0]), - sizeof(buffer) - (sizeof(*disc_log) + sizeof(disc_log->entries[0])))); + CU_ASSERT(spdk_mem_all_zero(buffer + sizeof(*disc_log) + sizeof(disc_log->entries[0]), + sizeof(buffer) - (sizeof(*disc_log) + sizeof(disc_log->entries[0])))); /* Get just the first entry, no header */ memset(buffer, 0xCC, sizeof(buffer));