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