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