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 "pipeline_actions_common.h"
44 #include "hash_func.h"
46 struct pipeline_flow_classification {
48 pipeline_msg_req_handler custom_handlers[PIPELINE_FC_MSG_REQS];
57 uint32_t flow_id_offset;
59 } __rte_cache_aligned;
62 pipeline_fc_msg_req_custom_handler(struct pipeline *p, void *msg);
64 static pipeline_msg_req_handler handlers[] = {
65 [PIPELINE_MSG_REQ_PING] =
66 pipeline_msg_req_ping_handler,
67 [PIPELINE_MSG_REQ_STATS_PORT_IN] =
68 pipeline_msg_req_stats_port_in_handler,
69 [PIPELINE_MSG_REQ_STATS_PORT_OUT] =
70 pipeline_msg_req_stats_port_out_handler,
71 [PIPELINE_MSG_REQ_STATS_TABLE] =
72 pipeline_msg_req_stats_table_handler,
73 [PIPELINE_MSG_REQ_PORT_IN_ENABLE] =
74 pipeline_msg_req_port_in_enable_handler,
75 [PIPELINE_MSG_REQ_PORT_IN_DISABLE] =
76 pipeline_msg_req_port_in_disable_handler,
77 [PIPELINE_MSG_REQ_CUSTOM] =
78 pipeline_fc_msg_req_custom_handler,
82 pipeline_fc_msg_req_add_handler(struct pipeline *p, void *msg);
85 pipeline_fc_msg_req_add_bulk_handler(struct pipeline *p, void *msg);
88 pipeline_fc_msg_req_del_handler(struct pipeline *p, void *msg);
91 pipeline_fc_msg_req_add_default_handler(struct pipeline *p, void *msg);
94 pipeline_fc_msg_req_del_default_handler(struct pipeline *p, void *msg);
96 static pipeline_msg_req_handler custom_handlers[] = {
97 [PIPELINE_FC_MSG_REQ_FLOW_ADD] =
98 pipeline_fc_msg_req_add_handler,
99 [PIPELINE_FC_MSG_REQ_FLOW_ADD_BULK] =
100 pipeline_fc_msg_req_add_bulk_handler,
101 [PIPELINE_FC_MSG_REQ_FLOW_DEL] =
102 pipeline_fc_msg_req_del_handler,
103 [PIPELINE_FC_MSG_REQ_FLOW_ADD_DEFAULT] =
104 pipeline_fc_msg_req_add_default_handler,
105 [PIPELINE_FC_MSG_REQ_FLOW_DEL_DEFAULT] =
106 pipeline_fc_msg_req_del_default_handler,
112 struct flow_table_entry {
113 struct rte_pipeline_table_entry head;
119 rte_table_hash_op_hash hash_func[] = {
131 * Flow table AH - Write flow_id to packet meta-data
135 struct rte_mbuf *pkt,
136 struct rte_pipeline_table_entry *table_entry,
139 struct pipeline_flow_classification *p_fc = arg;
140 uint32_t *flow_id_ptr =
141 RTE_MBUF_METADATA_UINT32_PTR(pkt, p_fc->flow_id_offset);
142 struct flow_table_entry *entry =
143 (struct flow_table_entry *) table_entry;
146 uint32_t flow_id = entry->flow_id;
151 *flow_id_ptr = flow_id;
156 struct rte_mbuf **pkts,
157 struct rte_pipeline_table_entry **table_entries,
160 struct pipeline_flow_classification *p_fc = arg;
162 uint32_t *flow_id_ptr0 =
163 RTE_MBUF_METADATA_UINT32_PTR(pkts[0], p_fc->flow_id_offset);
164 uint32_t *flow_id_ptr1 =
165 RTE_MBUF_METADATA_UINT32_PTR(pkts[1], p_fc->flow_id_offset);
166 uint32_t *flow_id_ptr2 =
167 RTE_MBUF_METADATA_UINT32_PTR(pkts[2], p_fc->flow_id_offset);
168 uint32_t *flow_id_ptr3 =
169 RTE_MBUF_METADATA_UINT32_PTR(pkts[3], p_fc->flow_id_offset);
171 struct flow_table_entry *entry0 =
172 (struct flow_table_entry *) table_entries[0];
173 struct flow_table_entry *entry1 =
174 (struct flow_table_entry *) table_entries[1];
175 struct flow_table_entry *entry2 =
176 (struct flow_table_entry *) table_entries[2];
177 struct flow_table_entry *entry3 =
178 (struct flow_table_entry *) table_entries[3];
181 uint32_t flow_id0 = entry0->flow_id;
182 uint32_t flow_id1 = entry1->flow_id;
183 uint32_t flow_id2 = entry2->flow_id;
184 uint32_t flow_id3 = entry3->flow_id;
189 *flow_id_ptr0 = flow_id0;
190 *flow_id_ptr1 = flow_id1;
191 *flow_id_ptr2 = flow_id2;
192 *flow_id_ptr3 = flow_id3;
195 PIPELINE_TABLE_AH_HIT(fc_table_ah_hit,
196 pkt_work_flow_id, pkt4_work_flow_id);
198 static rte_pipeline_table_action_handler_hit
199 get_fc_table_ah_hit(struct pipeline_flow_classification *p)
202 return fc_table_ah_hit;
211 pipeline_fc_parse_args(struct pipeline_flow_classification *p,
212 struct pipeline_params *params)
214 uint32_t n_flows_present = 0;
215 uint32_t key_offset_present = 0;
216 uint32_t key_size_present = 0;
217 uint32_t hash_offset_present = 0;
218 uint32_t key_mask_present = 0;
219 uint32_t flow_id_offset_present = 0;
222 char *key_mask_str = NULL;
229 for (i = 0; i < params->n_args; i++) {
230 char *arg_name = params->args_name[i];
231 char *arg_value = params->args_value[i];
234 if (strcmp(arg_name, "n_flows") == 0) {
239 p->n_flows = atoi(arg_value);
247 if (strcmp(arg_name, "key_offset") == 0) {
248 if (key_offset_present)
251 key_offset_present = 1;
253 p->key_offset = atoi(arg_value);
259 if (strcmp(arg_name, "key_size") == 0) {
260 if (key_size_present)
262 key_size_present = 1;
264 p->key_size = atoi(arg_value);
265 if ((p->key_size == 0) ||
266 (p->key_size > PIPELINE_FC_FLOW_KEY_MAX_SIZE) ||
274 if (strcmp(arg_name, "key_mask") == 0) {
275 if (key_mask_present)
278 key_mask_str = strdup(arg_value);
279 if (key_mask_str == NULL)
282 key_mask_present = 1;
288 if (strcmp(arg_name, "hash_offset") == 0) {
289 if (hash_offset_present)
291 hash_offset_present = 1;
293 p->hash_offset = atoi(arg_value);
299 if (strcmp(arg_name, "flowid_offset") == 0) {
300 if (flow_id_offset_present)
302 flow_id_offset_present = 1;
305 p->flow_id_offset = atoi(arg_value);
310 /* Unknown argument */
314 /* Check that mandatory arguments are present */
315 if ((n_flows_present == 0) ||
316 (key_offset_present == 0) ||
317 (key_size_present == 0))
320 if (key_mask_present) {
321 p->key_mask = rte_malloc(NULL, p->key_size, 0);
322 if (p->key_mask == NULL)
325 if (parse_hex_string(key_mask_str, p->key_mask, &p->key_size)
336 if (key_mask_str != NULL)
338 if (p->key_mask != NULL)
343 static void *pipeline_fc_init(struct pipeline_params *params,
344 __rte_unused void *arg)
347 struct pipeline_flow_classification *p_fc;
350 /* Check input arguments */
354 /* Memory allocation */
355 size = RTE_CACHE_LINE_ROUNDUP(
356 sizeof(struct pipeline_flow_classification));
357 p = rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE);
360 p_fc = (struct pipeline_flow_classification *) p;
362 strcpy(p->name, params->name);
363 p->log_level = params->log_level;
365 PLOG(p, HIGH, "Flow classification");
367 /* Parse arguments */
368 if (pipeline_fc_parse_args(p_fc, params))
373 struct rte_pipeline_params pipeline_params = {
374 .name = params->name,
375 .socket_id = params->socket_id,
379 p->p = rte_pipeline_create(&pipeline_params);
387 p->n_ports_in = params->n_ports_in;
388 for (i = 0; i < p->n_ports_in; i++) {
389 struct rte_pipeline_port_in_params port_params = {
390 .ops = pipeline_port_in_params_get_ops(
391 ¶ms->port_in[i]),
392 .arg_create = pipeline_port_in_params_convert(
393 ¶ms->port_in[i]),
396 .burst_size = params->port_in[i].burst_size,
399 int status = rte_pipeline_port_in_create(p->p,
404 rte_pipeline_free(p->p);
411 p->n_ports_out = params->n_ports_out;
412 for (i = 0; i < p->n_ports_out; i++) {
413 struct rte_pipeline_port_out_params port_params = {
414 .ops = pipeline_port_out_params_get_ops(
415 ¶ms->port_out[i]),
416 .arg_create = pipeline_port_out_params_convert(
417 ¶ms->port_out[i]),
419 .f_action_bulk = NULL,
423 int status = rte_pipeline_port_out_create(p->p,
428 rte_pipeline_free(p->p);
437 struct rte_table_hash_key8_ext_params
438 table_hash_key8_params = {
439 .n_entries = p_fc->n_flows,
440 .n_entries_ext = p_fc->n_flows,
441 .signature_offset = p_fc->hash_offset,
442 .key_offset = p_fc->key_offset,
443 .f_hash = hash_func[(p_fc->key_size / 8) - 1],
444 .key_mask = p_fc->key_mask,
448 struct rte_table_hash_key16_ext_params
449 table_hash_key16_params = {
450 .n_entries = p_fc->n_flows,
451 .n_entries_ext = p_fc->n_flows,
452 .signature_offset = p_fc->hash_offset,
453 .key_offset = p_fc->key_offset,
454 .f_hash = hash_func[(p_fc->key_size / 8) - 1],
455 .key_mask = p_fc->key_mask,
459 struct rte_table_hash_ext_params
460 table_hash_params = {
461 .key_size = p_fc->key_size,
462 .n_keys = p_fc->n_flows,
463 .n_buckets = p_fc->n_flows / 4,
464 .n_buckets_ext = p_fc->n_flows / 4,
465 .f_hash = hash_func[(p_fc->key_size / 8) - 1],
467 .signature_offset = p_fc->hash_offset,
468 .key_offset = p_fc->key_offset,
471 struct rte_pipeline_table_params table_params = {
472 .ops = NULL, /* set below */
473 .arg_create = NULL, /* set below */
474 .f_action_hit = get_fc_table_ah_hit(p_fc),
475 .f_action_miss = NULL,
477 .action_data_size = sizeof(struct flow_table_entry) -
478 sizeof(struct rte_pipeline_table_entry),
483 switch (p_fc->key_size) {
485 if (p_fc->hash_offset != 0) {
487 &rte_table_hash_key8_ext_ops;
490 &rte_table_hash_key8_ext_dosig_ops;
492 table_params.arg_create = &table_hash_key8_params;
496 if (p_fc->hash_offset != 0) {
498 &rte_table_hash_key16_ext_ops;
501 &rte_table_hash_key16_ext_dosig_ops;
503 table_params.arg_create = &table_hash_key16_params;
507 table_params.ops = &rte_table_hash_ext_ops;
508 table_params.arg_create = &table_hash_params;
511 status = rte_pipeline_table_create(p->p,
516 rte_pipeline_free(p->p);
522 /* Connecting input ports to tables */
523 for (i = 0; i < p->n_ports_in; i++) {
524 int status = rte_pipeline_port_in_connect_to_table(p->p,
529 rte_pipeline_free(p->p);
535 /* Enable input ports */
536 for (i = 0; i < p->n_ports_in; i++) {
537 int status = rte_pipeline_port_in_enable(p->p,
541 rte_pipeline_free(p->p);
547 /* Check pipeline consistency */
548 if (rte_pipeline_check(p->p) < 0) {
549 rte_pipeline_free(p->p);
555 p->n_msgq = params->n_msgq;
556 for (i = 0; i < p->n_msgq; i++)
557 p->msgq_in[i] = params->msgq_in[i];
558 for (i = 0; i < p->n_msgq; i++)
559 p->msgq_out[i] = params->msgq_out[i];
561 /* Message handlers */
562 memcpy(p->handlers, handlers, sizeof(p->handlers));
563 memcpy(p_fc->custom_handlers,
565 sizeof(p_fc->custom_handlers));
571 pipeline_fc_free(void *pipeline)
573 struct pipeline *p = (struct pipeline *) pipeline;
575 /* Check input arguments */
580 rte_pipeline_free(p->p);
586 pipeline_fc_track(void *pipeline,
587 __rte_unused uint32_t port_in,
590 struct pipeline *p = (struct pipeline *) pipeline;
592 /* Check input arguments */
594 (port_in >= p->n_ports_in) ||
598 if (p->n_ports_in == 1) {
607 pipeline_fc_timer(void *pipeline)
609 struct pipeline *p = (struct pipeline *) pipeline;
611 pipeline_msg_req_handle(p);
612 rte_pipeline_flush(p->p);
618 pipeline_fc_msg_req_custom_handler(struct pipeline *p, void *msg)
620 struct pipeline_flow_classification *p_fc =
621 (struct pipeline_flow_classification *) p;
622 struct pipeline_custom_msg_req *req = msg;
623 pipeline_msg_req_handler f_handle;
625 f_handle = (req->subtype < PIPELINE_FC_MSG_REQS) ?
626 p_fc->custom_handlers[req->subtype] :
627 pipeline_msg_req_invalid_handler;
629 if (f_handle == NULL)
630 f_handle = pipeline_msg_req_invalid_handler;
632 return f_handle(p, req);
636 pipeline_fc_msg_req_add_handler(struct pipeline *p, void *msg)
638 struct pipeline_fc_add_msg_req *req = msg;
639 struct pipeline_fc_add_msg_rsp *rsp = msg;
641 struct flow_table_entry entry = {
643 .action = RTE_PIPELINE_ACTION_PORT,
644 {.port_id = p->port_out_id[req->port_id]},
646 .flow_id = req->flow_id,
649 rsp->status = rte_pipeline_table_entry_add(p->p,
652 (struct rte_pipeline_table_entry *) &entry,
654 (struct rte_pipeline_table_entry **) &rsp->entry_ptr);
660 pipeline_fc_msg_req_add_bulk_handler(struct pipeline *p, void *msg)
662 struct pipeline_fc_add_bulk_msg_req *req = msg;
663 struct pipeline_fc_add_bulk_msg_rsp *rsp = msg;
666 for (i = 0; i < req->n_keys; i++) {
667 struct pipeline_fc_add_bulk_flow_req *flow_req = &req->req[i];
668 struct pipeline_fc_add_bulk_flow_rsp *flow_rsp = &req->rsp[i];
670 struct flow_table_entry entry = {
672 .action = RTE_PIPELINE_ACTION_PORT,
673 {.port_id = p->port_out_id[flow_req->port_id]},
675 .flow_id = flow_req->flow_id,
678 int status = rte_pipeline_table_entry_add(p->p,
681 (struct rte_pipeline_table_entry *) &entry,
682 &flow_rsp->key_found,
683 (struct rte_pipeline_table_entry **)
684 &flow_rsp->entry_ptr);
696 pipeline_fc_msg_req_del_handler(struct pipeline *p, void *msg)
698 struct pipeline_fc_del_msg_req *req = msg;
699 struct pipeline_fc_del_msg_rsp *rsp = msg;
701 rsp->status = rte_pipeline_table_entry_delete(p->p,
711 pipeline_fc_msg_req_add_default_handler(struct pipeline *p, void *msg)
713 struct pipeline_fc_add_default_msg_req *req = msg;
714 struct pipeline_fc_add_default_msg_rsp *rsp = msg;
716 struct flow_table_entry default_entry = {
718 .action = RTE_PIPELINE_ACTION_PORT,
719 {.port_id = p->port_out_id[req->port_id]},
725 rsp->status = rte_pipeline_table_default_entry_add(p->p,
727 (struct rte_pipeline_table_entry *) &default_entry,
728 (struct rte_pipeline_table_entry **) &rsp->entry_ptr);
734 pipeline_fc_msg_req_del_default_handler(struct pipeline *p, void *msg)
736 struct pipeline_fc_del_default_msg_rsp *rsp = msg;
738 rsp->status = rte_pipeline_table_default_entry_delete(p->p,
745 struct pipeline_be_ops pipeline_flow_classification_be_ops = {
746 .f_init = pipeline_fc_init,
747 .f_free = pipeline_fc_free,
749 .f_timer = pipeline_fc_timer,
750 .f_track = pipeline_fc_track,