vhost: fix leak of fds and mmaps
[dpdk.git] / lib / librte_vhost / virtio-net.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #include <assert.h>
40 #include <sys/mman.h>
41 #include <unistd.h>
42 #ifdef RTE_LIBRTE_VHOST_NUMA
43 #include <numaif.h>
44 #endif
45
46 #include <sys/socket.h>
47
48 #include <rte_ethdev.h>
49 #include <rte_log.h>
50 #include <rte_string_fns.h>
51 #include <rte_memory.h>
52 #include <rte_malloc.h>
53 #include <rte_virtio_net.h>
54
55 #include "vhost-net.h"
56 #include "virtio-net.h"
57
58 /*
59  * Device linked list structure for configuration.
60  */
61 struct virtio_net_config_ll {
62         struct virtio_net dev;                  /* Virtio device.*/
63         struct virtio_net_config_ll *next;      /* Next dev on linked list.*/
64 };
65
66 /* device ops to add/remove device to/from data core. */
67 struct virtio_net_device_ops const *notify_ops;
68 /* root address of the linked list of managed virtio devices */
69 static struct virtio_net_config_ll *ll_root;
70
71 #define VHOST_USER_F_PROTOCOL_FEATURES  30
72
73 /* Features supported by this lib. */
74 #define VHOST_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
75                                 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
76                                 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
77                                 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
78                                 (VHOST_SUPPORTS_MQ)            | \
79                                 (1ULL << VIRTIO_F_VERSION_1)   | \
80                                 (1ULL << VHOST_F_LOG_ALL)      | \
81                                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
82                                 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
83                                 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
84                                 (1ULL << VIRTIO_NET_F_CSUM)    | \
85                                 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
86                                 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
87                                 (1ULL << VIRTIO_NET_F_GUEST_TSO6))
88
89 static uint64_t VHOST_FEATURES = VHOST_SUPPORTED_FEATURES;
90
91
92 /*
93  * Converts QEMU virtual address to Vhost virtual address. This function is
94  * used to convert the ring addresses to our address space.
95  */
96 static uint64_t
97 qva_to_vva(struct virtio_net *dev, uint64_t qemu_va)
98 {
99         struct virtio_memory_regions *region;
100         uint64_t vhost_va = 0;
101         uint32_t regionidx = 0;
102
103         /* Find the region where the address lives. */
104         for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
105                 region = &dev->mem->regions[regionidx];
106                 if ((qemu_va >= region->userspace_address) &&
107                         (qemu_va <= region->userspace_address +
108                         region->memory_size)) {
109                         vhost_va = qemu_va + region->guest_phys_address +
110                                 region->address_offset -
111                                 region->userspace_address;
112                         break;
113                 }
114         }
115         return vhost_va;
116 }
117
118
119 /*
120  * Retrieves an entry from the devices configuration linked list.
121  */
122 static struct virtio_net_config_ll *
123 get_config_ll_entry(struct vhost_device_ctx ctx)
124 {
125         struct virtio_net_config_ll *ll_dev = ll_root;
126
127         /* Loop through linked list until the device_fh is found. */
128         while (ll_dev != NULL) {
129                 if (ll_dev->dev.device_fh == ctx.fh)
130                         return ll_dev;
131                 ll_dev = ll_dev->next;
132         }
133
134         return NULL;
135 }
136
137 /*
138  * Searches the configuration core linked list and
139  * retrieves the device if it exists.
140  */
141 struct virtio_net *
142 get_device(struct vhost_device_ctx ctx)
143 {
144         struct virtio_net_config_ll *ll_dev;
145
146         ll_dev = get_config_ll_entry(ctx);
147
148         if (ll_dev)
149                 return &ll_dev->dev;
150
151         RTE_LOG(ERR, VHOST_CONFIG,
152                 "(%"PRIu64") Device not found in linked list.\n", ctx.fh);
153         return NULL;
154 }
155
156 /*
157  * Add entry containing a device to the device configuration linked list.
158  */
159 static void
160 add_config_ll_entry(struct virtio_net_config_ll *new_ll_dev)
161 {
162         struct virtio_net_config_ll *ll_dev = ll_root;
163
164         /* If ll_dev == NULL then this is the first device so go to else */
165         if (ll_dev) {
166                 /* If the 1st device_fh != 0 then we insert our device here. */
167                 if (ll_dev->dev.device_fh != 0) {
168                         new_ll_dev->dev.device_fh = 0;
169                         new_ll_dev->next = ll_dev;
170                         ll_root = new_ll_dev;
171                 } else {
172                         /*
173                          * Increment through the ll until we find un unused
174                          * device_fh. Insert the device at that entry.
175                          */
176                         while ((ll_dev->next != NULL) &&
177                                 (ll_dev->dev.device_fh ==
178                                         (ll_dev->next->dev.device_fh - 1)))
179                                 ll_dev = ll_dev->next;
180
181                         new_ll_dev->dev.device_fh = ll_dev->dev.device_fh + 1;
182                         new_ll_dev->next = ll_dev->next;
183                         ll_dev->next = new_ll_dev;
184                 }
185         } else {
186                 ll_root = new_ll_dev;
187                 ll_root->dev.device_fh = 0;
188         }
189
190 }
191
192 static void
193 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
194 {
195         if ((vq->callfd >= 0) && (destroy != 0))
196                 close(vq->callfd);
197         if (vq->kickfd >= 0)
198                 close(vq->kickfd);
199 }
200
201 /*
202  * Unmap any memory, close any file descriptors and
203  * free any memory owned by a device.
204  */
205 static void
206 cleanup_device(struct virtio_net *dev, int destroy)
207 {
208         uint32_t i;
209
210         vhost_backend_cleanup(dev);
211
212         for (i = 0; i < dev->virt_qp_nb; i++) {
213                 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ], destroy);
214                 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ], destroy);
215         }
216 }
217
218 /*
219  * Release virtqueues and device memory.
220  */
221 static void
222 free_device(struct virtio_net_config_ll *ll_dev)
223 {
224         uint32_t i;
225
226         for (i = 0; i < ll_dev->dev.virt_qp_nb; i++)
227                 rte_free(ll_dev->dev.virtqueue[i * VIRTIO_QNUM]);
228
229         rte_free(ll_dev);
230 }
231
232 /*
233  * Remove an entry from the device configuration linked list.
234  */
235 static struct virtio_net_config_ll *
236 rm_config_ll_entry(struct virtio_net_config_ll *ll_dev,
237         struct virtio_net_config_ll *ll_dev_last)
238 {
239         /* First remove the device and then clean it up. */
240         if (ll_dev == ll_root) {
241                 ll_root = ll_dev->next;
242                 cleanup_device(&ll_dev->dev, 1);
243                 free_device(ll_dev);
244                 return ll_root;
245         } else {
246                 if (likely(ll_dev_last != NULL)) {
247                         ll_dev_last->next = ll_dev->next;
248                         cleanup_device(&ll_dev->dev, 1);
249                         free_device(ll_dev);
250                         return ll_dev_last->next;
251                 } else {
252                         cleanup_device(&ll_dev->dev, 1);
253                         free_device(ll_dev);
254                         RTE_LOG(ERR, VHOST_CONFIG,
255                                 "Remove entry from config_ll failed\n");
256                         return NULL;
257                 }
258         }
259 }
260
261 static void
262 init_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
263 {
264         memset(vq, 0, sizeof(struct vhost_virtqueue));
265
266         vq->kickfd = -1;
267         vq->callfd = -1;
268
269         /* Backends are set to -1 indicating an inactive device. */
270         vq->backend = -1;
271
272         /* always set the default vq pair to enabled */
273         if (qp_idx == 0)
274                 vq->enabled = 1;
275 }
276
277 static void
278 init_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
279 {
280         uint32_t base_idx = qp_idx * VIRTIO_QNUM;
281
282         init_vring_queue(dev->virtqueue[base_idx + VIRTIO_RXQ], qp_idx);
283         init_vring_queue(dev->virtqueue[base_idx + VIRTIO_TXQ], qp_idx);
284 }
285
286 static void
287 reset_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
288 {
289         int callfd;
290
291         callfd = vq->callfd;
292         init_vring_queue(vq, qp_idx);
293         vq->callfd = callfd;
294 }
295
296 static void
297 reset_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
298 {
299         uint32_t base_idx = qp_idx * VIRTIO_QNUM;
300
301         reset_vring_queue(dev->virtqueue[base_idx + VIRTIO_RXQ], qp_idx);
302         reset_vring_queue(dev->virtqueue[base_idx + VIRTIO_TXQ], qp_idx);
303 }
304
305 static int
306 alloc_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
307 {
308         struct vhost_virtqueue *virtqueue = NULL;
309         uint32_t virt_rx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_RXQ;
310         uint32_t virt_tx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_TXQ;
311
312         virtqueue = rte_malloc(NULL,
313                                sizeof(struct vhost_virtqueue) * VIRTIO_QNUM, 0);
314         if (virtqueue == NULL) {
315                 RTE_LOG(ERR, VHOST_CONFIG,
316                         "Failed to allocate memory for virt qp:%d.\n", qp_idx);
317                 return -1;
318         }
319
320         dev->virtqueue[virt_rx_q_idx] = virtqueue;
321         dev->virtqueue[virt_tx_q_idx] = virtqueue + VIRTIO_TXQ;
322
323         init_vring_queue_pair(dev, qp_idx);
324
325         dev->virt_qp_nb += 1;
326
327         return 0;
328 }
329
330 /*
331  * Reset some variables in device structure, while keeping few
332  * others untouched, such as device_fh, ifname, virt_qp_nb: they
333  * should be same unless the device is removed.
334  */
335 static void
336 reset_device(struct virtio_net *dev)
337 {
338         uint32_t i;
339
340         dev->features = 0;
341         dev->protocol_features = 0;
342         dev->flags = 0;
343
344         for (i = 0; i < dev->virt_qp_nb; i++)
345                 reset_vring_queue_pair(dev, i);
346 }
347
348 /*
349  * Function is called from the CUSE open function. The device structure is
350  * initialised and a new entry is added to the device configuration linked
351  * list.
352  */
353 static int
354 new_device(struct vhost_device_ctx ctx)
355 {
356         struct virtio_net_config_ll *new_ll_dev;
357
358         /* Setup device and virtqueues. */
359         new_ll_dev = rte_zmalloc(NULL, sizeof(struct virtio_net_config_ll), 0);
360         if (new_ll_dev == NULL) {
361                 RTE_LOG(ERR, VHOST_CONFIG,
362                         "(%"PRIu64") Failed to allocate memory for dev.\n",
363                         ctx.fh);
364                 return -1;
365         }
366
367         new_ll_dev->next = NULL;
368
369         /* Add entry to device configuration linked list. */
370         add_config_ll_entry(new_ll_dev);
371
372         return new_ll_dev->dev.device_fh;
373 }
374
375 /*
376  * Function is called from the CUSE release function. This function will
377  * cleanup the device and remove it from device configuration linked list.
378  */
379 static void
380 destroy_device(struct vhost_device_ctx ctx)
381 {
382         struct virtio_net_config_ll *ll_dev_cur_ctx, *ll_dev_last = NULL;
383         struct virtio_net_config_ll *ll_dev_cur = ll_root;
384
385         /* Find the linked list entry for the device to be removed. */
386         ll_dev_cur_ctx = get_config_ll_entry(ctx);
387         while (ll_dev_cur != NULL) {
388                 /*
389                  * If the device is found or
390                  * a device that doesn't exist is found then it is removed.
391                  */
392                 if (ll_dev_cur == ll_dev_cur_ctx) {
393                         /*
394                          * If the device is running on a data core then call
395                          * the function to remove it from the data core.
396                          */
397                         if ((ll_dev_cur->dev.flags & VIRTIO_DEV_RUNNING))
398                                 notify_ops->destroy_device(&(ll_dev_cur->dev));
399                         ll_dev_cur = rm_config_ll_entry(ll_dev_cur,
400                                         ll_dev_last);
401                 } else {
402                         ll_dev_last = ll_dev_cur;
403                         ll_dev_cur = ll_dev_cur->next;
404                 }
405         }
406 }
407
408 static void
409 set_ifname(struct vhost_device_ctx ctx,
410         const char *if_name, unsigned int if_len)
411 {
412         struct virtio_net *dev;
413         unsigned int len;
414
415         dev = get_device(ctx);
416         if (dev == NULL)
417                 return;
418
419         len = if_len > sizeof(dev->ifname) ?
420                 sizeof(dev->ifname) : if_len;
421
422         strncpy(dev->ifname, if_name, len);
423 }
424
425
426 /*
427  * Called from CUSE IOCTL: VHOST_SET_OWNER
428  * This function just returns success at the moment unless
429  * the device hasn't been initialised.
430  */
431 static int
432 set_owner(struct vhost_device_ctx ctx)
433 {
434         struct virtio_net *dev;
435
436         dev = get_device(ctx);
437         if (dev == NULL)
438                 return -1;
439
440         return 0;
441 }
442
443 /*
444  * Called from CUSE IOCTL: VHOST_RESET_OWNER
445  */
446 static int
447 reset_owner(struct vhost_device_ctx ctx)
448 {
449         struct virtio_net *dev;
450
451         dev = get_device(ctx);
452         if (dev == NULL)
453                 return -1;
454
455         if (dev->flags & VIRTIO_DEV_RUNNING)
456                 notify_ops->destroy_device(dev);
457
458         cleanup_device(dev, 0);
459         reset_device(dev);
460         return 0;
461 }
462
463 /*
464  * Called from CUSE IOCTL: VHOST_GET_FEATURES
465  * The features that we support are requested.
466  */
467 static int
468 get_features(struct vhost_device_ctx ctx, uint64_t *pu)
469 {
470         struct virtio_net *dev;
471
472         dev = get_device(ctx);
473         if (dev == NULL)
474                 return -1;
475
476         /* Send our supported features. */
477         *pu = VHOST_FEATURES;
478         return 0;
479 }
480
481 /*
482  * Called from CUSE IOCTL: VHOST_SET_FEATURES
483  * We receive the negotiated features supported by us and the virtio device.
484  */
485 static int
486 set_features(struct vhost_device_ctx ctx, uint64_t *pu)
487 {
488         struct virtio_net *dev;
489         uint16_t vhost_hlen;
490         uint16_t i;
491
492         dev = get_device(ctx);
493         if (dev == NULL)
494                 return -1;
495         if (*pu & ~VHOST_FEATURES)
496                 return -1;
497
498         dev->features = *pu;
499         if (dev->features &
500                 ((1 << VIRTIO_NET_F_MRG_RXBUF) | (1ULL << VIRTIO_F_VERSION_1))) {
501                 vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
502         } else {
503                 vhost_hlen = sizeof(struct virtio_net_hdr);
504         }
505         LOG_DEBUG(VHOST_CONFIG,
506                 "(%"PRIu64") Mergeable RX buffers %s, virtio 1 %s\n",
507                 dev->device_fh,
508                 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
509                 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
510
511         for (i = 0; i < dev->virt_qp_nb; i++) {
512                 uint16_t base_idx = i * VIRTIO_QNUM;
513
514                 dev->virtqueue[base_idx + VIRTIO_RXQ]->vhost_hlen = vhost_hlen;
515                 dev->virtqueue[base_idx + VIRTIO_TXQ]->vhost_hlen = vhost_hlen;
516         }
517
518         return 0;
519 }
520
521 /*
522  * Called from CUSE IOCTL: VHOST_SET_VRING_NUM
523  * The virtio device sends us the size of the descriptor ring.
524  */
525 static int
526 set_vring_num(struct vhost_device_ctx ctx, struct vhost_vring_state *state)
527 {
528         struct virtio_net *dev;
529
530         dev = get_device(ctx);
531         if (dev == NULL)
532                 return -1;
533
534         /* State->index refers to the queue index. The txq is 1, rxq is 0. */
535         dev->virtqueue[state->index]->size = state->num;
536
537         return 0;
538 }
539
540 /*
541  * Reallocate virtio_dev and vhost_virtqueue data structure to make them on the
542  * same numa node as the memory of vring descriptor.
543  */
544 #ifdef RTE_LIBRTE_VHOST_NUMA
545 static struct virtio_net*
546 numa_realloc(struct virtio_net *dev, int index)
547 {
548         int oldnode, newnode;
549         struct virtio_net_config_ll *old_ll_dev, *new_ll_dev = NULL;
550         struct vhost_virtqueue *old_vq, *new_vq = NULL;
551         int ret;
552         int realloc_dev = 0, realloc_vq = 0;
553
554         old_ll_dev = (struct virtio_net_config_ll *)dev;
555         old_vq = dev->virtqueue[index];
556
557         ret  = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
558                         MPOL_F_NODE | MPOL_F_ADDR);
559         ret = ret | get_mempolicy(&oldnode, NULL, 0, old_ll_dev,
560                         MPOL_F_NODE | MPOL_F_ADDR);
561         if (ret) {
562                 RTE_LOG(ERR, VHOST_CONFIG,
563                         "Unable to get vring desc or dev numa information.\n");
564                 return dev;
565         }
566         if (oldnode != newnode)
567                 realloc_dev = 1;
568
569         ret = get_mempolicy(&oldnode, NULL, 0, old_vq,
570                         MPOL_F_NODE | MPOL_F_ADDR);
571         if (ret) {
572                 RTE_LOG(ERR, VHOST_CONFIG,
573                         "Unable to get vq numa information.\n");
574                 return dev;
575         }
576         if (oldnode != newnode)
577                 realloc_vq = 1;
578
579         if (realloc_dev == 0 && realloc_vq == 0)
580                 return dev;
581
582         if (realloc_dev)
583                 new_ll_dev = rte_malloc_socket(NULL,
584                         sizeof(struct virtio_net_config_ll), 0, newnode);
585         if (realloc_vq)
586                 new_vq = rte_malloc_socket(NULL,
587                         sizeof(struct vhost_virtqueue), 0, newnode);
588         if (!new_ll_dev && !new_vq)
589                 return dev;
590
591         if (realloc_vq)
592                 memcpy(new_vq, old_vq, sizeof(*new_vq));
593         if (realloc_dev)
594                 memcpy(new_ll_dev, old_ll_dev, sizeof(*new_ll_dev));
595         (new_ll_dev ? new_ll_dev : old_ll_dev)->dev.virtqueue[index] =
596                 new_vq ? new_vq : old_vq;
597         if (realloc_vq)
598                 rte_free(old_vq);
599         if (realloc_dev) {
600                 if (ll_root == old_ll_dev)
601                         ll_root = new_ll_dev;
602                 else {
603                         struct virtio_net_config_ll *prev = ll_root;
604                         while (prev->next != old_ll_dev)
605                                 prev = prev->next;
606                         prev->next = new_ll_dev;
607                         new_ll_dev->next = old_ll_dev->next;
608                 }
609                 rte_free(old_ll_dev);
610         }
611
612         return realloc_dev ? &new_ll_dev->dev : dev;
613 }
614 #else
615 static struct virtio_net*
616 numa_realloc(struct virtio_net *dev, int index __rte_unused)
617 {
618         return dev;
619 }
620 #endif
621
622 /*
623  * Called from CUSE IOCTL: VHOST_SET_VRING_ADDR
624  * The virtio device sends us the desc, used and avail ring addresses.
625  * This function then converts these to our address space.
626  */
627 static int
628 set_vring_addr(struct vhost_device_ctx ctx, struct vhost_vring_addr *addr)
629 {
630         struct virtio_net *dev;
631         struct vhost_virtqueue *vq;
632
633         dev = get_device(ctx);
634         if (dev == NULL)
635                 return -1;
636
637         /* addr->index refers to the queue index. The txq 1, rxq is 0. */
638         vq = dev->virtqueue[addr->index];
639
640         /* The addresses are converted from QEMU virtual to Vhost virtual. */
641         vq->desc = (struct vring_desc *)(uintptr_t)qva_to_vva(dev,
642                         addr->desc_user_addr);
643         if (vq->desc == 0) {
644                 RTE_LOG(ERR, VHOST_CONFIG,
645                         "(%"PRIu64") Failed to find desc ring address.\n",
646                         dev->device_fh);
647                 return -1;
648         }
649
650         dev = numa_realloc(dev, addr->index);
651         vq = dev->virtqueue[addr->index];
652
653         vq->avail = (struct vring_avail *)(uintptr_t)qva_to_vva(dev,
654                         addr->avail_user_addr);
655         if (vq->avail == 0) {
656                 RTE_LOG(ERR, VHOST_CONFIG,
657                         "(%"PRIu64") Failed to find avail ring address.\n",
658                         dev->device_fh);
659                 return -1;
660         }
661
662         vq->used = (struct vring_used *)(uintptr_t)qva_to_vva(dev,
663                         addr->used_user_addr);
664         if (vq->used == 0) {
665                 RTE_LOG(ERR, VHOST_CONFIG,
666                         "(%"PRIu64") Failed to find used ring address.\n",
667                         dev->device_fh);
668                 return -1;
669         }
670
671         vq->log_guest_addr = addr->log_guest_addr;
672
673         LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address desc: %p\n",
674                         dev->device_fh, vq->desc);
675         LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address avail: %p\n",
676                         dev->device_fh, vq->avail);
677         LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address used: %p\n",
678                         dev->device_fh, vq->used);
679         LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") log_guest_addr: %"PRIx64"\n",
680                         dev->device_fh, vq->log_guest_addr);
681
682         return 0;
683 }
684
685 /*
686  * Called from CUSE IOCTL: VHOST_SET_VRING_BASE
687  * The virtio device sends us the available ring last used index.
688  */
689 static int
690 set_vring_base(struct vhost_device_ctx ctx, struct vhost_vring_state *state)
691 {
692         struct virtio_net *dev;
693
694         dev = get_device(ctx);
695         if (dev == NULL)
696                 return -1;
697
698         /* State->index refers to the queue index. The txq is 1, rxq is 0. */
699         dev->virtqueue[state->index]->last_used_idx = state->num;
700         dev->virtqueue[state->index]->last_used_idx_res = state->num;
701
702         return 0;
703 }
704
705 /*
706  * Called from CUSE IOCTL: VHOST_GET_VRING_BASE
707  * We send the virtio device our available ring last used index.
708  */
709 static int
710 get_vring_base(struct vhost_device_ctx ctx, uint32_t index,
711         struct vhost_vring_state *state)
712 {
713         struct virtio_net *dev;
714
715         dev = get_device(ctx);
716         if (dev == NULL)
717                 return -1;
718
719         state->index = index;
720         /* State->index refers to the queue index. The txq is 1, rxq is 0. */
721         state->num = dev->virtqueue[state->index]->last_used_idx;
722
723         return 0;
724 }
725
726
727 /*
728  * Called from CUSE IOCTL: VHOST_SET_VRING_CALL
729  * The virtio device sends an eventfd to interrupt the guest. This fd gets
730  * copied into our process space.
731  */
732 static int
733 set_vring_call(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
734 {
735         struct virtio_net *dev;
736         struct vhost_virtqueue *vq;
737         uint32_t cur_qp_idx = file->index / VIRTIO_QNUM;
738
739         dev = get_device(ctx);
740         if (dev == NULL)
741                 return -1;
742
743         /*
744          * FIXME: VHOST_SET_VRING_CALL is the first per-vring message
745          * we get, so we do vring queue pair allocation here.
746          */
747         if (cur_qp_idx + 1 > dev->virt_qp_nb) {
748                 if (alloc_vring_queue_pair(dev, cur_qp_idx) < 0)
749                         return -1;
750         }
751
752         /* file->index refers to the queue index. The txq is 1, rxq is 0. */
753         vq = dev->virtqueue[file->index];
754         assert(vq != NULL);
755
756         if (vq->callfd >= 0)
757                 close(vq->callfd);
758
759         vq->callfd = file->fd;
760
761         return 0;
762 }
763
764 /*
765  * Called from CUSE IOCTL: VHOST_SET_VRING_KICK
766  * The virtio device sends an eventfd that it can use to notify us.
767  * This fd gets copied into our process space.
768  */
769 static int
770 set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
771 {
772         struct virtio_net *dev;
773         struct vhost_virtqueue *vq;
774
775         dev = get_device(ctx);
776         if (dev == NULL)
777                 return -1;
778
779         /* file->index refers to the queue index. The txq is 1, rxq is 0. */
780         vq = dev->virtqueue[file->index];
781
782         if (vq->kickfd >= 0)
783                 close(vq->kickfd);
784
785         vq->kickfd = file->fd;
786
787         return 0;
788 }
789
790 /*
791  * Called from CUSE IOCTL: VHOST_NET_SET_BACKEND
792  * To complete device initialisation when the virtio driver is loaded,
793  * we are provided with a valid fd for a tap device (not used by us).
794  * If this happens then we can add the device to a data core.
795  * When the virtio driver is removed we get fd=-1.
796  * At that point we remove the device from the data core.
797  * The device will still exist in the device configuration linked list.
798  */
799 static int
800 set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
801 {
802         struct virtio_net *dev;
803
804         dev = get_device(ctx);
805         if (dev == NULL)
806                 return -1;
807
808         /* file->index refers to the queue index. The txq is 1, rxq is 0. */
809         dev->virtqueue[file->index]->backend = file->fd;
810
811         /*
812          * If the device isn't already running and both backend fds are set,
813          * we add the device.
814          */
815         if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
816                 if (((int)dev->virtqueue[VIRTIO_TXQ]->backend != VIRTIO_DEV_STOPPED) &&
817                         ((int)dev->virtqueue[VIRTIO_RXQ]->backend != VIRTIO_DEV_STOPPED)) {
818                         return notify_ops->new_device(dev);
819                 }
820         /* Otherwise we remove it. */
821         } else
822                 if (file->fd == VIRTIO_DEV_STOPPED)
823                         notify_ops->destroy_device(dev);
824         return 0;
825 }
826
827 /*
828  * Function pointers are set for the device operations to allow CUSE to call
829  * functions when an IOCTL, device_add or device_release is received.
830  */
831 static const struct vhost_net_device_ops vhost_device_ops = {
832         .new_device = new_device,
833         .destroy_device = destroy_device,
834
835         .set_ifname = set_ifname,
836
837         .get_features = get_features,
838         .set_features = set_features,
839
840         .set_vring_num = set_vring_num,
841         .set_vring_addr = set_vring_addr,
842         .set_vring_base = set_vring_base,
843         .get_vring_base = get_vring_base,
844
845         .set_vring_kick = set_vring_kick,
846         .set_vring_call = set_vring_call,
847
848         .set_backend = set_backend,
849
850         .set_owner = set_owner,
851         .reset_owner = reset_owner,
852 };
853
854 /*
855  * Called by main to setup callbacks when registering CUSE device.
856  */
857 struct vhost_net_device_ops const *
858 get_virtio_net_callbacks(void)
859 {
860         return &vhost_device_ops;
861 }
862
863 int rte_vhost_enable_guest_notification(struct virtio_net *dev,
864         uint16_t queue_id, int enable)
865 {
866         if (enable) {
867                 RTE_LOG(ERR, VHOST_CONFIG,
868                         "guest notification isn't supported.\n");
869                 return -1;
870         }
871
872         dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
873         return 0;
874 }
875
876 uint64_t rte_vhost_feature_get(void)
877 {
878         return VHOST_FEATURES;
879 }
880
881 int rte_vhost_feature_disable(uint64_t feature_mask)
882 {
883         VHOST_FEATURES = VHOST_FEATURES & ~feature_mask;
884         return 0;
885 }
886
887 int rte_vhost_feature_enable(uint64_t feature_mask)
888 {
889         if ((feature_mask & VHOST_SUPPORTED_FEATURES) == feature_mask) {
890                 VHOST_FEATURES = VHOST_FEATURES | feature_mask;
891                 return 0;
892         }
893         return -1;
894 }
895
896 /*
897  * Register ops so that we can add/remove device to data core.
898  */
899 int
900 rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const ops)
901 {
902         notify_ops = ops;
903
904         return 0;
905 }