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>
50 #include <rte_rwlock.h>
54 #include "vhost_user.h"
56 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
59 __vhost_iova_to_vva(struct virtio_net *dev, struct vhost_virtqueue *vq,
60 uint64_t iova, uint64_t size, uint8_t perm)
62 uint64_t vva, tmp_size;
69 vva = vhost_user_iotlb_cache_find(vq, iova, &tmp_size, perm);
73 if (!vhost_user_iotlb_pending_miss(vq, iova + tmp_size, perm)) {
74 vhost_user_iotlb_pending_insert(vq, iova + tmp_size, perm);
75 vhost_user_iotlb_miss(dev, iova + tmp_size, perm);
84 struct virtio_net *dev = vhost_devices[vid];
87 RTE_LOG(ERR, VHOST_CONFIG,
88 "(%d) device not found.\n", vid);
95 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
97 if ((vq->callfd >= 0) && (destroy != 0))
104 * Unmap any memory, close any file descriptors and
105 * free any memory owned by a device.
108 cleanup_device(struct virtio_net *dev, int destroy)
112 vhost_backend_cleanup(dev);
114 for (i = 0; i < dev->nr_vring; i++)
115 cleanup_vq(dev->virtqueue[i], destroy);
119 * Release virtqueues and device memory.
122 free_device(struct virtio_net *dev)
125 struct vhost_virtqueue *vq;
127 for (i = 0; i < dev->nr_vring; i++) {
128 vq = dev->virtqueue[i];
130 rte_free(vq->shadow_used_ring);
131 rte_free(vq->batch_copy_elems);
132 rte_mempool_free(vq->iotlb_pool);
140 init_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
142 struct vhost_virtqueue *vq;
144 if (vring_idx >= VHOST_MAX_VRING) {
145 RTE_LOG(ERR, VHOST_CONFIG,
146 "Failed not init vring, out of bound (%d)\n",
151 vq = dev->virtqueue[vring_idx];
153 memset(vq, 0, sizeof(struct vhost_virtqueue));
155 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
156 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
158 vhost_user_iotlb_init(dev, vring_idx);
159 /* Backends are set to -1 indicating an inactive device. */
162 TAILQ_INIT(&vq->zmbuf_list);
166 reset_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
168 struct vhost_virtqueue *vq;
171 if (vring_idx >= VHOST_MAX_VRING) {
172 RTE_LOG(ERR, VHOST_CONFIG,
173 "Failed not init vring, out of bound (%d)\n",
178 vq = dev->virtqueue[vring_idx];
180 init_vring_queue(dev, vring_idx);
185 alloc_vring_queue(struct virtio_net *dev, uint32_t vring_idx)
187 struct vhost_virtqueue *vq;
189 vq = rte_malloc(NULL, sizeof(struct vhost_virtqueue), 0);
191 RTE_LOG(ERR, VHOST_CONFIG,
192 "Failed to allocate memory for vring:%u.\n", vring_idx);
196 dev->virtqueue[vring_idx] = vq;
197 init_vring_queue(dev, vring_idx);
205 * Reset some variables in device structure, while keeping few
206 * others untouched, such as vid, ifname, nr_vring: they
207 * should be same unless the device is removed.
210 reset_device(struct virtio_net *dev)
215 dev->protocol_features = 0;
218 for (i = 0; i < dev->nr_vring; i++)
219 reset_vring_queue(dev, i);
223 * Invoked when there is a new vhost-user connection established (when
224 * there is a new virtio device being attached).
227 vhost_new_device(void)
229 struct virtio_net *dev;
232 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
234 RTE_LOG(ERR, VHOST_CONFIG,
235 "Failed to allocate memory for new dev.\n");
239 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
240 if (vhost_devices[i] == NULL)
243 if (i == MAX_VHOST_DEVICE) {
244 RTE_LOG(ERR, VHOST_CONFIG,
245 "Failed to find a free slot for new device.\n");
250 vhost_devices[i] = dev;
252 dev->slave_req_fd = -1;
258 * Invoked when there is the vhost-user connection is broken (when
259 * the virtio device is being detached).
262 vhost_destroy_device(int vid)
264 struct virtio_net *dev = get_device(vid);
269 if (dev->flags & VIRTIO_DEV_RUNNING) {
270 dev->flags &= ~VIRTIO_DEV_RUNNING;
271 dev->notify_ops->destroy_device(vid);
274 cleanup_device(dev, 1);
277 vhost_devices[vid] = NULL;
281 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
283 struct virtio_net *dev;
286 dev = get_device(vid);
290 len = if_len > sizeof(dev->ifname) ?
291 sizeof(dev->ifname) : if_len;
293 strncpy(dev->ifname, if_name, len);
294 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
298 vhost_enable_dequeue_zero_copy(int vid)
300 struct virtio_net *dev = get_device(vid);
305 dev->dequeue_zero_copy = 1;
309 rte_vhost_get_mtu(int vid, uint16_t *mtu)
311 struct virtio_net *dev = get_device(vid);
316 if (!(dev->flags & VIRTIO_DEV_READY))
319 if (!(dev->features & (1ULL << VIRTIO_NET_F_MTU)))
328 rte_vhost_get_numa_node(int vid)
330 #ifdef RTE_LIBRTE_VHOST_NUMA
331 struct virtio_net *dev = get_device(vid);
338 ret = get_mempolicy(&numa_node, NULL, 0, dev,
339 MPOL_F_NODE | MPOL_F_ADDR);
341 RTE_LOG(ERR, VHOST_CONFIG,
342 "(%d) failed to query numa node: %s\n",
343 vid, rte_strerror(errno));
355 rte_vhost_get_queue_num(int vid)
357 struct virtio_net *dev = get_device(vid);
362 return dev->nr_vring / 2;
366 rte_vhost_get_vring_num(int vid)
368 struct virtio_net *dev = get_device(vid);
373 return dev->nr_vring;
377 rte_vhost_get_ifname(int vid, char *buf, size_t len)
379 struct virtio_net *dev = get_device(vid);
384 len = RTE_MIN(len, sizeof(dev->ifname));
386 strncpy(buf, dev->ifname, len);
393 rte_vhost_get_negotiated_features(int vid, uint64_t *features)
395 struct virtio_net *dev;
397 dev = get_device(vid);
401 *features = dev->features;
406 rte_vhost_get_mem_table(int vid, struct rte_vhost_memory **mem)
408 struct virtio_net *dev;
409 struct rte_vhost_memory *m;
412 dev = get_device(vid);
416 size = dev->mem->nregions * sizeof(struct rte_vhost_mem_region);
417 m = malloc(sizeof(struct rte_vhost_memory) + size);
421 m->nregions = dev->mem->nregions;
422 memcpy(m->regions, dev->mem->regions, size);
429 rte_vhost_get_vhost_vring(int vid, uint16_t vring_idx,
430 struct rte_vhost_vring *vring)
432 struct virtio_net *dev;
433 struct vhost_virtqueue *vq;
435 dev = get_device(vid);
439 if (vring_idx >= VHOST_MAX_VRING)
442 vq = dev->virtqueue[vring_idx];
446 vring->desc = vq->desc;
447 vring->avail = vq->avail;
448 vring->used = vq->used;
449 vring->log_guest_addr = vq->log_guest_addr;
451 vring->callfd = vq->callfd;
452 vring->kickfd = vq->kickfd;
453 vring->size = vq->size;
459 rte_vhost_avail_entries(int vid, uint16_t queue_id)
461 struct virtio_net *dev;
462 struct vhost_virtqueue *vq;
464 dev = get_device(vid);
468 vq = dev->virtqueue[queue_id];
472 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
476 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
478 struct virtio_net *dev = get_device(vid);
484 RTE_LOG(ERR, VHOST_CONFIG,
485 "guest notification isn't supported.\n");
489 dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
494 rte_vhost_log_write(int vid, uint64_t addr, uint64_t len)
496 struct virtio_net *dev = get_device(vid);
501 vhost_log_write(dev, addr, len);
505 rte_vhost_log_used_vring(int vid, uint16_t vring_idx,
506 uint64_t offset, uint64_t len)
508 struct virtio_net *dev;
509 struct vhost_virtqueue *vq;
511 dev = get_device(vid);
515 if (vring_idx >= VHOST_MAX_VRING)
517 vq = dev->virtqueue[vring_idx];
521 vhost_log_used_vring(dev, vq, offset, len);
525 rte_vhost_rx_queue_count(int vid, uint16_t qid)
527 struct virtio_net *dev;
528 struct vhost_virtqueue *vq;
530 dev = get_device(vid);
534 if (unlikely(qid >= dev->nr_vring || (qid & 1) == 0)) {
535 RTE_LOG(ERR, VHOST_DATA, "(%d) %s: invalid virtqueue idx %d.\n",
536 dev->vid, __func__, qid);
540 vq = dev->virtqueue[qid];
544 if (unlikely(vq->enabled == 0 || vq->avail == NULL))
547 return *((volatile uint16_t *)&vq->avail->idx) - vq->last_avail_idx;