New upstream version 17.05.1
[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_ethdev.h>
44 #include <rte_log.h>
45 #include <rte_string_fns.h>
46 #include <rte_memory.h>
47 #include <rte_malloc.h>
48 #include <rte_vhost.h>
49
50 #include "vhost.h"
51
52 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
53
54 struct virtio_net *
55 get_device(int vid)
56 {
57         struct virtio_net *dev = vhost_devices[vid];
58
59         if (unlikely(!dev)) {
60                 RTE_LOG(ERR, VHOST_CONFIG,
61                         "(%d) device not found.\n", vid);
62         }
63
64         return dev;
65 }
66
67 static void
68 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
69 {
70         if ((vq->callfd >= 0) && (destroy != 0))
71                 close(vq->callfd);
72         if (vq->kickfd >= 0)
73                 close(vq->kickfd);
74 }
75
76 /*
77  * Unmap any memory, close any file descriptors and
78  * free any memory owned by a device.
79  */
80 void
81 cleanup_device(struct virtio_net *dev, int destroy)
82 {
83         uint32_t i;
84
85         vhost_backend_cleanup(dev);
86
87         for (i = 0; i < dev->nr_vring; i++)
88                 cleanup_vq(dev->virtqueue[i], destroy);
89 }
90
91 /*
92  * Release virtqueues and device memory.
93  */
94 static void
95 free_device(struct virtio_net *dev)
96 {
97         uint32_t i;
98         struct vhost_virtqueue *vq;
99
100         for (i = 0; i < dev->nr_vring; i++) {
101                 vq = dev->virtqueue[i];
102
103                 rte_free(vq->shadow_used_ring);
104
105                 rte_free(vq);
106         }
107
108         rte_free(dev);
109 }
110
111 static void
112 init_vring_queue(struct vhost_virtqueue *vq)
113 {
114         memset(vq, 0, sizeof(struct vhost_virtqueue));
115
116         vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
117         vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
118
119         /* Backends are set to -1 indicating an inactive device. */
120         vq->backend = -1;
121
122         /*
123          * always set the vq to enabled; this is to keep compatibility
124          * with the old QEMU, whereas there is no SET_VRING_ENABLE message.
125          */
126         vq->enabled = 1;
127
128         TAILQ_INIT(&vq->zmbuf_list);
129 }
130
131 static void
132 reset_vring_queue(struct vhost_virtqueue *vq)
133 {
134         int callfd;
135
136         callfd = vq->callfd;
137         init_vring_queue(vq);
138         vq->callfd = callfd;
139 }
140
141 int
142 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
143 {
144         struct vhost_virtqueue *vq;
145
146         vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
147         if (vq == NULL) {
148                 RTE_LOG(ERR, VHOST_CONFIG,
149                         "Failed to allocate memory for vring:%u.\n", vring_idx);
150                 return -1;
151         }
152
153         dev->virtqueue[vring_idx] = vq;
154         init_vring_queue(vq);
155
156         dev->nr_vring += 1;
157
158         return 0;
159 }
160
161 /*
162  * Reset some variables in device structure, while keeping few
163  * others untouched, such as vid, ifname, nr_vring: they
164  * should be same unless the device is removed.
165  */
166 void
167 reset_device(struct virtio_net *dev)
168 {
169         uint32_t i;
170
171         dev->features = 0;
172         dev->protocol_features = 0;
173         dev->flags = 0;
174
175         for (i = 0; i < dev->nr_vring; i++)
176                 reset_vring_queue(dev->virtqueue[i]);
177 }
178
179 /*
180  * Invoked when there is a new vhost-user connection established (when
181  * there is a new virtio device being attached).
182  */
183 int
184 vhost_new_device(void)
185 {
186         struct virtio_net *dev;
187         int i;
188
189         dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
190         if (dev == NULL) {
191                 RTE_LOG(ERR, VHOST_CONFIG,
192                         "Failed to allocate memory for new dev.\n");
193                 return -1;
194         }
195
196         for (i = 0; i < MAX_VHOST_DEVICE; i++) {
197                 if (vhost_devices[i] == NULL)
198                         break;
199         }
200         if (i == MAX_VHOST_DEVICE) {
201                 RTE_LOG(ERR, VHOST_CONFIG,
202                         "Failed to find a free slot for new device.\n");
203                 rte_free(dev);
204                 return -1;
205         }
206
207         vhost_devices[i] = dev;
208         dev->vid = i;
209
210         return i;
211 }
212
213 /*
214  * Invoked when there is the vhost-user connection is broken (when
215  * the virtio device is being detached).
216  */
217 void
218 vhost_destroy_device(int vid)
219 {
220         struct virtio_net *dev = get_device(vid);
221
222         if (dev == NULL)
223                 return;
224
225         if (dev->flags & VIRTIO_DEV_RUNNING) {
226                 dev->flags &= ~VIRTIO_DEV_RUNNING;
227                 dev->notify_ops->destroy_device(vid);
228         }
229
230         cleanup_device(dev, 1);
231         free_device(dev);
232
233         vhost_devices[vid] = NULL;
234 }
235
236 void
237 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
238 {
239         struct virtio_net *dev;
240         unsigned int len;
241
242         dev = get_device(vid);
243         if (dev == NULL)
244                 return;
245
246         len = if_len > sizeof(dev->ifname) ?
247                 sizeof(dev->ifname) : if_len;
248
249         strncpy(dev->ifname, if_name, len);
250         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
251 }
252
253 void
254 vhost_enable_dequeue_zero_copy(int vid)
255 {
256         struct virtio_net *dev = get_device(vid);
257
258         if (dev == NULL)
259                 return;
260
261         dev->dequeue_zero_copy = 1;
262 }
263
264 int
265 rte_vhost_get_mtu(int vid, uint16_t *mtu)
266 {
267         struct virtio_net *dev = get_device(vid);
268
269         if (!dev)
270                 return -ENODEV;
271
272         if (!(dev->flags & VIRTIO_DEV_READY))
273                 return -EAGAIN;
274
275         if (!(dev->features & VIRTIO_NET_F_MTU))
276                 return -ENOTSUP;
277
278         *mtu = dev->mtu;
279
280         return 0;
281 }
282
283 int
284 rte_vhost_get_numa_node(int vid)
285 {
286 #ifdef RTE_LIBRTE_VHOST_NUMA
287         struct virtio_net *dev = get_device(vid);
288         int numa_node;
289         int ret;
290
291         if (dev == NULL)
292                 return -1;
293
294         ret = get_mempolicy(&numa_node, NULL, 0, dev,
295                             MPOL_F_NODE | MPOL_F_ADDR);
296         if (ret < 0) {
297                 RTE_LOG(ERR, VHOST_CONFIG,
298                         "(%d) failed to query numa node: %d\n", vid, ret);
299                 return -1;
300         }
301
302         return numa_node;
303 #else
304         RTE_SET_USED(vid);
305         return -1;
306 #endif
307 }
308
309 uint32_t
310 rte_vhost_get_queue_num(int vid)
311 {
312         struct virtio_net *dev = get_device(vid);
313
314         if (dev == NULL)
315                 return 0;
316
317         return dev->nr_vring / 2;
318 }
319
320 uint16_t
321 rte_vhost_get_vring_num(int vid)
322 {
323         struct virtio_net *dev = get_device(vid);
324
325         if (dev == NULL)
326                 return 0;
327
328         return dev->nr_vring;
329 }
330
331 int
332 rte_vhost_get_ifname(int vid, char *buf, size_t len)
333 {
334         struct virtio_net *dev = get_device(vid);
335
336         if (dev == NULL)
337                 return -1;
338
339         len = RTE_MIN(len, sizeof(dev->ifname));
340
341         strncpy(buf, dev->ifname, len);
342         buf[len - 1] = '\0';
343
344         return 0;
345 }
346
347 int
348 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
349 {
350         struct virtio_net *dev;
351
352         dev = get_device(vid);
353         if (!dev)
354                 return -1;
355
356         *features = dev->features;
357         return 0;
358 }
359
360 int
361 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
362 {
363         struct virtio_net *dev;
364         struct rte_vhost_memory *m;
365         size_t size;
366
367         dev = get_device(vid);
368         if (!dev)
369                 return -1;
370
371         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
372         m = malloc(sizeof(struct rte_vhost_memory) + size);
373         if (!m)
374                 return -1;
375
376         m->nregions = dev->mem->nregions;
377         memcpy(m->regions, dev->mem->regions, size);
378         *mem = m;
379
380         return 0;
381 }
382
383 int
384 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
385                           struct rte_vhost_vring *vring)
386 {
387         struct virtio_net *dev;
388         struct vhost_virtqueue *vq;
389
390         dev = get_device(vid);
391         if (!dev)
392                 return -1;
393
394         if (vring_idx >= VHOST_MAX_VRING)
395                 return -1;
396
397         vq = dev->virtqueue[vring_idx];
398         if (!vq)
399                 return -1;
400
401         vring->desc  = vq->desc;
402         vring->avail = vq->avail;
403         vring->used  = vq->used;
404         vring->log_guest_addr  = vq->log_guest_addr;
405
406         vring->callfd  = vq->callfd;
407         vring->kickfd  = vq->kickfd;
408         vring->size    = vq->size;
409
410         return 0;
411 }
412
413 uint16_t
414 rte_vhost_avail_entries(int vid, uint16_t queue_id)
415 {
416         struct virtio_net *dev;
417         struct vhost_virtqueue *vq;
418
419         dev = get_device(vid);
420         if (!dev)
421                 return 0;
422
423         vq = dev->virtqueue[queue_id];
424         if (!vq->enabled)
425                 return 0;
426
427         return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
428 }
429
430 int
431 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
432 {
433         struct virtio_net *dev = get_device(vid);
434
435         if (dev == NULL)
436                 return -1;
437
438         if (enable) {
439                 RTE_LOG(ERR, VHOST_CONFIG,
440                         "guest notification isn't supported.\n");
441                 return -1;
442         }
443
444         dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
445         return 0;
446 }
447
448 void
449 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
450 {
451         struct virtio_net *dev = get_device(vid);
452
453         if (dev == NULL)
454                 return;
455
456         vhost_log_write(dev, addr, len);
457 }
458
459 void
460 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
461                          uint64_t offset, uint64_t len)
462 {
463         struct virtio_net *dev;
464         struct vhost_virtqueue *vq;
465
466         dev = get_device(vid);
467         if (dev == NULL)
468                 return;
469
470         if (vring_idx >= VHOST_MAX_VRING)
471                 return;
472         vq = dev->virtqueue[vring_idx];
473         if (!vq)
474                 return;
475
476         vhost_log_used_vring(dev, vq, offset, len);
477 }