Branch data Line data Source code
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 <eal_export.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 : 6 : __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 : 6 : *dst = *src; 27 [ - + ]: 6 : dst->payload_len = rte_cpu_to_be_16(len); 28 : 6 : dst->proto = IPPROTO_FRAGMENT; 29 : : 30 : : fh = (struct rte_ipv6_fragment_ext *) ++dst; 31 : 6 : fh->next_header = src->proto; 32 : 6 : fh->reserved = 0; 33 [ - + ]: 6 : fh->frag_data = rte_cpu_to_be_16(RTE_IPV6_SET_FRAG_DATA(fofs, mf)); 34 : 6 : fh->id = 0; 35 : 6 : } 36 : : 37 : : static inline void 38 : : __free_fragments(struct rte_mbuf *mb[], uint32_t num) 39 : : { 40 : : uint32_t i; 41 [ # # # # ]: 0 : for (i = 0; i < num; i++) 42 : 0 : 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 : : RTE_EXPORT_SYMBOL(rte_ipv6_fragment_packet) 67 : : int32_t 68 : 4 : rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in, 69 : : struct rte_mbuf **pkts_out, 70 : : uint16_t nb_pkts_out, 71 : : uint16_t mtu_size, 72 : : struct rte_mempool *pool_direct, 73 : : struct rte_mempool *pool_indirect) 74 : : { 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; 81 : : 82 : : /* 83 : : * Formal parameter checking. 84 : : */ 85 [ + - + - ]: 4 : if (unlikely(pkt_in == NULL) || unlikely(pkts_out == NULL) || 86 [ + - ]: 4 : unlikely(nb_pkts_out == 0) || 87 [ + - + - ]: 4 : unlikely(pool_direct == NULL) || unlikely(pool_indirect == NULL) || 88 [ + + ]: 4 : unlikely(mtu_size < RTE_IPV6_MIN_MTU)) 89 : : return -EINVAL; 90 : : 91 : : /* 92 : : * Check that the first segment is big enough to hold the IPv6 93 : : * header; otherwise in_seg_data_pos starts past its end, the 94 : : * difference below underflows and the loop never terminates. 95 : : */ 96 [ + - ]: 3 : if (unlikely(pkt_in->data_len < sizeof(struct rte_ipv6_hdr))) 97 : : return -EINVAL; 98 : : 99 : : /* 100 : : * Ensure the IP payload length of all fragments (except the 101 : : * last fragment) are a multiple of 8 bytes per RFC2460. 102 : : */ 103 : : 104 : 3 : frag_size = mtu_size - sizeof(struct rte_ipv6_hdr) - 105 : : sizeof(struct rte_ipv6_fragment_ext); 106 : 3 : frag_size = RTE_ALIGN_FLOOR(frag_size, RTE_IPV6_EHDR_FO_ALIGN); 107 : : 108 : : /* Check that pkts_out is big enough to hold all fragments */ 109 [ + - ]: 3 : if (unlikely (frag_size * nb_pkts_out < 110 : : (uint16_t)(pkt_in->pkt_len - sizeof(struct rte_ipv6_hdr)))) 111 : : return -EINVAL; 112 : : 113 : 3 : in_hdr = rte_pktmbuf_mtod(pkt_in, struct rte_ipv6_hdr *); 114 : : 115 : : in_seg = pkt_in; 116 : : in_seg_data_pos = sizeof(struct rte_ipv6_hdr); 117 : : out_pkt_pos = 0; 118 : : fragment_offset = 0; 119 : : 120 : : more_in_segs = 1; 121 [ + + ]: 9 : while (likely(more_in_segs)) { 122 : : struct rte_mbuf *out_pkt = NULL, *out_seg_prev = NULL; 123 : : uint32_t more_out_segs; 124 : : struct rte_ipv6_hdr *out_hdr; 125 : : 126 : : /* Allocate direct buffer */ 127 : 6 : out_pkt = rte_pktmbuf_alloc(pool_direct); 128 [ - + ]: 6 : if (unlikely(out_pkt == NULL)) { 129 : : __free_fragments(pkts_out, out_pkt_pos); 130 : : return -ENOMEM; 131 : : } 132 : : 133 : : /* Reserve space for the IP header that will be built later */ 134 : 6 : out_pkt->data_len = sizeof(struct rte_ipv6_hdr) + 135 : : sizeof(struct rte_ipv6_fragment_ext); 136 : 6 : out_pkt->pkt_len = sizeof(struct rte_ipv6_hdr) + 137 : : sizeof(struct rte_ipv6_fragment_ext); 138 : 6 : frag_bytes_remaining = frag_size; 139 : : 140 : : out_seg_prev = out_pkt; 141 : : more_out_segs = 1; 142 [ + + ]: 12 : while (likely(more_out_segs && more_in_segs)) { 143 : : struct rte_mbuf *out_seg = NULL; 144 : : uint32_t len; 145 : : 146 : : /* Allocate indirect buffer */ 147 : 6 : out_seg = rte_pktmbuf_alloc(pool_indirect); 148 [ - + ]: 6 : if (unlikely(out_seg == NULL)) { 149 : 0 : rte_pktmbuf_free(out_pkt); 150 : : __free_fragments(pkts_out, out_pkt_pos); 151 : : return -ENOMEM; 152 : : } 153 : 6 : out_seg_prev->next = out_seg; 154 : : out_seg_prev = out_seg; 155 : : 156 : : /* Prepare indirect buffer */ 157 : 6 : rte_pktmbuf_attach(out_seg, in_seg); 158 : 6 : len = frag_bytes_remaining; 159 : 6 : if (len > (in_seg->data_len - in_seg_data_pos)) { 160 : : len = in_seg->data_len - in_seg_data_pos; 161 : : } 162 : 6 : out_seg->data_off = in_seg->data_off + in_seg_data_pos; 163 : 6 : out_seg->data_len = (uint16_t)len; 164 : 6 : out_pkt->pkt_len = (uint16_t)(len + 165 : 6 : out_pkt->pkt_len); 166 : 6 : out_pkt->nb_segs += 1; 167 : 6 : in_seg_data_pos += len; 168 : 6 : frag_bytes_remaining -= len; 169 : : 170 : : /* Current output packet (i.e. fragment) done ? */ 171 [ + + ]: 6 : if (unlikely(frag_bytes_remaining == 0)) 172 : : more_out_segs = 0; 173 : : 174 : : /* Current input segment done ? */ 175 [ + + ]: 6 : if (unlikely(in_seg_data_pos == in_seg->data_len)) { 176 : 3 : in_seg = in_seg->next; 177 : : in_seg_data_pos = 0; 178 : : 179 [ + - ]: 3 : if (unlikely(in_seg == NULL)) { 180 : : more_in_segs = 0; 181 : : } 182 : : } 183 : : } 184 : : 185 : : /* Build the IP header */ 186 : : 187 : 6 : out_hdr = rte_pktmbuf_mtod(out_pkt, struct rte_ipv6_hdr *); 188 : : 189 : 6 : __fill_ipv6hdr_frag(out_hdr, in_hdr, 190 : 6 : (uint16_t) out_pkt->pkt_len - sizeof(struct rte_ipv6_hdr), 191 : : fragment_offset, more_in_segs); 192 : : 193 : 6 : fragment_offset = (uint16_t)(fragment_offset + 194 : : out_pkt->pkt_len - sizeof(struct rte_ipv6_hdr) 195 : : - sizeof(struct rte_ipv6_fragment_ext)); 196 : : 197 : : /* Write the fragment to the output list */ 198 : 6 : pkts_out[out_pkt_pos] = out_pkt; 199 : 6 : out_pkt_pos ++; 200 : : } 201 : : 202 : 3 : return out_pkt_pos; 203 : : }