test mbuf attach
[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 #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 len, uint16_t fofs,
27                 uint16_t dofs, uint32_t mf)
28 {
29         rte_memcpy(dst, src, sizeof(*dst));
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;
78         uint16_t frag_bytes_remaining;
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         /*
90          * Ensure the IP payload length of all fragments is aligned to a
91          * multiple of 8 bytes as per RFC791 section 2.3.
92          */
93         frag_size = RTE_ALIGN_FLOOR((mtu_size - sizeof(struct rte_ipv4_hdr)),
94                                     IPV4_HDR_FO_ALIGN);
95
96         in_hdr = rte_pktmbuf_mtod(pkt_in, struct rte_ipv4_hdr *);
97         flag_offset = rte_cpu_to_be_16(in_hdr->fragment_offset);
98
99         /* If Don't Fragment flag is set */
100         if (unlikely ((flag_offset & IPV4_HDR_DF_MASK) != 0))
101                 return -ENOTSUP;
102
103         /* Check that pkts_out is big enough to hold all fragments */
104         if (unlikely(frag_size * nb_pkts_out <
105             (uint16_t)(pkt_in->pkt_len - sizeof(struct rte_ipv4_hdr))))
106                 return -EINVAL;
107
108         in_seg = pkt_in;
109         in_seg_data_pos = sizeof(struct rte_ipv4_hdr);
110         out_pkt_pos = 0;
111         fragment_offset = 0;
112
113         more_in_segs = 1;
114         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_ipv4_hdr *out_hdr;
118
119                 /* Allocate direct buffer */
120                 out_pkt = rte_pktmbuf_alloc(pool_direct);
121                 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                 out_pkt->data_len = sizeof(struct rte_ipv4_hdr);
128                 out_pkt->pkt_len = sizeof(struct rte_ipv4_hdr);
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                 /* Build the IP header */
176
177                 out_hdr = rte_pktmbuf_mtod(out_pkt, struct rte_ipv4_hdr *);
178
179                 __fill_ipv4hdr_frag(out_hdr, in_hdr,
180                     (uint16_t)out_pkt->pkt_len,
181                     flag_offset, fragment_offset, more_in_segs);
182
183                 fragment_offset = (uint16_t)(fragment_offset +
184                     out_pkt->pkt_len - sizeof(struct rte_ipv4_hdr));
185
186                 out_pkt->l3_len = sizeof(struct rte_ipv4_hdr);
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 }