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, size_t s, int df,
93 uint8_t ttl, uint8_t proto, uint16_t pktid)
95 /* Create a packet, 2k bytes long */
97 char *data = rte_pktmbuf_mtod(b, char *);
99 memset(data, fill, sizeof(struct rte_ipv4_hdr) + s);
101 struct rte_ipv4_hdr *hdr = (struct rte_ipv4_hdr *)data;
103 hdr->version_ihl = 0x45; /* standard IP header... */
104 hdr->type_of_service = 0;
105 b->pkt_len = s + sizeof(struct rte_ipv4_hdr);
106 b->data_len = b->pkt_len;
107 hdr->total_length = rte_cpu_to_be_16(b->pkt_len);
108 hdr->packet_id = rte_cpu_to_be_16(pktid);
109 hdr->fragment_offset = 0;
111 hdr->fragment_offset = rte_cpu_to_be_16(0x4000);
114 ttl = 64; /* default to 64 */
117 proto = 1; /* icmp */
119 hdr->time_to_live = ttl;
120 hdr->next_proto_id = proto;
121 hdr->hdr_checksum = 0;
122 hdr->src_addr = rte_cpu_to_be_32(0x8080808);
123 hdr->dst_addr = rte_cpu_to_be_32(0x8080404);
127 v6_allocate_packet_of(struct rte_mbuf *b, int fill, size_t s, uint8_t ttl,
128 uint8_t proto, uint16_t pktid)
130 /* Create a packet, 2k bytes long */
132 char *data = rte_pktmbuf_mtod(b, char *);
134 memset(data, fill, sizeof(struct rte_ipv6_hdr) + s);
136 struct rte_ipv6_hdr *hdr = (struct rte_ipv6_hdr *)data;
137 b->pkt_len = s + sizeof(struct rte_ipv6_hdr);
138 b->data_len = b->pkt_len;
140 /* basic v6 header */
141 hdr->vtc_flow = rte_cpu_to_be_32(0x60 << 24 | pktid);
142 hdr->payload_len = rte_cpu_to_be_16(b->pkt_len);
144 hdr->hop_limits = ttl;
146 memset(hdr->src_addr, 0x08, sizeof(hdr->src_addr));
147 memset(hdr->dst_addr, 0x04, sizeof(hdr->src_addr));
151 test_free_fragments(struct rte_mbuf *mb[], uint32_t num)
154 for (i = 0; i < num; i++)
155 rte_pktmbuf_free(mb[i]);
161 static const uint16_t RND_ID = UINT16_MAX;
162 int result = TEST_SUCCESS;
165 struct test_ip_frags {
175 {4, 1280, 1400, 0, 64, IPPROTO_ICMP, RND_ID, 2},
176 {4, 1280, 1400, 0, 64, IPPROTO_ICMP, 0, 2},
177 {4, 600, 1400, 0, 64, IPPROTO_ICMP, RND_ID, 3},
178 {4, 4, 1400, 0, 64, IPPROTO_ICMP, RND_ID, -EINVAL},
179 {4, 600, 1400, 1, 64, IPPROTO_ICMP, RND_ID, -ENOTSUP},
180 {4, 600, 1400, 0, 0, IPPROTO_ICMP, RND_ID, 3},
182 {6, 1280, 1400, 0, 64, IPPROTO_ICMP, RND_ID, 2},
183 {6, 1300, 1400, 0, 64, IPPROTO_ICMP, RND_ID, 2},
184 {6, 4, 1400, 0, 64, IPPROTO_ICMP, RND_ID, -EINVAL},
185 {6, 1300, 1400, 0, 0, IPPROTO_ICMP, RND_ID, 2},
188 for (i = 0; i < RTE_DIM(tests); i++) {
190 uint16_t pktid = tests[i].pkt_id;
191 struct rte_mbuf *pkts_out[BURST];
192 struct rte_mbuf *b = rte_pktmbuf_alloc(pkt_pool);
194 RTE_TEST_ASSERT_NOT_EQUAL(b, NULL,
195 "Failed to allocate pkt.");
197 if (tests[i].pkt_id == RND_ID)
198 pktid = rte_rand_max(UINT16_MAX);
200 if (tests[i].ipv == 4) {
201 v4_allocate_packet_of(b, 0x41414141,
207 } else if (tests[i].ipv == 6) {
208 v6_allocate_packet_of(b, 0x41414141,
215 if (tests[i].ipv == 4)
216 len = rte_ipv4_fragment_packet(b, pkts_out, BURST,
220 else if (tests[i].ipv == 6)
221 len = rte_ipv6_fragment_packet(b, pkts_out, BURST,
229 test_free_fragments(pkts_out, len);
231 printf("%zd: checking %d with %d\n", i, len,
232 tests[i].expected_frags);
233 RTE_TEST_ASSERT_EQUAL(len, tests[i].expected_frags,
234 "Failed case %zd.\n", i);
241 static struct unit_test_suite ipfrag_testsuite = {
242 .suite_name = "IP Frag Unit Test Suite",
243 .setup = testsuite_setup,
244 .teardown = testsuite_teardown,
246 TEST_CASE_ST(ut_setup, ut_teardown,
249 TEST_CASES_END() /**< NULL terminate unit test array */
256 rte_log_set_global_level(RTE_LOG_DEBUG);
257 rte_log_set_level(RTE_LOGTYPE_EAL, RTE_LOG_DEBUG);
259 return unit_test_suite_runner(&ipfrag_testsuite);
262 REGISTER_TEST_COMMAND(ipfrag_autotest, test_ipfrag);