1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <rte_memcpy.h>
9 #include <rte_mempool.h>
10 #include <rte_debug.h>
11 #include <rte_ether.h>
13 #include "ip_frag_common.h"
16 #define RTE_IPV4_HDR_DF_SHIFT 14
17 #define RTE_IPV4_HDR_MF_SHIFT 13
18 #define RTE_IPV4_HDR_FO_SHIFT 3
20 #define IPV4_HDR_DF_MASK (1 << RTE_IPV4_HDR_DF_SHIFT)
21 #define IPV4_HDR_MF_MASK (1 << RTE_IPV4_HDR_MF_SHIFT)
23 #define IPV4_HDR_FO_ALIGN (1 << RTE_IPV4_HDR_FO_SHIFT)
25 static inline void __fill_ipv4hdr_frag(struct rte_ipv4_hdr *dst,
26 const struct rte_ipv4_hdr *src, uint16_t header_len,
27 uint16_t len, uint16_t fofs, uint16_t dofs, uint32_t mf)
29 rte_memcpy(dst, src, header_len);
30 fofs = (uint16_t)(fofs + (dofs >> RTE_IPV4_HDR_FO_SHIFT));
31 fofs = (uint16_t)(fofs | mf << RTE_IPV4_HDR_MF_SHIFT);
32 dst->fragment_offset = rte_cpu_to_be_16(fofs);
33 dst->total_length = rte_cpu_to_be_16(len);
34 dst->hdr_checksum = 0;
37 static inline void __free_fragments(struct rte_mbuf *mb[], uint32_t num)
40 for (i = 0; i != num; i++)
41 rte_pktmbuf_free(mb[i]);
47 * This function implements the fragmentation of IPv4 packets.
52 * Array storing the output fragments.
54 * Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv4
55 * datagrams. This value includes the size of the IPv4 header.
57 * MBUF pool used for allocating direct buffers for the output fragments.
58 * @param pool_indirect
59 * MBUF pool used for allocating indirect buffers for the output fragments.
61 * Upon successful completion - number of output fragments placed
62 * in the pkts_out array.
63 * Otherwise - (-1) * <errno>.
66 rte_ipv4_fragment_packet(struct rte_mbuf *pkt_in,
67 struct rte_mbuf **pkts_out,
70 struct rte_mempool *pool_direct,
71 struct rte_mempool *pool_indirect)
73 struct rte_mbuf *in_seg = NULL;
74 struct rte_ipv4_hdr *in_hdr;
75 uint32_t out_pkt_pos, in_seg_data_pos;
76 uint32_t more_in_segs;
77 uint16_t fragment_offset, flag_offset, frag_size, header_len;
78 uint16_t frag_bytes_remaining;
81 * Formal parameter checking.
83 if (unlikely(pkt_in == NULL) || unlikely(pkts_out == NULL) ||
84 unlikely(nb_pkts_out == 0) ||
85 unlikely(pool_direct == NULL) || unlikely(pool_indirect == NULL) ||
86 unlikely(mtu_size < RTE_ETHER_MIN_MTU))
89 in_hdr = rte_pktmbuf_mtod(pkt_in, struct rte_ipv4_hdr *);
90 header_len = (in_hdr->version_ihl & RTE_IPV4_HDR_IHL_MASK) *
91 RTE_IPV4_IHL_MULTIPLIER;
93 /* Check IP header length */
94 if (unlikely(pkt_in->data_len < header_len) ||
95 unlikely(mtu_size < header_len))
99 * Ensure the IP payload length of all fragments is aligned to a
100 * multiple of 8 bytes as per RFC791 section 2.3.
102 frag_size = RTE_ALIGN_FLOOR((mtu_size - header_len),
105 flag_offset = rte_cpu_to_be_16(in_hdr->fragment_offset);
107 /* If Don't Fragment flag is set */
108 if (unlikely ((flag_offset & IPV4_HDR_DF_MASK) != 0))
111 /* Check that pkts_out is big enough to hold all fragments */
112 if (unlikely(frag_size * nb_pkts_out <
113 (uint16_t)(pkt_in->pkt_len - header_len)))
117 in_seg_data_pos = header_len;
122 while (likely(more_in_segs)) {
123 struct rte_mbuf *out_pkt = NULL, *out_seg_prev = NULL;
124 uint32_t more_out_segs;
125 struct rte_ipv4_hdr *out_hdr;
127 /* Allocate direct buffer */
128 out_pkt = rte_pktmbuf_alloc(pool_direct);
129 if (unlikely(out_pkt == NULL)) {
130 __free_fragments(pkts_out, out_pkt_pos);
134 /* Reserve space for the IP header that will be built later */
135 out_pkt->data_len = header_len;
136 out_pkt->pkt_len = header_len;
137 frag_bytes_remaining = frag_size;
139 out_seg_prev = out_pkt;
141 while (likely(more_out_segs && more_in_segs)) {
142 struct rte_mbuf *out_seg = NULL;
145 /* Allocate indirect buffer */
146 out_seg = rte_pktmbuf_alloc(pool_indirect);
147 if (unlikely(out_seg == NULL)) {
148 rte_pktmbuf_free(out_pkt);
149 __free_fragments(pkts_out, out_pkt_pos);
152 out_seg_prev->next = out_seg;
153 out_seg_prev = out_seg;
155 /* Prepare indirect buffer */
156 rte_pktmbuf_attach(out_seg, in_seg);
157 len = frag_bytes_remaining;
158 if (len > (in_seg->data_len - in_seg_data_pos)) {
159 len = in_seg->data_len - in_seg_data_pos;
161 out_seg->data_off = in_seg->data_off + in_seg_data_pos;
162 out_seg->data_len = (uint16_t)len;
163 out_pkt->pkt_len = (uint16_t)(len +
165 out_pkt->nb_segs += 1;
166 in_seg_data_pos += len;
167 frag_bytes_remaining -= len;
169 /* Current output packet (i.e. fragment) done ? */
170 if (unlikely(frag_bytes_remaining == 0))
173 /* Current input segment done ? */
174 if (unlikely(in_seg_data_pos == in_seg->data_len)) {
175 in_seg = in_seg->next;
178 if (unlikely(in_seg == NULL))
183 /* Build the IP header */
185 out_hdr = rte_pktmbuf_mtod(out_pkt, struct rte_ipv4_hdr *);
187 __fill_ipv4hdr_frag(out_hdr, in_hdr, header_len,
188 (uint16_t)out_pkt->pkt_len,
189 flag_offset, fragment_offset, more_in_segs);
191 fragment_offset = (uint16_t)(fragment_offset +
192 out_pkt->pkt_len - header_len);
194 out_pkt->l3_len = header_len;
196 /* Write the fragment to the output list */
197 pkts_out[out_pkt_pos] = out_pkt;