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);
105 rte_free(vq->batch_copy_elems);
106 rte_mempool_free(vq->iotlb_pool);
114 init_vring_queue(struct vhost_virtqueue *vq)
116 memset(vq, 0, sizeof(struct vhost_virtqueue));
118 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
119 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
121 /* Backends are set to -1 indicating an inactive device. */
125 * always set the vq to enabled; this is to keep compatibility
126 * with the old QEMU, whereas there is no SET_VRING_ENABLE message.
130 TAILQ_INIT(&vq->zmbuf_list);
134 reset_vring_queue(struct vhost_virtqueue *vq)
139 init_vring_queue(vq);
144 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
146 struct vhost_virtqueue *vq;
148 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
150 RTE_LOG(ERR, VHOST_CONFIG,
151 "Failed to allocate memory for vring:%u.\n", vring_idx);
155 dev->virtqueue[vring_idx] = vq;
156 init_vring_queue(vq);
164 * Reset some variables in device structure, while keeping few
165 * others untouched, such as vid, ifname, nr_vring: they
166 * should be same unless the device is removed.
169 reset_device(struct virtio_net *dev)
174 dev->protocol_features = 0;
177 for (i = 0; i < dev->nr_vring; i++)
178 reset_vring_queue(dev->virtqueue[i]);
182 * Invoked when there is a new vhost-user connection established (when
183 * there is a new virtio device being attached).
186 vhost_new_device(void)
188 struct virtio_net *dev;
191 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
193 RTE_LOG(ERR, VHOST_CONFIG,
194 "Failed to allocate memory for new dev.\n");
198 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
199 if (vhost_devices[i] == NULL)
202 if (i == MAX_VHOST_DEVICE) {
203 RTE_LOG(ERR, VHOST_CONFIG,
204 "Failed to find a free slot for new device.\n");
209 vhost_devices[i] = dev;
211 dev->slave_req_fd = -1;
217 * Invoked when there is the vhost-user connection is broken (when
218 * the virtio device is being detached).
221 vhost_destroy_device(int vid)
223 struct virtio_net *dev = get_device(vid);
228 if (dev->flags & VIRTIO_DEV_RUNNING) {
229 dev->flags &= ~VIRTIO_DEV_RUNNING;
230 dev->notify_ops->destroy_device(vid);
233 cleanup_device(dev, 1);
236 vhost_devices[vid] = NULL;
240 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
242 struct virtio_net *dev;
245 dev = get_device(vid);
249 len = if_len > sizeof(dev->ifname) ?
250 sizeof(dev->ifname) : if_len;
252 strncpy(dev->ifname, if_name, len);
253 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
257 vhost_enable_dequeue_zero_copy(int vid)
259 struct virtio_net *dev = get_device(vid);
264 dev->dequeue_zero_copy = 1;
268 rte_vhost_get_mtu(int vid, uint16_t *mtu)
270 struct virtio_net *dev = get_device(vid);
275 if (!(dev->flags & VIRTIO_DEV_READY))
278 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
287 rte_vhost_get_numa_node(int vid)
289 #ifdef RTE_LIBRTE_VHOST_NUMA
290 struct virtio_net *dev = get_device(vid);
297 ret = get_mempolicy(&numa_node, NULL, 0, dev,
298 MPOL_F_NODE | MPOL_F_ADDR);
300 RTE_LOG(ERR, VHOST_CONFIG,
301 "(%d) failed to query numa node: %s\n",
302 vid, rte_strerror(errno));
314 rte_vhost_get_queue_num(int vid)
316 struct virtio_net *dev = get_device(vid);
321 return dev->nr_vring / 2;
325 rte_vhost_get_vring_num(int vid)
327 struct virtio_net *dev = get_device(vid);
332 return dev->nr_vring;
336 rte_vhost_get_ifname(int vid, char *buf, size_t len)
338 struct virtio_net *dev = get_device(vid);
343 len = RTE_MIN(len, sizeof(dev->ifname));
345 strncpy(buf, dev->ifname, len);
352 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
354 struct virtio_net *dev;
356 dev = get_device(vid);
360 *features = dev->features;
365 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
367 struct virtio_net *dev;
368 struct rte_vhost_memory *m;
371 dev = get_device(vid);
375 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
376 m = malloc(sizeof(struct rte_vhost_memory) + size);
380 m->nregions = dev->mem->nregions;
381 memcpy(m->regions, dev->mem->regions, size);
388 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
389 struct rte_vhost_vring *vring)
391 struct virtio_net *dev;
392 struct vhost_virtqueue *vq;
394 dev = get_device(vid);
398 if (vring_idx >= VHOST_MAX_VRING)
401 vq = dev->virtqueue[vring_idx];
405 vring->desc = vq->desc;
406 vring->avail = vq->avail;
407 vring->used = vq->used;
408 vring->log_guest_addr = vq->log_guest_addr;
410 vring->callfd = vq->callfd;
411 vring->kickfd = vq->kickfd;
412 vring->size = vq->size;
418 rte_vhost_avail_entries(int vid, uint16_t queue_id)
420 struct virtio_net *dev;
421 struct vhost_virtqueue *vq;
423 dev = get_device(vid);
427 vq = dev->virtqueue[queue_id];
431 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
435 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
437 struct virtio_net *dev = get_device(vid);
443 RTE_LOG(ERR, VHOST_CONFIG,
444 "guest notification isn't supported.\n");
448 dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
453 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
455 struct virtio_net *dev = get_device(vid);
460 vhost_log_write(dev, addr, len);
464 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
465 uint64_t offset, uint64_t len)
467 struct virtio_net *dev;
468 struct vhost_virtqueue *vq;
470 dev = get_device(vid);
474 if (vring_idx >= VHOST_MAX_VRING)
476 vq = dev->virtqueue[vring_idx];
480 vhost_log_used_vring(dev, vq, offset, len);
484 rte_vhost_rx_queue_count(int vid, uint16_t qid)
486 struct virtio_net *dev;
487 struct vhost_virtqueue *vq;
489 dev = get_device(vid);
493 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
494 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
495 dev->vid, __func__, qid);
499 vq = dev->virtqueue[qid];
503 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
506 return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;