* [PATCH net-next v3 1/6] net: bridge: Set BR_FDB_ADDED_BY_USER early in fdb_add_entry
2023-09-05 11:47 [PATCH net-next v3 0/6] bridge: Add a limit on learned FDB entries Johannes Nixdorf
@ 2023-09-05 11:47 ` Johannes Nixdorf
2023-09-05 20:53 ` Jakub Kicinski
2023-09-05 11:47 ` [PATCH net-next v3 2/6] net: bridge: Set strict_start_type for br_policy Johannes Nixdorf
` (4 subsequent siblings)
5 siblings, 1 reply; 10+ messages in thread
From: Johannes Nixdorf @ 2023-09-05 11:47 UTC (permalink / raw)
To: David S. Miller, Andrew Lunn, David Ahern, Eric Dumazet,
Florian Fainelli, Ido Schimmel, Jakub Kicinski,
Nikolay Aleksandrov, Oleksij Rempel, Paolo Abeni, Roopa Prabhu,
Shuah Khan, Vladimir Oltean
Cc: bridge, netdev, linux-kernel, linux-kselftest, Johannes Nixdorf
In preparation of the following fdb limit for dynamically learned entries,
allow fdb_create to detect that the entry was added by the user. This
way it can skip applying the limit in this case.
Signed-off-by: Johannes Nixdorf <jnixdorf-oss@avm.de>
---
net/bridge/br_fdb.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/net/bridge/br_fdb.c b/net/bridge/br_fdb.c
index e69a872bfc1d..06e28ef8d9ff 100644
--- a/net/bridge/br_fdb.c
+++ b/net/bridge/br_fdb.c
@@ -1056,7 +1056,7 @@ static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
if (!(flags & NLM_F_CREATE))
return -ENOENT;
- fdb = fdb_create(br, source, addr, vid, 0);
+ fdb = fdb_create(br, source, addr, vid, BIT(BR_FDB_ADDED_BY_USER));
if (!fdb)
return -ENOMEM;
@@ -1069,6 +1069,8 @@ static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
WRITE_ONCE(fdb->dst, source);
modified = true;
}
+
+ set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
}
if (fdb_to_nud(br, fdb) != state) {
@@ -1100,8 +1102,6 @@ static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
if (fdb_handle_notify(fdb, notify))
modified = true;
- set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
-
fdb->used = jiffies;
if (modified) {
if (refresh)
--
2.42.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [PATCH net-next v3 1/6] net: bridge: Set BR_FDB_ADDED_BY_USER early in fdb_add_entry
2023-09-05 11:47 ` [PATCH net-next v3 1/6] net: bridge: Set BR_FDB_ADDED_BY_USER early in fdb_add_entry Johannes Nixdorf
@ 2023-09-05 20:53 ` Jakub Kicinski
0 siblings, 0 replies; 10+ messages in thread
From: Jakub Kicinski @ 2023-09-05 20:53 UTC (permalink / raw)
To: Johannes Nixdorf
Cc: David S. Miller, Andrew Lunn, David Ahern, Eric Dumazet,
Florian Fainelli, Ido Schimmel, Nikolay Aleksandrov,
Oleksij Rempel, Paolo Abeni, Roopa Prabhu, Shuah Khan,
Vladimir Oltean, bridge, netdev, linux-kernel, linux-kselftest
On Tue, 05 Sep 2023 13:47:18 +0200 Johannes Nixdorf wrote:
> --- a/net/bridge/br_fdb.c
> +++ b/net/bridge/br_fdb.c
> @@ -1056,7 +1056,7 @@ static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
> if (!(flags & NLM_F_CREATE))
> return -ENOENT;
>
> - fdb = fdb_create(br, source, addr, vid, 0);
> + fdb = fdb_create(br, source, addr, vid, BIT(BR_FDB_ADDED_BY_USER));
Please try to wrap your code at 80 chars. Also:
## Form letter - net-next-closed
The merge window for v6.6 has begun and therefore net-next is closed
for new drivers, features, code refactoring and optimizations.
We are currently accepting bug fixes only.
Please repost when net-next reopens after Sept 11th.
RFC patches sent for review only are obviously welcome at any time.
See: https://www.kernel.org/doc/html/next/process/maintainer-netdev.html#development-cycle
--
pw-bot: defer
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH net-next v3 2/6] net: bridge: Set strict_start_type for br_policy
2023-09-05 11:47 [PATCH net-next v3 0/6] bridge: Add a limit on learned FDB entries Johannes Nixdorf
2023-09-05 11:47 ` [PATCH net-next v3 1/6] net: bridge: Set BR_FDB_ADDED_BY_USER early in fdb_add_entry Johannes Nixdorf
@ 2023-09-05 11:47 ` Johannes Nixdorf
2023-09-05 11:47 ` [PATCH net-next v3 3/6] net: bridge: Track and limit dynamically learned FDB entries Johannes Nixdorf
` (3 subsequent siblings)
5 siblings, 0 replies; 10+ messages in thread
From: Johannes Nixdorf @ 2023-09-05 11:47 UTC (permalink / raw)
To: David S. Miller, Andrew Lunn, David Ahern, Eric Dumazet,
Florian Fainelli, Ido Schimmel, Jakub Kicinski,
Nikolay Aleksandrov, Oleksij Rempel, Paolo Abeni, Roopa Prabhu,
Shuah Khan, Vladimir Oltean
Cc: bridge, netdev, linux-kernel, linux-kselftest, Johannes Nixdorf
Set any new attributes added to br_policy to be parsed strictly, to
prevent userspace from passing garbage.
Signed-off-by: Johannes Nixdorf <jnixdorf-oss@avm.de>
---
net/bridge/br_netlink.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/net/bridge/br_netlink.c b/net/bridge/br_netlink.c
index 05c5863d2e20..1dc4e1bce740 100644
--- a/net/bridge/br_netlink.c
+++ b/net/bridge/br_netlink.c
@@ -1217,6 +1217,7 @@ static size_t br_port_get_slave_size(const struct net_device *brdev,
}
static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
+ [IFLA_BR_UNSPEC] = { .strict_start_type = IFLA_BR_MCAST_QUERIER_STATE + 1 },
[IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
[IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
[IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
--
2.42.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH net-next v3 3/6] net: bridge: Track and limit dynamically learned FDB entries
2023-09-05 11:47 [PATCH net-next v3 0/6] bridge: Add a limit on learned FDB entries Johannes Nixdorf
2023-09-05 11:47 ` [PATCH net-next v3 1/6] net: bridge: Set BR_FDB_ADDED_BY_USER early in fdb_add_entry Johannes Nixdorf
2023-09-05 11:47 ` [PATCH net-next v3 2/6] net: bridge: Set strict_start_type for br_policy Johannes Nixdorf
@ 2023-09-05 11:47 ` Johannes Nixdorf
2023-09-14 15:15 ` Nikolay Aleksandrov
2023-09-05 11:47 ` [PATCH net-next v3 4/6] net: bridge: Add netlink knobs for number / max " Johannes Nixdorf
` (2 subsequent siblings)
5 siblings, 1 reply; 10+ messages in thread
From: Johannes Nixdorf @ 2023-09-05 11:47 UTC (permalink / raw)
To: David S. Miller, Andrew Lunn, David Ahern, Eric Dumazet,
Florian Fainelli, Ido Schimmel, Jakub Kicinski,
Nikolay Aleksandrov, Oleksij Rempel, Paolo Abeni, Roopa Prabhu,
Shuah Khan, Vladimir Oltean
Cc: bridge, netdev, linux-kernel, linux-kselftest, Johannes Nixdorf
A malicious actor behind one bridge port may spam the kernel with packets
with a random source MAC address, each of which will create an FDB entry,
each of which is a dynamic allocation in the kernel.
There are roughly 2^48 different MAC addresses, further limited by the
rhashtable they are stored in to 2^31. Each entry is of the type struct
net_bridge_fdb_entry, which is currently 128 bytes big. This means the
maximum amount of memory allocated for FDB entries is 2^31 * 128B =
256GiB, which is too much for most computers.
Mitigate this by maintaining a per bridge count of those automatically
generated entries in fdb_n_learned_entries, and a limit in
fdb_max_learned_entries. If the limit is hit new entries are not learned
anymore.
For backwards compatibility the default setting of 0 disables the limit.
User-added entries by netlink or from bridge or bridge port addresses
are never blocked and do not count towards that limit.
Introduce a new fdb entry flag BR_FDB_DYNAMIC_LEARNED to keep track of
whether an FDB entry is included in the count. The flag is enabled for
dynamically learned entries, and disabled for all other entries. This
should be equivalent to BR_FDB_ADDED_BY_USER and BR_FDB_LOCAL being unset,
but contrary to the two flags it can be toggled atomically.
Atomicity is required here, as there are multiple callers that modify the
flags, but are not under a common lock (br_fdb_update is the exception
for br->hash_lock, br_fdb_external_learn_add for RTNL).
Signed-off-by: Johannes Nixdorf <jnixdorf-oss@avm.de>
---
net/bridge/br_fdb.c | 34 ++++++++++++++++++++++++++++++++--
net/bridge/br_private.h | 4 ++++
2 files changed, 36 insertions(+), 2 deletions(-)
diff --git a/net/bridge/br_fdb.c b/net/bridge/br_fdb.c
index 06e28ef8d9ff..f8a96ed9a338 100644
--- a/net/bridge/br_fdb.c
+++ b/net/bridge/br_fdb.c
@@ -329,11 +329,18 @@ static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f,
hlist_del_init_rcu(&f->fdb_node);
rhashtable_remove_fast(&br->fdb_hash_tbl, &f->rhnode,
br_fdb_rht_params);
+ if (test_bit(BR_FDB_DYNAMIC_LEARNED, &f->flags))
+ atomic_dec(&br->fdb_n_learned_entries);
fdb_notify(br, f, RTM_DELNEIGH, swdev_notify);
call_rcu(&f->rcu, fdb_rcu_free);
}
-/* Delete a local entry if no other port had the same address. */
+/* Delete a local entry if no other port had the same address.
+ *
+ * This function should only be called on entries with BR_FDB_LOCAL set,
+ * so even with BR_FDB_ADDED_BY_USER cleared we never need to increase
+ * the accounting for dynamically learned entries again.
+ */
static void fdb_delete_local(struct net_bridge *br,
const struct net_bridge_port *p,
struct net_bridge_fdb_entry *f)
@@ -388,9 +395,20 @@ static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br,
__u16 vid,
unsigned long flags)
{
+ bool learned = !test_bit(BR_FDB_ADDED_BY_USER, &flags) &&
+ !test_bit(BR_FDB_LOCAL, &flags);
+ u32 max_learned = READ_ONCE(br->fdb_max_learned_entries);
struct net_bridge_fdb_entry *fdb;
int err;
+ if (likely(learned)) {
+ int n_learned = atomic_read(&br->fdb_n_learned_entries);
+
+ if (unlikely(max_learned && n_learned >= max_learned))
+ return NULL;
+ __set_bit(BR_FDB_DYNAMIC_LEARNED, &flags);
+ }
+
fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
if (!fdb)
return NULL;
@@ -407,6 +425,9 @@ static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br,
return NULL;
}
+ if (likely(learned))
+ atomic_inc(&br->fdb_n_learned_entries);
+
hlist_add_head_rcu(&fdb->fdb_node, &br->fdb_list);
return fdb;
@@ -893,8 +914,11 @@ void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
clear_bit(BR_FDB_LOCKED, &fdb->flags);
}
- if (unlikely(test_bit(BR_FDB_ADDED_BY_USER, &flags)))
+ if (unlikely(test_bit(BR_FDB_ADDED_BY_USER, &flags))) {
set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
+ if (test_and_clear_bit(BR_FDB_DYNAMIC_LEARNED, &fdb->flags))
+ atomic_dec(&br->fdb_n_learned_entries);
+ }
if (unlikely(fdb_modified)) {
trace_br_fdb_update(br, source, addr, vid, flags);
fdb_notify(br, fdb, RTM_NEWNEIGH, true);
@@ -1071,6 +1095,8 @@ static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
}
set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
+ if (test_and_clear_bit(BR_FDB_DYNAMIC_LEARNED, &fdb->flags))
+ atomic_dec(&br->fdb_n_learned_entries);
}
if (fdb_to_nud(br, fdb) != state) {
@@ -1445,6 +1471,10 @@ int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
if (!p)
set_bit(BR_FDB_LOCAL, &fdb->flags);
+ if ((swdev_notify || !p) &&
+ test_and_clear_bit(BR_FDB_DYNAMIC_LEARNED, &fdb->flags))
+ atomic_dec(&br->fdb_n_learned_entries);
+
if (modified)
fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
}
diff --git a/net/bridge/br_private.h b/net/bridge/br_private.h
index a63b32c1638e..675cc40ae1dc 100644
--- a/net/bridge/br_private.h
+++ b/net/bridge/br_private.h
@@ -274,6 +274,7 @@ enum {
BR_FDB_NOTIFY,
BR_FDB_NOTIFY_INACTIVE,
BR_FDB_LOCKED,
+ BR_FDB_DYNAMIC_LEARNED,
};
struct net_bridge_fdb_key {
@@ -554,6 +555,9 @@ struct net_bridge {
struct kobject *ifobj;
u32 auto_cnt;
+ atomic_t fdb_n_learned_entries;
+ u32 fdb_max_learned_entries;
+
#ifdef CONFIG_NET_SWITCHDEV
/* Counter used to make sure that hardware domains get unique
* identifiers in case a bridge spans multiple switchdev instances.
--
2.42.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [PATCH net-next v3 3/6] net: bridge: Track and limit dynamically learned FDB entries
2023-09-05 11:47 ` [PATCH net-next v3 3/6] net: bridge: Track and limit dynamically learned FDB entries Johannes Nixdorf
@ 2023-09-14 15:15 ` Nikolay Aleksandrov
0 siblings, 0 replies; 10+ messages in thread
From: Nikolay Aleksandrov @ 2023-09-14 15:15 UTC (permalink / raw)
To: Johannes Nixdorf, David S. Miller, Andrew Lunn, David Ahern,
Eric Dumazet, Florian Fainelli, Ido Schimmel, Jakub Kicinski,
Oleksij Rempel, Paolo Abeni, Roopa Prabhu, Shuah Khan,
Vladimir Oltean
Cc: bridge, netdev, linux-kernel, linux-kselftest
On 9/5/23 14:47, Johannes Nixdorf wrote:
> A malicious actor behind one bridge port may spam the kernel with packets
> with a random source MAC address, each of which will create an FDB entry,
> each of which is a dynamic allocation in the kernel.
>
> There are roughly 2^48 different MAC addresses, further limited by the
> rhashtable they are stored in to 2^31. Each entry is of the type struct
> net_bridge_fdb_entry, which is currently 128 bytes big. This means the
> maximum amount of memory allocated for FDB entries is 2^31 * 128B =
> 256GiB, which is too much for most computers.
>
> Mitigate this by maintaining a per bridge count of those automatically
> generated entries in fdb_n_learned_entries, and a limit in
> fdb_max_learned_entries. If the limit is hit new entries are not learned
> anymore.
>
> For backwards compatibility the default setting of 0 disables the limit.
>
> User-added entries by netlink or from bridge or bridge port addresses
> are never blocked and do not count towards that limit.
>
> Introduce a new fdb entry flag BR_FDB_DYNAMIC_LEARNED to keep track of
> whether an FDB entry is included in the count. The flag is enabled for
> dynamically learned entries, and disabled for all other entries. This
> should be equivalent to BR_FDB_ADDED_BY_USER and BR_FDB_LOCAL being unset,
> but contrary to the two flags it can be toggled atomically.
>
> Atomicity is required here, as there are multiple callers that modify the
> flags, but are not under a common lock (br_fdb_update is the exception
> for br->hash_lock, br_fdb_external_learn_add for RTNL).
>
> Signed-off-by: Johannes Nixdorf <jnixdorf-oss@avm.de>
> ---
> net/bridge/br_fdb.c | 34 ++++++++++++++++++++++++++++++++--
> net/bridge/br_private.h | 4 ++++
> 2 files changed, 36 insertions(+), 2 deletions(-)
>
> diff --git a/net/bridge/br_fdb.c b/net/bridge/br_fdb.c
> index 06e28ef8d9ff..f8a96ed9a338 100644
> --- a/net/bridge/br_fdb.c
> +++ b/net/bridge/br_fdb.c
> @@ -329,11 +329,18 @@ static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f,
> hlist_del_init_rcu(&f->fdb_node);
> rhashtable_remove_fast(&br->fdb_hash_tbl, &f->rhnode,
> br_fdb_rht_params);
> + if (test_bit(BR_FDB_DYNAMIC_LEARNED, &f->flags))
this is racy with br_fdb_update(), the fdb entry is still visible
because a grace period hasn't passed, so in theory br_fdb_update() can
unset the bit after this test and...
> + atomic_dec(&br->fdb_n_learned_entries);
... this will decrease once more wrongly
> fdb_notify(br, f, RTM_DELNEIGH, swdev_notify);
> call_rcu(&f->rcu, fdb_rcu_free);
> }
>
> -/* Delete a local entry if no other port had the same address. */
> +/* Delete a local entry if no other port had the same address.
> + *
> + * This function should only be called on entries with BR_FDB_LOCAL set,
> + * so even with BR_FDB_ADDED_BY_USER cleared we never need to increase
> + * the accounting for dynamically learned entries again.
> + */
> static void fdb_delete_local(struct net_bridge *br,
> const struct net_bridge_port *p,
> struct net_bridge_fdb_entry *f)
> @@ -388,9 +395,20 @@ static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br,
> __u16 vid,
> unsigned long flags)
> {
> + bool learned = !test_bit(BR_FDB_ADDED_BY_USER, &flags) &&
> + !test_bit(BR_FDB_LOCAL, &flags);
> + u32 max_learned = READ_ONCE(br->fdb_max_learned_entries);
> struct net_bridge_fdb_entry *fdb;
> int err;
>
> + if (likely(learned)) {
> + int n_learned = atomic_read(&br->fdb_n_learned_entries);
> +
> + if (unlikely(max_learned && n_learned >= max_learned))
> + return NULL;
> + __set_bit(BR_FDB_DYNAMIC_LEARNED, &flags);
> + }
> +
> fdb = kmem_cache_alloc(br_fdb_cache, GFP_ATOMIC);
> if (!fdb)
> return NULL;
> @@ -407,6 +425,9 @@ static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br,
> return NULL;
> }
>
> + if (likely(learned))
> + atomic_inc(&br->fdb_n_learned_entries);
> +
> hlist_add_head_rcu(&fdb->fdb_node, &br->fdb_list);
>
> return fdb;
> @@ -893,8 +914,11 @@ void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
> clear_bit(BR_FDB_LOCKED, &fdb->flags);
> }
>
> - if (unlikely(test_bit(BR_FDB_ADDED_BY_USER, &flags)))
> + if (unlikely(test_bit(BR_FDB_ADDED_BY_USER, &flags))) {
> set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
> + if (test_and_clear_bit(BR_FDB_DYNAMIC_LEARNED, &fdb->flags))
> + atomic_dec(&br->fdb_n_learned_entries);
> + }
> if (unlikely(fdb_modified)) {
> trace_br_fdb_update(br, source, addr, vid, flags);
> fdb_notify(br, fdb, RTM_NEWNEIGH, true);
> @@ -1071,6 +1095,8 @@ static int fdb_add_entry(struct net_bridge *br, struct net_bridge_port *source,
> }
>
> set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
> + if (test_and_clear_bit(BR_FDB_DYNAMIC_LEARNED, &fdb->flags))
> + atomic_dec(&br->fdb_n_learned_entries);
> }
>
> if (fdb_to_nud(br, fdb) != state) {
> @@ -1445,6 +1471,10 @@ int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
> if (!p)
> set_bit(BR_FDB_LOCAL, &fdb->flags);
>
> + if ((swdev_notify || !p) &&
> + test_and_clear_bit(BR_FDB_DYNAMIC_LEARNED, &fdb->flags))
> + atomic_dec(&br->fdb_n_learned_entries);
> +
> if (modified)
> fdb_notify(br, fdb, RTM_NEWNEIGH, swdev_notify);
> }
> diff --git a/net/bridge/br_private.h b/net/bridge/br_private.h
> index a63b32c1638e..675cc40ae1dc 100644
> --- a/net/bridge/br_private.h
> +++ b/net/bridge/br_private.h
> @@ -274,6 +274,7 @@ enum {
> BR_FDB_NOTIFY,
> BR_FDB_NOTIFY_INACTIVE,
> BR_FDB_LOCKED,
> + BR_FDB_DYNAMIC_LEARNED,
> };
>
> struct net_bridge_fdb_key {
> @@ -554,6 +555,9 @@ struct net_bridge {
> struct kobject *ifobj;
> u32 auto_cnt;
>
> + atomic_t fdb_n_learned_entries;
> + u32 fdb_max_learned_entries;
> +
> #ifdef CONFIG_NET_SWITCHDEV
> /* Counter used to make sure that hardware domains get unique
> * identifiers in case a bridge spans multiple switchdev instances.
>
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH net-next v3 4/6] net: bridge: Add netlink knobs for number / max learned FDB entries
2023-09-05 11:47 [PATCH net-next v3 0/6] bridge: Add a limit on learned FDB entries Johannes Nixdorf
` (2 preceding siblings ...)
2023-09-05 11:47 ` [PATCH net-next v3 3/6] net: bridge: Track and limit dynamically learned FDB entries Johannes Nixdorf
@ 2023-09-05 11:47 ` Johannes Nixdorf
2023-09-05 11:47 ` [PATCH net-next v3 5/6] net: bridge: Add a configurable default FDB learning limit Johannes Nixdorf
2023-09-05 11:47 ` [PATCH net-next v3 6/6] selftests: forwarding: bridge_fdb_learning_limit: Add a new selftest Johannes Nixdorf
5 siblings, 0 replies; 10+ messages in thread
From: Johannes Nixdorf @ 2023-09-05 11:47 UTC (permalink / raw)
To: David S. Miller, Andrew Lunn, David Ahern, Eric Dumazet,
Florian Fainelli, Ido Schimmel, Jakub Kicinski,
Nikolay Aleksandrov, Oleksij Rempel, Paolo Abeni, Roopa Prabhu,
Shuah Khan, Vladimir Oltean
Cc: bridge, netdev, linux-kernel, linux-kselftest, Johannes Nixdorf
The previous patch added accounting and a limit for the number of
dynamically learned FDB entries per bridge. However it did not provide
means to actually configure those bounds or read back the count. This
patch does that.
Two new netlink attributes are added for the accounting and limit of
dynamically learned FDB entries:
- IFLA_BR_FDB_N_LEARNED_ENTRIES (RO) for the number of entries accounted
for a single bridge.
- IFLA_BR_FDB_MAX_LEARNED_ENTRIES (RW) for the configured limit of
entries for the bridge.
The new attributes are used like this:
# ip link add name br up type bridge fdb_max_learned_entries 256
# ip link add name v1 up master br type veth peer v2
# ip link set up dev v2
# mausezahn -a rand -c 1024 v2
0.01 seconds (90877 packets per second
# bridge fdb | grep -v permanent | wc -l
256
# ip -d link show dev br
13: br: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 [...]
[...] fdb_n_learned_entries 256 fdb_max_learned_entries 256
Signed-off-by: Johannes Nixdorf <jnixdorf-oss@avm.de>
---
include/uapi/linux/if_link.h | 2 ++
net/bridge/br_netlink.c | 15 ++++++++++++++-
2 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/include/uapi/linux/if_link.h b/include/uapi/linux/if_link.h
index 0f6a0fe09bdb..bcdf728cfe98 100644
--- a/include/uapi/linux/if_link.h
+++ b/include/uapi/linux/if_link.h
@@ -510,6 +510,8 @@ enum {
IFLA_BR_VLAN_STATS_PER_PORT,
IFLA_BR_MULTI_BOOLOPT,
IFLA_BR_MCAST_QUERIER_STATE,
+ IFLA_BR_FDB_N_LEARNED_ENTRIES,
+ IFLA_BR_FDB_MAX_LEARNED_ENTRIES,
__IFLA_BR_MAX,
};
diff --git a/net/bridge/br_netlink.c b/net/bridge/br_netlink.c
index 1dc4e1bce740..d8595274879d 100644
--- a/net/bridge/br_netlink.c
+++ b/net/bridge/br_netlink.c
@@ -1254,6 +1254,8 @@ static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
[IFLA_BR_VLAN_STATS_PER_PORT] = { .type = NLA_U8 },
[IFLA_BR_MULTI_BOOLOPT] =
NLA_POLICY_EXACT_LEN(sizeof(struct br_boolopt_multi)),
+ [IFLA_BR_FDB_N_LEARNED_ENTRIES] = { .type = NLA_U32 },
+ [IFLA_BR_FDB_MAX_LEARNED_ENTRIES] = { .type = NLA_U32 },
};
static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
@@ -1528,6 +1530,12 @@ static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
return err;
}
+ if (data[IFLA_BR_FDB_MAX_LEARNED_ENTRIES]) {
+ u32 val = nla_get_u32(data[IFLA_BR_FDB_MAX_LEARNED_ENTRIES]);
+
+ WRITE_ONCE(br->fdb_max_learned_entries, val);
+ }
+
return 0;
}
@@ -1582,6 +1590,8 @@ static size_t br_get_size(const struct net_device *brdev)
nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
+ nla_total_size(sizeof(u32)) + /* IFLA_BR_FDB_N_LEARNED_ENTRIES */
+ nla_total_size(sizeof(u32)) + /* IFLA_BR_FDB_MAX_LEARNED_ENTRIES */
#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
@@ -1657,7 +1667,10 @@ static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
br->topology_change_detected) ||
nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr) ||
- nla_put(skb, IFLA_BR_MULTI_BOOLOPT, sizeof(bm), &bm))
+ nla_put(skb, IFLA_BR_MULTI_BOOLOPT, sizeof(bm), &bm) ||
+ nla_put_u32(skb, IFLA_BR_FDB_N_LEARNED_ENTRIES,
+ atomic_read(&br->fdb_n_learned_entries)) ||
+ nla_put_u32(skb, IFLA_BR_FDB_MAX_LEARNED_ENTRIES, br->fdb_max_learned_entries))
return -EMSGSIZE;
#ifdef CONFIG_BRIDGE_VLAN_FILTERING
--
2.42.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH net-next v3 5/6] net: bridge: Add a configurable default FDB learning limit
2023-09-05 11:47 [PATCH net-next v3 0/6] bridge: Add a limit on learned FDB entries Johannes Nixdorf
` (3 preceding siblings ...)
2023-09-05 11:47 ` [PATCH net-next v3 4/6] net: bridge: Add netlink knobs for number / max " Johannes Nixdorf
@ 2023-09-05 11:47 ` Johannes Nixdorf
2023-09-05 11:47 ` [PATCH net-next v3 6/6] selftests: forwarding: bridge_fdb_learning_limit: Add a new selftest Johannes Nixdorf
5 siblings, 0 replies; 10+ messages in thread
From: Johannes Nixdorf @ 2023-09-05 11:47 UTC (permalink / raw)
To: David S. Miller, Andrew Lunn, David Ahern, Eric Dumazet,
Florian Fainelli, Ido Schimmel, Jakub Kicinski,
Nikolay Aleksandrov, Oleksij Rempel, Paolo Abeni, Roopa Prabhu,
Shuah Khan, Vladimir Oltean
Cc: bridge, netdev, linux-kernel, linux-kselftest, Johannes Nixdorf
Add a Kconfig option to configure a default FDB learning limit system
wide, so a distributor building a special purpose kernel can limit all
created bridges by default.
The limit is only a soft default setting and overrideable on a per bridge
basis using netlink.
Signed-off-by: Johannes Nixdorf <jnixdorf-oss@avm.de>
---
net/bridge/Kconfig | 13 +++++++++++++
net/bridge/br_device.c | 2 ++
2 files changed, 15 insertions(+)
diff --git a/net/bridge/Kconfig b/net/bridge/Kconfig
index 3c8ded7d3e84..c0d9c08088c4 100644
--- a/net/bridge/Kconfig
+++ b/net/bridge/Kconfig
@@ -84,3 +84,16 @@ config BRIDGE_CFM
Say N to exclude this support and reduce the binary size.
If unsure, say N.
+
+config BRIDGE_DEFAULT_FDB_MAX_LEARNED
+ int "Default FDB learning limit"
+ default 0
+ depends on BRIDGE
+ help
+ Sets a default limit on the number of learned FDB entries on
+ new bridges. This limit can be overwritten via netlink on a
+ per bridge basis.
+
+ The default of 0 disables the limit.
+
+ If unsure, say 0.
diff --git a/net/bridge/br_device.c b/net/bridge/br_device.c
index 9a5ea06236bd..8d4221fc5a6c 100644
--- a/net/bridge/br_device.c
+++ b/net/bridge/br_device.c
@@ -531,6 +531,8 @@ void br_dev_setup(struct net_device *dev)
br->bridge_ageing_time = br->ageing_time = BR_DEFAULT_AGEING_TIME;
dev->max_mtu = ETH_MAX_MTU;
+ br->fdb_max_learned_entries = CONFIG_BRIDGE_DEFAULT_FDB_MAX_LEARNED;
+
br_netfilter_rtable_init(br);
br_stp_timer_init(br);
br_multicast_init(br);
--
2.42.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* [PATCH net-next v3 6/6] selftests: forwarding: bridge_fdb_learning_limit: Add a new selftest
2023-09-05 11:47 [PATCH net-next v3 0/6] bridge: Add a limit on learned FDB entries Johannes Nixdorf
` (4 preceding siblings ...)
2023-09-05 11:47 ` [PATCH net-next v3 5/6] net: bridge: Add a configurable default FDB learning limit Johannes Nixdorf
@ 2023-09-05 11:47 ` Johannes Nixdorf
2023-09-05 20:53 ` Jakub Kicinski
5 siblings, 1 reply; 10+ messages in thread
From: Johannes Nixdorf @ 2023-09-05 11:47 UTC (permalink / raw)
To: David S. Miller, Andrew Lunn, David Ahern, Eric Dumazet,
Florian Fainelli, Ido Schimmel, Jakub Kicinski,
Nikolay Aleksandrov, Oleksij Rempel, Paolo Abeni, Roopa Prabhu,
Shuah Khan, Vladimir Oltean
Cc: bridge, netdev, linux-kernel, linux-kselftest, Johannes Nixdorf
Add a suite covering the fdb_n_learned_entries and fdb_max_learned_entries
bridge features, touching all special cases in accounting at least once.
Signed-off-by: Johannes Nixdorf <jnixdorf-oss@avm.de>
---
.../net/forwarding/bridge_fdb_learning_limit.sh | 283 +++++++++++++++++++++
1 file changed, 283 insertions(+)
diff --git a/tools/testing/selftests/net/forwarding/bridge_fdb_learning_limit.sh b/tools/testing/selftests/net/forwarding/bridge_fdb_learning_limit.sh
new file mode 100755
index 000000000000..4da17ac65357
--- /dev/null
+++ b/tools/testing/selftests/net/forwarding/bridge_fdb_learning_limit.sh
@@ -0,0 +1,283 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+
+# ShellCheck incorrectly believes that most of the code here is unreachable
+# because it's invoked by variable name following ALL_TESTS.
+#
+# shellcheck disable=SC2317
+
+ALL_TESTS="check_accounting check_limit"
+NUM_NETIFS=6
+source lib.sh
+
+TEST_MAC_BASE=de:ad:be:ef:42:
+
+NUM_PKTS=16
+FDB_LIMIT=8
+
+FDB_TYPES=(
+ # name is counted? overrides learned?
+ 'learned 1 0'
+ 'static 0 1'
+ 'user 0 1'
+ 'extern_learn 0 1'
+ 'local 0 1'
+)
+
+mac()
+{
+ printf "${TEST_MAC_BASE}%02x" "$1"
+}
+
+H1_DEFAULT_MAC=$(mac 42)
+
+switch_create()
+{
+ ip link add dev br0 type bridge
+
+ ip link set dev "$swp1" master br0
+ ip link set dev "$swp2" master br0
+ # swp3 is used to add local MACs, so do not add it to the bridge yet.
+
+ # swp2 is only used for replying when learning on swp1, its MAC should not be learned.
+ ip link set dev "$swp2" type bridge_slave learning off
+
+ ip link set dev br0 up
+
+ ip link set dev "$swp1" up
+ ip link set dev "$swp2" up
+ ip link set dev "$swp3" up
+}
+
+switch_destroy()
+{
+ ip link set dev "$swp3" down
+ ip link set dev "$swp2" down
+ ip link set dev "$swp1" down
+
+ ip link del dev br0
+}
+
+h_create()
+{
+ ip link set "$h1" addr "$H1_DEFAULT_MAC"
+
+ simple_if_init "$h1" 192.0.2.1/24
+ simple_if_init "$h2" 192.0.2.2/24
+}
+
+h_destroy()
+{
+ simple_if_fini "$h1" 192.0.2.1/24
+ simple_if_fini "$h2" 192.0.2.2/24
+}
+
+setup_prepare()
+{
+ h1=${NETIFS[p1]}
+ swp1=${NETIFS[p2]}
+
+ h2=${NETIFS[p3]}
+ swp2=${NETIFS[p4]}
+
+ swp3=${NETIFS[p6]}
+
+ vrf_prepare
+
+ h_create
+
+ switch_create
+}
+
+cleanup()
+{
+ pre_cleanup
+
+ switch_destroy
+
+ h_destroy
+
+ vrf_cleanup
+}
+
+fdb_get_n_learned()
+{
+ ip -d -j link show dev br0 type bridge | \
+ jq '.[]["linkinfo"]["info_data"]["fdb_n_learned_entries"]'
+}
+
+fdb_get_n_mac()
+{
+ local mac=${1}
+
+ bridge -j fdb show br br0 | \
+ jq "map(select(.mac == \"${mac}\" and (has(\"vlan\") | not))) | length"
+}
+
+fdb_fill_learned()
+{
+ local i
+
+ for i in $(seq 1 "$NUM_PKTS"); do
+ fdb_add learned "$(mac "$i")"
+ done
+}
+
+fdb_reset()
+{
+ bridge fdb flush dev br0
+
+ # Keep the default MAC address of h1 in the table. We set it to a different one when
+ # testing dynamic learning.
+ bridge fdb add "$H1_DEFAULT_MAC" dev "$swp1" master static use
+}
+
+fdb_add()
+{
+ local type=$1 mac=$2
+
+ case "$type" in
+ learned)
+ ip link set "$h1" addr "$mac"
+ # Wait for a reply so we implicitly wait until after the forwarding
+ # code finished and the FDB entry was created.
+ PING_COUNT=1 ping_do "$h1" 192.0.2.2
+ check_err $? "Failed to ping another bridge port"
+ ip link set "$h1" addr "$H1_DEFAULT_MAC"
+ ;;
+ local)
+ ip link set dev "$swp3" addr "$mac" && ip link set "$swp3" master br0
+ ;;
+ static)
+ bridge fdb replace "$mac" dev "$swp1" master static
+ ;;
+ user)
+ bridge fdb replace "$mac" dev "$swp1" master static use
+ ;;
+ extern_learn)
+ bridge fdb replace "$mac" dev "$swp1" master extern_learn
+ ;;
+ esac
+
+ check_err $? "Failed to add a FDB entry of type ${type}"
+}
+
+fdb_del()
+{
+ local type=$1 mac=$2
+
+ case "$type" in
+ local)
+ ip link set "$swp3" nomaster
+ ;;
+ *)
+ bridge fdb del "$mac" dev "$swp1" master
+ ;;
+ esac
+
+ check_err $? "Failed to remove a FDB entry of type ${type}"
+}
+
+check_accounting_one_type()
+{
+ local type=$1 is_counted=$2 overrides_learned=$3
+ shift 3
+ RET=0
+
+ fdb_reset
+ fdb_add "$type" "$(mac 0)"
+ learned=$(fdb_get_n_learned)
+ [ "$learned" -ne "$is_counted" ]
+ check_fail $? "Inserted FDB type ${type}: Expected the count ${is_counted}, but got ${learned}"
+
+ fdb_del "$type" "$(mac 0)"
+ learned=$(fdb_get_n_learned)
+ [ "$learned" -ne 0 ]
+ check_fail $? "Removed FDB type ${type}: Expected the count 0, but got ${learned}"
+
+ if [ "$overrides_learned" -eq 1 ]; then
+ fdb_reset
+ fdb_add learned "$(mac 0)"
+ fdb_add "$type" "$(mac 0)"
+ learned=$(fdb_get_n_learned)
+ [ "$learned" -ne "$is_counted" ]
+ check_fail $? "Set a learned entry to FDB type ${type}: Expected the count ${is_counted}, but got ${learned}"
+ fdb_del "$type" "$(mac 0)"
+ fi
+
+ log_test "FDB accounting interacting with FDB type ${type}"
+}
+
+check_accounting()
+{
+ local type_args learned
+ RET=0
+
+ fdb_reset
+ learned=$(fdb_get_n_learned)
+ [ "$learned" -ne 0 ]
+ check_fail $? "Flushed the FDB table: Expected the count 0, but got ${learned}"
+
+ fdb_fill_learned
+ sleep 1
+
+ learned=$(fdb_get_n_learned)
+ [ "$learned" -ne "$NUM_PKTS" ]
+ check_fail $? "Filled the FDB table: Expected the count ${NUM_PKTS}, but got ${learned}"
+
+ log_test "FDB accounting"
+
+ for type_args in "${FDB_TYPES[@]}"; do
+ # This is intentional use of word splitting.
+ # shellcheck disable=SC2086
+ check_accounting_one_type $type_args
+ done
+}
+
+check_limit_one_type()
+{
+ local type=$1 is_counted=$2
+ local n_mac expected=$((1 - is_counted))
+ RET=0
+
+ fdb_reset
+ fdb_fill_learned
+
+ fdb_add "$type" "$(mac 0)"
+ n_mac=$(fdb_get_n_mac "$(mac 0)")
+ [ "$n_mac" -ne "$expected" ]
+ check_fail $? "Inserted FDB type ${type} at limit: Expected the count ${expected}, but got ${n_mac}"
+
+ log_test "FDB limits interacting with FDB type ${type}"
+}
+
+check_limit()
+{
+ local learned
+ RET=0
+
+ ip link set br0 type bridge fdb_max_learned_entries "$FDB_LIMIT"
+
+ fdb_reset
+ fdb_fill_learned
+
+ learned=$(fdb_get_n_learned)
+ [ "$learned" -ne "$FDB_LIMIT" ]
+ check_fail $? "Filled the limited FDB table: Expected the count ${FDB_LIMIT}, but got ${learned}"
+
+ log_test "FDB limits"
+
+ for type_args in "${FDB_TYPES[@]}"; do
+ # This is intentional use of word splitting.
+ # shellcheck disable=SC2086
+ check_limit_one_type $type_args
+ done
+}
+
+trap cleanup EXIT
+
+setup_prepare
+
+tests_run
+
+exit $EXIT_STATUS
--
2.42.0
^ permalink raw reply related [flat|nested] 10+ messages in thread* Re: [PATCH net-next v3 6/6] selftests: forwarding: bridge_fdb_learning_limit: Add a new selftest
2023-09-05 11:47 ` [PATCH net-next v3 6/6] selftests: forwarding: bridge_fdb_learning_limit: Add a new selftest Johannes Nixdorf
@ 2023-09-05 20:53 ` Jakub Kicinski
0 siblings, 0 replies; 10+ messages in thread
From: Jakub Kicinski @ 2023-09-05 20:53 UTC (permalink / raw)
To: Johannes Nixdorf
Cc: David S. Miller, Andrew Lunn, David Ahern, Eric Dumazet,
Florian Fainelli, Ido Schimmel, Nikolay Aleksandrov,
Oleksij Rempel, Paolo Abeni, Roopa Prabhu, Shuah Khan,
Vladimir Oltean, bridge, netdev, linux-kernel, linux-kselftest
On Tue, 05 Sep 2023 13:47:23 +0200 Johannes Nixdorf wrote:
> Add a suite covering the fdb_n_learned_entries and fdb_max_learned_entries
> bridge features, touching all special cases in accounting at least once.
>
> Signed-off-by: Johannes Nixdorf <jnixdorf-oss@avm.de>
> ---
> .../net/forwarding/bridge_fdb_learning_limit.sh | 283 +++++++++++++++++++++
Please add it to the Makefile so it gets run by automation.
^ permalink raw reply [flat|nested] 10+ messages in thread