4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #ifndef _VIRTIO_NET_H_
35 #define _VIRTIO_NET_H_
39 * Interface to vhost net
43 #include <linux/vhost.h>
44 #include <linux/virtio_ring.h>
45 #include <linux/virtio_net.h>
46 #include <sys/eventfd.h>
47 #include <sys/socket.h>
50 #include <rte_memory.h>
51 #include <rte_mempool.h>
52 #include <rte_ether.h>
54 #define RTE_VHOST_USER_CLIENT (1ULL << 0)
55 #define RTE_VHOST_USER_NO_RECONNECT (1ULL << 1)
56 #define RTE_VHOST_USER_DEQUEUE_ZERO_COPY (1ULL << 2)
58 /* Enum for virtqueue management. */
59 enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM};
62 * Information relating to memory regions including offsets to
63 * addresses in QEMUs memory file.
65 struct rte_vhost_mem_region {
66 uint64_t guest_phys_addr;
67 uint64_t guest_user_addr;
68 uint64_t host_user_addr;
76 * Memory structure includes region and mapping information.
78 struct rte_vhost_memory {
80 struct rte_vhost_mem_region regions[0];
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;
95 * Device and vring operations.
97 struct virtio_net_device_ops {
98 int (*new_device)(int vid); /**< Add device. */
99 void (*destroy_device)(int vid); /**< Remove device. */
101 int (*vring_state_changed)(int vid, uint16_t queue_id, int enable); /**< triggered when a vring is enabled or disabled */
103 void *reserved[5]; /**< Reserved for future extension */
107 * Convert guest physical address to host virtual address
110 * the guest memory regions
112 * the guest physical address for querying
114 * the host virtual address on success, 0 on failure
116 static inline uint64_t __attribute__((always_inline))
117 rte_vhost_gpa_to_vva(struct rte_vhost_memory *mem, uint64_t gpa)
119 struct rte_vhost_mem_region *reg;
122 for (i = 0; i < mem->nregions; i++) {
123 reg = &mem->regions[i];
124 if (gpa >= reg->guest_phys_addr &&
125 gpa < reg->guest_phys_addr + reg->size) {
126 return gpa - reg->guest_phys_addr +
134 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
137 * Register vhost driver. path could be different for multiple
140 int rte_vhost_driver_register(const char *path, uint64_t flags);
142 /* Unregister vhost driver. This is only meaningful to vhost user. */
143 int rte_vhost_driver_unregister(const char *path);
146 * Set the feature bits the vhost-user driver supports.
149 * The vhost-user socket file path
153 * 0 on success, -1 on failure
155 int rte_vhost_driver_set_features(const char *path, uint64_t features);
158 * Enable vhost-user driver features.
161 * - the param features should be a subset of the feature bits provided
162 * by rte_vhost_driver_set_features().
163 * - it must be invoked before vhost-user negotiation starts.
166 * The vhost-user socket file path
170 * 0 on success, -1 on failure
172 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
175 * Disable vhost-user driver features.
177 * The two notes at rte_vhost_driver_enable_features() also apply here.
180 * The vhost-user socket file path
182 * Features to disable
184 * 0 on success, -1 on failure
186 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
189 * Get the feature bits before feature negotiation.
192 * The vhost-user socket file path
194 * A pointer to store the queried feature bits
196 * 0 on success, -1 on failure
198 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
201 * Get the feature bits after negotiation
206 * A pointer to store the queried feature bits
208 * 0 on success, -1 on failure
210 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
212 /* Register callbacks. */
213 int rte_vhost_driver_callback_register(const char *path,
214 struct virtio_net_device_ops const * const ops);
215 /* Start vhost driver session blocking loop. */
216 int rte_vhost_driver_session_start(void);
219 * Get the MTU value of the device if set in QEMU.
222 * virtio-net device ID
224 * The variable to store the MTU value
228 * -EAGAIN: device not yet started
229 * -ENOTSUP: device does not support MTU feature
231 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
234 * Get the numa node from which the virtio net device's memory
238 * virtio-net device ID
241 * The numa node, -1 on failure
243 int rte_vhost_get_numa_node(int vid);
247 * Get the number of queues the device supports.
249 * Note this function is deprecated, as it returns a queue pair number,
250 * which is virtio-net specific. Instead, rte_vhost_get_vring_num should
254 * virtio-net device ID
257 * The number of queues, 0 on failure
260 uint32_t rte_vhost_get_queue_num(int vid);
263 * Get the number of vrings the device supports.
269 * The number of vrings, 0 on failure
271 uint16_t rte_vhost_get_vring_num(int vid);
274 * Get the virtio net device's ifname, which is the vhost-user socket
278 * virtio-net device ID
280 * The buffer to stored the queried ifname
285 * 0 on success, -1 on failure
287 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
290 * Get how many avail entries are left in the queue
293 * virtio-net device ID
298 * num of avail entires left
300 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
303 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
304 * be received from the physical port or from another virtual device. A packet
305 * count is returned to indicate the number of packets that were succesfully
306 * added to the RX queue.
308 * virtio-net device ID
310 * virtio queue index in mq case
312 * array to contain packets to be enqueued
314 * packets num to be enqueued
316 * num of packets enqueued
318 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
319 struct rte_mbuf **pkts, uint16_t count);
322 * This function gets guest buffers from the virtio device TX virtqueue,
323 * construct host mbufs, copies guest buffer content to host mbufs and
324 * store them in pkts to be processed.
328 * virtio queue index in mq case
330 * mbuf_pool where host mbuf is allocated.
332 * array to contain packets to be dequeued
334 * packets num to be dequeued
336 * num of packets dequeued
338 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
339 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
342 * Get guest mem table: a list of memory regions.
344 * An rte_vhost_vhost_memory object will be allocated internaly, to hold the
345 * guest memory regions. Application should free it at destroy_device()
351 * To store the returned mem regions
353 * 0 on success, -1 on failure
355 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
358 * Get guest vring info, including the vring address, vring size, etc.
365 * the structure to hold the requested vring info
367 * 0 on success, -1 on failure
369 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
370 struct rte_vhost_vring *vring);
372 #endif /* _VIRTIO_NET_H_ */