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.
41 #include <sys/eventfd.h>
44 #include "virtio_user_dev.h"
45 #include "../virtio_ethdev.h"
48 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
51 struct vhost_vring_file file;
52 struct vhost_vring_state state;
53 struct vring *vring = &dev->vrings[queue_sel];
54 struct vhost_vring_addr addr = {
56 .desc_user_addr = (uint64_t)(uintptr_t)vring->desc,
57 .avail_user_addr = (uint64_t)(uintptr_t)vring->avail,
58 .used_user_addr = (uint64_t)(uintptr_t)vring->used,
60 .flags = 0, /* disable log */
63 /* May use invalid flag, but some backend leverages kickfd and callfd as
64 * criteria to judge if dev is alive. so finally we use real event_fd.
66 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
68 PMD_DRV_LOG(ERR, "callfd error, %s\n", strerror(errno));
71 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
74 PMD_DRV_LOG(ERR, "kickfd error, %s\n", strerror(errno));
78 /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
79 * firstly because vhost depends on this msg to allocate virtqueue
82 file.index = queue_sel;
84 vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_CALL, &file);
85 dev->callfds[queue_sel] = callfd;
87 state.index = queue_sel;
88 state.num = vring->num;
89 vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_NUM, &state);
91 state.num = 0; /* no reservation */
92 vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_BASE, &state);
94 vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_ADDR, &addr);
96 /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
97 * lastly because vhost depends on this msg to judge if
101 vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_KICK, &file);
102 dev->kickfds[queue_sel] = kickfd;
108 virtio_user_start_device(struct virtio_user_dev *dev)
111 uint32_t i, queue_sel;
114 /* construct memory region inside each implementation */
115 ret = vhost_user_sock(dev->vhostfd, VHOST_USER_SET_MEM_TABLE, NULL);
119 for (i = 0; i < dev->max_queue_pairs; ++i) {
120 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
121 if (virtio_user_kick_queue(dev, queue_sel) < 0) {
122 PMD_DRV_LOG(INFO, "kick rx vq fails: %u", i);
126 for (i = 0; i < dev->max_queue_pairs; ++i) {
127 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
128 if (virtio_user_kick_queue(dev, queue_sel) < 0) {
129 PMD_DRV_LOG(INFO, "kick tx vq fails: %u", i);
134 /* After setup all virtqueues, we need to set_features so that these
135 * features can be set into each virtqueue in vhost side. And before
136 * that, make sure VHOST_USER_F_PROTOCOL_FEATURES is added if mq is
137 * enabled, and VIRTIO_NET_F_MAC is stripped.
139 features = dev->features;
140 if (dev->max_queue_pairs > 1)
141 features |= VHOST_USER_MQ;
142 features &= ~(1ull << VIRTIO_NET_F_MAC);
143 ret = vhost_user_sock(dev->vhostfd, VHOST_USER_SET_FEATURES, &features);
146 PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
150 /* TODO: free resource here or caller to check */
154 int virtio_user_stop_device(struct virtio_user_dev *dev)
156 return vhost_user_sock(dev->vhostfd, VHOST_USER_RESET_OWNER, NULL);
160 parse_mac(struct virtio_user_dev *dev, const char *mac)
163 uint32_t tmp[ETHER_ADDR_LEN];
168 r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
169 &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
170 if (r == ETHER_ADDR_LEN) {
171 for (i = 0; i < ETHER_ADDR_LEN; ++i)
172 dev->mac_addr[i] = (uint8_t)tmp[i];
173 dev->mac_specified = 1;
175 /* ignore the wrong mac, use random mac */
176 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
181 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
182 int cq, int queue_size, const char *mac)
184 snprintf(dev->path, PATH_MAX, "%s", path);
185 dev->max_queue_pairs = queues;
186 dev->queue_pairs = 1; /* mq disabled by default */
187 dev->queue_size = queue_size;
188 dev->mac_specified = 0;
192 dev->vhostfd = vhost_user_setup(dev->path);
193 if (dev->vhostfd < 0) {
194 PMD_INIT_LOG(ERR, "backend set up fails");
197 if (vhost_user_sock(dev->vhostfd, VHOST_USER_SET_OWNER, NULL) < 0) {
198 PMD_INIT_LOG(ERR, "set_owner fails: %s", strerror(errno));
202 if (vhost_user_sock(dev->vhostfd, VHOST_USER_GET_FEATURES,
203 &dev->features) < 0) {
204 PMD_INIT_LOG(ERR, "get_features failed: %s", strerror(errno));
207 if (dev->mac_specified)
208 dev->features |= (1ull << VIRTIO_NET_F_MAC);
211 dev->features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
212 /* Also disable features depends on VIRTIO_NET_F_CTRL_VQ */
213 dev->features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
214 dev->features &= ~(1ull << VIRTIO_NET_F_CTRL_VLAN);
215 dev->features &= ~(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
216 dev->features &= ~(1ull << VIRTIO_NET_F_MQ);
217 dev->features &= ~(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
219 /* vhost user backend does not need to know ctrl-q, so
220 * actually we need add this bit into features. However,
221 * DPDK vhost-user does send features with this bit, so we
222 * check it instead of OR it for now.
224 if (!(dev->features & (1ull << VIRTIO_NET_F_CTRL_VQ)))
225 PMD_INIT_LOG(INFO, "vhost does not support ctrl-q");
228 if (dev->max_queue_pairs > 1) {
229 if (!(dev->features & VHOST_USER_MQ)) {
230 PMD_INIT_LOG(ERR, "MQ not supported by the backend");
239 virtio_user_dev_uninit(struct virtio_user_dev *dev)
243 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
244 close(dev->callfds[i]);
245 close(dev->kickfds[i]);
252 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
257 if (q_pairs > dev->max_queue_pairs) {
258 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
259 q_pairs, dev->max_queue_pairs);
263 for (i = 0; i < q_pairs; ++i)
264 ret |= vhost_user_enable_queue_pair(dev->vhostfd, i, 1);
265 for (i = q_pairs; i < dev->max_queue_pairs; ++i)
266 ret |= vhost_user_enable_queue_pair(dev->vhostfd, i, 0);
268 dev->queue_pairs = q_pairs;
274 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
277 struct virtio_net_ctrl_hdr *hdr;
278 virtio_net_ctrl_ack status = ~0;
279 uint16_t i, idx_data, idx_status;
280 uint32_t n_descs = 0;
282 /* locate desc for header, data, and status */
283 idx_data = vring->desc[idx_hdr].next;
287 while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
288 i = vring->desc[i].next;
292 /* locate desc for status */
296 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
297 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
298 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
301 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
302 status = virtio_user_handle_mq(dev, queues);
306 *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
312 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
314 uint16_t avail_idx, desc_idx;
315 struct vring_used_elem *uep;
317 struct vring *vring = &dev->vrings[queue_idx];
319 /* Consume avail ring, using used ring idx as first one */
320 while (vring->used->idx != vring->avail->idx) {
321 avail_idx = (vring->used->idx) & (vring->num - 1);
322 desc_idx = vring->avail->ring[avail_idx];
324 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
326 /* Update used ring */
327 uep = &vring->used->ring[avail_idx];