From 19572e5b20c17304937265b54f2394c6cda6b056 Mon Sep 17 00:00:00 2001 From: Artur Paszkiewicz Date: Thu, 26 Jan 2023 14:04:08 +0100 Subject: [PATCH] util/uuid: use libc uuid functions on freebsd Remove libuuid usage on FreeBSD and add dedicated implementation of spdk_uuid API using functions from the standard library. Fixes: #2878 Change-Id: Ie49ccb2842acad6064bffd789e4f64b7365b6e5c Signed-off-by: Artur Paszkiewicz Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/16558 Reviewed-by: Shuhei Matsumoto Tested-by: SPDK CI Jenkins Reviewed-by: Jim Harris Reviewed-by: Tomasz Zawadzki --- lib/util/Makefile | 3 + lib/util/uuid.c | 60 +++++++++++++++++++ mk/spdk.common.mk | 2 + scripts/pkgdep/freebsd.sh | 6 +- test/unit/lib/nvmf/subsystem.c/subsystem_ut.c | 9 ++- 5 files changed, 71 insertions(+), 9 deletions(-) diff --git a/lib/util/Makefile b/lib/util/Makefile index 1c63ee922..d05ff457b 100644 --- a/lib/util/Makefile +++ b/lib/util/Makefile @@ -13,7 +13,10 @@ C_SRCS = base64.c bit_array.c cpuset.c crc16.c crc32.c crc32c.c crc32_ieee.c \ dif.c fd.c file.c hexlify.c iov.c math.c pipe.c strerror_tls.c string.c uuid.c \ fd_group.c xor.c zipf.c LIBNAME = util + +ifneq ($(OS),FreeBSD) LOCAL_SYS_LIBS = -luuid +endif ifeq ($(CONFIG_ISAL), y) LOCAL_SYS_LIBS += -L$(ISAL_DIR)/.libs -lisal diff --git a/lib/util/uuid.c b/lib/util/uuid.c index 03085ed46..5bf6a7e59 100644 --- a/lib/util/uuid.c +++ b/lib/util/uuid.c @@ -5,6 +5,8 @@ #include "spdk/uuid.h" +#ifndef __FreeBSD__ + #include SPDK_STATIC_ASSERT(sizeof(struct spdk_uuid) == sizeof(uuid_t), "Size mismatch"); @@ -43,3 +45,61 @@ spdk_uuid_copy(struct spdk_uuid *dst, const struct spdk_uuid *src) { uuid_copy((void *)dst, (void *)src); } + +#else + +#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) +{ + uint32_t status; + + uuid_from_string(uuid_str, (uuid_t *)uuid, &status); + + return status == 0 ? 0 : -EINVAL; +} + +int +spdk_uuid_fmt_lower(char *uuid_str, size_t uuid_str_size, const struct spdk_uuid *uuid) +{ + uint32_t status; + char *str; + + if (uuid_str_size < SPDK_UUID_STRING_LEN) { + return -EINVAL; + } + + uuid_to_string((const uuid_t *)uuid, &str, &status); + + if (status == uuid_s_no_memory) { + return -ENOMEM; + } + + snprintf(uuid_str, uuid_str_size, "%s", str); + free(str); + + return 0; +} + +int +spdk_uuid_compare(const struct spdk_uuid *u1, const struct spdk_uuid *u2) +{ + return uuid_compare((const uuid_t *)u1, (const uuid_t *)u2, NULL); +} + +void +spdk_uuid_generate(struct spdk_uuid *uuid) +{ + uuid_create((uuid_t *)uuid, NULL); +} + +void +spdk_uuid_copy(struct spdk_uuid *dst, const struct spdk_uuid *src) +{ + memcpy(dst, src, sizeof(*dst)); +} + +#endif diff --git a/mk/spdk.common.mk b/mk/spdk.common.mk index 42231b8e2..025441dc3 100644 --- a/mk/spdk.common.mk +++ b/mk/spdk.common.mk @@ -338,7 +338,9 @@ CFLAGS += $(COMMON_CFLAGS) -Wno-pointer-sign -Wstrict-prototypes -Wold-style-d CXXFLAGS += $(COMMON_CFLAGS) -std=c++11 SYS_LIBS += -lrt +ifneq ($(OS),FreeBSD) SYS_LIBS += -luuid +endif SYS_LIBS += -lssl SYS_LIBS += -lcrypto SYS_LIBS += -lm diff --git a/scripts/pkgdep/freebsd.sh b/scripts/pkgdep/freebsd.sh index 902f5d0e1..f7e1ec1b9 100755 --- a/scripts/pkgdep/freebsd.sh +++ b/scripts/pkgdep/freebsd.sh @@ -4,15 +4,13 @@ # All rights reserved. # # Minimal install -pkg install -y gmake cunit openssl git bash misc/e2fsprogs-libuuid python \ - ncurses ninja meson +pkg install -y gmake cunit openssl git bash python ncurses ninja meson pkg install -g -y "py*-pyelftools-*" "py*-pandas" # Additional dependencies for ISA-L used in compression pkg install -y autoconf automake libtool help2man if [[ $INSTALL_DEV_TOOLS == "true" ]]; then # Tools for developers - pkg install -y devel/astyle bash \ - misc/e2fsprogs-libuuid sysutils/sg3_utils nasm \ + pkg install -y devel/astyle bash sysutils/sg3_utils nasm \ bash-completion ruby devel/ruby-gems pkg install -g -y "py*-pycodestyle" # ruby and ruby-gems are not preinstalled on FreeBSD but are needed to diff --git a/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c b/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c index 3e04fb58d..b3ff0a170 100644 --- a/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c +++ b/test/unit/lib/nvmf/subsystem.c/subsystem_ut.c @@ -11,7 +11,6 @@ #include "spdk/bdev_module.h" #include "nvmf/subsystem.c" -#include SPDK_LOG_REGISTER_COMPONENT(nvmf) @@ -1593,7 +1592,7 @@ test_nvmf_valid_nqn(void) struct spdk_uuid s_uuid = {}; spdk_uuid_generate(&s_uuid); - uuid_unparse((void *)&s_uuid, uuid); + spdk_uuid_fmt_lower(uuid, sizeof(uuid), &s_uuid); /* discovery nqn */ snprintf(nqn, sizeof(nqn), "%s", SPDK_NVMF_DISCOVERY_NQN); @@ -1659,13 +1658,13 @@ test_nvmf_ns_reservation_restore(void) /* Generate and prepare uuids, make sure bdev and info uuid are the same */ spdk_uuid_generate(&s_uuid); - uuid_unparse((void *)&s_uuid, uuid); + spdk_uuid_fmt_lower(uuid, sizeof(uuid), &s_uuid); snprintf(info.holder_uuid, SPDK_UUID_STRING_LEN, "%s", uuid); snprintf(info.bdev_uuid, SPDK_UUID_STRING_LEN, "%s", uuid); snprintf(info.registrants[0].host_uuid, SPDK_UUID_STRING_LEN, "%s", uuid); spdk_uuid_copy(&bdev.uuid, &s_uuid); spdk_uuid_generate(&s_uuid); - uuid_unparse((void *)&s_uuid, uuid); + spdk_uuid_fmt_lower(uuid, sizeof(uuid), &s_uuid); snprintf(info.registrants[1].host_uuid, SPDK_UUID_STRING_LEN, "%s", uuid); /* info->rkey not exist in registrants */ @@ -1695,7 +1694,7 @@ test_nvmf_ns_reservation_restore(void) /* Existing bdev UUID is different with configuration */ spdk_uuid_generate(&s_uuid); - uuid_unparse((void *)&s_uuid, uuid); + spdk_uuid_fmt_lower(uuid, sizeof(uuid), &s_uuid); snprintf(info.bdev_uuid, SPDK_UUID_STRING_LEN, "%s", uuid); spdk_uuid_generate(&s_uuid); spdk_uuid_copy(&bdev.uuid, &s_uuid);