vhost: check message header size read
[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 int
357 vring_translate_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
358 {
359         uint64_t req_size, size;
360
361         req_size = sizeof(struct vring_desc) * vq->size;
362         size = req_size;
363         vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
364                                                 vq->ring_addrs.desc_user_addr,
365                                                 &size, VHOST_ACCESS_RW);
366         if (!vq->desc || size != req_size)
367                 return -1;
368
369         req_size = sizeof(struct vring_avail);
370         req_size += sizeof(uint16_t) * vq->size;
371         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
372                 req_size += sizeof(uint16_t);
373         size = req_size;
374         vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
375                                                 vq->ring_addrs.avail_user_addr,
376                                                 &size, VHOST_ACCESS_RW);
377         if (!vq->avail || size != req_size)
378                 return -1;
379
380         req_size = sizeof(struct vring_used);
381         req_size += sizeof(struct vring_used_elem) * vq->size;
382         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
383                 req_size += sizeof(uint16_t);
384         size = req_size;
385         vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
386                                                 vq->ring_addrs.used_user_addr,
387                                                 &size, VHOST_ACCESS_RW);
388         if (!vq->used || size != req_size)
389                 return -1;
390
391         return 0;
392 }
393
394 static int
395 vring_translate_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
396 {
397         uint64_t req_size, size;
398
399         req_size = sizeof(struct vring_packed_desc) * vq->size;
400         size = req_size;
401         vq->desc_packed = (struct vring_packed_desc *)(uintptr_t)
402                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.desc_user_addr,
403                                 &size, VHOST_ACCESS_RW);
404         if (!vq->desc_packed || size != req_size)
405                 return -1;
406
407         req_size = sizeof(struct vring_packed_desc_event);
408         size = req_size;
409         vq->driver_event = (struct vring_packed_desc_event *)(uintptr_t)
410                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.avail_user_addr,
411                                 &size, VHOST_ACCESS_RW);
412         if (!vq->driver_event || size != req_size)
413                 return -1;
414
415         req_size = sizeof(struct vring_packed_desc_event);
416         size = req_size;
417         vq->device_event = (struct vring_packed_desc_event *)(uintptr_t)
418                 vhost_iova_to_vva(dev, vq, vq->ring_addrs.used_user_addr,
419                                 &size, VHOST_ACCESS_RW);
420         if (!vq->device_event || size != req_size)
421                 return -1;
422
423         return 0;
424 }
425
426 int
427 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
428 {
429
430         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
431                 return -1;
432
433         if (vq_is_packed(dev)) {
434                 if (vring_translate_packed(dev, vq) < 0)
435                         return -1;
436         } else {
437                 if (vring_translate_split(dev, vq) < 0)
438                         return -1;
439         }
440         vq->access_ok = 1;
441
442         return 0;
443 }
444
445 void
446 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
447 {
448         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
449                 vhost_user_iotlb_wr_lock(vq);
450
451         vq->access_ok = 0;
452         vq->desc = NULL;
453         vq->avail = NULL;
454         vq->used = NULL;
455         vq->log_guest_addr = 0;
456
457         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
458                 vhost_user_iotlb_wr_unlock(vq);
459 }
460
461 static void
462 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
463 {
464         struct vhost_virtqueue *vq;
465
466         if (vring_idx >= VHOST_MAX_VRING) {
467                 VHOST_LOG_CONFIG(ERR,
468                                 "Failed not init vring, out of bound (%d)\n",
469                                 vring_idx);
470                 return;
471         }
472
473         vq = dev->virtqueue[vring_idx];
474
475         memset(vq, 0, sizeof(struct vhost_virtqueue));
476
477         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
478         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
479
480         vhost_user_iotlb_init(dev, vring_idx);
481         /* Backends are set to -1 indicating an inactive device. */
482         vq->backend = -1;
483
484         TAILQ_INIT(&vq->zmbuf_list);
485 }
486
487 static void
488 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
489 {
490         struct vhost_virtqueue *vq;
491         int callfd;
492
493         if (vring_idx >= VHOST_MAX_VRING) {
494                 VHOST_LOG_CONFIG(ERR,
495                                 "Failed not init vring, out of bound (%d)\n",
496                                 vring_idx);
497                 return;
498         }
499
500         vq = dev->virtqueue[vring_idx];
501         callfd = vq->callfd;
502         init_vring_queue(dev, vring_idx);
503         vq->callfd = callfd;
504 }
505
506 int
507 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
508 {
509         struct vhost_virtqueue *vq;
510
511         vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
512         if (vq == NULL) {
513                 VHOST_LOG_CONFIG(ERR,
514                         "Failed to allocate memory for vring:%u.\n", vring_idx);
515                 return -1;
516         }
517
518         dev->virtqueue[vring_idx] = vq;
519         init_vring_queue(dev, vring_idx);
520         rte_spinlock_init(&vq->access_lock);
521         vq->avail_wrap_counter = 1;
522         vq->used_wrap_counter = 1;
523         vq->signalled_used_valid = false;
524
525         dev->nr_vring += 1;
526
527         return 0;
528 }
529
530 /*
531  * Reset some variables in device structure, while keeping few
532  * others untouched, such as vid, ifname, nr_vring: they
533  * should be same unless the device is removed.
534  */
535 void
536 reset_device(struct virtio_net *dev)
537 {
538         uint32_t i;
539
540         dev->features = 0;
541         dev->protocol_features = 0;
542         dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
543
544         for (i = 0; i < dev->nr_vring; i++)
545                 reset_vring_queue(dev, i);
546 }
547
548 /*
549  * Invoked when there is a new vhost-user connection established (when
550  * there is a new virtio device being attached).
551  */
552 int
553 vhost_new_device(void)
554 {
555         struct virtio_net *dev;
556         int i;
557
558         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
559                 if (vhost_devices[i] == NULL)
560                         break;
561         }
562
563         if (i == MAX_VHOST_DEVICE) {
564                 VHOST_LOG_CONFIG(ERR,
565                         "Failed to find a free slot for new device.\n");
566                 return -1;
567         }
568
569         dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
570         if (dev == NULL) {
571                 VHOST_LOG_CONFIG(ERR,
572                         "Failed to allocate memory for new dev.\n");
573                 return -1;
574         }
575
576         vhost_devices[i] = dev;
577         dev->vid = i;
578         dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
579         dev->slave_req_fd = -1;
580         dev->vdpa_dev_id = -1;
581         dev->postcopy_ufd = -1;
582         rte_spinlock_init(&dev->slave_req_lock);
583
584         return i;
585 }
586
587 void
588 vhost_destroy_device_notify(struct virtio_net *dev)
589 {
590         struct rte_vdpa_device *vdpa_dev;
591         int did;
592
593         if (dev->flags & VIRTIO_DEV_RUNNING) {
594                 did = dev->vdpa_dev_id;
595                 vdpa_dev = rte_vdpa_get_device(did);
596                 if (vdpa_dev && vdpa_dev->ops->dev_close)
597                         vdpa_dev->ops->dev_close(dev->vid);
598                 dev->flags &= ~VIRTIO_DEV_RUNNING;
599                 dev->notify_ops->destroy_device(dev->vid);
600         }
601 }
602
603 /*
604  * Invoked when there is the vhost-user connection is broken (when
605  * the virtio device is being detached).
606  */
607 void
608 vhost_destroy_device(int vid)
609 {
610         struct virtio_net *dev = get_device(vid);
611
612         if (dev == NULL)
613                 return;
614
615         vhost_destroy_device_notify(dev);
616
617         cleanup_device(dev, 1);
618         free_device(dev);
619
620         vhost_devices[vid] = NULL;
621 }
622
623 void
624 vhost_attach_vdpa_device(int vid, int did)
625 {
626         struct virtio_net *dev = get_device(vid);
627
628         if (dev == NULL)
629                 return;
630
631         if (rte_vdpa_get_device(did) == NULL)
632                 return;
633
634         dev->vdpa_dev_id = did;
635 }
636
637 void
638 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
639 {
640         struct virtio_net *dev;
641         unsigned int len;
642
643         dev = get_device(vid);
644         if (dev == NULL)
645                 return;
646
647         len = if_len > sizeof(dev->ifname) ?
648                 sizeof(dev->ifname) : if_len;
649
650         strncpy(dev->ifname, if_name, len);
651         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
652 }
653
654 void
655 vhost_enable_dequeue_zero_copy(int vid)
656 {
657         struct virtio_net *dev = get_device(vid);
658
659         if (dev == NULL)
660                 return;
661
662         dev->dequeue_zero_copy = 1;
663 }
664
665 void
666 vhost_set_builtin_virtio_net(int vid, bool enable)
667 {
668         struct virtio_net *dev = get_device(vid);
669
670         if (dev == NULL)
671                 return;
672
673         if (enable)
674                 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
675         else
676                 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
677 }
678
679 void
680 vhost_enable_extbuf(int vid)
681 {
682         struct virtio_net *dev = get_device(vid);
683
684         if (dev == NULL)
685                 return;
686
687         dev->extbuf = 1;
688 }
689
690 void
691 vhost_enable_linearbuf(int vid)
692 {
693         struct virtio_net *dev = get_device(vid);
694
695         if (dev == NULL)
696                 return;
697
698         dev->linearbuf = 1;
699 }
700
701 int
702 rte_vhost_get_mtu(int vid, uint16_t *mtu)
703 {
704         struct virtio_net *dev = get_device(vid);
705
706         if (dev == NULL || mtu == NULL)
707                 return -ENODEV;
708
709         if (!(dev->flags & VIRTIO_DEV_READY))
710                 return -EAGAIN;
711
712         if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
713                 return -ENOTSUP;
714
715         *mtu = dev->mtu;
716
717         return 0;
718 }
719
720 int
721 rte_vhost_get_numa_node(int vid)
722 {
723 #ifdef RTE_LIBRTE_VHOST_NUMA
724         struct virtio_net *dev = get_device(vid);
725         int numa_node;
726         int ret;
727
728         if (dev == NULL || numa_available() != 0)
729                 return -1;
730
731         ret = get_mempolicy(&numa_node, NULL, 0, dev,
732                             MPOL_F_NODE | MPOL_F_ADDR);
733         if (ret < 0) {
734                 VHOST_LOG_CONFIG(ERR,
735                         "(%d) failed to query numa node: %s\n",
736                         vid, rte_strerror(errno));
737                 return -1;
738         }
739
740         return numa_node;
741 #else
742         RTE_SET_USED(vid);
743         return -1;
744 #endif
745 }
746
747 uint32_t
748 rte_vhost_get_queue_num(int vid)
749 {
750         struct virtio_net *dev = get_device(vid);
751
752         if (dev == NULL)
753                 return 0;
754
755         return dev->nr_vring / 2;
756 }
757
758 uint16_t
759 rte_vhost_get_vring_num(int vid)
760 {
761         struct virtio_net *dev = get_device(vid);
762
763         if (dev == NULL)
764                 return 0;
765
766         return dev->nr_vring;
767 }
768
769 int
770 rte_vhost_get_ifname(int vid, char *buf, size_t len)
771 {
772         struct virtio_net *dev = get_device(vid);
773
774         if (dev == NULL || buf == NULL)
775                 return -1;
776
777         len = RTE_MIN(len, sizeof(dev->ifname));
778
779         strncpy(buf, dev->ifname, len);
780         buf[len - 1] = '\0';
781
782         return 0;
783 }
784
785 int
786 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
787 {
788         struct virtio_net *dev;
789
790         dev = get_device(vid);
791         if (dev == NULL || features == NULL)
792                 return -1;
793
794         *features = dev->features;
795         return 0;
796 }
797
798 int
799 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
800 {
801         struct virtio_net *dev;
802         struct rte_vhost_memory *m;
803         size_t size;
804
805         dev = get_device(vid);
806         if (dev == NULL || mem == NULL)
807                 return -1;
808
809         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
810         m = malloc(sizeof(struct rte_vhost_memory) + size);
811         if (!m)
812                 return -1;
813
814         m->nregions = dev->mem->nregions;
815         memcpy(m->regions, dev->mem->regions, size);
816         *mem = m;
817
818         return 0;
819 }
820
821 int
822 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
823                           struct rte_vhost_vring *vring)
824 {
825         struct virtio_net *dev;
826         struct vhost_virtqueue *vq;
827
828         dev = get_device(vid);
829         if (dev == NULL || vring == NULL)
830                 return -1;
831
832         if (vring_idx >= VHOST_MAX_VRING)
833                 return -1;
834
835         vq = dev->virtqueue[vring_idx];
836         if (!vq)
837                 return -1;
838
839         if (vq_is_packed(dev)) {
840                 vring->desc_packed = vq->desc_packed;
841                 vring->driver_event = vq->driver_event;
842                 vring->device_event = vq->device_event;
843         } else {
844                 vring->desc = vq->desc;
845                 vring->avail = vq->avail;
846                 vring->used = vq->used;
847         }
848         vring->log_guest_addr  = vq->log_guest_addr;
849
850         vring->callfd  = vq->callfd;
851         vring->kickfd  = vq->kickfd;
852         vring->size    = vq->size;
853
854         return 0;
855 }
856
857 int
858 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
859                                   struct rte_vhost_ring_inflight *vring)
860 {
861         struct virtio_net *dev;
862         struct vhost_virtqueue *vq;
863
864         dev = get_device(vid);
865         if (unlikely(!dev))
866                 return -1;
867
868         if (vring_idx >= VHOST_MAX_VRING)
869                 return -1;
870
871         vq = dev->virtqueue[vring_idx];
872         if (unlikely(!vq))
873                 return -1;
874
875         if (vq_is_packed(dev)) {
876                 if (unlikely(!vq->inflight_packed))
877                         return -1;
878
879                 vring->inflight_packed = vq->inflight_packed;
880         } else {
881                 if (unlikely(!vq->inflight_split))
882                         return -1;
883
884                 vring->inflight_split = vq->inflight_split;
885         }
886
887         vring->resubmit_inflight = vq->resubmit_inflight;
888
889         return 0;
890 }
891
892 int
893 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
894                                   uint16_t idx)
895 {
896         struct vhost_virtqueue *vq;
897         struct virtio_net *dev;
898
899         dev = get_device(vid);
900         if (unlikely(!dev))
901                 return -1;
902
903         if (unlikely(!(dev->protocol_features &
904             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
905                 return 0;
906
907         if (unlikely(vq_is_packed(dev)))
908                 return -1;
909
910         if (unlikely(vring_idx >= VHOST_MAX_VRING))
911                 return -1;
912
913         vq = dev->virtqueue[vring_idx];
914         if (unlikely(!vq))
915                 return -1;
916
917         if (unlikely(!vq->inflight_split))
918                 return -1;
919
920         if (unlikely(idx >= vq->size))
921                 return -1;
922
923         vq->inflight_split->desc[idx].counter = vq->global_counter++;
924         vq->inflight_split->desc[idx].inflight = 1;
925         return 0;
926 }
927
928 int
929 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
930                                    uint16_t head, uint16_t last,
931                                    uint16_t *inflight_entry)
932 {
933         struct rte_vhost_inflight_info_packed *inflight_info;
934         struct virtio_net *dev;
935         struct vhost_virtqueue *vq;
936         struct vring_packed_desc *desc;
937         uint16_t old_free_head, free_head;
938
939         dev = get_device(vid);
940         if (unlikely(!dev))
941                 return -1;
942
943         if (unlikely(!(dev->protocol_features &
944             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
945                 return 0;
946
947         if (unlikely(!vq_is_packed(dev)))
948                 return -1;
949
950         if (unlikely(vring_idx >= VHOST_MAX_VRING))
951                 return -1;
952
953         vq = dev->virtqueue[vring_idx];
954         if (unlikely(!vq))
955                 return -1;
956
957         inflight_info = vq->inflight_packed;
958         if (unlikely(!inflight_info))
959                 return -1;
960
961         if (unlikely(head >= vq->size))
962                 return -1;
963
964         desc = vq->desc_packed;
965         old_free_head = inflight_info->old_free_head;
966         if (unlikely(old_free_head >= vq->size))
967                 return -1;
968
969         free_head = old_free_head;
970
971         /* init header descriptor */
972         inflight_info->desc[old_free_head].num = 0;
973         inflight_info->desc[old_free_head].counter = vq->global_counter++;
974         inflight_info->desc[old_free_head].inflight = 1;
975
976         /* save desc entry in flight entry */
977         while (head != ((last + 1) % vq->size)) {
978                 inflight_info->desc[old_free_head].num++;
979                 inflight_info->desc[free_head].addr = desc[head].addr;
980                 inflight_info->desc[free_head].len = desc[head].len;
981                 inflight_info->desc[free_head].flags = desc[head].flags;
982                 inflight_info->desc[free_head].id = desc[head].id;
983
984                 inflight_info->desc[old_free_head].last = free_head;
985                 free_head = inflight_info->desc[free_head].next;
986                 inflight_info->free_head = free_head;
987                 head = (head + 1) % vq->size;
988         }
989
990         inflight_info->old_free_head = free_head;
991         *inflight_entry = old_free_head;
992
993         return 0;
994 }
995
996 int
997 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
998                                   uint16_t last_used_idx, uint16_t idx)
999 {
1000         struct virtio_net *dev;
1001         struct vhost_virtqueue *vq;
1002
1003         dev = get_device(vid);
1004         if (unlikely(!dev))
1005                 return -1;
1006
1007         if (unlikely(!(dev->protocol_features &
1008             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1009                 return 0;
1010
1011         if (unlikely(vq_is_packed(dev)))
1012                 return -1;
1013
1014         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1015                 return -1;
1016
1017         vq = dev->virtqueue[vring_idx];
1018         if (unlikely(!vq))
1019                 return -1;
1020
1021         if (unlikely(!vq->inflight_split))
1022                 return -1;
1023
1024         if (unlikely(idx >= vq->size))
1025                 return -1;
1026
1027         rte_smp_mb();
1028
1029         vq->inflight_split->desc[idx].inflight = 0;
1030
1031         rte_smp_mb();
1032
1033         vq->inflight_split->used_idx = last_used_idx;
1034         return 0;
1035 }
1036
1037 int
1038 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
1039                                    uint16_t head)
1040 {
1041         struct rte_vhost_inflight_info_packed *inflight_info;
1042         struct virtio_net *dev;
1043         struct vhost_virtqueue *vq;
1044
1045         dev = get_device(vid);
1046         if (unlikely(!dev))
1047                 return -1;
1048
1049         if (unlikely(!(dev->protocol_features &
1050             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1051                 return 0;
1052
1053         if (unlikely(!vq_is_packed(dev)))
1054                 return -1;
1055
1056         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1057                 return -1;
1058
1059         vq = dev->virtqueue[vring_idx];
1060         if (unlikely(!vq))
1061                 return -1;
1062
1063         inflight_info = vq->inflight_packed;
1064         if (unlikely(!inflight_info))
1065                 return -1;
1066
1067         if (unlikely(head >= vq->size))
1068                 return -1;
1069
1070         rte_smp_mb();
1071
1072         inflight_info->desc[head].inflight = 0;
1073
1074         rte_smp_mb();
1075
1076         inflight_info->old_free_head = inflight_info->free_head;
1077         inflight_info->old_used_idx = inflight_info->used_idx;
1078         inflight_info->old_used_wrap_counter = inflight_info->used_wrap_counter;
1079
1080         return 0;
1081 }
1082
1083 int
1084 rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx,
1085                                      uint16_t idx)
1086 {
1087         struct virtio_net *dev;
1088         struct vhost_virtqueue *vq;
1089
1090         dev = get_device(vid);
1091         if (unlikely(!dev))
1092                 return -1;
1093
1094         if (unlikely(!(dev->protocol_features &
1095             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1096                 return 0;
1097
1098         if (unlikely(vq_is_packed(dev)))
1099                 return -1;
1100
1101         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1102                 return -1;
1103
1104         vq = dev->virtqueue[vring_idx];
1105         if (unlikely(!vq))
1106                 return -1;
1107
1108         if (unlikely(!vq->inflight_split))
1109                 return -1;
1110
1111         vq->inflight_split->last_inflight_io = idx;
1112         return 0;
1113 }
1114
1115 int
1116 rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx,
1117                                       uint16_t head)
1118 {
1119         struct rte_vhost_inflight_info_packed *inflight_info;
1120         struct virtio_net *dev;
1121         struct vhost_virtqueue *vq;
1122         uint16_t last;
1123
1124         dev = get_device(vid);
1125         if (unlikely(!dev))
1126                 return -1;
1127
1128         if (unlikely(!(dev->protocol_features &
1129             (1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD))))
1130                 return 0;
1131
1132         if (unlikely(!vq_is_packed(dev)))
1133                 return -1;
1134
1135         if (unlikely(vring_idx >= VHOST_MAX_VRING))
1136                 return -1;
1137
1138         vq = dev->virtqueue[vring_idx];
1139         if (unlikely(!vq))
1140                 return -1;
1141
1142         inflight_info = vq->inflight_packed;
1143         if (unlikely(!inflight_info))
1144                 return -1;
1145
1146         if (unlikely(head >= vq->size))
1147                 return -1;
1148
1149         last = inflight_info->desc[head].last;
1150         if (unlikely(last >= vq->size))
1151                 return -1;
1152
1153         inflight_info->desc[last].next = inflight_info->free_head;
1154         inflight_info->free_head = head;
1155         inflight_info->used_idx += inflight_info->desc[head].num;
1156         if (inflight_info->used_idx >= inflight_info->desc_num) {
1157                 inflight_info->used_idx -= inflight_info->desc_num;
1158                 inflight_info->used_wrap_counter =
1159                         !inflight_info->used_wrap_counter;
1160         }
1161
1162         return 0;
1163 }
1164
1165 int
1166 rte_vhost_vring_call(int vid, uint16_t vring_idx)
1167 {
1168         struct virtio_net *dev;
1169         struct vhost_virtqueue *vq;
1170
1171         dev = get_device(vid);
1172         if (!dev)
1173                 return -1;
1174
1175         if (vring_idx >= VHOST_MAX_VRING)
1176                 return -1;
1177
1178         vq = dev->virtqueue[vring_idx];
1179         if (!vq)
1180                 return -1;
1181
1182         if (vq_is_packed(dev))
1183                 vhost_vring_call_packed(dev, vq);
1184         else
1185                 vhost_vring_call_split(dev, vq);
1186
1187         return 0;
1188 }
1189
1190 uint16_t
1191 rte_vhost_avail_entries(int vid, uint16_t queue_id)
1192 {
1193         struct virtio_net *dev;
1194         struct vhost_virtqueue *vq;
1195         uint16_t ret = 0;
1196
1197         dev = get_device(vid);
1198         if (!dev)
1199                 return 0;
1200
1201         vq = dev->virtqueue[queue_id];
1202
1203         rte_spinlock_lock(&vq->access_lock);
1204
1205         if (unlikely(!vq->enabled || vq->avail == NULL))
1206                 goto out;
1207
1208         ret = *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
1209
1210 out:
1211         rte_spinlock_unlock(&vq->access_lock);
1212         return ret;
1213 }
1214
1215 static inline int
1216 vhost_enable_notify_split(struct virtio_net *dev,
1217                 struct vhost_virtqueue *vq, int enable)
1218 {
1219         if (vq->used == NULL)
1220                 return -1;
1221
1222         if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
1223                 if (enable)
1224                         vq->used->flags &= ~VRING_USED_F_NO_NOTIFY;
1225                 else
1226                         vq->used->flags |= VRING_USED_F_NO_NOTIFY;
1227         } else {
1228                 if (enable)
1229                         vhost_avail_event(vq) = vq->last_avail_idx;
1230         }
1231         return 0;
1232 }
1233
1234 static inline int
1235 vhost_enable_notify_packed(struct virtio_net *dev,
1236                 struct vhost_virtqueue *vq, int enable)
1237 {
1238         uint16_t flags;
1239
1240         if (vq->device_event == NULL)
1241                 return -1;
1242
1243         if (!enable) {
1244                 vq->device_event->flags = VRING_EVENT_F_DISABLE;
1245                 return 0;
1246         }
1247
1248         flags = VRING_EVENT_F_ENABLE;
1249         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
1250                 flags = VRING_EVENT_F_DESC;
1251                 vq->device_event->off_wrap = vq->last_avail_idx |
1252                         vq->avail_wrap_counter << 15;
1253         }
1254
1255         rte_smp_wmb();
1256
1257         vq->device_event->flags = flags;
1258         return 0;
1259 }
1260
1261 int
1262 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
1263 {
1264         struct virtio_net *dev = get_device(vid);
1265         struct vhost_virtqueue *vq;
1266         int ret;
1267
1268         if (!dev)
1269                 return -1;
1270
1271         vq = dev->virtqueue[queue_id];
1272
1273         rte_spinlock_lock(&vq->access_lock);
1274
1275         if (vq_is_packed(dev))
1276                 ret = vhost_enable_notify_packed(dev, vq, enable);
1277         else
1278                 ret = vhost_enable_notify_split(dev, vq, enable);
1279
1280         rte_spinlock_unlock(&vq->access_lock);
1281
1282         return ret;
1283 }
1284
1285 void
1286 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
1287 {
1288         struct virtio_net *dev = get_device(vid);
1289
1290         if (dev == NULL)
1291                 return;
1292
1293         vhost_log_write(dev, addr, len);
1294 }
1295
1296 void
1297 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
1298                          uint64_t offset, uint64_t len)
1299 {
1300         struct virtio_net *dev;
1301         struct vhost_virtqueue *vq;
1302
1303         dev = get_device(vid);
1304         if (dev == NULL)
1305                 return;
1306
1307         if (vring_idx >= VHOST_MAX_VRING)
1308                 return;
1309         vq = dev->virtqueue[vring_idx];
1310         if (!vq)
1311                 return;
1312
1313         vhost_log_used_vring(dev, vq, offset, len);
1314 }
1315
1316 uint32_t
1317 rte_vhost_rx_queue_count(int vid, uint16_t qid)
1318 {
1319         struct virtio_net *dev;
1320         struct vhost_virtqueue *vq;
1321         uint32_t ret = 0;
1322
1323         dev = get_device(vid);
1324         if (dev == NULL)
1325                 return 0;
1326
1327         if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
1328                 VHOST_LOG_DATA(ERR, "(%d) %s: invalid virtqueue idx %d.\n",
1329                         dev->vid, __func__, qid);
1330                 return 0;
1331         }
1332
1333         vq = dev->virtqueue[qid];
1334         if (vq == NULL)
1335                 return 0;
1336
1337         rte_spinlock_lock(&vq->access_lock);
1338
1339         if (unlikely(vq->enabled == 0 || vq->avail == NULL))
1340                 goto out;
1341
1342         ret = *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
1343
1344 out:
1345         rte_spinlock_unlock(&vq->access_lock);
1346         return ret;
1347 }
1348
1349 int rte_vhost_get_vdpa_device_id(int vid)
1350 {
1351         struct virtio_net *dev = get_device(vid);
1352
1353         if (dev == NULL)
1354                 return -1;
1355
1356         return dev->vdpa_dev_id;
1357 }
1358
1359 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
1360                 uint64_t *log_size)
1361 {
1362         struct virtio_net *dev = get_device(vid);
1363
1364         if (dev == NULL || log_base == NULL || log_size == NULL)
1365                 return -1;
1366
1367         *log_base = dev->log_base;
1368         *log_size = dev->log_size;
1369
1370         return 0;
1371 }
1372
1373 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1374                 uint16_t *last_avail_idx, uint16_t *last_used_idx)
1375 {
1376         struct vhost_virtqueue *vq;
1377         struct virtio_net *dev = get_device(vid);
1378
1379         if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1380                 return -1;
1381
1382         vq = dev->virtqueue[queue_id];
1383         if (!vq)
1384                 return -1;
1385
1386         if (vq_is_packed(dev)) {
1387                 *last_avail_idx = (vq->avail_wrap_counter << 15) |
1388                                   vq->last_avail_idx;
1389                 *last_used_idx = (vq->used_wrap_counter << 15) |
1390                                  vq->last_used_idx;
1391         } else {
1392                 *last_avail_idx = vq->last_avail_idx;
1393                 *last_used_idx = vq->last_used_idx;
1394         }
1395
1396         return 0;
1397 }
1398
1399 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1400                 uint16_t last_avail_idx, uint16_t last_used_idx)
1401 {
1402         struct vhost_virtqueue *vq;
1403         struct virtio_net *dev = get_device(vid);
1404
1405         if (!dev)
1406                 return -1;
1407
1408         vq = dev->virtqueue[queue_id];
1409         if (!vq)
1410                 return -1;
1411
1412         if (vq_is_packed(dev)) {
1413                 vq->last_avail_idx = last_avail_idx & 0x7fff;
1414                 vq->avail_wrap_counter = !!(last_avail_idx & (1 << 15));
1415                 vq->last_used_idx = last_used_idx & 0x7fff;
1416                 vq->used_wrap_counter = !!(last_used_idx & (1 << 15));
1417         } else {
1418                 vq->last_avail_idx = last_avail_idx;
1419                 vq->last_used_idx = last_used_idx;
1420         }
1421
1422         return 0;
1423 }
1424
1425 int
1426 rte_vhost_get_vring_base_from_inflight(int vid,
1427                                        uint16_t queue_id,
1428                                        uint16_t *last_avail_idx,
1429                                        uint16_t *last_used_idx)
1430 {
1431         struct rte_vhost_inflight_info_packed *inflight_info;
1432         struct virtio_net *dev = get_device(vid);
1433
1434         if (dev == NULL || last_avail_idx == NULL || last_used_idx == NULL)
1435                 return -1;
1436
1437         if (!vq_is_packed(dev))
1438                 return -1;
1439
1440         inflight_info = dev->virtqueue[queue_id]->inflight_packed;
1441         if (!inflight_info)
1442                 return -1;
1443
1444         *last_avail_idx = (inflight_info->old_used_wrap_counter << 15) |
1445                           inflight_info->old_used_idx;
1446         *last_used_idx = *last_avail_idx;
1447
1448         return 0;
1449 }
1450
1451 int rte_vhost_extern_callback_register(int vid,
1452                 struct rte_vhost_user_extern_ops const * const ops, void *ctx)
1453 {
1454         struct virtio_net *dev = get_device(vid);
1455
1456         if (dev == NULL || ops == NULL)
1457                 return -1;
1458
1459         dev->extern_ops = *ops;
1460         dev->extern_data = ctx;
1461         return 0;
1462 }
1463
1464 RTE_INIT(vhost_log_init)
1465 {
1466         vhost_config_log_level = rte_log_register("lib.vhost.config");
1467         if (vhost_config_log_level >= 0)
1468                 rte_log_set_level(vhost_config_log_level, RTE_LOG_INFO);
1469
1470         vhost_data_log_level = rte_log_register("lib.vhost.data");
1471         if (vhost_data_log_level >= 0)
1472                 rte_log_set_level(vhost_data_log_level, RTE_LOG_WARNING);
1473 }