New upstream version 17.11.2
[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 req_size, size;
162
163         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
164                 goto out;
165
166         req_size = sizeof(struct vring_desc) * vq->size;
167         size = req_size;
168         vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
169                                                 vq->ring_addrs.desc_user_addr,
170                                                 &size, VHOST_ACCESS_RW);
171         if (!vq->desc || size != req_size)
172                 return -1;
173
174         req_size = sizeof(struct vring_avail);
175         req_size += sizeof(uint16_t) * vq->size;
176         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
177                 req_size += sizeof(uint16_t);
178         size = req_size;
179         vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
180                                                 vq->ring_addrs.avail_user_addr,
181                                                 &size, VHOST_ACCESS_RW);
182         if (!vq->avail || size != req_size)
183                 return -1;
184
185         req_size = sizeof(struct vring_used);
186         req_size += sizeof(struct vring_used_elem) * vq->size;
187         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
188                 req_size += sizeof(uint16_t);
189         size = req_size;
190         vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
191                                                 vq->ring_addrs.used_user_addr,
192                                                 &size, VHOST_ACCESS_RW);
193         if (!vq->used || size != req_size)
194                 return -1;
195
196 out:
197         vq->access_ok = 1;
198
199         return 0;
200 }
201
202 void
203 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
204 {
205         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
206                 vhost_user_iotlb_wr_lock(vq);
207
208         vq->access_ok = 0;
209         vq->desc = NULL;
210         vq->avail = NULL;
211         vq->used = NULL;
212
213         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
214                 vhost_user_iotlb_wr_unlock(vq);
215 }
216
217 static void
218 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
219 {
220         struct vhost_virtqueue *vq;
221
222         if (vring_idx >= VHOST_MAX_VRING) {
223                 RTE_LOG(ERR, VHOST_CONFIG,
224                                 "Failed not init vring, out of bound (%d)\n",
225                                 vring_idx);
226                 return;
227         }
228
229         vq = dev->virtqueue[vring_idx];
230
231         memset(vq, 0, sizeof(struct vhost_virtqueue));
232
233         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
234         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
235
236         vhost_user_iotlb_init(dev, vring_idx);
237         /* Backends are set to -1 indicating an inactive device. */
238         vq->backend = -1;
239
240         TAILQ_INIT(&vq->zmbuf_list);
241 }
242
243 static void
244 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
245 {
246         struct vhost_virtqueue *vq;
247         int callfd;
248
249         if (vring_idx >= VHOST_MAX_VRING) {
250                 RTE_LOG(ERR, VHOST_CONFIG,
251                                 "Failed not init vring, out of bound (%d)\n",
252                                 vring_idx);
253                 return;
254         }
255
256         vq = dev->virtqueue[vring_idx];
257         callfd = vq->callfd;
258         init_vring_queue(dev, vring_idx);
259         vq->callfd = callfd;
260 }
261
262 int
263 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
264 {
265         struct vhost_virtqueue *vq;
266
267         vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
268         if (vq == NULL) {
269                 RTE_LOG(ERR, VHOST_CONFIG,
270                         "Failed to allocate memory for vring:%u.\n", vring_idx);
271                 return -1;
272         }
273
274         dev->virtqueue[vring_idx] = vq;
275         init_vring_queue(dev, vring_idx);
276         rte_spinlock_init(&vq->access_lock);
277
278         dev->nr_vring += 1;
279
280         return 0;
281 }
282
283 /*
284  * Reset some variables in device structure, while keeping few
285  * others untouched, such as vid, ifname, nr_vring: they
286  * should be same unless the device is removed.
287  */
288 void
289 reset_device(struct virtio_net *dev)
290 {
291         uint32_t i;
292
293         dev->features = 0;
294         dev->protocol_features = 0;
295         dev->flags = 0;
296
297         for (i = 0; i < dev->nr_vring; i++)
298                 reset_vring_queue(dev, i);
299 }
300
301 /*
302  * Invoked when there is a new vhost-user connection established (when
303  * there is a new virtio device being attached).
304  */
305 int
306 vhost_new_device(void)
307 {
308         struct virtio_net *dev;
309         int i;
310
311         dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
312         if (dev == NULL) {
313                 RTE_LOG(ERR, VHOST_CONFIG,
314                         "Failed to allocate memory for new dev.\n");
315                 return -1;
316         }
317
318         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
319                 if (vhost_devices[i] == NULL)
320                         break;
321         }
322         if (i == MAX_VHOST_DEVICE) {
323                 RTE_LOG(ERR, VHOST_CONFIG,
324                         "Failed to find a free slot for new device.\n");
325                 rte_free(dev);
326                 return -1;
327         }
328
329         vhost_devices[i] = dev;
330         dev->vid = i;
331         dev->slave_req_fd = -1;
332
333         return i;
334 }
335
336 /*
337  * Invoked when there is the vhost-user connection is broken (when
338  * the virtio device is being detached).
339  */
340 void
341 vhost_destroy_device(int vid)
342 {
343         struct virtio_net *dev = get_device(vid);
344
345         if (dev == NULL)
346                 return;
347
348         if (dev->flags & VIRTIO_DEV_RUNNING) {
349                 dev->flags &= ~VIRTIO_DEV_RUNNING;
350                 dev->notify_ops->destroy_device(vid);
351         }
352
353         cleanup_device(dev, 1);
354         free_device(dev);
355
356         vhost_devices[vid] = NULL;
357 }
358
359 void
360 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
361 {
362         struct virtio_net *dev;
363         unsigned int len;
364
365         dev = get_device(vid);
366         if (dev == NULL)
367                 return;
368
369         len = if_len > sizeof(dev->ifname) ?
370                 sizeof(dev->ifname) : if_len;
371
372         strncpy(dev->ifname, if_name, len);
373         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
374 }
375
376 void
377 vhost_enable_dequeue_zero_copy(int vid)
378 {
379         struct virtio_net *dev = get_device(vid);
380
381         if (dev == NULL)
382                 return;
383
384         dev->dequeue_zero_copy = 1;
385 }
386
387 int
388 rte_vhost_get_mtu(int vid, uint16_t *mtu)
389 {
390         struct virtio_net *dev = get_device(vid);
391
392         if (!dev)
393                 return -ENODEV;
394
395         if (!(dev->flags & VIRTIO_DEV_READY))
396                 return -EAGAIN;
397
398         if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
399                 return -ENOTSUP;
400
401         *mtu = dev->mtu;
402
403         return 0;
404 }
405
406 int
407 rte_vhost_get_numa_node(int vid)
408 {
409 #ifdef RTE_LIBRTE_VHOST_NUMA
410         struct virtio_net *dev = get_device(vid);
411         int numa_node;
412         int ret;
413
414         if (dev == NULL)
415                 return -1;
416
417         ret = get_mempolicy(&numa_node, NULL, 0, dev,
418                             MPOL_F_NODE | MPOL_F_ADDR);
419         if (ret < 0) {
420                 RTE_LOG(ERR, VHOST_CONFIG,
421                         "(%d) failed to query numa node: %s\n",
422                         vid, rte_strerror(errno));
423                 return -1;
424         }
425
426         return numa_node;
427 #else
428         RTE_SET_USED(vid);
429         return -1;
430 #endif
431 }
432
433 uint32_t
434 rte_vhost_get_queue_num(int vid)
435 {
436         struct virtio_net *dev = get_device(vid);
437
438         if (dev == NULL)
439                 return 0;
440
441         return dev->nr_vring / 2;
442 }
443
444 uint16_t
445 rte_vhost_get_vring_num(int vid)
446 {
447         struct virtio_net *dev = get_device(vid);
448
449         if (dev == NULL)
450                 return 0;
451
452         return dev->nr_vring;
453 }
454
455 int
456 rte_vhost_get_ifname(int vid, char *buf, size_t len)
457 {
458         struct virtio_net *dev = get_device(vid);
459
460         if (dev == NULL)
461                 return -1;
462
463         len = RTE_MIN(len, sizeof(dev->ifname));
464
465         strncpy(buf, dev->ifname, len);
466         buf[len - 1] = '\0';
467
468         return 0;
469 }
470
471 int
472 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
473 {
474         struct virtio_net *dev;
475
476         dev = get_device(vid);
477         if (!dev)
478                 return -1;
479
480         *features = dev->features;
481         return 0;
482 }
483
484 int
485 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
486 {
487         struct virtio_net *dev;
488         struct rte_vhost_memory *m;
489         size_t size;
490
491         dev = get_device(vid);
492         if (!dev)
493                 return -1;
494
495         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
496         m = malloc(sizeof(struct rte_vhost_memory) + size);
497         if (!m)
498                 return -1;
499
500         m->nregions = dev->mem->nregions;
501         memcpy(m->regions, dev->mem->regions, size);
502         *mem = m;
503
504         return 0;
505 }
506
507 int
508 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
509                           struct rte_vhost_vring *vring)
510 {
511         struct virtio_net *dev;
512         struct vhost_virtqueue *vq;
513
514         dev = get_device(vid);
515         if (!dev)
516                 return -1;
517
518         if (vring_idx >= VHOST_MAX_VRING)
519                 return -1;
520
521         vq = dev->virtqueue[vring_idx];
522         if (!vq)
523                 return -1;
524
525         vring->desc  = vq->desc;
526         vring->avail = vq->avail;
527         vring->used  = vq->used;
528         vring->log_guest_addr  = vq->log_guest_addr;
529
530         vring->callfd  = vq->callfd;
531         vring->kickfd  = vq->kickfd;
532         vring->size    = vq->size;
533
534         return 0;
535 }
536
537 uint16_t
538 rte_vhost_avail_entries(int vid, uint16_t queue_id)
539 {
540         struct virtio_net *dev;
541         struct vhost_virtqueue *vq;
542
543         dev = get_device(vid);
544         if (!dev)
545                 return 0;
546
547         vq = dev->virtqueue[queue_id];
548         if (!vq->enabled)
549                 return 0;
550
551         return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
552 }
553
554 int
555 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
556 {
557         struct virtio_net *dev = get_device(vid);
558
559         if (dev == NULL)
560                 return -1;
561
562         if (enable) {
563                 RTE_LOG(ERR, VHOST_CONFIG,
564                         "guest notification isn't supported.\n");
565                 return -1;
566         }
567
568         dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
569         return 0;
570 }
571
572 void
573 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
574 {
575         struct virtio_net *dev = get_device(vid);
576
577         if (dev == NULL)
578                 return;
579
580         vhost_log_write(dev, addr, len);
581 }
582
583 void
584 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
585                          uint64_t offset, uint64_t len)
586 {
587         struct virtio_net *dev;
588         struct vhost_virtqueue *vq;
589
590         dev = get_device(vid);
591         if (dev == NULL)
592                 return;
593
594         if (vring_idx >= VHOST_MAX_VRING)
595                 return;
596         vq = dev->virtqueue[vring_idx];
597         if (!vq)
598                 return;
599
600         vhost_log_used_vring(dev, vq, offset, len);
601 }
602
603 uint32_t
604 rte_vhost_rx_queue_count(int vid, uint16_t qid)
605 {
606         struct virtio_net *dev;
607         struct vhost_virtqueue *vq;
608
609         dev = get_device(vid);
610         if (dev == NULL)
611                 return 0;
612
613         if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
614                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
615                         dev->vid, __func__, qid);
616                 return 0;
617         }
618
619         vq = dev->virtqueue[qid];
620         if (vq == NULL)
621                 return 0;
622
623         if (unlikely(vq->enabled == 0 || vq->avail == NULL))
624                 return 0;
625
626         return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
627 }