From 3df91224ea0beb44c0f87b434a5df2b594033367 Mon Sep 17 00:00:00 2001 From: Martin Belanger Date: Tue, 21 Apr 2026 10:40:47 -0400 Subject: [PATCH] libnvme: move fabrics-only helpers to util-fabrics.c libnvmf_exat_ptr_next(), libnvmf_getifaddrs() (renamed from libnvme_getifaddrs()), and the libnvmf_exat_len()/libnvmf_exat_size() static inlines (renamed from nvmf_exat_len()/nvmf_exat_size()) are only meaningful in fabrics-capable builds. Moving them out of util.c and private.h into util-fabrics.c and private-fabrics.h keeps the fabrics footprint contained and ensures PCIe-only/embedded builds do not pull in this code. util-fabrics.c is compiled only when want_fabrics is set, which implies CONFIG_FABRICS is always defined, so no guard is needed around libnvmf_getifaddrs() in that file. While at it, simplify the util.c include guard from: #if defined(HAVE_NETDB) || defined(CONFIG_FABRICS) to: #ifdef HAVE_NETDB since CONFIG_FABRICS was only needed for libnvme_getifaddrs(), which is now in util-fabrics.c. Signed-off-by: Martin Belanger Assisted-by: Claude Sonnet 4.6 --- libnvme/src/meson.build | 1 + libnvme/src/nvme/fabrics.c | 8 ++--- libnvme/src/nvme/private-fabrics.h | 52 ++++++++++++++++++++++++++++++ libnvme/src/nvme/private.h | 50 ++-------------------------- libnvme/src/nvme/tree.c | 2 +- libnvme/src/nvme/util-fabrics.c | 34 +++++++++++++++++++ libnvme/src/nvme/util.c | 24 ++------------ 7 files changed, 97 insertions(+), 74 deletions(-) create mode 100644 libnvme/src/nvme/util-fabrics.c diff --git a/libnvme/src/meson.build b/libnvme/src/meson.build index 3bc24a3dd1..2099222c1c 100644 --- a/libnvme/src/meson.build +++ b/libnvme/src/meson.build @@ -44,6 +44,7 @@ if want_fabrics 'nvme/accessors-fabrics.c', 'nvme/fabrics.c', 'nvme/nbft.c', + 'nvme/util-fabrics.c', ] headers += [ 'nvme/accessors-fabrics.h', diff --git a/libnvme/src/nvme/fabrics.c b/libnvme/src/nvme/fabrics.c index 5d91a5b216..b0db83fe4c 100644 --- a/libnvme/src/nvme/fabrics.c +++ b/libnvme/src/nvme/fabrics.c @@ -1542,12 +1542,12 @@ static __u32 nvmf_get_tel(const char *hostsymname) __u16 len; /* Host ID is mandatory */ - tel += nvmf_exat_size(NVME_UUID_LEN); + tel += libnvmf_exat_size(NVME_UUID_LEN); /* Symbolic name is optional */ len = hostsymname ? strlen(hostsymname) : 0; if (len) - tel += nvmf_exat_size(len); + tel += libnvmf_exat_size(len); return tel; } @@ -1586,13 +1586,13 @@ static void nvmf_fill_die(struct nvmf_ext_die *die, struct libnvme_host *h, numexat++; exat = die->exat; exat->exattype = cpu_to_le16(NVMF_EXATTYPE_HOSTID); - exat->exatlen = cpu_to_le16(nvmf_exat_len(NVME_UUID_LEN)); + exat->exatlen = cpu_to_le16(libnvmf_exat_len(NVME_UUID_LEN)); libnvme_uuid_from_string(h->hostid, exat->exatval); /* Extended Attribute for the Symbolic Name (optional) */ symname_len = h->hostsymname ? strlen(h->hostsymname) : 0; if (symname_len) { - __u16 exatlen = nvmf_exat_len(symname_len); + __u16 exatlen = libnvmf_exat_len(symname_len); numexat++; exat = libnvmf_exat_ptr_next(exat); diff --git a/libnvme/src/nvme/private-fabrics.h b/libnvme/src/nvme/private-fabrics.h index 74646d3aa3..b2cfc4628c 100644 --- a/libnvme/src/nvme/private-fabrics.h +++ b/libnvme/src/nvme/private-fabrics.h @@ -7,6 +7,10 @@ */ #pragma once +#if defined(HAVE_NETDB) || defined(CONFIG_FABRICS) +#include +#endif + #include #include @@ -106,6 +110,54 @@ struct libnvmf_uri { // !generate-accessors char *fragment; }; +/** + * libnvmf_exat_len() - Return length rounded up by 4 + * @val_len: Value length + * + * Return the size in bytes, rounded to a multiple of 4 (e.g., size of + * __u32), of the buffer needed to hold the exat value of size + * @val_len. + * + * Return: Length rounded up by 4 + */ +static inline __u16 libnvmf_exat_len(size_t val_len) +{ + return (__u16)round_up(val_len, sizeof(__u32)); +} + +/** + * libnvmf_exat_size - Return min aligned size to hold value + * @val_len: This is the length of the data to be copied to the "exatval" + * field of a "struct nvmf_ext_attr". + * + * Return the size of the "struct nvmf_ext_attr" needed to hold + * a value of size @val_len. + * + * Return: The size in bytes, rounded to a multiple of 4 (i.e. size of + * __u32), of the "struct nvmf_ext_attr" required to hold a string of + * length @val_len. + */ +static inline __u16 libnvmf_exat_size(size_t val_len) +{ + return (__u16)(sizeof(struct nvmf_ext_attr) + libnvmf_exat_len(val_len)); +} + +#if defined(HAVE_NETDB) || defined(CONFIG_FABRICS) +/** + * libnvmf_getifaddrs - Cached wrapper around getifaddrs() + * @ctx: pointer to the global context + * + * On the first call, this function invokes the POSIX getifaddrs() + * and caches the result in the global context. Subsequent calls + * return the cached data. The caller must NOT call freeifaddrs() + * on the returned data. The cache will be freed when the global + * context is freed. + * + * Return: Pointer to I/F data, NULL on error (with errno set). + */ +const struct ifaddrs *libnvmf_getifaddrs(struct libnvme_global_ctx *ctx); +#endif /* HAVE_NETDB || CONFIG_FABRICS */ + bool traddr_is_hostname(struct libnvme_global_ctx *ctx, const char *transport, const char *traddr); diff --git a/libnvme/src/nvme/private.h b/libnvme/src/nvme/private.h index 8f9e72382c..a24f5e3ddb 100644 --- a/libnvme/src/nvme/private.h +++ b/libnvme/src/nvme/private.h @@ -337,7 +337,7 @@ struct libnvme_global_ctx { bool dry_run; #ifdef CONFIG_FABRICS struct libnvme_fabric_options *options; - struct ifaddrs *ifaddrs_cache; /* init with libnvme_getifaddrs() */ + struct ifaddrs *ifaddrs_cache; /* init with libnvmf_getifaddrs() */ #endif enum libnvme_io_uring_state uring_state; @@ -417,20 +417,6 @@ static inline char *xstrdup(const char *s) return strdup(s); } -/** - * libnvme_getifaddrs - Cached wrapper around getifaddrs() - * @ctx: pointer to the global context - * - * On the first call, this function invokes the POSIX getifaddrs() - * and caches the result in the global context. Subsequent calls - * return the cached data. The caller must NOT call freeifaddrs() - * on the returned data. The cache will be freed when the global - * context is freed. - * - * Return: Pointer to I/F data, NULL on error (with errno set). - */ -const struct ifaddrs *libnvme_getifaddrs(struct libnvme_global_ctx *ctx); - /** * libnvme_ipaddrs_eq - Check if 2 IP addresses are equal. * @addr1: IP address (can be IPv4 or IPv6) @@ -440,6 +426,7 @@ const struct ifaddrs *libnvme_getifaddrs(struct libnvme_global_ctx *ctx); */ bool libnvme_ipaddrs_eq(const char *addr1, const char *addr2); +#if defined(HAVE_NETDB) || defined(CONFIG_FABRICS) /** * libnvme_iface_matching_addr - Get interface matching @addr * @iface_list: Interface list returned by getifaddrs() @@ -469,6 +456,7 @@ const char *libnvme_iface_matching_addr(const struct ifaddrs *iface_list, */ bool libnvme_iface_primary_addr_matches(const struct ifaddrs *iface_list, const char *iface, const char *addr); +#endif /* HAVE_NETDB || CONFIG_FABRICS */ int hostname2traddr(struct libnvme_global_ctx *ctx, const char *traddr, char **hostname); @@ -552,38 +540,6 @@ char *kv_keymatch(const char *kv, const char *key); */ #define round_up(val, mult) ((((val)-1) | __round_mask((val), (mult)))+1) -/** - * nvmf_exat_len() - Return length rounded up by 4 - * @val_len: Value length - * - * Return the size in bytes, rounded to a multiple of 4 (e.g., size of - * __u32), of the buffer needed to hold the exat value of size - * @val_len. - * - * Return: Length rounded up by 4 - */ -static inline __u16 nvmf_exat_len(size_t val_len) -{ - return (__u16)round_up(val_len, sizeof(__u32)); -} - -/** - * nvmf_exat_size - Return min aligned size to hold value - * @val_len: This is the length of the data to be copied to the "exatval" - * field of a "struct nvmf_ext_attr". - * - * Return the size of the "struct nvmf_ext_attr" needed to hold - * a value of size @val_len. - * - * Return: The size in bytes, rounded to a multiple of 4 (i.e. size of - * __u32), of the "struct nvmf_ext_attr" required to hold a string of - * length @val_len. - */ -static inline __u16 nvmf_exat_size(size_t val_len) -{ - return (__u16)(sizeof(struct nvmf_ext_attr) + nvmf_exat_len(val_len)); -} - /** * libnvme_ns_get_transport_handle() - Get associated transport handle * @n: Namespace instance diff --git a/libnvme/src/nvme/tree.c b/libnvme/src/nvme/tree.c index b9d8cf39bf..94b4c1e837 100644 --- a/libnvme/src/nvme/tree.c +++ b/libnvme/src/nvme/tree.c @@ -1429,7 +1429,7 @@ static ctrl_match_t _candidate_init(struct libnvme_global_ctx *ctx, #ifdef CONFIG_FABRICS if (streq0(fctx->transport, "tcp")) { - candidate->iface_list = libnvme_getifaddrs(ctx); /* TCP only */ + candidate->iface_list = libnvmf_getifaddrs(ctx); /* TCP only */ candidate->addreq = libnvme_ipaddrs_eq; return _tcp_match_ctrl; } diff --git a/libnvme/src/nvme/util-fabrics.c b/libnvme/src/nvme/util-fabrics.c new file mode 100644 index 0000000000..8cfdcf8b23 --- /dev/null +++ b/libnvme/src/nvme/util-fabrics.c @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later +/* + * This file is part of libnvme. + * Copyright (c) 2026, Dell Technologies Inc. or its subsidiaries. + * + * Authors: Martin Belanger + */ + +#include + +#include + +#include "compiler-attributes.h" +#include "private-fabrics.h" +#include "util.h" + +__public struct nvmf_ext_attr *libnvmf_exat_ptr_next(struct nvmf_ext_attr *p) +{ + __u16 size = libnvmf_exat_size(le16_to_cpu(p->exatlen)); + + return (struct nvmf_ext_attr *)((uintptr_t)p + (ptrdiff_t)size); +} + +const struct ifaddrs *libnvmf_getifaddrs(struct libnvme_global_ctx *ctx) +{ + if (!ctx->ifaddrs_cache) { + struct ifaddrs *p; + + if (!getifaddrs(&p)) + ctx->ifaddrs_cache = p; + } + + return ctx->ifaddrs_cache; +} diff --git a/libnvme/src/nvme/util.c b/libnvme/src/nvme/util.c index c037e30859..da4449ceb4 100644 --- a/libnvme/src/nvme/util.c +++ b/libnvme/src/nvme/util.c @@ -716,12 +716,6 @@ size_t get_entity_version(char *buffer, size_t bufsz) return num_bytes; } -__public struct nvmf_ext_attr *libnvmf_exat_ptr_next(struct nvmf_ext_attr *p) -{ - return (struct nvmf_ext_attr *) - ((uintptr_t)p + (ptrdiff_t)nvmf_exat_size(le16_to_cpu(p->exatlen))); -} - __public const char *libnvme_get_version(enum libnvme_version type) { switch(type) { @@ -938,7 +932,7 @@ bool libnvme_iface_primary_addr_matches(const struct ifaddrs *iface_list, return match_found; } -#else /* HAVE_NETDB */ +#elif defined(CONFIG_FABRICS) const char *libnvme_iface_matching_addr(const struct ifaddrs *iface_list, const char *addr) @@ -958,7 +952,7 @@ bool libnvme_iface_primary_addr_matches(const struct ifaddrs *iface_list, return false; } -#endif /* HAVE_NETDB */ +#endif /* HAVE_NETDB || CONFIG_FABRICS */ void *__libnvme_alloc(size_t len) { @@ -986,20 +980,6 @@ void *__libnvme_realloc(void *p, size_t len) return result; } -#ifdef CONFIG_FABRICS -const struct ifaddrs *libnvme_getifaddrs(struct libnvme_global_ctx *ctx) -{ - if (!ctx->ifaddrs_cache) { - struct ifaddrs *p; - - if (!getifaddrs(&p)) - ctx->ifaddrs_cache = p; - } - - return ctx->ifaddrs_cache; -} -#endif - /* This used instead of basename() due to behavioral differences between * the POSIX and the GNU version. This is the glibc implementation. * Original source: https://github.com/bminor/glibc/blob/master/string/basename.c */