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 <sys/socket.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <sys/un.h>
40 #include <string.h>
41 #include <errno.h>
42 
43 #include "vhost.h"
44 #include "virtio_user_dev.h"
45 
46 /* The version of the protocol we support */
47 #define VHOST_USER_VERSION    0x1
48 
49 #define VHOST_MEMORY_MAX_NREGIONS 8
50 struct vhost_memory {
51 	uint32_t nregions;
52 	uint32_t padding;
53 	struct vhost_memory_region regions[VHOST_MEMORY_MAX_NREGIONS];
54 };
55 
56 struct vhost_user_msg {
57 	enum vhost_user_request request;
58 
59 #define VHOST_USER_VERSION_MASK     0x3
60 #define VHOST_USER_REPLY_MASK       (0x1 << 2)
61 	uint32_t flags;
62 	uint32_t size; /* the following payload size */
63 	union {
64 #define VHOST_USER_VRING_IDX_MASK   0xff
65 #define VHOST_USER_VRING_NOFD_MASK  (0x1 << 8)
66 		uint64_t u64;
67 		struct vhost_vring_state state;
68 		struct vhost_vring_addr addr;
69 		struct vhost_memory memory;
70 	} payload;
71 	int fds[VHOST_MEMORY_MAX_NREGIONS];
72 } __attribute((packed));
73 
74 #define VHOST_USER_HDR_SIZE offsetof(struct vhost_user_msg, payload.u64)
75 #define VHOST_USER_PAYLOAD_SIZE \
76 	(sizeof(struct vhost_user_msg) - VHOST_USER_HDR_SIZE)
77 
78 static int
79 vhost_user_write(int fd, void *buf, int len, int *fds, int fd_num)
80 {
81 	int r;
82 	struct msghdr msgh;
83 	struct iovec iov;
84 	size_t fd_size = fd_num * sizeof(int);
85 	char control[CMSG_SPACE(fd_size)];
86 	struct cmsghdr *cmsg;
87 
88 	memset(&msgh, 0, sizeof(msgh));
89 	memset(control, 0, sizeof(control));
90 
91 	iov.iov_base = (uint8_t *)buf;
92 	iov.iov_len = len;
93 
94 	msgh.msg_iov = &iov;
95 	msgh.msg_iovlen = 1;
96 	msgh.msg_control = control;
97 	msgh.msg_controllen = sizeof(control);
98 
99 	cmsg = CMSG_FIRSTHDR(&msgh);
100 	cmsg->cmsg_len = CMSG_LEN(fd_size);
101 	cmsg->cmsg_level = SOL_SOCKET;
102 	cmsg->cmsg_type = SCM_RIGHTS;
103 	memcpy(CMSG_DATA(cmsg), fds, fd_size);
104 
105 	do {
106 		r = sendmsg(fd, &msgh, 0);
107 	} while (r < 0 && errno == EINTR);
108 
109 	return r;
110 }
111 
112 static int
113 vhost_user_read(int fd, struct vhost_user_msg *msg)
114 {
115 	uint32_t valid_flags = VHOST_USER_REPLY_MASK | VHOST_USER_VERSION;
116 	int ret, sz_hdr = VHOST_USER_HDR_SIZE, sz_payload;
117 
118 	ret = recv(fd, (void *)msg, sz_hdr, 0);
119 	if (ret < sz_hdr) {
120 		PMD_DRV_LOG(ERR, "Failed to recv msg hdr: %d instead of %d.",
121 			    ret, sz_hdr);
122 		goto fail;
123 	}
124 
125 	/* validate msg flags */
126 	if (msg->flags != (valid_flags)) {
127 		PMD_DRV_LOG(ERR, "Failed to recv msg: flags %x instead of %x.",
128 			    msg->flags, valid_flags);
129 		goto fail;
130 	}
131 
132 	sz_payload = msg->size;
133 	if (sz_payload) {
134 		ret = recv(fd, (void *)((char *)msg + sz_hdr), sz_payload, 0);
135 		if (ret < sz_payload) {
136 			PMD_DRV_LOG(ERR,
137 				"Failed to recv msg payload: %d instead of %d.",
138 				ret, msg->size);
139 			goto fail;
140 		}
141 	}
142 
143 	return 0;
144 
145 fail:
146 	return -1;
147 }
148 
149 struct hugepage_file_info {
150 	uint64_t addr;            /**< virtual addr */
151 	size_t   size;            /**< the file size */
152 	char     path[PATH_MAX];  /**< path to backing file */
153 };
154 
155 /* Two possible options:
156  * 1. Match HUGEPAGE_INFO_FMT to find the file storing struct hugepage_file
157  * array. This is simple but cannot be used in secondary process because
158  * secondary process will close and munmap that file.
159  * 2. Match HUGEFILE_FMT to find hugepage files directly.
160  *
161  * We choose option 2.
162  */
163 static int
164 get_hugepage_file_info(struct hugepage_file_info huges[], int max)
165 {
166 	int idx;
167 	FILE *f;
168 	char buf[BUFSIZ], *tmp, *tail;
169 	char *str_underline, *str_start;
170 	int huge_index;
171 	uint64_t v_start, v_end;
172 
173 	f = fopen("/proc/self/maps", "r");
174 	if (!f) {
175 		PMD_DRV_LOG(ERR, "cannot open /proc/self/maps");
176 		return -1;
177 	}
178 
179 	idx = 0;
180 	while (fgets(buf, sizeof(buf), f) != NULL) {
181 		if (sscanf(buf, "%" PRIx64 "-%" PRIx64, &v_start, &v_end) < 2) {
182 			PMD_DRV_LOG(ERR, "Failed to parse address");
183 			goto error;
184 		}
185 
186 		tmp = strchr(buf, ' ') + 1; /** skip address */
187 		tmp = strchr(tmp, ' ') + 1; /** skip perm */
188 		tmp = strchr(tmp, ' ') + 1; /** skip offset */
189 		tmp = strchr(tmp, ' ') + 1; /** skip dev */
190 		tmp = strchr(tmp, ' ') + 1; /** skip inode */
191 		while (*tmp == ' ')         /** skip spaces */
192 			tmp++;
193 		tail = strrchr(tmp, '\n');  /** remove newline if exists */
194 		if (tail)
195 			*tail = '\0';
196 
197 		/* Match HUGEFILE_FMT, aka "%s/%smap_%d",
198 		 * which is defined in eal_filesystem.h
199 		 */
200 		str_underline = strrchr(tmp, '_');
201 		if (!str_underline)
202 			continue;
203 
204 		str_start = str_underline - strlen("map");
205 		if (str_start < tmp)
206 			continue;
207 
208 		if (sscanf(str_start, "map_%d", &huge_index) != 1)
209 			continue;
210 
211 		if (idx >= max) {
212 			PMD_DRV_LOG(ERR, "Exceed maximum of %d", max);
213 			goto error;
214 		}
215 		huges[idx].addr = v_start;
216 		huges[idx].size = v_end - v_start;
217 		snprintf(huges[idx].path, PATH_MAX, "%s", tmp);
218 		idx++;
219 	}
220 
221 	fclose(f);
222 	return idx;
223 
224 error:
225 	fclose(f);
226 	return -1;
227 }
228 
229 static int
230 prepare_vhost_memory_user(struct vhost_user_msg *msg, int fds[])
231 {
232 	int i, num;
233 	struct hugepage_file_info huges[VHOST_MEMORY_MAX_NREGIONS];
234 	struct vhost_memory_region *mr;
235 
236 	num = get_hugepage_file_info(huges, VHOST_MEMORY_MAX_NREGIONS);
237 	if (num < 0) {
238 		PMD_INIT_LOG(ERR, "Failed to prepare memory for vhost-user");
239 		return -1;
240 	}
241 
242 	for (i = 0; i < num; ++i) {
243 		mr = &msg->payload.memory.regions[i];
244 		mr->guest_phys_addr = huges[i].addr; /* use vaddr! */
245 		mr->userspace_addr = huges[i].addr;
246 		mr->memory_size = huges[i].size;
247 		mr->mmap_offset = 0;
248 		fds[i] = open(huges[i].path, O_RDWR);
249 	}
250 
251 	msg->payload.memory.nregions = num;
252 	msg->payload.memory.padding = 0;
253 
254 	return 0;
255 }
256 
257 static struct vhost_user_msg m;
258 
259 const char * const vhost_msg_strings[] = {
260 	[VHOST_USER_SET_OWNER] = "VHOST_SET_OWNER",
261 	[VHOST_USER_RESET_OWNER] = "VHOST_RESET_OWNER",
262 	[VHOST_USER_SET_FEATURES] = "VHOST_SET_FEATURES",
263 	[VHOST_USER_GET_FEATURES] = "VHOST_GET_FEATURES",
264 	[VHOST_USER_SET_VRING_CALL] = "VHOST_SET_VRING_CALL",
265 	[VHOST_USER_SET_VRING_NUM] = "VHOST_SET_VRING_NUM",
266 	[VHOST_USER_SET_VRING_BASE] = "VHOST_SET_VRING_BASE",
267 	[VHOST_USER_GET_VRING_BASE] = "VHOST_GET_VRING_BASE",
268 	[VHOST_USER_SET_VRING_ADDR] = "VHOST_SET_VRING_ADDR",
269 	[VHOST_USER_SET_VRING_KICK] = "VHOST_SET_VRING_KICK",
270 	[VHOST_USER_SET_MEM_TABLE] = "VHOST_SET_MEM_TABLE",
271 	[VHOST_USER_SET_VRING_ENABLE] = "VHOST_SET_VRING_ENABLE",
272 };
273 
274 static int
275 vhost_user_sock(struct virtio_user_dev *dev,
276 		enum vhost_user_request req,
277 		void *arg)
278 {
279 	struct vhost_user_msg msg;
280 	struct vhost_vring_file *file = 0;
281 	int need_reply = 0;
282 	int fds[VHOST_MEMORY_MAX_NREGIONS];
283 	int fd_num = 0;
284 	int i, len;
285 	int vhostfd = dev->vhostfd;
286 
287 	RTE_SET_USED(m);
288 
289 	PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
290 
291 	msg.request = req;
292 	msg.flags = VHOST_USER_VERSION;
293 	msg.size = 0;
294 
295 	switch (req) {
296 	case VHOST_USER_GET_FEATURES:
297 		need_reply = 1;
298 		break;
299 
300 	case VHOST_USER_SET_FEATURES:
301 	case VHOST_USER_SET_LOG_BASE:
302 		msg.payload.u64 = *((__u64 *)arg);
303 		msg.size = sizeof(m.payload.u64);
304 		break;
305 
306 	case VHOST_USER_SET_OWNER:
307 	case VHOST_USER_RESET_OWNER:
308 		break;
309 
310 	case VHOST_USER_SET_MEM_TABLE:
311 		if (prepare_vhost_memory_user(&msg, fds) < 0)
312 			return -1;
313 		fd_num = msg.payload.memory.nregions;
314 		msg.size = sizeof(m.payload.memory.nregions);
315 		msg.size += sizeof(m.payload.memory.padding);
316 		msg.size += fd_num * sizeof(struct vhost_memory_region);
317 		break;
318 
319 	case VHOST_USER_SET_LOG_FD:
320 		fds[fd_num++] = *((int *)arg);
321 		break;
322 
323 	case VHOST_USER_SET_VRING_NUM:
324 	case VHOST_USER_SET_VRING_BASE:
325 	case VHOST_USER_SET_VRING_ENABLE:
326 		memcpy(&msg.payload.state, arg, sizeof(msg.payload.state));
327 		msg.size = sizeof(m.payload.state);
328 		break;
329 
330 	case VHOST_USER_GET_VRING_BASE:
331 		memcpy(&msg.payload.state, arg, sizeof(msg.payload.state));
332 		msg.size = sizeof(m.payload.state);
333 		need_reply = 1;
334 		break;
335 
336 	case VHOST_USER_SET_VRING_ADDR:
337 		memcpy(&msg.payload.addr, arg, sizeof(msg.payload.addr));
338 		msg.size = sizeof(m.payload.addr);
339 		break;
340 
341 	case VHOST_USER_SET_VRING_KICK:
342 	case VHOST_USER_SET_VRING_CALL:
343 	case VHOST_USER_SET_VRING_ERR:
344 		file = arg;
345 		msg.payload.u64 = file->index & VHOST_USER_VRING_IDX_MASK;
346 		msg.size = sizeof(m.payload.u64);
347 		if (file->fd > 0)
348 			fds[fd_num++] = file->fd;
349 		else
350 			msg.payload.u64 |= VHOST_USER_VRING_NOFD_MASK;
351 		break;
352 
353 	default:
354 		PMD_DRV_LOG(ERR, "trying to send unhandled msg type");
355 		return -1;
356 	}
357 
358 	len = VHOST_USER_HDR_SIZE + msg.size;
359 	if (vhost_user_write(vhostfd, &msg, len, fds, fd_num) < 0) {
360 		PMD_DRV_LOG(ERR, "%s failed: %s",
361 			    vhost_msg_strings[req], strerror(errno));
362 		return -1;
363 	}
364 
365 	if (req == VHOST_USER_SET_MEM_TABLE)
366 		for (i = 0; i < fd_num; ++i)
367 			close(fds[i]);
368 
369 	if (need_reply) {
370 		if (vhost_user_read(vhostfd, &msg) < 0) {
371 			PMD_DRV_LOG(ERR, "Received msg failed: %s",
372 				    strerror(errno));
373 			return -1;
374 		}
375 
376 		if (req != msg.request) {
377 			PMD_DRV_LOG(ERR, "Received unexpected msg type");
378 			return -1;
379 		}
380 
381 		switch (req) {
382 		case VHOST_USER_GET_FEATURES:
383 			if (msg.size != sizeof(m.payload.u64)) {
384 				PMD_DRV_LOG(ERR, "Received bad msg size");
385 				return -1;
386 			}
387 			*((__u64 *)arg) = msg.payload.u64;
388 			break;
389 		case VHOST_USER_GET_VRING_BASE:
390 			if (msg.size != sizeof(m.payload.state)) {
391 				PMD_DRV_LOG(ERR, "Received bad msg size");
392 				return -1;
393 			}
394 			memcpy(arg, &msg.payload.state,
395 			       sizeof(struct vhost_vring_state));
396 			break;
397 		default:
398 			PMD_DRV_LOG(ERR, "Received unexpected msg type");
399 			return -1;
400 		}
401 	}
402 
403 	return 0;
404 }
405 
406 /**
407  * Set up environment to talk with a vhost user backend.
408  *
409  * @return
410  *   - (-1) if fail;
411  *   - (0) if succeed.
412  */
413 static int
414 vhost_user_setup(struct virtio_user_dev *dev)
415 {
416 	int fd;
417 	int flag;
418 	struct sockaddr_un un;
419 
420 	fd = socket(AF_UNIX, SOCK_STREAM, 0);
421 	if (fd < 0) {
422 		PMD_DRV_LOG(ERR, "socket() error, %s", strerror(errno));
423 		return -1;
424 	}
425 
426 	flag = fcntl(fd, F_GETFD);
427 	if (fcntl(fd, F_SETFD, flag | FD_CLOEXEC) < 0)
428 		PMD_DRV_LOG(WARNING, "fcntl failed, %s", strerror(errno));
429 
430 	memset(&un, 0, sizeof(un));
431 	un.sun_family = AF_UNIX;
432 	snprintf(un.sun_path, sizeof(un.sun_path), "%s", dev->path);
433 	if (connect(fd, (struct sockaddr *)&un, sizeof(un)) < 0) {
434 		PMD_DRV_LOG(ERR, "connect error, %s", strerror(errno));
435 		close(fd);
436 		return -1;
437 	}
438 
439 	dev->vhostfd = fd;
440 	return 0;
441 }
442 
443 static int
444 vhost_user_enable_queue_pair(struct virtio_user_dev *dev,
445 			     uint16_t pair_idx,
446 			     int enable)
447 {
448 	int i;
449 
450 	for (i = 0; i < 2; ++i) {
451 		struct vhost_vring_state state = {
452 			.index = pair_idx * 2 + i,
453 			.num   = enable,
454 		};
455 
456 		if (vhost_user_sock(dev, VHOST_USER_SET_VRING_ENABLE, &state))
457 			return -1;
458 	}
459 
460 	return 0;
461 }
462 
463 struct virtio_user_backend_ops ops_user = {
464 	.setup = vhost_user_setup,
465 	.send_request = vhost_user_sock,
466 	.enable_qp = vhost_user_enable_queue_pair
467 };
468