1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Red Hat, Inc.
9 #include <rte_common.h>
10 #include <rte_cycles.h>
11 #include <rte_hexdump.h>
14 #ifdef RTE_EXEC_ENV_WINDOWS
18 printf("ipfrag not supported on Windows, skipping test\n");
24 #include <rte_ip_frag.h>
26 #include <rte_memcpy.h>
27 #include <rte_random.h>
32 uint8_t expected_first_frag_ipv4_opts_copied[] = {
33 0x07, 0x0b, 0x04, 0x00,
34 0x00, 0x00, 0x00, 0x00,
35 0x00, 0x00, 0x00, 0x83,
36 0x07, 0x04, 0xc0, 0xa8,
37 0xe3, 0x96, 0x00, 0x00,
40 uint8_t expected_sub_frag_ipv4_opts_copied[] = {
41 0x83, 0x07, 0x04, 0xc0,
42 0xa8, 0xe3, 0x96, 0x00,
45 uint8_t expected_first_frag_ipv4_opts_nocopied[] = {
46 0x07, 0x0b, 0x04, 0x00,
47 0x00, 0x00, 0x00, 0x00,
48 0x00, 0x00, 0x00, 0x00,
51 uint8_t expected_sub_frag_ipv4_opts_nocopied[0];
53 struct test_opt_data {
54 bool is_first_frag; /**< offset is 0 */
55 bool opt_copied; /**< ip option copied flag */
56 uint16_t len; /**< option data len */
57 uint8_t data[RTE_IPV4_HDR_OPT_MAX_LEN]; /**< option data */
60 static struct rte_mempool *pkt_pool,
65 hex_to_str(uint8_t *hex, uint16_t len, char *str)
69 for (i = 0; i < len; i++) {
70 sprintf(str, "%02x", hex[i]);
79 pkt_pool = rte_pktmbuf_pool_create("FRAG_MBUF_POOL",
81 RTE_MBUF_DEFAULT_BUF_SIZE,
83 if (pkt_pool == NULL) {
84 printf("%s: Error creating pkt mempool\n", __func__);
88 direct_pool = rte_pktmbuf_pool_create("FRAG_D_MBUF_POOL",
90 RTE_MBUF_DEFAULT_BUF_SIZE,
92 if (direct_pool == NULL) {
93 printf("%s: Error creating direct mempool\n", __func__);
97 indirect_pool = rte_pktmbuf_pool_create("FRAG_I_MBUF_POOL",
100 if (indirect_pool == NULL) {
101 printf("%s: Error creating indirect mempool\n", __func__);
108 rte_mempool_free(pkt_pool);
111 rte_mempool_free(direct_pool);
117 static int testsuite_setup(void)
119 return setup_buf_pool();
122 static void testsuite_teardown(void)
124 rte_mempool_free(pkt_pool);
125 rte_mempool_free(direct_pool);
126 rte_mempool_free(indirect_pool);
130 indirect_pool = NULL;
133 static int ut_setup(void)
138 static void ut_teardown(void)
143 test_get_ipv4_opt(bool is_first_frag, bool opt_copied,
144 struct test_opt_data *expected_opt)
149 sizeof(expected_first_frag_ipv4_opts_copied);
150 rte_memcpy(expected_opt->data,
151 expected_first_frag_ipv4_opts_copied,
152 sizeof(expected_first_frag_ipv4_opts_copied));
155 sizeof(expected_first_frag_ipv4_opts_nocopied);
156 rte_memcpy(expected_opt->data,
157 expected_first_frag_ipv4_opts_nocopied,
158 sizeof(expected_first_frag_ipv4_opts_nocopied));
163 sizeof(expected_sub_frag_ipv4_opts_copied);
164 rte_memcpy(expected_opt->data,
165 expected_sub_frag_ipv4_opts_copied,
166 sizeof(expected_sub_frag_ipv4_opts_copied));
169 sizeof(expected_sub_frag_ipv4_opts_nocopied);
170 rte_memcpy(expected_opt->data,
171 expected_sub_frag_ipv4_opts_nocopied,
172 sizeof(expected_sub_frag_ipv4_opts_nocopied));
178 v4_allocate_packet_of(struct rte_mbuf *b, int fill, size_t s,
179 int df, uint8_t mf, uint16_t off, uint8_t ttl, uint8_t proto,
180 uint16_t pktid, bool have_opt, bool is_first_frag, bool opt_copied)
182 /* Create a packet, 2k bytes long */
184 char *data = rte_pktmbuf_mtod(b, char *);
185 rte_be16_t fragment_offset = 0; /* fragmentation offset */
187 struct test_opt_data opt;
192 test_get_ipv4_opt(is_first_frag, opt_copied, &opt);
194 iph_len = sizeof(struct rte_ipv4_hdr) + opt.len;
195 memset(data, fill, iph_len + s);
197 struct rte_ipv4_hdr *hdr = (struct rte_ipv4_hdr *)data;
199 hdr->version_ihl = 0x40; /* ipv4 */
200 hdr->version_ihl += (iph_len / 4);
201 hdr->type_of_service = 0;
202 b->pkt_len = s + iph_len;
203 b->data_len = b->pkt_len;
204 hdr->total_length = rte_cpu_to_be_16(b->pkt_len);
205 hdr->packet_id = rte_cpu_to_be_16(pktid);
208 fragment_offset |= 0x4000;
211 fragment_offset |= 0x2000;
214 fragment_offset |= off;
216 hdr->fragment_offset = rte_cpu_to_be_16(fragment_offset);
219 ttl = 64; /* default to 64 */
222 proto = 1; /* icmp */
224 hdr->time_to_live = ttl;
225 hdr->next_proto_id = proto;
226 hdr->hdr_checksum = 0;
227 hdr->src_addr = rte_cpu_to_be_32(0x8080808);
228 hdr->dst_addr = rte_cpu_to_be_32(0x8080404);
230 rte_memcpy(hdr + 1, opt.data, opt.len);
234 v6_allocate_packet_of(struct rte_mbuf *b, int fill, size_t s, uint8_t ttl,
235 uint8_t proto, uint16_t pktid)
237 /* Create a packet, 2k bytes long */
239 char *data = rte_pktmbuf_mtod(b, char *);
241 memset(data, fill, sizeof(struct rte_ipv6_hdr) + s);
243 struct rte_ipv6_hdr *hdr = (struct rte_ipv6_hdr *)data;
244 b->pkt_len = s + sizeof(struct rte_ipv6_hdr);
245 b->data_len = b->pkt_len;
247 /* basic v6 header */
248 hdr->vtc_flow = rte_cpu_to_be_32(0x60 << 24 | pktid);
249 hdr->payload_len = rte_cpu_to_be_16(b->pkt_len);
251 hdr->hop_limits = ttl;
253 memset(hdr->src_addr, 0x08, sizeof(hdr->src_addr));
254 memset(hdr->dst_addr, 0x04, sizeof(hdr->src_addr));
258 test_free_fragments(struct rte_mbuf *mb[], uint32_t num)
261 for (i = 0; i < num; i++)
262 rte_pktmbuf_free(mb[i]);
266 test_get_offset(struct rte_mbuf **mb, int32_t len,
267 uint16_t *offset, int ipv)
271 for (i = 0; i < len; i++) {
273 struct rte_ipv4_hdr *iph =
274 rte_pktmbuf_mtod(mb[i], struct rte_ipv4_hdr *);
275 offset[i] = iph->fragment_offset;
276 } else if (ipv == 6) {
277 struct ipv6_extension_fragment *fh =
278 rte_pktmbuf_mtod_offset(
280 struct ipv6_extension_fragment *,
281 sizeof(struct rte_ipv6_hdr));
282 offset[i] = fh->frag_data;
288 test_get_frag_opt(struct rte_mbuf **mb, int32_t num,
289 struct test_opt_data *opt, int ipv, bool opt_copied)
293 for (i = 0; i < num; i++) {
295 struct rte_ipv4_hdr *iph =
296 rte_pktmbuf_mtod(mb[i], struct rte_ipv4_hdr *);
297 uint16_t header_len = (iph->version_ihl &
298 RTE_IPV4_HDR_IHL_MASK) *
299 RTE_IPV4_IHL_MULTIPLIER;
300 uint16_t opt_len = header_len -
301 sizeof(struct rte_ipv4_hdr);
303 opt->opt_copied = opt_copied;
305 if ((rte_be_to_cpu_16(iph->fragment_offset) &
306 RTE_IPV4_HDR_OFFSET_MASK) == 0)
307 opt->is_first_frag = true;
309 opt->is_first_frag = false;
311 if (likely(opt_len <= RTE_IPV4_HDR_OPT_MAX_LEN)) {
312 char *iph_opt = rte_pktmbuf_mtod_offset(mb[i],
313 char *, sizeof(struct rte_ipv4_hdr));
315 rte_memcpy(opt->data, iph_opt, opt_len);
317 opt->len = RTE_IPV4_HDR_OPT_MAX_LEN;
318 memset(opt->data, RTE_IPV4_HDR_OPT_EOL,
329 static const uint16_t RND_ID = UINT16_MAX;
330 int result = TEST_SUCCESS;
333 struct test_ip_frags {
344 uint16_t expected_fragment_offset[BURST];
349 {4, 1280, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID, 2,
350 {0x2000, 0x009D}, false},
351 {4, 1280, 1400, 0, 0, 0, 64, IPPROTO_ICMP, 0, 2,
352 {0x2000, 0x009D}, false},
353 {4, 600, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID, 3,
354 {0x2000, 0x2048, 0x0090}, false},
355 {4, 4, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID, -EINVAL},
356 {4, 600, 1400, 1, 0, 0, 64, IPPROTO_ICMP, RND_ID, -ENOTSUP},
357 {4, 600, 1400, 0, 0, 0, 0, IPPROTO_ICMP, RND_ID, 3,
358 {0x2000, 0x2046, 0x008C}, true, true, true},
359 /* The first fragment */
360 {4, 68, 104, 0, 1, 0, 0, IPPROTO_ICMP, RND_ID, 5,
361 {0x2000, 0x2003, 0x2006, 0x2009, 0x200C}, true, true, true},
362 /* The middle fragment */
363 {4, 68, 104, 0, 1, 13, 0, IPPROTO_ICMP, RND_ID, 3,
364 {0x200D, 0x2012, 0x2017}, true, false, true},
365 /* The last fragment */
366 {4, 68, 104, 0, 0, 26, 0, IPPROTO_ICMP, RND_ID, 3,
367 {0x201A, 0x201F, 0x0024}, true, false, true},
368 /* The first fragment */
369 {4, 68, 104, 0, 1, 0, 0, IPPROTO_ICMP, RND_ID, 4,
370 {0x2000, 0x2004, 0x2008, 0x200C}, true, true, false},
371 /* The middle fragment */
372 {4, 68, 104, 0, 1, 13, 0, IPPROTO_ICMP, RND_ID, 3,
373 {0x200D, 0x2013, 0x2019}, true, false, false},
374 /* The last fragment */
375 {4, 68, 104, 0, 0, 26, 0, IPPROTO_ICMP, RND_ID, 3,
376 {0x201A, 0x2020, 0x0026}, true, false, false},
377 {6, 1280, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID, 2,
378 {0x0001, 0x04D0}, false},
379 {6, 1300, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID, 2,
380 {0x0001, 0x04E0}, false},
381 {6, 4, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID, -EINVAL},
382 {6, 1300, 1400, 0, 0, 0, 0, IPPROTO_ICMP, RND_ID, 2,
383 {0x0001, 0x04E0}, false},
386 for (i = 0; i < RTE_DIM(tests); i++) {
388 uint16_t fragment_offset[BURST];
389 struct test_opt_data opt_res[BURST];
390 struct test_opt_data opt_exp;
391 uint16_t pktid = tests[i].pkt_id;
392 struct rte_mbuf *pkts_out[BURST];
393 struct rte_mbuf *b = rte_pktmbuf_alloc(pkt_pool);
395 RTE_TEST_ASSERT_NOT_EQUAL(b, NULL,
396 "Failed to allocate pkt.");
398 if (tests[i].pkt_id == RND_ID)
399 pktid = rte_rand_max(UINT16_MAX);
401 if (tests[i].ipv == 4) {
402 v4_allocate_packet_of(b, 0x41414141,
411 tests[i].is_first_frag,
412 tests[i].opt_copied);
413 } else if (tests[i].ipv == 6) {
414 v6_allocate_packet_of(b, 0x41414141,
421 if (tests[i].ipv == 4)
422 len = rte_ipv4_fragment_packet(b, pkts_out, BURST,
426 else if (tests[i].ipv == 6)
427 len = rte_ipv6_fragment_packet(b, pkts_out, BURST,
435 test_get_offset(pkts_out, len,
436 fragment_offset, tests[i].ipv);
437 if (tests[i].have_opt)
438 test_get_frag_opt(pkts_out, len, opt_res,
439 tests[i].ipv, tests[i].opt_copied);
440 test_free_fragments(pkts_out, len);
443 printf("[check frag number]%zd: checking %d with %d\n", i, len,
444 tests[i].expected_frags);
445 RTE_TEST_ASSERT_EQUAL(len, tests[i].expected_frags,
446 "Failed case %zd.\n", i);
449 for (j = 0; j < (size_t)len; j++) {
450 printf("[check offset]%zd-%zd: checking %d with %d\n",
451 i, j, fragment_offset[j],
453 tests[i].expected_fragment_offset[j]));
454 RTE_TEST_ASSERT_EQUAL(fragment_offset[j],
456 tests[i].expected_fragment_offset[j]),
457 "Failed case %zd.\n", i);
460 if (tests[i].have_opt && (tests[i].ipv == 4)) {
461 for (j = 0; j < (size_t)len; j++) {
463 RTE_IPV4_HDR_OPT_MAX_LEN + 1];
465 RTE_IPV4_HDR_OPT_MAX_LEN + 1];
468 opt_res[j].is_first_frag,
469 opt_res[j].opt_copied,
471 hex_to_str(opt_res[j].data,
474 hex_to_str(opt_exp.data,
479 "[check ipv4 option]%zd-%zd: checking (len:%u)%s with (len:%u)%s\n",
481 opt_res[j].len, opt_res_str,
482 opt_exp.len, opt_exp_str);
483 RTE_TEST_ASSERT_SUCCESS(
486 "Failed case %zd.\n", i);
496 static struct unit_test_suite ipfrag_testsuite = {
497 .suite_name = "IP Frag Unit Test Suite",
498 .setup = testsuite_setup,
499 .teardown = testsuite_teardown,
501 TEST_CASE_ST(ut_setup, ut_teardown,
504 TEST_CASES_END() /**< NULL terminate unit test array */
511 rte_log_set_global_level(RTE_LOG_DEBUG);
512 rte_log_set_level(RTE_LOGTYPE_EAL, RTE_LOG_DEBUG);
514 return unit_test_suite_runner(&ipfrag_testsuite);
517 #endif /* !RTE_EXEC_ENV_WINDOWS */
519 REGISTER_TEST_COMMAND(ipfrag_autotest, test_ipfrag);