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