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