9aa1ce118113b9a23922fb4e45a6f8b9aac4e91f
[dpdk.git] / lib / librte_vhost / vhost_user.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 /* Security model
6  * --------------
7  * The vhost-user protocol connection is an external interface, so it must be
8  * robust against invalid inputs.
9  *
10  * This is important because the vhost-user master is only one step removed
11  * from the guest.  Malicious guests that have escaped will then launch further
12  * attacks from the vhost-user master.
13  *
14  * Even in deployments where guests are trusted, a bug in the vhost-user master
15  * can still cause invalid messages to be sent.  Such messages must not
16  * compromise the stability of the DPDK application by causing crashes, memory
17  * corruption, or other problematic behavior.
18  *
19  * Do not assume received VhostUserMsg fields contain sensible values!
20  */
21
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/mman.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <assert.h>
31 #ifdef RTE_LIBRTE_VHOST_NUMA
32 #include <numaif.h>
33 #endif
34
35 #include <rte_common.h>
36 #include <rte_malloc.h>
37 #include <rte_log.h>
38
39 #include "iotlb.h"
40 #include "vhost.h"
41 #include "vhost_user.h"
42
43 #define VIRTIO_MIN_MTU 68
44 #define VIRTIO_MAX_MTU 65535
45
46 static const char *vhost_message_str[VHOST_USER_MAX] = {
47         [VHOST_USER_NONE] = "VHOST_USER_NONE",
48         [VHOST_USER_GET_FEATURES] = "VHOST_USER_GET_FEATURES",
49         [VHOST_USER_SET_FEATURES] = "VHOST_USER_SET_FEATURES",
50         [VHOST_USER_SET_OWNER] = "VHOST_USER_SET_OWNER",
51         [VHOST_USER_RESET_OWNER] = "VHOST_USER_RESET_OWNER",
52         [VHOST_USER_SET_MEM_TABLE] = "VHOST_USER_SET_MEM_TABLE",
53         [VHOST_USER_SET_LOG_BASE] = "VHOST_USER_SET_LOG_BASE",
54         [VHOST_USER_SET_LOG_FD] = "VHOST_USER_SET_LOG_FD",
55         [VHOST_USER_SET_VRING_NUM] = "VHOST_USER_SET_VRING_NUM",
56         [VHOST_USER_SET_VRING_ADDR] = "VHOST_USER_SET_VRING_ADDR",
57         [VHOST_USER_SET_VRING_BASE] = "VHOST_USER_SET_VRING_BASE",
58         [VHOST_USER_GET_VRING_BASE] = "VHOST_USER_GET_VRING_BASE",
59         [VHOST_USER_SET_VRING_KICK] = "VHOST_USER_SET_VRING_KICK",
60         [VHOST_USER_SET_VRING_CALL] = "VHOST_USER_SET_VRING_CALL",
61         [VHOST_USER_SET_VRING_ERR]  = "VHOST_USER_SET_VRING_ERR",
62         [VHOST_USER_GET_PROTOCOL_FEATURES]  = "VHOST_USER_GET_PROTOCOL_FEATURES",
63         [VHOST_USER_SET_PROTOCOL_FEATURES]  = "VHOST_USER_SET_PROTOCOL_FEATURES",
64         [VHOST_USER_GET_QUEUE_NUM]  = "VHOST_USER_GET_QUEUE_NUM",
65         [VHOST_USER_SET_VRING_ENABLE]  = "VHOST_USER_SET_VRING_ENABLE",
66         [VHOST_USER_SEND_RARP]  = "VHOST_USER_SEND_RARP",
67         [VHOST_USER_NET_SET_MTU]  = "VHOST_USER_NET_SET_MTU",
68         [VHOST_USER_SET_SLAVE_REQ_FD]  = "VHOST_USER_SET_SLAVE_REQ_FD",
69         [VHOST_USER_IOTLB_MSG]  = "VHOST_USER_IOTLB_MSG",
70         [VHOST_USER_CRYPTO_CREATE_SESS] = "VHOST_USER_CRYPTO_CREATE_SESS",
71         [VHOST_USER_CRYPTO_CLOSE_SESS] = "VHOST_USER_CRYPTO_CLOSE_SESS",
72 };
73
74 static uint64_t
75 get_blk_size(int fd)
76 {
77         struct stat stat;
78         int ret;
79
80         ret = fstat(fd, &stat);
81         return ret == -1 ? (uint64_t)-1 : (uint64_t)stat.st_blksize;
82 }
83
84 static void
85 free_mem_region(struct virtio_net *dev)
86 {
87         uint32_t i;
88         struct rte_vhost_mem_region *reg;
89
90         if (!dev || !dev->mem)
91                 return;
92
93         for (i = 0; i < dev->mem->nregions; i++) {
94                 reg = &dev->mem->regions[i];
95                 if (reg->host_user_addr) {
96                         munmap(reg->mmap_addr, reg->mmap_size);
97                         close(reg->fd);
98                 }
99         }
100 }
101
102 void
103 vhost_backend_cleanup(struct virtio_net *dev)
104 {
105         if (dev->mem) {
106                 free_mem_region(dev);
107                 rte_free(dev->mem);
108                 dev->mem = NULL;
109         }
110
111         free(dev->guest_pages);
112         dev->guest_pages = NULL;
113
114         if (dev->log_addr) {
115                 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
116                 dev->log_addr = 0;
117         }
118
119         if (dev->slave_req_fd >= 0) {
120                 close(dev->slave_req_fd);
121                 dev->slave_req_fd = -1;
122         }
123 }
124
125 /*
126  * This function just returns success at the moment unless
127  * the device hasn't been initialised.
128  */
129 static int
130 vhost_user_set_owner(void)
131 {
132         return 0;
133 }
134
135 static int
136 vhost_user_reset_owner(struct virtio_net *dev)
137 {
138         vhost_destroy_device_notify(dev);
139
140         cleanup_device(dev, 0);
141         reset_device(dev);
142         return 0;
143 }
144
145 /*
146  * The features that we support are requested.
147  */
148 static uint64_t
149 vhost_user_get_features(struct virtio_net *dev)
150 {
151         uint64_t features = 0;
152
153         rte_vhost_driver_get_features(dev->ifname, &features);
154         return features;
155 }
156
157 /*
158  * The queue number that we support are requested.
159  */
160 static uint32_t
161 vhost_user_get_queue_num(struct virtio_net *dev)
162 {
163         uint32_t queue_num = 0;
164
165         rte_vhost_driver_get_queue_num(dev->ifname, &queue_num);
166         return queue_num;
167 }
168
169 /*
170  * We receive the negotiated features supported by us and the virtio device.
171  */
172 static int
173 vhost_user_set_features(struct virtio_net *dev, uint64_t features)
174 {
175         uint64_t vhost_features = 0;
176         struct rte_vdpa_device *vdpa_dev;
177         int did = -1;
178
179         rte_vhost_driver_get_features(dev->ifname, &vhost_features);
180         if (features & ~vhost_features) {
181                 RTE_LOG(ERR, VHOST_CONFIG,
182                         "(%d) received invalid negotiated features.\n",
183                         dev->vid);
184                 return -1;
185         }
186
187         if (dev->flags & VIRTIO_DEV_RUNNING) {
188                 if (dev->features == features)
189                         return 0;
190
191                 /*
192                  * Error out if master tries to change features while device is
193                  * in running state. The exception being VHOST_F_LOG_ALL, which
194                  * is enabled when the live-migration starts.
195                  */
196                 if ((dev->features ^ features) & ~(1ULL << VHOST_F_LOG_ALL)) {
197                         RTE_LOG(ERR, VHOST_CONFIG,
198                                 "(%d) features changed while device is running.\n",
199                                 dev->vid);
200                         return -1;
201                 }
202
203                 if (dev->notify_ops->features_changed)
204                         dev->notify_ops->features_changed(dev->vid, features);
205         }
206
207         dev->features = features;
208         if (dev->features &
209                 ((1 << VIRTIO_NET_F_MRG_RXBUF) | (1ULL << VIRTIO_F_VERSION_1))) {
210                 dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
211         } else {
212                 dev->vhost_hlen = sizeof(struct virtio_net_hdr);
213         }
214         VHOST_LOG_DEBUG(VHOST_CONFIG,
215                 "(%d) mergeable RX buffers %s, virtio 1 %s\n",
216                 dev->vid,
217                 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
218                 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
219
220         if ((dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET) &&
221             !(dev->features & (1ULL << VIRTIO_NET_F_MQ))) {
222                 /*
223                  * Remove all but first queue pair if MQ hasn't been
224                  * negotiated. This is safe because the device is not
225                  * running at this stage.
226                  */
227                 while (dev->nr_vring > 2) {
228                         struct vhost_virtqueue *vq;
229
230                         vq = dev->virtqueue[--dev->nr_vring];
231                         if (!vq)
232                                 continue;
233
234                         dev->virtqueue[dev->nr_vring] = NULL;
235                         cleanup_vq(vq, 1);
236                         free_vq(dev, vq);
237                 }
238         }
239
240         did = dev->vdpa_dev_id;
241         vdpa_dev = rte_vdpa_get_device(did);
242         if (vdpa_dev && vdpa_dev->ops->set_features)
243                 vdpa_dev->ops->set_features(dev->vid);
244
245         return 0;
246 }
247
248 /*
249  * The virtio device sends us the size of the descriptor ring.
250  */
251 static int
252 vhost_user_set_vring_num(struct virtio_net *dev,
253                          VhostUserMsg *msg)
254 {
255         struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
256
257         vq->size = msg->payload.state.num;
258
259         /* VIRTIO 1.0, 2.4 Virtqueues says:
260          *
261          *   Queue Size value is always a power of 2. The maximum Queue Size
262          *   value is 32768.
263          */
264         if ((vq->size & (vq->size - 1)) || vq->size > 32768) {
265                 RTE_LOG(ERR, VHOST_CONFIG,
266                         "invalid virtqueue size %u\n", vq->size);
267                 return -1;
268         }
269
270         if (dev->dequeue_zero_copy) {
271                 vq->nr_zmbuf = 0;
272                 vq->last_zmbuf_idx = 0;
273                 vq->zmbuf_size = vq->size;
274                 vq->zmbufs = rte_zmalloc(NULL, vq->zmbuf_size *
275                                          sizeof(struct zcopy_mbuf), 0);
276                 if (vq->zmbufs == NULL) {
277                         RTE_LOG(WARNING, VHOST_CONFIG,
278                                 "failed to allocate mem for zero copy; "
279                                 "zero copy is force disabled\n");
280                         dev->dequeue_zero_copy = 0;
281                 }
282                 TAILQ_INIT(&vq->zmbuf_list);
283         }
284
285         if (vq_is_packed(dev)) {
286                 vq->shadow_used_packed = rte_malloc(NULL,
287                                 vq->size *
288                                 sizeof(struct vring_used_elem_packed),
289                                 RTE_CACHE_LINE_SIZE);
290                 if (!vq->shadow_used_packed) {
291                         RTE_LOG(ERR, VHOST_CONFIG,
292                                         "failed to allocate memory for shadow used ring.\n");
293                         return -1;
294                 }
295
296         } else {
297                 vq->shadow_used_split = rte_malloc(NULL,
298                                 vq->size * sizeof(struct vring_used_elem),
299                                 RTE_CACHE_LINE_SIZE);
300                 if (!vq->shadow_used_split) {
301                         RTE_LOG(ERR, VHOST_CONFIG,
302                                         "failed to allocate memory for shadow used ring.\n");
303                         return -1;
304                 }
305         }
306
307         vq->batch_copy_elems = rte_malloc(NULL,
308                                 vq->size * sizeof(struct batch_copy_elem),
309                                 RTE_CACHE_LINE_SIZE);
310         if (!vq->batch_copy_elems) {
311                 RTE_LOG(ERR, VHOST_CONFIG,
312                         "failed to allocate memory for batching copy.\n");
313                 return -1;
314         }
315
316         return 0;
317 }
318
319 /*
320  * Reallocate virtio_dev and vhost_virtqueue data structure to make them on the
321  * same numa node as the memory of vring descriptor.
322  */
323 #ifdef RTE_LIBRTE_VHOST_NUMA
324 static struct virtio_net*
325 numa_realloc(struct virtio_net *dev, int index)
326 {
327         int oldnode, newnode;
328         struct virtio_net *old_dev;
329         struct vhost_virtqueue *old_vq, *vq;
330         struct zcopy_mbuf *new_zmbuf;
331         struct vring_used_elem *new_shadow_used_split;
332         struct vring_used_elem_packed *new_shadow_used_packed;
333         struct batch_copy_elem *new_batch_copy_elems;
334         int ret;
335
336         old_dev = dev;
337         vq = old_vq = dev->virtqueue[index];
338
339         ret = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
340                             MPOL_F_NODE | MPOL_F_ADDR);
341
342         /* check if we need to reallocate vq */
343         ret |= get_mempolicy(&oldnode, NULL, 0, old_vq,
344                              MPOL_F_NODE | MPOL_F_ADDR);
345         if (ret) {
346                 RTE_LOG(ERR, VHOST_CONFIG,
347                         "Unable to get vq numa information.\n");
348                 return dev;
349         }
350         if (oldnode != newnode) {
351                 RTE_LOG(INFO, VHOST_CONFIG,
352                         "reallocate vq from %d to %d node\n", oldnode, newnode);
353                 vq = rte_malloc_socket(NULL, sizeof(*vq), 0, newnode);
354                 if (!vq)
355                         return dev;
356
357                 memcpy(vq, old_vq, sizeof(*vq));
358                 TAILQ_INIT(&vq->zmbuf_list);
359
360                 if (dev->dequeue_zero_copy) {
361                         new_zmbuf = rte_malloc_socket(NULL, vq->zmbuf_size *
362                                         sizeof(struct zcopy_mbuf), 0, newnode);
363                         if (new_zmbuf) {
364                                 rte_free(vq->zmbufs);
365                                 vq->zmbufs = new_zmbuf;
366                         }
367                 }
368
369                 if (vq_is_packed(dev)) {
370                         new_shadow_used_packed = rte_malloc_socket(NULL,
371                                         vq->size *
372                                         sizeof(struct vring_used_elem_packed),
373                                         RTE_CACHE_LINE_SIZE,
374                                         newnode);
375                         if (new_shadow_used_packed) {
376                                 rte_free(vq->shadow_used_packed);
377                                 vq->shadow_used_packed = new_shadow_used_packed;
378                         }
379                 } else {
380                         new_shadow_used_split = rte_malloc_socket(NULL,
381                                         vq->size *
382                                         sizeof(struct vring_used_elem),
383                                         RTE_CACHE_LINE_SIZE,
384                                         newnode);
385                         if (new_shadow_used_split) {
386                                 rte_free(vq->shadow_used_split);
387                                 vq->shadow_used_split = new_shadow_used_split;
388                         }
389                 }
390
391                 new_batch_copy_elems = rte_malloc_socket(NULL,
392                         vq->size * sizeof(struct batch_copy_elem),
393                         RTE_CACHE_LINE_SIZE,
394                         newnode);
395                 if (new_batch_copy_elems) {
396                         rte_free(vq->batch_copy_elems);
397                         vq->batch_copy_elems = new_batch_copy_elems;
398                 }
399
400                 rte_free(old_vq);
401         }
402
403         /* check if we need to reallocate dev */
404         ret = get_mempolicy(&oldnode, NULL, 0, old_dev,
405                             MPOL_F_NODE | MPOL_F_ADDR);
406         if (ret) {
407                 RTE_LOG(ERR, VHOST_CONFIG,
408                         "Unable to get dev numa information.\n");
409                 goto out;
410         }
411         if (oldnode != newnode) {
412                 RTE_LOG(INFO, VHOST_CONFIG,
413                         "reallocate dev from %d to %d node\n",
414                         oldnode, newnode);
415                 dev = rte_malloc_socket(NULL, sizeof(*dev), 0, newnode);
416                 if (!dev) {
417                         dev = old_dev;
418                         goto out;
419                 }
420
421                 memcpy(dev, old_dev, sizeof(*dev));
422                 rte_free(old_dev);
423         }
424
425 out:
426         dev->virtqueue[index] = vq;
427         vhost_devices[dev->vid] = dev;
428
429         if (old_vq != vq)
430                 vhost_user_iotlb_init(dev, index);
431
432         return dev;
433 }
434 #else
435 static struct virtio_net*
436 numa_realloc(struct virtio_net *dev, int index __rte_unused)
437 {
438         return dev;
439 }
440 #endif
441
442 /* Converts QEMU virtual address to Vhost virtual address. */
443 static uint64_t
444 qva_to_vva(struct virtio_net *dev, uint64_t qva, uint64_t *len)
445 {
446         struct rte_vhost_mem_region *r;
447         uint32_t i;
448
449         /* Find the region where the address lives. */
450         for (i = 0; i < dev->mem->nregions; i++) {
451                 r = &dev->mem->regions[i];
452
453                 if (qva >= r->guest_user_addr &&
454                     qva <  r->guest_user_addr + r->size) {
455
456                         if (unlikely(*len > r->guest_user_addr + r->size - qva))
457                                 *len = r->guest_user_addr + r->size - qva;
458
459                         return qva - r->guest_user_addr +
460                                r->host_user_addr;
461                 }
462         }
463         *len = 0;
464
465         return 0;
466 }
467
468
469 /*
470  * Converts ring address to Vhost virtual address.
471  * If IOMMU is enabled, the ring address is a guest IO virtual address,
472  * else it is a QEMU virtual address.
473  */
474 static uint64_t
475 ring_addr_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
476                 uint64_t ra, uint64_t *size)
477 {
478         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
479                 uint64_t vva;
480
481                 vva = vhost_user_iotlb_cache_find(vq, ra,
482                                         size, VHOST_ACCESS_RW);
483                 if (!vva)
484                         vhost_user_iotlb_miss(dev, ra, VHOST_ACCESS_RW);
485
486                 return vva;
487         }
488
489         return qva_to_vva(dev, ra, size);
490 }
491
492 static struct virtio_net *
493 translate_ring_addresses(struct virtio_net *dev, int vq_index)
494 {
495         struct vhost_virtqueue *vq = dev->virtqueue[vq_index];
496         struct vhost_vring_addr *addr = &vq->ring_addrs;
497         uint64_t len;
498
499         if (vq_is_packed(dev)) {
500                 len = sizeof(struct vring_packed_desc) * vq->size;
501                 vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
502                         ring_addr_to_vva(dev, vq, addr->desc_user_addr, &len);
503                 vq->log_guest_addr = 0;
504                 if (vq->desc_packed == NULL ||
505                                 len != sizeof(struct vring_packed_desc) *
506                                 vq->size) {
507                         RTE_LOG(DEBUG, VHOST_CONFIG,
508                                 "(%d) failed to map desc_packed ring.\n",
509                                 dev->vid);
510                         return dev;
511                 }
512
513                 dev = numa_realloc(dev, vq_index);
514                 vq = dev->virtqueue[vq_index];
515                 addr = &vq->ring_addrs;
516
517                 len = sizeof(struct vring_packed_desc_event);
518                 vq->driver_event = (struct vring_packed_desc_event *)
519                                         (uintptr_t)ring_addr_to_vva(dev,
520                                         vq, addr->avail_user_addr, &len);
521                 if (vq->driver_event == NULL ||
522                                 len != sizeof(struct vring_packed_desc_event)) {
523                         RTE_LOG(DEBUG, VHOST_CONFIG,
524                                 "(%d) failed to find driver area address.\n",
525                                 dev->vid);
526                         return dev;
527                 }
528
529                 len = sizeof(struct vring_packed_desc_event);
530                 vq->device_event = (struct vring_packed_desc_event *)
531                                         (uintptr_t)ring_addr_to_vva(dev,
532                                         vq, addr->used_user_addr, &len);
533                 if (vq->device_event == NULL ||
534                                 len != sizeof(struct vring_packed_desc_event)) {
535                         RTE_LOG(DEBUG, VHOST_CONFIG,
536                                 "(%d) failed to find device area address.\n",
537                                 dev->vid);
538                         return dev;
539                 }
540
541                 return dev;
542         }
543
544         /* The addresses are converted from QEMU virtual to Vhost virtual. */
545         if (vq->desc && vq->avail && vq->used)
546                 return dev;
547
548         len = sizeof(struct vring_desc) * vq->size;
549         vq->desc = (struct vring_desc *)(uintptr_t)ring_addr_to_vva(dev,
550                         vq, addr->desc_user_addr, &len);
551         if (vq->desc == 0 || len != sizeof(struct vring_desc) * vq->size) {
552                 RTE_LOG(DEBUG, VHOST_CONFIG,
553                         "(%d) failed to map desc ring.\n",
554                         dev->vid);
555                 return dev;
556         }
557
558         dev = numa_realloc(dev, vq_index);
559         vq = dev->virtqueue[vq_index];
560         addr = &vq->ring_addrs;
561
562         len = sizeof(struct vring_avail) + sizeof(uint16_t) * vq->size;
563         vq->avail = (struct vring_avail *)(uintptr_t)ring_addr_to_vva(dev,
564                         vq, addr->avail_user_addr, &len);
565         if (vq->avail == 0 ||
566                         len != sizeof(struct vring_avail) +
567                         sizeof(uint16_t) * vq->size) {
568                 RTE_LOG(DEBUG, VHOST_CONFIG,
569                         "(%d) failed to map avail ring.\n",
570                         dev->vid);
571                 return dev;
572         }
573
574         len = sizeof(struct vring_used) +
575                 sizeof(struct vring_used_elem) * vq->size;
576         vq->used = (struct vring_used *)(uintptr_t)ring_addr_to_vva(dev,
577                         vq, addr->used_user_addr, &len);
578         if (vq->used == 0 || len != sizeof(struct vring_used) +
579                         sizeof(struct vring_used_elem) * vq->size) {
580                 RTE_LOG(DEBUG, VHOST_CONFIG,
581                         "(%d) failed to map used ring.\n",
582                         dev->vid);
583                 return dev;
584         }
585
586         if (vq->last_used_idx != vq->used->idx) {
587                 RTE_LOG(WARNING, VHOST_CONFIG,
588                         "last_used_idx (%u) and vq->used->idx (%u) mismatches; "
589                         "some packets maybe resent for Tx and dropped for Rx\n",
590                         vq->last_used_idx, vq->used->idx);
591                 vq->last_used_idx  = vq->used->idx;
592                 vq->last_avail_idx = vq->used->idx;
593         }
594
595         vq->log_guest_addr = addr->log_guest_addr;
596
597         VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
598                         dev->vid, vq->desc);
599         VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address avail: %p\n",
600                         dev->vid, vq->avail);
601         VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address used: %p\n",
602                         dev->vid, vq->used);
603         VHOST_LOG_DEBUG(VHOST_CONFIG, "(%d) log_guest_addr: %" PRIx64 "\n",
604                         dev->vid, vq->log_guest_addr);
605
606         return dev;
607 }
608
609 /*
610  * The virtio device sends us the desc, used and avail ring addresses.
611  * This function then converts these to our address space.
612  */
613 static int
614 vhost_user_set_vring_addr(struct virtio_net **pdev, VhostUserMsg *msg)
615 {
616         struct vhost_virtqueue *vq;
617         struct vhost_vring_addr *addr = &msg->payload.addr;
618         struct virtio_net *dev = *pdev;
619
620         if (dev->mem == NULL)
621                 return -1;
622
623         /* addr->index refers to the queue index. The txq 1, rxq is 0. */
624         vq = dev->virtqueue[msg->payload.addr.index];
625
626         /*
627          * Rings addresses should not be interpreted as long as the ring is not
628          * started and enabled
629          */
630         memcpy(&vq->ring_addrs, addr, sizeof(*addr));
631
632         vring_invalidate(dev, vq);
633
634         if (vq->enabled && (dev->features &
635                                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES))) {
636                 dev = translate_ring_addresses(dev, msg->payload.addr.index);
637                 if (!dev)
638                         return -1;
639
640                 *pdev = dev;
641         }
642
643         return 0;
644 }
645
646 /*
647  * The virtio device sends us the available ring last used index.
648  */
649 static int
650 vhost_user_set_vring_base(struct virtio_net *dev,
651                           VhostUserMsg *msg)
652 {
653         dev->virtqueue[msg->payload.state.index]->last_used_idx  =
654                         msg->payload.state.num;
655         dev->virtqueue[msg->payload.state.index]->last_avail_idx =
656                         msg->payload.state.num;
657
658         return 0;
659 }
660
661 static int
662 add_one_guest_page(struct virtio_net *dev, uint64_t guest_phys_addr,
663                    uint64_t host_phys_addr, uint64_t size)
664 {
665         struct guest_page *page, *last_page;
666
667         if (dev->nr_guest_pages == dev->max_guest_pages) {
668                 dev->max_guest_pages *= 2;
669                 dev->guest_pages = realloc(dev->guest_pages,
670                                         dev->max_guest_pages * sizeof(*page));
671                 if (!dev->guest_pages) {
672                         RTE_LOG(ERR, VHOST_CONFIG, "cannot realloc guest_pages\n");
673                         return -1;
674                 }
675         }
676
677         if (dev->nr_guest_pages > 0) {
678                 last_page = &dev->guest_pages[dev->nr_guest_pages - 1];
679                 /* merge if the two pages are continuous */
680                 if (host_phys_addr == last_page->host_phys_addr +
681                                       last_page->size) {
682                         last_page->size += size;
683                         return 0;
684                 }
685         }
686
687         page = &dev->guest_pages[dev->nr_guest_pages++];
688         page->guest_phys_addr = guest_phys_addr;
689         page->host_phys_addr  = host_phys_addr;
690         page->size = size;
691
692         return 0;
693 }
694
695 static int
696 add_guest_pages(struct virtio_net *dev, struct rte_vhost_mem_region *reg,
697                 uint64_t page_size)
698 {
699         uint64_t reg_size = reg->size;
700         uint64_t host_user_addr  = reg->host_user_addr;
701         uint64_t guest_phys_addr = reg->guest_phys_addr;
702         uint64_t host_phys_addr;
703         uint64_t size;
704
705         host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)host_user_addr);
706         size = page_size - (guest_phys_addr & (page_size - 1));
707         size = RTE_MIN(size, reg_size);
708
709         if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr, size) < 0)
710                 return -1;
711
712         host_user_addr  += size;
713         guest_phys_addr += size;
714         reg_size -= size;
715
716         while (reg_size > 0) {
717                 size = RTE_MIN(reg_size, page_size);
718                 host_phys_addr = rte_mem_virt2iova((void *)(uintptr_t)
719                                                   host_user_addr);
720                 if (add_one_guest_page(dev, guest_phys_addr, host_phys_addr,
721                                 size) < 0)
722                         return -1;
723
724                 host_user_addr  += size;
725                 guest_phys_addr += size;
726                 reg_size -= size;
727         }
728
729         return 0;
730 }
731
732 #ifdef RTE_LIBRTE_VHOST_DEBUG
733 /* TODO: enable it only in debug mode? */
734 static void
735 dump_guest_pages(struct virtio_net *dev)
736 {
737         uint32_t i;
738         struct guest_page *page;
739
740         for (i = 0; i < dev->nr_guest_pages; i++) {
741                 page = &dev->guest_pages[i];
742
743                 RTE_LOG(INFO, VHOST_CONFIG,
744                         "guest physical page region %u\n"
745                         "\t guest_phys_addr: %" PRIx64 "\n"
746                         "\t host_phys_addr : %" PRIx64 "\n"
747                         "\t size           : %" PRIx64 "\n",
748                         i,
749                         page->guest_phys_addr,
750                         page->host_phys_addr,
751                         page->size);
752         }
753 }
754 #else
755 #define dump_guest_pages(dev)
756 #endif
757
758 static bool
759 vhost_memory_changed(struct VhostUserMemory *new,
760                      struct rte_vhost_memory *old)
761 {
762         uint32_t i;
763
764         if (new->nregions != old->nregions)
765                 return true;
766
767         for (i = 0; i < new->nregions; ++i) {
768                 VhostUserMemoryRegion *new_r = &new->regions[i];
769                 struct rte_vhost_mem_region *old_r = &old->regions[i];
770
771                 if (new_r->guest_phys_addr != old_r->guest_phys_addr)
772                         return true;
773                 if (new_r->memory_size != old_r->size)
774                         return true;
775                 if (new_r->userspace_addr != old_r->guest_user_addr)
776                         return true;
777         }
778
779         return false;
780 }
781
782 static int
783 vhost_user_set_mem_table(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
784 {
785         struct virtio_net *dev = *pdev;
786         struct VhostUserMemory memory = pmsg->payload.memory;
787         struct rte_vhost_mem_region *reg;
788         void *mmap_addr;
789         uint64_t mmap_size;
790         uint64_t mmap_offset;
791         uint64_t alignment;
792         uint32_t i;
793         int populate;
794         int fd;
795
796         if (memory.nregions > VHOST_MEMORY_MAX_NREGIONS) {
797                 RTE_LOG(ERR, VHOST_CONFIG,
798                         "too many memory regions (%u)\n", memory.nregions);
799                 return -1;
800         }
801
802         if (dev->mem && !vhost_memory_changed(&memory, dev->mem)) {
803                 RTE_LOG(INFO, VHOST_CONFIG,
804                         "(%d) memory regions not changed\n", dev->vid);
805
806                 for (i = 0; i < memory.nregions; i++)
807                         close(pmsg->fds[i]);
808
809                 return 0;
810         }
811
812         if (dev->mem) {
813                 free_mem_region(dev);
814                 rte_free(dev->mem);
815                 dev->mem = NULL;
816         }
817
818         /* Flush IOTLB cache as previous HVAs are now invalid */
819         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
820                 for (i = 0; i < dev->nr_vring; i++)
821                         vhost_user_iotlb_flush_all(dev->virtqueue[i]);
822
823         dev->nr_guest_pages = 0;
824         if (!dev->guest_pages) {
825                 dev->max_guest_pages = 8;
826                 dev->guest_pages = malloc(dev->max_guest_pages *
827                                                 sizeof(struct guest_page));
828                 if (dev->guest_pages == NULL) {
829                         RTE_LOG(ERR, VHOST_CONFIG,
830                                 "(%d) failed to allocate memory "
831                                 "for dev->guest_pages\n",
832                                 dev->vid);
833                         return -1;
834                 }
835         }
836
837         dev->mem = rte_zmalloc("vhost-mem-table", sizeof(struct rte_vhost_memory) +
838                 sizeof(struct rte_vhost_mem_region) * memory.nregions, 0);
839         if (dev->mem == NULL) {
840                 RTE_LOG(ERR, VHOST_CONFIG,
841                         "(%d) failed to allocate memory for dev->mem\n",
842                         dev->vid);
843                 return -1;
844         }
845         dev->mem->nregions = memory.nregions;
846
847         for (i = 0; i < memory.nregions; i++) {
848                 fd  = pmsg->fds[i];
849                 reg = &dev->mem->regions[i];
850
851                 reg->guest_phys_addr = memory.regions[i].guest_phys_addr;
852                 reg->guest_user_addr = memory.regions[i].userspace_addr;
853                 reg->size            = memory.regions[i].memory_size;
854                 reg->fd              = fd;
855
856                 mmap_offset = memory.regions[i].mmap_offset;
857
858                 /* Check for memory_size + mmap_offset overflow */
859                 if (mmap_offset >= -reg->size) {
860                         RTE_LOG(ERR, VHOST_CONFIG,
861                                 "mmap_offset (%#"PRIx64") and memory_size "
862                                 "(%#"PRIx64") overflow\n",
863                                 mmap_offset, reg->size);
864                         goto err_mmap;
865                 }
866
867                 mmap_size = reg->size + mmap_offset;
868
869                 /* mmap() without flag of MAP_ANONYMOUS, should be called
870                  * with length argument aligned with hugepagesz at older
871                  * longterm version Linux, like 2.6.32 and 3.2.72, or
872                  * mmap() will fail with EINVAL.
873                  *
874                  * to avoid failure, make sure in caller to keep length
875                  * aligned.
876                  */
877                 alignment = get_blk_size(fd);
878                 if (alignment == (uint64_t)-1) {
879                         RTE_LOG(ERR, VHOST_CONFIG,
880                                 "couldn't get hugepage size through fstat\n");
881                         goto err_mmap;
882                 }
883                 mmap_size = RTE_ALIGN_CEIL(mmap_size, alignment);
884
885                 populate = (dev->dequeue_zero_copy) ? MAP_POPULATE : 0;
886                 mmap_addr = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE,
887                                  MAP_SHARED | populate, fd, 0);
888
889                 if (mmap_addr == MAP_FAILED) {
890                         RTE_LOG(ERR, VHOST_CONFIG,
891                                 "mmap region %u failed.\n", i);
892                         goto err_mmap;
893                 }
894
895                 reg->mmap_addr = mmap_addr;
896                 reg->mmap_size = mmap_size;
897                 reg->host_user_addr = (uint64_t)(uintptr_t)mmap_addr +
898                                       mmap_offset;
899
900                 if (dev->dequeue_zero_copy)
901                         if (add_guest_pages(dev, reg, alignment) < 0) {
902                                 RTE_LOG(ERR, VHOST_CONFIG,
903                                         "adding guest pages to region %u failed.\n",
904                                         i);
905                                 goto err_mmap;
906                         }
907
908                 RTE_LOG(INFO, VHOST_CONFIG,
909                         "guest memory region %u, size: 0x%" PRIx64 "\n"
910                         "\t guest physical addr: 0x%" PRIx64 "\n"
911                         "\t guest virtual  addr: 0x%" PRIx64 "\n"
912                         "\t host  virtual  addr: 0x%" PRIx64 "\n"
913                         "\t mmap addr : 0x%" PRIx64 "\n"
914                         "\t mmap size : 0x%" PRIx64 "\n"
915                         "\t mmap align: 0x%" PRIx64 "\n"
916                         "\t mmap off  : 0x%" PRIx64 "\n",
917                         i, reg->size,
918                         reg->guest_phys_addr,
919                         reg->guest_user_addr,
920                         reg->host_user_addr,
921                         (uint64_t)(uintptr_t)mmap_addr,
922                         mmap_size,
923                         alignment,
924                         mmap_offset);
925         }
926
927         for (i = 0; i < dev->nr_vring; i++) {
928                 struct vhost_virtqueue *vq = dev->virtqueue[i];
929
930                 if (vq->desc || vq->avail || vq->used) {
931                         /*
932                          * If the memory table got updated, the ring addresses
933                          * need to be translated again as virtual addresses have
934                          * changed.
935                          */
936                         vring_invalidate(dev, vq);
937
938                         dev = translate_ring_addresses(dev, i);
939                         if (!dev)
940                                 return -1;
941
942                         *pdev = dev;
943                 }
944         }
945
946         dump_guest_pages(dev);
947
948         return 0;
949
950 err_mmap:
951         free_mem_region(dev);
952         rte_free(dev->mem);
953         dev->mem = NULL;
954         return -1;
955 }
956
957 static bool
958 vq_is_ready(struct virtio_net *dev, struct vhost_virtqueue *vq)
959 {
960         bool rings_ok;
961
962         if (!vq)
963                 return false;
964
965         if (vq_is_packed(dev))
966                 rings_ok = !!vq->desc_packed;
967         else
968                 rings_ok = vq->desc && vq->avail && vq->used;
969
970         return rings_ok &&
971                vq->kickfd != VIRTIO_UNINITIALIZED_EVENTFD &&
972                vq->callfd != VIRTIO_UNINITIALIZED_EVENTFD;
973 }
974
975 static int
976 virtio_is_ready(struct virtio_net *dev)
977 {
978         struct vhost_virtqueue *vq;
979         uint32_t i;
980
981         if (dev->nr_vring == 0)
982                 return 0;
983
984         for (i = 0; i < dev->nr_vring; i++) {
985                 vq = dev->virtqueue[i];
986
987                 if (!vq_is_ready(dev, vq))
988                         return 0;
989         }
990
991         RTE_LOG(INFO, VHOST_CONFIG,
992                 "virtio is now ready for processing.\n");
993         return 1;
994 }
995
996 static void
997 vhost_user_set_vring_call(struct virtio_net *dev, struct VhostUserMsg *pmsg)
998 {
999         struct vhost_vring_file file;
1000         struct vhost_virtqueue *vq;
1001
1002         file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
1003         if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1004                 file.fd = VIRTIO_INVALID_EVENTFD;
1005         else
1006                 file.fd = pmsg->fds[0];
1007         RTE_LOG(INFO, VHOST_CONFIG,
1008                 "vring call idx:%d file:%d\n", file.index, file.fd);
1009
1010         vq = dev->virtqueue[file.index];
1011         if (vq->callfd >= 0)
1012                 close(vq->callfd);
1013
1014         vq->callfd = file.fd;
1015 }
1016
1017 static void
1018 vhost_user_set_vring_kick(struct virtio_net **pdev, struct VhostUserMsg *pmsg)
1019 {
1020         struct vhost_vring_file file;
1021         struct vhost_virtqueue *vq;
1022         struct virtio_net *dev = *pdev;
1023
1024         file.index = pmsg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
1025         if (pmsg->payload.u64 & VHOST_USER_VRING_NOFD_MASK)
1026                 file.fd = VIRTIO_INVALID_EVENTFD;
1027         else
1028                 file.fd = pmsg->fds[0];
1029         RTE_LOG(INFO, VHOST_CONFIG,
1030                 "vring kick idx:%d file:%d\n", file.index, file.fd);
1031
1032         /* Interpret ring addresses only when ring is started. */
1033         dev = translate_ring_addresses(dev, file.index);
1034         if (!dev)
1035                 return;
1036
1037         *pdev = dev;
1038
1039         vq = dev->virtqueue[file.index];
1040
1041         /*
1042          * When VHOST_USER_F_PROTOCOL_FEATURES is not negotiated,
1043          * the ring starts already enabled. Otherwise, it is enabled via
1044          * the SET_VRING_ENABLE message.
1045          */
1046         if (!(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)))
1047                 vq->enabled = 1;
1048
1049         if (vq->kickfd >= 0)
1050                 close(vq->kickfd);
1051         vq->kickfd = file.fd;
1052 }
1053
1054 static void
1055 free_zmbufs(struct vhost_virtqueue *vq)
1056 {
1057         struct zcopy_mbuf *zmbuf, *next;
1058
1059         for (zmbuf = TAILQ_FIRST(&vq->zmbuf_list);
1060              zmbuf != NULL; zmbuf = next) {
1061                 next = TAILQ_NEXT(zmbuf, next);
1062
1063                 rte_pktmbuf_free(zmbuf->mbuf);
1064                 TAILQ_REMOVE(&vq->zmbuf_list, zmbuf, next);
1065         }
1066
1067         rte_free(vq->zmbufs);
1068 }
1069
1070 /*
1071  * when virtio is stopped, qemu will send us the GET_VRING_BASE message.
1072  */
1073 static int
1074 vhost_user_get_vring_base(struct virtio_net *dev,
1075                           VhostUserMsg *msg)
1076 {
1077         struct vhost_virtqueue *vq = dev->virtqueue[msg->payload.state.index];
1078
1079         /* We have to stop the queue (virtio) if it is running. */
1080         vhost_destroy_device_notify(dev);
1081
1082         dev->flags &= ~VIRTIO_DEV_READY;
1083         dev->flags &= ~VIRTIO_DEV_VDPA_CONFIGURED;
1084
1085         /* Here we are safe to get the last avail index */
1086         msg->payload.state.num = vq->last_avail_idx;
1087
1088         RTE_LOG(INFO, VHOST_CONFIG,
1089                 "vring base idx:%d file:%d\n", msg->payload.state.index,
1090                 msg->payload.state.num);
1091         /*
1092          * Based on current qemu vhost-user implementation, this message is
1093          * sent and only sent in vhost_vring_stop.
1094          * TODO: cleanup the vring, it isn't usable since here.
1095          */
1096         if (vq->kickfd >= 0)
1097                 close(vq->kickfd);
1098
1099         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
1100
1101         if (vq->callfd >= 0)
1102                 close(vq->callfd);
1103
1104         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
1105
1106         if (dev->dequeue_zero_copy)
1107                 free_zmbufs(vq);
1108         if (vq_is_packed(dev)) {
1109                 rte_free(vq->shadow_used_packed);
1110                 vq->shadow_used_packed = NULL;
1111         } else {
1112                 rte_free(vq->shadow_used_split);
1113                 vq->shadow_used_split = NULL;
1114         }
1115
1116         rte_free(vq->batch_copy_elems);
1117         vq->batch_copy_elems = NULL;
1118
1119         return 0;
1120 }
1121
1122 /*
1123  * when virtio queues are ready to work, qemu will send us to
1124  * enable the virtio queue pair.
1125  */
1126 static int
1127 vhost_user_set_vring_enable(struct virtio_net *dev,
1128                             VhostUserMsg *msg)
1129 {
1130         int enable = (int)msg->payload.state.num;
1131         int index = (int)msg->payload.state.index;
1132         struct rte_vdpa_device *vdpa_dev;
1133         int did = -1;
1134
1135         RTE_LOG(INFO, VHOST_CONFIG,
1136                 "set queue enable: %d to qp idx: %d\n",
1137                 enable, index);
1138
1139         did = dev->vdpa_dev_id;
1140         vdpa_dev = rte_vdpa_get_device(did);
1141         if (vdpa_dev && vdpa_dev->ops->set_vring_state)
1142                 vdpa_dev->ops->set_vring_state(dev->vid, index, enable);
1143
1144         if (dev->notify_ops->vring_state_changed)
1145                 dev->notify_ops->vring_state_changed(dev->vid,
1146                                 index, enable);
1147
1148         dev->virtqueue[index]->enabled = enable;
1149
1150         return 0;
1151 }
1152
1153 static void
1154 vhost_user_get_protocol_features(struct virtio_net *dev,
1155                                  struct VhostUserMsg *msg)
1156 {
1157         uint64_t features, protocol_features;
1158
1159         rte_vhost_driver_get_features(dev->ifname, &features);
1160         rte_vhost_driver_get_protocol_features(dev->ifname, &protocol_features);
1161
1162         /*
1163          * REPLY_ACK protocol feature is only mandatory for now
1164          * for IOMMU feature. If IOMMU is explicitly disabled by the
1165          * application, disable also REPLY_ACK feature for older buggy
1166          * Qemu versions (from v2.7.0 to v2.9.0).
1167          */
1168         if (!(features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
1169                 protocol_features &= ~(1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK);
1170
1171         msg->payload.u64 = protocol_features;
1172         msg->size = sizeof(msg->payload.u64);
1173 }
1174
1175 static void
1176 vhost_user_set_protocol_features(struct virtio_net *dev,
1177                                  uint64_t protocol_features)
1178 {
1179         if (protocol_features & ~VHOST_USER_PROTOCOL_FEATURES)
1180                 return;
1181
1182         dev->protocol_features = protocol_features;
1183 }
1184
1185 static int
1186 vhost_user_set_log_base(struct virtio_net *dev, struct VhostUserMsg *msg)
1187 {
1188         int fd = msg->fds[0];
1189         uint64_t size, off;
1190         void *addr;
1191
1192         if (fd < 0) {
1193                 RTE_LOG(ERR, VHOST_CONFIG, "invalid log fd: %d\n", fd);
1194                 return -1;
1195         }
1196
1197         if (msg->size != sizeof(VhostUserLog)) {
1198                 RTE_LOG(ERR, VHOST_CONFIG,
1199                         "invalid log base msg size: %"PRId32" != %d\n",
1200                         msg->size, (int)sizeof(VhostUserLog));
1201                 return -1;
1202         }
1203
1204         size = msg->payload.log.mmap_size;
1205         off  = msg->payload.log.mmap_offset;
1206
1207         /* Don't allow mmap_offset to point outside the mmap region */
1208         if (off > size) {
1209                 RTE_LOG(ERR, VHOST_CONFIG,
1210                         "log offset %#"PRIx64" exceeds log size %#"PRIx64"\n",
1211                         off, size);
1212                 return -1;
1213         }
1214
1215         RTE_LOG(INFO, VHOST_CONFIG,
1216                 "log mmap size: %"PRId64", offset: %"PRId64"\n",
1217                 size, off);
1218
1219         /*
1220          * mmap from 0 to workaround a hugepage mmap bug: mmap will
1221          * fail when offset is not page size aligned.
1222          */
1223         addr = mmap(0, size + off, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1224         close(fd);
1225         if (addr == MAP_FAILED) {
1226                 RTE_LOG(ERR, VHOST_CONFIG, "mmap log base failed!\n");
1227                 return -1;
1228         }
1229
1230         /*
1231          * Free previously mapped log memory on occasionally
1232          * multiple VHOST_USER_SET_LOG_BASE.
1233          */
1234         if (dev->log_addr) {
1235                 munmap((void *)(uintptr_t)dev->log_addr, dev->log_size);
1236         }
1237         dev->log_addr = (uint64_t)(uintptr_t)addr;
1238         dev->log_base = dev->log_addr + off;
1239         dev->log_size = size;
1240
1241         return 0;
1242 }
1243
1244 /*
1245  * An rarp packet is constructed and broadcasted to notify switches about
1246  * the new location of the migrated VM, so that packets from outside will
1247  * not be lost after migration.
1248  *
1249  * However, we don't actually "send" a rarp packet here, instead, we set
1250  * a flag 'broadcast_rarp' to let rte_vhost_dequeue_burst() inject it.
1251  */
1252 static int
1253 vhost_user_send_rarp(struct virtio_net *dev, struct VhostUserMsg *msg)
1254 {
1255         uint8_t *mac = (uint8_t *)&msg->payload.u64;
1256         struct rte_vdpa_device *vdpa_dev;
1257         int did = -1;
1258
1259         RTE_LOG(DEBUG, VHOST_CONFIG,
1260                 ":: mac: %02x:%02x:%02x:%02x:%02x:%02x\n",
1261                 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
1262         memcpy(dev->mac.addr_bytes, mac, 6);
1263
1264         /*
1265          * Set the flag to inject a RARP broadcast packet at
1266          * rte_vhost_dequeue_burst().
1267          *
1268          * rte_smp_wmb() is for making sure the mac is copied
1269          * before the flag is set.
1270          */
1271         rte_smp_wmb();
1272         rte_atomic16_set(&dev->broadcast_rarp, 1);
1273         did = dev->vdpa_dev_id;
1274         vdpa_dev = rte_vdpa_get_device(did);
1275         if (vdpa_dev && vdpa_dev->ops->migration_done)
1276                 vdpa_dev->ops->migration_done(dev->vid);
1277
1278         return 0;
1279 }
1280
1281 static int
1282 vhost_user_net_set_mtu(struct virtio_net *dev, struct VhostUserMsg *msg)
1283 {
1284         if (msg->payload.u64 < VIRTIO_MIN_MTU ||
1285                         msg->payload.u64 > VIRTIO_MAX_MTU) {
1286                 RTE_LOG(ERR, VHOST_CONFIG, "Invalid MTU size (%"PRIu64")\n",
1287                                 msg->payload.u64);
1288
1289                 return -1;
1290         }
1291
1292         dev->mtu = msg->payload.u64;
1293
1294         return 0;
1295 }
1296
1297 static int
1298 vhost_user_set_req_fd(struct virtio_net *dev, struct VhostUserMsg *msg)
1299 {
1300         int fd = msg->fds[0];
1301
1302         if (fd < 0) {
1303                 RTE_LOG(ERR, VHOST_CONFIG,
1304                                 "Invalid file descriptor for slave channel (%d)\n",
1305                                 fd);
1306                 return -1;
1307         }
1308
1309         dev->slave_req_fd = fd;
1310
1311         return 0;
1312 }
1313
1314 static int
1315 is_vring_iotlb_update(struct vhost_virtqueue *vq, struct vhost_iotlb_msg *imsg)
1316 {
1317         struct vhost_vring_addr *ra;
1318         uint64_t start, end;
1319
1320         start = imsg->iova;
1321         end = start + imsg->size;
1322
1323         ra = &vq->ring_addrs;
1324         if (ra->desc_user_addr >= start && ra->desc_user_addr < end)
1325                 return 1;
1326         if (ra->avail_user_addr >= start && ra->avail_user_addr < end)
1327                 return 1;
1328         if (ra->used_user_addr >= start && ra->used_user_addr < end)
1329                 return 1;
1330
1331         return 0;
1332 }
1333
1334 static int
1335 is_vring_iotlb_invalidate(struct vhost_virtqueue *vq,
1336                                 struct vhost_iotlb_msg *imsg)
1337 {
1338         uint64_t istart, iend, vstart, vend;
1339
1340         istart = imsg->iova;
1341         iend = istart + imsg->size - 1;
1342
1343         vstart = (uintptr_t)vq->desc;
1344         vend = vstart + sizeof(struct vring_desc) * vq->size - 1;
1345         if (vstart <= iend && istart <= vend)
1346                 return 1;
1347
1348         vstart = (uintptr_t)vq->avail;
1349         vend = vstart + sizeof(struct vring_avail);
1350         vend += sizeof(uint16_t) * vq->size - 1;
1351         if (vstart <= iend && istart <= vend)
1352                 return 1;
1353
1354         vstart = (uintptr_t)vq->used;
1355         vend = vstart + sizeof(struct vring_used);
1356         vend += sizeof(struct vring_used_elem) * vq->size - 1;
1357         if (vstart <= iend && istart <= vend)
1358                 return 1;
1359
1360         return 0;
1361 }
1362
1363 static int
1364 vhost_user_iotlb_msg(struct virtio_net **pdev, struct VhostUserMsg *msg)
1365 {
1366         struct virtio_net *dev = *pdev;
1367         struct vhost_iotlb_msg *imsg = &msg->payload.iotlb;
1368         uint16_t i;
1369         uint64_t vva, len;
1370
1371         switch (imsg->type) {
1372         case VHOST_IOTLB_UPDATE:
1373                 len = imsg->size;
1374                 vva = qva_to_vva(dev, imsg->uaddr, &len);
1375                 if (!vva)
1376                         return -1;
1377
1378                 for (i = 0; i < dev->nr_vring; i++) {
1379                         struct vhost_virtqueue *vq = dev->virtqueue[i];
1380
1381                         vhost_user_iotlb_cache_insert(vq, imsg->iova, vva,
1382                                         len, imsg->perm);
1383
1384                         if (is_vring_iotlb_update(vq, imsg))
1385                                 *pdev = dev = translate_ring_addresses(dev, i);
1386                 }
1387                 break;
1388         case VHOST_IOTLB_INVALIDATE:
1389                 for (i = 0; i < dev->nr_vring; i++) {
1390                         struct vhost_virtqueue *vq = dev->virtqueue[i];
1391
1392                         vhost_user_iotlb_cache_remove(vq, imsg->iova,
1393                                         imsg->size);
1394
1395                         if (is_vring_iotlb_invalidate(vq, imsg))
1396                                 vring_invalidate(dev, vq);
1397                 }
1398                 break;
1399         default:
1400                 RTE_LOG(ERR, VHOST_CONFIG, "Invalid IOTLB message type (%d)\n",
1401                                 imsg->type);
1402                 return -1;
1403         }
1404
1405         return 0;
1406 }
1407
1408 /* return bytes# of read on success or negative val on failure. */
1409 static int
1410 read_vhost_message(int sockfd, struct VhostUserMsg *msg)
1411 {
1412         int ret;
1413
1414         ret = read_fd_message(sockfd, (char *)msg, VHOST_USER_HDR_SIZE,
1415                 msg->fds, VHOST_MEMORY_MAX_NREGIONS);
1416         if (ret <= 0)
1417                 return ret;
1418
1419         if (msg && msg->size) {
1420                 if (msg->size > sizeof(msg->payload)) {
1421                         RTE_LOG(ERR, VHOST_CONFIG,
1422                                 "invalid msg size: %d\n", msg->size);
1423                         return -1;
1424                 }
1425                 ret = read(sockfd, &msg->payload, msg->size);
1426                 if (ret <= 0)
1427                         return ret;
1428                 if (ret != (int)msg->size) {
1429                         RTE_LOG(ERR, VHOST_CONFIG,
1430                                 "read control message failed\n");
1431                         return -1;
1432                 }
1433         }
1434
1435         return ret;
1436 }
1437
1438 static int
1439 send_vhost_message(int sockfd, struct VhostUserMsg *msg, int *fds, int fd_num)
1440 {
1441         if (!msg)
1442                 return 0;
1443
1444         return send_fd_message(sockfd, (char *)msg,
1445                 VHOST_USER_HDR_SIZE + msg->size, fds, fd_num);
1446 }
1447
1448 static int
1449 send_vhost_reply(int sockfd, struct VhostUserMsg *msg)
1450 {
1451         if (!msg)
1452                 return 0;
1453
1454         msg->flags &= ~VHOST_USER_VERSION_MASK;
1455         msg->flags &= ~VHOST_USER_NEED_REPLY;
1456         msg->flags |= VHOST_USER_VERSION;
1457         msg->flags |= VHOST_USER_REPLY_MASK;
1458
1459         return send_vhost_message(sockfd, msg, NULL, 0);
1460 }
1461
1462 static int
1463 send_vhost_slave_message(struct virtio_net *dev, struct VhostUserMsg *msg,
1464                          int *fds, int fd_num)
1465 {
1466         int ret;
1467
1468         if (msg->flags & VHOST_USER_NEED_REPLY)
1469                 rte_spinlock_lock(&dev->slave_req_lock);
1470
1471         ret = send_vhost_message(dev->slave_req_fd, msg, fds, fd_num);
1472         if (ret < 0 && (msg->flags & VHOST_USER_NEED_REPLY))
1473                 rte_spinlock_unlock(&dev->slave_req_lock);
1474
1475         return ret;
1476 }
1477
1478 /*
1479  * Allocate a queue pair if it hasn't been allocated yet
1480  */
1481 static int
1482 vhost_user_check_and_alloc_queue_pair(struct virtio_net *dev, VhostUserMsg *msg)
1483 {
1484         uint16_t vring_idx;
1485
1486         switch (msg->request.master) {
1487         case VHOST_USER_SET_VRING_KICK:
1488         case VHOST_USER_SET_VRING_CALL:
1489         case VHOST_USER_SET_VRING_ERR:
1490                 vring_idx = msg->payload.u64 & VHOST_USER_VRING_IDX_MASK;
1491                 break;
1492         case VHOST_USER_SET_VRING_NUM:
1493         case VHOST_USER_SET_VRING_BASE:
1494         case VHOST_USER_SET_VRING_ENABLE:
1495                 vring_idx = msg->payload.state.index;
1496                 break;
1497         case VHOST_USER_SET_VRING_ADDR:
1498                 vring_idx = msg->payload.addr.index;
1499                 break;
1500         default:
1501                 return 0;
1502         }
1503
1504         if (vring_idx >= VHOST_MAX_VRING) {
1505                 RTE_LOG(ERR, VHOST_CONFIG,
1506                         "invalid vring index: %u\n", vring_idx);
1507                 return -1;
1508         }
1509
1510         if (dev->virtqueue[vring_idx])
1511                 return 0;
1512
1513         return alloc_vring_queue(dev, vring_idx);
1514 }
1515
1516 static void
1517 vhost_user_lock_all_queue_pairs(struct virtio_net *dev)
1518 {
1519         unsigned int i = 0;
1520         unsigned int vq_num = 0;
1521
1522         while (vq_num < dev->nr_vring) {
1523                 struct vhost_virtqueue *vq = dev->virtqueue[i];
1524
1525                 if (vq) {
1526                         rte_spinlock_lock(&vq->access_lock);
1527                         vq_num++;
1528                 }
1529                 i++;
1530         }
1531 }
1532
1533 static void
1534 vhost_user_unlock_all_queue_pairs(struct virtio_net *dev)
1535 {
1536         unsigned int i = 0;
1537         unsigned int vq_num = 0;
1538
1539         while (vq_num < dev->nr_vring) {
1540                 struct vhost_virtqueue *vq = dev->virtqueue[i];
1541
1542                 if (vq) {
1543                         rte_spinlock_unlock(&vq->access_lock);
1544                         vq_num++;
1545                 }
1546                 i++;
1547         }
1548 }
1549
1550 int
1551 vhost_user_msg_handler(int vid, int fd)
1552 {
1553         struct virtio_net *dev;
1554         struct VhostUserMsg msg;
1555         struct rte_vdpa_device *vdpa_dev;
1556         int did = -1;
1557         int ret;
1558         int unlock_required = 0;
1559         uint32_t skip_master = 0;
1560
1561         dev = get_device(vid);
1562         if (dev == NULL)
1563                 return -1;
1564
1565         if (!dev->notify_ops) {
1566                 dev->notify_ops = vhost_driver_callback_get(dev->ifname);
1567                 if (!dev->notify_ops) {
1568                         RTE_LOG(ERR, VHOST_CONFIG,
1569                                 "failed to get callback ops for driver %s\n",
1570                                 dev->ifname);
1571                         return -1;
1572                 }
1573         }
1574
1575         ret = read_vhost_message(fd, &msg);
1576         if (ret <= 0 || msg.request.master >= VHOST_USER_MAX) {
1577                 if (ret < 0)
1578                         RTE_LOG(ERR, VHOST_CONFIG,
1579                                 "vhost read message failed\n");
1580                 else if (ret == 0)
1581                         RTE_LOG(INFO, VHOST_CONFIG,
1582                                 "vhost peer closed\n");
1583                 else
1584                         RTE_LOG(ERR, VHOST_CONFIG,
1585                                 "vhost read incorrect message\n");
1586
1587                 return -1;
1588         }
1589
1590         ret = 0;
1591         if (msg.request.master != VHOST_USER_IOTLB_MSG)
1592                 RTE_LOG(INFO, VHOST_CONFIG, "read message %s\n",
1593                         vhost_message_str[msg.request.master]);
1594         else
1595                 RTE_LOG(DEBUG, VHOST_CONFIG, "read message %s\n",
1596                         vhost_message_str[msg.request.master]);
1597
1598         ret = vhost_user_check_and_alloc_queue_pair(dev, &msg);
1599         if (ret < 0) {
1600                 RTE_LOG(ERR, VHOST_CONFIG,
1601                         "failed to alloc queue\n");
1602                 return -1;
1603         }
1604
1605         /*
1606          * Note: we don't lock all queues on VHOST_USER_GET_VRING_BASE
1607          * and VHOST_USER_RESET_OWNER, since it is sent when virtio stops
1608          * and device is destroyed. destroy_device waits for queues to be
1609          * inactive, so it is safe. Otherwise taking the access_lock
1610          * would cause a dead lock.
1611          */
1612         switch (msg.request.master) {
1613         case VHOST_USER_SET_FEATURES:
1614         case VHOST_USER_SET_PROTOCOL_FEATURES:
1615         case VHOST_USER_SET_OWNER:
1616         case VHOST_USER_SET_MEM_TABLE:
1617         case VHOST_USER_SET_LOG_BASE:
1618         case VHOST_USER_SET_LOG_FD:
1619         case VHOST_USER_SET_VRING_NUM:
1620         case VHOST_USER_SET_VRING_ADDR:
1621         case VHOST_USER_SET_VRING_BASE:
1622         case VHOST_USER_SET_VRING_KICK:
1623         case VHOST_USER_SET_VRING_CALL:
1624         case VHOST_USER_SET_VRING_ERR:
1625         case VHOST_USER_SET_VRING_ENABLE:
1626         case VHOST_USER_SEND_RARP:
1627         case VHOST_USER_NET_SET_MTU:
1628         case VHOST_USER_SET_SLAVE_REQ_FD:
1629                 vhost_user_lock_all_queue_pairs(dev);
1630                 unlock_required = 1;
1631                 break;
1632         default:
1633                 break;
1634
1635         }
1636
1637         if (dev->extern_ops.pre_msg_handle) {
1638                 uint32_t need_reply;
1639
1640                 ret = (*dev->extern_ops.pre_msg_handle)(dev->vid,
1641                                 (void *)&msg, &need_reply, &skip_master);
1642                 if (ret < 0)
1643                         goto skip_to_reply;
1644
1645                 if (need_reply)
1646                         send_vhost_reply(fd, &msg);
1647
1648                 if (skip_master)
1649                         goto skip_to_post_handle;
1650         }
1651
1652         switch (msg.request.master) {
1653         case VHOST_USER_GET_FEATURES:
1654                 msg.payload.u64 = vhost_user_get_features(dev);
1655                 msg.size = sizeof(msg.payload.u64);
1656                 send_vhost_reply(fd, &msg);
1657                 break;
1658         case VHOST_USER_SET_FEATURES:
1659                 ret = vhost_user_set_features(dev, msg.payload.u64);
1660                 if (ret)
1661                         return -1;
1662                 break;
1663
1664         case VHOST_USER_GET_PROTOCOL_FEATURES:
1665                 vhost_user_get_protocol_features(dev, &msg);
1666                 send_vhost_reply(fd, &msg);
1667                 break;
1668         case VHOST_USER_SET_PROTOCOL_FEATURES:
1669                 vhost_user_set_protocol_features(dev, msg.payload.u64);
1670                 break;
1671
1672         case VHOST_USER_SET_OWNER:
1673                 vhost_user_set_owner();
1674                 break;
1675         case VHOST_USER_RESET_OWNER:
1676                 vhost_user_reset_owner(dev);
1677                 break;
1678
1679         case VHOST_USER_SET_MEM_TABLE:
1680                 ret = vhost_user_set_mem_table(&dev, &msg);
1681                 break;
1682
1683         case VHOST_USER_SET_LOG_BASE:
1684                 vhost_user_set_log_base(dev, &msg);
1685
1686                 /* it needs a reply */
1687                 msg.size = sizeof(msg.payload.u64);
1688                 send_vhost_reply(fd, &msg);
1689                 break;
1690         case VHOST_USER_SET_LOG_FD:
1691                 close(msg.fds[0]);
1692                 RTE_LOG(INFO, VHOST_CONFIG, "not implemented.\n");
1693                 break;
1694
1695         case VHOST_USER_SET_VRING_NUM:
1696                 vhost_user_set_vring_num(dev, &msg);
1697                 break;
1698         case VHOST_USER_SET_VRING_ADDR:
1699                 vhost_user_set_vring_addr(&dev, &msg);
1700                 break;
1701         case VHOST_USER_SET_VRING_BASE:
1702                 vhost_user_set_vring_base(dev, &msg);
1703                 break;
1704
1705         case VHOST_USER_GET_VRING_BASE:
1706                 vhost_user_get_vring_base(dev, &msg);
1707                 msg.size = sizeof(msg.payload.state);
1708                 send_vhost_reply(fd, &msg);
1709                 break;
1710
1711         case VHOST_USER_SET_VRING_KICK:
1712                 vhost_user_set_vring_kick(&dev, &msg);
1713                 break;
1714         case VHOST_USER_SET_VRING_CALL:
1715                 vhost_user_set_vring_call(dev, &msg);
1716                 break;
1717
1718         case VHOST_USER_SET_VRING_ERR:
1719                 if (!(msg.payload.u64 & VHOST_USER_VRING_NOFD_MASK))
1720                         close(msg.fds[0]);
1721                 RTE_LOG(INFO, VHOST_CONFIG, "not implemented\n");
1722                 break;
1723
1724         case VHOST_USER_GET_QUEUE_NUM:
1725                 msg.payload.u64 = (uint64_t)vhost_user_get_queue_num(dev);
1726                 msg.size = sizeof(msg.payload.u64);
1727                 send_vhost_reply(fd, &msg);
1728                 break;
1729
1730         case VHOST_USER_SET_VRING_ENABLE:
1731                 vhost_user_set_vring_enable(dev, &msg);
1732                 break;
1733         case VHOST_USER_SEND_RARP:
1734                 vhost_user_send_rarp(dev, &msg);
1735                 break;
1736
1737         case VHOST_USER_NET_SET_MTU:
1738                 ret = vhost_user_net_set_mtu(dev, &msg);
1739                 break;
1740
1741         case VHOST_USER_SET_SLAVE_REQ_FD:
1742                 ret = vhost_user_set_req_fd(dev, &msg);
1743                 break;
1744
1745         case VHOST_USER_IOTLB_MSG:
1746                 ret = vhost_user_iotlb_msg(&dev, &msg);
1747                 break;
1748
1749         default:
1750                 ret = -1;
1751                 break;
1752         }
1753
1754 skip_to_post_handle:
1755         if (dev->extern_ops.post_msg_handle) {
1756                 uint32_t need_reply;
1757
1758                 ret = (*dev->extern_ops.post_msg_handle)(
1759                                 dev->vid, (void *)&msg, &need_reply);
1760                 if (ret < 0)
1761                         goto skip_to_reply;
1762
1763                 if (need_reply)
1764                         send_vhost_reply(fd, &msg);
1765         }
1766
1767 skip_to_reply:
1768         if (unlock_required)
1769                 vhost_user_unlock_all_queue_pairs(dev);
1770
1771         if (msg.flags & VHOST_USER_NEED_REPLY) {
1772                 msg.payload.u64 = !!ret;
1773                 msg.size = sizeof(msg.payload.u64);
1774                 send_vhost_reply(fd, &msg);
1775         }
1776
1777         if (!(dev->flags & VIRTIO_DEV_RUNNING) && virtio_is_ready(dev)) {
1778                 dev->flags |= VIRTIO_DEV_READY;
1779
1780                 if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
1781                         if (dev->dequeue_zero_copy) {
1782                                 RTE_LOG(INFO, VHOST_CONFIG,
1783                                                 "dequeue zero copy is enabled\n");
1784                         }
1785
1786                         if (dev->notify_ops->new_device(dev->vid) == 0)
1787                                 dev->flags |= VIRTIO_DEV_RUNNING;
1788                 }
1789         }
1790
1791         did = dev->vdpa_dev_id;
1792         vdpa_dev = rte_vdpa_get_device(did);
1793         if (vdpa_dev && virtio_is_ready(dev) &&
1794                         !(dev->flags & VIRTIO_DEV_VDPA_CONFIGURED) &&
1795                         msg.request.master == VHOST_USER_SET_VRING_ENABLE) {
1796                 if (vdpa_dev->ops->dev_conf)
1797                         vdpa_dev->ops->dev_conf(dev->vid);
1798                 dev->flags |= VIRTIO_DEV_VDPA_CONFIGURED;
1799                 if (vhost_user_host_notifier_ctrl(dev->vid, true) != 0) {
1800                         RTE_LOG(INFO, VHOST_CONFIG,
1801                                 "(%d) software relay is used for vDPA, performance may be low.\n",
1802                                 dev->vid);
1803                 }
1804         }
1805
1806         return 0;
1807 }
1808
1809 static int process_slave_message_reply(struct virtio_net *dev,
1810                                        const VhostUserMsg *msg)
1811 {
1812         VhostUserMsg msg_reply;
1813         int ret;
1814
1815         if ((msg->flags & VHOST_USER_NEED_REPLY) == 0)
1816                 return 0;
1817
1818         if (read_vhost_message(dev->slave_req_fd, &msg_reply) < 0) {
1819                 ret = -1;
1820                 goto out;
1821         }
1822
1823         if (msg_reply.request.slave != msg->request.slave) {
1824                 RTE_LOG(ERR, VHOST_CONFIG,
1825                         "Received unexpected msg type (%u), expected %u\n",
1826                         msg_reply.request.slave, msg->request.slave);
1827                 ret = -1;
1828                 goto out;
1829         }
1830
1831         ret = msg_reply.payload.u64 ? -1 : 0;
1832
1833 out:
1834         rte_spinlock_unlock(&dev->slave_req_lock);
1835         return ret;
1836 }
1837
1838 int
1839 vhost_user_iotlb_miss(struct virtio_net *dev, uint64_t iova, uint8_t perm)
1840 {
1841         int ret;
1842         struct VhostUserMsg msg = {
1843                 .request.slave = VHOST_USER_SLAVE_IOTLB_MSG,
1844                 .flags = VHOST_USER_VERSION,
1845                 .size = sizeof(msg.payload.iotlb),
1846                 .payload.iotlb = {
1847                         .iova = iova,
1848                         .perm = perm,
1849                         .type = VHOST_IOTLB_MISS,
1850                 },
1851         };
1852
1853         ret = send_vhost_message(dev->slave_req_fd, &msg, NULL, 0);
1854         if (ret < 0) {
1855                 RTE_LOG(ERR, VHOST_CONFIG,
1856                                 "Failed to send IOTLB miss message (%d)\n",
1857                                 ret);
1858                 return ret;
1859         }
1860
1861         return 0;
1862 }
1863
1864 static int vhost_user_slave_set_vring_host_notifier(struct virtio_net *dev,
1865                                                     int index, int fd,
1866                                                     uint64_t offset,
1867                                                     uint64_t size)
1868 {
1869         int *fdp = NULL;
1870         size_t fd_num = 0;
1871         int ret;
1872         struct VhostUserMsg msg = {
1873                 .request.slave = VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG,
1874                 .flags = VHOST_USER_VERSION | VHOST_USER_NEED_REPLY,
1875                 .size = sizeof(msg.payload.area),
1876                 .payload.area = {
1877                         .u64 = index & VHOST_USER_VRING_IDX_MASK,
1878                         .size = size,
1879                         .offset = offset,
1880                 },
1881         };
1882
1883         if (fd < 0)
1884                 msg.payload.area.u64 |= VHOST_USER_VRING_NOFD_MASK;
1885         else {
1886                 fdp = &fd;
1887                 fd_num = 1;
1888         }
1889
1890         ret = send_vhost_slave_message(dev, &msg, fdp, fd_num);
1891         if (ret < 0) {
1892                 RTE_LOG(ERR, VHOST_CONFIG,
1893                         "Failed to set host notifier (%d)\n", ret);
1894                 return ret;
1895         }
1896
1897         return process_slave_message_reply(dev, &msg);
1898 }
1899
1900 int vhost_user_host_notifier_ctrl(int vid, bool enable)
1901 {
1902         struct virtio_net *dev;
1903         struct rte_vdpa_device *vdpa_dev;
1904         int vfio_device_fd, did, ret = 0;
1905         uint64_t offset, size;
1906         unsigned int i;
1907
1908         dev = get_device(vid);
1909         if (!dev)
1910                 return -ENODEV;
1911
1912         did = dev->vdpa_dev_id;
1913         if (did < 0)
1914                 return -EINVAL;
1915
1916         if (!(dev->features & (1ULL << VIRTIO_F_VERSION_1)) ||
1917             !(dev->features & (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) ||
1918             !(dev->protocol_features &
1919                         (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ)) ||
1920             !(dev->protocol_features &
1921                         (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD)) ||
1922             !(dev->protocol_features &
1923                         (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER)))
1924                 return -ENOTSUP;
1925
1926         vdpa_dev = rte_vdpa_get_device(did);
1927         if (!vdpa_dev)
1928                 return -ENODEV;
1929
1930         RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_vfio_device_fd, -ENOTSUP);
1931         RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_notify_area, -ENOTSUP);
1932
1933         vfio_device_fd = vdpa_dev->ops->get_vfio_device_fd(vid);
1934         if (vfio_device_fd < 0)
1935                 return -ENOTSUP;
1936
1937         if (enable) {
1938                 for (i = 0; i < dev->nr_vring; i++) {
1939                         if (vdpa_dev->ops->get_notify_area(vid, i, &offset,
1940                                         &size) < 0) {
1941                                 ret = -ENOTSUP;
1942                                 goto disable;
1943                         }
1944
1945                         if (vhost_user_slave_set_vring_host_notifier(dev, i,
1946                                         vfio_device_fd, offset, size) < 0) {
1947                                 ret = -EFAULT;
1948                                 goto disable;
1949                         }
1950                 }
1951         } else {
1952 disable:
1953                 for (i = 0; i < dev->nr_vring; i++) {
1954                         vhost_user_slave_set_vring_host_notifier(dev, i, -1,
1955                                         0, 0);
1956                 }
1957         }
1958
1959         return ret;
1960 }