1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Red Hat, Inc.
7 #include <rte_common.h>
8 #include <rte_cycles.h>
9 #include <rte_hexdump.h>
11 #include <rte_ip_frag.h>
13 #include <rte_memcpy.h>
14 #include <rte_random.h>
21 static struct rte_mempool *pkt_pool,
28 pkt_pool = rte_pktmbuf_pool_create("FRAG_MBUF_POOL",
30 RTE_MBUF_DEFAULT_BUF_SIZE,
32 if (pkt_pool == NULL) {
33 printf("%s: Error creating pkt mempool\n", __func__);
37 direct_pool = rte_pktmbuf_pool_create("FRAG_D_MBUF_POOL",
39 RTE_MBUF_DEFAULT_BUF_SIZE,
41 if (direct_pool == NULL) {
42 printf("%s: Error creating direct mempool\n", __func__);
46 indirect_pool = rte_pktmbuf_pool_create("FRAG_I_MBUF_POOL",
49 if (indirect_pool == NULL) {
50 printf("%s: Error creating indirect mempool\n", __func__);
57 rte_mempool_free(pkt_pool);
60 rte_mempool_free(direct_pool);
66 static int testsuite_setup(void)
68 return setup_buf_pool();
71 static void testsuite_teardown(void)
73 rte_mempool_free(pkt_pool);
74 rte_mempool_free(direct_pool);
75 rte_mempool_free(indirect_pool);
82 static int ut_setup(void)
87 static void ut_teardown(void)
92 v4_allocate_packet_of(struct rte_mbuf *b, int fill,
93 size_t s, int df, uint8_t mf, uint16_t off,
94 uint8_t ttl, uint8_t proto, uint16_t pktid)
96 /* Create a packet, 2k bytes long */
98 char *data = rte_pktmbuf_mtod(b, char *);
99 rte_be16_t fragment_offset = 0; /**< fragmentation offset */
101 memset(data, fill, sizeof(struct rte_ipv4_hdr) + s);
103 struct rte_ipv4_hdr *hdr = (struct rte_ipv4_hdr *)data;
105 hdr->version_ihl = 0x45; /* standard IP header... */
106 hdr->type_of_service = 0;
107 b->pkt_len = s + sizeof(struct rte_ipv4_hdr);
108 b->data_len = b->pkt_len;
109 hdr->total_length = rte_cpu_to_be_16(b->pkt_len);
110 hdr->packet_id = rte_cpu_to_be_16(pktid);
113 fragment_offset |= 0x4000;
116 fragment_offset |= 0x2000;
119 fragment_offset |= off;
121 hdr->fragment_offset = rte_cpu_to_be_16(fragment_offset);
124 ttl = 64; /* default to 64 */
127 proto = 1; /* icmp */
129 hdr->time_to_live = ttl;
130 hdr->next_proto_id = proto;
131 hdr->hdr_checksum = 0;
132 hdr->src_addr = rte_cpu_to_be_32(0x8080808);
133 hdr->dst_addr = rte_cpu_to_be_32(0x8080404);
137 v6_allocate_packet_of(struct rte_mbuf *b, int fill, size_t s, uint8_t ttl,
138 uint8_t proto, uint16_t pktid)
140 /* Create a packet, 2k bytes long */
142 char *data = rte_pktmbuf_mtod(b, char *);
144 memset(data, fill, sizeof(struct rte_ipv6_hdr) + s);
146 struct rte_ipv6_hdr *hdr = (struct rte_ipv6_hdr *)data;
147 b->pkt_len = s + sizeof(struct rte_ipv6_hdr);
148 b->data_len = b->pkt_len;
150 /* basic v6 header */
151 hdr->vtc_flow = rte_cpu_to_be_32(0x60 << 24 | pktid);
152 hdr->payload_len = rte_cpu_to_be_16(b->pkt_len);
154 hdr->hop_limits = ttl;
156 memset(hdr->src_addr, 0x08, sizeof(hdr->src_addr));
157 memset(hdr->dst_addr, 0x04, sizeof(hdr->src_addr));
161 test_free_fragments(struct rte_mbuf *mb[], uint32_t num)
164 for (i = 0; i < num; i++)
165 rte_pktmbuf_free(mb[i]);
169 test_get_offset(struct rte_mbuf **mb, int32_t len,
170 uint16_t *offset, int ipv)
174 for (i = 0; i < len; i++) {
176 struct rte_ipv4_hdr *iph =
177 rte_pktmbuf_mtod(mb[i], struct rte_ipv4_hdr *);
178 offset[i] = iph->fragment_offset;
179 } else if (ipv == 6) {
180 struct ipv6_extension_fragment *fh =
181 rte_pktmbuf_mtod_offset(
183 struct ipv6_extension_fragment *,
184 sizeof(struct rte_ipv6_hdr));
185 offset[i] = fh->frag_data;
193 static const uint16_t RND_ID = UINT16_MAX;
194 int result = TEST_SUCCESS;
197 struct test_ip_frags {
208 uint16_t expected_fragment_offset[BURST];
210 {4, 1280, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID, 2,
212 {4, 1280, 1400, 0, 0, 0, 64, IPPROTO_ICMP, 0, 2,
214 {4, 600, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID, 3,
215 {0x2000, 0x2048, 0x0090}},
216 {4, 4, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID, -EINVAL},
217 {4, 600, 1400, 1, 0, 0, 64, IPPROTO_ICMP, RND_ID, -ENOTSUP},
218 {4, 600, 1400, 0, 0, 0, 0, IPPROTO_ICMP, RND_ID, 3,
219 {0x2000, 0x2048, 0x0090}},
220 {4, 68, 104, 0, 1, 13, 0, IPPROTO_ICMP, RND_ID, 3,
221 {0x200D, 0x2013, 0x2019}},
223 {6, 1280, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID, 2,
225 {6, 1300, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID, 2,
227 {6, 4, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID, -EINVAL},
228 {6, 1300, 1400, 0, 0, 0, 0, IPPROTO_ICMP, RND_ID, 2,
232 for (i = 0; i < RTE_DIM(tests); i++) {
234 uint16_t fragment_offset[BURST];
235 uint16_t pktid = tests[i].pkt_id;
236 struct rte_mbuf *pkts_out[BURST];
237 struct rte_mbuf *b = rte_pktmbuf_alloc(pkt_pool);
239 RTE_TEST_ASSERT_NOT_EQUAL(b, NULL,
240 "Failed to allocate pkt.");
242 if (tests[i].pkt_id == RND_ID)
243 pktid = rte_rand_max(UINT16_MAX);
245 if (tests[i].ipv == 4) {
246 v4_allocate_packet_of(b, 0x41414141,
254 } else if (tests[i].ipv == 6) {
255 v6_allocate_packet_of(b, 0x41414141,
262 if (tests[i].ipv == 4)
263 len = rte_ipv4_fragment_packet(b, pkts_out, BURST,
267 else if (tests[i].ipv == 6)
268 len = rte_ipv6_fragment_packet(b, pkts_out, BURST,
276 test_get_offset(pkts_out, len,
277 fragment_offset, tests[i].ipv);
278 test_free_fragments(pkts_out, len);
281 printf("%zd: checking %d with %d\n", i, len,
282 tests[i].expected_frags);
283 RTE_TEST_ASSERT_EQUAL(len, tests[i].expected_frags,
284 "Failed case %zd.\n", i);
287 for (j = 0; j < (size_t)len; j++) {
288 printf("%zd-%zd: checking %d with %d\n",
289 i, j, fragment_offset[j],
291 tests[i].expected_fragment_offset[j]));
292 RTE_TEST_ASSERT_EQUAL(fragment_offset[j],
294 tests[i].expected_fragment_offset[j]),
295 "Failed case %zd.\n", i);
304 static struct unit_test_suite ipfrag_testsuite = {
305 .suite_name = "IP Frag Unit Test Suite",
306 .setup = testsuite_setup,
307 .teardown = testsuite_teardown,
309 TEST_CASE_ST(ut_setup, ut_teardown,
312 TEST_CASES_END() /**< NULL terminate unit test array */
319 rte_log_set_global_level(RTE_LOG_DEBUG);
320 rte_log_set_level(RTE_LOGTYPE_EAL, RTE_LOG_DEBUG);
322 return unit_test_suite_runner(&ipfrag_testsuite);
325 REGISTER_TEST_COMMAND(ipfrag_autotest, test_ipfrag);