From 4c06ce9b9ddd4b0e5cbe42dc765c053c1a4f0540 Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Thu, 1 Mar 2018 17:59:50 -0700 Subject: [PATCH] util: add uuid.h to wrap libuuid This lets us have a common place to put definitions like the length of the UUID string, as well as abstract away some of the API warts in libuuid (non-const values, no size checking for uuid_unparse, etc.). Change-Id: I80607fcd21ce57fdbb8729442fbb721bc71ccb98 Signed-off-by: Daniel Verkamp Reviewed-on: https://review.gerrithub.io/402176 Tested-by: SPDK Automated Test System Reviewed-by: Tomasz Zawadzki Reviewed-by: Jim Harris Reviewed-by: Changpeng Liu --- include/spdk/uuid.h | 94 +++++++++++++++++++ include/spdk_internal/lvolstore.h | 8 +- lib/bdev/lvol/vbdev_lvol.c | 15 +-- lib/bdev/lvol/vbdev_lvol_rpc.c | 8 +- lib/lvol/lvol.c | 23 ++--- lib/nvme/nvme.c | 8 +- lib/nvme/nvme_ctrlr.c | 9 +- lib/nvme/nvme_internal.h | 3 +- lib/nvmf/subsystem.c | 7 +- lib/util/Makefile | 2 +- lib/util/uuid.c | 68 ++++++++++++++ .../lib/bdev/vbdev_lvol.c/vbdev_lvol_ut.c | 10 +- test/unit/lib/lvol/lvol.c/lvol_ut.c | 42 ++++----- .../lib/nvme/nvme_ctrlr.c/nvme_ctrlr_ut.c | 8 +- 14 files changed, 226 insertions(+), 79 deletions(-) create mode 100644 include/spdk/uuid.h create mode 100644 lib/util/uuid.c diff --git a/include/spdk/uuid.h b/include/spdk/uuid.h new file mode 100644 index 000000000..200fd7af8 --- /dev/null +++ b/include/spdk/uuid.h @@ -0,0 +1,94 @@ +/*- + * BSD LICENSE + * + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** \file + * UUID types and functions + */ + +#ifndef SPDK_UUID_H +#define SPDK_UUID_H + +#include "spdk/stdinc.h" + +#include "spdk/assert.h" + +#ifdef __cplusplus +extern "C" { +#endif + +struct spdk_uuid { + union { + uint8_t raw[16]; + } u; +}; +SPDK_STATIC_ASSERT(sizeof(struct spdk_uuid) == 16, "Incorrect size"); + +#define SPDK_UUID_STRING_LEN 37 /* 36 characters + null terminator */ + +/** + * Convert UUID in textual format into a spdk_uuid. + * + * \param uuid[output] User-provided UUID buffer. + * \param uuid_str UUID in textual format in C string. + * \return 0 on success, or negated errno on failure. + */ +int spdk_uuid_parse(struct spdk_uuid *uuid, const char *uuid_str); + +/** + * Convert UUID in spdk_uuid into lowercase textual format. + * + * \param uuid_str User-provided string buffer to write the textual format into. + * \param uuid_str_size Size of uuid_str buffer. Must be at least SPDK_UUID_STRING_LEN. + * \param uuid UUID to convert to textual format. + */ +int spdk_uuid_fmt_lower(char *uuid_str, size_t uuid_str_size, const struct spdk_uuid *uuid); + +/** + * Compare two UUIDs. + * + * \return 0 if u1 == u2, less than 0 if u1 < u2, greater than 0 if u1 > u2 + */ +int spdk_uuid_compare(const struct spdk_uuid *u1, const struct spdk_uuid *u2); + +/** + * Generate a new UUID. + * + * \param uuid[out] User-provided UUID buffer to fill. + */ +void spdk_uuid_generate(struct spdk_uuid *uuid); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/spdk_internal/lvolstore.h b/include/spdk_internal/lvolstore.h index 2838e7cd7..f45fa411f 100644 --- a/include/spdk_internal/lvolstore.h +++ b/include/spdk_internal/lvolstore.h @@ -36,16 +36,12 @@ #include "spdk/blob.h" #include "spdk/lvol.h" +#include "spdk/uuid.h" #include "spdk_internal/bdev.h" -#include - /* Default size of blobstore cluster */ #define SPDK_LVS_OPTS_CLUSTER_SZ (4 * 1024 * 1024) -/* Length of string returned from uuid_unparse() */ -#define UUID_STRING_LEN 37 - struct spdk_lvs_req { spdk_lvs_op_complete cb_fn; void *cb_arg; @@ -84,7 +80,7 @@ struct spdk_lvol_store { struct spdk_blob_store *blobstore; struct spdk_blob *super_blob; spdk_blob_id super_blob_id; - uuid_t uuid; + struct spdk_uuid uuid; struct spdk_lvs_req *destruct_req; int lvol_count; int lvols_opened; diff --git a/lib/bdev/lvol/vbdev_lvol.c b/lib/bdev/lvol/vbdev_lvol.c index 437e049b1..4e8172836 100644 --- a/lib/bdev/lvol/vbdev_lvol.c +++ b/lib/bdev/lvol/vbdev_lvol.c @@ -36,6 +36,7 @@ #include "spdk_internal/bdev.h" #include "spdk_internal/log.h" #include "spdk/string.h" +#include "spdk/uuid.h" #include "vbdev_lvol.h" @@ -374,14 +375,14 @@ vbdev_lvol_store_next(struct lvol_store_bdev *prev) } static struct spdk_lvol_store * -_vbdev_get_lvol_store_by_uuid(uuid_t uuid) +_vbdev_get_lvol_store_by_uuid(const struct spdk_uuid *uuid) { struct spdk_lvol_store *lvs = NULL; struct lvol_store_bdev *lvs_bdev = vbdev_lvol_store_first(); while (lvs_bdev != NULL) { lvs = lvs_bdev->lvs; - if (uuid_compare(lvs->uuid, uuid) == 0) { + if (spdk_uuid_compare(&lvs->uuid, uuid) == 0) { return lvs; } lvs_bdev = vbdev_lvol_store_next(lvs_bdev); @@ -392,13 +393,13 @@ _vbdev_get_lvol_store_by_uuid(uuid_t uuid) struct spdk_lvol_store * vbdev_get_lvol_store_by_uuid(const char *uuid_str) { - uuid_t uuid; + struct spdk_uuid uuid; - if (uuid_parse(uuid_str, uuid)) { + if (spdk_uuid_parse(&uuid, uuid_str)) { return NULL; } - return _vbdev_get_lvol_store_by_uuid(uuid); + return _vbdev_get_lvol_store_by_uuid(&uuid); } struct spdk_lvol_store * @@ -524,7 +525,7 @@ vbdev_lvol_dump_info_json(void *ctx, struct spdk_json_write_ctx *w) struct spdk_lvol *lvol = ctx; struct lvol_store_bdev *lvs_bdev; struct spdk_bdev *bdev; - char lvol_store_uuid[UUID_STRING_LEN]; + char lvol_store_uuid[SPDK_UUID_STRING_LEN]; spdk_json_write_name(w, "lvol"); spdk_json_write_object_begin(w); @@ -532,7 +533,7 @@ vbdev_lvol_dump_info_json(void *ctx, struct spdk_json_write_ctx *w) lvs_bdev = vbdev_get_lvs_bdev_by_lvs(lvol->lvol_store); bdev = lvs_bdev->bdev; - uuid_unparse(lvol->lvol_store->uuid, lvol_store_uuid); + spdk_uuid_fmt_lower(lvol_store_uuid, sizeof(lvol_store_uuid), &lvol->lvol_store->uuid); spdk_json_write_name(w, "lvol_store_uuid"); spdk_json_write_string(w, lvol_store_uuid); diff --git a/lib/bdev/lvol/vbdev_lvol_rpc.c b/lib/bdev/lvol/vbdev_lvol_rpc.c index df3ed08f3..9f6866ae2 100644 --- a/lib/bdev/lvol/vbdev_lvol_rpc.c +++ b/lib/bdev/lvol/vbdev_lvol_rpc.c @@ -93,14 +93,14 @@ static void _spdk_rpc_lvol_store_construct_cb(void *cb_arg, struct spdk_lvol_store *lvol_store, int lvserrno) { struct spdk_json_write_ctx *w; - char lvol_store_uuid[UUID_STRING_LEN]; + char lvol_store_uuid[SPDK_UUID_STRING_LEN]; struct spdk_jsonrpc_request *request = cb_arg; if (lvserrno != 0) { goto invalid; } - uuid_unparse(lvol_store->uuid, lvol_store_uuid); + spdk_uuid_fmt_lower(lvol_store_uuid, sizeof(lvol_store_uuid), &lvol_store->uuid); w = spdk_jsonrpc_begin_result(request); if (w == NULL) { @@ -530,7 +530,7 @@ spdk_rpc_dump_lvol_store_info(struct spdk_json_write_ctx *w, struct lvol_store_b { struct spdk_blob_store *bs; uint64_t cluster_size, block_size; - char uuid[UUID_STRING_LEN]; + char uuid[SPDK_UUID_STRING_LEN]; bs = lvs_bdev->lvs->blobstore; cluster_size = spdk_bs_get_cluster_size(bs); @@ -539,7 +539,7 @@ spdk_rpc_dump_lvol_store_info(struct spdk_json_write_ctx *w, struct lvol_store_b spdk_json_write_object_begin(w); - uuid_unparse(lvs_bdev->lvs->uuid, uuid); + spdk_uuid_fmt_lower(uuid, sizeof(uuid), &lvs_bdev->lvs->uuid); spdk_json_write_name(w, "uuid"); spdk_json_write_string(w, uuid); diff --git a/lib/lvol/lvol.c b/lib/lvol/lvol.c index d7bd10a15..1a698a840 100644 --- a/lib/lvol/lvol.c +++ b/lib/lvol/lvol.c @@ -37,9 +37,6 @@ #include "spdk/io_channel.h" #include "spdk/blob_bdev.h" -/* Length of string returned from uuid_unparse() */ -#define UUID_STRING_LEN 37 - /* Default blob channel opts for lvol */ #define SPDK_LVOL_BLOB_OPTS_CHANNEL_OPS 512 @@ -160,7 +157,7 @@ _spdk_load_next_lvol(void *cb_arg, struct spdk_blob *blob, int lvolerrno) struct spdk_blob_store *bs = lvs->blobstore; struct spdk_lvol *lvol, *tmp; spdk_blob_id blob_id; - char uuid[UUID_STRING_LEN]; + char uuid[SPDK_UUID_STRING_LEN]; const char *attr; size_t value_len; int rc; @@ -196,7 +193,7 @@ _spdk_load_next_lvol(void *cb_arg, struct spdk_blob *blob, int lvolerrno) lvol->lvol_store = lvs; lvol->num_clusters = spdk_blob_get_num_clusters(blob); lvol->close_only = false; - uuid_unparse(lvol->lvol_store->uuid, uuid); + spdk_uuid_fmt_lower(uuid, sizeof(uuid), &lvol->lvol_store->uuid); lvol->unique_id = spdk_sprintf_alloc("%s_%"PRIu64, uuid, (uint64_t)blob_id); if (!lvol->unique_id) { SPDK_ERRLOG("Cannot assign lvol name\n"); @@ -287,14 +284,14 @@ _spdk_lvs_read_uuid(void *cb_arg, struct spdk_blob *blob, int lvolerrno) } rc = spdk_blob_get_xattr_value(blob, "uuid", (const void **)&attr, &value_len); - if (rc != 0 || value_len != UUID_STRING_LEN || attr[UUID_STRING_LEN - 1] != '\0') { + if (rc != 0 || value_len != SPDK_UUID_STRING_LEN || attr[SPDK_UUID_STRING_LEN - 1] != '\0') { SPDK_INFOLOG(SPDK_LOG_LVOL, "missing or incorrect UUID\n"); req->lvserrno = -EINVAL; spdk_blob_close(blob, _spdk_close_super_blob_with_error_cb, req); return; } - if (uuid_parse(attr, lvs->uuid)) { + if (spdk_uuid_parse(&lvs->uuid, attr)) { SPDK_INFOLOG(SPDK_LOG_LVOL, "incorrect UUID '%s'\n", attr); req->lvserrno = -EINVAL; spdk_blob_close(blob, _spdk_close_super_blob_with_error_cb, req); @@ -450,7 +447,7 @@ _spdk_super_blob_init_cb(void *cb_arg, int lvolerrno) struct spdk_lvs_with_handle_req *req = cb_arg; struct spdk_lvol_store *lvs = req->lvol_store; struct spdk_blob *blob = lvs->super_blob; - char uuid[UUID_STRING_LEN]; + char uuid[SPDK_UUID_STRING_LEN]; if (lvolerrno < 0) { req->cb_fn(req->cb_arg, NULL, lvolerrno); @@ -460,9 +457,9 @@ _spdk_super_blob_init_cb(void *cb_arg, int lvolerrno) return; } - uuid_unparse(lvs->uuid, uuid); + spdk_uuid_fmt_lower(uuid, sizeof(uuid), &lvs->uuid); - spdk_blob_set_xattr(blob, "uuid", uuid, UUID_STRING_LEN); + spdk_blob_set_xattr(blob, "uuid", uuid, sizeof(uuid)); spdk_blob_set_xattr(blob, "name", lvs->name, strnlen(lvs->name, SPDK_LVS_NAME_MAX) + 1); spdk_blob_sync_md(blob, _spdk_super_blob_set_cb, req); } @@ -584,7 +581,7 @@ spdk_lvs_init(struct spdk_bs_dev *bs_dev, struct spdk_lvs_opts *o, return -ENOMEM; } - uuid_generate(lvs->uuid); + spdk_uuid_generate(&lvs->uuid); strncpy(lvs->name, o->name, SPDK_LVS_NAME_MAX); rc = _spdk_add_lvs_to_list(lvs); @@ -850,7 +847,7 @@ _spdk_lvol_create_open_cb(void *cb_arg, struct spdk_blob *blob, int lvolerrno) struct spdk_lvol_with_handle_req *req = cb_arg; spdk_blob_id blob_id = spdk_blob_get_id(blob); struct spdk_lvol *lvol = req->lvol; - char uuid[UUID_STRING_LEN]; + char uuid[SPDK_UUID_STRING_LEN]; if (lvolerrno < 0) { free(lvol); @@ -864,7 +861,7 @@ _spdk_lvol_create_open_cb(void *cb_arg, struct spdk_blob *blob, int lvolerrno) TAILQ_INSERT_TAIL(&lvol->lvol_store->lvols, lvol, link); - uuid_unparse(lvol->lvol_store->uuid, uuid); + spdk_uuid_fmt_lower(uuid, sizeof(uuid), &lvol->lvol_store->uuid); lvol->unique_id = spdk_sprintf_alloc("%s_%"PRIu64, uuid, (uint64_t)blob_id); if (!lvol->unique_id) { SPDK_ERRLOG("Cannot alloc memory for lvol name\n"); diff --git a/lib/nvme/nvme.c b/lib/nvme/nvme.c index 535cbf2cd..d6ce184ee 100644 --- a/lib/nvme/nvme.c +++ b/lib/nvme/nvme.c @@ -34,8 +34,6 @@ #include "spdk/nvmf_spec.h" #include "nvme_internal.h" -#include - #define SPDK_NVME_DRIVER_NAME "spdk_nvme_driver" struct nvme_driver *g_spdk_nvme_driver; @@ -254,7 +252,6 @@ nvme_robust_mutex_init_shared(pthread_mutex_t *mtx) static int nvme_driver_init(void) { - uuid_t host_id; int ret = 0; /* Any socket ID */ int socket_id = -1; @@ -327,10 +324,7 @@ nvme_driver_init(void) TAILQ_INIT(&g_spdk_nvme_driver->shared_attached_ctrlrs); - SPDK_STATIC_ASSERT(sizeof(host_id) == sizeof(g_spdk_nvme_driver->default_extended_host_id), - "host ID size mismatch"); - uuid_generate(host_id); - memcpy(g_spdk_nvme_driver->default_extended_host_id, host_id, sizeof(host_id)); + spdk_uuid_generate(&g_spdk_nvme_driver->default_extended_host_id); nvme_robust_mutex_unlock(&g_spdk_nvme_driver->lock); diff --git a/lib/nvme/nvme_ctrlr.c b/lib/nvme/nvme_ctrlr.c index 4d66c18b8..5a4ea1a67 100644 --- a/lib/nvme/nvme_ctrlr.c +++ b/lib/nvme/nvme_ctrlr.c @@ -38,8 +38,6 @@ #include "spdk/env.h" #include "spdk/string.h" -#include - static int nvme_ctrlr_construct_and_submit_aer(struct spdk_nvme_ctrlr *ctrlr, struct nvme_async_event_request *aer); @@ -81,7 +79,7 @@ nvme_ctrlr_set_cc(struct spdk_nvme_ctrlr *ctrlr, const union spdk_nvme_cc_regist void spdk_nvme_ctrlr_get_default_ctrlr_opts(struct spdk_nvme_ctrlr_opts *opts, size_t opts_size) { - char host_id_str[37]; + char host_id_str[SPDK_UUID_STRING_LEN]; assert(opts); @@ -119,12 +117,13 @@ spdk_nvme_ctrlr_get_default_ctrlr_opts(struct spdk_nvme_ctrlr_opts *opts, size_t } if (FIELD_OK(extended_host_id)) { - memcpy(opts->extended_host_id, g_spdk_nvme_driver->default_extended_host_id, + memcpy(opts->extended_host_id, &g_spdk_nvme_driver->default_extended_host_id, sizeof(opts->extended_host_id)); } if (FIELD_OK(hostnqn)) { - uuid_unparse(g_spdk_nvme_driver->default_extended_host_id, host_id_str); + spdk_uuid_fmt_lower(host_id_str, sizeof(host_id_str), + &g_spdk_nvme_driver->default_extended_host_id); snprintf(opts->hostnqn, sizeof(opts->hostnqn), "2014-08.org.nvmexpress:uuid:%s", host_id_str); } diff --git a/lib/nvme/nvme_internal.h b/lib/nvme/nvme_internal.h index 613b368b2..4d2430205 100644 --- a/lib/nvme/nvme_internal.h +++ b/lib/nvme/nvme_internal.h @@ -50,6 +50,7 @@ #include "spdk/util.h" #include "spdk/nvme_intel.h" #include "spdk/nvmf_spec.h" +#include "spdk/uuid.h" #include "spdk_internal/assert.h" #include "spdk_internal/log.h" @@ -475,7 +476,7 @@ struct nvme_driver { TAILQ_HEAD(, spdk_nvme_ctrlr) shared_attached_ctrlrs; bool initialized; - uint8_t default_extended_host_id[16]; + struct spdk_uuid default_extended_host_id; }; extern struct nvme_driver *g_spdk_nvme_driver; diff --git a/lib/nvmf/subsystem.c b/lib/nvmf/subsystem.c index 4a1ebecb5..d1bc92ee4 100644 --- a/lib/nvmf/subsystem.c +++ b/lib/nvmf/subsystem.c @@ -41,13 +41,12 @@ #include "spdk/string.h" #include "spdk/trace.h" #include "spdk/nvmf_spec.h" +#include "spdk/uuid.h" #include "spdk_internal/bdev.h" #include "spdk_internal/log.h" #include "spdk_internal/utf.h" -#include - /* * States for parsing valid domains in NQNs according to RFC 1034 */ @@ -66,7 +65,7 @@ static bool spdk_nvmf_valid_nqn(const char *nqn) { size_t len; - uuid_t uuid_value; + struct spdk_uuid uuid_value; uint i; int bytes_consumed; uint domain_label_length; @@ -99,7 +98,7 @@ spdk_nvmf_valid_nqn(const char *nqn) return false; } - if (uuid_parse(&nqn[SPDK_NVMF_NQN_UUID_PRE_LEN], uuid_value) == -1) { + if (spdk_uuid_parse(&uuid_value, &nqn[SPDK_NVMF_NQN_UUID_PRE_LEN])) { SPDK_ERRLOG("Invalid NQN \"%s\": uuid is not formatted correctly\n", nqn); return false; } diff --git a/lib/util/Makefile b/lib/util/Makefile index 8bbc57506..4e2773a4f 100644 --- a/lib/util/Makefile +++ b/lib/util/Makefile @@ -34,7 +34,7 @@ SPDK_ROOT_DIR := $(abspath $(CURDIR)/../..) include $(SPDK_ROOT_DIR)/mk/spdk.common.mk -C_SRCS = bit_array.c cpuset.c crc16.c crc32.c crc32c.c crc32_ieee.c fd.c io_channel.c strerror_tls.c string.c +C_SRCS = bit_array.c cpuset.c crc16.c crc32.c crc32c.c crc32_ieee.c fd.c io_channel.c strerror_tls.c string.c uuid.c LIBNAME = util include $(SPDK_ROOT_DIR)/mk/spdk.lib.mk diff --git a/lib/util/uuid.c b/lib/util/uuid.c new file mode 100644 index 000000000..445b08594 --- /dev/null +++ b/lib/util/uuid.c @@ -0,0 +1,68 @@ +/*- + * BSD LICENSE + * + * Copyright (C) 2008-2012 Daisuke Aoyama . + * Copyright (c) Intel Corporation. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "spdk/uuid.h" + +#include + +SPDK_STATIC_ASSERT(sizeof(struct spdk_uuid) == sizeof(uuid_t), "Size mismatch"); + +int +spdk_uuid_parse(struct spdk_uuid *uuid, const char *uuid_str) +{ + return uuid_parse(uuid_str, (void *)uuid) == 0 ? 0 : -EINVAL; +} + +int +spdk_uuid_fmt_lower(char *uuid_str, size_t uuid_str_size, const struct spdk_uuid *uuid) +{ + if (uuid_str_size < SPDK_UUID_STRING_LEN) { + return -EINVAL; + } + + uuid_unparse_lower((void *)uuid, uuid_str); + return 0; +} + +int +spdk_uuid_compare(const struct spdk_uuid *u1, const struct spdk_uuid *u2) +{ + return uuid_compare((void *)u1, (void *)u2); +} + +void +spdk_uuid_generate(struct spdk_uuid *uuid) +{ + uuid_generate((void *)uuid); +} diff --git a/test/unit/lib/bdev/vbdev_lvol.c/vbdev_lvol_ut.c b/test/unit/lib/bdev/vbdev_lvol.c/vbdev_lvol_ut.c index 600eb9a97..432b6875f 100644 --- a/test/unit/lib/bdev/vbdev_lvol.c/vbdev_lvol_ut.c +++ b/test/unit/lib/bdev/vbdev_lvol.c/vbdev_lvol_ut.c @@ -614,7 +614,7 @@ ut_lvs_destroy(void) lvs = g_lvol_store; g_lvol_store = NULL; - uuid_generate(lvs->uuid); + spdk_uuid_generate(&lvs->uuid); /* Suuccessfully create lvol, which should be unloaded with lvs later */ g_lvolerrno = -1; @@ -653,7 +653,7 @@ ut_lvol_init(void) g_lvs_bdev->lvs = g_lvs; g_lvs_bdev->bdev = g_base_bdev; - uuid_generate(g_lvs->uuid); + spdk_uuid_generate(&g_lvs->uuid); TAILQ_INSERT_TAIL(&g_spdk_lvol_pairs, g_lvs_bdev, lvol_stores); @@ -821,7 +821,7 @@ ut_lvol_rename(void) g_lvs_bdev->lvs = g_lvs; g_lvs_bdev->bdev = g_base_bdev; - uuid_generate(g_lvs->uuid); + spdk_uuid_generate(&g_lvs->uuid); TAILQ_INSERT_TAIL(&g_spdk_lvol_pairs, g_lvs_bdev, lvol_stores); @@ -896,7 +896,7 @@ ut_lvol_resize(void) g_lvs_bdev->lvs = g_lvs; g_lvs_bdev->bdev = g_base_bdev; - uuid_generate(g_lvs->uuid); + spdk_uuid_generate(&g_lvs->uuid); g_base_bdev->blocklen = 4096; TAILQ_INSERT_TAIL(&g_spdk_lvol_pairs, g_lvs_bdev, lvol_stores); @@ -964,7 +964,7 @@ ut_lvs_unload(void) lvs = g_lvol_store; g_lvol_store = NULL; - uuid_generate(lvs->uuid); + spdk_uuid_generate(&lvs->uuid); /* Suuccessfully create lvol, which should be destroyed with lvs later */ g_lvolerrno = -1; diff --git a/test/unit/lib/lvol/lvol.c/lvol_ut.c b/test/unit/lib/lvol/lvol.c/lvol_ut.c index fd37019bc..0b5d1e346 100644 --- a/test/unit/lib/lvol/lvol.c/lvol_ut.c +++ b/test/unit/lib/lvol/lvol.c/lvol_ut.c @@ -63,7 +63,7 @@ struct spdk_blob { int open_status; int load_status; TAILQ_ENTRY(spdk_blob) link; - char uuid[UUID_STRING_LEN]; + char uuid[SPDK_UUID_STRING_LEN]; char name[SPDK_LVS_NAME_MAX]; bool thin_provisioned; }; @@ -172,8 +172,8 @@ spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value, uint16_t value_len) { if (!strcmp(name, "uuid")) { - CU_ASSERT(value_len == UUID_STRING_LEN); - memcpy(blob->uuid, value, UUID_STRING_LEN); + CU_ASSERT(value_len == SPDK_UUID_STRING_LEN); + memcpy(blob->uuid, value, SPDK_UUID_STRING_LEN); } else if (!strcmp(name, "name")) { CU_ASSERT(value_len <= SPDK_LVS_NAME_MAX); memcpy(blob->name, value, value_len); @@ -186,10 +186,10 @@ int spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name, const void **value, size_t *value_len) { - if (!strcmp(name, "uuid") && strnlen(blob->uuid, UUID_STRING_LEN) != 0) { - CU_ASSERT(strnlen(blob->uuid, UUID_STRING_LEN) == (UUID_STRING_LEN - 1)); + if (!strcmp(name, "uuid") && strnlen(blob->uuid, SPDK_UUID_STRING_LEN) != 0) { + CU_ASSERT(strnlen(blob->uuid, SPDK_UUID_STRING_LEN) == (SPDK_UUID_STRING_LEN - 1)); *value = blob->uuid; - *value_len = UUID_STRING_LEN; + *value_len = SPDK_UUID_STRING_LEN; return 0; } else if (!strcmp(name, "name") && strnlen(blob->name, SPDK_LVS_NAME_MAX) != 0) { *value = blob->name; @@ -1036,7 +1036,7 @@ lvs_load(void) /* Fail on getting name */ g_lvserrno = 0; - spdk_blob_set_xattr(super_blob, "uuid", uuid, UUID_STRING_LEN); + spdk_blob_set_xattr(super_blob, "uuid", uuid, SPDK_UUID_STRING_LEN); spdk_lvs_load(&dev.bs_dev, lvol_store_op_with_handle_complete, req); CU_ASSERT(g_lvserrno == -EINVAL); CU_ASSERT(g_lvol_store == NULL); @@ -1090,7 +1090,7 @@ lvols_load(void) super_blob = calloc(1, sizeof(*super_blob)); SPDK_CU_ASSERT_FATAL(super_blob != NULL); super_blob->id = 0x100; - spdk_blob_set_xattr(super_blob, "uuid", uuid, UUID_STRING_LEN); + spdk_blob_set_xattr(super_blob, "uuid", uuid, SPDK_UUID_STRING_LEN); spdk_blob_set_xattr(super_blob, "name", "lvs", strnlen("lvs", SPDK_LVS_NAME_MAX) + 1); TAILQ_INSERT_TAIL(&dev.bs->blobs, super_blob, link); dev.bs->super_blobid = 0x100; @@ -1102,23 +1102,23 @@ lvols_load(void) blob1 = calloc(1, sizeof(*blob1)); SPDK_CU_ASSERT_FATAL(blob1 != NULL); blob1->id = 0x1; - spdk_blob_set_xattr(blob1, "uuid", uuid, UUID_STRING_LEN); + spdk_blob_set_xattr(blob1, "uuid", uuid, SPDK_UUID_STRING_LEN); spdk_blob_set_xattr(blob1, "name", "lvol1", strnlen("lvol1", SPDK_LVOL_NAME_MAX) + 1); - blob1->uuid[UUID_STRING_LEN - 2] = '1'; + blob1->uuid[SPDK_UUID_STRING_LEN - 2] = '1'; blob2 = calloc(1, sizeof(*blob2)); SPDK_CU_ASSERT_FATAL(blob2 != NULL); blob2->id = 0x2; - spdk_blob_set_xattr(blob2, "uuid", uuid, UUID_STRING_LEN); + spdk_blob_set_xattr(blob2, "uuid", uuid, SPDK_UUID_STRING_LEN); spdk_blob_set_xattr(blob2, "name", "lvol2", strnlen("lvol2", SPDK_LVOL_NAME_MAX) + 1); - blob2->uuid[UUID_STRING_LEN - 2] = '2'; + blob2->uuid[SPDK_UUID_STRING_LEN - 2] = '2'; blob3 = calloc(1, sizeof(*blob3)); SPDK_CU_ASSERT_FATAL(blob3 != NULL); blob3->id = 0x2; - spdk_blob_set_xattr(blob3, "uuid", uuid, UUID_STRING_LEN); + spdk_blob_set_xattr(blob3, "uuid", uuid, SPDK_UUID_STRING_LEN); spdk_blob_set_xattr(blob3, "name", "lvol3", strnlen("lvol3", SPDK_LVOL_NAME_MAX) + 1); - blob3->uuid[UUID_STRING_LEN - 2] = '3'; + blob3->uuid[SPDK_UUID_STRING_LEN - 2] = '3'; spdk_allocate_thread(_lvol_send_msg, NULL, NULL, NULL, NULL); @@ -1201,7 +1201,7 @@ lvol_open(void) super_blob = calloc(1, sizeof(*super_blob)); SPDK_CU_ASSERT_FATAL(super_blob != NULL); super_blob->id = 0x100; - spdk_blob_set_xattr(super_blob, "uuid", uuid, UUID_STRING_LEN); + spdk_blob_set_xattr(super_blob, "uuid", uuid, SPDK_UUID_STRING_LEN); spdk_blob_set_xattr(super_blob, "name", "lvs", strnlen("lvs", SPDK_LVS_NAME_MAX) + 1); TAILQ_INSERT_TAIL(&dev.bs->blobs, super_blob, link); dev.bs->super_blobid = 0x100; @@ -1213,23 +1213,23 @@ lvol_open(void) blob1 = calloc(1, sizeof(*blob1)); SPDK_CU_ASSERT_FATAL(blob1 != NULL); blob1->id = 0x1; - spdk_blob_set_xattr(blob1, "uuid", uuid, UUID_STRING_LEN); + spdk_blob_set_xattr(blob1, "uuid", uuid, SPDK_UUID_STRING_LEN); spdk_blob_set_xattr(blob1, "name", "lvol1", strnlen("lvol1", SPDK_LVOL_NAME_MAX) + 1); - blob1->uuid[UUID_STRING_LEN - 2] = '1'; + blob1->uuid[SPDK_UUID_STRING_LEN - 2] = '1'; blob2 = calloc(1, sizeof(*blob2)); SPDK_CU_ASSERT_FATAL(blob2 != NULL); blob2->id = 0x2; - spdk_blob_set_xattr(blob2, "uuid", uuid, UUID_STRING_LEN); + spdk_blob_set_xattr(blob2, "uuid", uuid, SPDK_UUID_STRING_LEN); spdk_blob_set_xattr(blob2, "name", "lvol2", strnlen("lvol2", SPDK_LVOL_NAME_MAX) + 1); - blob2->uuid[UUID_STRING_LEN - 2] = '2'; + blob2->uuid[SPDK_UUID_STRING_LEN - 2] = '2'; blob3 = calloc(1, sizeof(*blob3)); SPDK_CU_ASSERT_FATAL(blob3 != NULL); blob3->id = 0x2; - spdk_blob_set_xattr(blob3, "uuid", uuid, UUID_STRING_LEN); + spdk_blob_set_xattr(blob3, "uuid", uuid, SPDK_UUID_STRING_LEN); spdk_blob_set_xattr(blob3, "name", "lvol3", strnlen("lvol3", SPDK_LVOL_NAME_MAX) + 1); - blob3->uuid[UUID_STRING_LEN - 2] = '3'; + blob3->uuid[SPDK_UUID_STRING_LEN - 2] = '3'; spdk_allocate_thread(_lvol_send_msg, NULL, NULL, NULL, NULL); diff --git a/test/unit/lib/nvme/nvme_ctrlr.c/nvme_ctrlr_ut.c b/test/unit/lib/nvme/nvme_ctrlr.c/nvme_ctrlr_ut.c index 094397864..4feb554bc 100644 --- a/test/unit/lib/nvme/nvme_ctrlr.c/nvme_ctrlr_ut.c +++ b/test/unit/lib/nvme/nvme_ctrlr.c/nvme_ctrlr_ut.c @@ -1418,11 +1418,9 @@ static void test_ctrlr_get_default_ctrlr_opts(void) { struct spdk_nvme_ctrlr_opts opts = {}; - uuid_t uuid; - CU_ASSERT(uuid_parse("e53e9258-c93b-48b5-be1a-f025af6d232a", uuid) == 0); - SPDK_CU_ASSERT_FATAL(sizeof(uuid) == sizeof(g_spdk_nvme_driver->default_extended_host_id)); - memcpy(g_spdk_nvme_driver->default_extended_host_id, uuid, sizeof(uuid)); + CU_ASSERT(spdk_uuid_parse(&g_spdk_nvme_driver->default_extended_host_id, + "e53e9258-c93b-48b5-be1a-f025af6d232a") == 0); memset(&opts, 0, sizeof(opts)); @@ -1459,7 +1457,7 @@ test_ctrlr_get_default_ctrlr_opts(void) } CU_ASSERT_STRING_EQUAL(opts.hostnqn, "2014-08.org.nvmexpress:uuid:e53e9258-c93b-48b5-be1a-f025af6d232a"); - CU_ASSERT(memcmp(opts.extended_host_id, g_spdk_nvme_driver->default_extended_host_id, + CU_ASSERT(memcmp(opts.extended_host_id, &g_spdk_nvme_driver->default_extended_host_id, sizeof(opts.extended_host_id)) == 0); CU_ASSERT(strlen(opts.src_addr) == 0); CU_ASSERT(strlen(opts.src_svcid) == 0);