xref: /openbmc/qemu/hw/virtio/vhost-user.c (revision 5b463530add462da8cca8548d46eea8bf70a7c28)
1 /*
2  * vhost-user
3  *
4  * Copyright (c) 2013 Virtual Open Systems Sarl.
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  *
9  */
10 
11 #include "qemu/osdep.h"
12 #include "qapi/error.h"
13 #include "hw/virtio/virtio-dmabuf.h"
14 #include "hw/virtio/vhost.h"
15 #include "hw/virtio/virtio-crypto.h"
16 #include "hw/virtio/vhost-user.h"
17 #include "hw/virtio/vhost-backend.h"
18 #include "hw/virtio/virtio.h"
19 #include "hw/virtio/virtio-net.h"
20 #include "chardev/char-fe.h"
21 #include "io/channel-socket.h"
22 #include "system/kvm.h"
23 #include "qemu/error-report.h"
24 #include "qemu/main-loop.h"
25 #include "qemu/uuid.h"
26 #include "qemu/sockets.h"
27 #include "system/runstate.h"
28 #include "system/cryptodev.h"
29 #include "migration/postcopy-ram.h"
30 #include "trace.h"
31 #include "system/ramblock.h"
32 
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <sys/un.h>
36 
37 #include "standard-headers/linux/vhost_types.h"
38 
39 #ifdef CONFIG_LINUX
40 #include <linux/userfaultfd.h>
41 #endif
42 
43 #define VHOST_MEMORY_BASELINE_NREGIONS    8
44 #define VHOST_USER_F_PROTOCOL_FEATURES 30
45 #define VHOST_USER_BACKEND_MAX_FDS     8
46 
47 #if defined(TARGET_PPC) || defined(TARGET_PPC64)
48 #include "hw/ppc/spapr.h"
49 #define VHOST_USER_MAX_RAM_SLOTS SPAPR_MAX_RAM_SLOTS
50 
51 #else
52 #define VHOST_USER_MAX_RAM_SLOTS 512
53 #endif
54 
55 /*
56  * Maximum size of virtio device config space
57  */
58 #define VHOST_USER_MAX_CONFIG_SIZE 256
59 
60 #define VHOST_USER_PROTOCOL_FEATURE_MASK ((1 << VHOST_USER_PROTOCOL_F_MAX) - 1)
61 
62 typedef enum VhostUserRequest {
63     VHOST_USER_NONE = 0,
64     VHOST_USER_GET_FEATURES = 1,
65     VHOST_USER_SET_FEATURES = 2,
66     VHOST_USER_SET_OWNER = 3,
67     VHOST_USER_RESET_OWNER = 4,
68     VHOST_USER_SET_MEM_TABLE = 5,
69     VHOST_USER_SET_LOG_BASE = 6,
70     VHOST_USER_SET_LOG_FD = 7,
71     VHOST_USER_SET_VRING_NUM = 8,
72     VHOST_USER_SET_VRING_ADDR = 9,
73     VHOST_USER_SET_VRING_BASE = 10,
74     VHOST_USER_GET_VRING_BASE = 11,
75     VHOST_USER_SET_VRING_KICK = 12,
76     VHOST_USER_SET_VRING_CALL = 13,
77     VHOST_USER_SET_VRING_ERR = 14,
78     VHOST_USER_GET_PROTOCOL_FEATURES = 15,
79     VHOST_USER_SET_PROTOCOL_FEATURES = 16,
80     VHOST_USER_GET_QUEUE_NUM = 17,
81     VHOST_USER_SET_VRING_ENABLE = 18,
82     VHOST_USER_SEND_RARP = 19,
83     VHOST_USER_NET_SET_MTU = 20,
84     VHOST_USER_SET_BACKEND_REQ_FD = 21,
85     VHOST_USER_IOTLB_MSG = 22,
86     VHOST_USER_SET_VRING_ENDIAN = 23,
87     VHOST_USER_GET_CONFIG = 24,
88     VHOST_USER_SET_CONFIG = 25,
89     VHOST_USER_CREATE_CRYPTO_SESSION = 26,
90     VHOST_USER_CLOSE_CRYPTO_SESSION = 27,
91     VHOST_USER_POSTCOPY_ADVISE  = 28,
92     VHOST_USER_POSTCOPY_LISTEN  = 29,
93     VHOST_USER_POSTCOPY_END     = 30,
94     VHOST_USER_GET_INFLIGHT_FD = 31,
95     VHOST_USER_SET_INFLIGHT_FD = 32,
96     VHOST_USER_GPU_SET_SOCKET = 33,
97     VHOST_USER_RESET_DEVICE = 34,
98     /* Message number 35 reserved for VHOST_USER_VRING_KICK. */
99     VHOST_USER_GET_MAX_MEM_SLOTS = 36,
100     VHOST_USER_ADD_MEM_REG = 37,
101     VHOST_USER_REM_MEM_REG = 38,
102     VHOST_USER_SET_STATUS = 39,
103     VHOST_USER_GET_STATUS = 40,
104     VHOST_USER_GET_SHARED_OBJECT = 41,
105     VHOST_USER_SET_DEVICE_STATE_FD = 42,
106     VHOST_USER_CHECK_DEVICE_STATE = 43,
107     VHOST_USER_MAX
108 } VhostUserRequest;
109 
110 typedef enum VhostUserBackendRequest {
111     VHOST_USER_BACKEND_NONE = 0,
112     VHOST_USER_BACKEND_IOTLB_MSG = 1,
113     VHOST_USER_BACKEND_CONFIG_CHANGE_MSG = 2,
114     VHOST_USER_BACKEND_VRING_HOST_NOTIFIER_MSG = 3,
115     VHOST_USER_BACKEND_SHARED_OBJECT_ADD = 6,
116     VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE = 7,
117     VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP = 8,
118     VHOST_USER_BACKEND_MAX
119 }  VhostUserBackendRequest;
120 
121 typedef struct VhostUserMemoryRegion {
122     uint64_t guest_phys_addr;
123     uint64_t memory_size;
124     uint64_t userspace_addr;
125     uint64_t mmap_offset;
126 } VhostUserMemoryRegion;
127 
128 typedef struct VhostUserMemory {
129     uint32_t nregions;
130     uint32_t padding;
131     VhostUserMemoryRegion regions[VHOST_MEMORY_BASELINE_NREGIONS];
132 } VhostUserMemory;
133 
134 typedef struct VhostUserMemRegMsg {
135     uint64_t padding;
136     VhostUserMemoryRegion region;
137 } VhostUserMemRegMsg;
138 
139 typedef struct VhostUserLog {
140     uint64_t mmap_size;
141     uint64_t mmap_offset;
142 } VhostUserLog;
143 
144 typedef struct VhostUserConfig {
145     uint32_t offset;
146     uint32_t size;
147     uint32_t flags;
148     uint8_t region[VHOST_USER_MAX_CONFIG_SIZE];
149 } VhostUserConfig;
150 
151 #define VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN    512
152 #define VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN  64
153 #define VHOST_CRYPTO_ASYM_MAX_KEY_LEN  1024
154 
155 typedef struct VhostUserCryptoSession {
156     uint64_t op_code;
157     union {
158         struct {
159             CryptoDevBackendSymSessionInfo session_setup_data;
160             uint8_t key[VHOST_CRYPTO_SYM_CIPHER_MAX_KEY_LEN];
161             uint8_t auth_key[VHOST_CRYPTO_SYM_HMAC_MAX_KEY_LEN];
162         } sym;
163         struct {
164             CryptoDevBackendAsymSessionInfo session_setup_data;
165             uint8_t key[VHOST_CRYPTO_ASYM_MAX_KEY_LEN];
166         } asym;
167     } u;
168 
169     /* session id for success, -1 on errors */
170     int64_t session_id;
171 } VhostUserCryptoSession;
172 
173 static VhostUserConfig c __attribute__ ((unused));
174 #define VHOST_USER_CONFIG_HDR_SIZE (sizeof(c.offset) \
175                                    + sizeof(c.size) \
176                                    + sizeof(c.flags))
177 
178 typedef struct VhostUserVringArea {
179     uint64_t u64;
180     uint64_t size;
181     uint64_t offset;
182 } VhostUserVringArea;
183 
184 typedef struct VhostUserInflight {
185     uint64_t mmap_size;
186     uint64_t mmap_offset;
187     uint16_t num_queues;
188     uint16_t queue_size;
189 } VhostUserInflight;
190 
191 typedef struct VhostUserShared {
192     unsigned char uuid[16];
193 } VhostUserShared;
194 
195 typedef struct {
196     VhostUserRequest request;
197 
198 #define VHOST_USER_VERSION_MASK     (0x3)
199 #define VHOST_USER_REPLY_MASK       (0x1 << 2)
200 #define VHOST_USER_NEED_REPLY_MASK  (0x1 << 3)
201     uint32_t flags;
202     uint32_t size; /* the following payload size */
203 } QEMU_PACKED VhostUserHeader;
204 
205 /* Request payload of VHOST_USER_SET_DEVICE_STATE_FD */
206 typedef struct VhostUserTransferDeviceState {
207     uint32_t direction;
208     uint32_t phase;
209 } VhostUserTransferDeviceState;
210 
211 typedef union {
212 #define VHOST_USER_VRING_IDX_MASK   (0xff)
213 #define VHOST_USER_VRING_NOFD_MASK  (0x1 << 8)
214         uint64_t u64;
215         struct vhost_vring_state state;
216         struct vhost_vring_addr addr;
217         VhostUserMemory memory;
218         VhostUserMemRegMsg mem_reg;
219         VhostUserLog log;
220         struct vhost_iotlb_msg iotlb;
221         VhostUserConfig config;
222         VhostUserCryptoSession session;
223         VhostUserVringArea area;
224         VhostUserInflight inflight;
225         VhostUserShared object;
226         VhostUserTransferDeviceState transfer_state;
227 } VhostUserPayload;
228 
229 typedef struct VhostUserMsg {
230     VhostUserHeader hdr;
231     VhostUserPayload payload;
232 } QEMU_PACKED VhostUserMsg;
233 
234 static VhostUserMsg m __attribute__ ((unused));
235 #define VHOST_USER_HDR_SIZE (sizeof(VhostUserHeader))
236 
237 #define VHOST_USER_PAYLOAD_SIZE (sizeof(VhostUserPayload))
238 
239 /* The version of the protocol we support */
240 #define VHOST_USER_VERSION    (0x1)
241 
242 struct vhost_user {
243     struct vhost_dev *dev;
244     /* Shared between vhost devs of the same virtio device */
245     VhostUserState *user;
246     QIOChannel *backend_ioc;
247     GSource *backend_src;
248     NotifierWithReturn postcopy_notifier;
249     struct PostCopyFD  postcopy_fd;
250     uint64_t           postcopy_client_bases[VHOST_USER_MAX_RAM_SLOTS];
251     /* Length of the region_rb and region_rb_offset arrays */
252     size_t             region_rb_len;
253     /* RAMBlock associated with a given region */
254     RAMBlock         **region_rb;
255     /*
256      * The offset from the start of the RAMBlock to the start of the
257      * vhost region.
258      */
259     ram_addr_t        *region_rb_offset;
260 
261     /* True once we've entered postcopy_listen */
262     bool               postcopy_listen;
263 
264     /* Our current regions */
265     int num_shadow_regions;
266     struct vhost_memory_region shadow_regions[VHOST_USER_MAX_RAM_SLOTS];
267 };
268 
269 struct scrub_regions {
270     struct vhost_memory_region *region;
271     int reg_idx;
272     int fd_idx;
273 };
274 
vhost_user_read_header(struct vhost_dev * dev,VhostUserMsg * msg)275 static int vhost_user_read_header(struct vhost_dev *dev, VhostUserMsg *msg)
276 {
277     struct vhost_user *u = dev->opaque;
278     CharBackend *chr = u->user->chr;
279     uint8_t *p = (uint8_t *) msg;
280     int r, size = VHOST_USER_HDR_SIZE;
281 
282     r = qemu_chr_fe_read_all(chr, p, size);
283     if (r != size) {
284         int saved_errno = errno;
285         error_report("Failed to read msg header. Read %d instead of %d."
286                      " Original request %d.", r, size, msg->hdr.request);
287         return r < 0 ? -saved_errno : -EIO;
288     }
289 
290     /* validate received flags */
291     if (msg->hdr.flags != (VHOST_USER_REPLY_MASK | VHOST_USER_VERSION)) {
292         error_report("Failed to read msg header."
293                 " Flags 0x%x instead of 0x%x.", msg->hdr.flags,
294                 VHOST_USER_REPLY_MASK | VHOST_USER_VERSION);
295         return -EPROTO;
296     }
297 
298     trace_vhost_user_read(msg->hdr.request, msg->hdr.flags);
299 
300     return 0;
301 }
302 
vhost_user_read(struct vhost_dev * dev,VhostUserMsg * msg)303 static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg)
304 {
305     struct vhost_user *u = dev->opaque;
306     CharBackend *chr = u->user->chr;
307     uint8_t *p = (uint8_t *) msg;
308     int r, size;
309 
310     r = vhost_user_read_header(dev, msg);
311     if (r < 0) {
312         return r;
313     }
314 
315     /* validate message size is sane */
316     if (msg->hdr.size > VHOST_USER_PAYLOAD_SIZE) {
317         error_report("Failed to read msg header."
318                 " Size %d exceeds the maximum %zu.", msg->hdr.size,
319                 VHOST_USER_PAYLOAD_SIZE);
320         return -EPROTO;
321     }
322 
323     if (msg->hdr.size) {
324         p += VHOST_USER_HDR_SIZE;
325         size = msg->hdr.size;
326         r = qemu_chr_fe_read_all(chr, p, size);
327         if (r != size) {
328             int saved_errno = errno;
329             error_report("Failed to read msg payload."
330                          " Read %d instead of %d.", r, msg->hdr.size);
331             return r < 0 ? -saved_errno : -EIO;
332         }
333     }
334 
335     return 0;
336 }
337 
process_message_reply(struct vhost_dev * dev,const VhostUserMsg * msg)338 static int process_message_reply(struct vhost_dev *dev,
339                                  const VhostUserMsg *msg)
340 {
341     int ret;
342     VhostUserMsg msg_reply;
343 
344     if ((msg->hdr.flags & VHOST_USER_NEED_REPLY_MASK) == 0) {
345         return 0;
346     }
347 
348     ret = vhost_user_read(dev, &msg_reply);
349     if (ret < 0) {
350         return ret;
351     }
352 
353     if (msg_reply.hdr.request != msg->hdr.request) {
354         error_report("Received unexpected msg type. "
355                      "Expected %d received %d",
356                      msg->hdr.request, msg_reply.hdr.request);
357         return -EPROTO;
358     }
359 
360     return msg_reply.payload.u64 ? -EIO : 0;
361 }
362 
vhost_user_per_device_request(VhostUserRequest request)363 static bool vhost_user_per_device_request(VhostUserRequest request)
364 {
365     switch (request) {
366     case VHOST_USER_SET_OWNER:
367     case VHOST_USER_RESET_OWNER:
368     case VHOST_USER_SET_MEM_TABLE:
369     case VHOST_USER_GET_QUEUE_NUM:
370     case VHOST_USER_NET_SET_MTU:
371     case VHOST_USER_RESET_DEVICE:
372     case VHOST_USER_ADD_MEM_REG:
373     case VHOST_USER_REM_MEM_REG:
374     case VHOST_USER_SET_LOG_BASE:
375         return true;
376     default:
377         return false;
378     }
379 }
380 
381 /* most non-init callers ignore the error */
vhost_user_write(struct vhost_dev * dev,VhostUserMsg * msg,int * fds,int fd_num)382 static int vhost_user_write(struct vhost_dev *dev, VhostUserMsg *msg,
383                             int *fds, int fd_num)
384 {
385     struct vhost_user *u = dev->opaque;
386     CharBackend *chr = u->user->chr;
387     int ret, size = VHOST_USER_HDR_SIZE + msg->hdr.size;
388 
389     /*
390      * Some devices, like virtio-scsi, are implemented as a single vhost_dev,
391      * while others, like virtio-net, contain multiple vhost_devs. For
392      * operations such as configuring device memory mappings or issuing device
393      * resets, which affect the whole device instead of individual VQs,
394      * vhost-user messages should only be sent once.
395      *
396      * Devices with multiple vhost_devs are given an associated dev->vq_index
397      * so per_device requests are only sent if vq_index is 0.
398      */
399     if (vhost_user_per_device_request(msg->hdr.request)
400         && dev->vq_index != 0) {
401         msg->hdr.flags &= ~VHOST_USER_NEED_REPLY_MASK;
402         return 0;
403     }
404 
405     if (qemu_chr_fe_set_msgfds(chr, fds, fd_num) < 0) {
406         error_report("Failed to set msg fds.");
407         return -EINVAL;
408     }
409 
410     ret = qemu_chr_fe_write_all(chr, (const uint8_t *) msg, size);
411     if (ret != size) {
412         int saved_errno = errno;
413         error_report("Failed to write msg."
414                      " Wrote %d instead of %d.", ret, size);
415         return ret < 0 ? -saved_errno : -EIO;
416     }
417 
418     trace_vhost_user_write(msg->hdr.request, msg->hdr.flags);
419 
420     return 0;
421 }
422 
vhost_user_gpu_set_socket(struct vhost_dev * dev,int fd)423 int vhost_user_gpu_set_socket(struct vhost_dev *dev, int fd)
424 {
425     VhostUserMsg msg = {
426         .hdr.request = VHOST_USER_GPU_SET_SOCKET,
427         .hdr.flags = VHOST_USER_VERSION,
428     };
429 
430     return vhost_user_write(dev, &msg, &fd, 1);
431 }
432 
vhost_user_set_log_base(struct vhost_dev * dev,uint64_t base,struct vhost_log * log)433 static int vhost_user_set_log_base(struct vhost_dev *dev, uint64_t base,
434                                    struct vhost_log *log)
435 {
436     int fds[VHOST_USER_MAX_RAM_SLOTS];
437     size_t fd_num = 0;
438     bool shmfd = virtio_has_feature(dev->protocol_features,
439                                     VHOST_USER_PROTOCOL_F_LOG_SHMFD);
440     int ret;
441     VhostUserMsg msg = {
442         .hdr.request = VHOST_USER_SET_LOG_BASE,
443         .hdr.flags = VHOST_USER_VERSION,
444         .payload.log.mmap_size = log->size * sizeof(*(log->log)),
445         .payload.log.mmap_offset = 0,
446         .hdr.size = sizeof(msg.payload.log),
447     };
448 
449     /* Send only once with first queue pair */
450     if (dev->vq_index != 0) {
451         return 0;
452     }
453 
454     if (shmfd && log->fd != -1) {
455         fds[fd_num++] = log->fd;
456     }
457 
458     ret = vhost_user_write(dev, &msg, fds, fd_num);
459     if (ret < 0) {
460         return ret;
461     }
462 
463     if (shmfd) {
464         msg.hdr.size = 0;
465         ret = vhost_user_read(dev, &msg);
466         if (ret < 0) {
467             return ret;
468         }
469 
470         if (msg.hdr.request != VHOST_USER_SET_LOG_BASE) {
471             error_report("Received unexpected msg type. "
472                          "Expected %d received %d",
473                          VHOST_USER_SET_LOG_BASE, msg.hdr.request);
474             return -EPROTO;
475         }
476     }
477 
478     return 0;
479 }
480 
vhost_user_get_mr_data(uint64_t addr,ram_addr_t * offset,int * fd)481 static MemoryRegion *vhost_user_get_mr_data(uint64_t addr, ram_addr_t *offset,
482                                             int *fd)
483 {
484     MemoryRegion *mr;
485 
486     assert((uintptr_t)addr == addr);
487     mr = memory_region_from_host((void *)(uintptr_t)addr, offset);
488     *fd = memory_region_get_fd(mr);
489     *offset += mr->ram_block->fd_offset;
490 
491     return mr;
492 }
493 
vhost_user_fill_msg_region(VhostUserMemoryRegion * dst,struct vhost_memory_region * src,uint64_t mmap_offset)494 static void vhost_user_fill_msg_region(VhostUserMemoryRegion *dst,
495                                        struct vhost_memory_region *src,
496                                        uint64_t mmap_offset)
497 {
498     assert(src != NULL && dst != NULL);
499     dst->userspace_addr = src->userspace_addr;
500     dst->memory_size = src->memory_size;
501     dst->guest_phys_addr = src->guest_phys_addr;
502     dst->mmap_offset = mmap_offset;
503 }
504 
vhost_user_fill_set_mem_table_msg(struct vhost_user * u,struct vhost_dev * dev,VhostUserMsg * msg,int * fds,size_t * fd_num,bool track_ramblocks)505 static int vhost_user_fill_set_mem_table_msg(struct vhost_user *u,
506                                              struct vhost_dev *dev,
507                                              VhostUserMsg *msg,
508                                              int *fds, size_t *fd_num,
509                                              bool track_ramblocks)
510 {
511     int i, fd;
512     ram_addr_t offset;
513     MemoryRegion *mr;
514     struct vhost_memory_region *reg;
515     VhostUserMemoryRegion region_buffer;
516 
517     msg->hdr.request = VHOST_USER_SET_MEM_TABLE;
518 
519     for (i = 0; i < dev->mem->nregions; ++i) {
520         reg = dev->mem->regions + i;
521 
522         mr = vhost_user_get_mr_data(reg->userspace_addr, &offset, &fd);
523         if (fd > 0) {
524             if (track_ramblocks) {
525                 assert(*fd_num < VHOST_MEMORY_BASELINE_NREGIONS);
526                 trace_vhost_user_set_mem_table_withfd(*fd_num, mr->name,
527                                                       reg->memory_size,
528                                                       reg->guest_phys_addr,
529                                                       reg->userspace_addr,
530                                                       offset);
531                 u->region_rb_offset[i] = offset;
532                 u->region_rb[i] = mr->ram_block;
533             } else if (*fd_num == VHOST_MEMORY_BASELINE_NREGIONS) {
534                 error_report("Failed preparing vhost-user memory table msg");
535                 return -ENOBUFS;
536             }
537             vhost_user_fill_msg_region(&region_buffer, reg, offset);
538             msg->payload.memory.regions[*fd_num] = region_buffer;
539             fds[(*fd_num)++] = fd;
540         } else if (track_ramblocks) {
541             u->region_rb_offset[i] = 0;
542             u->region_rb[i] = NULL;
543         }
544     }
545 
546     msg->payload.memory.nregions = *fd_num;
547 
548     if (!*fd_num) {
549         error_report("Failed initializing vhost-user memory map, "
550                      "consider using -object memory-backend-file share=on");
551         return -EINVAL;
552     }
553 
554     msg->hdr.size = sizeof(msg->payload.memory.nregions);
555     msg->hdr.size += sizeof(msg->payload.memory.padding);
556     msg->hdr.size += *fd_num * sizeof(VhostUserMemoryRegion);
557 
558     return 0;
559 }
560 
reg_equal(struct vhost_memory_region * shadow_reg,struct vhost_memory_region * vdev_reg)561 static inline bool reg_equal(struct vhost_memory_region *shadow_reg,
562                              struct vhost_memory_region *vdev_reg)
563 {
564     return shadow_reg->guest_phys_addr == vdev_reg->guest_phys_addr &&
565         shadow_reg->userspace_addr == vdev_reg->userspace_addr &&
566         shadow_reg->memory_size == vdev_reg->memory_size;
567 }
568 
scrub_shadow_regions(struct vhost_dev * dev,struct scrub_regions * add_reg,int * nr_add_reg,struct scrub_regions * rem_reg,int * nr_rem_reg,uint64_t * shadow_pcb,bool track_ramblocks)569 static void scrub_shadow_regions(struct vhost_dev *dev,
570                                  struct scrub_regions *add_reg,
571                                  int *nr_add_reg,
572                                  struct scrub_regions *rem_reg,
573                                  int *nr_rem_reg, uint64_t *shadow_pcb,
574                                  bool track_ramblocks)
575 {
576     struct vhost_user *u = dev->opaque;
577     bool found[VHOST_USER_MAX_RAM_SLOTS] = {};
578     struct vhost_memory_region *reg, *shadow_reg;
579     int i, j, fd, add_idx = 0, rm_idx = 0, fd_num = 0;
580     ram_addr_t offset;
581     MemoryRegion *mr;
582     bool matching;
583 
584     /*
585      * Find memory regions present in our shadow state which are not in
586      * the device's current memory state.
587      *
588      * Mark regions in both the shadow and device state as "found".
589      */
590     for (i = 0; i < u->num_shadow_regions; i++) {
591         shadow_reg = &u->shadow_regions[i];
592         matching = false;
593 
594         for (j = 0; j < dev->mem->nregions; j++) {
595             reg = &dev->mem->regions[j];
596 
597             mr = vhost_user_get_mr_data(reg->userspace_addr, &offset, &fd);
598 
599             if (reg_equal(shadow_reg, reg)) {
600                 matching = true;
601                 found[j] = true;
602                 if (track_ramblocks) {
603                     /*
604                      * Reset postcopy client bases, region_rb, and
605                      * region_rb_offset in case regions are removed.
606                      */
607                     if (fd > 0) {
608                         u->region_rb_offset[j] = offset;
609                         u->region_rb[j] = mr->ram_block;
610                         shadow_pcb[j] = u->postcopy_client_bases[i];
611                     } else {
612                         u->region_rb_offset[j] = 0;
613                         u->region_rb[j] = NULL;
614                     }
615                 }
616                 break;
617             }
618         }
619 
620         /*
621          * If the region was not found in the current device memory state
622          * create an entry for it in the removed list.
623          */
624         if (!matching) {
625             rem_reg[rm_idx].region = shadow_reg;
626             rem_reg[rm_idx++].reg_idx = i;
627         }
628     }
629 
630     /*
631      * For regions not marked "found", create entries in the added list.
632      *
633      * Note their indexes in the device memory state and the indexes of their
634      * file descriptors.
635      */
636     for (i = 0; i < dev->mem->nregions; i++) {
637         reg = &dev->mem->regions[i];
638         vhost_user_get_mr_data(reg->userspace_addr, &offset, &fd);
639         if (fd > 0) {
640             ++fd_num;
641         }
642 
643         /*
644          * If the region was in both the shadow and device state we don't
645          * need to send a VHOST_USER_ADD_MEM_REG message for it.
646          */
647         if (found[i]) {
648             continue;
649         }
650 
651         add_reg[add_idx].region = reg;
652         add_reg[add_idx].reg_idx = i;
653         add_reg[add_idx++].fd_idx = fd_num;
654     }
655     *nr_rem_reg = rm_idx;
656     *nr_add_reg = add_idx;
657 }
658 
send_remove_regions(struct vhost_dev * dev,struct scrub_regions * remove_reg,int nr_rem_reg,VhostUserMsg * msg,bool reply_supported)659 static int send_remove_regions(struct vhost_dev *dev,
660                                struct scrub_regions *remove_reg,
661                                int nr_rem_reg, VhostUserMsg *msg,
662                                bool reply_supported)
663 {
664     struct vhost_user *u = dev->opaque;
665     struct vhost_memory_region *shadow_reg;
666     int i, fd, shadow_reg_idx, ret;
667     ram_addr_t offset;
668     VhostUserMemoryRegion region_buffer;
669 
670     /*
671      * The regions in remove_reg appear in the same order they do in the
672      * shadow table. Therefore we can minimize memory copies by iterating
673      * through remove_reg backwards.
674      */
675     for (i = nr_rem_reg - 1; i >= 0; i--) {
676         shadow_reg = remove_reg[i].region;
677         shadow_reg_idx = remove_reg[i].reg_idx;
678 
679         vhost_user_get_mr_data(shadow_reg->userspace_addr, &offset, &fd);
680 
681         if (fd > 0) {
682             msg->hdr.request = VHOST_USER_REM_MEM_REG;
683             vhost_user_fill_msg_region(&region_buffer, shadow_reg, 0);
684             msg->payload.mem_reg.region = region_buffer;
685 
686             ret = vhost_user_write(dev, msg, NULL, 0);
687             if (ret < 0) {
688                 return ret;
689             }
690 
691             if (reply_supported) {
692                 ret = process_message_reply(dev, msg);
693                 if (ret) {
694                     return ret;
695                 }
696             }
697         }
698 
699         /*
700          * At this point we know the backend has unmapped the region. It is now
701          * safe to remove it from the shadow table.
702          */
703         memmove(&u->shadow_regions[shadow_reg_idx],
704                 &u->shadow_regions[shadow_reg_idx + 1],
705                 sizeof(struct vhost_memory_region) *
706                 (u->num_shadow_regions - shadow_reg_idx - 1));
707         u->num_shadow_regions--;
708     }
709 
710     return 0;
711 }
712 
send_add_regions(struct vhost_dev * dev,struct scrub_regions * add_reg,int nr_add_reg,VhostUserMsg * msg,uint64_t * shadow_pcb,bool reply_supported,bool track_ramblocks)713 static int send_add_regions(struct vhost_dev *dev,
714                             struct scrub_regions *add_reg, int nr_add_reg,
715                             VhostUserMsg *msg, uint64_t *shadow_pcb,
716                             bool reply_supported, bool track_ramblocks)
717 {
718     struct vhost_user *u = dev->opaque;
719     int i, fd, ret, reg_idx, reg_fd_idx;
720     struct vhost_memory_region *reg;
721     MemoryRegion *mr;
722     ram_addr_t offset;
723     VhostUserMsg msg_reply;
724     VhostUserMemoryRegion region_buffer;
725 
726     for (i = 0; i < nr_add_reg; i++) {
727         reg = add_reg[i].region;
728         reg_idx = add_reg[i].reg_idx;
729         reg_fd_idx = add_reg[i].fd_idx;
730 
731         mr = vhost_user_get_mr_data(reg->userspace_addr, &offset, &fd);
732 
733         if (fd > 0) {
734             if (track_ramblocks) {
735                 trace_vhost_user_set_mem_table_withfd(reg_fd_idx, mr->name,
736                                                       reg->memory_size,
737                                                       reg->guest_phys_addr,
738                                                       reg->userspace_addr,
739                                                       offset);
740                 u->region_rb_offset[reg_idx] = offset;
741                 u->region_rb[reg_idx] = mr->ram_block;
742             }
743             msg->hdr.request = VHOST_USER_ADD_MEM_REG;
744             vhost_user_fill_msg_region(&region_buffer, reg, offset);
745             msg->payload.mem_reg.region = region_buffer;
746 
747             ret = vhost_user_write(dev, msg, &fd, 1);
748             if (ret < 0) {
749                 return ret;
750             }
751 
752             if (track_ramblocks) {
753                 uint64_t reply_gpa;
754 
755                 ret = vhost_user_read(dev, &msg_reply);
756                 if (ret < 0) {
757                     return ret;
758                 }
759 
760                 reply_gpa = msg_reply.payload.mem_reg.region.guest_phys_addr;
761 
762                 if (msg_reply.hdr.request != VHOST_USER_ADD_MEM_REG) {
763                     error_report("%s: Received unexpected msg type."
764                                  "Expected %d received %d", __func__,
765                                  VHOST_USER_ADD_MEM_REG,
766                                  msg_reply.hdr.request);
767                     return -EPROTO;
768                 }
769 
770                 /*
771                  * We're using the same structure, just reusing one of the
772                  * fields, so it should be the same size.
773                  */
774                 if (msg_reply.hdr.size != msg->hdr.size) {
775                     error_report("%s: Unexpected size for postcopy reply "
776                                  "%d vs %d", __func__, msg_reply.hdr.size,
777                                  msg->hdr.size);
778                     return -EPROTO;
779                 }
780 
781                 /* Get the postcopy client base from the backend's reply. */
782                 if (reply_gpa == dev->mem->regions[reg_idx].guest_phys_addr) {
783                     shadow_pcb[reg_idx] =
784                         msg_reply.payload.mem_reg.region.userspace_addr;
785                     trace_vhost_user_set_mem_table_postcopy(
786                         msg_reply.payload.mem_reg.region.userspace_addr,
787                         msg->payload.mem_reg.region.userspace_addr,
788                         reg_fd_idx, reg_idx);
789                 } else {
790                     error_report("%s: invalid postcopy reply for region. "
791                                  "Got guest physical address %" PRIX64 ", expected "
792                                  "%" PRIX64, __func__, reply_gpa,
793                                  dev->mem->regions[reg_idx].guest_phys_addr);
794                     return -EPROTO;
795                 }
796             } else if (reply_supported) {
797                 ret = process_message_reply(dev, msg);
798                 if (ret) {
799                     return ret;
800                 }
801             }
802         } else if (track_ramblocks) {
803             u->region_rb_offset[reg_idx] = 0;
804             u->region_rb[reg_idx] = NULL;
805         }
806 
807         /*
808          * At this point, we know the backend has mapped in the new
809          * region, if the region has a valid file descriptor.
810          *
811          * The region should now be added to the shadow table.
812          */
813         u->shadow_regions[u->num_shadow_regions].guest_phys_addr =
814             reg->guest_phys_addr;
815         u->shadow_regions[u->num_shadow_regions].userspace_addr =
816             reg->userspace_addr;
817         u->shadow_regions[u->num_shadow_regions].memory_size =
818             reg->memory_size;
819         u->num_shadow_regions++;
820     }
821 
822     return 0;
823 }
824 
vhost_user_add_remove_regions(struct vhost_dev * dev,VhostUserMsg * msg,bool reply_supported,bool track_ramblocks)825 static int vhost_user_add_remove_regions(struct vhost_dev *dev,
826                                          VhostUserMsg *msg,
827                                          bool reply_supported,
828                                          bool track_ramblocks)
829 {
830     struct vhost_user *u = dev->opaque;
831     struct scrub_regions add_reg[VHOST_USER_MAX_RAM_SLOTS];
832     struct scrub_regions rem_reg[VHOST_USER_MAX_RAM_SLOTS];
833     uint64_t shadow_pcb[VHOST_USER_MAX_RAM_SLOTS] = {};
834     int nr_add_reg, nr_rem_reg;
835     int ret;
836 
837     msg->hdr.size = sizeof(msg->payload.mem_reg);
838 
839     /* Find the regions which need to be removed or added. */
840     scrub_shadow_regions(dev, add_reg, &nr_add_reg, rem_reg, &nr_rem_reg,
841                          shadow_pcb, track_ramblocks);
842 
843     if (nr_rem_reg) {
844         ret = send_remove_regions(dev, rem_reg, nr_rem_reg, msg,
845                                   reply_supported);
846         if (ret < 0) {
847             goto err;
848         }
849     }
850 
851     if (nr_add_reg) {
852         ret = send_add_regions(dev, add_reg, nr_add_reg, msg, shadow_pcb,
853                                reply_supported, track_ramblocks);
854         if (ret < 0) {
855             goto err;
856         }
857     }
858 
859     if (track_ramblocks) {
860         memcpy(u->postcopy_client_bases, shadow_pcb,
861                sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS);
862         /*
863          * Now we've registered this with the postcopy code, we ack to the
864          * client, because now we're in the position to be able to deal with
865          * any faults it generates.
866          */
867         /* TODO: Use this for failure cases as well with a bad value. */
868         msg->hdr.size = sizeof(msg->payload.u64);
869         msg->payload.u64 = 0; /* OK */
870 
871         ret = vhost_user_write(dev, msg, NULL, 0);
872         if (ret < 0) {
873             return ret;
874         }
875     }
876 
877     return 0;
878 
879 err:
880     if (track_ramblocks) {
881         memcpy(u->postcopy_client_bases, shadow_pcb,
882                sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS);
883     }
884 
885     return ret;
886 }
887 
vhost_user_set_mem_table_postcopy(struct vhost_dev * dev,struct vhost_memory * mem,bool reply_supported,bool config_mem_slots)888 static int vhost_user_set_mem_table_postcopy(struct vhost_dev *dev,
889                                              struct vhost_memory *mem,
890                                              bool reply_supported,
891                                              bool config_mem_slots)
892 {
893     struct vhost_user *u = dev->opaque;
894     int fds[VHOST_MEMORY_BASELINE_NREGIONS];
895     size_t fd_num = 0;
896     VhostUserMsg msg_reply;
897     int region_i, msg_i;
898     int ret;
899 
900     VhostUserMsg msg = {
901         .hdr.flags = VHOST_USER_VERSION,
902     };
903 
904     if (u->region_rb_len < dev->mem->nregions) {
905         u->region_rb = g_renew(RAMBlock*, u->region_rb, dev->mem->nregions);
906         u->region_rb_offset = g_renew(ram_addr_t, u->region_rb_offset,
907                                       dev->mem->nregions);
908         memset(&(u->region_rb[u->region_rb_len]), '\0',
909                sizeof(RAMBlock *) * (dev->mem->nregions - u->region_rb_len));
910         memset(&(u->region_rb_offset[u->region_rb_len]), '\0',
911                sizeof(ram_addr_t) * (dev->mem->nregions - u->region_rb_len));
912         u->region_rb_len = dev->mem->nregions;
913     }
914 
915     if (config_mem_slots) {
916         ret = vhost_user_add_remove_regions(dev, &msg, reply_supported, true);
917         if (ret < 0) {
918             return ret;
919         }
920     } else {
921         ret = vhost_user_fill_set_mem_table_msg(u, dev, &msg, fds, &fd_num,
922                                                 true);
923         if (ret < 0) {
924             return ret;
925         }
926 
927         ret = vhost_user_write(dev, &msg, fds, fd_num);
928         if (ret < 0) {
929             return ret;
930         }
931 
932         ret = vhost_user_read(dev, &msg_reply);
933         if (ret < 0) {
934             return ret;
935         }
936 
937         if (msg_reply.hdr.request != VHOST_USER_SET_MEM_TABLE) {
938             error_report("%s: Received unexpected msg type."
939                          "Expected %d received %d", __func__,
940                          VHOST_USER_SET_MEM_TABLE, msg_reply.hdr.request);
941             return -EPROTO;
942         }
943 
944         /*
945          * We're using the same structure, just reusing one of the
946          * fields, so it should be the same size.
947          */
948         if (msg_reply.hdr.size != msg.hdr.size) {
949             error_report("%s: Unexpected size for postcopy reply "
950                          "%d vs %d", __func__, msg_reply.hdr.size,
951                          msg.hdr.size);
952             return -EPROTO;
953         }
954 
955         memset(u->postcopy_client_bases, 0,
956                sizeof(uint64_t) * VHOST_USER_MAX_RAM_SLOTS);
957 
958         /*
959          * They're in the same order as the regions that were sent
960          * but some of the regions were skipped (above) if they
961          * didn't have fd's
962          */
963         for (msg_i = 0, region_i = 0;
964              region_i < dev->mem->nregions;
965              region_i++) {
966             if (msg_i < fd_num &&
967                 msg_reply.payload.memory.regions[msg_i].guest_phys_addr ==
968                 dev->mem->regions[region_i].guest_phys_addr) {
969                 u->postcopy_client_bases[region_i] =
970                     msg_reply.payload.memory.regions[msg_i].userspace_addr;
971                 trace_vhost_user_set_mem_table_postcopy(
972                     msg_reply.payload.memory.regions[msg_i].userspace_addr,
973                     msg.payload.memory.regions[msg_i].userspace_addr,
974                     msg_i, region_i);
975                 msg_i++;
976             }
977         }
978         if (msg_i != fd_num) {
979             error_report("%s: postcopy reply not fully consumed "
980                          "%d vs %zd",
981                          __func__, msg_i, fd_num);
982             return -EIO;
983         }
984 
985         /*
986          * Now we've registered this with the postcopy code, we ack to the
987          * client, because now we're in the position to be able to deal
988          * with any faults it generates.
989          */
990         /* TODO: Use this for failure cases as well with a bad value. */
991         msg.hdr.size = sizeof(msg.payload.u64);
992         msg.payload.u64 = 0; /* OK */
993         ret = vhost_user_write(dev, &msg, NULL, 0);
994         if (ret < 0) {
995             return ret;
996         }
997     }
998 
999     return 0;
1000 }
1001 
vhost_user_set_mem_table(struct vhost_dev * dev,struct vhost_memory * mem)1002 static int vhost_user_set_mem_table(struct vhost_dev *dev,
1003                                     struct vhost_memory *mem)
1004 {
1005     struct vhost_user *u = dev->opaque;
1006     int fds[VHOST_MEMORY_BASELINE_NREGIONS];
1007     size_t fd_num = 0;
1008     bool do_postcopy = u->postcopy_listen && u->postcopy_fd.handler;
1009     bool reply_supported = virtio_has_feature(dev->protocol_features,
1010                                               VHOST_USER_PROTOCOL_F_REPLY_ACK);
1011     bool config_mem_slots =
1012         virtio_has_feature(dev->protocol_features,
1013                            VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS);
1014     int ret;
1015 
1016     if (do_postcopy) {
1017         /*
1018          * Postcopy has enough differences that it's best done in it's own
1019          * version
1020          */
1021         return vhost_user_set_mem_table_postcopy(dev, mem, reply_supported,
1022                                                  config_mem_slots);
1023     }
1024 
1025     VhostUserMsg msg = {
1026         .hdr.flags = VHOST_USER_VERSION,
1027     };
1028 
1029     if (reply_supported) {
1030         msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
1031     }
1032 
1033     if (config_mem_slots) {
1034         ret = vhost_user_add_remove_regions(dev, &msg, reply_supported, false);
1035         if (ret < 0) {
1036             return ret;
1037         }
1038     } else {
1039         ret = vhost_user_fill_set_mem_table_msg(u, dev, &msg, fds, &fd_num,
1040                                                 false);
1041         if (ret < 0) {
1042             return ret;
1043         }
1044 
1045         ret = vhost_user_write(dev, &msg, fds, fd_num);
1046         if (ret < 0) {
1047             return ret;
1048         }
1049 
1050         if (reply_supported) {
1051             return process_message_reply(dev, &msg);
1052         }
1053     }
1054 
1055     return 0;
1056 }
1057 
vhost_user_set_vring_endian(struct vhost_dev * dev,struct vhost_vring_state * ring)1058 static int vhost_user_set_vring_endian(struct vhost_dev *dev,
1059                                        struct vhost_vring_state *ring)
1060 {
1061     bool cross_endian = virtio_has_feature(dev->protocol_features,
1062                                            VHOST_USER_PROTOCOL_F_CROSS_ENDIAN);
1063     VhostUserMsg msg = {
1064         .hdr.request = VHOST_USER_SET_VRING_ENDIAN,
1065         .hdr.flags = VHOST_USER_VERSION,
1066         .payload.state = *ring,
1067         .hdr.size = sizeof(msg.payload.state),
1068     };
1069 
1070     if (!cross_endian) {
1071         error_report("vhost-user trying to send unhandled ioctl");
1072         return -ENOTSUP;
1073     }
1074 
1075     return vhost_user_write(dev, &msg, NULL, 0);
1076 }
1077 
vhost_user_get_u64(struct vhost_dev * dev,int request,uint64_t * u64)1078 static int vhost_user_get_u64(struct vhost_dev *dev, int request, uint64_t *u64)
1079 {
1080     int ret;
1081     VhostUserMsg msg = {
1082         .hdr.request = request,
1083         .hdr.flags = VHOST_USER_VERSION,
1084     };
1085 
1086     if (vhost_user_per_device_request(request) && dev->vq_index != 0) {
1087         return 0;
1088     }
1089 
1090     ret = vhost_user_write(dev, &msg, NULL, 0);
1091     if (ret < 0) {
1092         return ret;
1093     }
1094 
1095     ret = vhost_user_read(dev, &msg);
1096     if (ret < 0) {
1097         return ret;
1098     }
1099 
1100     if (msg.hdr.request != request) {
1101         error_report("Received unexpected msg type. Expected %d received %d",
1102                      request, msg.hdr.request);
1103         return -EPROTO;
1104     }
1105 
1106     if (msg.hdr.size != sizeof(msg.payload.u64)) {
1107         error_report("Received bad msg size.");
1108         return -EPROTO;
1109     }
1110 
1111     *u64 = msg.payload.u64;
1112 
1113     return 0;
1114 }
1115 
vhost_user_get_features(struct vhost_dev * dev,uint64_t * features)1116 static int vhost_user_get_features(struct vhost_dev *dev, uint64_t *features)
1117 {
1118     if (vhost_user_get_u64(dev, VHOST_USER_GET_FEATURES, features) < 0) {
1119         return -EPROTO;
1120     }
1121 
1122     return 0;
1123 }
1124 
1125 /* Note: "msg->hdr.flags" may be modified. */
vhost_user_write_sync(struct vhost_dev * dev,VhostUserMsg * msg,bool wait_for_reply)1126 static int vhost_user_write_sync(struct vhost_dev *dev, VhostUserMsg *msg,
1127                                  bool wait_for_reply)
1128 {
1129     int ret;
1130 
1131     if (wait_for_reply) {
1132         bool reply_supported = virtio_has_feature(dev->protocol_features,
1133                                           VHOST_USER_PROTOCOL_F_REPLY_ACK);
1134         if (reply_supported) {
1135             msg->hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
1136         }
1137     }
1138 
1139     ret = vhost_user_write(dev, msg, NULL, 0);
1140     if (ret < 0) {
1141         return ret;
1142     }
1143 
1144     if (wait_for_reply) {
1145         uint64_t dummy;
1146 
1147         if (msg->hdr.flags & VHOST_USER_NEED_REPLY_MASK) {
1148             return process_message_reply(dev, msg);
1149         }
1150 
1151        /*
1152         * We need to wait for a reply but the backend does not
1153         * support replies for the command we just sent.
1154         * Send VHOST_USER_GET_FEATURES which makes all backends
1155         * send a reply.
1156         */
1157         return vhost_user_get_features(dev, &dummy);
1158     }
1159 
1160     return 0;
1161 }
1162 
vhost_set_vring(struct vhost_dev * dev,unsigned long int request,struct vhost_vring_state * ring,bool wait_for_reply)1163 static int vhost_set_vring(struct vhost_dev *dev,
1164                            unsigned long int request,
1165                            struct vhost_vring_state *ring,
1166                            bool wait_for_reply)
1167 {
1168     VhostUserMsg msg = {
1169         .hdr.request = request,
1170         .hdr.flags = VHOST_USER_VERSION,
1171         .payload.state = *ring,
1172         .hdr.size = sizeof(msg.payload.state),
1173     };
1174 
1175     return vhost_user_write_sync(dev, &msg, wait_for_reply);
1176 }
1177 
vhost_user_set_vring_num(struct vhost_dev * dev,struct vhost_vring_state * ring)1178 static int vhost_user_set_vring_num(struct vhost_dev *dev,
1179                                     struct vhost_vring_state *ring)
1180 {
1181     return vhost_set_vring(dev, VHOST_USER_SET_VRING_NUM, ring, false);
1182 }
1183 
vhost_user_host_notifier_free(VhostUserHostNotifier * n)1184 static void vhost_user_host_notifier_free(VhostUserHostNotifier *n)
1185 {
1186     if (n->unmap_addr) {
1187         munmap(n->unmap_addr, qemu_real_host_page_size());
1188         n->unmap_addr = NULL;
1189     }
1190     if (n->destroy) {
1191         memory_region_transaction_begin();
1192         object_unparent(OBJECT(&n->mr));
1193         memory_region_transaction_commit();
1194         g_free(n);
1195     }
1196 }
1197 
1198 /*
1199  * clean-up function for notifier, will finally free the structure
1200  * under rcu.
1201  */
vhost_user_host_notifier_remove(VhostUserHostNotifier * n,VirtIODevice * vdev,bool destroy)1202 static void vhost_user_host_notifier_remove(VhostUserHostNotifier *n,
1203                                             VirtIODevice *vdev, bool destroy)
1204 {
1205     /*
1206      * if destroy == false and n->addr == NULL, we have nothing to do.
1207      * so, just return.
1208      */
1209     if (!n || (!destroy && !n->addr)) {
1210         return;
1211     }
1212 
1213     if (n->addr) {
1214         if (vdev) {
1215             memory_region_transaction_begin();
1216             virtio_queue_set_host_notifier_mr(vdev, n->idx, &n->mr, false);
1217             memory_region_transaction_commit();
1218         }
1219         assert(!n->unmap_addr);
1220         n->unmap_addr = n->addr;
1221         n->addr = NULL;
1222     }
1223     n->destroy = destroy;
1224     call_rcu(n, vhost_user_host_notifier_free, rcu);
1225 }
1226 
vhost_user_set_vring_base(struct vhost_dev * dev,struct vhost_vring_state * ring)1227 static int vhost_user_set_vring_base(struct vhost_dev *dev,
1228                                      struct vhost_vring_state *ring)
1229 {
1230     return vhost_set_vring(dev, VHOST_USER_SET_VRING_BASE, ring, false);
1231 }
1232 
vhost_user_set_vring_enable(struct vhost_dev * dev,int enable)1233 static int vhost_user_set_vring_enable(struct vhost_dev *dev, int enable)
1234 {
1235     int i;
1236 
1237     if (!virtio_has_feature(dev->features, VHOST_USER_F_PROTOCOL_FEATURES)) {
1238         return -EINVAL;
1239     }
1240 
1241     for (i = 0; i < dev->nvqs; ++i) {
1242         int ret;
1243         struct vhost_vring_state state = {
1244             .index = dev->vq_index + i,
1245             .num   = enable,
1246         };
1247 
1248         /*
1249          * SET_VRING_ENABLE travels from guest to QEMU to vhost-user backend /
1250          * control plane thread via unix domain socket. Virtio requests travel
1251          * from guest to vhost-user backend / data plane thread via eventfd.
1252          * Even if the guest enables the ring first, and pushes its first virtio
1253          * request second (conforming to the virtio spec), the data plane thread
1254          * in the backend may see the virtio request before the control plane
1255          * thread sees the queue enablement. This causes (in fact, requires) the
1256          * data plane thread to discard the virtio request (it arrived on a
1257          * seemingly disabled queue). To prevent this out-of-order delivery,
1258          * don't let the guest proceed to pushing the virtio request until the
1259          * backend control plane acknowledges enabling the queue -- IOW, pass
1260          * wait_for_reply=true below.
1261          */
1262         ret = vhost_set_vring(dev, VHOST_USER_SET_VRING_ENABLE, &state, true);
1263         if (ret < 0) {
1264             /*
1265              * Restoring the previous state is likely infeasible, as well as
1266              * proceeding regardless the error, so just bail out and hope for
1267              * the device-level recovery.
1268              */
1269             return ret;
1270         }
1271     }
1272 
1273     return 0;
1274 }
1275 
fetch_notifier(VhostUserState * u,int idx)1276 static VhostUserHostNotifier *fetch_notifier(VhostUserState *u,
1277                                              int idx)
1278 {
1279     if (idx >= u->notifiers->len) {
1280         return NULL;
1281     }
1282     return g_ptr_array_index(u->notifiers, idx);
1283 }
1284 
vhost_user_get_vring_base(struct vhost_dev * dev,struct vhost_vring_state * ring)1285 static int vhost_user_get_vring_base(struct vhost_dev *dev,
1286                                      struct vhost_vring_state *ring)
1287 {
1288     int ret;
1289     VhostUserMsg msg = {
1290         .hdr.request = VHOST_USER_GET_VRING_BASE,
1291         .hdr.flags = VHOST_USER_VERSION,
1292         .payload.state = *ring,
1293         .hdr.size = sizeof(msg.payload.state),
1294     };
1295     struct vhost_user *u = dev->opaque;
1296 
1297     VhostUserHostNotifier *n = fetch_notifier(u->user, ring->index);
1298     vhost_user_host_notifier_remove(n, dev->vdev, false);
1299 
1300     ret = vhost_user_write(dev, &msg, NULL, 0);
1301     if (ret < 0) {
1302         return ret;
1303     }
1304 
1305     ret = vhost_user_read(dev, &msg);
1306     if (ret < 0) {
1307         return ret;
1308     }
1309 
1310     if (msg.hdr.request != VHOST_USER_GET_VRING_BASE) {
1311         error_report("Received unexpected msg type. Expected %d received %d",
1312                      VHOST_USER_GET_VRING_BASE, msg.hdr.request);
1313         return -EPROTO;
1314     }
1315 
1316     if (msg.hdr.size != sizeof(msg.payload.state)) {
1317         error_report("Received bad msg size.");
1318         return -EPROTO;
1319     }
1320 
1321     *ring = msg.payload.state;
1322 
1323     return 0;
1324 }
1325 
vhost_set_vring_file(struct vhost_dev * dev,VhostUserRequest request,struct vhost_vring_file * file)1326 static int vhost_set_vring_file(struct vhost_dev *dev,
1327                                 VhostUserRequest request,
1328                                 struct vhost_vring_file *file)
1329 {
1330     int fds[VHOST_USER_MAX_RAM_SLOTS];
1331     size_t fd_num = 0;
1332     VhostUserMsg msg = {
1333         .hdr.request = request,
1334         .hdr.flags = VHOST_USER_VERSION,
1335         .payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK,
1336         .hdr.size = sizeof(msg.payload.u64),
1337     };
1338 
1339     if (file->fd > 0) {
1340         fds[fd_num++] = file->fd;
1341     } else {
1342         msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
1343     }
1344 
1345     return vhost_user_write(dev, &msg, fds, fd_num);
1346 }
1347 
vhost_user_set_vring_kick(struct vhost_dev * dev,struct vhost_vring_file * file)1348 static int vhost_user_set_vring_kick(struct vhost_dev *dev,
1349                                      struct vhost_vring_file *file)
1350 {
1351     return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_KICK, file);
1352 }
1353 
vhost_user_set_vring_call(struct vhost_dev * dev,struct vhost_vring_file * file)1354 static int vhost_user_set_vring_call(struct vhost_dev *dev,
1355                                      struct vhost_vring_file *file)
1356 {
1357     return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_CALL, file);
1358 }
1359 
vhost_user_set_vring_err(struct vhost_dev * dev,struct vhost_vring_file * file)1360 static int vhost_user_set_vring_err(struct vhost_dev *dev,
1361                                     struct vhost_vring_file *file)
1362 {
1363     return vhost_set_vring_file(dev, VHOST_USER_SET_VRING_ERR, file);
1364 }
1365 
vhost_user_set_vring_addr(struct vhost_dev * dev,struct vhost_vring_addr * addr)1366 static int vhost_user_set_vring_addr(struct vhost_dev *dev,
1367                                      struct vhost_vring_addr *addr)
1368 {
1369     VhostUserMsg msg = {
1370         .hdr.request = VHOST_USER_SET_VRING_ADDR,
1371         .hdr.flags = VHOST_USER_VERSION,
1372         .payload.addr = *addr,
1373         .hdr.size = sizeof(msg.payload.addr),
1374     };
1375 
1376     /*
1377      * wait for a reply if logging is enabled to make sure
1378      * backend is actually logging changes
1379      */
1380     bool wait_for_reply = addr->flags & (1 << VHOST_VRING_F_LOG);
1381 
1382     return vhost_user_write_sync(dev, &msg, wait_for_reply);
1383 }
1384 
vhost_user_set_u64(struct vhost_dev * dev,int request,uint64_t u64,bool wait_for_reply)1385 static int vhost_user_set_u64(struct vhost_dev *dev, int request, uint64_t u64,
1386                               bool wait_for_reply)
1387 {
1388     VhostUserMsg msg = {
1389         .hdr.request = request,
1390         .hdr.flags = VHOST_USER_VERSION,
1391         .payload.u64 = u64,
1392         .hdr.size = sizeof(msg.payload.u64),
1393     };
1394 
1395     return vhost_user_write_sync(dev, &msg, wait_for_reply);
1396 }
1397 
vhost_user_set_status(struct vhost_dev * dev,uint8_t status)1398 static int vhost_user_set_status(struct vhost_dev *dev, uint8_t status)
1399 {
1400     return vhost_user_set_u64(dev, VHOST_USER_SET_STATUS, status, false);
1401 }
1402 
vhost_user_get_status(struct vhost_dev * dev,uint8_t * status)1403 static int vhost_user_get_status(struct vhost_dev *dev, uint8_t *status)
1404 {
1405     uint64_t value;
1406     int ret;
1407 
1408     ret = vhost_user_get_u64(dev, VHOST_USER_GET_STATUS, &value);
1409     if (ret < 0) {
1410         return ret;
1411     }
1412     *status = value;
1413 
1414     return 0;
1415 }
1416 
vhost_user_add_status(struct vhost_dev * dev,uint8_t status)1417 static int vhost_user_add_status(struct vhost_dev *dev, uint8_t status)
1418 {
1419     uint8_t s;
1420     int ret;
1421 
1422     ret = vhost_user_get_status(dev, &s);
1423     if (ret < 0) {
1424         return ret;
1425     }
1426 
1427     if ((s & status) == status) {
1428         return 0;
1429     }
1430     s |= status;
1431 
1432     return vhost_user_set_status(dev, s);
1433 }
1434 
vhost_user_set_features(struct vhost_dev * dev,uint64_t features)1435 static int vhost_user_set_features(struct vhost_dev *dev,
1436                                    uint64_t features)
1437 {
1438     /*
1439      * wait for a reply if logging is enabled to make sure
1440      * backend is actually logging changes
1441      */
1442     bool log_enabled = features & (0x1ULL << VHOST_F_LOG_ALL);
1443     int ret;
1444 
1445     /*
1446      * We need to include any extra backend only feature bits that
1447      * might be needed by our device. Currently this includes the
1448      * VHOST_USER_F_PROTOCOL_FEATURES bit for enabling protocol
1449      * features.
1450      */
1451     ret = vhost_user_set_u64(dev, VHOST_USER_SET_FEATURES,
1452                               features | dev->backend_features,
1453                               log_enabled);
1454 
1455     if (virtio_has_feature(dev->protocol_features,
1456                            VHOST_USER_PROTOCOL_F_STATUS)) {
1457         if (!ret) {
1458             return vhost_user_add_status(dev, VIRTIO_CONFIG_S_FEATURES_OK);
1459         }
1460     }
1461 
1462     return ret;
1463 }
1464 
vhost_user_set_protocol_features(struct vhost_dev * dev,uint64_t features)1465 static int vhost_user_set_protocol_features(struct vhost_dev *dev,
1466                                             uint64_t features)
1467 {
1468     return vhost_user_set_u64(dev, VHOST_USER_SET_PROTOCOL_FEATURES, features,
1469                               false);
1470 }
1471 
vhost_user_set_owner(struct vhost_dev * dev)1472 static int vhost_user_set_owner(struct vhost_dev *dev)
1473 {
1474     VhostUserMsg msg = {
1475         .hdr.request = VHOST_USER_SET_OWNER,
1476         .hdr.flags = VHOST_USER_VERSION,
1477     };
1478 
1479     return vhost_user_write(dev, &msg, NULL, 0);
1480 }
1481 
vhost_user_get_max_memslots(struct vhost_dev * dev,uint64_t * max_memslots)1482 static int vhost_user_get_max_memslots(struct vhost_dev *dev,
1483                                        uint64_t *max_memslots)
1484 {
1485     uint64_t backend_max_memslots;
1486     int err;
1487 
1488     err = vhost_user_get_u64(dev, VHOST_USER_GET_MAX_MEM_SLOTS,
1489                              &backend_max_memslots);
1490     if (err < 0) {
1491         return err;
1492     }
1493 
1494     *max_memslots = backend_max_memslots;
1495 
1496     return 0;
1497 }
1498 
vhost_user_reset_device(struct vhost_dev * dev)1499 static int vhost_user_reset_device(struct vhost_dev *dev)
1500 {
1501     VhostUserMsg msg = {
1502         .hdr.flags = VHOST_USER_VERSION,
1503         .hdr.request = VHOST_USER_RESET_DEVICE,
1504     };
1505 
1506     /*
1507      * Historically, reset was not implemented so only reset devices
1508      * that are expecting it.
1509      */
1510     if (!virtio_has_feature(dev->protocol_features,
1511                             VHOST_USER_PROTOCOL_F_RESET_DEVICE)) {
1512         return -ENOSYS;
1513     }
1514 
1515     return vhost_user_write(dev, &msg, NULL, 0);
1516 }
1517 
vhost_user_backend_handle_config_change(struct vhost_dev * dev)1518 static int vhost_user_backend_handle_config_change(struct vhost_dev *dev)
1519 {
1520     if (!dev->config_ops || !dev->config_ops->vhost_dev_config_notifier) {
1521         return -ENOSYS;
1522     }
1523 
1524     return dev->config_ops->vhost_dev_config_notifier(dev);
1525 }
1526 
1527 /*
1528  * Fetch or create the notifier for a given idx. Newly created
1529  * notifiers are added to the pointer array that tracks them.
1530  */
fetch_or_create_notifier(VhostUserState * u,int idx)1531 static VhostUserHostNotifier *fetch_or_create_notifier(VhostUserState *u,
1532                                                        int idx)
1533 {
1534     VhostUserHostNotifier *n = NULL;
1535     if (idx >= u->notifiers->len) {
1536         g_ptr_array_set_size(u->notifiers, idx + 1);
1537     }
1538 
1539     n = g_ptr_array_index(u->notifiers, idx);
1540     if (!n) {
1541         /*
1542          * In case notification arrive out-of-order,
1543          * make room for current index.
1544          */
1545         g_ptr_array_remove_index(u->notifiers, idx);
1546         n = g_new0(VhostUserHostNotifier, 1);
1547         n->idx = idx;
1548         g_ptr_array_insert(u->notifiers, idx, n);
1549         trace_vhost_user_create_notifier(idx, n);
1550     }
1551 
1552     return n;
1553 }
1554 
vhost_user_backend_handle_vring_host_notifier(struct vhost_dev * dev,VhostUserVringArea * area,int fd)1555 static int vhost_user_backend_handle_vring_host_notifier(struct vhost_dev *dev,
1556                                                        VhostUserVringArea *area,
1557                                                        int fd)
1558 {
1559     int queue_idx = area->u64 & VHOST_USER_VRING_IDX_MASK;
1560     size_t page_size = qemu_real_host_page_size();
1561     struct vhost_user *u = dev->opaque;
1562     VhostUserState *user = u->user;
1563     VirtIODevice *vdev = dev->vdev;
1564     VhostUserHostNotifier *n;
1565     void *addr;
1566     char *name;
1567 
1568     if (!virtio_has_feature(dev->protocol_features,
1569                             VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) ||
1570         vdev == NULL || queue_idx >= virtio_get_num_queues(vdev)) {
1571         return -EINVAL;
1572     }
1573 
1574     /*
1575      * Fetch notifier and invalidate any old data before setting up
1576      * new mapped address.
1577      */
1578     n = fetch_or_create_notifier(user, queue_idx);
1579     vhost_user_host_notifier_remove(n, vdev, false);
1580 
1581     if (area->u64 & VHOST_USER_VRING_NOFD_MASK) {
1582         return 0;
1583     }
1584 
1585     /* Sanity check. */
1586     if (area->size != page_size) {
1587         return -EINVAL;
1588     }
1589 
1590     addr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED,
1591                 fd, area->offset);
1592     if (addr == MAP_FAILED) {
1593         return -EFAULT;
1594     }
1595 
1596     name = g_strdup_printf("vhost-user/host-notifier@%p mmaps[%d]",
1597                            user, queue_idx);
1598     if (!n->mr.ram) { /* Don't init again after suspend. */
1599         memory_region_init_ram_device_ptr(&n->mr, OBJECT(vdev), name,
1600                                           page_size, addr);
1601     } else {
1602         n->mr.ram_block->host = addr;
1603     }
1604     g_free(name);
1605 
1606     if (virtio_queue_set_host_notifier_mr(vdev, queue_idx, &n->mr, true)) {
1607         object_unparent(OBJECT(&n->mr));
1608         munmap(addr, page_size);
1609         return -ENXIO;
1610     }
1611 
1612     n->addr = addr;
1613 
1614     return 0;
1615 }
1616 
1617 static int
vhost_user_backend_handle_shared_object_add(struct vhost_dev * dev,VhostUserShared * object)1618 vhost_user_backend_handle_shared_object_add(struct vhost_dev *dev,
1619                                             VhostUserShared *object)
1620 {
1621     QemuUUID uuid;
1622 
1623     memcpy(uuid.data, object->uuid, sizeof(object->uuid));
1624     return !virtio_add_vhost_device(&uuid, dev);
1625 }
1626 
1627 /*
1628  * Handle VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE backend requests.
1629  *
1630  * Return: 0 on success, 1 on error.
1631  */
1632 static int
vhost_user_backend_handle_shared_object_remove(struct vhost_dev * dev,VhostUserShared * object)1633 vhost_user_backend_handle_shared_object_remove(struct vhost_dev *dev,
1634                                                VhostUserShared *object)
1635 {
1636     QemuUUID uuid;
1637 
1638     memcpy(uuid.data, object->uuid, sizeof(object->uuid));
1639     switch (virtio_object_type(&uuid)) {
1640     case TYPE_VHOST_DEV:
1641     {
1642         struct vhost_dev *owner = virtio_lookup_vhost_device(&uuid);
1643         if (dev != owner) {
1644             /* Not allowed to remove non-owned entries */
1645             return 1;
1646         }
1647         break;
1648     }
1649     default:
1650         /* Not allowed to remove non-owned entries */
1651         return 1;
1652     }
1653 
1654     return !virtio_remove_resource(&uuid);
1655 }
1656 
vhost_user_send_resp(QIOChannel * ioc,VhostUserHeader * hdr,VhostUserPayload * payload,Error ** errp)1657 static bool vhost_user_send_resp(QIOChannel *ioc, VhostUserHeader *hdr,
1658                                  VhostUserPayload *payload, Error **errp)
1659 {
1660     struct iovec iov[] = {
1661         { .iov_base = hdr,      .iov_len = VHOST_USER_HDR_SIZE },
1662         { .iov_base = payload,  .iov_len = hdr->size },
1663     };
1664 
1665     hdr->flags &= ~VHOST_USER_NEED_REPLY_MASK;
1666     hdr->flags |= VHOST_USER_REPLY_MASK;
1667 
1668     return !qio_channel_writev_all(ioc, iov, ARRAY_SIZE(iov), errp);
1669 }
1670 
vhost_user_get_shared_object(struct vhost_dev * dev,unsigned char * uuid,int * dmabuf_fd)1671 int vhost_user_get_shared_object(struct vhost_dev *dev, unsigned char *uuid,
1672                                  int *dmabuf_fd)
1673 {
1674     struct vhost_user *u = dev->opaque;
1675     CharBackend *chr = u->user->chr;
1676     int ret;
1677     VhostUserMsg msg = {
1678         .hdr.request = VHOST_USER_GET_SHARED_OBJECT,
1679         .hdr.flags = VHOST_USER_VERSION,
1680     };
1681     memcpy(msg.payload.object.uuid, uuid, sizeof(msg.payload.object.uuid));
1682 
1683     ret = vhost_user_write(dev, &msg, NULL, 0);
1684     if (ret < 0) {
1685         return ret;
1686     }
1687 
1688     ret = vhost_user_read(dev, &msg);
1689     if (ret < 0) {
1690         return ret;
1691     }
1692 
1693     if (msg.hdr.request != VHOST_USER_GET_SHARED_OBJECT) {
1694         error_report("Received unexpected msg type. "
1695                      "Expected %d received %d",
1696                      VHOST_USER_GET_SHARED_OBJECT, msg.hdr.request);
1697         return -EPROTO;
1698     }
1699 
1700     *dmabuf_fd = qemu_chr_fe_get_msgfd(chr);
1701     if (*dmabuf_fd < 0) {
1702         error_report("Failed to get dmabuf fd");
1703         return -EIO;
1704     }
1705 
1706     return 0;
1707 }
1708 
1709 static int
vhost_user_backend_handle_shared_object_lookup(struct vhost_user * u,VhostUserShared * object)1710 vhost_user_backend_handle_shared_object_lookup(struct vhost_user *u,
1711                                                VhostUserShared *object)
1712 {
1713     QemuUUID uuid;
1714     CharBackend *chr = u->user->chr;
1715     int dmabuf_fd = -1;
1716     int fd_num = 0;
1717 
1718     memcpy(uuid.data, object->uuid, sizeof(object->uuid));
1719 
1720     switch (virtio_object_type(&uuid)) {
1721     case TYPE_DMABUF:
1722         dmabuf_fd = virtio_lookup_dmabuf(&uuid);
1723         break;
1724     case TYPE_VHOST_DEV:
1725     {
1726         struct vhost_dev *dev = virtio_lookup_vhost_device(&uuid);
1727         if (dev == NULL) {
1728             return -EINVAL;
1729         }
1730         int ret = vhost_user_get_shared_object(dev, uuid.data, &dmabuf_fd);
1731         if (ret < 0) {
1732             return ret;
1733         }
1734         break;
1735     }
1736     case TYPE_INVALID:
1737         return -EINVAL;
1738     }
1739 
1740     if (dmabuf_fd != -1) {
1741         fd_num++;
1742     }
1743 
1744     if (qemu_chr_fe_set_msgfds(chr, &dmabuf_fd, fd_num) < 0) {
1745         error_report("Failed to set msg fds.");
1746         return -EINVAL;
1747     }
1748 
1749     return 0;
1750 }
1751 
close_backend_channel(struct vhost_user * u)1752 static void close_backend_channel(struct vhost_user *u)
1753 {
1754     g_source_destroy(u->backend_src);
1755     g_source_unref(u->backend_src);
1756     u->backend_src = NULL;
1757     object_unref(OBJECT(u->backend_ioc));
1758     u->backend_ioc = NULL;
1759 }
1760 
backend_read(QIOChannel * ioc,GIOCondition condition,gpointer opaque)1761 static gboolean backend_read(QIOChannel *ioc, GIOCondition condition,
1762                            gpointer opaque)
1763 {
1764     struct vhost_dev *dev = opaque;
1765     struct vhost_user *u = dev->opaque;
1766     VhostUserHeader hdr = { 0, };
1767     VhostUserPayload payload = { 0, };
1768     Error *local_err = NULL;
1769     gboolean rc = G_SOURCE_CONTINUE;
1770     int ret = 0;
1771     struct iovec iov;
1772     g_autofree int *fd = NULL;
1773     size_t fdsize = 0;
1774     bool reply_ack;
1775     int i;
1776 
1777     /* Read header */
1778     iov.iov_base = &hdr;
1779     iov.iov_len = VHOST_USER_HDR_SIZE;
1780 
1781     if (qio_channel_readv_full_all(ioc, &iov, 1, &fd, &fdsize, &local_err)) {
1782         error_report_err(local_err);
1783         goto err;
1784     }
1785 
1786     if (hdr.size > VHOST_USER_PAYLOAD_SIZE) {
1787         error_report("Failed to read msg header."
1788                 " Size %d exceeds the maximum %zu.", hdr.size,
1789                 VHOST_USER_PAYLOAD_SIZE);
1790         goto err;
1791     }
1792 
1793     reply_ack = hdr.flags & VHOST_USER_NEED_REPLY_MASK;
1794 
1795     /* Read payload */
1796     if (qio_channel_read_all(ioc, (char *) &payload, hdr.size, &local_err)) {
1797         error_report_err(local_err);
1798         goto err;
1799     }
1800 
1801     switch (hdr.request) {
1802     case VHOST_USER_BACKEND_IOTLB_MSG:
1803         ret = vhost_backend_handle_iotlb_msg(dev, &payload.iotlb);
1804         break;
1805     case VHOST_USER_BACKEND_CONFIG_CHANGE_MSG:
1806         ret = vhost_user_backend_handle_config_change(dev);
1807         break;
1808     case VHOST_USER_BACKEND_VRING_HOST_NOTIFIER_MSG:
1809         ret = vhost_user_backend_handle_vring_host_notifier(dev, &payload.area,
1810                                                           fd ? fd[0] : -1);
1811         break;
1812     case VHOST_USER_BACKEND_SHARED_OBJECT_ADD:
1813         ret = vhost_user_backend_handle_shared_object_add(dev, &payload.object);
1814         break;
1815     case VHOST_USER_BACKEND_SHARED_OBJECT_REMOVE:
1816         ret = vhost_user_backend_handle_shared_object_remove(dev,
1817                                                              &payload.object);
1818         break;
1819     case VHOST_USER_BACKEND_SHARED_OBJECT_LOOKUP:
1820         /* The backend always expects a response */
1821         reply_ack = true;
1822         ret = vhost_user_backend_handle_shared_object_lookup(dev->opaque,
1823                                                              &payload.object);
1824         break;
1825     default:
1826         error_report("Received unexpected msg type: %d.", hdr.request);
1827         ret = -EINVAL;
1828     }
1829 
1830     /*
1831      * REPLY_ACK feature handling. Other reply types has to be managed
1832      * directly in their request handlers.
1833      */
1834     if (reply_ack) {
1835         payload.u64 = !!ret;
1836         hdr.size = sizeof(payload.u64);
1837 
1838         if (!vhost_user_send_resp(ioc, &hdr, &payload, &local_err)) {
1839             error_report_err(local_err);
1840             goto err;
1841         }
1842     }
1843 
1844     goto fdcleanup;
1845 
1846 err:
1847     close_backend_channel(u);
1848     rc = G_SOURCE_REMOVE;
1849 
1850 fdcleanup:
1851     if (fd) {
1852         for (i = 0; i < fdsize; i++) {
1853             close(fd[i]);
1854         }
1855     }
1856     return rc;
1857 }
1858 
vhost_setup_backend_channel(struct vhost_dev * dev)1859 static int vhost_setup_backend_channel(struct vhost_dev *dev)
1860 {
1861     VhostUserMsg msg = {
1862         .hdr.request = VHOST_USER_SET_BACKEND_REQ_FD,
1863         .hdr.flags = VHOST_USER_VERSION,
1864     };
1865     struct vhost_user *u = dev->opaque;
1866     int sv[2], ret = 0;
1867     bool reply_supported = virtio_has_feature(dev->protocol_features,
1868                                               VHOST_USER_PROTOCOL_F_REPLY_ACK);
1869     Error *local_err = NULL;
1870     QIOChannel *ioc;
1871 
1872     if (!virtio_has_feature(dev->protocol_features,
1873                             VHOST_USER_PROTOCOL_F_BACKEND_REQ)) {
1874         return 0;
1875     }
1876 
1877     if (qemu_socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1) {
1878         int saved_errno = errno;
1879         error_report("socketpair() failed");
1880         return -saved_errno;
1881     }
1882 
1883     ioc = QIO_CHANNEL(qio_channel_socket_new_fd(sv[0], &local_err));
1884     if (!ioc) {
1885         error_report_err(local_err);
1886         return -ECONNREFUSED;
1887     }
1888     u->backend_ioc = ioc;
1889     u->backend_src = qio_channel_add_watch_source(u->backend_ioc,
1890                                                 G_IO_IN | G_IO_HUP,
1891                                                 backend_read, dev, NULL, NULL);
1892 
1893     if (reply_supported) {
1894         msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
1895     }
1896 
1897     ret = vhost_user_write(dev, &msg, &sv[1], 1);
1898     if (ret) {
1899         goto out;
1900     }
1901 
1902     if (reply_supported) {
1903         ret = process_message_reply(dev, &msg);
1904     }
1905 
1906 out:
1907     close(sv[1]);
1908     if (ret) {
1909         close_backend_channel(u);
1910     }
1911 
1912     return ret;
1913 }
1914 
1915 #ifdef CONFIG_LINUX
1916 /*
1917  * Called back from the postcopy fault thread when a fault is received on our
1918  * ufd.
1919  * TODO: This is Linux specific
1920  */
vhost_user_postcopy_fault_handler(struct PostCopyFD * pcfd,void * ufd)1921 static int vhost_user_postcopy_fault_handler(struct PostCopyFD *pcfd,
1922                                              void *ufd)
1923 {
1924     struct vhost_dev *dev = pcfd->data;
1925     struct vhost_user *u = dev->opaque;
1926     struct uffd_msg *msg = ufd;
1927     uint64_t faultaddr = msg->arg.pagefault.address;
1928     RAMBlock *rb = NULL;
1929     uint64_t rb_offset;
1930     int i;
1931 
1932     trace_vhost_user_postcopy_fault_handler(pcfd->idstr, faultaddr,
1933                                             dev->mem->nregions);
1934     for (i = 0; i < MIN(dev->mem->nregions, u->region_rb_len); i++) {
1935         trace_vhost_user_postcopy_fault_handler_loop(i,
1936                 u->postcopy_client_bases[i], dev->mem->regions[i].memory_size);
1937         if (faultaddr >= u->postcopy_client_bases[i]) {
1938             /* Ofset of the fault address in the vhost region */
1939             uint64_t region_offset = faultaddr - u->postcopy_client_bases[i];
1940             if (region_offset < dev->mem->regions[i].memory_size) {
1941                 rb_offset = region_offset + u->region_rb_offset[i];
1942                 trace_vhost_user_postcopy_fault_handler_found(i,
1943                         region_offset, rb_offset);
1944                 rb = u->region_rb[i];
1945                 return postcopy_request_shared_page(pcfd, rb, faultaddr,
1946                                                     rb_offset);
1947             }
1948         }
1949     }
1950     error_report("%s: Failed to find region for fault %" PRIx64,
1951                  __func__, faultaddr);
1952     return -1;
1953 }
1954 
vhost_user_postcopy_waker(struct PostCopyFD * pcfd,RAMBlock * rb,uint64_t offset)1955 static int vhost_user_postcopy_waker(struct PostCopyFD *pcfd, RAMBlock *rb,
1956                                      uint64_t offset)
1957 {
1958     struct vhost_dev *dev = pcfd->data;
1959     struct vhost_user *u = dev->opaque;
1960     int i;
1961 
1962     trace_vhost_user_postcopy_waker(qemu_ram_get_idstr(rb), offset);
1963 
1964     if (!u) {
1965         return 0;
1966     }
1967     /* Translate the offset into an address in the clients address space */
1968     for (i = 0; i < MIN(dev->mem->nregions, u->region_rb_len); i++) {
1969         if (u->region_rb[i] == rb &&
1970             offset >= u->region_rb_offset[i] &&
1971             offset < (u->region_rb_offset[i] +
1972                       dev->mem->regions[i].memory_size)) {
1973             uint64_t client_addr = (offset - u->region_rb_offset[i]) +
1974                                    u->postcopy_client_bases[i];
1975             trace_vhost_user_postcopy_waker_found(client_addr);
1976             return postcopy_wake_shared(pcfd, client_addr, rb);
1977         }
1978     }
1979 
1980     trace_vhost_user_postcopy_waker_nomatch(qemu_ram_get_idstr(rb), offset);
1981     return 0;
1982 }
1983 #endif
1984 
1985 /*
1986  * Called at the start of an inbound postcopy on reception of the
1987  * 'advise' command.
1988  */
vhost_user_postcopy_advise(struct vhost_dev * dev,Error ** errp)1989 static int vhost_user_postcopy_advise(struct vhost_dev *dev, Error **errp)
1990 {
1991 #ifdef CONFIG_LINUX
1992     struct vhost_user *u = dev->opaque;
1993     CharBackend *chr = u->user->chr;
1994     int ufd;
1995     int ret;
1996     VhostUserMsg msg = {
1997         .hdr.request = VHOST_USER_POSTCOPY_ADVISE,
1998         .hdr.flags = VHOST_USER_VERSION,
1999     };
2000 
2001     ret = vhost_user_write(dev, &msg, NULL, 0);
2002     if (ret < 0) {
2003         error_setg(errp, "Failed to send postcopy_advise to vhost");
2004         return ret;
2005     }
2006 
2007     ret = vhost_user_read(dev, &msg);
2008     if (ret < 0) {
2009         error_setg(errp, "Failed to get postcopy_advise reply from vhost");
2010         return ret;
2011     }
2012 
2013     if (msg.hdr.request != VHOST_USER_POSTCOPY_ADVISE) {
2014         error_setg(errp, "Unexpected msg type. Expected %d received %d",
2015                      VHOST_USER_POSTCOPY_ADVISE, msg.hdr.request);
2016         return -EPROTO;
2017     }
2018 
2019     if (msg.hdr.size) {
2020         error_setg(errp, "Received bad msg size.");
2021         return -EPROTO;
2022     }
2023     ufd = qemu_chr_fe_get_msgfd(chr);
2024     if (ufd < 0) {
2025         error_setg(errp, "%s: Failed to get ufd", __func__);
2026         return -EIO;
2027     }
2028     qemu_socket_set_nonblock(ufd);
2029 
2030     /* register ufd with userfault thread */
2031     u->postcopy_fd.fd = ufd;
2032     u->postcopy_fd.data = dev;
2033     u->postcopy_fd.handler = vhost_user_postcopy_fault_handler;
2034     u->postcopy_fd.waker = vhost_user_postcopy_waker;
2035     u->postcopy_fd.idstr = "vhost-user"; /* Need to find unique name */
2036     postcopy_register_shared_ufd(&u->postcopy_fd);
2037     return 0;
2038 #else
2039     error_setg(errp, "Postcopy not supported on non-Linux systems");
2040     return -ENOSYS;
2041 #endif
2042 }
2043 
2044 /*
2045  * Called at the switch to postcopy on reception of the 'listen' command.
2046  */
vhost_user_postcopy_listen(struct vhost_dev * dev,Error ** errp)2047 static int vhost_user_postcopy_listen(struct vhost_dev *dev, Error **errp)
2048 {
2049     struct vhost_user *u = dev->opaque;
2050     int ret;
2051     VhostUserMsg msg = {
2052         .hdr.request = VHOST_USER_POSTCOPY_LISTEN,
2053         .hdr.flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY_MASK,
2054     };
2055     u->postcopy_listen = true;
2056 
2057     trace_vhost_user_postcopy_listen();
2058 
2059     ret = vhost_user_write(dev, &msg, NULL, 0);
2060     if (ret < 0) {
2061         error_setg(errp, "Failed to send postcopy_listen to vhost");
2062         return ret;
2063     }
2064 
2065     ret = process_message_reply(dev, &msg);
2066     if (ret) {
2067         error_setg(errp, "Failed to receive reply to postcopy_listen");
2068         return ret;
2069     }
2070 
2071     return 0;
2072 }
2073 
2074 /*
2075  * Called at the end of postcopy
2076  */
vhost_user_postcopy_end(struct vhost_dev * dev,Error ** errp)2077 static int vhost_user_postcopy_end(struct vhost_dev *dev, Error **errp)
2078 {
2079     VhostUserMsg msg = {
2080         .hdr.request = VHOST_USER_POSTCOPY_END,
2081         .hdr.flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY_MASK,
2082     };
2083     int ret;
2084     struct vhost_user *u = dev->opaque;
2085 
2086     trace_vhost_user_postcopy_end_entry();
2087 
2088     ret = vhost_user_write(dev, &msg, NULL, 0);
2089     if (ret < 0) {
2090         error_setg(errp, "Failed to send postcopy_end to vhost");
2091         return ret;
2092     }
2093 
2094     ret = process_message_reply(dev, &msg);
2095     if (ret) {
2096         error_setg(errp, "Failed to receive reply to postcopy_end");
2097         return ret;
2098     }
2099     postcopy_unregister_shared_ufd(&u->postcopy_fd);
2100     close(u->postcopy_fd.fd);
2101     u->postcopy_fd.handler = NULL;
2102 
2103     trace_vhost_user_postcopy_end_exit();
2104 
2105     return 0;
2106 }
2107 
vhost_user_postcopy_notifier(NotifierWithReturn * notifier,void * opaque,Error ** errp)2108 static int vhost_user_postcopy_notifier(NotifierWithReturn *notifier,
2109                                         void *opaque, Error **errp)
2110 {
2111     struct PostcopyNotifyData *pnd = opaque;
2112     struct vhost_user *u = container_of(notifier, struct vhost_user,
2113                                          postcopy_notifier);
2114     struct vhost_dev *dev = u->dev;
2115 
2116     switch (pnd->reason) {
2117     case POSTCOPY_NOTIFY_PROBE:
2118         if (!virtio_has_feature(dev->protocol_features,
2119                                 VHOST_USER_PROTOCOL_F_PAGEFAULT)) {
2120             /* TODO: Get the device name into this error somehow */
2121             error_setg(errp,
2122                        "vhost-user backend not capable of postcopy");
2123             return -ENOENT;
2124         }
2125         break;
2126 
2127     case POSTCOPY_NOTIFY_INBOUND_ADVISE:
2128         return vhost_user_postcopy_advise(dev, errp);
2129 
2130     case POSTCOPY_NOTIFY_INBOUND_LISTEN:
2131         return vhost_user_postcopy_listen(dev, errp);
2132 
2133     case POSTCOPY_NOTIFY_INBOUND_END:
2134         return vhost_user_postcopy_end(dev, errp);
2135 
2136     default:
2137         /* We ignore notifications we don't know */
2138         break;
2139     }
2140 
2141     return 0;
2142 }
2143 
vhost_user_backend_init(struct vhost_dev * dev,void * opaque,Error ** errp)2144 static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque,
2145                                    Error **errp)
2146 {
2147     uint64_t features, ram_slots;
2148     struct vhost_user *u;
2149     VhostUserState *vus = (VhostUserState *) opaque;
2150     int err;
2151 
2152     assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
2153 
2154     u = g_new0(struct vhost_user, 1);
2155     u->user = vus;
2156     u->dev = dev;
2157     dev->opaque = u;
2158 
2159     err = vhost_user_get_features(dev, &features);
2160     if (err < 0) {
2161         error_setg_errno(errp, -err, "vhost_backend_init failed");
2162         return err;
2163     }
2164 
2165     if (virtio_has_feature(features, VHOST_USER_F_PROTOCOL_FEATURES)) {
2166         bool supports_f_config = vus->supports_config ||
2167             (dev->config_ops && dev->config_ops->vhost_dev_config_notifier);
2168         uint64_t protocol_features;
2169 
2170         dev->backend_features |= 1ULL << VHOST_USER_F_PROTOCOL_FEATURES;
2171 
2172         err = vhost_user_get_u64(dev, VHOST_USER_GET_PROTOCOL_FEATURES,
2173                                  &protocol_features);
2174         if (err < 0) {
2175             error_setg_errno(errp, EPROTO, "vhost_backend_init failed");
2176             return -EPROTO;
2177         }
2178 
2179         /*
2180          * We will use all the protocol features we support - although
2181          * we suppress F_CONFIG if we know QEMUs internal code can not support
2182          * it.
2183          */
2184         protocol_features &= VHOST_USER_PROTOCOL_FEATURE_MASK;
2185 
2186         if (supports_f_config) {
2187             if (!virtio_has_feature(protocol_features,
2188                                     VHOST_USER_PROTOCOL_F_CONFIG)) {
2189                 error_setg(errp, "vhost-user device expecting "
2190                            "VHOST_USER_PROTOCOL_F_CONFIG but the vhost-user backend does "
2191                            "not support it.");
2192                 return -EPROTO;
2193             }
2194         } else {
2195             if (virtio_has_feature(protocol_features,
2196                                    VHOST_USER_PROTOCOL_F_CONFIG)) {
2197                 warn_report("vhost-user backend supports "
2198                             "VHOST_USER_PROTOCOL_F_CONFIG but QEMU does not.");
2199                 protocol_features &= ~(1ULL << VHOST_USER_PROTOCOL_F_CONFIG);
2200             }
2201         }
2202 
2203         /* final set of protocol features */
2204         dev->protocol_features = protocol_features;
2205         err = vhost_user_set_protocol_features(dev, dev->protocol_features);
2206         if (err < 0) {
2207             error_setg_errno(errp, EPROTO, "vhost_backend_init failed");
2208             return -EPROTO;
2209         }
2210 
2211         /* query the max queues we support if backend supports Multiple Queue */
2212         if (dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_MQ)) {
2213             err = vhost_user_get_u64(dev, VHOST_USER_GET_QUEUE_NUM,
2214                                      &dev->max_queues);
2215             if (err < 0) {
2216                 error_setg_errno(errp, EPROTO, "vhost_backend_init failed");
2217                 return -EPROTO;
2218             }
2219         } else {
2220             dev->max_queues = 1;
2221         }
2222 
2223         if (dev->num_queues && dev->max_queues < dev->num_queues) {
2224             error_setg(errp, "The maximum number of queues supported by the "
2225                        "backend is %" PRIu64, dev->max_queues);
2226             return -EINVAL;
2227         }
2228 
2229         if (virtio_has_feature(features, VIRTIO_F_IOMMU_PLATFORM) &&
2230                 !(virtio_has_feature(dev->protocol_features,
2231                     VHOST_USER_PROTOCOL_F_BACKEND_REQ) &&
2232                  virtio_has_feature(dev->protocol_features,
2233                     VHOST_USER_PROTOCOL_F_REPLY_ACK))) {
2234             error_setg(errp, "IOMMU support requires reply-ack and "
2235                        "backend-req protocol features.");
2236             return -EINVAL;
2237         }
2238 
2239         /* get max memory regions if backend supports configurable RAM slots */
2240         if (!virtio_has_feature(dev->protocol_features,
2241                                 VHOST_USER_PROTOCOL_F_CONFIGURE_MEM_SLOTS)) {
2242             u->user->memory_slots = VHOST_MEMORY_BASELINE_NREGIONS;
2243         } else {
2244             err = vhost_user_get_max_memslots(dev, &ram_slots);
2245             if (err < 0) {
2246                 error_setg_errno(errp, EPROTO, "vhost_backend_init failed");
2247                 return -EPROTO;
2248             }
2249 
2250             if (ram_slots < u->user->memory_slots) {
2251                 error_setg(errp, "The backend specified a max ram slots limit "
2252                            "of %" PRIu64", when the prior validated limit was "
2253                            "%d. This limit should never decrease.", ram_slots,
2254                            u->user->memory_slots);
2255                 return -EINVAL;
2256             }
2257 
2258             u->user->memory_slots = MIN(ram_slots, VHOST_USER_MAX_RAM_SLOTS);
2259         }
2260     }
2261 
2262     if (dev->migration_blocker == NULL &&
2263         !virtio_has_feature(dev->protocol_features,
2264                             VHOST_USER_PROTOCOL_F_LOG_SHMFD)) {
2265         error_setg(&dev->migration_blocker,
2266                    "Migration disabled: vhost-user backend lacks "
2267                    "VHOST_USER_PROTOCOL_F_LOG_SHMFD feature.");
2268     }
2269 
2270     if (dev->vq_index == 0) {
2271         err = vhost_setup_backend_channel(dev);
2272         if (err < 0) {
2273             error_setg_errno(errp, EPROTO, "vhost_backend_init failed");
2274             return -EPROTO;
2275         }
2276     }
2277 
2278     u->postcopy_notifier.notify = vhost_user_postcopy_notifier;
2279     postcopy_add_notifier(&u->postcopy_notifier);
2280 
2281     return 0;
2282 }
2283 
vhost_user_backend_cleanup(struct vhost_dev * dev)2284 static int vhost_user_backend_cleanup(struct vhost_dev *dev)
2285 {
2286     struct vhost_user *u;
2287 
2288     assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
2289 
2290     u = dev->opaque;
2291     if (u->postcopy_notifier.notify) {
2292         postcopy_remove_notifier(&u->postcopy_notifier);
2293         u->postcopy_notifier.notify = NULL;
2294     }
2295     u->postcopy_listen = false;
2296     if (u->postcopy_fd.handler) {
2297         postcopy_unregister_shared_ufd(&u->postcopy_fd);
2298         close(u->postcopy_fd.fd);
2299         u->postcopy_fd.handler = NULL;
2300     }
2301     if (u->backend_ioc) {
2302         close_backend_channel(u);
2303     }
2304     g_free(u->region_rb);
2305     u->region_rb = NULL;
2306     g_free(u->region_rb_offset);
2307     u->region_rb_offset = NULL;
2308     u->region_rb_len = 0;
2309     g_free(u);
2310     dev->opaque = 0;
2311 
2312     return 0;
2313 }
2314 
vhost_user_get_vq_index(struct vhost_dev * dev,int idx)2315 static int vhost_user_get_vq_index(struct vhost_dev *dev, int idx)
2316 {
2317     assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
2318 
2319     return idx;
2320 }
2321 
vhost_user_memslots_limit(struct vhost_dev * dev)2322 static int vhost_user_memslots_limit(struct vhost_dev *dev)
2323 {
2324     struct vhost_user *u = dev->opaque;
2325 
2326     return u->user->memory_slots;
2327 }
2328 
vhost_user_requires_shm_log(struct vhost_dev * dev)2329 static bool vhost_user_requires_shm_log(struct vhost_dev *dev)
2330 {
2331     assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
2332 
2333     return virtio_has_feature(dev->protocol_features,
2334                               VHOST_USER_PROTOCOL_F_LOG_SHMFD);
2335 }
2336 
vhost_user_migration_done(struct vhost_dev * dev,char * mac_addr)2337 static int vhost_user_migration_done(struct vhost_dev *dev, char* mac_addr)
2338 {
2339     VhostUserMsg msg = { };
2340 
2341     assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
2342 
2343     /* If guest supports GUEST_ANNOUNCE do nothing */
2344     if (virtio_has_feature(dev->acked_features, VIRTIO_NET_F_GUEST_ANNOUNCE)) {
2345         return 0;
2346     }
2347 
2348     /* if backend supports VHOST_USER_PROTOCOL_F_RARP ask it to send the RARP */
2349     if (virtio_has_feature(dev->protocol_features,
2350                            VHOST_USER_PROTOCOL_F_RARP)) {
2351         msg.hdr.request = VHOST_USER_SEND_RARP;
2352         msg.hdr.flags = VHOST_USER_VERSION;
2353         memcpy((char *)&msg.payload.u64, mac_addr, 6);
2354         msg.hdr.size = sizeof(msg.payload.u64);
2355 
2356         return vhost_user_write(dev, &msg, NULL, 0);
2357     }
2358     return -ENOTSUP;
2359 }
2360 
vhost_user_net_set_mtu(struct vhost_dev * dev,uint16_t mtu)2361 static int vhost_user_net_set_mtu(struct vhost_dev *dev, uint16_t mtu)
2362 {
2363     VhostUserMsg msg;
2364     bool reply_supported = virtio_has_feature(dev->protocol_features,
2365                                               VHOST_USER_PROTOCOL_F_REPLY_ACK);
2366     int ret;
2367 
2368     if (!(dev->protocol_features & (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU))) {
2369         return 0;
2370     }
2371 
2372     msg.hdr.request = VHOST_USER_NET_SET_MTU;
2373     msg.payload.u64 = mtu;
2374     msg.hdr.size = sizeof(msg.payload.u64);
2375     msg.hdr.flags = VHOST_USER_VERSION;
2376     if (reply_supported) {
2377         msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
2378     }
2379 
2380     ret = vhost_user_write(dev, &msg, NULL, 0);
2381     if (ret < 0) {
2382         return ret;
2383     }
2384 
2385     /* If reply_ack supported, backend has to ack specified MTU is valid */
2386     if (reply_supported) {
2387         return process_message_reply(dev, &msg);
2388     }
2389 
2390     return 0;
2391 }
2392 
vhost_user_send_device_iotlb_msg(struct vhost_dev * dev,struct vhost_iotlb_msg * imsg)2393 static int vhost_user_send_device_iotlb_msg(struct vhost_dev *dev,
2394                                             struct vhost_iotlb_msg *imsg)
2395 {
2396     int ret;
2397     VhostUserMsg msg = {
2398         .hdr.request = VHOST_USER_IOTLB_MSG,
2399         .hdr.size = sizeof(msg.payload.iotlb),
2400         .hdr.flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY_MASK,
2401         .payload.iotlb = *imsg,
2402     };
2403 
2404     ret = vhost_user_write(dev, &msg, NULL, 0);
2405     if (ret < 0) {
2406         return ret;
2407     }
2408 
2409     return process_message_reply(dev, &msg);
2410 }
2411 
2412 
vhost_user_set_iotlb_callback(struct vhost_dev * dev,int enabled)2413 static void vhost_user_set_iotlb_callback(struct vhost_dev *dev, int enabled)
2414 {
2415     /* No-op as the receive channel is not dedicated to IOTLB messages. */
2416 }
2417 
vhost_user_get_config(struct vhost_dev * dev,uint8_t * config,uint32_t config_len,Error ** errp)2418 static int vhost_user_get_config(struct vhost_dev *dev, uint8_t *config,
2419                                  uint32_t config_len, Error **errp)
2420 {
2421     int ret;
2422     VhostUserMsg msg = {
2423         .hdr.request = VHOST_USER_GET_CONFIG,
2424         .hdr.flags = VHOST_USER_VERSION,
2425         .hdr.size = VHOST_USER_CONFIG_HDR_SIZE + config_len,
2426     };
2427 
2428     if (!virtio_has_feature(dev->protocol_features,
2429                 VHOST_USER_PROTOCOL_F_CONFIG)) {
2430         error_setg(errp, "VHOST_USER_PROTOCOL_F_CONFIG not supported");
2431         return -EINVAL;
2432     }
2433 
2434     assert(config_len <= VHOST_USER_MAX_CONFIG_SIZE);
2435 
2436     msg.payload.config.offset = 0;
2437     msg.payload.config.size = config_len;
2438     ret = vhost_user_write(dev, &msg, NULL, 0);
2439     if (ret < 0) {
2440         error_setg_errno(errp, -ret, "vhost_get_config failed");
2441         return ret;
2442     }
2443 
2444     ret = vhost_user_read(dev, &msg);
2445     if (ret < 0) {
2446         error_setg_errno(errp, -ret, "vhost_get_config failed");
2447         return ret;
2448     }
2449 
2450     if (msg.hdr.request != VHOST_USER_GET_CONFIG) {
2451         error_setg(errp,
2452                    "Received unexpected msg type. Expected %d received %d",
2453                    VHOST_USER_GET_CONFIG, msg.hdr.request);
2454         return -EPROTO;
2455     }
2456 
2457     if (msg.hdr.size != VHOST_USER_CONFIG_HDR_SIZE + config_len) {
2458         error_setg(errp, "Received bad msg size.");
2459         return -EPROTO;
2460     }
2461 
2462     memcpy(config, msg.payload.config.region, config_len);
2463 
2464     return 0;
2465 }
2466 
vhost_user_set_config(struct vhost_dev * dev,const uint8_t * data,uint32_t offset,uint32_t size,uint32_t flags)2467 static int vhost_user_set_config(struct vhost_dev *dev, const uint8_t *data,
2468                                  uint32_t offset, uint32_t size, uint32_t flags)
2469 {
2470     int ret;
2471     uint8_t *p;
2472     bool reply_supported = virtio_has_feature(dev->protocol_features,
2473                                               VHOST_USER_PROTOCOL_F_REPLY_ACK);
2474 
2475     VhostUserMsg msg = {
2476         .hdr.request = VHOST_USER_SET_CONFIG,
2477         .hdr.flags = VHOST_USER_VERSION,
2478         .hdr.size = VHOST_USER_CONFIG_HDR_SIZE + size,
2479     };
2480 
2481     if (!virtio_has_feature(dev->protocol_features,
2482                 VHOST_USER_PROTOCOL_F_CONFIG)) {
2483         return -ENOTSUP;
2484     }
2485 
2486     if (reply_supported) {
2487         msg.hdr.flags |= VHOST_USER_NEED_REPLY_MASK;
2488     }
2489 
2490     if (size > VHOST_USER_MAX_CONFIG_SIZE) {
2491         return -EINVAL;
2492     }
2493 
2494     msg.payload.config.offset = offset,
2495     msg.payload.config.size = size,
2496     msg.payload.config.flags = flags,
2497     p = msg.payload.config.region;
2498     memcpy(p, data, size);
2499 
2500     ret = vhost_user_write(dev, &msg, NULL, 0);
2501     if (ret < 0) {
2502         return ret;
2503     }
2504 
2505     if (reply_supported) {
2506         return process_message_reply(dev, &msg);
2507     }
2508 
2509     return 0;
2510 }
2511 
vhost_user_crypto_create_session(struct vhost_dev * dev,void * session_info,uint64_t * session_id)2512 static int vhost_user_crypto_create_session(struct vhost_dev *dev,
2513                                             void *session_info,
2514                                             uint64_t *session_id)
2515 {
2516     int ret;
2517     bool crypto_session = virtio_has_feature(dev->protocol_features,
2518                                        VHOST_USER_PROTOCOL_F_CRYPTO_SESSION);
2519     CryptoDevBackendSessionInfo *backend_info = session_info;
2520     VhostUserMsg msg = {
2521         .hdr.request = VHOST_USER_CREATE_CRYPTO_SESSION,
2522         .hdr.flags = VHOST_USER_VERSION,
2523         .hdr.size = sizeof(msg.payload.session),
2524     };
2525 
2526     assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
2527 
2528     if (!crypto_session) {
2529         error_report("vhost-user trying to send unhandled ioctl");
2530         return -ENOTSUP;
2531     }
2532 
2533     if (backend_info->op_code == VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION) {
2534         CryptoDevBackendAsymSessionInfo *sess = &backend_info->u.asym_sess_info;
2535         size_t keylen;
2536 
2537         memcpy(&msg.payload.session.u.asym.session_setup_data, sess,
2538                sizeof(CryptoDevBackendAsymSessionInfo));
2539         if (sess->keylen) {
2540             keylen = sizeof(msg.payload.session.u.asym.key);
2541             if (sess->keylen > keylen) {
2542                 error_report("Unsupported asymmetric key size");
2543                 return -ENOTSUP;
2544             }
2545 
2546             memcpy(&msg.payload.session.u.asym.key, sess->key,
2547                    sess->keylen);
2548         }
2549     } else {
2550         CryptoDevBackendSymSessionInfo *sess = &backend_info->u.sym_sess_info;
2551         size_t keylen;
2552 
2553         memcpy(&msg.payload.session.u.sym.session_setup_data, sess,
2554                sizeof(CryptoDevBackendSymSessionInfo));
2555         if (sess->key_len) {
2556             keylen = sizeof(msg.payload.session.u.sym.key);
2557             if (sess->key_len > keylen) {
2558                 error_report("Unsupported cipher key size");
2559                 return -ENOTSUP;
2560             }
2561 
2562             memcpy(&msg.payload.session.u.sym.key, sess->cipher_key,
2563                    sess->key_len);
2564         }
2565 
2566         if (sess->auth_key_len > 0) {
2567             keylen = sizeof(msg.payload.session.u.sym.auth_key);
2568             if (sess->auth_key_len > keylen) {
2569                 error_report("Unsupported auth key size");
2570                 return -ENOTSUP;
2571             }
2572 
2573             memcpy(&msg.payload.session.u.sym.auth_key, sess->auth_key,
2574                    sess->auth_key_len);
2575         }
2576     }
2577 
2578     msg.payload.session.op_code = backend_info->op_code;
2579     msg.payload.session.session_id = backend_info->session_id;
2580     ret = vhost_user_write(dev, &msg, NULL, 0);
2581     if (ret < 0) {
2582         error_report("vhost_user_write() return %d, create session failed",
2583                      ret);
2584         return ret;
2585     }
2586 
2587     ret = vhost_user_read(dev, &msg);
2588     if (ret < 0) {
2589         error_report("vhost_user_read() return %d, create session failed",
2590                      ret);
2591         return ret;
2592     }
2593 
2594     if (msg.hdr.request != VHOST_USER_CREATE_CRYPTO_SESSION) {
2595         error_report("Received unexpected msg type. Expected %d received %d",
2596                      VHOST_USER_CREATE_CRYPTO_SESSION, msg.hdr.request);
2597         return -EPROTO;
2598     }
2599 
2600     if (msg.hdr.size != sizeof(msg.payload.session)) {
2601         error_report("Received bad msg size.");
2602         return -EPROTO;
2603     }
2604 
2605     if (msg.payload.session.session_id < 0) {
2606         error_report("Bad session id: %" PRId64 "",
2607                               msg.payload.session.session_id);
2608         return -EINVAL;
2609     }
2610     *session_id = msg.payload.session.session_id;
2611 
2612     return 0;
2613 }
2614 
2615 static int
vhost_user_crypto_close_session(struct vhost_dev * dev,uint64_t session_id)2616 vhost_user_crypto_close_session(struct vhost_dev *dev, uint64_t session_id)
2617 {
2618     int ret;
2619     bool crypto_session = virtio_has_feature(dev->protocol_features,
2620                                        VHOST_USER_PROTOCOL_F_CRYPTO_SESSION);
2621     VhostUserMsg msg = {
2622         .hdr.request = VHOST_USER_CLOSE_CRYPTO_SESSION,
2623         .hdr.flags = VHOST_USER_VERSION,
2624         .hdr.size = sizeof(msg.payload.u64),
2625     };
2626     msg.payload.u64 = session_id;
2627 
2628     if (!crypto_session) {
2629         error_report("vhost-user trying to send unhandled ioctl");
2630         return -ENOTSUP;
2631     }
2632 
2633     ret = vhost_user_write(dev, &msg, NULL, 0);
2634     if (ret < 0) {
2635         error_report("vhost_user_write() return %d, close session failed",
2636                      ret);
2637         return ret;
2638     }
2639 
2640     return 0;
2641 }
2642 
vhost_user_no_private_memslots(struct vhost_dev * dev)2643 static bool vhost_user_no_private_memslots(struct vhost_dev *dev)
2644 {
2645     return true;
2646 }
2647 
vhost_user_get_inflight_fd(struct vhost_dev * dev,uint16_t queue_size,struct vhost_inflight * inflight)2648 static int vhost_user_get_inflight_fd(struct vhost_dev *dev,
2649                                       uint16_t queue_size,
2650                                       struct vhost_inflight *inflight)
2651 {
2652     void *addr;
2653     int fd;
2654     int ret;
2655     struct vhost_user *u = dev->opaque;
2656     CharBackend *chr = u->user->chr;
2657     VhostUserMsg msg = {
2658         .hdr.request = VHOST_USER_GET_INFLIGHT_FD,
2659         .hdr.flags = VHOST_USER_VERSION,
2660         .payload.inflight.num_queues = dev->nvqs,
2661         .payload.inflight.queue_size = queue_size,
2662         .hdr.size = sizeof(msg.payload.inflight),
2663     };
2664 
2665     if (!virtio_has_feature(dev->protocol_features,
2666                             VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) {
2667         return 0;
2668     }
2669 
2670     ret = vhost_user_write(dev, &msg, NULL, 0);
2671     if (ret < 0) {
2672         return ret;
2673     }
2674 
2675     ret = vhost_user_read(dev, &msg);
2676     if (ret < 0) {
2677         return ret;
2678     }
2679 
2680     if (msg.hdr.request != VHOST_USER_GET_INFLIGHT_FD) {
2681         error_report("Received unexpected msg type. "
2682                      "Expected %d received %d",
2683                      VHOST_USER_GET_INFLIGHT_FD, msg.hdr.request);
2684         return -EPROTO;
2685     }
2686 
2687     if (msg.hdr.size != sizeof(msg.payload.inflight)) {
2688         error_report("Received bad msg size.");
2689         return -EPROTO;
2690     }
2691 
2692     if (!msg.payload.inflight.mmap_size) {
2693         return 0;
2694     }
2695 
2696     fd = qemu_chr_fe_get_msgfd(chr);
2697     if (fd < 0) {
2698         error_report("Failed to get mem fd");
2699         return -EIO;
2700     }
2701 
2702     addr = mmap(0, msg.payload.inflight.mmap_size, PROT_READ | PROT_WRITE,
2703                 MAP_SHARED, fd, msg.payload.inflight.mmap_offset);
2704 
2705     if (addr == MAP_FAILED) {
2706         error_report("Failed to mmap mem fd");
2707         close(fd);
2708         return -EFAULT;
2709     }
2710 
2711     inflight->addr = addr;
2712     inflight->fd = fd;
2713     inflight->size = msg.payload.inflight.mmap_size;
2714     inflight->offset = msg.payload.inflight.mmap_offset;
2715     inflight->queue_size = queue_size;
2716 
2717     return 0;
2718 }
2719 
vhost_user_set_inflight_fd(struct vhost_dev * dev,struct vhost_inflight * inflight)2720 static int vhost_user_set_inflight_fd(struct vhost_dev *dev,
2721                                       struct vhost_inflight *inflight)
2722 {
2723     VhostUserMsg msg = {
2724         .hdr.request = VHOST_USER_SET_INFLIGHT_FD,
2725         .hdr.flags = VHOST_USER_VERSION,
2726         .payload.inflight.mmap_size = inflight->size,
2727         .payload.inflight.mmap_offset = inflight->offset,
2728         .payload.inflight.num_queues = dev->nvqs,
2729         .payload.inflight.queue_size = inflight->queue_size,
2730         .hdr.size = sizeof(msg.payload.inflight),
2731     };
2732 
2733     if (!virtio_has_feature(dev->protocol_features,
2734                             VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)) {
2735         return 0;
2736     }
2737 
2738     return vhost_user_write(dev, &msg, &inflight->fd, 1);
2739 }
2740 
vhost_user_state_destroy(gpointer data)2741 static void vhost_user_state_destroy(gpointer data)
2742 {
2743     VhostUserHostNotifier *n = (VhostUserHostNotifier *) data;
2744     vhost_user_host_notifier_remove(n, NULL, true);
2745 }
2746 
vhost_user_init(VhostUserState * user,CharBackend * chr,Error ** errp)2747 bool vhost_user_init(VhostUserState *user, CharBackend *chr, Error **errp)
2748 {
2749     if (user->chr) {
2750         error_setg(errp, "Cannot initialize vhost-user state");
2751         return false;
2752     }
2753     user->chr = chr;
2754     user->memory_slots = 0;
2755     user->notifiers = g_ptr_array_new_full(VIRTIO_QUEUE_MAX / 4,
2756                                            &vhost_user_state_destroy);
2757     return true;
2758 }
2759 
vhost_user_cleanup(VhostUserState * user)2760 void vhost_user_cleanup(VhostUserState *user)
2761 {
2762     if (!user->chr) {
2763         return;
2764     }
2765     user->notifiers = (GPtrArray *) g_ptr_array_free(user->notifiers, true);
2766     user->chr = NULL;
2767 }
2768 
2769 
2770 typedef struct {
2771     vu_async_close_fn cb;
2772     DeviceState *dev;
2773     CharBackend *cd;
2774     struct vhost_dev *vhost;
2775 } VhostAsyncCallback;
2776 
vhost_user_async_close_bh(void * opaque)2777 static void vhost_user_async_close_bh(void *opaque)
2778 {
2779     VhostAsyncCallback *data = opaque;
2780 
2781     data->cb(data->dev);
2782 
2783     g_free(data);
2784 }
2785 
2786 /*
2787  * We only schedule the work if the machine is running. If suspended
2788  * we want to keep all the in-flight data as is for migration
2789  * purposes.
2790  */
vhost_user_async_close(DeviceState * d,CharBackend * chardev,struct vhost_dev * vhost,vu_async_close_fn cb)2791 void vhost_user_async_close(DeviceState *d,
2792                             CharBackend *chardev, struct vhost_dev *vhost,
2793                             vu_async_close_fn cb)
2794 {
2795     if (!runstate_check(RUN_STATE_SHUTDOWN)) {
2796         /*
2797          * A close event may happen during a read/write, but vhost
2798          * code assumes the vhost_dev remains setup, so delay the
2799          * stop & clear.
2800          */
2801         AioContext *ctx = qemu_get_current_aio_context();
2802         VhostAsyncCallback *data = g_new0(VhostAsyncCallback, 1);
2803 
2804         /* Save data for the callback */
2805         data->cb = cb;
2806         data->dev = d;
2807         data->cd = chardev;
2808         data->vhost = vhost;
2809 
2810         /* Disable any further notifications on the chardev */
2811         qemu_chr_fe_set_handlers(chardev,
2812                                  NULL, NULL, NULL, NULL, NULL, NULL,
2813                                  false);
2814 
2815         aio_bh_schedule_oneshot(ctx, vhost_user_async_close_bh, data);
2816 
2817         /*
2818          * Move vhost device to the stopped state. The vhost-user device
2819          * will be clean up and disconnected in BH. This can be useful in
2820          * the vhost migration code. If disconnect was caught there is an
2821          * option for the general vhost code to get the dev state without
2822          * knowing its type (in this case vhost-user).
2823          *
2824          * Note if the vhost device is fully cleared by the time we
2825          * execute the bottom half we won't continue with the cleanup.
2826          */
2827         vhost->started = false;
2828     }
2829 }
2830 
vhost_user_dev_start(struct vhost_dev * dev,bool started)2831 static int vhost_user_dev_start(struct vhost_dev *dev, bool started)
2832 {
2833     if (!virtio_has_feature(dev->protocol_features,
2834                             VHOST_USER_PROTOCOL_F_STATUS)) {
2835         return 0;
2836     }
2837 
2838     /* Set device status only for last queue pair */
2839     if (dev->vq_index + dev->nvqs != dev->vq_index_end) {
2840         return 0;
2841     }
2842 
2843     if (started) {
2844         return vhost_user_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE |
2845                                           VIRTIO_CONFIG_S_DRIVER |
2846                                           VIRTIO_CONFIG_S_DRIVER_OK);
2847     } else {
2848         return 0;
2849     }
2850 }
2851 
vhost_user_reset_status(struct vhost_dev * dev)2852 static void vhost_user_reset_status(struct vhost_dev *dev)
2853 {
2854     /* Set device status only for last queue pair */
2855     if (dev->vq_index + dev->nvqs != dev->vq_index_end) {
2856         return;
2857     }
2858 
2859     if (virtio_has_feature(dev->protocol_features,
2860                            VHOST_USER_PROTOCOL_F_STATUS)) {
2861         vhost_user_set_status(dev, 0);
2862     }
2863 }
2864 
vhost_user_supports_device_state(struct vhost_dev * dev)2865 static bool vhost_user_supports_device_state(struct vhost_dev *dev)
2866 {
2867     return virtio_has_feature(dev->protocol_features,
2868                               VHOST_USER_PROTOCOL_F_DEVICE_STATE);
2869 }
2870 
vhost_user_set_device_state_fd(struct vhost_dev * dev,VhostDeviceStateDirection direction,VhostDeviceStatePhase phase,int fd,int * reply_fd,Error ** errp)2871 static int vhost_user_set_device_state_fd(struct vhost_dev *dev,
2872                                           VhostDeviceStateDirection direction,
2873                                           VhostDeviceStatePhase phase,
2874                                           int fd,
2875                                           int *reply_fd,
2876                                           Error **errp)
2877 {
2878     int ret;
2879     struct vhost_user *vu = dev->opaque;
2880     VhostUserMsg msg = {
2881         .hdr = {
2882             .request = VHOST_USER_SET_DEVICE_STATE_FD,
2883             .flags = VHOST_USER_VERSION,
2884             .size = sizeof(msg.payload.transfer_state),
2885         },
2886         .payload.transfer_state = {
2887             .direction = direction,
2888             .phase = phase,
2889         },
2890     };
2891 
2892     *reply_fd = -1;
2893 
2894     if (!vhost_user_supports_device_state(dev)) {
2895         close(fd);
2896         error_setg(errp, "Back-end does not support migration state transfer");
2897         return -ENOTSUP;
2898     }
2899 
2900     ret = vhost_user_write(dev, &msg, &fd, 1);
2901     close(fd);
2902     if (ret < 0) {
2903         error_setg_errno(errp, -ret,
2904                          "Failed to send SET_DEVICE_STATE_FD message");
2905         return ret;
2906     }
2907 
2908     ret = vhost_user_read(dev, &msg);
2909     if (ret < 0) {
2910         error_setg_errno(errp, -ret,
2911                          "Failed to receive SET_DEVICE_STATE_FD reply");
2912         return ret;
2913     }
2914 
2915     if (msg.hdr.request != VHOST_USER_SET_DEVICE_STATE_FD) {
2916         error_setg(errp,
2917                    "Received unexpected message type, expected %d, received %d",
2918                    VHOST_USER_SET_DEVICE_STATE_FD, msg.hdr.request);
2919         return -EPROTO;
2920     }
2921 
2922     if (msg.hdr.size != sizeof(msg.payload.u64)) {
2923         error_setg(errp,
2924                    "Received bad message size, expected %zu, received %" PRIu32,
2925                    sizeof(msg.payload.u64), msg.hdr.size);
2926         return -EPROTO;
2927     }
2928 
2929     if ((msg.payload.u64 & 0xff) != 0) {
2930         error_setg(errp, "Back-end did not accept migration state transfer");
2931         return -EIO;
2932     }
2933 
2934     if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK)) {
2935         *reply_fd = qemu_chr_fe_get_msgfd(vu->user->chr);
2936         if (*reply_fd < 0) {
2937             error_setg(errp,
2938                        "Failed to get back-end-provided transfer pipe FD");
2939             *reply_fd = -1;
2940             return -EIO;
2941         }
2942     }
2943 
2944     return 0;
2945 }
2946 
vhost_user_check_device_state(struct vhost_dev * dev,Error ** errp)2947 static int vhost_user_check_device_state(struct vhost_dev *dev, Error **errp)
2948 {
2949     int ret;
2950     VhostUserMsg msg = {
2951         .hdr = {
2952             .request = VHOST_USER_CHECK_DEVICE_STATE,
2953             .flags = VHOST_USER_VERSION,
2954             .size = 0,
2955         },
2956     };
2957 
2958     if (!vhost_user_supports_device_state(dev)) {
2959         error_setg(errp, "Back-end does not support migration state transfer");
2960         return -ENOTSUP;
2961     }
2962 
2963     ret = vhost_user_write(dev, &msg, NULL, 0);
2964     if (ret < 0) {
2965         error_setg_errno(errp, -ret,
2966                          "Failed to send CHECK_DEVICE_STATE message");
2967         return ret;
2968     }
2969 
2970     ret = vhost_user_read(dev, &msg);
2971     if (ret < 0) {
2972         error_setg_errno(errp, -ret,
2973                          "Failed to receive CHECK_DEVICE_STATE reply");
2974         return ret;
2975     }
2976 
2977     if (msg.hdr.request != VHOST_USER_CHECK_DEVICE_STATE) {
2978         error_setg(errp,
2979                    "Received unexpected message type, expected %d, received %d",
2980                    VHOST_USER_CHECK_DEVICE_STATE, msg.hdr.request);
2981         return -EPROTO;
2982     }
2983 
2984     if (msg.hdr.size != sizeof(msg.payload.u64)) {
2985         error_setg(errp,
2986                    "Received bad message size, expected %zu, received %" PRIu32,
2987                    sizeof(msg.payload.u64), msg.hdr.size);
2988         return -EPROTO;
2989     }
2990 
2991     if (msg.payload.u64 != 0) {
2992         error_setg(errp, "Back-end failed to process its internal state");
2993         return -EIO;
2994     }
2995 
2996     return 0;
2997 }
2998 
2999 const VhostOps user_ops = {
3000         .backend_type = VHOST_BACKEND_TYPE_USER,
3001         .vhost_backend_init = vhost_user_backend_init,
3002         .vhost_backend_cleanup = vhost_user_backend_cleanup,
3003         .vhost_backend_memslots_limit = vhost_user_memslots_limit,
3004         .vhost_backend_no_private_memslots = vhost_user_no_private_memslots,
3005         .vhost_set_log_base = vhost_user_set_log_base,
3006         .vhost_set_mem_table = vhost_user_set_mem_table,
3007         .vhost_set_vring_addr = vhost_user_set_vring_addr,
3008         .vhost_set_vring_endian = vhost_user_set_vring_endian,
3009         .vhost_set_vring_num = vhost_user_set_vring_num,
3010         .vhost_set_vring_base = vhost_user_set_vring_base,
3011         .vhost_get_vring_base = vhost_user_get_vring_base,
3012         .vhost_set_vring_kick = vhost_user_set_vring_kick,
3013         .vhost_set_vring_call = vhost_user_set_vring_call,
3014         .vhost_set_vring_err = vhost_user_set_vring_err,
3015         .vhost_set_features = vhost_user_set_features,
3016         .vhost_get_features = vhost_user_get_features,
3017         .vhost_set_owner = vhost_user_set_owner,
3018         .vhost_reset_device = vhost_user_reset_device,
3019         .vhost_get_vq_index = vhost_user_get_vq_index,
3020         .vhost_set_vring_enable = vhost_user_set_vring_enable,
3021         .vhost_requires_shm_log = vhost_user_requires_shm_log,
3022         .vhost_migration_done = vhost_user_migration_done,
3023         .vhost_net_set_mtu = vhost_user_net_set_mtu,
3024         .vhost_set_iotlb_callback = vhost_user_set_iotlb_callback,
3025         .vhost_send_device_iotlb_msg = vhost_user_send_device_iotlb_msg,
3026         .vhost_get_config = vhost_user_get_config,
3027         .vhost_set_config = vhost_user_set_config,
3028         .vhost_crypto_create_session = vhost_user_crypto_create_session,
3029         .vhost_crypto_close_session = vhost_user_crypto_close_session,
3030         .vhost_get_inflight_fd = vhost_user_get_inflight_fd,
3031         .vhost_set_inflight_fd = vhost_user_set_inflight_fd,
3032         .vhost_dev_start = vhost_user_dev_start,
3033         .vhost_reset_status = vhost_user_reset_status,
3034         .vhost_supports_device_state = vhost_user_supports_device_state,
3035         .vhost_set_device_state_fd = vhost_user_set_device_state_fd,
3036         .vhost_check_device_state = vhost_user_check_device_state,
3037 };
3038