Skip to contentSkip to conversation

src: Pass stateless, numeric, ip2name and handle variables as structure members.

PATCH Superseded Varsha Rao 2 participants 4 messages Jun 14, 2017 – Jun 15, 2017 netfilter

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Varsha Rao

author
#0 lore raw

PATCH 1/1 src: Pass stateless, numeric, ip2name and handle variables as structure members.

libnftables library will be created soon. So, declare numeric_output, stateless_output, ip2name_output and handle_output as members of structure print_ctx, instead of global variables.

---
 include/datatype.h   |   8 +--
 include/expression.h |   7 +--
 include/netlink.h    |   1 +
 include/nftables.h   |  15 ++++--
 include/rule.h       |   9 ++--
 include/statement.h  |   5 +-
 src/cli.c            |   2 +-
 src/ct.c             |  10 ++--
 src/datatype.c       |  51 +++++++++---------
 src/evaluate.c       |  72 ++++++++++++++------------
 src/expression.c     |  84 +++++++++++++++---------------
 src/exthdr.c         |   2 +-
 src/fib.c            |   2 +-
 src/hash.c           |   4 +-
 src/main.c           |  25 +++++----
 src/meta.c           |  34 ++++++------
 src/netlink.c        |  38 ++++++++------
 src/numgen.c         |   2 +-
 src/payload.c        |   8 +--
 src/proto.c          |   4 +-
 src/rt.c             |   6 +--
 src/rule.c           | 142 +++++++++++++++++++++++++++------------------------
 src/segtree.c        |   4 +-
 src/statement.c      |  88 +++++++++++++++----------------
 24 files changed, 327 insertions(+), 296 deletions(-)
include/datatype.h +5 -3
index 04b7d88..748688e 100644
@@ -145,7 +145,8 @@ struct datatype {
 	const char			*desc;
 	const struct datatype		*basetype;
 	const char			*basefmt;
-	void				(*print)(const struct expr *expr);
+	void				(*print)(const struct expr *expr,
+						 struct print_ctx *ct);
 	struct error_record		*(*parse)(const struct expr *sym,
 						  struct expr **res);
 	const struct symbol_table	*sym_tbl;
@@ -157,7 +158,7 @@ extern const struct datatype *datatype_lookup_byname(const char *name);
 
 extern struct error_record *symbol_parse(const struct expr *sym,
 					 struct expr **res);
-extern void datatype_print(const struct expr *expr);
+extern void datatype_print(const struct expr *expr, struct print_ctx *ct);
 
 static inline bool datatype_equal(const struct datatype *d1,
 				  const struct datatype *d2)
@@ -205,7 +206,8 @@ extern struct error_record *symbolic_constant_parse(const struct expr *sym,
 						    const struct symbol_table *tbl,
 						    struct expr **res);
 extern void symbolic_constant_print(const struct symbol_table *tbl,
-				    const struct expr *expr, bool quotes);
+				    const struct expr *expr, bool quotes,
+				    struct print_ctx *ct);
 extern void symbol_table_print(const struct symbol_table *tbl,
 			       const struct datatype *dtype,
 			       enum byteorder byteorder);
include/expression.h +4 -3
index 9ba87e8..80f91d0 100644
@@ -157,7 +157,8 @@ struct expr_ops {
 	void			(*set_type)(const struct expr *expr,
 					    const struct datatype *dtype,
 					    enum byteorder byteorder);
-	void			(*print)(const struct expr *expr);
+	void			(*print)(const struct expr *expr,
+					 struct print_ctx *ct);
 	bool			(*cmp)(const struct expr *e1,
 				       const struct expr *e2);
 	void			(*pctx_update)(struct proto_ctx *ctx,
@@ -330,7 +331,7 @@ extern struct expr *expr_alloc(const struct location *loc,
 extern struct expr *expr_clone(const struct expr *expr);
 extern struct expr *expr_get(struct expr *expr);
 extern void expr_free(struct expr *expr);
-extern void expr_print(const struct expr *expr);
+extern void expr_print(const struct expr *expr, struct print_ctx *ct);
 extern bool expr_cmp(const struct expr *e1, const struct expr *e2);
 extern void expr_describe(const struct expr *expr);
 
@@ -410,7 +411,7 @@ extern struct expr *list_expr_alloc(const struct location *loc);
 
 extern struct expr *set_expr_alloc(const struct location *loc);
 extern int set_to_intervals(struct list_head *msgs, struct set *set,
-			    struct expr *init, bool add);
+			    struct expr *init, bool add, struct print_ctx *ct);
 extern void interval_map_decompose(struct expr *set);
 
 extern struct expr *mapping_expr_alloc(const struct location *loc,
include/netlink.h +1 -0
index 81538ff..0c7cd90 100644
@@ -213,6 +213,7 @@ struct netlink_mon_handler {
 	struct netlink_ctx	*ctx;
 	const struct location	*loc;
 	bool			cache_needed;
+	struct print_ctx	*ct;
 };
 
 extern int netlink_monitor(struct netlink_mon_handler *monhandler);
include/nftables.h +10 -5
index 6f54155..1c747d6 100644
@@ -24,12 +24,16 @@ enum debug_level {
 
 #define INCLUDE_PATHS_MAX	16
 
+struct print_ctx {
+	unsigned int numeric_output;
+	unsigned int stateless_output;
+	unsigned int ip2name_output;
+	unsigned int handle_output;
+};
+
 extern unsigned int max_errors;
-extern unsigned int numeric_output;
-extern unsigned int stateless_output;
-extern unsigned int ip2name_output;
-extern unsigned int handle_output;
 extern unsigned int debug_level;
+
 extern const char *include_paths[INCLUDE_PATHS_MAX];
 
 enum nftables_exit_codes {
@@ -107,6 +111,7 @@ struct input_descriptor {
 
 struct parser_state;
 
-int nft_run(void *scanner, struct parser_state *state, struct list_head *msgs);
+int nft_run(void *scanner, struct parser_state *state, struct list_head *msgs,
+	     struct print_ctx *ct);
 
 #endif /* NFTABLES_NFTABLES_H */
include/rule.h +5 -4
index fb46064..48cb5f1 100644
@@ -195,7 +195,7 @@ struct rule {
 extern struct rule *rule_alloc(const struct location *loc,
 			       const struct handle *h);
 extern void rule_free(struct rule *rule);
-extern void rule_print(const struct rule *rule);
+extern void rule_print(const struct rule *rule, struct print_ctx *ct);
 extern struct rule *rule_lookup(const struct chain *chain, uint64_t handle);
 
 /**
@@ -244,7 +244,7 @@ extern void set_add_hash(struct set *set, struct table *table);
 extern struct set *set_lookup(const struct table *table, const char *name);
 extern struct set *set_lookup_global(uint32_t family, const char *table,
 				     const char *name);
-extern void set_print(const struct set *set);
+extern void set_print(const struct set *set, struct print_ctx *ct);
 extern void set_print_plain(const struct set *s);
 
 #include <statement.h>
@@ -292,7 +292,7 @@ void obj_free(struct obj *obj);
 void obj_add_hash(struct obj *obj, struct table *table);
 struct obj *obj_lookup(const struct table *table, const char *name,
 		       uint32_t type);
-void obj_print(const struct obj *n);
+void obj_print(const struct obj *n, struct print_ctx *ct);
 void obj_print_plain(const struct obj *obj);
 const char *obj_type_name(uint32_t type);
 
@@ -475,7 +475,8 @@ extern int cmd_evaluate(struct eval_ctx *ctx, struct cmd *cmd);
 extern struct error_record *rule_postprocess(struct rule *rule);
 
 struct netlink_ctx;
-extern int do_command(struct netlink_ctx *ctx, struct cmd *cmd);
+extern int do_command(struct netlink_ctx *ctx, struct cmd *cmd,
+		       struct print_ctx *ct);
 
 extern int cache_update(enum cmd_ops cmd, struct list_head *msgs);
 extern void cache_flush(void);
include/statement.h +3 -2
index 317d53e..d6ebc01 100644
@@ -261,7 +261,8 @@ struct stmt_ops {
 	enum stmt_types		type;
 	const char		*name;
 	void			(*destroy)(struct stmt *stmt);
-	void			(*print)(const struct stmt *stmt);
+	void			(*print)(const struct stmt *stmt,
+					 struct print_ctx *ct);
 };
 
 enum stmt_flags {
@@ -312,7 +313,7 @@ extern struct stmt *stmt_alloc(const struct location *loc,
 int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt);
 extern void stmt_free(struct stmt *stmt);
 extern void stmt_list_free(struct list_head *list);
-extern void stmt_print(const struct stmt *stmt);
+extern void stmt_print(const struct stmt *stmt, struct print_ctx *ct);
 
 const char *get_rate(uint64_t byte_rate, uint64_t *rate);
 
src/cli.c +1 -1
index a74411a..c91eced 100644
@@ -129,7 +129,7 @@ static void cli_complete(char *line)
 
 	parser_init(state, &msgs);
 	scanner_push_buffer(scanner, &indesc_cli, line);
-	nft_run(scanner, state, &msgs);
+	nft_run(scanner, state, &msgs, NULL);
 	erec_print_list(stdout, &msgs);
 	xfree(line);
 	cache_release();
src/ct.c +5 -5
index ab50a16..46628cb 100644
@@ -132,7 +132,7 @@ static struct symbol_table *ct_label_tbl;
 
 #define CT_LABEL_BIT_SIZE 128
 
-static void ct_label_type_print(const struct expr *expr)
+static void ct_label_type_print(const struct expr *expr, struct print_ctx *ct)
 {
 	unsigned long bit = mpz_scan1(expr->value, 0);
 	const struct symbolic_constant *s;
@@ -286,7 +286,7 @@ static void ct_print(enum nft_ct_keys key, int8_t dir)
 	printf("%s", ct_templates[key].token);
 }
 
-static void ct_expr_print(const struct expr *expr)
+static void ct_expr_print(const struct expr *expr, struct print_ctx *ct)
 {
 	ct_print(expr->ct.key, expr->ct.direction);
 }
@@ -442,11 +442,11 @@ void ct_expr_update_type(struct proto_ctx *ctx, struct expr *expr)
 	}
 }
 
-static void ct_stmt_print(const struct stmt *stmt)
+static void ct_stmt_print(const struct stmt *stmt, struct print_ctx *ct)
 {
 	ct_print(stmt->ct.key, stmt->ct.direction);
 	printf(" set ");
-	expr_print(stmt->ct.expr);
+	expr_print(stmt->ct.expr, ct);
 }
 
 static const struct stmt_ops ct_stmt_ops = {
@@ -469,7 +469,7 @@ struct stmt *ct_stmt_alloc(const struct location *loc, enum nft_ct_keys key,
 	return stmt;
 }
 
-static void notrack_stmt_print(const struct stmt *stmt)
+static void notrack_stmt_print(const struct stmt *stmt, struct print_ctx *ct)
 {
 	printf("notrack");
 }
src/datatype.c +26 -25

171 lines not on this page · raw message

src/evaluate.c +38 -34

278 lines not on this page · raw message

src/expression.c +43 -41

244 lines not on this page · raw message

src/exthdr.c +1 -1
index c8599f2..51cd342 100644
@@ -22,7 +22,7 @@
 #include <headers.h>
 #include <expression.h>
 
-static void exthdr_expr_print(const struct expr *expr)
+static void exthdr_expr_print(const struct expr *expr, struct print_ctx *ct)
 {
 	if (expr->exthdr.op == NFT_EXTHDR_OP_TCPOPT) {
 		/* Offset calcualtion is a bit hacky at this point.
src/fib.c +1 -1
index 28ef4b5..411ccff 100644
@@ -71,7 +71,7 @@ static void __fib_expr_print_f(unsigned int *flags, unsigned int f, const char *
 		printf(" . ");
 }
 
-static void fib_expr_print(const struct expr *expr)
+static void fib_expr_print(const struct expr *expr, struct print_ctx *ct)
 {
 	unsigned int flags = expr->fib.flags & ~NFTA_FIB_F_PRESENT;
 
src/hash.c +2 -2
index c738d0b..7424481 100644
@@ -15,7 +15,7 @@
 #include <hash.h>
 #include <utils.h>
 
-static void hash_expr_print(const struct expr *expr)
+static void hash_expr_print(const struct expr *expr, struct print_ctx *ct)
 {
 	switch (expr->hash.type) {
 	case NFT_HASH_SYM:
@@ -24,7 +24,7 @@ static void hash_expr_print(const struct expr *expr)
 	case NFT_HASH_JENKINS:
 	default:
 		printf("jhash ");
-		expr_print(expr->hash.expr);
+		expr_print(expr->hash.expr, ct);
 	}
 
 	printf(" mod %u", expr->hash.mod);
src/main.c +12 -13
index 5089ff2..e139cad 100644
@@ -28,11 +28,8 @@
 #include <iface.h>
 #include <cli.h>
 
+struct print_ctx ct;
 unsigned int max_errors = 10;
-unsigned int numeric_output;
-unsigned int stateless_output;
-unsigned int ip2name_output;
-unsigned int handle_output;
 #ifdef DEBUG
 unsigned int debug_level;
 #endif
@@ -178,7 +175,8 @@ static const struct input_descriptor indesc_cmdline = {
 	.name	= "<cmdline>",
 };
 
-static int nft_netlink(struct parser_state *state, struct list_head *msgs)
+static int nft_netlink(struct parser_state *state, struct list_head *msgs,
+			struct print_ctx *ct)
 {
 	struct nftnl_batch *batch;
 	struct netlink_ctx ctx;
@@ -199,7 +197,7 @@ static int nft_netlink(struct parser_state *state, struct list_head *msgs)
 		ctx.batch = batch;
 		ctx.batch_supported = batch_supported;
 		init_list_head(&ctx.list);
-		ret = do_command(&ctx, cmd);
+		ret = do_command(&ctx, cmd, ct);
 		if (ret < 0)
 			goto out;
 	}
@@ -231,7 +229,8 @@ out:
 	return ret;
 }
 
-int nft_run(void *scanner, struct parser_state *state, struct list_head *msgs)
+int nft_run(void *scanner, struct parser_state *state, struct list_head *msgs,
+	     struct print_ctx *ct)
 {
 	struct cmd *cmd, *next;
 	int ret;
@@ -241,7 +240,7 @@ int nft_run(void *scanner, struct parser_state *state, struct list_head *msgs)
 		ret = -1;
 		goto err1;
 	}
-	ret = nft_netlink(state, msgs);
+	ret = nft_netlink(state, msgs, ct);
 err1:
 	list_for_each_entry_safe(cmd, next, &state->cmds, list) {
 		list_del(&cmd->list);
@@ -290,7 +289,7 @@ int main(int argc, char * const *argv)
 			include_paths[num_include_paths++] = optarg;
 			break;
 		case OPT_NUMERIC:
-			if (++numeric_output > NUMERIC_ALL) {
+			if (++(ct.numeric_output) > NUMERIC_ALL) {
 				fprintf(stderr, "Too many numeric options "
 						"used, max. %u\n",
 					NUMERIC_ALL);
@@ -298,10 +297,10 @@ int main(int argc, char * const *argv)
 			}
 			break;
 		case OPT_STATELESS:
-			stateless_output++;
+			ct.stateless_output++;
 			break;
 		case OPT_IP2NAME:
-			ip2name_output++;
+			ct.ip2name_output++;
 			break;
 #ifdef DEBUG
 		case OPT_DEBUG:
@@ -333,7 +332,7 @@ int main(int argc, char * const *argv)
 			break;
 #endif
 		case OPT_HANDLE_OUTPUT:
-			handle_output++;
+			ct.handle_output++;
 			break;
 		case OPT_INVALID:
 			exit(NFT_EXIT_FAILURE);
@@ -375,7 +374,7 @@ int main(int argc, char * const *argv)
 		exit(NFT_EXIT_FAILURE);
 	}
 
-	if (nft_run(scanner, &state, &msgs) != 0)
+	if (nft_run(scanner, &state, &msgs, &ct) != 0)
 		rc = NFT_EXIT_FAILURE;
 out:
 	scanner_destroy(scanner);
src/meta.c +17 -17
index dd09d49..6580dca 100644
@@ -47,9 +47,9 @@ static void __exit realm_table_exit(void)
 	rt_symbol_table_free(realm_tbl);
 }
 
-static void realm_type_print(const struct expr *expr)
+static void realm_type_print(const struct expr *expr, struct print_ctx *ct)
 {
-	return symbolic_constant_print(realm_tbl, expr, true);
+	return symbolic_constant_print(realm_tbl, expr, true, ct);
 }
 
 static struct error_record *realm_type_parse(const struct expr *sym,
@@ -70,7 +70,7 @@ static const struct datatype realm_type = {
 	.flags		= DTYPE_F_PREFIX,
 };
 
-static void tchandle_type_print(const struct expr *expr)
+static void tchandle_type_print(const struct expr *expr, struct print_ctx *ct)
 {
 	uint32_t handle = mpz_get_uint32(expr->value);
 
@@ -149,7 +149,7 @@ static const struct datatype tchandle_type = {
 	.parse		= tchandle_type_parse,
 };
 
-static void ifindex_type_print(const struct expr *expr)
+static void ifindex_type_print(const struct expr *expr, struct print_ctx *ct)
 {
 	char name[IFNAMSIZ];
 	int ifindex;
@@ -222,11 +222,11 @@ const struct datatype arphrd_type = {
 	.sym_tbl	= &arphrd_tbl,
 };
 
-static void uid_type_print(const struct expr *expr)
+static void uid_type_print(const struct expr *expr, struct print_ctx *ct)
 {
 	struct passwd *pw;
 
-	if (numeric_output < NUMERIC_ALL) {
+	if (ct->numeric_output < NUMERIC_ALL) {
 		uint32_t uid = mpz_get_uint32(expr->value);
 
 		pw = getpwuid(uid);
@@ -236,7 +236,7 @@ static void uid_type_print(const struct expr *expr)
 			printf("%d", uid);
 		return;
 	}
-	expr_basetype(expr)->print(expr);
+	expr_basetype(expr)->print(expr, ct);
 }
 
 static struct error_record *uid_type_parse(const struct expr *sym,
@@ -274,11 +274,11 @@ static const struct datatype uid_type = {
 	.parse		= uid_type_parse,
 };
 
-static void gid_type_print(const struct expr *expr)
+static void gid_type_print(const struct expr *expr, struct print_ctx *ct)
 {
 	struct group *gr;
 
-	if (numeric_output < NUMERIC_ALL) {
+	if (ct->numeric_output < NUMERIC_ALL) {
 		uint32_t gid = mpz_get_uint32(expr->value);
 
 		gr = getgrgid(gid);
@@ -288,7 +288,7 @@ static void gid_type_print(const struct expr *expr)
 			printf("%u", gid);
 		return;
 	}
-	expr_basetype(expr)->print(expr);
+	expr_basetype(expr)->print(expr, ct);
 }
 
 static struct error_record *gid_type_parse(const struct expr *sym,
@@ -338,9 +338,9 @@ static const struct symbol_table pkttype_type_tbl = {
 	},
 };
 
-static void pkttype_type_print(const struct expr *expr)
+static void pkttype_type_print(const struct expr *expr, struct print_ctx *ct)
 {
-	return symbolic_constant_print(&pkttype_type_tbl, expr, false);
+	return symbolic_constant_print(&pkttype_type_tbl, expr, false, ct);
 }
 
 static const struct datatype pkttype_type = {
@@ -365,9 +365,9 @@ static void __exit devgroup_table_exit(void)
 	rt_symbol_table_free(devgroup_tbl);
 }
 
-static void devgroup_type_print(const struct expr *expr)
+static void devgroup_type_print(const struct expr *expr, struct print_ctx *ct)
 {
-	return symbolic_constant_print(devgroup_tbl, expr, true);
+	return symbolic_constant_print(devgroup_tbl, expr, true, ct);
 }
 
 static struct error_record *devgroup_type_parse(const struct expr *sym,
@@ -464,7 +464,7 @@ static bool meta_key_is_qualified(enum nft_meta_keys key)
 	}
 }
 
-static void meta_expr_print(const struct expr *expr)
+static void meta_expr_print(const struct expr *expr, struct print_ctx *ct)
 {
 	if (meta_key_is_qualified(expr->meta.key))
 		printf("meta %s", meta_templates[expr->meta.key].token);
@@ -591,14 +591,14 @@ struct expr *meta_expr_alloc(const struct location *loc, enum nft_meta_keys key)
 	return expr;
 }
 
-static void meta_stmt_print(const struct stmt *stmt)
+static void meta_stmt_print(const struct stmt *stmt, struct print_ctx *ct)
 {
 	if (meta_key_is_qualified(stmt->meta.key))
 		printf("meta %s set ", meta_templates[stmt->meta.key].token);
 	else
 		printf("%s set ", meta_templates[stmt->meta.key].token);
 
-	expr_print(stmt->meta.expr);
+	expr_print(stmt->meta.expr, ct);
 }
 
 static const struct stmt_ops meta_stmt_ops = {
src/netlink.c +21 -17
index 6fda0b9..72fc2c8 100644
@@ -2261,7 +2261,7 @@ static int netlink_events_setelem_cb(const struct nlmsghdr *nlh, int type,
 			goto out;
 		}
 		printf("element %s %s %s ", family2str(family), table, setname);
-		expr_print(dummyset->init);
+		expr_print(dummyset->init, monh->ct);
 		printf("\n");
 
 		set_free(dummyset);
@@ -2355,7 +2355,7 @@ static int netlink_events_rule_cb(const struct nlmsghdr *nlh, int type,
 			nlr_for_each_set(nlr, rule_map_decompose_cb, NULL);
 
 			printf("add rule %s %s %s ", family, table, chain);
-			rule_print(r);
+			rule_print(r, monh->ct);
 			printf("\n");
 
 			rule_free(r);
@@ -2611,7 +2611,7 @@ static void trace_print_hdr(const struct nftnl_trace *nlt)
 }
 
 static void trace_print_expr(const struct nftnl_trace *nlt, unsigned int attr,
-			     struct expr *lhs)
+			     struct expr *lhs, struct print_ctx *ct)
 {
 	struct expr *rhs, *rel;
 	const void *data;
@@ -2623,12 +2623,13 @@ static void trace_print_expr(const struct nftnl_trace *nlt, unsigned int attr,
 				   len * BITS_PER_BYTE, data);
 	rel  = relational_expr_alloc(&netlink_location, OP_EQ, lhs, rhs);
 
-	expr_print(rel);
+	expr_print(rel, ct);
 	printf(" ");
 	expr_free(rel);
 }
 
-static void trace_print_verdict(const struct nftnl_trace *nlt)
+static void trace_print_verdict(const struct nftnl_trace *nlt,
+				 struct print_ctx *ct)
 {
 	const char *chain = NULL;
 	unsigned int verdict;
@@ -2640,11 +2641,12 @@ static void trace_print_verdict(const struct nftnl_trace *nlt)
 	expr = verdict_expr_alloc(&netlink_location, verdict, chain);
 
 	printf("verdict ");
-	expr_print(expr);
+	expr_print(expr, ct);
 	expr_free(expr);
 }
 
-static void trace_print_rule(const struct nftnl_trace *nlt)
+static void trace_print_rule(const struct nftnl_trace *nlt,
+			      struct print_ctx *ct)
 {
 	const struct table *table;
 	uint64_t rule_handle;
@@ -2674,9 +2676,9 @@ static void trace_print_rule(const struct nftnl_trace *nlt)
 
 	trace_print_hdr(nlt);
 	printf("rule ");
-	rule_print(rule);
+	rule_print(rule, ct);
 	printf(" (");
-	trace_print_verdict(nlt);
+	trace_print_verdict(nlt, ct);
 	printf(")\n");
 }
 
@@ -2775,7 +2777,8 @@ next:
 	}
 }
 
-static void trace_print_packet(const struct nftnl_trace *nlt)
+static void trace_print_packet(const struct nftnl_trace *nlt,
+			        struct print_ctx *ct)
 {
 	struct list_head stmts = LIST_HEAD_INIT(stmts);
 	const struct proto_desc *ll_desc;
@@ -2791,11 +2794,11 @@ static void trace_print_packet(const struct nftnl_trace *nlt)
 	if (nftnl_trace_is_set(nlt, NFTNL_TRACE_IIF))
 		trace_print_expr(nlt, NFTNL_TRACE_IIF,
 				 meta_expr_alloc(&netlink_location,
-						 NFT_META_IIF));
+						 NFT_META_IIF), ct);
 	if (nftnl_trace_is_set(nlt, NFTNL_TRACE_OIF))
 		trace_print_expr(nlt, NFTNL_TRACE_OIF,
 				 meta_expr_alloc(&netlink_location,
-						 NFT_META_OIF));
+						 NFT_META_OIF), ct);
 
 	proto_ctx_init(&ctx, nftnl_trace_get_u32(nlt, NFTNL_TRACE_FAMILY));
 	ll_desc = ctx.protocol[PROTO_BASE_LL_HDR].desc;
@@ -2822,7 +2825,7 @@ static void trace_print_packet(const struct nftnl_trace *nlt)
 			PROTO_BASE_TRANSPORT_HDR);
 
 	list_for_each_entry_safe(stmt, next, &stmts, list) {
-		stmt_print(stmt);
+		stmt_print(stmt, ct);
 		printf(" ");
 		stmt_free(stmt);
 	}
@@ -2847,24 +2850,25 @@ static int netlink_events_trace_cb(const struct nlmsghdr *nlh, int type,
 	case NFT_TRACETYPE_RULE:
 		if (nftnl_trace_is_set(nlt, NFTNL_TRACE_LL_HEADER) ||
 		    nftnl_trace_is_set(nlt, NFTNL_TRACE_NETWORK_HEADER))
-			trace_print_packet(nlt);
+			trace_print_packet(nlt, monh->ct);
 
 		if (nftnl_trace_is_set(nlt, NFTNL_TRACE_RULE_HANDLE))
-			trace_print_rule(nlt);
+			trace_print_rule(nlt, monh->ct);
 		break;
 	case NFT_TRACETYPE_POLICY:
 	case NFT_TRACETYPE_RETURN:
 		trace_print_hdr(nlt);
 
 		if (nftnl_trace_is_set(nlt, NFTNL_TRACE_VERDICT)) {
-			trace_print_verdict(nlt);
+			trace_print_verdict(nlt, monh->ct);
 			printf(" ");
 		}
 
 		if (nftnl_trace_is_set(nlt, NFTNL_TRACE_MARK))
 			trace_print_expr(nlt, NFTNL_TRACE_MARK,
 					 meta_expr_alloc(&netlink_location,
-							 NFT_META_MARK));
+							 NFT_META_MARK),
+					 monh->ct);
 		printf("\n");
 		break;
 	}
src/numgen.c +1 -1
index 5c1d00a..0d0fe85 100644
@@ -28,7 +28,7 @@ static const char *numgen_type_str(enum nft_ng_types type)
 	return numgen_type[type];
 }
 
-static void numgen_expr_print(const struct expr *expr)
+static void numgen_expr_print(const struct expr *expr, struct print_ctx *ct)
 {
 	printf("numgen %s mod %u", numgen_type_str(expr->numgen.type),
 	       expr->numgen.mod);
src/payload.c +4 -4
index 11b6df3..da20ed5 100644
@@ -38,7 +38,7 @@ bool payload_is_known(const struct expr *expr)
 	       tmpl != &proto_unknown_template;
 }
 
-static void payload_expr_print(const struct expr *expr)
+static void payload_expr_print(const struct expr *expr, struct print_ctx *ct)
 {
 	const struct proto_desc *desc;
 	const struct proto_hdr_template *tmpl;
@@ -184,11 +184,11 @@ unsigned int payload_hdr_field(const struct expr *expr)
 	return expr->payload.tmpl - expr->payload.desc->templates;
 }
 
-static void payload_stmt_print(const struct stmt *stmt)
+static void payload_stmt_print(const struct stmt *stmt, struct print_ctx *ct)
 {
-	expr_print(stmt->payload.expr);
+	expr_print(stmt->payload.expr, ct);
 	printf(" set ");
-	expr_print(stmt->payload.val);
+	expr_print(stmt->payload.val, ct);
 }
 
 static const struct stmt_ops payload_stmt_ops = {
src/proto.c +2 -2
index 2afedf7..6eaf43c 100644
@@ -893,9 +893,9 @@ static const struct symbol_table ethertype_tbl = {
 	},
 };
 
-static void ethertype_print(const struct expr *expr)
+static void ethertype_print(const struct expr *expr, struct print_ctx *ct)
 {
-	return symbolic_constant_print(&ethertype_tbl, expr, false);
+	return symbolic_constant_print(&ethertype_tbl, expr, false, ct);
 }
 
 const struct datatype ethertype_type = {
src/rt.c +3 -3
index 232c1dc..e9001ba 100644
@@ -34,9 +34,9 @@ static void __exit realm_table_exit(void)
 	rt_symbol_table_free(realm_tbl);
 }
 
-static void realm_type_print(const struct expr *expr)
+static void realm_type_print(const struct expr *expr, struct print_ctx *ct)
 {
-	return symbolic_constant_print(realm_tbl, expr, true);
+	return symbolic_constant_print(realm_tbl, expr, true, ct);
 }
 
 static struct error_record *realm_type_parse(const struct expr *sym,
@@ -75,7 +75,7 @@ static const struct rt_template rt_templates[] = {
 					      true),
 };
 
-static void rt_expr_print(const struct expr *expr)
+static void rt_expr_print(const struct expr *expr, struct print_ctx *ct)
 {
 	printf("rt %s", rt_templates[expr->rt.key].token);
 }
src/rule.c +76 -66

467 lines not on this page · raw message

src/segtree.c +2 -2
index 15e8849..dd3c698 100644
@@ -541,7 +541,7 @@ 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, struct print_ctx *ct)
 {
 	struct elementary_interval *ei, *next;
 	struct seg_tree tree;
@@ -564,7 +564,7 @@ int set_to_intervals(struct list_head *errs, struct set *set,
 	}
 
 	if (segtree_debug()) {
-		expr_print(init);
+		expr_print(init, ct);
 		pr_gmp_debug("\n");
 	}
 
src/statement.c +44 -44

276 lines not on this page · raw message

Signature · 1 line
2.9.4
#1 lore raw

include/datatype.h struct datatype { · line 149

Quoted 11 lines · Varsha Rao
include/datatype.h +2 -1
index 04b7d88..748688e 100644
@@ -145,7 +145,8 @@ struct datatype {
 	const char			*desc;
 	const struct datatype		*basetype;
 	const char			*basefmt;
-	void				(*print)(const struct expr *expr);
+	void				(*print)(const struct expr *expr,
+						 struct print_ctx *ct);

Could you use:

struct print_ctx *pctx

instead?

'ct' usually refers to the connection tracking system (ct) in our codebase, so I would prefer we have a different variable name.

This applies everywhere in this patch.

Quoted 46 lines
include/datatype.h +3 -2
 	struct error_record		*(*parse)(const struct expr *sym,
 						  struct expr **res);
 	const struct symbol_table	*sym_tbl;
@@ -157,7 +158,7 @@ extern const struct datatype *datatype_lookup_byname(const char *name);
 
 extern struct error_record *symbol_parse(const struct expr *sym,
 					 struct expr **res);
-extern void datatype_print(const struct expr *expr);
+extern void datatype_print(const struct expr *expr, struct print_ctx *ct);
 
 static inline bool datatype_equal(const struct datatype *d1,
 				  const struct datatype *d2)
@@ -205,7 +206,8 @@ extern struct error_record *symbolic_constant_parse(const struct expr *sym,
 						    const struct symbol_table *tbl,
 						    struct expr **res);
 extern void symbolic_constant_print(const struct symbol_table *tbl,
-				    const struct expr *expr, bool quotes);
+				    const struct expr *expr, bool quotes,
+				    struct print_ctx *ct);
 extern void symbol_table_print(const struct symbol_table *tbl,
 			       const struct datatype *dtype,
 			       enum byteorder byteorder);
include/expression.h +3 -2
index 9ba87e8..80f91d0 100644
@@ -157,7 +157,8 @@ struct expr_ops {
 	void			(*set_type)(const struct expr *expr,
 					    const struct datatype *dtype,
 					    enum byteorder byteorder);
-	void			(*print)(const struct expr *expr);
+	void			(*print)(const struct expr *expr,
+					 struct print_ctx *ct);
 	bool			(*cmp)(const struct expr *e1,
 				       const struct expr *e2);
 	void			(*pctx_update)(struct proto_ctx *ctx,
@@ -330,7 +331,7 @@ extern struct expr *expr_alloc(const struct location *loc,
 extern struct expr *expr_clone(const struct expr *expr);
 extern struct expr *expr_get(struct expr *expr);
 extern void expr_free(struct expr *expr);
-extern void expr_print(const struct expr *expr);
+extern void expr_print(const struct expr *expr, struct print_ctx *ct);
 extern bool expr_cmp(const struct expr *e1, const struct expr *e2);
 extern void expr_describe(const struct expr *expr);
 

include/expression.h extern struct expr *list_expr_alloc(const struct location *loc); · line 414

Quoted 5 lines
include/expression.h +1 -1
@@ -410,7 +411,7 @@ extern struct expr *list_expr_alloc(const struct location *loc);
 
 extern struct expr *set_expr_alloc(const struct location *loc);
 extern int set_to_intervals(struct list_head *msgs, struct set *set,
-			    struct expr *init, bool add);
+			    struct expr *init, bool add, struct print_ctx *ct);

Why do we need this? I think we only need print_ctx for *_print() functions.

include/netlink.h struct netlink_mon_handler { · line 216

Quoted 12 lines
include/expression.h +0 -0
 extern void interval_map_decompose(struct expr *set);
 
 extern struct expr *mapping_expr_alloc(const struct location *loc,
include/netlink.h +1 -0
index 81538ff..0c7cd90 100644
@@ -213,6 +213,7 @@ struct netlink_mon_handler {
 	struct netlink_ctx	*ctx;
 	const struct location	*loc;
 	bool			cache_needed;
+	struct print_ctx	*ct;

I think you can place this in 'struct netlink_ctx', see more detailed comment below.

include/nftables.h enum debug_level { · line 31

Quoted 13 lines
include/nftables.h +5 -0
index 6f54155..1c747d6 100644
@@ -24,12 +24,16 @@ enum debug_level {
 
 #define INCLUDE_PATHS_MAX	16
 
+struct print_ctx {
+	unsigned int numeric_output;
+	unsigned int stateless_output;
+	unsigned int ip2name_output;
+	unsigned int handle_output;

You can probably remove the trailing "_output" now that this is inside struct print_ctx, it is obvious they refer to printing toggles.

Quoted 53 lines
include/nftables.h +5 -5
+};
+
 extern unsigned int max_errors;
-extern unsigned int numeric_output;
-extern unsigned int stateless_output;
-extern unsigned int ip2name_output;
-extern unsigned int handle_output;
 extern unsigned int debug_level;
+
 extern const char *include_paths[INCLUDE_PATHS_MAX];
 
 enum nftables_exit_codes {
@@ -107,6 +111,7 @@ struct input_descriptor {
 
 struct parser_state;
 
-int nft_run(void *scanner, struct parser_state *state, struct list_head *msgs);
+int nft_run(void *scanner, struct parser_state *state, struct list_head *msgs,
+	     struct print_ctx *ct);
 
 #endif /* NFTABLES_NFTABLES_H */
include/rule.h +3 -3
index fb46064..48cb5f1 100644
@@ -195,7 +195,7 @@ struct rule {
 extern struct rule *rule_alloc(const struct location *loc,
 			       const struct handle *h);
 extern void rule_free(struct rule *rule);
-extern void rule_print(const struct rule *rule);
+extern void rule_print(const struct rule *rule, struct print_ctx *ct);
 extern struct rule *rule_lookup(const struct chain *chain, uint64_t handle);
 
 /**
@@ -244,7 +244,7 @@ extern void set_add_hash(struct set *set, struct table *table);
 extern struct set *set_lookup(const struct table *table, const char *name);
 extern struct set *set_lookup_global(uint32_t family, const char *table,
 				     const char *name);
-extern void set_print(const struct set *set);
+extern void set_print(const struct set *set, struct print_ctx *ct);
 extern void set_print_plain(const struct set *s);
 
 #include <statement.h>
@@ -292,7 +292,7 @@ void obj_free(struct obj *obj);
 void obj_add_hash(struct obj *obj, struct table *table);
 struct obj *obj_lookup(const struct table *table, const char *name,
 		       uint32_t type);
-void obj_print(const struct obj *n);
+void obj_print(const struct obj *n, struct print_ctx *ct);
 void obj_print_plain(const struct obj *obj);
 const char *obj_type_name(uint32_t type);
 

include/rule.h extern int cmd_evaluate(struct eval_ctx *ctx, struct cmd *cmd); · line 479

Quoted 6 lines
include/rule.h +2 -1
@@ -475,7 +475,8 @@ extern int cmd_evaluate(struct eval_ctx *ctx, struct cmd *cmd);
 extern struct error_record *rule_postprocess(struct rule *rule);
 
 struct netlink_ctx;
-extern int do_command(struct netlink_ctx *ctx, struct cmd *cmd);
+extern int do_command(struct netlink_ctx *ctx, struct cmd *cmd,
+		       struct print_ctx *ct);

You can place this in netlink_ctx so...

struct netlink_ctx {
        ...
        struct print_ctx        *pctx;
};

So we can make this patch smaller, you don't need to update that many functions.

Quoted 30 lines
include/rule.h +0 -0
 extern int cache_update(enum cmd_ops cmd, struct list_head *msgs);
 extern void cache_flush(void);
include/statement.h +3 -2
index 317d53e..d6ebc01 100644
@@ -261,7 +261,8 @@ struct stmt_ops {
 	enum stmt_types		type;
 	const char		*name;
 	void			(*destroy)(struct stmt *stmt);
-	void			(*print)(const struct stmt *stmt);
+	void			(*print)(const struct stmt *stmt,
+					 struct print_ctx *ct);
 };
 
 enum stmt_flags {
@@ -312,7 +313,7 @@ extern struct stmt *stmt_alloc(const struct location *loc,
 int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt);
 extern void stmt_free(struct stmt *stmt);
 extern void stmt_list_free(struct list_head *list);
-extern void stmt_print(const struct stmt *stmt);
+extern void stmt_print(const struct stmt *stmt, struct print_ctx *ct);
 
 const char *get_rate(uint64_t byte_rate, uint64_t *rate);
 
src/cli.c +0 -0
index a74411a..c91eced 100644

src/cli.c static void cli_complete(char *line) · line 132

Quoted 5 lines
src/cli.c +1 -1
@@ -129,7 +129,7 @@ static void cli_complete(char *line)
 
 	parser_init(state, &msgs);
 	scanner_push_buffer(scanner, &indesc_cli, line);
-	nft_run(scanner, state, &msgs);
+	nft_run(scanner, state, &msgs, NULL);

I suggest you pass it here as global. Look, the cli code (interactive mode that you exercise via -i) will not go into libnftables, so it's fine to to keep it global.

author
#2 lore raw
Quoted 15 lines · Pablo Neira Ayuso

include/datatype.h struct datatype { · line 149

Quoted 11 lines · Varsha Rao
include/datatype.h +2 -1
index 04b7d88..748688e 100644
@@ -145,7 +145,8 @@ struct datatype {
      const char                      *desc;
      const struct datatype           *basetype;
      const char                      *basefmt;
-     void                            (*print)(const struct expr *expr);
+     void                            (*print)(const struct expr *expr,
+                                              struct print_ctx *ct);

Could you use:

Quoted 8 lines

struct print_ctx *pctx

instead?

'ct' usually refers to the connection tracking system (ct) in our codebase, so I would prefer we have a different variable name.

This applies everywhere in this patch.

Okay, I will change it.

Quoted 24 lines
Quoted 24 lines
include/expression.h +3 -2
@@ -157,7 +157,8 @@ struct expr_ops {
      void                    (*set_type)(const struct expr *expr,
                                          const struct datatype *dtype,
                                          enum byteorder byteorder);
-     void                    (*print)(const struct expr *expr);
+     void                    (*print)(const struct expr *expr,
+                                      struct print_ctx *ct);
      bool                    (*cmp)(const struct expr *e1,
                                     const struct expr *e2);
      void                    (*pctx_update)(struct proto_ctx *ctx,
@@ -330,7 +331,7 @@ extern struct expr *expr_alloc(const struct location *loc,
 extern struct expr *expr_clone(const struct expr *expr);
 extern struct expr *expr_get(struct expr *expr);
 extern void expr_free(struct expr *expr);
-extern void expr_print(const struct expr *expr);
+extern void expr_print(const struct expr *expr, struct print_ctx *ct);
 extern bool expr_cmp(const struct expr *e1, const struct expr *e2);
 extern void expr_describe(const struct expr *expr);
 
@@ -410,7 +411,7 @@ extern struct expr *list_expr_alloc(const struct location *loc);
 
Quoted 7 lines

include/expression.h extern struct expr *list_expr_alloc(const struct location *loc); · line 414

Quoted 4 lines
include/expression.h +1 -1
 extern struct expr *set_expr_alloc(const struct location *loc);
 extern int set_to_intervals(struct list_head *msgs, struct set *set,
-                         struct expr *init, bool add);
+                         struct expr *init, bool add, struct print_ctx *ct);

Why do we need this? I think we only need print_ctx for *_print() functions.

In src/segtree.c, set_to_intervals calls expr_print() if segtree_debug() is true and it would cause problem if NULL is passed to expr_print().

Thanks,
varsha

#3 lore raw
Quoted 58 lines · Varsha Rao
Quoted 15 lines · Pablo Neira Ayuso

include/datatype.h struct datatype { · line 149

Quoted 11 lines · Varsha Rao
include/datatype.h +2 -1
index 04b7d88..748688e 100644
@@ -145,7 +145,8 @@ struct datatype {
      const char                      *desc;
      const struct datatype           *basetype;
      const char                      *basefmt;
-     void                            (*print)(const struct expr *expr);
+     void                            (*print)(const struct expr *expr,
+                                              struct print_ctx *ct);

Could you use:

Quoted 8 lines

struct print_ctx *pctx

instead?

'ct' usually refers to the connection tracking system (ct) in our codebase, so I would prefer we have a different variable name.

This applies everywhere in this patch.

Okay, I will change it.

Quoted 24 lines
Quoted 24 lines
include/expression.h +3 -2
@@ -157,7 +157,8 @@ struct expr_ops {
      void                    (*set_type)(const struct expr *expr,
                                          const struct datatype *dtype,
                                          enum byteorder byteorder);
-     void                    (*print)(const struct expr *expr);
+     void                    (*print)(const struct expr *expr,
+                                      struct print_ctx *ct);
      bool                    (*cmp)(const struct expr *e1,
                                     const struct expr *e2);
      void                    (*pctx_update)(struct proto_ctx *ctx,
@@ -330,7 +331,7 @@ extern struct expr *expr_alloc(const struct location *loc,
 extern struct expr *expr_clone(const struct expr *expr);
 extern struct expr *expr_get(struct expr *expr);
 extern void expr_free(struct expr *expr);
-extern void expr_print(const struct expr *expr);
+extern void expr_print(const struct expr *expr, struct print_ctx *ct);
 extern bool expr_cmp(const struct expr *e1, const struct expr *e2);
 extern void expr_describe(const struct expr *expr);
 
@@ -410,7 +411,7 @@ extern struct expr *list_expr_alloc(const struct location *loc);
 
Quoted 7 lines

include/expression.h extern struct expr *list_expr_alloc(const struct location *loc); · line 414

Quoted 4 lines
include/expression.h +1 -1
 extern struct expr *set_expr_alloc(const struct location *loc);
 extern int set_to_intervals(struct list_head *msgs, struct set *set,
-                         struct expr *init, bool add);
+                         struct expr *init, bool add, struct print_ctx *ct);

Why do we need this? I think we only need print_ctx for *_print() functions.

Quoted 2 lines

In src/segtree.c, set_to_intervals calls expr_print() if segtree_debug() is true and it would cause problem if NULL is passed to expr_print().

I see.

_debug() call should not rely on the option, so I suggest you pass a dummy print_ctx, ie.

        struct print_ctx dummy_pctx = {};

        expr_print(..., &dummy_pctx);

but only for debug codepath. OK?

Thanks!