DPDK  17.11.10
rte_vhost.h
Go to the documentation of this file.
1 /*-
2  * BSD LICENSE
3  *
4  * Copyright(c) 2010-2017 Intel Corporation. All rights reserved.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of Intel Corporation nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef _RTE_VHOST_H_
35 #define _RTE_VHOST_H_
36 
42 #include <stdint.h>
43 #include <sys/eventfd.h>
44 
45 #include <rte_memory.h>
46 #include <rte_mempool.h>
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 /* These are not C++-aware. */
53 #include <linux/vhost.h>
54 #include <linux/virtio_ring.h>
55 
56 #define RTE_VHOST_USER_CLIENT (1ULL << 0)
57 #define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1)
58 #define RTE_VHOST_USER_DEQUEUE_ZERO_COPY (1ULL << 2)
59 #define RTE_VHOST_USER_IOMMU_SUPPORT (1ULL << 3)
60 
66  uint64_t guest_phys_addr;
67  uint64_t guest_user_addr;
68  uint64_t host_user_addr;
69  uint64_t size;
70  void *mmap_addr;
71  uint64_t mmap_size;
72  int fd;
73 };
74 
79  uint32_t nregions;
80  struct rte_vhost_mem_region regions[];
81 };
82 
83 struct rte_vhost_vring {
84  struct vring_desc *desc;
85  struct vring_avail *avail;
86  struct vring_used *used;
87  uint64_t log_guest_addr;
88 
89  int callfd;
90  int kickfd;
91  uint16_t size;
92 };
93 
98  int (*new_device)(int vid);
99  void (*destroy_device)(int vid);
101  int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);
109  int (*features_changed)(int vid, uint64_t features);
110 
111  int (*new_connection)(int vid);
112  void (*destroy_connection)(int vid);
113 
114  void *reserved[2];
115 };
116 
132 __rte_deprecated
133 static __rte_always_inline uint64_t
134 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
135 {
136  struct rte_vhost_mem_region *reg;
137  uint32_t i;
138 
139  for (i = 0; i < mem->nregions; i++) {
140  reg = &mem->regions[i];
141  if (gpa >= reg->guest_phys_addr &&
142  gpa < reg->guest_phys_addr + reg->size) {
143  return gpa - reg->guest_phys_addr +
144  reg->host_user_addr;
145  }
146  }
147 
148  return 0;
149 }
150 
167 static __rte_always_inline uint64_t
169  uint64_t gpa, uint64_t *len)
170 {
171  struct rte_vhost_mem_region *r;
172  uint32_t i;
173 
174  for (i = 0; i < mem->nregions; i++) {
175  r = &mem->regions[i];
176  if (gpa >= r->guest_phys_addr &&
177  gpa < r->guest_phys_addr + r->size) {
178 
179  if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
180  *len = r->guest_phys_addr + r->size - gpa;
181 
182  return gpa - r->guest_phys_addr +
183  r->host_user_addr;
184  }
185  }
186  *len = 0;
187 
188  return 0;
189 }
190 
191 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
192 
211 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
212 
231 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
232  uint64_t offset, uint64_t len);
233 
234 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
235 
240 int rte_vhost_driver_register(const char *path, uint64_t flags);
241 
242 /* Unregister vhost driver. This is only meaningful to vhost user. */
243 int rte_vhost_driver_unregister(const char *path);
244 
255 int rte_vhost_driver_set_features(const char *path, uint64_t features);
256 
272 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
273 
286 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
287 
298 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
299 
310 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
311 
312 /* Register callbacks. */
313 int rte_vhost_driver_callback_register(const char *path,
314  struct vhost_device_ops const * const ops);
315 
327 int rte_vhost_driver_start(const char *path);
328 
342 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
343 
354 int rte_vhost_get_numa_node(int vid);
355 
370 __rte_deprecated
371 uint32_t rte_vhost_get_queue_num(int vid);
372 
382 uint16_t rte_vhost_get_vring_num(int vid);
383 
398 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
399 
411 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
412 
413 struct rte_mbuf;
414 struct rte_mempool;
431 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
432  struct rte_mbuf **pkts, uint16_t count);
433 
451 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
452  struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
453 
468 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
469 
482 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
483  struct rte_vhost_vring *vring);
484 
495 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
496 
497 #ifdef __cplusplus
498 }
499 #endif
500 
501 #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:134
int rte_vhost_driver_start(const char *path)
#define __rte_always_inline
Definition: rte_common.h:139
int rte_vhost_driver_register(const char *path, uint64_t flags)
uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id)
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)
int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx, struct rte_vhost_vring *vring)
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)
int rte_vhost_get_mtu(int vid, uint16_t *mtu)
void(* destroy_device)(int vid)
Definition: rte_vhost.h:99
void * reserved[2]
Definition: rte_vhost.h:114
static __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:168
#define unlikely(x)
uint16_t rte_vhost_get_vring_num(int vid)
int rte_vhost_get_numa_node(int vid)
int(* features_changed)(int vid, uint64_t features)
Definition: rte_vhost.h:109
int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
int rte_vhost_get_negotiated_features(int vid, uint64_t *features)
uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid)
int(* new_device)(int vid)
Definition: rte_vhost.h:98
uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id, struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count)
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:101
int rte_vhost_get_ifname(int vid, char *buf, size_t len)
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)