mbuf: remove the rte_pktmbuf structure
[dpdk.git] / lib / librte_ip_frag / ip_frag_common.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _IP_FRAG_COMMON_H_
35 #define _IP_FRAG_COMMON_H_
36
37 #include "rte_ip_frag.h"
38
39 /* logging macros. */
40 #ifdef RTE_LIBRTE_IP_FRAG_DEBUG
41
42 #define IP_FRAG_LOG(lvl, fmt, args...)  RTE_LOG(lvl, USER1, fmt, ##args)
43
44 #define IP_FRAG_ASSERT(exp)                                     \
45 if (!(exp))     {                                                       \
46         rte_panic("function %s, line%d\tassert \"" #exp "\" failed\n",  \
47                 __func__, __LINE__);                                    \
48 }
49 #else
50 #define IP_FRAG_LOG(lvl, fmt, args...)  do {} while(0)
51 #define IP_FRAG_ASSERT(exp)     do {} while (0)
52 #endif /* IP_FRAG_DEBUG */
53
54 #define IPV4_KEYLEN 1
55 #define IPV6_KEYLEN 4
56
57 /* helper macros */
58 #define IP_FRAG_MBUF2DR(dr, mb) ((dr)->row[(dr)->cnt++] = (mb))
59
60 #define IPv6_KEY_BYTES(key) \
61         (key)[0], (key)[1], (key)[2], (key)[3]
62 #define IPv6_KEY_BYTES_FMT \
63         "%08" PRIx64 "%08" PRIx64 "%08" PRIx64 "%08" PRIx64
64
65 /* internal functions declarations */
66 struct rte_mbuf * ip_frag_process(struct ip_frag_pkt *fp,
67                 struct rte_ip_frag_death_row *dr, struct rte_mbuf *mb,
68                 uint16_t ofs, uint16_t len, uint16_t more_frags);
69
70 struct ip_frag_pkt * ip_frag_find(struct rte_ip_frag_tbl *tbl,
71                 struct rte_ip_frag_death_row *dr,
72                 const struct ip_frag_key *key, uint64_t tms);
73
74 struct ip_frag_pkt * ip_frag_lookup(struct rte_ip_frag_tbl *tbl,
75         const struct ip_frag_key *key, uint64_t tms,
76         struct ip_frag_pkt **free, struct ip_frag_pkt **stale);
77
78 /* these functions need to be declared here as ip_frag_process relies on them */
79 struct rte_mbuf * ipv4_frag_reassemble(const struct ip_frag_pkt *fp);
80 struct rte_mbuf * ipv6_frag_reassemble(const struct ip_frag_pkt *fp);
81
82
83
84 /*
85  * misc frag key functions
86  */
87
88 /* check if key is empty */
89 static inline int
90 ip_frag_key_is_empty(const struct ip_frag_key * key)
91 {
92         uint32_t i;
93         for (i = 0; i < key->key_len; i++)
94                 if (key->src_dst[i] != 0)
95                         return 0;
96         return 1;
97 }
98
99 /* empty the key */
100 static inline void
101 ip_frag_key_invalidate(struct ip_frag_key * key)
102 {
103         uint32_t i;
104         for (i = 0; i < key->key_len; i++)
105                 key->src_dst[i] = 0;
106 }
107
108 /* compare two keys */
109 static inline int
110 ip_frag_key_cmp(const struct ip_frag_key * k1, const struct ip_frag_key * k2)
111 {
112         uint32_t i, val;
113         val = k1->id ^ k2->id;
114         for (i = 0; i < k1->key_len; i++)
115                 val |= k1->src_dst[i] ^ k2->src_dst[i];
116         return val;
117 }
118
119 /*
120  * misc fragment functions
121  */
122
123 /* put fragment on death row */
124 static inline void
125 ip_frag_free(struct ip_frag_pkt *fp, struct rte_ip_frag_death_row *dr)
126 {
127         uint32_t i, k;
128
129         k = dr->cnt;
130         for (i = 0; i != fp->last_idx; i++) {
131                 if (fp->frags[i].mb != NULL) {
132                         dr->row[k++] = fp->frags[i].mb;
133                         fp->frags[i].mb = NULL;
134                 }
135         }
136
137         fp->last_idx = 0;
138         dr->cnt = k;
139 }
140
141 /* if key is empty, mark key as in use */
142 static inline void
143 ip_frag_inuse(struct rte_ip_frag_tbl *tbl, const struct  ip_frag_pkt *fp)
144 {
145         if (ip_frag_key_is_empty(&fp->key)) {
146                 TAILQ_REMOVE(&tbl->lru, fp, lru);
147                 tbl->use_entries--;
148         }
149 }
150
151 /* reset the fragment */
152 static inline void
153 ip_frag_reset(struct ip_frag_pkt *fp, uint64_t tms)
154 {
155         static const struct ip_frag zero_frag = {
156                 .ofs = 0,
157                 .len = 0,
158                 .mb = NULL,
159         };
160
161         fp->start = tms;
162         fp->total_size = UINT32_MAX;
163         fp->frag_size = 0;
164         fp->last_idx = IP_MIN_FRAG_NUM;
165         fp->frags[IP_LAST_FRAG_IDX] = zero_frag;
166         fp->frags[IP_FIRST_FRAG_IDX] = zero_frag;
167 }
168
169 /* chain two mbufs */
170 static inline void
171 ip_frag_chain(struct rte_mbuf *mn, struct rte_mbuf *mp)
172 {
173         struct rte_mbuf *ms;
174
175         /* adjust start of the last fragment data. */
176         rte_pktmbuf_adj(mp, (uint16_t)(mp->vlan_macip.f.l2_len +
177                 mp->vlan_macip.f.l3_len));
178
179         /* chain two fragments. */
180         ms = rte_pktmbuf_lastseg(mn);
181         ms->next = mp;
182
183         /* accumulate number of segments and total length. */
184         mn->nb_segs = (uint8_t)(mn->nb_segs + mp->nb_segs);
185         mn->pkt_len += mp->pkt_len;
186
187         /* reset pkt_len and nb_segs for chained fragment. */
188         mp->pkt_len = mp->data_len;
189         mp->nb_segs = 1;
190 }
191
192
193 #endif /* _IP_FRAG_COMMON_H_ */