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