vhost: flush batched enqueue descs directly
[dpdk.git] / lib / librte_vhost / vhost.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2018 Intel Corporation
3  */
4
5 #ifndef _VHOST_NET_CDEV_H_
6 #define _VHOST_NET_CDEV_H_
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdbool.h>
10 #include <sys/types.h>
11 #include <sys/queue.h>
12 #include <unistd.h>
13 #include <linux/vhost.h>
14 #include <linux/virtio_net.h>
15 #include <sys/socket.h>
16 #include <linux/if.h>
17
18 #include <rte_log.h>
19 #include <rte_ether.h>
20 #include <rte_rwlock.h>
21 #include <rte_malloc.h>
22
23 #include "rte_vhost.h"
24 #include "rte_vdpa.h"
25
26 /* Used to indicate that the device is running on a data core */
27 #define VIRTIO_DEV_RUNNING 1
28 /* Used to indicate that the device is ready to operate */
29 #define VIRTIO_DEV_READY 2
30 /* Used to indicate that the built-in vhost net device backend is enabled */
31 #define VIRTIO_DEV_BUILTIN_VIRTIO_NET 4
32 /* Used to indicate that the device has its own data path and configured */
33 #define VIRTIO_DEV_VDPA_CONFIGURED 8
34
35 /* Backend value set by guest. */
36 #define VIRTIO_DEV_STOPPED -1
37
38 #define BUF_VECTOR_MAX 256
39
40 #define VHOST_LOG_CACHE_NR 32
41
42 #define PACKED_DESC_ENQUEUE_USED_FLAG(w)        \
43         ((w) ? (VRING_DESC_F_AVAIL | VRING_DESC_F_USED | VRING_DESC_F_WRITE) : \
44                 VRING_DESC_F_WRITE)
45 #define PACKED_DESC_SINGLE_DEQUEUE_FLAG (VRING_DESC_F_NEXT | \
46                                          VRING_DESC_F_INDIRECT)
47
48 #define PACKED_BATCH_SIZE (RTE_CACHE_LINE_SIZE / \
49                             sizeof(struct vring_packed_desc))
50 #define PACKED_BATCH_MASK (PACKED_BATCH_SIZE - 1)
51
52 #ifdef VHOST_GCC_UNROLL_PRAGMA
53 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("GCC unroll 4") \
54         for (iter = val; iter < size; iter++)
55 #endif
56
57 #ifdef VHOST_CLANG_UNROLL_PRAGMA
58 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll 4") \
59         for (iter = val; iter < size; iter++)
60 #endif
61
62 #ifdef VHOST_ICC_UNROLL_PRAGMA
63 #define vhost_for_each_try_unroll(iter, val, size) _Pragma("unroll (4)") \
64         for (iter = val; iter < size; iter++)
65 #endif
66
67 #ifndef vhost_for_each_try_unroll
68 #define vhost_for_each_try_unroll(iter, val, num) \
69         for (iter = val; iter < num; iter++)
70 #endif
71
72 /**
73  * Structure contains buffer address, length and descriptor index
74  * from vring to do scatter RX.
75  */
76 struct buf_vector {
77         uint64_t buf_iova;
78         uint64_t buf_addr;
79         uint32_t buf_len;
80         uint32_t desc_idx;
81 };
82
83 /*
84  * A structure to hold some fields needed in zero copy code path,
85  * mainly for associating an mbuf with the right desc_idx.
86  */
87 struct zcopy_mbuf {
88         struct rte_mbuf *mbuf;
89         uint32_t desc_idx;
90         uint16_t desc_count;
91         uint16_t in_use;
92
93         TAILQ_ENTRY(zcopy_mbuf) next;
94 };
95 TAILQ_HEAD(zcopy_mbuf_list, zcopy_mbuf);
96
97 /*
98  * Structure contains the info for each batched memory copy.
99  */
100 struct batch_copy_elem {
101         void *dst;
102         void *src;
103         uint32_t len;
104         uint64_t log_addr;
105 };
106
107 /*
108  * Structure that contains the info for batched dirty logging.
109  */
110 struct log_cache_entry {
111         uint32_t offset;
112         unsigned long val;
113 };
114
115 struct vring_used_elem_packed {
116         uint16_t id;
117         uint32_t len;
118         uint32_t count;
119 };
120
121 /**
122  * Structure contains variables relevant to RX/TX virtqueues.
123  */
124 struct vhost_virtqueue {
125         union {
126                 struct vring_desc       *desc;
127                 struct vring_packed_desc   *desc_packed;
128         };
129         union {
130                 struct vring_avail      *avail;
131                 struct vring_packed_desc_event *driver_event;
132         };
133         union {
134                 struct vring_used       *used;
135                 struct vring_packed_desc_event *device_event;
136         };
137         uint32_t                size;
138
139         uint16_t                last_avail_idx;
140         uint16_t                last_used_idx;
141         /* Last used index we notify to front end. */
142         uint16_t                signalled_used;
143         bool                    signalled_used_valid;
144 #define VIRTIO_INVALID_EVENTFD          (-1)
145 #define VIRTIO_UNINITIALIZED_EVENTFD    (-2)
146
147         /* Backend value to determine if device should started/stopped */
148         int                     backend;
149         int                     enabled;
150         int                     access_ok;
151         rte_spinlock_t          access_lock;
152
153         /* Used to notify the guest (trigger interrupt) */
154         int                     callfd;
155         /* Currently unused as polling mode is enabled */
156         int                     kickfd;
157
158         /* Physical address of used ring, for logging */
159         uint64_t                log_guest_addr;
160
161         /* inflight share memory info */
162         union {
163                 struct rte_vhost_inflight_info_split *inflight_split;
164                 struct rte_vhost_inflight_info_packed *inflight_packed;
165         };
166         struct rte_vhost_resubmit_info *resubmit_inflight;
167         uint64_t                global_counter;
168
169         uint16_t                nr_zmbuf;
170         uint16_t                zmbuf_size;
171         uint16_t                last_zmbuf_idx;
172         struct zcopy_mbuf       *zmbufs;
173         struct zcopy_mbuf_list  zmbuf_list;
174
175         union {
176                 struct vring_used_elem  *shadow_used_split;
177                 struct vring_used_elem_packed *shadow_used_packed;
178         };
179         uint16_t                shadow_used_idx;
180         /* Record packed ring enqueue latest desc cache aligned index */
181         uint16_t                shadow_aligned_idx;
182         struct vhost_vring_addr ring_addrs;
183
184         struct batch_copy_elem  *batch_copy_elems;
185         uint16_t                batch_copy_nb_elems;
186         bool                    used_wrap_counter;
187         bool                    avail_wrap_counter;
188
189         struct log_cache_entry log_cache[VHOST_LOG_CACHE_NR];
190         uint16_t log_cache_nb_elem;
191
192         rte_rwlock_t    iotlb_lock;
193         rte_rwlock_t    iotlb_pending_lock;
194         struct rte_mempool *iotlb_pool;
195         TAILQ_HEAD(, vhost_iotlb_entry) iotlb_list;
196         int                             iotlb_cache_nr;
197         TAILQ_HEAD(, vhost_iotlb_entry) iotlb_pending_list;
198 } __rte_cache_aligned;
199
200 /* Old kernels have no such macros defined */
201 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
202  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
203 #endif
204
205 #ifndef VIRTIO_NET_F_MQ
206  #define VIRTIO_NET_F_MQ                22
207 #endif
208
209 #define VHOST_MAX_VRING                 0x100
210 #define VHOST_MAX_QUEUE_PAIRS           0x80
211
212 #ifndef VIRTIO_NET_F_MTU
213  #define VIRTIO_NET_F_MTU 3
214 #endif
215
216 #ifndef VIRTIO_F_ANY_LAYOUT
217  #define VIRTIO_F_ANY_LAYOUT            27
218 #endif
219
220 /* Declare IOMMU related bits for older kernels */
221 #ifndef VIRTIO_F_IOMMU_PLATFORM
222
223 #define VIRTIO_F_IOMMU_PLATFORM 33
224
225 struct vhost_iotlb_msg {
226         __u64 iova;
227         __u64 size;
228         __u64 uaddr;
229 #define VHOST_ACCESS_RO      0x1
230 #define VHOST_ACCESS_WO      0x2
231 #define VHOST_ACCESS_RW      0x3
232         __u8 perm;
233 #define VHOST_IOTLB_MISS           1
234 #define VHOST_IOTLB_UPDATE         2
235 #define VHOST_IOTLB_INVALIDATE     3
236 #define VHOST_IOTLB_ACCESS_FAIL    4
237         __u8 type;
238 };
239
240 #define VHOST_IOTLB_MSG 0x1
241
242 struct vhost_msg {
243         int type;
244         union {
245                 struct vhost_iotlb_msg iotlb;
246                 __u8 padding[64];
247         };
248 };
249 #endif
250
251 /*
252  * Define virtio 1.0 for older kernels
253  */
254 #ifndef VIRTIO_F_VERSION_1
255  #define VIRTIO_F_VERSION_1 32
256 #endif
257
258 /* Declare packed ring related bits for older kernels */
259 #ifndef VIRTIO_F_RING_PACKED
260
261 #define VIRTIO_F_RING_PACKED 34
262
263 struct vring_packed_desc {
264         uint64_t addr;
265         uint32_t len;
266         uint16_t id;
267         uint16_t flags;
268 };
269
270 struct vring_packed_desc_event {
271         uint16_t off_wrap;
272         uint16_t flags;
273 };
274 #endif
275
276 /*
277  * Declare below packed ring defines unconditionally
278  * as Kernel header might use different names.
279  */
280 #define VRING_DESC_F_AVAIL      (1ULL << 7)
281 #define VRING_DESC_F_USED       (1ULL << 15)
282
283 #define VRING_EVENT_F_ENABLE 0x0
284 #define VRING_EVENT_F_DISABLE 0x1
285 #define VRING_EVENT_F_DESC 0x2
286
287 /*
288  * Available and used descs are in same order
289  */
290 #ifndef VIRTIO_F_IN_ORDER
291 #define VIRTIO_F_IN_ORDER      35
292 #endif
293
294 /* Features supported by this builtin vhost-user net driver. */
295 #define VIRTIO_NET_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
296                                 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
297                                 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
298                                 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
299                                 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
300                                 (1ULL << VIRTIO_NET_F_MQ)      | \
301                                 (1ULL << VIRTIO_F_VERSION_1)   | \
302                                 (1ULL << VHOST_F_LOG_ALL)      | \
303                                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
304                                 (1ULL << VIRTIO_NET_F_GSO) | \
305                                 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
306                                 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
307                                 (1ULL << VIRTIO_NET_F_HOST_UFO) | \
308                                 (1ULL << VIRTIO_NET_F_HOST_ECN) | \
309                                 (1ULL << VIRTIO_NET_F_CSUM)    | \
310                                 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
311                                 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
312                                 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
313                                 (1ULL << VIRTIO_NET_F_GUEST_UFO) | \
314                                 (1ULL << VIRTIO_NET_F_GUEST_ECN) | \
315                                 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
316                                 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \
317                                 (1ULL << VIRTIO_NET_F_MTU)  | \
318                                 (1ULL << VIRTIO_F_IN_ORDER) | \
319                                 (1ULL << VIRTIO_F_IOMMU_PLATFORM) | \
320                                 (1ULL << VIRTIO_F_RING_PACKED))
321
322
323 struct guest_page {
324         uint64_t guest_phys_addr;
325         uint64_t host_phys_addr;
326         uint64_t size;
327 };
328
329 struct inflight_mem_info {
330         int             fd;
331         void            *addr;
332         uint64_t        size;
333 };
334
335 /**
336  * Device structure contains all configuration information relating
337  * to the device.
338  */
339 struct virtio_net {
340         /* Frontend (QEMU) memory and memory region information */
341         struct rte_vhost_memory *mem;
342         uint64_t                features;
343         uint64_t                protocol_features;
344         int                     vid;
345         uint32_t                flags;
346         uint16_t                vhost_hlen;
347         /* to tell if we need broadcast rarp packet */
348         rte_atomic16_t          broadcast_rarp;
349         uint32_t                nr_vring;
350         int                     dequeue_zero_copy;
351         int                     extbuf;
352         int                     linearbuf;
353         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
354         struct inflight_mem_info *inflight_info;
355 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
356         char                    ifname[IF_NAME_SZ];
357         uint64_t                log_size;
358         uint64_t                log_base;
359         uint64_t                log_addr;
360         struct rte_ether_addr   mac;
361         uint16_t                mtu;
362
363         struct vhost_device_ops const *notify_ops;
364
365         uint32_t                nr_guest_pages;
366         uint32_t                max_guest_pages;
367         struct guest_page       *guest_pages;
368
369         int                     slave_req_fd;
370         rte_spinlock_t          slave_req_lock;
371
372         int                     postcopy_ufd;
373         int                     postcopy_listening;
374
375         /*
376          * Device id to identify a specific backend device.
377          * It's set to -1 for the default software implementation.
378          */
379         int                     vdpa_dev_id;
380
381         /* context data for the external message handlers */
382         void                    *extern_data;
383         /* pre and post vhost user message handlers for the device */
384         struct rte_vhost_user_extern_ops extern_ops;
385 } __rte_cache_aligned;
386
387 static __rte_always_inline bool
388 vq_is_packed(struct virtio_net *dev)
389 {
390         return dev->features & (1ull << VIRTIO_F_RING_PACKED);
391 }
392
393 static inline bool
394 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
395 {
396         uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
397
398         return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
399                 wrap_counter != !!(flags & VRING_DESC_F_USED);
400 }
401
402 static inline void
403 vq_inc_last_used_packed(struct vhost_virtqueue *vq, uint16_t num)
404 {
405         vq->last_used_idx += num;
406         if (vq->last_used_idx >= vq->size) {
407                 vq->used_wrap_counter ^= 1;
408                 vq->last_used_idx -= vq->size;
409         }
410 }
411
412 static inline void
413 vq_inc_last_avail_packed(struct vhost_virtqueue *vq, uint16_t num)
414 {
415         vq->last_avail_idx += num;
416         if (vq->last_avail_idx >= vq->size) {
417                 vq->avail_wrap_counter ^= 1;
418                 vq->last_avail_idx -= vq->size;
419         }
420 }
421
422 void __vhost_log_cache_write(struct virtio_net *dev,
423                 struct vhost_virtqueue *vq,
424                 uint64_t addr, uint64_t len);
425 void __vhost_log_cache_write_iova(struct virtio_net *dev,
426                 struct vhost_virtqueue *vq,
427                 uint64_t iova, uint64_t len);
428 void __vhost_log_cache_sync(struct virtio_net *dev,
429                 struct vhost_virtqueue *vq);
430 void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len);
431 void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
432                             uint64_t iova, uint64_t len);
433
434 static __rte_always_inline void
435 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
436 {
437         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
438                 __vhost_log_write(dev, addr, len);
439 }
440
441 static __rte_always_inline void
442 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
443 {
444         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
445                 __vhost_log_cache_sync(dev, vq);
446 }
447
448 static __rte_always_inline void
449 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
450                         uint64_t addr, uint64_t len)
451 {
452         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
453                 __vhost_log_cache_write(dev, vq, addr, len);
454 }
455
456 static __rte_always_inline void
457 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
458                         uint64_t offset, uint64_t len)
459 {
460         vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, len);
461 }
462
463 static __rte_always_inline void
464 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
465                      uint64_t offset, uint64_t len)
466 {
467         vhost_log_write(dev, vq->log_guest_addr + offset, len);
468 }
469
470 static __rte_always_inline void
471 vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
472                            uint64_t iova, uint64_t len)
473 {
474         if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
475                 return;
476
477         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
478                 __vhost_log_cache_write_iova(dev, vq, iova, len);
479         else
480                 __vhost_log_cache_write(dev, vq, iova, len);
481 }
482
483 static __rte_always_inline void
484 vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
485                            uint64_t iova, uint64_t len)
486 {
487         if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
488                 return;
489
490         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
491                 __vhost_log_write_iova(dev, vq, iova, len);
492         else
493                 __vhost_log_write(dev, iova, len);
494 }
495
496 /* Macros for printing using RTE_LOG */
497 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
498 #define RTE_LOGTYPE_VHOST_DATA   RTE_LOGTYPE_USER1
499
500 #ifdef RTE_LIBRTE_VHOST_DEBUG
501 #define VHOST_MAX_PRINT_BUFF 6072
502 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
503         RTE_LOG(DEBUG, log_type, fmt, ##args)
504 #define PRINT_PACKET(device, addr, size, header) do { \
505         char *pkt_addr = (char *)(addr); \
506         unsigned int index; \
507         char packet[VHOST_MAX_PRINT_BUFF]; \
508         \
509         if ((header)) \
510                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
511         else \
512                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
513         for (index = 0; index < (size); index++) { \
514                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
515                         "%02hhx ", pkt_addr[index]); \
516         } \
517         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
518         \
519         VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
520 } while (0)
521 #else
522 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
523 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
524 #endif
525
526 extern uint64_t VHOST_FEATURES;
527 #define MAX_VHOST_DEVICE        1024
528 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
529
530 /* Convert guest physical address to host physical address */
531 static __rte_always_inline rte_iova_t
532 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
533 {
534         uint32_t i;
535         struct guest_page *page;
536
537         for (i = 0; i < dev->nr_guest_pages; i++) {
538                 page = &dev->guest_pages[i];
539
540                 if (gpa >= page->guest_phys_addr &&
541                     gpa + size < page->guest_phys_addr + page->size) {
542                         return gpa - page->guest_phys_addr +
543                                page->host_phys_addr;
544                 }
545         }
546
547         return 0;
548 }
549
550 static __rte_always_inline uint64_t
551 hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len)
552 {
553         struct rte_vhost_mem_region *r;
554         uint32_t i;
555
556         if (unlikely(!dev || !dev->mem))
557                 return 0;
558
559         for (i = 0; i < dev->mem->nregions; i++) {
560                 r = &dev->mem->regions[i];
561
562                 if (vva >= r->host_user_addr &&
563                     vva + len <  r->host_user_addr + r->size) {
564                         return r->guest_phys_addr + vva - r->host_user_addr;
565                 }
566         }
567         return 0;
568 }
569
570 static __rte_always_inline struct virtio_net *
571 get_device(int vid)
572 {
573         struct virtio_net *dev = vhost_devices[vid];
574
575         if (unlikely(!dev)) {
576                 RTE_LOG(ERR, VHOST_CONFIG,
577                         "(%d) device not found.\n", vid);
578         }
579
580         return dev;
581 }
582
583 int vhost_new_device(void);
584 void cleanup_device(struct virtio_net *dev, int destroy);
585 void reset_device(struct virtio_net *dev);
586 void vhost_destroy_device(int);
587 void vhost_destroy_device_notify(struct virtio_net *dev);
588
589 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
590 void cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq);
591 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
592
593 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
594
595 void vhost_attach_vdpa_device(int vid, int did);
596
597 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
598 void vhost_enable_dequeue_zero_copy(int vid);
599 void vhost_set_builtin_virtio_net(int vid, bool enable);
600 void vhost_enable_extbuf(int vid);
601 void vhost_enable_linearbuf(int vid);
602
603 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
604
605 /*
606  * Backend-specific cleanup.
607  *
608  * TODO: fix it; we have one backend now
609  */
610 void vhost_backend_cleanup(struct virtio_net *dev);
611
612 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
613                         uint64_t iova, uint64_t *len, uint8_t perm);
614 void *vhost_alloc_copy_ind_table(struct virtio_net *dev,
615                         struct vhost_virtqueue *vq,
616                         uint64_t desc_addr, uint64_t desc_len);
617 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
618 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
619
620 static __rte_always_inline uint64_t
621 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
622                         uint64_t iova, uint64_t *len, uint8_t perm)
623 {
624         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
625                 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
626
627         return __vhost_iova_to_vva(dev, vq, iova, len, perm);
628 }
629
630 #define vhost_avail_event(vr) \
631         (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
632 #define vhost_used_event(vr) \
633         (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
634
635 /*
636  * The following is used with VIRTIO_RING_F_EVENT_IDX.
637  * Assuming a given event_idx value from the other size, if we have
638  * just incremented index from old to new_idx, should we trigger an
639  * event?
640  */
641 static __rte_always_inline int
642 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
643 {
644         return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
645 }
646
647 static __rte_always_inline void
648 vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
649 {
650         /* Flush used->idx update before we read avail->flags. */
651         rte_smp_mb();
652
653         /* Don't kick guest if we don't reach index specified by guest. */
654         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
655                 uint16_t old = vq->signalled_used;
656                 uint16_t new = vq->last_used_idx;
657                 bool signalled_used_valid = vq->signalled_used_valid;
658
659                 vq->signalled_used = new;
660                 vq->signalled_used_valid = true;
661
662                 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
663                         __func__,
664                         vhost_used_event(vq),
665                         old, new);
666
667                 if ((vhost_need_event(vhost_used_event(vq), new, old) &&
668                                         (vq->callfd >= 0)) ||
669                                 unlikely(!signalled_used_valid)) {
670                         eventfd_write(vq->callfd, (eventfd_t) 1);
671                         if (dev->notify_ops->guest_notified)
672                                 dev->notify_ops->guest_notified(dev->vid);
673                 }
674         } else {
675                 /* Kick the guest if necessary. */
676                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
677                                 && (vq->callfd >= 0)) {
678                         eventfd_write(vq->callfd, (eventfd_t)1);
679                         if (dev->notify_ops->guest_notified)
680                                 dev->notify_ops->guest_notified(dev->vid);
681                 }
682         }
683 }
684
685 static __rte_always_inline void
686 vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
687 {
688         uint16_t old, new, off, off_wrap;
689         bool signalled_used_valid, kick = false;
690
691         /* Flush used desc update. */
692         rte_smp_mb();
693
694         if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
695                 if (vq->driver_event->flags !=
696                                 VRING_EVENT_F_DISABLE)
697                         kick = true;
698                 goto kick;
699         }
700
701         old = vq->signalled_used;
702         new = vq->last_used_idx;
703         vq->signalled_used = new;
704         signalled_used_valid = vq->signalled_used_valid;
705         vq->signalled_used_valid = true;
706
707         if (vq->driver_event->flags != VRING_EVENT_F_DESC) {
708                 if (vq->driver_event->flags != VRING_EVENT_F_DISABLE)
709                         kick = true;
710                 goto kick;
711         }
712
713         if (unlikely(!signalled_used_valid)) {
714                 kick = true;
715                 goto kick;
716         }
717
718         rte_smp_rmb();
719
720         off_wrap = vq->driver_event->off_wrap;
721         off = off_wrap & ~(1 << 15);
722
723         if (new <= old)
724                 old -= vq->size;
725
726         if (vq->used_wrap_counter != off_wrap >> 15)
727                 off -= vq->size;
728
729         if (vhost_need_event(off, new, old))
730                 kick = true;
731 kick:
732         if (kick) {
733                 eventfd_write(vq->callfd, (eventfd_t)1);
734                 if (dev->notify_ops->guest_notified)
735                         dev->notify_ops->guest_notified(dev->vid);
736         }
737 }
738
739 static __rte_always_inline void
740 free_ind_table(void *idesc)
741 {
742         rte_free(idesc);
743 }
744
745 static __rte_always_inline void
746 restore_mbuf(struct rte_mbuf *m)
747 {
748         uint32_t mbuf_size, priv_size;
749
750         while (m) {
751                 priv_size = rte_pktmbuf_priv_size(m->pool);
752                 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
753                 /* start of buffer is after mbuf structure and priv data */
754
755                 m->buf_addr = (char *)m + mbuf_size;
756                 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
757                 m = m->next;
758         }
759 }
760
761 static __rte_always_inline bool
762 mbuf_is_consumed(struct rte_mbuf *m)
763 {
764         while (m) {
765                 if (rte_mbuf_refcnt_read(m) > 1)
766                         return false;
767                 m = m->next;
768         }
769
770         return true;
771 }
772
773 static __rte_always_inline void
774 put_zmbuf(struct zcopy_mbuf *zmbuf)
775 {
776         zmbuf->in_use = 0;
777 }
778
779 #endif /* _VHOST_NET_CDEV_H_ */