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;
114 struct virtio_net *dev = vhost_devices[vid];
116 if (unlikely(!dev)) {
117 RTE_LOG(ERR, VHOST_CONFIG,
118 "(%d) device not found.\n", vid);
125 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
127 if ((vq->callfd >= 0) && (destroy != 0))
134 * Unmap any memory, close any file descriptors and
135 * free any memory owned by a device.
138 cleanup_device(struct virtio_net *dev, int destroy)
142 vhost_backend_cleanup(dev);
144 for (i = 0; i < dev->virt_qp_nb; i++) {
145 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ], destroy);
146 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ], destroy);
151 * Release virtqueues and device memory.
154 free_device(struct virtio_net *dev)
158 for (i = 0; i < dev->virt_qp_nb; i++)
159 rte_free(dev->virtqueue[i * VIRTIO_QNUM]);
165 init_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
167 memset(vq, 0, sizeof(struct vhost_virtqueue));
169 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
170 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
172 /* Backends are set to -1 indicating an inactive device. */
175 /* always set the default vq pair to enabled */
181 init_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
183 uint32_t base_idx = qp_idx * VIRTIO_QNUM;
185 init_vring_queue(dev->virtqueue[base_idx + VIRTIO_RXQ], qp_idx);
186 init_vring_queue(dev->virtqueue[base_idx + VIRTIO_TXQ], qp_idx);
190 reset_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
195 init_vring_queue(vq, qp_idx);
200 reset_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
202 uint32_t base_idx = qp_idx * VIRTIO_QNUM;
204 reset_vring_queue(dev->virtqueue[base_idx + VIRTIO_RXQ], qp_idx);
205 reset_vring_queue(dev->virtqueue[base_idx + VIRTIO_TXQ], qp_idx);
209 alloc_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
211 struct vhost_virtqueue *virtqueue = NULL;
212 uint32_t virt_rx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_RXQ;
213 uint32_t virt_tx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_TXQ;
215 virtqueue = rte_malloc(NULL,
216 sizeof(struct vhost_virtqueue) * VIRTIO_QNUM, 0);
217 if (virtqueue == NULL) {
218 RTE_LOG(ERR, VHOST_CONFIG,
219 "Failed to allocate memory for virt qp:%d.\n", qp_idx);
223 dev->virtqueue[virt_rx_q_idx] = virtqueue;
224 dev->virtqueue[virt_tx_q_idx] = virtqueue + VIRTIO_TXQ;
226 init_vring_queue_pair(dev, qp_idx);
228 dev->virt_qp_nb += 1;
234 * Reset some variables in device structure, while keeping few
235 * others untouched, such as vid, ifname, virt_qp_nb: they
236 * should be same unless the device is removed.
239 reset_device(struct virtio_net *dev)
244 dev->protocol_features = 0;
247 for (i = 0; i < dev->virt_qp_nb; i++)
248 reset_vring_queue_pair(dev, i);
252 * Function is called from the CUSE open function. The device structure is
253 * initialised and a new entry is added to the device configuration linked
257 vhost_new_device(void)
259 struct virtio_net *dev;
262 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
264 RTE_LOG(ERR, VHOST_CONFIG,
265 "Failed to allocate memory for new dev.\n");
269 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
270 if (vhost_devices[i] == NULL)
273 if (i == MAX_VHOST_DEVICE) {
274 RTE_LOG(ERR, VHOST_CONFIG,
275 "Failed to find a free slot for new device.\n");
279 vhost_devices[i] = dev;
286 * Function is called from the CUSE release function. This function will
287 * cleanup the device and remove it from device configuration linked list.
290 vhost_destroy_device(int vid)
292 struct virtio_net *dev = get_device(vid);
297 if (dev->flags & VIRTIO_DEV_RUNNING) {
298 dev->flags &= ~VIRTIO_DEV_RUNNING;
299 notify_ops->destroy_device(dev);
302 cleanup_device(dev, 1);
305 vhost_devices[vid] = NULL;
309 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
311 struct virtio_net *dev;
314 dev = get_device(vid);
318 len = if_len > sizeof(dev->ifname) ?
319 sizeof(dev->ifname) : if_len;
321 strncpy(dev->ifname, if_name, len);
322 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
327 * Called from CUSE IOCTL: VHOST_SET_OWNER
328 * This function just returns success at the moment unless
329 * the device hasn't been initialised.
332 vhost_set_owner(int vid)
334 struct virtio_net *dev;
336 dev = get_device(vid);
344 * Called from CUSE IOCTL: VHOST_RESET_OWNER
347 vhost_reset_owner(int vid)
349 struct virtio_net *dev;
351 dev = get_device(vid);
355 if (dev->flags & VIRTIO_DEV_RUNNING) {
356 dev->flags &= ~VIRTIO_DEV_RUNNING;
357 notify_ops->destroy_device(dev);
360 cleanup_device(dev, 0);
366 * Called from CUSE IOCTL: VHOST_GET_FEATURES
367 * The features that we support are requested.
370 vhost_get_features(int vid, uint64_t *pu)
372 struct virtio_net *dev;
374 dev = get_device(vid);
378 /* Send our supported features. */
379 *pu = VHOST_FEATURES;
384 * Called from CUSE IOCTL: VHOST_SET_FEATURES
385 * We receive the negotiated features supported by us and the virtio device.
388 vhost_set_features(int vid, uint64_t *pu)
390 struct virtio_net *dev;
394 dev = get_device(vid);
397 if (*pu & ~VHOST_FEATURES)
402 ((1 << VIRTIO_NET_F_MRG_RXBUF) | (1ULL << VIRTIO_F_VERSION_1))) {
403 vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
405 vhost_hlen = sizeof(struct virtio_net_hdr);
407 LOG_DEBUG(VHOST_CONFIG,
408 "(%d) mergeable RX buffers %s, virtio 1 %s\n",
410 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
411 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
413 for (i = 0; i < dev->virt_qp_nb; i++) {
414 uint16_t base_idx = i * VIRTIO_QNUM;
416 dev->virtqueue[base_idx + VIRTIO_RXQ]->vhost_hlen = vhost_hlen;
417 dev->virtqueue[base_idx + VIRTIO_TXQ]->vhost_hlen = vhost_hlen;
424 * Called from CUSE IOCTL: VHOST_SET_VRING_NUM
425 * The virtio device sends us the size of the descriptor ring.
428 vhost_set_vring_num(int vid, struct vhost_vring_state *state)
430 struct virtio_net *dev;
432 dev = get_device(vid);
436 /* State->index refers to the queue index. The txq is 1, rxq is 0. */
437 dev->virtqueue[state->index]->size = state->num;
443 * Reallocate virtio_dev and vhost_virtqueue data structure to make them on the
444 * same numa node as the memory of vring descriptor.
446 #ifdef RTE_LIBRTE_VHOST_NUMA
447 static struct virtio_net*
448 numa_realloc(struct virtio_net *dev, int index)
450 int oldnode, newnode;
451 struct virtio_net *old_dev;
452 struct vhost_virtqueue *old_vq, *vq;
456 * vq is allocated on pairs, we should try to do realloc
457 * on first queue of one queue pair only.
459 if (index % VIRTIO_QNUM != 0)
463 vq = old_vq = dev->virtqueue[index];
465 ret = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
466 MPOL_F_NODE | MPOL_F_ADDR);
468 /* check if we need to reallocate vq */
469 ret |= get_mempolicy(&oldnode, NULL, 0, old_vq,
470 MPOL_F_NODE | MPOL_F_ADDR);
472 RTE_LOG(ERR, VHOST_CONFIG,
473 "Unable to get vq numa information.\n");
476 if (oldnode != newnode) {
477 RTE_LOG(INFO, VHOST_CONFIG,
478 "reallocate vq from %d to %d node\n", oldnode, newnode);
479 vq = rte_malloc_socket(NULL, sizeof(*vq) * VIRTIO_QNUM, 0,
484 memcpy(vq, old_vq, sizeof(*vq) * VIRTIO_QNUM);
488 /* check if we need to reallocate dev */
489 ret = get_mempolicy(&oldnode, NULL, 0, old_dev,
490 MPOL_F_NODE | MPOL_F_ADDR);
492 RTE_LOG(ERR, VHOST_CONFIG,
493 "Unable to get dev numa information.\n");
496 if (oldnode != newnode) {
497 RTE_LOG(INFO, VHOST_CONFIG,
498 "reallocate dev from %d to %d node\n",
500 dev = rte_malloc_socket(NULL, sizeof(*dev), 0, newnode);
506 memcpy(dev, old_dev, sizeof(*dev));
511 dev->virtqueue[index] = vq;
512 dev->virtqueue[index + 1] = vq + 1;
513 vhost_devices[dev->vid] = dev;
518 static struct virtio_net*
519 numa_realloc(struct virtio_net *dev, int index __rte_unused)
526 * Called from CUSE IOCTL: VHOST_SET_VRING_ADDR
527 * The virtio device sends us the desc, used and avail ring addresses.
528 * This function then converts these to our address space.
531 vhost_set_vring_addr(int vid, struct vhost_vring_addr *addr)
533 struct virtio_net *dev;
534 struct vhost_virtqueue *vq;
536 dev = get_device(vid);
537 if ((dev == NULL) || (dev->mem == NULL))
540 /* addr->index refers to the queue index. The txq 1, rxq is 0. */
541 vq = dev->virtqueue[addr->index];
543 /* The addresses are converted from QEMU virtual to Vhost virtual. */
544 vq->desc = (struct vring_desc *)(uintptr_t)qva_to_vva(dev,
545 addr->desc_user_addr);
547 RTE_LOG(ERR, VHOST_CONFIG,
548 "(%d) failed to find desc ring address.\n",
553 dev = numa_realloc(dev, addr->index);
554 vq = dev->virtqueue[addr->index];
556 vq->avail = (struct vring_avail *)(uintptr_t)qva_to_vva(dev,
557 addr->avail_user_addr);
558 if (vq->avail == 0) {
559 RTE_LOG(ERR, VHOST_CONFIG,
560 "(%d) failed to find avail ring address.\n",
565 vq->used = (struct vring_used *)(uintptr_t)qva_to_vva(dev,
566 addr->used_user_addr);
568 RTE_LOG(ERR, VHOST_CONFIG,
569 "(%d) failed to find used ring address.\n",
574 vq->log_guest_addr = addr->log_guest_addr;
576 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
578 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address avail: %p\n",
579 dev->vid, vq->avail);
580 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address used: %p\n",
582 LOG_DEBUG(VHOST_CONFIG, "(%d) log_guest_addr: %" PRIx64 "\n",
583 dev->vid, vq->log_guest_addr);
589 * Called from CUSE IOCTL: VHOST_SET_VRING_BASE
590 * The virtio device sends us the available ring last used index.
593 vhost_set_vring_base(int vid, struct vhost_vring_state *state)
595 struct virtio_net *dev;
597 dev = get_device(vid);
601 /* State->index refers to the queue index. The txq is 1, rxq is 0. */
602 dev->virtqueue[state->index]->last_used_idx = state->num;
603 dev->virtqueue[state->index]->last_used_idx_res = state->num;
609 * Called from CUSE IOCTL: VHOST_GET_VRING_BASE
610 * We send the virtio device our available ring last used index.
613 vhost_get_vring_base(int vid, uint32_t index,
614 struct vhost_vring_state *state)
616 struct virtio_net *dev;
618 dev = get_device(vid);
622 state->index = index;
623 /* State->index refers to the queue index. The txq is 1, rxq is 0. */
624 state->num = dev->virtqueue[state->index]->last_used_idx;
631 * Called from CUSE IOCTL: VHOST_SET_VRING_CALL
632 * The virtio device sends an eventfd to interrupt the guest. This fd gets
633 * copied into our process space.
636 vhost_set_vring_call(int vid, struct vhost_vring_file *file)
638 struct virtio_net *dev;
639 struct vhost_virtqueue *vq;
640 uint32_t cur_qp_idx = file->index / VIRTIO_QNUM;
642 dev = get_device(vid);
647 * FIXME: VHOST_SET_VRING_CALL is the first per-vring message
648 * we get, so we do vring queue pair allocation here.
650 if (cur_qp_idx + 1 > dev->virt_qp_nb) {
651 if (alloc_vring_queue_pair(dev, cur_qp_idx) < 0)
655 /* file->index refers to the queue index. The txq is 1, rxq is 0. */
656 vq = dev->virtqueue[file->index];
662 vq->callfd = file->fd;
668 * Called from CUSE IOCTL: VHOST_SET_VRING_KICK
669 * The virtio device sends an eventfd that it can use to notify us.
670 * This fd gets copied into our process space.
673 vhost_set_vring_kick(int vid, struct vhost_vring_file *file)
675 struct virtio_net *dev;
676 struct vhost_virtqueue *vq;
678 dev = get_device(vid);
682 /* file->index refers to the queue index. The txq is 1, rxq is 0. */
683 vq = dev->virtqueue[file->index];
688 vq->kickfd = file->fd;
694 * Called from CUSE IOCTL: VHOST_NET_SET_BACKEND
695 * To complete device initialisation when the virtio driver is loaded,
696 * we are provided with a valid fd for a tap device (not used by us).
697 * If this happens then we can add the device to a data core.
698 * When the virtio driver is removed we get fd=-1.
699 * At that point we remove the device from the data core.
700 * The device will still exist in the device configuration linked list.
703 vhost_set_backend(int vid, struct vhost_vring_file *file)
705 struct virtio_net *dev;
707 dev = get_device(vid);
711 /* file->index refers to the queue index. The txq is 1, rxq is 0. */
712 dev->virtqueue[file->index]->backend = file->fd;
715 * If the device isn't already running and both backend fds are set,
718 if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
719 if (dev->virtqueue[VIRTIO_TXQ]->backend != VIRTIO_DEV_STOPPED &&
720 dev->virtqueue[VIRTIO_RXQ]->backend != VIRTIO_DEV_STOPPED) {
721 if (notify_ops->new_device(dev) < 0)
723 dev->flags |= VIRTIO_DEV_RUNNING;
725 } else if (file->fd == VIRTIO_DEV_STOPPED) {
726 dev->flags &= ~VIRTIO_DEV_RUNNING;
727 notify_ops->destroy_device(dev);
734 rte_vhost_get_numa_node(int vid)
736 #ifdef RTE_LIBRTE_VHOST_NUMA
737 struct virtio_net *dev = get_device(vid);
744 ret = get_mempolicy(&numa_node, NULL, 0, dev,
745 MPOL_F_NODE | MPOL_F_ADDR);
747 RTE_LOG(ERR, VHOST_CONFIG,
748 "(%d) failed to query numa node: %d\n", vid, ret);
759 int rte_vhost_enable_guest_notification(struct virtio_net *dev,
760 uint16_t queue_id, int enable)
763 RTE_LOG(ERR, VHOST_CONFIG,
764 "guest notification isn't supported.\n");
768 dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
772 uint64_t rte_vhost_feature_get(void)
774 return VHOST_FEATURES;
777 int rte_vhost_feature_disable(uint64_t feature_mask)
779 VHOST_FEATURES = VHOST_FEATURES & ~feature_mask;
783 int rte_vhost_feature_enable(uint64_t feature_mask)
785 if ((feature_mask & VHOST_SUPPORTED_FEATURES) == feature_mask) {
786 VHOST_FEATURES = VHOST_FEATURES | feature_mask;
793 * Register ops so that we can add/remove device to data core.
796 rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const ops)