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