]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
cache: pass name to cache_add()
authorPablo Neira Ayuso <pablo@netfilter.org>
Sun, 15 Jun 2025 09:34:04 +0000 (11:34 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 23 Jun 2025 16:41:05 +0000 (18:41 +0200)
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
src/cache.c

index e6bde6c6bee322ae405759cb390aa4f98c70f941..c969534f4ea7855970190b3c9369f771b3b6fbdb 100644 (file)
@@ -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);
index d16052c608d5c4294a32b341d3db447727756112..d58fb59ff0613173b9a11abc13fd8315668a7840 100644 (file)
@@ -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);
 }