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