nftables/SOURCES/0154-parser_bison-compact-and-simplify-list-and-reset-syn.patch
2026-08-26 08:03:54 -04:00

283 lines
9.2 KiB
Diff

From 46e8c222f8d1faeeb495e34495fdcc59633a8eaf Mon Sep 17 00:00:00 2001
From: Phil Sutter <psutter@redhat.com>
Date: Fri, 17 Jul 2026 11:11:00 +0200
Subject: [PATCH] parser_bison: compact and simplify list and reset syntax
JIRA: https://issues.redhat.com/browse/RHEL-190549
Upstream Status: nftables commit b15854ef81b4c22e4660d3876384f375554887b2
commit b15854ef81b4c22e4660d3876384f375554887b2
Author: Florian Westphal <fw@strlen.de>
Date: Thu Jan 16 09:32:01 2025 +0100
parser_bison: compact and simplify list and reset syntax
Works:
list sets
list sets inet
list sets table inet foo
Doesn't work:
list sets inet foo
Same for "list counters", "list quotas", etc.
"reset" keyword however supports this:
reset counters inet foo
and aliased this to
reset counters table inet foo
This is inconsistent and not inuitive.
Moreover, unlike "list sets", "list maps" only supported "list maps" and
"list maps inet", without the ability to only list maps of a given table.
Compact this to unify the syntax so it becomes possible to omit the "table"
keyword for either reset or list mode.
flowtables, secmarks and synproxys keywords are updated too. "flow table"
and "meters" are NOT changed since both of these are deprecated in favor
of standard nft sets.
Reported-by: Slavko <linux@slavino.sk>
Signed-off-by: Florian Westphal <fw@strlen.de>
Reviewed-by: Pablo Neira Ayuso <pablo@netfilter.org>
Signed-off-by: Phil Sutter <psutter@redhat.com>
---
doc/nft.txt | 15 +++------
src/parser_bison.y | 79 ++++++++++++----------------------------------
2 files changed, 26 insertions(+), 68 deletions(-)
diff --git a/doc/nft.txt b/doc/nft.txt
index 2080c07..77f2bc3 100644
--- a/doc/nft.txt
+++ b/doc/nft.txt
@@ -585,7 +585,7 @@ section describes nft set syntax in more detail.
[verse]
*add set* ['family'] 'table' 'set' *{ type* 'type' | *typeof* 'expression' *;* [*flags* 'flags' *;*] [*timeout* 'timeout' *;*] [*gc-interval* 'gc-interval' *;*] [*elements = {* 'element'[*,* ...] *} ;*] [*size* 'size' *;*] [*comment* 'comment' *;*'] [*policy* 'policy' *;*] [*auto-merge ;*] *}*
{*delete* | *destroy* | *list* | *flush* | *reset* } *set* ['family'] 'table' 'set'
-*list sets* ['family']
+*list sets* ['family'] ['table']
*delete set* ['family'] 'table' *handle* 'handle'
{*add* | *delete* | *destroy* } *element* ['family'] 'table' 'set' *{* 'element'[*,* ...] *}*
@@ -639,7 +639,7 @@ MAPS
[verse]
*add map* ['family'] 'table' 'map' *{ type* 'type' | *typeof* 'expression' [*flags* 'flags' *;*] [*elements = {* 'element'[*,* ...] *} ;*] [*size* 'size' *;*] [*comment* 'comment' *;*'] [*policy* 'policy' *;*] *}*
{*delete* | *destroy* | *list* | *flush* | *reset* } *map* ['family'] 'table' 'map'
-*list maps* ['family']
+*list maps* ['family'] ['table']
Maps store data based on some specific key used as input. They are uniquely identified by a user-defined name and attached to tables.
@@ -736,7 +736,7 @@ FLOWTABLES
-----------
[verse]
{*add* | *create*} *flowtable* ['family'] 'table' 'flowtable' *{ hook* 'hook' *priority* 'priority' *; devices = {* 'device'[*,* ...] *} ; }*
-*list flowtables* ['family']
+*list flowtables* ['family'] ['table']
{*delete* | *destroy* | *list*} *flowtable* ['family'] 'table' 'flowtable'
*delete* *flowtable* ['family'] 'table' *handle* 'handle'
@@ -787,13 +787,8 @@ STATEFUL OBJECTS
*destroy* 'counter' ['family'] 'table' *handle* 'handle'
*destroy* 'quota' ['family'] 'table' *handle* 'handle'
*destroy* 'limit' ['family'] 'table' *handle* 'handle'
-*list counters* ['family']
-*list quotas* ['family']
-*list limits* ['family']
-*reset counters* ['family']
-*reset quotas* ['family']
-*reset counters* ['family'] 'table'
-*reset quotas* ['family'] 'table'
+*list* { *counters* | *limits* | *quotas* } ['family'] ['table']
+*reset* { *counters* | *quotas* } ['family'] ['table']
Stateful objects are attached to tables and are identified by a unique name.
They group stateful information from rules, to reference them in rules the
diff --git a/src/parser_bison.y b/src/parser_bison.y
index 6435e8e..e601934 100644
--- a/src/parser_bison.y
+++ b/src/parser_bison.y
@@ -725,6 +725,9 @@ int nft_lex(void *, void *, void *);
%type <handle> basehook_spec
%destructor { handle_free(&$$); } basehook_spec
+%type <handle> list_cmd_spec_any list_cmd_spec_table
+%destructor { handle_free(&$$); } list_cmd_spec_any list_cmd_spec_table
+
%type <val> family_spec family_spec_explicit
%type <val32> int_num chain_policy
%type <prio_spec> extended_prio_spec prio_spec
@@ -1568,6 +1571,13 @@ get_cmd : ELEMENT set_spec set_block_expr
}
;
+list_cmd_spec_table : TABLE table_spec { $$ = $2; }
+ | table_spec
+ ;
+list_cmd_spec_any : list_cmd_spec_table
+ | ruleset_spec
+ ;
+
list_cmd : TABLE table_spec
{
$$ = cmd_alloc(CMD_LIST, CMD_OBJ_TABLE, &$2, &@$, NULL);
@@ -1584,74 +1594,50 @@ list_cmd : TABLE table_spec
{
$$ = cmd_alloc(CMD_LIST, CMD_OBJ_CHAINS, &$2, &@$, NULL);
}
- | SETS ruleset_spec
+ | SETS list_cmd_spec_any
{
$$ = cmd_alloc(CMD_LIST, CMD_OBJ_SETS, &$2, &@$, NULL);
}
- | SETS TABLE table_spec
- {
- $$ = cmd_alloc(CMD_LIST, CMD_OBJ_SETS, &$3, &@$, NULL);
- }
| SET set_spec
{
$$ = cmd_alloc(CMD_LIST, CMD_OBJ_SET, &$2, &@$, NULL);
}
- | COUNTERS ruleset_spec
+ | COUNTERS list_cmd_spec_any
{
$$ = cmd_alloc(CMD_LIST, CMD_OBJ_COUNTERS, &$2, &@$, NULL);
}
- | COUNTERS TABLE table_spec
- {
- $$ = cmd_alloc(CMD_LIST, CMD_OBJ_COUNTERS, &$3, &@$, NULL);
- }
| COUNTER obj_spec close_scope_counter
{
$$ = cmd_alloc(CMD_LIST, CMD_OBJ_COUNTER, &$2, &@$, NULL);
}
- | QUOTAS ruleset_spec
+ | QUOTAS list_cmd_spec_any
{
$$ = cmd_alloc(CMD_LIST, CMD_OBJ_QUOTAS, &$2, &@$, NULL);
}
- | QUOTAS TABLE table_spec
- {
- $$ = cmd_alloc(CMD_LIST, CMD_OBJ_QUOTAS, &$3, &@$, NULL);
- }
| QUOTA obj_spec close_scope_quota
{
$$ = cmd_alloc(CMD_LIST, CMD_OBJ_QUOTA, &$2, &@$, NULL);
}
- | LIMITS ruleset_spec
+ | LIMITS list_cmd_spec_any
{
$$ = cmd_alloc(CMD_LIST, CMD_OBJ_LIMITS, &$2, &@$, NULL);
}
- | LIMITS TABLE table_spec
- {
- $$ = cmd_alloc(CMD_LIST, CMD_OBJ_LIMITS, &$3, &@$, NULL);
- }
| LIMIT obj_spec close_scope_limit
{
$$ = cmd_alloc(CMD_LIST, CMD_OBJ_LIMIT, &$2, &@$, NULL);
}
- | SECMARKS ruleset_spec
+ | SECMARKS list_cmd_spec_any
{
$$ = cmd_alloc(CMD_LIST, CMD_OBJ_SECMARKS, &$2, &@$, NULL);
}
- | SECMARKS TABLE table_spec
- {
- $$ = cmd_alloc(CMD_LIST, CMD_OBJ_SECMARKS, &$3, &@$, NULL);
- }
| SECMARK obj_spec close_scope_secmark
{
$$ = cmd_alloc(CMD_LIST, CMD_OBJ_SECMARK, &$2, &@$, NULL);
}
- | SYNPROXYS ruleset_spec
+ | SYNPROXYS list_cmd_spec_any
{
$$ = cmd_alloc(CMD_LIST, CMD_OBJ_SYNPROXYS, &$2, &@$, NULL);
}
- | SYNPROXYS TABLE table_spec
- {
- $$ = cmd_alloc(CMD_LIST, CMD_OBJ_SYNPROXYS, &$3, &@$, NULL);
- }
| SYNPROXY obj_spec close_scope_synproxy
{
$$ = cmd_alloc(CMD_LIST, CMD_OBJ_SYNPROXY, &$2, &@$, NULL);
@@ -1676,7 +1662,7 @@ list_cmd : TABLE table_spec
{
$$ = cmd_alloc(CMD_LIST, CMD_OBJ_METER, &$2, &@$, NULL);
}
- | FLOWTABLES ruleset_spec
+ | FLOWTABLES list_cmd_spec_any
{
$$ = cmd_alloc(CMD_LIST, CMD_OBJ_FLOWTABLES, &$2, &@$, NULL);
}
@@ -1684,7 +1670,7 @@ list_cmd : TABLE table_spec
{
$$ = cmd_alloc(CMD_LIST, CMD_OBJ_FLOWTABLE, &$2, &@$, NULL);
}
- | MAPS ruleset_spec
+ | MAPS list_cmd_spec_any
{
$$ = cmd_alloc(CMD_LIST, CMD_OBJ_MAPS, &$2, &@$, NULL);
}
@@ -1726,36 +1712,18 @@ basehook_spec : ruleset_spec
}
;
-reset_cmd : COUNTERS ruleset_spec
+reset_cmd : COUNTERS list_cmd_spec_any
{
$$ = cmd_alloc(CMD_RESET, CMD_OBJ_COUNTERS, &$2, &@$, NULL);
}
- | COUNTERS table_spec
- {
- $$ = cmd_alloc(CMD_RESET, CMD_OBJ_COUNTERS, &$2, &@$, NULL);
- }
- | COUNTERS TABLE table_spec
- {
- /* alias of previous rule. */
- $$ = cmd_alloc(CMD_RESET, CMD_OBJ_COUNTERS, &$3, &@$, NULL);
- }
| COUNTER obj_spec close_scope_counter
{
$$ = cmd_alloc(CMD_RESET, CMD_OBJ_COUNTER, &$2,&@$, NULL);
}
- | QUOTAS ruleset_spec
+ | QUOTAS list_cmd_spec_any
{
$$ = cmd_alloc(CMD_RESET, CMD_OBJ_QUOTAS, &$2, &@$, NULL);
}
- | QUOTAS TABLE table_spec
- {
- $$ = cmd_alloc(CMD_RESET, CMD_OBJ_QUOTAS, &$3, &@$, NULL);
- }
- | QUOTAS table_spec
- {
- /* alias of previous rule. */
- $$ = cmd_alloc(CMD_RESET, CMD_OBJ_QUOTAS, &$2, &@$, NULL);
- }
| QUOTA obj_spec close_scope_quota
{
$$ = cmd_alloc(CMD_RESET, CMD_OBJ_QUOTA, &$2, &@$, NULL);
@@ -1764,15 +1732,10 @@ reset_cmd : COUNTERS ruleset_spec
{
$$ = cmd_alloc(CMD_RESET, CMD_OBJ_RULES, &$2, &@$, NULL);
}
- | RULES table_spec
+ | RULES list_cmd_spec_table
{
$$ = cmd_alloc(CMD_RESET, CMD_OBJ_TABLE, &$2, &@$, NULL);
}
- | RULES TABLE table_spec
- {
- /* alias of previous rule. */
- $$ = cmd_alloc(CMD_RESET, CMD_OBJ_TABLE, &$3, &@$, NULL);
- }
| RULES chain_spec
{
$$ = cmd_alloc(CMD_RESET, CMD_OBJ_CHAIN, &$2, &@$, NULL);