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 len, uint16_t fofs,
27 uint16_t dofs, uint32_t mf)
29 rte_memcpy(dst, src, sizeof(*dst));
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;
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))
90 * Ensure the IP payload length of all fragments is aligned to a
91 * multiple of 8 bytes as per RFC791 section 2.3.
93 frag_size = RTE_ALIGN_FLOOR((mtu_size - sizeof(struct rte_ipv4_hdr)),
96 in_hdr = rte_pktmbuf_mtod(pkt_in, struct rte_ipv4_hdr *);
97 flag_offset = rte_cpu_to_be_16(in_hdr->fragment_offset);
99 /* If Don't Fragment flag is set */
100 if (unlikely ((flag_offset & IPV4_HDR_DF_MASK) != 0))
103 /* Check that pkts_out is big enough to hold all fragments */
104 if (unlikely(frag_size * nb_pkts_out <
105 (uint16_t)(pkt_in->pkt_len - sizeof(struct rte_ipv4_hdr))))
109 in_seg_data_pos = sizeof(struct rte_ipv4_hdr);
114 while (likely(more_in_segs)) {
115 struct rte_mbuf *out_pkt = NULL, *out_seg_prev = NULL;
116 uint32_t more_out_segs;
117 struct rte_ipv4_hdr *out_hdr;
119 /* Allocate direct buffer */
120 out_pkt = rte_pktmbuf_alloc(pool_direct);
121 if (unlikely(out_pkt == NULL)) {
122 __free_fragments(pkts_out, out_pkt_pos);
126 /* Reserve space for the IP header that will be built later */
127 out_pkt->data_len = sizeof(struct rte_ipv4_hdr);
128 out_pkt->pkt_len = sizeof(struct rte_ipv4_hdr);
129 frag_bytes_remaining = frag_size;
131 out_seg_prev = out_pkt;
133 while (likely(more_out_segs && more_in_segs)) {
134 struct rte_mbuf *out_seg = NULL;
137 /* Allocate indirect buffer */
138 out_seg = rte_pktmbuf_alloc(pool_indirect);
139 if (unlikely(out_seg == NULL)) {
140 rte_pktmbuf_free(out_pkt);
141 __free_fragments(pkts_out, out_pkt_pos);
144 out_seg_prev->next = out_seg;
145 out_seg_prev = out_seg;
147 /* Prepare indirect buffer */
148 rte_pktmbuf_attach(out_seg, in_seg);
149 len = frag_bytes_remaining;
150 if (len > (in_seg->data_len - in_seg_data_pos)) {
151 len = in_seg->data_len - in_seg_data_pos;
153 out_seg->data_off = in_seg->data_off + in_seg_data_pos;
154 out_seg->data_len = (uint16_t)len;
155 out_pkt->pkt_len = (uint16_t)(len +
157 out_pkt->nb_segs += 1;
158 in_seg_data_pos += len;
159 frag_bytes_remaining -= len;
161 /* Current output packet (i.e. fragment) done ? */
162 if (unlikely(frag_bytes_remaining == 0))
165 /* Current input segment done ? */
166 if (unlikely(in_seg_data_pos == in_seg->data_len)) {
167 in_seg = in_seg->next;
170 if (unlikely(in_seg == NULL))
175 /* Build the IP header */
177 out_hdr = rte_pktmbuf_mtod(out_pkt, struct rte_ipv4_hdr *);
179 __fill_ipv4hdr_frag(out_hdr, in_hdr,
180 (uint16_t)out_pkt->pkt_len,
181 flag_offset, fragment_offset, more_in_segs);
183 fragment_offset = (uint16_t)(fragment_offset +
184 out_pkt->pkt_len - sizeof(struct rte_ipv4_hdr));
186 out_pkt->l3_len = sizeof(struct rte_ipv4_hdr);
188 /* Write the fragment to the output list */
189 pkts_out[out_pkt_pos] = out_pkt;