ynl: Fix tc filters with actions
Subsystems (3)
v2 cover ynl: Fix tc filters with actions
The first patch in this series introduces an example tool that creates, shows and deletes flower filter with two VLAN actions. The subsequent patches address various issues to ensure the tool operates as intended.
---
v2:
- extend the sampe tool to show and delete the filter
- drop fix for ynl_attr_put_str as already fixed by:
- make indexed-arrays to start from index 1.
v1: https://lore.kernel.org/netdev/[email protected]/
Zahari Doychev (3):
ynl: samples: add tc filter example
tools: ynl: call nested attribute free function for indexed arrays
tools: ynl: ignore index 0 for indexed-arrays
Documentation/netlink/specs/tc.yaml | 8 + tools/net/ynl/Makefile.deps | 1 + tools/net/ynl/pyynl/ynl_gen_c.py | 14 +- tools/net/ynl/samples/.gitignore | 1 + tools/net/ynl/samples/tc-filter-add.c | 308 ++++++++++++++++++++++++++ 5 files changed, 331 insertions(+), 1 deletion(-) create mode 100644 tools/net/ynl/samples/tc-filter-add.c
Signature · 1 line
2.51.0
PATCH v2 1/3 ynl: samples: add tc filter example
Add a sample tool demonstrating how to add, dump, and delete a flower filter with two VLAN push actions. The example can be invoked as:
# ./tc-filter-add p2
flower pref 2211
flower vlan_id: 255
action order: 1 vlan push id 255
action order: 2 vlan push id 555
This verifies correct handling of tc action attributes for multiple VLAN push actions.
--- tools/net/ynl/Makefile.deps | 1 + tools/net/ynl/samples/.gitignore | 1 + tools/net/ynl/samples/tc-filter-add.c | 308 ++++++++++++++++++++++++++ 3 files changed, 310 insertions(+) create mode 100644 tools/net/ynl/samples/tc-filter-add.c
tools/net/ynl/Makefile.deps +1 -0
@@ -47,4 +47,5 @@ CFLAGS_tc:= $(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h) \ $(call get_hdr_inc,_TC_MIRRED_H,tc_act/tc_mirred.h) \ $(call get_hdr_inc,_TC_SKBEDIT_H,tc_act/tc_skbedit.h) \ $(call get_hdr_inc,_TC_TUNNEL_KEY_H,tc_act/tc_tunnel_key.h) +CFLAGS_tc-filter-add:=$(CFLAGS_tc) CFLAGS_tcp_metrics:=$(call get_hdr_inc,_LINUX_TCP_METRICS_H,tcp_metrics.h)
tools/net/ynl/samples/.gitignore +1 -0
@@ -7,3 +7,4 @@ rt-addr rt-link rt-route tc +tc-filter-add
tools/net/ynl/samples/tc-filter-add.c new file +308 -0
308 lines not on this page · raw message
Signature · 1 line
2.51.0
Jakub Kicinski
maintainerRe: [PATCH v2 1/3] ynl: samples: add tc filter example
tools/net/ynl/Makefile.deps CFLAGS_tc:= $(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h) \ · line 50
Quoted 9 lines · On Thu, 6 Nov 2025 16:15:27 +0100 Zahari Doychev wrote:
tools/net/ynl/Makefile.deps +1
-0@@ -47,4 +47,5 @@ CFLAGS_tc:= $(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h) \ $(call get_hdr_inc,_TC_MIRRED_H,tc_act/tc_mirred.h) \ $(call get_hdr_inc,_TC_SKBEDIT_H,tc_act/tc_skbedit.h) \ $(call get_hdr_inc,_TC_TUNNEL_KEY_H,tc_act/tc_tunnel_key.h) +CFLAGS_tc-filter-add:=$(CFLAGS_tc)
Why do we need this? This file is intended for families themselves, if sample needs flags it should be specified in samples/Makefile ?
Quoted 1 line
tools/net/ynl/Makefile.deps +0
-0CFLAGS_tcp_metrics:=$(call get_hdr_inc,_LINUX_TCP_METRICS_H,tcp_metrics.h)
Re: [PATCH v2 1/3] ynl: samples: add tc filter example
Quoted 13 lines · Jakub Kicinski
tools/net/ynl/Makefile.deps CFLAGS_tc:= $(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h) \ · line 50
Quoted 9 lines · On Thu, 6 Nov 2025 16:15:27 +0100 Zahari Doychev wrote:
tools/net/ynl/Makefile.deps +1
-0@@ -47,4 +47,5 @@ CFLAGS_tc:= $(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h) \ $(call get_hdr_inc,_TC_MIRRED_H,tc_act/tc_mirred.h) \ $(call get_hdr_inc,_TC_SKBEDIT_H,tc_act/tc_skbedit.h) \ $(call get_hdr_inc,_TC_TUNNEL_KEY_H,tc_act/tc_tunnel_key.h) +CFLAGS_tc-filter-add:=$(CFLAGS_tc)Why do we need this? This file is intended for families themselves, if sample needs flags it should be specified in
samples/Makefile?
I am getting compile errors as without the CFLAGS my system headers files are used and not the ones in the kernel tree. As samples/Makfile is passing the CFLAGS_tc-filter-add when compiling I thought this was the way to do this similiar to the other examples.
Actually the following flags are fixing my problem:
-D__LINUX_PKT_SCHED_H -include ../../../../include/uapi//linux/pkt_sched.h -D__LINUX_PKT_CLS_H -include ../../../../include/uapi//linux/pkt_cls.h
If I need to fix this in samples/Makefile then I probably need to create a new target. Is this really the expectation?
Quoted 3 lines
Quoted 2 lines
tools/net/ynl/Makefile.deps +0
-0CFLAGS_tcp_metrics:=$(call get_hdr_inc,_LINUX_TCP_METRICS_H,tcp_metrics.h)
Jakub Kicinski
maintainerRe: [PATCH v2 1/3] ynl: samples: add tc filter example
Quoted 21 lines · On Tue, 11 Nov 2025 21:09:11 +0200 Zahari Doychev wrote:
Quoted 13 lines · Jakub Kicinski
tools/net/ynl/Makefile.deps CFLAGS_tc:= $(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h) \ · line 50
Quoted 9 lines · On Thu, 6 Nov 2025 16:15:27 +0100 Zahari Doychev wrote:
tools/net/ynl/Makefile.deps +1
-0@@ -47,4 +47,5 @@ CFLAGS_tc:= $(call get_hdr_inc,__LINUX_RTNETLINK_H,rtnetlink.h) \ $(call get_hdr_inc,_TC_MIRRED_H,tc_act/tc_mirred.h) \ $(call get_hdr_inc,_TC_SKBEDIT_H,tc_act/tc_skbedit.h) \ $(call get_hdr_inc,_TC_TUNNEL_KEY_H,tc_act/tc_tunnel_key.h) +CFLAGS_tc-filter-add:=$(CFLAGS_tc)Why do we need this? This file is intended for families themselves, if sample needs flags it should be specified in
samples/Makefile?I am getting compile errors as without the CFLAGS my system headers files are used and not the ones in the kernel tree. As
samples/Makfileis passing the CFLAGS_tc-filter-add when compiling I thought this was the way to do this similiar to the other examples.
Quoted 6 lines
Actually the following flags are fixing my problem:
-D__LINUX_PKT_SCHED_H -include ../../../../include/uapi//linux/pkt_sched.h -D__LINUX_PKT_CLS_H -include ../../../../include/uapi//linux/pkt_cls.hIf I need to fix this in
samples/Makefilethen I probably need to create a new target. Is this really the expectation?
I meant:
tools/net/ynl/samples/Makefile +1 -0
@@ -19,6 +19,7 @@ include $(wildcard *.d) all: $(BINS) CFLAGS_page-pool=$(CFLAGS_netdev) +CFLAGS_tc-filter-add=$(CFLAGS_tc) $(BINS): ../lib/ynl.a ../generated/protos.a $(SRCS) @echo -e '\tCC sample $@'
I could be missing something, I have 6.17 headers installed so it builds for me without any extra flags :(
PATCH v2 2/3 tools: ynl: call nested attribute free function for indexed arrays
In reply to Zahari DoychevWhen freeing indexed arrays, the corresponding free function should be called for each entry of the indexed array. For example, for for 'struct tc_act_attrs' 'tc_act_attrs_free(...)' needs to be called for each entry.
Previously, memory leaks were reported when enabling the ASAN analyzer.
=================================================================
==874==ERROR: LeakSanitizer: detected memory leaks
Direct leak of 24 byte(s) in 1 object(s) allocated from:
#0 0x7f221fd20cb5 in malloc ./debug/gcc/gcc/libsanitizer/asan/asan_malloc_linux.cpp:67
#1 0x55c98db048af in tc_act_attrs_set_options_vlan_parms ../generated/tc-user.h:2813
#2 0x55c98db048af in main ./linux/tools/net/ynl/samples/tc-filter-add.c:71Direct leak of 24 byte(s) in 1 object(s) allocated from:
#0 0x7f221fd20cb5 in malloc ./debug/gcc/gcc/libsanitizer/asan/asan_malloc_linux.cpp:67
#1 0x55c98db04a93 in tc_act_attrs_set_options_vlan_parms ../generated/tc-user.h:2813
#2 0x55c98db04a93 in main ./linux/tools/net/ynl/samples/tc-filter-add.c:74Direct leak of 10 byte(s) in 2 object(s) allocated from:
#0 0x7f221fd20cb5 in malloc ./debug/gcc/gcc/libsanitizer/asan/asan_malloc_linux.cpp:67
#1 0x55c98db0527d in tc_act_attrs_set_kind ../generated/tc-user.h:1622SUMMARY: AddressSanitizer: 58 byte(s) leaked in 4 allocation(s).
The following diff illustrates the changes introduced compared to the previous version of the code.
void tc_flower_attrs_free(struct tc_flower_attrs *obj) {
+ unsigned int i;
+
free(obj->indev);
+ for (i = 0; i < obj->_count.act; i++)
+ tc_act_attrs_free(&obj->act[i]);
free(obj->act); free(obj->key_eth_dst); free(obj->key_eth_dst_mask);
--- tools/net/ynl/pyynl/ynl_gen_c.py | 12 ++++++++++++ 1 file changed, 12 insertions(+)
tools/net/ynl/pyynl/ynl_gen_c.py +12 -0
@@ -861,6 +861,18 @@ class TypeIndexedArray(Type): return [f"{member} = {self.c_name};", f"{presence} = n_{self.c_name};"] + def free_needs_iter(self): + return self.sub_type == 'nest' + + def _free_lines(self, ri, var, ref): + lines = [] + if self.sub_type == 'nest': + lines += [ + f"for (i = 0; i < {var}->{ref}_count.{self.c_name}; i++)", + f'{self.nested_render_name}_free(&{var}->{ref}{self.c_name}[i]);', + ] + lines += f"free({var}->{ref}{self.c_name});", + return lines class TypeNestTypeValue(Type): def _complex_member_type(self, ri):
Signature · 1 line
2.51.0
PATCH v2 3/3 tools: ynl: ignore index 0 for indexed-arrays
In reply to Zahari DoychevLinux tc actions expect the action order to start from index one. To accommodate this, update the code generation so array indexing begins at 1 for tc actions.
This results in the following change:
array = ynl_attr_nest_start(nlh, TCA_FLOWER_ACT);
for (i = 0; i < obj->_count.act; i++)
- tc_act_attrs_put(nlh, i, &obj->act[i]);
+ tc_act_attrs_put(nlh, i + 1, &obj->act[i]);
ynl_attr_nest_end(nlh, array);This change does not impact other indexed array attributes at the moment, as analyzed in [1].
[1]: https://lore.kernel.org/netdev/[email protected]/
--- Documentation/netlink/specs/tc.yaml | 8 ++++++++ tools/net/ynl/pyynl/ynl_gen_c.py | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-)
Documentation/netlink/specs/tc.yaml +8 -0
@@ -2044,6 +2044,7 @@ attribute-sets: type: indexed-array sub-type: nest nested-attributes: act-attrs + doc: Index 0 is ignored and array starts from index 1. - name: police type: nest
@@ -2064,6 +2065,7 @@ attribute-sets: type: indexed-array sub-type: nest nested-attributes: act-attrs + doc: Index 0 is ignored and array starts from index 1. - name: police type: nest
@@ -2303,6 +2305,7 @@ attribute-sets: type: indexed-array sub-type: nest nested-attributes: act-attrs + doc: Index 0 is ignored and array starts from index 1. - name: police type: nest
@@ -2493,6 +2496,7 @@ attribute-sets: type: indexed-array sub-type: nest nested-attributes: act-attrs + doc: Index 0 is ignored and array starts from index 1. - name: key-eth-dst type: binary
@@ -3020,6 +3024,7 @@ attribute-sets: type: indexed-array sub-type: nest nested-attributes: act-attrs + doc: Index 0 is ignored and array starts from index 1. - name: mask type: u32
@@ -3180,6 +3185,7 @@ attribute-sets: type: indexed-array sub-type: nest nested-attributes: act-attrs + doc: Index 0 is ignored and array starts from index 1. - name: flags type: u32
@@ -3566,6 +3572,7 @@ attribute-sets: type: indexed-array sub-type: nest nested-attributes: act-attrs + doc: Index 0 is ignored and array starts from index 1. - name: taprio-attrs name-prefix: tca-taprio-attr-
@@ -3798,6 +3805,7 @@ attribute-sets: type: indexed-array sub-type: nest nested-attributes: act-attrs + doc: Index 0 is ignored and array starts from index 1. - name: indev type: string
tools/net/ynl/pyynl/ynl_gen_c.py +1 -1
@@ -852,7 +852,7 @@ class TypeIndexedArray(Type): ri.cw.p(f"ynl_attr_put(nlh, i, {var}->{self.c_name}[i], {self.checks['exact-len']});") elif self.sub_type == 'nest': ri.cw.p(f'for (i = 0; i < {var}->_count.{self.c_name}; i++)') - ri.cw.p(f"{self.nested_render_name}_put(nlh, i, &{var}->{self.c_name}[i]);") + ri.cw.p(f"{self.nested_render_name}_put(nlh, i + 1, &{var}->{self.c_name}[i]);") else: raise Exception(f"Put for IndexedArray sub-type {self.attr['sub-type']} not supported, yet") ri.cw.p('ynl_attr_nest_end(nlh, array);')
Signature · 1 line
2.51.0
Jakub Kicinski
maintainerRe: [PATCH v2 3/3] tools: ynl: ignore index 0 for indexed-arrays
Quoted 12 lines · On Thu, 6 Nov 2025 16:15:29 +0100 Zahari Doychev wrote:
Linux tc actions expect the action order to start from index one. To accommodate this, update the code generation so array indexing begins at 1 for tc actions.
This results in the following change:
array = ynl_attr_nest_start(nlh, TCA_FLOWER_ACT); for (i = 0; i < obj->_count.act; i++)(quoted diff) +1
-1- tc_act_attrs_put(nlh, i, &obj->act[i]); + tc_act_attrs_put(nlh, i + 1, &obj->act[i]); ynl_attr_nest_end(nlh, array);
Quoted 2 lines
This change does not impact other indexed array attributes at the moment, as analyzed in [1].
YNL does not aim to provide perfect interfaces with weird old families.
Re: [PATCH v2 3/3] tools: ynl: ignore index 0 for indexed-arrays
Quoted 13 lines · Jakub Kicinski
Quoted 12 lines · On Thu, 6 Nov 2025 16:15:29 +0100 Zahari Doychev wrote:
Linux tc actions expect the action order to start from index one. To accommodate this, update the code generation so array indexing begins at 1 for tc actions.
This results in the following change:
array = ynl_attr_nest_start(nlh, TCA_FLOWER_ACT); for (i = 0; i < obj->_count.act; i++)(quoted diff) +1
-1- tc_act_attrs_put(nlh, i, &obj->act[i]); + tc_act_attrs_put(nlh, i + 1, &obj->act[i]); ynl_attr_nest_end(nlh, array);
Quoted 5 lines
Quoted 2 lines
This change does not impact other indexed array attributes at the moment, as analyzed in [1].
YNL does not aim to provide perfect interfaces with weird old families.
ok, maybe it is not that bad. I think I can workaround this by creating a dummy action at index 0. Does it make sense to send an update of the example with such change?
Jakub Kicinski
maintainerRe: [PATCH v2 3/3] tools: ynl: ignore index 0 for indexed-arrays
Quoted 14 lines · On Tue, 11 Nov 2025 21:34:28 +0200 Zahari Doychev wrote:
Quoted 13 lines · Jakub Kicinski
Quoted 12 lines · On Thu, 6 Nov 2025 16:15:29 +0100 Zahari Doychev wrote:
Linux tc actions expect the action order to start from index one. To accommodate this, update the code generation so array indexing begins at 1 for tc actions.
This results in the following change:
array = ynl_attr_nest_start(nlh, TCA_FLOWER_ACT); for (i = 0; i < obj->_count.act; i++)(quoted diff) +1
-1- tc_act_attrs_put(nlh, i, &obj->act[i]); + tc_act_attrs_put(nlh, i + 1, &obj->act[i]); ynl_attr_nest_end(nlh, array);
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
This change does not impact other indexed array attributes at the moment, as analyzed in [1].
YNL does not aim to provide perfect interfaces with weird old families.
ok, maybe it is not that bad. I think I can workaround this by creating a dummy action at index 0. Does it make sense to send an update of the example with such change?
Yes, the sample itself is nice to have!
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 9 lines · On Thu, 6 Nov 2025 16:15:26 +0100 you wrote:
The first patch in this series introduces an example tool that creates, shows and deletes flower filter with two VLAN actions. The subsequent patches address various issues to ensure the tool operates as intended.
---
v2:
- extend the sampe tool to show and delete the filter
- drop fix forynl_attr_put_stras already fixed by:
Quoted 5 lines
- make indexed-arrays to start from index 1.
[...]
Here is the summary with links:
- [v2,1/3] ynl: samples: add tc filter example
(no matching commit)
- [v2,2/3] tools: ynl: call nested attribute free function for indexed arrays
https://git.kernel.org/netdev/net/c/41d0c31be29f
- [v2,3/3] tools: ynl: ignore index 0 for indexed-arrays (no matching commit)
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html