ip_frag: add IPv4 options fragment
[dpdk.git] / app / test / test_table_pipeline.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #ifndef RTE_EXEC_ENV_WINDOWS
6
7 #include <string.h>
8 #include <rte_pipeline.h>
9 #include <rte_log.h>
10 #include <inttypes.h>
11 #include <rte_hexdump.h>
12 #include "test_table.h"
13 #include "test_table_pipeline.h"
14
15 #if 0
16
17 static rte_pipeline_port_out_action_handler port_action_0x00
18         (struct rte_mbuf **pkts, uint32_t n, uint64_t *pkts_mask, void *arg);
19 static rte_pipeline_port_out_action_handler port_action_0xFF
20         (struct rte_mbuf **pkts, uint32_t n, uint64_t *pkts_mask, void *arg);
21 static rte_pipeline_port_out_action_handler port_action_stub
22         (struct rte_mbuf **pkts, uint32_t n, uint64_t *pkts_mask, void *arg);
23
24
25 rte_pipeline_port_out_action_handler port_action_0x00(struct rte_mbuf **pkts,
26         uint32_t n,
27         uint64_t *pkts_mask,
28         void *arg)
29 {
30         RTE_SET_USED(pkts);
31         RTE_SET_USED(n);
32         RTE_SET_USED(arg);
33         printf("Port Action 0x00\n");
34         *pkts_mask = 0x00;
35         return 0;
36 }
37
38 rte_pipeline_port_out_action_handler port_action_0xFF(struct rte_mbuf **pkts,
39         uint32_t n,
40         uint64_t *pkts_mask,
41         void *arg)
42 {
43         RTE_SET_USED(pkts);
44         RTE_SET_USED(n);
45         RTE_SET_USED(arg);
46         printf("Port Action 0xFF\n");
47         *pkts_mask = 0xFF;
48         return 0;
49 }
50
51 rte_pipeline_port_out_action_handler port_action_stub(struct rte_mbuf **pkts,
52         uint32_t n,
53         uint64_t *pkts_mask,
54         void *arg)
55 {
56         RTE_SET_USED(pkts);
57         RTE_SET_USED(n);
58         RTE_SET_USED(pkts_mask);
59         RTE_SET_USED(arg);
60         printf("Port Action stub\n");
61         return 0;
62 }
63
64 #endif
65
66 rte_pipeline_table_action_handler_hit
67 table_action_0x00(struct rte_pipeline *p, struct rte_mbuf **pkts,
68         uint64_t pkts_mask, struct rte_pipeline_table_entry **entry, void *arg);
69
70 rte_pipeline_table_action_handler_hit
71 table_action_stub_hit(struct rte_pipeline *p, struct rte_mbuf **pkts,
72         uint64_t pkts_mask, struct rte_pipeline_table_entry **entry, void *arg);
73
74 static int
75 table_action_stub_miss(struct rte_pipeline *p, struct rte_mbuf **pkts,
76         uint64_t pkts_mask, struct rte_pipeline_table_entry *entry, void *arg);
77
78 rte_pipeline_table_action_handler_hit
79 table_action_0x00(__rte_unused struct rte_pipeline *p,
80         __rte_unused struct rte_mbuf **pkts,
81         uint64_t pkts_mask,
82         __rte_unused struct rte_pipeline_table_entry **entry,
83         __rte_unused void *arg)
84 {
85         printf("Table Action, setting pkts_mask to 0x00\n");
86         pkts_mask = ~0x00;
87         rte_pipeline_ah_packet_drop(p, pkts_mask);
88         return 0;
89 }
90
91 rte_pipeline_table_action_handler_hit
92 table_action_stub_hit(__rte_unused struct rte_pipeline *p,
93         __rte_unused struct rte_mbuf **pkts,
94         uint64_t pkts_mask,
95         __rte_unused struct rte_pipeline_table_entry **entry,
96         __rte_unused void *arg)
97 {
98         printf("STUB Table Action Hit - doing nothing\n");
99         printf("STUB Table Action Hit - setting mask to 0x%"PRIx64"\n",
100                 override_hit_mask);
101         pkts_mask = (~override_hit_mask) & 0x3;
102         rte_pipeline_ah_packet_drop(p, pkts_mask);
103         return 0;
104 }
105
106 static int
107 table_action_stub_miss(struct rte_pipeline *p,
108         __rte_unused struct rte_mbuf **pkts,
109         uint64_t pkts_mask,
110         __rte_unused struct rte_pipeline_table_entry *entry,
111         __rte_unused void *arg)
112 {
113         printf("STUB Table Action Miss - setting mask to 0x%"PRIx64"\n",
114                 override_miss_mask);
115         pkts_mask = (~override_miss_mask) & 0x3;
116         rte_pipeline_ah_packet_drop(p, pkts_mask);
117         return 0;
118 }
119
120 enum e_test_type {
121         e_TEST_STUB = 0,
122         e_TEST_LPM,
123         e_TEST_LPM6,
124         e_TEST_HASH_LRU_8,
125         e_TEST_HASH_LRU_16,
126         e_TEST_HASH_LRU_32,
127         e_TEST_HASH_EXT_8,
128         e_TEST_HASH_EXT_16,
129         e_TEST_HASH_EXT_32
130 };
131
132 char pipeline_test_names[][64] = {
133         "Stub",
134         "LPM",
135         "LPMv6",
136         "8-bit LRU Hash",
137         "16-bit LRU Hash",
138         "32-bit LRU Hash",
139         "16-bit Ext Hash",
140         "8-bit Ext Hash",
141         "32-bit Ext Hash",
142         ""
143 };
144
145
146 static int
147 cleanup_pipeline(void)
148 {
149
150         rte_pipeline_free(p);
151
152         return 0;
153 }
154
155
156 static int check_pipeline_invalid_params(void);
157
158 static int
159 check_pipeline_invalid_params(void)
160 {
161         struct rte_pipeline_params pipeline_params_1 = {
162                 .name = NULL,
163                 .socket_id = 0,
164         };
165         struct rte_pipeline_params pipeline_params_2 = {
166                 .name = "PIPELINE",
167                 .socket_id = -1,
168         };
169         struct rte_pipeline_params pipeline_params_3 = {
170                 .name = "PIPELINE",
171                 .socket_id = 127,
172         };
173
174         p = rte_pipeline_create(NULL);
175         if (p != NULL) {
176                 RTE_LOG(INFO, PIPELINE,
177                         "%s: configured pipeline with null params\n",
178                         __func__);
179                 goto fail;
180         }
181         p = rte_pipeline_create(&pipeline_params_1);
182         if (p != NULL) {
183                 RTE_LOG(INFO, PIPELINE, "%s: Configure pipeline with NULL "
184                         "name\n", __func__);
185                 goto fail;
186         }
187
188         p = rte_pipeline_create(&pipeline_params_2);
189         if (p != NULL) {
190                 RTE_LOG(INFO, PIPELINE, "%s: Configure pipeline with invalid "
191                         "socket\n", __func__);
192                 goto fail;
193         }
194
195         if (rte_eal_has_hugepages()) {
196                 p = rte_pipeline_create(&pipeline_params_3);
197                 if (p != NULL) {
198                         RTE_LOG(INFO, PIPELINE, "%s: Configure pipeline with "
199                                 "invalid socket\n", __func__);
200                         goto fail;
201                 }
202         }
203
204         /* Check pipeline consistency */
205         if (!rte_pipeline_check(p)) {
206                 rte_panic("Pipeline consistency reported as OK\n");
207                 goto fail;
208         }
209
210
211         return 0;
212 fail:
213         return -1;
214 }
215
216
217 static int
218 setup_pipeline(int test_type)
219 {
220         int ret;
221         int i;
222         struct rte_pipeline_params pipeline_params = {
223                 .name = "PIPELINE",
224                 .socket_id = 0,
225         };
226
227         RTE_LOG(INFO, PIPELINE, "%s: **** Setting up %s test\n",
228                 __func__, pipeline_test_names[test_type]);
229
230         /* Pipeline configuration */
231         p = rte_pipeline_create(&pipeline_params);
232         if (p == NULL) {
233                 RTE_LOG(INFO, PIPELINE, "%s: Failed to configure pipeline\n",
234                         __func__);
235                 goto fail;
236         }
237
238         ret = rte_pipeline_free(p);
239         if (ret != 0) {
240                 RTE_LOG(INFO, PIPELINE, "%s: Failed to free pipeline\n",
241                         __func__);
242                 goto fail;
243         }
244
245         /* Pipeline configuration */
246         p = rte_pipeline_create(&pipeline_params);
247         if (p == NULL) {
248                 RTE_LOG(INFO, PIPELINE, "%s: Failed to configure pipeline\n",
249                         __func__);
250                 goto fail;
251         }
252
253
254         /* Input port configuration */
255         for (i = 0; i < N_PORTS; i++) {
256                 struct rte_port_ring_reader_params port_ring_params = {
257                         .ring = rings_rx[i],
258                 };
259
260                 struct rte_pipeline_port_in_params port_params = {
261                         .ops = &rte_port_ring_reader_ops,
262                         .arg_create = (void *) &port_ring_params,
263                         .f_action = NULL,
264                         .burst_size = BURST_SIZE,
265                 };
266
267                 /* Put in action for some ports */
268                 if (i)
269                         port_params.f_action = NULL;
270
271                 ret = rte_pipeline_port_in_create(p, &port_params,
272                         &port_in_id[i]);
273                 if (ret) {
274                         rte_panic("Unable to configure input port %d, ret:%d\n",
275                                 i, ret);
276                         goto fail;
277                 }
278         }
279
280         /* output Port configuration */
281         for (i = 0; i < N_PORTS; i++) {
282                 struct rte_port_ring_writer_params port_ring_params = {
283                         .ring = rings_tx[i],
284                         .tx_burst_sz = BURST_SIZE,
285                 };
286
287                 struct rte_pipeline_port_out_params port_params = {
288                         .ops = &rte_port_ring_writer_ops,
289                         .arg_create = (void *) &port_ring_params,
290                         .f_action = NULL,
291                         .arg_ah = NULL,
292                 };
293
294                 if (i)
295                         port_params.f_action = port_out_action;
296
297                 if (rte_pipeline_port_out_create(p, &port_params,
298                         &port_out_id[i])) {
299                         rte_panic("Unable to configure output port %d\n", i);
300                         goto fail;
301                 }
302         }
303
304         /* Table configuration  */
305         for (i = 0; i < N_PORTS; i++) {
306                 struct rte_pipeline_table_params table_params = {
307                                 .ops = &rte_table_stub_ops,
308                                 .arg_create = NULL,
309                                 .f_action_hit = action_handler_hit,
310                                 .f_action_miss = action_handler_miss,
311                                 .action_data_size = 0,
312                 };
313
314                 if (rte_pipeline_table_create(p, &table_params, &table_id[i])) {
315                         rte_panic("Unable to configure table %u\n", i);
316                         goto fail;
317                 }
318
319                 if (connect_miss_action_to_table)
320                         if (rte_pipeline_table_create(p, &table_params,
321                                 &table_id[i+2])) {
322                                 rte_panic("Unable to configure table %u\n", i);
323                                 goto fail;
324                         }
325         }
326
327         for (i = 0; i < N_PORTS; i++)
328                 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
329                         table_id[i])) {
330                         rte_panic("Unable to connect input port %u to "
331                                 "table %u\n", port_in_id[i],  table_id[i]);
332                         goto fail;
333                 }
334
335         /* Add entries to tables */
336         for (i = 0; i < N_PORTS; i++) {
337                 struct rte_pipeline_table_entry default_entry = {
338                         .action = (enum rte_pipeline_action)
339                                 table_entry_default_action,
340                         {.port_id = port_out_id[i^1]},
341                 };
342                 struct rte_pipeline_table_entry *default_entry_ptr;
343
344                 if (connect_miss_action_to_table) {
345                         printf("Setting first table to output to next table\n");
346                         default_entry.action = RTE_PIPELINE_ACTION_TABLE;
347                         default_entry.table_id = table_id[i+2];
348                 }
349
350                 /* Add the default action for the table. */
351                 ret = rte_pipeline_table_default_entry_add(p, table_id[i],
352                         &default_entry, &default_entry_ptr);
353                 if (ret < 0) {
354                         rte_panic("Unable to add default entry to table %u "
355                                 "code %d\n", table_id[i], ret);
356                         goto fail;
357                 } else
358                         printf("Added default entry to table id %d with "
359                                 "action %x\n",
360                                 table_id[i], default_entry.action);
361
362                 if (connect_miss_action_to_table) {
363                         /* We create a second table so the first can pass
364                         traffic into it */
365                         struct rte_pipeline_table_entry default_entry = {
366                                 .action = RTE_PIPELINE_ACTION_PORT,
367                                 {.port_id = port_out_id[i^1]},
368                         };
369                         printf("Setting second table to output to port\n");
370
371                         /* Add the default action for the table. */
372                         ret = rte_pipeline_table_default_entry_add(p,
373                                 table_id[i+2],
374                                 &default_entry, &default_entry_ptr);
375                         if (ret < 0) {
376                                 rte_panic("Unable to add default entry to "
377                                         "table %u code %d\n",
378                                         table_id[i], ret);
379                                 goto fail;
380                         } else
381                                 printf("Added default entry to table id %d "
382                                         "with action %x\n",
383                                         table_id[i], default_entry.action);
384                 }
385         }
386
387         /* Enable input ports */
388         for (i = 0; i < N_PORTS ; i++)
389                 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
390                         rte_panic("Unable to enable input port %u\n",
391                                 port_in_id[i]);
392
393         /* Check pipeline consistency */
394         if (rte_pipeline_check(p) < 0) {
395                 rte_panic("Pipeline consistency check failed\n");
396                 goto fail;
397         } else
398                 printf("Pipeline Consistency OK!\n");
399
400         return 0;
401 fail:
402
403         return -1;
404 }
405
406 static int
407 test_pipeline_single_filter(int test_type, int expected_count)
408 {
409         int i;
410         int j;
411         int ret;
412         int tx_count;
413
414         RTE_LOG(INFO, PIPELINE, "%s: **** Running %s test\n",
415                 __func__, pipeline_test_names[test_type]);
416         /* Run pipeline once */
417         for (i = 0; i < N_PORTS; i++)
418                 rte_pipeline_run(p);
419
420
421         ret = rte_pipeline_flush(NULL);
422         if (ret != -EINVAL) {
423                 RTE_LOG(INFO, PIPELINE,
424                         "%s: No pipeline flush error NULL pipeline (%d)\n",
425                         __func__, ret);
426                 goto fail;
427         }
428
429         /*
430          * Allocate a few mbufs and manually insert into the rings. */
431         for (i = 0; i < N_PORTS; i++)
432                 for (j = 0; j < N_PORTS; j++) {
433                         struct rte_mbuf *m;
434                         uint8_t *key;
435                         uint32_t *k32;
436
437                         m = rte_pktmbuf_alloc(pool);
438                         if (m == NULL) {
439                                 rte_panic("Failed to alloc mbuf from pool\n");
440                                 return -1;
441                         }
442                         key = RTE_MBUF_METADATA_UINT8_PTR(m,
443                                         APP_METADATA_OFFSET(32));
444
445                         k32 = (uint32_t *) key;
446                         k32[0] = 0xadadadad >> (j % 2);
447
448                         RTE_LOG(INFO, PIPELINE, "%s: Enqueue onto ring %d\n",
449                                 __func__, i);
450                         rte_ring_enqueue(rings_rx[i], m);
451                 }
452
453         /* Run pipeline once */
454         for (i = 0; i < N_PORTS; i++)
455                 rte_pipeline_run(p);
456
457    /*
458         * need to flush the pipeline, as there may be less hits than the burst
459         size and they will not have been flushed to the tx rings. */
460         rte_pipeline_flush(p);
461
462    /*
463         * Now we'll see what we got back on the tx rings. We should see whatever
464         * packets we had hits on that were destined for the output ports.
465         */
466         tx_count = 0;
467
468         for (i = 0; i < N_PORTS; i++) {
469                 void *objs[RING_TX_SIZE];
470                 struct rte_mbuf *mbuf;
471
472                 ret = rte_ring_sc_dequeue_burst(rings_tx[i], objs, 10, NULL);
473                 if (ret <= 0)
474                         printf("Got no objects from ring %d - error code %d\n",
475                                 i, ret);
476                 else {
477                         printf("Got %d object(s) from ring %d!\n", ret, i);
478                         for (j = 0; j < ret; j++) {
479                                 mbuf = objs[j];
480                                 rte_hexdump(stdout, "Object:",
481                                         rte_pktmbuf_mtod(mbuf, char *),
482                                         mbuf->data_len);
483                                 rte_pktmbuf_free(mbuf);
484                         }
485                         tx_count += ret;
486                 }
487         }
488
489         if (tx_count != expected_count) {
490                 RTE_LOG(INFO, PIPELINE,
491                         "%s: Unexpected packets out for %s test, expected %d, "
492                         "got %d\n", __func__, pipeline_test_names[test_type],
493                         expected_count, tx_count);
494                 goto fail;
495         }
496
497         cleanup_pipeline();
498
499         return 0;
500 fail:
501         return -1;
502
503 }
504
505 int
506 test_table_pipeline(void)
507 {
508         /* TEST - All packets dropped */
509         action_handler_hit = NULL;
510         action_handler_miss = NULL;
511         table_entry_default_action = RTE_PIPELINE_ACTION_DROP;
512         setup_pipeline(e_TEST_STUB);
513         if (test_pipeline_single_filter(e_TEST_STUB, 0) < 0)
514                 return -1;
515
516         /* TEST - All packets passed through */
517         table_entry_default_action = RTE_PIPELINE_ACTION_PORT;
518         setup_pipeline(e_TEST_STUB);
519         if (test_pipeline_single_filter(e_TEST_STUB, 4) < 0)
520                 return -1;
521
522         /* TEST - one packet per port */
523         action_handler_hit = NULL;
524         action_handler_miss = table_action_stub_miss;
525         table_entry_default_action = RTE_PIPELINE_ACTION_PORT;
526         override_miss_mask = 0x01; /* one packet per port */
527         setup_pipeline(e_TEST_STUB);
528         if (test_pipeline_single_filter(e_TEST_STUB, 2) < 0)
529                 return -1;
530
531         /* TEST - one packet per port */
532         override_miss_mask = 0x02; /*all per port */
533         setup_pipeline(e_TEST_STUB);
534         if (test_pipeline_single_filter(e_TEST_STUB, 2) < 0)
535                 return -1;
536
537         /* TEST - all packets per port */
538         override_miss_mask = 0x03; /*all per port */
539         setup_pipeline(e_TEST_STUB);
540         if (test_pipeline_single_filter(e_TEST_STUB, 4) < 0)
541                 return -1;
542
543    /*
544         * This test will set up two tables in the pipeline. the first table
545         * will forward to another table on miss, and the second table will
546         * forward to port.
547         */
548         connect_miss_action_to_table = 1;
549         table_entry_default_action = RTE_PIPELINE_ACTION_TABLE;
550         action_handler_hit = NULL;  /* not for stub, hitmask always zero */
551         action_handler_miss = NULL;
552         setup_pipeline(e_TEST_STUB);
553         if (test_pipeline_single_filter(e_TEST_STUB, 4) < 0)
554                 return -1;
555         connect_miss_action_to_table = 0;
556
557         printf("TEST - two tables, hitmask override to 0x01\n");
558         connect_miss_action_to_table = 1;
559         action_handler_miss = table_action_stub_miss;
560         override_miss_mask = 0x01;
561         setup_pipeline(e_TEST_STUB);
562         if (test_pipeline_single_filter(e_TEST_STUB, 2) < 0)
563                 return -1;
564         connect_miss_action_to_table = 0;
565
566         if (check_pipeline_invalid_params()) {
567                 RTE_LOG(INFO, PIPELINE, "%s: Check pipeline invalid params "
568                         "failed.\n", __func__);
569                 return -1;
570         }
571
572         return 0;
573 }
574
575 #endif /* !RTE_EXEC_ENV_WINDOWS */