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