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