xref: /dpdk/drivers/net/virtio/virtio_user/virtio_user_dev.c (revision 4e30ead5e7ca886535e2b30632b2948d2aac1681)
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 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <unistd.h>
45 
46 #include "vhost.h"
47 #include "virtio_user_dev.h"
48 #include "../virtio_ethdev.h"
49 
50 static int
51 virtio_user_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
52 {
53 	/* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
54 	 * firstly because vhost depends on this msg to allocate virtqueue
55 	 * pair.
56 	 */
57 	struct vhost_vring_file file;
58 
59 	file.index = queue_sel;
60 	file.fd = dev->callfds[queue_sel];
61 	dev->ops->send_request(dev, VHOST_USER_SET_VRING_CALL, &file);
62 
63 	return 0;
64 }
65 
66 static int
67 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
68 {
69 	struct vhost_vring_file file;
70 	struct vhost_vring_state state;
71 	struct vring *vring = &dev->vrings[queue_sel];
72 	struct vhost_vring_addr addr = {
73 		.index = queue_sel,
74 		.desc_user_addr = (uint64_t)(uintptr_t)vring->desc,
75 		.avail_user_addr = (uint64_t)(uintptr_t)vring->avail,
76 		.used_user_addr = (uint64_t)(uintptr_t)vring->used,
77 		.log_guest_addr = 0,
78 		.flags = 0, /* disable log */
79 	};
80 
81 	state.index = queue_sel;
82 	state.num = vring->num;
83 	dev->ops->send_request(dev, VHOST_USER_SET_VRING_NUM, &state);
84 
85 	state.index = queue_sel;
86 	state.num = 0; /* no reservation */
87 	dev->ops->send_request(dev, VHOST_USER_SET_VRING_BASE, &state);
88 
89 	dev->ops->send_request(dev, VHOST_USER_SET_VRING_ADDR, &addr);
90 
91 	/* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
92 	 * lastly because vhost depends on this msg to judge if
93 	 * virtio is ready.
94 	 */
95 	file.index = queue_sel;
96 	file.fd = dev->kickfds[queue_sel];
97 	dev->ops->send_request(dev, VHOST_USER_SET_VRING_KICK, &file);
98 
99 	return 0;
100 }
101 
102 static int
103 virtio_user_queue_setup(struct virtio_user_dev *dev,
104 			int (*fn)(struct virtio_user_dev *, uint32_t))
105 {
106 	uint32_t i, queue_sel;
107 
108 	for (i = 0; i < dev->max_queue_pairs; ++i) {
109 		queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
110 		if (fn(dev, queue_sel) < 0) {
111 			PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i);
112 			return -1;
113 		}
114 	}
115 	for (i = 0; i < dev->max_queue_pairs; ++i) {
116 		queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
117 		if (fn(dev, queue_sel) < 0) {
118 			PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i);
119 			return -1;
120 		}
121 	}
122 
123 	return 0;
124 }
125 
126 int
127 virtio_user_start_device(struct virtio_user_dev *dev)
128 {
129 	uint64_t features;
130 	int ret;
131 
132 	/* Step 0: tell vhost to create queues */
133 	if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
134 		goto error;
135 
136 	/* Step 1: set features */
137 	features = dev->features;
138 	/* Strip VIRTIO_NET_F_MAC, as MAC address is handled in vdev init */
139 	features &= ~(1ull << VIRTIO_NET_F_MAC);
140 	/* Strip VIRTIO_NET_F_CTRL_VQ, as devices do not really need to know */
141 	features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
142 	features &= ~(1ull << VIRTIO_NET_F_STATUS);
143 	ret = dev->ops->send_request(dev, VHOST_USER_SET_FEATURES, &features);
144 	if (ret < 0)
145 		goto error;
146 	PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
147 
148 	/* Step 2: share memory regions */
149 	ret = dev->ops->send_request(dev, VHOST_USER_SET_MEM_TABLE, NULL);
150 	if (ret < 0)
151 		goto error;
152 
153 	/* Step 3: kick queues */
154 	if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0)
155 		goto error;
156 
157 	/* Step 4: enable queues
158 	 * we enable the 1st queue pair by default.
159 	 */
160 	dev->ops->enable_qp(dev, 0, 1);
161 
162 	return 0;
163 error:
164 	/* TODO: free resource here or caller to check */
165 	return -1;
166 }
167 
168 int virtio_user_stop_device(struct virtio_user_dev *dev)
169 {
170 	uint32_t i;
171 
172 	for (i = 0; i < dev->max_queue_pairs; ++i)
173 		dev->ops->enable_qp(dev, i, 0);
174 
175 	return 0;
176 }
177 
178 static inline void
179 parse_mac(struct virtio_user_dev *dev, const char *mac)
180 {
181 	int i, r;
182 	uint32_t tmp[ETHER_ADDR_LEN];
183 
184 	if (!mac)
185 		return;
186 
187 	r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
188 			&tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
189 	if (r == ETHER_ADDR_LEN) {
190 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
191 			dev->mac_addr[i] = (uint8_t)tmp[i];
192 		dev->mac_specified = 1;
193 	} else {
194 		/* ignore the wrong mac, use random mac */
195 		PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
196 	}
197 }
198 
199 int
200 is_vhost_user_by_type(const char *path)
201 {
202 	struct stat sb;
203 
204 	if (stat(path, &sb) == -1)
205 		return 0;
206 
207 	return S_ISSOCK(sb.st_mode);
208 }
209 
210 static int
211 virtio_user_dev_init_notify(struct virtio_user_dev *dev)
212 {
213 	uint32_t i, j;
214 	int callfd;
215 	int kickfd;
216 
217 	for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; ++i) {
218 		if (i >= dev->max_queue_pairs * 2) {
219 			dev->kickfds[i] = -1;
220 			dev->callfds[i] = -1;
221 			continue;
222 		}
223 
224 		/* May use invalid flag, but some backend uses kickfd and
225 		 * callfd as criteria to judge if dev is alive. so finally we
226 		 * use real event_fd.
227 		 */
228 		callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
229 		if (callfd < 0) {
230 			PMD_DRV_LOG(ERR, "callfd error, %s", strerror(errno));
231 			break;
232 		}
233 		kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
234 		if (kickfd < 0) {
235 			PMD_DRV_LOG(ERR, "kickfd error, %s", strerror(errno));
236 			break;
237 		}
238 		dev->callfds[i] = callfd;
239 		dev->kickfds[i] = kickfd;
240 	}
241 
242 	if (i < VIRTIO_MAX_VIRTQUEUES) {
243 		for (j = 0; j <= i; ++j) {
244 			close(dev->callfds[j]);
245 			close(dev->kickfds[j]);
246 		}
247 
248 		return -1;
249 	}
250 
251 	return 0;
252 }
253 
254 static int
255 virtio_user_fill_intr_handle(struct virtio_user_dev *dev)
256 {
257 	uint32_t i;
258 	struct rte_eth_dev *eth_dev = &rte_eth_devices[dev->port_id];
259 
260 	if (!eth_dev->intr_handle) {
261 		eth_dev->intr_handle = malloc(sizeof(*eth_dev->intr_handle));
262 		if (!eth_dev->intr_handle) {
263 			PMD_DRV_LOG(ERR, "fail to allocate intr_handle");
264 			return -1;
265 		}
266 		memset(eth_dev->intr_handle, 0, sizeof(*eth_dev->intr_handle));
267 	}
268 
269 	for (i = 0; i < dev->max_queue_pairs; ++i)
270 		eth_dev->intr_handle->efds[i] = dev->callfds[i];
271 	eth_dev->intr_handle->nb_efd = dev->max_queue_pairs;
272 	eth_dev->intr_handle->max_intr = dev->max_queue_pairs + 1;
273 	eth_dev->intr_handle->type = RTE_INTR_HANDLE_VDEV;
274 	if (dev->vhostfd >= 0)
275 		eth_dev->intr_handle->fd = dev->vhostfd;
276 
277 	return 0;
278 }
279 
280 static int
281 virtio_user_dev_setup(struct virtio_user_dev *dev)
282 {
283 	uint32_t q;
284 
285 	dev->vhostfd = -1;
286 	dev->vhostfds = NULL;
287 	dev->tapfds = NULL;
288 
289 	if (is_vhost_user_by_type(dev->path)) {
290 		dev->ops = &ops_user;
291 	} else {
292 		dev->ops = &ops_kernel;
293 
294 		dev->vhostfds = malloc(dev->max_queue_pairs * sizeof(int));
295 		dev->tapfds = malloc(dev->max_queue_pairs * sizeof(int));
296 		if (!dev->vhostfds || !dev->tapfds) {
297 			PMD_INIT_LOG(ERR, "Failed to malloc");
298 			return -1;
299 		}
300 
301 		for (q = 0; q < dev->max_queue_pairs; ++q) {
302 			dev->vhostfds[q] = -1;
303 			dev->tapfds[q] = -1;
304 		}
305 	}
306 
307 	if (dev->ops->setup(dev) < 0)
308 		return -1;
309 
310 	if (virtio_user_dev_init_notify(dev) < 0)
311 		return -1;
312 
313 	if (virtio_user_fill_intr_handle(dev) < 0)
314 		return -1;
315 
316 	return 0;
317 }
318 
319 /* Use below macro to filter features from vhost backend */
320 #define VIRTIO_USER_SUPPORTED_FEATURES			\
321 	(1ULL << VIRTIO_NET_F_MAC		|	\
322 	 1ULL << VIRTIO_NET_F_STATUS		|	\
323 	 1ULL << VIRTIO_NET_F_MQ		|	\
324 	 1ULL << VIRTIO_NET_F_CTRL_MAC_ADDR	|	\
325 	 1ULL << VIRTIO_NET_F_CTRL_VQ		|	\
326 	 1ULL << VIRTIO_NET_F_CTRL_RX		|	\
327 	 1ULL << VIRTIO_NET_F_CTRL_VLAN		|	\
328 	 1ULL << VIRTIO_NET_F_CSUM		|	\
329 	 1ULL << VIRTIO_NET_F_HOST_TSO4		|	\
330 	 1ULL << VIRTIO_NET_F_HOST_TSO6		|	\
331 	 1ULL << VIRTIO_NET_F_MRG_RXBUF		|	\
332 	 1ULL << VIRTIO_RING_F_INDIRECT_DESC	|	\
333 	 1ULL << VIRTIO_NET_F_GUEST_CSUM	|	\
334 	 1ULL << VIRTIO_NET_F_GUEST_TSO4	|	\
335 	 1ULL << VIRTIO_NET_F_GUEST_TSO6	|	\
336 	 1ULL << VIRTIO_F_VERSION_1)
337 
338 int
339 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
340 		     int cq, int queue_size, const char *mac, char **ifname)
341 {
342 	snprintf(dev->path, PATH_MAX, "%s", path);
343 	dev->max_queue_pairs = queues;
344 	dev->queue_pairs = 1; /* mq disabled by default */
345 	dev->queue_size = queue_size;
346 	dev->mac_specified = 0;
347 	parse_mac(dev, mac);
348 
349 	if (*ifname) {
350 		dev->ifname = *ifname;
351 		*ifname = NULL;
352 	}
353 
354 	if (virtio_user_dev_setup(dev) < 0) {
355 		PMD_INIT_LOG(ERR, "backend set up fails");
356 		return -1;
357 	}
358 	if (dev->ops->send_request(dev, VHOST_USER_SET_OWNER, NULL) < 0) {
359 		PMD_INIT_LOG(ERR, "set_owner fails: %s", strerror(errno));
360 		return -1;
361 	}
362 
363 	if (dev->ops->send_request(dev, VHOST_USER_GET_FEATURES,
364 			    &dev->device_features) < 0) {
365 		PMD_INIT_LOG(ERR, "get_features failed: %s", strerror(errno));
366 		return -1;
367 	}
368 	if (dev->mac_specified)
369 		dev->device_features |= (1ull << VIRTIO_NET_F_MAC);
370 
371 	if (cq) {
372 		/* device does not really need to know anything about CQ,
373 		 * so if necessary, we just claim to support CQ
374 		 */
375 		dev->device_features |= (1ull << VIRTIO_NET_F_CTRL_VQ);
376 	} else {
377 		dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
378 		/* Also disable features depends on VIRTIO_NET_F_CTRL_VQ */
379 		dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
380 		dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_VLAN);
381 		dev->device_features &= ~(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
382 		dev->device_features &= ~(1ull << VIRTIO_NET_F_MQ);
383 		dev->device_features &= ~(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
384 	}
385 
386 	/* The backend will not report this feature, we add it explicitly */
387 	if (is_vhost_user_by_type(dev->path))
388 		dev->device_features |= (1ull << VIRTIO_NET_F_STATUS);
389 
390 	dev->device_features &= VIRTIO_USER_SUPPORTED_FEATURES;
391 
392 	return 0;
393 }
394 
395 void
396 virtio_user_dev_uninit(struct virtio_user_dev *dev)
397 {
398 	uint32_t i;
399 
400 	virtio_user_stop_device(dev);
401 
402 	for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
403 		close(dev->callfds[i]);
404 		close(dev->kickfds[i]);
405 	}
406 
407 	close(dev->vhostfd);
408 
409 	if (dev->vhostfds) {
410 		for (i = 0; i < dev->max_queue_pairs; ++i)
411 			close(dev->vhostfds[i]);
412 		free(dev->vhostfds);
413 		free(dev->tapfds);
414 	}
415 
416 	free(dev->ifname);
417 }
418 
419 static uint8_t
420 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
421 {
422 	uint16_t i;
423 	uint8_t ret = 0;
424 
425 	if (q_pairs > dev->max_queue_pairs) {
426 		PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
427 			     q_pairs, dev->max_queue_pairs);
428 		return -1;
429 	}
430 
431 	for (i = 0; i < q_pairs; ++i)
432 		ret |= dev->ops->enable_qp(dev, i, 1);
433 	for (i = q_pairs; i < dev->max_queue_pairs; ++i)
434 		ret |= dev->ops->enable_qp(dev, i, 0);
435 
436 	dev->queue_pairs = q_pairs;
437 
438 	return ret;
439 }
440 
441 static uint32_t
442 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
443 			    uint16_t idx_hdr)
444 {
445 	struct virtio_net_ctrl_hdr *hdr;
446 	virtio_net_ctrl_ack status = ~0;
447 	uint16_t i, idx_data, idx_status;
448 	uint32_t n_descs = 0;
449 
450 	/* locate desc for header, data, and status */
451 	idx_data = vring->desc[idx_hdr].next;
452 	n_descs++;
453 
454 	i = idx_data;
455 	while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
456 		i = vring->desc[i].next;
457 		n_descs++;
458 	}
459 
460 	/* locate desc for status */
461 	idx_status = i;
462 	n_descs++;
463 
464 	hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
465 	if (hdr->class == VIRTIO_NET_CTRL_MQ &&
466 	    hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
467 		uint16_t queues;
468 
469 		queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
470 		status = virtio_user_handle_mq(dev, queues);
471 	}
472 
473 	/* Update status */
474 	*(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
475 
476 	return n_descs;
477 }
478 
479 void
480 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
481 {
482 	uint16_t avail_idx, desc_idx;
483 	struct vring_used_elem *uep;
484 	uint32_t n_descs;
485 	struct vring *vring = &dev->vrings[queue_idx];
486 
487 	/* Consume avail ring, using used ring idx as first one */
488 	while (vring->used->idx != vring->avail->idx) {
489 		avail_idx = (vring->used->idx) & (vring->num - 1);
490 		desc_idx = vring->avail->ring[avail_idx];
491 
492 		n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
493 
494 		/* Update used ring */
495 		uep = &vring->used->ring[avail_idx];
496 		uep->id = avail_idx;
497 		uep->len = n_descs;
498 
499 		vring->used->idx++;
500 	}
501 }
502