vhost: support slave requests channel
[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                 rte_free(vq->batch_copy_elems);
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         dev->slave_req_fd = -1;
211
212         return i;
213 }
214
215 /*
216  * Invoked when there is the vhost-user connection is broken (when
217  * the virtio device is being detached).
218  */
219 void
220 vhost_destroy_device(int vid)
221 {
222         struct virtio_net *dev = get_device(vid);
223
224         if (dev == NULL)
225                 return;
226
227         if (dev->flags & VIRTIO_DEV_RUNNING) {
228                 dev->flags &= ~VIRTIO_DEV_RUNNING;
229                 dev->notify_ops->destroy_device(vid);
230         }
231
232         cleanup_device(dev, 1);
233         free_device(dev);
234
235         vhost_devices[vid] = NULL;
236 }
237
238 void
239 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
240 {
241         struct virtio_net *dev;
242         unsigned int len;
243
244         dev = get_device(vid);
245         if (dev == NULL)
246                 return;
247
248         len = if_len > sizeof(dev->ifname) ?
249                 sizeof(dev->ifname) : if_len;
250
251         strncpy(dev->ifname, if_name, len);
252         dev->ifname[sizeof(dev->ifname) - 1] = '\0';
253 }
254
255 void
256 vhost_enable_dequeue_zero_copy(int vid)
257 {
258         struct virtio_net *dev = get_device(vid);
259
260         if (dev == NULL)
261                 return;
262
263         dev->dequeue_zero_copy = 1;
264 }
265
266 int
267 rte_vhost_get_mtu(int vid, uint16_t *mtu)
268 {
269         struct virtio_net *dev = get_device(vid);
270
271         if (!dev)
272                 return -ENODEV;
273
274         if (!(dev->flags & VIRTIO_DEV_READY))
275                 return -EAGAIN;
276
277         if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
278                 return -ENOTSUP;
279
280         *mtu = dev->mtu;
281
282         return 0;
283 }
284
285 int
286 rte_vhost_get_numa_node(int vid)
287 {
288 #ifdef RTE_LIBRTE_VHOST_NUMA
289         struct virtio_net *dev = get_device(vid);
290         int numa_node;
291         int ret;
292
293         if (dev == NULL)
294                 return -1;
295
296         ret = get_mempolicy(&numa_node, NULL, 0, dev,
297                             MPOL_F_NODE | MPOL_F_ADDR);
298         if (ret < 0) {
299                 RTE_LOG(ERR, VHOST_CONFIG,
300                         "(%d) failed to query numa node: %s\n",
301                         vid, rte_strerror(errno));
302                 return -1;
303         }
304
305         return numa_node;
306 #else
307         RTE_SET_USED(vid);
308         return -1;
309 #endif
310 }
311
312 uint32_t
313 rte_vhost_get_queue_num(int vid)
314 {
315         struct virtio_net *dev = get_device(vid);
316
317         if (dev == NULL)
318                 return 0;
319
320         return dev->nr_vring / 2;
321 }
322
323 uint16_t
324 rte_vhost_get_vring_num(int vid)
325 {
326         struct virtio_net *dev = get_device(vid);
327
328         if (dev == NULL)
329                 return 0;
330
331         return dev->nr_vring;
332 }
333
334 int
335 rte_vhost_get_ifname(int vid, char *buf, size_t len)
336 {
337         struct virtio_net *dev = get_device(vid);
338
339         if (dev == NULL)
340                 return -1;
341
342         len = RTE_MIN(len, sizeof(dev->ifname));
343
344         strncpy(buf, dev->ifname, len);
345         buf[len - 1] = '\0';
346
347         return 0;
348 }
349
350 int
351 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
352 {
353         struct virtio_net *dev;
354
355         dev = get_device(vid);
356         if (!dev)
357                 return -1;
358
359         *features = dev->features;
360         return 0;
361 }
362
363 int
364 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
365 {
366         struct virtio_net *dev;
367         struct rte_vhost_memory *m;
368         size_t size;
369
370         dev = get_device(vid);
371         if (!dev)
372                 return -1;
373
374         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
375         m = malloc(sizeof(struct rte_vhost_memory) + size);
376         if (!m)
377                 return -1;
378
379         m->nregions = dev->mem->nregions;
380         memcpy(m->regions, dev->mem->regions, size);
381         *mem = m;
382
383         return 0;
384 }
385
386 int
387 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
388                           struct rte_vhost_vring *vring)
389 {
390         struct virtio_net *dev;
391         struct vhost_virtqueue *vq;
392
393         dev = get_device(vid);
394         if (!dev)
395                 return -1;
396
397         if (vring_idx >= VHOST_MAX_VRING)
398                 return -1;
399
400         vq = dev->virtqueue[vring_idx];
401         if (!vq)
402                 return -1;
403
404         vring->desc  = vq->desc;
405         vring->avail = vq->avail;
406         vring->used  = vq->used;
407         vring->log_guest_addr  = vq->log_guest_addr;
408
409         vring->callfd  = vq->callfd;
410         vring->kickfd  = vq->kickfd;
411         vring->size    = vq->size;
412
413         return 0;
414 }
415
416 uint16_t
417 rte_vhost_avail_entries(int vid, uint16_t queue_id)
418 {
419         struct virtio_net *dev;
420         struct vhost_virtqueue *vq;
421
422         dev = get_device(vid);
423         if (!dev)
424                 return 0;
425
426         vq = dev->virtqueue[queue_id];
427         if (!vq->enabled)
428                 return 0;
429
430         return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
431 }
432
433 int
434 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
435 {
436         struct virtio_net *dev = get_device(vid);
437
438         if (dev == NULL)
439                 return -1;
440
441         if (enable) {
442                 RTE_LOG(ERR, VHOST_CONFIG,
443                         "guest notification isn't supported.\n");
444                 return -1;
445         }
446
447         dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
448         return 0;
449 }
450
451 void
452 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
453 {
454         struct virtio_net *dev = get_device(vid);
455
456         if (dev == NULL)
457                 return;
458
459         vhost_log_write(dev, addr, len);
460 }
461
462 void
463 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
464                          uint64_t offset, uint64_t len)
465 {
466         struct virtio_net *dev;
467         struct vhost_virtqueue *vq;
468
469         dev = get_device(vid);
470         if (dev == NULL)
471                 return;
472
473         if (vring_idx >= VHOST_MAX_VRING)
474                 return;
475         vq = dev->virtqueue[vring_idx];
476         if (!vq)
477                 return;
478
479         vhost_log_used_vring(dev, vq, offset, len);
480 }
481
482 uint32_t
483 rte_vhost_rx_queue_count(int vid, uint16_t qid)
484 {
485         struct virtio_net *dev;
486         struct vhost_virtqueue *vq;
487
488         dev = get_device(vid);
489         if (dev == NULL)
490                 return 0;
491
492         if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
493                 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
494                         dev->vid, __func__, qid);
495                 return 0;
496         }
497
498         vq = dev->virtqueue[qid];
499         if (vq == NULL)
500                 return 0;
501
502         if (unlikely(vq->enabled == 0 || vq->avail == NULL))
503                 return 0;
504
505         return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;
506 }