199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson * Copyright(c) 2017 Intel Corporation
399a2dd95SBruce Richardson */
499a2dd95SBruce Richardson
599a2dd95SBruce Richardson #include <rte_string_fns.h>
699a2dd95SBruce Richardson #include <rte_flow_classify.h>
799a2dd95SBruce Richardson #include "rte_flow_classify_parse.h"
899a2dd95SBruce Richardson #include <rte_table_acl.h>
999a2dd95SBruce Richardson
1099a2dd95SBruce Richardson static uint32_t unique_id = 1;
1199a2dd95SBruce Richardson
1299a2dd95SBruce Richardson enum rte_flow_classify_table_type table_type
1399a2dd95SBruce Richardson = RTE_FLOW_CLASSIFY_TABLE_TYPE_NONE;
1499a2dd95SBruce Richardson
1599a2dd95SBruce Richardson struct rte_flow_classify_table_entry {
1699a2dd95SBruce Richardson /* meta-data for classify rule */
1799a2dd95SBruce Richardson uint32_t rule_id;
1899a2dd95SBruce Richardson
1999a2dd95SBruce Richardson /* Flow action */
2099a2dd95SBruce Richardson struct classify_action action;
2199a2dd95SBruce Richardson };
2299a2dd95SBruce Richardson
2399a2dd95SBruce Richardson struct rte_cls_table {
2499a2dd95SBruce Richardson /* Input parameters */
2599a2dd95SBruce Richardson struct rte_table_ops ops;
2699a2dd95SBruce Richardson uint32_t entry_size;
2799a2dd95SBruce Richardson enum rte_flow_classify_table_type type;
2899a2dd95SBruce Richardson
2999a2dd95SBruce Richardson /* Handle to the low-level table object */
3099a2dd95SBruce Richardson void *h_table;
3199a2dd95SBruce Richardson };
3299a2dd95SBruce Richardson
3399a2dd95SBruce Richardson #define RTE_FLOW_CLASSIFIER_MAX_NAME_SZ 256
3499a2dd95SBruce Richardson
3599a2dd95SBruce Richardson struct rte_flow_classifier {
3699a2dd95SBruce Richardson /* Input parameters */
3799a2dd95SBruce Richardson char name[RTE_FLOW_CLASSIFIER_MAX_NAME_SZ];
3899a2dd95SBruce Richardson int socket_id;
3999a2dd95SBruce Richardson
4099a2dd95SBruce Richardson /* Internal */
4199a2dd95SBruce Richardson /* ntuple_filter */
4299a2dd95SBruce Richardson struct rte_eth_ntuple_filter ntuple_filter;
4399a2dd95SBruce Richardson
4499a2dd95SBruce Richardson /* classifier tables */
4599a2dd95SBruce Richardson struct rte_cls_table tables[RTE_FLOW_CLASSIFY_TABLE_MAX];
4699a2dd95SBruce Richardson uint32_t table_mask;
4799a2dd95SBruce Richardson uint32_t num_tables;
4899a2dd95SBruce Richardson
4999a2dd95SBruce Richardson uint16_t nb_pkts;
5099a2dd95SBruce Richardson struct rte_flow_classify_table_entry
5199a2dd95SBruce Richardson *entries[RTE_PORT_IN_BURST_SIZE_MAX];
5299a2dd95SBruce Richardson } __rte_cache_aligned;
5399a2dd95SBruce Richardson
5499a2dd95SBruce Richardson enum {
5599a2dd95SBruce Richardson PROTO_FIELD_IPV4,
5699a2dd95SBruce Richardson SRC_FIELD_IPV4,
5799a2dd95SBruce Richardson DST_FIELD_IPV4,
5899a2dd95SBruce Richardson SRCP_FIELD_IPV4,
5999a2dd95SBruce Richardson DSTP_FIELD_IPV4,
6099a2dd95SBruce Richardson NUM_FIELDS_IPV4
6199a2dd95SBruce Richardson };
6299a2dd95SBruce Richardson
6399a2dd95SBruce Richardson struct acl_keys {
6499a2dd95SBruce Richardson struct rte_table_acl_rule_add_params key_add; /* add key */
6599a2dd95SBruce Richardson struct rte_table_acl_rule_delete_params key_del; /* delete key */
6699a2dd95SBruce Richardson };
6799a2dd95SBruce Richardson
6899a2dd95SBruce Richardson struct classify_rules {
6999a2dd95SBruce Richardson enum rte_flow_classify_rule_type type;
7099a2dd95SBruce Richardson union {
7199a2dd95SBruce Richardson struct rte_flow_classify_ipv4_5tuple ipv4_5tuple;
7299a2dd95SBruce Richardson } u;
7399a2dd95SBruce Richardson };
7499a2dd95SBruce Richardson
7599a2dd95SBruce Richardson struct rte_flow_classify_rule {
7699a2dd95SBruce Richardson uint32_t id; /* unique ID of classify rule */
7799a2dd95SBruce Richardson enum rte_flow_classify_table_type tbl_type; /* rule table */
7899a2dd95SBruce Richardson struct classify_rules rules; /* union of rules */
7999a2dd95SBruce Richardson union {
8099a2dd95SBruce Richardson struct acl_keys key;
8199a2dd95SBruce Richardson } u;
8299a2dd95SBruce Richardson int key_found; /* rule key found in table */
8399a2dd95SBruce Richardson struct rte_flow_classify_table_entry entry; /* rule meta data */
8499a2dd95SBruce Richardson void *entry_ptr; /* handle to the table entry for rule meta data */
8599a2dd95SBruce Richardson };
8699a2dd95SBruce Richardson
8799a2dd95SBruce Richardson int
rte_flow_classify_validate(struct rte_flow_classifier * cls,const struct rte_flow_attr * attr,const struct rte_flow_item pattern[],const struct rte_flow_action actions[],struct rte_flow_error * error)8899a2dd95SBruce Richardson rte_flow_classify_validate(
8999a2dd95SBruce Richardson struct rte_flow_classifier *cls,
9099a2dd95SBruce Richardson const struct rte_flow_attr *attr,
9199a2dd95SBruce Richardson const struct rte_flow_item pattern[],
9299a2dd95SBruce Richardson const struct rte_flow_action actions[],
9399a2dd95SBruce Richardson struct rte_flow_error *error)
9499a2dd95SBruce Richardson {
9599a2dd95SBruce Richardson struct rte_flow_item *items;
9699a2dd95SBruce Richardson parse_filter_t parse_filter;
9799a2dd95SBruce Richardson uint32_t item_num = 0;
9899a2dd95SBruce Richardson uint32_t i = 0;
9999a2dd95SBruce Richardson int ret;
10099a2dd95SBruce Richardson
10199a2dd95SBruce Richardson if (error == NULL)
10299a2dd95SBruce Richardson return -EINVAL;
10399a2dd95SBruce Richardson
10499a2dd95SBruce Richardson if (cls == NULL) {
10599a2dd95SBruce Richardson RTE_FLOW_CLASSIFY_LOG(ERR,
10699a2dd95SBruce Richardson "%s: rte_flow_classifier parameter is NULL\n",
10799a2dd95SBruce Richardson __func__);
10899a2dd95SBruce Richardson return -EINVAL;
10999a2dd95SBruce Richardson }
11099a2dd95SBruce Richardson
11199a2dd95SBruce Richardson if (!attr) {
11299a2dd95SBruce Richardson rte_flow_error_set(error, EINVAL,
11399a2dd95SBruce Richardson RTE_FLOW_ERROR_TYPE_ATTR,
11499a2dd95SBruce Richardson NULL, "NULL attribute.");
11599a2dd95SBruce Richardson return -EINVAL;
11699a2dd95SBruce Richardson }
11799a2dd95SBruce Richardson
11899a2dd95SBruce Richardson if (!pattern) {
11999a2dd95SBruce Richardson rte_flow_error_set(error,
12099a2dd95SBruce Richardson EINVAL, RTE_FLOW_ERROR_TYPE_ITEM_NUM,
12199a2dd95SBruce Richardson NULL, "NULL pattern.");
12299a2dd95SBruce Richardson return -EINVAL;
12399a2dd95SBruce Richardson }
12499a2dd95SBruce Richardson
12599a2dd95SBruce Richardson if (!actions) {
12699a2dd95SBruce Richardson rte_flow_error_set(error, EINVAL,
12799a2dd95SBruce Richardson RTE_FLOW_ERROR_TYPE_ACTION_NUM,
12899a2dd95SBruce Richardson NULL, "NULL action.");
12999a2dd95SBruce Richardson return -EINVAL;
13099a2dd95SBruce Richardson }
13199a2dd95SBruce Richardson
13299a2dd95SBruce Richardson memset(&cls->ntuple_filter, 0, sizeof(cls->ntuple_filter));
13399a2dd95SBruce Richardson
13499a2dd95SBruce Richardson /* Get the non-void item number of pattern */
13599a2dd95SBruce Richardson while ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_END) {
13699a2dd95SBruce Richardson if ((pattern + i)->type != RTE_FLOW_ITEM_TYPE_VOID)
13799a2dd95SBruce Richardson item_num++;
13899a2dd95SBruce Richardson i++;
13999a2dd95SBruce Richardson }
14099a2dd95SBruce Richardson item_num++;
14199a2dd95SBruce Richardson
14299a2dd95SBruce Richardson items = malloc(item_num * sizeof(struct rte_flow_item));
14399a2dd95SBruce Richardson if (!items) {
14499a2dd95SBruce Richardson rte_flow_error_set(error, ENOMEM,
14599a2dd95SBruce Richardson RTE_FLOW_ERROR_TYPE_ITEM_NUM,
14699a2dd95SBruce Richardson NULL, "No memory for pattern items.");
14799a2dd95SBruce Richardson return -ENOMEM;
14899a2dd95SBruce Richardson }
14999a2dd95SBruce Richardson
15099a2dd95SBruce Richardson memset(items, 0, item_num * sizeof(struct rte_flow_item));
15199a2dd95SBruce Richardson classify_pattern_skip_void_item(items, pattern);
15299a2dd95SBruce Richardson
15399a2dd95SBruce Richardson parse_filter = classify_find_parse_filter_func(items);
15499a2dd95SBruce Richardson if (!parse_filter) {
15599a2dd95SBruce Richardson rte_flow_error_set(error, EINVAL,
15699a2dd95SBruce Richardson RTE_FLOW_ERROR_TYPE_ITEM,
15799a2dd95SBruce Richardson pattern, "Unsupported pattern");
15899a2dd95SBruce Richardson free(items);
15999a2dd95SBruce Richardson return -EINVAL;
16099a2dd95SBruce Richardson }
16199a2dd95SBruce Richardson
16299a2dd95SBruce Richardson ret = parse_filter(attr, items, actions, &cls->ntuple_filter, error);
16399a2dd95SBruce Richardson free(items);
16499a2dd95SBruce Richardson return ret;
16599a2dd95SBruce Richardson }
16699a2dd95SBruce Richardson
16799a2dd95SBruce Richardson
16899a2dd95SBruce Richardson #define uint32_t_to_char(ip, a, b, c, d) do {\
16999a2dd95SBruce Richardson *a = (unsigned char)(ip >> 24 & 0xff);\
17099a2dd95SBruce Richardson *b = (unsigned char)(ip >> 16 & 0xff);\
17199a2dd95SBruce Richardson *c = (unsigned char)(ip >> 8 & 0xff);\
17299a2dd95SBruce Richardson *d = (unsigned char)(ip & 0xff);\
17399a2dd95SBruce Richardson } while (0)
17499a2dd95SBruce Richardson
17599a2dd95SBruce Richardson static inline void
print_acl_ipv4_key_add(struct rte_table_acl_rule_add_params * key)17699a2dd95SBruce Richardson print_acl_ipv4_key_add(struct rte_table_acl_rule_add_params *key)
17799a2dd95SBruce Richardson {
17899a2dd95SBruce Richardson unsigned char a, b, c, d;
17999a2dd95SBruce Richardson
18099a2dd95SBruce Richardson printf("%s: 0x%02hhx/0x%hhx ", __func__,
18199a2dd95SBruce Richardson key->field_value[PROTO_FIELD_IPV4].value.u8,
18299a2dd95SBruce Richardson key->field_value[PROTO_FIELD_IPV4].mask_range.u8);
18399a2dd95SBruce Richardson
18499a2dd95SBruce Richardson uint32_t_to_char(key->field_value[SRC_FIELD_IPV4].value.u32,
18599a2dd95SBruce Richardson &a, &b, &c, &d);
18699a2dd95SBruce Richardson printf(" %hhu.%hhu.%hhu.%hhu/0x%x ", a, b, c, d,
18799a2dd95SBruce Richardson key->field_value[SRC_FIELD_IPV4].mask_range.u32);
18899a2dd95SBruce Richardson
18999a2dd95SBruce Richardson uint32_t_to_char(key->field_value[DST_FIELD_IPV4].value.u32,
19099a2dd95SBruce Richardson &a, &b, &c, &d);
19199a2dd95SBruce Richardson printf("%hhu.%hhu.%hhu.%hhu/0x%x ", a, b, c, d,
19299a2dd95SBruce Richardson key->field_value[DST_FIELD_IPV4].mask_range.u32);
19399a2dd95SBruce Richardson
19499a2dd95SBruce Richardson printf("%hu : 0x%x %hu : 0x%x",
19599a2dd95SBruce Richardson key->field_value[SRCP_FIELD_IPV4].value.u16,
19699a2dd95SBruce Richardson key->field_value[SRCP_FIELD_IPV4].mask_range.u16,
19799a2dd95SBruce Richardson key->field_value[DSTP_FIELD_IPV4].value.u16,
19899a2dd95SBruce Richardson key->field_value[DSTP_FIELD_IPV4].mask_range.u16);
19999a2dd95SBruce Richardson
20099a2dd95SBruce Richardson printf(" priority: 0x%x\n", key->priority);
20199a2dd95SBruce Richardson }
20299a2dd95SBruce Richardson
20399a2dd95SBruce Richardson static inline void
print_acl_ipv4_key_delete(struct rte_table_acl_rule_delete_params * key)20499a2dd95SBruce Richardson print_acl_ipv4_key_delete(struct rte_table_acl_rule_delete_params *key)
20599a2dd95SBruce Richardson {
20699a2dd95SBruce Richardson unsigned char a, b, c, d;
20799a2dd95SBruce Richardson
20899a2dd95SBruce Richardson printf("%s: 0x%02hhx/0x%hhx ", __func__,
20999a2dd95SBruce Richardson key->field_value[PROTO_FIELD_IPV4].value.u8,
21099a2dd95SBruce Richardson key->field_value[PROTO_FIELD_IPV4].mask_range.u8);
21199a2dd95SBruce Richardson
21299a2dd95SBruce Richardson uint32_t_to_char(key->field_value[SRC_FIELD_IPV4].value.u32,
21399a2dd95SBruce Richardson &a, &b, &c, &d);
21499a2dd95SBruce Richardson printf(" %hhu.%hhu.%hhu.%hhu/0x%x ", a, b, c, d,
21599a2dd95SBruce Richardson key->field_value[SRC_FIELD_IPV4].mask_range.u32);
21699a2dd95SBruce Richardson
21799a2dd95SBruce Richardson uint32_t_to_char(key->field_value[DST_FIELD_IPV4].value.u32,
21899a2dd95SBruce Richardson &a, &b, &c, &d);
21999a2dd95SBruce Richardson printf("%hhu.%hhu.%hhu.%hhu/0x%x ", a, b, c, d,
22099a2dd95SBruce Richardson key->field_value[DST_FIELD_IPV4].mask_range.u32);
22199a2dd95SBruce Richardson
22299a2dd95SBruce Richardson printf("%hu : 0x%x %hu : 0x%x\n",
22399a2dd95SBruce Richardson key->field_value[SRCP_FIELD_IPV4].value.u16,
22499a2dd95SBruce Richardson key->field_value[SRCP_FIELD_IPV4].mask_range.u16,
22599a2dd95SBruce Richardson key->field_value[DSTP_FIELD_IPV4].value.u16,
22699a2dd95SBruce Richardson key->field_value[DSTP_FIELD_IPV4].mask_range.u16);
22799a2dd95SBruce Richardson }
22899a2dd95SBruce Richardson
22999a2dd95SBruce Richardson static int
rte_flow_classifier_check_params(struct rte_flow_classifier_params * params)23099a2dd95SBruce Richardson rte_flow_classifier_check_params(struct rte_flow_classifier_params *params)
23199a2dd95SBruce Richardson {
23299a2dd95SBruce Richardson if (params == NULL) {
23399a2dd95SBruce Richardson RTE_FLOW_CLASSIFY_LOG(ERR,
23499a2dd95SBruce Richardson "%s: Incorrect value for parameter params\n", __func__);
23599a2dd95SBruce Richardson return -EINVAL;
23699a2dd95SBruce Richardson }
23799a2dd95SBruce Richardson
23899a2dd95SBruce Richardson /* name */
23999a2dd95SBruce Richardson if (params->name == NULL) {
24099a2dd95SBruce Richardson RTE_FLOW_CLASSIFY_LOG(ERR,
24199a2dd95SBruce Richardson "%s: Incorrect value for parameter name\n", __func__);
24299a2dd95SBruce Richardson return -EINVAL;
24399a2dd95SBruce Richardson }
24499a2dd95SBruce Richardson
24599a2dd95SBruce Richardson /* socket */
24699a2dd95SBruce Richardson if (params->socket_id < 0) {
24799a2dd95SBruce Richardson RTE_FLOW_CLASSIFY_LOG(ERR,
24899a2dd95SBruce Richardson "%s: Incorrect value for parameter socket_id\n",
24999a2dd95SBruce Richardson __func__);
25099a2dd95SBruce Richardson return -EINVAL;
25199a2dd95SBruce Richardson }
25299a2dd95SBruce Richardson
25399a2dd95SBruce Richardson return 0;
25499a2dd95SBruce Richardson }
25599a2dd95SBruce Richardson
25699a2dd95SBruce Richardson struct rte_flow_classifier *
rte_flow_classifier_create(struct rte_flow_classifier_params * params)25799a2dd95SBruce Richardson rte_flow_classifier_create(struct rte_flow_classifier_params *params)
25899a2dd95SBruce Richardson {
25999a2dd95SBruce Richardson struct rte_flow_classifier *cls;
26099a2dd95SBruce Richardson int ret;
26199a2dd95SBruce Richardson
26299a2dd95SBruce Richardson /* Check input parameters */
26399a2dd95SBruce Richardson ret = rte_flow_classifier_check_params(params);
26499a2dd95SBruce Richardson if (ret != 0) {
26599a2dd95SBruce Richardson RTE_FLOW_CLASSIFY_LOG(ERR,
26699a2dd95SBruce Richardson "%s: flow classifier params check failed (%d)\n",
26799a2dd95SBruce Richardson __func__, ret);
26899a2dd95SBruce Richardson return NULL;
26999a2dd95SBruce Richardson }
27099a2dd95SBruce Richardson
27199a2dd95SBruce Richardson /* Allocate memory for the flow classifier */
27299a2dd95SBruce Richardson cls = rte_zmalloc_socket("FLOW_CLASSIFIER",
27399a2dd95SBruce Richardson sizeof(struct rte_flow_classifier),
27499a2dd95SBruce Richardson RTE_CACHE_LINE_SIZE, params->socket_id);
27599a2dd95SBruce Richardson
27699a2dd95SBruce Richardson if (cls == NULL) {
27799a2dd95SBruce Richardson RTE_FLOW_CLASSIFY_LOG(ERR,
27899a2dd95SBruce Richardson "%s: flow classifier memory allocation failed\n",
27999a2dd95SBruce Richardson __func__);
28099a2dd95SBruce Richardson return NULL;
28199a2dd95SBruce Richardson }
28299a2dd95SBruce Richardson
28399a2dd95SBruce Richardson /* Save input parameters */
28499a2dd95SBruce Richardson strlcpy(cls->name, params->name, RTE_FLOW_CLASSIFIER_MAX_NAME_SZ);
28599a2dd95SBruce Richardson
28699a2dd95SBruce Richardson cls->socket_id = params->socket_id;
28799a2dd95SBruce Richardson
28899a2dd95SBruce Richardson return cls;
28999a2dd95SBruce Richardson }
29099a2dd95SBruce Richardson
29199a2dd95SBruce Richardson static void
rte_flow_classify_table_free(struct rte_cls_table * table)29299a2dd95SBruce Richardson rte_flow_classify_table_free(struct rte_cls_table *table)
29399a2dd95SBruce Richardson {
29499a2dd95SBruce Richardson if (table->ops.f_free != NULL)
29599a2dd95SBruce Richardson table->ops.f_free(table->h_table);
29699a2dd95SBruce Richardson }
29799a2dd95SBruce Richardson
29899a2dd95SBruce Richardson int
rte_flow_classifier_free(struct rte_flow_classifier * cls)29999a2dd95SBruce Richardson rte_flow_classifier_free(struct rte_flow_classifier *cls)
30099a2dd95SBruce Richardson {
30199a2dd95SBruce Richardson uint32_t i;
30299a2dd95SBruce Richardson
30399a2dd95SBruce Richardson /* Check input parameters */
30499a2dd95SBruce Richardson if (cls == NULL) {
30599a2dd95SBruce Richardson RTE_FLOW_CLASSIFY_LOG(ERR,
30699a2dd95SBruce Richardson "%s: rte_flow_classifier parameter is NULL\n",
30799a2dd95SBruce Richardson __func__);
30899a2dd95SBruce Richardson return -EINVAL;
30999a2dd95SBruce Richardson }
31099a2dd95SBruce Richardson
31199a2dd95SBruce Richardson /* Free tables */
31299a2dd95SBruce Richardson for (i = 0; i < cls->num_tables; i++) {
31399a2dd95SBruce Richardson struct rte_cls_table *table = &cls->tables[i];
31499a2dd95SBruce Richardson
31599a2dd95SBruce Richardson rte_flow_classify_table_free(table);
31699a2dd95SBruce Richardson }
31799a2dd95SBruce Richardson
31899a2dd95SBruce Richardson /* Free flow classifier memory */
31999a2dd95SBruce Richardson rte_free(cls);
32099a2dd95SBruce Richardson
32199a2dd95SBruce Richardson return 0;
32299a2dd95SBruce Richardson }
32399a2dd95SBruce Richardson
32499a2dd95SBruce Richardson static int
rte_table_check_params(struct rte_flow_classifier * cls,struct rte_flow_classify_table_params * params)32599a2dd95SBruce Richardson rte_table_check_params(struct rte_flow_classifier *cls,
32699a2dd95SBruce Richardson struct rte_flow_classify_table_params *params)
32799a2dd95SBruce Richardson {
32899a2dd95SBruce Richardson if (cls == NULL) {
32999a2dd95SBruce Richardson RTE_FLOW_CLASSIFY_LOG(ERR,
33099a2dd95SBruce Richardson "%s: flow classifier parameter is NULL\n",
33199a2dd95SBruce Richardson __func__);
33299a2dd95SBruce Richardson return -EINVAL;
33399a2dd95SBruce Richardson }
33499a2dd95SBruce Richardson if (params == NULL) {
33599a2dd95SBruce Richardson RTE_FLOW_CLASSIFY_LOG(ERR, "%s: params parameter is NULL\n",
33699a2dd95SBruce Richardson __func__);
33799a2dd95SBruce Richardson return -EINVAL;
33899a2dd95SBruce Richardson }
33999a2dd95SBruce Richardson
34099a2dd95SBruce Richardson /* ops */
34199a2dd95SBruce Richardson if (params->ops == NULL) {
34299a2dd95SBruce Richardson RTE_FLOW_CLASSIFY_LOG(ERR, "%s: params->ops is NULL\n",
34399a2dd95SBruce Richardson __func__);
34499a2dd95SBruce Richardson return -EINVAL;
34599a2dd95SBruce Richardson }
34699a2dd95SBruce Richardson
34799a2dd95SBruce Richardson if (params->ops->f_create == NULL) {
34899a2dd95SBruce Richardson RTE_FLOW_CLASSIFY_LOG(ERR,
34999a2dd95SBruce Richardson "%s: f_create function pointer is NULL\n", __func__);
35099a2dd95SBruce Richardson return -EINVAL;
35199a2dd95SBruce Richardson }
35299a2dd95SBruce Richardson
35399a2dd95SBruce Richardson if (params->ops->f_lookup == NULL) {
35499a2dd95SBruce Richardson RTE_FLOW_CLASSIFY_LOG(ERR,
35599a2dd95SBruce Richardson "%s: f_lookup function pointer is NULL\n", __func__);
35699a2dd95SBruce Richardson return -EINVAL;
35799a2dd95SBruce Richardson }
35899a2dd95SBruce Richardson
35999a2dd95SBruce Richardson /* De we have room for one more table? */
36099a2dd95SBruce Richardson if (cls->num_tables == RTE_FLOW_CLASSIFY_TABLE_MAX) {
36199a2dd95SBruce Richardson RTE_FLOW_CLASSIFY_LOG(ERR,
36299a2dd95SBruce Richardson "%s: Incorrect value for num_tables parameter\n",
36399a2dd95SBruce Richardson __func__);
36499a2dd95SBruce Richardson return -EINVAL;
36599a2dd95SBruce Richardson }
36699a2dd95SBruce Richardson
36799a2dd95SBruce Richardson return 0;
36899a2dd95SBruce Richardson }
36999a2dd95SBruce Richardson
37099a2dd95SBruce Richardson int
rte_flow_classify_table_create(struct rte_flow_classifier * cls,struct rte_flow_classify_table_params * params)37199a2dd95SBruce Richardson rte_flow_classify_table_create(struct rte_flow_classifier *cls,
37299a2dd95SBruce Richardson struct rte_flow_classify_table_params *params)
37399a2dd95SBruce Richardson {
37499a2dd95SBruce Richardson struct rte_cls_table *table;
37599a2dd95SBruce Richardson void *h_table;
37699a2dd95SBruce Richardson uint32_t entry_size;
37799a2dd95SBruce Richardson int ret;
37899a2dd95SBruce Richardson
37999a2dd95SBruce Richardson /* Check input arguments */
38099a2dd95SBruce Richardson ret = rte_table_check_params(cls, params);
38199a2dd95SBruce Richardson if (ret != 0)
38299a2dd95SBruce Richardson return ret;
38399a2dd95SBruce Richardson
38499a2dd95SBruce Richardson /* calculate table entry size */
38599a2dd95SBruce Richardson entry_size = sizeof(struct rte_flow_classify_table_entry);
38699a2dd95SBruce Richardson
38799a2dd95SBruce Richardson /* Create the table */
38899a2dd95SBruce Richardson h_table = params->ops->f_create(params->arg_create, cls->socket_id,
38999a2dd95SBruce Richardson entry_size);
39099a2dd95SBruce Richardson if (h_table == NULL) {
39199a2dd95SBruce Richardson RTE_FLOW_CLASSIFY_LOG(ERR, "%s: Table creation failed\n",
39299a2dd95SBruce Richardson __func__);
39399a2dd95SBruce Richardson return -EINVAL;
39499a2dd95SBruce Richardson }
39599a2dd95SBruce Richardson
39699a2dd95SBruce Richardson /* Commit current table to the classifier */
39799a2dd95SBruce Richardson table = &cls->tables[cls->num_tables];
39899a2dd95SBruce Richardson table->type = params->type;
39999a2dd95SBruce Richardson cls->num_tables++;
40099a2dd95SBruce Richardson
40199a2dd95SBruce Richardson /* Save input parameters */
40299a2dd95SBruce Richardson memcpy(&table->ops, params->ops, sizeof(struct rte_table_ops));
40399a2dd95SBruce Richardson
40499a2dd95SBruce Richardson /* Initialize table internal data structure */
40599a2dd95SBruce Richardson table->entry_size = entry_size;
40699a2dd95SBruce Richardson table->h_table = h_table;
40799a2dd95SBruce Richardson
40899a2dd95SBruce Richardson return 0;
40999a2dd95SBruce Richardson }
41099a2dd95SBruce Richardson
41199a2dd95SBruce Richardson static struct rte_flow_classify_rule *
allocate_acl_ipv4_5tuple_rule(struct rte_flow_classifier * cls)41299a2dd95SBruce Richardson allocate_acl_ipv4_5tuple_rule(struct rte_flow_classifier *cls)
41399a2dd95SBruce Richardson {
41499a2dd95SBruce Richardson struct rte_flow_classify_rule *rule;
41599a2dd95SBruce Richardson
41699a2dd95SBruce Richardson rule = malloc(sizeof(struct rte_flow_classify_rule));
41799a2dd95SBruce Richardson if (!rule)
41899a2dd95SBruce Richardson return rule;
41999a2dd95SBruce Richardson
42099a2dd95SBruce Richardson memset(rule, 0, sizeof(struct rte_flow_classify_rule));
42199a2dd95SBruce Richardson rule->id = unique_id++;
42299a2dd95SBruce Richardson rule->rules.type = RTE_FLOW_CLASSIFY_RULE_TYPE_IPV4_5TUPLE;
42399a2dd95SBruce Richardson
42499a2dd95SBruce Richardson /* key add values */
42599a2dd95SBruce Richardson rule->u.key.key_add.priority = cls->ntuple_filter.priority;
42699a2dd95SBruce Richardson rule->u.key.key_add.field_value[PROTO_FIELD_IPV4].mask_range.u8 =
42799a2dd95SBruce Richardson cls->ntuple_filter.proto_mask;
42899a2dd95SBruce Richardson rule->u.key.key_add.field_value[PROTO_FIELD_IPV4].value.u8 =
42999a2dd95SBruce Richardson cls->ntuple_filter.proto;
43099a2dd95SBruce Richardson rule->rules.u.ipv4_5tuple.proto = cls->ntuple_filter.proto;
43199a2dd95SBruce Richardson rule->rules.u.ipv4_5tuple.proto_mask = cls->ntuple_filter.proto_mask;
43299a2dd95SBruce Richardson
43399a2dd95SBruce Richardson rule->u.key.key_add.field_value[SRC_FIELD_IPV4].mask_range.u32 =
43499a2dd95SBruce Richardson cls->ntuple_filter.src_ip_mask;
43599a2dd95SBruce Richardson rule->u.key.key_add.field_value[SRC_FIELD_IPV4].value.u32 =
43699a2dd95SBruce Richardson cls->ntuple_filter.src_ip;
43799a2dd95SBruce Richardson rule->rules.u.ipv4_5tuple.src_ip_mask = cls->ntuple_filter.src_ip_mask;
43899a2dd95SBruce Richardson rule->rules.u.ipv4_5tuple.src_ip = cls->ntuple_filter.src_ip;
43999a2dd95SBruce Richardson
44099a2dd95SBruce Richardson rule->u.key.key_add.field_value[DST_FIELD_IPV4].mask_range.u32 =
44199a2dd95SBruce Richardson cls->ntuple_filter.dst_ip_mask;
44299a2dd95SBruce Richardson rule->u.key.key_add.field_value[DST_FIELD_IPV4].value.u32 =
44399a2dd95SBruce Richardson cls->ntuple_filter.dst_ip;
44499a2dd95SBruce Richardson rule->rules.u.ipv4_5tuple.dst_ip_mask = cls->ntuple_filter.dst_ip_mask;
44599a2dd95SBruce Richardson rule->rules.u.ipv4_5tuple.dst_ip = cls->ntuple_filter.dst_ip;
44699a2dd95SBruce Richardson
44799a2dd95SBruce Richardson rule->u.key.key_add.field_value[SRCP_FIELD_IPV4].mask_range.u16 =
44899a2dd95SBruce Richardson cls->ntuple_filter.src_port_mask;
44999a2dd95SBruce Richardson rule->u.key.key_add.field_value[SRCP_FIELD_IPV4].value.u16 =
45099a2dd95SBruce Richardson cls->ntuple_filter.src_port;
45199a2dd95SBruce Richardson rule->rules.u.ipv4_5tuple.src_port_mask =
45299a2dd95SBruce Richardson cls->ntuple_filter.src_port_mask;
45399a2dd95SBruce Richardson rule->rules.u.ipv4_5tuple.src_port = cls->ntuple_filter.src_port;
45499a2dd95SBruce Richardson
45599a2dd95SBruce Richardson rule->u.key.key_add.field_value[DSTP_FIELD_IPV4].mask_range.u16 =
45699a2dd95SBruce Richardson cls->ntuple_filter.dst_port_mask;
45799a2dd95SBruce Richardson rule->u.key.key_add.field_value[DSTP_FIELD_IPV4].value.u16 =
45899a2dd95SBruce Richardson cls->ntuple_filter.dst_port;
45999a2dd95SBruce Richardson rule->rules.u.ipv4_5tuple.dst_port_mask =
46099a2dd95SBruce Richardson cls->ntuple_filter.dst_port_mask;
46199a2dd95SBruce Richardson rule->rules.u.ipv4_5tuple.dst_port = cls->ntuple_filter.dst_port;
46299a2dd95SBruce Richardson
46399a2dd95SBruce Richardson if (rte_log_can_log(librte_flow_classify_logtype, RTE_LOG_DEBUG))
46499a2dd95SBruce Richardson print_acl_ipv4_key_add(&rule->u.key.key_add);
46599a2dd95SBruce Richardson
46699a2dd95SBruce Richardson /* key delete values */
46799a2dd95SBruce Richardson memcpy(&rule->u.key.key_del.field_value[PROTO_FIELD_IPV4],
46899a2dd95SBruce Richardson &rule->u.key.key_add.field_value[PROTO_FIELD_IPV4],
46999a2dd95SBruce Richardson NUM_FIELDS_IPV4 * sizeof(struct rte_acl_field));
47099a2dd95SBruce Richardson
47199a2dd95SBruce Richardson if (rte_log_can_log(librte_flow_classify_logtype, RTE_LOG_DEBUG))
47299a2dd95SBruce Richardson print_acl_ipv4_key_delete(&rule->u.key.key_del);
47399a2dd95SBruce Richardson
47499a2dd95SBruce Richardson return rule;
47599a2dd95SBruce Richardson }
47699a2dd95SBruce Richardson
47799a2dd95SBruce Richardson struct rte_flow_classify_rule *
rte_flow_classify_table_entry_add(struct rte_flow_classifier * cls,const struct rte_flow_attr * attr,const struct rte_flow_item pattern[],const struct rte_flow_action actions[],int * key_found,struct rte_flow_error * error)47899a2dd95SBruce Richardson rte_flow_classify_table_entry_add(struct rte_flow_classifier *cls,
47999a2dd95SBruce Richardson const struct rte_flow_attr *attr,
48099a2dd95SBruce Richardson const struct rte_flow_item pattern[],
48199a2dd95SBruce Richardson const struct rte_flow_action actions[],
48299a2dd95SBruce Richardson int *key_found,
48399a2dd95SBruce Richardson struct rte_flow_error *error)
48499a2dd95SBruce Richardson {
48599a2dd95SBruce Richardson struct rte_flow_classify_rule *rule;
48699a2dd95SBruce Richardson struct rte_flow_classify_table_entry *table_entry;
48799a2dd95SBruce Richardson struct classify_action *action;
48899a2dd95SBruce Richardson uint32_t i;
48999a2dd95SBruce Richardson int ret;
49099a2dd95SBruce Richardson
49199a2dd95SBruce Richardson if (!error)
49299a2dd95SBruce Richardson return NULL;
49399a2dd95SBruce Richardson
49499a2dd95SBruce Richardson if (key_found == NULL) {
49599a2dd95SBruce Richardson rte_flow_error_set(error, EINVAL,
49699a2dd95SBruce Richardson RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
49799a2dd95SBruce Richardson NULL, "NULL key_found.");
49899a2dd95SBruce Richardson return NULL;
49999a2dd95SBruce Richardson }
50099a2dd95SBruce Richardson
50199a2dd95SBruce Richardson /* parse attr, pattern and actions */
50299a2dd95SBruce Richardson ret = rte_flow_classify_validate(cls, attr, pattern, actions, error);
50399a2dd95SBruce Richardson if (ret < 0)
50499a2dd95SBruce Richardson return NULL;
50599a2dd95SBruce Richardson
50699a2dd95SBruce Richardson switch (table_type) {
50799a2dd95SBruce Richardson case RTE_FLOW_CLASSIFY_TABLE_ACL_IP4_5TUPLE:
50899a2dd95SBruce Richardson rule = allocate_acl_ipv4_5tuple_rule(cls);
50999a2dd95SBruce Richardson if (!rule)
51099a2dd95SBruce Richardson return NULL;
51199a2dd95SBruce Richardson rule->tbl_type = table_type;
51299a2dd95SBruce Richardson cls->table_mask |= table_type;
51399a2dd95SBruce Richardson break;
51499a2dd95SBruce Richardson default:
51599a2dd95SBruce Richardson return NULL;
51699a2dd95SBruce Richardson }
51799a2dd95SBruce Richardson
51899a2dd95SBruce Richardson action = classify_get_flow_action();
51999a2dd95SBruce Richardson table_entry = &rule->entry;
52099a2dd95SBruce Richardson table_entry->rule_id = rule->id;
52199a2dd95SBruce Richardson table_entry->action.action_mask = action->action_mask;
52299a2dd95SBruce Richardson
52399a2dd95SBruce Richardson /* Copy actions */
52499a2dd95SBruce Richardson if (action->action_mask & (1LLU << RTE_FLOW_ACTION_TYPE_COUNT)) {
52599a2dd95SBruce Richardson memcpy(&table_entry->action.act.counter, &action->act.counter,
52699a2dd95SBruce Richardson sizeof(table_entry->action.act.counter));
52799a2dd95SBruce Richardson }
52899a2dd95SBruce Richardson if (action->action_mask & (1LLU << RTE_FLOW_ACTION_TYPE_MARK)) {
52999a2dd95SBruce Richardson memcpy(&table_entry->action.act.mark, &action->act.mark,
53099a2dd95SBruce Richardson sizeof(table_entry->action.act.mark));
53199a2dd95SBruce Richardson }
53299a2dd95SBruce Richardson
53399a2dd95SBruce Richardson for (i = 0; i < cls->num_tables; i++) {
53499a2dd95SBruce Richardson struct rte_cls_table *table = &cls->tables[i];
53599a2dd95SBruce Richardson
53699a2dd95SBruce Richardson if (table->type == table_type) {
53799a2dd95SBruce Richardson if (table->ops.f_add != NULL) {
53899a2dd95SBruce Richardson ret = table->ops.f_add(
53999a2dd95SBruce Richardson table->h_table,
54099a2dd95SBruce Richardson &rule->u.key.key_add,
54199a2dd95SBruce Richardson &rule->entry,
54299a2dd95SBruce Richardson &rule->key_found,
54399a2dd95SBruce Richardson &rule->entry_ptr);
54499a2dd95SBruce Richardson if (ret) {
54599a2dd95SBruce Richardson free(rule);
54699a2dd95SBruce Richardson return NULL;
54799a2dd95SBruce Richardson }
54899a2dd95SBruce Richardson
54999a2dd95SBruce Richardson *key_found = rule->key_found;
55099a2dd95SBruce Richardson }
55199a2dd95SBruce Richardson
55299a2dd95SBruce Richardson return rule;
55399a2dd95SBruce Richardson }
55499a2dd95SBruce Richardson }
55599a2dd95SBruce Richardson free(rule);
55699a2dd95SBruce Richardson return NULL;
55799a2dd95SBruce Richardson }
55899a2dd95SBruce Richardson
55999a2dd95SBruce Richardson int
rte_flow_classify_table_entry_delete(struct rte_flow_classifier * cls,struct rte_flow_classify_rule * rule)56099a2dd95SBruce Richardson rte_flow_classify_table_entry_delete(struct rte_flow_classifier *cls,
56199a2dd95SBruce Richardson struct rte_flow_classify_rule *rule)
56299a2dd95SBruce Richardson {
56399a2dd95SBruce Richardson uint32_t i;
56499a2dd95SBruce Richardson int ret = -EINVAL;
56599a2dd95SBruce Richardson
56699a2dd95SBruce Richardson if (!cls || !rule)
56799a2dd95SBruce Richardson return ret;
56899a2dd95SBruce Richardson enum rte_flow_classify_table_type tbl_type = rule->tbl_type;
56999a2dd95SBruce Richardson
57099a2dd95SBruce Richardson for (i = 0; i < cls->num_tables; i++) {
57199a2dd95SBruce Richardson struct rte_cls_table *table = &cls->tables[i];
57299a2dd95SBruce Richardson
57399a2dd95SBruce Richardson if (table->type == tbl_type) {
57499a2dd95SBruce Richardson if (table->ops.f_delete != NULL) {
57599a2dd95SBruce Richardson ret = table->ops.f_delete(table->h_table,
57699a2dd95SBruce Richardson &rule->u.key.key_del,
57799a2dd95SBruce Richardson &rule->key_found,
57899a2dd95SBruce Richardson &rule->entry);
579*016441e3SOwen Hilyard if (ret == 0)
580*016441e3SOwen Hilyard free(rule);
58199a2dd95SBruce Richardson return ret;
58299a2dd95SBruce Richardson }
58399a2dd95SBruce Richardson }
58499a2dd95SBruce Richardson }
58599a2dd95SBruce Richardson return ret;
58699a2dd95SBruce Richardson }
58799a2dd95SBruce Richardson
58899a2dd95SBruce Richardson static int
flow_classifier_lookup(struct rte_flow_classifier * cls,struct rte_cls_table * table,struct rte_mbuf ** pkts,const uint16_t nb_pkts)58999a2dd95SBruce Richardson flow_classifier_lookup(struct rte_flow_classifier *cls,
59099a2dd95SBruce Richardson struct rte_cls_table *table,
59199a2dd95SBruce Richardson struct rte_mbuf **pkts,
59299a2dd95SBruce Richardson const uint16_t nb_pkts)
59399a2dd95SBruce Richardson {
59499a2dd95SBruce Richardson int ret = -EINVAL;
59599a2dd95SBruce Richardson uint64_t pkts_mask;
59699a2dd95SBruce Richardson uint64_t lookup_hit_mask;
59799a2dd95SBruce Richardson
59899a2dd95SBruce Richardson pkts_mask = RTE_LEN2MASK(nb_pkts, uint64_t);
59999a2dd95SBruce Richardson ret = table->ops.f_lookup(table->h_table,
60099a2dd95SBruce Richardson pkts, pkts_mask, &lookup_hit_mask,
60199a2dd95SBruce Richardson (void **)cls->entries);
60299a2dd95SBruce Richardson
60399a2dd95SBruce Richardson if (!ret && lookup_hit_mask)
60499a2dd95SBruce Richardson cls->nb_pkts = nb_pkts;
60599a2dd95SBruce Richardson else
60699a2dd95SBruce Richardson cls->nb_pkts = 0;
60799a2dd95SBruce Richardson
60899a2dd95SBruce Richardson return ret;
60999a2dd95SBruce Richardson }
61099a2dd95SBruce Richardson
61199a2dd95SBruce Richardson static int
action_apply(struct rte_flow_classifier * cls,struct rte_flow_classify_rule * rule,struct rte_flow_classify_stats * stats)61299a2dd95SBruce Richardson action_apply(struct rte_flow_classifier *cls,
61399a2dd95SBruce Richardson struct rte_flow_classify_rule *rule,
61499a2dd95SBruce Richardson struct rte_flow_classify_stats *stats)
61599a2dd95SBruce Richardson {
61699a2dd95SBruce Richardson struct rte_flow_classify_ipv4_5tuple_stats *ntuple_stats;
61799a2dd95SBruce Richardson struct rte_flow_classify_table_entry *entry = &rule->entry;
61899a2dd95SBruce Richardson uint64_t count = 0;
61999a2dd95SBruce Richardson uint32_t action_mask = entry->action.action_mask;
62099a2dd95SBruce Richardson int i, ret = -EINVAL;
62199a2dd95SBruce Richardson
62299a2dd95SBruce Richardson if (action_mask & (1LLU << RTE_FLOW_ACTION_TYPE_COUNT)) {
62399a2dd95SBruce Richardson for (i = 0; i < cls->nb_pkts; i++) {
62499a2dd95SBruce Richardson if (rule->id == cls->entries[i]->rule_id)
62599a2dd95SBruce Richardson count++;
62699a2dd95SBruce Richardson }
62799a2dd95SBruce Richardson if (count) {
62899a2dd95SBruce Richardson ret = 0;
62999a2dd95SBruce Richardson ntuple_stats = stats->stats;
63099a2dd95SBruce Richardson ntuple_stats->counter1 = count;
63199a2dd95SBruce Richardson ntuple_stats->ipv4_5tuple = rule->rules.u.ipv4_5tuple;
63299a2dd95SBruce Richardson }
63399a2dd95SBruce Richardson }
63499a2dd95SBruce Richardson return ret;
63599a2dd95SBruce Richardson }
63699a2dd95SBruce Richardson
63799a2dd95SBruce Richardson int
rte_flow_classifier_query(struct rte_flow_classifier * cls,struct rte_mbuf ** pkts,const uint16_t nb_pkts,struct rte_flow_classify_rule * rule,struct rte_flow_classify_stats * stats)63899a2dd95SBruce Richardson rte_flow_classifier_query(struct rte_flow_classifier *cls,
63999a2dd95SBruce Richardson struct rte_mbuf **pkts,
64099a2dd95SBruce Richardson const uint16_t nb_pkts,
64199a2dd95SBruce Richardson struct rte_flow_classify_rule *rule,
64299a2dd95SBruce Richardson struct rte_flow_classify_stats *stats)
64399a2dd95SBruce Richardson {
64499a2dd95SBruce Richardson enum rte_flow_classify_table_type tbl_type;
64599a2dd95SBruce Richardson uint32_t i;
64699a2dd95SBruce Richardson int ret = -EINVAL;
64799a2dd95SBruce Richardson
64899a2dd95SBruce Richardson if (!cls || !rule || !stats || !pkts || nb_pkts == 0)
64999a2dd95SBruce Richardson return ret;
65099a2dd95SBruce Richardson
65199a2dd95SBruce Richardson tbl_type = rule->tbl_type;
65299a2dd95SBruce Richardson for (i = 0; i < cls->num_tables; i++) {
65399a2dd95SBruce Richardson struct rte_cls_table *table = &cls->tables[i];
65499a2dd95SBruce Richardson
65599a2dd95SBruce Richardson if (table->type == tbl_type) {
65699a2dd95SBruce Richardson ret = flow_classifier_lookup(cls, table,
65799a2dd95SBruce Richardson pkts, nb_pkts);
65899a2dd95SBruce Richardson if (!ret) {
65999a2dd95SBruce Richardson ret = action_apply(cls, rule, stats);
66099a2dd95SBruce Richardson return ret;
66199a2dd95SBruce Richardson }
66299a2dd95SBruce Richardson }
66399a2dd95SBruce Richardson }
66499a2dd95SBruce Richardson return ret;
66599a2dd95SBruce Richardson }
66699a2dd95SBruce Richardson
667eeded204SDavid Marchand RTE_LOG_REGISTER_DEFAULT(librte_flow_classify_logtype, INFO);
668