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_ethdev.h>
45 #include <rte_string_fns.h>
46 #include <rte_memory.h>
47 #include <rte_malloc.h>
48 #include <rte_vhost.h>
52 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
57 struct virtio_net *dev = vhost_devices[vid];
60 RTE_LOG(ERR, VHOST_CONFIG,
61 "(%d) device not found.\n", vid);
68 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
70 if ((vq->callfd >= 0) && (destroy != 0))
77 * Unmap any memory, close any file descriptors and
78 * free any memory owned by a device.
81 cleanup_device(struct virtio_net *dev, int destroy)
85 vhost_backend_cleanup(dev);
87 for (i = 0; i < dev->nr_vring; i++)
88 cleanup_vq(dev->virtqueue[i], destroy);
92 * Release virtqueues and device memory.
95 free_device(struct virtio_net *dev)
98 struct vhost_virtqueue *vq;
100 for (i = 0; i < dev->nr_vring; i++) {
101 vq = dev->virtqueue[i];
103 rte_free(vq->shadow_used_ring);
112 init_vring_queue(struct vhost_virtqueue *vq)
114 memset(vq, 0, sizeof(struct vhost_virtqueue));
116 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
117 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
119 /* Backends are set to -1 indicating an inactive device. */
123 * always set the vq to enabled; this is to keep compatibility
124 * with the old QEMU, whereas there is no SET_VRING_ENABLE message.
128 TAILQ_INIT(&vq->zmbuf_list);
132 reset_vring_queue(struct vhost_virtqueue *vq)
137 init_vring_queue(vq);
142 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
144 struct vhost_virtqueue *vq;
146 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
148 RTE_LOG(ERR, VHOST_CONFIG,
149 "Failed to allocate memory for vring:%u.\n", vring_idx);
153 dev->virtqueue[vring_idx] = vq;
154 init_vring_queue(vq);
162 * Reset some variables in device structure, while keeping few
163 * others untouched, such as vid, ifname, nr_vring: they
164 * should be same unless the device is removed.
167 reset_device(struct virtio_net *dev)
172 dev->protocol_features = 0;
175 for (i = 0; i < dev->nr_vring; i++)
176 reset_vring_queue(dev->virtqueue[i]);
180 * Invoked when there is a new vhost-user connection established (when
181 * there is a new virtio device being attached).
184 vhost_new_device(void)
186 struct virtio_net *dev;
189 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
191 RTE_LOG(ERR, VHOST_CONFIG,
192 "Failed to allocate memory for new dev.\n");
196 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
197 if (vhost_devices[i] == NULL)
200 if (i == MAX_VHOST_DEVICE) {
201 RTE_LOG(ERR, VHOST_CONFIG,
202 "Failed to find a free slot for new device.\n");
207 vhost_devices[i] = dev;
214 * Invoked when there is the vhost-user connection is broken (when
215 * the virtio device is being detached).
218 vhost_destroy_device(int vid)
220 struct virtio_net *dev = get_device(vid);
225 if (dev->flags & VIRTIO_DEV_RUNNING) {
226 dev->flags &= ~VIRTIO_DEV_RUNNING;
227 dev->notify_ops->destroy_device(vid);
230 cleanup_device(dev, 1);
233 vhost_devices[vid] = NULL;
237 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
239 struct virtio_net *dev;
242 dev = get_device(vid);
246 len = if_len > sizeof(dev->ifname) ?
247 sizeof(dev->ifname) : if_len;
249 strncpy(dev->ifname, if_name, len);
250 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
254 vhost_enable_dequeue_zero_copy(int vid)
256 struct virtio_net *dev = get_device(vid);
261 dev->dequeue_zero_copy = 1;
265 rte_vhost_get_mtu(int vid, uint16_t *mtu)
267 struct virtio_net *dev = get_device(vid);
272 if (!(dev->flags & VIRTIO_DEV_READY))
275 if (!(dev->features & VIRTIO_NET_F_MTU))
284 rte_vhost_get_numa_node(int vid)
286 #ifdef RTE_LIBRTE_VHOST_NUMA
287 struct virtio_net *dev = get_device(vid);
294 ret = get_mempolicy(&numa_node, NULL, 0, dev,
295 MPOL_F_NODE | MPOL_F_ADDR);
297 RTE_LOG(ERR, VHOST_CONFIG,
298 "(%d) failed to query numa node: %d\n", vid, ret);
310 rte_vhost_get_queue_num(int vid)
312 struct virtio_net *dev = get_device(vid);
317 return dev->nr_vring / 2;
321 rte_vhost_get_vring_num(int vid)
323 struct virtio_net *dev = get_device(vid);
328 return dev->nr_vring;
332 rte_vhost_get_ifname(int vid, char *buf, size_t len)
334 struct virtio_net *dev = get_device(vid);
339 len = RTE_MIN(len, sizeof(dev->ifname));
341 strncpy(buf, dev->ifname, len);
348 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
350 struct virtio_net *dev;
352 dev = get_device(vid);
356 *features = dev->features;
361 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
363 struct virtio_net *dev;
364 struct rte_vhost_memory *m;
367 dev = get_device(vid);
371 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
376 m->nregions = dev->mem->nregions;
377 memcpy(m->regions, dev->mem->regions, size);
384 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
385 struct rte_vhost_vring *vring)
387 struct virtio_net *dev;
388 struct vhost_virtqueue *vq;
390 dev = get_device(vid);
394 if (vring_idx >= VHOST_MAX_VRING)
397 vq = dev->virtqueue[vring_idx];
401 vring->desc = vq->desc;
402 vring->avail = vq->avail;
403 vring->used = vq->used;
404 vring->log_guest_addr = vq->log_guest_addr;
406 vring->callfd = vq->callfd;
407 vring->kickfd = vq->kickfd;
408 vring->size = vq->size;
414 rte_vhost_avail_entries(int vid, uint16_t queue_id)
416 struct virtio_net *dev;
417 struct vhost_virtqueue *vq;
419 dev = get_device(vid);
423 vq = dev->virtqueue[queue_id];
427 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
431 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
433 struct virtio_net *dev = get_device(vid);
439 RTE_LOG(ERR, VHOST_CONFIG,
440 "guest notification isn't supported.\n");
444 dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
449 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
451 struct virtio_net *dev = get_device(vid);
456 vhost_log_write(dev, addr, len);
460 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
461 uint64_t offset, uint64_t len)
463 struct virtio_net *dev;
464 struct vhost_virtqueue *vq;
466 dev = get_device(vid);
470 if (vring_idx >= VHOST_MAX_VRING)
472 vq = dev->virtqueue[vring_idx];
476 vhost_log_used_vring(dev, vq, offset, len);