forked from nutanix/libvfio-user
-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.c
1308 lines (1114 loc) · 40.5 KB
/
client.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) 2020 Nutanix Inc. All rights reserved.
*
* Authors: Thanos Makatos <[email protected]>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Nutanix nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*/
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/eventfd.h>
#include <time.h>
#include <err.h>
#include <assert.h>
#include <sys/stat.h>
#include <libgen.h>
#include <pthread.h>
#include <openssl/md5.h>
#include <linux/limits.h>
#include "common.h"
#include "libvfio-user.h"
#include "tran_sock.h"
#define CLIENT_MAX_FDS (32)
/* This is low, so we get testing of vfu_dma_read/write() chunking. */
#define CLIENT_MAX_DATA_XFER_SIZE (1024)
static char *irq_to_str[] = {
[VFU_DEV_INTX_IRQ] = "INTx",
[VFU_DEV_MSI_IRQ] = "MSI",
[VFU_DEV_MSIX_IRQ] = "MSI-X",
[VFU_DEV_ERR_IRQ] = "ERR",
[VFU_DEV_REQ_IRQ] = "REQ"
};
void
vfu_log(UNUSED vfu_ctx_t *vfu_ctx, UNUSED int level,
const char *fmt, ...)
{
va_list ap;
printf("client: ");
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
static int
init_sock(const char *path)
{
int ret, sock;
struct sockaddr_un addr = {.sun_family = AF_UNIX};
/* TODO path should be defined elsewhere */
ret = snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
err(EXIT_FAILURE, "failed to open socket %s", path);
}
if ((ret = connect(sock, (struct sockaddr*)&addr, sizeof(addr))) == -1) {
err(EXIT_FAILURE, "failed to connect server");
}
return sock;
}
static void
send_version(int sock)
{
struct vfio_user_version cversion;
struct iovec iovecs[3] = { { 0 } };
char client_caps[1024];
int msg_id = 0xbada55;
int slen;
int ret;
slen = snprintf(client_caps, sizeof(client_caps),
"{"
"\"capabilities\":{"
"\"max_msg_fds\":%u,"
"\"max_data_xfer_size\":%u,"
"\"migration\":{"
"\"pgsize\":%zu"
"}"
"}"
"}", CLIENT_MAX_FDS, CLIENT_MAX_DATA_XFER_SIZE, sysconf(_SC_PAGESIZE));
cversion.major = LIB_VFIO_USER_MAJOR;
cversion.minor = LIB_VFIO_USER_MINOR;
/* [0] is for the header. */
iovecs[1].iov_base = &cversion;
iovecs[1].iov_len = sizeof(cversion);
iovecs[2].iov_base = client_caps;
/* Include the NUL. */
iovecs[2].iov_len = slen + 1;
ret = tran_sock_send_iovec(sock, msg_id, false, VFIO_USER_VERSION,
iovecs, ARRAY_SIZE(iovecs), NULL, 0, 0);
if (ret < 0) {
err(EXIT_FAILURE, "failed to send client version message");
}
}
static void
recv_version(int sock, int *server_max_fds, size_t *server_max_data_xfer_size,
size_t *pgsize)
{
struct vfio_user_version *sversion = NULL;
struct vfio_user_header hdr;
size_t vlen;
int ret;
ret = tran_sock_recv_alloc(sock, &hdr, true, NULL,
(void **)&sversion, &vlen);
if (ret < 0) {
err(EXIT_FAILURE, "failed to receive version");
}
if (hdr.cmd != VFIO_USER_VERSION) {
errx(EXIT_FAILURE, "msg%hx: invalid cmd %hu (expected %u)",
hdr.msg_id, hdr.cmd, VFIO_USER_VERSION);
}
if (vlen < sizeof(*sversion)) {
errx(EXIT_FAILURE, "VFIO_USER_VERSION: invalid size %lu", vlen);
}
if (sversion->major != LIB_VFIO_USER_MAJOR) {
errx(EXIT_FAILURE, "unsupported server major %hu (must be %u)",
sversion->major, LIB_VFIO_USER_MAJOR);
}
/*
* The server is supposed to tell us the minimum agreed version.
*/
if (sversion->minor > LIB_VFIO_USER_MINOR) {
errx(EXIT_FAILURE, "unsupported server minor %hu (must be <= %u)",
sversion->minor, LIB_VFIO_USER_MINOR);
}
*server_max_fds = 1;
*server_max_data_xfer_size = VFIO_USER_DEFAULT_MAX_DATA_XFER_SIZE;
*pgsize = sysconf(_SC_PAGESIZE);
if (vlen > sizeof(*sversion)) {
const char *json_str = (const char *)sversion->data;
size_t len = vlen - sizeof(*sversion);
if (json_str[len - 1] != '\0') {
errx(EXIT_FAILURE, "ignoring invalid JSON from server");
}
ret = tran_parse_version_json(json_str, server_max_fds,
server_max_data_xfer_size, pgsize);
if (ret < 0) {
err(EXIT_FAILURE, "failed to parse server JSON \"%s\"", json_str);
}
}
free(sversion);
}
static void
negotiate(int sock, int *server_max_fds, size_t *server_max_data_xfer_size,
size_t *pgsize)
{
send_version(sock);
recv_version(sock, server_max_fds, server_max_data_xfer_size, pgsize);
}
static void
send_device_reset(int sock)
{
int ret = tran_sock_msg(sock, 1, VFIO_USER_DEVICE_RESET,
NULL, 0, NULL, NULL, 0);
if (ret < 0) {
err(EXIT_FAILURE, "failed to reset device");
}
}
/* returns whether a VFIO migration capability is found */
static bool
get_region_vfio_caps(struct vfio_info_cap_header *header,
struct vfio_region_info_cap_sparse_mmap **sparse)
{
struct vfio_region_info_cap_type *type;
unsigned int i;
bool migr = false;
while (true) {
switch (header->id) {
case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
*sparse = (struct vfio_region_info_cap_sparse_mmap *)header;
printf("client: %s: Sparse cap nr_mmap_areas %d\n", __func__,
(*sparse)->nr_areas);
for (i = 0; i < (*sparse)->nr_areas; i++) {
printf("client: %s: area %d offset %#llx size %llu\n",
__func__, i, (*sparse)->areas[i].offset,
(*sparse)->areas[i].size);
}
break;
case VFIO_REGION_INFO_CAP_TYPE:
type = (struct vfio_region_info_cap_type*)header;
if (type->type != VFIO_REGION_TYPE_MIGRATION ||
type->subtype != VFIO_REGION_SUBTYPE_MIGRATION) {
errx(EXIT_FAILURE, "bad region type %d/%d", type->type,
type->subtype);
}
migr = true;
printf("client: migration region\n");
break;
default:
errx(EXIT_FAILURE, "bad VFIO cap ID %#x", header->id);
}
if (header->next == 0) {
break;
}
header = (struct vfio_info_cap_header*)((char*)header + header->next - sizeof(struct vfio_region_info));
}
return migr;
}
static void
do_get_device_region_info(int sock, struct vfio_region_info *region_info,
int *fds, size_t *nr_fds)
{
int ret = tran_sock_msg_fds(sock, 0xabcd, VFIO_USER_DEVICE_GET_REGION_INFO,
region_info, region_info->argsz, NULL,
region_info, region_info->argsz, fds, nr_fds);
if (ret < 0) {
err(EXIT_FAILURE, "failed to get device region info");
}
}
static void
mmap_sparse_areas(int *fds, struct vfio_region_info *region_info,
struct vfio_region_info_cap_sparse_mmap *sparse)
{
size_t i;
for (i = 0; i < sparse->nr_areas; i++) {
ssize_t ret;
void *addr;
char pathname[BUFSIZ];
char buf[PATH_MAX];
ret = snprintf(pathname, sizeof(pathname), "/proc/self/fd/%d", fds[i]);
assert(ret != -1 && (size_t)ret < sizeof(pathname));
ret = readlink(pathname, buf, sizeof(buf) - 1);
if (ret == -1) {
err(EXIT_FAILURE, "failed to resolve file descriptor %d", fds[i]);
}
buf[ret + 1] = '\0';
addr = mmap(NULL, sparse->areas[i].size, PROT_READ | PROT_WRITE,
MAP_SHARED, fds[i], region_info->offset +
sparse->areas[i].offset);
if (addr == MAP_FAILED) {
err(EXIT_FAILURE,
"failed to mmap sparse region #%lu in %s (%#llx-%#llx)",
i, buf, sparse->areas[i].offset,
sparse->areas[i].offset + sparse->areas[i].size - 1);
}
}
}
static void
get_device_region_info(int sock, uint32_t index)
{
struct vfio_region_info *region_info;
size_t cap_sz;
size_t size = sizeof(struct vfio_region_info);
int fds[CLIENT_MAX_FDS] = { 0 };
size_t nr_fds = ARRAY_SIZE(fds);
region_info = alloca(size);
memset(region_info, 0, size);
region_info->argsz = size;
region_info->index = index;
do_get_device_region_info(sock, region_info, NULL, 0);
if (region_info->argsz > size) {
size = region_info->argsz;
region_info = alloca(size);
memset(region_info, 0, size);
region_info->argsz = size;
region_info->index = index;
do_get_device_region_info(sock, region_info, fds, &nr_fds);
assert(region_info->argsz == size);
} else {
nr_fds = 0;
}
cap_sz = region_info->argsz - sizeof(struct vfio_region_info);
printf("client: %s: region_info[%d] offset %#llx flags %#x size %llu "
"cap_sz %lu #FDs %lu\n", __func__, index, region_info->offset,
region_info->flags, region_info->size, cap_sz, nr_fds);
if (cap_sz) {
struct vfio_region_info_cap_sparse_mmap *sparse = NULL;
if (get_region_vfio_caps((struct vfio_info_cap_header*)(region_info + 1),
&sparse)) {
if (sparse != NULL) {
assert((index == VFU_PCI_DEV_BAR1_REGION_IDX && nr_fds == 2) ||
(index == VFU_PCI_DEV_MIGR_REGION_IDX && nr_fds == 1));
assert(nr_fds == sparse->nr_areas);
mmap_sparse_areas(fds, region_info, sparse);
}
}
}
}
static void
get_device_regions_info(int sock, struct vfio_user_device_info *client_dev_info)
{
unsigned int i;
for (i = 0; i < client_dev_info->num_regions; i++) {
get_device_region_info(sock, i);
}
}
static void
get_device_info(int sock, struct vfio_user_device_info *dev_info)
{
uint16_t msg_id = 0xb10c;
int ret;
dev_info->argsz = sizeof(*dev_info);
ret = tran_sock_msg(sock, msg_id,
VFIO_USER_DEVICE_GET_INFO,
dev_info, sizeof(*dev_info),
NULL,
dev_info, sizeof(*dev_info));
if (ret < 0) {
err(EXIT_FAILURE, "failed to get device info");
}
if (dev_info->num_regions != 10) {
errx(EXIT_FAILURE, "bad number of device regions %d",
dev_info->num_regions);
}
printf("client: devinfo: flags %#x, num_regions %d, num_irqs %d\n",
dev_info->flags, dev_info->num_regions, dev_info->num_irqs);
}
static int
configure_irqs(int sock)
{
struct iovec iovecs[2] = { { 0, } };
struct vfio_irq_set irq_set;
uint16_t msg_id = 0x1bad;
int irq_fd;
int i, ret;
for (i = 0; i < VFU_DEV_NUM_IRQS; i++) { /* TODO move body of loop into function */
struct vfio_irq_info vfio_irq_info = {
.argsz = sizeof(vfio_irq_info),
.index = i
};
ret = tran_sock_msg(sock, msg_id,
VFIO_USER_DEVICE_GET_IRQ_INFO,
&vfio_irq_info, sizeof(vfio_irq_info),
NULL,
&vfio_irq_info, sizeof(vfio_irq_info));
if (ret < 0) {
err(EXIT_FAILURE, "failed to get %s info", irq_to_str[i]);
}
if (vfio_irq_info.count > 0) {
printf("client: IRQ %s: count=%d flags=%#x\n",
irq_to_str[i], vfio_irq_info.count, vfio_irq_info.flags);
}
}
msg_id++;
irq_set.argsz = sizeof(irq_set);
irq_set.flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
irq_set.index = 0;
irq_set.start = 0;
irq_set.count = 1;
irq_fd = eventfd(0, 0);
if (irq_fd == -1) {
err(EXIT_FAILURE, "failed to create eventfd");
}
/* [0] is for the header. */
iovecs[1].iov_base = &irq_set;
iovecs[1].iov_len = sizeof(irq_set);
ret = tran_sock_msg_iovec(sock, msg_id, VFIO_USER_DEVICE_SET_IRQS,
iovecs, ARRAY_SIZE(iovecs),
&irq_fd, 1,
NULL, NULL, 0, NULL, 0);
if (ret < 0) {
err(EXIT_FAILURE, "failed to send configure IRQs message");
}
return irq_fd;
}
static int
access_region(int sock, int region, bool is_write, uint64_t offset,
void *data, size_t data_len)
{
static int msg_id = 0xf00f;
struct vfio_user_region_access send_region_access = {
.offset = offset,
.region = region,
.count = data_len
};
struct iovec send_iovecs[3] = {
[1] = {
.iov_base = &send_region_access,
.iov_len = sizeof(send_region_access)
},
[2] = {
.iov_base = data,
.iov_len = data_len
}
};
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
struct vfio_user_region_access *recv_data;
size_t nr_send_iovecs, recv_data_len;
int op, ret;
if (is_write) {
op = VFIO_USER_REGION_WRITE;
nr_send_iovecs = 3;
recv_data_len = sizeof(*recv_data);
} else {
op = VFIO_USER_REGION_READ;
nr_send_iovecs = 2;
recv_data_len = sizeof(*recv_data) + data_len;
}
recv_data = calloc(1, recv_data_len);
if (recv_data == NULL) {
err(EXIT_FAILURE, "failed to alloc recv_data");
}
pthread_mutex_lock(&mutex);
ret = tran_sock_msg_iovec(sock, msg_id--, op,
send_iovecs, nr_send_iovecs,
NULL, 0, NULL,
recv_data, recv_data_len, NULL, 0);
pthread_mutex_unlock(&mutex);
if (ret != 0) {
warn("failed to %s region %d %#lx-%#lx",
is_write ? "write to" : "read from", region, offset,
offset + data_len - 1);
free(recv_data);
return ret;
}
if (recv_data->count != data_len) {
warnx("bad %s data count, expected=%lu, actual=%d",
is_write ? "write" : "read", data_len,
recv_data->count);
free(recv_data);
errno = EINVAL;
return -1;
}
/*
* TODO we could avoid the memcpy if tran_sock_msg_iovec() received the
* response into an iovec, but it's some work to implement it.
*/
if (!is_write) {
memcpy(data, ((char *)recv_data) + sizeof(*recv_data), data_len);
}
free(recv_data);
return 0;
}
static void
access_bar0(int sock, time_t *t)
{
int ret;
assert(t != NULL);
ret = access_region(sock, VFU_PCI_DEV_BAR0_REGION_IDX, true, 0, t, sizeof(*t));
if (ret < 0) {
err(EXIT_FAILURE, "failed to write to BAR0");
}
printf("client: wrote to BAR0: %ld\n", *t);
ret = access_region(sock, VFU_PCI_DEV_BAR0_REGION_IDX, false, 0, t, sizeof(*t));
if (ret < 0) {
err(EXIT_FAILURE, "failed to read from BAR0");
}
printf("client: read from BAR0: %ld\n", *t);
}
static void
wait_for_irq(int irq_fd)
{
uint64_t val;
if (read(irq_fd, &val, sizeof(val)) == -1) {
err(EXIT_FAILURE, "failed to read from irqfd");
}
printf("client: INTx triggered!\n");
}
static void
handle_dma_write(int sock, struct vfio_user_dma_map *dma_regions,
int nr_dma_regions, int *dma_region_fds)
{
struct vfio_user_dma_region_access dma_access;
struct vfio_user_header hdr;
int ret, i;
size_t size = sizeof(dma_access);
uint16_t msg_id = 0xcafe;
void *data;
ret = tran_sock_recv(sock, &hdr, false, &msg_id, &dma_access, &size);
if (ret < 0) {
err(EXIT_FAILURE, "failed to receive DMA read");
}
data = calloc(dma_access.count, 1);
if (data == NULL) {
err(EXIT_FAILURE, NULL);
}
if (recv(sock, data, dma_access.count, 0) == -1) {
err(EXIT_FAILURE, "failed to receive DMA read data");
}
for (i = 0; i < nr_dma_regions; i++) {
off_t offset;
ssize_t c;
if (dma_access.addr < dma_regions[i].addr ||
dma_access.addr >= dma_regions[i].addr + dma_regions[i].size) {
continue;
}
offset = dma_regions[i].offset + dma_access.addr;
c = pwrite(dma_region_fds[i], data, dma_access.count, offset);
if (c != (ssize_t)dma_access.count) {
err(EXIT_FAILURE, "failed to write to fd=%d at [%#lx-%#lx)",
dma_region_fds[i], offset, offset + dma_access.count);
}
break;
}
assert(i != nr_dma_regions);
ret = tran_sock_send(sock, msg_id, true, VFIO_USER_DMA_WRITE,
&dma_access, sizeof(dma_access));
if (ret < 0) {
err(EXIT_FAILURE, "failed to send reply of DMA write");
}
free(data);
}
static void
handle_dma_read(int sock, struct vfio_user_dma_map *dma_regions,
int nr_dma_regions, int *dma_region_fds)
{
struct vfio_user_dma_region_access dma_access, *response;
struct vfio_user_header hdr;
int ret, i, response_sz;
size_t size = sizeof(dma_access);
uint16_t msg_id = 0xcafe;
void *data;
ret = tran_sock_recv(sock, &hdr, false, &msg_id, &dma_access, &size);
if (ret < 0) {
err(EXIT_FAILURE, "failed to receive DMA read");
}
response_sz = sizeof(dma_access) + dma_access.count;
response = calloc(response_sz, 1);
if (response == NULL) {
err(EXIT_FAILURE, NULL);
}
response->addr = dma_access.addr;
response->count = dma_access.count;
data = (char *)response->data;
for (i = 0; i < nr_dma_regions; i++) {
off_t offset;
ssize_t c;
if (dma_access.addr < dma_regions[i].addr ||
dma_access.addr >= dma_regions[i].addr + dma_regions[i].size) {
continue;
}
offset = dma_regions[i].offset + dma_access.addr;
c = pread(dma_region_fds[i], data, dma_access.count, offset);
if (c != (ssize_t)dma_access.count) {
err(EXIT_FAILURE, "failed to read from fd=%d at [%#lx-%#lx)",
dma_region_fds[i], offset, offset + dma_access.count);
}
break;
}
assert(i != nr_dma_regions);
ret = tran_sock_send(sock, msg_id, true, VFIO_USER_DMA_READ,
response, response_sz);
if (ret < 0) {
err(EXIT_FAILURE, "failed to send reply of DMA read");
}
free(response);
}
static void
handle_dma_io(int sock, struct vfio_user_dma_map *dma_regions,
int nr_dma_regions, int *dma_region_fds)
{
size_t i;
for (i = 0; i < 4096 / CLIENT_MAX_DATA_XFER_SIZE; i++) {
handle_dma_write(sock, dma_regions, nr_dma_regions, dma_region_fds);
}
for (i = 0; i < 4096 / CLIENT_MAX_DATA_XFER_SIZE; i++) {
handle_dma_read(sock, dma_regions, nr_dma_regions, dma_region_fds);
}
}
static void
get_dirty_bitmap(int sock, struct vfio_user_dma_map *dma_region)
{
uint64_t bitmap_size = _get_bitmap_size(dma_region->size,
sysconf(_SC_PAGESIZE));
struct vfio_user_dirty_pages *dirty_pages;
struct vfio_user_bitmap_range *range;
char *bitmap;
size_t size;
void *data;
int ret;
size = sizeof(*dirty_pages) + sizeof(*range) + bitmap_size;
data = calloc(1, size);
assert(data != NULL);
dirty_pages = data;
dirty_pages->flags = VFIO_IOMMU_DIRTY_PAGES_FLAG_GET_BITMAP;
dirty_pages->argsz = sizeof(*dirty_pages) + sizeof(*range) + bitmap_size;
range = data + sizeof(*dirty_pages);
range->iova = dma_region->addr;
range->size = dma_region->size;
range->bitmap.size = bitmap_size;
range->bitmap.pgsize = sysconf(_SC_PAGESIZE);
bitmap = data + sizeof(*dirty_pages) + sizeof(*range);
ret = tran_sock_msg(sock, 0x99, VFIO_USER_DIRTY_PAGES,
data, sizeof(*dirty_pages) + sizeof(*range),
NULL, data, size);
if (ret != 0) {
err(EXIT_FAILURE, "failed to get dirty page bitmap");
}
printf("client: %s: %#lx-%#lx\t%#x\n", __func__, range->iova,
range->iova + range->size - 1, bitmap[0]);
free(data);
}
static void
usage(char *argv0)
{
fprintf(stderr, "Usage: %s [-h] [-m src|dst] /path/to/socket\n",
basename(argv0));
}
/*
* Normally each time the source client (QEMU) would read migration data from
* the device it would send them to the destination client. However, since in
* our sample both the source and the destination client are the same process,
* we simply accumulate the migration data of each iteration and apply it to
* the destination server at the end.
*
* Performs as many migration loops as @nr_iters or until the device has no
* more migration data (pending_bytes is zero), which ever comes first. The
* result of each migration iteration is stored in @migr_iter. @migr_iter must
* be at least @nr_iters.
*
* @returns the number of iterations performed
*/
static size_t
do_migrate(int sock, size_t nr_iters, struct iovec *migr_iter)
{
int ret;
uint64_t pending_bytes, data_offset, data_size;
size_t i = 0;
assert(nr_iters > 0);
/* XXX read pending_bytes */
ret = access_region(sock, VFU_PCI_DEV_MIGR_REGION_IDX, false,
offsetof(struct vfio_user_migration_info, pending_bytes),
&pending_bytes, sizeof(pending_bytes));
if (ret < 0) {
err(EXIT_FAILURE, "failed to read pending_bytes");
}
for (i = 0; i < nr_iters && pending_bytes > 0; i++) {
/* XXX read data_offset and data_size */
ret = access_region(sock, VFU_PCI_DEV_MIGR_REGION_IDX, false,
offsetof(struct vfio_user_migration_info, data_offset),
&data_offset, sizeof(data_offset));
if (ret < 0) {
err(EXIT_FAILURE, "failed to read data_offset");
}
ret = access_region(sock, VFU_PCI_DEV_MIGR_REGION_IDX, false,
offsetof(struct vfio_user_migration_info, data_size),
&data_size, sizeof(data_size));
if (ret < 0) {
err(EXIT_FAILURE, "failed to read data_size");
}
migr_iter[i].iov_len = data_size;
migr_iter[i].iov_base = malloc(data_size);
if (migr_iter[i].iov_base == NULL) {
err(EXIT_FAILURE, "failed to allocate migration buffer");
}
/* XXX read migration data */
ret = access_region(sock, VFU_PCI_DEV_MIGR_REGION_IDX, false,
data_offset,
(char *)migr_iter[i].iov_base, data_size);
if (ret < 0) {
err(EXIT_FAILURE, "failed to read migration data");
}
/* FIXME send migration data to the destination client process */
/*
* XXX read pending_bytes again to indicate to the server that the
* migration data have been consumed.
*/
ret = access_region(sock, VFU_PCI_DEV_MIGR_REGION_IDX, false,
offsetof(struct vfio_user_migration_info, pending_bytes),
&pending_bytes, sizeof(pending_bytes));
if (ret < 0) {
err(EXIT_FAILURE, "failed to read pending_bytes");
}
}
return i;
}
struct fake_guest_data {
int sock;
size_t bar1_size;
bool done;
unsigned char *md5sum;
};
static void *
fake_guest(void *arg)
{
struct fake_guest_data *fake_guest_data = arg;
int ret;
char buf[fake_guest_data->bar1_size];
MD5_CTX md5_ctx;
FILE *fp = fopen("/dev/urandom", "r");
if (fp == NULL) {
err(EXIT_FAILURE, "failed to open /dev/urandom");
}
do {
ret = fread(buf, fake_guest_data->bar1_size, 1, fp);
if (ret != 1) {
errx(EXIT_FAILURE, "short read %d", ret);
}
ret = access_region(fake_guest_data->sock, 1, true, 0, buf,
fake_guest_data->bar1_size);
if (ret != 0) {
err(EXIT_FAILURE, "fake guest failed to write garbage to BAR1");
}
MD5_Init(&md5_ctx);
MD5_Update(&md5_ctx, buf, fake_guest_data->bar1_size);
__sync_synchronize();
} while (!fake_guest_data->done);
MD5_Final(fake_guest_data->md5sum, &md5_ctx);
return NULL;
}
static size_t
migrate_from(int sock, size_t *nr_iters, struct iovec **migr_iters,
unsigned char *md5sum, size_t bar1_size)
{
uint32_t device_state;
int ret;
size_t _nr_iters;
pthread_t thread;
struct fake_guest_data fake_guest_data = {
.sock = sock,
.bar1_size = bar1_size,
.done = false,
.md5sum = md5sum
};
ret = pthread_create(&thread, NULL, fake_guest, &fake_guest_data);
if (ret != 0) {
errno = ret;
err(EXIT_FAILURE, "failed to create pthread");
}
*nr_iters = 2;
*migr_iters = malloc(sizeof(struct iovec) * *nr_iters);
if (*migr_iters == NULL) {
err(EXIT_FAILURE, NULL);
}
/*
* XXX set device state to pre-copy. This is technically optional but any
* VMM that cares about performance needs this.
*/
device_state = VFIO_DEVICE_STATE_SAVING | VFIO_DEVICE_STATE_RUNNING;
ret = access_region(sock, VFU_PCI_DEV_MIGR_REGION_IDX, true,
offsetof(struct vfio_user_migration_info, device_state),
&device_state, sizeof(device_state));
if (ret < 0) {
err(EXIT_FAILURE, "failed to write to device state");
}
_nr_iters = do_migrate(sock, 1, *migr_iters);
assert(_nr_iters == 1);
printf("client: stopping fake guest thread\n");
fake_guest_data.done = true;
__sync_synchronize();
ret = pthread_join(thread, NULL);
if (ret != 0) {
errno = ret;
err(EXIT_FAILURE, "failed to join fake guest pthread");
}
printf("client: setting device state to stop-and-copy\n");
device_state = VFIO_DEVICE_STATE_SAVING;
ret = access_region(sock, VFU_PCI_DEV_MIGR_REGION_IDX, true,
offsetof(struct vfio_user_migration_info, device_state),
&device_state, sizeof(device_state));
if (ret < 0) {
err(EXIT_FAILURE, "failed to write to device state");
}
_nr_iters += do_migrate(sock, 1, (*migr_iters) + _nr_iters);
if (_nr_iters != 2) {
errx(EXIT_FAILURE,
"expected 2 iterations instead of %ld while in stop-and-copy state",
_nr_iters);
}
/* XXX read device state, migration must have finished now */
device_state = VFIO_DEVICE_STATE_STOP;
ret = access_region(sock, VFU_PCI_DEV_MIGR_REGION_IDX, true,
offsetof(struct vfio_user_migration_info, device_state),
&device_state, sizeof(device_state));
if (ret < 0) {
err(EXIT_FAILURE, "failed to write to device state");
}
return _nr_iters;
}
static int
migrate_to(char *old_sock_path, int *server_max_fds,
size_t *server_max_data_xfer_size, size_t *pgsize, size_t nr_iters,
struct iovec *migr_iters, char *path_to_server,
unsigned char *src_md5sum, size_t bar1_size)
{
int ret, sock;
char *sock_path;
struct stat sb;
uint32_t device_state = VFIO_DEVICE_STATE_RESUMING;
uint64_t data_offset, data_len;
size_t i;
MD5_CTX md5_ctx;
char buf[bar1_size];
unsigned char dst_md5sum[MD5_DIGEST_LENGTH];
assert(old_sock_path != NULL);
printf("client: starting destination server\n");
ret = asprintf(&sock_path, "%s_migrated", old_sock_path);
if (ret == -1) {
err(EXIT_FAILURE, "failed to asprintf");
}
ret = fork();
if (ret == -1) {
err(EXIT_FAILURE, "failed to fork");
}
if (ret > 0) { /* child (destination server) */
char *_argv[] = {
path_to_server,
"-v",
sock_path,
NULL
};
ret = execvp(_argv[0] , _argv);
if (ret != 0) {
err(EXIT_FAILURE, "failed to start destination server (%s)",
path_to_server);
}
}
/* parent (client) */
/* wait for the server to come up */
while (stat(sock_path, &sb) == -1) {
if (errno != ENOENT) {
err(EXIT_FAILURE, "failed to stat %s", sock_path);
}
}
if ((sb.st_mode & S_IFMT) != S_IFSOCK) {
errx(EXIT_FAILURE, "%s: not a socket", sock_path);
}
/* connect to the destination server */
sock = init_sock(sock_path);
free(sock_path);
negotiate(sock, server_max_fds, server_max_data_xfer_size, pgsize);
/* XXX set device state to resuming */
ret = access_region(sock, VFU_PCI_DEV_MIGR_REGION_IDX, true,
offsetof(struct vfio_user_migration_info, device_state),
&device_state, sizeof(device_state));
if (ret < 0) {
err(EXIT_FAILURE, "failed to set device state to resuming");
}
for (i = 0; i < nr_iters; i++) {
/* XXX read data offset */
ret = access_region(sock, VFU_PCI_DEV_MIGR_REGION_IDX, false,
offsetof(struct vfio_user_migration_info, data_offset),
&data_offset, sizeof(data_offset));
if (ret < 0) {
err(EXIT_FAILURE, "failed to read migration data offset");
}
/* XXX write migration data */
/*
* TODO write half of migration data via regular write and other half via