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)
158 key_offset_present = 1;
160 p->key_offset = atoi(arg_value);
166 if (strcmp(arg_name, "key_size") == 0) {
167 if (key_size_present)
169 key_size_present = 1;
171 p->key_size = atoi(arg_value);
172 if ((p->key_size == 0) ||
173 (p->key_size > PIPELINE_FC_FLOW_KEY_MAX_SIZE) ||
181 if (strcmp(arg_name, "key_mask") == 0) {
182 if (key_mask_present)
185 key_mask_str = strdup(arg_value);
186 if (key_mask_str == NULL)
189 key_mask_present = 1;
195 if (strcmp(arg_name, "hash_offset") == 0) {
196 if (hash_offset_present)
198 hash_offset_present = 1;
200 p->hash_offset = atoi(arg_value);
205 /* Unknown argument */
209 /* Check that mandatory arguments are present */
210 if ((n_flows_present == 0) ||
211 (key_offset_present == 0) ||
212 (key_size_present == 0))
215 if (key_mask_present) {
216 p->key_mask = rte_malloc(NULL, p->key_size, 0);
217 if (p->key_mask == NULL)
220 if (parse_hex_string(key_mask_str, p->key_mask, &p->key_size)
232 static void *pipeline_fc_init(struct pipeline_params *params,
233 __rte_unused void *arg)
236 struct pipeline_flow_classification *p_fc;
239 /* Check input arguments */
243 /* Memory allocation */
244 size = RTE_CACHE_LINE_ROUNDUP(
245 sizeof(struct pipeline_flow_classification));
246 p = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
249 p_fc = (struct pipeline_flow_classification *) p;
251 strcpy(p->name, params->name);
252 p->log_level = params->log_level;
254 PLOG(p, HIGH, "Flow classification");
256 /* Parse arguments */
257 if (pipeline_fc_parse_args(p_fc, params))
262 struct rte_pipeline_params pipeline_params = {
263 .name = params->name,
264 .socket_id = params->socket_id,
268 p->p = rte_pipeline_create(&pipeline_params);
276 p->n_ports_in = params->n_ports_in;
277 for (i = 0; i < p->n_ports_in; i++) {
278 struct rte_pipeline_port_in_params port_params = {
279 .ops = pipeline_port_in_params_get_ops(
280 ¶ms->port_in[i]),
281 .arg_create = pipeline_port_in_params_convert(
282 ¶ms->port_in[i]),
285 .burst_size = params->port_in[i].burst_size,
288 int status = rte_pipeline_port_in_create(p->p,
293 rte_pipeline_free(p->p);
300 p->n_ports_out = params->n_ports_out;
301 for (i = 0; i < p->n_ports_out; i++) {
302 struct rte_pipeline_port_out_params port_params = {
303 .ops = pipeline_port_out_params_get_ops(
304 ¶ms->port_out[i]),
305 .arg_create = pipeline_port_out_params_convert(
306 ¶ms->port_out[i]),
308 .f_action_bulk = NULL,
312 int status = rte_pipeline_port_out_create(p->p,
317 rte_pipeline_free(p->p);
326 struct rte_table_hash_key8_ext_params
327 table_hash_key8_params = {
328 .n_entries = p_fc->n_flows,
329 .n_entries_ext = p_fc->n_flows,
330 .signature_offset = p_fc->hash_offset,
331 .key_offset = p_fc->key_offset,
332 .f_hash = hash_func[(p_fc->key_size / 8) - 1],
333 .key_mask = p_fc->key_mask,
337 struct rte_table_hash_key16_ext_params
338 table_hash_key16_params = {
339 .n_entries = p_fc->n_flows,
340 .n_entries_ext = p_fc->n_flows,
341 .signature_offset = p_fc->hash_offset,
342 .key_offset = p_fc->key_offset,
343 .f_hash = hash_func[(p_fc->key_size / 8) - 1],
344 .key_mask = p_fc->key_mask,
348 struct rte_table_hash_ext_params
349 table_hash_params = {
350 .key_size = p_fc->key_size,
351 .n_keys = p_fc->n_flows,
352 .n_buckets = p_fc->n_flows / 4,
353 .n_buckets_ext = p_fc->n_flows / 4,
354 .f_hash = hash_func[(p_fc->key_size / 8) - 1],
356 .signature_offset = p_fc->hash_offset,
357 .key_offset = p_fc->key_offset,
360 struct rte_pipeline_table_params table_params = {
361 .ops = NULL, /* set below */
362 .arg_create = NULL, /* set below */
363 .f_action_hit = NULL,
364 .f_action_miss = NULL,
366 .action_data_size = sizeof(struct flow_table_entry) -
367 sizeof(struct rte_pipeline_table_entry),
372 switch (p_fc->key_size) {
374 if (p_fc->hash_offset != 0) {
376 &rte_table_hash_key8_ext_ops;
379 &rte_table_hash_key8_ext_dosig_ops;
381 table_params.arg_create = &table_hash_key8_params;
386 if (p_fc->hash_offset != 0) {
388 &rte_table_hash_key16_ext_ops;
391 &rte_table_hash_key16_ext_dosig_ops;
393 table_params.arg_create = &table_hash_key16_params;
397 table_params.ops = &rte_table_hash_ext_ops;
398 table_params.arg_create = &table_hash_params;
401 status = rte_pipeline_table_create(p->p,
406 rte_pipeline_free(p->p);
412 /* Connecting input ports to tables */
413 for (i = 0; i < p->n_ports_in; i++) {
414 int status = rte_pipeline_port_in_connect_to_table(p->p,
419 rte_pipeline_free(p->p);
425 /* Enable input ports */
426 for (i = 0; i < p->n_ports_in; i++) {
427 int status = rte_pipeline_port_in_enable(p->p,
431 rte_pipeline_free(p->p);
437 /* Check pipeline consistency */
438 if (rte_pipeline_check(p->p) < 0) {
439 rte_pipeline_free(p->p);
445 p->n_msgq = params->n_msgq;
446 for (i = 0; i < p->n_msgq; i++)
447 p->msgq_in[i] = params->msgq_in[i];
448 for (i = 0; i < p->n_msgq; i++)
449 p->msgq_out[i] = params->msgq_out[i];
451 /* Message handlers */
452 memcpy(p->handlers, handlers, sizeof(p->handlers));
453 memcpy(p_fc->custom_handlers,
455 sizeof(p_fc->custom_handlers));
461 pipeline_fc_free(void *pipeline)
463 struct pipeline *p = (struct pipeline *) pipeline;
465 /* Check input arguments */
470 rte_pipeline_free(p->p);
476 pipeline_fc_track(void *pipeline,
477 __rte_unused uint32_t port_in,
480 struct pipeline *p = (struct pipeline *) pipeline;
482 /* Check input arguments */
484 (port_in >= p->n_ports_in) ||
488 if (p->n_ports_in == 1) {
497 pipeline_fc_timer(void *pipeline)
499 struct pipeline *p = (struct pipeline *) pipeline;
501 pipeline_msg_req_handle(p);
502 rte_pipeline_flush(p->p);
508 pipeline_fc_msg_req_custom_handler(struct pipeline *p, void *msg)
510 struct pipeline_flow_classification *p_fc =
511 (struct pipeline_flow_classification *) p;
512 struct pipeline_custom_msg_req *req = msg;
513 pipeline_msg_req_handler f_handle;
515 f_handle = (req->subtype < PIPELINE_FC_MSG_REQS) ?
516 p_fc->custom_handlers[req->subtype] :
517 pipeline_msg_req_invalid_handler;
519 if (f_handle == NULL)
520 f_handle = pipeline_msg_req_invalid_handler;
522 return f_handle(p, req);
526 pipeline_fc_msg_req_add_handler(struct pipeline *p, void *msg)
528 struct pipeline_fc_add_msg_req *req = msg;
529 struct pipeline_fc_add_msg_rsp *rsp = msg;
531 struct flow_table_entry entry = {
533 .action = RTE_PIPELINE_ACTION_PORT,
534 {.port_id = p->port_out_id[req->port_id]},
538 rsp->status = rte_pipeline_table_entry_add(p->p,
541 (struct rte_pipeline_table_entry *) &entry,
543 (struct rte_pipeline_table_entry **) &rsp->entry_ptr);
549 pipeline_fc_msg_req_add_bulk_handler(struct pipeline *p, void *msg)
551 struct pipeline_fc_add_bulk_msg_req *req = msg;
552 struct pipeline_fc_add_bulk_msg_rsp *rsp = msg;
555 for (i = 0; i < req->n_keys; i++) {
556 struct pipeline_fc_add_bulk_flow_req *flow_req = &req->req[i];
557 struct pipeline_fc_add_bulk_flow_rsp *flow_rsp = &req->rsp[i];
559 struct flow_table_entry entry = {
561 .action = RTE_PIPELINE_ACTION_PORT,
562 {.port_id = p->port_out_id[flow_req->port_id]},
566 int status = rte_pipeline_table_entry_add(p->p,
569 (struct rte_pipeline_table_entry *) &entry,
570 &flow_rsp->key_found,
571 (struct rte_pipeline_table_entry **)
572 &flow_rsp->entry_ptr);
584 pipeline_fc_msg_req_del_handler(struct pipeline *p, void *msg)
586 struct pipeline_fc_del_msg_req *req = msg;
587 struct pipeline_fc_del_msg_rsp *rsp = msg;
589 rsp->status = rte_pipeline_table_entry_delete(p->p,
599 pipeline_fc_msg_req_add_default_handler(struct pipeline *p, void *msg)
601 struct pipeline_fc_add_default_msg_req *req = msg;
602 struct pipeline_fc_add_default_msg_rsp *rsp = msg;
604 struct flow_table_entry default_entry = {
606 .action = RTE_PIPELINE_ACTION_PORT,
607 {.port_id = p->port_out_id[req->port_id]},
611 rsp->status = rte_pipeline_table_default_entry_add(p->p,
613 (struct rte_pipeline_table_entry *) &default_entry,
614 (struct rte_pipeline_table_entry **) &rsp->entry_ptr);
620 pipeline_fc_msg_req_del_default_handler(struct pipeline *p, void *msg)
622 struct pipeline_fc_del_default_msg_rsp *rsp = msg;
624 rsp->status = rte_pipeline_table_default_entry_delete(p->p,
631 struct pipeline_be_ops pipeline_flow_classification_be_ops = {
632 .f_init = pipeline_fc_init,
633 .f_free = pipeline_fc_free,
635 .f_timer = pipeline_fc_timer,
636 .f_track = pipeline_fc_track,