1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <rte_common.h>
9 #include <rte_malloc.h>
12 #include "rte_table_acl.h"
14 #ifdef RTE_TABLE_STATS_COLLECT
16 #define RTE_TABLE_ACL_STATS_PKTS_IN_ADD(table, val) \
17 table->stats.n_pkts_in += val
18 #define RTE_TABLE_ACL_STATS_PKTS_LOOKUP_MISS(table, val) \
19 table->stats.n_pkts_lookup_miss += val
23 #define RTE_TABLE_ACL_STATS_PKTS_IN_ADD(table, val)
24 #define RTE_TABLE_ACL_STATS_PKTS_LOOKUP_MISS(table, val)
28 struct rte_table_acl {
29 struct rte_table_stats stats;
31 /* Low-level ACL table */
32 char name[2][RTE_ACL_NAMESIZE];
33 struct rte_acl_param acl_params; /* for creating low level acl table */
34 struct rte_acl_config cfg; /* Holds the field definitions (metadata) */
35 struct rte_acl_ctx *ctx;
38 /* Input parameters */
43 uint8_t *action_table;
44 struct rte_acl_rule **acl_rule_list; /* Array of pointers to rules */
45 uint8_t *acl_rule_memory; /* Memory to store the rules */
47 /* Memory to store the action table and stack of free entries */
48 uint8_t memory[0] __rte_cache_aligned;
58 struct rte_table_acl_params *p = params;
59 struct rte_table_acl *acl;
60 uint32_t action_table_size, acl_rule_list_size, acl_rule_memory_size;
63 RTE_BUILD_BUG_ON(((sizeof(struct rte_table_acl) % RTE_CACHE_LINE_SIZE)
66 /* Check input parameters */
68 RTE_LOG(ERR, TABLE, "%s: Invalid value for params\n", __func__);
71 if (p->name == NULL) {
72 RTE_LOG(ERR, TABLE, "%s: Invalid value for name\n", __func__);
75 if (p->n_rules == 0) {
76 RTE_LOG(ERR, TABLE, "%s: Invalid value for n_rules\n",
80 if ((p->n_rule_fields == 0) ||
81 (p->n_rule_fields > RTE_ACL_MAX_FIELDS)) {
82 RTE_LOG(ERR, TABLE, "%s: Invalid value for n_rule_fields\n",
87 entry_size = RTE_ALIGN(entry_size, sizeof(uint64_t));
89 /* Memory allocation */
90 action_table_size = RTE_CACHE_LINE_ROUNDUP(p->n_rules * entry_size);
92 RTE_CACHE_LINE_ROUNDUP(p->n_rules * sizeof(struct rte_acl_rule *));
93 acl_rule_memory_size = RTE_CACHE_LINE_ROUNDUP(p->n_rules *
94 RTE_ACL_RULE_SZ(p->n_rule_fields));
95 total_size = sizeof(struct rte_table_acl) + action_table_size +
96 acl_rule_list_size + acl_rule_memory_size;
98 acl = rte_zmalloc_socket("TABLE", total_size, RTE_CACHE_LINE_SIZE,
102 "%s: Cannot allocate %u bytes for ACL table\n",
103 __func__, total_size);
107 acl->action_table = &acl->memory[0];
109 (struct rte_acl_rule **) &acl->memory[action_table_size];
110 acl->acl_rule_memory = (uint8_t *)
111 &acl->memory[action_table_size + acl_rule_list_size];
113 /* Initialization of internal fields */
114 snprintf(acl->name[0], RTE_ACL_NAMESIZE, "%s_a", p->name);
115 snprintf(acl->name[1], RTE_ACL_NAMESIZE, "%s_b", p->name);
118 acl->acl_params.name = acl->name[acl->name_id];
119 acl->acl_params.socket_id = socket_id;
120 acl->acl_params.rule_size = RTE_ACL_RULE_SZ(p->n_rule_fields);
121 acl->acl_params.max_rule_num = p->n_rules;
123 acl->cfg.num_categories = 1;
124 acl->cfg.num_fields = p->n_rule_fields;
125 memcpy(&acl->cfg.defs[0], &p->field_format[0],
126 p->n_rule_fields * sizeof(struct rte_acl_field_def));
130 acl->n_rules = p->n_rules;
131 acl->entry_size = entry_size;
137 rte_table_acl_free(void *table)
139 struct rte_table_acl *acl = table;
141 /* Check input parameters */
143 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
147 /* Free previously allocated resources */
148 if (acl->ctx != NULL)
149 rte_acl_free(acl->ctx);
156 RTE_ACL_RULE_DEF(rte_pipeline_acl_rule, RTE_ACL_MAX_FIELDS);
159 rte_table_acl_build(struct rte_table_acl *acl, struct rte_acl_ctx **acl_ctx)
161 struct rte_acl_ctx *ctx = NULL;
165 /* Create low level ACL table */
166 ctx = rte_acl_create(&acl->acl_params);
168 RTE_LOG(ERR, TABLE, "%s: Cannot create low level ACL table\n",
173 /* Add rules to low level ACL table */
175 for (i = 1; i < acl->n_rules; i++) {
176 if (acl->acl_rule_list[i] != NULL) {
177 status = rte_acl_add_rules(ctx, acl->acl_rule_list[i],
181 "%s: Cannot add rule to low level ACL table\n",
197 /* Build low level ACl table */
198 status = rte_acl_build(ctx, &acl->cfg);
201 "%s: Cannot build the low level ACL table\n",
212 rte_table_acl_entry_add(
219 struct rte_table_acl *acl = table;
220 struct rte_table_acl_rule_add_params *rule =
222 struct rte_pipeline_acl_rule acl_rule;
223 struct rte_acl_rule *rule_location;
224 struct rte_acl_ctx *ctx;
225 uint32_t free_pos, free_pos_valid, i;
228 /* Check input parameters */
230 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
234 RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
238 RTE_LOG(ERR, TABLE, "%s: entry parameter is NULL\n", __func__);
241 if (key_found == NULL) {
242 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
246 if (entry_ptr == NULL) {
247 RTE_LOG(ERR, TABLE, "%s: entry_ptr parameter is NULL\n",
251 if (rule->priority > RTE_ACL_MAX_PRIORITY) {
252 RTE_LOG(ERR, TABLE, "%s: Priority is too high\n", __func__);
256 /* Setup rule data structure */
257 memset(&acl_rule, 0, sizeof(acl_rule));
258 acl_rule.data.category_mask = 1;
259 acl_rule.data.priority = RTE_ACL_MAX_PRIORITY - rule->priority;
260 acl_rule.data.userdata = 0; /* To be set up later */
261 memcpy(&acl_rule.field[0],
262 &rule->field_value[0],
263 acl->cfg.num_fields * sizeof(struct rte_acl_field));
265 /* Look to see if the rule exists already in the table */
268 for (i = 1; i < acl->n_rules; i++) {
269 if (acl->acl_rule_list[i] == NULL) {
270 if (free_pos_valid == 0) {
278 /* Compare the key fields */
279 status = memcmp(&acl->acl_rule_list[i]->field[0],
280 &rule->field_value[0],
281 acl->cfg.num_fields * sizeof(struct rte_acl_field));
283 /* Rule found: update data associated with the rule */
286 *entry_ptr = &acl->memory[i * acl->entry_size];
287 memcpy(*entry_ptr, entry, acl->entry_size);
293 /* Return if max rules */
294 if (free_pos_valid == 0) {
295 RTE_LOG(ERR, TABLE, "%s: Max number of rules reached\n",
300 /* Add the new rule to the rule set */
301 acl_rule.data.userdata = free_pos;
302 rule_location = (struct rte_acl_rule *)
303 &acl->acl_rule_memory[free_pos * acl->acl_params.rule_size];
304 memcpy(rule_location, &acl_rule, acl->acl_params.rule_size);
305 acl->acl_rule_list[free_pos] = rule_location;
307 /* Build low level ACL table */
309 acl->acl_params.name = acl->name[acl->name_id];
310 status = rte_table_acl_build(acl, &ctx);
312 /* Roll back changes */
313 acl->acl_rule_list[free_pos] = NULL;
320 if (acl->ctx != NULL)
321 rte_acl_free(acl->ctx);
324 *entry_ptr = &acl->memory[free_pos * acl->entry_size];
325 memcpy(*entry_ptr, entry, acl->entry_size);
331 rte_table_acl_entry_delete(
337 struct rte_table_acl *acl = table;
338 struct rte_table_acl_rule_delete_params *rule =
340 struct rte_acl_rule *deleted_rule = NULL;
341 struct rte_acl_ctx *ctx;
342 uint32_t pos, pos_valid, i;
345 /* Check input parameters */
347 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
351 RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
354 if (key_found == NULL) {
355 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
360 /* Look for the rule in the table */
363 for (i = 1; i < acl->n_rules; i++) {
364 if (acl->acl_rule_list[i] != NULL) {
365 /* Compare the key fields */
366 status = memcmp(&acl->acl_rule_list[i]->field[0],
367 &rule->field_value[0], acl->cfg.num_fields *
368 sizeof(struct rte_acl_field));
370 /* Rule found: remove from table */
375 deleted_rule = acl->acl_rule_list[i];
376 acl->acl_rule_list[i] = NULL;
381 /* Return if rule not found */
382 if (pos_valid == 0) {
387 /* Build low level ACL table */
389 acl->acl_params.name = acl->name[acl->name_id];
390 status = rte_table_acl_build(acl, &ctx);
392 /* Roll back changes */
393 acl->acl_rule_list[pos] = deleted_rule;
400 if (acl->ctx != NULL)
401 rte_acl_free(acl->ctx);
406 memcpy(entry, &acl->memory[pos * acl->entry_size],
413 rte_table_acl_entry_add_bulk(
421 struct rte_table_acl *acl = table;
422 struct rte_acl_ctx *ctx;
423 uint32_t rule_pos[n_keys];
425 int err = 0, build = 0;
428 /* Check input parameters */
430 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
434 RTE_LOG(ERR, TABLE, "%s: keys parameter is NULL\n", __func__);
437 if (entries == NULL) {
438 RTE_LOG(ERR, TABLE, "%s: entries parameter is NULL\n", __func__);
442 RTE_LOG(ERR, TABLE, "%s: 0 rules to add\n", __func__);
445 if (key_found == NULL) {
446 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
450 if (entries_ptr == NULL) {
451 RTE_LOG(ERR, TABLE, "%s: entries_ptr parameter is NULL\n",
456 /* Check input parameters in arrays */
457 for (i = 0; i < n_keys; i++) {
458 struct rte_table_acl_rule_add_params *rule;
460 if (keys[i] == NULL) {
461 RTE_LOG(ERR, TABLE, "%s: keys[%" PRIu32 "] parameter is NULL\n",
466 if (entries[i] == NULL) {
467 RTE_LOG(ERR, TABLE, "%s: entries[%" PRIu32 "] parameter is NULL\n",
473 if (rule->priority > RTE_ACL_MAX_PRIORITY) {
474 RTE_LOG(ERR, TABLE, "%s: Priority is too high\n", __func__);
479 memset(rule_pos, 0, n_keys * sizeof(uint32_t));
480 memset(key_found, 0, n_keys * sizeof(int));
481 for (i = 0; i < n_keys; i++) {
482 struct rte_table_acl_rule_add_params *rule =
484 struct rte_pipeline_acl_rule acl_rule;
485 struct rte_acl_rule *rule_location;
486 uint32_t free_pos, free_pos_valid, j;
488 /* Setup rule data structure */
489 memset(&acl_rule, 0, sizeof(acl_rule));
490 acl_rule.data.category_mask = 1;
491 acl_rule.data.priority = RTE_ACL_MAX_PRIORITY - rule->priority;
492 acl_rule.data.userdata = 0; /* To be set up later */
493 memcpy(&acl_rule.field[0],
494 &rule->field_value[0],
495 acl->cfg.num_fields * sizeof(struct rte_acl_field));
497 /* Look to see if the rule exists already in the table */
500 for (j = 1; j < acl->n_rules; j++) {
501 if (acl->acl_rule_list[j] == NULL) {
502 if (free_pos_valid == 0) {
510 /* Compare the key fields */
511 status = memcmp(&acl->acl_rule_list[j]->field[0],
512 &rule->field_value[0],
513 acl->cfg.num_fields * sizeof(struct rte_acl_field));
515 /* Rule found: update data associated with the rule */
518 entries_ptr[i] = &acl->memory[j * acl->entry_size];
519 memcpy(entries_ptr[i], entries[i], acl->entry_size);
525 /* Key already in the table */
526 if (key_found[i] != 0)
529 /* Maximum number of rules reached */
530 if (free_pos_valid == 0) {
535 /* Add the new rule to the rule set */
536 acl_rule.data.userdata = free_pos;
537 rule_location = (struct rte_acl_rule *)
538 &acl->acl_rule_memory[free_pos * acl->acl_params.rule_size];
539 memcpy(rule_location, &acl_rule, acl->acl_params.rule_size);
540 acl->acl_rule_list[free_pos] = rule_location;
541 rule_pos[i] = free_pos;
546 for (i = 0; i < n_keys; i++) {
547 if (rule_pos[i] == 0)
550 acl->acl_rule_list[rule_pos[i]] = NULL;
559 /* Build low level ACL table */
561 acl->acl_params.name = acl->name[acl->name_id];
562 status = rte_table_acl_build(acl, &ctx);
564 /* Roll back changes */
565 for (i = 0; i < n_keys; i++) {
566 if (rule_pos[i] == 0)
569 acl->acl_rule_list[rule_pos[i]] = NULL;
577 if (acl->ctx != NULL)
578 rte_acl_free(acl->ctx);
581 for (i = 0; i < n_keys; i++) {
582 if (rule_pos[i] == 0)
586 entries_ptr[i] = &acl->memory[rule_pos[i] * acl->entry_size];
587 memcpy(entries_ptr[i], entries[i], acl->entry_size);
594 rte_table_acl_entry_delete_bulk(
601 struct rte_table_acl *acl = table;
602 struct rte_acl_rule *deleted_rules[n_keys];
603 uint32_t rule_pos[n_keys];
604 struct rte_acl_ctx *ctx;
609 /* Check input parameters */
611 RTE_LOG(ERR, TABLE, "%s: table parameter is NULL\n", __func__);
615 RTE_LOG(ERR, TABLE, "%s: key parameter is NULL\n", __func__);
619 RTE_LOG(ERR, TABLE, "%s: 0 rules to delete\n", __func__);
622 if (key_found == NULL) {
623 RTE_LOG(ERR, TABLE, "%s: key_found parameter is NULL\n",
628 for (i = 0; i < n_keys; i++) {
629 if (keys[i] == NULL) {
630 RTE_LOG(ERR, TABLE, "%s: keys[%" PRIu32 "] parameter is NULL\n",
636 memset(deleted_rules, 0, n_keys * sizeof(struct rte_acl_rule *));
637 memset(rule_pos, 0, n_keys * sizeof(uint32_t));
638 for (i = 0; i < n_keys; i++) {
639 struct rte_table_acl_rule_delete_params *rule =
641 uint32_t pos_valid, j;
643 /* Look for the rule in the table */
645 for (j = 1; j < acl->n_rules; j++) {
646 if (acl->acl_rule_list[j] == NULL)
649 /* Compare the key fields */
650 status = memcmp(&acl->acl_rule_list[j]->field[0],
651 &rule->field_value[0],
652 acl->cfg.num_fields * sizeof(struct rte_acl_field));
654 /* Rule found: remove from table */
658 deleted_rules[i] = acl->acl_rule_list[j];
659 acl->acl_rule_list[j] = NULL;
666 if (pos_valid == 0) {
672 /* Return if no changes to acl table */
677 /* Build low level ACL table */
679 acl->acl_params.name = acl->name[acl->name_id];
680 status = rte_table_acl_build(acl, &ctx);
682 /* Roll back changes */
683 for (i = 0; i < n_keys; i++) {
684 if (rule_pos[i] == 0)
687 acl->acl_rule_list[rule_pos[i]] = deleted_rules[i];
696 if (acl->ctx != NULL)
697 rte_acl_free(acl->ctx);
700 for (i = 0; i < n_keys; i++) {
701 if (rule_pos[i] == 0)
705 if (entries != NULL && entries[i] != NULL)
706 memcpy(entries[i], &acl->memory[rule_pos[i] * acl->entry_size],
714 rte_table_acl_lookup(
716 struct rte_mbuf **pkts,
718 uint64_t *lookup_hit_mask,
721 struct rte_table_acl *acl = (struct rte_table_acl *) table;
722 const uint8_t *pkts_data[RTE_PORT_IN_BURST_SIZE_MAX];
723 uint32_t results[RTE_PORT_IN_BURST_SIZE_MAX];
724 uint64_t pkts_out_mask;
725 uint32_t n_pkts, i, j;
727 __rte_unused uint32_t n_pkts_in = __builtin_popcountll(pkts_mask);
728 RTE_TABLE_ACL_STATS_PKTS_IN_ADD(acl, n_pkts_in);
730 /* Input conversion */
731 for (i = 0, j = 0; i < (uint32_t)(RTE_PORT_IN_BURST_SIZE_MAX -
732 __builtin_clzll(pkts_mask)); i++) {
733 uint64_t pkt_mask = 1LLU << i;
735 if (pkt_mask & pkts_mask) {
736 pkts_data[j] = rte_pktmbuf_mtod(pkts[i], uint8_t *);
742 /* Low-level ACL table lookup */
743 if (acl->ctx != NULL)
744 rte_acl_classify(acl->ctx, pkts_data, results, n_pkts, 1);
748 /* Output conversion */
750 for (i = 0; i < n_pkts; i++) {
751 uint32_t action_table_pos = results[i];
752 uint32_t pkt_pos = __builtin_ctzll(pkts_mask);
753 uint64_t pkt_mask = 1LLU << pkt_pos;
755 pkts_mask &= ~pkt_mask;
757 if (action_table_pos != 0) {
758 pkts_out_mask |= pkt_mask;
759 entries[pkt_pos] = (void *)
760 &acl->memory[action_table_pos *
762 rte_prefetch0(entries[pkt_pos]);
766 *lookup_hit_mask = pkts_out_mask;
767 RTE_TABLE_ACL_STATS_PKTS_LOOKUP_MISS(acl, n_pkts_in - __builtin_popcountll(pkts_out_mask));
773 rte_table_acl_stats_read(void *table, struct rte_table_stats *stats, int clear)
775 struct rte_table_acl *acl = table;
778 memcpy(stats, &acl->stats, sizeof(acl->stats));
781 memset(&acl->stats, 0, sizeof(acl->stats));
786 struct rte_table_ops rte_table_acl_ops = {
787 .f_create = rte_table_acl_create,
788 .f_free = rte_table_acl_free,
789 .f_add = rte_table_acl_entry_add,
790 .f_delete = rte_table_acl_entry_delete,
791 .f_add_bulk = rte_table_acl_entry_add_bulk,
792 .f_delete_bulk = rte_table_acl_entry_delete_bulk,
793 .f_lookup = rte_table_acl_lookup,
794 .f_stats = rte_table_acl_stats_read,