Skip to contentSkip to conversation

Introduce struct nftnl_str_array

PATCH Superseded Phil Sutter 2 participants 3 messages Oct 23, 2024 – Oct 29, 2024 netfilter

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH v2 1/1 Introduce struct nftnl_str_array

This data structure holds an array of allocated strings for use in nftnl_chain and nftnl_flowtable structs. For convenience, implement functions to clear, populate and iterate over contents.

While at it, extend chain and flowtable tests to cover these attributes, too.

---
Changes since v1:
- Add missing src/str_array.c

---
 include/internal.h         |  1 +
 src/Makefile.am            |  1 +
 src/chain.c                | 90 ++++++------------------------------
 src/flowtable.c            | 94 ++++++--------------------------------
 src/str_array.c            | 69 ++++++++++++++++++++++++++++
 src/utils.c                |  1 +
 tests/nft-chain-test.c     | 37 ++++++++++++++-
 tests/nft-flowtable-test.c | 21 +++++++++
 8 files changed, 155 insertions(+), 159 deletions(-)
 create mode 100644 src/str_array.c
include/internal.h +1 -0
index 1f96731589c04..b8fc7f129c76e 100644
@@ -12,5 +12,6 @@
 #include "expr.h"
 #include "expr_ops.h"
 #include "rule.h"
+#include "str_array.h"
 
 #endif /* _LIBNFTNL_INTERNAL_H_ */
src/Makefile.am +1 -0
index 3cd259c04d1c3..1c38d00c4e180 100644
@@ -17,6 +17,7 @@ libnftnl_la_SOURCES = utils.c		\
 		      rule.c		\
 		      set.c		\
 		      set_elem.c	\
+		      str_array.c	\
 		      ruleset.c		\
 		      udata.c		\
 		      expr.c		\
src/chain.c +13 -77

172 lines not on this page · raw message

src/flowtable.c +14 -80

174 lines not on this page · raw message

src/str_array.c new file +69 -0

69 lines not on this page · raw message

src/utils.c +1 -0
index 2f1ffd6227583..157b15f7afe8d 100644
@@ -19,6 +19,7 @@
 
 #include <libnftnl/common.h>
 
+#include <libmnl/libmnl.h>
 #include <linux/netfilter.h>
 #include <linux/netfilter/nf_tables.h>
 
tests/nft-chain-test.c +35 -2
index 35a65be8d1587..64c506eb62a15 100644
@@ -23,9 +23,25 @@ static void print_err(const char *msg)
 	printf("\033[31mERROR:\e[0m %s\n", msg);
 }
 
-static void cmp_nftnl_chain(struct nftnl_chain *a, struct nftnl_chain *b)
+static void cmp_devices(const char * const *adevs,
+			const char * const *bdevs)
 {
+	int i;
+
+	if (!adevs && !bdevs)
+		return;
+	if (!!adevs ^ !!bdevs)
+		print_err("Chain devices mismatches");
+	for (i = 0; adevs[i] && bdevs[i]; i++) {
+		if (strcmp(adevs[i], bdevs[i]))
+			break;
+	}
+	if (adevs[i] || bdevs[i])
+		print_err("Chain devices mismatches");
+}
 
+static void cmp_nftnl_chain(struct nftnl_chain *a, struct nftnl_chain *b)
+{
 	if (strcmp(nftnl_chain_get_str(a, NFTNL_CHAIN_NAME),
 		   nftnl_chain_get_str(b, NFTNL_CHAIN_NAME)) != 0)
 		print_err("Chain name mismatches");
@@ -59,13 +75,17 @@ static void cmp_nftnl_chain(struct nftnl_chain *a, struct nftnl_chain *b)
 	if (strcmp(nftnl_chain_get_str(a, NFTNL_CHAIN_TYPE),
 		   nftnl_chain_get_str(b, NFTNL_CHAIN_TYPE)) != 0)
 		print_err("Chain type mismatches");
-	if (strcmp(nftnl_chain_get_str(a, NFTNL_CHAIN_DEV),
+	if (nftnl_chain_is_set(a, NFTNL_CHAIN_DEV) &&
+	    strcmp(nftnl_chain_get_str(a, NFTNL_CHAIN_DEV),
 		   nftnl_chain_get_str(b, NFTNL_CHAIN_DEV)) != 0)
 		print_err("Chain device mismatches");
+	cmp_devices(nftnl_chain_get_array(a, NFTNL_CHAIN_DEVICES),
+		    nftnl_chain_get_array(b, NFTNL_CHAIN_DEVICES));
 }
 
 int main(int argc, char *argv[])
 {
+	const char *devs[] = { "eth0", "eth1", "eth2", NULL };
 	struct nftnl_chain *a, *b;
 	char buf[4096];
 	struct nlmsghdr *nlh;
@@ -97,6 +117,19 @@ int main(int argc, char *argv[])
 
 	cmp_nftnl_chain(a, b);
 
+	/* repeat test with multiple devices */
+
+	nftnl_chain_unset(a, NFTNL_CHAIN_DEV);
+	nftnl_chain_set_array(a, NFTNL_CHAIN_DEVICES, devs);
+
+	nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWCHAIN, AF_INET, 0, 1234);
+	nftnl_chain_nlmsg_build_payload(nlh, a);
+
+	if (nftnl_chain_nlmsg_parse(nlh, b) < 0)
+		print_err("parsing problems");
+
+	cmp_nftnl_chain(a, b);
+
 	nftnl_chain_free(a);
 	nftnl_chain_free(b);
 
tests/nft-flowtable-test.c +21 -0
index 8ab8d4c5347a4..49bc0a1c5e043 100644
@@ -13,6 +13,23 @@ static void print_err(const char *msg)
 	printf("\033[31mERROR:\e[0m %s\n", msg);
 }
 
+static void cmp_devices(const char * const *adevs,
+			const char * const *bdevs)
+{
+	int i;
+
+	if (!adevs && !bdevs)
+		return;
+	if (!!adevs ^ !!bdevs)
+		print_err("Flowtable devices mismatches");
+	for (i = 0; adevs[i] && bdevs[i]; i++) {
+		if (strcmp(adevs[i], bdevs[i]))
+			break;
+	}
+	if (adevs[i] || bdevs[i])
+		print_err("Flowtable devices mismatches");
+}
+
 static void cmp_nftnl_flowtable(struct nftnl_flowtable *a, struct nftnl_flowtable *b)
 {
 	if (strcmp(nftnl_flowtable_get_str(a, NFTNL_FLOWTABLE_NAME),
@@ -44,10 +61,13 @@ static void cmp_nftnl_flowtable(struct nftnl_flowtable *a, struct nftnl_flowtabl
 	if (nftnl_flowtable_get_u64(a, NFTNL_FLOWTABLE_HANDLE) !=
 	    nftnl_flowtable_get_u64(b, NFTNL_FLOWTABLE_HANDLE))
 		print_err("Flowtable handle mismatches");
+	cmp_devices(nftnl_flowtable_get_array(a, NFTNL_FLOWTABLE_DEVICES),
+		    nftnl_flowtable_get_array(b, NFTNL_FLOWTABLE_DEVICES));
 }
 
 int main(int argc, char *argv[])
 {
+	const char *devs[] = { "eth0", "eth1", "eth2", NULL };
 	struct nftnl_flowtable *a, *b;
 	char buf[4096];
 	struct nlmsghdr *nlh;
@@ -66,6 +86,7 @@ int main(int argc, char *argv[])
 	nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_SIZE, 0x89016745);
 	nftnl_flowtable_set_u32(a, NFTNL_FLOWTABLE_FLAGS, 0x45016723);
 	nftnl_flowtable_set_u64(a, NFTNL_FLOWTABLE_HANDLE, 0x2345016789);
+	nftnl_flowtable_set_array(a, NFTNL_FLOWTABLE_DEVICES, devs);
 
 	nlh = nftnl_nlmsg_build_hdr(buf, NFT_MSG_NEWFLOWTABLE, AF_INET,
 				    0, 1234);
Signature · 1 line
2.47.0
#1 lore raw

On Wed, Oct 23, 2024 at 10:21:04PM +0200, Phil Sutter wrote:
[...]

src/flowtable.c void nftnl_flowtable_nlmsg_build_payload(struct nlmsghdr *nlh, · line 301

Quoted 12 lines
src/flowtable.c +2 -4
@@ -325,12 +295,11 @@ void nftnl_flowtable_nlmsg_build_payload(struct nlmsghdr *nlh,
 
 	if (c->flags & (1 << NFTNL_FLOWTABLE_DEVICES)) {
 		struct nlattr *nest_dev;
+		const char *dev;
 
 		nest_dev = mnl_attr_nest_start(nlh, NFTA_FLOWTABLE_HOOK_DEVS);
-		for (i = 0; i < c->dev_array_len; i++) {
-			mnl_attr_put_strz(nlh, NFTA_DEVICE_NAME,
-					  c->dev_array[i]);
-		}
+		nftnl_str_array_foreach(dev, &c->dev_array, i)

Where is this nftnl_str_array_foreach defined? I don't find it in this patch.

[...]

src/str_array.c line 18

Quoted 6 lines
src/str_array.c new file +6 -0
+void nftnl_str_array_clear(struct nftnl_str_array *sa)
+{
+	while (sa->len > 0)
+		free(sa->array[--sa->len]);
+	free(sa->array);
+	sa->array = NULL;

This is new, I'm fine with this, but it is only defensive, right? This stale reference would not be reached because attribute flag is cleared.

Quoted 1 line

+}

[...]

src/utils.c line 24

Quoted 11 lines
src/utils.c +1 -0
index 2f1ffd6227583..157b15f7afe8d 100644
@@ -19,6 +19,7 @@
 
 #include <libnftnl/common.h>
 
+#include <libmnl/libmnl.h>
 #include <linux/netfilter.h>
 #include <linux/netfilter/nf_tables.h>

Remove this chunk? It looks unrelated.

author reviewer
#2 lore raw
Quoted 8 lines · Pablo Neira Ayuso

On Wed, Oct 23, 2024 at 10:21:04PM +0200, Phil Sutter wrote:
[...]

Quoted 6 lines
src/flowtable.c +1 -0
@@ -325,12 +295,11 @@ void nftnl_flowtable_nlmsg_build_payload(struct nlmsghdr *nlh,
 
 	if (c->flags & (1 << NFTNL_FLOWTABLE_DEVICES)) {
 		struct nlattr *nest_dev;
+		const char *dev;
 
Quoted 9 lines

src/flowtable.c void nftnl_flowtable_nlmsg_build_payload(struct nlmsghdr *nlh, · line 301

Quoted 6 lines
src/flowtable.c +1 -4
 		nest_dev = mnl_attr_nest_start(nlh, NFTA_FLOWTABLE_HOOK_DEVS);
-		for (i = 0; i < c->dev_array_len; i++) {
-			mnl_attr_put_strz(nlh, NFTA_DEVICE_NAME,
-					  c->dev_array[i]);
-		}
+		nftnl_str_array_foreach(dev, &c->dev_array, i)

Where is this nftnl_str_array_foreach defined? I don't find it in this patch.

Argh, not just did I forget to 'git add' the new files, I also missed that it's about two files, not just one. I'll send a v3 which also includes str_array.h. Sorry for the mess.

Quoted 11 lines

[...]

src/str_array.c line 18

Quoted 6 lines
src/str_array.c new file +6 -0
+void nftnl_str_array_clear(struct nftnl_str_array *sa)
+{
+	while (sa->len > 0)
+		free(sa->array[--sa->len]);
+	free(sa->array);
+	sa->array = NULL;

This is new, I'm fine with this, but it is only defensive, right? This stale reference would not be reached because attribute flag is cleared.

Oh, you're right. A first approach used realloc_array() in nftnl_str_array_set(), but I abandoned it and this is a left-over. I'll drop it from v3.

Quoted 8 lines
Quoted 1 line

+}

[...]

Quoted 6 lines
src/utils.c +0 -0
index 2f1ffd6227583..157b15f7afe8d 100644
@@ -19,6 +19,7 @@
 
Quoted 7 lines

src/utils.c line 24

Quoted 5 lines
src/utils.c +1 -0
 #include <libnftnl/common.h>
 
+#include <libmnl/libmnl.h>
 #include <linux/netfilter.h>
 #include <linux/netfilter/nf_tables.h>

Remove this chunk? It looks unrelated.

ACK, sorry. Also a left-over.

Thanks, Phil