vhost: checkout resubmit inflight information
[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         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
320         struct inflight_mem_info *inflight_info;
321 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
322         char                    ifname[IF_NAME_SZ];
323         uint64_t                log_size;
324         uint64_t                log_base;
325         uint64_t                log_addr;
326         struct rte_ether_addr   mac;
327         uint16_t                mtu;
328
329         struct vhost_device_ops const *notify_ops;
330
331         uint32_t                nr_guest_pages;
332         uint32_t                max_guest_pages;
333         struct guest_page       *guest_pages;
334
335         int                     slave_req_fd;
336         rte_spinlock_t          slave_req_lock;
337
338         int                     postcopy_ufd;
339         int                     postcopy_listening;
340
341         /*
342          * Device id to identify a specific backend device.
343          * It's set to -1 for the default software implementation.
344          */
345         int                     vdpa_dev_id;
346
347         /* context data for the external message handlers */
348         void                    *extern_data;
349         /* pre and post vhost user message handlers for the device */
350         struct rte_vhost_user_extern_ops extern_ops;
351 } __rte_cache_aligned;
352
353 static __rte_always_inline bool
354 vq_is_packed(struct virtio_net *dev)
355 {
356         return dev->features & (1ull << VIRTIO_F_RING_PACKED);
357 }
358
359 static inline bool
360 desc_is_avail(struct vring_packed_desc *desc, bool wrap_counter)
361 {
362         uint16_t flags = __atomic_load_n(&desc->flags, __ATOMIC_ACQUIRE);
363
364         return wrap_counter == !!(flags & VRING_DESC_F_AVAIL) &&
365                 wrap_counter != !!(flags & VRING_DESC_F_USED);
366 }
367
368 void __vhost_log_cache_write(struct virtio_net *dev,
369                 struct vhost_virtqueue *vq,
370                 uint64_t addr, uint64_t len);
371 void __vhost_log_cache_write_iova(struct virtio_net *dev,
372                 struct vhost_virtqueue *vq,
373                 uint64_t iova, uint64_t len);
374 void __vhost_log_cache_sync(struct virtio_net *dev,
375                 struct vhost_virtqueue *vq);
376 void __vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len);
377 void __vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
378                             uint64_t iova, uint64_t len);
379
380 static __rte_always_inline void
381 vhost_log_write(struct virtio_net *dev, uint64_t addr, uint64_t len)
382 {
383         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
384                 __vhost_log_write(dev, addr, len);
385 }
386
387 static __rte_always_inline void
388 vhost_log_cache_sync(struct virtio_net *dev, struct vhost_virtqueue *vq)
389 {
390         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
391                 __vhost_log_cache_sync(dev, vq);
392 }
393
394 static __rte_always_inline void
395 vhost_log_cache_write(struct virtio_net *dev, struct vhost_virtqueue *vq,
396                         uint64_t addr, uint64_t len)
397 {
398         if (unlikely(dev->features & (1ULL << VHOST_F_LOG_ALL)))
399                 __vhost_log_cache_write(dev, vq, addr, len);
400 }
401
402 static __rte_always_inline void
403 vhost_log_cache_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
404                         uint64_t offset, uint64_t len)
405 {
406         vhost_log_cache_write(dev, vq, vq->log_guest_addr + offset, len);
407 }
408
409 static __rte_always_inline void
410 vhost_log_used_vring(struct virtio_net *dev, struct vhost_virtqueue *vq,
411                      uint64_t offset, uint64_t len)
412 {
413         vhost_log_write(dev, vq->log_guest_addr + offset, len);
414 }
415
416 static __rte_always_inline void
417 vhost_log_cache_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
418                            uint64_t iova, uint64_t len)
419 {
420         if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
421                 return;
422
423         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
424                 __vhost_log_cache_write_iova(dev, vq, iova, len);
425         else
426                 __vhost_log_cache_write(dev, vq, iova, len);
427 }
428
429 static __rte_always_inline void
430 vhost_log_write_iova(struct virtio_net *dev, struct vhost_virtqueue *vq,
431                            uint64_t iova, uint64_t len)
432 {
433         if (likely(!(dev->features & (1ULL << VHOST_F_LOG_ALL))))
434                 return;
435
436         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
437                 __vhost_log_write_iova(dev, vq, iova, len);
438         else
439                 __vhost_log_write(dev, iova, len);
440 }
441
442 /* Macros for printing using RTE_LOG */
443 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
444 #define RTE_LOGTYPE_VHOST_DATA   RTE_LOGTYPE_USER1
445
446 #ifdef RTE_LIBRTE_VHOST_DEBUG
447 #define VHOST_MAX_PRINT_BUFF 6072
448 #define VHOST_LOG_DEBUG(log_type, fmt, args...) \
449         RTE_LOG(DEBUG, log_type, fmt, ##args)
450 #define PRINT_PACKET(device, addr, size, header) do { \
451         char *pkt_addr = (char *)(addr); \
452         unsigned int index; \
453         char packet[VHOST_MAX_PRINT_BUFF]; \
454         \
455         if ((header)) \
456                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
457         else \
458                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
459         for (index = 0; index < (size); index++) { \
460                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
461                         "%02hhx ", pkt_addr[index]); \
462         } \
463         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
464         \
465         VHOST_LOG_DEBUG(VHOST_DATA, "%s", packet); \
466 } while (0)
467 #else
468 #define VHOST_LOG_DEBUG(log_type, fmt, args...) do {} while (0)
469 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
470 #endif
471
472 extern uint64_t VHOST_FEATURES;
473 #define MAX_VHOST_DEVICE        1024
474 extern struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
475
476 /* Convert guest physical address to host physical address */
477 static __rte_always_inline rte_iova_t
478 gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
479 {
480         uint32_t i;
481         struct guest_page *page;
482
483         for (i = 0; i < dev->nr_guest_pages; i++) {
484                 page = &dev->guest_pages[i];
485
486                 if (gpa >= page->guest_phys_addr &&
487                     gpa + size < page->guest_phys_addr + page->size) {
488                         return gpa - page->guest_phys_addr +
489                                page->host_phys_addr;
490                 }
491         }
492
493         return 0;
494 }
495
496 static __rte_always_inline uint64_t
497 hva_to_gpa(struct virtio_net *dev, uint64_t vva, uint64_t len)
498 {
499         struct rte_vhost_mem_region *r;
500         uint32_t i;
501
502         if (unlikely(!dev || !dev->mem))
503                 return 0;
504
505         for (i = 0; i < dev->mem->nregions; i++) {
506                 r = &dev->mem->regions[i];
507
508                 if (vva >= r->host_user_addr &&
509                     vva + len <  r->host_user_addr + r->size) {
510                         return r->guest_phys_addr + vva - r->host_user_addr;
511                 }
512         }
513         return 0;
514 }
515
516 static __rte_always_inline struct virtio_net *
517 get_device(int vid)
518 {
519         struct virtio_net *dev = vhost_devices[vid];
520
521         if (unlikely(!dev)) {
522                 RTE_LOG(ERR, VHOST_CONFIG,
523                         "(%d) device not found.\n", vid);
524         }
525
526         return dev;
527 }
528
529 int vhost_new_device(void);
530 void cleanup_device(struct virtio_net *dev, int destroy);
531 void reset_device(struct virtio_net *dev);
532 void vhost_destroy_device(int);
533 void vhost_destroy_device_notify(struct virtio_net *dev);
534
535 void cleanup_vq(struct vhost_virtqueue *vq, int destroy);
536 void cleanup_vq_inflight(struct virtio_net *dev, struct vhost_virtqueue *vq);
537 void free_vq(struct virtio_net *dev, struct vhost_virtqueue *vq);
538
539 int alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx);
540
541 void vhost_attach_vdpa_device(int vid, int did);
542
543 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
544 void vhost_enable_dequeue_zero_copy(int vid);
545 void vhost_set_builtin_virtio_net(int vid, bool enable);
546
547 struct vhost_device_ops const *vhost_driver_callback_get(const char *path);
548
549 /*
550  * Backend-specific cleanup.
551  *
552  * TODO: fix it; we have one backend now
553  */
554 void vhost_backend_cleanup(struct virtio_net *dev);
555
556 uint64_t __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
557                         uint64_t iova, uint64_t *len, uint8_t perm);
558 void *vhost_alloc_copy_ind_table(struct virtio_net *dev,
559                         struct vhost_virtqueue *vq,
560                         uint64_t desc_addr, uint64_t desc_len);
561 int vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq);
562 void vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq);
563
564 static __rte_always_inline uint64_t
565 vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
566                         uint64_t iova, uint64_t *len, uint8_t perm)
567 {
568         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
569                 return rte_vhost_va_from_guest_pa(dev->mem, iova, len);
570
571         return __vhost_iova_to_vva(dev, vq, iova, len, perm);
572 }
573
574 #define vhost_avail_event(vr) \
575         (*(volatile uint16_t*)&(vr)->used->ring[(vr)->size])
576 #define vhost_used_event(vr) \
577         (*(volatile uint16_t*)&(vr)->avail->ring[(vr)->size])
578
579 /*
580  * The following is used with VIRTIO_RING_F_EVENT_IDX.
581  * Assuming a given event_idx value from the other size, if we have
582  * just incremented index from old to new_idx, should we trigger an
583  * event?
584  */
585 static __rte_always_inline int
586 vhost_need_event(uint16_t event_idx, uint16_t new_idx, uint16_t old)
587 {
588         return (uint16_t)(new_idx - event_idx - 1) < (uint16_t)(new_idx - old);
589 }
590
591 static __rte_always_inline void
592 vhost_vring_call_split(struct virtio_net *dev, struct vhost_virtqueue *vq)
593 {
594         /* Flush used->idx update before we read avail->flags. */
595         rte_smp_mb();
596
597         /* Don't kick guest if we don't reach index specified by guest. */
598         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX)) {
599                 uint16_t old = vq->signalled_used;
600                 uint16_t new = vq->last_used_idx;
601                 bool signalled_used_valid = vq->signalled_used_valid;
602
603                 vq->signalled_used = new;
604                 vq->signalled_used_valid = true;
605
606                 VHOST_LOG_DEBUG(VHOST_DATA, "%s: used_event_idx=%d, old=%d, new=%d\n",
607                         __func__,
608                         vhost_used_event(vq),
609                         old, new);
610
611                 if ((vhost_need_event(vhost_used_event(vq), new, old) &&
612                                         (vq->callfd >= 0)) ||
613                                 unlikely(!signalled_used_valid)) {
614                         eventfd_write(vq->callfd, (eventfd_t) 1);
615                         if (dev->notify_ops->guest_notified)
616                                 dev->notify_ops->guest_notified(dev->vid);
617                 }
618         } else {
619                 /* Kick the guest if necessary. */
620                 if (!(vq->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
621                                 && (vq->callfd >= 0)) {
622                         eventfd_write(vq->callfd, (eventfd_t)1);
623                         if (dev->notify_ops->guest_notified)
624                                 dev->notify_ops->guest_notified(dev->vid);
625                 }
626         }
627 }
628
629 static __rte_always_inline void
630 vhost_vring_call_packed(struct virtio_net *dev, struct vhost_virtqueue *vq)
631 {
632         uint16_t old, new, off, off_wrap;
633         bool signalled_used_valid, kick = false;
634
635         /* Flush used desc update. */
636         rte_smp_mb();
637
638         if (!(dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))) {
639                 if (vq->driver_event->flags !=
640                                 VRING_EVENT_F_DISABLE)
641                         kick = true;
642                 goto kick;
643         }
644
645         old = vq->signalled_used;
646         new = vq->last_used_idx;
647         vq->signalled_used = new;
648         signalled_used_valid = vq->signalled_used_valid;
649         vq->signalled_used_valid = true;
650
651         if (vq->driver_event->flags != VRING_EVENT_F_DESC) {
652                 if (vq->driver_event->flags != VRING_EVENT_F_DISABLE)
653                         kick = true;
654                 goto kick;
655         }
656
657         if (unlikely(!signalled_used_valid)) {
658                 kick = true;
659                 goto kick;
660         }
661
662         rte_smp_rmb();
663
664         off_wrap = vq->driver_event->off_wrap;
665         off = off_wrap & ~(1 << 15);
666
667         if (new <= old)
668                 old -= vq->size;
669
670         if (vq->used_wrap_counter != off_wrap >> 15)
671                 off -= vq->size;
672
673         if (vhost_need_event(off, new, old))
674                 kick = true;
675 kick:
676         if (kick) {
677                 eventfd_write(vq->callfd, (eventfd_t)1);
678                 if (dev->notify_ops->guest_notified)
679                         dev->notify_ops->guest_notified(dev->vid);
680         }
681 }
682
683 static __rte_always_inline void
684 free_ind_table(void *idesc)
685 {
686         rte_free(idesc);
687 }
688
689 static __rte_always_inline void
690 restore_mbuf(struct rte_mbuf *m)
691 {
692         uint32_t mbuf_size, priv_size;
693
694         while (m) {
695                 priv_size = rte_pktmbuf_priv_size(m->pool);
696                 mbuf_size = sizeof(struct rte_mbuf) + priv_size;
697                 /* start of buffer is after mbuf structure and priv data */
698
699                 m->buf_addr = (char *)m + mbuf_size;
700                 m->buf_iova = rte_mempool_virt2iova(m) + mbuf_size;
701                 m = m->next;
702         }
703 }
704
705 static __rte_always_inline bool
706 mbuf_is_consumed(struct rte_mbuf *m)
707 {
708         while (m) {
709                 if (rte_mbuf_refcnt_read(m) > 1)
710                         return false;
711                 m = m->next;
712         }
713
714         return true;
715 }
716
717 static __rte_always_inline void
718 put_zmbuf(struct zcopy_mbuf *zmbuf)
719 {
720         zmbuf->in_use = 0;
721 }
722
723 #endif /* _VHOST_NET_CDEV_H_ */