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