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"
57 #define MAX_VHOST_DEVICE 1024
58 static struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
60 /* device ops to add/remove device to/from data core. */
61 struct virtio_net_device_ops const *notify_ops;
63 #define VHOST_USER_F_PROTOCOL_FEATURES 30
65 /* Features supported by this lib. */
66 #define VHOST_SUPPORTED_FEATURES ((1ULL << VIRTIO_NET_F_MRG_RXBUF) | \
67 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
68 (1ULL << VIRTIO_NET_F_CTRL_RX) | \
69 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
70 (VHOST_SUPPORTS_MQ) | \
71 (1ULL << VIRTIO_F_VERSION_1) | \
72 (1ULL << VHOST_F_LOG_ALL) | \
73 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
74 (1ULL << VIRTIO_NET_F_HOST_TSO4) | \
75 (1ULL << VIRTIO_NET_F_HOST_TSO6) | \
76 (1ULL << VIRTIO_NET_F_CSUM) | \
77 (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
78 (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
79 (1ULL << VIRTIO_NET_F_GUEST_TSO6))
81 static uint64_t VHOST_FEATURES = VHOST_SUPPORTED_FEATURES;
85 * Converts QEMU virtual address to Vhost virtual address. This function is
86 * used to convert the ring addresses to our address space.
89 qva_to_vva(struct virtio_net *dev, uint64_t qemu_va)
91 struct virtio_memory_regions *region;
92 uint64_t vhost_va = 0;
93 uint32_t regionidx = 0;
95 /* Find the region where the address lives. */
96 for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
97 region = &dev->mem->regions[regionidx];
98 if ((qemu_va >= region->userspace_address) &&
99 (qemu_va <= region->userspace_address +
100 region->memory_size)) {
101 vhost_va = qemu_va + region->guest_phys_address +
102 region->address_offset -
103 region->userspace_address;
113 struct virtio_net *dev = vhost_devices[vid];
115 if (unlikely(!dev)) {
116 RTE_LOG(ERR, VHOST_CONFIG,
117 "(%d) device not found.\n", vid);
124 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
126 if ((vq->callfd >= 0) && (destroy != 0))
133 * Unmap any memory, close any file descriptors and
134 * free any memory owned by a device.
137 cleanup_device(struct virtio_net *dev, int destroy)
141 vhost_backend_cleanup(dev);
143 for (i = 0; i < dev->virt_qp_nb; i++) {
144 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ], destroy);
145 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ], destroy);
150 * Release virtqueues and device memory.
153 free_device(struct virtio_net *dev)
157 for (i = 0; i < dev->virt_qp_nb; i++)
158 rte_free(dev->virtqueue[i * VIRTIO_QNUM]);
164 init_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
166 memset(vq, 0, sizeof(struct vhost_virtqueue));
168 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
169 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
171 /* Backends are set to -1 indicating an inactive device. */
174 /* always set the default vq pair to enabled */
180 init_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
182 uint32_t base_idx = qp_idx * VIRTIO_QNUM;
184 init_vring_queue(dev->virtqueue[base_idx + VIRTIO_RXQ], qp_idx);
185 init_vring_queue(dev->virtqueue[base_idx + VIRTIO_TXQ], qp_idx);
189 reset_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
194 init_vring_queue(vq, qp_idx);
199 reset_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
201 uint32_t base_idx = qp_idx * VIRTIO_QNUM;
203 reset_vring_queue(dev->virtqueue[base_idx + VIRTIO_RXQ], qp_idx);
204 reset_vring_queue(dev->virtqueue[base_idx + VIRTIO_TXQ], qp_idx);
208 alloc_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
210 struct vhost_virtqueue *virtqueue = NULL;
211 uint32_t virt_rx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_RXQ;
212 uint32_t virt_tx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_TXQ;
214 virtqueue = rte_malloc(NULL,
215 sizeof(struct vhost_virtqueue) * VIRTIO_QNUM, 0);
216 if (virtqueue == NULL) {
217 RTE_LOG(ERR, VHOST_CONFIG,
218 "Failed to allocate memory for virt qp:%d.\n", qp_idx);
222 dev->virtqueue[virt_rx_q_idx] = virtqueue;
223 dev->virtqueue[virt_tx_q_idx] = virtqueue + VIRTIO_TXQ;
225 init_vring_queue_pair(dev, qp_idx);
227 dev->virt_qp_nb += 1;
233 * Reset some variables in device structure, while keeping few
234 * others untouched, such as vid, ifname, virt_qp_nb: they
235 * should be same unless the device is removed.
238 reset_device(struct virtio_net *dev)
243 dev->protocol_features = 0;
246 for (i = 0; i < dev->virt_qp_nb; i++)
247 reset_vring_queue_pair(dev, i);
251 * Function is called from the CUSE open function. The device structure is
252 * initialised and a new entry is added to the device configuration linked
256 vhost_new_device(void)
258 struct virtio_net *dev;
261 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
263 RTE_LOG(ERR, VHOST_CONFIG,
264 "Failed to allocate memory for new dev.\n");
268 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
269 if (vhost_devices[i] == NULL)
272 if (i == MAX_VHOST_DEVICE) {
273 RTE_LOG(ERR, VHOST_CONFIG,
274 "Failed to find a free slot for new device.\n");
278 vhost_devices[i] = dev;
285 * Function is called from the CUSE release function. This function will
286 * cleanup the device and remove it from device configuration linked list.
289 vhost_destroy_device(int vid)
291 struct virtio_net *dev = get_device(vid);
296 if (dev->flags & VIRTIO_DEV_RUNNING) {
297 dev->flags &= ~VIRTIO_DEV_RUNNING;
298 notify_ops->destroy_device(vid);
301 cleanup_device(dev, 1);
304 vhost_devices[vid] = NULL;
308 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
310 struct virtio_net *dev;
313 dev = get_device(vid);
317 len = if_len > sizeof(dev->ifname) ?
318 sizeof(dev->ifname) : if_len;
320 strncpy(dev->ifname, if_name, len);
321 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
326 * Called from CUSE IOCTL: VHOST_SET_OWNER
327 * This function just returns success at the moment unless
328 * the device hasn't been initialised.
331 vhost_set_owner(int vid)
333 struct virtio_net *dev;
335 dev = get_device(vid);
343 * Called from CUSE IOCTL: VHOST_RESET_OWNER
346 vhost_reset_owner(int vid)
348 struct virtio_net *dev;
350 dev = get_device(vid);
354 if (dev->flags & VIRTIO_DEV_RUNNING) {
355 dev->flags &= ~VIRTIO_DEV_RUNNING;
356 notify_ops->destroy_device(vid);
359 cleanup_device(dev, 0);
365 * Called from CUSE IOCTL: VHOST_GET_FEATURES
366 * The features that we support are requested.
369 vhost_get_features(int vid, uint64_t *pu)
371 struct virtio_net *dev;
373 dev = get_device(vid);
377 /* Send our supported features. */
378 *pu = VHOST_FEATURES;
383 * Called from CUSE IOCTL: VHOST_SET_FEATURES
384 * We receive the negotiated features supported by us and the virtio device.
387 vhost_set_features(int vid, uint64_t *pu)
389 struct virtio_net *dev;
393 dev = get_device(vid);
396 if (*pu & ~VHOST_FEATURES)
401 ((1 << VIRTIO_NET_F_MRG_RXBUF) | (1ULL << VIRTIO_F_VERSION_1))) {
402 vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
404 vhost_hlen = sizeof(struct virtio_net_hdr);
406 LOG_DEBUG(VHOST_CONFIG,
407 "(%d) mergeable RX buffers %s, virtio 1 %s\n",
409 (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
410 (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
412 for (i = 0; i < dev->virt_qp_nb; i++) {
413 uint16_t base_idx = i * VIRTIO_QNUM;
415 dev->virtqueue[base_idx + VIRTIO_RXQ]->vhost_hlen = vhost_hlen;
416 dev->virtqueue[base_idx + VIRTIO_TXQ]->vhost_hlen = vhost_hlen;
423 * Called from CUSE IOCTL: VHOST_SET_VRING_NUM
424 * The virtio device sends us the size of the descriptor ring.
427 vhost_set_vring_num(int vid, struct vhost_vring_state *state)
429 struct virtio_net *dev;
431 dev = get_device(vid);
435 /* State->index refers to the queue index. The txq is 1, rxq is 0. */
436 dev->virtqueue[state->index]->size = state->num;
442 * Reallocate virtio_dev and vhost_virtqueue data structure to make them on the
443 * same numa node as the memory of vring descriptor.
445 #ifdef RTE_LIBRTE_VHOST_NUMA
446 static struct virtio_net*
447 numa_realloc(struct virtio_net *dev, int index)
449 int oldnode, newnode;
450 struct virtio_net *old_dev;
451 struct vhost_virtqueue *old_vq, *vq;
455 * vq is allocated on pairs, we should try to do realloc
456 * on first queue of one queue pair only.
458 if (index % VIRTIO_QNUM != 0)
462 vq = old_vq = dev->virtqueue[index];
464 ret = get_mempolicy(&newnode, NULL, 0, old_vq->desc,
465 MPOL_F_NODE | MPOL_F_ADDR);
467 /* check if we need to reallocate vq */
468 ret |= get_mempolicy(&oldnode, NULL, 0, old_vq,
469 MPOL_F_NODE | MPOL_F_ADDR);
471 RTE_LOG(ERR, VHOST_CONFIG,
472 "Unable to get vq numa information.\n");
475 if (oldnode != newnode) {
476 RTE_LOG(INFO, VHOST_CONFIG,
477 "reallocate vq from %d to %d node\n", oldnode, newnode);
478 vq = rte_malloc_socket(NULL, sizeof(*vq) * VIRTIO_QNUM, 0,
483 memcpy(vq, old_vq, sizeof(*vq) * VIRTIO_QNUM);
487 /* check if we need to reallocate dev */
488 ret = get_mempolicy(&oldnode, NULL, 0, old_dev,
489 MPOL_F_NODE | MPOL_F_ADDR);
491 RTE_LOG(ERR, VHOST_CONFIG,
492 "Unable to get dev numa information.\n");
495 if (oldnode != newnode) {
496 RTE_LOG(INFO, VHOST_CONFIG,
497 "reallocate dev from %d to %d node\n",
499 dev = rte_malloc_socket(NULL, sizeof(*dev), 0, newnode);
505 memcpy(dev, old_dev, sizeof(*dev));
510 dev->virtqueue[index] = vq;
511 dev->virtqueue[index + 1] = vq + 1;
512 vhost_devices[dev->vid] = dev;
517 static struct virtio_net*
518 numa_realloc(struct virtio_net *dev, int index __rte_unused)
525 * Called from CUSE IOCTL: VHOST_SET_VRING_ADDR
526 * The virtio device sends us the desc, used and avail ring addresses.
527 * This function then converts these to our address space.
530 vhost_set_vring_addr(int vid, struct vhost_vring_addr *addr)
532 struct virtio_net *dev;
533 struct vhost_virtqueue *vq;
535 dev = get_device(vid);
536 if ((dev == NULL) || (dev->mem == NULL))
539 /* addr->index refers to the queue index. The txq 1, rxq is 0. */
540 vq = dev->virtqueue[addr->index];
542 /* The addresses are converted from QEMU virtual to Vhost virtual. */
543 vq->desc = (struct vring_desc *)(uintptr_t)qva_to_vva(dev,
544 addr->desc_user_addr);
546 RTE_LOG(ERR, VHOST_CONFIG,
547 "(%d) failed to find desc ring address.\n",
552 dev = numa_realloc(dev, addr->index);
553 vq = dev->virtqueue[addr->index];
555 vq->avail = (struct vring_avail *)(uintptr_t)qva_to_vva(dev,
556 addr->avail_user_addr);
557 if (vq->avail == 0) {
558 RTE_LOG(ERR, VHOST_CONFIG,
559 "(%d) failed to find avail ring address.\n",
564 vq->used = (struct vring_used *)(uintptr_t)qva_to_vva(dev,
565 addr->used_user_addr);
567 RTE_LOG(ERR, VHOST_CONFIG,
568 "(%d) failed to find used ring address.\n",
573 vq->log_guest_addr = addr->log_guest_addr;
575 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
577 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address avail: %p\n",
578 dev->vid, vq->avail);
579 LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address used: %p\n",
581 LOG_DEBUG(VHOST_CONFIG, "(%d) log_guest_addr: %" PRIx64 "\n",
582 dev->vid, vq->log_guest_addr);
588 * Called from CUSE IOCTL: VHOST_SET_VRING_BASE
589 * The virtio device sends us the available ring last used index.
592 vhost_set_vring_base(int vid, struct vhost_vring_state *state)
594 struct virtio_net *dev;
596 dev = get_device(vid);
600 /* State->index refers to the queue index. The txq is 1, rxq is 0. */
601 dev->virtqueue[state->index]->last_used_idx = state->num;
602 dev->virtqueue[state->index]->last_used_idx_res = state->num;
608 * Called from CUSE IOCTL: VHOST_GET_VRING_BASE
609 * We send the virtio device our available ring last used index.
612 vhost_get_vring_base(int vid, uint32_t index,
613 struct vhost_vring_state *state)
615 struct virtio_net *dev;
617 dev = get_device(vid);
621 state->index = index;
622 /* State->index refers to the queue index. The txq is 1, rxq is 0. */
623 state->num = dev->virtqueue[state->index]->last_used_idx;
630 * Called from CUSE IOCTL: VHOST_SET_VRING_CALL
631 * The virtio device sends an eventfd to interrupt the guest. This fd gets
632 * copied into our process space.
635 vhost_set_vring_call(int vid, struct vhost_vring_file *file)
637 struct virtio_net *dev;
638 struct vhost_virtqueue *vq;
639 uint32_t cur_qp_idx = file->index / VIRTIO_QNUM;
641 dev = get_device(vid);
646 * FIXME: VHOST_SET_VRING_CALL is the first per-vring message
647 * we get, so we do vring queue pair allocation here.
649 if (cur_qp_idx + 1 > dev->virt_qp_nb) {
650 if (alloc_vring_queue_pair(dev, cur_qp_idx) < 0)
654 /* file->index refers to the queue index. The txq is 1, rxq is 0. */
655 vq = dev->virtqueue[file->index];
661 vq->callfd = file->fd;
667 * Called from CUSE IOCTL: VHOST_SET_VRING_KICK
668 * The virtio device sends an eventfd that it can use to notify us.
669 * This fd gets copied into our process space.
672 vhost_set_vring_kick(int vid, struct vhost_vring_file *file)
674 struct virtio_net *dev;
675 struct vhost_virtqueue *vq;
677 dev = get_device(vid);
681 /* file->index refers to the queue index. The txq is 1, rxq is 0. */
682 vq = dev->virtqueue[file->index];
687 vq->kickfd = file->fd;
693 * Called from CUSE IOCTL: VHOST_NET_SET_BACKEND
694 * To complete device initialisation when the virtio driver is loaded,
695 * we are provided with a valid fd for a tap device (not used by us).
696 * If this happens then we can add the device to a data core.
697 * When the virtio driver is removed we get fd=-1.
698 * At that point we remove the device from the data core.
699 * The device will still exist in the device configuration linked list.
702 vhost_set_backend(int vid, struct vhost_vring_file *file)
704 struct virtio_net *dev;
706 dev = get_device(vid);
710 /* file->index refers to the queue index. The txq is 1, rxq is 0. */
711 dev->virtqueue[file->index]->backend = file->fd;
714 * If the device isn't already running and both backend fds are set,
717 if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
718 if (dev->virtqueue[VIRTIO_TXQ]->backend != VIRTIO_DEV_STOPPED &&
719 dev->virtqueue[VIRTIO_RXQ]->backend != VIRTIO_DEV_STOPPED) {
720 if (notify_ops->new_device(vid) < 0)
722 dev->flags |= VIRTIO_DEV_RUNNING;
724 } else if (file->fd == VIRTIO_DEV_STOPPED) {
725 dev->flags &= ~VIRTIO_DEV_RUNNING;
726 notify_ops->destroy_device(vid);
733 rte_vhost_get_numa_node(int vid)
735 #ifdef RTE_LIBRTE_VHOST_NUMA
736 struct virtio_net *dev = get_device(vid);
743 ret = get_mempolicy(&numa_node, NULL, 0, dev,
744 MPOL_F_NODE | MPOL_F_ADDR);
746 RTE_LOG(ERR, VHOST_CONFIG,
747 "(%d) failed to query numa node: %d\n", vid, ret);
759 rte_vhost_get_queue_num(int vid)
761 struct virtio_net *dev = get_device(vid);
766 return dev->virt_qp_nb;
770 rte_vhost_get_ifname(int vid, char *buf, size_t len)
772 struct virtio_net *dev = get_device(vid);
777 len = RTE_MIN(len, sizeof(dev->ifname));
779 strncpy(buf, dev->ifname, len);
786 rte_vhost_avail_entries(int vid, uint16_t queue_id)
788 struct virtio_net *dev;
789 struct vhost_virtqueue *vq;
791 dev = get_device(vid);
795 vq = dev->virtqueue[queue_id];
799 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx_res;
803 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
805 struct virtio_net *dev = get_device(vid);
811 RTE_LOG(ERR, VHOST_CONFIG,
812 "guest notification isn't supported.\n");
816 dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
820 uint64_t rte_vhost_feature_get(void)
822 return VHOST_FEATURES;
825 int rte_vhost_feature_disable(uint64_t feature_mask)
827 VHOST_FEATURES = VHOST_FEATURES & ~feature_mask;
831 int rte_vhost_feature_enable(uint64_t feature_mask)
833 if ((feature_mask & VHOST_SUPPORTED_FEATURES) == feature_mask) {
834 VHOST_FEATURES = VHOST_FEATURES | feature_mask;
841 * Register ops so that we can add/remove device to data core.
844 rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const ops)