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