Support wildcard netdev hooks
Phil Sutter
reviewerv3 cover Support wildcard netdev hooks
Changes since v2:
- New patch 1 fixing for excessive mnl_attr_nest_end() calls
- Introduce a helper function in patch 2 which also sanity-checks an array index - this is a copy of libnftnl's function for the same purpose, but we rather get rid of the dupicated calling code instead of exposing (and thus maintaining in future) the function from libnftnl
This is the remaining needed code change to support wildcard hook specs. Patch 4 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 (4):
mnl: Call mnl_attr_nest_end() just once
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 | 22 ++++++--- 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, 191 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 v3 1/4 mnl: Call mnl_attr_nest_end() just once
Calling the function after each added nested attribute is harmless but pointless.
--- src/mnl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
src/mnl.c +1 -1
@@ -811,8 +811,8 @@ static void mnl_nft_chain_devs_build(struct nlmsghdr *nlh, struct cmd *cmd)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); -mnl_attr_nest_end (nlh, nest_dev); } +mnl_attr_nest_end (nlh, nest_dev); }nft_dev_array_free (dev_array); }
Signature · 1 line
2.49.0
Re: [nft PATCH v3 1/4] mnl: Call mnl_attr_nest_end() just once
Quoted 2 lines · Phil Sutter
Calling the function after each added nested attribute is harmless but pointless.
Thanks, feel free to push it out.
Phil Sutter
reviewerRe: [nft PATCH v3 1/4] mnl: Call mnl_attr_nest_end() just once
Quoted 5 lines · Florian Westphal
Quoted 2 lines · Phil Sutter
Calling the function after each added nested attribute is harmless but pointless.
Thanks, feel free to push it out.
Patch applied, but ...
Quoted 1 line
Reviewed-by Florian Westphal <[email protected]>
forgot the tag. Gna!
Phil Sutter
reviewerPATCH v3 2/4 mnl: Support simple wildcards in netdev hooks
In reply to Phil SutterWhen 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.
---
Changes since v2:
- Introduce mnl_attr_put_ifname() to perform the conditional mnl_attr_put() parameter adjustment
- Sanity-check array index in above function to avoid out-of-bounds access
--- src/mnl.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-)
src/mnl.c +16 -4
@@ -795,6 +795,17 @@ static void nft_dev_array_free(const struct nft_dev *dev_array)free_const (dev_array); } +static void mnl_attr_put_ifname (struct nlmsghdr *nlh, +int attr,const char *ifname) +{ +int len =strlen (ifname) +1 ; + +if (len >=2 && ifname[len -2 ] =='*' ) + len -=2 ; + +mnl_attr_put (nlh, attr, len, ifname); +} +static void mnl_nft_chain_devs_build (struct nlmsghdr *nlh,struct cmd *cmd) {const struct expr *dev_expr = cmd->chain->dev_expr;
@@ -805,12 +816,13 @@ static void mnl_nft_chain_devs_build(struct nlmsghdr *nlh, struct cmd *cmd) 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); +mnl_attr_put_ifname (nlh,NFTA_HOOK_DEV , 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); +mnl_attr_put_ifname (nlh,NFTA_DEVICE_NAME , + dev_array[i].ifname); }mnl_attr_nest_end (nlh, nest_dev); }
@@ -2085,13 +2097,13 @@ 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;struct nlattr *nest_dev; -int i, num_devs=0 ; +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); +mnl_attr_put_ifname (nlh,NFTA_DEVICE_NAME , dev_array[i].ifname); }mnl_attr_nest_end (nlh, nest_dev);
Signature · 1 line
2.49.0
Florian Westphal
maintainerRe: [nft PATCH v3 2/4] mnl: Support simple wildcards in netdev hooks
src/mnl.c static void nft_dev_array_free(const struct nft_dev *dev_array) · line 801
Quoted 4 lines · Phil Sutter
src/mnl.c +4
-0+static void mnl_attr_put_ifname (struct nlmsghdr *nlh, +int attr,const char *ifname) +{ +int len =strlen (ifname) +1 ;
Nit: size_t len
Phil Sutter
reviewerRe: [nft PATCH v3 2/4] mnl: Support simple wildcards in netdev hooks
Quoted 7 lines · Florian Westphal
src/mnl.c static void nft_dev_array_free(const struct nft_dev *dev_array) · line 801
Quoted 4 lines · Phil Sutter
src/mnl.c +4
-0+static void mnl_attr_put_ifname (struct nlmsghdr *nlh, +int attr,const char *ifname) +{ +int len =strlen (ifname) +1 ;Nit:
size_tlen
Ah, and 'uint16_t attr' as well. I'd fix both before pushing the series, if that's fine and no v4 is needed.
Thanks, Phil
Phil Sutter
reviewerPATCH v3 3/4 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 v3 4/4 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