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