vhost: add number of fds to vhost-user messages
[dpdk.git] / lib / librte_vhost / socket.c
index db9be6a..c278e9b 100644 (file)
@@ -94,18 +94,23 @@ static struct vhost_user vhost_user = {
        .mutex = PTHREAD_MUTEX_INITIALIZER,
 };
 
-/* return bytes# of read on success or negative val on failure. */
+/*
+ * return bytes# of read on success or negative val on failure. Update fdnum
+ * with number of fds read.
+ */
 int
-read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
+read_fd_message(int sockfd, char *buf, int buflen, int *fds, int max_fds,
+               int *fd_num)
 {
        struct iovec iov;
        struct msghdr msgh;
-       size_t fdsize = fd_num * sizeof(int);
-       char control[CMSG_SPACE(fdsize)];
+       char control[CMSG_SPACE(max_fds * sizeof(int))];
        struct cmsghdr *cmsg;
        int got_fds = 0;
        int ret;
 
+       *fd_num = 0;
+
        memset(&msgh, 0, sizeof(msgh));
        iov.iov_base = buf;
        iov.iov_len  = buflen;
@@ -131,13 +136,14 @@ read_fd_message(int sockfd, char *buf, int buflen, int *fds, int fd_num)
                if ((cmsg->cmsg_level == SOL_SOCKET) &&
                        (cmsg->cmsg_type == SCM_RIGHTS)) {
                        got_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
+                       *fd_num = got_fds;
                        memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
                        break;
                }
        }
 
        /* Clear out unused file descriptors */
-       while (got_fds < fd_num)
+       while (got_fds < max_fds)
                fds[got_fds++] = -1;
 
        return ret;
@@ -199,6 +205,9 @@ vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
        struct vhost_user_connection *conn;
        int ret;
 
+       if (vsocket == NULL)
+               return;
+
        conn = malloc(sizeof(*conn));
        if (conn == NULL) {
                close(fd);
@@ -467,7 +476,6 @@ static int
 vhost_user_reconnect_init(void)
 {
        int ret;
-       char thread_name[RTE_MAX_THREAD_NAME_LEN];
 
        ret = pthread_mutex_init(&reconn_list.mutex, NULL);
        if (ret < 0) {
@@ -476,7 +484,7 @@ vhost_user_reconnect_init(void)
        }
        TAILQ_INIT(&reconn_list.head);
 
-       ret = rte_ctrl_thread_create(&reconn_tid, NULL,
+       ret = rte_ctrl_thread_create(&reconn_tid, "vhost_reconn", NULL,
                             vhost_user_client_reconnect, NULL);
        if (ret != 0) {
                RTE_LOG(ERR, VHOST_CONFIG, "failed to create reconnect thread");
@@ -484,14 +492,6 @@ vhost_user_reconnect_init(void)
                        RTE_LOG(ERR, VHOST_CONFIG,
                                "failed to destroy reconnect mutex");
                }
-       } else {
-               snprintf(thread_name, sizeof(thread_name),
-                        "vhost-reconn");
-
-               if (rte_thread_setname(reconn_tid, thread_name)) {
-                       RTE_LOG(DEBUG, VHOST_CONFIG,
-                               "failed to set reconnect thread name");
-               }
        }
 
        return ret;
@@ -787,6 +787,20 @@ unlock_exit:
        return ret;
 }
 
+static void
+vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
+{
+       if (vsocket && vsocket->path) {
+               free(vsocket->path);
+               vsocket->path = NULL;
+       }
+
+       if (vsocket) {
+               free(vsocket);
+               vsocket = NULL;
+       }
+}
+
 /*
  * Register a new vhost-user socket; here we could act as server
  * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
@@ -817,7 +831,7 @@ rte_vhost_driver_register(const char *path, uint64_t flags)
        if (vsocket->path == NULL) {
                RTE_LOG(ERR, VHOST_CONFIG,
                        "error: failed to copy socket path string\n");
-               free(vsocket);
+               vhost_user_socket_mem_free(vsocket);
                goto out;
        }
        TAILQ_INIT(&vsocket->conn_list);
@@ -845,6 +859,12 @@ rte_vhost_driver_register(const char *path, uint64_t flags)
        vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
        vsocket->features           = VIRTIO_NET_SUPPORTED_FEATURES;
 
+       /* Dequeue zero copy can't assure descriptors returned in order */
+       if (vsocket->dequeue_zero_copy) {
+               vsocket->supported_features &= ~(1ULL << VIRTIO_F_IN_ORDER);
+               vsocket->features &= ~(1ULL << VIRTIO_F_IN_ORDER);
+       }
+
        if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
                vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
                vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
@@ -875,8 +895,7 @@ out_mutex:
                        "error: failed to destroy connection mutex\n");
        }
 out_free:
-       free(vsocket->path);
-       free(vsocket);
+       vhost_user_socket_mem_free(vsocket);
 out:
        pthread_mutex_unlock(&vhost_user.mutex);
 
@@ -923,21 +942,25 @@ rte_vhost_driver_unregister(const char *path)
                struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
 
                if (!strcmp(vsocket->path, path)) {
-                       if (vsocket->is_server) {
-                               fdset_del(&vhost_user.fdset, vsocket->socket_fd);
-                               close(vsocket->socket_fd);
-                               unlink(path);
-                       } else if (vsocket->reconnect) {
-                               vhost_user_remove_reconnect(vsocket);
-                       }
-
+again:
                        pthread_mutex_lock(&vsocket->conn_mutex);
                        for (conn = TAILQ_FIRST(&vsocket->conn_list);
                             conn != NULL;
                             conn = next) {
                                next = TAILQ_NEXT(conn, next);
 
-                               fdset_del(&vhost_user.fdset, conn->connfd);
+                               /*
+                                * If r/wcb is executing, release the
+                                * conn_mutex lock, and try again since
+                                * the r/wcb may use the conn_mutex lock.
+                                */
+                               if (fdset_try_del(&vhost_user.fdset,
+                                                 conn->connfd) == -1) {
+                                       pthread_mutex_unlock(
+                                                       &vsocket->conn_mutex);
+                                       goto again;
+                               }
+
                                RTE_LOG(INFO, VHOST_CONFIG,
                                        "free connfd = %d for device '%s'\n",
                                        conn->connfd, path);
@@ -948,9 +971,17 @@ rte_vhost_driver_unregister(const char *path)
                        }
                        pthread_mutex_unlock(&vsocket->conn_mutex);
 
+                       if (vsocket->is_server) {
+                               fdset_del(&vhost_user.fdset,
+                                               vsocket->socket_fd);
+                               close(vsocket->socket_fd);
+                               unlink(path);
+                       } else if (vsocket->reconnect) {
+                               vhost_user_remove_reconnect(vsocket);
+                       }
+
                        pthread_mutex_destroy(&vsocket->conn_mutex);
-                       free(vsocket->path);
-                       free(vsocket);
+                       vhost_user_socket_mem_free(vsocket);
 
                        count = --vhost_user.vsocket_cnt;
                        vhost_user.vsockets[i] = vhost_user.vsockets[count];
@@ -1000,7 +1031,6 @@ rte_vhost_driver_start(const char *path)
 {
        struct vhost_user_socket *vsocket;
        static pthread_t fdset_tid;
-       char thread_name[RTE_MAX_THREAD_NAME_LEN];
 
        pthread_mutex_lock(&vhost_user.mutex);
        vsocket = find_vhost_user_socket(path);
@@ -1020,22 +1050,15 @@ rte_vhost_driver_start(const char *path)
                        return -1;
                }
 
-               int ret = rte_ctrl_thread_create(&fdset_tid, NULL,
-                       fdset_event_dispatch, &vhost_user.fdset);
+               int ret = rte_ctrl_thread_create(&fdset_tid,
+                       "vhost-events", NULL, fdset_event_dispatch,
+                       &vhost_user.fdset);
                if (ret != 0) {
                        RTE_LOG(ERR, VHOST_CONFIG,
                                "failed to create fdset handling thread");
 
                        fdset_pipe_uninit(&vhost_user.fdset);
                        return -1;
-               } else {
-                       snprintf(thread_name, sizeof(thread_name),
-                                "vhost-events");
-
-                       if (rte_thread_setname(fdset_tid, thread_name)) {
-                               RTE_LOG(DEBUG, VHOST_CONFIG,
-                                       "failed to set vhost-event thread name");
-                       }
                }
        }