LCOV - code coverage report
Current view: top level - spdk/module/sock/uring - uring.c (source / functions) Hit Total Coverage
Test: Combined Lines: 531 987 53.8 %
Date: 2024-08-12 07:19:58 Functions: 41 52 78.8 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 270 704 38.4 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2019 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/stdinc.h"
       7                 :            : #include "spdk/config.h"
       8                 :            : 
       9                 :            : #include <linux/errqueue.h>
      10                 :            : #include <sys/epoll.h>
      11                 :            : #include <liburing.h>
      12                 :            : 
      13                 :            : #include "spdk/barrier.h"
      14                 :            : #include "spdk/env.h"
      15                 :            : #include "spdk/log.h"
      16                 :            : #include "spdk/pipe.h"
      17                 :            : #include "spdk/sock.h"
      18                 :            : #include "spdk/string.h"
      19                 :            : #include "spdk/util.h"
      20                 :            : #include "spdk/net.h"
      21                 :            : #include "spdk/file.h"
      22                 :            : 
      23                 :            : #include "spdk_internal/sock.h"
      24                 :            : #include "spdk_internal/assert.h"
      25                 :            : #include "spdk/net.h"
      26                 :            : 
      27                 :            : #define MAX_TMPBUF 1024
      28                 :            : #define PORTNUMLEN 32
      29                 :            : #define SPDK_SOCK_GROUP_QUEUE_DEPTH 4096
      30                 :            : #define SPDK_SOCK_CMG_INFO_SIZE (sizeof(struct cmsghdr) + sizeof(struct sock_extended_err))
      31                 :            : 
      32                 :            : enum uring_task_type {
      33                 :            :         URING_TASK_READ = 0,
      34                 :            :         URING_TASK_ERRQUEUE,
      35                 :            :         URING_TASK_WRITE,
      36                 :            :         URING_TASK_CANCEL,
      37                 :            : };
      38                 :            : 
      39                 :            : #if defined(SO_ZEROCOPY) && defined(MSG_ZEROCOPY)
      40                 :            : #define SPDK_ZEROCOPY
      41                 :            : #endif
      42                 :            : 
      43                 :            : /* We don't know how big the buffers that the user posts will be, but this
      44                 :            :  * is the maximum we'll ever allow it to receive in a single command.
      45                 :            :  * If the user buffers are smaller, it will just receive less. */
      46                 :            : #define URING_MAX_RECV_SIZE (128 * 1024)
      47                 :            : 
      48                 :            : /* We don't know how many buffers the user will post, but this is the
      49                 :            :  * maximum number we'll take from the pool to post per group. */
      50                 :            : #define URING_BUF_POOL_SIZE 128
      51                 :            : 
      52                 :            : /* We use 1 just so it's not zero and we can validate it's right. */
      53                 :            : #define URING_BUF_GROUP_ID 1
      54                 :            : 
      55                 :            : enum spdk_uring_sock_task_status {
      56                 :            :         SPDK_URING_SOCK_TASK_NOT_IN_USE = 0,
      57                 :            :         SPDK_URING_SOCK_TASK_IN_PROCESS,
      58                 :            : };
      59                 :            : 
      60                 :            : struct spdk_uring_task {
      61                 :            :         enum spdk_uring_sock_task_status        status;
      62                 :            :         enum uring_task_type            type;
      63                 :            :         struct spdk_uring_sock                  *sock;
      64                 :            :         struct msghdr                           msg;
      65                 :            :         struct iovec                            iovs[IOV_BATCH_SIZE];
      66                 :            :         int                                     iov_cnt;
      67                 :            :         struct spdk_sock_request                *last_req;
      68                 :            :         bool                                    is_zcopy;
      69                 :            :         STAILQ_ENTRY(spdk_uring_task)           link;
      70                 :            : };
      71                 :            : 
      72                 :            : struct spdk_uring_sock {
      73                 :            :         struct spdk_sock                        base;
      74                 :            :         int                                     fd;
      75                 :            :         uint32_t                                sendmsg_idx;
      76                 :            :         struct spdk_uring_sock_group_impl       *group;
      77                 :            :         STAILQ_HEAD(, spdk_uring_buf_tracker)   recv_stream;
      78                 :            :         size_t                                  recv_offset;
      79                 :            :         struct spdk_uring_task                  write_task;
      80                 :            :         struct spdk_uring_task                  errqueue_task;
      81                 :            :         struct spdk_uring_task                  read_task;
      82                 :            :         struct spdk_uring_task                  cancel_task;
      83                 :            :         struct spdk_pipe                        *recv_pipe;
      84                 :            :         void                                    *recv_buf;
      85                 :            :         int                                     recv_buf_sz;
      86                 :            :         bool                                    zcopy;
      87                 :            :         bool                                    pending_recv;
      88                 :            :         bool                                    pending_group_remove;
      89                 :            :         int                                     zcopy_send_flags;
      90                 :            :         int                                     connection_status;
      91                 :            :         int                                     placement_id;
      92                 :            :         uint8_t                                 reserved[4];
      93                 :            :         uint8_t                                 buf[SPDK_SOCK_CMG_INFO_SIZE];
      94                 :            :         TAILQ_ENTRY(spdk_uring_sock)            link;
      95                 :            :         char                                    interface_name[IFNAMSIZ];
      96                 :            : };
      97                 :            : /* 'struct cmsghdr' is mapped to the buffer 'buf', and while first element
      98                 :            :  * of this control message header has a size of 8 bytes, 'buf'
      99                 :            :  * must be 8-byte aligned.
     100                 :            :  */
     101                 :            : SPDK_STATIC_ASSERT(offsetof(struct spdk_uring_sock, buf) % 8 == 0,
     102                 :            :                    "Incorrect alignment: `buf` must be aligned to 8 bytes");
     103                 :            : 
     104                 :            : TAILQ_HEAD(pending_recv_list, spdk_uring_sock);
     105                 :            : 
     106                 :            : struct spdk_uring_buf_tracker {
     107                 :            :         void                                    *buf;
     108                 :            :         size_t                                  buflen;
     109                 :            :         size_t                                  len;
     110                 :            :         void                                    *ctx;
     111                 :            :         int                                     id;
     112                 :            :         STAILQ_ENTRY(spdk_uring_buf_tracker)    link;
     113                 :            : };
     114                 :            : 
     115                 :            : struct spdk_uring_sock_group_impl {
     116                 :            :         struct spdk_sock_group_impl             base;
     117                 :            :         struct io_uring                         uring;
     118                 :            :         uint32_t                                io_inflight;
     119                 :            :         uint32_t                                io_queued;
     120                 :            :         uint32_t                                io_avail;
     121                 :            :         struct pending_recv_list                pending_recv;
     122                 :            : 
     123                 :            :         struct io_uring_buf_ring                *buf_ring;
     124                 :            :         uint32_t                                buf_ring_count;
     125                 :            :         struct spdk_uring_buf_tracker           *trackers;
     126                 :            :         STAILQ_HEAD(, spdk_uring_buf_tracker)   free_trackers;
     127                 :            : };
     128                 :            : 
     129                 :            : static struct spdk_sock_impl_opts g_spdk_uring_sock_impl_opts = {
     130                 :            :         .recv_buf_size = DEFAULT_SO_RCVBUF_SIZE,
     131                 :            :         .send_buf_size = DEFAULT_SO_SNDBUF_SIZE,
     132                 :            :         .enable_recv_pipe = true,
     133                 :            :         .enable_quickack = false,
     134                 :            :         .enable_placement_id = PLACEMENT_NONE,
     135                 :            :         .enable_zerocopy_send_server = false,
     136                 :            :         .enable_zerocopy_send_client = false,
     137                 :            :         .zerocopy_threshold = 0,
     138                 :            :         .tls_version = 0,
     139                 :            :         .enable_ktls = false,
     140                 :            :         .psk_key = NULL,
     141                 :            :         .psk_identity = NULL
     142                 :            : };
     143                 :            : 
     144                 :            : static struct spdk_sock_map g_map = {
     145                 :            :         .entries = STAILQ_HEAD_INITIALIZER(g_map.entries),
     146                 :            :         .mtx = PTHREAD_MUTEX_INITIALIZER
     147                 :            : };
     148                 :            : 
     149                 :            : __attribute((destructor)) static void
     150                 :        404 : uring_sock_map_cleanup(void)
     151                 :            : {
     152                 :        404 :         spdk_sock_map_cleanup(&g_map);
     153                 :        404 : }
     154                 :            : 
     155                 :            : #define SPDK_URING_SOCK_REQUEST_IOV(req) ((struct iovec *)((uint8_t *)req + sizeof(struct spdk_sock_request)))
     156                 :            : 
     157                 :            : #define __uring_sock(sock) (struct spdk_uring_sock *)sock
     158                 :            : #define __uring_group_impl(group) (struct spdk_uring_sock_group_impl *)group
     159                 :            : 
     160                 :            : static void
     161                 :         35 : uring_sock_copy_impl_opts(struct spdk_sock_impl_opts *dest, const struct spdk_sock_impl_opts *src,
     162                 :            :                           size_t len)
     163                 :            : {
     164                 :            : #define FIELD_OK(field) \
     165                 :            :         offsetof(struct spdk_sock_impl_opts, field) + sizeof(src->field) <= len
     166                 :            : 
     167                 :            : #define SET_FIELD(field) \
     168                 :            :         if (FIELD_OK(field)) { \
     169                 :            :                 dest->field = src->field; \
     170                 :            :         }
     171                 :            : 
     172         [ +  - ]:         35 :         SET_FIELD(recv_buf_size);
     173         [ +  - ]:         35 :         SET_FIELD(send_buf_size);
     174   [ +  -  -  + ]:         35 :         SET_FIELD(enable_recv_pipe);
     175   [ +  -  -  + ]:         35 :         SET_FIELD(enable_quickack);
     176         [ +  - ]:         35 :         SET_FIELD(enable_placement_id);
     177   [ +  -  -  + ]:         35 :         SET_FIELD(enable_zerocopy_send_server);
     178   [ +  -  -  + ]:         35 :         SET_FIELD(enable_zerocopy_send_client);
     179         [ +  - ]:         35 :         SET_FIELD(zerocopy_threshold);
     180         [ +  - ]:         35 :         SET_FIELD(tls_version);
     181   [ +  -  -  + ]:         35 :         SET_FIELD(enable_ktls);
     182         [ +  - ]:         35 :         SET_FIELD(psk_key);
     183         [ +  - ]:         35 :         SET_FIELD(psk_identity);
     184                 :            : 
     185                 :            : #undef SET_FIELD
     186                 :            : #undef FIELD_OK
     187                 :         35 : }
     188                 :            : 
     189                 :            : static int
     190                 :         26 : uring_sock_impl_get_opts(struct spdk_sock_impl_opts *opts, size_t *len)
     191                 :            : {
     192   [ +  -  -  + ]:         26 :         if (!opts || !len) {
     193                 :          0 :                 errno = EINVAL;
     194                 :          0 :                 return -1;
     195                 :            :         }
     196                 :            : 
     197         [ -  + ]:         26 :         assert(sizeof(*opts) >= *len);
     198         [ -  + ]:         26 :         memset(opts, 0, *len);
     199                 :            : 
     200                 :         26 :         uring_sock_copy_impl_opts(opts, &g_spdk_uring_sock_impl_opts, *len);
     201                 :         26 :         *len = spdk_min(*len, sizeof(g_spdk_uring_sock_impl_opts));
     202                 :            : 
     203                 :         26 :         return 0;
     204                 :            : }
     205                 :            : 
     206                 :            : static int
     207                 :          9 : uring_sock_impl_set_opts(const struct spdk_sock_impl_opts *opts, size_t len)
     208                 :            : {
     209         [ -  + ]:          9 :         if (!opts) {
     210                 :          0 :                 errno = EINVAL;
     211                 :          0 :                 return -1;
     212                 :            :         }
     213                 :            : 
     214         [ -  + ]:          9 :         assert(sizeof(*opts) >= len);
     215                 :          9 :         uring_sock_copy_impl_opts(&g_spdk_uring_sock_impl_opts, opts, len);
     216                 :            : 
     217                 :          9 :         return 0;
     218                 :            : }
     219                 :            : 
     220                 :            : static void
     221                 :        515 : uring_opts_get_impl_opts(const struct spdk_sock_opts *opts, struct spdk_sock_impl_opts *dest)
     222                 :            : {
     223                 :            :         /* Copy the default impl_opts first to cover cases when user's impl_opts is smaller */
     224   [ -  +  -  + ]:        515 :         memcpy(dest, &g_spdk_uring_sock_impl_opts, sizeof(*dest));
     225                 :            : 
     226         [ -  + ]:        515 :         if (opts->impl_opts != NULL) {
     227         [ #  # ]:          0 :                 assert(sizeof(*dest) >= opts->impl_opts_size);
     228                 :          0 :                 uring_sock_copy_impl_opts(dest, opts->impl_opts, opts->impl_opts_size);
     229                 :            :         }
     230                 :        515 : }
     231                 :            : 
     232                 :            : static int
     233                 :       1642 : uring_sock_getaddr(struct spdk_sock *_sock, char *saddr, int slen, uint16_t *sport,
     234                 :            :                    char *caddr, int clen, uint16_t *cport)
     235                 :            : {
     236                 :       1642 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
     237                 :            : 
     238         [ -  + ]:       1642 :         assert(sock != NULL);
     239                 :       1642 :         return spdk_net_getaddr(sock->fd, saddr, slen, sport, caddr, clen, cport);
     240                 :            : }
     241                 :            : 
     242                 :            : static const char *
     243                 :          0 : uring_sock_get_interface_name(struct spdk_sock *_sock)
     244                 :            : {
     245                 :          0 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
     246                 :          0 :         char saddr[64];
     247                 :            :         int rc;
     248                 :            : 
     249                 :          0 :         rc = spdk_net_getaddr(sock->fd, saddr, sizeof(saddr), NULL, NULL, 0, NULL);
     250         [ #  # ]:          0 :         if (rc != 0) {
     251                 :          0 :                 return NULL;
     252                 :            :         }
     253                 :            : 
     254                 :          0 :         rc = spdk_net_get_interface_name(saddr, sock->interface_name,
     255                 :            :                                          sizeof(sock->interface_name));
     256         [ #  # ]:          0 :         if (rc != 0) {
     257                 :          0 :                 return NULL;
     258                 :            :         }
     259                 :            : 
     260                 :          0 :         return sock->interface_name;
     261                 :            : }
     262                 :            : 
     263                 :            : static uint32_t
     264                 :          0 : uring_sock_get_numa_socket_id(struct spdk_sock *sock)
     265                 :            : {
     266                 :            :         const char *interface_name;
     267                 :          0 :         uint32_t numa_socket_id;
     268                 :            :         int rc;
     269                 :            : 
     270                 :          0 :         interface_name = uring_sock_get_interface_name(sock);
     271         [ #  # ]:          0 :         if (interface_name == NULL) {
     272                 :          0 :                 return SPDK_ENV_SOCKET_ID_ANY;
     273                 :            :         }
     274                 :            : 
     275                 :          0 :         rc = spdk_read_sysfs_attribute_uint32(&numa_socket_id,
     276                 :            :                                               "/sys/class/net/%s/device/numa_node", interface_name);
     277         [ #  # ]:          0 :         if (rc == 0) {
     278                 :          0 :                 return numa_socket_id;
     279                 :            :         } else {
     280                 :          0 :                 return SPDK_ENV_SOCKET_ID_ANY;
     281                 :            :         }
     282                 :            : }
     283                 :            : 
     284                 :            : enum uring_sock_create_type {
     285                 :            :         SPDK_SOCK_CREATE_LISTEN,
     286                 :            :         SPDK_SOCK_CREATE_CONNECT,
     287                 :            : };
     288                 :            : 
     289                 :            : static int
     290                 :       3235 : uring_sock_alloc_pipe(struct spdk_uring_sock *sock, int sz)
     291                 :            : {
     292                 :          0 :         uint8_t *new_buf;
     293                 :            :         struct spdk_pipe *new_pipe;
     294                 :          0 :         struct iovec siov[2];
     295                 :          0 :         struct iovec diov[2];
     296                 :            :         int sbytes;
     297                 :            :         ssize_t bytes;
     298                 :            :         int rc;
     299                 :            : 
     300         [ +  + ]:       3235 :         if (sock->recv_buf_sz == sz) {
     301                 :       1886 :                 return 0;
     302                 :            :         }
     303                 :            : 
     304                 :            :         /* If the new size is 0, just free the pipe */
     305         [ -  + ]:       1349 :         if (sz == 0) {
     306                 :          0 :                 spdk_pipe_destroy(sock->recv_pipe);
     307                 :          0 :                 free(sock->recv_buf);
     308                 :          0 :                 sock->recv_pipe = NULL;
     309                 :          0 :                 sock->recv_buf = NULL;
     310                 :          0 :                 return 0;
     311         [ -  + ]:       1349 :         } else if (sz < MIN_SOCK_PIPE_SIZE) {
     312                 :          0 :                 SPDK_ERRLOG("The size of the pipe must be larger than %d\n", MIN_SOCK_PIPE_SIZE);
     313                 :          0 :                 return -1;
     314                 :            :         }
     315                 :            : 
     316                 :            :         /* Round up to next 64 byte multiple */
     317         [ -  + ]:       1349 :         rc = posix_memalign((void **)&new_buf, 64, sz);
     318         [ -  + ]:       1349 :         if (rc != 0) {
     319                 :          0 :                 SPDK_ERRLOG("socket recv buf allocation failed\n");
     320                 :          0 :                 return -ENOMEM;
     321                 :            :         }
     322         [ -  + ]:       1349 :         memset(new_buf, 0, sz);
     323                 :            : 
     324                 :       1349 :         new_pipe = spdk_pipe_create(new_buf, sz);
     325         [ -  + ]:       1349 :         if (new_pipe == NULL) {
     326                 :          0 :                 SPDK_ERRLOG("socket pipe allocation failed\n");
     327                 :          0 :                 free(new_buf);
     328                 :          0 :                 return -ENOMEM;
     329                 :            :         }
     330                 :            : 
     331         [ -  + ]:       1349 :         if (sock->recv_pipe != NULL) {
     332                 :            :                 /* Pull all of the data out of the old pipe */
     333                 :          0 :                 sbytes = spdk_pipe_reader_get_buffer(sock->recv_pipe, sock->recv_buf_sz, siov);
     334         [ #  # ]:          0 :                 if (sbytes > sz) {
     335                 :            :                         /* Too much data to fit into the new pipe size */
     336                 :          0 :                         spdk_pipe_destroy(new_pipe);
     337                 :          0 :                         free(new_buf);
     338                 :          0 :                         return -EINVAL;
     339                 :            :                 }
     340                 :            : 
     341                 :          0 :                 sbytes = spdk_pipe_writer_get_buffer(new_pipe, sz, diov);
     342         [ #  # ]:          0 :                 assert(sbytes == sz);
     343                 :            : 
     344                 :          0 :                 bytes = spdk_iovcpy(siov, 2, diov, 2);
     345                 :          0 :                 spdk_pipe_writer_advance(new_pipe, bytes);
     346                 :            : 
     347                 :          0 :                 spdk_pipe_destroy(sock->recv_pipe);
     348                 :          0 :                 free(sock->recv_buf);
     349                 :            :         }
     350                 :            : 
     351                 :       1349 :         sock->recv_buf_sz = sz;
     352                 :       1349 :         sock->recv_buf = new_buf;
     353                 :       1349 :         sock->recv_pipe = new_pipe;
     354                 :            : 
     355                 :       1349 :         return 0;
     356                 :            : }
     357                 :            : 
     358                 :            : static int
     359                 :       3235 : uring_sock_set_recvbuf(struct spdk_sock *_sock, int sz)
     360                 :            : {
     361                 :       3235 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
     362                 :            :         int min_size;
     363                 :            :         int rc;
     364                 :            : 
     365         [ -  + ]:       3235 :         assert(sock != NULL);
     366                 :            : 
     367   [ -  +  +  - ]:       3235 :         if (_sock->impl_opts.enable_recv_pipe) {
     368                 :       3235 :                 rc = uring_sock_alloc_pipe(sock, sz);
     369         [ -  + ]:       3235 :                 if (rc) {
     370                 :          0 :                         SPDK_ERRLOG("unable to allocate sufficient recvbuf with sz=%d on sock=%p\n", sz, _sock);
     371                 :          0 :                         return rc;
     372                 :            :                 }
     373                 :            :         }
     374                 :            : 
     375                 :            :         /* Set kernel buffer size to be at least MIN_SO_RCVBUF_SIZE and
     376                 :            :          * g_spdk_uring_sock_impl_opts.recv_buf_size. */
     377                 :       3235 :         min_size = spdk_max(MIN_SO_RCVBUF_SIZE, g_spdk_uring_sock_impl_opts.recv_buf_size);
     378                 :            : 
     379         [ +  - ]:       3235 :         if (sz < min_size) {
     380                 :       3235 :                 sz = min_size;
     381                 :            :         }
     382                 :            : 
     383                 :       3235 :         rc = setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF, &sz, sizeof(sz));
     384         [ -  + ]:       3235 :         if (rc < 0) {
     385                 :          0 :                 return rc;
     386                 :            :         }
     387                 :            : 
     388                 :       3235 :         _sock->impl_opts.recv_buf_size = sz;
     389                 :            : 
     390                 :       3235 :         return 0;
     391                 :            : }
     392                 :            : 
     393                 :            : static int
     394                 :          0 : uring_sock_set_sendbuf(struct spdk_sock *_sock, int sz)
     395                 :            : {
     396                 :          0 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
     397                 :            :         int min_size;
     398                 :            :         int rc;
     399                 :            : 
     400         [ #  # ]:          0 :         assert(sock != NULL);
     401                 :            : 
     402                 :            :         /* Set kernel buffer size to be at least MIN_SO_SNDBUF_SIZE and
     403                 :            :          * g_spdk_uring_sock_impl_opts.seend_buf_size. */
     404                 :          0 :         min_size = spdk_max(MIN_SO_SNDBUF_SIZE, g_spdk_uring_sock_impl_opts.send_buf_size);
     405                 :            : 
     406         [ #  # ]:          0 :         if (sz < min_size) {
     407                 :          0 :                 sz = min_size;
     408                 :            :         }
     409                 :            : 
     410                 :          0 :         rc = setsockopt(sock->fd, SOL_SOCKET, SO_SNDBUF, &sz, sizeof(sz));
     411         [ #  # ]:          0 :         if (rc < 0) {
     412                 :          0 :                 return rc;
     413                 :            :         }
     414                 :            : 
     415                 :          0 :         _sock->impl_opts.send_buf_size = sz;
     416                 :            : 
     417                 :          0 :         return 0;
     418                 :            : }
     419                 :            : 
     420                 :            : static struct spdk_uring_sock *
     421                 :       1461 : uring_sock_alloc(int fd, struct spdk_sock_impl_opts *impl_opts, bool enable_zero_copy)
     422                 :            : {
     423                 :            :         struct spdk_uring_sock *sock;
     424                 :            : #if defined(__linux__)
     425                 :          0 :         int flag;
     426                 :            :         int rc;
     427                 :            : #endif
     428                 :            : 
     429                 :       1461 :         sock = calloc(1, sizeof(*sock));
     430         [ -  + ]:       1461 :         if (sock == NULL) {
     431                 :          0 :                 SPDK_ERRLOG("sock allocation failed\n");
     432                 :          0 :                 return NULL;
     433                 :            :         }
     434                 :            : 
     435                 :       1461 :         sock->fd = fd;
     436   [ -  +  -  + ]:       1461 :         memcpy(&sock->base.impl_opts, impl_opts, sizeof(*impl_opts));
     437                 :            : 
     438                 :       1461 :         STAILQ_INIT(&sock->recv_stream);
     439                 :            : 
     440                 :            : #if defined(__linux__)
     441                 :       1461 :         flag = 1;
     442                 :            : 
     443   [ -  +  -  + ]:       1461 :         if (sock->base.impl_opts.enable_quickack) {
     444                 :          0 :                 rc = setsockopt(sock->fd, IPPROTO_TCP, TCP_QUICKACK, &flag, sizeof(flag));
     445         [ #  # ]:          0 :                 if (rc != 0) {
     446                 :          0 :                         SPDK_ERRLOG("quickack was failed to set\n");
     447                 :            :                 }
     448                 :            :         }
     449                 :            : 
     450                 :       1461 :         spdk_sock_get_placement_id(sock->fd, sock->base.impl_opts.enable_placement_id,
     451                 :            :                                    &sock->placement_id);
     452                 :            : #ifdef SPDK_ZEROCOPY
     453                 :            :         /* Try to turn on zero copy sends */
     454                 :       1461 :         flag = 1;
     455                 :            : 
     456         [ -  + ]:       1461 :         if (enable_zero_copy) {
     457                 :          0 :                 rc = setsockopt(sock->fd, SOL_SOCKET, SO_ZEROCOPY, &flag, sizeof(flag));
     458         [ #  # ]:          0 :                 if (rc == 0) {
     459                 :          0 :                         sock->zcopy = true;
     460                 :          0 :                         sock->zcopy_send_flags = MSG_ZEROCOPY;
     461                 :            :                 }
     462                 :            :         }
     463                 :            : #endif
     464                 :            : #endif
     465                 :            : 
     466                 :       1461 :         return sock;
     467                 :            : }
     468                 :            : 
     469                 :            : static struct spdk_sock *
     470                 :        515 : uring_sock_create(const char *ip, int port,
     471                 :            :                   enum uring_sock_create_type type,
     472                 :            :                   struct spdk_sock_opts *opts)
     473                 :            : {
     474                 :            :         struct spdk_uring_sock *sock;
     475                 :          0 :         struct spdk_sock_impl_opts impl_opts;
     476                 :          0 :         char buf[MAX_TMPBUF];
     477                 :          0 :         char portnum[PORTNUMLEN];
     478                 :            :         char *p;
     479                 :            :         const char *src_addr;
     480                 :            :         uint16_t src_port;
     481                 :          0 :         struct addrinfo hints, *res, *res0, *src_ai;
     482                 :            :         int fd, flag;
     483                 :        515 :         int val = 1;
     484                 :            :         int rc;
     485                 :        515 :         bool enable_zcopy_impl_opts = false;
     486                 :        515 :         bool enable_zcopy_user_opts = true;
     487                 :            : 
     488         [ -  + ]:        515 :         assert(opts != NULL);
     489                 :        515 :         uring_opts_get_impl_opts(opts, &impl_opts);
     490                 :            : 
     491         [ -  + ]:        515 :         if (ip == NULL) {
     492                 :          0 :                 return NULL;
     493                 :            :         }
     494         [ -  + ]:        515 :         if (ip[0] == '[') {
     495                 :          0 :                 snprintf(buf, sizeof(buf), "%s", ip + 1);
     496                 :          0 :                 p = strchr(buf, ']');
     497         [ #  # ]:          0 :                 if (p != NULL) {
     498                 :          0 :                         *p = '\0';
     499                 :            :                 }
     500                 :          0 :                 ip = (const char *) &buf[0];
     501                 :            :         }
     502                 :            : 
     503                 :        515 :         snprintf(portnum, sizeof portnum, "%d", port);
     504                 :        515 :         memset(&hints, 0, sizeof hints);
     505                 :        515 :         hints.ai_family = PF_UNSPEC;
     506                 :        515 :         hints.ai_socktype = SOCK_STREAM;
     507                 :        515 :         hints.ai_flags = AI_NUMERICSERV;
     508                 :        515 :         hints.ai_flags |= AI_PASSIVE;
     509                 :        515 :         hints.ai_flags |= AI_NUMERICHOST;
     510                 :        515 :         rc = getaddrinfo(ip, portnum, &hints, &res0);
     511         [ -  + ]:        515 :         if (rc != 0) {
     512                 :          0 :                 SPDK_ERRLOG("getaddrinfo() failed %s (%d)\n", gai_strerror(rc), rc);
     513                 :          0 :                 return NULL;
     514                 :            :         }
     515                 :            : 
     516                 :            :         /* try listen */
     517                 :        515 :         fd = -1;
     518         [ +  + ]:        531 :         for (res = res0; res != NULL; res = res->ai_next) {
     519                 :        515 : retry:
     520                 :        515 :                 fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
     521         [ -  + ]:        515 :                 if (fd < 0) {
     522                 :            :                         /* error */
     523                 :          0 :                         continue;
     524                 :            :                 }
     525                 :            : 
     526                 :        515 :                 val = impl_opts.recv_buf_size;
     527                 :        515 :                 rc = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &val, sizeof val);
     528                 :            :                 if (rc) {
     529                 :            :                         /* Not fatal */
     530                 :            :                 }
     531                 :            : 
     532                 :        515 :                 val = impl_opts.send_buf_size;
     533                 :        515 :                 rc = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &val, sizeof val);
     534                 :            :                 if (rc) {
     535                 :            :                         /* Not fatal */
     536                 :            :                 }
     537                 :            : 
     538                 :        515 :                 rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof val);
     539         [ -  + ]:        515 :                 if (rc != 0) {
     540                 :          0 :                         close(fd);
     541                 :          0 :                         fd = -1;
     542                 :            :                         /* error */
     543                 :          0 :                         continue;
     544                 :            :                 }
     545                 :        515 :                 rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof val);
     546         [ -  + ]:        515 :                 if (rc != 0) {
     547                 :          0 :                         close(fd);
     548                 :          0 :                         fd = -1;
     549                 :            :                         /* error */
     550                 :          0 :                         continue;
     551                 :            :                 }
     552                 :            : 
     553         [ +  + ]:        515 :                 if (opts->ack_timeout) {
     554                 :            : #if defined(__linux__)
     555                 :         11 :                         val = opts->ack_timeout;
     556                 :         11 :                         rc = setsockopt(fd, IPPROTO_TCP, TCP_USER_TIMEOUT, &val, sizeof val);
     557         [ -  + ]:         11 :                         if (rc != 0) {
     558                 :          0 :                                 close(fd);
     559                 :          0 :                                 fd = -1;
     560                 :            :                                 /* error */
     561                 :          0 :                                 continue;
     562                 :            :                         }
     563                 :            : #else
     564                 :            :                         SPDK_WARNLOG("TCP_USER_TIMEOUT is not supported.\n");
     565                 :            : #endif
     566                 :            :                 }
     567                 :            : 
     568                 :            : 
     569                 :            : 
     570                 :            : #if defined(SO_PRIORITY)
     571   [ +  -  -  + ]:        515 :                 if (opts != NULL && opts->priority) {
     572                 :          0 :                         rc = setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &opts->priority, sizeof val);
     573         [ #  # ]:          0 :                         if (rc != 0) {
     574                 :          0 :                                 close(fd);
     575                 :          0 :                                 fd = -1;
     576                 :            :                                 /* error */
     577                 :          0 :                                 continue;
     578                 :            :                         }
     579                 :            :                 }
     580                 :            : #endif
     581         [ -  + ]:        515 :                 if (res->ai_family == AF_INET6) {
     582                 :          0 :                         rc = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof val);
     583         [ #  # ]:          0 :                         if (rc != 0) {
     584                 :          0 :                                 close(fd);
     585                 :          0 :                                 fd = -1;
     586                 :            :                                 /* error */
     587                 :          0 :                                 continue;
     588                 :            :                         }
     589                 :            :                 }
     590                 :            : 
     591         [ +  + ]:        515 :                 if (type == SPDK_SOCK_CREATE_LISTEN) {
     592                 :         80 :                         rc = bind(fd, res->ai_addr, res->ai_addrlen);
     593         [ -  + ]:         80 :                         if (rc != 0) {
     594                 :          0 :                                 SPDK_ERRLOG("bind() failed at port %d, errno = %d\n", port, errno);
     595      [ #  #  # ]:          0 :                                 switch (errno) {
     596                 :          0 :                                 case EINTR:
     597                 :            :                                         /* interrupted? */
     598                 :          0 :                                         close(fd);
     599                 :          0 :                                         goto retry;
     600                 :          0 :                                 case EADDRNOTAVAIL:
     601                 :          0 :                                         SPDK_ERRLOG("IP address %s not available. "
     602                 :            :                                                     "Verify IP address in config file "
     603                 :            :                                                     "and make sure setup script is "
     604                 :            :                                                     "run before starting spdk app.\n", ip);
     605                 :            :                                 /* FALLTHROUGH */
     606                 :          0 :                                 default:
     607                 :            :                                         /* try next family */
     608                 :          0 :                                         close(fd);
     609                 :          0 :                                         fd = -1;
     610                 :          0 :                                         continue;
     611                 :            :                                 }
     612                 :            :                         }
     613                 :            :                         /* bind OK */
     614                 :         80 :                         rc = listen(fd, 512);
     615         [ -  + ]:         80 :                         if (rc != 0) {
     616                 :          0 :                                 SPDK_ERRLOG("listen() failed, errno = %d\n", errno);
     617                 :          0 :                                 close(fd);
     618                 :          0 :                                 fd = -1;
     619                 :          0 :                                 break;
     620                 :            :                         }
     621                 :            : 
     622                 :         80 :                         flag = fcntl(fd, F_GETFL);
     623         [ -  + ]:         80 :                         if (fcntl(fd, F_SETFL, flag | O_NONBLOCK) < 0) {
     624                 :          0 :                                 SPDK_ERRLOG("fcntl can't set nonblocking mode for socket, fd: %d (%d)\n", fd, errno);
     625                 :          0 :                                 close(fd);
     626                 :          0 :                                 fd = -1;
     627                 :          0 :                                 break;
     628                 :            :                         }
     629                 :            : 
     630         [ -  + ]:         80 :                         enable_zcopy_impl_opts = impl_opts.enable_zerocopy_send_server;
     631         [ +  - ]:        435 :                 } else if (type == SPDK_SOCK_CREATE_CONNECT) {
     632         [ +  - ]:        435 :                         src_addr = SPDK_GET_FIELD(opts, src_addr, NULL, opts->opts_size);
     633         [ +  - ]:        435 :                         src_port = SPDK_GET_FIELD(opts, src_port, 0, opts->opts_size);
     634   [ +  -  -  + ]:        435 :                         if (src_addr != NULL || src_port != 0) {
     635                 :          0 :                                 snprintf(portnum, sizeof(portnum), "%"PRIu16, src_port);
     636                 :          0 :                                 memset(&hints, 0, sizeof hints);
     637                 :          0 :                                 hints.ai_family = AF_UNSPEC;
     638                 :          0 :                                 hints.ai_socktype = SOCK_STREAM;
     639                 :          0 :                                 hints.ai_flags = AI_NUMERICSERV | AI_NUMERICHOST | AI_PASSIVE;
     640         [ #  # ]:          0 :                                 rc = getaddrinfo(src_addr, src_port > 0 ? portnum : NULL,
     641                 :            :                                                  &hints, &src_ai);
     642   [ #  #  #  # ]:          0 :                                 if (rc != 0 || src_ai == NULL) {
     643         [ #  # ]:          0 :                                         SPDK_ERRLOG("getaddrinfo() failed %s (%d)\n",
     644                 :            :                                                     rc != 0 ? gai_strerror(rc) : "", rc);
     645                 :          0 :                                         close(fd);
     646                 :          0 :                                         fd = -1;
     647                 :          0 :                                         break;
     648                 :            :                                 }
     649                 :          0 :                                 rc = bind(fd, src_ai->ai_addr, src_ai->ai_addrlen);
     650         [ #  # ]:          0 :                                 if (rc != 0) {
     651         [ #  # ]:          0 :                                         SPDK_ERRLOG("bind() failed errno %d (%s:%s)\n", errno,
     652                 :            :                                                     src_addr ? src_addr : "", portnum);
     653                 :          0 :                                         close(fd);
     654                 :          0 :                                         fd = -1;
     655                 :          0 :                                         freeaddrinfo(src_ai);
     656                 :          0 :                                         src_ai = NULL;
     657                 :          0 :                                         break;
     658                 :            :                                 }
     659                 :          0 :                                 freeaddrinfo(src_ai);
     660                 :          0 :                                 src_ai = NULL;
     661                 :            :                         }
     662                 :            : 
     663                 :        435 :                         rc = connect(fd, res->ai_addr, res->ai_addrlen);
     664         [ +  + ]:        435 :                         if (rc != 0) {
     665                 :         16 :                                 SPDK_ERRLOG("connect() failed, errno = %d\n", errno);
     666                 :            :                                 /* try next family */
     667                 :         16 :                                 close(fd);
     668                 :         16 :                                 fd = -1;
     669                 :         16 :                                 continue;
     670                 :            :                         }
     671                 :            : 
     672                 :        419 :                         flag = fcntl(fd, F_GETFL);
     673         [ -  + ]:        419 :                         if (fcntl(fd, F_SETFL, flag & ~O_NONBLOCK) < 0) {
     674                 :          0 :                                 SPDK_ERRLOG("fcntl can't set blocking mode for socket, fd: %d (%d)\n", fd, errno);
     675                 :          0 :                                 close(fd);
     676                 :          0 :                                 fd = -1;
     677                 :          0 :                                 break;
     678                 :            :                         }
     679                 :            : 
     680         [ -  + ]:        419 :                         enable_zcopy_impl_opts = impl_opts.enable_zerocopy_send_client;
     681                 :            :                 }
     682                 :        499 :                 break;
     683                 :            :         }
     684                 :        515 :         freeaddrinfo(res0);
     685                 :            : 
     686         [ +  + ]:        515 :         if (fd < 0) {
     687                 :         16 :                 return NULL;
     688                 :            :         }
     689                 :            : 
     690   [ -  +  +  +  :        499 :         enable_zcopy_user_opts = opts->zcopy && !spdk_net_is_loopback(fd);
                   +  + ]
     691   [ +  +  -  + ]:        499 :         sock = uring_sock_alloc(fd, &impl_opts, enable_zcopy_user_opts && enable_zcopy_impl_opts);
     692         [ -  + ]:        499 :         if (sock == NULL) {
     693                 :          0 :                 SPDK_ERRLOG("sock allocation failed\n");
     694                 :          0 :                 close(fd);
     695                 :          0 :                 return NULL;
     696                 :            :         }
     697                 :            : 
     698                 :        499 :         return &sock->base;
     699                 :            : }
     700                 :            : 
     701                 :            : static struct spdk_sock *
     702                 :         80 : uring_sock_listen(const char *ip, int port, struct spdk_sock_opts *opts)
     703                 :            : {
     704         [ -  + ]:         80 :         if (spdk_interrupt_mode_is_enabled()) {
     705                 :          0 :                 SPDK_ERRLOG("Interrupt mode is not supported in the uring sock implementation.");
     706                 :          0 :                 return NULL;
     707                 :            :         }
     708                 :            : 
     709                 :         80 :         return uring_sock_create(ip, port, SPDK_SOCK_CREATE_LISTEN, opts);
     710                 :            : }
     711                 :            : 
     712                 :            : static struct spdk_sock *
     713                 :        435 : uring_sock_connect(const char *ip, int port, struct spdk_sock_opts *opts)
     714                 :            : {
     715         [ -  + ]:        435 :         if (spdk_interrupt_mode_is_enabled()) {
     716                 :          0 :                 SPDK_ERRLOG("Interrupt mode is not supported in the uring sock implementation.");
     717                 :          0 :                 return NULL;
     718                 :            :         }
     719                 :            : 
     720                 :        435 :         return uring_sock_create(ip, port, SPDK_SOCK_CREATE_CONNECT, opts);
     721                 :            : }
     722                 :            : 
     723                 :            : static struct spdk_sock *
     724                 :    1119773 : uring_sock_accept(struct spdk_sock *_sock)
     725                 :            : {
     726                 :    1119773 :         struct spdk_uring_sock          *sock = __uring_sock(_sock);
     727                 :          0 :         struct sockaddr_storage         sa;
     728                 :          0 :         socklen_t                       salen;
     729                 :            :         int                             rc, fd;
     730                 :            :         struct spdk_uring_sock          *new_sock;
     731                 :            :         int                             flag;
     732                 :            : 
     733                 :    1119773 :         memset(&sa, 0, sizeof(sa));
     734                 :    1119773 :         salen = sizeof(sa);
     735                 :            : 
     736         [ -  + ]:    1119773 :         assert(sock != NULL);
     737                 :            : 
     738                 :    1119773 :         rc = accept(sock->fd, (struct sockaddr *)&sa, &salen);
     739                 :            : 
     740         [ +  + ]:    1119773 :         if (rc == -1) {
     741                 :    1118811 :                 return NULL;
     742                 :            :         }
     743                 :            : 
     744                 :        962 :         fd = rc;
     745                 :            : 
     746                 :        962 :         flag = fcntl(fd, F_GETFL);
     747   [ -  +  -  - ]:        962 :         if ((flag & O_NONBLOCK) && (fcntl(fd, F_SETFL, flag & ~O_NONBLOCK) < 0)) {
     748                 :          0 :                 SPDK_ERRLOG("fcntl can't set blocking mode for socket, fd: %d (%d)\n", fd, errno);
     749                 :          0 :                 close(fd);
     750                 :          0 :                 return NULL;
     751                 :            :         }
     752                 :            : 
     753                 :            : #if defined(SO_PRIORITY)
     754                 :            :         /* The priority is not inherited, so call this function again */
     755         [ -  + ]:        962 :         if (sock->base.opts.priority) {
     756                 :          0 :                 rc = setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &sock->base.opts.priority, sizeof(int));
     757         [ #  # ]:          0 :                 if (rc != 0) {
     758                 :          0 :                         close(fd);
     759                 :          0 :                         return NULL;
     760                 :            :                 }
     761                 :            :         }
     762                 :            : #endif
     763                 :            : 
     764         [ -  + ]:        962 :         new_sock = uring_sock_alloc(fd, &sock->base.impl_opts, sock->zcopy);
     765         [ -  + ]:        962 :         if (new_sock == NULL) {
     766                 :          0 :                 close(fd);
     767                 :          0 :                 return NULL;
     768                 :            :         }
     769                 :            : 
     770                 :        962 :         return &new_sock->base;
     771                 :            : }
     772                 :            : 
     773                 :            : static int
     774                 :       1461 : uring_sock_close(struct spdk_sock *_sock)
     775                 :            : {
     776                 :       1461 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
     777                 :            : 
     778         [ -  + ]:       1461 :         assert(TAILQ_EMPTY(&_sock->pending_reqs));
     779         [ -  + ]:       1461 :         assert(sock->group == NULL);
     780                 :            : 
     781                 :            :         /* If the socket fails to close, the best choice is to
     782                 :            :          * leak the fd but continue to free the rest of the sock
     783                 :            :          * memory. */
     784                 :       1461 :         close(sock->fd);
     785                 :            : 
     786                 :       1461 :         spdk_pipe_destroy(sock->recv_pipe);
     787                 :       1461 :         free(sock->recv_buf);
     788                 :       1461 :         free(sock);
     789                 :            : 
     790                 :       1461 :         return 0;
     791                 :            : }
     792                 :            : 
     793                 :            : static ssize_t
     794                 :   40135826 : uring_sock_recv_from_pipe(struct spdk_uring_sock *sock, struct iovec *diov, int diovcnt)
     795                 :            : {
     796                 :          0 :         struct iovec siov[2];
     797                 :            :         int sbytes;
     798                 :            :         ssize_t bytes;
     799                 :            :         struct spdk_uring_sock_group_impl *group;
     800                 :            : 
     801                 :   40135826 :         sbytes = spdk_pipe_reader_get_buffer(sock->recv_pipe, sock->recv_buf_sz, siov);
     802         [ -  + ]:   40135826 :         if (sbytes < 0) {
     803                 :          0 :                 errno = EINVAL;
     804                 :          0 :                 return -1;
     805         [ -  + ]:   40135826 :         } else if (sbytes == 0) {
     806                 :          0 :                 errno = EAGAIN;
     807                 :          0 :                 return -1;
     808                 :            :         }
     809                 :            : 
     810                 :   40135826 :         bytes = spdk_iovcpy(siov, 2, diov, diovcnt);
     811                 :            : 
     812         [ -  + ]:   40135826 :         if (bytes == 0) {
     813                 :            :                 /* The only way this happens is if diov is 0 length */
     814                 :          0 :                 errno = EINVAL;
     815                 :          0 :                 return -1;
     816                 :            :         }
     817                 :            : 
     818                 :   40135826 :         spdk_pipe_reader_advance(sock->recv_pipe, bytes);
     819                 :            : 
     820                 :            :         /* If we drained the pipe, take it off the level-triggered list */
     821   [ +  +  +  + ]:   40135826 :         if (sock->base.group_impl && spdk_pipe_reader_bytes_available(sock->recv_pipe) == 0) {
     822                 :   10055057 :                 group = __uring_group_impl(sock->base.group_impl);
     823         [ +  + ]:   10055057 :                 TAILQ_REMOVE(&group->pending_recv, sock, link);
     824                 :   10055057 :                 sock->pending_recv = false;
     825                 :            :         }
     826                 :            : 
     827                 :   40135826 :         return bytes;
     828                 :            : }
     829                 :            : 
     830                 :            : static inline ssize_t
     831                 :  214863059 : sock_readv(int fd, struct iovec *iov, int iovcnt)
     832                 :            : {
     833                 :  214863059 :         struct msghdr msg = {
     834                 :            :                 .msg_iov = iov,
     835                 :            :                 .msg_iovlen = iovcnt,
     836                 :            :         };
     837                 :            : 
     838                 :  214863059 :         return recvmsg(fd, &msg, MSG_DONTWAIT);
     839                 :            : }
     840                 :            : 
     841                 :            : static inline ssize_t
     842                 :  206692074 : uring_sock_read(struct spdk_uring_sock *sock)
     843                 :            : {
     844                 :          0 :         struct iovec iov[2];
     845                 :            :         int bytes;
     846                 :            :         struct spdk_uring_sock_group_impl *group;
     847                 :            : 
     848                 :  206692074 :         bytes = spdk_pipe_writer_get_buffer(sock->recv_pipe, sock->recv_buf_sz, iov);
     849                 :            : 
     850         [ +  - ]:  206692074 :         if (bytes > 0) {
     851                 :  206692074 :                 bytes = sock_readv(sock->fd, iov, 2);
     852         [ +  + ]:  206692074 :                 if (bytes > 0) {
     853                 :   10059787 :                         spdk_pipe_writer_advance(sock->recv_pipe, bytes);
     854   [ +  +  -  +  :   10059787 :                         if (sock->base.group_impl && !sock->pending_recv) {
                   +  + ]
     855                 :    3371899 :                                 group = __uring_group_impl(sock->base.group_impl);
     856                 :    3371899 :                                 TAILQ_INSERT_TAIL(&group->pending_recv, sock, link);
     857                 :    3371899 :                                 sock->pending_recv = true;
     858                 :            :                         }
     859                 :            :                 }
     860                 :            :         }
     861                 :            : 
     862                 :  206692074 :         return bytes;
     863                 :            : }
     864                 :            : 
     865                 :            : static int
     866                 :          0 : uring_sock_recv_next(struct spdk_sock *_sock, void **_buf, void **ctx)
     867                 :            : {
     868                 :          0 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
     869                 :            :         struct spdk_uring_sock_group_impl *group;
     870                 :            :         struct spdk_uring_buf_tracker *tr;
     871                 :            : 
     872         [ #  # ]:          0 :         if (sock->connection_status < 0) {
     873                 :          0 :                 errno = -sock->connection_status;
     874                 :          0 :                 return -1;
     875                 :            :         }
     876                 :            : 
     877         [ #  # ]:          0 :         if (sock->recv_pipe != NULL) {
     878                 :          0 :                 errno = ENOTSUP;
     879                 :          0 :                 return -1;
     880                 :            :         }
     881                 :            : 
     882                 :          0 :         group = __uring_group_impl(_sock->group_impl);
     883                 :            : 
     884                 :          0 :         tr = STAILQ_FIRST(&sock->recv_stream);
     885         [ #  # ]:          0 :         if (tr == NULL) {
     886         [ #  # ]:          0 :                 if (sock->group->buf_ring_count > 0) {
     887                 :            :                         /* There are buffers posted, but data hasn't arrived. */
     888                 :          0 :                         errno = EAGAIN;
     889                 :            :                 } else {
     890                 :            :                         /* There are no buffers posted, so this won't ever
     891                 :            :                          * make forward progress. */
     892                 :          0 :                         errno = ENOBUFS;
     893                 :            :                 }
     894                 :          0 :                 return -1;
     895                 :            :         }
     896   [ #  #  #  # ]:          0 :         assert(sock->pending_recv == true);
     897         [ #  # ]:          0 :         assert(tr->buf != NULL);
     898                 :            : 
     899                 :          0 :         *_buf = tr->buf + sock->recv_offset;
     900                 :          0 :         *ctx = tr->ctx;
     901                 :            : 
     902         [ #  # ]:          0 :         STAILQ_REMOVE_HEAD(&sock->recv_stream, link);
     903         [ #  # ]:          0 :         STAILQ_INSERT_HEAD(&group->free_trackers, tr, link);
     904                 :            : 
     905         [ #  # ]:          0 :         if (STAILQ_EMPTY(&sock->recv_stream)) {
     906                 :          0 :                 sock->pending_recv = false;
     907         [ #  # ]:          0 :                 TAILQ_REMOVE(&group->pending_recv, sock, link);
     908                 :            :         }
     909                 :            : 
     910                 :          0 :         return tr->len - sock->recv_offset;
     911                 :            : }
     912                 :            : 
     913                 :            : static ssize_t
     914                 :     198885 : uring_sock_readv_no_pipe(struct spdk_sock *_sock, struct iovec *iovs, int iovcnt)
     915                 :            : {
     916                 :     198885 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
     917                 :            :         struct spdk_uring_buf_tracker *tr;
     918                 :            :         struct iovec iov;
     919                 :            :         ssize_t total, len;
     920                 :            :         int i;
     921                 :            : 
     922         [ -  + ]:     198885 :         if (sock->connection_status < 0) {
     923                 :          0 :                 errno = -sock->connection_status;
     924                 :          0 :                 return -1;
     925                 :            :         }
     926                 :            : 
     927         [ +  + ]:     198885 :         if (_sock->group_impl == NULL) {
     928                 :            :                 /* If not in a group just read from the socket the regular way. */
     929                 :      17677 :                 return sock_readv(sock->fd, iovs, iovcnt);
     930                 :            :         }
     931                 :            : 
     932         [ +  - ]:     181208 :         if (STAILQ_EMPTY(&sock->recv_stream)) {
     933         [ +  - ]:     181208 :                 if (sock->group->buf_ring_count == 0) {
     934                 :            :                         /* If the user hasn't posted any buffers, read from the socket
     935                 :            :                          * directly. */
     936                 :            : 
     937   [ -  +  +  + ]:     181208 :                         if (sock->pending_recv) {
     938                 :     178712 :                                 sock->pending_recv = false;
     939         [ +  + ]:     178712 :                                 TAILQ_REMOVE(&(__uring_group_impl(_sock->group_impl))->pending_recv, sock, link);
     940                 :            :                         }
     941                 :            : 
     942                 :     181208 :                         return sock_readv(sock->fd, iovs, iovcnt);
     943                 :            :                 }
     944                 :            : 
     945                 :          0 :                 errno = EAGAIN;
     946                 :          0 :                 return -1;
     947                 :            :         }
     948                 :            : 
     949                 :          0 :         total = 0;
     950         [ #  # ]:          0 :         for (i = 0; i < iovcnt; i++) {
     951                 :            :                 /* Copy to stack so we can change it */
     952                 :          0 :                 iov = iovs[i];
     953                 :            : 
     954                 :          0 :                 tr = STAILQ_FIRST(&sock->recv_stream);
     955         [ #  # ]:          0 :                 while (tr != NULL) {
     956                 :          0 :                         len = spdk_min(iov.iov_len, tr->len - sock->recv_offset);
     957   [ #  #  #  # ]:          0 :                         memcpy(iov.iov_base, tr->buf + sock->recv_offset, len);
     958                 :            : 
     959                 :          0 :                         total += len;
     960                 :          0 :                         sock->recv_offset += len;
     961                 :          0 :                         iov.iov_base += len;
     962                 :          0 :                         iov.iov_len -= len;
     963                 :            : 
     964         [ #  # ]:          0 :                         if (sock->recv_offset == tr->len) {
     965                 :          0 :                                 sock->recv_offset = 0;
     966         [ #  # ]:          0 :                                 STAILQ_REMOVE_HEAD(&sock->recv_stream, link);
     967         [ #  # ]:          0 :                                 STAILQ_INSERT_HEAD(&sock->group->free_trackers, tr, link);
     968                 :          0 :                                 spdk_sock_group_provide_buf(sock->group->base.group, tr->buf, tr->buflen, tr->ctx);
     969                 :          0 :                                 tr = STAILQ_FIRST(&sock->recv_stream);
     970                 :            :                         }
     971                 :            : 
     972         [ #  # ]:          0 :                         if (iov.iov_len == 0) {
     973                 :          0 :                                 break;
     974                 :            :                         }
     975                 :            :                 }
     976                 :            :         }
     977                 :            : 
     978         [ #  # ]:          0 :         if (STAILQ_EMPTY(&sock->recv_stream)) {
     979                 :            :                 struct spdk_uring_sock_group_impl *group;
     980                 :            : 
     981                 :          0 :                 group = __uring_group_impl(_sock->group_impl);
     982                 :          0 :                 sock->pending_recv = false;
     983         [ #  # ]:          0 :                 TAILQ_REMOVE(&group->pending_recv, sock, link);
     984                 :            :         }
     985                 :            : 
     986         [ #  # ]:          0 :         assert(total > 0);
     987                 :          0 :         return total;
     988                 :            : }
     989                 :            : 
     990                 :            : static ssize_t
     991                 :  244939098 : uring_sock_readv(struct spdk_sock *_sock, struct iovec *iov, int iovcnt)
     992                 :            : {
     993                 :  244939098 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
     994                 :            :         int rc, i;
     995                 :            :         size_t len;
     996                 :            : 
     997         [ -  + ]:  244939098 :         if (sock->connection_status < 0) {
     998                 :          0 :                 errno = -sock->connection_status;
     999                 :          0 :                 return -1;
    1000                 :            :         }
    1001                 :            : 
    1002         [ +  + ]:  244939098 :         if (sock->recv_pipe == NULL) {
    1003                 :     198885 :                 return uring_sock_readv_no_pipe(_sock, iov, iovcnt);
    1004                 :            :         }
    1005                 :            : 
    1006                 :  244740213 :         len = 0;
    1007         [ +  + ]:  491557153 :         for (i = 0; i < iovcnt; i++) {
    1008                 :  246816940 :                 len += iov[i].iov_len;
    1009                 :            :         }
    1010                 :            : 
    1011         [ +  + ]:  244740213 :         if (spdk_pipe_reader_bytes_available(sock->recv_pipe) == 0) {
    1012                 :            :                 /* If the user is receiving a sufficiently large amount of data,
    1013                 :            :                  * receive directly to their buffers. */
    1014         [ +  + ]:  214664174 :                 if (len >= MIN_SOCK_PIPE_SIZE) {
    1015                 :    7972100 :                         return sock_readv(sock->fd, iov, iovcnt);
    1016                 :            :                 }
    1017                 :            : 
    1018                 :            :                 /* Otherwise, do a big read into our pipe */
    1019                 :  206692074 :                 rc = uring_sock_read(sock);
    1020         [ +  + ]:  206692074 :                 if (rc <= 0) {
    1021                 :  196632287 :                         return rc;
    1022                 :            :                 }
    1023                 :            :         }
    1024                 :            : 
    1025                 :   40135826 :         return uring_sock_recv_from_pipe(sock, iov, iovcnt);
    1026                 :            : }
    1027                 :            : 
    1028                 :            : static ssize_t
    1029                 :  244365570 : uring_sock_recv(struct spdk_sock *sock, void *buf, size_t len)
    1030                 :            : {
    1031                 :          0 :         struct iovec iov[1];
    1032                 :            : 
    1033                 :  244365570 :         iov[0].iov_base = buf;
    1034                 :  244365570 :         iov[0].iov_len = len;
    1035                 :            : 
    1036                 :  244365570 :         return uring_sock_readv(sock, iov, 1);
    1037                 :            : }
    1038                 :            : 
    1039                 :            : static ssize_t
    1040                 :          0 : uring_sock_writev(struct spdk_sock *_sock, struct iovec *iov, int iovcnt)
    1041                 :            : {
    1042                 :          0 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
    1043                 :          0 :         struct msghdr msg = {
    1044                 :            :                 .msg_iov = iov,
    1045                 :            :                 .msg_iovlen = iovcnt,
    1046                 :            :         };
    1047                 :            : 
    1048         [ #  # ]:          0 :         if (sock->write_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE) {
    1049                 :          0 :                 errno = EAGAIN;
    1050                 :          0 :                 return -1;
    1051                 :            :         }
    1052                 :            : 
    1053                 :          0 :         return sendmsg(sock->fd, &msg, MSG_DONTWAIT);
    1054                 :            : }
    1055                 :            : 
    1056                 :            : static ssize_t
    1057                 :   22917853 : sock_request_advance_offset(struct spdk_sock_request *req, ssize_t rc)
    1058                 :            : {
    1059                 :            :         unsigned int offset;
    1060                 :            :         size_t len;
    1061                 :            :         int i;
    1062                 :            : 
    1063                 :   22917853 :         offset = req->internal.offset;
    1064         [ +  + ]:   63704848 :         for (i = 0; i < req->iovcnt; i++) {
    1065                 :            :                 /* Advance by the offset first */
    1066         [ +  + ]:   40916407 :                 if (offset >= SPDK_SOCK_REQUEST_IOV(req, i)->iov_len) {
    1067                 :     408406 :                         offset -= SPDK_SOCK_REQUEST_IOV(req, i)->iov_len;
    1068                 :     408406 :                         continue;
    1069                 :            :                 }
    1070                 :            : 
    1071                 :            :                 /* Calculate the remaining length of this element */
    1072                 :   40508001 :                 len = SPDK_SOCK_REQUEST_IOV(req, i)->iov_len - offset;
    1073                 :            : 
    1074         [ +  + ]:   40508001 :                 if (len > (size_t)rc) {
    1075                 :     129412 :                         req->internal.offset += rc;
    1076                 :     129412 :                         return -1;
    1077                 :            :                 }
    1078                 :            : 
    1079                 :   40378589 :                 offset = 0;
    1080                 :   40378589 :                 req->internal.offset += len;
    1081                 :   40378589 :                 rc -= len;
    1082                 :            :         }
    1083                 :            : 
    1084                 :   22788441 :         return rc;
    1085                 :            : }
    1086                 :            : 
    1087                 :            : static int
    1088                 :    6633406 : sock_complete_write_reqs(struct spdk_sock *_sock, ssize_t rc, bool is_zcopy)
    1089                 :            : {
    1090                 :    6633406 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
    1091                 :            :         struct spdk_sock_request *req;
    1092                 :            :         int retval;
    1093                 :            : 
    1094         [ -  + ]:    6633406 :         if (is_zcopy) {
    1095                 :            :                 /* Handling overflow case, because we use psock->sendmsg_idx - 1 for the
    1096                 :            :                  * req->internal.offset, so sendmsg_idx should not be zero */
    1097         [ #  # ]:          0 :                 if (spdk_unlikely(sock->sendmsg_idx == UINT32_MAX)) {
    1098                 :          0 :                         sock->sendmsg_idx = 1;
    1099                 :            :                 } else {
    1100                 :          0 :                         sock->sendmsg_idx++;
    1101                 :            :                 }
    1102                 :            :         }
    1103                 :            : 
    1104                 :            :         /* Consume the requests that were actually written */
    1105                 :    6633406 :         req = TAILQ_FIRST(&_sock->queued_reqs);
    1106         [ +  - ]:   22917853 :         while (req) {
    1107                 :            :                 /* req->internal.is_zcopy is true when the whole req or part of it is sent with zerocopy */
    1108                 :   22917853 :                 req->internal.is_zcopy = is_zcopy;
    1109                 :            : 
    1110                 :   22917853 :                 rc = sock_request_advance_offset(req, rc);
    1111         [ +  + ]:   22917853 :                 if (rc < 0) {
    1112                 :            :                         /* This element was partially sent. */
    1113                 :     129412 :                         return 0;
    1114                 :            :                 }
    1115                 :            : 
    1116                 :            :                 /* Handled a full request. */
    1117                 :   22788441 :                 spdk_sock_request_pend(_sock, req);
    1118                 :            : 
    1119   [ -  +  +  -  :   22788441 :                 if (!req->internal.is_zcopy && req == TAILQ_FIRST(&_sock->pending_reqs)) {
                   +  - ]
    1120                 :   22788441 :                         retval = spdk_sock_request_put(_sock, req, 0);
    1121         [ -  + ]:   22788441 :                         if (retval) {
    1122                 :          0 :                                 return retval;
    1123                 :            :                         }
    1124                 :            :                 } else {
    1125                 :            :                         /* Re-use the offset field to hold the sendmsg call index. The
    1126                 :            :                          * index is 0 based, so subtract one here because we've already
    1127                 :            :                          * incremented above. */
    1128                 :          0 :                         req->internal.offset = sock->sendmsg_idx - 1;
    1129                 :            :                 }
    1130                 :            : 
    1131         [ +  + ]:   22788441 :                 if (rc == 0) {
    1132                 :    6503994 :                         break;
    1133                 :            :                 }
    1134                 :            : 
    1135                 :   16284447 :                 req = TAILQ_FIRST(&_sock->queued_reqs);
    1136                 :            :         }
    1137                 :            : 
    1138                 :    6503994 :         return 0;
    1139                 :            : }
    1140                 :            : 
    1141                 :            : #ifdef SPDK_ZEROCOPY
    1142                 :            : static int
    1143                 :          0 : _sock_check_zcopy(struct spdk_sock *_sock, int status)
    1144                 :            : {
    1145                 :          0 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
    1146                 :            :         ssize_t rc;
    1147                 :            :         struct sock_extended_err *serr;
    1148                 :            :         struct cmsghdr *cm;
    1149                 :            :         uint32_t idx;
    1150                 :            :         struct spdk_sock_request *req, *treq;
    1151                 :            :         bool found;
    1152                 :            : 
    1153   [ #  #  #  # ]:          0 :         assert(sock->zcopy == true);
    1154         [ #  # ]:          0 :         if (spdk_unlikely(status) < 0) {
    1155         [ #  # ]:          0 :                 if (!TAILQ_EMPTY(&_sock->pending_reqs)) {
    1156                 :          0 :                         SPDK_ERRLOG("Attempting to receive from ERRQUEUE yielded error, but pending list still has orphaned entries, status =%d\n",
    1157                 :            :                                     status);
    1158                 :            :                 } else {
    1159                 :          0 :                         SPDK_WARNLOG("Recvmsg yielded an error!\n");
    1160                 :            :                 }
    1161                 :          0 :                 return 0;
    1162                 :            :         }
    1163                 :            : 
    1164         [ #  # ]:          0 :         cm = CMSG_FIRSTHDR(&sock->errqueue_task.msg);
    1165   [ #  #  #  # ]:          0 :         if (!((cm->cmsg_level == SOL_IP && cm->cmsg_type == IP_RECVERR) ||
    1166   [ #  #  #  # ]:          0 :               (cm->cmsg_level == SOL_IPV6 && cm->cmsg_type == IPV6_RECVERR))) {
    1167                 :          0 :                 SPDK_WARNLOG("Unexpected cmsg level or type!\n");
    1168                 :          0 :                 return 0;
    1169                 :            :         }
    1170                 :            : 
    1171                 :          0 :         serr = (struct sock_extended_err *)CMSG_DATA(cm);
    1172   [ #  #  #  # ]:          0 :         if (serr->ee_errno != 0 || serr->ee_origin != SO_EE_ORIGIN_ZEROCOPY) {
    1173                 :          0 :                 SPDK_WARNLOG("Unexpected extended error origin\n");
    1174                 :          0 :                 return 0;
    1175                 :            :         }
    1176                 :            : 
    1177                 :            :         /* Most of the time, the pending_reqs array is in the exact
    1178                 :            :          * order we need such that all of the requests to complete are
    1179                 :            :          * in order, in the front. It is guaranteed that all requests
    1180                 :            :          * belonging to the same sendmsg call are sequential, so once
    1181                 :            :          * we encounter one match we can stop looping as soon as a
    1182                 :            :          * non-match is found.
    1183                 :            :          */
    1184         [ #  # ]:          0 :         for (idx = serr->ee_info; idx <= serr->ee_data; idx++) {
    1185                 :          0 :                 found = false;
    1186         [ #  # ]:          0 :                 TAILQ_FOREACH_SAFE(req, &_sock->pending_reqs, internal.link, treq) {
    1187   [ #  #  #  # ]:          0 :                         if (!req->internal.is_zcopy) {
    1188                 :            :                                 /* This wasn't a zcopy request. It was just waiting in line to complete */
    1189                 :          0 :                                 rc = spdk_sock_request_put(_sock, req, 0);
    1190         [ #  # ]:          0 :                                 if (rc < 0) {
    1191                 :          0 :                                         return rc;
    1192                 :            :                                 }
    1193         [ #  # ]:          0 :                         } else if (req->internal.offset == idx) {
    1194                 :          0 :                                 found = true;
    1195                 :          0 :                                 rc = spdk_sock_request_put(_sock, req, 0);
    1196         [ #  # ]:          0 :                                 if (rc < 0) {
    1197                 :          0 :                                         return rc;
    1198                 :            :                                 }
    1199         [ #  # ]:          0 :                         } else if (found) {
    1200                 :          0 :                                 break;
    1201                 :            :                         }
    1202                 :            :                 }
    1203                 :            :         }
    1204                 :            : 
    1205                 :          0 :         return 0;
    1206                 :            : }
    1207                 :            : 
    1208                 :            : static void
    1209                 :          0 : _sock_prep_errqueue(struct spdk_sock *_sock)
    1210                 :            : {
    1211                 :          0 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
    1212                 :          0 :         struct spdk_uring_task *task = &sock->errqueue_task;
    1213                 :            :         struct io_uring_sqe *sqe;
    1214                 :            : 
    1215         [ #  # ]:          0 :         if (task->status == SPDK_URING_SOCK_TASK_IN_PROCESS) {
    1216                 :          0 :                 return;
    1217                 :            :         }
    1218                 :            : 
    1219   [ #  #  #  # ]:          0 :         if (sock->pending_group_remove) {
    1220                 :          0 :                 return;
    1221                 :            :         }
    1222                 :            : 
    1223         [ #  # ]:          0 :         assert(sock->group != NULL);
    1224                 :          0 :         sock->group->io_queued++;
    1225                 :            : 
    1226                 :          0 :         sqe = io_uring_get_sqe(&sock->group->uring);
    1227                 :          0 :         io_uring_prep_recvmsg(sqe, sock->fd, &task->msg, MSG_ERRQUEUE);
    1228                 :          0 :         io_uring_sqe_set_data(sqe, task);
    1229                 :          0 :         task->status = SPDK_URING_SOCK_TASK_IN_PROCESS;
    1230                 :            : }
    1231                 :            : 
    1232                 :            : #endif
    1233                 :            : 
    1234                 :            : static void
    1235                 :  203656821 : _sock_flush(struct spdk_sock *_sock)
    1236                 :            : {
    1237                 :  203656821 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
    1238                 :  203656821 :         struct spdk_uring_task *task = &sock->write_task;
    1239                 :            :         uint32_t iovcnt;
    1240                 :            :         struct io_uring_sqe *sqe;
    1241                 :          0 :         int flags;
    1242                 :            : 
    1243         [ -  + ]:  203656821 :         if (task->status == SPDK_URING_SOCK_TASK_IN_PROCESS) {
    1244                 :  196469700 :                 return;
    1245                 :            :         }
    1246                 :            : 
    1247                 :            : #ifdef SPDK_ZEROCOPY
    1248   [ -  +  -  + ]:  203656821 :         if (sock->zcopy) {
    1249                 :          0 :                 flags = MSG_DONTWAIT | sock->zcopy_send_flags;
    1250                 :            :         } else
    1251                 :            : #endif
    1252                 :            :         {
    1253                 :  203656821 :                 flags = MSG_DONTWAIT;
    1254                 :            :         }
    1255                 :            : 
    1256                 :  203656821 :         iovcnt = spdk_sock_prep_reqs(&sock->base, task->iovs, task->iov_cnt, &task->last_req, &flags);
    1257         [ +  + ]:  203656821 :         if (!iovcnt) {
    1258                 :  196469700 :                 return;
    1259                 :            :         }
    1260                 :            : 
    1261                 :    7187121 :         task->iov_cnt = iovcnt;
    1262         [ -  + ]:    7187121 :         assert(sock->group != NULL);
    1263                 :    7187121 :         task->msg.msg_iov = task->iovs;
    1264                 :    7187121 :         task->msg.msg_iovlen = task->iov_cnt;
    1265                 :            : #ifdef SPDK_ZEROCOPY
    1266                 :    7187121 :         task->is_zcopy = (flags & MSG_ZEROCOPY) ? true : false;
    1267                 :            : #endif
    1268                 :    7187121 :         sock->group->io_queued++;
    1269                 :            : 
    1270                 :    7187121 :         sqe = io_uring_get_sqe(&sock->group->uring);
    1271                 :    7187121 :         io_uring_prep_sendmsg(sqe, sock->fd, &sock->write_task.msg, flags);
    1272                 :    7187121 :         io_uring_sqe_set_data(sqe, task);
    1273                 :    7187121 :         task->status = SPDK_URING_SOCK_TASK_IN_PROCESS;
    1274                 :            : }
    1275                 :            : 
    1276                 :            : static void
    1277                 :  203663282 : _sock_prep_read(struct spdk_sock *_sock)
    1278                 :            : {
    1279                 :  203663282 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
    1280                 :  203663282 :         struct spdk_uring_task *task = &sock->read_task;
    1281                 :            :         struct io_uring_sqe *sqe;
    1282                 :            : 
    1283                 :            :         /* Do not prepare read event */
    1284         [ -  + ]:  203663282 :         if (task->status == SPDK_URING_SOCK_TASK_IN_PROCESS) {
    1285                 :          0 :                 return;
    1286                 :            :         }
    1287                 :            : 
    1288   [ -  +  +  + ]:  203663282 :         if (sock->pending_group_remove) {
    1289                 :       1445 :                 return;
    1290                 :            :         }
    1291                 :            : 
    1292         [ -  + ]:  203661837 :         assert(sock->group != NULL);
    1293                 :  203661837 :         sock->group->io_queued++;
    1294                 :            : 
    1295                 :  203661837 :         sqe = io_uring_get_sqe(&sock->group->uring);
    1296                 :  203661837 :         io_uring_prep_recv(sqe, sock->fd, NULL, URING_MAX_RECV_SIZE, 0);
    1297                 :  203661837 :         sqe->buf_group = URING_BUF_GROUP_ID;
    1298                 :  203661837 :         sqe->flags |= IOSQE_BUFFER_SELECT;
    1299                 :  203661837 :         io_uring_sqe_set_data(sqe, task);
    1300                 :  203661837 :         task->status = SPDK_URING_SOCK_TASK_IN_PROCESS;
    1301                 :            : }
    1302                 :            : 
    1303                 :            : static void
    1304                 :       1445 : _sock_prep_cancel_task(struct spdk_sock *_sock, void *user_data)
    1305                 :            : {
    1306                 :       1445 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
    1307                 :       1445 :         struct spdk_uring_task *task = &sock->cancel_task;
    1308                 :            :         struct io_uring_sqe *sqe;
    1309                 :            : 
    1310         [ -  + ]:       1445 :         if (task->status == SPDK_URING_SOCK_TASK_IN_PROCESS) {
    1311                 :          0 :                 return;
    1312                 :            :         }
    1313                 :            : 
    1314         [ -  + ]:       1445 :         assert(sock->group != NULL);
    1315                 :       1445 :         sock->group->io_queued++;
    1316                 :            : 
    1317                 :       1445 :         sqe = io_uring_get_sqe(&sock->group->uring);
    1318                 :       1445 :         io_uring_prep_cancel(sqe, user_data, 0);
    1319                 :       1445 :         io_uring_sqe_set_data(sqe, task);
    1320                 :       1445 :         task->status = SPDK_URING_SOCK_TASK_IN_PROCESS;
    1321                 :            : }
    1322                 :            : 
    1323                 :            : static void
    1324                 :          0 : uring_sock_fail(struct spdk_uring_sock *sock, int status)
    1325                 :            : {
    1326                 :          0 :         struct spdk_uring_sock_group_impl *group = sock->group;
    1327                 :            :         int rc;
    1328                 :            : 
    1329                 :          0 :         sock->connection_status = status;
    1330                 :          0 :         rc = spdk_sock_abort_requests(&sock->base);
    1331                 :            : 
    1332                 :            :         /* The user needs to be notified that this socket is dead. */
    1333   [ #  #  #  # ]:          0 :         if (rc == 0 && sock->base.cb_fn != NULL &&
    1334   [ #  #  #  # ]:          0 :             sock->pending_recv == false) {
    1335                 :          0 :                 sock->pending_recv = true;
    1336                 :          0 :                 TAILQ_INSERT_TAIL(&group->pending_recv, sock, link);
    1337                 :            :         }
    1338                 :          0 : }
    1339                 :            : 
    1340                 :            : static int
    1341                 :  135275295 : sock_uring_group_reap(struct spdk_uring_sock_group_impl *group, int max, int max_read_events,
    1342                 :            :                       struct spdk_sock **socks)
    1343                 :            : {
    1344                 :            :         int i, count, ret;
    1345                 :          0 :         struct io_uring_cqe *cqe;
    1346                 :            :         struct spdk_uring_sock *sock, *tmp;
    1347                 :            :         struct spdk_uring_task *task;
    1348                 :            :         int status, bid, flags;
    1349                 :            :         bool is_zcopy;
    1350                 :            : 
    1351         [ +  + ]:  346125698 :         for (i = 0; i < max; i++) {
    1352                 :  210850403 :                 ret = io_uring_peek_cqe(&group->uring, &cqe);
    1353         [ -  + ]:  210850403 :                 if (ret != 0) {
    1354                 :          0 :                         break;
    1355                 :            :                 }
    1356                 :            : 
    1357         [ -  + ]:  210850403 :                 if (cqe == NULL) {
    1358                 :          0 :                         break;
    1359                 :            :                 }
    1360                 :            : 
    1361                 :  210850403 :                 task = (struct spdk_uring_task *)cqe->user_data;
    1362         [ -  + ]:  210850403 :                 assert(task != NULL);
    1363                 :  210850403 :                 sock = task->sock;
    1364         [ -  + ]:  210850403 :                 assert(sock != NULL);
    1365         [ -  + ]:  210850403 :                 assert(sock->group != NULL);
    1366         [ -  + ]:  210850403 :                 assert(sock->group == group);
    1367                 :  210850403 :                 sock->group->io_inflight--;
    1368                 :  210850403 :                 sock->group->io_avail++;
    1369                 :  210850403 :                 status = cqe->res;
    1370                 :  210850403 :                 flags = cqe->flags;
    1371                 :  210850403 :                 io_uring_cqe_seen(&group->uring, cqe);
    1372                 :            : 
    1373                 :  210850403 :                 task->status = SPDK_URING_SOCK_TASK_NOT_IN_USE;
    1374                 :            : 
    1375   [ +  +  -  +  :  210850403 :                 switch (task->type) {
                      - ]
    1376                 :  203661837 :                 case URING_TASK_READ:
    1377   [ +  -  -  + ]:  203661837 :                         if (status == -EAGAIN || status == -EWOULDBLOCK) {
    1378                 :            :                                 /* This likely shouldn't happen, but would indicate that the
    1379                 :            :                                  * kernel didn't have enough resources to queue a task internally. */
    1380                 :          0 :                                 _sock_prep_read(&sock->base);
    1381         [ -  + ]:  203661837 :                         } else if (status == -ECANCELED) {
    1382                 :          0 :                                 continue;
    1383         [ +  - ]:  203661837 :                         } else if (status == -ENOBUFS) {
    1384                 :            :                                 /* There's data in the socket but the user hasn't provided any buffers.
    1385                 :            :                                  * We need to notify the user that the socket has data pending. */
    1386         [ +  - ]:  203661837 :                                 if (sock->base.cb_fn != NULL &&
    1387   [ -  +  +  + ]:  203661837 :                                     sock->pending_recv == false) {
    1388                 :    6863315 :                                         sock->pending_recv = true;
    1389                 :    6863315 :                                         TAILQ_INSERT_TAIL(&group->pending_recv, sock, link);
    1390                 :            :                                 }
    1391                 :            : 
    1392                 :  203661837 :                                 _sock_prep_read(&sock->base);
    1393         [ #  # ]:          0 :                         } else if (spdk_unlikely(status <= 0)) {
    1394         [ #  # ]:          0 :                                 uring_sock_fail(sock, status < 0 ? status : -ECONNRESET);
    1395                 :            :                         } else {
    1396                 :            :                                 struct spdk_uring_buf_tracker *tracker;
    1397                 :            : 
    1398         [ #  # ]:          0 :                                 assert((flags & IORING_CQE_F_BUFFER) != 0);
    1399                 :            : 
    1400                 :          0 :                                 bid = flags >> IORING_CQE_BUFFER_SHIFT;
    1401                 :          0 :                                 tracker = &group->trackers[bid];
    1402                 :            : 
    1403         [ #  # ]:          0 :                                 assert(tracker->buf != NULL);
    1404         [ #  # ]:          0 :                                 assert(tracker->len != 0);
    1405                 :            : 
    1406                 :            :                                 /* Append this data to the stream */
    1407                 :          0 :                                 tracker->len = status;
    1408                 :          0 :                                 STAILQ_INSERT_TAIL(&sock->recv_stream, tracker, link);
    1409         [ #  # ]:          0 :                                 assert(group->buf_ring_count > 0);
    1410                 :          0 :                                 group->buf_ring_count--;
    1411                 :            : 
    1412         [ #  # ]:          0 :                                 if (sock->base.cb_fn != NULL &&
    1413   [ #  #  #  # ]:          0 :                                     sock->pending_recv == false) {
    1414                 :          0 :                                         sock->pending_recv = true;
    1415                 :          0 :                                         TAILQ_INSERT_TAIL(&group->pending_recv, sock, link);
    1416                 :            :                                 }
    1417                 :            : 
    1418                 :          0 :                                 _sock_prep_read(&sock->base);
    1419                 :            :                         }
    1420                 :  203661837 :                         break;
    1421                 :    7187121 :                 case URING_TASK_WRITE:
    1422   [ +  +  +  -  :    7187121 :                         if (status == -EAGAIN || status == -EWOULDBLOCK ||
                   -  + ]
    1423   [ -  -  -  -  :    6627801 :                             (status == -ENOBUFS && sock->zcopy) ||
                   -  + ]
    1424                 :            :                             status == -ECANCELED) {
    1425                 :     559320 :                                 continue;
    1426         [ -  + ]:    6627801 :                         } else if (spdk_unlikely(status) < 0) {
    1427                 :          0 :                                 uring_sock_fail(sock, status);
    1428                 :            :                         } else {
    1429                 :    6627801 :                                 task->last_req = NULL;
    1430                 :    6627801 :                                 task->iov_cnt = 0;
    1431         [ -  + ]:    6627801 :                                 is_zcopy = task->is_zcopy;
    1432                 :    6627801 :                                 task->is_zcopy = false;
    1433                 :    6627801 :                                 sock_complete_write_reqs(&sock->base, status, is_zcopy);
    1434                 :            :                         }
    1435                 :            : 
    1436                 :    6627801 :                         break;
    1437                 :            : #ifdef SPDK_ZEROCOPY
    1438                 :          0 :                 case URING_TASK_ERRQUEUE:
    1439   [ #  #  #  # ]:          0 :                         if (status == -EAGAIN || status == -EWOULDBLOCK) {
    1440                 :          0 :                                 _sock_prep_errqueue(&sock->base);
    1441         [ #  # ]:          0 :                         } else if (status == -ECANCELED) {
    1442                 :          0 :                                 continue;
    1443         [ #  # ]:          0 :                         } else if (spdk_unlikely(status < 0)) {
    1444                 :          0 :                                 uring_sock_fail(sock, status);
    1445                 :            :                         } else {
    1446                 :          0 :                                 _sock_check_zcopy(&sock->base, status);
    1447                 :          0 :                                 _sock_prep_errqueue(&sock->base);
    1448                 :            :                         }
    1449                 :          0 :                         break;
    1450                 :            : #endif
    1451                 :       1445 :                 case URING_TASK_CANCEL:
    1452                 :            :                         /* Do nothing */
    1453                 :       1445 :                         break;
    1454                 :          0 :                 default:
    1455                 :          0 :                         SPDK_UNREACHABLE();
    1456                 :            :                 }
    1457                 :            :         }
    1458                 :            : 
    1459         [ +  + ]:  135275295 :         if (!socks) {
    1460                 :       1445 :                 return 0;
    1461                 :            :         }
    1462                 :  135273850 :         count = 0;
    1463         [ +  + ]:  338930436 :         TAILQ_FOREACH_SAFE(sock, &group->pending_recv, link, tmp) {
    1464         [ -  + ]:  203656586 :                 if (count == max_read_events) {
    1465                 :          0 :                         break;
    1466                 :            :                 }
    1467                 :            : 
    1468                 :            :                 /* If the socket's cb_fn is NULL, do not add it to socks array */
    1469         [ -  + ]:  203656586 :                 if (spdk_unlikely(sock->base.cb_fn == NULL)) {
    1470   [ #  #  #  # ]:          0 :                         assert(sock->pending_recv == true);
    1471                 :          0 :                         sock->pending_recv = false;
    1472         [ #  # ]:          0 :                         TAILQ_REMOVE(&group->pending_recv, sock, link);
    1473                 :          0 :                         continue;
    1474                 :            :                 }
    1475                 :            : 
    1476                 :  203656586 :                 socks[count++] = &sock->base;
    1477                 :            :         }
    1478                 :            : 
    1479                 :            : 
    1480                 :            :         /* Cycle the pending_recv list so that each time we poll things aren't
    1481                 :            :          * in the same order. Say we have 6 sockets in the list, named as follows:
    1482                 :            :          * A B C D E F
    1483                 :            :          * And all 6 sockets had the poll events, but max_events is only 3. That means
    1484                 :            :          * psock currently points at D. We want to rearrange the list to the following:
    1485                 :            :          * D E F A B C
    1486                 :            :          *
    1487                 :            :          * The variables below are named according to this example to make it easier to
    1488                 :            :          * follow the swaps.
    1489                 :            :          */
    1490         [ +  - ]:  135273850 :         if (sock != NULL) {
    1491                 :            :                 struct spdk_uring_sock *ua, *uc, *ud, *uf;
    1492                 :            : 
    1493                 :            :                 /* Capture pointers to the elements we need */
    1494                 :          0 :                 ud = sock;
    1495                 :            : 
    1496                 :          0 :                 ua = TAILQ_FIRST(&group->pending_recv);
    1497         [ #  # ]:          0 :                 if (ua == ud) {
    1498                 :          0 :                         goto end;
    1499                 :            :                 }
    1500                 :            : 
    1501                 :          0 :                 uf = TAILQ_LAST(&group->pending_recv, pending_recv_list);
    1502         [ #  # ]:          0 :                 if (uf == ud) {
    1503         [ #  # ]:          0 :                         TAILQ_REMOVE(&group->pending_recv, ud, link);
    1504         [ #  # ]:          0 :                         TAILQ_INSERT_HEAD(&group->pending_recv, ud, link);
    1505                 :          0 :                         goto end;
    1506                 :            :                 }
    1507                 :            : 
    1508                 :          0 :                 uc = TAILQ_PREV(ud, pending_recv_list, link);
    1509         [ #  # ]:          0 :                 assert(uc != NULL);
    1510                 :            : 
    1511                 :            :                 /* Break the link between C and D */
    1512                 :          0 :                 uc->link.tqe_next = NULL;
    1513                 :            : 
    1514                 :            :                 /* Connect F to A */
    1515                 :          0 :                 uf->link.tqe_next = ua;
    1516                 :          0 :                 ua->link.tqe_prev = &uf->link.tqe_next;
    1517                 :            : 
    1518                 :            :                 /* Fix up the list first/last pointers */
    1519                 :          0 :                 group->pending_recv.tqh_first = ud;
    1520                 :          0 :                 group->pending_recv.tqh_last = &uc->link.tqe_next;
    1521                 :            : 
    1522                 :            :                 /* D is in front of the list, make tqe prev pointer point to the head of list */
    1523                 :          0 :                 ud->link.tqe_prev = &group->pending_recv.tqh_first;
    1524                 :            :         }
    1525                 :            : 
    1526                 :  135273850 : end:
    1527                 :  135273850 :         return count;
    1528                 :            : }
    1529                 :            : 
    1530                 :            : static int uring_sock_flush(struct spdk_sock *_sock);
    1531                 :            : 
    1532                 :            : static void
    1533                 :   22788852 : uring_sock_writev_async(struct spdk_sock *_sock, struct spdk_sock_request *req)
    1534                 :            : {
    1535                 :   22788852 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
    1536                 :            :         int rc;
    1537                 :            : 
    1538         [ -  + ]:   22788852 :         if (spdk_unlikely(sock->connection_status)) {
    1539                 :          0 :                 req->cb_fn(req->cb_arg, sock->connection_status);
    1540                 :          0 :                 return;
    1541                 :            :         }
    1542                 :            : 
    1543                 :   22788852 :         spdk_sock_request_queue(_sock, req);
    1544                 :            : 
    1545         [ +  + ]:   22788852 :         if (!sock->group) {
    1546         [ -  + ]:       5862 :                 if (_sock->queued_iovcnt >= IOV_BATCH_SIZE) {
    1547                 :          0 :                         rc = uring_sock_flush(_sock);
    1548   [ #  #  #  # ]:          0 :                         if (rc < 0 && errno != EAGAIN) {
    1549                 :          0 :                                 spdk_sock_abort_requests(_sock);
    1550                 :            :                         }
    1551                 :            :                 }
    1552                 :            :         }
    1553                 :            : }
    1554                 :            : 
    1555                 :            : static int
    1556                 :        962 : uring_sock_set_recvlowat(struct spdk_sock *_sock, int nbytes)
    1557                 :            : {
    1558                 :        962 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
    1559                 :          0 :         int val;
    1560                 :            :         int rc;
    1561                 :            : 
    1562         [ -  + ]:        962 :         assert(sock != NULL);
    1563                 :            : 
    1564                 :        962 :         val = nbytes;
    1565                 :        962 :         rc = setsockopt(sock->fd, SOL_SOCKET, SO_RCVLOWAT, &val, sizeof val);
    1566         [ -  + ]:        962 :         if (rc != 0) {
    1567                 :          0 :                 return -1;
    1568                 :            :         }
    1569                 :        962 :         return 0;
    1570                 :            : }
    1571                 :            : 
    1572                 :            : static bool
    1573                 :          0 : uring_sock_is_ipv6(struct spdk_sock *_sock)
    1574                 :            : {
    1575                 :          0 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
    1576                 :          0 :         struct sockaddr_storage sa;
    1577                 :          0 :         socklen_t salen;
    1578                 :            :         int rc;
    1579                 :            : 
    1580         [ #  # ]:          0 :         assert(sock != NULL);
    1581                 :            : 
    1582                 :          0 :         memset(&sa, 0, sizeof sa);
    1583                 :          0 :         salen = sizeof sa;
    1584                 :          0 :         rc = getsockname(sock->fd, (struct sockaddr *) &sa, &salen);
    1585         [ #  # ]:          0 :         if (rc != 0) {
    1586                 :          0 :                 SPDK_ERRLOG("getsockname() failed (errno=%d)\n", errno);
    1587                 :          0 :                 return false;
    1588                 :            :         }
    1589                 :            : 
    1590                 :          0 :         return (sa.ss_family == AF_INET6);
    1591                 :            : }
    1592                 :            : 
    1593                 :            : static bool
    1594                 :       1229 : uring_sock_is_ipv4(struct spdk_sock *_sock)
    1595                 :            : {
    1596                 :       1229 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
    1597                 :          0 :         struct sockaddr_storage sa;
    1598                 :          0 :         socklen_t salen;
    1599                 :            :         int rc;
    1600                 :            : 
    1601         [ -  + ]:       1229 :         assert(sock != NULL);
    1602                 :            : 
    1603                 :       1229 :         memset(&sa, 0, sizeof sa);
    1604                 :       1229 :         salen = sizeof sa;
    1605                 :       1229 :         rc = getsockname(sock->fd, (struct sockaddr *) &sa, &salen);
    1606         [ -  + ]:       1229 :         if (rc != 0) {
    1607                 :          0 :                 SPDK_ERRLOG("getsockname() failed (errno=%d)\n", errno);
    1608                 :          0 :                 return false;
    1609                 :            :         }
    1610                 :            : 
    1611                 :       1229 :         return (sa.ss_family == AF_INET);
    1612                 :            : }
    1613                 :            : 
    1614                 :            : static bool
    1615                 :      77523 : uring_sock_is_connected(struct spdk_sock *_sock)
    1616                 :            : {
    1617                 :      77523 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
    1618                 :          0 :         uint8_t byte;
    1619                 :            :         int rc;
    1620                 :            : 
    1621                 :      77523 :         rc = recv(sock->fd, &byte, 1, MSG_PEEK | MSG_DONTWAIT);
    1622         [ +  - ]:      77523 :         if (rc == 0) {
    1623                 :      77523 :                 return false;
    1624                 :            :         }
    1625                 :            : 
    1626         [ #  # ]:          0 :         if (rc < 0) {
    1627   [ #  #  #  # ]:          0 :                 if (errno == EAGAIN || errno == EWOULDBLOCK) {
    1628                 :          0 :                         return true;
    1629                 :            :                 }
    1630                 :            : 
    1631                 :          0 :                 return false;
    1632                 :            :         }
    1633                 :            : 
    1634                 :          0 :         return true;
    1635                 :            : }
    1636                 :            : 
    1637                 :            : static struct spdk_sock_group_impl *
    1638                 :        680 : uring_sock_group_impl_get_optimal(struct spdk_sock *_sock, struct spdk_sock_group_impl *hint)
    1639                 :            : {
    1640                 :        680 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
    1641                 :          0 :         struct spdk_sock_group_impl *group;
    1642                 :            : 
    1643         [ -  + ]:        680 :         if (sock->placement_id != -1) {
    1644                 :          0 :                 spdk_sock_map_lookup(&g_map, sock->placement_id, &group, hint);
    1645                 :          0 :                 return group;
    1646                 :            :         }
    1647                 :            : 
    1648                 :        680 :         return NULL;
    1649                 :            : }
    1650                 :            : 
    1651                 :            : static int
    1652                 :        865 : uring_sock_group_impl_buf_pool_free(struct spdk_uring_sock_group_impl *group_impl)
    1653                 :            : {
    1654         [ +  - ]:        865 :         if (group_impl->buf_ring) {
    1655                 :        865 :                 io_uring_unregister_buf_ring(&group_impl->uring, URING_BUF_GROUP_ID);
    1656                 :        865 :                 free(group_impl->buf_ring);
    1657                 :            :         }
    1658                 :            : 
    1659                 :        865 :         free(group_impl->trackers);
    1660                 :            : 
    1661                 :        865 :         return 0;
    1662                 :            : }
    1663                 :            : 
    1664                 :            : static int
    1665                 :        867 : uring_sock_group_impl_buf_pool_alloc(struct spdk_uring_sock_group_impl *group_impl)
    1666                 :            : {
    1667                 :        867 :         struct io_uring_buf_reg buf_reg = {};
    1668                 :          1 :         struct io_uring_buf_ring *buf_ring;
    1669                 :            :         int i, rc;
    1670                 :            : 
    1671         [ -  + ]:        867 :         rc = posix_memalign((void **)&buf_ring, 0x1000, URING_BUF_POOL_SIZE * sizeof(struct io_uring_buf));
    1672         [ -  + ]:        867 :         if (rc != 0) {
    1673                 :            :                 /* posix_memalign returns positive errno values */
    1674                 :          0 :                 return -rc;
    1675                 :            :         }
    1676                 :            : 
    1677                 :        867 :         buf_reg.ring_addr = (unsigned long long)buf_ring;
    1678                 :        867 :         buf_reg.ring_entries = URING_BUF_POOL_SIZE;
    1679                 :        867 :         buf_reg.bgid = URING_BUF_GROUP_ID;
    1680                 :            : 
    1681                 :        867 :         rc = io_uring_register_buf_ring(&group_impl->uring, &buf_reg, 0);
    1682         [ +  + ]:        867 :         if (rc != 0) {
    1683                 :          2 :                 free(buf_ring);
    1684                 :          2 :                 return rc;
    1685                 :            :         }
    1686                 :            : 
    1687                 :        865 :         group_impl->buf_ring = buf_ring;
    1688                 :        865 :         io_uring_buf_ring_init(group_impl->buf_ring);
    1689                 :        865 :         group_impl->buf_ring_count = 0;
    1690                 :            : 
    1691                 :        865 :         group_impl->trackers = calloc(URING_BUF_POOL_SIZE, sizeof(struct spdk_uring_buf_tracker));
    1692         [ -  + ]:        865 :         if (group_impl->trackers == NULL) {
    1693                 :          0 :                 uring_sock_group_impl_buf_pool_free(group_impl);
    1694                 :          0 :                 return -ENOMEM;
    1695                 :            :         }
    1696                 :            : 
    1697                 :        865 :         STAILQ_INIT(&group_impl->free_trackers);
    1698                 :            : 
    1699         [ +  + ]:     111585 :         for (i = 0; i < URING_BUF_POOL_SIZE; i++) {
    1700                 :     110720 :                 struct spdk_uring_buf_tracker *tracker = &group_impl->trackers[i];
    1701                 :            : 
    1702                 :     110720 :                 tracker->buf = NULL;
    1703                 :     110720 :                 tracker->len = 0;
    1704                 :     110720 :                 tracker->ctx = NULL;
    1705                 :     110720 :                 tracker->id = i;
    1706                 :            : 
    1707                 :     110720 :                 STAILQ_INSERT_TAIL(&group_impl->free_trackers, tracker, link);
    1708                 :            :         }
    1709                 :            : 
    1710                 :        865 :         return 0;
    1711                 :            : }
    1712                 :            : 
    1713                 :            : static struct spdk_sock_group_impl *
    1714                 :        867 : uring_sock_group_impl_create(void)
    1715                 :            : {
    1716                 :            :         struct spdk_uring_sock_group_impl *group_impl;
    1717                 :            : 
    1718                 :        867 :         group_impl = calloc(1, sizeof(*group_impl));
    1719         [ -  + ]:        867 :         if (group_impl == NULL) {
    1720                 :          0 :                 SPDK_ERRLOG("group_impl allocation failed\n");
    1721                 :          0 :                 return NULL;
    1722                 :            :         }
    1723                 :            : 
    1724                 :        867 :         group_impl->io_avail = SPDK_SOCK_GROUP_QUEUE_DEPTH;
    1725                 :            : 
    1726         [ -  + ]:        867 :         if (io_uring_queue_init(SPDK_SOCK_GROUP_QUEUE_DEPTH, &group_impl->uring, 0) < 0) {
    1727                 :          0 :                 SPDK_ERRLOG("uring I/O context setup failure\n");
    1728                 :          0 :                 free(group_impl);
    1729                 :          0 :                 return NULL;
    1730                 :            :         }
    1731                 :            : 
    1732                 :        867 :         TAILQ_INIT(&group_impl->pending_recv);
    1733                 :            : 
    1734         [ +  + ]:        867 :         if (uring_sock_group_impl_buf_pool_alloc(group_impl) < 0) {
    1735                 :          2 :                 SPDK_ERRLOG("Failed to create buffer ring."
    1736                 :            :                             "uring sock implementation is likely not supported on this kernel.\n");
    1737                 :          2 :                 io_uring_queue_exit(&group_impl->uring);
    1738                 :          2 :                 free(group_impl);
    1739                 :          2 :                 return NULL;
    1740                 :            :         }
    1741                 :            : 
    1742         [ -  + ]:        865 :         if (g_spdk_uring_sock_impl_opts.enable_placement_id == PLACEMENT_CPU) {
    1743                 :          0 :                 spdk_sock_map_insert(&g_map, spdk_env_get_current_core(), &group_impl->base);
    1744                 :            :         }
    1745                 :            : 
    1746                 :        865 :         return &group_impl->base;
    1747                 :            : }
    1748                 :            : 
    1749                 :            : static int
    1750                 :       1445 : uring_sock_group_impl_add_sock(struct spdk_sock_group_impl *_group,
    1751                 :            :                                struct spdk_sock *_sock)
    1752                 :            : {
    1753                 :       1445 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
    1754                 :       1445 :         struct spdk_uring_sock_group_impl *group = __uring_group_impl(_group);
    1755                 :            :         int rc;
    1756                 :            : 
    1757                 :       1445 :         sock->group = group;
    1758                 :       1445 :         sock->write_task.sock = sock;
    1759                 :       1445 :         sock->write_task.type = URING_TASK_WRITE;
    1760                 :            : 
    1761                 :       1445 :         sock->read_task.sock = sock;
    1762                 :       1445 :         sock->read_task.type = URING_TASK_READ;
    1763                 :            : 
    1764                 :       1445 :         sock->errqueue_task.sock = sock;
    1765                 :       1445 :         sock->errqueue_task.type = URING_TASK_ERRQUEUE;
    1766                 :       1445 :         sock->errqueue_task.msg.msg_control = sock->buf;
    1767                 :       1445 :         sock->errqueue_task.msg.msg_controllen = sizeof(sock->buf);
    1768                 :            : 
    1769                 :       1445 :         sock->cancel_task.sock = sock;
    1770                 :       1445 :         sock->cancel_task.type = URING_TASK_CANCEL;
    1771                 :            : 
    1772                 :            :         /* switched from another polling group due to scheduling */
    1773   [ +  +  -  + ]:       1445 :         if (spdk_unlikely(sock->recv_pipe != NULL &&
    1774                 :            :                           (spdk_pipe_reader_bytes_available(sock->recv_pipe) > 0))) {
    1775   [ #  #  #  # ]:          0 :                 assert(sock->pending_recv == false);
    1776                 :          0 :                 sock->pending_recv = true;
    1777                 :          0 :                 TAILQ_INSERT_TAIL(&group->pending_recv, sock, link);
    1778                 :            :         }
    1779                 :            : 
    1780         [ -  + ]:       1445 :         if (sock->placement_id != -1) {
    1781                 :          0 :                 rc = spdk_sock_map_insert(&g_map, sock->placement_id, &group->base);
    1782         [ #  # ]:          0 :                 if (rc != 0) {
    1783                 :          0 :                         SPDK_ERRLOG("Failed to insert sock group into map: %d", rc);
    1784                 :            :                         /* Do not treat this as an error. The system will continue running. */
    1785                 :            :                 }
    1786                 :            :         }
    1787                 :            : 
    1788                 :            :         /* We get an async read going immediately */
    1789                 :       1445 :         _sock_prep_read(&sock->base);
    1790                 :            : #ifdef SPDK_ZEROCOPY
    1791   [ -  +  -  + ]:       1445 :         if (sock->zcopy) {
    1792                 :          0 :                 _sock_prep_errqueue(_sock);
    1793                 :            :         }
    1794                 :            : #endif
    1795                 :            : 
    1796                 :       1445 :         return 0;
    1797                 :            : }
    1798                 :            : 
    1799                 :            : static void
    1800                 :  135275295 : uring_sock_group_populate_buf_ring(struct spdk_uring_sock_group_impl *group)
    1801                 :            : {
    1802                 :            :         struct spdk_uring_buf_tracker *tracker;
    1803                 :            :         int count, mask;
    1804                 :            : 
    1805   [ -  +  +  - ]:  135275295 :         if (g_spdk_uring_sock_impl_opts.enable_recv_pipe) {
    1806                 :            :                 /* If recv_pipe is enabled, we do not post buffers. */
    1807                 :  135275295 :                 return;
    1808                 :            :         }
    1809                 :            : 
    1810                 :            :         /* Try to re-populate the io_uring's buffer pool using user-provided buffers */
    1811                 :          0 :         tracker = STAILQ_FIRST(&group->free_trackers);
    1812                 :          0 :         count = 0;
    1813                 :          0 :         mask = io_uring_buf_ring_mask(URING_BUF_POOL_SIZE);
    1814         [ #  # ]:          0 :         while (tracker != NULL) {
    1815                 :          0 :                 tracker->buflen = spdk_sock_group_get_buf(group->base.group, &tracker->buf, &tracker->ctx);
    1816         [ #  # ]:          0 :                 if (tracker->buflen == 0) {
    1817                 :          0 :                         break;
    1818                 :            :                 }
    1819                 :            : 
    1820         [ #  # ]:          0 :                 assert(tracker->buf != NULL);
    1821         [ #  # ]:          0 :                 STAILQ_REMOVE_HEAD(&group->free_trackers, link);
    1822         [ #  # ]:          0 :                 assert(STAILQ_FIRST(&group->free_trackers) != tracker);
    1823                 :            : 
    1824                 :          0 :                 io_uring_buf_ring_add(group->buf_ring, tracker->buf, tracker->buflen, tracker->id, mask, count);
    1825                 :          0 :                 count++;
    1826                 :          0 :                 tracker = STAILQ_FIRST(&group->free_trackers);
    1827                 :            :         }
    1828                 :            : 
    1829         [ #  # ]:          0 :         if (count > 0) {
    1830                 :          0 :                 group->buf_ring_count += count;
    1831                 :          0 :                 io_uring_buf_ring_advance(group->buf_ring, count);
    1832                 :            :         }
    1833                 :            : }
    1834                 :            : 
    1835                 :            : static int
    1836                 :  135275295 : uring_sock_group_impl_poll(struct spdk_sock_group_impl *_group, int max_events,
    1837                 :            :                            struct spdk_sock **socks)
    1838                 :            : {
    1839                 :  135275295 :         struct spdk_uring_sock_group_impl *group = __uring_group_impl(_group);
    1840                 :            :         int count, ret;
    1841                 :            :         int to_complete, to_submit;
    1842                 :            :         struct spdk_sock *_sock, *tmp;
    1843                 :            :         struct spdk_uring_sock *sock;
    1844                 :            : 
    1845         [ +  + ]:  135275295 :         if (spdk_likely(socks)) {
    1846         [ +  + ]:  338930671 :                 TAILQ_FOREACH_SAFE(_sock, &group->base.socks, link, tmp) {
    1847                 :  203656821 :                         sock = __uring_sock(_sock);
    1848         [ -  + ]:  203656821 :                         if (spdk_unlikely(sock->connection_status)) {
    1849                 :          0 :                                 continue;
    1850                 :            :                         }
    1851                 :  203656821 :                         _sock_flush(_sock);
    1852                 :            :                 }
    1853                 :            :         }
    1854                 :            : 
    1855                 :            :         /* Try to re-populate the io_uring's buffer pool using user-provided buffers */
    1856                 :  135275295 :         uring_sock_group_populate_buf_ring(group);
    1857                 :            : 
    1858                 :  135275295 :         to_submit = group->io_queued;
    1859                 :            : 
    1860                 :            :         /* For network I/O, it cannot be set with O_DIRECT, so we do not need to call spdk_io_uring_enter */
    1861         [ +  - ]:  135275295 :         if (to_submit > 0) {
    1862                 :            :                 /* If there are I/O to submit, use io_uring_submit here.
    1863                 :            :                  * It will automatically call io_uring_enter appropriately. */
    1864                 :  135275295 :                 ret = io_uring_submit(&group->uring);
    1865         [ -  + ]:  135275295 :                 if (ret < 0) {
    1866                 :          0 :                         return 1;
    1867                 :            :                 }
    1868                 :  135275295 :                 group->io_queued = 0;
    1869                 :  135275295 :                 group->io_inflight += to_submit;
    1870                 :  135275295 :                 group->io_avail -= to_submit;
    1871                 :            :         }
    1872                 :            : 
    1873                 :  135275295 :         count = 0;
    1874                 :  135275295 :         to_complete = group->io_inflight;
    1875   [ -  +  -  - ]:  135275295 :         if (to_complete > 0 || !TAILQ_EMPTY(&group->pending_recv)) {
    1876                 :  135275295 :                 count = sock_uring_group_reap(group, to_complete, max_events, socks);
    1877                 :            :         }
    1878                 :            : 
    1879                 :  135275295 :         return count;
    1880                 :            : }
    1881                 :            : 
    1882                 :            : static int
    1883                 :       1445 : uring_sock_group_impl_remove_sock(struct spdk_sock_group_impl *_group,
    1884                 :            :                                   struct spdk_sock *_sock)
    1885                 :            : {
    1886                 :       1445 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
    1887                 :       1445 :         struct spdk_uring_sock_group_impl *group = __uring_group_impl(_group);
    1888                 :            : 
    1889                 :       1445 :         sock->pending_group_remove = true;
    1890                 :            : 
    1891         [ -  + ]:       1445 :         if (sock->write_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE) {
    1892                 :          0 :                 _sock_prep_cancel_task(_sock, &sock->write_task);
    1893                 :            :                 /* Since spdk_sock_group_remove_sock is not asynchronous interface, so
    1894                 :            :                  * currently can use a while loop here. */
    1895         [ #  # ]:          0 :                 while ((sock->write_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE) ||
    1896         [ #  # ]:          0 :                        (sock->cancel_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE)) {
    1897                 :          0 :                         uring_sock_group_impl_poll(_group, 32, NULL);
    1898                 :            :                 }
    1899                 :            :         }
    1900                 :            : 
    1901         [ +  - ]:       1445 :         if (sock->read_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE) {
    1902                 :       1445 :                 _sock_prep_cancel_task(_sock, &sock->read_task);
    1903                 :            :                 /* Since spdk_sock_group_remove_sock is not asynchronous interface, so
    1904                 :            :                  * currently can use a while loop here. */
    1905         [ +  + ]:       2890 :                 while ((sock->read_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE) ||
    1906         [ -  + ]:       1445 :                        (sock->cancel_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE)) {
    1907                 :       1445 :                         uring_sock_group_impl_poll(_group, 32, NULL);
    1908                 :            :                 }
    1909                 :            :         }
    1910                 :            : 
    1911         [ -  + ]:       1445 :         if (sock->errqueue_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE) {
    1912                 :          0 :                 _sock_prep_cancel_task(_sock, &sock->errqueue_task);
    1913                 :            :                 /* Since spdk_sock_group_remove_sock is not asynchronous interface, so
    1914                 :            :                  * currently can use a while loop here. */
    1915         [ #  # ]:          0 :                 while ((sock->errqueue_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE) ||
    1916         [ #  # ]:          0 :                        (sock->cancel_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE)) {
    1917                 :          0 :                         uring_sock_group_impl_poll(_group, 32, NULL);
    1918                 :            :                 }
    1919                 :            :         }
    1920                 :            : 
    1921                 :            :         /* Make sure the cancelling the tasks above didn't cause sending new requests */
    1922         [ -  + ]:       1445 :         assert(sock->write_task.status == SPDK_URING_SOCK_TASK_NOT_IN_USE);
    1923         [ -  + ]:       1445 :         assert(sock->read_task.status == SPDK_URING_SOCK_TASK_NOT_IN_USE);
    1924         [ -  + ]:       1445 :         assert(sock->errqueue_task.status == SPDK_URING_SOCK_TASK_NOT_IN_USE);
    1925                 :            : 
    1926   [ -  +  +  - ]:       1445 :         if (sock->pending_recv) {
    1927         [ +  + ]:       1445 :                 TAILQ_REMOVE(&group->pending_recv, sock, link);
    1928                 :       1445 :                 sock->pending_recv = false;
    1929                 :            :         }
    1930   [ -  +  -  + ]:       1445 :         assert(sock->pending_recv == false);
    1931                 :            : 
    1932                 :            :         /* We have no way to handle this case. We could let the user read this
    1933                 :            :          * buffer, but the buffer came from a group and we have lost the association
    1934                 :            :          * to that so we couldn't release it. */
    1935         [ -  + ]:       1445 :         assert(STAILQ_EMPTY(&sock->recv_stream));
    1936                 :            : 
    1937         [ -  + ]:       1445 :         if (sock->placement_id != -1) {
    1938                 :          0 :                 spdk_sock_map_release(&g_map, sock->placement_id);
    1939                 :            :         }
    1940                 :            : 
    1941                 :       1445 :         sock->pending_group_remove = false;
    1942                 :       1445 :         sock->group = NULL;
    1943                 :       1445 :         return 0;
    1944                 :            : }
    1945                 :            : 
    1946                 :            : static int
    1947                 :        865 : uring_sock_group_impl_close(struct spdk_sock_group_impl *_group)
    1948                 :            : {
    1949                 :        865 :         struct spdk_uring_sock_group_impl *group = __uring_group_impl(_group);
    1950                 :            : 
    1951                 :            :         /* try to reap all the active I/O */
    1952         [ -  + ]:        865 :         while (group->io_inflight) {
    1953                 :          0 :                 uring_sock_group_impl_poll(_group, 32, NULL);
    1954                 :            :         }
    1955         [ -  + ]:        865 :         assert(group->io_inflight == 0);
    1956         [ -  + ]:        865 :         assert(group->io_avail == SPDK_SOCK_GROUP_QUEUE_DEPTH);
    1957                 :            : 
    1958                 :        865 :         uring_sock_group_impl_buf_pool_free(group);
    1959                 :            : 
    1960                 :        865 :         io_uring_queue_exit(&group->uring);
    1961                 :            : 
    1962         [ -  + ]:        865 :         if (g_spdk_uring_sock_impl_opts.enable_placement_id == PLACEMENT_CPU) {
    1963                 :          0 :                 spdk_sock_map_release(&g_map, spdk_env_get_current_core());
    1964                 :            :         }
    1965                 :            : 
    1966                 :        865 :         free(group);
    1967                 :        865 :         return 0;
    1968                 :            : }
    1969                 :            : 
    1970                 :            : static int
    1971                 :      91918 : uring_sock_flush(struct spdk_sock *_sock)
    1972                 :            : {
    1973                 :      91918 :         struct spdk_uring_sock *sock = __uring_sock(_sock);
    1974                 :      91918 :         struct msghdr msg = {};
    1975                 :          6 :         struct iovec iovs[IOV_BATCH_SIZE];
    1976                 :            :         int iovcnt;
    1977                 :            :         ssize_t rc;
    1978                 :      91918 :         int flags = sock->zcopy_send_flags;
    1979                 :            :         int retval;
    1980                 :      91918 :         bool is_zcopy = false;
    1981                 :      91918 :         struct spdk_uring_task *task = &sock->errqueue_task;
    1982                 :            : 
    1983                 :            :         /* Can't flush from within a callback or we end up with recursive calls */
    1984         [ -  + ]:      91918 :         if (_sock->cb_cnt > 0) {
    1985                 :          0 :                 errno = EAGAIN;
    1986                 :          0 :                 return -1;
    1987                 :            :         }
    1988                 :            : 
    1989                 :            :         /* Can't flush while a write is already outstanding */
    1990         [ -  + ]:      91918 :         if (sock->write_task.status != SPDK_URING_SOCK_TASK_NOT_IN_USE) {
    1991                 :          0 :                 errno = EAGAIN;
    1992                 :          0 :                 return -1;
    1993                 :            :         }
    1994                 :            : 
    1995                 :            :         /* Gather an iov */
    1996                 :      91918 :         iovcnt = spdk_sock_prep_reqs(_sock, iovs, 0, NULL, &flags);
    1997         [ +  + ]:      91918 :         if (iovcnt == 0) {
    1998                 :            :                 /* Nothing to send */
    1999                 :      86323 :                 return 0;
    2000                 :            :         }
    2001                 :            : 
    2002                 :            :         /* Perform the vectored write */
    2003                 :       5595 :         msg.msg_iov = iovs;
    2004                 :       5595 :         msg.msg_iovlen = iovcnt;
    2005                 :       5595 :         rc = sendmsg(sock->fd, &msg, flags | MSG_DONTWAIT);
    2006         [ -  + ]:       5595 :         if (rc <= 0) {
    2007   [ #  #  #  #  :          0 :                 if (rc == 0 || errno == EAGAIN || errno == EWOULDBLOCK || (errno == ENOBUFS && sock->zcopy)) {
          #  #  #  #  #  
                #  #  # ]
    2008                 :          0 :                         errno = EAGAIN;
    2009                 :            :                 }
    2010                 :          0 :                 return -1;
    2011                 :            :         }
    2012                 :            : 
    2013                 :            : #ifdef SPDK_ZEROCOPY
    2014                 :       5595 :         is_zcopy = flags & MSG_ZEROCOPY;
    2015                 :            : #endif
    2016                 :       5595 :         retval = sock_complete_write_reqs(_sock, rc, is_zcopy);
    2017         [ -  + ]:       5595 :         if (retval < 0) {
    2018                 :            :                 /* if the socket is closed, return to avoid heap-use-after-free error */
    2019                 :          0 :                 errno = ENOTCONN;
    2020                 :          0 :                 return -1;
    2021                 :            :         }
    2022                 :            : 
    2023                 :            : #ifdef SPDK_ZEROCOPY
    2024                 :            :         /* At least do once to check zero copy case */
    2025   [ -  +  -  +  :       5595 :         if (sock->zcopy && !TAILQ_EMPTY(&_sock->pending_reqs)) {
                   -  - ]
    2026                 :          0 :                 retval = recvmsg(sock->fd, &task->msg, MSG_ERRQUEUE);
    2027         [ #  # ]:          0 :                 if (retval < 0) {
    2028   [ #  #  #  # ]:          0 :                         if (errno == EWOULDBLOCK || errno == EAGAIN) {
    2029                 :          0 :                                 return rc;
    2030                 :            :                         }
    2031                 :            :                 }
    2032                 :          0 :                 _sock_check_zcopy(_sock, retval);;
    2033                 :            :         }
    2034                 :            : #endif
    2035                 :            : 
    2036                 :       5595 :         return rc;
    2037                 :            : }
    2038                 :            : 
    2039                 :            : static int
    2040                 :          0 : uring_sock_group_impl_register_interrupt(struct spdk_sock_group_impl *_group, uint32_t events,
    2041                 :            :                 spdk_interrupt_fn fn, void *arg, const char *name)
    2042                 :            : {
    2043                 :          0 :         SPDK_ERRLOG("Interrupt mode is not supported in the uring sock implementation.");
    2044                 :            : 
    2045                 :          0 :         return -ENOTSUP;
    2046                 :            : }
    2047                 :            : 
    2048                 :            : static void
    2049                 :          0 : uring_sock_group_impl_unregister_interrupt(struct spdk_sock_group_impl *_group)
    2050                 :            : {
    2051                 :          0 : }
    2052                 :            : 
    2053                 :            : static struct spdk_net_impl g_uring_net_impl = {
    2054                 :            :         .name           = "uring",
    2055                 :            :         .getaddr        = uring_sock_getaddr,
    2056                 :            :         .get_interface_name = uring_sock_get_interface_name,
    2057                 :            :         .get_numa_socket_id = uring_sock_get_numa_socket_id,
    2058                 :            :         .connect        = uring_sock_connect,
    2059                 :            :         .listen         = uring_sock_listen,
    2060                 :            :         .accept         = uring_sock_accept,
    2061                 :            :         .close          = uring_sock_close,
    2062                 :            :         .recv           = uring_sock_recv,
    2063                 :            :         .readv          = uring_sock_readv,
    2064                 :            :         .writev         = uring_sock_writev,
    2065                 :            :         .recv_next      = uring_sock_recv_next,
    2066                 :            :         .writev_async   = uring_sock_writev_async,
    2067                 :            :         .flush          = uring_sock_flush,
    2068                 :            :         .set_recvlowat  = uring_sock_set_recvlowat,
    2069                 :            :         .set_recvbuf    = uring_sock_set_recvbuf,
    2070                 :            :         .set_sendbuf    = uring_sock_set_sendbuf,
    2071                 :            :         .is_ipv6        = uring_sock_is_ipv6,
    2072                 :            :         .is_ipv4        = uring_sock_is_ipv4,
    2073                 :            :         .is_connected   = uring_sock_is_connected,
    2074                 :            :         .group_impl_get_optimal = uring_sock_group_impl_get_optimal,
    2075                 :            :         .group_impl_create      = uring_sock_group_impl_create,
    2076                 :            :         .group_impl_add_sock    = uring_sock_group_impl_add_sock,
    2077                 :            :         .group_impl_remove_sock = uring_sock_group_impl_remove_sock,
    2078                 :            :         .group_impl_poll        = uring_sock_group_impl_poll,
    2079                 :            :         .group_impl_register_interrupt    = uring_sock_group_impl_register_interrupt,
    2080                 :            :         .group_impl_unregister_interrupt  = uring_sock_group_impl_unregister_interrupt,
    2081                 :            :         .group_impl_close       = uring_sock_group_impl_close,
    2082                 :            :         .get_opts               = uring_sock_impl_get_opts,
    2083                 :            :         .set_opts               = uring_sock_impl_set_opts,
    2084                 :            : };
    2085                 :            : 
    2086                 :            : __attribute__((constructor)) static void
    2087                 :        404 : net_impl_register_uring(void)
    2088                 :            : {
    2089                 :            :         struct spdk_sock_group_impl *impl;
    2090                 :            : 
    2091                 :            :         /* Check if we can create a uring sock group before we register
    2092                 :            :          * it as a valid impl. */
    2093                 :        404 :         impl = uring_sock_group_impl_create();
    2094         [ +  + ]:        404 :         if (impl) {
    2095                 :        402 :                 uring_sock_group_impl_close(impl);
    2096                 :        402 :                 spdk_net_impl_register(&g_uring_net_impl);
    2097                 :            :         }
    2098                 :        404 : }

Generated by: LCOV version 1.14