1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Intel Corporation
5 #include <rte_string_fns.h>
6 #include <rte_flow_classify.h>
7 #include "rte_flow_classify_parse.h"
8 #include <rte_table_acl.h>
10 static uint32_t unique_id = 1;
12 enum rte_flow_classify_table_type table_type
13 = RTE_FLOW_CLASSIFY_TABLE_TYPE_NONE;
15 struct rte_flow_classify_table_entry {
16 /* meta-data for classify rule */
20 struct classify_action action;
23 struct rte_cls_table {
24 /* Input parameters */
25 struct rte_table_ops ops;
27 enum rte_flow_classify_table_type type;
29 /* Handle to the low-level table object */
33 #define RTE_FLOW_CLASSIFIER_MAX_NAME_SZ 256
35 struct rte_flow_classifier {
36 /* Input parameters */
37 char name[RTE_FLOW_CLASSIFIER_MAX_NAME_SZ];
42 struct rte_eth_ntuple_filter ntuple_filter;
44 /* classifier tables */
45 struct rte_cls_table tables[RTE_FLOW_CLASSIFY_TABLE_MAX];
50 struct rte_flow_classify_table_entry
51 *entries[RTE_PORT_IN_BURST_SIZE_MAX];
52 } __rte_cache_aligned;
64 struct rte_table_acl_rule_add_params key_add; /* add key */
65 struct rte_table_acl_rule_delete_params key_del; /* delete key */
68 struct classify_rules {
69 enum rte_flow_classify_rule_type type;
71 struct rte_flow_classify_ipv4_5tuple ipv4_5tuple;
75 struct rte_flow_classify_rule {
76 uint32_t id; /* unique ID of classify rule */
77 enum rte_flow_classify_table_type tbl_type; /* rule table */
78 struct classify_rules rules; /* union of rules */
82 int key_found; /* rule key found in table */
83 struct rte_flow_classify_table_entry entry; /* rule meta data */
84 void *entry_ptr; /* handle to the table entry for rule meta data */
88 rte_flow_classify_validate(
89 struct rte_flow_classifier *cls,
90 const struct rte_flow_attr *attr,
91 const struct rte_flow_item pattern[],
92 const struct rte_flow_action actions[],
93 struct rte_flow_error *error)
95 struct rte_flow_item *items;
96 parse_filter_t parse_filter;
97 uint32_t item_num = 0;
105 RTE_FLOW_CLASSIFY_LOG(ERR,
106 "%s: rte_flow_classifier parameter is NULL\n",
112 rte_flow_error_set(error, EINVAL,
113 RTE_FLOW_ERROR_TYPE_ATTR,
114 NULL, "NULL attribute.");
119 rte_flow_error_set(error,
120 EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
121 NULL, "NULL pattern.");
126 rte_flow_error_set(error, EINVAL,
127 RTE_FLOW_ERROR_TYPE_ACTION_NUM,
128 NULL, "NULL action.");
132 memset(&cls->ntuple_filter, 0, sizeof(cls->ntuple_filter));
134 /* Get the non-void item number of pattern */
135 while ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_END) {
136 if ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_VOID)
142 items = malloc(item_num * sizeof(struct rte_flow_item));
144 rte_flow_error_set(error, ENOMEM,
145 RTE_FLOW_ERROR_TYPE_ITEM_NUM,
146 NULL, "No memory for pattern items.");
150 memset(items, 0, item_num * sizeof(struct rte_flow_item));
151 classify_pattern_skip_void_item(items, pattern);
153 parse_filter = classify_find_parse_filter_func(items);
155 rte_flow_error_set(error, EINVAL,
156 RTE_FLOW_ERROR_TYPE_ITEM,
157 pattern, "Unsupported pattern");
162 ret = parse_filter(attr, items, actions, &cls->ntuple_filter, error);
168 #define uint32_t_to_char(ip, a, b, c, d) do {\
169 *a = (unsigned char)(ip >> 24 & 0xff);\
170 *b = (unsigned char)(ip >> 16 & 0xff);\
171 *c = (unsigned char)(ip >> 8 & 0xff);\
172 *d = (unsigned char)(ip & 0xff);\
176 print_acl_ipv4_key_add(struct rte_table_acl_rule_add_params *key)
178 unsigned char a, b, c, d;
180 printf("%s: 0x%02hhx/0x%hhx ", __func__,
181 key->field_value[PROTO_FIELD_IPV4].value.u8,
182 key->field_value[PROTO_FIELD_IPV4].mask_range.u8);
184 uint32_t_to_char(key->field_value[SRC_FIELD_IPV4].value.u32,
186 printf(" %hhu.%hhu.%hhu.%hhu/0x%x ", a, b, c, d,
187 key->field_value[SRC_FIELD_IPV4].mask_range.u32);
189 uint32_t_to_char(key->field_value[DST_FIELD_IPV4].value.u32,
191 printf("%hhu.%hhu.%hhu.%hhu/0x%x ", a, b, c, d,
192 key->field_value[DST_FIELD_IPV4].mask_range.u32);
194 printf("%hu : 0x%x %hu : 0x%x",
195 key->field_value[SRCP_FIELD_IPV4].value.u16,
196 key->field_value[SRCP_FIELD_IPV4].mask_range.u16,
197 key->field_value[DSTP_FIELD_IPV4].value.u16,
198 key->field_value[DSTP_FIELD_IPV4].mask_range.u16);
200 printf(" priority: 0x%x\n", key->priority);
204 print_acl_ipv4_key_delete(struct rte_table_acl_rule_delete_params *key)
206 unsigned char a, b, c, d;
208 printf("%s: 0x%02hhx/0x%hhx ", __func__,
209 key->field_value[PROTO_FIELD_IPV4].value.u8,
210 key->field_value[PROTO_FIELD_IPV4].mask_range.u8);
212 uint32_t_to_char(key->field_value[SRC_FIELD_IPV4].value.u32,
214 printf(" %hhu.%hhu.%hhu.%hhu/0x%x ", a, b, c, d,
215 key->field_value[SRC_FIELD_IPV4].mask_range.u32);
217 uint32_t_to_char(key->field_value[DST_FIELD_IPV4].value.u32,
219 printf("%hhu.%hhu.%hhu.%hhu/0x%x ", a, b, c, d,
220 key->field_value[DST_FIELD_IPV4].mask_range.u32);
222 printf("%hu : 0x%x %hu : 0x%x\n",
223 key->field_value[SRCP_FIELD_IPV4].value.u16,
224 key->field_value[SRCP_FIELD_IPV4].mask_range.u16,
225 key->field_value[DSTP_FIELD_IPV4].value.u16,
226 key->field_value[DSTP_FIELD_IPV4].mask_range.u16);
230 rte_flow_classifier_check_params(struct rte_flow_classifier_params *params)
232 if (params == NULL) {
233 RTE_FLOW_CLASSIFY_LOG(ERR,
234 "%s: Incorrect value for parameter params\n", __func__);
239 if (params->name == NULL) {
240 RTE_FLOW_CLASSIFY_LOG(ERR,
241 "%s: Incorrect value for parameter name\n", __func__);
246 if (params->socket_id < 0) {
247 RTE_FLOW_CLASSIFY_LOG(ERR,
248 "%s: Incorrect value for parameter socket_id\n",
256 struct rte_flow_classifier *
257 rte_flow_classifier_create(struct rte_flow_classifier_params *params)
259 struct rte_flow_classifier *cls;
262 /* Check input parameters */
263 ret = rte_flow_classifier_check_params(params);
265 RTE_FLOW_CLASSIFY_LOG(ERR,
266 "%s: flow classifier params check failed (%d)\n",
271 /* Allocate memory for the flow classifier */
272 cls = rte_zmalloc_socket("FLOW_CLASSIFIER",
273 sizeof(struct rte_flow_classifier),
274 RTE_CACHE_LINE_SIZE, params->socket_id);
277 RTE_FLOW_CLASSIFY_LOG(ERR,
278 "%s: flow classifier memory allocation failed\n",
283 /* Save input parameters */
284 strlcpy(cls->name, params->name, RTE_FLOW_CLASSIFIER_MAX_NAME_SZ);
286 cls->socket_id = params->socket_id;
292 rte_flow_classify_table_free(struct rte_cls_table *table)
294 if (table->ops.f_free != NULL)
295 table->ops.f_free(table->h_table);
299 rte_flow_classifier_free(struct rte_flow_classifier *cls)
303 /* Check input parameters */
305 RTE_FLOW_CLASSIFY_LOG(ERR,
306 "%s: rte_flow_classifier parameter is NULL\n",
312 for (i = 0; i < cls->num_tables; i++) {
313 struct rte_cls_table *table = &cls->tables[i];
315 rte_flow_classify_table_free(table);
318 /* Free flow classifier memory */
325 rte_table_check_params(struct rte_flow_classifier *cls,
326 struct rte_flow_classify_table_params *params)
329 RTE_FLOW_CLASSIFY_LOG(ERR,
330 "%s: flow classifier parameter is NULL\n",
334 if (params == NULL) {
335 RTE_FLOW_CLASSIFY_LOG(ERR, "%s: params parameter is NULL\n",
341 if (params->ops == NULL) {
342 RTE_FLOW_CLASSIFY_LOG(ERR, "%s: params->ops is NULL\n",
347 if (params->ops->f_create == NULL) {
348 RTE_FLOW_CLASSIFY_LOG(ERR,
349 "%s: f_create function pointer is NULL\n", __func__);
353 if (params->ops->f_lookup == NULL) {
354 RTE_FLOW_CLASSIFY_LOG(ERR,
355 "%s: f_lookup function pointer is NULL\n", __func__);
359 /* De we have room for one more table? */
360 if (cls->num_tables == RTE_FLOW_CLASSIFY_TABLE_MAX) {
361 RTE_FLOW_CLASSIFY_LOG(ERR,
362 "%s: Incorrect value for num_tables parameter\n",
371 rte_flow_classify_table_create(struct rte_flow_classifier *cls,
372 struct rte_flow_classify_table_params *params)
374 struct rte_cls_table *table;
379 /* Check input arguments */
380 ret = rte_table_check_params(cls, params);
384 /* calculate table entry size */
385 entry_size = sizeof(struct rte_flow_classify_table_entry);
387 /* Create the table */
388 h_table = params->ops->f_create(params->arg_create, cls->socket_id,
390 if (h_table == NULL) {
391 RTE_FLOW_CLASSIFY_LOG(ERR, "%s: Table creation failed\n",
396 /* Commit current table to the classifier */
397 table = &cls->tables[cls->num_tables];
398 table->type = params->type;
401 /* Save input parameters */
402 memcpy(&table->ops, params->ops, sizeof(struct rte_table_ops));
404 /* Initialize table internal data structure */
405 table->entry_size = entry_size;
406 table->h_table = h_table;
411 static struct rte_flow_classify_rule *
412 allocate_acl_ipv4_5tuple_rule(struct rte_flow_classifier *cls)
414 struct rte_flow_classify_rule *rule;
416 rule = malloc(sizeof(struct rte_flow_classify_rule));
420 memset(rule, 0, sizeof(struct rte_flow_classify_rule));
421 rule->id = unique_id++;
422 rule->rules.type = RTE_FLOW_CLASSIFY_RULE_TYPE_IPV4_5TUPLE;
425 rule->u.key.key_add.priority = cls->ntuple_filter.priority;
426 rule->u.key.key_add.field_value[PROTO_FIELD_IPV4].mask_range.u8 =
427 cls->ntuple_filter.proto_mask;
428 rule->u.key.key_add.field_value[PROTO_FIELD_IPV4].value.u8 =
429 cls->ntuple_filter.proto;
430 rule->rules.u.ipv4_5tuple.proto = cls->ntuple_filter.proto;
431 rule->rules.u.ipv4_5tuple.proto_mask = cls->ntuple_filter.proto_mask;
433 rule->u.key.key_add.field_value[SRC_FIELD_IPV4].mask_range.u32 =
434 cls->ntuple_filter.src_ip_mask;
435 rule->u.key.key_add.field_value[SRC_FIELD_IPV4].value.u32 =
436 cls->ntuple_filter.src_ip;
437 rule->rules.u.ipv4_5tuple.src_ip_mask = cls->ntuple_filter.src_ip_mask;
438 rule->rules.u.ipv4_5tuple.src_ip = cls->ntuple_filter.src_ip;
440 rule->u.key.key_add.field_value[DST_FIELD_IPV4].mask_range.u32 =
441 cls->ntuple_filter.dst_ip_mask;
442 rule->u.key.key_add.field_value[DST_FIELD_IPV4].value.u32 =
443 cls->ntuple_filter.dst_ip;
444 rule->rules.u.ipv4_5tuple.dst_ip_mask = cls->ntuple_filter.dst_ip_mask;
445 rule->rules.u.ipv4_5tuple.dst_ip = cls->ntuple_filter.dst_ip;
447 rule->u.key.key_add.field_value[SRCP_FIELD_IPV4].mask_range.u16 =
448 cls->ntuple_filter.src_port_mask;
449 rule->u.key.key_add.field_value[SRCP_FIELD_IPV4].value.u16 =
450 cls->ntuple_filter.src_port;
451 rule->rules.u.ipv4_5tuple.src_port_mask =
452 cls->ntuple_filter.src_port_mask;
453 rule->rules.u.ipv4_5tuple.src_port = cls->ntuple_filter.src_port;
455 rule->u.key.key_add.field_value[DSTP_FIELD_IPV4].mask_range.u16 =
456 cls->ntuple_filter.dst_port_mask;
457 rule->u.key.key_add.field_value[DSTP_FIELD_IPV4].value.u16 =
458 cls->ntuple_filter.dst_port;
459 rule->rules.u.ipv4_5tuple.dst_port_mask =
460 cls->ntuple_filter.dst_port_mask;
461 rule->rules.u.ipv4_5tuple.dst_port = cls->ntuple_filter.dst_port;
463 if (rte_log_can_log(librte_flow_classify_logtype, RTE_LOG_DEBUG))
464 print_acl_ipv4_key_add(&rule->u.key.key_add);
466 /* key delete values */
467 memcpy(&rule->u.key.key_del.field_value[PROTO_FIELD_IPV4],
468 &rule->u.key.key_add.field_value[PROTO_FIELD_IPV4],
469 NUM_FIELDS_IPV4 * sizeof(struct rte_acl_field));
471 if (rte_log_can_log(librte_flow_classify_logtype, RTE_LOG_DEBUG))
472 print_acl_ipv4_key_delete(&rule->u.key.key_del);
477 struct rte_flow_classify_rule *
478 rte_flow_classify_table_entry_add(struct rte_flow_classifier *cls,
479 const struct rte_flow_attr *attr,
480 const struct rte_flow_item pattern[],
481 const struct rte_flow_action actions[],
483 struct rte_flow_error *error)
485 struct rte_flow_classify_rule *rule;
486 struct rte_flow_classify_table_entry *table_entry;
487 struct classify_action *action;
494 if (key_found == NULL) {
495 rte_flow_error_set(error, EINVAL,
496 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
497 NULL, "NULL key_found.");
501 /* parse attr, pattern and actions */
502 ret = rte_flow_classify_validate(cls, attr, pattern, actions, error);
506 switch (table_type) {
507 case RTE_FLOW_CLASSIFY_TABLE_ACL_IP4_5TUPLE:
508 rule = allocate_acl_ipv4_5tuple_rule(cls);
511 rule->tbl_type = table_type;
512 cls->table_mask |= table_type;
518 action = classify_get_flow_action();
519 table_entry = &rule->entry;
520 table_entry->rule_id = rule->id;
521 table_entry->action.action_mask = action->action_mask;
524 if (action->action_mask & (1LLU << RTE_FLOW_ACTION_TYPE_COUNT)) {
525 memcpy(&table_entry->action.act.counter, &action->act.counter,
526 sizeof(table_entry->action.act.counter));
528 if (action->action_mask & (1LLU << RTE_FLOW_ACTION_TYPE_MARK)) {
529 memcpy(&table_entry->action.act.mark, &action->act.mark,
530 sizeof(table_entry->action.act.mark));
533 for (i = 0; i < cls->num_tables; i++) {
534 struct rte_cls_table *table = &cls->tables[i];
536 if (table->type == table_type) {
537 if (table->ops.f_add != NULL) {
538 ret = table->ops.f_add(
540 &rule->u.key.key_add,
549 *key_found = rule->key_found;
560 rte_flow_classify_table_entry_delete(struct rte_flow_classifier *cls,
561 struct rte_flow_classify_rule *rule)
568 enum rte_flow_classify_table_type tbl_type = rule->tbl_type;
570 for (i = 0; i < cls->num_tables; i++) {
571 struct rte_cls_table *table = &cls->tables[i];
573 if (table->type == tbl_type) {
574 if (table->ops.f_delete != NULL) {
575 ret = table->ops.f_delete(table->h_table,
576 &rule->u.key.key_del,
589 flow_classifier_lookup(struct rte_flow_classifier *cls,
590 struct rte_cls_table *table,
591 struct rte_mbuf **pkts,
592 const uint16_t nb_pkts)
596 uint64_t lookup_hit_mask;
598 pkts_mask = RTE_LEN2MASK(nb_pkts, uint64_t);
599 ret = table->ops.f_lookup(table->h_table,
600 pkts, pkts_mask, &lookup_hit_mask,
601 (void **)cls->entries);
603 if (!ret && lookup_hit_mask)
604 cls->nb_pkts = nb_pkts;
612 action_apply(struct rte_flow_classifier *cls,
613 struct rte_flow_classify_rule *rule,
614 struct rte_flow_classify_stats *stats)
616 struct rte_flow_classify_ipv4_5tuple_stats *ntuple_stats;
617 struct rte_flow_classify_table_entry *entry = &rule->entry;
619 uint32_t action_mask = entry->action.action_mask;
620 int i, ret = -EINVAL;
622 if (action_mask & (1LLU << RTE_FLOW_ACTION_TYPE_COUNT)) {
623 for (i = 0; i < cls->nb_pkts; i++) {
624 if (rule->id == cls->entries[i]->rule_id)
629 ntuple_stats = stats->stats;
630 ntuple_stats->counter1 = count;
631 ntuple_stats->ipv4_5tuple = rule->rules.u.ipv4_5tuple;
638 rte_flow_classifier_query(struct rte_flow_classifier *cls,
639 struct rte_mbuf **pkts,
640 const uint16_t nb_pkts,
641 struct rte_flow_classify_rule *rule,
642 struct rte_flow_classify_stats *stats)
644 enum rte_flow_classify_table_type tbl_type;
648 if (!cls || !rule || !stats || !pkts || nb_pkts == 0)
651 tbl_type = rule->tbl_type;
652 for (i = 0; i < cls->num_tables; i++) {
653 struct rte_cls_table *table = &cls->tables[i];
655 if (table->type == tbl_type) {
656 ret = flow_classifier_lookup(cls, table,
659 ret = action_apply(cls, rule, stats);
667 RTE_LOG_REGISTER_DEFAULT(librte_flow_classify_logtype, INFO);