vhost: protect vring access done by application
[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                 return -1;
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         vq->access_ok = 1;
371
372         return 0;
373 }
374
375 void
376 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
377 {
378         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
379                 vhost_user_iotlb_wr_lock(vq);
380
381         vq->access_ok = 0;
382         vq->desc = NULL;
383         vq->avail = NULL;
384         vq->used = NULL;
385
386         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
387                 vhost_user_iotlb_wr_unlock(vq);
388 }
389
390 static void
391 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
392 {
393         struct vhost_virtqueue *vq;
394
395         if (vring_idx >= VHOST_MAX_VRING) {
396                 RTE_LOG(ERR, VHOST_CONFIG,
397                                 "Failed not init vring, out of bound (%d)\n",
398                                 vring_idx);
399                 return;
400         }
401
402         vq = dev->virtqueue[vring_idx];
403
404         memset(vq, 0, sizeof(struct vhost_virtqueue));
405
406         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
407         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
408
409         vhost_user_iotlb_init(dev, vring_idx);
410         /* Backends are set to -1 indicating an inactive device. */
411         vq->backend = -1;
412
413         TAILQ_INIT(&vq->zmbuf_list);
414 }
415
416 static void
417 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
418 {
419         struct vhost_virtqueue *vq;
420         int callfd;
421
422         if (vring_idx >= VHOST_MAX_VRING) {
423                 RTE_LOG(ERR, VHOST_CONFIG,
424                                 "Failed not init vring, out of bound (%d)\n",
425                                 vring_idx);
426                 return;
427         }
428
429         vq = dev->virtqueue[vring_idx];
430         callfd = vq->callfd;
431         init_vring_queue(dev, vring_idx);
432         vq->callfd = callfd;
433 }
434
435 int
436 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
437 {
438         struct vhost_virtqueue *vq;
439
440         vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
441         if (vq == NULL) {
442                 RTE_LOG(ERR, VHOST_CONFIG,
443                         "Failed to allocate memory for vring:%u.\n", vring_idx);
444                 return -1;
445         }
446
447         dev->virtqueue[vring_idx] = vq;
448         init_vring_queue(dev, vring_idx);
449         rte_spinlock_init(&vq->access_lock);
450         vq->avail_wrap_counter = 1;
451         vq->used_wrap_counter = 1;
452         vq->signalled_used_valid = false;
453
454         dev->nr_vring += 1;
455
456         return 0;
457 }
458
459 /*
460  * Reset some variables in device structure, while keeping few
461  * others untouched, such as vid, ifname, nr_vring: they
462  * should be same unless the device is removed.
463  */
464 void
465 reset_device(struct virtio_net *dev)
466 {
467         uint32_t i;
468
469         dev->features = 0;
470         dev->protocol_features = 0;
471         dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
472
473         for (i = 0; i < dev->nr_vring; i++)
474                 reset_vring_queue(dev, i);
475 }
476
477 /*
478  * Invoked when there is a new vhost-user connection established (when
479  * there is a new virtio device being attached).
480  */
481 int
482 vhost_new_device(void)
483 {
484         struct virtio_net *dev;
485         int i;
486
487         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
488                 if (vhost_devices[i] == NULL)
489                         break;
490         }
491
492         if (i == MAX_VHOST_DEVICE) {
493                 RTE_LOG(ERR, VHOST_CONFIG,
494                         "Failed to find a free slot for new device.\n");
495                 return -1;
496         }
497
498         dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
499         if (dev == NULL) {
500                 RTE_LOG(ERR, VHOST_CONFIG,
501                         "Failed to allocate memory for new dev.\n");
502                 return -1;
503         }
504
505         vhost_devices[i] = dev;
506         dev->vid = i;
507         dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
508         dev->slave_req_fd = -1;
509         dev->vdpa_dev_id = -1;
510         dev->postcopy_ufd = -1;
511         rte_spinlock_init(&dev->slave_req_lock);
512
513         return i;
514 }
515
516 void
517 vhost_destroy_device_notify(struct virtio_net *dev)
518 {
519         struct rte_vdpa_device *vdpa_dev;
520         int did;
521
522         if (dev->flags & VIRTIO_DEV_RUNNING) {
523                 did = dev->vdpa_dev_id;
524                 vdpa_dev = rte_vdpa_get_device(did);
525                 if (vdpa_dev && vdpa_dev->ops->dev_close)
526                         vdpa_dev->ops->dev_close(dev->vid);
527                 dev->flags &= ~VIRTIO_DEV_RUNNING;
528                 dev->notify_ops->destroy_device(dev->vid);
529         }
530 }
531
532 /*
533  * Invoked when there is the vhost-user connection is broken (when
534  * the virtio device is being detached).
535  */
536 void
537 vhost_destroy_device(int vid)
538 {
539         struct virtio_net *dev = get_device(vid);
540
541         if (dev == NULL)
542                 return;
543
544         vhost_destroy_device_notify(dev);
545
546         cleanup_device(dev, 1);
547         free_device(dev);
548
549         vhost_devices[vid] = NULL;
550 }
551
552 void
553 vhost_attach_vdpa_device(int vid, int did)
554 {
555         struct virtio_net *dev = get_device(vid);
556
557         if (dev == NULL)
558                 return;
559
560         if (rte_vdpa_get_device(did) == NULL)
561                 return;
562
563         dev->vdpa_dev_id = did;
564 }
565
566 void
567 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
568 {
569         struct virtio_net *dev;
570         unsigned int len;
571
572         dev = get_device(vid);
573         if (dev == NULL)
574                 return;
575
576         len = if_len > sizeof(dev->ifname) ?
577                 sizeof(dev->ifname) : if_len;
578
579         strncpy(dev->ifname, if_name, len);
580         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
581 }
582
583 void
584 vhost_enable_dequeue_zero_copy(int vid)
585 {
586         struct virtio_net *dev = get_device(vid);
587
588         if (dev == NULL)
589                 return;
590
591         dev->dequeue_zero_copy = 1;
592 }
593
594 void
595 vhost_set_builtin_virtio_net(int vid, bool enable)
596 {
597         struct virtio_net *dev = get_device(vid);
598
599         if (dev == NULL)
600                 return;
601
602         if (enable)
603                 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
604         else
605                 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
606 }
607
608 int
609 rte_vhost_get_mtu(int vid, uint16_t *mtu)
610 {
611         struct virtio_net *dev = get_device(vid);
612
613         if (dev == NULL || mtu == NULL)
614                 return -ENODEV;
615
616         if (!(dev->flags & VIRTIO_DEV_READY))
617                 return -EAGAIN;
618
619         if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
620                 return -ENOTSUP;
621
622         *mtu = dev->mtu;
623
624         return 0;
625 }
626
627 int
628 rte_vhost_get_numa_node(int vid)
629 {
630 #ifdef RTE_LIBRTE_VHOST_NUMA
631         struct virtio_net *dev = get_device(vid);
632         int numa_node;
633         int ret;
634
635         if (dev == NULL || numa_available() != 0)
636                 return -1;
637
638         ret = get_mempolicy(&numa_node, NULL, 0, dev,
639                             MPOL_F_NODE | MPOL_F_ADDR);
640         if (ret < 0) {
641                 RTE_LOG(ERR, VHOST_CONFIG,
642                         "(%d) failed to query numa node: %s\n",
643                         vid, rte_strerror(errno));
644                 return -1;
645         }
646
647         return numa_node;
648 #else
649         RTE_SET_USED(vid);
650         return -1;
651 #endif
652 }
653
654 uint32_t
655 rte_vhost_get_queue_num(int vid)
656 {
657         struct virtio_net *dev = get_device(vid);
658
659         if (dev == NULL)
660                 return 0;
661
662         return dev->nr_vring / 2;
663 }
664
665 uint16_t
666 rte_vhost_get_vring_num(int vid)
667 {
668         struct virtio_net *dev = get_device(vid);
669
670         if (dev == NULL)
671                 return 0;
672
673         return dev->nr_vring;
674 }
675
676 int
677 rte_vhost_get_ifname(int vid, char *buf, size_t len)
678 {
679         struct virtio_net *dev = get_device(vid);
680
681         if (dev == NULL || buf == NULL)
682                 return -1;
683
684         len = RTE_MIN(len, sizeof(dev->ifname));
685
686         strncpy(buf, dev->ifname, len);
687         buf[len - 1] = '\0';
688
689         return 0;
690 }
691
692 int
693 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
694 {
695         struct virtio_net *dev;
696
697         dev = get_device(vid);
698         if (dev == NULL || features == NULL)
699                 return -1;
700
701         *features = dev->features;
702         return 0;
703 }
704
705 int
706 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
707 {
708         struct virtio_net *dev;
709         struct rte_vhost_memory *m;
710         size_t size;
711
712         dev = get_device(vid);
713         if (dev == NULL || mem == NULL)
714                 return -1;
715
716         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
717         m = malloc(sizeof(struct rte_vhost_memory) + size);
718         if (!m)
719                 return -1;
720
721         m->nregions = dev->mem->nregions;
722         memcpy(m->regions, dev->mem->regions, size);
723         *mem = m;
724
725         return 0;
726 }
727
728 int
729 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
730                           struct rte_vhost_vring *vring)
731 {
732         struct virtio_net *dev;
733         struct vhost_virtqueue *vq;
734
735         dev = get_device(vid);
736         if (dev == NULL || vring == NULL)
737                 return -1;
738
739         if (vring_idx >= VHOST_MAX_VRING)
740                 return -1;
741
742         vq = dev->virtqueue[vring_idx];
743         if (!vq)
744                 return -1;
745
746         vring->desc  = vq->desc;
747         vring->avail = vq->avail;
748         vring->used  = vq->used;
749         vring->log_guest_addr  = vq->log_guest_addr;
750
751         vring->callfd  = vq->callfd;
752         vring->kickfd  = vq->kickfd;
753         vring->size    = vq->size;
754
755         return 0;
756 }
757
758 int
759 rte_vhost_vring_call(int vid, uint16_t vring_idx)
760 {
761         struct virtio_net *dev;
762         struct vhost_virtqueue *vq;
763
764         dev = get_device(vid);
765         if (!dev)
766                 return -1;
767
768         if (vring_idx >= VHOST_MAX_VRING)
769                 return -1;
770
771         vq = dev->virtqueue[vring_idx];
772         if (!vq)
773                 return -1;
774
775         if (vq_is_packed(dev))
776                 vhost_vring_call_packed(dev, vq);
777         else
778                 vhost_vring_call_split(dev, vq);
779
780         return 0;
781 }
782
783 uint16_t
784 rte_vhost_avail_entries(int vid, uint16_t queue_id)
785 {
786         struct virtio_net *dev;
787         struct vhost_virtqueue *vq;
788         uint16_t ret = 0;
789
790         dev = get_device(vid);
791         if (!dev)
792                 return 0;
793
794         vq = dev->virtqueue[queue_id];
795
796         rte_spinlock_lock(&vq->access_lock);
797
798         if (unlikely(!vq->enabled || vq->avail == NULL))
799                 goto out;
800
801         ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
802
803 out:
804         rte_spinlock_unlock(&vq->access_lock);
805         return ret;
806 }
807
808 static inline int
809 vhost_enable_notify_split(struct virtio_net *dev,
810                 struct vhost_virtqueue *vq, int enable)
811 {
812         if (vq->used == NULL)
813                 return -1;
814
815         if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
816                 if (enable)
817                         vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
818                 else
819                         vq->used->flags |= VRING_USED_F_NO_NOTIFY;
820         } else {
821                 if (enable)
822                         vhost_avail_event(vq) = vq->last_avail_idx;
823         }
824         return 0;
825 }
826
827 static inline int
828 vhost_enable_notify_packed(struct virtio_net *dev,
829                 struct vhost_virtqueue *vq, int enable)
830 {
831         uint16_t flags;
832
833         if (vq->device_event == NULL)
834                 return -1;
835
836         if (!enable) {
837                 vq->device_event->flags = VRING_EVENT_F_DISABLE;
838                 return 0;
839         }
840
841         flags = VRING_EVENT_F_ENABLE;
842         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
843                 flags = VRING_EVENT_F_DESC;
844                 vq->device_event->off_wrap = vq->last_avail_idx |
845                         vq->avail_wrap_counter << 15;
846         }
847
848         rte_smp_wmb();
849
850         vq->device_event->flags = flags;
851         return 0;
852 }
853
854 int
855 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
856 {
857         struct virtio_net *dev = get_device(vid);
858         struct vhost_virtqueue *vq;
859         int ret;
860
861         if (!dev)
862                 return -1;
863
864         vq = dev->virtqueue[queue_id];
865
866         rte_spinlock_lock(&vq->access_lock);
867
868         if (vq_is_packed(dev))
869                 ret = vhost_enable_notify_packed(dev, vq, enable);
870         else
871                 ret = vhost_enable_notify_split(dev, vq, enable);
872
873         rte_spinlock_unlock(&vq->access_lock);
874
875         return ret;
876 }
877
878 void
879 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
880 {
881         struct virtio_net *dev = get_device(vid);
882
883         if (dev == NULL)
884                 return;
885
886         vhost_log_write(dev, addr, len);
887 }
888
889 void
890 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
891                          uint64_t offset, uint64_t len)
892 {
893         struct virtio_net *dev;
894         struct vhost_virtqueue *vq;
895
896         dev = get_device(vid);
897         if (dev == NULL)
898                 return;
899
900         if (vring_idx >= VHOST_MAX_VRING)
901                 return;
902         vq = dev->virtqueue[vring_idx];
903         if (!vq)
904                 return;
905
906         vhost_log_used_vring(dev, vq, offset, len);
907 }
908
909 uint32_t
910 rte_vhost_rx_queue_count(int vid, uint16_t qid)
911 {
912         struct virtio_net *dev;
913         struct vhost_virtqueue *vq;
914         uint32_t ret = 0;
915
916         dev = get_device(vid);
917         if (dev == NULL)
918                 return 0;
919
920         if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
921                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
922                         dev->vid, __func__, qid);
923                 return 0;
924         }
925
926         vq = dev->virtqueue[qid];
927         if (vq == NULL)
928                 return 0;
929
930         rte_spinlock_lock(&vq->access_lock);
931
932         if (unlikely(vq->enabled == 0 || vq->avail == NULL))
933                 goto out;
934
935         ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
936
937 out:
938         rte_spinlock_unlock(&vq->access_lock);
939         return ret;
940 }
941
942 int rte_vhost_get_vdpa_device_id(int vid)
943 {
944         struct virtio_net *dev = get_device(vid);
945
946         if (dev == NULL)
947                 return -1;
948
949         return dev->vdpa_dev_id;
950 }
951
952 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
953                 uint64_t *log_size)
954 {
955         struct virtio_net *dev = get_device(vid);
956
957         if (dev == NULL || log_base == NULL || log_size == NULL)
958                 return -1;
959
960         *log_base = dev->log_base;
961         *log_size = dev->log_size;
962
963         return 0;
964 }
965
966 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
967                 uint16_t *last_avail_idx, uint16_t *last_used_idx)
968 {
969         struct virtio_net *dev = get_device(vid);
970
971         if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
972                 return -1;
973
974         *last_avail_idx = dev->virtqueue[queue_id]->last_avail_idx;
975         *last_used_idx = dev->virtqueue[queue_id]->last_used_idx;
976
977         return 0;
978 }
979
980 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
981                 uint16_t last_avail_idx, uint16_t last_used_idx)
982 {
983         struct virtio_net *dev = get_device(vid);
984
985         if (!dev)
986                 return -1;
987
988         dev->virtqueue[queue_id]->last_avail_idx = last_avail_idx;
989         dev->virtqueue[queue_id]->last_used_idx = last_used_idx;
990
991         return 0;
992 }
993
994 int rte_vhost_extern_callback_register(int vid,
995                 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
996 {
997         struct virtio_net *dev = get_device(vid);
998
999         if (dev == NULL || ops == NULL)
1000                 return -1;
1001
1002         dev->extern_ops = *ops;
1003         dev->extern_data = ctx;
1004         return 0;
1005 }