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>
42 #include "pipeline_flow_classification_be.h"
43 #include "hash_func.h"
45 struct pipeline_flow_classification {
47 pipeline_msg_req_handler custom_handlers[PIPELINE_FC_MSG_REQS];
54 } __rte_cache_aligned;
57 pipeline_fc_msg_req_custom_handler(struct pipeline *p, void *msg);
59 static pipeline_msg_req_handler handlers[] = {
60 [PIPELINE_MSG_REQ_PING] =
61 pipeline_msg_req_ping_handler,
62 [PIPELINE_MSG_REQ_STATS_PORT_IN] =
63 pipeline_msg_req_stats_port_in_handler,
64 [PIPELINE_MSG_REQ_STATS_PORT_OUT] =
65 pipeline_msg_req_stats_port_out_handler,
66 [PIPELINE_MSG_REQ_STATS_TABLE] =
67 pipeline_msg_req_stats_table_handler,
68 [PIPELINE_MSG_REQ_PORT_IN_ENABLE] =
69 pipeline_msg_req_port_in_enable_handler,
70 [PIPELINE_MSG_REQ_PORT_IN_DISABLE] =
71 pipeline_msg_req_port_in_disable_handler,
72 [PIPELINE_MSG_REQ_CUSTOM] =
73 pipeline_fc_msg_req_custom_handler,
77 pipeline_fc_msg_req_add_handler(struct pipeline *p, void *msg);
80 pipeline_fc_msg_req_add_bulk_handler(struct pipeline *p, void *msg);
83 pipeline_fc_msg_req_del_handler(struct pipeline *p, void *msg);
86 pipeline_fc_msg_req_add_default_handler(struct pipeline *p, void *msg);
89 pipeline_fc_msg_req_del_default_handler(struct pipeline *p, void *msg);
91 static pipeline_msg_req_handler custom_handlers[] = {
92 [PIPELINE_FC_MSG_REQ_FLOW_ADD] =
93 pipeline_fc_msg_req_add_handler,
94 [PIPELINE_FC_MSG_REQ_FLOW_ADD_BULK] =
95 pipeline_fc_msg_req_add_bulk_handler,
96 [PIPELINE_FC_MSG_REQ_FLOW_DEL] =
97 pipeline_fc_msg_req_del_handler,
98 [PIPELINE_FC_MSG_REQ_FLOW_ADD_DEFAULT] =
99 pipeline_fc_msg_req_add_default_handler,
100 [PIPELINE_FC_MSG_REQ_FLOW_DEL_DEFAULT] =
101 pipeline_fc_msg_req_del_default_handler,
107 struct flow_table_entry {
108 struct rte_pipeline_table_entry head;
111 rte_table_hash_op_hash hash_func[] = {
123 pipeline_fc_parse_args(struct pipeline_flow_classification *p,
124 struct pipeline_params *params)
126 uint32_t n_flows_present = 0;
127 uint32_t key_offset_present = 0;
128 uint32_t key_size_present = 0;
129 uint32_t hash_offset_present = 0;
130 uint32_t key_mask_present = 0;
133 char *key_mask_str = NULL;
137 for (i = 0; i < params->n_args; i++) {
138 char *arg_name = params->args_name[i];
139 char *arg_value = params->args_value[i];
142 if (strcmp(arg_name, "n_flows") == 0) {
147 p->n_flows = atoi(arg_value);
155 if (strcmp(arg_name, "key_offset") == 0) {
156 if (key_offset_present)
159 key_offset_present = 1;
161 p->key_offset = atoi(arg_value);
167 if (strcmp(arg_name, "key_size") == 0) {
168 if (key_size_present)
170 key_size_present = 1;
172 p->key_size = atoi(arg_value);
173 if ((p->key_size == 0) ||
174 (p->key_size > PIPELINE_FC_FLOW_KEY_MAX_SIZE) ||
182 if (strcmp(arg_name, "key_mask") == 0) {
183 if (key_mask_present)
186 key_mask_str = strdup(arg_value);
187 if (key_mask_str == NULL)
190 key_mask_present = 1;
196 if (strcmp(arg_name, "hash_offset") == 0) {
197 if (hash_offset_present)
199 hash_offset_present = 1;
201 p->hash_offset = atoi(arg_value);
206 /* Unknown argument */
210 /* Check that mandatory arguments are present */
211 if ((n_flows_present == 0) ||
212 (key_offset_present == 0) ||
213 (key_size_present == 0))
216 if (key_mask_present) {
217 p->key_mask = rte_malloc(NULL, p->key_size, 0);
218 if (p->key_mask == NULL)
221 if (parse_hex_string(key_mask_str, p->key_mask, &p->key_size)
232 if (key_mask_str != NULL)
234 if (p->key_mask != NULL)
239 static void *pipeline_fc_init(struct pipeline_params *params,
240 __rte_unused void *arg)
243 struct pipeline_flow_classification *p_fc;
246 /* Check input arguments */
250 /* Memory allocation */
251 size = RTE_CACHE_LINE_ROUNDUP(
252 sizeof(struct pipeline_flow_classification));
253 p = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
256 p_fc = (struct pipeline_flow_classification *) p;
258 strcpy(p->name, params->name);
259 p->log_level = params->log_level;
261 PLOG(p, HIGH, "Flow classification");
263 /* Parse arguments */
264 if (pipeline_fc_parse_args(p_fc, params))
269 struct rte_pipeline_params pipeline_params = {
270 .name = params->name,
271 .socket_id = params->socket_id,
275 p->p = rte_pipeline_create(&pipeline_params);
283 p->n_ports_in = params->n_ports_in;
284 for (i = 0; i < p->n_ports_in; i++) {
285 struct rte_pipeline_port_in_params port_params = {
286 .ops = pipeline_port_in_params_get_ops(
287 ¶ms->port_in[i]),
288 .arg_create = pipeline_port_in_params_convert(
289 ¶ms->port_in[i]),
292 .burst_size = params->port_in[i].burst_size,
295 int status = rte_pipeline_port_in_create(p->p,
300 rte_pipeline_free(p->p);
307 p->n_ports_out = params->n_ports_out;
308 for (i = 0; i < p->n_ports_out; i++) {
309 struct rte_pipeline_port_out_params port_params = {
310 .ops = pipeline_port_out_params_get_ops(
311 ¶ms->port_out[i]),
312 .arg_create = pipeline_port_out_params_convert(
313 ¶ms->port_out[i]),
315 .f_action_bulk = NULL,
319 int status = rte_pipeline_port_out_create(p->p,
324 rte_pipeline_free(p->p);
333 struct rte_table_hash_key8_ext_params
334 table_hash_key8_params = {
335 .n_entries = p_fc->n_flows,
336 .n_entries_ext = p_fc->n_flows,
337 .signature_offset = p_fc->hash_offset,
338 .key_offset = p_fc->key_offset,
339 .f_hash = hash_func[(p_fc->key_size / 8) - 1],
340 .key_mask = p_fc->key_mask,
344 struct rte_table_hash_key16_ext_params
345 table_hash_key16_params = {
346 .n_entries = p_fc->n_flows,
347 .n_entries_ext = p_fc->n_flows,
348 .signature_offset = p_fc->hash_offset,
349 .key_offset = p_fc->key_offset,
350 .f_hash = hash_func[(p_fc->key_size / 8) - 1],
351 .key_mask = p_fc->key_mask,
355 struct rte_table_hash_ext_params
356 table_hash_params = {
357 .key_size = p_fc->key_size,
358 .n_keys = p_fc->n_flows,
359 .n_buckets = p_fc->n_flows / 4,
360 .n_buckets_ext = p_fc->n_flows / 4,
361 .f_hash = hash_func[(p_fc->key_size / 8) - 1],
363 .signature_offset = p_fc->hash_offset,
364 .key_offset = p_fc->key_offset,
367 struct rte_pipeline_table_params table_params = {
368 .ops = NULL, /* set below */
369 .arg_create = NULL, /* set below */
370 .f_action_hit = NULL,
371 .f_action_miss = NULL,
373 .action_data_size = sizeof(struct flow_table_entry) -
374 sizeof(struct rte_pipeline_table_entry),
379 switch (p_fc->key_size) {
381 if (p_fc->hash_offset != 0) {
383 &rte_table_hash_key8_ext_ops;
386 &rte_table_hash_key8_ext_dosig_ops;
388 table_params.arg_create = &table_hash_key8_params;
393 if (p_fc->hash_offset != 0) {
395 &rte_table_hash_key16_ext_ops;
398 &rte_table_hash_key16_ext_dosig_ops;
400 table_params.arg_create = &table_hash_key16_params;
404 table_params.ops = &rte_table_hash_ext_ops;
405 table_params.arg_create = &table_hash_params;
408 status = rte_pipeline_table_create(p->p,
413 rte_pipeline_free(p->p);
419 /* Connecting input ports to tables */
420 for (i = 0; i < p->n_ports_in; i++) {
421 int status = rte_pipeline_port_in_connect_to_table(p->p,
426 rte_pipeline_free(p->p);
432 /* Enable input ports */
433 for (i = 0; i < p->n_ports_in; i++) {
434 int status = rte_pipeline_port_in_enable(p->p,
438 rte_pipeline_free(p->p);
444 /* Check pipeline consistency */
445 if (rte_pipeline_check(p->p) < 0) {
446 rte_pipeline_free(p->p);
452 p->n_msgq = params->n_msgq;
453 for (i = 0; i < p->n_msgq; i++)
454 p->msgq_in[i] = params->msgq_in[i];
455 for (i = 0; i < p->n_msgq; i++)
456 p->msgq_out[i] = params->msgq_out[i];
458 /* Message handlers */
459 memcpy(p->handlers, handlers, sizeof(p->handlers));
460 memcpy(p_fc->custom_handlers,
462 sizeof(p_fc->custom_handlers));
468 pipeline_fc_free(void *pipeline)
470 struct pipeline *p = (struct pipeline *) pipeline;
472 /* Check input arguments */
477 rte_pipeline_free(p->p);
483 pipeline_fc_track(void *pipeline,
484 __rte_unused uint32_t port_in,
487 struct pipeline *p = (struct pipeline *) pipeline;
489 /* Check input arguments */
491 (port_in >= p->n_ports_in) ||
495 if (p->n_ports_in == 1) {
504 pipeline_fc_timer(void *pipeline)
506 struct pipeline *p = (struct pipeline *) pipeline;
508 pipeline_msg_req_handle(p);
509 rte_pipeline_flush(p->p);
515 pipeline_fc_msg_req_custom_handler(struct pipeline *p, void *msg)
517 struct pipeline_flow_classification *p_fc =
518 (struct pipeline_flow_classification *) p;
519 struct pipeline_custom_msg_req *req = msg;
520 pipeline_msg_req_handler f_handle;
522 f_handle = (req->subtype < PIPELINE_FC_MSG_REQS) ?
523 p_fc->custom_handlers[req->subtype] :
524 pipeline_msg_req_invalid_handler;
526 if (f_handle == NULL)
527 f_handle = pipeline_msg_req_invalid_handler;
529 return f_handle(p, req);
533 pipeline_fc_msg_req_add_handler(struct pipeline *p, void *msg)
535 struct pipeline_fc_add_msg_req *req = msg;
536 struct pipeline_fc_add_msg_rsp *rsp = msg;
538 struct flow_table_entry entry = {
540 .action = RTE_PIPELINE_ACTION_PORT,
541 {.port_id = p->port_out_id[req->port_id]},
545 rsp->status = rte_pipeline_table_entry_add(p->p,
548 (struct rte_pipeline_table_entry *) &entry,
550 (struct rte_pipeline_table_entry **) &rsp->entry_ptr);
556 pipeline_fc_msg_req_add_bulk_handler(struct pipeline *p, void *msg)
558 struct pipeline_fc_add_bulk_msg_req *req = msg;
559 struct pipeline_fc_add_bulk_msg_rsp *rsp = msg;
562 for (i = 0; i < req->n_keys; i++) {
563 struct pipeline_fc_add_bulk_flow_req *flow_req = &req->req[i];
564 struct pipeline_fc_add_bulk_flow_rsp *flow_rsp = &req->rsp[i];
566 struct flow_table_entry entry = {
568 .action = RTE_PIPELINE_ACTION_PORT,
569 {.port_id = p->port_out_id[flow_req->port_id]},
573 int status = rte_pipeline_table_entry_add(p->p,
576 (struct rte_pipeline_table_entry *) &entry,
577 &flow_rsp->key_found,
578 (struct rte_pipeline_table_entry **)
579 &flow_rsp->entry_ptr);
591 pipeline_fc_msg_req_del_handler(struct pipeline *p, void *msg)
593 struct pipeline_fc_del_msg_req *req = msg;
594 struct pipeline_fc_del_msg_rsp *rsp = msg;
596 rsp->status = rte_pipeline_table_entry_delete(p->p,
606 pipeline_fc_msg_req_add_default_handler(struct pipeline *p, void *msg)
608 struct pipeline_fc_add_default_msg_req *req = msg;
609 struct pipeline_fc_add_default_msg_rsp *rsp = msg;
611 struct flow_table_entry default_entry = {
613 .action = RTE_PIPELINE_ACTION_PORT,
614 {.port_id = p->port_out_id[req->port_id]},
618 rsp->status = rte_pipeline_table_default_entry_add(p->p,
620 (struct rte_pipeline_table_entry *) &default_entry,
621 (struct rte_pipeline_table_entry **) &rsp->entry_ptr);
627 pipeline_fc_msg_req_del_default_handler(struct pipeline *p, void *msg)
629 struct pipeline_fc_del_default_msg_rsp *rsp = msg;
631 rsp->status = rte_pipeline_table_default_entry_delete(p->p,
638 struct pipeline_be_ops pipeline_flow_classification_be_ops = {
639 .f_init = pipeline_fc_init,
640 .f_free = pipeline_fc_free,
642 .f_timer = pipeline_fc_timer,
643 .f_track = pipeline_fc_track,