vhost: replace vDPA device ID in Vhost
[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 int vhost_config_log_level;
31 int vhost_data_log_level;
32
33 /* Called with iotlb_lock read-locked */
34 uint64_t
35 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
36                     uint64_t iova, uint64_t *size, uint8_t perm)
37 {
38         uint64_t vva, tmp_size;
39
40         if (unlikely(!*size))
41                 return 0;
42
43         tmp_size = *size;
44
45         vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
46         if (tmp_size == *size)
47                 return vva;
48
49         iova += tmp_size;
50
51         if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
52                 /*
53                  * iotlb_lock is read-locked for a full burst,
54                  * but it only protects the iotlb cache.
55                  * In case of IOTLB miss, we might block on the socket,
56                  * which could cause a deadlock with QEMU if an IOTLB update
57                  * is being handled. We can safely unlock here to avoid it.
58                  */
59                 vhost_user_iotlb_rd_unlock(vq);
60
61                 vhost_user_iotlb_pending_insert(vq, iova, perm);
62                 if (vhost_user_iotlb_miss(dev, iova, perm)) {
63                         VHOST_LOG_CONFIG(ERR,
64                                 "IOTLB miss req failed for IOVA 0x%" PRIx64 "\n",
65                                 iova);
66                         vhost_user_iotlb_pending_remove(vq, iova, 1, perm);
67                 }
68
69                 vhost_user_iotlb_rd_lock(vq);
70         }
71
72         return 0;
73 }
74
75 #define VHOST_LOG_PAGE  4096
76
77 /*
78  * Atomically set a bit in memory.
79  */
80 static __rte_always_inline void
81 vhost_set_bit(unsigned int nr, volatile uint8_t *addr)
82 {
83 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
84         /*
85          * __sync_ built-ins are deprecated, but __atomic_ ones
86          * are sub-optimized in older GCC versions.
87          */
88         __sync_fetch_and_or_1(addr, (1U << nr));
89 #else
90         __atomic_fetch_or(addr, (1U << nr), __ATOMIC_RELAXED);
91 #endif
92 }
93
94 static __rte_always_inline void
95 vhost_log_page(uint8_t *log_base, uint64_t page)
96 {
97         vhost_set_bit(page % 8, &log_base[page / 8]);
98 }
99
100 void
101 __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
102 {
103         uint64_t page;
104
105         if (unlikely(!dev->log_base || !len))
106                 return;
107
108         if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
109                 return;
110
111         /* To make sure guest memory updates are committed before logging */
112         rte_smp_wmb();
113
114         page = addr / VHOST_LOG_PAGE;
115         while (page * VHOST_LOG_PAGE < addr + len) {
116                 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
117                 page += 1;
118         }
119 }
120
121 void
122 __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
123                              uint64_t iova, uint64_t len)
124 {
125         uint64_t hva, gpa, map_len;
126         map_len = len;
127
128         hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
129         if (map_len != len) {
130                 VHOST_LOG_DATA(ERR,
131                         "Failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
132                         iova);
133                 return;
134         }
135
136         gpa = hva_to_gpa(dev, hva, len);
137         if (gpa)
138                 __vhost_log_write(dev, gpa, len);
139 }
140
141 void
142 __vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
143 {
144         unsigned long *log_base;
145         int i;
146
147         if (unlikely(!dev->log_base))
148                 return;
149
150         rte_smp_wmb();
151
152         log_base = (unsigned long *)(uintptr_t)dev->log_base;
153
154         for (i = 0; i < vq->log_cache_nb_elem; i++) {
155                 struct log_cache_entry *elem = vq->log_cache + i;
156
157 #if defined(RTE_TOOLCHAIN_GCC) && (GCC_VERSION < 70100)
158                 /*
159                  * '__sync' builtins are deprecated, but '__atomic' ones
160                  * are sub-optimized in older GCC versions.
161                  */
162                 __sync_fetch_and_or(log_base + elem->offset, elem->val);
163 #else
164                 __atomic_fetch_or(log_base + elem->offset, elem->val,
165                                 __ATOMIC_RELAXED);
166 #endif
167         }
168
169         rte_smp_wmb();
170
171         vq->log_cache_nb_elem = 0;
172 }
173
174 static __rte_always_inline void
175 vhost_log_cache_page(struct virtio_net *dev, struct vhost_virtqueue *vq,
176                         uint64_t page)
177 {
178         uint32_t bit_nr = page % (sizeof(unsigned long) << 3);
179         uint32_t offset = page / (sizeof(unsigned long) << 3);
180         int i;
181
182         for (i = 0; i < vq->log_cache_nb_elem; i++) {
183                 struct log_cache_entry *elem = vq->log_cache + i;
184
185                 if (elem->offset == offset) {
186                         elem->val |= (1UL << bit_nr);
187                         return;
188                 }
189         }
190
191         if (unlikely(i >= VHOST_LOG_CACHE_NR)) {
192                 /*
193                  * No more room for a new log cache entry,
194                  * so write the dirty log map directly.
195                  */
196                 rte_smp_wmb();
197                 vhost_log_page((uint8_t *)(uintptr_t)dev->log_base, page);
198
199                 return;
200         }
201
202         vq->log_cache[i].offset = offset;
203         vq->log_cache[i].val = (1UL << bit_nr);
204         vq->log_cache_nb_elem++;
205 }
206
207 void
208 __vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
209                         uint64_t addr, uint64_t len)
210 {
211         uint64_t page;
212
213         if (unlikely(!dev->log_base || !len))
214                 return;
215
216         if (unlikely(dev->log_size <= ((addr + len - 1) / VHOST_LOG_PAGE / 8)))
217                 return;
218
219         page = addr / VHOST_LOG_PAGE;
220         while (page * VHOST_LOG_PAGE < addr + len) {
221                 vhost_log_cache_page(dev, vq, page);
222                 page += 1;
223         }
224 }
225
226 void
227 __vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
228                              uint64_t iova, uint64_t len)
229 {
230         uint64_t hva, gpa, map_len;
231         map_len = len;
232
233         hva = __vhost_iova_to_vva(dev, vq, iova, &map_len, VHOST_ACCESS_RW);
234         if (map_len != len) {
235                 VHOST_LOG_DATA(ERR,
236                         "Failed to write log for IOVA 0x%" PRIx64 ". No IOTLB entry found\n",
237                         iova);
238                 return;
239         }
240
241         gpa = hva_to_gpa(dev, hva, len);
242         if (gpa)
243                 __vhost_log_cache_write(dev, vq, gpa, len);
244 }
245
246 void *
247 vhost_alloc_copy_ind_table(struct virtio_net *dev, struct vhost_virtqueue *vq,
248                 uint64_t desc_addr, uint64_t desc_len)
249 {
250         void *idesc;
251         uint64_t src, dst;
252         uint64_t len, remain = desc_len;
253
254         idesc = rte_malloc(__func__, desc_len, 0);
255         if (unlikely(!idesc))
256                 return NULL;
257
258         dst = (uint64_t)(uintptr_t)idesc;
259
260         while (remain) {
261                 len = remain;
262                 src = vhost_iova_to_vva(dev, vq, desc_addr, &len,
263                                 VHOST_ACCESS_RO);
264                 if (unlikely(!src || !len)) {
265                         rte_free(idesc);
266                         return NULL;
267                 }
268
269                 rte_memcpy((void *)(uintptr_t)dst, (void *)(uintptr_t)src, len);
270
271                 remain -= len;
272                 dst += len;
273                 desc_addr += len;
274         }
275
276         return idesc;
277 }
278
279 void
280 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
281 {
282         if ((vq->callfd >= 0) && (destroy != 0))
283                 close(vq->callfd);
284         if (vq->kickfd >= 0)
285                 close(vq->kickfd);
286 }
287
288 void
289 cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq)
290 {
291         if (!(dev->protocol_features &
292             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD)))
293                 return;
294
295         if (vq_is_packed(dev)) {
296                 if (vq->inflight_packed)
297                         vq->inflight_packed = NULL;
298         } else {
299                 if (vq->inflight_split)
300                         vq->inflight_split = NULL;
301         }
302
303         if (vq->resubmit_inflight) {
304                 if (vq->resubmit_inflight->resubmit_list) {
305                         free(vq->resubmit_inflight->resubmit_list);
306                         vq->resubmit_inflight->resubmit_list = NULL;
307                 }
308                 free(vq->resubmit_inflight);
309                 vq->resubmit_inflight = NULL;
310         }
311 }
312
313 /*
314  * Unmap any memory, close any file descriptors and
315  * free any memory owned by a device.
316  */
317 void
318 cleanup_device(struct virtio_net *dev, int destroy)
319 {
320         uint32_t i;
321
322         vhost_backend_cleanup(dev);
323
324         for (i = 0; i < dev->nr_vring; i++) {
325                 cleanup_vq(dev->virtqueue[i], destroy);
326                 cleanup_vq_inflight(dev, dev->virtqueue[i]);
327         }
328 }
329
330 void
331 free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq)
332 {
333         if (vq_is_packed(dev))
334                 rte_free(vq->shadow_used_packed);
335         else
336                 rte_free(vq->shadow_used_split);
337         rte_free(vq->batch_copy_elems);
338         rte_mempool_free(vq->iotlb_pool);
339         rte_free(vq);
340 }
341
342 /*
343  * Release virtqueues and device memory.
344  */
345 static void
346 free_device(struct virtio_net *dev)
347 {
348         uint32_t i;
349
350         for (i = 0; i < dev->nr_vring; i++)
351                 free_vq(dev, dev->virtqueue[i]);
352
353         rte_free(dev);
354 }
355
356 static __rte_always_inline int
357 log_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
358 {
359         if (likely(!(vq->ring_addrs.flags & (1 << VHOST_VRING_F_LOG))))
360                 return 0;
361
362         vq->log_guest_addr = translate_log_addr(dev, vq,
363                                                 vq->ring_addrs.log_guest_addr);
364         if (vq->log_guest_addr == 0)
365                 return -1;
366
367         return 0;
368 }
369
370 /*
371  * Converts vring log address to GPA
372  * If IOMMU is enabled, the log address is IOVA
373  * If IOMMU not enabled, the log address is already GPA
374  *
375  * Caller should have iotlb_lock read-locked
376  */
377 uint64_t
378 translate_log_addr(struct virtio_net *dev, struct vhost_virtqueue *vq,
379                 uint64_t log_addr)
380 {
381         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)) {
382                 const uint64_t exp_size = sizeof(uint64_t);
383                 uint64_t hva, gpa;
384                 uint64_t size = exp_size;
385
386                 hva = vhost_iova_to_vva(dev, vq, log_addr,
387                                         &size, VHOST_ACCESS_RW);
388
389                 if (size != exp_size)
390                         return 0;
391
392                 gpa = hva_to_gpa(dev, hva, exp_size);
393                 if (!gpa) {
394                         VHOST_LOG_CONFIG(ERR,
395                                 "VQ: Failed to find GPA for log_addr: 0x%"
396                                 PRIx64 " hva: 0x%" PRIx64 "\n",
397                                 log_addr, hva);
398                         return 0;
399                 }
400                 return gpa;
401
402         } else
403                 return log_addr;
404 }
405
406 /* Caller should have iotlb_lock read-locked */
407 static int
408 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
409 {
410         uint64_t req_size, size;
411
412         req_size = sizeof(struct vring_desc) * vq->size;
413         size = req_size;
414         vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
415                                                 vq->ring_addrs.desc_user_addr,
416                                                 &size, VHOST_ACCESS_RW);
417         if (!vq->desc || size != req_size)
418                 return -1;
419
420         req_size = sizeof(struct vring_avail);
421         req_size += sizeof(uint16_t) * vq->size;
422         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
423                 req_size += sizeof(uint16_t);
424         size = req_size;
425         vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
426                                                 vq->ring_addrs.avail_user_addr,
427                                                 &size, VHOST_ACCESS_RW);
428         if (!vq->avail || size != req_size)
429                 return -1;
430
431         req_size = sizeof(struct vring_used);
432         req_size += sizeof(struct vring_used_elem) * vq->size;
433         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
434                 req_size += sizeof(uint16_t);
435         size = req_size;
436         vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
437                                                 vq->ring_addrs.used_user_addr,
438                                                 &size, VHOST_ACCESS_RW);
439         if (!vq->used || size != req_size)
440                 return -1;
441
442         return 0;
443 }
444
445 /* Caller should have iotlb_lock read-locked */
446 static int
447 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
448 {
449         uint64_t req_size, size;
450
451         req_size = sizeof(struct vring_packed_desc) * vq->size;
452         size = req_size;
453         vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
454                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
455                                 &size, VHOST_ACCESS_RW);
456         if (!vq->desc_packed || size != req_size)
457                 return -1;
458
459         req_size = sizeof(struct vring_packed_desc_event);
460         size = req_size;
461         vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
462                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
463                                 &size, VHOST_ACCESS_RW);
464         if (!vq->driver_event || size != req_size)
465                 return -1;
466
467         req_size = sizeof(struct vring_packed_desc_event);
468         size = req_size;
469         vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
470                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
471                                 &size, VHOST_ACCESS_RW);
472         if (!vq->device_event || size != req_size)
473                 return -1;
474
475         return 0;
476 }
477
478 int
479 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
480 {
481
482         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
483                 return -1;
484
485         if (vq_is_packed(dev)) {
486                 if (vring_translate_packed(dev, vq) < 0)
487                         return -1;
488         } else {
489                 if (vring_translate_split(dev, vq) < 0)
490                         return -1;
491         }
492
493         if (log_translate(dev, vq) < 0)
494                 return -1;
495
496         vq->access_ok = 1;
497
498         return 0;
499 }
500
501 void
502 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
503 {
504         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
505                 vhost_user_iotlb_wr_lock(vq);
506
507         vq->access_ok = 0;
508         vq->desc = NULL;
509         vq->avail = NULL;
510         vq->used = NULL;
511         vq->log_guest_addr = 0;
512
513         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
514                 vhost_user_iotlb_wr_unlock(vq);
515 }
516
517 static void
518 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
519 {
520         struct vhost_virtqueue *vq;
521
522         if (vring_idx >= VHOST_MAX_VRING) {
523                 VHOST_LOG_CONFIG(ERR,
524                                 "Failed not init vring, out of bound (%d)\n",
525                                 vring_idx);
526                 return;
527         }
528
529         vq = dev->virtqueue[vring_idx];
530
531         memset(vq, 0, sizeof(struct vhost_virtqueue));
532
533         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
534         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
535
536         vhost_user_iotlb_init(dev, vring_idx);
537         /* Backends are set to -1 indicating an inactive device. */
538         vq->backend = -1;
539
540         TAILQ_INIT(&vq->zmbuf_list);
541 }
542
543 static void
544 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
545 {
546         struct vhost_virtqueue *vq;
547         int callfd;
548
549         if (vring_idx >= VHOST_MAX_VRING) {
550                 VHOST_LOG_CONFIG(ERR,
551                                 "Failed not init vring, out of bound (%d)\n",
552                                 vring_idx);
553                 return;
554         }
555
556         vq = dev->virtqueue[vring_idx];
557         callfd = vq->callfd;
558         init_vring_queue(dev, vring_idx);
559         vq->callfd = callfd;
560 }
561
562 int
563 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
564 {
565         struct vhost_virtqueue *vq;
566
567         vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
568         if (vq == NULL) {
569                 VHOST_LOG_CONFIG(ERR,
570                         "Failed to allocate memory for vring:%u.\n", vring_idx);
571                 return -1;
572         }
573
574         dev->virtqueue[vring_idx] = vq;
575         init_vring_queue(dev, vring_idx);
576         rte_spinlock_init(&vq->access_lock);
577         vq->avail_wrap_counter = 1;
578         vq->used_wrap_counter = 1;
579         vq->signalled_used_valid = false;
580
581         dev->nr_vring += 1;
582
583         return 0;
584 }
585
586 /*
587  * Reset some variables in device structure, while keeping few
588  * others untouched, such as vid, ifname, nr_vring: they
589  * should be same unless the device is removed.
590  */
591 void
592 reset_device(struct virtio_net *dev)
593 {
594         uint32_t i;
595
596         dev->features = 0;
597         dev->protocol_features = 0;
598         dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
599
600         for (i = 0; i < dev->nr_vring; i++)
601                 reset_vring_queue(dev, i);
602 }
603
604 /*
605  * Invoked when there is a new vhost-user connection established (when
606  * there is a new virtio device being attached).
607  */
608 int
609 vhost_new_device(void)
610 {
611         struct virtio_net *dev;
612         int i;
613
614         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
615                 if (vhost_devices[i] == NULL)
616                         break;
617         }
618
619         if (i == MAX_VHOST_DEVICE) {
620                 VHOST_LOG_CONFIG(ERR,
621                         "Failed to find a free slot for new device.\n");
622                 return -1;
623         }
624
625         dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
626         if (dev == NULL) {
627                 VHOST_LOG_CONFIG(ERR,
628                         "Failed to allocate memory for new dev.\n");
629                 return -1;
630         }
631
632         vhost_devices[i] = dev;
633         dev->vid = i;
634         dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
635         dev->slave_req_fd = -1;
636         dev->postcopy_ufd = -1;
637         rte_spinlock_init(&dev->slave_req_lock);
638
639         return i;
640 }
641
642 void
643 vhost_destroy_device_notify(struct virtio_net *dev)
644 {
645         struct rte_vdpa_device *vdpa_dev;
646
647         if (dev->flags & VIRTIO_DEV_RUNNING) {
648                 vdpa_dev = dev->vdpa_dev;
649                 if (vdpa_dev && vdpa_dev->ops->dev_close)
650                         vdpa_dev->ops->dev_close(dev->vid);
651                 dev->flags &= ~VIRTIO_DEV_RUNNING;
652                 dev->notify_ops->destroy_device(dev->vid);
653         }
654 }
655
656 /*
657  * Invoked when there is the vhost-user connection is broken (when
658  * the virtio device is being detached).
659  */
660 void
661 vhost_destroy_device(int vid)
662 {
663         struct virtio_net *dev = get_device(vid);
664
665         if (dev == NULL)
666                 return;
667
668         vhost_destroy_device_notify(dev);
669
670         cleanup_device(dev, 1);
671         free_device(dev);
672
673         vhost_devices[vid] = NULL;
674 }
675
676 void
677 vhost_attach_vdpa_device(int vid, struct rte_vdpa_device *vdpa_dev)
678 {
679         struct virtio_net *dev = get_device(vid);
680
681         if (dev == NULL)
682                 return;
683
684         dev->vdpa_dev = vdpa_dev;
685 }
686
687 void
688 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
689 {
690         struct virtio_net *dev;
691         unsigned int len;
692
693         dev = get_device(vid);
694         if (dev == NULL)
695                 return;
696
697         len = if_len > sizeof(dev->ifname) ?
698                 sizeof(dev->ifname) : if_len;
699
700         strncpy(dev->ifname, if_name, len);
701         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
702 }
703
704 void
705 vhost_enable_dequeue_zero_copy(int vid)
706 {
707         struct virtio_net *dev = get_device(vid);
708
709         if (dev == NULL)
710                 return;
711
712         dev->dequeue_zero_copy = 1;
713 }
714
715 void
716 vhost_set_builtin_virtio_net(int vid, bool enable)
717 {
718         struct virtio_net *dev = get_device(vid);
719
720         if (dev == NULL)
721                 return;
722
723         if (enable)
724                 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
725         else
726                 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
727 }
728
729 void
730 vhost_enable_extbuf(int vid)
731 {
732         struct virtio_net *dev = get_device(vid);
733
734         if (dev == NULL)
735                 return;
736
737         dev->extbuf = 1;
738 }
739
740 void
741 vhost_enable_linearbuf(int vid)
742 {
743         struct virtio_net *dev = get_device(vid);
744
745         if (dev == NULL)
746                 return;
747
748         dev->linearbuf = 1;
749 }
750
751 int
752 rte_vhost_get_mtu(int vid, uint16_t *mtu)
753 {
754         struct virtio_net *dev = get_device(vid);
755
756         if (dev == NULL || mtu == NULL)
757                 return -ENODEV;
758
759         if (!(dev->flags & VIRTIO_DEV_READY))
760                 return -EAGAIN;
761
762         if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
763                 return -ENOTSUP;
764
765         *mtu = dev->mtu;
766
767         return 0;
768 }
769
770 int
771 rte_vhost_get_numa_node(int vid)
772 {
773 #ifdef RTE_LIBRTE_VHOST_NUMA
774         struct virtio_net *dev = get_device(vid);
775         int numa_node;
776         int ret;
777
778         if (dev == NULL || numa_available() != 0)
779                 return -1;
780
781         ret = get_mempolicy(&numa_node, NULL, 0, dev,
782                             MPOL_F_NODE | MPOL_F_ADDR);
783         if (ret < 0) {
784                 VHOST_LOG_CONFIG(ERR,
785                         "(%d) failed to query numa node: %s\n",
786                         vid, rte_strerror(errno));
787                 return -1;
788         }
789
790         return numa_node;
791 #else
792         RTE_SET_USED(vid);
793         return -1;
794 #endif
795 }
796
797 uint32_t
798 rte_vhost_get_queue_num(int vid)
799 {
800         struct virtio_net *dev = get_device(vid);
801
802         if (dev == NULL)
803                 return 0;
804
805         return dev->nr_vring / 2;
806 }
807
808 uint16_t
809 rte_vhost_get_vring_num(int vid)
810 {
811         struct virtio_net *dev = get_device(vid);
812
813         if (dev == NULL)
814                 return 0;
815
816         return dev->nr_vring;
817 }
818
819 int
820 rte_vhost_get_ifname(int vid, char *buf, size_t len)
821 {
822         struct virtio_net *dev = get_device(vid);
823
824         if (dev == NULL || buf == NULL)
825                 return -1;
826
827         len = RTE_MIN(len, sizeof(dev->ifname));
828
829         strncpy(buf, dev->ifname, len);
830         buf[len - 1] = '\0';
831
832         return 0;
833 }
834
835 int
836 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
837 {
838         struct virtio_net *dev;
839
840         dev = get_device(vid);
841         if (dev == NULL || features == NULL)
842                 return -1;
843
844         *features = dev->features;
845         return 0;
846 }
847
848 int
849 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
850 {
851         struct virtio_net *dev;
852         struct rte_vhost_memory *m;
853         size_t size;
854
855         dev = get_device(vid);
856         if (dev == NULL || mem == NULL)
857                 return -1;
858
859         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
860         m = malloc(sizeof(struct rte_vhost_memory) + size);
861         if (!m)
862                 return -1;
863
864         m->nregions = dev->mem->nregions;
865         memcpy(m->regions, dev->mem->regions, size);
866         *mem = m;
867
868         return 0;
869 }
870
871 int
872 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
873                           struct rte_vhost_vring *vring)
874 {
875         struct virtio_net *dev;
876         struct vhost_virtqueue *vq;
877
878         dev = get_device(vid);
879         if (dev == NULL || vring == NULL)
880                 return -1;
881
882         if (vring_idx >= VHOST_MAX_VRING)
883                 return -1;
884
885         vq = dev->virtqueue[vring_idx];
886         if (!vq)
887                 return -1;
888
889         if (vq_is_packed(dev)) {
890                 vring->desc_packed = vq->desc_packed;
891                 vring->driver_event = vq->driver_event;
892                 vring->device_event = vq->device_event;
893         } else {
894                 vring->desc = vq->desc;
895                 vring->avail = vq->avail;
896                 vring->used = vq->used;
897         }
898         vring->log_guest_addr  = vq->log_guest_addr;
899
900         vring->callfd  = vq->callfd;
901         vring->kickfd  = vq->kickfd;
902         vring->size    = vq->size;
903
904         return 0;
905 }
906
907 int
908 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
909                                   struct rte_vhost_ring_inflight *vring)
910 {
911         struct virtio_net *dev;
912         struct vhost_virtqueue *vq;
913
914         dev = get_device(vid);
915         if (unlikely(!dev))
916                 return -1;
917
918         if (vring_idx >= VHOST_MAX_VRING)
919                 return -1;
920
921         vq = dev->virtqueue[vring_idx];
922         if (unlikely(!vq))
923                 return -1;
924
925         if (vq_is_packed(dev)) {
926                 if (unlikely(!vq->inflight_packed))
927                         return -1;
928
929                 vring->inflight_packed = vq->inflight_packed;
930         } else {
931                 if (unlikely(!vq->inflight_split))
932                         return -1;
933
934                 vring->inflight_split = vq->inflight_split;
935         }
936
937         vring->resubmit_inflight = vq->resubmit_inflight;
938
939         return 0;
940 }
941
942 int
943 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
944                                   uint16_t idx)
945 {
946         struct vhost_virtqueue *vq;
947         struct virtio_net *dev;
948
949         dev = get_device(vid);
950         if (unlikely(!dev))
951                 return -1;
952
953         if (unlikely(!(dev->protocol_features &
954             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
955                 return 0;
956
957         if (unlikely(vq_is_packed(dev)))
958                 return -1;
959
960         if (unlikely(vring_idx >= VHOST_MAX_VRING))
961                 return -1;
962
963         vq = dev->virtqueue[vring_idx];
964         if (unlikely(!vq))
965                 return -1;
966
967         if (unlikely(!vq->inflight_split))
968                 return -1;
969
970         if (unlikely(idx >= vq->size))
971                 return -1;
972
973         vq->inflight_split->desc[idx].counter = vq->global_counter++;
974         vq->inflight_split->desc[idx].inflight = 1;
975         return 0;
976 }
977
978 int
979 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
980                                    uint16_t head, uint16_t last,
981                                    uint16_t *inflight_entry)
982 {
983         struct rte_vhost_inflight_info_packed *inflight_info;
984         struct virtio_net *dev;
985         struct vhost_virtqueue *vq;
986         struct vring_packed_desc *desc;
987         uint16_t old_free_head, free_head;
988
989         dev = get_device(vid);
990         if (unlikely(!dev))
991                 return -1;
992
993         if (unlikely(!(dev->protocol_features &
994             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
995                 return 0;
996
997         if (unlikely(!vq_is_packed(dev)))
998                 return -1;
999
1000         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1001                 return -1;
1002
1003         vq = dev->virtqueue[vring_idx];
1004         if (unlikely(!vq))
1005                 return -1;
1006
1007         inflight_info = vq->inflight_packed;
1008         if (unlikely(!inflight_info))
1009                 return -1;
1010
1011         if (unlikely(head >= vq->size))
1012                 return -1;
1013
1014         desc = vq->desc_packed;
1015         old_free_head = inflight_info->old_free_head;
1016         if (unlikely(old_free_head >= vq->size))
1017                 return -1;
1018
1019         free_head = old_free_head;
1020
1021         /* init header descriptor */
1022         inflight_info->desc[old_free_head].num = 0;
1023         inflight_info->desc[old_free_head].counter = vq->global_counter++;
1024         inflight_info->desc[old_free_head].inflight = 1;
1025
1026         /* save desc entry in flight entry */
1027         while (head != ((last + 1) % vq->size)) {
1028                 inflight_info->desc[old_free_head].num++;
1029                 inflight_info->desc[free_head].addr = desc[head].addr;
1030                 inflight_info->desc[free_head].len = desc[head].len;
1031                 inflight_info->desc[free_head].flags = desc[head].flags;
1032                 inflight_info->desc[free_head].id = desc[head].id;
1033
1034                 inflight_info->desc[old_free_head].last = free_head;
1035                 free_head = inflight_info->desc[free_head].next;
1036                 inflight_info->free_head = free_head;
1037                 head = (head + 1) % vq->size;
1038         }
1039
1040         inflight_info->old_free_head = free_head;
1041         *inflight_entry = old_free_head;
1042
1043         return 0;
1044 }
1045
1046 int
1047 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
1048                                   uint16_t last_used_idx, uint16_t idx)
1049 {
1050         struct virtio_net *dev;
1051         struct vhost_virtqueue *vq;
1052
1053         dev = get_device(vid);
1054         if (unlikely(!dev))
1055                 return -1;
1056
1057         if (unlikely(!(dev->protocol_features &
1058             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1059                 return 0;
1060
1061         if (unlikely(vq_is_packed(dev)))
1062                 return -1;
1063
1064         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1065                 return -1;
1066
1067         vq = dev->virtqueue[vring_idx];
1068         if (unlikely(!vq))
1069                 return -1;
1070
1071         if (unlikely(!vq->inflight_split))
1072                 return -1;
1073
1074         if (unlikely(idx >= vq->size))
1075                 return -1;
1076
1077         rte_smp_mb();
1078
1079         vq->inflight_split->desc[idx].inflight = 0;
1080
1081         rte_smp_mb();
1082
1083         vq->inflight_split->used_idx = last_used_idx;
1084         return 0;
1085 }
1086
1087 int
1088 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1089                                    uint16_t head)
1090 {
1091         struct rte_vhost_inflight_info_packed *inflight_info;
1092         struct virtio_net *dev;
1093         struct vhost_virtqueue *vq;
1094
1095         dev = get_device(vid);
1096         if (unlikely(!dev))
1097                 return -1;
1098
1099         if (unlikely(!(dev->protocol_features &
1100             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1101                 return 0;
1102
1103         if (unlikely(!vq_is_packed(dev)))
1104                 return -1;
1105
1106         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1107                 return -1;
1108
1109         vq = dev->virtqueue[vring_idx];
1110         if (unlikely(!vq))
1111                 return -1;
1112
1113         inflight_info = vq->inflight_packed;
1114         if (unlikely(!inflight_info))
1115                 return -1;
1116
1117         if (unlikely(head >= vq->size))
1118                 return -1;
1119
1120         rte_smp_mb();
1121
1122         inflight_info->desc[head].inflight = 0;
1123
1124         rte_smp_mb();
1125
1126         inflight_info->old_free_head = inflight_info->free_head;
1127         inflight_info->old_used_idx = inflight_info->used_idx;
1128         inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1129
1130         return 0;
1131 }
1132
1133 int
1134 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1135                                      uint16_t idx)
1136 {
1137         struct virtio_net *dev;
1138         struct vhost_virtqueue *vq;
1139
1140         dev = get_device(vid);
1141         if (unlikely(!dev))
1142                 return -1;
1143
1144         if (unlikely(!(dev->protocol_features &
1145             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1146                 return 0;
1147
1148         if (unlikely(vq_is_packed(dev)))
1149                 return -1;
1150
1151         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1152                 return -1;
1153
1154         vq = dev->virtqueue[vring_idx];
1155         if (unlikely(!vq))
1156                 return -1;
1157
1158         if (unlikely(!vq->inflight_split))
1159                 return -1;
1160
1161         vq->inflight_split->last_inflight_io = idx;
1162         return 0;
1163 }
1164
1165 int
1166 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1167                                       uint16_t head)
1168 {
1169         struct rte_vhost_inflight_info_packed *inflight_info;
1170         struct virtio_net *dev;
1171         struct vhost_virtqueue *vq;
1172         uint16_t last;
1173
1174         dev = get_device(vid);
1175         if (unlikely(!dev))
1176                 return -1;
1177
1178         if (unlikely(!(dev->protocol_features &
1179             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1180                 return 0;
1181
1182         if (unlikely(!vq_is_packed(dev)))
1183                 return -1;
1184
1185         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1186                 return -1;
1187
1188         vq = dev->virtqueue[vring_idx];
1189         if (unlikely(!vq))
1190                 return -1;
1191
1192         inflight_info = vq->inflight_packed;
1193         if (unlikely(!inflight_info))
1194                 return -1;
1195
1196         if (unlikely(head >= vq->size))
1197                 return -1;
1198
1199         last = inflight_info->desc[head].last;
1200         if (unlikely(last >= vq->size))
1201                 return -1;
1202
1203         inflight_info->desc[last].next = inflight_info->free_head;
1204         inflight_info->free_head = head;
1205         inflight_info->used_idx += inflight_info->desc[head].num;
1206         if (inflight_info->used_idx >= inflight_info->desc_num) {
1207                 inflight_info->used_idx -= inflight_info->desc_num;
1208                 inflight_info->used_wrap_counter =
1209                         !inflight_info->used_wrap_counter;
1210         }
1211
1212         return 0;
1213 }
1214
1215 int
1216 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1217 {
1218         struct virtio_net *dev;
1219         struct vhost_virtqueue *vq;
1220
1221         dev = get_device(vid);
1222         if (!dev)
1223                 return -1;
1224
1225         if (vring_idx >= VHOST_MAX_VRING)
1226                 return -1;
1227
1228         vq = dev->virtqueue[vring_idx];
1229         if (!vq)
1230                 return -1;
1231
1232         if (vq_is_packed(dev))
1233                 vhost_vring_call_packed(dev, vq);
1234         else
1235                 vhost_vring_call_split(dev, vq);
1236
1237         return 0;
1238 }
1239
1240 uint16_t
1241 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1242 {
1243         struct virtio_net *dev;
1244         struct vhost_virtqueue *vq;
1245         uint16_t ret = 0;
1246
1247         dev = get_device(vid);
1248         if (!dev)
1249                 return 0;
1250
1251         vq = dev->virtqueue[queue_id];
1252
1253         rte_spinlock_lock(&vq->access_lock);
1254
1255         if (unlikely(!vq->enabled || vq->avail == NULL))
1256                 goto out;
1257
1258         ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1259
1260 out:
1261         rte_spinlock_unlock(&vq->access_lock);
1262         return ret;
1263 }
1264
1265 static inline int
1266 vhost_enable_notify_split(struct virtio_net *dev,
1267                 struct vhost_virtqueue *vq, int enable)
1268 {
1269         if (vq->used == NULL)
1270                 return -1;
1271
1272         if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1273                 if (enable)
1274                         vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1275                 else
1276                         vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1277         } else {
1278                 if (enable)
1279                         vhost_avail_event(vq) = vq->last_avail_idx;
1280         }
1281         return 0;
1282 }
1283
1284 static inline int
1285 vhost_enable_notify_packed(struct virtio_net *dev,
1286                 struct vhost_virtqueue *vq, int enable)
1287 {
1288         uint16_t flags;
1289
1290         if (vq->device_event == NULL)
1291                 return -1;
1292
1293         if (!enable) {
1294                 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1295                 return 0;
1296         }
1297
1298         flags = VRING_EVENT_F_ENABLE;
1299         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1300                 flags = VRING_EVENT_F_DESC;
1301                 vq->device_event->off_wrap = vq->last_avail_idx |
1302                         vq->avail_wrap_counter << 15;
1303         }
1304
1305         rte_smp_wmb();
1306
1307         vq->device_event->flags = flags;
1308         return 0;
1309 }
1310
1311 int
1312 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1313 {
1314         struct virtio_net *dev = get_device(vid);
1315         struct vhost_virtqueue *vq;
1316         int ret;
1317
1318         if (!dev)
1319                 return -1;
1320
1321         vq = dev->virtqueue[queue_id];
1322
1323         rte_spinlock_lock(&vq->access_lock);
1324
1325         if (vq_is_packed(dev))
1326                 ret = vhost_enable_notify_packed(dev, vq, enable);
1327         else
1328                 ret = vhost_enable_notify_split(dev, vq, enable);
1329
1330         rte_spinlock_unlock(&vq->access_lock);
1331
1332         return ret;
1333 }
1334
1335 void
1336 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1337 {
1338         struct virtio_net *dev = get_device(vid);
1339
1340         if (dev == NULL)
1341                 return;
1342
1343         vhost_log_write(dev, addr, len);
1344 }
1345
1346 void
1347 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1348                          uint64_t offset, uint64_t len)
1349 {
1350         struct virtio_net *dev;
1351         struct vhost_virtqueue *vq;
1352
1353         dev = get_device(vid);
1354         if (dev == NULL)
1355                 return;
1356
1357         if (vring_idx >= VHOST_MAX_VRING)
1358                 return;
1359         vq = dev->virtqueue[vring_idx];
1360         if (!vq)
1361                 return;
1362
1363         vhost_log_used_vring(dev, vq, offset, len);
1364 }
1365
1366 uint32_t
1367 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1368 {
1369         struct virtio_net *dev;
1370         struct vhost_virtqueue *vq;
1371         uint32_t ret = 0;
1372
1373         dev = get_device(vid);
1374         if (dev == NULL)
1375                 return 0;
1376
1377         if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1378                 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1379                         dev->vid, __func__, qid);
1380                 return 0;
1381         }
1382
1383         vq = dev->virtqueue[qid];
1384         if (vq == NULL)
1385                 return 0;
1386
1387         rte_spinlock_lock(&vq->access_lock);
1388
1389         if (unlikely(vq->enabled == 0 || vq->avail == NULL))
1390                 goto out;
1391
1392         ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1393
1394 out:
1395         rte_spinlock_unlock(&vq->access_lock);
1396         return ret;
1397 }
1398
1399 struct rte_vdpa_device *
1400 rte_vhost_get_vdpa_device(int vid)
1401 {
1402         struct virtio_net *dev = get_device(vid);
1403
1404         if (dev == NULL)
1405                 return NULL;
1406
1407         return dev->vdpa_dev;
1408 }
1409
1410 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
1411                 uint64_t *log_size)
1412 {
1413         struct virtio_net *dev = get_device(vid);
1414
1415         if (dev == NULL || log_base == NULL || log_size == NULL)
1416                 return -1;
1417
1418         *log_base = dev->log_base;
1419         *log_size = dev->log_size;
1420
1421         return 0;
1422 }
1423
1424 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1425                 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1426 {
1427         struct vhost_virtqueue *vq;
1428         struct virtio_net *dev = get_device(vid);
1429
1430         if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1431                 return -1;
1432
1433         vq = dev->virtqueue[queue_id];
1434         if (!vq)
1435                 return -1;
1436
1437         if (vq_is_packed(dev)) {
1438                 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1439                                   vq->last_avail_idx;
1440                 *last_used_idx = (vq->used_wrap_counter << 15) |
1441                                  vq->last_used_idx;
1442         } else {
1443                 *last_avail_idx = vq->last_avail_idx;
1444                 *last_used_idx = vq->last_used_idx;
1445         }
1446
1447         return 0;
1448 }
1449
1450 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1451                 uint16_t last_avail_idx, uint16_t last_used_idx)
1452 {
1453         struct vhost_virtqueue *vq;
1454         struct virtio_net *dev = get_device(vid);
1455
1456         if (!dev)
1457                 return -1;
1458
1459         vq = dev->virtqueue[queue_id];
1460         if (!vq)
1461                 return -1;
1462
1463         if (vq_is_packed(dev)) {
1464                 vq->last_avail_idx = last_avail_idx & 0x7fff;
1465                 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1466                 vq->last_used_idx = last_used_idx & 0x7fff;
1467                 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1468         } else {
1469                 vq->last_avail_idx = last_avail_idx;
1470                 vq->last_used_idx = last_used_idx;
1471         }
1472
1473         return 0;
1474 }
1475
1476 int
1477 rte_vhost_get_vring_base_from_inflight(int vid,
1478                                        uint16_t queue_id,
1479                                        uint16_t *last_avail_idx,
1480                                        uint16_t *last_used_idx)
1481 {
1482         struct rte_vhost_inflight_info_packed *inflight_info;
1483         struct virtio_net *dev = get_device(vid);
1484
1485         if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1486                 return -1;
1487
1488         if (!vq_is_packed(dev))
1489                 return -1;
1490
1491         inflight_info = dev->virtqueue[queue_id]->inflight_packed;
1492         if (!inflight_info)
1493                 return -1;
1494
1495         *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1496                           inflight_info->old_used_idx;
1497         *last_used_idx = *last_avail_idx;
1498
1499         return 0;
1500 }
1501
1502 int rte_vhost_extern_callback_register(int vid,
1503                 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1504 {
1505         struct virtio_net *dev = get_device(vid);
1506
1507         if (dev == NULL || ops == NULL)
1508                 return -1;
1509
1510         dev->extern_ops = *ops;
1511         dev->extern_data = ctx;
1512         return 0;
1513 }
1514
1515 RTE_INIT(vhost_log_init)
1516 {
1517         vhost_config_log_level = rte_log_register("lib.vhost.config");
1518         if (vhost_config_log_level >= 0)
1519                 rte_log_set_level(vhost_config_log_level, RTE_LOG_INFO);
1520
1521         vhost_data_log_level = rte_log_register("lib.vhost.data");
1522         if (vhost_data_log_level >= 0)
1523                 rte_log_set_level(vhost_data_log_level, RTE_LOG_WARNING);
1524 }