vhost: add packed ring support to vring related APIs
[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         if (vq_is_packed(dev)) {
815                 vring->desc_packed = vq->desc_packed;
816                 vring->driver_event = vq->driver_event;
817                 vring->device_event = vq->device_event;
818         } else {
819                 vring->desc = vq->desc;
820                 vring->avail = vq->avail;
821                 vring->used = vq->used;
822         }
823         vring->log_guest_addr  = vq->log_guest_addr;
824
825         vring->callfd  = vq->callfd;
826         vring->kickfd  = vq->kickfd;
827         vring->size    = vq->size;
828
829         return 0;
830 }
831
832 int
833 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
834                                   struct rte_vhost_ring_inflight *vring)
835 {
836         struct virtio_net *dev;
837         struct vhost_virtqueue *vq;
838
839         dev = get_device(vid);
840         if (unlikely(!dev))
841                 return -1;
842
843         if (vring_idx >= VHOST_MAX_VRING)
844                 return -1;
845
846         vq = dev->virtqueue[vring_idx];
847         if (unlikely(!vq))
848                 return -1;
849
850         if (vq_is_packed(dev)) {
851                 if (unlikely(!vq->inflight_packed))
852                         return -1;
853
854                 vring->inflight_packed = vq->inflight_packed;
855         } else {
856                 if (unlikely(!vq->inflight_split))
857                         return -1;
858
859                 vring->inflight_split = vq->inflight_split;
860         }
861
862         vring->resubmit_inflight = vq->resubmit_inflight;
863
864         return 0;
865 }
866
867 int
868 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
869                                   uint16_t idx)
870 {
871         struct vhost_virtqueue *vq;
872         struct virtio_net *dev;
873
874         dev = get_device(vid);
875         if (unlikely(!dev))
876                 return -1;
877
878         if (unlikely(!(dev->protocol_features &
879             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
880                 return 0;
881
882         if (unlikely(vq_is_packed(dev)))
883                 return -1;
884
885         if (unlikely(vring_idx >= VHOST_MAX_VRING))
886                 return -1;
887
888         vq = dev->virtqueue[vring_idx];
889         if (unlikely(!vq))
890                 return -1;
891
892         if (unlikely(!vq->inflight_split))
893                 return -1;
894
895         if (unlikely(idx >= vq->size))
896                 return -1;
897
898         vq->inflight_split->desc[idx].counter = vq->global_counter++;
899         vq->inflight_split->desc[idx].inflight = 1;
900         return 0;
901 }
902
903 int
904 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
905                                    uint16_t head, uint16_t last,
906                                    uint16_t *inflight_entry)
907 {
908         struct rte_vhost_inflight_info_packed *inflight_info;
909         struct virtio_net *dev;
910         struct vhost_virtqueue *vq;
911         struct vring_packed_desc *desc;
912         uint16_t old_free_head, free_head;
913
914         dev = get_device(vid);
915         if (unlikely(!dev))
916                 return -1;
917
918         if (unlikely(!(dev->protocol_features &
919             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
920                 return 0;
921
922         if (unlikely(!vq_is_packed(dev)))
923                 return -1;
924
925         if (unlikely(vring_idx >= VHOST_MAX_VRING))
926                 return -1;
927
928         vq = dev->virtqueue[vring_idx];
929         if (unlikely(!vq))
930                 return -1;
931
932         inflight_info = vq->inflight_packed;
933         if (unlikely(!inflight_info))
934                 return -1;
935
936         if (unlikely(head >= vq->size))
937                 return -1;
938
939         desc = vq->desc_packed;
940         old_free_head = inflight_info->old_free_head;
941         if (unlikely(old_free_head >= vq->size))
942                 return -1;
943
944         free_head = old_free_head;
945
946         /* init header descriptor */
947         inflight_info->desc[old_free_head].num = 0;
948         inflight_info->desc[old_free_head].counter = vq->global_counter++;
949         inflight_info->desc[old_free_head].inflight = 1;
950
951         /* save desc entry in flight entry */
952         while (head != ((last + 1) % vq->size)) {
953                 inflight_info->desc[old_free_head].num++;
954                 inflight_info->desc[free_head].addr = desc[head].addr;
955                 inflight_info->desc[free_head].len = desc[head].len;
956                 inflight_info->desc[free_head].flags = desc[head].flags;
957                 inflight_info->desc[free_head].id = desc[head].id;
958
959                 inflight_info->desc[old_free_head].last = free_head;
960                 free_head = inflight_info->desc[free_head].next;
961                 inflight_info->free_head = free_head;
962                 head = (head + 1) % vq->size;
963         }
964
965         inflight_info->old_free_head = free_head;
966         *inflight_entry = old_free_head;
967
968         return 0;
969 }
970
971 int
972 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
973                                   uint16_t last_used_idx, uint16_t idx)
974 {
975         struct virtio_net *dev;
976         struct vhost_virtqueue *vq;
977
978         dev = get_device(vid);
979         if (unlikely(!dev))
980                 return -1;
981
982         if (unlikely(!(dev->protocol_features &
983             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
984                 return 0;
985
986         if (unlikely(vq_is_packed(dev)))
987                 return -1;
988
989         if (unlikely(vring_idx >= VHOST_MAX_VRING))
990                 return -1;
991
992         vq = dev->virtqueue[vring_idx];
993         if (unlikely(!vq))
994                 return -1;
995
996         if (unlikely(!vq->inflight_split))
997                 return -1;
998
999         if (unlikely(idx >= vq->size))
1000                 return -1;
1001
1002         rte_smp_mb();
1003
1004         vq->inflight_split->desc[idx].inflight = 0;
1005
1006         rte_smp_mb();
1007
1008         vq->inflight_split->used_idx = last_used_idx;
1009         return 0;
1010 }
1011
1012 int
1013 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1014                                    uint16_t head)
1015 {
1016         struct rte_vhost_inflight_info_packed *inflight_info;
1017         struct virtio_net *dev;
1018         struct vhost_virtqueue *vq;
1019
1020         dev = get_device(vid);
1021         if (unlikely(!dev))
1022                 return -1;
1023
1024         if (unlikely(!(dev->protocol_features &
1025             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1026                 return 0;
1027
1028         if (unlikely(!vq_is_packed(dev)))
1029                 return -1;
1030
1031         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1032                 return -1;
1033
1034         vq = dev->virtqueue[vring_idx];
1035         if (unlikely(!vq))
1036                 return -1;
1037
1038         inflight_info = vq->inflight_packed;
1039         if (unlikely(!inflight_info))
1040                 return -1;
1041
1042         if (unlikely(head >= vq->size))
1043                 return -1;
1044
1045         rte_smp_mb();
1046
1047         inflight_info->desc[head].inflight = 0;
1048
1049         rte_smp_mb();
1050
1051         inflight_info->old_free_head = inflight_info->free_head;
1052         inflight_info->old_used_idx = inflight_info->used_idx;
1053         inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1054
1055         return 0;
1056 }
1057
1058 int
1059 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1060                                      uint16_t idx)
1061 {
1062         struct virtio_net *dev;
1063         struct vhost_virtqueue *vq;
1064
1065         dev = get_device(vid);
1066         if (unlikely(!dev))
1067                 return -1;
1068
1069         if (unlikely(!(dev->protocol_features &
1070             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1071                 return 0;
1072
1073         if (unlikely(vq_is_packed(dev)))
1074                 return -1;
1075
1076         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1077                 return -1;
1078
1079         vq = dev->virtqueue[vring_idx];
1080         if (unlikely(!vq))
1081                 return -1;
1082
1083         if (unlikely(!vq->inflight_split))
1084                 return -1;
1085
1086         vq->inflight_split->last_inflight_io = idx;
1087         return 0;
1088 }
1089
1090 int
1091 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1092                                       uint16_t head)
1093 {
1094         struct rte_vhost_inflight_info_packed *inflight_info;
1095         struct virtio_net *dev;
1096         struct vhost_virtqueue *vq;
1097         uint16_t last;
1098
1099         dev = get_device(vid);
1100         if (unlikely(!dev))
1101                 return -1;
1102
1103         if (unlikely(!(dev->protocol_features &
1104             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1105                 return 0;
1106
1107         if (unlikely(!vq_is_packed(dev)))
1108                 return -1;
1109
1110         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1111                 return -1;
1112
1113         vq = dev->virtqueue[vring_idx];
1114         if (unlikely(!vq))
1115                 return -1;
1116
1117         inflight_info = vq->inflight_packed;
1118         if (unlikely(!inflight_info))
1119                 return -1;
1120
1121         if (unlikely(head >= vq->size))
1122                 return -1;
1123
1124         last = inflight_info->desc[head].last;
1125         if (unlikely(last >= vq->size))
1126                 return -1;
1127
1128         inflight_info->desc[last].next = inflight_info->free_head;
1129         inflight_info->free_head = head;
1130         inflight_info->used_idx += inflight_info->desc[head].num;
1131         if (inflight_info->used_idx >= inflight_info->desc_num) {
1132                 inflight_info->used_idx -= inflight_info->desc_num;
1133                 inflight_info->used_wrap_counter =
1134                         !inflight_info->used_wrap_counter;
1135         }
1136
1137         return 0;
1138 }
1139
1140 int
1141 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1142 {
1143         struct virtio_net *dev;
1144         struct vhost_virtqueue *vq;
1145
1146         dev = get_device(vid);
1147         if (!dev)
1148                 return -1;
1149
1150         if (vring_idx >= VHOST_MAX_VRING)
1151                 return -1;
1152
1153         vq = dev->virtqueue[vring_idx];
1154         if (!vq)
1155                 return -1;
1156
1157         if (vq_is_packed(dev))
1158                 vhost_vring_call_packed(dev, vq);
1159         else
1160                 vhost_vring_call_split(dev, vq);
1161
1162         return 0;
1163 }
1164
1165 uint16_t
1166 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1167 {
1168         struct virtio_net *dev;
1169         struct vhost_virtqueue *vq;
1170         uint16_t ret = 0;
1171
1172         dev = get_device(vid);
1173         if (!dev)
1174                 return 0;
1175
1176         vq = dev->virtqueue[queue_id];
1177
1178         rte_spinlock_lock(&vq->access_lock);
1179
1180         if (unlikely(!vq->enabled || vq->avail == NULL))
1181                 goto out;
1182
1183         ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1184
1185 out:
1186         rte_spinlock_unlock(&vq->access_lock);
1187         return ret;
1188 }
1189
1190 static inline int
1191 vhost_enable_notify_split(struct virtio_net *dev,
1192                 struct vhost_virtqueue *vq, int enable)
1193 {
1194         if (vq->used == NULL)
1195                 return -1;
1196
1197         if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1198                 if (enable)
1199                         vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1200                 else
1201                         vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1202         } else {
1203                 if (enable)
1204                         vhost_avail_event(vq) = vq->last_avail_idx;
1205         }
1206         return 0;
1207 }
1208
1209 static inline int
1210 vhost_enable_notify_packed(struct virtio_net *dev,
1211                 struct vhost_virtqueue *vq, int enable)
1212 {
1213         uint16_t flags;
1214
1215         if (vq->device_event == NULL)
1216                 return -1;
1217
1218         if (!enable) {
1219                 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1220                 return 0;
1221         }
1222
1223         flags = VRING_EVENT_F_ENABLE;
1224         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1225                 flags = VRING_EVENT_F_DESC;
1226                 vq->device_event->off_wrap = vq->last_avail_idx |
1227                         vq->avail_wrap_counter << 15;
1228         }
1229
1230         rte_smp_wmb();
1231
1232         vq->device_event->flags = flags;
1233         return 0;
1234 }
1235
1236 int
1237 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1238 {
1239         struct virtio_net *dev = get_device(vid);
1240         struct vhost_virtqueue *vq;
1241         int ret;
1242
1243         if (!dev)
1244                 return -1;
1245
1246         vq = dev->virtqueue[queue_id];
1247
1248         rte_spinlock_lock(&vq->access_lock);
1249
1250         if (vq_is_packed(dev))
1251                 ret = vhost_enable_notify_packed(dev, vq, enable);
1252         else
1253                 ret = vhost_enable_notify_split(dev, vq, enable);
1254
1255         rte_spinlock_unlock(&vq->access_lock);
1256
1257         return ret;
1258 }
1259
1260 void
1261 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1262 {
1263         struct virtio_net *dev = get_device(vid);
1264
1265         if (dev == NULL)
1266                 return;
1267
1268         vhost_log_write(dev, addr, len);
1269 }
1270
1271 void
1272 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1273                          uint64_t offset, uint64_t len)
1274 {
1275         struct virtio_net *dev;
1276         struct vhost_virtqueue *vq;
1277
1278         dev = get_device(vid);
1279         if (dev == NULL)
1280                 return;
1281
1282         if (vring_idx >= VHOST_MAX_VRING)
1283                 return;
1284         vq = dev->virtqueue[vring_idx];
1285         if (!vq)
1286                 return;
1287
1288         vhost_log_used_vring(dev, vq, offset, len);
1289 }
1290
1291 uint32_t
1292 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1293 {
1294         struct virtio_net *dev;
1295         struct vhost_virtqueue *vq;
1296         uint32_t ret = 0;
1297
1298         dev = get_device(vid);
1299         if (dev == NULL)
1300                 return 0;
1301
1302         if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1303                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
1304                         dev->vid, __func__, qid);
1305                 return 0;
1306         }
1307
1308         vq = dev->virtqueue[qid];
1309         if (vq == NULL)
1310                 return 0;
1311
1312         rte_spinlock_lock(&vq->access_lock);
1313
1314         if (unlikely(vq->enabled == 0 || vq->avail == NULL))
1315                 goto out;
1316
1317         ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1318
1319 out:
1320         rte_spinlock_unlock(&vq->access_lock);
1321         return ret;
1322 }
1323
1324 int rte_vhost_get_vdpa_device_id(int vid)
1325 {
1326         struct virtio_net *dev = get_device(vid);
1327
1328         if (dev == NULL)
1329                 return -1;
1330
1331         return dev->vdpa_dev_id;
1332 }
1333
1334 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
1335                 uint64_t *log_size)
1336 {
1337         struct virtio_net *dev = get_device(vid);
1338
1339         if (dev == NULL || log_base == NULL || log_size == NULL)
1340                 return -1;
1341
1342         *log_base = dev->log_base;
1343         *log_size = dev->log_size;
1344
1345         return 0;
1346 }
1347
1348 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1349                 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1350 {
1351         struct vhost_virtqueue *vq;
1352         struct virtio_net *dev = get_device(vid);
1353
1354         if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1355                 return -1;
1356
1357         vq = dev->virtqueue[queue_id];
1358         if (!vq)
1359                 return -1;
1360
1361         if (vq_is_packed(dev)) {
1362                 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1363                                   vq->last_avail_idx;
1364                 *last_used_idx = (vq->used_wrap_counter << 15) |
1365                                  vq->last_used_idx;
1366         } else {
1367                 *last_avail_idx = vq->last_avail_idx;
1368                 *last_used_idx = vq->last_used_idx;
1369         }
1370
1371         return 0;
1372 }
1373
1374 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1375                 uint16_t last_avail_idx, uint16_t last_used_idx)
1376 {
1377         struct vhost_virtqueue *vq;
1378         struct virtio_net *dev = get_device(vid);
1379
1380         if (!dev)
1381                 return -1;
1382
1383         vq = dev->virtqueue[queue_id];
1384         if (!vq)
1385                 return -1;
1386
1387         if (vq_is_packed(dev)) {
1388                 vq->last_avail_idx = last_avail_idx & 0x7fff;
1389                 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1390                 vq->last_used_idx = last_used_idx & 0x7fff;
1391                 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1392         } else {
1393                 vq->last_avail_idx = last_avail_idx;
1394                 vq->last_used_idx = last_used_idx;
1395         }
1396
1397         return 0;
1398 }
1399
1400 int
1401 rte_vhost_get_vring_base_from_inflight(int vid,
1402                                        uint16_t queue_id,
1403                                        uint16_t *last_avail_idx,
1404                                        uint16_t *last_used_idx)
1405 {
1406         struct rte_vhost_inflight_info_packed *inflight_info;
1407         struct virtio_net *dev = get_device(vid);
1408
1409         if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1410                 return -1;
1411
1412         if (!vq_is_packed(dev))
1413                 return -1;
1414
1415         inflight_info = dev->virtqueue[queue_id]->inflight_packed;
1416         if (!inflight_info)
1417                 return -1;
1418
1419         *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1420                           inflight_info->old_used_idx;
1421         *last_used_idx = *last_avail_idx;
1422
1423         return 0;
1424 }
1425
1426 int rte_vhost_extern_callback_register(int vid,
1427                 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1428 {
1429         struct virtio_net *dev = get_device(vid);
1430
1431         if (dev == NULL || ops == NULL)
1432                 return -1;
1433
1434         dev->extern_ops = *ops;
1435         dev->extern_data = ctx;
1436         return 0;
1437 }