]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/dpdk/lib/librte_ip_frag/rte_ipv6_fragmentation.c
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / spdk / dpdk / lib / librte_ip_frag / rte_ipv6_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
10 #include "ip_frag_common.h"
11
12 /**
13 * @file
14 * RTE IPv6 Fragmentation
15 *
16 * Implementation of IPv6 fragmentation.
17 *
18 */
19
20 static inline void
21 __fill_ipv6hdr_frag(struct ipv6_hdr *dst,
22 const struct ipv6_hdr *src, uint16_t len, uint16_t fofs,
23 uint32_t mf)
24 {
25 struct ipv6_extension_fragment *fh;
26
27 rte_memcpy(dst, src, sizeof(*dst));
28 dst->payload_len = rte_cpu_to_be_16(len);
29 dst->proto = IPPROTO_FRAGMENT;
30
31 fh = (struct ipv6_extension_fragment *) ++dst;
32 fh->next_header = src->proto;
33 fh->reserved = 0;
34 fh->frag_data = rte_cpu_to_be_16(RTE_IPV6_SET_FRAG_DATA(fofs, mf));
35 fh->id = 0;
36 }
37
38 static inline void
39 __free_fragments(struct rte_mbuf *mb[], uint32_t num)
40 {
41 uint32_t i;
42 for (i = 0; i < num; i++)
43 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 int32_t
68 rte_ipv6_fragment_packet(struct rte_mbuf *pkt_in,
69 struct rte_mbuf **pkts_out,
70 uint16_t nb_pkts_out,
71 uint16_t mtu_size,
72 struct rte_mempool *pool_direct,
73 struct rte_mempool *pool_indirect)
74 {
75 struct rte_mbuf *in_seg = NULL;
76 struct ipv6_hdr *in_hdr;
77 uint32_t out_pkt_pos, in_seg_data_pos;
78 uint32_t more_in_segs;
79 uint16_t fragment_offset, frag_size;
80
81 frag_size = (uint16_t)(mtu_size - sizeof(struct ipv6_hdr));
82
83 /* Fragment size should be a multiple of 8. */
84 RTE_ASSERT((frag_size & ~RTE_IPV6_EHDR_FO_MASK) == 0);
85
86 /* Check that pkts_out is big enough to hold all fragments */
87 if (unlikely (frag_size * nb_pkts_out <
88 (uint16_t)(pkt_in->pkt_len - sizeof (struct ipv6_hdr))))
89 return -EINVAL;
90
91 in_hdr = rte_pktmbuf_mtod(pkt_in, struct ipv6_hdr *);
92
93 in_seg = pkt_in;
94 in_seg_data_pos = sizeof(struct ipv6_hdr);
95 out_pkt_pos = 0;
96 fragment_offset = 0;
97
98 more_in_segs = 1;
99 while (likely(more_in_segs)) {
100 struct rte_mbuf *out_pkt = NULL, *out_seg_prev = NULL;
101 uint32_t more_out_segs;
102 struct ipv6_hdr *out_hdr;
103
104 /* Allocate direct buffer */
105 out_pkt = rte_pktmbuf_alloc(pool_direct);
106 if (unlikely(out_pkt == NULL)) {
107 __free_fragments(pkts_out, out_pkt_pos);
108 return -ENOMEM;
109 }
110
111 /* Reserve space for the IP header that will be built later */
112 out_pkt->data_len = sizeof(struct ipv6_hdr) + sizeof(struct ipv6_extension_fragment);
113 out_pkt->pkt_len = sizeof(struct ipv6_hdr) + sizeof(struct ipv6_extension_fragment);
114
115 out_seg_prev = out_pkt;
116 more_out_segs = 1;
117 while (likely(more_out_segs && more_in_segs)) {
118 struct rte_mbuf *out_seg = NULL;
119 uint32_t len;
120
121 /* Allocate indirect buffer */
122 out_seg = rte_pktmbuf_alloc(pool_indirect);
123 if (unlikely(out_seg == NULL)) {
124 rte_pktmbuf_free(out_pkt);
125 __free_fragments(pkts_out, out_pkt_pos);
126 return -ENOMEM;
127 }
128 out_seg_prev->next = out_seg;
129 out_seg_prev = out_seg;
130
131 /* Prepare indirect buffer */
132 rte_pktmbuf_attach(out_seg, in_seg);
133 len = mtu_size - out_pkt->pkt_len;
134 if (len > (in_seg->data_len - in_seg_data_pos)) {
135 len = in_seg->data_len - in_seg_data_pos;
136 }
137 out_seg->data_off = in_seg->data_off + in_seg_data_pos;
138 out_seg->data_len = (uint16_t)len;
139 out_pkt->pkt_len = (uint16_t)(len +
140 out_pkt->pkt_len);
141 out_pkt->nb_segs += 1;
142 in_seg_data_pos += len;
143
144 /* Current output packet (i.e. fragment) done ? */
145 if (unlikely(out_pkt->pkt_len >= mtu_size)) {
146 more_out_segs = 0;
147 }
148
149 /* Current input segment done ? */
150 if (unlikely(in_seg_data_pos == in_seg->data_len)) {
151 in_seg = in_seg->next;
152 in_seg_data_pos = 0;
153
154 if (unlikely(in_seg == NULL)) {
155 more_in_segs = 0;
156 }
157 }
158 }
159
160 /* Build the IP header */
161
162 out_hdr = rte_pktmbuf_mtod(out_pkt, struct ipv6_hdr *);
163
164 __fill_ipv6hdr_frag(out_hdr, in_hdr,
165 (uint16_t) out_pkt->pkt_len - sizeof(struct ipv6_hdr),
166 fragment_offset, more_in_segs);
167
168 fragment_offset = (uint16_t)(fragment_offset +
169 out_pkt->pkt_len - sizeof(struct ipv6_hdr)
170 - sizeof(struct ipv6_extension_fragment));
171
172 /* Write the fragment to the output list */
173 pkts_out[out_pkt_pos] = out_pkt;
174 out_pkt_pos ++;
175 }
176
177 return out_pkt_pos;
178 }