New upstream version 18.02
[deb_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 /* internal functions declarations */
29 struct rte_mbuf * ip_frag_process(struct ip_frag_pkt *fp,
30                 struct rte_ip_frag_death_row *dr, struct rte_mbuf *mb,
31                 uint16_t ofs, uint16_t len, uint16_t more_frags);
32
33 struct ip_frag_pkt * ip_frag_find(struct rte_ip_frag_tbl *tbl,
34                 struct rte_ip_frag_death_row *dr,
35                 const struct ip_frag_key *key, uint64_t tms);
36
37 struct ip_frag_pkt * ip_frag_lookup(struct rte_ip_frag_tbl *tbl,
38         const struct ip_frag_key *key, uint64_t tms,
39         struct ip_frag_pkt **free, struct ip_frag_pkt **stale);
40
41 /* these functions need to be declared here as ip_frag_process relies on them */
42 struct rte_mbuf *ipv4_frag_reassemble(struct ip_frag_pkt *fp);
43 struct rte_mbuf *ipv6_frag_reassemble(struct ip_frag_pkt *fp);
44
45
46
47 /*
48  * misc frag key functions
49  */
50
51 /* check if key is empty */
52 static inline int
53 ip_frag_key_is_empty(const struct ip_frag_key * key)
54 {
55         uint32_t i;
56         for (i = 0; i < RTE_MIN(key->key_len, RTE_DIM(key->src_dst)); i++)
57                 if (key->src_dst[i] != 0)
58                         return 0;
59         return 1;
60 }
61
62 /* empty the key */
63 static inline void
64 ip_frag_key_invalidate(struct ip_frag_key * key)
65 {
66         uint32_t i;
67         for (i = 0; i < key->key_len; i++)
68                 key->src_dst[i] = 0;
69 }
70
71 /* compare two keys */
72 static inline int
73 ip_frag_key_cmp(const struct ip_frag_key * k1, const struct ip_frag_key * k2)
74 {
75         uint32_t i, val;
76         val = k1->id ^ k2->id;
77         for (i = 0; i < k1->key_len; i++)
78                 val |= k1->src_dst[i] ^ k2->src_dst[i];
79         return val;
80 }
81
82 /*
83  * misc fragment functions
84  */
85
86 /* put fragment on death row */
87 static inline void
88 ip_frag_free(struct ip_frag_pkt *fp, struct rte_ip_frag_death_row *dr)
89 {
90         uint32_t i, k;
91
92         k = dr->cnt;
93         for (i = 0; i != fp->last_idx; i++) {
94                 if (fp->frags[i].mb != NULL) {
95                         dr->row[k++] = fp->frags[i].mb;
96                         fp->frags[i].mb = NULL;
97                 }
98         }
99
100         fp->last_idx = 0;
101         dr->cnt = k;
102 }
103
104 /* delete fragment's mbufs immediately instead of using death row */
105 static inline void
106 ip_frag_free_immediate(struct ip_frag_pkt *fp)
107 {
108         uint32_t i;
109
110         for (i = 0; i < fp->last_idx; i++) {
111                 if (fp->frags[i].mb != NULL) {
112                         IP_FRAG_LOG(DEBUG, "%s:%d\n"
113                             "mbuf: %p, tms: %" PRIu64", key: <%" PRIx64 ", %#x>\n",
114                             __func__, __LINE__, fp->frags[i].mb, fp->start,
115                             fp->key.src_dst[0], fp->key.id);
116                         rte_pktmbuf_free(fp->frags[i].mb);
117                         fp->frags[i].mb = NULL;
118                 }
119         }
120
121         fp->last_idx = 0;
122 }
123
124 /* if key is empty, mark key as in use */
125 static inline void
126 ip_frag_inuse(struct rte_ip_frag_tbl *tbl, const struct  ip_frag_pkt *fp)
127 {
128         if (ip_frag_key_is_empty(&fp->key)) {
129                 TAILQ_REMOVE(&tbl->lru, fp, lru);
130                 tbl->use_entries--;
131         }
132 }
133
134 /* reset the fragment */
135 static inline void
136 ip_frag_reset(struct ip_frag_pkt *fp, uint64_t tms)
137 {
138         static const struct ip_frag zero_frag = {
139                 .ofs = 0,
140                 .len = 0,
141                 .mb = NULL,
142         };
143
144         fp->start = tms;
145         fp->total_size = UINT32_MAX;
146         fp->frag_size = 0;
147         fp->last_idx = IP_MIN_FRAG_NUM;
148         fp->frags[IP_LAST_FRAG_IDX] = zero_frag;
149         fp->frags[IP_FIRST_FRAG_IDX] = zero_frag;
150 }
151
152 #endif /* _IP_FRAG_COMMON_H_ */