LCOV - code coverage report
Current view: top level - module/sock/posix - posix.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 398 978 40.7 %
Date: 2024-07-13 09:25:29 Functions: 42 60 70.0 %

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

Generated by: LCOV version 1.15