4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
35 #include <fuse/cuse_lowlevel.h>
36 #include <linux/vhost.h>
37 #include <linux/virtio_net.h>
41 #include <sys/eventfd.h>
42 #include <sys/ioctl.h>
46 #include <rte_ethdev.h>
48 #include <rte_string_fns.h>
49 #include <rte_memory.h>
52 #include "virtio-net.h"
53 #include "vhost-net-cdev.h"
54 #include "eventfd_link/eventfd_link.h"
57 * Device linked list structure for configuration.
59 struct virtio_net_config_ll {
60 struct virtio_net dev; /* Virtio device. */
61 struct virtio_net_config_ll *next; /* Next entry on linked list. */
64 const char eventfd_cdev[] = "/dev/eventfd-link";
66 /* device ops to add/remove device to data core. */
67 static struct virtio_net_device_ops const * notify_ops;
68 /* Root address of the linked list in the configuration core. */
69 static struct virtio_net_config_ll *ll_root = NULL;
71 /* Features supported by this application. RX merge buffers are disabled by default. */
72 uint64_t VHOST_FEATURES = (0ULL << VIRTIO_NET_F_MRG_RXBUF);
74 /* Line size for reading maps file. */
75 const uint32_t BUFSIZE = PATH_MAX;
77 /* Size of prot char array in procmap. */
80 /* Number of elements in procmap struct. */
83 /* Structure containing information gathered from maps file. */
86 uint64_t va_start; /* Start virtual address in file. */
87 uint64_t len; /* Size of file. */
88 uint64_t pgoff; /* Not used. */
89 uint32_t maj; /* Not used. */
90 uint32_t min; /* Not used. */
91 uint32_t ino; /* Not used. */
92 char prot[PROT_SZ]; /* Not used. */
93 char fname[PATH_MAX]; /* File name. */
97 * Converts QEMU virtual address to Vhost virtual address. This function is used
98 * to convert the ring addresses to our address space.
101 qva_to_vva(struct virtio_net *dev, uint64_t qemu_va)
103 struct virtio_memory_regions *region;
104 uint64_t vhost_va = 0;
105 uint32_t regionidx = 0;
107 /* Find the region where the address lives. */
108 for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
109 region = &dev->mem->regions[regionidx];
110 if ((qemu_va >= region->userspace_address) &&
111 (qemu_va <= region->userspace_address +
112 region->memory_size)) {
113 vhost_va = dev->mem->mapped_address + qemu_va - dev->mem->base_address;
121 * Locate the file containing QEMU's memory space and map it to our address space.
124 host_memory_map (struct virtio_net *dev, struct virtio_memory *mem, pid_t pid, uint64_t addr)
126 struct dirent *dptr = NULL;
127 struct procmap procmap;
131 char memfile[PATH_MAX];
132 char mapfile[PATH_MAX];
133 char procdir[PATH_MAX];
134 char resolved_path[PATH_MAX];
140 char *str, *sp, *in[PROCMAP_SZ];
143 /* Path where mem files are located. */
144 snprintf (procdir, PATH_MAX, "/proc/%u/fd/", pid);
145 /* Maps file used to locate mem file. */
146 snprintf (mapfile, PATH_MAX, "/proc/%u/maps", pid);
148 fmap = fopen(mapfile, "r");
150 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Failed to open maps file for pid %d\n", dev->device_fh, pid);
154 /* Read through maps file until we find out base_address. */
155 while (fgets(line, BUFSIZE, fmap) != 0) {
158 /* Split line in to fields. */
159 for (i = 0; i < PROCMAP_SZ; i++) {
160 if (((in[i] = strtok_r(str, &dlm[i], &sp)) == NULL) || (errno != 0)) {
167 /* Convert/Copy each field as needed. */
168 procmap.va_start = strtoull(in[0], &end, 16);
169 if ((in[0] == '\0') || (end == NULL) || (*end != '\0') || (errno != 0)) {
174 procmap.len = strtoull(in[1], &end, 16);
175 if ((in[1] == '\0') || (end == NULL) || (*end != '\0') || (errno != 0)) {
180 procmap.pgoff = strtoull(in[3], &end, 16);
181 if ((in[3] == '\0') || (end == NULL) || (*end != '\0') || (errno != 0)) {
186 procmap.maj = strtoul(in[4], &end, 16);
187 if ((in[4] == '\0') || (end == NULL) || (*end != '\0') || (errno != 0)) {
192 procmap.min = strtoul(in[5], &end, 16);
193 if ((in[5] == '\0') || (end == NULL) || (*end != '\0') || (errno != 0)) {
198 procmap.ino = strtoul(in[6], &end, 16);
199 if ((in[6] == '\0') || (end == NULL) || (*end != '\0') || (errno != 0)) {
204 memcpy(&procmap.prot, in[2], PROT_SZ);
205 memcpy(&procmap.fname, in[7], PATH_MAX);
207 if (procmap.va_start == addr) {
208 procmap.len = procmap.len - procmap.va_start;
216 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Failed to find memory file in pid %d maps file\n", dev->device_fh, pid);
220 /* Find the guest memory file among the process fds. */
221 dp = opendir(procdir);
223 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Cannot open pid %d process directory \n", dev->device_fh, pid);
230 /* Read the fd directory contents. */
231 while (NULL != (dptr = readdir(dp))) {
232 snprintf (memfile, PATH_MAX, "/proc/%u/fd/%s", pid, dptr->d_name);
233 realpath(memfile, resolved_path);
234 if (resolved_path == NULL) {
235 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Failed to resolve fd directory\n", dev->device_fh);
239 if (strncmp(resolved_path, procmap.fname,
240 strnlen(procmap.fname, PATH_MAX)) == 0) {
249 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Failed to find memory file for pid %d\n", dev->device_fh, pid);
252 /* Open the shared memory file and map the memory into this process. */
253 fd = open(memfile, O_RDWR);
256 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Failed to open %s for pid %d\n", dev->device_fh, memfile, pid);
260 map = mmap(0, (size_t)procmap.len, PROT_READ|PROT_WRITE , MAP_POPULATE|MAP_SHARED, fd, 0);
263 if (map == MAP_FAILED) {
264 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Error mapping the file %s for pid %d\n", dev->device_fh, memfile, pid);
268 /* Store the memory address and size in the device data structure */
269 mem->mapped_address = (uint64_t)(uintptr_t)map;
270 mem->mapped_size = procmap.len;
272 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") Mem File: %s->%s - Size: %llu - VA: %p\n", dev->device_fh,
273 memfile, resolved_path, (long long unsigned)mem->mapped_size, map);
279 * Retrieves an entry from the devices configuration linked list.
281 static struct virtio_net_config_ll *
282 get_config_ll_entry(struct vhost_device_ctx ctx)
284 struct virtio_net_config_ll *ll_dev = ll_root;
286 /* Loop through linked list until the device_fh is found. */
287 while (ll_dev != NULL) {
288 if (ll_dev->dev.device_fh == ctx.fh)
290 ll_dev = ll_dev->next;
297 * Searches the configuration core linked list and retrieves the device if it exists.
299 static struct virtio_net *
300 get_device(struct vhost_device_ctx ctx)
302 struct virtio_net_config_ll *ll_dev;
304 ll_dev = get_config_ll_entry(ctx);
306 /* If a matching entry is found in the linked list, return the device in that entry. */
311 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Device not found in linked list.\n", ctx.fh);
316 * Add entry containing a device to the device configuration linked list.
319 add_config_ll_entry(struct virtio_net_config_ll *new_ll_dev)
321 struct virtio_net_config_ll *ll_dev = ll_root;
323 /* If ll_dev == NULL then this is the first device so go to else */
325 /* If the 1st device_fh != 0 then we insert our device here. */
326 if (ll_dev->dev.device_fh != 0) {
327 new_ll_dev->dev.device_fh = 0;
328 new_ll_dev->next = ll_dev;
329 ll_root = new_ll_dev;
331 /* Increment through the ll until we find un unused device_fh. Insert the device at that entry*/
332 while ((ll_dev->next != NULL) && (ll_dev->dev.device_fh == (ll_dev->next->dev.device_fh - 1)))
333 ll_dev = ll_dev->next;
335 new_ll_dev->dev.device_fh = ll_dev->dev.device_fh + 1;
336 new_ll_dev->next = ll_dev->next;
337 ll_dev->next = new_ll_dev;
340 ll_root = new_ll_dev;
341 ll_root->dev.device_fh = 0;
347 * Unmap any memory, close any file descriptors and free any memory owned by a device.
350 cleanup_device(struct virtio_net *dev)
352 /* Unmap QEMU memory file if mapped. */
354 munmap((void*)(uintptr_t)dev->mem->mapped_address, (size_t)dev->mem->mapped_size);
358 /* Close any event notifiers opened by device. */
359 if (dev->virtqueue[VIRTIO_RXQ]->callfd)
360 close((int)dev->virtqueue[VIRTIO_RXQ]->callfd);
361 if (dev->virtqueue[VIRTIO_RXQ]->kickfd)
362 close((int)dev->virtqueue[VIRTIO_RXQ]->kickfd);
363 if (dev->virtqueue[VIRTIO_TXQ]->callfd)
364 close((int)dev->virtqueue[VIRTIO_TXQ]->callfd);
365 if (dev->virtqueue[VIRTIO_TXQ]->kickfd)
366 close((int)dev->virtqueue[VIRTIO_TXQ]->kickfd);
370 * Release virtqueues and device memory.
373 free_device(struct virtio_net_config_ll *ll_dev)
375 /* Free any malloc'd memory */
376 free(ll_dev->dev.virtqueue[VIRTIO_RXQ]);
377 free(ll_dev->dev.virtqueue[VIRTIO_TXQ]);
381 * Remove an entry from the device configuration linked list.
383 static struct virtio_net_config_ll *
384 rm_config_ll_entry(struct virtio_net_config_ll *ll_dev, struct virtio_net_config_ll *ll_dev_last)
386 /* First remove the device and then clean it up. */
387 if (ll_dev == ll_root) {
388 ll_root = ll_dev->next;
389 cleanup_device(&ll_dev->dev);
393 if (likely(ll_dev_last != NULL)) {
394 ll_dev_last->next = ll_dev->next;
395 cleanup_device(&ll_dev->dev);
397 return ll_dev_last->next;
399 cleanup_device(&ll_dev->dev);
401 RTE_LOG(ERR, VHOST_CONFIG, "Remove entry from config_ll failed\n");
408 * Initialise all variables in device structure.
411 init_device(struct virtio_net *dev)
415 /* Virtqueues have already been malloced so we don't want to set them to NULL. */
416 vq_offset = offsetof(struct virtio_net, mem);
418 /* Set everything to 0. */
419 memset((void*)(uintptr_t)((uint64_t)(uintptr_t)dev + vq_offset), 0,
420 (sizeof(struct virtio_net) - (size_t)vq_offset));
421 memset(dev->virtqueue[VIRTIO_RXQ], 0, sizeof(struct vhost_virtqueue));
422 memset(dev->virtqueue[VIRTIO_TXQ], 0, sizeof(struct vhost_virtqueue));
424 /* Backends are set to -1 indicating an inactive device. */
425 dev->virtqueue[VIRTIO_RXQ]->backend = VIRTIO_DEV_STOPPED;
426 dev->virtqueue[VIRTIO_TXQ]->backend = VIRTIO_DEV_STOPPED;
430 * Function is called from the CUSE open function. The device structure is
431 * initialised and a new entry is added to the device configuration linked
435 new_device(struct vhost_device_ctx ctx)
437 struct virtio_net_config_ll *new_ll_dev;
438 struct vhost_virtqueue *virtqueue_rx, *virtqueue_tx;
440 /* Setup device and virtqueues. */
441 new_ll_dev = malloc(sizeof(struct virtio_net_config_ll));
442 if (new_ll_dev == NULL) {
443 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Failed to allocate memory for dev.\n", ctx.fh);
447 virtqueue_rx = malloc(sizeof(struct vhost_virtqueue));
448 if (virtqueue_rx == NULL) {
450 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Failed to allocate memory for virtqueue_rx.\n", ctx.fh);
454 virtqueue_tx = malloc(sizeof(struct vhost_virtqueue));
455 if (virtqueue_tx == NULL) {
458 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Failed to allocate memory for virtqueue_tx.\n", ctx.fh);
462 new_ll_dev->dev.virtqueue[VIRTIO_RXQ] = virtqueue_rx;
463 new_ll_dev->dev.virtqueue[VIRTIO_TXQ] = virtqueue_tx;
465 /* Initialise device and virtqueues. */
466 init_device(&new_ll_dev->dev);
468 new_ll_dev->next = NULL;
470 /* Add entry to device configuration linked list. */
471 add_config_ll_entry(new_ll_dev);
473 return new_ll_dev->dev.device_fh;
477 * Function is called from the CUSE release function. This function will cleanup
478 * the device and remove it from device configuration linked list.
481 destroy_device(struct vhost_device_ctx ctx)
483 struct virtio_net_config_ll *ll_dev_cur_ctx, *ll_dev_last = NULL;
484 struct virtio_net_config_ll *ll_dev_cur = ll_root;
486 /* Find the linked list entry for the device to be removed. */
487 ll_dev_cur_ctx = get_config_ll_entry(ctx);
488 while (ll_dev_cur != NULL) {
489 /* If the device is found or a device that doesn't exist is found then it is removed. */
490 if (ll_dev_cur == ll_dev_cur_ctx) {
492 * If the device is running on a data core then call the function to remove it from
495 if ((ll_dev_cur->dev.flags & VIRTIO_DEV_RUNNING))
496 notify_ops->destroy_device(&(ll_dev_cur->dev));
497 ll_dev_cur = rm_config_ll_entry(ll_dev_cur, ll_dev_last);
499 ll_dev_last = ll_dev_cur;
500 ll_dev_cur = ll_dev_cur->next;
506 * Called from CUSE IOCTL: VHOST_SET_OWNER
507 * This function just returns success at the moment unless the device hasn't been initialised.
510 set_owner(struct vhost_device_ctx ctx)
512 struct virtio_net *dev;
514 dev = get_device(ctx);
522 * Called from CUSE IOCTL: VHOST_RESET_OWNER
525 reset_owner(struct vhost_device_ctx ctx)
527 struct virtio_net_config_ll *ll_dev;
529 ll_dev = get_config_ll_entry(ctx);
531 cleanup_device(&ll_dev->dev);
532 init_device(&ll_dev->dev);
538 * Called from CUSE IOCTL: VHOST_GET_FEATURES
539 * The features that we support are requested.
542 get_features(struct vhost_device_ctx ctx, uint64_t *pu)
544 struct virtio_net *dev;
546 dev = get_device(ctx);
550 /* Send our supported features. */
551 *pu = VHOST_FEATURES;
556 * Called from CUSE IOCTL: VHOST_SET_FEATURES
557 * We receive the negotiated set of features supported by us and the virtio device.
560 set_features(struct vhost_device_ctx ctx, uint64_t *pu)
562 struct virtio_net *dev;
564 dev = get_device(ctx);
567 if (*pu & ~VHOST_FEATURES)
570 /* Store the negotiated feature list for the device. */
573 /* Set the vhost_hlen depending on if VIRTIO_NET_F_MRG_RXBUF is set. */
574 if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
575 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") Mergeable RX buffers enabled\n", dev->device_fh);
576 dev->virtqueue[VIRTIO_RXQ]->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
577 dev->virtqueue[VIRTIO_TXQ]->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
579 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") Mergeable RX buffers disabled\n", dev->device_fh);
580 dev->virtqueue[VIRTIO_RXQ]->vhost_hlen = sizeof(struct virtio_net_hdr);
581 dev->virtqueue[VIRTIO_TXQ]->vhost_hlen = sizeof(struct virtio_net_hdr);
588 * Called from CUSE IOCTL: VHOST_SET_MEM_TABLE
589 * This function creates and populates the memory structure for the device. This includes
590 * storing offsets used to translate buffer addresses.
593 set_mem_table(struct vhost_device_ctx ctx, const void *mem_regions_addr, uint32_t nregions)
595 struct virtio_net *dev;
596 struct vhost_memory_region *mem_regions;
597 struct virtio_memory *mem;
598 uint64_t size = offsetof(struct vhost_memory, regions);
599 uint32_t regionidx, valid_regions;
601 dev = get_device(ctx);
606 munmap((void*)(uintptr_t)dev->mem->mapped_address, (size_t)dev->mem->mapped_size);
610 /* Malloc the memory structure depending on the number of regions. */
611 mem = calloc(1, sizeof(struct virtio_memory) + (sizeof(struct virtio_memory_regions) * nregions));
613 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Failed to allocate memory for dev->mem.\n", dev->device_fh);
617 mem->nregions = nregions;
619 mem_regions = (void*)(uintptr_t)((uint64_t)(uintptr_t)mem_regions_addr + size);
621 for (regionidx = 0; regionidx < mem->nregions; regionidx++) {
622 /* Populate the region structure for each region. */
623 mem->regions[regionidx].guest_phys_address = mem_regions[regionidx].guest_phys_addr;
624 mem->regions[regionidx].guest_phys_address_end = mem->regions[regionidx].guest_phys_address +
625 mem_regions[regionidx].memory_size;
626 mem->regions[regionidx].memory_size = mem_regions[regionidx].memory_size;
627 mem->regions[regionidx].userspace_address = mem_regions[regionidx].userspace_addr;
629 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") REGION: %u - GPA: %p - QEMU VA: %p - SIZE (%"PRIu64")\n", dev->device_fh,
630 regionidx, (void*)(uintptr_t)mem->regions[regionidx].guest_phys_address,
631 (void*)(uintptr_t)mem->regions[regionidx].userspace_address,
632 mem->regions[regionidx].memory_size);
634 /*set the base address mapping*/
635 if (mem->regions[regionidx].guest_phys_address == 0x0) {
636 mem->base_address = mem->regions[regionidx].userspace_address;
637 /* Map VM memory file */
638 if (host_memory_map(dev, mem, ctx.pid, mem->base_address) != 0) {
645 /* Check that we have a valid base address. */
646 if (mem->base_address == 0) {
647 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Failed to find base address of qemu memory file.\n", dev->device_fh);
652 /* Check if all of our regions have valid mappings. Usually one does not exist in the QEMU memory file. */
653 valid_regions = mem->nregions;
654 for (regionidx = 0; regionidx < mem->nregions; regionidx++) {
655 if ((mem->regions[regionidx].userspace_address < mem->base_address) ||
656 (mem->regions[regionidx].userspace_address > (mem->base_address + mem->mapped_size)))
660 /* If a region does not have a valid mapping we rebuild our memory struct to contain only valid entries. */
661 if (valid_regions != mem->nregions) {
662 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") Not all memory regions exist in the QEMU mem file. Re-populating mem structure\n",
665 /* Re-populate the memory structure with only valid regions. Invalid regions are over-written with memmove. */
668 for (regionidx = mem->nregions; 0 != regionidx--;) {
669 if ((mem->regions[regionidx].userspace_address < mem->base_address) ||
670 (mem->regions[regionidx].userspace_address > (mem->base_address + mem->mapped_size))) {
671 memmove(&mem->regions[regionidx], &mem->regions[regionidx + 1],
672 sizeof(struct virtio_memory_regions) * valid_regions);
678 mem->nregions = valid_regions;
682 * Calculate the address offset for each region. This offset is used to identify the vhost virtual address
683 * corresponding to a QEMU guest physical address.
685 for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
686 dev->mem->regions[regionidx].address_offset = dev->mem->regions[regionidx].userspace_address - dev->mem->base_address
687 + dev->mem->mapped_address - dev->mem->regions[regionidx].guest_phys_address;
694 * Called from CUSE IOCTL: VHOST_SET_VRING_NUM
695 * The virtio device sends us the size of the descriptor ring.
698 set_vring_num(struct vhost_device_ctx ctx, struct vhost_vring_state *state)
700 struct virtio_net *dev;
702 dev = get_device(ctx);
706 /* State->index refers to the queue index. The TX queue is 1, RX queue is 0. */
707 dev->virtqueue[state->index]->size = state->num;
713 * Called from CUSE IOCTL: VHOST_SET_VRING_ADDR
714 * The virtio device sends us the desc, used and avail ring addresses. This function
715 * then converts these to our address space.
718 set_vring_addr(struct vhost_device_ctx ctx, struct vhost_vring_addr *addr)
720 struct virtio_net *dev;
721 struct vhost_virtqueue *vq;
723 dev = get_device(ctx);
727 /* addr->index refers to the queue index. The TX queue is 1, RX queue is 0. */
728 vq = dev->virtqueue[addr->index];
730 /* The addresses are converted from QEMU virtual to Vhost virtual. */
731 vq->desc = (struct vring_desc*)(uintptr_t)qva_to_vva(dev, addr->desc_user_addr);
733 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Failed to find descriptor ring address.\n", dev->device_fh);
737 vq->avail = (struct vring_avail*)(uintptr_t)qva_to_vva(dev, addr->avail_user_addr);
738 if (vq->avail == 0) {
739 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Failed to find available ring address.\n", dev->device_fh);
743 vq->used = (struct vring_used*)(uintptr_t)qva_to_vva(dev, addr->used_user_addr);
745 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Failed to find used ring address.\n", dev->device_fh);
749 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address desc: %p\n", dev->device_fh, vq->desc);
750 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address avail: %p\n", dev->device_fh, vq->avail);
751 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address used: %p\n", dev->device_fh, vq->used);
757 * Called from CUSE IOCTL: VHOST_SET_VRING_BASE
758 * The virtio device sends us the available ring last used index.
761 set_vring_base(struct vhost_device_ctx ctx, struct vhost_vring_state *state)
763 struct virtio_net *dev;
765 dev = get_device(ctx);
769 /* State->index refers to the queue index. The TX queue is 1, RX queue is 0. */
770 dev->virtqueue[state->index]->last_used_idx = state->num;
771 dev->virtqueue[state->index]->last_used_idx_res = state->num;
777 * Called from CUSE IOCTL: VHOST_GET_VRING_BASE
778 * We send the virtio device our available ring last used index.
781 get_vring_base(struct vhost_device_ctx ctx, uint32_t index, struct vhost_vring_state *state)
783 struct virtio_net *dev;
785 dev = get_device(ctx);
789 state->index = index;
790 /* State->index refers to the queue index. The TX queue is 1, RX queue is 0. */
791 state->num = dev->virtqueue[state->index]->last_used_idx;
797 * This function uses the eventfd_link kernel module to copy an eventfd file descriptor
798 * provided by QEMU in to our process space.
801 eventfd_copy(struct virtio_net *dev, struct eventfd_copy *eventfd_copy)
803 int eventfd_link, ret;
805 /* Open the character device to the kernel module. */
806 eventfd_link = open(eventfd_cdev, O_RDWR);
807 if (eventfd_link < 0) {
808 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") eventfd_link module is not loaded\n", dev->device_fh);
812 /* Call the IOCTL to copy the eventfd. */
813 ret = ioctl(eventfd_link, EVENTFD_COPY, eventfd_copy);
817 RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") EVENTFD_COPY ioctl failed\n", dev->device_fh);
826 * Called from CUSE IOCTL: VHOST_SET_VRING_CALL
827 * The virtio device sends an eventfd to interrupt the guest. This fd gets copied in
828 * to our process space.
831 set_vring_call(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
833 struct virtio_net *dev;
834 struct eventfd_copy eventfd_kick;
835 struct vhost_virtqueue *vq;
837 dev = get_device(ctx);
841 /* file->index refers to the queue index. The TX queue is 1, RX queue is 0. */
842 vq = dev->virtqueue[file->index];
845 close((int)vq->kickfd);
847 /* Populate the eventfd_copy structure and call eventfd_copy. */
848 vq->kickfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
849 eventfd_kick.source_fd = vq->kickfd;
850 eventfd_kick.target_fd = file->fd;
851 eventfd_kick.target_pid = ctx.pid;
853 if (eventfd_copy(dev, &eventfd_kick))
860 * Called from CUSE IOCTL: VHOST_SET_VRING_KICK
861 * The virtio device sends an eventfd that it can use to notify us. This fd gets copied in
862 * to our process space.
865 set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
867 struct virtio_net *dev;
868 struct eventfd_copy eventfd_call;
869 struct vhost_virtqueue *vq;
871 dev = get_device(ctx);
875 /* file->index refers to the queue index. The TX queue is 1, RX queue is 0. */
876 vq = dev->virtqueue[file->index];
879 close((int)vq->callfd);
881 /* Populate the eventfd_copy structure and call eventfd_copy. */
882 vq->callfd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
883 eventfd_call.source_fd = vq->callfd;
884 eventfd_call.target_fd = file->fd;
885 eventfd_call.target_pid = ctx.pid;
887 if (eventfd_copy(dev, &eventfd_call))
894 * Called from CUSE IOCTL: VHOST_NET_SET_BACKEND
895 * To complete device initialisation when the virtio driver is loaded we are provided with a
896 * valid fd for a tap device (not used by us). If this happens then we can add the device to a
897 * data core. When the virtio driver is removed we get fd=-1. At that point we remove the device
898 * from the data core. The device will still exist in the device configuration linked list.
901 set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
903 struct virtio_net *dev;
905 dev = get_device(ctx);
910 /* file->index refers to the queue index. The TX queue is 1, RX queue is 0. */
911 dev->virtqueue[file->index]->backend = file->fd;
913 /* If the device isn't already running and both backend fds are set we add the device. */
914 if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
915 if (((int)dev->virtqueue[VIRTIO_TXQ]->backend != VIRTIO_DEV_STOPPED) &&
916 ((int)dev->virtqueue[VIRTIO_RXQ]->backend != VIRTIO_DEV_STOPPED))
917 return notify_ops->new_device(dev);
918 /* Otherwise we remove it. */
920 if (file->fd == VIRTIO_DEV_STOPPED) {
921 notify_ops->destroy_device(dev);
927 * Function pointers are set for the device operations to allow CUSE to call functions
928 * when an IOCTL, device_add or device_release is received.
930 static const struct vhost_net_device_ops vhost_device_ops =
932 .new_device = new_device,
933 .destroy_device = destroy_device,
935 .get_features = get_features,
936 .set_features = set_features,
938 .set_mem_table = set_mem_table,
940 .set_vring_num = set_vring_num,
941 .set_vring_addr = set_vring_addr,
942 .set_vring_base = set_vring_base,
943 .get_vring_base = get_vring_base,
945 .set_vring_kick = set_vring_kick,
946 .set_vring_call = set_vring_call,
948 .set_backend = set_backend,
950 .set_owner = set_owner,
951 .reset_owner = reset_owner,
955 * Called by main to setup callbacks when registering CUSE device.
957 struct vhost_net_device_ops const *
958 get_virtio_net_callbacks(void)
960 return &vhost_device_ops;
964 * Register ops so that we can add/remove device to data core.
967 rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const ops)