Use dsa_cpu_ports() and dsa_is_cpu_port() instead for a clearer view of code. Signed-off-by: David Yang --- drivers/net/dsa/motorcomm/chip.c | 18 +++++++++--------- drivers/net/dsa/motorcomm/chip.h | 2 -- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/drivers/net/dsa/motorcomm/chip.c b/drivers/net/dsa/motorcomm/chip.c index 82f364986aeb..c96512faeca2 100644 --- a/drivers/net/dsa/motorcomm/chip.c +++ b/drivers/net/dsa/motorcomm/chip.c @@ -1101,7 +1101,7 @@ yt921x_vlan_add(struct yt921x_priv *priv, int port, u16 vid, bool untagged) u32 ctrls[2]; masks[0] = YT921X_VLAN_CTRLa_PORTn(port) | - YT921X_VLAN_CTRLa_PORTS(priv->cpu_ports_mask); + YT921X_VLAN_CTRLa_PORTS(dsa_cpu_ports(&priv->ds)); ctrls[0] = masks[0]; masks[1] = YT921X_VLAN_CTRLb_UNTAG_PORTn(port); @@ -1256,11 +1256,12 @@ yt921x_dsa_port_vlan_add(struct dsa_switch *ds, int port, static int yt921x_userport_standalone(struct yt921x_priv *priv, int port) { + struct dsa_switch *ds = &priv->ds; u32 mask; u32 ctrl; int res; - ctrl = ~priv->cpu_ports_mask; + ctrl = ~dsa_cpu_ports(ds); res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port), ctrl); if (res) return res; @@ -1306,7 +1307,7 @@ static int yt921x_isolate(struct yt921x_priv *priv, int port) mask = BIT(port); for (int i = 0; i < YT921X_PORT_NUM; i++) { - if ((BIT(i) & priv->cpu_ports_mask) || i == port) + if (dsa_is_cpu_port(&priv->ds, i) || i == port) continue; res = yt921x_reg_set_bits(priv, YT921X_PORTn_ISOLATION(i), @@ -1323,7 +1324,7 @@ static int yt921x_isolate(struct yt921x_priv *priv, int port) */ static int yt921x_bridge(struct yt921x_priv *priv, u16 ports_mask) { - unsigned long targets_mask = ports_mask & ~priv->cpu_ports_mask; + unsigned long targets_mask = ports_mask & ~dsa_cpu_ports(&priv->ds); u32 isolated_mask; u32 ctrl; int port; @@ -1436,7 +1437,7 @@ yt921x_bridge_flags(struct yt921x_priv *priv, int port, u32 ports_mask; ports_mask = dsa_bridge_ports(ds, bdev); - ports_mask |= priv->cpu_ports_mask; + ports_mask |= dsa_cpu_ports(ds); res = yt921x_bridge(priv, ports_mask); if (res) return res; @@ -1511,7 +1512,7 @@ yt921x_dsa_port_bridge_join(struct dsa_switch *ds, int port, return 0; ports_mask = dsa_bridge_ports(ds, bridge.dev); - ports_mask |= priv->cpu_ports_mask; + ports_mask |= dsa_cpu_ports(ds); dp = dsa_to_port(ds, port); brport_dev = dsa_port_to_bridge_port(dp); @@ -2382,10 +2383,10 @@ static int yt921x_chip_setup_dsa(struct yt921x_priv *priv) int res; /* Enable DSA */ - priv->cpu_ports_mask = dsa_cpu_ports(ds); + cpu_ports_mask = dsa_cpu_ports(ds); ctrl = YT921X_EXT_CPU_PORT_TAG_EN | YT921X_EXT_CPU_PORT_PORT_EN | - YT921X_EXT_CPU_PORT_PORT(__ffs(priv->cpu_ports_mask)); + YT921X_EXT_CPU_PORT_PORT(__ffs(cpu_ports_mask)); res = yt921x_reg_write(priv, YT921X_EXT_CPU_PORT, ctrl); if (res) return res; @@ -2422,7 +2423,6 @@ static int yt921x_chip_setup_dsa(struct yt921x_priv *priv) /* Except for CPU ports, if any packets are sent via CPU ports without * tag, they should be dropped. */ - cpu_ports_mask = priv->cpu_ports_mask; for_each_set_bit(port, &cpu_ports_mask, YT921X_PORT_NUM) { ctrl &= ~YT921X_ACT_UNK_ACTn_M(port); ctrl |= YT921X_ACT_UNK_ACTn_DROP(port); diff --git a/drivers/net/dsa/motorcomm/chip.h b/drivers/net/dsa/motorcomm/chip.h index fc315f1d8f87..d5ed580f8a78 100644 --- a/drivers/net/dsa/motorcomm/chip.h +++ b/drivers/net/dsa/motorcomm/chip.h @@ -509,8 +509,6 @@ struct yt921x_priv { const struct yt921x_info *info; unsigned int meter_slot_ns; unsigned int port_shape_slot_ns; - /* cache of dsa_cpu_ports(ds) */ - u16 cpu_ports_mask; unsigned char cycle_ns; /* protect the access to the switch registers */ -- 2.53.0 Port isolation takes after REDIR judgment. An ACL rule redirected to itself on a standalone port was blocked before. Lift that block despite rare usage. Fixes: 8eb9e7520b17 ("net: dsa: yt921x: Add ACL support") Signed-off-by: David Yang --- drivers/net/dsa/motorcomm/chip.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/net/dsa/motorcomm/chip.c b/drivers/net/dsa/motorcomm/chip.c index c96512faeca2..6e57c9262c68 100644 --- a/drivers/net/dsa/motorcomm/chip.c +++ b/drivers/net/dsa/motorcomm/chip.c @@ -1261,7 +1261,7 @@ static int yt921x_userport_standalone(struct yt921x_priv *priv, int port) u32 ctrl; int res; - ctrl = ~dsa_cpu_ports(ds); + ctrl = ~(dsa_cpu_ports(ds) | BIT(port)); res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port), ctrl); if (res) return res; -- 2.53.0 For a following commit, an accurate snapshot of port isolation masks is required. However, .port_pre_bridge_flags() can only veto the changes, and .port_bridge_flags() is deferred, thus the driver never knows the exact time point when flags take effect. Introduce a helper to derive such mask on the fly, and remove local cache for port flags. Signed-off-by: David Yang --- drivers/net/dsa/motorcomm/chip.c | 158 ++++++++++++++++++++----------- drivers/net/dsa/motorcomm/chip.h | 4 - 2 files changed, 105 insertions(+), 57 deletions(-) diff --git a/drivers/net/dsa/motorcomm/chip.c b/drivers/net/dsa/motorcomm/chip.c index 6e57c9262c68..567ce8716acd 100644 --- a/drivers/net/dsa/motorcomm/chip.c +++ b/drivers/net/dsa/motorcomm/chip.c @@ -1322,39 +1322,112 @@ static int yt921x_isolate(struct yt921x_priv *priv, int port) /* Make sure to include the CPU port in ports_mask, or your bridge will * not have it. */ -static int yt921x_bridge(struct yt921x_priv *priv, u16 ports_mask) +static void +yt921x_isolation_calc_bridge(struct yt921x_priv *priv, u16 ports_mask, + u16 *isolation_masks) { - unsigned long targets_mask = ports_mask & ~dsa_cpu_ports(&priv->ds); + struct dsa_switch *ds = &priv->ds; + struct net_device *brport; + struct dsa_port *dp; u32 isolated_mask; + u32 hairpin_mask; u32 ctrl; - int port; - int res; isolated_mask = 0; - for_each_set_bit(port, &targets_mask, YT921X_PORT_NUM) { - struct yt921x_port *pp = &priv->ports[port]; + hairpin_mask = 0; + dsa_switch_for_each_user_port(dp, ds) { + int port = dp->index; + + if (!(ports_mask & BIT(port))) + continue; - if (pp->isolated) + brport = dsa_port_to_bridge_port(dp); + + if (br_port_flag_is_set(brport, BR_ISOLATED)) isolated_mask |= BIT(port); + if (br_port_flag_is_set(brport, BR_HAIRPIN_MODE)) + hairpin_mask |= BIT(port); } /* Block from non-cpu bridge ports ... */ - for_each_set_bit(port, &targets_mask, YT921X_PORT_NUM) { - struct yt921x_port *pp = &priv->ports[port]; + dsa_switch_for_each_user_port(dp, ds) { + int port = dp->index; + + if (!(ports_mask & BIT(port))) + continue; /* to non-bridge ports */ ctrl = ~ports_mask; /* to isolated ports when isolated */ - if (pp->isolated) + if (isolated_mask & BIT(port)) ctrl |= isolated_mask; /* to itself when non-hairpin */ - if (!pp->hairpin) + if (!(hairpin_mask & BIT(port))) ctrl |= BIT(port); else ctrl &= ~BIT(port); + isolation_masks[port] = ctrl; + } +} + +static void +yt921x_isolation_calc_ports(struct yt921x_priv *priv, u16 *ports_maskp, + u16 *isolation_masks) +{ + struct dsa_switch *ds = &priv->ds; + struct net_device *bdev; + struct dsa_port *dp; + u32 cpu_ports_mask; + u32 targets_mask; + + cpu_ports_mask = dsa_cpu_ports(ds); + targets_mask = *ports_maskp & ~cpu_ports_mask; + if (!targets_mask) + return; + + dsa_switch_for_each_user_port(dp, ds) { + u32 bridge_ports_mask; + int port = dp->index; + + if (!(targets_mask & BIT(port))) + continue; + + bdev = dsa_port_bridge_dev_get(dp); + if (!bdev) { + isolation_masks[port] = ~(BIT(port) | cpu_ports_mask); + targets_mask &= ~BIT(port); + } else { + bridge_ports_mask = dsa_bridge_ports(ds, bdev); + yt921x_isolation_calc_bridge(priv, + bridge_ports_mask | + cpu_ports_mask, + isolation_masks); + targets_mask &= ~bridge_ports_mask; + *ports_maskp |= bridge_ports_mask; + } + + if (!targets_mask) + break; + } +} + +static int +yt921x_isolation_commit(struct yt921x_priv *priv, u16 ports_mask, + const u16 *isolation_masks) +{ + struct dsa_switch *ds = &priv->ds; + struct dsa_port *dp; + int res; + + dsa_switch_for_each_user_port(dp, ds) { + int port = dp->index; + + if (!(ports_mask & BIT(port))) + continue; + res = yt921x_reg_write(priv, YT921X_PORTn_ISOLATION(port), - ctrl); + isolation_masks[port]); if (res) return res; } @@ -1362,6 +1435,19 @@ static int yt921x_bridge(struct yt921x_priv *priv, u16 ports_mask) return 0; } +static int +yt921x_isolation_refresh(struct yt921x_priv *priv, u16 ports_mask, + bool also_bridge) +{ + u16 isolation_masks[YT921X_PORT_NUM]; + u16 targets_mask = ports_mask; + + yt921x_isolation_calc_ports(priv, &targets_mask, isolation_masks); + return yt921x_isolation_commit(priv, + also_bridge ? targets_mask : ports_mask, + isolation_masks); +} + static int yt921x_bridge_leave(struct yt921x_priv *priv, int port) { int res; @@ -1378,7 +1464,7 @@ static int yt921x_bridge_leave(struct yt921x_priv *priv, int port) } static int -yt921x_bridge_join(struct yt921x_priv *priv, int port, u16 ports_mask) +yt921x_bridge_join(struct yt921x_priv *priv, int port) { int res; @@ -1386,7 +1472,7 @@ yt921x_bridge_join(struct yt921x_priv *priv, int port, u16 ports_mask) if (res) return res; - res = yt921x_bridge(priv, ports_mask); + res = yt921x_isolation_refresh(priv, BIT(port), true); if (res) return res; @@ -1397,8 +1483,6 @@ static int yt921x_bridge_flags(struct yt921x_priv *priv, int port, struct switchdev_brport_flags flags) { - struct yt921x_port *pp = &priv->ports[port]; - bool do_flush; u32 mask; int res; @@ -1418,30 +1502,10 @@ yt921x_bridge_flags(struct yt921x_priv *priv, int port, /* BR_BCAST_FLOOD: we can filter bcast, but cannot trap them */ - do_flush = false; - if (flags.mask & BR_HAIRPIN_MODE) { - pp->hairpin = flags.val & BR_HAIRPIN_MODE; - do_flush = true; - } - if (flags.mask & BR_ISOLATED) { - pp->isolated = flags.val & BR_ISOLATED; - do_flush = true; - } - if (do_flush) { - struct dsa_switch *ds = &priv->ds; - struct dsa_port *dp = dsa_to_port(ds, port); - struct net_device *bdev; - - bdev = dsa_port_bridge_dev_get(dp); - if (bdev) { - u32 ports_mask; - - ports_mask = dsa_bridge_ports(ds, bdev); - ports_mask |= dsa_cpu_ports(ds); - res = yt921x_bridge(priv, ports_mask); - if (res) - return res; - } + if ((flags.mask & BR_HAIRPIN_MODE) || (flags.mask & BR_ISOLATED)) { + res = yt921x_isolation_refresh(priv, BIT(port), true); + if (res) + return res; } return 0; @@ -1502,25 +1566,13 @@ yt921x_dsa_port_bridge_join(struct dsa_switch *ds, int port, struct netlink_ext_ack *extack) { struct yt921x_priv *priv = dsa_to_yt921x_priv(ds); - struct yt921x_port *pp = &priv->ports[port]; - struct net_device *brport_dev; - struct dsa_port *dp; - u16 ports_mask; int res; if (dsa_is_cpu_port(ds, port)) return 0; - ports_mask = dsa_bridge_ports(ds, bridge.dev); - ports_mask |= dsa_cpu_ports(ds); - - dp = dsa_to_port(ds, port); - brport_dev = dsa_port_to_bridge_port(dp); - mutex_lock(&priv->reg_lock); - pp->isolated = br_port_flag_is_set(brport_dev, BR_ISOLATED); - pp->hairpin = br_port_flag_is_set(brport_dev, BR_HAIRPIN_MODE); - res = yt921x_bridge_join(priv, port, ports_mask); + res = yt921x_bridge_join(priv, port); mutex_unlock(&priv->reg_lock); return res; diff --git a/drivers/net/dsa/motorcomm/chip.h b/drivers/net/dsa/motorcomm/chip.h index d5ed580f8a78..22381009db51 100644 --- a/drivers/net/dsa/motorcomm/chip.h +++ b/drivers/net/dsa/motorcomm/chip.h @@ -457,10 +457,6 @@ struct yt921x_port { bool serdes:1; /* Link from in-band status (PHYLINK_PCS_NEG_INBAND) */ bool inband:1; - /* BR_HAIRPIN_MODE */ - bool hairpin:1; - /* BR_ISOLATED */ - bool isolated:1; unsigned short acl_cnt; -- 2.53.0 Port isolation takes after REDIR judgment, but software delivers such frames. Either route such rules to the software, or reject offloading. Fixes: 8eb9e7520b17 ("net: dsa: yt921x: Add ACL support") Signed-off-by: David Yang --- drivers/net/dsa/motorcomm/chip.c | 9 +++++++++ drivers/net/dsa/motorcomm/chip.h | 2 ++ drivers/net/dsa/motorcomm/tc.c | 33 ++++++++++++++++++++++++++++++-- drivers/net/dsa/motorcomm/tc.h | 6 +++++- 4 files changed, 47 insertions(+), 3 deletions(-) diff --git a/drivers/net/dsa/motorcomm/chip.c b/drivers/net/dsa/motorcomm/chip.c index 567ce8716acd..a71a8dd5dc63 100644 --- a/drivers/net/dsa/motorcomm/chip.c +++ b/drivers/net/dsa/motorcomm/chip.c @@ -1412,6 +1412,15 @@ yt921x_isolation_calc_ports(struct yt921x_priv *priv, u16 *ports_maskp, } } +u16 yt921x_isolation_calc_port(struct yt921x_priv *priv, int port) +{ + u16 isolation_masks[YT921X_PORT_NUM]; + u16 ports_mask = BIT(port); + + yt921x_isolation_calc_ports(priv, &ports_mask, isolation_masks); + return isolation_masks[port]; +} + static int yt921x_isolation_commit(struct yt921x_priv *priv, u16 ports_mask, const u16 *isolation_masks) diff --git a/drivers/net/dsa/motorcomm/chip.h b/drivers/net/dsa/motorcomm/chip.h index 22381009db51..749fe12fa852 100644 --- a/drivers/net/dsa/motorcomm/chip.h +++ b/drivers/net/dsa/motorcomm/chip.h @@ -559,4 +559,6 @@ static inline u32 ethaddr_lo2_to_u32(const unsigned char *addr) return (addr[4] << 8) | addr[5]; } +u16 yt921x_isolation_calc_port(struct yt921x_priv *priv, int port); + #endif diff --git a/drivers/net/dsa/motorcomm/tc.c b/drivers/net/dsa/motorcomm/tc.c index e3f057ed580f..e65baccda99e 100644 --- a/drivers/net/dsa/motorcomm/tc.c +++ b/drivers/net/dsa/motorcomm/tc.c @@ -1041,6 +1041,8 @@ yt921x_acl_rule_ext_parse_flow_action(struct yt921x_acl_rule_ext *ruleext, int res; memset(action, 0, 3 * sizeof(*action)); + ruleext->r.sw_assisting = false; + flow_action_for_each(i, act, flow_action) { /* The flow action entry carries no mirred control, so there * is no way to tell whether software would continue past a @@ -1072,6 +1074,21 @@ yt921x_acl_rule_ext_parse_flow_action(struct yt921x_acl_rule_ext *ruleext, goto fallback; } + /* Port isolation takes after REDIR judgment, but + * software delivers such frames. + */ + if (yt921x_isolation_calc_port(priv, port) & + BIT(to_dp->index)) { + if (cls->common.skip_sw) { + if (to_dp->index == port) + reason = "Redirect to self, which is not hairpinning"; + else + reason = "Redirect target isolated, or not in the same bridge"; + goto fallback; + } + ruleext->r.sw_assisting = true; + } + action[2] |= YT921X_ACL_ACTc_FWD_EN | YT921X_ACL_ACTc_FWD_REDIR | YT921X_ACL_ACTc_FWD_REDIR_DPORTn(to_dp->index); @@ -1301,8 +1318,19 @@ yt921x_acl_commit(struct yt921x_priv *priv, unsigned int entid, u8 entsmask) aclrule = aclblk->rules[binid]; /* Write actions */ - res = yt921x_reg96_write(priv, YT921X_ACLn_ACT(entid), - aclrule ? aclrule->action : zeros); + if (!aclrule || !aclrule->sw_assisting) { + res = yt921x_reg96_write(priv, YT921X_ACLn_ACT(entid), + aclrule ? aclrule->action : zeros); + } else { + u32 action[3]; + + memcpy(action, aclrule->action, sizeof(action)); + action[2] &= ~YT921X_ACL_ACTc_FWD_REDIR_DPORTS_M & + ~YT921X_ACL_ACTc_FWD_M; + action[2] |= YT921X_ACL_ACTc_FWD_EN | YT921X_ACL_ACTc_FWD_TRAP; + + res = yt921x_reg96_write(priv, YT921X_ACLn_ACT(entid), action); + } if (res) return res; @@ -1502,6 +1530,7 @@ yt921x_acl_add(struct yt921x_priv *priv, ~YT921X_ACL_ACTc_FWD_M; aclrule->action[2] |= YT921X_ACL_ACTc_FWD_EN | YT921X_ACL_ACTc_FWD_TRAP; + aclrule->sw_assisting = false; } if (meterid < YT921X_METER_NUM) aclrule->action[0] |= YT921X_ACL_ACTa_METER_ID(meterid); diff --git a/drivers/net/dsa/motorcomm/tc.h b/drivers/net/dsa/motorcomm/tc.h index cc53f148d690..81c29a8eec5a 100644 --- a/drivers/net/dsa/motorcomm/tc.h +++ b/drivers/net/dsa/motorcomm/tc.h @@ -349,7 +349,11 @@ struct yt921x_acl_rule { enum tc_setup_type type; u32 action[3]; - bool sw_assisted; + bool sw_assisted:1; + /* not a TRAP rule, but installed as trapped as some conditions are not + * met + */ + bool sw_assisting:1; u8 mask; struct yt921x_acl_entry entries[YT921X_ACL_ENT_PER_BLK]; -- 2.53.0 Bridge flag changes rewrite the isolation masks, so a reachable redirect rule may be blocked by port isolation. Reject such changes based on bookkeeping counters: .port_pre_bridge_flags() is atomic. Fixes: 8eb9e7520b17 ("net: dsa: yt921x: Add ACL support") Signed-off-by: David Yang --- drivers/net/dsa/motorcomm/chip.c | 43 ++++++++++++++++++++++++++++++++ drivers/net/dsa/motorcomm/chip.h | 1 + drivers/net/dsa/motorcomm/tc.c | 36 ++++++++++++++++++++------ 3 files changed, 72 insertions(+), 8 deletions(-) diff --git a/drivers/net/dsa/motorcomm/chip.c b/drivers/net/dsa/motorcomm/chip.c index a71a8dd5dc63..323c1705f613 100644 --- a/drivers/net/dsa/motorcomm/chip.c +++ b/drivers/net/dsa/motorcomm/chip.c @@ -1525,9 +1525,52 @@ yt921x_dsa_port_pre_bridge_flags(struct dsa_switch *ds, int port, struct switchdev_brport_flags flags, struct netlink_ext_ack *extack) { + struct yt921x_priv *priv = dsa_to_yt921x_priv(ds); + struct yt921x_port *pp = &priv->ports[port]; + struct net_device *bdev; + struct dsa_port *dp; + u32 ports_mask; + if (flags.mask & ~(BR_HAIRPIN_MODE | BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_ISOLATED)) return -EINVAL; + + if ((flags.mask & BR_HAIRPIN_MODE) && !(flags.val & BR_HAIRPIN_MODE) && + pp->acl_dst_cnts[port]) { + NL_SET_ERR_MSG_MOD(extack, + "Cannot disable hairpin mode while there are hairpin redirect rules"); + return -EINVAL; + } + + dp = dsa_to_port(ds, port); + bdev = dsa_port_bridge_dev_get(dp); + if (!bdev) + return 0; + ports_mask = dsa_bridge_ports(ds, bdev); + + if ((flags.mask & BR_ISOLATED) && (flags.val & BR_ISOLATED)) { + struct dsa_port *other_dp; + + dsa_switch_for_each_user_port(other_dp, ds) { + struct net_device *other_brport; + int i = other_dp->index; + + if (i == port || !(ports_mask & BIT(i))) + continue; + if (!pp->acl_dst_cnts[i] && + !priv->ports[i].acl_dst_cnts[port]) + continue; + + other_brport = dsa_port_to_bridge_port(other_dp); + if (!br_port_flag_is_set(other_brport, BR_ISOLATED)) + continue; + + NL_SET_ERR_MSG_MOD(extack, + "Cannot isolate port while there are redirect rules"); + return -EINVAL; + } + } + return 0; } diff --git a/drivers/net/dsa/motorcomm/chip.h b/drivers/net/dsa/motorcomm/chip.h index 749fe12fa852..4085a3c52f20 100644 --- a/drivers/net/dsa/motorcomm/chip.h +++ b/drivers/net/dsa/motorcomm/chip.h @@ -459,6 +459,7 @@ struct yt921x_port { bool inband:1; unsigned short acl_cnt; + unsigned short acl_dst_cnts[YT921X_PORT_NUM]; #if IS_ENABLED(CONFIG_NET_DSA_YT921X_LEDS) unsigned char led_duty; diff --git a/drivers/net/dsa/motorcomm/tc.c b/drivers/net/dsa/motorcomm/tc.c index e65baccda99e..7691da95d76f 100644 --- a/drivers/net/dsa/motorcomm/tc.c +++ b/drivers/net/dsa/motorcomm/tc.c @@ -1403,7 +1403,8 @@ yt921x_acl_del(struct yt921x_priv *priv, enum tc_setup_type type, unsigned int binid; unsigned int blkid; unsigned int entid; - unsigned long m; + unsigned long srcs; + unsigned long dsts; unsigned int o; u32 ctrl; int res; @@ -1422,9 +1423,17 @@ yt921x_acl_del(struct yt921x_priv *priv, enum tc_setup_type type, ret = yt921x_acl_commit(priv, entid, aclrule->mask); /* the kernel never rolls back on failure */ - m = FIELD_GET(YT921X_ACL_KEYb_SPORTS_M, aclrule->entries[0].key[1]); - for_each_set_bit(o, &m, YT921X_PORT_NUM) { + srcs = FIELD_GET(YT921X_ACL_KEYb_SPORTS_M, aclrule->entries[0].key[1]); + dsts = FIELD_GET(YT921X_ACL_ACTc_FWD_REDIR_DPORTS_M, + aclrule->action[2]); + for_each_set_bit(o, &srcs, YT921X_PORT_NUM) { struct yt921x_port *pp = &priv->ports[o]; + unsigned int i; + + if (!aclrule->sw_assisted) + for_each_set_bit(i, &dsts, YT921X_PORT_NUM) + if (!WARN_ON(!pp->acl_dst_cnts[i])) + pp->acl_dst_cnts[i]--; if (!WARN_ON(!pp->acl_cnt)) { pp->acl_cnt--; @@ -1466,6 +1475,8 @@ yt921x_acl_add(struct yt921x_priv *priv, unsigned int binid; unsigned int blkid; unsigned int entid; + unsigned long srcs; + unsigned long dsts; unsigned long m; unsigned int o; u32 ctrl; @@ -1539,8 +1550,8 @@ yt921x_acl_add(struct yt921x_priv *priv, /* Activate ACL if needed */ ctrl = 0; - m = FIELD_GET(YT921X_ACL_KEYb_SPORTS_M, ruleext->r.entries[0].key[1]); - for_each_set_bit(o, &m, YT921X_PORT_NUM) + srcs = FIELD_GET(YT921X_ACL_KEYb_SPORTS_M, aclrule->entries[0].key[1]); + for_each_set_bit(o, &srcs, YT921X_PORT_NUM) if (!priv->ports[o].acl_cnt) ctrl |= YT921X_ACL_PORT_PORTn(o); if (ctrl) { @@ -1559,9 +1570,18 @@ yt921x_acl_add(struct yt921x_priv *priv, } /* Bookkeeping */ - m = FIELD_GET(YT921X_ACL_KEYb_SPORTS_M, ruleext->r.entries[0].key[1]); - for_each_set_bit(o, &m, YT921X_PORT_NUM) - priv->ports[o].acl_cnt++; + dsts = FIELD_GET(YT921X_ACL_ACTc_FWD_REDIR_DPORTS_M, + aclrule->action[2]); + for_each_set_bit(o, &srcs, YT921X_PORT_NUM) { + struct yt921x_port *pp = &priv->ports[o]; + unsigned int i; + + pp->acl_cnt++; + + if (!aclrule->sw_assisted) + for_each_set_bit(i, &dsts, YT921X_PORT_NUM) + pp->acl_dst_cnts[i]++; + } if (meterid < YT921X_METER_NUM) set_bit(meterid, priv->meters_map); -- 2.53.0 Bridge join, leave and flag changes rewrite the isolation masks, so a redirect rule that was trapped because of isolation may become reachable again, and a reachable one may need software assistant. Reload such non-skip_sw redirect rules accordingly. skip_sw rules are not touched at all: they cannot be degraded, port_bridge_leave() cannot be rejected (return value is void), nor can tc be told which rule got invalidated. Warn if any skip_sw rules are invalidated. Fixes: 8eb9e7520b17 ("net: dsa: yt921x: Add ACL support") Signed-off-by: David Yang --- drivers/net/dsa/motorcomm/chip.c | 53 ++++++++++++++++++++--- drivers/net/dsa/motorcomm/chip.h | 2 + drivers/net/dsa/motorcomm/tc.c | 74 ++++++++++++++++++++++++++++++++ drivers/net/dsa/motorcomm/tc.h | 2 + 4 files changed, 125 insertions(+), 6 deletions(-) diff --git a/drivers/net/dsa/motorcomm/chip.c b/drivers/net/dsa/motorcomm/chip.c index 323c1705f613..dc64ec1e310d 100644 --- a/drivers/net/dsa/motorcomm/chip.c +++ b/drivers/net/dsa/motorcomm/chip.c @@ -1371,7 +1371,7 @@ yt921x_isolation_calc_bridge(struct yt921x_priv *priv, u16 ports_mask, } } -static void +void yt921x_isolation_calc_ports(struct yt921x_priv *priv, u16 *ports_maskp, u16 *isolation_masks) { @@ -1488,12 +1488,25 @@ yt921x_bridge_join(struct yt921x_priv *priv, int port) return 0; } +static void yt921x_acl_refresh_extack(int res, struct netlink_ext_ack *extack) +{ + if (res < 0) + NL_SET_ERR_MSG_FMT_MOD(extack, + "Failed to refresh ACL rules: %i\n", + res); + else if (res > 0) + NL_SET_ERR_MSG_MOD(extack, + "Could not reload some redirect rules due to skip_sw, they no longer work as expected\n"); +} + static int yt921x_bridge_flags(struct yt921x_priv *priv, int port, - struct switchdev_brport_flags flags) + struct switchdev_brport_flags flags, + struct netlink_ext_ack *extack) { u32 mask; int res; + int ret; if (flags.mask & BR_LEARNING) { bool learning = flags.val & BR_LEARNING; @@ -1512,9 +1525,17 @@ yt921x_bridge_flags(struct yt921x_priv *priv, int port, /* BR_BCAST_FLOOD: we can filter bcast, but cannot trap them */ if ((flags.mask & BR_HAIRPIN_MODE) || (flags.mask & BR_ISOLATED)) { - res = yt921x_isolation_refresh(priv, BIT(port), true); - if (res) - return res; + /* Trap the rules first, so no frame is dropped in between */ + res = yt921x_acl_refresh(priv, port); + /* Refresh failure is not considered fatal, though the hardware + * is likely desynced + */ + + ret = yt921x_isolation_refresh(priv, BIT(port), true); + if (ret) + return ret; + + yt921x_acl_refresh_extack(res, extack); } return 0; @@ -1586,7 +1607,7 @@ yt921x_dsa_port_bridge_flags(struct dsa_switch *ds, int port, return 0; mutex_lock(&priv->reg_lock); - res = yt921x_bridge_flags(priv, port, flags); + res = yt921x_bridge_flags(priv, port, flags, extack); mutex_unlock(&priv->reg_lock); return res; @@ -1604,7 +1625,17 @@ yt921x_dsa_port_bridge_leave(struct dsa_switch *ds, int port, return; mutex_lock(&priv->reg_lock); + + res = yt921x_acl_refresh(priv, port); + if (res < 0) + dev_err(dev, "Failed to refresh ACL rules for port %d: %i\n", + port, res); + else if (res > 0) + dev_warn(dev, "Could not reload some redirect rules for port %d due to skip_sw, they no longer work as expected\n", + port); + res = yt921x_bridge_leave(priv, port); + mutex_unlock(&priv->reg_lock); if (res) @@ -1625,6 +1656,16 @@ yt921x_dsa_port_bridge_join(struct dsa_switch *ds, int port, mutex_lock(&priv->reg_lock); res = yt921x_bridge_join(priv, port); + if (res) + goto end; + + res = yt921x_acl_refresh(priv, port); + if (res) { + yt921x_acl_refresh_extack(res, extack); + res = 0; + } + +end: mutex_unlock(&priv->reg_lock); return res; diff --git a/drivers/net/dsa/motorcomm/chip.h b/drivers/net/dsa/motorcomm/chip.h index 4085a3c52f20..0484d1bb2e44 100644 --- a/drivers/net/dsa/motorcomm/chip.h +++ b/drivers/net/dsa/motorcomm/chip.h @@ -560,6 +560,8 @@ static inline u32 ethaddr_lo2_to_u32(const unsigned char *addr) return (addr[4] << 8) | addr[5]; } +void yt921x_isolation_calc_ports(struct yt921x_priv *priv, u16 *ports_maskp, + u16 *isolation_masks); u16 yt921x_isolation_calc_port(struct yt921x_priv *priv, int port); #endif diff --git a/drivers/net/dsa/motorcomm/tc.c b/drivers/net/dsa/motorcomm/tc.c index 7691da95d76f..00c632447b3c 100644 --- a/drivers/net/dsa/motorcomm/tc.c +++ b/drivers/net/dsa/motorcomm/tc.c @@ -1393,6 +1393,80 @@ yt921x_acl_commit(struct yt921x_priv *priv, unsigned int entid, u8 entsmask) return 0; } +/* Only non-skip_sw rules are refreshes; skip_sw rules cannot be degraded, + * port_bridge_leave() cannot be rejected (return value is void), nor can tc be + * told which rule got invalidated. + * + * Return 1 if any skip_sw rules are now invalidated. + */ +int yt921x_acl_refresh(struct yt921x_priv *priv, int port) +{ + u16 isolation_masks[YT921X_PORT_NUM]; + bool inability = false; + u16 ports_maskp = ~0; + int res; + int ret; + + yt921x_isolation_calc_ports(priv, &ports_maskp, isolation_masks); + + ret = 0; + + for (unsigned int blkid = 0; blkid < YT921X_ACL_BLK_NUM; blkid++) { + const struct yt921x_acl_blk *aclblk = priv->acl_blks[blkid]; + unsigned long dsts; + unsigned long srcs; + unsigned int o; + bool assisting; + + if (!aclblk) + continue; + + for (unsigned int i = 0; i < YT921X_ACL_ENT_PER_BLK; i++) { + struct yt921x_acl_rule *aclrule; + unsigned int entid; + + aclrule = aclblk->rules[i]; + if (!aclrule) + continue; + + if (!(aclrule->action[2] & YT921X_ACL_ACTc_FWD_EN) || + (aclrule->action[2] & YT921X_ACL_ACTc_FWD_M) != + YT921X_ACL_ACTc_FWD_REDIR) + continue; + + srcs = FIELD_GET(YT921X_ACL_KEYb_SPORTS_M, + aclrule->entries[0].key[1]); + dsts = FIELD_GET(YT921X_ACL_ACTc_FWD_REDIR_DPORTS_M, + aclrule->action[2]); + if (!((srcs | dsts) & BIT(port))) + continue; + + assisting = false; + for_each_set_bit(o, &srcs, YT921X_PORT_NUM) + if (dsts & isolation_masks[o]) { + assisting = true; + break; + } + + if (!aclrule->sw_assisted) { + inability = true; + continue; + } + + entid = YT921X_ACL_ENT_PER_BLK * blkid + i; + if (aclrule->sw_assisting != assisting) { + aclrule->sw_assisting = assisting; + res = yt921x_acl_commit(priv, entid, + aclrule->mask); + if (res && !ret) + ret = res; + } + } + } + + return ret ? ret : inability ? 1 : 0; +} + static int yt921x_acl_del(struct yt921x_priv *priv, enum tc_setup_type type, unsigned long tag, u16 ports_mask) diff --git a/drivers/net/dsa/motorcomm/tc.h b/drivers/net/dsa/motorcomm/tc.h index 81c29a8eec5a..8712a4f3af2d 100644 --- a/drivers/net/dsa/motorcomm/tc.h +++ b/drivers/net/dsa/motorcomm/tc.h @@ -371,6 +371,8 @@ yt921x_dsa_port_policer_add(struct dsa_switch *ds, int port, int yt921x_dsa_port_setup_tc(struct dsa_switch *ds, int port, enum tc_setup_type type, void *type_data); + +int yt921x_acl_refresh(struct yt921x_priv *priv, int port); int yt921x_dsa_cls_flower_del(struct dsa_switch *ds, int port, struct flow_cls_offload *cls, bool ingress); -- 2.53.0