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