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>
54 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
59 struct virtio_net *dev = vhost_devices[vid];
62 RTE_LOG(ERR, VHOST_CONFIG,
63 "(%d) device not found.\n", vid);
70 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
72 if ((vq->callfd >= 0) && (destroy != 0))
79 * Unmap any memory, close any file descriptors and
80 * free any memory owned by a device.
83 cleanup_device(struct virtio_net *dev, int destroy)
87 vhost_backend_cleanup(dev);
89 for (i = 0; i < dev->nr_vring; i++)
90 cleanup_vq(dev->virtqueue[i], destroy);
94 * Release virtqueues and device memory.
97 free_device(struct virtio_net *dev)
100 struct vhost_virtqueue *vq;
102 for (i = 0; i < dev->nr_vring; i++) {
103 vq = dev->virtqueue[i];
105 rte_free(vq->shadow_used_ring);
106 rte_free(vq->batch_copy_elems);
107 rte_mempool_free(vq->iotlb_pool);
115 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
117 struct vhost_virtqueue *vq;
119 if (vring_idx >= VHOST_MAX_VRING) {
120 RTE_LOG(ERR, VHOST_CONFIG,
121 "Failed not init vring, out of bound (%d)\n",
126 vq = dev->virtqueue[vring_idx];
128 memset(vq, 0, sizeof(struct vhost_virtqueue));
130 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
131 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
133 vhost_user_iotlb_init(dev, vring_idx);
134 /* Backends are set to -1 indicating an inactive device. */
138 * always set the vq to enabled; this is to keep compatibility
139 * with the old QEMU, whereas there is no SET_VRING_ENABLE message.
143 TAILQ_INIT(&vq->zmbuf_list);
147 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
149 struct vhost_virtqueue *vq;
152 if (vring_idx >= VHOST_MAX_VRING) {
153 RTE_LOG(ERR, VHOST_CONFIG,
154 "Failed not init vring, out of bound (%d)\n",
159 vq = dev->virtqueue[vring_idx];
161 init_vring_queue(dev, vring_idx);
166 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
168 struct vhost_virtqueue *vq;
170 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
172 RTE_LOG(ERR, VHOST_CONFIG,
173 "Failed to allocate memory for vring:%u.\n", vring_idx);
177 dev->virtqueue[vring_idx] = vq;
178 init_vring_queue(dev, vring_idx);
186 * Reset some variables in device structure, while keeping few
187 * others untouched, such as vid, ifname, nr_vring: they
188 * should be same unless the device is removed.
191 reset_device(struct virtio_net *dev)
196 dev->protocol_features = 0;
199 for (i = 0; i < dev->nr_vring; i++)
200 reset_vring_queue(dev, i);
204 * Invoked when there is a new vhost-user connection established (when
205 * there is a new virtio device being attached).
208 vhost_new_device(void)
210 struct virtio_net *dev;
213 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
215 RTE_LOG(ERR, VHOST_CONFIG,
216 "Failed to allocate memory for new dev.\n");
220 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
221 if (vhost_devices[i] == NULL)
224 if (i == MAX_VHOST_DEVICE) {
225 RTE_LOG(ERR, VHOST_CONFIG,
226 "Failed to find a free slot for new device.\n");
231 vhost_devices[i] = dev;
233 dev->slave_req_fd = -1;
239 * Invoked when there is the vhost-user connection is broken (when
240 * the virtio device is being detached).
243 vhost_destroy_device(int vid)
245 struct virtio_net *dev = get_device(vid);
250 if (dev->flags & VIRTIO_DEV_RUNNING) {
251 dev->flags &= ~VIRTIO_DEV_RUNNING;
252 dev->notify_ops->destroy_device(vid);
255 cleanup_device(dev, 1);
258 vhost_devices[vid] = NULL;
262 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
264 struct virtio_net *dev;
267 dev = get_device(vid);
271 len = if_len > sizeof(dev->ifname) ?
272 sizeof(dev->ifname) : if_len;
274 strncpy(dev->ifname, if_name, len);
275 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
279 vhost_enable_dequeue_zero_copy(int vid)
281 struct virtio_net *dev = get_device(vid);
286 dev->dequeue_zero_copy = 1;
290 rte_vhost_get_mtu(int vid, uint16_t *mtu)
292 struct virtio_net *dev = get_device(vid);
297 if (!(dev->flags & VIRTIO_DEV_READY))
300 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
309 rte_vhost_get_numa_node(int vid)
311 #ifdef RTE_LIBRTE_VHOST_NUMA
312 struct virtio_net *dev = get_device(vid);
319 ret = get_mempolicy(&numa_node, NULL, 0, dev,
320 MPOL_F_NODE | MPOL_F_ADDR);
322 RTE_LOG(ERR, VHOST_CONFIG,
323 "(%d) failed to query numa node: %s\n",
324 vid, rte_strerror(errno));
336 rte_vhost_get_queue_num(int vid)
338 struct virtio_net *dev = get_device(vid);
343 return dev->nr_vring / 2;
347 rte_vhost_get_vring_num(int vid)
349 struct virtio_net *dev = get_device(vid);
354 return dev->nr_vring;
358 rte_vhost_get_ifname(int vid, char *buf, size_t len)
360 struct virtio_net *dev = get_device(vid);
365 len = RTE_MIN(len, sizeof(dev->ifname));
367 strncpy(buf, dev->ifname, len);
374 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
376 struct virtio_net *dev;
378 dev = get_device(vid);
382 *features = dev->features;
387 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
389 struct virtio_net *dev;
390 struct rte_vhost_memory *m;
393 dev = get_device(vid);
397 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
398 m = malloc(sizeof(struct rte_vhost_memory) + size);
402 m->nregions = dev->mem->nregions;
403 memcpy(m->regions, dev->mem->regions, size);
410 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
411 struct rte_vhost_vring *vring)
413 struct virtio_net *dev;
414 struct vhost_virtqueue *vq;
416 dev = get_device(vid);
420 if (vring_idx >= VHOST_MAX_VRING)
423 vq = dev->virtqueue[vring_idx];
427 vring->desc = vq->desc;
428 vring->avail = vq->avail;
429 vring->used = vq->used;
430 vring->log_guest_addr = vq->log_guest_addr;
432 vring->callfd = vq->callfd;
433 vring->kickfd = vq->kickfd;
434 vring->size = vq->size;
440 rte_vhost_avail_entries(int vid, uint16_t queue_id)
442 struct virtio_net *dev;
443 struct vhost_virtqueue *vq;
445 dev = get_device(vid);
449 vq = dev->virtqueue[queue_id];
453 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
457 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
459 struct virtio_net *dev = get_device(vid);
465 RTE_LOG(ERR, VHOST_CONFIG,
466 "guest notification isn't supported.\n");
470 dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
475 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
477 struct virtio_net *dev = get_device(vid);
482 vhost_log_write(dev, addr, len);
486 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
487 uint64_t offset, uint64_t len)
489 struct virtio_net *dev;
490 struct vhost_virtqueue *vq;
492 dev = get_device(vid);
496 if (vring_idx >= VHOST_MAX_VRING)
498 vq = dev->virtqueue[vring_idx];
502 vhost_log_used_vring(dev, vq, offset, len);
506 rte_vhost_rx_queue_count(int vid, uint16_t qid)
508 struct virtio_net *dev;
509 struct vhost_virtqueue *vq;
511 dev = get_device(vid);
515 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
516 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
517 dev->vid, __func__, qid);
521 vq = dev->virtqueue[qid];
525 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
528 return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;