vdpa/mlx5: add task ring for multi-thread management
[dpdk.git] / lib / vhost / rte_vhost.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4
5 #ifndef _RTE_VHOST_H_
6 #define _RTE_VHOST_H_
7
8 /**
9  * @file
10  * Interface to vhost-user
11  */
12
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <sys/eventfd.h>
16
17 #include <rte_memory.h>
18 #include <rte_mempool.h>
19
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23
24 #ifndef __cplusplus
25 /* These are not C++-aware. */
26 #include <linux/vhost.h>
27 #include <linux/virtio_ring.h>
28 #include <linux/virtio_net.h>
29 #endif
30
31 #define RTE_VHOST_USER_CLIENT           (1ULL << 0)
32 #define RTE_VHOST_USER_NO_RECONNECT     (1ULL << 1)
33 #define RTE_VHOST_USER_RESERVED_1       (1ULL << 2)
34 #define RTE_VHOST_USER_IOMMU_SUPPORT    (1ULL << 3)
35 #define RTE_VHOST_USER_POSTCOPY_SUPPORT         (1ULL << 4)
36 /* support mbuf with external buffer attached */
37 #define RTE_VHOST_USER_EXTBUF_SUPPORT   (1ULL << 5)
38 /* support only linear buffers (no chained mbufs) */
39 #define RTE_VHOST_USER_LINEARBUF_SUPPORT        (1ULL << 6)
40 #define RTE_VHOST_USER_ASYNC_COPY       (1ULL << 7)
41 #define RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS   (1ULL << 8)
42 #define RTE_VHOST_USER_NET_STATS_ENABLE (1ULL << 9)
43
44 /* Features. */
45 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
46  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
47 #endif
48
49 #ifndef VIRTIO_NET_F_MQ
50  #define VIRTIO_NET_F_MQ                22
51 #endif
52
53 #ifndef VIRTIO_NET_F_MTU
54  #define VIRTIO_NET_F_MTU 3
55 #endif
56
57 #ifndef VIRTIO_F_ANY_LAYOUT
58  #define VIRTIO_F_ANY_LAYOUT            27
59 #endif
60
61 /** Protocol features. */
62 #ifndef VHOST_USER_PROTOCOL_F_MQ
63 #define VHOST_USER_PROTOCOL_F_MQ        0
64 #endif
65
66 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
67 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
68 #endif
69
70 #ifndef VHOST_USER_PROTOCOL_F_RARP
71 #define VHOST_USER_PROTOCOL_F_RARP      2
72 #endif
73
74 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
75 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
76 #endif
77
78 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
79 #define VHOST_USER_PROTOCOL_F_NET_MTU   4
80 #endif
81
82 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
83 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
84 #endif
85
86 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
87 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
88 #endif
89
90 #ifndef VHOST_USER_PROTOCOL_F_PAGEFAULT
91 #define VHOST_USER_PROTOCOL_F_PAGEFAULT 8
92 #endif
93
94 #ifndef VHOST_USER_PROTOCOL_F_CONFIG
95 #define VHOST_USER_PROTOCOL_F_CONFIG 9
96 #endif
97
98 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
99 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD 10
100 #endif
101
102 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
103 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
104 #endif
105
106 #ifndef VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
107 #define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 12
108 #endif
109
110 #ifndef VHOST_USER_PROTOCOL_F_STATUS
111 #define VHOST_USER_PROTOCOL_F_STATUS 16
112 #endif
113
114 /** Indicate whether protocol features negotiation is supported. */
115 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
116 #define VHOST_USER_F_PROTOCOL_FEATURES  30
117 #endif
118
119 #define RTE_MAX_VHOST_DEVICE    1024
120
121 #define RTE_VHOST_VDPA_DEVICE_TYPE_NET 0
122 #define RTE_VHOST_VDPA_DEVICE_TYPE_BLK 1
123
124 struct rte_vdpa_device;
125
126 /**
127  * Information relating to memory regions including offsets to
128  * addresses in QEMUs memory file.
129  */
130 struct rte_vhost_mem_region {
131         uint64_t guest_phys_addr;
132         uint64_t guest_user_addr;
133         uint64_t host_user_addr;
134         uint64_t size;
135         void     *mmap_addr;
136         uint64_t mmap_size;
137         int fd;
138 };
139
140 /**
141  * Memory structure includes region and mapping information.
142  */
143 struct rte_vhost_memory {
144         uint32_t nregions;
145         struct rte_vhost_mem_region regions[];
146 };
147
148 struct rte_vhost_inflight_desc_split {
149         uint8_t inflight;
150         uint8_t padding[5];
151         uint16_t next;
152         uint64_t counter;
153 };
154
155 struct rte_vhost_inflight_info_split {
156         uint64_t features;
157         uint16_t version;
158         uint16_t desc_num;
159         uint16_t last_inflight_io;
160         uint16_t used_idx;
161         struct rte_vhost_inflight_desc_split desc[];
162 };
163
164 struct rte_vhost_inflight_desc_packed {
165         uint8_t inflight;
166         uint8_t padding;
167         uint16_t next;
168         uint16_t last;
169         uint16_t num;
170         uint64_t counter;
171         uint16_t id;
172         uint16_t flags;
173         uint32_t len;
174         uint64_t addr;
175 };
176
177 struct rte_vhost_inflight_info_packed {
178         uint64_t features;
179         uint16_t version;
180         uint16_t desc_num;
181         uint16_t free_head;
182         uint16_t old_free_head;
183         uint16_t used_idx;
184         uint16_t old_used_idx;
185         uint8_t used_wrap_counter;
186         uint8_t old_used_wrap_counter;
187         uint8_t padding[7];
188         struct rte_vhost_inflight_desc_packed desc[];
189 };
190
191 struct rte_vhost_resubmit_desc {
192         uint16_t index;
193         uint64_t counter;
194 };
195
196 struct rte_vhost_resubmit_info {
197         struct rte_vhost_resubmit_desc *resubmit_list;
198         uint16_t resubmit_num;
199 };
200
201 struct rte_vhost_ring_inflight {
202         union {
203                 struct rte_vhost_inflight_info_split *inflight_split;
204                 struct rte_vhost_inflight_info_packed *inflight_packed;
205         };
206
207         struct rte_vhost_resubmit_info *resubmit_inflight;
208 };
209
210 struct rte_vhost_vring {
211         union {
212                 struct vring_desc *desc;
213                 struct vring_packed_desc *desc_packed;
214         };
215         union {
216                 struct vring_avail *avail;
217                 struct vring_packed_desc_event *driver_event;
218         };
219         union {
220                 struct vring_used *used;
221                 struct vring_packed_desc_event *device_event;
222         };
223         uint64_t                log_guest_addr;
224
225         /** Deprecated, use rte_vhost_vring_call() instead. */
226         int                     callfd;
227
228         int                     kickfd;
229         uint16_t                size;
230 };
231
232 /**
233  * Possible results of the vhost user message handling callbacks
234  */
235 enum rte_vhost_msg_result {
236         /* Message handling failed */
237         RTE_VHOST_MSG_RESULT_ERR = -1,
238         /* Message handling successful */
239         RTE_VHOST_MSG_RESULT_OK =  0,
240         /* Message handling successful and reply prepared */
241         RTE_VHOST_MSG_RESULT_REPLY =  1,
242         /* Message not handled */
243         RTE_VHOST_MSG_RESULT_NOT_HANDLED,
244 };
245
246 /**
247  * Function prototype for the vhost backend to handle specific vhost user
248  * messages.
249  *
250  * @param vid
251  *  vhost device id
252  * @param msg
253  *  Message pointer.
254  * @return
255  *  RTE_VHOST_MSG_RESULT_OK on success,
256  *  RTE_VHOST_MSG_RESULT_REPLY on success with reply,
257  *  RTE_VHOST_MSG_RESULT_ERR on failure,
258  *  RTE_VHOST_MSG_RESULT_NOT_HANDLED if message was not handled.
259  */
260 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
261
262 /**
263  * Optional vhost user message handlers.
264  */
265 struct rte_vhost_user_extern_ops {
266         /* Called prior to the master message handling. */
267         rte_vhost_msg_handle pre_msg_handle;
268         /* Called after the master message handling. */
269         rte_vhost_msg_handle post_msg_handle;
270 };
271
272 /**
273  * Device and vring operations.
274  */
275 struct rte_vhost_device_ops {
276         int (*new_device)(int vid);             /**< Add device. */
277         void (*destroy_device)(int vid);        /**< Remove device. */
278
279         int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);     /**< triggered when a vring is enabled or disabled */
280
281         /**
282          * Features could be changed after the feature negotiation.
283          * For example, VHOST_F_LOG_ALL will be set/cleared at the
284          * start/end of live migration, respectively. This callback
285          * is used to inform the application on such change.
286          */
287         int (*features_changed)(int vid, uint64_t features);
288
289         int (*new_connection)(int vid);
290         void (*destroy_connection)(int vid);
291
292         /**
293          * This callback gets called each time a guest gets notified
294          * about waiting packets. This is the interrupt handling through
295          * the eventfd_write(callfd), which can be used for counting these
296          * "slow" syscalls.
297          */
298         void (*guest_notified)(int vid);
299
300         void *reserved[1]; /**< Reserved for future extension */
301 };
302
303 /**
304  * Power monitor condition.
305  */
306 struct rte_vhost_power_monitor_cond {
307         /**< Address to monitor for changes */
308         volatile void *addr;
309         /**< If the `mask` is non-zero, location pointed
310          *   to by `addr` will be read and masked, then
311          *   compared with this value.
312          */
313         uint64_t val;
314         /**< 64-bit mask to extract value read from `addr` */
315         uint64_t mask;
316         /**< Data size (in bytes) that will be read from the
317          *   monitored memory location (`addr`).
318          */
319         uint8_t size;
320         /**< If 1, and masked value that read from 'addr' equals
321          *   'val', the driver should skip core sleep. If 0, and
322          *  masked value that read from 'addr' does not equal 'val',
323          *  the driver should skip core sleep.
324          */
325         uint8_t match;
326 };
327
328 /** Maximum name length for the statistics counters */
329 #define RTE_VHOST_STATS_NAME_SIZE 64
330
331 /**
332  * Vhost virtqueue statistics structure
333  *
334  * This structure is used by rte_vhost_vring_stats_get() to provide
335  * virtqueue statistics to the calling application.
336  * It maps a name ID, corresponding to an index in the array returned
337  * by rte_vhost_vring_stats_get_names(), to a statistic value.
338  */
339 struct rte_vhost_stat {
340         uint64_t id;    /**< The index in xstats name array. */
341         uint64_t value; /**< The statistic counter value. */
342 };
343
344 /**
345  * Vhost virtqueue statistic name element
346  *
347  * This structure is used by rte_vhost_vring_stats_get_names() to
348  * provide virtqueue statistics names to the calling application.
349  */
350 struct rte_vhost_stat_name {
351         char name[RTE_VHOST_STATS_NAME_SIZE]; /**< The statistic name. */
352 };
353
354 /**
355  * Convert guest physical address to host virtual address
356  *
357  * This function is deprecated because unsafe.
358  * New rte_vhost_va_from_guest_pa() should be used instead to ensure
359  * guest physical ranges are fully and contiguously mapped into
360  * process virtual address space.
361  *
362  * @param mem
363  *  the guest memory regions
364  * @param gpa
365  *  the guest physical address for querying
366  * @return
367  *  the host virtual address on success, 0 on failure
368  */
369 __rte_deprecated
370 static __rte_always_inline uint64_t
371 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
372 {
373         struct rte_vhost_mem_region *reg;
374         uint32_t i;
375
376         for (i = 0; i < mem->nregions; i++) {
377                 reg = &mem->regions[i];
378                 if (gpa >= reg->guest_phys_addr &&
379                     gpa <  reg->guest_phys_addr + reg->size) {
380                         return gpa - reg->guest_phys_addr +
381                                reg->host_user_addr;
382                 }
383         }
384
385         return 0;
386 }
387
388 /**
389  * Convert guest physical address to host virtual address safely
390  *
391  * This variant of rte_vhost_gpa_to_vva() takes care all the
392  * requested length is mapped and contiguous in process address
393  * space.
394  *
395  * @param mem
396  *  the guest memory regions
397  * @param gpa
398  *  the guest physical address for querying
399  * @param len
400  *  the size of the requested area to map, updated with actual size mapped
401  * @return
402  *  the host virtual address on success, 0 on failure
403  */
404 static __rte_always_inline uint64_t
405 rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem,
406                                                    uint64_t gpa, uint64_t *len)
407 {
408         struct rte_vhost_mem_region *r;
409         uint32_t i;
410
411         for (i = 0; i < mem->nregions; i++) {
412                 r = &mem->regions[i];
413                 if (gpa >= r->guest_phys_addr &&
414                     gpa <  r->guest_phys_addr + r->size) {
415
416                         if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
417                                 *len = r->guest_phys_addr + r->size - gpa;
418
419                         return gpa - r->guest_phys_addr +
420                                r->host_user_addr;
421                 }
422         }
423         *len = 0;
424
425         return 0;
426 }
427
428 #define RTE_VHOST_NEED_LOG(features)    ((features) & (1ULL << VHOST_F_LOG_ALL))
429
430 /**
431  * Log the memory write start with given address.
432  *
433  * This function only need be invoked when the live migration starts.
434  * Therefore, we won't need call it at all in the most of time. For
435  * making the performance impact be minimum, it's suggested to do a
436  * check before calling it:
437  *
438  *        if (unlikely(RTE_VHOST_NEED_LOG(features)))
439  *                rte_vhost_log_write(vid, addr, len);
440  *
441  * @param vid
442  *  vhost device ID
443  * @param addr
444  *  the starting address for write (in guest physical address space)
445  * @param len
446  *  the length to write
447  */
448 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
449
450 /**
451  * Log the used ring update start at given offset.
452  *
453  * Same as rte_vhost_log_write, it's suggested to do a check before
454  * calling it:
455  *
456  *        if (unlikely(RTE_VHOST_NEED_LOG(features)))
457  *                rte_vhost_log_used_vring(vid, vring_idx, offset, len);
458  *
459  * @param vid
460  *  vhost device ID
461  * @param vring_idx
462  *  the vring index
463  * @param offset
464  *  the offset inside the used ring
465  * @param len
466  *  the length to write
467  */
468 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
469                               uint64_t offset, uint64_t len);
470
471 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
472
473 /**
474  * Register vhost driver. path could be different for multiple
475  * instance support.
476  */
477 int rte_vhost_driver_register(const char *path, uint64_t flags);
478
479 /* Unregister vhost driver. This is only meaningful to vhost user. */
480 int rte_vhost_driver_unregister(const char *path);
481
482 /**
483  * Set the vdpa device id, enforce single connection per socket
484  *
485  * @param path
486  *  The vhost-user socket file path
487  * @param dev
488  *  vDPA device pointer
489  * @return
490  *  0 on success, -1 on failure
491  */
492 int
493 rte_vhost_driver_attach_vdpa_device(const char *path,
494                 struct rte_vdpa_device *dev);
495
496 /**
497  * Unset the vdpa device id
498  *
499  * @param path
500  *  The vhost-user socket file path
501  * @return
502  *  0 on success, -1 on failure
503  */
504 int
505 rte_vhost_driver_detach_vdpa_device(const char *path);
506
507 /**
508  * Get the device id
509  *
510  * @param path
511  *  The vhost-user socket file path
512  * @return
513  *  vDPA device pointer, NULL on failure
514  */
515 struct rte_vdpa_device *
516 rte_vhost_driver_get_vdpa_device(const char *path);
517
518 /**
519  * Get the device type of the vdpa device.
520  *
521  * @param path
522  *  The vhost-user socket file path
523  * @param type
524  *  the device type of the vdpa device
525  * @return
526  *  0 on success, -1 on failure
527  */
528 __rte_experimental
529 int
530 rte_vhost_driver_get_vdpa_dev_type(const char *path, uint32_t *type);
531
532 /**
533  * Set the feature bits the vhost-user driver supports.
534  *
535  * @param path
536  *  The vhost-user socket file path
537  * @param features
538  *  Supported features
539  * @return
540  *  0 on success, -1 on failure
541  */
542 int rte_vhost_driver_set_features(const char *path, uint64_t features);
543
544 /**
545  * Enable vhost-user driver features.
546  *
547  * Note that
548  * - the param features should be a subset of the feature bits provided
549  *   by rte_vhost_driver_set_features().
550  * - it must be invoked before vhost-user negotiation starts.
551  *
552  * @param path
553  *  The vhost-user socket file path
554  * @param features
555  *  Features to enable
556  * @return
557  *  0 on success, -1 on failure
558  */
559 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
560
561 /**
562  * Disable vhost-user driver features.
563  *
564  * The two notes at rte_vhost_driver_enable_features() also apply here.
565  *
566  * @param path
567  *  The vhost-user socket file path
568  * @param features
569  *  Features to disable
570  * @return
571  *  0 on success, -1 on failure
572  */
573 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
574
575 /**
576  * Get the feature bits before feature negotiation.
577  *
578  * @param path
579  *  The vhost-user socket file path
580  * @param features
581  *  A pointer to store the queried feature bits
582  * @return
583  *  0 on success, -1 on failure
584  */
585 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
586
587 /**
588  * Set the protocol feature bits before feature negotiation.
589  *
590  * @param path
591  *  The vhost-user socket file path
592  * @param protocol_features
593  *  Supported protocol features
594  * @return
595  *  0 on success, -1 on failure
596  */
597 int
598 rte_vhost_driver_set_protocol_features(const char *path,
599                 uint64_t protocol_features);
600
601 /**
602  * Get the protocol feature bits before feature negotiation.
603  *
604  * @param path
605  *  The vhost-user socket file path
606  * @param protocol_features
607  *  A pointer to store the queried protocol feature bits
608  * @return
609  *  0 on success, -1 on failure
610  */
611 int
612 rte_vhost_driver_get_protocol_features(const char *path,
613                 uint64_t *protocol_features);
614
615 /**
616  * Get the queue number bits before feature negotiation.
617  *
618  * @param path
619  *  The vhost-user socket file path
620  * @param queue_num
621  *  A pointer to store the queried queue number bits
622  * @return
623  *  0 on success, -1 on failure
624  */
625 int
626 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
627
628 /**
629  * Get the feature bits after negotiation
630  *
631  * @param vid
632  *  Vhost device ID
633  * @param features
634  *  A pointer to store the queried feature bits
635  * @return
636  *  0 on success, -1 on failure
637  */
638 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
639
640 /**
641  * Get the protocol feature bits after negotiation
642  *
643  * @param vid
644  *  Vhost device ID
645  * @param protocol_features
646  *  A pointer to store the queried protocol feature bits
647  * @return
648  *  0 on success, -1 on failure
649  */
650 __rte_experimental
651 int
652 rte_vhost_get_negotiated_protocol_features(int vid,
653                                            uint64_t *protocol_features);
654
655 /* Register callbacks. */
656 int rte_vhost_driver_callback_register(const char *path,
657         struct rte_vhost_device_ops const * const ops);
658
659 /**
660  *
661  * Start the vhost-user driver.
662  *
663  * This function triggers the vhost-user negotiation.
664  *
665  * @param path
666  *  The vhost-user socket file path
667  * @return
668  *  0 on success, -1 on failure
669  */
670 int rte_vhost_driver_start(const char *path);
671
672 /**
673  * Get the MTU value of the device if set in QEMU.
674  *
675  * @param vid
676  *  virtio-net device ID
677  * @param mtu
678  *  The variable to store the MTU value
679  *
680  * @return
681  *  0: success
682  *  -EAGAIN: device not yet started
683  *  -ENOTSUP: device does not support MTU feature
684  */
685 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
686
687 /**
688  * Get the numa node from which the virtio net device's memory
689  * is allocated.
690  *
691  * @param vid
692  *  vhost device ID
693  *
694  * @return
695  *  The numa node, -1 on failure
696  */
697 int rte_vhost_get_numa_node(int vid);
698
699 /**
700  * @deprecated
701  * Get the number of queues the device supports.
702  *
703  * Note this function is deprecated, as it returns a queue pair number,
704  * which is vhost specific. Instead, rte_vhost_get_vring_num should
705  * be used.
706  *
707  * @param vid
708  *  vhost device ID
709  *
710  * @return
711  *  The number of queues, 0 on failure
712  */
713 __rte_deprecated
714 uint32_t rte_vhost_get_queue_num(int vid);
715
716 /**
717  * Get the number of vrings the device supports.
718  *
719  * @param vid
720  *  vhost device ID
721  *
722  * @return
723  *  The number of vrings, 0 on failure
724  */
725 uint16_t rte_vhost_get_vring_num(int vid);
726
727 /**
728  * Get the virtio net device's ifname, which is the vhost-user socket
729  * file path.
730  *
731  * @param vid
732  *  vhost device ID
733  * @param buf
734  *  The buffer to stored the queried ifname
735  * @param len
736  *  The length of buf
737  *
738  * @return
739  *  0 on success, -1 on failure
740  */
741 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
742
743 /**
744  * Get how many avail entries are left in the queue
745  *
746  * @param vid
747  *  vhost device ID
748  * @param queue_id
749  *  virtio queue index
750  *
751  * @return
752  *  num of avail entries left
753  */
754 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
755
756 struct rte_mbuf;
757 struct rte_mempool;
758 /**
759  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
760  * be received from the physical port or from another virtual device. A packet
761  * count is returned to indicate the number of packets that were successfully
762  * added to the RX queue.
763  * @param vid
764  *  vhost device ID
765  * @param queue_id
766  *  virtio queue index in mq case
767  * @param pkts
768  *  array to contain packets to be enqueued
769  * @param count
770  *  packets num to be enqueued
771  * @return
772  *  num of packets enqueued
773  */
774 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
775         struct rte_mbuf **pkts, uint16_t count);
776
777 /**
778  * This function gets guest buffers from the virtio device TX virtqueue,
779  * construct host mbufs, copies guest buffer content to host mbufs and
780  * store them in pkts to be processed.
781  * @param vid
782  *  vhost device ID
783  * @param queue_id
784  *  virtio queue index in mq case
785  * @param mbuf_pool
786  *  mbuf_pool where host mbuf is allocated.
787  * @param pkts
788  *  array to contain packets to be dequeued
789  * @param count
790  *  packets num to be dequeued
791  * @return
792  *  num of packets dequeued
793  */
794 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
795         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
796
797 /**
798  * Get guest mem table: a list of memory regions.
799  *
800  * An rte_vhost_vhost_memory object will be allocated internally, to hold the
801  * guest memory regions. Application should free it at destroy_device()
802  * callback.
803  *
804  * @param vid
805  *  vhost device ID
806  * @param mem
807  *  To store the returned mem regions
808  * @return
809  *  0 on success, -1 on failure
810  */
811 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
812
813 /**
814  * Get guest vring info, including the vring address, vring size, etc.
815  *
816  * @param vid
817  *  vhost device ID
818  * @param vring_idx
819  *  vring index
820  * @param vring
821  *  the structure to hold the requested vring info
822  * @return
823  *  0 on success, -1 on failure
824  */
825 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
826                               struct rte_vhost_vring *vring);
827
828 /**
829  * Get guest inflight vring info, including inflight ring and resubmit list.
830  *
831  * @param vid
832  *  vhost device ID
833  * @param vring_idx
834  *  vring index
835  * @param vring
836  *  the structure to hold the requested inflight vring info
837  * @return
838  *  0 on success, -1 on failure
839  */
840 int
841 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
842         struct rte_vhost_ring_inflight *vring);
843
844 /**
845  * Set split inflight descriptor.
846  *
847  * This function save descriptors that has been consumed in available
848  * ring
849  *
850  * @param vid
851  *  vhost device ID
852  * @param vring_idx
853  *  vring index
854  * @param idx
855  *  inflight entry index
856  * @return
857  *  0 on success, -1 on failure
858  */
859 int
860 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
861         uint16_t idx);
862
863 /**
864  * Set packed inflight descriptor and get corresponding inflight entry
865  *
866  * This function save descriptors that has been consumed
867  *
868  * @param vid
869  *  vhost device ID
870  * @param vring_idx
871  *  vring index
872  * @param head
873  *  head of descriptors
874  * @param last
875  *  last of descriptors
876  * @param inflight_entry
877  *  corresponding inflight entry
878  * @return
879  *  0 on success, -1 on failure
880  */
881 int
882 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
883         uint16_t head, uint16_t last, uint16_t *inflight_entry);
884
885 /**
886  * Save the head of list that the last batch of used descriptors.
887  *
888  * @param vid
889  *  vhost device ID
890  * @param vring_idx
891  *  vring index
892  * @param idx
893  *  descriptor entry index
894  * @return
895  *  0 on success, -1 on failure
896  */
897 int
898 rte_vhost_set_last_inflight_io_split(int vid,
899         uint16_t vring_idx, uint16_t idx);
900
901 /**
902  * Update the inflight free_head, used_idx and used_wrap_counter.
903  *
904  * This function will update status first before updating descriptors
905  * to used
906  *
907  * @param vid
908  *  vhost device ID
909  * @param vring_idx
910  *  vring index
911  * @param head
912  *  head of descriptors
913  * @return
914  *  0 on success, -1 on failure
915  */
916 int
917 rte_vhost_set_last_inflight_io_packed(int vid,
918         uint16_t vring_idx, uint16_t head);
919
920 /**
921  * Clear the split inflight status.
922  *
923  * @param vid
924  *  vhost device ID
925  * @param vring_idx
926  *  vring index
927  * @param last_used_idx
928  *  last used idx of used ring
929  * @param idx
930  *  inflight entry index
931  * @return
932  *  0 on success, -1 on failure
933  */
934 int
935 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
936         uint16_t last_used_idx, uint16_t idx);
937
938 /**
939  * Clear the packed inflight status.
940  *
941  * @param vid
942  *  vhost device ID
943  * @param vring_idx
944  *  vring index
945  * @param head
946  *  inflight entry index
947  * @return
948  *  0 on success, -1 on failure
949  */
950 int
951 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
952         uint16_t head);
953
954 /**
955  * Notify the guest that used descriptors have been added to the vring.  This
956  * function acts as a memory barrier.
957  *
958  * @param vid
959  *  vhost device ID
960  * @param vring_idx
961  *  vring index
962  * @return
963  *  0 on success, -1 on failure
964  */
965 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
966
967 /**
968  * Get vhost RX queue avail count.
969  *
970  * @param vid
971  *  vhost device ID
972  * @param qid
973  *  virtio queue index in mq case
974  * @return
975  *  num of desc available
976  */
977 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
978
979 /**
980  * Get power monitor address of the vhost device
981  *
982  * @param vid
983  *  vhost device ID
984  * @param queue_id
985  *  vhost queue ID
986  * @param pmc
987  *  power monitor condition
988  * @return
989  *  0 on success, -1 on failure
990  */
991 __rte_experimental
992 int
993 rte_vhost_get_monitor_addr(int vid, uint16_t queue_id,
994                 struct rte_vhost_power_monitor_cond *pmc);
995
996 /**
997  * Get log base and log size of the vhost device
998  *
999  * @param vid
1000  *  vhost device ID
1001  * @param log_base
1002  *  vhost log base
1003  * @param log_size
1004  *  vhost log size
1005  * @return
1006  *  0 on success, -1 on failure
1007  */
1008 int
1009 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
1010
1011 /**
1012  * Get last_avail/used_idx of the vhost virtqueue
1013  *
1014  * @param vid
1015  *  vhost device ID
1016  * @param queue_id
1017  *  vhost queue index
1018  * @param last_avail_idx
1019  *  vhost last_avail_idx to get
1020  * @param last_used_idx
1021  *  vhost last_used_idx to get
1022  * @return
1023  *  0 on success, -1 on failure
1024  */
1025 int
1026 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
1027                 uint16_t *last_avail_idx, uint16_t *last_used_idx);
1028
1029 /**
1030  * Get last_avail/last_used of the vhost virtqueue
1031  *
1032  * This function is designed for the reconnection and it's specific for
1033  * the packed ring as we can get the two parameters from the inflight
1034  * queueregion
1035  *
1036  * @param vid
1037  *  vhost device ID
1038  * @param queue_id
1039  *  vhost queue index
1040  * @param last_avail_idx
1041  *  vhost last_avail_idx to get
1042  * @param last_used_idx
1043  *  vhost last_used_idx to get
1044  * @return
1045  *  0 on success, -1 on failure
1046  */
1047 int
1048 rte_vhost_get_vring_base_from_inflight(int vid,
1049         uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx);
1050
1051 /**
1052  * Set last_avail/used_idx of the vhost virtqueue
1053  *
1054  * @param vid
1055  *  vhost device ID
1056  * @param queue_id
1057  *  vhost queue index
1058  * @param last_avail_idx
1059  *  last_avail_idx to set
1060  * @param last_used_idx
1061  *  last_used_idx to set
1062  * @return
1063  *  0 on success, -1 on failure
1064  */
1065 int
1066 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
1067                 uint16_t last_avail_idx, uint16_t last_used_idx);
1068
1069 /**
1070  * Register external message handling callbacks
1071  *
1072  * @param vid
1073  *  vhost device ID
1074  * @param ops
1075  *  virtio external callbacks to register
1076  * @param ctx
1077  *  additional context passed to the callbacks
1078  * @return
1079  *  0 on success, -1 on failure
1080  */
1081 int
1082 rte_vhost_extern_callback_register(int vid,
1083                 struct rte_vhost_user_extern_ops const * const ops, void *ctx);
1084
1085 /**
1086  * Get vdpa device id for vhost device.
1087  *
1088  * @param vid
1089  *  vhost device id
1090  * @return
1091  *  vDPA device pointer on success, NULL on failure
1092  */
1093 struct rte_vdpa_device *
1094 rte_vhost_get_vdpa_device(int vid);
1095
1096 /**
1097  * Notify the guest that should get virtio configuration space from backend.
1098  *
1099  * @param vid
1100  *  vhost device ID
1101  * @param need_reply
1102  *  wait for the master response the status of this operation
1103  * @return
1104  *  0 on success, < 0 on failure
1105  */
1106 __rte_experimental
1107 int
1108 rte_vhost_slave_config_change(int vid, bool need_reply);
1109
1110 /**
1111  * Retrieve names of statistics of a Vhost virtqueue.
1112  *
1113  * There is an assumption that 'stat_names' and 'stats' arrays are matched
1114  * by array index: stats_names[i].name => stats[i].value
1115  *
1116  * @param vid
1117  *   vhost device ID
1118  * @param queue_id
1119  *   vhost queue index
1120  * @param name
1121  *   array of at least size elements to be filled.
1122  *   If set to NULL, the function returns the required number of elements.
1123  * @param size
1124  *   The number of elements in stats_names array.
1125  * @return
1126  *  - Success if greater than 0 and lower or equal to *size*. The return value
1127  *    indicates the number of elements filled in the *names* array.
1128  *  - Failure if greater than *size*. The return value indicates the number of
1129  *    elements the *names* array that should be given to succeed.
1130  *  - Failure if lower than 0. The device ID or queue ID is invalid or
1131  +    statistics collection is not enabled.
1132  */
1133 __rte_experimental
1134 int
1135 rte_vhost_vring_stats_get_names(int vid, uint16_t queue_id,
1136                 struct rte_vhost_stat_name *name, unsigned int size);
1137
1138 /**
1139  * Retrieve statistics of a Vhost virtqueue.
1140  *
1141  * There is an assumption that 'stat_names' and 'stats' arrays are matched
1142  * by array index: stats_names[i].name => stats[i].value
1143  *
1144  * @param vid
1145  *   vhost device ID
1146  * @param queue_id
1147  *   vhost queue index
1148  * @param stats
1149  *   A pointer to a table of structure of type rte_vhost_stat to be filled with
1150  *   virtqueue statistics ids and values.
1151  * @param n
1152  *   The number of elements in stats array.
1153  * @return
1154  *  - Success if greater than 0 and lower or equal to *n*. The return value
1155  *    indicates the number of elements filled in the *stats* array.
1156  *  - Failure if greater than *n*. The return value indicates the number of
1157  *    elements the *stats* array that should be given to succeed.
1158  *  - Failure if lower than 0. The device ID or queue ID is invalid, or
1159  *    statistics collection is not enabled.
1160  */
1161 __rte_experimental
1162 int
1163 rte_vhost_vring_stats_get(int vid, uint16_t queue_id,
1164                 struct rte_vhost_stat *stats, unsigned int n);
1165
1166 /**
1167  * Reset statistics of a Vhost virtqueue.
1168  *
1169  * @param vid
1170  *   vhost device ID
1171  * @param queue_id
1172  *   vhost queue index
1173  * @return
1174  *  - Success if 0. Statistics have been reset.
1175  *  - Failure if lower than 0. The device ID or queue ID is invalid, or
1176  *    statistics collection is not enabled.
1177  */
1178 __rte_experimental
1179 int
1180 rte_vhost_vring_stats_reset(int vid, uint16_t queue_id);
1181
1182 #ifdef __cplusplus
1183 }
1184 #endif
1185
1186 #endif /* _RTE_VHOST_H_ */