DPDK  19.11.13-rc3
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 <stdint.h>
14 #include <sys/eventfd.h>
15 
16 #include <rte_memory.h>
17 #include <rte_mempool.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 #ifndef __cplusplus
24 /* These are not C++-aware. */
25 #include <linux/vhost.h>
26 #include <linux/virtio_ring.h>
27 #include <linux/virtio_net.h>
28 #endif
29 
30 #define RTE_VHOST_USER_CLIENT (1ULL << 0)
31 #define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1)
32 #define RTE_VHOST_USER_DEQUEUE_ZERO_COPY (1ULL << 2)
33 #define RTE_VHOST_USER_IOMMU_SUPPORT (1ULL << 3)
34 #define RTE_VHOST_USER_POSTCOPY_SUPPORT (1ULL << 4)
35 /* support mbuf with external buffer attached */
36 #define RTE_VHOST_USER_EXTBUF_SUPPORT (1ULL << 5)
37 /* support only linear buffers (no chained mbufs) */
38 #define RTE_VHOST_USER_LINEARBUF_SUPPORT (1ULL << 6)
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 
107 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
108 #define VHOST_USER_F_PROTOCOL_FEATURES 30
109 #endif
110 
111 
117  uint64_t guest_phys_addr;
118  uint64_t guest_user_addr;
119  uint64_t host_user_addr;
120  uint64_t size;
121  void *mmap_addr;
122  uint64_t mmap_size;
123  int fd;
124 };
125 
130  uint32_t nregions;
131  struct rte_vhost_mem_region regions[];
132 };
133 
134 struct rte_vhost_inflight_desc_split {
135  uint8_t inflight;
136  uint8_t padding[5];
137  uint16_t next;
138  uint64_t counter;
139 };
140 
141 struct rte_vhost_inflight_info_split {
142  uint64_t features;
143  uint16_t version;
144  uint16_t desc_num;
145  uint16_t last_inflight_io;
146  uint16_t used_idx;
147  struct rte_vhost_inflight_desc_split desc[0];
148 };
149 
150 struct rte_vhost_inflight_desc_packed {
151  uint8_t inflight;
152  uint8_t padding;
153  uint16_t next;
154  uint16_t last;
155  uint16_t num;
156  uint64_t counter;
157  uint16_t id;
158  uint16_t flags;
159  uint32_t len;
160  uint64_t addr;
161 };
162 
163 struct rte_vhost_inflight_info_packed {
164  uint64_t features;
165  uint16_t version;
166  uint16_t desc_num;
167  uint16_t free_head;
168  uint16_t old_free_head;
169  uint16_t used_idx;
170  uint16_t old_used_idx;
171  uint8_t used_wrap_counter;
172  uint8_t old_used_wrap_counter;
173  uint8_t padding[7];
174  struct rte_vhost_inflight_desc_packed desc[0];
175 };
176 
177 struct rte_vhost_resubmit_desc {
178  uint16_t index;
179  uint64_t counter;
180 };
181 
182 struct rte_vhost_resubmit_info {
183  struct rte_vhost_resubmit_desc *resubmit_list;
184  uint16_t resubmit_num;
185 };
186 
187 struct rte_vhost_ring_inflight {
188  union {
189  struct rte_vhost_inflight_info_split *inflight_split;
190  struct rte_vhost_inflight_info_packed *inflight_packed;
191  };
192 
193  struct rte_vhost_resubmit_info *resubmit_inflight;
194 };
195 
196 struct rte_vhost_vring {
197  union {
198  struct vring_desc *desc;
199  struct vring_packed_desc *desc_packed;
200  };
201  union {
202  struct vring_avail *avail;
203  struct vring_packed_desc_event *driver_event;
204  };
205  union {
206  struct vring_used *used;
207  struct vring_packed_desc_event *device_event;
208  };
209  uint64_t log_guest_addr;
210 
212  int callfd;
213 
214  int kickfd;
215  uint16_t size;
216 };
217 
222  /* Message handling failed */
223  RTE_VHOST_MSG_RESULT_ERR = -1,
224  /* Message handling successful */
225  RTE_VHOST_MSG_RESULT_OK = 0,
226  /* Message handling successful and reply prepared */
227  RTE_VHOST_MSG_RESULT_REPLY = 1,
228  /* Message not handled */
229  RTE_VHOST_MSG_RESULT_NOT_HANDLED,
230 };
231 
246 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
247 
252  /* Called prior to the master message handling. */
253  rte_vhost_msg_handle pre_msg_handle;
254  /* Called after the master message handling. */
255  rte_vhost_msg_handle post_msg_handle;
256 };
257 
262  int (*new_device)(int vid);
263  void (*destroy_device)(int vid);
265  int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);
273  int (*features_changed)(int vid, uint64_t features);
274 
275  int (*new_connection)(int vid);
276  void (*destroy_connection)(int vid);
277 
284  void (*guest_notified)(int vid);
285 
286  void *reserved[1];
287 };
288 
304 __rte_deprecated
305 static __rte_always_inline uint64_t
306 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
307 {
308  struct rte_vhost_mem_region *reg;
309  uint32_t i;
310 
311  for (i = 0; i < mem->nregions; i++) {
312  reg = &mem->regions[i];
313  if (gpa >= reg->guest_phys_addr &&
314  gpa < reg->guest_phys_addr + reg->size) {
315  return gpa - reg->guest_phys_addr +
316  reg->host_user_addr;
317  }
318  }
319 
320  return 0;
321 }
322 
339 __rte_experimental
340 static __rte_always_inline uint64_t
342  uint64_t gpa, uint64_t *len)
343 {
344  struct rte_vhost_mem_region *r;
345  uint32_t i;
346 
347  for (i = 0; i < mem->nregions; i++) {
348  r = &mem->regions[i];
349  if (gpa >= r->guest_phys_addr &&
350  gpa < r->guest_phys_addr + r->size) {
351 
352  if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
353  *len = r->guest_phys_addr + r->size - gpa;
354 
355  return gpa - r->guest_phys_addr +
356  r->host_user_addr;
357  }
358  }
359  *len = 0;
360 
361  return 0;
362 }
363 
364 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
365 
384 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
385 
404 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
405  uint64_t offset, uint64_t len);
406 
407 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
408 
413 int rte_vhost_driver_register(const char *path, uint64_t flags);
414 
415 /* Unregister vhost driver. This is only meaningful to vhost user. */
416 int rte_vhost_driver_unregister(const char *path);
417 
428 __rte_experimental
429 int
430 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
431 
440 __rte_experimental
441 int
442 rte_vhost_driver_detach_vdpa_device(const char *path);
443 
452 __rte_experimental
453 int
454 rte_vhost_driver_get_vdpa_device_id(const char *path);
455 
466 int rte_vhost_driver_set_features(const char *path, uint64_t features);
467 
483 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
484 
497 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
498 
509 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
510 
521 __rte_experimental
522 int
524  uint64_t protocol_features);
525 
536 __rte_experimental
537 int
539  uint64_t *protocol_features);
540 
551 __rte_experimental
552 int
553 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
554 
565 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
566 
567 /* Register callbacks. */
568 int rte_vhost_driver_callback_register(const char *path,
569  struct vhost_device_ops const * const ops);
570 
582 int rte_vhost_driver_start(const char *path);
583 
597 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
598 
609 int rte_vhost_get_numa_node(int vid);
610 
625 __rte_deprecated
626 uint32_t rte_vhost_get_queue_num(int vid);
627 
637 uint16_t rte_vhost_get_vring_num(int vid);
638 
653 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
654 
666 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
667 
668 struct rte_mbuf;
669 struct rte_mempool;
686 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
687  struct rte_mbuf **pkts, uint16_t count);
688 
706 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
707  struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
708 
723 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
724 
737 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
738  struct rte_vhost_vring *vring);
739 
752 __rte_experimental
753 int
754 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
755  struct rte_vhost_ring_inflight *vring);
756 
772 __rte_experimental
773 int
774 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
775  uint16_t idx);
776 
795 __rte_experimental
796 int
797 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
798  uint16_t head, uint16_t last, uint16_t *inflight_entry);
799 
812 __rte_experimental
813 int
815  uint16_t vring_idx, uint16_t idx);
816 
832 __rte_experimental
833 int
835  uint16_t vring_idx, uint16_t head);
836 
851 __rte_experimental
852 int
853 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
854  uint16_t last_used_idx, uint16_t idx);
855 
868 __rte_experimental
869 int
870 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
871  uint16_t head);
872 
884 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
885 
896 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
897 
910 __rte_experimental
911 int
912 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
913 
928 __rte_experimental
929 int
930 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
931  uint16_t *last_avail_idx, uint16_t *last_used_idx);
932 
951 __rte_experimental
952 int
954  uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx);
955 
970 __rte_experimental
971 int
972 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
973  uint16_t last_avail_idx, uint16_t last_used_idx);
974 
987 __rte_experimental
988 int
990  struct rte_vhost_user_extern_ops const * const ops, void *ctx);
991 
1000 __rte_experimental
1001 int
1003 
1004 #ifdef __cplusplus
1005 }
1006 #endif
1007 
1008 #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:306
__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:158
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 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)
__rte_experimental int rte_vhost_driver_set_protocol_features(const char *path, uint64_t protocol_features)
__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)
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:341
__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_get_vdpa_device_id(int vid)
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:221
__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)
__rte_experimental int rte_vhost_driver_get_vdpa_device_id(const char *path)
uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid)
__rte_experimental int rte_vhost_driver_attach_vdpa_device(const char *path, int did)
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 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:246
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)