4 * Copyright(c) 2010-2015 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.
36 #include <rte_common.h>
37 #include <rte_malloc.h>
38 #include <rte_table_hash.h>
39 #include <rte_byteorder.h>
41 #include "pipeline_flow_classification_be.h"
42 #include "hash_func.h"
44 struct pipeline_flow_classification {
46 pipeline_msg_req_handler custom_handlers[PIPELINE_FC_MSG_REQS];
52 } __rte_cache_aligned;
55 pipeline_fc_msg_req_custom_handler(struct pipeline *p, void *msg);
57 static pipeline_msg_req_handler handlers[] = {
58 [PIPELINE_MSG_REQ_PING] =
59 pipeline_msg_req_ping_handler,
60 [PIPELINE_MSG_REQ_STATS_PORT_IN] =
61 pipeline_msg_req_stats_port_in_handler,
62 [PIPELINE_MSG_REQ_STATS_PORT_OUT] =
63 pipeline_msg_req_stats_port_out_handler,
64 [PIPELINE_MSG_REQ_STATS_TABLE] =
65 pipeline_msg_req_stats_table_handler,
66 [PIPELINE_MSG_REQ_PORT_IN_ENABLE] =
67 pipeline_msg_req_port_in_enable_handler,
68 [PIPELINE_MSG_REQ_PORT_IN_DISABLE] =
69 pipeline_msg_req_port_in_disable_handler,
70 [PIPELINE_MSG_REQ_CUSTOM] =
71 pipeline_fc_msg_req_custom_handler,
75 pipeline_fc_msg_req_add_handler(struct pipeline *p, void *msg);
78 pipeline_fc_msg_req_add_bulk_handler(struct pipeline *p, void *msg);
81 pipeline_fc_msg_req_del_handler(struct pipeline *p, void *msg);
84 pipeline_fc_msg_req_add_default_handler(struct pipeline *p, void *msg);
87 pipeline_fc_msg_req_del_default_handler(struct pipeline *p, void *msg);
89 static pipeline_msg_req_handler custom_handlers[] = {
90 [PIPELINE_FC_MSG_REQ_FLOW_ADD] =
91 pipeline_fc_msg_req_add_handler,
92 [PIPELINE_FC_MSG_REQ_FLOW_ADD_BULK] =
93 pipeline_fc_msg_req_add_bulk_handler,
94 [PIPELINE_FC_MSG_REQ_FLOW_DEL] =
95 pipeline_fc_msg_req_del_handler,
96 [PIPELINE_FC_MSG_REQ_FLOW_ADD_DEFAULT] =
97 pipeline_fc_msg_req_add_default_handler,
98 [PIPELINE_FC_MSG_REQ_FLOW_DEL_DEFAULT] =
99 pipeline_fc_msg_req_del_default_handler,
105 struct flow_table_entry {
106 struct rte_pipeline_table_entry head;
109 rte_table_hash_op_hash hash_func[] = {
121 pipeline_fc_parse_args(struct pipeline_flow_classification *p,
122 struct pipeline_params *params)
124 uint32_t n_flows_present = 0;
125 uint32_t key_offset_present = 0;
126 uint32_t key_size_present = 0;
127 uint32_t hash_offset_present = 0;
131 for (i = 0; i < params->n_args; i++) {
132 char *arg_name = params->args_name[i];
133 char *arg_value = params->args_value[i];
136 if (strcmp(arg_name, "n_flows") == 0) {
141 p->n_flows = atoi(arg_value);
149 if (strcmp(arg_name, "key_offset") == 0) {
150 if (key_offset_present)
152 key_offset_present = 1;
154 p->key_offset = atoi(arg_value);
160 if (strcmp(arg_name, "key_size") == 0) {
161 if (key_size_present)
163 key_size_present = 1;
165 p->key_size = atoi(arg_value);
166 if ((p->key_size == 0) ||
167 (p->key_size > PIPELINE_FC_FLOW_KEY_MAX_SIZE) ||
175 if (strcmp(arg_name, "hash_offset") == 0) {
176 if (hash_offset_present)
178 hash_offset_present = 1;
180 p->hash_offset = atoi(arg_value);
185 /* Unknown argument */
189 /* Check that mandatory arguments are present */
190 if ((n_flows_present == 0) ||
191 (key_offset_present == 0) ||
192 (key_size_present == 0) ||
193 (hash_offset_present == 0))
199 static void *pipeline_fc_init(struct pipeline_params *params,
200 __rte_unused void *arg)
203 struct pipeline_flow_classification *p_fc;
206 /* Check input arguments */
210 /* Memory allocation */
211 size = RTE_CACHE_LINE_ROUNDUP(
212 sizeof(struct pipeline_flow_classification));
213 p = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
216 p_fc = (struct pipeline_flow_classification *) p;
218 strcpy(p->name, params->name);
219 p->log_level = params->log_level;
221 PLOG(p, HIGH, "Flow classification");
223 /* Parse arguments */
224 if (pipeline_fc_parse_args(p_fc, params))
229 struct rte_pipeline_params pipeline_params = {
230 .name = params->name,
231 .socket_id = params->socket_id,
235 p->p = rte_pipeline_create(&pipeline_params);
243 p->n_ports_in = params->n_ports_in;
244 for (i = 0; i < p->n_ports_in; i++) {
245 struct rte_pipeline_port_in_params port_params = {
246 .ops = pipeline_port_in_params_get_ops(
247 ¶ms->port_in[i]),
248 .arg_create = pipeline_port_in_params_convert(
249 ¶ms->port_in[i]),
252 .burst_size = params->port_in[i].burst_size,
255 int status = rte_pipeline_port_in_create(p->p,
260 rte_pipeline_free(p->p);
267 p->n_ports_out = params->n_ports_out;
268 for (i = 0; i < p->n_ports_out; i++) {
269 struct rte_pipeline_port_out_params port_params = {
270 .ops = pipeline_port_out_params_get_ops(
271 ¶ms->port_out[i]),
272 .arg_create = pipeline_port_out_params_convert(
273 ¶ms->port_out[i]),
275 .f_action_bulk = NULL,
279 int status = rte_pipeline_port_out_create(p->p,
284 rte_pipeline_free(p->p);
293 struct rte_table_hash_key8_ext_params
294 table_hash_key8_params = {
295 .n_entries = p_fc->n_flows,
296 .n_entries_ext = p_fc->n_flows,
297 .signature_offset = p_fc->hash_offset,
298 .key_offset = p_fc->key_offset,
299 .f_hash = hash_func[(p_fc->key_size / 8) - 1],
303 struct rte_table_hash_key16_ext_params
304 table_hash_key16_params = {
305 .n_entries = p_fc->n_flows,
306 .n_entries_ext = p_fc->n_flows,
307 .signature_offset = p_fc->hash_offset,
308 .key_offset = p_fc->key_offset,
309 .f_hash = hash_func[(p_fc->key_size / 8) - 1],
313 struct rte_table_hash_ext_params
314 table_hash_params = {
315 .key_size = p_fc->key_size,
316 .n_keys = p_fc->n_flows,
317 .n_buckets = p_fc->n_flows / 4,
318 .n_buckets_ext = p_fc->n_flows / 4,
319 .f_hash = hash_func[(p_fc->key_size / 8) - 1],
321 .signature_offset = p_fc->hash_offset,
322 .key_offset = p_fc->key_offset,
325 struct rte_pipeline_table_params table_params = {
326 .ops = NULL, /* set below */
327 .arg_create = NULL, /* set below */
328 .f_action_hit = NULL,
329 .f_action_miss = NULL,
331 .action_data_size = sizeof(struct flow_table_entry) -
332 sizeof(struct rte_pipeline_table_entry),
337 switch (p_fc->key_size) {
339 table_params.ops = &rte_table_hash_key8_lru_ops;
340 table_params.arg_create = &table_hash_key8_params;
344 table_params.ops = &rte_table_hash_key16_ext_ops;
345 table_params.arg_create = &table_hash_key16_params;
349 table_params.ops = &rte_table_hash_ext_ops;
350 table_params.arg_create = &table_hash_params;
353 status = rte_pipeline_table_create(p->p,
358 rte_pipeline_free(p->p);
364 /* Connecting input ports to tables */
365 for (i = 0; i < p->n_ports_in; i++) {
366 int status = rte_pipeline_port_in_connect_to_table(p->p,
371 rte_pipeline_free(p->p);
377 /* Enable input ports */
378 for (i = 0; i < p->n_ports_in; i++) {
379 int status = rte_pipeline_port_in_enable(p->p,
383 rte_pipeline_free(p->p);
389 /* Check pipeline consistency */
390 if (rte_pipeline_check(p->p) < 0) {
391 rte_pipeline_free(p->p);
397 p->n_msgq = params->n_msgq;
398 for (i = 0; i < p->n_msgq; i++)
399 p->msgq_in[i] = params->msgq_in[i];
400 for (i = 0; i < p->n_msgq; i++)
401 p->msgq_out[i] = params->msgq_out[i];
403 /* Message handlers */
404 memcpy(p->handlers, handlers, sizeof(p->handlers));
405 memcpy(p_fc->custom_handlers,
407 sizeof(p_fc->custom_handlers));
413 pipeline_fc_free(void *pipeline)
415 struct pipeline *p = (struct pipeline *) pipeline;
417 /* Check input arguments */
422 rte_pipeline_free(p->p);
428 pipeline_fc_track(void *pipeline,
429 __rte_unused uint32_t port_in,
432 struct pipeline *p = (struct pipeline *) pipeline;
434 /* Check input arguments */
436 (port_in >= p->n_ports_in) ||
440 if (p->n_ports_in == 1) {
449 pipeline_fc_timer(void *pipeline)
451 struct pipeline *p = (struct pipeline *) pipeline;
453 pipeline_msg_req_handle(p);
454 rte_pipeline_flush(p->p);
460 pipeline_fc_msg_req_custom_handler(struct pipeline *p, void *msg)
462 struct pipeline_flow_classification *p_fc =
463 (struct pipeline_flow_classification *) p;
464 struct pipeline_custom_msg_req *req = msg;
465 pipeline_msg_req_handler f_handle;
467 f_handle = (req->subtype < PIPELINE_FC_MSG_REQS) ?
468 p_fc->custom_handlers[req->subtype] :
469 pipeline_msg_req_invalid_handler;
471 if (f_handle == NULL)
472 f_handle = pipeline_msg_req_invalid_handler;
474 return f_handle(p, req);
478 pipeline_fc_msg_req_add_handler(struct pipeline *p, void *msg)
480 struct pipeline_fc_add_msg_req *req = msg;
481 struct pipeline_fc_add_msg_rsp *rsp = msg;
483 struct flow_table_entry entry = {
485 .action = RTE_PIPELINE_ACTION_PORT,
486 {.port_id = p->port_out_id[req->port_id]},
490 rsp->status = rte_pipeline_table_entry_add(p->p,
493 (struct rte_pipeline_table_entry *) &entry,
495 (struct rte_pipeline_table_entry **) &rsp->entry_ptr);
501 pipeline_fc_msg_req_add_bulk_handler(struct pipeline *p, void *msg)
503 struct pipeline_fc_add_bulk_msg_req *req = msg;
504 struct pipeline_fc_add_bulk_msg_rsp *rsp = msg;
507 for (i = 0; i < req->n_keys; i++) {
508 struct pipeline_fc_add_bulk_flow_req *flow_req = &req->req[i];
509 struct pipeline_fc_add_bulk_flow_rsp *flow_rsp = &req->rsp[i];
511 struct flow_table_entry entry = {
513 .action = RTE_PIPELINE_ACTION_PORT,
514 {.port_id = p->port_out_id[flow_req->port_id]},
518 int status = rte_pipeline_table_entry_add(p->p,
521 (struct rte_pipeline_table_entry *) &entry,
522 &flow_rsp->key_found,
523 (struct rte_pipeline_table_entry **)
524 &flow_rsp->entry_ptr);
536 pipeline_fc_msg_req_del_handler(struct pipeline *p, void *msg)
538 struct pipeline_fc_del_msg_req *req = msg;
539 struct pipeline_fc_del_msg_rsp *rsp = msg;
541 rsp->status = rte_pipeline_table_entry_delete(p->p,
551 pipeline_fc_msg_req_add_default_handler(struct pipeline *p, void *msg)
553 struct pipeline_fc_add_default_msg_req *req = msg;
554 struct pipeline_fc_add_default_msg_rsp *rsp = msg;
556 struct flow_table_entry default_entry = {
558 .action = RTE_PIPELINE_ACTION_PORT,
559 {.port_id = p->port_out_id[req->port_id]},
563 rsp->status = rte_pipeline_table_default_entry_add(p->p,
565 (struct rte_pipeline_table_entry *) &default_entry,
566 (struct rte_pipeline_table_entry **) &rsp->entry_ptr);
572 pipeline_fc_msg_req_del_default_handler(struct pipeline *p, void *msg)
574 struct pipeline_fc_del_default_msg_rsp *rsp = msg;
576 rsp->status = rte_pipeline_table_default_entry_delete(p->p,
583 struct pipeline_be_ops pipeline_flow_classification_be_ops = {
584 .f_init = pipeline_fc_init,
585 .f_free = pipeline_fc_free,
587 .f_timer = pipeline_fc_timer,
588 .f_track = pipeline_fc_track,