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];
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->virt_qp_nb; i++) {
88 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ], destroy);
89 cleanup_vq(dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ], destroy);
94 * Release virtqueues and device memory.
97 free_device(struct virtio_net *dev)
100 struct vhost_virtqueue *rxq, *txq;
102 for (i = 0; i < dev->virt_qp_nb; i++) {
103 rxq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ];
104 txq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ];
106 rte_free(rxq->shadow_used_ring);
107 rte_free(txq->shadow_used_ring);
109 /* rxq and txq are allocated together as queue-pair */
117 init_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
119 memset(vq, 0, sizeof(struct vhost_virtqueue));
121 vq->kickfd = VIRTIO_UNINITIALIZED_EVENTFD;
122 vq->callfd = VIRTIO_UNINITIALIZED_EVENTFD;
124 /* Backends are set to -1 indicating an inactive device. */
127 /* always set the default vq pair to enabled */
131 TAILQ_INIT(&vq->zmbuf_list);
135 init_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
137 uint32_t base_idx = qp_idx * VIRTIO_QNUM;
139 init_vring_queue(dev->virtqueue[base_idx + VIRTIO_RXQ], qp_idx);
140 init_vring_queue(dev->virtqueue[base_idx + VIRTIO_TXQ], qp_idx);
144 reset_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
149 init_vring_queue(vq, qp_idx);
154 reset_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
156 uint32_t base_idx = qp_idx * VIRTIO_QNUM;
158 reset_vring_queue(dev->virtqueue[base_idx + VIRTIO_RXQ], qp_idx);
159 reset_vring_queue(dev->virtqueue[base_idx + VIRTIO_TXQ], qp_idx);
163 alloc_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx)
165 struct vhost_virtqueue *virtqueue = NULL;
166 uint32_t virt_rx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_RXQ;
167 uint32_t virt_tx_q_idx = qp_idx * VIRTIO_QNUM + VIRTIO_TXQ;
169 virtqueue = rte_malloc(NULL,
170 sizeof(struct vhost_virtqueue) * VIRTIO_QNUM, 0);
171 if (virtqueue == NULL) {
172 RTE_LOG(ERR, VHOST_CONFIG,
173 "Failed to allocate memory for virt qp:%d.\n", qp_idx);
177 dev->virtqueue[virt_rx_q_idx] = virtqueue;
178 dev->virtqueue[virt_tx_q_idx] = virtqueue + VIRTIO_TXQ;
180 init_vring_queue_pair(dev, qp_idx);
182 dev->virt_qp_nb += 1;
188 * Reset some variables in device structure, while keeping few
189 * others untouched, such as vid, ifname, virt_qp_nb: they
190 * should be same unless the device is removed.
193 reset_device(struct virtio_net *dev)
198 dev->protocol_features = 0;
201 for (i = 0; i < dev->virt_qp_nb; i++)
202 reset_vring_queue_pair(dev, i);
206 * Invoked when there is a new vhost-user connection established (when
207 * there is a new virtio device being attached).
210 vhost_new_device(void)
212 struct virtio_net *dev;
215 dev = rte_zmalloc(NULL, sizeof(struct virtio_net), 0);
217 RTE_LOG(ERR, VHOST_CONFIG,
218 "Failed to allocate memory for new dev.\n");
222 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
223 if (vhost_devices[i] == NULL)
226 if (i == MAX_VHOST_DEVICE) {
227 RTE_LOG(ERR, VHOST_CONFIG,
228 "Failed to find a free slot for new device.\n");
233 vhost_devices[i] = dev;
240 * Invoked when there is the vhost-user connection is broken (when
241 * the virtio device is being detached).
244 vhost_destroy_device(int vid)
246 struct virtio_net *dev = get_device(vid);
251 if (dev->flags & VIRTIO_DEV_RUNNING) {
252 dev->flags &= ~VIRTIO_DEV_RUNNING;
253 dev->notify_ops->destroy_device(vid);
256 cleanup_device(dev, 1);
259 vhost_devices[vid] = NULL;
263 vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
265 struct virtio_net *dev;
268 dev = get_device(vid);
272 len = if_len > sizeof(dev->ifname) ?
273 sizeof(dev->ifname) : if_len;
275 strncpy(dev->ifname, if_name, len);
276 dev->ifname[sizeof(dev->ifname) - 1] = '\0';
280 vhost_enable_dequeue_zero_copy(int vid)
282 struct virtio_net *dev = get_device(vid);
287 dev->dequeue_zero_copy = 1;
291 rte_vhost_get_mtu(int vid, uint16_t *mtu)
293 struct virtio_net *dev = get_device(vid);
298 if (!(dev->flags & VIRTIO_DEV_READY))
301 if (!(dev->features & VIRTIO_NET_F_MTU))
310 rte_vhost_get_numa_node(int vid)
312 #ifdef RTE_LIBRTE_VHOST_NUMA
313 struct virtio_net *dev = get_device(vid);
320 ret = get_mempolicy(&numa_node, NULL, 0, dev,
321 MPOL_F_NODE | MPOL_F_ADDR);
323 RTE_LOG(ERR, VHOST_CONFIG,
324 "(%d) failed to query numa node: %d\n", vid, ret);
336 rte_vhost_get_queue_num(int vid)
338 struct virtio_net *dev = get_device(vid);
343 return dev->virt_qp_nb;
347 rte_vhost_get_ifname(int vid, char *buf, size_t len)
349 struct virtio_net *dev = get_device(vid);
354 len = RTE_MIN(len, sizeof(dev->ifname));
356 strncpy(buf, dev->ifname, len);
363 rte_vhost_avail_entries(int vid, uint16_t queue_id)
365 struct virtio_net *dev;
366 struct vhost_virtqueue *vq;
368 dev = get_device(vid);
372 vq = dev->virtqueue[queue_id];
376 return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
380 rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
382 struct virtio_net *dev = get_device(vid);
388 RTE_LOG(ERR, VHOST_CONFIG,
389 "guest notification isn't supported.\n");
393 dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;