net/virtio: fix incorrect cast of void *
[dpdk.git] / 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 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;
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 = 0;
120
121         more_in_segs = 1;
122         while (likely(more_in_segs)) {
123                 struct rte_mbuf *out_pkt = NULL, *out_seg_prev = NULL;
124                 uint32_t more_out_segs;
125                 struct rte_ipv4_hdr *out_hdr;
126
127                 /* Allocate direct buffer */
128                 out_pkt = rte_pktmbuf_alloc(pool_direct);
129                 if (unlikely(out_pkt == NULL)) {
130                         __free_fragments(pkts_out, out_pkt_pos);
131                         return -ENOMEM;
132                 }
133
134                 /* Reserve space for the IP header that will be built later */
135                 out_pkt->data_len = header_len;
136                 out_pkt->pkt_len = header_len;
137                 frag_bytes_remaining = frag_size;
138
139                 out_seg_prev = out_pkt;
140                 more_out_segs = 1;
141                 while (likely(more_out_segs && more_in_segs)) {
142                         struct rte_mbuf *out_seg = NULL;
143                         uint32_t len;
144
145                         /* Allocate indirect buffer */
146                         out_seg = rte_pktmbuf_alloc(pool_indirect);
147                         if (unlikely(out_seg == NULL)) {
148                                 rte_pktmbuf_free(out_pkt);
149                                 __free_fragments(pkts_out, out_pkt_pos);
150                                 return -ENOMEM;
151                         }
152                         out_seg_prev->next = out_seg;
153                         out_seg_prev = out_seg;
154
155                         /* Prepare indirect buffer */
156                         rte_pktmbuf_attach(out_seg, in_seg);
157                         len = frag_bytes_remaining;
158                         if (len > (in_seg->data_len - in_seg_data_pos)) {
159                                 len = in_seg->data_len - in_seg_data_pos;
160                         }
161                         out_seg->data_off = in_seg->data_off + in_seg_data_pos;
162                         out_seg->data_len = (uint16_t)len;
163                         out_pkt->pkt_len = (uint16_t)(len +
164                             out_pkt->pkt_len);
165                         out_pkt->nb_segs += 1;
166                         in_seg_data_pos += len;
167                         frag_bytes_remaining -= len;
168
169                         /* Current output packet (i.e. fragment) done ? */
170                         if (unlikely(frag_bytes_remaining == 0))
171                                 more_out_segs = 0;
172
173                         /* Current input segment done ? */
174                         if (unlikely(in_seg_data_pos == in_seg->data_len)) {
175                                 in_seg = in_seg->next;
176                                 in_seg_data_pos = 0;
177
178                                 if (unlikely(in_seg == NULL))
179                                         more_in_segs = 0;
180                         }
181                 }
182
183                 /* Build the IP header */
184
185                 out_hdr = rte_pktmbuf_mtod(out_pkt, struct rte_ipv4_hdr *);
186
187                 __fill_ipv4hdr_frag(out_hdr, in_hdr, header_len,
188                     (uint16_t)out_pkt->pkt_len,
189                     flag_offset, fragment_offset, more_in_segs);
190
191                 fragment_offset = (uint16_t)(fragment_offset +
192                     out_pkt->pkt_len - header_len);
193
194                 out_pkt->l3_len = header_len;
195
196                 /* Write the fragment to the output list */
197                 pkts_out[out_pkt_pos] = out_pkt;
198                 out_pkt_pos ++;
199         }
200
201         return out_pkt_pos;
202 }