LCOV - code coverage report
Current view: top level - lib/vhost - socket.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 515 0.0 %
Date: 2025-03-01 20:23:48 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 *close);
      88                 :            : static void vhost_user_read_cb(int fd, void *dat, int *close);
      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 *close __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 *close)
     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 = 1;
     317                 :            : 
     318         [ #  # ]:          0 :                 if (dev)
     319                 :          0 :                         vhost_destroy_device_notify(dev);
     320                 :            : 
     321         [ #  # ]:          0 :                 if (vsocket->notify_ops->destroy_connection)
     322                 :          0 :                         vsocket->notify_ops->destroy_connection(conn->vid);
     323                 :            : 
     324                 :          0 :                 vhost_destroy_device(conn->vid);
     325                 :            : 
     326         [ #  # ]:          0 :                 if (vsocket->reconnect) {
     327                 :          0 :                         create_unix_socket(vsocket);
     328                 :          0 :                         vhost_user_start_client(vsocket);
     329                 :            :                 }
     330                 :            : 
     331                 :          0 :                 pthread_mutex_lock(&vsocket->conn_mutex);
     332         [ #  # ]:          0 :                 TAILQ_REMOVE(&vsocket->conn_list, conn, next);
     333                 :          0 :                 pthread_mutex_unlock(&vsocket->conn_mutex);
     334                 :            : 
     335                 :          0 :                 free(conn);
     336                 :            :         }
     337                 :          0 : }
     338                 :            : 
     339                 :            : static int
     340                 :          0 : create_unix_socket(struct vhost_user_socket *vsocket)
     341                 :            : {
     342                 :            :         int fd;
     343                 :          0 :         struct sockaddr_un *un = &vsocket->un;
     344                 :            : 
     345                 :          0 :         fd = socket(AF_UNIX, SOCK_STREAM, 0);
     346         [ #  # ]:          0 :         if (fd < 0)
     347                 :            :                 return -1;
     348         [ #  # ]:          0 :         VHOST_CONFIG_LOG(vsocket->path, INFO, "vhost-user %s: socket created, fd: %d",
     349                 :            :                 vsocket->is_server ? "server" : "client", fd);
     350                 :            : 
     351   [ #  #  #  # ]:          0 :         if (!vsocket->is_server && fcntl(fd, F_SETFL, O_NONBLOCK)) {
     352                 :          0 :                 VHOST_CONFIG_LOG(vsocket->path, ERR,
     353                 :            :                         "vhost-user: can't set nonblocking mode for socket, fd: %d (%s)",
     354                 :            :                         fd, strerror(errno));
     355                 :          0 :                 close(fd);
     356                 :          0 :                 return -1;
     357                 :            :         }
     358                 :            : 
     359                 :            :         memset(un, 0, sizeof(*un));
     360                 :          0 :         un->sun_family = AF_UNIX;
     361                 :          0 :         strlcpy(un->sun_path, vsocket->path, sizeof(un->sun_path));
     362                 :            : 
     363                 :          0 :         vsocket->socket_fd = fd;
     364                 :          0 :         return 0;
     365                 :            : }
     366                 :            : 
     367                 :            : static int
     368                 :          0 : vhost_user_start_server(struct vhost_user_socket *vsocket)
     369                 :            : {
     370                 :            :         int ret;
     371                 :          0 :         int fd = vsocket->socket_fd;
     372                 :          0 :         const char *path = vsocket->path;
     373                 :            : 
     374                 :            :         /*
     375                 :            :          * bind () may fail if the socket file with the same name already
     376                 :            :          * exists. But the library obviously should not delete the file
     377                 :            :          * provided by the user, since we can not be sure that it is not
     378                 :            :          * being used by other applications. Moreover, many applications form
     379                 :            :          * socket names based on user input, which is prone to errors.
     380                 :            :          *
     381                 :            :          * The user must ensure that the socket does not exist before
     382                 :            :          * registering the vhost driver in server mode.
     383                 :            :          */
     384                 :          0 :         ret = bind(fd, (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
     385         [ #  # ]:          0 :         if (ret < 0) {
     386                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to bind: %s; remove it and try again",
     387                 :            :                         strerror(errno));
     388                 :          0 :                 goto err;
     389                 :            :         }
     390                 :          0 :         VHOST_CONFIG_LOG(path, INFO, "binding succeeded");
     391                 :            : 
     392                 :          0 :         ret = listen(fd, MAX_VIRTIO_BACKLOG);
     393         [ #  # ]:          0 :         if (ret < 0)
     394                 :          0 :                 goto err;
     395                 :            : 
     396                 :          0 :         ret = fdset_add(vhost_user.fdset, fd, vhost_user_server_new_connection,
     397                 :            :                   NULL, vsocket);
     398         [ #  # ]:          0 :         if (ret < 0) {
     399                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to add listen fd %d to vhost server fdset",
     400                 :            :                         fd);
     401                 :          0 :                 goto err;
     402                 :            :         }
     403                 :            : 
     404                 :            :         return 0;
     405                 :            : 
     406                 :          0 : err:
     407                 :          0 :         close(fd);
     408                 :          0 :         return -1;
     409                 :            : }
     410                 :            : 
     411                 :            : struct vhost_user_reconnect {
     412                 :            :         struct sockaddr_un un;
     413                 :            :         int fd;
     414                 :            :         struct vhost_user_socket *vsocket;
     415                 :            : 
     416                 :            :         TAILQ_ENTRY(vhost_user_reconnect) next;
     417                 :            : };
     418                 :            : 
     419                 :            : TAILQ_HEAD(vhost_user_reconnect_tailq_list, vhost_user_reconnect);
     420                 :            : struct vhost_user_reconnect_list {
     421                 :            :         struct vhost_user_reconnect_tailq_list head;
     422                 :            :         pthread_mutex_t mutex;
     423                 :            : };
     424                 :            : 
     425                 :            : static struct vhost_user_reconnect_list reconn_list;
     426                 :            : static rte_thread_t reconn_tid;
     427                 :            : 
     428                 :            : static int
     429                 :          0 : vhost_user_connect_nonblock(char *path, int fd, struct sockaddr *un, size_t sz)
     430                 :            : {
     431                 :            :         int ret, flags;
     432                 :            : 
     433                 :          0 :         ret = connect(fd, un, sz);
     434   [ #  #  #  # ]:          0 :         if (ret < 0 && errno != EISCONN)
     435                 :            :                 return -1;
     436                 :            : 
     437                 :          0 :         flags = fcntl(fd, F_GETFL, 0);
     438         [ #  # ]:          0 :         if (flags < 0) {
     439                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "can't get flags for connfd %d (%s)",
     440                 :            :                         fd, strerror(errno));
     441                 :          0 :                 return -2;
     442                 :            :         }
     443   [ #  #  #  # ]:          0 :         if ((flags & O_NONBLOCK) && fcntl(fd, F_SETFL, flags & ~O_NONBLOCK)) {
     444                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "can't disable nonblocking on fd %d", fd);
     445                 :          0 :                 return -2;
     446                 :            :         }
     447                 :            :         return 0;
     448                 :            : }
     449                 :            : 
     450                 :            : static uint32_t
     451                 :          0 : vhost_user_client_reconnect(void *arg __rte_unused)
     452                 :            : {
     453                 :            :         int ret;
     454                 :            :         struct vhost_user_reconnect *reconn, *next;
     455                 :            : 
     456                 :            :         while (1) {
     457                 :          0 :                 pthread_mutex_lock(&reconn_list.mutex);
     458                 :            : 
     459                 :            :                 /*
     460                 :            :                  * An equal implementation of TAILQ_FOREACH_SAFE,
     461                 :            :                  * which does not exist on all platforms.
     462                 :            :                  */
     463                 :          0 :                 for (reconn = TAILQ_FIRST(&reconn_list.head);
     464         [ #  # ]:          0 :                      reconn != NULL; reconn = next) {
     465                 :          0 :                         next = TAILQ_NEXT(reconn, next);
     466                 :            : 
     467                 :          0 :                         ret = vhost_user_connect_nonblock(reconn->vsocket->path, reconn->fd,
     468                 :          0 :                                                 (struct sockaddr *)&reconn->un,
     469                 :            :                                                 sizeof(reconn->un));
     470         [ #  # ]:          0 :                         if (ret == -2) {
     471                 :          0 :                                 close(reconn->fd);
     472                 :          0 :                                 VHOST_CONFIG_LOG(reconn->vsocket->path, ERR,
     473                 :            :                                         "reconnection for fd %d failed",
     474                 :            :                                         reconn->fd);
     475                 :          0 :                                 goto remove_fd;
     476                 :            :                         }
     477         [ #  # ]:          0 :                         if (ret == -1)
     478                 :          0 :                                 continue;
     479                 :            : 
     480                 :          0 :                         VHOST_CONFIG_LOG(reconn->vsocket->path, INFO, "connected");
     481                 :          0 :                         vhost_user_add_connection(reconn->fd, reconn->vsocket);
     482                 :          0 : remove_fd:
     483         [ #  # ]:          0 :                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
     484                 :          0 :                         free(reconn);
     485                 :            :                 }
     486                 :            : 
     487                 :          0 :                 pthread_mutex_unlock(&reconn_list.mutex);
     488                 :          0 :                 sleep(1);
     489                 :            :         }
     490                 :            : 
     491                 :            :         return 0;
     492                 :            : }
     493                 :            : 
     494                 :            : static int
     495                 :          0 : vhost_user_reconnect_init(void)
     496                 :            : {
     497                 :            :         int ret;
     498                 :            : 
     499                 :          0 :         pthread_mutex_init(&reconn_list.mutex, NULL);
     500                 :          0 :         TAILQ_INIT(&reconn_list.head);
     501                 :            : 
     502                 :          0 :         ret = rte_thread_create_internal_control(&reconn_tid, "vhost-reco",
     503                 :            :                         vhost_user_client_reconnect, NULL);
     504         [ #  # ]:          0 :         if (ret != 0) {
     505                 :          0 :                 VHOST_CONFIG_LOG("thread", ERR, "failed to create reconnect thread");
     506         [ #  # ]:          0 :                 if (pthread_mutex_destroy(&reconn_list.mutex))
     507                 :          0 :                         VHOST_CONFIG_LOG("thread", ERR,
     508                 :            :                                 "%s: failed to destroy reconnect mutex",
     509                 :            :                                 __func__);
     510                 :            :         }
     511                 :            : 
     512                 :          0 :         return ret;
     513                 :            : }
     514                 :            : 
     515                 :            : static int
     516                 :          0 : vhost_user_start_client(struct vhost_user_socket *vsocket)
     517                 :            : {
     518                 :            :         int ret;
     519                 :          0 :         int fd = vsocket->socket_fd;
     520                 :          0 :         const char *path = vsocket->path;
     521                 :            :         struct vhost_user_reconnect *reconn;
     522                 :            : 
     523   [ #  #  #  # ]:          0 :         if (!vsocket->async_connect || !vsocket->reconnect) {
     524                 :          0 :                 ret = vhost_user_connect_nonblock(vsocket->path, fd,
     525                 :          0 :                         (struct sockaddr *)&vsocket->un, sizeof(vsocket->un));
     526         [ #  # ]:          0 :                 if (ret == 0) {
     527                 :          0 :                         vhost_user_add_connection(fd, vsocket);
     528                 :          0 :                         return 0;
     529                 :            :                 }
     530                 :            : 
     531                 :          0 :                 VHOST_CONFIG_LOG(path, WARNING, "failed to connect: %s", strerror(errno));
     532                 :            : 
     533   [ #  #  #  # ]:          0 :                 if (ret == -2 || !vsocket->reconnect) {
     534                 :          0 :                         close(fd);
     535                 :          0 :                         return -1;
     536                 :            :                 }
     537                 :            : 
     538                 :          0 :                 VHOST_CONFIG_LOG(path, INFO, "reconnecting...");
     539                 :            :         }
     540                 :          0 :         reconn = malloc(sizeof(*reconn));
     541         [ #  # ]:          0 :         if (reconn == NULL) {
     542                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to allocate memory for reconnect");
     543                 :          0 :                 close(fd);
     544                 :          0 :                 return -1;
     545                 :            :         }
     546                 :          0 :         reconn->un = vsocket->un;
     547                 :          0 :         reconn->fd = fd;
     548                 :          0 :         reconn->vsocket = vsocket;
     549                 :          0 :         pthread_mutex_lock(&reconn_list.mutex);
     550                 :          0 :         TAILQ_INSERT_TAIL(&reconn_list.head, reconn, next);
     551                 :          0 :         pthread_mutex_unlock(&reconn_list.mutex);
     552                 :            : 
     553                 :          0 :         return 0;
     554                 :            : }
     555                 :            : 
     556                 :            : static struct vhost_user_socket *
     557                 :          0 : find_vhost_user_socket(const char *path)
     558                 :            : {
     559                 :            :         int i;
     560                 :            : 
     561         [ #  # ]:          0 :         if (path == NULL)
     562                 :            :                 return NULL;
     563                 :            : 
     564         [ #  # ]:          0 :         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
     565                 :          0 :                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
     566                 :            : 
     567         [ #  # ]:          0 :                 if (!strcmp(vsocket->path, path))
     568                 :          0 :                         return vsocket;
     569                 :            :         }
     570                 :            : 
     571                 :            :         return NULL;
     572                 :            : }
     573                 :            : 
     574                 :            : int
     575                 :          0 : rte_vhost_driver_attach_vdpa_device(const char *path,
     576                 :            :                 struct rte_vdpa_device *dev)
     577                 :            : {
     578                 :            :         struct vhost_user_socket *vsocket;
     579                 :            : 
     580         [ #  # ]:          0 :         if (dev == NULL || path == NULL)
     581                 :            :                 return -1;
     582                 :            : 
     583                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     584                 :          0 :         vsocket = find_vhost_user_socket(path);
     585         [ #  # ]:          0 :         if (vsocket)
     586                 :          0 :                 vsocket->vdpa_dev = dev;
     587                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     588                 :            : 
     589         [ #  # ]:          0 :         return vsocket ? 0 : -1;
     590                 :            : }
     591                 :            : 
     592                 :            : int
     593                 :          0 : rte_vhost_driver_detach_vdpa_device(const char *path)
     594                 :            : {
     595                 :            :         struct vhost_user_socket *vsocket;
     596                 :            : 
     597                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     598                 :          0 :         vsocket = find_vhost_user_socket(path);
     599         [ #  # ]:          0 :         if (vsocket)
     600                 :          0 :                 vsocket->vdpa_dev = NULL;
     601                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     602                 :            : 
     603         [ #  # ]:          0 :         return vsocket ? 0 : -1;
     604                 :            : }
     605                 :            : 
     606                 :            : struct rte_vdpa_device *
     607                 :          0 : rte_vhost_driver_get_vdpa_device(const char *path)
     608                 :            : {
     609                 :            :         struct vhost_user_socket *vsocket;
     610                 :            :         struct rte_vdpa_device *dev = NULL;
     611                 :            : 
     612                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     613                 :          0 :         vsocket = find_vhost_user_socket(path);
     614         [ #  # ]:          0 :         if (vsocket)
     615                 :          0 :                 dev = vsocket->vdpa_dev;
     616                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     617                 :            : 
     618                 :          0 :         return dev;
     619                 :            : }
     620                 :            : 
     621                 :            : int
     622                 :          0 : rte_vhost_driver_get_vdpa_dev_type(const char *path, uint32_t *type)
     623                 :            : {
     624                 :            :         struct vhost_user_socket *vsocket;
     625                 :            :         struct rte_vdpa_device *vdpa_dev;
     626                 :            :         int ret = 0;
     627                 :            : 
     628                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     629                 :          0 :         vsocket = find_vhost_user_socket(path);
     630         [ #  # ]:          0 :         if (!vsocket) {
     631                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
     632                 :            :                 ret = -1;
     633                 :          0 :                 goto unlock_exit;
     634                 :            :         }
     635                 :            : 
     636                 :          0 :         vdpa_dev = vsocket->vdpa_dev;
     637         [ #  # ]:          0 :         if (!vdpa_dev) {
     638                 :            :                 ret = -1;
     639                 :          0 :                 goto unlock_exit;
     640                 :            :         }
     641                 :            : 
     642                 :          0 :         *type = vdpa_dev->type;
     643                 :            : 
     644                 :          0 : unlock_exit:
     645                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     646                 :          0 :         return ret;
     647                 :            : }
     648                 :            : 
     649                 :            : int
     650                 :          0 : rte_vhost_driver_disable_features(const char *path, uint64_t features)
     651                 :            : {
     652                 :            :         struct vhost_user_socket *vsocket;
     653                 :            : 
     654                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     655                 :          0 :         vsocket = find_vhost_user_socket(path);
     656                 :            : 
     657                 :            :         /* Note that use_builtin_virtio_net is not affected by this function
     658                 :            :          * since callers may want to selectively disable features of the
     659                 :            :          * built-in vhost net device backend.
     660                 :            :          */
     661                 :            : 
     662         [ #  # ]:          0 :         if (vsocket)
     663                 :          0 :                 vsocket->features &= ~features;
     664                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     665                 :            : 
     666         [ #  # ]:          0 :         return vsocket ? 0 : -1;
     667                 :            : }
     668                 :            : 
     669                 :            : int
     670                 :          0 : rte_vhost_driver_enable_features(const char *path, uint64_t features)
     671                 :            : {
     672                 :            :         struct vhost_user_socket *vsocket;
     673                 :            : 
     674                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     675                 :          0 :         vsocket = find_vhost_user_socket(path);
     676         [ #  # ]:          0 :         if (vsocket) {
     677         [ #  # ]:          0 :                 if ((vsocket->supported_features & features) != features) {
     678                 :            :                         /*
     679                 :            :                          * trying to enable features the driver doesn't
     680                 :            :                          * support.
     681                 :            :                          */
     682                 :          0 :                         pthread_mutex_unlock(&vhost_user.mutex);
     683                 :          0 :                         return -1;
     684                 :            :                 }
     685                 :          0 :                 vsocket->features |= features;
     686                 :            :         }
     687                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     688                 :            : 
     689         [ #  # ]:          0 :         return vsocket ? 0 : -1;
     690                 :            : }
     691                 :            : 
     692                 :            : int
     693                 :          0 : rte_vhost_driver_set_features(const char *path, uint64_t features)
     694                 :            : {
     695                 :            :         struct vhost_user_socket *vsocket;
     696                 :            : 
     697                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     698                 :          0 :         vsocket = find_vhost_user_socket(path);
     699         [ #  # ]:          0 :         if (vsocket) {
     700                 :          0 :                 vsocket->supported_features = features;
     701                 :          0 :                 vsocket->features = features;
     702                 :            : 
     703                 :            :                 /* Anyone setting feature bits is implementing their own vhost
     704                 :            :                  * device backend.
     705                 :            :                  */
     706                 :          0 :                 vsocket->use_builtin_virtio_net = false;
     707                 :            :         }
     708                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     709                 :            : 
     710         [ #  # ]:          0 :         return vsocket ? 0 : -1;
     711                 :            : }
     712                 :            : 
     713                 :            : int
     714                 :          0 : rte_vhost_driver_get_features(const char *path, uint64_t *features)
     715                 :            : {
     716                 :            :         struct vhost_user_socket *vsocket;
     717                 :            :         uint64_t vdpa_features;
     718                 :            :         struct rte_vdpa_device *vdpa_dev;
     719                 :            :         int ret = 0;
     720                 :            : 
     721                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     722                 :          0 :         vsocket = find_vhost_user_socket(path);
     723         [ #  # ]:          0 :         if (!vsocket) {
     724                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
     725                 :            :                 ret = -1;
     726                 :          0 :                 goto unlock_exit;
     727                 :            :         }
     728                 :            : 
     729                 :          0 :         vdpa_dev = vsocket->vdpa_dev;
     730         [ #  # ]:          0 :         if (!vdpa_dev) {
     731                 :          0 :                 *features = vsocket->features;
     732                 :          0 :                 goto unlock_exit;
     733                 :            :         }
     734                 :            : 
     735         [ #  # ]:          0 :         if (vdpa_dev->ops->get_features(vdpa_dev, &vdpa_features) < 0) {
     736                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to get vdpa features for socket file.");
     737                 :            :                 ret = -1;
     738                 :          0 :                 goto unlock_exit;
     739                 :            :         }
     740                 :            : 
     741                 :          0 :         *features = vsocket->features & vdpa_features;
     742                 :            : 
     743                 :          0 : unlock_exit:
     744                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     745                 :          0 :         return ret;
     746                 :            : }
     747                 :            : 
     748                 :            : int
     749                 :          0 : rte_vhost_driver_set_protocol_features(const char *path,
     750                 :            :                 uint64_t protocol_features)
     751                 :            : {
     752                 :            :         struct vhost_user_socket *vsocket;
     753                 :            : 
     754                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     755                 :          0 :         vsocket = find_vhost_user_socket(path);
     756         [ #  # ]:          0 :         if (vsocket)
     757                 :          0 :                 vsocket->protocol_features = protocol_features;
     758                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     759         [ #  # ]:          0 :         return vsocket ? 0 : -1;
     760                 :            : }
     761                 :            : 
     762                 :            : int
     763                 :          0 : rte_vhost_driver_get_protocol_features(const char *path,
     764                 :            :                 uint64_t *protocol_features)
     765                 :            : {
     766                 :            :         struct vhost_user_socket *vsocket;
     767                 :            :         uint64_t vdpa_protocol_features;
     768                 :            :         struct rte_vdpa_device *vdpa_dev;
     769                 :            :         int ret = 0;
     770                 :            : 
     771                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     772                 :          0 :         vsocket = find_vhost_user_socket(path);
     773         [ #  # ]:          0 :         if (!vsocket) {
     774                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
     775                 :            :                 ret = -1;
     776                 :          0 :                 goto unlock_exit;
     777                 :            :         }
     778                 :            : 
     779                 :          0 :         vdpa_dev = vsocket->vdpa_dev;
     780         [ #  # ]:          0 :         if (!vdpa_dev) {
     781                 :          0 :                 *protocol_features = vsocket->protocol_features;
     782                 :          0 :                 goto unlock_exit;
     783                 :            :         }
     784                 :            : 
     785         [ #  # ]:          0 :         if (vdpa_dev->ops->get_protocol_features(vdpa_dev,
     786                 :            :                                 &vdpa_protocol_features) < 0) {
     787                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to get vdpa protocol features.");
     788                 :            :                 ret = -1;
     789                 :          0 :                 goto unlock_exit;
     790                 :            :         }
     791                 :            : 
     792                 :          0 :         *protocol_features = vsocket->protocol_features
     793                 :          0 :                 & vdpa_protocol_features;
     794                 :            : 
     795                 :          0 : unlock_exit:
     796                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     797                 :          0 :         return ret;
     798                 :            : }
     799                 :            : 
     800                 :            : int
     801                 :          0 : rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
     802                 :            : {
     803                 :            :         struct vhost_user_socket *vsocket;
     804                 :            :         uint32_t vdpa_queue_num;
     805                 :            :         struct rte_vdpa_device *vdpa_dev;
     806                 :            :         int ret = 0;
     807                 :            : 
     808                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     809                 :          0 :         vsocket = find_vhost_user_socket(path);
     810         [ #  # ]:          0 :         if (!vsocket) {
     811                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
     812                 :            :                 ret = -1;
     813                 :          0 :                 goto unlock_exit;
     814                 :            :         }
     815                 :            : 
     816                 :          0 :         vdpa_dev = vsocket->vdpa_dev;
     817         [ #  # ]:          0 :         if (!vdpa_dev) {
     818                 :          0 :                 *queue_num = vsocket->max_queue_pairs;
     819                 :          0 :                 goto unlock_exit;
     820                 :            :         }
     821                 :            : 
     822         [ #  # ]:          0 :         if (vdpa_dev->ops->get_queue_num(vdpa_dev, &vdpa_queue_num) < 0) {
     823                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to get vdpa queue number.");
     824                 :            :                 ret = -1;
     825                 :          0 :                 goto unlock_exit;
     826                 :            :         }
     827                 :            : 
     828                 :          0 :         *queue_num = RTE_MIN(vsocket->max_queue_pairs, vdpa_queue_num);
     829                 :            : 
     830                 :          0 : unlock_exit:
     831                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     832                 :          0 :         return ret;
     833                 :            : }
     834                 :            : 
     835                 :            : int
     836                 :          0 : rte_vhost_driver_set_max_queue_num(const char *path, uint32_t max_queue_pairs)
     837                 :            : {
     838                 :            :         struct vhost_user_socket *vsocket;
     839                 :            :         int ret = 0;
     840                 :            : 
     841                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     842                 :          0 :         vsocket = find_vhost_user_socket(path);
     843         [ #  # ]:          0 :         if (!vsocket) {
     844                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
     845                 :            :                 ret = -1;
     846                 :          0 :                 goto unlock_exit;
     847                 :            :         }
     848                 :            : 
     849                 :            :         /*
     850                 :            :          * This is only useful for VDUSE for which number of virtqueues is set
     851                 :            :          * by the backend. For Vhost-user, the number of virtqueues is defined
     852                 :            :          * by the frontend.
     853                 :            :          */
     854         [ #  # ]:          0 :         if (!vsocket->is_vduse) {
     855                 :          0 :                 VHOST_CONFIG_LOG(path, DEBUG,
     856                 :            :                                 "Keeping %u max queue pairs for Vhost-user backend",
     857                 :            :                                 VHOST_MAX_QUEUE_PAIRS);
     858                 :          0 :                 goto unlock_exit;
     859                 :            :         }
     860                 :            : 
     861                 :          0 :         VHOST_CONFIG_LOG(path, INFO, "Setting max queue pairs to %u", max_queue_pairs);
     862                 :            : 
     863         [ #  # ]:          0 :         if (max_queue_pairs > VHOST_MAX_QUEUE_PAIRS) {
     864                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "Library only supports up to %u queue pairs",
     865                 :            :                                 VHOST_MAX_QUEUE_PAIRS);
     866                 :            :                 ret = -1;
     867                 :          0 :                 goto unlock_exit;
     868                 :            :         }
     869                 :            : 
     870                 :          0 :         vsocket->max_queue_pairs = max_queue_pairs;
     871                 :            : 
     872                 :          0 : unlock_exit:
     873                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     874                 :          0 :         return ret;
     875                 :            : }
     876                 :            : 
     877                 :            : static void
     878                 :          0 : vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
     879                 :            : {
     880         [ #  # ]:          0 :         if (vsocket == NULL)
     881                 :            :                 return;
     882                 :            : 
     883                 :          0 :         free(vsocket->path);
     884                 :          0 :         free(vsocket);
     885                 :            : }
     886                 :            : 
     887                 :            : /*
     888                 :            :  * Register a new vhost-user socket; here we could act as server
     889                 :            :  * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
     890                 :            :  * is set.
     891                 :            :  */
     892                 :            : int
     893                 :          0 : rte_vhost_driver_register(const char *path, uint64_t flags)
     894                 :            : {
     895                 :            :         struct vhost_user_socket *vsocket;
     896                 :            : 
     897         [ #  # ]:          0 :         if (!path)
     898                 :            :                 return -1;
     899                 :            : 
     900                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     901                 :            : 
     902         [ #  # ]:          0 :         if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
     903                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "the number of vhost sockets reaches maximum");
     904                 :          0 :                 goto out;
     905                 :            :         }
     906                 :            : 
     907                 :          0 :         vsocket = malloc(sizeof(struct vhost_user_socket));
     908         [ #  # ]:          0 :         if (!vsocket)
     909                 :          0 :                 goto out;
     910                 :            :         memset(vsocket, 0, sizeof(struct vhost_user_socket));
     911                 :          0 :         vsocket->path = strdup(path);
     912         [ #  # ]:          0 :         if (vsocket->path == NULL) {
     913                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to copy socket path string");
     914                 :          0 :                 vhost_user_socket_mem_free(vsocket);
     915                 :          0 :                 goto out;
     916                 :            :         }
     917                 :          0 :         TAILQ_INIT(&vsocket->conn_list);
     918                 :          0 :         pthread_mutex_init(&vsocket->conn_mutex, NULL);
     919                 :            : 
     920         [ #  # ]:          0 :         if (!strncmp("/dev/vduse/", path, strlen("/dev/vduse/")))
     921                 :          0 :                 vsocket->is_vduse = true;
     922                 :            : 
     923                 :          0 :         vsocket->vdpa_dev = NULL;
     924                 :          0 :         vsocket->max_queue_pairs = VHOST_MAX_QUEUE_PAIRS;
     925                 :          0 :         vsocket->extbuf = flags & RTE_VHOST_USER_EXTBUF_SUPPORT;
     926                 :          0 :         vsocket->linearbuf = flags & RTE_VHOST_USER_LINEARBUF_SUPPORT;
     927                 :          0 :         vsocket->async_copy = flags & RTE_VHOST_USER_ASYNC_COPY;
     928                 :          0 :         vsocket->net_compliant_ol_flags = flags & RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS;
     929                 :          0 :         vsocket->stats_enabled = flags & RTE_VHOST_USER_NET_STATS_ENABLE;
     930                 :          0 :         vsocket->async_connect = flags & RTE_VHOST_USER_ASYNC_CONNECT;
     931         [ #  # ]:          0 :         if (vsocket->is_vduse)
     932                 :          0 :                 vsocket->iommu_support = true;
     933                 :            :         else
     934                 :          0 :                 vsocket->iommu_support = flags & RTE_VHOST_USER_IOMMU_SUPPORT;
     935                 :            : 
     936   [ #  #  #  # ]:          0 :         if (vsocket->async_copy && (vsocket->iommu_support ||
     937         [ #  # ]:          0 :                                 (flags & RTE_VHOST_USER_POSTCOPY_SUPPORT))) {
     938                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "async copy with IOMMU or post-copy not supported");
     939                 :          0 :                 goto out_mutex;
     940                 :            :         }
     941                 :            : 
     942                 :            :         /*
     943                 :            :          * Set the supported features correctly for the builtin vhost-user
     944                 :            :          * net driver.
     945                 :            :          *
     946                 :            :          * Applications know nothing about features the builtin virtio net
     947                 :            :          * driver (virtio_net.c) supports, thus it's not possible for them
     948                 :            :          * to invoke rte_vhost_driver_set_features(). To workaround it, here
     949                 :            :          * we set it unconditionally. If the application want to implement
     950                 :            :          * another vhost-user driver (say SCSI), it should call the
     951                 :            :          * rte_vhost_driver_set_features(), which will overwrite following
     952                 :            :          * two values.
     953                 :            :          */
     954                 :          0 :         vsocket->use_builtin_virtio_net = true;
     955         [ #  # ]:          0 :         if (vsocket->is_vduse) {
     956                 :          0 :                 vsocket->supported_features = VDUSE_NET_SUPPORTED_FEATURES;
     957                 :          0 :                 vsocket->features           = VDUSE_NET_SUPPORTED_FEATURES;
     958                 :            :         } else {
     959                 :          0 :                 vsocket->supported_features = VHOST_USER_NET_SUPPORTED_FEATURES;
     960                 :          0 :                 vsocket->features           = VHOST_USER_NET_SUPPORTED_FEATURES;
     961                 :          0 :                 vsocket->protocol_features  = VHOST_USER_PROTOCOL_FEATURES;
     962                 :            :         }
     963                 :            : 
     964         [ #  # ]:          0 :         if (vsocket->async_copy) {
     965                 :          0 :                 vsocket->supported_features &= ~(1ULL << VHOST_F_LOG_ALL);
     966                 :          0 :                 vsocket->features &= ~(1ULL << VHOST_F_LOG_ALL);
     967                 :          0 :                 VHOST_CONFIG_LOG(path, INFO, "logging feature is disabled in async copy mode");
     968                 :            :         }
     969                 :            : 
     970                 :            :         /*
     971                 :            :          * We'll not be able to receive a buffer from guest in linear mode
     972                 :            :          * without external buffer if it will not fit in a single mbuf, which is
     973                 :            :          * likely if segmentation offloading enabled.
     974                 :            :          */
     975   [ #  #  #  # ]:          0 :         if (vsocket->linearbuf && !vsocket->extbuf) {
     976                 :            :                 uint64_t seg_offload_features =
     977                 :            :                                 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
     978                 :            :                                 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
     979                 :            :                                 (1ULL << VIRTIO_NET_F_HOST_UFO);
     980                 :            : 
     981                 :          0 :                 VHOST_CONFIG_LOG(path, INFO, "Linear buffers requested without external buffers,");
     982                 :          0 :                 VHOST_CONFIG_LOG(path, INFO, "disabling host segmentation offloading support");
     983                 :          0 :                 vsocket->supported_features &= ~seg_offload_features;
     984                 :          0 :                 vsocket->features &= ~seg_offload_features;
     985                 :            :         }
     986                 :            : 
     987         [ #  # ]:          0 :         if (!vsocket->iommu_support) {
     988                 :          0 :                 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
     989                 :          0 :                 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
     990                 :            :         }
     991                 :            : 
     992         [ #  # ]:          0 :         if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
     993                 :          0 :                 vsocket->protocol_features &=
     994                 :            :                         ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
     995                 :            :         } else {
     996                 :            : #ifndef RTE_LIBRTE_VHOST_POSTCOPY
     997                 :            :                 VHOST_CONFIG_LOG(path, ERR, "Postcopy requested but not compiled");
     998                 :            :                 goto out_mutex;
     999                 :            : #endif
    1000                 :            :         }
    1001                 :            : 
    1002         [ #  # ]:          0 :         if (!vsocket->is_vduse) {
    1003         [ #  # ]:          0 :                 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
    1004                 :          0 :                         vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
    1005   [ #  #  #  # ]:          0 :                         if (vsocket->reconnect && reconn_tid.opaque_id == 0) {
    1006         [ #  # ]:          0 :                                 if (vhost_user_reconnect_init() != 0)
    1007                 :          0 :                                         goto out_mutex;
    1008                 :            :                         }
    1009                 :            :                 } else {
    1010                 :          0 :                         vsocket->is_server = true;
    1011                 :            :                 }
    1012         [ #  # ]:          0 :                 if (create_unix_socket(vsocket) < 0)
    1013                 :          0 :                         goto out_mutex;
    1014                 :            :         }
    1015                 :            : 
    1016                 :          0 :         vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
    1017                 :            : 
    1018                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
    1019                 :          0 :         return 0;
    1020                 :            : 
    1021                 :          0 : out_mutex:
    1022         [ #  # ]:          0 :         if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
    1023                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to destroy connection mutex");
    1024                 :            :         }
    1025                 :          0 : out:
    1026                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
    1027                 :          0 :         return -1;
    1028                 :            : }
    1029                 :            : 
    1030                 :            : static bool
    1031                 :          0 : vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
    1032                 :            : {
    1033                 :            :         int found = false;
    1034                 :            :         struct vhost_user_reconnect *reconn, *next;
    1035                 :            : 
    1036                 :          0 :         pthread_mutex_lock(&reconn_list.mutex);
    1037                 :            : 
    1038                 :          0 :         for (reconn = TAILQ_FIRST(&reconn_list.head);
    1039         [ #  # ]:          0 :              reconn != NULL; reconn = next) {
    1040                 :          0 :                 next = TAILQ_NEXT(reconn, next);
    1041                 :            : 
    1042         [ #  # ]:          0 :                 if (reconn->vsocket == vsocket) {
    1043         [ #  # ]:          0 :                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
    1044                 :          0 :                         close(reconn->fd);
    1045                 :          0 :                         free(reconn);
    1046                 :            :                         found = true;
    1047                 :          0 :                         break;
    1048                 :            :                 }
    1049                 :            :         }
    1050                 :          0 :         pthread_mutex_unlock(&reconn_list.mutex);
    1051                 :          0 :         return found;
    1052                 :            : }
    1053                 :            : 
    1054                 :            : /**
    1055                 :            :  * Unregister the specified vhost socket
    1056                 :            :  */
    1057                 :            : int
    1058                 :          0 : rte_vhost_driver_unregister(const char *path)
    1059                 :            : {
    1060                 :            :         int i;
    1061                 :            :         int count;
    1062                 :            :         struct vhost_user_connection *conn, *next;
    1063                 :            : 
    1064         [ #  # ]:          0 :         if (path == NULL)
    1065                 :            :                 return -1;
    1066                 :            : 
    1067                 :          0 : again:
    1068                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
    1069                 :            : 
    1070         [ #  # ]:          0 :         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
    1071                 :          0 :                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
    1072         [ #  # ]:          0 :                 if (strcmp(vsocket->path, path))
    1073                 :            :                         continue;
    1074                 :            : 
    1075         [ #  # ]:          0 :                 if (vsocket->is_vduse) {
    1076                 :          0 :                         vduse_device_destroy(path);
    1077         [ #  # ]:          0 :                 } else if (vsocket->is_server) {
    1078                 :            :                         /*
    1079                 :            :                          * If r/wcb is executing, release vhost_user's
    1080                 :            :                          * mutex lock, and try again since the r/wcb
    1081                 :            :                          * may use the mutex lock.
    1082                 :            :                          */
    1083         [ #  # ]:          0 :                         if (fdset_try_del(vhost_user.fdset, vsocket->socket_fd) == -1) {
    1084                 :          0 :                                 pthread_mutex_unlock(&vhost_user.mutex);
    1085                 :          0 :                                 goto again;
    1086                 :            :                         }
    1087         [ #  # ]:          0 :                 } else if (vsocket->reconnect) {
    1088                 :          0 :                         vhost_user_remove_reconnect(vsocket);
    1089                 :            :                 }
    1090                 :            : 
    1091                 :          0 :                 pthread_mutex_lock(&vsocket->conn_mutex);
    1092                 :          0 :                 for (conn = TAILQ_FIRST(&vsocket->conn_list);
    1093         [ #  # ]:          0 :                          conn != NULL;
    1094                 :            :                          conn = next) {
    1095                 :          0 :                         next = TAILQ_NEXT(conn, next);
    1096                 :            : 
    1097                 :            :                         /*
    1098                 :            :                          * If r/wcb is executing, release vsocket's
    1099                 :            :                          * conn_mutex and vhost_user's mutex locks, and
    1100                 :            :                          * try again since the r/wcb may use the
    1101                 :            :                          * conn_mutex and mutex locks.
    1102                 :            :                          */
    1103         [ #  # ]:          0 :                         if (fdset_try_del(vhost_user.fdset,
    1104                 :            :                                           conn->connfd) == -1) {
    1105                 :          0 :                                 pthread_mutex_unlock(&vsocket->conn_mutex);
    1106                 :          0 :                                 pthread_mutex_unlock(&vhost_user.mutex);
    1107                 :          0 :                                 goto again;
    1108                 :            :                         }
    1109                 :            : 
    1110                 :          0 :                         VHOST_CONFIG_LOG(path, INFO, "free connfd %d", conn->connfd);
    1111                 :          0 :                         close(conn->connfd);
    1112                 :          0 :                         vhost_destroy_device(conn->vid);
    1113         [ #  # ]:          0 :                         TAILQ_REMOVE(&vsocket->conn_list, conn, next);
    1114                 :          0 :                         free(conn);
    1115                 :            :                 }
    1116                 :          0 :                 pthread_mutex_unlock(&vsocket->conn_mutex);
    1117                 :            : 
    1118         [ #  # ]:          0 :                 if (vsocket->is_server) {
    1119                 :          0 :                         close(vsocket->socket_fd);
    1120                 :          0 :                         unlink(path);
    1121                 :            :                 }
    1122                 :            : 
    1123                 :          0 :                 pthread_mutex_destroy(&vsocket->conn_mutex);
    1124                 :          0 :                 vhost_user_socket_mem_free(vsocket);
    1125                 :            : 
    1126                 :          0 :                 count = --vhost_user.vsocket_cnt;
    1127                 :          0 :                 vhost_user.vsockets[i] = vhost_user.vsockets[count];
    1128                 :          0 :                 vhost_user.vsockets[count] = NULL;
    1129                 :          0 :                 pthread_mutex_unlock(&vhost_user.mutex);
    1130                 :          0 :                 return 0;
    1131                 :            :         }
    1132                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
    1133                 :            : 
    1134                 :          0 :         return -1;
    1135                 :            : }
    1136                 :            : 
    1137                 :            : /*
    1138                 :            :  * Register ops so that we can add/remove device to data core.
    1139                 :            :  */
    1140                 :            : int
    1141                 :          0 : rte_vhost_driver_callback_register(const char *path,
    1142                 :            :         struct rte_vhost_device_ops const * const ops)
    1143                 :            : {
    1144                 :            :         struct vhost_user_socket *vsocket;
    1145                 :            : 
    1146                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
    1147                 :          0 :         vsocket = find_vhost_user_socket(path);
    1148         [ #  # ]:          0 :         if (vsocket)
    1149                 :          0 :                 vsocket->notify_ops = ops;
    1150                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
    1151                 :            : 
    1152         [ #  # ]:          0 :         return vsocket ? 0 : -1;
    1153                 :            : }
    1154                 :            : 
    1155                 :            : struct rte_vhost_device_ops const *
    1156                 :          0 : vhost_driver_callback_get(const char *path)
    1157                 :            : {
    1158                 :            :         struct vhost_user_socket *vsocket;
    1159                 :            : 
    1160                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
    1161                 :          0 :         vsocket = find_vhost_user_socket(path);
    1162                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
    1163                 :            : 
    1164         [ #  # ]:          0 :         return vsocket ? vsocket->notify_ops : NULL;
    1165                 :            : }
    1166                 :            : 
    1167                 :            : int
    1168                 :          0 : rte_vhost_driver_start(const char *path)
    1169                 :            : {
    1170                 :            :         struct vhost_user_socket *vsocket;
    1171                 :            : 
    1172                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
    1173                 :          0 :         vsocket = find_vhost_user_socket(path);
    1174                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
    1175                 :            : 
    1176         [ #  # ]:          0 :         if (!vsocket)
    1177                 :            :                 return -1;
    1178                 :            : 
    1179         [ #  # ]:          0 :         if (vsocket->is_vduse)
    1180                 :          0 :                 return vduse_device_create(path, vsocket->net_compliant_ol_flags);
    1181                 :            : 
    1182         [ #  # ]:          0 :         if (vhost_user.fdset == NULL) {
    1183                 :          0 :                 vhost_user.fdset = fdset_init("vhost-evt");
    1184         [ #  # ]:          0 :                 if (vhost_user.fdset == NULL) {
    1185                 :          0 :                         VHOST_CONFIG_LOG(path, ERR, "failed to init Vhost-user fdset");
    1186                 :          0 :                         return -1;
    1187                 :            :                 }
    1188                 :            :         }
    1189                 :            : 
    1190         [ #  # ]:          0 :         if (vsocket->is_server)
    1191                 :          0 :                 return vhost_user_start_server(vsocket);
    1192                 :            :         else
    1193                 :          0 :                 return vhost_user_start_client(vsocket);
    1194                 :            : }

Generated by: LCOV version 1.14