1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
11 #include <sys/types.h>
12 #include <sys/socket.h>
14 #include <sys/queue.h>
23 #include "vhost_user.h"
26 TAILQ_HEAD(vhost_user_connection_list, vhost_user_connection);
29 * Every time rte_vhost_driver_register() is invoked, an associated
30 * vhost_user_socket struct will be created.
32 struct vhost_user_socket {
33 struct vhost_user_connection_list conn_list;
34 pthread_mutex_t conn_mutex;
37 struct sockaddr_un un;
40 bool dequeue_zero_copy;
42 bool use_builtin_virtio_net;
45 * The "supported_features" indicates the feature bits the
46 * vhost driver supports. The "features" indicates the feature
47 * bits after the rte_vhost_driver_features_disable/enable().
48 * It is also the final feature bits used for vhost-user
49 * features negotiation.
51 uint64_t supported_features;
54 uint64_t protocol_features;
57 * Device id to identify a specific backend device.
58 * It's set to -1 for the default software implementation.
59 * If valid, one socket can have 1 connection only.
63 struct vhost_device_ops const *notify_ops;
66 struct vhost_user_connection {
67 struct vhost_user_socket *vsocket;
71 TAILQ_ENTRY(vhost_user_connection) next;
74 #define MAX_VHOST_SOCKET 1024
76 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
79 pthread_mutex_t mutex;
82 #define MAX_VIRTIO_BACKLOG 128
84 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
85 static void vhost_user_read_cb(int fd, void *dat, int *remove);
86 static int create_unix_socket(struct vhost_user_socket *vsocket);
87 static int vhost_user_start_client(struct vhost_user_socket *vsocket);
89 static struct vhost_user vhost_user = {
91 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
92 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
96 .mutex = PTHREAD_MUTEX_INITIALIZER,
100 * return bytes# of read on success or negative val on failure. Update fdnum
101 * with number of fds read.
104 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int max_fds,
109 char control[CMSG_SPACE(max_fds * sizeof(int))];
110 struct cmsghdr *cmsg;
116 memset(&msgh, 0, sizeof(msgh));
118 iov.iov_len = buflen;
122 msgh.msg_control = control;
123 msgh.msg_controllen = sizeof(control);
125 ret = recvmsg(sockfd, &msgh, 0);
127 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
131 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
132 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
136 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
137 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
138 if ((cmsg->cmsg_level == SOL_SOCKET) &&
139 (cmsg->cmsg_type == SCM_RIGHTS)) {
140 got_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
142 memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
147 /* Clear out unused file descriptors */
148 while (got_fds < max_fds)
155 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
160 size_t fdsize = fd_num * sizeof(int);
161 char control[CMSG_SPACE(fdsize)];
162 struct cmsghdr *cmsg;
165 memset(&msgh, 0, sizeof(msgh));
167 iov.iov_len = buflen;
172 if (fds && fd_num > 0) {
173 msgh.msg_control = control;
174 msgh.msg_controllen = sizeof(control);
175 cmsg = CMSG_FIRSTHDR(&msgh);
177 RTE_LOG(ERR, VHOST_CONFIG, "cmsg == NULL\n");
181 cmsg->cmsg_len = CMSG_LEN(fdsize);
182 cmsg->cmsg_level = SOL_SOCKET;
183 cmsg->cmsg_type = SCM_RIGHTS;
184 memcpy(CMSG_DATA(cmsg), fds, fdsize);
186 msgh.msg_control = NULL;
187 msgh.msg_controllen = 0;
191 ret = sendmsg(sockfd, &msgh, MSG_NOSIGNAL);
192 } while (ret < 0 && errno == EINTR);
195 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
203 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
207 struct vhost_user_connection *conn;
213 conn = malloc(sizeof(*conn));
219 vid = vhost_new_device();
224 size = strnlen(vsocket->path, PATH_MAX);
225 vhost_set_ifname(vid, vsocket->path, size);
227 vhost_set_builtin_virtio_net(vid, vsocket->use_builtin_virtio_net);
229 vhost_attach_vdpa_device(vid, vsocket->vdpa_dev_id);
231 if (vsocket->dequeue_zero_copy)
232 vhost_enable_dequeue_zero_copy(vid);
234 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
236 if (vsocket->notify_ops->new_connection) {
237 ret = vsocket->notify_ops->new_connection(vid);
239 RTE_LOG(ERR, VHOST_CONFIG,
240 "failed to add vhost user connection with fd %d\n",
247 conn->vsocket = vsocket;
249 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
252 RTE_LOG(ERR, VHOST_CONFIG,
253 "failed to add fd %d into vhost server fdset\n",
256 if (vsocket->notify_ops->destroy_connection)
257 vsocket->notify_ops->destroy_connection(conn->vid);
262 pthread_mutex_lock(&vsocket->conn_mutex);
263 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
264 pthread_mutex_unlock(&vsocket->conn_mutex);
266 fdset_pipe_notify(&vhost_user.fdset);
274 /* call back when there is new vhost-user connection from client */
276 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
278 struct vhost_user_socket *vsocket = dat;
280 fd = accept(fd, NULL, NULL);
284 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
285 vhost_user_add_connection(fd, vsocket);
289 vhost_user_read_cb(int connfd, void *dat, int *remove)
291 struct vhost_user_connection *conn = dat;
292 struct vhost_user_socket *vsocket = conn->vsocket;
295 ret = vhost_user_msg_handler(conn->vid, connfd);
299 vhost_destroy_device(conn->vid);
301 if (vsocket->notify_ops->destroy_connection)
302 vsocket->notify_ops->destroy_connection(conn->vid);
304 pthread_mutex_lock(&vsocket->conn_mutex);
305 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
306 pthread_mutex_unlock(&vsocket->conn_mutex);
310 if (vsocket->reconnect) {
311 create_unix_socket(vsocket);
312 vhost_user_start_client(vsocket);
318 create_unix_socket(struct vhost_user_socket *vsocket)
321 struct sockaddr_un *un = &vsocket->un;
323 fd = socket(AF_UNIX, SOCK_STREAM, 0);
326 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
327 vsocket->is_server ? "server" : "client", fd);
329 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
330 RTE_LOG(ERR, VHOST_CONFIG,
331 "vhost-user: can't set nonblocking mode for socket, fd: "
332 "%d (%s)\n", fd, strerror(errno));
337 memset(un, 0, sizeof(*un));
338 un->sun_family = AF_UNIX;
339 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
340 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
342 vsocket->socket_fd = fd;
347 vhost_user_start_server(struct vhost_user_socket *vsocket)
350 int fd = vsocket->socket_fd;
351 const char *path = vsocket->path;
354 * bind () may fail if the socket file with the same name already
355 * exists. But the library obviously should not delete the file
356 * provided by the user, since we can not be sure that it is not
357 * being used by other applications. Moreover, many applications form
358 * socket names based on user input, which is prone to errors.
360 * The user must ensure that the socket does not exist before
361 * registering the vhost driver in server mode.
363 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
365 RTE_LOG(ERR, VHOST_CONFIG,
366 "failed to bind to %s: %s; remove it and try again\n",
367 path, strerror(errno));
370 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
372 ret = listen(fd, MAX_VIRTIO_BACKLOG);
376 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
379 RTE_LOG(ERR, VHOST_CONFIG,
380 "failed to add listen fd %d to vhost server fdset\n",
392 struct vhost_user_reconnect {
393 struct sockaddr_un un;
395 struct vhost_user_socket *vsocket;
397 TAILQ_ENTRY(vhost_user_reconnect) next;
400 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
401 struct vhost_user_reconnect_list {
402 struct vhost_user_reconnect_tailq_list head;
403 pthread_mutex_t mutex;
406 static struct vhost_user_reconnect_list reconn_list;
407 static pthread_t reconn_tid;
410 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
414 ret = connect(fd, un, sz);
415 if (ret < 0 && errno != EISCONN)
418 flags = fcntl(fd, F_GETFL, 0);
420 RTE_LOG(ERR, VHOST_CONFIG,
421 "can't get flags for connfd %d\n", fd);
424 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
425 RTE_LOG(ERR, VHOST_CONFIG,
426 "can't disable nonblocking on fd %d\n", fd);
433 vhost_user_client_reconnect(void *arg __rte_unused)
436 struct vhost_user_reconnect *reconn, *next;
439 pthread_mutex_lock(&reconn_list.mutex);
442 * An equal implementation of TAILQ_FOREACH_SAFE,
443 * which does not exist on all platforms.
445 for (reconn = TAILQ_FIRST(&reconn_list.head);
446 reconn != NULL; reconn = next) {
447 next = TAILQ_NEXT(reconn, next);
449 ret = vhost_user_connect_nonblock(reconn->fd,
450 (struct sockaddr *)&reconn->un,
454 RTE_LOG(ERR, VHOST_CONFIG,
455 "reconnection for fd %d failed\n",
462 RTE_LOG(INFO, VHOST_CONFIG,
463 "%s: connected\n", reconn->vsocket->path);
464 vhost_user_add_connection(reconn->fd, reconn->vsocket);
466 TAILQ_REMOVE(&reconn_list.head, reconn, next);
470 pthread_mutex_unlock(&reconn_list.mutex);
478 vhost_user_reconnect_init(void)
482 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
484 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
487 TAILQ_INIT(&reconn_list.head);
489 ret = rte_ctrl_thread_create(&reconn_tid, "vhost_reconn", NULL,
490 vhost_user_client_reconnect, NULL);
492 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
493 if (pthread_mutex_destroy(&reconn_list.mutex)) {
494 RTE_LOG(ERR, VHOST_CONFIG,
495 "failed to destroy reconnect mutex");
503 vhost_user_start_client(struct vhost_user_socket *vsocket)
506 int fd = vsocket->socket_fd;
507 const char *path = vsocket->path;
508 struct vhost_user_reconnect *reconn;
510 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
511 sizeof(vsocket->un));
513 vhost_user_add_connection(fd, vsocket);
517 RTE_LOG(WARNING, VHOST_CONFIG,
518 "failed to connect to %s: %s\n",
519 path, strerror(errno));
521 if (ret == -2 || !vsocket->reconnect) {
526 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
527 reconn = malloc(sizeof(*reconn));
528 if (reconn == NULL) {
529 RTE_LOG(ERR, VHOST_CONFIG,
530 "failed to allocate memory for reconnect\n");
534 reconn->un = vsocket->un;
536 reconn->vsocket = vsocket;
537 pthread_mutex_lock(&reconn_list.mutex);
538 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
539 pthread_mutex_unlock(&reconn_list.mutex);
544 static struct vhost_user_socket *
545 find_vhost_user_socket(const char *path)
549 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
550 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
552 if (!strcmp(vsocket->path, path))
560 rte_vhost_driver_attach_vdpa_device(const char *path, int did)
562 struct vhost_user_socket *vsocket;
564 if (rte_vdpa_get_device(did) == NULL)
567 pthread_mutex_lock(&vhost_user.mutex);
568 vsocket = find_vhost_user_socket(path);
570 vsocket->vdpa_dev_id = did;
571 pthread_mutex_unlock(&vhost_user.mutex);
573 return vsocket ? 0 : -1;
577 rte_vhost_driver_detach_vdpa_device(const char *path)
579 struct vhost_user_socket *vsocket;
581 pthread_mutex_lock(&vhost_user.mutex);
582 vsocket = find_vhost_user_socket(path);
584 vsocket->vdpa_dev_id = -1;
585 pthread_mutex_unlock(&vhost_user.mutex);
587 return vsocket ? 0 : -1;
591 rte_vhost_driver_get_vdpa_device_id(const char *path)
593 struct vhost_user_socket *vsocket;
596 pthread_mutex_lock(&vhost_user.mutex);
597 vsocket = find_vhost_user_socket(path);
599 did = vsocket->vdpa_dev_id;
600 pthread_mutex_unlock(&vhost_user.mutex);
606 rte_vhost_driver_disable_features(const char *path, uint64_t features)
608 struct vhost_user_socket *vsocket;
610 pthread_mutex_lock(&vhost_user.mutex);
611 vsocket = find_vhost_user_socket(path);
613 /* Note that use_builtin_virtio_net is not affected by this function
614 * since callers may want to selectively disable features of the
615 * built-in vhost net device backend.
619 vsocket->features &= ~features;
620 pthread_mutex_unlock(&vhost_user.mutex);
622 return vsocket ? 0 : -1;
626 rte_vhost_driver_enable_features(const char *path, uint64_t features)
628 struct vhost_user_socket *vsocket;
630 pthread_mutex_lock(&vhost_user.mutex);
631 vsocket = find_vhost_user_socket(path);
633 if ((vsocket->supported_features & features) != features) {
635 * trying to enable features the driver doesn't
638 pthread_mutex_unlock(&vhost_user.mutex);
641 vsocket->features |= features;
643 pthread_mutex_unlock(&vhost_user.mutex);
645 return vsocket ? 0 : -1;
649 rte_vhost_driver_set_features(const char *path, uint64_t features)
651 struct vhost_user_socket *vsocket;
653 pthread_mutex_lock(&vhost_user.mutex);
654 vsocket = find_vhost_user_socket(path);
656 vsocket->supported_features = features;
657 vsocket->features = features;
659 /* Anyone setting feature bits is implementing their own vhost
662 vsocket->use_builtin_virtio_net = false;
664 pthread_mutex_unlock(&vhost_user.mutex);
666 return vsocket ? 0 : -1;
670 rte_vhost_driver_get_features(const char *path, uint64_t *features)
672 struct vhost_user_socket *vsocket;
673 uint64_t vdpa_features;
674 struct rte_vdpa_device *vdpa_dev;
678 pthread_mutex_lock(&vhost_user.mutex);
679 vsocket = find_vhost_user_socket(path);
681 RTE_LOG(ERR, VHOST_CONFIG,
682 "socket file %s is not registered yet.\n", path);
687 did = vsocket->vdpa_dev_id;
688 vdpa_dev = rte_vdpa_get_device(did);
689 if (!vdpa_dev || !vdpa_dev->ops->get_features) {
690 *features = vsocket->features;
694 if (vdpa_dev->ops->get_features(did, &vdpa_features) < 0) {
695 RTE_LOG(ERR, VHOST_CONFIG,
696 "failed to get vdpa features "
697 "for socket file %s.\n", path);
702 *features = vsocket->features & vdpa_features;
705 pthread_mutex_unlock(&vhost_user.mutex);
710 rte_vhost_driver_get_protocol_features(const char *path,
711 uint64_t *protocol_features)
713 struct vhost_user_socket *vsocket;
714 uint64_t vdpa_protocol_features;
715 struct rte_vdpa_device *vdpa_dev;
719 pthread_mutex_lock(&vhost_user.mutex);
720 vsocket = find_vhost_user_socket(path);
722 RTE_LOG(ERR, VHOST_CONFIG,
723 "socket file %s is not registered yet.\n", path);
728 did = vsocket->vdpa_dev_id;
729 vdpa_dev = rte_vdpa_get_device(did);
730 if (!vdpa_dev || !vdpa_dev->ops->get_protocol_features) {
731 *protocol_features = vsocket->protocol_features;
735 if (vdpa_dev->ops->get_protocol_features(did,
736 &vdpa_protocol_features) < 0) {
737 RTE_LOG(ERR, VHOST_CONFIG,
738 "failed to get vdpa protocol features "
739 "for socket file %s.\n", path);
744 *protocol_features = vsocket->protocol_features
745 & vdpa_protocol_features;
748 pthread_mutex_unlock(&vhost_user.mutex);
753 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
755 struct vhost_user_socket *vsocket;
756 uint32_t vdpa_queue_num;
757 struct rte_vdpa_device *vdpa_dev;
761 pthread_mutex_lock(&vhost_user.mutex);
762 vsocket = find_vhost_user_socket(path);
764 RTE_LOG(ERR, VHOST_CONFIG,
765 "socket file %s is not registered yet.\n", path);
770 did = vsocket->vdpa_dev_id;
771 vdpa_dev = rte_vdpa_get_device(did);
772 if (!vdpa_dev || !vdpa_dev->ops->get_queue_num) {
773 *queue_num = VHOST_MAX_QUEUE_PAIRS;
777 if (vdpa_dev->ops->get_queue_num(did, &vdpa_queue_num) < 0) {
778 RTE_LOG(ERR, VHOST_CONFIG,
779 "failed to get vdpa queue number "
780 "for socket file %s.\n", path);
785 *queue_num = RTE_MIN((uint32_t)VHOST_MAX_QUEUE_PAIRS, vdpa_queue_num);
788 pthread_mutex_unlock(&vhost_user.mutex);
793 vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
795 if (vsocket && vsocket->path) {
797 vsocket->path = NULL;
807 * Register a new vhost-user socket; here we could act as server
808 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
812 rte_vhost_driver_register(const char *path, uint64_t flags)
815 struct vhost_user_socket *vsocket;
820 pthread_mutex_lock(&vhost_user.mutex);
822 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
823 RTE_LOG(ERR, VHOST_CONFIG,
824 "error: the number of vhost sockets reaches maximum\n");
828 vsocket = malloc(sizeof(struct vhost_user_socket));
831 memset(vsocket, 0, sizeof(struct vhost_user_socket));
832 vsocket->path = strdup(path);
833 if (vsocket->path == NULL) {
834 RTE_LOG(ERR, VHOST_CONFIG,
835 "error: failed to copy socket path string\n");
836 vhost_user_socket_mem_free(vsocket);
839 TAILQ_INIT(&vsocket->conn_list);
840 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
842 RTE_LOG(ERR, VHOST_CONFIG,
843 "error: failed to init connection mutex\n");
846 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
849 * Set the supported features correctly for the builtin vhost-user
852 * Applications know nothing about features the builtin virtio net
853 * driver (virtio_net.c) supports, thus it's not possible for them
854 * to invoke rte_vhost_driver_set_features(). To workaround it, here
855 * we set it unconditionally. If the application want to implement
856 * another vhost-user driver (say SCSI), it should call the
857 * rte_vhost_driver_set_features(), which will overwrite following
860 vsocket->use_builtin_virtio_net = true;
861 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
862 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
863 vsocket->protocol_features = VHOST_USER_PROTOCOL_FEATURES;
866 * Dequeue zero copy can't assure descriptors returned in order.
867 * Also, it requires that the guest memory is populated, which is
868 * not compatible with postcopy.
870 if (vsocket->dequeue_zero_copy) {
871 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IN_ORDER);
872 vsocket->features &= ~(1ULL << VIRTIO_F_IN_ORDER);
874 RTE_LOG(INFO, VHOST_CONFIG,
875 "Dequeue zero copy requested, disabling postcopy support\n");
876 vsocket->protocol_features &=
877 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
880 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
881 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
882 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
885 if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
886 vsocket->protocol_features &=
887 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
889 #ifndef RTE_LIBRTE_VHOST_POSTCOPY
890 RTE_LOG(ERR, VHOST_CONFIG,
891 "Postcopy requested but not compiled\n");
897 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
898 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
899 if (vsocket->reconnect && reconn_tid == 0) {
900 if (vhost_user_reconnect_init() != 0)
904 vsocket->is_server = true;
906 ret = create_unix_socket(vsocket);
911 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
913 pthread_mutex_unlock(&vhost_user.mutex);
917 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
918 RTE_LOG(ERR, VHOST_CONFIG,
919 "error: failed to destroy connection mutex\n");
922 vhost_user_socket_mem_free(vsocket);
924 pthread_mutex_unlock(&vhost_user.mutex);
930 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
933 struct vhost_user_reconnect *reconn, *next;
935 pthread_mutex_lock(&reconn_list.mutex);
937 for (reconn = TAILQ_FIRST(&reconn_list.head);
938 reconn != NULL; reconn = next) {
939 next = TAILQ_NEXT(reconn, next);
941 if (reconn->vsocket == vsocket) {
942 TAILQ_REMOVE(&reconn_list.head, reconn, next);
949 pthread_mutex_unlock(&reconn_list.mutex);
954 * Unregister the specified vhost socket
957 rte_vhost_driver_unregister(const char *path)
961 struct vhost_user_connection *conn, *next;
963 pthread_mutex_lock(&vhost_user.mutex);
965 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
966 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
968 if (!strcmp(vsocket->path, path)) {
970 pthread_mutex_lock(&vsocket->conn_mutex);
971 for (conn = TAILQ_FIRST(&vsocket->conn_list);
974 next = TAILQ_NEXT(conn, next);
977 * If r/wcb is executing, release the
978 * conn_mutex lock, and try again since
979 * the r/wcb may use the conn_mutex lock.
981 if (fdset_try_del(&vhost_user.fdset,
982 conn->connfd) == -1) {
983 pthread_mutex_unlock(
984 &vsocket->conn_mutex);
988 RTE_LOG(INFO, VHOST_CONFIG,
989 "free connfd = %d for device '%s'\n",
992 vhost_destroy_device(conn->vid);
993 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
996 pthread_mutex_unlock(&vsocket->conn_mutex);
998 if (vsocket->is_server) {
999 fdset_del(&vhost_user.fdset,
1000 vsocket->socket_fd);
1001 close(vsocket->socket_fd);
1003 } else if (vsocket->reconnect) {
1004 vhost_user_remove_reconnect(vsocket);
1007 pthread_mutex_destroy(&vsocket->conn_mutex);
1008 vhost_user_socket_mem_free(vsocket);
1010 count = --vhost_user.vsocket_cnt;
1011 vhost_user.vsockets[i] = vhost_user.vsockets[count];
1012 vhost_user.vsockets[count] = NULL;
1013 pthread_mutex_unlock(&vhost_user.mutex);
1018 pthread_mutex_unlock(&vhost_user.mutex);
1024 * Register ops so that we can add/remove device to data core.
1027 rte_vhost_driver_callback_register(const char *path,
1028 struct vhost_device_ops const * const ops)
1030 struct vhost_user_socket *vsocket;
1032 pthread_mutex_lock(&vhost_user.mutex);
1033 vsocket = find_vhost_user_socket(path);
1035 vsocket->notify_ops = ops;
1036 pthread_mutex_unlock(&vhost_user.mutex);
1038 return vsocket ? 0 : -1;
1041 struct vhost_device_ops const *
1042 vhost_driver_callback_get(const char *path)
1044 struct vhost_user_socket *vsocket;
1046 pthread_mutex_lock(&vhost_user.mutex);
1047 vsocket = find_vhost_user_socket(path);
1048 pthread_mutex_unlock(&vhost_user.mutex);
1050 return vsocket ? vsocket->notify_ops : NULL;
1054 rte_vhost_driver_start(const char *path)
1056 struct vhost_user_socket *vsocket;
1057 static pthread_t fdset_tid;
1059 pthread_mutex_lock(&vhost_user.mutex);
1060 vsocket = find_vhost_user_socket(path);
1061 pthread_mutex_unlock(&vhost_user.mutex);
1066 if (fdset_tid == 0) {
1068 * create a pipe which will be waited by poll and notified to
1069 * rebuild the wait list of poll.
1071 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1072 RTE_LOG(ERR, VHOST_CONFIG,
1073 "failed to create pipe for vhost fdset\n");
1077 int ret = rte_ctrl_thread_create(&fdset_tid,
1078 "vhost-events", NULL, fdset_event_dispatch,
1081 RTE_LOG(ERR, VHOST_CONFIG,
1082 "failed to create fdset handling thread");
1084 fdset_pipe_uninit(&vhost_user.fdset);
1089 if (vsocket->is_server)
1090 return vhost_user_start_server(vsocket);
1092 return vhost_user_start_client(vsocket);