Skip to contentSkip to conversation

cache updates

PATCH New Pablo Neira Ayuso 1 participant 11 messages Apr 15, 2021 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover cache updates

Hi,

The following patchset contains cache updates for nft:

#1 adds a object hashtable for lookups by name. #2 adds a flowtable hashtable for lookups by name. #3 adds set_cache_del() and use it from the monitor path. #4 adds ruleset-defined sets to the cache.
#5 adds ruleset-defined flowtables to the cache. #6 populates the table cache for several objects. #7 adds ruleset-define policy objects to the cache. #8 move struct nft_cache declaration to include/cache.h #9 adds a table hashtable for lookups by name. #10 removes table_lookup_global() which is not required
anymore after the previous updates.

Pablo Neira Ayuso (10):
cache: add hashtable cache for object
cache: add hashtable cache for flowtable
cache: add set_cache_del() and use it
evaluate: add set to the cache
evaluate: add flowtable to the cache
cache: missing table cache for several policy objects
evaluate: add object to the cache
cache: move struct nft_cache declaration to cache.h
cache: add hashtable cache for table
evaluate: remove table_lookup_global()

 include/cache.h           |  25 ++++
 include/netlink.h         |   2 +
 include/nftables.h        |   8 +-
 include/rule.h            |  18 +--
 src/cache.c               | 244 ++++++++++++++++++++++++++++++++++++--
 src/evaluate.c            |  83 ++++++-------
 src/json.c                |  24 ++--
 src/libnftables.c         |   8 ++
 src/monitor.c             |  20 ++--
 src/netlink.c             |  23 +---
 src/netlink_delinearize.c |   4 +-
 src/rule.c                | 125 ++++++++-----------
 12 files changed, 399 insertions(+), 185 deletions(-)
Signature · 1 line
2.20.1
author maintainer
#1 lore raw

PATCH 1/10 cache: add hashtable cache for object

This patch adds a hashtable for object lookups.

This patch also splits table->objs in two:

- Sets that reside in the cache are stored in the new tables->cache_obj and tables->cache_obj_ht.

- Set that defined via command line / ruleset file reside in tables->obj.

Sets in the cache (already in the kernel) are not placed in the table->objs list.

By keeping separated lists, objs defined via command line / ruleset file can be added to cache.

---
 include/cache.h |   5 +++
 include/rule.h  |   7 ++--
 src/cache.c     | 101 +++++++++++++++++++++++++++++++++++++++++++++++-
 src/evaluate.c  |   2 +-
 src/json.c      |   4 +-
 src/monitor.c   |   6 +--
 src/netlink.c   |  19 ---------
 src/rule.c      |  43 +++++++++------------
 8 files changed, 133 insertions(+), 54 deletions(-)
include/cache.h +5 -0
index f500e1b19e45..b0458dc9c284 100644
@@ -62,4 +62,9 @@ struct chain *chain_cache_find(const struct table *table,
 void set_cache_add(struct set *set, struct table *table);
 struct set *set_cache_find(const struct table *table, const char *name);
 
+void obj_cache_add(struct obj *obj, struct table *table);
+void obj_cache_del(struct obj *obj);
+struct obj *obj_cache_find(const struct table *table, const char *name,
+			   uint32_t obj_type);
+
 #endif /* _NFT_CACHE_H_ */
include/rule.h +4 -3
index 90c01e9014c8..226171a8def7 100644
@@ -161,6 +161,8 @@ struct table {
 	struct list_head	*cache_set_ht;
 	struct list_head	cache_set;
 	struct list_head	sets;
+	struct list_head	*cache_obj_ht;
+	struct list_head	cache_obj;
 	struct list_head	objs;
 	struct list_head	flowtables;
 	struct list_head	chain_bindings;
@@ -488,6 +490,8 @@ struct secmark {
  */
 struct obj {
 	struct list_head		list;
+	struct list_head		cache_hlist;
+	struct list_head		cache_list;
 	struct location			location;
 	struct handle			handle;
 	uint32_t			type;
@@ -508,9 +512,6 @@ struct obj {
 struct obj *obj_alloc(const struct location *loc);
 extern struct obj *obj_get(struct obj *obj);
 void obj_free(struct obj *obj);
-void obj_add_hash(struct obj *obj, struct table *table);
-struct obj *obj_lookup(const struct table *table, const char *name,
-		       uint32_t type);
 struct obj *obj_lookup_fuzzy(const char *obj_name,
 			     const struct nft_cache *cache,
 			     const struct table **t);
src/cache.c +99 -2

123 lines not on this page · raw message

src/evaluate.c +1 -1
index 85cf9e05b641..d33a152c3de4 100644
@@ -4373,7 +4373,7 @@ static int cmd_evaluate_list_obj(struct eval_ctx *ctx, const struct cmd *cmd,
 	if (table == NULL)
 		return table_not_found(ctx);
 
-	if (obj_lookup(table, cmd->handle.obj.name, obj_type) == NULL)
+	if (obj_cache_find(table, cmd->handle.obj.name, obj_type) == NULL)
 		return obj_not_found(ctx, &cmd->handle.obj.location,
 				     cmd->handle.obj.name);
 
src/json.c +2 -2
index 52603a57de50..bf151e927661 100644
@@ -1570,7 +1570,7 @@ static json_t *table_print_json_full(struct netlink_ctx *ctx,
 	tmp = table_print_json(table);
 	json_array_append_new(root, tmp);
 
-	list_for_each_entry(obj, &table->objs, list) {
+	list_for_each_entry(obj, &table->cache_obj, cache_list) {
 		tmp = obj_print_json(obj);
 		json_array_append_new(root, tmp);
 	}
@@ -1740,7 +1740,7 @@ static json_t *do_list_obj_json(struct netlink_ctx *ctx,
 		    strcmp(cmd->handle.table.name, table->handle.table.name))
 			continue;
 
-		list_for_each_entry(obj, &table->objs, list) {
+		list_for_each_entry(obj, &table->cache_obj, cache_list) {
 			if (obj->type != type ||
 			    (cmd->handle.obj.name &&
 			     strcmp(cmd->handle.obj.name, obj->handle.obj.name)))
src/monitor.c +3 -3
index dc3f0848ba66..1f0f8a361fbd 100644
@@ -727,7 +727,7 @@ static void netlink_events_cache_addobj(struct netlink_mon_handler *monh,
 		goto out;
 	}
 
-	obj_add_hash(obj, t);
+	obj_cache_add(obj, t);
 out:
 	nftnl_obj_free(nlo);
 }
@@ -756,13 +756,13 @@ static void netlink_events_cache_delobj(struct netlink_mon_handler *monh,
 		goto out;
 	}
 
-	obj = obj_lookup(t, name, type);
+	obj = obj_cache_find(t, name, type);
 	if (obj == NULL) {
 		fprintf(stderr, "W: Unable to find object in cache\n");
 		goto out;
 	}
 
-	list_del(&obj->list);
+	obj_cache_del(obj);
 	obj_free(obj);
 out:
 	nftnl_obj_free(nlo);
src/netlink.c +0 -19
index e8b016096b67..2286a6ffabf8 100644
@@ -1517,25 +1517,6 @@ static int list_obj_cb(struct nftnl_obj *nls, void *arg)
 	return 0;
 }
 
-int netlink_list_objs(struct netlink_ctx *ctx, const struct handle *h)
-{
-	struct nftnl_obj_list *obj_cache;
-	int err;
-
-	obj_cache = mnl_nft_obj_dump(ctx, h->family,
-				     h->table.name, NULL, 0, true, false);
-	if (obj_cache == NULL) {
-		if (errno == EINTR)
-			return -1;
-
-		return 0;
-	}
-
-	err = nftnl_obj_list_foreach(obj_cache, list_obj_cb, ctx);
-	nftnl_obj_list_free(obj_cache);
-	return err;
-}
-
 int netlink_reset_objs(struct netlink_ctx *ctx, const struct cmd *cmd,
 		       uint32_t type, bool dump)
 {
src/rule.c +19 -24

101 lines not on this page · raw message

Signature · 1 line
2.20.1
author maintainer
#2 lore raw

PATCH 2/10 cache: add hashtable cache for flowtable

In reply to Pablo Neira Ayuso

Add flowtable hashtable cache.

Actually I am not expecting that many flowtables to benefit from the hashtable to be created by streamline this code with tables, chains, sets and policy objects.

---
 include/cache.h   |  4 +++
 include/netlink.h |  2 ++
 include/rule.h    |  6 ++--
 src/cache.c       | 90 +++++++++++++++++++++++++++++++++++++++++++++--
 src/evaluate.c    |  2 +-
 src/json.c        |  6 ++--
 src/netlink.c     |  2 +-
 src/rule.c        | 32 +++++++----------
 8 files changed, 116 insertions(+), 28 deletions(-)
include/cache.h +4 -0
index b0458dc9c284..6fa21742503c 100644
@@ -67,4 +67,8 @@ void obj_cache_del(struct obj *obj);
 struct obj *obj_cache_find(const struct table *table, const char *name,
 			   uint32_t obj_type);
 
+struct flowtable;
+void ft_cache_add(struct flowtable *ft, struct table *table);
+struct flowtable *ft_cache_find(const struct table *table, const char *name);
+
 #endif /* _NFT_CACHE_H_ */
include/netlink.h +2 -0
index f93c5322100f..a7c524ca9674 100644
@@ -163,6 +163,8 @@ extern struct obj *netlink_delinearize_obj(struct netlink_ctx *ctx,
 
 extern int netlink_list_flowtables(struct netlink_ctx *ctx,
 				   const struct handle *h);
+extern struct flowtable *netlink_delinearize_flowtable(struct netlink_ctx *ctx,
+						       struct nftnl_flowtable *nlo);
 
 extern void netlink_dump_chain(const struct nftnl_chain *nlc,
 			       struct netlink_ctx *ctx);
include/rule.h +4 -2
index 226171a8def7..44e057ab341f 100644
@@ -164,6 +164,8 @@ struct table {
 	struct list_head	*cache_obj_ht;
 	struct list_head	cache_obj;
 	struct list_head	objs;
+	struct list_head	*cache_ft_ht;
+	struct list_head	cache_ft;
 	struct list_head	flowtables;
 	struct list_head	chain_bindings;
 	enum table_flags 	flags;
@@ -522,6 +524,8 @@ uint32_t obj_type_to_cmd(uint32_t type);
 
 struct flowtable {
 	struct list_head	list;
+	struct list_head	cache_hlist;
+	struct list_head	cache_list;
 	struct handle		handle;
 	struct scope		scope;
 	struct location		location;
@@ -537,8 +541,6 @@ struct flowtable {
 extern struct flowtable *flowtable_alloc(const struct location *loc);
 extern struct flowtable *flowtable_get(struct flowtable *flowtable);
 extern void flowtable_free(struct flowtable *flowtable);
-extern void flowtable_add_hash(struct flowtable *flowtable, struct table *table);
-extern struct flowtable *flowtable_lookup(const struct table *table, const char *name);
 extern struct flowtable *flowtable_lookup_fuzzy(const char *ft_name,
 						const struct nft_cache *cache,
 						const struct table **table);
src/cache.c +88 -2

112 lines not on this page · raw message

src/evaluate.c +1 -1
index d33a152c3de4..a516a01ffc30 100644
@@ -4452,7 +4452,7 @@ static int cmd_evaluate_list(struct eval_ctx *ctx, struct cmd *cmd)
 		if (table == NULL)
 			return table_not_found(ctx);
 
-		ft = flowtable_lookup(table, cmd->handle.flowtable.name);
+		ft = ft_cache_find(table, cmd->handle.flowtable.name);
 		if (ft == NULL)
 			return flowtable_not_found(ctx, &ctx->cmd->handle.flowtable.location,
 						   ctx->cmd->handle.flowtable.name);
src/json.c +3 -3
index bf151e927661..29923092d12c 100644
@@ -1580,7 +1580,7 @@ static json_t *table_print_json_full(struct netlink_ctx *ctx,
 		tmp = set_print_json(&ctx->nft->output, set);
 		json_array_append_new(root, tmp);
 	}
-	list_for_each_entry(flowtable, &table->flowtables, list) {
+	list_for_each_entry(flowtable, &table->cache_ft, cache_list) {
 		tmp = flowtable_print_json(flowtable);
 		json_array_append_new(root, tmp);
 	}
@@ -1759,7 +1759,7 @@ static json_t *do_list_flowtable_json(struct netlink_ctx *ctx,
 	json_t *root = json_array();
 	struct flowtable *ft;
 
-	ft = flowtable_lookup(table, cmd->handle.flowtable.name);
+	ft = ft_cache_find(table, cmd->handle.flowtable.name);
 	if (ft == NULL)
 		return json_null();
 
@@ -1779,7 +1779,7 @@ static json_t *do_list_flowtables_json(struct netlink_ctx *ctx, struct cmd *cmd)
 		    cmd->handle.family != table->handle.family)
 			continue;
 
-		list_for_each_entry(flowtable, &table->flowtables, list) {
+		list_for_each_entry(flowtable, &table->cache_ft, cache_list) {
 			tmp = flowtable_print_json(flowtable);
 			json_array_append_new(root, tmp);
 		}
src/netlink.c +1 -1
index 2286a6ffabf8..5b2ad676f3e5 100644
@@ -1534,7 +1534,7 @@ int netlink_reset_objs(struct netlink_ctx *ctx, const struct cmd *cmd,
 	return err;
 }
 
-static struct flowtable *
+struct flowtable *
 netlink_delinearize_flowtable(struct netlink_ctx *ctx,
 			      struct nftnl_flowtable *nlo)
 {
src/rule.c +13 -19
index b78d4e7f9cd3..414e53e7d2f6 100644
@@ -1112,6 +1112,7 @@ struct table *table_alloc(void)
 	init_list_head(&table->objs);
 	init_list_head(&table->cache_obj);
 	init_list_head(&table->flowtables);
+	init_list_head(&table->cache_ft);
 	init_list_head(&table->chain_bindings);
 	init_list_head(&table->scope.symbols);
 	table->refcnt = 1;
@@ -1131,6 +1132,11 @@ struct table *table_alloc(void)
 	for (i = 0; i < NFT_CACHE_HSIZE; i++)
 		init_list_head(&table->cache_obj_ht[i]);
 
+	table->cache_ft_ht =
+		xmalloc(sizeof(struct list_head) * NFT_CACHE_HSIZE);
+	for (i = 0; i < NFT_CACHE_HSIZE; i++)
+		init_list_head(&table->cache_ft_ht[i]);
+
 	return table;
 }
 
@@ -1159,6 +1165,9 @@ void table_free(struct table *table)
 		set_free(set);
 	list_for_each_entry_safe(ft, nft, &table->flowtables, list)
 		flowtable_free(ft);
+	/* this is implicitly releasing flowtables in the hashtable cache */
+	list_for_each_entry_safe(ft, nft, &table->cache_ft, cache_list)
+		flowtable_free(ft);
 	list_for_each_entry_safe(obj, nobj, &table->objs, list)
 		obj_free(obj);
 	/* this is implicitly releasing objs in the hashtable cache */
@@ -1170,6 +1179,7 @@ void table_free(struct table *table)
 	xfree(table->cache_chain_ht);
 	xfree(table->cache_set_ht);
 	xfree(table->cache_obj_ht);
+	xfree(table->cache_ft_ht);
 	xfree(table);
 }
 
@@ -1287,7 +1297,7 @@ static void table_print(const struct table *table, struct output_ctx *octx)
 		set_print(set, octx);
 		delim = "\n";
 	}
-	list_for_each_entry(flowtable, &table->flowtables, list) {
+	list_for_each_entry(flowtable, &table->cache_ft, cache_list) {
 		nft_print(octx, "%s", delim);
 		flowtable_print(flowtable, octx);
 		delim = "\n";
@@ -2161,11 +2171,6 @@ void flowtable_free(struct flowtable *flowtable)
 	xfree(flowtable);
 }
 
-void flowtable_add_hash(struct flowtable *flowtable, struct table *table)
-{
-	list_add_tail(&flowtable->list, &table->flowtables);
-}
-
 static void flowtable_print_declaration(const struct flowtable *flowtable,
 					struct print_fmt_options *opts,
 					struct output_ctx *octx)
@@ -2231,17 +2236,6 @@ void flowtable_print(const struct flowtable *s, struct output_ctx *octx)
 	do_flowtable_print(s, &opts, octx);
 }
 
-struct flowtable *flowtable_lookup(const struct table *table, const char *name)
-{
-	struct flowtable *ft;
-
-	list_for_each_entry(ft, &table->flowtables, list) {
-		if (!strcmp(ft->handle.flowtable.name, name))
-			return ft;
-	}
-	return NULL;
-}
-
 struct flowtable *flowtable_lookup_fuzzy(const char *ft_name,
 					 const struct nft_cache *cache,
 					 const struct table **t)
@@ -2271,7 +2265,7 @@ static int do_list_flowtable(struct netlink_ctx *ctx, struct cmd *cmd,
 {
 	struct flowtable *ft;
 
-	ft = flowtable_lookup(table, cmd->handle.flowtable.name);
+	ft = ft_cache_find(table, cmd->handle.flowtable.name);
 	if (ft == NULL)
 		return -1;
 
@@ -2304,7 +2298,7 @@ static int do_list_flowtables(struct netlink_ctx *ctx, struct cmd *cmd)
 			  family2str(table->handle.family),
 			  table->handle.table.name);
 
-		list_for_each_entry(flowtable, &table->flowtables, list) {
+		list_for_each_entry(flowtable, &table->cache_ft, cache_list) {
 			flowtable_print_declaration(flowtable, &opts, &ctx->nft->output);
 			nft_print(&ctx->nft->output, "%s}%s", opts.tab, opts.nl);
 		}
Signature · 1 line
2.20.1
author maintainer
#3 lore raw

PATCH 3/10 cache: add set_cache_del() and use it

In reply to Pablo Neira Ayuso

Update set_cache_del() from the monitor path to remove sets in the cache.

Fixes df48e56e987f ("cache: add hashtable cache for sets")
---
 include/cache.h | 1 +
 src/cache.c     | 6 ++++++
 src/monitor.c   | 2 +-
 3 files changed, 8 insertions(+), 1 deletion(-)
include/cache.h +1 -0
index 6fa21742503c..d3be4c8a8693 100644
@@ -60,6 +60,7 @@ void chain_cache_add(struct chain *chain, struct table *table);
 struct chain *chain_cache_find(const struct table *table,
 			       const struct handle *handle);
 void set_cache_add(struct set *set, struct table *table);
+void set_cache_del(struct set *set);
 struct set *set_cache_find(const struct table *table, const char *name);
 
 void obj_cache_add(struct obj *obj, struct table *table);
src/cache.c +6 -0
index 95b5c46306c3..73c96a17704a 100644
@@ -324,6 +324,12 @@ void set_cache_add(struct set *set, struct table *table)
 	list_add_tail(&set->cache_list, &table->cache_set);
 }
 
+void set_cache_del(struct set *set)
+{
+	list_del(&set->cache_hlist);
+	list_del(&set->cache_list);
+}
+
 struct set *set_cache_find(const struct table *table, const char *name)
 {
 	struct set *set;
src/monitor.c +1 -1
index 1f0f8a361fbd..eb887d9344fa 100644
@@ -687,7 +687,7 @@ out:
 static void netlink_events_cache_delset_cb(struct set *s,
 					   void *data)
 {
-	list_del(&s->list);
+	set_cache_del(s);
 	set_free(s);
 }
 
Signature · 1 line
2.20.1
author maintainer
#4 lore raw

PATCH 4/10 evaluate: add set to the cache

In reply to Pablo Neira Ayuso

If the cache does not contain the set that is defined in this batch, add it to the cache. This allows for references to this new set in the same batch.

---
 src/evaluate.c | 4 ++++
 1 file changed, 4 insertions(+)
src/evaluate.c +4 -0
index a516a01ffc30..d0dfbabdf538 100644
@@ -3718,6 +3718,10 @@ static int set_evaluate(struct eval_ctx *ctx, struct set *set)
 	if (table == NULL)
 		return table_not_found(ctx);
 
+	if (!(set->flags & NFT_SET_ANONYMOUS) &&
+	    !set_cache_find(table, set->handle.set.name))
+		set_cache_add(set_get(set), table);
+
 	if (!(set->flags & NFT_SET_INTERVAL) && set->automerge)
 		return set_error(ctx, set, "auto-merge only works with interval sets");
 
Signature · 1 line
2.20.1
author maintainer
#5 lore raw

PATCH 5/10 evaluate: add flowtable to the cache

In reply to Pablo Neira Ayuso

If the cache does not contain this flowtable that is defined in this batch, then add it to the cache. This allows for references to this new flowtable in the same batch.

---
 src/evaluate.c | 3 +++
 src/rule.c     | 2 +-
 2 files changed, 4 insertions(+), 1 deletion(-)
src/evaluate.c +3 -0
index d0dfbabdf538..7b2d01c5dee1 100644
@@ -3925,6 +3925,9 @@ static int flowtable_evaluate(struct eval_ctx *ctx, struct flowtable *ft)
 	if (table == NULL)
 		return table_not_found(ctx);
 
+	if (ft_cache_find(table, ft->handle.flowtable.name) == NULL)
+		ft_cache_add(flowtable_get(ft), table);
+
 	if (ft->hook.name) {
 		ft->hook.num = str2hooknum(NFPROTO_NETDEV, ft->hook.name);
 		if (ft->hook.num == NF_INET_NUMHOOKS)
src/rule.c +1 -1
index 414e53e7d2f6..a6909fc75060 100644
@@ -2247,7 +2247,7 @@ struct flowtable *flowtable_lookup_fuzzy(const char *ft_name,
 	string_misspell_init(&st);
 
 	list_for_each_entry(table, &cache->list, list) {
-		list_for_each_entry(ft, &table->flowtables, list) {
+		list_for_each_entry(ft, &table->cache_ft, cache_list) {
 			if (!strcmp(ft->handle.flowtable.name, ft_name)) {
 				*t = table;
 				return ft;
Signature · 1 line
2.20.1
author maintainer
#6 lore raw

PATCH 6/10 cache: missing table cache for several policy objects

In reply to Pablo Neira Ayuso

Populate the cache with tables for several policy objects types.

---
 src/cache.c | 4 ++++
 1 file changed, 4 insertions(+)
src/cache.c +4 -0
index 73c96a17704a..8590e14cfa33 100644
@@ -26,6 +26,10 @@ static unsigned int evaluate_cache_add(struct cmd *cmd, unsigned int flags)
 	case CMD_OBJ_QUOTA:
 	case CMD_OBJ_LIMIT:
 	case CMD_OBJ_SECMARK:
+	case CMD_OBJ_CT_HELPER:
+	case CMD_OBJ_CT_TIMEOUT:
+	case CMD_OBJ_CT_EXPECT:
+	case CMD_OBJ_SYNPROXY:
 	case CMD_OBJ_FLOWTABLE:
 		flags |= NFT_CACHE_TABLE;
 		break;
Signature · 1 line
2.20.1
author maintainer
#7 lore raw

PATCH 7/10 evaluate: add object to the cache

In reply to Pablo Neira Ayuso

If the cache does not contain this object that is defined in this batch, add it to the cache. This allows for references to this new object in the same batch.

This patch also adds missing handle_merge() to set the object name, otherwise object name is NULL and obj_cache_find() crashes.

---
 src/evaluate.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
src/evaluate.c +10 -0
index 7b2d01c5dee1..72cf756bbb5c 100644
@@ -4210,6 +4210,15 @@ static int ct_timeout_evaluate(struct eval_ctx *ctx, struct obj *obj)
 
 static int obj_evaluate(struct eval_ctx *ctx, struct obj *obj)
 {
+	struct table *table;
+
+	table = table_lookup_global(ctx);
+	if (table == NULL)
+		return table_not_found(ctx);
+
+	if (obj_cache_find(table, obj->handle.obj.name, obj->type) == NULL)
+		obj_cache_add(obj_get(obj), table);
+
 	switch (obj->type) {
 	case NFT_OBJECT_CT_TIMEOUT:
 		return ct_timeout_evaluate(ctx, obj);
@@ -4296,6 +4305,7 @@ static int cmd_evaluate_add(struct eval_ctx *ctx, struct cmd *cmd)
 	case CMD_OBJ_SECMARK:
 	case CMD_OBJ_CT_EXPECT:
 	case CMD_OBJ_SYNPROXY:
+		handle_merge(&cmd->object->handle, &cmd->handle);
 		return obj_evaluate(ctx, cmd->object);
 	default:
 		BUG("invalid command object type %u\n", cmd->obj);
Signature · 1 line
2.20.1
author maintainer
#8 lore raw

PATCH 8/10 cache: move struct nft_cache declaration to cache.h

In reply to Pablo Neira Ayuso

Move struct nft_cache declaration to include/cache.h.

---
 include/cache.h    | 9 +++++++++
 include/nftables.h | 8 +-------
 2 files changed, 10 insertions(+), 7 deletions(-)
include/cache.h +9 -0
index d3be4c8a8693..cab8a6bcca05 100644
@@ -1,6 +1,15 @@
 #ifndef _NFT_CACHE_H_
 #define _NFT_CACHE_H_
 
+#include <string.h>
+
+struct nft_cache {
+	uint32_t		genid;
+	struct list_head	list;
+	uint32_t		seqnum;
+	uint32_t		flags;
+};
+
 enum cache_level_bits {
 	NFT_CACHE_TABLE_BIT	= (1 << 0),
 	NFT_CACHE_CHAIN_BIT	= (1 << 1),
include/nftables.h +1 -7
index 9095ff3d0b79..f239fcf0e1f4 100644
@@ -5,6 +5,7 @@
 #include <stdarg.h>
 #include <limits.h>
 #include <utils.h>
+#include <cache.h>
 #include <nftables/libnftables.h>
 
 struct cookie {
@@ -95,13 +96,6 @@ static inline bool nft_output_terse(const struct output_ctx *octx)
 	return octx->flags & NFT_CTX_OUTPUT_TERSE;
 }
 
-struct nft_cache {
-	uint32_t		genid;
-	struct list_head	list;
-	uint32_t		seqnum;
-	uint32_t		flags;
-};
-
 struct mnl_socket;
 struct parser_state;
 struct scope;
Signature · 1 line
2.20.1
author maintainer
#9 lore raw

PATCH 9/10 cache: add hashtable cache for table

In reply to Pablo Neira Ayuso

Add a hashtable for fast table lookups.

Tables that reside in the cache use the table->cache_hlist and table->cache_list heads.

Table that are created from command line / ruleset are also added to the cache.

---
 include/cache.h           |  8 ++++++-
 include/rule.h            |  5 ++--
 src/cache.c               | 43 +++++++++++++++++++++++++++++++----
 src/evaluate.c            | 40 ++++++++++++++++----------------
 src/json.c                | 14 ++++++------
 src/libnftables.c         |  8 +++++++
 src/monitor.c             | 12 +++++-----
 src/netlink.c             |  2 +-
 src/netlink_delinearize.c |  4 ++--
 src/rule.c                | 48 ++++++++++++---------------------------
 10 files changed, 107 insertions(+), 77 deletions(-)
include/cache.h +7 -1
index cab8a6bcca05..de27697072a1 100644
@@ -5,7 +5,8 @@
 
 struct nft_cache {
 	uint32_t		genid;
-	struct list_head	list;
+	struct list_head        *ht;
+	struct list_head        list;
 	uint32_t		seqnum;
 	uint32_t		flags;
 };
@@ -65,6 +66,11 @@ int cache_init(struct netlink_ctx *ctx, unsigned int flags);
 int cache_update(struct nft_ctx *nft, unsigned int flags, struct list_head *msgs);
 void cache_release(struct nft_cache *cache);
 
+void table_cache_add(struct table *table, struct nft_cache *cache);
+void table_cache_del(struct table *table);
+struct table *table_cache_find(const struct handle *h,
+			       const struct nft_cache *cache);
+
 void chain_cache_add(struct chain *chain, struct table *table);
 struct chain *chain_cache_find(const struct table *table,
 			       const struct handle *handle);
include/rule.h +2 -3
index 44e057ab341f..a39788284bc1 100644
@@ -152,6 +152,8 @@ const char *table_flag_name(uint32_t flag);
  */
 struct table {
 	struct list_head	list;
+	struct list_head	cache_hlist;
+	struct list_head	cache_list;
 	struct handle		handle;
 	struct location		location;
 	struct scope		scope;
@@ -177,9 +179,6 @@ struct table {
 extern struct table *table_alloc(void);
 extern struct table *table_get(struct table *table);
 extern void table_free(struct table *table);
-extern void table_add_hash(struct table *table, struct nft_cache *cache);
-extern struct table *table_lookup(const struct handle *h,
-				  const struct nft_cache *cache);
 extern struct table *table_lookup_fuzzy(const struct handle *h,
 					const struct nft_cache *cache);
 
src/cache.c +39 -4

73 lines not on this page · raw message

src/evaluate.c +20 -20
index 72cf756bbb5c..c33e7268d655 100644
@@ -173,7 +173,7 @@ static struct table *table_lookup_global(struct eval_ctx *ctx)
 	if (ctx->table != NULL)
 		return ctx->table;
 
-	table = table_lookup(&ctx->cmd->handle, &ctx->nft->cache);
+	table = table_cache_find(&ctx->cmd->handle, &ctx->nft->cache);
 	if (table == NULL)
 		return NULL;
 
@@ -3969,7 +3969,7 @@ static int rule_cache_update(struct eval_ctx *ctx, enum cmd_ops op)
 	struct table *table;
 	struct chain *chain;
 
-	table = table_lookup(&rule->handle, &ctx->nft->cache);
+	table = table_cache_find(&rule->handle, &ctx->nft->cache);
 	if (!table)
 		return table_not_found(ctx);
 
@@ -4238,13 +4238,13 @@ static int table_evaluate(struct eval_ctx *ctx, struct table *table)
 	struct set *set;
 	struct obj *obj;
 
-	if (table_lookup(&ctx->cmd->handle, &ctx->nft->cache) == NULL) {
+	if (table_cache_find(&ctx->cmd->handle, &ctx->nft->cache) == NULL) {
 		if (table == NULL) {
 			table = table_alloc();
 			handle_merge(&table->handle, &ctx->cmd->handle);
-			table_add_hash(table, &ctx->nft->cache);
+			table_cache_add(table, &ctx->nft->cache);
 		} else {
-			table_add_hash(table_get(table), &ctx->nft->cache);
+			table_cache_add(table_get(table), &ctx->nft->cache);
 		}
 	}
 
@@ -4316,11 +4316,11 @@ static void table_del_cache(struct eval_ctx *ctx, struct cmd *cmd)
 {
 	struct table *table;
 
-	table = table_lookup(&cmd->handle, &ctx->nft->cache);
+	table = table_cache_find(&cmd->handle, &ctx->nft->cache);
 	if (!table)
 		return;
 
-	list_del(&table->list);
+	table_cache_del(table);
 	table_free(table);
 }
 
@@ -4386,7 +4386,7 @@ static int cmd_evaluate_list_obj(struct eval_ctx *ctx, const struct cmd *cmd,
 	if (obj_type == NFT_OBJECT_UNSPEC)
 		obj_type = NFT_OBJECT_COUNTER;
 
-	table = table_lookup(&cmd->handle, &ctx->nft->cache);
+	table = table_cache_find(&cmd->handle, &ctx->nft->cache);
 	if (table == NULL)
 		return table_not_found(ctx);
 
@@ -4408,13 +4408,13 @@ static int cmd_evaluate_list(struct eval_ctx *ctx, struct cmd *cmd)
 		if (cmd->handle.table.name == NULL)
 			return 0;
 
-		table = table_lookup(&cmd->handle, &ctx->nft->cache);
+		table = table_cache_find(&cmd->handle, &ctx->nft->cache);
 		if (table == NULL)
 			return table_not_found(ctx);
 
 		return 0;
 	case CMD_OBJ_SET:
-		table = table_lookup(&cmd->handle, &ctx->nft->cache);
+		table = table_cache_find(&cmd->handle, &ctx->nft->cache);
 		if (table == NULL)
 			return table_not_found(ctx);
 
@@ -4428,7 +4428,7 @@ static int cmd_evaluate_list(struct eval_ctx *ctx, struct cmd *cmd)
 
 		return 0;
 	case CMD_OBJ_METER:
-		table = table_lookup(&cmd->handle, &ctx->nft->cache);
+		table = table_cache_find(&cmd->handle, &ctx->nft->cache);
 		if (table == NULL)
 			return table_not_found(ctx);
 
@@ -4442,7 +4442,7 @@ static int cmd_evaluate_list(struct eval_ctx *ctx, struct cmd *cmd)
 
 		return 0;
 	case CMD_OBJ_MAP:
-		table = table_lookup(&cmd->handle, &ctx->nft->cache);
+		table = table_cache_find(&cmd->handle, &ctx->nft->cache);
 		if (table == NULL)
 			return table_not_found(ctx);
 
@@ -4456,7 +4456,7 @@ static int cmd_evaluate_list(struct eval_ctx *ctx, struct cmd *cmd)
 
 		return 0;
 	case CMD_OBJ_CHAIN:
-		table = table_lookup(&cmd->handle, &ctx->nft->cache);
+		table = table_cache_find(&cmd->handle, &ctx->nft->cache);
 		if (table == NULL)
 			return table_not_found(ctx);
 
@@ -4465,7 +4465,7 @@ static int cmd_evaluate_list(struct eval_ctx *ctx, struct cmd *cmd)
 
 		return 0;
 	case CMD_OBJ_FLOWTABLE:
-		table = table_lookup(&cmd->handle, &ctx->nft->cache);
+		table = table_cache_find(&cmd->handle, &ctx->nft->cache);
 		if (table == NULL)
 			return table_not_found(ctx);
 
@@ -4501,7 +4501,7 @@ static int cmd_evaluate_list(struct eval_ctx *ctx, struct cmd *cmd)
 	case CMD_OBJ_SYNPROXYS:
 		if (cmd->handle.table.name == NULL)
 			return 0;
-		if (table_lookup(&cmd->handle, &ctx->nft->cache) == NULL)
+		if (table_cache_find(&cmd->handle, &ctx->nft->cache) == NULL)
 			return table_not_found(ctx);
 
 		return 0;
@@ -4524,7 +4524,7 @@ static int cmd_evaluate_reset(struct eval_ctx *ctx, struct cmd *cmd)
 	case CMD_OBJ_QUOTAS:
 		if (cmd->handle.table.name == NULL)
 			return 0;
-		if (table_lookup(&cmd->handle, &ctx->nft->cache) == NULL)
+		if (table_cache_find(&cmd->handle, &ctx->nft->cache) == NULL)
 			return table_not_found(ctx);
 
 		return 0;
@@ -4557,7 +4557,7 @@ static int cmd_evaluate_flush(struct eval_ctx *ctx, struct cmd *cmd)
 		/* Chains don't hold sets */
 		break;
 	case CMD_OBJ_SET:
-		table = table_lookup(&cmd->handle, &ctx->nft->cache);
+		table = table_cache_find(&cmd->handle, &ctx->nft->cache);
 		if (table == NULL)
 			return table_not_found(ctx);
 
@@ -4573,7 +4573,7 @@ static int cmd_evaluate_flush(struct eval_ctx *ctx, struct cmd *cmd)
 
 		return 0;
 	case CMD_OBJ_MAP:
-		table = table_lookup(&cmd->handle, &ctx->nft->cache);
+		table = table_cache_find(&cmd->handle, &ctx->nft->cache);
 		if (table == NULL)
 			return table_not_found(ctx);
 
@@ -4589,7 +4589,7 @@ static int cmd_evaluate_flush(struct eval_ctx *ctx, struct cmd *cmd)
 
 		return 0;
 	case CMD_OBJ_METER:
-		table = table_lookup(&cmd->handle, &ctx->nft->cache);
+		table = table_cache_find(&cmd->handle, &ctx->nft->cache);
 		if (table == NULL)
 			return table_not_found(ctx);
 
@@ -4616,7 +4616,7 @@ static int cmd_evaluate_rename(struct eval_ctx *ctx, struct cmd *cmd)
 
 	switch (cmd->obj) {
 	case CMD_OBJ_CHAIN:
-		table = table_lookup(&ctx->cmd->handle, &ctx->nft->cache);
+		table = table_cache_find(&ctx->cmd->handle, &ctx->nft->cache);
 		if (table == NULL)
 			return table_not_found(ctx);
 
src/json.c +7 -7
index 29923092d12c..744efc49d04e 100644
@@ -1603,7 +1603,7 @@ static json_t *do_list_ruleset_json(struct netlink_ctx *ctx, struct cmd *cmd)
 	json_t *root = json_array(), *tmp;
 	struct table *table;
 
-	list_for_each_entry(table, &ctx->nft->cache.list, list) {
+	list_for_each_entry(table, &ctx->nft->cache.list, cache_list) {
 		if (family != NFPROTO_UNSPEC &&
 		    table->handle.family != family)
 			continue;
@@ -1622,7 +1622,7 @@ static json_t *do_list_tables_json(struct netlink_ctx *ctx, struct cmd *cmd)
 	json_t *root = json_array();
 	struct table *table;
 
-	list_for_each_entry(table, &ctx->nft->cache.list, list) {
+	list_for_each_entry(table, &ctx->nft->cache.list, cache_list) {
 		if (family != NFPROTO_UNSPEC &&
 		    table->handle.family != family)
 			continue;
@@ -1669,7 +1669,7 @@ static json_t *do_list_chains_json(struct netlink_ctx *ctx, struct cmd *cmd)
 	struct table *table;
 	struct chain *chain;
 
-	list_for_each_entry(table, &ctx->nft->cache.list, list) {
+	list_for_each_entry(table, &ctx->nft->cache.list, cache_list) {
 		if (cmd->handle.family != NFPROTO_UNSPEC &&
 		    cmd->handle.family != table->handle.family)
 			continue;
@@ -1702,7 +1702,7 @@ static json_t *do_list_sets_json(struct netlink_ctx *ctx, struct cmd *cmd)
 	struct table *table;
 	struct set *set;
 
-	list_for_each_entry(table, &ctx->nft->cache.list, list) {
+	list_for_each_entry(table, &ctx->nft->cache.list, cache_list) {
 		if (cmd->handle.family != NFPROTO_UNSPEC &&
 		    cmd->handle.family != table->handle.family)
 			continue;
@@ -1731,7 +1731,7 @@ static json_t *do_list_obj_json(struct netlink_ctx *ctx,
 	struct table *table;
 	struct obj *obj;
 
-	list_for_each_entry(table, &ctx->nft->cache.list, list) {
+	list_for_each_entry(table, &ctx->nft->cache.list, cache_list) {
 		if (cmd->handle.family != NFPROTO_UNSPEC &&
 		    cmd->handle.family != table->handle.family)
 			continue;
@@ -1774,7 +1774,7 @@ static json_t *do_list_flowtables_json(struct netlink_ctx *ctx, struct cmd *cmd)
 	struct flowtable *flowtable;
 	struct table *table;
 
-	list_for_each_entry(table, &ctx->nft->cache.list, list) {
+	list_for_each_entry(table, &ctx->nft->cache.list, cache_list) {
 		if (cmd->handle.family != NFPROTO_UNSPEC &&
 		    cmd->handle.family != table->handle.family)
 			continue;
@@ -1802,7 +1802,7 @@ int do_command_list_json(struct netlink_ctx *ctx, struct cmd *cmd)
 	json_t *root;
 
 	if (cmd->handle.table.name)
-		table = table_lookup(&cmd->handle, &ctx->nft->cache);
+		table = table_cache_find(&cmd->handle, &ctx->nft->cache);
 
 	switch (cmd->obj) {
 	case CMD_OBJ_TABLE:
src/libnftables.c +8 -0
index 044365914747..8c1e4c34f682 100644
@@ -96,15 +96,23 @@ out:
 
 static void nft_init(struct nft_ctx *ctx)
 {
+	int i;
+
 	mark_table_init(ctx);
 	realm_table_rt_init(ctx);
 	devgroup_table_init(ctx);
 	ct_label_table_init(ctx);
 	expr_handler_init();
+
+	init_list_head(&ctx->cache.list);
+	ctx->cache.ht = xmalloc(sizeof(struct list_head) * NFT_CACHE_HSIZE);
+	for (i = 0; i < NFT_CACHE_HSIZE; i++)
+		init_list_head(&ctx->cache.ht[i]);
 }
 
 static void nft_exit(struct nft_ctx *ctx)
 {
+	xfree(ctx->cache.ht);
 	expr_handler_exit();
 	ct_label_table_exit(ctx);
 	realm_table_rt_exit(ctx);
src/monitor.c +6 -6
index eb887d9344fa..5745633610d2 100644
@@ -575,7 +575,7 @@ static void netlink_events_cache_addtable(struct netlink_mon_handler *monh,
 	t = netlink_delinearize_table(monh->ctx, nlt);
 	nftnl_table_free(nlt);
 
-	table_add_hash(t, &monh->ctx->nft->cache);
+	table_cache_add(t, &monh->ctx->nft->cache);
 }
 
 static void netlink_events_cache_deltable(struct netlink_mon_handler *monh,
@@ -589,11 +589,11 @@ static void netlink_events_cache_deltable(struct netlink_mon_handler *monh,
 	h.family = nftnl_table_get_u32(nlt, NFTNL_TABLE_FAMILY);
 	h.table.name  = nftnl_table_get_str(nlt, NFTNL_TABLE_NAME);
 
-	t = table_lookup(&h, &monh->ctx->nft->cache);
+	t = table_cache_find(&h, &monh->ctx->nft->cache);
 	if (t == NULL)
 		goto out;
 
-	list_del(&t->list);
+	table_cache_del(t);
 	table_free(t);
 out:
 	nftnl_table_free(nlt);
@@ -619,7 +619,7 @@ static void netlink_events_cache_addset(struct netlink_mon_handler *monh,
 		goto out;
 	s->init = set_expr_alloc(monh->loc, s);
 
-	t = table_lookup(&s->handle, &monh->ctx->nft->cache);
+	t = table_cache_find(&s->handle, &monh->ctx->nft->cache);
 	if (t == NULL) {
 		fprintf(stderr, "W: Unable to cache set: table not found.\n");
 		set_free(s);
@@ -720,7 +720,7 @@ static void netlink_events_cache_addobj(struct netlink_mon_handler *monh,
 	if (obj == NULL)
 		goto out;
 
-	t = table_lookup(&obj->handle, &monh->ctx->nft->cache);
+	t = table_cache_find(&obj->handle, &monh->ctx->nft->cache);
 	if (t == NULL) {
 		fprintf(stderr, "W: Unable to cache object: table not found.\n");
 		obj_free(obj);
@@ -750,7 +750,7 @@ static void netlink_events_cache_delobj(struct netlink_mon_handler *monh,
 	type	 = nftnl_obj_get_u32(nlo, NFTNL_OBJ_TYPE);
 	h.handle.id	= nftnl_obj_get_u64(nlo, NFTNL_OBJ_HANDLE);
 
-	t = table_lookup(&h, &monh->ctx->nft->cache);
+	t = table_cache_find(&h, &monh->ctx->nft->cache);
 	if (t == NULL) {
 		fprintf(stderr, "W: Unable to cache object: table not found.\n");
 		goto out;
src/netlink.c +1 -1
index 5b2ad676f3e5..3ed49c10bbee 100644
@@ -1695,7 +1695,7 @@ static struct rule *trace_lookup_rule(const struct nftnl_trace *nlt,
 	if (!h.table.name)
 		return NULL;
 
-	table = table_lookup(&h, cache);
+	table = table_cache_find(&h, cache);
 	if (!table)
 		return NULL;
 
src/netlink_delinearize.c +2 -2
index 710c668a0258..9e282a61708c 100644
@@ -1792,7 +1792,7 @@ struct stmt *netlink_parse_set_expr(const struct set *set,
 
 	handle_merge(&h, &set->handle);
 	pctx->rule = rule_alloc(&netlink_location, &h);
-	pctx->table = table_lookup(&set->handle, cache);
+	pctx->table = table_cache_find(&set->handle, cache);
 	assert(pctx->table != NULL);
 
 	if (netlink_parse_expr(nle, pctx) < 0)
@@ -2938,7 +2938,7 @@ struct rule *netlink_delinearize_rule(struct netlink_ctx *ctx,
 		h.position.id = nftnl_rule_get_u64(nlr, NFTNL_RULE_POSITION);
 
 	pctx->rule = rule_alloc(&netlink_location, &h);
-	pctx->table = table_lookup(&h, &ctx->nft->cache);
+	pctx->table = table_cache_find(&h, &ctx->nft->cache);
 	assert(pctx->table != NULL);
 
 	pctx->rule->comment = nftnl_rule_get_comment(nlr);
src/rule.c +15 -33

144 lines not on this page · raw message

Signature · 1 line
2.20.1
author maintainer
#10 lore raw

PATCH 10/10 evaluate: remove table_lookup_global()

In reply to Pablo Neira Ayuso

No need to check for ctx->table, use the existing table in the cache.

---
 src/evaluate.c | 26 ++++++--------------------
 1 file changed, 6 insertions(+), 20 deletions(-)
src/evaluate.c +6 -20
index c33e7268d655..ca13ad9e25e1 100644
@@ -166,20 +166,6 @@ static int byteorder_conversion(struct eval_ctx *ctx, struct expr **expr,
 	return 0;
 }
 
-static struct table *table_lookup_global(struct eval_ctx *ctx)
-{
-	struct table *table;
-
-	if (ctx->table != NULL)
-		return ctx->table;
-
-	table = table_cache_find(&ctx->cmd->handle, &ctx->nft->cache);
-	if (table == NULL)
-		return NULL;
-
-	return table;
-}
-
 static int table_not_found(struct eval_ctx *ctx)
 {
 	struct table *table;
@@ -269,7 +255,7 @@ static int expr_evaluate_symbol(struct eval_ctx *ctx, struct expr **expr)
 		}
 		break;
 	case SYMBOL_SET:
-		table = table_lookup_global(ctx);
+		table = table_cache_find(&ctx->cmd->handle, &ctx->nft->cache);
 		if (table == NULL)
 			return table_not_found(ctx);
 
@@ -3673,7 +3659,7 @@ static int setelem_evaluate(struct eval_ctx *ctx, struct cmd *cmd)
 	struct table *table;
 	struct set *set;
 
-	table = table_lookup_global(ctx);
+	table = table_cache_find(&ctx->cmd->handle, &ctx->nft->cache);
 	if (table == NULL)
 		return table_not_found(ctx);
 
@@ -3714,7 +3700,7 @@ static int set_evaluate(struct eval_ctx *ctx, struct set *set)
 	struct stmt *stmt;
 	const char *type;
 
-	table = table_lookup_global(ctx);
+	table = table_cache_find(&ctx->cmd->handle, &ctx->nft->cache);
 	if (table == NULL)
 		return table_not_found(ctx);
 
@@ -3921,7 +3907,7 @@ static int flowtable_evaluate(struct eval_ctx *ctx, struct flowtable *ft)
 {
 	struct table *table;
 
-	table = table_lookup_global(ctx);
+	table = table_cache_find(&ctx->cmd->handle, &ctx->nft->cache);
 	if (table == NULL)
 		return table_not_found(ctx);
 
@@ -4111,7 +4097,7 @@ static int chain_evaluate(struct eval_ctx *ctx, struct chain *chain)
 	struct table *table;
 	struct rule *rule;
 
-	table = table_lookup_global(ctx);
+	table = table_cache_find(&ctx->cmd->handle, &ctx->nft->cache);
 	if (table == NULL)
 		return table_not_found(ctx);
 
@@ -4212,7 +4198,7 @@ static int obj_evaluate(struct eval_ctx *ctx, struct obj *obj)
 {
 	struct table *table;
 
-	table = table_lookup_global(ctx);
+	table = table_cache_find(&ctx->cmd->handle, &ctx->nft->cache);
 	if (table == NULL)
 		return table_not_found(ctx);
 
Signature · 1 line
2.20.1