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