doc: add Meson coding style to contributors guide
[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_RESERVED_1       (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 int
434 rte_vhost_driver_attach_vdpa_device(const char *path,
435                 struct rte_vdpa_device *dev);
436
437 /**
438  * Unset the vdpa device id
439  *
440  * @param path
441  *  The vhost-user socket file path
442  * @return
443  *  0 on success, -1 on failure
444  */
445 int
446 rte_vhost_driver_detach_vdpa_device(const char *path);
447
448 /**
449  * Get the device id
450  *
451  * @param path
452  *  The vhost-user socket file path
453  * @return
454  *  vDPA device pointer, NULL on failure
455  */
456 struct rte_vdpa_device *
457 rte_vhost_driver_get_vdpa_device(const char *path);
458
459 /**
460  * Set the feature bits the vhost-user driver supports.
461  *
462  * @param path
463  *  The vhost-user socket file path
464  * @param features
465  *  Supported features
466  * @return
467  *  0 on success, -1 on failure
468  */
469 int rte_vhost_driver_set_features(const char *path, uint64_t features);
470
471 /**
472  * Enable vhost-user driver features.
473  *
474  * Note that
475  * - the param features should be a subset of the feature bits provided
476  *   by rte_vhost_driver_set_features().
477  * - it must be invoked before vhost-user negotiation starts.
478  *
479  * @param path
480  *  The vhost-user socket file path
481  * @param features
482  *  Features to enable
483  * @return
484  *  0 on success, -1 on failure
485  */
486 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
487
488 /**
489  * Disable vhost-user driver features.
490  *
491  * The two notes at rte_vhost_driver_enable_features() also apply here.
492  *
493  * @param path
494  *  The vhost-user socket file path
495  * @param features
496  *  Features to disable
497  * @return
498  *  0 on success, -1 on failure
499  */
500 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
501
502 /**
503  * Get the feature bits before feature negotiation.
504  *
505  * @param path
506  *  The vhost-user socket file path
507  * @param features
508  *  A pointer to store the queried feature bits
509  * @return
510  *  0 on success, -1 on failure
511  */
512 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
513
514 /**
515  * Set the protocol feature bits before feature negotiation.
516  *
517  * @param path
518  *  The vhost-user socket file path
519  * @param protocol_features
520  *  Supported protocol features
521  * @return
522  *  0 on success, -1 on failure
523  */
524 __rte_experimental
525 int
526 rte_vhost_driver_set_protocol_features(const char *path,
527                 uint64_t protocol_features);
528
529 /**
530  * Get the protocol feature bits before feature negotiation.
531  *
532  * @param path
533  *  The vhost-user socket file path
534  * @param protocol_features
535  *  A pointer to store the queried protocol feature bits
536  * @return
537  *  0 on success, -1 on failure
538  */
539 __rte_experimental
540 int
541 rte_vhost_driver_get_protocol_features(const char *path,
542                 uint64_t *protocol_features);
543
544 /**
545  * Get the queue number bits before feature negotiation.
546  *
547  * @param path
548  *  The vhost-user socket file path
549  * @param queue_num
550  *  A pointer to store the queried queue number bits
551  * @return
552  *  0 on success, -1 on failure
553  */
554 __rte_experimental
555 int
556 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
557
558 /**
559  * Get the feature bits after negotiation
560  *
561  * @param vid
562  *  Vhost device ID
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_get_negotiated_features(int vid, uint64_t *features);
569
570 /**
571  * Get the protocol feature bits after negotiation
572  *
573  * @param vid
574  *  Vhost device ID
575  * @param protocol_features
576  *  A pointer to store the queried protocol feature bits
577  * @return
578  *  0 on success, -1 on failure
579  */
580 __rte_experimental
581 int
582 rte_vhost_get_negotiated_protocol_features(int vid,
583                                            uint64_t *protocol_features);
584
585 /* Register callbacks. */
586 int rte_vhost_driver_callback_register(const char *path,
587         struct vhost_device_ops const * const ops);
588
589 /**
590  *
591  * Start the vhost-user driver.
592  *
593  * This function triggers the vhost-user negotiation.
594  *
595  * @param path
596  *  The vhost-user socket file path
597  * @return
598  *  0 on success, -1 on failure
599  */
600 int rte_vhost_driver_start(const char *path);
601
602 /**
603  * Get the MTU value of the device if set in QEMU.
604  *
605  * @param vid
606  *  virtio-net device ID
607  * @param mtu
608  *  The variable to store the MTU value
609  *
610  * @return
611  *  0: success
612  *  -EAGAIN: device not yet started
613  *  -ENOTSUP: device does not support MTU feature
614  */
615 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
616
617 /**
618  * Get the numa node from which the virtio net device's memory
619  * is allocated.
620  *
621  * @param vid
622  *  vhost device ID
623  *
624  * @return
625  *  The numa node, -1 on failure
626  */
627 int rte_vhost_get_numa_node(int vid);
628
629 /**
630  * @deprecated
631  * Get the number of queues the device supports.
632  *
633  * Note this function is deprecated, as it returns a queue pair number,
634  * which is vhost specific. Instead, rte_vhost_get_vring_num should
635  * be used.
636  *
637  * @param vid
638  *  vhost device ID
639  *
640  * @return
641  *  The number of queues, 0 on failure
642  */
643 __rte_deprecated
644 uint32_t rte_vhost_get_queue_num(int vid);
645
646 /**
647  * Get the number of vrings the device supports.
648  *
649  * @param vid
650  *  vhost device ID
651  *
652  * @return
653  *  The number of vrings, 0 on failure
654  */
655 uint16_t rte_vhost_get_vring_num(int vid);
656
657 /**
658  * Get the virtio net device's ifname, which is the vhost-user socket
659  * file path.
660  *
661  * @param vid
662  *  vhost device ID
663  * @param buf
664  *  The buffer to stored the queried ifname
665  * @param len
666  *  The length of buf
667  *
668  * @return
669  *  0 on success, -1 on failure
670  */
671 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
672
673 /**
674  * Get how many avail entries are left in the queue
675  *
676  * @param vid
677  *  vhost device ID
678  * @param queue_id
679  *  virtio queue index
680  *
681  * @return
682  *  num of avail entries left
683  */
684 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
685
686 struct rte_mbuf;
687 struct rte_mempool;
688 /**
689  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
690  * be received from the physical port or from another virtual device. A packet
691  * count is returned to indicate the number of packets that were successfully
692  * added to the RX queue.
693  * @param vid
694  *  vhost device ID
695  * @param queue_id
696  *  virtio queue index in mq case
697  * @param pkts
698  *  array to contain packets to be enqueued
699  * @param count
700  *  packets num to be enqueued
701  * @return
702  *  num of packets enqueued
703  */
704 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
705         struct rte_mbuf **pkts, uint16_t count);
706
707 /**
708  * This function gets guest buffers from the virtio device TX virtqueue,
709  * construct host mbufs, copies guest buffer content to host mbufs and
710  * store them in pkts to be processed.
711  * @param vid
712  *  vhost device ID
713  * @param queue_id
714  *  virtio queue index in mq case
715  * @param mbuf_pool
716  *  mbuf_pool where host mbuf is allocated.
717  * @param pkts
718  *  array to contain packets to be dequeued
719  * @param count
720  *  packets num to be dequeued
721  * @return
722  *  num of packets dequeued
723  */
724 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
725         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
726
727 /**
728  * Get guest mem table: a list of memory regions.
729  *
730  * An rte_vhost_vhost_memory object will be allocated internally, to hold the
731  * guest memory regions. Application should free it at destroy_device()
732  * callback.
733  *
734  * @param vid
735  *  vhost device ID
736  * @param mem
737  *  To store the returned mem regions
738  * @return
739  *  0 on success, -1 on failure
740  */
741 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
742
743 /**
744  * Get guest vring info, including the vring address, vring size, etc.
745  *
746  * @param vid
747  *  vhost device ID
748  * @param vring_idx
749  *  vring index
750  * @param vring
751  *  the structure to hold the requested vring info
752  * @return
753  *  0 on success, -1 on failure
754  */
755 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
756                               struct rte_vhost_vring *vring);
757
758 /**
759  * Get guest inflight vring info, including inflight ring and resubmit list.
760  *
761  * @param vid
762  *  vhost device ID
763  * @param vring_idx
764  *  vring index
765  * @param vring
766  *  the structure to hold the requested inflight vring info
767  * @return
768  *  0 on success, -1 on failure
769  */
770 __rte_experimental
771 int
772 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
773         struct rte_vhost_ring_inflight *vring);
774
775 /**
776  * Set split inflight descriptor.
777  *
778  * This function save descriptors that has been comsumed in available
779  * ring
780  *
781  * @param vid
782  *  vhost device ID
783  * @param vring_idx
784  *  vring index
785  * @param idx
786  *  inflight entry index
787  * @return
788  *  0 on success, -1 on failure
789  */
790 __rte_experimental
791 int
792 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
793         uint16_t idx);
794
795 /**
796  * Set packed inflight descriptor and get corresponding inflight entry
797  *
798  * This function save descriptors that has been comsumed
799  *
800  * @param vid
801  *  vhost device ID
802  * @param vring_idx
803  *  vring index
804  * @param head
805  *  head of descriptors
806  * @param last
807  *  last of descriptors
808  * @param inflight_entry
809  *  corresponding inflight entry
810  * @return
811  *  0 on success, -1 on failure
812  */
813 __rte_experimental
814 int
815 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
816         uint16_t head, uint16_t last, uint16_t *inflight_entry);
817
818 /**
819  * Save the head of list that the last batch of used descriptors.
820  *
821  * @param vid
822  *  vhost device ID
823  * @param vring_idx
824  *  vring index
825  * @param idx
826  *  descriptor entry index
827  * @return
828  *  0 on success, -1 on failure
829  */
830 __rte_experimental
831 int
832 rte_vhost_set_last_inflight_io_split(int vid,
833         uint16_t vring_idx, uint16_t idx);
834
835 /**
836  * Update the inflight free_head, used_idx and used_wrap_counter.
837  *
838  * This function will update status first before updating descriptors
839  * to used
840  *
841  * @param vid
842  *  vhost device ID
843  * @param vring_idx
844  *  vring index
845  * @param head
846  *  head of descriptors
847  * @return
848  *  0 on success, -1 on failure
849  */
850 __rte_experimental
851 int
852 rte_vhost_set_last_inflight_io_packed(int vid,
853         uint16_t vring_idx, uint16_t head);
854
855 /**
856  * Clear the split inflight status.
857  *
858  * @param vid
859  *  vhost device ID
860  * @param vring_idx
861  *  vring index
862  * @param last_used_idx
863  *  last used idx of used ring
864  * @param idx
865  *  inflight entry index
866  * @return
867  *  0 on success, -1 on failure
868  */
869 __rte_experimental
870 int
871 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
872         uint16_t last_used_idx, uint16_t idx);
873
874 /**
875  * Clear the packed inflight status.
876  *
877  * @param vid
878  *  vhost device ID
879  * @param vring_idx
880  *  vring index
881  * @param head
882  *  inflight entry index
883  * @return
884  *  0 on success, -1 on failure
885  */
886 __rte_experimental
887 int
888 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
889         uint16_t head);
890
891 /**
892  * Notify the guest that used descriptors have been added to the vring.  This
893  * function acts as a memory barrier.
894  *
895  * @param vid
896  *  vhost device ID
897  * @param vring_idx
898  *  vring index
899  * @return
900  *  0 on success, -1 on failure
901  */
902 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
903
904 /**
905  * Get vhost RX queue avail count.
906  *
907  * @param vid
908  *  vhost device ID
909  * @param qid
910  *  virtio queue index in mq case
911  * @return
912  *  num of desc available
913  */
914 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
915
916 /**
917  * Get log base and log size of the vhost device
918  *
919  * @param vid
920  *  vhost device ID
921  * @param log_base
922  *  vhost log base
923  * @param log_size
924  *  vhost log size
925  * @return
926  *  0 on success, -1 on failure
927  */
928 int
929 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
930
931 /**
932  * Get last_avail/used_idx of the vhost virtqueue
933  *
934  * @param vid
935  *  vhost device ID
936  * @param queue_id
937  *  vhost queue index
938  * @param last_avail_idx
939  *  vhost last_avail_idx to get
940  * @param last_used_idx
941  *  vhost last_used_idx to get
942  * @return
943  *  0 on success, -1 on failure
944  */
945 int
946 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
947                 uint16_t *last_avail_idx, uint16_t *last_used_idx);
948
949 /**
950  * Get last_avail/last_used of the vhost virtqueue
951  *
952  * This function is designed for the reconnection and it's specific for
953  * the packed ring as we can get the two parameters from the inflight
954  * queueregion
955  *
956  * @param vid
957  *  vhost device ID
958  * @param queue_id
959  *  vhost queue index
960  * @param last_avail_idx
961  *  vhost last_avail_idx to get
962  * @param last_used_idx
963  *  vhost last_used_idx to get
964  * @return
965  *  0 on success, -1 on failure
966  */
967 __rte_experimental
968 int
969 rte_vhost_get_vring_base_from_inflight(int vid,
970         uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx);
971
972 /**
973  * Set last_avail/used_idx of the vhost virtqueue
974  *
975  * @param vid
976  *  vhost device ID
977  * @param queue_id
978  *  vhost queue index
979  * @param last_avail_idx
980  *  last_avail_idx to set
981  * @param last_used_idx
982  *  last_used_idx to set
983  * @return
984  *  0 on success, -1 on failure
985  */
986 int
987 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
988                 uint16_t last_avail_idx, uint16_t last_used_idx);
989
990 /**
991  * Register external message handling callbacks
992  *
993  * @param vid
994  *  vhost device ID
995  * @param ops
996  *  virtio external callbacks to register
997  * @param ctx
998  *  additional context passed to the callbacks
999  * @return
1000  *  0 on success, -1 on failure
1001  */
1002 __rte_experimental
1003 int
1004 rte_vhost_extern_callback_register(int vid,
1005                 struct rte_vhost_user_extern_ops const * const ops, void *ctx);
1006
1007 /**
1008  * Get vdpa device id for vhost device.
1009  *
1010  * @param vid
1011  *  vhost device id
1012  * @return
1013  *  vDPA device pointer on success, NULL on failure
1014  */
1015 struct rte_vdpa_device *
1016 rte_vhost_get_vdpa_device(int vid);
1017
1018 /**
1019  * Notify the guest that should get virtio configuration space from backend.
1020  *
1021  * @param vid
1022  *  vhost device ID
1023  * @param need_reply
1024  *  wait for the master response the status of this operation
1025  * @return
1026  *  0 on success, < 0 on failure
1027  */
1028 __rte_experimental
1029 int
1030 rte_vhost_slave_config_change(int vid, bool need_reply);
1031
1032 #ifdef __cplusplus
1033 }
1034 #endif
1035
1036 #endif /* _RTE_VHOST_H_ */