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_create_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
49 {
50 	/* Of all per virtqueue MSGs, make sure VHOST_SET_VRING_CALL come
51 	 * firstly because vhost depends on this msg to allocate virtqueue
52 	 * pair.
53 	 */
54 	int callfd;
55 	struct vhost_vring_file file;
56 
57 	/* May use invalid flag, but some backend leverages kickfd and callfd as
58 	 * criteria to judge if dev is alive. so finally we use real event_fd.
59 	 */
60 	callfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
61 	if (callfd < 0) {
62 		PMD_DRV_LOG(ERR, "callfd error, %s\n", strerror(errno));
63 		return -1;
64 	}
65 	file.index = queue_sel;
66 	file.fd = callfd;
67 	vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_CALL, &file);
68 	dev->callfds[queue_sel] = callfd;
69 
70 	return 0;
71 }
72 
73 static int
74 virtio_user_kick_queue(struct virtio_user_dev *dev, uint32_t queue_sel)
75 {
76 	int kickfd;
77 	struct vhost_vring_file file;
78 	struct vhost_vring_state state;
79 	struct vring *vring = &dev->vrings[queue_sel];
80 	struct vhost_vring_addr addr = {
81 		.index = queue_sel,
82 		.desc_user_addr = (uint64_t)(uintptr_t)vring->desc,
83 		.avail_user_addr = (uint64_t)(uintptr_t)vring->avail,
84 		.used_user_addr = (uint64_t)(uintptr_t)vring->used,
85 		.log_guest_addr = 0,
86 		.flags = 0, /* disable log */
87 	};
88 
89 	state.index = queue_sel;
90 	state.num = vring->num;
91 	vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_NUM, &state);
92 
93 	state.num = 0; /* no reservation */
94 	vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_BASE, &state);
95 
96 	vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_ADDR, &addr);
97 
98 	/* Of all per virtqueue MSGs, make sure VHOST_USER_SET_VRING_KICK comes
99 	 * lastly because vhost depends on this msg to judge if
100 	 * virtio is ready.
101 	 */
102 	kickfd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
103 	if (kickfd < 0) {
104 		PMD_DRV_LOG(ERR, "kickfd error, %s\n", strerror(errno));
105 		return -1;
106 	}
107 	file.index = queue_sel;
108 	file.fd = kickfd;
109 	vhost_user_sock(dev->vhostfd, VHOST_USER_SET_VRING_KICK, &file);
110 	dev->kickfds[queue_sel] = kickfd;
111 
112 	return 0;
113 }
114 
115 static int
116 virtio_user_queue_setup(struct virtio_user_dev *dev,
117 			int (*fn)(struct virtio_user_dev *, uint32_t))
118 {
119 	uint32_t i, queue_sel;
120 
121 	for (i = 0; i < dev->max_queue_pairs; ++i) {
122 		queue_sel = 2 * i + VTNET_SQ_RQ_QUEUE_IDX;
123 		if (fn(dev, queue_sel) < 0) {
124 			PMD_DRV_LOG(INFO, "setup rx vq fails: %u", i);
125 			return -1;
126 		}
127 	}
128 	for (i = 0; i < dev->max_queue_pairs; ++i) {
129 		queue_sel = 2 * i + VTNET_SQ_TQ_QUEUE_IDX;
130 		if (fn(dev, queue_sel) < 0) {
131 			PMD_DRV_LOG(INFO, "setup tx vq fails: %u", i);
132 			return -1;
133 		}
134 	}
135 
136 	return 0;
137 }
138 
139 int
140 virtio_user_start_device(struct virtio_user_dev *dev)
141 {
142 	uint64_t features;
143 	int ret;
144 
145 	/* Step 0: tell vhost to create queues */
146 	if (virtio_user_queue_setup(dev, virtio_user_create_queue) < 0)
147 		goto error;
148 
149 	/* Step 1: set features
150 	 * Make sure VHOST_USER_F_PROTOCOL_FEATURES is added if mq is enabled,
151 	 * and VIRTIO_NET_F_MAC is stripped.
152 	 */
153 	features = dev->features;
154 	if (dev->max_queue_pairs > 1)
155 		features |= VHOST_USER_MQ;
156 	features &= ~(1ull << VIRTIO_NET_F_MAC);
157 	ret = vhost_user_sock(dev->vhostfd, VHOST_USER_SET_FEATURES, &features);
158 	if (ret < 0)
159 		goto error;
160 	PMD_DRV_LOG(INFO, "set features: %" PRIx64, features);
161 
162 	/* Step 2: share memory regions */
163 	ret = vhost_user_sock(dev->vhostfd, VHOST_USER_SET_MEM_TABLE, NULL);
164 	if (ret < 0)
165 		goto error;
166 
167 	/* Step 3: kick queues */
168 	if (virtio_user_queue_setup(dev, virtio_user_kick_queue) < 0)
169 		goto error;
170 
171 	/* Step 4: enable queues
172 	 * we enable the 1st queue pair by default.
173 	 */
174 	vhost_user_enable_queue_pair(dev->vhostfd, 0, 1);
175 
176 	return 0;
177 error:
178 	/* TODO: free resource here or caller to check */
179 	return -1;
180 }
181 
182 int virtio_user_stop_device(struct virtio_user_dev *dev)
183 {
184 	return vhost_user_sock(dev->vhostfd, VHOST_USER_RESET_OWNER, NULL);
185 }
186 
187 static inline void
188 parse_mac(struct virtio_user_dev *dev, const char *mac)
189 {
190 	int i, r;
191 	uint32_t tmp[ETHER_ADDR_LEN];
192 
193 	if (!mac)
194 		return;
195 
196 	r = sscanf(mac, "%x:%x:%x:%x:%x:%x", &tmp[0],
197 			&tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5]);
198 	if (r == ETHER_ADDR_LEN) {
199 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
200 			dev->mac_addr[i] = (uint8_t)tmp[i];
201 		dev->mac_specified = 1;
202 	} else {
203 		/* ignore the wrong mac, use random mac */
204 		PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
205 	}
206 }
207 
208 int
209 virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
210 		     int cq, int queue_size, const char *mac)
211 {
212 	snprintf(dev->path, PATH_MAX, "%s", path);
213 	dev->max_queue_pairs = queues;
214 	dev->queue_pairs = 1; /* mq disabled by default */
215 	dev->queue_size = queue_size;
216 	dev->mac_specified = 0;
217 	parse_mac(dev, mac);
218 	dev->vhostfd = -1;
219 
220 	dev->vhostfd = vhost_user_setup(dev->path);
221 	if (dev->vhostfd < 0) {
222 		PMD_INIT_LOG(ERR, "backend set up fails");
223 		return -1;
224 	}
225 	if (vhost_user_sock(dev->vhostfd, VHOST_USER_SET_OWNER, NULL) < 0) {
226 		PMD_INIT_LOG(ERR, "set_owner fails: %s", strerror(errno));
227 		return -1;
228 	}
229 
230 	if (vhost_user_sock(dev->vhostfd, VHOST_USER_GET_FEATURES,
231 			    &dev->features) < 0) {
232 		PMD_INIT_LOG(ERR, "get_features failed: %s", strerror(errno));
233 		return -1;
234 	}
235 	if (dev->mac_specified)
236 		dev->features |= (1ull << VIRTIO_NET_F_MAC);
237 
238 	if (!cq) {
239 		dev->features &= ~(1ull << VIRTIO_NET_F_CTRL_VQ);
240 		/* Also disable features depends on VIRTIO_NET_F_CTRL_VQ */
241 		dev->features &= ~(1ull << VIRTIO_NET_F_CTRL_RX);
242 		dev->features &= ~(1ull << VIRTIO_NET_F_CTRL_VLAN);
243 		dev->features &= ~(1ull << VIRTIO_NET_F_GUEST_ANNOUNCE);
244 		dev->features &= ~(1ull << VIRTIO_NET_F_MQ);
245 		dev->features &= ~(1ull << VIRTIO_NET_F_CTRL_MAC_ADDR);
246 	} else {
247 		/* vhost user backend does not need to know ctrl-q, so
248 		 * actually we need add this bit into features. However,
249 		 * DPDK vhost-user does send features with this bit, so we
250 		 * check it instead of OR it for now.
251 		 */
252 		if (!(dev->features & (1ull << VIRTIO_NET_F_CTRL_VQ)))
253 			PMD_INIT_LOG(INFO, "vhost does not support ctrl-q");
254 	}
255 
256 	if (dev->max_queue_pairs > 1) {
257 		if (!(dev->features & VHOST_USER_MQ)) {
258 			PMD_INIT_LOG(ERR, "MQ not supported by the backend");
259 			return -1;
260 		}
261 	}
262 
263 	return 0;
264 }
265 
266 void
267 virtio_user_dev_uninit(struct virtio_user_dev *dev)
268 {
269 	uint32_t i;
270 
271 	for (i = 0; i < dev->max_queue_pairs * 2; ++i) {
272 		close(dev->callfds[i]);
273 		close(dev->kickfds[i]);
274 	}
275 
276 	close(dev->vhostfd);
277 }
278 
279 static uint8_t
280 virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs)
281 {
282 	uint16_t i;
283 	uint8_t ret = 0;
284 
285 	if (q_pairs > dev->max_queue_pairs) {
286 		PMD_INIT_LOG(ERR, "multi-q config %u, but only %u supported",
287 			     q_pairs, dev->max_queue_pairs);
288 		return -1;
289 	}
290 
291 	for (i = 0; i < q_pairs; ++i)
292 		ret |= vhost_user_enable_queue_pair(dev->vhostfd, i, 1);
293 	for (i = q_pairs; i < dev->max_queue_pairs; ++i)
294 		ret |= vhost_user_enable_queue_pair(dev->vhostfd, i, 0);
295 
296 	dev->queue_pairs = q_pairs;
297 
298 	return ret;
299 }
300 
301 static uint32_t
302 virtio_user_handle_ctrl_msg(struct virtio_user_dev *dev, struct vring *vring,
303 			    uint16_t idx_hdr)
304 {
305 	struct virtio_net_ctrl_hdr *hdr;
306 	virtio_net_ctrl_ack status = ~0;
307 	uint16_t i, idx_data, idx_status;
308 	uint32_t n_descs = 0;
309 
310 	/* locate desc for header, data, and status */
311 	idx_data = vring->desc[idx_hdr].next;
312 	n_descs++;
313 
314 	i = idx_data;
315 	while (vring->desc[i].flags == VRING_DESC_F_NEXT) {
316 		i = vring->desc[i].next;
317 		n_descs++;
318 	}
319 
320 	/* locate desc for status */
321 	idx_status = i;
322 	n_descs++;
323 
324 	hdr = (void *)(uintptr_t)vring->desc[idx_hdr].addr;
325 	if (hdr->class == VIRTIO_NET_CTRL_MQ &&
326 	    hdr->cmd == VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
327 		uint16_t queues;
328 
329 		queues = *(uint16_t *)(uintptr_t)vring->desc[idx_data].addr;
330 		status = virtio_user_handle_mq(dev, queues);
331 	}
332 
333 	/* Update status */
334 	*(virtio_net_ctrl_ack *)(uintptr_t)vring->desc[idx_status].addr = status;
335 
336 	return n_descs;
337 }
338 
339 void
340 virtio_user_handle_cq(struct virtio_user_dev *dev, uint16_t queue_idx)
341 {
342 	uint16_t avail_idx, desc_idx;
343 	struct vring_used_elem *uep;
344 	uint32_t n_descs;
345 	struct vring *vring = &dev->vrings[queue_idx];
346 
347 	/* Consume avail ring, using used ring idx as first one */
348 	while (vring->used->idx != vring->avail->idx) {
349 		avail_idx = (vring->used->idx) & (vring->num - 1);
350 		desc_idx = vring->avail->ring[avail_idx];
351 
352 		n_descs = virtio_user_handle_ctrl_msg(dev, vring, desc_idx);
353 
354 		/* Update used ring */
355 		uep = &vring->used->ring[avail_idx];
356 		uep->id = avail_idx;
357 		uep->len = n_descs;
358 
359 		vring->used->idx++;
360 	}
361 }
362