vhost: turn queue pair to vring
[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_virtio_net.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 int
321 rte_vhost_get_ifname(int vid, char *buf, size_t len)
322 {
323         struct virtio_net *dev = get_device(vid);
324
325         if (dev == NULL)
326                 return -1;
327
328         len = RTE_MIN(len, sizeof(dev->ifname));
329
330         strncpy(buf, dev->ifname, len);
331         buf[len - 1] = '\0';
332
333         return 0;
334 }
335
336 int
337 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
338 {
339         struct virtio_net *dev;
340
341         dev = get_device(vid);
342         if (!dev)
343                 return -1;
344
345         *features = dev->features;
346         return 0;
347 }
348
349 int
350 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
351 {
352         struct virtio_net *dev;
353         struct rte_vhost_memory *m;
354         size_t size;
355
356         dev = get_device(vid);
357         if (!dev)
358                 return -1;
359
360         size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
361         m = malloc(size);
362         if (!m)
363                 return -1;
364
365         m->nregions = dev->mem->nregions;
366         memcpy(m->regions, dev->mem->regions, size);
367         *mem = m;
368
369         return 0;
370 }
371
372 int
373 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
374                           struct rte_vhost_vring *vring)
375 {
376         struct virtio_net *dev;
377         struct vhost_virtqueue *vq;
378
379         dev = get_device(vid);
380         if (!dev)
381                 return -1;
382
383         if (vring_idx >= VHOST_MAX_VRING)
384                 return -1;
385
386         vq = dev->virtqueue[vring_idx];
387         if (!vq)
388                 return -1;
389
390         vring->desc  = vq->desc;
391         vring->avail = vq->avail;
392         vring->used  = vq->used;
393         vring->log_guest_addr  = vq->log_guest_addr;
394
395         vring->callfd  = vq->callfd;
396         vring->kickfd  = vq->kickfd;
397         vring->size    = vq->size;
398
399         return 0;
400 }
401
402 uint16_t
403 rte_vhost_avail_entries(int vid, uint16_t queue_id)
404 {
405         struct virtio_net *dev;
406         struct vhost_virtqueue *vq;
407
408         dev = get_device(vid);
409         if (!dev)
410                 return 0;
411
412         vq = dev->virtqueue[queue_id];
413         if (!vq->enabled)
414                 return 0;
415
416         return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
417 }
418
419 int
420 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
421 {
422         struct virtio_net *dev = get_device(vid);
423
424         if (dev == NULL)
425                 return -1;
426
427         if (enable) {
428                 RTE_LOG(ERR, VHOST_CONFIG,
429                         "guest notification isn't supported.\n");
430                 return -1;
431         }
432
433         dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
434         return 0;
435 }