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