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