Currently vlan modification action checks existence of vlan priority by comparing it to 0. Therefore it is impossible to modify existing vlan tag to have priority 0.
For example, the following tc command will change the vlan id but will not affect vlan priority:
tc filter add dev eth1 ingress matchall action vlan modify id 300 \ priority 0 pipe mirred egress redirect dev eth2
The incoming packet on eth1:
ethertype 802.1Q (0x8100), vlan 200, p 4, ethertype IPv4
will be changed to:
ethertype 802.1Q (0x8100), vlan 300, p 4, ethertype IPv4
although the user has intended to have p == 0.
The fix is to add tcfv_push_prio_exists flag to struct tcf_vlan_params and rely on it when deciding to set the priority.
The same flag is used to avoid dumping unset vlan priority.
Change Log: v2 -> v3: - Push assumes that the priority is being set - tcf_vlan_get_fill_size accounts for priority existence
v1 -> v2: - Do not dump unset priority and fix tests accordingly - Test for priority 0 modification
Boris Sukholitko (3): net/sched: act_vlan: Fix modify to allow 0 net/sched: act_vlan: No dump for unset priority net/sched: act_vlan: Test priority 0 modification
include/net/tc_act/tc_vlan.h | 1 + net/sched/act_vlan.c | 26 ++++++++++++----- .../tc-testing/tc-tests/actions/vlan.json | 28 +++++++++++++++++-- 3 files changed, 46 insertions(+), 9 deletions(-)
Currently vlan modification action checks existence of vlan priority by comparing it to 0. Therefore it is impossible to modify existing vlan tag to have priority 0.
For example, the following tc command will change the vlan id but will not affect vlan priority:
tc filter add dev eth1 ingress matchall action vlan modify id 300 \ priority 0 pipe mirred egress redirect dev eth2
The incoming packet on eth1:
ethertype 802.1Q (0x8100), vlan 200, p 4, ethertype IPv4
will be changed to:
ethertype 802.1Q (0x8100), vlan 300, p 4, ethertype IPv4
although the user has intended to have p == 0.
The fix is to add tcfv_push_prio_exists flag to struct tcf_vlan_params and rely on it when deciding to set the priority.
Fixes: 45a497f2d149a4a8061c (net/sched: act_vlan: Introduce TCA_VLAN_ACT_MODIFY vlan action) Signed-off-by: Boris Sukholitko boris.sukholitko@broadcom.com --- include/net/tc_act/tc_vlan.h | 1 + net/sched/act_vlan.c | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/include/net/tc_act/tc_vlan.h b/include/net/tc_act/tc_vlan.h index f051046ba034..f94b8bc26f9e 100644 --- a/include/net/tc_act/tc_vlan.h +++ b/include/net/tc_act/tc_vlan.h @@ -16,6 +16,7 @@ struct tcf_vlan_params { u16 tcfv_push_vid; __be16 tcfv_push_proto; u8 tcfv_push_prio; + bool tcfv_push_prio_exists; struct rcu_head rcu; };
diff --git a/net/sched/act_vlan.c b/net/sched/act_vlan.c index 1cac3c6fbb49..a108469c664f 100644 --- a/net/sched/act_vlan.c +++ b/net/sched/act_vlan.c @@ -70,7 +70,7 @@ static int tcf_vlan_act(struct sk_buff *skb, const struct tc_action *a, /* replace the vid */ tci = (tci & ~VLAN_VID_MASK) | p->tcfv_push_vid; /* replace prio bits, if tcfv_push_prio specified */ - if (p->tcfv_push_prio) { + if (p->tcfv_push_prio_exists) { tci &= ~VLAN_PRIO_MASK; tci |= p->tcfv_push_prio << VLAN_PRIO_SHIFT; } @@ -121,6 +121,7 @@ static int tcf_vlan_init(struct net *net, struct nlattr *nla, struct tc_action_net *tn = net_generic(net, vlan_net_id); struct nlattr *tb[TCA_VLAN_MAX + 1]; struct tcf_chain *goto_ch = NULL; + bool push_prio_exists = false; struct tcf_vlan_params *p; struct tc_vlan *parm; struct tcf_vlan *v; @@ -189,7 +190,8 @@ static int tcf_vlan_init(struct net *net, struct nlattr *nla, push_proto = htons(ETH_P_8021Q); }
- if (tb[TCA_VLAN_PUSH_VLAN_PRIORITY]) + push_prio_exists = !!tb[TCA_VLAN_PUSH_VLAN_PRIORITY]; + if (push_prio_exists) push_prio = nla_get_u8(tb[TCA_VLAN_PUSH_VLAN_PRIORITY]); break; case TCA_VLAN_ACT_POP_ETH: @@ -241,6 +243,7 @@ static int tcf_vlan_init(struct net *net, struct nlattr *nla, p->tcfv_action = action; p->tcfv_push_vid = push_vid; p->tcfv_push_prio = push_prio; + p->tcfv_push_prio_exists = push_prio_exists || action == TCA_VLAN_ACT_PUSH; p->tcfv_push_proto = push_proto;
if (action == TCA_VLAN_ACT_PUSH_ETH) {
Dump vlan priority only if it has been previously set.
Fix the tests accordingly.
Signed-off-by: Boris Sukholitko boris.sukholitko@broadcom.com --- net/sched/act_vlan.c | 19 ++++++++++++++----- .../tc-testing/tc-tests/actions/vlan.json | 4 ++-- 2 files changed, 16 insertions(+), 7 deletions(-)
diff --git a/net/sched/act_vlan.c b/net/sched/act_vlan.c index a108469c664f..ccd1acfa4c55 100644 --- a/net/sched/act_vlan.c +++ b/net/sched/act_vlan.c @@ -307,8 +307,8 @@ static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a, (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, p->tcfv_push_vid) || nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL, p->tcfv_push_proto) || - (nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY, - p->tcfv_push_prio)))) + (p->tcfv_push_prio_exists && + nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY, p->tcfv_push_prio)))) goto nla_put_failure;
if (p->tcfv_action == TCA_VLAN_ACT_PUSH_ETH) { @@ -362,10 +362,19 @@ static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index)
static size_t tcf_vlan_get_fill_size(const struct tc_action *act) { - return nla_total_size(sizeof(struct tc_vlan)) + struct tcf_vlan *v = to_vlan(act); + struct tcf_vlan_params *p; + size_t ret = nla_total_size(sizeof(struct tc_vlan)) + nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_ID */ - + nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_PROTOCOL */ - + nla_total_size(sizeof(u8)); /* TCA_VLAN_PUSH_VLAN_PRIORITY */ + + nla_total_size(sizeof(u16)); /* TCA_VLAN_PUSH_VLAN_PROTOCOL */ + + spin_lock_bh(&v->tcf_lock); + p = rcu_dereference_protected(v->vlan_p, lockdep_is_held(&v->tcf_lock)); + if (p->tcfv_push_prio_exists) + ret += nla_total_size(sizeof(u8)); /* TCA_VLAN_PUSH_VLAN_PRIORITY */ + spin_unlock_bh(&v->tcf_lock); + + return ret; }
static struct tc_action_ops act_vlan_ops = { diff --git a/tools/testing/selftests/tc-testing/tc-tests/actions/vlan.json b/tools/testing/selftests/tc-testing/tc-tests/actions/vlan.json index 527ce5410314..1d9d261aa0b3 100644 --- a/tools/testing/selftests/tc-testing/tc-tests/actions/vlan.json +++ b/tools/testing/selftests/tc-testing/tc-tests/actions/vlan.json @@ -463,7 +463,7 @@ "cmdUnderTest": "$TC actions add action vlan modify protocol 802.1Q id 5 index 100", "expExitCode": "0", "verifyCmd": "$TC actions get action vlan index 100", - "matchPattern": "action order [0-9]+: vlan.*modify id 100 protocol 802.1Q priority 0 pipe.*index 100 ref", + "matchPattern": "action order [0-9]+: vlan.*modify id 100 protocol 802.1Q pipe.*index 100 ref", "matchCount": "0", "teardown": [ "$TC actions flush action vlan" @@ -487,7 +487,7 @@ "cmdUnderTest": "$TC actions add action vlan modify protocol 802.1ad id 500 reclassify index 12", "expExitCode": "0", "verifyCmd": "$TC actions get action vlan index 12", - "matchPattern": "action order [0-9]+: vlan.*modify id 500 protocol 802.1ad priority 0 reclassify.*index 12 ref", + "matchPattern": "action order [0-9]+: vlan.*modify id 500 protocol 802.1ad reclassify.*index 12 ref", "matchCount": "1", "teardown": [ "$TC actions flush action vlan"
On Sun, 30 May 2021 14:40:51 +0300 Boris Sukholitko wrote:
diff --git a/net/sched/act_vlan.c b/net/sched/act_vlan.c index a108469c664f..ccd1acfa4c55 100644 --- a/net/sched/act_vlan.c +++ b/net/sched/act_vlan.c @@ -307,8 +307,8 @@ static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a, (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, p->tcfv_push_vid) || nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL, p->tcfv_push_proto) ||
(nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY,
p->tcfv_push_prio))))
(p->tcfv_push_prio_exists &&
goto nla_put_failure;nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY, p->tcfv_push_prio))))
if (p->tcfv_action == TCA_VLAN_ACT_PUSH_ETH) { @@ -362,10 +362,19 @@ static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index) static size_t tcf_vlan_get_fill_size(const struct tc_action *act) {
- return nla_total_size(sizeof(struct tc_vlan))
- struct tcf_vlan *v = to_vlan(act);
- struct tcf_vlan_params *p;
- size_t ret = nla_total_size(sizeof(struct tc_vlan))
- nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_ID */
+ nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_PROTOCOL */
+ nla_total_size(sizeof(u8)); /* TCA_VLAN_PUSH_VLAN_PRIORITY */
+ nla_total_size(sizeof(u16)); /* TCA_VLAN_PUSH_VLAN_PROTOCOL */
- spin_lock_bh(&v->tcf_lock);
- p = rcu_dereference_protected(v->vlan_p, lockdep_is_held(&v->tcf_lock));
- if (p->tcfv_push_prio_exists)
ret += nla_total_size(sizeof(u8)); /* TCA_VLAN_PUSH_VLAN_PRIORITY */
- spin_unlock_bh(&v->tcf_lock);
This jumps out a little bit - if we need to take this lock to inspect tcf_vlan_params, then I infer its value may change. And if it may change what guarantees it doesn't change between calculating the skb length and dumping?
It's common practice to calculate the max skb len required when attributes are this small.
- return ret;
}
Hi Jacub,
On Mon, May 31, 2021 at 10:21:36PM -0700, Jakub Kicinski wrote:
On Sun, 30 May 2021 14:40:51 +0300 Boris Sukholitko wrote:
diff --git a/net/sched/act_vlan.c b/net/sched/act_vlan.c index a108469c664f..ccd1acfa4c55 100644 --- a/net/sched/act_vlan.c +++ b/net/sched/act_vlan.c @@ -307,8 +307,8 @@ static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a, (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, p->tcfv_push_vid) || nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL, p->tcfv_push_proto) ||
(nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY,
p->tcfv_push_prio))))
(p->tcfv_push_prio_exists &&
goto nla_put_failure;nla_put_u8(skb, TCA_VLAN_PUSH_VLAN_PRIORITY, p->tcfv_push_prio))))
if (p->tcfv_action == TCA_VLAN_ACT_PUSH_ETH) { @@ -362,10 +362,19 @@ static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index) static size_t tcf_vlan_get_fill_size(const struct tc_action *act) {
- return nla_total_size(sizeof(struct tc_vlan))
- struct tcf_vlan *v = to_vlan(act);
- struct tcf_vlan_params *p;
- size_t ret = nla_total_size(sizeof(struct tc_vlan))
- nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_ID */
+ nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_PROTOCOL */
+ nla_total_size(sizeof(u8)); /* TCA_VLAN_PUSH_VLAN_PRIORITY */
+ nla_total_size(sizeof(u16)); /* TCA_VLAN_PUSH_VLAN_PROTOCOL */
- spin_lock_bh(&v->tcf_lock);
- p = rcu_dereference_protected(v->vlan_p, lockdep_is_held(&v->tcf_lock));
- if (p->tcfv_push_prio_exists)
ret += nla_total_size(sizeof(u8)); /* TCA_VLAN_PUSH_VLAN_PRIORITY */
- spin_unlock_bh(&v->tcf_lock);
This jumps out a little bit - if we need to take this lock to inspect tcf_vlan_params, then I infer its value may change. And if it may change what guarantees it doesn't change between calculating the skb length and dumping?
It's common practice to calculate the max skb len required when attributes are this small.
I believe you are right.
I've just sent out v4 of the patch with tcf_vlan_get_fill_size change reverted.
Thanks, Boris.
On Tue, Jun 01, 2021 at 03:35:10PM +0300, Boris Sukholitko wrote:
Hi Jacub,
On Mon, May 31, 2021 at 10:21:36PM -0700, Jakub Kicinski wrote:
On Sun, 30 May 2021 14:40:51 +0300 Boris Sukholitko wrote:
diff --git a/net/sched/act_vlan.c b/net/sched/act_vlan.c
[...]
@@ -362,10 +362,19 @@ static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index) static size_t tcf_vlan_get_fill_size(const struct tc_action *act) {
- return nla_total_size(sizeof(struct tc_vlan))
- struct tcf_vlan *v = to_vlan(act);
- struct tcf_vlan_params *p;
- size_t ret = nla_total_size(sizeof(struct tc_vlan))
- nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_ID */
+ nla_total_size(sizeof(u16)) /* TCA_VLAN_PUSH_VLAN_PROTOCOL */
+ nla_total_size(sizeof(u8)); /* TCA_VLAN_PUSH_VLAN_PRIORITY */
+ nla_total_size(sizeof(u16)); /* TCA_VLAN_PUSH_VLAN_PROTOCOL */
- spin_lock_bh(&v->tcf_lock);
- p = rcu_dereference_protected(v->vlan_p, lockdep_is_held(&v->tcf_lock));
- if (p->tcfv_push_prio_exists)
ret += nla_total_size(sizeof(u8)); /* TCA_VLAN_PUSH_VLAN_PRIORITY */
- spin_unlock_bh(&v->tcf_lock);
This jumps out a little bit - if we need to take this lock to inspect tcf_vlan_params, then I infer its value may change. And if it may change what guarantees it doesn't change between calculating the skb length and dumping?
It's common practice to calculate the max skb len required when attributes are this small.
I believe you are right.
ouch, that's my fault actually - it's true, TC rules can be modified and dumped at the same time. Then the only thing we can do is to account for TCA_VLAN_PUSH_VLAN_PRIORITY even if we will not fill it.
thanks for spotting this,
Because explicitly being set, the priority 0 should appear in the output.
Signed-off-by: Boris Sukholitko boris.sukholitko@broadcom.com --- .../tc-testing/tc-tests/actions/vlan.json | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+)
diff --git a/tools/testing/selftests/tc-testing/tc-tests/actions/vlan.json b/tools/testing/selftests/tc-testing/tc-tests/actions/vlan.json index 1d9d261aa0b3..7467021b31ca 100644 --- a/tools/testing/selftests/tc-testing/tc-tests/actions/vlan.json +++ b/tools/testing/selftests/tc-testing/tc-tests/actions/vlan.json @@ -445,6 +445,30 @@ "matchCount": "0", "teardown": [] }, + { + "id": "ba5b", + "name": "Add vlan modify action for protocol 802.1Q setting priority 0", + "category": [ + "actions", + "vlan" + ], + "setup": [ + [ + "$TC actions flush action vlan", + 0, + 1, + 255 + ] + ], + "cmdUnderTest": "$TC actions add action vlan modify protocol 802.1Q id 5 priority 0 index 100", + "expExitCode": "0", + "verifyCmd": "$TC actions get action vlan index 100", + "matchPattern": "action order [0-9]+: vlan.*modify id 100 priority 0 protocol 802.1Q pipe.*index 100 ref", + "matchCount": "0", + "teardown": [ + "$TC actions flush action vlan" + ] + }, { "id": "6812", "name": "Add vlan modify action for protocol 802.1Q",
On Sun, May 30, 2021 at 02:40:49PM +0300, Boris Sukholitko wrote:
Currently vlan modification action checks existence of vlan priority by comparing it to 0. Therefore it is impossible to modify existing vlan tag to have priority 0.
hello Boris, thanks for following up!
Change Log: v2 -> v3:
- Push assumes that the priority is being set
- tcf_vlan_get_fill_size accounts for priority existence
Reviewed-by: Davide Caratti dcaratti@redhat.com
On 2021-05-31 7:57 a.m., Davide Caratti wrote:
On Sun, May 30, 2021 at 02:40:49PM +0300, Boris Sukholitko wrote:
Currently vlan modification action checks existence of vlan priority by comparing it to 0. Therefore it is impossible to modify existing vlan tag to have priority 0.
hello Boris, thanks for following up!
Change Log: v2 -> v3:
- Push assumes that the priority is being set
- tcf_vlan_get_fill_size accounts for priority existence
Reviewed-by: Davide Caratti dcaratti@redhat.com
Looks good to me as well. And thanks for adding the tests!
Acked-by: Jamal Hadi Salim jhs@mojatatu.com
cheers, jamal
linux-kselftest-mirror@lists.linaro.org