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, O_CLOEXEC | O_NONBLOCK);
68 PMD_DRV_LOG(ERR, "callfd error, %s\n", strerror(errno));
71 kickfd = eventfd(0, O_CLOEXEC | O_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
135 * these features can be set into each virtqueue in vhost side.
136 * And before that, make sure VIRTIO_NET_F_MAC is stripped.
138 features = dev->features;
139 features &= ~(1ull << VIRTIO_NET_F_MAC);
140 ret = vhost_user_sock(dev->vhostfd, VHOST_USER_SET_FEATURES, &features);
143 PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
147 /* TODO: free resource here or caller to check */
151 int virtio_user_stop_device(struct virtio_user_dev *dev)
153 return vhost_user_sock(dev->vhostfd, VHOST_USER_RESET_OWNER, NULL);
157 parse_mac(struct virtio_user_dev *dev, const char *mac)
160 uint32_t tmp[ETHER_ADDR_LEN];
165 r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
166 &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
167 if (r == ETHER_ADDR_LEN) {
168 for (i = 0; i < ETHER_ADDR_LEN; ++i)
169 dev->mac_addr[i] = (uint8_t)tmp[i];
170 dev->mac_specified = 1;
172 /* ignore the wrong mac, use random mac */
173 PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
178 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
179 int cq, int queue_size, const char *mac)
181 strncpy(dev->path, path, PATH_MAX);
182 dev->max_queue_pairs = queues;
183 dev->queue_pairs = 1; /* mq disabled by default */
184 dev->queue_size = queue_size;
185 dev->mac_specified = 0;
191 dev->vhostfd = vhost_user_setup(dev->path);
192 if (dev->vhostfd < 0) {
193 PMD_INIT_LOG(ERR, "backend set up fails");
196 if (vhost_user_sock(dev->vhostfd, VHOST_USER_SET_OWNER, NULL) < 0) {
197 PMD_INIT_LOG(ERR, "set_owner fails: %s", strerror(errno));
201 if (vhost_user_sock(dev->vhostfd, VHOST_USER_GET_FEATURES,
202 &dev->features) < 0) {
203 PMD_INIT_LOG(ERR, "get_features failed: %s", strerror(errno));
206 if (dev->mac_specified)
207 dev->features |= (1ull << VIRTIO_NET_F_MAC);
208 /* disable it until we support CQ */
209 dev->features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
210 dev->features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
216 virtio_user_dev_uninit(struct virtio_user_dev *dev)
220 for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
221 close(dev->callfds[i]);
222 close(dev->kickfds[i]);