1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
10 #include <rte_ethdev.h>
11 #include <rte_ether.h>
13 #include <rte_byteorder.h>
15 #include <rte_port_ring.h>
16 #include <rte_table_acl.h>
17 #include <rte_pipeline.h>
31 * Here we define the 'shape' of the data we're searching for,
32 * by defining the meta-data of the ACL rules.
33 * in this case, we're defining 5 tuples. IP addresses, ports,
36 struct rte_acl_field_def ipv4_field_formats[NUM_FIELDS_IPV4] = {
38 .type = RTE_ACL_FIELD_TYPE_BITMASK,
39 .size = sizeof(uint8_t),
40 .field_index = PROTO_FIELD_IPV4,
41 .input_index = PROTO_FIELD_IPV4,
42 .offset = sizeof(struct ether_hdr) +
43 offsetof(struct ipv4_hdr, next_proto_id),
46 .type = RTE_ACL_FIELD_TYPE_MASK,
47 .size = sizeof(uint32_t),
48 .field_index = SRC_FIELD_IPV4,
49 .input_index = SRC_FIELD_IPV4,
50 .offset = sizeof(struct ether_hdr) +
51 offsetof(struct ipv4_hdr, src_addr),
54 .type = RTE_ACL_FIELD_TYPE_MASK,
55 .size = sizeof(uint32_t),
56 .field_index = DST_FIELD_IPV4,
57 .input_index = DST_FIELD_IPV4,
58 .offset = sizeof(struct ether_hdr) +
59 offsetof(struct ipv4_hdr, dst_addr),
62 .type = RTE_ACL_FIELD_TYPE_RANGE,
63 .size = sizeof(uint16_t),
64 .field_index = SRCP_FIELD_IPV4,
65 .input_index = SRCP_FIELD_IPV4,
66 .offset = sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr),
69 .type = RTE_ACL_FIELD_TYPE_RANGE,
70 .size = sizeof(uint16_t),
71 .field_index = DSTP_FIELD_IPV4,
72 .input_index = SRCP_FIELD_IPV4,
73 .offset = sizeof(struct ether_hdr) + sizeof(struct ipv4_hdr) +
81 app_main_loop_worker_pipeline_acl(void) {
82 struct rte_pipeline_params pipeline_params = {
84 .socket_id = rte_socket_id(),
87 struct rte_pipeline *p;
88 uint32_t port_in_id[APP_MAX_PORTS];
89 uint32_t port_out_id[APP_MAX_PORTS];
94 "Core %u is doing work (pipeline with ACL table)\n",
97 /* Pipeline configuration */
98 p = rte_pipeline_create(&pipeline_params);
100 rte_panic("Unable to configure the pipeline\n");
102 /* Input port configuration */
103 for (i = 0; i < app.n_ports; i++) {
104 struct rte_port_ring_reader_params port_ring_params = {
105 .ring = app.rings_rx[i],
108 struct rte_pipeline_port_in_params port_params = {
109 .ops = &rte_port_ring_reader_ops,
110 .arg_create = (void *) &port_ring_params,
113 .burst_size = app.burst_size_worker_read,
116 if (rte_pipeline_port_in_create(p, &port_params,
118 rte_panic("Unable to configure input port for "
122 /* Output port configuration */
123 for (i = 0; i < app.n_ports; i++) {
124 struct rte_port_ring_writer_params port_ring_params = {
125 .ring = app.rings_tx[i],
126 .tx_burst_sz = app.burst_size_worker_write,
129 struct rte_pipeline_port_out_params port_params = {
130 .ops = &rte_port_ring_writer_ops,
131 .arg_create = (void *) &port_ring_params,
136 if (rte_pipeline_port_out_create(p, &port_params,
138 rte_panic("Unable to configure output port for "
142 /* Table configuration */
144 struct rte_table_acl_params table_acl_params = {
145 .name = "test", /* unique identifier for acl contexts */
147 .n_rule_fields = DIM(ipv4_field_formats),
150 /* Copy in the rule meta-data defined above into the params */
151 memcpy(table_acl_params.field_format, ipv4_field_formats,
152 sizeof(ipv4_field_formats));
154 struct rte_pipeline_table_params table_params = {
155 .ops = &rte_table_acl_ops,
156 .arg_create = &table_acl_params,
157 .f_action_hit = NULL,
158 .f_action_miss = NULL,
160 .action_data_size = 0,
163 if (rte_pipeline_table_create(p, &table_params, &table_id))
164 rte_panic("Unable to configure the ACL table\n");
167 /* Interconnecting ports and tables */
168 for (i = 0; i < app.n_ports; i++)
169 if (rte_pipeline_port_in_connect_to_table(p, port_in_id[i],
171 rte_panic("Unable to connect input port %u to "
172 "table %u\n", port_in_id[i], table_id);
174 /* Add entries to tables */
175 for (i = 0; i < app.n_ports; i++) {
176 struct rte_pipeline_table_entry table_entry = {
177 .action = RTE_PIPELINE_ACTION_PORT,
178 {.port_id = port_out_id[i & (app.n_ports - 1)]},
180 struct rte_table_acl_rule_add_params rule_params;
181 struct rte_pipeline_table_entry *entry_ptr;
184 memset(&rule_params, 0, sizeof(rule_params));
186 /* Set the rule values */
187 rule_params.field_value[SRC_FIELD_IPV4].value.u32 = 0;
188 rule_params.field_value[SRC_FIELD_IPV4].mask_range.u32 = 0;
189 rule_params.field_value[DST_FIELD_IPV4].value.u32 =
190 i << (24 - __builtin_popcount(app.n_ports - 1));
191 rule_params.field_value[DST_FIELD_IPV4].mask_range.u32 =
192 8 + __builtin_popcount(app.n_ports - 1);
193 rule_params.field_value[SRCP_FIELD_IPV4].value.u16 = 0;
194 rule_params.field_value[SRCP_FIELD_IPV4].mask_range.u16 =
196 rule_params.field_value[DSTP_FIELD_IPV4].value.u16 = 0;
197 rule_params.field_value[DSTP_FIELD_IPV4].mask_range.u16 =
199 rule_params.field_value[PROTO_FIELD_IPV4].value.u8 = 0;
200 rule_params.field_value[PROTO_FIELD_IPV4].mask_range.u8 = 0;
202 rule_params.priority = 0;
204 uint32_t dst_addr = rule_params.field_value[DST_FIELD_IPV4].
207 rule_params.field_value[DST_FIELD_IPV4].mask_range.u32;
209 printf("Adding rule to ACL table (IPv4 destination = "
210 "%u.%u.%u.%u/%u => port out = %u)\n",
211 (dst_addr & 0xFF000000) >> 24,
212 (dst_addr & 0x00FF0000) >> 16,
213 (dst_addr & 0x0000FF00) >> 8,
214 dst_addr & 0x000000FF,
216 table_entry.port_id);
218 /* For ACL, add needs an rte_table_acl_rule_add_params struct */
219 ret = rte_pipeline_table_entry_add(p, table_id, &rule_params,
220 &table_entry, &key_found, &entry_ptr);
222 rte_panic("Unable to add entry to table %u (%d)\n",
226 /* Enable input ports */
227 for (i = 0; i < app.n_ports; i++)
228 if (rte_pipeline_port_in_enable(p, port_in_id[i]))
229 rte_panic("Unable to enable input port %u\n",
232 /* Check pipeline consistency */
233 if (rte_pipeline_check(p) < 0)
234 rte_panic("Pipeline consistency check failed\n");
244 if ((i & APP_FLUSH) == 0)
245 rte_pipeline_flush(p);