7d427b60a50e31e8a07a32d90a223353b5629998
[dpdk.git] / lib / librte_vhost / vhost.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4
5 #include <linux/vhost.h>
6 #include <linux/virtio_net.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #ifdef RTE_LIBRTE_VHOST_NUMA
11 #include <numa.h>
12 #include <numaif.h>
13 #endif
14
15 #include <rte_errno.h>
16 #include <rte_ethdev.h>
17 #include <rte_log.h>
18 #include <rte_string_fns.h>
19 #include <rte_memory.h>
20 #include <rte_malloc.h>
21 #include <rte_vhost.h>
22 #include <rte_rwlock.h>
23
24 #include "iotlb.h"
25 #include "vhost.h"
26 #include "vhost_user.h"
27
28 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
29
30 /* Called with iotlb_lock read-locked */
31 uint64_t
32 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
33                     uint64_t iova, uint64_t *size, uint8_t perm)
34 {
35         uint64_t vva, tmp_size;
36
37         if (unlikely(!*size))
38                 return 0;
39
40         tmp_size = *size;
41
42         vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
43         if (tmp_size == *size)
44                 return vva;
45
46         iova += tmp_size;
47
48         if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
49                 /*
50                  * iotlb_lock is read-locked for a full burst,
51                  * but it only protects the iotlb cache.
52                  * In case of IOTLB miss, we might block on the socket,
53                  * which could cause a deadlock with QEMU if an IOTLB update
54                  * is being handled. We can safely unlock here to avoid it.
55                  */
56                 vhost_user_iotlb_rd_unlock(vq);
57
58                 vhost_user_iotlb_pending_insert(vq, iova, perm);
59                 if (vhost_user_iotlb_miss(dev, iova, perm)) {
60                         RTE_LOG(ERR, VHOST_CONFIG,
61                                 "IOTLB miss req failed for IOVA 0x%" PRIx64 "\n",
62                                 iova);
63                         vhost_user_iotlb_pending_remove(vq, iova, 1, perm);
64                 }
65
66                 vhost_user_iotlb_rd_lock(vq);
67         }
68
69         return 0;
70 }
71
72 #define VHOST_LOG_PAGE  4096
73
74 /*
75  * Atomically set a bit in memory.
76  */
77 static __rte_always_inline void
78 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
79 {
80 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
81         /*
82          * __sync_ built-ins are deprecated, but __atomic_ ones
83          * are sub-optimized in older GCC versions.
84          */
85         __sync_fetch_and_or_1(addr, (1U << nr));
86 #else
87         __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
88 #endif
89 }
90
91 static __rte_always_inline void
92 vhost_log_page(uint8_t *log_base, uint64_t page)
93 {
94         vhost_set_bit(page % 8, &log_base[page / 8]);
95 }
96
97 void
98 __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
99 {
100         uint64_t page;
101
102         if (unlikely(!dev->log_base || !len))
103                 return;
104
105         if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
106                 return;
107
108         /* To make sure guest memory updates are committed before logging */
109         rte_smp_wmb();
110
111         page = addr / VHOST_LOG_PAGE;
112         while (page * VHOST_LOG_PAGE < addr + len) {
113                 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
114                 page += 1;
115         }
116 }
117
118 void
119 __vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
120 {
121         unsigned long *log_base;
122         int i;
123
124         if (unlikely(!dev->log_base))
125                 return;
126
127         rte_smp_wmb();
128
129         log_base = (unsigned long *)(uintptr_t)dev->log_base;
130
131         for (i = 0; i < vq->log_cache_nb_elem; i++) {
132                 struct log_cache_entry *elem = vq->log_cache + i;
133
134 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
135                 /*
136                  * '__sync' builtins are deprecated, but '__atomic' ones
137                  * are sub-optimized in older GCC versions.
138                  */
139                 __sync_fetch_and_or(log_base + elem->offset, elem->val);
140 #else
141                 __atomic_fetch_or(log_base + elem->offset, elem->val,
142                                 __ATOMIC_RELAXED);
143 #endif
144         }
145
146         rte_smp_wmb();
147
148         vq->log_cache_nb_elem = 0;
149 }
150
151 static __rte_always_inline void
152 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
153                         uint64_t page)
154 {
155         uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
156         uint32_t offset = page / (sizeof(unsigned long) << 3);
157         int i;
158
159         for (i = 0; i < vq->log_cache_nb_elem; i++) {
160                 struct log_cache_entry *elem = vq->log_cache + i;
161
162                 if (elem->offset == offset) {
163                         elem->val |= (1UL << bit_nr);
164                         return;
165                 }
166         }
167
168         if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
169                 /*
170                  * No more room for a new log cache entry,
171                  * so write the dirty log map directly.
172                  */
173                 rte_smp_wmb();
174                 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
175
176                 return;
177         }
178
179         vq->log_cache[i].offset = offset;
180         vq->log_cache[i].val = (1UL << bit_nr);
181         vq->log_cache_nb_elem++;
182 }
183
184 void
185 __vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
186                         uint64_t addr, uint64_t len)
187 {
188         uint64_t page;
189
190         if (unlikely(!dev->log_base || !len))
191                 return;
192
193         if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
194                 return;
195
196         page = addr / VHOST_LOG_PAGE;
197         while (page * VHOST_LOG_PAGE < addr + len) {
198                 vhost_log_cache_page(dev, vq, page);
199                 page += 1;
200         }
201 }
202
203 void
204 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
205 {
206         if ((vq->callfd >= 0) && (destroy != 0))
207                 close(vq->callfd);
208         if (vq->kickfd >= 0)
209                 close(vq->kickfd);
210 }
211
212 /*
213  * Unmap any memory, close any file descriptors and
214  * free any memory owned by a device.
215  */
216 void
217 cleanup_device(struct virtio_net *dev, int destroy)
218 {
219         uint32_t i;
220
221         vhost_backend_cleanup(dev);
222
223         for (i = 0; i < dev->nr_vring; i++)
224                 cleanup_vq(dev->virtqueue[i], destroy);
225 }
226
227 void
228 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
229 {
230         if (vq_is_packed(dev))
231                 rte_free(vq->shadow_used_packed);
232         else
233                 rte_free(vq->shadow_used_split);
234         rte_free(vq->batch_copy_elems);
235         rte_mempool_free(vq->iotlb_pool);
236         rte_free(vq);
237 }
238
239 /*
240  * Release virtqueues and device memory.
241  */
242 static void
243 free_device(struct virtio_net *dev)
244 {
245         uint32_t i;
246
247         for (i = 0; i < dev->nr_vring; i++)
248                 free_vq(dev, dev->virtqueue[i]);
249
250         rte_free(dev);
251 }
252
253 static int
254 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
255 {
256         uint64_t req_size, size;
257
258         req_size = sizeof(struct vring_desc) * vq->size;
259         size = req_size;
260         vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
261                                                 vq->ring_addrs.desc_user_addr,
262                                                 &size, VHOST_ACCESS_RW);
263         if (!vq->desc || size != req_size)
264                 return -1;
265
266         req_size = sizeof(struct vring_avail);
267         req_size += sizeof(uint16_t) * vq->size;
268         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
269                 req_size += sizeof(uint16_t);
270         size = req_size;
271         vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
272                                                 vq->ring_addrs.avail_user_addr,
273                                                 &size, VHOST_ACCESS_RW);
274         if (!vq->avail || size != req_size)
275                 return -1;
276
277         req_size = sizeof(struct vring_used);
278         req_size += sizeof(struct vring_used_elem) * vq->size;
279         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
280                 req_size += sizeof(uint16_t);
281         size = req_size;
282         vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
283                                                 vq->ring_addrs.used_user_addr,
284                                                 &size, VHOST_ACCESS_RW);
285         if (!vq->used || size != req_size)
286                 return -1;
287
288         return 0;
289 }
290
291 static int
292 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
293 {
294         uint64_t req_size, size;
295
296         req_size = sizeof(struct vring_packed_desc) * vq->size;
297         size = req_size;
298         vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
299                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
300                                 &size, VHOST_ACCESS_RW);
301         if (!vq->desc_packed || size != req_size)
302                 return -1;
303
304         req_size = sizeof(struct vring_packed_desc_event);
305         size = req_size;
306         vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
307                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
308                                 &size, VHOST_ACCESS_RW);
309         if (!vq->driver_event || size != req_size)
310                 return -1;
311
312         req_size = sizeof(struct vring_packed_desc_event);
313         size = req_size;
314         vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
315                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
316                                 &size, VHOST_ACCESS_RW);
317         if (!vq->device_event || size != req_size)
318                 return -1;
319
320         return 0;
321 }
322
323 int
324 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
325 {
326
327         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
328                 goto out;
329
330         if (vq_is_packed(dev)) {
331                 if (vring_translate_packed(dev, vq) < 0)
332                         return -1;
333         } else {
334                 if (vring_translate_split(dev, vq) < 0)
335                         return -1;
336         }
337 out:
338         vq->access_ok = 1;
339
340         return 0;
341 }
342
343 void
344 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
345 {
346         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
347                 vhost_user_iotlb_wr_lock(vq);
348
349         vq->access_ok = 0;
350         vq->desc = NULL;
351         vq->avail = NULL;
352         vq->used = NULL;
353
354         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
355                 vhost_user_iotlb_wr_unlock(vq);
356 }
357
358 static void
359 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
360 {
361         struct vhost_virtqueue *vq;
362
363         if (vring_idx >= VHOST_MAX_VRING) {
364                 RTE_LOG(ERR, VHOST_CONFIG,
365                                 "Failed not init vring, out of bound (%d)\n",
366                                 vring_idx);
367                 return;
368         }
369
370         vq = dev->virtqueue[vring_idx];
371
372         memset(vq, 0, sizeof(struct vhost_virtqueue));
373
374         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
375         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
376
377         vhost_user_iotlb_init(dev, vring_idx);
378         /* Backends are set to -1 indicating an inactive device. */
379         vq->backend = -1;
380
381         TAILQ_INIT(&vq->zmbuf_list);
382 }
383
384 static void
385 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
386 {
387         struct vhost_virtqueue *vq;
388         int callfd;
389
390         if (vring_idx >= VHOST_MAX_VRING) {
391                 RTE_LOG(ERR, VHOST_CONFIG,
392                                 "Failed not init vring, out of bound (%d)\n",
393                                 vring_idx);
394                 return;
395         }
396
397         vq = dev->virtqueue[vring_idx];
398         callfd = vq->callfd;
399         init_vring_queue(dev, vring_idx);
400         vq->callfd = callfd;
401 }
402
403 int
404 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
405 {
406         struct vhost_virtqueue *vq;
407
408         vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
409         if (vq == NULL) {
410                 RTE_LOG(ERR, VHOST_CONFIG,
411                         "Failed to allocate memory for vring:%u.\n", vring_idx);
412                 return -1;
413         }
414
415         dev->virtqueue[vring_idx] = vq;
416         init_vring_queue(dev, vring_idx);
417         rte_spinlock_init(&vq->access_lock);
418         vq->avail_wrap_counter = 1;
419         vq->used_wrap_counter = 1;
420         vq->signalled_used_valid = false;
421
422         dev->nr_vring += 1;
423
424         return 0;
425 }
426
427 /*
428  * Reset some variables in device structure, while keeping few
429  * others untouched, such as vid, ifname, nr_vring: they
430  * should be same unless the device is removed.
431  */
432 void
433 reset_device(struct virtio_net *dev)
434 {
435         uint32_t i;
436
437         dev->features = 0;
438         dev->protocol_features = 0;
439         dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
440
441         for (i = 0; i < dev->nr_vring; i++)
442                 reset_vring_queue(dev, i);
443 }
444
445 /*
446  * Invoked when there is a new vhost-user connection established (when
447  * there is a new virtio device being attached).
448  */
449 int
450 vhost_new_device(void)
451 {
452         struct virtio_net *dev;
453         int i;
454
455         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
456                 if (vhost_devices[i] == NULL)
457                         break;
458         }
459
460         if (i == MAX_VHOST_DEVICE) {
461                 RTE_LOG(ERR, VHOST_CONFIG,
462                         "Failed to find a free slot for new device.\n");
463                 return -1;
464         }
465
466         dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
467         if (dev == NULL) {
468                 RTE_LOG(ERR, VHOST_CONFIG,
469                         "Failed to allocate memory for new dev.\n");
470                 return -1;
471         }
472
473         vhost_devices[i] = dev;
474         dev->vid = i;
475         dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
476         dev->slave_req_fd = -1;
477         dev->vdpa_dev_id = -1;
478         dev->postcopy_ufd = -1;
479         rte_spinlock_init(&dev->slave_req_lock);
480
481         return i;
482 }
483
484 void
485 vhost_destroy_device_notify(struct virtio_net *dev)
486 {
487         struct rte_vdpa_device *vdpa_dev;
488         int did;
489
490         if (dev->flags & VIRTIO_DEV_RUNNING) {
491                 did = dev->vdpa_dev_id;
492                 vdpa_dev = rte_vdpa_get_device(did);
493                 if (vdpa_dev && vdpa_dev->ops->dev_close)
494                         vdpa_dev->ops->dev_close(dev->vid);
495                 dev->flags &= ~VIRTIO_DEV_RUNNING;
496                 dev->notify_ops->destroy_device(dev->vid);
497         }
498 }
499
500 /*
501  * Invoked when there is the vhost-user connection is broken (when
502  * the virtio device is being detached).
503  */
504 void
505 vhost_destroy_device(int vid)
506 {
507         struct virtio_net *dev = get_device(vid);
508
509         if (dev == NULL)
510                 return;
511
512         vhost_destroy_device_notify(dev);
513
514         cleanup_device(dev, 1);
515         free_device(dev);
516
517         vhost_devices[vid] = NULL;
518 }
519
520 void
521 vhost_attach_vdpa_device(int vid, int did)
522 {
523         struct virtio_net *dev = get_device(vid);
524
525         if (dev == NULL)
526                 return;
527
528         if (rte_vdpa_get_device(did) == NULL)
529                 return;
530
531         dev->vdpa_dev_id = did;
532 }
533
534 void
535 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
536 {
537         struct virtio_net *dev;
538         unsigned int len;
539
540         dev = get_device(vid);
541         if (dev == NULL)
542                 return;
543
544         len = if_len > sizeof(dev->ifname) ?
545                 sizeof(dev->ifname) : if_len;
546
547         strncpy(dev->ifname, if_name, len);
548         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
549 }
550
551 void
552 vhost_enable_dequeue_zero_copy(int vid)
553 {
554         struct virtio_net *dev = get_device(vid);
555
556         if (dev == NULL)
557                 return;
558
559         dev->dequeue_zero_copy = 1;
560 }
561
562 void
563 vhost_set_builtin_virtio_net(int vid, bool enable)
564 {
565         struct virtio_net *dev = get_device(vid);
566
567         if (dev == NULL)
568                 return;
569
570         if (enable)
571                 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
572         else
573                 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
574 }
575
576 int
577 rte_vhost_get_mtu(int vid, uint16_t *mtu)
578 {
579         struct virtio_net *dev = get_device(vid);
580
581         if (dev == NULL || mtu == NULL)
582                 return -ENODEV;
583
584         if (!(dev->flags & VIRTIO_DEV_READY))
585                 return -EAGAIN;
586
587         if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
588                 return -ENOTSUP;
589
590         *mtu = dev->mtu;
591
592         return 0;
593 }
594
595 int
596 rte_vhost_get_numa_node(int vid)
597 {
598 #ifdef RTE_LIBRTE_VHOST_NUMA
599         struct virtio_net *dev = get_device(vid);
600         int numa_node;
601         int ret;
602
603         if (dev == NULL || numa_available() != 0)
604                 return -1;
605
606         ret = get_mempolicy(&numa_node, NULL, 0, dev,
607                             MPOL_F_NODE | MPOL_F_ADDR);
608         if (ret < 0) {
609                 RTE_LOG(ERR, VHOST_CONFIG,
610                         "(%d) failed to query numa node: %s\n",
611                         vid, rte_strerror(errno));
612                 return -1;
613         }
614
615         return numa_node;
616 #else
617         RTE_SET_USED(vid);
618         return -1;
619 #endif
620 }
621
622 uint32_t
623 rte_vhost_get_queue_num(int vid)
624 {
625         struct virtio_net *dev = get_device(vid);
626
627         if (dev == NULL)
628                 return 0;
629
630         return dev->nr_vring / 2;
631 }
632
633 uint16_t
634 rte_vhost_get_vring_num(int vid)
635 {
636         struct virtio_net *dev = get_device(vid);
637
638         if (dev == NULL)
639                 return 0;
640
641         return dev->nr_vring;
642 }
643
644 int
645 rte_vhost_get_ifname(int vid, char *buf, size_t len)
646 {
647         struct virtio_net *dev = get_device(vid);
648
649         if (dev == NULL || buf == NULL)
650                 return -1;
651
652         len = RTE_MIN(len, sizeof(dev->ifname));
653
654         strncpy(buf, dev->ifname, len);
655         buf[len - 1] = '\0';
656
657         return 0;
658 }
659
660 int
661 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
662 {
663         struct virtio_net *dev;
664
665         dev = get_device(vid);
666         if (dev == NULL || features == NULL)
667                 return -1;
668
669         *features = dev->features;
670         return 0;
671 }
672
673 int
674 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
675 {
676         struct virtio_net *dev;
677         struct rte_vhost_memory *m;
678         size_t size;
679
680         dev = get_device(vid);
681         if (dev == NULL || mem == NULL)
682                 return -1;
683
684         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
685         m = malloc(sizeof(struct rte_vhost_memory) + size);
686         if (!m)
687                 return -1;
688
689         m->nregions = dev->mem->nregions;
690         memcpy(m->regions, dev->mem->regions, size);
691         *mem = m;
692
693         return 0;
694 }
695
696 int
697 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
698                           struct rte_vhost_vring *vring)
699 {
700         struct virtio_net *dev;
701         struct vhost_virtqueue *vq;
702
703         dev = get_device(vid);
704         if (dev == NULL || vring == NULL)
705                 return -1;
706
707         if (vring_idx >= VHOST_MAX_VRING)
708                 return -1;
709
710         vq = dev->virtqueue[vring_idx];
711         if (!vq)
712                 return -1;
713
714         vring->desc  = vq->desc;
715         vring->avail = vq->avail;
716         vring->used  = vq->used;
717         vring->log_guest_addr  = vq->log_guest_addr;
718
719         vring->callfd  = vq->callfd;
720         vring->kickfd  = vq->kickfd;
721         vring->size    = vq->size;
722
723         return 0;
724 }
725
726 int
727 rte_vhost_vring_call(int vid, uint16_t vring_idx)
728 {
729         struct virtio_net *dev;
730         struct vhost_virtqueue *vq;
731
732         dev = get_device(vid);
733         if (!dev)
734                 return -1;
735
736         if (vring_idx >= VHOST_MAX_VRING)
737                 return -1;
738
739         vq = dev->virtqueue[vring_idx];
740         if (!vq)
741                 return -1;
742
743         if (vq_is_packed(dev))
744                 vhost_vring_call_packed(dev, vq);
745         else
746                 vhost_vring_call_split(dev, vq);
747
748         return 0;
749 }
750
751 uint16_t
752 rte_vhost_avail_entries(int vid, uint16_t queue_id)
753 {
754         struct virtio_net *dev;
755         struct vhost_virtqueue *vq;
756
757         dev = get_device(vid);
758         if (!dev)
759                 return 0;
760
761         vq = dev->virtqueue[queue_id];
762         if (!vq->enabled)
763                 return 0;
764
765         return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
766 }
767
768 static inline void
769 vhost_enable_notify_split(struct virtio_net *dev,
770                 struct vhost_virtqueue *vq, int enable)
771 {
772         if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
773                 if (enable)
774                         vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
775                 else
776                         vq->used->flags |= VRING_USED_F_NO_NOTIFY;
777         } else {
778                 if (enable)
779                         vhost_avail_event(vq) = vq->last_avail_idx;
780         }
781 }
782
783 static inline void
784 vhost_enable_notify_packed(struct virtio_net *dev,
785                 struct vhost_virtqueue *vq, int enable)
786 {
787         uint16_t flags;
788
789         if (!enable) {
790                 vq->device_event->flags = VRING_EVENT_F_DISABLE;
791                 return;
792         }
793
794         flags = VRING_EVENT_F_ENABLE;
795         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
796                 flags = VRING_EVENT_F_DESC;
797                 vq->device_event->off_wrap = vq->last_avail_idx |
798                         vq->avail_wrap_counter << 15;
799         }
800
801         rte_smp_wmb();
802
803         vq->device_event->flags = flags;
804 }
805
806 int
807 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
808 {
809         struct virtio_net *dev = get_device(vid);
810         struct vhost_virtqueue *vq;
811
812         if (!dev)
813                 return -1;
814
815         vq = dev->virtqueue[queue_id];
816
817         if (vq_is_packed(dev))
818                 vhost_enable_notify_packed(dev, vq, enable);
819         else
820                 vhost_enable_notify_split(dev, vq, enable);
821
822         return 0;
823 }
824
825 void
826 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
827 {
828         struct virtio_net *dev = get_device(vid);
829
830         if (dev == NULL)
831                 return;
832
833         vhost_log_write(dev, addr, len);
834 }
835
836 void
837 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
838                          uint64_t offset, uint64_t len)
839 {
840         struct virtio_net *dev;
841         struct vhost_virtqueue *vq;
842
843         dev = get_device(vid);
844         if (dev == NULL)
845                 return;
846
847         if (vring_idx >= VHOST_MAX_VRING)
848                 return;
849         vq = dev->virtqueue[vring_idx];
850         if (!vq)
851                 return;
852
853         vhost_log_used_vring(dev, vq, offset, len);
854 }
855
856 uint32_t
857 rte_vhost_rx_queue_count(int vid, uint16_t qid)
858 {
859         struct virtio_net *dev;
860         struct vhost_virtqueue *vq;
861
862         dev = get_device(vid);
863         if (dev == NULL)
864                 return 0;
865
866         if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
867                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
868                         dev->vid, __func__, qid);
869                 return 0;
870         }
871
872         vq = dev->virtqueue[qid];
873         if (vq == NULL)
874                 return 0;
875
876         if (unlikely(vq->enabled == 0 || vq->avail == NULL))
877                 return 0;
878
879         return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
880 }
881
882 int rte_vhost_get_vdpa_device_id(int vid)
883 {
884         struct virtio_net *dev = get_device(vid);
885
886         if (dev == NULL)
887                 return -1;
888
889         return dev->vdpa_dev_id;
890 }
891
892 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
893                 uint64_t *log_size)
894 {
895         struct virtio_net *dev = get_device(vid);
896
897         if (dev == NULL || log_base == NULL || log_size == NULL)
898                 return -1;
899
900         *log_base = dev->log_base;
901         *log_size = dev->log_size;
902
903         return 0;
904 }
905
906 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
907                 uint16_t *last_avail_idx, uint16_t *last_used_idx)
908 {
909         struct virtio_net *dev = get_device(vid);
910
911         if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
912                 return -1;
913
914         *last_avail_idx = dev->virtqueue[queue_id]->last_avail_idx;
915         *last_used_idx = dev->virtqueue[queue_id]->last_used_idx;
916
917         return 0;
918 }
919
920 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
921                 uint16_t last_avail_idx, uint16_t last_used_idx)
922 {
923         struct virtio_net *dev = get_device(vid);
924
925         if (!dev)
926                 return -1;
927
928         dev->virtqueue[queue_id]->last_avail_idx = last_avail_idx;
929         dev->virtqueue[queue_id]->last_used_idx = last_used_idx;
930
931         return 0;
932 }
933
934 int rte_vhost_extern_callback_register(int vid,
935                 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
936 {
937         struct virtio_net *dev = get_device(vid);
938
939         if (dev == NULL || ops == NULL)
940                 return -1;
941
942         dev->extern_ops = *ops;
943         dev->extern_data = ctx;
944         return 0;
945 }