-
Notifications
You must be signed in to change notification settings - Fork 45
/
tgt_nbd.cpp
1150 lines (965 loc) · 30.5 KB
/
tgt_nbd.cpp
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
#include <config.h>
#include <vector>
#include "ublksrv_tgt.h"
#include "ublksrv_tgt_endian.h"
#include "cliserv.h"
#include "nbd.h"
//#define NBD_DEBUG_HANDSHAKE 1
//#define NBD_DEBUG_IO 1
//#define NBD_DEBUG_CQE 1
#ifdef NBD_DEBUG_IO
#define NBD_IO_DBG ublk_err
#else
#define NBD_IO_DBG(...)
#endif
#ifdef NBD_DEBUG_HANDSHAKE
#define NBD_HS_DBG ublk_err
#else
#define NBD_HS_DBG(...)
#endif
#define nbd_err ublk_err
#define NBD_MAX_NAME 512
#define NBD_OP_READ_REQ 0x80
#define NBD_OP_READ_REPLY 0x81
#define NBD_OP_WRITE_REQ 0x82
#define NBD_OP_FUSE_WR 0x83
#define NBD_OP_FUSE_RD 0x84
struct nbd_tgt_data {
bool unix_sock;
bool use_send_zc;
};
#ifndef HAVE_LIBURING_SEND_ZC
#define io_uring_prep_sendmsg_zc io_uring_prep_sendmsg
#define IORING_CQE_F_NOTIF (1U << 3)
#endif
struct nbd_queue_data {
unsigned short in_flight_ios;
unsigned short recv_started:1;
unsigned short use_send_zc:1;
unsigned short use_unix_sock:1;
unsigned short need_handle_recv:1;
unsigned short send_sqe_chain_busy:1;
unsigned int chained_send_ios;
/*
* When the current chain is busy, staggering send ios
* into this queue(next_chain). After the current chain
* is consumed, submit all send ios in 'next_chain' as
* one whole batch.
*/
std::vector <const struct ublk_io_data *> next_chain;
struct io_uring_sqe *last_send_sqe;
struct nbd_reply reply;
struct io_uring_cqe recv_cqe;
};
struct nbd_io_data {
unsigned int cmd_cookie;
unsigned int done; //for handling partial recv
};
static int use_zc = 0;
#ifdef HAVE_LIBURING_SEND_ZC
static inline void io_uring_prep_send_zc_ublk_zc(struct io_uring_sqe *sqe,
struct io_uring_sqe *secondary, int dev_fd,
int tag, int q_id, int fd, void *buf, unsigned offset,
unsigned len, unsigned msg_flags)
{
io_uring_prep_grp_lead(sqe, dev_fd, tag, q_id, 0);
sqe->flags |= IOSQE_IO_LINK;
sqe->user_data = build_user_data(tag, NBD_OP_FUSE_WR, 1, 1);
io_uring_prep_send_zc(secondary, fd, (void *)(u64)offset, len, msg_flags, 0);
io_uring_sqe_set_flags(secondary, IOSQE_FIXED_FILE);
}
#else
static inline void io_uring_prep_send_zc_ublk_zc io_uring_prep_send_ublk_zc
#endif
static inline void io_uring_prep_send_ublk_zc(struct io_uring_sqe *sqe,
struct io_uring_sqe *secondary, int dev_fd,
int tag, int q_id, int fd, void *buf, unsigned offset,
unsigned len, unsigned msg_flags)
{
io_uring_prep_grp_lead(sqe, dev_fd, tag, q_id, 0);
sqe->flags |= IOSQE_IO_LINK;
sqe->user_data = build_user_data(tag, NBD_OP_FUSE_WR, 1, 1);
io_uring_prep_send(secondary, fd, (void *)(u64)offset, len, msg_flags);
io_uring_sqe_set_flags(secondary, IOSQE_FIXED_FILE);
}
static inline void io_uring_prep_recv_ublk_zc(struct io_uring_sqe *sqe,
struct io_uring_sqe *secondary, int dev_fd,
int tag, int q_id, int fd, void *buf, unsigned offset, unsigned len)
{
io_uring_prep_grp_lead(sqe, dev_fd, tag, q_id, 0);
sqe->user_data = build_user_data(tag, NBD_OP_FUSE_RD, 1, 1);
io_uring_prep_recv(secondary, fd, (void *)(u64)offset, len, MSG_WAITALL);
io_uring_sqe_set_flags(secondary, IOSQE_FIXED_FILE);
}
static inline void nbd_set_chain_tail(struct nbd_queue_data *q_data,
struct io_uring_sqe *sqe)
{
q_data->last_send_sqe = sqe;
}
static inline struct nbd_queue_data *
nbd_get_queue_data(const struct ublksrv_queue *q)
{
return (struct nbd_queue_data *)q->private_data;
}
static inline struct nbd_io_data *
io_tgt_to_nbd_data(const struct ublk_io_tgt *io)
{
return (struct nbd_io_data *)(io + 1);
}
static int req_to_nbd_cmd_type(const struct ublksrv_io_desc *iod)
{
switch (ublksrv_get_op(iod)) {
case UBLK_IO_OP_DISCARD:
return NBD_CMD_TRIM;
case UBLK_IO_OP_FLUSH:
return NBD_CMD_FLUSH;
case UBLK_IO_OP_WRITE:
return NBD_CMD_WRITE;
case UBLK_IO_OP_READ:
return NBD_CMD_READ;
default:
return -1;
}
}
static inline bool is_recv_io(const struct ublksrv_queue *q,
const struct ublk_io_data *data)
{
return data->tag >= q->q_depth;
}
#define NBD_COOKIE_BITS 32
static inline u64 nbd_cmd_handle(const struct ublksrv_queue *q,
const struct ublk_io_data *data,
const struct nbd_io_data *nbd_data)
{
u64 cookie = nbd_data->cmd_cookie;
return (cookie << NBD_COOKIE_BITS) | ublk_unique_tag(q->q_id, data->tag);
}
static inline u32 nbd_handle_to_cookie(u64 handle)
{
return (u32)(handle >> NBD_COOKIE_BITS);
}
static inline u32 nbd_handle_to_tag(u64 handle)
{
return (u32)handle;
}
static inline void __nbd_build_req(const struct ublksrv_queue *q,
const struct ublk_io_data *data,
const struct nbd_io_data *nbd_data,
u32 type, struct nbd_request *req)
{
u32 nbd_cmd_flags = 0;
u64 handle;
if (data->iod->op_flags & UBLK_IO_F_FUA)
nbd_cmd_flags |= NBD_CMD_FLAG_FUA;
req->type = htonl(type | nbd_cmd_flags);
if (type != NBD_CMD_FLUSH) {
req->from = cpu_to_be64((u64)data->iod->start_sector << 9);
req->len = htonl(data->iod->nr_sectors << 9);
}
handle = nbd_cmd_handle(q, data, nbd_data);
memcpy(req->handle, &handle, sizeof(handle));
}
static inline void nbd_prep_non_write_sqe(struct io_uring_sqe *sqe, int fd,
const struct nbd_request *req, unsigned msg_flags,
unsigned ublk_op, unsigned tag)
{
io_uring_prep_send(sqe, fd, req, sizeof(*req), msg_flags);
sqe->user_data = build_user_data(tag, ublk_op, 0, 1);
io_uring_sqe_set_flags(sqe, /*IOSQE_CQE_SKIP_SUCCESS |*/
IOSQE_FIXED_FILE | IOSQE_IO_LINK);
}
/* use send() instead of sendmsg() */
static inline int nbd_prep_write_sqe_zc(const struct ublksrv_queue *q,
const struct ublk_io_data *data,
struct io_uring_sqe *sqe, int fd,
const struct msghdr *msg, unsigned msg_flags,
struct nbd_queue_data *q_data)
{
bool use_send_zc = q_data->use_send_zc;
unsigned nr_sects = msg->msg_iov[1].iov_len >> 9;
struct io_uring_sqe *sqe2, *sqe3;
io_uring_prep_send(sqe, fd, msg->msg_iov[0].iov_base,
msg->msg_iov[0].iov_len, msg_flags);
sqe->user_data = build_user_data(data->tag, NBD_OP_WRITE_REQ, 0, 1);
io_uring_sqe_set_flags(sqe, /*IOSQE_CQE_SKIP_SUCCESS |*/
IOSQE_FIXED_FILE | IOSQE_IO_LINK);
if (use_send_zc) {
ublk_get_sqe_pair(q->ring_ptr, &sqe3, &sqe2);
io_uring_prep_send_zc_ublk_zc(sqe3, sqe2, 0, data->tag, q->q_id, fd,
msg->msg_iov[1].iov_base, 0,
msg->msg_iov[1].iov_len, msg_flags);
nbd_set_chain_tail(q_data, sqe3);
} else {
ublk_get_sqe_pair(q->ring_ptr, &sqe3, &sqe2);
//io_uring_prep_send(sqe2, fd, msg->msg_iov[1].iov_base,
// msg->msg_iov[1].iov_len, msg_flags);
io_uring_prep_send_ublk_zc(sqe3, sqe2, 0, data->tag, q->q_id, fd,
msg->msg_iov[1].iov_base, 0,
msg->msg_iov[1].iov_len, msg_flags);
nbd_set_chain_tail(q_data, sqe3);
}
sqe2->user_data = build_user_data(data->tag, UBLK_IO_OP_WRITE, nr_sects, 1);
return 2;
}
static inline int nbd_prep_write_sqe(const struct ublksrv_queue *q,
const struct ublk_io_data *data,
struct io_uring_sqe *sqe, int fd,
const struct msghdr *msg, unsigned msg_flags,
struct nbd_queue_data *q_data)
{
bool use_send_zc = q_data->use_send_zc;
unsigned nr_sects = msg->msg_iov[1].iov_len >> 9;
if (use_send_zc)
io_uring_prep_sendmsg_zc(sqe, fd, msg, msg_flags);
else
io_uring_prep_sendmsg(sqe, fd, msg, msg_flags);
/*
* The encoded nr_sectors should only be used for validating write req
* when its cqe is completed, since iod data isn't available at that time
* because request can be reused.
*/
sqe->user_data = build_user_data(data->tag, UBLK_IO_OP_WRITE, nr_sects, 1);
io_uring_sqe_set_flags(sqe, /*IOSQE_CQE_SKIP_SUCCESS |*/
IOSQE_FIXED_FILE | IOSQE_IO_LINK);
nbd_set_chain_tail(q_data, sqe);
return 1;
}
static int nbd_queue_req(const struct ublksrv_queue *q,
const struct ublk_io_data *data,
const struct nbd_request *req, const struct msghdr *msg)
{
struct nbd_queue_data *q_data = nbd_get_queue_data(q);
const struct ublksrv_io_desc *iod = data->iod;
struct io_uring_sqe *sqe = io_uring_get_sqe(q->ring_ptr);
unsigned ublk_op = ublksrv_get_op(iod);
unsigned msg_flags = MSG_NOSIGNAL;
int queued_sqe = 1;
if (!sqe) {
nbd_err("%s: get sqe failed, tag %d op %d\n",
__func__, data->tag, ublk_op);
return -ENOMEM;
}
/*
* Always set WAITALL, so io_uring will handle retry in case of
* short send, see below link:
*
* https://lore.kernel.org/io-uring/b8011ec8-8d43-9b9b-4dcc-53b6cb272354@samba.org/
*
* note: It was added for recv* in 5.18 and send* in 5.19.
*/
msg_flags |= MSG_WAITALL;
if (ublk_op == UBLK_IO_OP_READ)
ublk_op = NBD_OP_READ_REQ;
if (ublk_op != UBLK_IO_OP_WRITE) {
nbd_prep_non_write_sqe(sqe, q->q_id + 1, req, msg_flags,
ublk_op, data->tag);
nbd_set_chain_tail(q_data, sqe);
} else {
if (use_zc)
queued_sqe = nbd_prep_write_sqe_zc(q, data, sqe, q->q_id + 1, msg,
msg_flags, q_data);
else
queued_sqe = nbd_prep_write_sqe(q, data, sqe, q->q_id + 1, msg,
msg_flags, q_data);
}
q_data->chained_send_ios += queued_sqe;
NBD_IO_DBG("%s: queue io op %d(%llu %x %llx) ios(%u %u)"
" (qid %d tag %u, cmd_op %u target: %d, user_data %llx)\n",
__func__, ublk_op, data->iod->start_sector,
data->iod->nr_sectors, sqe->addr,
q_data->in_flight_ios, q_data->chained_send_ios,
q->q_id, data->tag, ublk_op, 1, sqe->user_data);
return 1;
}
static co_io_job __nbd_handle_io_async(const struct ublksrv_queue *q,
const struct ublk_io_data *data, struct ublk_io_tgt *io)
{
int ret = -EIO;
struct nbd_request req = {.magic = htonl(NBD_REQUEST_MAGIC),};
struct nbd_queue_data *q_data = nbd_get_queue_data(q);
struct nbd_io_data *nbd_data = io_tgt_to_nbd_data(io);
int type = req_to_nbd_cmd_type(data->iod);
struct iovec iov[2] = {
[0] = {
.iov_base = (void *)&req,
.iov_len = sizeof(req),
},
[1] = {
.iov_base = (void *)data->iod->addr,
.iov_len = data->iod->nr_sectors << 9,
},
};
struct msghdr msg = {
.msg_iov = iov,
.msg_iovlen = 2,
};
if (type == -1)
goto fail;
nbd_data->cmd_cookie += 1;
__nbd_build_req(q, data, nbd_data, type, &req);
q_data->in_flight_ios += 1;
nbd_data->done = 0;
again:
ret = nbd_queue_req(q, data, &req, &msg);
if (ret < 0)
goto fail;
co_await__suspend_always(data->tag);
if (io->tgt_io_cqe->res == -EAGAIN)
goto again;
ret = io->tgt_io_cqe->res;
fail:
if (ret < 0)
nbd_err("%s: err %d\n", __func__, ret);
else
ret += nbd_data->done;
ublksrv_complete_io(q, data->tag, ret);
q_data->in_flight_ios -= 1;
NBD_IO_DBG("%s: tag %d res %d\n", __func__, data->tag, ret);
co_return;
}
static int nbd_handle_recv_reply(const struct ublksrv_queue *q,
struct nbd_io_data *nbd_data,
const struct io_uring_cqe *cqe,
const struct ublk_io_data **io_data)
{
struct nbd_queue_data *q_data = nbd_get_queue_data(q);
const struct ublk_io_data *data;
struct ublk_io_tgt *io;
u64 handle;
int tag, hwq;
unsigned ublk_op;
int ret = -EINVAL;
if (cqe->res < 0) {
nbd_err("%s %d: reply cqe %d\n", __func__,
__LINE__, cqe->res);
ret = cqe->res;
goto fail;
} else if (cqe->res == 0 && !nbd_data->done) {
//return 0;
nbd_err("%s %d: zero reply cqe %d %llx\n", __func__,
__LINE__, cqe->res, cqe->user_data);
}
if (ntohl(q_data->reply.magic) != NBD_REPLY_MAGIC) {
nbd_err("%s %d: reply bad magic %x res %d\n",
__func__, __LINE__,
ntohl(q_data->reply.magic), cqe->res);
ret = -EPROTO;
goto fail;
}
if (cqe->res + nbd_data->done != sizeof(struct nbd_reply)) {
nbd_err("%s %d: bad reply cqe %d %llx, done %u\n",
__func__, __LINE__,
cqe->res, cqe->user_data,
nbd_data->done);
}
ublk_assert(cqe->res + nbd_data->done == sizeof(struct nbd_reply));
memcpy(&handle, q_data->reply.handle, sizeof(handle));
tag = nbd_handle_to_tag(handle);
hwq = ublk_unique_tag_to_hwq(tag);
tag = ublk_unique_tag_to_tag(tag);
if (tag >= q->q_depth) {
nbd_err("%s %d: tag is too big %d\n", __func__,
__LINE__, tag);
goto fail;
}
if (hwq != q->q_id) {
nbd_err("%s %d: hwq is too big %d\n", __func__,
__LINE__, hwq);
goto fail;
}
data = ublksrv_queue_get_io_data(q, tag);
io = __ublk_get_io_tgt_data(data);
nbd_data = io_tgt_to_nbd_data(io);
if (nbd_data->cmd_cookie != nbd_handle_to_cookie(handle)) {
nbd_err("%s %d: cookie not match tag %d: %x %lx\n",
__func__, __LINE__, data->tag,
nbd_data->cmd_cookie, handle);
goto fail;
}
ublk_op = ublksrv_get_op(data->iod);
if (ublk_op == UBLK_IO_OP_READ) {
*io_data = data;
return 1;
} else {
int err = ntohl(q_data->reply.error);
struct io_uring_cqe fake_cqe;
NBD_IO_DBG("%s: got write reply, tag %d res %d\n",
__func__, data->tag, err);
if (err) {
fake_cqe.res = -EIO;
} else {
if (ublk_op == UBLK_IO_OP_WRITE)
fake_cqe.res = data->iod->nr_sectors << 9;
else
fake_cqe.res = 0;
}
io->tgt_io_cqe = &fake_cqe;
io->co.resume();
return 0;
}
fail:
return ret;
}
static void __nbd_resume_read_req(const struct ublk_io_data *data,
const struct io_uring_cqe *cqe, unsigned done)
{
struct ublk_io_tgt *io = __ublk_get_io_tgt_data(data);
struct nbd_io_data *nbd_data = io_tgt_to_nbd_data(io);
nbd_data->done = done;
io->tgt_io_cqe = cqe;
io->co.resume();
}
/* recv completion drives the whole IO flow */
static inline int nbd_start_recv(const struct ublksrv_queue *q,
struct nbd_io_data *nbd_data, void *buf, int len,
unsigned done, const struct ublk_io_data *data)
{
struct nbd_queue_data *q_data = nbd_get_queue_data(q);
struct io_uring_sqe *sqe, *sqe2;
bool reply = (data == NULL);
unsigned int op = reply ? NBD_OP_READ_REPLY : UBLK_IO_OP_READ;
unsigned int tag = q->q_depth; //recv always use this extra tag
nbd_data->done = done;
if (!use_zc || reply) {
ublk_get_sqe_pair(q->ring_ptr, &sqe, NULL);
io_uring_prep_recv(sqe, q->q_id + 1, (char *)buf + done, len - done, MSG_WAITALL);
io_uring_sqe_set_flags(sqe, IOSQE_FIXED_FILE);
} else {
ublk_get_sqe_pair(q->ring_ptr, &sqe2, &sqe);
io_uring_prep_recv_ublk_zc(sqe2, sqe, 0, data->tag, q->q_id, q->q_id + 1,
buf, done, len - done);
}
/* bit63 marks us as tgt io */
sqe->user_data = build_user_data(tag, op, 0, 1);
ublk_assert(q_data->in_flight_ios);
NBD_IO_DBG("%s: q_inflight %d queue recv %s"
"(qid %d tag %u, target: %d, user_data %llx)\n",
__func__, q_data->in_flight_ios, reply ? "reply" : "io",
q->q_id, tag, 1, sqe->user_data);
return 0;
}
/*
* Submit recv worker for reading nbd reply or read io data
*
* return value:
*
* 0 : queued via io_uring
* len : data read already, must be same with len
* < 0 : failure
*/
static int nbd_do_recv(const struct ublksrv_queue *q,
struct nbd_io_data *nbd_data, int fd,
void *buf, unsigned len, const struct ublk_io_data *data)
{
unsigned msg_flags = MSG_DONTWAIT | MSG_WAITALL;
int i = 0, done = 0;
int loops = len < 512 ? 16 : 32;
int ret;
if (use_zc && data)
loops = 0;
while (i++ < loops && done < len) {
ret = recv(fd, (char *)buf + done, len - done, msg_flags);
if (ret > 0)
done += ret;
if (!done)
break;
}
if (done == len)
return done;
NBD_IO_DBG("%s: sync(non-blocking) recv %d(%s)/%d/%u\n",
__func__, ret, strerror(errno), done, len);
ret = nbd_start_recv(q, nbd_data, buf, len, done, data);
return ret;
}
/*
* Every request will be responded with one reply, and we complete the
* request after the reply is received.
*
* Read request is a bit special, since the data returned are received
* with the reply together, so we have to handle read IO data here.
*/
static co_io_job __nbd_handle_recv(const struct ublksrv_queue *q,
const struct ublk_io_data *data, struct ublk_io_tgt *io)
{
struct nbd_io_data *nbd_data = io_tgt_to_nbd_data(io);
struct nbd_queue_data *q_data = nbd_get_queue_data(q);
int fd = q->dev->tgt.fds[q->q_id + 1];
unsigned int len;
u64 cqe_buf[2] = {0};
struct io_uring_cqe *fake_cqe = (struct io_uring_cqe *)cqe_buf;
q_data->recv_started = 1;
while (q_data->in_flight_ios > 0) {
const struct ublk_io_data *io_data = NULL;
int ret;
read_reply:
ret = nbd_do_recv(q, nbd_data, fd, &q_data->reply,
sizeof(q_data->reply), NULL);
if (ret == sizeof(q_data->reply)) {
nbd_data->done = ret;
fake_cqe->res = 0;
io->tgt_io_cqe = fake_cqe;
goto handle_recv;
} else if (ret < 0)
break;
co_await__suspend_always(data->tag);
if (io->tgt_io_cqe->res == -EAGAIN)
goto read_reply;
handle_recv:
ret = nbd_handle_recv_reply(q, nbd_data, io->tgt_io_cqe, &io_data);
if (ret < 0)
break;
if (!ret)
continue;
read_io:
ublk_assert(io_data != NULL);
len = io_data->iod->nr_sectors << 9;
ret = nbd_do_recv(q, nbd_data, fd, (void *)io_data->iod->addr, len,
io_data);
if (ret == len) {
nbd_data->done = ret;
fake_cqe->res = 0;
io->tgt_io_cqe = fake_cqe;
goto handle_read_io;
} else if (ret < 0)
break;
/* still wait on recv coroutine context */
co_await__suspend_always(data->tag);
ret = io->tgt_io_cqe->res;
if (ret == -EAGAIN)
goto read_io;
handle_read_io:
__nbd_resume_read_req(io_data, io->tgt_io_cqe, nbd_data->done);
}
q_data->recv_started = 0;
co_return;
}
static int nbd_handle_io_async(const struct ublksrv_queue *q,
const struct ublk_io_data *data)
{
struct ublk_io_tgt *io = __ublk_get_io_tgt_data(data);
struct nbd_queue_data *q_data = nbd_get_queue_data(q);
/*
* Put the io in the queue and submit them after
* the current chain becomes idle.
*/
if (q_data->send_sqe_chain_busy)
q_data->next_chain.push_back(data);
else
io->co = __nbd_handle_io_async(q, data, io);
return 0;
}
/*
* Don't touch @data because the pointed ublk io request may have been
* completed before this send cqe is handled. And ublk io request completion
* is triggered by reply received from nbd server.
*/
static void nbd_send_req_done(const struct ublksrv_queue *q,
const struct ublk_io_data *data,
const struct io_uring_cqe *cqe)
{
struct nbd_queue_data *q_data = nbd_get_queue_data(q);
unsigned ublk_op = user_data_to_op(cqe->user_data);
int tag = user_data_to_tag(cqe->user_data);
unsigned int nr_sects = user_data_to_tgt_data(cqe->user_data);
unsigned total;
/* nothing to do for send_zc notification */
if (cqe->flags & IORING_CQE_F_NOTIF)
return;
ublk_assert(q_data->chained_send_ios);
if (!--q_data->chained_send_ios) {
if (q_data->send_sqe_chain_busy)
q_data->send_sqe_chain_busy = 0;
}
/*
* In case of failure, how to tell recv work to handle the
* request? So far just warn it, maybe nbd server will
* send one err reply.
*/
if (cqe->res < 0)
nbd_err("%s: tag %d cqe fail %d %llx\n",
__func__, tag, cqe->res, cqe->user_data);
total = sizeof(nbd_request);
/*
* We have set MSG_WAITALL, so short send shouldn't be possible,
* but just warn in case of io_uring regression
*/
if (ublk_op == UBLK_IO_OP_WRITE) {
if (!use_zc)
total += (nr_sects << 9);
else
total = (nr_sects << 9);
}
if (cqe->res < total)
nbd_err("%s: short send/receive tag %d op %d %llx, len %u written %u cqe flags %x\n",
__func__, tag, ublk_op, cqe->user_data,
total, cqe->res, cqe->flags);
}
static void nbd_tgt_io_done(const struct ublksrv_queue *q,
const struct ublk_io_data *data,
const struct io_uring_cqe *cqe)
{
int tag = user_data_to_tag(cqe->user_data);
ublk_assert(tag == data->tag);
#if NBD_DEBUG_CQE == 1
struct nbd_queue_data *q_data = nbd_get_queue_data(q);
nbd_err("%s: tag %d queue(ios %u %u) cqe(res %d flags %x user data %llx)\n",
__func__, tag,
q_data->in_flight_ios, q_data->chained_send_ios,
cqe->res, cqe->flags, cqe->user_data);
#endif
/* both reply and read io is done in recv io coroutine */
if (is_recv_io(q, data)) {
struct nbd_queue_data *q_data = nbd_get_queue_data(q);
/*
* Delay recv data handling into nbd_handle_io_bg(), so
* any recv sqe won't cut in the send sqe chain.
*
* So far, recv is strictly serialized, so saving
* this single cqe works; in the future, if
* recv becomes batched, here has to be fixed
*/
q_data->recv_cqe = *cqe;
q_data->need_handle_recv = 1;
return;
}
nbd_send_req_done(q, data, cqe);
}
static void nbd_mark_chain_tail(struct nbd_queue_data *q_data)
{
struct io_uring_sqe *sqe = q_data->last_send_sqe;
if (sqe) {
sqe->flags &= ~IOSQE_IO_LINK;
q_data->last_send_sqe = NULL;
}
}
static void nbd_handle_send_bg(const struct ublksrv_queue *q,
struct nbd_queue_data *q_data)
{
if (!q_data->send_sqe_chain_busy) {
std::vector<const struct ublk_io_data *> &ios =
q_data->next_chain;
for (auto it = ios.cbegin(); it != ios.cend(); ++it) {
auto data = *it;
struct ublk_io_tgt *io = __ublk_get_io_tgt_data(data);
ublk_assert(data->tag < q->q_depth);
io->co = __nbd_handle_io_async(q, data, io);
}
ios.clear();
if (q_data->chained_send_ios && !q_data->send_sqe_chain_busy)
q_data->send_sqe_chain_busy = 1;
}
nbd_mark_chain_tail(q_data);
}
static void nbd_handle_recv_bg(const struct ublksrv_queue *q,
struct nbd_queue_data *q_data)
{
if (q_data->in_flight_ios && !q_data->recv_started) {
const struct ublk_io_data *data =
ublksrv_queue_get_io_data(q, q->q_depth);
struct ublk_io_tgt *io = __ublk_get_io_tgt_data(data);
ublk_assert(data->tag == q->q_depth);
io->co = __nbd_handle_recv(q, data, io);
}
/* reply or read io data is comming */
if (q_data->need_handle_recv) {
const struct ublk_io_data *data =
ublksrv_queue_get_io_data(q, q->q_depth);
struct ublk_io_tgt *io = __ublk_get_io_tgt_data(data);
ublk_assert(data->tag == q->q_depth);
io->tgt_io_cqe = &q_data->recv_cqe;
io->co.resume();
q_data->need_handle_recv = 0;
}
}
static void __nbd_handle_io_bg(const struct ublksrv_queue *q,
struct nbd_queue_data *q_data)
{
nbd_handle_send_bg(q, q_data);
/* stop to queue send now since we need to recv now */
if (q_data->chained_send_ios && !q_data->send_sqe_chain_busy)
q_data->send_sqe_chain_busy = 1;
/*
* recv SQE can't cut in send SQE chain, so it has to be
* moved here after the send SQE chain is built
*
* Also queuing ublk io command may allocate sqe too.
*/
nbd_handle_recv_bg(q, q_data);
}
/*
* The initial send request batch should be in same send sqe batch, before
* this batch isn't done, all new send requests are staggered into next_chain
* which will be flushed after the current chain is completed.
*
* Also recv work is always started after send requests are queued, because
* the recv sqe may cut the send sqe chain, and the ublk io cmd sqe may cut
* the send sqe chain too.
*
* This is why nbd_handle_recv_bg() always follows nbd_handle_send_bg().
*/
static void nbd_handle_io_bg(const struct ublksrv_queue *q, int nr_queued_io)
{
struct nbd_queue_data *q_data = nbd_get_queue_data(q);
NBD_IO_DBG("%s: pending ios %d/%d chain_busy %d next_chain %ld recv(%d) sqes %u\n",
__func__, q_data->in_flight_ios,
q_data->chained_send_ios,
q_data->send_sqe_chain_busy,
q_data->next_chain.size(),
q_data->recv_started,
nr_queued_io);
__nbd_handle_io_bg(q, q_data);
/*
* io can be completed in recv work since we do sync recv, so
* io could be completed before the send seq's cqe is returned.
*
* When this happens, simply clear chain busy, so that we can
* queue more requests.
*/
if (!q_data->in_flight_ios && q_data->send_sqe_chain_busy) {
/* all inflight ios are done, so it is safe to send request */
q_data->send_sqe_chain_busy = 0;
if (!q_data->next_chain.empty())
__nbd_handle_io_bg(q, q_data);
}
if (!q_data->recv_started && !q_data->send_sqe_chain_busy &&
!q_data->next_chain.empty())
nbd_err("%s: hang risk: pending ios %d/%d\n",
__func__, q_data->in_flight_ios,
q_data->chained_send_ios);
}
static void nbd_usage_for_add(void)
{
printf(" nbd: --host=$HOST [--port=$PORT] | --unix=$UNIX_PATH\n");
}
static int nbd_init_queue(const struct ublksrv_queue *q,
void **queue_data_ptr)
{
struct nbd_queue_data *data =
(struct nbd_queue_data *)calloc(sizeof(*data), 1);
struct nbd_tgt_data *ddata = (struct nbd_tgt_data*)q->dev->tgt.tgt_data;
if (!data)
return -ENOMEM;
data->next_chain.clear();
data->use_send_zc = ddata->unix_sock ? false : ddata->use_send_zc;
data->use_unix_sock = ddata->unix_sock;
data->recv_started = 0;
//nbd_err("%s send zc %d\n", __func__, data->use_send_zc);
*queue_data_ptr = (void *)data;
return 0;
}
static void nbd_deinit_queue(const struct ublksrv_queue *q)
{
struct nbd_queue_data *data = nbd_get_queue_data(q);
free(data);
}
static void nbd_deinit_tgt(const struct ublksrv_dev *dev)
{
const struct ublksrv_tgt_info *tgt = &dev->tgt;
const struct ublksrv_ctrl_dev_info *info =
ublksrv_ctrl_get_dev_info(ublksrv_get_ctrl_dev(dev));
int i;
free(tgt->tgt_data);
for (i = 0; i < info->nr_hw_queues; i++) {
int fd = tgt->fds[i + 1];
shutdown(fd, SHUT_RDWR);
close(fd);
}
}
static int nbd_setup_tgt(struct ublksrv_dev *dev, int type, bool recovery,
const char *jbuf, uint16_t *flags)
{
struct ublksrv_tgt_info *tgt = &dev->tgt;
const struct ublksrv_ctrl_dev_info *info =
ublksrv_ctrl_get_dev_info(ublksrv_get_ctrl_dev(dev));
int i;
struct nbd_tgt_data *data = (struct nbd_tgt_data *)dev->tgt.tgt_data;
const char *port = NBD_DEFAULT_PORT;
uint16_t needed_flags = 0;
uint32_t cflags = NBD_FLAG_C_FIXED_NEWSTYLE;
char host_name[NBD_MAX_NAME] = {0};
char exp_name[NBD_MAX_NAME] = {0};
char unix_path[NBD_MAX_NAME] = {0};
u64 size64 = 0;
bool can_opt_go = true;
/* todo: support tls */
char *certfile = NULL;
char *keyfile = NULL;
char *cacertfile = NULL;
char *tlshostname = NULL;
bool tls = false;
long send_zc = 0;
ublk_assert(jbuf);
ublk_assert(type == UBLKSRV_TGT_TYPE_NBD);
ublk_assert(!recovery || info->state == UBLK_S_DEV_QUIESCED);
ublksrv_json_read_target_str_info(jbuf, NBD_MAX_NAME, "host",
host_name);
ublksrv_json_read_target_str_info(jbuf, NBD_MAX_NAME, "unix",
unix_path);
ublksrv_json_read_target_str_info(jbuf, NBD_MAX_NAME, "export_name",
exp_name);
ublksrv_json_read_target_ulong_info(jbuf, "send_zc", &send_zc);
NBD_HS_DBG("%s: host %s unix %s exp_name %s send_zc %ld\n", __func__,
host_name, unix_path, exp_name, send_zc);
for (i = 0; i < info->nr_hw_queues; i++) {
int sock;
unsigned int opts = 0;
if (strlen(unix_path))
sock = openunix(unix_path);
else
sock = opennet(host_name, port, false);
if (sock >= 0)
negotiate(&sock, &size64, flags, exp_name,
needed_flags, cflags, opts, certfile,
keyfile, cacertfile, tlshostname, tls,
can_opt_go);
else
ublk_err("%s: open socket failed %d\n", __func__, sock);
tgt->fds[i + 1] = sock;
NBD_HS_DBG("%s:qid %d %s-%s size %luMB flags %x sock %d\n",
__func__, i, host_name, port,
size64 >> 20, *flags, sock);
}
tgt->dev_size = size64;
use_zc = info->flags & UBLK_F_SUPPORT_ZERO_COPY;
/*
* one extra slot for receiving reply & read io, so
* the preferred queue depth should be 127 or 255,
* then half of SQ memory consumption can be saved
* especially we use IORING_SETUP_SQE128
*/
if (!use_zc)
tgt->tgt_ring_depth = info->queue_depth + 1;
else
tgt->tgt_ring_depth = (info->queue_depth + 1) * 3;
tgt->nr_fds = info->nr_hw_queues;
tgt->extra_ios = 1; //one extra slot for receiving nbd reply
data->unix_sock = strlen(unix_path) > 0 ? true : false;
data->use_send_zc = !!send_zc;
tgt->io_data_size = sizeof(struct ublk_io_tgt) +
sizeof(struct nbd_io_data);
ublksrv_dev_set_cq_depth(dev, 2 * tgt->tgt_ring_depth);