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,
93 .fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
97 .mutex = PTHREAD_MUTEX_INITIALIZER,
101 * return bytes# of read on success or negative val on failure. Update fdnum
102 * with number of fds read.
105 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int max_fds,
110 char control[CMSG_SPACE(max_fds * sizeof(int))];
111 struct cmsghdr *cmsg;
117 memset(&msgh, 0, sizeof(msgh));
119 iov.iov_len = buflen;
123 msgh.msg_control = control;
124 msgh.msg_controllen = sizeof(control);
126 ret = recvmsg(sockfd, &msgh, 0);
128 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
132 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
133 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
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);
143 memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
148 /* Clear out unused file descriptors */
149 while (got_fds < max_fds)
156 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
161 size_t fdsize = fd_num * sizeof(int);
162 char control[CMSG_SPACE(fdsize)];
163 struct cmsghdr *cmsg;
166 memset(&msgh, 0, sizeof(msgh));
168 iov.iov_len = buflen;
173 if (fds && fd_num > 0) {
174 msgh.msg_control = control;
175 msgh.msg_controllen = sizeof(control);
176 cmsg = CMSG_FIRSTHDR(&msgh);
178 RTE_LOG(ERR, VHOST_CONFIG, "cmsg == NULL\n");
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);
187 msgh.msg_control = NULL;
188 msgh.msg_controllen = 0;
192 ret = sendmsg(sockfd, &msgh, MSG_NOSIGNAL);
193 } while (ret < 0 && errno == EINTR);
196 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
204 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
208 struct vhost_user_connection *conn;
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_set_builtin_virtio_net(vid, vsocket->use_builtin_virtio_net);
230 vhost_attach_vdpa_device(vid, vsocket->vdpa_dev_id);
232 if (vsocket->dequeue_zero_copy)
233 vhost_enable_dequeue_zero_copy(vid);
235 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
237 if (vsocket->notify_ops->new_connection) {
238 ret = vsocket->notify_ops->new_connection(vid);
240 RTE_LOG(ERR, VHOST_CONFIG,
241 "failed to add vhost user connection with fd %d\n",
248 conn->vsocket = vsocket;
250 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
253 RTE_LOG(ERR, VHOST_CONFIG,
254 "failed to add fd %d into vhost server fdset\n",
257 if (vsocket->notify_ops->destroy_connection)
258 vsocket->notify_ops->destroy_connection(conn->vid);
263 pthread_mutex_lock(&vsocket->conn_mutex);
264 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
265 pthread_mutex_unlock(&vsocket->conn_mutex);
267 fdset_pipe_notify(&vhost_user.fdset);
271 vhost_destroy_device(vid);
277 /* call back when there is new vhost-user connection from client */
279 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
281 struct vhost_user_socket *vsocket = dat;
283 fd = accept(fd, NULL, NULL);
287 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
288 vhost_user_add_connection(fd, vsocket);
292 vhost_user_read_cb(int connfd, void *dat, int *remove)
294 struct vhost_user_connection *conn = dat;
295 struct vhost_user_socket *vsocket = conn->vsocket;
298 ret = vhost_user_msg_handler(conn->vid, connfd);
300 struct virtio_net *dev = get_device(conn->vid);
306 vhost_destroy_device_notify(dev);
308 if (vsocket->notify_ops->destroy_connection)
309 vsocket->notify_ops->destroy_connection(conn->vid);
311 vhost_destroy_device(conn->vid);
313 pthread_mutex_lock(&vsocket->conn_mutex);
314 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
315 pthread_mutex_unlock(&vsocket->conn_mutex);
319 if (vsocket->reconnect) {
320 create_unix_socket(vsocket);
321 vhost_user_start_client(vsocket);
327 create_unix_socket(struct vhost_user_socket *vsocket)
330 struct sockaddr_un *un = &vsocket->un;
332 fd = socket(AF_UNIX, SOCK_STREAM, 0);
335 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
336 vsocket->is_server ? "server" : "client", fd);
338 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
339 RTE_LOG(ERR, VHOST_CONFIG,
340 "vhost-user: can't set nonblocking mode for socket, fd: "
341 "%d (%s)\n", fd, strerror(errno));
346 memset(un, 0, sizeof(*un));
347 un->sun_family = AF_UNIX;
348 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
349 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
351 vsocket->socket_fd = fd;
356 vhost_user_start_server(struct vhost_user_socket *vsocket)
359 int fd = vsocket->socket_fd;
360 const char *path = vsocket->path;
363 * bind () may fail if the socket file with the same name already
364 * exists. But the library obviously should not delete the file
365 * provided by the user, since we can not be sure that it is not
366 * being used by other applications. Moreover, many applications form
367 * socket names based on user input, which is prone to errors.
369 * The user must ensure that the socket does not exist before
370 * registering the vhost driver in server mode.
372 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
374 RTE_LOG(ERR, VHOST_CONFIG,
375 "failed to bind to %s: %s; remove it and try again\n",
376 path, strerror(errno));
379 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
381 ret = listen(fd, MAX_VIRTIO_BACKLOG);
385 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
388 RTE_LOG(ERR, VHOST_CONFIG,
389 "failed to add listen fd %d to vhost server fdset\n",
401 struct vhost_user_reconnect {
402 struct sockaddr_un un;
404 struct vhost_user_socket *vsocket;
406 TAILQ_ENTRY(vhost_user_reconnect) next;
409 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
410 struct vhost_user_reconnect_list {
411 struct vhost_user_reconnect_tailq_list head;
412 pthread_mutex_t mutex;
415 static struct vhost_user_reconnect_list reconn_list;
416 static pthread_t reconn_tid;
419 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
423 ret = connect(fd, un, sz);
424 if (ret < 0 && errno != EISCONN)
427 flags = fcntl(fd, F_GETFL, 0);
429 RTE_LOG(ERR, VHOST_CONFIG,
430 "can't get flags for connfd %d\n", fd);
433 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
434 RTE_LOG(ERR, VHOST_CONFIG,
435 "can't disable nonblocking on fd %d\n", fd);
442 vhost_user_client_reconnect(void *arg __rte_unused)
445 struct vhost_user_reconnect *reconn, *next;
448 pthread_mutex_lock(&reconn_list.mutex);
451 * An equal implementation of TAILQ_FOREACH_SAFE,
452 * which does not exist on all platforms.
454 for (reconn = TAILQ_FIRST(&reconn_list.head);
455 reconn != NULL; reconn = next) {
456 next = TAILQ_NEXT(reconn, next);
458 ret = vhost_user_connect_nonblock(reconn->fd,
459 (struct sockaddr *)&reconn->un,
463 RTE_LOG(ERR, VHOST_CONFIG,
464 "reconnection for fd %d failed\n",
471 RTE_LOG(INFO, VHOST_CONFIG,
472 "%s: connected\n", reconn->vsocket->path);
473 vhost_user_add_connection(reconn->fd, reconn->vsocket);
475 TAILQ_REMOVE(&reconn_list.head, reconn, next);
479 pthread_mutex_unlock(&reconn_list.mutex);
487 vhost_user_reconnect_init(void)
491 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
493 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
496 TAILQ_INIT(&reconn_list.head);
498 ret = rte_ctrl_thread_create(&reconn_tid, "vhost_reconn", NULL,
499 vhost_user_client_reconnect, NULL);
501 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
502 if (pthread_mutex_destroy(&reconn_list.mutex)) {
503 RTE_LOG(ERR, VHOST_CONFIG,
504 "failed to destroy reconnect mutex");
512 vhost_user_start_client(struct vhost_user_socket *vsocket)
515 int fd = vsocket->socket_fd;
516 const char *path = vsocket->path;
517 struct vhost_user_reconnect *reconn;
519 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
520 sizeof(vsocket->un));
522 vhost_user_add_connection(fd, vsocket);
526 RTE_LOG(WARNING, VHOST_CONFIG,
527 "failed to connect to %s: %s\n",
528 path, strerror(errno));
530 if (ret == -2 || !vsocket->reconnect) {
535 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
536 reconn = malloc(sizeof(*reconn));
537 if (reconn == NULL) {
538 RTE_LOG(ERR, VHOST_CONFIG,
539 "failed to allocate memory for reconnect\n");
543 reconn->un = vsocket->un;
545 reconn->vsocket = vsocket;
546 pthread_mutex_lock(&reconn_list.mutex);
547 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
548 pthread_mutex_unlock(&reconn_list.mutex);
553 static struct vhost_user_socket *
554 find_vhost_user_socket(const char *path)
561 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
562 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
564 if (!strcmp(vsocket->path, path))
572 rte_vhost_driver_attach_vdpa_device(const char *path, int did)
574 struct vhost_user_socket *vsocket;
576 if (rte_vdpa_get_device(did) == NULL || path == NULL)
579 pthread_mutex_lock(&vhost_user.mutex);
580 vsocket = find_vhost_user_socket(path);
582 vsocket->vdpa_dev_id = did;
583 pthread_mutex_unlock(&vhost_user.mutex);
585 return vsocket ? 0 : -1;
589 rte_vhost_driver_detach_vdpa_device(const char *path)
591 struct vhost_user_socket *vsocket;
593 pthread_mutex_lock(&vhost_user.mutex);
594 vsocket = find_vhost_user_socket(path);
596 vsocket->vdpa_dev_id = -1;
597 pthread_mutex_unlock(&vhost_user.mutex);
599 return vsocket ? 0 : -1;
603 rte_vhost_driver_get_vdpa_device_id(const char *path)
605 struct vhost_user_socket *vsocket;
608 pthread_mutex_lock(&vhost_user.mutex);
609 vsocket = find_vhost_user_socket(path);
611 did = vsocket->vdpa_dev_id;
612 pthread_mutex_unlock(&vhost_user.mutex);
618 rte_vhost_driver_disable_features(const char *path, uint64_t features)
620 struct vhost_user_socket *vsocket;
622 pthread_mutex_lock(&vhost_user.mutex);
623 vsocket = find_vhost_user_socket(path);
625 /* Note that use_builtin_virtio_net is not affected by this function
626 * since callers may want to selectively disable features of the
627 * built-in vhost net device backend.
631 vsocket->features &= ~features;
632 pthread_mutex_unlock(&vhost_user.mutex);
634 return vsocket ? 0 : -1;
638 rte_vhost_driver_enable_features(const char *path, uint64_t features)
640 struct vhost_user_socket *vsocket;
642 pthread_mutex_lock(&vhost_user.mutex);
643 vsocket = find_vhost_user_socket(path);
645 if ((vsocket->supported_features & features) != features) {
647 * trying to enable features the driver doesn't
650 pthread_mutex_unlock(&vhost_user.mutex);
653 vsocket->features |= features;
655 pthread_mutex_unlock(&vhost_user.mutex);
657 return vsocket ? 0 : -1;
661 rte_vhost_driver_set_features(const char *path, uint64_t features)
663 struct vhost_user_socket *vsocket;
665 pthread_mutex_lock(&vhost_user.mutex);
666 vsocket = find_vhost_user_socket(path);
668 vsocket->supported_features = features;
669 vsocket->features = features;
671 /* Anyone setting feature bits is implementing their own vhost
674 vsocket->use_builtin_virtio_net = false;
676 pthread_mutex_unlock(&vhost_user.mutex);
678 return vsocket ? 0 : -1;
682 rte_vhost_driver_get_features(const char *path, uint64_t *features)
684 struct vhost_user_socket *vsocket;
685 uint64_t vdpa_features;
686 struct rte_vdpa_device *vdpa_dev;
690 pthread_mutex_lock(&vhost_user.mutex);
691 vsocket = find_vhost_user_socket(path);
693 RTE_LOG(ERR, VHOST_CONFIG,
694 "socket file %s is not registered yet.\n", path);
699 did = vsocket->vdpa_dev_id;
700 vdpa_dev = rte_vdpa_get_device(did);
701 if (!vdpa_dev || !vdpa_dev->ops->get_features) {
702 *features = vsocket->features;
706 if (vdpa_dev->ops->get_features(did, &vdpa_features) < 0) {
707 RTE_LOG(ERR, VHOST_CONFIG,
708 "failed to get vdpa features "
709 "for socket file %s.\n", path);
714 *features = vsocket->features & vdpa_features;
717 pthread_mutex_unlock(&vhost_user.mutex);
722 rte_vhost_driver_set_protocol_features(const char *path,
723 uint64_t protocol_features)
725 struct vhost_user_socket *vsocket;
727 pthread_mutex_lock(&vhost_user.mutex);
728 vsocket = find_vhost_user_socket(path);
730 vsocket->protocol_features = protocol_features;
731 pthread_mutex_unlock(&vhost_user.mutex);
732 return vsocket ? 0 : -1;
736 rte_vhost_driver_get_protocol_features(const char *path,
737 uint64_t *protocol_features)
739 struct vhost_user_socket *vsocket;
740 uint64_t vdpa_protocol_features;
741 struct rte_vdpa_device *vdpa_dev;
745 pthread_mutex_lock(&vhost_user.mutex);
746 vsocket = find_vhost_user_socket(path);
748 RTE_LOG(ERR, VHOST_CONFIG,
749 "socket file %s is not registered yet.\n", path);
754 did = vsocket->vdpa_dev_id;
755 vdpa_dev = rte_vdpa_get_device(did);
756 if (!vdpa_dev || !vdpa_dev->ops->get_protocol_features) {
757 *protocol_features = vsocket->protocol_features;
761 if (vdpa_dev->ops->get_protocol_features(did,
762 &vdpa_protocol_features) < 0) {
763 RTE_LOG(ERR, VHOST_CONFIG,
764 "failed to get vdpa protocol features "
765 "for socket file %s.\n", path);
770 *protocol_features = vsocket->protocol_features
771 & vdpa_protocol_features;
774 pthread_mutex_unlock(&vhost_user.mutex);
779 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
781 struct vhost_user_socket *vsocket;
782 uint32_t vdpa_queue_num;
783 struct rte_vdpa_device *vdpa_dev;
787 pthread_mutex_lock(&vhost_user.mutex);
788 vsocket = find_vhost_user_socket(path);
790 RTE_LOG(ERR, VHOST_CONFIG,
791 "socket file %s is not registered yet.\n", path);
796 did = vsocket->vdpa_dev_id;
797 vdpa_dev = rte_vdpa_get_device(did);
798 if (!vdpa_dev || !vdpa_dev->ops->get_queue_num) {
799 *queue_num = VHOST_MAX_QUEUE_PAIRS;
803 if (vdpa_dev->ops->get_queue_num(did, &vdpa_queue_num) < 0) {
804 RTE_LOG(ERR, VHOST_CONFIG,
805 "failed to get vdpa queue number "
806 "for socket file %s.\n", path);
811 *queue_num = RTE_MIN((uint32_t)VHOST_MAX_QUEUE_PAIRS, vdpa_queue_num);
814 pthread_mutex_unlock(&vhost_user.mutex);
819 vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
821 if (vsocket && vsocket->path) {
823 vsocket->path = NULL;
833 * Register a new vhost-user socket; here we could act as server
834 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
838 rte_vhost_driver_register(const char *path, uint64_t flags)
841 struct vhost_user_socket *vsocket;
846 pthread_mutex_lock(&vhost_user.mutex);
848 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
849 RTE_LOG(ERR, VHOST_CONFIG,
850 "error: the number of vhost sockets reaches maximum\n");
854 vsocket = malloc(sizeof(struct vhost_user_socket));
857 memset(vsocket, 0, sizeof(struct vhost_user_socket));
858 vsocket->path = strdup(path);
859 if (vsocket->path == NULL) {
860 RTE_LOG(ERR, VHOST_CONFIG,
861 "error: failed to copy socket path string\n");
862 vhost_user_socket_mem_free(vsocket);
865 TAILQ_INIT(&vsocket->conn_list);
866 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
868 RTE_LOG(ERR, VHOST_CONFIG,
869 "error: failed to init connection mutex\n");
872 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
875 * Set the supported features correctly for the builtin vhost-user
878 * Applications know nothing about features the builtin virtio net
879 * driver (virtio_net.c) supports, thus it's not possible for them
880 * to invoke rte_vhost_driver_set_features(). To workaround it, here
881 * we set it unconditionally. If the application want to implement
882 * another vhost-user driver (say SCSI), it should call the
883 * rte_vhost_driver_set_features(), which will overwrite following
886 vsocket->use_builtin_virtio_net = true;
887 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
888 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
889 vsocket->protocol_features = VHOST_USER_PROTOCOL_FEATURES;
892 * Dequeue zero copy can't assure descriptors returned in order.
893 * Also, it requires that the guest memory is populated, which is
894 * not compatible with postcopy.
896 if (vsocket->dequeue_zero_copy) {
897 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IN_ORDER);
898 vsocket->features &= ~(1ULL << VIRTIO_F_IN_ORDER);
900 RTE_LOG(INFO, VHOST_CONFIG,
901 "Dequeue zero copy requested, disabling postcopy support\n");
902 vsocket->protocol_features &=
903 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
906 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
907 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
908 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
911 if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
912 vsocket->protocol_features &=
913 ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
915 #ifndef RTE_LIBRTE_VHOST_POSTCOPY
916 RTE_LOG(ERR, VHOST_CONFIG,
917 "Postcopy requested but not compiled\n");
923 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
924 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
925 if (vsocket->reconnect && reconn_tid == 0) {
926 if (vhost_user_reconnect_init() != 0)
930 vsocket->is_server = true;
932 ret = create_unix_socket(vsocket);
937 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
939 pthread_mutex_unlock(&vhost_user.mutex);
943 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
944 RTE_LOG(ERR, VHOST_CONFIG,
945 "error: failed to destroy connection mutex\n");
948 vhost_user_socket_mem_free(vsocket);
950 pthread_mutex_unlock(&vhost_user.mutex);
956 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
959 struct vhost_user_reconnect *reconn, *next;
961 pthread_mutex_lock(&reconn_list.mutex);
963 for (reconn = TAILQ_FIRST(&reconn_list.head);
964 reconn != NULL; reconn = next) {
965 next = TAILQ_NEXT(reconn, next);
967 if (reconn->vsocket == vsocket) {
968 TAILQ_REMOVE(&reconn_list.head, reconn, next);
975 pthread_mutex_unlock(&reconn_list.mutex);
980 * Unregister the specified vhost socket
983 rte_vhost_driver_unregister(const char *path)
987 struct vhost_user_connection *conn, *next;
993 pthread_mutex_lock(&vhost_user.mutex);
995 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
996 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
998 if (!strcmp(vsocket->path, path)) {
999 pthread_mutex_lock(&vsocket->conn_mutex);
1000 for (conn = TAILQ_FIRST(&vsocket->conn_list);
1003 next = TAILQ_NEXT(conn, next);
1006 * If r/wcb is executing, release the
1007 * conn_mutex lock, and try again since
1008 * the r/wcb may use the conn_mutex lock.
1010 if (fdset_try_del(&vhost_user.fdset,
1011 conn->connfd) == -1) {
1012 pthread_mutex_unlock(
1013 &vsocket->conn_mutex);
1014 pthread_mutex_unlock(&vhost_user.mutex);
1018 RTE_LOG(INFO, VHOST_CONFIG,
1019 "free connfd = %d for device '%s'\n",
1020 conn->connfd, path);
1021 close(conn->connfd);
1022 vhost_destroy_device(conn->vid);
1023 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
1026 pthread_mutex_unlock(&vsocket->conn_mutex);
1028 if (vsocket->is_server) {
1029 fdset_del(&vhost_user.fdset,
1030 vsocket->socket_fd);
1031 close(vsocket->socket_fd);
1033 } else if (vsocket->reconnect) {
1034 vhost_user_remove_reconnect(vsocket);
1037 pthread_mutex_destroy(&vsocket->conn_mutex);
1038 vhost_user_socket_mem_free(vsocket);
1040 count = --vhost_user.vsocket_cnt;
1041 vhost_user.vsockets[i] = vhost_user.vsockets[count];
1042 vhost_user.vsockets[count] = NULL;
1043 pthread_mutex_unlock(&vhost_user.mutex);
1048 pthread_mutex_unlock(&vhost_user.mutex);
1054 * Register ops so that we can add/remove device to data core.
1057 rte_vhost_driver_callback_register(const char *path,
1058 struct vhost_device_ops const * const ops)
1060 struct vhost_user_socket *vsocket;
1062 pthread_mutex_lock(&vhost_user.mutex);
1063 vsocket = find_vhost_user_socket(path);
1065 vsocket->notify_ops = ops;
1066 pthread_mutex_unlock(&vhost_user.mutex);
1068 return vsocket ? 0 : -1;
1071 struct vhost_device_ops const *
1072 vhost_driver_callback_get(const char *path)
1074 struct vhost_user_socket *vsocket;
1076 pthread_mutex_lock(&vhost_user.mutex);
1077 vsocket = find_vhost_user_socket(path);
1078 pthread_mutex_unlock(&vhost_user.mutex);
1080 return vsocket ? vsocket->notify_ops : NULL;
1084 rte_vhost_driver_start(const char *path)
1086 struct vhost_user_socket *vsocket;
1087 static pthread_t fdset_tid;
1089 pthread_mutex_lock(&vhost_user.mutex);
1090 vsocket = find_vhost_user_socket(path);
1091 pthread_mutex_unlock(&vhost_user.mutex);
1096 if (fdset_tid == 0) {
1098 * create a pipe which will be waited by poll and notified to
1099 * rebuild the wait list of poll.
1101 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1102 RTE_LOG(ERR, VHOST_CONFIG,
1103 "failed to create pipe for vhost fdset\n");
1107 int ret = rte_ctrl_thread_create(&fdset_tid,
1108 "vhost-events", NULL, fdset_event_dispatch,
1111 RTE_LOG(ERR, VHOST_CONFIG,
1112 "failed to create fdset handling thread");
1114 fdset_pipe_uninit(&vhost_user.fdset);
1119 if (vsocket->is_server)
1120 return vhost_user_start_server(vsocket);
1122 return vhost_user_start_client(vsocket);