xref: /dpdk/lib/vhost/socket.c (revision 515cd4a488b6a0c6e40d20e6b10d8e89657dc23f)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4 
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <limits.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <sys/socket.h>
12 #include <sys/un.h>
13 #include <sys/queue.h>
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <pthread.h>
17 
18 #include <rte_log.h>
19 
20 #include "fd_man.h"
21 #include "vhost.h"
22 #include "vhost_user.h"
23 
24 
25 TAILQ_HEAD(vhost_user_connection_list, vhost_user_connection);
26 
27 /*
28  * Every time rte_vhost_driver_register() is invoked, an associated
29  * vhost_user_socket struct will be created.
30  */
31 struct vhost_user_socket {
32 	struct vhost_user_connection_list conn_list;
33 	pthread_mutex_t conn_mutex;
34 	char *path;
35 	int socket_fd;
36 	struct sockaddr_un un;
37 	bool is_server;
38 	bool reconnect;
39 	bool iommu_support;
40 	bool use_builtin_virtio_net;
41 	bool extbuf;
42 	bool linearbuf;
43 	bool async_copy;
44 	bool net_compliant_ol_flags;
45 	bool stats_enabled;
46 
47 	/*
48 	 * The "supported_features" indicates the feature bits the
49 	 * vhost driver supports. The "features" indicates the feature
50 	 * bits after the rte_vhost_driver_features_disable/enable().
51 	 * It is also the final feature bits used for vhost-user
52 	 * features negotiation.
53 	 */
54 	uint64_t supported_features;
55 	uint64_t features;
56 
57 	uint64_t protocol_features;
58 
59 	struct rte_vdpa_device *vdpa_dev;
60 
61 	struct rte_vhost_device_ops const *notify_ops;
62 };
63 
64 struct vhost_user_connection {
65 	struct vhost_user_socket *vsocket;
66 	int connfd;
67 	int vid;
68 
69 	TAILQ_ENTRY(vhost_user_connection) next;
70 };
71 
72 #define MAX_VHOST_SOCKET 1024
73 struct vhost_user {
74 	struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
75 	struct fdset fdset;
76 	int vsocket_cnt;
77 	pthread_mutex_t mutex;
78 };
79 
80 #define MAX_VIRTIO_BACKLOG 128
81 
82 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
83 static void vhost_user_read_cb(int fd, void *dat, int *remove);
84 static int create_unix_socket(struct vhost_user_socket *vsocket);
85 static int vhost_user_start_client(struct vhost_user_socket *vsocket);
86 
87 static struct vhost_user vhost_user = {
88 	.fdset = {
89 		.fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
90 		.fd_mutex = PTHREAD_MUTEX_INITIALIZER,
91 		.fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
92 		.num = 0
93 	},
94 	.vsocket_cnt = 0,
95 	.mutex = PTHREAD_MUTEX_INITIALIZER,
96 };
97 
98 /*
99  * return bytes# of read on success or negative val on failure. Update fdnum
100  * with number of fds read.
101  */
102 int
103 read_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int max_fds,
104 		int *fd_num)
105 {
106 	struct iovec iov;
107 	struct msghdr msgh;
108 	char control[CMSG_SPACE(max_fds * sizeof(int))];
109 	struct cmsghdr *cmsg;
110 	int got_fds = 0;
111 	int ret;
112 
113 	*fd_num = 0;
114 
115 	memset(&msgh, 0, sizeof(msgh));
116 	iov.iov_base = buf;
117 	iov.iov_len  = buflen;
118 
119 	msgh.msg_iov = &iov;
120 	msgh.msg_iovlen = 1;
121 	msgh.msg_control = control;
122 	msgh.msg_controllen = sizeof(control);
123 
124 	ret = recvmsg(sockfd, &msgh, 0);
125 	if (ret <= 0) {
126 		if (ret)
127 			VHOST_LOG_CONFIG(ifname, ERR, "recvmsg failed on fd %d (%s)\n",
128 				sockfd, strerror(errno));
129 		return ret;
130 	}
131 
132 	if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
133 		VHOST_LOG_CONFIG(ifname, ERR, "truncated msg (fd %d)\n", sockfd);
134 		return -1;
135 	}
136 
137 	for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
138 		cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
139 		if ((cmsg->cmsg_level == SOL_SOCKET) &&
140 			(cmsg->cmsg_type == SCM_RIGHTS)) {
141 			got_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
142 			*fd_num = got_fds;
143 			memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
144 			break;
145 		}
146 	}
147 
148 	/* Clear out unused file descriptors */
149 	while (got_fds < max_fds)
150 		fds[got_fds++] = -1;
151 
152 	return ret;
153 }
154 
155 int
156 send_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int fd_num)
157 {
158 
159 	struct iovec iov;
160 	struct msghdr msgh;
161 	size_t fdsize = fd_num * sizeof(int);
162 	char control[CMSG_SPACE(fdsize)];
163 	struct cmsghdr *cmsg;
164 	int ret;
165 
166 	memset(&msgh, 0, sizeof(msgh));
167 	iov.iov_base = buf;
168 	iov.iov_len = buflen;
169 
170 	msgh.msg_iov = &iov;
171 	msgh.msg_iovlen = 1;
172 
173 	if (fds && fd_num > 0) {
174 		msgh.msg_control = control;
175 		msgh.msg_controllen = sizeof(control);
176 		cmsg = CMSG_FIRSTHDR(&msgh);
177 		if (cmsg == NULL) {
178 			VHOST_LOG_CONFIG(ifname, ERR, "cmsg == NULL\n");
179 			errno = EINVAL;
180 			return -1;
181 		}
182 		cmsg->cmsg_len = CMSG_LEN(fdsize);
183 		cmsg->cmsg_level = SOL_SOCKET;
184 		cmsg->cmsg_type = SCM_RIGHTS;
185 		memcpy(CMSG_DATA(cmsg), fds, fdsize);
186 	} else {
187 		msgh.msg_control = NULL;
188 		msgh.msg_controllen = 0;
189 	}
190 
191 	do {
192 		ret = sendmsg(sockfd, &msgh, MSG_NOSIGNAL);
193 	} while (ret < 0 && errno == EINTR);
194 
195 	if (ret < 0) {
196 		VHOST_LOG_CONFIG(ifname, ERR, "sendmsg error on fd %d (%s)\n",
197 			sockfd, strerror(errno));
198 		return ret;
199 	}
200 
201 	return ret;
202 }
203 
204 static void
205 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
206 {
207 	int vid;
208 	size_t size;
209 	struct vhost_user_connection *conn;
210 	int ret;
211 	struct virtio_net *dev;
212 
213 	if (vsocket == NULL)
214 		return;
215 
216 	conn = malloc(sizeof(*conn));
217 	if (conn == NULL) {
218 		close(fd);
219 		return;
220 	}
221 
222 	vid = vhost_new_device();
223 	if (vid == -1) {
224 		goto err;
225 	}
226 
227 	size = strnlen(vsocket->path, PATH_MAX);
228 	vhost_set_ifname(vid, vsocket->path, size);
229 
230 	vhost_setup_virtio_net(vid, vsocket->use_builtin_virtio_net,
231 		vsocket->net_compliant_ol_flags, vsocket->stats_enabled,
232 		vsocket->iommu_support);
233 
234 	vhost_attach_vdpa_device(vid, vsocket->vdpa_dev);
235 
236 	if (vsocket->extbuf)
237 		vhost_enable_extbuf(vid);
238 
239 	if (vsocket->linearbuf)
240 		vhost_enable_linearbuf(vid);
241 
242 	if (vsocket->async_copy) {
243 		dev = get_device(vid);
244 
245 		if (dev)
246 			dev->async_copy = 1;
247 	}
248 
249 	VHOST_LOG_CONFIG(vsocket->path, INFO, "new device, handle is %d\n", vid);
250 
251 	if (vsocket->notify_ops->new_connection) {
252 		ret = vsocket->notify_ops->new_connection(vid);
253 		if (ret < 0) {
254 			VHOST_LOG_CONFIG(vsocket->path, ERR,
255 				"failed to add vhost user connection with fd %d\n",
256 				fd);
257 			goto err_cleanup;
258 		}
259 	}
260 
261 	conn->connfd = fd;
262 	conn->vsocket = vsocket;
263 	conn->vid = vid;
264 	ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
265 			NULL, conn);
266 	if (ret < 0) {
267 		VHOST_LOG_CONFIG(vsocket->path, ERR,
268 			"failed to add fd %d into vhost server fdset\n",
269 			fd);
270 
271 		if (vsocket->notify_ops->destroy_connection)
272 			vsocket->notify_ops->destroy_connection(conn->vid);
273 
274 		goto err_cleanup;
275 	}
276 
277 	pthread_mutex_lock(&vsocket->conn_mutex);
278 	TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
279 	pthread_mutex_unlock(&vsocket->conn_mutex);
280 
281 	fdset_pipe_notify(&vhost_user.fdset);
282 	return;
283 
284 err_cleanup:
285 	vhost_destroy_device(vid);
286 err:
287 	free(conn);
288 	close(fd);
289 }
290 
291 /* call back when there is new vhost-user connection from client  */
292 static void
293 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
294 {
295 	struct vhost_user_socket *vsocket = dat;
296 
297 	fd = accept(fd, NULL, NULL);
298 	if (fd < 0)
299 		return;
300 
301 	VHOST_LOG_CONFIG(vsocket->path, INFO, "new vhost user connection is %d\n", fd);
302 	vhost_user_add_connection(fd, vsocket);
303 }
304 
305 static void
306 vhost_user_read_cb(int connfd, void *dat, int *remove)
307 {
308 	struct vhost_user_connection *conn = dat;
309 	struct vhost_user_socket *vsocket = conn->vsocket;
310 	int ret;
311 
312 	ret = vhost_user_msg_handler(conn->vid, connfd);
313 	if (ret < 0) {
314 		struct virtio_net *dev = get_device(conn->vid);
315 
316 		close(connfd);
317 		*remove = 1;
318 
319 		if (dev)
320 			vhost_destroy_device_notify(dev);
321 
322 		if (vsocket->notify_ops->destroy_connection)
323 			vsocket->notify_ops->destroy_connection(conn->vid);
324 
325 		vhost_destroy_device(conn->vid);
326 
327 		if (vsocket->reconnect) {
328 			create_unix_socket(vsocket);
329 			vhost_user_start_client(vsocket);
330 		}
331 
332 		pthread_mutex_lock(&vsocket->conn_mutex);
333 		TAILQ_REMOVE(&vsocket->conn_list, conn, next);
334 		pthread_mutex_unlock(&vsocket->conn_mutex);
335 
336 		free(conn);
337 	}
338 }
339 
340 static int
341 create_unix_socket(struct vhost_user_socket *vsocket)
342 {
343 	int fd;
344 	struct sockaddr_un *un = &vsocket->un;
345 
346 	fd = socket(AF_UNIX, SOCK_STREAM, 0);
347 	if (fd < 0)
348 		return -1;
349 	VHOST_LOG_CONFIG(vsocket->path, INFO, "vhost-user %s: socket created, fd: %d\n",
350 		vsocket->is_server ? "server" : "client", fd);
351 
352 	if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
353 		VHOST_LOG_CONFIG(vsocket->path, ERR,
354 			"vhost-user: can't set nonblocking mode for socket, fd: %d (%s)\n",
355 			fd, strerror(errno));
356 		close(fd);
357 		return -1;
358 	}
359 
360 	memset(un, 0, sizeof(*un));
361 	un->sun_family = AF_UNIX;
362 	strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
363 	un->sun_path[sizeof(un->sun_path) - 1] = '\0';
364 
365 	vsocket->socket_fd = fd;
366 	return 0;
367 }
368 
369 static int
370 vhost_user_start_server(struct vhost_user_socket *vsocket)
371 {
372 	int ret;
373 	int fd = vsocket->socket_fd;
374 	const char *path = vsocket->path;
375 
376 	/*
377 	 * bind () may fail if the socket file with the same name already
378 	 * exists. But the library obviously should not delete the file
379 	 * provided by the user, since we can not be sure that it is not
380 	 * being used by other applications. Moreover, many applications form
381 	 * socket names based on user input, which is prone to errors.
382 	 *
383 	 * The user must ensure that the socket does not exist before
384 	 * registering the vhost driver in server mode.
385 	 */
386 	ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
387 	if (ret < 0) {
388 		VHOST_LOG_CONFIG(path, ERR, "failed to bind: %s; remove it and try again\n",
389 			strerror(errno));
390 		goto err;
391 	}
392 	VHOST_LOG_CONFIG(path, INFO, "binding succeeded\n");
393 
394 	ret = listen(fd, MAX_VIRTIO_BACKLOG);
395 	if (ret < 0)
396 		goto err;
397 
398 	ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
399 		  NULL, vsocket);
400 	if (ret < 0) {
401 		VHOST_LOG_CONFIG(path, ERR, "failed to add listen fd %d to vhost server fdset\n",
402 			fd);
403 		goto err;
404 	}
405 
406 	return 0;
407 
408 err:
409 	close(fd);
410 	return -1;
411 }
412 
413 struct vhost_user_reconnect {
414 	struct sockaddr_un un;
415 	int fd;
416 	struct vhost_user_socket *vsocket;
417 
418 	TAILQ_ENTRY(vhost_user_reconnect) next;
419 };
420 
421 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
422 struct vhost_user_reconnect_list {
423 	struct vhost_user_reconnect_tailq_list head;
424 	pthread_mutex_t mutex;
425 };
426 
427 static struct vhost_user_reconnect_list reconn_list;
428 static pthread_t reconn_tid;
429 
430 static int
431 vhost_user_connect_nonblock(char *path, int fd, struct sockaddr *un, size_t sz)
432 {
433 	int ret, flags;
434 
435 	ret = connect(fd, un, sz);
436 	if (ret < 0 && errno != EISCONN)
437 		return -1;
438 
439 	flags = fcntl(fd, F_GETFL, 0);
440 	if (flags < 0) {
441 		VHOST_LOG_CONFIG(path, ERR, "can't get flags for connfd %d (%s)\n",
442 			fd, strerror(errno));
443 		return -2;
444 	}
445 	if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
446 		VHOST_LOG_CONFIG(path, ERR, "can't disable nonblocking on fd %d\n", fd);
447 		return -2;
448 	}
449 	return 0;
450 }
451 
452 static void *
453 vhost_user_client_reconnect(void *arg __rte_unused)
454 {
455 	int ret;
456 	struct vhost_user_reconnect *reconn, *next;
457 
458 	while (1) {
459 		pthread_mutex_lock(&reconn_list.mutex);
460 
461 		/*
462 		 * An equal implementation of TAILQ_FOREACH_SAFE,
463 		 * which does not exist on all platforms.
464 		 */
465 		for (reconn = TAILQ_FIRST(&reconn_list.head);
466 		     reconn != NULL; reconn = next) {
467 			next = TAILQ_NEXT(reconn, next);
468 
469 			ret = vhost_user_connect_nonblock(reconn->vsocket->path, reconn->fd,
470 						(struct sockaddr *)&reconn->un,
471 						sizeof(reconn->un));
472 			if (ret == -2) {
473 				close(reconn->fd);
474 				VHOST_LOG_CONFIG(reconn->vsocket->path, ERR,
475 					"reconnection for fd %d failed\n",
476 					reconn->fd);
477 				goto remove_fd;
478 			}
479 			if (ret == -1)
480 				continue;
481 
482 			VHOST_LOG_CONFIG(reconn->vsocket->path, INFO, "connected\n");
483 			vhost_user_add_connection(reconn->fd, reconn->vsocket);
484 remove_fd:
485 			TAILQ_REMOVE(&reconn_list.head, reconn, next);
486 			free(reconn);
487 		}
488 
489 		pthread_mutex_unlock(&reconn_list.mutex);
490 		sleep(1);
491 	}
492 
493 	return NULL;
494 }
495 
496 static int
497 vhost_user_reconnect_init(void)
498 {
499 	int ret;
500 
501 	ret = pthread_mutex_init(&reconn_list.mutex, NULL);
502 	if (ret < 0) {
503 		VHOST_LOG_CONFIG("thread", ERR, "%s: failed to initialize mutex\n", __func__);
504 		return ret;
505 	}
506 	TAILQ_INIT(&reconn_list.head);
507 
508 	ret = rte_ctrl_thread_create(&reconn_tid, "vhost_reconn", NULL,
509 			     vhost_user_client_reconnect, NULL);
510 	if (ret != 0) {
511 		VHOST_LOG_CONFIG("thread", ERR, "failed to create reconnect thread\n");
512 		if (pthread_mutex_destroy(&reconn_list.mutex))
513 			VHOST_LOG_CONFIG("thread", ERR,
514 				"%s: failed to destroy reconnect mutex\n",
515 				__func__);
516 	}
517 
518 	return ret;
519 }
520 
521 static int
522 vhost_user_start_client(struct vhost_user_socket *vsocket)
523 {
524 	int ret;
525 	int fd = vsocket->socket_fd;
526 	const char *path = vsocket->path;
527 	struct vhost_user_reconnect *reconn;
528 
529 	ret = vhost_user_connect_nonblock(vsocket->path, fd, (struct sockaddr *)&vsocket->un,
530 					  sizeof(vsocket->un));
531 	if (ret == 0) {
532 		vhost_user_add_connection(fd, vsocket);
533 		return 0;
534 	}
535 
536 	VHOST_LOG_CONFIG(path, WARNING, "failed to connect: %s\n", strerror(errno));
537 
538 	if (ret == -2 || !vsocket->reconnect) {
539 		close(fd);
540 		return -1;
541 	}
542 
543 	VHOST_LOG_CONFIG(path, INFO, "reconnecting...\n");
544 	reconn = malloc(sizeof(*reconn));
545 	if (reconn == NULL) {
546 		VHOST_LOG_CONFIG(path, ERR, "failed to allocate memory for reconnect\n");
547 		close(fd);
548 		return -1;
549 	}
550 	reconn->un = vsocket->un;
551 	reconn->fd = fd;
552 	reconn->vsocket = vsocket;
553 	pthread_mutex_lock(&reconn_list.mutex);
554 	TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
555 	pthread_mutex_unlock(&reconn_list.mutex);
556 
557 	return 0;
558 }
559 
560 static struct vhost_user_socket *
561 find_vhost_user_socket(const char *path)
562 {
563 	int i;
564 
565 	if (path == NULL)
566 		return NULL;
567 
568 	for (i = 0; i < vhost_user.vsocket_cnt; i++) {
569 		struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
570 
571 		if (!strcmp(vsocket->path, path))
572 			return vsocket;
573 	}
574 
575 	return NULL;
576 }
577 
578 int
579 rte_vhost_driver_attach_vdpa_device(const char *path,
580 		struct rte_vdpa_device *dev)
581 {
582 	struct vhost_user_socket *vsocket;
583 
584 	if (dev == NULL || path == NULL)
585 		return -1;
586 
587 	pthread_mutex_lock(&vhost_user.mutex);
588 	vsocket = find_vhost_user_socket(path);
589 	if (vsocket)
590 		vsocket->vdpa_dev = dev;
591 	pthread_mutex_unlock(&vhost_user.mutex);
592 
593 	return vsocket ? 0 : -1;
594 }
595 
596 int
597 rte_vhost_driver_detach_vdpa_device(const char *path)
598 {
599 	struct vhost_user_socket *vsocket;
600 
601 	pthread_mutex_lock(&vhost_user.mutex);
602 	vsocket = find_vhost_user_socket(path);
603 	if (vsocket)
604 		vsocket->vdpa_dev = NULL;
605 	pthread_mutex_unlock(&vhost_user.mutex);
606 
607 	return vsocket ? 0 : -1;
608 }
609 
610 struct rte_vdpa_device *
611 rte_vhost_driver_get_vdpa_device(const char *path)
612 {
613 	struct vhost_user_socket *vsocket;
614 	struct rte_vdpa_device *dev = NULL;
615 
616 	pthread_mutex_lock(&vhost_user.mutex);
617 	vsocket = find_vhost_user_socket(path);
618 	if (vsocket)
619 		dev = vsocket->vdpa_dev;
620 	pthread_mutex_unlock(&vhost_user.mutex);
621 
622 	return dev;
623 }
624 
625 int
626 rte_vhost_driver_get_vdpa_dev_type(const char *path, uint32_t *type)
627 {
628 	struct vhost_user_socket *vsocket;
629 	struct rte_vdpa_device *vdpa_dev;
630 	uint32_t vdpa_type = 0;
631 	int ret = 0;
632 
633 	pthread_mutex_lock(&vhost_user.mutex);
634 	vsocket = find_vhost_user_socket(path);
635 	if (!vsocket) {
636 		VHOST_LOG_CONFIG(path, ERR, "socket file is not registered yet.\n");
637 		ret = -1;
638 		goto unlock_exit;
639 	}
640 
641 	vdpa_dev = vsocket->vdpa_dev;
642 	if (!vdpa_dev) {
643 		ret = -1;
644 		goto unlock_exit;
645 	}
646 
647 	if (vdpa_dev->ops->get_dev_type) {
648 		ret = vdpa_dev->ops->get_dev_type(vdpa_dev, &vdpa_type);
649 		if (ret) {
650 			VHOST_LOG_CONFIG(path, ERR,
651 				"failed to get vdpa dev type for socket file.\n");
652 			ret = -1;
653 			goto unlock_exit;
654 		}
655 	} else {
656 		vdpa_type = RTE_VHOST_VDPA_DEVICE_TYPE_NET;
657 	}
658 
659 	*type = vdpa_type;
660 
661 unlock_exit:
662 	pthread_mutex_unlock(&vhost_user.mutex);
663 	return ret;
664 }
665 
666 int
667 rte_vhost_driver_disable_features(const char *path, uint64_t features)
668 {
669 	struct vhost_user_socket *vsocket;
670 
671 	pthread_mutex_lock(&vhost_user.mutex);
672 	vsocket = find_vhost_user_socket(path);
673 
674 	/* Note that use_builtin_virtio_net is not affected by this function
675 	 * since callers may want to selectively disable features of the
676 	 * built-in vhost net device backend.
677 	 */
678 
679 	if (vsocket)
680 		vsocket->features &= ~features;
681 	pthread_mutex_unlock(&vhost_user.mutex);
682 
683 	return vsocket ? 0 : -1;
684 }
685 
686 int
687 rte_vhost_driver_enable_features(const char *path, uint64_t features)
688 {
689 	struct vhost_user_socket *vsocket;
690 
691 	pthread_mutex_lock(&vhost_user.mutex);
692 	vsocket = find_vhost_user_socket(path);
693 	if (vsocket) {
694 		if ((vsocket->supported_features & features) != features) {
695 			/*
696 			 * trying to enable features the driver doesn't
697 			 * support.
698 			 */
699 			pthread_mutex_unlock(&vhost_user.mutex);
700 			return -1;
701 		}
702 		vsocket->features |= features;
703 	}
704 	pthread_mutex_unlock(&vhost_user.mutex);
705 
706 	return vsocket ? 0 : -1;
707 }
708 
709 int
710 rte_vhost_driver_set_features(const char *path, uint64_t features)
711 {
712 	struct vhost_user_socket *vsocket;
713 
714 	pthread_mutex_lock(&vhost_user.mutex);
715 	vsocket = find_vhost_user_socket(path);
716 	if (vsocket) {
717 		vsocket->supported_features = features;
718 		vsocket->features = features;
719 
720 		/* Anyone setting feature bits is implementing their own vhost
721 		 * device backend.
722 		 */
723 		vsocket->use_builtin_virtio_net = false;
724 	}
725 	pthread_mutex_unlock(&vhost_user.mutex);
726 
727 	return vsocket ? 0 : -1;
728 }
729 
730 int
731 rte_vhost_driver_get_features(const char *path, uint64_t *features)
732 {
733 	struct vhost_user_socket *vsocket;
734 	uint64_t vdpa_features;
735 	struct rte_vdpa_device *vdpa_dev;
736 	int ret = 0;
737 
738 	pthread_mutex_lock(&vhost_user.mutex);
739 	vsocket = find_vhost_user_socket(path);
740 	if (!vsocket) {
741 		VHOST_LOG_CONFIG(path, ERR, "socket file is not registered yet.\n");
742 		ret = -1;
743 		goto unlock_exit;
744 	}
745 
746 	vdpa_dev = vsocket->vdpa_dev;
747 	if (!vdpa_dev) {
748 		*features = vsocket->features;
749 		goto unlock_exit;
750 	}
751 
752 	if (vdpa_dev->ops->get_features(vdpa_dev, &vdpa_features) < 0) {
753 		VHOST_LOG_CONFIG(path, ERR, "failed to get vdpa features for socket file.\n");
754 		ret = -1;
755 		goto unlock_exit;
756 	}
757 
758 	*features = vsocket->features & vdpa_features;
759 
760 unlock_exit:
761 	pthread_mutex_unlock(&vhost_user.mutex);
762 	return ret;
763 }
764 
765 int
766 rte_vhost_driver_set_protocol_features(const char *path,
767 		uint64_t protocol_features)
768 {
769 	struct vhost_user_socket *vsocket;
770 
771 	pthread_mutex_lock(&vhost_user.mutex);
772 	vsocket = find_vhost_user_socket(path);
773 	if (vsocket)
774 		vsocket->protocol_features = protocol_features;
775 	pthread_mutex_unlock(&vhost_user.mutex);
776 	return vsocket ? 0 : -1;
777 }
778 
779 int
780 rte_vhost_driver_get_protocol_features(const char *path,
781 		uint64_t *protocol_features)
782 {
783 	struct vhost_user_socket *vsocket;
784 	uint64_t vdpa_protocol_features;
785 	struct rte_vdpa_device *vdpa_dev;
786 	int ret = 0;
787 
788 	pthread_mutex_lock(&vhost_user.mutex);
789 	vsocket = find_vhost_user_socket(path);
790 	if (!vsocket) {
791 		VHOST_LOG_CONFIG(path, ERR, "socket file is not registered yet.\n");
792 		ret = -1;
793 		goto unlock_exit;
794 	}
795 
796 	vdpa_dev = vsocket->vdpa_dev;
797 	if (!vdpa_dev) {
798 		*protocol_features = vsocket->protocol_features;
799 		goto unlock_exit;
800 	}
801 
802 	if (vdpa_dev->ops->get_protocol_features(vdpa_dev,
803 				&vdpa_protocol_features) < 0) {
804 		VHOST_LOG_CONFIG(path, ERR, "failed to get vdpa protocol features.\n");
805 		ret = -1;
806 		goto unlock_exit;
807 	}
808 
809 	*protocol_features = vsocket->protocol_features
810 		& vdpa_protocol_features;
811 
812 unlock_exit:
813 	pthread_mutex_unlock(&vhost_user.mutex);
814 	return ret;
815 }
816 
817 int
818 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
819 {
820 	struct vhost_user_socket *vsocket;
821 	uint32_t vdpa_queue_num;
822 	struct rte_vdpa_device *vdpa_dev;
823 	int ret = 0;
824 
825 	pthread_mutex_lock(&vhost_user.mutex);
826 	vsocket = find_vhost_user_socket(path);
827 	if (!vsocket) {
828 		VHOST_LOG_CONFIG(path, ERR, "socket file is not registered yet.\n");
829 		ret = -1;
830 		goto unlock_exit;
831 	}
832 
833 	vdpa_dev = vsocket->vdpa_dev;
834 	if (!vdpa_dev) {
835 		*queue_num = VHOST_MAX_QUEUE_PAIRS;
836 		goto unlock_exit;
837 	}
838 
839 	if (vdpa_dev->ops->get_queue_num(vdpa_dev, &vdpa_queue_num) < 0) {
840 		VHOST_LOG_CONFIG(path, ERR, "failed to get vdpa queue number.\n");
841 		ret = -1;
842 		goto unlock_exit;
843 	}
844 
845 	*queue_num = RTE_MIN((uint32_t)VHOST_MAX_QUEUE_PAIRS, vdpa_queue_num);
846 
847 unlock_exit:
848 	pthread_mutex_unlock(&vhost_user.mutex);
849 	return ret;
850 }
851 
852 static void
853 vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
854 {
855 	if (vsocket && vsocket->path) {
856 		free(vsocket->path);
857 		vsocket->path = NULL;
858 	}
859 
860 	if (vsocket) {
861 		free(vsocket);
862 		vsocket = NULL;
863 	}
864 }
865 
866 /*
867  * Register a new vhost-user socket; here we could act as server
868  * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
869  * is set.
870  */
871 int
872 rte_vhost_driver_register(const char *path, uint64_t flags)
873 {
874 	int ret = -1;
875 	struct vhost_user_socket *vsocket;
876 
877 	if (!path)
878 		return -1;
879 
880 	pthread_mutex_lock(&vhost_user.mutex);
881 
882 	if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
883 		VHOST_LOG_CONFIG(path, ERR, "the number of vhost sockets reaches maximum\n");
884 		goto out;
885 	}
886 
887 	vsocket = malloc(sizeof(struct vhost_user_socket));
888 	if (!vsocket)
889 		goto out;
890 	memset(vsocket, 0, sizeof(struct vhost_user_socket));
891 	vsocket->path = strdup(path);
892 	if (vsocket->path == NULL) {
893 		VHOST_LOG_CONFIG(path, ERR, "failed to copy socket path string\n");
894 		vhost_user_socket_mem_free(vsocket);
895 		goto out;
896 	}
897 	TAILQ_INIT(&vsocket->conn_list);
898 	ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
899 	if (ret) {
900 		VHOST_LOG_CONFIG(path, ERR, "failed to init connection mutex\n");
901 		goto out_free;
902 	}
903 	vsocket->vdpa_dev = NULL;
904 	vsocket->extbuf = flags & RTE_VHOST_USER_EXTBUF_SUPPORT;
905 	vsocket->linearbuf = flags & RTE_VHOST_USER_LINEARBUF_SUPPORT;
906 	vsocket->async_copy = flags & RTE_VHOST_USER_ASYNC_COPY;
907 	vsocket->net_compliant_ol_flags = flags & RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS;
908 	vsocket->stats_enabled = flags & RTE_VHOST_USER_NET_STATS_ENABLE;
909 	vsocket->iommu_support = flags & RTE_VHOST_USER_IOMMU_SUPPORT;
910 
911 	if (vsocket->async_copy &&
912 		(flags & (RTE_VHOST_USER_IOMMU_SUPPORT |
913 		RTE_VHOST_USER_POSTCOPY_SUPPORT))) {
914 		VHOST_LOG_CONFIG(path, ERR, "async copy with IOMMU or post-copy not supported\n");
915 		goto out_mutex;
916 	}
917 
918 	/*
919 	 * Set the supported features correctly for the builtin vhost-user
920 	 * net driver.
921 	 *
922 	 * Applications know nothing about features the builtin virtio net
923 	 * driver (virtio_net.c) supports, thus it's not possible for them
924 	 * to invoke rte_vhost_driver_set_features(). To workaround it, here
925 	 * we set it unconditionally. If the application want to implement
926 	 * another vhost-user driver (say SCSI), it should call the
927 	 * rte_vhost_driver_set_features(), which will overwrite following
928 	 * two values.
929 	 */
930 	vsocket->use_builtin_virtio_net = true;
931 	vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
932 	vsocket->features           = VIRTIO_NET_SUPPORTED_FEATURES;
933 	vsocket->protocol_features  = VHOST_USER_PROTOCOL_FEATURES;
934 
935 	if (vsocket->async_copy) {
936 		vsocket->supported_features &= ~(1ULL << VHOST_F_LOG_ALL);
937 		vsocket->features &= ~(1ULL << VHOST_F_LOG_ALL);
938 		VHOST_LOG_CONFIG(path, INFO, "logging feature is disabled in async copy mode\n");
939 	}
940 
941 	/*
942 	 * We'll not be able to receive a buffer from guest in linear mode
943 	 * without external buffer if it will not fit in a single mbuf, which is
944 	 * likely if segmentation offloading enabled.
945 	 */
946 	if (vsocket->linearbuf && !vsocket->extbuf) {
947 		uint64_t seg_offload_features =
948 				(1ULL << VIRTIO_NET_F_HOST_TSO4) |
949 				(1ULL << VIRTIO_NET_F_HOST_TSO6) |
950 				(1ULL << VIRTIO_NET_F_HOST_UFO);
951 
952 		VHOST_LOG_CONFIG(path, INFO, "Linear buffers requested without external buffers,\n");
953 		VHOST_LOG_CONFIG(path, INFO, "disabling host segmentation offloading support\n");
954 		vsocket->supported_features &= ~seg_offload_features;
955 		vsocket->features &= ~seg_offload_features;
956 	}
957 
958 	if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
959 		vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
960 		vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
961 	}
962 
963 	if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
964 		vsocket->protocol_features &=
965 			~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
966 	} else {
967 #ifndef RTE_LIBRTE_VHOST_POSTCOPY
968 		VHOST_LOG_CONFIG(path, ERR, "Postcopy requested but not compiled\n");
969 		ret = -1;
970 		goto out_mutex;
971 #endif
972 	}
973 
974 	if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
975 		vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
976 		if (vsocket->reconnect && reconn_tid == 0) {
977 			if (vhost_user_reconnect_init() != 0)
978 				goto out_mutex;
979 		}
980 	} else {
981 		vsocket->is_server = true;
982 	}
983 	ret = create_unix_socket(vsocket);
984 	if (ret < 0) {
985 		goto out_mutex;
986 	}
987 
988 	vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
989 
990 	pthread_mutex_unlock(&vhost_user.mutex);
991 	return ret;
992 
993 out_mutex:
994 	if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
995 		VHOST_LOG_CONFIG(path, ERR, "failed to destroy connection mutex\n");
996 	}
997 out_free:
998 	vhost_user_socket_mem_free(vsocket);
999 out:
1000 	pthread_mutex_unlock(&vhost_user.mutex);
1001 
1002 	return ret;
1003 }
1004 
1005 static bool
1006 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
1007 {
1008 	int found = false;
1009 	struct vhost_user_reconnect *reconn, *next;
1010 
1011 	pthread_mutex_lock(&reconn_list.mutex);
1012 
1013 	for (reconn = TAILQ_FIRST(&reconn_list.head);
1014 	     reconn != NULL; reconn = next) {
1015 		next = TAILQ_NEXT(reconn, next);
1016 
1017 		if (reconn->vsocket == vsocket) {
1018 			TAILQ_REMOVE(&reconn_list.head, reconn, next);
1019 			close(reconn->fd);
1020 			free(reconn);
1021 			found = true;
1022 			break;
1023 		}
1024 	}
1025 	pthread_mutex_unlock(&reconn_list.mutex);
1026 	return found;
1027 }
1028 
1029 /**
1030  * Unregister the specified vhost socket
1031  */
1032 int
1033 rte_vhost_driver_unregister(const char *path)
1034 {
1035 	int i;
1036 	int count;
1037 	struct vhost_user_connection *conn, *next;
1038 
1039 	if (path == NULL)
1040 		return -1;
1041 
1042 again:
1043 	pthread_mutex_lock(&vhost_user.mutex);
1044 
1045 	for (i = 0; i < vhost_user.vsocket_cnt; i++) {
1046 		struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
1047 		if (strcmp(vsocket->path, path))
1048 			continue;
1049 
1050 		if (vsocket->is_server) {
1051 			/*
1052 			 * If r/wcb is executing, release vhost_user's
1053 			 * mutex lock, and try again since the r/wcb
1054 			 * may use the mutex lock.
1055 			 */
1056 			if (fdset_try_del(&vhost_user.fdset, vsocket->socket_fd) == -1) {
1057 				pthread_mutex_unlock(&vhost_user.mutex);
1058 				goto again;
1059 			}
1060 		} else if (vsocket->reconnect) {
1061 			vhost_user_remove_reconnect(vsocket);
1062 		}
1063 
1064 		pthread_mutex_lock(&vsocket->conn_mutex);
1065 		for (conn = TAILQ_FIRST(&vsocket->conn_list);
1066 			 conn != NULL;
1067 			 conn = next) {
1068 			next = TAILQ_NEXT(conn, next);
1069 
1070 			/*
1071 			 * If r/wcb is executing, release vsocket's
1072 			 * conn_mutex and vhost_user's mutex locks, and
1073 			 * try again since the r/wcb may use the
1074 			 * conn_mutex and mutex locks.
1075 			 */
1076 			if (fdset_try_del(&vhost_user.fdset,
1077 					  conn->connfd) == -1) {
1078 				pthread_mutex_unlock(&vsocket->conn_mutex);
1079 				pthread_mutex_unlock(&vhost_user.mutex);
1080 				goto again;
1081 			}
1082 
1083 			VHOST_LOG_CONFIG(path, INFO, "free connfd %d\n", conn->connfd);
1084 			close(conn->connfd);
1085 			vhost_destroy_device(conn->vid);
1086 			TAILQ_REMOVE(&vsocket->conn_list, conn, next);
1087 			free(conn);
1088 		}
1089 		pthread_mutex_unlock(&vsocket->conn_mutex);
1090 
1091 		if (vsocket->is_server) {
1092 			close(vsocket->socket_fd);
1093 			unlink(path);
1094 		}
1095 
1096 		pthread_mutex_destroy(&vsocket->conn_mutex);
1097 		vhost_user_socket_mem_free(vsocket);
1098 
1099 		count = --vhost_user.vsocket_cnt;
1100 		vhost_user.vsockets[i] = vhost_user.vsockets[count];
1101 		vhost_user.vsockets[count] = NULL;
1102 		pthread_mutex_unlock(&vhost_user.mutex);
1103 		return 0;
1104 	}
1105 	pthread_mutex_unlock(&vhost_user.mutex);
1106 
1107 	return -1;
1108 }
1109 
1110 /*
1111  * Register ops so that we can add/remove device to data core.
1112  */
1113 int
1114 rte_vhost_driver_callback_register(const char *path,
1115 	struct rte_vhost_device_ops const * const ops)
1116 {
1117 	struct vhost_user_socket *vsocket;
1118 
1119 	pthread_mutex_lock(&vhost_user.mutex);
1120 	vsocket = find_vhost_user_socket(path);
1121 	if (vsocket)
1122 		vsocket->notify_ops = ops;
1123 	pthread_mutex_unlock(&vhost_user.mutex);
1124 
1125 	return vsocket ? 0 : -1;
1126 }
1127 
1128 struct rte_vhost_device_ops const *
1129 vhost_driver_callback_get(const char *path)
1130 {
1131 	struct vhost_user_socket *vsocket;
1132 
1133 	pthread_mutex_lock(&vhost_user.mutex);
1134 	vsocket = find_vhost_user_socket(path);
1135 	pthread_mutex_unlock(&vhost_user.mutex);
1136 
1137 	return vsocket ? vsocket->notify_ops : NULL;
1138 }
1139 
1140 int
1141 rte_vhost_driver_start(const char *path)
1142 {
1143 	struct vhost_user_socket *vsocket;
1144 	static pthread_t fdset_tid;
1145 
1146 	pthread_mutex_lock(&vhost_user.mutex);
1147 	vsocket = find_vhost_user_socket(path);
1148 	pthread_mutex_unlock(&vhost_user.mutex);
1149 
1150 	if (!vsocket)
1151 		return -1;
1152 
1153 	if (fdset_tid == 0) {
1154 		/**
1155 		 * create a pipe which will be waited by poll and notified to
1156 		 * rebuild the wait list of poll.
1157 		 */
1158 		if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1159 			VHOST_LOG_CONFIG(path, ERR, "failed to create pipe for vhost fdset\n");
1160 			return -1;
1161 		}
1162 
1163 		int ret = rte_ctrl_thread_create(&fdset_tid,
1164 			"vhost-events", NULL, fdset_event_dispatch,
1165 			&vhost_user.fdset);
1166 		if (ret != 0) {
1167 			VHOST_LOG_CONFIG(path, ERR, "failed to create fdset handling thread\n");
1168 			fdset_pipe_uninit(&vhost_user.fdset);
1169 			return -1;
1170 		}
1171 	}
1172 
1173 	if (vsocket->is_server)
1174 		return vhost_user_start_server(vsocket);
1175 	else
1176 		return vhost_user_start_client(vsocket);
1177 }
1178