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