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