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