adf35e4f9ff9bd0fc4b152ae22fdb910a6398cd9
[dpdk.git] / lib / librte_vhost / rte_virtio_net.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 _VIRTIO_NET_H_
35 #define _VIRTIO_NET_H_
36
37 #include <stdint.h>
38 #include <linux/virtio_ring.h>
39 #include <linux/virtio_net.h>
40 #include <sys/eventfd.h>
41
42 #include <rte_memory.h>
43 #include <rte_mempool.h>
44 #include <rte_mbuf.h>
45
46 /* Used to indicate that the device is running on a data core */
47 #define VIRTIO_DEV_RUNNING 1
48
49 /* Backend value set by guest. */
50 #define VIRTIO_DEV_STOPPED -1
51
52
53 /* Enum for virtqueue management. */
54 enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM};
55
56 #define BUF_VECTOR_MAX 256
57
58 /*
59  * Structure contains buffer address, length and descriptor index
60  * from vring to do scatter RX.
61 */
62 struct buf_vector {
63 uint64_t buf_addr;
64 uint32_t buf_len;
65 uint32_t desc_idx;
66 };
67
68 /*
69  * Structure contains variables relevant to TX/RX virtqueues.
70  */
71 struct vhost_virtqueue
72 {
73         struct vring_desc       *desc;                          /* Virtqueue descriptor ring. */
74         struct vring_avail      *avail;                         /* Virtqueue available ring. */
75         struct vring_used       *used;                          /* Virtqueue used ring. */
76         uint32_t                        size;                           /* Size of descriptor ring. */
77         uint32_t                        backend;                        /* Backend value to determine if device should started/stopped. */
78         uint16_t                        vhost_hlen;                     /* Vhost header length (varies depending on RX merge buffers. */
79         volatile uint16_t       last_used_idx;          /* Last index used on the available ring */
80         volatile uint16_t       last_used_idx_res;      /* Used for multiple devices reserving buffers. */
81         eventfd_t                       callfd;                         /* Currently unused as polling mode is enabled. */
82         eventfd_t                       kickfd;                         /* Used to notify the guest (trigger interrupt). */
83         /* Used for scatter RX. */
84         struct buf_vector       buf_vec[BUF_VECTOR_MAX];
85 } __rte_cache_aligned;
86
87 /*
88  * Device structure contains all configuration information relating to the device.
89  */
90 struct virtio_net
91 {
92         struct vhost_virtqueue  *virtqueue[VIRTIO_QNUM];        /* Contains all virtqueue information. */
93         struct virtio_memory    *mem;                                           /* QEMU memory and memory region information. */
94         uint64_t                                features;                                       /* Negotiated feature set. */
95         uint64_t                                device_fh;                                      /* device identifier. */
96         uint32_t                                flags;                                          /* Device flags. Only used to check if device is running on data core. */
97         void                    *priv;                          /**< private context */
98 } __rte_cache_aligned;
99
100 /*
101  * Information relating to memory regions including offsets to addresses in QEMUs memory file.
102  */
103 struct virtio_memory_regions {
104         uint64_t        guest_phys_address;             /* Base guest physical address of region. */
105         uint64_t        guest_phys_address_end; /* End guest physical address of region. */
106         uint64_t        memory_size;                    /* Size of region. */
107         uint64_t        userspace_address;              /* Base userspace address of region. */
108         uint64_t        address_offset;                 /* Offset of region for address translation. */
109 };
110
111
112 /*
113  * Memory structure includes region and mapping information.
114  */
115 struct virtio_memory {
116         uint64_t                        base_address;                   /* Base QEMU userspace address of the memory file. */
117         uint64_t                        mapped_address;                 /* Mapped address of memory file base in our applications memory space. */
118         uint64_t                        mapped_size;                    /* Total size of memory file. */
119         uint32_t                        nregions;                               /* Number of memory regions. */
120         /* Memory region information. */
121         struct virtio_memory_regions      regions[0];
122 };
123
124 /*
125  * Device operations to add/remove device.
126  */
127 struct virtio_net_device_ops {
128         int (* new_device)              (struct virtio_net *);  /* Add device. */
129         void (* destroy_device) (volatile struct virtio_net *); /* Remove device. */
130 };
131
132 static inline uint16_t __attribute__((always_inline))
133 rte_vring_available_entries(struct virtio_net *dev, uint16_t queue_id)
134 {
135         struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
136         return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx_res;
137 }
138
139 /**
140  * Function to convert guest physical addresses to vhost virtual addresses.
141  * This is used to convert guest virtio buffer addresses.
142  */
143 static inline uint64_t __attribute__((always_inline))
144 gpa_to_vva(struct virtio_net *dev, uint64_t guest_pa)
145 {
146         struct virtio_memory_regions *region;
147         uint32_t regionidx;
148         uint64_t vhost_va = 0;
149
150         for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
151                 region = &dev->mem->regions[regionidx];
152                 if ((guest_pa >= region->guest_phys_address) &&
153                         (guest_pa <= region->guest_phys_address_end)) {
154                         vhost_va = region->address_offset + guest_pa;
155                         break;
156                 }
157         }
158         return vhost_va;
159 }
160
161 /**
162  *  Disable features in feature_mask. Returns 0 on success.
163  */
164 int rte_vhost_feature_disable(uint64_t feature_mask);
165
166 /**
167  *  Enable features in feature_mask. Returns 0 on success.
168  */
169 int rte_vhost_feature_enable(uint64_t feature_mask);
170
171 /* Returns currently supported vhost features */
172 uint64_t rte_vhost_feature_get(void);
173
174 int rte_vhost_enable_guest_notification(struct virtio_net *dev, uint16_t queue_id, int enable);
175
176 /* Register vhost driver. dev_name could be different for multiple instance support. */
177 int rte_vhost_driver_register(const char *dev_name);
178
179 /* Register callbacks. */
180 int rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const);
181 /* Start vhost driver session blocking loop. */
182 int rte_vhost_driver_session_start(void);
183
184 /**
185  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
186  * be received from the physical port or from another virtual device. A packet
187  * count is returned to indicate the number of packets that were succesfully
188  * added to the RX queue.
189  * @param queue_id
190  *  virtio queue index in mq case
191  * @return
192  *  num of packets enqueued
193  */
194 uint16_t rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id,
195         struct rte_mbuf **pkts, uint16_t count);
196
197 /**
198  * This function gets guest buffers from the virtio device TX virtqueue,
199  * construct host mbufs, copies guest buffer content to host mbufs and
200  * store them in pkts to be processed.
201  * @param mbuf_pool
202  *  mbuf_pool where host mbuf is allocated.
203  * @param queue_id
204  *  virtio queue index in mq case.
205  * @return
206  *  num of packets dequeued
207  */
208 uint16_t rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,
209         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
210
211 #endif /* _VIRTIO_NET_H_ */