LCOV - code coverage report
Current view: top level - spdk/lib/sock - sock.c (source / functions) Hit Total Coverage
Test: Combined Lines: 499 577 86.5 %
Date: 2024-11-19 10:58:32 Functions: 54 54 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 816 1966 41.5 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2016 Intel Corporation. All rights reserved.
       3                 :            :  *   Copyright (c) 2020 Mellanox Technologies LTD. All rights reserved.
       4                 :            :  *   Copyright (c) 2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
       5                 :            :  */
       6                 :            : 
       7                 :            : #include "spdk/stdinc.h"
       8                 :            : 
       9                 :            : #include "spdk/sock.h"
      10                 :            : #include "spdk_internal/sock.h"
      11                 :            : #include "spdk/log.h"
      12                 :            : #include "spdk/env.h"
      13                 :            : #include "spdk/util.h"
      14                 :            : #include "spdk/trace.h"
      15                 :            : #include "spdk/thread.h"
      16                 :            : #include "spdk_internal/trace_defs.h"
      17                 :            : 
      18                 :            : #define SPDK_SOCK_DEFAULT_PRIORITY 0
      19                 :            : #define SPDK_SOCK_DEFAULT_ZCOPY true
      20                 :            : #define SPDK_SOCK_DEFAULT_ACK_TIMEOUT 0
      21                 :            : 
      22                 :            : #define SPDK_SOCK_OPTS_FIELD_OK(opts, field) (offsetof(struct spdk_sock_opts, field) + sizeof(opts->field) <= (opts->opts_size))
      23                 :            : 
      24                 :            : static STAILQ_HEAD(, spdk_net_impl) g_net_impls = STAILQ_HEAD_INITIALIZER(g_net_impls);
      25                 :            : static struct spdk_net_impl *g_default_impl;
      26                 :            : 
      27                 :            : struct spdk_sock_placement_id_entry {
      28                 :            :         int placement_id;
      29                 :            :         uint32_t ref;
      30                 :            :         struct spdk_sock_group_impl *group;
      31                 :            :         STAILQ_ENTRY(spdk_sock_placement_id_entry) link;
      32                 :            : };
      33                 :            : 
      34                 :            : static inline struct spdk_sock_group_impl *
      35                 :      33318 : sock_get_group_impl_from_group(struct spdk_sock *sock, struct spdk_sock_group *group)
      36                 :            : {
      37                 :      33318 :         struct spdk_sock_group_impl *group_impl = NULL;
      38                 :            : 
      39   [ +  -  +  -  :      69729 :         STAILQ_FOREACH_FROM(group_impl, &group->group_impls, link) {
          +  -  +  -  -  
          +  +  -  +  -  
                   +  - ]
      40   [ +  +  +  -  :      69729 :                 if (sock->net_impl == group_impl->net_impl) {
          +  -  +  -  +  
                      + ]
      41                 :      33318 :                         return group_impl;
      42                 :            :                 }
      43                 :       4658 :         }
      44                 :          0 :         return NULL;
      45                 :       4658 : }
      46                 :            : 
      47                 :            : /* Called under map->mtx lock */
      48                 :            : static struct spdk_sock_placement_id_entry *
      49                 :         24 : _sock_map_entry_alloc(struct spdk_sock_map *map, int placement_id)
      50                 :            : {
      51                 :            :         struct spdk_sock_placement_id_entry *entry;
      52                 :            : 
      53                 :         24 :         entry = calloc(1, sizeof(*entry));
      54         [ -  + ]:         24 :         if (!entry) {
      55                 :          0 :                 SPDK_ERRLOG("Cannot allocate an entry for placement_id=%u\n", placement_id);
      56                 :          0 :                 return NULL;
      57                 :            :         }
      58                 :            : 
      59   [ #  #  #  # ]:         24 :         entry->placement_id = placement_id;
      60                 :            : 
      61   [ #  #  #  #  :         24 :         STAILQ_INSERT_TAIL(&map->entries, entry, link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      62                 :            : 
      63                 :         24 :         return entry;
      64                 :          0 : }
      65                 :            : 
      66                 :            : int
      67                 :         39 : spdk_sock_map_insert(struct spdk_sock_map *map, int placement_id,
      68                 :            :                      struct spdk_sock_group_impl *group)
      69                 :            : {
      70                 :            :         struct spdk_sock_placement_id_entry *entry;
      71                 :         39 :         int rc = 0;
      72                 :            : 
      73   [ -  +  #  # ]:         39 :         pthread_mutex_lock(&map->mtx);
      74   [ +  +  #  #  :         50 :         STAILQ_FOREACH(entry, &map->entries, link) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      75   [ +  +  #  #  :         31 :                 if (placement_id == entry->placement_id) {
                   #  # ]
      76                 :            :                         /* Can't set group to NULL if it is already not-NULL */
      77         [ -  + ]:         20 :                         if (group == NULL) {
      78   [ #  #  #  # ]:          0 :                                 rc = (entry->group == NULL) ? 0 : -EINVAL;
      79                 :          0 :                                 goto end;
      80                 :            :                         }
      81                 :            : 
      82   [ +  +  #  #  :         20 :                         if (entry->group == NULL) {
                   #  # ]
      83   [ #  #  #  # ]:          3 :                                 entry->group = group;
      84   [ +  +  #  #  :         17 :                         } else if (entry->group != group) {
                   #  # ]
      85                 :          6 :                                 rc = -EINVAL;
      86                 :          6 :                                 goto end;
      87                 :            :                         }
      88                 :            : 
      89         [ #  # ]:         14 :                         entry->ref++;
      90                 :         14 :                         goto end;
      91                 :            :                 }
      92                 :          0 :         }
      93                 :            : 
      94                 :         19 :         entry = _sock_map_entry_alloc(map, placement_id);
      95         [ -  + ]:         19 :         if (entry == NULL) {
      96                 :          0 :                 rc = -ENOMEM;
      97                 :          0 :                 goto end;
      98                 :            :         }
      99         [ +  + ]:         19 :         if (group) {
     100   [ #  #  #  # ]:         16 :                 entry->group = group;
     101         [ #  # ]:         16 :                 entry->ref++;
     102                 :          0 :         }
     103                 :          3 : end:
     104   [ -  +  #  # ]:         39 :         pthread_mutex_unlock(&map->mtx);
     105                 :            : 
     106                 :         39 :         return rc;
     107                 :            : }
     108                 :            : 
     109                 :            : void
     110                 :         15 : spdk_sock_map_release(struct spdk_sock_map *map, int placement_id)
     111                 :            : {
     112                 :            :         struct spdk_sock_placement_id_entry *entry;
     113                 :            : 
     114   [ -  +  #  # ]:         15 :         pthread_mutex_lock(&map->mtx);
     115   [ +  -  #  #  :         23 :         STAILQ_FOREACH(entry, &map->entries, link) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     116   [ +  +  #  #  :         23 :                 if (placement_id == entry->placement_id) {
                   #  # ]
     117   [ -  +  #  #  :         15 :                         assert(entry->ref > 0);
             #  #  #  # ]
     118         [ #  # ]:         15 :                         entry->ref--;
     119                 :            : 
     120   [ +  +  #  #  :         15 :                         if (entry->ref == 0) {
                   #  # ]
     121   [ #  #  #  # ]:          9 :                                 entry->group = NULL;
     122                 :          0 :                         }
     123                 :         15 :                         break;
     124                 :            :                 }
     125                 :          0 :         }
     126                 :            : 
     127   [ -  +  #  # ]:         15 :         pthread_mutex_unlock(&map->mtx);
     128                 :         15 : }
     129                 :            : 
     130                 :            : int
     131                 :         41 : spdk_sock_map_lookup(struct spdk_sock_map *map, int placement_id,
     132                 :            :                      struct spdk_sock_group_impl **group, struct spdk_sock_group_impl *hint)
     133                 :            : {
     134                 :            :         struct spdk_sock_placement_id_entry *entry;
     135                 :            : 
     136         [ #  # ]:         41 :         *group = NULL;
     137   [ -  +  #  # ]:         41 :         pthread_mutex_lock(&map->mtx);
     138   [ +  +  #  #  :         52 :         STAILQ_FOREACH(entry, &map->entries, link) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     139   [ +  +  #  #  :         44 :                 if (placement_id == entry->placement_id) {
                   #  # ]
     140   [ #  #  #  #  :         33 :                         *group = entry->group;
                   #  # ]
     141   [ +  +  #  # ]:         33 :                         if (*group != NULL) {
     142                 :            :                                 /* Return previously assigned sock_group */
     143   [ -  +  #  # ]:         27 :                                 pthread_mutex_unlock(&map->mtx);
     144                 :         27 :                                 return 0;
     145                 :            :                         }
     146                 :          6 :                         break;
     147                 :            :                 }
     148                 :          0 :         }
     149                 :            : 
     150                 :            :         /* No entry with assigned sock_group, nor hint to use */
     151         [ +  + ]:         14 :         if (hint == NULL) {
     152   [ -  +  #  # ]:          9 :                 pthread_mutex_unlock(&map->mtx);
     153                 :          9 :                 return -EINVAL;
     154                 :            :         }
     155                 :            : 
     156                 :            :         /* Create new entry if there is none with matching placement_id */
     157         [ +  - ]:          5 :         if (entry == NULL) {
     158                 :          5 :                 entry = _sock_map_entry_alloc(map, placement_id);
     159         [ -  + ]:          5 :                 if (entry == NULL) {
     160   [ #  #  #  # ]:          0 :                         pthread_mutex_unlock(&map->mtx);
     161                 :          0 :                         return -ENOMEM;
     162                 :            :                 }
     163                 :          0 :         }
     164                 :            : 
     165   [ #  #  #  # ]:          5 :         entry->group = hint;
     166   [ -  +  #  # ]:          5 :         pthread_mutex_unlock(&map->mtx);
     167                 :            : 
     168                 :          5 :         return 0;
     169                 :          0 : }
     170                 :            : 
     171                 :            : void
     172                 :       3077 : spdk_sock_map_cleanup(struct spdk_sock_map *map)
     173                 :            : {
     174                 :            :         struct spdk_sock_placement_id_entry *entry, *tmp;
     175                 :            : 
     176   [ +  +  +  - ]:       3077 :         pthread_mutex_lock(&map->mtx);
     177   [ +  +  +  -  :       3101 :         STAILQ_FOREACH_SAFE(entry, &map->entries, link, tmp) {
          +  -  +  -  #  
          #  #  #  #  #  
                   +  - ]
     178   [ +  -  +  +  :         24 :                 STAILQ_REMOVE(&map->entries, entry, spdk_sock_placement_id_entry, link);
          -  -  -  -  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  # ]
     179                 :         24 :                 free(entry);
     180                 :          0 :         }
     181   [ +  +  +  - ]:       3077 :         pthread_mutex_unlock(&map->mtx);
     182                 :       3077 : }
     183                 :            : 
     184                 :            : int
     185                 :         15 : spdk_sock_map_find_free(struct spdk_sock_map *map)
     186                 :            : {
     187                 :            :         struct spdk_sock_placement_id_entry *entry;
     188                 :         15 :         int placement_id = -1;
     189                 :            : 
     190   [ -  +  #  # ]:         15 :         pthread_mutex_lock(&map->mtx);
     191   [ +  +  #  #  :         21 :         STAILQ_FOREACH(entry, &map->entries, link) {
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     192   [ +  +  #  #  :         12 :                 if (entry->group == NULL) {
                   #  # ]
     193   [ #  #  #  # ]:          6 :                         placement_id = entry->placement_id;
     194                 :          6 :                         break;
     195                 :            :                 }
     196                 :          0 :         }
     197                 :            : 
     198   [ -  +  #  # ]:         15 :         pthread_mutex_unlock(&map->mtx);
     199                 :            : 
     200                 :         15 :         return placement_id;
     201                 :            : }
     202                 :            : 
     203                 :            : int
     204                 :       9442 : spdk_sock_get_optimal_sock_group(struct spdk_sock *sock, struct spdk_sock_group **group,
     205                 :            :                                  struct spdk_sock_group *hint)
     206                 :            : {
     207                 :            :         struct spdk_sock_group_impl *group_impl;
     208                 :       9442 :         struct spdk_sock_group_impl *hint_group_impl = NULL;
     209                 :            : 
     210   [ +  +  #  # ]:       9442 :         assert(group != NULL);
     211                 :            : 
     212         [ +  + ]:       9442 :         if (hint != NULL) {
     213                 :       9442 :                 hint_group_impl = sock_get_group_impl_from_group(sock, hint);
     214         [ +  + ]:       9442 :                 if (hint_group_impl == NULL) {
     215                 :          0 :                         return -EINVAL;
     216                 :            :                 }
     217                 :       1536 :         }
     218                 :            : 
     219   [ +  -  +  -  :       9442 :         group_impl = sock->net_impl->group_impl_get_optimal(sock, hint_group_impl);
          +  -  +  -  -  
                +  +  - ]
     220                 :            : 
     221         [ +  + ]:       9442 :         if (group_impl) {
     222   [ #  #  #  #  :          3 :                 *group = group_impl->group;
                   #  # ]
     223                 :          0 :         }
     224                 :            : 
     225                 :       9442 :         return 0;
     226                 :       1536 : }
     227                 :            : 
     228                 :            : int
     229                 :      19481 : spdk_sock_getaddr(struct spdk_sock *sock, char *saddr, int slen, uint16_t *sport,
     230                 :            :                   char *caddr, int clen, uint16_t *cport)
     231                 :            : {
     232   [ +  -  +  -  :      19481 :         return sock->net_impl->getaddr(sock, saddr, slen, sport, caddr, clen, cport);
          +  -  +  -  -  
                +  +  - ]
     233                 :            : }
     234                 :            : 
     235                 :            : const char *
     236                 :         18 : spdk_sock_get_interface_name(struct spdk_sock *sock)
     237                 :            : {
     238   [ +  -  #  #  :         18 :         if (sock->net_impl->get_interface_name) {
          #  #  #  #  #  
                      # ]
     239   [ #  #  #  #  :         18 :                 return sock->net_impl->get_interface_name(sock);
          #  #  #  #  #  
                #  #  # ]
     240                 :            :         } else {
     241                 :          0 :                 return NULL;
     242                 :            :         }
     243                 :          0 : }
     244                 :            : 
     245                 :            : int32_t
     246                 :      11583 : spdk_sock_get_numa_id(struct spdk_sock *sock)
     247                 :            : {
     248   [ +  -  +  -  :      11583 :         if (sock->net_impl->get_numa_id) {
          +  -  +  -  +  
                      - ]
     249   [ +  -  +  -  :      11583 :                 return sock->net_impl->get_numa_id(sock);
          +  -  +  -  -  
                +  -  + ]
     250                 :            :         } else {
     251                 :          0 :                 return SPDK_ENV_NUMA_ID_ANY;
     252                 :            :         }
     253                 :       2304 : }
     254                 :            : 
     255                 :            : const char *
     256                 :        680 : spdk_sock_get_impl_name(struct spdk_sock *sock)
     257                 :            : {
     258   [ #  #  #  #  :        680 :         return sock->net_impl->name;
             #  #  #  # ]
     259                 :            : }
     260                 :            : 
     261                 :            : void
     262                 :      41956 : spdk_sock_get_default_opts(struct spdk_sock_opts *opts)
     263                 :            : {
     264   [ +  +  #  # ]:      41956 :         assert(opts);
     265                 :            : 
     266   [ +  +  +  -  :      41956 :         if (SPDK_SOCK_OPTS_FIELD_OK(opts, priority)) {
                   -  + ]
     267   [ +  -  +  - ]:      41950 :                 opts->priority = SPDK_SOCK_DEFAULT_PRIORITY;
     268                 :       3122 :         }
     269                 :            : 
     270   [ +  +  +  -  :      41956 :         if (SPDK_SOCK_OPTS_FIELD_OK(opts, zcopy)) {
                   -  + ]
     271   [ +  -  +  - ]:      41950 :                 opts->zcopy = SPDK_SOCK_DEFAULT_ZCOPY;
     272                 :       3122 :         }
     273                 :            : 
     274   [ +  +  +  -  :      41956 :         if (SPDK_SOCK_OPTS_FIELD_OK(opts, ack_timeout)) {
                   -  + ]
     275   [ +  -  +  - ]:      41950 :                 opts->ack_timeout = SPDK_SOCK_DEFAULT_ACK_TIMEOUT;
     276                 :       3122 :         }
     277                 :            : 
     278   [ +  +  +  -  :      41956 :         if (SPDK_SOCK_OPTS_FIELD_OK(opts, impl_opts)) {
                   -  + ]
     279   [ +  -  +  - ]:      41950 :                 opts->impl_opts = NULL;
     280                 :       3122 :         }
     281                 :            : 
     282   [ +  +  +  -  :      41956 :         if (SPDK_SOCK_OPTS_FIELD_OK(opts, impl_opts_size)) {
                   -  + ]
     283   [ +  -  +  - ]:      41950 :                 opts->impl_opts_size = 0;
     284                 :       3122 :         }
     285                 :            : 
     286   [ +  +  +  -  :      41956 :         if (SPDK_SOCK_OPTS_FIELD_OK(opts, src_addr)) {
                   -  + ]
     287   [ +  -  +  - ]:      41950 :                 opts->src_addr = NULL;
     288                 :       3122 :         }
     289                 :            : 
     290   [ +  +  +  -  :      41956 :         if (SPDK_SOCK_OPTS_FIELD_OK(opts, src_port)) {
                   -  + ]
     291   [ +  -  +  - ]:      41950 :                 opts->src_port = 0;
     292                 :       3122 :         }
     293                 :      41956 : }
     294                 :            : 
     295                 :            : /*
     296                 :            :  * opts The opts allocated in the current library.
     297                 :            :  * opts_user The opts passed by the caller.
     298                 :            :  * */
     299                 :            : static void
     300                 :      20972 : sock_init_opts(struct spdk_sock_opts *opts, struct spdk_sock_opts *opts_user)
     301                 :            : {
     302   [ +  +  #  # ]:      20972 :         assert(opts);
     303   [ +  +  #  # ]:      20972 :         assert(opts_user);
     304                 :            : 
     305   [ +  -  +  - ]:      20972 :         opts->opts_size = sizeof(*opts);
     306                 :      20972 :         spdk_sock_get_default_opts(opts);
     307                 :            : 
     308                 :            :         /* reset the size according to the user */
     309   [ +  -  +  -  :      20972 :         opts->opts_size = opts_user->opts_size;
             +  -  +  - ]
     310   [ +  -  +  -  :      20972 :         if (SPDK_SOCK_OPTS_FIELD_OK(opts, priority)) {
                   -  + ]
     311   [ +  -  +  -  :      20972 :                 opts->priority = opts_user->priority;
             +  -  +  - ]
     312                 :       1561 :         }
     313                 :            : 
     314   [ +  -  +  -  :      20972 :         if (SPDK_SOCK_OPTS_FIELD_OK(opts, zcopy)) {
                   -  + ]
     315   [ +  +  +  -  :      20972 :                 opts->zcopy = opts_user->zcopy;
          +  -  +  -  +  
                      - ]
     316                 :       1561 :         }
     317                 :            : 
     318   [ +  -  +  -  :      20972 :         if (SPDK_SOCK_OPTS_FIELD_OK(opts, ack_timeout)) {
                   -  + ]
     319   [ +  -  +  -  :      20972 :                 opts->ack_timeout = opts_user->ack_timeout;
             +  -  +  - ]
     320                 :       1561 :         }
     321                 :            : 
     322   [ +  -  +  -  :      20972 :         if (SPDK_SOCK_OPTS_FIELD_OK(opts, impl_opts)) {
                   -  + ]
     323   [ +  -  +  -  :      20972 :                 opts->impl_opts = opts_user->impl_opts;
             +  -  +  - ]
     324                 :       1561 :         }
     325                 :            : 
     326   [ +  -  +  -  :      20972 :         if (SPDK_SOCK_OPTS_FIELD_OK(opts, impl_opts_size)) {
                   -  + ]
     327   [ +  -  +  -  :      20972 :                 opts->impl_opts_size = opts_user->impl_opts_size;
             +  -  +  - ]
     328                 :       1561 :         }
     329                 :            : 
     330   [ +  -  +  -  :      20972 :         if (SPDK_SOCK_OPTS_FIELD_OK(opts, src_addr)) {
                   -  + ]
     331   [ +  -  +  -  :      20972 :                 opts->src_addr = opts_user->src_addr;
             +  -  +  - ]
     332                 :       1561 :         }
     333                 :            : 
     334   [ +  -  +  -  :      20972 :         if (SPDK_SOCK_OPTS_FIELD_OK(opts, src_port)) {
                   -  + ]
     335   [ +  -  +  -  :      20972 :                 opts->src_port = opts_user->src_port;
             +  -  +  - ]
     336                 :       1561 :         }
     337                 :      20972 : }
     338                 :            : 
     339                 :            : struct spdk_sock *
     340                 :         26 : spdk_sock_connect(const char *ip, int port, const char *impl_name)
     341                 :            : {
     342                 :         24 :         struct spdk_sock_opts opts;
     343                 :            : 
     344                 :         26 :         opts.opts_size = sizeof(opts);
     345                 :         26 :         spdk_sock_get_default_opts(&opts);
     346                 :         26 :         return spdk_sock_connect_ext(ip, port, impl_name, &opts);
     347                 :            : }
     348                 :            : 
     349                 :            : struct spdk_sock *
     350                 :      20534 : spdk_sock_connect_ext(const char *ip, int port, const char *_impl_name, struct spdk_sock_opts *opts)
     351                 :            : {
     352                 :      20534 :         struct spdk_net_impl *impl = NULL;
     353                 :            :         struct spdk_sock *sock;
     354                 :        136 :         struct spdk_sock_opts opts_local;
     355                 :      20534 :         const char *impl_name = NULL;
     356                 :            : 
     357         [ +  + ]:      20534 :         if (opts == NULL) {
     358                 :          0 :                 SPDK_ERRLOG("the opts should not be NULL pointer\n");
     359                 :          0 :                 return NULL;
     360                 :            :         }
     361                 :            : 
     362         [ +  + ]:      20534 :         if (_impl_name) {
     363                 :        181 :                 impl_name = _impl_name;
     364         [ +  - ]:      20353 :         } else if (g_default_impl) {
     365   [ +  -  +  - ]:      20353 :                 impl_name = g_default_impl->name;
     366                 :       1536 :         }
     367                 :            : 
     368   [ +  +  +  +  :      56330 :         STAILQ_FOREACH_FROM(impl, &g_net_impls, link) {
          +  -  +  -  +  
                      - ]
     369   [ +  -  +  +  :      41411 :                 if (impl_name && strncmp(impl_name, impl->name, strlen(impl->name) + 1)) {
          +  +  +  +  +  
          +  +  -  +  -  
             +  -  +  + ]
     370                 :      20877 :                         continue;
     371                 :            :                 }
     372                 :            : 
     373   [ +  +  +  +  :      20534 :                 SPDK_DEBUGLOG(sock, "Creating a client socket using impl %s\n", impl->name);
          +  -  #  #  #  
                      # ]
     374                 :      20534 :                 sock_init_opts(&opts_local, opts);
     375   [ +  -  +  -  :      20534 :                 sock = impl->connect(ip, port, &opts_local);
             -  +  +  - ]
     376         [ +  + ]:      20534 :                 if (sock != NULL) {
     377                 :            :                         /* Copy the contents, both the two structures are the same ABI version */
     378   [ +  +  +  -  :       5615 :                         memcpy(&sock->opts, &opts_local, sizeof(sock->opts));
                   +  - ]
     379                 :            :                         /* Clear out impl_opts to make sure we don't keep reference to a dangling
     380                 :            :                          * pointer */
     381   [ +  -  +  -  :       5615 :                         sock->opts.impl_opts = NULL;
                   +  - ]
     382   [ +  -  +  - ]:       5615 :                         sock->net_impl = impl;
     383   [ +  -  +  -  :       5615 :                         TAILQ_INIT(&sock->queued_reqs);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     384   [ +  -  +  -  :       5615 :                         TAILQ_INIT(&sock->pending_reqs);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     385                 :            : 
     386                 :       5615 :                         return sock;
     387                 :            :                 }
     388                 :          0 :         }
     389                 :            : 
     390                 :      14919 :         return NULL;
     391                 :       1536 : }
     392                 :            : 
     393                 :            : struct spdk_sock *
     394                 :         76 : spdk_sock_listen(const char *ip, int port, const char *impl_name)
     395                 :            : {
     396                 :         18 :         struct spdk_sock_opts opts;
     397                 :            : 
     398                 :         76 :         opts.opts_size = sizeof(opts);
     399                 :         76 :         spdk_sock_get_default_opts(&opts);
     400                 :         76 :         return spdk_sock_listen_ext(ip, port, impl_name, &opts);
     401                 :            : }
     402                 :            : 
     403                 :            : struct spdk_sock *
     404                 :        438 : spdk_sock_listen_ext(const char *ip, int port, const char *_impl_name, struct spdk_sock_opts *opts)
     405                 :            : {
     406                 :        438 :         struct spdk_net_impl *impl = NULL;
     407                 :            :         struct spdk_sock *sock;
     408                 :         47 :         struct spdk_sock_opts opts_local;
     409                 :        438 :         const char *impl_name = NULL;
     410                 :            : 
     411         [ +  + ]:        438 :         if (opts == NULL) {
     412                 :          0 :                 SPDK_ERRLOG("the opts should not be NULL pointer\n");
     413                 :          0 :                 return NULL;
     414                 :            :         }
     415                 :            : 
     416         [ +  + ]:        438 :         if (_impl_name) {
     417                 :         66 :                 impl_name = _impl_name;
     418         [ +  - ]:        372 :         } else if (g_default_impl) {
     419   [ +  -  +  - ]:        372 :                 impl_name = g_default_impl->name;
     420                 :         25 :         }
     421                 :            : 
     422   [ +  +  +  -  :        936 :         STAILQ_FOREACH_FROM(impl, &g_net_impls, link) {
          +  -  +  -  +  
                      - ]
     423   [ +  -  +  +  :        936 :                 if (impl_name && strncmp(impl_name, impl->name, strlen(impl->name) + 1)) {
          +  +  +  +  +  
          +  +  -  +  -  
             +  -  +  + ]
     424                 :        498 :                         continue;
     425                 :            :                 }
     426                 :            : 
     427   [ +  +  +  +  :        438 :                 SPDK_DEBUGLOG(sock, "Creating a listening socket using impl %s\n", impl->name);
          +  -  #  #  #  
                      # ]
     428                 :        438 :                 sock_init_opts(&opts_local, opts);
     429   [ +  -  +  -  :        438 :                 sock = impl->listen(ip, port, &opts_local);
             -  +  +  - ]
     430         [ +  + ]:        438 :                 if (sock != NULL) {
     431                 :            :                         /* Copy the contents, both the two structures are the same ABI version */
     432   [ +  +  +  -  :        438 :                         memcpy(&sock->opts, &opts_local, sizeof(sock->opts));
                   +  - ]
     433                 :            :                         /* Clear out impl_opts to make sure we don't keep reference to a dangling
     434                 :            :                          * pointer */
     435   [ +  -  +  -  :        438 :                         sock->opts.impl_opts = NULL;
                   +  - ]
     436   [ +  -  +  - ]:        438 :                         sock->net_impl = impl;
     437                 :            :                         /* Don't need to initialize the request queues for listen
     438                 :            :                          * sockets. */
     439                 :        438 :                         return sock;
     440                 :            :                 }
     441                 :          0 :         }
     442                 :            : 
     443                 :          0 :         return NULL;
     444                 :         25 : }
     445                 :            : 
     446                 :            : struct spdk_sock *
     447                 :    2346397 : spdk_sock_accept(struct spdk_sock *sock)
     448                 :            : {
     449                 :            :         struct spdk_sock *new_sock;
     450                 :            : 
     451   [ +  -  +  -  :    2346397 :         new_sock = sock->net_impl->accept(sock);
          +  -  +  -  -  
                +  +  - ]
     452         [ +  + ]:    2346397 :         if (new_sock != NULL) {
     453                 :            :                 /* Inherit the opts from the "accept sock" */
     454   [ +  -  +  - ]:      10057 :                 new_sock->opts = sock->opts;
     455   [ +  +  +  +  :      10057 :                 memcpy(&new_sock->opts, &sock->opts, sizeof(new_sock->opts));
             +  -  +  - ]
     456   [ +  -  +  -  :      10057 :                 new_sock->net_impl = sock->net_impl;
             +  -  +  - ]
     457   [ +  -  +  -  :      10057 :                 TAILQ_INIT(&new_sock->queued_reqs);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     458   [ +  -  +  -  :      10057 :                 TAILQ_INIT(&new_sock->pending_reqs);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     459                 :       1536 :         }
     460                 :            : 
     461                 :    2346397 :         return new_sock;
     462                 :            : }
     463                 :            : 
     464                 :            : int
     465                 :      31082 : spdk_sock_close(struct spdk_sock **_sock)
     466                 :            : {
     467         [ +  - ]:      31082 :         struct spdk_sock *sock = *_sock;
     468                 :            : 
     469         [ +  + ]:      31082 :         if (sock == NULL) {
     470         [ #  # ]:      14903 :                 errno = EBADF;
     471                 :      14903 :                 return -1;
     472                 :            :         }
     473                 :            : 
     474   [ +  +  +  -  :      16179 :         if (sock->cb_fn != NULL) {
                   -  + ]
     475                 :            :                 /* This sock is still part of a sock_group. */
     476         [ #  # ]:          6 :                 errno = EBUSY;
     477                 :          6 :                 return -1;
     478                 :            :         }
     479                 :            : 
     480                 :            :         /* Beyond this point the socket is considered closed. */
     481         [ +  - ]:      16173 :         *_sock = NULL;
     482                 :            : 
     483   [ +  -  +  - ]:      16173 :         sock->flags.closed = true;
     484                 :            : 
     485   [ +  +  +  -  :      16173 :         if (sock->cb_cnt > 0) {
                   -  + ]
     486                 :            :                 /* Let the callback unwind before destroying the socket */
     487                 :         63 :                 return 0;
     488                 :            :         }
     489                 :            : 
     490                 :      16110 :         spdk_sock_abort_requests(sock);
     491                 :            : 
     492   [ +  -  +  -  :      16110 :         return sock->net_impl->close(sock);
          +  -  +  -  -  
                +  +  - ]
     493                 :       3097 : }
     494                 :            : 
     495                 :            : ssize_t
     496                 :  390711438 : spdk_sock_recv(struct spdk_sock *sock, void *buf, size_t len)
     497                 :            : {
     498   [ +  +  +  +  :  390711438 :         if (sock == NULL || sock->flags.closed) {
             +  -  +  + ]
     499         [ #  # ]:        522 :                 errno = EBADF;
     500                 :          0 :                 return -1;
     501                 :            :         }
     502                 :            : 
     503   [ +  -  +  -  :  390710916 :         return sock->net_impl->recv(sock, buf, len);
          +  -  +  -  +  
                +  +  - ]
     504                 :    1323032 : }
     505                 :            : 
     506                 :            : ssize_t
     507                 :    2946047 : spdk_sock_readv(struct spdk_sock *sock, struct iovec *iov, int iovcnt)
     508                 :            : {
     509   [ +  -  -  +  :    2946047 :         if (sock == NULL || sock->flags.closed) {
             #  #  #  # ]
     510         [ #  # ]:          0 :                 errno = EBADF;
     511                 :          0 :                 return -1;
     512                 :            :         }
     513                 :            : 
     514   [ #  #  #  #  :    2946047 :         return sock->net_impl->readv(sock, iov, iovcnt);
          #  #  #  #  #  
                #  #  # ]
     515                 :          0 : }
     516                 :            : 
     517                 :            : ssize_t
     518                 :        494 : spdk_sock_writev(struct spdk_sock *sock, struct iovec *iov, int iovcnt)
     519                 :            : {
     520   [ +  -  -  +  :        494 :         if (sock == NULL || sock->flags.closed) {
             #  #  #  # ]
     521         [ #  # ]:          0 :                 errno = EBADF;
     522                 :          0 :                 return -1;
     523                 :            :         }
     524                 :            : 
     525   [ #  #  #  #  :        494 :         return sock->net_impl->writev(sock, iov, iovcnt);
          #  #  #  #  #  
                #  #  # ]
     526                 :          0 : }
     527                 :            : 
     528                 :            : void
     529                 :   69847375 : spdk_sock_writev_async(struct spdk_sock *sock, struct spdk_sock_request *req)
     530                 :            : {
     531   [ +  +  +  -  :   69847375 :         assert(req->cb_fn != NULL);
             +  -  #  # ]
     532                 :            : 
     533   [ +  -  +  +  :   69847375 :         if (sock == NULL || sock->flags.closed) {
             +  -  -  + ]
     534   [ #  #  #  #  :          0 :                 req->cb_fn(req->cb_arg, -EBADF);
          #  #  #  #  #  
                #  #  # ]
     535                 :          0 :                 return;
     536                 :            :         }
     537                 :            : 
     538   [ +  -  +  -  :   69847375 :         sock->net_impl->writev_async(sock, req);
          +  -  +  -  -  
                +  +  - ]
     539                 :      42273 : }
     540                 :            : 
     541                 :            : int
     542                 :         36 : spdk_sock_recv_next(struct spdk_sock *sock, void **buf, void **ctx)
     543                 :            : {
     544   [ +  -  -  +  :         36 :         if (sock == NULL || sock->flags.closed) {
             #  #  #  # ]
     545         [ #  # ]:          0 :                 errno = EBADF;
     546                 :          0 :                 return -1;
     547                 :            :         }
     548                 :            : 
     549   [ -  +  #  #  :         36 :         if (sock->group_impl == NULL) {
                   #  # ]
     550         [ #  # ]:          0 :                 errno = ENOTSUP;
     551                 :          0 :                 return -1;
     552                 :            :         }
     553                 :            : 
     554   [ #  #  #  #  :         36 :         return sock->net_impl->recv_next(sock, buf, ctx);
          #  #  #  #  #  
                #  #  # ]
     555                 :          0 : }
     556                 :            : 
     557                 :            : int
     558                 :   38823029 : spdk_sock_flush(struct spdk_sock *sock)
     559                 :            : {
     560   [ +  +  +  +  :   38823029 :         if (sock == NULL || sock->flags.closed) {
             +  -  +  + ]
     561         [ #  # ]:        492 :                 errno = EBADF;
     562                 :        462 :                 return -1;
     563                 :            :         }
     564                 :            : 
     565   [ +  -  +  -  :   38822537 :         return sock->net_impl->flush(sock);
          +  -  +  -  +  
                +  +  - ]
     566                 :    1219234 : }
     567                 :            : 
     568                 :            : int
     569                 :      10017 : spdk_sock_set_recvlowat(struct spdk_sock *sock, int nbytes)
     570                 :            : {
     571   [ +  -  +  -  :      10017 :         return sock->net_impl->set_recvlowat(sock, nbytes);
          +  -  +  -  -  
                +  +  - ]
     572                 :            : }
     573                 :            : 
     574                 :            : int
     575                 :      18733 : spdk_sock_set_recvbuf(struct spdk_sock *sock, int sz)
     576                 :            : {
     577   [ +  -  +  -  :      18733 :         return sock->net_impl->set_recvbuf(sock, sz);
          +  -  +  -  -  
                +  +  - ]
     578                 :            : }
     579                 :            : 
     580                 :            : int
     581                 :          6 : spdk_sock_set_sendbuf(struct spdk_sock *sock, int sz)
     582                 :            : {
     583   [ #  #  #  #  :          6 :         return sock->net_impl->set_sendbuf(sock, sz);
          #  #  #  #  #  
                #  #  # ]
     584                 :            : }
     585                 :            : 
     586                 :            : bool
     587                 :          6 : spdk_sock_is_ipv6(struct spdk_sock *sock)
     588                 :            : {
     589   [ #  #  #  #  :          6 :         return sock->net_impl->is_ipv6(sock);
          #  #  #  #  #  
                #  #  # ]
     590                 :            : }
     591                 :            : 
     592                 :            : bool
     593                 :      12998 : spdk_sock_is_ipv4(struct spdk_sock *sock)
     594                 :            : {
     595   [ +  -  +  -  :      12998 :         return sock->net_impl->is_ipv4(sock);
          +  -  +  -  -  
                +  +  - ]
     596                 :            : }
     597                 :            : 
     598                 :            : bool
     599                 :     493157 : spdk_sock_is_connected(struct spdk_sock *sock)
     600                 :            : {
     601   [ +  -  +  -  :     493157 :         return sock->net_impl->is_connected(sock);
          +  -  +  -  -  
                +  +  - ]
     602                 :            : }
     603                 :            : 
     604                 :            : struct spdk_sock_group *
     605                 :       2756 : spdk_sock_group_create(void *ctx)
     606                 :            : {
     607                 :       2756 :         struct spdk_net_impl *impl = NULL;
     608                 :            :         struct spdk_sock_group *group;
     609                 :            :         struct spdk_sock_group_impl *group_impl;
     610                 :            : 
     611                 :       2756 :         group = calloc(1, sizeof(*group));
     612         [ +  + ]:       2756 :         if (group == NULL) {
     613                 :          0 :                 return NULL;
     614                 :            :         }
     615                 :            : 
     616   [ +  -  +  -  :       2756 :         STAILQ_INIT(&group->group_impls);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     617   [ +  -  +  -  :       2756 :         STAILQ_INIT(&group->pool);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     618                 :            : 
     619   [ +  +  +  +  :       8766 :         STAILQ_FOREACH_FROM(impl, &g_net_impls, link) {
          +  -  +  -  +  
                      - ]
     620   [ +  -  +  -  :       6010 :                 group_impl = impl->group_impl_create();
             -  +  +  - ]
     621         [ +  + ]:       6010 :                 if (group_impl != NULL) {
     622   [ +  -  +  -  :       6010 :                         STAILQ_INSERT_TAIL(&group->group_impls, group_impl, link);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     623   [ +  -  +  -  :       6010 :                         TAILQ_INIT(&group_impl->socks);
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     624   [ +  -  +  - ]:       6010 :                         group_impl->net_impl = impl;
     625   [ +  -  +  - ]:       6010 :                         group_impl->group = group;
     626                 :        164 :                 }
     627                 :        164 :         }
     628                 :            : 
     629   [ +  -  +  - ]:       2756 :         group->ctx = ctx;
     630                 :            : 
     631                 :       2756 :         return group;
     632                 :         82 : }
     633                 :            : 
     634                 :            : void *
     635                 :       9442 : spdk_sock_group_get_ctx(struct spdk_sock_group *group)
     636                 :            : {
     637         [ +  + ]:       9442 :         if (group == NULL) {
     638                 :          3 :                 return NULL;
     639                 :            :         }
     640                 :            : 
     641   [ +  -  +  - ]:       9439 :         return group->ctx;
     642                 :       1536 : }
     643                 :            : 
     644                 :            : int
     645                 :      11950 : spdk_sock_group_add_sock(struct spdk_sock_group *group, struct spdk_sock *sock,
     646                 :            :                          spdk_sock_cb cb_fn, void *cb_arg)
     647                 :            : {
     648                 :      11950 :         struct spdk_sock_group_impl *group_impl = NULL;
     649                 :            :         int rc;
     650                 :            : 
     651         [ +  + ]:      11950 :         if (cb_fn == NULL) {
     652         [ #  # ]:          6 :                 errno = EINVAL;
     653                 :          6 :                 return -1;
     654                 :            :         }
     655                 :            : 
     656   [ +  +  +  -  :      11944 :         if (sock->group_impl != NULL) {
                   -  + ]
     657                 :            :                 /*
     658                 :            :                  * This sock is already part of a sock_group.
     659                 :            :                  */
     660         [ #  # ]:          6 :                 errno = EINVAL;
     661                 :          6 :                 return -1;
     662                 :            :         }
     663                 :            : 
     664                 :      11938 :         group_impl = sock_get_group_impl_from_group(sock, group);
     665         [ +  + ]:      11938 :         if (group_impl == NULL) {
     666         [ #  # ]:          0 :                 errno = EINVAL;
     667                 :          0 :                 return -1;
     668                 :            :         }
     669                 :            : 
     670   [ +  -  +  -  :      11938 :         rc = group_impl->net_impl->group_impl_add_sock(group_impl, sock);
          +  -  +  -  -  
                +  +  - ]
     671         [ +  + ]:      11938 :         if (rc != 0) {
     672                 :          0 :                 return rc;
     673                 :            :         }
     674                 :            : 
     675   [ +  -  +  -  :      11938 :         TAILQ_INSERT_TAIL(&group_impl->socks, sock, link);
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     676   [ +  -  +  - ]:      11938 :         sock->group_impl = group_impl;
     677   [ +  -  +  - ]:      11938 :         sock->cb_fn = cb_fn;
     678   [ +  -  +  - ]:      11938 :         sock->cb_arg = cb_arg;
     679                 :            : 
     680                 :      11938 :         return 0;
     681                 :       1561 : }
     682                 :            : 
     683                 :            : int
     684                 :      11938 : spdk_sock_group_remove_sock(struct spdk_sock_group *group, struct spdk_sock *sock)
     685                 :            : {
     686                 :      11938 :         struct spdk_sock_group_impl *group_impl = NULL;
     687                 :            :         int rc;
     688                 :            : 
     689                 :      11938 :         group_impl = sock_get_group_impl_from_group(sock, group);
     690         [ +  + ]:      11938 :         if (group_impl == NULL) {
     691         [ #  # ]:          0 :                 errno = EINVAL;
     692                 :          0 :                 return -1;
     693                 :            :         }
     694                 :            : 
     695   [ +  +  +  -  :      11938 :         assert(group_impl == sock->group_impl);
             +  -  #  # ]
     696                 :            : 
     697   [ +  -  +  -  :      11938 :         rc = group_impl->net_impl->group_impl_remove_sock(group_impl, sock);
          +  -  +  -  -  
                +  +  - ]
     698         [ +  + ]:      11938 :         if (rc == 0) {
     699   [ +  +  +  -  :      11938 :                 TAILQ_REMOVE(&group_impl->socks, sock, link);
          +  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                -  +  - ]
     700   [ +  -  +  - ]:      11938 :                 sock->group_impl = NULL;
     701   [ +  -  +  - ]:      11938 :                 sock->cb_fn = NULL;
     702   [ +  -  +  - ]:      11938 :                 sock->cb_arg = NULL;
     703                 :       1561 :         }
     704                 :            : 
     705                 :      11938 :         return rc;
     706                 :       1561 : }
     707                 :            : 
     708                 :            : int
     709                 :         40 : spdk_sock_group_provide_buf(struct spdk_sock_group *group, void *buf, size_t len, void *ctx)
     710                 :            : {
     711                 :            :         struct spdk_sock_group_provided_buf *provided;
     712                 :            : 
     713                 :         40 :         provided = (struct spdk_sock_group_provided_buf *)buf;
     714                 :            : 
     715   [ #  #  #  # ]:         40 :         provided->len = len;
     716   [ #  #  #  # ]:         40 :         provided->ctx = ctx;
     717   [ +  -  #  #  :         40 :         STAILQ_INSERT_HEAD(&group->pool, provided, link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     718                 :            : 
     719                 :         40 :         return 0;
     720                 :            : }
     721                 :            : 
     722                 :            : size_t
     723                 :         36 : spdk_sock_group_get_buf(struct spdk_sock_group *group, void **buf, void **ctx)
     724                 :            : {
     725                 :            :         struct spdk_sock_group_provided_buf *provided;
     726                 :            : 
     727   [ #  #  #  #  :         36 :         provided = STAILQ_FIRST(&group->pool);
                   #  # ]
     728         [ -  + ]:         36 :         if (provided == NULL) {
     729         [ #  # ]:          0 :                 *buf = NULL;
     730                 :          0 :                 return 0;
     731                 :            :         }
     732   [ +  -  #  #  :         36 :         STAILQ_REMOVE_HEAD(&group->pool, link);
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
     733                 :            : 
     734         [ #  # ]:         36 :         *buf = provided;
     735   [ #  #  #  #  :         36 :         *ctx = provided->ctx;
                   #  # ]
     736   [ #  #  #  # ]:         36 :         return provided->len;
     737                 :          0 : }
     738                 :            : 
     739                 :            : int
     740                 : 1079109300 : spdk_sock_group_poll(struct spdk_sock_group *group)
     741                 :            : {
     742                 : 1079109300 :         return spdk_sock_group_poll_count(group, MAX_EVENTS_PER_POLL);
     743                 :            : }
     744                 :            : 
     745                 :            : static int
     746                 : 2309901831 : sock_group_impl_poll_count(struct spdk_sock_group_impl *group_impl,
     747                 :            :                            struct spdk_sock_group *group,
     748                 :            :                            int max_events)
     749                 :            : {
     750                 :  105631945 :         struct spdk_sock *socks[MAX_EVENTS_PER_POLL];
     751                 :            :         int num_events, i;
     752                 :            : 
     753   [ +  +  +  -  : 2309901831 :         if (TAILQ_EMPTY(&group_impl->socks)) {
             +  -  +  + ]
     754                 : 1253626587 :                 return 0;
     755                 :            :         }
     756                 :            : 
     757   [ +  -  +  -  : 1056275244 :         num_events = group_impl->net_impl->group_impl_poll(group_impl, max_events, socks);
          +  -  +  -  -  
                +  +  - ]
     758         [ +  + ]: 1056275244 :         if (num_events == -1) {
     759                 :          0 :                 return -1;
     760                 :            :         }
     761                 :            : 
     762   [ +  +  +  - ]: 1261961530 :         for (i = 0; i < num_events; i++) {
     763   [ +  -  +  -  :  205686286 :                 struct spdk_sock *sock = socks[i];
                   +  - ]
     764   [ +  +  +  -  :  205686286 :                 assert(sock->cb_fn != NULL);
             +  -  #  # ]
     765   [ +  -  +  -  :  205686286 :                 sock->cb_fn(sock->cb_arg, group, sock);
          -  +  +  -  +  
                -  +  - ]
     766                 :      19486 :         }
     767                 :            : 
     768                 : 1056275244 :         return num_events;
     769                 :    1231458 : }
     770                 :            : 
     771                 :            : int
     772                 : 1079109312 : spdk_sock_group_poll_count(struct spdk_sock_group *group, int max_events)
     773                 :            : {
     774                 : 1079109312 :         struct spdk_sock_group_impl *group_impl = NULL;
     775                 : 1079109312 :         int rc, num_events = 0;
     776                 :            : 
     777         [ +  + ]: 1079109312 :         if (max_events < 1) {
     778         [ #  # ]:          0 :                 errno = -EINVAL;
     779                 :          0 :                 return -1;
     780                 :            :         }
     781                 :            : 
     782                 :            :         /*
     783                 :            :          * Only poll for up to 32 events at a time - if more events are pending,
     784                 :            :          *  the next call to this function will reap them.
     785                 :            :          */
     786         [ +  + ]: 1079109312 :         if (max_events > MAX_EVENTS_PER_POLL) {
     787                 :          0 :                 max_events = MAX_EVENTS_PER_POLL;
     788                 :          0 :         }
     789                 :            : 
     790   [ +  -  +  +  : 3389011143 :         STAILQ_FOREACH_FROM(group_impl, &group->group_impls, link) {
          +  -  +  -  +  
          +  +  -  +  -  
                   +  - ]
     791                 : 2309901831 :                 rc = sock_group_impl_poll_count(group_impl, group, max_events);
     792         [ -  + ]: 2309901831 :                 if (rc < 0) {
     793                 :          0 :                         num_events = -1;
     794   [ #  #  #  #  :          0 :                         SPDK_ERRLOG("group_impl_poll_count for net(%s) failed\n",
             #  #  #  # ]
     795                 :            :                                     group_impl->net_impl->name);
     796         [ +  + ]: 2309901831 :                 } else if (num_events >= 0) {
     797         [ +  - ]: 2309901831 :                         num_events += rc;
     798                 :    1231458 :                 }
     799                 :    1231458 :         }
     800                 :            : 
     801                 : 1079109312 :         return num_events;
     802                 :     615729 : }
     803                 :            : 
     804                 :            : int
     805                 :       2778 : spdk_sock_group_close(struct spdk_sock_group **group)
     806                 :            : {
     807                 :       2778 :         struct spdk_sock_group_impl *group_impl = NULL, *tmp;
     808                 :            :         int rc;
     809                 :            : 
     810   [ +  +  +  - ]:       2778 :         if (*group == NULL) {
     811         [ #  # ]:         16 :                 errno = EBADF;
     812                 :         16 :                 return -1;
     813                 :            :         }
     814                 :            : 
     815   [ +  +  +  -  :       8778 :         STAILQ_FOREACH_SAFE(group_impl, &(*group)->group_impls, link, tmp) {
          +  -  +  -  +  
          +  +  -  +  -  
             +  -  +  + ]
     816   [ +  +  +  -  :       6022 :                 if (!TAILQ_EMPTY(&group_impl->socks)) {
             +  -  +  - ]
     817         [ #  # ]:          6 :                         errno = EBUSY;
     818                 :          6 :                         return -1;
     819                 :            :                 }
     820                 :        164 :         }
     821                 :            : 
     822   [ +  +  +  -  :       8766 :         STAILQ_FOREACH_SAFE(group_impl, &(*group)->group_impls, link, tmp) {
          +  -  +  -  +  
          +  +  -  +  -  
             +  -  +  + ]
     823   [ +  -  +  -  :       6010 :                 rc = group_impl->net_impl->group_impl_close(group_impl);
          +  -  +  -  -  
                +  +  - ]
     824         [ +  + ]:       6010 :                 if (rc != 0) {
     825                 :          0 :                         SPDK_ERRLOG("group_impl_close for net failed\n");
     826                 :          0 :                 }
     827                 :        164 :         }
     828                 :            : 
     829         [ +  - ]:       2756 :         free(*group);
     830         [ +  - ]:       2756 :         *group = NULL;
     831                 :            : 
     832                 :       2756 :         return 0;
     833                 :         82 : }
     834                 :            : 
     835                 :            : static inline struct spdk_net_impl *
     836                 :       3584 : sock_get_impl_by_name(const char *impl_name)
     837                 :            : {
     838                 :            :         struct spdk_net_impl *impl;
     839                 :            : 
     840   [ +  +  #  # ]:       3584 :         assert(impl_name != NULL);
     841   [ +  -  +  -  :       4562 :         STAILQ_FOREACH(impl, &g_net_impls, link) {
             +  -  +  - ]
     842   [ +  +  +  +  :       4562 :                 if (0 == strcmp(impl_name, impl->name)) {
          +  +  +  -  +  
                      + ]
     843                 :       3584 :                         return impl;
     844                 :            :                 }
     845                 :          3 :         }
     846                 :            : 
     847                 :          0 :         return NULL;
     848                 :         65 : }
     849                 :            : 
     850                 :            : int
     851                 :        393 : spdk_sock_impl_get_opts(const char *impl_name, struct spdk_sock_impl_opts *opts, size_t *len)
     852                 :            : {
     853                 :            :         struct spdk_net_impl *impl;
     854                 :            : 
     855   [ +  -  +  +  :        393 :         if (!impl_name || !opts || !len) {
                   +  + ]
     856         [ #  # ]:         12 :                 errno = EINVAL;
     857                 :         12 :                 return -1;
     858                 :            :         }
     859                 :            : 
     860                 :        381 :         impl = sock_get_impl_by_name(impl_name);
     861         [ +  + ]:        381 :         if (!impl) {
     862         [ #  # ]:          0 :                 errno = EINVAL;
     863                 :          0 :                 return -1;
     864                 :            :         }
     865                 :            : 
     866   [ +  +  +  -  :        381 :         if (!impl->get_opts) {
                   +  - ]
     867         [ #  # ]:          3 :                 errno = ENOTSUP;
     868                 :          3 :                 return -1;
     869                 :            :         }
     870                 :            : 
     871   [ +  -  +  -  :        378 :         return impl->get_opts(opts, len);
             -  +  -  + ]
     872                 :          2 : }
     873                 :            : 
     874                 :            : int
     875                 :        158 : spdk_sock_impl_set_opts(const char *impl_name, const struct spdk_sock_impl_opts *opts, size_t len)
     876                 :            : {
     877                 :            :         struct spdk_net_impl *impl;
     878                 :            : 
     879   [ +  -  +  + ]:        158 :         if (!impl_name || !opts) {
     880         [ #  # ]:          6 :                 errno = EINVAL;
     881                 :          6 :                 return -1;
     882                 :            :         }
     883                 :            : 
     884                 :        152 :         impl = sock_get_impl_by_name(impl_name);
     885         [ +  + ]:        152 :         if (!impl) {
     886         [ #  # ]:          0 :                 errno = EINVAL;
     887                 :          0 :                 return -1;
     888                 :            :         }
     889                 :            : 
     890   [ +  +  +  -  :        152 :         if (!impl->set_opts) {
                   +  - ]
     891         [ #  # ]:          3 :                 errno = ENOTSUP;
     892                 :          3 :                 return -1;
     893                 :            :         }
     894                 :            : 
     895   [ +  -  +  -  :        149 :         return impl->set_opts(opts, len);
             -  +  -  + ]
     896                 :          2 : }
     897                 :            : 
     898                 :            : void
     899                 :        121 : spdk_sock_write_config_json(struct spdk_json_write_ctx *w)
     900                 :            : {
     901                 :            :         struct spdk_net_impl *impl;
     902                 :         42 :         struct spdk_sock_impl_opts opts;
     903                 :         42 :         size_t len;
     904                 :            : 
     905   [ +  +  #  # ]:        121 :         assert(w != NULL);
     906                 :            : 
     907                 :        121 :         spdk_json_write_array_begin(w);
     908                 :            : 
     909         [ +  + ]:        121 :         if (g_default_impl) {
     910                 :        121 :                 spdk_json_write_object_begin(w);
     911                 :        121 :                 spdk_json_write_named_string(w, "method", "sock_set_default_impl");
     912                 :        121 :                 spdk_json_write_named_object_begin(w, "params");
     913   [ +  -  +  - ]:        121 :                 spdk_json_write_named_string(w, "impl_name", g_default_impl->name);
     914                 :        121 :                 spdk_json_write_object_end(w);
     915                 :        121 :                 spdk_json_write_object_end(w);
     916                 :          1 :         }
     917                 :            : 
     918   [ +  +  +  -  :        380 :         STAILQ_FOREACH(impl, &g_net_impls, link) {
             +  -  +  - ]
     919   [ +  +  +  -  :        259 :                 if (!impl->get_opts) {
                   +  - ]
     920                 :          0 :                         continue;
     921                 :            :                 }
     922                 :            : 
     923                 :        259 :                 len = sizeof(opts);
     924   [ +  -  +  -  :        259 :                 if (impl->get_opts(&opts, &len) == 0) {
          -  +  +  -  -  
                      + ]
     925                 :        259 :                         spdk_json_write_object_begin(w);
     926                 :        259 :                         spdk_json_write_named_string(w, "method", "sock_impl_set_options");
     927                 :        259 :                         spdk_json_write_named_object_begin(w, "params");
     928   [ +  -  +  - ]:        259 :                         spdk_json_write_named_string(w, "impl_name", impl->name);
     929                 :        259 :                         spdk_json_write_named_uint32(w, "recv_buf_size", opts.recv_buf_size);
     930         [ +  - ]:        259 :                         spdk_json_write_named_uint32(w, "send_buf_size", opts.send_buf_size);
     931   [ +  +  +  - ]:        259 :                         spdk_json_write_named_bool(w, "enable_recv_pipe", opts.enable_recv_pipe);
     932   [ +  +  +  - ]:        259 :                         spdk_json_write_named_bool(w, "enable_quickack", opts.enable_quickack);
     933         [ +  - ]:        259 :                         spdk_json_write_named_uint32(w, "enable_placement_id", opts.enable_placement_id);
     934   [ +  +  +  - ]:        259 :                         spdk_json_write_named_bool(w, "enable_zerocopy_send_server", opts.enable_zerocopy_send_server);
     935         [ +  + ]:        259 :                         spdk_json_write_named_bool(w, "enable_zerocopy_send_client", opts.enable_zerocopy_send_client);
     936         [ +  - ]:        259 :                         spdk_json_write_named_uint32(w, "zerocopy_threshold", opts.zerocopy_threshold);
     937         [ +  - ]:        259 :                         spdk_json_write_named_uint32(w, "tls_version", opts.tls_version);
     938   [ +  +  +  - ]:        259 :                         spdk_json_write_named_bool(w, "enable_ktls", opts.enable_ktls);
     939                 :        259 :                         spdk_json_write_object_end(w);
     940                 :        259 :                         spdk_json_write_object_end(w);
     941                 :          2 :                 } else {
     942   [ #  #  #  # ]:          0 :                         SPDK_ERRLOG("Failed to get socket options for socket implementation %s\n", impl->name);
     943                 :            :                 }
     944                 :          2 :         }
     945                 :            : 
     946                 :        121 :         spdk_json_write_array_end(w);
     947                 :        121 : }
     948                 :            : 
     949                 :            : void
     950                 :       5713 : spdk_net_impl_register(struct spdk_net_impl *impl)
     951                 :            : {
     952   [ +  +  +  -  :       5713 :         STAILQ_INSERT_HEAD(&g_net_impls, impl, link);
          +  -  +  +  +  
             -  +  -  +  
                      - ]
     953                 :       5713 : }
     954                 :            : 
     955                 :            : int
     956                 :       3057 : spdk_sock_set_default_impl(const char *impl_name)
     957                 :            : {
     958                 :            :         struct spdk_net_impl *impl;
     959                 :            : 
     960         [ +  + ]:       3057 :         if (!impl_name) {
     961         [ #  # ]:          6 :                 errno = EINVAL;
     962                 :          6 :                 return -1;
     963                 :            :         }
     964                 :            : 
     965                 :       3051 :         impl = sock_get_impl_by_name(impl_name);
     966         [ +  + ]:       3051 :         if (!impl) {
     967         [ #  # ]:          0 :                 errno = EINVAL;
     968                 :          0 :                 return -1;
     969                 :            :         }
     970                 :            : 
     971         [ +  + ]:       3051 :         if (impl == g_default_impl) {
     972                 :         64 :                 return 0;
     973                 :            :         }
     974                 :            : 
     975         [ +  + ]:       2987 :         if (g_default_impl) {
     976   [ -  +  -  +  :        334 :                 SPDK_DEBUGLOG(sock, "Change the default sock impl from %s to %s\n", g_default_impl->name,
          #  #  #  #  #  
             #  #  #  #  
                      # ]
     977                 :            :                               impl->name);
     978                 :          0 :         } else {
     979   [ +  +  +  +  :       2653 :                 SPDK_DEBUGLOG(sock, "Set default sock implementation to %s\n", impl_name);
                   +  - ]
     980                 :            :         }
     981                 :            : 
     982                 :       2987 :         g_default_impl = impl;
     983                 :            : 
     984                 :       2987 :         return 0;
     985                 :         61 : }
     986                 :            : 
     987                 :            : const char *
     988                 :          2 : spdk_sock_get_default_impl(void)
     989                 :            : {
     990         [ +  - ]:          2 :         if (g_default_impl) {
     991   [ #  #  #  # ]:          2 :                 return g_default_impl->name;
     992                 :            :         }
     993                 :            : 
     994                 :          0 :         return NULL;
     995                 :          0 : }
     996                 :            : 
     997                 :            : int
     998                 :         99 : spdk_sock_group_register_interrupt(struct spdk_sock_group *group, uint32_t events,
     999                 :            :                                    spdk_interrupt_fn fn,
    1000                 :            :                                    void *arg, const char *name)
    1001                 :            : {
    1002                 :         99 :         struct spdk_sock_group_impl *group_impl = NULL;
    1003                 :            :         int rc;
    1004                 :            : 
    1005   [ -  +  #  # ]:         99 :         assert(group != NULL);
    1006   [ -  +  #  # ]:         99 :         assert(fn != NULL);
    1007                 :            : 
    1008   [ +  -  +  +  :        297 :         STAILQ_FOREACH_FROM(group_impl, &group->group_impls, link) {
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    1009   [ #  #  #  #  :        198 :                 rc = group_impl->net_impl->group_impl_register_interrupt(group_impl, events, fn, arg, name);
          #  #  #  #  #  
                #  #  # ]
    1010         [ -  + ]:        198 :                 if (rc != 0) {
    1011                 :          0 :                         return rc;
    1012                 :            :                 }
    1013                 :          0 :         }
    1014                 :            : 
    1015                 :         99 :         return 0;
    1016                 :          0 : }
    1017                 :            : 
    1018                 :            : void
    1019                 :        870 : spdk_sock_group_unregister_interrupt(struct spdk_sock_group *group)
    1020                 :            : {
    1021                 :        870 :         struct spdk_sock_group_impl *group_impl = NULL;
    1022                 :            : 
    1023   [ +  +  #  # ]:        870 :         assert(group != NULL);
    1024                 :            : 
    1025   [ +  +  +  +  :       2754 :         STAILQ_FOREACH_FROM(group_impl, &group->group_impls, link) {
          +  -  +  -  +  
          +  +  -  +  -  
                   +  - ]
    1026   [ +  -  +  -  :       1884 :                 group_impl->net_impl->group_impl_unregister_interrupt(group_impl);
          +  -  +  -  -  
                +  +  - ]
    1027                 :        108 :         }
    1028                 :        870 : }
    1029                 :            : 
    1030                 :       2667 : SPDK_LOG_REGISTER_COMPONENT(sock)
    1031                 :            : 
    1032                 :            : static void
    1033                 :       2116 : sock_trace(void)
    1034                 :            : {
    1035                 :       2116 :         struct spdk_trace_tpoint_opts opts[] = {
    1036                 :            :                 {
    1037                 :            :                         "SOCK_REQ_QUEUE", TRACE_SOCK_REQ_QUEUE,
    1038                 :            :                         OWNER_TYPE_SOCK, OBJECT_SOCK_REQ, 1,
    1039                 :            :                         {
    1040                 :            :                                 { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 },
    1041                 :            :                         }
    1042                 :            :                 },
    1043                 :            :                 {
    1044                 :            :                         "SOCK_REQ_PEND", TRACE_SOCK_REQ_PEND,
    1045                 :            :                         OWNER_TYPE_SOCK, OBJECT_SOCK_REQ, 0,
    1046                 :            :                         {
    1047                 :            :                                 { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 },
    1048                 :            :                         }
    1049                 :            :                 },
    1050                 :            :                 {
    1051                 :            :                         "SOCK_REQ_COMPLETE", TRACE_SOCK_REQ_COMPLETE,
    1052                 :            :                         OWNER_TYPE_SOCK, OBJECT_SOCK_REQ, 0,
    1053                 :            :                         {
    1054                 :            :                                 { "ctx", SPDK_TRACE_ARG_TYPE_PTR, 8 },
    1055                 :            :                         }
    1056                 :            :                 },
    1057                 :            :         };
    1058                 :            : 
    1059                 :       2116 :         spdk_trace_register_owner_type(OWNER_TYPE_SOCK, 's');
    1060                 :       2116 :         spdk_trace_register_object(OBJECT_SOCK_REQ, 's');
    1061                 :       2116 :         spdk_trace_register_description_ext(opts, SPDK_COUNTOF(opts));
    1062                 :       2116 : }
    1063                 :       2667 : SPDK_TRACE_REGISTER_FN(sock_trace, "sock", TRACE_GROUP_SOCK)

Generated by: LCOV version 1.15