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_virtio_net.h>
52 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
54 /* device ops to add/remove device to/from data core. */
55 struct virtio_net_device_ops const *notify_ops;
60 struct virtio_net *dev = vhost_devices[vid];
63 RTE_LOG(ERR, VHOST_CONFIG,
64 "(%d) device not found.\n", vid);
71 cleanup_vq(struct vhost_virtqueue *vq, int destroy)
73 if ((vq->callfd >= 0) && (destroy != 0))
80 * Unmap any memory, close any file descriptors and
81 * free any memory owned by a device.
84 cleanup_device(struct virtio_net *dev, int destroy)
88 vhost_backend_cleanup(dev);
90 for (i = 0; i < dev->virt_qp_nb; i++) {
91 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ], destroy);
92 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ], destroy);
97 * Release virtqueues and device memory.
100 free_device(struct virtio_net *dev)
103 struct vhost_virtqueue *rxq, *txq;
105 for (i = 0; i < dev->virt_qp_nb; i++) {
106 rxq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ];
107 txq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ];
109 rte_free(rxq->shadow_used_ring);
110 rte_free(txq->shadow_used_ring);
112 /* rxq and txq are allocated together as queue-pair */
120 init_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
122 memset(vq, 0, sizeof(struct vhost_virtqueue));
124 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
125 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
127 /* Backends are set to -1 indicating an inactive device. */
130 /* always set the default vq pair to enabled */
134 TAILQ_INIT(&vq->zmbuf_list);
138 init_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
140 uint32_t base_idx = qp_idx * VIRTIO_QNUM;
142 init_vring_queue(dev->virtqueue[base_idx + VIRTIO_RXQ], qp_idx);
143 init_vring_queue(dev->virtqueue[base_idx + VIRTIO_TXQ], qp_idx);
147 reset_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
152 init_vring_queue(vq, qp_idx);
157 reset_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
159 uint32_t base_idx = qp_idx * VIRTIO_QNUM;
161 reset_vring_queue(dev->virtqueue[base_idx + VIRTIO_RXQ], qp_idx);
162 reset_vring_queue(dev->virtqueue[base_idx + VIRTIO_TXQ], qp_idx);
166 alloc_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
168 struct vhost_virtqueue *virtqueue = NULL;
169 uint32_t virt_rx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_RXQ;
170 uint32_t virt_tx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_TXQ;
172 virtqueue = rte_malloc(NULL,
173 sizeof(struct vhost_virtqueue) * VIRTIO_QNUM, 0);
174 if (virtqueue == NULL) {
175 RTE_LOG(ERR, VHOST_CONFIG,
176 "Failed to allocate memory for virt qp:%d.\n", qp_idx);
180 dev->virtqueue[virt_rx_q_idx] = virtqueue;
181 dev->virtqueue[virt_tx_q_idx] = virtqueue + VIRTIO_TXQ;
183 init_vring_queue_pair(dev, qp_idx);
185 dev->virt_qp_nb += 1;
191 * Reset some variables in device structure, while keeping few
192 * others untouched, such as vid, ifname, virt_qp_nb: they
193 * should be same unless the device is removed.
196 reset_device(struct virtio_net *dev)
201 dev->protocol_features = 0;
204 for (i = 0; i < dev->virt_qp_nb; i++)
205 reset_vring_queue_pair(dev, i);
209 * Invoked when there is a new vhost-user connection established (when
210 * there is a new virtio device being attached).
213 vhost_new_device(void)
215 struct virtio_net *dev;
218 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
220 RTE_LOG(ERR, VHOST_CONFIG,
221 "Failed to allocate memory for new dev.\n");
225 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
226 if (vhost_devices[i] == NULL)
229 if (i == MAX_VHOST_DEVICE) {
230 RTE_LOG(ERR, VHOST_CONFIG,
231 "Failed to find a free slot for new device.\n");
236 vhost_devices[i] = dev;
243 * Invoked when there is the vhost-user connection is broken (when
244 * the virtio device is being detached).
247 vhost_destroy_device(int vid)
249 struct virtio_net *dev = get_device(vid);
254 if (dev->flags & VIRTIO_DEV_RUNNING) {
255 dev->flags &= ~VIRTIO_DEV_RUNNING;
256 notify_ops->destroy_device(vid);
259 cleanup_device(dev, 1);
262 vhost_devices[vid] = NULL;
266 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
268 struct virtio_net *dev;
271 dev = get_device(vid);
275 len = if_len > sizeof(dev->ifname) ?
276 sizeof(dev->ifname) : if_len;
278 strncpy(dev->ifname, if_name, len);
279 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
283 vhost_enable_dequeue_zero_copy(int vid)
285 struct virtio_net *dev = get_device(vid);
290 dev->dequeue_zero_copy = 1;
294 rte_vhost_get_mtu(int vid, uint16_t *mtu)
296 struct virtio_net *dev = get_device(vid);
301 if (!(dev->flags & VIRTIO_DEV_READY))
304 if (!(dev->features & VIRTIO_NET_F_MTU))
313 rte_vhost_get_numa_node(int vid)
315 #ifdef RTE_LIBRTE_VHOST_NUMA
316 struct virtio_net *dev = get_device(vid);
323 ret = get_mempolicy(&numa_node, NULL, 0, dev,
324 MPOL_F_NODE | MPOL_F_ADDR);
326 RTE_LOG(ERR, VHOST_CONFIG,
327 "(%d) failed to query numa node: %d\n", vid, ret);
339 rte_vhost_get_queue_num(int vid)
341 struct virtio_net *dev = get_device(vid);
346 return dev->virt_qp_nb;
350 rte_vhost_get_ifname(int vid, char *buf, size_t len)
352 struct virtio_net *dev = get_device(vid);
357 len = RTE_MIN(len, sizeof(dev->ifname));
359 strncpy(buf, dev->ifname, len);
366 rte_vhost_avail_entries(int vid, uint16_t queue_id)
368 struct virtio_net *dev;
369 struct vhost_virtqueue *vq;
371 dev = get_device(vid);
375 vq = dev->virtqueue[queue_id];
379 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
383 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
385 struct virtio_net *dev = get_device(vid);
391 RTE_LOG(ERR, VHOST_CONFIG,
392 "guest notification isn't supported.\n");
396 dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
401 * Register ops so that we can add/remove device to data core.
404 rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const ops)