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