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