Skip to contentSkip to conversation

Remove more global variables

PATCH New Pablo Neira Ayuso 1 participant 7 messages Aug 22, 2017 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover Remove more global variables

Hi,

This patchset contains more updates to prepare the introduction of libnftables, by placing global variables into context structures.

This series applies on top of Phil's "[nft] Keep cache in struct nft_ctx":

	http://patchwork.ozlabs.org/patch/804410/

Pablo Neira Ayuso (6):
src: add include_paths to struct nft_ctx
src: add maximum number of parser errors to struct nft_ctx
src: remove ifdef DEBUG pollution
src: add struct mnl_ctx
mnl: pass struct netlink_ctx to mnl_nft_socket_sendmsg()
src: add debugging mask to context structure

 configure.ac              |   4 +-
 include/erec.h            |   6 +-
 include/expression.h      |   3 +-
 include/mnl.h             |   9 ++-
 include/netlink.h         |  18 ++++--
 include/nftables.h        |   8 +--
 include/parser.h          |   8 ++-
 include/proto.h           |   5 +-
 include/rule.h            |   4 +-
 include/utils.h           |  12 ----
 src/cli.c                 |   5 +-
 src/erec.c                |   9 +--
 src/evaluate.c            |  51 ++++++++-------
 src/main.c                |  42 ++++++------
 src/mnl.c                 | 160 +++++++++++++++++++++++++++++-----------------
 src/netlink.c             |  86 +++++++++++--------------
 src/netlink_delinearize.c |   3 +-
 src/netlink_linearize.c   |   2 +-
 src/parser_bison.y        |  21 +++---
 src/proto.c               |  16 +++--
 src/rule.c                |  23 ++++---
 src/scanner.l             |  10 +--
 src/segtree.c             |  30 +++++----
 23 files changed, 293 insertions(+), 242 deletions(-)
Signature · 1 line
2.1.4
author maintainer
#1 lore raw

PATCH 1/6 src: add include_paths to struct nft_ctx

Not convenient to keep this as static for the upcoming library, so let's move it where it belongs.

---
 include/nftables.h |  3 ++-
 include/parser.h   |  5 +++--
 src/main.c         | 17 +++++++++++------
 src/parser_bison.y |  5 +++--
 src/scanner.l      | 10 +++++-----
 5 files changed, 24 insertions(+), 16 deletions(-)
include/nftables.h +2 -1
index 994b5111176c..8399b1ae68f9 100644
@@ -39,6 +39,8 @@ struct nft_cache {
 };
 
 struct nft_ctx {
+	const char		*include_paths[INCLUDE_PATHS_MAX];
+	unsigned int		num_include_paths;
 	struct output_ctx	output;
 	bool			check;
 	struct nft_cache	cache;
@@ -46,7 +48,6 @@ struct nft_ctx {
 
 extern unsigned int max_errors;
 extern unsigned int debug_level;
-extern const char *include_paths[INCLUDE_PATHS_MAX];
 
 enum nftables_exit_codes {
 	NFT_EXIT_SUCCESS	= 0,
include/parser.h +3 -2
index 5a452f7767aa..df6026824584 100644
@@ -33,14 +33,15 @@ struct mnl_socket;
 
 extern void parser_init(struct mnl_socket *nf_sock, struct nft_cache *cache,
 			struct parser_state *state, struct list_head *msgs);
-extern int nft_parse(void *, struct parser_state *state);
+extern int nft_parse(struct nft_ctx *ctx, void *, struct parser_state *state);
 
 extern void *scanner_init(struct parser_state *state);
 extern void scanner_destroy(struct parser_state *state);
 
 extern int scanner_read_file(void *scanner, const char *filename,
 			     const struct location *loc);
-extern int scanner_include_file(void *scanner, const char *filename,
+extern int scanner_include_file(struct nft_ctx *ctx, void *scanner,
+				const char *filename,
 				const struct location *loc);
 extern void scanner_push_buffer(void *scanner,
 				const struct input_descriptor *indesc,
src/main.c +11 -6
index b86ae62f1343..eb0dfb02fd15 100644
@@ -34,9 +34,6 @@ unsigned int max_errors = 10;
 unsigned int debug_level;
 #endif
 
-const char *include_paths[INCLUDE_PATHS_MAX] = { DEFAULT_INCLUDE_PATH };
-static unsigned int num_include_paths = 1;
-
 enum opt_vals {
 	OPT_HELP		= 'h',
 	OPT_VERSION		= 'v',
@@ -253,7 +250,7 @@ int nft_run(struct nft_ctx *nft, struct mnl_socket *nf_sock,
 	struct cmd *cmd, *next;
 	int ret;
 
-	ret = nft_parse(scanner, state);
+	ret = nft_parse(nft, scanner, state);
 	if (ret != 0 || state->nerrs > 0) {
 		ret = -1;
 		goto err1;
@@ -294,6 +291,12 @@ void nft_exit(void)
 	mark_table_exit();
 }
 
+static void nft_ctx_init(struct nft_ctx *nft)
+{
+	nft->include_paths[0]	= DEFAULT_INCLUDE_PATH;
+	nft->num_include_paths	= 1;
+}
+
 int main(int argc, char * const *argv)
 {
 	struct parser_state state;
@@ -308,6 +311,8 @@ int main(int argc, char * const *argv)
 	init_list_head(&nft.cache.list);
 
 	nft_init();
+	nft_ctx_init(&nft);
+
 	nf_sock = netlink_open_sock();
 	while (1) {
 		val = getopt_long(argc, argv, OPTSTRING, options, NULL);
@@ -332,13 +337,13 @@ int main(int argc, char * const *argv)
 			interactive = true;
 			break;
 		case OPT_INCLUDEPATH:
-			if (num_include_paths >= INCLUDE_PATHS_MAX) {
+			if (nft.num_include_paths >= INCLUDE_PATHS_MAX) {
 				fprintf(stderr, "Too many include paths "
 						"specified, max. %u\n",
 					INCLUDE_PATHS_MAX - 1);
 				exit(NFT_EXIT_FAILURE);
 			}
-			include_paths[num_include_paths++] = optarg;
+			nft.include_paths[nft.num_include_paths++] = optarg;
 			break;
 		case OPT_NUMERIC:
 			if (++nft.output.numeric > NUMERIC_ALL) {
src/parser_bison.y +3 -2
index 783b72f5a343..18c0f0aa9600 100644
@@ -48,7 +48,7 @@ void parser_init(struct mnl_socket *nf_sock, struct nft_cache *cache,
 	state->ectx.nf_sock = nf_sock;
 }
 
-static void yyerror(struct location *loc, void *scanner,
+static void yyerror(struct location *loc, struct nft_ctx *nft, void *scanner,
 		    struct parser_state *state, const char *s)
 {
 	erec_queue(error(loc, "%s", s), state->msgs);
@@ -109,6 +109,7 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 %name-prefix "nft_"
 %debug
 %pure-parser
+%parse-param		{ struct nft_ctx *nft }
 %parse-param		{ void *scanner }
 %parse-param		{ struct parser_state *state }
 %lex-param		{ scanner }
@@ -709,7 +710,7 @@ opt_newline		:	NEWLINE
 
 common_block		:	INCLUDE		QUOTED_STRING	stmt_seperator
 			{
-				if (scanner_include_file(scanner, $2, &@$) < 0) {
+				if (scanner_include_file(nft, scanner, $2, &@$) < 0) {
 					xfree($2);
 					YYERROR;
 				}
src/scanner.l +5 -5
index b6ba32d88f4a..d50e2b671065 100644
@@ -773,8 +773,8 @@ static bool search_in_include_path(const char *filename)
 		filename[0] != '/');
 }
 
-int scanner_include_file(void *scanner, const char *filename,
-			 const struct location *loc)
+int scanner_include_file(struct nft_ctx *nft, void *scanner,
+			 const char *filename, const struct location *loc)
 {
 	struct parser_state *state = yyget_extra(scanner);
 	struct error_record *erec;
@@ -784,13 +784,13 @@ int scanner_include_file(void *scanner, const char *filename,
 
 	if (search_in_include_path(filename)) {
 		for (i = 0; i < INCLUDE_PATHS_MAX; i++) {
-			if (include_paths[i] == NULL)
+			if (nft->include_paths[i] == NULL)
 				break;
 			ret = snprintf(buf, sizeof(buf), "%s/%s",
-				       include_paths[i], filename);
+				       nft->include_paths[i], filename);
 			if (ret < 0 || ret >= PATH_MAX) {
 				erec = error(loc, "Too long file path \"%s/%s\"\n",
-					     include_paths[i], filename);
+					     nft->include_paths[i], filename);
 				erec_queue(erec, state->msgs);
 				return -1;
 			}
Signature · 1 line
2.1.4
author maintainer
#2 lore raw

PATCH 2/6 src: add maximum number of parser errors to struct nft_ctx

In reply to Pablo Neira Ayuso

Not a global variable anymore.

---
 include/nftables.h | 2 +-
 src/main.c         | 2 +-
 src/parser_bison.y | 6 +++---
 3 files changed, 5 insertions(+), 5 deletions(-)
include/nftables.h +1 -1
index 8399b1ae68f9..8858ad605516 100644
@@ -41,12 +41,12 @@ struct nft_cache {
 struct nft_ctx {
 	const char		*include_paths[INCLUDE_PATHS_MAX];
 	unsigned int		num_include_paths;
+	unsigned int		parser_max_errors;
 	struct output_ctx	output;
 	bool			check;
 	struct nft_cache	cache;
 };
 
-extern unsigned int max_errors;
 extern unsigned int debug_level;
 
 enum nftables_exit_codes {
src/main.c +1 -1
index eb0dfb02fd15..fc44b186d5f0 100644
@@ -29,7 +29,6 @@
 #include <cli.h>
 
 static struct nft_ctx nft;
-unsigned int max_errors = 10;
 #ifdef DEBUG
 unsigned int debug_level;
 #endif
@@ -295,6 +294,7 @@ static void nft_ctx_init(struct nft_ctx *nft)
 {
 	nft->include_paths[0]	= DEFAULT_INCLUDE_PATH;
 	nft->num_include_paths	= 1;
+	nft->parser_max_errors = 10;
 }
 
 int main(int argc, char * const *argv)
src/parser_bison.y +3 -3
index 18c0f0aa9600..7c00f4f099f7 100644
@@ -692,7 +692,7 @@ input			:	/* empty */
 
 					list_add_tail(&$2->list, &list);
 					if (cmd_evaluate(&state->ectx, $2) < 0) {
-						if (++state->nerrs == max_errors)
+						if (++state->nerrs == nft->parser_max_errors)
 							YYABORT;
 					} else
 						list_splice_tail(&list, &state->cmds);
@@ -731,7 +731,7 @@ common_block		:	INCLUDE		QUOTED_STRING	stmt_seperator
 			}
 			|	error		stmt_seperator
 			{
-				if (++state->nerrs == max_errors)
+				if (++state->nerrs == nft->parser_max_errors)
 					YYABORT;
 				yyerrok;
 			}
@@ -758,7 +758,7 @@ line			:	common_block			{ $$ = NULL; }
 
 					list_add_tail(&$1->list, &list);
 					if (cmd_evaluate(&state->ectx, $1) < 0) {
-						if (++state->nerrs == max_errors)
+						if (++state->nerrs == nft->parser_max_errors)
 							YYABORT;
 					} else
 						list_splice_tail(&list, &state->cmds);
Signature · 1 line
2.1.4
author maintainer
#3 lore raw

PATCH 3/6 src: remove ifdef DEBUG pollution

In reply to Pablo Neira Ayuso

Get rid of lots of ifdef DEBUG pollution in the code.

The --debug= option is useful to get feedback from users, so it should be always there. And we really save nothing from keeping this code away from the control plane with a compile time option. Just running tests/shell/ before and after this patch, time shows almost no difference.

So this patch leaves --enable-debug around to add debugging symbols in your builds, this is left set on by default.

---
 configure.ac       |  4 ++--
 include/utils.h    | 12 ------------
 src/evaluate.c     |  8 --------
 src/main.c         | 10 ----------
 src/mnl.c          |  8 --------
 src/netlink.c      | 14 --------------
 src/parser_bison.y |  2 --
 src/proto.c        |  2 --
 src/segtree.c      |  3 +--
 9 files changed, 3 insertions(+), 60 deletions(-)
configure.ac +2 -2
index bef6c0b631df..2570b03a91ef 100644
@@ -23,7 +23,7 @@ AC_DEFINE([_GNU_SOURCE], [], [Enable various GNU extensions])
 AC_DEFINE([_STDC_FORMAT_MACROS], [], [printf-style format macros])
 
 AC_ARG_ENABLE([debug],
-	      AS_HELP_STRING([--enable-debug], [Disable debugging]),
+	      AS_HELP_STRING([--enable-debug], [Disable debugging symbols]),
 	      AS_IF([test "x$enable_debug" = "xno"], [with_debug=no], [with_debug=yes]),
 	      [with_debug=yes])
 AC_SUBST(with_debug)
@@ -155,7 +155,7 @@ AC_OUTPUT
 echo "
 nft configuration:
   cli support:			${with_cli}
-  enable debugging:		${with_debug}
+  enable debugging symbols:	${with_debug}
   use mini-gmp:			${with_mini_gmp}
   enable pdf documentation:	${enable_pdf_doc}
   libxtables support:		${with_libxtables}"
include/utils.h +0 -12
index 0605eeed3b06..369195240e24 100644
@@ -13,17 +13,9 @@
 
 #define BITS_PER_BYTE	8
 
-#if defined(DEBUG)
 #define pr_debug(fmt, arg...) printf(fmt, ##arg)
-#else
-#define pr_debug(fmt, arg...) ({ if (false) {}; 0; })
-#endif
 
-#if defined(DEBUG) && defined(HAVE_LIBGMP)
 #define pr_gmp_debug(fmt, arg...) gmp_printf(fmt, ##arg)
-#else
-#define pr_gmp_debug(fmt, arg...) ({ if (false) {}; 0; })
-#endif
 
 #define __fmtstring(x, y)	__attribute__((format(printf, x, y)))
 #if 0
@@ -35,11 +27,7 @@
 #define __must_check		__attribute__((warn_unused_result))
 #define __noreturn		__attribute__((__noreturn__))
 
-#ifdef DEBUG
 #define BUG(fmt, arg...)	({ fprintf(stderr, "BUG: " fmt, ##arg); assert(0); })
-#else
-#define BUG(fmt, arg...)	assert(0)
-#endif
 
 #define BUILD_BUG_ON(condition)	((void)sizeof(char[1 - 2*!!(condition)]))
 #define BUILD_BUG_ON_ZERO(e)	(sizeof(char[1 - 2 * !!(e)]) - 1)
src/evaluate.c +0 -8
index f52a0843a0c0..c1ad05b7ad86 100644
@@ -1708,7 +1708,6 @@ static int expr_evaluate_meta(struct eval_ctx *ctx, struct expr **exprp)
 
 static int expr_evaluate(struct eval_ctx *ctx, struct expr **expr)
 {
-#ifdef DEBUG
 	if (debug_level & DEBUG_EVALUATION) {
 		struct error_record *erec;
 		erec = erec_create(EREC_INFORMATIONAL, &(*expr)->location,
@@ -1717,7 +1716,6 @@ static int expr_evaluate(struct eval_ctx *ctx, struct expr **expr)
 		expr_print(*expr, &octx_debug_dummy);
 		printf("\n\n");
 	}
-#endif
 
 	switch ((*expr)->ops->type) {
 	case EXPR_SYMBOL:
@@ -2678,7 +2676,6 @@ static int stmt_evaluate_objref(struct eval_ctx *ctx, struct stmt *stmt)
 
 int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt)
 {
-#ifdef DEBUG
 	if (debug_level & DEBUG_EVALUATION) {
 		struct error_record *erec;
 		erec = erec_create(EREC_INFORMATIONAL, &stmt->location,
@@ -2686,7 +2683,6 @@ int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt)
 		erec_print(stdout, erec); stmt_print(stmt, &octx_debug_dummy);
 		printf("\n\n");
 	}
-#endif
 
 	switch (stmt->ops->type) {
 	case STMT_COUNTER:
@@ -3326,7 +3322,6 @@ static int cmd_evaluate_export(struct eval_ctx *ctx, struct cmd *cmd)
 	return cache_update(ctx->nf_sock, ctx->cache, cmd->op, ctx->msgs);
 }
 
-#ifdef DEBUG
 static const char *cmd_op_name[] = {
 	[CMD_INVALID]	= "invalid",
 	[CMD_ADD]	= "add",
@@ -3349,11 +3344,9 @@ static const char *cmd_op_to_name(enum cmd_ops op)
 
 	return cmd_op_name[op];
 }
-#endif
 
 int cmd_evaluate(struct eval_ctx *ctx, struct cmd *cmd)
 {
-#ifdef DEBUG
 	if (debug_level & DEBUG_EVALUATION) {
 		struct error_record *erec;
 
@@ -3361,7 +3354,6 @@ int cmd_evaluate(struct eval_ctx *ctx, struct cmd *cmd)
 				   "Evaluate %s", cmd_op_to_name(cmd->op));
 		erec_print(stdout, erec); printf("\n\n");
 	}
-#endif
 
 	ctx->cmd = cmd;
 	switch (cmd->op) {
src/main.c +0 -10
index fc44b186d5f0..4c74bdce2824 100644
@@ -29,9 +29,7 @@
 #include <cli.h>
 
 static struct nft_ctx nft;
-#ifdef DEBUG
 unsigned int debug_level;
-#endif
 
 enum opt_vals {
 	OPT_HELP		= 'h',
@@ -90,13 +88,11 @@ static const struct option options[] = {
 		.val		= OPT_INCLUDEPATH,
 		.has_arg	= 1,
 	},
-#ifdef DEBUG
 	{
 		.name		= "debug",
 		.val		= OPT_DEBUG,
 		.has_arg	= 1,
 	},
-#endif
 	{
 		.name		= "handle",
 		.val		= OPT_HANDLE_OUTPUT,
@@ -131,14 +127,11 @@ static void show_help(const char *name)
 "  -a, --handle			Output rule handle.\n"
 "  -e, --echo			Echo what has been added, inserted or replaced.\n"
 "  -I, --includepath <directory>	Add <directory> to the paths searched for include files. Default is: %s\n"
-#ifdef DEBUG
 "  --debug <level [,level...]>	Specify debugging level (scanner, parser, eval, netlink, mnl, proto-ctx, segtree, all)\n"
-#endif
 "\n",
 	name, DEFAULT_INCLUDE_PATH);
 }
 
-#ifdef DEBUG
 static const struct {
 	const char		*name;
 	enum debug_level	level;
@@ -176,7 +169,6 @@ static const struct {
 		.level		= ~0,
 	},
 };
-#endif
 
 static const struct input_descriptor indesc_cmdline = {
 	.type	= INDESC_BUFFER,
@@ -359,7 +351,6 @@ int main(int argc, char * const *argv)
 		case OPT_IP2NAME:
 			nft.output.ip2name++;
 			break;
-#ifdef DEBUG
 		case OPT_DEBUG:
 			for (;;) {
 				unsigned int i;
@@ -387,7 +378,6 @@ int main(int argc, char * const *argv)
 				optarg = end + 1;
 			}
 			break;
-#endif
 		case OPT_HANDLE_OUTPUT:
 			nft.output.handle++;
 			break;
src/mnl.c +0 -8
index f5859c989f3c..f57982da1bc5 100644
@@ -72,10 +72,8 @@ nft_mnl_talk(struct mnl_socket *nf_sock, const void *data,
 {
 	uint32_t portid = mnl_socket_get_portid(nf_sock);
 
-#ifdef DEBUG
 	if (debug_level & DEBUG_MNL)
 		mnl_nlmsg_fprintf(stdout, data, len, sizeof(struct nfgenmsg));
-#endif
 
 	if (mnl_socket_sendto(nf_sock, data, len) < 0)
 		return -1;
@@ -223,14 +221,11 @@ static ssize_t mnl_nft_socket_sendmsg(const struct mnl_socket *nl,
 		.msg_iov	= iov,
 		.msg_iovlen	= iov_len,
 	};
-#ifdef DEBUG
 	uint32_t i;
-#endif
 
 	mnl_set_sndbuffer(nl, batch);
 	nftnl_batch_iovec(batch, iov, iov_len);
 
-#ifdef DEBUG
 	for (i = 0; i < iov_len; i++) {
 		if (debug_level & DEBUG_MNL) {
 			mnl_nlmsg_fprintf(stdout,
@@ -238,7 +233,6 @@ static ssize_t mnl_nft_socket_sendmsg(const struct mnl_socket *nl,
 					  sizeof(struct nfgenmsg));
 		}
 	}
-#endif
 
 	return sendmsg(mnl_socket_get_fd(nl), &msg, 0);
 }
@@ -1072,12 +1066,10 @@ int mnl_nft_event_listener(struct mnl_socket *nf_sock,
 			}
 		}
 
-#ifdef DEBUG
 		if (debug_level & DEBUG_MNL) {
 			mnl_nlmsg_fprintf(stdout, buf, sizeof(buf),
 					  sizeof(struct nfgenmsg));
 		}
-#endif /* DEBUG */
 		ret = mnl_cb_run(buf, ret, 0, 0, cb, cb_data);
 		if (ret <= 0)
 			break;
src/netlink.c +0 -14
index f6eb08fd8d41..7730b724d4ac 100644
@@ -504,7 +504,6 @@ int netlink_del_rule_batch(struct netlink_ctx *ctx, const struct handle *h,
 
 void netlink_dump_rule(const struct nftnl_rule *nlr)
 {
-#ifdef DEBUG
 	char buf[4096];
 
 	if (!(debug_level & DEBUG_NETLINK))
@@ -512,12 +511,10 @@ void netlink_dump_rule(const struct nftnl_rule *nlr)
 
 	nftnl_rule_snprintf(buf, sizeof(buf), nlr, 0, 0);
 	fprintf(stdout, "%s\n", buf);
-#endif
 }
 
 void netlink_dump_expr(const struct nftnl_expr *nle)
 {
-#ifdef DEBUG
 	char buf[4096];
 
 	if (!(debug_level & DEBUG_NETLINK))
@@ -525,7 +522,6 @@ void netlink_dump_expr(const struct nftnl_expr *nle)
 
 	nftnl_expr_snprintf(buf, sizeof(buf), nle, 0, 0);
 	fprintf(stdout, "%s\n", buf);
-#endif
 }
 
 static int list_rule_cb(struct nftnl_rule *nlr, void *arg)
@@ -579,7 +575,6 @@ static int netlink_flush_rules(struct netlink_ctx *ctx, const struct handle *h,
 
 void netlink_dump_chain(const struct nftnl_chain *nlc)
 {
-#ifdef DEBUG
 	char buf[4096];
 
 	if (!(debug_level & DEBUG_NETLINK))
@@ -587,7 +582,6 @@ void netlink_dump_chain(const struct nftnl_chain *nlc)
 
 	nftnl_chain_snprintf(buf, sizeof(buf), nlc, 0, 0);
 	fprintf(stdout, "%s\n", buf);
-#endif
 }
 
 static int netlink_add_chain_compat(struct netlink_ctx *ctx,
@@ -1036,7 +1030,6 @@ static const struct datatype *dtype_map_from_kernel(enum nft_data_types type)
 
 void netlink_dump_set(const struct nftnl_set *nls)
 {
-#ifdef DEBUG
 	char buf[4096];
 
 	if (!(debug_level & DEBUG_NETLINK))
@@ -1044,7 +1037,6 @@ void netlink_dump_set(const struct nftnl_set *nls)
 
 	nftnl_set_snprintf(buf, sizeof(buf), nls, 0, 0);
 	fprintf(stdout, "%s\n", buf);
-#endif
 }
 
 static int set_parse_udata_cb(const struct nftnl_udata *attr, void *data)
@@ -1661,7 +1653,6 @@ out:
 
 void netlink_dump_obj(struct nftnl_obj *nln)
 {
-#ifdef DEBUG
 	char buf[4096];
 
 	if (!(debug_level & DEBUG_NETLINK))
@@ -1669,7 +1660,6 @@ void netlink_dump_obj(struct nftnl_obj *nln)
 
 	nftnl_obj_snprintf(buf, sizeof(buf), nln, 0, 0);
 	fprintf(stdout, "%s\n", buf);
-#endif
 }
 
 int netlink_add_obj(struct netlink_ctx *ctx, const struct handle *h,
@@ -2854,7 +2844,6 @@ static int netlink_events_trace_cb(const struct nlmsghdr *nlh, int type,
 	return MNL_CB_OK;
 }
 
-#ifdef DEBUG
 /* only those which could be useful listening to events */
 static const char *const nftnl_msg_types[NFT_MSG_MAX] = {
 	[NFT_MSG_NEWTABLE]	= "NFT_MSG_NEWTABLE",
@@ -2880,16 +2869,13 @@ static const char *nftnl_msgtype2str(uint16_t type)
 
 	return nftnl_msg_types[type];
 }
-#endif /* DEBUG */
 
 static void netlink_events_debug(uint16_t type)
 {
-#ifdef DEBUG
 	if (!(debug_level & DEBUG_NETLINK))
 		return;
 
 	printf("netlink event: %s\n", nftnl_msgtype2str(type));
-#endif /* DEBUG */
 }
 
 static int netlink_events_newgen_cb(const struct nlmsghdr *nlh, int type,
src/parser_bison.y +0 -2
index 7c00f4f099f7..76a0115d46a2 100644
@@ -118,12 +118,10 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 
 %initial-action {
 	location_init(scanner, state, &yylloc);
-#ifdef DEBUG
 	if (debug_level & DEBUG_SCANNER)
 		nft_set_debug(1, scanner);
 	if (debug_level & DEBUG_PARSER)
 		yydebug = 1;
-#endif
 }
 
 %union {
src/proto.c +0 -2
index 7ac0ee03191a..045f2d811969 100644
@@ -140,7 +140,6 @@ const struct hook_proto_desc hook_proto_desc[] = {
 
 static void proto_ctx_debug(const struct proto_ctx *ctx, enum proto_bases base)
 {
-#ifdef DEBUG
 	unsigned int i;
 
 	if (!(debug_level & DEBUG_PROTO_CTX))
@@ -159,7 +158,6 @@ static void proto_ctx_debug(const struct proto_ctx *ctx, enum proto_bases base)
 		pr_debug("\n");
 	}
 	pr_debug("\n");
-#endif
 }
 
 /**
src/segtree.c +1 -2
index 34a001613eab..8623e862cf77 100644
@@ -163,10 +163,9 @@ static void __ei_insert(struct seg_tree *tree, struct elementary_interval *new)
 
 static bool segtree_debug(void)
 {
-#ifdef DEBUG
 	if (debug_level & DEBUG_SEGTREE)
 		return true;
-#endif
+
 	return false;
 }
 
Signature · 1 line
2.1.4
author maintainer
#4 lore raw

PATCH 4/6 src: add struct mnl_ctx

In reply to Pablo Neira Ayuso

This new structure contains the netlink socket and the sequence number.

---
 include/mnl.h |   6 ++-
 src/mnl.c     | 127 ++++++++++++++++++++++++++++++++++++++++------------------
 2 files changed, 94 insertions(+), 39 deletions(-)
include/mnl.h +5 -1
index affbf746688e..72072f7f9a00 100644
@@ -3,8 +3,12 @@
 
 #include <list.h>
 #include <netlink.h>
+#include <libmnl/libmnl.h>
 
-struct mnl_socket;
+struct mnl_ctx {
+	struct mnl_socket	*nf_sock;
+	unsigned int		seqnum;
+};
 
 struct mnl_socket *netlink_open_sock(void);
 void netlink_close_sock(struct mnl_socket *nf_sock);
src/mnl.c +89 -38

341 lines not on this page · raw message

Signature · 1 line
2.1.4
author maintainer
#5 lore raw

PATCH 5/6 mnl: pass struct netlink_ctx to mnl_nft_socket_sendmsg()

In reply to Pablo Neira Ayuso

Reduce function footprint.

---
 src/mnl.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
src/mnl.c +6 -7
index 4f9c30c25d8f..e51c3294f76d 100644
@@ -210,13 +210,12 @@ static void mnl_set_sndbuffer(const struct mnl_socket *nl,
 	nlbuffsiz = newbuffsiz;
 }
 
-static ssize_t mnl_nft_socket_sendmsg(const struct mnl_socket *nl,
-				      struct nftnl_batch *batch)
+static ssize_t mnl_nft_socket_sendmsg(const struct netlink_ctx *ctx)
 {
 	static const struct sockaddr_nl snl = {
 		.nl_family = AF_NETLINK
 	};
-	uint32_t iov_len = nftnl_batch_iovec_len(batch);
+	uint32_t iov_len = nftnl_batch_iovec_len(ctx->batch);
 	struct iovec iov[iov_len];
 	struct msghdr msg = {
 		.msg_name	= (struct sockaddr *) &snl,
@@ -226,8 +225,8 @@ static ssize_t mnl_nft_socket_sendmsg(const struct mnl_socket *nl,
 	};
 	uint32_t i;
 
-	mnl_set_sndbuffer(nl, batch);
-	nftnl_batch_iovec(batch, iov, iov_len);
+	mnl_set_sndbuffer(ctx->nf_sock, ctx->batch);
+	nftnl_batch_iovec(ctx->batch, iov, iov_len);
 
 	for (i = 0; i < iov_len; i++) {
 		if (debug_level & DEBUG_MNL) {
@@ -237,7 +236,7 @@ static ssize_t mnl_nft_socket_sendmsg(const struct mnl_socket *nl,
 		}
 	}
 
-	return sendmsg(mnl_socket_get_fd(nl), &msg, 0);
+	return sendmsg(mnl_socket_get_fd(ctx->nf_sock), &msg, 0);
 }
 
 int mnl_batch_talk(struct netlink_ctx *ctx, struct list_head *err_list)
@@ -251,7 +250,7 @@ int mnl_batch_talk(struct netlink_ctx *ctx, struct list_head *err_list)
 		.tv_usec	= 0
 	};
 
-	ret = mnl_nft_socket_sendmsg(nl, ctx->batch);
+	ret = mnl_nft_socket_sendmsg(ctx);
 	if (ret == -1)
 		return -1;
 
Signature · 1 line
2.1.4
author maintainer
#6 lore raw

PATCH 6/6 src: add debugging mask to context structure

In reply to Pablo Neira Ayuso

So this toggle is not global anymore. Update name that fits better with the semantics of this variable.

---
 include/erec.h            |  6 ++--
 include/expression.h      |  3 +-
 include/mnl.h             |  3 +-
 include/netlink.h         | 18 ++++++++----
 include/nftables.h        |  3 +-
 include/parser.h          |  3 +-
 include/proto.h           |  5 +++-
 include/rule.h            |  4 ++-
 src/cli.c                 |  5 ++--
 src/erec.c                |  9 +++---
 src/evaluate.c            | 43 ++++++++++++++++------------
 src/main.c                | 13 +++++----
 src/mnl.c                 | 12 ++++----
 src/netlink.c             | 72 +++++++++++++++++++++++++----------------------
 src/netlink_delinearize.c |  3 +-
 src/netlink_linearize.c   |  2 +-
 src/parser_bison.y        |  8 ++++--
 src/proto.c               | 14 +++++----
 src/rule.c                | 23 ++++++++-------
 src/segtree.c             | 27 ++++++++++--------
 20 files changed, 161 insertions(+), 115 deletions(-)
include/erec.h +4 -2
index 36e0efa4fc1d..223cb12d826a 100644
@@ -58,8 +58,10 @@ static inline void erec_queue(struct error_record *erec,
 	list_add_tail(&erec->list, queue);
 }
 
-extern void erec_print(FILE *f, const struct error_record *erec);
-extern void erec_print_list(FILE *f, struct list_head *list);
+extern void erec_print(FILE *f, const struct error_record *erec,
+		       unsigned int debug_mask);
+extern void erec_print_list(FILE *f, struct list_head *list,
+			    unsigned int debug_mask);
 
 struct eval_ctx;
 
include/expression.h +2 -1
index 828dbaee6338..32d4423a5e06 100644
@@ -413,7 +413,8 @@ extern struct expr *list_expr_alloc(const struct location *loc);
 extern struct expr *set_expr_alloc(const struct location *loc,
 				   const struct set *set);
 extern int set_to_intervals(struct list_head *msgs, struct set *set,
-			    struct expr *init, bool add);
+			    struct expr *init, bool add,
+			    unsigned int debug_mask);
 extern void interval_map_decompose(struct expr *set);
 
 extern struct expr *mapping_expr_alloc(const struct location *loc,
include/mnl.h +2 -1
index 72072f7f9a00..3df71467c108 100644
@@ -8,6 +8,7 @@
 struct mnl_ctx {
 	struct mnl_socket	*nf_sock;
 	unsigned int		seqnum;
+	unsigned int		debug_mask;
 };
 
 struct mnl_socket *netlink_open_sock(void);
@@ -97,7 +98,7 @@ int mnl_nft_obj_batch_del(struct nftnl_obj *nln, struct nftnl_batch *batch,
 
 struct nftnl_ruleset *mnl_nft_ruleset_dump(struct mnl_socket *nf_sock,
 					   uint32_t family, uint32_t seqnum);
-int mnl_nft_event_listener(struct mnl_socket *nf_sock,
+int mnl_nft_event_listener(struct mnl_ctx *ctx,
 			   int (*cb)(const struct nlmsghdr *nlh, void *data),
 			   void *cb_data);
 
include/netlink.h +13 -5
index 4bed0e0b1e94..41b87ff8c2c5 100644
@@ -19,6 +19,7 @@ struct netlink_parse_ctx {
 	struct rule		*rule;
 	struct stmt		*stmt;
 	struct expr		*registers[1 + NFT_REG32_15 - NFT_REG32_00 + 1];
+	unsigned int		debug_mask;
 };
 
 struct rule_pp_ctx {
@@ -39,6 +40,7 @@ extern const struct location netlink_location;
  * @data:	pointer to pass data to callback
  * @seqnum:	sequence number
  * @octx:	output context
+ * @debug:	display debugging information
  * @cache:	cache context
  */
 struct netlink_ctx {
@@ -50,6 +52,7 @@ struct netlink_ctx {
 	uint32_t		seqnum;
 	struct nftnl_batch	*batch;
 	bool			batch_supported;
+	unsigned int		debug_mask;
 	struct output_ctx	*octx;
 	struct nft_cache	*cache;
 };
@@ -176,11 +179,15 @@ extern int netlink_add_obj(struct netlink_ctx *ctx, const struct handle *h,
 extern int netlink_delete_obj(struct netlink_ctx *ctx, const struct handle *h,
 			      struct location *loc, uint32_t type);
 
-extern void netlink_dump_chain(const struct nftnl_chain *nlc);
-extern void netlink_dump_rule(const struct nftnl_rule *nlr);
-extern void netlink_dump_expr(const struct nftnl_expr *nle);
-extern void netlink_dump_set(const struct nftnl_set *nls);
-extern void netlink_dump_obj(struct nftnl_obj *nlo);
+extern void netlink_dump_chain(const struct nftnl_chain *nlc,
+			       unsigned int debug_mask);
+extern void netlink_dump_rule(const struct nftnl_rule *nlr,
+			      unsigned int debug_mask);
+extern void netlink_dump_expr(const struct nftnl_expr *nle,
+			      unsigned int debug_mask);
+extern void netlink_dump_set(const struct nftnl_set *nls,
+			     unsigned int debug_mask);
+extern void netlink_dump_obj(struct nftnl_obj *nlo, unsigned int debug_mask);
 
 extern int netlink_batch_send(struct netlink_ctx *ctx, struct list_head *err_list);
 
@@ -207,6 +214,7 @@ struct netlink_mon_handler {
 	uint32_t		format;
 	struct netlink_ctx	*ctx;
 	const struct location	*loc;
+	unsigned int		debug_mask;
 	bool			cache_needed;
 	struct nft_cache	*cache;
 };
include/nftables.h +1 -2
index 8858ad605516..c992d3023567 100644
@@ -42,13 +42,12 @@ struct nft_ctx {
 	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;
 };
 
-extern unsigned int debug_level;
-
 enum nftables_exit_codes {
 	NFT_EXIT_SUCCESS	= 0,
 	NFT_EXIT_FAILURE	= 1,
include/parser.h +2 -1
index df6026824584..0e266d60b8a3 100644
@@ -32,7 +32,8 @@ struct parser_state {
 struct mnl_socket;
 
 extern void parser_init(struct mnl_socket *nf_sock, struct nft_cache *cache,
-			struct parser_state *state, struct list_head *msgs);
+			struct parser_state *state, struct list_head *msgs,
+			unsigned int debug_level);
 extern int nft_parse(struct nft_ctx *ctx, void *, struct parser_state *state);
 
 extern void *scanner_init(struct parser_state *state);
include/proto.h +4 -1
index 39aa4850740c..9a9f9255f047 100644
@@ -130,6 +130,7 @@ extern const struct proto_desc *proto_dev_desc(uint16_t type);
 /**
  * struct proto_ctx - protocol context
  *
+ * debug_mask:	display debugging information
  * @family:	hook family
  * @location:	location of the relational expression defining the context
  * @desc:	protocol description for this layer
@@ -140,6 +141,7 @@ extern const struct proto_desc *proto_dev_desc(uint16_t type);
  * through a dependency.
  */
 struct proto_ctx {
+	unsigned int			debug_mask;
 	unsigned int			family;
 	struct {
 		struct location			location;
@@ -148,7 +150,8 @@ struct proto_ctx {
 	} protocol[PROTO_BASE_MAX + 1];
 };
 
-extern void proto_ctx_init(struct proto_ctx *ctx, unsigned int family);
+extern void proto_ctx_init(struct proto_ctx *ctx, unsigned int family,
+			   unsigned int debug_mask);
 extern void proto_ctx_update(struct proto_ctx *ctx, enum proto_bases base,
 			     const struct location *loc,
 			     const struct proto_desc *desc);
include/rule.h +3 -1
index 10ac0e26accc..04da000f6c79 100644
@@ -470,6 +470,7 @@ extern void cmd_free(struct cmd *cmd);
  * @set:	current set
  * @stmt:	current statement
  * @cache:	cache context
+ * @debug_mask: debugging bitmask
  * @ectx:	expression context
  * @pctx:	payload context
  */
@@ -482,6 +483,7 @@ struct eval_ctx {
 	struct set		*set;
 	struct stmt		*stmt;
 	struct nft_cache	*cache;
+	unsigned int		debug_mask;
 	struct expr_ctx		ectx;
 	struct proto_ctx	pctx;
 };
@@ -494,7 +496,7 @@ struct netlink_ctx;
 extern int do_command(struct netlink_ctx *ctx, struct cmd *cmd);
 
 extern int cache_update(struct mnl_socket *nf_sock, struct nft_cache *cache,
-			 enum cmd_ops cmd, struct list_head *msgs);
+			 enum cmd_ops cmd, struct list_head *msgs, bool debug);
 extern void cache_flush(struct list_head *table_list);
 extern void cache_release(struct nft_cache *cache);
 
src/cli.c +3 -2
index 4f05f276542e..d923ff7d3617 100644
@@ -134,10 +134,11 @@ static void cli_complete(char *line)
 	xfree(line);
 	line = s;
 
-	parser_init(cli_nf_sock, &cli_nft->cache, state, &msgs);
+	parser_init(cli_nf_sock, &cli_nft->cache, state, &msgs,
+		    cli_nft->debug_mask);
 	scanner_push_buffer(scanner, &indesc_cli, line);
 	nft_run(cli_nft, cli_nf_sock, scanner, state, &msgs);
-	erec_print_list(stdout, &msgs);
+	erec_print_list(stdout, &msgs, cli_nft->debug_mask);
 	xfree(line);
 	cache_release(&cli_nft->cache);
 	iface_cache_release();
src/erec.c +5 -4
index 439add97c4e0..b5964465fbf3 100644
@@ -112,7 +112,8 @@ struct error_record *erec_create(enum error_record_types type,
 	return erec;
 }
 
-void erec_print(FILE *f, const struct error_record *erec)
+void erec_print(FILE *f, const struct error_record *erec,
+		unsigned int debug_mask)
 {
 	const struct location *loc = erec->locations, *iloc;
 	const struct input_descriptor *indesc = loc->indesc, *tmp;
@@ -153,7 +154,7 @@ void erec_print(FILE *f, const struct error_record *erec)
 		fprintf(f, "%s\n", erec->msg);
 		for (l = 0; l < (int)erec->num_locations; l++) {
 			loc = &erec->locations[l];
-			netlink_dump_expr(loc->nle);
+			netlink_dump_expr(loc->nle, debug_mask);
 		}
 		fprintf(f, "\n");
 	} else {
@@ -202,13 +203,13 @@ void erec_print(FILE *f, const struct error_record *erec)
 	fprintf(f, "\n");
 }
 
-void erec_print_list(FILE *f, struct list_head *list)
+void erec_print_list(FILE *f, struct list_head *list, unsigned int debug_mask)
 {
 	struct error_record *erec, *next;
 
 	list_for_each_entry_safe(erec, next, list, list) {
 		list_del(&erec->list);
-		erec_print(f, erec);
+		erec_print(f, erec, debug_mask);
 		erec_destroy(erec);
 	}
 }
src/evaluate.c +25 -18

143 lines not on this page · raw message

src/main.c +7 -6
index 4c74bdce2824..3519377b6e2c 100644
@@ -29,7 +29,6 @@
 #include <cli.h>
 
 static struct nft_ctx nft;
-unsigned int debug_level;
 
 enum opt_vals {
 	OPT_HELP		= 'h',
@@ -200,6 +199,7 @@ static int nft_netlink(struct nft_ctx *nft,
 		ctx.octx = &nft->output;
 		ctx.nf_sock = nf_sock;
 		ctx.cache = &nft->cache;
+		ctx.debug_mask = nft->debug_mask;
 		init_list_head(&ctx.list);
 		ret = do_command(&ctx, cmd);
 		if (ret < 0)
@@ -363,7 +363,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;
-					debug_level |= debug_param[i].level;
+					nft.debug_mask |= debug_param[i].level;
 					break;
 				}
 
@@ -400,15 +400,16 @@ int main(int argc, char * const *argv)
 				strcat(buf, " ");
 		}
 		strcat(buf, "\n");
-		parser_init(nf_sock, &nft.cache, &state, &msgs);
+		parser_init(nf_sock, &nft.cache, &state, &msgs, nft.debug_mask);
 		scanner = scanner_init(&state);
 		scanner_push_buffer(scanner, &indesc_cmdline, buf);
 	} else if (filename != NULL) {
-		rc = cache_update(nf_sock, &nft.cache, CMD_INVALID, &msgs);
+		rc = cache_update(nf_sock, &nft.cache, CMD_INVALID, &msgs,
+				  nft.debug_mask);
 		if (rc < 0)
 			return rc;
 
-		parser_init(nf_sock, &nft.cache, &state, &msgs);
+		parser_init(nf_sock, &nft.cache, &state, &msgs, nft.debug_mask);
 		scanner = scanner_init(&state);
 		if (scanner_read_file(scanner, filename, &internal_location) < 0)
 			goto out;
@@ -428,7 +429,7 @@ int main(int argc, char * const *argv)
 		rc = NFT_EXIT_FAILURE;
 out:
 	scanner_destroy(scanner);
-	erec_print_list(stderr, &msgs);
+	erec_print_list(stderr, &msgs, nft.debug_mask);
 	xfree(buf);
 	cache_release(&nft.cache);
 	iface_cache_release();
src/mnl.c +6 -6
index e51c3294f76d..a770dc567d9f 100644
@@ -71,7 +71,7 @@ nft_mnl_talk(struct mnl_ctx *ctx, const void *data, unsigned int len,
 {
 	uint32_t portid = mnl_socket_get_portid(ctx->nf_sock);
 
-	if (debug_level & DEBUG_MNL)
+	if (ctx->debug_mask & DEBUG_MNL)
 		mnl_nlmsg_fprintf(stdout, data, len, sizeof(struct nfgenmsg));
 
 	if (mnl_socket_sendto(ctx->nf_sock, data, len) < 0)
@@ -229,7 +229,7 @@ static ssize_t mnl_nft_socket_sendmsg(const struct netlink_ctx *ctx)
 	nftnl_batch_iovec(ctx->batch, iov, iov_len);
 
 	for (i = 0; i < iov_len; i++) {
-		if (debug_level & DEBUG_MNL) {
+		if (ctx->debug_mask & DEBUG_MNL) {
 			mnl_nlmsg_fprintf(stdout,
 					  iov[i].iov_base, iov[i].iov_len,
 					  sizeof(struct nfgenmsg));
@@ -1071,7 +1071,7 @@ err:
  */
 #define NFTABLES_NLEVENT_BUFSIZ	(1 << 24)
 
-int mnl_nft_event_listener(struct mnl_socket *nf_sock,
+int mnl_nft_event_listener(struct mnl_ctx *ctx,
 			   int (*cb)(const struct nlmsghdr *nlh, void *data),
 			   void *cb_data)
 {
@@ -1079,7 +1079,7 @@ int mnl_nft_event_listener(struct mnl_socket *nf_sock,
  	 * message loss due to ENOBUFS.
 	 */
 	unsigned int bufsiz = NFTABLES_NLEVENT_BUFSIZ;
-	int fd = mnl_socket_get_fd(nf_sock);
+	int fd = mnl_socket_get_fd(ctx->nf_sock);
 	char buf[NFT_NLMSG_MAXSIZE];
 	fd_set readfds;
 	int ret;
@@ -1105,7 +1105,7 @@ int mnl_nft_event_listener(struct mnl_socket *nf_sock,
 			return -1;
 
 		if (FD_ISSET(fd, &readfds)) {
-			ret = mnl_socket_recvfrom(nf_sock, buf, sizeof(buf));
+			ret = mnl_socket_recvfrom(ctx->nf_sock, buf, sizeof(buf));
 			if (ret < 0) {
 				if (errno == ENOBUFS) {
 					printf("# ERROR: We lost some netlink events!\n");
@@ -1116,7 +1116,7 @@ int mnl_nft_event_listener(struct mnl_socket *nf_sock,
 			}
 		}
 
-		if (debug_level & DEBUG_MNL) {
+		if (ctx->debug_mask & DEBUG_MNL) {
 			mnl_nlmsg_fprintf(stdout, buf, sizeof(buf),
 					  sizeof(struct nfgenmsg));
 		}
src/netlink.c +39 -33

257 lines not on this page · raw message

src/netlink_delinearize.c +2 -1
index 5317a830ac6d..875690ff2a9c 100644
@@ -2184,7 +2184,7 @@ static void rule_parse_postprocess(struct netlink_parse_ctx *ctx, struct rule *r
 	struct stmt *stmt, *next;
 
 	memset(&rctx, 0, sizeof(rctx));
-	proto_ctx_init(&rctx.pctx, rule->handle.family);
+	proto_ctx_init(&rctx.pctx, rule->handle.family, ctx->debug_mask);
 
 	list_for_each_entry_safe(stmt, next, &rule->stmts, list) {
 		enum stmt_types type = stmt->ops->type;
@@ -2296,6 +2296,7 @@ struct rule *netlink_delinearize_rule(struct netlink_ctx *ctx,
 
 	memset(&_ctx, 0, sizeof(_ctx));
 	_ctx.msgs = ctx->msgs;
+	_ctx.debug_mask = ctx->debug_mask;
 
 	memset(&h, 0, sizeof(h));
 	h.family = nftnl_rule_get_u32(nlr, NFTNL_RULE_FAMILY);
src/netlink_linearize.c +1 -1
index 3d684569cabf..c1fe6b010781 100644
@@ -1308,5 +1308,5 @@ void netlink_linearize_rule(struct netlink_ctx *ctx, struct nftnl_rule *nlr,
 		nftnl_udata_buf_free(udata);
 	}
 
-	netlink_dump_rule(nlr);
+	netlink_dump_rule(nlr, ctx->debug_mask);
 }
src/parser_bison.y +5 -3
index 76a0115d46a2..ab291467e8fe 100644
@@ -36,7 +36,8 @@
 #include "parser_bison.h"
 
 void parser_init(struct mnl_socket *nf_sock, struct nft_cache *cache,
-		 struct parser_state *state, struct list_head *msgs)
+		 struct parser_state *state, struct list_head *msgs,
+		 unsigned int debug_mask)
 {
 	memset(state, 0, sizeof(*state));
 	init_list_head(&state->cmds);
@@ -46,6 +47,7 @@ void parser_init(struct mnl_socket *nf_sock, struct nft_cache *cache,
 	state->ectx.cache = cache;
 	state->ectx.msgs = msgs;
 	state->ectx.nf_sock = nf_sock;
+	state->ectx.debug_mask = debug_mask;
 }
 
 static void yyerror(struct location *loc, struct nft_ctx *nft, void *scanner,
@@ -118,9 +120,9 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 
 %initial-action {
 	location_init(scanner, state, &yylloc);
-	if (debug_level & DEBUG_SCANNER)
+	if (nft->debug_mask & DEBUG_SCANNER)
 		nft_set_debug(1, scanner);
-	if (debug_level & DEBUG_PARSER)
+	if (nft->debug_mask & DEBUG_PARSER)
 		yydebug = 1;
 }
 
src/proto.c +9 -5
index 045f2d811969..d69e9a30de78 100644
@@ -138,11 +138,12 @@ const struct hook_proto_desc hook_proto_desc[] = {
 	[NFPROTO_ARP]		= HOOK_PROTO_DESC(PROTO_BASE_NETWORK_HDR, &proto_arp),
 };
 
-static void proto_ctx_debug(const struct proto_ctx *ctx, enum proto_bases base)
+static void proto_ctx_debug(const struct proto_ctx *ctx, enum proto_bases base,
+			    unsigned int debug_mask)
 {
 	unsigned int i;
 
-	if (!(debug_level & DEBUG_PROTO_CTX))
+	if (!(debug_mask & DEBUG_NETLINK))
 		return;
 
 	pr_debug("update %s protocol context:\n", proto_base_names[base]);
@@ -165,16 +166,19 @@ static void proto_ctx_debug(const struct proto_ctx *ctx, enum proto_bases base)
  *
  * @ctx:	protocol context
  * @family:	hook family
+ * @debug:	display debugging information
  */
-void proto_ctx_init(struct proto_ctx *ctx, unsigned int family)
+void proto_ctx_init(struct proto_ctx *ctx, unsigned int family,
+		    unsigned int debug_mask)
 {
 	const struct hook_proto_desc *h = &hook_proto_desc[family];
 
 	memset(ctx, 0, sizeof(*ctx));
 	ctx->family = family;
 	ctx->protocol[h->base].desc = h->desc;
+	ctx->debug_mask = debug_mask;
 
-	proto_ctx_debug(ctx, h->base);
+	proto_ctx_debug(ctx, h->base, debug_mask);
 }
 
 /**
@@ -192,7 +196,7 @@ void proto_ctx_update(struct proto_ctx *ctx, enum proto_bases base,
 	ctx->protocol[base].location	= *loc;
 	ctx->protocol[base].desc	= desc;
 
-	proto_ctx_debug(ctx, base);
+	proto_ctx_debug(ctx, base, ctx->debug_mask);
 }
 
 #define HDR_TEMPLATE(__name, __dtype, __type, __member)			\
src/rule.c +13 -10
index ef12becdea94..140855f51757 100644
@@ -122,7 +122,8 @@ static int cache_init_objects(struct netlink_ctx *ctx, enum cmd_ops cmd)
 }
 
 static int cache_init(struct mnl_socket *nf_sock, struct nft_cache *cache,
-		      enum cmd_ops cmd, struct list_head *msgs)
+		      enum cmd_ops cmd, struct list_head *msgs,
+		      unsigned int debug_mask)
 {
 	struct handle handle = {
 		.family = NFPROTO_UNSPEC,
@@ -136,6 +137,7 @@ static int cache_init(struct mnl_socket *nf_sock, struct nft_cache *cache,
 	ctx.cache = cache;
 	ctx.msgs = msgs;
 	ctx.seqnum = cache->seqnum++;
+	ctx.debug_mask = debug_mask;
 
 	ret = cache_init_tables(&ctx, &handle, cache);
 	if (ret < 0)
@@ -148,7 +150,7 @@ static int cache_init(struct mnl_socket *nf_sock, struct nft_cache *cache,
 }
 
 int cache_update(struct mnl_socket *nf_sock, struct nft_cache *cache,
-		 enum cmd_ops cmd, struct list_head *msgs)
+		 enum cmd_ops cmd, struct list_head *msgs, bool debug)
 {
 	int ret;
 
@@ -156,7 +158,7 @@ int cache_update(struct mnl_socket *nf_sock, struct nft_cache *cache,
 		return 0;
 replay:
 	netlink_genid_get(nf_sock, cache->seqnum++);
-	ret = cache_init(nf_sock, cache, cmd, msgs);
+	ret = cache_init(nf_sock, cache, cmd, msgs, debug);
 	if (ret < 0) {
 		cache_release(cache);
 		if (errno == EINTR) {
@@ -991,7 +993,7 @@ static int do_add_setelems(struct netlink_ctx *ctx, const struct handle *h,
 	set = set_lookup(table, h->set);
 
 	if (set->flags & NFT_SET_INTERVAL &&
-	    set_to_intervals(ctx->msgs, set, init, true) < 0)
+	    set_to_intervals(ctx->msgs, set, init, true, ctx->debug_mask) < 0)
 		return -1;
 
 	return __do_add_setelems(ctx, h, set, init, flags);
@@ -1002,7 +1004,8 @@ static int do_add_set(struct netlink_ctx *ctx, const struct handle *h,
 {
 	if (set->init != NULL) {
 		if (set->flags & NFT_SET_INTERVAL &&
-		    set_to_intervals(ctx->msgs, set, set->init, true) < 0)
+		    set_to_intervals(ctx->msgs, set, set->init, true,
+				     ctx->debug_mask) < 0)
 			return -1;
 	}
 	if (netlink_add_set(ctx, h, set, flags) < 0)
@@ -1021,8 +1024,8 @@ static int do_command_add(struct netlink_ctx *ctx, struct cmd *cmd, bool excl)
 	if (ctx->octx->echo) {
 		int ret;
 
-		ret = cache_update(ctx->nf_sock, ctx->cache,
-				  cmd->obj, ctx->msgs);
+		ret = cache_update(ctx->nf_sock, ctx->cache, cmd->obj,
+				   ctx->msgs, ctx->debug_mask);
 		if (ret < 0)
 			return ret;
 
@@ -1072,8 +1075,8 @@ static int do_command_insert(struct netlink_ctx *ctx, struct cmd *cmd)
 	if (ctx->octx->echo) {
 		int ret;
 
-		ret = cache_update(ctx->nf_sock, ctx->cache,
-				  cmd->obj, ctx->msgs);
+		ret = cache_update(ctx->nf_sock, ctx->cache, cmd->obj,
+				   ctx->msgs, ctx->debug_mask);
 		if (ret < 0)
 			return ret;
 
@@ -1100,7 +1103,7 @@ static int do_delete_setelems(struct netlink_ctx *ctx, const struct handle *h,
 	set = set_lookup(table, h->set);
 
 	if (set->flags & NFT_SET_INTERVAL &&
-	    set_to_intervals(ctx->msgs, set, expr, false) < 0)
+	    set_to_intervals(ctx->msgs, set, expr, false, ctx->debug_mask) < 0)
 		return -1;
 
 	if (netlink_delete_setelems(ctx, h, expr) < 0)
src/segtree.c +15 -12
index 8623e862cf77..f81e117421a1 100644
@@ -25,6 +25,7 @@
  * @type:	the datatype of the dimension
  * @dwidth:	width of the dimension
  * @byteorder:	byteorder of elements
+ * @debug_mask:	display debugging information
  */
 struct seg_tree {
 	struct rb_root			root;
@@ -33,6 +34,7 @@ struct seg_tree {
 	const struct datatype		*datatype;
 	unsigned int			datalen;
 	enum byteorder			byteorder;
+	unsigned int			debug_mask;
 };
 
 enum elementary_interval_flags {
@@ -68,7 +70,7 @@ struct elementary_interval {
 static struct output_ctx debug_octx = {};
 
 static void seg_tree_init(struct seg_tree *tree, const struct set *set,
-			  struct expr *init)
+			  struct expr *init, unsigned int debug_mask)
 {
 	struct expr *first;
 
@@ -79,6 +81,7 @@ static void seg_tree_init(struct seg_tree *tree, const struct set *set,
 	tree->datatype	= set->datatype;
 	tree->datalen	= set->datalen;
 	tree->byteorder	= first->byteorder;
+	tree->debug_mask = debug_mask;
 }
 
 static struct elementary_interval *ei_alloc(const mpz_t left, const mpz_t right,
@@ -161,9 +164,9 @@ static void __ei_insert(struct seg_tree *tree, struct elementary_interval *new)
 	rb_insert_color(&new->rb_node, &tree->root);
 }
 
-static bool segtree_debug(void)
+static bool segtree_debug(unsigned int debug_mask)
 {
-	if (debug_level & DEBUG_SEGTREE)
+	if (debug_mask & DEBUG_SEGTREE)
 		return true;
 
 	return false;
@@ -192,7 +195,7 @@ static void ei_insert(struct seg_tree *tree, struct elementary_interval *new)
 	lei = ei_lookup(tree, new->left);
 	rei = ei_lookup(tree, new->right);
 
-	if (segtree_debug())
+	if (segtree_debug(tree->debug_mask))
 		pr_gmp_debug("insert: [%Zx %Zx]\n", new->left, new->right);
 
 	if (lei != NULL && rei != NULL && lei == rei) {
@@ -202,7 +205,7 @@ static void ei_insert(struct seg_tree *tree, struct elementary_interval *new)
 		 *
 		 * [lei_left, new_left) and (new_right, rei_right]
 		 */
-		if (segtree_debug())
+		if (segtree_debug(tree->debug_mask))
 			pr_gmp_debug("split [%Zx %Zx]\n", lei->left, lei->right);
 
 		ei_remove(tree, lei);
@@ -222,7 +225,7 @@ static void ei_insert(struct seg_tree *tree, struct elementary_interval *new)
 			 *
 			 * [lei_left, new_left)[new_left, new_right]
 			 */
-			if (segtree_debug()) {
+			if (segtree_debug(tree->debug_mask)) {
 				pr_gmp_debug("adjust left [%Zx %Zx]\n",
 					     lei->left, lei->right);
 			}
@@ -240,7 +243,7 @@ static void ei_insert(struct seg_tree *tree, struct elementary_interval *new)
 			 *
 			 * [new_left, new_right](new_right, rei_right]
 			 */
-			if (segtree_debug()) {
+			if (segtree_debug(tree->debug_mask)) {
 				pr_gmp_debug("adjust right [%Zx %Zx]\n",
 					     rei->left, rei->right);
 			}
@@ -461,7 +464,7 @@ static void segtree_linearize(struct list_head *list, const struct set *set,
 	 * Convert the tree of open intervals to half-closed map expressions.
 	 */
 	rb_for_each_entry_safe(ei, node, next, &tree->root, rb_node) {
-		if (segtree_debug())
+		if (segtree_debug(tree->debug_mask))
 			pr_gmp_debug("iter: [%Zx %Zx]\n", ei->left, ei->right);
 
 		if (prev == NULL) {
@@ -547,20 +550,20 @@ static void set_insert_interval(struct expr *set, struct seg_tree *tree,
 }
 
 int set_to_intervals(struct list_head *errs, struct set *set,
-		     struct expr *init, bool add)
+		     struct expr *init, bool add, unsigned int debug_mask)
 {
 	struct elementary_interval *ei, *next;
 	struct seg_tree tree;
 	LIST_HEAD(list);
 
-	seg_tree_init(&tree, set, init);
+	seg_tree_init(&tree, set, init, debug_mask);
 	if (set_to_segtree(errs, set, init, &tree, add) < 0)
 		return -1;
 	segtree_linearize(&list, set, init, &tree, add);
 
 	init->size = 0;
 	list_for_each_entry_safe(ei, next, &list, list) {
-		if (segtree_debug()) {
+		if (segtree_debug(tree.debug_mask)) {
 			pr_gmp_debug("list: [%.*Zx %.*Zx]\n",
 				     2 * tree.keylen / BITS_PER_BYTE, ei->left,
 				     2 * tree.keylen / BITS_PER_BYTE, ei->right);
@@ -569,7 +572,7 @@ int set_to_intervals(struct list_head *errs, struct set *set,
 		ei_destroy(ei);
 	}
 
-	if (segtree_debug()) {
+	if (segtree_debug(tree.debug_mask)) {
 		expr_print(init, &debug_octx);
 		pr_gmp_debug("\n");
 	}
Signature · 1 line
2.1.4