apps: add name to LPM parameters
[dpdk.git] / app / test-pipeline / pipeline_lpm.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdint.h>
37
38 #include <rte_log.h>
39 #include <rte_ethdev.h>
40 #include <rte_ether.h>
41 #include <rte_ip.h>
42 #include <rte_byteorder.h>
43
44 #include <rte_port_ring.h>
45 #include <rte_table_lpm.h>
46 #include <rte_pipeline.h>
47
48 #include "main.h"
49
50 void
51 app_main_loop_worker_pipeline_lpm(void) {
52         struct rte_pipeline_params pipeline_params = {
53                 .name = "pipeline",
54                 .socket_id = rte_socket_id(),
55         };
56
57         struct rte_pipeline *p;
58         uint32_t port_in_id[APP_MAX_PORTS];
59         uint32_t port_out_id[APP_MAX_PORTS];
60         uint32_t table_id;
61         uint32_t i;
62
63         RTE_LOG(INFO, USER1, "Core %u is doing work (pipeline with "
64                 "LPM table)\n", rte_lcore_id());
65
66         /* Pipeline configuration */
67         p = rte_pipeline_create(&pipeline_params);
68         if (p == NULL)
69                 rte_panic("Unable to configure the pipeline\n");
70
71         /* Input port configuration */
72         for (i = 0; i < app.n_ports; i++) {
73                 struct rte_port_ring_reader_params port_ring_params = {
74                         .ring = app.rings_rx[i],
75                 };
76
77                 struct rte_pipeline_port_in_params port_params = {
78                         .ops = &rte_port_ring_reader_ops,
79                         .arg_create = (void *) &port_ring_params,
80                         .f_action = NULL,
81                         .arg_ah = NULL,
82                         .burst_size = app.burst_size_worker_read,
83                 };
84
85                 if (rte_pipeline_port_in_create(p, &port_params,
86                         &port_in_id[i]))
87                         rte_panic("Unable to configure input port for "
88                                 "ring %d\n", i);
89         }
90
91         /* Output port configuration */
92         for (i = 0; i < app.n_ports; i++) {
93                 struct rte_port_ring_writer_params port_ring_params = {
94                         .ring = app.rings_tx[i],
95                         .tx_burst_sz = app.burst_size_worker_write,
96                 };
97
98                 struct rte_pipeline_port_out_params port_params = {
99                         .ops = &rte_port_ring_writer_ops,
100                         .arg_create = (void *) &port_ring_params,
101                         .f_action = NULL,
102                         .f_action_bulk = NULL,
103                         .arg_ah = NULL,
104                 };
105
106                 if (rte_pipeline_port_out_create(p, &port_params,
107                         &port_out_id[i]))
108                         rte_panic("Unable to configure output port for "
109                                 "ring %d\n", i);
110         }
111
112         /* Table configuration */
113         {
114                 struct rte_table_lpm_params table_lpm_params = {
115                         .name = "LPM",
116                         .n_rules = 1 << 24,
117                         .entry_unique_size =
118                                 sizeof(struct rte_pipeline_table_entry),
119                         .offset = 32,
120                 };
121
122                 struct rte_pipeline_table_params table_params = {
123                         .ops = &rte_table_lpm_ops,
124                         .arg_create = &table_lpm_params,
125                         .f_action_hit = NULL,
126                         .f_action_miss = NULL,
127                         .arg_ah = NULL,
128                         .action_data_size = 0,
129                 };
130
131                 if (rte_pipeline_table_create(p, &table_params, &table_id))
132                         rte_panic("Unable to configure the LPM table\n");
133         }
134
135         /* Interconnecting ports and tables */
136         for (i = 0; i < app.n_ports; i++)
137                 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
138                         table_id))
139                         rte_panic("Unable to connect input port %u to "
140                                 "table %u\n", port_in_id[i],  table_id);
141
142         /* Add entries to tables */
143         for (i = 0; i < app.n_ports; i++) {
144                 struct rte_pipeline_table_entry entry = {
145                         .action = RTE_PIPELINE_ACTION_PORT,
146                         {.port_id = port_out_id[i & (app.n_ports - 1)]},
147                 };
148
149                 struct rte_table_lpm_key key = {
150                         .ip = i << (24 - __builtin_popcount(app.n_ports - 1)),
151                         .depth = 8 + __builtin_popcount(app.n_ports - 1),
152                 };
153
154                 struct rte_pipeline_table_entry *entry_ptr;
155
156                 int key_found, status;
157
158                 printf("Adding rule to LPM table (IPv4 destination = %"
159                         PRIu32 ".%" PRIu32 ".%" PRIu32 ".%" PRIu32 "/%" PRIu8
160                         " => port out = %" PRIu32 ")\n",
161                         (key.ip & 0xFF000000) >> 24,
162                         (key.ip & 0x00FF0000) >> 16,
163                         (key.ip & 0x0000FF00) >> 8,
164                         key.ip & 0x000000FF,
165                         key.depth,
166                         i);
167
168                 status = rte_pipeline_table_entry_add(p, table_id, &key, &entry,
169                         &key_found, &entry_ptr);
170                 if (status < 0)
171                         rte_panic("Unable to add entry to table %u (%d)\n",
172                                 table_id, status);
173         }
174
175         /* Enable input ports */
176         for (i = 0; i < app.n_ports; i++)
177                 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
178                         rte_panic("Unable to enable input port %u\n",
179                                 port_in_id[i]);
180
181         /* Check pipeline consistency */
182         if (rte_pipeline_check(p) < 0)
183                 rte_panic("Pipeline consistency check failed\n");
184
185         /* Run-time */
186 #if APP_FLUSH == 0
187         for ( ; ; )
188                 rte_pipeline_run(p);
189 #else
190         for (i = 0; ; i++) {
191                 rte_pipeline_run(p);
192
193                 if ((i & APP_FLUSH) == 0)
194                         rte_pipeline_flush(p);
195         }
196 #endif
197 }