Skip to content

Commit

Permalink
Merge branch 'bridge-non-promisc'
Browse files Browse the repository at this point in the history
Vlad Yasevich says:

====================
bridge: Non-promisc bridge ports support

This series adds functionality to the bridge device to enable
operations without setting all ports to promiscuous mode.

The basic concept is this.  The bridge keeps track of the ports
that support learning and flooding packets to unknown destinations.
We call these ports auto-discovery ports since they automatically
discover who is behind them through learning and flooding.

If flooding and learning are disabled via flags, then the port
requires static configuration to tell it which mac addresses
are behind it.  This is accomplished through adding of fdbs.
These fdbs should be static as dynamic fdbs can expire and systems
will become unreachable due to lack of flooding.

If the user marks all ports as needing static configuration then
we can safely make them non-promiscuous since we will know all the
information about them.

If the user leaves only 1 port as automatic, then we can mark
that port as not-promiscuous as well.  One could think of
this a edge relay similar to what's support by embedded switches
in SRIOV devices.  Since we have all the information about the
other ports, we can just program the mac addresses into the
single automatic port to receive all necessary traffic.
More information about this is patch 6.

In other cases, we keep all ports promiscuous as before.

There are some other cases when promiscuous mode has to be turned
back on.  One is when the bridge itself if placed in promiscuous
mode (user sets promisc flag).  The other is if vlan filtering is
turned off.  Since this is the default configuration, the default
bridge operation is not changed.

Changes since v2:
 - White space and spelling fixes from Michael Tsirkin
 - Squash patches 6, 7 and 8 to prevent bisect breakage.

Changes since v1:
 - Address issues rasied by Stephen Heminger
 - Address initializer comments raised by Sergey Shtylyov
 - Rebased recent net-next.

Changes since rfc v2:
 - Better description of in the commit logs
 - Leave port in promiscuous mode if IFF_UNICAST_FLT is disabled on the
   device.
 - Fix issue with flag masking
 - Rework patch ordering a bit.

Changes since rfc v1:
 - Removed private list.  We now traverse the fdb hashtable itself
   to write necessary addresses to the ports (Stephen's concern)
 - Add learning flag to the mask for flags that decides if the port
   is 'auto' or not (suggest by MST and Jamal).
 - Simplified tracking of such ports at the cost of a loop over all
   ports (suggested by MST)

I've played with quite a large number of ports and the current approach
seems to work fairly well.
====================

Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
David S. Miller committed May 16, 2014
2 parents 4c30b52 + 2796d0c commit 2770abc
Show file tree
Hide file tree
Showing 7 changed files with 294 additions and 20 deletions.
7 changes: 7 additions & 0 deletions net/bridge/br_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ static void br_dev_set_multicast_list(struct net_device *dev)
{
}

static void br_dev_change_rx_flags(struct net_device *dev, int change)
{
if (change & IFF_PROMISC)
br_manage_promisc(netdev_priv(dev));
}

static int br_dev_stop(struct net_device *dev)
{
struct net_bridge *br = netdev_priv(dev);
Expand Down Expand Up @@ -309,6 +315,7 @@ static const struct net_device_ops br_netdev_ops = {
.ndo_get_stats64 = br_get_stats64,
.ndo_set_mac_address = br_set_mac_address,
.ndo_set_rx_mode = br_dev_set_multicast_list,
.ndo_change_rx_flags = br_dev_change_rx_flags,
.ndo_change_mtu = br_change_mtu,
.ndo_do_ioctl = br_dev_ioctl,
#ifdef CONFIG_NET_POLL_CONTROLLER
Expand Down
131 changes: 125 additions & 6 deletions net/bridge/br_fdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,58 @@ static void fdb_rcu_free(struct rcu_head *head)
kmem_cache_free(br_fdb_cache, ent);
}

/* When a static FDB entry is added, the mac address from the entry is
* added to the bridge private HW address list and all required ports
* are then updated with the new information.
* Called under RTNL.
*/
static void fdb_add_hw(struct net_bridge *br, const unsigned char *addr)
{
int err;
struct net_bridge_port *p, *tmp;

ASSERT_RTNL();

list_for_each_entry(p, &br->port_list, list) {
if (!br_promisc_port(p)) {
err = dev_uc_add(p->dev, addr);
if (err)
goto undo;
}
}

return;
undo:
list_for_each_entry(tmp, &br->port_list, list) {
if (tmp == p)
break;
if (!br_promisc_port(tmp))
dev_uc_del(tmp->dev, addr);
}
}

/* When a static FDB entry is deleted, the HW address from that entry is
* also removed from the bridge private HW address list and updates all
* the ports with needed information.
* Called under RTNL.
*/
static void fdb_del_hw(struct net_bridge *br, const unsigned char *addr)
{
struct net_bridge_port *p;

ASSERT_RTNL();

list_for_each_entry(p, &br->port_list, list) {
if (!br_promisc_port(p))
dev_uc_del(p->dev, addr);
}
}

static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f)
{
if (f->is_static)
fdb_del_hw(br, f->addr.addr);

hlist_del_rcu(&f->hlist);
fdb_notify(br, f, RTM_DELNEIGH);
call_rcu(&f->rcu, fdb_rcu_free);
Expand Down Expand Up @@ -466,6 +516,7 @@ static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
return -ENOMEM;

fdb->is_local = fdb->is_static = 1;
fdb_add_hw(br, addr);
fdb_notify(br, fdb, RTM_NEWNEIGH);
return 0;
}
Expand Down Expand Up @@ -678,13 +729,25 @@ static int fdb_add_entry(struct net_bridge_port *source, const __u8 *addr,
}

if (fdb_to_nud(fdb) != state) {
if (state & NUD_PERMANENT)
fdb->is_local = fdb->is_static = 1;
else if (state & NUD_NOARP) {
if (state & NUD_PERMANENT) {
fdb->is_local = 1;
if (!fdb->is_static) {
fdb->is_static = 1;
fdb_add_hw(br, addr);
}
} else if (state & NUD_NOARP) {
fdb->is_local = 0;
if (!fdb->is_static) {
fdb->is_static = 1;
fdb_add_hw(br, addr);
}
} else {
fdb->is_local = 0;
fdb->is_static = 1;
} else
fdb->is_local = fdb->is_static = 0;
if (fdb->is_static) {
fdb->is_static = 0;
fdb_del_hw(br, addr);
}
}

modified = true;
}
Expand Down Expand Up @@ -874,3 +937,59 @@ int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
out:
return err;
}

int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p)
{
struct net_bridge_fdb_entry *fdb, *tmp;
int i;
int err;

ASSERT_RTNL();

for (i = 0; i < BR_HASH_SIZE; i++) {
hlist_for_each_entry(fdb, &br->hash[i], hlist) {
/* We only care for static entries */
if (!fdb->is_static)
continue;

err = dev_uc_add(p->dev, fdb->addr.addr);
if (err)
goto rollback;
}
}
return 0;

rollback:
for (i = 0; i < BR_HASH_SIZE; i++) {
hlist_for_each_entry(tmp, &br->hash[i], hlist) {
/* If we reached the fdb that failed, we can stop */
if (tmp == fdb)
break;

/* We only care for static entries */
if (!tmp->is_static)
continue;

dev_uc_del(p->dev, tmp->addr.addr);
}
}
return err;
}

void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p)
{
struct net_bridge_fdb_entry *fdb;
int i;

ASSERT_RTNL();

for (i = 0; i < BR_HASH_SIZE; i++) {
hlist_for_each_entry_rcu(fdb, &br->hash[i], hlist) {
/* We only care for static entries */
if (!fdb->is_static)
continue;

dev_uc_del(p->dev, fdb->addr.addr);
}
}
}
123 changes: 119 additions & 4 deletions net/bridge/br_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,110 @@ void br_port_carrier_check(struct net_bridge_port *p)
spin_unlock_bh(&br->lock);
}

static void br_port_set_promisc(struct net_bridge_port *p)
{
int err = 0;

if (br_promisc_port(p))
return;

err = dev_set_promiscuity(p->dev, 1);
if (err)
return;

br_fdb_unsync_static(p->br, p);
p->flags |= BR_PROMISC;
}

static void br_port_clear_promisc(struct net_bridge_port *p)
{
int err;

/* Check if the port is already non-promisc or if it doesn't
* support UNICAST filtering. Without unicast filtering support
* we'll end up re-enabling promisc mode anyway, so just check for
* it here.
*/
if (!br_promisc_port(p) || !(p->dev->priv_flags & IFF_UNICAST_FLT))
return;

/* Since we'll be clearing the promisc mode, program the port
* first so that we don't have interruption in traffic.
*/
err = br_fdb_sync_static(p->br, p);
if (err)
return;

dev_set_promiscuity(p->dev, -1);
p->flags &= ~BR_PROMISC;
}

/* When a port is added or removed or when certain port flags
* change, this function is called to automatically manage
* promiscuity setting of all the bridge ports. We are always called
* under RTNL so can skip using rcu primitives.
*/
void br_manage_promisc(struct net_bridge *br)
{
struct net_bridge_port *p;
bool set_all = false;

/* If vlan filtering is disabled or bridge interface is placed
* into promiscuous mode, place all ports in promiscuous mode.
*/
if ((br->dev->flags & IFF_PROMISC) || !br_vlan_enabled(br))
set_all = true;

list_for_each_entry(p, &br->port_list, list) {
if (set_all) {
br_port_set_promisc(p);
} else {
/* If the number of auto-ports is <= 1, then all other
* ports will have their output configuration
* statically specified through fdbs. Since ingress
* on the auto-port becomes forwarding/egress to other
* ports and egress configuration is statically known,
* we can say that ingress configuration of the
* auto-port is also statically known.
* This lets us disable promiscuous mode and write
* this config to hw.
*/
if (br->auto_cnt <= br_auto_port(p))
br_port_clear_promisc(p);
else
br_port_set_promisc(p);
}
}
}

static void nbp_update_port_count(struct net_bridge *br)
{
struct net_bridge_port *p;
u32 cnt = 0;

list_for_each_entry(p, &br->port_list, list) {
if (br_auto_port(p))
cnt++;
}
if (br->auto_cnt != cnt) {
br->auto_cnt = cnt;
br_manage_promisc(br);
}
}

static void nbp_delete_promisc(struct net_bridge_port *p)
{
/* If port is currently promiscous, unset promiscuity.
* Otherwise, it is a static port so remove all addresses
* from it.
*/
dev_set_allmulti(p->dev, -1);
if (br_promisc_port(p))
dev_set_promiscuity(p->dev, -1);
else
br_fdb_unsync_static(p->br, p);
}

static void release_nbp(struct kobject *kobj)
{
struct net_bridge_port *p
Expand Down Expand Up @@ -133,18 +237,19 @@ static void del_nbp(struct net_bridge_port *p)

sysfs_remove_link(br->ifobj, p->dev->name);

dev_set_promiscuity(dev, -1);
nbp_delete_promisc(p);

spin_lock_bh(&br->lock);
br_stp_disable_port(p);
spin_unlock_bh(&br->lock);

br_ifinfo_notify(RTM_DELLINK, p);

list_del_rcu(&p->list);

nbp_vlan_flush(p);
br_fdb_delete_by_port(br, p, 1);

list_del_rcu(&p->list);
nbp_update_port_count(br);

dev->priv_flags &= ~IFF_BRIDGE_PORT;

Expand Down Expand Up @@ -353,7 +458,7 @@ int br_add_if(struct net_bridge *br, struct net_device *dev)

call_netdevice_notifiers(NETDEV_JOIN, dev);

err = dev_set_promiscuity(dev, 1);
err = dev_set_allmulti(dev, 1);
if (err)
goto put_back;

Expand Down Expand Up @@ -384,6 +489,8 @@ int br_add_if(struct net_bridge *br, struct net_device *dev)

list_add_rcu(&p->list, &br->port_list);

nbp_update_port_count(br);

netdev_update_features(br->dev);

if (br->dev->needed_headroom < dev->needed_headroom)
Expand Down Expand Up @@ -455,3 +562,11 @@ int br_del_if(struct net_bridge *br, struct net_device *dev)

return 0;
}

void br_port_flags_change(struct net_bridge_port *p, unsigned long mask)
{
struct net_bridge *br = p->br;

if (mask & BR_AUTO_MASK)
nbp_update_port_count(br);
}
3 changes: 3 additions & 0 deletions net/bridge/br_netlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
{
int err;
unsigned long old_flags = p->flags;

br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
Expand All @@ -353,6 +354,8 @@ static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
if (err)
return err;
}

br_port_flags_change(p, old_flags ^ p->flags);
return 0;
}

Expand Down
Loading

0 comments on commit 2770abc

Please sign in to comment.