1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
5 #ifndef _IP_FRAG_COMMON_H_
6 #define _IP_FRAG_COMMON_H_
8 #include "rte_ip_frag.h"
9 #include "ip_reassembly.h"
12 #ifdef RTE_LIBRTE_IP_FRAG_DEBUG
13 #define IP_FRAG_LOG(lvl, fmt, args...) RTE_LOG(lvl, USER1, fmt, ##args)
15 #define IP_FRAG_LOG(lvl, fmt, args...) do {} while(0)
16 #endif /* IP_FRAG_DEBUG */
22 #define IP_FRAG_MBUF2DR(dr, mb) ((dr)->row[(dr)->cnt++] = (mb))
24 #define IPv6_KEY_BYTES(key) \
25 (key)[0], (key)[1], (key)[2], (key)[3]
26 #define IPv6_KEY_BYTES_FMT \
27 "%08" PRIx64 "%08" PRIx64 "%08" PRIx64 "%08" PRIx64
29 #ifdef RTE_LIBRTE_IP_FRAG_TBL_STAT
30 #define IP_FRAG_TBL_STAT_UPDATE(s, f, v) ((s)->f += (v))
32 #define IP_FRAG_TBL_STAT_UPDATE(s, f, v) do {} while (0)
33 #endif /* IP_FRAG_TBL_STAT */
35 /* internal functions declarations */
36 struct rte_mbuf * ip_frag_process(struct ip_frag_pkt *fp,
37 struct rte_ip_frag_death_row *dr, struct rte_mbuf *mb,
38 uint16_t ofs, uint16_t len, uint16_t more_frags);
40 struct ip_frag_pkt * ip_frag_find(struct rte_ip_frag_tbl *tbl,
41 struct rte_ip_frag_death_row *dr,
42 const struct ip_frag_key *key, uint64_t tms);
44 struct ip_frag_pkt * ip_frag_lookup(struct rte_ip_frag_tbl *tbl,
45 const struct ip_frag_key *key, uint64_t tms,
46 struct ip_frag_pkt **free, struct ip_frag_pkt **stale);
48 /* these functions need to be declared here as ip_frag_process relies on them */
49 struct rte_mbuf *ipv4_frag_reassemble(struct ip_frag_pkt *fp);
50 struct rte_mbuf *ipv6_frag_reassemble(struct ip_frag_pkt *fp);
55 * misc frag key functions
58 /* check if key is empty */
60 ip_frag_key_is_empty(const struct ip_frag_key * key)
62 return (key->key_len == 0);
65 /* invalidate the key */
67 ip_frag_key_invalidate(struct ip_frag_key * key)
72 /* compare two keys */
73 static inline uint64_t
74 ip_frag_key_cmp(const struct ip_frag_key * k1, const struct ip_frag_key * k2)
78 val = k1->id_key_len ^ k2->id_key_len;
79 for (i = 0; i < k1->key_len; i++)
80 val |= k1->src_dst[i] ^ k2->src_dst[i];
85 * misc fragment functions
88 /* put fragment on death row */
90 ip_frag_free(struct ip_frag_pkt *fp, struct rte_ip_frag_death_row *dr)
95 for (i = 0; i != fp->last_idx; i++) {
96 if (fp->frags[i].mb != NULL) {
97 dr->row[k++] = fp->frags[i].mb;
98 fp->frags[i].mb = NULL;
106 /* delete fragment's mbufs immediately instead of using death row */
108 ip_frag_free_immediate(struct ip_frag_pkt *fp)
112 for (i = 0; i < fp->last_idx; i++) {
113 if (fp->frags[i].mb != NULL) {
114 IP_FRAG_LOG(DEBUG, "%s:%d\n"
115 "mbuf: %p, tms: %" PRIu64", key: <%" PRIx64 ", %#x>\n",
116 __func__, __LINE__, fp->frags[i].mb, fp->start,
117 fp->key.src_dst[0], fp->key.id);
118 rte_pktmbuf_free(fp->frags[i].mb);
119 fp->frags[i].mb = NULL;
126 /* if key is empty, mark key as in use */
128 ip_frag_inuse(struct rte_ip_frag_tbl *tbl, const struct ip_frag_pkt *fp)
130 if (ip_frag_key_is_empty(&fp->key)) {
131 TAILQ_REMOVE(&tbl->lru, fp, lru);
136 /* reset the fragment */
138 ip_frag_reset(struct ip_frag_pkt *fp, uint64_t tms)
140 static const struct ip_frag zero_frag = {
147 fp->total_size = UINT32_MAX;
149 fp->last_idx = IP_MIN_FRAG_NUM;
150 fp->frags[IP_LAST_FRAG_IDX] = zero_frag;
151 fp->frags[IP_FIRST_FRAG_IDX] = zero_frag;
154 /* local frag table helper functions */
156 ip_frag_tbl_del(struct rte_ip_frag_tbl *tbl, struct rte_ip_frag_death_row *dr,
157 struct ip_frag_pkt *fp)
159 ip_frag_free(fp, dr);
160 ip_frag_key_invalidate(&fp->key);
161 TAILQ_REMOVE(&tbl->lru, fp, lru);
163 IP_FRAG_TBL_STAT_UPDATE(&tbl->stat, del_num, 1);
166 #endif /* _IP_FRAG_COMMON_H_ */