Skip to contentSkip to conversation

src: sort set elements in netlink_get_setelems()

PATCH Applied Elise Lennion 2 participants 2 messages Jan 6, 2017 – Jan 10, 2017 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 src: sort set elements in netlink_get_setelems()

So users can better track their ruleset via git.

Without sorting, the elements can be listed in a different order every time the set is created, generating unnecessary git changes.

Mergesort is used. Doesn't sort sets with 'flags interval' set on.

---
 include/expression.h |   1 +
 src/Makefile.am      |   1 +
 src/mergesort.c      | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/netlink.c        |   4 +++
 4 files changed, 106 insertions(+)
 create mode 100644 src/mergesort.c
include/expression.h +1 -0
index 71e9c43..ec90265 100644
@@ -396,6 +396,7 @@ extern struct expr *range_expr_alloc(const struct location *loc,
 
 extern void compound_expr_add(struct expr *compound, struct expr *expr);
 extern void compound_expr_remove(struct expr *compound, struct expr *expr);
+extern void list_expr_sort(struct list_head *head);
 
 extern struct expr *concat_expr_alloc(const struct location *loc);
 
src/Makefile.am +1 -0
index 2a69e19..65cb4b4 100644
@@ -53,6 +53,7 @@ nft_SOURCES =	main.c				\
 		mnl.c				\
 		iface.c				\
 		services.c			\
+		mergesort.c			\
 		scanner.l			\
 		parser_bison.y
 
src/mergesort.c new file +100 -0

100 lines not on this page · raw message

src/netlink.c +4 -0
index 5f478ff..4135f25 100644
@@ -1666,6 +1666,10 @@ int netlink_get_setelems(struct netlink_ctx *ctx, const struct handle *h,
 	ctx->set = set;
 	set->init = set_expr_alloc(loc);
 	nftnl_set_elem_foreach(nls, list_setelem_cb, ctx);
+
+	if (!(set->flags & NFT_SET_INTERVAL))
+		list_expr_sort(&ctx->set->init->expressions);
+
 	nftnl_set_free(nls);
 	ctx->set = NULL;
 
Signature · 1 line
2.7.4
maintainer Applied
#1 lore raw
Quoted 6 lines · Elise Lennion

So users can better track their ruleset via git.

Without sorting, the elements can be listed in a different order every time the set is created, generating unnecessary git changes.

Mergesort is used. Doesn't sort sets with 'flags interval' set on.

Applied, thanks Elise! I'm appending this to your description BTW:

Currently these interval set elements are dumped in order. We'll likely get new representations soon that may not guarantee this anymore, so let's revisit this later in case we need it.

Without this patch, nft list ruleset with a set containing 40000 elements takes on my laptop:

        real    0m2.742s
        user    0m0.112s
        sys     0m0.280s

With this patch:

        real    0m2.846s
        user    0m0.180s
        sys     0m0.284s

Difference is small, so don't get nft more complicated with yet another getopt() option, enable this by default.