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_
37 /* Used to indicate that the device is running on a data core */
38 #define VIRTIO_DEV_RUNNING 1
40 /* Backend value set by guest. */
41 #define VIRTIO_DEV_STOPPED -1
44 /* Enum for virtqueue management. */
45 enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM};
47 #define BUF_VECTOR_MAX 256
50 * Structure contains buffer address, length and descriptor index
51 * from vring to do scatter RX.
60 * Structure contains variables relevant to TX/RX virtqueues.
62 struct vhost_virtqueue
64 struct vring_desc *desc; /* Virtqueue descriptor ring. */
65 struct vring_avail *avail; /* Virtqueue available ring. */
66 struct vring_used *used; /* Virtqueue used ring. */
67 uint32_t size; /* Size of descriptor ring. */
68 uint32_t backend; /* Backend value to determine if device should started/stopped. */
69 uint16_t vhost_hlen; /* Vhost header length (varies depending on RX merge buffers. */
70 volatile uint16_t last_used_idx; /* Last index used on the available ring */
71 volatile uint16_t last_used_idx_res; /* Used for multiple devices reserving buffers. */
72 eventfd_t callfd; /* Currently unused as polling mode is enabled. */
73 eventfd_t kickfd; /* Used to notify the guest (trigger interrupt). */
74 /* Used for scatter RX. */
75 struct buf_vector buf_vec[BUF_VECTOR_MAX];
76 } __rte_cache_aligned;
79 * Device structure contains all configuration information relating to the device.
83 struct vhost_virtqueue *virtqueue[VIRTIO_QNUM]; /* Contains all virtqueue information. */
84 struct virtio_memory *mem; /* QEMU memory and memory region information. */
85 uint64_t features; /* Negotiated feature set. */
86 uint64_t device_fh; /* device identifier. */
87 uint32_t flags; /* Device flags. Only used to check if device is running on data core. */
88 } __rte_cache_aligned;
91 * Information relating to memory regions including offsets to addresses in QEMUs memory file.
93 struct virtio_memory_regions {
94 uint64_t guest_phys_address; /* Base guest physical address of region. */
95 uint64_t guest_phys_address_end; /* End guest physical address of region. */
96 uint64_t memory_size; /* Size of region. */
97 uint64_t userspace_address; /* Base userspace address of region. */
98 uint64_t address_offset; /* Offset of region for address translation. */
103 * Memory structure includes region and mapping information.
105 struct virtio_memory {
106 uint64_t base_address; /* Base QEMU userspace address of the memory file. */
107 uint64_t mapped_address; /* Mapped address of memory file base in our applications memory space. */
108 uint64_t mapped_size; /* Total size of memory file. */
109 uint32_t nregions; /* Number of memory regions. */
110 /* Memory region information. */
111 struct virtio_memory_regions regions[0];
115 * Device operations to add/remove device.
117 struct virtio_net_device_ops {
118 int (* new_device) (struct virtio_net *); /* Add device. */
119 void (* destroy_device) (volatile struct virtio_net *); /* Remove device. */
122 int init_virtio_net(struct virtio_net_device_ops const * const);
123 int deinit_virtio_net(void);
125 struct vhost_net_device_ops const * get_virtio_net_callbacks(void);
128 * Function to convert guest physical addresses to vhost virtual addresses.
129 * This is used to convert guest virtio buffer addresses.
131 static inline uint64_t __attribute__((always_inline))
132 gpa_to_vva(struct virtio_net *dev, uint64_t guest_pa)
134 struct virtio_memory_regions *region;
136 uint64_t vhost_va = 0;
138 for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
139 region = &dev->mem->regions[regionidx];
140 if ((guest_pa >= region->guest_phys_address) &&
141 (guest_pa <= region->guest_phys_address_end)) {
142 vhost_va = region->address_offset + guest_pa;
149 /* Register vhost driver. dev_name could be different for multiple instance support. */
150 int rte_vhost_driver_register(const char *dev_name);
152 /* Start vhost driver session blocking loop. */
153 int rte_vhost_driver_session_start(void);
156 * This function adds buffers to the virtio devices RX virtqueue. Buffers can
157 * be received from the physical port or from another virtual device. A packet
158 * count is returned to indicate the number of packets that were succesfully
159 * added to the RX queue.
161 * virtio queue index in mq case
163 * num of packets enqueued
165 uint16_t rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id,
166 struct rte_mbuf **pkts, uint16_t count);
169 * This function gets guest buffers from the virtio device TX virtqueue,
170 * construct host mbufs, copies guest buffer content to host mbufs and
171 * store them in pkts to be processed.
173 * mbuf_pool where host mbuf is allocated.
175 * virtio queue index in mq case.
177 * num of packets dequeued
179 uint16_t rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,
180 struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
182 #endif /* _VIRTIO_NET_H_ */