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