4 * Copyright(c) 2010-2016 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>
39 #ifdef RTE_LIBRTE_VHOST_NUMA
43 #include <rte_errno.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_vhost.h>
53 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
58 struct virtio_net *dev = vhost_devices[vid];
61 RTE_LOG(ERR, VHOST_CONFIG,
62 "(%d) device not found.\n", vid);
69 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
71 if ((vq->callfd >= 0) && (destroy != 0))
78 * Unmap any memory, close any file descriptors and
79 * free any memory owned by a device.
82 cleanup_device(struct virtio_net *dev, int destroy)
86 vhost_backend_cleanup(dev);
88 for (i = 0; i < dev->nr_vring; i++)
89 cleanup_vq(dev->virtqueue[i], destroy);
93 * Release virtqueues and device memory.
96 free_device(struct virtio_net *dev)
99 struct vhost_virtqueue *vq;
101 for (i = 0; i < dev->nr_vring; i++) {
102 vq = dev->virtqueue[i];
104 rte_free(vq->shadow_used_ring);
113 init_vring_queue(struct vhost_virtqueue *vq)
115 memset(vq, 0, sizeof(struct vhost_virtqueue));
117 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
118 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
120 /* Backends are set to -1 indicating an inactive device. */
124 * always set the vq to enabled; this is to keep compatibility
125 * with the old QEMU, whereas there is no SET_VRING_ENABLE message.
129 TAILQ_INIT(&vq->zmbuf_list);
133 reset_vring_queue(struct vhost_virtqueue *vq)
138 init_vring_queue(vq);
143 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
145 struct vhost_virtqueue *vq;
147 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
149 RTE_LOG(ERR, VHOST_CONFIG,
150 "Failed to allocate memory for vring:%u.\n", vring_idx);
154 dev->virtqueue[vring_idx] = vq;
155 init_vring_queue(vq);
163 * Reset some variables in device structure, while keeping few
164 * others untouched, such as vid, ifname, nr_vring: they
165 * should be same unless the device is removed.
168 reset_device(struct virtio_net *dev)
173 dev->protocol_features = 0;
176 for (i = 0; i < dev->nr_vring; i++)
177 reset_vring_queue(dev->virtqueue[i]);
181 * Invoked when there is a new vhost-user connection established (when
182 * there is a new virtio device being attached).
185 vhost_new_device(void)
187 struct virtio_net *dev;
190 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
192 RTE_LOG(ERR, VHOST_CONFIG,
193 "Failed to allocate memory for new dev.\n");
197 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
198 if (vhost_devices[i] == NULL)
201 if (i == MAX_VHOST_DEVICE) {
202 RTE_LOG(ERR, VHOST_CONFIG,
203 "Failed to find a free slot for new device.\n");
208 vhost_devices[i] = dev;
215 * Invoked when there is the vhost-user connection is broken (when
216 * the virtio device is being detached).
219 vhost_destroy_device(int vid)
221 struct virtio_net *dev = get_device(vid);
226 if (dev->flags & VIRTIO_DEV_RUNNING) {
227 dev->flags &= ~VIRTIO_DEV_RUNNING;
228 dev->notify_ops->destroy_device(vid);
231 cleanup_device(dev, 1);
234 vhost_devices[vid] = NULL;
238 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
240 struct virtio_net *dev;
243 dev = get_device(vid);
247 len = if_len > sizeof(dev->ifname) ?
248 sizeof(dev->ifname) : if_len;
250 strncpy(dev->ifname, if_name, len);
251 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
255 vhost_enable_dequeue_zero_copy(int vid)
257 struct virtio_net *dev = get_device(vid);
262 dev->dequeue_zero_copy = 1;
266 rte_vhost_get_mtu(int vid, uint16_t *mtu)
268 struct virtio_net *dev = get_device(vid);
273 if (!(dev->flags & VIRTIO_DEV_READY))
276 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
285 rte_vhost_get_numa_node(int vid)
287 #ifdef RTE_LIBRTE_VHOST_NUMA
288 struct virtio_net *dev = get_device(vid);
295 ret = get_mempolicy(&numa_node, NULL, 0, dev,
296 MPOL_F_NODE | MPOL_F_ADDR);
298 RTE_LOG(ERR, VHOST_CONFIG,
299 "(%d) failed to query numa node: %s\n",
300 vid, rte_strerror(errno));
312 rte_vhost_get_queue_num(int vid)
314 struct virtio_net *dev = get_device(vid);
319 return dev->nr_vring / 2;
323 rte_vhost_get_vring_num(int vid)
325 struct virtio_net *dev = get_device(vid);
330 return dev->nr_vring;
334 rte_vhost_get_ifname(int vid, char *buf, size_t len)
336 struct virtio_net *dev = get_device(vid);
341 len = RTE_MIN(len, sizeof(dev->ifname));
343 strncpy(buf, dev->ifname, len);
350 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
352 struct virtio_net *dev;
354 dev = get_device(vid);
358 *features = dev->features;
363 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
365 struct virtio_net *dev;
366 struct rte_vhost_memory *m;
369 dev = get_device(vid);
373 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
374 m = malloc(sizeof(struct rte_vhost_memory) + size);
378 m->nregions = dev->mem->nregions;
379 memcpy(m->regions, dev->mem->regions, size);
386 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
387 struct rte_vhost_vring *vring)
389 struct virtio_net *dev;
390 struct vhost_virtqueue *vq;
392 dev = get_device(vid);
396 if (vring_idx >= VHOST_MAX_VRING)
399 vq = dev->virtqueue[vring_idx];
403 vring->desc = vq->desc;
404 vring->avail = vq->avail;
405 vring->used = vq->used;
406 vring->log_guest_addr = vq->log_guest_addr;
408 vring->callfd = vq->callfd;
409 vring->kickfd = vq->kickfd;
410 vring->size = vq->size;
416 rte_vhost_avail_entries(int vid, uint16_t queue_id)
418 struct virtio_net *dev;
419 struct vhost_virtqueue *vq;
421 dev = get_device(vid);
425 vq = dev->virtqueue[queue_id];
429 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
433 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
435 struct virtio_net *dev = get_device(vid);
441 RTE_LOG(ERR, VHOST_CONFIG,
442 "guest notification isn't supported.\n");
446 dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
451 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
453 struct virtio_net *dev = get_device(vid);
458 vhost_log_write(dev, addr, len);
462 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
463 uint64_t offset, uint64_t len)
465 struct virtio_net *dev;
466 struct vhost_virtqueue *vq;
468 dev = get_device(vid);
472 if (vring_idx >= VHOST_MAX_VRING)
474 vq = dev->virtqueue[vring_idx];
478 vhost_log_used_vring(dev, vq, offset, len);
482 rte_vhost_rx_queue_count(int vid, uint16_t qid)
484 struct virtio_net *dev;
485 struct vhost_virtqueue *vq;
487 dev = get_device(vid);
491 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
492 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
493 dev->vid, __func__, qid);
497 vq = dev->virtqueue[qid];
501 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
504 return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;