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