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