vhost: invalidate vring in case of matching IOTLB invalidate
[dpdk.git] / lib / librte_vhost / vhost.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <linux/vhost.h>
35 #include <linux/virtio_net.h>
36 #include <stddef.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 #ifdef RTE_LIBRTE_VHOST_NUMA
40 #include <numaif.h>
41 #endif
42
43 #include <rte_errno.h>
44 #include <rte_ethdev.h>
45 #include <rte_log.h>
46 #include <rte_string_fns.h>
47 #include <rte_memory.h>
48 #include <rte_malloc.h>
49 #include <rte_vhost.h>
50 #include <rte_rwlock.h>
51
52 #include "iotlb.h"
53 #include "vhost.h"
54 #include "vhost_user.h"
55
56 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
57
58 uint64_t
59 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
60                     uint64_t iova, uint64_t size, uint8_t perm)
61 {
62         uint64_t vva, tmp_size;
63
64         if (unlikely(!size))
65                 return 0;
66
67         tmp_size = size;
68
69         vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
70         if (tmp_size == size)
71                 return vva;
72
73         if (!vhost_user_iotlb_pending_miss(vq, iova + tmp_size, perm)) {
74                 vhost_user_iotlb_pending_insert(vq, iova + tmp_size, perm);
75                 vhost_user_iotlb_miss(dev, iova + tmp_size, perm);
76         }
77
78         return 0;
79 }
80
81 struct virtio_net *
82 get_device(int vid)
83 {
84         struct virtio_net *dev = vhost_devices[vid];
85
86         if (unlikely(!dev)) {
87                 RTE_LOG(ERR, VHOST_CONFIG,
88                         "(%d) device not found.\n", vid);
89         }
90
91         return dev;
92 }
93
94 static void
95 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
96 {
97         if ((vq->callfd >= 0) && (destroy != 0))
98                 close(vq->callfd);
99         if (vq->kickfd >= 0)
100                 close(vq->kickfd);
101 }
102
103 /*
104  * Unmap any memory, close any file descriptors and
105  * free any memory owned by a device.
106  */
107 void
108 cleanup_device(struct virtio_net *dev, int destroy)
109 {
110         uint32_t i;
111
112         vhost_backend_cleanup(dev);
113
114         for (i = 0; i < dev->nr_vring; i++)
115                 cleanup_vq(dev->virtqueue[i], destroy);
116 }
117
118 /*
119  * Release virtqueues and device memory.
120  */
121 static void
122 free_device(struct virtio_net *dev)
123 {
124         uint32_t i;
125         struct vhost_virtqueue *vq;
126
127         for (i = 0; i < dev->nr_vring; i++) {
128                 vq = dev->virtqueue[i];
129
130                 rte_free(vq->shadow_used_ring);
131                 rte_free(vq->batch_copy_elems);
132                 rte_mempool_free(vq->iotlb_pool);
133                 rte_free(vq);
134         }
135
136         rte_free(dev);
137 }
138
139 int
140 vring_translate(struct virtio_net *dev, struct vhost_virtqueue *vq)
141 {
142         uint64_t size;
143
144         if (!(dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
145                 goto out;
146
147         size = sizeof(struct vring_desc) * vq->size;
148         vq->desc = (struct vring_desc *)(uintptr_t)vhost_iova_to_vva(dev, vq,
149                                                 vq->ring_addrs.desc_user_addr,
150                                                 size, VHOST_ACCESS_RW);
151         if (!vq->desc)
152                 return -1;
153
154         size = sizeof(struct vring_avail);
155         size += sizeof(uint16_t) * vq->size;
156         vq->avail = (struct vring_avail *)(uintptr_t)vhost_iova_to_vva(dev, vq,
157                                                 vq->ring_addrs.avail_user_addr,
158                                                 size, VHOST_ACCESS_RW);
159         if (!vq->avail)
160                 return -1;
161
162         size = sizeof(struct vring_used);
163         size += sizeof(struct vring_used_elem) * vq->size;
164         vq->used = (struct vring_used *)(uintptr_t)vhost_iova_to_vva(dev, vq,
165                                                 vq->ring_addrs.used_user_addr,
166                                                 size, VHOST_ACCESS_RW);
167         if (!vq->used)
168                 return -1;
169
170 out:
171         vq->access_ok = 1;
172
173         return 0;
174 }
175
176 void
177 vring_invalidate(struct virtio_net *dev, struct vhost_virtqueue *vq)
178 {
179         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
180                 vhost_user_iotlb_wr_lock(vq);
181
182         vq->access_ok = 0;
183         vq->desc = NULL;
184         vq->avail = NULL;
185         vq->used = NULL;
186
187         if (dev->features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))
188                 vhost_user_iotlb_wr_unlock(vq);
189 }
190
191 static void
192 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
193 {
194         struct vhost_virtqueue *vq;
195
196         if (vring_idx >= VHOST_MAX_VRING) {
197                 RTE_LOG(ERR, VHOST_CONFIG,
198                                 "Failed not init vring, out of bound (%d)\n",
199                                 vring_idx);
200                 return;
201         }
202
203         vq = dev->virtqueue[vring_idx];
204
205         memset(vq, 0, sizeof(struct vhost_virtqueue));
206
207         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
208         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
209
210         vhost_user_iotlb_init(dev, vring_idx);
211         /* Backends are set to -1 indicating an inactive device. */
212         vq->backend = -1;
213
214         TAILQ_INIT(&vq->zmbuf_list);
215 }
216
217 static void
218 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
219 {
220         struct vhost_virtqueue *vq;
221         int callfd;
222
223         if (vring_idx >= VHOST_MAX_VRING) {
224                 RTE_LOG(ERR, VHOST_CONFIG,
225                                 "Failed not init vring, out of bound (%d)\n",
226                                 vring_idx);
227                 return;
228         }
229
230         vq = dev->virtqueue[vring_idx];
231         callfd = vq->callfd;
232         init_vring_queue(dev, vring_idx);
233         vq->callfd = callfd;
234 }
235
236 int
237 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
238 {
239         struct vhost_virtqueue *vq;
240
241         vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
242         if (vq == NULL) {
243                 RTE_LOG(ERR, VHOST_CONFIG,
244                         "Failed to allocate memory for vring:%u.\n", vring_idx);
245                 return -1;
246         }
247
248         dev->virtqueue[vring_idx] = vq;
249         init_vring_queue(dev, vring_idx);
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 = 0;
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->slave_req_fd = -1;
305
306         return i;
307 }
308
309 /*
310  * Invoked when there is the vhost-user connection is broken (when
311  * the virtio device is being detached).
312  */
313 void
314 vhost_destroy_device(int vid)
315 {
316         struct virtio_net *dev = get_device(vid);
317
318         if (dev == NULL)
319                 return;
320
321         if (dev->flags & VIRTIO_DEV_RUNNING) {
322                 dev->flags &= ~VIRTIO_DEV_RUNNING;
323                 dev->notify_ops->destroy_device(vid);
324         }
325
326         cleanup_device(dev, 1);
327         free_device(dev);
328
329         vhost_devices[vid] = NULL;
330 }
331
332 void
333 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
334 {
335         struct virtio_net *dev;
336         unsigned int len;
337
338         dev = get_device(vid);
339         if (dev == NULL)
340                 return;
341
342         len = if_len > sizeof(dev->ifname) ?
343                 sizeof(dev->ifname) : if_len;
344
345         strncpy(dev->ifname, if_name, len);
346         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
347 }
348
349 void
350 vhost_enable_dequeue_zero_copy(int vid)
351 {
352         struct virtio_net *dev = get_device(vid);
353
354         if (dev == NULL)
355                 return;
356
357         dev->dequeue_zero_copy = 1;
358 }
359
360 int
361 rte_vhost_get_mtu(int vid, uint16_t *mtu)
362 {
363         struct virtio_net *dev = get_device(vid);
364
365         if (!dev)
366                 return -ENODEV;
367
368         if (!(dev->flags & VIRTIO_DEV_READY))
369                 return -EAGAIN;
370
371         if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
372                 return -ENOTSUP;
373
374         *mtu = dev->mtu;
375
376         return 0;
377 }
378
379 int
380 rte_vhost_get_numa_node(int vid)
381 {
382 #ifdef RTE_LIBRTE_VHOST_NUMA
383         struct virtio_net *dev = get_device(vid);
384         int numa_node;
385         int ret;
386
387         if (dev == NULL)
388                 return -1;
389
390         ret = get_mempolicy(&numa_node, NULL, 0, dev,
391                             MPOL_F_NODE | MPOL_F_ADDR);
392         if (ret < 0) {
393                 RTE_LOG(ERR, VHOST_CONFIG,
394                         "(%d) failed to query numa node: %s\n",
395                         vid, rte_strerror(errno));
396                 return -1;
397         }
398
399         return numa_node;
400 #else
401         RTE_SET_USED(vid);
402         return -1;
403 #endif
404 }
405
406 uint32_t
407 rte_vhost_get_queue_num(int vid)
408 {
409         struct virtio_net *dev = get_device(vid);
410
411         if (dev == NULL)
412                 return 0;
413
414         return dev->nr_vring / 2;
415 }
416
417 uint16_t
418 rte_vhost_get_vring_num(int vid)
419 {
420         struct virtio_net *dev = get_device(vid);
421
422         if (dev == NULL)
423                 return 0;
424
425         return dev->nr_vring;
426 }
427
428 int
429 rte_vhost_get_ifname(int vid, char *buf, size_t len)
430 {
431         struct virtio_net *dev = get_device(vid);
432
433         if (dev == NULL)
434                 return -1;
435
436         len = RTE_MIN(len, sizeof(dev->ifname));
437
438         strncpy(buf, dev->ifname, len);
439         buf[len - 1] = '\0';
440
441         return 0;
442 }
443
444 int
445 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
446 {
447         struct virtio_net *dev;
448
449         dev = get_device(vid);
450         if (!dev)
451                 return -1;
452
453         *features = dev->features;
454         return 0;
455 }
456
457 int
458 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
459 {
460         struct virtio_net *dev;
461         struct rte_vhost_memory *m;
462         size_t size;
463
464         dev = get_device(vid);
465         if (!dev)
466                 return -1;
467
468         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
469         m = malloc(sizeof(struct rte_vhost_memory) + size);
470         if (!m)
471                 return -1;
472
473         m->nregions = dev->mem->nregions;
474         memcpy(m->regions, dev->mem->regions, size);
475         *mem = m;
476
477         return 0;
478 }
479
480 int
481 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
482                           struct rte_vhost_vring *vring)
483 {
484         struct virtio_net *dev;
485         struct vhost_virtqueue *vq;
486
487         dev = get_device(vid);
488         if (!dev)
489                 return -1;
490
491         if (vring_idx >= VHOST_MAX_VRING)
492                 return -1;
493
494         vq = dev->virtqueue[vring_idx];
495         if (!vq)
496                 return -1;
497
498         vring->desc  = vq->desc;
499         vring->avail = vq->avail;
500         vring->used  = vq->used;
501         vring->log_guest_addr  = vq->log_guest_addr;
502
503         vring->callfd  = vq->callfd;
504         vring->kickfd  = vq->kickfd;
505         vring->size    = vq->size;
506
507         return 0;
508 }
509
510 uint16_t
511 rte_vhost_avail_entries(int vid, uint16_t queue_id)
512 {
513         struct virtio_net *dev;
514         struct vhost_virtqueue *vq;
515
516         dev = get_device(vid);
517         if (!dev)
518                 return 0;
519
520         vq = dev->virtqueue[queue_id];
521         if (!vq->enabled)
522                 return 0;
523
524         return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
525 }
526
527 int
528 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
529 {
530         struct virtio_net *dev = get_device(vid);
531
532         if (dev == NULL)
533                 return -1;
534
535         if (enable) {
536                 RTE_LOG(ERR, VHOST_CONFIG,
537                         "guest notification isn't supported.\n");
538                 return -1;
539         }
540
541         dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
542         return 0;
543 }
544
545 void
546 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
547 {
548         struct virtio_net *dev = get_device(vid);
549
550         if (dev == NULL)
551                 return;
552
553         vhost_log_write(dev, addr, len);
554 }
555
556 void
557 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
558                          uint64_t offset, uint64_t len)
559 {
560         struct virtio_net *dev;
561         struct vhost_virtqueue *vq;
562
563         dev = get_device(vid);
564         if (dev == NULL)
565                 return;
566
567         if (vring_idx >= VHOST_MAX_VRING)
568                 return;
569         vq = dev->virtqueue[vring_idx];
570         if (!vq)
571                 return;
572
573         vhost_log_used_vring(dev, vq, offset, len);
574 }
575
576 uint32_t
577 rte_vhost_rx_queue_count(int vid, uint16_t qid)
578 {
579         struct virtio_net *dev;
580         struct vhost_virtqueue *vq;
581
582         dev = get_device(vid);
583         if (dev == NULL)
584                 return 0;
585
586         if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
587                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
588                         dev->vid, __func__, qid);
589                 return 0;
590         }
591
592         vq = dev->virtqueue[qid];
593         if (vq == NULL)
594                 return 0;
595
596         if (unlikely(vq->enabled == 0 || vq->avail == NULL))
597                 return 0;
598
599         return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
600 }