Skip to contentSkip to conversation

net: mlx5: Allow police as the last action of a TC rule

PATCH New Mohsin Bashir 2 participants 2 messages Nov 19

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: mlx5: Allow police as the last action of a TC rule

From: Mohsin Bashir <[email protected]>

In switchdev mode, mlx5 offloads police only when other actions follow it. TC needs no extra action for a police-only rule to work. The mlx5 driver makes every police rule forward to the post meter table, but for a rate police it adds that forward only after verify_attr_actions() has checked the rule, so the check rejects it. The ok result of police is also handled as pipe. In TC, ok accepts the packet and ends the action list, whereas mlx5 goes on with the actions after police.

Support police as the only action of a rule. Set the forward while parsing police, as is already done for police with mtu, and forward an ok result to the end of the TC chains, as the driver does when it offloads the gact ok action. This also changes existing offloaded rules where police is followed by other actions. Before, an ok result ran the actions after police. Now it accepts the packet, as TC does. The upcoming selftest series also relies on such rules for TC offload validation.

When police is the last action, a pipe result also ends the action list, so forward it to the end of the TC chains too. Otherwise it is sent back to the post action table, and with two police actions the packet loops through the second police. A jump counts from the start of the action list, so its target may be an earlier action or police itself. Reject jump when police is the last action.

The patch was tested on a ConnectX-7 in switchdev mode with the commands below. It was verified, using the hardware drop counter, that the rules are offloaded and limit the matching traffic to the configured rate, that the second police of the two police rule only sees the packets conforming to the first, and that jump on the last police is rejected.

  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 1
Cc Carolina Jubran <[email protected]>

---
Changelog:
v2:
- Treat pipe on the last police as ok and forward it to the end of the TC chains.
- Reject jump when police is the last action, since the jump target is counted from the start of the action list.
- Update commit message

v1: https://lore.kernel.org/netdev/[email protected]

---
 .../mellanox/mlx5/core/en/tc/act/police.c     | 11 +++++++-
 .../net/ethernet/mellanox/mlx5/core/en_tc.c   | 26 ++++++++++++-------
 2 files changed, 27 insertions(+), 10 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en/tc/act/police.c +10 -1
index 1bd1c94fb977..faa0822b1136 100644
@@ -49,6 +49,14 @@ tc_act_can_offload_police(struct mlx5e_tc_act_parse_state *parse_state,
 	if (err)
 		return false;
 
+	if (flow_action_is_last_entry(parse_state->flow_action, act) &&
+	    (act->police.exceed.act_id == FLOW_ACTION_JUMP ||
+	     act->police.notexceed.act_id == FLOW_ACTION_JUMP)) {
+		NL_SET_ERR_MSG_MOD(parse_state->extack,
+				   "Jump on last police action is not supported");
+		return false;
+	}
+
 	return !!mlx5e_get_flow_meters(parse_state->flow->priv->mdev);
 }
 
@@ -94,13 +102,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
index 1d35c97aea0b..7e7111e6d058 100644
@@ -3874,7 +3874,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;
@@ -3891,11 +3891,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) {
@@ -3967,7 +3973,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;
@@ -3980,7 +3986,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;
 
@@ -3988,7 +3994,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;
 
@@ -4052,7 +4058,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
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 19 lines

From: Mohsin Bashir <[email protected]>

In switchdev mode, mlx5 offloads police only when other actions follow it. TC needs no extra action for a police-only rule to work. The mlx5 driver makes every police rule forward to the post meter table, but for a rate police it adds that forward only after verify_attr_actions() has checked the rule, so the check rejects it. The ok result of police is also handled as pipe. In TC, ok accepts the packet and ends the action list, whereas mlx5 goes on with the actions after police.

Support police as the only action of a rule. Set the forward while parsing police, as is already done for police with mtu, and forward an ok result to the end of the TC chains, as the driver does when it offloads the gact ok action. This also changes existing offloaded rules where police is followed by other actions. Before, an ok result ran the [ ... ]
ip_proto udp dst_port 5201 \
action police rate 100mbit burst 1m conform-exceed drop / jump 1

Quoted 2 lines
Cc Carolina Jubran <[email protected]>

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1