Skip to contentSkip to conversation

src: replace struct stmt_ops by type field in struct stmt

PATCH In review Pablo Neira Ayuso 2 participants 2 messages Mar 17, 2025 – Mar 18, 2025 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 src: replace struct stmt_ops by type field in struct stmt

Shrink struct stmt in 8 bytes.

__stmt_ops_by_type() provides an operation for STMT_INVALID since this is required by -o/--optimize.

There are many checks for stmt->ops->type, which is the most accessed field, that can be trivially replaced.

BUG() uses statement type enum instead of name.

Similar to:

68e76238749f ("src: expr: add and use expr_name helper"). 72931553828a ("src: expr: add expression etype") 2cc91e6198e7 ("src: expr: add and use internal expr_ops helper")

---
 include/ct.h              |  4 +++
 include/exthdr.h          |  2 ++
 include/meta.h            |  2 ++
 include/payload.h         |  2 ++
 include/statement.h       |  6 ++--
 src/ct.c                  |  6 ++--
 src/evaluate.c            | 24 ++++++-------
 src/exthdr.c              |  2 +-
 src/json.c                |  9 ++---
 src/meta.c                |  2 +-
 src/netlink_delinearize.c |  6 ++--
 src/netlink_linearize.c   |  8 ++---
 src/optimize.c            | 55 ++++++++++++++---------------
 src/parser_bison.y        |  4 +--
 src/payload.c             |  4 +--
 src/rule.c                |  6 ++--
 src/statement.c           | 73 +++++++++++++++++++++++++++++++++++----
 17 files changed, 144 insertions(+), 71 deletions(-)
include/ct.h +4 -0
index 0a705fd06ee1..bb9193d8fc50 100644
@@ -42,4 +42,8 @@ extern const struct datatype ct_status_type;
 extern const struct datatype ct_label_type;
 extern const struct datatype ct_event_type;
 
+extern const struct stmt_ops ct_stmt_ops;
+extern const struct stmt_ops notrack_stmt_ops;
+extern const struct stmt_ops flow_offload_stmt_ops;
+
 #endif /* NFTABLES_CT_H */
include/exthdr.h +2 -0
index 084daba5358f..98494e4d5bf7 100644
@@ -117,4 +117,6 @@ extern const struct exthdr_desc exthdr_dst;
 extern const struct exthdr_desc exthdr_mh;
 extern const struct datatype mh_type_type;
 
+extern const struct stmt_ops exthdr_stmt_ops;
+
 #endif /* NFTABLES_EXTHDR_H */
include/meta.h +2 -0
index af2d772bb6a0..84937ba3a1fe 100644
@@ -47,4 +47,6 @@ extern const struct datatype day_type;
 
 bool lhs_is_meta_hour(const struct expr *meta);
 
+extern const struct stmt_ops meta_stmt_ops;
+
 #endif /* NFTABLES_META_H */
include/payload.h +2 -0
index 20304252e3f6..e14fc0f24477 100644
@@ -74,4 +74,6 @@ bool payload_expr_cmp(const struct expr *e1, const struct expr *e2);
 
 const struct proto_desc *find_proto_desc(const struct nftnl_udata *ud);
 
+extern const struct stmt_ops payload_stmt_ops;
+
 #endif /* NFTABLES_PAYLOAD_H */
include/statement.h +4 -2
index 9376911bb377..e8724dde63d0 100644
@@ -372,16 +372,16 @@ enum stmt_flags {
  * struct stmt
  *
  * @list:	rule list node
- * @ops:	statement ops
  * @location:	location where the statement was defined
  * @flags:	statement flags
+ * @type:	statement type
  * @union:	type specific data
  */
 struct stmt {
 	struct list_head		list;
-	const struct stmt_ops		*ops;
 	struct location			location;
 	enum stmt_flags			flags;
+	enum stmt_types			type:8;
 
 	union {
 		struct expr		*expr;
@@ -420,6 +420,8 @@ int stmt_dependency_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, struct output_ctx *octx);
+const char *stmt_name(const struct stmt *stmt);
+const struct stmt_ops *stmt_ops(const struct stmt *stmt);
 
 const char *get_rate(uint64_t byte_rate, uint64_t *rate);
 const char *get_unit(uint64_t u);
src/ct.c +3 -3
index 6793464859ca..4d71a4b0103b 100644
@@ -530,7 +530,7 @@ static void ct_stmt_destroy(struct stmt *stmt)
 	expr_free(stmt->ct.expr);
 }
 
-static const struct stmt_ops ct_stmt_ops = {
+const struct stmt_ops ct_stmt_ops = {
 	.type		= STMT_CT,
 	.name		= "ct",
 	.print		= ct_stmt_print,
@@ -557,7 +557,7 @@ static void notrack_stmt_print(const struct stmt *stmt, struct output_ctx *octx)
 	nft_print(octx, "notrack");
 }
 
-static const struct stmt_ops notrack_stmt_ops = {
+const struct stmt_ops notrack_stmt_ops = {
 	.type		= STMT_NOTRACK,
 	.name		= "notrack",
 	.print		= notrack_stmt_print,
@@ -580,7 +580,7 @@ static void flow_offload_stmt_destroy(struct stmt *stmt)
 	free_const(stmt->flow.table_name);
 }
 
-static const struct stmt_ops flow_offload_stmt_ops = {
+const struct stmt_ops flow_offload_stmt_ops = {
 	.type		= STMT_FLOW_OFFLOAD,
 	.name		= "flow_offload",
 	.print		= flow_offload_stmt_print,
src/evaluate.c +12 -12
index 7fc210fd3b12..90a212117a5b 100644
@@ -74,9 +74,9 @@ static int __fmtstring(3, 4) set_error(struct eval_ctx *ctx,
 	return -1;
 }
 
-static const char *stmt_name(const struct stmt *stmt)
+const char *stmt_name(const struct stmt *stmt)
 {
-	switch (stmt->ops->type) {
+	switch (stmt->type) {
 	case STMT_NAT:
 		switch (stmt->nat.type) {
 		case NFT_NAT_SNAT:
@@ -93,7 +93,7 @@ static const char *stmt_name(const struct stmt *stmt)
 		break;
 	}
 
-	return stmt->ops->name;
+	return stmt_ops(stmt)->name;
 }
 
 static int stmt_error_range(struct eval_ctx *ctx, const struct stmt *stmt, const struct expr *e)
@@ -573,7 +573,7 @@ static int expr_evaluate_bits(struct eval_ctx *ctx, struct expr **exprp)
 	 * require the transformations that are needed for payload matching,
 	 * skip this.
 	 */
-	if (ctx->stmt && ctx->stmt->ops->type == STMT_PAYLOAD)
+	if (ctx->stmt && ctx->stmt->type == STMT_PAYLOAD)
 		return 0;
 
 	switch (expr->etype) {
@@ -790,7 +790,7 @@ static int stmt_dep_conflict(struct eval_ctx *ctx, const struct stmt *nstmt)
 		if (stmt == nstmt)
 			break;
 
-		if (stmt->ops->type != STMT_EXPRESSION ||
+		if (stmt->type != STMT_EXPRESSION ||
 		    stmt->expr->etype != EXPR_RELATIONAL ||
 		    stmt->expr->right->etype != EXPR_VALUE ||
 		    stmt->expr->left->etype != EXPR_PAYLOAD ||
@@ -1841,13 +1841,13 @@ static int __expr_evaluate_set_elem(struct eval_ctx *ctx, struct expr *elem)
 		set_stmt = list_first_entry(&set->stmt_list, struct stmt, list);
 
 		list_for_each_entry(elem_stmt, &elem->stmt_list, list) {
-			if (set_stmt->ops != elem_stmt->ops) {
+			if (set_stmt->type != elem_stmt->type) {
 				return stmt_error(ctx, elem_stmt,
 						  "statement mismatch, element expects %s, "
 						  "but %s has type %s",
-						  elem_stmt->ops->name,
+						  stmt_name(elem_stmt),
 						  set_is_map(set->flags) ? "map" : "set",
-						  set_stmt->ops->name);
+						  stmt_name(set_stmt));
 			}
 			set_stmt = list_next_entry(set_stmt, list);
 		}
@@ -4126,7 +4126,7 @@ static int stmt_evaluate_l3proto(struct eval_ctx *ctx,
 					 "conflicting protocols specified: %s vs. %s. You must specify ip or ip6 family in %s statement",
 					 pctx->protocol[PROTO_BASE_NETWORK_HDR].desc->name,
 					 family2str(family),
-					 stmt->ops->name);
+					 stmt_name(stmt));
 	return 0;
 }
 
@@ -4854,7 +4854,7 @@ int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt)
 	if (ctx->nft->debug_mask & NFT_DEBUG_EVALUATION) {
 		struct error_record *erec;
 		erec = erec_create(EREC_INFORMATIONAL, &stmt->location,
-				   "Evaluate %s", stmt->ops->name);
+				   "Evaluate %s", stmt_name(stmt));
 		erec_print(&ctx->nft->output, erec, ctx->nft->debug_mask);
 		stmt_print(stmt, &ctx->nft->output);
 		nft_print(&ctx->nft->output, "\n\n");
@@ -4863,7 +4863,7 @@ int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt)
 
 	ctx->stmt_len = 0;
 
-	switch (stmt->ops->type) {
+	switch (stmt->type) {
 	case STMT_CONNLIMIT:
 	case STMT_COUNTER:
 	case STMT_LAST:
@@ -4913,7 +4913,7 @@ int stmt_evaluate(struct eval_ctx *ctx, struct stmt *stmt)
 	case STMT_OPTSTRIP:
 		return stmt_evaluate_optstrip(ctx, stmt);
 	default:
-		BUG("unknown statement type %s\n", stmt->ops->name);
+		BUG("unknown statement type %d\n", stmt->type);
 	}
 }
 
src/exthdr.c +1 -1
index 1438d7e2d2dc..c7d876a45aab 100644
@@ -269,7 +269,7 @@ static void exthdr_stmt_destroy(struct stmt *stmt)
 	expr_free(stmt->exthdr.val);
 }
 
-static const struct stmt_ops exthdr_stmt_ops = {
+const struct stmt_ops exthdr_stmt_ops = {
 	.type		= STMT_EXTHDR,
 	.name		= "exthdr",
 	.print		= exthdr_stmt_print,
src/json.c +5 -4
index 64a6888f9e0a..96413d70895a 100644
@@ -109,19 +109,20 @@ static json_t *set_key_dtype_json(const struct set *set,
 
 static json_t *stmt_print_json(const struct stmt *stmt, struct output_ctx *octx)
 {
+	const struct stmt_ops *ops = stmt_ops(stmt);
 	char buf[1024];
 	FILE *fp;
 
-	if (stmt->ops->json)
-		return stmt->ops->json(stmt, octx);
+	if (ops->json)
+		return ops->json(stmt, octx);
 
 	fprintf(stderr, "warning: stmt ops %s have no json callback\n",
-		stmt->ops->name);
+		ops->name);
 
 	fp = octx->output_fp;
 	octx->output_fp = fmemopen(buf, 1024, "w");
 
-	stmt->ops->print(stmt, octx);
+	ops->print(stmt, octx);
 
 	fclose(octx->output_fp);
 	octx->output_fp = fp;
src/meta.c +1 -1
index a17bacf07d0e..1010209d3152 100644
@@ -952,7 +952,7 @@ static void meta_stmt_destroy(struct stmt *stmt)
 	expr_free(stmt->meta.expr);
 }
 
-static const struct stmt_ops meta_stmt_ops = {
+const struct stmt_ops meta_stmt_ops = {
 	.type		= STMT_META,
 	.name		= "meta",
 	.print		= meta_stmt_print,
src/netlink_delinearize.c +3 -3
index ae14065c00d6..ae1ee53f6e7c 100644
@@ -3121,7 +3121,7 @@ static void stmt_expr_postprocess(struct rule_pp_ctx *ctx)
 	expr_postprocess(ctx, &ctx->stmt->expr);
 
 	if (dl->pdctx.prev && ctx->stmt &&
-	    ctx->stmt->ops->type == dl->pdctx.prev->ops->type &&
+	    ctx->stmt->type == dl->pdctx.prev->type &&
 	    expr_may_merge_range(ctx->stmt->expr, dl->pdctx.prev->expr, &op))
 		expr_postprocess_range(ctx, op);
 }
@@ -3404,7 +3404,7 @@ static struct dl_proto_ctx *rule_update_dl_proto_ctx(struct rule_pp_ctx *rctx)
 	const struct stmt *stmt = rctx->stmt;
 	bool inner = false;
 
-	switch (stmt->ops->type) {
+	switch (stmt->type) {
 	case STMT_EXPRESSION:
 		if (has_inner_desc(stmt->expr->left))
 			inner = true;
@@ -3438,7 +3438,7 @@ static void rule_parse_postprocess(struct netlink_parse_ctx *ctx, struct rule *r
 	proto_ctx_init(&rctx._dl[1].pctx, NFPROTO_BRIDGE, ctx->debug_mask, true);
 
 	list_for_each_entry_safe(stmt, next, &rule->stmts, list) {
-		enum stmt_types type = stmt->ops->type;
+		enum stmt_types type = stmt->type;
 
 		rctx.stmt = stmt;
 		dl = rule_update_dl_proto_ctx(&rctx);
src/netlink_linearize.c +4 -4
index 598ddfab5827..5f73183bf19a 100644
@@ -1046,7 +1046,7 @@ static struct nftnl_expr *netlink_gen_last_stmt(const struct stmt *stmt)
 
 struct nftnl_expr *netlink_gen_stmt_stateful(const struct stmt *stmt)
 {
-	switch (stmt->ops->type) {
+	switch (stmt->type) {
 	case STMT_CONNLIMIT:
 		return netlink_gen_connlimit_stmt(stmt);
 	case STMT_COUNTER:
@@ -1058,7 +1058,7 @@ struct nftnl_expr *netlink_gen_stmt_stateful(const struct stmt *stmt)
 	case STMT_LAST:
 		return netlink_gen_last_stmt(stmt);
 	default:
-		BUG("unknown stateful statement type %s\n", stmt->ops->name);
+		BUG("unknown stateful statement type %d\n", stmt->type);
 	}
 }
 
@@ -1694,7 +1694,7 @@ static void netlink_gen_stmt(struct netlink_linearize_ctx *ctx,
 {
 	struct nftnl_expr *nle;
 
-	switch (stmt->ops->type) {
+	switch (stmt->type) {
 	case STMT_EXPRESSION:
 		return netlink_gen_expr(ctx, stmt->expr, NFT_REG_VERDICT);
 	case STMT_VERDICT:
@@ -1748,7 +1748,7 @@ static void netlink_gen_stmt(struct netlink_linearize_ctx *ctx,
 	case STMT_OPTSTRIP:
 		return netlink_gen_optstrip_stmt(ctx, stmt);
 	default:
-		BUG("unknown statement type %s\n", stmt->ops->name);
+		BUG("unknown statement type %d\n", stmt->type);
 	}
 }
 
src/optimize.c +26 -29

191 lines not on this page · raw message

src/parser_bison.y +2 -2
index e494079d6373..4d4d39342bf7 100644
@@ -3306,12 +3306,12 @@ counter_args		:	counter_arg
 
 counter_arg		:	PACKETS			NUM
 			{
-				assert($<stmt>0->ops->type == STMT_COUNTER);
+				assert($<stmt>0->type == STMT_COUNTER);
 				$<stmt>0->counter.packets = $2;
 			}
 			|	BYTES			NUM
 			{
-				assert($<stmt>0->ops->type == STMT_COUNTER);
+				assert($<stmt>0->type == STMT_COUNTER);
 				$<stmt>0->counter.bytes	 = $2;
 			}
 			;
src/payload.c +2 -2
index 50b5acc9a927..a38f5bf730d1 100644
@@ -378,7 +378,7 @@ static void payload_stmt_destroy(struct stmt *stmt)
 	expr_free(stmt->payload.val);
 }
 
-static const struct stmt_ops payload_stmt_ops = {
+const struct stmt_ops payload_stmt_ops = {
 	.type		= STMT_PAYLOAD,
 	.name		= "payload",
 	.print		= payload_stmt_print,
@@ -1198,7 +1198,7 @@ bool stmt_payload_expr_trim(struct stmt *stmt, const struct proto_ctx *pctx)
 	mpz_t bitmask, tmp, tmp2;
 	unsigned long n;
 
-	assert(stmt->ops->type == STMT_PAYLOAD);
+	assert(stmt->type == STMT_PAYLOAD);
 	assert(expr->etype == EXPR_BINOP);
 
 	payload = expr->left;
src/rule.c +4 -2
index 9c317934139c..3edfa4715853 100644
@@ -494,10 +494,12 @@ void rule_free(struct rule *rule)
 
 void rule_print(const struct rule *rule, struct output_ctx *octx)
 {
+	const struct stmt_ops *ops;
 	const struct stmt *stmt;
 
 	list_for_each_entry(stmt, &rule->stmts, list) {
-		stmt->ops->print(stmt, octx);
+		ops = stmt_ops(stmt);
+		ops->print(stmt, octx);
 		if (!list_is_last(&stmt->list, &rule->stmts))
 			nft_print(octx, " ");
 	}
@@ -2741,7 +2743,7 @@ static void stmt_reduce(const struct rule *rule)
 		}
 
 		/* Must not merge across other statements */
-		if (stmt->ops->type != STMT_EXPRESSION) {
+		if (stmt->type != STMT_EXPRESSION) {
 			if (idx >= 2)
 				payload_do_merge(sa, idx);
 			idx = 0;
src/statement.c +67 -6

101 lines not on this page · raw message

Signature · 1 line
2.30.2
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · Pablo Neira Ayuso

Shrink struct stmt in 8 bytes.

__stmt_ops_by_type() provides an operation for STMT_INVALID since this is required by -o/--optimize.

We could have lots of statements in a ruleset, so I think this makes sense to compact things.

Quoted 6 lines

Similar to:

68e76238749f ("src: expr: add and use expr_name helper"). 72931553828a ("src: expr: add expression etype") 2cc91e6198e7 ("src: expr: add and use internal expr_ops helper")

... and it makes statements more like expressions wrt. ops, so this is good too.