Skip to contentSkip to conversation

parser_bison: Less STRING more tokens

RFC PATCH In review Phil Sutter 2 participants 17 messages Nov 26, 2025 – Nov 28, 2025 netfilter

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

cover parser_bison: Less STRING more tokens

Introducing new tokens should be harmless as long as they are scoped since it won't harm existing rulesets using them for user-defined names.

Leverage this to offload more input parsing to flex/bison, thereby reducing custom code size.

This series is marked RFC mostly for the last patch which introduces an exclusive start condition to defeat the scanner's tendency to fall back to STRING token when one really wishes it to split up the input string as all parts are well-defined.

The other patches are less debatable but given upstream's hesitation to add new tokens in the past, I'd like to propose this as a formal policy change.

Phil Sutter (6):
parser_bison: Introduce tokens for monitor events
parser_bison: Introduce tokens for chain types
parser_bison: Introduce tokens for osf ttl values
parser_bison: Introduce tokens for log levels
parser_bison: Introduce bytes_unit
scanner: Introduce SCANSTATE_RATE

 include/datatype.h           |   7 --
 include/parser.h             |   1 +
 include/rule.h               |  12 ++-
 src/datatype.c               |  61 -----------
 src/evaluate.c               |  22 +---
 src/parser_bison.y           | 204 +++++++++++------------------------
 src/rule.c                   |  22 +---
 src/scanner.l                |  76 ++++++++-----
 tests/py/any/limit.t         |   6 ++
 tests/py/any/limit.t.json    |  70 ++++++++++++
 tests/py/any/limit.t.payload |  20 ++++
 11 files changed, 226 insertions(+), 275 deletions(-)
Signature · 1 line
2.51.0
author reviewer
#1 lore raw

RFC PATCH 1/6 parser_bison: Introduce tokens for monitor events

There already is a start condition for "monitor" keyword and also a DESTROY token. So just add the missing one and get rid of the intermediate string buffer.

Keep checking the struct monitor::event value in eval phase just to be on the safe side.

---
 include/rule.h     | 11 +++++++++--
 src/evaluate.c     | 22 +++-------------------
 src/parser_bison.y | 10 +++++-----
 src/rule.c         |  3 +--
 src/scanner.l      |  2 ++
 5 files changed, 20 insertions(+), 28 deletions(-)
include/rule.h +9 -2
index e8b3c0376e367..4c647f732caf2 100644
@@ -739,15 +739,22 @@ enum {
 	CMD_MONITOR_OBJ_MAX
 };
 
+enum {
+	CMD_MONITOR_EVENT_ANY,
+	CMD_MONITOR_EVENT_NEW,
+	CMD_MONITOR_EVENT_DEL,
+	CMD_MONITOR_EVENT_MAX
+};
+
 struct monitor {
 	struct location	location;
 	uint32_t	format;
 	uint32_t	flags;
 	uint32_t	type;
-	const char	*event;
+	uint32_t	event;
 };
 
-struct monitor *monitor_alloc(uint32_t format, uint32_t type, const char *event);
+struct monitor *monitor_alloc(uint32_t format, uint32_t type, uint32_t event);
 void monitor_free(struct monitor *m);
 
 #define NFT_NLATTR_LOC_MAX 32
src/evaluate.c +3 -19
index 4be5299274d26..45de3ad6e5998 100644
@@ -6452,13 +6452,6 @@ static int cmd_evaluate_rename(struct eval_ctx *ctx, struct cmd *cmd)
 	return 0;
 }
 
-enum {
-	CMD_MONITOR_EVENT_ANY,
-	CMD_MONITOR_EVENT_NEW,
-	CMD_MONITOR_EVENT_DEL,
-	CMD_MONITOR_EVENT_MAX
-};
-
 static uint32_t monitor_flags[CMD_MONITOR_EVENT_MAX][CMD_MONITOR_OBJ_MAX] = {
 	[CMD_MONITOR_EVENT_ANY] = {
 		[CMD_MONITOR_OBJ_ANY]		= 0xffffffff,
@@ -6528,20 +6521,11 @@ static uint32_t monitor_flags[CMD_MONITOR_EVENT_MAX][CMD_MONITOR_OBJ_MAX] = {
 
 static int cmd_evaluate_monitor(struct eval_ctx *ctx, struct cmd *cmd)
 {
-	uint32_t event;
-
-	if (cmd->monitor->event == NULL)
-		event = CMD_MONITOR_EVENT_ANY;
-	else if (strcmp(cmd->monitor->event, "new") == 0)
-		event = CMD_MONITOR_EVENT_NEW;
-	else if (strcmp(cmd->monitor->event, "destroy") == 0)
-		event = CMD_MONITOR_EVENT_DEL;
-	else {
-		return monitor_error(ctx, cmd->monitor, "invalid event %s",
+	if (cmd->monitor->event >= CMD_MONITOR_EVENT_MAX)
+		return monitor_error(ctx, cmd->monitor, "invalid event %u",
 				     cmd->monitor->event);
-	}
 
-	cmd->monitor->flags = monitor_flags[event][cmd->monitor->type];
+	cmd->monitor->flags = monitor_flags[cmd->monitor->event][cmd->monitor->type];
 	return 0;
 }
 
src/parser_bison.y +5 -5
index 3ceef79469d7d..96d0e151b1586 100644
@@ -353,6 +353,7 @@ int nft_lex(void *, void *, void *);
 %token DESCRIBE			"describe"
 %token IMPORT			"import"
 %token EXPORT			"export"
+%token NEW			"new"
 %token DESTROY			"destroy"
 
 %token MONITOR			"monitor"
@@ -985,9 +986,7 @@ int nft_lex(void *, void *, void *);
 %destructor { expr_free($$); }	osf_expr
 
 %type <val>			markup_format
-%type <string>			monitor_event
-%destructor { free_const($$); }	monitor_event
-%type <val>			monitor_object	monitor_format
+%type <val>			monitor_event monitor_object monitor_format
 
 %type <val>			synproxy_ts	synproxy_sack
 
@@ -1892,8 +1891,9 @@ monitor_cmd		:	monitor_event	monitor_object	monitor_format
 			}
 			;
 
-monitor_event		:	/* empty */	{ $$ = NULL; }
-			|       STRING		{ $$ = $1; }
+monitor_event		:	/* empty */	{ $$ = CMD_MONITOR_EVENT_ANY; }
+			|	NEW		{ $$ = CMD_MONITOR_EVENT_NEW; }
+			|       DESTROY		{ $$ = CMD_MONITOR_EVENT_DEL; }
 			;
 
 monitor_object		:	/* empty */	{ $$ = CMD_MONITOR_OBJ_ANY; }
src/rule.c +1 -2
index 8f8b77f1e8836..a915c395acffb 100644
@@ -1389,7 +1389,7 @@ void markup_free(struct markup *m)
 	free(m);
 }
 
-struct monitor *monitor_alloc(uint32_t format, uint32_t type, const char *event)
+struct monitor *monitor_alloc(uint32_t format, uint32_t type, uint32_t event)
 {
 	struct monitor *mon;
 
@@ -1404,7 +1404,6 @@ struct monitor *monitor_alloc(uint32_t format, uint32_t type, const char *event)
 
 void monitor_free(struct monitor *m)
 {
-	free_const(m->event);
 	free(m);
 }
 
src/scanner.l +2 -0
index df8e536be2276..99ace05773816 100644
@@ -322,6 +322,8 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 <SCANSTATE_CMD_MONITOR>{
 	"rules"			{ return RULES; }
 	"trace"			{ return TRACE; }
+	"new"			{ return NEW; }
+	"destroy"		{ return DESTROY; }
 }
 "hook"			{ return HOOK; }
 "device"		{ return DEVICE; }
Signature · 1 line
2.51.0
maintainer
#2 lore raw

Re: [nft PATCH RFC 1/6] parser_bison: Introduce tokens for monitor events

Quoted 6 lines · Phil Sutter

There already is a start condition for "monitor" keyword and also a DESTROY token. So just add the missing one and get rid of the intermediate string buffer.

Keep checking the struct monitor::event value in eval phase just to be on the safe side.

Looks good, just minor nits below:

Quoted 12 lines
include/rule.h +4 -0
index e8b3c0376e367..4c647f732caf2 100644
@@ -739,15 +739,22 @@ enum {
 	CMD_MONITOR_OBJ_MAX
 };
 
+enum {
+	CMD_MONITOR_EVENT_ANY,
+	CMD_MONITOR_EVENT_NEW,
+	CMD_MONITOR_EVENT_DEL,

include/rule.h enum { · line 754

Quoted 10 lines
include/rule.h +4 -1
+	CMD_MONITOR_EVENT_MAX
+};
+
 struct monitor {
 	struct location	location;
 	uint32_t	format;
 	uint32_t	flags;
 	uint32_t	type;
-	const char	*event;
+	uint32_t	event;

any reason for using u32_t rather than an enum?

include/rule.h enum { · line 757

Quoted 2 lines
include/rule.h +1 -1
-struct monitor *monitor_alloc(uint32_t format, uint32_t type, const char *event);
+struct monitor *monitor_alloc(uint32_t format, uint32_t type, uint32_t event);

If we can get rid of internal parsing I would prefer
... , enum monitor_event_type event); or similar.

author reviewer
#3 lore raw

Re: [nft PATCH RFC 1/6] parser_bison: Introduce tokens for monitor events

Quoted 18 lines · Florian Westphal
Quoted 6 lines · Phil Sutter

There already is a start condition for "monitor" keyword and also a DESTROY token. So just add the missing one and get rid of the intermediate string buffer.

Keep checking the struct monitor::event value in eval phase just to be on the safe side.

Looks good, just minor nits below:

Quoted 8 lines
include/rule.h +0 -0
index e8b3c0376e367..4c647f732caf2 100644
@@ -739,15 +739,22 @@ enum {
 	CMD_MONITOR_OBJ_MAX
 };
 
Quoted 16 lines
Quoted 4 lines
include/rule.h +4 -0
+enum {
+	CMD_MONITOR_EVENT_ANY,
+	CMD_MONITOR_EVENT_NEW,
+	CMD_MONITOR_EVENT_DEL,

include/rule.h enum { · line 754

Quoted 10 lines
include/rule.h +4 -1
+	CMD_MONITOR_EVENT_MAX
+};
+
 struct monitor {
 	struct location	location;
 	uint32_t	format;
 	uint32_t	flags;
 	uint32_t	type;
-	const char	*event;
+	uint32_t	event;

any reason for using u32_t rather than an enum?

Nope, good point.

Quoted 5 lines

include/rule.h enum { · line 757

Quoted 2 lines
include/rule.h +1 -1
-struct monitor *monitor_alloc(uint32_t format, uint32_t type, const char *event);
+struct monitor *monitor_alloc(uint32_t format, uint32_t type, uint32_t event);

If we can get rid of internal parsing I would prefer
... , enum monitor_event_type event); or similar.

I'll then also remove CMD_MONITOR_EVENT_MAX from the enum so only allowed values are valid indexes to monitor_flags array. This eliminates the boundary check from cmd_evaluate_monitor().

Thanks, Phil

author reviewer
#4 lore raw

RFC PATCH 2/6 parser_bison: Introduce tokens for chain types

In reply to Phil Sutter

Use the already existing SCANSTATE_TYPE for keyword scoping. This is a bit of back-n-forth from string to token and back to string but it eliminates the helper function and also takes care of error handling.

Note that JSON parser does not validate the type string at all but relies upon the kernel to reject wrong ones.

---
 include/rule.h     |  1 -
 src/parser_bison.y | 28 ++++++++++++++--------------
 src/rule.c         | 19 -------------------
 src/scanner.l      |  6 ++++++
 4 files changed, 20 insertions(+), 34 deletions(-)
include/rule.h +0 -1
index 4c647f732caf2..c9a683de3880e 100644
@@ -260,7 +260,6 @@ struct chain {
 
 #define STD_PRIO_BUFSIZE 100
 extern int std_prio_lookup(const char *std_prio_name, int family, int hook);
-extern const char *chain_type_name_lookup(const char *name);
 extern const char *chain_hookname_lookup(const char *name);
 extern struct chain *chain_alloc(void);
 extern struct chain *chain_get(struct chain *chain);
src/parser_bison.y +14 -14
index 96d0e151b1586..767d5d7063c26 100644
@@ -715,6 +715,10 @@ int nft_lex(void *, void *, void *);
 
 %token XT		"xt"
 
+%token FILTER		"filter"
+%token NAT		"nat"
+%token ROUTE		"route"
+
 %type <limit_rate>		limit_rate_pkts
 %type <limit_rate>		limit_rate_bytes
 
@@ -1034,6 +1038,9 @@ int nft_lex(void *, void *, void *);
 %type <expr>			set_elem_key_expr
 %destructor { expr_free($$); }	set_elem_key_expr
 
+%type <string>			chain_type
+%destructor { free_const($$); }	chain_type
+
 %%
 
 input			:	/* empty */
@@ -2736,22 +2743,10 @@ type_identifier		:	STRING	{ $$ = $1; }
 			|	CLASSID { $$ = xstrdup("classid"); }
 			;
 
-hook_spec		:	TYPE		close_scope_type	STRING		HOOK		STRING		dev_spec	prio_spec
+hook_spec		:	TYPE		chain_type	close_scope_type	HOOK		STRING		dev_spec	prio_spec
 			{
-				const char *chain_type = chain_type_name_lookup($3);
-
-				if (chain_type == NULL) {
-					erec_queue(error(&@3, "unknown chain type"),
-						   state->msgs);
-					free_const($3);
-					free_const($5);
-					expr_free($6);
-					expr_free($7.expr);
-					YYERROR;
-				}
 				$<chain>0->type.loc = @3;
-				$<chain>0->type.str = xstrdup(chain_type);
-				free_const($3);
+				$<chain>0->type.str = xstrdup($2);
 
 				$<chain>0->loc = @$;
 				$<chain>0->hook.loc = @5;
@@ -2772,6 +2767,11 @@ hook_spec		:	TYPE		close_scope_type	STRING		HOOK		STRING		dev_spec	prio_spec
 			}
 			;
 
+chain_type		:	FILTER	{ $$ = xstrdup("filter"); }
+			|	NAT	{ $$ = xstrdup("nat"); }
+			|	ROUTE	{ $$ = xstrdup("route"); }
+			;
+
 prio_spec		:	PRIORITY extended_prio_spec
 			{
 				$$ = $2;
src/rule.c +0 -19
index a915c395acffb..d7756d2bd73dd 100644
@@ -676,25 +676,6 @@ struct symbol *symbol_lookup_fuzzy(const struct scope *scope,
 	return st.obj;
 }
 
-static const char * const chain_type_str_array[] = {
-	"filter",
-	"nat",
-	"route",
-	NULL,
-};
-
-const char *chain_type_name_lookup(const char *name)
-{
-	int i;
-
-	for (i = 0; chain_type_str_array[i]; i++) {
-		if (!strcmp(name, chain_type_str_array[i]))
-			return chain_type_str_array[i];
-	}
-
-	return NULL;
-}
-
 static const char * const chain_hookname_str_array[] = {
 	"prerouting",
 	"input",
src/scanner.l +6 -0
index 99ace05773816..b397a147ef9bd 100644
@@ -858,6 +858,12 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 	"out"			{ return OUT; }
 }
 
+<SCANSTATE_TYPE>{
+	"filter"		{ return FILTER; }
+	"nat"			{ return NAT; }
+	"route"			{ return ROUTE; }
+}
+
 "secmark"		{ scanner_push_start_cond(yyscanner, SCANSTATE_SECMARK); return SECMARK; }
 
 "xt"			{ scanner_push_start_cond(yyscanner, SCANSTATE_XT); return XT; }
Signature · 1 line
2.51.0
maintainer
#5 lore raw

Re: [nft PATCH RFC 2/6] parser_bison: Introduce tokens for chain types

Quoted 4 lines · Phil Sutter

Use the already existing SCANSTATE_TYPE for keyword scoping. This is a bit of back-n-forth from string to token and back to string but it eliminates the helper function and also takes care of error handling.

Did you run this with valgrind or CFLAGS+=-fsanitize=address ?

src/parser_bison.y type_identifier : STRING { $$ = $1; } · line 2749

Quoted 1 line
src/parser_bison.y +1 -0
+				$<chain>0->type.str = xstrdup($2);

This should probably be $<chain>0->type.str = $2;

No?

author reviewer
#6 lore raw

Re: [nft PATCH RFC 2/6] parser_bison: Introduce tokens for chain types

Quoted 11 lines · Florian Westphal
Quoted 4 lines · Phil Sutter

Use the already existing SCANSTATE_TYPE for keyword scoping. This is a bit of back-n-forth from string to token and back to string but it eliminates the helper function and also takes care of error handling.

Did you run this with valgrind or CFLAGS+=-fsanitize=address ?

src/parser_bison.y type_identifier : STRING { $$ = $1; } · line 2749

Quoted 1 line
src/parser_bison.y +1 -0
+				$<chain>0->type.str = xstrdup($2);

This should probably be $<chain>0->type.str = $2;

Oh yes, indeed! I assumed the chain_type destructor would run after reducing to hook_spec, but they're used for error path, only.

Thanks, Phil

author reviewer
#7 lore raw

RFC PATCH 3/6 parser_bison: Introduce tokens for osf ttl values

In reply to Phil Sutter

Eliminate the open-coded string parsing and error handling.

---
 src/parser_bison.y | 24 ++++++------------------
 src/scanner.l      |  6 ++++++
 2 files changed, 12 insertions(+), 18 deletions(-)
src/parser_bison.y +6 -18
index 767d5d7063c26..a190cbbbd6dee 100644
@@ -719,6 +719,9 @@ int nft_lex(void *, void *, void *);
 %token NAT		"nat"
 %token ROUTE		"route"
 
+%token LOOSE		"loose"
+%token SKIP		"skip"
+
 %type <limit_rate>		limit_rate_pkts
 %type <limit_rate>		limit_rate_bytes
 
@@ -4485,24 +4488,9 @@ osf_expr		:	OSF	osf_ttl		HDRVERSION	close_scope_osf
 			}
 			;
 
-osf_ttl			:	/* empty */
-			{
-				$$ = NF_OSF_TTL_TRUE;
-			}
-			|	TTL	STRING
-			{
-				if (!strcmp($2, "loose"))
-					$$ = NF_OSF_TTL_LESS;
-				else if (!strcmp($2, "skip"))
-					$$ = NF_OSF_TTL_NOCHECK;
-				else {
-					erec_queue(error(&@2, "invalid ttl option"),
-						   state->msgs);
-					free_const($2);
-					YYERROR;
-				}
-				free_const($2);
-			}
+osf_ttl			:	/* empty */	{ $$ = NF_OSF_TTL_TRUE; }
+			|	TTL	LOOSE	{ $$ = NF_OSF_TTL_LESS; }
+			|	TTL	SKIP	{ $$ = NF_OSF_TTL_NOCHECK; }
 			;
 
 shift_expr		:	primary_expr
src/scanner.l +6 -0
index b397a147ef9bd..e0f0aabb683a3 100644
@@ -533,6 +533,12 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 <SCANSTATE_EXPR_OSF,SCANSTATE_IP>{
 	"ttl"			{ return TTL; }
 }
+
+<SCANSTATE_EXPR_OSF>{
+	"loose"			{ return LOOSE; }
+	"skip"			{ return SKIP; }
+}
+
 <SCANSTATE_CT,SCANSTATE_IP,SCANSTATE_META,SCANSTATE_TYPE,SCANSTATE_GRE>"protocol"		{ return PROTOCOL; }
 <SCANSTATE_EXPR_MH,SCANSTATE_EXPR_UDP,SCANSTATE_EXPR_UDPLITE,SCANSTATE_ICMP,SCANSTATE_IGMP,SCANSTATE_IP,SCANSTATE_SCTP,SCANSTATE_TCP>{
 	"checksum"		{ return CHECKSUM; }
Signature · 1 line
2.51.0
author reviewer
#9 lore raw

RFC PATCH 4/6 parser_bison: Introduce tokens for log levels

In reply to Phil Sutter

Since log statement is scoped already, it's just a matter of declaring the tokens in that scope and using them. This eliminates the redundant copy of log level string parsing in parser_bison.y - the remaining one, namely log_level_parse() in statement.c is used by JSON parser.

---
 src/parser_bison.y | 46 ++++++++++++++++++----------------------------
 src/scanner.l      |  9 +++++++++
 2 files changed, 27 insertions(+), 28 deletions(-)
src/parser_bison.y +18 -28
index a190cbbbd6dee..91a03f78c611a 100644
@@ -643,6 +643,15 @@ int nft_lex(void *, void *, void *);
 %token SNAPLEN			"snaplen"
 %token QUEUE_THRESHOLD		"queue-threshold"
 %token LEVEL			"level"
+%token EMERG			"emerg"
+%token ALERT			"alert"
+%token CRIT			"crit"
+%token ERR			"err"
+%token WARN			"warn"
+%token NOTICE			"notice"
+%token INFO			"info"
+%token DEBUG_TOKEN		"debug"
+%token AUDIT			"audit"
 
 %token LIMIT			"limit"
 %token RATE			"rate"
@@ -3490,34 +3499,15 @@ log_arg			:	PREFIX			string
 			}
 			;
 
-level_type		:	string
-			{
-				if (!strcmp("emerg", $1))
-					$$ = NFT_LOGLEVEL_EMERG;
-				else if (!strcmp("alert", $1))
-					$$ = NFT_LOGLEVEL_ALERT;
-				else if (!strcmp("crit", $1))
-					$$ = NFT_LOGLEVEL_CRIT;
-				else if (!strcmp("err", $1))
-					$$ = NFT_LOGLEVEL_ERR;
-				else if (!strcmp("warn", $1))
-					$$ = NFT_LOGLEVEL_WARNING;
-				else if (!strcmp("notice", $1))
-					$$ = NFT_LOGLEVEL_NOTICE;
-				else if (!strcmp("info", $1))
-					$$ = NFT_LOGLEVEL_INFO;
-				else if (!strcmp("debug", $1))
-					$$ = NFT_LOGLEVEL_DEBUG;
-				else if (!strcmp("audit", $1))
-					$$ = NFT_LOGLEVEL_AUDIT;
-				else {
-					erec_queue(error(&@1, "invalid log level"),
-						   state->msgs);
-					free_const($1);
-					YYERROR;
-				}
-				free_const($1);
-			}
+level_type		:	EMERG		{ $$ = NFT_LOGLEVEL_EMERG; }
+			|	ALERT		{ $$ = NFT_LOGLEVEL_ALERT; }
+			|	CRIT		{ $$ = NFT_LOGLEVEL_CRIT; }
+			|	ERR		{ $$ = NFT_LOGLEVEL_ERR; }
+			|	WARN		{ $$ = NFT_LOGLEVEL_WARNING; }
+			|	NOTICE		{ $$ = NFT_LOGLEVEL_NOTICE; }
+			|	INFO		{ $$ = NFT_LOGLEVEL_INFO; }
+			|	DEBUG_TOKEN	{ $$ = NFT_LOGLEVEL_DEBUG; }
+			|	AUDIT		{ $$ = NFT_LOGLEVEL_AUDIT; }
 			;
 
 log_flags		:	TCP	log_flags_tcp	close_scope_tcp
src/scanner.l +9 -0
index e0f0aabb683a3..ca570e2bfe066 100644
@@ -433,6 +433,15 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 	"queue-threshold"	{ return QUEUE_THRESHOLD; }
 	"level"			{ return LEVEL; }
 	"group"			{ return GROUP; }
+	"emerg"			{ return EMERG; }
+	"alert"			{ return ALERT; }
+	"crit"			{ return CRIT; }
+	"err"			{ return ERR; }
+	"warn"			{ return WARN; }
+	"notice"		{ return NOTICE; }
+	"info"			{ return INFO; }
+	"debug"			{ return DEBUG_TOKEN; }
+	"audit"			{ return AUDIT; }
 }
 
 "queue"			{ scanner_push_start_cond(yyscanner, SCANSTATE_EXPR_QUEUE); return QUEUE;}
Signature · 1 line
2.51.0
maintainer Reviewed-by on v1 4/6
#10 lore raw

Re: [nft PATCH RFC 4/6] parser_bison: Introduce tokens for log levels

Quoted 4 lines · Phil Sutter

Since log statement is scoped already, it's just a matter of declaring the tokens in that scope and using them. This eliminates the redundant copy of log level string parsing in parser_bison.y - the remaining one, namely log_level_parse() in statement.c is used by JSON parser.

author reviewer
#11 lore raw

RFC PATCH 5/6 parser_bison: Introduce bytes_unit

In reply to Phil Sutter

Introduce scoped tokens for "kbytes" and "mbytes", completing already existing "bytes" one. Then generalize the unit for byte values and replace both quota_unit and limit_bytes by a combination of NUM and bytes_unit.

With this in place, data_unit_parse() is not called outside of datatype.c, so make it static.

---
 include/datatype.h |  3 --
 src/datatype.c     |  4 +--
 src/parser_bison.y | 75 +++++++++++-----------------------------------
 src/scanner.l      |  2 ++
 4 files changed, 22 insertions(+), 62 deletions(-)
include/datatype.h +0 -3
index 4fb47f158fc2f..63dba330137a0 100644
@@ -313,9 +313,6 @@ extern struct error_record *rate_parse(const struct location *loc,
 				       const char *str, uint64_t *rate,
 				       uint64_t *unit);
 
-extern struct error_record *data_unit_parse(const struct location *loc,
-					    const char *str, uint64_t *rate);
-
 struct limit_rate {
 	uint64_t rate, unit;
 };
src/datatype.c +2 -2
index fac4eb9cdcecd..1950a2f3757b8 100644
@@ -1507,8 +1507,8 @@ static struct error_record *time_unit_parse(const struct location *loc,
 	return NULL;
 }
 
-struct error_record *data_unit_parse(const struct location *loc,
-				     const char *str, uint64_t *rate)
+static struct error_record *data_unit_parse(const struct location *loc,
+					    const char *str, uint64_t *rate)
 {
 	if (strcmp(str, "bytes") == 0)
 		*rate = 1ULL;
src/parser_bison.y +18 -57

138 lines not on this page · raw message

src/scanner.l +2 -0
index ca570e2bfe066..4cbc8a44c89c8 100644
@@ -414,6 +414,8 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 <SCANSTATE_COUNTER,SCANSTATE_LIMIT,SCANSTATE_QUOTA,SCANSTATE_STMT_SYNPROXY,SCANSTATE_EXPR_OSF,SCANSTATE_TUNNEL>"name"			{ return NAME; }
 <SCANSTATE_COUNTER,SCANSTATE_CT,SCANSTATE_LIMIT>"packets"		{ return PACKETS; }
 <SCANSTATE_COUNTER,SCANSTATE_CT,SCANSTATE_LIMIT,SCANSTATE_QUOTA>"bytes"	{ return BYTES; }
+<SCANSTATE_LIMIT,SCANSTATE_QUOTA>"kbytes"	{ return KBYTES; }
+<SCANSTATE_LIMIT,SCANSTATE_QUOTA>"mbytes"	{ return MBYTES; }
 
 "last"				{ scanner_push_start_cond(yyscanner, SCANSTATE_LAST); return LAST; }
 <SCANSTATE_LAST>{
Signature · 1 line
2.51.0
maintainer Reviewed-by on v1 5/6
#12 lore raw

Re: [nft PATCH RFC 5/6] parser_bison: Introduce bytes_unit

Quoted 4 lines · Phil Sutter

Introduce scoped tokens for "kbytes" and "mbytes", completing already existing "bytes" one. Then generalize the unit for byte values and replace both quota_unit and limit_bytes by a combination of NUM and bytes_unit.

author reviewer
#13 lore raw

RFC PATCH 6/6 scanner: Introduce SCANSTATE_RATE

In reply to Phil Sutter

This is a first exclusive start condition, i.e. one which rejects unscoped tokens. When tokenizing, flex all too easily falls back into treating something as STRING when it could be split into tokens instead. Via an exclusive start condition, the string-fallback can be disabled as needed.

With rates in typical formatting <NUM><bytes-unit>/<time-unit>, tokenizer result depended on whitespace placement. SCANSTATE_RATE forces flex to split the string into tokens and fall back to JUNK upon failure. For this to work, tokens which shall still be recognized must be enabled in SCANSTATE_RATE (or all scopes denoted by '*'). This includes any tokens possibly following SCANSTATE_RATE to please the parser's lookahead behaviour.

---
 include/datatype.h           |  4 ---
 include/parser.h             |  1 +
 src/datatype.c               | 61 -------------------------------
 src/parser_bison.y           | 23 +++---------
 src/scanner.l                | 55 ++++++++++++++--------------
 tests/py/any/limit.t         |  6 ++++
 tests/py/any/limit.t.json    | 70 ++++++++++++++++++++++++++++++++++++
 tests/py/any/limit.t.payload | 20 +++++++++++
 8 files changed, 130 insertions(+), 110 deletions(-)
include/datatype.h +0 -4
index 63dba330137a0..4c5d6ff8d9002 100644
@@ -309,10 +309,6 @@ extern void time_print(uint64_t msec, struct output_ctx *octx);
 extern struct error_record *time_parse(const struct location *loc,
 				       const char *c, uint64_t *res);
 
-extern struct error_record *rate_parse(const struct location *loc,
-				       const char *str, uint64_t *rate,
-				       uint64_t *unit);
-
 struct limit_rate {
 	uint64_t rate, unit;
 };
include/parser.h +1 -0
index 8cfd22e9e6c42..889302baf5950 100644
@@ -47,6 +47,7 @@ enum startcond_type {
 	PARSER_SC_META,
 	PARSER_SC_POLICY,
 	PARSER_SC_QUOTA,
+	PARSER_SC_RATE,
 	PARSER_SC_SCTP,
 	PARSER_SC_SECMARK,
 	PARSER_SC_TCP,
src/datatype.c +0 -61

67 lines not on this page · raw message

src/parser_bison.y +5 -18
index a7e5ace067bf5..c2964799f7e97 100644
@@ -1113,6 +1113,7 @@ close_scope_osf		: { scanner_pop_start_cond(nft->scanner, PARSER_SC_EXPR_OSF); }
 close_scope_policy	: { scanner_pop_start_cond(nft->scanner, PARSER_SC_POLICY); };
 close_scope_quota	: { scanner_pop_start_cond(nft->scanner, PARSER_SC_QUOTA); };
 close_scope_queue	: { scanner_pop_start_cond(nft->scanner, PARSER_SC_EXPR_QUEUE); };
+close_scope_rate	: { scanner_pop_start_cond(nft->scanner, PARSER_SC_RATE); };
 close_scope_reject	: { scanner_pop_start_cond(nft->scanner, PARSER_SC_STMT_REJECT); };
 close_scope_reset	: { scanner_pop_start_cond(nft->scanner, PARSER_SC_CMD_RESET); };
 close_scope_rt		: { scanner_pop_start_cond(nft->scanner, PARSER_SC_EXPR_RT); };
@@ -3557,7 +3558,7 @@ limit_stmt_alloc	:	LIMIT	RATE
 			}
 			;
 
-limit_stmt		:	limit_stmt_alloc limit_args
+limit_stmt		:	limit_stmt_alloc limit_args close_scope_rate
 			;
 
 limit_args		:	limit_mode	limit_rate_pkts	limit_burst_pkts
@@ -3652,21 +3653,7 @@ limit_burst_bytes	:	/* empty */			{ $$ = 0; }
 			|	BURST	NUM	bytes_unit	{ $$ = $2 * $3; }
 			;
 
-limit_rate_bytes	:	NUM     STRING
-			{
-				struct error_record *erec;
-				uint64_t rate, unit;
-
-				erec = rate_parse(&@$, $2, &rate, &unit);
-				free_const($2);
-				if (erec != NULL) {
-					erec_queue(erec, state->msgs);
-					YYERROR;
-				}
-				$$.rate = rate * $1;
-				$$.unit = unit;
-			}
-			|	NUM bytes_unit SLASH time_unit
+limit_rate_bytes	:	NUM bytes_unit SLASH time_unit
 			{
 				$$.rate = $1 * $2;
 				$$.unit = $4;
@@ -4897,7 +4884,7 @@ ct_obj_alloc		:	/* empty */
 			}
 			;
 
-limit_config		:	RATE	limit_mode	limit_rate_pkts	limit_burst_pkts
+limit_config		:	RATE	limit_mode	limit_rate_pkts	limit_burst_pkts	close_scope_rate
 			{
 				struct limit *limit;
 
@@ -4908,7 +4895,7 @@ limit_config		:	RATE	limit_mode	limit_rate_pkts	limit_burst_pkts
 				limit->type	= NFT_LIMIT_PKTS;
 				limit->flags	= $2;
 			}
-			|	RATE	limit_mode	limit_rate_bytes	limit_burst_bytes
+			|	RATE	limit_mode	limit_rate_bytes	limit_burst_bytes close_scope_rate
 			{
 				struct limit *limit;
 
src/scanner.l +28 -27

143 lines not on this page · raw message

tests/py/any/limit.t +6 -0
index 2a84e3f56e4ef..5c95ffa52ee8a 100644
@@ -49,3 +49,9 @@ limit rate over 10230 mbytes/second;ok
 limit rate over 1025 bytes/second burst 512 bytes;ok
 limit rate over 1025 kbytes/second burst 1023 kbytes;ok
 limit rate over 1025 mbytes/second burst 1025 kbytes;ok
+
+limit rate over 1025bytes/second burst 512bytes;ok;limit rate over 1025 bytes/second burst 512 bytes
+limit rate over 1025bytes /second burst 512bytes;ok;limit rate over 1025 bytes/second burst 512 bytes
+limit rate over 1025bytes/ second burst 512bytes;ok;limit rate over 1025 bytes/second burst 512 bytes
+limit rate over 1025bytes / second burst 512bytes;ok;limit rate over 1025 bytes/second burst 512 bytes
+limit rate over 1025 bytes / second burst 512bytes;ok;limit rate over 1025 bytes/second burst 512 bytes
tests/py/any/limit.t.json +70 -0

73 lines not on this page · raw message

tests/py/any/limit.t.payload +20 -0
index dc6701b3521c9..901275dafcaa0 100644
@@ -122,3 +122,23 @@ ip test-ip4 output
 # limit rate over 1025 mbytes/second burst 1025 kbytes
 ip test-ip4 output
   [ limit rate 1074790400/second burst 1049600 type bytes flags 0x1 ]
+
+# limit rate over 1025bytes/second burst 512bytes
+ip test-ip4 output
+  [ limit rate 1025/second burst 512 type bytes flags 0x1 ]
+
+# limit rate over 1025bytes /second burst 512bytes
+ip test-ip4 output
+  [ limit rate 1025/second burst 512 type bytes flags 0x1 ]
+
+# limit rate over 1025bytes/ second burst 512bytes
+ip test-ip4 output
+  [ limit rate 1025/second burst 512 type bytes flags 0x1 ]
+
+# limit rate over 1025bytes / second burst 512bytes
+ip test-ip4 output
+  [ limit rate 1025/second burst 512 type bytes flags 0x1 ]
+
+# limit rate over 1025 bytes / second burst 512bytes
+ip test-ip4 output
+  [ limit rate 1025/second burst 512 type bytes flags 0x1 ]
Signature · 1 line
2.51.0
maintainer
#14 lore raw

Re: [nft PATCH RFC 6/6] scanner: Introduce SCANSTATE_RATE

include/parser.h enum startcond_type { · line 50

Quoted 12 lines · Phil Sutter

struct limit_rate {

include/datatype.h +0 -0
 	uint64_t rate, unit;
 };
include/parser.h +1 -0
index 8cfd22e9e6c42..889302baf5950 100644
@@ -47,6 +47,7 @@ enum startcond_type {
 	PARSER_SC_META,
 	PARSER_SC_POLICY,
 	PARSER_SC_QUOTA,
+	PARSER_SC_RATE,

Could this new scanstate get added in a different patch?

include/parser.h enum startcond_type { · line 52

Quoted 4 lines
include/parser.h +2 -2
-"hour"			{ return HOUR; }
-"day"			{ return DAY; }
+<*>"hour"		{ return HOUR; }
+<*>"day"		{ return DAY; }

this might allow to scope this to meta+rate first before adding the exclusive start condition change.

author reviewer
#15 lore raw

Re: [nft PATCH RFC 6/6] scanner: Introduce SCANSTATE_RATE

Quoted 16 lines · Florian Westphal

include/parser.h enum startcond_type { · line 50

Quoted 12 lines · Phil Sutter

struct limit_rate {

include/datatype.h +0 -0
 	uint64_t rate, unit;
 };
include/parser.h +1 -0
index 8cfd22e9e6c42..889302baf5950 100644
@@ -47,6 +47,7 @@ enum startcond_type {
 	PARSER_SC_META,
 	PARSER_SC_POLICY,
 	PARSER_SC_QUOTA,
+	PARSER_SC_RATE,

Could this new scanstate get added in a different patch?

Quoted 7 lines

include/parser.h enum startcond_type { · line 52

Quoted 4 lines
include/parser.h +2 -2
-"hour"			{ return HOUR; }
-"day"			{ return DAY; }
+<*>"hour"		{ return HOUR; }
+<*>"day"		{ return DAY; }

this might allow to scope this to meta+rate first before adding the exclusive start condition change.

Sadly, not. Unless you want to get rid of meta_key_unqualified as well:
Since nft accepts rules like "hour 10 accept", we have to keep "hour" and "day" in global scope.

Cheers, Phil

maintainer
#16 lore raw

Re: [nft PATCH RFC 6/6] scanner: Introduce SCANSTATE_RATE

Quoted 6 lines · Phil Sutter
Quoted 2 lines

this might allow to scope this to meta+rate first before adding the exclusive start condition change.

Sadly, not. Unless you want to get rid of meta_key_unqualified as well:
Since nft accepts rules like "hour 10 accept", we have to keep "hour" and "day" in global scope.

Sigh, yes, I forgot about unqualified :-(