Skip to content

Commit d2b92f0

Browse files
committed
tree: use traddr_is_hostname from fabrics
This function exists in two files with slightly different implementations. Use a single shared implementation instead. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 185a61e commit d2b92f0

5 files changed

Lines changed: 34 additions & 26 deletions

File tree

libnvme/src/meson.build

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ if want_fabrics
5050
'nvme/nbft.h',
5151
'nvme/accessors-fabrics.h',
5252
]
53+
else
54+
sources += [
55+
'nvme/no-fabrics.c',
56+
]
5357
endif
5458

5559
if want_mi

libnvme/src/nvme/fabrics.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -714,16 +714,21 @@ static int inet_pton_with_scope(struct libnvme_global_ctx *ctx, int af,
714714
return ret;
715715
}
716716

717-
static bool traddr_is_hostname(struct libnvme_global_ctx *ctx, libnvme_ctrl_t c)
717+
bool traddr_is_hostname(struct libnvme_global_ctx *ctx,
718+
const char *transport, const char *traddr)
718719
{
719720
struct sockaddr_storage addr;
720721

721-
if (!c->traddr)
722+
if (!traddr || !transport)
722723
return false;
723-
if (strcmp(c->transport, "tcp") && strcmp(c->transport, "rdma"))
724+
if (!strcmp(traddr, "none"))
724725
return false;
725-
if (inet_pton_with_scope(ctx, AF_UNSPEC, c->traddr, c->trsvcid, &addr) == 0)
726+
if (strcmp(transport, "tcp") && strcmp(transport, "rdma"))
726727
return false;
728+
if (inet_pton_with_scope(ctx, AF_UNSPEC,
729+
traddr, NULL, &addr) == 0) /* scope-aware */
730+
return false;
731+
727732
return true;
728733
}
729734

@@ -1115,7 +1120,7 @@ __public int libnvmf_add_ctrl(libnvme_host_t h, libnvme_ctrl_t c,
11151120
}
11161121

11171122
libnvme_ctrl_set_discovered(c, true);
1118-
if (traddr_is_hostname(h->ctx, c)) {
1123+
if (traddr_is_hostname(h->ctx, c->transport, c->traddr)) {
11191124
char *traddr = c->traddr;
11201125

11211126
if (hostname2traddr(h->ctx, traddr, &c->traddr)) {

libnvme/src/nvme/no-fabrics.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// SPDX-License-Identifier: LGPL-2.1-or-later
2+
/*
3+
* This file is part of libnvme.
4+
* Copyright (c) 2026 SUSE Software Solutions
5+
*
6+
* Authors: Daniel Wagner <[email protected]>
7+
*/
8+
9+
#include "private-fabrics.h"
10+
11+
bool traddr_is_hostname(struct libnvme_global_ctx *ctx,
12+
const char *transport, const char *traddr)
13+
{
14+
return false;
15+
}

libnvme/src/nvme/private-fabrics.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ struct libnvmf_discovery_args { /*!generate-accessors*/
2222
int max_retries;
2323
__u8 lsp;
2424
};
25+
26+
bool traddr_is_hostname(struct libnvme_global_ctx *ctx,
27+
const char *transport, const char *traddr);

libnvme/src/nvme/tree.c

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "cleanup.h"
3535
#include "cleanup-linux.h"
3636
#include "private.h"
37+
#include "private-fabrics.h"
3738
#include "util.h"
3839
#include "compiler-attributes.h"
3940

@@ -1069,26 +1070,6 @@ __public void libnvme_free_ctrl(libnvme_ctrl_t c)
10691070
__libnvme_free_ctrl(c);
10701071
}
10711072

1072-
static bool traddr_is_hostname(const char *transport, const char *traddr)
1073-
{
1074-
1075-
if (!traddr || !transport)
1076-
return false;
1077-
if (!strcmp(traddr, "none"))
1078-
return false;
1079-
#ifdef CONFIG_FABRICS
1080-
char addrstr[NVMF_TRADDR_SIZE];
1081-
1082-
if (strcmp(transport, "tcp") &&
1083-
strcmp(transport, "rdma"))
1084-
return false;
1085-
if (inet_pton(AF_INET, traddr, addrstr) > 0 ||
1086-
inet_pton(AF_INET6, traddr, addrstr) > 0)
1087-
return false;
1088-
#endif
1089-
return true;
1090-
}
1091-
10921073
__public void libnvmf_default_config(struct libnvme_fabrics_config *cfg)
10931074
{
10941075
memset(cfg, 0, sizeof(*cfg));
@@ -1131,7 +1112,7 @@ int _libnvme_create_ctrl(struct libnvme_global_ctx *ctx,
11311112
if (fctx->traddr)
11321113
c->traddr = strdup(fctx->traddr);
11331114
if (fctx->host_traddr) {
1134-
if (traddr_is_hostname(fctx->transport, fctx->host_traddr))
1115+
if (traddr_is_hostname(ctx, fctx->transport, fctx->host_traddr))
11351116
hostname2traddr(ctx, fctx->host_traddr,
11361117
&c->host_traddr);
11371118
if (!c->host_traddr)

0 commit comments

Comments
 (0)