net/mlx5: fix assertion on flags set in packet mbuf
[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,
93                       size_t s, int df, uint8_t mf, uint16_t off,
94                       uint8_t ttl, uint8_t proto, uint16_t pktid)
95 {
96         /* Create a packet, 2k bytes long */
97         b->data_off = 0;
98         char *data = rte_pktmbuf_mtod(b, char *);
99         rte_be16_t fragment_offset = 0; /**< fragmentation offset */
100
101         memset(data, fill, sizeof(struct rte_ipv4_hdr) + s);
102
103         struct rte_ipv4_hdr *hdr = (struct rte_ipv4_hdr *)data;
104
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);
111
112         if (df)
113                 fragment_offset |= 0x4000;
114
115         if (mf)
116                 fragment_offset |= 0x2000;
117
118         if (off)
119                 fragment_offset |= off;
120
121         hdr->fragment_offset = rte_cpu_to_be_16(fragment_offset);
122
123         if (!ttl)
124                 ttl = 64; /* default to 64 */
125
126         if (!proto)
127                 proto = 1; /* icmp */
128
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);
134 }
135
136 static void
137 v6_allocate_packet_of(struct rte_mbuf *b, int fill, size_t s, uint8_t ttl,
138                       uint8_t proto, uint16_t pktid)
139 {
140         /* Create a packet, 2k bytes long */
141         b->data_off = 0;
142         char *data = rte_pktmbuf_mtod(b, char *);
143
144         memset(data, fill, sizeof(struct rte_ipv6_hdr) + s);
145
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;
149
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);
153         hdr->proto = proto;
154         hdr->hop_limits = ttl;
155
156         memset(hdr->src_addr, 0x08, sizeof(hdr->src_addr));
157         memset(hdr->dst_addr, 0x04, sizeof(hdr->src_addr));
158 }
159
160 static inline void
161 test_free_fragments(struct rte_mbuf *mb[], uint32_t num)
162 {
163         uint32_t i;
164         for (i = 0; i < num; i++)
165                 rte_pktmbuf_free(mb[i]);
166 }
167
168 static inline void
169 test_get_offset(struct rte_mbuf **mb, int32_t len,
170         uint16_t *offset, int ipv)
171 {
172         int32_t i;
173
174         for (i = 0; i < len; i++) {
175                 if (ipv == 4) {
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(
182                                         mb[i],
183                                         struct ipv6_extension_fragment *,
184                                         sizeof(struct rte_ipv6_hdr));
185                         offset[i] = fh->frag_data;
186                 }
187         }
188 }
189
190 static int
191 test_ip_frag(void)
192 {
193         static const uint16_t RND_ID = UINT16_MAX;
194         int result = TEST_SUCCESS;
195         size_t i, j;
196
197         struct test_ip_frags {
198                 int      ipv;
199                 size_t   mtu_size;
200                 size_t   pkt_size;
201                 int      set_df;
202                 uint8_t  set_mf;
203                 uint16_t set_of;
204                 uint8_t  ttl;
205                 uint8_t  proto;
206                 uint16_t pkt_id;
207                 int      expected_frags;
208                 uint16_t expected_fragment_offset[BURST];
209         } tests[] = {
210                  {4, 1280, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID,       2,
211                   {0x2000, 0x009D}},
212                  {4, 1280, 1400, 0, 0, 0, 64, IPPROTO_ICMP, 0,            2,
213                   {0x2000, 0x009D}},
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}},
222
223                  {6, 1280, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID,       2,
224                   {0x0001, 0x04D0}},
225                  {6, 1300, 1400, 0, 0, 0, 64, IPPROTO_ICMP, RND_ID,       2,
226                   {0x0001, 0x04E0}},
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,
229                   {0x0001, 0x04E0}},
230         };
231
232         for (i = 0; i < RTE_DIM(tests); i++) {
233                 int32_t len = 0;
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);
238
239                 RTE_TEST_ASSERT_NOT_EQUAL(b, NULL,
240                                           "Failed to allocate pkt.");
241
242                 if (tests[i].pkt_id == RND_ID)
243                         pktid = rte_rand_max(UINT16_MAX);
244
245                 if (tests[i].ipv == 4) {
246                         v4_allocate_packet_of(b, 0x41414141,
247                                               tests[i].pkt_size,
248                                               tests[i].set_df,
249                                               tests[i].set_mf,
250                                               tests[i].set_of,
251                                               tests[i].ttl,
252                                               tests[i].proto,
253                                               pktid);
254                 } else if (tests[i].ipv == 6) {
255                         v6_allocate_packet_of(b, 0x41414141,
256                                               tests[i].pkt_size,
257                                               tests[i].ttl,
258                                               tests[i].proto,
259                                               pktid);
260                 }
261
262                 if (tests[i].ipv == 4)
263                         len = rte_ipv4_fragment_packet(b, pkts_out, BURST,
264                                                        tests[i].mtu_size,
265                                                        direct_pool,
266                                                        indirect_pool);
267                 else if (tests[i].ipv == 6)
268                         len = rte_ipv6_fragment_packet(b, pkts_out, BURST,
269                                                        tests[i].mtu_size,
270                                                        direct_pool,
271                                                        indirect_pool);
272
273                 rte_pktmbuf_free(b);
274
275                 if (len > 0) {
276                         test_get_offset(pkts_out, len,
277                             fragment_offset, tests[i].ipv);
278                         test_free_fragments(pkts_out, len);
279                 }
280
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);
285
286                 if (len > 0) {
287                         for (j = 0; j < (size_t)len; j++) {
288                                 printf("%zd-%zd: checking %d with %d\n",
289                                     i, j, fragment_offset[j],
290                                     rte_cpu_to_be_16(
291                                         tests[i].expected_fragment_offset[j]));
292                                 RTE_TEST_ASSERT_EQUAL(fragment_offset[j],
293                                     rte_cpu_to_be_16(
294                                         tests[i].expected_fragment_offset[j]),
295                                     "Failed case %zd.\n", i);
296                         }
297                 }
298
299         }
300
301         return result;
302 }
303
304 static struct unit_test_suite ipfrag_testsuite  = {
305         .suite_name = "IP Frag Unit Test Suite",
306         .setup = testsuite_setup,
307         .teardown = testsuite_teardown,
308         .unit_test_cases = {
309                 TEST_CASE_ST(ut_setup, ut_teardown,
310                              test_ip_frag),
311
312                 TEST_CASES_END() /**< NULL terminate unit test array */
313         }
314 };
315
316 static int
317 test_ipfrag(void)
318 {
319         rte_log_set_global_level(RTE_LOG_DEBUG);
320         rte_log_set_level(RTE_LOGTYPE_EAL, RTE_LOG_DEBUG);
321
322         return unit_test_suite_runner(&ipfrag_testsuite);
323 }
324
325 REGISTER_TEST_COMMAND(ipfrag_autotest, test_ipfrag);