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