DPDK  17.08.2
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 
65  uint64_t guest_phys_addr;
66  uint64_t guest_user_addr;
67  uint64_t host_user_addr;
68  uint64_t size;
69  void *mmap_addr;
70  uint64_t mmap_size;
71  int fd;
72 };
73 
78  uint32_t nregions;
79  struct rte_vhost_mem_region regions[];
80 };
81 
82 struct rte_vhost_vring {
83  struct vring_desc *desc;
84  struct vring_avail *avail;
85  struct vring_used *used;
86  uint64_t log_guest_addr;
87 
88  int callfd;
89  int kickfd;
90  uint16_t size;
91 };
92 
97  int (*new_device)(int vid);
98  void (*destroy_device)(int vid);
100  int (*vring_state_changed)(int vid, uint16_t queue_id, int enable);
108  int (*features_changed)(int vid, uint64_t features);
109 
110  void *reserved[4];
111 };
112 
128 __rte_deprecated
129 static __rte_always_inline uint64_t
130 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
131 {
132  struct rte_vhost_mem_region *reg;
133  uint32_t i;
134 
135  for (i = 0; i < mem->nregions; i++) {
136  reg = &mem->regions[i];
137  if (gpa >= reg->guest_phys_addr &&
138  gpa < reg->guest_phys_addr + reg->size) {
139  return gpa - reg->guest_phys_addr +
140  reg->host_user_addr;
141  }
142  }
143 
144  return 0;
145 }
146 
163 static __rte_always_inline uint64_t
165  uint64_t gpa, uint64_t *len)
166 {
167  struct rte_vhost_mem_region *r;
168  uint32_t i;
169 
170  for (i = 0; i < mem->nregions; i++) {
171  r = &mem->regions[i];
172  if (gpa >= r->guest_phys_addr &&
173  gpa < r->guest_phys_addr + r->size) {
174 
175  if (unlikely(*len > r->guest_phys_addr + r->size - gpa))
176  *len = r->guest_phys_addr + r->size - gpa;
177 
178  return gpa - r->guest_phys_addr +
179  r->host_user_addr;
180  }
181  }
182  *len = 0;
183 
184  return 0;
185 }
186 
187 #define RTE_VHOST_NEED_LOG(features) ((features) & (1ULL << VHOST_F_LOG_ALL))
188 
207 void rte_vhost_log_write(int vid, uint64_t addr, uint64_t len);
208 
227 void rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
228  uint64_t offset, uint64_t len);
229 
230 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
231 
236 int rte_vhost_driver_register(const char *path, uint64_t flags);
237 
238 /* Unregister vhost driver. This is only meaningful to vhost user. */
239 int rte_vhost_driver_unregister(const char *path);
240 
251 int rte_vhost_driver_set_features(const char *path, uint64_t features);
252 
268 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
269 
282 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
283 
294 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
295 
306 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
307 
308 /* Register callbacks. */
309 int rte_vhost_driver_callback_register(const char *path,
310  struct vhost_device_ops const * const ops);
311 
323 int rte_vhost_driver_start(const char *path);
324 
338 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
339 
350 int rte_vhost_get_numa_node(int vid);
351 
366 __rte_deprecated
367 uint32_t rte_vhost_get_queue_num(int vid);
368 
378 uint16_t rte_vhost_get_vring_num(int vid);
379 
394 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
395 
407 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
408 
409 struct rte_mbuf;
410 struct rte_mempool;
427 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
428  struct rte_mbuf **pkts, uint16_t count);
429 
447 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
448  struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
449 
464 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
465 
478 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
479  struct rte_vhost_vring *vring);
480 
491 uint32_t rte_vhost_rx_queue_count(int vid, uint16_t qid);
492 
493 #ifdef __cplusplus
494 }
495 #endif
496 
497 #endif /* _RTE_VHOST_H_ */