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>
12 #include "ip_frag_common.h"
15 #define RTE_IPV4_HDR_DF_SHIFT 14
16 #define RTE_IPV4_HDR_MF_SHIFT 13
17 #define RTE_IPV4_HDR_FO_SHIFT 3
19 #define IPV4_HDR_DF_MASK (1 << RTE_IPV4_HDR_DF_SHIFT)
20 #define IPV4_HDR_MF_MASK (1 << RTE_IPV4_HDR_MF_SHIFT)
22 #define IPV4_HDR_FO_ALIGN (1 << RTE_IPV4_HDR_FO_SHIFT)
24 static inline void __fill_ipv4hdr_frag(struct rte_ipv4_hdr *dst,
25 const struct rte_ipv4_hdr *src, uint16_t len, uint16_t fofs,
26 uint16_t dofs, uint32_t mf)
28 rte_memcpy(dst, src, sizeof(*dst));
29 fofs = (uint16_t)(fofs + (dofs >> RTE_IPV4_HDR_FO_SHIFT));
30 fofs = (uint16_t)(fofs | mf << RTE_IPV4_HDR_MF_SHIFT);
31 dst->fragment_offset = rte_cpu_to_be_16(fofs);
32 dst->total_length = rte_cpu_to_be_16(len);
33 dst->hdr_checksum = 0;
36 static inline void __free_fragments(struct rte_mbuf *mb[], uint32_t num)
39 for (i = 0; i != num; i++)
40 rte_pktmbuf_free(mb[i]);
46 * This function implements the fragmentation of IPv4 packets.
51 * Array storing the output fragments.
53 * Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv4
54 * datagrams. This value includes the size of the IPv4 header.
56 * MBUF pool used for allocating direct buffers for the output fragments.
57 * @param pool_indirect
58 * MBUF pool used for allocating indirect buffers for the output fragments.
60 * Upon successful completion - number of output fragments placed
61 * in the pkts_out array.
62 * Otherwise - (-1) * <errno>.
65 rte_ipv4_fragment_packet(struct rte_mbuf *pkt_in,
66 struct rte_mbuf **pkts_out,
69 struct rte_mempool *pool_direct,
70 struct rte_mempool *pool_indirect)
72 struct rte_mbuf *in_seg = NULL;
73 struct rte_ipv4_hdr *in_hdr;
74 uint32_t out_pkt_pos, in_seg_data_pos;
75 uint32_t more_in_segs;
76 uint16_t fragment_offset, flag_offset, frag_size;
77 uint16_t frag_bytes_remaining;
80 * Ensure the IP payload length of all fragments is aligned to a
81 * multiple of 8 bytes as per RFC791 section 2.3.
83 frag_size = RTE_ALIGN_FLOOR((mtu_size - sizeof(struct rte_ipv4_hdr)),
86 in_hdr = rte_pktmbuf_mtod(pkt_in, struct rte_ipv4_hdr *);
87 flag_offset = rte_cpu_to_be_16(in_hdr->fragment_offset);
89 /* If Don't Fragment flag is set */
90 if (unlikely ((flag_offset & IPV4_HDR_DF_MASK) != 0))
93 /* Check that pkts_out is big enough to hold all fragments */
94 if (unlikely(frag_size * nb_pkts_out <
95 (uint16_t)(pkt_in->pkt_len - sizeof(struct rte_ipv4_hdr))))
99 in_seg_data_pos = sizeof(struct rte_ipv4_hdr);
104 while (likely(more_in_segs)) {
105 struct rte_mbuf *out_pkt = NULL, *out_seg_prev = NULL;
106 uint32_t more_out_segs;
107 struct rte_ipv4_hdr *out_hdr;
109 /* Allocate direct buffer */
110 out_pkt = rte_pktmbuf_alloc(pool_direct);
111 if (unlikely(out_pkt == NULL)) {
112 __free_fragments(pkts_out, out_pkt_pos);
116 /* Reserve space for the IP header that will be built later */
117 out_pkt->data_len = sizeof(struct rte_ipv4_hdr);
118 out_pkt->pkt_len = sizeof(struct rte_ipv4_hdr);
119 frag_bytes_remaining = frag_size;
121 out_seg_prev = out_pkt;
123 while (likely(more_out_segs && more_in_segs)) {
124 struct rte_mbuf *out_seg = NULL;
127 /* Allocate indirect buffer */
128 out_seg = rte_pktmbuf_alloc(pool_indirect);
129 if (unlikely(out_seg == NULL)) {
130 rte_pktmbuf_free(out_pkt);
131 __free_fragments(pkts_out, out_pkt_pos);
134 out_seg_prev->next = out_seg;
135 out_seg_prev = out_seg;
137 /* Prepare indirect buffer */
138 rte_pktmbuf_attach(out_seg, in_seg);
139 len = frag_bytes_remaining;
140 if (len > (in_seg->data_len - in_seg_data_pos)) {
141 len = in_seg->data_len - in_seg_data_pos;
143 out_seg->data_off = in_seg->data_off + in_seg_data_pos;
144 out_seg->data_len = (uint16_t)len;
145 out_pkt->pkt_len = (uint16_t)(len +
147 out_pkt->nb_segs += 1;
148 in_seg_data_pos += len;
149 frag_bytes_remaining -= len;
151 /* Current output packet (i.e. fragment) done ? */
152 if (unlikely(frag_bytes_remaining == 0))
155 /* Current input segment done ? */
156 if (unlikely(in_seg_data_pos == in_seg->data_len)) {
157 in_seg = in_seg->next;
160 if (unlikely(in_seg == NULL))
165 /* Build the IP header */
167 out_hdr = rte_pktmbuf_mtod(out_pkt, struct rte_ipv4_hdr *);
169 __fill_ipv4hdr_frag(out_hdr, in_hdr,
170 (uint16_t)out_pkt->pkt_len,
171 flag_offset, fragment_offset, more_in_segs);
173 fragment_offset = (uint16_t)(fragment_offset +
174 out_pkt->pkt_len - sizeof(struct rte_ipv4_hdr));
176 out_pkt->ol_flags |= PKT_TX_IP_CKSUM;
177 out_pkt->l3_len = sizeof(struct rte_ipv4_hdr);
179 /* Write the fragment to the output list */
180 pkts_out[out_pkt_pos] = out_pkt;