net/ixgbe: fix statistics in flow control mode
[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->vdpa_dev_id = -1;
637         dev->postcopy_ufd = -1;
638         rte_spinlock_init(&dev->slave_req_lock);
639
640         return i;
641 }
642
643 void
644 vhost_destroy_device_notify(struct virtio_net *dev)
645 {
646         struct rte_vdpa_device *vdpa_dev;
647         int did;
648
649         if (dev->flags & VIRTIO_DEV_RUNNING) {
650                 did = dev->vdpa_dev_id;
651                 vdpa_dev = rte_vdpa_get_device(did);
652                 if (vdpa_dev && vdpa_dev->ops->dev_close)
653                         vdpa_dev->ops->dev_close(dev->vid);
654                 dev->flags &= ~VIRTIO_DEV_RUNNING;
655                 dev->notify_ops->destroy_device(dev->vid);
656         }
657 }
658
659 /*
660  * Invoked when there is the vhost-user connection is broken (when
661  * the virtio device is being detached).
662  */
663 void
664 vhost_destroy_device(int vid)
665 {
666         struct virtio_net *dev = get_device(vid);
667
668         if (dev == NULL)
669                 return;
670
671         vhost_destroy_device_notify(dev);
672
673         cleanup_device(dev, 1);
674         free_device(dev);
675
676         vhost_devices[vid] = NULL;
677 }
678
679 void
680 vhost_attach_vdpa_device(int vid, int did)
681 {
682         struct virtio_net *dev = get_device(vid);
683
684         if (dev == NULL)
685                 return;
686
687         if (rte_vdpa_get_device(did) == NULL)
688                 return;
689
690         dev->vdpa_dev_id = did;
691 }
692
693 void
694 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
695 {
696         struct virtio_net *dev;
697         unsigned int len;
698
699         dev = get_device(vid);
700         if (dev == NULL)
701                 return;
702
703         len = if_len > sizeof(dev->ifname) ?
704                 sizeof(dev->ifname) : if_len;
705
706         strncpy(dev->ifname, if_name, len);
707         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
708 }
709
710 void
711 vhost_enable_dequeue_zero_copy(int vid)
712 {
713         struct virtio_net *dev = get_device(vid);
714
715         if (dev == NULL)
716                 return;
717
718         dev->dequeue_zero_copy = 1;
719 }
720
721 void
722 vhost_set_builtin_virtio_net(int vid, bool enable)
723 {
724         struct virtio_net *dev = get_device(vid);
725
726         if (dev == NULL)
727                 return;
728
729         if (enable)
730                 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
731         else
732                 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
733 }
734
735 void
736 vhost_enable_extbuf(int vid)
737 {
738         struct virtio_net *dev = get_device(vid);
739
740         if (dev == NULL)
741                 return;
742
743         dev->extbuf = 1;
744 }
745
746 void
747 vhost_enable_linearbuf(int vid)
748 {
749         struct virtio_net *dev = get_device(vid);
750
751         if (dev == NULL)
752                 return;
753
754         dev->linearbuf = 1;
755 }
756
757 int
758 rte_vhost_get_mtu(int vid, uint16_t *mtu)
759 {
760         struct virtio_net *dev = get_device(vid);
761
762         if (dev == NULL || mtu == NULL)
763                 return -ENODEV;
764
765         if (!(dev->flags & VIRTIO_DEV_READY))
766                 return -EAGAIN;
767
768         if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
769                 return -ENOTSUP;
770
771         *mtu = dev->mtu;
772
773         return 0;
774 }
775
776 int
777 rte_vhost_get_numa_node(int vid)
778 {
779 #ifdef RTE_LIBRTE_VHOST_NUMA
780         struct virtio_net *dev = get_device(vid);
781         int numa_node;
782         int ret;
783
784         if (dev == NULL || numa_available() != 0)
785                 return -1;
786
787         ret = get_mempolicy(&numa_node, NULL, 0, dev,
788                             MPOL_F_NODE | MPOL_F_ADDR);
789         if (ret < 0) {
790                 VHOST_LOG_CONFIG(ERR,
791                         "(%d) failed to query numa node: %s\n",
792                         vid, rte_strerror(errno));
793                 return -1;
794         }
795
796         return numa_node;
797 #else
798         RTE_SET_USED(vid);
799         return -1;
800 #endif
801 }
802
803 uint32_t
804 rte_vhost_get_queue_num(int vid)
805 {
806         struct virtio_net *dev = get_device(vid);
807
808         if (dev == NULL)
809                 return 0;
810
811         return dev->nr_vring / 2;
812 }
813
814 uint16_t
815 rte_vhost_get_vring_num(int vid)
816 {
817         struct virtio_net *dev = get_device(vid);
818
819         if (dev == NULL)
820                 return 0;
821
822         return dev->nr_vring;
823 }
824
825 int
826 rte_vhost_get_ifname(int vid, char *buf, size_t len)
827 {
828         struct virtio_net *dev = get_device(vid);
829
830         if (dev == NULL || buf == NULL)
831                 return -1;
832
833         len = RTE_MIN(len, sizeof(dev->ifname));
834
835         strncpy(buf, dev->ifname, len);
836         buf[len - 1] = '\0';
837
838         return 0;
839 }
840
841 int
842 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
843 {
844         struct virtio_net *dev;
845
846         dev = get_device(vid);
847         if (dev == NULL || features == NULL)
848                 return -1;
849
850         *features = dev->features;
851         return 0;
852 }
853
854 int
855 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
856 {
857         struct virtio_net *dev;
858         struct rte_vhost_memory *m;
859         size_t size;
860
861         dev = get_device(vid);
862         if (dev == NULL || mem == NULL)
863                 return -1;
864
865         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
866         m = malloc(sizeof(struct rte_vhost_memory) + size);
867         if (!m)
868                 return -1;
869
870         m->nregions = dev->mem->nregions;
871         memcpy(m->regions, dev->mem->regions, size);
872         *mem = m;
873
874         return 0;
875 }
876
877 int
878 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
879                           struct rte_vhost_vring *vring)
880 {
881         struct virtio_net *dev;
882         struct vhost_virtqueue *vq;
883
884         dev = get_device(vid);
885         if (dev == NULL || vring == NULL)
886                 return -1;
887
888         if (vring_idx >= VHOST_MAX_VRING)
889                 return -1;
890
891         vq = dev->virtqueue[vring_idx];
892         if (!vq)
893                 return -1;
894
895         if (vq_is_packed(dev)) {
896                 vring->desc_packed = vq->desc_packed;
897                 vring->driver_event = vq->driver_event;
898                 vring->device_event = vq->device_event;
899         } else {
900                 vring->desc = vq->desc;
901                 vring->avail = vq->avail;
902                 vring->used = vq->used;
903         }
904         vring->log_guest_addr  = vq->log_guest_addr;
905
906         vring->callfd  = vq->callfd;
907         vring->kickfd  = vq->kickfd;
908         vring->size    = vq->size;
909
910         return 0;
911 }
912
913 int
914 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
915                                   struct rte_vhost_ring_inflight *vring)
916 {
917         struct virtio_net *dev;
918         struct vhost_virtqueue *vq;
919
920         dev = get_device(vid);
921         if (unlikely(!dev))
922                 return -1;
923
924         if (vring_idx >= VHOST_MAX_VRING)
925                 return -1;
926
927         vq = dev->virtqueue[vring_idx];
928         if (unlikely(!vq))
929                 return -1;
930
931         if (vq_is_packed(dev)) {
932                 if (unlikely(!vq->inflight_packed))
933                         return -1;
934
935                 vring->inflight_packed = vq->inflight_packed;
936         } else {
937                 if (unlikely(!vq->inflight_split))
938                         return -1;
939
940                 vring->inflight_split = vq->inflight_split;
941         }
942
943         vring->resubmit_inflight = vq->resubmit_inflight;
944
945         return 0;
946 }
947
948 int
949 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
950                                   uint16_t idx)
951 {
952         struct vhost_virtqueue *vq;
953         struct virtio_net *dev;
954
955         dev = get_device(vid);
956         if (unlikely(!dev))
957                 return -1;
958
959         if (unlikely(!(dev->protocol_features &
960             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
961                 return 0;
962
963         if (unlikely(vq_is_packed(dev)))
964                 return -1;
965
966         if (unlikely(vring_idx >= VHOST_MAX_VRING))
967                 return -1;
968
969         vq = dev->virtqueue[vring_idx];
970         if (unlikely(!vq))
971                 return -1;
972
973         if (unlikely(!vq->inflight_split))
974                 return -1;
975
976         if (unlikely(idx >= vq->size))
977                 return -1;
978
979         vq->inflight_split->desc[idx].counter = vq->global_counter++;
980         vq->inflight_split->desc[idx].inflight = 1;
981         return 0;
982 }
983
984 int
985 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
986                                    uint16_t head, uint16_t last,
987                                    uint16_t *inflight_entry)
988 {
989         struct rte_vhost_inflight_info_packed *inflight_info;
990         struct virtio_net *dev;
991         struct vhost_virtqueue *vq;
992         struct vring_packed_desc *desc;
993         uint16_t old_free_head, free_head;
994
995         dev = get_device(vid);
996         if (unlikely(!dev))
997                 return -1;
998
999         if (unlikely(!(dev->protocol_features &
1000             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1001                 return 0;
1002
1003         if (unlikely(!vq_is_packed(dev)))
1004                 return -1;
1005
1006         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1007                 return -1;
1008
1009         vq = dev->virtqueue[vring_idx];
1010         if (unlikely(!vq))
1011                 return -1;
1012
1013         inflight_info = vq->inflight_packed;
1014         if (unlikely(!inflight_info))
1015                 return -1;
1016
1017         if (unlikely(head >= vq->size))
1018                 return -1;
1019
1020         desc = vq->desc_packed;
1021         old_free_head = inflight_info->old_free_head;
1022         if (unlikely(old_free_head >= vq->size))
1023                 return -1;
1024
1025         free_head = old_free_head;
1026
1027         /* init header descriptor */
1028         inflight_info->desc[old_free_head].num = 0;
1029         inflight_info->desc[old_free_head].counter = vq->global_counter++;
1030         inflight_info->desc[old_free_head].inflight = 1;
1031
1032         /* save desc entry in flight entry */
1033         while (head != ((last + 1) % vq->size)) {
1034                 inflight_info->desc[old_free_head].num++;
1035                 inflight_info->desc[free_head].addr = desc[head].addr;
1036                 inflight_info->desc[free_head].len = desc[head].len;
1037                 inflight_info->desc[free_head].flags = desc[head].flags;
1038                 inflight_info->desc[free_head].id = desc[head].id;
1039
1040                 inflight_info->desc[old_free_head].last = free_head;
1041                 free_head = inflight_info->desc[free_head].next;
1042                 inflight_info->free_head = free_head;
1043                 head = (head + 1) % vq->size;
1044         }
1045
1046         inflight_info->old_free_head = free_head;
1047         *inflight_entry = old_free_head;
1048
1049         return 0;
1050 }
1051
1052 int
1053 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
1054                                   uint16_t last_used_idx, uint16_t idx)
1055 {
1056         struct virtio_net *dev;
1057         struct vhost_virtqueue *vq;
1058
1059         dev = get_device(vid);
1060         if (unlikely(!dev))
1061                 return -1;
1062
1063         if (unlikely(!(dev->protocol_features &
1064             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1065                 return 0;
1066
1067         if (unlikely(vq_is_packed(dev)))
1068                 return -1;
1069
1070         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1071                 return -1;
1072
1073         vq = dev->virtqueue[vring_idx];
1074         if (unlikely(!vq))
1075                 return -1;
1076
1077         if (unlikely(!vq->inflight_split))
1078                 return -1;
1079
1080         if (unlikely(idx >= vq->size))
1081                 return -1;
1082
1083         rte_smp_mb();
1084
1085         vq->inflight_split->desc[idx].inflight = 0;
1086
1087         rte_smp_mb();
1088
1089         vq->inflight_split->used_idx = last_used_idx;
1090         return 0;
1091 }
1092
1093 int
1094 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1095                                    uint16_t head)
1096 {
1097         struct rte_vhost_inflight_info_packed *inflight_info;
1098         struct virtio_net *dev;
1099         struct vhost_virtqueue *vq;
1100
1101         dev = get_device(vid);
1102         if (unlikely(!dev))
1103                 return -1;
1104
1105         if (unlikely(!(dev->protocol_features &
1106             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1107                 return 0;
1108
1109         if (unlikely(!vq_is_packed(dev)))
1110                 return -1;
1111
1112         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1113                 return -1;
1114
1115         vq = dev->virtqueue[vring_idx];
1116         if (unlikely(!vq))
1117                 return -1;
1118
1119         inflight_info = vq->inflight_packed;
1120         if (unlikely(!inflight_info))
1121                 return -1;
1122
1123         if (unlikely(head >= vq->size))
1124                 return -1;
1125
1126         rte_smp_mb();
1127
1128         inflight_info->desc[head].inflight = 0;
1129
1130         rte_smp_mb();
1131
1132         inflight_info->old_free_head = inflight_info->free_head;
1133         inflight_info->old_used_idx = inflight_info->used_idx;
1134         inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1135
1136         return 0;
1137 }
1138
1139 int
1140 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1141                                      uint16_t idx)
1142 {
1143         struct virtio_net *dev;
1144         struct vhost_virtqueue *vq;
1145
1146         dev = get_device(vid);
1147         if (unlikely(!dev))
1148                 return -1;
1149
1150         if (unlikely(!(dev->protocol_features &
1151             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1152                 return 0;
1153
1154         if (unlikely(vq_is_packed(dev)))
1155                 return -1;
1156
1157         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1158                 return -1;
1159
1160         vq = dev->virtqueue[vring_idx];
1161         if (unlikely(!vq))
1162                 return -1;
1163
1164         if (unlikely(!vq->inflight_split))
1165                 return -1;
1166
1167         vq->inflight_split->last_inflight_io = idx;
1168         return 0;
1169 }
1170
1171 int
1172 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1173                                       uint16_t head)
1174 {
1175         struct rte_vhost_inflight_info_packed *inflight_info;
1176         struct virtio_net *dev;
1177         struct vhost_virtqueue *vq;
1178         uint16_t last;
1179
1180         dev = get_device(vid);
1181         if (unlikely(!dev))
1182                 return -1;
1183
1184         if (unlikely(!(dev->protocol_features &
1185             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1186                 return 0;
1187
1188         if (unlikely(!vq_is_packed(dev)))
1189                 return -1;
1190
1191         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1192                 return -1;
1193
1194         vq = dev->virtqueue[vring_idx];
1195         if (unlikely(!vq))
1196                 return -1;
1197
1198         inflight_info = vq->inflight_packed;
1199         if (unlikely(!inflight_info))
1200                 return -1;
1201
1202         if (unlikely(head >= vq->size))
1203                 return -1;
1204
1205         last = inflight_info->desc[head].last;
1206         if (unlikely(last >= vq->size))
1207                 return -1;
1208
1209         inflight_info->desc[last].next = inflight_info->free_head;
1210         inflight_info->free_head = head;
1211         inflight_info->used_idx += inflight_info->desc[head].num;
1212         if (inflight_info->used_idx >= inflight_info->desc_num) {
1213                 inflight_info->used_idx -= inflight_info->desc_num;
1214                 inflight_info->used_wrap_counter =
1215                         !inflight_info->used_wrap_counter;
1216         }
1217
1218         return 0;
1219 }
1220
1221 int
1222 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1223 {
1224         struct virtio_net *dev;
1225         struct vhost_virtqueue *vq;
1226
1227         dev = get_device(vid);
1228         if (!dev)
1229                 return -1;
1230
1231         if (vring_idx >= VHOST_MAX_VRING)
1232                 return -1;
1233
1234         vq = dev->virtqueue[vring_idx];
1235         if (!vq)
1236                 return -1;
1237
1238         if (vq_is_packed(dev))
1239                 vhost_vring_call_packed(dev, vq);
1240         else
1241                 vhost_vring_call_split(dev, vq);
1242
1243         return 0;
1244 }
1245
1246 uint16_t
1247 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1248 {
1249         struct virtio_net *dev;
1250         struct vhost_virtqueue *vq;
1251         uint16_t ret = 0;
1252
1253         dev = get_device(vid);
1254         if (!dev)
1255                 return 0;
1256
1257         vq = dev->virtqueue[queue_id];
1258
1259         rte_spinlock_lock(&vq->access_lock);
1260
1261         if (unlikely(!vq->enabled || vq->avail == NULL))
1262                 goto out;
1263
1264         ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1265
1266 out:
1267         rte_spinlock_unlock(&vq->access_lock);
1268         return ret;
1269 }
1270
1271 static inline int
1272 vhost_enable_notify_split(struct virtio_net *dev,
1273                 struct vhost_virtqueue *vq, int enable)
1274 {
1275         if (vq->used == NULL)
1276                 return -1;
1277
1278         if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1279                 if (enable)
1280                         vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1281                 else
1282                         vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1283         } else {
1284                 if (enable)
1285                         vhost_avail_event(vq) = vq->last_avail_idx;
1286         }
1287         return 0;
1288 }
1289
1290 static inline int
1291 vhost_enable_notify_packed(struct virtio_net *dev,
1292                 struct vhost_virtqueue *vq, int enable)
1293 {
1294         uint16_t flags;
1295
1296         if (vq->device_event == NULL)
1297                 return -1;
1298
1299         if (!enable) {
1300                 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1301                 return 0;
1302         }
1303
1304         flags = VRING_EVENT_F_ENABLE;
1305         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1306                 flags = VRING_EVENT_F_DESC;
1307                 vq->device_event->off_wrap = vq->last_avail_idx |
1308                         vq->avail_wrap_counter << 15;
1309         }
1310
1311         rte_smp_wmb();
1312
1313         vq->device_event->flags = flags;
1314         return 0;
1315 }
1316
1317 int
1318 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1319 {
1320         struct virtio_net *dev = get_device(vid);
1321         struct vhost_virtqueue *vq;
1322         int ret;
1323
1324         if (!dev)
1325                 return -1;
1326
1327         vq = dev->virtqueue[queue_id];
1328
1329         rte_spinlock_lock(&vq->access_lock);
1330
1331         if (vq_is_packed(dev))
1332                 ret = vhost_enable_notify_packed(dev, vq, enable);
1333         else
1334                 ret = vhost_enable_notify_split(dev, vq, enable);
1335
1336         rte_spinlock_unlock(&vq->access_lock);
1337
1338         return ret;
1339 }
1340
1341 void
1342 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1343 {
1344         struct virtio_net *dev = get_device(vid);
1345
1346         if (dev == NULL)
1347                 return;
1348
1349         vhost_log_write(dev, addr, len);
1350 }
1351
1352 void
1353 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1354                          uint64_t offset, uint64_t len)
1355 {
1356         struct virtio_net *dev;
1357         struct vhost_virtqueue *vq;
1358
1359         dev = get_device(vid);
1360         if (dev == NULL)
1361                 return;
1362
1363         if (vring_idx >= VHOST_MAX_VRING)
1364                 return;
1365         vq = dev->virtqueue[vring_idx];
1366         if (!vq)
1367                 return;
1368
1369         vhost_log_used_vring(dev, vq, offset, len);
1370 }
1371
1372 uint32_t
1373 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1374 {
1375         struct virtio_net *dev;
1376         struct vhost_virtqueue *vq;
1377         uint32_t ret = 0;
1378
1379         dev = get_device(vid);
1380         if (dev == NULL)
1381                 return 0;
1382
1383         if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1384                 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1385                         dev->vid, __func__, qid);
1386                 return 0;
1387         }
1388
1389         vq = dev->virtqueue[qid];
1390         if (vq == NULL)
1391                 return 0;
1392
1393         rte_spinlock_lock(&vq->access_lock);
1394
1395         if (unlikely(vq->enabled == 0 || vq->avail == NULL))
1396                 goto out;
1397
1398         ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1399
1400 out:
1401         rte_spinlock_unlock(&vq->access_lock);
1402         return ret;
1403 }
1404
1405 int rte_vhost_get_vdpa_device_id(int vid)
1406 {
1407         struct virtio_net *dev = get_device(vid);
1408
1409         if (dev == NULL)
1410                 return -1;
1411
1412         return dev->vdpa_dev_id;
1413 }
1414
1415 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
1416                 uint64_t *log_size)
1417 {
1418         struct virtio_net *dev = get_device(vid);
1419
1420         if (dev == NULL || log_base == NULL || log_size == NULL)
1421                 return -1;
1422
1423         *log_base = dev->log_base;
1424         *log_size = dev->log_size;
1425
1426         return 0;
1427 }
1428
1429 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1430                 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1431 {
1432         struct vhost_virtqueue *vq;
1433         struct virtio_net *dev = get_device(vid);
1434
1435         if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1436                 return -1;
1437
1438         vq = dev->virtqueue[queue_id];
1439         if (!vq)
1440                 return -1;
1441
1442         if (vq_is_packed(dev)) {
1443                 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1444                                   vq->last_avail_idx;
1445                 *last_used_idx = (vq->used_wrap_counter << 15) |
1446                                  vq->last_used_idx;
1447         } else {
1448                 *last_avail_idx = vq->last_avail_idx;
1449                 *last_used_idx = vq->last_used_idx;
1450         }
1451
1452         return 0;
1453 }
1454
1455 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1456                 uint16_t last_avail_idx, uint16_t last_used_idx)
1457 {
1458         struct vhost_virtqueue *vq;
1459         struct virtio_net *dev = get_device(vid);
1460
1461         if (!dev)
1462                 return -1;
1463
1464         vq = dev->virtqueue[queue_id];
1465         if (!vq)
1466                 return -1;
1467
1468         if (vq_is_packed(dev)) {
1469                 vq->last_avail_idx = last_avail_idx & 0x7fff;
1470                 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1471                 vq->last_used_idx = last_used_idx & 0x7fff;
1472                 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1473         } else {
1474                 vq->last_avail_idx = last_avail_idx;
1475                 vq->last_used_idx = last_used_idx;
1476         }
1477
1478         return 0;
1479 }
1480
1481 int
1482 rte_vhost_get_vring_base_from_inflight(int vid,
1483                                        uint16_t queue_id,
1484                                        uint16_t *last_avail_idx,
1485                                        uint16_t *last_used_idx)
1486 {
1487         struct rte_vhost_inflight_info_packed *inflight_info;
1488         struct virtio_net *dev = get_device(vid);
1489
1490         if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1491                 return -1;
1492
1493         if (!vq_is_packed(dev))
1494                 return -1;
1495
1496         inflight_info = dev->virtqueue[queue_id]->inflight_packed;
1497         if (!inflight_info)
1498                 return -1;
1499
1500         *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1501                           inflight_info->old_used_idx;
1502         *last_used_idx = *last_avail_idx;
1503
1504         return 0;
1505 }
1506
1507 int rte_vhost_extern_callback_register(int vid,
1508                 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1509 {
1510         struct virtio_net *dev = get_device(vid);
1511
1512         if (dev == NULL || ops == NULL)
1513                 return -1;
1514
1515         dev->extern_ops = *ops;
1516         dev->extern_data = ctx;
1517         return 0;
1518 }
1519
1520 RTE_INIT(vhost_log_init)
1521 {
1522         vhost_config_log_level = rte_log_register("lib.vhost.config");
1523         if (vhost_config_log_level >= 0)
1524                 rte_log_set_level(vhost_config_log_level, RTE_LOG_INFO);
1525
1526         vhost_data_log_level = rte_log_register("lib.vhost.data");
1527         if (vhost_data_log_level >= 0)
1528                 rte_log_set_level(vhost_data_log_level, RTE_LOG_WARNING);
1529 }