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
|
/*
* Copyright (c) 2020 Nutanix Inc. All rights reserved.
*
* Authors: Thanos Makatos <thanos@nutanix.com>
*
* 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.
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/eventfd.h>
#include <time.h>
#include <err.h>
#include <assert.h>
#include "../lib/muser.h"
#include "../lib/muser_priv.h"
#include "../lib/common.h"
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
set_version(int sock, int client_max_fds, int *server_max_fds, size_t *pgsize)
{
int ret, mj, mn;
uint16_t msg_id;
char *client_caps = NULL;
assert(server_max_fds != NULL);
assert(pgsize != NULL);
ret = recv_version(sock, &mj, &mn, &msg_id, false, server_max_fds, pgsize);
if (ret < 0) {
errx(EXIT_FAILURE, "failed to receive version from server: %s",
strerror(-ret));
}
if (mj != LIB_MUSER_VFIO_USER_VERS_MJ || mn != LIB_MUSER_VFIO_USER_VERS_MN) {
errx(EXIT_FAILURE, "bad server version %d.%d\n", mj, mn);
}
if (asprintf(&client_caps, "{max_fds: %d, migration: {pgsize: %lu}}",
client_max_fds, sysconf(_SC_PAGESIZE)) == -1) {
err(EXIT_FAILURE, NULL);
}
ret = send_version(sock, mj, mn, msg_id, true, client_caps);
if (ret < 0) {
errx(EXIT_FAILURE, "failed to send version to server: %s",
strerror(-ret));
}
free(client_caps);
}
static void
send_device_reset(int sock)
{
int ret = send_recv_vfio_user_msg(sock, 1, VFIO_USER_DEVICE_RESET,
NULL, 0, NULL, 0, NULL, NULL, 0);
if (ret < 0) {
errx(EXIT_FAILURE, "failed to reset device: %s\n", strerror(-ret));
}
}
static void
get_region_vfio_caps(int sock, size_t cap_sz)
{
struct vfio_info_cap_header *header, *_header;
struct vfio_region_info_cap_type *type;
struct vfio_region_info_cap_sparse_mmap *sparse;
unsigned int i;
ssize_t ret;
header = _header = calloc(cap_sz, 1);
if (header == NULL) {
err(EXIT_FAILURE, NULL);
}
ret = recv(sock, header, cap_sz, 0);
if (ret < 0) {
err(EXIT_FAILURE, "failed to receive VFIO cap info");
}
assert((size_t)ret == cap_sz);
while (true) {
switch (header->id) {
case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
sparse = (struct vfio_region_info_cap_sparse_mmap*)header;
fprintf(stdout, "%s: Sparse cap nr_mmap_areas %d\n", __func__,
sparse->nr_areas);
for (i = 0; i < sparse->nr_areas; i++) {
fprintf(stdout, "%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);
}
printf("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));
}
free(_header);
}
static void
get_device_region_info(int sock, struct vfio_device_info *client_dev_info)
{
struct vfio_region_info region_info;
uint16_t msg_id = 0;
size_t cap_sz;
int ret;
unsigned int i;
msg_id = 1;
for (i = 0; i < client_dev_info->num_regions; i++) {
memset(®ion_info, 0, sizeof(region_info));
region_info.argsz = sizeof(region_info);
region_info.index = i;
msg_id++;
ret = send_recv_vfio_user_msg(sock, msg_id,
VFIO_USER_DEVICE_GET_REGION_INFO,
®ion_info, sizeof region_info,
NULL, 0, NULL,
®ion_info, sizeof(region_info));
if (ret < 0) {
errx(EXIT_FAILURE, "failed to get device region info: %s",
strerror(-ret));
}
cap_sz = region_info.argsz - sizeof(struct vfio_region_info);
fprintf(stdout, "%s: region_info[%d] offset %#llx flags %#x size %llu "
"cap_sz %lu\n", __func__, i, region_info.offset,
region_info.flags, region_info.size, cap_sz);
if (cap_sz) {
get_region_vfio_caps(sock, cap_sz);
}
}
}
static void
get_device_info(int sock, struct vfio_device_info *dev_info)
{
uint16_t msg_id;
int ret;
dev_info->argsz = sizeof(*dev_info);
msg_id = 1;
ret = send_recv_vfio_user_msg(sock, msg_id, VFIO_USER_DEVICE_GET_INFO,
dev_info, sizeof(*dev_info), NULL, 0, NULL,
dev_info, sizeof(*dev_info));
if (ret < 0) {
errx(EXIT_FAILURE, "failed to get device info: %s", strerror(-ret));
}
if (dev_info->num_regions != 10) {
errx(EXIT_FAILURE, "bad number of device regions %d",
dev_info->num_regions);
}
printf("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)
{
int i, ret;
struct vfio_irq_set irq_set;
uint16_t msg_id = 1;
int irq_fd;
for (i = 0; i < LM_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 = send_recv_vfio_user_msg(sock, msg_id,
VFIO_USER_DEVICE_GET_IRQ_INFO,
&vfio_irq_info, sizeof vfio_irq_info,
NULL, 0, NULL,
&vfio_irq_info, sizeof vfio_irq_info);
if (ret < 0) {
errx(EXIT_FAILURE, "failed to get %s info: %s", irq_to_str[i],
strerror(-ret));
}
if (vfio_irq_info.count > 0) {
printf("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");
}
ret = send_recv_vfio_user_msg(sock, msg_id, VFIO_USER_DEVICE_SET_IRQS,
&irq_set, sizeof irq_set, &irq_fd, 1, NULL,
NULL, 0);
if (ret < 0) {
errx(EXIT_FAILURE, "failed to send configure IRQs message: %s",
strerror(-ret));
}
return irq_fd;
}
static int
access_region(int sock, int region, bool is_write, uint64_t offset,
void *data, size_t data_len)
{
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
}
};
struct {
struct vfio_user_region_access region_access;
char data[data_len];
} __attribute__((packed)) recv_data;
int op, ret;
size_t nr_send_iovecs, recv_data_len;
if (is_write) {
op = VFIO_USER_REGION_WRITE;
nr_send_iovecs = 3;
recv_data_len = sizeof(recv_data.region_access);
} else {
op = VFIO_USER_REGION_READ;
nr_send_iovecs = 2;
recv_data_len = sizeof(recv_data);
}
ret = _send_recv_vfio_user_msg(sock, 0, op,
send_iovecs, nr_send_iovecs,
NULL, 0, NULL,
&recv_data, recv_data_len);
if (ret != 0) {
warnx("failed to %s region %d %#lx-%#lx: %s",
is_write ? "write to" : "read from", region, offset,
offset + data_len - 1, strerror(-ret));
return ret;
}
if (recv_data.region_access.count != data_len) {
warnx("bad %s data count, expected=%lu, actual=%d",
is_write ? "write" : "read", data_len,
recv_data.region_access.count);
return -EINVAL;
}
/*
* TODO we could avoid the memcpy if _sed_recv_vfio_user_msg received the
* response into an iovec, but it's some work to implement it.
*/
if (!is_write) {
memcpy(data, recv_data.data, data_len);
}
return 0;
}
static void
wait_for_irqs(int sock, int irq_fd)
{
int ret;
uint64_t val;
size_t size;
struct vfio_user_irq_info vfio_user_irq_info;
struct vfio_user_header hdr;
uint16_t msg_id = 1;
if (read(irq_fd, &val, sizeof val) == -1) {
err(EXIT_FAILURE, "failed to read from irqfd");
}
printf("INTx triggered!\n");
size = sizeof(vfio_user_irq_info);
ret = recv_vfio_user_msg(sock, &hdr, false, &msg_id, &vfio_user_irq_info,
&size);
if (ret < 0) {
errx(EXIT_FAILURE, "failed to receive IRQ message: %s",
strerror(-ret));
}
if (vfio_user_irq_info.subindex >= 1) {
errx(EXIT_FAILURE, "bad IRQ %d, max=1\n", vfio_user_irq_info.subindex);
}
ret = send_vfio_user_msg(sock, msg_id, true, VFIO_USER_VM_INTERRUPT,
NULL, 0, NULL, 0);
if (ret < 0) {
errx(EXIT_FAILURE,
"failed to send reply for VFIO_USER_VM_INTERRUPT: %s",
strerror(-ret));
}
printf("INTx messaged triggered!\n");
}
static int
access_bar0(int sock, int irq_fd)
{
time_t t = 1;
int ret = access_region(sock, LM_DEV_BAR0_REG_IDX, true, 0, &t, sizeof t);
if (ret < 0) {
errx(EXIT_FAILURE, "failed to write to BAR0: %s", strerror(-ret));
}
printf("wrote to BAR0: %ld\n", t);
ret = access_region(sock, LM_DEV_BAR0_REG_IDX, false, 0, &t, sizeof t);
if (ret < 0) {
errx(EXIT_FAILURE, "failed to read from BAR0: %s", strerror(-ret));
}
printf("read from BAR0: %ld\n", t);
ret = wait_for_irqs(sock, irq_fd);
return 0;
}
static void
handle_dma_write(int sock, struct vfio_user_dma_region *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;
void *data;
msg_id = 1;
ret = recv_vfio_user_msg(sock, &hdr, false, &msg_id, &dma_access, &size);
if (ret < 0) {
errx(EXIT_FAILURE, "failed to receive DMA read: %s", strerror(-ret));
}
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 recieve DMA read data");
}
for (i = 0; i < nr_dma_regions; i++) {
if (dma_regions[i].addr == dma_access.addr) {
ret = pwrite(dma_region_fds[i], data, dma_access.count,
dma_regions[i].offset);
if (ret < 0) {
err(EXIT_FAILURE,
"failed to write data to fd=%d at %#lx-%#lx",
dma_region_fds[i],
dma_regions[i].offset,
dma_regions[i].offset + dma_access.count - 1);
}
break;
}
}
dma_access.count = 0;
ret = send_vfio_user_msg(sock, msg_id, true, VFIO_USER_DMA_WRITE,
&dma_access, sizeof dma_access, NULL, 0);
if (ret < 0) {
errx(EXIT_FAILURE, "failed to send reply of DMA write: %s",
strerror(-ret));
}
free(data);
}
static void
handle_dma_read(int sock, struct vfio_user_dma_region *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;
void *data;
msg_id = 1;
ret = recv_vfio_user_msg(sock, &hdr, false, &msg_id, &dma_access, &size);
if (ret < 0) {
errx(EXIT_FAILURE, "failed to recieve DMA read");
}
response_sz = sizeof(dma_access) + dma_access.count;
response = calloc(response_sz, 1);
if (response == NULL) {
err(EXIT_FAILURE, NULL);
}
response->count = dma_access.count;
data = (char *)response->data;
for (i = 0; i < nr_dma_regions; i++) {
if (dma_regions[i].addr == dma_access.addr) {
if (pread(dma_region_fds[i], data, dma_access.count, dma_regions[i].offset) == -1) {
err(EXIT_FAILURE, "failed to write data at %#lx-%#lx",
dma_regions[i].offset,
dma_regions[i].offset + dma_access.count);
}
break;
}
}
ret = send_vfio_user_msg(sock, msg_id, true, VFIO_USER_DMA_READ,
response, response_sz, NULL, 0);
if (ret < 0) {
errx(EXIT_FAILURE, "failed to send reply of DMA write: %s",
strerror(-ret));
}
free(response);
}
static void
handle_dma_io(int sock, struct vfio_user_dma_region *dma_regions,
int nr_dma_regions, int *dma_region_fds)
{
handle_dma_write(sock, dma_regions, nr_dma_regions, dma_region_fds);
handle_dma_read(sock, dma_regions, nr_dma_regions, dma_region_fds);
}
static void
get_dirty_bitmaps(int sock, struct vfio_user_dma_region *dma_regions,
UNUSED int nr_dma_regions)
{
struct vfio_iommu_type1_dirty_bitmap dirty_bitmap = {0};
struct vfio_iommu_type1_dirty_bitmap_get bitmaps[2];
int ret;
size_t i;
struct iovec iovecs[4] = {
[1] = {
.iov_base = &dirty_bitmap,
.iov_len = sizeof dirty_bitmap
}
};
struct vfio_user_header hdr = {0};
char data[ARRAY_SIZE(bitmaps)];
assert(dma_regions != NULL);
//FIXME: Is below assert correct?
//assert(nr_dma_regions >= (int)ARRAY_SIZE(bitmaps));
for (i = 0; i < ARRAY_SIZE(bitmaps); i++) {
bitmaps[i].iova = dma_regions[i].addr;
bitmaps[i].size = dma_regions[i].size;
bitmaps[i].bitmap.size = 1; /* FIXME calculate based on page and IOVA size, don't hardcode */
bitmaps[i].bitmap.pgsize = sysconf(_SC_PAGESIZE);
iovecs[(i + 2)].iov_base = &bitmaps[i]; /* FIXME the +2 is because iovecs[0] is the vfio_user_header and iovecs[1] is vfio_iommu_type1_dirty_bitmap */
iovecs[(i + 2)].iov_len = sizeof(struct vfio_iommu_type1_dirty_bitmap_get);
}
/*
* FIXME there should be at least two IOVAs. Send single message for two
* IOVAs and ensure only one bit is set in first IOVA.
*/
dirty_bitmap.argsz = sizeof(dirty_bitmap) + ARRAY_SIZE(bitmaps) * sizeof(struct vfio_iommu_type1_dirty_bitmap_get);
dirty_bitmap.flags = VFIO_IOMMU_DIRTY_PAGES_FLAG_GET_BITMAP;
ret = _send_recv_vfio_user_msg(sock, 0, VFIO_USER_DIRTY_PAGES,
iovecs, ARRAY_SIZE(iovecs),
NULL, 0,
&hdr, data, ARRAY_SIZE(data));
if (ret != 0) {
errx(EXIT_FAILURE, "failed to start dirty page logging: %s",
strerror(-ret));
}
for (i = 0; i < ARRAY_SIZE(bitmaps); i++) {
printf("%#llx-%#llx\t%hhu\n", bitmaps[i].iova,
bitmaps[i].iova + bitmaps[i].size - 1, data[i]);
}
}
enum migration {
NO_MIGRATION,
MIGRATION_SOURCE,
MIGRATION_DESTINATION,
};
static void
usage(char *path) {
fprintf(stderr, "Usage: %s [-h] [-m src|dst] /path/to/socket\n",
basename(path));
}
static void
migrate_from(int sock, void **data, __u64 *len)
{
__u32 device_state = VFIO_DEVICE_STATE_SAVING;
__u64 pending_bytes, data_offset, data_size;
void *data;
/* XXX set device state to stop-and-copy */
int ret = access_region(sock, LM_DEV_MIGRATION_REG_IDX, true,
offsetof(struct vfio_device_migration_info, device_state),
&device_state, sizeof(device_state));
if (ret < 0) {
errx(EXIT_FAILURE, "failed to write to device state: %s",
strerror(-ret));
}
/* XXX read pending_bytes */
ret = access_region(sock, LM_DEV_MIGRATION_REG_IDX, false,
offsetof(struct vfio_device_migration_info, pending_bytes),
&pending_bytes, sizeof pending_bytes);
if (ret < 0) {
errx(EXIT_FAILURE, "failed to read pending_bytes: %s",
strerror(-ret));
}
while (pending_bytes > 0) {
/* XXX read data_offset and data_size */
ret = access_region(sock, LM_DEV_MIGRATION_REG_IDX, false,
offsetof(struct vfio_device_migration_info, data_offset),
&data_offset, sizeof data_offset);
if (ret < 0) {
errx(EXIT_FAILURE, "failed to read data_offset: %s",
strerror(-ret));
}
ret = access_region(sock, LM_DEV_MIGRATION_REG_IDX, false,
offsetof(struct vfio_device_migration_info, data_size),
&data_size, sizeof data_size);
if (ret < 0) {
errx(EXIT_FAILURE, "failed to read data_size: %s",
strerror(-ret));
}
/* XXX read migration data */
data = malloc(data_size);
if (data == NULL) {
return -errno;
}
ret = access_region(sock, LM_DEV_MIGRATION_REG_IDX, false, data_offset,
data, data_size);
if (ret < 0) {
errx(EXIT_FAILURE, "failed to read migration data: %s",
strerror(-ret));
}
/* FIXME send migration data to the destination client process */
printf("XXX migration: %#llx bytes worth of data\n", data_size);
/*
* XXX read pending_bytes again to indicate to the sever that the
* migration data have been consumed.
*/
ret = access_region(sock, LM_DEV_MIGRATION_REG_IDX, false,
offsetof(struct vfio_device_migration_info, pending_bytes),
&pending_bytes, sizeof pending_bytes);
if (ret < 0) {
errx(EXIT_FAILURE, "failed to read pending_bytes: %s",
strerror(-ret));
}
}
/* XXX read device state, migration must have finished now */
device_state = VFIO_DEVICE_STATE_STOP;
ret = access_region(sock, LM_DEV_MIGRATION_REG_IDX, true,
offsetof(struct vfio_device_migration_info, device_state),
&device_state, sizeof(device_state));
if (ret < 0) {
errx(EXIT_FAILURE, "failed to write to device state: %s",
strerror(-ret));
}
return 0;
}
int main(int argc, char *argv[])
{
int ret, sock, irq_fd;
struct vfio_user_dma_region *dma_regions;
struct vfio_device_info client_dev_info = {0};
int *dma_region_fds;
uint16_t msg_id = 1;
int i;
FILE *fp;
const int client_max_fds = 32;
int server_max_fds;
size_t pgsize;
int nr_dma_regions;
struct vfio_iommu_type1_dirty_bitmap dirty_bitmap = {0};
int opt;
enum migration migration = NO_MIGRATION;
while ((opt = getopt(argc, argv, "hm:")) != -1) {
switch (opt) {
case 'h':
usage(argv[0]);
exit(EXIT_SUCCESS);
case 'm':
if (strcmp(optarg, "src") == 0) {
migration = MIGRATION_SOURCE;
} else if (strcmp(optarg, "dst") == 0) {
migration = MIGRATION_DESTINATION;
} else {
fprintf(stderr, "invalid migration argument %s\n", optarg);
exit(EXIT_FAILURE);
}
break;
default:
usage(argv[0]);
exit(EXIT_FAILURE);
}
}
if (argc != optind + 1) {
usage(argv[0]);
exit(EXIT_FAILURE);
}
sock = init_sock(argv[optind]);
/*
* XXX VFIO_USER_VERSION
*
* The server proposes version upon connection, we need to send back the
* version the version we support.
*/
set_version(sock, client_max_fds, &server_max_fds, &pgsize);
/* try to access a bogus region, we should het an error */
ret = access_region(sock, 0xdeadbeef, false, 0, &ret, sizeof ret);
if (ret != -EINVAL) {
errx(EXIT_FAILURE,
"expected -EINVAL accessing bogus region, got %d instead",
ret);
}
/* XXX VFIO_USER_DEVICE_GET_INFO */
get_device_info(sock, &client_dev_info);
/* XXX VFIO_USER_DEVICE_GET_REGION_INFO */
get_device_region_info(sock, &client_dev_info);
/* XXX VFIO_USER_DEVICE_RESET */
send_device_reset(sock);
/*
* XXX VFIO_USER_DMA_MAP
*
* Tell the server we have some DMA regions it can access. Each DMA region
* is accompanied by a file descriptor, so let's create more (2x) DMA
* regions that can fit in a message that can be handled by the server.
*/
nr_dma_regions = server_max_fds << 1;
if ((fp = tmpfile()) == NULL) {
err(EXIT_FAILURE, "failed to create DMA file");
}
if ((ret = ftruncate(fileno(fp), nr_dma_regions * sysconf(_SC_PAGESIZE))) == -1) {
err(EXIT_FAILURE, "failed to truncate file");
}
dma_regions = alloca(sizeof *dma_regions * nr_dma_regions);
dma_region_fds = alloca(sizeof *dma_region_fds * nr_dma_regions);
for (i = 0; i < nr_dma_regions; i++) {
dma_regions[i].addr = i * sysconf(_SC_PAGESIZE);
dma_regions[i].size = sysconf(_SC_PAGESIZE);
dma_regions[i].offset = dma_regions[i].addr;
dma_regions[i].prot = PROT_READ | PROT_WRITE;
dma_regions[i].flags = VFIO_USER_F_DMA_REGION_MAPPABLE;
dma_region_fds[i] = fileno(fp);
}
for (i = 0; i < nr_dma_regions / server_max_fds; i++, msg_id++) {
ret = send_recv_vfio_user_msg(sock, msg_id, VFIO_USER_DMA_MAP,
dma_regions + (i * server_max_fds),
sizeof(*dma_regions) * server_max_fds,
dma_region_fds + (i * server_max_fds),
server_max_fds, NULL, NULL, 0);
if (ret < 0) {
fprintf(stderr, "failed to map DMA regions: %s\n", strerror(-ret));
return ret;
}
}
/*
* XXX VFIO_USER_DEVICE_GET_IRQ_INFO and VFIO_IRQ_SET_ACTION_TRIGGER
* Query interrupts and configure an eventfd to be associated with INTx.
*/
irq_fd = configure_irqs(sock);
dirty_bitmap.argsz = sizeof dirty_bitmap;
dirty_bitmap.flags = VFIO_IOMMU_DIRTY_PAGES_FLAG_START;
ret = send_recv_vfio_user_msg(sock, 0, VFIO_USER_DIRTY_PAGES,
&dirty_bitmap, sizeof dirty_bitmap,
NULL, 0, NULL, NULL, 0);
if (ret != 0) {
errx(EXIT_FAILURE, "failed to start dirty page logging: %s",
strerror(-ret));
}
/*
* XXX VFIO_USER_REGION_READ and VFIO_USER_REGION_WRITE
*
* BAR0 in the server does not support memory mapping so it must be accessed
* via explicit messages.
*/
ret = access_bar0(sock, irq_fd);
if (ret < 0) {
fprintf(stderr, "failed to access BAR0: %s\n", strerror(-ret));
exit(EXIT_FAILURE);
}
handle_dma_io(sock, dma_regions, nr_dma_regions, dma_region_fds);
get_dirty_bitmaps(sock, dma_regions, nr_dma_regions);
dirty_bitmap.argsz = sizeof dirty_bitmap;
dirty_bitmap.flags = VFIO_IOMMU_DIRTY_PAGES_FLAG_STOP;
ret = send_recv_vfio_user_msg(sock, 0, VFIO_USER_DIRTY_PAGES,
&dirty_bitmap, sizeof dirty_bitmap,
NULL, 0, NULL, NULL, 0);
if (ret != 0) {
errx(EXIT_FAILURE, "failed to stop dirty page logging: %s",
strerror(-ret));
}
/*
* FIXME now that region read/write works, change the server implementation
* to trigger an interrupt after N seconds, where N is the value written to
* BAR0 by the client.
*/
/* BAR1 can be memory mapped and read directly */
/*
* TODO implement the following: write a value in BAR1, a server timer will
* increase it every second (SIGALARM)
*/
/*
* XXX VFIO_USER_DMA_UNMAP
*
* unmap the first group of the DMA regions
*/
ret = send_recv_vfio_user_msg(sock, msg_id, VFIO_USER_DMA_UNMAP,
dma_regions, sizeof *dma_regions * server_max_fds,
NULL, 0, NULL, NULL, 0);
if (ret < 0) {
errx(EXIT_FAILURE, "failed to unmap DMA regions: %s", strerror(-ret));
}
if (migration == MIGRATION_SOURCE) {
ret = migrate_from(sock);
}
return 0;
}
/* ex: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab: */
|