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