1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation
8 #include <rte_common.h>
9 #include <rte_cycles.h>
12 #include "rte_swx_table_wm.h"
14 #ifndef RTE_SWX_TABLE_EM_USE_HUGE_PAGES
15 #define RTE_SWX_TABLE_EM_USE_HUGE_PAGES 1
18 #if RTE_SWX_TABLE_EM_USE_HUGE_PAGES
20 #include <rte_malloc.h>
23 env_malloc(size_t size, size_t alignment, int numa_node)
25 return rte_zmalloc_socket(NULL, size, alignment, numa_node);
29 env_free(void *start, size_t size __rte_unused)
39 env_malloc(size_t size, size_t alignment __rte_unused, int numa_node)
41 return numa_alloc_onnode(size, numa_node);
45 env_free(void *start, size_t size)
47 numa_free(start, size);
52 static char *get_unique_name(void)
54 uint64_t tsc = rte_get_tsc_cycles();
55 size_t size = sizeof(uint64_t) * 2 + 1;
56 char *name = calloc(1, size);
61 snprintf(name, size, "%016" PRIx64, tsc);
66 count_entries(struct rte_swx_table_entry_list *entries)
68 struct rte_swx_table_entry *entry;
69 uint32_t n_entries = 0;
74 TAILQ_FOREACH(entry, entries, node)
81 acl_table_cfg_get(struct rte_acl_config *cfg, struct rte_swx_table_params *p)
83 uint32_t byte_id = 0, field_id = 0;
85 /* cfg->num_categories. */
86 cfg->num_categories = 1;
88 /* cfg->defs and cfg->num_fields. */
89 for (byte_id = 0; byte_id < p->key_size; ) {
90 uint32_t field_size = field_id ? 4 : 1;
91 uint8_t byte = p->key_mask0 ? p->key_mask0[byte_id] : 0xFF;
98 if (field_id == RTE_ACL_MAX_FIELDS)
101 cfg->defs[field_id].type = RTE_ACL_FIELD_TYPE_BITMASK;
102 cfg->defs[field_id].size = field_size;
103 cfg->defs[field_id].field_index = field_id;
104 cfg->defs[field_id].input_index = field_id;
105 cfg->defs[field_id].offset = p->key_offset + byte_id;
108 byte_id += field_size;
114 cfg->num_fields = field_id;
123 acl_table_rule_field8(uint8_t *value,
132 km0 = key_mask0 ? key_mask0[offset] : 0xFF;
133 km = key_mask ? key_mask[offset] : 0xFF;
135 *value = key[offset];
140 acl_table_rule_field32(uint32_t *value,
148 uint32_t km0[4], km[4], k[4];
151 /* Byte 0 = MSB, byte 3 = LSB. */
152 for (byte_id = 0; byte_id < 4; byte_id++) {
153 if (offset + byte_id >= key_size) {
160 km0[byte_id] = key_mask0 ? key_mask0[offset + byte_id] : 0xFF;
161 km[byte_id] = key_mask ? key_mask[offset + byte_id] : 0xFF;
162 k[byte_id] = key[offset + byte_id];
165 *value = (k[0] << 24) |
170 *mask = ((km[0] & km0[0]) << 24) |
171 ((km[1] & km0[1]) << 16) |
172 ((km[2] & km0[2]) << 8) |
176 RTE_ACL_RULE_DEF(acl_rule, RTE_ACL_MAX_FIELDS);
178 static struct rte_acl_rule *
179 acl_table_rules_get(struct rte_acl_config *acl_cfg,
180 struct rte_swx_table_params *p,
181 struct rte_swx_table_entry_list *entries,
184 struct rte_swx_table_entry *entry;
186 uint32_t acl_rule_size = RTE_ACL_RULE_SZ(acl_cfg->num_fields);
187 uint32_t n_fields = acl_cfg->num_fields;
193 memory = malloc(n_entries * acl_rule_size);
198 TAILQ_FOREACH(entry, entries, node) {
199 uint8_t *m = &memory[rule_id * acl_rule_size];
200 struct acl_rule *acl_rule = (struct acl_rule *)m;
203 acl_rule->data.category_mask = 1;
204 acl_rule->data.priority = RTE_ACL_MAX_PRIORITY -
206 acl_rule->data.userdata = rule_id + 1;
208 for (field_id = 0; field_id < n_fields; field_id++) {
209 struct rte_acl_field *f = &acl_rule->field[field_id];
210 uint32_t size = acl_cfg->defs[field_id].size;
211 uint32_t offset = acl_cfg->defs[field_id].offset -
217 acl_table_rule_field8(&value,
225 f->mask_range.u8 = mask;
227 uint32_t value, mask;
229 acl_table_rule_field32(&value,
237 f->value.u32 = value;
238 f->mask_range.u32 = mask;
245 return (struct rte_acl_rule *)memory;
248 /* When the table to be created has no rules, the expected behavior is to always
249 * get lookup miss for any input key. To achieve this, we add a single bogus
250 * rule to the table with the rule user data set to 0, i.e. the value returned
251 * when lookup miss takes place. Whether lookup hit (the bogus rule is hit) or
252 * miss, a user data of 0 is returned, which for the ACL library is equivalent
255 static struct rte_acl_rule *
256 acl_table_rules_default_get(struct rte_acl_config *acl_cfg)
258 struct rte_acl_rule *acl_rule;
259 uint32_t acl_rule_size = RTE_ACL_RULE_SZ(acl_cfg->num_fields);
261 acl_rule = calloc(1, acl_rule_size);
265 acl_rule->data.category_mask = 1;
266 acl_rule->data.priority = RTE_ACL_MAX_PRIORITY;
267 acl_rule->data.userdata = 0;
269 memset(&acl_rule[1], 0xFF, acl_rule_size - sizeof(struct rte_acl_rule));
274 static struct rte_acl_ctx *
275 acl_table_create(struct rte_swx_table_params *params,
276 struct rte_swx_table_entry_list *entries,
280 struct rte_acl_param acl_params = {0};
281 struct rte_acl_config acl_cfg = {0};
282 struct rte_acl_ctx *acl_ctx = NULL;
283 struct rte_acl_rule *acl_rules = NULL;
287 /* ACL config data structures. */
288 name = get_unique_name();
294 status = acl_table_cfg_get(&acl_cfg, params);
298 acl_rules = n_entries ?
299 acl_table_rules_get(&acl_cfg, params, entries, n_entries) :
300 acl_table_rules_default_get(&acl_cfg);
306 n_entries = n_entries ? n_entries : 1;
309 acl_params.name = name;
310 acl_params.socket_id = numa_node;
311 acl_params.rule_size = RTE_ACL_RULE_SZ(acl_cfg.num_fields);
312 acl_params.max_rule_num = n_entries;
314 acl_ctx = rte_acl_create(&acl_params);
321 status = rte_acl_add_rules(acl_ctx, acl_rules, n_entries);
326 status = rte_acl_build(acl_ctx, &acl_cfg);
329 if (status && acl_ctx)
330 rte_acl_free(acl_ctx);
336 return status ? NULL : acl_ctx;
340 entry_data_copy(uint8_t *data,
341 struct rte_swx_table_entry_list *entries,
343 uint32_t entry_data_size)
345 struct rte_swx_table_entry *entry;
351 TAILQ_FOREACH(entry, entries, node) {
352 uint64_t *d = (uint64_t *)&data[i * entry_data_size];
354 d[0] = entry->action_id;
355 memcpy(&d[1], entry->action_data, entry_data_size - 8);
362 struct rte_acl_ctx *acl_ctx;
365 uint32_t entry_data_size;
369 table_free(void *table)
371 struct table *t = table;
377 rte_acl_free(t->acl_ctx);
378 env_free(t, t->total_size);
382 table_create(struct rte_swx_table_params *params,
383 struct rte_swx_table_entry_list *entries,
384 const char *args __rte_unused,
387 struct table *t = NULL;
388 size_t meta_sz, data_sz, total_size;
389 uint32_t entry_data_size;
390 uint32_t n_entries = count_entries(entries);
392 /* Check input arguments. */
393 if (!params || !params->key_size)
396 /* Memory allocation and initialization. */
397 entry_data_size = 8 + params->action_data_size;
398 meta_sz = sizeof(struct table);
399 data_sz = n_entries * entry_data_size;
400 total_size = meta_sz + data_sz;
402 t = env_malloc(total_size, RTE_CACHE_LINE_SIZE, numa_node);
406 memset(t, 0, total_size);
407 t->entry_data_size = entry_data_size;
408 t->total_size = total_size;
409 t->data = (uint8_t *)&t[1];
411 t->acl_ctx = acl_table_create(params, entries, n_entries, numa_node);
415 entry_data_copy(t->data, entries, n_entries, entry_data_size);
429 table_mailbox_size_get(void)
431 return sizeof(struct mailbox);
435 table_lookup(void *table,
436 void *mailbox __rte_unused,
439 uint8_t **action_data,
442 struct table *t = table;
446 rte_acl_classify(t->acl_ctx, key, &user_data, 1, 1);
452 data = &t->data[(user_data - 1) * t->entry_data_size];
453 *action_id = ((uint64_t *)data)[0];
454 *action_data = &data[8];
459 struct rte_swx_table_ops rte_swx_table_wildcard_match_ops = {
460 .footprint_get = NULL,
461 .mailbox_size_get = table_mailbox_size_get,
462 .create = table_create,
465 .lkp = (rte_swx_table_lookup_t)table_lookup,