forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfabrics.c
More file actions
1097 lines (917 loc) · 29.4 KB
/
fabrics.c
File metadata and controls
1097 lines (917 loc) · 29.4 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: GPL-2.0-only
/*
* Copyright (C) 2016 Intel Corporation. All rights reserved.
* Copyright (c) 2016 HGST, a Western Digital Company.
* Copyright (c) 2016 Samsung Electronics Co., Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version
* 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* This file implements the discovery controller feature of NVMe over
* Fabrics specification standard.
*/
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <inttypes.h>
#include <libgen.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include <linux/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <libnvme.h>
#include "common.h"
#include "nvme.h"
#include "nvme-print.h"
#include "fabrics.h"
#include "util/cleanup.h"
#include "logging.h"
#include "util/sighdl.h"
#define PATH_NVMF_DISC SYSCONFDIR "/nvme/discovery.conf"
#define PATH_NVMF_CONFIG SYSCONFDIR "/nvme/config.json"
#define PATH_NVMF_RUNDIR RUNDIR "/nvme"
#define MAX_DISC_ARGS 32
#define MAX_DISC_RETRIES 10
#define NVMF_DEF_DISC_TMO 30
/* Name of file to output log pages in their raw format */
static char *raw;
static bool persistent;
static bool quiet;
static bool dump_config;
static const char *nvmf_tport = "transport type";
static const char *nvmf_traddr = "transport address";
static const char *nvmf_nqn = "subsystem nqn";
static const char *nvmf_trsvcid = "transport service id (e.g. IP port)";
static const char *nvmf_htraddr = "host traddr (e.g. FC WWN's)";
static const char *nvmf_hiface = "host interface (for tcp transport)";
static const char *nvmf_hostnqn = "user-defined hostnqn";
static const char *nvmf_hostid = "user-defined hostid (if default not used)";
static const char *nvmf_hostkey = "user-defined dhchap key (if default not used)";
static const char *nvmf_ctrlkey = "user-defined dhchap controller key (for bi-directional authentication)";
static const char *nvmf_nr_io_queues = "number of io queues to use (default is core count)";
static const char *nvmf_nr_write_queues = "number of write queues to use (default 0)";
static const char *nvmf_nr_poll_queues = "number of poll queues to use (default 0)";
static const char *nvmf_queue_size = "number of io queue elements to use (default 128)";
static const char *nvmf_keep_alive_tmo = "keep alive timeout period in seconds";
static const char *nvmf_reconnect_delay = "reconnect timeout period in seconds";
static const char *nvmf_ctrl_loss_tmo = "controller loss timeout period in seconds";
static const char *nvmf_fast_io_fail_tmo = "fast I/O fail timeout (default off)";
static const char *nvmf_tos = "type of service";
static const char *nvmf_keyring = "Keyring for TLS key lookup (key id or keyring name)";
static const char *nvmf_tls_key = "TLS key to use (key id or key in interchange format)";
static const char *nvmf_tls_key_legacy = "TLS key to use (key id)";
static const char *nvmf_tls_key_identity = "TLS key identity";
static const char *nvmf_dup_connect = "allow duplicate connections between same transport host and subsystem port";
static const char *nvmf_disable_sqflow = "disable controller sq flow control (default false)";
static const char *nvmf_hdr_digest = "enable transport protocol header digest (TCP transport)";
static const char *nvmf_data_digest = "enable transport protocol data digest (TCP transport)";
static const char *nvmf_tls = "enable TLS";
static const char *nvmf_concat = "enable secure concatenation";
static const char *nvmf_config_file = "Use specified JSON configuration file or 'none' to disable";
static const char *nvmf_context = "execution context identification string";
struct nvmf_args {
const char *subsysnqn;
const char *transport;
const char *traddr;
const char *host_traddr;
const char *host_iface;
const char *trsvcid;
const char *hostnqn;
const char *hostid;
const char *hostkey;
const char *ctrlkey;
const char *keyring;
const char *tls_key;
const char *tls_key_identity;
};
#define NVMF_ARGS(n, f, c, ...) \
NVME_ARGS(n, \
OPT_STRING("transport", 't', "STR", &f.transport, nvmf_tport), \
OPT_STRING("nqn", 'n', "STR", &f.subsysnqn, nvmf_nqn), \
OPT_STRING("traddr", 'a', "STR", &f.traddr, nvmf_traddr), \
OPT_STRING("trsvcid", 's', "STR", &f.trsvcid, nvmf_trsvcid), \
OPT_STRING("host-traddr", 'w', "STR", &f.host_traddr, nvmf_htraddr), \
OPT_STRING("host-iface", 'f', "STR", &f.host_iface, nvmf_hiface), \
OPT_STRING("hostnqn", 'q', "STR", &f.hostnqn, nvmf_hostnqn), \
OPT_STRING("hostid", 'I', "STR", &f.hostid, nvmf_hostid), \
OPT_STRING("dhchap-secret", 'S', "STR", &f.hostkey, nvmf_hostkey), \
OPT_STRING("dhchap-ctrl-secret", 'C', "STR", &f.ctrlkey, nvmf_ctrlkey), \
OPT_STRING("keyring", 0, "STR", &f.keyring, nvmf_keyring), \
OPT_STRING("tls-key", 0, "STR", &f.tls_key, nvmf_tls_key), \
OPT_STRING("tls-key-identity", 0, "STR", &f.tls_key_identity, nvmf_tls_key_identity), \
OPT_INT("nr-io-queues", 'i', &c.nr_io_queues, nvmf_nr_io_queues), \
OPT_INT("nr-write-queues", 'W', &c.nr_write_queues, nvmf_nr_write_queues), \
OPT_INT("nr-poll-queues", 'P', &c.nr_poll_queues, nvmf_nr_poll_queues), \
OPT_INT("queue-size", 'Q', &c.queue_size, nvmf_queue_size), \
OPT_INT("keep-alive-tmo", 'k', &c.keep_alive_tmo, nvmf_keep_alive_tmo), \
OPT_INT("reconnect-delay", 'c', &c.reconnect_delay, nvmf_reconnect_delay), \
OPT_INT("ctrl-loss-tmo", 'l', &c.ctrl_loss_tmo, nvmf_ctrl_loss_tmo), \
OPT_INT("fast_io_fail_tmo", 'F', &c.fast_io_fail_tmo, nvmf_fast_io_fail_tmo),\
OPT_INT("tos", 'T', &c.tos, nvmf_tos), \
OPT_INT("tls_key", 0, &c.tls_key, nvmf_tls_key_legacy), \
OPT_FLAG("duplicate-connect", 'D', &c.duplicate_connect, nvmf_dup_connect), \
OPT_FLAG("disable-sqflow", 0, &c.disable_sqflow, nvmf_disable_sqflow), \
OPT_FLAG("hdr-digest", 'g', &c.hdr_digest, nvmf_hdr_digest), \
OPT_FLAG("data-digest", 'G', &c.data_digest, nvmf_data_digest), \
OPT_FLAG("tls", 0, &c.tls, nvmf_tls), \
OPT_FLAG("concat", 0, &c.concat, nvmf_concat), \
##__VA_ARGS__ \
)
static void save_discovery_log(char *raw, struct nvmf_discovery_log *log)
{
uint64_t numrec = le64_to_cpu(log->numrec);
int fd, len, ret;
fd = open(raw, O_CREAT | O_RDWR | O_TRUNC, 0600);
if (fd < 0) {
fprintf(stderr, "failed to open %s: %s\n", raw, libnvme_strerror(errno));
return;
}
len = sizeof(struct nvmf_discovery_log) + numrec * sizeof(struct nvmf_disc_log_entry);
ret = write(fd, log, len);
if (ret < 0)
fprintf(stderr, "failed to write to %s: %s\n",
raw, libnvme_strerror(errno));
else
printf("Discovery log is saved to %s\n", raw);
close(fd);
}
static int setup_common_context(struct libnvmf_context *fctx,
struct nvmf_args *fa);
struct cb_fabrics_data {
struct libnvme_fabrics_config *cfg;
nvme_print_flags_t flags;
bool quiet;
char *raw;
char **argv;
FILE *f;
};
static bool cb_decide_retry(struct libnvmf_context *fctx, int err,
void *user_data)
{
if (err == -EAGAIN || (err == -EINTR && !nvme_sigint_received)) {
print_debug("libnvmf_add_ctrl returned '%s'\n", libnvme_strerror(-err));
return true;
}
return false;
}
static void cb_connected(struct libnvmf_context *fctx,
struct libnvme_ctrl *c, void *user_data)
{
struct cb_fabrics_data *cfd = user_data;
if (cfd->quiet)
return;
if (cfd->flags == NORMAL) {
printf("connecting to device: %s\n", libnvme_ctrl_get_name(c));
return;
}
#ifdef CONFIG_JSONC
if (cfd->flags == JSON) {
struct json_object *root;
root = json_create_object();
json_object_add_value_string(root, "device",
libnvme_ctrl_get_name(c));
json_print_object(root, NULL);
printf("\n");
json_free_object(root);
}
#endif
}
static void cb_already_connected(struct libnvmf_context *fctx,
struct libnvme_host *host, const char *subsysnqn,
const char *transport, const char *traddr,
const char *trsvcid, void *user_data)
{
if (quiet)
return;
fprintf(stderr, "already connected to hostnqn=%s,nqn=%s,transport=%s,traddr=%s,trsvcid=%s\n",
libnvme_host_get_hostnqn(host), subsysnqn,
transport, traddr, trsvcid);
}
static void cb_discovery_log(struct libnvmf_context *fctx,
bool connect, struct nvmf_discovery_log *log,
uint64_t numrec, void *user_data)
{
struct cb_fabrics_data *cfd = user_data;
if (cfd->raw)
save_discovery_log(cfd->raw, log);
else if (!connect)
nvme_show_discovery_log(log, numrec, cfd->flags);
}
static int cb_parser_init(struct libnvmf_context *dctx, void *user_data)
{
struct cb_fabrics_data *cfd = user_data;
cfd->f = fopen(PATH_NVMF_DISC, "r");
if (cfd->f == NULL) {
fprintf(stderr, "No params given and no %s\n", PATH_NVMF_DISC);
return -ENOENT;
}
cfd->argv = calloc(MAX_DISC_ARGS, sizeof(char *));
if (!cfd->argv)
return -1;
cfd->argv[0] = "discover";
return 0;
}
static void cb_parser_cleanup(struct libnvmf_context *fctx, void *user_data)
{
struct cb_fabrics_data *cfd = user_data;
free(cfd->argv);
fclose(cfd->f);
}
static int cb_parser_next_line(struct libnvmf_context *fctx, void *user_data)
{
struct cb_fabrics_data *cfd = user_data;
struct libnvme_fabrics_config cfg;
struct nvmf_args fa = {};
char *ptr, *p, line[4096];
int argc, ret = 0;
bool force = false;
NVMF_ARGS(opts, fa, cfg,
OPT_FLAG("persistent", 'p', &persistent, "persistent discovery connection"),
OPT_FLAG("force", 0, &force, "Force persistent discovery controller creation"));
memcpy(&cfg, cfd->cfg, sizeof(cfg));
next:
if (fgets(line, sizeof(line), cfd->f) == NULL)
return -EOF;
if (line[0] == '#' || line[0] == '\n')
goto next;
argc = 1;
p = line;
while ((ptr = strsep(&p, " =\n")) != NULL)
cfd->argv[argc++] = ptr;
cfd->argv[argc] = NULL;
fa.subsysnqn = NVME_DISC_SUBSYS_NAME;
ret = argconfig_parse(argc, cfd->argv, "config", opts);
if (ret)
goto next;
if (!fa.transport && !fa.traddr)
goto next;
if (!fa.trsvcid)
fa.trsvcid = libnvmf_get_default_trsvcid(fa.transport, true);
ret = setup_common_context(fctx, &fa);
if (ret)
return ret;
ret = libnvmf_context_set_fabrics_config(fctx, &cfg);
if (ret)
return ret;
return 0;
}
static int setup_common_context(struct libnvmf_context *fctx,
struct nvmf_args *fa)
{
int err;
err = libnvmf_context_set_connection(fctx,
fa->subsysnqn, fa->transport,
fa->traddr, fa->trsvcid,
fa->host_traddr, fa->host_iface);
if (err)
return err;
err = libnvmf_context_set_hostnqn(fctx,
fa->hostnqn, fa->hostid);
if (err)
return err;
err = libnvmf_context_set_crypto(fctx,
fa->hostkey, fa->ctrlkey,
fa->keyring, fa->tls_key,
fa->tls_key_identity);
if (err)
return err;
return 0;
}
static int create_common_context(struct libnvme_global_ctx *ctx,
bool persistent, struct nvmf_args *fa,
struct libnvme_fabrics_config *cfg,
void *user_data, struct libnvmf_context **fctxp)
{
struct libnvmf_context *fctx;
int err;
err = libnvmf_context_create(ctx, cb_decide_retry, cb_connected,
cb_already_connected, user_data, &fctx);
if (err)
return err;
err = libnvmf_context_set_connection(fctx, fa->subsysnqn,
fa->transport, fa->traddr, fa->trsvcid,
fa->host_traddr, fa->host_iface);
if (err)
goto err;
err = libnvmf_context_set_hostnqn(fctx, fa->hostnqn, fa->hostid);
if (err)
goto err;
err = libnvmf_context_set_fabrics_config(fctx, cfg);
if (err)
goto err;
err = libnvmf_context_set_crypto(fctx, fa->hostkey, fa->ctrlkey,
fa->keyring, fa->tls_key, fa->tls_key_identity);
if (err)
goto err;
err = libnvmf_context_set_persistent(fctx, persistent);
if (err)
goto err;
*fctxp = fctx;
return 0;
err:
libnvmf_context_free(fctx);
return err;
}
static int create_discovery_context(struct libnvme_global_ctx *ctx,
bool persistent, const char *device,
struct nvmf_args *fa,
struct libnvme_fabrics_config *cfg,
void *user_data, struct libnvmf_context **fctxp)
{
struct libnvmf_context *fctx;
int err;
err = create_common_context(ctx, persistent, fa, cfg, user_data,
&fctx);
if (err)
return err;
err = libnvmf_context_set_discovery_cbs(fctx, cb_discovery_log,
cb_parser_init, cb_parser_cleanup, cb_parser_next_line);
if (err)
goto err;
err = libnvmf_context_set_discovery_defaults(fctx, MAX_DISC_RETRIES,
NVMF_DEF_DISC_TMO);
if (err)
goto err;
err = libnvmf_context_set_device(fctx, device);
if (err)
goto err;
*fctxp = fctx;
return 0;
err:
libnvmf_context_free(fctx);
return err;
}
static int nvme_read_volatile_config(struct libnvme_global_ctx *ctx)
{
char *filename, *ext;
struct dirent *dir;
DIR *d;
int ret = -ENOENT;
d = opendir(PATH_NVMF_RUNDIR);
if (!d)
return -ENOTDIR;
while ((dir = readdir(d))) {
if (dir->d_type != DT_REG)
continue;
ext = strchr(dir->d_name, '.');
if (!ext || strcmp("json", ext + 1))
continue;
if (asprintf(&filename, "%s/%s", PATH_NVMF_RUNDIR, dir->d_name) < 0) {
ret = -ENOMEM;
break;
}
if (libnvme_read_config(ctx, filename))
ret = 0;
free(filename);
}
closedir(d);
return ret;
}
static int nvme_read_config_checked(struct libnvme_global_ctx *ctx,
const char *filename)
{
if (access(filename, F_OK))
return -errno;
return libnvme_read_config(ctx, filename);
}
#define NBFT_SYSFS_PATH "/sys/firmware/acpi/tables"
int fabrics_discovery(const char *desc, int argc, char **argv, bool connect)
{
char *config_file = PATH_NVMF_CONFIG;
char *context = NULL;
nvme_print_flags_t flags;
_cleanup_nvme_global_ctx_ struct libnvme_global_ctx *ctx = NULL;
_cleanup_nvmf_context_ struct libnvmf_context *fctx = NULL;
int ret;
struct libnvme_fabrics_config cfg;
struct nvmf_args fa = { .subsysnqn = NVME_DISC_SUBSYS_NAME };
char *device = NULL;
bool force = false;
bool json_config = false;
bool nbft = false, nonbft = false;
char *nbft_path = NBFT_SYSFS_PATH;
NVMF_ARGS(opts, fa, cfg,
OPT_STRING("device", 'd', "DEV", &device, "use existing discovery controller device"),
OPT_FILE("raw", 'r', &raw, "save raw output to file"),
OPT_FLAG("persistent", 'p', &persistent, "persistent discovery connection"),
OPT_FLAG("quiet", 0, &quiet, "suppress already connected errors"),
OPT_STRING("config", 'J', "FILE", &config_file, nvmf_config_file),
OPT_FLAG("dump-config", 'O', &dump_config, "Dump configuration file to stdout"),
OPT_FLAG("force", 0, &force, "Force persistent discovery controller creation"),
OPT_FLAG("nbft", 0, &nbft, "Only look at NBFT tables"),
OPT_FLAG("no-nbft", 0, &nonbft, "Do not look at NBFT tables"),
OPT_STRING("nbft-path", 0, "STR", &nbft_path, "user-defined path for NBFT tables"),
OPT_STRING("context", 0, "STR", &context, nvmf_context));
libnvmf_default_config(&cfg);
ret = argconfig_parse(argc, argv, desc, opts);
if (ret)
return ret;
ret = validate_output_format(nvme_args.output_format, &flags);
if (ret < 0) {
nvme_show_error("Invalid output format");
return ret;
}
if (!strcmp(config_file, "none"))
config_file = NULL;
log_level = map_log_level(nvme_args.verbose, quiet);
ctx = libnvme_create_global_ctx(stderr, log_level);
if (!ctx) {
fprintf(stderr, "Failed to create topology root: %s\n",
libnvme_strerror(errno));
return -ENOMEM;
}
if (context)
libnvme_set_application(ctx, context);
if (!nvme_read_config_checked(ctx, config_file))
json_config = true;
if (!nvme_read_volatile_config(ctx))
json_config = true;
libnvme_skip_namespaces(ctx);
ret = libnvme_scan_topology(ctx, NULL, NULL);
if (ret < 0) {
fprintf(stderr, "Failed to scan topology: %s\n",
libnvme_strerror(-ret));
return ret;
}
if (device) {
if (!strcmp(device, "none"))
device = NULL;
else if (!strncmp(device, "/dev/", 5))
device += 5;
}
struct cb_fabrics_data dld = {
.cfg = &cfg,
.flags = flags,
.raw = raw,
};
ret = create_discovery_context(ctx, persistent, device, &fa,
&cfg, &dld, &fctx);
if (ret)
return ret;
if (!device && !fa.transport && !fa.traddr) {
if (!nonbft)
ret = libnvmf_discovery_nbft(ctx, fctx,
connect, nbft_path);
if (nbft)
goto out_free;
if (json_config)
ret = libnvmf_discovery_config_json(ctx, fctx,
connect, force);
if (ret || access(PATH_NVMF_DISC, F_OK))
goto out_free;
ret = libnvmf_discovery_config_file(ctx, fctx, connect, force);
goto out_free;
}
ret = libnvmf_discovery(ctx, fctx, connect, force);
out_free:
if (dump_config)
libnvme_dump_config(ctx, STDOUT_FILENO);
return ret;
}
int fabrics_connect(const char *desc, int argc, char **argv)
{
_cleanup_free_ char *hnqn = NULL;
_cleanup_free_ char *hid = NULL;
char *config_file = NULL;
char *context = NULL;
_cleanup_nvme_global_ctx_ struct libnvme_global_ctx *ctx = NULL;
_cleanup_nvmf_context_ struct libnvmf_context *fctx = NULL;
_cleanup_nvme_ctrl_ libnvme_ctrl_t c = NULL;
int ret;
nvme_print_flags_t flags;
struct libnvme_fabrics_config cfg = { 0 };
struct nvmf_args fa = { 0 };
NVMF_ARGS(opts, fa, cfg,
OPT_STRING("config", 'J', "FILE", &config_file, nvmf_config_file),
OPT_FLAG("dump-config", 'O', &dump_config, "Dump JSON configuration to stdout"),
OPT_STRING("context", 0, "STR", &context, nvmf_context));
libnvmf_default_config(&cfg);
ret = argconfig_parse(argc, argv, desc, opts);
if (ret)
return ret;
ret = validate_output_format(nvme_args.output_format, &flags);
if (ret < 0) {
nvme_show_error("Invalid output format");
return ret;
}
if (config_file && strcmp(config_file, "none"))
goto do_connect;
if (!fa.subsysnqn) {
fprintf(stderr,
"required argument [--nqn | -n] not specified\n");
return -EINVAL;
}
if (!fa.transport) {
fprintf(stderr,
"required argument [--transport | -t] not specified\n");
return -EINVAL;
}
if (strcmp(fa.transport, "loop")) {
if (!fa.traddr) {
fprintf(stderr,
"required argument [--traddr | -a] not specified for transport %s\n",
fa.transport);
return -EINVAL;
}
}
do_connect:
log_level = map_log_level(nvme_args.verbose, quiet);
ctx = libnvme_create_global_ctx(stderr, log_level);
if (!ctx) {
fprintf(stderr, "Failed to create topology root: %s\n",
libnvme_strerror(errno));
return -ENOMEM;
}
if (context)
libnvme_set_application(ctx, context);
libnvme_read_config(ctx, config_file);
nvme_read_volatile_config(ctx);
libnvme_skip_namespaces(ctx);
ret = libnvme_scan_topology(ctx, NULL, NULL);
if (ret < 0) {
fprintf(stderr, "Failed to scan topology: %s\n",
libnvme_strerror(-ret));
return ret;
}
struct cb_fabrics_data cfd = {
.flags = flags,
.quiet = dump_config,
.raw = raw,
};
ret = create_common_context(ctx, persistent, &fa,
&cfg, &cfd, &fctx);
if (ret)
return ret;
if (config_file)
return libnvmf_connect_config_json(ctx, fctx);
ret = libnvmf_connect(ctx, fctx);
if (ret) {
fprintf(stderr, "failed to connect: %s\n",
libnvme_strerror(-ret));
return ret;
}
if (dump_config)
libnvme_dump_config(ctx, STDOUT_FILENO);
return 0;
}
static libnvme_ctrl_t lookup_nvme_ctrl(struct libnvme_global_ctx *ctx,
const char *name)
{
libnvme_host_t h;
libnvme_subsystem_t s;
libnvme_ctrl_t c;
libnvme_for_each_host(ctx, h) {
libnvme_for_each_subsystem(h, s) {
libnvme_subsystem_for_each_ctrl(s, c) {
if (!strcmp(libnvme_ctrl_get_name(c), name))
return c;
}
}
}
return NULL;
}
static void nvmf_disconnect_nqn(struct libnvme_global_ctx *ctx, char *nqn)
{
int i = 0;
char *n = nqn;
char *p;
libnvme_host_t h;
libnvme_subsystem_t s;
libnvme_ctrl_t c;
while ((p = strsep(&n, ",")) != NULL) {
if (!strlen(p))
continue;
libnvme_for_each_host(ctx, h) {
libnvme_for_each_subsystem(h, s) {
if (strcmp(libnvme_subsystem_get_subsysnqn(s), p))
continue;
libnvme_subsystem_for_each_ctrl(s, c) {
if (!libnvme_disconnect_ctrl(c))
i++;
}
}
}
}
printf("NQN:%s disconnected %d controller(s)\n", nqn, i);
}
int fabrics_disconnect(const char *desc, int argc, char **argv)
{
const char *device = "nvme device handle";
_cleanup_nvme_global_ctx_ struct libnvme_global_ctx *ctx = NULL;
libnvme_ctrl_t c;
char *p;
int ret;
struct config {
char *nqn;
char *device;
};
struct config cfg = { 0 };
NVME_ARGS(opts,
OPT_STRING("nqn", 'n', "NAME", &cfg.nqn, nvmf_nqn),
OPT_STRING("device", 'd', "DEV", &cfg.device, device));
ret = argconfig_parse(argc, argv, desc, opts);
if (ret)
return ret;
if (cfg.nqn && cfg.device) {
fprintf(stderr,
"Both device name [--device | -d] and NQN [--nqn | -n] are specified\n");
return -EINVAL;
}
if (!cfg.nqn && !cfg.device) {
fprintf(stderr,
"Neither device name [--device | -d] nor NQN [--nqn | -n] provided\n");
return -EINVAL;
}
log_level = map_log_level(nvme_args.verbose, false);
ctx = libnvme_create_global_ctx(stderr, log_level);
if (!ctx) {
fprintf(stderr, "Failed to create topology root: %s\n",
libnvme_strerror(errno));
return -ENOMEM;
}
libnvme_skip_namespaces(ctx);
ret = libnvme_scan_topology(ctx, NULL, NULL);
if (ret < 0) {
/*
* Do not report an error when the modules are not
* loaded, this allows the user to unconditionally call
* disconnect.
*/
if (ret == -ENOENT)
return 0;
fprintf(stderr, "Failed to scan topology: %s\n",
libnvme_strerror(-ret));
return ret;
}
if (cfg.nqn)
nvmf_disconnect_nqn(ctx, cfg.nqn);
if (cfg.device) {
char *d;
d = cfg.device;
while ((p = strsep(&d, ",")) != NULL) {
if (!strncmp(p, "/dev/", 5))
p += 5;
c = lookup_nvme_ctrl(ctx, p);
if (!c) {
fprintf(stderr,
"Did not find device %s\n", p);
return -ENODEV;
}
ret = libnvme_disconnect_ctrl(c);
if (ret)
fprintf(stderr,
"Failed to disconnect %s: %s\n",
p, libnvme_strerror(-ret));
}
}
return 0;
}
int fabrics_disconnect_all(const char *desc, int argc, char **argv)
{
_cleanup_nvme_global_ctx_ struct libnvme_global_ctx *ctx = NULL;
libnvme_host_t h;
libnvme_subsystem_t s;
libnvme_ctrl_t c;
int ret;
struct config {
char *transport;
};
struct config cfg = { 0 };
NVME_ARGS(opts,
OPT_STRING("transport", 'r', "STR", (char *)&cfg.transport, nvmf_tport));
ret = argconfig_parse(argc, argv, desc, opts);
if (ret)
return ret;
log_level = map_log_level(nvme_args.verbose, false);
ctx = libnvme_create_global_ctx(stderr, log_level);
if (!ctx) {
fprintf(stderr, "Failed to create topology root: %s\n",
libnvme_strerror(errno));
return -ENOMEM;
}
libnvme_skip_namespaces(ctx);
ret = libnvme_scan_topology(ctx, NULL, NULL);
if (ret < 0) {
/*
* Do not report an error when the modules are not
* loaded, this allows the user to unconditionally call
* disconnect.
*/
if (ret == -ENOENT)
return 0;
fprintf(stderr, "Failed to scan topology: %s\n",
libnvme_strerror(-ret));
return ret;
}
libnvme_for_each_host(ctx, h) {
libnvme_for_each_subsystem(h, s) {
libnvme_subsystem_for_each_ctrl(s, c) {
if (cfg.transport &&
strcmp(cfg.transport,
libnvme_ctrl_get_transport(c)))
continue;
else if (!strcmp(libnvme_ctrl_get_transport(c),
"pcie"))
continue;
if (libnvme_disconnect_ctrl(c))
fprintf(stderr,
"failed to disconnect %s\n",
libnvme_ctrl_get_name(c));
}
}
}
return 0;
}
int fabrics_config(const char *desc, int argc, char **argv)
{
bool scan_tree = false, modify_config = false, update_config = false;
_cleanup_nvme_global_ctx_ struct libnvme_global_ctx *ctx = NULL;
char *config_file = PATH_NVMF_CONFIG;
struct libnvme_fabrics_config cfg;
struct nvmf_args fa = { };
int ret;
NVMF_ARGS(opts, fa, cfg,
OPT_STRING("config", 'J', "FILE", &config_file, nvmf_config_file),
OPT_FLAG("scan", 'R', &scan_tree, "Scan current NVMeoF topology"),
OPT_FLAG("modify", 'M', &modify_config, "Modify JSON configuration file"),
OPT_FLAG("dump", 'O', &dump_config, "Dump JSON configuration to stdout"),
OPT_FLAG("update", 'U', &update_config, "Update JSON configuration file"));
libnvmf_default_config(&cfg);
ret = argconfig_parse(argc, argv, desc, opts);
if (ret)
return ret;
if (!strcmp(config_file, "none"))
config_file = NULL;
log_level = map_log_level(nvme_args.verbose, quiet);
ctx = libnvme_create_global_ctx(stderr, log_level);
if (!ctx) {
fprintf(stderr, "Failed to create topology root: %s\n",
libnvme_strerror(errno));
return -ENOMEM;
}
libnvme_read_config(ctx, config_file);
if (scan_tree) {
libnvme_skip_namespaces(ctx);
ret = libnvme_scan_topology(ctx, NULL, NULL);
if (ret < 0) {
fprintf(stderr, "Failed to scan topology: %s\n",
libnvme_strerror(-ret));
return -ret;
}
}
if (modify_config) {
_cleanup_nvmf_context_ struct libnvmf_context *fctx = NULL;
if (!fa.subsysnqn) {
fprintf(stderr,
"required argument [--nqn | -n] needed with --modify\n");
return -EINVAL;
}
if (!fa.transport) {
fprintf(stderr,
"required argument [--transport | -t] needed with --modify\n");
return -EINVAL;
}
ret = create_common_context(ctx, persistent, &fa,
&cfg, NULL, &fctx);
if (ret)
return ret;
ret = libnvmf_config_modify(ctx, fctx);
if (ret) {
fprintf(stderr, "failed to update config\n");
return ret;
}
}
if (update_config) {
_cleanup_fd_ int fd = -1;
fd = open(config_file, O_RDONLY, 0);
if (fd != -1)
libnvme_dump_config(ctx, fd);
}
if (dump_config)
libnvme_dump_config(ctx, STDOUT_FILENO);
return 0;
}
static int dim_operation(libnvme_ctrl_t c, enum nvmf_dim_tas tas, const char *name)
{
static const char * const task[] = {
[NVMF_DIM_TAS_REGISTER] = "register",
[NVMF_DIM_TAS_DEREGISTER] = "deregister",
};
const char *t;
int status;
__u32 result;
t = (tas > NVMF_DIM_TAS_DEREGISTER || !task[tas]) ? "reserved" : task[tas];
status = libnvmf_register_ctrl(c, tas, &result);
if (status == NVME_SC_SUCCESS) {
printf("%s DIM %s command success\n", name, t);
} else if (status < NVME_SC_SUCCESS) {
fprintf(stderr, "%s DIM %s command error. Status:0x%04x - %s\n",
name, t, status, libnvme_status_to_string(status, false));
} else {
fprintf(stderr, "%s DIM %s command error. Result:0x%04x, Status:0x%04x - %s\n",
name, t, result, status, libnvme_status_to_string(status, false));
}
return libnvme_status_to_errno(status, true);
}
int fabrics_dim(const char *desc, int argc, char **argv)
{