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 #ifdef RTE_LIBRTE_VHOST_NUMA
46 #include <sys/socket.h>
48 #include <rte_ethdev.h>
50 #include <rte_string_fns.h>
51 #include <rte_memory.h>
52 #include <rte_malloc.h>
53 #include <rte_virtio_net.h>
55 #include "vhost-net.h"
56 #include "virtio-net.h"
58 #define MAX_VHOST_DEVICE 1024
59 static struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
61 /* device ops to add/remove device to/from data core. */
62 struct virtio_net_device_ops const *notify_ops;
64 #define VHOST_USER_F_PROTOCOL_FEATURES 30
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 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
71 (VHOST_SUPPORTS_MQ) | \
72 (1ULL << VIRTIO_F_VERSION_1) | \
73 (1ULL << VHOST_F_LOG_ALL) | \
74 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
75 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
76 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
77 (1ULL << VIRTIO_NET_F_CSUM) | \
78 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
79 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
80 (1ULL << VIRTIO_NET_F_GUEST_TSO6))
82 static uint64_t VHOST_FEATURES = VHOST_SUPPORTED_FEATURES;
86 * Converts QEMU virtual address to Vhost virtual address. This function is
87 * used to convert the ring addresses to our address space.
90 qva_to_vva(struct virtio_net *dev, uint64_t qemu_va)
92 struct virtio_memory_regions *region;
93 uint64_t vhost_va = 0;
94 uint32_t regionidx = 0;
96 /* Find the region where the address lives. */
97 for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
98 region = &dev->mem->regions[regionidx];
99 if ((qemu_va >= region->userspace_address) &&
100 (qemu_va <= region->userspace_address +
101 region->memory_size)) {
102 vhost_va = qemu_va + region->guest_phys_address +
103 region->address_offset -
104 region->userspace_address;
113 get_device(struct vhost_device_ctx ctx)
115 struct virtio_net *dev = vhost_devices[ctx.fh];
117 if (unlikely(!dev)) {
118 RTE_LOG(ERR, VHOST_CONFIG,
119 "(%d) device not found.\n", ctx.fh);
126 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
128 if ((vq->callfd >= 0) && (destroy != 0))
135 * Unmap any memory, close any file descriptors and
136 * free any memory owned by a device.
139 cleanup_device(struct virtio_net *dev, int destroy)
143 vhost_backend_cleanup(dev);
145 for (i = 0; i < dev->virt_qp_nb; i++) {
146 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ], destroy);
147 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ], destroy);
152 * Release virtqueues and device memory.
155 free_device(struct virtio_net *dev)
159 for (i = 0; i < dev->virt_qp_nb; i++)
160 rte_free(dev->virtqueue[i * VIRTIO_QNUM]);
166 init_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
168 memset(vq, 0, sizeof(struct vhost_virtqueue));
170 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
171 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
173 /* Backends are set to -1 indicating an inactive device. */
176 /* always set the default vq pair to enabled */
182 init_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
184 uint32_t base_idx = qp_idx * VIRTIO_QNUM;
186 init_vring_queue(dev->virtqueue[base_idx + VIRTIO_RXQ], qp_idx);
187 init_vring_queue(dev->virtqueue[base_idx + VIRTIO_TXQ], qp_idx);
191 reset_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
196 init_vring_queue(vq, qp_idx);
201 reset_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
203 uint32_t base_idx = qp_idx * VIRTIO_QNUM;
205 reset_vring_queue(dev->virtqueue[base_idx + VIRTIO_RXQ], qp_idx);
206 reset_vring_queue(dev->virtqueue[base_idx + VIRTIO_TXQ], qp_idx);
210 alloc_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
212 struct vhost_virtqueue *virtqueue = NULL;
213 uint32_t virt_rx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_RXQ;
214 uint32_t virt_tx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_TXQ;
216 virtqueue = rte_malloc(NULL,
217 sizeof(struct vhost_virtqueue) * VIRTIO_QNUM, 0);
218 if (virtqueue == NULL) {
219 RTE_LOG(ERR, VHOST_CONFIG,
220 "Failed to allocate memory for virt qp:%d.\n", qp_idx);
224 dev->virtqueue[virt_rx_q_idx] = virtqueue;
225 dev->virtqueue[virt_tx_q_idx] = virtqueue + VIRTIO_TXQ;
227 init_vring_queue_pair(dev, qp_idx);
229 dev->virt_qp_nb += 1;
235 * Reset some variables in device structure, while keeping few
236 * others untouched, such as device_fh, ifname, virt_qp_nb: they
237 * should be same unless the device is removed.
240 reset_device(struct virtio_net *dev)
245 dev->protocol_features = 0;
248 for (i = 0; i < dev->virt_qp_nb; i++)
249 reset_vring_queue_pair(dev, i);
253 * Function is called from the CUSE open function. The device structure is
254 * initialised and a new entry is added to the device configuration linked
258 vhost_new_device(struct vhost_device_ctx ctx)
260 struct virtio_net *dev;
263 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
265 RTE_LOG(ERR, VHOST_CONFIG,
266 "(%d) failed to allocate memory for dev.\n", ctx.fh);
270 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
271 if (vhost_devices[i] == NULL)
274 if (i == MAX_VHOST_DEVICE) {
275 RTE_LOG(ERR, VHOST_CONFIG,
276 "Failed to find a free slot for new device.\n");
280 vhost_devices[i] = dev;
287 * Function is called from the CUSE release function. This function will
288 * cleanup the device and remove it from device configuration linked list.
291 vhost_destroy_device(struct vhost_device_ctx ctx)
293 struct virtio_net *dev = get_device(ctx);
298 if (dev->flags & VIRTIO_DEV_RUNNING) {
299 dev->flags &= ~VIRTIO_DEV_RUNNING;
300 notify_ops->destroy_device(dev);
303 cleanup_device(dev, 1);
306 vhost_devices[ctx.fh] = NULL;
310 vhost_set_ifname(struct vhost_device_ctx ctx,
311 const char *if_name, unsigned int if_len)
313 struct virtio_net *dev;
316 dev = get_device(ctx);
320 len = if_len > sizeof(dev->ifname) ?
321 sizeof(dev->ifname) : if_len;
323 strncpy(dev->ifname, if_name, len);
324 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
329 * Called from CUSE IOCTL: VHOST_SET_OWNER
330 * This function just returns success at the moment unless
331 * the device hasn't been initialised.
334 vhost_set_owner(struct vhost_device_ctx ctx)
336 struct virtio_net *dev;
338 dev = get_device(ctx);
346 * Called from CUSE IOCTL: VHOST_RESET_OWNER
349 vhost_reset_owner(struct vhost_device_ctx ctx)
351 struct virtio_net *dev;
353 dev = get_device(ctx);
357 if (dev->flags & VIRTIO_DEV_RUNNING) {
358 dev->flags &= ~VIRTIO_DEV_RUNNING;
359 notify_ops->destroy_device(dev);
362 cleanup_device(dev, 0);
368 * Called from CUSE IOCTL: VHOST_GET_FEATURES
369 * The features that we support are requested.
372 vhost_get_features(struct vhost_device_ctx ctx, uint64_t *pu)
374 struct virtio_net *dev;
376 dev = get_device(ctx);
380 /* Send our supported features. */
381 *pu = VHOST_FEATURES;
386 * Called from CUSE IOCTL: VHOST_SET_FEATURES
387 * We receive the negotiated features supported by us and the virtio device.
390 vhost_set_features(struct vhost_device_ctx ctx, uint64_t *pu)
392 struct virtio_net *dev;
396 dev = get_device(ctx);
399 if (*pu & ~VHOST_FEATURES)
404 ((1 << VIRTIO_NET_F_MRG_RXBUF) | (1ULL << VIRTIO_F_VERSION_1))) {
405 vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
407 vhost_hlen = sizeof(struct virtio_net_hdr);
409 LOG_DEBUG(VHOST_CONFIG,
410 "(%d) mergeable RX buffers %s, virtio 1 %s\n",
412 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
413 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
415 for (i = 0; i < dev->virt_qp_nb; i++) {
416 uint16_t base_idx = i * VIRTIO_QNUM;
418 dev->virtqueue[base_idx + VIRTIO_RXQ]->vhost_hlen = vhost_hlen;
419 dev->virtqueue[base_idx + VIRTIO_TXQ]->vhost_hlen = vhost_hlen;
426 * Called from CUSE IOCTL: VHOST_SET_VRING_NUM
427 * The virtio device sends us the size of the descriptor ring.
430 vhost_set_vring_num(struct vhost_device_ctx ctx,
431 struct vhost_vring_state *state)
433 struct virtio_net *dev;
435 dev = get_device(ctx);
439 /* State->index refers to the queue index. The txq is 1, rxq is 0. */
440 dev->virtqueue[state->index]->size = state->num;
446 * Reallocate virtio_dev and vhost_virtqueue data structure to make them on the
447 * same numa node as the memory of vring descriptor.
449 #ifdef RTE_LIBRTE_VHOST_NUMA
450 static struct virtio_net*
451 numa_realloc(struct virtio_net *dev, int index)
453 int oldnode, newnode;
454 struct virtio_net *old_dev;
455 struct vhost_virtqueue *old_vq, *vq;
459 * vq is allocated on pairs, we should try to do realloc
460 * on first queue of one queue pair only.
462 if (index % VIRTIO_QNUM != 0)
466 vq = old_vq = dev->virtqueue[index];
468 ret = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
469 MPOL_F_NODE | MPOL_F_ADDR);
471 /* check if we need to reallocate vq */
472 ret |= get_mempolicy(&oldnode, NULL, 0, old_vq,
473 MPOL_F_NODE | MPOL_F_ADDR);
475 RTE_LOG(ERR, VHOST_CONFIG,
476 "Unable to get vq numa information.\n");
479 if (oldnode != newnode) {
480 RTE_LOG(INFO, VHOST_CONFIG,
481 "reallocate vq from %d to %d node\n", oldnode, newnode);
482 vq = rte_malloc_socket(NULL, sizeof(*vq) * VIRTIO_QNUM, 0,
487 memcpy(vq, old_vq, sizeof(*vq) * VIRTIO_QNUM);
491 /* check if we need to reallocate dev */
492 ret = get_mempolicy(&oldnode, NULL, 0, old_dev,
493 MPOL_F_NODE | MPOL_F_ADDR);
495 RTE_LOG(ERR, VHOST_CONFIG,
496 "Unable to get dev numa information.\n");
499 if (oldnode != newnode) {
500 RTE_LOG(INFO, VHOST_CONFIG,
501 "reallocate dev from %d to %d node\n",
503 dev = rte_malloc_socket(NULL, sizeof(*dev), 0, newnode);
509 memcpy(dev, old_dev, sizeof(*dev));
514 dev->virtqueue[index] = vq;
515 dev->virtqueue[index + 1] = vq + 1;
516 vhost_devices[dev->device_fh] = dev;
521 static struct virtio_net*
522 numa_realloc(struct virtio_net *dev, int index __rte_unused)
529 * Called from CUSE IOCTL: VHOST_SET_VRING_ADDR
530 * The virtio device sends us the desc, used and avail ring addresses.
531 * This function then converts these to our address space.
534 vhost_set_vring_addr(struct vhost_device_ctx ctx, struct vhost_vring_addr *addr)
536 struct virtio_net *dev;
537 struct vhost_virtqueue *vq;
539 dev = get_device(ctx);
540 if ((dev == NULL) || (dev->mem == NULL))
543 /* addr->index refers to the queue index. The txq 1, rxq is 0. */
544 vq = dev->virtqueue[addr->index];
546 /* The addresses are converted from QEMU virtual to Vhost virtual. */
547 vq->desc = (struct vring_desc *)(uintptr_t)qva_to_vva(dev,
548 addr->desc_user_addr);
550 RTE_LOG(ERR, VHOST_CONFIG,
551 "(%d) failed to find desc ring address.\n",
556 dev = numa_realloc(dev, addr->index);
557 vq = dev->virtqueue[addr->index];
559 vq->avail = (struct vring_avail *)(uintptr_t)qva_to_vva(dev,
560 addr->avail_user_addr);
561 if (vq->avail == 0) {
562 RTE_LOG(ERR, VHOST_CONFIG,
563 "(%d) failed to find avail ring address.\n",
568 vq->used = (struct vring_used *)(uintptr_t)qva_to_vva(dev,
569 addr->used_user_addr);
571 RTE_LOG(ERR, VHOST_CONFIG,
572 "(%d) failed to find used ring address.\n",
577 vq->log_guest_addr = addr->log_guest_addr;
579 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
580 dev->device_fh, vq->desc);
581 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address avail: %p\n",
582 dev->device_fh, vq->avail);
583 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address used: %p\n",
584 dev->device_fh, vq->used);
585 LOG_DEBUG(VHOST_CONFIG, "(%d) log_guest_addr: %" PRIx64 "\n",
586 dev->device_fh, vq->log_guest_addr);
592 * Called from CUSE IOCTL: VHOST_SET_VRING_BASE
593 * The virtio device sends us the available ring last used index.
596 vhost_set_vring_base(struct vhost_device_ctx ctx,
597 struct vhost_vring_state *state)
599 struct virtio_net *dev;
601 dev = get_device(ctx);
605 /* State->index refers to the queue index. The txq is 1, rxq is 0. */
606 dev->virtqueue[state->index]->last_used_idx = state->num;
607 dev->virtqueue[state->index]->last_used_idx_res = state->num;
613 * Called from CUSE IOCTL: VHOST_GET_VRING_BASE
614 * We send the virtio device our available ring last used index.
617 vhost_get_vring_base(struct vhost_device_ctx ctx, uint32_t index,
618 struct vhost_vring_state *state)
620 struct virtio_net *dev;
622 dev = get_device(ctx);
626 state->index = index;
627 /* State->index refers to the queue index. The txq is 1, rxq is 0. */
628 state->num = dev->virtqueue[state->index]->last_used_idx;
635 * Called from CUSE IOCTL: VHOST_SET_VRING_CALL
636 * The virtio device sends an eventfd to interrupt the guest. This fd gets
637 * copied into our process space.
640 vhost_set_vring_call(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
642 struct virtio_net *dev;
643 struct vhost_virtqueue *vq;
644 uint32_t cur_qp_idx = file->index / VIRTIO_QNUM;
646 dev = get_device(ctx);
651 * FIXME: VHOST_SET_VRING_CALL is the first per-vring message
652 * we get, so we do vring queue pair allocation here.
654 if (cur_qp_idx + 1 > dev->virt_qp_nb) {
655 if (alloc_vring_queue_pair(dev, cur_qp_idx) < 0)
659 /* file->index refers to the queue index. The txq is 1, rxq is 0. */
660 vq = dev->virtqueue[file->index];
666 vq->callfd = file->fd;
672 * Called from CUSE IOCTL: VHOST_SET_VRING_KICK
673 * The virtio device sends an eventfd that it can use to notify us.
674 * This fd gets copied into our process space.
677 vhost_set_vring_kick(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
679 struct virtio_net *dev;
680 struct vhost_virtqueue *vq;
682 dev = get_device(ctx);
686 /* file->index refers to the queue index. The txq is 1, rxq is 0. */
687 vq = dev->virtqueue[file->index];
692 vq->kickfd = file->fd;
698 * Called from CUSE IOCTL: VHOST_NET_SET_BACKEND
699 * To complete device initialisation when the virtio driver is loaded,
700 * we are provided with a valid fd for a tap device (not used by us).
701 * If this happens then we can add the device to a data core.
702 * When the virtio driver is removed we get fd=-1.
703 * At that point we remove the device from the data core.
704 * The device will still exist in the device configuration linked list.
707 vhost_set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file *file)
709 struct virtio_net *dev;
711 dev = get_device(ctx);
715 /* file->index refers to the queue index. The txq is 1, rxq is 0. */
716 dev->virtqueue[file->index]->backend = file->fd;
719 * If the device isn't already running and both backend fds are set,
722 if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
723 if (dev->virtqueue[VIRTIO_TXQ]->backend != VIRTIO_DEV_STOPPED &&
724 dev->virtqueue[VIRTIO_RXQ]->backend != VIRTIO_DEV_STOPPED) {
725 if (notify_ops->new_device(dev) < 0)
727 dev->flags |= VIRTIO_DEV_RUNNING;
729 } else if (file->fd == VIRTIO_DEV_STOPPED) {
730 dev->flags &= ~VIRTIO_DEV_RUNNING;
731 notify_ops->destroy_device(dev);
737 int rte_vhost_enable_guest_notification(struct virtio_net *dev,
738 uint16_t queue_id, int enable)
741 RTE_LOG(ERR, VHOST_CONFIG,
742 "guest notification isn't supported.\n");
746 dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
750 uint64_t rte_vhost_feature_get(void)
752 return VHOST_FEATURES;
755 int rte_vhost_feature_disable(uint64_t feature_mask)
757 VHOST_FEATURES = VHOST_FEATURES & ~feature_mask;
761 int rte_vhost_feature_enable(uint64_t feature_mask)
763 if ((feature_mask & VHOST_SUPPORTED_FEATURES) == feature_mask) {
764 VHOST_FEATURES = VHOST_FEATURES | feature_mask;
771 * Register ops so that we can add/remove device to data core.
774 rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const ops)