4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
38 #include <rte_byteorder.h>
39 #include <rte_memcpy.h>
40 #include <rte_mempool.h>
41 #include <rte_debug.h>
45 #include "rte_ip_frag.h"
46 #include "ip_frag_common.h"
49 #define IPV4_HDR_DF_SHIFT 14
50 #define IPV4_HDR_MF_SHIFT 13
51 #define IPV4_HDR_FO_SHIFT 3
53 #define IPV4_HDR_DF_MASK (1 << IPV4_HDR_DF_SHIFT)
54 #define IPV4_HDR_MF_MASK (1 << IPV4_HDR_MF_SHIFT)
56 #define IPV4_HDR_FO_MASK ((1 << IPV4_HDR_FO_SHIFT) - 1)
58 static inline void __fill_ipv4hdr_frag(struct ipv4_hdr *dst,
59 const struct ipv4_hdr *src, uint16_t len, uint16_t fofs,
60 uint16_t dofs, uint32_t mf)
62 rte_memcpy(dst, src, sizeof(*dst));
63 fofs = (uint16_t)(fofs + (dofs >> IPV4_HDR_FO_SHIFT));
64 fofs = (uint16_t)(fofs | mf << IPV4_HDR_MF_SHIFT);
65 dst->fragment_offset = rte_cpu_to_be_16(fofs);
66 dst->total_length = rte_cpu_to_be_16(len);
67 dst->hdr_checksum = 0;
70 static inline void __free_fragments(struct rte_mbuf *mb[], uint32_t num)
73 for (i = 0; i != num; i++)
74 rte_pktmbuf_free(mb[i]);
80 * This function implements the fragmentation of IPv4 packets.
85 * Array storing the output fragments.
87 * Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv4
88 * datagrams. This value includes the size of the IPv4 header.
90 * MBUF pool used for allocating direct buffers for the output fragments.
91 * @param pool_indirect
92 * MBUF pool used for allocating indirect buffers for the output fragments.
94 * Upon successful completion - number of output fragments placed
95 * in the pkts_out array.
96 * Otherwise - (-1) * <errno>.
99 rte_ipv4_fragment_packet(struct rte_mbuf *pkt_in,
100 struct rte_mbuf **pkts_out,
101 uint16_t nb_pkts_out,
103 struct rte_mempool *pool_direct,
104 struct rte_mempool *pool_indirect)
106 struct rte_mbuf *in_seg = NULL;
107 struct ipv4_hdr *in_hdr;
108 uint32_t out_pkt_pos, in_seg_data_pos;
109 uint32_t more_in_segs;
110 uint16_t fragment_offset, flag_offset, frag_size;
112 frag_size = (uint16_t)(mtu_size - sizeof(struct ipv4_hdr));
114 /* Fragment size should be a multiply of 8. */
115 RTE_IP_FRAG_ASSERT((frag_size & IPV4_HDR_FO_MASK) == 0);
117 in_hdr = (struct ipv4_hdr *) pkt_in->pkt.data;
118 flag_offset = rte_cpu_to_be_16(in_hdr->fragment_offset);
120 /* If Don't Fragment flag is set */
121 if (unlikely ((flag_offset & IPV4_HDR_DF_MASK) != 0))
124 /* Check that pkts_out is big enough to hold all fragments */
125 if (unlikely(frag_size * nb_pkts_out <
126 (uint16_t)(pkt_in->pkt.pkt_len - sizeof (struct ipv4_hdr))))
130 in_seg_data_pos = sizeof(struct ipv4_hdr);
135 while (likely(more_in_segs)) {
136 struct rte_mbuf *out_pkt = NULL, *out_seg_prev = NULL;
137 uint32_t more_out_segs;
138 struct ipv4_hdr *out_hdr;
140 /* Allocate direct buffer */
141 out_pkt = rte_pktmbuf_alloc(pool_direct);
142 if (unlikely(out_pkt == NULL)) {
143 __free_fragments(pkts_out, out_pkt_pos);
147 /* Reserve space for the IP header that will be built later */
148 out_pkt->pkt.data_len = sizeof(struct ipv4_hdr);
149 out_pkt->pkt.pkt_len = sizeof(struct ipv4_hdr);
151 out_seg_prev = out_pkt;
153 while (likely(more_out_segs && more_in_segs)) {
154 struct rte_mbuf *out_seg = NULL;
157 /* Allocate indirect buffer */
158 out_seg = rte_pktmbuf_alloc(pool_indirect);
159 if (unlikely(out_seg == NULL)) {
160 rte_pktmbuf_free(out_pkt);
161 __free_fragments(pkts_out, out_pkt_pos);
164 out_seg_prev->pkt.next = out_seg;
165 out_seg_prev = out_seg;
167 /* Prepare indirect buffer */
168 rte_pktmbuf_attach(out_seg, in_seg);
169 len = mtu_size - out_pkt->pkt.pkt_len;
170 if (len > (in_seg->pkt.data_len - in_seg_data_pos)) {
171 len = in_seg->pkt.data_len - in_seg_data_pos;
173 out_seg->pkt.data = (char*) in_seg->pkt.data + (uint16_t)in_seg_data_pos;
174 out_seg->pkt.data_len = (uint16_t)len;
175 out_pkt->pkt.pkt_len = (uint16_t)(len +
176 out_pkt->pkt.pkt_len);
177 out_pkt->pkt.nb_segs += 1;
178 in_seg_data_pos += len;
180 /* Current output packet (i.e. fragment) done ? */
181 if (unlikely(out_pkt->pkt.pkt_len >= mtu_size))
184 /* Current input segment done ? */
185 if (unlikely(in_seg_data_pos == in_seg->pkt.data_len)) {
186 in_seg = in_seg->pkt.next;
189 if (unlikely(in_seg == NULL))
194 /* Build the IP header */
196 out_hdr = (struct ipv4_hdr*) out_pkt->pkt.data;
198 __fill_ipv4hdr_frag(out_hdr, in_hdr,
199 (uint16_t)out_pkt->pkt.pkt_len,
200 flag_offset, fragment_offset, more_in_segs);
202 fragment_offset = (uint16_t)(fragment_offset +
203 out_pkt->pkt.pkt_len - sizeof(struct ipv4_hdr));
205 out_pkt->ol_flags |= PKT_TX_IP_CKSUM;
206 out_pkt->pkt.vlan_macip.f.l3_len = sizeof(struct ipv4_hdr);
208 /* Write the fragment to the output list */
209 pkts_out[out_pkt_pos] = out_pkt;