Skip to content

Commit 3df9122

Browse files
author
Martin Belanger
committed
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 <[email protected]> Assisted-by: Claude Sonnet 4.6 <[email protected]>
1 parent 6f3cfb1 commit 3df9122

7 files changed

Lines changed: 97 additions & 74 deletions

File tree

libnvme/src/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ if want_fabrics
4444
'nvme/accessors-fabrics.c',
4545
'nvme/fabrics.c',
4646
'nvme/nbft.c',
47+
'nvme/util-fabrics.c',
4748
]
4849
headers += [
4950
'nvme/accessors-fabrics.h',

libnvme/src/nvme/fabrics.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,12 +1542,12 @@ static __u32 nvmf_get_tel(const char *hostsymname)
15421542
__u16 len;
15431543

15441544
/* Host ID is mandatory */
1545-
tel += nvmf_exat_size(NVME_UUID_LEN);
1545+
tel += libnvmf_exat_size(NVME_UUID_LEN);
15461546

15471547
/* Symbolic name is optional */
15481548
len = hostsymname ? strlen(hostsymname) : 0;
15491549
if (len)
1550-
tel += nvmf_exat_size(len);
1550+
tel += libnvmf_exat_size(len);
15511551

15521552
return tel;
15531553
}
@@ -1586,13 +1586,13 @@ static void nvmf_fill_die(struct nvmf_ext_die *die, struct libnvme_host *h,
15861586
numexat++;
15871587
exat = die->exat;
15881588
exat->exattype = cpu_to_le16(NVMF_EXATTYPE_HOSTID);
1589-
exat->exatlen = cpu_to_le16(nvmf_exat_len(NVME_UUID_LEN));
1589+
exat->exatlen = cpu_to_le16(libnvmf_exat_len(NVME_UUID_LEN));
15901590
libnvme_uuid_from_string(h->hostid, exat->exatval);
15911591

15921592
/* Extended Attribute for the Symbolic Name (optional) */
15931593
symname_len = h->hostsymname ? strlen(h->hostsymname) : 0;
15941594
if (symname_len) {
1595-
__u16 exatlen = nvmf_exat_len(symname_len);
1595+
__u16 exatlen = libnvmf_exat_len(symname_len);
15961596

15971597
numexat++;
15981598
exat = libnvmf_exat_ptr_next(exat);

libnvme/src/nvme/private-fabrics.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
*/
88
#pragma once
99

10+
#if defined(HAVE_NETDB) || defined(CONFIG_FABRICS)
11+
#include <ifaddrs.h>
12+
#endif
13+
1014
#include <nvme/fabrics.h>
1115
#include <nvme/tree.h>
1216

@@ -106,6 +110,54 @@ struct libnvmf_uri { // !generate-accessors
106110
char *fragment;
107111
};
108112

113+
/**
114+
* libnvmf_exat_len() - Return length rounded up by 4
115+
* @val_len: Value length
116+
*
117+
* Return the size in bytes, rounded to a multiple of 4 (e.g., size of
118+
* __u32), of the buffer needed to hold the exat value of size
119+
* @val_len.
120+
*
121+
* Return: Length rounded up by 4
122+
*/
123+
static inline __u16 libnvmf_exat_len(size_t val_len)
124+
{
125+
return (__u16)round_up(val_len, sizeof(__u32));
126+
}
127+
128+
/**
129+
* libnvmf_exat_size - Return min aligned size to hold value
130+
* @val_len: This is the length of the data to be copied to the "exatval"
131+
* field of a "struct nvmf_ext_attr".
132+
*
133+
* Return the size of the "struct nvmf_ext_attr" needed to hold
134+
* a value of size @val_len.
135+
*
136+
* Return: The size in bytes, rounded to a multiple of 4 (i.e. size of
137+
* __u32), of the "struct nvmf_ext_attr" required to hold a string of
138+
* length @val_len.
139+
*/
140+
static inline __u16 libnvmf_exat_size(size_t val_len)
141+
{
142+
return (__u16)(sizeof(struct nvmf_ext_attr) + libnvmf_exat_len(val_len));
143+
}
144+
145+
#if defined(HAVE_NETDB) || defined(CONFIG_FABRICS)
146+
/**
147+
* libnvmf_getifaddrs - Cached wrapper around getifaddrs()
148+
* @ctx: pointer to the global context
149+
*
150+
* On the first call, this function invokes the POSIX getifaddrs()
151+
* and caches the result in the global context. Subsequent calls
152+
* return the cached data. The caller must NOT call freeifaddrs()
153+
* on the returned data. The cache will be freed when the global
154+
* context is freed.
155+
*
156+
* Return: Pointer to I/F data, NULL on error (with errno set).
157+
*/
158+
const struct ifaddrs *libnvmf_getifaddrs(struct libnvme_global_ctx *ctx);
159+
#endif /* HAVE_NETDB || CONFIG_FABRICS */
160+
109161
bool traddr_is_hostname(struct libnvme_global_ctx *ctx,
110162
const char *transport, const char *traddr);
111163

libnvme/src/nvme/private.h

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ struct libnvme_global_ctx {
337337
bool dry_run;
338338
#ifdef CONFIG_FABRICS
339339
struct libnvme_fabric_options *options;
340-
struct ifaddrs *ifaddrs_cache; /* init with libnvme_getifaddrs() */
340+
struct ifaddrs *ifaddrs_cache; /* init with libnvmf_getifaddrs() */
341341
#endif
342342

343343
enum libnvme_io_uring_state uring_state;
@@ -417,20 +417,6 @@ static inline char *xstrdup(const char *s)
417417
return strdup(s);
418418
}
419419

420-
/**
421-
* libnvme_getifaddrs - Cached wrapper around getifaddrs()
422-
* @ctx: pointer to the global context
423-
*
424-
* On the first call, this function invokes the POSIX getifaddrs()
425-
* and caches the result in the global context. Subsequent calls
426-
* return the cached data. The caller must NOT call freeifaddrs()
427-
* on the returned data. The cache will be freed when the global
428-
* context is freed.
429-
*
430-
* Return: Pointer to I/F data, NULL on error (with errno set).
431-
*/
432-
const struct ifaddrs *libnvme_getifaddrs(struct libnvme_global_ctx *ctx);
433-
434420
/**
435421
* libnvme_ipaddrs_eq - Check if 2 IP addresses are equal.
436422
* @addr1: IP address (can be IPv4 or IPv6)
@@ -440,6 +426,7 @@ const struct ifaddrs *libnvme_getifaddrs(struct libnvme_global_ctx *ctx);
440426
*/
441427
bool libnvme_ipaddrs_eq(const char *addr1, const char *addr2);
442428

429+
#if defined(HAVE_NETDB) || defined(CONFIG_FABRICS)
443430
/**
444431
* libnvme_iface_matching_addr - Get interface matching @addr
445432
* @iface_list: Interface list returned by getifaddrs()
@@ -469,6 +456,7 @@ const char *libnvme_iface_matching_addr(const struct ifaddrs *iface_list,
469456
*/
470457
bool libnvme_iface_primary_addr_matches(const struct ifaddrs *iface_list,
471458
const char *iface, const char *addr);
459+
#endif /* HAVE_NETDB || CONFIG_FABRICS */
472460

473461
int hostname2traddr(struct libnvme_global_ctx *ctx, const char *traddr,
474462
char **hostname);
@@ -552,38 +540,6 @@ char *kv_keymatch(const char *kv, const char *key);
552540
*/
553541
#define round_up(val, mult) ((((val)-1) | __round_mask((val), (mult)))+1)
554542

555-
/**
556-
* nvmf_exat_len() - Return length rounded up by 4
557-
* @val_len: Value length
558-
*
559-
* Return the size in bytes, rounded to a multiple of 4 (e.g., size of
560-
* __u32), of the buffer needed to hold the exat value of size
561-
* @val_len.
562-
*
563-
* Return: Length rounded up by 4
564-
*/
565-
static inline __u16 nvmf_exat_len(size_t val_len)
566-
{
567-
return (__u16)round_up(val_len, sizeof(__u32));
568-
}
569-
570-
/**
571-
* nvmf_exat_size - Return min aligned size to hold value
572-
* @val_len: This is the length of the data to be copied to the "exatval"
573-
* field of a "struct nvmf_ext_attr".
574-
*
575-
* Return the size of the "struct nvmf_ext_attr" needed to hold
576-
* a value of size @val_len.
577-
*
578-
* Return: The size in bytes, rounded to a multiple of 4 (i.e. size of
579-
* __u32), of the "struct nvmf_ext_attr" required to hold a string of
580-
* length @val_len.
581-
*/
582-
static inline __u16 nvmf_exat_size(size_t val_len)
583-
{
584-
return (__u16)(sizeof(struct nvmf_ext_attr) + nvmf_exat_len(val_len));
585-
}
586-
587543
/**
588544
* libnvme_ns_get_transport_handle() - Get associated transport handle
589545
* @n: Namespace instance

libnvme/src/nvme/tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1429,7 +1429,7 @@ static ctrl_match_t _candidate_init(struct libnvme_global_ctx *ctx,
14291429

14301430
#ifdef CONFIG_FABRICS
14311431
if (streq0(fctx->transport, "tcp")) {
1432-
candidate->iface_list = libnvme_getifaddrs(ctx); /* TCP only */
1432+
candidate->iface_list = libnvmf_getifaddrs(ctx); /* TCP only */
14331433
candidate->addreq = libnvme_ipaddrs_eq;
14341434
return _tcp_match_ctrl;
14351435
}

libnvme/src/nvme/util-fabrics.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SPDX-License-Identifier: LGPL-2.1-or-later
2+
/*
3+
* This file is part of libnvme.
4+
* Copyright (c) 2026, Dell Technologies Inc. or its subsidiaries.
5+
*
6+
* Authors: Martin Belanger <[email protected]>
7+
*/
8+
9+
#include <ccan/endian/endian.h>
10+
11+
#include <libnvme.h>
12+
13+
#include "compiler-attributes.h"
14+
#include "private-fabrics.h"
15+
#include "util.h"
16+
17+
__public struct nvmf_ext_attr *libnvmf_exat_ptr_next(struct nvmf_ext_attr *p)
18+
{
19+
__u16 size = libnvmf_exat_size(le16_to_cpu(p->exatlen));
20+
21+
return (struct nvmf_ext_attr *)((uintptr_t)p + (ptrdiff_t)size);
22+
}
23+
24+
const struct ifaddrs *libnvmf_getifaddrs(struct libnvme_global_ctx *ctx)
25+
{
26+
if (!ctx->ifaddrs_cache) {
27+
struct ifaddrs *p;
28+
29+
if (!getifaddrs(&p))
30+
ctx->ifaddrs_cache = p;
31+
}
32+
33+
return ctx->ifaddrs_cache;
34+
}

libnvme/src/nvme/util.c

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -716,12 +716,6 @@ size_t get_entity_version(char *buffer, size_t bufsz)
716716
return num_bytes;
717717
}
718718

719-
__public struct nvmf_ext_attr *libnvmf_exat_ptr_next(struct nvmf_ext_attr *p)
720-
{
721-
return (struct nvmf_ext_attr *)
722-
((uintptr_t)p + (ptrdiff_t)nvmf_exat_size(le16_to_cpu(p->exatlen)));
723-
}
724-
725719
__public const char *libnvme_get_version(enum libnvme_version type)
726720
{
727721
switch(type) {
@@ -938,7 +932,7 @@ bool libnvme_iface_primary_addr_matches(const struct ifaddrs *iface_list,
938932
return match_found;
939933
}
940934

941-
#else /* HAVE_NETDB */
935+
#elif defined(CONFIG_FABRICS)
942936

943937
const char *libnvme_iface_matching_addr(const struct ifaddrs *iface_list,
944938
const char *addr)
@@ -958,7 +952,7 @@ bool libnvme_iface_primary_addr_matches(const struct ifaddrs *iface_list,
958952
return false;
959953
}
960954

961-
#endif /* HAVE_NETDB */
955+
#endif /* HAVE_NETDB || CONFIG_FABRICS */
962956

963957
void *__libnvme_alloc(size_t len)
964958
{
@@ -986,20 +980,6 @@ void *__libnvme_realloc(void *p, size_t len)
986980
return result;
987981
}
988982

989-
#ifdef CONFIG_FABRICS
990-
const struct ifaddrs *libnvme_getifaddrs(struct libnvme_global_ctx *ctx)
991-
{
992-
if (!ctx->ifaddrs_cache) {
993-
struct ifaddrs *p;
994-
995-
if (!getifaddrs(&p))
996-
ctx->ifaddrs_cache = p;
997-
}
998-
999-
return ctx->ifaddrs_cache;
1000-
}
1001-
#endif
1002-
1003983
/* This used instead of basename() due to behavioral differences between
1004984
* the POSIX and the GNU version. This is the glibc implementation.
1005985
* Original source: https://github.com/bminor/glibc/blob/master/string/basename.c */

0 commit comments

Comments
 (0)