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