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