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_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
50 /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
51 * firstly because vhost depends on this msg to allocate virtqueue
55 struct vhost_vring_file file;
57 /* May use invalid flag, but some backend leverages kickfd and callfd as
58 * criteria to judge if dev is alive. so finally we use real event_fd.
60 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
62 PMD_DRV_LOG(ERR, "callfd error, %s\n", strerror(errno));
65 file.index = queue_sel;
67 vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_CALL, &file);
68 dev->callfds[queue_sel] = callfd;
74 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
77 struct vhost_vring_file file;
78 struct vhost_vring_state state;
79 struct vring *vring = &dev->vrings[queue_sel];
80 struct vhost_vring_addr addr = {
82 .desc_user_addr = (uint64_t)(uintptr_t)vring->desc,
83 .avail_user_addr = (uint64_t)(uintptr_t)vring->avail,
84 .used_user_addr = (uint64_t)(uintptr_t)vring->used,
86 .flags = 0, /* disable log */
89 state.index = queue_sel;
90 state.num = vring->num;
91 vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_NUM, &state);
93 state.num = 0; /* no reservation */
94 vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_BASE, &state);
96 vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_ADDR, &addr);
98 /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
99 * lastly because vhost depends on this msg to judge if
102 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
104 PMD_DRV_LOG(ERR, "kickfd error, %s\n", strerror(errno));
107 file.index = queue_sel;
109 vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_KICK, &file);
110 dev->kickfds[queue_sel] = kickfd;
116 virtio_user_queue_setup(struct virtio_user_dev *dev,
117 int (*fn)(struct virtio_user_dev *, uint32_t))
119 uint32_t i, queue_sel;
121 for (i = 0; i < dev->max_queue_pairs; ++i) {
122 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
123 if (fn(dev, queue_sel) < 0) {
124 PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i);
128 for (i = 0; i < dev->max_queue_pairs; ++i) {
129 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
130 if (fn(dev, queue_sel) < 0) {
131 PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i);
140 virtio_user_start_device(struct virtio_user_dev *dev)
145 /* Step 0: tell vhost to create queues */
146 if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
149 /* Step 1: set features
150 * Make sure VHOST_USER_F_PROTOCOL_FEATURES is added if mq is enabled,
151 * VIRTIO_NET_F_MAC and VIRTIO_NET_F_CTRL_VQ is stripped.
153 features = dev->features;
154 if (dev->max_queue_pairs > 1)
155 features |= VHOST_USER_MQ;
156 features &= ~(1ull << VIRTIO_NET_F_MAC);
157 features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
158 ret = vhost_user_sock(dev->vhostfd, VHOST_USER_SET_FEATURES, &features);
161 PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
163 /* Step 2: share memory regions */
164 ret = vhost_user_sock(dev->vhostfd, VHOST_USER_SET_MEM_TABLE, NULL);
168 /* Step 3: kick queues */
169 if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0)
172 /* Step 4: enable queues
173 * we enable the 1st queue pair by default.
175 vhost_user_enable_queue_pair(dev->vhostfd, 0, 1);
179 /* TODO: free resource here or caller to check */
183 int virtio_user_stop_device(struct virtio_user_dev *dev)
185 return vhost_user_sock(dev->vhostfd, VHOST_USER_RESET_OWNER, NULL);
189 parse_mac(struct virtio_user_dev *dev, const char *mac)
192 uint32_t tmp[ETHER_ADDR_LEN];
197 r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
198 &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
199 if (r == ETHER_ADDR_LEN) {
200 for (i = 0; i < ETHER_ADDR_LEN; ++i)
201 dev->mac_addr[i] = (uint8_t)tmp[i];
202 dev->mac_specified = 1;
204 /* ignore the wrong mac, use random mac */
205 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
210 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
211 int cq, int queue_size, const char *mac)
213 snprintf(dev->path, PATH_MAX, "%s", path);
214 dev->max_queue_pairs = queues;
215 dev->queue_pairs = 1; /* mq disabled by default */
216 dev->queue_size = queue_size;
217 dev->mac_specified = 0;
221 dev->vhostfd = vhost_user_setup(dev->path);
222 if (dev->vhostfd < 0) {
223 PMD_INIT_LOG(ERR, "backend set up fails");
226 if (vhost_user_sock(dev->vhostfd, VHOST_USER_SET_OWNER, NULL) < 0) {
227 PMD_INIT_LOG(ERR, "set_owner fails: %s", strerror(errno));
231 if (vhost_user_sock(dev->vhostfd, VHOST_USER_GET_FEATURES,
232 &dev->device_features) < 0) {
233 PMD_INIT_LOG(ERR, "get_features failed: %s", strerror(errno));
236 if (dev->mac_specified)
237 dev->device_features |= (1ull << VIRTIO_NET_F_MAC);
240 /* device does not really need to know anything about CQ,
241 * so if necessary, we just claim to support CQ
243 dev->device_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
245 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
246 /* Also disable features depends on VIRTIO_NET_F_CTRL_VQ */
247 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
248 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VLAN);
249 dev->device_features &= ~(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
250 dev->device_features &= ~(1ull << VIRTIO_NET_F_MQ);
251 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
254 if (dev->max_queue_pairs > 1) {
255 if (!(dev->features & VHOST_USER_MQ)) {
256 PMD_INIT_LOG(ERR, "MQ not supported by the backend");
265 virtio_user_dev_uninit(struct virtio_user_dev *dev)
269 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
270 close(dev->callfds[i]);
271 close(dev->kickfds[i]);
278 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
283 if (q_pairs > dev->max_queue_pairs) {
284 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
285 q_pairs, dev->max_queue_pairs);
289 for (i = 0; i < q_pairs; ++i)
290 ret |= vhost_user_enable_queue_pair(dev->vhostfd, i, 1);
291 for (i = q_pairs; i < dev->max_queue_pairs; ++i)
292 ret |= vhost_user_enable_queue_pair(dev->vhostfd, i, 0);
294 dev->queue_pairs = q_pairs;
300 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
303 struct virtio_net_ctrl_hdr *hdr;
304 virtio_net_ctrl_ack status = ~0;
305 uint16_t i, idx_data, idx_status;
306 uint32_t n_descs = 0;
308 /* locate desc for header, data, and status */
309 idx_data = vring->desc[idx_hdr].next;
313 while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
314 i = vring->desc[i].next;
318 /* locate desc for status */
322 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
323 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
324 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
327 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
328 status = virtio_user_handle_mq(dev, queues);
332 *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
338 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
340 uint16_t avail_idx, desc_idx;
341 struct vring_used_elem *uep;
343 struct vring *vring = &dev->vrings[queue_idx];
345 /* Consume avail ring, using used ring idx as first one */
346 while (vring->used->idx != vring->avail->idx) {
347 avail_idx = (vring->used->idx) & (vring->num - 1);
348 desc_idx = vring->avail->ring[avail_idx];
350 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
352 /* Update used ring */
353 uep = &vring->used->ring[avail_idx];