Commit 9b8e1056 authored by David S. Miller's avatar David S. Miller

Merge branch 'bridge-fdb-minor-cleanup'

Nikolay Aleksandrov says:

====================
bridge: minor fdb cleanup

These patches aim to simplify the bridge fdb API a little by removing some
redundant functions and converting them into wrappers of a single function.
Also add proper lock checking to avoid future mistakes for the search
functions.
====================
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 91eaa475 5019ab50
...@@ -79,7 +79,7 @@ netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev) ...@@ -79,7 +79,7 @@ netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
br_multicast_flood(mdst, skb, false, true); br_multicast_flood(mdst, skb, false, true);
else else
br_flood(br, skb, BR_PKT_MULTICAST, false, true); br_flood(br, skb, BR_PKT_MULTICAST, false, true);
} else if ((dst = __br_fdb_get(br, dest, vid)) != NULL) { } else if ((dst = br_fdb_find_rcu(br, dest, vid)) != NULL) {
br_forward(dst->dst, skb, false, true); br_forward(dst->dst, skb, false, true);
} else { } else {
br_flood(br, skb, BR_PKT_UNICAST, false, true); br_flood(br, skb, BR_PKT_UNICAST, false, true);
......
...@@ -28,9 +28,6 @@ ...@@ -28,9 +28,6 @@
#include "br_private.h" #include "br_private.h"
static struct kmem_cache *br_fdb_cache __read_mostly; static struct kmem_cache *br_fdb_cache __read_mostly;
static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
const unsigned char *addr,
__u16 vid);
static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source, static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
const unsigned char *addr, u16 vid); const unsigned char *addr, u16 vid);
static void fdb_notify(struct net_bridge *br, static void fdb_notify(struct net_bridge *br,
...@@ -86,6 +83,47 @@ static void fdb_rcu_free(struct rcu_head *head) ...@@ -86,6 +83,47 @@ static void fdb_rcu_free(struct rcu_head *head)
kmem_cache_free(br_fdb_cache, ent); kmem_cache_free(br_fdb_cache, ent);
} }
static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head,
const unsigned char *addr,
__u16 vid)
{
struct net_bridge_fdb_entry *f;
WARN_ON_ONCE(!rcu_read_lock_held());
hlist_for_each_entry_rcu(f, head, hlist)
if (ether_addr_equal(f->addr.addr, addr) && f->vlan_id == vid)
break;
return f;
}
/* requires bridge hash_lock */
static struct net_bridge_fdb_entry *br_fdb_find(struct net_bridge *br,
const unsigned char *addr,
__u16 vid)
{
struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
struct net_bridge_fdb_entry *fdb;
WARN_ON_ONCE(!br_hash_lock_held(br));
rcu_read_lock();
fdb = fdb_find_rcu(head, addr, vid);
rcu_read_unlock();
return fdb;
}
struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
const unsigned char *addr,
__u16 vid)
{
struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
return fdb_find_rcu(head, addr, vid);
}
/* When a static FDB entry is added, the mac address from the entry is /* 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 * added to the bridge private HW address list and all required ports
* are then updated with the new information. * are then updated with the new information.
...@@ -198,11 +236,10 @@ void br_fdb_find_delete_local(struct net_bridge *br, ...@@ -198,11 +236,10 @@ void br_fdb_find_delete_local(struct net_bridge *br,
const struct net_bridge_port *p, const struct net_bridge_port *p,
const unsigned char *addr, u16 vid) const unsigned char *addr, u16 vid)
{ {
struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
struct net_bridge_fdb_entry *f; struct net_bridge_fdb_entry *f;
spin_lock_bh(&br->hash_lock); spin_lock_bh(&br->hash_lock);
f = fdb_find(head, addr, vid); f = br_fdb_find(br, addr, vid);
if (f && f->is_local && !f->added_by_user && f->dst == p) if (f && f->is_local && !f->added_by_user && f->dst == p)
fdb_delete_local(br, p, f); fdb_delete_local(br, p, f);
spin_unlock_bh(&br->hash_lock); spin_unlock_bh(&br->hash_lock);
...@@ -266,7 +303,7 @@ void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr) ...@@ -266,7 +303,7 @@ void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
spin_lock_bh(&br->hash_lock); spin_lock_bh(&br->hash_lock);
/* If old entry was unassociated with any port, then delete it. */ /* If old entry was unassociated with any port, then delete it. */
f = __br_fdb_get(br, br->dev->dev_addr, 0); f = br_fdb_find(br, br->dev->dev_addr, 0);
if (f && f->is_local && !f->dst && !f->added_by_user) if (f && f->is_local && !f->dst && !f->added_by_user)
fdb_delete_local(br, NULL, f); fdb_delete_local(br, NULL, f);
...@@ -281,7 +318,7 @@ void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr) ...@@ -281,7 +318,7 @@ void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr)
list_for_each_entry(v, &vg->vlan_list, vlist) { list_for_each_entry(v, &vg->vlan_list, vlist) {
if (!br_vlan_should_use(v)) if (!br_vlan_should_use(v))
continue; continue;
f = __br_fdb_get(br, br->dev->dev_addr, v->vid); f = br_fdb_find(br, br->dev->dev_addr, v->vid);
if (f && f->is_local && !f->dst && !f->added_by_user) if (f && f->is_local && !f->dst && !f->added_by_user)
fdb_delete_local(br, NULL, f); fdb_delete_local(br, NULL, f);
fdb_insert(br, NULL, newaddr, v->vid); fdb_insert(br, NULL, newaddr, v->vid);
...@@ -380,24 +417,6 @@ void br_fdb_delete_by_port(struct net_bridge *br, ...@@ -380,24 +417,6 @@ void br_fdb_delete_by_port(struct net_bridge *br,
spin_unlock_bh(&br->hash_lock); spin_unlock_bh(&br->hash_lock);
} }
/* No locking or refcounting, assumes caller has rcu_read_lock */
struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
const unsigned char *addr,
__u16 vid)
{
struct net_bridge_fdb_entry *fdb;
hlist_for_each_entry_rcu(fdb,
&br->hash[br_mac_hash(addr, vid)], hlist) {
if (ether_addr_equal(fdb->addr.addr, addr) &&
fdb->vlan_id == vid) {
return fdb;
}
}
return NULL;
}
#if IS_ENABLED(CONFIG_ATM_LANE) #if IS_ENABLED(CONFIG_ATM_LANE)
/* Interface used by ATM LANE hook to test /* Interface used by ATM LANE hook to test
* if an addr is on some other bridge port */ * if an addr is on some other bridge port */
...@@ -412,7 +431,7 @@ int br_fdb_test_addr(struct net_device *dev, unsigned char *addr) ...@@ -412,7 +431,7 @@ int br_fdb_test_addr(struct net_device *dev, unsigned char *addr)
if (!port) if (!port)
ret = 0; ret = 0;
else { else {
fdb = __br_fdb_get(port->br, addr, 0); fdb = br_fdb_find_rcu(port->br, addr, 0);
ret = fdb && fdb->dst && fdb->dst->dev != dev && ret = fdb && fdb->dst && fdb->dst->dev != dev &&
fdb->dst->state == BR_STATE_FORWARDING; fdb->dst->state == BR_STATE_FORWARDING;
} }
...@@ -474,34 +493,6 @@ int br_fdb_fillbuf(struct net_bridge *br, void *buf, ...@@ -474,34 +493,6 @@ int br_fdb_fillbuf(struct net_bridge *br, void *buf,
return num; return num;
} }
static struct net_bridge_fdb_entry *fdb_find(struct hlist_head *head,
const unsigned char *addr,
__u16 vid)
{
struct net_bridge_fdb_entry *fdb;
hlist_for_each_entry(fdb, head, hlist) {
if (ether_addr_equal(fdb->addr.addr, addr) &&
fdb->vlan_id == vid)
return fdb;
}
return NULL;
}
static struct net_bridge_fdb_entry *fdb_find_rcu(struct hlist_head *head,
const unsigned char *addr,
__u16 vid)
{
struct net_bridge_fdb_entry *fdb;
hlist_for_each_entry_rcu(fdb, head, hlist) {
if (ether_addr_equal(fdb->addr.addr, addr) &&
fdb->vlan_id == vid)
return fdb;
}
return NULL;
}
static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head, static struct net_bridge_fdb_entry *fdb_create(struct hlist_head *head,
struct net_bridge_port *source, struct net_bridge_port *source,
const unsigned char *addr, const unsigned char *addr,
...@@ -535,7 +526,7 @@ static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source, ...@@ -535,7 +526,7 @@ static int fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
if (!is_valid_ether_addr(addr)) if (!is_valid_ether_addr(addr))
return -EINVAL; return -EINVAL;
fdb = fdb_find(head, addr, vid); fdb = br_fdb_find(br, addr, vid);
if (fdb) { if (fdb) {
/* it is okay to have multiple ports with same /* it is okay to have multiple ports with same
* address, just use the first one. * address, just use the first one.
...@@ -608,7 +599,7 @@ void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source, ...@@ -608,7 +599,7 @@ void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
} }
} else { } else {
spin_lock(&br->hash_lock); spin_lock(&br->hash_lock);
if (likely(!fdb_find(head, addr, vid))) { if (likely(!fdb_find_rcu(head, addr, vid))) {
fdb = fdb_create(head, source, addr, vid, 0, 0); fdb = fdb_create(head, source, addr, vid, 0, 0);
if (fdb) { if (fdb) {
if (unlikely(added_by_user)) if (unlikely(added_by_user))
...@@ -792,7 +783,7 @@ static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source, ...@@ -792,7 +783,7 @@ static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
return -EINVAL; return -EINVAL;
} }
fdb = fdb_find(head, addr, vid); fdb = br_fdb_find(br, addr, vid);
if (fdb == NULL) { if (fdb == NULL) {
if (!(flags & NLM_F_CREATE)) if (!(flags & NLM_F_CREATE))
return -ENOENT; return -ENOENT;
...@@ -939,55 +930,30 @@ int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[], ...@@ -939,55 +930,30 @@ int br_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
return err; return err;
} }
static int fdb_delete_by_addr(struct net_bridge *br, const u8 *addr, static int fdb_delete_by_addr_and_port(struct net_bridge *br,
u16 vid) const struct net_bridge_port *p,
{
struct hlist_head *head = &br->hash[br_mac_hash(addr, vid)];
struct net_bridge_fdb_entry *fdb;
fdb = fdb_find(head, addr, vid);
if (!fdb)
return -ENOENT;
fdb_delete(br, fdb);
return 0;
}
static int __br_fdb_delete_by_addr(struct net_bridge *br,
const unsigned char *addr, u16 vid)
{
int err;
spin_lock_bh(&br->hash_lock);
err = fdb_delete_by_addr(br, addr, vid);
spin_unlock_bh(&br->hash_lock);
return err;
}
static int fdb_delete_by_addr_and_port(struct net_bridge_port *p,
const u8 *addr, u16 vlan) const u8 *addr, u16 vlan)
{ {
struct net_bridge *br = p->br;
struct hlist_head *head = &br->hash[br_mac_hash(addr, vlan)];
struct net_bridge_fdb_entry *fdb; struct net_bridge_fdb_entry *fdb;
fdb = fdb_find(head, addr, vlan); fdb = br_fdb_find(br, addr, vlan);
if (!fdb || fdb->dst != p) if (!fdb || fdb->dst != p)
return -ENOENT; return -ENOENT;
fdb_delete(br, fdb); fdb_delete(br, fdb);
return 0; return 0;
} }
static int __br_fdb_delete(struct net_bridge_port *p, static int __br_fdb_delete(struct net_bridge *br,
const struct net_bridge_port *p,
const unsigned char *addr, u16 vid) const unsigned char *addr, u16 vid)
{ {
int err; int err;
spin_lock_bh(&p->br->hash_lock); spin_lock_bh(&br->hash_lock);
err = fdb_delete_by_addr_and_port(p, addr, vid); err = fdb_delete_by_addr_and_port(br, p, addr, vid);
spin_unlock_bh(&p->br->hash_lock); spin_unlock_bh(&br->hash_lock);
return err; return err;
} }
...@@ -1000,7 +966,7 @@ int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], ...@@ -1000,7 +966,7 @@ int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
struct net_bridge_vlan_group *vg; struct net_bridge_vlan_group *vg;
struct net_bridge_port *p = NULL; struct net_bridge_port *p = NULL;
struct net_bridge_vlan *v; struct net_bridge_vlan *v;
struct net_bridge *br = NULL; struct net_bridge *br;
int err; int err;
if (dev->priv_flags & IFF_EBRIDGE) { if (dev->priv_flags & IFF_EBRIDGE) {
...@@ -1014,6 +980,7 @@ int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], ...@@ -1014,6 +980,7 @@ int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
return -EINVAL; return -EINVAL;
} }
vg = nbp_vlan_group(p); vg = nbp_vlan_group(p);
br = p->br;
} }
if (vid) { if (vid) {
...@@ -1023,30 +990,20 @@ int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[], ...@@ -1023,30 +990,20 @@ int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
return -EINVAL; return -EINVAL;
} }
if (dev->priv_flags & IFF_EBRIDGE) err = __br_fdb_delete(br, p, addr, vid);
err = __br_fdb_delete_by_addr(br, addr, vid);
else
err = __br_fdb_delete(p, addr, vid);
} else { } else {
err = -ENOENT; err = -ENOENT;
if (dev->priv_flags & IFF_EBRIDGE) err &= __br_fdb_delete(br, p, addr, 0);
err = __br_fdb_delete_by_addr(br, addr, 0);
else
err &= __br_fdb_delete(p, addr, 0);
if (!vg || !vg->num_vlans) if (!vg || !vg->num_vlans)
goto out; return err;
list_for_each_entry(v, &vg->vlan_list, vlist) { list_for_each_entry(v, &vg->vlan_list, vlist) {
if (!br_vlan_should_use(v)) if (!br_vlan_should_use(v))
continue; continue;
if (dev->priv_flags & IFF_EBRIDGE) err &= __br_fdb_delete(br, p, addr, v->vid);
err = __br_fdb_delete_by_addr(br, addr, v->vid);
else
err &= __br_fdb_delete(p, addr, v->vid);
} }
} }
out:
return err; return err;
} }
...@@ -1117,7 +1074,7 @@ int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p, ...@@ -1117,7 +1074,7 @@ int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
spin_lock_bh(&br->hash_lock); spin_lock_bh(&br->hash_lock);
head = &br->hash[br_mac_hash(addr, vid)]; head = &br->hash[br_mac_hash(addr, vid)];
fdb = fdb_find(head, addr, vid); fdb = br_fdb_find(br, addr, vid);
if (!fdb) { if (!fdb) {
fdb = fdb_create(head, p, addr, vid, 0, 0); fdb = fdb_create(head, p, addr, vid, 0, 0);
if (!fdb) { if (!fdb) {
...@@ -1145,15 +1102,13 @@ int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p, ...@@ -1145,15 +1102,13 @@ int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p, int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
const unsigned char *addr, u16 vid) const unsigned char *addr, u16 vid)
{ {
struct hlist_head *head;
struct net_bridge_fdb_entry *fdb; struct net_bridge_fdb_entry *fdb;
int err = 0; int err = 0;
ASSERT_RTNL(); ASSERT_RTNL();
spin_lock_bh(&br->hash_lock); spin_lock_bh(&br->hash_lock);
head = &br->hash[br_mac_hash(addr, vid)]; fdb = br_fdb_find(br, addr, vid);
fdb = fdb_find(head, addr, vid);
if (fdb && fdb->added_by_external_learn) if (fdb && fdb->added_by_external_learn)
fdb_delete(br, fdb); fdb_delete(br, fdb);
else else
......
...@@ -114,7 +114,7 @@ static void br_do_proxy_arp(struct sk_buff *skb, struct net_bridge *br, ...@@ -114,7 +114,7 @@ static void br_do_proxy_arp(struct sk_buff *skb, struct net_bridge *br,
return; return;
} }
f = __br_fdb_get(br, n->ha, vid); f = br_fdb_find_rcu(br, n->ha, vid);
if (f && ((p->flags & BR_PROXYARP) || if (f && ((p->flags & BR_PROXYARP) ||
(f->dst && (f->dst->flags & BR_PROXYARP_WIFI)))) { (f->dst && (f->dst->flags & BR_PROXYARP_WIFI)))) {
arp_send(ARPOP_REPLY, ETH_P_ARP, sip, skb->dev, tip, arp_send(ARPOP_REPLY, ETH_P_ARP, sip, skb->dev, tip,
...@@ -189,7 +189,7 @@ int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb ...@@ -189,7 +189,7 @@ int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb
} }
break; break;
case BR_PKT_UNICAST: case BR_PKT_UNICAST:
dst = __br_fdb_get(br, dest, vid); dst = br_fdb_find_rcu(br, dest, vid);
default: default:
break; break;
} }
......
...@@ -507,8 +507,9 @@ void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr); ...@@ -507,8 +507,9 @@ void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr);
void br_fdb_cleanup(struct work_struct *work); void br_fdb_cleanup(struct work_struct *work);
void br_fdb_delete_by_port(struct net_bridge *br, void br_fdb_delete_by_port(struct net_bridge *br,
const struct net_bridge_port *p, u16 vid, int do_all); const struct net_bridge_port *p, u16 vid, int do_all);
struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br, struct net_bridge_fdb_entry *br_fdb_find_rcu(struct net_bridge *br,
const unsigned char *addr, __u16 vid); const unsigned char *addr,
__u16 vid);
int br_fdb_test_addr(struct net_device *dev, unsigned char *addr); int br_fdb_test_addr(struct net_device *dev, unsigned char *addr);
int br_fdb_fillbuf(struct net_bridge *br, void *buf, unsigned long count, int br_fdb_fillbuf(struct net_bridge *br, void *buf, unsigned long count,
unsigned long off); unsigned long off);
...@@ -530,6 +531,15 @@ int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p, ...@@ -530,6 +531,15 @@ int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p, int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
const unsigned char *addr, u16 vid); const unsigned char *addr, u16 vid);
static inline bool br_hash_lock_held(struct net_bridge *br)
{
#ifdef CONFIG_LOCKDEP
return lockdep_is_held(&br->hash_lock);
#else
return true;
#endif
}
/* br_forward.c */ /* br_forward.c */
enum br_pkt_type { enum br_pkt_type {
BR_PKT_UNICAST, BR_PKT_UNICAST,
......
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