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 */
106 int rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable);
109 * Register vhost driver. path could be different for multiple
112 int rte_vhost_driver_register(const char *path, uint64_t flags);
114 /* Unregister vhost driver. This is only meaningful to vhost user. */
115 int rte_vhost_driver_unregister(const char *path);
118 * Set the feature bits the vhost-user driver supports.
121 * The vhost-user socket file path
125 * 0 on success, -1 on failure
127 int rte_vhost_driver_set_features(const char *path, uint64_t features);
130 * Enable vhost-user driver features.
133 * - the param features should be a subset of the feature bits provided
134 * by rte_vhost_driver_set_features().
135 * - it must be invoked before vhost-user negotiation starts.
138 * The vhost-user socket file path
142 * 0 on success, -1 on failure
144 int rte_vhost_driver_enable_features(const char *path, uint64_t features);
147 * Disable vhost-user driver features.
149 * The two notes at rte_vhost_driver_enable_features() also apply here.
152 * The vhost-user socket file path
154 * Features to disable
156 * 0 on success, -1 on failure
158 int rte_vhost_driver_disable_features(const char *path, uint64_t features);
161 * Get the feature bits before feature negotiation.
164 * The vhost-user socket file path
166 * A pointer to store the queried feature bits
168 * 0 on success, -1 on failure
170 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
173 * Get the feature bits after negotiation
178 * A pointer to store the queried feature bits
180 * 0 on success, -1 on failure
182 int rte_vhost_get_negotiated_features(int vid, uint64_t *features);
184 /* Register callbacks. */
185 int rte_vhost_driver_callback_register(const char *path,
186 struct virtio_net_device_ops const * const ops);
187 /* Start vhost driver session blocking loop. */
188 int rte_vhost_driver_session_start(void);
191 * Get the MTU value of the device if set in QEMU.
194 * virtio-net device ID
196 * The variable to store the MTU value
200 * -EAGAIN: device not yet started
201 * -ENOTSUP: device does not support MTU feature
203 int rte_vhost_get_mtu(int vid, uint16_t *mtu);
206 * Get the numa node from which the virtio net device's memory
210 * virtio-net device ID
213 * The numa node, -1 on failure
215 int rte_vhost_get_numa_node(int vid);
218 * Get the number of queues the device supports.
221 * virtio-net device ID
224 * The number of queues, 0 on failure
226 uint32_t rte_vhost_get_queue_num(int vid);
229 * Get the virtio net device's ifname, which is the vhost-user socket
233 * virtio-net device ID
235 * The buffer to stored the queried ifname
240 * 0 on success, -1 on failure
242 int rte_vhost_get_ifname(int vid, char *buf, size_t len);
245 * Get how many avail entries are left in the queue
248 * virtio-net device ID
253 * num of avail entires left
255 uint16_t rte_vhost_avail_entries(int vid, uint16_t queue_id);
258 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
259 * be received from the physical port or from another virtual device. A packet
260 * count is returned to indicate the number of packets that were succesfully
261 * added to the RX queue.
263 * virtio-net device ID
265 * virtio queue index in mq case
267 * array to contain packets to be enqueued
269 * packets num to be enqueued
271 * num of packets enqueued
273 uint16_t rte_vhost_enqueue_burst(int vid, uint16_t queue_id,
274 struct rte_mbuf **pkts, uint16_t count);
277 * This function gets guest buffers from the virtio device TX virtqueue,
278 * construct host mbufs, copies guest buffer content to host mbufs and
279 * store them in pkts to be processed.
283 * virtio queue index in mq case
285 * mbuf_pool where host mbuf is allocated.
287 * array to contain packets to be dequeued
289 * packets num to be dequeued
291 * num of packets dequeued
293 uint16_t rte_vhost_dequeue_burst(int vid, uint16_t queue_id,
294 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
297 * Get guest mem table: a list of memory regions.
299 * An rte_vhost_vhost_memory object will be allocated internaly, to hold the
300 * guest memory regions. Application should free it at destroy_device()
306 * To store the returned mem regions
308 * 0 on success, -1 on failure
310 int rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem);
313 * Get guest vring info, including the vring address, vring size, etc.
320 * the structure to hold the requested vring info
322 * 0 on success, -1 on failure
324 int rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
325 struct rte_vhost_vring *vring);
327 #endif /* _VIRTIO_NET_H_ */