vhost: do not inline unlikely fragmented buffers code
[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 vhost_alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
205                 uint64_t desc_addr, uint64_t desc_len)
206 {
207         void *idesc;
208         uint64_t src, dst;
209         uint64_t len, remain = desc_len;
210
211         idesc = rte_malloc(__func__, desc_len, 0);
212         if (unlikely(!idesc))
213                 return NULL;
214
215         dst = (uint64_t)(uintptr_t)idesc;
216
217         while (remain) {
218                 len = remain;
219                 src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
220                                 VHOST_ACCESS_RO);
221                 if (unlikely(!src || !len)) {
222                         rte_free(idesc);
223                         return NULL;
224                 }
225
226                 rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
227
228                 remain -= len;
229                 dst += len;
230                 desc_addr += len;
231         }
232
233         return idesc;
234 }
235
236 void
237 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
238 {
239         if ((vq->callfd >= 0) && (destroy != 0))
240                 close(vq->callfd);
241         if (vq->kickfd >= 0)
242                 close(vq->kickfd);
243 }
244
245 /*
246  * Unmap any memory, close any file descriptors and
247  * free any memory owned by a device.
248  */
249 void
250 cleanup_device(struct virtio_net *dev, int destroy)
251 {
252         uint32_t i;
253
254         vhost_backend_cleanup(dev);
255
256         for (i = 0; i < dev->nr_vring; i++)
257                 cleanup_vq(dev->virtqueue[i], destroy);
258 }
259
260 void
261 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
262 {
263         if (vq_is_packed(dev))
264                 rte_free(vq->shadow_used_packed);
265         else
266                 rte_free(vq->shadow_used_split);
267         rte_free(vq->batch_copy_elems);
268         rte_mempool_free(vq->iotlb_pool);
269         rte_free(vq);
270 }
271
272 /*
273  * Release virtqueues and device memory.
274  */
275 static void
276 free_device(struct virtio_net *dev)
277 {
278         uint32_t i;
279
280         for (i = 0; i < dev->nr_vring; i++)
281                 free_vq(dev, dev->virtqueue[i]);
282
283         rte_free(dev);
284 }
285
286 static int
287 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
288 {
289         uint64_t req_size, size;
290
291         req_size = sizeof(struct vring_desc) * vq->size;
292         size = req_size;
293         vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
294                                                 vq->ring_addrs.desc_user_addr,
295                                                 &size, VHOST_ACCESS_RW);
296         if (!vq->desc || size != req_size)
297                 return -1;
298
299         req_size = sizeof(struct vring_avail);
300         req_size += sizeof(uint16_t) * vq->size;
301         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
302                 req_size += sizeof(uint16_t);
303         size = req_size;
304         vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
305                                                 vq->ring_addrs.avail_user_addr,
306                                                 &size, VHOST_ACCESS_RW);
307         if (!vq->avail || size != req_size)
308                 return -1;
309
310         req_size = sizeof(struct vring_used);
311         req_size += sizeof(struct vring_used_elem) * vq->size;
312         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
313                 req_size += sizeof(uint16_t);
314         size = req_size;
315         vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
316                                                 vq->ring_addrs.used_user_addr,
317                                                 &size, VHOST_ACCESS_RW);
318         if (!vq->used || size != req_size)
319                 return -1;
320
321         return 0;
322 }
323
324 static int
325 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
326 {
327         uint64_t req_size, size;
328
329         req_size = sizeof(struct vring_packed_desc) * vq->size;
330         size = req_size;
331         vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
332                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
333                                 &size, VHOST_ACCESS_RW);
334         if (!vq->desc_packed || size != req_size)
335                 return -1;
336
337         req_size = sizeof(struct vring_packed_desc_event);
338         size = req_size;
339         vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
340                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
341                                 &size, VHOST_ACCESS_RW);
342         if (!vq->driver_event || size != req_size)
343                 return -1;
344
345         req_size = sizeof(struct vring_packed_desc_event);
346         size = req_size;
347         vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
348                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
349                                 &size, VHOST_ACCESS_RW);
350         if (!vq->device_event || size != req_size)
351                 return -1;
352
353         return 0;
354 }
355
356 int
357 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
358 {
359
360         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
361                 goto out;
362
363         if (vq_is_packed(dev)) {
364                 if (vring_translate_packed(dev, vq) < 0)
365                         return -1;
366         } else {
367                 if (vring_translate_split(dev, vq) < 0)
368                         return -1;
369         }
370 out:
371         vq->access_ok = 1;
372
373         return 0;
374 }
375
376 void
377 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
378 {
379         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
380                 vhost_user_iotlb_wr_lock(vq);
381
382         vq->access_ok = 0;
383         vq->desc = NULL;
384         vq->avail = NULL;
385         vq->used = NULL;
386
387         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
388                 vhost_user_iotlb_wr_unlock(vq);
389 }
390
391 static void
392 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
393 {
394         struct vhost_virtqueue *vq;
395
396         if (vring_idx >= VHOST_MAX_VRING) {
397                 RTE_LOG(ERR, VHOST_CONFIG,
398                                 "Failed not init vring, out of bound (%d)\n",
399                                 vring_idx);
400                 return;
401         }
402
403         vq = dev->virtqueue[vring_idx];
404
405         memset(vq, 0, sizeof(struct vhost_virtqueue));
406
407         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
408         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
409
410         vhost_user_iotlb_init(dev, vring_idx);
411         /* Backends are set to -1 indicating an inactive device. */
412         vq->backend = -1;
413
414         TAILQ_INIT(&vq->zmbuf_list);
415 }
416
417 static void
418 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
419 {
420         struct vhost_virtqueue *vq;
421         int callfd;
422
423         if (vring_idx >= VHOST_MAX_VRING) {
424                 RTE_LOG(ERR, VHOST_CONFIG,
425                                 "Failed not init vring, out of bound (%d)\n",
426                                 vring_idx);
427                 return;
428         }
429
430         vq = dev->virtqueue[vring_idx];
431         callfd = vq->callfd;
432         init_vring_queue(dev, vring_idx);
433         vq->callfd = callfd;
434 }
435
436 int
437 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
438 {
439         struct vhost_virtqueue *vq;
440
441         vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
442         if (vq == NULL) {
443                 RTE_LOG(ERR, VHOST_CONFIG,
444                         "Failed to allocate memory for vring:%u.\n", vring_idx);
445                 return -1;
446         }
447
448         dev->virtqueue[vring_idx] = vq;
449         init_vring_queue(dev, vring_idx);
450         rte_spinlock_init(&vq->access_lock);
451         vq->avail_wrap_counter = 1;
452         vq->used_wrap_counter = 1;
453         vq->signalled_used_valid = false;
454
455         dev->nr_vring += 1;
456
457         return 0;
458 }
459
460 /*
461  * Reset some variables in device structure, while keeping few
462  * others untouched, such as vid, ifname, nr_vring: they
463  * should be same unless the device is removed.
464  */
465 void
466 reset_device(struct virtio_net *dev)
467 {
468         uint32_t i;
469
470         dev->features = 0;
471         dev->protocol_features = 0;
472         dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
473
474         for (i = 0; i < dev->nr_vring; i++)
475                 reset_vring_queue(dev, i);
476 }
477
478 /*
479  * Invoked when there is a new vhost-user connection established (when
480  * there is a new virtio device being attached).
481  */
482 int
483 vhost_new_device(void)
484 {
485         struct virtio_net *dev;
486         int i;
487
488         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
489                 if (vhost_devices[i] == NULL)
490                         break;
491         }
492
493         if (i == MAX_VHOST_DEVICE) {
494                 RTE_LOG(ERR, VHOST_CONFIG,
495                         "Failed to find a free slot for new device.\n");
496                 return -1;
497         }
498
499         dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
500         if (dev == NULL) {
501                 RTE_LOG(ERR, VHOST_CONFIG,
502                         "Failed to allocate memory for new dev.\n");
503                 return -1;
504         }
505
506         vhost_devices[i] = dev;
507         dev->vid = i;
508         dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
509         dev->slave_req_fd = -1;
510         dev->vdpa_dev_id = -1;
511         dev->postcopy_ufd = -1;
512         rte_spinlock_init(&dev->slave_req_lock);
513
514         return i;
515 }
516
517 void
518 vhost_destroy_device_notify(struct virtio_net *dev)
519 {
520         struct rte_vdpa_device *vdpa_dev;
521         int did;
522
523         if (dev->flags & VIRTIO_DEV_RUNNING) {
524                 did = dev->vdpa_dev_id;
525                 vdpa_dev = rte_vdpa_get_device(did);
526                 if (vdpa_dev && vdpa_dev->ops->dev_close)
527                         vdpa_dev->ops->dev_close(dev->vid);
528                 dev->flags &= ~VIRTIO_DEV_RUNNING;
529                 dev->notify_ops->destroy_device(dev->vid);
530         }
531 }
532
533 /*
534  * Invoked when there is the vhost-user connection is broken (when
535  * the virtio device is being detached).
536  */
537 void
538 vhost_destroy_device(int vid)
539 {
540         struct virtio_net *dev = get_device(vid);
541
542         if (dev == NULL)
543                 return;
544
545         vhost_destroy_device_notify(dev);
546
547         cleanup_device(dev, 1);
548         free_device(dev);
549
550         vhost_devices[vid] = NULL;
551 }
552
553 void
554 vhost_attach_vdpa_device(int vid, int did)
555 {
556         struct virtio_net *dev = get_device(vid);
557
558         if (dev == NULL)
559                 return;
560
561         if (rte_vdpa_get_device(did) == NULL)
562                 return;
563
564         dev->vdpa_dev_id = did;
565 }
566
567 void
568 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
569 {
570         struct virtio_net *dev;
571         unsigned int len;
572
573         dev = get_device(vid);
574         if (dev == NULL)
575                 return;
576
577         len = if_len > sizeof(dev->ifname) ?
578                 sizeof(dev->ifname) : if_len;
579
580         strncpy(dev->ifname, if_name, len);
581         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
582 }
583
584 void
585 vhost_enable_dequeue_zero_copy(int vid)
586 {
587         struct virtio_net *dev = get_device(vid);
588
589         if (dev == NULL)
590                 return;
591
592         dev->dequeue_zero_copy = 1;
593 }
594
595 void
596 vhost_set_builtin_virtio_net(int vid, bool enable)
597 {
598         struct virtio_net *dev = get_device(vid);
599
600         if (dev == NULL)
601                 return;
602
603         if (enable)
604                 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
605         else
606                 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
607 }
608
609 int
610 rte_vhost_get_mtu(int vid, uint16_t *mtu)
611 {
612         struct virtio_net *dev = get_device(vid);
613
614         if (dev == NULL || mtu == NULL)
615                 return -ENODEV;
616
617         if (!(dev->flags & VIRTIO_DEV_READY))
618                 return -EAGAIN;
619
620         if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
621                 return -ENOTSUP;
622
623         *mtu = dev->mtu;
624
625         return 0;
626 }
627
628 int
629 rte_vhost_get_numa_node(int vid)
630 {
631 #ifdef RTE_LIBRTE_VHOST_NUMA
632         struct virtio_net *dev = get_device(vid);
633         int numa_node;
634         int ret;
635
636         if (dev == NULL || numa_available() != 0)
637                 return -1;
638
639         ret = get_mempolicy(&numa_node, NULL, 0, dev,
640                             MPOL_F_NODE | MPOL_F_ADDR);
641         if (ret < 0) {
642                 RTE_LOG(ERR, VHOST_CONFIG,
643                         "(%d) failed to query numa node: %s\n",
644                         vid, rte_strerror(errno));
645                 return -1;
646         }
647
648         return numa_node;
649 #else
650         RTE_SET_USED(vid);
651         return -1;
652 #endif
653 }
654
655 uint32_t
656 rte_vhost_get_queue_num(int vid)
657 {
658         struct virtio_net *dev = get_device(vid);
659
660         if (dev == NULL)
661                 return 0;
662
663         return dev->nr_vring / 2;
664 }
665
666 uint16_t
667 rte_vhost_get_vring_num(int vid)
668 {
669         struct virtio_net *dev = get_device(vid);
670
671         if (dev == NULL)
672                 return 0;
673
674         return dev->nr_vring;
675 }
676
677 int
678 rte_vhost_get_ifname(int vid, char *buf, size_t len)
679 {
680         struct virtio_net *dev = get_device(vid);
681
682         if (dev == NULL || buf == NULL)
683                 return -1;
684
685         len = RTE_MIN(len, sizeof(dev->ifname));
686
687         strncpy(buf, dev->ifname, len);
688         buf[len - 1] = '\0';
689
690         return 0;
691 }
692
693 int
694 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
695 {
696         struct virtio_net *dev;
697
698         dev = get_device(vid);
699         if (dev == NULL || features == NULL)
700                 return -1;
701
702         *features = dev->features;
703         return 0;
704 }
705
706 int
707 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
708 {
709         struct virtio_net *dev;
710         struct rte_vhost_memory *m;
711         size_t size;
712
713         dev = get_device(vid);
714         if (dev == NULL || mem == NULL)
715                 return -1;
716
717         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
718         m = malloc(sizeof(struct rte_vhost_memory) + size);
719         if (!m)
720                 return -1;
721
722         m->nregions = dev->mem->nregions;
723         memcpy(m->regions, dev->mem->regions, size);
724         *mem = m;
725
726         return 0;
727 }
728
729 int
730 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
731                           struct rte_vhost_vring *vring)
732 {
733         struct virtio_net *dev;
734         struct vhost_virtqueue *vq;
735
736         dev = get_device(vid);
737         if (dev == NULL || vring == NULL)
738                 return -1;
739
740         if (vring_idx >= VHOST_MAX_VRING)
741                 return -1;
742
743         vq = dev->virtqueue[vring_idx];
744         if (!vq)
745                 return -1;
746
747         vring->desc  = vq->desc;
748         vring->avail = vq->avail;
749         vring->used  = vq->used;
750         vring->log_guest_addr  = vq->log_guest_addr;
751
752         vring->callfd  = vq->callfd;
753         vring->kickfd  = vq->kickfd;
754         vring->size    = vq->size;
755
756         return 0;
757 }
758
759 int
760 rte_vhost_vring_call(int vid, uint16_t vring_idx)
761 {
762         struct virtio_net *dev;
763         struct vhost_virtqueue *vq;
764
765         dev = get_device(vid);
766         if (!dev)
767                 return -1;
768
769         if (vring_idx >= VHOST_MAX_VRING)
770                 return -1;
771
772         vq = dev->virtqueue[vring_idx];
773         if (!vq)
774                 return -1;
775
776         if (vq_is_packed(dev))
777                 vhost_vring_call_packed(dev, vq);
778         else
779                 vhost_vring_call_split(dev, vq);
780
781         return 0;
782 }
783
784 uint16_t
785 rte_vhost_avail_entries(int vid, uint16_t queue_id)
786 {
787         struct virtio_net *dev;
788         struct vhost_virtqueue *vq;
789
790         dev = get_device(vid);
791         if (!dev)
792                 return 0;
793
794         vq = dev->virtqueue[queue_id];
795         if (!vq->enabled)
796                 return 0;
797
798         return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
799 }
800
801 static inline void
802 vhost_enable_notify_split(struct virtio_net *dev,
803                 struct vhost_virtqueue *vq, int enable)
804 {
805         if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
806                 if (enable)
807                         vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
808                 else
809                         vq->used->flags |= VRING_USED_F_NO_NOTIFY;
810         } else {
811                 if (enable)
812                         vhost_avail_event(vq) = vq->last_avail_idx;
813         }
814 }
815
816 static inline void
817 vhost_enable_notify_packed(struct virtio_net *dev,
818                 struct vhost_virtqueue *vq, int enable)
819 {
820         uint16_t flags;
821
822         if (!enable) {
823                 vq->device_event->flags = VRING_EVENT_F_DISABLE;
824                 return;
825         }
826
827         flags = VRING_EVENT_F_ENABLE;
828         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
829                 flags = VRING_EVENT_F_DESC;
830                 vq->device_event->off_wrap = vq->last_avail_idx |
831                         vq->avail_wrap_counter << 15;
832         }
833
834         rte_smp_wmb();
835
836         vq->device_event->flags = flags;
837 }
838
839 int
840 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
841 {
842         struct virtio_net *dev = get_device(vid);
843         struct vhost_virtqueue *vq;
844
845         if (!dev)
846                 return -1;
847
848         vq = dev->virtqueue[queue_id];
849
850         if (vq_is_packed(dev))
851                 vhost_enable_notify_packed(dev, vq, enable);
852         else
853                 vhost_enable_notify_split(dev, vq, enable);
854
855         return 0;
856 }
857
858 void
859 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
860 {
861         struct virtio_net *dev = get_device(vid);
862
863         if (dev == NULL)
864                 return;
865
866         vhost_log_write(dev, addr, len);
867 }
868
869 void
870 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
871                          uint64_t offset, uint64_t len)
872 {
873         struct virtio_net *dev;
874         struct vhost_virtqueue *vq;
875
876         dev = get_device(vid);
877         if (dev == NULL)
878                 return;
879
880         if (vring_idx >= VHOST_MAX_VRING)
881                 return;
882         vq = dev->virtqueue[vring_idx];
883         if (!vq)
884                 return;
885
886         vhost_log_used_vring(dev, vq, offset, len);
887 }
888
889 uint32_t
890 rte_vhost_rx_queue_count(int vid, uint16_t qid)
891 {
892         struct virtio_net *dev;
893         struct vhost_virtqueue *vq;
894
895         dev = get_device(vid);
896         if (dev == NULL)
897                 return 0;
898
899         if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
900                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
901                         dev->vid, __func__, qid);
902                 return 0;
903         }
904
905         vq = dev->virtqueue[qid];
906         if (vq == NULL)
907                 return 0;
908
909         if (unlikely(vq->enabled == 0 || vq->avail == NULL))
910                 return 0;
911
912         return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
913 }
914
915 int rte_vhost_get_vdpa_device_id(int vid)
916 {
917         struct virtio_net *dev = get_device(vid);
918
919         if (dev == NULL)
920                 return -1;
921
922         return dev->vdpa_dev_id;
923 }
924
925 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
926                 uint64_t *log_size)
927 {
928         struct virtio_net *dev = get_device(vid);
929
930         if (dev == NULL || log_base == NULL || log_size == NULL)
931                 return -1;
932
933         *log_base = dev->log_base;
934         *log_size = dev->log_size;
935
936         return 0;
937 }
938
939 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
940                 uint16_t *last_avail_idx, uint16_t *last_used_idx)
941 {
942         struct virtio_net *dev = get_device(vid);
943
944         if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
945                 return -1;
946
947         *last_avail_idx = dev->virtqueue[queue_id]->last_avail_idx;
948         *last_used_idx = dev->virtqueue[queue_id]->last_used_idx;
949
950         return 0;
951 }
952
953 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
954                 uint16_t last_avail_idx, uint16_t last_used_idx)
955 {
956         struct virtio_net *dev = get_device(vid);
957
958         if (!dev)
959                 return -1;
960
961         dev->virtqueue[queue_id]->last_avail_idx = last_avail_idx;
962         dev->virtqueue[queue_id]->last_used_idx = last_used_idx;
963
964         return 0;
965 }
966
967 int rte_vhost_extern_callback_register(int vid,
968                 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
969 {
970         struct virtio_net *dev = get_device(vid);
971
972         if (dev == NULL || ops == NULL)
973                 return -1;
974
975         dev->extern_ops = *ops;
976         dev->extern_data = ctx;
977         return 0;
978 }