vhost: initialize vrings IOTLB caches
[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_errno.h>
44 #include <rte_ethdev.h>
45 #include <rte_log.h>
46 #include <rte_string_fns.h>
47 #include <rte_memory.h>
48 #include <rte_malloc.h>
49 #include <rte_vhost.h>
50
51 #include "iotlb.h"
52 #include "vhost.h"
53
54 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
55
56 struct virtio_net *
57 get_device(int vid)
58 {
59         struct virtio_net *dev = vhost_devices[vid];
60
61         if (unlikely(!dev)) {
62                 RTE_LOG(ERR, VHOST_CONFIG,
63                         "(%d) device not found.\n", vid);
64         }
65
66         return dev;
67 }
68
69 static void
70 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
71 {
72         if ((vq->callfd >= 0) && (destroy != 0))
73                 close(vq->callfd);
74         if (vq->kickfd >= 0)
75                 close(vq->kickfd);
76 }
77
78 /*
79  * Unmap any memory, close any file descriptors and
80  * free any memory owned by a device.
81  */
82 void
83 cleanup_device(struct virtio_net *dev, int destroy)
84 {
85         uint32_t i;
86
87         vhost_backend_cleanup(dev);
88
89         for (i = 0; i < dev->nr_vring; i++)
90                 cleanup_vq(dev->virtqueue[i], destroy);
91 }
92
93 /*
94  * Release virtqueues and device memory.
95  */
96 static void
97 free_device(struct virtio_net *dev)
98 {
99         uint32_t i;
100         struct vhost_virtqueue *vq;
101
102         for (i = 0; i < dev->nr_vring; i++) {
103                 vq = dev->virtqueue[i];
104
105                 rte_free(vq->shadow_used_ring);
106                 rte_free(vq->batch_copy_elems);
107                 rte_mempool_free(vq->iotlb_pool);
108                 rte_free(vq);
109         }
110
111         rte_free(dev);
112 }
113
114 static void
115 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
116 {
117         struct vhost_virtqueue *vq;
118
119         if (vring_idx >= VHOST_MAX_VRING) {
120                 RTE_LOG(ERR, VHOST_CONFIG,
121                                 "Failed not init vring, out of bound (%d)\n",
122                                 vring_idx);
123                 return;
124         }
125
126         vq = dev->virtqueue[vring_idx];
127
128         memset(vq, 0, sizeof(struct vhost_virtqueue));
129
130         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
131         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
132
133         vhost_user_iotlb_init(dev, vring_idx);
134         /* Backends are set to -1 indicating an inactive device. */
135         vq->backend = -1;
136
137         /*
138          * always set the vq to enabled; this is to keep compatibility
139          * with the old QEMU, whereas there is no SET_VRING_ENABLE message.
140          */
141         vq->enabled = 1;
142
143         TAILQ_INIT(&vq->zmbuf_list);
144 }
145
146 static void
147 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
148 {
149         struct vhost_virtqueue *vq;
150         int callfd;
151
152         if (vring_idx >= VHOST_MAX_VRING) {
153                 RTE_LOG(ERR, VHOST_CONFIG,
154                                 "Failed not init vring, out of bound (%d)\n",
155                                 vring_idx);
156                 return;
157         }
158
159         vq = dev->virtqueue[vring_idx];
160         callfd = vq->callfd;
161         init_vring_queue(dev, vring_idx);
162         vq->callfd = callfd;
163 }
164
165 int
166 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
167 {
168         struct vhost_virtqueue *vq;
169
170         vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
171         if (vq == NULL) {
172                 RTE_LOG(ERR, VHOST_CONFIG,
173                         "Failed to allocate memory for vring:%u.\n", vring_idx);
174                 return -1;
175         }
176
177         dev->virtqueue[vring_idx] = vq;
178         init_vring_queue(dev, vring_idx);
179
180         dev->nr_vring += 1;
181
182         return 0;
183 }
184
185 /*
186  * Reset some variables in device structure, while keeping few
187  * others untouched, such as vid, ifname, nr_vring: they
188  * should be same unless the device is removed.
189  */
190 void
191 reset_device(struct virtio_net *dev)
192 {
193         uint32_t i;
194
195         dev->features = 0;
196         dev->protocol_features = 0;
197         dev->flags = 0;
198
199         for (i = 0; i < dev->nr_vring; i++)
200                 reset_vring_queue(dev, i);
201 }
202
203 /*
204  * Invoked when there is a new vhost-user connection established (when
205  * there is a new virtio device being attached).
206  */
207 int
208 vhost_new_device(void)
209 {
210         struct virtio_net *dev;
211         int i;
212
213         dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
214         if (dev == NULL) {
215                 RTE_LOG(ERR, VHOST_CONFIG,
216                         "Failed to allocate memory for new dev.\n");
217                 return -1;
218         }
219
220         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
221                 if (vhost_devices[i] == NULL)
222                         break;
223         }
224         if (i == MAX_VHOST_DEVICE) {
225                 RTE_LOG(ERR, VHOST_CONFIG,
226                         "Failed to find a free slot for new device.\n");
227                 rte_free(dev);
228                 return -1;
229         }
230
231         vhost_devices[i] = dev;
232         dev->vid = i;
233         dev->slave_req_fd = -1;
234
235         return i;
236 }
237
238 /*
239  * Invoked when there is the vhost-user connection is broken (when
240  * the virtio device is being detached).
241  */
242 void
243 vhost_destroy_device(int vid)
244 {
245         struct virtio_net *dev = get_device(vid);
246
247         if (dev == NULL)
248                 return;
249
250         if (dev->flags & VIRTIO_DEV_RUNNING) {
251                 dev->flags &= ~VIRTIO_DEV_RUNNING;
252                 dev->notify_ops->destroy_device(vid);
253         }
254
255         cleanup_device(dev, 1);
256         free_device(dev);
257
258         vhost_devices[vid] = NULL;
259 }
260
261 void
262 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
263 {
264         struct virtio_net *dev;
265         unsigned int len;
266
267         dev = get_device(vid);
268         if (dev == NULL)
269                 return;
270
271         len = if_len > sizeof(dev->ifname) ?
272                 sizeof(dev->ifname) : if_len;
273
274         strncpy(dev->ifname, if_name, len);
275         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
276 }
277
278 void
279 vhost_enable_dequeue_zero_copy(int vid)
280 {
281         struct virtio_net *dev = get_device(vid);
282
283         if (dev == NULL)
284                 return;
285
286         dev->dequeue_zero_copy = 1;
287 }
288
289 int
290 rte_vhost_get_mtu(int vid, uint16_t *mtu)
291 {
292         struct virtio_net *dev = get_device(vid);
293
294         if (!dev)
295                 return -ENODEV;
296
297         if (!(dev->flags & VIRTIO_DEV_READY))
298                 return -EAGAIN;
299
300         if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
301                 return -ENOTSUP;
302
303         *mtu = dev->mtu;
304
305         return 0;
306 }
307
308 int
309 rte_vhost_get_numa_node(int vid)
310 {
311 #ifdef RTE_LIBRTE_VHOST_NUMA
312         struct virtio_net *dev = get_device(vid);
313         int numa_node;
314         int ret;
315
316         if (dev == NULL)
317                 return -1;
318
319         ret = get_mempolicy(&numa_node, NULL, 0, dev,
320                             MPOL_F_NODE | MPOL_F_ADDR);
321         if (ret < 0) {
322                 RTE_LOG(ERR, VHOST_CONFIG,
323                         "(%d) failed to query numa node: %s\n",
324                         vid, rte_strerror(errno));
325                 return -1;
326         }
327
328         return numa_node;
329 #else
330         RTE_SET_USED(vid);
331         return -1;
332 #endif
333 }
334
335 uint32_t
336 rte_vhost_get_queue_num(int vid)
337 {
338         struct virtio_net *dev = get_device(vid);
339
340         if (dev == NULL)
341                 return 0;
342
343         return dev->nr_vring / 2;
344 }
345
346 uint16_t
347 rte_vhost_get_vring_num(int vid)
348 {
349         struct virtio_net *dev = get_device(vid);
350
351         if (dev == NULL)
352                 return 0;
353
354         return dev->nr_vring;
355 }
356
357 int
358 rte_vhost_get_ifname(int vid, char *buf, size_t len)
359 {
360         struct virtio_net *dev = get_device(vid);
361
362         if (dev == NULL)
363                 return -1;
364
365         len = RTE_MIN(len, sizeof(dev->ifname));
366
367         strncpy(buf, dev->ifname, len);
368         buf[len - 1] = '\0';
369
370         return 0;
371 }
372
373 int
374 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
375 {
376         struct virtio_net *dev;
377
378         dev = get_device(vid);
379         if (!dev)
380                 return -1;
381
382         *features = dev->features;
383         return 0;
384 }
385
386 int
387 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
388 {
389         struct virtio_net *dev;
390         struct rte_vhost_memory *m;
391         size_t size;
392
393         dev = get_device(vid);
394         if (!dev)
395                 return -1;
396
397         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
398         m = malloc(sizeof(struct rte_vhost_memory) + size);
399         if (!m)
400                 return -1;
401
402         m->nregions = dev->mem->nregions;
403         memcpy(m->regions, dev->mem->regions, size);
404         *mem = m;
405
406         return 0;
407 }
408
409 int
410 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
411                           struct rte_vhost_vring *vring)
412 {
413         struct virtio_net *dev;
414         struct vhost_virtqueue *vq;
415
416         dev = get_device(vid);
417         if (!dev)
418                 return -1;
419
420         if (vring_idx >= VHOST_MAX_VRING)
421                 return -1;
422
423         vq = dev->virtqueue[vring_idx];
424         if (!vq)
425                 return -1;
426
427         vring->desc  = vq->desc;
428         vring->avail = vq->avail;
429         vring->used  = vq->used;
430         vring->log_guest_addr  = vq->log_guest_addr;
431
432         vring->callfd  = vq->callfd;
433         vring->kickfd  = vq->kickfd;
434         vring->size    = vq->size;
435
436         return 0;
437 }
438
439 uint16_t
440 rte_vhost_avail_entries(int vid, uint16_t queue_id)
441 {
442         struct virtio_net *dev;
443         struct vhost_virtqueue *vq;
444
445         dev = get_device(vid);
446         if (!dev)
447                 return 0;
448
449         vq = dev->virtqueue[queue_id];
450         if (!vq->enabled)
451                 return 0;
452
453         return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
454 }
455
456 int
457 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
458 {
459         struct virtio_net *dev = get_device(vid);
460
461         if (dev == NULL)
462                 return -1;
463
464         if (enable) {
465                 RTE_LOG(ERR, VHOST_CONFIG,
466                         "guest notification isn't supported.\n");
467                 return -1;
468         }
469
470         dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
471         return 0;
472 }
473
474 void
475 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
476 {
477         struct virtio_net *dev = get_device(vid);
478
479         if (dev == NULL)
480                 return;
481
482         vhost_log_write(dev, addr, len);
483 }
484
485 void
486 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
487                          uint64_t offset, uint64_t len)
488 {
489         struct virtio_net *dev;
490         struct vhost_virtqueue *vq;
491
492         dev = get_device(vid);
493         if (dev == NULL)
494                 return;
495
496         if (vring_idx >= VHOST_MAX_VRING)
497                 return;
498         vq = dev->virtqueue[vring_idx];
499         if (!vq)
500                 return;
501
502         vhost_log_used_vring(dev, vq, offset, len);
503 }
504
505 uint32_t
506 rte_vhost_rx_queue_count(int vid, uint16_t qid)
507 {
508         struct virtio_net *dev;
509         struct vhost_virtqueue *vq;
510
511         dev = get_device(vid);
512         if (dev == NULL)
513                 return 0;
514
515         if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
516                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
517                         dev->vid, __func__, qid);
518                 return 0;
519         }
520
521         vq = dev->virtqueue[qid];
522         if (vq == NULL)
523                 return 0;
524
525         if (unlikely(vq->enabled == 0 || vq->avail == NULL))
526                 return 0;
527
528         return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
529 }