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.
39 #include <rte_ethdev.h>
40 #include <rte_ether.h>
42 #include <rte_byteorder.h>
44 #include <rte_port_ring.h>
45 #include <rte_table_acl.h>
46 #include <rte_pipeline.h>
60 * Here we define the 'shape' of the data we're searching for,
61 * by defining the meta-data of the ACL rules.
62 * in this case, we're defining 5 tuples. IP addresses, ports,
65 struct rte_acl_field_def ipv4_field_formats[NUM_FIELDS_IPV4] = {
67 .type = RTE_ACL_FIELD_TYPE_BITMASK,
68 .size = sizeof(uint8_t),
69 .field_index = PROTO_FIELD_IPV4,
70 .input_index = PROTO_FIELD_IPV4,
71 .offset = sizeof(struct ether_hdr) +
72 offsetof(struct ipv4_hdr, next_proto_id),
75 .type = RTE_ACL_FIELD_TYPE_MASK,
76 .size = sizeof(uint32_t),
77 .field_index = SRC_FIELD_IPV4,
78 .input_index = SRC_FIELD_IPV4,
79 .offset = sizeof(struct ether_hdr) +
80 offsetof(struct ipv4_hdr, src_addr),
83 .type = RTE_ACL_FIELD_TYPE_MASK,
84 .size = sizeof(uint32_t),
85 .field_index = DST_FIELD_IPV4,
86 .input_index = DST_FIELD_IPV4,
87 .offset = sizeof(struct ether_hdr) +
88 offsetof(struct ipv4_hdr, dst_addr),
91 .type = RTE_ACL_FIELD_TYPE_RANGE,
92 .size = sizeof(uint16_t),
93 .field_index = SRCP_FIELD_IPV4,
94 .input_index = SRCP_FIELD_IPV4,
95 .offset = sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr),
98 .type = RTE_ACL_FIELD_TYPE_RANGE,
99 .size = sizeof(uint16_t),
100 .field_index = DSTP_FIELD_IPV4,
101 .input_index = SRCP_FIELD_IPV4,
102 .offset = sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr) +
110 app_main_loop_worker_pipeline_acl(void) {
111 struct rte_pipeline_params pipeline_params = {
113 .socket_id = rte_socket_id(),
116 struct rte_pipeline *p;
117 uint32_t port_in_id[APP_MAX_PORTS];
118 uint32_t port_out_id[APP_MAX_PORTS];
123 "Core %u is doing work (pipeline with ACL table)\n",
126 /* Pipeline configuration */
127 p = rte_pipeline_create(&pipeline_params);
129 rte_panic("Unable to configure the pipeline\n");
131 /* Input port configuration */
132 for (i = 0; i < app.n_ports; i++) {
133 struct rte_port_ring_reader_params port_ring_params = {
134 .ring = app.rings_rx[i],
137 struct rte_pipeline_port_in_params port_params = {
138 .ops = &rte_port_ring_reader_ops,
139 .arg_create = (void *) &port_ring_params,
142 .burst_size = app.burst_size_worker_read,
145 if (rte_pipeline_port_in_create(p, &port_params,
147 rte_panic("Unable to configure input port for "
151 /* Output port configuration */
152 for (i = 0; i < app.n_ports; i++) {
153 struct rte_port_ring_writer_params port_ring_params = {
154 .ring = app.rings_tx[i],
155 .tx_burst_sz = app.burst_size_worker_write,
158 struct rte_pipeline_port_out_params port_params = {
159 .ops = &rte_port_ring_writer_ops,
160 .arg_create = (void *) &port_ring_params,
162 .f_action_bulk = NULL,
166 if (rte_pipeline_port_out_create(p, &port_params,
168 rte_panic("Unable to configure output port for "
172 /* Table configuration */
174 struct rte_table_acl_params table_acl_params = {
175 .name = "test", /* unique identifier for acl contexts */
177 .n_rule_fields = DIM(ipv4_field_formats),
180 /* Copy in the rule meta-data defined above into the params */
181 memcpy(table_acl_params.field_format, ipv4_field_formats,
182 sizeof(ipv4_field_formats));
184 struct rte_pipeline_table_params table_params = {
185 .ops = &rte_table_acl_ops,
186 .arg_create = &table_acl_params,
187 .f_action_hit = NULL,
188 .f_action_miss = NULL,
190 .action_data_size = 0,
193 if (rte_pipeline_table_create(p, &table_params, &table_id))
194 rte_panic("Unable to configure the ACL table\n");
197 /* Interconnecting ports and tables */
198 for (i = 0; i < app.n_ports; i++)
199 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
201 rte_panic("Unable to connect input port %u to "
202 "table %u\n", port_in_id[i], table_id);
204 /* Add entries to tables */
205 for (i = 0; i < app.n_ports; i++) {
206 struct rte_pipeline_table_entry table_entry = {
207 .action = RTE_PIPELINE_ACTION_PORT,
208 {.port_id = port_out_id[i & (app.n_ports - 1)]},
210 struct rte_table_acl_rule_add_params rule_params;
211 struct rte_pipeline_table_entry *entry_ptr;
214 memset(&rule_params, 0, sizeof(rule_params));
216 /* Set the rule values */
217 rule_params.field_value[SRC_FIELD_IPV4].value.u32 = 0;
218 rule_params.field_value[SRC_FIELD_IPV4].mask_range.u32 = 0;
219 rule_params.field_value[DST_FIELD_IPV4].value.u32 =
220 i << (24 - __builtin_popcount(app.n_ports - 1));
221 rule_params.field_value[DST_FIELD_IPV4].mask_range.u32 =
222 8 + __builtin_popcount(app.n_ports - 1);
223 rule_params.field_value[SRCP_FIELD_IPV4].value.u16 = 0;
224 rule_params.field_value[SRCP_FIELD_IPV4].mask_range.u16 =
226 rule_params.field_value[DSTP_FIELD_IPV4].value.u16 = 0;
227 rule_params.field_value[DSTP_FIELD_IPV4].mask_range.u16 =
229 rule_params.field_value[PROTO_FIELD_IPV4].value.u8 = 0;
230 rule_params.field_value[PROTO_FIELD_IPV4].mask_range.u8 = 0;
232 rule_params.priority = 0;
234 uint32_t dst_addr = rule_params.field_value[DST_FIELD_IPV4].
237 rule_params.field_value[DST_FIELD_IPV4].mask_range.u32;
239 printf("Adding rule to ACL table (IPv4 destination = "
240 "%u.%u.%u.%u/%u => port out = %u)\n",
241 (dst_addr & 0xFF000000) >> 24,
242 (dst_addr & 0x00FF0000) >> 16,
243 (dst_addr & 0x0000FF00) >> 8,
244 dst_addr & 0x000000FF,
246 table_entry.port_id);
248 /* For ACL, add needs an rte_table_acl_rule_add_params struct */
249 ret = rte_pipeline_table_entry_add(p, table_id, &rule_params,
250 &table_entry, &key_found, &entry_ptr);
252 rte_panic("Unable to add entry to table %u (%d)\n",
256 /* Enable input ports */
257 for (i = 0; i < app.n_ports; i++)
258 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
259 rte_panic("Unable to enable input port %u\n",
262 /* Check pipeline consistency */
263 if (rte_pipeline_check(p) < 0)
264 rte_panic("Pipeline consistency check failed\n");
274 if ((i & APP_FLUSH) == 0)
275 rte_pipeline_flush(p);