Commit 27c5f74c authored by Vladimir Oltean's avatar Vladimir Oltean Committed by David S. Miller

net: bridge: vlan: notify switchdev only when something changed

Currently, when a VLAN entry is added multiple times in a row to a
bridge port, nbp_vlan_add() calls br_switchdev_port_vlan_add() each
time, even if the VLAN already exists and nothing about it has changed:

bridge vlan add dev lan12 vid 100 master static

Similarly, when a VLAN is added multiple times in a row to a bridge,
br_vlan_add_existing() doesn't filter at all the calls to
br_switchdev_port_vlan_add():

bridge vlan add dev br0 vid 100 self

This behavior makes driver-level accounting of VLANs impossible, since
it is enough for a single deletion event to remove a VLAN, but the
addition event can be emitted an unlimited number of times.

The cause for this can be identified as follows: we rely on
__vlan_add_flags() to retroactively tell us whether it has changed
anything about the VLAN flags or VLAN group pvid. So we'd first have to
call __vlan_add_flags() before calling br_switchdev_port_vlan_add(), in
order to have access to the "bool *changed" information. But we don't
want to change the event ordering, because we'd have to revert the
struct net_bridge_vlan changes we've made if switchdev returns an error.

So to solve this, we need another function that tells us whether any
change is going to occur in the VLAN or VLAN group, _prior_ to calling
__vlan_add_flags().

Split __vlan_add_flags() into a precommit and a commit stage, and rename
it to __vlan_flags_update(). The precommit stage,
__vlan_flags_would_change(), will determine whether there is any reason
to notify switchdev due to a change of flags (note: the BRENTRY flag
transition from false to true is treated separately: as a new switchdev
entry, because we skipped notifying the master VLAN when it wasn't a
brentry yet, and therefore not as a change of flags).

With this lookahead/precommit function in place, we can avoid notifying
switchdev if nothing changed for the VLAN and VLAN group.
Signed-off-by: default avatarVladimir Oltean <vladimir.oltean@nxp.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent cab2cd77
...@@ -34,55 +34,70 @@ static struct net_bridge_vlan *br_vlan_lookup(struct rhashtable *tbl, u16 vid) ...@@ -34,55 +34,70 @@ static struct net_bridge_vlan *br_vlan_lookup(struct rhashtable *tbl, u16 vid)
return rhashtable_lookup_fast(tbl, &vid, br_vlan_rht_params); return rhashtable_lookup_fast(tbl, &vid, br_vlan_rht_params);
} }
static bool __vlan_add_pvid(struct net_bridge_vlan_group *vg, static void __vlan_add_pvid(struct net_bridge_vlan_group *vg,
const struct net_bridge_vlan *v) const struct net_bridge_vlan *v)
{ {
if (vg->pvid == v->vid) if (vg->pvid == v->vid)
return false; return;
smp_wmb(); smp_wmb();
br_vlan_set_pvid_state(vg, v->state); br_vlan_set_pvid_state(vg, v->state);
vg->pvid = v->vid; vg->pvid = v->vid;
return true;
} }
static bool __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid) static void __vlan_delete_pvid(struct net_bridge_vlan_group *vg, u16 vid)
{ {
if (vg->pvid != vid) if (vg->pvid != vid)
return false; return;
smp_wmb(); smp_wmb();
vg->pvid = 0; vg->pvid = 0;
return true;
} }
/* Returns true if the BRIDGE_VLAN_INFO_PVID and BRIDGE_VLAN_INFO_UNTAGGED bits /* Update the BRIDGE_VLAN_INFO_PVID and BRIDGE_VLAN_INFO_UNTAGGED flags of @v.
* of @flags produced any change onto @v, false otherwise * If @commit is false, return just whether the BRIDGE_VLAN_INFO_PVID and
* BRIDGE_VLAN_INFO_UNTAGGED bits of @flags would produce any change onto @v.
*/ */
static bool __vlan_add_flags(struct net_bridge_vlan *v, u16 flags) static bool __vlan_flags_update(struct net_bridge_vlan *v, u16 flags,
bool commit)
{ {
struct net_bridge_vlan_group *vg; struct net_bridge_vlan_group *vg;
u16 old_flags = v->flags; bool change;
bool ret;
if (br_vlan_is_master(v)) if (br_vlan_is_master(v))
vg = br_vlan_group(v->br); vg = br_vlan_group(v->br);
else else
vg = nbp_vlan_group(v->port); vg = nbp_vlan_group(v->port);
/* check if anything would be changed on commit */
change = !!(flags & BRIDGE_VLAN_INFO_PVID) == !!(vg->pvid != v->vid) ||
((flags ^ v->flags) & BRIDGE_VLAN_INFO_UNTAGGED);
if (!commit)
goto out;
if (flags & BRIDGE_VLAN_INFO_PVID) if (flags & BRIDGE_VLAN_INFO_PVID)
ret = __vlan_add_pvid(vg, v); __vlan_add_pvid(vg, v);
else else
ret = __vlan_delete_pvid(vg, v->vid); __vlan_delete_pvid(vg, v->vid);
if (flags & BRIDGE_VLAN_INFO_UNTAGGED) if (flags & BRIDGE_VLAN_INFO_UNTAGGED)
v->flags |= BRIDGE_VLAN_INFO_UNTAGGED; v->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
else else
v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED; v->flags &= ~BRIDGE_VLAN_INFO_UNTAGGED;
return ret || !!((old_flags ^ v->flags) & BRIDGE_VLAN_INFO_UNTAGGED); out:
return change;
}
static bool __vlan_flags_would_change(struct net_bridge_vlan *v, u16 flags)
{
return __vlan_flags_update(v, flags, false);
}
static void __vlan_flags_commit(struct net_bridge_vlan *v, u16 flags)
{
__vlan_flags_update(v, flags, true);
} }
static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br, static int __vlan_vid_add(struct net_device *dev, struct net_bridge *br,
...@@ -315,7 +330,7 @@ static int __vlan_add(struct net_bridge_vlan *v, u16 flags, ...@@ -315,7 +330,7 @@ static int __vlan_add(struct net_bridge_vlan *v, u16 flags,
goto out_fdb_insert; goto out_fdb_insert;
__vlan_add_list(v); __vlan_add_list(v);
__vlan_add_flags(v, flags); __vlan_flags_commit(v, flags);
br_multicast_toggle_one_vlan(v, true); br_multicast_toggle_one_vlan(v, true);
if (p) if (p)
...@@ -682,17 +697,29 @@ static int br_vlan_add_existing(struct net_bridge *br, ...@@ -682,17 +697,29 @@ static int br_vlan_add_existing(struct net_bridge *br,
u16 flags, bool *changed, u16 flags, bool *changed,
struct netlink_ext_ack *extack) struct netlink_ext_ack *extack)
{ {
bool would_change = __vlan_flags_would_change(vlan, flags);
bool becomes_brentry = false;
int err; int err;
if (!br_vlan_is_brentry(vlan)) {
/* Trying to change flags of non-existent bridge vlan */ /* Trying to change flags of non-existent bridge vlan */
if (!br_vlan_is_brentry(vlan) && !(flags & BRIDGE_VLAN_INFO_BRENTRY)) if (!(flags & BRIDGE_VLAN_INFO_BRENTRY))
return -EINVAL; return -EINVAL;
err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags, extack); becomes_brentry = true;
}
/* Master VLANs that aren't brentries weren't notified before,
* time to notify them now.
*/
if (becomes_brentry || would_change) {
err = br_switchdev_port_vlan_add(br->dev, vlan->vid, flags,
extack);
if (err && err != -EOPNOTSUPP) if (err && err != -EOPNOTSUPP)
return err; return err;
}
if (!br_vlan_is_brentry(vlan)) { if (becomes_brentry) {
/* It was only kept for port vlans, now make it real */ /* It was only kept for port vlans, now make it real */
err = br_fdb_add_local(br, NULL, br->dev->dev_addr, vlan->vid); err = br_fdb_add_local(br, NULL, br->dev->dev_addr, vlan->vid);
if (err) { if (err) {
...@@ -707,7 +734,8 @@ static int br_vlan_add_existing(struct net_bridge *br, ...@@ -707,7 +734,8 @@ static int br_vlan_add_existing(struct net_bridge *br,
br_multicast_toggle_one_vlan(vlan, true); br_multicast_toggle_one_vlan(vlan, true);
} }
if (__vlan_add_flags(vlan, flags)) __vlan_flags_commit(vlan, flags);
if (would_change)
*changed = true; *changed = true;
return 0; return 0;
...@@ -1257,11 +1285,18 @@ int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags, ...@@ -1257,11 +1285,18 @@ int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags,
*changed = false; *changed = false;
vlan = br_vlan_find(nbp_vlan_group(port), vid); vlan = br_vlan_find(nbp_vlan_group(port), vid);
if (vlan) { if (vlan) {
bool would_change = __vlan_flags_would_change(vlan, flags);
if (would_change) {
/* Pass the flags to the hardware bridge */ /* Pass the flags to the hardware bridge */
ret = br_switchdev_port_vlan_add(port->dev, vid, flags, extack); ret = br_switchdev_port_vlan_add(port->dev, vid,
flags, extack);
if (ret && ret != -EOPNOTSUPP) if (ret && ret != -EOPNOTSUPP)
return ret; return ret;
*changed = __vlan_add_flags(vlan, flags); }
__vlan_flags_commit(vlan, flags);
*changed = would_change;
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment