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;
47 * The "supported_features" indicates the feature bits the
48 * vhost driver supports. The "features" indicates the feature
49 * bits after the rte_vhost_driver_features_disable/enable().
50 * It is also the final feature bits used for vhost-user
51 * features negotiation.
53 uint64_t supported_features;
56 uint64_t protocol_features;
59 * Device id to identify a specific backend device.
60 * It's set to -1 for the default software implementation.
61 * If valid, one socket can have 1 connection only.
65 struct vhost_device_ops const *notify_ops;
68 struct vhost_user_connection {
69 struct vhost_user_socket *vsocket;
73 TAILQ_ENTRY(vhost_user_connection) next;
76 #define MAX_VHOST_SOCKET 1024
78 struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
81 pthread_mutex_t mutex;
84 #define MAX_VIRTIO_BACKLOG 128
86 static void vhost_user_server_new_connection(int fd, void *data, int *remove);
87 static void vhost_user_read_cb(int fd, void *dat, int *remove);
88 static int create_unix_socket(struct vhost_user_socket *vsocket);
89 static int vhost_user_start_client(struct vhost_user_socket *vsocket);
91 static struct vhost_user vhost_user = {
93 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
94 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
95 .fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
99 .mutex = PTHREAD_MUTEX_INITIALIZER,
103 * return bytes# of read on success or negative val on failure. Update fdnum
104 * with number of fds read.
107 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int max_fds,
112 char control[CMSG_SPACE(max_fds * sizeof(int))];
113 struct cmsghdr *cmsg;
119 memset(&msgh, 0, sizeof(msgh));
121 iov.iov_len = buflen;
125 msgh.msg_control = control;
126 msgh.msg_controllen = sizeof(control);
128 ret = recvmsg(sockfd, &msgh, 0);
130 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
134 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
135 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
139 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
140 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
141 if ((cmsg->cmsg_level == SOL_SOCKET) &&
142 (cmsg->cmsg_type == SCM_RIGHTS)) {
143 got_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
145 memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
150 /* Clear out unused file descriptors */
151 while (got_fds < max_fds)
158 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
163 size_t fdsize = fd_num * sizeof(int);
164 char control[CMSG_SPACE(fdsize)];
165 struct cmsghdr *cmsg;
168 memset(&msgh, 0, sizeof(msgh));
170 iov.iov_len = buflen;
175 if (fds && fd_num > 0) {
176 msgh.msg_control = control;
177 msgh.msg_controllen = sizeof(control);
178 cmsg = CMSG_FIRSTHDR(&msgh);
180 RTE_LOG(ERR, VHOST_CONFIG, "cmsg == NULL\n");
184 cmsg->cmsg_len = CMSG_LEN(fdsize);
185 cmsg->cmsg_level = SOL_SOCKET;
186 cmsg->cmsg_type = SCM_RIGHTS;
187 memcpy(CMSG_DATA(cmsg), fds, fdsize);
189 msgh.msg_control = NULL;
190 msgh.msg_controllen = 0;
194 ret = sendmsg(sockfd, &msgh, MSG_NOSIGNAL);
195 } while (ret < 0 && errno == EINTR);
198 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
206 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
210 struct vhost_user_connection *conn;
216 conn = malloc(sizeof(*conn));
222 vid = vhost_new_device();
227 size = strnlen(vsocket->path, PATH_MAX);
228 vhost_set_ifname(vid, vsocket->path, size);
230 vhost_set_builtin_virtio_net(vid, vsocket->use_builtin_virtio_net);
232 vhost_attach_vdpa_device(vid, vsocket->vdpa_dev_id);
234 if (vsocket->dequeue_zero_copy)
235 vhost_enable_dequeue_zero_copy(vid);
238 vhost_enable_extbuf(vid);
240 if (vsocket->linearbuf)
241 vhost_enable_linearbuf(vid);
243 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
245 if (vsocket->notify_ops->new_connection) {
246 ret = vsocket->notify_ops->new_connection(vid);
248 RTE_LOG(ERR, VHOST_CONFIG,
249 "failed to add vhost user connection with fd %d\n",
256 conn->vsocket = vsocket;
258 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
261 RTE_LOG(ERR, VHOST_CONFIG,
262 "failed to add fd %d into vhost server fdset\n",
265 if (vsocket->notify_ops->destroy_connection)
266 vsocket->notify_ops->destroy_connection(conn->vid);
271 pthread_mutex_lock(&vsocket->conn_mutex);
272 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
273 pthread_mutex_unlock(&vsocket->conn_mutex);
275 fdset_pipe_notify(&vhost_user.fdset);
279 vhost_destroy_device(vid);
285 /* call back when there is new vhost-user connection from client */
287 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
289 struct vhost_user_socket *vsocket = dat;
291 fd = accept(fd, NULL, NULL);
295 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
296 vhost_user_add_connection(fd, vsocket);
300 vhost_user_read_cb(int connfd, void *dat, int *remove)
302 struct vhost_user_connection *conn = dat;
303 struct vhost_user_socket *vsocket = conn->vsocket;
306 ret = vhost_user_msg_handler(conn->vid, connfd);
308 struct virtio_net *dev = get_device(conn->vid);
314 vhost_destroy_device_notify(dev);
316 if (vsocket->notify_ops->destroy_connection)
317 vsocket->notify_ops->destroy_connection(conn->vid);
319 vhost_destroy_device(conn->vid);
321 pthread_mutex_lock(&vsocket->conn_mutex);
322 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
323 pthread_mutex_unlock(&vsocket->conn_mutex);
327 if (vsocket->reconnect) {
328 create_unix_socket(vsocket);
329 vhost_user_start_client(vsocket);
335 create_unix_socket(struct vhost_user_socket *vsocket)
338 struct sockaddr_un *un = &vsocket->un;
340 fd = socket(AF_UNIX, SOCK_STREAM, 0);
343 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
344 vsocket->is_server ? "server" : "client", fd);
346 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
347 RTE_LOG(ERR, VHOST_CONFIG,
348 "vhost-user: can't set nonblocking mode for socket, fd: "
349 "%d (%s)\n", fd, strerror(errno));
354 memset(un, 0, sizeof(*un));
355 un->sun_family = AF_UNIX;
356 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
357 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
359 vsocket->socket_fd = fd;
364 vhost_user_start_server(struct vhost_user_socket *vsocket)
367 int fd = vsocket->socket_fd;
368 const char *path = vsocket->path;
371 * bind () may fail if the socket file with the same name already
372 * exists. But the library obviously should not delete the file
373 * provided by the user, since we can not be sure that it is not
374 * being used by other applications. Moreover, many applications form
375 * socket names based on user input, which is prone to errors.
377 * The user must ensure that the socket does not exist before
378 * registering the vhost driver in server mode.
380 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
382 RTE_LOG(ERR, VHOST_CONFIG,
383 "failed to bind to %s: %s; remove it and try again\n",
384 path, strerror(errno));
387 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
389 ret = listen(fd, MAX_VIRTIO_BACKLOG);
393 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
396 RTE_LOG(ERR, VHOST_CONFIG,
397 "failed to add listen fd %d to vhost server fdset\n",
409 struct vhost_user_reconnect {
410 struct sockaddr_un un;
412 struct vhost_user_socket *vsocket;
414 TAILQ_ENTRY(vhost_user_reconnect) next;
417 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
418 struct vhost_user_reconnect_list {
419 struct vhost_user_reconnect_tailq_list head;
420 pthread_mutex_t mutex;
423 static struct vhost_user_reconnect_list reconn_list;
424 static pthread_t reconn_tid;
427 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
431 ret = connect(fd, un, sz);
432 if (ret < 0 && errno != EISCONN)
435 flags = fcntl(fd, F_GETFL, 0);
437 RTE_LOG(ERR, VHOST_CONFIG,
438 "can't get flags for connfd %d\n", fd);
441 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
442 RTE_LOG(ERR, VHOST_CONFIG,
443 "can't disable nonblocking on fd %d\n", fd);
450 vhost_user_client_reconnect(void *arg __rte_unused)
453 struct vhost_user_reconnect *reconn, *next;
456 pthread_mutex_lock(&reconn_list.mutex);
459 * An equal implementation of TAILQ_FOREACH_SAFE,
460 * which does not exist on all platforms.
462 for (reconn = TAILQ_FIRST(&reconn_list.head);
463 reconn != NULL; reconn = next) {
464 next = TAILQ_NEXT(reconn, next);
466 ret = vhost_user_connect_nonblock(reconn->fd,
467 (struct sockaddr *)&reconn->un,
471 RTE_LOG(ERR, VHOST_CONFIG,
472 "reconnection for fd %d failed\n",
479 RTE_LOG(INFO, VHOST_CONFIG,
480 "%s: connected\n", reconn->vsocket->path);
481 vhost_user_add_connection(reconn->fd, reconn->vsocket);
483 TAILQ_REMOVE(&reconn_list.head, reconn, next);
487 pthread_mutex_unlock(&reconn_list.mutex);
495 vhost_user_reconnect_init(void)
499 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
501 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
504 TAILQ_INIT(&reconn_list.head);
506 ret = rte_ctrl_thread_create(&reconn_tid, "vhost_reconn", NULL,
507 vhost_user_client_reconnect, NULL);
509 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
510 if (pthread_mutex_destroy(&reconn_list.mutex)) {
511 RTE_LOG(ERR, VHOST_CONFIG,
512 "failed to destroy reconnect mutex");
520 vhost_user_start_client(struct vhost_user_socket *vsocket)
523 int fd = vsocket->socket_fd;
524 const char *path = vsocket->path;
525 struct vhost_user_reconnect *reconn;
527 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
528 sizeof(vsocket->un));
530 vhost_user_add_connection(fd, vsocket);
534 RTE_LOG(WARNING, VHOST_CONFIG,
535 "failed to connect to %s: %s\n",
536 path, strerror(errno));
538 if (ret == -2 || !vsocket->reconnect) {
543 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
544 reconn = malloc(sizeof(*reconn));
545 if (reconn == NULL) {
546 RTE_LOG(ERR, VHOST_CONFIG,
547 "failed to allocate memory for reconnect\n");
551 reconn->un = vsocket->un;
553 reconn->vsocket = vsocket;
554 pthread_mutex_lock(&reconn_list.mutex);
555 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
556 pthread_mutex_unlock(&reconn_list.mutex);
561 static struct vhost_user_socket *
562 find_vhost_user_socket(const char *path)
569 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
570 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
572 if (!strcmp(vsocket->path, path))
580 rte_vhost_driver_attach_vdpa_device(const char *path, int did)
582 struct vhost_user_socket *vsocket;
584 if (rte_vdpa_get_device(did) == NULL || path == NULL)
587 pthread_mutex_lock(&vhost_user.mutex);
588 vsocket = find_vhost_user_socket(path);
590 vsocket->vdpa_dev_id = did;
591 pthread_mutex_unlock(&vhost_user.mutex);
593 return vsocket ? 0 : -1;
597 rte_vhost_driver_detach_vdpa_device(const char *path)
599 struct vhost_user_socket *vsocket;
601 pthread_mutex_lock(&vhost_user.mutex);
602 vsocket = find_vhost_user_socket(path);
604 vsocket->vdpa_dev_id = -1;
605 pthread_mutex_unlock(&vhost_user.mutex);
607 return vsocket ? 0 : -1;
611 rte_vhost_driver_get_vdpa_device_id(const char *path)
613 struct vhost_user_socket *vsocket;
616 pthread_mutex_lock(&vhost_user.mutex);
617 vsocket = find_vhost_user_socket(path);
619 did = vsocket->vdpa_dev_id;
620 pthread_mutex_unlock(&vhost_user.mutex);
626 rte_vhost_driver_disable_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 /* Note that use_builtin_virtio_net is not affected by this function
634 * since callers may want to selectively disable features of the
635 * built-in vhost net device backend.
639 vsocket->features &= ~features;
640 pthread_mutex_unlock(&vhost_user.mutex);
642 return vsocket ? 0 : -1;
646 rte_vhost_driver_enable_features(const char *path, uint64_t features)
648 struct vhost_user_socket *vsocket;
650 pthread_mutex_lock(&vhost_user.mutex);
651 vsocket = find_vhost_user_socket(path);
653 if ((vsocket->supported_features & features) != features) {
655 * trying to enable features the driver doesn't
658 pthread_mutex_unlock(&vhost_user.mutex);
661 vsocket->features |= features;
663 pthread_mutex_unlock(&vhost_user.mutex);
665 return vsocket ? 0 : -1;
669 rte_vhost_driver_set_features(const char *path, uint64_t features)
671 struct vhost_user_socket *vsocket;
673 pthread_mutex_lock(&vhost_user.mutex);
674 vsocket = find_vhost_user_socket(path);
676 vsocket->supported_features = features;
677 vsocket->features = features;
679 /* Anyone setting feature bits is implementing their own vhost
682 vsocket->use_builtin_virtio_net = false;
684 pthread_mutex_unlock(&vhost_user.mutex);
686 return vsocket ? 0 : -1;
690 rte_vhost_driver_get_features(const char *path, uint64_t *features)
692 struct vhost_user_socket *vsocket;
693 uint64_t vdpa_features;
694 struct rte_vdpa_device *vdpa_dev;
698 pthread_mutex_lock(&vhost_user.mutex);
699 vsocket = find_vhost_user_socket(path);
701 RTE_LOG(ERR, VHOST_CONFIG,
702 "socket file %s is not registered yet.\n", path);
707 did = vsocket->vdpa_dev_id;
708 vdpa_dev = rte_vdpa_get_device(did);
709 if (!vdpa_dev || !vdpa_dev->ops->get_features) {
710 *features = vsocket->features;
714 if (vdpa_dev->ops->get_features(did, &vdpa_features) < 0) {
715 RTE_LOG(ERR, VHOST_CONFIG,
716 "failed to get vdpa features "
717 "for socket file %s.\n", path);
722 *features = vsocket->features & vdpa_features;
725 pthread_mutex_unlock(&vhost_user.mutex);
730 rte_vhost_driver_set_protocol_features(const char *path,
731 uint64_t protocol_features)
733 struct vhost_user_socket *vsocket;
735 pthread_mutex_lock(&vhost_user.mutex);
736 vsocket = find_vhost_user_socket(path);
738 vsocket->protocol_features = protocol_features;
739 pthread_mutex_unlock(&vhost_user.mutex);
740 return vsocket ? 0 : -1;
744 rte_vhost_driver_get_protocol_features(const char *path,
745 uint64_t *protocol_features)
747 struct vhost_user_socket *vsocket;
748 uint64_t vdpa_protocol_features;
749 struct rte_vdpa_device *vdpa_dev;
753 pthread_mutex_lock(&vhost_user.mutex);
754 vsocket = find_vhost_user_socket(path);
756 RTE_LOG(ERR, VHOST_CONFIG,
757 "socket file %s is not registered yet.\n", path);
762 did = vsocket->vdpa_dev_id;
763 vdpa_dev = rte_vdpa_get_device(did);
764 if (!vdpa_dev || !vdpa_dev->ops->get_protocol_features) {
765 *protocol_features = vsocket->protocol_features;
769 if (vdpa_dev->ops->get_protocol_features(did,
770 &vdpa_protocol_features) < 0) {
771 RTE_LOG(ERR, VHOST_CONFIG,
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;
795 pthread_mutex_lock(&vhost_user.mutex);
796 vsocket = find_vhost_user_socket(path);
798 RTE_LOG(ERR, VHOST_CONFIG,
799 "socket file %s is not registered yet.\n", path);
804 did = vsocket->vdpa_dev_id;
805 vdpa_dev = rte_vdpa_get_device(did);
806 if (!vdpa_dev || !vdpa_dev->ops->get_queue_num) {
807 *queue_num = VHOST_MAX_QUEUE_PAIRS;
811 if (vdpa_dev->ops->get_queue_num(did, &vdpa_queue_num) < 0) {
812 RTE_LOG(ERR, VHOST_CONFIG,
813 "failed to get vdpa queue number "
814 "for socket file %s.\n", path);
819 *queue_num = RTE_MIN((uint32_t)VHOST_MAX_QUEUE_PAIRS, vdpa_queue_num);
822 pthread_mutex_unlock(&vhost_user.mutex);
827 vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
829 if (vsocket && vsocket->path) {
831 vsocket->path = NULL;
841 * Register a new vhost-user socket; here we could act as server
842 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
846 rte_vhost_driver_register(const char *path, uint64_t flags)
849 struct vhost_user_socket *vsocket;
854 pthread_mutex_lock(&vhost_user.mutex);
856 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
857 RTE_LOG(ERR, VHOST_CONFIG,
858 "error: the number of vhost sockets reaches maximum\n");
862 vsocket = malloc(sizeof(struct vhost_user_socket));
865 memset(vsocket, 0, sizeof(struct vhost_user_socket));
866 vsocket->path = strdup(path);
867 if (vsocket->path == NULL) {
868 RTE_LOG(ERR, VHOST_CONFIG,
869 "error: failed to copy socket path string\n");
870 vhost_user_socket_mem_free(vsocket);
873 TAILQ_INIT(&vsocket->conn_list);
874 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
876 RTE_LOG(ERR, VHOST_CONFIG,
877 "error: failed to init connection mutex\n");
880 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
881 vsocket->extbuf = flags & RTE_VHOST_USER_EXTBUF_SUPPORT;
882 vsocket->linearbuf = flags & RTE_VHOST_USER_LINEARBUF_SUPPORT;
884 if (vsocket->dequeue_zero_copy &&
885 (flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
886 RTE_LOG(ERR, VHOST_CONFIG,
887 "error: enabling dequeue zero copy and IOMMU features "
888 "simultaneously is not supported\n");
893 * Set the supported features correctly for the builtin vhost-user
896 * Applications know nothing about features the builtin virtio net
897 * driver (virtio_net.c) supports, thus it's not possible for them
898 * to invoke rte_vhost_driver_set_features(). To workaround it, here
899 * we set it unconditionally. If the application want to implement
900 * another vhost-user driver (say SCSI), it should call the
901 * rte_vhost_driver_set_features(), which will overwrite following
904 vsocket->use_builtin_virtio_net = true;
905 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
906 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
907 vsocket->protocol_features = VHOST_USER_PROTOCOL_FEATURES;
910 * Dequeue zero copy can't assure descriptors returned in order.
911 * Also, it requires that the guest memory is populated, which is
912 * not compatible with postcopy.
914 if (vsocket->dequeue_zero_copy) {
915 if (vsocket->extbuf) {
916 RTE_LOG(ERR, VHOST_CONFIG,
917 "error: zero copy is incompatible with external buffers\n");
921 if (vsocket->linearbuf) {
922 RTE_LOG(ERR, VHOST_CONFIG,
923 "error: zero copy is incompatible with linear buffers\n");
927 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IN_ORDER);
928 vsocket->features &= ~(1ULL << VIRTIO_F_IN_ORDER);
930 RTE_LOG(INFO, VHOST_CONFIG,
931 "Dequeue zero copy requested, disabling postcopy support\n");
932 vsocket->protocol_features &=
933 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
936 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
937 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
938 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
941 if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
942 vsocket->protocol_features &=
943 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
945 #ifndef RTE_LIBRTE_VHOST_POSTCOPY
946 RTE_LOG(ERR, VHOST_CONFIG,
947 "Postcopy requested but not compiled\n");
953 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
954 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
955 if (vsocket->reconnect && reconn_tid == 0) {
956 if (vhost_user_reconnect_init() != 0)
960 vsocket->is_server = true;
962 ret = create_unix_socket(vsocket);
967 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
969 pthread_mutex_unlock(&vhost_user.mutex);
973 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
974 RTE_LOG(ERR, VHOST_CONFIG,
975 "error: failed to destroy connection mutex\n");
978 vhost_user_socket_mem_free(vsocket);
980 pthread_mutex_unlock(&vhost_user.mutex);
986 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
989 struct vhost_user_reconnect *reconn, *next;
991 pthread_mutex_lock(&reconn_list.mutex);
993 for (reconn = TAILQ_FIRST(&reconn_list.head);
994 reconn != NULL; reconn = next) {
995 next = TAILQ_NEXT(reconn, next);
997 if (reconn->vsocket == vsocket) {
998 TAILQ_REMOVE(&reconn_list.head, reconn, next);
1005 pthread_mutex_unlock(&reconn_list.mutex);
1010 * Unregister the specified vhost socket
1013 rte_vhost_driver_unregister(const char *path)
1017 struct vhost_user_connection *conn, *next;
1023 pthread_mutex_lock(&vhost_user.mutex);
1025 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
1026 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
1028 if (!strcmp(vsocket->path, path)) {
1029 pthread_mutex_lock(&vsocket->conn_mutex);
1030 for (conn = TAILQ_FIRST(&vsocket->conn_list);
1033 next = TAILQ_NEXT(conn, next);
1036 * If r/wcb is executing, release the
1037 * conn_mutex lock, and try again since
1038 * the r/wcb may use the conn_mutex lock.
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 RTE_LOG(INFO, VHOST_CONFIG,
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) {
1059 fdset_del(&vhost_user.fdset,
1060 vsocket->socket_fd);
1061 close(vsocket->socket_fd);
1063 } else if (vsocket->reconnect) {
1064 vhost_user_remove_reconnect(vsocket);
1067 pthread_mutex_destroy(&vsocket->conn_mutex);
1068 vhost_user_socket_mem_free(vsocket);
1070 count = --vhost_user.vsocket_cnt;
1071 vhost_user.vsockets[i] = vhost_user.vsockets[count];
1072 vhost_user.vsockets[count] = NULL;
1073 pthread_mutex_unlock(&vhost_user.mutex);
1078 pthread_mutex_unlock(&vhost_user.mutex);
1084 * Register ops so that we can add/remove device to data core.
1087 rte_vhost_driver_callback_register(const char *path,
1088 struct vhost_device_ops const * const ops)
1090 struct vhost_user_socket *vsocket;
1092 pthread_mutex_lock(&vhost_user.mutex);
1093 vsocket = find_vhost_user_socket(path);
1095 vsocket->notify_ops = ops;
1096 pthread_mutex_unlock(&vhost_user.mutex);
1098 return vsocket ? 0 : -1;
1101 struct vhost_device_ops const *
1102 vhost_driver_callback_get(const char *path)
1104 struct vhost_user_socket *vsocket;
1106 pthread_mutex_lock(&vhost_user.mutex);
1107 vsocket = find_vhost_user_socket(path);
1108 pthread_mutex_unlock(&vhost_user.mutex);
1110 return vsocket ? vsocket->notify_ops : NULL;
1114 rte_vhost_driver_start(const char *path)
1116 struct vhost_user_socket *vsocket;
1117 static pthread_t fdset_tid;
1119 pthread_mutex_lock(&vhost_user.mutex);
1120 vsocket = find_vhost_user_socket(path);
1121 pthread_mutex_unlock(&vhost_user.mutex);
1126 if (fdset_tid == 0) {
1128 * create a pipe which will be waited by poll and notified to
1129 * rebuild the wait list of poll.
1131 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1132 RTE_LOG(ERR, VHOST_CONFIG,
1133 "failed to create pipe for vhost fdset\n");
1137 int ret = rte_ctrl_thread_create(&fdset_tid,
1138 "vhost-events", NULL, fdset_event_dispatch,
1141 RTE_LOG(ERR, VHOST_CONFIG,
1142 "failed to create fdset handling thread");
1144 fdset_pipe_uninit(&vhost_user.fdset);
1149 if (vsocket->is_server)
1150 return vhost_user_start_server(vsocket);
1152 return vhost_user_start_client(vsocket);