Support wildcard netdev hooks
Phil Sutter
reviewerv2 cover Support wildcard netdev hooks
This is the remaining needed code change to support wildcard hook specs. Patch 3 also adds shell test cases to cover the functionality. The flowtable variant is skipped if 'nft list hooks' does not provide flowtable information as this requires NFNL_HOOK_TYPE_NFT_FLOWTABLE in kernel.
Phil Sutter (3):
mnl: Support simple wildcards in netdev hooks
parser_bison: Accept ASTERISK_STRING in flowtable_expr_member
tests: shell: Test ifname-based hooks
src/mnl.c | 19 +++++--- src/parser_bison.y | 11 +---- .../features/list_hooks_flowtable_info.sh | 7 +++ .../netdev_chain_name_based_hook_0.json-nft | 34 ++++++++++++++ .../dumps/netdev_chain_name_based_hook_0.nft | 5 +++ .../chains/netdev_chain_name_based_hook_0 | 44 ++++++++++++++++++ .../testcases/flowtable/0016name_based_hook_0 | 45 +++++++++++++++++++ .../dumps/0016name_based_hook_0.json-nft | 32 +++++++++++++ .../flowtable/dumps/0016name_based_hook_0.nft | 6 +++ 9 files changed, 188 insertions(+), 15 deletions(-) create mode 100755 tests/shell/features/list_hooks_flowtable_info.sh create mode 100644 tests/shell/testcases/chains/dumps/netdev_chain_name_based_hook_0.json-nft create mode 100644 tests/shell/testcases/chains/dumps/netdev_chain_name_based_hook_0.nft create mode 100755 tests/shell/testcases/chains/netdev_chain_name_based_hook_0 create mode 100755 tests/shell/testcases/flowtable/0016name_based_hook_0 create mode 100644 tests/shell/testcases/flowtable/dumps/0016name_based_hook_0.json-nft create mode 100644 tests/shell/testcases/flowtable/dumps/0016name_based_hook_0.nft
Signature · 1 line
2.49.0
Phil Sutter
reviewerPATCH v2 1/3 mnl: Support simple wildcards in netdev hooks
When building NFTA_FLOWTABLE_HOOK_DEVS, NFTA_HOOK_DEV or NFTA_HOOK_DEVS attributes, detect trailing asterisks in interface names and reduce attribute length accordingly. Kernel will use strncmp(), effectively performing a prefix search this way.
Deserialization (i.e., appending asterisk to interface names which don't include a trailing nul-char) happens in libnftnl.
--- src/mnl.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-)
src/mnl.c +14 -5
@@ -799,18 +799,24 @@ static void mnl_nft_chain_devs_build(struct nlmsghdr *nlh, struct cmd *cmd) {const struct expr *dev_expr = cmd->chain->dev_expr;const struct nft_dev *dev_array; +int i, len, num_devs =0 ;struct nlattr *nest_dev; -int i, num_devs =0 ; dev_array =nft_dev_array (dev_expr, &num_devs);if (num_devs ==1 ) {cmd_add_loc (cmd, nlh, dev_array[0 ].location); -mnl_attr_put_strz (nlh,NFTA_HOOK_DEV , dev_array[0 ].ifname); + len =strlen (dev_array[0 ].ifname) +1 ; +if (dev_array[0 ].ifname[len -2 ] =='*' ) + len -=2 ; +mnl_attr_put (nlh,NFTA_HOOK_DEV , len, dev_array[0 ].ifname); }else { nest_dev =mnl_attr_nest_start (nlh,NFTA_HOOK_DEVS );for (i =0 ; i < num_devs; i++) {cmd_add_loc (cmd, nlh, dev_array[i].location); -mnl_attr_put_strz (nlh,NFTA_DEVICE_NAME , dev_array[i].ifname); + len =strlen (dev_array[i].ifname) +1 ; +if (dev_array[i].ifname[len -2 ] =='*' ) + len -=2 ; +mnl_attr_put (nlh,NFTA_DEVICE_NAME , len, dev_array[i].ifname);mnl_attr_nest_end (nlh, nest_dev); } }
@@ -2084,14 +2090,17 @@ static void mnl_nft_ft_devs_build(struct nlmsghdr *nlh, struct cmd *cmd) {const struct expr *dev_expr = cmd->flowtable->dev_expr;const struct nft_dev *dev_array; +int i, len, num_devs =0 ;struct nlattr *nest_dev; -int i, num_devs=0 ; dev_array =nft_dev_array (dev_expr, &num_devs); nest_dev =mnl_attr_nest_start (nlh,NFTA_FLOWTABLE_HOOK_DEVS );for (i =0 ; i < num_devs; i++) {cmd_add_loc (cmd, nlh, dev_array[i].location); -mnl_attr_put_strz (nlh,NFTA_DEVICE_NAME , dev_array[i].ifname); + len =strlen (dev_array[i].ifname) +1 ; +if (dev_array[i].ifname[len -2 ] =='*' ) + len -=2 ; +mnl_attr_put (nlh,NFTA_DEVICE_NAME , len, dev_array[i].ifname); }mnl_attr_nest_end (nlh, nest_dev);
Signature · 1 line
2.49.0
Florian Westphal
maintainerRe: [nft PATCH v2 1/3] mnl: Support simple wildcards in netdev hooks
src/mnl.c static void mnl_nft_chain_devs_build(struct nlmsghdr *nlh, struct cmd *cmd) · line 810
Quoted 3 lines · Phil Sutter
src/mnl.c +3
-0+ len =strlen (dev_array[0 ].ifname) +1 ; +if (dev_array[0 ].ifname[len -2 ] =='*' ) + len -=2 ;
Not obvious to me, is there a guarantee that 'len' is 2? And, what if len yields 0 here?
Quoted 13 lines
src/mnl.c +4
-1+if (dev_array[i].ifname[len -2 ] =='*' ) + len -=2 ; +mnl_attr_put (nlh,NFTA_DEVICE_NAME , len, dev_array[i].ifname);mnl_attr_nest_end (nlh, nest_dev); } }@@ -2084,14 +2090,17 @@ static void mnl_nft_ft_devs_build(struct nlmsghdr *nlh, struct cmd *cmd) {const struct expr *dev_expr = cmd->flowtable->dev_expr;const struct nft_dev *dev_array; +int i, len, num_devs =0 ;struct nlattr *nest_dev; -int i, num_devs=0 ;
src/mnl.c static void mnl_nft_ft_devs_build(struct nlmsghdr *nlh, struct cmd *cmd) · line 2103
Quoted 10 lines
src/mnl.c +4
-1dev_array =nft_dev_array (dev_expr, &num_devs); nest_dev =mnl_attr_nest_start (nlh,NFTA_FLOWTABLE_HOOK_DEVS );for (i =0 ; i < num_devs; i++) {cmd_add_loc (cmd, nlh, dev_array[i].location); -mnl_attr_put_strz (nlh,NFTA_DEVICE_NAME , dev_array[i].ifname); + len =strlen (dev_array[i].ifname) +1 ; +if (dev_array[i].ifname[len -2 ] =='*' ) + len -=2 ; +mnl_attr_put (nlh,NFTA_DEVICE_NAME , len, dev_array[i].ifname);
This (test, subtract, put) is a repeating pattern, perhaps this warrants a helper?
Phil Sutter
reviewerRe: [nft PATCH v2 1/3] mnl: Support simple wildcards in netdev hooks
Quoted 7 lines · Florian Westphal
src/mnl.c static void mnl_nft_chain_devs_build(struct nlmsghdr *nlh, struct cmd *cmd) · line 810
Quoted 3 lines · Phil Sutter
src/mnl.c +3
-0+ len =strlen (dev_array[0 ].ifname) +1 ; +if (dev_array[0 ].ifname[len -2 ] =='*' ) + len -=2 ;Not obvious to me, is there a guarantee that 'len' is 2? And, what if len yields 0 here?
Oh, right. Same goes for the introduced libnftnl helpers.
Quoted 14 lines
Quoted 14 lines
src/mnl.c +4
-1+if (dev_array[i].ifname[len -2 ] =='*' ) + len -=2 ; +mnl_attr_put (nlh,NFTA_DEVICE_NAME , len, dev_array[i].ifname);mnl_attr_nest_end (nlh, nest_dev); } }@@ -2084,14 +2090,17 @@ static void mnl_nft_ft_devs_build(struct nlmsghdr *nlh, struct cmd *cmd) {const struct expr *dev_expr = cmd->flowtable->dev_expr;const struct nft_dev *dev_array; +int i, len, num_devs =0 ;struct nlattr *nest_dev; -int i, num_devs=0 ;
Quoted 11 lines
src/mnl.c static void mnl_nft_ft_devs_build(struct nlmsghdr *nlh, struct cmd *cmd) · line 2103
Quoted 9 lines
src/mnl.c +4
-1dev_array =nft_dev_array (dev_expr, &num_devs); nest_dev =mnl_attr_nest_start (nlh,NFTA_FLOWTABLE_HOOK_DEVS );for (i =0 ; i < num_devs; i++) {cmd_add_loc (cmd, nlh, dev_array[i].location); -mnl_attr_put_strz (nlh,NFTA_DEVICE_NAME , dev_array[i].ifname); + len =strlen (dev_array[i].ifname) +1 ; +if (dev_array[i].ifname[len -2 ] =='*' ) + len -=2 ; +mnl_attr_put (nlh,NFTA_DEVICE_NAME , len, dev_array[i].ifname);This (test, subtract, put) is a repeating pattern, perhaps this warrants a helper?
It's even there already, (still) named 'mnl_attr_put_ifname' in my libnftnl patch. I'll export it (with correct prefix) for use in nftables.
Thanks, Phil
Phil Sutter
reviewerPATCH v2 2/3 parser_bison: Accept ASTERISK_STRING in flowtable_expr_member
In reply to Phil SutterAll clauses are identical, so instead of adding a third one for ASTERISK_STRING, use a single one for 'string' (which combines all three variants).
--- src/parser_bison.y | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-)
src/parser_bison.y +1 -10
@@ -2472,16 +2472,7 @@ flowtable_list_expr : flowtable_expr_member | flowtable_list_expr COMMA opt_newline ; -flowtable_expr_member : QUOTED_STRING - { - struct expr *expr = ifname_expr_alloc(&@$, state->msgs, $1); - - if (!expr) - YYERROR; - - $$ = expr; - } - | STRING +flowtable_expr_member : string { struct expr *expr = ifname_expr_alloc(&@$, state->msgs, $1);
Signature · 1 line
2.49.0
Phil Sutter
reviewerPATCH v2 3/3 tests: shell: Test ifname-based hooks
In reply to Phil SutterAssert that:
- Non-matching interface specs are accepted
- Existing interfaces are hooked into upon flowtable/chain creation
- A new device matching the spec is hooked into immediately
- No stale hooks remain in 'nft list hooks' output
- Wildcard hooks basically work
--- .../features/list_hooks_flowtable_info.sh | 7 +++ .../netdev_chain_name_based_hook_0.json-nft | 34 ++++++++++++++ .../dumps/netdev_chain_name_based_hook_0.nft | 5 +++ .../chains/netdev_chain_name_based_hook_0 | 44 ++++++++++++++++++ .../testcases/flowtable/0016name_based_hook_0 | 45 +++++++++++++++++++ .../dumps/0016name_based_hook_0.json-nft | 32 +++++++++++++ .../flowtable/dumps/0016name_based_hook_0.nft | 6 +++ 7 files changed, 173 insertions(+) create mode 100755 tests/shell/features/list_hooks_flowtable_info.sh create mode 100644 tests/shell/testcases/chains/dumps/netdev_chain_name_based_hook_0.json-nft create mode 100644 tests/shell/testcases/chains/dumps/netdev_chain_name_based_hook_0.nft create mode 100755 tests/shell/testcases/chains/netdev_chain_name_based_hook_0 create mode 100755 tests/shell/testcases/flowtable/0016name_based_hook_0 create mode 100644 tests/shell/testcases/flowtable/dumps/0016name_based_hook_0.json-nft create mode 100644 tests/shell/testcases/flowtable/dumps/0016name_based_hook_0.nft
tests/shell/features/list_hooks_flowtable_info.sh new file +7 -0
@@ -0,0 +1,7 @@ +#!/bin/sh + +# check for flowtable info in 'list hooks' output + +unshare -n bash -c " \ +$NFT \"table inet t { flowtable ft { hook ingress priority 0; devices = { lo }; }; }\"; \ +$NFT list hooks netdev device lo | grep -q flowtable\ inet\ t\ ft"
tests/shell/testcases/chains/dumps/netdev_chain_name_based_hook_0.json-nft new file +34 -0
@@ -0,0 +1,34 @@ +{ + "nftables": [ + { + "metainfo": { + "version": "VERSION", + "release_name": "RELEASE_NAME", + "json_schema_version": 1 + } + }, + { + "table": { + "family": "netdev", + "name": "t", + "handle": 0 + } + }, + { + "chain": { + "family": "netdev", + "table": "t", + "name": "c", + "handle": 0, + "dev": [ + "foo*", + "lo" + ], + "type": "filter", + "hook": "ingress", + "prio": 0, + "policy": "accept" + } + } + ] +}
tests/shell/testcases/chains/dumps/netdev_chain_name_based_hook_0.nft new file +5 -0
@@ -0,0 +1,5 @@ +table netdev t { + chain c { + type filter hook ingress devices = { "foo*", "lo" } priority filter; policy accept; + } +}
tests/shell/testcases/chains/netdev_chain_name_based_hook_0 new file +44 -0
44 lines not on this page · raw message
tests/shell/testcases/flowtable/0016name_based_hook_0 new file +45 -0
45 lines not on this page · raw message
tests/shell/testcases/flowtable/dumps/0016name_based_hook_0.json-nft new file +32 -0
@@ -0,0 +1,32 @@ +{ + "nftables": [ + { + "metainfo": { + "version": "VERSION", + "release_name": "RELEASE_NAME", + "json_schema_version": 1 + } + }, + { + "table": { + "family": "ip", + "name": "t", + "handle": 0 + } + }, + { + "flowtable": { + "family": "ip", + "name": "ft", + "table": "t", + "handle": 0, + "hook": "ingress", + "prio": 0, + "dev": [ + "foo*", + "lo" + ] + } + } + ] +}
tests/shell/testcases/flowtable/dumps/0016name_based_hook_0.nft new file +6 -0
@@ -0,0 +1,6 @@ +table ip t { + flowtable ft { + hook ingress priority filter + devices = { "foo*", "lo" } + } +}
Signature · 1 line
2.49.0