1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
5 #ifndef RTE_EXEC_ENV_WINDOWS
8 #include <rte_pipeline.h>
11 #include <rte_hexdump.h>
12 #include "test_table.h"
13 #include "test_table_pipeline.h"
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);
25 rte_pipeline_port_out_action_handler port_action_0x00(struct rte_mbuf **pkts,
33 printf("Port Action 0x00\n");
38 rte_pipeline_port_out_action_handler port_action_0xFF(struct rte_mbuf **pkts,
46 printf("Port Action 0xFF\n");
51 rte_pipeline_port_out_action_handler port_action_stub(struct rte_mbuf **pkts,
58 RTE_SET_USED(pkts_mask);
60 printf("Port Action stub\n");
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);
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);
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);
78 rte_pipeline_table_action_handler_hit
79 table_action_0x00(__rte_unused struct rte_pipeline *p,
80 __rte_unused struct rte_mbuf **pkts,
82 __rte_unused struct rte_pipeline_table_entry **entry,
83 __rte_unused void *arg)
85 printf("Table Action, setting pkts_mask to 0x00\n");
87 rte_pipeline_ah_packet_drop(p, pkts_mask);
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,
95 __rte_unused struct rte_pipeline_table_entry **entry,
96 __rte_unused void *arg)
98 printf("STUB Table Action Hit - doing nothing\n");
99 printf("STUB Table Action Hit - setting mask to 0x%"PRIx64"\n",
101 pkts_mask = (~override_hit_mask) & 0x3;
102 rte_pipeline_ah_packet_drop(p, pkts_mask);
107 table_action_stub_miss(struct rte_pipeline *p,
108 __rte_unused struct rte_mbuf **pkts,
110 __rte_unused struct rte_pipeline_table_entry *entry,
111 __rte_unused void *arg)
113 printf("STUB Table Action Miss - setting mask to 0x%"PRIx64"\n",
115 pkts_mask = (~override_miss_mask) & 0x3;
116 rte_pipeline_ah_packet_drop(p, pkts_mask);
132 char pipeline_test_names[][64] = {
147 cleanup_pipeline(void)
150 rte_pipeline_free(p);
156 static int check_pipeline_invalid_params(void);
159 check_pipeline_invalid_params(void)
161 struct rte_pipeline_params pipeline_params_1 = {
165 struct rte_pipeline_params pipeline_params_2 = {
169 struct rte_pipeline_params pipeline_params_3 = {
174 p = rte_pipeline_create(NULL);
176 RTE_LOG(INFO, PIPELINE,
177 "%s: configured pipeline with null params\n",
181 p = rte_pipeline_create(&pipeline_params_1);
183 RTE_LOG(INFO, PIPELINE, "%s: Configure pipeline with NULL "
188 p = rte_pipeline_create(&pipeline_params_2);
190 RTE_LOG(INFO, PIPELINE, "%s: Configure pipeline with invalid "
191 "socket\n", __func__);
195 if (rte_eal_has_hugepages()) {
196 p = rte_pipeline_create(&pipeline_params_3);
198 RTE_LOG(INFO, PIPELINE, "%s: Configure pipeline with "
199 "invalid socket\n", __func__);
204 /* Check pipeline consistency */
205 if (!rte_pipeline_check(p)) {
206 rte_panic("Pipeline consistency reported as OK\n");
218 setup_pipeline(int test_type)
222 struct rte_pipeline_params pipeline_params = {
227 RTE_LOG(INFO, PIPELINE, "%s: **** Setting up %s test\n",
228 __func__, pipeline_test_names[test_type]);
230 /* Pipeline configuration */
231 p = rte_pipeline_create(&pipeline_params);
233 RTE_LOG(INFO, PIPELINE, "%s: Failed to configure pipeline\n",
238 ret = rte_pipeline_free(p);
240 RTE_LOG(INFO, PIPELINE, "%s: Failed to free pipeline\n",
245 /* Pipeline configuration */
246 p = rte_pipeline_create(&pipeline_params);
248 RTE_LOG(INFO, PIPELINE, "%s: Failed to configure pipeline\n",
254 /* Input port configuration */
255 for (i = 0; i < N_PORTS; i++) {
256 struct rte_port_ring_reader_params port_ring_params = {
260 struct rte_pipeline_port_in_params port_params = {
261 .ops = &rte_port_ring_reader_ops,
262 .arg_create = (void *) &port_ring_params,
264 .burst_size = BURST_SIZE,
267 /* Put in action for some ports */
269 port_params.f_action = NULL;
271 ret = rte_pipeline_port_in_create(p, &port_params,
274 rte_panic("Unable to configure input port %d, ret:%d\n",
280 /* output Port configuration */
281 for (i = 0; i < N_PORTS; i++) {
282 struct rte_port_ring_writer_params port_ring_params = {
284 .tx_burst_sz = BURST_SIZE,
287 struct rte_pipeline_port_out_params port_params = {
288 .ops = &rte_port_ring_writer_ops,
289 .arg_create = (void *) &port_ring_params,
295 port_params.f_action = port_out_action;
297 if (rte_pipeline_port_out_create(p, &port_params,
299 rte_panic("Unable to configure output port %d\n", i);
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,
309 .f_action_hit = action_handler_hit,
310 .f_action_miss = action_handler_miss,
311 .action_data_size = 0,
314 if (rte_pipeline_table_create(p, &table_params, &table_id[i])) {
315 rte_panic("Unable to configure table %u\n", i);
319 if (connect_miss_action_to_table)
320 if (rte_pipeline_table_create(p, &table_params,
322 rte_panic("Unable to configure table %u\n", i);
327 for (i = 0; i < N_PORTS; i++)
328 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
330 rte_panic("Unable to connect input port %u to "
331 "table %u\n", port_in_id[i], table_id[i]);
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]},
342 struct rte_pipeline_table_entry *default_entry_ptr;
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];
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);
354 rte_panic("Unable to add default entry to table %u "
355 "code %d\n", table_id[i], ret);
358 printf("Added default entry to table id %d with "
360 table_id[i], default_entry.action);
362 if (connect_miss_action_to_table) {
363 /* We create a second table so the first can pass
365 struct rte_pipeline_table_entry default_entry = {
366 .action = RTE_PIPELINE_ACTION_PORT,
367 {.port_id = port_out_id[i^1]},
369 printf("Setting second table to output to port\n");
371 /* Add the default action for the table. */
372 ret = rte_pipeline_table_default_entry_add(p,
374 &default_entry, &default_entry_ptr);
376 rte_panic("Unable to add default entry to "
377 "table %u code %d\n",
381 printf("Added default entry to table id %d "
383 table_id[i], default_entry.action);
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",
393 /* Check pipeline consistency */
394 if (rte_pipeline_check(p) < 0) {
395 rte_panic("Pipeline consistency check failed\n");
398 printf("Pipeline Consistency OK!\n");
407 test_pipeline_single_filter(int test_type, int expected_count)
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++)
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",
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++) {
437 m = rte_pktmbuf_alloc(pool);
439 rte_panic("Failed to alloc mbuf from pool\n");
442 key = RTE_MBUF_METADATA_UINT8_PTR(m,
443 APP_METADATA_OFFSET(32));
445 k32 = (uint32_t *) key;
446 k32[0] = 0xadadadad >> (j % 2);
448 RTE_LOG(INFO, PIPELINE, "%s: Enqueue onto ring %d\n",
450 rte_ring_enqueue(rings_rx[i], m);
453 /* Run pipeline once */
454 for (i = 0; i < N_PORTS; i++)
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);
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.
468 for (i = 0; i < N_PORTS; i++) {
469 void *objs[RING_TX_SIZE];
470 struct rte_mbuf *mbuf;
472 ret = rte_ring_sc_dequeue_burst(rings_tx[i], objs, 10, NULL);
474 printf("Got no objects from ring %d - error code %d\n",
477 printf("Got %d object(s) from ring %d!\n", ret, i);
478 for (j = 0; j < ret; j++) {
480 rte_hexdump(stdout, "Object:",
481 rte_pktmbuf_mtod(mbuf, char *),
483 rte_pktmbuf_free(mbuf);
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);
506 test_table_pipeline(void)
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)
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)
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)
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)
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)
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
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)
555 connect_miss_action_to_table = 0;
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)
564 connect_miss_action_to_table = 0;
566 if (check_pipeline_invalid_params()) {
567 RTE_LOG(INFO, PIPELINE, "%s: Check pipeline invalid params "
568 "failed.\n", __func__);
575 #endif /* !RTE_EXEC_ENV_WINDOWS */