LCOV - code coverage report
Current view: top level - lib/fib - rte_fib.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 103 131 78.6 %
Date: 2024-01-22 15:55:54 Functions: 12 14 85.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 53 85 62.4 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2018 Vladimir Medvedkin <medvedkinv@gmail.com>
       3                 :            :  * Copyright(c) 2019 Intel Corporation
       4                 :            :  */
       5                 :            : 
       6                 :            : #include <stdint.h>
       7                 :            : #include <string.h>
       8                 :            : #include <sys/queue.h>
       9                 :            : 
      10                 :            : #include <rte_eal_memconfig.h>
      11                 :            : #include <rte_errno.h>
      12                 :            : #include <rte_malloc.h>
      13                 :            : #include <rte_string_fns.h>
      14                 :            : #include <rte_tailq.h>
      15                 :            : 
      16                 :            : #include <rte_rib.h>
      17                 :            : #include <rte_fib.h>
      18                 :            : 
      19                 :            : #include "dir24_8.h"
      20                 :            : #include "fib_log.h"
      21                 :            : 
      22         [ -  + ]:        235 : RTE_LOG_REGISTER_DEFAULT(fib_logtype, INFO);
      23                 :            : 
      24                 :            : TAILQ_HEAD(rte_fib_list, rte_tailq_entry);
      25                 :            : static struct rte_tailq_elem rte_fib_tailq = {
      26                 :            :         .name = "RTE_FIB",
      27                 :            : };
      28         [ -  + ]:        235 : EAL_REGISTER_TAILQ(rte_fib_tailq)
      29                 :            : 
      30                 :            : /* Maximum length of a FIB name. */
      31                 :            : #define RTE_FIB_NAMESIZE        64
      32                 :            : 
      33                 :            : #if defined(RTE_LIBRTE_FIB_DEBUG)
      34                 :            : #define FIB_RETURN_IF_TRUE(cond, retval) do {           \
      35                 :            :         if (cond)                                       \
      36                 :            :                 return retval;                          \
      37                 :            : } while (0)
      38                 :            : #else
      39                 :            : #define FIB_RETURN_IF_TRUE(cond, retval)
      40                 :            : #endif
      41                 :            : 
      42                 :            : struct rte_fib {
      43                 :            :         char                    name[RTE_FIB_NAMESIZE];
      44                 :            :         enum rte_fib_type       type;   /**< Type of FIB struct */
      45                 :            :         struct rte_rib          *rib;   /**< RIB helper datastructure */
      46                 :            :         void                    *dp;    /**< pointer to the dataplane struct*/
      47                 :            :         rte_fib_lookup_fn_t     lookup; /**< FIB lookup function */
      48                 :            :         rte_fib_modify_fn_t     modify; /**< modify FIB datastructure */
      49                 :            :         uint64_t                def_nh;
      50                 :            : };
      51                 :            : 
      52                 :            : static void
      53                 :        258 : dummy_lookup(void *fib_p, const uint32_t *ips, uint64_t *next_hops,
      54                 :            :         const unsigned int n)
      55                 :            : {
      56                 :            :         unsigned int i;
      57                 :            :         struct rte_fib *fib = fib_p;
      58                 :            :         struct rte_rib_node *node;
      59                 :            : 
      60         [ +  + ]:       4515 :         for (i = 0; i < n; i++) {
      61                 :       4257 :                 node = rte_rib_lookup(fib->rib, ips[i]);
      62         [ +  + ]:       4257 :                 if (node != NULL)
      63                 :       3040 :                         rte_rib_get_nh(node, &next_hops[i]);
      64                 :            :                 else
      65                 :       1217 :                         next_hops[i] = fib->def_nh;
      66                 :            :         }
      67                 :        258 : }
      68                 :            : 
      69                 :            : static int
      70                 :        128 : dummy_modify(struct rte_fib *fib, uint32_t ip, uint8_t depth,
      71                 :            :         uint64_t next_hop, int op)
      72                 :            : {
      73                 :            :         struct rte_rib_node *node;
      74         [ +  - ]:        128 :         if ((fib == NULL) || (depth > RTE_FIB_MAXDEPTH))
      75                 :            :                 return -EINVAL;
      76                 :            : 
      77                 :        128 :         node = rte_rib_lookup_exact(fib->rib, ip, depth);
      78                 :            : 
      79      [ +  +  - ]:        128 :         switch (op) {
      80                 :         64 :         case RTE_FIB_ADD:
      81         [ +  - ]:         64 :                 if (node == NULL)
      82                 :         64 :                         node = rte_rib_insert(fib->rib, ip, depth);
      83         [ -  + ]:         64 :                 if (node == NULL)
      84                 :          0 :                         return -rte_errno;
      85                 :         64 :                 return rte_rib_set_nh(node, next_hop);
      86                 :         64 :         case RTE_FIB_DEL:
      87         [ +  - ]:         64 :                 if (node == NULL)
      88                 :            :                         return -ENOENT;
      89                 :         64 :                 rte_rib_remove(fib->rib, ip, depth);
      90                 :         64 :                 return 0;
      91                 :            :         }
      92                 :            :         return -EINVAL;
      93                 :            : }
      94                 :            : 
      95                 :            : static int
      96      [ +  +  - ]:          9 : init_dataplane(struct rte_fib *fib, __rte_unused int socket_id,
      97                 :            :         struct rte_fib_conf *conf)
      98                 :            : {
      99                 :            :         char dp_name[sizeof(void *)];
     100                 :            : 
     101                 :            :         snprintf(dp_name, sizeof(dp_name), "%p", fib);
     102      [ +  +  - ]:          9 :         switch (conf->type) {
     103                 :          3 :         case RTE_FIB_DUMMY:
     104                 :          3 :                 fib->dp = fib;
     105                 :          3 :                 fib->lookup = dummy_lookup;
     106                 :          3 :                 fib->modify = dummy_modify;
     107                 :          3 :                 return 0;
     108                 :          6 :         case RTE_FIB_DIR24_8:
     109                 :          6 :                 fib->dp = dir24_8_create(dp_name, socket_id, conf);
     110         [ +  + ]:          6 :                 if (fib->dp == NULL)
     111                 :          2 :                         return -rte_errno;
     112                 :          4 :                 fib->lookup = dir24_8_get_lookup_fn(fib->dp,
     113                 :            :                         RTE_FIB_LOOKUP_DEFAULT);
     114                 :          4 :                 fib->modify = dir24_8_modify;
     115                 :          4 :                 return 0;
     116                 :            :         default:
     117                 :            :                 return -EINVAL;
     118                 :            :         }
     119                 :            :         return 0;
     120                 :            : }
     121                 :            : 
     122                 :            : int
     123                 :        322 : rte_fib_add(struct rte_fib *fib, uint32_t ip, uint8_t depth, uint64_t next_hop)
     124                 :            : {
     125   [ +  +  +  -  :        322 :         if ((fib == NULL) || (fib->modify == NULL) ||
                   +  + ]
     126                 :            :                         (depth > RTE_FIB_MAXDEPTH))
     127                 :            :                 return -EINVAL;
     128                 :        320 :         return fib->modify(fib, ip, depth, next_hop, RTE_FIB_ADD);
     129                 :            : }
     130                 :            : 
     131                 :            : int
     132                 :        322 : rte_fib_delete(struct rte_fib *fib, uint32_t ip, uint8_t depth)
     133                 :            : {
     134   [ +  +  +  -  :        322 :         if ((fib == NULL) || (fib->modify == NULL) ||
                   +  + ]
     135                 :            :                         (depth > RTE_FIB_MAXDEPTH))
     136                 :            :                 return -EINVAL;
     137                 :        320 :         return fib->modify(fib, ip, depth, 0, RTE_FIB_DEL);
     138                 :            : }
     139                 :            : 
     140                 :            : int
     141                 :       1290 : rte_fib_lookup_bulk(struct rte_fib *fib, uint32_t *ips,
     142                 :            :         uint64_t *next_hops, int n)
     143                 :            : {
     144                 :            :         FIB_RETURN_IF_TRUE(((fib == NULL) || (ips == NULL) ||
     145                 :            :                 (next_hops == NULL) || (fib->lookup == NULL)), -EINVAL);
     146                 :            : 
     147                 :       1290 :         fib->lookup(fib->dp, ips, next_hops, n);
     148                 :       1290 :         return 0;
     149                 :            : }
     150                 :            : 
     151                 :            : struct rte_fib *
     152                 :         14 : rte_fib_create(const char *name, int socket_id, struct rte_fib_conf *conf)
     153                 :            : {
     154                 :            :         char mem_name[RTE_FIB_NAMESIZE];
     155                 :            :         int ret;
     156                 :            :         struct rte_fib *fib = NULL;
     157                 :            :         struct rte_rib *rib = NULL;
     158                 :            :         struct rte_tailq_entry *te;
     159                 :            :         struct rte_fib_list *fib_list;
     160                 :            :         struct rte_rib_conf rib_conf;
     161                 :            : 
     162                 :            :         /* Check user arguments. */
     163   [ +  +  +  - ]:         14 :         if ((name == NULL) || (conf == NULL) || (conf->max_routes < 0) ||
     164         [ +  + ]:         12 :                         (conf->type > RTE_FIB_DIR24_8)) {
     165                 :          3 :                 rte_errno = EINVAL;
     166                 :          3 :                 return NULL;
     167                 :            :         }
     168                 :            : 
     169                 :         11 :         rib_conf.ext_sz = conf->rib_ext_sz;
     170                 :         11 :         rib_conf.max_nodes = conf->max_routes * 2;
     171                 :            : 
     172                 :         11 :         rib = rte_rib_create(name, socket_id, &rib_conf);
     173         [ +  + ]:         11 :         if (rib == NULL) {
     174                 :          2 :                 FIB_LOG(ERR,
     175                 :            :                         "Can not allocate RIB %s", name);
     176                 :          2 :                 return NULL;
     177                 :            :         }
     178                 :            : 
     179                 :            :         snprintf(mem_name, sizeof(mem_name), "FIB_%s", name);
     180                 :          9 :         fib_list = RTE_TAILQ_CAST(rte_fib_tailq.head, rte_fib_list);
     181                 :            : 
     182                 :          9 :         rte_mcfg_tailq_write_lock();
     183                 :            : 
     184                 :            :         /* guarantee there's no existing */
     185         [ -  + ]:          9 :         TAILQ_FOREACH(te, fib_list, next) {
     186                 :          0 :                 fib = (struct rte_fib *)te->data;
     187         [ #  # ]:          0 :                 if (strncmp(name, fib->name, RTE_FIB_NAMESIZE) == 0)
     188                 :            :                         break;
     189                 :            :         }
     190                 :            :         fib = NULL;
     191         [ -  + ]:          9 :         if (te != NULL) {
     192                 :          0 :                 rte_errno = EEXIST;
     193                 :          0 :                 goto exit;
     194                 :            :         }
     195                 :            : 
     196                 :            :         /* allocate tailq entry */
     197                 :          9 :         te = rte_zmalloc("FIB_TAILQ_ENTRY", sizeof(*te), 0);
     198         [ -  + ]:          9 :         if (te == NULL) {
     199                 :          0 :                 FIB_LOG(ERR,
     200                 :            :                         "Can not allocate tailq entry for FIB %s", name);
     201                 :          0 :                 rte_errno = ENOMEM;
     202                 :          0 :                 goto exit;
     203                 :            :         }
     204                 :            : 
     205                 :            :         /* Allocate memory to store the FIB data structures. */
     206                 :          9 :         fib = rte_zmalloc_socket(mem_name,
     207                 :            :                 sizeof(struct rte_fib), RTE_CACHE_LINE_SIZE, socket_id);
     208         [ -  + ]:          9 :         if (fib == NULL) {
     209                 :          0 :                 FIB_LOG(ERR, "FIB %s memory allocation failed", name);
     210                 :          0 :                 rte_errno = ENOMEM;
     211                 :          0 :                 goto free_te;
     212                 :            :         }
     213                 :            : 
     214                 :          9 :         rte_strlcpy(fib->name, name, sizeof(fib->name));
     215                 :          9 :         fib->rib = rib;
     216                 :          9 :         fib->type = conf->type;
     217                 :          9 :         fib->def_nh = conf->default_nh;
     218                 :          9 :         ret = init_dataplane(fib, socket_id, conf);
     219         [ +  + ]:          9 :         if (ret < 0) {
     220                 :          2 :                 FIB_LOG(ERR,
     221                 :            :                         "FIB dataplane struct %s memory allocation failed "
     222                 :            :                         "with err %d", name, ret);
     223                 :          2 :                 rte_errno = -ret;
     224                 :          2 :                 goto free_fib;
     225                 :            :         }
     226                 :            : 
     227                 :          7 :         te->data = (void *)fib;
     228                 :          7 :         TAILQ_INSERT_TAIL(fib_list, te, next);
     229                 :            : 
     230                 :          7 :         rte_mcfg_tailq_write_unlock();
     231                 :            : 
     232                 :          7 :         return fib;
     233                 :            : 
     234                 :            : free_fib:
     235                 :          2 :         rte_free(fib);
     236                 :          2 : free_te:
     237                 :          2 :         rte_free(te);
     238                 :          2 : exit:
     239                 :          2 :         rte_mcfg_tailq_write_unlock();
     240                 :          2 :         rte_rib_free(rib);
     241                 :            : 
     242                 :          2 :         return NULL;
     243                 :            : }
     244                 :            : 
     245                 :            : struct rte_fib *
     246                 :          0 : rte_fib_find_existing(const char *name)
     247                 :            : {
     248                 :            :         struct rte_fib *fib = NULL;
     249                 :            :         struct rte_tailq_entry *te;
     250                 :            :         struct rte_fib_list *fib_list;
     251                 :            : 
     252                 :          0 :         fib_list = RTE_TAILQ_CAST(rte_fib_tailq.head, rte_fib_list);
     253                 :            : 
     254                 :          0 :         rte_mcfg_tailq_read_lock();
     255         [ #  # ]:          0 :         TAILQ_FOREACH(te, fib_list, next) {
     256                 :          0 :                 fib = (struct rte_fib *) te->data;
     257         [ #  # ]:          0 :                 if (strncmp(name, fib->name, RTE_FIB_NAMESIZE) == 0)
     258                 :            :                         break;
     259                 :            :         }
     260                 :          0 :         rte_mcfg_tailq_read_unlock();
     261                 :            : 
     262         [ #  # ]:          0 :         if (te == NULL) {
     263                 :          0 :                 rte_errno = ENOENT;
     264                 :          0 :                 return NULL;
     265                 :            :         }
     266                 :            : 
     267                 :            :         return fib;
     268                 :            : }
     269                 :            : 
     270                 :            : static void
     271                 :            : free_dataplane(struct rte_fib *fib)
     272                 :            : {
     273         [ +  + ]:          7 :         switch (fib->type) {
     274                 :            :         case RTE_FIB_DUMMY:
     275                 :            :                 return;
     276                 :          4 :         case RTE_FIB_DIR24_8:
     277                 :          4 :                 dir24_8_free(fib->dp);
     278                 :            :         default:
     279                 :            :                 return;
     280                 :            :         }
     281                 :            : }
     282                 :            : 
     283                 :            : void
     284                 :          8 : rte_fib_free(struct rte_fib *fib)
     285                 :            : {
     286                 :            :         struct rte_tailq_entry *te;
     287                 :            :         struct rte_fib_list *fib_list;
     288                 :            : 
     289         [ +  + ]:          8 :         if (fib == NULL)
     290                 :            :                 return;
     291                 :            : 
     292                 :          7 :         fib_list = RTE_TAILQ_CAST(rte_fib_tailq.head, rte_fib_list);
     293                 :            : 
     294                 :          7 :         rte_mcfg_tailq_write_lock();
     295                 :            : 
     296                 :            :         /* find our tailq entry */
     297         [ +  - ]:          7 :         TAILQ_FOREACH(te, fib_list, next) {
     298         [ -  + ]:          7 :                 if (te->data == (void *)fib)
     299                 :            :                         break;
     300                 :            :         }
     301         [ +  - ]:          7 :         if (te != NULL)
     302         [ -  + ]:          7 :                 TAILQ_REMOVE(fib_list, te, next);
     303                 :            : 
     304                 :          7 :         rte_mcfg_tailq_write_unlock();
     305                 :            : 
     306                 :            :         free_dataplane(fib);
     307                 :          7 :         rte_rib_free(fib->rib);
     308                 :          7 :         rte_free(fib);
     309                 :          7 :         rte_free(te);
     310                 :            : }
     311                 :            : 
     312                 :            : void *
     313                 :        513 : rte_fib_get_dp(struct rte_fib *fib)
     314                 :            : {
     315         [ +  + ]:        513 :         return (fib == NULL) ? NULL : fib->dp;
     316                 :            : }
     317                 :            : 
     318                 :            : struct rte_rib *
     319                 :        513 : rte_fib_get_rib(struct rte_fib *fib)
     320                 :            : {
     321         [ +  + ]:        513 :         return (fib == NULL) ? NULL : fib->rib;
     322                 :            : }
     323                 :            : 
     324                 :            : int
     325                 :          0 : rte_fib_select_lookup(struct rte_fib *fib,
     326                 :            :         enum rte_fib_lookup_type type)
     327                 :            : {
     328                 :            :         rte_fib_lookup_fn_t fn;
     329                 :            : 
     330         [ #  # ]:          0 :         switch (fib->type) {
     331                 :          0 :         case RTE_FIB_DIR24_8:
     332                 :          0 :                 fn = dir24_8_get_lookup_fn(fib->dp, type);
     333         [ #  # ]:          0 :                 if (fn == NULL)
     334                 :            :                         return -EINVAL;
     335                 :          0 :                 fib->lookup = fn;
     336                 :          0 :                 return 0;
     337                 :            :         default:
     338                 :            :                 return -EINVAL;
     339                 :            :         }
     340                 :            : }

Generated by: LCOV version 1.14