LCOV - code coverage report
Current view: top level - lib/fib - trie.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 326 357 91.3 %
Date: 2026-12-01 19:01:22 Functions: 18 18 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 194 260 74.6 %

           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 <stdio.h>
       8                 :            : 
       9                 :            : #include <rte_debug.h>
      10                 :            : #include <rte_malloc.h>
      11                 :            : #include <rte_cpuflags.h>
      12                 :            : #include <rte_errno.h>
      13                 :            : 
      14                 :            : #include <rte_rib6.h>
      15                 :            : #include <rte_fib6.h>
      16                 :            : #include <rib6_internal.h>
      17                 :            : #include "fib_log.h"
      18                 :            : #include "trie.h"
      19                 :            : 
      20                 :            : #ifdef CC_AVX512_SUPPORT
      21                 :            : 
      22                 :            : #include "trie_avx512.h"
      23                 :            : 
      24                 :            : #endif /* CC_AVX512_SUPPORT */
      25                 :            : 
      26                 :            : #define TRIE_NAMESIZE           64
      27                 :            : 
      28                 :            : enum edge {
      29                 :            :         LEDGE,
      30                 :            :         REDGE
      31                 :            : };
      32                 :            : 
      33                 :            : static inline rte_fib6_lookup_fn_t
      34                 :            : get_scalar_fn(enum rte_fib_trie_nh_sz nh_sz)
      35                 :            : {
      36   [ -  -  -  -  :         15 :         switch (nh_sz) {
             +  +  -  + ]
      37                 :            :         case RTE_FIB6_TRIE_2B:
      38                 :            :                 return rte_trie_lookup_bulk_2b;
      39                 :          5 :         case RTE_FIB6_TRIE_4B:
      40                 :          5 :                 return rte_trie_lookup_bulk_4b;
      41                 :          1 :         case RTE_FIB6_TRIE_8B:
      42                 :          1 :                 return rte_trie_lookup_bulk_8b;
      43                 :          0 :         default:
      44                 :          0 :                 return NULL;
      45                 :            :         }
      46                 :            : }
      47                 :            : 
      48                 :            : static inline rte_fib6_lookup_fn_t
      49                 :         15 : get_vector_fn(enum rte_fib_trie_nh_sz nh_sz)
      50                 :            : {
      51                 :            : #ifdef CC_AVX512_SUPPORT
      52   [ +  -  +  - ]:         30 :         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512F) <= 0 ||
      53         [ +  - ]:         30 :                         rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512DQ) <= 0 ||
      54         [ +  - ]:         30 :                         rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX512BW) <= 0 ||
      55                 :         15 :                         rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_512)
      56                 :         15 :                 return NULL;
      57   [ #  #  #  # ]:          0 :         switch (nh_sz) {
      58                 :            :         case RTE_FIB6_TRIE_2B:
      59                 :            :                 return rte_trie_vec_lookup_bulk_2b;
      60                 :          0 :         case RTE_FIB6_TRIE_4B:
      61                 :          0 :                 return rte_trie_vec_lookup_bulk_4b;
      62                 :          0 :         case RTE_FIB6_TRIE_8B:
      63                 :          0 :                 return rte_trie_vec_lookup_bulk_8b;
      64                 :          0 :         default:
      65                 :          0 :                 return NULL;
      66                 :            :         }
      67                 :            : #else
      68                 :            :         RTE_SET_USED(nh_sz);
      69                 :            : #endif
      70                 :            :         return NULL;
      71                 :            : }
      72                 :            : 
      73                 :            : rte_fib6_lookup_fn_t
      74                 :         15 : trie_get_lookup_fn(void *p, enum rte_fib6_lookup_type type)
      75                 :            : {
      76                 :            :         enum rte_fib_trie_nh_sz nh_sz;
      77                 :            :         rte_fib6_lookup_fn_t ret_fn;
      78                 :            :         struct rte_trie_tbl *dp = p;
      79                 :            : 
      80         [ +  - ]:         15 :         if (dp == NULL)
      81                 :            :                 return NULL;
      82                 :            : 
      83                 :         15 :         nh_sz = dp->nh_sz;
      84                 :            : 
      85   [ -  -  +  - ]:         15 :         switch (type) {
      86                 :            :         case RTE_FIB6_LOOKUP_TRIE_SCALAR:
      87                 :            :                 return get_scalar_fn(nh_sz);
      88                 :          0 :         case RTE_FIB6_LOOKUP_TRIE_VECTOR_AVX512:
      89                 :          0 :                 return get_vector_fn(nh_sz);
      90                 :         15 :         case RTE_FIB6_LOOKUP_DEFAULT:
      91                 :         15 :                 ret_fn = get_vector_fn(nh_sz);
      92         [ +  - ]:         15 :                 return (ret_fn != NULL) ? ret_fn : get_scalar_fn(nh_sz);
      93                 :            :         default:
      94                 :            :                 return NULL;
      95                 :            :         }
      96                 :            :         return NULL;
      97                 :            : }
      98                 :            : 
      99                 :            : static void
     100                 :      32199 : write_to_dp(void *ptr, uint64_t val, enum rte_fib_trie_nh_sz size, int n)
     101                 :            : {
     102                 :            :         int i;
     103                 :            :         uint16_t *ptr16 = (uint16_t *)ptr;
     104                 :            :         uint32_t *ptr32 = (uint32_t *)ptr;
     105                 :            :         uint64_t *ptr64 = (uint64_t *)ptr;
     106                 :            : 
     107   [ +  +  +  - ]:      32199 :         switch (size) {
     108                 :            :         case RTE_FIB6_TRIE_2B:
     109         [ +  + ]:  202890775 :                 for (i = 0; i < n; i++)
     110                 :  202866240 :                         ptr16[i] = (uint16_t)val;
     111                 :            :                 break;
     112                 :            :         case RTE_FIB6_TRIE_4B:
     113         [ +  + ]:  167950956 :                 for (i = 0; i < n; i++)
     114                 :  167944793 :                         ptr32[i] = (uint32_t)val;
     115                 :            :                 break;
     116                 :            :         case RTE_FIB6_TRIE_8B:
     117         [ +  + ]:   67130389 :                 for (i = 0; i < n; i++)
     118                 :   67128888 :                         ptr64[i] = (uint64_t)val;
     119                 :            :                 break;
     120                 :            :         }
     121                 :      32199 : }
     122                 :            : 
     123                 :            : static void
     124                 :            : tbl8_pool_init(struct rte_trie_tbl *dp)
     125                 :            : {
     126                 :            :         uint32_t i;
     127                 :            : 
     128                 :            :         /* put entire range of indexes to the tbl8 pool */
     129         [ +  + ]:     132632 :         for (i = 0; i < dp->number_tbl8s; i++)
     130                 :     132617 :                 dp->tbl8_pool[i] = i;
     131                 :            : 
     132                 :         15 :         dp->tbl8_pool_pos = 0;
     133                 :            : }
     134                 :            : 
     135                 :            : /*
     136                 :            :  * Get an index of a free tbl8 from the pool
     137                 :            :  */
     138                 :            : static inline int32_t
     139                 :            : tbl8_get(struct rte_trie_tbl *dp)
     140                 :            : {
     141         [ #  # ]:          0 :         if (dp->tbl8_pool_pos == dp->number_tbl8s)
     142                 :            :                 /* no more free tbl8 */
     143                 :            :                 return -ENOSPC;
     144                 :            : 
     145                 :            :         /* next index */
     146                 :       2938 :         return dp->tbl8_pool[dp->tbl8_pool_pos++];
     147                 :            : }
     148                 :            : 
     149                 :            : /*
     150                 :            :  * Put an index of a free tbl8 back to the pool
     151                 :            :  */
     152                 :            : static inline void
     153                 :            : tbl8_put(struct rte_trie_tbl *dp, uint32_t tbl8_ind)
     154                 :            : {
     155                 :       2919 :         dp->tbl8_pool[--dp->tbl8_pool_pos] = tbl8_ind;
     156                 :            : }
     157                 :            : 
     158                 :            : static int
     159         [ +  - ]:       2938 : tbl8_alloc(struct rte_trie_tbl *dp, uint64_t nh)
     160                 :            : {
     161                 :            :         int64_t         tbl8_idx;
     162                 :            :         uint8_t         *tbl8_ptr;
     163                 :            : 
     164                 :       2938 :         tbl8_idx = tbl8_get(dp);
     165                 :            : 
     166                 :            :         /* If there are no tbl8 groups try to reclaim one. */
     167   [ -  +  -  -  :       2938 :         if (unlikely(tbl8_idx == -ENOSPC && dp->dq &&
                   -  - ]
     168                 :            :                         !rte_rcu_qsbr_dq_reclaim(dp->dq, 1, NULL, NULL, NULL)))
     169                 :          0 :                 tbl8_idx = tbl8_get(dp);
     170                 :            : 
     171         [ -  + ]:       2938 :         if (tbl8_idx < 0)
     172                 :          0 :                 return tbl8_idx;
     173                 :       2938 :         tbl8_ptr = get_tbl_p_by_idx(dp->tbl8,
     174                 :       2938 :                 tbl8_idx * TRIE_TBL8_GRP_NUM_ENT, dp->nh_sz);
     175                 :            :         /*Init tbl8 entries with nexthop from tbl24*/
     176                 :       2938 :         write_to_dp((void *)tbl8_ptr, nh, dp->nh_sz,
     177                 :            :                 TRIE_TBL8_GRP_NUM_ENT);
     178                 :       2938 :         dp->cur_tbl8s++;
     179                 :       2938 :         return tbl8_idx;
     180                 :            : }
     181                 :            : 
     182                 :            : static void
     183                 :       2919 : tbl8_cleanup_and_free(struct rte_trie_tbl *dp, uint64_t tbl8_idx)
     184                 :            : {
     185                 :       2919 :         uint8_t *ptr = (uint8_t *)dp->tbl8 + (tbl8_idx * TRIE_TBL8_GRP_NUM_ENT << dp->nh_sz);
     186                 :            : 
     187                 :       2919 :         memset(ptr, 0, TRIE_TBL8_GRP_NUM_ENT << dp->nh_sz);
     188                 :       2919 :         tbl8_put(dp, tbl8_idx);
     189                 :       2919 : }
     190                 :            : 
     191                 :            : static void
     192                 :          3 : __rcu_qsbr_free_resource(void *p, void *data, unsigned int n __rte_unused)
     193                 :            : {
     194                 :            :         struct rte_trie_tbl *dp = p;
     195                 :          3 :         uint64_t tbl8_idx = *(uint64_t *)data;
     196                 :          3 :         tbl8_cleanup_and_free(dp, tbl8_idx);
     197                 :          3 : }
     198                 :            : 
     199                 :            : static void
     200                 :      28171 : tbl8_recycle(struct rte_trie_tbl *dp, void *par, uint64_t tbl8_idx)
     201                 :            : {
     202                 :            :         uint32_t i;
     203                 :            :         uint64_t nh;
     204                 :            :         uint16_t *ptr16;
     205                 :            :         uint32_t *ptr32;
     206                 :            :         uint64_t *ptr64;
     207                 :            : 
     208   [ +  +  +  - ]:      28171 :         switch (dp->nh_sz) {
     209                 :      21243 :         case RTE_FIB6_TRIE_2B:
     210                 :      21243 :                 ptr16 = &((uint16_t *)dp->tbl8)[tbl8_idx *
     211                 :            :                                 TRIE_TBL8_GRP_NUM_ENT];
     212                 :      21243 :                 nh = *ptr16;
     213         [ +  + ]:      21243 :                 if (nh & TRIE_EXT_ENT)
     214                 :            :                         return;
     215         [ +  + ]:     701705 :                 for (i = 1; i < TRIE_TBL8_GRP_NUM_ENT; i++) {
     216         [ +  + ]:     699350 :                         if (nh != ptr16[i])
     217                 :            :                                 return;
     218                 :            :                 }
     219                 :       2355 :                 write_to_dp(par, nh, dp->nh_sz, 1);
     220                 :       2355 :                 break;
     221                 :       3990 :         case RTE_FIB6_TRIE_4B:
     222                 :       3990 :                 ptr32 = &((uint32_t *)dp->tbl8)[tbl8_idx *
     223                 :            :                                 TRIE_TBL8_GRP_NUM_ENT];
     224                 :       3990 :                 nh = *ptr32;
     225         [ +  + ]:       3990 :                 if (nh & TRIE_EXT_ENT)
     226                 :            :                         return;
     227         [ +  + ]:     249430 :                 for (i = 1; i < TRIE_TBL8_GRP_NUM_ENT; i++) {
     228         [ +  + ]:     248892 :                         if (nh != ptr32[i])
     229                 :            :                                 return;
     230                 :            :                 }
     231                 :        538 :                 write_to_dp(par, nh, dp->nh_sz, 1);
     232                 :        538 :                 break;
     233                 :       2938 :         case RTE_FIB6_TRIE_8B:
     234                 :       2938 :                 ptr64 = &((uint64_t *)dp->tbl8)[tbl8_idx *
     235                 :            :                                 TRIE_TBL8_GRP_NUM_ENT];
     236                 :       2938 :                 nh = *ptr64;
     237         [ +  + ]:       2938 :                 if (nh & TRIE_EXT_ENT)
     238                 :            :                         return;
     239         [ +  + ]:      13302 :                 for (i = 1; i < TRIE_TBL8_GRP_NUM_ENT; i++) {
     240         [ +  + ]:      13276 :                         if (nh != ptr64[i])
     241                 :            :                                 return;
     242                 :            :                 }
     243                 :         26 :                 write_to_dp(par, nh, dp->nh_sz, 1);
     244                 :         26 :                 break;
     245                 :            :         }
     246                 :            : 
     247                 :       2919 :         dp->cur_tbl8s--;
     248         [ +  + ]:       2919 :         if (dp->v == NULL) {
     249                 :       2404 :                 tbl8_cleanup_and_free(dp, tbl8_idx);
     250         [ +  + ]:        515 :         } else if (dp->rcu_mode == RTE_FIB6_QSBR_MODE_SYNC) {
     251                 :        512 :                 rte_rcu_qsbr_synchronize(dp->v, RTE_QSBR_THRID_INVALID);
     252                 :        512 :                 tbl8_cleanup_and_free(dp, tbl8_idx);
     253                 :            :         } else { /* RTE_FIB6_QSBR_MODE_DQ */
     254         [ -  + ]:          3 :                 if (rte_rcu_qsbr_dq_enqueue(dp->dq, &tbl8_idx))
     255                 :          0 :                         FIB_LOG(ERR, "Failed to push QSBR FIFO");
     256                 :            :         }
     257                 :            : }
     258                 :            : 
     259                 :            : #define BYTE_SIZE       8
     260                 :            : static inline uint64_t
     261                 :            : get_idx(const struct rte_ipv6_addr *ip, uint64_t prev_idx, int bytes, int first_byte)
     262                 :            : {
     263                 :            :         int i;
     264                 :            :         uint32_t idx = 0;
     265                 :            :         uint8_t bitshift;
     266                 :            : 
     267   [ +  +  +  +  :      81108 :         for (i = first_byte; i < (first_byte + bytes); i++) {
                   +  + ]
     268                 :      46527 :                 bitshift = (int8_t)(((first_byte + bytes - 1) - i)*BYTE_SIZE);
     269                 :      46527 :                 idx |= ip->a[i] <<  bitshift;
     270                 :            :         }
     271                 :       5685 :         return (prev_idx * TRIE_TBL8_GRP_NUM_ENT) + idx;
     272                 :            : }
     273                 :            : 
     274                 :            : static inline uint64_t
     275                 :            : get_val_by_p(void *p, uint8_t nh_sz)
     276                 :            : {
     277                 :            :         uint64_t val = 0;
     278                 :            : 
     279                 :      32968 :         switch (nh_sz) {
     280                 :      23985 :         case RTE_FIB6_TRIE_2B:
     281                 :      23985 :                 val = *(uint16_t *)p;
     282                 :      23985 :                 break;
     283                 :       5533 :         case RTE_FIB6_TRIE_4B:
     284                 :       5533 :                 val = *(uint32_t *)p;
     285                 :       5533 :                 break;
     286                 :       3450 :         case RTE_FIB6_TRIE_8B:
     287                 :       3450 :                 val = *(uint64_t *)p;
     288                 :       3450 :                 break;
     289                 :            :         }
     290                 :            :         return val;
     291                 :            : }
     292                 :            : 
     293                 :            : /*
     294                 :            :  * recursively recycle tbl8's
     295                 :            :  */
     296                 :            : static void
     297                 :      28896 : recycle_root_path(struct rte_trie_tbl *dp, const uint8_t *ip_part,
     298                 :            :         uint8_t common_tbl8, void *prev)
     299                 :            : {
     300                 :            :         void *p;
     301                 :            :         uint64_t val;
     302                 :            : 
     303   [ +  +  +  - ]:      28896 :         val = get_val_by_p(prev, dp->nh_sz);
     304         [ +  + ]:      28896 :         if (unlikely((val & TRIE_EXT_ENT) != TRIE_EXT_ENT))
     305                 :            :                 return;
     306                 :            : 
     307         [ +  + ]:      24099 :         if (common_tbl8 != 0) {
     308                 :      23211 :                 p = get_tbl_p_by_idx(dp->tbl8, (val >> 1) *
     309                 :      23211 :                         TRIE_TBL8_GRP_NUM_ENT + *ip_part, dp->nh_sz);
     310                 :      23211 :                 recycle_root_path(dp, ip_part + 1, common_tbl8 - 1, p);
     311                 :            :         }
     312                 :      24099 :         tbl8_recycle(dp, prev, val >> 1);
     313                 :            : }
     314                 :            : 
     315                 :            : static inline int
     316                 :       5685 : build_common_root(struct rte_trie_tbl *dp, const struct rte_ipv6_addr *ip,
     317                 :            :         int common_bytes, void **tbl)
     318                 :            : {
     319                 :            :         void *tbl_ptr = NULL;
     320                 :            :         uint64_t *cur_tbl;
     321                 :            :         uint64_t val;
     322                 :            :         uint64_t idx, prev_idx = 0;
     323                 :            :         int i, j, tbl8_idx;
     324                 :            : 
     325                 :       5685 :         cur_tbl = dp->tbl24;
     326         [ +  + ]:      28896 :         for (i = 3, j = 0; i <= common_bytes; i++) {
     327                 :            :                 idx = get_idx(ip, prev_idx, i - j, j);
     328         [ +  + ]:      23211 :                 val = get_tbl_val_by_idx(cur_tbl, idx, dp->nh_sz);
     329                 :            :                 tbl_ptr = get_tbl_p_by_idx(cur_tbl, idx, dp->nh_sz);
     330         [ +  + ]:      23211 :                 if ((val & TRIE_EXT_ENT) != TRIE_EXT_ENT) {
     331                 :       2933 :                         tbl8_idx = tbl8_alloc(dp, val);
     332         [ -  + ]:       2933 :                         if (unlikely(tbl8_idx < 0))
     333                 :          0 :                                 return tbl8_idx;
     334                 :       2933 :                         write_to_dp(tbl_ptr, ((uint64_t)tbl8_idx << 1) |
     335                 :            :                                 TRIE_EXT_ENT, dp->nh_sz, 1);
     336                 :            :                         prev_idx = (uint64_t)tbl8_idx;
     337                 :            :                 } else
     338                 :      20278 :                         prev_idx = val >> 1;
     339                 :            : 
     340                 :            :                 j = i;
     341                 :      23211 :                 cur_tbl = dp->tbl8;
     342                 :            :         }
     343                 :       5685 :         *tbl = get_tbl_p_by_idx(cur_tbl, prev_idx * TRIE_TBL8_GRP_NUM_ENT,
     344                 :       5685 :                 dp->nh_sz);
     345                 :       5685 :         return 0;
     346                 :            : }
     347                 :            : 
     348                 :            : static int
     349                 :      15442 : write_edge(struct rte_trie_tbl *dp, const uint8_t *ip_part, uint64_t next_hop,
     350                 :            :         int len, enum edge edge, void *ent)
     351                 :            : {
     352                 :      15442 :         uint64_t val = next_hop << 1;
     353                 :            :         int tbl8_idx;
     354                 :            :         int ret = 0;
     355                 :            :         void *p;
     356                 :            : 
     357         [ +  + ]:      15442 :         if (len != 0) {
     358   [ +  -  -  - ]:       4072 :                 val = get_val_by_p(ent, dp->nh_sz);
     359         [ +  + ]:       4072 :                 if ((val & TRIE_EXT_ENT) == TRIE_EXT_ENT)
     360                 :       4067 :                         tbl8_idx = val >> 1;
     361                 :            :                 else {
     362                 :          5 :                         tbl8_idx = tbl8_alloc(dp, val);
     363         [ +  - ]:          5 :                         if (tbl8_idx < 0)
     364                 :            :                                 return tbl8_idx;
     365                 :          5 :                         val = ((uint64_t)tbl8_idx << 1) | TRIE_EXT_ENT;
     366                 :            :                 }
     367                 :       4072 :                 p = get_tbl_p_by_idx(dp->tbl8, (tbl8_idx *
     368                 :       4072 :                         TRIE_TBL8_GRP_NUM_ENT) + *ip_part, dp->nh_sz);
     369                 :       4072 :                 ret = write_edge(dp, ip_part + 1, next_hop, len - 1, edge, p);
     370         [ +  - ]:       4072 :                 if (ret < 0)
     371                 :            :                         return ret;
     372         [ +  + ]:       4072 :                 if (edge == LEDGE) {
     373                 :       3032 :                         write_to_dp(RTE_PTR_ADD(p, (uintptr_t)(1) << dp->nh_sz),
     374                 :       3032 :                                 next_hop << 1, dp->nh_sz, UINT8_MAX - *ip_part);
     375                 :            :                 } else {
     376                 :       1040 :                         write_to_dp(get_tbl_p_by_idx(dp->tbl8, tbl8_idx *
     377                 :            :                                 TRIE_TBL8_GRP_NUM_ENT, dp->nh_sz),
     378                 :       1040 :                                 next_hop << 1, dp->nh_sz, *ip_part);
     379                 :            :                 }
     380                 :       4072 :                 tbl8_recycle(dp, &val, tbl8_idx);
     381                 :            :         }
     382                 :            : 
     383                 :      15442 :         write_to_dp(ent, val, dp->nh_sz, 1);
     384                 :      15442 :         return ret;
     385                 :            : }
     386                 :            : 
     387                 :            : #define IPV6_MAX_IDX    (RTE_IPV6_ADDR_SIZE - 1)
     388                 :            : #define TBL24_BYTES     3
     389                 :            : #define TBL8_LEN        (RTE_IPV6_ADDR_SIZE - TBL24_BYTES)
     390                 :            : 
     391                 :            : static int
     392                 :       5685 : install_to_dp(struct rte_trie_tbl *dp, const struct rte_ipv6_addr *ledge,
     393                 :            :         const struct rte_ipv6_addr *r, uint64_t next_hop)
     394                 :            : {
     395                 :            :         void *common_root_tbl;
     396                 :            :         void *ent;
     397                 :            :         int ret;
     398                 :            :         int i;
     399                 :            :         int common_bytes;
     400                 :            :         int llen, rlen;
     401                 :            :         struct rte_ipv6_addr redge;
     402                 :            : 
     403                 :            :         /* decrement redge by 1*/
     404                 :       5685 :         redge = *r;
     405         [ +  + ]:      56250 :         for (i = 15; i >= 0; i--) {
     406                 :      56235 :                 redge.a[i]--;
     407         [ +  + ]:      56235 :                 if (redge.a[i] != 0xff)
     408                 :            :                         break;
     409                 :            :         }
     410                 :            : 
     411         [ +  + ]:      41100 :         for (common_bytes = 0; common_bytes < 15; common_bytes++) {
     412         [ +  + ]:      40612 :                 if (ledge->a[common_bytes] != redge.a[common_bytes])
     413                 :            :                         break;
     414                 :            :         }
     415                 :            : 
     416                 :            :         /*
     417                 :            :          * A range covering a whole tbl8 is a single entry one level up.
     418                 :            :          * Write that entry rather than build a tbl8 only to recycle it.
     419                 :            :          */
     420         [ +  + ]:       5685 :         if (common_bytes >= TBL24_BYTES) {
     421         [ +  + ]:      14717 :                 for (i = common_bytes; i <= IPV6_MAX_IDX; i++) {
     422   [ +  +  +  + ]:      13625 :                         if (ledge->a[i] != 0 || redge.a[i] != UINT8_MAX)
     423                 :            :                                 break;
     424                 :            :                 }
     425         [ +  + ]:       5411 :                 if (i > IPV6_MAX_IDX)
     426                 :       1092 :                         common_bytes--;
     427                 :            :         }
     428                 :            : 
     429                 :       5685 :         ret = build_common_root(dp, ledge, common_bytes, &common_root_tbl);
     430         [ +  - ]:       5685 :         if (unlikely(ret != 0))
     431                 :            :                 return ret;
     432                 :            :         /*first uncommon tbl8 byte idx*/
     433                 :       5685 :         uint8_t first_tbl8_byte = RTE_MAX(common_bytes, TBL24_BYTES);
     434                 :            : 
     435         [ +  + ]:      53347 :         for (i = IPV6_MAX_IDX; i >= first_tbl8_byte + !(common_bytes < 3); i--) {
     436         [ +  + ]:      48554 :                 if (ledge->a[i] != 0)
     437                 :            :                         break;
     438                 :            :         }
     439                 :            : 
     440                 :       5685 :         llen = i - first_tbl8_byte + (common_bytes < 3);
     441                 :            : 
     442         [ +  + ]:      55339 :         for (i = IPV6_MAX_IDX; i >= first_tbl8_byte + !(common_bytes < 3); i--) {
     443         [ +  + ]:      49905 :                 if (redge.a[i] != UINT8_MAX)
     444                 :            :                         break;
     445                 :            :         }
     446                 :       5685 :         rlen = i - first_tbl8_byte + (common_bytes < 3);
     447                 :            : 
     448                 :            :         /*first noncommon byte*/
     449         [ +  + ]:       5685 :         uint8_t first_byte_idx = (common_bytes < 3) ? 0 : common_bytes;
     450         [ +  + ]:       5685 :         uint8_t first_idx_len = (common_bytes < 3) ? 3 : 1;
     451                 :            : 
     452                 :       5685 :         uint64_t left_idx = get_idx(ledge, 0, first_idx_len, first_byte_idx);
     453                 :            :         uint64_t right_idx = get_idx(&redge, 0, first_idx_len, first_byte_idx);
     454                 :            : 
     455                 :       5685 :         ent = get_tbl_p_by_idx(common_root_tbl, left_idx, dp->nh_sz);
     456                 :       5685 :         ret = write_edge(dp, &ledge->a[first_tbl8_byte + !(common_bytes < 3)],
     457                 :            :                 next_hop, llen, LEDGE, ent);
     458         [ +  - ]:       5685 :         if (ret < 0)
     459                 :            :                 return ret;
     460                 :            : 
     461         [ +  + ]:       5685 :         if (right_idx > left_idx + 1) {
     462                 :       3880 :                 ent = get_tbl_p_by_idx(common_root_tbl, left_idx + 1,
     463                 :       3880 :                         dp->nh_sz);
     464                 :       3880 :                 write_to_dp(ent, next_hop << 1, dp->nh_sz,
     465                 :       3880 :                         right_idx - (left_idx + 1));
     466                 :            :         }
     467                 :       5685 :         ent = get_tbl_p_by_idx(common_root_tbl, right_idx, dp->nh_sz);
     468                 :       5685 :         ret = write_edge(dp, &redge.a[first_tbl8_byte + !((common_bytes < 3))],
     469                 :            :                 next_hop, rlen, REDGE, ent);
     470         [ +  - ]:       5685 :         if (ret < 0)
     471                 :            :                 return ret;
     472                 :            : 
     473                 :       5685 :         uint8_t common_tbl8 = (common_bytes < TBL24_BYTES) ?
     474                 :       5685 :                         0 : common_bytes - (TBL24_BYTES - 1);
     475                 :       5685 :         ent = get_tbl24_p(dp, ledge, dp->nh_sz);
     476                 :       5685 :         recycle_root_path(dp, ledge->a + TBL24_BYTES, common_tbl8, ent);
     477                 :       5685 :         return 0;
     478                 :            : }
     479                 :            : 
     480                 :            : static void
     481                 :       6451 : get_nxt_net(struct rte_ipv6_addr *ip, uint8_t depth)
     482                 :            : {
     483                 :            :         int i;
     484                 :            :         uint8_t part_depth;
     485                 :            :         uint8_t prev_byte;
     486                 :            : 
     487         [ +  + ]:      47682 :         for (i = 0, part_depth = depth; part_depth > 8; part_depth -= 8, i++)
     488                 :            :                 ;
     489                 :            : 
     490                 :       6451 :         prev_byte = ip->a[i];
     491                 :       6451 :         ip->a[i] += 1 << (8 - part_depth);
     492         [ +  + ]:       6451 :         if (ip->a[i] < prev_byte) {
     493         [ +  + ]:         46 :                 while (i > 0) {
     494                 :         30 :                         ip->a[--i] += 1;
     495         [ +  - ]:         30 :                         if (ip->a[i] != 0)
     496                 :            :                                 break;
     497                 :            :                 }
     498                 :            :         }
     499                 :       6451 : }
     500                 :            : 
     501                 :            : static int
     502                 :       4606 : modify_dp(struct rte_trie_tbl *dp, struct rte_rib6 *rib,
     503                 :            :         const struct rte_ipv6_addr *ip,
     504                 :            :         uint8_t depth, uint64_t next_hop)
     505                 :            : {
     506                 :            :         struct rte_rib6_node *tmp = NULL;
     507                 :            :         struct rte_ipv6_addr ledge, redge;
     508                 :            :         int ret;
     509                 :            :         uint8_t tmp_depth;
     510                 :            : 
     511         [ +  - ]:       4606 :         if (next_hop > get_max_nh(dp->nh_sz))
     512                 :            :                 return -EINVAL;
     513                 :            : 
     514                 :            :         /*
     515                 :            :          * Check if we can safely allocate new tbl8s further in process of modifying the dataplane.
     516                 :            :          */
     517                 :       4606 :         uint32_t tbl8s_in_defer_queue = dp->tbl8_pool_pos - dp->cur_tbl8s;
     518         [ +  + ]:       4606 :         if (dp->rsvd_tbl8s + tbl8s_in_defer_queue > dp->number_tbl8s) {
     519                 :          4 :                 rte_rcu_qsbr_dq_reclaim(dp->dq, dp->tbl8_pool_pos - dp->cur_tbl8s,
     520                 :            :                         NULL, NULL, NULL);
     521                 :          4 :                 tbl8s_in_defer_queue = dp->tbl8_pool_pos - dp->cur_tbl8s;
     522         [ +  + ]:          4 :                 if (dp->rsvd_tbl8s + tbl8s_in_defer_queue > dp->number_tbl8s)
     523                 :            :                         return -ENOSPC;
     524                 :            :         }
     525                 :            : 
     526                 :       4603 :         ledge = *ip;
     527                 :            :         do {
     528                 :       6451 :                 tmp = rte_rib6_get_nxt(rib, ip, depth, tmp,
     529                 :            :                         RTE_RIB6_GET_NXT_COVER);
     530         [ +  + ]:       6451 :                 if (tmp != NULL) {
     531                 :       1850 :                         rte_rib6_get_depth(tmp, &tmp_depth);
     532         [ -  + ]:       1850 :                         if (tmp_depth == depth)
     533                 :          0 :                                 continue;
     534                 :       1850 :                         rte_rib6_get_ip(tmp, &redge);
     535         [ +  + ]:       1850 :                         if (rte_ipv6_addr_eq(&ledge, &redge)) {
     536                 :        766 :                                 get_nxt_net(&ledge, tmp_depth);
     537                 :            :                                 /*
     538                 :            :                                  * we got to the end of address space
     539                 :            :                                  * and wrapped around
     540                 :            :                                  */
     541         [ +  + ]:        766 :                                 if (rte_ipv6_addr_is_unspec(&ledge))
     542                 :            :                                         break;
     543                 :        764 :                                 continue;
     544                 :            :                         }
     545                 :       1084 :                         ret = install_to_dp(dp, &ledge, &redge, next_hop);
     546         [ -  + ]:       1084 :                         if (ret != 0)
     547                 :          0 :                                 return ret;
     548                 :       1084 :                         get_nxt_net(&redge, tmp_depth);
     549         [ +  - ]:       1084 :                         ledge = redge;
     550                 :            :                         /*
     551                 :            :                          * we got to the end of address space
     552                 :            :                          * and wrapped around
     553                 :            :                          */
     554         [ +  - ]:       1084 :                         if (rte_ipv6_addr_is_unspec(&ledge))
     555                 :            :                                 break;
     556                 :            :                 } else {
     557                 :       4601 :                         redge = *ip;
     558                 :       4601 :                         get_nxt_net(&redge, depth);
     559   [ +  +  +  - ]:       4601 :                         if (rte_ipv6_addr_eq(&ledge, &redge) &&
     560                 :            :                                         !rte_ipv6_addr_is_unspec(&ledge))
     561                 :            :                                 break;
     562                 :            : 
     563                 :       4601 :                         ret = install_to_dp(dp, &ledge, &redge, next_hop);
     564         [ -  + ]:       4601 :                         if (ret != 0)
     565                 :          0 :                                 return ret;
     566                 :            :                 }
     567         [ +  + ]:       6449 :         } while (tmp);
     568                 :            : 
     569                 :            :         return 0;
     570                 :            : }
     571                 :            : 
     572                 :            : /*
     573                 :            :  * Count number of TBL8s that can be freed after deleting a prefix or allocated
     574                 :            :  * after adding a prefix.
     575                 :            :  */
     576                 :            : static uint8_t
     577                 :       4618 : count_empty_levels(const struct rte_rib6_node *node)
     578                 :            : {
     579                 :            :         struct rte_rib6_node *parent;
     580                 :       4618 :         uint8_t depth, parent_depth = 24;
     581                 :            : 
     582                 :            :         /* more specifics present */
     583         [ +  + ]:       4618 :         if (rte_rib6_node_has_children(node))
     584                 :            :                 return 0;
     585                 :            : 
     586                 :       3155 :         rte_rib6_get_depth(node, &depth);
     587                 :       3155 :         depth = RTE_MAX(depth, 24);
     588                 :            : 
     589                 :            :         /* we know parent depth lt a target node depth
     590                 :            :          * also, there exists tbl8 path up to RTE_ALIGN_CEIL(parent depth, 8)
     591                 :            :          */
     592                 :       3155 :         parent = rte_rib6_get_parent(node);
     593         [ +  + ]:       3155 :         if (parent != NULL) {
     594                 :       2079 :                 rte_rib6_get_depth(parent, &parent_depth);
     595                 :       2079 :                 parent_depth = RTE_MAX(parent_depth, 24);
     596                 :            :         }
     597                 :            : 
     598                 :       3155 :         return (RTE_ALIGN_CEIL(depth, 8) - RTE_ALIGN_CEIL(parent_depth, 8)) >> 3;
     599                 :            : }
     600                 :            : 
     601                 :            : int
     602                 :       4622 : trie_modify(struct rte_fib6 *fib, const struct rte_ipv6_addr *ip,
     603                 :            :         uint8_t depth, uint64_t next_hop, int op)
     604                 :            : {
     605                 :            :         struct rte_trie_tbl *dp;
     606                 :            :         struct rte_rib6 *rib;
     607                 :            :         struct rte_rib6_node *node;
     608                 :            :         struct rte_rib6_node *parent;
     609                 :            :         struct rte_ipv6_addr ip_masked;
     610                 :            :         int ret = 0;
     611                 :            :         uint64_t par_nh, node_nh;
     612                 :            :         uint8_t new_levels;
     613                 :            : 
     614   [ +  -  +  - ]:       4622 :         if ((fib == NULL) || (ip == NULL) || (depth > RTE_IPV6_MAX_DEPTH))
     615                 :            :                 return -EINVAL;
     616                 :            : 
     617                 :       4622 :         dp = rte_fib6_get_dp(fib);
     618                 :            :         RTE_ASSERT(dp);
     619                 :       4622 :         rib = rte_fib6_get_rib(fib);
     620                 :            :         RTE_ASSERT(rib);
     621                 :            : 
     622                 :       4622 :         ip_masked = *ip;
     623         [ +  + ]:       4622 :         rte_ipv6_addr_mask(&ip_masked, depth);
     624                 :            : 
     625                 :       4622 :         node = rte_rib6_lookup_exact(rib, &ip_masked, depth);
     626      [ +  +  - ]:       4622 :         switch (op) {
     627                 :       2320 :         case RTE_FIB6_ADD:
     628         [ +  + ]:       2320 :                 if (node != NULL) {
     629                 :          3 :                         rte_rib6_get_nh(node, &node_nh);
     630         [ +  - ]:          3 :                         if (node_nh == next_hop)
     631                 :            :                                 return 0;
     632                 :          3 :                         ret = modify_dp(dp, rib, &ip_masked, depth, next_hop);
     633         [ +  + ]:          3 :                         if (ret == 0)
     634                 :          2 :                                 rte_rib6_set_nh(node, next_hop);
     635                 :            : 
     636                 :          3 :                         return ret;
     637                 :            :                 }
     638                 :            : 
     639                 :       2317 :                 node = rte_rib6_insert(rib, &ip_masked, depth);
     640         [ -  + ]:       2317 :                 if (node == NULL)
     641                 :          0 :                         return -rte_errno;
     642                 :            : 
     643                 :       2317 :                 new_levels = count_empty_levels(node);
     644         [ +  + ]:       2317 :                 if (dp->rsvd_tbl8s + new_levels > dp->number_tbl8s) {
     645                 :          2 :                         rte_rib6_remove(rib, &ip_masked, depth);
     646                 :          2 :                         return -ENOSPC;
     647                 :            :                 }
     648                 :       2315 :                 dp->rsvd_tbl8s += new_levels;
     649                 :            : 
     650                 :       2315 :                 rte_rib6_set_nh(node, next_hop);
     651                 :       2315 :                 parent = rte_rib6_lookup_parent(node);
     652         [ +  + ]:       2315 :                 if (parent != NULL) {
     653                 :        930 :                         rte_rib6_get_nh(parent, &par_nh);
     654         [ +  + ]:        930 :                         if (par_nh == next_hop)
     655                 :          6 :                                 goto successfully_added;
     656                 :            :                 }
     657                 :       2309 :                 ret = modify_dp(dp, rib, &ip_masked, depth, next_hop);
     658         [ +  + ]:       2309 :                 if (ret != 0) {
     659                 :          1 :                         dp->rsvd_tbl8s -= new_levels;
     660                 :          1 :                         rte_rib6_remove(rib, &ip_masked, depth);
     661                 :          1 :                         return ret;
     662                 :            :                 }
     663                 :       2308 : successfully_added:
     664                 :            :                 return 0;
     665                 :       2302 :         case RTE_FIB6_DEL:
     666         [ +  - ]:       2302 :                 if (node == NULL)
     667                 :            :                         return -ENOENT;
     668                 :            : 
     669                 :       2302 :                 parent = rte_rib6_lookup_parent(node);
     670         [ +  + ]:       2302 :                 if (parent != NULL) {
     671                 :        926 :                         rte_rib6_get_nh(parent, &par_nh);
     672                 :        926 :                         rte_rib6_get_nh(node, &node_nh);
     673         [ +  + ]:        926 :                         if (par_nh != node_nh)
     674                 :        918 :                                 ret = modify_dp(dp, rib, &ip_masked, depth,
     675                 :            :                                         par_nh);
     676                 :            :                 } else
     677                 :       1376 :                         ret = modify_dp(dp, rib, &ip_masked, depth, dp->def_nh);
     678                 :            : 
     679         [ +  + ]:       2294 :                 if (ret != 0)
     680                 :            :                         return ret;
     681                 :            : 
     682                 :       2301 :                 dp->rsvd_tbl8s -= count_empty_levels(node);
     683                 :       2301 :                 rte_rib6_remove(rib, &ip_masked, depth);
     684                 :            : 
     685                 :       2301 :                 return 0;
     686                 :            :         default:
     687                 :            :                 break;
     688                 :            :         }
     689                 :            :         return -EINVAL;
     690                 :            : }
     691                 :            : 
     692                 :            : void *
     693                 :         17 : trie_create(const char *name, int socket_id,
     694                 :            :         struct rte_fib6_conf *conf)
     695                 :            : {
     696                 :            :         char mem_name[TRIE_NAMESIZE];
     697                 :            :         struct rte_trie_tbl *dp = NULL;
     698                 :            :         uint64_t        def_nh;
     699                 :            :         uint32_t        num_tbl8;
     700                 :            :         enum rte_fib_trie_nh_sz nh_sz;
     701                 :            : 
     702         [ +  - ]:         17 :         if ((name == NULL) || (conf == NULL) ||
     703   [ +  -  +  + ]:         17 :                         (conf->trie.nh_sz < RTE_FIB6_TRIE_2B) ||
     704         [ +  - ]:         16 :                         (conf->trie.nh_sz > RTE_FIB6_TRIE_8B) ||
     705                 :         16 :                         (conf->trie.num_tbl8 >
     706   [ +  -  +  + ]:         16 :                         get_max_nh(conf->trie.nh_sz)) ||
     707                 :         15 :                         (conf->trie.num_tbl8 == 0) ||
     708         [ -  + ]:         15 :                         (conf->default_nh >
     709                 :            :                         get_max_nh(conf->trie.nh_sz))) {
     710                 :            : 
     711                 :          2 :                 rte_errno = EINVAL;
     712                 :          2 :                 return NULL;
     713                 :            :         }
     714                 :            : 
     715                 :            :         def_nh = conf->default_nh;
     716                 :            :         nh_sz = conf->trie.nh_sz;
     717                 :            :         num_tbl8 = conf->trie.num_tbl8;
     718                 :            : 
     719                 :            :         snprintf(mem_name, sizeof(mem_name), "DP_%s", name);
     720                 :         15 :         dp = rte_zmalloc_socket(name, sizeof(struct rte_trie_tbl) +
     721                 :         15 :                 TRIE_TBL24_NUM_ENT * (1 << nh_sz) + sizeof(uint32_t),
     722                 :            :                 RTE_CACHE_LINE_SIZE, socket_id);
     723         [ -  + ]:         15 :         if (dp == NULL) {
     724                 :          0 :                 rte_errno = ENOMEM;
     725                 :          0 :                 return dp;
     726                 :            :         }
     727                 :            : 
     728                 :         15 :         write_to_dp(&dp->tbl24, (def_nh << 1), nh_sz, 1 << 24);
     729                 :            : 
     730                 :            :         snprintf(mem_name, sizeof(mem_name), "TBL8_%p", dp);
     731                 :         30 :         dp->tbl8 = rte_zmalloc_socket(mem_name, TRIE_TBL8_GRP_NUM_ENT *
     732                 :         15 :                         (1ll << nh_sz) * (num_tbl8 + 1),
     733                 :            :                         RTE_CACHE_LINE_SIZE, socket_id);
     734         [ -  + ]:         15 :         if (dp->tbl8 == NULL) {
     735                 :          0 :                 rte_errno = ENOMEM;
     736                 :          0 :                 rte_free(dp);
     737                 :          0 :                 return NULL;
     738                 :            :         }
     739                 :         15 :         dp->def_nh = def_nh;
     740                 :         15 :         dp->nh_sz = nh_sz;
     741                 :         15 :         dp->number_tbl8s = num_tbl8;
     742                 :            : 
     743                 :            :         snprintf(mem_name, sizeof(mem_name), "TBL8_idxes_%p", dp);
     744                 :         30 :         dp->tbl8_pool = rte_zmalloc_socket(mem_name,
     745                 :         15 :                         sizeof(uint32_t) * dp->number_tbl8s,
     746                 :            :                         RTE_CACHE_LINE_SIZE, socket_id);
     747         [ -  + ]:         15 :         if (dp->tbl8_pool == NULL) {
     748                 :          0 :                 rte_errno = ENOMEM;
     749                 :          0 :                 rte_free(dp->tbl8);
     750                 :          0 :                 rte_free(dp);
     751                 :          0 :                 return NULL;
     752                 :            :         }
     753                 :            : 
     754                 :            :         tbl8_pool_init(dp);
     755                 :            : 
     756                 :         15 :         return dp;
     757                 :            : }
     758                 :            : 
     759                 :            : void
     760                 :         15 : trie_free(void *p)
     761                 :            : {
     762                 :            :         struct rte_trie_tbl *dp = (struct rte_trie_tbl *)p;
     763                 :            : 
     764                 :         15 :         rte_rcu_qsbr_dq_delete(dp->dq);
     765                 :         15 :         rte_free(dp->tbl8_pool);
     766                 :         15 :         rte_free(dp->tbl8);
     767                 :         15 :         rte_free(dp);
     768                 :         15 : }
     769                 :            : 
     770                 :            : int
     771                 :          6 : trie_rcu_qsbr_add(struct rte_trie_tbl *dp, struct rte_fib6_rcu_config *cfg,
     772                 :            :         const char *name)
     773                 :            : {
     774                 :          6 :         struct rte_rcu_qsbr_dq_parameters params = {0};
     775                 :            :         char rcu_dq_name[RTE_RCU_QSBR_DQ_NAMESIZE];
     776                 :            : 
     777         [ +  - ]:          6 :         if (dp == NULL || cfg == NULL)
     778                 :            :                 return -EINVAL;
     779                 :            : 
     780         [ +  + ]:          6 :         if (dp->v != NULL)
     781                 :            :                 return -EEXIST;
     782                 :            : 
     783      [ +  +  + ]:          5 :         switch (cfg->mode) {
     784                 :            :         case RTE_FIB6_QSBR_MODE_DQ:
     785                 :            :                 /* Init QSBR defer queue. */
     786                 :            :                 snprintf(rcu_dq_name, sizeof(rcu_dq_name),
     787                 :            :                         "FIB_RCU_%s", name);
     788                 :          3 :                 params.name = rcu_dq_name;
     789                 :          3 :                 params.size = cfg->dq_size;
     790         [ +  - ]:          3 :                 if (params.size == 0)
     791                 :          3 :                         params.size = RTE_FIB6_RCU_DQ_RECLAIM_SZ;
     792                 :          3 :                 params.trigger_reclaim_limit = cfg->reclaim_thd;
     793                 :          3 :                 params.max_reclaim_size = cfg->reclaim_max;
     794         [ +  - ]:          3 :                 if (params.max_reclaim_size == 0)
     795                 :          3 :                         params.max_reclaim_size = RTE_FIB6_RCU_DQ_RECLAIM_MAX;
     796                 :          3 :                 params.esize = sizeof(uint64_t);
     797                 :          3 :                 params.free_fn = __rcu_qsbr_free_resource;
     798                 :          3 :                 params.p = dp;
     799                 :          3 :                 params.v = cfg->v;
     800                 :          3 :                 dp->dq = rte_rcu_qsbr_dq_create(&params);
     801         [ -  + ]:          3 :                 if (dp->dq == NULL) {
     802                 :          0 :                         FIB_LOG(ERR, "FIB6 defer queue creation failed");
     803                 :          0 :                         return -ENOMEM;
     804                 :            :                 }
     805                 :            :                 break;
     806                 :            :         case RTE_FIB6_QSBR_MODE_SYNC:
     807                 :            :                 /* No other things to do. */
     808                 :            :                 break;
     809                 :            :         default:
     810                 :            :                 return -EINVAL;
     811                 :            :         }
     812                 :          4 :         dp->rcu_mode = cfg->mode;
     813                 :          4 :         dp->v = cfg->v;
     814                 :            : 
     815                 :          4 :         return 0;
     816                 :            : }

Generated by: LCOV version 1.14