Skip to contentSkip to conversation

src: remove DTYPE_F_PREFIX

PATCH New Pablo Neira Ayuso 1 participant 2 messages Aug 19, 2024 netfilter

lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/2 src: remove DTYPE_F_PREFIX

only ipv4 and ipv6 datatype support this, add datatype_prefix_notation() helper function to report that datatype prefers prefix notation, if possible.

---
 include/datatype.h        | 3 +--
 src/datatype.c            | 7 +++++--
 src/netlink_delinearize.c | 2 +-
 src/segtree.c             | 4 ++--
 4 files changed, 9 insertions(+), 7 deletions(-)
include/datatype.h +1 -2
index d4b4737cc9ae..09b84eca27a7 100644
@@ -124,11 +124,9 @@ struct expr;
  * enum datatype_flags
  *
  * @DTYPE_F_ALLOC:		datatype is dynamically allocated
- * @DTYPE_F_PREFIX:		preferred representation for ranges is a prefix
  */
 enum datatype_flags {
 	DTYPE_F_ALLOC		= (1 << 0),
-	DTYPE_F_PREFIX		= (1 << 1),
 };
 
 struct parse_ctx;
@@ -179,6 +177,7 @@ extern void datatype_set(struct expr *expr, const struct datatype *dtype);
 extern void __datatype_set(struct expr *expr, const struct datatype *dtype);
 extern void datatype_free(const struct datatype *dtype);
 struct datatype *datatype_clone(const struct datatype *orig_dtype);
+bool datatype_prefix_notation(const struct datatype *dtype);
 
 struct parse_ctx {
 	struct symbol_tables	*tbl;
src/datatype.c +5 -2
index 6bbe900295f7..9293f38ed713 100644
@@ -123,6 +123,11 @@ void datatype_print(const struct expr *expr, struct output_ctx *octx)
 	    expr->dtype->name);
 }
 
+bool datatype_prefix_notation(const struct datatype *dtype)
+{
+	return dtype->type == TYPE_IPADDR || dtype->type == TYPE_IP6ADDR;
+}
+
 struct error_record *symbol_parse(struct parse_ctx *ctx, const struct expr *sym,
 				  struct expr **res)
 {
@@ -642,7 +647,6 @@ const struct datatype ipaddr_type = {
 	.basetype	= &integer_type,
 	.print		= ipaddr_type_print,
 	.parse		= ipaddr_type_parse,
-	.flags		= DTYPE_F_PREFIX,
 };
 
 static void ip6addr_type_print(const struct expr *expr, struct output_ctx *octx)
@@ -709,7 +713,6 @@ const struct datatype ip6addr_type = {
 	.basetype	= &integer_type,
 	.print		= ip6addr_type_print,
 	.parse		= ip6addr_type_parse,
-	.flags		= DTYPE_F_PREFIX,
 };
 
 static void inet_protocol_type_print(const struct expr *expr,
src/netlink_delinearize.c +1 -1
index 82e68999a432..e3d9cfbbede5 100644
@@ -2536,7 +2536,7 @@ static void relational_binop_postprocess(struct rule_pp_ctx *ctx,
 			BUG("unknown operation type %d\n", expr->op);
 		}
 		expr_free(binop);
-	} else if (binop->left->dtype->flags & DTYPE_F_PREFIX &&
+	} else if (datatype_prefix_notation(binop->left->dtype) &&
 		   binop->op == OP_AND && expr->right->etype == EXPR_VALUE &&
 		   expr_mask_is_prefix(binop->right)) {
 		expr->left = expr_get(binop->left);
src/segtree.c +2 -2
index 4df96467c3f5..2e32a3291979 100644
@@ -402,7 +402,7 @@ void concat_range_aggregate(struct expr *set)
 			}
 
 			if (prefix_len < 0 ||
-			    !(r1->dtype->flags & DTYPE_F_PREFIX)) {
+			    !datatype_prefix_notation(r1->dtype)) {
 				tmp = range_expr_alloc(&r1->location, r1,
 						       r2);
 
@@ -517,7 +517,7 @@ add_interval(struct expr *set, struct expr *low, struct expr *i)
 		expr = expr_get(low);
 	} else if (range_is_prefix(range) && !mpz_cmp_ui(p, 0)) {
 
-		if (i->dtype->flags & DTYPE_F_PREFIX)
+		if (datatype_prefix_notation(i->dtype))
 			expr = interval_to_prefix(low, i, range);
 		else if (expr_basetype(i)->type == TYPE_STRING)
 			expr = interval_to_string(low, i, range);
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 2/2 datatype: replace DTYPE_F_ALLOC by bitfield

Only user of the datatype flags field is DTYPE_F_ALLOC, replace it by bitfield, squash byteorder to 8 bits which is sufficient.

---
 include/datatype.h | 14 +++-----------
 src/datatype.c     |  8 ++++----
 2 files changed, 7 insertions(+), 15 deletions(-)
include/datatype.h +3 -11
index 09b84eca27a7..df3bc3850b51 100644
@@ -120,15 +120,6 @@ enum byteorder {
 
 struct expr;
 
-/**
- * enum datatype_flags
- *
- * @DTYPE_F_ALLOC:		datatype is dynamically allocated
- */
-enum datatype_flags {
-	DTYPE_F_ALLOC		= (1 << 0),
-};
-
 struct parse_ctx;
 /**
  * struct datatype
@@ -145,11 +136,12 @@ struct parse_ctx;
  * @print:	function to print a constant of this type
  * @parse:	function to parse a symbol and return an expression
  * @sym_tbl:	symbol table for this type
- * @refcnt:	reference counter (only for DTYPE_F_ALLOC)
+ * @refcnt:	reference counter (only for dynamically allocated, see .alloc)
  */
 struct datatype {
 	uint32_t			type;
-	enum byteorder			byteorder;
+	enum byteorder			byteorder:8;
+	uint32_t			alloc:1;
 	unsigned int			flags;
 	unsigned int			size;
 	unsigned int			subtypes;
src/datatype.c +4 -4
index 9293f38ed713..ea73eaf9a691 100644
@@ -1347,7 +1347,7 @@ static struct datatype *datatype_alloc(void)
 	struct datatype *dtype;
 
 	dtype = xzalloc(sizeof(*dtype));
-	dtype->flags = DTYPE_F_ALLOC;
+	dtype->alloc = 1;
 	dtype->refcnt = 1;
 
 	return dtype;
@@ -1359,7 +1359,7 @@ const struct datatype *datatype_get(const struct datatype *ptr)
 
 	if (!dtype)
 		return NULL;
-	if (!(dtype->flags & DTYPE_F_ALLOC))
+	if (!dtype->alloc)
 		return dtype;
 
 	dtype->refcnt++;
@@ -1389,7 +1389,7 @@ struct datatype *datatype_clone(const struct datatype *orig_dtype)
 	*dtype = *orig_dtype;
 	dtype->name = xstrdup(orig_dtype->name);
 	dtype->desc = xstrdup(orig_dtype->desc);
-	dtype->flags = DTYPE_F_ALLOC | orig_dtype->flags;
+	dtype->alloc = 1;
 	dtype->refcnt = 1;
 
 	return dtype;
@@ -1401,7 +1401,7 @@ void datatype_free(const struct datatype *ptr)
 
 	if (!dtype)
 		return;
-	if (!(dtype->flags & DTYPE_F_ALLOC))
+	if (!dtype->alloc)
 		return;
 
 	assert(dtype->refcnt != 0);
Signature · 1 line
2.30.2