LCOV - code coverage report
Current view: top level - lib/ip_frag - ip_frag_internal.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 0 97 0.0 %
Date: 2026-06-01 18:36:17 Functions: 0 6 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 66 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  * Copyright(c) 2010-2014 Intel Corporation
       3                 :            :  */
       4                 :            : 
       5                 :            : #include <stddef.h>
       6                 :            : 
       7                 :            : #include <rte_jhash.h>
       8                 :            : #include <rte_hash_crc.h>
       9                 :            : 
      10                 :            : #include "ip_frag_common.h"
      11                 :            : 
      12                 :            : #define IP_FRAG_TBL_POS(tbl, sig)       \
      13                 :            :         ((tbl)->pkt + ((sig) & (tbl)->entry_mask))
      14                 :            : 
      15                 :            : static inline void
      16                 :            : ip_frag_tbl_add(struct rte_ip_frag_tbl *tbl,  struct ip_frag_pkt *fp,
      17                 :            :         const struct ip_frag_key *key, uint64_t tms)
      18                 :            : {
      19                 :          0 :         fp->key = key[0];
      20                 :            :         ip_frag_reset(fp, tms);
      21                 :          0 :         TAILQ_INSERT_TAIL(&tbl->lru, fp, lru);
      22                 :          0 :         tbl->use_entries++;
      23                 :            :         IP_FRAG_TBL_STAT_UPDATE(&tbl->stat, add_num, 1);
      24                 :            : }
      25                 :            : 
      26                 :            : static inline void
      27                 :          0 : ip_frag_tbl_reuse(struct rte_ip_frag_tbl *tbl, struct rte_ip_frag_death_row *dr,
      28                 :            :         struct ip_frag_pkt *fp, uint64_t tms)
      29                 :            : {
      30                 :            :         ip_frag_free(fp, dr);
      31                 :            :         ip_frag_reset(fp, tms);
      32         [ #  # ]:          0 :         TAILQ_REMOVE(&tbl->lru, fp, lru);
      33                 :          0 :         TAILQ_INSERT_TAIL(&tbl->lru, fp, lru);
      34                 :            :         IP_FRAG_TBL_STAT_UPDATE(&tbl->stat, reuse_num, 1);
      35                 :          0 : }
      36                 :            : 
      37                 :            : 
      38                 :            : static inline void
      39                 :          0 : ipv4_frag_hash(const struct ip_frag_key *key, uint32_t *v1, uint32_t *v2, uint32_t seed)
      40                 :            : {
      41                 :            :         uint32_t v;
      42                 :            :         const uint32_t *p;
      43                 :            : 
      44                 :            :         p = (const uint32_t *)&key->src_dst;
      45                 :            : 
      46                 :            : #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
      47                 :          0 :         v = rte_hash_crc_4byte(p[0], seed);
      48                 :          0 :         v = rte_hash_crc_4byte(p[1], v);
      49                 :          0 :         v = rte_hash_crc_4byte(key->id, v);
      50                 :            : #else
      51                 :            : 
      52                 :            :         v = rte_jhash_3words(p[0], p[1], key->id, seed);
      53                 :            : #endif /* RTE_ARCH_X86 */
      54                 :            : 
      55                 :          0 :         *v1 =  v;
      56                 :          0 :         *v2 = (v << 7) + (v >> 14);
      57                 :          0 : }
      58                 :            : 
      59                 :            : static inline void
      60                 :          0 : ipv6_frag_hash(const struct ip_frag_key *key, uint32_t *v1, uint32_t *v2, uint32_t seed)
      61                 :            : {
      62                 :            :         uint32_t v;
      63                 :            :         const uint32_t *p;
      64                 :            : 
      65                 :            :         p = (const uint32_t *) &key->src_dst;
      66                 :            : 
      67                 :            : #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
      68                 :          0 :         v = rte_hash_crc_4byte(p[0], seed);
      69                 :          0 :         v = rte_hash_crc_4byte(p[1], v);
      70                 :          0 :         v = rte_hash_crc_4byte(p[2], v);
      71                 :          0 :         v = rte_hash_crc_4byte(p[3], v);
      72                 :          0 :         v = rte_hash_crc_4byte(p[4], v);
      73                 :          0 :         v = rte_hash_crc_4byte(p[5], v);
      74                 :          0 :         v = rte_hash_crc_4byte(p[6], v);
      75                 :          0 :         v = rte_hash_crc_4byte(p[7], v);
      76                 :          0 :         v = rte_hash_crc_4byte(key->id, v);
      77                 :            : #else
      78                 :            : 
      79                 :            :         v = rte_jhash_3words(p[0], p[1], p[2], seed);
      80                 :            :         v = rte_jhash_3words(p[3], p[4], p[5], v);
      81                 :            :         v = rte_jhash_3words(p[6], p[7], key->id, v);
      82                 :            : #endif /* RTE_ARCH_X86 */
      83                 :            : 
      84                 :          0 :         *v1 =  v;
      85                 :          0 :         *v2 = (v << 7) + (v >> 14);
      86                 :          0 : }
      87                 :            : 
      88                 :            : struct rte_mbuf *
      89                 :          0 : ip_frag_process(struct ip_frag_pkt *fp, struct rte_ip_frag_death_row *dr,
      90                 :            :         struct rte_mbuf *mb, uint16_t ofs, uint16_t len, uint16_t more_frags)
      91                 :            : {
      92                 :            :         uint32_t idx;
      93                 :            : 
      94                 :          0 :         fp->frag_size += len;
      95                 :            : 
      96                 :            :         /* this is the first fragment. */
      97         [ #  # ]:          0 :         if (ofs == 0) {
      98                 :          0 :                 idx = (fp->frags[IP_FIRST_FRAG_IDX].mb == NULL) ?
      99         [ #  # ]:          0 :                                 IP_FIRST_FRAG_IDX : UINT32_MAX;
     100                 :            : 
     101                 :            :         /* this is the last fragment. */
     102         [ #  # ]:          0 :         } else if (more_frags == 0) {
     103                 :          0 :                 fp->total_size = ofs + len;
     104                 :          0 :                 idx = (fp->frags[IP_LAST_FRAG_IDX].mb == NULL) ?
     105         [ #  # ]:          0 :                                 IP_LAST_FRAG_IDX : UINT32_MAX;
     106                 :            : 
     107                 :            :         /* this is the intermediate fragment. */
     108         [ #  # ]:          0 :         } else if ((idx = fp->last_idx) < RTE_DIM(fp->frags)) {
     109                 :          0 :                 fp->last_idx++;
     110                 :            :         }
     111                 :            : 
     112                 :            :         /*
     113                 :            :          * erroneous packet: either exceed max allowed number of fragments,
     114                 :            :          * or duplicate first/last fragment encountered.
     115                 :            :          */
     116         [ #  # ]:          0 :         if (idx >= RTE_DIM(fp->frags)) {
     117                 :            : 
     118                 :            :                 /* report an error. */
     119                 :            :                 if (fp->key.key_len == IPV4_KEYLEN)
     120                 :            :                         IP_FRAG_LOG(DEBUG, "%s:%d invalid fragmented packet:\n"
     121                 :            :                                 "ipv4_frag_pkt: %p, key: <%" PRIx64 ", %#x>, "
     122                 :            :                                 "total_size: %u, frag_size: %u, last_idx: %u\n"
     123                 :            :                                 "first fragment: ofs: %u, len: %u\n"
     124                 :            :                                 "last fragment: ofs: %u, len: %u\n\n",
     125                 :            :                                 __func__, __LINE__,
     126                 :            :                                 fp, fp->key.src_dst[0], fp->key.id,
     127                 :            :                                 fp->total_size, fp->frag_size, fp->last_idx,
     128                 :            :                                 fp->frags[IP_FIRST_FRAG_IDX].ofs,
     129                 :            :                                 fp->frags[IP_FIRST_FRAG_IDX].len,
     130                 :            :                                 fp->frags[IP_LAST_FRAG_IDX].ofs,
     131                 :            :                                 fp->frags[IP_LAST_FRAG_IDX].len);
     132                 :            :                 else
     133                 :            :                         IP_FRAG_LOG(DEBUG, "%s:%d invalid fragmented packet:\n"
     134                 :            :                                 "ipv6_frag_pkt: %p, key: <" IPv6_KEY_BYTES_FMT ", %#x>, "
     135                 :            :                                 "total_size: %u, frag_size: %u, last_idx: %u\n"
     136                 :            :                                 "first fragment: ofs: %u, len: %u\n"
     137                 :            :                                 "last fragment: ofs: %u, len: %u\n\n",
     138                 :            :                                 __func__, __LINE__,
     139                 :            :                                 fp, IPv6_KEY_BYTES(fp->key.src_dst), fp->key.id,
     140                 :            :                                 fp->total_size, fp->frag_size, fp->last_idx,
     141                 :            :                                 fp->frags[IP_FIRST_FRAG_IDX].ofs,
     142                 :            :                                 fp->frags[IP_FIRST_FRAG_IDX].len,
     143                 :            :                                 fp->frags[IP_LAST_FRAG_IDX].ofs,
     144                 :            :                                 fp->frags[IP_LAST_FRAG_IDX].len);
     145                 :            : 
     146                 :            :                 /* free all fragments, invalidate the entry. */
     147                 :            :                 ip_frag_free(fp, dr);
     148                 :            :                 ip_frag_key_invalidate(&fp->key);
     149                 :          0 :                 IP_FRAG_MBUF2DR(dr, mb);
     150                 :            : 
     151                 :          0 :                 return NULL;
     152                 :            :         }
     153                 :            : 
     154                 :          0 :         fp->frags[idx].ofs = ofs;
     155                 :          0 :         fp->frags[idx].len = len;
     156                 :          0 :         fp->frags[idx].mb = mb;
     157                 :            : 
     158                 :            :         mb = NULL;
     159                 :            : 
     160                 :            :         /* not all fragments are collected yet. */
     161         [ #  # ]:          0 :         if (likely (fp->frag_size < fp->total_size)) {
     162                 :            :                 return mb;
     163                 :            : 
     164                 :            :         /* if we collected all fragments, then try to reassemble. */
     165         [ #  # ]:          0 :         } else if (fp->frag_size == fp->total_size &&
     166         [ #  # ]:          0 :                         fp->frags[IP_FIRST_FRAG_IDX].mb != NULL) {
     167         [ #  # ]:          0 :                 if (fp->key.key_len == IPV4_KEYLEN)
     168                 :          0 :                         mb = ipv4_frag_reassemble(fp);
     169                 :            :                 else
     170                 :          0 :                         mb = ipv6_frag_reassemble(fp);
     171                 :            :         }
     172                 :            : 
     173                 :            :         /* errorenous set of fragments. */
     174         [ #  # ]:          0 :         if (mb == NULL) {
     175                 :            : 
     176                 :            :                 /* report an error. */
     177                 :            :                 if (fp->key.key_len == IPV4_KEYLEN)
     178                 :            :                         IP_FRAG_LOG(DEBUG, "%s:%d invalid fragmented packet:\n"
     179                 :            :                                 "ipv4_frag_pkt: %p, key: <%" PRIx64 ", %#x>, "
     180                 :            :                                 "total_size: %u, frag_size: %u, last_idx: %u\n"
     181                 :            :                                 "first fragment: ofs: %u, len: %u\n"
     182                 :            :                                 "last fragment: ofs: %u, len: %u\n\n",
     183                 :            :                                 __func__, __LINE__,
     184                 :            :                                 fp, fp->key.src_dst[0], fp->key.id,
     185                 :            :                                 fp->total_size, fp->frag_size, fp->last_idx,
     186                 :            :                                 fp->frags[IP_FIRST_FRAG_IDX].ofs,
     187                 :            :                                 fp->frags[IP_FIRST_FRAG_IDX].len,
     188                 :            :                                 fp->frags[IP_LAST_FRAG_IDX].ofs,
     189                 :            :                                 fp->frags[IP_LAST_FRAG_IDX].len);
     190                 :            :                 else
     191                 :            :                         IP_FRAG_LOG(DEBUG, "%s:%d invalid fragmented packet:\n"
     192                 :            :                                 "ipv6_frag_pkt: %p, key: <" IPv6_KEY_BYTES_FMT ", %#x>, "
     193                 :            :                                 "total_size: %u, frag_size: %u, last_idx: %u\n"
     194                 :            :                                 "first fragment: ofs: %u, len: %u\n"
     195                 :            :                                 "last fragment: ofs: %u, len: %u\n\n",
     196                 :            :                                 __func__, __LINE__,
     197                 :            :                                 fp, IPv6_KEY_BYTES(fp->key.src_dst), fp->key.id,
     198                 :            :                                 fp->total_size, fp->frag_size, fp->last_idx,
     199                 :            :                                 fp->frags[IP_FIRST_FRAG_IDX].ofs,
     200                 :            :                                 fp->frags[IP_FIRST_FRAG_IDX].len,
     201                 :            :                                 fp->frags[IP_LAST_FRAG_IDX].ofs,
     202                 :            :                                 fp->frags[IP_LAST_FRAG_IDX].len);
     203                 :            : 
     204                 :            :                 /* free associated resources. */
     205                 :            :                 ip_frag_free(fp, dr);
     206                 :            :         }
     207                 :            : 
     208                 :            :         /* we are done with that entry, invalidate it. */
     209                 :            :         ip_frag_key_invalidate(&fp->key);
     210                 :          0 :         return mb;
     211                 :            : }
     212                 :            : 
     213                 :            : 
     214                 :            : /*
     215                 :            :  * Find an entry in the table for the corresponding fragment.
     216                 :            :  * If such entry is not present, then allocate a new one.
     217                 :            :  * If the entry is stale, then free and reuse it.
     218                 :            :  */
     219                 :            : struct ip_frag_pkt *
     220                 :          0 : ip_frag_find(struct rte_ip_frag_tbl *tbl, struct rte_ip_frag_death_row *dr,
     221                 :            :         const struct ip_frag_key *key, uint64_t tms)
     222                 :            : {
     223                 :            :         struct ip_frag_pkt *pkt, *free, *stale, *lru;
     224                 :            :         uint64_t max_cycles;
     225                 :            : 
     226                 :            :         /*
     227                 :            :          * Actually the two line below are totally redundant.
     228                 :            :          * they are here, just to make gcc 4.6 happy.
     229                 :            :          */
     230                 :          0 :         free = NULL;
     231                 :          0 :         stale = NULL;
     232                 :          0 :         max_cycles = tbl->max_cycles;
     233                 :            : 
     234                 :            :         IP_FRAG_TBL_STAT_UPDATE(&tbl->stat, find_num, 1);
     235                 :            : 
     236         [ #  # ]:          0 :         if ((pkt = ip_frag_lookup(tbl, key, tms, &free, &stale)) == NULL) {
     237                 :            : 
     238                 :            :                 /*timed-out entry, free and invalidate it*/
     239         [ #  # ]:          0 :                 if (stale != NULL) {
     240                 :          0 :                         ip_frag_tbl_del(tbl, dr, stale);
     241                 :          0 :                         free = stale;
     242                 :            : 
     243                 :            :                 /*
     244                 :            :                  * we found a free entry, check if we can use it.
     245                 :            :                  * If we run out of free entries in the table, then
     246                 :            :                  * check if we have a timed out entry to delete.
     247                 :            :                  */
     248         [ #  # ]:          0 :                 } else if (free != NULL &&
     249         [ #  # ]:          0 :                                 tbl->max_entries <= tbl->use_entries) {
     250                 :          0 :                         lru = TAILQ_FIRST(&tbl->lru);
     251         [ #  # ]:          0 :                         if (max_cycles + lru->start < tms) {
     252                 :          0 :                                 ip_frag_tbl_del(tbl, dr, lru);
     253                 :            :                         } else {
     254                 :          0 :                                 free = NULL;
     255                 :            :                                 IP_FRAG_TBL_STAT_UPDATE(&tbl->stat,
     256                 :            :                                         fail_nospace, 1);
     257                 :            :                         }
     258                 :            :                 }
     259                 :            : 
     260                 :            :                 /* found a free entry to reuse. */
     261         [ #  # ]:          0 :                 if (free != NULL) {
     262                 :            :                         ip_frag_tbl_add(tbl,  free, key, tms);
     263                 :          0 :                         pkt = free;
     264                 :            :                 }
     265                 :            : 
     266                 :            :         /*
     267                 :            :          * we found the flow, but it is already timed out,
     268                 :            :          * so free associated resources, reposition it in the LRU list,
     269                 :            :          * and reuse it.
     270                 :            :          */
     271         [ #  # ]:          0 :         } else if (max_cycles + pkt->start < tms) {
     272                 :          0 :                 ip_frag_tbl_reuse(tbl, dr, pkt, tms);
     273                 :            :         }
     274                 :            : 
     275                 :            :         IP_FRAG_TBL_STAT_UPDATE(&tbl->stat, fail_total, (pkt == NULL));
     276                 :            : 
     277                 :          0 :         tbl->last = pkt;
     278                 :          0 :         return pkt;
     279                 :            : }
     280                 :            : 
     281                 :            : struct ip_frag_pkt *
     282                 :          0 : ip_frag_lookup(struct rte_ip_frag_tbl *tbl,
     283                 :            :         const struct ip_frag_key *key, uint64_t tms,
     284                 :            :         struct ip_frag_pkt **free, struct ip_frag_pkt **stale)
     285                 :            : {
     286                 :            :         struct ip_frag_pkt *p1, *p2;
     287                 :            :         struct ip_frag_pkt *empty, *old;
     288                 :            :         uint64_t max_cycles;
     289                 :            :         uint32_t i, assoc, sig1, sig2;
     290                 :            : 
     291                 :            :         empty = NULL;
     292                 :            :         old = NULL;
     293                 :            : 
     294                 :          0 :         max_cycles = tbl->max_cycles;
     295                 :          0 :         assoc = tbl->bucket_entries;
     296                 :            : 
     297   [ #  #  #  # ]:          0 :         if (tbl->last != NULL && ip_frag_key_cmp(key, &tbl->last->key) == 0)
     298                 :            :                 return tbl->last;
     299                 :            : 
     300                 :            :         /* different hashing methods for IPv4 and IPv6 */
     301         [ #  # ]:          0 :         if (key->key_len == IPV4_KEYLEN)
     302                 :          0 :                 ipv4_frag_hash(key, &sig1, &sig2, tbl->seed);
     303                 :            :         else
     304                 :          0 :                 ipv6_frag_hash(key, &sig1, &sig2, tbl->seed);
     305                 :            : 
     306                 :          0 :         p1 = IP_FRAG_TBL_POS(tbl, sig1);
     307                 :          0 :         p2 = IP_FRAG_TBL_POS(tbl, sig2);
     308                 :            : 
     309         [ #  # ]:          0 :         for (i = 0; i != assoc; i++) {
     310                 :            :                 if (p1->key.key_len == IPV4_KEYLEN)
     311                 :            :                         IP_FRAG_LOG(DEBUG, "%s:%d:\n"
     312                 :            :                                         "tbl: %p, max_entries: %u, use_entries: %u\n"
     313                 :            :                                         "ipv4_frag_pkt line0: %p, index: %u from %u\n"
     314                 :            :                         "key: <%" PRIx64 ", %#x>, start: %" PRIu64 "\n",
     315                 :            :                                         __func__, __LINE__,
     316                 :            :                                         tbl, tbl->max_entries, tbl->use_entries,
     317                 :            :                                         p1, i, assoc,
     318                 :            :                         p1[i].key.src_dst[0], p1[i].key.id, p1[i].start);
     319                 :            :                 else
     320                 :            :                         IP_FRAG_LOG(DEBUG, "%s:%d:\n"
     321                 :            :                                         "tbl: %p, max_entries: %u, use_entries: %u\n"
     322                 :            :                                         "ipv6_frag_pkt line0: %p, index: %u from %u\n"
     323                 :            :                         "key: <" IPv6_KEY_BYTES_FMT ", %#x>, start: %" PRIu64 "\n",
     324                 :            :                                         __func__, __LINE__,
     325                 :            :                                         tbl, tbl->max_entries, tbl->use_entries,
     326                 :            :                                         p1, i, assoc,
     327                 :            :                         IPv6_KEY_BYTES(p1[i].key.src_dst), p1[i].key.id, p1[i].start);
     328                 :            : 
     329         [ #  # ]:          0 :                 if (ip_frag_key_cmp(key, &p1[i].key) == 0)
     330                 :          0 :                         return p1 + i;
     331         [ #  # ]:          0 :                 else if (ip_frag_key_is_empty(&p1[i].key))
     332         [ #  # ]:          0 :                         empty = (empty == NULL) ? (p1 + i) : empty;
     333         [ #  # ]:          0 :                 else if (max_cycles + p1[i].start < tms)
     334         [ #  # ]:          0 :                         old = (old == NULL) ? (p1 + i) : old;
     335                 :            : 
     336                 :            :                 if (p2->key.key_len == IPV4_KEYLEN)
     337                 :            :                         IP_FRAG_LOG(DEBUG, "%s:%d:\n"
     338                 :            :                                         "tbl: %p, max_entries: %u, use_entries: %u\n"
     339                 :            :                                         "ipv4_frag_pkt line1: %p, index: %u from %u\n"
     340                 :            :                         "key: <%" PRIx64 ", %#x>, start: %" PRIu64 "\n",
     341                 :            :                                         __func__, __LINE__,
     342                 :            :                                         tbl, tbl->max_entries, tbl->use_entries,
     343                 :            :                                         p2, i, assoc,
     344                 :            :                         p2[i].key.src_dst[0], p2[i].key.id, p2[i].start);
     345                 :            :                 else
     346                 :            :                         IP_FRAG_LOG(DEBUG, "%s:%d:\n"
     347                 :            :                                         "tbl: %p, max_entries: %u, use_entries: %u\n"
     348                 :            :                                         "ipv6_frag_pkt line1: %p, index: %u from %u\n"
     349                 :            :                         "key: <" IPv6_KEY_BYTES_FMT ", %#x>, start: %" PRIu64 "\n",
     350                 :            :                                         __func__, __LINE__,
     351                 :            :                                         tbl, tbl->max_entries, tbl->use_entries,
     352                 :            :                                         p2, i, assoc,
     353                 :            :                         IPv6_KEY_BYTES(p2[i].key.src_dst), p2[i].key.id, p2[i].start);
     354                 :            : 
     355         [ #  # ]:          0 :                 if (ip_frag_key_cmp(key, &p2[i].key) == 0)
     356                 :          0 :                         return p2 + i;
     357         [ #  # ]:          0 :                 else if (ip_frag_key_is_empty(&p2[i].key))
     358         [ #  # ]:          0 :                         empty = (empty == NULL) ?( p2 + i) : empty;
     359         [ #  # ]:          0 :                 else if (max_cycles + p2[i].start < tms)
     360         [ #  # ]:          0 :                         old = (old == NULL) ? (p2 + i) : old;
     361                 :            :         }
     362                 :            : 
     363                 :          0 :         *free = empty;
     364                 :          0 :         *stale = old;
     365                 :          0 :         return NULL;
     366                 :            : }

Generated by: LCOV version 1.14