Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libnvme/src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 4 additions & 4 deletions libnvme/src/nvme/fabrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down
52 changes: 52 additions & 0 deletions libnvme/src/nvme/private-fabrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
*/
#pragma once

#if defined(HAVE_NETDB) || defined(CONFIG_FABRICS)
#include <ifaddrs.h>
#endif

#include <nvme/fabrics.h>
#include <nvme/tree.h>

Expand Down Expand Up @@ -106,6 +110,54 @@
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));

Check failure on line 142 in libnvme/src/nvme/private-fabrics.h

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: line length of 81 exceeds 80 columns
}

#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);

Expand Down
50 changes: 3 additions & 47 deletions libnvme/src/nvme/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libnvme/src/nvme/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
34 changes: 34 additions & 0 deletions libnvme/src/nvme/util-fabrics.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*/

#include <ccan/endian/endian.h>

#include <libnvme.h>

#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;
}
24 changes: 2 additions & 22 deletions libnvme/src/nvme/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
{
Expand Down Expand Up @@ -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 */
Expand Down
Loading