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.
34 #include <linux/vhost.h>
35 #include <linux/virtio_net.h>
42 #include <sys/socket.h>
44 #include <rte_ethdev.h>
46 #include <rte_string_fns.h>
47 #include <rte_memory.h>
48 #include <rte_virtio_net.h>
50 #include "vhost-net.h"
51 #include "virtio-net.h"
54 * Device linked list structure for configuration.
56 struct virtio_net_config_ll {
57 struct virtio_net dev; /* Virtio device.*/
58 struct virtio_net_config_ll *next; /* Next dev on linked list.*/
61 /* device ops to add/remove device to/from data core. */
62 struct virtio_net_device_ops const *notify_ops;
63 /* root address of the linked list of managed virtio devices */
64 static struct virtio_net_config_ll *ll_root;
66 /* Features supported by this lib. */
67 #define VHOST_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
68 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
69 (1ULL << VIRTIO_NET_F_CTRL_RX))
70 static uint64_t VHOST_FEATURES = VHOST_SUPPORTED_FEATURES;
74 * Converts QEMU virtual address to Vhost virtual address. This function is
75 * used to convert the ring addresses to our address space.
78 qva_to_vva(struct virtio_net *dev, uint64_t qemu_va)
80 struct virtio_memory_regions *region;
81 uint64_t vhost_va = 0;
82 uint32_t regionidx = 0;
84 /* Find the region where the address lives. */
85 for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
86 region = &dev->mem->regions[regionidx];
87 if ((qemu_va >= region->userspace_address) &&
88 (qemu_va <= region->userspace_address +
89 region->memory_size)) {
90 vhost_va = qemu_va + region->guest_phys_address +
91 region->address_offset -
92 region->userspace_address;
101 * Retrieves an entry from the devices configuration linked list.
103 static struct virtio_net_config_ll *
104 get_config_ll_entry(struct vhost_device_ctx ctx)
106 struct virtio_net_config_ll *ll_dev = ll_root;
108 /* Loop through linked list until the device_fh is found. */
109 while (ll_dev != NULL) {
110 if (ll_dev->dev.device_fh == ctx.fh)
112 ll_dev = ll_dev->next;
119 * Searches the configuration core linked list and
120 * retrieves the device if it exists.
123 get_device(struct vhost_device_ctx ctx)
125 struct virtio_net_config_ll *ll_dev;
127 ll_dev = get_config_ll_entry(ctx);
132 RTE_LOG(ERR, VHOST_CONFIG,
133 "(%"PRIu64") Device not found in linked list.\n", ctx.fh);
138 * Add entry containing a device to the device configuration linked list.
141 add_config_ll_entry(struct virtio_net_config_ll *new_ll_dev)
143 struct virtio_net_config_ll *ll_dev = ll_root;
145 /* If ll_dev == NULL then this is the first device so go to else */
147 /* If the 1st device_fh != 0 then we insert our device here. */
148 if (ll_dev->dev.device_fh != 0) {
149 new_ll_dev->dev.device_fh = 0;
150 new_ll_dev->next = ll_dev;
151 ll_root = new_ll_dev;
154 * Increment through the ll until we find un unused
155 * device_fh. Insert the device at that entry.
157 while ((ll_dev->next != NULL) &&
158 (ll_dev->dev.device_fh ==
159 (ll_dev->next->dev.device_fh - 1)))
160 ll_dev = ll_dev->next;
162 new_ll_dev->dev.device_fh = ll_dev->dev.device_fh + 1;
163 new_ll_dev->next = ll_dev->next;
164 ll_dev->next = new_ll_dev;
167 ll_root = new_ll_dev;
168 ll_root->dev.device_fh = 0;
174 * Unmap any memory, close any file descriptors and
175 * free any memory owned by a device.
178 cleanup_device(struct virtio_net *dev)
180 /* Unmap QEMU memory file if mapped. */
182 munmap((void *)(uintptr_t)dev->mem->mapped_address,
183 (size_t)dev->mem->mapped_size);
187 /* Close any event notifiers opened by device. */
188 if (dev->virtqueue[VIRTIO_RXQ]->callfd)
189 close((int)dev->virtqueue[VIRTIO_RXQ]->callfd);
190 if (dev->virtqueue[VIRTIO_RXQ]->kickfd)
191 close((int)dev->virtqueue[VIRTIO_RXQ]->kickfd);
192 if (dev->virtqueue[VIRTIO_TXQ]->callfd)
193 close((int)dev->virtqueue[VIRTIO_TXQ]->callfd);
194 if (dev->virtqueue[VIRTIO_TXQ]->kickfd)
195 close((int)dev->virtqueue[VIRTIO_TXQ]->kickfd);
199 * Release virtqueues and device memory.
202 free_device(struct virtio_net_config_ll *ll_dev)
204 /* Free any malloc'd memory */
205 free(ll_dev->dev.virtqueue[VIRTIO_RXQ]);
206 free(ll_dev->dev.virtqueue[VIRTIO_TXQ]);
211 * Remove an entry from the device configuration linked list.
213 static struct virtio_net_config_ll *
214 rm_config_ll_entry(struct virtio_net_config_ll *ll_dev,
215 struct virtio_net_config_ll *ll_dev_last)
217 /* First remove the device and then clean it up. */
218 if (ll_dev == ll_root) {
219 ll_root = ll_dev->next;
220 cleanup_device(&ll_dev->dev);
224 if (likely(ll_dev_last != NULL)) {
225 ll_dev_last->next = ll_dev->next;
226 cleanup_device(&ll_dev->dev);
228 return ll_dev_last->next;
230 cleanup_device(&ll_dev->dev);
232 RTE_LOG(ERR, VHOST_CONFIG,
233 "Remove entry from config_ll failed\n");
240 * Initialise all variables in device structure.
243 init_device(struct virtio_net *dev)
248 * Virtqueues have already been malloced so
249 * we don't want to set them to NULL.
251 vq_offset = offsetof(struct virtio_net, mem);
253 /* Set everything to 0. */
254 memset((void *)(uintptr_t)((uint64_t)(uintptr_t)dev + vq_offset), 0,
255 (sizeof(struct virtio_net) - (size_t)vq_offset));
256 memset(dev->virtqueue[VIRTIO_RXQ], 0, sizeof(struct vhost_virtqueue));
257 memset(dev->virtqueue[VIRTIO_TXQ], 0, sizeof(struct vhost_virtqueue));
259 dev->virtqueue[VIRTIO_RXQ]->kickfd = (eventfd_t)-1;
260 dev->virtqueue[VIRTIO_RXQ]->callfd = (eventfd_t)-1;
261 dev->virtqueue[VIRTIO_TXQ]->kickfd = (eventfd_t)-1;
262 dev->virtqueue[VIRTIO_TXQ]->callfd = (eventfd_t)-1;
264 /* Backends are set to -1 indicating an inactive device. */
265 dev->virtqueue[VIRTIO_RXQ]->backend = VIRTIO_DEV_STOPPED;
266 dev->virtqueue[VIRTIO_TXQ]->backend = VIRTIO_DEV_STOPPED;
270 * Function is called from the CUSE open function. The device structure is
271 * initialised and a new entry is added to the device configuration linked
275 new_device(struct vhost_device_ctx ctx)
277 struct virtio_net_config_ll *new_ll_dev;
278 struct vhost_virtqueue *virtqueue_rx, *virtqueue_tx;
280 /* Setup device and virtqueues. */
281 new_ll_dev = malloc(sizeof(struct virtio_net_config_ll));
282 if (new_ll_dev == NULL) {
283 RTE_LOG(ERR, VHOST_CONFIG,
284 "(%"PRIu64") Failed to allocate memory for dev.\n",
289 virtqueue_rx = malloc(sizeof(struct vhost_virtqueue));
290 if (virtqueue_rx == NULL) {
292 RTE_LOG(ERR, VHOST_CONFIG,
293 "(%"PRIu64") Failed to allocate memory for rxq.\n",
298 virtqueue_tx = malloc(sizeof(struct vhost_virtqueue));
299 if (virtqueue_tx == NULL) {
302 RTE_LOG(ERR, VHOST_CONFIG,
303 "(%"PRIu64") Failed to allocate memory for txq.\n",
308 new_ll_dev->dev.virtqueue[VIRTIO_RXQ] = virtqueue_rx;
309 new_ll_dev->dev.virtqueue[VIRTIO_TXQ] = virtqueue_tx;
311 /* Initialise device and virtqueues. */
312 init_device(&new_ll_dev->dev);
314 new_ll_dev->next = NULL;
316 /* Add entry to device configuration linked list. */
317 add_config_ll_entry(new_ll_dev);
319 return new_ll_dev->dev.device_fh;
323 * Function is called from the CUSE release function. This function will
324 * cleanup the device and remove it from device configuration linked list.
327 destroy_device(struct vhost_device_ctx ctx)
329 struct virtio_net_config_ll *ll_dev_cur_ctx, *ll_dev_last = NULL;
330 struct virtio_net_config_ll *ll_dev_cur = ll_root;
332 /* Find the linked list entry for the device to be removed. */
333 ll_dev_cur_ctx = get_config_ll_entry(ctx);
334 while (ll_dev_cur != NULL) {
336 * If the device is found or
337 * a device that doesn't exist is found then it is removed.
339 if (ll_dev_cur == ll_dev_cur_ctx) {
341 * If the device is running on a data core then call
342 * the function to remove it from the data core.
344 if ((ll_dev_cur->dev.flags & VIRTIO_DEV_RUNNING))
345 notify_ops->destroy_device(&(ll_dev_cur->dev));
346 ll_dev_cur = rm_config_ll_entry(ll_dev_cur,
349 ll_dev_last = ll_dev_cur;
350 ll_dev_cur = ll_dev_cur->next;
356 set_ifname(struct vhost_device_ctx ctx,
357 const char *if_name, unsigned int if_len)
359 struct virtio_net *dev;
362 dev = get_device(ctx);
366 len = if_len > sizeof(dev->ifname) ?
367 sizeof(dev->ifname) : if_len;
369 strncpy(dev->ifname, if_name, len);
374 * Called from CUSE IOCTL: VHOST_SET_OWNER
375 * This function just returns success at the moment unless
376 * the device hasn't been initialised.
379 set_owner(struct vhost_device_ctx ctx)
381 struct virtio_net *dev;
383 dev = get_device(ctx);
391 * Called from CUSE IOCTL: VHOST_RESET_OWNER
394 reset_owner(struct vhost_device_ctx ctx)
396 struct virtio_net_config_ll *ll_dev;
398 ll_dev = get_config_ll_entry(ctx);
400 cleanup_device(&ll_dev->dev);
401 init_device(&ll_dev->dev);
407 * Called from CUSE IOCTL: VHOST_GET_FEATURES
408 * The features that we support are requested.
411 get_features(struct vhost_device_ctx ctx, uint64_t *pu)
413 struct virtio_net *dev;
415 dev = get_device(ctx);
419 /* Send our supported features. */
420 *pu = VHOST_FEATURES;
425 * Called from CUSE IOCTL: VHOST_SET_FEATURES
426 * We receive the negotiated features supported by us and the virtio device.
429 set_features(struct vhost_device_ctx ctx, uint64_t *pu)
431 struct virtio_net *dev;
433 dev = get_device(ctx);
436 if (*pu & ~VHOST_FEATURES)
439 /* Store the negotiated feature list for the device. */
442 /* Set the vhost_hlen depending on if VIRTIO_NET_F_MRG_RXBUF is set. */
443 if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
444 LOG_DEBUG(VHOST_CONFIG,
445 "(%"PRIu64") Mergeable RX buffers enabled\n",
447 dev->virtqueue[VIRTIO_RXQ]->vhost_hlen =
448 sizeof(struct virtio_net_hdr_mrg_rxbuf);
449 dev->virtqueue[VIRTIO_TXQ]->vhost_hlen =
450 sizeof(struct virtio_net_hdr_mrg_rxbuf);
452 LOG_DEBUG(VHOST_CONFIG,
453 "(%"PRIu64") Mergeable RX buffers disabled\n",
455 dev->virtqueue[VIRTIO_RXQ]->vhost_hlen =
456 sizeof(struct virtio_net_hdr);
457 dev->virtqueue[VIRTIO_TXQ]->vhost_hlen =
458 sizeof(struct virtio_net_hdr);
464 * Called from CUSE IOCTL: VHOST_SET_VRING_NUM
465 * The virtio device sends us the size of the descriptor ring.
468 set_vring_num(struct vhost_device_ctx ctx, struct vhost_vring_state *state)
470 struct virtio_net *dev;
472 dev = get_device(ctx);
476 /* State->index refers to the queue index. The txq is 1, rxq is 0. */
477 dev->virtqueue[state->index]->size = state->num;
483 * Called from CUSE IOCTL: VHOST_SET_VRING_ADDR
484 * The virtio device sends us the desc, used and avail ring addresses.
485 * This function then converts these to our address space.
488 set_vring_addr(struct vhost_device_ctx ctx, struct vhost_vring_addr *addr)
490 struct virtio_net *dev;
491 struct vhost_virtqueue *vq;
493 dev = get_device(ctx);
497 /* addr->index refers to the queue index. The txq 1, rxq is 0. */
498 vq = dev->virtqueue[addr->index];
500 /* The addresses are converted from QEMU virtual to Vhost virtual. */
501 vq->desc = (struct vring_desc *)(uintptr_t)qva_to_vva(dev,
502 addr->desc_user_addr);
504 RTE_LOG(ERR, VHOST_CONFIG,
505 "(%"PRIu64") Failed to find desc ring address.\n",
510 vq->avail = (struct vring_avail *)(uintptr_t)qva_to_vva(dev,
511 addr->avail_user_addr);
512 if (vq->avail == 0) {
513 RTE_LOG(ERR, VHOST_CONFIG,
514 "(%"PRIu64") Failed to find avail ring address.\n",
519 vq->used = (struct vring_used *)(uintptr_t)qva_to_vva(dev,
520 addr->used_user_addr);
522 RTE_LOG(ERR, VHOST_CONFIG,
523 "(%"PRIu64") Failed to find used ring address.\n",
528 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address desc: %p\n",
529 dev->device_fh, vq->desc);
530 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address avail: %p\n",
531 dev->device_fh, vq->avail);
532 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address used: %p\n",
533 dev->device_fh, vq->used);
539 * Called from CUSE IOCTL: VHOST_SET_VRING_BASE
540 * The virtio device sends us the available ring last used index.
543 set_vring_base(struct vhost_device_ctx ctx, struct vhost_vring_state *state)
545 struct virtio_net *dev;
547 dev = get_device(ctx);
551 /* State->index refers to the queue index. The txq is 1, rxq is 0. */
552 dev->virtqueue[state->index]->last_used_idx = state->num;
553 dev->virtqueue[state->index]->last_used_idx_res = state->num;
559 * Called from CUSE IOCTL: VHOST_GET_VRING_BASE
560 * We send the virtio device our available ring last used index.
563 get_vring_base(struct vhost_device_ctx ctx, uint32_t index,
564 struct vhost_vring_state *state)
566 struct virtio_net *dev;
568 dev = get_device(ctx);
572 state->index = index;
573 /* State->index refers to the queue index. The txq is 1, rxq is 0. */
574 state->num = dev->virtqueue[state->index]->last_used_idx;
581 * Called from CUSE IOCTL: VHOST_SET_VRING_CALL
582 * The virtio device sends an eventfd to interrupt the guest. This fd gets
583 * copied into our process space.
586 set_vring_call(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
588 struct virtio_net *dev;
589 struct vhost_virtqueue *vq;
591 dev = get_device(ctx);
595 /* file->index refers to the queue index. The txq is 1, rxq is 0. */
596 vq = dev->virtqueue[file->index];
598 if ((int)vq->callfd >= 0)
599 close((int)vq->callfd);
601 vq->callfd = file->fd;
607 * Called from CUSE IOCTL: VHOST_SET_VRING_KICK
608 * The virtio device sends an eventfd that it can use to notify us.
609 * This fd gets copied into our process space.
612 set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
614 struct virtio_net *dev;
615 struct vhost_virtqueue *vq;
617 dev = get_device(ctx);
621 /* file->index refers to the queue index. The txq is 1, rxq is 0. */
622 vq = dev->virtqueue[file->index];
624 if ((int)vq->kickfd >= 0)
625 close((int)vq->kickfd);
627 vq->kickfd = file->fd;
633 * Called from CUSE IOCTL: VHOST_NET_SET_BACKEND
634 * To complete device initialisation when the virtio driver is loaded,
635 * we are provided with a valid fd for a tap device (not used by us).
636 * If this happens then we can add the device to a data core.
637 * When the virtio driver is removed we get fd=-1.
638 * At that point we remove the device from the data core.
639 * The device will still exist in the device configuration linked list.
642 set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
644 struct virtio_net *dev;
646 dev = get_device(ctx);
650 /* file->index refers to the queue index. The txq is 1, rxq is 0. */
651 dev->virtqueue[file->index]->backend = file->fd;
654 * If the device isn't already running and both backend fds are set,
657 if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
658 if (((int)dev->virtqueue[VIRTIO_TXQ]->backend != VIRTIO_DEV_STOPPED) &&
659 ((int)dev->virtqueue[VIRTIO_RXQ]->backend != VIRTIO_DEV_STOPPED)) {
660 return notify_ops->new_device(dev);
662 /* Otherwise we remove it. */
664 if (file->fd == VIRTIO_DEV_STOPPED)
665 notify_ops->destroy_device(dev);
670 * Function pointers are set for the device operations to allow CUSE to call
671 * functions when an IOCTL, device_add or device_release is received.
673 static const struct vhost_net_device_ops vhost_device_ops = {
674 .new_device = new_device,
675 .destroy_device = destroy_device,
677 .set_ifname = set_ifname,
679 .get_features = get_features,
680 .set_features = set_features,
682 .set_vring_num = set_vring_num,
683 .set_vring_addr = set_vring_addr,
684 .set_vring_base = set_vring_base,
685 .get_vring_base = get_vring_base,
687 .set_vring_kick = set_vring_kick,
688 .set_vring_call = set_vring_call,
690 .set_backend = set_backend,
692 .set_owner = set_owner,
693 .reset_owner = reset_owner,
697 * Called by main to setup callbacks when registering CUSE device.
699 struct vhost_net_device_ops const *
700 get_virtio_net_callbacks(void)
702 return &vhost_device_ops;
705 int rte_vhost_enable_guest_notification(struct virtio_net *dev,
706 uint16_t queue_id, int enable)
709 RTE_LOG(ERR, VHOST_CONFIG,
710 "guest notification isn't supported.\n");
714 dev->virtqueue[queue_id]->used->flags =
715 enable ? 0 : VRING_USED_F_NO_NOTIFY;
719 uint64_t rte_vhost_feature_get(void)
721 return VHOST_FEATURES;
724 int rte_vhost_feature_disable(uint64_t feature_mask)
726 VHOST_FEATURES = VHOST_FEATURES & ~feature_mask;
730 int rte_vhost_feature_enable(uint64_t feature_mask)
732 if ((feature_mask & VHOST_SUPPORTED_FEATURES) == feature_mask) {
733 VHOST_FEATURES = VHOST_FEATURES | feature_mask;
740 * Register ops so that we can add/remove device to data core.
743 rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const ops)