New upstream version 17.11.1
[deb_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 #include <rte_rwlock.h>
51
52 #include "iotlb.h"
53 #include "vhost.h"
54 #include "vhost_user.h"
55
56 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
57
58 /* Called with iotlb_lock read-locked */
59 uint64_t
60 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
61                     uint64_t iova, uint64_t size, uint8_t perm)
62 {
63         uint64_t vva, tmp_size;
64
65         if (unlikely(!size))
66                 return 0;
67
68         tmp_size = size;
69
70         vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
71         if (tmp_size == size)
72                 return vva;
73
74         iova += tmp_size;
75
76         if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
77                 /*
78                  * iotlb_lock is read-locked for a full burst,
79                  * but it only protects the iotlb cache.
80                  * In case of IOTLB miss, we might block on the socket,
81                  * which could cause a deadlock with QEMU if an IOTLB update
82                  * is being handled. We can safely unlock here to avoid it.
83                  */
84                 vhost_user_iotlb_rd_unlock(vq);
85
86                 vhost_user_iotlb_pending_insert(vq, iova, perm);
87                 if (vhost_user_iotlb_miss(dev, iova, perm)) {
88                         RTE_LOG(ERR, VHOST_CONFIG,
89                                 "IOTLB miss req failed for IOVA 0x%" PRIx64 "\n",
90                                 iova);
91                         vhost_user_iotlb_pending_remove(vq, iova, 1, perm);
92                 }
93
94                 vhost_user_iotlb_rd_lock(vq);
95         }
96
97         return 0;
98 }
99
100 struct virtio_net *
101 get_device(int vid)
102 {
103         struct virtio_net *dev = vhost_devices[vid];
104
105         if (unlikely(!dev)) {
106                 RTE_LOG(ERR, VHOST_CONFIG,
107                         "(%d) device not found.\n", vid);
108         }
109
110         return dev;
111 }
112
113 static void
114 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
115 {
116         if ((vq->callfd >= 0) && (destroy != 0))
117                 close(vq->callfd);
118         if (vq->kickfd >= 0)
119                 close(vq->kickfd);
120 }
121
122 /*
123  * Unmap any memory, close any file descriptors and
124  * free any memory owned by a device.
125  */
126 void
127 cleanup_device(struct virtio_net *dev, int destroy)
128 {
129         uint32_t i;
130
131         vhost_backend_cleanup(dev);
132
133         for (i = 0; i < dev->nr_vring; i++)
134                 cleanup_vq(dev->virtqueue[i], destroy);
135 }
136
137 /*
138  * Release virtqueues and device memory.
139  */
140 static void
141 free_device(struct virtio_net *dev)
142 {
143         uint32_t i;
144         struct vhost_virtqueue *vq;
145
146         for (i = 0; i < dev->nr_vring; i++) {
147                 vq = dev->virtqueue[i];
148
149                 rte_free(vq->shadow_used_ring);
150                 rte_free(vq->batch_copy_elems);
151                 rte_mempool_free(vq->iotlb_pool);
152                 rte_free(vq);
153         }
154
155         rte_free(dev);
156 }
157
158 int
159 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
160 {
161         uint64_t size;
162
163         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
164                 goto out;
165
166         size = sizeof(struct vring_desc) * vq->size;
167         vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
168                                                 vq->ring_addrs.desc_user_addr,
169                                                 size, VHOST_ACCESS_RW);
170         if (!vq->desc)
171                 return -1;
172
173         size = sizeof(struct vring_avail);
174         size += sizeof(uint16_t) * vq->size;
175         vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
176                                                 vq->ring_addrs.avail_user_addr,
177                                                 size, VHOST_ACCESS_RW);
178         if (!vq->avail)
179                 return -1;
180
181         size = sizeof(struct vring_used);
182         size += sizeof(struct vring_used_elem) * vq->size;
183         vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
184                                                 vq->ring_addrs.used_user_addr,
185                                                 size, VHOST_ACCESS_RW);
186         if (!vq->used)
187                 return -1;
188
189 out:
190         vq->access_ok = 1;
191
192         return 0;
193 }
194
195 void
196 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
197 {
198         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
199                 vhost_user_iotlb_wr_lock(vq);
200
201         vq->access_ok = 0;
202         vq->desc = NULL;
203         vq->avail = NULL;
204         vq->used = NULL;
205
206         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
207                 vhost_user_iotlb_wr_unlock(vq);
208 }
209
210 static void
211 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
212 {
213         struct vhost_virtqueue *vq;
214
215         if (vring_idx >= VHOST_MAX_VRING) {
216                 RTE_LOG(ERR, VHOST_CONFIG,
217                                 "Failed not init vring, out of bound (%d)\n",
218                                 vring_idx);
219                 return;
220         }
221
222         vq = dev->virtqueue[vring_idx];
223
224         memset(vq, 0, sizeof(struct vhost_virtqueue));
225
226         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
227         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
228
229         vhost_user_iotlb_init(dev, vring_idx);
230         /* Backends are set to -1 indicating an inactive device. */
231         vq->backend = -1;
232
233         TAILQ_INIT(&vq->zmbuf_list);
234 }
235
236 static void
237 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
238 {
239         struct vhost_virtqueue *vq;
240         int callfd;
241
242         if (vring_idx >= VHOST_MAX_VRING) {
243                 RTE_LOG(ERR, VHOST_CONFIG,
244                                 "Failed not init vring, out of bound (%d)\n",
245                                 vring_idx);
246                 return;
247         }
248
249         vq = dev->virtqueue[vring_idx];
250         callfd = vq->callfd;
251         init_vring_queue(dev, vring_idx);
252         vq->callfd = callfd;
253 }
254
255 int
256 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
257 {
258         struct vhost_virtqueue *vq;
259
260         vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
261         if (vq == NULL) {
262                 RTE_LOG(ERR, VHOST_CONFIG,
263                         "Failed to allocate memory for vring:%u.\n", vring_idx);
264                 return -1;
265         }
266
267         dev->virtqueue[vring_idx] = vq;
268         init_vring_queue(dev, vring_idx);
269         rte_spinlock_init(&vq->access_lock);
270
271         dev->nr_vring += 1;
272
273         return 0;
274 }
275
276 /*
277  * Reset some variables in device structure, while keeping few
278  * others untouched, such as vid, ifname, nr_vring: they
279  * should be same unless the device is removed.
280  */
281 void
282 reset_device(struct virtio_net *dev)
283 {
284         uint32_t i;
285
286         dev->features = 0;
287         dev->protocol_features = 0;
288         dev->flags = 0;
289
290         for (i = 0; i < dev->nr_vring; i++)
291                 reset_vring_queue(dev, i);
292 }
293
294 /*
295  * Invoked when there is a new vhost-user connection established (when
296  * there is a new virtio device being attached).
297  */
298 int
299 vhost_new_device(void)
300 {
301         struct virtio_net *dev;
302         int i;
303
304         dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
305         if (dev == NULL) {
306                 RTE_LOG(ERR, VHOST_CONFIG,
307                         "Failed to allocate memory for new dev.\n");
308                 return -1;
309         }
310
311         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
312                 if (vhost_devices[i] == NULL)
313                         break;
314         }
315         if (i == MAX_VHOST_DEVICE) {
316                 RTE_LOG(ERR, VHOST_CONFIG,
317                         "Failed to find a free slot for new device.\n");
318                 rte_free(dev);
319                 return -1;
320         }
321
322         vhost_devices[i] = dev;
323         dev->vid = i;
324         dev->slave_req_fd = -1;
325
326         return i;
327 }
328
329 /*
330  * Invoked when there is the vhost-user connection is broken (when
331  * the virtio device is being detached).
332  */
333 void
334 vhost_destroy_device(int vid)
335 {
336         struct virtio_net *dev = get_device(vid);
337
338         if (dev == NULL)
339                 return;
340
341         if (dev->flags & VIRTIO_DEV_RUNNING) {
342                 dev->flags &= ~VIRTIO_DEV_RUNNING;
343                 dev->notify_ops->destroy_device(vid);
344         }
345
346         cleanup_device(dev, 1);
347         free_device(dev);
348
349         vhost_devices[vid] = NULL;
350 }
351
352 void
353 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
354 {
355         struct virtio_net *dev;
356         unsigned int len;
357
358         dev = get_device(vid);
359         if (dev == NULL)
360                 return;
361
362         len = if_len > sizeof(dev->ifname) ?
363                 sizeof(dev->ifname) : if_len;
364
365         strncpy(dev->ifname, if_name, len);
366         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
367 }
368
369 void
370 vhost_enable_dequeue_zero_copy(int vid)
371 {
372         struct virtio_net *dev = get_device(vid);
373
374         if (dev == NULL)
375                 return;
376
377         dev->dequeue_zero_copy = 1;
378 }
379
380 int
381 rte_vhost_get_mtu(int vid, uint16_t *mtu)
382 {
383         struct virtio_net *dev = get_device(vid);
384
385         if (!dev)
386                 return -ENODEV;
387
388         if (!(dev->flags & VIRTIO_DEV_READY))
389                 return -EAGAIN;
390
391         if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
392                 return -ENOTSUP;
393
394         *mtu = dev->mtu;
395
396         return 0;
397 }
398
399 int
400 rte_vhost_get_numa_node(int vid)
401 {
402 #ifdef RTE_LIBRTE_VHOST_NUMA
403         struct virtio_net *dev = get_device(vid);
404         int numa_node;
405         int ret;
406
407         if (dev == NULL)
408                 return -1;
409
410         ret = get_mempolicy(&numa_node, NULL, 0, dev,
411                             MPOL_F_NODE | MPOL_F_ADDR);
412         if (ret < 0) {
413                 RTE_LOG(ERR, VHOST_CONFIG,
414                         "(%d) failed to query numa node: %s\n",
415                         vid, rte_strerror(errno));
416                 return -1;
417         }
418
419         return numa_node;
420 #else
421         RTE_SET_USED(vid);
422         return -1;
423 #endif
424 }
425
426 uint32_t
427 rte_vhost_get_queue_num(int vid)
428 {
429         struct virtio_net *dev = get_device(vid);
430
431         if (dev == NULL)
432                 return 0;
433
434         return dev->nr_vring / 2;
435 }
436
437 uint16_t
438 rte_vhost_get_vring_num(int vid)
439 {
440         struct virtio_net *dev = get_device(vid);
441
442         if (dev == NULL)
443                 return 0;
444
445         return dev->nr_vring;
446 }
447
448 int
449 rte_vhost_get_ifname(int vid, char *buf, size_t len)
450 {
451         struct virtio_net *dev = get_device(vid);
452
453         if (dev == NULL)
454                 return -1;
455
456         len = RTE_MIN(len, sizeof(dev->ifname));
457
458         strncpy(buf, dev->ifname, len);
459         buf[len - 1] = '\0';
460
461         return 0;
462 }
463
464 int
465 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
466 {
467         struct virtio_net *dev;
468
469         dev = get_device(vid);
470         if (!dev)
471                 return -1;
472
473         *features = dev->features;
474         return 0;
475 }
476
477 int
478 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
479 {
480         struct virtio_net *dev;
481         struct rte_vhost_memory *m;
482         size_t size;
483
484         dev = get_device(vid);
485         if (!dev)
486                 return -1;
487
488         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
489         m = malloc(sizeof(struct rte_vhost_memory) + size);
490         if (!m)
491                 return -1;
492
493         m->nregions = dev->mem->nregions;
494         memcpy(m->regions, dev->mem->regions, size);
495         *mem = m;
496
497         return 0;
498 }
499
500 int
501 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
502                           struct rte_vhost_vring *vring)
503 {
504         struct virtio_net *dev;
505         struct vhost_virtqueue *vq;
506
507         dev = get_device(vid);
508         if (!dev)
509                 return -1;
510
511         if (vring_idx >= VHOST_MAX_VRING)
512                 return -1;
513
514         vq = dev->virtqueue[vring_idx];
515         if (!vq)
516                 return -1;
517
518         vring->desc  = vq->desc;
519         vring->avail = vq->avail;
520         vring->used  = vq->used;
521         vring->log_guest_addr  = vq->log_guest_addr;
522
523         vring->callfd  = vq->callfd;
524         vring->kickfd  = vq->kickfd;
525         vring->size    = vq->size;
526
527         return 0;
528 }
529
530 uint16_t
531 rte_vhost_avail_entries(int vid, uint16_t queue_id)
532 {
533         struct virtio_net *dev;
534         struct vhost_virtqueue *vq;
535
536         dev = get_device(vid);
537         if (!dev)
538                 return 0;
539
540         vq = dev->virtqueue[queue_id];
541         if (!vq->enabled)
542                 return 0;
543
544         return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
545 }
546
547 int
548 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
549 {
550         struct virtio_net *dev = get_device(vid);
551
552         if (dev == NULL)
553                 return -1;
554
555         if (enable) {
556                 RTE_LOG(ERR, VHOST_CONFIG,
557                         "guest notification isn't supported.\n");
558                 return -1;
559         }
560
561         dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
562         return 0;
563 }
564
565 void
566 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
567 {
568         struct virtio_net *dev = get_device(vid);
569
570         if (dev == NULL)
571                 return;
572
573         vhost_log_write(dev, addr, len);
574 }
575
576 void
577 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
578                          uint64_t offset, uint64_t len)
579 {
580         struct virtio_net *dev;
581         struct vhost_virtqueue *vq;
582
583         dev = get_device(vid);
584         if (dev == NULL)
585                 return;
586
587         if (vring_idx >= VHOST_MAX_VRING)
588                 return;
589         vq = dev->virtqueue[vring_idx];
590         if (!vq)
591                 return;
592
593         vhost_log_used_vring(dev, vq, offset, len);
594 }
595
596 uint32_t
597 rte_vhost_rx_queue_count(int vid, uint16_t qid)
598 {
599         struct virtio_net *dev;
600         struct vhost_virtqueue *vq;
601
602         dev = get_device(vid);
603         if (dev == NULL)
604                 return 0;
605
606         if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
607                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
608                         dev->vid, __func__, qid);
609                 return 0;
610         }
611
612         vq = dev->virtqueue[qid];
613         if (vq == NULL)
614                 return 0;
615
616         if (unlikely(vq->enabled == 0 || vq->avail == NULL))
617                 return 0;
618
619         return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
620 }