remove version in all files
[dpdk.git] / examples / ipv4_frag / rte_ipv4_frag.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
35 #ifndef __INCLUDE_RTE_IPV4_FRAG_H__
36 #define __INCLUDE_RTE_IPV4_FRAG_H__
37 #include <rte_ip.h>
38
39 /**
40  * @file
41  * RTE IPv4 Fragmentation
42  *
43  * Implementation of IPv4 fragmentation.
44  *
45  */
46
47 /*
48  * Default byte size for the IPv4 Maximum Transfer Unit (MTU).
49  * This value includes the size of IPv4 header.
50  */
51 #define IPV4_MTU_DEFAULT        ETHER_MTU
52
53 /*
54  * Default payload in bytes for the IPv4 packet.
55  */
56 #define IPV4_DEFAULT_PAYLOAD    (IPV4_MTU_DEFAULT - sizeof(struct ipv4_hdr))
57
58 /*
59  * MAX number of fragments per packet allowed.
60  */
61 #define IPV4_MAX_FRAGS_PER_PACKET       0x80
62
63
64 /* Debug on/off */
65 #ifdef RTE_IPV4_FRAG_DEBUG
66
67 #define RTE_IPV4_FRAG_ASSERT(exp)                                       \
68 if (!(exp))     {                                                       \
69         rte_panic("function %s, line%d\tassert \"" #exp "\" failed\n",  \
70                 __func__, __LINE__);                                    \
71 }
72
73 #else /*RTE_IPV4_FRAG_DEBUG*/
74
75 #define RTE_IPV4_FRAG_ASSERT(exp)       do { } while(0)
76
77 #endif /*RTE_IPV4_FRAG_DEBUG*/
78
79 /* Fragment Offset */
80 #define IPV4_HDR_DF_SHIFT                       14
81 #define IPV4_HDR_MF_SHIFT                       13
82 #define IPV4_HDR_FO_SHIFT                       3
83
84 #define IPV4_HDR_DF_MASK                        (1 << IPV4_HDR_DF_SHIFT)
85 #define IPV4_HDR_MF_MASK                        (1 << IPV4_HDR_MF_SHIFT)
86
87 #define IPV4_HDR_FO_MASK                        ((1 << IPV4_HDR_FO_SHIFT) - 1)
88
89 static inline void __fill_ipv4hdr_frag(struct ipv4_hdr *dst,
90                 const struct ipv4_hdr *src, uint16_t len, uint16_t fofs,
91                 uint16_t dofs, uint32_t mf)
92 {
93         rte_memcpy(dst, src, sizeof(*dst));
94         fofs = (uint16_t)(fofs + (dofs >> IPV4_HDR_FO_SHIFT));
95         fofs = (uint16_t)(fofs | mf << IPV4_HDR_MF_SHIFT);
96         dst->fragment_offset = rte_cpu_to_be_16(fofs);
97         dst->total_length = rte_cpu_to_be_16(len);
98         dst->hdr_checksum = 0;
99 }
100
101 static inline void __free_fragments(struct rte_mbuf *mb[], uint32_t num)
102 {
103         uint32_t i;
104         for (i = 0; i != num; i++)
105                 rte_pktmbuf_free(mb[i]);
106 }
107
108 /**
109  * IPv4 fragmentation.
110  *
111  * This function implements the fragmentation of IPv4 packets.
112  *
113  * @param pkt_in
114  *   The input packet.
115  * @param pkts_out
116  *   Array storing the output fragments.
117  * @param mtu_size
118  *   Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv4
119  *   datagrams. This value includes the size of the IPv4 header.
120  * @param pool_direct
121  *   MBUF pool used for allocating direct buffers for the output fragments.
122  * @param pool_indirect
123  *   MBUF pool used for allocating indirect buffers for the output fragments.
124  * @return
125  *   Upon successful completion - number of output fragments placed
126  *   in the pkts_out array.
127  *   Otherwise - (-1) * <errno>.
128  */
129 static inline int32_t rte_ipv4_fragmentation(struct rte_mbuf *pkt_in,
130         struct rte_mbuf **pkts_out,
131         uint16_t nb_pkts_out,
132         uint16_t mtu_size,
133         struct rte_mempool *pool_direct,
134         struct rte_mempool *pool_indirect)
135 {
136         struct rte_mbuf *in_seg = NULL;
137         struct ipv4_hdr *in_hdr;
138         uint32_t out_pkt_pos, in_seg_data_pos;
139         uint32_t more_in_segs;
140         uint16_t fragment_offset, flag_offset, frag_size;
141
142         frag_size = (uint16_t)(mtu_size - sizeof(struct ipv4_hdr));
143
144         /* Fragment size should be a multiply of 8. */
145         RTE_IPV4_FRAG_ASSERT((frag_size & IPV4_HDR_FO_MASK) == 0);
146
147         /* Fragment size should be a multiply of 8. */
148         RTE_IPV4_FRAG_ASSERT(IPV4_MAX_FRAGS_PER_PACKET * frag_size >=
149             (uint16_t)(pkt_in->pkt.pkt_len - sizeof (struct ipv4_hdr)));
150
151         in_hdr = (struct ipv4_hdr*) pkt_in->pkt.data;
152         flag_offset = rte_cpu_to_be_16(in_hdr->fragment_offset);
153
154         /* If Don't Fragment flag is set */
155         if (unlikely ((flag_offset & IPV4_HDR_DF_MASK) != 0))
156                 return (-ENOTSUP);
157
158         /* Check that pkts_out is big enough to hold all fragments */
159         if (unlikely (frag_size * nb_pkts_out <
160             (uint16_t)(pkt_in->pkt.pkt_len - sizeof (struct ipv4_hdr))))
161                 return (-EINVAL);
162
163         in_seg = pkt_in;
164         in_seg_data_pos = sizeof(struct ipv4_hdr);
165         out_pkt_pos = 0;
166         fragment_offset = 0;
167
168         more_in_segs = 1;
169         while (likely(more_in_segs)) {
170                 struct rte_mbuf *out_pkt = NULL, *out_seg_prev = NULL;
171                 uint32_t more_out_segs;
172                 struct ipv4_hdr *out_hdr;
173
174                 /* Allocate direct buffer */
175                 out_pkt = rte_pktmbuf_alloc(pool_direct);
176                 if (unlikely(out_pkt == NULL)) {
177                         __free_fragments(pkts_out, out_pkt_pos);
178                         return (-ENOMEM);
179                 }
180
181                 /* Reserve space for the IP header that will be built later */
182                 out_pkt->pkt.data_len = sizeof(struct ipv4_hdr);
183                 out_pkt->pkt.pkt_len = sizeof(struct ipv4_hdr);
184
185                 out_seg_prev = out_pkt;
186                 more_out_segs = 1;
187                 while (likely(more_out_segs && more_in_segs)) {
188                         struct rte_mbuf *out_seg = NULL;
189                         uint32_t len;
190
191                         /* Allocate indirect buffer */
192                         out_seg = rte_pktmbuf_alloc(pool_indirect);
193                         if (unlikely(out_seg == NULL)) {
194                                 rte_pktmbuf_free(out_pkt);
195                                 __free_fragments(pkts_out, out_pkt_pos);
196                                 return (-ENOMEM);
197                         }
198                         out_seg_prev->pkt.next = out_seg;
199                         out_seg_prev = out_seg;
200
201                         /* Prepare indirect buffer */
202                         rte_pktmbuf_attach(out_seg, in_seg);
203                         len = mtu_size - out_pkt->pkt.pkt_len;
204                         if (len > (in_seg->pkt.data_len - in_seg_data_pos)) {
205                                 len = in_seg->pkt.data_len - in_seg_data_pos;
206                         }
207                         out_seg->pkt.data = (char*) in_seg->pkt.data + (uint16_t)in_seg_data_pos;
208                         out_seg->pkt.data_len = (uint16_t)len;
209                         out_pkt->pkt.pkt_len = (uint16_t)(len +
210                             out_pkt->pkt.pkt_len);
211                         out_pkt->pkt.nb_segs += 1;
212                         in_seg_data_pos += len;
213
214                         /* Current output packet (i.e. fragment) done ? */
215                         if (unlikely(out_pkt->pkt.pkt_len >= mtu_size)) {
216                                 more_out_segs = 0;
217                         }
218
219                         /* Current input segment done ? */
220                         if (unlikely(in_seg_data_pos == in_seg->pkt.data_len)) {
221                                 in_seg = in_seg->pkt.next;
222                                 in_seg_data_pos = 0;
223
224                                 if (unlikely(in_seg == NULL)) {
225                                         more_in_segs = 0;
226                                 }
227                         }
228                 }
229
230                 /* Build the IP header */
231
232                 out_hdr = (struct ipv4_hdr*) out_pkt->pkt.data;
233
234                 __fill_ipv4hdr_frag(out_hdr, in_hdr,
235                     (uint16_t)out_pkt->pkt.pkt_len,
236                     flag_offset, fragment_offset, more_in_segs);
237
238                 fragment_offset = (uint16_t)(fragment_offset +
239                     out_pkt->pkt.pkt_len - sizeof(struct ipv4_hdr));
240
241                 out_pkt->ol_flags |= PKT_TX_IP_CKSUM;
242                 out_pkt->pkt.l3_len = sizeof(struct ipv4_hdr);
243
244                 /* Write the fragment to the output list */
245                 pkts_out[out_pkt_pos] = out_pkt;
246                 out_pkt_pos ++;
247         }
248
249         return (out_pkt_pos);
250 }
251
252 #endif