ipsec: reorder packet check for ESP inbound
[dpdk.git] / lib / librte_vhost / socket.c
index 4ce916d..3da9de6 100644 (file)
@@ -51,6 +51,8 @@ struct vhost_user_socket {
        uint64_t supported_features;
        uint64_t features;
 
+       uint64_t protocol_features;
+
        /*
         * Device id to identify a specific backend device.
         * It's set to -1 for the default software implementation.
@@ -88,24 +90,30 @@ static struct vhost_user vhost_user = {
        .fdset = {
                .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
                .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
+               .fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
                .num = 0
        },
        .vsocket_cnt = 0,
        .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 +139,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 +208,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);
@@ -695,6 +707,20 @@ unlock_exit:
        return ret;
 }
 
+int
+rte_vhost_driver_set_protocol_features(const char *path,
+               uint64_t protocol_features)
+{
+       struct vhost_user_socket *vsocket;
+
+       pthread_mutex_lock(&vhost_user.mutex);
+       vsocket = find_vhost_user_socket(path);
+       if (vsocket)
+               vsocket->protocol_features = protocol_features;
+       pthread_mutex_unlock(&vhost_user.mutex);
+       return vsocket ? 0 : -1;
+}
+
 int
 rte_vhost_driver_get_protocol_features(const char *path,
                uint64_t *protocol_features)
@@ -717,7 +743,7 @@ rte_vhost_driver_get_protocol_features(const char *path,
        did = vsocket->vdpa_dev_id;
        vdpa_dev = rte_vdpa_get_device(did);
        if (!vdpa_dev || !vdpa_dev->ops->get_protocol_features) {
-               *protocol_features = VHOST_USER_PROTOCOL_FEATURES;
+               *protocol_features = vsocket->protocol_features;
                goto unlock_exit;
        }
 
@@ -730,7 +756,7 @@ rte_vhost_driver_get_protocol_features(const char *path,
                goto unlock_exit;
        }
 
-       *protocol_features = VHOST_USER_PROTOCOL_FEATURES
+       *protocol_features = vsocket->protocol_features
                & vdpa_protocol_features;
 
 unlock_exit:
@@ -778,6 +804,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
@@ -808,7 +848,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);
@@ -835,12 +875,40 @@ rte_vhost_driver_register(const char *path, uint64_t flags)
        vsocket->use_builtin_virtio_net = true;
        vsocket->supported_features = VIRTIO_NET_SUPPORTED_FEATURES;
        vsocket->features           = VIRTIO_NET_SUPPORTED_FEATURES;
+       vsocket->protocol_features  = VHOST_USER_PROTOCOL_FEATURES;
+
+       /*
+        * Dequeue zero copy can't assure descriptors returned in order.
+        * Also, it requires that the guest memory is populated, which is
+        * not compatible with postcopy.
+        */
+       if (vsocket->dequeue_zero_copy) {
+               vsocket->supported_features &= ~(1ULL << VIRTIO_F_IN_ORDER);
+               vsocket->features &= ~(1ULL << VIRTIO_F_IN_ORDER);
+
+               RTE_LOG(INFO, VHOST_CONFIG,
+                       "Dequeue zero copy requested, disabling postcopy support\n");
+               vsocket->protocol_features &=
+                       ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
+       }
 
        if (!(flags & RTE_VHOST_USER_IOMMU_SUPPORT)) {
                vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
                vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
        }
 
+       if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
+               vsocket->protocol_features &=
+                       ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
+       } else {
+#ifndef RTE_LIBRTE_VHOST_POSTCOPY
+               RTE_LOG(ERR, VHOST_CONFIG,
+                       "Postcopy requested but not compiled\n");
+               ret = -1;
+               goto out_mutex;
+#endif
+       }
+
        if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
                vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
                if (vsocket->reconnect && reconn_tid == 0) {
@@ -866,8 +934,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);
 
@@ -908,21 +975,13 @@ rte_vhost_driver_unregister(const char *path)
        int count;
        struct vhost_user_connection *conn, *next;
 
+again:
        pthread_mutex_lock(&vhost_user.mutex);
 
        for (i = 0; i < vhost_user.vsocket_cnt; i++) {
                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;
@@ -938,6 +997,7 @@ again:
                                                  conn->connfd) == -1) {
                                        pthread_mutex_unlock(
                                                        &vsocket->conn_mutex);
+                                       pthread_mutex_unlock(&vhost_user.mutex);
                                        goto again;
                                }
 
@@ -951,9 +1011,17 @@ again:
                        }
                        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];