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 static struct rte_mempool *pkt_pool,
39 pkt_pool = rte_pktmbuf_pool_create("FRAG_MBUF_POOL",
41 RTE_MBUF_DEFAULT_BUF_SIZE,
43 if (pkt_pool == NULL) {
44 printf("%s: Error creating pkt mempool\n", __func__);
48 direct_pool = rte_pktmbuf_pool_create("FRAG_D_MBUF_POOL",
50 RTE_MBUF_DEFAULT_BUF_SIZE,
52 if (direct_pool == NULL) {
53 printf("%s: Error creating direct mempool\n", __func__);
57 indirect_pool = rte_pktmbuf_pool_create("FRAG_I_MBUF_POOL",
60 if (indirect_pool == NULL) {
61 printf("%s: Error creating indirect mempool\n", __func__);
68 rte_mempool_free(pkt_pool);
71 rte_mempool_free(direct_pool);
77 static int testsuite_setup(void)
79 return setup_buf_pool();
82 static void testsuite_teardown(void)
84 rte_mempool_free(pkt_pool);
85 rte_mempool_free(direct_pool);
86 rte_mempool_free(indirect_pool);
93 static int ut_setup(void)
98 static void ut_teardown(void)
103 v4_allocate_packet_of(struct rte_mbuf *b, int fill,
104 size_t s, int df, uint8_t mf, uint16_t off,
105 uint8_t ttl, uint8_t proto, uint16_t pktid)
107 /* Create a packet, 2k bytes long */
109 char *data = rte_pktmbuf_mtod(b, char *);
110 rte_be16_t fragment_offset = 0; /**< fragmentation offset */
112 memset(data, fill, sizeof(struct rte_ipv4_hdr) + s);
114 struct rte_ipv4_hdr *hdr = (struct rte_ipv4_hdr *)data;
116 hdr->version_ihl = 0x45; /* standard IP header... */
117 hdr->type_of_service = 0;
118 b->pkt_len = s + sizeof(struct rte_ipv4_hdr);
119 b->data_len = b->pkt_len;
120 hdr->total_length = rte_cpu_to_be_16(b->pkt_len);
121 hdr->packet_id = rte_cpu_to_be_16(pktid);
124 fragment_offset |= 0x4000;
127 fragment_offset |= 0x2000;
130 fragment_offset |= off;
132 hdr->fragment_offset = rte_cpu_to_be_16(fragment_offset);
135 ttl = 64; /* default to 64 */
138 proto = 1; /* icmp */
140 hdr->time_to_live = ttl;
141 hdr->next_proto_id = proto;
142 hdr->hdr_checksum = 0;
143 hdr->src_addr = rte_cpu_to_be_32(0x8080808);
144 hdr->dst_addr = rte_cpu_to_be_32(0x8080404);
148 v6_allocate_packet_of(struct rte_mbuf *b, int fill, size_t s, uint8_t ttl,
149 uint8_t proto, uint16_t pktid)
151 /* Create a packet, 2k bytes long */
153 char *data = rte_pktmbuf_mtod(b, char *);
155 memset(data, fill, sizeof(struct rte_ipv6_hdr) + s);
157 struct rte_ipv6_hdr *hdr = (struct rte_ipv6_hdr *)data;
158 b->pkt_len = s + sizeof(struct rte_ipv6_hdr);
159 b->data_len = b->pkt_len;
161 /* basic v6 header */
162 hdr->vtc_flow = rte_cpu_to_be_32(0x60 << 24 | pktid);
163 hdr->payload_len = rte_cpu_to_be_16(b->pkt_len);
165 hdr->hop_limits = ttl;
167 memset(hdr->src_addr, 0x08, sizeof(hdr->src_addr));
168 memset(hdr->dst_addr, 0x04, sizeof(hdr->src_addr));
172 test_free_fragments(struct rte_mbuf *mb[], uint32_t num)
175 for (i = 0; i < num; i++)
176 rte_pktmbuf_free(mb[i]);
180 test_get_offset(struct rte_mbuf **mb, int32_t len,
181 uint16_t *offset, int ipv)
185 for (i = 0; i < len; i++) {
187 struct rte_ipv4_hdr *iph =
188 rte_pktmbuf_mtod(mb[i], struct rte_ipv4_hdr *);
189 offset[i] = iph->fragment_offset;
190 } else if (ipv == 6) {
191 struct ipv6_extension_fragment *fh =
192 rte_pktmbuf_mtod_offset(
194 struct ipv6_extension_fragment *,
195 sizeof(struct rte_ipv6_hdr));
196 offset[i] = fh->frag_data;
204 static const uint16_t RND_ID = UINT16_MAX;
205 int result = TEST_SUCCESS;
208 struct test_ip_frags {
219 uint16_t expected_fragment_offset[BURST];
221 {4, 1280, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID, 2,
223 {4, 1280, 1400, 0, 0, 0, 64, IPPROTO_ICMP, 0, 2,
225 {4, 600, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID, 3,
226 {0x2000, 0x2048, 0x0090}},
227 {4, 4, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID, -EINVAL},
228 {4, 600, 1400, 1, 0, 0, 64, IPPROTO_ICMP, RND_ID, -ENOTSUP},
229 {4, 600, 1400, 0, 0, 0, 0, IPPROTO_ICMP, RND_ID, 3,
230 {0x2000, 0x2048, 0x0090}},
231 {4, 68, 104, 0, 1, 13, 0, IPPROTO_ICMP, RND_ID, 3,
232 {0x200D, 0x2013, 0x2019}},
234 {6, 1280, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID, 2,
236 {6, 1300, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID, 2,
238 {6, 4, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID, -EINVAL},
239 {6, 1300, 1400, 0, 0, 0, 0, IPPROTO_ICMP, RND_ID, 2,
243 for (i = 0; i < RTE_DIM(tests); i++) {
245 uint16_t fragment_offset[BURST];
246 uint16_t pktid = tests[i].pkt_id;
247 struct rte_mbuf *pkts_out[BURST];
248 struct rte_mbuf *b = rte_pktmbuf_alloc(pkt_pool);
250 RTE_TEST_ASSERT_NOT_EQUAL(b, NULL,
251 "Failed to allocate pkt.");
253 if (tests[i].pkt_id == RND_ID)
254 pktid = rte_rand_max(UINT16_MAX);
256 if (tests[i].ipv == 4) {
257 v4_allocate_packet_of(b, 0x41414141,
265 } else if (tests[i].ipv == 6) {
266 v6_allocate_packet_of(b, 0x41414141,
273 if (tests[i].ipv == 4)
274 len = rte_ipv4_fragment_packet(b, pkts_out, BURST,
278 else if (tests[i].ipv == 6)
279 len = rte_ipv6_fragment_packet(b, pkts_out, BURST,
287 test_get_offset(pkts_out, len,
288 fragment_offset, tests[i].ipv);
289 test_free_fragments(pkts_out, len);
292 printf("%zd: checking %d with %d\n", i, len,
293 tests[i].expected_frags);
294 RTE_TEST_ASSERT_EQUAL(len, tests[i].expected_frags,
295 "Failed case %zd.\n", i);
298 for (j = 0; j < (size_t)len; j++) {
299 printf("%zd-%zd: checking %d with %d\n",
300 i, j, fragment_offset[j],
302 tests[i].expected_fragment_offset[j]));
303 RTE_TEST_ASSERT_EQUAL(fragment_offset[j],
305 tests[i].expected_fragment_offset[j]),
306 "Failed case %zd.\n", i);
315 static struct unit_test_suite ipfrag_testsuite = {
316 .suite_name = "IP Frag Unit Test Suite",
317 .setup = testsuite_setup,
318 .teardown = testsuite_teardown,
320 TEST_CASE_ST(ut_setup, ut_teardown,
323 TEST_CASES_END() /**< NULL terminate unit test array */
330 rte_log_set_global_level(RTE_LOG_DEBUG);
331 rte_log_set_level(RTE_LOGTYPE_EAL, RTE_LOG_DEBUG);
333 return unit_test_suite_runner(&ipfrag_testsuite);
336 #endif /* !RTE_EXEC_ENV_WINDOWS */
338 REGISTER_TEST_COMMAND(ipfrag_autotest, test_ipfrag);