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>
42 #include <sys/types.h>
46 #include "virtio_user_dev.h"
47 #include "../virtio_ethdev.h"
50 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
52 /* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
53 * firstly because vhost depends on this msg to allocate virtqueue
56 struct vhost_vring_file file;
58 file.index = queue_sel;
59 file.fd = dev->callfds[queue_sel];
60 dev->ops->send_request(dev, VHOST_USER_SET_VRING_CALL, &file);
66 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
68 struct vhost_vring_file file;
69 struct vhost_vring_state state;
70 struct vring *vring = &dev->vrings[queue_sel];
71 struct vhost_vring_addr addr = {
73 .desc_user_addr = (uint64_t)(uintptr_t)vring->desc,
74 .avail_user_addr = (uint64_t)(uintptr_t)vring->avail,
75 .used_user_addr = (uint64_t)(uintptr_t)vring->used,
77 .flags = 0, /* disable log */
80 state.index = queue_sel;
81 state.num = vring->num;
82 dev->ops->send_request(dev, VHOST_USER_SET_VRING_NUM, &state);
84 state.index = queue_sel;
85 state.num = 0; /* no reservation */
86 dev->ops->send_request(dev, VHOST_USER_SET_VRING_BASE, &state);
88 dev->ops->send_request(dev, VHOST_USER_SET_VRING_ADDR, &addr);
90 /* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
91 * lastly because vhost depends on this msg to judge if
94 file.index = queue_sel;
95 file.fd = dev->kickfds[queue_sel];
96 dev->ops->send_request(dev, VHOST_USER_SET_VRING_KICK, &file);
102 virtio_user_queue_setup(struct virtio_user_dev *dev,
103 int (*fn)(struct virtio_user_dev *, uint32_t))
105 uint32_t i, queue_sel;
107 for (i = 0; i < dev->max_queue_pairs; ++i) {
108 queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
109 if (fn(dev, queue_sel) < 0) {
110 PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i);
114 for (i = 0; i < dev->max_queue_pairs; ++i) {
115 queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
116 if (fn(dev, queue_sel) < 0) {
117 PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i);
126 virtio_user_start_device(struct virtio_user_dev *dev)
131 /* Step 0: tell vhost to create queues */
132 if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
135 /* Step 1: set features */
136 features = dev->features;
137 /* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
138 features &= ~(1ull << VIRTIO_NET_F_MAC);
139 /* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
140 features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
141 features &= ~(1ull << VIRTIO_NET_F_STATUS);
142 ret = dev->ops->send_request(dev, VHOST_USER_SET_FEATURES, &features);
145 PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
147 /* Step 2: share memory regions */
148 ret = dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
152 /* Step 3: kick queues */
153 if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0)
156 /* Step 4: enable queues
157 * we enable the 1st queue pair by default.
159 dev->ops->enable_qp(dev, 0, 1);
163 /* TODO: free resource here or caller to check */
167 int virtio_user_stop_device(struct virtio_user_dev *dev)
169 struct vhost_vring_state state;
173 for (i = 0; i < dev->max_queue_pairs; ++i)
174 dev->ops->enable_qp(dev, i, 0);
176 /* Stop the backend. */
177 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
179 if (dev->ops->send_request(dev, VHOST_USER_GET_VRING_BASE,
181 PMD_DRV_LOG(ERR, "get_vring_base failed, index=%u\n",
193 parse_mac(struct virtio_user_dev *dev, const char *mac)
196 uint32_t tmp[ETHER_ADDR_LEN];
201 r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
202 &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
203 if (r == ETHER_ADDR_LEN) {
204 for (i = 0; i < ETHER_ADDR_LEN; ++i)
205 dev->mac_addr[i] = (uint8_t)tmp[i];
206 dev->mac_specified = 1;
208 /* ignore the wrong mac, use random mac */
209 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
214 is_vhost_user_by_type(const char *path)
218 if (stat(path, &sb) == -1)
221 return S_ISSOCK(sb.st_mode);
225 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
231 for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) {
232 if (i >= dev->max_queue_pairs * 2) {
233 dev->kickfds[i] = -1;
234 dev->callfds[i] = -1;
238 /* May use invalid flag, but some backend uses kickfd and
239 * callfd as criteria to judge if dev is alive. so finally we
242 callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
244 PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno));
247 kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
249 PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno));
252 dev->callfds[i] = callfd;
253 dev->kickfds[i] = kickfd;
256 if (i < VIRTIO_MAX_VIRTQUEUES) {
257 for (j = 0; j <= i; ++j) {
258 close(dev->callfds[j]);
259 close(dev->kickfds[j]);
269 virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
272 struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
274 if (!eth_dev->intr_handle) {
275 eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
276 if (!eth_dev->intr_handle) {
277 PMD_DRV_LOG(ERR, "fail to allocate intr_handle");
280 memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
283 for (i = 0; i < dev->max_queue_pairs; ++i)
284 eth_dev->intr_handle->efds[i] = dev->callfds[i];
285 eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
286 eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
287 eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
288 /* For virtio vdev, no need to read counter for clean */
289 eth_dev->intr_handle->efd_counter_size = 0;
290 if (dev->vhostfd >= 0)
291 eth_dev->intr_handle->fd = dev->vhostfd;
297 virtio_user_dev_setup(struct virtio_user_dev *dev)
302 dev->vhostfds = NULL;
305 if (is_vhost_user_by_type(dev->path)) {
306 dev->ops = &ops_user;
308 dev->ops = &ops_kernel;
310 dev->vhostfds = malloc(dev->max_queue_pairs * sizeof(int));
311 dev->tapfds = malloc(dev->max_queue_pairs * sizeof(int));
312 if (!dev->vhostfds || !dev->tapfds) {
313 PMD_INIT_LOG(ERR, "Failed to malloc");
317 for (q = 0; q < dev->max_queue_pairs; ++q) {
318 dev->vhostfds[q] = -1;
323 if (dev->ops->setup(dev) < 0)
326 if (virtio_user_dev_init_notify(dev) < 0)
329 if (virtio_user_fill_intr_handle(dev) < 0)
335 /* Use below macro to filter features from vhost backend */
336 #define VIRTIO_USER_SUPPORTED_FEATURES \
337 (1ULL << VIRTIO_NET_F_MAC | \
338 1ULL << VIRTIO_NET_F_STATUS | \
339 1ULL << VIRTIO_NET_F_MQ | \
340 1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR | \
341 1ULL << VIRTIO_NET_F_CTRL_VQ | \
342 1ULL << VIRTIO_NET_F_CTRL_RX | \
343 1ULL << VIRTIO_NET_F_CTRL_VLAN | \
344 1ULL << VIRTIO_NET_F_CSUM | \
345 1ULL << VIRTIO_NET_F_HOST_TSO4 | \
346 1ULL << VIRTIO_NET_F_HOST_TSO6 | \
347 1ULL << VIRTIO_NET_F_MRG_RXBUF | \
348 1ULL << VIRTIO_RING_F_INDIRECT_DESC | \
349 1ULL << VIRTIO_NET_F_GUEST_CSUM | \
350 1ULL << VIRTIO_NET_F_GUEST_TSO4 | \
351 1ULL << VIRTIO_NET_F_GUEST_TSO6 | \
352 1ULL << VIRTIO_F_VERSION_1)
355 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
356 int cq, int queue_size, const char *mac, char **ifname)
358 snprintf(dev->path, PATH_MAX, "%s", path);
359 dev->max_queue_pairs = queues;
360 dev->queue_pairs = 1; /* mq disabled by default */
361 dev->queue_size = queue_size;
362 dev->mac_specified = 0;
366 dev->ifname = *ifname;
370 if (virtio_user_dev_setup(dev) < 0) {
371 PMD_INIT_LOG(ERR, "backend set up fails");
374 if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER, NULL) < 0) {
375 PMD_INIT_LOG(ERR, "set_owner fails: %s", strerror(errno));
379 if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
380 &dev->device_features) < 0) {
381 PMD_INIT_LOG(ERR, "get_features failed: %s", strerror(errno));
384 if (dev->mac_specified)
385 dev->device_features |= (1ull << VIRTIO_NET_F_MAC);
388 /* device does not really need to know anything about CQ,
389 * so if necessary, we just claim to support CQ
391 dev->device_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
393 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
394 /* Also disable features depends on VIRTIO_NET_F_CTRL_VQ */
395 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
396 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VLAN);
397 dev->device_features &= ~(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
398 dev->device_features &= ~(1ull << VIRTIO_NET_F_MQ);
399 dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
402 /* The backend will not report this feature, we add it explicitly */
403 if (is_vhost_user_by_type(dev->path))
404 dev->device_features |= (1ull << VIRTIO_NET_F_STATUS);
406 dev->device_features &= VIRTIO_USER_SUPPORTED_FEATURES;
412 virtio_user_dev_uninit(struct virtio_user_dev *dev)
416 virtio_user_stop_device(dev);
418 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
419 close(dev->callfds[i]);
420 close(dev->kickfds[i]);
426 for (i = 0; i < dev->max_queue_pairs; ++i)
427 close(dev->vhostfds[i]);
436 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
441 if (q_pairs > dev->max_queue_pairs) {
442 PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
443 q_pairs, dev->max_queue_pairs);
447 for (i = 0; i < q_pairs; ++i)
448 ret |= dev->ops->enable_qp(dev, i, 1);
449 for (i = q_pairs; i < dev->max_queue_pairs; ++i)
450 ret |= dev->ops->enable_qp(dev, i, 0);
452 dev->queue_pairs = q_pairs;
458 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
461 struct virtio_net_ctrl_hdr *hdr;
462 virtio_net_ctrl_ack status = ~0;
463 uint16_t i, idx_data, idx_status;
464 uint32_t n_descs = 0;
466 /* locate desc for header, data, and status */
467 idx_data = vring->desc[idx_hdr].next;
471 while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
472 i = vring->desc[i].next;
476 /* locate desc for status */
480 hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
481 if (hdr->class == VIRTIO_NET_CTRL_MQ &&
482 hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
485 queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
486 status = virtio_user_handle_mq(dev, queues);
490 *(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
496 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
498 uint16_t avail_idx, desc_idx;
499 struct vring_used_elem *uep;
501 struct vring *vring = &dev->vrings[queue_idx];
503 /* Consume avail ring, using used ring idx as first one */
504 while (vring->used->idx != vring->avail->idx) {
505 avail_idx = (vring->used->idx) & (vring->num - 1);
506 desc_idx = vring->avail->ring[avail_idx];
508 n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
510 /* Update used ring */
511 uep = &vring->used->ring[avail_idx];