forked from yandex-cloud/yc-libvhost-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvdev.c
2267 lines (1889 loc) · 62.3 KB
/
vdev.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
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <sys/stat.h>
#include <sys/timerfd.h>
#include <sys/un.h>
#include <sys/mman.h>
#include <pthread.h>
#include <inttypes.h>
#include "vdev.h"
#include "server_internal.h"
#include "logging.h"
#include "memmap.h"
#include "memlog.h"
#define VHOST_REQ(req) [VHOST_USER_ ## req] = #req
static const char *const vhost_req_names[] = {
VHOST_REQ(GET_FEATURES),
VHOST_REQ(SET_FEATURES),
VHOST_REQ(SET_OWNER),
VHOST_REQ(RESET_OWNER),
VHOST_REQ(SET_MEM_TABLE),
VHOST_REQ(SET_LOG_BASE),
VHOST_REQ(SET_LOG_FD),
VHOST_REQ(SET_VRING_NUM),
VHOST_REQ(SET_VRING_ADDR),
VHOST_REQ(SET_VRING_BASE),
VHOST_REQ(GET_VRING_BASE),
VHOST_REQ(SET_VRING_KICK),
VHOST_REQ(SET_VRING_CALL),
VHOST_REQ(SET_VRING_ERR),
VHOST_REQ(GET_PROTOCOL_FEATURES),
VHOST_REQ(SET_PROTOCOL_FEATURES),
VHOST_REQ(GET_QUEUE_NUM),
VHOST_REQ(SET_VRING_ENABLE),
VHOST_REQ(SEND_RARP),
VHOST_REQ(NET_SET_MTU),
VHOST_REQ(SET_SLAVE_REQ_FD),
VHOST_REQ(IOTLB_MSG),
VHOST_REQ(SET_VRING_ENDIAN),
VHOST_REQ(GET_CONFIG),
VHOST_REQ(SET_CONFIG),
VHOST_REQ(CREATE_CRYPTO_SESSION),
VHOST_REQ(CLOSE_CRYPTO_SESSION),
VHOST_REQ(POSTCOPY_ADVISE),
VHOST_REQ(POSTCOPY_LISTEN),
VHOST_REQ(POSTCOPY_END),
VHOST_REQ(GET_INFLIGHT_FD),
VHOST_REQ(SET_INFLIGHT_FD),
VHOST_REQ(GET_MAX_MEM_SLOTS),
VHOST_REQ(ADD_MEM_REG),
VHOST_REQ(REM_MEM_REG),
};
#undef VHOST_REQ
static const char *vhost_req_name(uint32_t req)
{
if (req >= sizeof(vhost_req_names) / sizeof(vhost_req_names[0]) ||
!vhost_req_names[req]) {
return "**UNKNOWN**";
}
return vhost_req_names[req];
}
static LIST_HEAD(, vhd_vdev) g_vdevs = LIST_HEAD_INITIALIZER(g_vdevs);
static uint16_t vring_idx(struct vhd_vring *vring)
{
return vring - vring->vdev->vrings;
}
struct vhd_request_queue *vhd_get_rq_for_vring(struct vhd_vring *vring)
{
struct vhd_vdev *vdev = vring->vdev;
return vdev->rqs[vring_idx(vring) % vdev->num_rqs];
}
static void replace_fd(int *fd, int newfd)
{
if (*fd >= 0) {
close(*fd);
}
*fd = newfd;
}
/* Return size of per queue inflight buffer. */
static size_t vring_inflight_buf_size(uint16_t num)
{
return sizeof(struct inflight_split_region) +
num * sizeof(struct inflight_split_desc);
}
static int vring_kick(void *opaque)
{
int ret;
struct vhd_vring *vring = opaque;
struct vhd_vdev *vdev = vring->vdev;
/*
* Clear vring event now, before processing virtq.
* Otherwise we might lose events if guest has managed to
* signal eventfd again while we were processing
*/
vhd_clear_eventfd(vring->kickfd);
if (!vring->vq.enabled) {
return 0;
}
ret = vdev->type->dispatch_requests(vdev, vring);
if (ret < 0) {
/*
* seems like full-fledged vring stop may surprize the client, so just
* disable notifications and effectively suspend the vring
*/
VHD_OBJ_ERROR(vring, "dispatch_requests: %s, suspending vring",
strerror(-ret));
vhd_detach_io_handler(vring->kick_handler);
}
return 0;
}
/*
* Resolve (and thus validate) the addresses used by the virtq, and record them
* in the shadow structure, in the control event loop, to be later propagated
* into the actual virtq in the dataplane.
*/
static int vring_update_shadow_vq_addrs(struct vhd_vring *vring,
struct vhd_memory_map *mm)
{
void *desc = uva_to_ptr(mm, vring->shadow_vq.desc_addr);
void *used = uva_to_ptr(mm, vring->shadow_vq.used_addr);
void *avail = uva_to_ptr(mm, vring->shadow_vq.avail_addr);
if (!desc || !used || !avail) {
VHD_OBJ_ERROR(vring, "failed to resolve vring addresses "
"(0x%" PRIx64 ", 0x%" PRIx64 ", 0x%" PRIx64 ")",
vring->shadow_vq.desc_addr, vring->shadow_vq.used_addr,
vring->shadow_vq.avail_addr);
return -EINVAL;
}
vring->shadow_vq.desc = desc;
vring->shadow_vq.used = used;
vring->shadow_vq.avail = avail;
vring->shadow_vq.mm = mm;
return 0;
}
static void vring_sync_to_virtq(struct vhd_vring *vring)
{
bool should_kick;
vring->vq.flags = vring->shadow_vq.flags;
vring->vq.desc = vring->shadow_vq.desc;
vring->vq.used = vring->shadow_vq.used;
vring->vq.avail = vring->shadow_vq.avail;
vring->vq.used_gpa_base = vring->shadow_vq.used_gpa_base;
vring->vq.mm = vring->shadow_vq.mm;
vring->vq.log = vring->shadow_vq.log;
/*
* Since QEMU sets kickfd before enabling the vring, it gets kicked while
* still in the disabled state. Kick manually after enabling here just
* in case.
*/
should_kick = !vring->vq.enabled && vring->shadow_vq.enabled;
vring->vq.enabled = vring->shadow_vq.enabled;
virtq_set_notify_fd(&vring->vq, vring->callfd);
if (should_kick) {
vhd_set_eventfd(vring->kickfd);
}
}
/*
* There are several counters of vrings in particular state:
*
* ->num_vrings_handling_msg
* counts vrings that are performing some state transitions in response to a
* client message; once it drops to zero, the handling of this message is
* finished, the reply is sent if necessary, and the device resumes
* accepting further messages
*
* ->num_vrings_started
* counts vrings that have been started and haven't acknowledged being
* stopped yet; once it drops to zero, the device is safe to assume no more
* requests will be submitted to the backend, and therefore release the
* semaphore and let vhd_vdev_stop_server return
*
* ->num_vrings_in_flight
* counts vrings that have any potential to have requests in flight: it's
* incremented when a vring is started and decremented when a stopped vring
* reports there are no requests remaining in flight
*/
static void vring_handle_msg(struct vhd_vring *vring,
void (*handler_bh)(void *))
{
struct vhd_vdev *vdev = vring->vdev;
if (!vring->started_in_ctl) {
return;
}
vdev->num_vrings_handling_msg++;
vhd_run_in_rq(vhd_get_rq_for_vring(vring), handler_bh, vring);
}
static void vdev_disconnect(struct vhd_vdev *vdev);
static void vring_mark_msg_handled(struct vhd_vring *vring)
{
struct vhd_vdev *vdev = vring->vdev;
VHD_ASSERT(vdev->num_vrings_handling_msg);
vdev->num_vrings_handling_msg--;
if (!vdev->num_vrings_handling_msg) {
int ret = vdev->handle_complete(vdev);
vdev->handle_complete = NULL;
if (ret < 0) {
vdev_disconnect(vdev);
}
}
}
static void vring_mark_msg_handled_bh(void *opaque)
{
vring_mark_msg_handled(opaque);
}
static void vdev_vrings_stopped(struct vhd_vdev *vdev);
static void vdev_drained(struct vhd_vdev *vdev);
static bool vdev_in_use(struct vhd_vdev *vdev)
{
return vdev->num_vrings_in_flight || vdev->num_vrings_handling_msg ||
vdev->num_vrings_started || vdev->conn_handler;
}
static void vdev_maybe_finished(struct vhd_vdev *vdev)
{
if (!vdev->num_vrings_started) {
vdev_vrings_stopped(vdev);
}
if (!vdev_in_use(vdev)) {
vdev_drained(vdev);
}
}
static void vring_mark_stopped(struct vhd_vring *vring)
{
struct vhd_vdev *vdev = vring->vdev;
VHD_OBJ_INFO(vring, "stopped vring with %u in-flight requests",
vring->num_in_flight_at_stop);
replace_fd(&vring->kickfd, -1);
VHD_ASSERT(vdev->num_vrings_started);
vdev->num_vrings_started--;
vdev_maybe_finished(vdev);
}
static void vring_mark_stopped_bh(void *opaque)
{
vring_mark_stopped(opaque);
}
static void vring_reset(struct vhd_vring *vring)
{
replace_fd(&vring->callfd, -1);
replace_fd(&vring->kickfd, -1);
replace_fd(&vring->errfd, -1);
memset(&vring->shadow_vq, 0, sizeof(vring->shadow_vq));
vring->num_in_flight_at_stop = 0;
vring->disconnecting = false;
}
static void vring_mark_drained(struct vhd_vring *vring)
{
struct vhd_vdev *vdev = vring->vdev;
VHD_ASSERT(vring->started_in_ctl);
vring->started_in_ctl = false;
if (vring->on_drain_cb) {
int ret = vring->on_drain_cb(vring);
vring->on_drain_cb = NULL;
if (ret < 0) {
vdev_disconnect(vdev);
}
}
virtio_virtq_release(&vring->vq);
VHD_ASSERT(vdev->num_vrings_in_flight);
vdev->num_vrings_in_flight--;
vdev_maybe_finished(vdev);
}
static void vring_mark_drained_bh(void *opaque)
{
vring_mark_drained(opaque);
}
void vhd_vring_inc_in_flight(struct vhd_vring *vring)
{
vring->num_in_flight++;
}
void vhd_vring_dec_in_flight(struct vhd_vring *vring)
{
vring->num_in_flight--;
if (!vring->num_in_flight && !vring->started_in_rq) {
vhd_run_in_ctl(vring_mark_drained_bh, vring);
}
}
static void vring_stop_bh(void *opaque)
{
struct vhd_vring *vring = opaque;
if (!vring->started_in_rq) {
return;
}
vhd_del_io_handler(vring->kick_handler);
vring->kick_handler = NULL;
/*
* FIXME: if the vring is stopped on request from the client via
* GET_VRING_BASE message (as opposed to a disconnect), the requests have
* to run through the backend because inflight requests aren't migrated by
* QEMU yet. Once this is fixed (with the help of the inflight region
* migration), the requests should be canceled here unconditionally,
* speeding up vm migration and shutdown.
*/
if (vring->disconnecting) {
vhd_cancel_queued_requests(vhd_get_rq_for_vring(vring), vring);
}
vring->started_in_rq = false;
vring->num_in_flight_at_stop = vring->num_in_flight;
vhd_run_in_ctl(vring_mark_stopped_bh, vring);
if (!vring->num_in_flight) {
vhd_run_in_ctl(vring_mark_drained_bh, vring);
}
}
static void vring_disconnect(struct vhd_vring *vring)
{
if (vring->started_in_ctl) {
/*
* If vring_start_bh gets reordered with vring_stop_bh, make sure it
* doesn't actually start vring.
*/
vring->disconnecting = true;
vhd_run_in_rq(vhd_get_rq_for_vring(vring), vring_stop_bh, vring);
}
}
/*
* Receive and store the message from the socket. Fill in the file
* descriptor array. Return number of bytes received or
* negative error code in case of error.
*/
static ssize_t net_recv_msg(int fd, struct vhost_user_msg_hdr *hdr,
void *payload, size_t len,
int *fds, size_t *num_fds)
{
ssize_t ret;
ssize_t rlen;
struct cmsghdr *cmsg;
int fds_rcvd[VHOST_USER_MAX_FDS];
size_t num_fds_rcvd = 0;
size_t i;
union {
char buf[CMSG_SPACE(sizeof(fds_rcvd))];
struct cmsghdr cmsg_align;
} control;
struct iovec iov = {
.iov_base = hdr,
.iov_len = sizeof(*hdr),
};
struct msghdr msgh = {
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = &control,
.msg_controllen = sizeof(control),
};
do {
ret = recvmsg(fd, &msgh, MSG_CMSG_CLOEXEC);
} while (ret < 0 && errno == EINTR);
if (ret == 0) {
goto out;
}
if (ret < 0) {
ret = -errno;
VHD_LOG_ERROR("recvmsg: %s", strerror(-ret));
goto out;
}
for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg; cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
if ((cmsg->cmsg_level == SOL_SOCKET) &&
(cmsg->cmsg_type == SCM_RIGHTS)) {
num_fds_rcvd = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
memcpy(fds_rcvd, CMSG_DATA(cmsg), num_fds_rcvd * sizeof(int));
break;
}
}
if (ret != sizeof(*hdr)) {
VHD_LOG_ERROR("recvmsg() read %zd expected %lu", ret, sizeof(*hdr));
ret = -EIO;
goto out;
}
if (hdr->size > len) {
VHD_LOG_ERROR("payload size %d exceeds buffer size %zu", hdr->size,
len);
ret = -EMSGSIZE;
goto out;
}
do {
rlen = read(fd, payload, hdr->size);
} while (rlen < 0 && errno == EINTR);
if (rlen < 0) {
ret = -errno;
VHD_LOG_ERROR("payload read failed: %s", strerror(-ret));
goto out;
}
if ((size_t)rlen != hdr->size) {
VHD_LOG_ERROR("payload read %zd, expected %d", rlen, hdr->size);
ret = -EIO;
goto out;
}
ret += rlen;
out:
if (ret <= 0) {
*num_fds = 0;
}
if (*num_fds > num_fds_rcvd) {
*num_fds = num_fds_rcvd;
}
memcpy(fds, fds_rcvd, *num_fds * sizeof(int));
for (i = *num_fds; i < num_fds_rcvd; i++) {
close(fds_rcvd[i]);
}
return ret;
}
/*
* Send message to master. Return number of bytes sent or negative
* error code in case of error.
*/
static int net_send_msg(int fd, const struct vhost_user_msg_hdr *hdr,
const void *payload, int *fds, size_t num_fds)
{
int ret;
struct iovec iov[] = {
{
.iov_base = (void *)hdr,
.iov_len = sizeof(*hdr),
}, {
.iov_base = (void *)payload,
.iov_len = hdr->size,
}
};
struct msghdr msgh = {
.msg_iov = iov,
.msg_iovlen = 2,
};
union {
char buf[CMSG_SPACE(sizeof(int) * VHOST_USER_MAX_FDS)];
struct cmsghdr cmsg_align;
} control;
struct cmsghdr *cmsgh;
if (num_fds > VHOST_USER_MAX_FDS) {
VHD_LOG_ERROR("too many fds: %zu", num_fds);
return -EMSGSIZE;
}
if (num_fds) {
size_t fdsize = sizeof(*fds) * num_fds;
msgh.msg_control = &control;
msgh.msg_controllen = CMSG_SPACE(fdsize);
cmsgh = CMSG_FIRSTHDR(&msgh);
cmsgh->cmsg_len = CMSG_LEN(fdsize);
cmsgh->cmsg_level = SOL_SOCKET;
cmsgh->cmsg_type = SCM_RIGHTS;
memcpy(CMSG_DATA(cmsgh), fds, fdsize);
}
do {
ret = sendmsg(fd, &msgh, MSG_NOSIGNAL);
} while (ret < 0 && errno == EINTR);
if (ret < 0) {
ret = -errno;
VHD_LOG_ERROR("sendmsg: %s", strerror(-ret));
return ret;
}
if ((unsigned)ret != sizeof(*hdr) + hdr->size) {
VHD_LOG_ERROR("sent %d wanted %zu", ret, sizeof(*hdr) + hdr->size);
return -EIO;
}
return ret;
}
/*
* Vhost protocol handling
*/
static const uint64_t g_default_features =
(1UL << VHOST_USER_F_PROTOCOL_FEATURES) |
(1UL << VHOST_F_LOG_ALL);
static const uint64_t g_default_protocol_features =
(1UL << VHOST_USER_PROTOCOL_F_MQ) |
(1UL << VHOST_USER_PROTOCOL_F_LOG_SHMFD) |
(1UL << VHOST_USER_PROTOCOL_F_REPLY_ACK) |
(1UL << VHOST_USER_PROTOCOL_F_CONFIG) |
(1UL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD) |
(1UL << VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS);
static inline bool has_feature(uint64_t features_qword, size_t feature_bit)
{
return features_qword & (1ull << feature_bit);
}
#define NSEC_PER_SEC 1000000000
#define NSEC_PER_MSEC 1000000
static void elapsed_time(struct vhd_vdev *vdev, struct timespec *et)
{
clock_gettime(CLOCK_MONOTONIC, et);
if (et->tv_nsec < vdev->msg_handling_started.tv_nsec) {
et->tv_sec -= 1;
et->tv_nsec += NSEC_PER_SEC;
}
et->tv_sec -= vdev->msg_handling_started.tv_sec;
et->tv_nsec -= vdev->msg_handling_started.tv_nsec;
}
static void arm_msg_handling_timer(struct vhd_vdev *vdev, int secs)
{
struct itimerspec itimer = {
.it_interval.tv_sec = secs,
.it_value.tv_sec = secs,
};
timerfd_settime(vdev->timerfd, 0, &itimer, NULL);
}
/* Every so many seconds report if the message is still being handled */
#define MSG_HANDLING_LOG_INTERVAL 30
#ifdef VHD_DEBUG
#define MSG_ELAPSED_NSEC_LOG_THRESHOLD 0
#else
/*
* This number must be under 1 sec as that's handled by a different
* field inside the timespec struct.
*/
#define MSG_ELAPSED_NSEC_LOG_THRESHOLD (500 * NSEC_PER_MSEC)
#endif
static void vdev_handle_start(struct vhd_vdev *vdev, uint32_t req,
bool ack_pending)
{
/* do not accept further messages until this one is fully handled */
vhd_detach_io_handler(vdev->conn_handler);
vdev->req = req;
vdev->ack_pending = ack_pending;
clock_gettime(CLOCK_MONOTONIC, &vdev->msg_handling_started);
VHD_OBJ_DEBUG(vdev, "%s (%u)", vhost_req_name(req), req);
vhd_attach_io_handler(vdev->timer_handler);
arm_msg_handling_timer(vdev, MSG_HANDLING_LOG_INTERVAL);
}
static void vdev_handle_finish(struct vhd_vdev *vdev)
{
struct timespec elapsed;
vhd_detach_io_handler(vdev->timer_handler);
arm_msg_handling_timer(vdev, 0);
elapsed_time(vdev, &elapsed);
if (elapsed.tv_sec || elapsed.tv_nsec > MSG_ELAPSED_NSEC_LOG_THRESHOLD) {
VHD_OBJ_INFO(vdev, "%s (%u): elapsed %jd.%03lds",
vhost_req_name(vdev->req), vdev->req,
(intmax_t) elapsed.tv_sec,
elapsed.tv_nsec / NSEC_PER_MSEC);
}
vdev->ack_pending = false;
vdev->req = VHOST_USER_NONE;
/* resume accepting further messages if still connected */
if (vdev->conn_handler) {
vhd_attach_io_handler(vdev->conn_handler);
}
}
static int vhost_send_fds(struct vhd_vdev *vdev,
const struct vhost_user_msg_hdr *hdr,
const void *payload,
int *fds, size_t num_fds,
bool handle_finish)
{
int len;
len = net_send_msg(vdev->connfd, hdr, payload, fds, num_fds);
if (len < 0) {
return len;
}
if (handle_finish) {
vdev_handle_finish(vdev);
}
return 0;
}
static int vhost_reply_fds(struct vhd_vdev *vdev,
const void *payload, uint32_t len,
int *fds, size_t num_fds)
{
struct vhost_user_msg_hdr hdr = {
.req = vdev->req,
.size = len,
.flags = VHOST_USER_MSG_FLAGS_REPLY,
};
return vhost_send_fds(vdev, &hdr, payload, fds, num_fds, true);
}
static int vhost_reply(struct vhd_vdev *vdev,
const void *payload, uint32_t len)
{
return vhost_reply_fds(vdev, payload, len, NULL, 0);
}
static int vhost_reply_u64(struct vhd_vdev *vdev, uint64_t u64)
{
return vhost_reply(vdev, &u64, sizeof(u64));
}
static int vhost_send_vring_base(struct vhd_vring *vring)
{
struct vhost_user_vring_state vrstate = {
.index = vring_idx(vring),
.num = vring->vq.last_avail,
};
return vhost_reply(vring->vdev, &vrstate, sizeof(vrstate));
}
static bool msg_ack_needed(struct vhd_vdev *vdev, uint32_t flags)
{
return has_feature(vdev->negotiated_protocol_features,
VHOST_USER_PROTOCOL_F_REPLY_ACK) &&
(flags & VHOST_USER_MSG_FLAGS_REPLY_ACK);
}
static int vhost_ack(struct vhd_vdev *vdev, int ret)
{
if (!vdev->ack_pending) {
vdev_handle_finish(vdev);
return 0;
}
return vhost_reply_u64(vdev, ret);
}
static int vhost_get_protocol_features(struct vhd_vdev *vdev,
const void *payload, size_t size,
const int *fds, size_t num_fds)
{
if (num_fds) {
VHD_OBJ_ERROR(vdev, "malformed message num_fds=%zu", num_fds);
return -EINVAL;
}
return vhost_reply_u64(vdev, vdev->supported_protocol_features);
}
static int vhost_set_protocol_features(struct vhd_vdev *vdev,
const void *payload, size_t size,
const int *fds, size_t num_fds)
{
const uint64_t *features = payload;
if (num_fds || size < sizeof(*features)) {
VHD_OBJ_ERROR(vdev, "malformed message size=%zu #fds=%zu", size,
num_fds);
return -EINVAL;
}
if (vdev->num_vrings_in_flight) {
VHD_OBJ_ERROR(vdev, "not allowed once vrings are started");
return -EISCONN;
}
if (*features & ~vdev->supported_protocol_features) {
VHD_OBJ_ERROR(vdev, "requested unsupported features 0x%" PRIx64,
*features & ~vdev->supported_protocol_features);
return -ENOTSUP;
}
vdev->negotiated_protocol_features = *features;
return vhost_ack(vdev, 0);
}
static int vhost_get_features(struct vhd_vdev *vdev, const void *payload,
size_t size, const int *fds, size_t num_fds)
{
if (num_fds) {
VHD_OBJ_ERROR(vdev, "malformed message num_fds=%zu", num_fds);
return -EINVAL;
}
vdev->supported_features = g_default_features |
vdev->type->get_features(vdev);
return vhost_reply_u64(vdev, vdev->supported_features);
}
static void update_shadow_vq_memlog(struct vhd_vdev *vdev)
{
uint16_t i;
struct vhd_memory_log *log =
has_feature(vdev->negotiated_features, VHOST_F_LOG_ALL) ?
vdev->memlog : NULL;
for (i = 0; i < vdev->num_queues; i++) {
vdev->vrings[i].shadow_vq.log = log;
}
}
static void vring_sync_to_virtq_bh(void *opaque)
{
struct vhd_vring *vring = opaque;
vring_sync_to_virtq(vring);
vhd_run_in_ctl(vring_mark_msg_handled_bh, vring);
}
static int set_features_complete(struct vhd_vdev *vdev)
{
return vhost_ack(vdev, 0);
}
static int vhost_set_features(struct vhd_vdev *vdev, const void *payload,
size_t size, const int *fds, size_t num_fds)
{
uint16_t i;
const uint64_t *features = payload;
bool has_event_idx = has_feature(*features, VIRTIO_F_RING_EVENT_IDX);
bool has_vring_enable = has_feature(*features, VHOST_USER_F_PROTOCOL_FEATURES);
uint64_t supported_features = vdev->supported_features;
uint64_t changed_features;
if (num_fds || size < sizeof(*features)) {
VHD_OBJ_ERROR(vdev, "malformed message size=%zu #fds=%zu", size,
num_fds);
return -EINVAL;
}
if (*features & ~supported_features) {
VHD_OBJ_ERROR(vdev, "requested unsupported features 0x%" PRIx64,
*features & ~supported_features);
return -ENOTSUP;
}
if (!vdev->num_vrings_in_flight) {
vdev->negotiated_features = *features;
for (i = 0; i < vdev->num_queues; i++) {
vdev->vrings[i].vq.has_event_idx = has_event_idx;
vdev->vrings[i].shadow_vq.enabled = !has_vring_enable;
vdev->vrings[i].vq.enabled = vdev->vrings[i].shadow_vq.enabled;
}
return set_features_complete(vdev);
}
/* only logging may be toggled in a started device */
changed_features =
(vdev->negotiated_features ^ *features) & ~(1ull << VHOST_F_LOG_ALL);
if (changed_features) {
VHD_OBJ_ERROR(vdev, "changing features 0x%" PRIx64
" not allowed once vrings are started",
changed_features);
return -EISCONN;
}
vdev->negotiated_features = *features;
update_shadow_vq_memlog(vdev);
vdev->handle_complete = set_features_complete;
for (i = 0; i < vdev->num_queues; i++) {
vring_handle_msg(&vdev->vrings[i], vring_sync_to_virtq_bh);
}
return 0;
}
static int vhost_set_owner(struct vhd_vdev *vdev, const void *payload,
size_t size, const int *fds, size_t num_fds)
{
if (num_fds) {
VHD_OBJ_ERROR(vdev, "malformed message num_fds=%zu", num_fds);
return -EINVAL;
}
return vhost_ack(vdev, 0);
}
static int set_mem_table_complete(struct vhd_vdev *vdev)
{
if (vdev->old_memmap) {
vhd_memmap_unref(vdev->old_memmap);
vdev->old_memmap = NULL;
}
return vhost_ack(vdev, 0);
}
static int vhost_set_mem_table(struct vhd_vdev *vdev, const void *payload,
size_t size, const int *fds, size_t num_fds)
{
int ret;
const struct vhost_user_mem_desc *desc = payload;
size_t exp_size = offsetof(struct vhost_user_mem_desc, regions) +
sizeof(desc->regions[0]) * num_fds;
struct vhd_memory_map *mm;
uint16_t i;
if (size < exp_size) {
VHD_OBJ_ERROR(vdev, "malformed message: size %zu expected %zu", size,
exp_size);
return -EMSGSIZE;
}
if (desc->nregions > VHOST_USER_MEM_REGIONS_MAX) {
VHD_OBJ_ERROR(vdev, "invalid #memory regions %u", desc->nregions);
return -EINVAL;
}
if (desc->nregions != num_fds) {
VHD_OBJ_ERROR(vdev, "#memory regions != #fds: %u != %zu",
desc->nregions, num_fds);
return -EINVAL;
}
mm = vhd_memmap_new(vdev->map_cb, vdev->unmap_cb);
for (i = 0; i < desc->nregions; i++) {
const struct vhost_user_mem_region *region = &desc->regions[i];
ret = vhd_memmap_add_slot(mm, region->guest_addr, region->user_addr,
region->size, fds[i], region->mmap_offset);
if (ret < 0) {
goto fail;
}
}
for (i = 0; i < vdev->num_queues; i++) {
if (!vdev->vrings[i].started_in_ctl) {
continue;
}
ret = vring_update_shadow_vq_addrs(&vdev->vrings[i], mm);
if (ret < 0) {
goto fail;
}
}
vdev->old_memmap = vdev->memmap;
vdev->memmap = mm;
if (!vdev->num_vrings_in_flight) {
return set_mem_table_complete(vdev);
}
vdev->handle_complete = set_mem_table_complete;
for (i = 0; i < vdev->num_queues; i++) {
vring_handle_msg(&vdev->vrings[i], vring_sync_to_virtq_bh);
}
return 0;
fail:
vhd_memmap_unref(mm);
return ret;
}
static int vhost_add_mem_reg(struct vhd_vdev *vdev, const void *payload,
size_t size, const int *fds, size_t num_fds)
{
int ret;
const struct vhost_user_mem_single_mem_desc *desc = payload;
const struct vhost_user_mem_region *region = &desc->region;
struct vhd_memory_map *mm = vdev->memmap;
bool can_add_inplace = false;
uint16_t i;
if (size < sizeof(*desc)) {
VHD_OBJ_ERROR(vdev, "malformed message: size %zu expected %zu",
size, sizeof(*desc));
return -EMSGSIZE;
}
if (num_fds != 1) {
VHD_OBJ_ERROR(vdev, "expected a region file descriptor");
return -EINVAL;
}
if (mm == NULL) {
mm = vhd_memmap_new(vdev->map_cb, vdev->unmap_cb);
} else {
can_add_inplace = vdev->num_vrings_in_flight == 0;
/*
* Slow path:
* The rings are already live, therefore we cannot touch their memory
* map here. All we can do is create a copy, modify it how we want,
* and then tell the rings to use it via a message to their event loop.
*/
if (!can_add_inplace) {
mm = vhd_memmap_dup(mm);
}
}
ret = vhd_memmap_add_slot(mm, region->guest_addr, region->user_addr,
region->size, fds[0], region->mmap_offset);
if (ret < 0) {
goto fail;
}
for (i = 0; i < vdev->num_queues; i++) {
if (!vdev->vrings[i].started_in_ctl) {
continue;
}
ret = vring_update_shadow_vq_addrs(&vdev->vrings[i], mm);
if (ret < 0) {
goto fail;
}
}
/*
* Fast path:
* The rings are not yet started, but a valid memory map
* structure already exists. In this case we can just modify
* it in-place and return right away without creating a copy
* of it.
*/
if (can_add_inplace) {
return vhost_ack(vdev, 0);
}
vdev->old_memmap = vdev->memmap;
vdev->memmap = mm;
if (!vdev->num_vrings_in_flight) {
return set_mem_table_complete(vdev);
}
vdev->handle_complete = set_mem_table_complete;
for (i = 0; i < vdev->num_queues; i++) {
vring_handle_msg(&vdev->vrings[i], vring_sync_to_virtq_bh);
}
return 0;
fail:
if (!can_add_inplace) {