vhost: support indirect Tx descriptors
[dpdk.git] / lib / librte_vhost / vhost.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 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 #include <linux/vhost.h>
35 #include <linux/virtio_net.h>
36 #include <stddef.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 #ifdef RTE_LIBRTE_VHOST_NUMA
40 #include <numaif.h>
41 #endif
42
43 #include <rte_ethdev.h>
44 #include <rte_log.h>
45 #include <rte_string_fns.h>
46 #include <rte_memory.h>
47 #include <rte_malloc.h>
48 #include <rte_virtio_net.h>
49
50 #include "vhost.h"
51
52 #define VHOST_USER_F_PROTOCOL_FEATURES  30
53
54 /* Features supported by this lib. */
55 #define VHOST_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
56                                 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
57                                 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
58                                 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
59                                 (VHOST_SUPPORTS_MQ)            | \
60                                 (1ULL << VIRTIO_F_VERSION_1)   | \
61                                 (1ULL << VHOST_F_LOG_ALL)      | \
62                                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
63                                 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
64                                 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
65                                 (1ULL << VIRTIO_NET_F_CSUM)    | \
66                                 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
67                                 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
68                                 (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
69                                 (1ULL << VIRTIO_RING_F_INDIRECT_DESC))
70
71 uint64_t VHOST_FEATURES = VHOST_SUPPORTED_FEATURES;
72
73 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
74
75 /* device ops to add/remove device to/from data core. */
76 struct virtio_net_device_ops const *notify_ops;
77
78 struct virtio_net *
79 get_device(int vid)
80 {
81         struct virtio_net *dev = vhost_devices[vid];
82
83         if (unlikely(!dev)) {
84                 RTE_LOG(ERR, VHOST_CONFIG,
85                         "(%d) device not found.\n", vid);
86         }
87
88         return dev;
89 }
90
91 static void
92 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
93 {
94         if ((vq->callfd >= 0) && (destroy != 0))
95                 close(vq->callfd);
96         if (vq->kickfd >= 0)
97                 close(vq->kickfd);
98 }
99
100 /*
101  * Unmap any memory, close any file descriptors and
102  * free any memory owned by a device.
103  */
104 void
105 cleanup_device(struct virtio_net *dev, int destroy)
106 {
107         uint32_t i;
108
109         vhost_backend_cleanup(dev);
110
111         for (i = 0; i < dev->virt_qp_nb; i++) {
112                 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ], destroy);
113                 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ], destroy);
114         }
115 }
116
117 /*
118  * Release virtqueues and device memory.
119  */
120 static void
121 free_device(struct virtio_net *dev)
122 {
123         uint32_t i;
124
125         for (i = 0; i < dev->virt_qp_nb; i++)
126                 rte_free(dev->virtqueue[i * VIRTIO_QNUM]);
127
128         rte_free(dev);
129 }
130
131 static void
132 init_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
133 {
134         memset(vq, 0, sizeof(struct vhost_virtqueue));
135
136         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
137         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
138
139         /* Backends are set to -1 indicating an inactive device. */
140         vq->backend = -1;
141
142         /* always set the default vq pair to enabled */
143         if (qp_idx == 0)
144                 vq->enabled = 1;
145 }
146
147 static void
148 init_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
149 {
150         uint32_t base_idx = qp_idx * VIRTIO_QNUM;
151
152         init_vring_queue(dev->virtqueue[base_idx + VIRTIO_RXQ], qp_idx);
153         init_vring_queue(dev->virtqueue[base_idx + VIRTIO_TXQ], qp_idx);
154 }
155
156 static void
157 reset_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
158 {
159         int callfd;
160
161         callfd = vq->callfd;
162         init_vring_queue(vq, qp_idx);
163         vq->callfd = callfd;
164 }
165
166 static void
167 reset_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
168 {
169         uint32_t base_idx = qp_idx * VIRTIO_QNUM;
170
171         reset_vring_queue(dev->virtqueue[base_idx + VIRTIO_RXQ], qp_idx);
172         reset_vring_queue(dev->virtqueue[base_idx + VIRTIO_TXQ], qp_idx);
173 }
174
175 int
176 alloc_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
177 {
178         struct vhost_virtqueue *virtqueue = NULL;
179         uint32_t virt_rx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_RXQ;
180         uint32_t virt_tx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_TXQ;
181
182         virtqueue = rte_malloc(NULL,
183                                sizeof(struct vhost_virtqueue) * VIRTIO_QNUM, 0);
184         if (virtqueue == NULL) {
185                 RTE_LOG(ERR, VHOST_CONFIG,
186                         "Failed to allocate memory for virt qp:%d.\n", qp_idx);
187                 return -1;
188         }
189
190         dev->virtqueue[virt_rx_q_idx] = virtqueue;
191         dev->virtqueue[virt_tx_q_idx] = virtqueue + VIRTIO_TXQ;
192
193         init_vring_queue_pair(dev, qp_idx);
194
195         dev->virt_qp_nb += 1;
196
197         return 0;
198 }
199
200 /*
201  * Reset some variables in device structure, while keeping few
202  * others untouched, such as vid, ifname, virt_qp_nb: they
203  * should be same unless the device is removed.
204  */
205 void
206 reset_device(struct virtio_net *dev)
207 {
208         uint32_t i;
209
210         dev->features = 0;
211         dev->protocol_features = 0;
212         dev->flags = 0;
213
214         for (i = 0; i < dev->virt_qp_nb; i++)
215                 reset_vring_queue_pair(dev, i);
216 }
217
218 /*
219  * Function is called from the CUSE open function. The device structure is
220  * initialised and a new entry is added to the device configuration linked
221  * list.
222  */
223 int
224 vhost_new_device(void)
225 {
226         struct virtio_net *dev;
227         int i;
228
229         dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
230         if (dev == NULL) {
231                 RTE_LOG(ERR, VHOST_CONFIG,
232                         "Failed to allocate memory for new dev.\n");
233                 return -1;
234         }
235
236         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
237                 if (vhost_devices[i] == NULL)
238                         break;
239         }
240         if (i == MAX_VHOST_DEVICE) {
241                 RTE_LOG(ERR, VHOST_CONFIG,
242                         "Failed to find a free slot for new device.\n");
243                 return -1;
244         }
245
246         vhost_devices[i] = dev;
247         dev->vid = i;
248
249         return i;
250 }
251
252 /*
253  * Function is called from the CUSE release function. This function will
254  * cleanup the device and remove it from device configuration linked list.
255  */
256 void
257 vhost_destroy_device(int vid)
258 {
259         struct virtio_net *dev = get_device(vid);
260
261         if (dev == NULL)
262                 return;
263
264         if (dev->flags & VIRTIO_DEV_RUNNING) {
265                 dev->flags &= ~VIRTIO_DEV_RUNNING;
266                 notify_ops->destroy_device(vid);
267         }
268
269         cleanup_device(dev, 1);
270         free_device(dev);
271
272         vhost_devices[vid] = NULL;
273 }
274
275 void
276 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
277 {
278         struct virtio_net *dev;
279         unsigned int len;
280
281         dev = get_device(vid);
282         if (dev == NULL)
283                 return;
284
285         len = if_len > sizeof(dev->ifname) ?
286                 sizeof(dev->ifname) : if_len;
287
288         strncpy(dev->ifname, if_name, len);
289         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
290 }
291
292
293 int
294 rte_vhost_get_numa_node(int vid)
295 {
296 #ifdef RTE_LIBRTE_VHOST_NUMA
297         struct virtio_net *dev = get_device(vid);
298         int numa_node;
299         int ret;
300
301         if (dev == NULL)
302                 return -1;
303
304         ret = get_mempolicy(&numa_node, NULL, 0, dev,
305                             MPOL_F_NODE | MPOL_F_ADDR);
306         if (ret < 0) {
307                 RTE_LOG(ERR, VHOST_CONFIG,
308                         "(%d) failed to query numa node: %d\n", vid, ret);
309                 return -1;
310         }
311
312         return numa_node;
313 #else
314         RTE_SET_USED(vid);
315         return -1;
316 #endif
317 }
318
319 uint32_t
320 rte_vhost_get_queue_num(int vid)
321 {
322         struct virtio_net *dev = get_device(vid);
323
324         if (dev == NULL)
325                 return 0;
326
327         return dev->virt_qp_nb;
328 }
329
330 int
331 rte_vhost_get_ifname(int vid, char *buf, size_t len)
332 {
333         struct virtio_net *dev = get_device(vid);
334
335         if (dev == NULL)
336                 return -1;
337
338         len = RTE_MIN(len, sizeof(dev->ifname));
339
340         strncpy(buf, dev->ifname, len);
341         buf[len - 1] = '\0';
342
343         return 0;
344 }
345
346 uint16_t
347 rte_vhost_avail_entries(int vid, uint16_t queue_id)
348 {
349         struct virtio_net *dev;
350         struct vhost_virtqueue *vq;
351
352         dev = get_device(vid);
353         if (!dev)
354                 return 0;
355
356         vq = dev->virtqueue[queue_id];
357         if (!vq->enabled)
358                 return 0;
359
360         return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
361 }
362
363 int
364 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
365 {
366         struct virtio_net *dev = get_device(vid);
367
368         if (dev == NULL)
369                 return -1;
370
371         if (enable) {
372                 RTE_LOG(ERR, VHOST_CONFIG,
373                         "guest notification isn't supported.\n");
374                 return -1;
375         }
376
377         dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
378         return 0;
379 }
380
381 uint64_t rte_vhost_feature_get(void)
382 {
383         return VHOST_FEATURES;
384 }
385
386 int rte_vhost_feature_disable(uint64_t feature_mask)
387 {
388         VHOST_FEATURES = VHOST_FEATURES & ~feature_mask;
389         return 0;
390 }
391
392 int rte_vhost_feature_enable(uint64_t feature_mask)
393 {
394         if ((feature_mask & VHOST_SUPPORTED_FEATURES) == feature_mask) {
395                 VHOST_FEATURES = VHOST_FEATURES | feature_mask;
396                 return 0;
397         }
398         return -1;
399 }
400
401 /*
402  * Register ops so that we can add/remove device to data core.
403  */
404 int
405 rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const ops)
406 {
407         notify_ops = ops;
408
409         return 0;
410 }