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-05-01 17:49:45 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 <eal_export.h>
      18                 :            : #include <rte_thread.h>
      19                 :            : #include <rte_log.h>
      20                 :            : 
      21                 :            : #include "fd_man.h"
      22                 :            : #include "vduse.h"
      23                 :            : #include "vhost.h"
      24                 :            : #include "vhost_user.h"
      25                 :            : 
      26                 :            : 
      27                 :            : TAILQ_HEAD(vhost_user_connection_list, vhost_user_connection);
      28                 :            : 
      29                 :            : /*
      30                 :            :  * Every time rte_vhost_driver_register() is invoked, an associated
      31                 :            :  * vhost_user_socket struct will be created.
      32                 :            :  */
      33                 :            : struct vhost_user_socket {
      34                 :            :         struct vhost_user_connection_list conn_list;
      35                 :            :         pthread_mutex_t conn_mutex;
      36                 :            :         char *path;
      37                 :            :         int socket_fd;
      38                 :            :         struct sockaddr_un un;
      39                 :            :         bool is_server;
      40                 :            :         bool is_vduse;
      41                 :            :         bool reconnect;
      42                 :            :         bool iommu_support;
      43                 :            :         bool use_builtin_virtio_net;
      44                 :            :         bool extbuf;
      45                 :            :         bool linearbuf;
      46                 :            :         bool async_copy;
      47                 :            :         bool net_compliant_ol_flags;
      48                 :            :         bool stats_enabled;
      49                 :            :         bool async_connect;
      50                 :            : 
      51                 :            :         /*
      52                 :            :          * The "supported_features" indicates the feature bits the
      53                 :            :          * vhost driver supports. The "features" indicates the feature
      54                 :            :          * bits after the rte_vhost_driver_features_disable/enable().
      55                 :            :          * It is also the final feature bits used for vhost-user
      56                 :            :          * features negotiation.
      57                 :            :          */
      58                 :            :         uint64_t supported_features;
      59                 :            :         uint64_t features;
      60                 :            : 
      61                 :            :         uint64_t protocol_features;
      62                 :            : 
      63                 :            :         uint32_t max_queue_pairs;
      64                 :            : 
      65                 :            :         struct rte_vdpa_device *vdpa_dev;
      66                 :            : 
      67                 :            :         struct rte_vhost_device_ops const *notify_ops;
      68                 :            : };
      69                 :            : 
      70                 :            : struct vhost_user_connection {
      71                 :            :         struct vhost_user_socket *vsocket;
      72                 :            :         int connfd;
      73                 :            :         int vid;
      74                 :            : 
      75                 :            :         TAILQ_ENTRY(vhost_user_connection) next;
      76                 :            : };
      77                 :            : 
      78                 :            : #define MAX_VHOST_SOCKET 1024
      79                 :            : struct vhost_user {
      80                 :            :         struct vhost_user_socket *vsockets[MAX_VHOST_SOCKET];
      81                 :            :         struct fdset *fdset;
      82                 :            :         int vsocket_cnt;
      83                 :            :         pthread_mutex_t mutex;
      84                 :            : };
      85                 :            : 
      86                 :            : #define MAX_VIRTIO_BACKLOG 128
      87                 :            : 
      88                 :            : static void vhost_user_server_new_connection(int fd, void *data, int *close);
      89                 :            : static void vhost_user_read_cb(int fd, void *dat, int *close);
      90                 :            : static int create_unix_socket(struct vhost_user_socket *vsocket);
      91                 :            : static int vhost_user_start_client(struct vhost_user_socket *vsocket);
      92                 :            : 
      93                 :            : static struct vhost_user vhost_user = {
      94                 :            :         .vsocket_cnt = 0,
      95                 :            :         .mutex = PTHREAD_MUTEX_INITIALIZER,
      96                 :            : };
      97                 :            : 
      98                 :            : /*
      99                 :            :  * return bytes# of read on success or negative val on failure. Update fdnum
     100                 :            :  * with number of fds read.
     101                 :            :  */
     102                 :            : int
     103                 :          0 : read_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int max_fds,
     104                 :            :                 int *fd_num)
     105                 :          0 : {
     106                 :            :         struct iovec iov;
     107                 :            :         struct msghdr msgh;
     108                 :          0 :         char control[CMSG_SPACE(max_fds * sizeof(int))];
     109                 :            :         struct cmsghdr *cmsg;
     110                 :            :         int got_fds = 0;
     111                 :            :         int ret;
     112                 :            : 
     113                 :          0 :         *fd_num = 0;
     114                 :            : 
     115                 :            :         memset(&msgh, 0, sizeof(msgh));
     116                 :          0 :         iov.iov_base = buf;
     117                 :          0 :         iov.iov_len  = buflen;
     118                 :            : 
     119                 :          0 :         msgh.msg_iov = &iov;
     120                 :          0 :         msgh.msg_iovlen = 1;
     121                 :          0 :         msgh.msg_control = control;
     122                 :          0 :         msgh.msg_controllen = sizeof(control);
     123                 :            : 
     124                 :          0 :         ret = recvmsg(sockfd, &msgh, 0);
     125         [ #  # ]:          0 :         if (ret <= 0) {
     126         [ #  # ]:          0 :                 if (ret)
     127                 :          0 :                         VHOST_CONFIG_LOG(ifname, ERR, "recvmsg failed on fd %d (%s)",
     128                 :            :                                 sockfd, strerror(errno));
     129                 :          0 :                 return ret;
     130                 :            :         }
     131                 :            : 
     132         [ #  # ]:          0 :         if (msgh.msg_flags & MSG_TRUNC)
     133                 :          0 :                 VHOST_CONFIG_LOG(ifname, ERR, "truncated msg (fd %d)", sockfd);
     134                 :            : 
     135                 :            :         /* MSG_CTRUNC may be caused by LSM misconfiguration */
     136         [ #  # ]:          0 :         if (msgh.msg_flags & MSG_CTRUNC)
     137                 :          0 :                 VHOST_CONFIG_LOG(ifname, ERR, "truncated control data (fd %d)", sockfd);
     138                 :            : 
     139   [ #  #  #  # ]:          0 :         for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL;
     140                 :            :                 cmsg = CMSG_NXTHDR(&msgh, cmsg)) {
     141         [ #  # ]:          0 :                 if ((cmsg->cmsg_level == SOL_SOCKET) &&
     142                 :            :                         (cmsg->cmsg_type == SCM_RIGHTS)) {
     143                 :          0 :                         got_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
     144                 :          0 :                         *fd_num = got_fds;
     145                 :          0 :                         memcpy(fds, CMSG_DATA(cmsg), got_fds * sizeof(int));
     146                 :            :                         break;
     147                 :            :                 }
     148                 :            :         }
     149                 :            : 
     150                 :            :         /* Clear out unused file descriptors */
     151         [ #  # ]:          0 :         while (got_fds < max_fds)
     152                 :          0 :                 fds[got_fds++] = -1;
     153                 :            : 
     154                 :            :         return ret;
     155                 :            : }
     156                 :            : 
     157                 :            : int
     158                 :          0 : send_fd_message(char *ifname, int sockfd, char *buf, int buflen, int *fds, int fd_num)
     159                 :          0 : {
     160                 :            : 
     161                 :            :         struct iovec iov;
     162                 :            :         struct msghdr msgh;
     163                 :          0 :         size_t fdsize = fd_num * sizeof(int);
     164         [ #  # ]:          0 :         char control[CMSG_SPACE(fdsize)];
     165                 :            :         struct cmsghdr *cmsg;
     166                 :            :         int ret;
     167                 :            : 
     168                 :            :         memset(&msgh, 0, sizeof(msgh));
     169                 :          0 :         iov.iov_base = buf;
     170                 :          0 :         iov.iov_len = buflen;
     171                 :            : 
     172                 :          0 :         msgh.msg_iov = &iov;
     173                 :          0 :         msgh.msg_iovlen = 1;
     174                 :            : 
     175         [ #  # ]:          0 :         if (fds && fd_num > 0) {
     176                 :          0 :                 msgh.msg_control = control;
     177                 :          0 :                 msgh.msg_controllen = sizeof(control);
     178         [ #  # ]:          0 :                 cmsg = CMSG_FIRSTHDR(&msgh);
     179                 :            :                 if (cmsg == NULL) {
     180                 :          0 :                         VHOST_CONFIG_LOG(ifname, ERR, "cmsg == NULL");
     181                 :          0 :                         errno = EINVAL;
     182                 :          0 :                         return -1;
     183                 :            :                 }
     184                 :          0 :                 cmsg->cmsg_len = CMSG_LEN(fdsize);
     185                 :          0 :                 cmsg->cmsg_level = SOL_SOCKET;
     186                 :          0 :                 cmsg->cmsg_type = SCM_RIGHTS;
     187                 :          0 :                 memcpy(CMSG_DATA(cmsg), fds, fdsize);
     188                 :            :         } else {
     189                 :            :                 msgh.msg_control = NULL;
     190                 :            :                 msgh.msg_controllen = 0;
     191                 :            :         }
     192                 :            : 
     193                 :            :         do {
     194                 :          0 :                 ret = sendmsg(sockfd, &msgh, MSG_NOSIGNAL);
     195   [ #  #  #  # ]:          0 :         } while (ret < 0 && errno == EINTR);
     196                 :            : 
     197         [ #  # ]:          0 :         if (ret < 0) {
     198                 :          0 :                 VHOST_CONFIG_LOG(ifname, ERR, "sendmsg error on fd %d (%s)",
     199                 :            :                         sockfd, strerror(errno));
     200                 :          0 :                 return ret;
     201                 :            :         }
     202                 :            : 
     203                 :            :         return ret;
     204                 :            : }
     205                 :            : 
     206                 :            : static void
     207                 :          0 : vhost_user_add_connection(int fd, struct vhost_user_socket *vsocket)
     208                 :            : {
     209                 :            :         int vid;
     210                 :            :         size_t size;
     211                 :            :         struct vhost_user_connection *conn;
     212                 :            :         int ret;
     213                 :            :         struct virtio_net *dev;
     214                 :            : 
     215         [ #  # ]:          0 :         if (vsocket == NULL)
     216                 :            :                 return;
     217                 :            : 
     218                 :          0 :         conn = malloc(sizeof(*conn));
     219         [ #  # ]:          0 :         if (conn == NULL) {
     220                 :          0 :                 close(fd);
     221                 :          0 :                 return;
     222                 :            :         }
     223                 :            : 
     224                 :          0 :         vid = vhost_user_new_device();
     225         [ #  # ]:          0 :         if (vid == -1) {
     226                 :          0 :                 goto err;
     227                 :            :         }
     228                 :            : 
     229                 :          0 :         size = strnlen(vsocket->path, PATH_MAX);
     230                 :          0 :         vhost_set_ifname(vid, vsocket->path, size);
     231                 :            : 
     232                 :          0 :         vhost_setup_virtio_net(vid, vsocket->use_builtin_virtio_net,
     233                 :          0 :                 vsocket->net_compliant_ol_flags, vsocket->stats_enabled,
     234                 :          0 :                 vsocket->iommu_support);
     235                 :            : 
     236                 :          0 :         vhost_attach_vdpa_device(vid, vsocket->vdpa_dev);
     237                 :            : 
     238         [ #  # ]:          0 :         if (vsocket->extbuf)
     239                 :          0 :                 vhost_enable_extbuf(vid);
     240                 :            : 
     241         [ #  # ]:          0 :         if (vsocket->linearbuf)
     242                 :          0 :                 vhost_enable_linearbuf(vid);
     243                 :            : 
     244         [ #  # ]:          0 :         if (vsocket->async_copy) {
     245                 :            :                 dev = get_device(vid);
     246                 :            : 
     247         [ #  # ]:          0 :                 if (dev)
     248                 :          0 :                         dev->async_copy = 1;
     249                 :            :         }
     250                 :            : 
     251                 :          0 :         VHOST_CONFIG_LOG(vsocket->path, INFO, "new device, handle is %d", vid);
     252                 :            : 
     253         [ #  # ]:          0 :         if (vsocket->notify_ops->new_connection) {
     254                 :          0 :                 ret = vsocket->notify_ops->new_connection(vid);
     255         [ #  # ]:          0 :                 if (ret < 0) {
     256                 :          0 :                         VHOST_CONFIG_LOG(vsocket->path, ERR,
     257                 :            :                                 "failed to add vhost user connection with fd %d",
     258                 :            :                                 fd);
     259                 :          0 :                         goto err_cleanup;
     260                 :            :                 }
     261                 :            :         }
     262                 :            : 
     263                 :          0 :         conn->connfd = fd;
     264                 :          0 :         conn->vsocket = vsocket;
     265                 :          0 :         conn->vid = vid;
     266                 :          0 :         ret = fdset_add(vhost_user.fdset, fd, vhost_user_read_cb,
     267                 :            :                         NULL, conn);
     268         [ #  # ]:          0 :         if (ret < 0) {
     269                 :          0 :                 VHOST_CONFIG_LOG(vsocket->path, ERR,
     270                 :            :                         "failed to add fd %d into vhost server fdset",
     271                 :            :                         fd);
     272                 :            : 
     273         [ #  # ]:          0 :                 if (vsocket->notify_ops->destroy_connection)
     274                 :          0 :                         vsocket->notify_ops->destroy_connection(conn->vid);
     275                 :            : 
     276                 :          0 :                 goto err_cleanup;
     277                 :            :         }
     278                 :            : 
     279                 :          0 :         pthread_mutex_lock(&vsocket->conn_mutex);
     280                 :          0 :         TAILQ_INSERT_TAIL(&vsocket->conn_list, conn, next);
     281                 :          0 :         pthread_mutex_unlock(&vsocket->conn_mutex);
     282                 :            : 
     283                 :          0 :         return;
     284                 :            : 
     285                 :          0 : err_cleanup:
     286                 :          0 :         vhost_destroy_device(vid);
     287                 :          0 : err:
     288                 :          0 :         free(conn);
     289                 :          0 :         close(fd);
     290                 :            : }
     291                 :            : 
     292                 :            : /* call back when there is new vhost-user connection from client  */
     293                 :            : static void
     294                 :          0 : vhost_user_server_new_connection(int fd, void *dat, int *close __rte_unused)
     295                 :            : {
     296                 :            :         struct vhost_user_socket *vsocket = dat;
     297                 :            : 
     298                 :          0 :         fd = accept(fd, NULL, NULL);
     299         [ #  # ]:          0 :         if (fd < 0)
     300                 :            :                 return;
     301                 :            : 
     302                 :          0 :         VHOST_CONFIG_LOG(vsocket->path, INFO, "new vhost user connection is %d", fd);
     303                 :          0 :         vhost_user_add_connection(fd, vsocket);
     304                 :            : }
     305                 :            : 
     306                 :            : static void
     307                 :          0 : vhost_user_read_cb(int connfd, void *dat, int *close)
     308                 :            : {
     309                 :            :         struct vhost_user_connection *conn = dat;
     310                 :          0 :         struct vhost_user_socket *vsocket = conn->vsocket;
     311                 :            :         int ret;
     312                 :            : 
     313                 :          0 :         ret = vhost_user_msg_handler(conn->vid, connfd);
     314         [ #  # ]:          0 :         if (ret < 0) {
     315         [ #  # ]:          0 :                 struct virtio_net *dev = get_device(conn->vid);
     316                 :            : 
     317                 :          0 :                 *close = 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                 :            : RTE_EXPORT_SYMBOL(rte_vhost_driver_attach_vdpa_device)
     576                 :            : int
     577                 :          0 : rte_vhost_driver_attach_vdpa_device(const char *path,
     578                 :            :                 struct rte_vdpa_device *dev)
     579                 :            : {
     580                 :            :         struct vhost_user_socket *vsocket;
     581                 :            : 
     582         [ #  # ]:          0 :         if (dev == NULL || path == NULL)
     583                 :            :                 return -1;
     584                 :            : 
     585                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     586                 :          0 :         vsocket = find_vhost_user_socket(path);
     587         [ #  # ]:          0 :         if (vsocket)
     588                 :          0 :                 vsocket->vdpa_dev = dev;
     589                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     590                 :            : 
     591         [ #  # ]:          0 :         return vsocket ? 0 : -1;
     592                 :            : }
     593                 :            : 
     594                 :            : RTE_EXPORT_SYMBOL(rte_vhost_driver_detach_vdpa_device)
     595                 :            : int
     596                 :          0 : rte_vhost_driver_detach_vdpa_device(const char *path)
     597                 :            : {
     598                 :            :         struct vhost_user_socket *vsocket;
     599                 :            : 
     600                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     601                 :          0 :         vsocket = find_vhost_user_socket(path);
     602         [ #  # ]:          0 :         if (vsocket)
     603                 :          0 :                 vsocket->vdpa_dev = NULL;
     604                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     605                 :            : 
     606         [ #  # ]:          0 :         return vsocket ? 0 : -1;
     607                 :            : }
     608                 :            : 
     609                 :            : RTE_EXPORT_SYMBOL(rte_vhost_driver_get_vdpa_device)
     610                 :            : struct rte_vdpa_device *
     611                 :          0 : rte_vhost_driver_get_vdpa_device(const char *path)
     612                 :            : {
     613                 :            :         struct vhost_user_socket *vsocket;
     614                 :            :         struct rte_vdpa_device *dev = NULL;
     615                 :            : 
     616                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     617                 :          0 :         vsocket = find_vhost_user_socket(path);
     618         [ #  # ]:          0 :         if (vsocket)
     619                 :          0 :                 dev = vsocket->vdpa_dev;
     620                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     621                 :            : 
     622                 :          0 :         return dev;
     623                 :            : }
     624                 :            : 
     625                 :            : RTE_EXPORT_SYMBOL(rte_vhost_driver_get_vdpa_dev_type)
     626                 :            : int
     627                 :          0 : rte_vhost_driver_get_vdpa_dev_type(const char *path, uint32_t *type)
     628                 :            : {
     629                 :            :         struct vhost_user_socket *vsocket;
     630                 :            :         struct rte_vdpa_device *vdpa_dev;
     631                 :            :         int ret = 0;
     632                 :            : 
     633                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     634                 :          0 :         vsocket = find_vhost_user_socket(path);
     635         [ #  # ]:          0 :         if (!vsocket) {
     636                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
     637                 :            :                 ret = -1;
     638                 :          0 :                 goto unlock_exit;
     639                 :            :         }
     640                 :            : 
     641                 :          0 :         vdpa_dev = vsocket->vdpa_dev;
     642         [ #  # ]:          0 :         if (!vdpa_dev) {
     643                 :            :                 ret = -1;
     644                 :          0 :                 goto unlock_exit;
     645                 :            :         }
     646                 :            : 
     647                 :          0 :         *type = vdpa_dev->type;
     648                 :            : 
     649                 :          0 : unlock_exit:
     650                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     651                 :          0 :         return ret;
     652                 :            : }
     653                 :            : 
     654                 :            : RTE_EXPORT_SYMBOL(rte_vhost_driver_disable_features)
     655                 :            : int
     656                 :          0 : rte_vhost_driver_disable_features(const char *path, uint64_t features)
     657                 :            : {
     658                 :            :         struct vhost_user_socket *vsocket;
     659                 :            : 
     660                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     661                 :          0 :         vsocket = find_vhost_user_socket(path);
     662                 :            : 
     663                 :            :         /* Note that use_builtin_virtio_net is not affected by this function
     664                 :            :          * since callers may want to selectively disable features of the
     665                 :            :          * built-in vhost net device backend.
     666                 :            :          */
     667                 :            : 
     668         [ #  # ]:          0 :         if (vsocket)
     669                 :          0 :                 vsocket->features &= ~features;
     670                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     671                 :            : 
     672         [ #  # ]:          0 :         return vsocket ? 0 : -1;
     673                 :            : }
     674                 :            : 
     675                 :            : RTE_EXPORT_SYMBOL(rte_vhost_driver_enable_features)
     676                 :            : int
     677                 :          0 : rte_vhost_driver_enable_features(const char *path, uint64_t features)
     678                 :            : {
     679                 :            :         struct vhost_user_socket *vsocket;
     680                 :            : 
     681                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     682                 :          0 :         vsocket = find_vhost_user_socket(path);
     683         [ #  # ]:          0 :         if (vsocket) {
     684         [ #  # ]:          0 :                 if ((vsocket->supported_features & features) != features) {
     685                 :            :                         /*
     686                 :            :                          * trying to enable features the driver doesn't
     687                 :            :                          * support.
     688                 :            :                          */
     689                 :          0 :                         pthread_mutex_unlock(&vhost_user.mutex);
     690                 :          0 :                         return -1;
     691                 :            :                 }
     692                 :          0 :                 vsocket->features |= features;
     693                 :            :         }
     694                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     695                 :            : 
     696         [ #  # ]:          0 :         return vsocket ? 0 : -1;
     697                 :            : }
     698                 :            : 
     699                 :            : RTE_EXPORT_SYMBOL(rte_vhost_driver_set_features)
     700                 :            : int
     701                 :          0 : rte_vhost_driver_set_features(const char *path, uint64_t features)
     702                 :            : {
     703                 :            :         struct vhost_user_socket *vsocket;
     704                 :            : 
     705                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     706                 :          0 :         vsocket = find_vhost_user_socket(path);
     707         [ #  # ]:          0 :         if (vsocket) {
     708                 :          0 :                 vsocket->supported_features = features;
     709                 :          0 :                 vsocket->features = features;
     710                 :            : 
     711                 :            :                 /* Anyone setting feature bits is implementing their own vhost
     712                 :            :                  * device backend.
     713                 :            :                  */
     714                 :          0 :                 vsocket->use_builtin_virtio_net = false;
     715                 :            :         }
     716                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     717                 :            : 
     718         [ #  # ]:          0 :         return vsocket ? 0 : -1;
     719                 :            : }
     720                 :            : 
     721                 :            : RTE_EXPORT_SYMBOL(rte_vhost_driver_get_features)
     722                 :            : int
     723                 :          0 : rte_vhost_driver_get_features(const char *path, uint64_t *features)
     724                 :            : {
     725                 :            :         struct vhost_user_socket *vsocket;
     726                 :            :         uint64_t vdpa_features;
     727                 :            :         struct rte_vdpa_device *vdpa_dev;
     728                 :            :         int ret = 0;
     729                 :            : 
     730                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     731                 :          0 :         vsocket = find_vhost_user_socket(path);
     732         [ #  # ]:          0 :         if (!vsocket) {
     733                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
     734                 :            :                 ret = -1;
     735                 :          0 :                 goto unlock_exit;
     736                 :            :         }
     737                 :            : 
     738                 :          0 :         vdpa_dev = vsocket->vdpa_dev;
     739         [ #  # ]:          0 :         if (!vdpa_dev) {
     740                 :          0 :                 *features = vsocket->features;
     741                 :          0 :                 goto unlock_exit;
     742                 :            :         }
     743                 :            : 
     744         [ #  # ]:          0 :         if (vdpa_dev->ops->get_features(vdpa_dev, &vdpa_features) < 0) {
     745                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to get vdpa features for socket file.");
     746                 :            :                 ret = -1;
     747                 :          0 :                 goto unlock_exit;
     748                 :            :         }
     749                 :            : 
     750                 :          0 :         *features = vsocket->features & vdpa_features;
     751                 :            : 
     752                 :          0 : unlock_exit:
     753                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     754                 :          0 :         return ret;
     755                 :            : }
     756                 :            : 
     757                 :            : RTE_EXPORT_SYMBOL(rte_vhost_driver_set_protocol_features)
     758                 :            : int
     759                 :          0 : rte_vhost_driver_set_protocol_features(const char *path,
     760                 :            :                 uint64_t protocol_features)
     761                 :            : {
     762                 :            :         struct vhost_user_socket *vsocket;
     763                 :            : 
     764                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     765                 :          0 :         vsocket = find_vhost_user_socket(path);
     766         [ #  # ]:          0 :         if (vsocket)
     767                 :          0 :                 vsocket->protocol_features = protocol_features;
     768                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     769         [ #  # ]:          0 :         return vsocket ? 0 : -1;
     770                 :            : }
     771                 :            : 
     772                 :            : RTE_EXPORT_SYMBOL(rte_vhost_driver_get_protocol_features)
     773                 :            : int
     774                 :          0 : rte_vhost_driver_get_protocol_features(const char *path,
     775                 :            :                 uint64_t *protocol_features)
     776                 :            : {
     777                 :            :         struct vhost_user_socket *vsocket;
     778                 :            :         uint64_t vdpa_protocol_features;
     779                 :            :         struct rte_vdpa_device *vdpa_dev;
     780                 :            :         int ret = 0;
     781                 :            : 
     782                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     783                 :          0 :         vsocket = find_vhost_user_socket(path);
     784         [ #  # ]:          0 :         if (!vsocket) {
     785                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
     786                 :            :                 ret = -1;
     787                 :          0 :                 goto unlock_exit;
     788                 :            :         }
     789                 :            : 
     790                 :          0 :         vdpa_dev = vsocket->vdpa_dev;
     791         [ #  # ]:          0 :         if (!vdpa_dev) {
     792                 :          0 :                 *protocol_features = vsocket->protocol_features;
     793                 :          0 :                 goto unlock_exit;
     794                 :            :         }
     795                 :            : 
     796         [ #  # ]:          0 :         if (vdpa_dev->ops->get_protocol_features(vdpa_dev,
     797                 :            :                                 &vdpa_protocol_features) < 0) {
     798                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to get vdpa protocol features.");
     799                 :            :                 ret = -1;
     800                 :          0 :                 goto unlock_exit;
     801                 :            :         }
     802                 :            : 
     803                 :          0 :         *protocol_features = vsocket->protocol_features
     804                 :          0 :                 & vdpa_protocol_features;
     805                 :            : 
     806                 :          0 : unlock_exit:
     807                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     808                 :          0 :         return ret;
     809                 :            : }
     810                 :            : 
     811                 :            : RTE_EXPORT_SYMBOL(rte_vhost_driver_get_queue_num)
     812                 :            : int
     813                 :          0 : rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
     814                 :            : {
     815                 :            :         struct vhost_user_socket *vsocket;
     816                 :            :         uint32_t vdpa_queue_num;
     817                 :            :         struct rte_vdpa_device *vdpa_dev;
     818                 :            :         int ret = 0;
     819                 :            : 
     820                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     821                 :          0 :         vsocket = find_vhost_user_socket(path);
     822         [ #  # ]:          0 :         if (!vsocket) {
     823                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
     824                 :            :                 ret = -1;
     825                 :          0 :                 goto unlock_exit;
     826                 :            :         }
     827                 :            : 
     828                 :          0 :         vdpa_dev = vsocket->vdpa_dev;
     829         [ #  # ]:          0 :         if (!vdpa_dev) {
     830                 :          0 :                 *queue_num = vsocket->max_queue_pairs;
     831                 :          0 :                 goto unlock_exit;
     832                 :            :         }
     833                 :            : 
     834         [ #  # ]:          0 :         if (vdpa_dev->ops->get_queue_num(vdpa_dev, &vdpa_queue_num) < 0) {
     835                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to get vdpa queue number.");
     836                 :            :                 ret = -1;
     837                 :          0 :                 goto unlock_exit;
     838                 :            :         }
     839                 :            : 
     840                 :          0 :         *queue_num = RTE_MIN(vsocket->max_queue_pairs, vdpa_queue_num);
     841                 :            : 
     842                 :          0 : unlock_exit:
     843                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     844                 :          0 :         return ret;
     845                 :            : }
     846                 :            : 
     847                 :            : RTE_EXPORT_SYMBOL(rte_vhost_driver_set_max_queue_num)
     848                 :            : int
     849                 :          0 : rte_vhost_driver_set_max_queue_num(const char *path, uint32_t max_queue_pairs)
     850                 :            : {
     851                 :            :         struct vhost_user_socket *vsocket;
     852                 :            :         int ret = 0;
     853                 :            : 
     854                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     855                 :          0 :         vsocket = find_vhost_user_socket(path);
     856         [ #  # ]:          0 :         if (!vsocket) {
     857                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "socket file is not registered yet.");
     858                 :            :                 ret = -1;
     859                 :          0 :                 goto unlock_exit;
     860                 :            :         }
     861                 :            : 
     862                 :            :         /*
     863                 :            :          * This is only useful for VDUSE for which number of virtqueues is set
     864                 :            :          * by the backend. For Vhost-user, the number of virtqueues is defined
     865                 :            :          * by the frontend.
     866                 :            :          */
     867         [ #  # ]:          0 :         if (!vsocket->is_vduse) {
     868                 :          0 :                 VHOST_CONFIG_LOG(path, DEBUG,
     869                 :            :                                 "Keeping %u max queue pairs for Vhost-user backend",
     870                 :            :                                 VHOST_MAX_QUEUE_PAIRS);
     871                 :          0 :                 goto unlock_exit;
     872                 :            :         }
     873                 :            : 
     874                 :          0 :         VHOST_CONFIG_LOG(path, INFO, "Setting max queue pairs to %u", max_queue_pairs);
     875                 :            : 
     876         [ #  # ]:          0 :         if (max_queue_pairs > VHOST_MAX_QUEUE_PAIRS) {
     877                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "Library only supports up to %u queue pairs",
     878                 :            :                                 VHOST_MAX_QUEUE_PAIRS);
     879                 :            :                 ret = -1;
     880                 :          0 :                 goto unlock_exit;
     881                 :            :         }
     882                 :            : 
     883                 :          0 :         vsocket->max_queue_pairs = max_queue_pairs;
     884                 :            : 
     885                 :          0 : unlock_exit:
     886                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
     887                 :          0 :         return ret;
     888                 :            : }
     889                 :            : 
     890                 :            : static void
     891                 :          0 : vhost_user_socket_mem_free(struct vhost_user_socket *vsocket)
     892                 :            : {
     893         [ #  # ]:          0 :         if (vsocket == NULL)
     894                 :            :                 return;
     895                 :            : 
     896                 :          0 :         free(vsocket->path);
     897                 :          0 :         free(vsocket);
     898                 :            : }
     899                 :            : 
     900                 :            : /*
     901                 :            :  * Register a new vhost-user socket; here we could act as server
     902                 :            :  * (the default case), or client (when RTE_VHOST_USER_CLIENT) flag
     903                 :            :  * is set.
     904                 :            :  */
     905                 :            : RTE_EXPORT_SYMBOL(rte_vhost_driver_register)
     906                 :            : int
     907                 :          0 : rte_vhost_driver_register(const char *path, uint64_t flags)
     908                 :            : {
     909                 :            :         struct vhost_user_socket *vsocket;
     910                 :            : 
     911         [ #  # ]:          0 :         if (!path)
     912                 :            :                 return -1;
     913                 :            : 
     914                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
     915                 :            : 
     916         [ #  # ]:          0 :         if (vhost_user.vsocket_cnt == MAX_VHOST_SOCKET) {
     917                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "the number of vhost sockets reaches maximum");
     918                 :          0 :                 goto out;
     919                 :            :         }
     920                 :            : 
     921                 :          0 :         vsocket = malloc(sizeof(struct vhost_user_socket));
     922         [ #  # ]:          0 :         if (!vsocket)
     923                 :          0 :                 goto out;
     924                 :            :         memset(vsocket, 0, sizeof(struct vhost_user_socket));
     925                 :          0 :         vsocket->path = strdup(path);
     926         [ #  # ]:          0 :         if (vsocket->path == NULL) {
     927                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to copy socket path string");
     928                 :          0 :                 vhost_user_socket_mem_free(vsocket);
     929                 :          0 :                 goto out;
     930                 :            :         }
     931                 :          0 :         TAILQ_INIT(&vsocket->conn_list);
     932                 :          0 :         pthread_mutex_init(&vsocket->conn_mutex, NULL);
     933                 :            : 
     934         [ #  # ]:          0 :         if (!strncmp("/dev/vduse/", path, strlen("/dev/vduse/")))
     935                 :          0 :                 vsocket->is_vduse = true;
     936                 :            : 
     937                 :          0 :         vsocket->vdpa_dev = NULL;
     938                 :          0 :         vsocket->max_queue_pairs = VHOST_MAX_QUEUE_PAIRS;
     939                 :          0 :         vsocket->extbuf = flags & RTE_VHOST_USER_EXTBUF_SUPPORT;
     940                 :          0 :         vsocket->linearbuf = flags & RTE_VHOST_USER_LINEARBUF_SUPPORT;
     941                 :          0 :         vsocket->async_copy = flags & RTE_VHOST_USER_ASYNC_COPY;
     942                 :          0 :         vsocket->net_compliant_ol_flags = flags & RTE_VHOST_USER_NET_COMPLIANT_OL_FLAGS;
     943                 :          0 :         vsocket->stats_enabled = flags & RTE_VHOST_USER_NET_STATS_ENABLE;
     944                 :          0 :         vsocket->async_connect = flags & RTE_VHOST_USER_ASYNC_CONNECT;
     945         [ #  # ]:          0 :         if (vsocket->is_vduse)
     946                 :          0 :                 vsocket->iommu_support = true;
     947                 :            :         else
     948                 :          0 :                 vsocket->iommu_support = flags & RTE_VHOST_USER_IOMMU_SUPPORT;
     949                 :            : 
     950   [ #  #  #  # ]:          0 :         if (vsocket->async_copy && (vsocket->iommu_support ||
     951         [ #  # ]:          0 :                                 (flags & RTE_VHOST_USER_POSTCOPY_SUPPORT))) {
     952                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "async copy with IOMMU or post-copy not supported");
     953                 :          0 :                 goto out_mutex;
     954                 :            :         }
     955                 :            : 
     956                 :            :         /*
     957                 :            :          * Set the supported features correctly for the builtin vhost-user
     958                 :            :          * net driver.
     959                 :            :          *
     960                 :            :          * Applications know nothing about features the builtin virtio net
     961                 :            :          * driver (virtio_net.c) supports, thus it's not possible for them
     962                 :            :          * to invoke rte_vhost_driver_set_features(). To workaround it, here
     963                 :            :          * we set it unconditionally. If the application want to implement
     964                 :            :          * another vhost-user driver (say SCSI), it should call the
     965                 :            :          * rte_vhost_driver_set_features(), which will overwrite following
     966                 :            :          * two values.
     967                 :            :          */
     968                 :          0 :         vsocket->use_builtin_virtio_net = true;
     969         [ #  # ]:          0 :         if (vsocket->is_vduse) {
     970                 :          0 :                 vsocket->supported_features = VDUSE_NET_SUPPORTED_FEATURES;
     971                 :          0 :                 vsocket->features           = VDUSE_NET_SUPPORTED_FEATURES;
     972                 :            :         } else {
     973                 :          0 :                 vsocket->supported_features = VHOST_USER_NET_SUPPORTED_FEATURES;
     974                 :          0 :                 vsocket->features           = VHOST_USER_NET_SUPPORTED_FEATURES;
     975                 :          0 :                 vsocket->protocol_features  = VHOST_USER_PROTOCOL_FEATURES;
     976                 :            :         }
     977                 :            : 
     978         [ #  # ]:          0 :         if (vsocket->async_copy) {
     979                 :          0 :                 vsocket->supported_features &= ~(1ULL << VHOST_F_LOG_ALL);
     980                 :          0 :                 vsocket->features &= ~(1ULL << VHOST_F_LOG_ALL);
     981                 :          0 :                 VHOST_CONFIG_LOG(path, INFO, "logging feature is disabled in async copy mode");
     982                 :            :         }
     983                 :            : 
     984                 :            :         /*
     985                 :            :          * We'll not be able to receive a buffer from guest in linear mode
     986                 :            :          * without external buffer if it will not fit in a single mbuf, which is
     987                 :            :          * likely if segmentation offloading enabled.
     988                 :            :          */
     989   [ #  #  #  # ]:          0 :         if (vsocket->linearbuf && !vsocket->extbuf) {
     990                 :            :                 uint64_t seg_offload_features =
     991                 :            :                                 (1ULL << VIRTIO_NET_F_HOST_TSO4) |
     992                 :            :                                 (1ULL << VIRTIO_NET_F_HOST_TSO6) |
     993                 :            :                                 (1ULL << VIRTIO_NET_F_HOST_UFO);
     994                 :            : 
     995                 :          0 :                 VHOST_CONFIG_LOG(path, INFO, "Linear buffers requested without external buffers,");
     996                 :          0 :                 VHOST_CONFIG_LOG(path, INFO, "disabling host segmentation offloading support");
     997                 :          0 :                 vsocket->supported_features &= ~seg_offload_features;
     998                 :          0 :                 vsocket->features &= ~seg_offload_features;
     999                 :            :         }
    1000                 :            : 
    1001         [ #  # ]:          0 :         if (!vsocket->iommu_support) {
    1002                 :          0 :                 vsocket->supported_features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
    1003                 :          0 :                 vsocket->features &= ~(1ULL << VIRTIO_F_IOMMU_PLATFORM);
    1004                 :            :         }
    1005                 :            : 
    1006         [ #  # ]:          0 :         if (!(flags & RTE_VHOST_USER_POSTCOPY_SUPPORT)) {
    1007                 :          0 :                 vsocket->protocol_features &=
    1008                 :            :                         ~(1ULL << VHOST_USER_PROTOCOL_F_PAGEFAULT);
    1009                 :            :         } else {
    1010                 :            : #ifndef RTE_LIBRTE_VHOST_POSTCOPY
    1011                 :            :                 VHOST_CONFIG_LOG(path, ERR, "Postcopy requested but not compiled");
    1012                 :            :                 goto out_mutex;
    1013                 :            : #endif
    1014                 :            :         }
    1015                 :            : 
    1016         [ #  # ]:          0 :         if (!vsocket->is_vduse) {
    1017         [ #  # ]:          0 :                 if ((flags & RTE_VHOST_USER_CLIENT) != 0) {
    1018                 :          0 :                         vsocket->reconnect = !(flags & RTE_VHOST_USER_NO_RECONNECT);
    1019   [ #  #  #  # ]:          0 :                         if (vsocket->reconnect && reconn_tid.opaque_id == 0) {
    1020         [ #  # ]:          0 :                                 if (vhost_user_reconnect_init() != 0)
    1021                 :          0 :                                         goto out_mutex;
    1022                 :            :                         }
    1023                 :            :                 } else {
    1024                 :          0 :                         vsocket->is_server = true;
    1025                 :            :                 }
    1026         [ #  # ]:          0 :                 if (create_unix_socket(vsocket) < 0)
    1027                 :          0 :                         goto out_mutex;
    1028                 :            :         }
    1029                 :            : 
    1030                 :          0 :         vhost_user.vsockets[vhost_user.vsocket_cnt++] = vsocket;
    1031                 :            : 
    1032                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
    1033                 :          0 :         return 0;
    1034                 :            : 
    1035                 :          0 : out_mutex:
    1036         [ #  # ]:          0 :         if (pthread_mutex_destroy(&vsocket->conn_mutex)) {
    1037                 :          0 :                 VHOST_CONFIG_LOG(path, ERR, "failed to destroy connection mutex");
    1038                 :            :         }
    1039                 :          0 : out:
    1040                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
    1041                 :          0 :         return -1;
    1042                 :            : }
    1043                 :            : 
    1044                 :            : static bool
    1045                 :          0 : vhost_user_remove_reconnect(struct vhost_user_socket *vsocket)
    1046                 :            : {
    1047                 :            :         int found = false;
    1048                 :            :         struct vhost_user_reconnect *reconn, *next;
    1049                 :            : 
    1050                 :          0 :         pthread_mutex_lock(&reconn_list.mutex);
    1051                 :            : 
    1052                 :          0 :         for (reconn = TAILQ_FIRST(&reconn_list.head);
    1053         [ #  # ]:          0 :              reconn != NULL; reconn = next) {
    1054                 :          0 :                 next = TAILQ_NEXT(reconn, next);
    1055                 :            : 
    1056         [ #  # ]:          0 :                 if (reconn->vsocket == vsocket) {
    1057         [ #  # ]:          0 :                         TAILQ_REMOVE(&reconn_list.head, reconn, next);
    1058                 :          0 :                         close(reconn->fd);
    1059                 :          0 :                         free(reconn);
    1060                 :            :                         found = true;
    1061                 :          0 :                         break;
    1062                 :            :                 }
    1063                 :            :         }
    1064                 :          0 :         pthread_mutex_unlock(&reconn_list.mutex);
    1065                 :          0 :         return found;
    1066                 :            : }
    1067                 :            : 
    1068                 :            : /**
    1069                 :            :  * Unregister the specified vhost socket
    1070                 :            :  */
    1071                 :            : RTE_EXPORT_SYMBOL(rte_vhost_driver_unregister)
    1072                 :            : int
    1073                 :          0 : rte_vhost_driver_unregister(const char *path)
    1074                 :            : {
    1075                 :            :         int i;
    1076                 :            :         int count;
    1077                 :            :         struct vhost_user_connection *conn, *next;
    1078                 :            : 
    1079         [ #  # ]:          0 :         if (path == NULL)
    1080                 :            :                 return -1;
    1081                 :            : 
    1082                 :          0 : again:
    1083                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
    1084                 :            : 
    1085         [ #  # ]:          0 :         for (i = 0; i < vhost_user.vsocket_cnt; i++) {
    1086                 :          0 :                 struct vhost_user_socket *vsocket = vhost_user.vsockets[i];
    1087         [ #  # ]:          0 :                 if (strcmp(vsocket->path, path))
    1088                 :            :                         continue;
    1089                 :            : 
    1090         [ #  # ]:          0 :                 if (vsocket->is_vduse) {
    1091                 :          0 :                         vduse_device_destroy(path);
    1092         [ #  # ]:          0 :                 } else if (vsocket->is_server) {
    1093                 :            :                         /*
    1094                 :            :                          * If r/wcb is executing, release vhost_user's
    1095                 :            :                          * mutex lock, and try again since the r/wcb
    1096                 :            :                          * may use the mutex lock.
    1097                 :            :                          */
    1098         [ #  # ]:          0 :                         if (fdset_try_del(vhost_user.fdset, vsocket->socket_fd) == -1) {
    1099                 :          0 :                                 pthread_mutex_unlock(&vhost_user.mutex);
    1100                 :          0 :                                 goto again;
    1101                 :            :                         }
    1102         [ #  # ]:          0 :                 } else if (vsocket->reconnect) {
    1103                 :          0 :                         vhost_user_remove_reconnect(vsocket);
    1104                 :            :                 }
    1105                 :            : 
    1106                 :          0 :                 pthread_mutex_lock(&vsocket->conn_mutex);
    1107                 :          0 :                 for (conn = TAILQ_FIRST(&vsocket->conn_list);
    1108         [ #  # ]:          0 :                          conn != NULL;
    1109                 :            :                          conn = next) {
    1110                 :          0 :                         next = TAILQ_NEXT(conn, next);
    1111                 :            : 
    1112                 :            :                         /*
    1113                 :            :                          * If r/wcb is executing, release vsocket's
    1114                 :            :                          * conn_mutex and vhost_user's mutex locks, and
    1115                 :            :                          * try again since the r/wcb may use the
    1116                 :            :                          * conn_mutex and mutex locks.
    1117                 :            :                          */
    1118         [ #  # ]:          0 :                         if (fdset_try_del(vhost_user.fdset,
    1119                 :            :                                           conn->connfd) == -1) {
    1120                 :          0 :                                 pthread_mutex_unlock(&vsocket->conn_mutex);
    1121                 :          0 :                                 pthread_mutex_unlock(&vhost_user.mutex);
    1122                 :          0 :                                 goto again;
    1123                 :            :                         }
    1124                 :            : 
    1125                 :          0 :                         VHOST_CONFIG_LOG(path, INFO, "free connfd %d", conn->connfd);
    1126                 :          0 :                         close(conn->connfd);
    1127                 :          0 :                         vhost_destroy_device(conn->vid);
    1128         [ #  # ]:          0 :                         TAILQ_REMOVE(&vsocket->conn_list, conn, next);
    1129                 :          0 :                         free(conn);
    1130                 :            :                 }
    1131                 :          0 :                 pthread_mutex_unlock(&vsocket->conn_mutex);
    1132                 :            : 
    1133         [ #  # ]:          0 :                 if (vsocket->is_server) {
    1134                 :          0 :                         close(vsocket->socket_fd);
    1135                 :          0 :                         unlink(path);
    1136                 :            :                 }
    1137                 :            : 
    1138                 :          0 :                 pthread_mutex_destroy(&vsocket->conn_mutex);
    1139                 :          0 :                 vhost_user_socket_mem_free(vsocket);
    1140                 :            : 
    1141                 :          0 :                 count = --vhost_user.vsocket_cnt;
    1142                 :          0 :                 vhost_user.vsockets[i] = vhost_user.vsockets[count];
    1143                 :          0 :                 vhost_user.vsockets[count] = NULL;
    1144                 :          0 :                 pthread_mutex_unlock(&vhost_user.mutex);
    1145                 :          0 :                 return 0;
    1146                 :            :         }
    1147                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
    1148                 :            : 
    1149                 :          0 :         return -1;
    1150                 :            : }
    1151                 :            : 
    1152                 :            : /*
    1153                 :            :  * Register ops so that we can add/remove device to data core.
    1154                 :            :  */
    1155                 :            : RTE_EXPORT_SYMBOL(rte_vhost_driver_callback_register)
    1156                 :            : int
    1157                 :          0 : rte_vhost_driver_callback_register(const char *path,
    1158                 :            :         struct rte_vhost_device_ops const * const ops)
    1159                 :            : {
    1160                 :            :         struct vhost_user_socket *vsocket;
    1161                 :            : 
    1162                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
    1163                 :          0 :         vsocket = find_vhost_user_socket(path);
    1164         [ #  # ]:          0 :         if (vsocket)
    1165                 :          0 :                 vsocket->notify_ops = ops;
    1166                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
    1167                 :            : 
    1168         [ #  # ]:          0 :         return vsocket ? 0 : -1;
    1169                 :            : }
    1170                 :            : 
    1171                 :            : struct rte_vhost_device_ops const *
    1172                 :          0 : vhost_driver_callback_get(const char *path)
    1173                 :            : {
    1174                 :            :         struct vhost_user_socket *vsocket;
    1175                 :            : 
    1176                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
    1177                 :          0 :         vsocket = find_vhost_user_socket(path);
    1178                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
    1179                 :            : 
    1180         [ #  # ]:          0 :         return vsocket ? vsocket->notify_ops : NULL;
    1181                 :            : }
    1182                 :            : 
    1183                 :            : RTE_EXPORT_SYMBOL(rte_vhost_driver_start)
    1184                 :            : int
    1185                 :          0 : rte_vhost_driver_start(const char *path)
    1186                 :            : {
    1187                 :            :         struct vhost_user_socket *vsocket;
    1188                 :            : 
    1189                 :          0 :         pthread_mutex_lock(&vhost_user.mutex);
    1190                 :          0 :         vsocket = find_vhost_user_socket(path);
    1191                 :          0 :         pthread_mutex_unlock(&vhost_user.mutex);
    1192                 :            : 
    1193         [ #  # ]:          0 :         if (!vsocket)
    1194                 :            :                 return -1;
    1195                 :            : 
    1196         [ #  # ]:          0 :         if (vsocket->is_vduse)
    1197                 :          0 :                 return vduse_device_create(path, vsocket->net_compliant_ol_flags);
    1198                 :            : 
    1199         [ #  # ]:          0 :         if (vhost_user.fdset == NULL) {
    1200                 :          0 :                 vhost_user.fdset = fdset_init("vhost-evt");
    1201         [ #  # ]:          0 :                 if (vhost_user.fdset == NULL) {
    1202                 :          0 :                         VHOST_CONFIG_LOG(path, ERR, "failed to init Vhost-user fdset");
    1203                 :          0 :                         return -1;
    1204                 :            :                 }
    1205                 :            :         }
    1206                 :            : 
    1207         [ #  # ]:          0 :         if (vsocket->is_server)
    1208                 :          0 :                 return vhost_user_start_server(vsocket);
    1209                 :            :         else
    1210                 :          0 :                 return vhost_user_start_client(vsocket);
    1211                 :            : }

Generated by: LCOV version 1.14