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;
55 * Device id to identify a specific backend device.
56 * It's set to -1 for the default software implementation.
57 * If valid, one socket can have 1 connection only.
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,
94 .mutex = PTHREAD_MUTEX_INITIALIZER,
97 /* return bytes# of read on success or negative val on failure. */
99 read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
103 size_t fdsize = fd_num * sizeof(int);
104 char control[CMSG_SPACE(fdsize)];
105 struct cmsghdr *cmsg;
109 memset(&msgh, 0, sizeof(msgh));
111 iov.iov_len = buflen;
115 msgh.msg_control = control;
116 msgh.msg_controllen = sizeof(control);
118 ret = recvmsg(sockfd, &msgh, 0);
120 RTE_LOG(ERR, VHOST_CONFIG, "recvmsg failed\n");
124 if (msgh.msg_flags & (MSG_TRUNC | MSG_CTRUNC)) {
125 RTE_LOG(ERR, VHOST_CONFIG, "truncted msg\n");
129 for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
130 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
131 if ((cmsg->cmsg_level == SOL_SOCKET) &&
132 (cmsg->cmsg_type == SCM_RIGHTS)) {
133 got_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
134 memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
139 /* Clear out unused file descriptors */
140 while (got_fds < fd_num)
147 send_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
152 size_t fdsize = fd_num * sizeof(int);
153 char control[CMSG_SPACE(fdsize)];
154 struct cmsghdr *cmsg;
157 memset(&msgh, 0, sizeof(msgh));
159 iov.iov_len = buflen;
164 if (fds && fd_num > 0) {
165 msgh.msg_control = control;
166 msgh.msg_controllen = sizeof(control);
167 cmsg = CMSG_FIRSTHDR(&msgh);
169 RTE_LOG(ERR, VHOST_CONFIG, "cmsg == NULL\n");
173 cmsg->cmsg_len = CMSG_LEN(fdsize);
174 cmsg->cmsg_level = SOL_SOCKET;
175 cmsg->cmsg_type = SCM_RIGHTS;
176 memcpy(CMSG_DATA(cmsg), fds, fdsize);
178 msgh.msg_control = NULL;
179 msgh.msg_controllen = 0;
183 ret = sendmsg(sockfd, &msgh, MSG_NOSIGNAL);
184 } while (ret < 0 && errno == EINTR);
187 RTE_LOG(ERR, VHOST_CONFIG, "sendmsg error\n");
195 vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
199 struct vhost_user_connection *conn;
202 conn = malloc(sizeof(*conn));
208 vid = vhost_new_device();
213 size = strnlen(vsocket->path, PATH_MAX);
214 vhost_set_ifname(vid, vsocket->path, size);
216 vhost_set_builtin_virtio_net(vid, vsocket->use_builtin_virtio_net);
218 vhost_attach_vdpa_device(vid, vsocket->vdpa_dev_id);
220 if (vsocket->dequeue_zero_copy)
221 vhost_enable_dequeue_zero_copy(vid);
223 RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", vid);
225 if (vsocket->notify_ops->new_connection) {
226 ret = vsocket->notify_ops->new_connection(vid);
228 RTE_LOG(ERR, VHOST_CONFIG,
229 "failed to add vhost user connection with fd %d\n",
236 conn->vsocket = vsocket;
238 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
241 RTE_LOG(ERR, VHOST_CONFIG,
242 "failed to add fd %d into vhost server fdset\n",
245 if (vsocket->notify_ops->destroy_connection)
246 vsocket->notify_ops->destroy_connection(conn->vid);
251 pthread_mutex_lock(&vsocket->conn_mutex);
252 TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
253 pthread_mutex_unlock(&vsocket->conn_mutex);
255 fdset_pipe_notify(&vhost_user.fdset);
263 /* call back when there is new vhost-user connection from client */
265 vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
267 struct vhost_user_socket *vsocket = dat;
269 fd = accept(fd, NULL, NULL);
273 RTE_LOG(INFO, VHOST_CONFIG, "new vhost user connection is %d\n", fd);
274 vhost_user_add_connection(fd, vsocket);
278 vhost_user_read_cb(int connfd, void *dat, int *remove)
280 struct vhost_user_connection *conn = dat;
281 struct vhost_user_socket *vsocket = conn->vsocket;
284 ret = vhost_user_msg_handler(conn->vid, connfd);
288 vhost_destroy_device(conn->vid);
290 if (vsocket->notify_ops->destroy_connection)
291 vsocket->notify_ops->destroy_connection(conn->vid);
293 pthread_mutex_lock(&vsocket->conn_mutex);
294 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
295 pthread_mutex_unlock(&vsocket->conn_mutex);
299 if (vsocket->reconnect) {
300 create_unix_socket(vsocket);
301 vhost_user_start_client(vsocket);
307 create_unix_socket(struct vhost_user_socket *vsocket)
310 struct sockaddr_un *un = &vsocket->un;
312 fd = socket(AF_UNIX, SOCK_STREAM, 0);
315 RTE_LOG(INFO, VHOST_CONFIG, "vhost-user %s: socket created, fd: %d\n",
316 vsocket->is_server ? "server" : "client", fd);
318 if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
319 RTE_LOG(ERR, VHOST_CONFIG,
320 "vhost-user: can't set nonblocking mode for socket, fd: "
321 "%d (%s)\n", fd, strerror(errno));
326 memset(un, 0, sizeof(*un));
327 un->sun_family = AF_UNIX;
328 strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
329 un->sun_path[sizeof(un->sun_path) - 1] = '\0';
331 vsocket->socket_fd = fd;
336 vhost_user_start_server(struct vhost_user_socket *vsocket)
339 int fd = vsocket->socket_fd;
340 const char *path = vsocket->path;
343 * bind () may fail if the socket file with the same name already
344 * exists. But the library obviously should not delete the file
345 * provided by the user, since we can not be sure that it is not
346 * being used by other applications. Moreover, many applications form
347 * socket names based on user input, which is prone to errors.
349 * The user must ensure that the socket does not exist before
350 * registering the vhost driver in server mode.
352 ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
354 RTE_LOG(ERR, VHOST_CONFIG,
355 "failed to bind to %s: %s; remove it and try again\n",
356 path, strerror(errno));
359 RTE_LOG(INFO, VHOST_CONFIG, "bind to %s\n", path);
361 ret = listen(fd, MAX_VIRTIO_BACKLOG);
365 ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
368 RTE_LOG(ERR, VHOST_CONFIG,
369 "failed to add listen fd %d to vhost server fdset\n",
381 struct vhost_user_reconnect {
382 struct sockaddr_un un;
384 struct vhost_user_socket *vsocket;
386 TAILQ_ENTRY(vhost_user_reconnect) next;
389 TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
390 struct vhost_user_reconnect_list {
391 struct vhost_user_reconnect_tailq_list head;
392 pthread_mutex_t mutex;
395 static struct vhost_user_reconnect_list reconn_list;
396 static pthread_t reconn_tid;
399 vhost_user_connect_nonblock(int fd, struct sockaddr *un, size_t sz)
403 ret = connect(fd, un, sz);
404 if (ret < 0 && errno != EISCONN)
407 flags = fcntl(fd, F_GETFL, 0);
409 RTE_LOG(ERR, VHOST_CONFIG,
410 "can't get flags for connfd %d\n", fd);
413 if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
414 RTE_LOG(ERR, VHOST_CONFIG,
415 "can't disable nonblocking on fd %d\n", fd);
422 vhost_user_client_reconnect(void *arg __rte_unused)
425 struct vhost_user_reconnect *reconn, *next;
428 pthread_mutex_lock(&reconn_list.mutex);
431 * An equal implementation of TAILQ_FOREACH_SAFE,
432 * which does not exist on all platforms.
434 for (reconn = TAILQ_FIRST(&reconn_list.head);
435 reconn != NULL; reconn = next) {
436 next = TAILQ_NEXT(reconn, next);
438 ret = vhost_user_connect_nonblock(reconn->fd,
439 (struct sockaddr *)&reconn->un,
443 RTE_LOG(ERR, VHOST_CONFIG,
444 "reconnection for fd %d failed\n",
451 RTE_LOG(INFO, VHOST_CONFIG,
452 "%s: connected\n", reconn->vsocket->path);
453 vhost_user_add_connection(reconn->fd, reconn->vsocket);
455 TAILQ_REMOVE(&reconn_list.head, reconn, next);
459 pthread_mutex_unlock(&reconn_list.mutex);
467 vhost_user_reconnect_init(void)
470 char thread_name[RTE_MAX_THREAD_NAME_LEN];
472 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
474 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
477 TAILQ_INIT(&reconn_list.head);
479 ret = pthread_create(&reconn_tid, NULL,
480 vhost_user_client_reconnect, NULL);
482 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
483 if (pthread_mutex_destroy(&reconn_list.mutex)) {
484 RTE_LOG(ERR, VHOST_CONFIG,
485 "failed to destroy reconnect mutex");
488 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
491 if (rte_thread_setname(reconn_tid, thread_name)) {
492 RTE_LOG(DEBUG, VHOST_CONFIG,
493 "failed to set reconnect thread name");
501 vhost_user_start_client(struct vhost_user_socket *vsocket)
504 int fd = vsocket->socket_fd;
505 const char *path = vsocket->path;
506 struct vhost_user_reconnect *reconn;
508 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
509 sizeof(vsocket->un));
511 vhost_user_add_connection(fd, vsocket);
515 RTE_LOG(WARNING, VHOST_CONFIG,
516 "failed to connect to %s: %s\n",
517 path, strerror(errno));
519 if (ret == -2 || !vsocket->reconnect) {
524 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
525 reconn = malloc(sizeof(*reconn));
526 if (reconn == NULL) {
527 RTE_LOG(ERR, VHOST_CONFIG,
528 "failed to allocate memory for reconnect\n");
532 reconn->un = vsocket->un;
534 reconn->vsocket = vsocket;
535 pthread_mutex_lock(&reconn_list.mutex);
536 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
537 pthread_mutex_unlock(&reconn_list.mutex);
542 static struct vhost_user_socket *
543 find_vhost_user_socket(const char *path)
547 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
548 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
550 if (!strcmp(vsocket->path, path))
558 rte_vhost_driver_attach_vdpa_device(const char *path, int did)
560 struct vhost_user_socket *vsocket;
562 if (rte_vdpa_get_device(did) == NULL)
565 pthread_mutex_lock(&vhost_user.mutex);
566 vsocket = find_vhost_user_socket(path);
568 vsocket->vdpa_dev_id = did;
569 pthread_mutex_unlock(&vhost_user.mutex);
571 return vsocket ? 0 : -1;
575 rte_vhost_driver_detach_vdpa_device(const char *path)
577 struct vhost_user_socket *vsocket;
579 pthread_mutex_lock(&vhost_user.mutex);
580 vsocket = find_vhost_user_socket(path);
582 vsocket->vdpa_dev_id = -1;
583 pthread_mutex_unlock(&vhost_user.mutex);
585 return vsocket ? 0 : -1;
589 rte_vhost_driver_get_vdpa_device_id(const char *path)
591 struct vhost_user_socket *vsocket;
594 pthread_mutex_lock(&vhost_user.mutex);
595 vsocket = find_vhost_user_socket(path);
597 did = vsocket->vdpa_dev_id;
598 pthread_mutex_unlock(&vhost_user.mutex);
604 rte_vhost_driver_disable_features(const char *path, uint64_t features)
606 struct vhost_user_socket *vsocket;
608 pthread_mutex_lock(&vhost_user.mutex);
609 vsocket = find_vhost_user_socket(path);
611 /* Note that use_builtin_virtio_net is not affected by this function
612 * since callers may want to selectively disable features of the
613 * built-in vhost net device backend.
617 vsocket->features &= ~features;
618 pthread_mutex_unlock(&vhost_user.mutex);
620 return vsocket ? 0 : -1;
624 rte_vhost_driver_enable_features(const char *path, uint64_t features)
626 struct vhost_user_socket *vsocket;
628 pthread_mutex_lock(&vhost_user.mutex);
629 vsocket = find_vhost_user_socket(path);
631 if ((vsocket->supported_features & features) != features) {
633 * trying to enable features the driver doesn't
636 pthread_mutex_unlock(&vhost_user.mutex);
639 vsocket->features |= features;
641 pthread_mutex_unlock(&vhost_user.mutex);
643 return vsocket ? 0 : -1;
647 rte_vhost_driver_set_features(const char *path, uint64_t features)
649 struct vhost_user_socket *vsocket;
651 pthread_mutex_lock(&vhost_user.mutex);
652 vsocket = find_vhost_user_socket(path);
654 vsocket->supported_features = features;
655 vsocket->features = features;
657 /* Anyone setting feature bits is implementing their own vhost
660 vsocket->use_builtin_virtio_net = false;
662 pthread_mutex_unlock(&vhost_user.mutex);
664 return vsocket ? 0 : -1;
668 rte_vhost_driver_get_features(const char *path, uint64_t *features)
670 struct vhost_user_socket *vsocket;
671 uint64_t vdpa_features;
672 struct rte_vdpa_device *vdpa_dev;
676 pthread_mutex_lock(&vhost_user.mutex);
677 vsocket = find_vhost_user_socket(path);
679 RTE_LOG(ERR, VHOST_CONFIG,
680 "socket file %s is not registered yet.\n", path);
685 did = vsocket->vdpa_dev_id;
686 vdpa_dev = rte_vdpa_get_device(did);
687 if (!vdpa_dev || !vdpa_dev->ops->get_features) {
688 *features = vsocket->features;
692 if (vdpa_dev->ops->get_features(did, &vdpa_features) < 0) {
693 RTE_LOG(ERR, VHOST_CONFIG,
694 "failed to get vdpa features "
695 "for socket file %s.\n", path);
700 *features = vsocket->features & vdpa_features;
703 pthread_mutex_unlock(&vhost_user.mutex);
708 rte_vhost_driver_get_protocol_features(const char *path,
709 uint64_t *protocol_features)
711 struct vhost_user_socket *vsocket;
712 uint64_t vdpa_protocol_features;
713 struct rte_vdpa_device *vdpa_dev;
717 pthread_mutex_lock(&vhost_user.mutex);
718 vsocket = find_vhost_user_socket(path);
720 RTE_LOG(ERR, VHOST_CONFIG,
721 "socket file %s is not registered yet.\n", path);
726 did = vsocket->vdpa_dev_id;
727 vdpa_dev = rte_vdpa_get_device(did);
728 if (!vdpa_dev || !vdpa_dev->ops->get_protocol_features) {
729 *protocol_features = VHOST_USER_PROTOCOL_FEATURES;
733 if (vdpa_dev->ops->get_protocol_features(did,
734 &vdpa_protocol_features) < 0) {
735 RTE_LOG(ERR, VHOST_CONFIG,
736 "failed to get vdpa protocol features "
737 "for socket file %s.\n", path);
742 *protocol_features = VHOST_USER_PROTOCOL_FEATURES
743 & vdpa_protocol_features;
746 pthread_mutex_unlock(&vhost_user.mutex);
751 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
753 struct vhost_user_socket *vsocket;
754 uint32_t vdpa_queue_num;
755 struct rte_vdpa_device *vdpa_dev;
759 pthread_mutex_lock(&vhost_user.mutex);
760 vsocket = find_vhost_user_socket(path);
762 RTE_LOG(ERR, VHOST_CONFIG,
763 "socket file %s is not registered yet.\n", path);
768 did = vsocket->vdpa_dev_id;
769 vdpa_dev = rte_vdpa_get_device(did);
770 if (!vdpa_dev || !vdpa_dev->ops->get_queue_num) {
771 *queue_num = VHOST_MAX_QUEUE_PAIRS;
775 if (vdpa_dev->ops->get_queue_num(did, &vdpa_queue_num) < 0) {
776 RTE_LOG(ERR, VHOST_CONFIG,
777 "failed to get vdpa queue number "
778 "for socket file %s.\n", path);
783 *queue_num = RTE_MIN((uint32_t)VHOST_MAX_QUEUE_PAIRS, vdpa_queue_num);
786 pthread_mutex_unlock(&vhost_user.mutex);
791 * Register a new vhost-user socket; here we could act as server
792 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
796 rte_vhost_driver_register(const char *path, uint64_t flags)
799 struct vhost_user_socket *vsocket;
804 pthread_mutex_lock(&vhost_user.mutex);
806 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
807 RTE_LOG(ERR, VHOST_CONFIG,
808 "error: the number of vhost sockets reaches maximum\n");
812 vsocket = malloc(sizeof(struct vhost_user_socket));
815 memset(vsocket, 0, sizeof(struct vhost_user_socket));
816 vsocket->path = strdup(path);
817 if (vsocket->path == NULL) {
818 RTE_LOG(ERR, VHOST_CONFIG,
819 "error: failed to copy socket path string\n");
823 TAILQ_INIT(&vsocket->conn_list);
824 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
826 RTE_LOG(ERR, VHOST_CONFIG,
827 "error: failed to init connection mutex\n");
830 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
833 * Set the supported features correctly for the builtin vhost-user
836 * Applications know nothing about features the builtin virtio net
837 * driver (virtio_net.c) supports, thus it's not possible for them
838 * to invoke rte_vhost_driver_set_features(). To workaround it, here
839 * we set it unconditionally. If the application want to implement
840 * another vhost-user driver (say SCSI), it should call the
841 * rte_vhost_driver_set_features(), which will overwrite following
844 vsocket->use_builtin_virtio_net = true;
845 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
846 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
848 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
849 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
850 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
853 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
854 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
855 if (vsocket->reconnect && reconn_tid == 0) {
856 if (vhost_user_reconnect_init() != 0)
860 vsocket->is_server = true;
862 ret = create_unix_socket(vsocket);
867 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
869 pthread_mutex_unlock(&vhost_user.mutex);
873 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
874 RTE_LOG(ERR, VHOST_CONFIG,
875 "error: failed to destroy connection mutex\n");
881 pthread_mutex_unlock(&vhost_user.mutex);
887 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
890 struct vhost_user_reconnect *reconn, *next;
892 pthread_mutex_lock(&reconn_list.mutex);
894 for (reconn = TAILQ_FIRST(&reconn_list.head);
895 reconn != NULL; reconn = next) {
896 next = TAILQ_NEXT(reconn, next);
898 if (reconn->vsocket == vsocket) {
899 TAILQ_REMOVE(&reconn_list.head, reconn, next);
906 pthread_mutex_unlock(&reconn_list.mutex);
911 * Unregister the specified vhost socket
914 rte_vhost_driver_unregister(const char *path)
918 struct vhost_user_connection *conn, *next;
920 pthread_mutex_lock(&vhost_user.mutex);
922 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
923 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
925 if (!strcmp(vsocket->path, path)) {
926 if (vsocket->is_server) {
927 fdset_del(&vhost_user.fdset, vsocket->socket_fd);
928 close(vsocket->socket_fd);
930 } else if (vsocket->reconnect) {
931 vhost_user_remove_reconnect(vsocket);
934 pthread_mutex_lock(&vsocket->conn_mutex);
935 for (conn = TAILQ_FIRST(&vsocket->conn_list);
938 next = TAILQ_NEXT(conn, next);
940 fdset_del(&vhost_user.fdset, conn->connfd);
941 RTE_LOG(INFO, VHOST_CONFIG,
942 "free connfd = %d for device '%s'\n",
945 vhost_destroy_device(conn->vid);
946 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
949 pthread_mutex_unlock(&vsocket->conn_mutex);
951 pthread_mutex_destroy(&vsocket->conn_mutex);
955 count = --vhost_user.vsocket_cnt;
956 vhost_user.vsockets[i] = vhost_user.vsockets[count];
957 vhost_user.vsockets[count] = NULL;
958 pthread_mutex_unlock(&vhost_user.mutex);
963 pthread_mutex_unlock(&vhost_user.mutex);
969 * Register ops so that we can add/remove device to data core.
972 rte_vhost_driver_callback_register(const char *path,
973 struct vhost_device_ops const * const ops)
975 struct vhost_user_socket *vsocket;
977 pthread_mutex_lock(&vhost_user.mutex);
978 vsocket = find_vhost_user_socket(path);
980 vsocket->notify_ops = ops;
981 pthread_mutex_unlock(&vhost_user.mutex);
983 return vsocket ? 0 : -1;
986 struct vhost_device_ops const *
987 vhost_driver_callback_get(const char *path)
989 struct vhost_user_socket *vsocket;
991 pthread_mutex_lock(&vhost_user.mutex);
992 vsocket = find_vhost_user_socket(path);
993 pthread_mutex_unlock(&vhost_user.mutex);
995 return vsocket ? vsocket->notify_ops : NULL;
999 rte_vhost_driver_start(const char *path)
1001 struct vhost_user_socket *vsocket;
1002 static pthread_t fdset_tid;
1003 char thread_name[RTE_MAX_THREAD_NAME_LEN];
1005 pthread_mutex_lock(&vhost_user.mutex);
1006 vsocket = find_vhost_user_socket(path);
1007 pthread_mutex_unlock(&vhost_user.mutex);
1012 if (fdset_tid == 0) {
1014 * create a pipe which will be waited by poll and notified to
1015 * rebuild the wait list of poll.
1017 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1018 RTE_LOG(ERR, VHOST_CONFIG,
1019 "failed to create pipe for vhost fdset\n");
1023 int ret = pthread_create(&fdset_tid, NULL, fdset_event_dispatch,
1026 RTE_LOG(ERR, VHOST_CONFIG,
1027 "failed to create fdset handling thread");
1029 fdset_pipe_uninit(&vhost_user.fdset);
1032 snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN,
1035 if (rte_thread_setname(fdset_tid, thread_name)) {
1036 RTE_LOG(DEBUG, VHOST_CONFIG,
1037 "failed to set vhost-event thread name");
1042 if (vsocket->is_server)
1043 return vhost_user_start_server(vsocket);
1045 return vhost_user_start_client(vsocket);