LCOV - code coverage report
Current view: top level - spdk/module/sock/posix - posix.c (source / functions) Hit Total Coverage
Test: Combined Lines: 756 1039 72.8 %
Date: 2024-07-15 22:31:11 Functions: 60 65 92.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 479 744 64.4 %

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

Generated by: LCOV version 1.14