LCOV - code coverage report
Current view: top level - lib/vhost - socket.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 524 0.0 %
Date: 2024-01-22 16:13:49 Functions: 0 31 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 266 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2016 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <stdint.h>
       6                 :            : #include <stdio.h>
       7                 :            : #include <limits.h>
       8                 :            : #include <stdlib.h>
       9                 :            : #include <unistd.h>
      10                 :            : #include <string.h>
      11                 :            : #include <sys/socket.h>
      12                 :            : #include <sys/un.h>
      13                 :            : #include <sys/queue.h>
      14                 :            : #include <errno.h>
      15                 :            : #include <fcntl.h>
      16                 :            : 
      17                 :            : #include <rte_thread.h>
      18                 :            : #include <rte_log.h>
      19                 :            : 
      20                 :            : #include "fd_man.h"
      21                 :            : #include "vduse.h"
      22                 :            : #include "vhost.h"
      23                 :            : #include "vhost_user.h"
      24                 :            : 
      25                 :            : 
      26                 :            : TAILQ_HEAD(vhost_user_connection_list, vhost_user_connection);
      27                 :            : 
      28                 :            : /*
      29                 :            :  * Every time rte_vhost_driver_register() is invoked, an associated
      30                 :            :  * vhost_user_socket struct will be created.
      31                 :            :  */
      32                 :            : struct vhost_user_socket {
      33                 :            :         struct vhost_user_connection_list conn_list;
      34                 :            :         pthread_mutex_t conn_mutex;
      35                 :            :         char *path;
      36                 :            :         int socket_fd;
      37                 :            :         struct sockaddr_un un;
      38                 :            :         bool is_server;
      39                 :            :         bool is_vduse;
      40                 :            :         bool reconnect;
      41                 :            :         bool iommu_support;
      42                 :            :         bool use_builtin_virtio_net;
      43                 :            :         bool extbuf;
      44                 :            :         bool linearbuf;
      45                 :            :         bool async_copy;
      46                 :            :         bool net_compliant_ol_flags;
      47                 :            :         bool stats_enabled;
      48                 :            : 
      49                 :            :         /*
      50                 :            :          * The "supported_features" indicates the feature bits the
      51                 :            :          * vhost driver supports. The "features" indicates the feature
      52                 :            :          * bits after the rte_vhost_driver_features_disable/enable().
      53                 :            :          * It is also the final feature bits used for vhost-user
      54                 :            :          * features negotiation.
      55                 :            :          */
      56                 :            :         uint64_t supported_features;
      57                 :            :         uint64_t features;
      58                 :            : 
      59                 :            :         uint64_t protocol_features;
      60                 :            : 
      61                 :            :         uint32_t max_queue_pairs;
      62                 :            : 
      63                 :            :         struct rte_vdpa_device *vdpa_dev;
      64                 :            : 
      65                 :            :         struct rte_vhost_device_ops const *notify_ops;
      66                 :            : };
      67                 :            : 
      68                 :            : struct vhost_user_connection {
      69                 :            :         struct vhost_user_socket *vsocket;
      70                 :            :         int connfd;
      71                 :            :         int vid;
      72                 :            : 
      73                 :            :         TAILQ_ENTRY(vhost_user_connection) next;
      74                 :            : };
      75                 :            : 
      76                 :            : #define MAX_VHOST_SOCKET 1024
      77                 :            : struct vhost_user {
      78                 :            :         struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
      79                 :            :         struct fdset fdset;
      80                 :            :         int vsocket_cnt;
      81                 :            :         pthread_mutex_t mutex;
      82                 :            : };
      83                 :            : 
      84                 :            : #define MAX_VIRTIO_BACKLOG 128
      85                 :            : 
      86                 :            : static void vhost_user_server_new_connection(int fd, void *data, int *remove);
      87                 :            : static void vhost_user_read_cb(int fd, void *dat, int *remove);
      88                 :            : static int create_unix_socket(struct vhost_user_socket *vsocket);
      89                 :            : static int vhost_user_start_client(struct vhost_user_socket *vsocket);
      90                 :            : 
      91                 :            : static struct vhost_user vhost_user = {
      92                 :            :         .fdset = {
      93                 :            :                 .fd = { [0 ... MAX_FDS - 1] = {-1, NULL, NULL, NULL, 0} },
      94                 :            :                 .fd_mutex = PTHREAD_MUTEX_INITIALIZER,
      95                 :            :                 .fd_pooling_mutex = PTHREAD_MUTEX_INITIALIZER,
      96                 :            :                 .num = 0
      97                 :            :         },
      98                 :            :         .vsocket_cnt = 0,
      99                 :            :         .mutex = PTHREAD_MUTEX_INITIALIZER,
     100                 :            : };
     101                 :            : 
     102                 :            : /*
     103                 :            :  * return bytes# of read on success or negative val on failure. Update fdnum
     104                 :            :  * with number of fds read.
     105                 :            :  */
     106                 :            : int
     107                 :          0 : read_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int max_fds,
     108                 :            :                 int *fd_num)
     109                 :          0 : {
     110                 :            :         struct iovec iov;
     111                 :            :         struct msghdr msgh;
     112                 :          0 :         char control[CMSG_SPACE(max_fds * sizeof(int))];
     113                 :            :         struct cmsghdr *cmsg;
     114                 :            :         int got_fds = 0;
     115                 :            :         int ret;
     116                 :            : 
     117                 :          0 :         *fd_num = 0;
     118                 :            : 
     119                 :            :         memset(&msgh, 0, sizeof(msgh));
     120                 :          0 :         iov.iov_base = buf;
     121                 :          0 :         iov.iov_len  = buflen;
     122                 :            : 
     123                 :          0 :         msgh.msg_iov = &iov;
     124                 :          0 :         msgh.msg_iovlen = 1;
     125                 :          0 :         msgh.msg_control = control;
     126                 :          0 :         msgh.msg_controllen = sizeof(control);
     127                 :            : 
     128                 :          0 :         ret = recvmsg(sockfd, &msgh, 0);
     129         [ #  # ]:          0 :         if (ret <= 0) {
     130         [ #  # ]:          0 :                 if (ret)
     131                 :          0 :                         VHOST_CONFIG_LOG(ifname, ERR, "recvmsg failed on fd %d (%s)",
     132                 :            :                                 sockfd, strerror(errno));
     133                 :          0 :                 return ret;
     134                 :            :         }
     135                 :            : 
     136         [ #  # ]:          0 :         if (msgh.msg_flags & MSG_TRUNC)
     137                 :          0 :                 VHOST_CONFIG_LOG(ifname, ERR, "truncated msg (fd %d)", sockfd);
     138                 :            : 
     139                 :            :         /* MSG_CTRUNC may be caused by LSM misconfiguration */
     140         [ #  # ]:          0 :         if (msgh.msg_flags & MSG_CTRUNC)
     141                 :          0 :                 VHOST_CONFIG_LOG(ifname, ERR, "truncated control data (fd %d)", sockfd);
     142                 :            : 
     143   [ #  #  #  # ]:          0 :         for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
     144                 :            :                 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
     145         [ #  # ]:          0 :                 if ((cmsg->cmsg_level == SOL_SOCKET) &&
     146                 :            :                         (cmsg->cmsg_type == SCM_RIGHTS)) {
     147                 :          0 :                         got_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
     148                 :          0 :                         *fd_num = got_fds;
     149                 :          0 :                         memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
     150                 :            :                         break;
     151                 :            :                 }
     152                 :            :         }
     153                 :            : 
     154                 :            :         /* Clear out unused file descriptors */
     155         [ #  # ]:          0 :         while (got_fds < max_fds)
     156                 :          0 :                 fds[got_fds++] = -1;
     157                 :            : 
     158                 :            :         return ret;
     159                 :            : }
     160                 :            : 
     161                 :            : int
     162                 :          0 : send_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int fd_num)
     163                 :          0 : {
     164                 :            : 
     165                 :            :         struct iovec iov;
     166                 :            :         struct msghdr msgh;
     167                 :          0 :         size_t fdsize = fd_num * sizeof(int);
     168         [ #  # ]:          0 :         char control[CMSG_SPACE(fdsize)];
     169                 :            :         struct cmsghdr *cmsg;
     170                 :            :         int ret;
     171                 :            : 
     172                 :            :         memset(&msgh, 0, sizeof(msgh));
     173                 :          0 :         iov.iov_base = buf;
     174                 :          0 :         iov.iov_len = buflen;
     175                 :            : 
     176                 :          0 :         msgh.msg_iov = &iov;
     177                 :          0 :         msgh.msg_iovlen = 1;
     178                 :            : 
     179         [ #  # ]:          0 :         if (fds && fd_num > 0) {
     180                 :          0 :                 msgh.msg_control = control;
     181                 :          0 :                 msgh.msg_controllen = sizeof(control);
     182         [ #  # ]:          0 :                 cmsg = CMSG_FIRSTHDR(&msgh);
     183                 :            :                 if (cmsg == NULL) {
     184                 :          0 :                         VHOST_CONFIG_LOG(ifname, ERR, "cmsg == NULL");
     185                 :          0 :                         errno = EINVAL;
     186                 :          0 :                         return -1;
     187                 :            :                 }
     188                 :          0 :                 cmsg->cmsg_len = CMSG_LEN(fdsize);
     189                 :          0 :                 cmsg->cmsg_level = SOL_SOCKET;
     190                 :          0 :                 cmsg->cmsg_type = SCM_RIGHTS;
     191                 :          0 :                 memcpy(CMSG_DATA(cmsg), fds, fdsize);
     192                 :            :         } else {
     193                 :            :                 msgh.msg_control = NULL;
     194                 :            :                 msgh.msg_controllen = 0;
     195                 :            :         }
     196                 :            : 
     197                 :            :         do {
     198                 :          0 :                 ret = sendmsg(sockfd, &msgh, MSG_NOSIGNAL);
     199   [ #  #  #  # ]:          0 :         } while (ret < 0 && errno == EINTR);
     200                 :            : 
     201         [ #  # ]:          0 :         if (ret < 0) {
     202                 :          0 :                 VHOST_CONFIG_LOG(ifname, ERR, "sendmsg error on fd %d (%s)",
     203                 :            :                         sockfd, strerror(errno));
     204                 :          0 :                 return ret;
     205                 :            :         }
     206                 :            : 
     207                 :            :         return ret;
     208                 :            : }
     209                 :            : 
     210                 :            : static void
     211                 :          0 : vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
     212                 :            : {
     213                 :            :         int vid;
     214                 :            :         size_t size;
     215                 :            :         struct vhost_user_connection *conn;
     216                 :            :         int ret;
     217                 :            :         struct virtio_net *dev;
     218                 :            : 
     219         [ #  # ]:          0 :         if (vsocket == NULL)
     220                 :            :                 return;
     221                 :            : 
     222                 :          0 :         conn = malloc(sizeof(*conn));
     223         [ #  # ]:          0 :         if (conn == NULL) {
     224                 :          0 :                 close(fd);
     225                 :          0 :                 return;
     226                 :            :         }
     227                 :            : 
     228                 :          0 :         vid = vhost_user_new_device();
     229         [ #  # ]:          0 :         if (vid == -1) {
     230                 :          0 :                 goto err;
     231                 :            :         }
     232                 :            : 
     233                 :          0 :         size = strnlen(vsocket->path, PATH_MAX);
     234                 :          0 :         vhost_set_ifname(vid, vsocket->path, size);
     235                 :            : 
     236                 :          0 :         vhost_setup_virtio_net(vid, vsocket->use_builtin_virtio_net,
     237                 :          0 :                 vsocket->net_compliant_ol_flags, vsocket->stats_enabled,
     238                 :          0 :                 vsocket->iommu_support);
     239                 :            : 
     240                 :          0 :         vhost_attach_vdpa_device(vid, vsocket->vdpa_dev);
     241                 :            : 
     242         [ #  # ]:          0 :         if (vsocket->extbuf)
     243                 :          0 :                 vhost_enable_extbuf(vid);
     244                 :            : 
     245         [ #  # ]:          0 :         if (vsocket->linearbuf)
     246                 :          0 :                 vhost_enable_linearbuf(vid);
     247                 :            : 
     248         [ #  # ]:          0 :         if (vsocket->async_copy) {
     249                 :            :                 dev = get_device(vid);
     250                 :            : 
     251         [ #  # ]:          0 :                 if (dev)
     252                 :          0 :                         dev->async_copy = 1;
     253                 :            :         }
     254                 :            : 
     255                 :          0 :         VHOST_CONFIG_LOG(vsocket->path, INFO, "new device, handle is %d", vid);
     256                 :            : 
     257         [ #  # ]:          0 :         if (vsocket->notify_ops->new_connection) {
     258                 :          0 :                 ret = vsocket->notify_ops->new_connection(vid);
     259         [ #  # ]:          0 :                 if (ret < 0) {
     260                 :          0 :                         VHOST_CONFIG_LOG(vsocket->path, ERR,
     261                 :            :                                 "failed to add vhost user connection with fd %d",
     262                 :            :                                 fd);
     263                 :          0 :                         goto err_cleanup;
     264                 :            :                 }
     265                 :            :         }
     266                 :            : 
     267                 :          0 :         conn->connfd = fd;
     268                 :          0 :         conn->vsocket = vsocket;
     269                 :          0 :         conn->vid = vid;
     270                 :          0 :         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_read_cb,
     271                 :            :                         NULL, conn);
     272         [ #  # ]:          0 :         if (ret < 0) {
     273                 :          0 :                 VHOST_CONFIG_LOG(vsocket->path, ERR,
     274                 :            :                         "failed to add fd %d into vhost server fdset",
     275                 :            :                         fd);
     276                 :            : 
     277         [ #  # ]:          0 :                 if (vsocket->notify_ops->destroy_connection)
     278                 :          0 :                         vsocket->notify_ops->destroy_connection(conn->vid);
     279                 :            : 
     280                 :          0 :                 goto err_cleanup;
     281                 :            :         }
     282                 :            : 
     283                 :          0 :         pthread_mutex_lock(&vsocket->conn_mutex);
     284                 :          0 :         TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
     285                 :          0 :         pthread_mutex_unlock(&vsocket->conn_mutex);
     286                 :            : 
     287                 :          0 :         fdset_pipe_notify(&vhost_user.fdset);
     288                 :          0 :         return;
     289                 :            : 
     290                 :          0 : err_cleanup:
     291                 :          0 :         vhost_destroy_device(vid);
     292                 :          0 : err:
     293                 :          0 :         free(conn);
     294                 :          0 :         close(fd);
     295                 :            : }
     296                 :            : 
     297                 :            : /* call back when there is new vhost-user connection from client  */
     298                 :            : static void
     299                 :          0 : vhost_user_server_new_connection(int fd, void *dat, int *remove __rte_unused)
     300                 :            : {
     301                 :            :         struct vhost_user_socket *vsocket = dat;
     302                 :            : 
     303                 :          0 :         fd = accept(fd, NULL, NULL);
     304         [ #  # ]:          0 :         if (fd < 0)
     305                 :            :                 return;
     306                 :            : 
     307                 :          0 :         VHOST_CONFIG_LOG(vsocket->path, INFO, "new vhost user connection is %d", fd);
     308                 :          0 :         vhost_user_add_connection(fd, vsocket);
     309                 :            : }
     310                 :            : 
     311                 :            : static void
     312                 :          0 : vhost_user_read_cb(int connfd, void *dat, int *remove)
     313                 :            : {
     314                 :            :         struct vhost_user_connection *conn = dat;
     315                 :          0 :         struct vhost_user_socket *vsocket = conn->vsocket;
     316                 :            :         int ret;
     317                 :            : 
     318                 :          0 :         ret = vhost_user_msg_handler(conn->vid, connfd);
     319         [ #  # ]:          0 :         if (ret < 0) {
     320         [ #  # ]:          0 :                 struct virtio_net *dev = get_device(conn->vid);
     321                 :            : 
     322                 :          0 :                 close(connfd);
     323                 :          0 :                 *remove = 1;
     324                 :            : 
     325         [ #  # ]:          0 :                 if (dev)
     326                 :          0 :                         vhost_destroy_device_notify(dev);
     327                 :            : 
     328         [ #  # ]:          0 :                 if (vsocket->notify_ops->destroy_connection)
     329                 :          0 :                         vsocket->notify_ops->destroy_connection(conn->vid);
     330                 :            : 
     331                 :          0 :                 vhost_destroy_device(conn->vid);
     332                 :            : 
     333         [ #  # ]:          0 :                 if (vsocket->reconnect) {
     334                 :          0 :                         create_unix_socket(vsocket);
     335                 :          0 :                         vhost_user_start_client(vsocket);
     336                 :            :                 }
     337                 :            : 
     338                 :          0 :                 pthread_mutex_lock(&vsocket->conn_mutex);
     339         [ #  # ]:          0 :                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
     340                 :          0 :                 pthread_mutex_unlock(&vsocket->conn_mutex);
     341                 :            : 
     342                 :          0 :                 free(conn);
     343                 :            :         }
     344                 :          0 : }
     345                 :            : 
     346                 :            : static int
     347                 :          0 : create_unix_socket(struct vhost_user_socket *vsocket)
     348                 :            : {
     349                 :            :         int fd;
     350                 :          0 :         struct sockaddr_un *un = &vsocket->un;
     351                 :            : 
     352                 :          0 :         fd = socket(AF_UNIX, SOCK_STREAM, 0);
     353         [ #  # ]:          0 :         if (fd < 0)
     354                 :            :                 return -1;
     355         [ #  # ]:          0 :         VHOST_CONFIG_LOG(vsocket->path, INFO, "vhost-user %s: socket created, fd: %d",
     356                 :            :                 vsocket->is_server ? "server" : "client", fd);
     357                 :            : 
     358   [ #  #  #  # ]:          0 :         if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
     359                 :          0 :                 VHOST_CONFIG_LOG(vsocket->path, ERR,
     360                 :            :                         "vhost-user: can't set nonblocking mode for socket, fd: %d (%s)",
     361                 :            :                         fd, strerror(errno));
     362                 :          0 :                 close(fd);
     363                 :          0 :                 return -1;
     364                 :            :         }
     365                 :            : 
     366                 :            :         memset(un, 0, sizeof(*un));
     367                 :          0 :         un->sun_family = AF_UNIX;
     368                 :          0 :         strncpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
     369                 :          0 :         un->sun_path[sizeof(un->sun_path) - 1] = '\0';
     370                 :            : 
     371                 :          0 :         vsocket->socket_fd = fd;
     372                 :          0 :         return 0;
     373                 :            : }
     374                 :            : 
     375                 :            : static int
     376                 :          0 : vhost_user_start_server(struct vhost_user_socket *vsocket)
     377                 :            : {
     378                 :            :         int ret;
     379                 :          0 :         int fd = vsocket->socket_fd;
     380                 :          0 :         const char *path = vsocket->path;
     381                 :            : 
     382                 :            :         /*
     383                 :            :          * bind () may fail if the socket file with the same name already
     384                 :            :          * exists. But the library obviously should not delete the file
     385                 :            :          * provided by the user, since we can not be sure that it is not
     386                 :            :          * being used by other applications. Moreover, many applications form
     387                 :            :          * socket names based on user input, which is prone to errors.
     388                 :            :          *
     389                 :            :          * The user must ensure that the socket does not exist before
     390                 :            :          * registering the vhost driver in server mode.
     391                 :            :          */
     392                 :          0 :         ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
     393         [ #  # ]:          0 :         if (ret < 0) {
     394                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to bind: %s; remove it and try again",
     395                 :            :                         strerror(errno));
     396                 :          0 :                 goto err;
     397                 :            :         }
     398                 :          0 :         VHOST_CONFIG_LOG(path, INFO, "binding succeeded");
     399                 :            : 
     400                 :          0 :         ret = listen(fd, MAX_VIRTIO_BACKLOG);
     401         [ #  # ]:          0 :         if (ret < 0)
     402                 :          0 :                 goto err;
     403                 :            : 
     404                 :          0 :         ret = fdset_add(&vhost_user.fdset, fd, vhost_user_server_new_connection,
     405                 :            :                   NULL, vsocket);
     406         [ #  # ]:          0 :         if (ret < 0) {
     407                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to add listen fd %d to vhost server fdset",
     408                 :            :                         fd);
     409                 :          0 :                 goto err;
     410                 :            :         }
     411                 :            : 
     412                 :            :         return 0;
     413                 :            : 
     414                 :          0 : err:
     415                 :          0 :         close(fd);
     416                 :          0 :         return -1;
     417                 :            : }
     418                 :            : 
     419                 :            : struct vhost_user_reconnect {
     420                 :            :         struct sockaddr_un un;
     421                 :            :         int fd;
     422                 :            :         struct vhost_user_socket *vsocket;
     423                 :            : 
     424                 :            :         TAILQ_ENTRY(vhost_user_reconnect) next;
     425                 :            : };
     426                 :            : 
     427                 :            : TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
     428                 :            : struct vhost_user_reconnect_list {
     429                 :            :         struct vhost_user_reconnect_tailq_list head;
     430                 :            :         pthread_mutex_t mutex;
     431                 :            : };
     432                 :            : 
     433                 :            : static struct vhost_user_reconnect_list reconn_list;
     434                 :            : static rte_thread_t reconn_tid;
     435                 :            : 
     436                 :            : static int
     437                 :          0 : vhost_user_connect_nonblock(char *path, int fd, struct sockaddr *un, size_t sz)
     438                 :            : {
     439                 :            :         int ret, flags;
     440                 :            : 
     441                 :          0 :         ret = connect(fd, un, sz);
     442   [ #  #  #  # ]:          0 :         if (ret < 0 && errno != EISCONN)
     443                 :            :                 return -1;
     444                 :            : 
     445                 :          0 :         flags = fcntl(fd, F_GETFL, 0);
     446         [ #  # ]:          0 :         if (flags < 0) {
     447                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "can't get flags for connfd %d (%s)",
     448                 :            :                         fd, strerror(errno));
     449                 :          0 :                 return -2;
     450                 :            :         }
     451   [ #  #  #  # ]:          0 :         if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
     452                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "can't disable nonblocking on fd %d", fd);
     453                 :          0 :                 return -2;
     454                 :            :         }
     455                 :            :         return 0;
     456                 :            : }
     457                 :            : 
     458                 :            : static uint32_t
     459                 :          0 : vhost_user_client_reconnect(void *arg __rte_unused)
     460                 :            : {
     461                 :            :         int ret;
     462                 :            :         struct vhost_user_reconnect *reconn, *next;
     463                 :            : 
     464                 :            :         while (1) {
     465                 :          0 :                 pthread_mutex_lock(&reconn_list.mutex);
     466                 :            : 
     467                 :            :                 /*
     468                 :            :                  * An equal implementation of TAILQ_FOREACH_SAFE,
     469                 :            :                  * which does not exist on all platforms.
     470                 :            :                  */
     471                 :          0 :                 for (reconn = TAILQ_FIRST(&reconn_list.head);
     472         [ #  # ]:          0 :                      reconn != NULL; reconn = next) {
     473                 :          0 :                         next = TAILQ_NEXT(reconn, next);
     474                 :            : 
     475                 :          0 :                         ret = vhost_user_connect_nonblock(reconn->vsocket->path, reconn->fd,
     476                 :          0 :                                                 (struct sockaddr *)&reconn->un,
     477                 :            :                                                 sizeof(reconn->un));
     478         [ #  # ]:          0 :                         if (ret == -2) {
     479                 :          0 :                                 close(reconn->fd);
     480                 :          0 :                                 VHOST_CONFIG_LOG(reconn->vsocket->path, ERR,
     481                 :            :                                         "reconnection for fd %d failed",
     482                 :            :                                         reconn->fd);
     483                 :          0 :                                 goto remove_fd;
     484                 :            :                         }
     485         [ #  # ]:          0 :                         if (ret == -1)
     486                 :          0 :                                 continue;
     487                 :            : 
     488                 :          0 :                         VHOST_CONFIG_LOG(reconn->vsocket->path, INFO, "connected");
     489                 :          0 :                         vhost_user_add_connection(reconn->fd, reconn->vsocket);
     490                 :          0 : remove_fd:
     491         [ #  # ]:          0 :                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
     492                 :          0 :                         free(reconn);
     493                 :            :                 }
     494                 :            : 
     495                 :          0 :                 pthread_mutex_unlock(&reconn_list.mutex);
     496                 :          0 :                 sleep(1);
     497                 :            :         }
     498                 :            : 
     499                 :            :         return 0;
     500                 :            : }
     501                 :            : 
     502                 :            : static int
     503                 :          0 : vhost_user_reconnect_init(void)
     504                 :            : {
     505                 :            :         int ret;
     506                 :            : 
     507                 :          0 :         ret = pthread_mutex_init(&reconn_list.mutex, NULL);
     508         [ #  # ]:          0 :         if (ret < 0) {
     509                 :          0 :                 VHOST_CONFIG_LOG("thread", ERR, "%s: failed to initialize mutex", __func__);
     510                 :          0 :                 return ret;
     511                 :            :         }
     512                 :          0 :         TAILQ_INIT(&reconn_list.head);
     513                 :            : 
     514                 :          0 :         ret = rte_thread_create_internal_control(&reconn_tid, "vhost-reco",
     515                 :            :                         vhost_user_client_reconnect, NULL);
     516         [ #  # ]:          0 :         if (ret != 0) {
     517                 :          0 :                 VHOST_CONFIG_LOG("thread", ERR, "failed to create reconnect thread");
     518         [ #  # ]:          0 :                 if (pthread_mutex_destroy(&reconn_list.mutex))
     519                 :          0 :                         VHOST_CONFIG_LOG("thread", ERR,
     520                 :            :                                 "%s: failed to destroy reconnect mutex",
     521                 :            :                                 __func__);
     522                 :            :         }
     523                 :            : 
     524                 :            :         return ret;
     525                 :            : }
     526                 :            : 
     527                 :            : static int
     528                 :          0 : vhost_user_start_client(struct vhost_user_socket *vsocket)
     529                 :            : {
     530                 :            :         int ret;
     531                 :          0 :         int fd = vsocket->socket_fd;
     532                 :          0 :         const char *path = vsocket->path;
     533                 :            :         struct vhost_user_reconnect *reconn;
     534                 :            : 
     535                 :          0 :         ret = vhost_user_connect_nonblock(vsocket->path, fd, (struct sockaddr *)&vsocket->un,
     536                 :            :                                           sizeof(vsocket->un));
     537         [ #  # ]:          0 :         if (ret == 0) {
     538                 :          0 :                 vhost_user_add_connection(fd, vsocket);
     539                 :          0 :                 return 0;
     540                 :            :         }
     541                 :            : 
     542                 :          0 :         VHOST_CONFIG_LOG(path, WARNING, "failed to connect: %s", strerror(errno));
     543                 :            : 
     544   [ #  #  #  # ]:          0 :         if (ret == -2 || !vsocket->reconnect) {
     545                 :          0 :                 close(fd);
     546                 :          0 :                 return -1;
     547                 :            :         }
     548                 :            : 
     549                 :          0 :         VHOST_CONFIG_LOG(path, INFO, "reconnecting...");
     550                 :          0 :         reconn = malloc(sizeof(*reconn));
     551         [ #  # ]:          0 :         if (reconn == NULL) {
     552                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to allocate memory for reconnect");
     553                 :          0 :                 close(fd);
     554                 :          0 :                 return -1;
     555                 :            :         }
     556                 :          0 :         reconn->un = vsocket->un;
     557                 :          0 :         reconn->fd = fd;
     558                 :          0 :         reconn->vsocket = vsocket;
     559                 :          0 :         pthread_mutex_lock(&reconn_list.mutex);
     560                 :          0 :         TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
     561                 :          0 :         pthread_mutex_unlock(&reconn_list.mutex);
     562                 :            : 
     563                 :          0 :         return 0;
     564                 :            : }
     565                 :            : 
     566                 :            : static struct vhost_user_socket *
     567                 :          0 : find_vhost_user_socket(const char *path)
     568                 :            : {
     569                 :            :         int i;
     570                 :            : 
     571         [ #  # ]:          0 :         if (path == NULL)
     572                 :            :                 return NULL;
     573                 :            : 
     574         [ #  # ]:          0 :         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
     575                 :          0 :                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
     576                 :            : 
     577         [ #  # ]:          0 :                 if (!strcmp(vsocket->path, path))
     578                 :          0 :                         return vsocket;
     579                 :            :         }
     580                 :            : 
     581                 :            :         return NULL;
     582                 :            : }
     583                 :            : 
     584                 :            : int
     585                 :          0 : rte_vhost_driver_attach_vdpa_device(const char *path,
     586                 :            :                 struct rte_vdpa_device *dev)
     587                 :            : {
     588                 :            :         struct vhost_user_socket *vsocket;
     589                 :            : 
     590         [ #  # ]:          0 :         if (dev == NULL || path == NULL)
     591                 :            :                 return -1;
     592                 :            : 
     593                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     594                 :          0 :         vsocket = find_vhost_user_socket(path);
     595         [ #  # ]:          0 :         if (vsocket)
     596                 :          0 :                 vsocket->vdpa_dev = dev;
     597                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     598                 :            : 
     599         [ #  # ]:          0 :         return vsocket ? 0 : -1;
     600                 :            : }
     601                 :            : 
     602                 :            : int
     603                 :          0 : rte_vhost_driver_detach_vdpa_device(const char *path)
     604                 :            : {
     605                 :            :         struct vhost_user_socket *vsocket;
     606                 :            : 
     607                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     608                 :          0 :         vsocket = find_vhost_user_socket(path);
     609         [ #  # ]:          0 :         if (vsocket)
     610                 :          0 :                 vsocket->vdpa_dev = NULL;
     611                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     612                 :            : 
     613         [ #  # ]:          0 :         return vsocket ? 0 : -1;
     614                 :            : }
     615                 :            : 
     616                 :            : struct rte_vdpa_device *
     617                 :          0 : rte_vhost_driver_get_vdpa_device(const char *path)
     618                 :            : {
     619                 :            :         struct vhost_user_socket *vsocket;
     620                 :            :         struct rte_vdpa_device *dev = NULL;
     621                 :            : 
     622                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     623                 :          0 :         vsocket = find_vhost_user_socket(path);
     624         [ #  # ]:          0 :         if (vsocket)
     625                 :          0 :                 dev = vsocket->vdpa_dev;
     626                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     627                 :            : 
     628                 :          0 :         return dev;
     629                 :            : }
     630                 :            : 
     631                 :            : int
     632                 :          0 : rte_vhost_driver_get_vdpa_dev_type(const char *path, uint32_t *type)
     633                 :            : {
     634                 :            :         struct vhost_user_socket *vsocket;
     635                 :            :         struct rte_vdpa_device *vdpa_dev;
     636                 :            :         int ret = 0;
     637                 :            : 
     638                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     639                 :          0 :         vsocket = find_vhost_user_socket(path);
     640         [ #  # ]:          0 :         if (!vsocket) {
     641                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
     642                 :            :                 ret = -1;
     643                 :          0 :                 goto unlock_exit;
     644                 :            :         }
     645                 :            : 
     646                 :          0 :         vdpa_dev = vsocket->vdpa_dev;
     647         [ #  # ]:          0 :         if (!vdpa_dev) {
     648                 :            :                 ret = -1;
     649                 :          0 :                 goto unlock_exit;
     650                 :            :         }
     651                 :            : 
     652                 :          0 :         *type = vdpa_dev->type;
     653                 :            : 
     654                 :          0 : unlock_exit:
     655                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     656                 :          0 :         return ret;
     657                 :            : }
     658                 :            : 
     659                 :            : int
     660                 :          0 : rte_vhost_driver_disable_features(const char *path, uint64_t features)
     661                 :            : {
     662                 :            :         struct vhost_user_socket *vsocket;
     663                 :            : 
     664                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     665                 :          0 :         vsocket = find_vhost_user_socket(path);
     666                 :            : 
     667                 :            :         /* Note that use_builtin_virtio_net is not affected by this function
     668                 :            :          * since callers may want to selectively disable features of the
     669                 :            :          * built-in vhost net device backend.
     670                 :            :          */
     671                 :            : 
     672         [ #  # ]:          0 :         if (vsocket)
     673                 :          0 :                 vsocket->features &= ~features;
     674                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     675                 :            : 
     676         [ #  # ]:          0 :         return vsocket ? 0 : -1;
     677                 :            : }
     678                 :            : 
     679                 :            : int
     680                 :          0 : rte_vhost_driver_enable_features(const char *path, uint64_t features)
     681                 :            : {
     682                 :            :         struct vhost_user_socket *vsocket;
     683                 :            : 
     684                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     685                 :          0 :         vsocket = find_vhost_user_socket(path);
     686         [ #  # ]:          0 :         if (vsocket) {
     687         [ #  # ]:          0 :                 if ((vsocket->supported_features & features) != features) {
     688                 :            :                         /*
     689                 :            :                          * trying to enable features the driver doesn't
     690                 :            :                          * support.
     691                 :            :                          */
     692                 :          0 :                         pthread_mutex_unlock(&vhost_user.mutex);
     693                 :          0 :                         return -1;
     694                 :            :                 }
     695                 :          0 :                 vsocket->features |= features;
     696                 :            :         }
     697                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     698                 :            : 
     699         [ #  # ]:          0 :         return vsocket ? 0 : -1;
     700                 :            : }
     701                 :            : 
     702                 :            : int
     703                 :          0 : rte_vhost_driver_set_features(const char *path, uint64_t features)
     704                 :            : {
     705                 :            :         struct vhost_user_socket *vsocket;
     706                 :            : 
     707                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     708                 :          0 :         vsocket = find_vhost_user_socket(path);
     709         [ #  # ]:          0 :         if (vsocket) {
     710                 :          0 :                 vsocket->supported_features = features;
     711                 :          0 :                 vsocket->features = features;
     712                 :            : 
     713                 :            :                 /* Anyone setting feature bits is implementing their own vhost
     714                 :            :                  * device backend.
     715                 :            :                  */
     716                 :          0 :                 vsocket->use_builtin_virtio_net = false;
     717                 :            :         }
     718                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     719                 :            : 
     720         [ #  # ]:          0 :         return vsocket ? 0 : -1;
     721                 :            : }
     722                 :            : 
     723                 :            : int
     724                 :          0 : rte_vhost_driver_get_features(const char *path, uint64_t *features)
     725                 :            : {
     726                 :            :         struct vhost_user_socket *vsocket;
     727                 :            :         uint64_t vdpa_features;
     728                 :            :         struct rte_vdpa_device *vdpa_dev;
     729                 :            :         int ret = 0;
     730                 :            : 
     731                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     732                 :          0 :         vsocket = find_vhost_user_socket(path);
     733         [ #  # ]:          0 :         if (!vsocket) {
     734                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
     735                 :            :                 ret = -1;
     736                 :          0 :                 goto unlock_exit;
     737                 :            :         }
     738                 :            : 
     739                 :          0 :         vdpa_dev = vsocket->vdpa_dev;
     740         [ #  # ]:          0 :         if (!vdpa_dev) {
     741                 :          0 :                 *features = vsocket->features;
     742                 :          0 :                 goto unlock_exit;
     743                 :            :         }
     744                 :            : 
     745         [ #  # ]:          0 :         if (vdpa_dev->ops->get_features(vdpa_dev, &vdpa_features) < 0) {
     746                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to get vdpa features for socket file.");
     747                 :            :                 ret = -1;
     748                 :          0 :                 goto unlock_exit;
     749                 :            :         }
     750                 :            : 
     751                 :          0 :         *features = vsocket->features & vdpa_features;
     752                 :            : 
     753                 :          0 : unlock_exit:
     754                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     755                 :          0 :         return ret;
     756                 :            : }
     757                 :            : 
     758                 :            : int
     759                 :          0 : rte_vhost_driver_set_protocol_features(const char *path,
     760                 :            :                 uint64_t protocol_features)
     761                 :            : {
     762                 :            :         struct vhost_user_socket *vsocket;
     763                 :            : 
     764                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     765                 :          0 :         vsocket = find_vhost_user_socket(path);
     766         [ #  # ]:          0 :         if (vsocket)
     767                 :          0 :                 vsocket->protocol_features = protocol_features;
     768                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     769         [ #  # ]:          0 :         return vsocket ? 0 : -1;
     770                 :            : }
     771                 :            : 
     772                 :            : int
     773                 :          0 : rte_vhost_driver_get_protocol_features(const char *path,
     774                 :            :                 uint64_t *protocol_features)
     775                 :            : {
     776                 :            :         struct vhost_user_socket *vsocket;
     777                 :            :         uint64_t vdpa_protocol_features;
     778                 :            :         struct rte_vdpa_device *vdpa_dev;
     779                 :            :         int ret = 0;
     780                 :            : 
     781                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     782                 :          0 :         vsocket = find_vhost_user_socket(path);
     783         [ #  # ]:          0 :         if (!vsocket) {
     784                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
     785                 :            :                 ret = -1;
     786                 :          0 :                 goto unlock_exit;
     787                 :            :         }
     788                 :            : 
     789                 :          0 :         vdpa_dev = vsocket->vdpa_dev;
     790         [ #  # ]:          0 :         if (!vdpa_dev) {
     791                 :          0 :                 *protocol_features = vsocket->protocol_features;
     792                 :          0 :                 goto unlock_exit;
     793                 :            :         }
     794                 :            : 
     795         [ #  # ]:          0 :         if (vdpa_dev->ops->get_protocol_features(vdpa_dev,
     796                 :            :                                 &vdpa_protocol_features) < 0) {
     797                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to get vdpa protocol features.");
     798                 :            :                 ret = -1;
     799                 :          0 :                 goto unlock_exit;
     800                 :            :         }
     801                 :            : 
     802                 :          0 :         *protocol_features = vsocket->protocol_features
     803                 :          0 :                 & vdpa_protocol_features;
     804                 :            : 
     805                 :          0 : unlock_exit:
     806                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     807                 :          0 :         return ret;
     808                 :            : }
     809                 :            : 
     810                 :            : int
     811                 :          0 : rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
     812                 :            : {
     813                 :            :         struct vhost_user_socket *vsocket;
     814                 :            :         uint32_t vdpa_queue_num;
     815                 :            :         struct rte_vdpa_device *vdpa_dev;
     816                 :            :         int ret = 0;
     817                 :            : 
     818                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     819                 :          0 :         vsocket = find_vhost_user_socket(path);
     820         [ #  # ]:          0 :         if (!vsocket) {
     821                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
     822                 :            :                 ret = -1;
     823                 :          0 :                 goto unlock_exit;
     824                 :            :         }
     825                 :            : 
     826                 :          0 :         vdpa_dev = vsocket->vdpa_dev;
     827         [ #  # ]:          0 :         if (!vdpa_dev) {
     828                 :          0 :                 *queue_num = vsocket->max_queue_pairs;
     829                 :          0 :                 goto unlock_exit;
     830                 :            :         }
     831                 :            : 
     832         [ #  # ]:          0 :         if (vdpa_dev->ops->get_queue_num(vdpa_dev, &vdpa_queue_num) < 0) {
     833                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to get vdpa queue number.");
     834                 :            :                 ret = -1;
     835                 :          0 :                 goto unlock_exit;
     836                 :            :         }
     837                 :            : 
     838                 :          0 :         *queue_num = RTE_MIN(vsocket->max_queue_pairs, vdpa_queue_num);
     839                 :            : 
     840                 :          0 : unlock_exit:
     841                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     842                 :          0 :         return ret;
     843                 :            : }
     844                 :            : 
     845                 :            : int
     846                 :          0 : rte_vhost_driver_set_max_queue_num(const char *path, uint32_t max_queue_pairs)
     847                 :            : {
     848                 :            :         struct vhost_user_socket *vsocket;
     849                 :            :         int ret = 0;
     850                 :            : 
     851                 :          0 :         VHOST_CONFIG_LOG(path, INFO, "Setting max queue pairs to %u", max_queue_pairs);
     852                 :            : 
     853         [ #  # ]:          0 :         if (max_queue_pairs > VHOST_MAX_QUEUE_PAIRS) {
     854                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "Library only supports up to %u queue pairs",
     855                 :            :                                 VHOST_MAX_QUEUE_PAIRS);
     856                 :          0 :                 return -1;
     857                 :            :         }
     858                 :            : 
     859                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     860                 :          0 :         vsocket = find_vhost_user_socket(path);
     861         [ #  # ]:          0 :         if (!vsocket) {
     862                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
     863                 :            :                 ret = -1;
     864                 :          0 :                 goto unlock_exit;
     865                 :            :         }
     866                 :            : 
     867                 :          0 :         vsocket->max_queue_pairs = max_queue_pairs;
     868                 :            : 
     869                 :          0 : unlock_exit:
     870                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     871                 :          0 :         return ret;
     872                 :            : }
     873                 :            : 
     874                 :            : static void
     875                 :          0 : vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
     876                 :            : {
     877         [ #  # ]:          0 :         if (vsocket == NULL)
     878                 :            :                 return;
     879                 :            : 
     880                 :          0 :         free(vsocket->path);
     881                 :          0 :         free(vsocket);
     882                 :            : }
     883                 :            : 
     884                 :            : /*
     885                 :            :  * Register a new vhost-user socket; here we could act as server
     886                 :            :  * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
     887                 :            :  * is set.
     888                 :            :  */
     889                 :            : int
     890                 :          0 : rte_vhost_driver_register(const char *path, uint64_t flags)
     891                 :            : {
     892                 :            :         int ret = -1;
     893                 :            :         struct vhost_user_socket *vsocket;
     894                 :            : 
     895         [ #  # ]:          0 :         if (!path)
     896                 :            :                 return -1;
     897                 :            : 
     898                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     899                 :            : 
     900         [ #  # ]:          0 :         if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
     901                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "the number of vhost sockets reaches maximum");
     902                 :          0 :                 goto out;
     903                 :            :         }
     904                 :            : 
     905                 :          0 :         vsocket = malloc(sizeof(struct vhost_user_socket));
     906         [ #  # ]:          0 :         if (!vsocket)
     907                 :          0 :                 goto out;
     908                 :            :         memset(vsocket, 0, sizeof(struct vhost_user_socket));
     909                 :          0 :         vsocket->path = strdup(path);
     910         [ #  # ]:          0 :         if (vsocket->path == NULL) {
     911                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to copy socket path string");
     912                 :          0 :                 vhost_user_socket_mem_free(vsocket);
     913                 :          0 :                 goto out;
     914                 :            :         }
     915                 :          0 :         TAILQ_INIT(&vsocket->conn_list);
     916                 :          0 :         ret = pthread_mutex_init(&vsocket->conn_mutex, NULL);
     917         [ #  # ]:          0 :         if (ret) {
     918                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to init connection mutex");
     919                 :          0 :                 goto out_free;
     920                 :            :         }
     921                 :            : 
     922         [ #  # ]:          0 :         if (!strncmp("/dev/vduse/", path, strlen("/dev/vduse/")))
     923                 :          0 :                 vsocket->is_vduse = true;
     924                 :            : 
     925                 :          0 :         vsocket->vdpa_dev = NULL;
     926                 :          0 :         vsocket->max_queue_pairs = VHOST_MAX_QUEUE_PAIRS;
     927                 :          0 :         vsocket->extbuf = flags & RTE_VHOST_USER_EXTBUF_SUPPORT;
     928                 :          0 :         vsocket->linearbuf = flags & RTE_VHOST_USER_LINEARBUF_SUPPORT;
     929                 :          0 :         vsocket->async_copy = flags & RTE_VHOST_USER_ASYNC_COPY;
     930                 :          0 :         vsocket->net_compliant_ol_flags = flags & RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS;
     931                 :          0 :         vsocket->stats_enabled = flags & RTE_VHOST_USER_NET_STATS_ENABLE;
     932         [ #  # ]:          0 :         if (vsocket->is_vduse)
     933                 :          0 :                 vsocket->iommu_support = true;
     934                 :            :         else
     935                 :          0 :                 vsocket->iommu_support = flags & RTE_VHOST_USER_IOMMU_SUPPORT;
     936                 :            : 
     937   [ #  #  #  # ]:          0 :         if (vsocket->async_copy && (vsocket->iommu_support ||
     938         [ #  # ]:          0 :                                 (flags & RTE_VHOST_USER_POSTCOPY_SUPPORT))) {
     939                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "async copy with IOMMU or post-copy not supported");
     940                 :          0 :                 goto out_mutex;
     941                 :            :         }
     942                 :            : 
     943                 :            :         /*
     944                 :            :          * Set the supported features correctly for the builtin vhost-user
     945                 :            :          * net driver.
     946                 :            :          *
     947                 :            :          * Applications know nothing about features the builtin virtio net
     948                 :            :          * driver (virtio_net.c) supports, thus it's not possible for them
     949                 :            :          * to invoke rte_vhost_driver_set_features(). To workaround it, here
     950                 :            :          * we set it unconditionally. If the application want to implement
     951                 :            :          * another vhost-user driver (say SCSI), it should call the
     952                 :            :          * rte_vhost_driver_set_features(), which will overwrite following
     953                 :            :          * two values.
     954                 :            :          */
     955                 :          0 :         vsocket->use_builtin_virtio_net = true;
     956         [ #  # ]:          0 :         if (vsocket->is_vduse) {
     957                 :          0 :                 vsocket->supported_features = VDUSE_NET_SUPPORTED_FEATURES;
     958                 :          0 :                 vsocket->features           = VDUSE_NET_SUPPORTED_FEATURES;
     959                 :            :         } else {
     960                 :          0 :                 vsocket->supported_features = VHOST_USER_NET_SUPPORTED_FEATURES;
     961                 :          0 :                 vsocket->features           = VHOST_USER_NET_SUPPORTED_FEATURES;
     962                 :          0 :                 vsocket->protocol_features  = VHOST_USER_PROTOCOL_FEATURES;
     963                 :            :         }
     964                 :            : 
     965         [ #  # ]:          0 :         if (vsocket->async_copy) {
     966                 :          0 :                 vsocket->supported_features &= ~(1ULL << VHOST_F_LOG_ALL);
     967                 :          0 :                 vsocket->features &= ~(1ULL << VHOST_F_LOG_ALL);
     968                 :          0 :                 VHOST_CONFIG_LOG(path, INFO, "logging feature is disabled in async copy mode");
     969                 :            :         }
     970                 :            : 
     971                 :            :         /*
     972                 :            :          * We'll not be able to receive a buffer from guest in linear mode
     973                 :            :          * without external buffer if it will not fit in a single mbuf, which is
     974                 :            :          * likely if segmentation offloading enabled.
     975                 :            :          */
     976   [ #  #  #  # ]:          0 :         if (vsocket->linearbuf && !vsocket->extbuf) {
     977                 :            :                 uint64_t seg_offload_features =
     978                 :            :                                 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
     979                 :            :                                 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
     980                 :            :                                 (1ULL << VIRTIO_NET_F_HOST_UFO);
     981                 :            : 
     982                 :          0 :                 VHOST_CONFIG_LOG(path, INFO, "Linear buffers requested without external buffers,");
     983                 :          0 :                 VHOST_CONFIG_LOG(path, INFO, "disabling host segmentation offloading support");
     984                 :          0 :                 vsocket->supported_features &= ~seg_offload_features;
     985                 :          0 :                 vsocket->features &= ~seg_offload_features;
     986                 :            :         }
     987                 :            : 
     988         [ #  # ]:          0 :         if (!vsocket->iommu_support) {
     989                 :          0 :                 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
     990                 :          0 :                 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
     991                 :            :         }
     992                 :            : 
     993         [ #  # ]:          0 :         if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
     994                 :          0 :                 vsocket->protocol_features &=
     995                 :            :                         ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
     996                 :            :         } else {
     997                 :            : #ifndef RTE_LIBRTE_VHOST_POSTCOPY
     998                 :            :                 VHOST_CONFIG_LOG(path, ERR, "Postcopy requested but not compiled");
     999                 :            :                 ret = -1;
    1000                 :            :                 goto out_mutex;
    1001                 :            : #endif
    1002                 :            :         }
    1003                 :            : 
    1004         [ #  # ]:          0 :         if (!vsocket->is_vduse) {
    1005         [ #  # ]:          0 :                 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
    1006                 :          0 :                         vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
    1007   [ #  #  #  # ]:          0 :                         if (vsocket->reconnect && reconn_tid.opaque_id == 0) {
    1008         [ #  # ]:          0 :                                 if (vhost_user_reconnect_init() != 0)
    1009                 :          0 :                                         goto out_mutex;
    1010                 :            :                         }
    1011                 :            :                 } else {
    1012                 :          0 :                         vsocket->is_server = true;
    1013                 :            :                 }
    1014                 :          0 :                 ret = create_unix_socket(vsocket);
    1015         [ #  # ]:          0 :                 if (ret < 0)
    1016                 :          0 :                         goto out_mutex;
    1017                 :            :         }
    1018                 :            : 
    1019                 :          0 :         vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
    1020                 :            : 
    1021                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
    1022                 :          0 :         return ret;
    1023                 :            : 
    1024                 :          0 : out_mutex:
    1025         [ #  # ]:          0 :         if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
    1026                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to destroy connection mutex");
    1027                 :            :         }
    1028                 :          0 : out_free:
    1029                 :          0 :         vhost_user_socket_mem_free(vsocket);
    1030                 :          0 : out:
    1031                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
    1032                 :            : 
    1033                 :          0 :         return ret;
    1034                 :            : }
    1035                 :            : 
    1036                 :            : static bool
    1037                 :          0 : vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
    1038                 :            : {
    1039                 :            :         int found = false;
    1040                 :            :         struct vhost_user_reconnect *reconn, *next;
    1041                 :            : 
    1042                 :          0 :         pthread_mutex_lock(&reconn_list.mutex);
    1043                 :            : 
    1044                 :          0 :         for (reconn = TAILQ_FIRST(&reconn_list.head);
    1045         [ #  # ]:          0 :              reconn != NULL; reconn = next) {
    1046                 :          0 :                 next = TAILQ_NEXT(reconn, next);
    1047                 :            : 
    1048         [ #  # ]:          0 :                 if (reconn->vsocket == vsocket) {
    1049         [ #  # ]:          0 :                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
    1050                 :          0 :                         close(reconn->fd);
    1051                 :          0 :                         free(reconn);
    1052                 :            :                         found = true;
    1053                 :          0 :                         break;
    1054                 :            :                 }
    1055                 :            :         }
    1056                 :          0 :         pthread_mutex_unlock(&reconn_list.mutex);
    1057                 :          0 :         return found;
    1058                 :            : }
    1059                 :            : 
    1060                 :            : /**
    1061                 :            :  * Unregister the specified vhost socket
    1062                 :            :  */
    1063                 :            : int
    1064                 :          0 : rte_vhost_driver_unregister(const char *path)
    1065                 :            : {
    1066                 :            :         int i;
    1067                 :            :         int count;
    1068                 :            :         struct vhost_user_connection *conn, *next;
    1069                 :            : 
    1070         [ #  # ]:          0 :         if (path == NULL)
    1071                 :            :                 return -1;
    1072                 :            : 
    1073                 :          0 : again:
    1074                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
    1075                 :            : 
    1076         [ #  # ]:          0 :         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
    1077                 :          0 :                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
    1078         [ #  # ]:          0 :                 if (strcmp(vsocket->path, path))
    1079                 :            :                         continue;
    1080                 :            : 
    1081         [ #  # ]:          0 :                 if (vsocket->is_vduse) {
    1082                 :          0 :                         vduse_device_destroy(path);
    1083         [ #  # ]:          0 :                 } else if (vsocket->is_server) {
    1084                 :            :                         /*
    1085                 :            :                          * If r/wcb is executing, release vhost_user's
    1086                 :            :                          * mutex lock, and try again since the r/wcb
    1087                 :            :                          * may use the mutex lock.
    1088                 :            :                          */
    1089         [ #  # ]:          0 :                         if (fdset_try_del(&vhost_user.fdset, vsocket->socket_fd) == -1) {
    1090                 :          0 :                                 pthread_mutex_unlock(&vhost_user.mutex);
    1091                 :          0 :                                 goto again;
    1092                 :            :                         }
    1093         [ #  # ]:          0 :                 } else if (vsocket->reconnect) {
    1094                 :          0 :                         vhost_user_remove_reconnect(vsocket);
    1095                 :            :                 }
    1096                 :            : 
    1097                 :          0 :                 pthread_mutex_lock(&vsocket->conn_mutex);
    1098                 :          0 :                 for (conn = TAILQ_FIRST(&vsocket->conn_list);
    1099         [ #  # ]:          0 :                          conn != NULL;
    1100                 :            :                          conn = next) {
    1101                 :          0 :                         next = TAILQ_NEXT(conn, next);
    1102                 :            : 
    1103                 :            :                         /*
    1104                 :            :                          * If r/wcb is executing, release vsocket's
    1105                 :            :                          * conn_mutex and vhost_user's mutex locks, and
    1106                 :            :                          * try again since the r/wcb may use the
    1107                 :            :                          * conn_mutex and mutex locks.
    1108                 :            :                          */
    1109         [ #  # ]:          0 :                         if (fdset_try_del(&vhost_user.fdset,
    1110                 :            :                                           conn->connfd) == -1) {
    1111                 :          0 :                                 pthread_mutex_unlock(&vsocket->conn_mutex);
    1112                 :          0 :                                 pthread_mutex_unlock(&vhost_user.mutex);
    1113                 :          0 :                                 goto again;
    1114                 :            :                         }
    1115                 :            : 
    1116                 :          0 :                         VHOST_CONFIG_LOG(path, INFO, "free connfd %d", conn->connfd);
    1117                 :          0 :                         close(conn->connfd);
    1118                 :          0 :                         vhost_destroy_device(conn->vid);
    1119         [ #  # ]:          0 :                         TAILQ_REMOVE(&vsocket->conn_list, conn, next);
    1120                 :          0 :                         free(conn);
    1121                 :            :                 }
    1122                 :          0 :                 pthread_mutex_unlock(&vsocket->conn_mutex);
    1123                 :            : 
    1124         [ #  # ]:          0 :                 if (vsocket->is_server) {
    1125                 :          0 :                         close(vsocket->socket_fd);
    1126                 :          0 :                         unlink(path);
    1127                 :            :                 }
    1128                 :            : 
    1129                 :          0 :                 pthread_mutex_destroy(&vsocket->conn_mutex);
    1130                 :          0 :                 vhost_user_socket_mem_free(vsocket);
    1131                 :            : 
    1132                 :          0 :                 count = --vhost_user.vsocket_cnt;
    1133                 :          0 :                 vhost_user.vsockets[i] = vhost_user.vsockets[count];
    1134                 :          0 :                 vhost_user.vsockets[count] = NULL;
    1135                 :          0 :                 pthread_mutex_unlock(&vhost_user.mutex);
    1136                 :          0 :                 return 0;
    1137                 :            :         }
    1138                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
    1139                 :            : 
    1140                 :          0 :         return -1;
    1141                 :            : }
    1142                 :            : 
    1143                 :            : /*
    1144                 :            :  * Register ops so that we can add/remove device to data core.
    1145                 :            :  */
    1146                 :            : int
    1147                 :          0 : rte_vhost_driver_callback_register(const char *path,
    1148                 :            :         struct rte_vhost_device_ops const * const ops)
    1149                 :            : {
    1150                 :            :         struct vhost_user_socket *vsocket;
    1151                 :            : 
    1152                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
    1153                 :          0 :         vsocket = find_vhost_user_socket(path);
    1154         [ #  # ]:          0 :         if (vsocket)
    1155                 :          0 :                 vsocket->notify_ops = ops;
    1156                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
    1157                 :            : 
    1158         [ #  # ]:          0 :         return vsocket ? 0 : -1;
    1159                 :            : }
    1160                 :            : 
    1161                 :            : struct rte_vhost_device_ops const *
    1162                 :          0 : vhost_driver_callback_get(const char *path)
    1163                 :            : {
    1164                 :            :         struct vhost_user_socket *vsocket;
    1165                 :            : 
    1166                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
    1167                 :          0 :         vsocket = find_vhost_user_socket(path);
    1168                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
    1169                 :            : 
    1170         [ #  # ]:          0 :         return vsocket ? vsocket->notify_ops : NULL;
    1171                 :            : }
    1172                 :            : 
    1173                 :            : int
    1174                 :          0 : rte_vhost_driver_start(const char *path)
    1175                 :            : {
    1176                 :            :         struct vhost_user_socket *vsocket;
    1177                 :            :         static rte_thread_t fdset_tid;
    1178                 :            : 
    1179                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
    1180                 :          0 :         vsocket = find_vhost_user_socket(path);
    1181                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
    1182                 :            : 
    1183         [ #  # ]:          0 :         if (!vsocket)
    1184                 :            :                 return -1;
    1185                 :            : 
    1186         [ #  # ]:          0 :         if (vsocket->is_vduse)
    1187                 :          0 :                 return vduse_device_create(path, vsocket->net_compliant_ol_flags);
    1188                 :            : 
    1189         [ #  # ]:          0 :         if (fdset_tid.opaque_id == 0) {
    1190                 :            :                 /**
    1191                 :            :                  * create a pipe which will be waited by poll and notified to
    1192                 :            :                  * rebuild the wait list of poll.
    1193                 :            :                  */
    1194         [ #  # ]:          0 :                 if (fdset_pipe_init(&vhost_user.fdset) < 0) {
    1195                 :          0 :                         VHOST_CONFIG_LOG(path, ERR, "failed to create pipe for vhost fdset");
    1196                 :          0 :                         return -1;
    1197                 :            :                 }
    1198                 :            : 
    1199                 :          0 :                 int ret = rte_thread_create_internal_control(&fdset_tid,
    1200                 :            :                                 "vhost-evt", fdset_event_dispatch, &vhost_user.fdset);
    1201         [ #  # ]:          0 :                 if (ret != 0) {
    1202                 :          0 :                         VHOST_CONFIG_LOG(path, ERR, "failed to create fdset handling thread");
    1203                 :          0 :                         fdset_pipe_uninit(&vhost_user.fdset);
    1204                 :          0 :                         return -1;
    1205                 :            :                 }
    1206                 :            :         }
    1207                 :            : 
    1208         [ #  # ]:          0 :         if (vsocket->is_server)
    1209                 :          0 :                 return vhost_user_start_server(vsocket);
    1210                 :            :         else
    1211                 :          0 :                 return vhost_user_start_client(vsocket);
    1212                 :            : }

Generated by: LCOV version 1.14