xref: /dpdk/drivers/net/virtio/virtio_user/virtio_user_dev.c (revision 2f45703c17acb943aaded9f79676fd56a72542b2)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33 
34 #include <stdint.h>
35 #include <stdio.h>
36 #include <fcntl.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <sys/mman.h>
40 #include <unistd.h>
41 #include <sys/eventfd.h>
42 
43 #include "vhost.h"
44 #include "virtio_user_dev.h"
45 #include "../virtio_ethdev.h"
46 
47 static int
48 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
49 {
50 	int callfd, kickfd;
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 = {
55 		.index = queue_sel,
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,
59 		.log_guest_addr = 0,
60 		.flags = 0, /* disable log */
61 	};
62 
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.
65 	 */
66 	callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
67 	if (callfd < 0) {
68 		PMD_DRV_LOG(ERR, "callfd error, %s\n", strerror(errno));
69 		return -1;
70 	}
71 	kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
72 	if (kickfd < 0) {
73 		close(callfd);
74 		PMD_DRV_LOG(ERR, "kickfd error, %s\n", strerror(errno));
75 		return -1;
76 	}
77 
78 	/* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
79 	 * firstly because vhost depends on this msg to allocate virtqueue
80 	 * pair.
81 	 */
82 	file.index = queue_sel;
83 	file.fd = callfd;
84 	vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_CALL, &file);
85 	dev->callfds[queue_sel] = callfd;
86 
87 	state.index = queue_sel;
88 	state.num = vring->num;
89 	vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_NUM, &state);
90 
91 	state.num = 0; /* no reservation */
92 	vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_BASE, &state);
93 
94 	vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_ADDR, &addr);
95 
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
98 	 * virtio is ready.
99 	 */
100 	file.fd = kickfd;
101 	vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_KICK, &file);
102 	dev->kickfds[queue_sel] = kickfd;
103 
104 	return 0;
105 }
106 
107 int
108 virtio_user_start_device(struct virtio_user_dev *dev)
109 {
110 	uint64_t features;
111 	uint32_t i, queue_sel;
112 	int ret;
113 
114 	/* construct memory region inside each implementation */
115 	ret = vhost_user_sock(dev->vhostfd, VHOST_USER_SET_MEM_TABLE, NULL);
116 	if (ret < 0)
117 		goto error;
118 
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);
123 			goto error;
124 		}
125 	}
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);
130 			goto error;
131 		}
132 	}
133 
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.
138 	 */
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);
144 	if (ret < 0)
145 		goto error;
146 	PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
147 
148 	return 0;
149 error:
150 	/* TODO: free resource here or caller to check */
151 	return -1;
152 }
153 
154 int virtio_user_stop_device(struct virtio_user_dev *dev)
155 {
156 	return vhost_user_sock(dev->vhostfd, VHOST_USER_RESET_OWNER, NULL);
157 }
158 
159 static inline void
160 parse_mac(struct virtio_user_dev *dev, const char *mac)
161 {
162 	int i, r;
163 	uint32_t tmp[ETHER_ADDR_LEN];
164 
165 	if (!mac)
166 		return;
167 
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;
174 	} else {
175 		/* ignore the wrong mac, use random mac */
176 		PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
177 	}
178 }
179 
180 int
181 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
182 		     int cq, int queue_size, const char *mac)
183 {
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;
189 	parse_mac(dev, mac);
190 	dev->vhostfd = -1;
191 
192 	dev->vhostfd = vhost_user_setup(dev->path);
193 	if (dev->vhostfd < 0) {
194 		PMD_INIT_LOG(ERR, "backend set up fails");
195 		return -1;
196 	}
197 	if (vhost_user_sock(dev->vhostfd, VHOST_USER_SET_OWNER, NULL) < 0) {
198 		PMD_INIT_LOG(ERR, "set_owner fails: %s", strerror(errno));
199 		return -1;
200 	}
201 
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));
205 		return -1;
206 	}
207 	if (dev->mac_specified)
208 		dev->features |= (1ull << VIRTIO_NET_F_MAC);
209 
210 	if (!cq) {
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);
218 	} else {
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.
223 		 */
224 		if (!(dev->features & (1ull << VIRTIO_NET_F_CTRL_VQ)))
225 			PMD_INIT_LOG(INFO, "vhost does not support ctrl-q");
226 	}
227 
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");
231 			return -1;
232 		}
233 	}
234 
235 	return 0;
236 }
237 
238 void
239 virtio_user_dev_uninit(struct virtio_user_dev *dev)
240 {
241 	uint32_t i;
242 
243 	for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
244 		close(dev->callfds[i]);
245 		close(dev->kickfds[i]);
246 	}
247 
248 	close(dev->vhostfd);
249 }
250 
251 static uint8_t
252 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
253 {
254 	uint16_t i;
255 	uint8_t ret = 0;
256 
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);
260 		return -1;
261 	}
262 
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);
267 
268 	dev->queue_pairs = q_pairs;
269 
270 	return ret;
271 }
272 
273 static uint32_t
274 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
275 			    uint16_t idx_hdr)
276 {
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;
281 
282 	/* locate desc for header, data, and status */
283 	idx_data = vring->desc[idx_hdr].next;
284 	n_descs++;
285 
286 	i = idx_data;
287 	while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
288 		i = vring->desc[i].next;
289 		n_descs++;
290 	}
291 
292 	/* locate desc for status */
293 	idx_status = i;
294 	n_descs++;
295 
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) {
299 		uint16_t queues;
300 
301 		queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
302 		status = virtio_user_handle_mq(dev, queues);
303 	}
304 
305 	/* Update status */
306 	*(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
307 
308 	return n_descs;
309 }
310 
311 void
312 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
313 {
314 	uint16_t avail_idx, desc_idx;
315 	struct vring_used_elem *uep;
316 	uint32_t n_descs;
317 	struct vring *vring = &dev->vrings[queue_idx];
318 
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];
323 
324 		n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
325 
326 		/* Update used ring */
327 		uep = &vring->used->ring[avail_idx];
328 		uep->id = avail_idx;
329 		uep->len = n_descs;
330 
331 		vring->used->idx++;
332 	}
333 }
334