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