test/ipfrag: add unit test case
[dpdk.git] / app / test / test_ipfrag.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2020 Red Hat, Inc.
3  */
4
5 #include <time.h>
6
7 #include <rte_common.h>
8 #include <rte_cycles.h>
9 #include <rte_hexdump.h>
10 #include <rte_ip.h>
11 #include <rte_ip_frag.h>
12 #include <rte_mbuf.h>
13 #include <rte_memcpy.h>
14 #include <rte_random.h>
15
16 #include "test.h"
17
18 #define NUM_MBUFS 128
19 #define BURST 32
20
21 static struct rte_mempool *pkt_pool,
22                           *direct_pool,
23                           *indirect_pool;
24
25 static int
26 setup_buf_pool(void)
27 {
28         pkt_pool = rte_pktmbuf_pool_create("FRAG_MBUF_POOL",
29                                            NUM_MBUFS, BURST, 0,
30                                            RTE_MBUF_DEFAULT_BUF_SIZE,
31                                            SOCKET_ID_ANY);
32         if (pkt_pool == NULL) {
33                 printf("%s: Error creating pkt mempool\n", __func__);
34                 goto bad_setup;
35         }
36
37         direct_pool = rte_pktmbuf_pool_create("FRAG_D_MBUF_POOL",
38                                               NUM_MBUFS, BURST, 0,
39                                               RTE_MBUF_DEFAULT_BUF_SIZE,
40                                               SOCKET_ID_ANY);
41         if (direct_pool == NULL) {
42                 printf("%s: Error creating direct mempool\n", __func__);
43                 goto bad_setup;
44         }
45
46         indirect_pool = rte_pktmbuf_pool_create("FRAG_I_MBUF_POOL",
47                                                 NUM_MBUFS, BURST, 0,
48                                                 0, SOCKET_ID_ANY);
49         if (indirect_pool == NULL) {
50                 printf("%s: Error creating indirect mempool\n", __func__);
51                 goto bad_setup;
52         }
53
54         return TEST_SUCCESS;
55
56 bad_setup:
57         rte_mempool_free(pkt_pool);
58         pkt_pool = NULL;
59
60         rte_mempool_free(direct_pool);
61         direct_pool = NULL;
62
63         return TEST_FAILED;
64 }
65
66 static int testsuite_setup(void)
67 {
68         return setup_buf_pool();
69 }
70
71 static void testsuite_teardown(void)
72 {
73         rte_mempool_free(pkt_pool);
74         rte_mempool_free(direct_pool);
75         rte_mempool_free(indirect_pool);
76
77         pkt_pool = NULL;
78         direct_pool = NULL;
79         indirect_pool = NULL;
80 }
81
82 static int ut_setup(void)
83 {
84         return TEST_SUCCESS;
85 }
86
87 static void ut_teardown(void)
88 {
89 }
90
91 static 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)
94 {
95         /* Create a packet, 2k bytes long */
96         b->data_off = 0;
97         char *data = rte_pktmbuf_mtod(b, char *);
98
99         memset(data, fill, sizeof(struct rte_ipv4_hdr) + s);
100
101         struct rte_ipv4_hdr *hdr = (struct rte_ipv4_hdr *)data;
102
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;
110         if (df)
111                 hdr->fragment_offset = rte_cpu_to_be_16(0x4000);
112
113         if (!ttl)
114                 ttl = 64; /* default to 64 */
115
116         if (!proto)
117                 proto = 1; /* icmp */
118
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);
124 }
125
126 static void
127 v6_allocate_packet_of(struct rte_mbuf *b, int fill, size_t s, uint8_t ttl,
128                       uint8_t proto, uint16_t pktid)
129 {
130         /* Create a packet, 2k bytes long */
131         b->data_off = 0;
132         char *data = rte_pktmbuf_mtod(b, char *);
133
134         memset(data, fill, sizeof(struct rte_ipv6_hdr) + s);
135
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;
139
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);
143         hdr->proto = proto;
144         hdr->hop_limits = ttl;
145
146         memset(hdr->src_addr, 0x08, sizeof(hdr->src_addr));
147         memset(hdr->dst_addr, 0x04, sizeof(hdr->src_addr));
148 }
149
150 static inline void
151 test_free_fragments(struct rte_mbuf *mb[], uint32_t num)
152 {
153         uint32_t i;
154         for (i = 0; i < num; i++)
155                 rte_pktmbuf_free(mb[i]);
156 }
157
158 static int
159 test_ip_frag(void)
160 {
161         static const uint16_t RND_ID = UINT16_MAX;
162         int result = TEST_SUCCESS;
163         size_t i;
164
165         struct test_ip_frags {
166                 int      ipv;
167                 size_t   mtu_size;
168                 size_t   pkt_size;
169                 int      set_df;
170                 uint8_t  ttl;
171                 uint8_t  proto;
172                 uint16_t pkt_id;
173                 int      expected_frags;
174         } tests[] = {
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},
181
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},
186         };
187
188         for (i = 0; i < RTE_DIM(tests); i++) {
189                 int32_t len = 0;
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);
193
194                 RTE_TEST_ASSERT_NOT_EQUAL(b, NULL,
195                                           "Failed to allocate pkt.");
196
197                 if (tests[i].pkt_id == RND_ID)
198                         pktid = rte_rand_max(UINT16_MAX);
199
200                 if (tests[i].ipv == 4) {
201                         v4_allocate_packet_of(b, 0x41414141,
202                                               tests[i].pkt_size,
203                                               tests[i].set_df,
204                                               tests[i].ttl,
205                                               tests[i].proto,
206                                               pktid);
207                 } else if (tests[i].ipv == 6) {
208                         v6_allocate_packet_of(b, 0x41414141,
209                                               tests[i].pkt_size,
210                                               tests[i].ttl,
211                                               tests[i].proto,
212                                               pktid);
213                 }
214
215                 if (tests[i].ipv == 4)
216                         len = rte_ipv4_fragment_packet(b, pkts_out, BURST,
217                                                        tests[i].mtu_size,
218                                                        direct_pool,
219                                                        indirect_pool);
220                 else if (tests[i].ipv == 6)
221                         len = rte_ipv6_fragment_packet(b, pkts_out, BURST,
222                                                        tests[i].mtu_size,
223                                                        direct_pool,
224                                                        indirect_pool);
225
226                 rte_pktmbuf_free(b);
227
228                 if (len > 0)
229                         test_free_fragments(pkts_out, len);
230
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);
235
236         }
237
238         return result;
239 }
240
241 static struct unit_test_suite ipfrag_testsuite  = {
242         .suite_name = "IP Frag Unit Test Suite",
243         .setup = testsuite_setup,
244         .teardown = testsuite_teardown,
245         .unit_test_cases = {
246                 TEST_CASE_ST(ut_setup, ut_teardown,
247                              test_ip_frag),
248
249                 TEST_CASES_END() /**< NULL terminate unit test array */
250         }
251 };
252
253 static int
254 test_ipfrag(void)
255 {
256         rte_log_set_global_level(RTE_LOG_DEBUG);
257         rte_log_set_level(RTE_LOGTYPE_EAL, RTE_LOG_DEBUG);
258
259         return unit_test_suite_runner(&ipfrag_testsuite);
260 }
261
262 REGISTER_TEST_COMMAND(ipfrag_autotest, test_ipfrag);