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_malloc.h>
49 #include <rte_virtio_net.h>
51 #include "vhost-net.h"
52 #include "virtio-net.h"
55 * Device linked list structure for configuration.
57 struct virtio_net_config_ll {
58 struct virtio_net dev; /* Virtio device.*/
59 struct virtio_net_config_ll *next; /* Next dev on linked list.*/
62 /* device ops to add/remove device to/from data core. */
63 struct virtio_net_device_ops const *notify_ops;
64 /* root address of the linked list of managed virtio devices */
65 static struct virtio_net_config_ll *ll_root;
67 /* Features supported by this lib. */
68 #define VHOST_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
69 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
70 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
71 (1ULL << VHOST_F_LOG_ALL))
72 static uint64_t VHOST_FEATURES = VHOST_SUPPORTED_FEATURES;
76 * Converts QEMU virtual address to Vhost virtual address. This function is
77 * used to convert the ring addresses to our address space.
80 qva_to_vva(struct virtio_net *dev, uint64_t qemu_va)
82 struct virtio_memory_regions *region;
83 uint64_t vhost_va = 0;
84 uint32_t regionidx = 0;
86 /* Find the region where the address lives. */
87 for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
88 region = &dev->mem->regions[regionidx];
89 if ((qemu_va >= region->userspace_address) &&
90 (qemu_va <= region->userspace_address +
91 region->memory_size)) {
92 vhost_va = qemu_va + region->guest_phys_address +
93 region->address_offset -
94 region->userspace_address;
103 * Retrieves an entry from the devices configuration linked list.
105 static struct virtio_net_config_ll *
106 get_config_ll_entry(struct vhost_device_ctx ctx)
108 struct virtio_net_config_ll *ll_dev = ll_root;
110 /* Loop through linked list until the device_fh is found. */
111 while (ll_dev != NULL) {
112 if (ll_dev->dev.device_fh == ctx.fh)
114 ll_dev = ll_dev->next;
121 * Searches the configuration core linked list and
122 * retrieves the device if it exists.
125 get_device(struct vhost_device_ctx ctx)
127 struct virtio_net_config_ll *ll_dev;
129 ll_dev = get_config_ll_entry(ctx);
134 RTE_LOG(ERR, VHOST_CONFIG,
135 "(%"PRIu64") Device not found in linked list.\n", ctx.fh);
140 * Add entry containing a device to the device configuration linked list.
143 add_config_ll_entry(struct virtio_net_config_ll *new_ll_dev)
145 struct virtio_net_config_ll *ll_dev = ll_root;
147 /* If ll_dev == NULL then this is the first device so go to else */
149 /* If the 1st device_fh != 0 then we insert our device here. */
150 if (ll_dev->dev.device_fh != 0) {
151 new_ll_dev->dev.device_fh = 0;
152 new_ll_dev->next = ll_dev;
153 ll_root = new_ll_dev;
156 * Increment through the ll until we find un unused
157 * device_fh. Insert the device at that entry.
159 while ((ll_dev->next != NULL) &&
160 (ll_dev->dev.device_fh ==
161 (ll_dev->next->dev.device_fh - 1)))
162 ll_dev = ll_dev->next;
164 new_ll_dev->dev.device_fh = ll_dev->dev.device_fh + 1;
165 new_ll_dev->next = ll_dev->next;
166 ll_dev->next = new_ll_dev;
169 ll_root = new_ll_dev;
170 ll_root->dev.device_fh = 0;
176 * Unmap any memory, close any file descriptors and
177 * free any memory owned by a device.
180 cleanup_device(struct virtio_net *dev)
182 /* Unmap QEMU memory file if mapped. */
184 munmap((void *)(uintptr_t)dev->mem->mapped_address,
185 (size_t)dev->mem->mapped_size);
189 /* Close any event notifiers opened by device. */
190 if ((int)dev->virtqueue[VIRTIO_RXQ]->callfd >= 0)
191 close((int)dev->virtqueue[VIRTIO_RXQ]->callfd);
192 if ((int)dev->virtqueue[VIRTIO_RXQ]->kickfd >= 0)
193 close((int)dev->virtqueue[VIRTIO_RXQ]->kickfd);
194 if ((int)dev->virtqueue[VIRTIO_TXQ]->callfd >= 0)
195 close((int)dev->virtqueue[VIRTIO_TXQ]->callfd);
196 if ((int)dev->virtqueue[VIRTIO_TXQ]->kickfd >= 0)
197 close((int)dev->virtqueue[VIRTIO_TXQ]->kickfd);
201 * Release virtqueues and device memory.
204 free_device(struct virtio_net_config_ll *ll_dev)
206 /* Free any malloc'd memory */
207 rte_free(ll_dev->dev.virtqueue[VIRTIO_RXQ]);
208 rte_free(ll_dev->dev.virtqueue[VIRTIO_TXQ]);
213 * Remove an entry from the device configuration linked list.
215 static struct virtio_net_config_ll *
216 rm_config_ll_entry(struct virtio_net_config_ll *ll_dev,
217 struct virtio_net_config_ll *ll_dev_last)
219 /* First remove the device and then clean it up. */
220 if (ll_dev == ll_root) {
221 ll_root = ll_dev->next;
222 cleanup_device(&ll_dev->dev);
226 if (likely(ll_dev_last != NULL)) {
227 ll_dev_last->next = ll_dev->next;
228 cleanup_device(&ll_dev->dev);
230 return ll_dev_last->next;
232 cleanup_device(&ll_dev->dev);
234 RTE_LOG(ERR, VHOST_CONFIG,
235 "Remove entry from config_ll failed\n");
242 * Initialise all variables in device structure.
245 init_device(struct virtio_net *dev)
250 * Virtqueues have already been malloced so
251 * we don't want to set them to NULL.
253 vq_offset = offsetof(struct virtio_net, mem);
255 /* Set everything to 0. */
256 memset((void *)(uintptr_t)((uint64_t)(uintptr_t)dev + vq_offset), 0,
257 (sizeof(struct virtio_net) - (size_t)vq_offset));
258 memset(dev->virtqueue[VIRTIO_RXQ], 0, sizeof(struct vhost_virtqueue));
259 memset(dev->virtqueue[VIRTIO_TXQ], 0, sizeof(struct vhost_virtqueue));
261 dev->virtqueue[VIRTIO_RXQ]->kickfd = (eventfd_t)-1;
262 dev->virtqueue[VIRTIO_RXQ]->callfd = (eventfd_t)-1;
263 dev->virtqueue[VIRTIO_TXQ]->kickfd = (eventfd_t)-1;
264 dev->virtqueue[VIRTIO_TXQ]->callfd = (eventfd_t)-1;
266 /* Backends are set to -1 indicating an inactive device. */
267 dev->virtqueue[VIRTIO_RXQ]->backend = VIRTIO_DEV_STOPPED;
268 dev->virtqueue[VIRTIO_TXQ]->backend = VIRTIO_DEV_STOPPED;
272 * Function is called from the CUSE open function. The device structure is
273 * initialised and a new entry is added to the device configuration linked
277 new_device(struct vhost_device_ctx ctx)
279 struct virtio_net_config_ll *new_ll_dev;
280 struct vhost_virtqueue *virtqueue_rx, *virtqueue_tx;
282 /* Setup device and virtqueues. */
283 new_ll_dev = rte_malloc(NULL, sizeof(struct virtio_net_config_ll), 0);
284 if (new_ll_dev == NULL) {
285 RTE_LOG(ERR, VHOST_CONFIG,
286 "(%"PRIu64") Failed to allocate memory for dev.\n",
291 virtqueue_rx = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
292 if (virtqueue_rx == NULL) {
293 rte_free(new_ll_dev);
294 RTE_LOG(ERR, VHOST_CONFIG,
295 "(%"PRIu64") Failed to allocate memory for rxq.\n",
300 virtqueue_tx = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
301 if (virtqueue_tx == NULL) {
302 rte_free(virtqueue_rx);
303 rte_free(new_ll_dev);
304 RTE_LOG(ERR, VHOST_CONFIG,
305 "(%"PRIu64") Failed to allocate memory for txq.\n",
310 new_ll_dev->dev.virtqueue[VIRTIO_RXQ] = virtqueue_rx;
311 new_ll_dev->dev.virtqueue[VIRTIO_TXQ] = virtqueue_tx;
313 /* Initialise device and virtqueues. */
314 init_device(&new_ll_dev->dev);
316 new_ll_dev->next = NULL;
318 /* Add entry to device configuration linked list. */
319 add_config_ll_entry(new_ll_dev);
321 return new_ll_dev->dev.device_fh;
325 * Function is called from the CUSE release function. This function will
326 * cleanup the device and remove it from device configuration linked list.
329 destroy_device(struct vhost_device_ctx ctx)
331 struct virtio_net_config_ll *ll_dev_cur_ctx, *ll_dev_last = NULL;
332 struct virtio_net_config_ll *ll_dev_cur = ll_root;
334 /* Find the linked list entry for the device to be removed. */
335 ll_dev_cur_ctx = get_config_ll_entry(ctx);
336 while (ll_dev_cur != NULL) {
338 * If the device is found or
339 * a device that doesn't exist is found then it is removed.
341 if (ll_dev_cur == ll_dev_cur_ctx) {
343 * If the device is running on a data core then call
344 * the function to remove it from the data core.
346 if ((ll_dev_cur->dev.flags & VIRTIO_DEV_RUNNING))
347 notify_ops->destroy_device(&(ll_dev_cur->dev));
348 ll_dev_cur = rm_config_ll_entry(ll_dev_cur,
351 ll_dev_last = ll_dev_cur;
352 ll_dev_cur = ll_dev_cur->next;
358 set_ifname(struct vhost_device_ctx ctx,
359 const char *if_name, unsigned int if_len)
361 struct virtio_net *dev;
364 dev = get_device(ctx);
368 len = if_len > sizeof(dev->ifname) ?
369 sizeof(dev->ifname) : if_len;
371 strncpy(dev->ifname, if_name, len);
376 * Called from CUSE IOCTL: VHOST_SET_OWNER
377 * This function just returns success at the moment unless
378 * the device hasn't been initialised.
381 set_owner(struct vhost_device_ctx ctx)
383 struct virtio_net *dev;
385 dev = get_device(ctx);
393 * Called from CUSE IOCTL: VHOST_RESET_OWNER
396 reset_owner(struct vhost_device_ctx ctx)
398 struct virtio_net_config_ll *ll_dev;
400 ll_dev = get_config_ll_entry(ctx);
402 cleanup_device(&ll_dev->dev);
403 init_device(&ll_dev->dev);
409 * Called from CUSE IOCTL: VHOST_GET_FEATURES
410 * The features that we support are requested.
413 get_features(struct vhost_device_ctx ctx, uint64_t *pu)
415 struct virtio_net *dev;
417 dev = get_device(ctx);
421 /* Send our supported features. */
422 *pu = VHOST_FEATURES;
427 * Called from CUSE IOCTL: VHOST_SET_FEATURES
428 * We receive the negotiated features supported by us and the virtio device.
431 set_features(struct vhost_device_ctx ctx, uint64_t *pu)
433 struct virtio_net *dev;
435 dev = get_device(ctx);
438 if (*pu & ~VHOST_FEATURES)
441 /* Store the negotiated feature list for the device. */
444 /* Set the vhost_hlen depending on if VIRTIO_NET_F_MRG_RXBUF is set. */
445 if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) {
446 LOG_DEBUG(VHOST_CONFIG,
447 "(%"PRIu64") Mergeable RX buffers enabled\n",
449 dev->virtqueue[VIRTIO_RXQ]->vhost_hlen =
450 sizeof(struct virtio_net_hdr_mrg_rxbuf);
451 dev->virtqueue[VIRTIO_TXQ]->vhost_hlen =
452 sizeof(struct virtio_net_hdr_mrg_rxbuf);
454 LOG_DEBUG(VHOST_CONFIG,
455 "(%"PRIu64") Mergeable RX buffers disabled\n",
457 dev->virtqueue[VIRTIO_RXQ]->vhost_hlen =
458 sizeof(struct virtio_net_hdr);
459 dev->virtqueue[VIRTIO_TXQ]->vhost_hlen =
460 sizeof(struct virtio_net_hdr);
466 * Called from CUSE IOCTL: VHOST_SET_VRING_NUM
467 * The virtio device sends us the size of the descriptor ring.
470 set_vring_num(struct vhost_device_ctx ctx, struct vhost_vring_state *state)
472 struct virtio_net *dev;
474 dev = get_device(ctx);
478 /* State->index refers to the queue index. The txq is 1, rxq is 0. */
479 dev->virtqueue[state->index]->size = state->num;
485 * Called from CUSE IOCTL: VHOST_SET_VRING_ADDR
486 * The virtio device sends us the desc, used and avail ring addresses.
487 * This function then converts these to our address space.
490 set_vring_addr(struct vhost_device_ctx ctx, struct vhost_vring_addr *addr)
492 struct virtio_net *dev;
493 struct vhost_virtqueue *vq;
495 dev = get_device(ctx);
499 /* addr->index refers to the queue index. The txq 1, rxq is 0. */
500 vq = dev->virtqueue[addr->index];
502 /* The addresses are converted from QEMU virtual to Vhost virtual. */
503 vq->desc = (struct vring_desc *)(uintptr_t)qva_to_vva(dev,
504 addr->desc_user_addr);
506 RTE_LOG(ERR, VHOST_CONFIG,
507 "(%"PRIu64") Failed to find desc ring address.\n",
512 vq->avail = (struct vring_avail *)(uintptr_t)qva_to_vva(dev,
513 addr->avail_user_addr);
514 if (vq->avail == 0) {
515 RTE_LOG(ERR, VHOST_CONFIG,
516 "(%"PRIu64") Failed to find avail ring address.\n",
521 vq->used = (struct vring_used *)(uintptr_t)qva_to_vva(dev,
522 addr->used_user_addr);
524 RTE_LOG(ERR, VHOST_CONFIG,
525 "(%"PRIu64") Failed to find used ring address.\n",
530 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address desc: %p\n",
531 dev->device_fh, vq->desc);
532 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address avail: %p\n",
533 dev->device_fh, vq->avail);
534 LOG_DEBUG(VHOST_CONFIG, "(%"PRIu64") mapped address used: %p\n",
535 dev->device_fh, vq->used);
541 * Called from CUSE IOCTL: VHOST_SET_VRING_BASE
542 * The virtio device sends us the available ring last used index.
545 set_vring_base(struct vhost_device_ctx ctx, struct vhost_vring_state *state)
547 struct virtio_net *dev;
549 dev = get_device(ctx);
553 /* State->index refers to the queue index. The txq is 1, rxq is 0. */
554 dev->virtqueue[state->index]->last_used_idx = state->num;
555 dev->virtqueue[state->index]->last_used_idx_res = state->num;
561 * Called from CUSE IOCTL: VHOST_GET_VRING_BASE
562 * We send the virtio device our available ring last used index.
565 get_vring_base(struct vhost_device_ctx ctx, uint32_t index,
566 struct vhost_vring_state *state)
568 struct virtio_net *dev;
570 dev = get_device(ctx);
574 state->index = index;
575 /* State->index refers to the queue index. The txq is 1, rxq is 0. */
576 state->num = dev->virtqueue[state->index]->last_used_idx;
583 * Called from CUSE IOCTL: VHOST_SET_VRING_CALL
584 * The virtio device sends an eventfd to interrupt the guest. This fd gets
585 * copied into our process space.
588 set_vring_call(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
590 struct virtio_net *dev;
591 struct vhost_virtqueue *vq;
593 dev = get_device(ctx);
597 /* file->index refers to the queue index. The txq is 1, rxq is 0. */
598 vq = dev->virtqueue[file->index];
600 if ((int)vq->callfd >= 0)
601 close((int)vq->callfd);
603 vq->callfd = file->fd;
609 * Called from CUSE IOCTL: VHOST_SET_VRING_KICK
610 * The virtio device sends an eventfd that it can use to notify us.
611 * This fd gets copied into our process space.
614 set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
616 struct virtio_net *dev;
617 struct vhost_virtqueue *vq;
619 dev = get_device(ctx);
623 /* file->index refers to the queue index. The txq is 1, rxq is 0. */
624 vq = dev->virtqueue[file->index];
626 if ((int)vq->kickfd >= 0)
627 close((int)vq->kickfd);
629 vq->kickfd = file->fd;
635 * Called from CUSE IOCTL: VHOST_NET_SET_BACKEND
636 * To complete device initialisation when the virtio driver is loaded,
637 * we are provided with a valid fd for a tap device (not used by us).
638 * If this happens then we can add the device to a data core.
639 * When the virtio driver is removed we get fd=-1.
640 * At that point we remove the device from the data core.
641 * The device will still exist in the device configuration linked list.
644 set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
646 struct virtio_net *dev;
648 dev = get_device(ctx);
652 /* file->index refers to the queue index. The txq is 1, rxq is 0. */
653 dev->virtqueue[file->index]->backend = file->fd;
656 * If the device isn't already running and both backend fds are set,
659 if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
660 if (((int)dev->virtqueue[VIRTIO_TXQ]->backend != VIRTIO_DEV_STOPPED) &&
661 ((int)dev->virtqueue[VIRTIO_RXQ]->backend != VIRTIO_DEV_STOPPED)) {
662 return notify_ops->new_device(dev);
664 /* Otherwise we remove it. */
666 if (file->fd == VIRTIO_DEV_STOPPED)
667 notify_ops->destroy_device(dev);
672 * Function pointers are set for the device operations to allow CUSE to call
673 * functions when an IOCTL, device_add or device_release is received.
675 static const struct vhost_net_device_ops vhost_device_ops = {
676 .new_device = new_device,
677 .destroy_device = destroy_device,
679 .set_ifname = set_ifname,
681 .get_features = get_features,
682 .set_features = set_features,
684 .set_vring_num = set_vring_num,
685 .set_vring_addr = set_vring_addr,
686 .set_vring_base = set_vring_base,
687 .get_vring_base = get_vring_base,
689 .set_vring_kick = set_vring_kick,
690 .set_vring_call = set_vring_call,
692 .set_backend = set_backend,
694 .set_owner = set_owner,
695 .reset_owner = reset_owner,
699 * Called by main to setup callbacks when registering CUSE device.
701 struct vhost_net_device_ops const *
702 get_virtio_net_callbacks(void)
704 return &vhost_device_ops;
707 int rte_vhost_enable_guest_notification(struct virtio_net *dev,
708 uint16_t queue_id, int enable)
711 RTE_LOG(ERR, VHOST_CONFIG,
712 "guest notification isn't supported.\n");
716 dev->virtqueue[queue_id]->used->flags =
717 enable ? 0 : VRING_USED_F_NO_NOTIFY;
721 uint64_t rte_vhost_feature_get(void)
723 return VHOST_FEATURES;
726 int rte_vhost_feature_disable(uint64_t feature_mask)
728 VHOST_FEATURES = VHOST_FEATURES & ~feature_mask;
732 int rte_vhost_feature_enable(uint64_t feature_mask)
734 if ((feature_mask & VHOST_SUPPORTED_FEATURES) == feature_mask) {
735 VHOST_FEATURES = VHOST_FEATURES | feature_mask;
742 * Register ops so that we can add/remove device to data core.
745 rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const ops)