Commit d81f3dfd authored by Daniel Verkamp's avatar Daniel Verkamp Committed by Jim Harris
Browse files

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: default avatarDaniel Verkamp <daniel.verkamp@intel.com>
Reviewed-on: https://review.gerrithub.io/400892


Tested-by: default avatarSPDK Automated Test System <sys_sgsw@intel.com>
Reviewed-by: default avatarChangpeng Liu <changpeng.liu@intel.com>
Reviewed-by: default avatarJim Harris <james.r.harris@intel.com>
parent 576f8ed2
Loading
Loading
Loading
Loading
+3 −16
Original line number Diff line number Diff line
@@ -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));
			}
+3 −15
Original line number Diff line number Diff line
@@ -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");
+9 −0
Original line number Diff line number Diff line
@@ -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
+2 −11
Original line number Diff line number Diff line
@@ -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;
+14 −0
Original line number Diff line number Diff line
@@ -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;
}
Loading