e00766225dce39a8af7a7290a9d98de11e6920db
[dpdk.git] / lib / librte_ip_frag / rte_ipv6_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 <errno.h>
36
37 #include <rte_memcpy.h>
38
39 #include "ip_frag_common.h"
40
41 /**
42  * @file
43  * RTE IPv6 Fragmentation
44  *
45  * Implementation of IPv6 fragmentation.
46  *
47  */
48
49 /* Fragment Extension Header */
50 #define IPV6_HDR_MF_SHIFT                       0
51 #define IPV6_HDR_FO_SHIFT                       3
52 #define IPV6_HDR_MF_MASK                        (1 << IPV6_HDR_MF_SHIFT)
53 #define IPV6_HDR_FO_MASK                        ((1 << IPV6_HDR_FO_SHIFT) - 1)
54
55 static inline void
56 __fill_ipv6hdr_frag(struct ipv6_hdr *dst,
57                 const struct ipv6_hdr *src, uint16_t len, uint16_t fofs,
58                 uint32_t mf)
59 {
60         struct ipv6_extension_fragment *fh;
61
62         rte_memcpy(dst, src, sizeof(*dst));
63         dst->payload_len = rte_cpu_to_be_16(len);
64         dst->proto = IPPROTO_FRAGMENT;
65
66         fh = (struct ipv6_extension_fragment *) ++dst;
67         fh->next_header = src->proto;
68         fh->reserved1   = 0;
69         fh->frag_offset = rte_cpu_to_be_16(fofs);
70         fh->reserved2   = 0;
71         fh->more_frags  = rte_cpu_to_be_16(mf);
72         fh->id = 0;
73 }
74
75 static inline void
76 __free_fragments(struct rte_mbuf *mb[], uint32_t num)
77 {
78         uint32_t i;
79         for (i = 0; i < num; i++)
80                 rte_pktmbuf_free(mb[i]);
81 }
82
83 /**
84  * IPv6 fragmentation.
85  *
86  * This function implements the fragmentation of IPv6 packets.
87  *
88  * @param pkt_in
89  *   The input packet.
90  * @param pkts_out
91  *   Array storing the output fragments.
92  * @param mtu_size
93  *   Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv6
94  *   datagrams. This value includes the size of the IPv6 header.
95  * @param pool_direct
96  *   MBUF pool used for allocating direct buffers for the output fragments.
97  * @param pool_indirect
98  *   MBUF pool used for allocating indirect buffers for the output fragments.
99  * @return
100  *   Upon successful completion - number of output fragments placed
101  *   in the pkts_out array.
102  *   Otherwise - (-1) * <errno>.
103  */
104 int32_t
105 rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in,
106         struct rte_mbuf **pkts_out,
107         uint16_t nb_pkts_out,
108         uint16_t mtu_size,
109         struct rte_mempool *pool_direct,
110         struct rte_mempool *pool_indirect)
111 {
112         struct rte_mbuf *in_seg = NULL;
113         struct ipv6_hdr *in_hdr;
114         uint32_t out_pkt_pos, in_seg_data_pos;
115         uint32_t more_in_segs;
116         uint16_t fragment_offset, frag_size;
117
118         frag_size = (uint16_t)(mtu_size - sizeof(struct ipv6_hdr));
119
120         /* Fragment size should be a multiple of 8. */
121         IP_FRAG_ASSERT((frag_size & IPV6_HDR_FO_MASK) == 0);
122
123         /* Check that pkts_out is big enough to hold all fragments */
124         if (unlikely (frag_size * nb_pkts_out <
125             (uint16_t)(pkt_in->pkt_len - sizeof (struct ipv6_hdr))))
126                 return (-EINVAL);
127
128         in_hdr = (struct ipv6_hdr *) pkt_in->data;
129
130         in_seg = pkt_in;
131         in_seg_data_pos = sizeof(struct ipv6_hdr);
132         out_pkt_pos = 0;
133         fragment_offset = 0;
134
135         more_in_segs = 1;
136         while (likely(more_in_segs)) {
137                 struct rte_mbuf *out_pkt = NULL, *out_seg_prev = NULL;
138                 uint32_t more_out_segs;
139                 struct ipv6_hdr *out_hdr;
140
141                 /* Allocate direct buffer */
142                 out_pkt = rte_pktmbuf_alloc(pool_direct);
143                 if (unlikely(out_pkt == NULL)) {
144                         __free_fragments(pkts_out, out_pkt_pos);
145                         return (-ENOMEM);
146                 }
147
148                 /* Reserve space for the IP header that will be built later */
149                 out_pkt->data_len = sizeof(struct ipv6_hdr) + sizeof(struct ipv6_extension_fragment);
150                 out_pkt->pkt_len  = sizeof(struct ipv6_hdr) + sizeof(struct ipv6_extension_fragment);
151
152                 out_seg_prev = out_pkt;
153                 more_out_segs = 1;
154                 while (likely(more_out_segs && more_in_segs)) {
155                         struct rte_mbuf *out_seg = NULL;
156                         uint32_t len;
157
158                         /* Allocate indirect buffer */
159                         out_seg = rte_pktmbuf_alloc(pool_indirect);
160                         if (unlikely(out_seg == NULL)) {
161                                 rte_pktmbuf_free(out_pkt);
162                                 __free_fragments(pkts_out, out_pkt_pos);
163                                 return (-ENOMEM);
164                         }
165                         out_seg_prev->next = out_seg;
166                         out_seg_prev = out_seg;
167
168                         /* Prepare indirect buffer */
169                         rte_pktmbuf_attach(out_seg, in_seg);
170                         len = mtu_size - out_pkt->pkt_len;
171                         if (len > (in_seg->data_len - in_seg_data_pos)) {
172                                 len = in_seg->data_len - in_seg_data_pos;
173                         }
174                         out_seg->data = (char *) in_seg->data + (uint16_t) in_seg_data_pos;
175                         out_seg->data_len = (uint16_t)len;
176                         out_pkt->pkt_len = (uint16_t)(len +
177                             out_pkt->pkt_len);
178                         out_pkt->nb_segs += 1;
179                         in_seg_data_pos += len;
180
181                         /* Current output packet (i.e. fragment) done ? */
182                         if (unlikely(out_pkt->pkt_len >= mtu_size)) {
183                                 more_out_segs = 0;
184                         }
185
186                         /* Current input segment done ? */
187                         if (unlikely(in_seg_data_pos == in_seg->data_len)) {
188                                 in_seg = in_seg->next;
189                                 in_seg_data_pos = 0;
190
191                                 if (unlikely(in_seg == NULL)) {
192                                         more_in_segs = 0;
193                                 }
194                         }
195                 }
196
197                 /* Build the IP header */
198
199                 out_hdr = (struct ipv6_hdr *) out_pkt->data;
200
201                 __fill_ipv6hdr_frag(out_hdr, in_hdr,
202                     (uint16_t) out_pkt->pkt_len - sizeof(struct ipv6_hdr),
203                     fragment_offset, more_in_segs);
204
205                 fragment_offset = (uint16_t)(fragment_offset +
206                     out_pkt->pkt_len - sizeof(struct ipv6_hdr)
207                         - sizeof(struct ipv6_extension_fragment));
208
209                 /* Write the fragment to the output list */
210                 pkts_out[out_pkt_pos] = out_pkt;
211                 out_pkt_pos ++;
212         }
213
214         return (out_pkt_pos);
215 }