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

Generated by: LCOV version 1.14