DPDK  19.11.10
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 /* These are not C++-aware. */
24 #include <linux/vhost.h>
25 #include <linux/virtio_ring.h>
26 #include <linux/virtio_net.h>
27 
28 #define RTE_VHOST_USER_CLIENT (1ULL << 0)
29 #define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1)
30 #define RTE_VHOST_USER_DEQUEUE_ZERO_COPY (1ULL << 2)
31 #define RTE_VHOST_USER_IOMMU_SUPPORT (1ULL << 3)
32 #define RTE_VHOST_USER_POSTCOPY_SUPPORT (1ULL << 4)
33 /* support mbuf with external buffer attached */
34 #define RTE_VHOST_USER_EXTBUF_SUPPORT (1ULL << 5)
35 /* support only linear buffers (no chained mbufs) */
36 #define RTE_VHOST_USER_LINEARBUF_SUPPORT (1ULL << 6)
37 
38 /* Features. */
39 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
40  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
41 #endif
42 
43 #ifndef VIRTIO_NET_F_MQ
44  #define VIRTIO_NET_F_MQ 22
45 #endif
46 
47 #ifndef VIRTIO_NET_F_MTU
48  #define VIRTIO_NET_F_MTU 3
49 #endif
50 
51 #ifndef VIRTIO_F_ANY_LAYOUT
52  #define VIRTIO_F_ANY_LAYOUT 27
53 #endif
54 
56 #ifndef VHOST_USER_PROTOCOL_F_MQ
57 #define VHOST_USER_PROTOCOL_F_MQ 0
58 #endif
59 
60 #ifndef VHOST_USER_PROTOCOL_F_LOG_SHMFD
61 #define VHOST_USER_PROTOCOL_F_LOG_SHMFD 1
62 #endif
63 
64 #ifndef VHOST_USER_PROTOCOL_F_RARP
65 #define VHOST_USER_PROTOCOL_F_RARP 2
66 #endif
67 
68 #ifndef VHOST_USER_PROTOCOL_F_REPLY_ACK
69 #define VHOST_USER_PROTOCOL_F_REPLY_ACK 3
70 #endif
71 
72 #ifndef VHOST_USER_PROTOCOL_F_NET_MTU
73 #define VHOST_USER_PROTOCOL_F_NET_MTU 4
74 #endif
75 
76 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_REQ
77 #define VHOST_USER_PROTOCOL_F_SLAVE_REQ 5
78 #endif
79 
80 #ifndef VHOST_USER_PROTOCOL_F_CRYPTO_SESSION
81 #define VHOST_USER_PROTOCOL_F_CRYPTO_SESSION 7
82 #endif
83 
84 #ifndef VHOST_USER_PROTOCOL_F_PAGEFAULT
85 #define VHOST_USER_PROTOCOL_F_PAGEFAULT 8
86 #endif
87 
88 #ifndef VHOST_USER_PROTOCOL_F_CONFIG
89 #define VHOST_USER_PROTOCOL_F_CONFIG 9
90 #endif
91 
92 #ifndef VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD
93 #define VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD 10
94 #endif
95 
96 #ifndef VHOST_USER_PROTOCOL_F_HOST_NOTIFIER
97 #define VHOST_USER_PROTOCOL_F_HOST_NOTIFIER 11
98 #endif
99 
100 #ifndef VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
101 #define VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD 12
102 #endif
103 
105 #ifndef VHOST_USER_F_PROTOCOL_FEATURES
106 #define VHOST_USER_F_PROTOCOL_FEATURES 30
107 #endif
108 
109 
115  uint64_t guest_phys_addr;
116  uint64_t guest_user_addr;
117  uint64_t host_user_addr;
118  uint64_t size;
119  void *mmap_addr;
120  uint64_t mmap_size;
121  int fd;
122 };
123 
128  uint32_t nregions;
129  struct rte_vhost_mem_region regions[];
130 };
131 
132 struct rte_vhost_inflight_desc_split {
133  uint8_t inflight;
134  uint8_t padding[5];
135  uint16_t next;
136  uint64_t counter;
137 };
138 
139 struct rte_vhost_inflight_info_split {
140  uint64_t features;
141  uint16_t version;
142  uint16_t desc_num;
143  uint16_t last_inflight_io;
144  uint16_t used_idx;
145  struct rte_vhost_inflight_desc_split desc[0];
146 };
147 
148 struct rte_vhost_inflight_desc_packed {
149  uint8_t inflight;
150  uint8_t padding;
151  uint16_t next;
152  uint16_t last;
153  uint16_t num;
154  uint64_t counter;
155  uint16_t id;
156  uint16_t flags;
157  uint32_t len;
158  uint64_t addr;
159 };
160 
161 struct rte_vhost_inflight_info_packed {
162  uint64_t features;
163  uint16_t version;
164  uint16_t desc_num;
165  uint16_t free_head;
166  uint16_t old_free_head;
167  uint16_t used_idx;
168  uint16_t old_used_idx;
169  uint8_t used_wrap_counter;
170  uint8_t old_used_wrap_counter;
171  uint8_t padding[7];
172  struct rte_vhost_inflight_desc_packed desc[0];
173 };
174 
175 struct rte_vhost_resubmit_desc {
176  uint16_t index;
177  uint64_t counter;
178 };
179 
180 struct rte_vhost_resubmit_info {
181  struct rte_vhost_resubmit_desc *resubmit_list;
182  uint16_t resubmit_num;
183 };
184 
185 struct rte_vhost_ring_inflight {
186  union {
187  struct rte_vhost_inflight_info_split *inflight_split;
188  struct rte_vhost_inflight_info_packed *inflight_packed;
189  };
190 
191  struct rte_vhost_resubmit_info *resubmit_inflight;
192 };
193 
194 struct rte_vhost_vring {
195  union {
196  struct vring_desc *desc;
197  struct vring_packed_desc *desc_packed;
198  };
199  union {
200  struct vring_avail *avail;
201  struct vring_packed_desc_event *driver_event;
202  };
203  union {
204  struct vring_used *used;
205  struct vring_packed_desc_event *device_event;
206  };
207  uint64_t log_guest_addr;
208 
210  int callfd;
211 
212  int kickfd;
213  uint16_t size;
214 };
215 
220  /* Message handling failed */
221  RTE_VHOST_MSG_RESULT_ERR = -1,
222  /* Message handling successful */
223  RTE_VHOST_MSG_RESULT_OK = 0,
224  /* Message handling successful and reply prepared */
225  RTE_VHOST_MSG_RESULT_REPLY = 1,
226  /* Message not handled */
227  RTE_VHOST_MSG_RESULT_NOT_HANDLED,
228 };
229 
244 typedef enum rte_vhost_msg_result (*rte_vhost_msg_handle)(int vid, void *msg);
245 
250  /* Called prior to the master message handling. */
251  rte_vhost_msg_handle pre_msg_handle;
252  /* Called after the master message handling. */
253  rte_vhost_msg_handle post_msg_handle;
254 };
255 
260  int (*new_device)(int vid);
261  void (*destroy_device)(int vid);
263  int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);
271  int (*features_changed)(int vid, uint64_t features);
272 
273  int (*new_connection)(int vid);
274  void (*destroy_connection)(int vid);
275 
282  void (*guest_notified)(int vid);
283 
284  void *reserved[1];
285 };
286 
302 __rte_deprecated
303 static __rte_always_inline uint64_t
304 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
305 {
306  struct rte_vhost_mem_region *reg;
307  uint32_t i;
308 
309  for (i = 0; i < mem->nregions; i++) {
310  reg = &mem->regions[i];
311  if (gpa >= reg->guest_phys_addr &&
312  gpa < reg->guest_phys_addr + reg->size) {
313  return gpa - reg->guest_phys_addr +
314  reg->host_user_addr;
315  }
316  }
317 
318  return 0;
319 }
320 
337 __rte_experimental
338 static __rte_always_inline uint64_t
340  uint64_t gpa, uint64_t *len)
341 {
342  struct rte_vhost_mem_region *r;
343  uint32_t i;
344 
345  for (i = 0; i < mem->nregions; i++) {
346  r = &mem->regions[i];
347  if (gpa >= r->guest_phys_addr &&
348  gpa < r->guest_phys_addr + r->size) {
349 
350  if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
351  *len = r->guest_phys_addr + r->size - gpa;
352 
353  return gpa - r->guest_phys_addr +
354  r->host_user_addr;
355  }
356  }
357  *len = 0;
358 
359  return 0;
360 }
361 
362 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
363 
382 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
383 
402 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
403  uint64_t offset, uint64_t len);
404 
405 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
406 
411 int rte_vhost_driver_register(const char *path, uint64_t flags);
412 
413 /* Unregister vhost driver. This is only meaningful to vhost user. */
414 int rte_vhost_driver_unregister(const char *path);
415 
426 __rte_experimental
427 int
428 rte_vhost_driver_attach_vdpa_device(const char *path, int did);
429 
438 __rte_experimental
439 int
440 rte_vhost_driver_detach_vdpa_device(const char *path);
441 
450 __rte_experimental
451 int
452 rte_vhost_driver_get_vdpa_device_id(const char *path);
453 
464 int rte_vhost_driver_set_features(const char *path, uint64_t features);
465 
481 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
482 
495 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
496 
507 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
508 
519 __rte_experimental
520 int
522  uint64_t protocol_features);
523 
534 __rte_experimental
535 int
537  uint64_t *protocol_features);
538 
549 __rte_experimental
550 int
551 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num);
552 
563 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
564 
565 /* Register callbacks. */
566 int rte_vhost_driver_callback_register(const char *path,
567  struct vhost_device_ops const * const ops);
568 
580 int rte_vhost_driver_start(const char *path);
581 
595 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
596 
607 int rte_vhost_get_numa_node(int vid);
608 
623 __rte_deprecated
624 uint32_t rte_vhost_get_queue_num(int vid);
625 
635 uint16_t rte_vhost_get_vring_num(int vid);
636 
651 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
652 
664 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
665 
666 struct rte_mbuf;
667 struct rte_mempool;
684 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
685  struct rte_mbuf **pkts, uint16_t count);
686 
704 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
705  struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
706 
721 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
722 
735 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
736  struct rte_vhost_vring *vring);
737 
750 __rte_experimental
751 int
752 rte_vhost_get_vhost_ring_inflight(int vid, uint16_t vring_idx,
753  struct rte_vhost_ring_inflight *vring);
754 
770 __rte_experimental
771 int
772 rte_vhost_set_inflight_desc_split(int vid, uint16_t vring_idx,
773  uint16_t idx);
774 
793 __rte_experimental
794 int
795 rte_vhost_set_inflight_desc_packed(int vid, uint16_t vring_idx,
796  uint16_t head, uint16_t last, uint16_t *inflight_entry);
797 
810 __rte_experimental
811 int
813  uint16_t vring_idx, uint16_t idx);
814 
830 __rte_experimental
831 int
833  uint16_t vring_idx, uint16_t head);
834 
849 __rte_experimental
850 int
851 rte_vhost_clr_inflight_desc_split(int vid, uint16_t vring_idx,
852  uint16_t last_used_idx, uint16_t idx);
853 
866 __rte_experimental
867 int
868 rte_vhost_clr_inflight_desc_packed(int vid, uint16_t vring_idx,
869  uint16_t head);
870 
882 int rte_vhost_vring_call(int vid, uint16_t vring_idx);
883 
894 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
895 
908 __rte_experimental
909 int
910 rte_vhost_get_log_base(int vid, uint64_t *log_base, uint64_t *log_size);
911 
926 __rte_experimental
927 int
928 rte_vhost_get_vring_base(int vid, uint16_t queue_id,
929  uint16_t *last_avail_idx, uint16_t *last_used_idx);
930 
949 __rte_experimental
950 int
952  uint16_t queue_id, uint16_t *last_avail_idx, uint16_t *last_used_idx);
953 
968 __rte_experimental
969 int
970 rte_vhost_set_vring_base(int vid, uint16_t queue_id,
971  uint16_t last_avail_idx, uint16_t last_used_idx);
972 
985 __rte_experimental
986 int
988  struct rte_vhost_user_extern_ops const * const ops, void *ctx);
989 
998 __rte_experimental
999 int
1001 
1002 #ifdef __cplusplus
1003 }
1004 #endif
1005 
1006 #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:304
__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:153
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:339
__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:219
__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:244
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)