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:
parent
576f8ed256
commit
d81f3dfd3e
@ -39,23 +39,10 @@
|
|||||||
#include "spdk/env.h"
|
#include "spdk/env.h"
|
||||||
#include "spdk/nvme.h"
|
#include "spdk/nvme.h"
|
||||||
#include "spdk/nvmf.h"
|
#include "spdk/nvmf.h"
|
||||||
|
#include "spdk/string.h"
|
||||||
#include "spdk/util.h"
|
#include "spdk/util.h"
|
||||||
|
|
||||||
#include "nvmf_tgt.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
|
static int
|
||||||
json_write_hex_str(struct spdk_json_write_ctx *w, const void *data, size_t size)
|
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_name(w, "name");
|
||||||
spdk_json_write_string(w, spdk_bdev_get_name(spdk_nvmf_ns_get_bdev(ns)));
|
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");
|
spdk_json_write_name(w, "nguid");
|
||||||
json_write_hex_str(w, ns_opts.nguid, sizeof(ns_opts.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");
|
spdk_json_write_name(w, "eui64");
|
||||||
json_write_hex_str(w, ns_opts.eui64, sizeof(ns_opts.eui64));
|
json_write_hex_str(w, ns_opts.eui64, sizeof(ns_opts.eui64));
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,7 @@
|
|||||||
#include "spdk/nvme_intel.h"
|
#include "spdk/nvme_intel.h"
|
||||||
#include "spdk/nvmf_spec.h"
|
#include "spdk/nvmf_spec.h"
|
||||||
#include "spdk/pci_ids.h"
|
#include "spdk/pci_ids.h"
|
||||||
|
#include "spdk/string.h"
|
||||||
#include "spdk/util.h"
|
#include "spdk/util.h"
|
||||||
|
|
||||||
#define MAX_DISCOVERY_LOG_ENTRIES ((uint64_t)1000)
|
#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
|
static void
|
||||||
get_feature_completion(void *cb_arg, const struct spdk_nvme_cpl *cpl)
|
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) {
|
if (nsdata->noiob) {
|
||||||
printf("Optimal I/O Boundary: %u blocks\n", 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: ");
|
printf("NGUID: ");
|
||||||
print_hex_be(nsdata->nguid, sizeof(nsdata->nguid));
|
print_hex_be(nsdata->nguid, sizeof(nsdata->nguid));
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
if (!all_zero(&nsdata->eui64, sizeof(nsdata->eui64))) {
|
if (!spdk_mem_all_zero(&nsdata->eui64, sizeof(nsdata->eui64))) {
|
||||||
printf("EUI64: ");
|
printf("EUI64: ");
|
||||||
print_hex_be(&nsdata->eui64, sizeof(nsdata->eui64));
|
print_hex_be(&nsdata->eui64, sizeof(nsdata->eui64));
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
@ -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);
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -36,6 +36,7 @@
|
|||||||
#include "nvme_internal.h"
|
#include "nvme_internal.h"
|
||||||
|
|
||||||
#include "spdk/env.h"
|
#include "spdk/env.h"
|
||||||
|
#include "spdk/string.h"
|
||||||
|
|
||||||
#include <uuid/uuid.h>
|
#include <uuid/uuid.h>
|
||||||
|
|
||||||
@ -934,10 +935,8 @@ static int
|
|||||||
nvme_ctrlr_set_host_id(struct spdk_nvme_ctrlr *ctrlr)
|
nvme_ctrlr_set_host_id(struct spdk_nvme_ctrlr *ctrlr)
|
||||||
{
|
{
|
||||||
struct nvme_completion_poll_status status;
|
struct nvme_completion_poll_status status;
|
||||||
bool all_zeroes;
|
|
||||||
uint8_t *host_id;
|
uint8_t *host_id;
|
||||||
uint32_t host_id_size;
|
uint32_t host_id_size;
|
||||||
uint32_t i;
|
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
if (ctrlr->trid.trtype != SPDK_NVME_TRANSPORT_PCIE) {
|
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. */
|
/* If the user specified an all-zeroes host identifier, don't send the command. */
|
||||||
all_zeroes = true;
|
if (spdk_mem_all_zero(host_id, host_id_size)) {
|
||||||
for (i = 0; i < host_id_size; i++) {
|
|
||||||
if (host_id[i] != 0) {
|
|
||||||
all_zeroes = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (all_zeroes) {
|
|
||||||
SPDK_DEBUGLOG(SPDK_LOG_NVME,
|
SPDK_DEBUGLOG(SPDK_LOG_NVME,
|
||||||
"User did not specify host ID - not sending Set Features - Host ID\n");
|
"User did not specify host ID - not sending Set Features - Host ID\n");
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -390,3 +390,17 @@ spdk_parse_capacity(const char *cap_str, uint64_t *cap, bool *has_prefix)
|
|||||||
|
|
||||||
return 0;
|
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;
|
||||||
|
}
|
||||||
|
@ -174,21 +174,6 @@ spdk_nvmf_poll_group_resume_subsystem(struct spdk_nvmf_poll_group *group,
|
|||||||
return 0;
|
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
|
static void
|
||||||
test_discovery_log(void)
|
test_discovery_log(void)
|
||||||
{
|
{
|
||||||
@ -239,7 +224,7 @@ test_discovery_log(void)
|
|||||||
CU_ASSERT(disc_log->genctr != 0);
|
CU_ASSERT(disc_log->genctr != 0);
|
||||||
CU_ASSERT(disc_log->numrec == 1);
|
CU_ASSERT(disc_log->numrec == 1);
|
||||||
CU_ASSERT(disc_log->entries[0].trtype == 42);
|
CU_ASSERT(disc_log->entries[0].trtype == 42);
|
||||||
CU_ASSERT(all_zero(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]))));
|
sizeof(buffer) - (sizeof(*disc_log) + sizeof(disc_log->entries[0]))));
|
||||||
|
|
||||||
/* Get just the first entry, no header */
|
/* Get just the first entry, no header */
|
||||||
|
Loading…
Reference in New Issue
Block a user