forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnvme.i
More file actions
1279 lines (1127 loc) · 37.7 KB
/
nvme.i
File metadata and controls
1279 lines (1127 loc) · 37.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* This file is part of libnvme.
* Copyright (c) 2021 SUSE Software Solutions
*
* Authors: Hannes Reinecke <[email protected]>
*/
%begin %{
/* WORKAROUND: The top-level meson.build defines the macro "fallthrough", which
clashes with the same macro defined in Python.h.
*/
#undef fallthrough
%}
%module(docstring="Python bindings for libnvme") nvme
%feature("autodoc", "1");
%include "exception.i"
%allowexception;
%rename(global_ctx) nvme_global_ctx;
%rename(host) nvme_host;
%rename(ctrl) nvme_ctrl;
%rename(subsystem) nvme_subsystem;
%rename(ns) nvme_ns;
%{
#include <ccan/list/list.h>
#include <ccan/endian/endian.h>
#include "nvme/tree.h"
#include "nvme/fabrics.h"
#include "nvme/private.h"
#include "nvme/log.h"
#include "nvme/ioctl.h"
#include "nvme/types.h"
#include "nvme/nbft.h"
static int connect_err = 0;
static int discover_err = 0;
static void PyDict_SetItemStringDecRef(PyObject * p, const char *key, PyObject *val) {
PyDict_SetItemString(p, key, val); /* Does NOT steal reference to val .. */
Py_XDECREF(val); /* .. therefore decrement ref. count. */
}
PyObject *hostnqn_from_file() {
char * val = nvmf_hostnqn_from_file();
PyObject * obj = PyUnicode_FromString(val);
free(val);
return obj;
}
PyObject *hostid_from_file() {
char * val = nvmf_hostid_from_file();
PyObject * obj = PyUnicode_FromString(val);
free(val);
return obj;
}
%}
PyObject *hostnqn_from_file();
PyObject *hostid_from_file();
%exception nvme_ctrl::connect {
connect_err = 0;
$action /* $action sets connect_err to non-zero value on failure */
if (connect_err) {
const char *errstr = nvme_errno_to_string(-connect_err);
if (errstr) {
SWIG_exception(SWIG_RuntimeError, errstr);
} else {
SWIG_exception(SWIG_RuntimeError, "Connect failed");
}
}
}
%exception nvme_ctrl::disconnect {
connect_err = 0;
errno = 0;
$action /* $action sets connect_err to non-zero value on failure */
if (connect_err == 1) {
SWIG_exception(SWIG_AttributeError, "No controller connection");
} else if (connect_err) {
const char *errstr = nvme_errno_to_string(-connect_err);
if (errstr) {
SWIG_exception(SWIG_RuntimeError, errstr);
} else {
SWIG_exception(SWIG_RuntimeError, "Disconnect failed");
}
}
}
%exception nvme_ctrl::discover {
discover_err = 0;
$action /* $action sets discover_err to non-zero value on failure */
if (discover_err == 1) {
SWIG_exception(SWIG_AttributeError, "No controller connection");
} else if (discover_err) {
const char *errstr = nvme_errno_to_string(-discover_err);
if (errstr) {
SWIG_exception(SWIG_RuntimeError, errstr);
} else {
SWIG_exception(SWIG_RuntimeError, "Discover failed");
}
}
}
%typemap(in) struct nvme_fabrics_config *($*1_type temp){
Py_ssize_t pos = 0;
PyObject * key,*value;
memset(&temp, 0, sizeof(temp));
temp.tos = -1;
temp.ctrl_loss_tmo = NVMF_DEF_CTRL_LOSS_TMO;
while (PyDict_Next($input, &pos, &key, &value)) {
if (!PyUnicode_CompareWithASCIIString(key, "nr_io_queues")) {
temp.nr_io_queues = PyLong_AsLong(value);
continue;
}
if (!PyUnicode_CompareWithASCIIString(key, "reconnect_delay")) {
temp.reconnect_delay = PyLong_AsLong(value);
continue;
}
if (!PyUnicode_CompareWithASCIIString(key, "ctrl_loss_tmo")) {
temp.ctrl_loss_tmo = PyLong_AsLong(value);
continue;
}
if (!PyUnicode_CompareWithASCIIString(key, "keep_alive_tmo")) {
temp.keep_alive_tmo = PyLong_AsLong(value);
continue;
}
if (!PyUnicode_CompareWithASCIIString(key, "nr_write_queues")) {
temp.nr_write_queues = PyLong_AsLong(value);
continue;
}
if (!PyUnicode_CompareWithASCIIString(key, "nr_poll_queues")) {
temp.nr_poll_queues = PyLong_AsLong(value);
continue;
}
if (!PyUnicode_CompareWithASCIIString(key, "tos")) {
temp.tos = PyLong_AsLong(value);
continue;
}
if (!PyUnicode_CompareWithASCIIString(key, "duplicate_connect")) {
temp.duplicate_connect = PyObject_IsTrue(value) ? true : false;
continue;
}
if (!PyUnicode_CompareWithASCIIString(key, "disable_sqflow")) {
temp.disable_sqflow = PyObject_IsTrue(value) ? true : false;
continue;
}
if (!PyUnicode_CompareWithASCIIString(key, "hdr_digest")) {
temp.hdr_digest = PyObject_IsTrue(value) ? true : false;
continue;
}
if (!PyUnicode_CompareWithASCIIString(key, "data_digest")) {
temp.data_digest = PyObject_IsTrue(value) ? true : false;
continue;
}
}
$1 = &temp;
};
%typemap(out) uint8_t [8] {
$result = PyBytes_FromStringAndSize((char *)$1, 8);
};
%typemap(out) uint8_t [16] {
$result = PyBytes_FromStringAndSize((char *)$1, 16);
};
%typemap(newfree) struct nvmf_discovery_log * {
if ($1) free($1);
}
%typemap(out) struct nvmf_discovery_log * {
struct nvmf_discovery_log *log = $1;
int numrec = log ? log->numrec : 0, i;
PyObject *obj = PyList_New(numrec);
if (!obj) return NULL;
for (i = 0; i < numrec; i++) {
struct nvmf_disc_log_entry *e = &log->entries[i];
PyObject *entry = PyDict_New(), *val;
switch (e->trtype) {
case NVMF_TRTYPE_UNSPECIFIED:
val = PyUnicode_FromString("unspecified");
break;
case NVMF_TRTYPE_RDMA:
val = PyUnicode_FromString("rdma");
break;
case NVMF_TRTYPE_FC:
val = PyUnicode_FromString("fc");
break;
case NVMF_TRTYPE_TCP:
val = PyUnicode_FromString("tcp");
break;
case NVMF_TRTYPE_LOOP:
val = PyUnicode_FromString("loop");
break;
default:
val = PyLong_FromLong(e->trtype);
}
PyDict_SetItemStringDecRef(entry, "trtype", val);
switch (e->adrfam) {
case NVMF_ADDR_FAMILY_PCI:
val = PyUnicode_FromString("pci");
break;
case NVMF_ADDR_FAMILY_IP4:
val = PyUnicode_FromString("ipv4");
break;
case NVMF_ADDR_FAMILY_IP6:
val = PyUnicode_FromString("ipv6");
break;
case NVMF_ADDR_FAMILY_IB:
val = PyUnicode_FromString("infiniband");
break;
case NVMF_ADDR_FAMILY_FC:
val = PyUnicode_FromString("fc");
break;
default:
val = PyLong_FromLong(e->adrfam);
}
PyDict_SetItemStringDecRef(entry, "adrfam", val);
val = PyUnicode_FromString(e->traddr);
PyDict_SetItemStringDecRef(entry, "traddr", val);
val = PyUnicode_FromString(e->trsvcid);
PyDict_SetItemStringDecRef(entry, "trsvcid", val);
val = PyUnicode_FromString(e->subnqn);
PyDict_SetItemStringDecRef(entry, "subnqn", val);
switch (e->subtype) {
case NVME_NQN_DISC:
val = PyUnicode_FromString("referral");
break;
case NVME_NQN_NVME:
val = PyUnicode_FromString("nvme");
break;
case NVME_NQN_CURR:
val = PyUnicode_FromString("discovery");
break;
default:
val = PyLong_FromLong(e->subtype);
}
PyDict_SetItemStringDecRef(entry, "subtype", val);
switch (e->treq) {
case NVMF_TREQ_NOT_SPECIFIED:
val = PyUnicode_FromString("not specified");
break;
case NVMF_TREQ_REQUIRED:
val = PyUnicode_FromString("required");
break;
case NVMF_TREQ_NOT_REQUIRED:
val = PyUnicode_FromString("not required");
break;
case NVMF_TREQ_DISABLE_SQFLOW:
val = PyUnicode_FromString("disable sqflow");
break;
default:
val = PyLong_FromLong(e->treq);
}
PyDict_SetItemStringDecRef(entry, "treq", val);
if (e->trtype == NVMF_TRTYPE_TCP) {
PyObject *tsas = PyDict_New();
switch (e->tsas.tcp.sectype) {
case NVMF_TCP_SECTYPE_NONE:
val = PyUnicode_FromString("none");
break;
case NVMF_TCP_SECTYPE_TLS:
val = PyUnicode_FromString("tls");
break;
case NVMF_TCP_SECTYPE_TLS13:
val = PyUnicode_FromString("tls1.3");
break;
default:
val = PyUnicode_FromString("reserved");
break;
}
PyDict_SetItemStringDecRef(tsas, "sectype", val);
PyDict_SetItemStringDecRef(entry, "tsas", tsas);
} else if (e->trtype == NVMF_TRTYPE_RDMA) {
PyObject *tsas = PyDict_New();
switch (e->tsas.rdma.qptype) {
case NVMF_RDMA_QPTYPE_CONNECTED:
val = PyUnicode_FromString("connected");
break;
case NVMF_RDMA_QPTYPE_DATAGRAM:
val = PyUnicode_FromString("datagram");
break;
default:
val = PyUnicode_FromString("reserved");
break;
}
PyDict_SetItemStringDecRef(tsas, "qptype", val);
switch (e->tsas.rdma.prtype) {
case NVMF_RDMA_PRTYPE_NOT_SPECIFIED:
val = PyUnicode_FromString("not specified");
break;
case NVMF_RDMA_PRTYPE_IB:
val = PyUnicode_FromString("infiniband");
break;
case NVMF_RDMA_PRTYPE_ROCE:
val = PyUnicode_FromString("roce");
break;
case NVMF_RDMA_PRTYPE_ROCEV2:
val = PyUnicode_FromString("rocev2");
break;
case NVMF_RDMA_PRTYPE_IWARP:
val = PyUnicode_FromString("iwarp");
break;
default:
val = PyUnicode_FromString("reserved");
break;
}
PyDict_SetItemStringDecRef(tsas, "prtype", val);
switch (e->tsas.rdma.cms) {
case NVMF_RDMA_CMS_RDMA_CM:
val = PyUnicode_FromString("cm");
break;
default:
val = PyUnicode_FromString("reserved");
break;
}
PyDict_SetItemStringDecRef(tsas, "cms", val);
PyDict_SetItemStringDecRef(entry, "tsas", tsas);
}
val = PyLong_FromLong(e->portid);
PyDict_SetItemStringDecRef(entry, "portid", val);
val = PyLong_FromLong(e->cntlid);
PyDict_SetItemStringDecRef(entry, "cntlid", val);
val = PyLong_FromLong(e->asqsz);
PyDict_SetItemStringDecRef(entry, "asqsz", val);
val = PyLong_FromLong(e->eflags);
PyDict_SetItemStringDecRef(entry, "eflags", val);
PyList_SetItem(obj, i, entry); /* steals ref. to object - no need to decref */
}
$result = obj;
};
#include "tree.h"
#include "fabrics.h"
#define STR_OR_NONE(str) (!(str) ? "None" : str)
struct nvme_host * nvme_first_host(struct nvme_global_ctx * ctx);
struct nvme_host * nvme_next_host(struct nvme_global_ctx *ctx, struct nvme_host * h);
struct nvme_subsystem * nvme_first_subsystem(struct nvme_host * h);
struct nvme_subsystem * nvme_next_subsystem(struct nvme_host * h, struct nvme_subsystem * s);
struct nvme_ctrl * nvme_subsystem_first_ctrl(struct nvme_subsystem * s);
struct nvme_ctrl * nvme_subsystem_next_ctrl(struct nvme_subsystem * s, struct nvme_ctrl * c);
struct nvme_ns * nvme_subsystem_first_ns(struct nvme_subsystem * s);
struct nvme_ns * nvme_subsystem_next_ns(struct nvme_subsystem * s, struct nvme_ns * n);
struct nvme_ns * nvme_ctrl_first_ns(struct nvme_ctrl * c);
struct nvme_ns * nvme_ctrl_next_ns(struct nvme_ctrl * c, struct nvme_ns * n);
struct nvme_global_ctx {
%immutable config_file;
%immutable application;
char *config_file;
char *application;
};
struct nvme_host {
%immutable hostnqn;
%immutable hostid;
%immutable hostsymname;
char *hostnqn;
char *hostid;
char *hostsymname;
%extend {
char *dhchap_key;
}
};
struct nvme_subsystem {
%immutable sysfs_dir;
%immutable subsysnqn;
%immutable model;
%immutable serial;
%immutable firmware;
%immutable subsystype;
%immutable iopolicy;
char *subsysnqn;
char *model;
char *serial;
char *firmware;
char *subsystype;
%extend {
const char *sysfs_dir;
const char *application;
const char *iopolicy;
}
};
struct nvme_ctrl {
%immutable name;
%immutable subsystem;
%immutable state;
%immutable sysfs_dir;
%immutable address;
%immutable firmware;
%immutable model;
%immutable numa_node;
%immutable queue_count;
%immutable serial;
%immutable sqsize;
%immutable transport;
%immutable subsysnqn;
%immutable traddr;
%immutable trsvcid;
%immutable cntrltype;
%immutable cntlid;
%immutable dctype;
%immutable phy_slot;
%immutable discovered;
const char *cntrltype; // Do not put in %extend because there's no getter method in libnvme.map
const char *dctype; // Do not put in %extend because there's no getter method in libnvme.map
const bool discovered; // Do not put in %extend because there's no getter method in libnvme.map
%extend {
/**
* By putting these attributes in an %extend block, we're
* forcing SWIG to invoke getter/setter methods instead of
* accessing the members directly.
*
* For example, SWIG will generate code like this:
* name = nvme_ctrl_name_get(ctrl)
*
* instead of that:
* name = ctrl->name
*/
const char *name;
const char *state;
const char *sysfs_dir;
const char *address;
const char *firmware;
const char *model;
const char *numa_node;
const char *queue_count;
const char *serial;
const char *sqsize;
const char *transport;
const char *subsysnqn;
const char *traddr;
const char *trsvcid;
const char *cntlid;
const char *phy_slot;
bool unique_discovery_ctrl;
bool discovery_ctrl;
bool persistent;
char *keyring;
char *tls_key_identity;
char *tls_key;
/**
* We are remapping the following members of the C code's
* nvme_ctrl_t to different names in Python. Here's the mapping:
*
* C code Python (SWIG)
* ===================== =====================
* ctrl->s ctrl->subsystem
* ctrl->dhchap_key ctrl->dhchap_host_key
* ctrl->dhchap_ctrl_key ctrl->dhchap_key
*/
struct nvme_subsystem *subsystem; // Maps to "s" in the C code
char *dhchap_host_key; // Maps to "dhchap_key" in the C code
char *dhchap_key; // Maps to "dhchap_ctrl_key" in the C code
}
};
struct nvme_ns {
%immutable nsid;
%immutable eui64;
%immutable nguid;
%immutable uuid;
unsigned int nsid;
uint8_t eui64[8];
uint8_t nguid[16];
uint8_t uuid[16];
};
%extend nvme_global_ctx {
nvme_global_ctx(const char *config_file = NULL) {
struct nvme_global_ctx *ctx;
if (nvme_scan(config_file, &ctx))
return NULL;
return ctx;
}
~nvme_global_ctx() {
nvme_free_global_ctx($self);
}
struct nvme_global_ctx* __enter__() {
return $self;
}
struct nvme_global_ctx* __exit__(PyObject *type, PyObject *value, PyObject *traceback) {
return $self;
}
void log_level(const char *level) {
int log_level = DEFAULT_LOGLEVEL;
if (!strcmp(level, "debug")) log_level = LOG_DEBUG;
else if (!strcmp(level, "info")) log_level = LOG_INFO;
else if (!strcmp(level, "notice")) log_level = LOG_NOTICE;
else if (!strcmp(level, "warning")) log_level = LOG_WARNING;
else if (!strcmp(level, "err")) log_level = LOG_ERR;
else if (!strcmp(level, "crit")) log_level = LOG_CRIT;
else if (!strcmp(level, "alert")) log_level = LOG_ALERT;
else if (!strcmp(level, "emerg")) log_level = LOG_EMERG;
nvme_init_logging($self, log_level, false, false);
}
%pythoncode %{
def hosts(self):
"""Iterator over all host objects"""
h = nvme_first_host(self)
while h:
yield h
h = nvme_next_host(self, h)
%}
void refresh_topology() {
nvme_refresh_topology($self);
}
void dump_config() {
nvme_dump_config($self, NULL);
}
}
%define SET_SYMNAME_DOCSTRING
"@brief Set or Clear Host's Symbolic Name
@param hostsymname: A symbolic name, or None to clear the symbolic name.
@type hostsymname: str|None
@return: None"
%enddef
%pythonappend nvme_host::nvme_host(struct nvme_global_ctx *ctx,
const char *hostnqn,
const char *hostid,
const char *hostkey,
const char *hostsymname) {
self.__parent = ctx # Keep a reference to parent to ensure garbage collection happens in the right order}
%extend nvme_host {
nvme_host(struct nvme_global_ctx *ctx,
const char *hostnqn = NULL,
const char *hostid = NULL,
const char *hostkey = NULL,
const char *hostsymname = NULL) {
nvme_host_t h;
if (nvme_host_get(ctx, hostnqn, hostid, &h))
return NULL;
if (hostsymname)
nvme_host_set_hostsymname(h, hostsymname);
if (hostkey)
nvme_host_set_dhchap_key(h, hostkey);
return h;
}
~nvme_host() {
nvme_free_host($self);
}
struct nvme_host* __enter__() {
return $self;
}
struct nvme_host* __exit__(PyObject *type, PyObject *value, PyObject *traceback) {
return $self;
}
%feature("autodoc", SET_SYMNAME_DOCSTRING) set_symname;
void set_symname(const char *hostsymname) {
nvme_host_set_hostsymname($self, hostsymname);
}
PyObject* __str__() {
return PyUnicode_FromFormat("nvme.host(%s,%s)", STR_OR_NONE($self->hostnqn), STR_OR_NONE($self->hostid));
}
%pythoncode %{
def subsystems(self):
"""Iterator over all subsystem objects"""
s = nvme_first_subsystem(self)
while s:
yield s
s = nvme_next_subsystem(self, s)
%}
}
%{
const char *nvme_host_dhchap_key_get(struct nvme_host *h) {
return nvme_host_get_dhchap_key(h);
}
void nvme_host_dhchap_key_set(struct nvme_host *h, char *key) {
nvme_host_set_dhchap_key(h, key);
}
%};
%pythonappend nvme_subsystem::nvme_subsystem(struct nvme_global_ctx *ctx,
struct nvme_host *host,
const char *subsysnqn,
const char *name) {
self.__parent = host # Keep a reference to parent to ensure garbage collection happens in the right order}
%extend nvme_subsystem {
nvme_subsystem(struct nvme_global_ctx *ctx,
struct nvme_host *host,
const char *subsysnqn,
const char *name = NULL) {
struct nvme_subsystem *s;
if (nvme_subsystem_get(ctx, host, name, subsysnqn, &s))
return NULL;
return s;
}
~nvme_subsystem() {
nvme_free_subsystem($self);
}
struct nvme_subsystem* __enter__() {
return $self;
}
struct nvme_subsystem* __exit__(PyObject *type, PyObject *value, PyObject *traceback) {
return $self;
}
PyObject *__str__() {
return PyUnicode_FromFormat("nvme.subsystem(%s,%s)", STR_OR_NONE($self->name), STR_OR_NONE($self->subsysnqn));
}
%pythoncode %{
def controllers(self):
"""Iterator over all controller objects"""
c = nvme_subsystem_first_ctrl(self)
while c:
yield c
c = nvme_subsystem_next_ctrl(self, c)
%}
%pythoncode %{
def namespaces(self):
"""Iterator over all namespace objects"""
ns = nvme_subsystem_first_ns(self)
while ns:
yield ns
ns = nvme_subsystem_next_ns(self, ns)
%}
%immutable name;
const char *name;
%immutable host;
struct nvme_host *host;
}
%{
const char *nvme_subsystem_name_get(struct nvme_subsystem *s) {
return nvme_subsystem_get_name(s);
}
struct nvme_host *nvme_subsystem_host_get(struct nvme_subsystem *s) {
return nvme_subsystem_get_host(s);
}
const char *nvme_subsystem_sysfs_dir_get(struct nvme_subsystem *s) {
return nvme_subsystem_get_sysfs_dir(s);
}
const char *nvme_subsystem_iopolicy_get(struct nvme_subsystem *s) {
return nvme_subsystem_get_iopolicy(s);
}
const char *nvme_subsystem_application_get(struct nvme_subsystem *s) {
return nvme_subsystem_get_application(s);
}
void nvme_subsystem_application_set(struct nvme_subsystem *s, const char *a) {
nvme_subsystem_set_application(s, a);
}
%};
%pythonappend nvme_ctrl::connect(struct nvme_host *h,
struct nvme_fabrics_config *cfg) {
self.__host = h # Keep a reference to parent to ensure ctrl obj gets GCed before host}
%pythonappend nvme_ctrl::init(struct nvme_host *h, int instance) {
self.__host = h # Keep a reference to parent to ensure ctrl obj gets GCed before host}
%extend nvme_ctrl {
nvme_ctrl(struct nvme_global_ctx *ctx,
const char *subsysnqn,
const char *transport,
const char *traddr = NULL,
const char *host_traddr = NULL,
const char *host_iface = NULL,
const char *trsvcid = NULL) {
struct nvme_ctrl *c;
if (nvme_create_ctrl(ctx, subsysnqn, transport, traddr,
host_traddr, host_iface, trsvcid, &c))
return NULL;
return c;
}
~nvme_ctrl() {
nvme_free_ctrl($self);
}
struct nvme_ctrl* __enter__() {
return $self;
}
struct nvme_ctrl* __exit__(PyObject *type, PyObject *value, PyObject *traceback) {
if (nvme_ctrl_get_name($self))
nvme_disconnect_ctrl($self);
return $self;
}
%pythoncode %{
def discovery_ctrl_set(self, discovery: bool):
r"""DEPRECATED METHOD: Use property setter instead (e.g. ctrl.discovery_ctrl = True)"""
import warnings
warnings.warn("Use property setter instead (e.g. ctrl_obj.discovery_ctrl = True)", DeprecationWarning, stacklevel=2)
return _nvme.ctrl_discovery_ctrl_set(self, discovery)
%}
bool init(struct nvme_host *h, int instance) {
return nvme_init_ctrl(h, $self, instance) == 0;
}
void connect(struct nvme_host *h,
struct nvme_fabrics_config *cfg = NULL) {
int ret;
const char *dev;
dev = nvme_ctrl_get_name($self);
if (dev && !cfg->duplicate_connect) {
connect_err = -ENVME_CONNECT_ALREADY;
return;
}
Py_BEGIN_ALLOW_THREADS /* Release Python GIL */
ret = nvmf_add_ctrl(h, $self, cfg);
Py_END_ALLOW_THREADS /* Reacquire Python GIL */
if (ret) {
connect_err = ret;
return;
}
}
bool connected() {
return nvme_ctrl_get_name($self) != NULL;
}
%pythoncode %{
def persistent_set(self, persistent: bool):
r"""DEPRECATED METHOD: Use property setter instead (e.g. ctrl.persistent = True)"""
import warnings
warnings.warn("Use property setter instead (e.g. ctrl_obj.persistent = True)", DeprecationWarning, stacklevel=2)
return _nvme.ctrl_persistent_set(self, persistent)
%}
void rescan() {
nvme_rescan_ctrl($self);
}
void disconnect() {
int ret;
const char *dev;
dev = nvme_ctrl_get_name($self);
if (!dev) {
connect_err = 1;
return;
}
Py_BEGIN_ALLOW_THREADS /* Release Python GIL */
ret = nvme_disconnect_ctrl($self);
Py_END_ALLOW_THREADS /* Reacquire Python GIL */
if (ret < 0)
connect_err = 2;
}
%feature("autodoc", "@return: True if controller supports explicit registration. False otherwise.") is_registration_supported;
bool is_registration_supported() {
return nvmf_is_registration_supported($self);
}
%feature("autodoc", "@return None on success or Error string on error.") registration_ctlr;
PyObject *registration_ctlr(enum nvmf_dim_tas tas) {
__u32 result;
int status;
Py_BEGIN_ALLOW_THREADS /* Release Python GIL */
status = nvmf_register_ctrl($self, NVMF_DIM_TAS_REGISTER, &result);
Py_END_ALLOW_THREADS /* Reacquire Python GIL */
if (status != NVME_SC_SUCCESS) {
/* On error, return an error message */
return (status < 0) ?
PyUnicode_FromFormat("Status:0x%04x - %s", status, nvme_status_to_string(status, false)) :
PyUnicode_FromFormat("Result:0x%04x, Status:0x%04x - %s", result, status, nvme_status_to_string(status, false));
}
/* On success, return None */
Py_RETURN_NONE;
}
%newobject discover;
struct nvmf_discovery_log *discover(int lsp = 0, int max_retries = 6) {
const char *dev;
struct nvmf_discovery_log *logp = NULL;
struct nvme_get_discovery_args args = {
.c = $self,
.args_size = sizeof(args),
.max_retries = max_retries,
.result = NULL,
.timeout = NVME_DEFAULT_IOCTL_TIMEOUT,
.lsp = lsp,
};
dev = nvme_ctrl_get_name($self);
if (!dev) {
discover_err = 1;
return NULL;
}
Py_BEGIN_ALLOW_THREADS /* Release Python GIL */
discover_err = nvmf_get_discovery_wargs(&args, &logp);
Py_END_ALLOW_THREADS /* Reacquire Python GIL */
if (logp == NULL) discover_err = 2;
return logp;
}
%feature("autodoc", "@return: List of supported log pages") supported_log_pages;
PyObject *supported_log_pages(bool rae = true) {
struct nvme_supported_log_pages log;
struct nvme_passthru_cmd cmd;
PyObject *obj = NULL;
int ret = 0;
Py_BEGIN_ALLOW_THREADS /* Release Python GIL */
nvme_init_get_log_supported_log_pages(&cmd, NVME_CSI_NVM, &log);
ret = nvme_get_log(nvme_ctrl_get_transport_handle($self), &cmd, rae, NVME_LOG_PAGE_PDU_SIZE);
Py_END_ALLOW_THREADS /* Reacquire Python GIL */
if (ret) {
Py_RETURN_NONE;
}
obj = PyList_New(NVME_LOG_SUPPORTED_LOG_PAGES_MAX);
if (!obj) Py_RETURN_NONE;
for (int i = 0; i < NVME_LOG_SUPPORTED_LOG_PAGES_MAX; i++)
PyList_SetItem(obj, i, PyLong_FromLong(le32_to_cpu(log.lid_support[i]))); /* steals ref. to object - no need to decref */
return obj;
}
PyObject* __str__() {
return $self->address ?
PyUnicode_FromFormat("nvme_ctrl(transport=%s,%s)", STR_OR_NONE($self->transport), STR_OR_NONE($self->address)) :
PyUnicode_FromFormat("nvme_ctrl(transport=%s)", STR_OR_NONE($self->transport));
}
%pythoncode %{
def namespaces(self):
"""Iterator over all namespace objects"""
ns = nvme_ctrl_first_ns(self)
while ns:
yield ns
ns = nvme_ctrl_next_ns(self, ns)
%}
}
%{
/**********************************************************************
* SWIG automatically generates getter and setter methods using
* the syntax: [class]_[member]_[get|set]. These need to be mapped
* to the matching methods in libnvme (i.e. those that are defined
* publicly in libnvme.map). Typically, we get the following mapping:
*
* SWIG libnvme.map
* ====================== =======================
* nvme_ctrl_[member]_get -> nvme_ctrl_get_[member]
* nvme_ctrl_[member]_set -> nvme_ctrl_set_[member]
*
*/
const char *nvme_ctrl_name_get(struct nvme_ctrl *c) {
return nvme_ctrl_get_name(c);
}
struct nvme_subsystem *nvme_ctrl_subsystem_get(struct nvme_ctrl *c) {
return nvme_ctrl_get_subsystem(c);
}
const char *nvme_ctrl_state_get(struct nvme_ctrl *c) {
return nvme_ctrl_get_state(c);
}
const char *nvme_ctrl_dhchap_key_get(struct nvme_ctrl *c) {
return nvme_ctrl_get_dhchap_key(c);
}
void nvme_ctrl_dhchap_key_set(struct nvme_ctrl *c, const char *key) {
nvme_ctrl_set_dhchap_key(c, key);
}
const char *nvme_ctrl_dhchap_host_key_get(struct nvme_ctrl *c) {
return nvme_ctrl_get_dhchap_host_key(c);
}
void nvme_ctrl_dhchap_host_key_set(struct nvme_ctrl *c, const char *key) {
nvme_ctrl_set_dhchap_host_key(c, key);
}
const char *nvme_ctrl_cntlid_get(nvme_ctrl_t c) {
return nvme_ctrl_get_cntlid(c);
}
bool nvme_ctrl_persistent_get(struct nvme_ctrl *c) {
return nvme_ctrl_is_persistent(c);
}
void nvme_ctrl_persistent_set(struct nvme_ctrl *c, bool persistent) {
nvme_ctrl_set_persistent(c, persistent);
}
const char *nvme_ctrl_phy_slot_get(nvme_ctrl_t c) {
return nvme_ctrl_get_phy_slot(c);
}
const char *nvme_ctrl_trsvcid_get(nvme_ctrl_t c) {
return nvme_ctrl_get_trsvcid(c);
}
const char *nvme_ctrl_traddr_get(nvme_ctrl_t c) {
return nvme_ctrl_get_traddr(c);
}
const char *nvme_ctrl_subsysnqn_get(nvme_ctrl_t c) {
return nvme_ctrl_get_subsysnqn(c);
}
const char *nvme_ctrl_transport_get(nvme_ctrl_t c) {
return nvme_ctrl_get_transport(c);
}
const char *nvme_ctrl_sqsize_get(nvme_ctrl_t c) {
return nvme_ctrl_get_sqsize(c);
}
const char *nvme_ctrl_serial_get(nvme_ctrl_t c) {
return nvme_ctrl_get_serial(c);
}
const char *nvme_ctrl_queue_count_get(nvme_ctrl_t c) {
return nvme_ctrl_get_queue_count(c);
}
const char *nvme_ctrl_numa_node_get(nvme_ctrl_t c) {
return nvme_ctrl_get_numa_node(c);
}
const char *nvme_ctrl_model_get(nvme_ctrl_t c) {
return nvme_ctrl_get_model(c);
}
const char *nvme_ctrl_firmware_get(nvme_ctrl_t c) {
return nvme_ctrl_get_firmware(c);
}
const char *nvme_ctrl_address_get(nvme_ctrl_t c) {
return nvme_ctrl_get_address(c);
}
const char *nvme_ctrl_sysfs_dir_get(nvme_ctrl_t c) {
return nvme_ctrl_get_sysfs_dir(c);
}
bool nvme_ctrl_discovery_ctrl_get(struct nvme_ctrl *c) {
return nvme_ctrl_is_discovery_ctrl(c);
}
void nvme_ctrl_discovery_ctrl_set(struct nvme_ctrl *c, bool discovery) {
nvme_ctrl_set_discovery_ctrl(c, discovery);
}
bool nvme_ctrl_unique_discovery_ctrl_get(nvme_ctrl_t c) {
return nvme_ctrl_is_unique_discovery_ctrl(c);
}
void nvme_ctrl_unique_discovery_ctrl_set(nvme_ctrl_t c, bool unique) {
nvme_ctrl_set_unique_discovery_ctrl(c, unique);
}
const char *nvme_ctrl_keyring_get(nvme_ctrl_t c) {
return nvme_ctrl_get_keyring(c);
}
void nvme_ctrl_keyring_set(nvme_ctrl_t c, const char *keyring) {
nvme_ctrl_set_keyring(c, keyring);
}
const char *nvme_ctrl_tls_key_identity_get(nvme_ctrl_t c) {
return nvme_ctrl_get_tls_key_identity(c);
}
void nvme_ctrl_tls_key_identity_set(nvme_ctrl_t c, const char *identity) {
nvme_ctrl_set_tls_key_identity(c, identity);
}
const char *nvme_ctrl_tls_key_get(nvme_ctrl_t c) {
return nvme_ctrl_get_tls_key(c);
}
void nvme_ctrl_tls_key_set(nvme_ctrl_t c, const char *key) {
nvme_ctrl_set_tls_key(c, key);
}
%}
%pythonappend nvme_ns::nvme_ns(struct nvme_subsystem *s,
unsigned int nsid) {
self.__parent = s # Keep a reference to parent to ensure garbage collection happens in the right order}
%extend nvme_ns {
nvme_ns(struct nvme_subsystem *s,
unsigned int nsid) {
return nvme_subsystem_lookup_namespace(s, nsid);