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