1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
5 #include <rte_string_fns.h>
6 #include <rte_compat.h>
7 #include <rte_flow_classify.h>
8 #include "rte_flow_classify_parse.h"
9 #include <rte_flow_driver.h>
10 #include <rte_table_acl.h>
13 static uint32_t unique_id = 1;
15 enum rte_flow_classify_table_type table_type
16 = RTE_FLOW_CLASSIFY_TABLE_TYPE_NONE;
18 struct rte_flow_classify_table_entry {
19 /* meta-data for classify rule */
23 struct classify_action action;
26 struct rte_cls_table {
27 /* Input parameters */
28 struct rte_table_ops ops;
30 enum rte_flow_classify_table_type type;
32 /* Handle to the low-level table object */
36 #define RTE_FLOW_CLASSIFIER_MAX_NAME_SZ 256
38 struct rte_flow_classifier {
39 /* Input parameters */
40 char name[RTE_FLOW_CLASSIFIER_MAX_NAME_SZ];
45 struct rte_eth_ntuple_filter ntuple_filter;
47 /* classifier tables */
48 struct rte_cls_table tables[RTE_FLOW_CLASSIFY_TABLE_MAX];
53 struct rte_flow_classify_table_entry
54 *entries[RTE_PORT_IN_BURST_SIZE_MAX];
55 } __rte_cache_aligned;
67 struct rte_table_acl_rule_add_params key_add; /* add key */
68 struct rte_table_acl_rule_delete_params key_del; /* delete key */
71 struct classify_rules {
72 enum rte_flow_classify_rule_type type;
74 struct rte_flow_classify_ipv4_5tuple ipv4_5tuple;
78 struct rte_flow_classify_rule {
79 uint32_t id; /* unique ID of classify rule */
80 enum rte_flow_classify_table_type tbl_type; /* rule table */
81 struct classify_rules rules; /* union of rules */
85 int key_found; /* rule key found in table */
86 struct rte_flow_classify_table_entry entry; /* rule meta data */
87 void *entry_ptr; /* handle to the table entry for rule meta data */
91 rte_flow_classify_validate(
92 struct rte_flow_classifier *cls,
93 const struct rte_flow_attr *attr,
94 const struct rte_flow_item pattern[],
95 const struct rte_flow_action actions[],
96 struct rte_flow_error *error)
98 struct rte_flow_item *items;
99 parse_filter_t parse_filter;
100 uint32_t item_num = 0;
108 RTE_FLOW_CLASSIFY_LOG(ERR,
109 "%s: rte_flow_classifier parameter is NULL\n",
115 rte_flow_error_set(error, EINVAL,
116 RTE_FLOW_ERROR_TYPE_ATTR,
117 NULL, "NULL attribute.");
122 rte_flow_error_set(error,
123 EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
124 NULL, "NULL pattern.");
129 rte_flow_error_set(error, EINVAL,
130 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
131 NULL, "NULL action.");
135 memset(&cls->ntuple_filter, 0, sizeof(cls->ntuple_filter));
137 /* Get the non-void item number of pattern */
138 while ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_END) {
139 if ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_VOID)
145 items = malloc(item_num * sizeof(struct rte_flow_item));
147 rte_flow_error_set(error, ENOMEM,
148 RTE_FLOW_ERROR_TYPE_ITEM_NUM,
149 NULL, "No memory for pattern items.");
153 memset(items, 0, item_num * sizeof(struct rte_flow_item));
154 classify_pattern_skip_void_item(items, pattern);
156 parse_filter = classify_find_parse_filter_func(items);
158 rte_flow_error_set(error, EINVAL,
159 RTE_FLOW_ERROR_TYPE_ITEM,
160 pattern, "Unsupported pattern");
165 ret = parse_filter(attr, items, actions, &cls->ntuple_filter, error);
171 #define uint32_t_to_char(ip, a, b, c, d) do {\
172 *a = (unsigned char)(ip >> 24 & 0xff);\
173 *b = (unsigned char)(ip >> 16 & 0xff);\
174 *c = (unsigned char)(ip >> 8 & 0xff);\
175 *d = (unsigned char)(ip & 0xff);\
179 print_acl_ipv4_key_add(struct rte_table_acl_rule_add_params *key)
181 unsigned char a, b, c, d;
183 printf("%s: 0x%02hhx/0x%hhx ", __func__,
184 key->field_value[PROTO_FIELD_IPV4].value.u8,
185 key->field_value[PROTO_FIELD_IPV4].mask_range.u8);
187 uint32_t_to_char(key->field_value[SRC_FIELD_IPV4].value.u32,
189 printf(" %hhu.%hhu.%hhu.%hhu/0x%x ", a, b, c, d,
190 key->field_value[SRC_FIELD_IPV4].mask_range.u32);
192 uint32_t_to_char(key->field_value[DST_FIELD_IPV4].value.u32,
194 printf("%hhu.%hhu.%hhu.%hhu/0x%x ", a, b, c, d,
195 key->field_value[DST_FIELD_IPV4].mask_range.u32);
197 printf("%hu : 0x%x %hu : 0x%x",
198 key->field_value[SRCP_FIELD_IPV4].value.u16,
199 key->field_value[SRCP_FIELD_IPV4].mask_range.u16,
200 key->field_value[DSTP_FIELD_IPV4].value.u16,
201 key->field_value[DSTP_FIELD_IPV4].mask_range.u16);
203 printf(" priority: 0x%x\n", key->priority);
207 print_acl_ipv4_key_delete(struct rte_table_acl_rule_delete_params *key)
209 unsigned char a, b, c, d;
211 printf("%s: 0x%02hhx/0x%hhx ", __func__,
212 key->field_value[PROTO_FIELD_IPV4].value.u8,
213 key->field_value[PROTO_FIELD_IPV4].mask_range.u8);
215 uint32_t_to_char(key->field_value[SRC_FIELD_IPV4].value.u32,
217 printf(" %hhu.%hhu.%hhu.%hhu/0x%x ", a, b, c, d,
218 key->field_value[SRC_FIELD_IPV4].mask_range.u32);
220 uint32_t_to_char(key->field_value[DST_FIELD_IPV4].value.u32,
222 printf("%hhu.%hhu.%hhu.%hhu/0x%x ", a, b, c, d,
223 key->field_value[DST_FIELD_IPV4].mask_range.u32);
225 printf("%hu : 0x%x %hu : 0x%x\n",
226 key->field_value[SRCP_FIELD_IPV4].value.u16,
227 key->field_value[SRCP_FIELD_IPV4].mask_range.u16,
228 key->field_value[DSTP_FIELD_IPV4].value.u16,
229 key->field_value[DSTP_FIELD_IPV4].mask_range.u16);
233 rte_flow_classifier_check_params(struct rte_flow_classifier_params *params)
235 if (params == NULL) {
236 RTE_FLOW_CLASSIFY_LOG(ERR,
237 "%s: Incorrect value for parameter params\n", __func__);
242 if (params->name == NULL) {
243 RTE_FLOW_CLASSIFY_LOG(ERR,
244 "%s: Incorrect value for parameter name\n", __func__);
249 if (params->socket_id < 0) {
250 RTE_FLOW_CLASSIFY_LOG(ERR,
251 "%s: Incorrect value for parameter socket_id\n",
259 struct rte_flow_classifier *
260 rte_flow_classifier_create(struct rte_flow_classifier_params *params)
262 struct rte_flow_classifier *cls;
265 /* Check input parameters */
266 ret = rte_flow_classifier_check_params(params);
268 RTE_FLOW_CLASSIFY_LOG(ERR,
269 "%s: flow classifier params check failed (%d)\n",
274 /* Allocate memory for the flow classifier */
275 cls = rte_zmalloc_socket("FLOW_CLASSIFIER",
276 sizeof(struct rte_flow_classifier),
277 RTE_CACHE_LINE_SIZE, params->socket_id);
280 RTE_FLOW_CLASSIFY_LOG(ERR,
281 "%s: flow classifier memory allocation failed\n",
286 /* Save input parameters */
287 strlcpy(cls->name, params->name, RTE_FLOW_CLASSIFIER_MAX_NAME_SZ);
289 cls->socket_id = params->socket_id;
295 rte_flow_classify_table_free(struct rte_cls_table *table)
297 if (table->ops.f_free != NULL)
298 table->ops.f_free(table->h_table);
302 rte_flow_classifier_free(struct rte_flow_classifier *cls)
306 /* Check input parameters */
308 RTE_FLOW_CLASSIFY_LOG(ERR,
309 "%s: rte_flow_classifier parameter is NULL\n",
315 for (i = 0; i < cls->num_tables; i++) {
316 struct rte_cls_table *table = &cls->tables[i];
318 rte_flow_classify_table_free(table);
321 /* Free flow classifier memory */
328 rte_table_check_params(struct rte_flow_classifier *cls,
329 struct rte_flow_classify_table_params *params)
332 RTE_FLOW_CLASSIFY_LOG(ERR,
333 "%s: flow classifier parameter is NULL\n",
337 if (params == NULL) {
338 RTE_FLOW_CLASSIFY_LOG(ERR, "%s: params parameter is NULL\n",
344 if (params->ops == NULL) {
345 RTE_FLOW_CLASSIFY_LOG(ERR, "%s: params->ops is NULL\n",
350 if (params->ops->f_create == NULL) {
351 RTE_FLOW_CLASSIFY_LOG(ERR,
352 "%s: f_create function pointer is NULL\n", __func__);
356 if (params->ops->f_lookup == NULL) {
357 RTE_FLOW_CLASSIFY_LOG(ERR,
358 "%s: f_lookup function pointer is NULL\n", __func__);
362 /* De we have room for one more table? */
363 if (cls->num_tables == RTE_FLOW_CLASSIFY_TABLE_MAX) {
364 RTE_FLOW_CLASSIFY_LOG(ERR,
365 "%s: Incorrect value for num_tables parameter\n",
374 rte_flow_classify_table_create(struct rte_flow_classifier *cls,
375 struct rte_flow_classify_table_params *params)
377 struct rte_cls_table *table;
382 /* Check input arguments */
383 ret = rte_table_check_params(cls, params);
387 /* calculate table entry size */
388 entry_size = sizeof(struct rte_flow_classify_table_entry);
390 /* Create the table */
391 h_table = params->ops->f_create(params->arg_create, cls->socket_id,
393 if (h_table == NULL) {
394 RTE_FLOW_CLASSIFY_LOG(ERR, "%s: Table creation failed\n",
399 /* Commit current table to the classifier */
400 table = &cls->tables[cls->num_tables];
401 table->type = params->type;
404 /* Save input parameters */
405 memcpy(&table->ops, params->ops, sizeof(struct rte_table_ops));
407 /* Initialize table internal data structure */
408 table->entry_size = entry_size;
409 table->h_table = h_table;
414 static struct rte_flow_classify_rule *
415 allocate_acl_ipv4_5tuple_rule(struct rte_flow_classifier *cls)
417 struct rte_flow_classify_rule *rule;
419 rule = malloc(sizeof(struct rte_flow_classify_rule));
423 memset(rule, 0, sizeof(struct rte_flow_classify_rule));
424 rule->id = unique_id++;
425 rule->rules.type = RTE_FLOW_CLASSIFY_RULE_TYPE_IPV4_5TUPLE;
428 rule->u.key.key_add.priority = cls->ntuple_filter.priority;
429 rule->u.key.key_add.field_value[PROTO_FIELD_IPV4].mask_range.u8 =
430 cls->ntuple_filter.proto_mask;
431 rule->u.key.key_add.field_value[PROTO_FIELD_IPV4].value.u8 =
432 cls->ntuple_filter.proto;
433 rule->rules.u.ipv4_5tuple.proto = cls->ntuple_filter.proto;
434 rule->rules.u.ipv4_5tuple.proto_mask = cls->ntuple_filter.proto_mask;
436 rule->u.key.key_add.field_value[SRC_FIELD_IPV4].mask_range.u32 =
437 cls->ntuple_filter.src_ip_mask;
438 rule->u.key.key_add.field_value[SRC_FIELD_IPV4].value.u32 =
439 cls->ntuple_filter.src_ip;
440 rule->rules.u.ipv4_5tuple.src_ip_mask = cls->ntuple_filter.src_ip_mask;
441 rule->rules.u.ipv4_5tuple.src_ip = cls->ntuple_filter.src_ip;
443 rule->u.key.key_add.field_value[DST_FIELD_IPV4].mask_range.u32 =
444 cls->ntuple_filter.dst_ip_mask;
445 rule->u.key.key_add.field_value[DST_FIELD_IPV4].value.u32 =
446 cls->ntuple_filter.dst_ip;
447 rule->rules.u.ipv4_5tuple.dst_ip_mask = cls->ntuple_filter.dst_ip_mask;
448 rule->rules.u.ipv4_5tuple.dst_ip = cls->ntuple_filter.dst_ip;
450 rule->u.key.key_add.field_value[SRCP_FIELD_IPV4].mask_range.u16 =
451 cls->ntuple_filter.src_port_mask;
452 rule->u.key.key_add.field_value[SRCP_FIELD_IPV4].value.u16 =
453 cls->ntuple_filter.src_port;
454 rule->rules.u.ipv4_5tuple.src_port_mask =
455 cls->ntuple_filter.src_port_mask;
456 rule->rules.u.ipv4_5tuple.src_port = cls->ntuple_filter.src_port;
458 rule->u.key.key_add.field_value[DSTP_FIELD_IPV4].mask_range.u16 =
459 cls->ntuple_filter.dst_port_mask;
460 rule->u.key.key_add.field_value[DSTP_FIELD_IPV4].value.u16 =
461 cls->ntuple_filter.dst_port;
462 rule->rules.u.ipv4_5tuple.dst_port_mask =
463 cls->ntuple_filter.dst_port_mask;
464 rule->rules.u.ipv4_5tuple.dst_port = cls->ntuple_filter.dst_port;
466 if (rte_log_can_log(librte_flow_classify_logtype, RTE_LOG_DEBUG))
467 print_acl_ipv4_key_add(&rule->u.key.key_add);
469 /* key delete values */
470 memcpy(&rule->u.key.key_del.field_value[PROTO_FIELD_IPV4],
471 &rule->u.key.key_add.field_value[PROTO_FIELD_IPV4],
472 NUM_FIELDS_IPV4 * sizeof(struct rte_acl_field));
474 if (rte_log_can_log(librte_flow_classify_logtype, RTE_LOG_DEBUG))
475 print_acl_ipv4_key_delete(&rule->u.key.key_del);
480 struct rte_flow_classify_rule *
481 rte_flow_classify_table_entry_add(struct rte_flow_classifier *cls,
482 const struct rte_flow_attr *attr,
483 const struct rte_flow_item pattern[],
484 const struct rte_flow_action actions[],
486 struct rte_flow_error *error)
488 struct rte_flow_classify_rule *rule;
489 struct rte_flow_classify_table_entry *table_entry;
490 struct classify_action *action;
497 if (key_found == NULL) {
498 rte_flow_error_set(error, EINVAL,
499 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
500 NULL, "NULL key_found.");
504 /* parse attr, pattern and actions */
505 ret = rte_flow_classify_validate(cls, attr, pattern, actions, error);
509 switch (table_type) {
510 case RTE_FLOW_CLASSIFY_TABLE_ACL_IP4_5TUPLE:
511 rule = allocate_acl_ipv4_5tuple_rule(cls);
514 rule->tbl_type = table_type;
515 cls->table_mask |= table_type;
521 action = classify_get_flow_action();
522 table_entry = &rule->entry;
523 table_entry->rule_id = rule->id;
524 table_entry->action.action_mask = action->action_mask;
527 if (action->action_mask & (1LLU << RTE_FLOW_ACTION_TYPE_COUNT)) {
528 memcpy(&table_entry->action.act.counter, &action->act.counter,
529 sizeof(table_entry->action.act.counter));
531 if (action->action_mask & (1LLU << RTE_FLOW_ACTION_TYPE_MARK)) {
532 memcpy(&table_entry->action.act.mark, &action->act.mark,
533 sizeof(table_entry->action.act.mark));
536 for (i = 0; i < cls->num_tables; i++) {
537 struct rte_cls_table *table = &cls->tables[i];
539 if (table->type == table_type) {
540 if (table->ops.f_add != NULL) {
541 ret = table->ops.f_add(
543 &rule->u.key.key_add,
552 *key_found = rule->key_found;
563 rte_flow_classify_table_entry_delete(struct rte_flow_classifier *cls,
564 struct rte_flow_classify_rule *rule)
571 enum rte_flow_classify_table_type tbl_type = rule->tbl_type;
573 for (i = 0; i < cls->num_tables; i++) {
574 struct rte_cls_table *table = &cls->tables[i];
576 if (table->type == tbl_type) {
577 if (table->ops.f_delete != NULL) {
578 ret = table->ops.f_delete(table->h_table,
579 &rule->u.key.key_del,
592 flow_classifier_lookup(struct rte_flow_classifier *cls,
593 struct rte_cls_table *table,
594 struct rte_mbuf **pkts,
595 const uint16_t nb_pkts)
599 uint64_t lookup_hit_mask;
601 pkts_mask = RTE_LEN2MASK(nb_pkts, uint64_t);
602 ret = table->ops.f_lookup(table->h_table,
603 pkts, pkts_mask, &lookup_hit_mask,
604 (void **)cls->entries);
606 if (!ret && lookup_hit_mask)
607 cls->nb_pkts = nb_pkts;
615 action_apply(struct rte_flow_classifier *cls,
616 struct rte_flow_classify_rule *rule,
617 struct rte_flow_classify_stats *stats)
619 struct rte_flow_classify_ipv4_5tuple_stats *ntuple_stats;
620 struct rte_flow_classify_table_entry *entry = &rule->entry;
622 uint32_t action_mask = entry->action.action_mask;
623 int i, ret = -EINVAL;
625 if (action_mask & (1LLU << RTE_FLOW_ACTION_TYPE_COUNT)) {
626 for (i = 0; i < cls->nb_pkts; i++) {
627 if (rule->id == cls->entries[i]->rule_id)
632 ntuple_stats = stats->stats;
633 ntuple_stats->counter1 = count;
634 ntuple_stats->ipv4_5tuple = rule->rules.u.ipv4_5tuple;
641 rte_flow_classifier_query(struct rte_flow_classifier *cls,
642 struct rte_mbuf **pkts,
643 const uint16_t nb_pkts,
644 struct rte_flow_classify_rule *rule,
645 struct rte_flow_classify_stats *stats)
647 enum rte_flow_classify_table_type tbl_type;
651 if (!cls || !rule || !stats || !pkts || nb_pkts == 0)
654 tbl_type = rule->tbl_type;
655 for (i = 0; i < cls->num_tables; i++) {
656 struct rte_cls_table *table = &cls->tables[i];
658 if (table->type == tbl_type) {
659 ret = flow_classifier_lookup(cls, table,
662 ret = action_apply(cls, rule, stats);
670 RTE_LOG_REGISTER_DEFAULT(librte_flow_classify_logtype, INFO);