net: mlx5: Allow police as the last action of a TC rule
Subsystems (5)
PATCH v4 1/1 net: mlx5: Allow police as the last action of a TC rule
From: Mohsin Bashir <[email protected]>
In switchdev mode, mlx5 does not offload a rule where police is the last action, although TC needs no other action for such a rule. The upcoming selftest series relies on these rules. With nothing after police, the police result alone decides where the packet goes, so each result must be offloaded as TC executes it. The driver does not do that today. An ok result is handled as pipe and runs the actions after police, a pipe result always returns to the post action table, and a jump is counted from police while TC counts it from the start of the action list. A rate police is also rejected by verify_attr_actions(), as its forward to the post meter table is added only after the check.
Make the offloaded rule behave as it does in TC. An ok result ends processing in TC. A pipe result does the same when police is the last action. Send both to the end of the TC chains as the driver already does for gact ok. For jump, offload only the case where police is the first action, no pedit or police is skipped, and no action follows itself. The driver counts a jump in flow action entries and merges only consecutive entries of the same action, so a pedit with both set and add keys counts more than once, and an action repeated in a row counts once. A skipped police takes over the jump. Reject the rest. Existing rules where police is followed by other actions also change. An ok result now accepts the packet instead of running those actions. Finally set the forward to the post meter table while parsing police. This is already done for police with mtu and lets a police-only rule pass verify_attr_actions().
Tested on a ConnectX-7 in switchdev mode with the commands below. The hardware drop counter showed that the rules are offloaded and limit the traffic to the configured rate, that the second police only sees the packets conforming to the first, and that the jump lands on its target.
tc filter add dev eth0 ingress pref 100 protocol ipv6 flower skip_sw \
ip_proto udp dst_port 5201 \
action police rate 100mbit burst 1m conform-exceed drop/ok
tc filter add dev eth0 ingress pref 100 protocol ipv6 flower skip_sw \
ip_proto udp dst_port 5201 \
action police rate 200mbit burst 1m conform-exceed drop/pipe \
action police rate 100mbit burst 1m conform-exceed drop/pipe
tc filter add dev eth0 ingress pref 100 protocol ipv6 flower skip_sw \
ip_proto udp dst_port 5201 \
action police rate 100mbit burst 1m conform-exceed drop / jump 2 \
action drop action ok---
Changelog:
v4:
- Reject only a skipped pedit or police, instead of allowing only skipped actions that are all drop or all ok, as suggested by Mark:
https://lore.kernel.org/netdev/[email protected]
v3: https://lore.kernel.org/netdev/[email protected]
v2: https://lore.kernel.org/netdev/[email protected]
v1: https://lore.kernel.org/netdev/[email protected]
--- .../mellanox/mlx5/core/en/tc/act/police.c | 59 ++++++++++++++++++- .../net/ethernet/mellanox/mlx5/core/en_tc.c | 26 +++++--- 2 files changed, 75 insertions(+), 10 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en/tc/act/police.c +58 -1
@@ -37,6 +37,52 @@ static int police_act_validate(const struct flow_action_entry *act,return 0 ; } +static bool +police_act_can_offload_jump (struct mlx5e_tc_act_parse_state *parse_state, +const struct flow_action_entry *act, +int act_index,u32 target) +{ +const struct flow_action *flow_action = parse_state->flow_action; +struct netlink_ext_ack *extack = parse_state->extack; +const struct flow_action_entry *prev, *entry; +u32 i; + +if (act_index) { +NL_SET_ERR_MSG_MOD (extack, +"Jump is only supported when police is the first action" ); +return false; + } + +if (!target || target >= flow_action->num_entries) { +NL_SET_ERR_MSG_MOD (extack,"Jump target must be a later action" ); +return false; + } + + prev = act; +for (i =1 ; i <= target; i++) { + entry = &flow_action->entries[i]; + +if (i < target && + (entry->id ==FLOW_ACTION_MANGLE || + entry->id ==FLOW_ACTION_ADD || + entry->id ==FLOW_ACTION_POLICE )) { +NL_SET_ERR_MSG_MOD (extack, +"Jump over a pedit or police action is not supported" ); +return false; + } + +if (entry->id == prev->id && entry->hw_index == prev->hw_index) { +NL_SET_ERR_MSG_MOD (extack, +"Jump over the same action twice in a row is not supported" ); +return false; + } + + prev = entry; + } + +return true; +} + static bool tc_act_can_offload_police(struct mlx5e_tc_act_parse_state *parse_state, const struct flow_action_entry *act,
@@ -49,6 +95,16 @@ tc_act_can_offload_police(struct mlx5e_tc_act_parse_state *parse_state,if (err)return false; +if (act->police.exceed.act_id ==FLOW_ACTION_JUMP && + !police_act_can_offload_jump (parse_state, act, act_index, + act->police.exceed.extval)) +return false; + +if (act->police.notexceed.act_id ==FLOW_ACTION_JUMP && + !police_act_can_offload_jump (parse_state, act, act_index, + act->police.notexceed.extval)) +return false; +return !!mlx5e_get_flow_meters (parse_state->flow->priv->mdev); }
@@ -94,13 +150,14 @@ tc_act_parse_police(struct mlx5e_tc_act_parse_state *parse_state, MLX5_FLOW_STEERING_CAP_MATCH_RANGES))return -EOPNOTSUPP ; - attr->action |=MLX5_FLOW_CONTEXT_ACTION_FWD_DEST ; attr->flags |=MLX5_ATTR_FLAG_MTU ; }else { attr->action |=MLX5_FLOW_CONTEXT_ACTION_EXECUTE_ASO ; attr->exe_aso_type =MLX5_EXE_ASO_FLOW_METER ; } + attr->action |=MLX5_FLOW_CONTEXT_ACTION_FWD_DEST ; +return 0 ; }
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c +17 -9
@@ -3877,7 +3877,7 @@ static int alloc_branch_attr(struct mlx5e_tc_flow *flow, struct mlx5e_tc_act_branch_ctrl *cond, struct mlx5_flow_attr **cond_attr, - u32 *jump_count, + u32 *jump_count, bool last, struct netlink_ext_ack *extack) { struct mlx5_flow_attr *attr;
@@ -3894,11 +3894,17 @@ alloc_branch_attr(struct mlx5e_tc_flow *flow,case FLOW_ACTION_DROP : attr->action |=MLX5_FLOW_CONTEXT_ACTION_DROP ;break ; -case FLOW_ACTION_ACCEPT :case FLOW_ACTION_PIPE : - err =set_branch_dest_ft (flow->priv, attr); -if (err) - goto out_err; +if (!last) { + err =set_branch_dest_ft (flow->priv, attr); +if (err) + goto out_err; +break ; + } + fallthrough; +case FLOW_ACTION_ACCEPT : + attr->action |=MLX5_FLOW_CONTEXT_ACTION_FWD_DEST ; + attr->flags |=MLX5_ATTR_FLAG_ACCEPT ;break ;case FLOW_ACTION_JUMP :if (*jump_count) {
@@ -3970,7 +3976,7 @@ dec_jump_count(struct flow_action_entry *act, struct mlx5e_tc_act *tc_act,static int parse_branch_ctrl (struct flow_action_entry *act,struct mlx5e_tc_act *tc_act,struct mlx5e_tc_flow *flow,struct mlx5_flow_attr *attr, -struct mlx5e_tc_jump_state *jump_state, +struct mlx5e_tc_jump_state *jump_state,bool last,struct netlink_ext_ack *extack) {struct mlx5e_tc_act_branch_ctrl cond_true, cond_false;
@@ -3983,7 +3989,7 @@ parse_branch_ctrl(struct flow_action_entry *act, struct mlx5e_tc_act *tc_act, tc_act->get_branch_ctrl (act, &cond_true, &cond_false); err =alloc_branch_attr (flow, &cond_true, - &attr->branch_true, &jump_count, extack); + &attr->branch_true, &jump_count, last, extack);if (err) goto out_err;
@@ -3991,7 +3997,7 @@ parse_branch_ctrl(struct flow_action_entry *act, struct mlx5e_tc_act *tc_act, jump_state->jumping_attr = attr->branch_true; err =alloc_branch_attr (flow, &cond_false, - &attr->branch_false, &jump_count, extack); + &attr->branch_false, &jump_count, last, extack);if (err) goto err_branch_false;
@@ -4055,7 +4061,9 @@ parse_tc_actions(struct mlx5e_tc_act_parse_state *parse_state,dec_jump_count (act, tc_act, attr, priv, &jump_state); - err =parse_branch_ctrl (act, tc_act, flow, attr, &jump_state, extack); + err =parse_branch_ctrl (act, tc_act, flow, attr, &jump_state, +flow_action_is_last_entry (flow_action, act), + extack);if (err) goto out_free_post_acts;
Signature · 1 line
2.53.1-Meta
Quoted 46 lines · Mohsin Bashir
From: Mohsin Bashir <[email protected]>
In switchdev mode, mlx5 does not offload a rule where police is the last action, although TC needs no other action for such a rule. The upcoming selftest series relies on these rules. With nothing after police, the police result alone decides where the packet goes, so each result must be offloaded as TC executes it. The driver does not do that today. An ok result is handled as pipe and runs the actions after police, a pipe result always returns to the post action table, and a jump is counted from police while TC counts it from the start of the action list. A rate police is also rejected by
verify_attr_actions(), as its forward to the post meter table is added only after the check.Make the offloaded rule behave as it does in TC. An ok result ends processing in TC. A pipe result does the same when police is the last action. Send both to the end of the TC chains as the driver already does for gact ok. For jump, offload only the case where police is the first action, no pedit or police is skipped, and no action follows itself. The driver counts a jump in flow action entries and merges only consecutive entries of the same action, so a pedit with both set and add keys counts more than once, and an action repeated in a row counts once. A skipped police takes over the jump. Reject the rest. Existing rules where police is followed by other actions also change. An ok result now accepts the packet instead of running those actions. Finally set the forward to the post meter table while parsing police. This is already done for police with mtu and lets a police-only rule pass
verify_attr_actions().Tested on a ConnectX-7 in switchdev mode with the commands below. The hardware drop counter showed that the rules are offloaded and limit the traffic to the configured rate, that the second police only sees the packets conforming to the first, and that the jump lands on its target.
tc filter add dev eth0 ingress pref 100 protocol ipv6 flower skip_sw \ ip_proto udp dst_port 5201 \ action police rate 100mbit burst 1m conform-exceed drop/ok tc filter add dev eth0 ingress pref 100 protocol ipv6 flower skip_sw \ ip_proto udp dst_port 5201 \ action police rate 200mbit burst 1m conform-exceed drop/pipe \ action police rate 100mbit burst 1m conform-exceed drop/pipe tc filter add dev eth0 ingress pref 100 protocol ipv6 flower skip_sw \ ip_proto udp dst_port 5201 \ action police rate 100mbit burst 1m conform-exceed drop / jump 2 \ action drop action ok
Quoted 2 lines
Cc Carolina Jubran <[email protected]>Signed-off-by Mohsin Bashir <[email protected]>
I'm okay with this version.
Quoted 177 lines
---
Changelog:
v4:
- Reject only a skipped pedit or police, instead of allowing only skipped actions that are all drop or all ok, as suggested by Mark:
https://lore.kernel.org/netdev/[email protected]
v3: https://lore.kernel.org/netdev/[email protected]
v2: https://lore.kernel.org/netdev/[email protected]
v1: https://lore.kernel.org/netdev/[email protected]--- .../mellanox/mlx5/core/en/tc/act/police.c | 59 ++++++++++++++++++- .../net/ethernet/mellanox/mlx5/core/en_tc.c | 26 +++++--- 2 files changed, 75 insertions(+), 10 deletions(-)drivers/net/ethernet/mellanox/mlx5/core/en/tc/act/police.c +58
-1@@ -37,6 +37,52 @@ static int police_act_validate(const struct flow_action_entry *act,return 0 ; } +static bool +police_act_can_offload_jump (struct mlx5e_tc_act_parse_state *parse_state, +const struct flow_action_entry *act, +int act_index,u32 target) +{ +const struct flow_action *flow_action = parse_state->flow_action; +struct netlink_ext_ack *extack = parse_state->extack; +const struct flow_action_entry *prev, *entry; +u32 i; + +if (act_index) { +NL_SET_ERR_MSG_MOD (extack, +"Jump is only supported when police is the first action" ); +return false; + } + +if (!target || target >= flow_action->num_entries) { +NL_SET_ERR_MSG_MOD (extack,"Jump target must be a later action" ); +return false; + } + + prev = act; +for (i =1 ; i <= target; i++) { + entry = &flow_action->entries[i]; + +if (i < target && + (entry->id ==FLOW_ACTION_MANGLE || + entry->id ==FLOW_ACTION_ADD || + entry->id ==FLOW_ACTION_POLICE )) { +NL_SET_ERR_MSG_MOD (extack, +"Jump over a pedit or police action is not supported" ); +return false; + } + +if (entry->id == prev->id && entry->hw_index == prev->hw_index) { +NL_SET_ERR_MSG_MOD (extack, +"Jump over the same action twice in a row is not supported" ); +return false; + } + + prev = entry; + } + +return true; +} + static bool tc_act_can_offload_police(struct mlx5e_tc_act_parse_state *parse_state, const struct flow_action_entry *act,@@ -49,6 +95,16 @@ tc_act_can_offload_police(struct mlx5e_tc_act_parse_state *parse_state,if (err)return false; +if (act->police.exceed.act_id ==FLOW_ACTION_JUMP && + !police_act_can_offload_jump (parse_state, act, act_index, + act->police.exceed.extval)) +return false; + +if (act->police.notexceed.act_id ==FLOW_ACTION_JUMP && + !police_act_can_offload_jump (parse_state, act, act_index, + act->police.notexceed.extval)) +return false; +return !!mlx5e_get_flow_meters (parse_state->flow->priv->mdev); }@@ -94,13 +150,14 @@ tc_act_parse_police(struct mlx5e_tc_act_parse_state *parse_state, MLX5_FLOW_STEERING_CAP_MATCH_RANGES))return -EOPNOTSUPP ; - attr->action |=MLX5_FLOW_CONTEXT_ACTION_FWD_DEST ; attr->flags |=MLX5_ATTR_FLAG_MTU ; }else { attr->action |=MLX5_FLOW_CONTEXT_ACTION_EXECUTE_ASO ; attr->exe_aso_type =MLX5_EXE_ASO_FLOW_METER ; } + attr->action |=MLX5_FLOW_CONTEXT_ACTION_FWD_DEST ; +return 0 ; }drivers/net/ethernet/mellanox/mlx5/core/en_tc.c +17
-9@@ -3877,7 +3877,7 @@ static int alloc_branch_attr(struct mlx5e_tc_flow *flow, struct mlx5e_tc_act_branch_ctrl *cond, struct mlx5_flow_attr **cond_attr, - u32 *jump_count, + u32 *jump_count, bool last, struct netlink_ext_ack *extack) { struct mlx5_flow_attr *attr;@@ -3894,11 +3894,17 @@ alloc_branch_attr(struct mlx5e_tc_flow *flow,case FLOW_ACTION_DROP : attr->action |=MLX5_FLOW_CONTEXT_ACTION_DROP ;break ; -case FLOW_ACTION_ACCEPT :case FLOW_ACTION_PIPE : - err =set_branch_dest_ft (flow->priv, attr); -if (err) - goto out_err; +if (!last) { + err =set_branch_dest_ft (flow->priv, attr); +if (err) + goto out_err; +break ; + } + fallthrough; +case FLOW_ACTION_ACCEPT : + attr->action |=MLX5_FLOW_CONTEXT_ACTION_FWD_DEST ; + attr->flags |=MLX5_ATTR_FLAG_ACCEPT ;break ;case FLOW_ACTION_JUMP :if (*jump_count) {@@ -3970,7 +3976,7 @@ dec_jump_count(struct flow_action_entry *act, struct mlx5e_tc_act *tc_act,static int parse_branch_ctrl (struct flow_action_entry *act,struct mlx5e_tc_act *tc_act,struct mlx5e_tc_flow *flow,struct mlx5_flow_attr *attr, -struct mlx5e_tc_jump_state *jump_state, +struct mlx5e_tc_jump_state *jump_state,bool last,struct netlink_ext_ack *extack) {struct mlx5e_tc_act_branch_ctrl cond_true, cond_false;@@ -3983,7 +3989,7 @@ parse_branch_ctrl(struct flow_action_entry *act, struct mlx5e_tc_act *tc_act, tc_act->get_branch_ctrl (act, &cond_true, &cond_false); err =alloc_branch_attr (flow, &cond_true, - &attr->branch_true, &jump_count, extack); + &attr->branch_true, &jump_count, last, extack);if (err) goto out_err;@@ -3991,7 +3997,7 @@ parse_branch_ctrl(struct flow_action_entry *act, struct mlx5e_tc_act *tc_act, jump_state->jumping_attr = attr->branch_true; err =alloc_branch_attr (flow, &cond_false, - &attr->branch_false, &jump_count, extack); + &attr->branch_false, &jump_count, last, extack);if (err) goto err_branch_false;@@ -4055,7 +4061,9 @@ parse_tc_actions(struct mlx5e_tc_act_parse_state *parse_state,dec_jump_count (act, tc_act, attr, priv, &jump_state); - err =parse_branch_ctrl (act, tc_act, flow, attr, &jump_state, extack); + err =parse_branch_ctrl (act, tc_act, flow, attr, &jump_state, +flow_action_is_last_entry (flow_action, act), + extack);if (err) goto out_free_post_acts;
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] net/mlx5e: miscompiled skipped actions in police_act_can_offload_jump() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] net/mlx5e: miscompiled skipped actions in police_act_can_offload_jump()
--
commit ca98fcfa7fb19e6a3e27e9587d032f9f75c3bf2b
Author: Mohsin Bashir <[email protected]>
net: mlx5: Allow police as the last action of a TC rule
This commit updates mlx5 TC police action offloading to allow police as the last action in a flow, handle ok and terminal pipe branch controls as accept, and validate police jump targets.
Quoted 31 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc/act/police.c +23
-0@@ -37,6 +37,52 @@ static int police_act_validate(const struct flow_action_entry *act,return 0 ; } +static bool +police_act_can_offload_jump (struct mlx5e_tc_act_parse_state *parse_state, +const struct flow_action_entry *act, +int act_index,u32 target) +{ +const struct flow_action *flow_action = parse_state->flow_action; +struct netlink_ext_ack *extack = parse_state->extack; +const struct flow_action_entry *prev, *entry; +u32 i; + +if (act_index) { +NL_SET_ERR_MSG_MOD (extack, +"Jump is only supported when police is the first action" ); +return false; + } + +if (!target || target >= flow_action->num_entries) { +NL_SET_ERR_MSG_MOD (extack,"Jump target must be a later action" ); +return false; + } + + prev = act; +for (i =1 ; i <= target; i++) {
drivers/net/ethernet/mellanox/mlx5/core/en/tc/act/police.c static int police_act_validate(const struct flow_action_entry *act, · line 72
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc/act/police.c +10
-0+ entry = &flow_action->entries[i]; + +if (i < target && + (entry->id ==FLOW_ACTION_MANGLE || + entry->id ==FLOW_ACTION_ADD || + entry->id ==FLOW_ACTION_POLICE )) { +NL_SET_ERR_MSG_MOD (extack, +"Jump over a pedit or police action is not supported" ); +return false; + }
[Severity: Medium]
Can police_act_can_offload_jump() allow skipped action sequences that dec_jump_count() and parse_tc_actions() miscompile into hardware rules?
dec_jump_count() only sets jump_state->jump_target and checks tc_act->is_terminating_action when --jump_state->jump_count == 1:
drivers/net/ethernet/mellanox/mlx5/core/en_tc.c:dec_jump_count() {
...
/* nothing to do for intermediate actions */
if (--jump_state->jump_count > 1)
return;
if (jump_state->jump_count == 1) { /* last action in the jump action list */
/* create a new attribute after this action */
jump_state->jump_target = true;
if (tc_act->is_terminating_action) { /* the branch ends here */
attr->flags |= MLX5_ATTR_FLAG_TERMINATING;
attr->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
} else { /* the branch continues executing the rest of the actions */
struct mlx5e_post_act *post_act;
attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
post_act = get_post_action(priv);
attr->dest_ft = mlx5e_tc_post_act_get_ft(post_act);
}
...
}When target >= 3, parse_tc_actions() merges all non-multi-table skipped actions in 1 <= i < target into a single mlx5_flow_attr. If an earlier skipped action is terminating (for example, conform-exceed pipe / jump 3 followed by redirect eth1 and ok), TC software stops at entries[1], whereas merging entries[2] into the same attr sets MLX5_ATTR_FLAG_ACCEPT or combines incompatible actions (such as drop and ok) that fail in verify_attr_actions().
Even when target == 2, several allowed skipped actions do not work with the jump split in parse_tc_actions():
- FLOW_ACTION_TRAP has is_terminating_action == false on mlx5e_tc_act_trap, so dec_jump_count() overwrites attr->dest_ft (slow_fdb) with post_act->ft and chains the trap rule into the jump target instead of trapping to the CPU.
- Actions that only record state in parse_state (such as FLOW_ACTION_TUNNEL_ENCAP, FLOW_ACTION_TUNNEL_DECAP, FLOW_ACTION_MPLS_PUSH, FLOW_ACTION_PTYPE, FLOW_ACTION_VLAN_PUSH_ETH and FLOW_ACTION_VLAN_POP_ETH) leave their parse_state flags set across the jump_target split because parse_tc_actions() only resets parse_state->if_count = 0, applying the skipped action to the jump target attribute.
- Any skipped action with is_terminating_action == false does not get MLX5_FLOW_CONTEXT_ACTION_COUNT in dec_jump_count(), so mlx5e_tc_act_stats_add_flow() attaches the jump target's counter to the skipped action's cookie and counts packets that jumped over it.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 96 lines · On 12/2/26 1:25 PM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] net/mlx5e: miscompiled skipped actions inpolice_act_can_offload_jump()
--commit
ca98fcfa7fb19e6a3e27e9587d032f9f75c3bf2b
Author: Mohsin Bashir <[email protected]>net: mlx5: Allow police as the last action of a TC rule
This commit updates mlx5 TC police action offloading to allow police as the last action in a flow, handle ok and terminal pipe branch controls as accept, and validate police jump targets.
Quoted 31 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc/act/police.c +23
-0@@ -37,6 +37,52 @@ static int police_act_validate(const struct flow_action_entry *act,return 0 ; } +static bool +police_act_can_offload_jump (struct mlx5e_tc_act_parse_state *parse_state, +const struct flow_action_entry *act, +int act_index,u32 target) +{ +const struct flow_action *flow_action = parse_state->flow_action; +struct netlink_ext_ack *extack = parse_state->extack; +const struct flow_action_entry *prev, *entry; +u32 i; + +if (act_index) { +NL_SET_ERR_MSG_MOD (extack, +"Jump is only supported when police is the first action" ); +return false; + } + +if (!target || target >= flow_action->num_entries) { +NL_SET_ERR_MSG_MOD (extack,"Jump target must be a later action" ); +return false; + } + + prev = act; +for (i =1 ; i <= target; i++) {drivers/net/ethernet/mellanox/mlx5/core/en/tc/act/police.c static int police_act_validate(const struct flow_action_entry *act, · line 72
Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/en/tc/act/police.c +10
-0+ entry = &flow_action->entries[i]; + +if (i < target && + (entry->id ==FLOW_ACTION_MANGLE || + entry->id ==FLOW_ACTION_ADD || + entry->id ==FLOW_ACTION_POLICE )) { +NL_SET_ERR_MSG_MOD (extack, +"Jump over a pedit or police action is not supported" ); +return false; + }[Severity: Medium]
Canpolice_act_can_offload_jump()allow skipped action sequences thatdec_jump_count()andparse_tc_actions()miscompile into hardware rules?
dec_jump_count()only setsjump_state->jump_targetand checkstc_act->is_terminating_actionwhen --jump_state->jump_count == 1:drivers/net/ethernet/mellanox/mlx5/core/en_tc.c:dec_jump_count() { ... /* nothing to do for intermediate actions */ if (--jump_state->jump_count > 1) return; if (jump_state->jump_count == 1) { /* last action in the jump action list */ /* create a new attribute after this action */ jump_state->jump_target = true; if (tc_act->is_terminating_action) { /* the branch ends here */ attr->flags |= MLX5_ATTR_FLAG_TERMINATING; attr->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT; } else { /* the branch continues executing the rest of the actions */ struct mlx5e_post_act *post_act; attr->action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST; post_act = get_post_action(priv); attr->dest_ft = mlx5e_tc_post_act_get_ft(post_act); } ... }When target >= 3,
parse_tc_actions()merges all non-multi-table skipped actions in 1 <= i < target into a singlemlx5_flow_attr. If an earlier skipped action is terminating (for example, conform-exceed pipe / jump 3 followed by redirect eth1 and ok), TC software stops at entries[1], whereas merging entries[2] into the same attr setsMLX5_ATTR_FLAG_ACCEPTor combines incompatible actions (such as drop and ok) that fail inverify_attr_actions().
Quoted 17 lines
Even when target == 2, several allowed skipped actions do not work with the jump split in
parse_tc_actions():
-FLOW_ACTION_TRAPhasis_terminating_action== false onmlx5e_tc_act_trap, sodec_jump_count()overwritesattr->dest_ft(slow_fdb) withpost_act->ftand chains the trap rule into the jump target instead of trapping to the CPU.
- Actions that only record state inparse_state(such asFLOW_ACTION_TUNNEL_ENCAP,FLOW_ACTION_TUNNEL_DECAP,FLOW_ACTION_MPLS_PUSH,FLOW_ACTION_PTYPE,FLOW_ACTION_VLAN_PUSH_ETHandFLOW_ACTION_VLAN_POP_ETH) leave theirparse_stateflags set across thejump_targetsplit becauseparse_tc_actions()only resetsparse_state->if_count= 0, applying the skipped action to the jump target attribute.
- Any skipped action withis_terminating_action== false does not getMLX5_FLOW_CONTEXT_ACTION_COUNTindec_jump_count(), somlx5e_tc_act_stats_add_flow()attaches the jump target's counter to the skipped action's cookie and counts packets that jumped over it.
These rules are offloaded the same way today. This patch only rejects more of them. The skipped actions also only run when the other police result is pipe.
v3 limited the skipped actions to all drop or all ok, and I dropped that in v4 based on feedback on v3: https://lore.kernel.org/all/[email protected]/
Quoted 46 lines · Mohsin Bashir
From: Mohsin Bashir <[email protected]>
In switchdev mode, mlx5 does not offload a rule where police is the last action, although TC needs no other action for such a rule. The upcoming selftest series relies on these rules. With nothing after police, the police result alone decides where the packet goes, so each result must be offloaded as TC executes it. The driver does not do that today. An ok result is handled as pipe and runs the actions after police, a pipe result always returns to the post action table, and a jump is counted from police while TC counts it from the start of the action list. A rate police is also rejected by
verify_attr_actions(), as its forward to the post meter table is added only after the check.Make the offloaded rule behave as it does in TC. An ok result ends processing in TC. A pipe result does the same when police is the last action. Send both to the end of the TC chains as the driver already does for gact ok. For jump, offload only the case where police is the first action, no pedit or police is skipped, and no action follows itself. The driver counts a jump in flow action entries and merges only consecutive entries of the same action, so a pedit with both set and add keys counts more than once, and an action repeated in a row counts once. A skipped police takes over the jump. Reject the rest. Existing rules where police is followed by other actions also change. An ok result now accepts the packet instead of running those actions. Finally set the forward to the post meter table while parsing police. This is already done for police with mtu and lets a police-only rule pass
verify_attr_actions().Tested on a ConnectX-7 in switchdev mode with the commands below. The hardware drop counter showed that the rules are offloaded and limit the traffic to the configured rate, that the second police only sees the packets conforming to the first, and that the jump lands on its target.
tc filter add dev eth0 ingress pref 100 protocol ipv6 flower skip_sw \ ip_proto udp dst_port 5201 \ action police rate 100mbit burst 1m conform-exceed drop/ok tc filter add dev eth0 ingress pref 100 protocol ipv6 flower skip_sw \ ip_proto udp dst_port 5201 \ action police rate 200mbit burst 1m conform-exceed drop/pipe \ action police rate 100mbit burst 1m conform-exceed drop/pipe tc filter add dev eth0 ingress pref 100 protocol ipv6 flower skip_sw \ ip_proto udp dst_port 5201 \ action police rate 100mbit burst 1m conform-exceed drop / jump 2 \ action drop action ok
Quoted 3 lines
Cc Carolina Jubran <[email protected]>Signed-off-by Mohsin Bashir <[email protected]>---
Sent by mistake only to the rdma mailing list, sending again to all.
Quoted 176 lines
Changelog:
v4:
- Reject only a skipped pedit or police, instead of allowing only skipped actions that are all drop or all ok, as suggested by Mark:
https://lore.kernel.org/netdev/[email protected]
v3: https://lore.kernel.org/netdev/[email protected]
v2: https://lore.kernel.org/netdev/[email protected]
v1: https://lore.kernel.org/netdev/[email protected]--- .../mellanox/mlx5/core/en/tc/act/police.c | 59 ++++++++++++++++++- .../net/ethernet/mellanox/mlx5/core/en_tc.c | 26 +++++--- 2 files changed, 75 insertions(+), 10 deletions(-)drivers/net/ethernet/mellanox/mlx5/core/en/tc/act/police.c +58
-1@@ -37,6 +37,52 @@ static int police_act_validate(const struct flow_action_entry *act,return 0 ; } +static bool +police_act_can_offload_jump (struct mlx5e_tc_act_parse_state *parse_state, +const struct flow_action_entry *act, +int act_index,u32 target) +{ +const struct flow_action *flow_action = parse_state->flow_action; +struct netlink_ext_ack *extack = parse_state->extack; +const struct flow_action_entry *prev, *entry; +u32 i; + +if (act_index) { +NL_SET_ERR_MSG_MOD (extack, +"Jump is only supported when police is the first action" ); +return false; + } + +if (!target || target >= flow_action->num_entries) { +NL_SET_ERR_MSG_MOD (extack,"Jump target must be a later action" ); +return false; + } + + prev = act; +for (i =1 ; i <= target; i++) { + entry = &flow_action->entries[i]; + +if (i < target && + (entry->id ==FLOW_ACTION_MANGLE || + entry->id ==FLOW_ACTION_ADD || + entry->id ==FLOW_ACTION_POLICE )) { +NL_SET_ERR_MSG_MOD (extack, +"Jump over a pedit or police action is not supported" ); +return false; + } + +if (entry->id == prev->id && entry->hw_index == prev->hw_index) { +NL_SET_ERR_MSG_MOD (extack, +"Jump over the same action twice in a row is not supported" ); +return false; + } + + prev = entry; + } + +return true; +} + static bool tc_act_can_offload_police(struct mlx5e_tc_act_parse_state *parse_state, const struct flow_action_entry *act,@@ -49,6 +95,16 @@ tc_act_can_offload_police(struct mlx5e_tc_act_parse_state *parse_state,if (err)return false; +if (act->police.exceed.act_id ==FLOW_ACTION_JUMP && + !police_act_can_offload_jump (parse_state, act, act_index, + act->police.exceed.extval)) +return false; + +if (act->police.notexceed.act_id ==FLOW_ACTION_JUMP && + !police_act_can_offload_jump (parse_state, act, act_index, + act->police.notexceed.extval)) +return false; +return !!mlx5e_get_flow_meters (parse_state->flow->priv->mdev); }@@ -94,13 +150,14 @@ tc_act_parse_police(struct mlx5e_tc_act_parse_state *parse_state, MLX5_FLOW_STEERING_CAP_MATCH_RANGES))return -EOPNOTSUPP ; - attr->action |=MLX5_FLOW_CONTEXT_ACTION_FWD_DEST ; attr->flags |=MLX5_ATTR_FLAG_MTU ; }else { attr->action |=MLX5_FLOW_CONTEXT_ACTION_EXECUTE_ASO ; attr->exe_aso_type =MLX5_EXE_ASO_FLOW_METER ; } + attr->action |=MLX5_FLOW_CONTEXT_ACTION_FWD_DEST ; +return 0 ; }drivers/net/ethernet/mellanox/mlx5/core/en_tc.c +17
-9@@ -3877,7 +3877,7 @@ static int alloc_branch_attr(struct mlx5e_tc_flow *flow, struct mlx5e_tc_act_branch_ctrl *cond, struct mlx5_flow_attr **cond_attr, - u32 *jump_count, + u32 *jump_count, bool last, struct netlink_ext_ack *extack) { struct mlx5_flow_attr *attr;@@ -3894,11 +3894,17 @@ alloc_branch_attr(struct mlx5e_tc_flow *flow,case FLOW_ACTION_DROP : attr->action |=MLX5_FLOW_CONTEXT_ACTION_DROP ;break ; -case FLOW_ACTION_ACCEPT :case FLOW_ACTION_PIPE : - err =set_branch_dest_ft (flow->priv, attr); -if (err) - goto out_err; +if (!last) { + err =set_branch_dest_ft (flow->priv, attr); +if (err) + goto out_err; +break ; + } + fallthrough; +case FLOW_ACTION_ACCEPT : + attr->action |=MLX5_FLOW_CONTEXT_ACTION_FWD_DEST ; + attr->flags |=MLX5_ATTR_FLAG_ACCEPT ;break ;case FLOW_ACTION_JUMP :if (*jump_count) {@@ -3970,7 +3976,7 @@ dec_jump_count(struct flow_action_entry *act, struct mlx5e_tc_act *tc_act,static int parse_branch_ctrl (struct flow_action_entry *act,struct mlx5e_tc_act *tc_act,struct mlx5e_tc_flow *flow,struct mlx5_flow_attr *attr, -struct mlx5e_tc_jump_state *jump_state, +struct mlx5e_tc_jump_state *jump_state,bool last,struct netlink_ext_ack *extack) {struct mlx5e_tc_act_branch_ctrl cond_true, cond_false;@@ -3983,7 +3989,7 @@ parse_branch_ctrl(struct flow_action_entry *act, struct mlx5e_tc_act *tc_act, tc_act->get_branch_ctrl (act, &cond_true, &cond_false); err =alloc_branch_attr (flow, &cond_true, - &attr->branch_true, &jump_count, extack); + &attr->branch_true, &jump_count, last, extack);if (err) goto out_err;@@ -3991,7 +3997,7 @@ parse_branch_ctrl(struct flow_action_entry *act, struct mlx5e_tc_act *tc_act, jump_state->jumping_attr = attr->branch_true; err =alloc_branch_attr (flow, &cond_false, - &attr->branch_false, &jump_count, extack); + &attr->branch_false, &jump_count, last, extack);if (err) goto err_branch_false;@@ -4055,7 +4061,9 @@ parse_tc_actions(struct mlx5e_tc_act_parse_state *parse_state,dec_jump_count (act, tc_act, attr, priv, &jump_state); - err =parse_branch_ctrl (act, tc_act, flow, attr, &jump_state, extack); + err =parse_branch_ctrl (act, tc_act, flow, attr, &jump_state, +flow_action_is_last_entry (flow_action, act), + extack);if (err) goto out_free_post_acts;