Skip to contentSkip to conversation

libnftables preparations

PATCH Applied Phil Sutter 2 participants 26 messages Oct 19, 2017 – Oct 20, 2017 netfilter

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

cover libnftables preparations

The following series prepares libnftables libarary split-off by moving API functions into src/libnftables.c, introducing include/nftables/nftables.h and enhancing the code by a number of getters and setters for applications to change configurable parts of struct nft_ctx without knowledge of that struct's internals.

The 'nft' binary will become the first "demo" user of libnftables and acts as a reference for library design and usability.

Phil Sutter (7):
nft_ctx_free: Fix for wrong argument passed to cache_release
libnftables: Move library stuff out of main.c
libnftables: Introduce nft_ctx_flush_cache()
cli: Use nft_run_cmd_from_buffer()
libnftables: Introduce nft_ctx_set_dry_run()
libnftables: Provide an API for include path handling
libnftables: Add remaining getters and setters

 include/Makefile.am          |   3 +-
 include/cli.h                |   6 +-
 include/nftables.h           |  48 +-----
 include/nftables/Makefile.am |   1 +
 include/nftables/nftables.h  |  72 +++++++++
 src/Makefile.am              |   3 +-
 src/cli.c                    |  24 +--
 src/libnftables.c            | 360 +++++++++++++++++++++++++++++++++++++++++++
 src/main.c                   | 285 +++-------------------------------
 src/scanner.l                |   4 +-
 10 files changed, 468 insertions(+), 338 deletions(-)
 create mode 100644 include/nftables/Makefile.am
 create mode 100644 include/nftables/nftables.h
 create mode 100644 src/libnftables.c
Signature · 1 line
2.13.1
author reviewer
#1 lore raw

PATCH 1/7 nft_ctx_free: Fix for wrong argument passed to cache_release

nft_ctx_free() should not refer to the global 'nft' variable, this will break as soon as the function is moved away from main.c. In order to use the cache reference from passed argument, the latter must not be const.

---
 src/main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/main.c +2 -2
index b59c932ad4299..1b26838058a4a 100644
@@ -305,13 +305,13 @@ static struct nft_ctx *nft_ctx_new(uint32_t flags)
 	return ctx;
 }
 
-static void nft_ctx_free(const struct nft_ctx *ctx)
+static void nft_ctx_free(struct nft_ctx *ctx)
 {
 	if (ctx->nf_sock)
 		netlink_close_sock(ctx->nf_sock);
 
 	iface_cache_release();
-	cache_release(&nft->cache);
+	cache_release(&ctx->cache);
 	xfree(ctx);
 	nft_exit();
 }
Signature · 1 line
2.13.1
maintainer Applied
#2 lore raw

Re: [nft PATCH 1/7] nft_ctx_free: Fix for wrong argument passed to cache_release

Quoted 3 lines · Phil Sutter

nft_ctx_free() should not refer to the global 'nft' variable, this will break as soon as the function is moved away from main.c. In order to use the cache reference from passed argument, the latter must not be const.

Applied, thanks.

author reviewer
#3 lore raw

PATCH 2/7 libnftables: Move library stuff out of main.c

In reply to Phil Sutter
---
 include/Makefile.am          |   3 +-
 include/nftables.h           |  65 +----------
 include/nftables/Makefile.am |   1 +
 include/nftables/nftables.h  |  88 +++++++++++++++
 src/Makefile.am              |   3 +-
 src/libnftables.c            | 261 +++++++++++++++++++++++++++++++++++++++++++
 src/main.c                   | 253 +----------------------------------------
 7 files changed, 356 insertions(+), 318 deletions(-)
 create mode 100644 include/nftables/Makefile.am
 create mode 100644 include/nftables/nftables.h
 create mode 100644 src/libnftables.c
include/Makefile.am +2 -1
index 5dd73d81f427e..a74ffbfa8de0a 100644
@@ -1,4 +1,5 @@
-SUBDIRS = linux
+SUBDIRS =		linux		\
+			nftables
 
 noinst_HEADERS = 	cli.h		\
 			datatype.h	\
include/nftables.h +1 -64

77 lines not on this page · raw message

include/nftables/Makefile.am new file +1 -0
new file mode 100644
index 0000000000000..9e31d519599c1
@@ -0,0 +1 @@
+noinst_HEADERS = nftables.h
include/nftables/nftables.h new file +88 -0

88 lines not on this page · raw message

src/Makefile.am +2 -1
index 99eef7bb849b6..4d613a731dfb9 100644
@@ -56,7 +56,8 @@ nft_SOURCES =	main.c				\
 		mergesort.c			\
 		scanner.l			\
 		tcpopt.c			\
-		parser_bison.y
+		parser_bison.y			\
+		libnftables.c
 
 if BUILD_CLI
 nft_SOURCES +=	cli.c
src/libnftables.c new file +261 -0

261 lines not on this page · raw message

src/main.c +1 -252

267 lines not on this page · raw message

Signature · 1 line
2.13.1
#4 lore raw

Re: [nft PATCH 2/7] libnftables: Move library stuff out of main.c

include/nftables/nftables.h

Quoted 120 lines · Phil Sutter
---
 include/Makefile.am          |   3 +-
 include/nftables.h           |  65 +----------
 include/nftables/Makefile.am |   1 +
 include/nftables/nftables.h  |  88 +++++++++++++++
 src/Makefile.am              |   3 +-
 src/libnftables.c            | 261 +++++++++++++++++++++++++++++++++++++++++++
 src/main.c                   | 253 +----------------------------------------
 7 files changed, 356 insertions(+), 318 deletions(-)
 create mode 100644 include/nftables/Makefile.am
 create mode 100644 include/nftables/nftables.h
 create mode 100644 src/libnftables.c
include/Makefile.am +2 -1
index 5dd73d81f427e..a74ffbfa8de0a 100644
@@ -1,4 +1,5 @@
-SUBDIRS = linux
+SUBDIRS =		linux		\
+			nftables
 
 noinst_HEADERS = 	cli.h		\
 			datatype.h	\
include/nftables.h +1 -64
index 01d72a87212ea..a633e1a2cc2e2 100644
@@ -4,63 +4,7 @@
 #include <stdbool.h>
 #include <stdarg.h>
 #include <utils.h>
-
-enum numeric_level {
-	NUMERIC_NONE,
-	NUMERIC_ADDR,
-	NUMERIC_PORT,
-	NUMERIC_ALL,
-};
-
-enum debug_level {
-	DEBUG_SCANNER		= 0x1,
-	DEBUG_PARSER		= 0x2,
-	DEBUG_EVALUATION	= 0x4,
-	DEBUG_NETLINK		= 0x8,
-	DEBUG_MNL		= 0x10,
-	DEBUG_PROTO_CTX		= 0x20,
-	DEBUG_SEGTREE		= 0x40,
-};
-
-#define INCLUDE_PATHS_MAX	16
-
-struct output_ctx {
-	unsigned int numeric;
-	unsigned int stateless;
-	unsigned int ip2name;
-	unsigned int handle;
-	unsigned int echo;
-	FILE *output_fp;
-};
-
-struct nft_cache {
-	bool			initialized;
-	struct list_head	list;
-	uint32_t		seqnum;
-};
-
-struct mnl_socket;
-
-struct nft_ctx {
-	struct mnl_socket	*nf_sock;
-	const char		*include_paths[INCLUDE_PATHS_MAX];
-	unsigned int		num_include_paths;
-	unsigned int		parser_max_errors;
-	unsigned int		debug_mask;
-	struct output_ctx	output;
-	bool			check;
-	struct nft_cache	cache;
-	uint32_t		flags;
-};
-
-#define NFT_CTX_DEFAULT		0
-
-enum nftables_exit_codes {
-	NFT_EXIT_SUCCESS	= 0,
-	NFT_EXIT_FAILURE	= 1,
-	NFT_EXIT_NOMEM		= 2,
-	NFT_EXIT_NONL		= 3,
-};
+#include <nftables/nftables.h>
 
 struct input_descriptor;
 struct location {
@@ -128,13 +72,6 @@ struct input_descriptor {
 	off_t				line_offset;
 };
 
-struct parser_state;
-struct mnl_socket;
-
-int nft_run(struct nft_ctx *nft, struct mnl_socket *nf_sock,
-	    void *scanner, struct parser_state *state,
-	    struct list_head *msgs);
-
 void ct_label_table_init(void);
 void mark_table_init(void);
 void gmp_init(void);
include/nftables/Makefile.am new file +1 -0
new file mode 100644
index 0000000000000..9e31d519599c1
@@ -0,0 +1 @@
+noinst_HEADERS = nftables.h
include/nftables/nftables.h new file +0 -0
new file mode 100644
index 0000000000000..052a77bfb5371

Is this nftables/nftables.h file what we will expose later on as header for this library?

If so... see below.

Quoted 21 lines
include/nftables/nftables.h new file +20 -0
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2017 Eric Leblond <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+#ifndef LIB_NFTABLES_H
+#define LIB_NFTABLES_H
+
+struct parser_state;
+struct mnl_socket;
+
+struct nft_cache {
+	bool			initialized;
+	struct list_head	list;
+	uint32_t		seqnum;
+};
+

include/nftables/nftables.h line 30

Quoted 10 lines
include/nftables/nftables.h new file +10 -0
+#define INCLUDE_PATHS_MAX	16
+
+struct output_ctx {
+	unsigned int numeric;
+	unsigned int stateless;
+	unsigned int ip2name;
+	unsigned int handle;
+	unsigned int echo;
+	FILE *output_fp;
+};

I think these structure should be just like:

struct output_ctx;

as a forward declaration. So we enforce users to use getters and setters.

So we can just move easily in a follow up patch to expose the library API to everyone, right?

Thanks.

author reviewer
#5 lore raw

Re: [nft PATCH 2/7] libnftables: Move library stuff out of main.c

Hi,

Quoted 1 line · Pablo Neira Ayuso

On Thu, Oct 19, 2017 at 10:18:42AM +0200, Phil Sutter wrote:

[...]

Quoted 8 lines

include/nftables/nftables.h

Quoted 5 lines
include/nftables/nftables.h new file +0 -0
new file mode 100644
index 0000000000000..052a77bfb5371

Is this nftables/nftables.h file what we will expose later on as header for this library?

Yes, exactly.

[...]

Quoted 34 lines
Quoted 21 lines
include/nftables/nftables.h new file +20 -0
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2017 Eric Leblond <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+#ifndef LIB_NFTABLES_H
+#define LIB_NFTABLES_H
+
+struct parser_state;
+struct mnl_socket;
+
+struct nft_cache {
+	bool			initialized;
+	struct list_head	list;
+	uint32_t		seqnum;
+};
+

include/nftables/nftables.h line 30

Quoted 10 lines
include/nftables/nftables.h new file +10 -0
+#define INCLUDE_PATHS_MAX	16
+
+struct output_ctx {
+	unsigned int numeric;
+	unsigned int stateless;
+	unsigned int ip2name;
+	unsigned int handle;
+	unsigned int echo;
+	FILE *output_fp;
+};

I think these structure should be just like:

Quoted 4 lines

struct output_ctx;

as a forward declaration. So we enforce users to use getters and setters.

Ultimately, I want to forward-declare struct nft_ctx as a whole. Is this fine with you (also from advanced API point of view)?

Thanks, Phil

#6 lore raw

Re: [nft PATCH 2/7] libnftables: Move library stuff out of main.c

Quoted 51 lines · Phil Sutter

Hi,

Quoted 1 line · Pablo Neira Ayuso

On Thu, Oct 19, 2017 at 10:18:42AM +0200, Phil Sutter wrote:

[...]

Quoted 8 lines

include/nftables/nftables.h

Quoted 5 lines
include/nftables/nftables.h new file +0 -0
new file mode 100644
index 0000000000000..052a77bfb5371

Is this nftables/nftables.h file what we will expose later on as header for this library?

Yes, exactly.

[...]

Quoted 34 lines
Quoted 21 lines
include/nftables/nftables.h new file +20 -0
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2017 Eric Leblond <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ */
+#ifndef LIB_NFTABLES_H
+#define LIB_NFTABLES_H
+
+struct parser_state;
+struct mnl_socket;
+
+struct nft_cache {
+	bool			initialized;
+	struct list_head	list;
+	uint32_t		seqnum;
+};
+

include/nftables/nftables.h line 30

Quoted 10 lines
include/nftables/nftables.h new file +10 -0
+#define INCLUDE_PATHS_MAX	16
+
+struct output_ctx {
+	unsigned int numeric;
+	unsigned int stateless;
+	unsigned int ip2name;
+	unsigned int handle;
+	unsigned int echo;
+	FILE *output_fp;
+};

I think these structure should be just like:

Quoted 7 lines
Quoted 4 lines

struct output_ctx;

as a forward declaration. So we enforce users to use getters and setters.

Ultimately, I want to forward-declare struct nft_ctx as a whole. Is this fine with you (also from advanced API point of view)?

Yes. No layout exposes, so we can freely changed them in the future.

author reviewer
#7 lore raw

PATCH 3/7 libnftables: Introduce nft_ctx_flush_cache()

In reply to Phil Sutter

This allows an application to explicitly flush caches associated with a given nft context.

Note that this is a bit inconsistent in that it releases the global interface cache, but nft_ctx_free() does the same so at least it's not a regression.

---
 include/nftables/nftables.h | 1 +
 src/libnftables.c           | 9 +++++++--
 2 files changed, 8 insertions(+), 2 deletions(-)
include/nftables/nftables.h +1 -0
index 052a77bfb5371..fbc6fd4252a97 100644
@@ -77,6 +77,7 @@ enum nftables_exit_codes {
 struct nft_ctx *nft_ctx_new(uint32_t flags);
 void nft_ctx_free(struct nft_ctx *ctx);
 FILE *nft_ctx_set_output(struct nft_ctx *ctx, FILE *fp);
+void nft_ctx_flush_cache(struct nft_ctx *ctx);
 
 int nft_run(struct nft_ctx *nft, struct mnl_socket *nf_sock,
 	    void *scanner, struct parser_state *state,
src/libnftables.c +7 -2
index 187747c66af21..0de50c854d572 100644
@@ -146,13 +146,18 @@ struct nft_ctx *nft_ctx_new(uint32_t flags)
 	return ctx;
 }
 
+void nft_ctx_flush_cache(struct nft_ctx *ctx)
+{
+	iface_cache_release();
+	cache_release(&ctx->cache);
+}
+
 void nft_ctx_free(struct nft_ctx *ctx)
 {
 	if (ctx->nf_sock)
 		netlink_close_sock(ctx->nf_sock);
 
-	iface_cache_release();
-	cache_release(&ctx->cache);
+	nft_ctx_flush_cache(ctx);
 	xfree(ctx);
 	nft_exit();
 }
Signature · 1 line
2.13.1
#8 lore raw

Re: [nft PATCH 3/7] libnftables: Introduce nft_ctx_flush_cache()

Quoted 30 lines · Phil Sutter

This allows an application to explicitly flush caches associated with a given nft context.

Note that this is a bit inconsistent in that it releases the global interface cache, but nft_ctx_free() does the same so at least it's not a regression.

---
 include/nftables/nftables.h | 1 +
 src/libnftables.c           | 9 +++++++--
 2 files changed, 8 insertions(+), 2 deletions(-)
include/nftables/nftables.h +1 -0
index 052a77bfb5371..fbc6fd4252a97 100644
@@ -77,6 +77,7 @@ enum nftables_exit_codes {
 struct nft_ctx *nft_ctx_new(uint32_t flags);
 void nft_ctx_free(struct nft_ctx *ctx);
 FILE *nft_ctx_set_output(struct nft_ctx *ctx, FILE *fp);
+void nft_ctx_flush_cache(struct nft_ctx *ctx);
 
 int nft_run(struct nft_ctx *nft, struct mnl_socket *nf_sock,
 	    void *scanner, struct parser_state *state,
src/libnftables.c +0 -0
index 187747c66af21..0de50c854d572 100644

src/libnftables.c struct nft_ctx *nft_ctx_new(uint32_t flags) · line 153

Quoted 8 lines
src/libnftables.c +5 -0
@@ -146,13 +146,18 @@ struct nft_ctx *nft_ctx_new(uint32_t flags)
 	return ctx;
 }
 
+void nft_ctx_flush_cache(struct nft_ctx *ctx)
+{
+	iface_cache_release();
+	cache_release(&ctx->cache);
+}

This flush allows us to release the cache, but nft_ctx_alloc() populates it. I'm missing something here, can we force a context repopulation?

If there is no usecase for this yet, I would keep this behind by now.

author reviewer
#9 lore raw

Re: [nft PATCH 3/7] libnftables: Introduce nft_ctx_flush_cache()

Hi,

Quoted 1 line · Pablo Neira Ayuso

On Thu, Oct 19, 2017 at 10:18:43AM +0200, Phil Sutter wrote:

[...]

Quoted 9 lines

src/libnftables.c struct nft_ctx *nft_ctx_new(uint32_t flags) · line 153

Quoted 5 lines
src/libnftables.c +5 -0
+void nft_ctx_flush_cache(struct nft_ctx *ctx)
+{
+	iface_cache_release();
+	cache_release(&ctx->cache);
+}

This flush allows us to release the cache, but nft_ctx_alloc() populates it. I'm missing something here, can we force a context repopulation?

No, nft_ctx_alloc() does not populate the cache, but just initialize cache list head (which is not undone by cache_release()). Cache population happens during command execution depending on whether a cache is needed or not.

Quoted 1 line

If there is no usecase for this yet, I would keep this behind by now.

The use-case for the above is cli_complete(), which explicitly drops the cache after execution of every command (probably because it's potentially long-lived and therefore things might change in background).

Cheers, Phil

#10 lore raw

Re: [nft PATCH 3/7] libnftables: Introduce nft_ctx_flush_cache()

Quoted 15 lines · Phil Sutter

Hi,

Quoted 1 line · Pablo Neira Ayuso

On Thu, Oct 19, 2017 at 10:18:43AM +0200, Phil Sutter wrote:

[...]

Quoted 9 lines

src/libnftables.c struct nft_ctx *nft_ctx_new(uint32_t flags) · line 153

Quoted 5 lines
src/libnftables.c +5 -0
+void nft_ctx_flush_cache(struct nft_ctx *ctx)
+{
+	iface_cache_release();
+	cache_release(&ctx->cache);
+}

This flush allows us to release the cache, but nft_ctx_alloc() populates it. I'm missing something here, can we force a context repopulation?

Quoted 4 lines

No, nft_ctx_alloc() does not populate the cache, but just initialize cache list head (which is not undone by cache_release()). Cache population happens during command execution depending on whether a cache is needed or not.

I see.

I think cache population should happen from nft_ctx_alloc(), caches are context after all.

Quoted 6 lines
Quoted 1 line

If there is no usecase for this yet, I would keep this behind by now.

The use-case for the above is cli_complete(), which explicitly drops the cache after execution of every command (probably because it's potentially long-lived and therefore things might change in background).

I see. If we follow the approach I'm describe above, then we need something like nft_ctx_reset(), where we reset all context and we get a fresh cache.

Makes sense to you?

author reviewer
#11 lore raw

Re: [nft PATCH 3/7] libnftables: Introduce nft_ctx_flush_cache()

Quoted 28 lines · Pablo Neira Ayuso
Quoted 15 lines · Phil Sutter

Hi,

Quoted 1 line · Pablo Neira Ayuso

On Thu, Oct 19, 2017 at 10:18:43AM +0200, Phil Sutter wrote:

[...]

Quoted 9 lines

src/libnftables.c struct nft_ctx *nft_ctx_new(uint32_t flags) · line 153

Quoted 5 lines
src/libnftables.c +5 -0
+void nft_ctx_flush_cache(struct nft_ctx *ctx)
+{
+	iface_cache_release();
+	cache_release(&ctx->cache);
+}

This flush allows us to release the cache, but nft_ctx_alloc() populates it. I'm missing something here, can we force a context repopulation?

Quoted 4 lines

No, nft_ctx_alloc() does not populate the cache, but just initialize cache list head (which is not undone by cache_release()). Cache population happens during command execution depending on whether a cache is needed or not.

I see.

I think cache population should happen from nft_ctx_alloc(), caches are context after all.

Quoted 2 lines
Quoted 1 line

If there is no usecase for this yet, I would keep this behind by now.

Quoted 8 lines
Quoted 4 lines

The use-case for the above is cli_complete(), which explicitly drops the cache after execution of every command (probably because it's potentially long-lived and therefore things might change in background).

I see. If we follow the approach I'm describe above, then we need something like nft_ctx_reset(), where we reset all context and we get a fresh cache.

I think it's worth keeping the current logic of when to populate the cache. Remember when I added a call to cache_update() for echo output support which caused an unnecessary slowdown you later complained about? Populating the cache unconditionally upon context creation would cause the same issue again if we follow my plan of making 'nft' binary the first user of libnftables.

Right now, we only explicitly clear the cache in CLI, and my patch allows applications to do that if they think it's necessary. Cache population is completely pointless if an application just wants to e.g. add a new table to the rule set, so I'd prefer to leave it this way. We spoke about possible performance implications of cache updates at NFWS already, keeping the existing (and well defined) logic will at least limit the impact of this potential bottleneck.

Cheers, Phil

author reviewer
#12 lore raw

PATCH 4/7 cli: Use nft_run_cmd_from_buffer()

In reply to Phil Sutter

This simplifies CLI code and allows to reduce libnftables API by not exporting nft_run().

Since nft_run_cmd_from_buffer() takes care of scanner initialization and libmnl socket passed to cli_init() is present as nft_ctx field as well, signature of cli_init() can be reduced to just take nft_ctx pointer as single argument.

Note that this change introduces two (possibly unwanted) side-effects:

* Input descriptor passed to scanner_push_buffer() is changed from the CLI-specific one to the one used by nft_run_cmd_from_buffer().

In practice though, this doesn't make a difference: input descriptor types INDESC_CLI and INDESC_BUFFER are treated equally by erec_print(). Also, scanner_push_buffer() NULLs input descriptor name, so that is not used at all in latter code.

* Error messages are printed to stderr instead of cli_nft->output.

This could be fixed by introducing an 'error_output' field in nft_ctx for nft_run_cmd_from_buffer() to use when printing error messages.

---
 include/cli.h               |  6 ++----
 include/nftables/nftables.h |  5 -----
 src/cli.c                   | 24 +++---------------------
 src/libnftables.c           |  6 +++---
 src/main.c                  |  3 +--
 5 files changed, 9 insertions(+), 35 deletions(-)
include/cli.h +2 -4
index 3ae1c459bce2d..3780e0917969d 100644
@@ -5,11 +5,9 @@
 
 struct parser_state;
 #ifdef HAVE_LIBREADLINE
-extern int cli_init(struct nft_ctx *nft, struct mnl_socket *nf_sock,
-		    struct parser_state *state);
+extern int cli_init(struct nft_ctx *nft);
 #else
-static inline int cli_init(struct nft_ctx *nft, struct mnl_socket *nf_sock,
-			   struct parser_state *state)
+static inline int cli_init(struct nft_ctx *nft)
 {
         return -1;
 }
include/nftables/nftables.h +0 -5
index fbc6fd4252a97..b91219d423df9 100644
@@ -9,7 +9,6 @@
 #ifndef LIB_NFTABLES_H
 #define LIB_NFTABLES_H
 
-struct parser_state;
 struct mnl_socket;
 
 struct nft_cache {
@@ -79,10 +78,6 @@ void nft_ctx_free(struct nft_ctx *ctx);
 FILE *nft_ctx_set_output(struct nft_ctx *ctx, FILE *fp);
 void nft_ctx_flush_cache(struct nft_ctx *ctx);
 
-int nft_run(struct nft_ctx *nft, struct mnl_socket *nf_sock,
-	    void *scanner, struct parser_state *state,
-	    struct list_head *msgs);
-
 int nft_run_cmd_from_buffer(struct nft_ctx *nft, char *buf, size_t buflen);
 int nft_run_cmd_from_filename(struct nft_ctx *nft, const char *filename);
 
src/cli.c +3 -21
index cadc3af6e8034..37351f2f8b04f 100644
@@ -35,15 +35,7 @@
 
 #define CMDLINE_HISTFILE	".nft.history"
 
-static const struct input_descriptor indesc_cli = {
-	.type	= INDESC_CLI,
-	.name   = "<cli>",
-};
-
-static struct parser_state *state;
 static struct nft_ctx *cli_nft;
-static struct mnl_socket *cli_nf_sock;
-static void *scanner;
 static char histfile[PATH_MAX];
 static char *multiline;
 static bool eof;
@@ -134,14 +126,9 @@ static void cli_complete(char *line)
 	xfree(line);
 	line = s;
 
-	parser_init(cli_nf_sock, &cli_nft->cache, state, &msgs,
-		    cli_nft->debug_mask, &cli_nft->output);
-	scanner_push_buffer(scanner, &indesc_cli, line);
-	nft_run(cli_nft, cli_nf_sock, scanner, state, &msgs);
-	erec_print_list(&cli_nft->output, &msgs, cli_nft->debug_mask);
+	nft_run_cmd_from_buffer(cli_nft, line, len + 2);
 	xfree(line);
-	cache_release(&cli_nft->cache);
-	iface_cache_release();
+	nft_ctx_flush_cache(cli_nft);
 }
 
 static char **cli_completion(const char *text, int start, int end)
@@ -149,12 +136,10 @@ static char **cli_completion(const char *text, int start, int end)
 	return NULL;
 }
 
-int cli_init(struct nft_ctx *nft, struct mnl_socket *nf_sock,
-	     struct parser_state *_state)
+int cli_init(struct nft_ctx *nft)
 {
 	const char *home;
 
-	cli_nf_sock = nf_sock;
 	cli_nft = nft;
 	rl_readline_name = "nft";
 	rl_instream  = stdin;
@@ -171,9 +156,6 @@ int cli_init(struct nft_ctx *nft, struct mnl_socket *nf_sock,
 	read_history(histfile);
 	history_set_pos(history_length);
 
-	state	= _state;
-	scanner = scanner_init(state);
-
 	while (!eof)
 		rl_callback_read_char();
 	return 0;
src/libnftables.c +3 -3
index 0de50c854d572..d88c299c3647e 100644
@@ -74,9 +74,9 @@ out:
 	return ret;
 }
 
-int nft_run(struct nft_ctx *nft, struct mnl_socket *nf_sock,
-	    void *scanner, struct parser_state *state,
-	    struct list_head *msgs)
+static int nft_run(struct nft_ctx *nft, struct mnl_socket *nf_sock,
+		   void *scanner, struct parser_state *state,
+		   struct list_head *msgs)
 {
 	struct cmd *cmd, *next;
 	int ret;
src/main.c +1 -2
index a7da460f28ca5..3c107181305c7 100644
@@ -168,7 +168,6 @@ int main(int argc, char * const *argv)
 	char *buf = NULL, *filename = NULL;
 	unsigned int len;
 	bool interactive = false;
-	struct parser_state state;
 	int i, val, rc;
 
 	nft = nft_ctx_new(NFT_CTX_DEFAULT);
@@ -272,7 +271,7 @@ int main(int argc, char * const *argv)
 	} else if (filename != NULL) {
 		rc = nft_run_cmd_from_filename(nft, filename);
 	} else if (interactive) {
-		if (cli_init(nft, nft->nf_sock, &state) < 0) {
+		if (cli_init(nft) < 0) {
 			fprintf(stderr, "%s: interactive CLI not supported in this build\n",
 				argv[0]);
 			exit(NFT_EXIT_FAILURE);
Signature · 1 line
2.13.1
#13 lore raw

Re: [nft PATCH 4/7] cli: Use nft_run_cmd_from_buffer()

Quoted 7 lines · Phil Sutter

This simplifies CLI code and allows to reduce libnftables API by not exporting nft_run().

Since nft_run_cmd_from_buffer() takes care of scanner initialization and libmnl socket passed to cli_init() is present as nft_ctx field as well, signature of cli_init() can be reduced to just take nft_ctx pointer as single argument.

libmnl socket is indeed in nft_ctx, but we're planning a mode that allows to expose the mnl_socket for advanced handling. In that scenario, nft->nf_sock will be null.

So I would prefer we don't do changes that we have to undo once the advanced API is in place.

author reviewer
#14 lore raw

Re: [nft PATCH 4/7] cli: Use nft_run_cmd_from_buffer()

Quoted 9 lines · Pablo Neira Ayuso
Quoted 7 lines · Phil Sutter

This simplifies CLI code and allows to reduce libnftables API by not exporting nft_run().

Since nft_run_cmd_from_buffer() takes care of scanner initialization and libmnl socket passed to cli_init() is present as nft_ctx field as well, signature of cli_init() can be reduced to just take nft_ctx pointer as single argument.

Quoted 6 lines

libmnl socket is indeed in nft_ctx, but we're planning a mode that allows to expose the mnl_socket for advanced handling. In that scenario, nft->nf_sock will be null.

So I would prefer we don't do changes that we have to undo once the advanced API is in place.

IMHO this doesn't contradict what the patch does. Right now we only have the "simple API", and the patch changes src/cli.c to use just that. CLI code doesn't need anything which is not fulfilled by simple API at this point, so I'd say changing it to use advanced API should be done when we implement features (e.g. transaction control) there.

What do you think?

Cheers, Phil

#15 lore raw

Re: [nft PATCH 4/7] cli: Use nft_run_cmd_from_buffer()

Quoted 17 lines · Phil Sutter
Quoted 9 lines · Pablo Neira Ayuso
Quoted 7 lines · Phil Sutter

This simplifies CLI code and allows to reduce libnftables API by not exporting nft_run().

Since nft_run_cmd_from_buffer() takes care of scanner initialization and libmnl socket passed to cli_init() is present as nft_ctx field as well, signature of cli_init() can be reduced to just take nft_ctx pointer as single argument.

Quoted 6 lines

libmnl socket is indeed in nft_ctx, but we're planning a mode that allows to expose the mnl_socket for advanced handling. In that scenario, nft->nf_sock will be null.

So I would prefer we don't do changes that we have to undo once the advanced API is in place.

Quoted 7 lines

IMHO this doesn't contradict what the patch does. Right now we only have the "simple API", and the patch changes src/cli.c to use just that. CLI code doesn't need anything which is not fulfilled by simple API at this point, so I'd say changing it to use advanced API should be done when we implement features (e.g. transaction control) there.

What do you think?

I have no strong objection against this, I just would like we don't lose track of the high level API, and that one will need to expose the netlink socket. So all these calls we will end up needed the nf_sock parameter again at some point.

I don't have any strong opinion against this, just an observation.

author reviewer
#16 lore raw

Re: [nft PATCH 4/7] cli: Use nft_run_cmd_from_buffer()

Quoted 24 lines · Pablo Neira Ayuso
Quoted 23 lines · Phil Sutter
Quoted 9 lines · Pablo Neira Ayuso
Quoted 7 lines · Phil Sutter

This simplifies CLI code and allows to reduce libnftables API by not exporting nft_run().

Since nft_run_cmd_from_buffer() takes care of scanner initialization and libmnl socket passed to cli_init() is present as nft_ctx field as well, signature of cli_init() can be reduced to just take nft_ctx pointer as single argument.

Quoted 6 lines

libmnl socket is indeed in nft_ctx, but we're planning a mode that allows to expose the mnl_socket for advanced handling. In that scenario, nft->nf_sock will be null.

So I would prefer we don't do changes that we have to undo once the advanced API is in place.

IMHO this doesn't contradict what the patch does. Right now we only have the "simple API", and the patch changes src/cli.c to use just that. CLI code doesn't need anything which is not fulfilled by simple API at this point, so I'd say changing it to use advanced API should be done when we implement features (e.g. transaction control) there.

Quoted 8 lines
Quoted 1 line

What do you think?

I have no strong objection against this, I just would like we don't lose track of the high level API, and that one will need to expose the netlink socket. So all these calls we will end up needed the nf_sock parameter again at some point.

I don't have any strong opinion against this, just an observation.

I'd suggest to review things again once we have a common view of how the advanced API will look like. Right now, CLI is fine with using the simple API as proposed by my patches, so passing it ctx->nf_sock separately just to have it either ignore it or reassign it to ctx->nf_sock again doesn't make sense to me.

Cheers, Phil

author reviewer
#17 lore raw

PATCH 5/7 libnftables: Introduce nft_ctx_set_dry_run()

In reply to Phil Sutter

Provide a convenient interface to configure dry run mode.

---
 include/nftables/nftables.h | 3 +++
 src/libnftables.c           | 5 +++++
 src/main.c                  | 2 +-
 3 files changed, 9 insertions(+), 1 deletion(-)
include/nftables/nftables.h +3 -0
index b91219d423df9..f0c9bbf3ba3fe 100644
@@ -75,7 +75,10 @@ enum nftables_exit_codes {
 
 struct nft_ctx *nft_ctx_new(uint32_t flags);
 void nft_ctx_free(struct nft_ctx *ctx);
+
 FILE *nft_ctx_set_output(struct nft_ctx *ctx, FILE *fp);
+void nft_ctx_set_dry_run(struct nft_ctx *ctx, bool dry);
+
 void nft_ctx_flush_cache(struct nft_ctx *ctx);
 
 int nft_run_cmd_from_buffer(struct nft_ctx *nft, char *buf, size_t buflen);
src/libnftables.c +5 -0
index d88c299c3647e..817f537e32618 100644
@@ -171,6 +171,11 @@ FILE *nft_ctx_set_output(struct nft_ctx *ctx, FILE *fp)
 	return old;
 }
 
+void nft_ctx_set_dry_run(struct nft_ctx *ctx, bool dry)
+{
+	ctx->check = dry;
+}
+
 static const struct input_descriptor indesc_cmdline = {
 	.type	= INDESC_BUFFER,
 	.name	= "<cmdline>",
src/main.c +1 -1
index 3c107181305c7..8359367b78654 100644
@@ -187,7 +187,7 @@ int main(int argc, char * const *argv)
 			       PACKAGE_NAME, PACKAGE_VERSION, RELEASE_NAME);
 			exit(NFT_EXIT_SUCCESS);
 		case OPT_CHECK:
-			nft->check = true;
+			nft_ctx_set_dry_run(nft, true);
 			break;
 		case OPT_FILE:
 			filename = optarg;
Signature · 1 line
2.13.1
author reviewer
#18 lore raw

PATCH 6/7 libnftables: Provide an API for include path handling

In reply to Phil Sutter

In order to keep the API simple, remove INCLUDE_PATHS_MAX restraint and dynamically allocate nft_ctx field include_paths instead.

---
 include/nftables/nftables.h |  6 +++---
 src/libnftables.c           | 34 ++++++++++++++++++++++++++++++++--
 src/main.c                  |  9 ++++-----
 src/scanner.l               |  4 +---
 4 files changed, 40 insertions(+), 13 deletions(-)
include/nftables/nftables.h +3 -3
index f0c9bbf3ba3fe..a752f20d74132 100644
@@ -17,8 +17,6 @@ struct nft_cache {
 	uint32_t		seqnum;
 };
 
-#define INCLUDE_PATHS_MAX	16
-
 struct output_ctx {
 	unsigned int numeric;
 	unsigned int stateless;
@@ -30,7 +28,7 @@ struct output_ctx {
 
 struct nft_ctx {
 	struct mnl_socket	*nf_sock;
-	const char		*include_paths[INCLUDE_PATHS_MAX];
+	char			**include_paths;
 	unsigned int		num_include_paths;
 	unsigned int		parser_max_errors;
 	unsigned int		debug_mask;
@@ -78,6 +76,8 @@ void nft_ctx_free(struct nft_ctx *ctx);
 
 FILE *nft_ctx_set_output(struct nft_ctx *ctx, FILE *fp);
 void nft_ctx_set_dry_run(struct nft_ctx *ctx, bool dry);
+int nft_ctx_add_include_path(struct nft_ctx *ctx, const char *path);
+void nft_ctx_clear_include_paths(struct nft_ctx *ctx);
 
 void nft_ctx_flush_cache(struct nft_ctx *ctx);
 
src/libnftables.c +32 -2
index 817f537e32618..2f4275c9a0a94 100644
@@ -6,10 +6,13 @@
  * published by the Free Software Foundation.
  *
  */
+#define _GNU_SOURCE
 #include <erec.h>
 #include <errno.h>
 #include <mnl.h>
 #include <parser.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <utils.h>
 #include <iface.h>
@@ -122,6 +125,33 @@ static void nft_exit(void)
 	mark_table_exit();
 }
 
+int nft_ctx_add_include_path(struct nft_ctx *ctx, const char *path)
+{
+	char **tmp;
+	int pcount = ctx->num_include_paths;
+
+	tmp = realloc(ctx->include_paths, (pcount + 1) * sizeof(char *));
+	if (!tmp)
+		return -1;
+
+	ctx->include_paths = tmp;
+
+	if (asprintf(&ctx->include_paths[pcount], "%s", path) < 0)
+		return -1;
+
+	ctx->num_include_paths++;
+	return 0;
+}
+
+void nft_ctx_clear_include_paths(struct nft_ctx *ctx)
+{
+	while (ctx->num_include_paths)
+		xfree(ctx->include_paths[--ctx->num_include_paths]);
+
+	xfree(ctx->include_paths);
+	ctx->include_paths = NULL;
+}
+
 static void nft_ctx_netlink_init(struct nft_ctx *ctx)
 {
 	ctx->nf_sock = netlink_open_sock();
@@ -134,8 +164,7 @@ struct nft_ctx *nft_ctx_new(uint32_t flags)
 	nft_init();
 	ctx = xzalloc(sizeof(struct nft_ctx));
 
-	ctx->include_paths[0]	= DEFAULT_INCLUDE_PATH;
-	ctx->num_include_paths	= 1;
+	nft_ctx_add_include_path(ctx, DEFAULT_INCLUDE_PATH);
 	ctx->parser_max_errors	= 10;
 	init_list_head(&ctx->cache.list);
 	ctx->flags = flags;
@@ -158,6 +187,7 @@ void nft_ctx_free(struct nft_ctx *ctx)
 		netlink_close_sock(ctx->nf_sock);
 
 	nft_ctx_flush_cache(ctx);
+	nft_ctx_clear_include_paths(ctx);
 	xfree(ctx);
 	nft_exit();
 }
src/main.c +4 -5
index 8359367b78654..de5c115757f44 100644
@@ -196,13 +196,12 @@ int main(int argc, char * const *argv)
 			interactive = true;
 			break;
 		case OPT_INCLUDEPATH:
-			if (nft->num_include_paths >= INCLUDE_PATHS_MAX) {
-				fprintf(stderr, "Too many include paths "
-						"specified, max. %u\n",
-					INCLUDE_PATHS_MAX - 1);
+			if (nft_ctx_add_include_path(nft, optarg)) {
+				fprintf(stderr,
+					"Failed to add include path '%s'\n",
+					optarg);
 				exit(NFT_EXIT_FAILURE);
 			}
-			nft->include_paths[nft->num_include_paths++] = optarg;
 			break;
 		case OPT_NUMERIC:
 			if (++nft->output.numeric > NUMERIC_ALL) {
src/scanner.l +1 -3
index 594073660c6b1..ee09775ebf1d9 100644
@@ -794,9 +794,7 @@ int scanner_include_file(struct nft_ctx *nft, void *scanner,
 	int ret = -1;
 
 	if (search_in_include_path(filename)) {
-		for (i = 0; i < INCLUDE_PATHS_MAX; i++) {
-			if (nft->include_paths[i] == NULL)
-				break;
+		for (i = 0; i < nft->num_include_paths; i++) {
 			ret = snprintf(buf, sizeof(buf), "%s/%s",
 				       nft->include_paths[i], filename);
 			if (ret < 0 || ret >= PATH_MAX) {
Signature · 1 line
2.13.1
#19 lore raw

Re: [nft PATCH 6/7] libnftables: Provide an API for include path handling

Quoted 61 lines · Phil Sutter

In order to keep the API simple, remove INCLUDE_PATHS_MAX restraint and dynamically allocate nft_ctx field include_paths instead.

---
 include/nftables/nftables.h |  6 +++---
 src/libnftables.c           | 34 ++++++++++++++++++++++++++++++++--
 src/main.c                  |  9 ++++-----
 src/scanner.l               |  4 +---
 4 files changed, 40 insertions(+), 13 deletions(-)
include/nftables/nftables.h +3 -3
index f0c9bbf3ba3fe..a752f20d74132 100644
@@ -17,8 +17,6 @@ struct nft_cache {
 	uint32_t		seqnum;
 };
 
-#define INCLUDE_PATHS_MAX	16
-
 struct output_ctx {
 	unsigned int numeric;
 	unsigned int stateless;
@@ -30,7 +28,7 @@ struct output_ctx {
 
 struct nft_ctx {
 	struct mnl_socket	*nf_sock;
-	const char		*include_paths[INCLUDE_PATHS_MAX];
+	char			**include_paths;
 	unsigned int		num_include_paths;
 	unsigned int		parser_max_errors;
 	unsigned int		debug_mask;
@@ -78,6 +76,8 @@ void nft_ctx_free(struct nft_ctx *ctx);
 
 FILE *nft_ctx_set_output(struct nft_ctx *ctx, FILE *fp);
 void nft_ctx_set_dry_run(struct nft_ctx *ctx, bool dry);
+int nft_ctx_add_include_path(struct nft_ctx *ctx, const char *path);
+void nft_ctx_clear_include_paths(struct nft_ctx *ctx);
 
 void nft_ctx_flush_cache(struct nft_ctx *ctx);
 
src/libnftables.c +3 -0
index 817f537e32618..2f4275c9a0a94 100644
@@ -6,10 +6,13 @@
  * published by the Free Software Foundation.
  *
  */
+#define _GNU_SOURCE
 #include <erec.h>
 #include <errno.h>
 #include <mnl.h>
 #include <parser.h>
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <utils.h>
 #include <iface.h>

src/libnftables.c static void nft_exit(void) · line 128

Quoted 4 lines
src/libnftables.c +1 -0
@@ -122,6 +125,33 @@ static void nft_exit(void)
 	mark_table_exit();
 }
 
+int nft_ctx_add_include_path(struct nft_ctx *ctx, const char *path)

Do we want to accept runtime addition/removal of include paths?

I mean, I would just make it nft_ctx_set_include_path(), then add an unsetter, so we simplify this.

Let me know if I'm overlooking anything, thanks.

author reviewer
#20 lore raw

Re: [nft PATCH 6/7] libnftables: Provide an API for include path handling

Hi,

Quoted 1 line · Pablo Neira Ayuso

On Thu, Oct 19, 2017 at 10:18:46AM +0200, Phil Sutter wrote:

[...]

Quoted 3 lines
Quoted 1 line

+int nft_ctx_add_include_path(struct nft_ctx *ctx, const char *path)

Do we want to accept runtime addition/removal of include paths?

Not necessarily, but src/main.c does just that: It calls nft_ctx_new() first, then adds include paths as it parses them from command line.

Quoted 2 lines

I mean, I would just make it nft_ctx_set_include_path(), then add an unsetter, so we simplify this.

The counterpart to nft_ctx_add_include_path() is nft_ctx_clear_include_paths(), which just drops all the previously set ones. Does that meet your understanding of an unsetter, or am I missing something?

The reason why this patch is a bit more complicated is because I wanted to get rid of the hard upper limit of include paths to avoid introducing a getter for number of set include paths or to make it necessary for applications (read: src/main.c) to check what return code nft_ctx_add_include_path() returned to print a reasonable error message.

Cheers, Phil

#21 lore raw

Re: [nft PATCH 6/7] libnftables: Provide an API for include path handling

Quoted 11 lines · Phil Sutter

Hi,

Quoted 1 line · Pablo Neira Ayuso

On Thu, Oct 19, 2017 at 10:18:46AM +0200, Phil Sutter wrote:

[...]

Quoted 3 lines
Quoted 1 line

+int nft_ctx_add_include_path(struct nft_ctx *ctx, const char *path)

Do we want to accept runtime addition/removal of include paths?

Not necessarily, but src/main.c does just that: It calls nft_ctx_new() first, then adds include paths as it parses them from command line.

So it's more like a one time call to set up the include path, right? So I think semantically this is just another setter. This _add_ name made me think you can keep adding including path one after another anytime.

Quoted 7 lines
Quoted 2 lines

I mean, I would just make it nft_ctx_set_include_path(), then add an unsetter, so we simplify this.

The counterpart to nft_ctx_add_include_path() is nft_ctx_clear_include_paths(), which just drops all the previously set ones. Does that meet your understanding of an unsetter, or am I missing something?

Do we have a usecase for nft_ctx_clear_include_paths(). If we don't
- I don't see any at least from my side - I'd prefer, to keep it back.

Quoted 5 lines

The reason why this patch is a bit more complicated is because I wanted to get rid of the hard upper limit of include paths to avoid introducing a getter for number of set include paths or to make it necessary for applications (read: src/main.c) to check what return code nft_ctx_add_include_path() returned to print a reasonable error message.

I'm fine with removing the upper limit, but that is a different thing. My only concerns are related to the API we provide to set include paths.

author reviewer
#22 lore raw

Re: [nft PATCH 6/7] libnftables: Provide an API for include path handling

Quoted 10 lines · Pablo Neira Ayuso
Quoted 9 lines · Phil Sutter

Hi,

Quoted 1 line · Pablo Neira Ayuso

On Thu, Oct 19, 2017 at 10:18:46AM +0200, Phil Sutter wrote:

[...]

Quoted 3 lines
Quoted 1 line

+int nft_ctx_add_include_path(struct nft_ctx *ctx, const char *path)

Do we want to accept runtime addition/removal of include paths?

Quoted 7 lines
Quoted 2 lines

Not necessarily, but src/main.c does just that: It calls nft_ctx_new() first, then adds include paths as it parses them from command line.

So it's more like a one time call to set up the include path, right? So I think semantically this is just another setter. This _add_ name made me think you can keep adding including path one after another anytime.

Yes, the API (or specifically, nft_ctx_add_include_path()) allows that. The only alternative I could think of would be to introduce something like:

| int nft_ctx_set_include_paths(struct nft_ctx *ctx, const char **paths)

Which means src/main.c would have to take care of populating the char ** array itself in order to later pass it in one go to the setter. Fine with me, you decide! :)

Quoted 10 lines
Quoted 7 lines
Quoted 2 lines

I mean, I would just make it nft_ctx_set_include_path(), then add an unsetter, so we simplify this.

The counterpart to nft_ctx_add_include_path() is nft_ctx_clear_include_paths(), which just drops all the previously set ones. Does that meet your understanding of an unsetter, or am I missing something?

Do we have a usecase for nft_ctx_clear_include_paths(). If we don't
- I don't see any at least from my side - I'd prefer, to keep it back.

It's only used in nft_ctx_free() for now, just because it's convenient. If you don't want to export it (yet), I can make it static so code readability is kept but it won't be available to applications.

Quoted 9 lines
Quoted 5 lines

The reason why this patch is a bit more complicated is because I wanted to get rid of the hard upper limit of include paths to avoid introducing a getter for number of set include paths or to make it necessary for applications (read: src/main.c) to check what return code nft_ctx_add_include_path() returned to print a reasonable error message.

I'm fine with removing the upper limit, but that is a different thing. My only concerns are related to the API we provide to set include paths.

OK, cool. So we only have to agree about above items.

Cheers, Phil

author reviewer
#23 lore raw

PATCH 7/7 libnftables: Add remaining getters and setters

In reply to Phil Sutter

Provide API functions for remaining context settings changed by main.c, then hide struct nft_ctx definition from applications. This allows us to later change data structures internally without risk of breaking applications.

---
 include/nftables.h          | 27 +++++++++++++++++++++
 include/nftables/nftables.h | 41 ++++++++++---------------------
 src/libnftables.c           | 59 +++++++++++++++++++++++++++++++++++++++++++++
 src/main.c                  | 18 +++++++++-----
 4 files changed, 111 insertions(+), 34 deletions(-)
include/nftables.h +27 -0
index a633e1a2cc2e2..ad72383303bdb 100644
@@ -6,6 +6,33 @@
 #include <utils.h>
 #include <nftables/nftables.h>
 
+struct nft_cache {
+	bool			initialized;
+	struct list_head	list;
+	uint32_t		seqnum;
+};
+
+struct output_ctx {
+	unsigned int numeric;
+	unsigned int stateless;
+	unsigned int ip2name;
+	unsigned int handle;
+	unsigned int echo;
+	FILE *output_fp;
+};
+
+struct nft_ctx {
+	struct mnl_socket	*nf_sock;
+	char			**include_paths;
+	unsigned int		num_include_paths;
+	unsigned int		parser_max_errors;
+	unsigned int		debug_mask;
+	struct output_ctx	output;
+	bool			check;
+	struct nft_cache	cache;
+	uint32_t		flags;
+};
+
 struct input_descriptor;
 struct location {
 	const struct input_descriptor		*indesc;
include/nftables/nftables.h +13 -28

53 lines not on this page · raw message

src/libnftables.c +59 -0

65 lines not on this page · raw message

src/main.c +12 -6
index de5c115757f44..c65966bcf5995 100644
@@ -169,6 +169,8 @@ int main(int argc, char * const *argv)
 	unsigned int len;
 	bool interactive = false;
 	int i, val, rc;
+	enum numeric_level numeric;
+	unsigned int debug_mask;
 
 	nft = nft_ctx_new(NFT_CTX_DEFAULT);
 	nft_ctx_set_output(nft, stdout);
@@ -204,20 +206,23 @@ int main(int argc, char * const *argv)
 			}
 			break;
 		case OPT_NUMERIC:
-			if (++nft->output.numeric > NUMERIC_ALL) {
+			numeric = nft_ctx_output_get_numeric(nft);
+			if (numeric == NUMERIC_ALL) {
 				fprintf(stderr, "Too many numeric options "
 						"used, max. %u\n",
 					NUMERIC_ALL);
 				exit(NFT_EXIT_FAILURE);
 			}
+			nft_ctx_output_set_numeric(nft, numeric + 1);
 			break;
 		case OPT_STATELESS:
-			nft->output.stateless++;
+			nft_ctx_output_set_stateless(nft, true);
 			break;
 		case OPT_IP2NAME:
-			nft->output.ip2name++;
+			nft_ctx_output_set_ip2name(nft, true);
 			break;
 		case OPT_DEBUG:
+			debug_mask = nft_ctx_output_get_debug(nft);
 			for (;;) {
 				unsigned int i;
 				char *end;
@@ -229,7 +234,7 @@ int main(int argc, char * const *argv)
 				for (i = 0; i < array_size(debug_param); i++) {
 					if (strcmp(debug_param[i].name, optarg))
 						continue;
-					nft->debug_mask |= debug_param[i].level;
+					debug_mask |= debug_param[i].level;
 					break;
 				}
 
@@ -243,12 +248,13 @@ int main(int argc, char * const *argv)
 					break;
 				optarg = end + 1;
 			}
+			nft_ctx_output_set_debug(nft, debug_mask);
 			break;
 		case OPT_HANDLE_OUTPUT:
-			nft->output.handle++;
+			nft_ctx_output_set_handle(nft, true);
 			break;
 		case OPT_ECHO:
-			nft->output.echo++;
+			nft_ctx_output_set_echo(nft, true);
 			break;
 		case OPT_INVALID:
 			exit(NFT_EXIT_FAILURE);
Signature · 1 line
2.13.1
#24 lore raw

Re: [nft PATCH 7/7] libnftables: Add remaining getters and setters

Quoted 37 lines · Phil Sutter

Provide API functions for remaining context settings changed by main.c, then hide struct nft_ctx definition from applications. This allows us to later change data structures internally without risk of breaking applications.

---
 include/nftables.h          | 27 +++++++++++++++++++++
 include/nftables/nftables.h | 41 ++++++++++---------------------
 src/libnftables.c           | 59 +++++++++++++++++++++++++++++++++++++++++++++
 src/main.c                  | 18 +++++++++-----
 4 files changed, 111 insertions(+), 34 deletions(-)
include/nftables.h +16 -0
index a633e1a2cc2e2..ad72383303bdb 100644
@@ -6,6 +6,33 @@
 #include <utils.h>
 #include <nftables/nftables.h>
 
+struct nft_cache {
+	bool			initialized;
+	struct list_head	list;
+	uint32_t		seqnum;
+};
+
+struct output_ctx {
+	unsigned int numeric;
+	unsigned int stateless;
+	unsigned int ip2name;
+	unsigned int handle;
+	unsigned int echo;
+	FILE *output_fp;
+};
+
+struct nft_ctx {

include/nftables.h line 34

Quoted 10 lines
include/nftables.h +10 -0
+	struct mnl_socket	*nf_sock;
+	char			**include_paths;
+	unsigned int		num_include_paths;
+	unsigned int		parser_max_errors;
+	unsigned int		debug_mask;
+	struct output_ctx	output;
+	bool			check;
+	struct nft_cache	cache;
+	uint32_t		flags;
+};

Oh, I see. Now these structure definitions are coming back to include/nftables.h. I'm telling this because of what I mentioned in 2/7.

I would prefer we avoid these goes back and forth with this code.

Quoted 119 lines
include/nftables.h +1 -0
+
 struct input_descriptor;
 struct location {
 	const struct input_descriptor		*indesc;
include/nftables/nftables.h +13 -28
index a752f20d74132..2bc3061457257 100644
@@ -9,34 +9,7 @@
 #ifndef LIB_NFTABLES_H
 #define LIB_NFTABLES_H
 
-struct mnl_socket;
-
-struct nft_cache {
-	bool			initialized;
-	struct list_head	list;
-	uint32_t		seqnum;
-};
-
-struct output_ctx {
-	unsigned int numeric;
-	unsigned int stateless;
-	unsigned int ip2name;
-	unsigned int handle;
-	unsigned int echo;
-	FILE *output_fp;
-};
-
-struct nft_ctx {
-	struct mnl_socket	*nf_sock;
-	char			**include_paths;
-	unsigned int		num_include_paths;
-	unsigned int		parser_max_errors;
-	unsigned int		debug_mask;
-	struct output_ctx	output;
-	bool			check;
-	struct nft_cache	cache;
-	uint32_t		flags;
-};
+struct nft_ctx;
 
 enum debug_level {
 	DEBUG_SCANNER		= 0x1,
@@ -78,6 +51,18 @@ FILE *nft_ctx_set_output(struct nft_ctx *ctx, FILE *fp);
 void nft_ctx_set_dry_run(struct nft_ctx *ctx, bool dry);
 int nft_ctx_add_include_path(struct nft_ctx *ctx, const char *path);
 void nft_ctx_clear_include_paths(struct nft_ctx *ctx);
+enum numeric_level nft_ctx_output_get_numeric(struct nft_ctx *ctx);
+void nft_ctx_output_set_numeric(struct nft_ctx *ctx, enum numeric_level level);
+bool nft_ctx_output_get_stateless(struct nft_ctx *ctx);
+void nft_ctx_output_set_stateless(struct nft_ctx *ctx, bool val);
+bool nft_ctx_output_get_ip2name(struct nft_ctx *ctx);
+void nft_ctx_output_set_ip2name(struct nft_ctx *ctx, bool val);
+unsigned int nft_ctx_output_get_debug(struct nft_ctx *ctx);
+void nft_ctx_output_set_debug(struct nft_ctx *ctx, unsigned int mask);
+bool nft_ctx_output_get_handle(struct nft_ctx *ctx);
+void nft_ctx_output_set_handle(struct nft_ctx *ctx, bool val);
+bool nft_ctx_output_get_echo(struct nft_ctx *ctx);
+void nft_ctx_output_set_echo(struct nft_ctx *ctx, bool val);
 
 void nft_ctx_flush_cache(struct nft_ctx *ctx);
 
src/libnftables.c +48 -0
index 2f4275c9a0a94..925c96d1272a3 100644
@@ -206,6 +206,65 @@ void nft_ctx_set_dry_run(struct nft_ctx *ctx, bool dry)
 	ctx->check = dry;
 }
 
+enum numeric_level nft_ctx_output_get_numeric(struct nft_ctx *ctx)
+{
+	return ctx->output.numeric;
+}
+
+void nft_ctx_output_set_numeric(struct nft_ctx *ctx, enum numeric_level level)
+{
+	ctx->output.numeric = level;
+}
+
+bool nft_ctx_output_get_stateless(struct nft_ctx *ctx)
+{
+	return ctx->output.stateless;
+}
+
+void nft_ctx_output_set_stateless(struct nft_ctx *ctx, bool val)
+{
+	ctx->output.stateless = val;
+}
+
+bool nft_ctx_output_get_ip2name(struct nft_ctx *ctx)
+{
+	return ctx->output.ip2name;
+}
+
+void nft_ctx_output_set_ip2name(struct nft_ctx *ctx, bool val)
+{
+	ctx->output.ip2name = val;
+}
+
+unsigned int nft_ctx_output_get_debug(struct nft_ctx *ctx)
+{
+	return ctx->debug_mask;
+}
+void nft_ctx_output_set_debug(struct nft_ctx *ctx, unsigned int mask)
+{
+	ctx->debug_mask = mask;
+}
+
+bool nft_ctx_output_get_handle(struct nft_ctx *ctx)
+{
+	return ctx->output.handle;
+}
+
+void nft_ctx_output_set_handle(struct nft_ctx *ctx, bool val)
+{
+	ctx->output.handle = val;
+}

src/libnftables.c void nft_ctx_set_dry_run(struct nft_ctx *ctx, bool dry) · line 266

Quoted 10 lines
src/libnftables.c +10 -0
+
+bool nft_ctx_output_get_echo(struct nft_ctx *ctx)
+{
+	return ctx->output.echo;
+}
+
+void nft_ctx_output_set_echo(struct nft_ctx *ctx, bool val)
+{
+	ctx->output.echo = val;
+}

BTW, why not just add dry_run setter here too?

I see no need for the standalone patch that comes in this series.

Thanks!

author reviewer
#25 lore raw

Re: [nft PATCH 7/7] libnftables: Add remaining getters and setters

Hi Pablo,

On Fri, Oct 20, 2017 at 02:18:53PM +0200, Pablo Neira Ayuso wrote:
[...]

Quoted 3 lines

Oh, I see. Now these structure definitions are coming back to include/nftables.h. I'm telling this because of what I mentioned in 2/7.

I have to admit, it was quite entertaining watching you following my series through your comments. :)

Quoted 1 line

I would prefer we avoid these goes back and forth with this code.

Yes, point taken - I will recombine the changes into more sensible chunks.

Thanks, Phil