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