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