From: Pablo Neira Ayuso <pablo@netfilter.org>
To: netfilter-devel@vger.kernel.org
Subject: [PATCH nft 3/5] cache: pass name to cache_add()
Date: Sun, 15 Jun 2025 12:00:17 +0200 [thread overview]
Message-ID: <20250615100019.2988872-4-pablo@netfilter.org> (raw)
In-Reply-To: <20250615100019.2988872-1-pablo@netfilter.org>
Consolidate the name hash in the cache_add() function.
No functional changes are intended.
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
include/cache.h | 2 +-
src/cache.c | 49 +++++++++++++++----------------------------------
2 files changed, 16 insertions(+), 35 deletions(-)
diff --git a/include/cache.h b/include/cache.h
index e6bde6c6bee3..c969534f4ea7 100644
--- a/include/cache.h
+++ b/include/cache.h
@@ -123,7 +123,7 @@ struct cache_item {
void cache_init(struct cache *cache);
void cache_free(struct cache *cache);
-void cache_add(struct cache_item *item, struct cache *cache, uint32_t hash);
+void cache_add(struct cache_item *item, struct cache *cache, const char *name);
void cache_del(struct cache_item *item);
void table_cache_add(struct table *table, struct nft_cache *cache);
diff --git a/src/cache.c b/src/cache.c
index d16052c608d5..d58fb59ff061 100644
--- a/src/cache.c
+++ b/src/cache.c
@@ -534,10 +534,7 @@ int nft_cache_evaluate(struct nft_ctx *nft, struct list_head *cmds,
void table_cache_add(struct table *table, struct nft_cache *cache)
{
- uint32_t hash;
-
- hash = djb_hash(table->handle.table.name) % NFT_CACHE_HSIZE;
- cache_add(&table->cache, &cache->table_cache, hash);
+ cache_add(&table->cache, &cache->table_cache, table->handle.table.name);
}
void table_cache_del(struct table *table)
@@ -572,8 +569,8 @@ static int chain_cache_cb(struct nftnl_chain *nlc, void *arg)
{
struct chain_cache_dump_ctx *ctx = arg;
const char *chain_name, *table_name;
- uint32_t hash, family;
struct chain *chain;
+ uint32_t family;
table_name = nftnl_chain_get_str(nlc, NFTNL_CHAIN_TABLE);
family = nftnl_chain_get_u32(nlc, NFTNL_CHAIN_FAMILY);
@@ -583,13 +580,12 @@ static int chain_cache_cb(struct nftnl_chain *nlc, void *arg)
return 0;
chain_name = nftnl_chain_get_str(nlc, NFTNL_CHAIN_NAME);
- hash = djb_hash(chain_name) % NFT_CACHE_HSIZE;
- chain = netlink_delinearize_chain(ctx->nlctx, nlc);
+ chain = netlink_delinearize_chain(ctx->nlctx, nlc);
if (chain->flags & CHAIN_F_BINDING) {
list_add_tail(&chain->cache.list, &ctx->table->chain_bindings);
} else {
- cache_add(&chain->cache, &ctx->table->chain_cache, hash);
+ cache_add(&chain->cache, &ctx->table->chain_cache, chain_name);
}
nftnl_chain_list_del(nlc);
@@ -655,10 +651,7 @@ void nft_chain_cache_update(struct netlink_ctx *ctx, struct table *table,
void chain_cache_add(struct chain *chain, struct table *table)
{
- uint32_t hash;
-
- hash = djb_hash(chain->handle.chain.name) % NFT_CACHE_HSIZE;
- cache_add(&chain->cache, &table->chain_cache, hash);
+ cache_add(&chain->cache, &table->chain_cache, chain->handle.chain.name);
}
void chain_cache_del(struct chain *chain)
@@ -760,7 +753,6 @@ static int set_cache_cb(struct nftnl_set *nls, void *arg)
const char *set_name;
uint32_t set_family;
struct set *set;
- uint32_t hash;
set_table = nftnl_set_get_str(nls, NFTNL_SET_TABLE);
set_family = nftnl_set_get_u32(nls, NFTNL_SET_FAMILY);
@@ -774,8 +766,7 @@ static int set_cache_cb(struct nftnl_set *nls, void *arg)
return -1;
set_name = nftnl_set_get_str(nls, NFTNL_SET_NAME);
- hash = djb_hash(set_name) % NFT_CACHE_HSIZE;
- cache_add(&set->cache, &ctx->table->set_cache, hash);
+ cache_add(&set->cache, &ctx->table->set_cache, set_name);
nftnl_set_list_del(nls);
nftnl_set_free(nls);
@@ -825,10 +816,7 @@ set_cache_dump(struct netlink_ctx *ctx,
void set_cache_add(struct set *set, struct table *table)
{
- uint32_t hash;
-
- hash = djb_hash(set->handle.set.name) % NFT_CACHE_HSIZE;
- cache_add(&set->cache, &table->set_cache, hash);
+ cache_add(&set->cache, &table->set_cache, set->handle.set.name);
}
void set_cache_del(struct set *set)
@@ -864,7 +852,6 @@ static int obj_cache_cb(struct nftnl_obj *nlo, void *arg)
const char *obj_name;
uint32_t obj_family;
struct obj *obj;
- uint32_t hash;
obj_table = nftnl_obj_get_str(nlo, NFTNL_OBJ_TABLE);
obj_family = nftnl_obj_get_u32(nlo, NFTNL_OBJ_FAMILY);
@@ -876,8 +863,7 @@ static int obj_cache_cb(struct nftnl_obj *nlo, void *arg)
obj = netlink_delinearize_obj(ctx->nlctx, nlo);
if (obj) {
obj_name = nftnl_obj_get_str(nlo, NFTNL_OBJ_NAME);
- hash = djb_hash(obj_name) % NFT_CACHE_HSIZE;
- cache_add(&obj->cache, &ctx->table->obj_cache, hash);
+ cache_add(&obj->cache, &ctx->table->obj_cache, obj_name);
}
nftnl_obj_list_del(nlo);
@@ -940,10 +926,7 @@ static struct nftnl_obj_list *obj_cache_dump(struct netlink_ctx *ctx,
void obj_cache_add(struct obj *obj, struct table *table)
{
- uint32_t hash;
-
- hash = djb_hash(obj->handle.obj.name) % NFT_CACHE_HSIZE;
- cache_add(&obj->cache, &table->obj_cache, hash);
+ cache_add(&obj->cache, &table->obj_cache, obj->handle.obj.name);
}
void obj_cache_del(struct obj *obj)
@@ -981,7 +964,6 @@ static int ft_cache_cb(struct nftnl_flowtable *nlf, void *arg)
const char *ft_table;
const char *ft_name;
uint32_t ft_family;
- uint32_t hash;
ft_family = nftnl_flowtable_get_u32(nlf, NFTNL_FLOWTABLE_FAMILY);
ft_table = nftnl_flowtable_get_str(nlf, NFTNL_FLOWTABLE_TABLE);
@@ -995,8 +977,7 @@ static int ft_cache_cb(struct nftnl_flowtable *nlf, void *arg)
return -1;
ft_name = nftnl_flowtable_get_str(nlf, NFTNL_FLOWTABLE_NAME);
- hash = djb_hash(ft_name) % NFT_CACHE_HSIZE;
- cache_add(&ft->cache, &ctx->table->ft_cache, hash);
+ cache_add(&ft->cache, &ctx->table->ft_cache, ft_name);
nftnl_flowtable_list_del(nlf);
nftnl_flowtable_free(nlf);
@@ -1047,10 +1028,7 @@ ft_cache_dump(struct netlink_ctx *ctx, const struct nft_cache_filter *filter)
void ft_cache_add(struct flowtable *ft, struct table *table)
{
- uint32_t hash;
-
- hash = djb_hash(ft->handle.flowtable.name) % NFT_CACHE_HSIZE;
- cache_add(&ft->cache, &table->ft_cache, hash);
+ cache_add(&ft->cache, &table->ft_cache, ft->handle.flowtable.name);
}
void ft_cache_del(struct flowtable *ft)
@@ -1389,8 +1367,11 @@ void cache_free(struct cache *cache)
free(cache->ht);
}
-void cache_add(struct cache_item *item, struct cache *cache, uint32_t hash)
+void cache_add(struct cache_item *item, struct cache *cache, const char *name)
{
+ uint32_t hash;
+
+ hash = djb_hash(name) % NFT_CACHE_HSIZE;
list_add_tail(&item->hlist, &cache->ht[hash]);
list_add_tail(&item->list, &cache->list);
}
--
2.30.2
next prev parent reply other threads:[~2025-06-15 10:00 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-15 10:00 [PATCH nft 0/5] assorted updates and fixes Pablo Neira Ayuso
2025-06-15 10:00 ` [PATCH nft 1/5] rule: skip fuzzy lookup if object name is not available Pablo Neira Ayuso
2025-06-15 10:00 ` [PATCH nft 2/5] cache: assert name is non-nul when looking up Pablo Neira Ayuso
2025-06-15 10:00 ` Pablo Neira Ayuso [this message]
2025-06-15 10:00 ` [PATCH nft 4/5] parser_bison: only reset by name is supported by now Pablo Neira Ayuso
2025-06-15 10:00 ` [PATCH nft 5/5] parser_bison: allow delete command with map via handle Pablo Neira Ayuso
2025-06-23 17:08 ` [PATCH nft 0/5] assorted updates and fixes Pablo Neira Ayuso
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250615100019.2988872-4-pablo@netfilter.org \
--to=pablo@netfilter.org \
--cc=netfilter-devel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.