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)
471 ret = pthread_mutex_init(&reconn_list.mutex, NULL);
473 RTE_LOG(ERR, VHOST_CONFIG, "failed to initialize mutex");
476 TAILQ_INIT(&reconn_list.head);
478 ret = rte_ctrl_thread_create(&reconn_tid, "vhost_reconn", NULL,
479 vhost_user_client_reconnect, NULL);
481 RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
482 if (pthread_mutex_destroy(&reconn_list.mutex)) {
483 RTE_LOG(ERR, VHOST_CONFIG,
484 "failed to destroy reconnect mutex");
492 vhost_user_start_client(struct vhost_user_socket *vsocket)
495 int fd = vsocket->socket_fd;
496 const char *path = vsocket->path;
497 struct vhost_user_reconnect *reconn;
499 ret = vhost_user_connect_nonblock(fd, (struct sockaddr *)&vsocket->un,
500 sizeof(vsocket->un));
502 vhost_user_add_connection(fd, vsocket);
506 RTE_LOG(WARNING, VHOST_CONFIG,
507 "failed to connect to %s: %s\n",
508 path, strerror(errno));
510 if (ret == -2 || !vsocket->reconnect) {
515 RTE_LOG(INFO, VHOST_CONFIG, "%s: reconnecting...\n", path);
516 reconn = malloc(sizeof(*reconn));
517 if (reconn == NULL) {
518 RTE_LOG(ERR, VHOST_CONFIG,
519 "failed to allocate memory for reconnect\n");
523 reconn->un = vsocket->un;
525 reconn->vsocket = vsocket;
526 pthread_mutex_lock(&reconn_list.mutex);
527 TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
528 pthread_mutex_unlock(&reconn_list.mutex);
533 static struct vhost_user_socket *
534 find_vhost_user_socket(const char *path)
538 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
539 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
541 if (!strcmp(vsocket->path, path))
549 rte_vhost_driver_attach_vdpa_device(const char *path, int did)
551 struct vhost_user_socket *vsocket;
553 if (rte_vdpa_get_device(did) == NULL)
556 pthread_mutex_lock(&vhost_user.mutex);
557 vsocket = find_vhost_user_socket(path);
559 vsocket->vdpa_dev_id = did;
560 pthread_mutex_unlock(&vhost_user.mutex);
562 return vsocket ? 0 : -1;
566 rte_vhost_driver_detach_vdpa_device(const char *path)
568 struct vhost_user_socket *vsocket;
570 pthread_mutex_lock(&vhost_user.mutex);
571 vsocket = find_vhost_user_socket(path);
573 vsocket->vdpa_dev_id = -1;
574 pthread_mutex_unlock(&vhost_user.mutex);
576 return vsocket ? 0 : -1;
580 rte_vhost_driver_get_vdpa_device_id(const char *path)
582 struct vhost_user_socket *vsocket;
585 pthread_mutex_lock(&vhost_user.mutex);
586 vsocket = find_vhost_user_socket(path);
588 did = vsocket->vdpa_dev_id;
589 pthread_mutex_unlock(&vhost_user.mutex);
595 rte_vhost_driver_disable_features(const char *path, uint64_t features)
597 struct vhost_user_socket *vsocket;
599 pthread_mutex_lock(&vhost_user.mutex);
600 vsocket = find_vhost_user_socket(path);
602 /* Note that use_builtin_virtio_net is not affected by this function
603 * since callers may want to selectively disable features of the
604 * built-in vhost net device backend.
608 vsocket->features &= ~features;
609 pthread_mutex_unlock(&vhost_user.mutex);
611 return vsocket ? 0 : -1;
615 rte_vhost_driver_enable_features(const char *path, uint64_t features)
617 struct vhost_user_socket *vsocket;
619 pthread_mutex_lock(&vhost_user.mutex);
620 vsocket = find_vhost_user_socket(path);
622 if ((vsocket->supported_features & features) != features) {
624 * trying to enable features the driver doesn't
627 pthread_mutex_unlock(&vhost_user.mutex);
630 vsocket->features |= features;
632 pthread_mutex_unlock(&vhost_user.mutex);
634 return vsocket ? 0 : -1;
638 rte_vhost_driver_set_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 vsocket->supported_features = features;
646 vsocket->features = features;
648 /* Anyone setting feature bits is implementing their own vhost
651 vsocket->use_builtin_virtio_net = false;
653 pthread_mutex_unlock(&vhost_user.mutex);
655 return vsocket ? 0 : -1;
659 rte_vhost_driver_get_features(const char *path, uint64_t *features)
661 struct vhost_user_socket *vsocket;
662 uint64_t vdpa_features;
663 struct rte_vdpa_device *vdpa_dev;
667 pthread_mutex_lock(&vhost_user.mutex);
668 vsocket = find_vhost_user_socket(path);
670 RTE_LOG(ERR, VHOST_CONFIG,
671 "socket file %s is not registered yet.\n", path);
676 did = vsocket->vdpa_dev_id;
677 vdpa_dev = rte_vdpa_get_device(did);
678 if (!vdpa_dev || !vdpa_dev->ops->get_features) {
679 *features = vsocket->features;
683 if (vdpa_dev->ops->get_features(did, &vdpa_features) < 0) {
684 RTE_LOG(ERR, VHOST_CONFIG,
685 "failed to get vdpa features "
686 "for socket file %s.\n", path);
691 *features = vsocket->features & vdpa_features;
694 pthread_mutex_unlock(&vhost_user.mutex);
699 rte_vhost_driver_get_protocol_features(const char *path,
700 uint64_t *protocol_features)
702 struct vhost_user_socket *vsocket;
703 uint64_t vdpa_protocol_features;
704 struct rte_vdpa_device *vdpa_dev;
708 pthread_mutex_lock(&vhost_user.mutex);
709 vsocket = find_vhost_user_socket(path);
711 RTE_LOG(ERR, VHOST_CONFIG,
712 "socket file %s is not registered yet.\n", path);
717 did = vsocket->vdpa_dev_id;
718 vdpa_dev = rte_vdpa_get_device(did);
719 if (!vdpa_dev || !vdpa_dev->ops->get_protocol_features) {
720 *protocol_features = VHOST_USER_PROTOCOL_FEATURES;
724 if (vdpa_dev->ops->get_protocol_features(did,
725 &vdpa_protocol_features) < 0) {
726 RTE_LOG(ERR, VHOST_CONFIG,
727 "failed to get vdpa protocol features "
728 "for socket file %s.\n", path);
733 *protocol_features = VHOST_USER_PROTOCOL_FEATURES
734 & vdpa_protocol_features;
737 pthread_mutex_unlock(&vhost_user.mutex);
742 rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
744 struct vhost_user_socket *vsocket;
745 uint32_t vdpa_queue_num;
746 struct rte_vdpa_device *vdpa_dev;
750 pthread_mutex_lock(&vhost_user.mutex);
751 vsocket = find_vhost_user_socket(path);
753 RTE_LOG(ERR, VHOST_CONFIG,
754 "socket file %s is not registered yet.\n", path);
759 did = vsocket->vdpa_dev_id;
760 vdpa_dev = rte_vdpa_get_device(did);
761 if (!vdpa_dev || !vdpa_dev->ops->get_queue_num) {
762 *queue_num = VHOST_MAX_QUEUE_PAIRS;
766 if (vdpa_dev->ops->get_queue_num(did, &vdpa_queue_num) < 0) {
767 RTE_LOG(ERR, VHOST_CONFIG,
768 "failed to get vdpa queue number "
769 "for socket file %s.\n", path);
774 *queue_num = RTE_MIN((uint32_t)VHOST_MAX_QUEUE_PAIRS, vdpa_queue_num);
777 pthread_mutex_unlock(&vhost_user.mutex);
782 * Register a new vhost-user socket; here we could act as server
783 * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
787 rte_vhost_driver_register(const char *path, uint64_t flags)
790 struct vhost_user_socket *vsocket;
795 pthread_mutex_lock(&vhost_user.mutex);
797 if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
798 RTE_LOG(ERR, VHOST_CONFIG,
799 "error: the number of vhost sockets reaches maximum\n");
803 vsocket = malloc(sizeof(struct vhost_user_socket));
806 memset(vsocket, 0, sizeof(struct vhost_user_socket));
807 vsocket->path = strdup(path);
808 if (vsocket->path == NULL) {
809 RTE_LOG(ERR, VHOST_CONFIG,
810 "error: failed to copy socket path string\n");
814 TAILQ_INIT(&vsocket->conn_list);
815 ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
817 RTE_LOG(ERR, VHOST_CONFIG,
818 "error: failed to init connection mutex\n");
821 vsocket->dequeue_zero_copy = flags & RTE_VHOST_USER_DEQUEUE_ZERO_COPY;
824 * Set the supported features correctly for the builtin vhost-user
827 * Applications know nothing about features the builtin virtio net
828 * driver (virtio_net.c) supports, thus it's not possible for them
829 * to invoke rte_vhost_driver_set_features(). To workaround it, here
830 * we set it unconditionally. If the application want to implement
831 * another vhost-user driver (say SCSI), it should call the
832 * rte_vhost_driver_set_features(), which will overwrite following
835 vsocket->use_builtin_virtio_net = true;
836 vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
837 vsocket->features = VIRTIO_NET_SUPPORTED_FEATURES;
839 if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
840 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
841 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
844 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
845 vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
846 if (vsocket->reconnect && reconn_tid == 0) {
847 if (vhost_user_reconnect_init() != 0)
851 vsocket->is_server = true;
853 ret = create_unix_socket(vsocket);
858 vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
860 pthread_mutex_unlock(&vhost_user.mutex);
864 if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
865 RTE_LOG(ERR, VHOST_CONFIG,
866 "error: failed to destroy connection mutex\n");
872 pthread_mutex_unlock(&vhost_user.mutex);
878 vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
881 struct vhost_user_reconnect *reconn, *next;
883 pthread_mutex_lock(&reconn_list.mutex);
885 for (reconn = TAILQ_FIRST(&reconn_list.head);
886 reconn != NULL; reconn = next) {
887 next = TAILQ_NEXT(reconn, next);
889 if (reconn->vsocket == vsocket) {
890 TAILQ_REMOVE(&reconn_list.head, reconn, next);
897 pthread_mutex_unlock(&reconn_list.mutex);
902 * Unregister the specified vhost socket
905 rte_vhost_driver_unregister(const char *path)
909 struct vhost_user_connection *conn, *next;
911 pthread_mutex_lock(&vhost_user.mutex);
913 for (i = 0; i < vhost_user.vsocket_cnt; i++) {
914 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
916 if (!strcmp(vsocket->path, path)) {
917 if (vsocket->is_server) {
918 fdset_del(&vhost_user.fdset, vsocket->socket_fd);
919 close(vsocket->socket_fd);
921 } else if (vsocket->reconnect) {
922 vhost_user_remove_reconnect(vsocket);
925 pthread_mutex_lock(&vsocket->conn_mutex);
926 for (conn = TAILQ_FIRST(&vsocket->conn_list);
929 next = TAILQ_NEXT(conn, next);
931 fdset_del(&vhost_user.fdset, conn->connfd);
932 RTE_LOG(INFO, VHOST_CONFIG,
933 "free connfd = %d for device '%s'\n",
936 vhost_destroy_device(conn->vid);
937 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
940 pthread_mutex_unlock(&vsocket->conn_mutex);
942 pthread_mutex_destroy(&vsocket->conn_mutex);
946 count = --vhost_user.vsocket_cnt;
947 vhost_user.vsockets[i] = vhost_user.vsockets[count];
948 vhost_user.vsockets[count] = NULL;
949 pthread_mutex_unlock(&vhost_user.mutex);
954 pthread_mutex_unlock(&vhost_user.mutex);
960 * Register ops so that we can add/remove device to data core.
963 rte_vhost_driver_callback_register(const char *path,
964 struct vhost_device_ops const * const ops)
966 struct vhost_user_socket *vsocket;
968 pthread_mutex_lock(&vhost_user.mutex);
969 vsocket = find_vhost_user_socket(path);
971 vsocket->notify_ops = ops;
972 pthread_mutex_unlock(&vhost_user.mutex);
974 return vsocket ? 0 : -1;
977 struct vhost_device_ops const *
978 vhost_driver_callback_get(const char *path)
980 struct vhost_user_socket *vsocket;
982 pthread_mutex_lock(&vhost_user.mutex);
983 vsocket = find_vhost_user_socket(path);
984 pthread_mutex_unlock(&vhost_user.mutex);
986 return vsocket ? vsocket->notify_ops : NULL;
990 rte_vhost_driver_start(const char *path)
992 struct vhost_user_socket *vsocket;
993 static pthread_t fdset_tid;
995 pthread_mutex_lock(&vhost_user.mutex);
996 vsocket = find_vhost_user_socket(path);
997 pthread_mutex_unlock(&vhost_user.mutex);
1002 if (fdset_tid == 0) {
1004 * create a pipe which will be waited by poll and notified to
1005 * rebuild the wait list of poll.
1007 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
1008 RTE_LOG(ERR, VHOST_CONFIG,
1009 "failed to create pipe for vhost fdset\n");
1013 int ret = rte_ctrl_thread_create(&fdset_tid,
1014 "vhost-events", NULL, fdset_event_dispatch,
1017 RTE_LOG(ERR, VHOST_CONFIG,
1018 "failed to create fdset handling thread");
1020 fdset_pipe_uninit(&vhost_user.fdset);
1025 if (vsocket->is_server)
1026 return vhost_user_start_server(vsocket);
1028 return vhost_user_start_client(vsocket);