mbuf: fix performance with 128-byte cache line
[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 /**
38  * @file
39  * Interface to vhost net
40  */
41
42 #include <stdint.h>
43 #include <linux/virtio_ring.h>
44 #include <linux/virtio_net.h>
45 #include <sys/eventfd.h>
46 #include <sys/socket.h>
47 #include <linux/if.h>
48
49 #include <rte_memory.h>
50 #include <rte_mempool.h>
51
52 struct rte_mbuf;
53
54 #define VHOST_MEMORY_MAX_NREGIONS 8
55
56 /* Used to indicate that the device is running on a data core */
57 #define VIRTIO_DEV_RUNNING 1
58
59 /* Backend value set by guest. */
60 #define VIRTIO_DEV_STOPPED -1
61
62
63 /* Enum for virtqueue management. */
64 enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM};
65
66 #define BUF_VECTOR_MAX 256
67
68 /**
69  * Structure contains buffer address, length and descriptor index
70  * from vring to do scatter RX.
71  */
72 struct buf_vector {
73         uint64_t buf_addr;
74         uint32_t buf_len;
75         uint32_t desc_idx;
76 };
77
78 /**
79  * Structure contains variables relevant to RX/TX virtqueues.
80  */
81 struct vhost_virtqueue {
82         struct vring_desc       *desc;                  /**< Virtqueue descriptor ring. */
83         struct vring_avail      *avail;                 /**< Virtqueue available ring. */
84         struct vring_used       *used;                  /**< Virtqueue used ring. */
85         uint32_t                size;                   /**< Size of descriptor ring. */
86         uint32_t                backend;                /**< Backend value to determine if device should started/stopped. */
87         uint16_t                vhost_hlen;             /**< Vhost header length (varies depending on RX merge buffers. */
88         volatile uint16_t       last_used_idx;          /**< Last index used on the available ring */
89         volatile uint16_t       last_used_idx_res;      /**< Used for multiple devices reserving buffers. */
90         int                     callfd;                 /**< Used to notify the guest (trigger interrupt). */
91         int                     kickfd;                 /**< Currently unused as polling mode is enabled. */
92         int                     enabled;
93         uint64_t                reserved[16];           /**< Reserve some spaces for future extension. */
94         struct buf_vector       buf_vec[BUF_VECTOR_MAX];        /**< for scatter RX. */
95 } __rte_cache_aligned;
96
97
98 /*
99  * Make an extra wrapper for VIRTIO_NET_F_MQ and
100  * VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX as they are
101  * introduced since kernel v3.8. This makes our
102  * code buildable for older kernel.
103  */
104 #ifdef VIRTIO_NET_F_MQ
105  #define VHOST_MAX_QUEUE_PAIRS  VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX
106  #define VHOST_SUPPORTS_MQ      (1ULL << VIRTIO_NET_F_MQ)
107 #else
108  #define VHOST_MAX_QUEUE_PAIRS  1
109  #define VHOST_SUPPORTS_MQ      0
110 #endif
111
112 /*
113  * Define virtio 1.0 for older kernels
114  */
115 #ifndef VIRTIO_F_VERSION_1
116  #define VIRTIO_F_VERSION_1 32
117 #endif
118
119 /**
120  * Device structure contains all configuration information relating to the device.
121  */
122 struct virtio_net {
123         struct virtio_memory    *mem;           /**< QEMU memory and memory region information. */
124         uint64_t                features;       /**< Negotiated feature set. */
125         uint64_t                protocol_features;      /**< Negotiated protocol feature set. */
126         uint64_t                device_fh;      /**< device identifier. */
127         uint32_t                flags;          /**< Device flags. Only used to check if device is running on data core. */
128 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
129         char                    ifname[IF_NAME_SZ];     /**< Name of the tap device or socket path. */
130         uint32_t                virt_qp_nb;     /**< number of queue pair we have allocated */
131         void                    *priv;          /**< private context */
132         uint64_t                reserved[64];   /**< Reserve some spaces for future extension. */
133         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];  /**< Contains all virtqueue information. */
134 } __rte_cache_aligned;
135
136 /**
137  * Information relating to memory regions including offsets to addresses in QEMUs memory file.
138  */
139 struct virtio_memory_regions {
140         uint64_t        guest_phys_address;     /**< Base guest physical address of region. */
141         uint64_t        guest_phys_address_end; /**< End guest physical address of region. */
142         uint64_t        memory_size;            /**< Size of region. */
143         uint64_t        userspace_address;      /**< Base userspace address of region. */
144         uint64_t        address_offset;         /**< Offset of region for address translation. */
145 };
146
147
148 /**
149  * Memory structure includes region and mapping information.
150  */
151 struct virtio_memory {
152         uint64_t        base_address;   /**< Base QEMU userspace address of the memory file. */
153         uint64_t        mapped_address; /**< Mapped address of memory file base in our applications memory space. */
154         uint64_t        mapped_size;    /**< Total size of memory file. */
155         uint32_t        nregions;       /**< Number of memory regions. */
156         struct virtio_memory_regions      regions[0]; /**< Memory region information. */
157 };
158
159 /**
160  * Device and vring operations.
161  *
162  * Make sure to set VIRTIO_DEV_RUNNING to the device flags in new_device and
163  * remove it in destroy_device.
164  *
165  */
166 struct virtio_net_device_ops {
167         int (*new_device)(struct virtio_net *); /**< Add device. */
168         void (*destroy_device)(volatile struct virtio_net *);   /**< Remove device. */
169
170         int (*vring_state_changed)(struct virtio_net *dev, uint16_t queue_id, int enable);      /**< triggered when a vring is enabled or disabled */
171 };
172
173 static inline uint16_t __attribute__((always_inline))
174 rte_vring_available_entries(struct virtio_net *dev, uint16_t queue_id)
175 {
176         struct vhost_virtqueue *vq = dev->virtqueue[queue_id];
177
178         if (!vq->enabled)
179                 return 0;
180
181         return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx_res;
182 }
183
184 /**
185  * Function to convert guest physical addresses to vhost virtual addresses.
186  * This is used to convert guest virtio buffer addresses.
187  */
188 static inline uint64_t __attribute__((always_inline))
189 gpa_to_vva(struct virtio_net *dev, uint64_t guest_pa)
190 {
191         struct virtio_memory_regions *region;
192         uint32_t regionidx;
193         uint64_t vhost_va = 0;
194
195         for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
196                 region = &dev->mem->regions[regionidx];
197                 if ((guest_pa >= region->guest_phys_address) &&
198                         (guest_pa <= region->guest_phys_address_end)) {
199                         vhost_va = region->address_offset + guest_pa;
200                         break;
201                 }
202         }
203         return vhost_va;
204 }
205
206 /**
207  *  Disable features in feature_mask. Returns 0 on success.
208  */
209 int rte_vhost_feature_disable(uint64_t feature_mask);
210
211 /**
212  *  Enable features in feature_mask. Returns 0 on success.
213  */
214 int rte_vhost_feature_enable(uint64_t feature_mask);
215
216 /* Returns currently supported vhost features */
217 uint64_t rte_vhost_feature_get(void);
218
219 int rte_vhost_enable_guest_notification(struct virtio_net *dev, uint16_t queue_id, int enable);
220
221 /* Register vhost driver. dev_name could be different for multiple instance support. */
222 int rte_vhost_driver_register(const char *dev_name);
223
224 /* Unregister vhost driver. This is only meaningful to vhost user. */
225 int rte_vhost_driver_unregister(const char *dev_name);
226
227 /* Register callbacks. */
228 int rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const);
229 /* Start vhost driver session blocking loop. */
230 int rte_vhost_driver_session_start(void);
231
232 /**
233  * This function adds buffers to the virtio devices RX virtqueue. Buffers can
234  * be received from the physical port or from another virtual device. A packet
235  * count is returned to indicate the number of packets that were succesfully
236  * added to the RX queue.
237  * @param dev
238  *  virtio-net device
239  * @param queue_id
240  *  virtio queue index in mq case
241  * @param pkts
242  *  array to contain packets to be enqueued
243  * @param count
244  *  packets num to be enqueued
245  * @return
246  *  num of packets enqueued
247  */
248 uint16_t rte_vhost_enqueue_burst(struct virtio_net *dev, uint16_t queue_id,
249         struct rte_mbuf **pkts, uint16_t count);
250
251 /**
252  * This function gets guest buffers from the virtio device TX virtqueue,
253  * construct host mbufs, copies guest buffer content to host mbufs and
254  * store them in pkts to be processed.
255  * @param dev
256  *  virtio-net device
257  * @param queue_id
258  *  virtio queue index in mq case
259  * @param mbuf_pool
260  *  mbuf_pool where host mbuf is allocated.
261  * @param pkts
262  *  array to contain packets to be dequeued
263  * @param count
264  *  packets num to be dequeued
265  * @return
266  *  num of packets dequeued
267  */
268 uint16_t rte_vhost_dequeue_burst(struct virtio_net *dev, uint16_t queue_id,
269         struct rte_mempool *mbuf_pool, struct rte_mbuf **pkts, uint16_t count);
270
271 #endif /* _VIRTIO_NET_H_ */