forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 3
/
fabrics.c
1014 lines (859 loc) · 23.6 KB
/
fabrics.c
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
/*
* 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 <errno.h>
#include <getopt.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/ioctl.h>
#include <asm/byteorder.h>
#include <inttypes.h>
#include <linux/types.h>
#include <libgen.h>
#include <sys/stat.h>
#include <stddef.h>
#include "parser.h"
#include "nvme-ioctl.h"
#include "spdk-nvme.h"
#include "fabrics.h"
#include "nvme.h"
#include "argconfig.h"
#include "common.h"
#define NVMF_HOSTID_SIZE 36
static struct config cfg = { NULL };
#define BUF_SIZE 4096
#define PATH_NVME_FABRICS "/dev/nvme-fabrics"
#define PATH_NVMF_DISC "/etc/nvme/discovery.conf"
#define PATH_NVMF_HOSTNQN "/etc/nvme/hostnqn"
#define PATH_NVMF_HOSTID "/etc/nvme/hostid"
#define SYS_NVME "/sys/class/nvme"
#define MAX_DISC_ARGS 10
enum {
OPT_INSTANCE,
OPT_CNTLID,
OPT_ERR
};
static const match_table_t opt_tokens = {
{ OPT_INSTANCE, "instance=%d" },
{ OPT_CNTLID, "cntlid=%d" },
{ OPT_ERR, NULL },
};
static const char *arg_str(const char * const *strings,
size_t array_size, size_t idx)
{
if (idx < array_size && strings[idx])
return strings[idx];
return "unrecognized";
}
static const char * const trtypes[] = {
[NVMF_TRTYPE_RDMA] = "rdma",
[NVMF_TRTYPE_FC] = "fibre-channel",
[NVMF_TRTYPE_LOOP] = "loop",
};
static const char *trtype_str(__u8 trtype)
{
return arg_str(trtypes, ARRAY_SIZE(trtypes), trtype);
}
static const char * const adrfams[] = {
[NVMF_ADDR_FAMILY_PCI] = "pci",
[NVMF_ADDR_FAMILY_IP4] = "ipv4",
[NVMF_ADDR_FAMILY_IP6] = "ipv6",
[NVMF_ADDR_FAMILY_IB] = "infiniband",
[NVMF_ADDR_FAMILY_FC] = "fibre-channel",
};
static inline const char *adrfam_str(__u8 adrfam)
{
return arg_str(adrfams, ARRAY_SIZE(adrfams), adrfam);
}
static const char * const subtypes[] = {
[NVME_NQN_DISC] = "discovery subsystem",
[NVME_NQN_NVME] = "nvme subsystem",
};
static inline const char *subtype_str(__u8 subtype)
{
return arg_str(subtypes, ARRAY_SIZE(subtypes), subtype);
}
static const char * const treqs[] = {
[NVMF_TREQ_NOT_SPECIFIED] = "not specified",
[NVMF_TREQ_REQUIRED] = "required",
[NVMF_TREQ_NOT_REQUIRED] = "not required",
};
static inline const char *treq_str(__u8 treq)
{
return arg_str(treqs, ARRAY_SIZE(treqs), treq);
}
static const char * const prtypes[] = {
[NVMF_RDMA_PRTYPE_NOT_SPECIFIED] = "not specified",
[NVMF_RDMA_PRTYPE_IB] = "infiniband",
[NVMF_RDMA_PRTYPE_ROCE] = "roce",
[NVMF_RDMA_PRTYPE_ROCEV2] = "roce-v2",
[NVMF_RDMA_PRTYPE_IWARP] = "iwarp",
};
static inline const char *prtype_str(__u8 prtype)
{
return arg_str(prtypes, ARRAY_SIZE(prtypes), prtype);
}
static const char * const qptypes[] = {
[NVMF_RDMA_QPTYPE_CONNECTED] = "connected",
[NVMF_RDMA_QPTYPE_DATAGRAM] = "datagram",
};
static inline const char *qptype_str(__u8 qptype)
{
return arg_str(qptypes, ARRAY_SIZE(qptypes), qptype);
}
static const char * const cms[] = {
[NVMF_RDMA_CMS_RDMA_CM] = "rdma-cm",
};
static const char *cms_str(__u8 cm)
{
return arg_str(cms, ARRAY_SIZE(cms), cm);
}
static int do_discover(char *argstr, bool connect);
static int add_ctrl(const char *argstr)
{
substring_t args[MAX_OPT_ARGS];
char buf[BUF_SIZE], *options, *p;
int token, ret, fd, len = strlen(argstr);
fd = open(PATH_NVME_FABRICS, O_RDWR);
if (fd < 0) {
fprintf(stderr, "Failed to open %s: %s\n",
PATH_NVME_FABRICS, strerror(errno));
ret = -errno;
goto out;
}
if (write(fd, argstr, len) != len) {
fprintf(stderr, "Failed to write to %s: %s\n",
PATH_NVME_FABRICS, strerror(errno));
ret = -errno;
goto out_close;
}
len = read(fd, buf, BUF_SIZE);
if (len < 0) {
fprintf(stderr, "Failed to read from %s: %s\n",
PATH_NVME_FABRICS, strerror(errno));
ret = -errno;
goto out_close;
}
buf[len] = '\0';
options = buf;
while ((p = strsep(&options, ",\n")) != NULL) {
if (!*p)
continue;
token = match_token(p, opt_tokens, args);
switch (token) {
case OPT_INSTANCE:
if (match_int(args, &token))
goto out_fail;
ret = token;
goto out_close;
default:
/* ignore */
break;
}
}
out_fail:
fprintf(stderr, "Failed to parse ctrl info for \"%s\"\n", argstr);
ret = -EINVAL;
out_close:
close(fd);
out:
return ret;
}
static int remove_ctrl_by_path(char *sysfs_path)
{
int ret, fd;
fd = open(sysfs_path, O_WRONLY);
if (fd < 0) {
ret = errno;
goto out;
}
if (write(fd, "1", 1) != 1) {
ret = errno;
goto out_close;
}
ret = 0;
out_close:
close(fd);
out:
return ret;
}
static int remove_ctrl(int instance)
{
char *sysfs_path;
int ret;
if (asprintf(&sysfs_path, "/sys/class/nvme/nvme%d/delete_controller",
instance) < 0) {
ret = errno;
goto out;
}
ret = remove_ctrl_by_path(sysfs_path);
free(sysfs_path);
out:
return ret;
}
enum {
DISC_OK,
DISC_NO_LOG,
DISC_GET_NUMRECS,
DISC_GET_LOG,
DISC_NOT_EQUAL,
};
static int nvmf_get_log_page_discovery(const char *dev_path,
struct nvmf_disc_rsp_page_hdr **logp, int *numrec)
{
struct nvmf_disc_rsp_page_hdr *log;
unsigned int log_size = 0;
unsigned long genctr;
int error, fd;
fd = open(dev_path, O_RDWR);
if (fd < 0) {
error = -errno;
goto out;
}
/* first get_log_page we just need numrec entry from discovery hdr.
* host supplies its desired bytes via dwords, per NVMe spec.
*/
log_size = round_up((offsetof(struct nvmf_disc_rsp_page_hdr, numrec) +
sizeof(log->numrec)), sizeof(__u32));
/*
* Issue first get log page w/numdl small enough to retrieve numrec.
* We just want to know how many records to retrieve.
*/
log = calloc(1, log_size);
if (!log) {
error = -ENOMEM;
goto out_close;
}
error = nvme_discovery_log(fd, log, log_size);
if (error) {
error = DISC_GET_NUMRECS;
goto out_free_log;
}
/* check numrec limits */
*numrec = le64_to_cpu(log->numrec);
genctr = le64_to_cpu(log->genctr);
free(log);
if (*numrec == 0) {
error = DISC_NO_LOG;
goto out_close;
}
/* we are actually retrieving the entire discovery tables
* for the second get_log_page(), per
* NVMe spec so no need to round_up(), or there is something
* seriously wrong with the standard
*/
log_size = sizeof(struct nvmf_disc_rsp_page_hdr) +
sizeof(struct nvmf_disc_rsp_page_entry) * *numrec;
/* allocate discovery log pages based on page_hdr->numrec */
log = calloc(1, log_size);
if (!log) {
error = -ENOMEM;
goto out_close;
}
/*
* issue new get_log_page w/numdl+numdh set to get all records,
* up to MAX_DISC_LOGS.
*/
error = nvme_discovery_log(fd, log, log_size);
if (error) {
error = DISC_GET_LOG;
goto out_free_log;
}
if (*numrec != le32_to_cpu(log->numrec) || genctr != le64_to_cpu(log->genctr)) {
error = DISC_NOT_EQUAL;
goto out_free_log;
}
/* needs to be freed by the caller */
*logp = log;
goto out_close;
error = DISC_OK;
out_free_log:
free(log);
out_close:
close(fd);
out:
return error;
}
static int space_strip_len(int max, const char *str)
{
int i;
for (i = max - 1; i >= 0; i--)
if (str[i] != '\0' && str[i] != ' ')
break;
return i + 1;
}
static void print_discovery_log(struct nvmf_disc_rsp_page_hdr *log, int numrec)
{
int i;
printf("\nDiscovery Log Number of Records %d, "
"Generation counter %"PRIu64"\n",
numrec, (uint64_t)le64_to_cpu(log->genctr));
for (i = 0; i < numrec; i++) {
struct nvmf_disc_rsp_page_entry *e = &log->entries[i];
printf("=====Discovery Log Entry %d======\n", i);
printf("trtype: %s\n", trtype_str(e->trtype));
printf("adrfam: %s\n", adrfam_str(e->adrfam));
printf("subtype: %s\n", subtype_str(e->subtype));
printf("treq: %s\n", treq_str(e->treq));
printf("portid: %d\n", e->portid);
printf("trsvcid: %.*s\n",
space_strip_len(NVMF_TRSVCID_SIZE, e->trsvcid),
e->trsvcid);
printf("subnqn: %s\n", e->subnqn);
printf("traddr: %.*s\n",
space_strip_len(NVMF_TRADDR_SIZE, e->traddr),
e->traddr);
switch (e->trtype) {
case NVMF_TRTYPE_RDMA:
printf("rdma_prtype: %s\n",
prtype_str(e->tsas.rdma.prtype));
printf("rdma_qptype: %s\n",
qptype_str(e->tsas.rdma.qptype));
printf("rdma_cms: %s\n",
cms_str(e->tsas.rdma.cms));
printf("rdma_pkey: 0x%04x\n",
e->tsas.rdma.pkey);
break;
}
}
}
static void save_discovery_log(struct nvmf_disc_rsp_page_hdr *log, int numrec)
{
int fd;
int len, ret;
fd = open(cfg.raw, O_CREAT|O_RDWR|O_TRUNC, S_IRUSR|S_IWUSR);
if (fd < 0) {
fprintf(stderr, "failed to open %s: %s\n",
cfg.raw, strerror(errno));
return;
}
len = sizeof(struct nvmf_disc_rsp_page_hdr) +
numrec * sizeof(struct nvmf_disc_rsp_page_entry);
ret = write(fd, log, len);
if (ret < 0)
fprintf(stderr, "failed to write to %s: %s\n",
cfg.raw, strerror(errno));
else
printf("Discovery log is saved to %s\n", cfg.raw);
close(fd);
}
static int nvmf_hostnqn_file(void)
{
FILE *f;
char hostnqn[NVMF_NQN_SIZE];
int ret = false;
f = fopen(PATH_NVMF_HOSTNQN, "r");
if (f == NULL)
return false;
if (fgets(hostnqn, sizeof(hostnqn), f) == NULL)
goto out;
cfg.hostnqn = strndup(hostnqn, strcspn(hostnqn, "\n"));
if (!cfg.hostnqn)
goto out;
ret = true;
out:
fclose(f);
return ret;
}
static int nvmf_hostid_file(void)
{
FILE *f;
char hostid[NVMF_HOSTID_SIZE + 1];
int ret = false;
f = fopen(PATH_NVMF_HOSTID, "r");
if (f == NULL)
return false;
if (fgets(hostid, sizeof(hostid), f) == NULL)
goto out;
cfg.hostid = strdup(hostid);
if (!cfg.hostid)
goto out;
ret = true;
out:
fclose(f);
return ret;
}
static int
add_bool_argument(char **argstr, int *max_len, char *arg_str, bool arg)
{
int len;
if (arg) {
len = snprintf(*argstr, *max_len, ",%s", arg_str);
if (len < 0)
return -EINVAL;
*argstr += len;
*max_len -= len;
}
return 0;
}
static int
add_argument(char **argstr, int *max_len, char *arg_str, char *arg)
{
int len;
if (arg) {
len = snprintf(*argstr, *max_len, ",%s=%s", arg_str, arg);
if (len < 0)
return -EINVAL;
*argstr += len;
*max_len -= len;
}
return 0;
}
static int build_options(char *argstr, int max_len)
{
int len;
if (!cfg.transport) {
fprintf(stderr, "need a transport (-t) argument\n");
return -EINVAL;
}
if (strncmp(cfg.transport, "loop", 4)) {
if (!cfg.traddr) {
fprintf(stderr, "need a address (-a) argument\n");
return -EINVAL;
}
}
/* always specify nqn as first arg - this will init the string */
len = snprintf(argstr, max_len, "nqn=%s", cfg.nqn);
if (len < 0)
return -EINVAL;
argstr += len;
max_len -= len;
if (add_argument(&argstr, &max_len, "transport", cfg.transport) ||
add_argument(&argstr, &max_len, "traddr", cfg.traddr) ||
add_argument(&argstr, &max_len, "host_traddr", cfg.host_traddr) ||
add_argument(&argstr, &max_len, "trsvcid", cfg.trsvcid) ||
((cfg.hostnqn || nvmf_hostnqn_file()) &&
add_argument(&argstr, &max_len, "hostnqn", cfg.hostnqn)) ||
((cfg.hostid || nvmf_hostid_file()) &&
add_argument(&argstr, &max_len, "hostid", cfg.hostid)) ||
add_argument(&argstr, &max_len, "nr_io_queues",
cfg.nr_io_queues) ||
add_argument(&argstr, &max_len, "queue_size", cfg.queue_size) ||
add_argument(&argstr, &max_len, "keep_alive_tmo",
cfg.keep_alive_tmo) ||
add_argument(&argstr, &max_len, "reconnect_delay",
cfg.reconnect_delay) ||
add_argument(&argstr, &max_len, "ctrl_loss_tmo",
cfg.ctrl_loss_tmo) ||
add_bool_argument(&argstr, &max_len, "duplicate_connect",
cfg.duplicate_connect))
return -EINVAL;
return 0;
}
static int connect_ctrl(struct nvmf_disc_rsp_page_entry *e)
{
char argstr[BUF_SIZE], *p = argstr;
bool discover = false;
int len;
switch (e->subtype) {
case NVME_NQN_DISC:
discover = true;
case NVME_NQN_NVME:
break;
default:
fprintf(stderr, "skipping unsupported subtype %d\n",
e->subtype);
return -EINVAL;
}
len = sprintf(p, "nqn=%s", e->subnqn);
if (len < 0)
return -EINVAL;
p += len;
if (cfg.hostnqn) {
len = sprintf(p, ",hostnqn=%s", cfg.hostnqn);
if (len < 0)
return -EINVAL;
p += len;
}
if (cfg.hostid) {
len = sprintf(p, ",hostid=%s", cfg.hostid);
if (len < 0)
return -EINVAL;
p += len;
}
if (cfg.queue_size) {
len = sprintf(p, ",queue_size=%s", cfg.queue_size);
if (len < 0)
return -EINVAL;
p += len;
}
if (cfg.nr_io_queues) {
len = sprintf(p, ",nr_io_queues=%s", cfg.nr_io_queues);
if (len < 0)
return -EINVAL;
p += len;
}
if (cfg.host_traddr) {
len = sprintf(p, ",host_traddr=%s", cfg.host_traddr);
if (len < 0)
return -EINVAL;
p+= len;
}
switch (e->trtype) {
case NVMF_TRTYPE_LOOP: /* loop */
len = sprintf(p, ",transport=loop");
if (len < 0)
return -EINVAL;
p += len;
/* we can safely ignore the rest of the entries */
break;
case NVMF_TRTYPE_RDMA:
switch (e->adrfam) {
case NVMF_ADDR_FAMILY_IP4:
case NVMF_ADDR_FAMILY_IP6:
/* FALLTHRU */
len = sprintf(p, ",transport=rdma");
if (len < 0)
return -EINVAL;
p += len;
len = sprintf(p, ",traddr=%.*s",
space_strip_len(NVMF_TRADDR_SIZE, e->traddr),
e->traddr);
if (len < 0)
return -EINVAL;
p += len;
len = sprintf(p, ",trsvcid=%.*s",
space_strip_len(NVMF_TRSVCID_SIZE, e->trsvcid),
e->trsvcid);
if (len < 0)
return -EINVAL;
p += len;
break;
default:
fprintf(stderr, "skipping unsupported adrfam\n");
return -EINVAL;
}
break;
case NVMF_TRTYPE_FC:
switch (e->adrfam) {
case NVMF_ADDR_FAMILY_FC:
len = sprintf(p, ",transport=fc");
if (len < 0)
return -EINVAL;
p += len;
len = sprintf(p, ",traddr=%.*s",
space_strip_len(NVMF_TRADDR_SIZE, e->traddr),
e->traddr);
if (len < 0)
return -EINVAL;
p += len;
break;
default:
fprintf(stderr, "skipping unsupported adrfam\n");
return -EINVAL;
}
break;
default:
fprintf(stderr, "skipping unsupported transport %d\n",
e->trtype);
return -EINVAL;
}
if (discover)
return do_discover(argstr, true);
else
return add_ctrl(argstr);
}
static void connect_ctrls(struct nvmf_disc_rsp_page_hdr *log, int numrec)
{
int i;
for (i = 0; i < numrec; i++)
connect_ctrl(&log->entries[i]);
}
static int do_discover(char *argstr, bool connect)
{
struct nvmf_disc_rsp_page_hdr *log = NULL;
char *dev_name;
int instance, numrec = 0, ret;
instance = add_ctrl(argstr);
if (instance < 0)
return instance;
if (asprintf(&dev_name, "/dev/nvme%d", instance) < 0)
return errno;
ret = nvmf_get_log_page_discovery(dev_name, &log, &numrec);
free(dev_name);
remove_ctrl(instance);
switch (ret) {
case DISC_OK:
if (connect)
connect_ctrls(log, numrec);
else if (cfg.raw)
save_discovery_log(log, numrec);
else
print_discovery_log(log, numrec);
free(log);
break;
case DISC_GET_NUMRECS:
fprintf(stderr,
"Get number of discovery log entries failed.\n");
break;
case DISC_GET_LOG:
fprintf(stderr, "Get discovery log entries failed.\n");
break;
case DISC_NO_LOG:
fprintf(stderr, "No discovery log entries to fetch.\n");
break;
case DISC_NOT_EQUAL:
fprintf(stderr,
"Numrec values of last two get discovery log page not equal\n");
break;
default:
fprintf(stderr, "Get discovery log page failed: %d\n", ret);
break;
}
return ret;
}
static int discover_from_conf_file(const char *desc, char *argstr,
const struct argconfig_commandline_options *opts, bool connect)
{
FILE *f;
char line[256], *ptr, *args, **argv;
int argc, err, ret = 0;
f = fopen(PATH_NVMF_DISC, "r");
if (f == NULL) {
fprintf(stderr, "No discover params given and no %s conf\n",
PATH_NVMF_DISC);
return -EINVAL;
}
while (fgets(line, sizeof(line), f) != NULL) {
if (line[0] == '#' || line[0] == '\n')
continue;
args = strdup(line);
if (!args) {
fprintf(stderr, "failed to strdup args\n");
ret = -ENOMEM;
goto out;
}
argv = calloc(MAX_DISC_ARGS, BUF_SIZE);
if (!argv) {
fprintf(stderr, "failed to allocate argv vector\n");
free(args);
ret = -ENOMEM;
goto out;
}
argc = 0;
argv[argc++] = "discover";
while ((ptr = strsep(&args, " =\n")) != NULL)
argv[argc++] = ptr;
err = argconfig_parse(argc, argv, desc, opts, &cfg, sizeof(cfg));
if (err)
continue;
err = build_options(argstr, BUF_SIZE);
if (err) {
ret = err;
continue;
}
err = do_discover(argstr, connect);
if (err) {
ret = err;
continue;
}
free(args);
free(argv);
}
out:
fclose(f);
return ret;
}
int fdiscover(const char *desc, int argc, char **argv, bool connect)
{
char argstr[BUF_SIZE];
int ret;
const struct argconfig_commandline_options command_line_options[] = {
{"transport", 't', "LIST", CFG_STRING, &cfg.transport, required_argument, "transport type" },
{"traddr", 'a', "LIST", CFG_STRING, &cfg.traddr, required_argument, "transport address" },
{"trsvcid", 's', "LIST", CFG_STRING, &cfg.trsvcid, required_argument, "transport service id (e.g. IP port)" },
{"host-traddr", 'w', "LIST", CFG_STRING, &cfg.host_traddr, required_argument, "host traddr (e.g. FC WWN's)" },
{"hostnqn", 'q', "LIST", CFG_STRING, &cfg.hostnqn, required_argument, "user-defined hostnqn (if default not used)" },
{"hostid", 'I', "LIST", CFG_STRING, &cfg.hostid, required_argument, "user-defined hostid (if default not used)"},
{"queue-size", 'Q', "LIST", CFG_STRING, &cfg.queue_size, required_argument, "number of io queue elements to use (default 128)" },
{"nr-io-queues",'i', "LIST", CFG_STRING, &cfg.nr_io_queues,required_argument, "number of io queues to use (default is core count)" },
{"raw", 'r', "LIST", CFG_STRING, &cfg.raw, required_argument, "raw output file" },
{"keep-alive-tmo", 'k', "LIST", CFG_STRING, &cfg.keep_alive_tmo, required_argument, "keep alive timeout period in seconds" },
{"ctrl-loss-tmo", 'l', "LIST", CFG_STRING, &cfg.ctrl_loss_tmo, required_argument, "controller loss timeout period in seconds" },
{NULL},
};
ret = argconfig_parse(argc, argv, desc, command_line_options, &cfg,
sizeof(cfg));
if (ret)
return ret;
cfg.nqn = NVME_DISC_SUBSYS_NAME;
if (g_spdk_enabled == true) {
ret = nvme_spdk_nvmf_connect((void *)&cfg);
if (ret)
return ret;
}
if (!cfg.transport && !cfg.traddr) {
return discover_from_conf_file(desc, argstr,
command_line_options, connect);
} else {
ret = build_options(argstr, BUF_SIZE);
if (ret)
return ret;
return do_discover(argstr, connect);
}
}
int fconnect(const char *desc, int argc, char **argv)
{
char argstr[BUF_SIZE];
int instance, ret;
const struct argconfig_commandline_options command_line_options[] = {
{"transport", 't', "LIST", CFG_STRING, &cfg.transport, required_argument, "transport type" },
{"nqn", 'n', "LIST", CFG_STRING, &cfg.nqn, required_argument, "nqn name" },
{"traddr", 'a', "LIST", CFG_STRING, &cfg.traddr, required_argument, "transport address" },
{"trsvcid", 's', "LIST", CFG_STRING, &cfg.trsvcid, required_argument, "transport service id (e.g. IP port)" },
{"host-traddr", 'w', "LIST", CFG_STRING, &cfg.host_traddr, required_argument, "host traddr (e.g. FC WWN's)" },
{"hostnqn", 'q', "LIST", CFG_STRING, &cfg.hostnqn, required_argument, "user-defined hostnqn" },
{"hostid", 'I', "LIST", CFG_STRING, &cfg.hostid, required_argument, "user-defined hostid (if default not used)"},
{"nr-io-queues", 'i', "LIST", CFG_STRING, &cfg.nr_io_queues, required_argument, "number of io queues to use (default is core count)" },
{"queue-size", 'Q', "LIST", CFG_STRING, &cfg.queue_size, required_argument, "number of io queue elements to use (default 128)" },
{"keep-alive-tmo", 'k', "LIST", CFG_STRING, &cfg.keep_alive_tmo, required_argument, "keep alive timeout period in seconds" },
{"reconnect-delay", 'c', "LIST", CFG_STRING, &cfg.reconnect_delay, required_argument, "reconnect timeout period in seconds" },
{"ctrl-loss-tmo", 'l', "LIST", CFG_STRING, &cfg.ctrl_loss_tmo, required_argument, "controller loss timeout period in seconds" },
{"duplicate_connect", 'D', "", CFG_NONE, &cfg.duplicate_connect, no_argument, "allow duplicate connections between same transport host and subsystem port" },
{NULL},
};
ret = argconfig_parse(argc, argv, desc, command_line_options, &cfg,
sizeof(cfg));
if (ret)
return ret;
ret = build_options(argstr, BUF_SIZE);
if (ret)
return ret;
if (!cfg.nqn) {
fprintf(stderr, "need a -n argument\n");
return -EINVAL;
}
if (g_spdk_enabled == true) {
nvme_spdk_nvmf_connect((void *)&cfg);
}
instance = add_ctrl(argstr);
if (instance < 0)
return instance;
return 0;
}
static int scan_sys_nvme_filter(const struct dirent *d)
{
if (!strcmp(d->d_name, "."))
return 0;
if (!strcmp(d->d_name, ".."))
return 0;
return 1;
}
/*
* Returns 1 if disconnect occurred, 0 otherwise.
*/
static int disconnect_subsys(char *nqn, char *ctrl)
{
char *sysfs_nqn_path = NULL, *sysfs_del_path = NULL;
char subsysnqn[NVMF_NQN_SIZE] = {};
int fd, ret = 0;
if (asprintf(&sysfs_nqn_path, "%s/%s/subsysnqn", SYS_NVME, ctrl) < 0)
goto free;
if (asprintf(&sysfs_del_path, "%s/%s/delete_controller", SYS_NVME, ctrl) < 0)
goto free;
fd = open(sysfs_nqn_path, O_RDONLY);
if (fd < 0)
goto free;
if (read(fd, subsysnqn, NVMF_NQN_SIZE) < 0)
goto close;
subsysnqn[strcspn(subsysnqn, "\n")] = '\0';
if (strcmp(subsysnqn, nqn))
goto close;
if (!remove_ctrl_by_path(sysfs_del_path))
ret = 1;
close:
close(fd);
free:
free(sysfs_del_path);
free(sysfs_nqn_path);
return ret;
}
/*
* Returns the number of controllers successfully disconnected.
*/
static int disconnect_by_nqn(char *nqn)
{
struct dirent **devices = NULL;
int i, n, ret = 0;
if (strlen(nqn) > NVMF_NQN_SIZE)
return -EINVAL;
n = scandir(SYS_NVME, &devices, scan_sys_nvme_filter, alphasort);
if (n < 0)
return n;
for (i = 0; i < n; i++)
ret += disconnect_subsys(nqn, devices[i]->d_name);
for (i = 0; i < n; i++)
free(devices[i]);
free(devices);
return ret;
}
static int disconnect_by_device(char *device)
{
int instance;
int ret;
device = basename(device);
ret = sscanf(device, "nvme%d", &instance);
if (ret < 0)
return ret;
if (!ret)
return -1;
return remove_ctrl(instance);
}
int fdisconnect(const char *desc, int argc, char **argv)
{
const char *nqn = "nqn name";
const char *device = "nvme device";
int ret;
const struct argconfig_commandline_options command_line_options[] = {
{"nqn", 'n', "LIST", CFG_STRING, &cfg.nqn, required_argument, nqn},
{"device", 'd', "LIST", CFG_STRING, &cfg.device, required_argument, device},
{NULL},
};
ret = argconfig_parse(argc, argv, desc, command_line_options, &cfg,
sizeof(cfg));
if (ret)
return ret;
if (!cfg.nqn && !cfg.device) {
fprintf(stderr, "need a -n or -d argument\n");
return -EINVAL;
}
if (cfg.nqn) {
ret = disconnect_by_nqn(cfg.nqn);
if (ret < 0)
fprintf(stderr, "Failed to disconnect by NQN: %s\n",
cfg.nqn);
else {
printf("NQN:%s disconnected %d controller(s)\n", cfg.nqn, ret);