DPDK  20.08.0
rte_vhost.h
Go to the documentation of this file.
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 
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 
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 
111 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
112 #define VHOST_USER_F_PROTOCOL_FEATURES 30
113 #endif
114 
115 struct rte_vdpa_device;
116 
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 
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 
217  int callfd;
218 
219  int kickfd;
220  uint16_t size;
221 };
222 
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 
251 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
252 
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 
267  int (*new_device)(int vid);
268  void (*destroy_device)(int vid);
270  int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);
278  int (*features_changed)(int vid, uint64_t features);
279 
280  int (*new_connection)(int vid);
281  void (*destroy_connection)(int vid);
282 
289  void (*guest_notified)(int vid);
290 
291  void *reserved[1];
292 };
293 
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 
344 __rte_experimental
345 static __rte_always_inline uint64_t
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 
389 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
390 
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 
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 
433 __rte_experimental
434 int
435 rte_vhost_driver_attach_vdpa_device(const char *path,
436  struct rte_vdpa_device *dev);
437 
446 __rte_experimental
447 int
448 rte_vhost_driver_detach_vdpa_device(const char *path);
449 
458 __rte_experimental
459 struct rte_vdpa_device *
460 rte_vhost_driver_get_vdpa_device(const char *path);
461 
472 int rte_vhost_driver_set_features(const char *path, uint64_t features);
473 
489 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
490 
503 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
504 
515 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
516 
527 __rte_experimental
528 int
530  uint64_t protocol_features);
531 
542 __rte_experimental
543 int
545  uint64_t *protocol_features);
546 
557 __rte_experimental
558 int
559 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
560 
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 
588 int rte_vhost_driver_start(const char *path);
589 
603 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
604 
615 int rte_vhost_get_numa_node(int vid);
616 
631 __rte_deprecated
632 uint32_t rte_vhost_get_queue_num(int vid);
633 
643 uint16_t rte_vhost_get_vring_num(int vid);
644 
659 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
660 
672 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
673 
674 struct rte_mbuf;
675 struct rte_mempool;
692 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
693  struct rte_mbuf **pkts, uint16_t count);
694 
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 
729 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
730 
743 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
744  struct rte_vhost_vring *vring);
745 
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 
778 __rte_experimental
779 int
780 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
781  uint16_t idx);
782 
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 
818 __rte_experimental
819 int
821  uint16_t vring_idx, uint16_t idx);
822 
838 __rte_experimental
839 int
841  uint16_t vring_idx, uint16_t head);
842 
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 
874 __rte_experimental
875 int
876 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
877  uint16_t head);
878 
890 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
891 
902 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
903 
916 __rte_experimental
917 int
918 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
919 
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 
957 __rte_experimental
958 int
960  uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx);
961 
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 
993 __rte_experimental
994 int
996  struct rte_vhost_user_extern_ops const * const ops, void *ctx);
997 
1006 __rte_experimental
1007 struct rte_vdpa_device *
1008 rte_vhost_get_vdpa_device(int vid);
1009 
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_ */
static __rte_deprecated __rte_always_inline uint64_t rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
Definition: rte_vhost.h:311
__rte_experimental int rte_vhost_set_last_inflight_io_split(int vid, uint16_t vring_idx, uint16_t idx)
int rte_vhost_driver_start(const char *path)
#define __rte_always_inline
Definition: rte_common.h:202
void * reserved[1]
Definition: rte_vhost.h:291
int rte_vhost_driver_register(const char *path, uint64_t flags)
uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id)
__rte_experimental int rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx, uint16_t last_used_idx, uint16_t idx)
int rte_vhost_driver_disable_features(const char *path, uint64_t features)
void rte_vhost_log_used_vring(int vid, uint16_t vring_idx, uint64_t offset, uint64_t len)
__rte_experimental int rte_vhost_extern_callback_register(int vid, struct rte_vhost_user_extern_ops const *const ops, void *ctx)
int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx, struct rte_vhost_vring *vring)
__rte_experimental struct rte_vdpa_device * rte_vhost_driver_get_vdpa_device(const char *path)
__rte_experimental int rte_vhost_set_last_inflight_io_packed(int vid, uint16_t vring_idx, uint16_t head)
uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id, struct rte_mbuf **pkts, uint16_t count)
__rte_deprecated uint32_t rte_vhost_get_queue_num(int vid)
__rte_experimental int rte_vhost_get_vring_base(int vid, uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx)
__rte_experimental int rte_vhost_set_vring_base(int vid, uint16_t queue_id, uint16_t last_avail_idx, uint16_t last_used_idx)
int rte_vhost_get_mtu(int vid, uint16_t *mtu)
void(* guest_notified)(int vid)
Definition: rte_vhost.h:289
__rte_experimental int rte_vhost_slave_config_change(int vid, bool need_reply)
__rte_experimental int rte_vhost_driver_set_protocol_features(const char *path, uint64_t protocol_features)
void(* destroy_device)(int vid)
Definition: rte_vhost.h:268
__rte_experimental struct rte_vdpa_device * rte_vhost_get_vdpa_device(int vid)
__rte_experimental int rte_vhost_driver_detach_vdpa_device(const char *path)
#define unlikely(x)
uint16_t rte_vhost_get_vring_num(int vid)
int rte_vhost_get_numa_node(int vid)
struct rte_vdpa_dev_ops * ops
Definition: rte_vdpa_dev.h:77
static __rte_experimental __rte_always_inline uint64_t rte_vhost_va_from_guest_pa(struct rte_vhost_memory *mem, uint64_t gpa, uint64_t *len)
Definition: rte_vhost.h:346
int(* features_changed)(int vid, uint64_t features)
Definition: rte_vhost.h:278
__rte_experimental int rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
__rte_experimental int rte_vhost_driver_attach_vdpa_device(const char *path, struct rte_vdpa_device *dev)
int rte_vhost_vring_call(int vid, uint16_t vring_idx)
int rte_vhost_get_negotiated_features(int vid, uint64_t *features)
rte_vhost_msg_result
Definition: rte_vhost.h:226
__rte_experimental int rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx, uint16_t head, uint16_t last, uint16_t *inflight_entry)
uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid)
int(* new_device)(int vid)
Definition: rte_vhost.h:267
uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id, struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
__rte_experimental int rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx, uint16_t head)
__rte_experimental int rte_vhost_driver_get_protocol_features(const char *path, uint64_t *protocol_features)
__rte_experimental int rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx, struct rte_vhost_ring_inflight *vring)
__rte_experimental int rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size)
int rte_vhost_driver_get_features(const char *path, uint64_t *features)
int rte_vhost_driver_set_features(const char *path, uint64_t features)
int(* vring_state_changed)(int vid, uint16_t queue_id, int enable)
Definition: rte_vhost.h:270
int rte_vhost_get_ifname(int vid, char *buf, size_t len)
__rte_experimental int rte_vhost_get_vring_base_from_inflight(int vid, uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx)
__rte_experimental int rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx, uint16_t idx)
enum rte_vhost_msg_result(* rte_vhost_msg_handle)(int vid, void *msg)
Definition: rte_vhost.h:251
int rte_vhost_driver_enable_features(const char *path, uint64_t features)
void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)