examples: use new API to create control threads
[dpdk.git] / lib / librte_vhost / vhost.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2017 Intel Corporation
3  */
4
5 #include <linux/vhost.h>
6 #include <linux/virtio_net.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #ifdef RTE_LIBRTE_VHOST_NUMA
11 #include <numaif.h>
12 #endif
13
14 #include <rte_errno.h>
15 #include <rte_ethdev.h>
16 #include <rte_log.h>
17 #include <rte_string_fns.h>
18 #include <rte_memory.h>
19 #include <rte_malloc.h>
20 #include <rte_vhost.h>
21 #include <rte_rwlock.h>
22
23 #include "iotlb.h"
24 #include "vhost.h"
25 #include "vhost_user.h"
26
27 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
28
29 /* Called with iotlb_lock read-locked */
30 uint64_t
31 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
32                     uint64_t iova, uint64_t *size, uint8_t perm)
33 {
34         uint64_t vva, tmp_size;
35
36         if (unlikely(!*size))
37                 return 0;
38
39         tmp_size = *size;
40
41         vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
42         if (tmp_size == *size)
43                 return vva;
44
45         iova += tmp_size;
46
47         if (!vhost_user_iotlb_pending_miss(vq, iova, perm)) {
48                 /*
49                  * iotlb_lock is read-locked for a full burst,
50                  * but it only protects the iotlb cache.
51                  * In case of IOTLB miss, we might block on the socket,
52                  * which could cause a deadlock with QEMU if an IOTLB update
53                  * is being handled. We can safely unlock here to avoid it.
54                  */
55                 vhost_user_iotlb_rd_unlock(vq);
56
57                 vhost_user_iotlb_pending_insert(vq, iova, perm);
58                 if (vhost_user_iotlb_miss(dev, iova, perm)) {
59                         RTE_LOG(ERR, VHOST_CONFIG,
60                                 "IOTLB miss req failed for IOVA 0x%" PRIx64 "\n",
61                                 iova);
62                         vhost_user_iotlb_pending_remove(vq, iova, 1, perm);
63                 }
64
65                 vhost_user_iotlb_rd_lock(vq);
66         }
67
68         return 0;
69 }
70
71 void
72 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
73 {
74         if ((vq->callfd >= 0) && (destroy != 0))
75                 close(vq->callfd);
76         if (vq->kickfd >= 0)
77                 close(vq->kickfd);
78 }
79
80 /*
81  * Unmap any memory, close any file descriptors and
82  * free any memory owned by a device.
83  */
84 void
85 cleanup_device(struct virtio_net *dev, int destroy)
86 {
87         uint32_t i;
88
89         vhost_backend_cleanup(dev);
90
91         for (i = 0; i < dev->nr_vring; i++)
92                 cleanup_vq(dev->virtqueue[i], destroy);
93 }
94
95 void
96 free_vq(struct vhost_virtqueue *vq)
97 {
98         rte_free(vq->shadow_used_ring);
99         rte_free(vq->batch_copy_elems);
100         rte_mempool_free(vq->iotlb_pool);
101         rte_free(vq);
102 }
103
104 /*
105  * Release virtqueues and device memory.
106  */
107 static void
108 free_device(struct virtio_net *dev)
109 {
110         uint32_t i;
111
112         for (i = 0; i < dev->nr_vring; i++)
113                 free_vq(dev->virtqueue[i]);
114
115         rte_free(dev);
116 }
117
118 int
119 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
120 {
121         uint64_t req_size, size;
122
123         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
124                 goto out;
125
126         req_size = sizeof(struct vring_desc) * vq->size;
127         size = req_size;
128         vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
129                                                 vq->ring_addrs.desc_user_addr,
130                                                 &size, VHOST_ACCESS_RW);
131         if (!vq->desc || size != req_size)
132                 return -1;
133
134         req_size = sizeof(struct vring_avail);
135         req_size += sizeof(uint16_t) * vq->size;
136         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
137                 req_size += sizeof(uint16_t);
138         size = req_size;
139         vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
140                                                 vq->ring_addrs.avail_user_addr,
141                                                 &size, VHOST_ACCESS_RW);
142         if (!vq->avail || size != req_size)
143                 return -1;
144
145         req_size = sizeof(struct vring_used);
146         req_size += sizeof(struct vring_used_elem) * vq->size;
147         if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
148                 req_size += sizeof(uint16_t);
149         size = req_size;
150         vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
151                                                 vq->ring_addrs.used_user_addr,
152                                                 &size, VHOST_ACCESS_RW);
153         if (!vq->used || size != req_size)
154                 return -1;
155
156 out:
157         vq->access_ok = 1;
158
159         return 0;
160 }
161
162 void
163 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
164 {
165         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
166                 vhost_user_iotlb_wr_lock(vq);
167
168         vq->access_ok = 0;
169         vq->desc = NULL;
170         vq->avail = NULL;
171         vq->used = NULL;
172
173         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
174                 vhost_user_iotlb_wr_unlock(vq);
175 }
176
177 static void
178 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
179 {
180         struct vhost_virtqueue *vq;
181
182         if (vring_idx >= VHOST_MAX_VRING) {
183                 RTE_LOG(ERR, VHOST_CONFIG,
184                                 "Failed not init vring, out of bound (%d)\n",
185                                 vring_idx);
186                 return;
187         }
188
189         vq = dev->virtqueue[vring_idx];
190
191         memset(vq, 0, sizeof(struct vhost_virtqueue));
192
193         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
194         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
195
196         vhost_user_iotlb_init(dev, vring_idx);
197         /* Backends are set to -1 indicating an inactive device. */
198         vq->backend = -1;
199
200         TAILQ_INIT(&vq->zmbuf_list);
201 }
202
203 static void
204 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
205 {
206         struct vhost_virtqueue *vq;
207         int callfd;
208
209         if (vring_idx >= VHOST_MAX_VRING) {
210                 RTE_LOG(ERR, VHOST_CONFIG,
211                                 "Failed not init vring, out of bound (%d)\n",
212                                 vring_idx);
213                 return;
214         }
215
216         vq = dev->virtqueue[vring_idx];
217         callfd = vq->callfd;
218         init_vring_queue(dev, vring_idx);
219         vq->callfd = callfd;
220 }
221
222 int
223 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
224 {
225         struct vhost_virtqueue *vq;
226
227         vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
228         if (vq == NULL) {
229                 RTE_LOG(ERR, VHOST_CONFIG,
230                         "Failed to allocate memory for vring:%u.\n", vring_idx);
231                 return -1;
232         }
233
234         dev->virtqueue[vring_idx] = vq;
235         init_vring_queue(dev, vring_idx);
236         rte_spinlock_init(&vq->access_lock);
237
238         dev->nr_vring += 1;
239
240         return 0;
241 }
242
243 /*
244  * Reset some variables in device structure, while keeping few
245  * others untouched, such as vid, ifname, nr_vring: they
246  * should be same unless the device is removed.
247  */
248 void
249 reset_device(struct virtio_net *dev)
250 {
251         uint32_t i;
252
253         dev->features = 0;
254         dev->protocol_features = 0;
255         dev->flags &= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
256
257         for (i = 0; i < dev->nr_vring; i++)
258                 reset_vring_queue(dev, i);
259 }
260
261 /*
262  * Invoked when there is a new vhost-user connection established (when
263  * there is a new virtio device being attached).
264  */
265 int
266 vhost_new_device(void)
267 {
268         struct virtio_net *dev;
269         int i;
270
271         dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
272         if (dev == NULL) {
273                 RTE_LOG(ERR, VHOST_CONFIG,
274                         "Failed to allocate memory for new dev.\n");
275                 return -1;
276         }
277
278         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
279                 if (vhost_devices[i] == NULL)
280                         break;
281         }
282         if (i == MAX_VHOST_DEVICE) {
283                 RTE_LOG(ERR, VHOST_CONFIG,
284                         "Failed to find a free slot for new device.\n");
285                 rte_free(dev);
286                 return -1;
287         }
288
289         vhost_devices[i] = dev;
290         dev->vid = i;
291         dev->flags = VIRTIO_DEV_BUILTIN_VIRTIO_NET;
292         dev->slave_req_fd = -1;
293         dev->vdpa_dev_id = -1;
294
295         return i;
296 }
297
298 /*
299  * Invoked when there is the vhost-user connection is broken (when
300  * the virtio device is being detached).
301  */
302 void
303 vhost_destroy_device(int vid)
304 {
305         struct virtio_net *dev = get_device(vid);
306         struct rte_vdpa_device *vdpa_dev;
307         int did = -1;
308
309         if (dev == NULL)
310                 return;
311
312         if (dev->flags & VIRTIO_DEV_RUNNING) {
313                 did = dev->vdpa_dev_id;
314                 vdpa_dev = rte_vdpa_get_device(did);
315                 if (vdpa_dev && vdpa_dev->ops->dev_close)
316                         vdpa_dev->ops->dev_close(dev->vid);
317                 dev->flags &= ~VIRTIO_DEV_RUNNING;
318                 dev->notify_ops->destroy_device(vid);
319         }
320
321         cleanup_device(dev, 1);
322         free_device(dev);
323
324         vhost_devices[vid] = NULL;
325 }
326
327 void
328 vhost_attach_vdpa_device(int vid, int did)
329 {
330         struct virtio_net *dev = get_device(vid);
331
332         if (dev == NULL)
333                 return;
334
335         if (rte_vdpa_get_device(did) == NULL)
336                 return;
337
338         dev->vdpa_dev_id = did;
339 }
340
341 void
342 vhost_detach_vdpa_device(int vid)
343 {
344         struct virtio_net *dev = get_device(vid);
345
346         if (dev == NULL)
347                 return;
348
349         dev->vdpa_dev_id = -1;
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 void
381 vhost_set_builtin_virtio_net(int vid, bool enable)
382 {
383         struct virtio_net *dev = get_device(vid);
384
385         if (dev == NULL)
386                 return;
387
388         if (enable)
389                 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
390         else
391                 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
392 }
393
394 int
395 rte_vhost_get_mtu(int vid, uint16_t *mtu)
396 {
397         struct virtio_net *dev = get_device(vid);
398
399         if (!dev)
400                 return -ENODEV;
401
402         if (!(dev->flags & VIRTIO_DEV_READY))
403                 return -EAGAIN;
404
405         if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
406                 return -ENOTSUP;
407
408         *mtu = dev->mtu;
409
410         return 0;
411 }
412
413 int
414 rte_vhost_get_numa_node(int vid)
415 {
416 #ifdef RTE_LIBRTE_VHOST_NUMA
417         struct virtio_net *dev = get_device(vid);
418         int numa_node;
419         int ret;
420
421         if (dev == NULL)
422                 return -1;
423
424         ret = get_mempolicy(&numa_node, NULL, 0, dev,
425                             MPOL_F_NODE | MPOL_F_ADDR);
426         if (ret < 0) {
427                 RTE_LOG(ERR, VHOST_CONFIG,
428                         "(%d) failed to query numa node: %s\n",
429                         vid, rte_strerror(errno));
430                 return -1;
431         }
432
433         return numa_node;
434 #else
435         RTE_SET_USED(vid);
436         return -1;
437 #endif
438 }
439
440 uint32_t
441 rte_vhost_get_queue_num(int vid)
442 {
443         struct virtio_net *dev = get_device(vid);
444
445         if (dev == NULL)
446                 return 0;
447
448         return dev->nr_vring / 2;
449 }
450
451 uint16_t
452 rte_vhost_get_vring_num(int vid)
453 {
454         struct virtio_net *dev = get_device(vid);
455
456         if (dev == NULL)
457                 return 0;
458
459         return dev->nr_vring;
460 }
461
462 int
463 rte_vhost_get_ifname(int vid, char *buf, size_t len)
464 {
465         struct virtio_net *dev = get_device(vid);
466
467         if (dev == NULL)
468                 return -1;
469
470         len = RTE_MIN(len, sizeof(dev->ifname));
471
472         strncpy(buf, dev->ifname, len);
473         buf[len - 1] = '\0';
474
475         return 0;
476 }
477
478 int
479 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
480 {
481         struct virtio_net *dev;
482
483         dev = get_device(vid);
484         if (!dev)
485                 return -1;
486
487         *features = dev->features;
488         return 0;
489 }
490
491 int
492 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
493 {
494         struct virtio_net *dev;
495         struct rte_vhost_memory *m;
496         size_t size;
497
498         dev = get_device(vid);
499         if (!dev)
500                 return -1;
501
502         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
503         m = malloc(sizeof(struct rte_vhost_memory) + size);
504         if (!m)
505                 return -1;
506
507         m->nregions = dev->mem->nregions;
508         memcpy(m->regions, dev->mem->regions, size);
509         *mem = m;
510
511         return 0;
512 }
513
514 int
515 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
516                           struct rte_vhost_vring *vring)
517 {
518         struct virtio_net *dev;
519         struct vhost_virtqueue *vq;
520
521         dev = get_device(vid);
522         if (!dev)
523                 return -1;
524
525         if (vring_idx >= VHOST_MAX_VRING)
526                 return -1;
527
528         vq = dev->virtqueue[vring_idx];
529         if (!vq)
530                 return -1;
531
532         vring->desc  = vq->desc;
533         vring->avail = vq->avail;
534         vring->used  = vq->used;
535         vring->log_guest_addr  = vq->log_guest_addr;
536
537         vring->callfd  = vq->callfd;
538         vring->kickfd  = vq->kickfd;
539         vring->size    = vq->size;
540
541         return 0;
542 }
543
544 int
545 rte_vhost_vring_call(int vid, uint16_t vring_idx)
546 {
547         struct virtio_net *dev;
548         struct vhost_virtqueue *vq;
549
550         dev = get_device(vid);
551         if (!dev)
552                 return -1;
553
554         if (vring_idx >= VHOST_MAX_VRING)
555                 return -1;
556
557         vq = dev->virtqueue[vring_idx];
558         if (!vq)
559                 return -1;
560
561         vhost_vring_call(dev, vq);
562         return 0;
563 }
564
565 uint16_t
566 rte_vhost_avail_entries(int vid, uint16_t queue_id)
567 {
568         struct virtio_net *dev;
569         struct vhost_virtqueue *vq;
570
571         dev = get_device(vid);
572         if (!dev)
573                 return 0;
574
575         vq = dev->virtqueue[queue_id];
576         if (!vq->enabled)
577                 return 0;
578
579         return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
580 }
581
582 int
583 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
584 {
585         struct virtio_net *dev = get_device(vid);
586
587         if (!dev)
588                 return -1;
589
590         if (enable)
591                 dev->virtqueue[queue_id]->used->flags &=
592                         ~VRING_USED_F_NO_NOTIFY;
593         else
594                 dev->virtqueue[queue_id]->used->flags |= VRING_USED_F_NO_NOTIFY;
595         return 0;
596 }
597
598 void
599 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
600 {
601         struct virtio_net *dev = get_device(vid);
602
603         if (dev == NULL)
604                 return;
605
606         vhost_log_write(dev, addr, len);
607 }
608
609 void
610 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
611                          uint64_t offset, uint64_t len)
612 {
613         struct virtio_net *dev;
614         struct vhost_virtqueue *vq;
615
616         dev = get_device(vid);
617         if (dev == NULL)
618                 return;
619
620         if (vring_idx >= VHOST_MAX_VRING)
621                 return;
622         vq = dev->virtqueue[vring_idx];
623         if (!vq)
624                 return;
625
626         vhost_log_used_vring(dev, vq, offset, len);
627 }
628
629 uint32_t
630 rte_vhost_rx_queue_count(int vid, uint16_t qid)
631 {
632         struct virtio_net *dev;
633         struct vhost_virtqueue *vq;
634
635         dev = get_device(vid);
636         if (dev == NULL)
637                 return 0;
638
639         if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
640                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
641                         dev->vid, __func__, qid);
642                 return 0;
643         }
644
645         vq = dev->virtqueue[qid];
646         if (vq == NULL)
647                 return 0;
648
649         if (unlikely(vq->enabled == 0 || vq->avail == NULL))
650                 return 0;
651
652         return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
653 }
654
655 int rte_vhost_get_vdpa_device_id(int vid)
656 {
657         struct virtio_net *dev = get_device(vid);
658
659         if (dev == NULL)
660                 return -1;
661
662         return dev->vdpa_dev_id;
663 }
664
665 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
666                 uint64_t *log_size)
667 {
668         struct virtio_net *dev = get_device(vid);
669
670         if (!dev)
671                 return -1;
672
673         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
674                 RTE_LOG(ERR, VHOST_DATA,
675                         "(%d) %s: built-in vhost net backend is disabled.\n",
676                         dev->vid, __func__);
677                 return -1;
678         }
679
680         *log_base = dev->log_base;
681         *log_size = dev->log_size;
682
683         return 0;
684 }
685
686 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
687                 uint16_t *last_avail_idx, uint16_t *last_used_idx)
688 {
689         struct virtio_net *dev = get_device(vid);
690
691         if (!dev)
692                 return -1;
693
694         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
695                 RTE_LOG(ERR, VHOST_DATA,
696                         "(%d) %s: built-in vhost net backend is disabled.\n",
697                         dev->vid, __func__);
698                 return -1;
699         }
700
701         *last_avail_idx = dev->virtqueue[queue_id]->last_avail_idx;
702         *last_used_idx = dev->virtqueue[queue_id]->last_used_idx;
703
704         return 0;
705 }
706
707 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
708                 uint16_t last_avail_idx, uint16_t last_used_idx)
709 {
710         struct virtio_net *dev = get_device(vid);
711
712         if (!dev)
713                 return -1;
714
715         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
716                 RTE_LOG(ERR, VHOST_DATA,
717                         "(%d) %s: built-in vhost net backend is disabled.\n",
718                         dev->vid, __func__);
719                 return -1;
720         }
721
722         dev->virtqueue[queue_id]->last_avail_idx = last_avail_idx;
723         dev->virtqueue[queue_id]->last_used_idx = last_used_idx;
724
725         return 0;
726 }