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