13ce864db3634de2a22590af92e4e05d4b3aa193
[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         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
272                 if (vhost_devices[i] == NULL)
273                         break;
274         }
275
276         if (i == MAX_VHOST_DEVICE) {
277                 RTE_LOG(ERR, VHOST_CONFIG,
278                         "Failed to find a free slot for new device.\n");
279                 return -1;
280         }
281
282         dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
283         if (dev == NULL) {
284                 RTE_LOG(ERR, VHOST_CONFIG,
285                         "Failed to allocate memory for new dev.\n");
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         rte_spinlock_init(&dev->slave_req_lock);
295
296         return i;
297 }
298
299 void
300 vhost_destroy_device_notify(struct virtio_net *dev)
301 {
302         struct rte_vdpa_device *vdpa_dev;
303         int did;
304
305         if (dev->flags & VIRTIO_DEV_RUNNING) {
306                 did = dev->vdpa_dev_id;
307                 vdpa_dev = rte_vdpa_get_device(did);
308                 if (vdpa_dev && vdpa_dev->ops->dev_close)
309                         vdpa_dev->ops->dev_close(dev->vid);
310                 dev->flags &= ~VIRTIO_DEV_RUNNING;
311                 dev->notify_ops->destroy_device(dev->vid);
312         }
313 }
314
315 /*
316  * Invoked when there is the vhost-user connection is broken (when
317  * the virtio device is being detached).
318  */
319 void
320 vhost_destroy_device(int vid)
321 {
322         struct virtio_net *dev = get_device(vid);
323
324         if (dev == NULL)
325                 return;
326
327         vhost_destroy_device_notify(dev);
328
329         cleanup_device(dev, 1);
330         free_device(dev);
331
332         vhost_devices[vid] = NULL;
333 }
334
335 void
336 vhost_attach_vdpa_device(int vid, int did)
337 {
338         struct virtio_net *dev = get_device(vid);
339
340         if (dev == NULL)
341                 return;
342
343         if (rte_vdpa_get_device(did) == NULL)
344                 return;
345
346         dev->vdpa_dev_id = did;
347 }
348
349 void
350 vhost_detach_vdpa_device(int vid)
351 {
352         struct virtio_net *dev = get_device(vid);
353
354         if (dev == NULL)
355                 return;
356
357         vhost_user_host_notifier_ctrl(vid, false);
358
359         dev->vdpa_dev_id = -1;
360 }
361
362 void
363 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
364 {
365         struct virtio_net *dev;
366         unsigned int len;
367
368         dev = get_device(vid);
369         if (dev == NULL)
370                 return;
371
372         len = if_len > sizeof(dev->ifname) ?
373                 sizeof(dev->ifname) : if_len;
374
375         strncpy(dev->ifname, if_name, len);
376         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
377 }
378
379 void
380 vhost_enable_dequeue_zero_copy(int vid)
381 {
382         struct virtio_net *dev = get_device(vid);
383
384         if (dev == NULL)
385                 return;
386
387         dev->dequeue_zero_copy = 1;
388 }
389
390 void
391 vhost_set_builtin_virtio_net(int vid, bool enable)
392 {
393         struct virtio_net *dev = get_device(vid);
394
395         if (dev == NULL)
396                 return;
397
398         if (enable)
399                 dev->flags |= VIRTIO_DEV_BUILTIN_VIRTIO_NET;
400         else
401                 dev->flags &= ~VIRTIO_DEV_BUILTIN_VIRTIO_NET;
402 }
403
404 int
405 rte_vhost_get_mtu(int vid, uint16_t *mtu)
406 {
407         struct virtio_net *dev = get_device(vid);
408
409         if (!dev)
410                 return -ENODEV;
411
412         if (!(dev->flags & VIRTIO_DEV_READY))
413                 return -EAGAIN;
414
415         if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
416                 return -ENOTSUP;
417
418         *mtu = dev->mtu;
419
420         return 0;
421 }
422
423 int
424 rte_vhost_get_numa_node(int vid)
425 {
426 #ifdef RTE_LIBRTE_VHOST_NUMA
427         struct virtio_net *dev = get_device(vid);
428         int numa_node;
429         int ret;
430
431         if (dev == NULL)
432                 return -1;
433
434         ret = get_mempolicy(&numa_node, NULL, 0, dev,
435                             MPOL_F_NODE | MPOL_F_ADDR);
436         if (ret < 0) {
437                 RTE_LOG(ERR, VHOST_CONFIG,
438                         "(%d) failed to query numa node: %s\n",
439                         vid, rte_strerror(errno));
440                 return -1;
441         }
442
443         return numa_node;
444 #else
445         RTE_SET_USED(vid);
446         return -1;
447 #endif
448 }
449
450 uint32_t
451 rte_vhost_get_queue_num(int vid)
452 {
453         struct virtio_net *dev = get_device(vid);
454
455         if (dev == NULL)
456                 return 0;
457
458         return dev->nr_vring / 2;
459 }
460
461 uint16_t
462 rte_vhost_get_vring_num(int vid)
463 {
464         struct virtio_net *dev = get_device(vid);
465
466         if (dev == NULL)
467                 return 0;
468
469         return dev->nr_vring;
470 }
471
472 int
473 rte_vhost_get_ifname(int vid, char *buf, size_t len)
474 {
475         struct virtio_net *dev = get_device(vid);
476
477         if (dev == NULL)
478                 return -1;
479
480         len = RTE_MIN(len, sizeof(dev->ifname));
481
482         strncpy(buf, dev->ifname, len);
483         buf[len - 1] = '\0';
484
485         return 0;
486 }
487
488 int
489 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
490 {
491         struct virtio_net *dev;
492
493         dev = get_device(vid);
494         if (!dev)
495                 return -1;
496
497         *features = dev->features;
498         return 0;
499 }
500
501 int
502 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
503 {
504         struct virtio_net *dev;
505         struct rte_vhost_memory *m;
506         size_t size;
507
508         dev = get_device(vid);
509         if (!dev)
510                 return -1;
511
512         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
513         m = malloc(sizeof(struct rte_vhost_memory) + size);
514         if (!m)
515                 return -1;
516
517         m->nregions = dev->mem->nregions;
518         memcpy(m->regions, dev->mem->regions, size);
519         *mem = m;
520
521         return 0;
522 }
523
524 int
525 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
526                           struct rte_vhost_vring *vring)
527 {
528         struct virtio_net *dev;
529         struct vhost_virtqueue *vq;
530
531         dev = get_device(vid);
532         if (!dev)
533                 return -1;
534
535         if (vring_idx >= VHOST_MAX_VRING)
536                 return -1;
537
538         vq = dev->virtqueue[vring_idx];
539         if (!vq)
540                 return -1;
541
542         vring->desc  = vq->desc;
543         vring->avail = vq->avail;
544         vring->used  = vq->used;
545         vring->log_guest_addr  = vq->log_guest_addr;
546
547         vring->callfd  = vq->callfd;
548         vring->kickfd  = vq->kickfd;
549         vring->size    = vq->size;
550
551         return 0;
552 }
553
554 int
555 rte_vhost_vring_call(int vid, uint16_t vring_idx)
556 {
557         struct virtio_net *dev;
558         struct vhost_virtqueue *vq;
559
560         dev = get_device(vid);
561         if (!dev)
562                 return -1;
563
564         if (vring_idx >= VHOST_MAX_VRING)
565                 return -1;
566
567         vq = dev->virtqueue[vring_idx];
568         if (!vq)
569                 return -1;
570
571         vhost_vring_call(dev, vq);
572         return 0;
573 }
574
575 uint16_t
576 rte_vhost_avail_entries(int vid, uint16_t queue_id)
577 {
578         struct virtio_net *dev;
579         struct vhost_virtqueue *vq;
580
581         dev = get_device(vid);
582         if (!dev)
583                 return 0;
584
585         vq = dev->virtqueue[queue_id];
586         if (!vq->enabled)
587                 return 0;
588
589         return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
590 }
591
592 int
593 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
594 {
595         struct virtio_net *dev = get_device(vid);
596
597         if (!dev)
598                 return -1;
599
600         if (enable)
601                 dev->virtqueue[queue_id]->used->flags &=
602                         ~VRING_USED_F_NO_NOTIFY;
603         else
604                 dev->virtqueue[queue_id]->used->flags |= VRING_USED_F_NO_NOTIFY;
605         return 0;
606 }
607
608 void
609 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
610 {
611         struct virtio_net *dev = get_device(vid);
612
613         if (dev == NULL)
614                 return;
615
616         vhost_log_write(dev, addr, len);
617 }
618
619 void
620 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
621                          uint64_t offset, uint64_t len)
622 {
623         struct virtio_net *dev;
624         struct vhost_virtqueue *vq;
625
626         dev = get_device(vid);
627         if (dev == NULL)
628                 return;
629
630         if (vring_idx >= VHOST_MAX_VRING)
631                 return;
632         vq = dev->virtqueue[vring_idx];
633         if (!vq)
634                 return;
635
636         vhost_log_used_vring(dev, vq, offset, len);
637 }
638
639 uint32_t
640 rte_vhost_rx_queue_count(int vid, uint16_t qid)
641 {
642         struct virtio_net *dev;
643         struct vhost_virtqueue *vq;
644
645         dev = get_device(vid);
646         if (dev == NULL)
647                 return 0;
648
649         if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
650                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
651                         dev->vid, __func__, qid);
652                 return 0;
653         }
654
655         vq = dev->virtqueue[qid];
656         if (vq == NULL)
657                 return 0;
658
659         if (unlikely(vq->enabled == 0 || vq->avail == NULL))
660                 return 0;
661
662         return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
663 }
664
665 int rte_vhost_get_vdpa_device_id(int vid)
666 {
667         struct virtio_net *dev = get_device(vid);
668
669         if (dev == NULL)
670                 return -1;
671
672         return dev->vdpa_dev_id;
673 }
674
675 int rte_vhost_get_log_base(int vid, uint64_t *log_base,
676                 uint64_t *log_size)
677 {
678         struct virtio_net *dev = get_device(vid);
679
680         if (!dev)
681                 return -1;
682
683         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
684                 RTE_LOG(ERR, VHOST_DATA,
685                         "(%d) %s: built-in vhost net backend is disabled.\n",
686                         dev->vid, __func__);
687                 return -1;
688         }
689
690         *log_base = dev->log_base;
691         *log_size = dev->log_size;
692
693         return 0;
694 }
695
696 int rte_vhost_get_vring_base(int vid, uint16_t queue_id,
697                 uint16_t *last_avail_idx, uint16_t *last_used_idx)
698 {
699         struct virtio_net *dev = get_device(vid);
700
701         if (!dev)
702                 return -1;
703
704         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
705                 RTE_LOG(ERR, VHOST_DATA,
706                         "(%d) %s: built-in vhost net backend is disabled.\n",
707                         dev->vid, __func__);
708                 return -1;
709         }
710
711         *last_avail_idx = dev->virtqueue[queue_id]->last_avail_idx;
712         *last_used_idx = dev->virtqueue[queue_id]->last_used_idx;
713
714         return 0;
715 }
716
717 int rte_vhost_set_vring_base(int vid, uint16_t queue_id,
718                 uint16_t last_avail_idx, uint16_t last_used_idx)
719 {
720         struct virtio_net *dev = get_device(vid);
721
722         if (!dev)
723                 return -1;
724
725         if (unlikely(!(dev->flags & VIRTIO_DEV_BUILTIN_VIRTIO_NET))) {
726                 RTE_LOG(ERR, VHOST_DATA,
727                         "(%d) %s: built-in vhost net backend is disabled.\n",
728                         dev->vid, __func__);
729                 return -1;
730         }
731
732         dev->virtqueue[queue_id]->last_avail_idx = last_avail_idx;
733         dev->virtqueue[queue_id]->last_used_idx = last_used_idx;
734
735         return 0;
736 }