From 31a57f195f1cda03741a18c3393b7bebddaacf1c Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Thu, 1 Apr 2021 22:25:24 +0200 Subject: evaluate: use chain hashtable for lookups Instead of the linear list lookup. Before this patch: real 0m21,735s user 0m20,329s sys 0m1,384s After: real 0m10,910s user 0m9,448s sys 0m1,434s chain_lookup() is removed since linear list lookups are only used by the fuzzy chain name matching for error reporting. Signed-off-by: Pablo Neira Ayuso --- include/rule.h | 2 -- src/evaluate.c | 8 ++++---- src/netlink.c | 2 +- src/rule.c | 13 +------------ 4 files changed, 6 insertions(+), 19 deletions(-) diff --git a/include/rule.h b/include/rule.h index 6c6ada6b..ad9cca90 100644 --- a/include/rule.h +++ b/include/rule.h @@ -259,8 +259,6 @@ extern const char *chain_hookname_lookup(const char *name); extern struct chain *chain_alloc(const char *name); extern struct chain *chain_get(struct chain *chain); extern void chain_free(struct chain *chain); -extern struct chain *chain_lookup(const struct table *table, - const struct handle *h); extern struct chain *chain_lookup_fuzzy(const struct handle *h, const struct nft_cache *cache, const struct table **table); diff --git a/src/evaluate.c b/src/evaluate.c index 8105d8d5..50f100db 100644 --- a/src/evaluate.c +++ b/src/evaluate.c @@ -4109,14 +4109,14 @@ static int chain_evaluate(struct eval_ctx *ctx, struct chain *chain) return table_not_found(ctx); if (chain == NULL) { - if (chain_lookup(table, &ctx->cmd->handle) == NULL) { + if (chain_cache_find(table, &ctx->cmd->handle) == NULL) { chain = chain_alloc(NULL); handle_merge(&chain->handle, &ctx->cmd->handle); chain_cache_add(chain, table); } return 0; } else if (!(chain->flags & CHAIN_F_BINDING)) { - if (chain_lookup(table, &chain->handle) == NULL) + if (chain_cache_find(table, &chain->handle) == NULL) chain_cache_add(chain_get(chain), table); } @@ -4443,7 +4443,7 @@ static int cmd_evaluate_list(struct eval_ctx *ctx, struct cmd *cmd) if (table == NULL) return table_not_found(ctx); - if (chain_lookup(table, &cmd->handle) == NULL) + if (chain_cache_find(table, &cmd->handle) == NULL) return chain_not_found(ctx); return 0; @@ -4603,7 +4603,7 @@ static int cmd_evaluate_rename(struct eval_ctx *ctx, struct cmd *cmd) if (table == NULL) return table_not_found(ctx); - if (chain_lookup(table, &ctx->cmd->handle) == NULL) + if (chain_cache_find(table, &ctx->cmd->handle) == NULL) return chain_not_found(ctx); break; diff --git a/src/netlink.c b/src/netlink.c index 97ae88c7..89d224ed 100644 --- a/src/netlink.c +++ b/src/netlink.c @@ -1749,7 +1749,7 @@ static struct rule *trace_lookup_rule(const struct nftnl_trace *nlt, if (!table) return NULL; - chain = chain_lookup(table, &h); + chain = chain_cache_find(table, &h); if (!chain) return NULL; diff --git a/src/rule.c b/src/rule.c index 5be9c0c8..9c9fd7fd 100644 --- a/src/rule.c +++ b/src/rule.c @@ -761,17 +761,6 @@ void chain_free(struct chain *chain) xfree(chain); } -struct chain *chain_lookup(const struct table *table, const struct handle *h) -{ - struct chain *chain; - - list_for_each_entry(chain, &table->cache_chain, cache_list) { - if (!strcmp(chain->handle.chain.name, h->chain.name)) - return chain; - } - return NULL; -} - struct chain *chain_binding_lookup(const struct table *table, const char *chain_name) { @@ -2625,7 +2614,7 @@ static int do_command_rename(struct netlink_ctx *ctx, struct cmd *cmd) switch (cmd->obj) { case CMD_OBJ_CHAIN: - chain = chain_lookup(table, &cmd->handle); + chain = chain_cache_find(table, &cmd->handle); return mnl_nft_chain_rename(ctx, cmd, chain); default: -- cgit v1.2.3