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/virtio_ring.h>
44 #include <linux/virtio_net.h>
45 #include <sys/eventfd.h>
46 #include <sys/socket.h>
49 #include <rte_memory.h>
50 #include <rte_mempool.h>
53 /* Used to indicate that the device is running on a data core */
54 #define VIRTIO_DEV_RUNNING 1
56 /* Backend value set by guest. */
57 #define VIRTIO_DEV_STOPPED -1
60 /* Enum for virtqueue management. */
61 enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM};
63 #define BUF_VECTOR_MAX 256
66 * Structure contains buffer address, length and descriptor index
67 * from vring to do scatter RX.
76 * Structure contains variables relevant to RX/TX virtqueues.
78 struct vhost_virtqueue {
79 struct vring_desc *desc; /**< Virtqueue descriptor ring. */
80 struct vring_avail *avail; /**< Virtqueue available ring. */
81 struct vring_used *used; /**< Virtqueue used ring. */
82 uint32_t size; /**< Size of descriptor ring. */
83 uint32_t backend; /**< Backend value to determine if device should started/stopped. */
84 uint16_t vhost_hlen; /**< Vhost header length (varies depending on RX merge buffers. */
85 volatile uint16_t last_used_idx; /**< Last index used on the available ring */
86 volatile uint16_t last_used_idx_res; /**< Used for multiple devices reserving buffers. */
87 eventfd_t callfd; /**< Currently unused as polling mode is enabled. */
88 eventfd_t kickfd; /**< Used to notify the guest (trigger interrupt). */
89 struct buf_vector buf_vec[BUF_VECTOR_MAX]; /**< for scatter RX. */
90 } __rte_cache_aligned;
93 * Device structure contains all configuration information relating to the device.
96 struct vhost_virtqueue *virtqueue[VIRTIO_QNUM]; /**< Contains all virtqueue information. */
97 struct virtio_memory *mem; /**< QEMU memory and memory region information. */
98 uint64_t features; /**< Negotiated feature set. */
99 uint64_t device_fh; /**< device identifier. */
100 uint32_t flags; /**< Device flags. Only used to check if device is running on data core. */
101 char ifname[IFNAMSIZ]; /**< Name of the tap device. */
102 void *priv; /**< private context */
103 } __rte_cache_aligned;
106 * Information relating to memory regions including offsets to addresses in QEMUs memory file.
108 struct virtio_memory_regions {
109 uint64_t guest_phys_address; /**< Base guest physical address of region. */
110 uint64_t guest_phys_address_end; /**< End guest physical address of region. */
111 uint64_t memory_size; /**< Size of region. */
112 uint64_t userspace_address; /**< Base userspace address of region. */
113 uint64_t address_offset; /**< Offset of region for address translation. */
118 * Memory structure includes region and mapping information.
120 struct virtio_memory {
121 uint64_t base_address; /**< Base QEMU userspace address of the memory file. */
122 uint64_t mapped_address; /**< Mapped address of memory file base in our applications memory space. */
123 uint64_t mapped_size; /**< Total size of memory file. */
124 uint32_t nregions; /**< Number of memory regions. */
125 struct virtio_memory_regions regions[0]; /**< Memory region information. */
129 * Device operations to add/remove device.
131 struct virtio_net_device_ops {
132 int (*new_device)(struct virtio_net *); /**< Add device. */
133 void (*destroy_device)(volatile struct virtio_net *); /**< Remove device. */
136 static inline uint16_t __attribute__((always_inline))
137 rte_vring_available_entries(struct virtio_net *dev, uint16_t queue_id)
139 struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
140 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx_res;
144 * Function to convert guest physical addresses to vhost virtual addresses.
145 * This is used to convert guest virtio buffer addresses.
147 static inline uint64_t __attribute__((always_inline))
148 gpa_to_vva(struct virtio_net *dev, uint64_t guest_pa)
150 struct virtio_memory_regions *region;
152 uint64_t vhost_va = 0;
154 for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
155 region = &dev->mem->regions[regionidx];
156 if ((guest_pa >= region->guest_phys_address) &&
157 (guest_pa <= region->guest_phys_address_end)) {
158 vhost_va = region->address_offset + guest_pa;
166 * Disable features in feature_mask. Returns 0 on success.
168 int rte_vhost_feature_disable(uint64_t feature_mask);
171 * Enable features in feature_mask. Returns 0 on success.
173 int rte_vhost_feature_enable(uint64_t feature_mask);
175 /* Returns currently supported vhost features */
176 uint64_t rte_vhost_feature_get(void);
178 int rte_vhost_enable_guest_notification(struct virtio_net *dev, uint16_t queue_id, int enable);
180 /* Register vhost driver. dev_name could be different for multiple instance support. */
181 int rte_vhost_driver_register(const char *dev_name);
183 /* Register callbacks. */
184 int rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const);
185 /* Start vhost driver session blocking loop. */
186 int rte_vhost_driver_session_start(void);
189 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
190 * be received from the physical port or from another virtual device. A packet
191 * count is returned to indicate the number of packets that were succesfully
192 * added to the RX queue.
194 * virtio queue index in mq case
196 * num of packets enqueued
198 uint16_t rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id,
199 struct rte_mbuf **pkts, uint16_t count);
202 * This function gets guest buffers from the virtio device TX virtqueue,
203 * construct host mbufs, copies guest buffer content to host mbufs and
204 * store them in pkts to be processed.
206 * mbuf_pool where host mbuf is allocated.
208 * virtio queue index in mq case.
210 * num of packets dequeued
212 uint16_t rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,
213 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
215 #endif /* _VIRTIO_NET_H_ */