util: add spdk_mem_all_zero() function

Unify several similar functions checking for a buffer of all zero bytes
into the util library.

Change-Id: Idfbeffa22add34ac9ed1bd75ee27d6bd8b188940
Signed-off-by: Daniel Verkamp <daniel.verkamp@intel.com>
Reviewed-on: https://review.gerrithub.io/400892
Tested-by: SPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-by: Jim Harris <james.r.harris@intel.com>
This commit is contained in:
Daniel Verkamp 2018-02-21 16:04:17 -07:00 committed by Jim Harris
parent 576f8ed256
commit d81f3dfd3e
6 changed files with 33 additions and 59 deletions

View File

@ -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));
}

View File

@ -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");

View File

@ -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

View File

@ -36,6 +36,7 @@
#include "nvme_internal.h"
#include "spdk/env.h"
#include "spdk/string.h"
#include <uuid/uuid.h>
@ -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;

View File

@ -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;
}

View File

@ -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));