ip_frag: add function to delete expired entries
[dpdk.git] / lib / librte_ip_frag / ip_frag_common.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef _IP_FRAG_COMMON_H_
6 #define _IP_FRAG_COMMON_H_
7
8 #include "rte_ip_frag.h"
9
10 /* logging macros. */
11 #ifdef RTE_LIBRTE_IP_FRAG_DEBUG
12 #define IP_FRAG_LOG(lvl, fmt, args...)  RTE_LOG(lvl, USER1, fmt, ##args)
13 #else
14 #define IP_FRAG_LOG(lvl, fmt, args...)  do {} while(0)
15 #endif /* IP_FRAG_DEBUG */
16
17 #define IPV4_KEYLEN 1
18 #define IPV6_KEYLEN 4
19
20 /* helper macros */
21 #define IP_FRAG_MBUF2DR(dr, mb) ((dr)->row[(dr)->cnt++] = (mb))
22
23 #define IPv6_KEY_BYTES(key) \
24         (key)[0], (key)[1], (key)[2], (key)[3]
25 #define IPv6_KEY_BYTES_FMT \
26         "%08" PRIx64 "%08" PRIx64 "%08" PRIx64 "%08" PRIx64
27
28 #ifdef RTE_LIBRTE_IP_FRAG_TBL_STAT
29 #define IP_FRAG_TBL_STAT_UPDATE(s, f, v)        ((s)->f += (v))
30 #else
31 #define IP_FRAG_TBL_STAT_UPDATE(s, f, v)        do {} while (0)
32 #endif /* IP_FRAG_TBL_STAT */
33
34 /* internal functions declarations */
35 struct rte_mbuf * ip_frag_process(struct ip_frag_pkt *fp,
36                 struct rte_ip_frag_death_row *dr, struct rte_mbuf *mb,
37                 uint16_t ofs, uint16_t len, uint16_t more_frags);
38
39 struct ip_frag_pkt * ip_frag_find(struct rte_ip_frag_tbl *tbl,
40                 struct rte_ip_frag_death_row *dr,
41                 const struct ip_frag_key *key, uint64_t tms);
42
43 struct ip_frag_pkt * ip_frag_lookup(struct rte_ip_frag_tbl *tbl,
44         const struct ip_frag_key *key, uint64_t tms,
45         struct ip_frag_pkt **free, struct ip_frag_pkt **stale);
46
47 /* these functions need to be declared here as ip_frag_process relies on them */
48 struct rte_mbuf *ipv4_frag_reassemble(struct ip_frag_pkt *fp);
49 struct rte_mbuf *ipv6_frag_reassemble(struct ip_frag_pkt *fp);
50
51
52
53 /*
54  * misc frag key functions
55  */
56
57 /* check if key is empty */
58 static inline int
59 ip_frag_key_is_empty(const struct ip_frag_key * key)
60 {
61         uint32_t i;
62         for (i = 0; i < RTE_MIN(key->key_len, RTE_DIM(key->src_dst)); i++)
63                 if (key->src_dst[i] != 0)
64                         return 0;
65         return 1;
66 }
67
68 /* empty the key */
69 static inline void
70 ip_frag_key_invalidate(struct ip_frag_key * key)
71 {
72         uint32_t i;
73         for (i = 0; i < key->key_len; i++)
74                 key->src_dst[i] = 0;
75 }
76
77 /* compare two keys */
78 static inline int
79 ip_frag_key_cmp(const struct ip_frag_key * k1, const struct ip_frag_key * k2)
80 {
81         uint32_t i, val;
82         val = k1->id ^ k2->id;
83         for (i = 0; i < k1->key_len; i++)
84                 val |= k1->src_dst[i] ^ k2->src_dst[i];
85         return val;
86 }
87
88 /*
89  * misc fragment functions
90  */
91
92 /* put fragment on death row */
93 static inline void
94 ip_frag_free(struct ip_frag_pkt *fp, struct rte_ip_frag_death_row *dr)
95 {
96         uint32_t i, k;
97
98         k = dr->cnt;
99         for (i = 0; i != fp->last_idx; i++) {
100                 if (fp->frags[i].mb != NULL) {
101                         dr->row[k++] = fp->frags[i].mb;
102                         fp->frags[i].mb = NULL;
103                 }
104         }
105
106         fp->last_idx = 0;
107         dr->cnt = k;
108 }
109
110 /* delete fragment's mbufs immediately instead of using death row */
111 static inline void
112 ip_frag_free_immediate(struct ip_frag_pkt *fp)
113 {
114         uint32_t i;
115
116         for (i = 0; i < fp->last_idx; i++) {
117                 if (fp->frags[i].mb != NULL) {
118                         IP_FRAG_LOG(DEBUG, "%s:%d\n"
119                             "mbuf: %p, tms: %" PRIu64", key: <%" PRIx64 ", %#x>\n",
120                             __func__, __LINE__, fp->frags[i].mb, fp->start,
121                             fp->key.src_dst[0], fp->key.id);
122                         rte_pktmbuf_free(fp->frags[i].mb);
123                         fp->frags[i].mb = NULL;
124                 }
125         }
126
127         fp->last_idx = 0;
128 }
129
130 /* if key is empty, mark key as in use */
131 static inline void
132 ip_frag_inuse(struct rte_ip_frag_tbl *tbl, const struct  ip_frag_pkt *fp)
133 {
134         if (ip_frag_key_is_empty(&fp->key)) {
135                 TAILQ_REMOVE(&tbl->lru, fp, lru);
136                 tbl->use_entries--;
137         }
138 }
139
140 /* reset the fragment */
141 static inline void
142 ip_frag_reset(struct ip_frag_pkt *fp, uint64_t tms)
143 {
144         static const struct ip_frag zero_frag = {
145                 .ofs = 0,
146                 .len = 0,
147                 .mb = NULL,
148         };
149
150         fp->start = tms;
151         fp->total_size = UINT32_MAX;
152         fp->frag_size = 0;
153         fp->last_idx = IP_MIN_FRAG_NUM;
154         fp->frags[IP_LAST_FRAG_IDX] = zero_frag;
155         fp->frags[IP_FIRST_FRAG_IDX] = zero_frag;
156 }
157
158 /* local frag table helper functions */
159 static inline void
160 ip_frag_tbl_del(struct rte_ip_frag_tbl *tbl, struct rte_ip_frag_death_row *dr,
161         struct ip_frag_pkt *fp)
162 {
163         ip_frag_free(fp, dr);
164         ip_frag_key_invalidate(&fp->key);
165         TAILQ_REMOVE(&tbl->lru, fp, lru);
166         tbl->use_entries--;
167         IP_FRAG_TBL_STAT_UPDATE(&tbl->stat, del_num, 1);
168 }
169
170 #endif /* _IP_FRAG_COMMON_H_ */