LCOV - code coverage report
Current view: top level - lib/vhost - socket.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 525 0.0 %
Date: 2024-12-01 18:57:19 Functions: 0 31 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 270 0.0 %

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

Generated by: LCOV version 1.14