1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <rte_memcpy.h>
10 #include "ip_frag_common.h"
14 * RTE IPv6 Fragmentation
16 * Implementation of IPv6 fragmentation.
21 __fill_ipv6hdr_frag(struct rte_ipv6_hdr *dst,
22 const struct rte_ipv6_hdr *src, uint16_t len, uint16_t fofs,
25 struct ipv6_extension_fragment *fh;
27 rte_memcpy(dst, src, sizeof(*dst));
28 dst->payload_len = rte_cpu_to_be_16(len);
29 dst->proto = IPPROTO_FRAGMENT;
31 fh = (struct ipv6_extension_fragment *) ++dst;
32 fh->next_header = src->proto;
34 fh->frag_data = rte_cpu_to_be_16(RTE_IPV6_SET_FRAG_DATA(fofs, mf));
39 __free_fragments(struct rte_mbuf *mb[], uint32_t num)
42 for (i = 0; i < num; i++)
43 rte_pktmbuf_free(mb[i]);
49 * This function implements the fragmentation of IPv6 packets.
54 * Array storing the output fragments.
56 * Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv6
57 * datagrams. This value includes the size of the IPv6 header.
59 * MBUF pool used for allocating direct buffers for the output fragments.
60 * @param pool_indirect
61 * MBUF pool used for allocating indirect buffers for the output fragments.
63 * Upon successful completion - number of output fragments placed
64 * in the pkts_out array.
65 * Otherwise - (-1) * <errno>.
68 rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in,
69 struct rte_mbuf **pkts_out,
72 struct rte_mempool *pool_direct,
73 struct rte_mempool *pool_indirect)
75 struct rte_mbuf *in_seg = NULL;
76 struct rte_ipv6_hdr *in_hdr;
77 uint32_t out_pkt_pos, in_seg_data_pos;
78 uint32_t more_in_segs;
79 uint16_t fragment_offset, frag_size;
80 uint64_t frag_bytes_remaining;
83 * Ensure the IP payload length of all fragments (except the
84 * the last fragment) are a multiple of 8 bytes per RFC2460.
86 frag_size = RTE_ALIGN_FLOOR(mtu_size - sizeof(struct rte_ipv6_hdr),
87 RTE_IPV6_EHDR_FO_ALIGN);
89 /* Check that pkts_out is big enough to hold all fragments */
90 if (unlikely (frag_size * nb_pkts_out <
91 (uint16_t)(pkt_in->pkt_len - sizeof(struct rte_ipv6_hdr))))
94 in_hdr = rte_pktmbuf_mtod(pkt_in, struct rte_ipv6_hdr *);
97 in_seg_data_pos = sizeof(struct rte_ipv6_hdr);
102 while (likely(more_in_segs)) {
103 struct rte_mbuf *out_pkt = NULL, *out_seg_prev = NULL;
104 uint32_t more_out_segs;
105 struct rte_ipv6_hdr *out_hdr;
107 /* Allocate direct buffer */
108 out_pkt = rte_pktmbuf_alloc(pool_direct);
109 if (unlikely(out_pkt == NULL)) {
110 __free_fragments(pkts_out, out_pkt_pos);
114 /* Reserve space for the IP header that will be built later */
115 out_pkt->data_len = sizeof(struct rte_ipv6_hdr) +
116 sizeof(struct ipv6_extension_fragment);
117 out_pkt->pkt_len = sizeof(struct rte_ipv6_hdr) +
118 sizeof(struct ipv6_extension_fragment);
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)) {
166 /* Build the IP header */
168 out_hdr = rte_pktmbuf_mtod(out_pkt, struct rte_ipv6_hdr *);
170 __fill_ipv6hdr_frag(out_hdr, in_hdr,
171 (uint16_t) out_pkt->pkt_len - sizeof(struct rte_ipv6_hdr),
172 fragment_offset, more_in_segs);
174 fragment_offset = (uint16_t)(fragment_offset +
175 out_pkt->pkt_len - sizeof(struct rte_ipv6_hdr)
176 - sizeof(struct ipv6_extension_fragment));
178 /* Write the fragment to the output list */
179 pkts_out[out_pkt_pos] = out_pkt;