LCOV - code coverage report
Current view: top level - spdk/module/sock/posix - posix.c (source / functions) Hit Total Coverage
Test: Combined Lines: 771 1059 72.8 %
Date: 2024-07-15 19:24:52 Functions: 59 63 93.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 488 759 64.3 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2018 Intel Corporation. All rights reserved.
       3                 :            :  *   Copyright (c) 2020, 2021 Mellanox Technologies LTD. All rights reserved.
       4                 :            :  *   Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
       5                 :            :  */
       6                 :            : 
       7                 :            : #include "spdk/stdinc.h"
       8                 :            : 
       9                 :            : #if defined(__FreeBSD__)
      10                 :            : #include <sys/event.h>
      11                 :            : #define SPDK_KEVENT
      12                 :            : #else
      13                 :            : #include <sys/epoll.h>
      14                 :            : #define SPDK_EPOLL
      15                 :            : #endif
      16                 :            : 
      17                 :            : #if defined(__linux__)
      18                 :            : #include <linux/errqueue.h>
      19                 :            : #endif
      20                 :            : 
      21                 :            : #include "spdk/env.h"
      22                 :            : #include "spdk/log.h"
      23                 :            : #include "spdk/pipe.h"
      24                 :            : #include "spdk/sock.h"
      25                 :            : #include "spdk/util.h"
      26                 :            : #include "spdk/string.h"
      27                 :            : #include "spdk_internal/sock.h"
      28                 :            : #include "../sock_kernel.h"
      29                 :            : 
      30                 :            : #include "openssl/crypto.h"
      31                 :            : #include "openssl/err.h"
      32                 :            : #include "openssl/ssl.h"
      33                 :            : 
      34                 :            : #define MAX_TMPBUF 1024
      35                 :            : #define PORTNUMLEN 32
      36                 :            : 
      37                 :            : #if defined(SO_ZEROCOPY) && defined(MSG_ZEROCOPY)
      38                 :            : #define SPDK_ZEROCOPY
      39                 :            : #endif
      40                 :            : 
      41                 :            : struct spdk_posix_sock {
      42                 :            :         struct spdk_sock        base;
      43                 :            :         int                     fd;
      44                 :            : 
      45                 :            :         uint32_t                sendmsg_idx;
      46                 :            : 
      47                 :            :         struct spdk_pipe        *recv_pipe;
      48                 :            :         int                     recv_buf_sz;
      49                 :            :         bool                    pipe_has_data;
      50                 :            :         bool                    socket_has_data;
      51                 :            :         bool                    zcopy;
      52                 :            : 
      53                 :            :         int                     placement_id;
      54                 :            : 
      55                 :            :         SSL_CTX                 *ctx;
      56                 :            :         SSL                     *ssl;
      57                 :            : 
      58                 :            :         TAILQ_ENTRY(spdk_posix_sock)    link;
      59                 :            : };
      60                 :            : 
      61                 :            : TAILQ_HEAD(spdk_has_data_list, spdk_posix_sock);
      62                 :            : 
      63                 :            : struct spdk_posix_sock_group_impl {
      64                 :            :         struct spdk_sock_group_impl     base;
      65                 :            :         int                             fd;
      66                 :            :         struct spdk_interrupt           *intr;
      67                 :            :         struct spdk_has_data_list       socks_with_data;
      68                 :            :         int                             placement_id;
      69                 :            :         struct spdk_pipe_group          *pipe_group;
      70                 :            : };
      71                 :            : 
      72                 :            : static struct spdk_sock_impl_opts g_posix_impl_opts = {
      73                 :            :         .recv_buf_size = DEFAULT_SO_RCVBUF_SIZE,
      74                 :            :         .send_buf_size = DEFAULT_SO_SNDBUF_SIZE,
      75                 :            :         .enable_recv_pipe = true,
      76                 :            :         .enable_quickack = false,
      77                 :            :         .enable_placement_id = PLACEMENT_NONE,
      78                 :            :         .enable_zerocopy_send_server = true,
      79                 :            :         .enable_zerocopy_send_client = false,
      80                 :            :         .zerocopy_threshold = 0,
      81                 :            :         .tls_version = 0,
      82                 :            :         .enable_ktls = false,
      83                 :            :         .psk_key = NULL,
      84                 :            :         .psk_key_size = 0,
      85                 :            :         .psk_identity = NULL,
      86                 :            :         .get_key = NULL,
      87                 :            :         .get_key_ctx = NULL,
      88                 :            :         .tls_cipher_suites = NULL
      89                 :            : };
      90                 :            : 
      91                 :            : static struct spdk_sock_impl_opts g_ssl_impl_opts = {
      92                 :            :         .recv_buf_size = MIN_SO_RCVBUF_SIZE,
      93                 :            :         .send_buf_size = MIN_SO_SNDBUF_SIZE,
      94                 :            :         .enable_recv_pipe = true,
      95                 :            :         .enable_quickack = false,
      96                 :            :         .enable_placement_id = PLACEMENT_NONE,
      97                 :            :         .enable_zerocopy_send_server = true,
      98                 :            :         .enable_zerocopy_send_client = false,
      99                 :            :         .zerocopy_threshold = 0,
     100                 :            :         .tls_version = 0,
     101                 :            :         .enable_ktls = false,
     102                 :            :         .psk_key = NULL,
     103                 :            :         .psk_identity = NULL
     104                 :            : };
     105                 :            : 
     106                 :            : static struct spdk_sock_map g_map = {
     107                 :            :         .entries = STAILQ_HEAD_INITIALIZER(g_map.entries),
     108                 :            :         .mtx = PTHREAD_MUTEX_INITIALIZER
     109                 :            : };
     110                 :            : 
     111                 :            : __attribute((destructor)) static void
     112                 :       2560 : posix_sock_map_cleanup(void)
     113                 :            : {
     114                 :       2560 :         spdk_sock_map_cleanup(&g_map);
     115                 :       2560 : }
     116                 :            : 
     117                 :            : #define __posix_sock(sock) (struct spdk_posix_sock *)sock
     118                 :            : #define __posix_group_impl(group) (struct spdk_posix_sock_group_impl *)group
     119                 :            : 
     120                 :            : static void
     121                 :        932 : posix_sock_copy_impl_opts(struct spdk_sock_impl_opts *dest, const struct spdk_sock_impl_opts *src,
     122                 :            :                           size_t len)
     123                 :            : {
     124                 :            : #define FIELD_OK(field) \
     125                 :            :         offsetof(struct spdk_sock_impl_opts, field) + sizeof(src->field) <= len
     126                 :            : 
     127                 :            : #define SET_FIELD(field) \
     128                 :            :         if (FIELD_OK(field)) { \
     129                 :            :                 dest->field = src->field; \
     130                 :            :         }
     131                 :            : 
     132         [ +  + ]:        932 :         SET_FIELD(recv_buf_size);
     133         [ +  + ]:        932 :         SET_FIELD(send_buf_size);
     134   [ +  +  -  + ]:        932 :         SET_FIELD(enable_recv_pipe);
     135   [ +  +  -  + ]:        932 :         SET_FIELD(enable_zerocopy_send);
     136   [ +  +  -  + ]:        932 :         SET_FIELD(enable_quickack);
     137         [ +  + ]:        932 :         SET_FIELD(enable_placement_id);
     138   [ +  +  -  + ]:        932 :         SET_FIELD(enable_zerocopy_send_server);
     139   [ +  +  -  + ]:        932 :         SET_FIELD(enable_zerocopy_send_client);
     140         [ +  + ]:        932 :         SET_FIELD(zerocopy_threshold);
     141         [ +  + ]:        932 :         SET_FIELD(tls_version);
     142   [ +  +  -  + ]:        932 :         SET_FIELD(enable_ktls);
     143         [ +  + ]:        932 :         SET_FIELD(psk_key);
     144         [ +  + ]:        932 :         SET_FIELD(psk_key_size);
     145         [ +  + ]:        932 :         SET_FIELD(psk_identity);
     146         [ +  + ]:        932 :         SET_FIELD(get_key);
     147         [ +  + ]:        932 :         SET_FIELD(get_key_ctx);
     148         [ +  + ]:        932 :         SET_FIELD(tls_cipher_suites);
     149                 :            : 
     150                 :            : #undef SET_FIELD
     151                 :            : #undef FIELD_OK
     152                 :        932 : }
     153                 :            : 
     154                 :            : static int
     155                 :        596 : _sock_impl_get_opts(struct spdk_sock_impl_opts *opts, struct spdk_sock_impl_opts *impl_opts,
     156                 :            :                     size_t *len)
     157                 :            : {
     158   [ +  -  -  + ]:        596 :         if (!opts || !len) {
     159                 :          0 :                 errno = EINVAL;
     160                 :          0 :                 return -1;
     161                 :            :         }
     162                 :            : 
     163         [ -  + ]:        596 :         assert(sizeof(*opts) >= *len);
     164         [ -  + ]:        596 :         memset(opts, 0, *len);
     165                 :            : 
     166                 :        596 :         posix_sock_copy_impl_opts(opts, impl_opts, *len);
     167                 :        596 :         *len = spdk_min(*len, sizeof(*impl_opts));
     168                 :            : 
     169                 :        596 :         return 0;
     170                 :            : }
     171                 :            : 
     172                 :            : static int
     173                 :        209 : posix_sock_impl_get_opts(struct spdk_sock_impl_opts *opts, size_t *len)
     174                 :            : {
     175                 :        209 :         return _sock_impl_get_opts(opts, &g_posix_impl_opts, len);
     176                 :            : }
     177                 :            : 
     178                 :            : static int
     179                 :        387 : ssl_sock_impl_get_opts(struct spdk_sock_impl_opts *opts, size_t *len)
     180                 :            : {
     181                 :        387 :         return _sock_impl_get_opts(opts, &g_ssl_impl_opts, len);
     182                 :            : }
     183                 :            : 
     184                 :            : static int
     185                 :        137 : _sock_impl_set_opts(const struct spdk_sock_impl_opts *opts, struct spdk_sock_impl_opts *impl_opts,
     186                 :            :                     size_t len)
     187                 :            : {
     188         [ -  + ]:        137 :         if (!opts) {
     189                 :          0 :                 errno = EINVAL;
     190                 :          0 :                 return -1;
     191                 :            :         }
     192                 :            : 
     193         [ -  + ]:        137 :         assert(sizeof(*opts) >= len);
     194                 :        137 :         posix_sock_copy_impl_opts(impl_opts, opts, len);
     195                 :            : 
     196                 :        137 :         return 0;
     197                 :            : }
     198                 :            : 
     199                 :            : static int
     200                 :         65 : posix_sock_impl_set_opts(const struct spdk_sock_impl_opts *opts, size_t len)
     201                 :            : {
     202                 :         65 :         return _sock_impl_set_opts(opts, &g_posix_impl_opts, len);
     203                 :            : }
     204                 :            : 
     205                 :            : static int
     206                 :         72 : ssl_sock_impl_set_opts(const struct spdk_sock_impl_opts *opts, size_t len)
     207                 :            : {
     208                 :         72 :         return _sock_impl_set_opts(opts, &g_ssl_impl_opts, len);
     209                 :            : }
     210                 :            : 
     211                 :            : static void
     212                 :      18940 : _opts_get_impl_opts(const struct spdk_sock_opts *opts, struct spdk_sock_impl_opts *dest,
     213                 :            :                     const struct spdk_sock_impl_opts *default_impl)
     214                 :            : {
     215                 :            :         /* Copy the default impl_opts first to cover cases when user's impl_opts is smaller */
     216   [ -  +  -  + ]:      18940 :         memcpy(dest, default_impl, sizeof(*dest));
     217                 :            : 
     218         [ +  + ]:      18940 :         if (opts->impl_opts != NULL) {
     219         [ -  + ]:        199 :                 assert(sizeof(*dest) >= opts->impl_opts_size);
     220                 :        199 :                 posix_sock_copy_impl_opts(dest, opts->impl_opts, opts->impl_opts_size);
     221                 :            :         }
     222                 :      18940 : }
     223                 :            : 
     224                 :            : static int
     225                 :      11567 : posix_sock_getaddr(struct spdk_sock *_sock, char *saddr, int slen, uint16_t *sport,
     226                 :            :                    char *caddr, int clen, uint16_t *cport)
     227                 :            : {
     228                 :      11567 :         struct spdk_posix_sock *sock = __posix_sock(_sock);
     229                 :        172 :         struct sockaddr_storage sa;
     230                 :        172 :         socklen_t salen;
     231                 :            :         int rc;
     232                 :            : 
     233         [ -  + ]:      11567 :         assert(sock != NULL);
     234                 :            : 
     235                 :      11567 :         memset(&sa, 0, sizeof sa);
     236                 :      11567 :         salen = sizeof sa;
     237                 :      11567 :         rc = getsockname(sock->fd, (struct sockaddr *) &sa, &salen);
     238         [ -  + ]:      11567 :         if (rc != 0) {
     239                 :          0 :                 SPDK_ERRLOG("getsockname() failed (errno=%d)\n", errno);
     240                 :          0 :                 return -1;
     241                 :            :         }
     242                 :            : 
     243      [ -  +  - ]:      11567 :         switch (sa.ss_family) {
     244                 :          0 :         case AF_UNIX:
     245                 :            :                 /* Acceptable connection types that don't have IPs */
     246                 :          0 :                 return 0;
     247                 :      11567 :         case AF_INET:
     248                 :            :         case AF_INET6:
     249                 :            :                 /* Code below will get IP addresses */
     250                 :      11567 :                 break;
     251                 :          0 :         default:
     252                 :            :                 /* Unsupported socket family */
     253                 :          0 :                 return -1;
     254                 :            :         }
     255                 :            : 
     256                 :      11567 :         rc = get_addr_str((struct sockaddr *)&sa, saddr, slen);
     257         [ -  + ]:      11567 :         if (rc != 0) {
     258                 :          0 :                 SPDK_ERRLOG("getnameinfo() failed (errno=%d)\n", errno);
     259                 :          0 :                 return -1;
     260                 :            :         }
     261                 :            : 
     262         [ +  + ]:      11567 :         if (sport) {
     263         [ +  - ]:      11286 :                 if (sa.ss_family == AF_INET) {
     264                 :      11286 :                         *sport = ntohs(((struct sockaddr_in *) &sa)->sin_port);
     265         [ #  # ]:          0 :                 } else if (sa.ss_family == AF_INET6) {
     266                 :          0 :                         *sport = ntohs(((struct sockaddr_in6 *) &sa)->sin6_port);
     267                 :            :                 }
     268                 :            :         }
     269                 :            : 
     270                 :      11567 :         memset(&sa, 0, sizeof sa);
     271                 :      11567 :         salen = sizeof sa;
     272                 :      11567 :         rc = getpeername(sock->fd, (struct sockaddr *) &sa, &salen);
     273         [ -  + ]:      11567 :         if (rc != 0) {
     274                 :          0 :                 SPDK_ERRLOG("getpeername() failed (errno=%d)\n", errno);
     275                 :          0 :                 return -1;
     276                 :            :         }
     277                 :            : 
     278                 :      11567 :         rc = get_addr_str((struct sockaddr *)&sa, caddr, clen);
     279         [ -  + ]:      11567 :         if (rc != 0) {
     280                 :          0 :                 SPDK_ERRLOG("getnameinfo() failed (errno=%d)\n", errno);
     281                 :          0 :                 return -1;
     282                 :            :         }
     283                 :            : 
     284         [ +  + ]:      11567 :         if (cport) {
     285         [ +  - ]:      11286 :                 if (sa.ss_family == AF_INET) {
     286                 :      11286 :                         *cport = ntohs(((struct sockaddr_in *) &sa)->sin_port);
     287         [ #  # ]:          0 :                 } else if (sa.ss_family == AF_INET6) {
     288                 :          0 :                         *cport = ntohs(((struct sockaddr_in6 *) &sa)->sin6_port);
     289                 :            :                 }
     290                 :            :         }
     291                 :            : 
     292                 :      11567 :         return 0;
     293                 :            : }
     294                 :            : 
     295                 :            : enum posix_sock_create_type {
     296                 :            :         SPDK_SOCK_CREATE_LISTEN,
     297                 :            :         SPDK_SOCK_CREATE_CONNECT,
     298                 :            : };
     299                 :            : 
     300                 :            : static int
     301                 :      11553 : posix_sock_alloc_pipe(struct spdk_posix_sock *sock, int sz)
     302                 :            : {
     303                 :        181 :         uint8_t *new_buf, *old_buf;
     304                 :            :         struct spdk_pipe *new_pipe;
     305                 :        181 :         struct iovec siov[2];
     306                 :        181 :         struct iovec diov[2];
     307                 :            :         int sbytes;
     308                 :            :         ssize_t bytes;
     309                 :            :         int rc;
     310                 :            : 
     311         [ +  + ]:      11553 :         if (sock->recv_buf_sz == sz) {
     312                 :       2001 :                 return 0;
     313                 :            :         }
     314                 :            : 
     315                 :            :         /* If the new size is 0, just free the pipe */
     316         [ -  + ]:       9552 :         if (sz == 0) {
     317                 :          0 :                 old_buf = spdk_pipe_destroy(sock->recv_pipe);
     318                 :          0 :                 free(old_buf);
     319                 :          0 :                 sock->recv_pipe = NULL;
     320                 :          0 :                 return 0;
     321         [ -  + ]:       9552 :         } else if (sz < MIN_SOCK_PIPE_SIZE) {
     322                 :          0 :                 SPDK_ERRLOG("The size of the pipe must be larger than %d\n", MIN_SOCK_PIPE_SIZE);
     323                 :          0 :                 return -1;
     324                 :            :         }
     325                 :            : 
     326                 :            :         /* Round up to next 64 byte multiple */
     327         [ -  + ]:       9552 :         rc = posix_memalign((void **)&new_buf, 64, sz);
     328         [ -  + ]:       9552 :         if (rc != 0) {
     329                 :          0 :                 SPDK_ERRLOG("socket recv buf allocation failed\n");
     330                 :          0 :                 return -ENOMEM;
     331                 :            :         }
     332         [ -  + ]:       9552 :         memset(new_buf, 0, sz);
     333                 :            : 
     334                 :       9552 :         new_pipe = spdk_pipe_create(new_buf, sz);
     335         [ -  + ]:       9552 :         if (new_pipe == NULL) {
     336                 :          0 :                 SPDK_ERRLOG("socket pipe allocation failed\n");
     337                 :          0 :                 free(new_buf);
     338                 :          0 :                 return -ENOMEM;
     339                 :            :         }
     340                 :            : 
     341         [ -  + ]:       9552 :         if (sock->recv_pipe != NULL) {
     342                 :            :                 /* Pull all of the data out of the old pipe */
     343                 :          0 :                 sbytes = spdk_pipe_reader_get_buffer(sock->recv_pipe, sock->recv_buf_sz, siov);
     344         [ #  # ]:          0 :                 if (sbytes > sz) {
     345                 :            :                         /* Too much data to fit into the new pipe size */
     346                 :          0 :                         old_buf = spdk_pipe_destroy(new_pipe);
     347                 :          0 :                         free(old_buf);
     348                 :          0 :                         return -EINVAL;
     349                 :            :                 }
     350                 :            : 
     351                 :          0 :                 sbytes = spdk_pipe_writer_get_buffer(new_pipe, sz, diov);
     352         [ #  # ]:          0 :                 assert(sbytes == sz);
     353                 :            : 
     354                 :          0 :                 bytes = spdk_iovcpy(siov, 2, diov, 2);
     355                 :          0 :                 spdk_pipe_writer_advance(new_pipe, bytes);
     356                 :            : 
     357                 :          0 :                 old_buf = spdk_pipe_destroy(sock->recv_pipe);
     358                 :          0 :                 free(old_buf);
     359                 :            :         }
     360                 :            : 
     361                 :       9552 :         sock->recv_buf_sz = sz;
     362                 :       9552 :         sock->recv_pipe = new_pipe;
     363                 :            : 
     364         [ +  + ]:       9552 :         if (sock->base.group_impl) {
     365                 :            :                 struct spdk_posix_sock_group_impl *group;
     366                 :            : 
     367                 :       6519 :                 group = __posix_group_impl(sock->base.group_impl);
     368                 :       6519 :                 spdk_pipe_group_add(group->pipe_group, sock->recv_pipe);
     369                 :            :         }
     370                 :            : 
     371                 :       9552 :         return 0;
     372                 :            : }
     373                 :            : 
     374                 :            : static int
     375                 :      11553 : posix_sock_set_recvbuf(struct spdk_sock *_sock, int sz)
     376                 :            : {
     377                 :      11553 :         struct spdk_posix_sock *sock = __posix_sock(_sock);
     378                 :            :         int min_size;
     379                 :            :         int rc;
     380                 :            : 
     381         [ -  + ]:      11553 :         assert(sock != NULL);
     382                 :            : 
     383   [ +  +  +  - ]:      11553 :         if (_sock->impl_opts.enable_recv_pipe) {
     384                 :      11553 :                 rc = posix_sock_alloc_pipe(sock, sz);
     385         [ -  + ]:      11553 :                 if (rc) {
     386                 :          0 :                         return rc;
     387                 :            :                 }
     388                 :            :         }
     389                 :            : 
     390                 :            :         /* Set kernel buffer size to be at least MIN_SO_RCVBUF_SIZE and
     391                 :            :          * _sock->impl_opts.recv_buf_size. */
     392                 :      11553 :         min_size = spdk_max(MIN_SO_RCVBUF_SIZE, _sock->impl_opts.recv_buf_size);
     393                 :            : 
     394         [ +  + ]:      11553 :         if (sz < min_size) {
     395                 :      11323 :                 sz = min_size;
     396                 :            :         }
     397                 :            : 
     398                 :      11553 :         rc = setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz));
     399         [ -  + ]:      11553 :         if (rc < 0) {
     400                 :          0 :                 return rc;
     401                 :            :         }
     402                 :            : 
     403                 :      11553 :         _sock->impl_opts.recv_buf_size = sz;
     404                 :            : 
     405                 :      11553 :         return 0;
     406                 :            : }
     407                 :            : 
     408                 :            : static int
     409                 :          3 : posix_sock_set_sendbuf(struct spdk_sock *_sock, int sz)
     410                 :            : {
     411                 :          3 :         struct spdk_posix_sock *sock = __posix_sock(_sock);
     412                 :            :         int min_size;
     413                 :            :         int rc;
     414                 :            : 
     415         [ -  + ]:          3 :         assert(sock != NULL);
     416                 :            : 
     417                 :            :         /* Set kernel buffer size to be at least MIN_SO_SNDBUF_SIZE and
     418                 :            :          * _sock->impl_opts.send_buf_size. */
     419                 :          3 :         min_size = spdk_max(MIN_SO_SNDBUF_SIZE, _sock->impl_opts.send_buf_size);
     420                 :            : 
     421         [ +  - ]:          3 :         if (sz < min_size) {
     422                 :          3 :                 sz = min_size;
     423                 :            :         }
     424                 :            : 
     425                 :          3 :         rc = setsockopt(sock->fd, SOL_SOCKET, SO_SNDBUF, &sz, sizeof(sz));
     426         [ -  + ]:          3 :         if (rc < 0) {
     427                 :          0 :                 return rc;
     428                 :            :         }
     429                 :            : 
     430                 :          3 :         _sock->impl_opts.send_buf_size = sz;
     431                 :            : 
     432                 :          3 :         return 0;
     433                 :            : }
     434                 :            : 
     435                 :            : static void
     436                 :       9967 : posix_sock_init(struct spdk_posix_sock *sock, bool enable_zero_copy)
     437                 :            : {
     438                 :            : #if defined(SPDK_ZEROCOPY) || defined(__linux__)
     439                 :        260 :         int flag;
     440                 :            :         int rc;
     441                 :            : #endif
     442                 :            : 
     443                 :            : #if defined(SPDK_ZEROCOPY)
     444                 :       9967 :         flag = 1;
     445                 :            : 
     446         [ +  + ]:       9967 :         if (enable_zero_copy) {
     447                 :            :                 /* Try to turn on zero copy sends */
     448                 :       5875 :                 rc = setsockopt(sock->fd, SOL_SOCKET, SO_ZEROCOPY, &flag, sizeof(flag));
     449         [ +  - ]:       5875 :                 if (rc == 0) {
     450                 :       5875 :                         sock->zcopy = true;
     451                 :            :                 }
     452                 :            :         }
     453                 :            : #endif
     454                 :            : 
     455                 :            : #if defined(__linux__)
     456                 :       9967 :         flag = 1;
     457                 :            : 
     458   [ -  +  -  + ]:       9967 :         if (sock->base.impl_opts.enable_quickack) {
     459                 :          0 :                 rc = setsockopt(sock->fd, IPPROTO_TCP, TCP_QUICKACK, &flag, sizeof(flag));
     460         [ #  # ]:          0 :                 if (rc != 0) {
     461                 :          0 :                         SPDK_ERRLOG("quickack was failed to set\n");
     462                 :            :                 }
     463                 :            :         }
     464                 :            : 
     465                 :       9967 :         spdk_sock_get_placement_id(sock->fd, sock->base.impl_opts.enable_placement_id,
     466                 :            :                                    &sock->placement_id);
     467                 :            : 
     468         [ -  + ]:       9967 :         if (sock->base.impl_opts.enable_placement_id == PLACEMENT_MARK) {
     469                 :            :                 /* Save placement_id */
     470                 :          0 :                 spdk_sock_map_insert(&g_map, sock->placement_id, NULL);
     471                 :            :         }
     472                 :            : #endif
     473                 :       9967 : }
     474                 :            : 
     475                 :            : static struct spdk_posix_sock *
     476                 :       9967 : posix_sock_alloc(int fd, struct spdk_sock_impl_opts *impl_opts, bool enable_zero_copy)
     477                 :            : {
     478                 :            :         struct spdk_posix_sock *sock;
     479                 :            : 
     480                 :       9967 :         sock = calloc(1, sizeof(*sock));
     481         [ -  + ]:       9967 :         if (sock == NULL) {
     482                 :          0 :                 SPDK_ERRLOG("sock allocation failed\n");
     483                 :          0 :                 return NULL;
     484                 :            :         }
     485                 :            : 
     486                 :       9967 :         sock->fd = fd;
     487   [ -  +  -  + ]:       9967 :         memcpy(&sock->base.impl_opts, impl_opts, sizeof(*impl_opts));
     488                 :       9967 :         posix_sock_init(sock, enable_zero_copy);
     489                 :            : 
     490                 :       9967 :         return sock;
     491                 :            : }
     492                 :            : 
     493                 :            : static int
     494                 :      18940 : posix_fd_create(struct addrinfo *res, struct spdk_sock_opts *opts,
     495                 :            :                 struct spdk_sock_impl_opts *impl_opts)
     496                 :            : {
     497                 :            :         int fd;
     498                 :      18940 :         int val = 1;
     499                 :        157 :         int rc, sz;
     500                 :            : #if defined(__linux__)
     501                 :        157 :         int to;
     502                 :            : #endif
     503                 :            : 
     504                 :      18940 :         fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
     505         [ -  + ]:      18940 :         if (fd < 0) {
     506                 :            :                 /* error */
     507                 :          0 :                 return -1;
     508                 :            :         }
     509                 :            : 
     510                 :      18940 :         sz = impl_opts->recv_buf_size;
     511                 :      18940 :         rc = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz));
     512                 :            :         if (rc) {
     513                 :            :                 /* Not fatal */
     514                 :            :         }
     515                 :            : 
     516                 :      18940 :         sz = impl_opts->send_buf_size;
     517                 :      18940 :         rc = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sz, sizeof(sz));
     518                 :            :         if (rc) {
     519                 :            :                 /* Not fatal */
     520                 :            :         }
     521                 :            : 
     522                 :      18940 :         rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof val);
     523         [ -  + ]:      18940 :         if (rc != 0) {
     524                 :          0 :                 close(fd);
     525                 :            :                 /* error */
     526                 :          0 :                 return -1;
     527                 :            :         }
     528                 :      18940 :         rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof val);
     529         [ -  + ]:      18940 :         if (rc != 0) {
     530                 :          0 :                 close(fd);
     531                 :            :                 /* error */
     532                 :          0 :                 return -1;
     533                 :            :         }
     534                 :            : 
     535                 :            : #if defined(SO_PRIORITY)
     536         [ +  + ]:      18940 :         if (opts->priority) {
     537                 :          1 :                 rc = setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &opts->priority, sizeof val);
     538         [ -  + ]:          1 :                 if (rc != 0) {
     539                 :          0 :                         close(fd);
     540                 :            :                         /* error */
     541                 :          0 :                         return -1;
     542                 :            :                 }
     543                 :            :         }
     544                 :            : #endif
     545                 :            : 
     546         [ -  + ]:      18940 :         if (res->ai_family == AF_INET6) {
     547                 :          0 :                 rc = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof val);
     548         [ #  # ]:          0 :                 if (rc != 0) {
     549                 :          0 :                         close(fd);
     550                 :            :                         /* error */
     551                 :          0 :                         return -1;
     552                 :            :                 }
     553                 :            :         }
     554                 :            : 
     555         [ +  + ]:      18940 :         if (opts->ack_timeout) {
     556                 :            : #if defined(__linux__)
     557                 :         16 :                 to = opts->ack_timeout;
     558                 :         16 :                 rc = setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &to, sizeof(to));
     559         [ -  + ]:         16 :                 if (rc != 0) {
     560                 :          0 :                         close(fd);
     561                 :            :                         /* error */
     562                 :          0 :                         return -1;
     563                 :            :                 }
     564                 :            : #else
     565                 :            :                 SPDK_WARNLOG("TCP_USER_TIMEOUT is not supported.\n");
     566                 :            : #endif
     567                 :            :         }
     568                 :            : 
     569                 :      18940 :         return fd;
     570                 :            : }
     571                 :            : 
     572                 :            : static int
     573                 :        142 : posix_sock_psk_find_session_server_cb(SSL *ssl, const unsigned char *identity,
     574                 :            :                                       size_t identity_len, SSL_SESSION **sess)
     575                 :            : {
     576                 :        142 :         struct spdk_sock_impl_opts *impl_opts = SSL_get_app_data(ssl);
     577                 :        142 :         uint8_t key[SSL_MAX_MASTER_KEY_LENGTH] = {};
     578                 :            :         int keylen;
     579                 :            :         int rc, i;
     580                 :            :         STACK_OF(SSL_CIPHER) *ciphers;
     581                 :            :         const SSL_CIPHER *cipher;
     582                 :            :         const char *cipher_name;
     583                 :        142 :         const char *user_cipher = NULL;
     584                 :        142 :         bool found = false;
     585                 :            : 
     586         [ +  + ]:        142 :         if (impl_opts->get_key) {
     587                 :        130 :                 rc = impl_opts->get_key(key, sizeof(key), &user_cipher, identity, impl_opts->get_key_ctx);
     588         [ +  + ]:        130 :                 if (rc < 0) {
     589                 :          6 :                         SPDK_ERRLOG("Unable to find PSK for identity: %s\n", identity);
     590                 :          6 :                         return 0;
     591                 :            :                 }
     592                 :        124 :                 keylen = rc;
     593                 :            :         } else {
     594         [ -  + ]:         12 :                 if (impl_opts->psk_key == NULL) {
     595                 :          0 :                         SPDK_ERRLOG("PSK is not set\n");
     596                 :          0 :                         return 0;
     597                 :            :                 }
     598                 :            : 
     599   [ -  +  -  +  :         12 :                 SPDK_DEBUGLOG(sock_posix, "Length of Client's PSK ID %lu\n", strlen(impl_opts->psk_identity));
                   -  - ]
     600   [ -  +  -  +  :         12 :                 if (strcmp(impl_opts->psk_identity, identity) != 0) {
                   +  + ]
     601                 :          2 :                         SPDK_ERRLOG("Unknown Client's PSK ID\n");
     602                 :          2 :                         return 0;
     603                 :            :                 }
     604                 :         10 :                 keylen = impl_opts->psk_key_size;
     605                 :            : 
     606         [ -  + ]:         10 :                 memcpy(key, impl_opts->psk_key, keylen);
     607                 :         10 :                 user_cipher = impl_opts->tls_cipher_suites;
     608                 :            :         }
     609                 :            : 
     610         [ -  + ]:        134 :         if (user_cipher == NULL) {
     611                 :          0 :                 SPDK_ERRLOG("Cipher suite not set\n");
     612                 :          0 :                 return 0;
     613                 :            :         }
     614                 :            : 
     615                 :        134 :         *sess = SSL_SESSION_new();
     616         [ -  + ]:        134 :         if (*sess == NULL) {
     617                 :          0 :                 SPDK_ERRLOG("Unable to allocate new SSL session\n");
     618                 :          0 :                 return 0;
     619                 :            :         }
     620                 :            : 
     621                 :        134 :         ciphers = SSL_get_ciphers(ssl);
     622         [ +  - ]:        207 :         for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
     623                 :        207 :                 cipher = sk_SSL_CIPHER_value(ciphers, i);
     624                 :        207 :                 cipher_name = SSL_CIPHER_get_name(cipher);
     625                 :            : 
     626   [ -  +  -  +  :        207 :                 if (strcmp(user_cipher, cipher_name) == 0) {
                   +  + ]
     627                 :        134 :                         rc = SSL_SESSION_set_cipher(*sess, cipher);
     628         [ -  + ]:        134 :                         if (rc != 1) {
     629                 :          0 :                                 SPDK_ERRLOG("Unable to set cipher: %s\n", cipher_name);
     630                 :          0 :                                 goto err;
     631                 :            :                         }
     632                 :        134 :                         found = true;
     633                 :        134 :                         break;
     634                 :            :                 }
     635                 :            :         }
     636         [ -  + ]:        134 :         if (found == false) {
     637                 :          0 :                 SPDK_ERRLOG("No suitable cipher found\n");
     638                 :          0 :                 goto err;
     639                 :            :         }
     640                 :            : 
     641   [ -  +  -  + ]:        134 :         SPDK_DEBUGLOG(sock_posix, "Cipher selected: %s\n", cipher_name);
     642                 :            : 
     643                 :        134 :         rc = SSL_SESSION_set_protocol_version(*sess, TLS1_3_VERSION);
     644         [ -  + ]:        134 :         if (rc != 1) {
     645                 :          0 :                 SPDK_ERRLOG("Unable to set TLS version: %d\n", TLS1_3_VERSION);
     646                 :          0 :                 goto err;
     647                 :            :         }
     648                 :            : 
     649                 :        134 :         rc = SSL_SESSION_set1_master_key(*sess, key, keylen);
     650         [ -  + ]:        134 :         if (rc != 1) {
     651                 :          0 :                 SPDK_ERRLOG("Unable to set PSK for session\n");
     652                 :          0 :                 goto err;
     653                 :            :         }
     654                 :            : 
     655                 :        134 :         return 1;
     656                 :            : 
     657                 :          0 : err:
     658                 :          0 :         SSL_SESSION_free(*sess);
     659                 :          0 :         *sess = NULL;
     660                 :          0 :         return 0;
     661                 :            : }
     662                 :            : 
     663                 :            : static int
     664                 :        140 : posix_sock_psk_use_session_client_cb(SSL *ssl, const EVP_MD *md, const unsigned char **identity,
     665                 :            :                                      size_t *identity_len, SSL_SESSION **sess)
     666                 :            : {
     667                 :        140 :         struct spdk_sock_impl_opts *impl_opts = SSL_get_app_data(ssl);
     668                 :            :         int rc, i;
     669                 :            :         STACK_OF(SSL_CIPHER) *ciphers;
     670                 :            :         const SSL_CIPHER *cipher;
     671                 :            :         const char *cipher_name;
     672                 :            :         long keylen;
     673                 :        140 :         bool found = false;
     674                 :            : 
     675         [ -  + ]:        140 :         if (impl_opts->psk_key == NULL) {
     676                 :          0 :                 SPDK_ERRLOG("PSK is not set\n");
     677                 :          0 :                 return 0;
     678                 :            :         }
     679         [ -  + ]:        140 :         if (impl_opts->psk_key_size > SSL_MAX_MASTER_KEY_LENGTH) {
     680                 :          0 :                 SPDK_ERRLOG("PSK too long\n");
     681                 :          0 :                 return 0;
     682                 :            :         }
     683                 :        140 :         keylen = impl_opts->psk_key_size;
     684                 :            : 
     685         [ -  + ]:        140 :         if (impl_opts->tls_cipher_suites == NULL) {
     686                 :          0 :                 SPDK_ERRLOG("Cipher suite not set\n");
     687                 :          0 :                 return 0;
     688                 :            :         }
     689                 :        140 :         *sess = SSL_SESSION_new();
     690         [ -  + ]:        140 :         if (*sess == NULL) {
     691                 :          0 :                 SPDK_ERRLOG("Unable to allocate new SSL session\n");
     692                 :          0 :                 return 0;
     693                 :            :         }
     694                 :            : 
     695                 :        140 :         ciphers = SSL_get_ciphers(ssl);
     696         [ +  - ]:        140 :         for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
     697                 :        140 :                 cipher = sk_SSL_CIPHER_value(ciphers, i);
     698                 :        140 :                 cipher_name = SSL_CIPHER_get_name(cipher);
     699                 :            : 
     700   [ -  +  -  +  :        140 :                 if (strcmp(impl_opts->tls_cipher_suites, cipher_name) == 0) {
                   +  - ]
     701                 :        140 :                         rc = SSL_SESSION_set_cipher(*sess, cipher);
     702         [ -  + ]:        140 :                         if (rc != 1) {
     703                 :          0 :                                 SPDK_ERRLOG("Unable to set cipher: %s\n", cipher_name);
     704                 :          0 :                                 goto err;
     705                 :            :                         }
     706                 :        140 :                         found = true;
     707                 :        140 :                         break;
     708                 :            :                 }
     709                 :            :         }
     710         [ -  + ]:        140 :         if (found == false) {
     711                 :          0 :                 SPDK_ERRLOG("No suitable cipher found\n");
     712                 :          0 :                 goto err;
     713                 :            :         }
     714                 :            : 
     715   [ -  +  -  + ]:        140 :         SPDK_DEBUGLOG(sock_posix, "Cipher selected: %s\n", cipher_name);
     716                 :            : 
     717                 :        140 :         rc = SSL_SESSION_set_protocol_version(*sess, TLS1_3_VERSION);
     718         [ -  + ]:        140 :         if (rc != 1) {
     719                 :          0 :                 SPDK_ERRLOG("Unable to set TLS version: %d\n", TLS1_3_VERSION);
     720                 :          0 :                 goto err;
     721                 :            :         }
     722                 :            : 
     723                 :        140 :         rc = SSL_SESSION_set1_master_key(*sess, impl_opts->psk_key, keylen);
     724         [ -  + ]:        140 :         if (rc != 1) {
     725                 :          0 :                 SPDK_ERRLOG("Unable to set PSK for session\n");
     726                 :          0 :                 goto err;
     727                 :            :         }
     728                 :            : 
     729         [ -  + ]:        140 :         *identity_len = strlen(impl_opts->psk_identity);
     730                 :        140 :         *identity = impl_opts->psk_identity;
     731                 :            : 
     732                 :        140 :         return 1;
     733                 :            : 
     734                 :          0 : err:
     735                 :          0 :         SSL_SESSION_free(*sess);
     736                 :          0 :         *sess = NULL;
     737                 :          0 :         return 0;
     738                 :            : }
     739                 :            : 
     740                 :            : static SSL_CTX *
     741                 :        289 : posix_sock_create_ssl_context(const SSL_METHOD *method, struct spdk_sock_opts *opts,
     742                 :            :                               struct spdk_sock_impl_opts *impl_opts)
     743                 :            : {
     744                 :            :         SSL_CTX *ctx;
     745                 :        289 :         int tls_version = 0;
     746                 :        289 :         bool ktls_enabled = false;
     747                 :            : #ifdef SSL_OP_ENABLE_KTLS
     748                 :            :         long options;
     749                 :            : #endif
     750                 :            : 
     751                 :        289 :         SSL_library_init();
     752                 :        289 :         OpenSSL_add_all_algorithms();
     753                 :        289 :         SSL_load_error_strings();
     754                 :            :         /* Produce a SSL CTX in SSL V2 and V3 standards compliant way */
     755                 :        289 :         ctx = SSL_CTX_new(method);
     756         [ -  + ]:        289 :         if (!ctx) {
     757                 :          0 :                 SPDK_ERRLOG("SSL_CTX_new() failed, msg = %s\n", ERR_error_string(ERR_peek_last_error(), NULL));
     758                 :          0 :                 return NULL;
     759                 :            :         }
     760   [ -  +  -  + ]:        289 :         SPDK_DEBUGLOG(sock_posix, "SSL context created\n");
     761                 :            : 
     762      [ +  +  + ]:        289 :         switch (impl_opts->tls_version) {
     763                 :         22 :         case 0:
     764                 :            :                 /* auto-negotioation */
     765                 :         22 :                 break;
     766                 :        265 :         case SPDK_TLS_VERSION_1_3:
     767                 :        265 :                 tls_version = TLS1_3_VERSION;
     768                 :        265 :                 break;
     769                 :          2 :         default:
     770                 :          2 :                 SPDK_ERRLOG("Incorrect TLS version provided: %d\n", impl_opts->tls_version);
     771                 :          2 :                 goto err;
     772                 :            :         }
     773                 :            : 
     774         [ +  + ]:        287 :         if (tls_version) {
     775   [ -  +  -  + ]:        265 :                 SPDK_DEBUGLOG(sock_posix, "Hardening TLS version to '%d'='0x%X'\n", impl_opts->tls_version,
     776                 :            :                               tls_version);
     777         [ -  + ]:        265 :                 if (!SSL_CTX_set_min_proto_version(ctx, tls_version)) {
     778                 :          0 :                         SPDK_ERRLOG("Unable to set Min TLS version to '%d'='0x%X\n", impl_opts->tls_version, tls_version);
     779                 :          0 :                         goto err;
     780                 :            :                 }
     781         [ -  + ]:        265 :                 if (!SSL_CTX_set_max_proto_version(ctx, tls_version)) {
     782                 :          0 :                         SPDK_ERRLOG("Unable to set Max TLS version to '%d'='0x%X\n", impl_opts->tls_version, tls_version);
     783                 :          0 :                         goto err;
     784                 :            :                 }
     785                 :            :         }
     786   [ -  +  -  + ]:        287 :         if (impl_opts->enable_ktls) {
     787   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(sock_posix, "Enabling kTLS offload\n");
     788                 :            : #ifdef SSL_OP_ENABLE_KTLS
     789                 :          0 :                 options = SSL_CTX_set_options(ctx, SSL_OP_ENABLE_KTLS);
     790                 :          0 :                 ktls_enabled = options & SSL_OP_ENABLE_KTLS;
     791                 :            : #else
     792                 :          0 :                 ktls_enabled = false;
     793                 :            : #endif
     794         [ #  # ]:          0 :                 if (!ktls_enabled) {
     795                 :          0 :                         SPDK_ERRLOG("Unable to set kTLS offload via SSL_CTX_set_options(). Configure openssl with 'enable-ktls'\n");
     796                 :          0 :                         goto err;
     797                 :            :                 }
     798                 :            :         }
     799                 :            : 
     800                 :            :         /* SSL_CTX_set_ciphersuites() return 1 if the requested
     801                 :            :          * cipher suite list was configured, and 0 otherwise. */
     802   [ +  -  -  + ]:        574 :         if (impl_opts->tls_cipher_suites != NULL &&
     803                 :        287 :             SSL_CTX_set_ciphersuites(ctx, impl_opts->tls_cipher_suites) != 1) {
     804                 :          0 :                 SPDK_ERRLOG("Unable to set TLS cipher suites for SSL'\n");
     805                 :          0 :                 goto err;
     806                 :            :         }
     807                 :            : 
     808                 :        287 :         return ctx;
     809                 :            : 
     810                 :          2 : err:
     811                 :          2 :         SSL_CTX_free(ctx);
     812                 :          2 :         return NULL;
     813                 :            : }
     814                 :            : 
     815                 :            : static SSL *
     816                 :        140 : ssl_sock_setup_connect(SSL_CTX *ctx, int fd)
     817                 :            : {
     818                 :            :         SSL *ssl;
     819                 :            : 
     820                 :        140 :         ssl = SSL_new(ctx);
     821         [ -  + ]:        140 :         if (!ssl) {
     822                 :          0 :                 SPDK_ERRLOG("SSL_new() failed, msg = %s\n", ERR_error_string(ERR_peek_last_error(), NULL));
     823                 :          0 :                 return NULL;
     824                 :            :         }
     825                 :        140 :         SSL_set_fd(ssl, fd);
     826                 :        140 :         SSL_set_connect_state(ssl);
     827                 :        140 :         SSL_set_psk_use_session_callback(ssl, posix_sock_psk_use_session_client_cb);
     828   [ -  +  -  + ]:        140 :         SPDK_DEBUGLOG(sock_posix, "SSL object creation finished: %p\n", ssl);
     829   [ -  +  -  + ]:        140 :         SPDK_DEBUGLOG(sock_posix, "%s = SSL_state_string_long(%p)\n", SSL_state_string_long(ssl), ssl);
     830   [ -  +  -  + ]:        140 :         SPDK_DEBUGLOG(sock_posix, "%s = SSL_state_string_long(%p)\n", SSL_state_string_long(ssl), ssl);
     831   [ -  +  -  + ]:        140 :         SPDK_DEBUGLOG(sock_posix, "Negotiated Cipher suite:%s\n",
     832                 :            :                       SSL_CIPHER_get_name(SSL_get_current_cipher(ssl)));
     833                 :        140 :         return ssl;
     834                 :            : }
     835                 :            : 
     836                 :            : static SSL *
     837                 :        147 : ssl_sock_setup_accept(SSL_CTX *ctx, int fd)
     838                 :            : {
     839                 :            :         SSL *ssl;
     840                 :            : 
     841                 :        147 :         ssl = SSL_new(ctx);
     842         [ -  + ]:        147 :         if (!ssl) {
     843                 :          0 :                 SPDK_ERRLOG("SSL_new() failed, msg = %s\n", ERR_error_string(ERR_peek_last_error(), NULL));
     844                 :          0 :                 return NULL;
     845                 :            :         }
     846                 :        147 :         SSL_set_fd(ssl, fd);
     847                 :        147 :         SSL_set_accept_state(ssl);
     848                 :        147 :         SSL_set_psk_find_session_callback(ssl, posix_sock_psk_find_session_server_cb);
     849   [ -  +  -  + ]:        147 :         SPDK_DEBUGLOG(sock_posix, "SSL object creation finished: %p\n", ssl);
     850   [ -  +  -  + ]:        147 :         SPDK_DEBUGLOG(sock_posix, "%s = SSL_state_string_long(%p)\n", SSL_state_string_long(ssl), ssl);
     851   [ -  +  -  + ]:        147 :         SPDK_DEBUGLOG(sock_posix, "%s = SSL_state_string_long(%p)\n", SSL_state_string_long(ssl), ssl);
     852   [ -  +  -  + ]:        147 :         SPDK_DEBUGLOG(sock_posix, "Negotiated Cipher suite:%s\n",
     853                 :            :                       SSL_CIPHER_get_name(SSL_get_current_cipher(ssl)));
     854                 :        147 :         return ssl;
     855                 :            : }
     856                 :            : 
     857                 :            : static ssize_t
     858                 :   10673096 : SSL_readv(SSL *ssl, const struct iovec *iov, int iovcnt)
     859                 :            : {
     860                 :   10673096 :         int i, rc = 0;
     861                 :   10673096 :         ssize_t total = 0;
     862                 :            : 
     863         [ +  + ]:   12406362 :         for (i = 0; i < iovcnt; i++) {
     864                 :   10673096 :                 rc = SSL_read(ssl, iov[i].iov_base, iov[i].iov_len);
     865                 :            : 
     866         [ +  + ]:   10673096 :                 if (rc > 0) {
     867                 :    5943275 :                         total += rc;
     868                 :            :                 }
     869         [ +  + ]:   10673096 :                 if (rc != (int)iov[i].iov_len) {
     870                 :    8939830 :                         break;
     871                 :            :                 }
     872                 :            :         }
     873         [ +  + ]:   10673096 :         if (total > 0) {
     874                 :    5943275 :                 errno = 0;
     875                 :    5943275 :                 return total;
     876                 :            :         }
     877   [ +  +  +  - ]:    4729821 :         switch (SSL_get_error(ssl, rc)) {
     878                 :        123 :         case SSL_ERROR_ZERO_RETURN:
     879                 :        123 :                 errno = ENOTCONN;
     880                 :        123 :                 return 0;
     881                 :    4471658 :         case SSL_ERROR_WANT_READ:
     882                 :            :         case SSL_ERROR_WANT_WRITE:
     883                 :            :         case SSL_ERROR_WANT_CONNECT:
     884                 :            :         case SSL_ERROR_WANT_ACCEPT:
     885                 :            :         case SSL_ERROR_WANT_X509_LOOKUP:
     886                 :            :         case SSL_ERROR_WANT_ASYNC:
     887                 :            :         case SSL_ERROR_WANT_ASYNC_JOB:
     888                 :            :         case SSL_ERROR_WANT_CLIENT_HELLO_CB:
     889                 :    4471658 :                 errno = EAGAIN;
     890                 :    4471658 :                 return -1;
     891                 :     258040 :         case SSL_ERROR_SYSCALL:
     892                 :            :         case SSL_ERROR_SSL:
     893                 :     258040 :                 errno = ENOTCONN;
     894                 :     258040 :                 return -1;
     895                 :          0 :         default:
     896                 :          0 :                 errno = ENOTCONN;
     897                 :          0 :                 return -1;
     898                 :            :         }
     899                 :            : }
     900                 :            : 
     901                 :            : static ssize_t
     902                 :    7021021 : SSL_writev(SSL *ssl, struct iovec *iov, int iovcnt)
     903                 :            : {
     904                 :    7021021 :         int i, rc = 0;
     905                 :    7021021 :         ssize_t total = 0;
     906                 :            : 
     907         [ +  + ]:   12964025 :         for (i = 0; i < iovcnt; i++) {
     908                 :   12284796 :                 rc = SSL_write(ssl, iov[i].iov_base, iov[i].iov_len);
     909                 :            : 
     910         [ +  + ]:   12284796 :                 if (rc > 0) {
     911                 :    5943004 :                         total += rc;
     912                 :            :                 }
     913         [ +  + ]:   12284796 :                 if (rc != (int)iov[i].iov_len) {
     914                 :    6341792 :                         break;
     915                 :            :                 }
     916                 :            :         }
     917         [ +  + ]:    7021021 :         if (total > 0) {
     918                 :    1815181 :                 errno = 0;
     919                 :    1815181 :                 return total;
     920                 :            :         }
     921   [ -  +  +  - ]:    5205840 :         switch (SSL_get_error(ssl, rc)) {
     922                 :          0 :         case SSL_ERROR_ZERO_RETURN:
     923                 :          0 :                 errno = ENOTCONN;
     924                 :          0 :                 return 0;
     925                 :    5205822 :         case SSL_ERROR_WANT_READ:
     926                 :            :         case SSL_ERROR_WANT_WRITE:
     927                 :            :         case SSL_ERROR_WANT_CONNECT:
     928                 :            :         case SSL_ERROR_WANT_ACCEPT:
     929                 :            :         case SSL_ERROR_WANT_X509_LOOKUP:
     930                 :            :         case SSL_ERROR_WANT_ASYNC:
     931                 :            :         case SSL_ERROR_WANT_ASYNC_JOB:
     932                 :            :         case SSL_ERROR_WANT_CLIENT_HELLO_CB:
     933                 :    5205822 :                 errno = EAGAIN;
     934                 :    5205822 :                 return -1;
     935                 :         18 :         case SSL_ERROR_SYSCALL:
     936                 :            :         case SSL_ERROR_SSL:
     937                 :         18 :                 errno = ENOTCONN;
     938                 :         18 :                 return -1;
     939                 :          0 :         default:
     940                 :          0 :                 errno = ENOTCONN;
     941                 :          0 :                 return -1;
     942                 :            :         }
     943                 :            : }
     944                 :            : 
     945                 :            : static struct spdk_sock *
     946                 :      18940 : posix_sock_create(const char *ip, int port,
     947                 :            :                   enum posix_sock_create_type type,
     948                 :            :                   struct spdk_sock_opts *opts,
     949                 :            :                   bool enable_ssl)
     950                 :            : {
     951                 :            :         struct spdk_posix_sock *sock;
     952                 :        157 :         struct spdk_sock_impl_opts impl_opts;
     953                 :        157 :         char buf[MAX_TMPBUF];
     954                 :        157 :         char portnum[PORTNUMLEN];
     955                 :            :         char *p;
     956                 :        157 :         struct addrinfo hints, *res, *res0;
     957                 :            :         int fd, flag;
     958                 :            :         int rc;
     959                 :      18940 :         bool enable_zcopy_user_opts = true;
     960                 :      18940 :         bool enable_zcopy_impl_opts = true;
     961                 :      18940 :         SSL_CTX *ctx = 0;
     962                 :      18940 :         SSL *ssl = 0;
     963                 :            : 
     964         [ -  + ]:      18940 :         assert(opts != NULL);
     965         [ +  + ]:      18940 :         if (enable_ssl) {
     966                 :        179 :                 _opts_get_impl_opts(opts, &impl_opts, &g_ssl_impl_opts);
     967                 :            :         } else {
     968                 :      18761 :                 _opts_get_impl_opts(opts, &impl_opts, &g_posix_impl_opts);
     969                 :            :         }
     970                 :            : 
     971         [ -  + ]:      18940 :         if (ip == NULL) {
     972                 :          0 :                 return NULL;
     973                 :            :         }
     974         [ -  + ]:      18940 :         if (ip[0] == '[') {
     975                 :          0 :                 snprintf(buf, sizeof(buf), "%s", ip + 1);
     976                 :          0 :                 p = strchr(buf, ']');
     977         [ #  # ]:          0 :                 if (p != NULL) {
     978                 :          0 :                         *p = '\0';
     979                 :            :                 }
     980                 :          0 :                 ip = (const char *) &buf[0];
     981                 :            :         }
     982                 :            : 
     983                 :      18940 :         snprintf(portnum, sizeof portnum, "%d", port);
     984                 :      18940 :         memset(&hints, 0, sizeof hints);
     985                 :      18940 :         hints.ai_family = PF_UNSPEC;
     986                 :      18940 :         hints.ai_socktype = SOCK_STREAM;
     987                 :      18940 :         hints.ai_flags = AI_NUMERICSERV;
     988                 :      18940 :         hints.ai_flags |= AI_PASSIVE;
     989                 :      18940 :         hints.ai_flags |= AI_NUMERICHOST;
     990                 :      18940 :         rc = getaddrinfo(ip, portnum, &hints, &res0);
     991         [ -  + ]:      18940 :         if (rc != 0) {
     992                 :          0 :                 SPDK_ERRLOG("getaddrinfo() failed %s (%d)\n", gai_strerror(rc), rc);
     993                 :          0 :                 return NULL;
     994                 :            :         }
     995                 :            : 
     996                 :            :         /* try listen */
     997                 :      18940 :         fd = -1;
     998         [ +  + ]:      33855 :         for (res = res0; res != NULL; res = res->ai_next) {
     999                 :      18940 : retry:
    1000                 :      18940 :                 fd = posix_fd_create(res, opts, &impl_opts);
    1001         [ -  + ]:      18940 :                 if (fd < 0) {
    1002                 :          0 :                         continue;
    1003                 :            :                 }
    1004         [ +  + ]:      18940 :                 if (type == SPDK_SOCK_CREATE_LISTEN) {
    1005                 :        273 :                         rc = bind(fd, res->ai_addr, res->ai_addrlen);
    1006         [ -  + ]:        273 :                         if (rc != 0) {
    1007                 :          0 :                                 SPDK_ERRLOG("bind() failed at port %d, errno = %d\n", port, errno);
    1008      [ #  #  # ]:          0 :                                 switch (errno) {
    1009                 :          0 :                                 case EINTR:
    1010                 :            :                                         /* interrupted? */
    1011                 :          0 :                                         close(fd);
    1012                 :          0 :                                         goto retry;
    1013                 :          0 :                                 case EADDRNOTAVAIL:
    1014                 :          0 :                                         SPDK_ERRLOG("IP address %s not available. "
    1015                 :            :                                                     "Verify IP address in config file "
    1016                 :            :                                                     "and make sure setup script is "
    1017                 :            :                                                     "run before starting spdk app.\n", ip);
    1018                 :            :                                 /* FALLTHROUGH */
    1019                 :          0 :                                 default:
    1020                 :            :                                         /* try next family */
    1021                 :          0 :                                         close(fd);
    1022                 :          0 :                                         fd = -1;
    1023                 :          0 :                                         continue;
    1024                 :            :                                 }
    1025                 :            :                         }
    1026                 :            :                         /* bind OK */
    1027                 :        273 :                         rc = listen(fd, 512);
    1028         [ -  + ]:        273 :                         if (rc != 0) {
    1029                 :          0 :                                 SPDK_ERRLOG("listen() failed, errno = %d\n", errno);
    1030                 :          0 :                                 close(fd);
    1031                 :          0 :                                 fd = -1;
    1032                 :          0 :                                 break;
    1033                 :            :                         }
    1034         [ -  + ]:        273 :                         enable_zcopy_impl_opts = impl_opts.enable_zerocopy_send_server;
    1035         [ +  - ]:      18667 :                 } else if (type == SPDK_SOCK_CREATE_CONNECT) {
    1036                 :      18667 :                         rc = connect(fd, res->ai_addr, res->ai_addrlen);
    1037         [ +  + ]:      18667 :                         if (rc != 0) {
    1038                 :      14915 :                                 SPDK_ERRLOG("connect() failed, errno = %d\n", errno);
    1039                 :            :                                 /* try next family */
    1040                 :      14915 :                                 close(fd);
    1041                 :      14915 :                                 fd = -1;
    1042                 :      14915 :                                 continue;
    1043                 :            :                         }
    1044         [ -  + ]:       3752 :                         enable_zcopy_impl_opts = impl_opts.enable_zerocopy_send_client;
    1045         [ +  + ]:       3752 :                         if (enable_ssl) {
    1046                 :        142 :                                 ctx = posix_sock_create_ssl_context(TLS_client_method(), opts, &impl_opts);
    1047         [ +  + ]:        142 :                                 if (!ctx) {
    1048                 :          2 :                                         SPDK_ERRLOG("posix_sock_create_ssl_context() failed, errno = %d\n", errno);
    1049                 :          2 :                                         close(fd);
    1050                 :          2 :                                         fd = -1;
    1051                 :          2 :                                         break;
    1052                 :            :                                 }
    1053                 :        140 :                                 ssl = ssl_sock_setup_connect(ctx, fd);
    1054         [ -  + ]:        140 :                                 if (!ssl) {
    1055                 :          0 :                                         SPDK_ERRLOG("ssl_sock_setup_connect() failed, errno = %d\n", errno);
    1056                 :          0 :                                         close(fd);
    1057                 :          0 :                                         fd = -1;
    1058                 :          0 :                                         SSL_CTX_free(ctx);
    1059                 :          0 :                                         break;
    1060                 :            :                                 }
    1061                 :            :                         }
    1062                 :            :                 }
    1063                 :            : 
    1064                 :       4023 :                 flag = fcntl(fd, F_GETFL);
    1065         [ -  + ]:       4023 :                 if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
    1066                 :          0 :                         SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%d)\n", fd, errno);
    1067                 :          0 :                         SSL_free(ssl);
    1068                 :          0 :                         SSL_CTX_free(ctx);
    1069                 :          0 :                         close(fd);
    1070                 :          0 :                         fd = -1;
    1071                 :          0 :                         break;
    1072                 :            :                 }
    1073                 :       4023 :                 break;
    1074                 :            :         }
    1075                 :      18940 :         freeaddrinfo(res0);
    1076                 :            : 
    1077         [ +  + ]:      18940 :         if (fd < 0) {
    1078                 :      14917 :                 return NULL;
    1079                 :            :         }
    1080                 :            : 
    1081                 :            :         /* Only enable zero copy for non-loopback and non-ssl sockets. */
    1082   [ +  +  +  +  :       4023 :         enable_zcopy_user_opts = opts->zcopy && !sock_is_loopback(fd) && !enable_ssl;
             +  +  +  + ]
    1083                 :            : 
    1084   [ +  +  +  + ]:       4023 :         sock = posix_sock_alloc(fd, &impl_opts, enable_zcopy_user_opts && enable_zcopy_impl_opts);
    1085         [ -  + ]:       4023 :         if (sock == NULL) {
    1086                 :          0 :                 SPDK_ERRLOG("sock allocation failed\n");
    1087                 :          0 :                 SSL_free(ssl);
    1088                 :          0 :                 SSL_CTX_free(ctx);
    1089                 :          0 :                 close(fd);
    1090                 :          0 :                 return NULL;
    1091                 :            :         }
    1092                 :            : 
    1093         [ +  + ]:       4023 :         if (ctx) {
    1094                 :        140 :                 sock->ctx = ctx;
    1095                 :            :         }
    1096                 :            : 
    1097         [ +  + ]:       4023 :         if (ssl) {
    1098                 :        140 :                 sock->ssl = ssl;
    1099                 :        140 :                 SSL_set_app_data(ssl, &sock->base.impl_opts);
    1100                 :            :         }
    1101                 :            : 
    1102                 :       4023 :         return &sock->base;
    1103                 :            : }
    1104                 :            : 
    1105                 :            : static struct spdk_sock *
    1106                 :        236 : posix_sock_listen(const char *ip, int port, struct spdk_sock_opts *opts)
    1107                 :            : {
    1108                 :        236 :         return posix_sock_create(ip, port, SPDK_SOCK_CREATE_LISTEN, opts, false);
    1109                 :            : }
    1110                 :            : 
    1111                 :            : static struct spdk_sock *
    1112                 :      18525 : posix_sock_connect(const char *ip, int port, struct spdk_sock_opts *opts)
    1113                 :            : {
    1114                 :      18525 :         return posix_sock_create(ip, port, SPDK_SOCK_CREATE_CONNECT, opts, false);
    1115                 :            : }
    1116                 :            : 
    1117                 :            : static struct spdk_sock *
    1118                 :     618437 : _posix_sock_accept(struct spdk_sock *_sock, bool enable_ssl)
    1119                 :            : {
    1120                 :     618437 :         struct spdk_posix_sock          *sock = __posix_sock(_sock);
    1121                 :     618437 :         struct spdk_posix_sock_group_impl *group = __posix_group_impl(sock->base.group_impl);
    1122                 :      45574 :         struct sockaddr_storage         sa;
    1123                 :      45574 :         socklen_t                       salen;
    1124                 :            :         int                             rc, fd;
    1125                 :            :         struct spdk_posix_sock          *new_sock;
    1126                 :            :         int                             flag;
    1127                 :     618437 :         SSL_CTX *ctx = 0;
    1128                 :     618437 :         SSL *ssl = 0;
    1129                 :            : 
    1130                 :     618437 :         memset(&sa, 0, sizeof(sa));
    1131                 :     618437 :         salen = sizeof(sa);
    1132                 :            : 
    1133         [ -  + ]:     618437 :         assert(sock != NULL);
    1134                 :            : 
    1135                 :            :         /* epoll_wait will trigger again if there is more than one request */
    1136   [ -  +  -  -  :     618437 :         if (group && sock->socket_has_data) {
                   -  - ]
    1137                 :          0 :                 sock->socket_has_data = false;
    1138         [ #  # ]:          0 :                 TAILQ_REMOVE(&group->socks_with_data, sock, link);
    1139                 :            :         }
    1140                 :            : 
    1141                 :     618437 :         rc = accept(sock->fd, (struct sockaddr *)&sa, &salen);
    1142                 :            : 
    1143         [ +  + ]:     618437 :         if (rc == -1) {
    1144                 :     612493 :                 return NULL;
    1145                 :            :         }
    1146                 :            : 
    1147                 :       5944 :         fd = rc;
    1148                 :            : 
    1149                 :       5944 :         flag = fcntl(fd, F_GETFL);
    1150   [ +  -  -  + ]:       5944 :         if ((!(flag & O_NONBLOCK)) && (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0)) {
    1151                 :          0 :                 SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%d)\n", fd, errno);
    1152                 :          0 :                 close(fd);
    1153                 :          0 :                 return NULL;
    1154                 :            :         }
    1155                 :            : 
    1156                 :            : #if defined(SO_PRIORITY)
    1157                 :            :         /* The priority is not inherited, so call this function again */
    1158         [ +  + ]:       5944 :         if (sock->base.opts.priority) {
    1159                 :          5 :                 rc = setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &sock->base.opts.priority, sizeof(int));
    1160         [ -  + ]:          5 :                 if (rc != 0) {
    1161                 :          0 :                         close(fd);
    1162                 :          0 :                         return NULL;
    1163                 :            :                 }
    1164                 :            :         }
    1165                 :            : #endif
    1166                 :            : 
    1167                 :            :         /* Establish SSL connection */
    1168         [ +  + ]:       5944 :         if (enable_ssl) {
    1169                 :        147 :                 ctx = posix_sock_create_ssl_context(TLS_server_method(), &sock->base.opts, &sock->base.impl_opts);
    1170         [ -  + ]:        147 :                 if (!ctx) {
    1171                 :          0 :                         SPDK_ERRLOG("posix_sock_create_ssl_context() failed, errno = %d\n", errno);
    1172                 :          0 :                         close(fd);
    1173                 :          0 :                         return NULL;
    1174                 :            :                 }
    1175                 :        147 :                 ssl = ssl_sock_setup_accept(ctx, fd);
    1176         [ -  + ]:        147 :                 if (!ssl) {
    1177                 :          0 :                         SPDK_ERRLOG("ssl_sock_setup_accept() failed, errno = %d\n", errno);
    1178                 :          0 :                         close(fd);
    1179                 :          0 :                         SSL_CTX_free(ctx);
    1180                 :          0 :                         return NULL;
    1181                 :            :                 }
    1182                 :            :         }
    1183                 :            : 
    1184                 :            :         /* Inherit the zero copy feature from the listen socket */
    1185         [ -  + ]:       5944 :         new_sock = posix_sock_alloc(fd, &sock->base.impl_opts, sock->zcopy);
    1186         [ -  + ]:       5944 :         if (new_sock == NULL) {
    1187                 :          0 :                 close(fd);
    1188                 :          0 :                 SSL_free(ssl);
    1189                 :          0 :                 SSL_CTX_free(ctx);
    1190                 :          0 :                 return NULL;
    1191                 :            :         }
    1192                 :            : 
    1193         [ +  + ]:       5944 :         if (ctx) {
    1194                 :        147 :                 new_sock->ctx = ctx;
    1195                 :            :         }
    1196                 :            : 
    1197         [ +  + ]:       5944 :         if (ssl) {
    1198                 :        147 :                 new_sock->ssl = ssl;
    1199                 :        147 :                 SSL_set_app_data(ssl, &new_sock->base.impl_opts);
    1200                 :            :         }
    1201                 :            : 
    1202                 :       5944 :         return &new_sock->base;
    1203                 :            : }
    1204                 :            : 
    1205                 :            : static struct spdk_sock *
    1206                 :     570954 : posix_sock_accept(struct spdk_sock *_sock)
    1207                 :            : {
    1208                 :     570954 :         return _posix_sock_accept(_sock, false);
    1209                 :            : }
    1210                 :            : 
    1211                 :            : static int
    1212                 :       9967 : posix_sock_close(struct spdk_sock *_sock)
    1213                 :            : {
    1214                 :       9967 :         struct spdk_posix_sock *sock = __posix_sock(_sock);
    1215                 :            :         void *pipe_buf;
    1216                 :            : 
    1217         [ -  + ]:       9967 :         assert(TAILQ_EMPTY(&_sock->pending_reqs));
    1218                 :            : 
    1219         [ +  + ]:       9967 :         if (sock->ssl != NULL) {
    1220                 :        287 :                 SSL_shutdown(sock->ssl);
    1221                 :            :         }
    1222                 :            : 
    1223                 :            :         /* If the socket fails to close, the best choice is to
    1224                 :            :          * leak the fd but continue to free the rest of the sock
    1225                 :            :          * memory. */
    1226                 :       9967 :         close(sock->fd);
    1227                 :            : 
    1228                 :       9967 :         SSL_free(sock->ssl);
    1229                 :       9967 :         SSL_CTX_free(sock->ctx);
    1230                 :            : 
    1231                 :       9967 :         pipe_buf = spdk_pipe_destroy(sock->recv_pipe);
    1232                 :       9967 :         free(pipe_buf);
    1233                 :       9967 :         free(sock);
    1234                 :            : 
    1235                 :       9967 :         return 0;
    1236                 :            : }
    1237                 :            : 
    1238                 :            : #ifdef SPDK_ZEROCOPY
    1239                 :            : static int
    1240                 :    1762909 : _sock_check_zcopy(struct spdk_sock *sock)
    1241                 :            : {
    1242                 :    1762909 :         struct spdk_posix_sock *psock = __posix_sock(sock);
    1243                 :    1762909 :         struct msghdr msgh = {};
    1244                 :      30253 :         uint8_t buf[sizeof(struct cmsghdr) + sizeof(struct sock_extended_err)];
    1245                 :            :         ssize_t rc;
    1246                 :            :         struct sock_extended_err *serr;
    1247                 :            :         struct cmsghdr *cm;
    1248                 :            :         uint32_t idx;
    1249                 :            :         struct spdk_sock_request *req, *treq;
    1250                 :            :         bool found;
    1251                 :            : 
    1252                 :    1762909 :         msgh.msg_control = buf;
    1253                 :    1762909 :         msgh.msg_controllen = sizeof(buf);
    1254                 :            : 
    1255                 :            :         while (true) {
    1256                 :    3616133 :                 rc = recvmsg(psock->fd, &msgh, MSG_ERRQUEUE);
    1257                 :            : 
    1258         [ +  + ]:    3616133 :                 if (rc < 0) {
    1259   [ -  +  -  - ]:    1762909 :                         if (errno == EWOULDBLOCK || errno == EAGAIN) {
    1260                 :    1762909 :                                 return 0;
    1261                 :            :                         }
    1262                 :            : 
    1263         [ #  # ]:          0 :                         if (!TAILQ_EMPTY(&sock->pending_reqs)) {
    1264                 :          0 :                                 SPDK_ERRLOG("Attempting to receive from ERRQUEUE yielded error, but pending list still has orphaned entries\n");
    1265                 :            :                         } else {
    1266                 :          0 :                                 SPDK_WARNLOG("Recvmsg yielded an error!\n");
    1267                 :            :                         }
    1268                 :          0 :                         return 0;
    1269                 :            :                 }
    1270                 :            : 
    1271         [ +  - ]:    1853224 :                 cm = CMSG_FIRSTHDR(&msgh);
    1272         [ +  - ]:    1853224 :                 if (!(cm &&
    1273   [ +  -  -  + ]:    1853224 :                       ((cm->cmsg_level == SOL_IP && cm->cmsg_type == IP_RECVERR) ||
    1274   [ #  #  #  # ]:          0 :                        (cm->cmsg_level == SOL_IPV6 && cm->cmsg_type == IPV6_RECVERR)))) {
    1275                 :          0 :                         SPDK_WARNLOG("Unexpected cmsg level or type!\n");
    1276                 :          0 :                         return 0;
    1277                 :            :                 }
    1278                 :            : 
    1279                 :    1853224 :                 serr = (struct sock_extended_err *)CMSG_DATA(cm);
    1280   [ +  -  -  + ]:    1853224 :                 if (serr->ee_errno != 0 || serr->ee_origin != SO_EE_ORIGIN_ZEROCOPY) {
    1281                 :          0 :                         SPDK_WARNLOG("Unexpected extended error origin\n");
    1282                 :          0 :                         return 0;
    1283                 :            :                 }
    1284                 :            : 
    1285                 :            :                 /* Most of the time, the pending_reqs array is in the exact
    1286                 :            :                  * order we need such that all of the requests to complete are
    1287                 :            :                  * in order, in the front. It is guaranteed that all requests
    1288                 :            :                  * belonging to the same sendmsg call are sequential, so once
    1289                 :            :                  * we encounter one match we can stop looping as soon as a
    1290                 :            :                  * non-match is found.
    1291                 :            :                  */
    1292                 :    1853224 :                 idx = serr->ee_info;
    1293                 :            :                 while (true) {
    1294                 :    2341935 :                         found = false;
    1295         [ +  + ]:   19666577 :                         TAILQ_FOREACH_SAFE(req, &sock->pending_reqs, internal.link, treq) {
    1296   [ -  +  -  + ]:   18409978 :                                 if (!req->internal.is_zcopy) {
    1297                 :            :                                         /* This wasn't a zcopy request. It was just waiting in line to complete */
    1298                 :          0 :                                         rc = spdk_sock_request_put(sock, req, 0);
    1299         [ #  # ]:          0 :                                         if (rc < 0) {
    1300                 :          0 :                                                 return rc;
    1301                 :            :                                         }
    1302         [ +  + ]:   18409978 :                                 } else if (req->internal.offset == idx) {
    1303                 :   16800486 :                                         found = true;
    1304                 :   16800486 :                                         rc = spdk_sock_request_put(sock, req, 0);
    1305         [ -  + ]:   16800486 :                                         if (rc < 0) {
    1306                 :          0 :                                                 return rc;
    1307                 :            :                                         }
    1308         [ +  + ]:    1609492 :                                 } else if (found) {
    1309                 :    1085336 :                                         break;
    1310                 :            :                                 }
    1311                 :            :                         }
    1312                 :            : 
    1313         [ +  + ]:    2341935 :                         if (idx == serr->ee_data) {
    1314                 :    1853224 :                                 break;
    1315                 :            :                         }
    1316                 :            : 
    1317         [ -  + ]:     488711 :                         if (idx == UINT32_MAX) {
    1318                 :          0 :                                 idx = 0;
    1319                 :            :                         } else {
    1320                 :     488711 :                                 idx++;
    1321                 :            :                         }
    1322                 :            :                 }
    1323                 :            :         }
    1324                 :            : 
    1325                 :            :         return 0;
    1326                 :            : }
    1327                 :            : #endif
    1328                 :            : 
    1329                 :            : static int
    1330                 : 1133982302 : _sock_flush(struct spdk_sock *sock)
    1331                 :            : {
    1332                 : 1133982302 :         struct spdk_posix_sock *psock = __posix_sock(sock);
    1333                 : 1133982302 :         struct msghdr msg = {};
    1334                 :   58061643 :         int flags;
    1335                 :   58061643 :         struct iovec iovs[IOV_BATCH_SIZE];
    1336                 :            :         int iovcnt;
    1337                 :            :         int retval;
    1338                 :            :         struct spdk_sock_request *req;
    1339                 :            :         int i;
    1340                 :            :         ssize_t rc, sent;
    1341                 :            :         unsigned int offset;
    1342                 :            :         size_t len;
    1343                 : 1133982302 :         bool is_zcopy = false;
    1344                 :            : 
    1345                 :            :         /* Can't flush from within a callback or we end up with recursive calls */
    1346         [ +  + ]: 1133982302 :         if (sock->cb_cnt > 0) {
    1347                 :     590093 :                 errno = EAGAIN;
    1348                 :     590093 :                 return -1;
    1349                 :            :         }
    1350                 :            : 
    1351                 :            : #ifdef SPDK_ZEROCOPY
    1352   [ -  +  +  + ]: 1133392209 :         if (psock->zcopy) {
    1353                 :  827045901 :                 flags = MSG_ZEROCOPY | MSG_NOSIGNAL;
    1354                 :            :         } else
    1355                 :            : #endif
    1356                 :            :         {
    1357                 :  306346308 :                 flags = MSG_NOSIGNAL;
    1358                 :            :         }
    1359                 :            : 
    1360                 : 1133392209 :         iovcnt = spdk_sock_prep_reqs(sock, iovs, 0, NULL, &flags);
    1361         [ +  + ]: 1133392209 :         if (iovcnt == 0) {
    1362                 : 1118713743 :                 return 0;
    1363                 :            :         }
    1364                 :            : 
    1365                 :            : #ifdef SPDK_ZEROCOPY
    1366                 :   14678466 :         is_zcopy = flags & MSG_ZEROCOPY;
    1367                 :            : #endif
    1368                 :            : 
    1369                 :            :         /* Perform the vectored write */
    1370                 :   14678466 :         msg.msg_iov = iovs;
    1371                 :   14678466 :         msg.msg_iovlen = iovcnt;
    1372                 :            : 
    1373         [ +  + ]:   14678466 :         if (psock->ssl) {
    1374                 :    7020433 :                 rc = SSL_writev(psock->ssl, iovs, iovcnt);
    1375                 :            :         } else {
    1376                 :    7658033 :                 rc = sendmsg(psock->fd, &msg, flags);
    1377                 :            :         }
    1378         [ +  + ]:   14678466 :         if (rc <= 0) {
    1379   [ +  -  +  +  :    8812410 :                 if (rc == 0 || errno == EAGAIN || errno == EWOULDBLOCK || (errno == ENOBUFS && psock->zcopy)) {
          +  -  -  +  -  
                -  -  - ]
    1380                 :    8812376 :                         errno = EAGAIN;
    1381                 :            :                 }
    1382                 :    8812410 :                 return -1;
    1383                 :            :         }
    1384                 :            : 
    1385                 :    5866056 :         sent = rc;
    1386                 :            : 
    1387         [ +  + ]:    5866056 :         if (is_zcopy) {
    1388                 :            :                 /* Handling overflow case, because we use psock->sendmsg_idx - 1 for the
    1389                 :            :                  * req->internal.offset, so sendmsg_idx should not be zero  */
    1390         [ -  + ]:    2342029 :                 if (spdk_unlikely(psock->sendmsg_idx == UINT32_MAX)) {
    1391                 :          0 :                         psock->sendmsg_idx = 1;
    1392                 :            :                 } else {
    1393                 :    2342029 :                         psock->sendmsg_idx++;
    1394                 :            :                 }
    1395                 :            :         }
    1396                 :            : 
    1397                 :            :         /* Consume the requests that were actually written */
    1398                 :    5866056 :         req = TAILQ_FIRST(&sock->queued_reqs);
    1399         [ +  - ]:   33374282 :         while (req) {
    1400                 :   33374282 :                 offset = req->internal.offset;
    1401                 :            : 
    1402                 :            :                 /* req->internal.is_zcopy is true when the whole req or part of it is sent with zerocopy */
    1403                 :   33374282 :                 req->internal.is_zcopy = is_zcopy;
    1404                 :            : 
    1405         [ +  + ]:   90867929 :                 for (i = 0; i < req->iovcnt; i++) {
    1406                 :            :                         /* Advance by the offset first */
    1407         [ +  + ]:   58957813 :                         if (offset >= SPDK_SOCK_REQUEST_IOV(req, i)->iov_len) {
    1408                 :    2251621 :                                 offset -= SPDK_SOCK_REQUEST_IOV(req, i)->iov_len;
    1409                 :    2251621 :                                 continue;
    1410                 :            :                         }
    1411                 :            : 
    1412                 :            :                         /* Calculate the remaining length of this element */
    1413                 :   56706192 :                         len = SPDK_SOCK_REQUEST_IOV(req, i)->iov_len - offset;
    1414                 :            : 
    1415         [ +  + ]:   56706192 :                         if (len > (size_t)rc) {
    1416                 :            :                                 /* This element was partially sent. */
    1417                 :    1464166 :                                 req->internal.offset += rc;
    1418                 :    1464166 :                                 return sent;
    1419                 :            :                         }
    1420                 :            : 
    1421                 :   55242026 :                         offset = 0;
    1422                 :   55242026 :                         req->internal.offset += len;
    1423                 :   55242026 :                         rc -= len;
    1424                 :            :                 }
    1425                 :            : 
    1426                 :            :                 /* Handled a full request. */
    1427                 :   31910116 :                 spdk_sock_request_pend(sock, req);
    1428                 :            : 
    1429   [ +  +  +  +  :   31910116 :                 if (!req->internal.is_zcopy && req == TAILQ_FIRST(&sock->pending_reqs)) {
                   +  - ]
    1430                 :            :                         /* The sendmsg syscall above isn't currently asynchronous,
    1431                 :            :                         * so it's already done. */
    1432                 :   15108860 :                         retval = spdk_sock_request_put(sock, req, 0);
    1433         [ +  + ]:   15108860 :                         if (retval) {
    1434                 :          3 :                                 break;
    1435                 :            :                         }
    1436                 :            :                 } else {
    1437                 :            :                         /* Re-use the offset field to hold the sendmsg call index. The
    1438                 :            :                          * index is 0 based, so subtract one here because we've already
    1439                 :            :                          * incremented above. */
    1440                 :   16801256 :                         req->internal.offset = psock->sendmsg_idx - 1;
    1441                 :            :                 }
    1442                 :            : 
    1443         [ +  + ]:   31910113 :                 if (rc == 0) {
    1444                 :    4401887 :                         break;
    1445                 :            :                 }
    1446                 :            : 
    1447                 :   27508226 :                 req = TAILQ_FIRST(&sock->queued_reqs);
    1448                 :            :         }
    1449                 :            : 
    1450                 :    4401890 :         return sent;
    1451                 :            : }
    1452                 :            : 
    1453                 :            : static int
    1454                 :   28207469 : posix_sock_flush(struct spdk_sock *sock)
    1455                 :            : {
    1456                 :            : #ifdef SPDK_ZEROCOPY
    1457                 :   28207469 :         struct spdk_posix_sock *psock = __posix_sock(sock);
    1458                 :            : 
    1459   [ -  +  +  +  :   28207469 :         if (psock->zcopy && !TAILQ_EMPTY(&sock->pending_reqs)) {
                   +  + ]
    1460                 :         31 :                 _sock_check_zcopy(sock);
    1461                 :            :         }
    1462                 :            : #endif
    1463                 :            : 
    1464                 :   28207469 :         return _sock_flush(sock);
    1465                 :            : }
    1466                 :            : 
    1467                 :            : static ssize_t
    1468                 :   77172849 : posix_sock_recv_from_pipe(struct spdk_posix_sock *sock, struct iovec *diov, int diovcnt)
    1469                 :            : {
    1470                 :     646109 :         struct iovec siov[2];
    1471                 :            :         int sbytes;
    1472                 :            :         ssize_t bytes;
    1473                 :            :         struct spdk_posix_sock_group_impl *group;
    1474                 :            : 
    1475                 :   77172849 :         sbytes = spdk_pipe_reader_get_buffer(sock->recv_pipe, sock->recv_buf_sz, siov);
    1476         [ -  + ]:   77172849 :         if (sbytes < 0) {
    1477                 :          0 :                 errno = EINVAL;
    1478                 :          0 :                 return -1;
    1479         [ +  + ]:   77172849 :         } else if (sbytes == 0) {
    1480                 :    6534209 :                 errno = EAGAIN;
    1481                 :    6534209 :                 return -1;
    1482                 :            :         }
    1483                 :            : 
    1484                 :   70638640 :         bytes = spdk_iovcpy(siov, 2, diov, diovcnt);
    1485                 :            : 
    1486         [ -  + ]:   70638640 :         if (bytes == 0) {
    1487                 :            :                 /* The only way this happens is if diov is 0 length */
    1488                 :          0 :                 errno = EINVAL;
    1489                 :          0 :                 return -1;
    1490                 :            :         }
    1491                 :            : 
    1492                 :   70638640 :         spdk_pipe_reader_advance(sock->recv_pipe, bytes);
    1493                 :            : 
    1494                 :            :         /* If we drained the pipe, mark it appropriately */
    1495         [ +  + ]:   70638640 :         if (spdk_pipe_reader_bytes_available(sock->recv_pipe) == 0) {
    1496   [ -  +  -  + ]:    8246272 :                 assert(sock->pipe_has_data == true);
    1497                 :            : 
    1498                 :    8246272 :                 group = __posix_group_impl(sock->base.group_impl);
    1499   [ +  +  -  +  :    8246272 :                 if (group && !sock->socket_has_data) {
                   +  + ]
    1500         [ +  + ]:    6469598 :                         TAILQ_REMOVE(&group->socks_with_data, sock, link);
    1501                 :            :                 }
    1502                 :            : 
    1503                 :    8246272 :                 sock->pipe_has_data = false;
    1504                 :            :         }
    1505                 :            : 
    1506                 :   70638640 :         return bytes;
    1507                 :            : }
    1508                 :            : 
    1509                 :            : static inline ssize_t
    1510                 :   27597659 : posix_sock_read(struct spdk_posix_sock *sock)
    1511                 :            : {
    1512                 :     212222 :         struct iovec iov[2];
    1513                 :            :         int bytes_avail, bytes_recvd;
    1514                 :            :         struct spdk_posix_sock_group_impl *group;
    1515                 :            : 
    1516                 :   27597659 :         bytes_avail = spdk_pipe_writer_get_buffer(sock->recv_pipe, sock->recv_buf_sz, iov);
    1517                 :            : 
    1518         [ -  + ]:   27597659 :         if (bytes_avail <= 0) {
    1519                 :          0 :                 return bytes_avail;
    1520                 :            :         }
    1521                 :            : 
    1522         [ +  + ]:   27597659 :         if (sock->ssl) {
    1523                 :    4295004 :                 bytes_recvd = SSL_readv(sock->ssl, iov, 2);
    1524                 :            :         } else {
    1525                 :   23302655 :                 bytes_recvd = readv(sock->fd, iov, 2);
    1526                 :            :         }
    1527                 :            : 
    1528   [ -  +  -  + ]:   27597659 :         assert(sock->pipe_has_data == false);
    1529                 :            : 
    1530         [ +  + ]:   27597659 :         if (bytes_recvd <= 0) {
    1531                 :            :                 /* Errors count as draining the socket data */
    1532   [ +  +  -  +  :   19351371 :                 if (sock->base.group_impl && sock->socket_has_data) {
                   +  - ]
    1533                 :     232766 :                         group = __posix_group_impl(sock->base.group_impl);
    1534         [ +  + ]:     232766 :                         TAILQ_REMOVE(&group->socks_with_data, sock, link);
    1535                 :            :                 }
    1536                 :            : 
    1537                 :   19351371 :                 sock->socket_has_data = false;
    1538                 :            : 
    1539                 :   19351371 :                 return bytes_recvd;
    1540                 :            :         }
    1541                 :            : 
    1542                 :    8246288 :         spdk_pipe_writer_advance(sock->recv_pipe, bytes_recvd);
    1543                 :            : 
    1544                 :            : #if DEBUG
    1545         [ +  + ]:    8246288 :         if (sock->base.group_impl) {
    1546   [ -  +  -  + ]:    7872400 :                 assert(sock->socket_has_data == true);
    1547                 :            :         }
    1548                 :            : #endif
    1549                 :            : 
    1550                 :    8246288 :         sock->pipe_has_data = true;
    1551         [ +  + ]:    8246288 :         if (bytes_recvd < bytes_avail) {
    1552                 :            :                 /* We drained the kernel socket entirely. */
    1553                 :    6808989 :                 sock->socket_has_data = false;
    1554                 :            :         }
    1555                 :            : 
    1556                 :    8246288 :         return bytes_recvd;
    1557                 :            : }
    1558                 :            : 
    1559                 :            : static ssize_t
    1560                 :  119635837 : posix_sock_readv(struct spdk_sock *_sock, struct iovec *iov, int iovcnt)
    1561                 :            : {
    1562                 :  119635837 :         struct spdk_posix_sock *sock = __posix_sock(_sock);
    1563                 :  119635837 :         struct spdk_posix_sock_group_impl *group = __posix_group_impl(sock->base.group_impl);
    1564                 :            :         int rc, i;
    1565                 :            :         size_t len;
    1566                 :            : 
    1567         [ +  + ]:  119635837 :         if (sock->recv_pipe == NULL) {
    1568   [ -  +  -  + ]:   15902553 :                 assert(sock->pipe_has_data == false);
    1569   [ +  +  +  +  :   15902553 :                 if (group && sock->socket_has_data) {
                   +  + ]
    1570                 :       7391 :                         sock->socket_has_data = false;
    1571         [ +  + ]:       7391 :                         TAILQ_REMOVE(&group->socks_with_data, sock, link);
    1572                 :            :                 }
    1573         [ +  + ]:   15902553 :                 if (sock->ssl) {
    1574                 :    3206087 :                         return SSL_readv(sock->ssl, iov, iovcnt);
    1575                 :            :                 } else {
    1576                 :   12696466 :                         return readv(sock->fd, iov, iovcnt);
    1577                 :            :                 }
    1578                 :            :         }
    1579                 :            : 
    1580                 :            :         /* If the socket is not in a group, we must assume it always has
    1581                 :            :          * data waiting for us because it is not epolled */
    1582   [ -  +  +  +  :  103733284 :         if (!sock->pipe_has_data && (group == NULL || sock->socket_has_data)) {
          +  +  -  +  +  
                      + ]
    1583                 :            :                 /* If the user is receiving a sufficiently large amount of data,
    1584                 :            :                  * receive directly to their buffers. */
    1585                 :   34806723 :                 len = 0;
    1586         [ +  + ]:   71859787 :                 for (i = 0; i < iovcnt; i++) {
    1587                 :   37053064 :                         len += iov[i].iov_len;
    1588                 :            :                 }
    1589                 :            : 
    1590         [ +  + ]:   34806723 :                 if (len >= MIN_SOCK_PIPE_SIZE) {
    1591                 :            :                         /* TODO: Should this detect if kernel socket is drained? */
    1592         [ +  + ]:    7209064 :                         if (sock->ssl) {
    1593                 :    3172005 :                                 return SSL_readv(sock->ssl, iov, iovcnt);
    1594                 :            :                         } else {
    1595                 :    4037059 :                                 return readv(sock->fd, iov, iovcnt);
    1596                 :            :                         }
    1597                 :            :                 }
    1598                 :            : 
    1599                 :            :                 /* Otherwise, do a big read into our pipe */
    1600                 :   27597659 :                 rc = posix_sock_read(sock);
    1601         [ +  + ]:   27597659 :                 if (rc <= 0) {
    1602                 :   19351371 :                         return rc;
    1603                 :            :                 }
    1604                 :            :         }
    1605                 :            : 
    1606                 :   77172849 :         return posix_sock_recv_from_pipe(sock, iov, iovcnt);
    1607                 :            : }
    1608                 :            : 
    1609                 :            : static ssize_t
    1610                 :  117348244 : posix_sock_recv(struct spdk_sock *sock, void *buf, size_t len)
    1611                 :            : {
    1612                 :    1259347 :         struct iovec iov[1];
    1613                 :            : 
    1614                 :  117348244 :         iov[0].iov_base = buf;
    1615                 :  117348244 :         iov[0].iov_len = len;
    1616                 :            : 
    1617                 :  117348244 :         return posix_sock_readv(sock, iov, 1);
    1618                 :            : }
    1619                 :            : 
    1620                 :            : static ssize_t
    1621                 :        617 : posix_sock_writev(struct spdk_sock *_sock, struct iovec *iov, int iovcnt)
    1622                 :            : {
    1623                 :        617 :         struct spdk_posix_sock *sock = __posix_sock(_sock);
    1624                 :            :         int rc;
    1625                 :            : 
    1626                 :            :         /* In order to process a writev, we need to flush any asynchronous writes
    1627                 :            :          * first. */
    1628                 :        617 :         rc = _sock_flush(_sock);
    1629         [ -  + ]:        617 :         if (rc < 0) {
    1630                 :          0 :                 return rc;
    1631                 :            :         }
    1632                 :            : 
    1633         [ -  + ]:        617 :         if (!TAILQ_EMPTY(&_sock->queued_reqs)) {
    1634                 :            :                 /* We weren't able to flush all requests */
    1635                 :          0 :                 errno = EAGAIN;
    1636                 :          0 :                 return -1;
    1637                 :            :         }
    1638                 :            : 
    1639         [ +  + ]:        617 :         if (sock->ssl) {
    1640                 :        588 :                 return SSL_writev(sock->ssl, iov, iovcnt);
    1641                 :            :         } else {
    1642                 :         29 :                 return writev(sock->fd, iov, iovcnt);
    1643                 :            :         }
    1644                 :            : }
    1645                 :            : 
    1646                 :            : static int
    1647                 :         35 : posix_sock_recv_next(struct spdk_sock *_sock, void **buf, void **ctx)
    1648                 :            : {
    1649                 :         35 :         struct spdk_posix_sock *sock = __posix_sock(_sock);
    1650                 :          0 :         struct iovec iov;
    1651                 :            :         ssize_t rc;
    1652                 :            : 
    1653         [ -  + ]:         35 :         if (sock->recv_pipe != NULL) {
    1654                 :          0 :                 errno = ENOTSUP;
    1655                 :          0 :                 return -1;
    1656                 :            :         }
    1657                 :            : 
    1658                 :         35 :         iov.iov_len = spdk_sock_group_get_buf(_sock->group_impl->group, &iov.iov_base, ctx);
    1659         [ -  + ]:         35 :         if (iov.iov_len == 0) {
    1660                 :          0 :                 errno = ENOBUFS;
    1661                 :          0 :                 return -1;
    1662                 :            :         }
    1663                 :            : 
    1664                 :         35 :         rc = posix_sock_readv(_sock, &iov, 1);
    1665         [ +  + ]:         35 :         if (rc <= 0) {
    1666                 :         25 :                 spdk_sock_group_provide_buf(_sock->group_impl->group, iov.iov_base, iov.iov_len, *ctx);
    1667                 :         25 :                 return rc;
    1668                 :            :         }
    1669                 :            : 
    1670                 :         10 :         *buf = iov.iov_base;
    1671                 :            : 
    1672                 :         10 :         return rc;
    1673                 :            : }
    1674                 :            : 
    1675                 :            : static void
    1676                 :   31911172 : posix_sock_writev_async(struct spdk_sock *sock, struct spdk_sock_request *req)
    1677                 :            : {
    1678                 :            :         int rc;
    1679                 :            : 
    1680                 :   31911172 :         spdk_sock_request_queue(sock, req);
    1681                 :            : 
    1682                 :            :         /* If there are a sufficient number queued, just flush them out immediately. */
    1683         [ +  + ]:   31911172 :         if (sock->queued_iovcnt >= IOV_BATCH_SIZE) {
    1684                 :    2588613 :                 rc = _sock_flush(sock);
    1685   [ +  +  +  + ]:    2588613 :                 if (rc < 0 && errno != EAGAIN) {
    1686                 :         10 :                         spdk_sock_abort_requests(sock);
    1687                 :            :                 }
    1688                 :            :         }
    1689                 :   31911172 : }
    1690                 :            : 
    1691                 :            : static int
    1692                 :       5910 : posix_sock_set_recvlowat(struct spdk_sock *_sock, int nbytes)
    1693                 :            : {
    1694                 :       5910 :         struct spdk_posix_sock *sock = __posix_sock(_sock);
    1695                 :         89 :         int val;
    1696                 :            :         int rc;
    1697                 :            : 
    1698         [ -  + ]:       5910 :         assert(sock != NULL);
    1699                 :            : 
    1700                 :       5910 :         val = nbytes;
    1701                 :       5910 :         rc = setsockopt(sock->fd, SOL_SOCKET, SO_RCVLOWAT, &val, sizeof val);
    1702         [ -  + ]:       5910 :         if (rc != 0) {
    1703                 :          0 :                 return -1;
    1704                 :            :         }
    1705                 :       5910 :         return 0;
    1706                 :            : }
    1707                 :            : 
    1708                 :            : static bool
    1709                 :          3 : posix_sock_is_ipv6(struct spdk_sock *_sock)
    1710                 :            : {
    1711                 :          3 :         struct spdk_posix_sock *sock = __posix_sock(_sock);
    1712                 :          3 :         struct sockaddr_storage sa;
    1713                 :          3 :         socklen_t salen;
    1714                 :            :         int rc;
    1715                 :            : 
    1716         [ -  + ]:          3 :         assert(sock != NULL);
    1717                 :            : 
    1718                 :          3 :         memset(&sa, 0, sizeof sa);
    1719                 :          3 :         salen = sizeof sa;
    1720                 :          3 :         rc = getsockname(sock->fd, (struct sockaddr *) &sa, &salen);
    1721         [ -  + ]:          3 :         if (rc != 0) {
    1722                 :          0 :                 SPDK_ERRLOG("getsockname() failed (errno=%d)\n", errno);
    1723                 :          0 :                 return false;
    1724                 :            :         }
    1725                 :            : 
    1726                 :          3 :         return (sa.ss_family == AF_INET6);
    1727                 :            : }
    1728                 :            : 
    1729                 :            : static bool
    1730                 :       7355 : posix_sock_is_ipv4(struct spdk_sock *_sock)
    1731                 :            : {
    1732                 :       7355 :         struct spdk_posix_sock *sock = __posix_sock(_sock);
    1733                 :        183 :         struct sockaddr_storage sa;
    1734                 :        183 :         socklen_t salen;
    1735                 :            :         int rc;
    1736                 :            : 
    1737         [ -  + ]:       7355 :         assert(sock != NULL);
    1738                 :            : 
    1739                 :       7355 :         memset(&sa, 0, sizeof sa);
    1740                 :       7355 :         salen = sizeof sa;
    1741                 :       7355 :         rc = getsockname(sock->fd, (struct sockaddr *) &sa, &salen);
    1742         [ -  + ]:       7355 :         if (rc != 0) {
    1743                 :          0 :                 SPDK_ERRLOG("getsockname() failed (errno=%d)\n", errno);
    1744                 :          0 :                 return false;
    1745                 :            :         }
    1746                 :            : 
    1747                 :       7355 :         return (sa.ss_family == AF_INET);
    1748                 :            : }
    1749                 :            : 
    1750                 :            : static bool
    1751                 :     207423 : posix_sock_is_connected(struct spdk_sock *_sock)
    1752                 :            : {
    1753                 :     207423 :         struct spdk_posix_sock *sock = __posix_sock(_sock);
    1754                 :         95 :         uint8_t byte;
    1755                 :            :         int rc;
    1756                 :            : 
    1757                 :     207423 :         rc = recv(sock->fd, &byte, 1, MSG_PEEK);
    1758         [ +  + ]:     207423 :         if (rc == 0) {
    1759                 :     207389 :                 return false;
    1760                 :            :         }
    1761                 :            : 
    1762         [ +  + ]:         34 :         if (rc < 0) {
    1763   [ -  +  -  - ]:         31 :                 if (errno == EAGAIN || errno == EWOULDBLOCK) {
    1764                 :         31 :                         return true;
    1765                 :            :                 }
    1766                 :            : 
    1767                 :          0 :                 return false;
    1768                 :            :         }
    1769                 :            : 
    1770                 :          3 :         return true;
    1771                 :            : }
    1772                 :            : 
    1773                 :            : static struct spdk_sock_group_impl *
    1774                 :       5629 : posix_sock_group_impl_get_optimal(struct spdk_sock *_sock, struct spdk_sock_group_impl *hint)
    1775                 :            : {
    1776                 :       5629 :         struct spdk_posix_sock *sock = __posix_sock(_sock);
    1777                 :         89 :         struct spdk_sock_group_impl *group_impl;
    1778                 :            : 
    1779         [ +  + ]:       5629 :         if (sock->placement_id != -1) {
    1780                 :          5 :                 spdk_sock_map_lookup(&g_map, sock->placement_id, &group_impl, hint);
    1781                 :          5 :                 return group_impl;
    1782                 :            :         }
    1783                 :            : 
    1784                 :       5624 :         return NULL;
    1785                 :            : }
    1786                 :            : 
    1787                 :            : static struct spdk_sock_group_impl *
    1788                 :       4162 : _sock_group_impl_create(uint32_t enable_placement_id)
    1789                 :            : {
    1790                 :            :         struct spdk_posix_sock_group_impl *group_impl;
    1791                 :            :         int fd;
    1792                 :            : 
    1793                 :            : #if defined(SPDK_EPOLL)
    1794                 :       4162 :         fd = epoll_create1(0);
    1795                 :            : #elif defined(SPDK_KEVENT)
    1796                 :            :         fd = kqueue();
    1797                 :            : #endif
    1798         [ -  + ]:       4162 :         if (fd == -1) {
    1799                 :          0 :                 return NULL;
    1800                 :            :         }
    1801                 :            : 
    1802                 :       4162 :         group_impl = calloc(1, sizeof(*group_impl));
    1803         [ -  + ]:       4162 :         if (group_impl == NULL) {
    1804                 :          0 :                 SPDK_ERRLOG("group_impl allocation failed\n");
    1805                 :          0 :                 close(fd);
    1806                 :          0 :                 return NULL;
    1807                 :            :         }
    1808                 :            : 
    1809                 :       4162 :         group_impl->pipe_group = spdk_pipe_group_create();
    1810         [ -  + ]:       4162 :         if (group_impl->pipe_group == NULL) {
    1811                 :          0 :                 SPDK_ERRLOG("pipe_group allocation failed\n");
    1812                 :          0 :                 free(group_impl);
    1813                 :          0 :                 close(fd);
    1814                 :          0 :                 return NULL;
    1815                 :            :         }
    1816                 :            : 
    1817                 :       4162 :         group_impl->fd = fd;
    1818                 :       4162 :         TAILQ_INIT(&group_impl->socks_with_data);
    1819                 :       4162 :         group_impl->placement_id = -1;
    1820                 :            : 
    1821         [ -  + ]:       4162 :         if (enable_placement_id == PLACEMENT_CPU) {
    1822                 :          0 :                 spdk_sock_map_insert(&g_map, spdk_env_get_current_core(), &group_impl->base);
    1823                 :          0 :                 group_impl->placement_id = spdk_env_get_current_core();
    1824                 :            :         }
    1825                 :            : 
    1826                 :       4162 :         return &group_impl->base;
    1827                 :            : }
    1828                 :            : 
    1829                 :            : static struct spdk_sock_group_impl *
    1830                 :       2081 : posix_sock_group_impl_create(void)
    1831                 :            : {
    1832                 :       2081 :         return _sock_group_impl_create(g_posix_impl_opts.enable_placement_id);
    1833                 :            : }
    1834                 :            : 
    1835                 :            : static struct spdk_sock_group_impl *
    1836                 :       2081 : ssl_sock_group_impl_create(void)
    1837                 :            : {
    1838                 :       2081 :         return _sock_group_impl_create(g_ssl_impl_opts.enable_placement_id);
    1839                 :            : }
    1840                 :            : 
    1841                 :            : static void
    1842                 :          0 : posix_sock_mark(struct spdk_posix_sock_group_impl *group, struct spdk_posix_sock *sock,
    1843                 :            :                 int placement_id)
    1844                 :            : {
    1845                 :            : #if defined(SO_MARK)
    1846                 :            :         int rc;
    1847                 :            : 
    1848                 :          0 :         rc = setsockopt(sock->fd, SOL_SOCKET, SO_MARK,
    1849                 :            :                         &placement_id, sizeof(placement_id));
    1850         [ #  # ]:          0 :         if (rc != 0) {
    1851                 :            :                 /* Not fatal */
    1852                 :          0 :                 SPDK_ERRLOG("Error setting SO_MARK\n");
    1853                 :          0 :                 return;
    1854                 :            :         }
    1855                 :            : 
    1856                 :          0 :         rc = spdk_sock_map_insert(&g_map, placement_id, &group->base);
    1857         [ #  # ]:          0 :         if (rc != 0) {
    1858                 :            :                 /* Not fatal */
    1859                 :          0 :                 SPDK_ERRLOG("Failed to insert sock group into map: %d\n", rc);
    1860                 :          0 :                 return;
    1861                 :            :         }
    1862                 :            : 
    1863                 :          0 :         sock->placement_id = placement_id;
    1864                 :            : #endif
    1865                 :            : }
    1866                 :            : 
    1867                 :            : static void
    1868                 :          0 : posix_sock_update_mark(struct spdk_sock_group_impl *_group, struct spdk_sock *_sock)
    1869                 :            : {
    1870                 :          0 :         struct spdk_posix_sock_group_impl *group = __posix_group_impl(_group);
    1871                 :            : 
    1872         [ #  # ]:          0 :         if (group->placement_id == -1) {
    1873                 :          0 :                 group->placement_id = spdk_sock_map_find_free(&g_map);
    1874                 :            : 
    1875                 :            :                 /* If a free placement id is found, update existing sockets in this group */
    1876         [ #  # ]:          0 :                 if (group->placement_id != -1) {
    1877                 :            :                         struct spdk_sock  *sock, *tmp;
    1878                 :            : 
    1879         [ #  # ]:          0 :                         TAILQ_FOREACH_SAFE(sock, &_group->socks, link, tmp) {
    1880                 :          0 :                                 posix_sock_mark(group, __posix_sock(sock), group->placement_id);
    1881                 :            :                         }
    1882                 :            :                 }
    1883                 :            :         }
    1884                 :            : 
    1885         [ #  # ]:          0 :         if (group->placement_id != -1) {
    1886                 :            :                 /*
    1887                 :            :                  * group placement id is already determined for this poll group.
    1888                 :            :                  * Mark socket with group's placement id.
    1889                 :            :                  */
    1890                 :          0 :                 posix_sock_mark(group, __posix_sock(_sock), group->placement_id);
    1891                 :            :         }
    1892                 :          0 : }
    1893                 :            : 
    1894                 :            : static int
    1895                 :       6834 : posix_sock_group_impl_add_sock(struct spdk_sock_group_impl *_group, struct spdk_sock *_sock)
    1896                 :            : {
    1897                 :       6834 :         struct spdk_posix_sock_group_impl *group = __posix_group_impl(_group);
    1898                 :       6834 :         struct spdk_posix_sock *sock = __posix_sock(_sock);
    1899                 :            :         int rc;
    1900                 :            : 
    1901                 :            : #if defined(SPDK_EPOLL)
    1902                 :        150 :         struct epoll_event event;
    1903                 :            : 
    1904                 :       6834 :         memset(&event, 0, sizeof(event));
    1905                 :            :         /* EPOLLERR is always on even if we don't set it, but be explicit for clarity */
    1906                 :       6834 :         event.events = EPOLLIN | EPOLLERR;
    1907         [ -  + ]:       6834 :         if (spdk_interrupt_mode_is_enabled()) {
    1908                 :          0 :                 event.events |= EPOLLOUT;
    1909                 :            :         }
    1910                 :            : 
    1911                 :       6834 :         event.data.ptr = sock;
    1912                 :            : 
    1913                 :       6834 :         rc = epoll_ctl(group->fd, EPOLL_CTL_ADD, sock->fd, &event);
    1914                 :            : #elif defined(SPDK_KEVENT)
    1915                 :            :         struct kevent event;
    1916                 :            :         struct timespec ts = {0};
    1917                 :            : 
    1918                 :            :         EV_SET(&event, sock->fd, EVFILT_READ, EV_ADD, 0, 0, sock);
    1919                 :            : 
    1920                 :            :         rc = kevent(group->fd, &event, 1, NULL, 0, &ts);
    1921                 :            : #endif
    1922                 :            : 
    1923         [ -  + ]:       6834 :         if (rc != 0) {
    1924                 :          0 :                 return rc;
    1925                 :            :         }
    1926                 :            : 
    1927                 :            :         /* switched from another polling group due to scheduling */
    1928   [ +  +  -  + ]:       6834 :         if (spdk_unlikely(sock->recv_pipe != NULL  &&
    1929                 :            :                           (spdk_pipe_reader_bytes_available(sock->recv_pipe) > 0))) {
    1930                 :          0 :                 sock->pipe_has_data = true;
    1931                 :          0 :                 sock->socket_has_data = false;
    1932                 :          0 :                 TAILQ_INSERT_TAIL(&group->socks_with_data, sock, link);
    1933         [ +  + ]:       6834 :         } else if (sock->recv_pipe != NULL) {
    1934                 :        235 :                 rc = spdk_pipe_group_add(group->pipe_group, sock->recv_pipe);
    1935         [ -  + ]:        235 :                 assert(rc == 0);
    1936                 :            :         }
    1937                 :            : 
    1938         [ -  + ]:       6834 :         if (_sock->impl_opts.enable_placement_id == PLACEMENT_MARK) {
    1939                 :          0 :                 posix_sock_update_mark(_group, _sock);
    1940         [ +  + ]:       6834 :         } else if (sock->placement_id != -1) {
    1941                 :          5 :                 rc = spdk_sock_map_insert(&g_map, sock->placement_id, &group->base);
    1942         [ -  + ]:          5 :                 if (rc != 0) {
    1943                 :          0 :                         SPDK_ERRLOG("Failed to insert sock group into map: %d\n", rc);
    1944                 :            :                         /* Do not treat this as an error. The system will continue running. */
    1945                 :            :                 }
    1946                 :            :         }
    1947                 :            : 
    1948                 :       6834 :         return rc;
    1949                 :            : }
    1950                 :            : 
    1951                 :            : static int
    1952                 :       6834 : posix_sock_group_impl_remove_sock(struct spdk_sock_group_impl *_group, struct spdk_sock *_sock)
    1953                 :            : {
    1954                 :       6834 :         struct spdk_posix_sock_group_impl *group = __posix_group_impl(_group);
    1955                 :       6834 :         struct spdk_posix_sock *sock = __posix_sock(_sock);
    1956                 :            :         int rc;
    1957                 :            : 
    1958   [ +  +  +  +  :       6834 :         if (sock->pipe_has_data || sock->socket_has_data) {
             -  +  +  + ]
    1959         [ +  + ]:         28 :                 TAILQ_REMOVE(&group->socks_with_data, sock, link);
    1960                 :         28 :                 sock->pipe_has_data = false;
    1961                 :         28 :                 sock->socket_has_data = false;
    1962         [ +  + ]:       6806 :         } else if (sock->recv_pipe != NULL) {
    1963                 :       6726 :                 rc = spdk_pipe_group_remove(group->pipe_group, sock->recv_pipe);
    1964         [ -  + ]:       6726 :                 assert(rc == 0);
    1965                 :            :         }
    1966                 :            : 
    1967         [ +  + ]:       6834 :         if (sock->placement_id != -1) {
    1968                 :          5 :                 spdk_sock_map_release(&g_map, sock->placement_id);
    1969                 :            :         }
    1970                 :            : 
    1971                 :            : #if defined(SPDK_EPOLL)
    1972                 :        150 :         struct epoll_event event;
    1973                 :            : 
    1974                 :            :         /* Event parameter is ignored but some old kernel version still require it. */
    1975                 :       6834 :         rc = epoll_ctl(group->fd, EPOLL_CTL_DEL, sock->fd, &event);
    1976                 :            : #elif defined(SPDK_KEVENT)
    1977                 :            :         struct kevent event;
    1978                 :            :         struct timespec ts = {0};
    1979                 :            : 
    1980                 :            :         EV_SET(&event, sock->fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
    1981                 :            : 
    1982                 :            :         rc = kevent(group->fd, &event, 1, NULL, 0, &ts);
    1983                 :            :         if (rc == 0 && event.flags & EV_ERROR) {
    1984                 :            :                 rc = -1;
    1985                 :            :                 errno = event.data;
    1986                 :            :         }
    1987                 :            : #endif
    1988                 :            : 
    1989                 :       6834 :         spdk_sock_abort_requests(_sock);
    1990                 :            : 
    1991                 :       6834 :         return rc;
    1992                 :            : }
    1993                 :            : 
    1994                 :            : static int
    1995                 :  487162802 : posix_sock_group_impl_poll(struct spdk_sock_group_impl *_group, int max_events,
    1996                 :            :                            struct spdk_sock **socks)
    1997                 :            : {
    1998                 :  487162802 :         struct spdk_posix_sock_group_impl *group = __posix_group_impl(_group);
    1999                 :            :         struct spdk_sock *sock, *tmp;
    2000                 :            :         int num_events, i, rc;
    2001                 :            :         struct spdk_posix_sock *psock, *ptmp;
    2002                 :            : #if defined(SPDK_EPOLL)
    2003                 :   35261723 :         struct epoll_event events[MAX_EVENTS_PER_POLL];
    2004                 :            : #elif defined(SPDK_KEVENT)
    2005                 :            :         struct kevent events[MAX_EVENTS_PER_POLL];
    2006                 :            :         struct timespec ts = {0};
    2007                 :            : #endif
    2008                 :            : 
    2009                 :            : #ifdef SPDK_ZEROCOPY
    2010                 :            :         /* When all of the following conditions are met
    2011                 :            :          * - non-blocking socket
    2012                 :            :          * - zero copy is enabled
    2013                 :            :          * - interrupts suppressed (i.e. busy polling)
    2014                 :            :          * - the NIC tx queue is full at the time sendmsg() is called
    2015                 :            :          * - epoll_wait determines there is an EPOLLIN event for the socket
    2016                 :            :          * then we can get into a situation where data we've sent is queued
    2017                 :            :          * up in the kernel network stack, but interrupts have been suppressed
    2018                 :            :          * because other traffic is flowing so the kernel misses the signal
    2019                 :            :          * to flush the software tx queue. If there wasn't incoming data
    2020                 :            :          * pending on the socket, then epoll_wait would have been sufficient
    2021                 :            :          * to kick off the send operation, but since there is a pending event
    2022                 :            :          * epoll_wait does not trigger the necessary operation.
    2023                 :            :          *
    2024                 :            :          * We deal with this by checking for all of the above conditions and
    2025                 :            :          * additionally looking for EPOLLIN events that were not consumed from
    2026                 :            :          * the last poll loop. We take this to mean that the upper layer is
    2027                 :            :          * unable to consume them because it is blocked waiting for resources
    2028                 :            :          * to free up, and those resources are most likely freed in response
    2029                 :            :          * to a pending asynchronous write completing.
    2030                 :            :          *
    2031                 :            :          * Additionally, sockets that have the same placement_id actually share
    2032                 :            :          * an underlying hardware queue. That means polling one of them is
    2033                 :            :          * equivalent to polling all of them. As a quick mechanism to avoid
    2034                 :            :          * making extra poll() calls, stash the last placement_id during the loop
    2035                 :            :          * and only poll if it's not the same. The overwhelmingly common case
    2036                 :            :          * is that all sockets in this list have the same placement_id because
    2037                 :            :          * SPDK is intentionally grouping sockets by that value, so even
    2038                 :            :          * though this won't stop all extra calls to poll(), it's very fast
    2039                 :            :          * and will catch all of them in practice.
    2040                 :            :          */
    2041                 :  487162802 :         int last_placement_id = -1;
    2042                 :            : 
    2043         [ +  + ]:  492906424 :         TAILQ_FOREACH(psock, &group->socks_with_data, link) {
    2044   [ -  +  +  +  :    5743622 :                 if (psock->zcopy && psock->placement_id >= 0 &&
                   -  + ]
    2045         [ #  # ]:          0 :                     psock->placement_id != last_placement_id) {
    2046                 :          0 :                         struct pollfd pfd = {psock->fd, POLLIN | POLLERR, 0};
    2047                 :            : 
    2048                 :          0 :                         poll(&pfd, 1, 0);
    2049                 :          0 :                         last_placement_id = psock->placement_id;
    2050                 :            :                 }
    2051                 :            :         }
    2052                 :            : #endif
    2053                 :            : 
    2054                 :            :         /* This must be a TAILQ_FOREACH_SAFE because while flushing,
    2055                 :            :          * a completion callback could remove the sock from the
    2056                 :            :          * group. */
    2057         [ +  + ]: 1590348387 :         TAILQ_FOREACH_SAFE(sock, &_group->socks, link, tmp) {
    2058                 : 1103185585 :                 rc = _sock_flush(sock);
    2059   [ +  +  +  + ]: 1103185585 :                 if (rc < 0 && errno != EAGAIN) {
    2060                 :          9 :                         spdk_sock_abort_requests(sock);
    2061                 :            :                 }
    2062                 :            :         }
    2063                 :            : 
    2064         [ -  + ]:  487162802 :         assert(max_events > 0);
    2065                 :            : 
    2066                 :            : #if defined(SPDK_EPOLL)
    2067                 :  487162802 :         num_events = epoll_wait(group->fd, events, max_events, 0);
    2068                 :            : #elif defined(SPDK_KEVENT)
    2069                 :            :         num_events = kevent(group->fd, NULL, 0, events, max_events, &ts);
    2070                 :            : #endif
    2071                 :            : 
    2072         [ -  + ]:  487162802 :         if (num_events == -1) {
    2073                 :          0 :                 return -1;
    2074   [ +  +  +  + ]:  487162802 :         } else if (num_events == 0 && !TAILQ_EMPTY(&_group->socks)) {
    2075                 :  477890315 :                 sock = TAILQ_FIRST(&_group->socks);
    2076                 :  477890315 :                 psock = __posix_sock(sock);
    2077                 :            :                 /* poll() is called here to busy poll the queue associated with
    2078                 :            :                  * first socket in list and potentially reap incoming data.
    2079                 :            :                  */
    2080         [ +  + ]:  477890315 :                 if (sock->opts.priority) {
    2081                 :     148358 :                         struct pollfd pfd = {0, 0, 0};
    2082                 :            : 
    2083                 :     148358 :                         pfd.fd = psock->fd;
    2084                 :     148358 :                         pfd.events = POLLIN | POLLERR;
    2085                 :     148358 :                         poll(&pfd, 1, 0);
    2086                 :            :                 }
    2087                 :            :         }
    2088                 :            : 
    2089         [ +  + ]:  496788975 :         for (i = 0; i < num_events; i++) {
    2090                 :            : #if defined(SPDK_EPOLL)
    2091                 :    9626173 :                 sock = events[i].data.ptr;
    2092                 :    9626173 :                 psock = __posix_sock(sock);
    2093                 :            : 
    2094                 :            : #ifdef SPDK_ZEROCOPY
    2095         [ +  + ]:    9626173 :                 if (events[i].events & EPOLLERR) {
    2096                 :    1762878 :                         rc = _sock_check_zcopy(sock);
    2097                 :            :                         /* If the socket was closed or removed from
    2098                 :            :                          * the group in response to a send ack, don't
    2099                 :            :                          * add it to the array here. */
    2100   [ +  -  +  + ]:    1762878 :                         if (rc || sock->cb_fn == NULL) {
    2101                 :        235 :                                 continue;
    2102                 :            :                         }
    2103                 :            :                 }
    2104                 :            : #endif
    2105         [ +  + ]:    9625938 :                 if ((events[i].events & EPOLLIN) == 0) {
    2106                 :     425317 :                         continue;
    2107                 :            :                 }
    2108                 :            : 
    2109                 :            : #elif defined(SPDK_KEVENT)
    2110                 :            :                 sock = events[i].udata;
    2111                 :            :                 psock = __posix_sock(sock);
    2112                 :            : #endif
    2113                 :            : 
    2114                 :            :                 /* If the socket is not already in the list, add it now */
    2115   [ +  +  +  +  :    9200621 :                 if (!psock->socket_has_data && !psock->pipe_has_data) {
             -  +  +  + ]
    2116                 :    6709783 :                         TAILQ_INSERT_TAIL(&group->socks_with_data, psock, link);
    2117                 :            :                 }
    2118                 :    9200621 :                 psock->socket_has_data = true;
    2119                 :            :         }
    2120                 :            : 
    2121                 :  487162802 :         num_events = 0;
    2122                 :            : 
    2123         [ +  + ]:  499616206 :         TAILQ_FOREACH_SAFE(psock, &group->socks_with_data, link, ptmp) {
    2124         [ -  + ]:   12453404 :                 if (num_events == max_events) {
    2125                 :          0 :                         break;
    2126                 :            :                 }
    2127                 :            : 
    2128                 :            :                 /* If the socket's cb_fn is NULL, just remove it from the
    2129                 :            :                  * list and do not add it to socks array */
    2130         [ -  + ]:   12453404 :                 if (spdk_unlikely(psock->base.cb_fn == NULL)) {
    2131                 :          0 :                         psock->socket_has_data = false;
    2132                 :          0 :                         psock->pipe_has_data = false;
    2133         [ #  # ]:          0 :                         TAILQ_REMOVE(&group->socks_with_data, psock, link);
    2134                 :          0 :                         continue;
    2135                 :            :                 }
    2136                 :            : 
    2137                 :   12453404 :                 socks[num_events++] = &psock->base;
    2138                 :            :         }
    2139                 :            : 
    2140                 :            :         /* Cycle the has_data list so that each time we poll things aren't
    2141                 :            :          * in the same order. Say we have 6 sockets in the list, named as follows:
    2142                 :            :          * A B C D E F
    2143                 :            :          * And all 6 sockets had epoll events, but max_events is only 3. That means
    2144                 :            :          * psock currently points at D. We want to rearrange the list to the following:
    2145                 :            :          * D E F A B C
    2146                 :            :          *
    2147                 :            :          * The variables below are named according to this example to make it easier to
    2148                 :            :          * follow the swaps.
    2149                 :            :          */
    2150         [ -  + ]:  487162802 :         if (psock != NULL) {
    2151                 :            :                 struct spdk_posix_sock *pa, *pc, *pd, *pf;
    2152                 :            : 
    2153                 :            :                 /* Capture pointers to the elements we need */
    2154                 :          0 :                 pd = psock;
    2155                 :          0 :                 pc = TAILQ_PREV(pd, spdk_has_data_list, link);
    2156                 :          0 :                 pa = TAILQ_FIRST(&group->socks_with_data);
    2157                 :          0 :                 pf = TAILQ_LAST(&group->socks_with_data, spdk_has_data_list);
    2158                 :            : 
    2159                 :            :                 /* Break the link between C and D */
    2160                 :          0 :                 pc->link.tqe_next = NULL;
    2161                 :            : 
    2162                 :            :                 /* Connect F to A */
    2163                 :          0 :                 pf->link.tqe_next = pa;
    2164                 :          0 :                 pa->link.tqe_prev = &pf->link.tqe_next;
    2165                 :            : 
    2166                 :            :                 /* Fix up the list first/last pointers */
    2167                 :          0 :                 group->socks_with_data.tqh_first = pd;
    2168                 :          0 :                 group->socks_with_data.tqh_last = &pc->link.tqe_next;
    2169                 :            : 
    2170                 :            :                 /* D is in front of the list, make tqe prev pointer point to the head of list */
    2171                 :          0 :                 pd->link.tqe_prev = &group->socks_with_data.tqh_first;
    2172                 :            :         }
    2173                 :            : 
    2174                 :  487162802 :         return num_events;
    2175                 :            : }
    2176                 :            : 
    2177                 :            : static int
    2178                 :          0 : posix_sock_group_impl_register_interrupt(struct spdk_sock_group_impl *_group, uint32_t events,
    2179                 :            :                 spdk_interrupt_fn fn, void *arg, const char *name)
    2180                 :            : {
    2181                 :          0 :         struct spdk_posix_sock_group_impl *group = __posix_group_impl(_group);
    2182                 :            : 
    2183                 :          0 :         group->intr = spdk_interrupt_register_for_events(group->fd, events, fn, arg, name);
    2184                 :            : 
    2185         [ #  # ]:          0 :         return group->intr ? 0 : -1;
    2186                 :            : }
    2187                 :            : 
    2188                 :            : static void
    2189                 :          0 : posix_sock_group_impl_unregister_interrupt(struct spdk_sock_group_impl *_group)
    2190                 :            : {
    2191                 :          0 :         struct spdk_posix_sock_group_impl *group = __posix_group_impl(_group);
    2192                 :            : 
    2193                 :          0 :         spdk_interrupt_unregister(&group->intr);
    2194                 :          0 : }
    2195                 :            : 
    2196                 :            : static int
    2197                 :       4162 : _sock_group_impl_close(struct spdk_sock_group_impl *_group, uint32_t enable_placement_id)
    2198                 :            : {
    2199                 :       4162 :         struct spdk_posix_sock_group_impl *group = __posix_group_impl(_group);
    2200                 :            :         int rc;
    2201                 :            : 
    2202         [ -  + ]:       4162 :         if (enable_placement_id == PLACEMENT_CPU) {
    2203                 :          0 :                 spdk_sock_map_release(&g_map, spdk_env_get_current_core());
    2204                 :            :         }
    2205                 :            : 
    2206                 :       4162 :         spdk_pipe_group_destroy(group->pipe_group);
    2207                 :       4162 :         rc = close(group->fd);
    2208                 :       4162 :         free(group);
    2209                 :       4162 :         return rc;
    2210                 :            : }
    2211                 :            : 
    2212                 :            : static int
    2213                 :       2081 : posix_sock_group_impl_close(struct spdk_sock_group_impl *_group)
    2214                 :            : {
    2215                 :       2081 :         return _sock_group_impl_close(_group, g_posix_impl_opts.enable_placement_id);
    2216                 :            : }
    2217                 :            : 
    2218                 :            : static int
    2219                 :       2081 : ssl_sock_group_impl_close(struct spdk_sock_group_impl *_group)
    2220                 :            : {
    2221                 :       2081 :         return _sock_group_impl_close(_group, g_ssl_impl_opts.enable_placement_id);
    2222                 :            : }
    2223                 :            : 
    2224                 :            : static struct spdk_net_impl g_posix_net_impl = {
    2225                 :            :         .name           = "posix",
    2226                 :            :         .getaddr        = posix_sock_getaddr,
    2227                 :            :         .connect        = posix_sock_connect,
    2228                 :            :         .listen         = posix_sock_listen,
    2229                 :            :         .accept         = posix_sock_accept,
    2230                 :            :         .close          = posix_sock_close,
    2231                 :            :         .recv           = posix_sock_recv,
    2232                 :            :         .readv          = posix_sock_readv,
    2233                 :            :         .writev         = posix_sock_writev,
    2234                 :            :         .recv_next      = posix_sock_recv_next,
    2235                 :            :         .writev_async   = posix_sock_writev_async,
    2236                 :            :         .flush          = posix_sock_flush,
    2237                 :            :         .set_recvlowat  = posix_sock_set_recvlowat,
    2238                 :            :         .set_recvbuf    = posix_sock_set_recvbuf,
    2239                 :            :         .set_sendbuf    = posix_sock_set_sendbuf,
    2240                 :            :         .is_ipv6        = posix_sock_is_ipv6,
    2241                 :            :         .is_ipv4        = posix_sock_is_ipv4,
    2242                 :            :         .is_connected   = posix_sock_is_connected,
    2243                 :            :         .group_impl_get_optimal = posix_sock_group_impl_get_optimal,
    2244                 :            :         .group_impl_create      = posix_sock_group_impl_create,
    2245                 :            :         .group_impl_add_sock    = posix_sock_group_impl_add_sock,
    2246                 :            :         .group_impl_remove_sock = posix_sock_group_impl_remove_sock,
    2247                 :            :         .group_impl_poll        = posix_sock_group_impl_poll,
    2248                 :            :         .group_impl_register_interrupt     = posix_sock_group_impl_register_interrupt,
    2249                 :            :         .group_impl_unregister_interrupt  = posix_sock_group_impl_unregister_interrupt,
    2250                 :            :         .group_impl_close       = posix_sock_group_impl_close,
    2251                 :            :         .get_opts       = posix_sock_impl_get_opts,
    2252                 :            :         .set_opts       = posix_sock_impl_set_opts,
    2253                 :            : };
    2254                 :            : 
    2255                 :       2558 : SPDK_NET_IMPL_REGISTER_DEFAULT(posix, &g_posix_net_impl);
    2256                 :            : 
    2257                 :            : static struct spdk_sock *
    2258                 :         37 : ssl_sock_listen(const char *ip, int port, struct spdk_sock_opts *opts)
    2259                 :            : {
    2260                 :         37 :         return posix_sock_create(ip, port, SPDK_SOCK_CREATE_LISTEN, opts, true);
    2261                 :            : }
    2262                 :            : 
    2263                 :            : static struct spdk_sock *
    2264                 :        142 : ssl_sock_connect(const char *ip, int port, struct spdk_sock_opts *opts)
    2265                 :            : {
    2266                 :        142 :         return posix_sock_create(ip, port, SPDK_SOCK_CREATE_CONNECT, opts, true);
    2267                 :            : }
    2268                 :            : 
    2269                 :            : static struct spdk_sock *
    2270                 :      47483 : ssl_sock_accept(struct spdk_sock *_sock)
    2271                 :            : {
    2272                 :      47483 :         return _posix_sock_accept(_sock, true);
    2273                 :            : }
    2274                 :            : 
    2275                 :            : static struct spdk_net_impl g_ssl_net_impl = {
    2276                 :            :         .name           = "ssl",
    2277                 :            :         .getaddr        = posix_sock_getaddr,
    2278                 :            :         .connect        = ssl_sock_connect,
    2279                 :            :         .listen         = ssl_sock_listen,
    2280                 :            :         .accept         = ssl_sock_accept,
    2281                 :            :         .close          = posix_sock_close,
    2282                 :            :         .recv           = posix_sock_recv,
    2283                 :            :         .readv          = posix_sock_readv,
    2284                 :            :         .writev         = posix_sock_writev,
    2285                 :            :         .recv_next      = posix_sock_recv_next,
    2286                 :            :         .writev_async   = posix_sock_writev_async,
    2287                 :            :         .flush          = posix_sock_flush,
    2288                 :            :         .set_recvlowat  = posix_sock_set_recvlowat,
    2289                 :            :         .set_recvbuf    = posix_sock_set_recvbuf,
    2290                 :            :         .set_sendbuf    = posix_sock_set_sendbuf,
    2291                 :            :         .is_ipv6        = posix_sock_is_ipv6,
    2292                 :            :         .is_ipv4        = posix_sock_is_ipv4,
    2293                 :            :         .is_connected   = posix_sock_is_connected,
    2294                 :            :         .group_impl_get_optimal = posix_sock_group_impl_get_optimal,
    2295                 :            :         .group_impl_create      = ssl_sock_group_impl_create,
    2296                 :            :         .group_impl_add_sock    = posix_sock_group_impl_add_sock,
    2297                 :            :         .group_impl_remove_sock = posix_sock_group_impl_remove_sock,
    2298                 :            :         .group_impl_poll        = posix_sock_group_impl_poll,
    2299                 :            :         .group_impl_register_interrupt    = posix_sock_group_impl_register_interrupt,
    2300                 :            :         .group_impl_unregister_interrupt  = posix_sock_group_impl_unregister_interrupt,
    2301                 :            :         .group_impl_close       = ssl_sock_group_impl_close,
    2302                 :            :         .get_opts       = ssl_sock_impl_get_opts,
    2303                 :            :         .set_opts       = ssl_sock_impl_set_opts,
    2304                 :            : };
    2305                 :            : 
    2306                 :       2558 : SPDK_NET_IMPL_REGISTER(ssl, &g_ssl_net_impl);
    2307                 :       2558 : SPDK_LOG_REGISTER_COMPONENT(sock_posix)

Generated by: LCOV version 1.14