-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathxnvme.c
1394 lines (1176 loc) · 35.1 KB
/
xnvme.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
/*
* fio xNVMe IO Engine
*
* IO engine using the xNVMe C API.
*
* See: http://xnvme.io/
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
#include <assert.h>
#include <libxnvme.h>
#include "fio.h"
#include "verify.h"
#include "zbd_types.h"
#include "dataplacement.h"
#include "optgroup.h"
static pthread_mutex_t g_serialize = PTHREAD_MUTEX_INITIALIZER;
struct xnvme_fioe_fwrap {
/* fio file representation */
struct fio_file *fio_file;
/* xNVMe device handle */
struct xnvme_dev *dev;
/* xNVMe device geometry */
const struct xnvme_geo *geo;
struct xnvme_queue *queue;
uint32_t ssw;
uint32_t lba_nbytes;
uint32_t md_nbytes;
uint32_t lba_pow2;
uint8_t _pad[16];
};
XNVME_STATIC_ASSERT(sizeof(struct xnvme_fioe_fwrap) == 64, "Incorrect size")
struct xnvme_fioe_data {
/* I/O completion queue */
struct io_u **iocq;
/* # of iocq entries; incremented via getevents()/cb_pool() */
uint64_t completed;
/*
* # of errors; incremented when observed on completion via
* getevents()/cb_pool()
*/
uint64_t ecount;
/* Controller which device/file to select */
int32_t prev;
int32_t cur;
/* Number of devices/files for which open() has been called */
int64_t nopen;
/* Number of devices/files allocated in files[] */
uint64_t nallocated;
struct iovec *iovec;
struct iovec *md_iovec;
struct xnvme_fioe_fwrap files[];
};
XNVME_STATIC_ASSERT(sizeof(struct xnvme_fioe_data) == 64, "Incorrect size")
struct xnvme_fioe_request {
/* Context for NVMe PI */
struct xnvme_pi_ctx pi_ctx;
/* Separate metadata buffer pointer */
void *md_buf;
};
struct xnvme_fioe_options {
void *padding;
unsigned int hipri;
unsigned int sqpoll_thread;
unsigned int xnvme_dev_nsid;
unsigned int xnvme_iovec;
unsigned int md_per_io_size;
unsigned int pi_act;
unsigned int apptag;
unsigned int apptag_mask;
unsigned int prchk;
char *xnvme_be;
char *xnvme_mem;
char *xnvme_async;
char *xnvme_sync;
char *xnvme_admin;
char *xnvme_dev_subnqn;
};
static int str_pi_chk_cb(void *data, const char *str)
{
struct xnvme_fioe_options *o = data;
if (strstr(str, "GUARD") != NULL)
o->prchk = XNVME_PI_FLAGS_GUARD_CHECK;
if (strstr(str, "REFTAG") != NULL)
o->prchk |= XNVME_PI_FLAGS_REFTAG_CHECK;
if (strstr(str, "APPTAG") != NULL)
o->prchk |= XNVME_PI_FLAGS_APPTAG_CHECK;
return 0;
}
static struct fio_option options[] = {
{
.name = "hipri",
.lname = "High Priority",
.type = FIO_OPT_STR_SET,
.off1 = offsetof(struct xnvme_fioe_options, hipri),
.help = "Use polled IO completions",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_XNVME,
},
{
.name = "sqthread_poll",
.lname = "Kernel SQ thread polling",
.type = FIO_OPT_STR_SET,
.off1 = offsetof(struct xnvme_fioe_options, sqpoll_thread),
.help = "Offload submission/completion to kernel thread",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_XNVME,
},
{
.name = "xnvme_be",
.lname = "xNVMe Backend",
.type = FIO_OPT_STR_STORE,
.off1 = offsetof(struct xnvme_fioe_options, xnvme_be),
.help = "Select xNVMe backend [spdk,linux,fbsd]",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_XNVME,
},
{
.name = "xnvme_mem",
.lname = "xNVMe Memory Backend",
.type = FIO_OPT_STR_STORE,
.off1 = offsetof(struct xnvme_fioe_options, xnvme_mem),
.help = "Select xNVMe memory backend",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_XNVME,
},
{
.name = "xnvme_async",
.lname = "xNVMe Asynchronous command-interface",
.type = FIO_OPT_STR_STORE,
.off1 = offsetof(struct xnvme_fioe_options, xnvme_async),
.help = "Select xNVMe async. interface: "
"[emu,thrpool,io_uring,io_uring_cmd,libaio,posix,vfio,nil]",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_XNVME,
},
{
.name = "xnvme_sync",
.lname = "xNVMe Synchronous. command-interface",
.type = FIO_OPT_STR_STORE,
.off1 = offsetof(struct xnvme_fioe_options, xnvme_sync),
.help = "Select xNVMe sync. interface: [nvme,psync,block]",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_XNVME,
},
{
.name = "xnvme_admin",
.lname = "xNVMe Admin command-interface",
.type = FIO_OPT_STR_STORE,
.off1 = offsetof(struct xnvme_fioe_options, xnvme_admin),
.help = "Select xNVMe admin. cmd-interface: [nvme,block]",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_XNVME,
},
{
.name = "xnvme_dev_nsid",
.lname = "xNVMe Namespace-Identifier, for user-space NVMe driver",
.type = FIO_OPT_INT,
.off1 = offsetof(struct xnvme_fioe_options, xnvme_dev_nsid),
.help = "xNVMe Namespace-Identifier, for user-space NVMe driver",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_XNVME,
},
{
.name = "xnvme_dev_subnqn",
.lname = "Subsystem nqn for Fabrics",
.type = FIO_OPT_STR_STORE,
.off1 = offsetof(struct xnvme_fioe_options, xnvme_dev_subnqn),
.help = "Subsystem NQN for Fabrics",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_XNVME,
},
{
.name = "xnvme_iovec",
.lname = "Vectored IOs",
.type = FIO_OPT_STR_SET,
.off1 = offsetof(struct xnvme_fioe_options, xnvme_iovec),
.help = "Send vectored IOs",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_XNVME,
},
{
.name = "md_per_io_size",
.lname = "Separate Metadata Buffer Size per I/O",
.type = FIO_OPT_INT,
.off1 = offsetof(struct xnvme_fioe_options, md_per_io_size),
.def = "0",
.help = "Size of separate metadata buffer per I/O (Default: 0)",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_XNVME,
},
{
.name = "pi_act",
.lname = "Protection Information Action",
.type = FIO_OPT_BOOL,
.off1 = offsetof(struct xnvme_fioe_options, pi_act),
.def = "1",
.help = "Protection Information Action bit (pi_act=1 or pi_act=0)",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_XNVME,
},
{
.name = "pi_chk",
.lname = "Protection Information Check",
.type = FIO_OPT_STR_STORE,
.def = NULL,
.help = "Control of Protection Information Checking (pi_chk=GUARD,REFTAG,APPTAG)",
.cb = str_pi_chk_cb,
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_XNVME,
},
{
.name = "apptag",
.lname = "Application Tag used in Protection Information",
.type = FIO_OPT_INT,
.off1 = offsetof(struct xnvme_fioe_options, apptag),
.def = "0x1234",
.help = "Application Tag used in Protection Information field (Default: 0x1234)",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_XNVME,
},
{
.name = "apptag_mask",
.lname = "Application Tag Mask",
.type = FIO_OPT_INT,
.off1 = offsetof(struct xnvme_fioe_options, apptag_mask),
.def = "0xffff",
.help = "Application Tag Mask used with Application Tag (Default: 0xffff)",
.category = FIO_OPT_C_ENGINE,
.group = FIO_OPT_G_XNVME,
},
{
.name = NULL,
},
};
static void cb_pool(struct xnvme_cmd_ctx *ctx, void *cb_arg)
{
struct io_u *io_u = cb_arg;
struct xnvme_fioe_data *xd = io_u->mmap_data;
struct xnvme_fioe_request *fio_req = io_u->engine_data;
struct xnvme_fioe_fwrap *fwrap = &xd->files[io_u->file->fileno];
bool pi_act = (fio_req->pi_ctx.pi_flags >> 3);
int err;
if (xnvme_cmd_ctx_cpl_status(ctx)) {
xnvme_cmd_ctx_pr(ctx, XNVME_PR_DEF);
xd->ecount += 1;
io_u->error = EIO;
}
if (!io_u->error && fwrap->geo->pi_type && (io_u->ddir == DDIR_READ) && !pi_act) {
err = xnvme_pi_verify(&fio_req->pi_ctx, io_u->xfer_buf,
fio_req->md_buf, io_u->xfer_buflen / fwrap->lba_nbytes);
if (err) {
xd->ecount += 1;
io_u->error = EIO;
}
}
xd->iocq[xd->completed++] = io_u;
xnvme_queue_put_cmd_ctx(ctx->async.queue, ctx);
}
static struct xnvme_opts xnvme_opts_from_fioe(struct thread_data *td)
{
struct xnvme_fioe_options *o = td->eo;
struct xnvme_opts opts = xnvme_opts_default();
opts.nsid = o->xnvme_dev_nsid;
opts.subnqn = o->xnvme_dev_subnqn;
opts.be = o->xnvme_be;
opts.mem = o->xnvme_mem;
opts.async = o->xnvme_async;
opts.sync = o->xnvme_sync;
opts.admin = o->xnvme_admin;
opts.poll_io = o->hipri;
opts.poll_sq = o->sqpoll_thread;
opts.direct = td->o.odirect;
return opts;
}
static void _dev_close(struct thread_data *td, struct xnvme_fioe_fwrap *fwrap)
{
if (fwrap->dev)
xnvme_queue_term(fwrap->queue);
xnvme_dev_close(fwrap->dev);
memset(fwrap, 0, sizeof(*fwrap));
}
static void xnvme_fioe_cleanup(struct thread_data *td)
{
struct xnvme_fioe_data *xd = NULL;
int err;
if (!td->io_ops_data)
return;
xd = td->io_ops_data;
err = pthread_mutex_lock(&g_serialize);
if (err)
log_err("ioeng->cleanup(): pthread_mutex_lock(), err(%d)\n", err);
/* NOTE: not returning here */
for (uint64_t i = 0; i < xd->nallocated; ++i)
_dev_close(td, &xd->files[i]);
if (!err) {
err = pthread_mutex_unlock(&g_serialize);
if (err)
log_err("ioeng->cleanup(): pthread_mutex_unlock(), err(%d)\n", err);
}
free(xd->iocq);
free(xd->iovec);
free(xd->md_iovec);
free(xd);
td->io_ops_data = NULL;
}
static int _verify_options(struct thread_data *td, struct fio_file *f,
struct xnvme_fioe_fwrap *fwrap)
{
struct xnvme_fioe_options *o = td->eo;
unsigned int correct_md_size;
for_each_rw_ddir(ddir) {
if (td->o.min_bs[ddir] % fwrap->lba_nbytes || td->o.max_bs[ddir] % fwrap->lba_nbytes) {
if (!fwrap->lba_pow2) {
log_err("ioeng->_verify_options(%s): block size must be a multiple of %u "
"(LBA data size + Metadata size)\n", f->file_name, fwrap->lba_nbytes);
} else {
log_err("ioeng->_verify_options(%s): block size must be a multiple of LBA data size\n",
f->file_name);
}
return 1;
}
if (ddir == DDIR_TRIM)
continue;
correct_md_size = (td->o.max_bs[ddir] / fwrap->lba_nbytes) * fwrap->md_nbytes;
if (fwrap->md_nbytes && fwrap->lba_pow2 && (o->md_per_io_size < correct_md_size)) {
log_err("ioeng->_verify_options(%s): md_per_io_size should be at least %u bytes\n",
f->file_name, correct_md_size);
return 1;
}
}
/*
* For extended logical block sizes we cannot use verify when
* end to end data protection checks are enabled, as the PI
* section of data buffer conflicts with verify.
*/
if (fwrap->md_nbytes && fwrap->geo->pi_type && !fwrap->lba_pow2 &&
td->o.verify != VERIFY_NONE) {
log_err("ioeng->_verify_options(%s): for extended LBA, verify cannot be used when E2E data protection is enabled\n",
f->file_name);
return 1;
}
return 0;
}
/**
* Helper function setting up device handles as addressed by the naming
* convention of the given `fio_file` filename.
*
* Checks thread-options for explicit control of asynchronous implementation via
* the ``--xnvme_async={thrpool,emu,posix,io_uring,libaio,nil}``.
*/
static int _dev_open(struct thread_data *td, struct fio_file *f)
{
struct xnvme_opts opts = xnvme_opts_from_fioe(td);
struct xnvme_fioe_options *o = td->eo;
struct xnvme_fioe_data *xd = td->io_ops_data;
struct xnvme_fioe_fwrap *fwrap;
int flags = 0;
int err;
if (f->fileno > (int)xd->nallocated) {
log_err("ioeng->_dev_open(%s): invalid assumption\n", f->file_name);
return 1;
}
fwrap = &xd->files[f->fileno];
err = pthread_mutex_lock(&g_serialize);
if (err) {
log_err("ioeng->_dev_open(%s): pthread_mutex_lock(), err(%d)\n", f->file_name,
err);
return -err;
}
fwrap->dev = xnvme_dev_open(f->file_name, &opts);
if (!fwrap->dev) {
log_err("ioeng->_dev_open(%s): xnvme_dev_open(), err(%d)\n", f->file_name, errno);
goto failure;
}
fwrap->geo = xnvme_dev_get_geo(fwrap->dev);
if (xnvme_queue_init(fwrap->dev, td->o.iodepth, flags, &(fwrap->queue))) {
log_err("ioeng->_dev_open(%s): xnvme_queue_init(), err(?)\n", f->file_name);
goto failure;
}
xnvme_queue_set_cb(fwrap->queue, cb_pool, NULL);
fwrap->ssw = xnvme_dev_get_ssw(fwrap->dev);
fwrap->lba_nbytes = fwrap->geo->lba_nbytes;
fwrap->md_nbytes = fwrap->geo->nbytes_oob;
if (fwrap->geo->lba_extended)
fwrap->lba_pow2 = 0;
else
fwrap->lba_pow2 = 1;
/*
* When PI action is set and PI size is equal to metadata size, the
* controller inserts/removes PI. So update the LBA data and metadata
* sizes accordingly.
*/
if (o->pi_act && fwrap->geo->pi_type &&
fwrap->geo->nbytes_oob == xnvme_pi_size(fwrap->geo->pi_format)) {
if (fwrap->geo->lba_extended) {
fwrap->lba_nbytes -= fwrap->geo->nbytes_oob;
fwrap->lba_pow2 = 1;
}
fwrap->md_nbytes = 0;
}
if (_verify_options(td, f, fwrap)) {
td_verror(td, EINVAL, "_dev_open");
goto failure;
}
fwrap->fio_file = f;
fwrap->fio_file->filetype = FIO_TYPE_BLOCK;
fwrap->fio_file->real_file_size = fwrap->geo->tbytes;
fio_file_set_size_known(fwrap->fio_file);
err = pthread_mutex_unlock(&g_serialize);
if (err)
log_err("ioeng->_dev_open(%s): pthread_mutex_unlock(), err(%d)\n", f->file_name,
err);
return 0;
failure:
xnvme_queue_term(fwrap->queue);
xnvme_dev_close(fwrap->dev);
err = pthread_mutex_unlock(&g_serialize);
if (err)
log_err("ioeng->_dev_open(%s): pthread_mutex_unlock(), err(%d)\n", f->file_name,
err);
return 1;
}
static int xnvme_fioe_init(struct thread_data *td)
{
struct xnvme_fioe_data *xd = NULL;
struct xnvme_fioe_options *o = td->eo;
struct fio_file *f;
unsigned int i;
if (!td->o.use_thread) {
log_err("ioeng->init(): --thread=1 is required\n");
return 1;
}
/* Allocate xd and iocq */
xd = calloc(1, sizeof(*xd) + sizeof(*xd->files) * td->o.nr_files);
if (!xd) {
log_err("ioeng->init(): !calloc(), err(%d)\n", errno);
return 1;
}
xd->iocq = calloc(td->o.iodepth, sizeof(struct io_u *));
if (!xd->iocq) {
free(xd);
log_err("ioeng->init(): !calloc(xd->iocq), err(%d)\n", errno);
return 1;
}
if (o->xnvme_iovec) {
xd->iovec = calloc(td->o.iodepth, sizeof(*xd->iovec));
if (!xd->iovec) {
free(xd->iocq);
free(xd);
log_err("ioeng->init(): !calloc(xd->iovec), err(%d)\n", errno);
return 1;
}
}
if (o->xnvme_iovec && o->md_per_io_size) {
xd->md_iovec = calloc(td->o.iodepth, sizeof(*xd->md_iovec));
if (!xd->md_iovec) {
free(xd->iocq);
free(xd->iovec);
free(xd);
log_err("ioeng->init(): !calloc(xd->md_iovec), err(%d)\n", errno);
return 1;
}
}
xd->prev = -1;
td->io_ops_data = xd;
for_each_file(td, f, i)
{
if (_dev_open(td, f)) {
/*
* Note: We are not freeing xd, iocq, iovec and md_iovec.
* This will be done as part of cleanup routine.
*/
log_err("ioeng->init(): failed; _dev_open(%s)\n", f->file_name);
return 1;
}
++(xd->nallocated);
}
if (xd->nallocated != td->o.nr_files) {
log_err("ioeng->init(): failed; nallocated != td->o.nr_files\n");
return 1;
}
return 0;
}
/* NOTE: using the first device for buffer-allocators) */
static int xnvme_fioe_iomem_alloc(struct thread_data *td, size_t total_mem)
{
struct xnvme_fioe_data *xd = td->io_ops_data;
struct xnvme_fioe_fwrap *fwrap = &xd->files[0];
if (!fwrap->dev) {
log_err("ioeng->iomem_alloc(): failed; no dev-handle\n");
return 1;
}
td->orig_buffer = xnvme_buf_alloc(fwrap->dev, total_mem);
return td->orig_buffer == NULL;
}
/* NOTE: using the first device for buffer-allocators) */
static void xnvme_fioe_iomem_free(struct thread_data *td)
{
struct xnvme_fioe_data *xd = NULL;
struct xnvme_fioe_fwrap *fwrap = NULL;
if (!td->io_ops_data)
return;
xd = td->io_ops_data;
fwrap = &xd->files[0];
if (!fwrap->dev) {
log_err("ioeng->iomem_free(): failed no dev-handle\n");
return;
}
xnvme_buf_free(fwrap->dev, td->orig_buffer);
}
static int xnvme_fioe_io_u_init(struct thread_data *td, struct io_u *io_u)
{
struct xnvme_fioe_request *fio_req;
struct xnvme_fioe_options *o = td->eo;
struct xnvme_fioe_data *xd = td->io_ops_data;
struct xnvme_fioe_fwrap *fwrap = &xd->files[0];
if (!fwrap->dev) {
log_err("ioeng->io_u_init(): failed; no dev-handle\n");
return 1;
}
io_u->mmap_data = td->io_ops_data;
io_u->engine_data = NULL;
fio_req = calloc(1, sizeof(*fio_req));
if (!fio_req) {
log_err("ioeng->io_u_init(): !calloc(fio_req), err(%d)\n", errno);
return 1;
}
if (o->md_per_io_size) {
fio_req->md_buf = xnvme_buf_alloc(fwrap->dev, o->md_per_io_size);
if (!fio_req->md_buf) {
free(fio_req);
return 1;
}
}
io_u->engine_data = fio_req;
return 0;
}
static void xnvme_fioe_io_u_free(struct thread_data *td, struct io_u *io_u)
{
struct xnvme_fioe_data *xd = NULL;
struct xnvme_fioe_fwrap *fwrap = NULL;
struct xnvme_fioe_request *fio_req = NULL;
if (!td->io_ops_data)
return;
xd = td->io_ops_data;
fwrap = &xd->files[0];
if (!fwrap->dev) {
log_err("ioeng->io_u_free(): failed no dev-handle\n");
return;
}
fio_req = io_u->engine_data;
if (fio_req->md_buf)
xnvme_buf_free(fwrap->dev, fio_req->md_buf);
free(fio_req);
io_u->mmap_data = NULL;
}
static struct io_u *xnvme_fioe_event(struct thread_data *td, int event)
{
struct xnvme_fioe_data *xd = td->io_ops_data;
assert(event >= 0);
assert((unsigned)event < xd->completed);
return xd->iocq[event];
}
static int xnvme_fioe_getevents(struct thread_data *td, unsigned int min, unsigned int max,
const struct timespec *t)
{
struct xnvme_fioe_data *xd = td->io_ops_data;
struct xnvme_fioe_fwrap *fwrap = NULL;
int nfiles = xd->nallocated;
int err = 0;
if (xd->prev != -1 && ++xd->prev < nfiles) {
fwrap = &xd->files[xd->prev];
xd->cur = xd->prev;
}
xd->completed = 0;
for (;;) {
if (fwrap == NULL || xd->cur == nfiles) {
fwrap = &xd->files[0];
xd->cur = 0;
}
while (fwrap != NULL && xd->cur < nfiles && err >= 0) {
err = xnvme_queue_poke(fwrap->queue, max - xd->completed);
if (err < 0) {
switch (err) {
case -EBUSY:
case -EAGAIN:
usleep(1);
break;
default:
log_err("ioeng->getevents(): unhandled IO error\n");
assert(false);
return 0;
}
}
if (xd->completed >= min) {
xd->prev = xd->cur;
return xd->completed;
}
xd->cur++;
fwrap = &xd->files[xd->cur];
if (err < 0) {
switch (err) {
case -EBUSY:
case -EAGAIN:
usleep(1);
break;
}
}
}
}
xd->cur = 0;
return xd->completed;
}
static enum fio_q_status xnvme_fioe_queue(struct thread_data *td, struct io_u *io_u)
{
struct xnvme_fioe_data *xd = td->io_ops_data;
struct xnvme_fioe_options *o = td->eo;
struct xnvme_fioe_fwrap *fwrap;
struct xnvme_cmd_ctx *ctx;
struct xnvme_fioe_request *fio_req = io_u->engine_data;
uint32_t nsid;
uint64_t slba;
uint16_t nlb;
int err;
bool vectored_io = ((struct xnvme_fioe_options *)td->eo)->xnvme_iovec;
uint32_t dir = io_u->dtype;
fio_ro_check(td, io_u);
fwrap = &xd->files[io_u->file->fileno];
nsid = xnvme_dev_get_nsid(fwrap->dev);
if (fwrap->lba_pow2) {
slba = io_u->offset >> fwrap->ssw;
nlb = (io_u->xfer_buflen >> fwrap->ssw) - 1;
} else {
slba = io_u->offset / fwrap->lba_nbytes;
nlb = (io_u->xfer_buflen / fwrap->lba_nbytes) - 1;
}
ctx = xnvme_queue_get_cmd_ctx(fwrap->queue);
ctx->async.cb_arg = io_u;
ctx->cmd.common.nsid = nsid;
ctx->cmd.nvm.slba = slba;
ctx->cmd.nvm.nlb = nlb;
if (dir) {
ctx->cmd.nvm.dtype = io_u->dtype;
ctx->cmd.nvm.cdw13.dspec = io_u->dspec;
}
switch (io_u->ddir) {
case DDIR_READ:
ctx->cmd.common.opcode = XNVME_SPEC_NVM_OPC_READ;
break;
case DDIR_WRITE:
ctx->cmd.common.opcode = XNVME_SPEC_NVM_OPC_WRITE;
break;
default:
log_err("ioeng->queue(): ENOSYS: %u\n", io_u->ddir);
xnvme_queue_put_cmd_ctx(ctx->async.queue, ctx);
io_u->error = ENOSYS;
assert(false);
return FIO_Q_COMPLETED;
}
if (fwrap->geo->pi_type && !o->pi_act) {
err = xnvme_pi_ctx_init(&fio_req->pi_ctx, fwrap->lba_nbytes,
fwrap->geo->nbytes_oob, fwrap->geo->lba_extended,
fwrap->geo->pi_loc, fwrap->geo->pi_type,
(o->pi_act << 3 | o->prchk), slba, o->apptag_mask,
o->apptag, fwrap->geo->pi_format);
if (err) {
log_err("ioeng->queue(): err: '%d'\n", err);
xnvme_queue_put_cmd_ctx(ctx->async.queue, ctx);
io_u->error = abs(err);
return FIO_Q_COMPLETED;
}
if (io_u->ddir == DDIR_WRITE)
xnvme_pi_generate(&fio_req->pi_ctx, io_u->xfer_buf, fio_req->md_buf,
nlb + 1);
}
if (fwrap->geo->pi_type)
ctx->cmd.nvm.prinfo = (o->pi_act << 3 | o->prchk);
switch (fwrap->geo->pi_type) {
case XNVME_PI_TYPE1:
case XNVME_PI_TYPE2:
switch (fwrap->geo->pi_format) {
case XNVME_SPEC_NVM_NS_16B_GUARD:
if (o->prchk & XNVME_PI_FLAGS_REFTAG_CHECK)
ctx->cmd.nvm.ilbrt = (uint32_t)slba;
break;
case XNVME_SPEC_NVM_NS_64B_GUARD:
if (o->prchk & XNVME_PI_FLAGS_REFTAG_CHECK) {
ctx->cmd.nvm.ilbrt = (uint32_t)slba;
ctx->cmd.common.cdw03 = ((slba >> 32) & 0xffff);
}
break;
default:
break;
}
if (o->prchk & XNVME_PI_FLAGS_APPTAG_CHECK) {
ctx->cmd.nvm.lbat = o->apptag;
ctx->cmd.nvm.lbatm = o->apptag_mask;
}
break;
case XNVME_PI_TYPE3:
if (o->prchk & XNVME_PI_FLAGS_APPTAG_CHECK) {
ctx->cmd.nvm.lbat = o->apptag;
ctx->cmd.nvm.lbatm = o->apptag_mask;
}
break;
case XNVME_PI_DISABLE:
break;
}
if (vectored_io) {
xd->iovec[io_u->index].iov_base = io_u->xfer_buf;
xd->iovec[io_u->index].iov_len = io_u->xfer_buflen;
if (fwrap->md_nbytes && fwrap->lba_pow2) {
xd->md_iovec[io_u->index].iov_base = fio_req->md_buf;
xd->md_iovec[io_u->index].iov_len = fwrap->md_nbytes * (nlb + 1);
err = xnvme_cmd_passv(ctx, &xd->iovec[io_u->index], 1, io_u->xfer_buflen,
&xd->md_iovec[io_u->index], 1,
fwrap->md_nbytes * (nlb + 1));
} else {
err = xnvme_cmd_passv(ctx, &xd->iovec[io_u->index], 1, io_u->xfer_buflen,
NULL, 0, 0);
}
} else {
if (fwrap->md_nbytes && fwrap->lba_pow2)
err = xnvme_cmd_pass(ctx, io_u->xfer_buf, io_u->xfer_buflen,
fio_req->md_buf, fwrap->md_nbytes * (nlb + 1));
else
err = xnvme_cmd_pass(ctx, io_u->xfer_buf, io_u->xfer_buflen, NULL, 0);
}
switch (err) {
case 0:
return FIO_Q_QUEUED;
case -EBUSY:
case -EAGAIN:
xnvme_queue_put_cmd_ctx(ctx->async.queue, ctx);
return FIO_Q_BUSY;
default:
log_err("ioeng->queue(): err: '%d'\n", err);
xnvme_queue_put_cmd_ctx(ctx->async.queue, ctx);
io_u->error = abs(err);
assert(false);
return FIO_Q_COMPLETED;
}
}
static int xnvme_fioe_close(struct thread_data *td, struct fio_file *f)
{
struct xnvme_fioe_data *xd = td->io_ops_data;
dprint(FD_FILE, "xnvme close %s -- nopen: %ld\n", f->file_name, xd->nopen);
--(xd->nopen);
return 0;
}
static int xnvme_fioe_open(struct thread_data *td, struct fio_file *f)
{
struct xnvme_fioe_data *xd = td->io_ops_data;
dprint(FD_FILE, "xnvme open %s -- nopen: %ld\n", f->file_name, xd->nopen);
if (f->fileno > (int)xd->nallocated) {
log_err("ioeng->open(): f->fileno > xd->nallocated; invalid assumption\n");
return 1;
}
if (xd->files[f->fileno].fio_file != f) {
log_err("ioeng->open(): fio_file != f; invalid assumption\n");
return 1;
}
++(xd->nopen);
return 0;
}
static int xnvme_fioe_invalidate(struct thread_data *td, struct fio_file *f)
{
/* Consider only doing this with be:spdk */
return 0;
}
static int xnvme_fioe_get_max_open_zones(struct thread_data *td, struct fio_file *f,
unsigned int *max_open_zones)
{
struct xnvme_opts opts = xnvme_opts_from_fioe(td);
struct xnvme_dev *dev;
const struct xnvme_spec_znd_idfy_ns *zns;
int err = 0, err_lock;
if (f->filetype != FIO_TYPE_FILE && f->filetype != FIO_TYPE_BLOCK &&
f->filetype != FIO_TYPE_CHAR) {
log_info("ioeng->get_max_open_zoned(): ignoring filetype: %d\n", f->filetype);
return 0;
}
err_lock = pthread_mutex_lock(&g_serialize);
if (err_lock) {
log_err("ioeng->get_max_open_zones(): pthread_mutex_lock(), err(%d)\n", err_lock);
return -err_lock;
}
dev = xnvme_dev_open(f->file_name, &opts);
if (!dev) {
log_err("ioeng->get_max_open_zones(): xnvme_dev_open(), err(%d)\n", err_lock);
err = -errno;
goto exit;
}
if (xnvme_dev_get_geo(dev)->type != XNVME_GEO_ZONED) {
errno = EINVAL;
err = -errno;
goto exit;
}
zns = (void *)xnvme_dev_get_ns_css(dev);
if (!zns) {
log_err("ioeng->get_max_open_zones(): xnvme_dev_get_ns_css(), err(%d)\n", errno);
err = -errno;
goto exit;
}
/*
* intentional overflow as the value is zero-based and NVMe
* defines 0xFFFFFFFF as unlimited thus overflowing to 0 which
* is how fio indicates unlimited and otherwise just converting
* to one-based.
*/
*max_open_zones = zns->mor + 1;
exit:
xnvme_dev_close(dev);
err_lock = pthread_mutex_unlock(&g_serialize);
if (err_lock)
log_err("ioeng->get_max_open_zones(): pthread_mutex_unlock(), err(%d)\n",
err_lock);
return err;
}
/**
* Currently, this function is called before of I/O engine initialization, so,
* we cannot consult the file-wrapping done when 'fioe' initializes.
* Instead we just open based on the given filename.
*
* TODO: unify the different setup methods, consider keeping the handle around,
* and consider how to support the --be option in this usecase
*/
static int xnvme_fioe_get_zoned_model(struct thread_data *td, struct fio_file *f,
enum zbd_zoned_model *model)
{
struct xnvme_opts opts = xnvme_opts_from_fioe(td);
struct xnvme_dev *dev;
int err = 0, err_lock;
if (f->filetype != FIO_TYPE_FILE && f->filetype != FIO_TYPE_BLOCK &&
f->filetype != FIO_TYPE_CHAR) {
log_info("ioeng->get_zoned_model(): ignoring filetype: %d\n", f->filetype);
return -EINVAL;
}
err = pthread_mutex_lock(&g_serialize);
if (err) {
log_err("ioeng->get_zoned_model(): pthread_mutex_lock(), err(%d)\n", err);
return -err;
}
dev = xnvme_dev_open(f->file_name, &opts);
if (!dev) {
log_err("ioeng->get_zoned_model(): xnvme_dev_open(%s) failed, errno: %d\n",
f->file_name, errno);
err = -errno;
goto exit;
}