Skip to content

Commit 96e369d

Browse files
committed
libnvme: update Python binding to use libnvmf_context
Teach the Python binding about libnvmf_context so that libnvme_fabric_options can eventually be retired. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 94376ca commit 96e369d

1 file changed

Lines changed: 132 additions & 62 deletions

File tree

libnvme/libnvme/nvme.i

Lines changed: 132 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@
1919

2020
%allowexception;
2121

22-
%rename(global_ctx) libnvme_global_ctx;
23-
%rename(host) libnvme_host;
24-
%rename(ctrl) libnvme_ctrl;
25-
%rename(subsystem) libnvme_subsystem;
26-
%rename(ns) libnvme_ns;
22+
%rename(global_ctx) libnvme_global_ctx;
23+
%rename(host) libnvme_host;
24+
%rename(ctrl) libnvme_ctrl;
25+
%rename(subsystem) libnvme_subsystem;
26+
%rename(ns) libnvme_ns;
27+
%rename(fabrics_context) libnvmf_context;
2728

2829
%{
2930
#include <ccan/list/list.h>
3031
#include <ccan/endian/endian.h>
3132
#include <libnvme.h>
3233
#include "nvme/private.h"
34+
#include "nvme/private-fabrics.h"
3335

3436
static int connect_err = 0;
3537
static int discover_err = 0;
@@ -98,60 +100,6 @@ PyObject *read_hostid();
98100
}
99101
}
100102

101-
%typemap(in) struct libnvme_fabrics_config *($*1_type temp){
102-
Py_ssize_t pos = 0;
103-
PyObject * key,*value;
104-
memset(&temp, 0, sizeof(temp));
105-
temp.tos = -1;
106-
temp.ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO;
107-
while (PyDict_Next($input, &pos, &key, &value)) {
108-
if (!PyUnicode_CompareWithASCIIString(key, "nr_io_queues")) {
109-
temp.nr_io_queues = PyLong_AsLong(value);
110-
continue;
111-
}
112-
if (!PyUnicode_CompareWithASCIIString(key, "reconnect_delay")) {
113-
temp.reconnect_delay = PyLong_AsLong(value);
114-
continue;
115-
}
116-
if (!PyUnicode_CompareWithASCIIString(key, "ctrl_loss_tmo")) {
117-
temp.ctrl_loss_tmo = PyLong_AsLong(value);
118-
continue;
119-
}
120-
if (!PyUnicode_CompareWithASCIIString(key, "keep_alive_tmo")) {
121-
temp.keep_alive_tmo = PyLong_AsLong(value);
122-
continue;
123-
}
124-
if (!PyUnicode_CompareWithASCIIString(key, "nr_write_queues")) {
125-
temp.nr_write_queues = PyLong_AsLong(value);
126-
continue;
127-
}
128-
if (!PyUnicode_CompareWithASCIIString(key, "nr_poll_queues")) {
129-
temp.nr_poll_queues = PyLong_AsLong(value);
130-
continue;
131-
}
132-
if (!PyUnicode_CompareWithASCIIString(key, "tos")) {
133-
temp.tos = PyLong_AsLong(value);
134-
continue;
135-
}
136-
if (!PyUnicode_CompareWithASCIIString(key, "duplicate_connect")) {
137-
temp.duplicate_connect = PyObject_IsTrue(value) ? true : false;
138-
continue;
139-
}
140-
if (!PyUnicode_CompareWithASCIIString(key, "disable_sqflow")) {
141-
temp.disable_sqflow = PyObject_IsTrue(value) ? true : false;
142-
continue;
143-
}
144-
if (!PyUnicode_CompareWithASCIIString(key, "hdr_digest")) {
145-
temp.hdr_digest = PyObject_IsTrue(value) ? true : false;
146-
continue;
147-
}
148-
if (!PyUnicode_CompareWithASCIIString(key, "data_digest")) {
149-
temp.data_digest = PyObject_IsTrue(value) ? true : false;
150-
continue;
151-
}
152-
}
153-
$1 = &temp;
154-
};
155103

156104
%typemap(out) uint8_t [8] {
157105
$result = PyBytes_FromStringAndSize((char *)$1, 8);
@@ -484,6 +432,127 @@ struct libnvme_ns {
484432
uint8_t uuid[16];
485433
};
486434

435+
/*
436+
* %rename directives give the %extend methods Python-friendly names while
437+
* using distinct C-level names (fctx_*) that do not collide with the public
438+
* libnvmf_context_* API declarations in fabrics.h. Without this, SWIG would
439+
* emit SWIGINTERN libnvmf_context_set_hostnqn() which clashes with the
440+
* non-static extern of the same name.
441+
*/
442+
%rename(set_hostnqn) libnvmf_context::fctx_set_hostnqn;
443+
%rename(set_connection) libnvmf_context::fctx_set_connection;
444+
%rename(set_persistent) libnvmf_context::fctx_set_persistent;
445+
%rename(set_device) libnvmf_context::fctx_set_device;
446+
%rename(set_fabrics_config) libnvmf_context::fctx_set_fabrics_config;
447+
448+
struct libnvmf_context {};
449+
450+
%extend libnvmf_context {
451+
libnvmf_context(struct libnvme_global_ctx *ctx) {
452+
struct libnvmf_context *fctx;
453+
struct libnvme_fabrics_config *cfg;
454+
int err;
455+
456+
err = libnvmf_context_create(ctx, NULL, NULL, NULL, NULL, &fctx);
457+
if (err)
458+
return NULL;
459+
460+
cfg = calloc(1, sizeof(*cfg));
461+
if (!cfg) {
462+
libnvmf_context_free(fctx);
463+
return NULL;
464+
}
465+
libnvmf_default_config(cfg);
466+
libnvmf_context_set_fabrics_config(fctx, cfg);
467+
468+
return fctx;
469+
}
470+
~libnvmf_context() {
471+
free($self->cfg);
472+
libnvmf_context_free($self);
473+
}
474+
int fctx_set_hostnqn(const char *hostnqn, const char *hostid = NULL) {
475+
return libnvmf_context_set_hostnqn($self, hostnqn, hostid);
476+
}
477+
int fctx_set_connection(const char *subsysnqn, const char *transport,
478+
const char *traddr = NULL, const char *trsvcid = NULL,
479+
const char *host_traddr = NULL,
480+
const char *host_iface = NULL) {
481+
return libnvmf_context_set_connection($self, subsysnqn, transport,
482+
traddr, trsvcid,
483+
host_traddr, host_iface);
484+
}
485+
int fctx_set_persistent(bool persistent) {
486+
return libnvmf_context_set_persistent($self, persistent);
487+
}
488+
int fctx_set_device(const char *device) {
489+
return libnvmf_context_set_device($self, device);
490+
}
491+
void fctx_set_fabrics_config(PyObject *dict) {
492+
Py_ssize_t pos = 0;
493+
PyObject *key, *value;
494+
495+
if (!$self->cfg)
496+
return;
497+
if (!PyDict_Check(dict)) {
498+
PyErr_SetString(PyExc_TypeError,
499+
"set_fabrics_config: argument must be a dict");
500+
return;
501+
}
502+
503+
while (PyDict_Next(dict, &pos, &key, &value)) {
504+
if (!PyUnicode_CompareWithASCIIString(key, "nr_io_queues")) {
505+
$self->cfg->nr_io_queues = PyLong_AsLong(value);
506+
continue;
507+
}
508+
if (!PyUnicode_CompareWithASCIIString(key, "reconnect_delay")) {
509+
$self->cfg->reconnect_delay = PyLong_AsLong(value);
510+
continue;
511+
}
512+
if (!PyUnicode_CompareWithASCIIString(key, "ctrl_loss_tmo")) {
513+
$self->cfg->ctrl_loss_tmo = PyLong_AsLong(value);
514+
continue;
515+
}
516+
if (!PyUnicode_CompareWithASCIIString(key, "keep_alive_tmo")) {
517+
$self->cfg->keep_alive_tmo = PyLong_AsLong(value);
518+
continue;
519+
}
520+
if (!PyUnicode_CompareWithASCIIString(key, "nr_write_queues")) {
521+
$self->cfg->nr_write_queues = PyLong_AsLong(value);
522+
continue;
523+
}
524+
if (!PyUnicode_CompareWithASCIIString(key, "nr_poll_queues")) {
525+
$self->cfg->nr_poll_queues = PyLong_AsLong(value);
526+
continue;
527+
}
528+
if (!PyUnicode_CompareWithASCIIString(key, "tos")) {
529+
$self->cfg->tos = PyLong_AsLong(value);
530+
continue;
531+
}
532+
if (!PyUnicode_CompareWithASCIIString(key, "duplicate_connect")) {
533+
$self->cfg->duplicate_connect =
534+
PyObject_IsTrue(value) ? true : false;
535+
continue;
536+
}
537+
if (!PyUnicode_CompareWithASCIIString(key, "disable_sqflow")) {
538+
$self->cfg->disable_sqflow =
539+
PyObject_IsTrue(value) ? true : false;
540+
continue;
541+
}
542+
if (!PyUnicode_CompareWithASCIIString(key, "hdr_digest")) {
543+
$self->cfg->hdr_digest =
544+
PyObject_IsTrue(value) ? true : false;
545+
continue;
546+
}
547+
if (!PyUnicode_CompareWithASCIIString(key, "data_digest")) {
548+
$self->cfg->data_digest =
549+
PyObject_IsTrue(value) ? true : false;
550+
continue;
551+
}
552+
}
553+
}
554+
};
555+
487556
%extend libnvme_global_ctx {
488557
libnvme_global_ctx(const char *config_file = NULL) {
489558
struct libnvme_global_ctx *ctx;
@@ -670,7 +739,7 @@ struct libnvme_ns {
670739
%};
671740

672741
%pythonappend libnvme_ctrl::connect(struct libnvme_host *h,
673-
struct libnvme_fabrics_config *cfg) {
742+
struct libnvmf_context *fctx) {
674743
self.__host = h # Keep a reference to parent to ensure ctrl obj gets GCed before host}
675744
%pythonappend libnvme_ctrl::init(struct libnvme_host *h, int instance) {
676745
self.__host = h # Keep a reference to parent to ensure ctrl obj gets GCed before host}
@@ -705,12 +774,13 @@ struct libnvme_ns {
705774
}
706775

707776
void connect(struct libnvme_host *h,
708-
struct libnvme_fabrics_config *cfg = NULL) {
777+
struct libnvmf_context *fctx = NULL) {
709778
int ret;
710779
const char *dev;
780+
const struct libnvme_fabrics_config *cfg = fctx ? fctx->cfg : NULL;
711781

712782
dev = libnvme_ctrl_get_name($self);
713-
if (dev && !cfg->duplicate_connect) {
783+
if (dev && !(cfg && cfg->duplicate_connect)) {
714784
connect_err = -ENVME_CONNECT_ALREADY;
715785
return;
716786
}

0 commit comments

Comments
 (0)