199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation
399a2dd95SBruce Richardson */
499a2dd95SBruce Richardson
599a2dd95SBruce Richardson #include <stddef.h>
699a2dd95SBruce Richardson #include <errno.h>
799a2dd95SBruce Richardson
899a2dd95SBruce Richardson #include <rte_memcpy.h>
999a2dd95SBruce Richardson
1099a2dd95SBruce Richardson #include "ip_frag_common.h"
1199a2dd95SBruce Richardson
1299a2dd95SBruce Richardson /**
1399a2dd95SBruce Richardson * @file
1499a2dd95SBruce Richardson * RTE IPv6 Fragmentation
1599a2dd95SBruce Richardson *
1699a2dd95SBruce Richardson * Implementation of IPv6 fragmentation.
1799a2dd95SBruce Richardson */
1899a2dd95SBruce Richardson
1999a2dd95SBruce Richardson static inline void
__fill_ipv6hdr_frag(struct rte_ipv6_hdr * dst,const struct rte_ipv6_hdr * src,uint16_t len,uint16_t fofs,uint32_t mf)2099a2dd95SBruce Richardson __fill_ipv6hdr_frag(struct rte_ipv6_hdr *dst,
2199a2dd95SBruce Richardson const struct rte_ipv6_hdr *src, uint16_t len, uint16_t fofs,
2299a2dd95SBruce Richardson uint32_t mf)
2399a2dd95SBruce Richardson {
24b7fc82ecSKonstantin Ananyev struct rte_ipv6_fragment_ext *fh;
2599a2dd95SBruce Richardson
2699a2dd95SBruce Richardson rte_memcpy(dst, src, sizeof(*dst));
2799a2dd95SBruce Richardson dst->payload_len = rte_cpu_to_be_16(len);
2899a2dd95SBruce Richardson dst->proto = IPPROTO_FRAGMENT;
2999a2dd95SBruce Richardson
30b7fc82ecSKonstantin Ananyev fh = (struct rte_ipv6_fragment_ext *) ++dst;
3199a2dd95SBruce Richardson fh->next_header = src->proto;
3299a2dd95SBruce Richardson fh->reserved = 0;
3399a2dd95SBruce Richardson fh->frag_data = rte_cpu_to_be_16(RTE_IPV6_SET_FRAG_DATA(fofs, mf));
3499a2dd95SBruce Richardson fh->id = 0;
3599a2dd95SBruce Richardson }
3699a2dd95SBruce Richardson
3799a2dd95SBruce Richardson static inline void
__free_fragments(struct rte_mbuf * mb[],uint32_t num)3899a2dd95SBruce Richardson __free_fragments(struct rte_mbuf *mb[], uint32_t num)
3999a2dd95SBruce Richardson {
4099a2dd95SBruce Richardson uint32_t i;
4199a2dd95SBruce Richardson for (i = 0; i < num; i++)
4299a2dd95SBruce Richardson rte_pktmbuf_free(mb[i]);
4399a2dd95SBruce Richardson }
4499a2dd95SBruce Richardson
4599a2dd95SBruce Richardson /**
4699a2dd95SBruce Richardson * IPv6 fragmentation.
4799a2dd95SBruce Richardson *
4899a2dd95SBruce Richardson * This function implements the fragmentation of IPv6 packets.
4999a2dd95SBruce Richardson *
5099a2dd95SBruce Richardson * @param pkt_in
5199a2dd95SBruce Richardson * The input packet.
5299a2dd95SBruce Richardson * @param pkts_out
5399a2dd95SBruce Richardson * Array storing the output fragments.
5499a2dd95SBruce Richardson * @param mtu_size
5599a2dd95SBruce Richardson * Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv6
5699a2dd95SBruce Richardson * datagrams. This value includes the size of the IPv6 header.
5799a2dd95SBruce Richardson * @param pool_direct
5899a2dd95SBruce Richardson * MBUF pool used for allocating direct buffers for the output fragments.
5999a2dd95SBruce Richardson * @param pool_indirect
6099a2dd95SBruce Richardson * MBUF pool used for allocating indirect buffers for the output fragments.
6199a2dd95SBruce Richardson * @return
6299a2dd95SBruce Richardson * Upon successful completion - number of output fragments placed
6399a2dd95SBruce Richardson * in the pkts_out array.
6499a2dd95SBruce Richardson * Otherwise - (-1) * <errno>.
6599a2dd95SBruce Richardson */
6699a2dd95SBruce Richardson int32_t
rte_ipv6_fragment_packet(struct rte_mbuf * pkt_in,struct rte_mbuf ** pkts_out,uint16_t nb_pkts_out,uint16_t mtu_size,struct rte_mempool * pool_direct,struct rte_mempool * pool_indirect)6799a2dd95SBruce Richardson rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in,
6899a2dd95SBruce Richardson struct rte_mbuf **pkts_out,
6999a2dd95SBruce Richardson uint16_t nb_pkts_out,
7099a2dd95SBruce Richardson uint16_t mtu_size,
7199a2dd95SBruce Richardson struct rte_mempool *pool_direct,
7299a2dd95SBruce Richardson struct rte_mempool *pool_indirect)
7399a2dd95SBruce Richardson {
7499a2dd95SBruce Richardson struct rte_mbuf *in_seg = NULL;
7599a2dd95SBruce Richardson struct rte_ipv6_hdr *in_hdr;
7699a2dd95SBruce Richardson uint32_t out_pkt_pos, in_seg_data_pos;
7799a2dd95SBruce Richardson uint32_t more_in_segs;
7899a2dd95SBruce Richardson uint16_t fragment_offset, frag_size;
7999a2dd95SBruce Richardson uint64_t frag_bytes_remaining;
8099a2dd95SBruce Richardson
8199a2dd95SBruce Richardson /*
8299a2dd95SBruce Richardson * Formal parameter checking.
8399a2dd95SBruce Richardson */
8499a2dd95SBruce Richardson if (unlikely(pkt_in == NULL) || unlikely(pkts_out == NULL) ||
8599a2dd95SBruce Richardson unlikely(nb_pkts_out == 0) ||
8699a2dd95SBruce Richardson unlikely(pool_direct == NULL) || unlikely(pool_indirect == NULL) ||
8799a2dd95SBruce Richardson unlikely(mtu_size < RTE_IPV6_MIN_MTU))
8899a2dd95SBruce Richardson return -EINVAL;
8999a2dd95SBruce Richardson
9099a2dd95SBruce Richardson /*
9199a2dd95SBruce Richardson * Ensure the IP payload length of all fragments (except the
92*b53d106dSSean Morrissey * last fragment) are a multiple of 8 bytes per RFC2460.
9399a2dd95SBruce Richardson */
9499a2dd95SBruce Richardson
9599a2dd95SBruce Richardson frag_size = mtu_size - sizeof(struct rte_ipv6_hdr) -
96b7fc82ecSKonstantin Ananyev sizeof(struct rte_ipv6_fragment_ext);
9799a2dd95SBruce Richardson frag_size = RTE_ALIGN_FLOOR(frag_size, RTE_IPV6_EHDR_FO_ALIGN);
9899a2dd95SBruce Richardson
9999a2dd95SBruce Richardson /* Check that pkts_out is big enough to hold all fragments */
10099a2dd95SBruce Richardson if (unlikely (frag_size * nb_pkts_out <
10199a2dd95SBruce Richardson (uint16_t)(pkt_in->pkt_len - sizeof(struct rte_ipv6_hdr))))
10299a2dd95SBruce Richardson return -EINVAL;
10399a2dd95SBruce Richardson
10499a2dd95SBruce Richardson in_hdr = rte_pktmbuf_mtod(pkt_in, struct rte_ipv6_hdr *);
10599a2dd95SBruce Richardson
10699a2dd95SBruce Richardson in_seg = pkt_in;
10799a2dd95SBruce Richardson in_seg_data_pos = sizeof(struct rte_ipv6_hdr);
10899a2dd95SBruce Richardson out_pkt_pos = 0;
10999a2dd95SBruce Richardson fragment_offset = 0;
11099a2dd95SBruce Richardson
11199a2dd95SBruce Richardson more_in_segs = 1;
11299a2dd95SBruce Richardson while (likely(more_in_segs)) {
11399a2dd95SBruce Richardson struct rte_mbuf *out_pkt = NULL, *out_seg_prev = NULL;
11499a2dd95SBruce Richardson uint32_t more_out_segs;
11599a2dd95SBruce Richardson struct rte_ipv6_hdr *out_hdr;
11699a2dd95SBruce Richardson
11799a2dd95SBruce Richardson /* Allocate direct buffer */
11899a2dd95SBruce Richardson out_pkt = rte_pktmbuf_alloc(pool_direct);
11999a2dd95SBruce Richardson if (unlikely(out_pkt == NULL)) {
12099a2dd95SBruce Richardson __free_fragments(pkts_out, out_pkt_pos);
12199a2dd95SBruce Richardson return -ENOMEM;
12299a2dd95SBruce Richardson }
12399a2dd95SBruce Richardson
12499a2dd95SBruce Richardson /* Reserve space for the IP header that will be built later */
12599a2dd95SBruce Richardson out_pkt->data_len = sizeof(struct rte_ipv6_hdr) +
126b7fc82ecSKonstantin Ananyev sizeof(struct rte_ipv6_fragment_ext);
12799a2dd95SBruce Richardson out_pkt->pkt_len = sizeof(struct rte_ipv6_hdr) +
128b7fc82ecSKonstantin Ananyev sizeof(struct rte_ipv6_fragment_ext);
12999a2dd95SBruce Richardson frag_bytes_remaining = frag_size;
13099a2dd95SBruce Richardson
13199a2dd95SBruce Richardson out_seg_prev = out_pkt;
13299a2dd95SBruce Richardson more_out_segs = 1;
13399a2dd95SBruce Richardson while (likely(more_out_segs && more_in_segs)) {
13499a2dd95SBruce Richardson struct rte_mbuf *out_seg = NULL;
13599a2dd95SBruce Richardson uint32_t len;
13699a2dd95SBruce Richardson
13799a2dd95SBruce Richardson /* Allocate indirect buffer */
13899a2dd95SBruce Richardson out_seg = rte_pktmbuf_alloc(pool_indirect);
13999a2dd95SBruce Richardson if (unlikely(out_seg == NULL)) {
14099a2dd95SBruce Richardson rte_pktmbuf_free(out_pkt);
14199a2dd95SBruce Richardson __free_fragments(pkts_out, out_pkt_pos);
14299a2dd95SBruce Richardson return -ENOMEM;
14399a2dd95SBruce Richardson }
14499a2dd95SBruce Richardson out_seg_prev->next = out_seg;
14599a2dd95SBruce Richardson out_seg_prev = out_seg;
14699a2dd95SBruce Richardson
14799a2dd95SBruce Richardson /* Prepare indirect buffer */
14899a2dd95SBruce Richardson rte_pktmbuf_attach(out_seg, in_seg);
14999a2dd95SBruce Richardson len = frag_bytes_remaining;
15099a2dd95SBruce Richardson if (len > (in_seg->data_len - in_seg_data_pos)) {
15199a2dd95SBruce Richardson len = in_seg->data_len - in_seg_data_pos;
15299a2dd95SBruce Richardson }
15399a2dd95SBruce Richardson out_seg->data_off = in_seg->data_off + in_seg_data_pos;
15499a2dd95SBruce Richardson out_seg->data_len = (uint16_t)len;
15599a2dd95SBruce Richardson out_pkt->pkt_len = (uint16_t)(len +
15699a2dd95SBruce Richardson out_pkt->pkt_len);
15799a2dd95SBruce Richardson out_pkt->nb_segs += 1;
15899a2dd95SBruce Richardson in_seg_data_pos += len;
15999a2dd95SBruce Richardson frag_bytes_remaining -= len;
16099a2dd95SBruce Richardson
16199a2dd95SBruce Richardson /* Current output packet (i.e. fragment) done ? */
16299a2dd95SBruce Richardson if (unlikely(frag_bytes_remaining == 0))
16399a2dd95SBruce Richardson more_out_segs = 0;
16499a2dd95SBruce Richardson
16599a2dd95SBruce Richardson /* Current input segment done ? */
16699a2dd95SBruce Richardson if (unlikely(in_seg_data_pos == in_seg->data_len)) {
16799a2dd95SBruce Richardson in_seg = in_seg->next;
16899a2dd95SBruce Richardson in_seg_data_pos = 0;
16999a2dd95SBruce Richardson
17099a2dd95SBruce Richardson if (unlikely(in_seg == NULL)) {
17199a2dd95SBruce Richardson more_in_segs = 0;
17299a2dd95SBruce Richardson }
17399a2dd95SBruce Richardson }
17499a2dd95SBruce Richardson }
17599a2dd95SBruce Richardson
17699a2dd95SBruce Richardson /* Build the IP header */
17799a2dd95SBruce Richardson
17899a2dd95SBruce Richardson out_hdr = rte_pktmbuf_mtod(out_pkt, struct rte_ipv6_hdr *);
17999a2dd95SBruce Richardson
18099a2dd95SBruce Richardson __fill_ipv6hdr_frag(out_hdr, in_hdr,
18199a2dd95SBruce Richardson (uint16_t) out_pkt->pkt_len - sizeof(struct rte_ipv6_hdr),
18299a2dd95SBruce Richardson fragment_offset, more_in_segs);
18399a2dd95SBruce Richardson
18499a2dd95SBruce Richardson fragment_offset = (uint16_t)(fragment_offset +
18599a2dd95SBruce Richardson out_pkt->pkt_len - sizeof(struct rte_ipv6_hdr)
186b7fc82ecSKonstantin Ananyev - sizeof(struct rte_ipv6_fragment_ext));
18799a2dd95SBruce Richardson
18899a2dd95SBruce Richardson /* Write the fragment to the output list */
18999a2dd95SBruce Richardson pkts_out[out_pkt_pos] = out_pkt;
19099a2dd95SBruce Richardson out_pkt_pos ++;
19199a2dd95SBruce Richardson }
19299a2dd95SBruce Richardson
19399a2dd95SBruce Richardson return out_pkt_pos;
19499a2dd95SBruce Richardson }
195