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;
41 bool use_builtin_virtio_net;
45 bool net_compliant_ol_flags;
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.
54 uint64_t supported_features;
57 uint64_t protocol_features;
59 struct rte_vdpa_device *vdpa_dev;
61 struct vhost_device_ops const *notify_ops;
64 struct vhost_user_connection {
65 struct vhost_user_socket *vsocket;
69 TAILQ_ENTRY(vhost_user_connection) next;
72 #define MAX_VHOST_SOCKET 1024
74 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
77 pthread_mutex_t mutex;
80 #define MAX_VIRTIO_BACKLOG 128
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);
87 static struct vhost_user vhost_user = {
89 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
90 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
91 .fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
95 .mutex = PTHREAD_MUTEX_INITIALIZER,
99 * return bytes# of read on success or negative val on failure. Update fdnum
100 * with number of fds read.
103 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int max_fds,
108 char control[CMSG_SPACE(max_fds * sizeof(int))];
109 struct cmsghdr *cmsg;
115 memset(&msgh, 0, sizeof(msgh));
117 iov.iov_len = buflen;
121 msgh.msg_control = control;
122 msgh.msg_controllen = sizeof(control);
124 ret = recvmsg(sockfd, &msgh, 0);
127 VHOST_LOG_CONFIG(ERR, "recvmsg failed\n");
131 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
132 VHOST_LOG_CONFIG(ERR, "truncated 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 VHOST_LOG_CONFIG(ERR, "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 VHOST_LOG_CONFIG(ERR, "sendmsg error\n");
203 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
207 struct vhost_user_connection *conn;
209 struct virtio_net *dev;
214 conn = malloc(sizeof(*conn));
220 vid = vhost_new_device();
225 size = strnlen(vsocket->path, PATH_MAX);
226 vhost_set_ifname(vid, vsocket->path, size);
228 vhost_setup_virtio_net(vid, vsocket->use_builtin_virtio_net,
229 vsocket->net_compliant_ol_flags);
231 vhost_attach_vdpa_device(vid, vsocket->vdpa_dev);
234 vhost_enable_extbuf(vid);
236 if (vsocket->linearbuf)
237 vhost_enable_linearbuf(vid);
239 if (vsocket->async_copy) {
240 dev = get_device(vid);
246 VHOST_LOG_CONFIG(INFO, "new device, handle is %d\n", vid);
248 if (vsocket->notify_ops->new_connection) {
249 ret = vsocket->notify_ops->new_connection(vid);
251 VHOST_LOG_CONFIG(ERR,
252 "failed to add vhost user connection with fd %d\n",
259 conn->vsocket = vsocket;
261 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
264 VHOST_LOG_CONFIG(ERR,
265 "failed to add fd %d into vhost server fdset\n",
268 if (vsocket->notify_ops->destroy_connection)
269 vsocket->notify_ops->destroy_connection(conn->vid);
274 pthread_mutex_lock(&vsocket->conn_mutex);
275 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
276 pthread_mutex_unlock(&vsocket->conn_mutex);
278 fdset_pipe_notify(&vhost_user.fdset);
282 vhost_destroy_device(vid);
288 /* call back when there is new vhost-user connection from client */
290 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
292 struct vhost_user_socket *vsocket = dat;
294 fd = accept(fd, NULL, NULL);
298 VHOST_LOG_CONFIG(INFO, "new vhost user connection is %d\n", fd);
299 vhost_user_add_connection(fd, vsocket);
303 vhost_user_read_cb(int connfd, void *dat, int *remove)
305 struct vhost_user_connection *conn = dat;
306 struct vhost_user_socket *vsocket = conn->vsocket;
309 ret = vhost_user_msg_handler(conn->vid, connfd);
311 struct virtio_net *dev = get_device(conn->vid);
317 vhost_destroy_device_notify(dev);
319 if (vsocket->notify_ops->destroy_connection)
320 vsocket->notify_ops->destroy_connection(conn->vid);
322 vhost_destroy_device(conn->vid);
324 if (vsocket->reconnect) {
325 create_unix_socket(vsocket);
326 vhost_user_start_client(vsocket);
329 pthread_mutex_lock(&vsocket->conn_mutex);
330 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
331 pthread_mutex_unlock(&vsocket->conn_mutex);
338 create_unix_socket(struct vhost_user_socket *vsocket)
341 struct sockaddr_un *un = &vsocket->un;
343 fd = socket(AF_UNIX, SOCK_STREAM, 0);
346 VHOST_LOG_CONFIG(INFO, "vhost-user %s: socket created, fd: %d\n",
347 vsocket->is_server ? "server" : "client", fd);
349 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
350 VHOST_LOG_CONFIG(ERR,
351 "vhost-user: can't set nonblocking mode for socket, fd: "
352 "%d (%s)\n", fd, strerror(errno));
357 memset(un, 0, sizeof(*un));
358 un->sun_family = AF_UNIX;
359 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
360 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
362 vsocket->socket_fd = fd;
367 vhost_user_start_server(struct vhost_user_socket *vsocket)
370 int fd = vsocket->socket_fd;
371 const char *path = vsocket->path;
374 * bind () may fail if the socket file with the same name already
375 * exists. But the library obviously should not delete the file
376 * provided by the user, since we can not be sure that it is not
377 * being used by other applications. Moreover, many applications form
378 * socket names based on user input, which is prone to errors.
380 * The user must ensure that the socket does not exist before
381 * registering the vhost driver in server mode.
383 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
385 VHOST_LOG_CONFIG(ERR,
386 "failed to bind to %s: %s; remove it and try again\n",
387 path, strerror(errno));
390 VHOST_LOG_CONFIG(INFO, "bind to %s\n", path);
392 ret = listen(fd, MAX_VIRTIO_BACKLOG);
396 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
399 VHOST_LOG_CONFIG(ERR,
400 "failed to add listen fd %d to vhost server fdset\n",
412 struct vhost_user_reconnect {
413 struct sockaddr_un un;
415 struct vhost_user_socket *vsocket;
417 TAILQ_ENTRY(vhost_user_reconnect) next;
420 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
421 struct vhost_user_reconnect_list {
422 struct vhost_user_reconnect_tailq_list head;
423 pthread_mutex_t mutex;
426 static struct vhost_user_reconnect_list reconn_list;
427 static pthread_t reconn_tid;
430 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
434 ret = connect(fd, un, sz);
435 if (ret < 0 && errno != EISCONN)
438 flags = fcntl(fd, F_GETFL, 0);
440 VHOST_LOG_CONFIG(ERR,
441 "can't get flags for connfd %d\n", fd);
444 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
445 VHOST_LOG_CONFIG(ERR,
446 "can't disable nonblocking on fd %d\n", fd);
453 vhost_user_client_reconnect(void *arg __rte_unused)
456 struct vhost_user_reconnect *reconn, *next;
459 pthread_mutex_lock(&reconn_list.mutex);
462 * An equal implementation of TAILQ_FOREACH_SAFE,
463 * which does not exist on all platforms.
465 for (reconn = TAILQ_FIRST(&reconn_list.head);
466 reconn != NULL; reconn = next) {
467 next = TAILQ_NEXT(reconn, next);
469 ret = vhost_user_connect_nonblock(reconn->fd,
470 (struct sockaddr *)&reconn->un,
474 VHOST_LOG_CONFIG(ERR,
475 "reconnection for fd %d failed\n",
482 VHOST_LOG_CONFIG(INFO,
483 "%s: connected\n", reconn->vsocket->path);
484 vhost_user_add_connection(reconn->fd, reconn->vsocket);
486 TAILQ_REMOVE(&reconn_list.head, reconn, next);
490 pthread_mutex_unlock(&reconn_list.mutex);
498 vhost_user_reconnect_init(void)
502 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
504 VHOST_LOG_CONFIG(ERR, "failed to initialize mutex");
507 TAILQ_INIT(&reconn_list.head);
509 ret = rte_ctrl_thread_create(&reconn_tid, "vhost_reconn", NULL,
510 vhost_user_client_reconnect, NULL);
512 VHOST_LOG_CONFIG(ERR, "failed to create reconnect thread");
513 if (pthread_mutex_destroy(&reconn_list.mutex)) {
514 VHOST_LOG_CONFIG(ERR,
515 "failed to destroy reconnect mutex");
523 vhost_user_start_client(struct vhost_user_socket *vsocket)
526 int fd = vsocket->socket_fd;
527 const char *path = vsocket->path;
528 struct vhost_user_reconnect *reconn;
530 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
531 sizeof(vsocket->un));
533 vhost_user_add_connection(fd, vsocket);
537 VHOST_LOG_CONFIG(WARNING,
538 "failed to connect to %s: %s\n",
539 path, strerror(errno));
541 if (ret == -2 || !vsocket->reconnect) {
546 VHOST_LOG_CONFIG(INFO, "%s: reconnecting...\n", path);
547 reconn = malloc(sizeof(*reconn));
548 if (reconn == NULL) {
549 VHOST_LOG_CONFIG(ERR,
550 "failed to allocate memory for reconnect\n");
554 reconn->un = vsocket->un;
556 reconn->vsocket = vsocket;
557 pthread_mutex_lock(&reconn_list.mutex);
558 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
559 pthread_mutex_unlock(&reconn_list.mutex);
564 static struct vhost_user_socket *
565 find_vhost_user_socket(const char *path)
572 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
573 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
575 if (!strcmp(vsocket->path, path))
583 rte_vhost_driver_attach_vdpa_device(const char *path,
584 struct rte_vdpa_device *dev)
586 struct vhost_user_socket *vsocket;
588 if (dev == NULL || path == NULL)
591 pthread_mutex_lock(&vhost_user.mutex);
592 vsocket = find_vhost_user_socket(path);
594 vsocket->vdpa_dev = dev;
595 pthread_mutex_unlock(&vhost_user.mutex);
597 return vsocket ? 0 : -1;
601 rte_vhost_driver_detach_vdpa_device(const char *path)
603 struct vhost_user_socket *vsocket;
605 pthread_mutex_lock(&vhost_user.mutex);
606 vsocket = find_vhost_user_socket(path);
608 vsocket->vdpa_dev = NULL;
609 pthread_mutex_unlock(&vhost_user.mutex);
611 return vsocket ? 0 : -1;
614 struct rte_vdpa_device *
615 rte_vhost_driver_get_vdpa_device(const char *path)
617 struct vhost_user_socket *vsocket;
618 struct rte_vdpa_device *dev = NULL;
620 pthread_mutex_lock(&vhost_user.mutex);
621 vsocket = find_vhost_user_socket(path);
623 dev = vsocket->vdpa_dev;
624 pthread_mutex_unlock(&vhost_user.mutex);
630 rte_vhost_driver_disable_features(const char *path, uint64_t features)
632 struct vhost_user_socket *vsocket;
634 pthread_mutex_lock(&vhost_user.mutex);
635 vsocket = find_vhost_user_socket(path);
637 /* Note that use_builtin_virtio_net is not affected by this function
638 * since callers may want to selectively disable features of the
639 * built-in vhost net device backend.
643 vsocket->features &= ~features;
644 pthread_mutex_unlock(&vhost_user.mutex);
646 return vsocket ? 0 : -1;
650 rte_vhost_driver_enable_features(const char *path, uint64_t features)
652 struct vhost_user_socket *vsocket;
654 pthread_mutex_lock(&vhost_user.mutex);
655 vsocket = find_vhost_user_socket(path);
657 if ((vsocket->supported_features & features) != features) {
659 * trying to enable features the driver doesn't
662 pthread_mutex_unlock(&vhost_user.mutex);
665 vsocket->features |= features;
667 pthread_mutex_unlock(&vhost_user.mutex);
669 return vsocket ? 0 : -1;
673 rte_vhost_driver_set_features(const char *path, uint64_t features)
675 struct vhost_user_socket *vsocket;
677 pthread_mutex_lock(&vhost_user.mutex);
678 vsocket = find_vhost_user_socket(path);
680 vsocket->supported_features = features;
681 vsocket->features = features;
683 /* Anyone setting feature bits is implementing their own vhost
686 vsocket->use_builtin_virtio_net = false;
688 pthread_mutex_unlock(&vhost_user.mutex);
690 return vsocket ? 0 : -1;
694 rte_vhost_driver_get_features(const char *path, uint64_t *features)
696 struct vhost_user_socket *vsocket;
697 uint64_t vdpa_features;
698 struct rte_vdpa_device *vdpa_dev;
701 pthread_mutex_lock(&vhost_user.mutex);
702 vsocket = find_vhost_user_socket(path);
704 VHOST_LOG_CONFIG(ERR,
705 "socket file %s is not registered yet.\n", path);
710 vdpa_dev = vsocket->vdpa_dev;
712 *features = vsocket->features;
716 if (vdpa_dev->ops->get_features(vdpa_dev, &vdpa_features) < 0) {
717 VHOST_LOG_CONFIG(ERR,
718 "failed to get vdpa features "
719 "for socket file %s.\n", path);
724 *features = vsocket->features & vdpa_features;
727 pthread_mutex_unlock(&vhost_user.mutex);
732 rte_vhost_driver_set_protocol_features(const char *path,
733 uint64_t protocol_features)
735 struct vhost_user_socket *vsocket;
737 pthread_mutex_lock(&vhost_user.mutex);
738 vsocket = find_vhost_user_socket(path);
740 vsocket->protocol_features = protocol_features;
741 pthread_mutex_unlock(&vhost_user.mutex);
742 return vsocket ? 0 : -1;
746 rte_vhost_driver_get_protocol_features(const char *path,
747 uint64_t *protocol_features)
749 struct vhost_user_socket *vsocket;
750 uint64_t vdpa_protocol_features;
751 struct rte_vdpa_device *vdpa_dev;
754 pthread_mutex_lock(&vhost_user.mutex);
755 vsocket = find_vhost_user_socket(path);
757 VHOST_LOG_CONFIG(ERR,
758 "socket file %s is not registered yet.\n", path);
763 vdpa_dev = vsocket->vdpa_dev;
765 *protocol_features = vsocket->protocol_features;
769 if (vdpa_dev->ops->get_protocol_features(vdpa_dev,
770 &vdpa_protocol_features) < 0) {
771 VHOST_LOG_CONFIG(ERR,
772 "failed to get vdpa protocol features "
773 "for socket file %s.\n", path);
778 *protocol_features = vsocket->protocol_features
779 & vdpa_protocol_features;
782 pthread_mutex_unlock(&vhost_user.mutex);
787 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
789 struct vhost_user_socket *vsocket;
790 uint32_t vdpa_queue_num;
791 struct rte_vdpa_device *vdpa_dev;
794 pthread_mutex_lock(&vhost_user.mutex);
795 vsocket = find_vhost_user_socket(path);
797 VHOST_LOG_CONFIG(ERR,
798 "socket file %s is not registered yet.\n", path);
803 vdpa_dev = vsocket->vdpa_dev;
805 *queue_num = VHOST_MAX_QUEUE_PAIRS;
809 if (vdpa_dev->ops->get_queue_num(vdpa_dev, &vdpa_queue_num) < 0) {
810 VHOST_LOG_CONFIG(ERR,
811 "failed to get vdpa queue number "
812 "for socket file %s.\n", path);
817 *queue_num = RTE_MIN((uint32_t)VHOST_MAX_QUEUE_PAIRS, vdpa_queue_num);
820 pthread_mutex_unlock(&vhost_user.mutex);
825 vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
827 if (vsocket && vsocket->path) {
829 vsocket->path = NULL;
839 * Register a new vhost-user socket; here we could act as server
840 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
844 rte_vhost_driver_register(const char *path, uint64_t flags)
847 struct vhost_user_socket *vsocket;
852 pthread_mutex_lock(&vhost_user.mutex);
854 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
855 VHOST_LOG_CONFIG(ERR,
856 "error: the number of vhost sockets reaches maximum\n");
860 vsocket = malloc(sizeof(struct vhost_user_socket));
863 memset(vsocket, 0, sizeof(struct vhost_user_socket));
864 vsocket->path = strdup(path);
865 if (vsocket->path == NULL) {
866 VHOST_LOG_CONFIG(ERR,
867 "error: failed to copy socket path string\n");
868 vhost_user_socket_mem_free(vsocket);
871 TAILQ_INIT(&vsocket->conn_list);
872 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
874 VHOST_LOG_CONFIG(ERR,
875 "error: failed to init connection mutex\n");
878 vsocket->vdpa_dev = NULL;
879 vsocket->extbuf = flags & RTE_VHOST_USER_EXTBUF_SUPPORT;
880 vsocket->linearbuf = flags & RTE_VHOST_USER_LINEARBUF_SUPPORT;
881 vsocket->async_copy = flags & RTE_VHOST_USER_ASYNC_COPY;
882 vsocket->net_compliant_ol_flags = flags & RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS;
884 if (vsocket->async_copy &&
885 (flags & (RTE_VHOST_USER_IOMMU_SUPPORT |
886 RTE_VHOST_USER_POSTCOPY_SUPPORT))) {
887 VHOST_LOG_CONFIG(ERR, "error: enabling async copy and IOMMU "
888 "or post-copy feature simultaneously is not "
894 * Set the supported features correctly for the builtin vhost-user
897 * Applications know nothing about features the builtin virtio net
898 * driver (virtio_net.c) supports, thus it's not possible for them
899 * to invoke rte_vhost_driver_set_features(). To workaround it, here
900 * we set it unconditionally. If the application want to implement
901 * another vhost-user driver (say SCSI), it should call the
902 * rte_vhost_driver_set_features(), which will overwrite following
905 vsocket->use_builtin_virtio_net = true;
906 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
907 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
908 vsocket->protocol_features = VHOST_USER_PROTOCOL_FEATURES;
910 if (vsocket->async_copy) {
911 vsocket->supported_features &= ~(1ULL << VHOST_F_LOG_ALL);
912 vsocket->features &= ~(1ULL << VHOST_F_LOG_ALL);
913 VHOST_LOG_CONFIG(INFO,
914 "Logging feature is disabled in async copy mode\n");
918 * We'll not be able to receive a buffer from guest in linear mode
919 * without external buffer if it will not fit in a single mbuf, which is
920 * likely if segmentation offloading enabled.
922 if (vsocket->linearbuf && !vsocket->extbuf) {
923 uint64_t seg_offload_features =
924 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
925 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
926 (1ULL << VIRTIO_NET_F_HOST_UFO);
928 VHOST_LOG_CONFIG(INFO,
929 "Linear buffers requested without external buffers, "
930 "disabling host segmentation offloading support\n");
931 vsocket->supported_features &= ~seg_offload_features;
932 vsocket->features &= ~seg_offload_features;
935 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
936 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
937 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
940 if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
941 vsocket->protocol_features &=
942 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
944 #ifndef RTE_LIBRTE_VHOST_POSTCOPY
945 VHOST_LOG_CONFIG(ERR,
946 "Postcopy requested but not compiled\n");
952 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
953 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
954 if (vsocket->reconnect && reconn_tid == 0) {
955 if (vhost_user_reconnect_init() != 0)
959 vsocket->is_server = true;
961 ret = create_unix_socket(vsocket);
966 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
968 pthread_mutex_unlock(&vhost_user.mutex);
972 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
973 VHOST_LOG_CONFIG(ERR,
974 "error: failed to destroy connection mutex\n");
977 vhost_user_socket_mem_free(vsocket);
979 pthread_mutex_unlock(&vhost_user.mutex);
985 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
988 struct vhost_user_reconnect *reconn, *next;
990 pthread_mutex_lock(&reconn_list.mutex);
992 for (reconn = TAILQ_FIRST(&reconn_list.head);
993 reconn != NULL; reconn = next) {
994 next = TAILQ_NEXT(reconn, next);
996 if (reconn->vsocket == vsocket) {
997 TAILQ_REMOVE(&reconn_list.head, reconn, next);
1004 pthread_mutex_unlock(&reconn_list.mutex);
1009 * Unregister the specified vhost socket
1012 rte_vhost_driver_unregister(const char *path)
1016 struct vhost_user_connection *conn, *next;
1022 pthread_mutex_lock(&vhost_user.mutex);
1024 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
1025 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
1027 if (!strcmp(vsocket->path, path)) {
1028 pthread_mutex_lock(&vsocket->conn_mutex);
1029 for (conn = TAILQ_FIRST(&vsocket->conn_list);
1032 next = TAILQ_NEXT(conn, next);
1035 * If r/wcb is executing, release vsocket's
1036 * conn_mutex and vhost_user's mutex locks, and
1037 * try again since the r/wcb may use the
1038 * conn_mutex and mutex locks.
1040 if (fdset_try_del(&vhost_user.fdset,
1041 conn->connfd) == -1) {
1042 pthread_mutex_unlock(
1043 &vsocket->conn_mutex);
1044 pthread_mutex_unlock(&vhost_user.mutex);
1048 VHOST_LOG_CONFIG(INFO,
1049 "free connfd = %d for device '%s'\n",
1050 conn->connfd, path);
1051 close(conn->connfd);
1052 vhost_destroy_device(conn->vid);
1053 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
1056 pthread_mutex_unlock(&vsocket->conn_mutex);
1058 if (vsocket->is_server) {
1060 * If r/wcb is executing, release vhost_user's
1061 * mutex lock, and try again since the r/wcb
1062 * may use the mutex lock.
1064 if (fdset_try_del(&vhost_user.fdset,
1065 vsocket->socket_fd) == -1) {
1066 pthread_mutex_unlock(&vhost_user.mutex);
1070 close(vsocket->socket_fd);
1072 } else if (vsocket->reconnect) {
1073 vhost_user_remove_reconnect(vsocket);
1076 pthread_mutex_destroy(&vsocket->conn_mutex);
1077 vhost_user_socket_mem_free(vsocket);
1079 count = --vhost_user.vsocket_cnt;
1080 vhost_user.vsockets[i] = vhost_user.vsockets[count];
1081 vhost_user.vsockets[count] = NULL;
1082 pthread_mutex_unlock(&vhost_user.mutex);
1087 pthread_mutex_unlock(&vhost_user.mutex);
1093 * Register ops so that we can add/remove device to data core.
1096 rte_vhost_driver_callback_register(const char *path,
1097 struct vhost_device_ops const * const ops)
1099 struct vhost_user_socket *vsocket;
1101 pthread_mutex_lock(&vhost_user.mutex);
1102 vsocket = find_vhost_user_socket(path);
1104 vsocket->notify_ops = ops;
1105 pthread_mutex_unlock(&vhost_user.mutex);
1107 return vsocket ? 0 : -1;
1110 struct vhost_device_ops const *
1111 vhost_driver_callback_get(const char *path)
1113 struct vhost_user_socket *vsocket;
1115 pthread_mutex_lock(&vhost_user.mutex);
1116 vsocket = find_vhost_user_socket(path);
1117 pthread_mutex_unlock(&vhost_user.mutex);
1119 return vsocket ? vsocket->notify_ops : NULL;
1123 rte_vhost_driver_start(const char *path)
1125 struct vhost_user_socket *vsocket;
1126 static pthread_t fdset_tid;
1128 pthread_mutex_lock(&vhost_user.mutex);
1129 vsocket = find_vhost_user_socket(path);
1130 pthread_mutex_unlock(&vhost_user.mutex);
1135 if (fdset_tid == 0) {
1137 * create a pipe which will be waited by poll and notified to
1138 * rebuild the wait list of poll.
1140 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1141 VHOST_LOG_CONFIG(ERR,
1142 "failed to create pipe for vhost fdset\n");
1146 int ret = rte_ctrl_thread_create(&fdset_tid,
1147 "vhost-events", NULL, fdset_event_dispatch,
1150 VHOST_LOG_CONFIG(ERR,
1151 "failed to create fdset handling thread");
1153 fdset_pipe_uninit(&vhost_user.fdset);
1158 if (vsocket->is_server)
1159 return vhost_user_start_server(vsocket);
1161 return vhost_user_start_client(vsocket);