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