Skip to content

Commit fe245d2

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 nvmf_exat_len()/nvmf_exat_size() static inlines 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. 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 fe245d2

6 files changed

Lines changed: 85 additions & 69 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/private-fabrics.h

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

10+
#include <ifaddrs.h>
11+
1012
#include <nvme/fabrics.h>
1113
#include <nvme/tree.h>
1214

@@ -106,6 +108,52 @@ struct libnvmf_uri { // !generate-accessors
106108
char *fragment;
107109
};
108110

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

libnvme/src/nvme/private.h

Lines changed: 1 addition & 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)
@@ -552,38 +538,6 @@ char *kv_keymatch(const char *kv, const char *key);
552538
*/
553539
#define round_up(val, mult) ((((val)-1) | __round_mask((val), (mult)))+1)
554540

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-
587541
/**
588542
* libnvme_ns_get_transport_handle() - Get associated transport handle
589543
* @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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
return (struct nvmf_ext_attr *)
20+
((uintptr_t)p + (ptrdiff_t)nvmf_exat_size(le16_to_cpu(p->exatlen)));
21+
}
22+
23+
const struct ifaddrs *libnvmf_getifaddrs(struct libnvme_global_ctx *ctx)
24+
{
25+
if (!ctx->ifaddrs_cache) {
26+
struct ifaddrs *p;
27+
28+
if (!getifaddrs(&p))
29+
ctx->ifaddrs_cache = p;
30+
}
31+
32+
return ctx->ifaddrs_cache;
33+
}

libnvme/src/nvme/util.c

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <string.h>
1717
#include <unistd.h>
1818

19-
#if defined(HAVE_NETDB) || defined(CONFIG_FABRICS)
19+
#ifdef HAVE_NETDB
2020
#include <ifaddrs.h>
2121

2222
#include <arpa/inet.h>
@@ -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) {
@@ -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)