1*99a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause 2*99a2dd95SBruce Richardson * Copyright(c) 2010-2014 Intel Corporation 3*99a2dd95SBruce Richardson */ 4*99a2dd95SBruce Richardson 5*99a2dd95SBruce Richardson #include <stddef.h> 6*99a2dd95SBruce Richardson #include <errno.h> 7*99a2dd95SBruce Richardson 8*99a2dd95SBruce Richardson #include <rte_memcpy.h> 9*99a2dd95SBruce Richardson 10*99a2dd95SBruce Richardson #include "ip_frag_common.h" 11*99a2dd95SBruce Richardson 12*99a2dd95SBruce Richardson /** 13*99a2dd95SBruce Richardson * @file 14*99a2dd95SBruce Richardson * RTE IPv6 Fragmentation 15*99a2dd95SBruce Richardson * 16*99a2dd95SBruce Richardson * Implementation of IPv6 fragmentation. 17*99a2dd95SBruce Richardson * 18*99a2dd95SBruce Richardson */ 19*99a2dd95SBruce Richardson 20*99a2dd95SBruce Richardson static inline void 21*99a2dd95SBruce Richardson __fill_ipv6hdr_frag(struct rte_ipv6_hdr *dst, 22*99a2dd95SBruce Richardson const struct rte_ipv6_hdr *src, uint16_t len, uint16_t fofs, 23*99a2dd95SBruce Richardson uint32_t mf) 24*99a2dd95SBruce Richardson { 25*99a2dd95SBruce Richardson struct ipv6_extension_fragment *fh; 26*99a2dd95SBruce Richardson 27*99a2dd95SBruce Richardson rte_memcpy(dst, src, sizeof(*dst)); 28*99a2dd95SBruce Richardson dst->payload_len = rte_cpu_to_be_16(len); 29*99a2dd95SBruce Richardson dst->proto = IPPROTO_FRAGMENT; 30*99a2dd95SBruce Richardson 31*99a2dd95SBruce Richardson fh = (struct ipv6_extension_fragment *) ++dst; 32*99a2dd95SBruce Richardson fh->next_header = src->proto; 33*99a2dd95SBruce Richardson fh->reserved = 0; 34*99a2dd95SBruce Richardson fh->frag_data = rte_cpu_to_be_16(RTE_IPV6_SET_FRAG_DATA(fofs, mf)); 35*99a2dd95SBruce Richardson fh->id = 0; 36*99a2dd95SBruce Richardson } 37*99a2dd95SBruce Richardson 38*99a2dd95SBruce Richardson static inline void 39*99a2dd95SBruce Richardson __free_fragments(struct rte_mbuf *mb[], uint32_t num) 40*99a2dd95SBruce Richardson { 41*99a2dd95SBruce Richardson uint32_t i; 42*99a2dd95SBruce Richardson for (i = 0; i < num; i++) 43*99a2dd95SBruce Richardson rte_pktmbuf_free(mb[i]); 44*99a2dd95SBruce Richardson } 45*99a2dd95SBruce Richardson 46*99a2dd95SBruce Richardson /** 47*99a2dd95SBruce Richardson * IPv6 fragmentation. 48*99a2dd95SBruce Richardson * 49*99a2dd95SBruce Richardson * This function implements the fragmentation of IPv6 packets. 50*99a2dd95SBruce Richardson * 51*99a2dd95SBruce Richardson * @param pkt_in 52*99a2dd95SBruce Richardson * The input packet. 53*99a2dd95SBruce Richardson * @param pkts_out 54*99a2dd95SBruce Richardson * Array storing the output fragments. 55*99a2dd95SBruce Richardson * @param mtu_size 56*99a2dd95SBruce Richardson * Size in bytes of the Maximum Transfer Unit (MTU) for the outgoing IPv6 57*99a2dd95SBruce Richardson * datagrams. This value includes the size of the IPv6 header. 58*99a2dd95SBruce Richardson * @param pool_direct 59*99a2dd95SBruce Richardson * MBUF pool used for allocating direct buffers for the output fragments. 60*99a2dd95SBruce Richardson * @param pool_indirect 61*99a2dd95SBruce Richardson * MBUF pool used for allocating indirect buffers for the output fragments. 62*99a2dd95SBruce Richardson * @return 63*99a2dd95SBruce Richardson * Upon successful completion - number of output fragments placed 64*99a2dd95SBruce Richardson * in the pkts_out array. 65*99a2dd95SBruce Richardson * Otherwise - (-1) * <errno>. 66*99a2dd95SBruce Richardson */ 67*99a2dd95SBruce Richardson int32_t 68*99a2dd95SBruce Richardson rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in, 69*99a2dd95SBruce Richardson struct rte_mbuf **pkts_out, 70*99a2dd95SBruce Richardson uint16_t nb_pkts_out, 71*99a2dd95SBruce Richardson uint16_t mtu_size, 72*99a2dd95SBruce Richardson struct rte_mempool *pool_direct, 73*99a2dd95SBruce Richardson struct rte_mempool *pool_indirect) 74*99a2dd95SBruce Richardson { 75*99a2dd95SBruce Richardson struct rte_mbuf *in_seg = NULL; 76*99a2dd95SBruce Richardson struct rte_ipv6_hdr *in_hdr; 77*99a2dd95SBruce Richardson uint32_t out_pkt_pos, in_seg_data_pos; 78*99a2dd95SBruce Richardson uint32_t more_in_segs; 79*99a2dd95SBruce Richardson uint16_t fragment_offset, frag_size; 80*99a2dd95SBruce Richardson uint64_t frag_bytes_remaining; 81*99a2dd95SBruce Richardson 82*99a2dd95SBruce Richardson /* 83*99a2dd95SBruce Richardson * Formal parameter checking. 84*99a2dd95SBruce Richardson */ 85*99a2dd95SBruce Richardson if (unlikely(pkt_in == NULL) || unlikely(pkts_out == NULL) || 86*99a2dd95SBruce Richardson unlikely(nb_pkts_out == 0) || 87*99a2dd95SBruce Richardson unlikely(pool_direct == NULL) || unlikely(pool_indirect == NULL) || 88*99a2dd95SBruce Richardson unlikely(mtu_size < RTE_IPV6_MIN_MTU)) 89*99a2dd95SBruce Richardson return -EINVAL; 90*99a2dd95SBruce Richardson 91*99a2dd95SBruce Richardson /* 92*99a2dd95SBruce Richardson * Ensure the IP payload length of all fragments (except the 93*99a2dd95SBruce Richardson * the last fragment) are a multiple of 8 bytes per RFC2460. 94*99a2dd95SBruce Richardson */ 95*99a2dd95SBruce Richardson 96*99a2dd95SBruce Richardson frag_size = mtu_size - sizeof(struct rte_ipv6_hdr) - 97*99a2dd95SBruce Richardson sizeof(struct ipv6_extension_fragment); 98*99a2dd95SBruce Richardson frag_size = RTE_ALIGN_FLOOR(frag_size, RTE_IPV6_EHDR_FO_ALIGN); 99*99a2dd95SBruce Richardson 100*99a2dd95SBruce Richardson /* Check that pkts_out is big enough to hold all fragments */ 101*99a2dd95SBruce Richardson if (unlikely (frag_size * nb_pkts_out < 102*99a2dd95SBruce Richardson (uint16_t)(pkt_in->pkt_len - sizeof(struct rte_ipv6_hdr)))) 103*99a2dd95SBruce Richardson return -EINVAL; 104*99a2dd95SBruce Richardson 105*99a2dd95SBruce Richardson in_hdr = rte_pktmbuf_mtod(pkt_in, struct rte_ipv6_hdr *); 106*99a2dd95SBruce Richardson 107*99a2dd95SBruce Richardson in_seg = pkt_in; 108*99a2dd95SBruce Richardson in_seg_data_pos = sizeof(struct rte_ipv6_hdr); 109*99a2dd95SBruce Richardson out_pkt_pos = 0; 110*99a2dd95SBruce Richardson fragment_offset = 0; 111*99a2dd95SBruce Richardson 112*99a2dd95SBruce Richardson more_in_segs = 1; 113*99a2dd95SBruce Richardson while (likely(more_in_segs)) { 114*99a2dd95SBruce Richardson struct rte_mbuf *out_pkt = NULL, *out_seg_prev = NULL; 115*99a2dd95SBruce Richardson uint32_t more_out_segs; 116*99a2dd95SBruce Richardson struct rte_ipv6_hdr *out_hdr; 117*99a2dd95SBruce Richardson 118*99a2dd95SBruce Richardson /* Allocate direct buffer */ 119*99a2dd95SBruce Richardson out_pkt = rte_pktmbuf_alloc(pool_direct); 120*99a2dd95SBruce Richardson if (unlikely(out_pkt == NULL)) { 121*99a2dd95SBruce Richardson __free_fragments(pkts_out, out_pkt_pos); 122*99a2dd95SBruce Richardson return -ENOMEM; 123*99a2dd95SBruce Richardson } 124*99a2dd95SBruce Richardson 125*99a2dd95SBruce Richardson /* Reserve space for the IP header that will be built later */ 126*99a2dd95SBruce Richardson out_pkt->data_len = sizeof(struct rte_ipv6_hdr) + 127*99a2dd95SBruce Richardson sizeof(struct ipv6_extension_fragment); 128*99a2dd95SBruce Richardson out_pkt->pkt_len = sizeof(struct rte_ipv6_hdr) + 129*99a2dd95SBruce Richardson sizeof(struct ipv6_extension_fragment); 130*99a2dd95SBruce Richardson frag_bytes_remaining = frag_size; 131*99a2dd95SBruce Richardson 132*99a2dd95SBruce Richardson out_seg_prev = out_pkt; 133*99a2dd95SBruce Richardson more_out_segs = 1; 134*99a2dd95SBruce Richardson while (likely(more_out_segs && more_in_segs)) { 135*99a2dd95SBruce Richardson struct rte_mbuf *out_seg = NULL; 136*99a2dd95SBruce Richardson uint32_t len; 137*99a2dd95SBruce Richardson 138*99a2dd95SBruce Richardson /* Allocate indirect buffer */ 139*99a2dd95SBruce Richardson out_seg = rte_pktmbuf_alloc(pool_indirect); 140*99a2dd95SBruce Richardson if (unlikely(out_seg == NULL)) { 141*99a2dd95SBruce Richardson rte_pktmbuf_free(out_pkt); 142*99a2dd95SBruce Richardson __free_fragments(pkts_out, out_pkt_pos); 143*99a2dd95SBruce Richardson return -ENOMEM; 144*99a2dd95SBruce Richardson } 145*99a2dd95SBruce Richardson out_seg_prev->next = out_seg; 146*99a2dd95SBruce Richardson out_seg_prev = out_seg; 147*99a2dd95SBruce Richardson 148*99a2dd95SBruce Richardson /* Prepare indirect buffer */ 149*99a2dd95SBruce Richardson rte_pktmbuf_attach(out_seg, in_seg); 150*99a2dd95SBruce Richardson len = frag_bytes_remaining; 151*99a2dd95SBruce Richardson if (len > (in_seg->data_len - in_seg_data_pos)) { 152*99a2dd95SBruce Richardson len = in_seg->data_len - in_seg_data_pos; 153*99a2dd95SBruce Richardson } 154*99a2dd95SBruce Richardson out_seg->data_off = in_seg->data_off + in_seg_data_pos; 155*99a2dd95SBruce Richardson out_seg->data_len = (uint16_t)len; 156*99a2dd95SBruce Richardson out_pkt->pkt_len = (uint16_t)(len + 157*99a2dd95SBruce Richardson out_pkt->pkt_len); 158*99a2dd95SBruce Richardson out_pkt->nb_segs += 1; 159*99a2dd95SBruce Richardson in_seg_data_pos += len; 160*99a2dd95SBruce Richardson frag_bytes_remaining -= len; 161*99a2dd95SBruce Richardson 162*99a2dd95SBruce Richardson /* Current output packet (i.e. fragment) done ? */ 163*99a2dd95SBruce Richardson if (unlikely(frag_bytes_remaining == 0)) 164*99a2dd95SBruce Richardson more_out_segs = 0; 165*99a2dd95SBruce Richardson 166*99a2dd95SBruce Richardson /* Current input segment done ? */ 167*99a2dd95SBruce Richardson if (unlikely(in_seg_data_pos == in_seg->data_len)) { 168*99a2dd95SBruce Richardson in_seg = in_seg->next; 169*99a2dd95SBruce Richardson in_seg_data_pos = 0; 170*99a2dd95SBruce Richardson 171*99a2dd95SBruce Richardson if (unlikely(in_seg == NULL)) { 172*99a2dd95SBruce Richardson more_in_segs = 0; 173*99a2dd95SBruce Richardson } 174*99a2dd95SBruce Richardson } 175*99a2dd95SBruce Richardson } 176*99a2dd95SBruce Richardson 177*99a2dd95SBruce Richardson /* Build the IP header */ 178*99a2dd95SBruce Richardson 179*99a2dd95SBruce Richardson out_hdr = rte_pktmbuf_mtod(out_pkt, struct rte_ipv6_hdr *); 180*99a2dd95SBruce Richardson 181*99a2dd95SBruce Richardson __fill_ipv6hdr_frag(out_hdr, in_hdr, 182*99a2dd95SBruce Richardson (uint16_t) out_pkt->pkt_len - sizeof(struct rte_ipv6_hdr), 183*99a2dd95SBruce Richardson fragment_offset, more_in_segs); 184*99a2dd95SBruce Richardson 185*99a2dd95SBruce Richardson fragment_offset = (uint16_t)(fragment_offset + 186*99a2dd95SBruce Richardson out_pkt->pkt_len - sizeof(struct rte_ipv6_hdr) 187*99a2dd95SBruce Richardson - sizeof(struct ipv6_extension_fragment)); 188*99a2dd95SBruce Richardson 189*99a2dd95SBruce Richardson /* Write the fragment to the output list */ 190*99a2dd95SBruce Richardson pkts_out[out_pkt_pos] = out_pkt; 191*99a2dd95SBruce Richardson out_pkt_pos ++; 192*99a2dd95SBruce Richardson } 193*99a2dd95SBruce Richardson 194*99a2dd95SBruce Richardson return out_pkt_pos; 195*99a2dd95SBruce Richardson } 196