4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef RTE_LIBRTE_PIPELINE
41 #include <rte_pipeline.h>
44 #include <rte_hexdump.h>
45 #include "test_table.h"
46 #include "test_table_pipeline.h"
48 #define RTE_CBUF_UINT8_PTR(cbuf, offset) \
50 #define RTE_CBUF_UINT32_PTR(cbuf, offset) \
51 (&cbuf->data32[offset/sizeof(uint32_t)])
55 static rte_pipeline_port_out_action_handler port_action_0x00
56 (struct rte_mbuf **pkts, uint32_t n, uint64_t *pkts_mask, void *arg);
57 static rte_pipeline_port_out_action_handler port_action_0xFF
58 (struct rte_mbuf **pkts, uint32_t n, uint64_t *pkts_mask, void *arg);
59 static rte_pipeline_port_out_action_handler port_action_stub
60 (struct rte_mbuf **pkts, uint32_t n, uint64_t *pkts_mask, void *arg);
63 rte_pipeline_port_out_action_handler port_action_0x00(struct rte_mbuf **pkts,
71 printf("Port Action 0x00\n");
76 rte_pipeline_port_out_action_handler port_action_0xFF(struct rte_mbuf **pkts,
84 printf("Port Action 0xFF\n");
89 rte_pipeline_port_out_action_handler port_action_stub(struct rte_mbuf **pkts,
96 RTE_SET_USED(pkts_mask);
98 printf("Port Action stub\n");
104 rte_pipeline_table_action_handler_hit
105 table_action_0x00(struct rte_mbuf **pkts, uint64_t *pkts_mask,
106 struct rte_pipeline_table_entry **actions, uint32_t action_mask);
108 rte_pipeline_table_action_handler_hit
109 table_action_stub_hit(struct rte_mbuf **pkts, uint64_t *pkts_mask,
110 struct rte_pipeline_table_entry **actions, uint32_t action_mask);
112 rte_pipeline_table_action_handler_miss
113 table_action_stub_miss(struct rte_mbuf **pkts, uint64_t *pkts_mask,
114 struct rte_pipeline_table_entry *action, uint32_t action_mask);
116 rte_pipeline_table_action_handler_hit
117 table_action_0x00(__attribute__((unused)) struct rte_mbuf **pkts,
119 __attribute__((unused)) struct rte_pipeline_table_entry **actions,
120 __attribute__((unused)) uint32_t action_mask)
122 printf("Table Action, setting pkts_mask to 0x00\n");
127 rte_pipeline_table_action_handler_hit
128 table_action_stub_hit(__attribute__((unused)) struct rte_mbuf **pkts,
130 __attribute__((unused)) struct rte_pipeline_table_entry **actions,
131 __attribute__((unused)) uint32_t action_mask)
133 printf("STUB Table Action Hit - doing nothing\n");
134 printf("STUB Table Action Hit - setting mask to 0x%"PRIx64"\n",
136 *pkts_mask = override_hit_mask;
139 rte_pipeline_table_action_handler_miss
140 table_action_stub_miss(__attribute__((unused)) struct rte_mbuf **pkts,
142 __attribute__((unused)) struct rte_pipeline_table_entry *action,
143 __attribute__((unused)) uint32_t action_mask)
145 printf("STUB Table Action Miss - setting mask to 0x%"PRIx64"\n",
147 *pkts_mask = override_miss_mask;
164 char pipeline_test_names[][64] = {
179 cleanup_pipeline(void)
182 rte_pipeline_free(p);
188 static int check_pipeline_invalid_params(void);
191 check_pipeline_invalid_params(void)
193 struct rte_pipeline_params pipeline_params_1 = {
197 struct rte_pipeline_params pipeline_params_2 = {
201 struct rte_pipeline_params pipeline_params_3 = {
206 p = rte_pipeline_create(NULL);
208 RTE_LOG(INFO, PIPELINE,
209 "%s: configured pipeline with null params\n",
213 p = rte_pipeline_create(&pipeline_params_1);
215 RTE_LOG(INFO, PIPELINE, "%s: Configure pipeline with NULL "
220 p = rte_pipeline_create(&pipeline_params_2);
222 RTE_LOG(INFO, PIPELINE, "%s: Configure pipeline with invalid "
223 "socket\n", __func__);
227 p = rte_pipeline_create(&pipeline_params_3);
229 RTE_LOG(INFO, PIPELINE, "%s: Configure pipeline with invalid "
230 "socket\n", __func__);
234 /* Check pipeline consistency */
235 if (!rte_pipeline_check(p)) {
236 rte_panic("Pipeline consistency reported as OK\n");
248 setup_pipeline(int test_type)
252 struct rte_pipeline_params pipeline_params = {
257 RTE_LOG(INFO, PIPELINE, "%s: **** Setting up %s test\n",
258 __func__, pipeline_test_names[test_type]);
260 /* Pipeline configuration */
261 p = rte_pipeline_create(&pipeline_params);
263 RTE_LOG(INFO, PIPELINE, "%s: Failed to configure pipeline\n",
268 ret = rte_pipeline_free(p);
270 RTE_LOG(INFO, PIPELINE, "%s: Failed to free pipeline\n",
275 /* Pipeline configuration */
276 p = rte_pipeline_create(&pipeline_params);
278 RTE_LOG(INFO, PIPELINE, "%s: Failed to configure pipeline\n",
284 /* Input port configuration */
285 for (i = 0; i < N_PORTS; i++) {
286 struct rte_port_ring_reader_params port_ring_params = {
290 struct rte_pipeline_port_in_params port_params = {
291 .ops = &rte_port_ring_reader_ops,
292 .arg_create = (void *) &port_ring_params,
294 .burst_size = BURST_SIZE,
297 /* Put in action for some ports */
299 port_params.f_action = NULL;
301 ret = rte_pipeline_port_in_create(p, &port_params,
304 rte_panic("Unable to configure input port %d, ret:%d\n",
310 /* output Port configuration */
311 for (i = 0; i < N_PORTS; i++) {
312 struct rte_port_ring_writer_params port_ring_params = {
314 .tx_burst_sz = BURST_SIZE,
317 struct rte_pipeline_port_out_params port_params = {
318 .ops = &rte_port_ring_writer_ops,
319 .arg_create = (void *) &port_ring_params,
325 port_params.f_action = port_out_action;
327 if (rte_pipeline_port_out_create(p, &port_params,
329 rte_panic("Unable to configure output port %d\n", i);
334 /* Table configuration */
335 for (i = 0; i < N_PORTS; i++) {
336 struct rte_pipeline_table_params table_params = {
337 .ops = &rte_table_stub_ops,
339 .f_action_hit = action_handler_hit,
340 .f_action_miss = action_handler_miss,
341 .action_data_size = 0,
344 if (rte_pipeline_table_create(p, &table_params, &table_id[i])) {
345 rte_panic("Unable to configure table %u\n", i);
349 if (connect_miss_action_to_table)
350 if (rte_pipeline_table_create(p, &table_params,
352 rte_panic("Unable to configure table %u\n", i);
357 for (i = 0; i < N_PORTS; i++)
358 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
360 rte_panic("Unable to connect input port %u to "
361 "table %u\n", port_in_id[i], table_id[i]);
365 /* Add entries to tables */
366 for (i = 0; i < N_PORTS; i++) {
367 struct rte_pipeline_table_entry default_entry = {
368 .action = (enum rte_pipeline_action)
369 table_entry_default_action,
370 {.port_id = port_out_id[i^1]},
372 struct rte_pipeline_table_entry *default_entry_ptr;
374 if (connect_miss_action_to_table) {
375 printf("Setting first table to output to next table\n");
376 default_entry.action = RTE_PIPELINE_ACTION_TABLE;
377 default_entry.table_id = table_id[i+2];
380 /* Add the default action for the table. */
381 ret = rte_pipeline_table_default_entry_add(p, table_id[i],
382 &default_entry, &default_entry_ptr);
384 rte_panic("Unable to add default entry to table %u "
385 "code %d\n", table_id[i], ret);
388 printf("Added default entry to table id %d with "
390 table_id[i], default_entry.action);
392 if (connect_miss_action_to_table) {
393 /* We create a second table so the first can pass
395 struct rte_pipeline_table_entry default_entry = {
396 .action = RTE_PIPELINE_ACTION_PORT,
397 {.port_id = port_out_id[i^1]},
399 printf("Setting secont table to output to port\n");
401 /* Add the default action for the table. */
402 ret = rte_pipeline_table_default_entry_add(p,
404 &default_entry, &default_entry_ptr);
406 rte_panic("Unable to add default entry to "
407 "table %u code %d\n",
411 printf("Added default entry to table id %d "
413 table_id[i], default_entry.action);
417 /* Enable input ports */
418 for (i = 0; i < N_PORTS ; i++)
419 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
420 rte_panic("Unable to enable input port %u\n",
423 /* Check pipeline consistency */
424 if (rte_pipeline_check(p) < 0) {
425 rte_panic("Pipeline consistency check failed\n");
428 printf("Pipeline Consistency OK!\n");
437 test_pipeline_single_filter(int test_type, int expected_count)
444 RTE_LOG(INFO, PIPELINE, "%s: **** Running %s test\n",
445 __func__, pipeline_test_names[test_type]);
446 /* Run pipeline once */
450 ret = rte_pipeline_flush(NULL);
451 if (ret != -EINVAL) {
452 RTE_LOG(INFO, PIPELINE,
453 "%s: No pipeline flush error NULL pipeline (%d)\n",
459 * Allocate a few mbufs and manually insert into the rings. */
460 for (i = 0; i < N_PORTS; i++)
461 for (j = 0; j < N_PORTS; j++) {
466 m = rte_pktmbuf_alloc(pool);
468 rte_panic("Failed to alloc mbuf from pool\n");
471 key = RTE_MBUF_METADATA_UINT8_PTR(m, 32);
473 k32 = (uint32_t *) key;
474 k32[0] = 0xadadadad >> (j % 2);
476 RTE_LOG(INFO, PIPELINE, "%s: Enqueue onto ring %d\n",
478 rte_ring_enqueue(rings_rx[i], m);
481 /* Run pipeline once */
485 * need to flush the pipeline, as there may be less hits than the burst
486 size and they will not have been flushed to the tx rings. */
487 rte_pipeline_flush(p);
490 * Now we'll see what we got back on the tx rings. We should see whatever
491 * packets we had hits on that were destined for the output ports.
495 for (i = 0; i < N_PORTS; i++) {
496 void *objs[RING_TX_SIZE];
497 struct rte_mbuf *mbuf;
499 ret = rte_ring_sc_dequeue_burst(rings_tx[i], objs, 10);
501 printf("Got no objects from ring %d - error code %d\n",
504 printf("Got %d object(s) from ring %d!\n", ret, i);
505 for (j = 0; j < ret; j++) {
506 mbuf = (struct rte_mbuf *)objs[j];
507 rte_hexdump(stdout, "Object:", mbuf->pkt.data,
509 rte_pktmbuf_free(mbuf);
515 if (tx_count != expected_count) {
516 RTE_LOG(INFO, PIPELINE,
517 "%s: Unexpected packets out for %s test, expected %d, "
518 "got %d\n", __func__, pipeline_test_names[test_type],
519 expected_count, tx_count);
532 test_table_pipeline(void)
534 /* TEST - All packets dropped */
535 action_handler_hit = NULL;
536 action_handler_miss = NULL;
537 table_entry_default_action = RTE_PIPELINE_ACTION_DROP;
538 setup_pipeline(e_TEST_STUB);
539 if (test_pipeline_single_filter(e_TEST_STUB, 0) < 0)
542 /* TEST - All packets passed through */
543 table_entry_default_action = RTE_PIPELINE_ACTION_PORT;
544 setup_pipeline(e_TEST_STUB);
545 if (test_pipeline_single_filter(e_TEST_STUB, 4) < 0)
548 /* TEST - one packet per port */
549 action_handler_hit = NULL;
550 action_handler_miss =
551 (rte_pipeline_table_action_handler_miss) table_action_stub_miss;
552 table_entry_default_action = RTE_PIPELINE_ACTION_PORT;
553 override_miss_mask = 0x01; /* one packet per port */
554 setup_pipeline(e_TEST_STUB);
555 if (test_pipeline_single_filter(e_TEST_STUB, 2) < 0)
558 /* TEST - one packet per port */
559 override_miss_mask = 0x02; /*all per port */
560 setup_pipeline(e_TEST_STUB);
561 if (test_pipeline_single_filter(e_TEST_STUB, 2) < 0)
564 /* TEST - all packets per port */
565 override_miss_mask = 0x03; /*all per port */
566 setup_pipeline(e_TEST_STUB);
567 if (test_pipeline_single_filter(e_TEST_STUB, 4) < 0)
571 * This test will set up two tables in the pipeline. the first table
572 * will forward to another table on miss, and the second table will
575 connect_miss_action_to_table = 1;
576 table_entry_default_action = RTE_PIPELINE_ACTION_TABLE;
577 action_handler_hit = NULL; /* not for stub, hitmask always zero */
578 action_handler_miss = NULL;
579 setup_pipeline(e_TEST_STUB);
580 if (test_pipeline_single_filter(e_TEST_STUB, 4) < 0)
582 connect_miss_action_to_table = 0;
584 printf("TEST - two tables, hitmask override to 0x01\n");
585 connect_miss_action_to_table = 1;
586 action_handler_miss =
587 (rte_pipeline_table_action_handler_miss)table_action_stub_miss;
588 override_miss_mask = 0x01;
589 setup_pipeline(e_TEST_STUB);
590 if (test_pipeline_single_filter(e_TEST_STUB, 2) < 0)
592 connect_miss_action_to_table = 0;
594 if (check_pipeline_invalid_params()) {
595 RTE_LOG(INFO, PIPELINE, "%s: Check pipeline invalid params "
596 "failed.\n", __func__);