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