Skip to content

Commit db85560

Browse files
author
Martin Belanger
committed
libnvmf: replace nvmf_get_discovery_log() with opaque-args API
Introduce struct nvmf_discovery_args as an ABI-stable opaque object with auto-generated setter/getter accessors. The mandatory controller argument moves to the function signature; optional parameters (timeout, max_retries, lsp) are set via accessors. Remove the old nvmf_get_discovery_wargs() API entirely. Split the accessor generator output into a common NVMe triplet (accessors.{h,c,ld}) and an NVMe-oF-specific triplet (nvmf-accessors.{h,c,ld}) so that non-fabrics builds can exclude all fabrics code. The generator script is made generic; two meson targets (update-common-accessors, update-fabrics-accessors) are exposed under the update-accessors alias. Fix a bug in the generator where //!accessors:readonly members were correctly suppressed in the .h/.c output but still emitted as setters in the .ld version script. Signed-off-by: Martin Belanger <[email protected]>
1 parent 3e86668 commit db85560

16 files changed

Lines changed: 454 additions & 170 deletions

libnvme/examples/discover-loop.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,16 @@ int main()
8686
return 1;
8787
}
8888

89-
ret = nvmf_get_discovery_log(c, &log, 4);
89+
{
90+
struct nvmf_discovery_args *dargs;
91+
92+
ret = nvmf_discovery_args_create(&dargs);
93+
if (!ret) {
94+
nvmf_discovery_args_set_max_retries(dargs, 4);
95+
ret = nvmf_get_discovery_log(c, dargs, &log);
96+
nvmf_discovery_args_free(dargs);
97+
}
98+
}
9099
libnvme_disconnect_ctrl(c);
91100
libnvme_free_ctrl(c);
92101

libnvme/libnvme/nvme.i

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -777,25 +777,22 @@ struct libnvme_ns {
777777

778778
%newobject discover;
779779
struct nvmf_discovery_log *discover(int lsp = 0, int max_retries = 6) {
780-
const char *dev;
781780
struct nvmf_discovery_log *logp = NULL;
782-
struct libnvme_get_discovery_args args = {
783-
.c = $self,
784-
.args_size = sizeof(args),
785-
.max_retries = max_retries,
786-
.result = NULL,
787-
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT,
788-
.lsp = lsp,
789-
};
781+
struct nvmf_discovery_args *args = NULL;
790782

791-
dev = libnvme_ctrl_get_name($self);
792-
if (!dev) {
783+
if (!libnvme_ctrl_get_name($self)) {
793784
discover_err = 1;
794785
return NULL;
795786
}
787+
discover_err = nvmf_discovery_args_create(&args);
788+
if (discover_err)
789+
return NULL;
790+
nvmf_discovery_args_set_lsp(args, lsp);
791+
nvmf_discovery_args_set_max_retries(args, max_retries);
796792
Py_BEGIN_ALLOW_THREADS /* Release Python GIL */
797-
discover_err = nvmf_get_discovery_wargs(&args, &logp);
793+
discover_err = nvmf_get_discovery_log($self, args, &logp);
798794
Py_END_ALLOW_THREADS /* Reacquire Python GIL */
795+
nvmf_discovery_args_free(args);
799796

800797
if (logp == NULL) discover_err = 2;
801798
return logp;

libnvme/src/accessors.ld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ LIBNVME_ACCESSORS_3 {
6161
libnvme_ctrl_get_cntrltype;
6262
libnvme_ctrl_get_cntlid;
6363
libnvme_ctrl_get_dctype;
64+
libnvme_ctrl_get_phy_slot;
6465
libnvme_ctrl_get_host_traddr;
6566
libnvme_ctrl_get_host_iface;
6667
libnvme_ctrl_get_discovery_ctrl;
@@ -71,7 +72,6 @@ LIBNVME_ACCESSORS_3 {
7172
libnvme_ctrl_set_discovered;
7273
libnvme_ctrl_get_persistent;
7374
libnvme_ctrl_set_persistent;
74-
libnvme_ctrl_get_phy_slot;
7575
libnvme_subsystem_get_name;
7676
libnvme_subsystem_get_sysfs_dir;
7777
libnvme_subsystem_get_subsysnqn;

libnvme/src/libnvmf.ld

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ LIBNVMF_3 {
2828
nvmf_exat_ptr_next;
2929
nvmf_free_uri;
3030
nvmf_get_default_trsvcid;
31+
nvmf_discovery_args_create;
32+
nvmf_discovery_args_free;
3133
nvmf_get_discovery_log;
32-
nvmf_get_discovery_wargs;
3334
nvmf_is_registration_supported;
3435
nvmf_nbft_free;
3536
nvmf_nbft_read_files;

libnvme/src/meson.build

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ if want_fabrics
4545
sources += [
4646
'nvme/fabrics.c',
4747
'nvme/nbft.c',
48+
'nvme/nvmf-accessors.c',
4849
]
4950
headers += [
5051
'nvme/fabrics.h',
5152
'nvme/nbft.h',
53+
'nvme/nvmf-accessors.h',
5254
]
5355
endif
5456

@@ -85,6 +87,7 @@ endif
8587
nvme_ld = meson.current_source_dir() / 'libnvme.ld'
8688
nvmf_ld = meson.current_source_dir() / 'libnvmf.ld'
8789
accessors_ld = meson.current_source_dir() / 'accessors.ld'
90+
nvmf_accessors_ld = meson.current_source_dir() / 'nvmf-accessors.ld'
8891

8992
link_args = [
9093
'-Wl,--version-script=@0@'.format(nvme_ld),
@@ -93,9 +96,12 @@ link_args = [
9396

9497
libconf = configuration_data()
9598
if want_fabrics
96-
link_args += '-Wl,--version-script=@0@'.format(nvmf_ld)
99+
link_args += [
100+
'-Wl,--version-script=@0@'.format(nvmf_ld),
101+
'-Wl,--version-script=@0@'.format(nvmf_accessors_ld),
102+
]
97103
libconf.set('FABRICS_INCLUDE',
98-
'#include <nvme/fabrics.h>\n#include <nvme/nbft.h>')
104+
'#include <nvme/fabrics.h>\n#include <nvme/nbft.h>\n#include <nvme/nvmf-accessors.h>')
99105
else
100106
libconf.set('FABRICS_INCLUDE', '')
101107
endif

libnvme/src/nvme/fabrics.c

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
#include "cleanup.h"
3838
#include "private.h"
39+
#include "private-fabrics.h"
3940
#include "compiler_attributes.h"
4041

4142
const char *nvmf_dev = "/dev/nvme-fabrics";
@@ -1297,16 +1298,17 @@ static int nvmf_connect_disc_entry(libnvme_host_t h,
12971298
*/
12981299
#define DISCOVERY_HEADER_LEN 20
12991300

1300-
static int nvme_discovery_log(const struct libnvme_get_discovery_args *args,
1301+
static int nvme_discovery_log(libnvme_ctrl_t ctrl,
1302+
const struct nvmf_discovery_args *args,
13011303
struct nvmf_discovery_log **logp)
13021304
{
1303-
struct libnvme_global_ctx *ctx = args->c->ctx;
1305+
struct libnvme_global_ctx *ctx = ctrl->ctx;
13041306
struct nvmf_discovery_log *log;
13051307
int retries = 0;
13061308
int err;
1307-
const char *name = libnvme_ctrl_get_name(args->c);
1309+
const char *name = libnvme_ctrl_get_name(ctrl);
13081310
uint64_t genctr, numrec;
1309-
struct libnvme_transport_handle *hdl = libnvme_ctrl_get_transport_handle(args->c);
1311+
struct libnvme_transport_handle *hdl = libnvme_ctrl_get_transport_handle(ctrl);
13101312
struct libnvme_passthru_cmd cmd;
13111313

13121314
log = __libnvme_alloc(sizeof(*log));
@@ -1319,6 +1321,7 @@ static int nvme_discovery_log(const struct libnvme_get_discovery_args *args,
13191321
libnvme_msg(ctx, LOG_DEBUG, "%s: get header (try %d/%d)\n",
13201322
name, retries, args->max_retries);
13211323
nvme_init_get_log_discovery(&cmd, 0, log, DISCOVERY_HEADER_LEN);
1324+
cmd.timeout_ms = args->timeout;
13221325
err = libnvme_get_log(hdl, &cmd, false, DISCOVERY_HEADER_LEN);
13231326
if (err) {
13241327
libnvme_msg(ctx, LOG_INFO,
@@ -1350,6 +1353,7 @@ static int nvme_discovery_log(const struct libnvme_get_discovery_args *args,
13501353
name, numrec, genctr);
13511354

13521355
nvme_init_get_log_discovery(&cmd, sizeof(*log), log->entries, entries_size);
1356+
cmd.timeout_ms = args->timeout;
13531357
cmd.cdw10 |= NVME_FIELD_ENCODE(args->lsp,
13541358
NVME_LOG_CDW10_LSP_SHIFT,
13551359
NVME_LOG_CDW10_LSP_MASK);
@@ -1368,6 +1372,7 @@ static int nvme_discovery_log(const struct libnvme_get_discovery_args *args,
13681372
libnvme_msg(ctx, LOG_DEBUG, "%s: get header again\n", name);
13691373

13701374
nvme_init_get_log_discovery(&cmd, 0, log, DISCOVERY_HEADER_LEN);
1375+
cmd.timeout_ms = args->timeout;
13711376
err = libnvme_get_log(hdl, &cmd, false, DISCOVERY_HEADER_LEN);
13721377
if (err) {
13731378
libnvme_msg(ctx, LOG_INFO,
@@ -1414,27 +1419,38 @@ static void sanitize_discovery_log_entry(struct nvmf_disc_log_entry *e)
14141419
}
14151420
}
14161421

1417-
__public int nvmf_get_discovery_log(libnvme_ctrl_t c, struct nvmf_discovery_log **logp,
1418-
int max_retries)
1422+
__public int nvmf_discovery_args_create(struct nvmf_discovery_args **argsp)
14191423
{
1420-
struct libnvme_get_discovery_args args = {
1421-
.c = c,
1422-
.max_retries = max_retries,
1423-
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT,
1424-
.lsp = NVMF_LOG_DISC_LSP_NONE,
1425-
};
1424+
struct nvmf_discovery_args *args;
1425+
1426+
if (!argsp)
1427+
return -EINVAL;
1428+
1429+
args = calloc(1, sizeof(*args));
1430+
if (!args)
1431+
return -ENOMEM;
14261432

1433+
args->max_retries = 6;
1434+
args->timeout = NVME_DEFAULT_IOCTL_TIMEOUT;
1435+
args->lsp = NVMF_LOG_DISC_LSP_NONE;
14271436

1428-
return nvmf_get_discovery_wargs(&args, logp);
1437+
*argsp = args;
1438+
return 0;
14291439
}
14301440

1431-
__public int nvmf_get_discovery_wargs(struct libnvme_get_discovery_args *args,
1432-
struct nvmf_discovery_log **logp)
1441+
__public void nvmf_discovery_args_free(struct nvmf_discovery_args *args)
1442+
{
1443+
free(args);
1444+
}
1445+
1446+
__public int nvmf_get_discovery_log(libnvme_ctrl_t ctrl,
1447+
struct nvmf_discovery_args *args,
1448+
struct nvmf_discovery_log **logp)
14331449
{
14341450
struct nvmf_discovery_log *log;
14351451
int err;
14361452

1437-
err = nvme_discovery_log(args, &log);
1453+
err = nvme_discovery_log(ctrl, args, &log);
14381454
if (err)
14391455
return err;
14401456

@@ -1445,6 +1461,7 @@ __public int nvmf_get_discovery_wargs(struct libnvme_get_discovery_args *args,
14451461
return 0;
14461462
}
14471463

1464+
14481465
/**
14491466
* nvmf_get_tel() - Calculate the amount of memory needed for a DIE.
14501467
* @hostsymname: Symbolic name (may be NULL)
@@ -1978,15 +1995,13 @@ static int _nvmf_discovery(struct libnvme_global_ctx *ctx,
19781995
uint64_t numrec;
19791996
int err;
19801997

1981-
struct libnvme_get_discovery_args args = {
1982-
.c = c,
1983-
.args_size = sizeof(args),
1998+
struct nvmf_discovery_args args = {
19841999
.max_retries = fctx->default_max_discovery_retries,
1985-
.result = 0,
1986-
.lsp = 0,
2000+
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT,
2001+
.lsp = NVMF_LOG_DISC_LSP_NONE,
19872002
};
19882003

1989-
err = nvmf_get_discovery_wargs(&args, &log);
2004+
err = nvme_discovery_log(c, &args, &log);
19902005
if (err) {
19912006
libnvme_msg(ctx, LOG_ERR, "failed to get discovery log: %s\n",
19922007
libnvme_strerror(err));
@@ -2661,16 +2676,13 @@ static int nbft_discovery(struct libnvme_global_ctx *ctx,
26612676
int ret;
26622677
int i;
26632678

2664-
struct libnvme_get_discovery_args args = {
2665-
.c = c,
2666-
.args_size = sizeof(args),
2679+
struct nvmf_discovery_args args = {
26672680
.max_retries = 10 /* MAX_DISC_RETRIES */,
2668-
.result = 0,
26692681
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT,
2670-
.lsp = 0,
2682+
.lsp = NVMF_LOG_DISC_LSP_NONE,
26712683
};
26722684

2673-
ret = nvmf_get_discovery_wargs(&args, &log);
2685+
ret = nvme_discovery_log(c, &args, &log);
26742686
if (ret) {
26752687
libnvme_msg(ctx, LOG_ERR,
26762688
"Discovery Descriptor %d: failed to get discovery log: %s\n",

libnvme/src/nvme/fabrics.h

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -232,57 +232,47 @@ int nvmf_add_ctrl(libnvme_host_t h, libnvme_ctrl_t c,
232232
*/
233233
int nvmf_connect_ctrl(libnvme_ctrl_t c);
234234

235-
/**
236-
* nvmf_get_discovery_log() - Return the discovery log page
237-
* @c: Discovery controller to use
238-
* @logp: Log page object to return
239-
* @max_retries: Number of retries in case of failure
235+
/*
236+
* struct nvmf_discovery_args - Opaque arguments for nvmf_get_discovery_log()
240237
*
241-
* The memory allocated for the log page and returned in @logp
242-
* must be freed by the caller using free().
238+
* Allocate with nvmf_discovery_args_create() and release with
239+
* nvmf_discovery_args_free(). Use the setter/getter accessors to configure
240+
* fields; do not access members directly.
241+
*/
242+
struct nvmf_discovery_args;
243+
244+
/**
245+
* nvmf_discovery_args_create() - Allocate a discovery args object
246+
* @argsp: On success, set to the newly allocated object
243247
*
244-
* Note: Consider using nvmf_get_discovery_wargs() instead.
248+
* Allocates and initialises a &struct nvmf_discovery_args with sensible
249+
* defaults. The caller must release it with nvmf_discovery_args_free().
245250
*
246-
* Return: 0 on success, or an error code on failure.
251+
* Return: 0 on success, or a negative error code on failure.
247252
*/
248-
int nvmf_get_discovery_log(libnvme_ctrl_t c, struct nvmf_discovery_log **logp,
249-
int max_retries);
250-
251-
/**
252-
* struct libnvme_get_discovery_args - Arguments for nvmf_get_discovery_wargs()
253-
* @c: Discovery controller
254-
* @args_size: Length of the structure
255-
* @max_retries: Number of retries in case of failure
256-
* @result: The command completion result from CQE dword0
257-
* @timeout: Timeout in ms (default: NVME_DEFAULT_IOCTL_TIMEOUT)
258-
* @lsp: Log specific field (See enum nvmf_log_discovery_lsp)
259-
*/
260-
struct libnvme_get_discovery_args {
261-
libnvme_ctrl_t c;
262-
int args_size;
263-
int max_retries;
264-
__u32 *result;
265-
__u32 timeout;
266-
__u8 lsp;
267-
};
253+
int nvmf_discovery_args_create(struct nvmf_discovery_args **argsp);
268254

269255
/**
270-
* nvmf_get_discovery_wargs() - Get the discovery log page with args
271-
* @args: Argument structure
272-
* @log: Discovery log page object to return
273-
*
274-
* This function is similar to nvmf_get_discovery_log(), but
275-
* takes an extensible @args parameter. @args provides more
276-
* options than nvmf_get_discovery_log().
256+
* nvmf_discovery_args_free() - Release a discovery args object
257+
* @args: Object previously returned by nvmf_discovery_args_create()
258+
*/
259+
void nvmf_discovery_args_free(struct nvmf_discovery_args *args);
260+
261+
/**
262+
* nvmf_get_discovery_log() - Fetch the NVMe-oF discovery log page
263+
* @ctrl: Discovery controller
264+
* @args: Optional arguments (pass NULL for defaults)
265+
* @logp: On success, set to the allocated log page (caller must free())
277266
*
278-
* This function performs a get discovery log page (DLP) command
279-
* and returns the DLP. The memory allocated for the returned
280-
* DLP must be freed by the caller using free().
267+
* Issues the three-phase Get Log Page protocol against @ctrl, validates
268+
* generation-counter atomicity, and normalises each log entry.
281269
*
282-
* Return: 0 on success, or an error code on failure.
270+
* Return: 0 on success, or a negative error code on failure.
283271
*/
284-
int nvmf_get_discovery_wargs(struct libnvme_get_discovery_args *args,
285-
struct nvmf_discovery_log **log);
272+
int nvmf_get_discovery_log(libnvme_ctrl_t ctrl,
273+
struct nvmf_discovery_args *args,
274+
struct nvmf_discovery_log **logp);
275+
286276

287277
/**
288278
* nvmf_is_registration_supported - check whether registration can be performed.

0 commit comments

Comments
 (0)