vhost: make buffer vector for scatter Rx local
[dpdk.git] / lib / librte_vhost / vhost-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 _VHOST_NET_CDEV_H_
35 #define _VHOST_NET_CDEV_H_
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <linux/vhost.h>
41
42 #include <rte_log.h>
43
44 #include "rte_virtio_net.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 #define BUF_VECTOR_MAX 256
53
54 /**
55  * Structure contains buffer address, length and descriptor index
56  * from vring to do scatter RX.
57  */
58 struct buf_vector {
59         uint64_t buf_addr;
60         uint32_t buf_len;
61         uint32_t desc_idx;
62 };
63
64 /**
65  * Structure contains variables relevant to RX/TX virtqueues.
66  */
67 struct vhost_virtqueue {
68         struct vring_desc       *desc;
69         struct vring_avail      *avail;
70         struct vring_used       *used;
71         uint32_t                size;
72
73         /* Last index used on the available ring */
74         volatile uint16_t       last_used_idx;
75         /* Used for multiple devices reserving buffers */
76         volatile uint16_t       last_used_idx_res;
77 #define VIRTIO_INVALID_EVENTFD          (-1)
78 #define VIRTIO_UNINITIALIZED_EVENTFD    (-2)
79
80         /* Backend value to determine if device should started/stopped */
81         int                     backend;
82         /* Used to notify the guest (trigger interrupt) */
83         int                     callfd;
84         /* Currently unused as polling mode is enabled */
85         int                     kickfd;
86         int                     enabled;
87
88         /* Physical address of used ring, for logging */
89         uint64_t                log_guest_addr;
90 } __rte_cache_aligned;
91
92 /* Old kernels have no such macro defined */
93 #ifndef VIRTIO_NET_F_GUEST_ANNOUNCE
94  #define VIRTIO_NET_F_GUEST_ANNOUNCE 21
95 #endif
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
121  * to the device.
122  */
123 struct virtio_net {
124         /* Frontend (QEMU) memory and memory region information */
125         struct virtio_memory    *mem;
126         uint64_t                features;
127         uint64_t                protocol_features;
128         int                     vid;
129         uint32_t                flags;
130         uint16_t                vhost_hlen;
131 #define IF_NAME_SZ (PATH_MAX > IFNAMSIZ ? PATH_MAX : IFNAMSIZ)
132         char                    ifname[IF_NAME_SZ];
133         uint32_t                virt_qp_nb;
134         uint64_t                log_size;
135         uint64_t                log_base;
136         struct ether_addr       mac;
137
138         /* to tell if we need broadcast rarp packet */
139         rte_atomic16_t          broadcast_rarp;
140         struct vhost_virtqueue  *virtqueue[VHOST_MAX_QUEUE_PAIRS * 2];
141 } __rte_cache_aligned;
142
143 /**
144  * Information relating to memory regions including offsets to
145  * addresses in QEMUs memory file.
146  */
147 struct virtio_memory_regions {
148         uint64_t guest_phys_address;
149         uint64_t guest_phys_address_end;
150         uint64_t memory_size;
151         uint64_t userspace_address;
152         uint64_t address_offset;
153 };
154
155
156 /**
157  * Memory structure includes region and mapping information.
158  */
159 struct virtio_memory {
160         /* Base QEMU userspace address of the memory file. */
161         uint64_t base_address;
162         uint64_t mapped_address;
163         uint64_t mapped_size;
164         uint32_t nregions;
165         struct virtio_memory_regions regions[0];
166 };
167
168
169 /* Macros for printing using RTE_LOG */
170 #define RTE_LOGTYPE_VHOST_CONFIG RTE_LOGTYPE_USER1
171 #define RTE_LOGTYPE_VHOST_DATA   RTE_LOGTYPE_USER1
172
173 #ifdef RTE_LIBRTE_VHOST_DEBUG
174 #define VHOST_MAX_PRINT_BUFF 6072
175 #define LOG_LEVEL RTE_LOG_DEBUG
176 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
177 #define PRINT_PACKET(device, addr, size, header) do { \
178         char *pkt_addr = (char *)(addr); \
179         unsigned int index; \
180         char packet[VHOST_MAX_PRINT_BUFF]; \
181         \
182         if ((header)) \
183                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Header size %d: ", (device->vid), (size)); \
184         else \
185                 snprintf(packet, VHOST_MAX_PRINT_BUFF, "(%d) Packet size %d: ", (device->vid), (size)); \
186         for (index = 0; index < (size); index++) { \
187                 snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), \
188                         "%02hhx ", pkt_addr[index]); \
189         } \
190         snprintf(packet + strnlen(packet, VHOST_MAX_PRINT_BUFF), VHOST_MAX_PRINT_BUFF - strnlen(packet, VHOST_MAX_PRINT_BUFF), "\n"); \
191         \
192         LOG_DEBUG(VHOST_DATA, "%s", packet); \
193 } while (0)
194 #else
195 #define LOG_LEVEL RTE_LOG_INFO
196 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
197 #define PRINT_PACKET(device, addr, size, header) do {} while (0)
198 #endif
199
200 /**
201  * Function to convert guest physical addresses to vhost virtual addresses.
202  * This is used to convert guest virtio buffer addresses.
203  */
204 static inline uint64_t __attribute__((always_inline))
205 gpa_to_vva(struct virtio_net *dev, uint64_t guest_pa)
206 {
207         struct virtio_memory_regions *region;
208         uint32_t regionidx;
209         uint64_t vhost_va = 0;
210
211         for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
212                 region = &dev->mem->regions[regionidx];
213                 if ((guest_pa >= region->guest_phys_address) &&
214                         (guest_pa <= region->guest_phys_address_end)) {
215                         vhost_va = region->address_offset + guest_pa;
216                         break;
217                 }
218         }
219         return vhost_va;
220 }
221
222 struct virtio_net_device_ops const *notify_ops;
223 struct virtio_net *get_device(int vid);
224
225 int vhost_new_device(void);
226 void vhost_destroy_device(int);
227
228 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
229
230 int vhost_get_features(int, uint64_t *);
231 int vhost_set_features(int, uint64_t *);
232
233 int vhost_set_vring_num(int, struct vhost_vring_state *);
234 int vhost_set_vring_addr(int, struct vhost_vring_addr *);
235 int vhost_set_vring_base(int, struct vhost_vring_state *);
236 int vhost_get_vring_base(int, uint32_t, struct vhost_vring_state *);
237
238 int vhost_set_vring_kick(int, struct vhost_vring_file *);
239 int vhost_set_vring_call(int, struct vhost_vring_file *);
240
241 int vhost_set_backend(int, struct vhost_vring_file *);
242
243 int vhost_set_owner(int);
244 int vhost_reset_owner(int);
245
246 /*
247  * Backend-specific cleanup. Defined by vhost-cuse and vhost-user.
248  */
249 void vhost_backend_cleanup(struct virtio_net *dev);
250
251 #endif /* _VHOST_NET_CDEV_H_ */