* [PATCH net-next v2 1/6] dt-bindings: net: describe the Northstar flow accelerator
2026-12-01 18:22 [PATCH net-next v2 0/6] Add Northstar FA TCP/UDP NAT offload with a bridged LAN Florian Fainelli
@ 2026-12-01 18:22 ` Florian Fainelli
2026-12-01 18:22 ` [PATCH net-next v2 2/6] net: dsa: add Broadcom FA metadata and IPv4 exception paths Florian Fainelli
` (4 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Florian Fainelli @ 2026-12-01 18:22 UTC (permalink / raw)
To: netdev
Cc: Florian Fainelli, andrew+netdev, conor+dt, davem, devicetree,
edumazet, krzk+dt, kuba, linux-kernel, pabeni, robh
Describe the BCM4708/BCM53011/BCM53012 accelerator register resource and
the GMAC2 consumer phandle. The accelerator is inline on switch CPU port
8 (IMP0); GMAC0 and GMAC1 bypass it. Document the additional port 5
(IMP1)/GMAC0 CPU connection for exceptions that cannot safely traverse
the inline lookup. Keep existing boards unchanged unless their topology
explicitly opts in.
The accelerator registers belong to the AXI MAC + flow accelerator block
and sit in the 4 KiB BCMA window of the GMAC3 core, outside of the range
described for the GMAC3 node. BCMA GMAC nodes have no compatible string,
so the consumer schema is selected by the property and restricted to the
GMAC2 node name.
Signed-off-by: Florian Fainelli <florian.fainelli@broadcom.com>
---
Changes in v2:
- Describe the AXI MAC/FA block and the register placement within the
GMAC3 4 KiB BCMA window; clarify that FA has no separate DT clock or
reset input and correct port 5 to IMP1.
- Explain property-based schema selection for BCMA GMAC nodes and
restrict the consumer to GMAC2 (ethernet@26000).
.../bindings/net/brcm,bcm4708-fa.yaml | 52 +++++++++++++++++++
.../bindings/net/brcm,bcm5301x-gmac.yaml | 33 ++++++++++++
MAINTAINERS | 2 +
3 files changed, 87 insertions(+)
create mode 100644 Documentation/devicetree/bindings/net/brcm,bcm4708-fa.yaml
create mode 100644 Documentation/devicetree/bindings/net/brcm,bcm5301x-gmac.yaml
diff --git a/Documentation/devicetree/bindings/net/brcm,bcm4708-fa.yaml b/Documentation/devicetree/bindings/net/brcm,bcm4708-fa.yaml
new file mode 100644
index 0000000000000..69dcfd060589b
--- /dev/null
+++ b/Documentation/devicetree/bindings/net/brcm,bcm4708-fa.yaml
@@ -0,0 +1,52 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/net/brcm,bcm4708-fa.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Broadcom Northstar BCM4708 and BCM5301X flow accelerator
+
+maintainers:
+ - Florian Fainelli <f.fainelli@gmail.com>
+
+description: |
+ The flow accelerator is part of the AXI MAC + flow accelerator block, which
+ also contains the four GMAC cores enumerated by BCMA. It is inline between
+ GMAC2 and switch port 8 (IMP0); GMAC0 and GMAC1 bypass it. The GMAC2 BCMA
+ Ethernet node references this register block through a
+ brcm,flow-accelerator phandle. Miss packets carry a prepended accelerator
+ descriptor in place of the embedded switch tag.
+
+ The registers are in the same 4 KiB BCMA window as the GMAC3 core (the
+ loopback port of the block), above the 0x800 bytes that the GMAC3 node
+ describes. The accelerator is part of that block and has no clock or reset
+ input of its own in the device tree.
+
+ Switch port 5 (IMP1), connected to GMAC0, provides a second CPU path for
+ exceptions that must bypass the accelerator, including TCP control,
+ low-TTL and oversized packets. Describe that CPU port with an ethernet
+ phandle to GMAC0 and an internal, 1 Gbps full-duplex fixed link in the
+ switch's ports node. Port 8 remains the data connection to GMAC2.
+
+properties:
+ compatible:
+ enum:
+ - brcm,bcm4708-fa
+ - brcm,bcm53011-fa
+ - brcm,bcm53012-fa
+
+ reg:
+ maxItems: 1
+
+required:
+ - compatible
+ - reg
+
+additionalProperties: false
+
+examples:
+ - |
+ flow-accelerator@18027c00 {
+ compatible = "brcm,bcm53011-fa";
+ reg = <0x18027c00 0xc0>;
+ };
diff --git a/Documentation/devicetree/bindings/net/brcm,bcm5301x-gmac.yaml b/Documentation/devicetree/bindings/net/brcm,bcm5301x-gmac.yaml
new file mode 100644
index 0000000000000..c560731987bed
--- /dev/null
+++ b/Documentation/devicetree/bindings/net/brcm,bcm5301x-gmac.yaml
@@ -0,0 +1,33 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/net/brcm,bcm5301x-gmac.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Broadcom Northstar BCMA GMAC flow accelerator connection
+
+maintainers:
+ - Florian Fainelli <f.fainelli@gmail.com>
+
+description: |
+ BCMA enumerates the Northstar GMACs, and their nodes have no compatible
+ string. This schema is therefore selected by the brcm,flow-accelerator
+ property and describes the optional inline flow accelerator connection on
+ GMAC2, connected to switch IMP0.
+
+select:
+ required:
+ - brcm,flow-accelerator
+
+properties:
+ $nodename:
+ pattern: "^ethernet@26000$"
+
+ brcm,flow-accelerator:
+ $ref: /schemas/types.yaml#/definitions/phandle
+ description: |
+ Phandle to the BCM4708, BCM53011 or BCM53012 inline flow accelerator.
+ Only GMAC2 (BCMA Ethernet core unit 2, ethernet@26000) has this
+ connection.
+
+additionalProperties: true
diff --git a/MAINTAINERS b/MAINTAINERS
index 56212dbc9a0d0..dce9a88f4aef4 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5669,6 +5669,8 @@ R: Broadcom internal kernel review list <bcm-kernel-feedback-list@broadcom.com>
L: netdev@vger.kernel.org
S: Maintained
F: Documentation/devicetree/bindings/net/brcm,amac.yaml
+F: Documentation/devicetree/bindings/net/brcm,bcm4708-fa.yaml
+F: Documentation/devicetree/bindings/net/brcm,bcm5301x-gmac.yaml
F: drivers/net/ethernet/broadcom/bgmac*
F: drivers/net/ethernet/broadcom/unimac.h
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH net-next v2 2/6] net: dsa: add Broadcom FA metadata and IPv4 exception paths
2026-12-01 18:22 [PATCH net-next v2 0/6] Add Northstar FA TCP/UDP NAT offload with a bridged LAN Florian Fainelli
2026-12-01 18:22 ` [PATCH net-next v2 1/6] dt-bindings: net: describe the Northstar flow accelerator Florian Fainelli
@ 2026-12-01 18:22 ` Florian Fainelli
2026-12-01 18:22 ` [PATCH net-next v2 3/6] net: flow_offload: allow drivers to validate and invalidate cached paths Florian Fainelli
` (3 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Florian Fainelli @ 2026-12-01 18:22 UTC (permalink / raw)
To: netdev
Cc: Florian Fainelli, andrew, davem, edumazet, horms, jonas.gorski,
kuba, olteanv, pabeni, willemb
The original Northstar FA prepends miss metadata which is incompatible
with the ordinary Broadcom switch tag. Add a distinct tag protocol and
receive callback registry for hash learning and exception handling.
Use B53 CFP to redirect TCP control, low-TTL, oversized and wrong-
router- MAC traffic through the auxiliary GMAC0 connection, outside
inline FA. Require complete guards on all user ports and mirror data-CPU
VLAN membership to the auxiliary CPU port. Preserve its flooding policy.
Support a LAN bridge with shared router-MAC and refcounted source-MAC
guards. Trap a continued tuple arriving on another port before FA can
forward it, while preserving ordinary LAN switching. Clear the forwarded
mark on auxiliary redirects so Linux can bridge packets back to wired
members. Publish client guards before NAT entries and release them only
after the last owner has retired; reject uncertain classifier state.
Resynchronize guards after successful and vetoed MAC changes, retire
entries before changing guards, and stop notifiers and deferred work
before switch teardown. Other Broadcom tag protocols retain their opcode
validation and forwarding semantics.
Signed-off-by: Florian Fainelli <florian.fainelli@broadcom.com>
---
Changes in v2:
- Support an untagged LAN bridge with software members opposite a
standalone WAN. Use router-domain guards on all user ports and shared
source-MAC guards for wired clients, preserving ordinary LAN switching
and trapping continued tuples after client movement.
- Clear the forwarded mark on auxiliary classifier redirects. Mirror CPU
VLAN membership, retain the auxiliary flooding policy, and handle MAC-
change failure and notifier teardown.
- Keep the prepended-tag link-local correction in the independent
prerequisites.
drivers/net/dsa/b53/Kconfig | 1 +
drivers/net/dsa/b53/b53_common.c | 516 ++++++++++++++++++++++++++++++-
drivers/net/dsa/b53/b53_priv.h | 16 +
drivers/net/dsa/b53/b53_regs.h | 39 +++
include/linux/dsa/brcm.h | 34 ++
include/net/dsa.h | 2 +
net/dsa/Kconfig | 12 +
net/dsa/tag_brcm.c | 162 +++++++++-
8 files changed, 769 insertions(+), 13 deletions(-)
diff --git a/drivers/net/dsa/b53/Kconfig b/drivers/net/dsa/b53/Kconfig
index 915008e8eff53..8e2a8c20bc4ac 100644
--- a/drivers/net/dsa/b53/Kconfig
+++ b/drivers/net/dsa/b53/Kconfig
@@ -7,6 +7,7 @@ menuconfig B53
select NET_DSA_TAG_BRCM_LEGACY
select NET_DSA_TAG_BRCM_LEGACY_FCS
select NET_DSA_TAG_BRCM_PREPEND
+ select NET_DSA_TAG_BRCM_FA
help
This driver adds support for Broadcom managed switch chips. It supports
BCM5325E, BCM5365, BCM539x, BCM53115 and BCM53125 as well as BCM63XX
diff --git a/drivers/net/dsa/b53/b53_common.c b/drivers/net/dsa/b53/b53_common.c
index b7aff5e238e85..234c6a2905e26 100644
--- a/drivers/net/dsa/b53/b53_common.c
+++ b/drivers/net/dsa/b53/b53_common.c
@@ -17,19 +17,25 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#include <linux/bitfield.h>
#include <linux/delay.h>
+#include <linux/dsa/brcm.h>
#include <linux/export.h>
#include <linux/gpio/consumer.h>
+#include <linux/in.h>
+#include <linux/iopoll.h>
#include <linux/kernel.h>
#include <linux/math.h>
#include <linux/minmax.h>
#include <linux/module.h>
+#include <linux/of.h>
#include <linux/platform_data/b53.h>
#include <linux/phy.h>
#include <linux/phylink.h>
#include <linux/etherdevice.h>
#include <linux/if_bridge.h>
#include <linux/if_vlan.h>
+#include <linux/unaligned.h>
#include <net/dsa.h>
#include "b53_regs.h"
@@ -667,9 +673,10 @@ static void b53_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
int b53_setup_port(struct dsa_switch *ds, int port)
{
struct b53_device *dev = ds->priv;
+ bool auxiliary = dev->fa_cfp_enabled && port == 5;
- b53_port_set_ucast_flood(dev, port, true);
- b53_port_set_mcast_flood(dev, port, true);
+ b53_port_set_ucast_flood(dev, port, !auxiliary);
+ b53_port_set_mcast_flood(dev, port, !auxiliary);
b53_port_set_learning(dev, port, false);
b53_port_set_isolated(dev, port, false);
@@ -678,7 +685,8 @@ int b53_setup_port(struct dsa_switch *ds, int port)
* dropping frames because it isn't allowed to forward there.
*/
if (dsa_is_user_port(ds, port))
- b53_set_eap_mode(dev, port, EAP_MODE_SIMPLIFIED);
+ b53_set_eap_mode(dev, port, dev->fa_cfp_enabled ? EAP_MODE_BASIC :
+ EAP_MODE_SIMPLIFIED);
if (is5325(dev) &&
in_range(port, 1, 4)) {
@@ -728,6 +736,8 @@ int b53_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy)
pvlan &= ~0x1ff;
pvlan |= BIT(port);
pvlan |= dev->ports[port].vlan_ctl_mask;
+ if (dev->fa_cfp_enabled && (dev->fa_cfp_ports & BIT(port)))
+ pvlan |= BIT(5);
b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
b53_imp_vlan_setup(ds, cpu_port);
@@ -1326,6 +1336,391 @@ int b53_setup_devlink_resources(struct dsa_switch *ds)
}
EXPORT_SYMBOL(b53_setup_devlink_resources);
+static int b53_fa_cfp_wait(struct b53_device *dev, u32 mask)
+{
+ u32 val;
+ int ret, err;
+
+ ret = read_poll_timeout(b53_read32, err, err || !(val & mask),
+ 10, 10000, false, dev, B53_CFP_TCAM_PAGE,
+ B53_CFP_ACC, &val);
+ return ret ?: err;
+}
+
+static int b53_fa_cfp_write(struct b53_device *dev, unsigned int index, u32 ram)
+{
+ int err;
+
+ err = b53_write32(dev, B53_CFP_TCAM_PAGE, B53_CFP_ACC,
+ CFP_ACC_INDEX(index) | ram | CFP_ACC_WRITE | CFP_ACC_START);
+ return err ?: b53_fa_cfp_wait(dev, CFP_ACC_START);
+}
+
+#define B53_FA_GUARD_BASE 16
+#define B53_FA_CLIENT_BASE 64
+
+static void b53_fa_udf(u32 data[8], u32 mask[8], unsigned int udf,
+ u16 value, u16 bits)
+{
+ unsigned int bit = CFP_TCAM_UDF_OFFSET + 16 * udf;
+ unsigned int word = bit / 32;
+ unsigned int shift = bit % 32;
+
+ data[word] |= (u32)value << shift;
+ mask[word] |= (u32)bits << shift;
+ if (shift > 16) {
+ data[word + 1] |= value >> (32 - shift);
+ mask[word + 1] |= bits >> (32 - shift);
+ }
+ bit = 5 * 32 + 24 + udf;
+ data[bit / 32] |= BIT(bit % 32);
+ mask[bit / 32] |= BIT(bit % 32);
+}
+
+static int b53_fa_rule(struct b53_device *dev, unsigned int index,
+ const u32 data[8], const u32 mask[8], bool trap)
+{
+ unsigned int i;
+ int err;
+
+ for (i = 0; i < 8; i++) {
+ err = b53_write32(dev, B53_CFP_TCAM_PAGE, B53_CFP_DATA(i), data[i]);
+ if (!err)
+ err = b53_write32(dev, B53_CFP_TCAM_PAGE, B53_CFP_MASK(i), mask[i]);
+ if (err)
+ return err;
+ }
+ err = b53_write32(dev, B53_CFP_TCAM_PAGE, B53_CFP_ACTION(0),
+ trap ? CFP_ACT0_FWD_MAP_REPLACE | CFP_ACT0_DST_PORT5 |
+ CFP_ACT0_REASON_TERMINATION : 0);
+ if (!err)
+ err = b53_write32(dev, B53_CFP_TCAM_PAGE, B53_CFP_ACTION(1),
+ trap ? CFP_ACT1_CLASS_ID(1) | CFP_ACT1_FWD_MAP_REPLACE |
+ CFP_ACT1_DST_PORT5 : 0);
+ if (!err)
+ err = b53_fa_cfp_write(dev, index, CFP_ACC_ACTION);
+ if (!err)
+ err = b53_write32(dev, B53_CFP_TCAM_PAGE, B53_CFP_METER, 0);
+ if (!err)
+ err = b53_write32(dev, B53_CFP_TCAM_PAGE, B53_CFP_METER + 4, 0);
+ if (!err)
+ err = b53_fa_cfp_write(dev, index, CFP_ACC_METER);
+ return err ?: b53_fa_cfp_write(dev, index, CFP_ACC_TCAM);
+}
+
+static const u16 b53_fa_lengths[][2] = {
+ { 1501, 0xffff }, { 1502, 0xfffe }, { 1504, 0xffe0 },
+ { 1536, 0xfe00 }, { 2048, 0xf800 }, { 4096, 0xf000 },
+ { 8192, 0xe000 }, { 16384, 0xc000 }, { 32768, 0x8000 },
+};
+
+static int b53_fa_guards(struct b53_device *dev)
+{
+ u32 data[8] = {}, mask[8] = {};
+ unsigned int i;
+ int err;
+
+ /* UDF A1: IPv4 TTL/protocol; A2: IPv4 total length. */
+ err = b53_write8(dev, B53_CFP_PAGE, B53_CFP_UDF1_A0 + 1,
+ CFP_UDF_BASE_END_L2 | CFP_UDF_OFFSET(4));
+ if (!err)
+ err = b53_write8(dev, B53_CFP_PAGE, B53_CFP_UDF1_A0 + 2,
+ CFP_UDF_BASE_END_L2 | CFP_UDF_OFFSET(1));
+ for (i = 0; !err && i < 3; i++)
+ err = b53_write8(dev, B53_CFP_PAGE, B53_CFP_UDF1_A0 + 3 + i, i);
+ if (err)
+ return err;
+ data[0] = CFP_TCAM0_VALID | CFP_TCAM0_SLICE1;
+ mask[0] = CFP_TCAM0_VALID | CFP_TCAM0_SLICE_MASK;
+ mask[6] = CFP_TCAM6_L3_FRAMING; /* IPv4, including options and fragments. */
+ b53_fa_udf(data, mask, 1, 0, 0xfe00); /* TTL 0 or 1. */
+ err = b53_fa_rule(dev, 3, data, mask, true);
+ for (i = 0; !err && i < ARRAY_SIZE(b53_fa_lengths); i++) {
+ memset(data, 0, sizeof(data));
+ memset(mask, 0, sizeof(mask));
+ data[0] = CFP_TCAM0_VALID | CFP_TCAM0_SLICE1;
+ mask[0] = CFP_TCAM0_VALID | CFP_TCAM0_SLICE_MASK;
+ mask[6] = CFP_TCAM6_L3_FRAMING;
+ b53_fa_udf(data, mask, 2, b53_fa_lengths[i][0], b53_fa_lengths[i][1]);
+ err = b53_fa_rule(dev, 4 + i, data, mask, true);
+ }
+ if (err)
+ return err;
+ /* UDF A6..A8: source MAC for active bridge-client move guards. */
+ for (i = 0; !err && i < 3; i++)
+ err = b53_write8(dev, B53_CFP_PAGE, B53_CFP_UDF1_A0 + 6 + i, 3 + i);
+ return err;
+}
+
+/* Original FA ignores switch exceptions on its inline port. Redirect IPv4
+ * exceptions through GMAC0/IMP1, outside FA, while GMAC2/IMP0 carries data.
+ * Reserve classifier rules only when the board describes both CPU paths.
+ */
+static int b53_fa_setup(struct b53_device *dev)
+{
+ struct dsa_port *dp;
+ unsigned int rule;
+ u32 data[8], mask[8];
+ int err;
+
+ if (!is5301x(dev) || dev->tag_protocol != DSA_TAG_PROTO_BRCM_FA ||
+ !dsa_is_cpu_port(dev->ds, 5) || !dsa_is_cpu_port(dev->ds, 8))
+ return 0;
+
+ err = b53_write16(dev, B53_CFP_PAGE, B53_CFP_CTRL, 0);
+ if (err)
+ return err;
+ err = b53_write32(dev, B53_CFP_TCAM_PAGE, B53_CFP_ACC, CFP_ACC_RESET);
+ if (!err)
+ err = b53_fa_cfp_wait(dev, CFP_ACC_RESET);
+ if (err)
+ return err;
+ /* Slice 1 UDF A0 extracts TCP bytes 12..13, relative to the parsed end of L3.
+ * This also works with IP options. Match the individual
+ * FIN/SYN/RST bits, never payload bytes in UDP packets. Slice 0
+ * with a nonzero class ID would request a chained search instead.
+ */
+ err = b53_write8(dev, B53_CFP_PAGE, B53_CFP_UDF1_A0,
+ CFP_UDF_BASE_END_L3 | CFP_UDF_OFFSET(6));
+ if (err)
+ return err;
+ for (rule = 0; rule < 3; rule++) {
+ memset(data, 0, sizeof(data));
+ memset(mask, 0, sizeof(mask));
+ data[0] = CFP_TCAM0_VALID | CFP_TCAM0_SLICE1 | CFP_TCAM0_UDF0_FLAG(rule);
+ /* Slice 1; FIN, SYN or RST. */
+ mask[0] = CFP_TCAM0_VALID | CFP_TCAM0_SLICE_MASK | CFP_TCAM0_UDF0_FLAG(rule);
+ data[5] = CFP_TCAM5_UDF_VALID(0); /* UDF A0 valid. */
+ mask[5] = CFP_TCAM5_UDF_VALID(0);
+ data[6] = FIELD_PREP(CFP_TCAM6_IP_PROTOCOL, IPPROTO_TCP);
+ mask[6] = CFP_TCAM6_L3_FRAMING | CFP_TCAM6_IP_PROTOCOL | CFP_TCAM6_IP_FRAGMENT;
+ /* Both meter outcomes redirect to IMP1, with class 1 and a
+ * termination reason. Preserve VLAN, EAP and STP filtering.
+ */
+ err = b53_fa_rule(dev, rule, data, mask, true);
+ if (err)
+ return err;
+ }
+ err = b53_fa_guards(dev);
+ if (err)
+ return err;
+ /* Unicast and multicast flooding must reach only the data CPU. The
+ * RX_xCST_EN bits only exist in the port 8 IMP control register.
+ */
+ b53_port_set_ucast_flood(dev, 5, false);
+ b53_port_set_mcast_flood(dev, 5, false);
+ dev->fa_cfp_ports = 0;
+ dsa_switch_for_each_user_port(dp, dev->ds)
+ dev->fa_cfp_ports |= BIT(dp->index);
+ dev->fa_user_ports = dev->fa_cfp_ports;
+ /* The conduit state callback enables these rules after GMAC0 is up. */
+ err = b53_write16(dev, B53_CFP_PAGE, B53_CFP_CTRL, 0);
+ if (!err)
+ dev->fa_cfp_enabled = true;
+ return err;
+}
+
+static bool b53_fa_ready(struct b53_device *dev, bool operational)
+{
+ /* Every user port must have its exception rules: the FA key has no input
+ * port, so a port without them could inject into a live tuple.
+ */
+ return operational && READ_ONCE(dev->fa_guards_ready) &&
+ !READ_ONCE(dev->fa_guards_failed) &&
+ dev->fa_cfp_ports == dev->fa_user_ports;
+}
+
+static void b53_fa_retire(struct b53_device *dev)
+{
+ struct dsa_port *data = dsa_to_port(dev->ds, B53_CPU_PORT);
+
+ /* Retire offloads before removing any of the exception paths. */
+ if (data && data->conduit)
+ brcm_fa_control_set_ready(data->conduit, false);
+}
+
+/* Apply the readiness of the exception paths: retire first, then enable the
+ * rules and only then allow new entries. Failing to update the hardware keeps
+ * FA retired.
+ */
+static int b53_fa_update(struct b53_device *dev, bool operational)
+{
+ bool ready = b53_fa_ready(dev, operational);
+ struct dsa_port *data;
+ int err;
+
+ b53_fa_retire(dev);
+ err = b53_write16(dev, B53_CFP_PAGE, B53_CFP_CTRL,
+ ready ? dev->fa_cfp_ports : 0);
+ if (err) {
+ WRITE_ONCE(dev->fa_guards_failed, true);
+ dev_err(dev->dev, "failed to update FA exception path: %d\n", err);
+ return err;
+ }
+ data = dsa_to_port(dev->ds, B53_CPU_PORT);
+ if (ready && data && data->conduit)
+ brcm_fa_control_set_ready(data->conduit, true);
+ return 0;
+}
+
+static int b53_fa_port(struct b53_device *dev)
+{
+ int err;
+
+ if (!dev->fa_cfp_enabled)
+ return 0;
+ WRITE_ONCE(dev->fa_guards_ready, false);
+ err = b53_fa_update(dev, false);
+ /* Membership changes finish after the switch callback, under RTNL. */
+ if (READ_ONCE(dev->fa_notifier_registered) && !READ_ONCE(dev->fa_stopping))
+ schedule_work(&dev->fa_resync_work);
+ return err;
+}
+
+static void b53_fa_conduit_state_change(struct dsa_switch *ds,
+ const struct net_device *conduit, bool operational)
+{
+ struct b53_device *dev = ds->priv;
+ struct dsa_port *control;
+
+ if (!dev->fa_cfp_enabled)
+ return;
+ control = dsa_to_port(ds, 5);
+ if (conduit != control->conduit)
+ return;
+ b53_fa_update(dev, operational);
+}
+
+/* Called under RTNL, after the bridge membership/MAC transaction. */
+static const u8 *b53_fa_router_mac(struct dsa_port *dp)
+{
+ struct net_device *upper = netdev_master_upper_dev_get(dp->user);
+
+ return upper && netif_is_bridge_master(upper) ? upper->dev_addr : dp->user->dev_addr;
+}
+
+static int b53_fa_update_guard(struct b53_device *dev, struct dsa_port *dp)
+{
+ const u8 *addr = b53_fa_router_mac(dp);
+ u32 data[8] = {}, mask[8] = {};
+ struct dsa_port *other;
+ unsigned int i;
+
+ data[0] = CFP_TCAM0_VALID | CFP_TCAM0_SLICE1;
+ mask[0] = CFP_TCAM0_VALID | CFP_TCAM0_SLICE_MASK;
+ mask[6] = CFP_TCAM6_L3_FRAMING;
+ /* Source ports are one-hot. Match this router MAC outside its own
+ * L3 domain, allowing all ports of the same LAN bridge to share it.
+ * Other destination MACs cannot hit FA's router-MAC pool and retain
+ * ordinary bridge switching, including copies to software ports.
+ */
+ dsa_switch_for_each_user_port(other, dev->ds)
+ if (other->user && ether_addr_equal(addr, b53_fa_router_mac(other)))
+ mask[7] |= BIT(other->index);
+ for (i = 0; i < 3; i++)
+ b53_fa_udf(data, mask, 3 + i, get_unaligned_be16(addr + 2 * i), 0xffff);
+ return b53_fa_rule(dev, B53_FA_GUARD_BASE + dp->index, data, mask, true);
+}
+
+static int b53_fa_client_guard(void *priv, unsigned int slot, const u8 *addr,
+ unsigned int port)
+{
+ struct b53_device *dev = priv;
+ u32 data[8] = {}, mask[8] = {};
+ unsigned int i;
+ int err;
+
+ if (addr) {
+ if (port >= 8 || !dsa_is_user_port(dev->ds, port))
+ return -EINVAL;
+ data[0] = CFP_TCAM0_VALID | CFP_TCAM0_SLICE1;
+ mask[0] = CFP_TCAM0_VALID | CFP_TCAM0_SLICE_MASK;
+ mask[6] = CFP_TCAM6_L3_FRAMING;
+ /* A client appearing on any other input must reach bridge
+ * learning before it can hit an entry with the old egress port.
+ */
+ mask[7] = BIT(port);
+ for (i = 0; i < 3; i++)
+ b53_fa_udf(data, mask, 6 + i, get_unaligned_be16(addr + 2 * i), 0xffff);
+ }
+ err = b53_fa_rule(dev, B53_FA_CLIENT_BASE + slot, data, mask, true);
+ if (err)
+ WRITE_ONCE(dev->fa_guards_failed, true);
+ return err;
+}
+
+/* Rewrite all router-domain guards from the current MAC addresses. This also restores
+ * the rules after a MAC address change which did not complete.
+ */
+static void b53_fa_resync_work(struct work_struct *work)
+{
+ struct b53_device *dev = container_of(work, struct b53_device, fa_resync_work);
+ struct dsa_port *control, *dp;
+
+ rtnl_lock();
+ if (!READ_ONCE(dev->fa_stopping)) {
+ b53_fa_retire(dev);
+ control = dsa_to_port(dev->ds, 5);
+ dsa_switch_for_each_user_port(dp, dev->ds) {
+ if (dp->index >= 8 || !dp->user)
+ continue;
+ if (b53_fa_update_guard(dev, dp)) {
+ WRITE_ONCE(dev->fa_guards_failed, true);
+ dev_err(dev->dev, "failed to update FA ingress guard\n");
+ }
+ }
+ WRITE_ONCE(dev->fa_guards_ready, true);
+ b53_fa_update(dev, dsa_port_conduit_is_operational(control));
+ }
+ rtnl_unlock();
+}
+
+static int b53_fa_netdev_event(struct notifier_block *nb, unsigned long event, void *ptr)
+{
+ struct b53_device *dev = container_of(nb, struct b53_device, fa_netdev_nb);
+ struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
+ struct dsa_port *dp;
+
+ if (READ_ONCE(dev->fa_stopping) ||
+ (event != NETDEV_REGISTER && event != NETDEV_UP &&
+ event != NETDEV_PRE_CHANGEADDR && event != NETDEV_CHANGEADDR &&
+ event != NETDEV_PRECHANGEUPPER && event != NETDEV_CHANGEUPPER))
+ return NOTIFY_DONE;
+ if (!netif_is_bridge_master(netdev)) {
+ if (!dsa_user_dev_check(netdev))
+ return NOTIFY_DONE;
+ dp = dsa_port_from_netdev(netdev);
+ if (dp->ds != dev->ds || dp->index >= 8)
+ return NOTIFY_DONE;
+ }
+ /* A vetoed MAC change may have no completion event. Resynchronize
+ * after RTNL is released, using whichever addresses actually remain.
+ */
+ b53_fa_port(dev);
+ return NOTIFY_DONE;
+}
+
+/* Stop tracking user devices and disable the exception paths. Must not be
+ * called with RTNL held.
+ */
+void b53_fa_stop(struct b53_device *dev)
+{
+ if (!dev->fa_notifier_registered && !dev->fa_switch_registered)
+ return;
+ WRITE_ONCE(dev->fa_stopping, true);
+ if (dev->fa_notifier_registered)
+ unregister_netdevice_notifier(&dev->fa_netdev_nb);
+ cancel_work_sync(&dev->fa_resync_work);
+ dev->fa_notifier_registered = false;
+ b53_fa_retire(dev);
+ if (dev->fa_switch_registered) {
+ brcm_fa_switch_unregister(&dev->fa_switch);
+ dev->fa_switch_registered = false;
+ }
+ b53_write16(dev, B53_CFP_PAGE, B53_CFP_CTRL, 0);
+}
+EXPORT_SYMBOL(b53_fa_stop);
+
static int b53_setup(struct dsa_switch *ds)
{
struct b53_device *dev = ds->priv;
@@ -1391,11 +1786,20 @@ static int b53_setup(struct dsa_switch *ds)
if (ret)
return ret;
+ ret = b53_fa_setup(dev);
+ if (ret)
+ return ret;
+
return b53_setup_devlink_resources(ds);
}
static void b53_teardown(struct dsa_switch *ds)
{
+ struct b53_device *dev = ds->priv;
+
+ WRITE_ONCE(dev->fa_stopping, true);
+ if (dev->fa_cfp_enabled)
+ b53_write16(dev, B53_CFP_PAGE, B53_CFP_CTRL, 0);
dsa_devlink_resources_unregister(ds);
}
@@ -1745,8 +2149,12 @@ int b53_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering,
struct netlink_ext_ack *extack)
{
struct b53_device *dev = ds->priv;
+ int err;
if (dev->vlan_filtering != vlan_filtering) {
+ err = b53_fa_port(dev);
+ if (err)
+ return err;
dev->vlan_filtering = vlan_filtering;
b53_apply_config(dev);
}
@@ -1813,6 +2221,13 @@ int b53_vlan_add(struct dsa_switch *ds, int port,
vl->untag |= BIT(port);
else
vl->untag &= ~BIT(port);
+ /* CFP redirects to the auxiliary CPU port, which VLAN filtering still
+ * applies to: keep it a tagged member wherever the data CPU port is.
+ */
+ if (dev->fa_cfp_enabled && port == B53_CPU_PORT) {
+ vl->members |= BIT(5);
+ vl->untag &= ~BIT(5);
+ }
if (!dev->vlan_filtering)
return 0;
@@ -1846,6 +2261,8 @@ int b53_vlan_del(struct dsa_switch *ds, int port,
vl = &dev->vlans[vlan->vid];
vl->members &= ~BIT(port);
+ if (dev->fa_cfp_enabled && port == B53_CPU_PORT)
+ vl->members &= ~BIT(5);
if (pvid == vlan->vid)
pvid = b53_default_pvid(dev);
@@ -2332,6 +2749,7 @@ int b53_br_join(struct dsa_switch *ds, int port, struct dsa_bridge bridge,
s8 cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
u16 pvlan, reg, pvid;
unsigned int i;
+ int err;
/* On 7278, port 7 which connects to the ASP should only receive
* traffic from matching CFP rules.
@@ -2339,6 +2757,10 @@ int b53_br_join(struct dsa_switch *ds, int port, struct dsa_bridge bridge,
if (dev->chip_id == BCM7278_DEVICE_ID && port == 7)
return -EINVAL;
+ err = b53_fa_port(dev);
+ if (err)
+ return err;
+
pvid = b53_default_pvid(dev);
vl = &dev->vlans[pvid];
@@ -2416,9 +2838,14 @@ void b53_br_leave(struct dsa_switch *ds, int port, struct dsa_bridge bridge)
pvlan &= ~BIT(i);
}
- /* Enable redirection of unknown SA to the CPU port */
- b53_set_eap_mode(dev, port, EAP_MODE_SIMPLIFIED);
+ /* Enable redirection of unknown SA to the CPU port, except with FA
+ * where CFP classifies standalone traffic instead.
+ */
+ b53_set_eap_mode(dev, port, dev->fa_cfp_enabled ? EAP_MODE_BASIC :
+ EAP_MODE_SIMPLIFIED);
+ if (dev->fa_cfp_enabled)
+ pvlan |= BIT(5);
b53_write16(dev, B53_PVLAN_PAGE, B53_PVLAN_PORT_MASK(port), pvlan);
dev->ports[port].vlan_ctl_mask = pvlan;
@@ -2439,6 +2866,11 @@ void b53_br_leave(struct dsa_switch *ds, int port, struct dsa_bridge bridge)
vl->members |= BIT(port);
b53_set_vlan_entry(dev, pvid, vl);
}
+ if (b53_fa_port(dev)) {
+ /* fa_cfp_ports records the port, and FA stays disabled. */
+ dev_err(ds->dev, "failed to restore FA classification on port %d\n", port);
+ b53_disable_port(ds, port);
+ }
}
EXPORT_SYMBOL(b53_br_leave);
@@ -2552,6 +2984,7 @@ static bool b53_can_enable_brcm_tags(struct dsa_switch *ds, int port,
switch (tag_protocol) {
case DSA_TAG_PROTO_BRCM:
case DSA_TAG_PROTO_BRCM_PREPEND:
+ case DSA_TAG_PROTO_BRCM_FA:
dev_warn(ds->dev,
"Port %d is stacked to Broadcom tag switch\n", port);
ret = false;
@@ -2564,6 +2997,47 @@ static bool b53_can_enable_brcm_tags(struct dsa_switch *ds, int port,
return ret;
}
+static bool b53_uses_fa_tag(struct dsa_switch *ds, int port)
+{
+ struct device_node *ports, *node, *ethernet = NULL, *fa = NULL;
+ struct b53_device *dev = ds->priv;
+ bool enabled = false;
+ u32 index;
+
+ if (port != B53_CPU_PORT)
+ return false;
+ /* CPU5 may be parsed before CPU8 has a dsa_port firmware node. */
+ ports = of_get_child_by_name(ds->dev->of_node, "ports");
+ if (!ports)
+ ports = of_get_child_by_name(ds->dev->of_node, "ethernet-ports");
+ for_each_available_child_of_node(ports, node) {
+ if (of_property_read_u32(node, "reg", &index) || index != B53_CPU_PORT)
+ continue;
+ ethernet = of_parse_phandle(node, "ethernet", 0);
+ fa = of_parse_phandle(ethernet, "brcm,flow-accelerator", 0);
+ enabled = IS_ENABLED(CONFIG_BCM_NS_FA) && is5301x(dev) &&
+ of_device_is_available(fa) &&
+ (of_device_is_compatible(fa, "brcm,bcm4708-fa") ||
+ of_device_is_compatible(fa, "brcm,bcm53011-fa") ||
+ of_device_is_compatible(fa, "brcm,bcm53012-fa"));
+ of_node_put(node);
+ break;
+ }
+ of_node_put(ports);
+ of_node_put(fa);
+ of_node_put(ethernet);
+ return enabled;
+}
+
+static struct dsa_port *b53_preferred_cpu_port(struct dsa_switch *ds)
+{
+ struct b53_device *dev = ds->priv;
+
+ if (is5301x(dev) && dsa_is_cpu_port(ds, B53_CPU_PORT) && b53_uses_fa_tag(ds, B53_CPU_PORT))
+ return dsa_to_port(ds, B53_CPU_PORT);
+ return NULL;
+}
+
enum dsa_tag_protocol b53_get_tag_protocol(struct dsa_switch *ds, int port,
enum dsa_tag_protocol mprot)
{
@@ -2583,6 +3057,11 @@ enum dsa_tag_protocol b53_get_tag_protocol(struct dsa_switch *ds, int port,
goto out;
}
+ if (b53_uses_fa_tag(ds, B53_CPU_PORT)) {
+ dev->tag_protocol = DSA_TAG_PROTO_BRCM_FA;
+ goto out;
+ }
+
/* Broadcom BCM58xx chips have a flow accelerator on Port 8
* which requires us to use the prepended Broadcom tag type
*/
@@ -2763,6 +3242,8 @@ static const struct phylink_mac_ops b53_phylink_mac_ops = {
static const struct dsa_switch_ops b53_switch_ops = {
.get_tag_protocol = b53_get_tag_protocol,
+ .preferred_default_local_cpu_port = b53_preferred_cpu_port,
+ .conduit_state_change = b53_fa_conduit_state_change,
.setup = b53_setup,
.teardown = b53_teardown,
.get_strings = b53_get_strings,
@@ -3385,7 +3866,30 @@ int b53_switch_register(struct b53_device *dev)
dev_info(dev->dev, "found switch: %s, rev %i\n",
dev->name, dev->core_rev);
- return dsa_register_switch(dev->ds);
+ ret = dsa_register_switch(dev->ds);
+ if (ret || !dev->fa_cfp_enabled)
+ return ret;
+ /* Registration replays existing user devices, after their MACs and
+ * DSA private data are initialized. Keep lookup gated until it finishes.
+ */
+ dev->fa_netdev_nb.notifier_call = b53_fa_netdev_event;
+ INIT_WORK(&dev->fa_resync_work, b53_fa_resync_work);
+ dev->fa_switch.conduit = dsa_to_port(dev->ds, 8)->conduit;
+ dev->fa_switch.set_guard = b53_fa_client_guard;
+ dev->fa_switch.priv = dev;
+ brcm_fa_switch_register(&dev->fa_switch);
+ dev->fa_switch_registered = true;
+ ret = register_netdevice_notifier(&dev->fa_netdev_nb);
+ if (ret)
+ goto unregister_switch;
+ dev->fa_notifier_registered = true;
+ /* The worker takes RTNL, so all replayed devices are fully visible. */
+ schedule_work(&dev->fa_resync_work);
+ return 0;
+unregister_switch:
+ b53_fa_stop(dev);
+ dsa_unregister_switch(dev->ds);
+ return ret;
}
EXPORT_SYMBOL(b53_switch_register);
diff --git a/drivers/net/dsa/b53/b53_priv.h b/drivers/net/dsa/b53/b53_priv.h
index c4d2a41c714f6..2a2769b15d1b0 100644
--- a/drivers/net/dsa/b53/b53_priv.h
+++ b/drivers/net/dsa/b53/b53_priv.h
@@ -19,6 +19,7 @@
#ifndef __B53_PRIV_H
#define __B53_PRIV_H
+#include <linux/dsa/brcm.h>
#include <linux/kernel.h>
#include <linux/mutex.h>
#include <linux/phylink.h>
@@ -153,6 +154,17 @@ struct b53_device {
u8 num_arl_bins;
u16 num_arl_buckets;
enum dsa_tag_protocol tag_protocol;
+ u16 fa_cfp_ports;
+ u16 fa_user_ports;
+ bool fa_cfp_enabled;
+ bool fa_guards_ready;
+ bool fa_guards_failed;
+ bool fa_stopping;
+ bool fa_notifier_registered;
+ bool fa_switch_registered;
+ struct work_struct fa_resync_work;
+ struct notifier_block fa_netdev_nb;
+ struct brcm_fa_switch fa_switch;
/* used ports mask */
u16 enabled_ports;
@@ -285,13 +297,17 @@ int b53_switch_detect(struct b53_device *dev);
int b53_switch_register(struct b53_device *dev);
+void b53_fa_stop(struct b53_device *dev);
+
static inline void b53_switch_remove(struct b53_device *dev)
{
+ b53_fa_stop(dev);
dsa_unregister_switch(dev->ds);
}
static inline void b53_switch_shutdown(struct b53_device *dev)
{
+ b53_fa_stop(dev);
dsa_switch_shutdown(dev->ds);
}
diff --git a/drivers/net/dsa/b53/b53_regs.h b/drivers/net/dsa/b53/b53_regs.h
index cfdb5ed383260..4ca7ea64efc6c 100644
--- a/drivers/net/dsa/b53/b53_regs.h
+++ b/drivers/net/dsa/b53/b53_regs.h
@@ -614,4 +614,43 @@
/* CFP Control Register with ports map (8 bit) */
#define B53_CFP_CTRL 0x00
+/* Northstar CFP indirect table port. */
+#define B53_CFP_TCAM_PAGE 0xa0
+#define B53_CFP_ACC 0x00
+#define CFP_ACC_START BIT(0)
+#define CFP_ACC_WRITE (2 << 1)
+#define CFP_ACC_TCAM BIT(10)
+#define CFP_ACC_ACTION (2 << 10)
+#define CFP_ACC_METER (4 << 10)
+#define CFP_ACC_RESET BIT(15)
+#define CFP_ACC_INDEX(n) ((n) << 16)
+#define B53_CFP_DATA(n) (0x10 + (n) * 4)
+#define B53_CFP_MASK(n) (0x30 + (n) * 4)
+#define B53_CFP_ACTION(n) (0x50 + (n) * 4)
+#define B53_CFP_METER 0x60
+#define B53_CFP_UDF1_A0 0x20
+#define CFP_UDF_BASE_END_L2 (2 << 5)
+#define CFP_UDF_BASE_END_L3 (3 << 5)
+#define CFP_UDF_OFFSET(n) (n)
+
+/* Slice 1 IPv4 key fields, indexed by DATA/MASK word. */
+#define CFP_TCAM0_VALID GENMASK(1, 0)
+#define CFP_TCAM0_SLICE1 BIT(2)
+#define CFP_TCAM0_SLICE_MASK GENMASK(3, 2)
+#define CFP_TCAM0_UDF0_FLAG(n) BIT(8 + (n))
+#define CFP_TCAM_UDF_OFFSET 8
+#define CFP_TCAM5_UDF_VALID(n) BIT(24 + (n))
+#define CFP_TCAM6_L3_FRAMING GENMASK(25, 24)
+#define CFP_TCAM6_IP_PROTOCOL GENMASK(15, 8)
+#define CFP_TCAM6_IP_FRAGMENT BIT(7)
+#define CFP_TCAM7_SOURCE_PORTS GENMASK(7, 0)
+
+/* Both meter outcomes replace the destination map with CPU port 5. */
+#define CFP_ACT0_FWD_MAP_REPLACE (2 << 24)
+#define CFP_ACT0_DST_PORT5 BIT(19)
+#define CFP_ACT0_REASON_TERMINATION BIT(6)
+#define CFP_ACT1_CLASS_ID(n) ((n) << 20)
+#define CFP_ACT1_FWD_MAP_REPLACE (2 << 11)
+#define CFP_ACT1_DST_PORT5 BIT(6)
+
#endif /* !__B53_REGS_H */
diff --git a/include/linux/dsa/brcm.h b/include/linux/dsa/brcm.h
index 47545a9487840..3a99d0a80920f 100644
--- a/include/linux/dsa/brcm.h
+++ b/include/linux/dsa/brcm.h
@@ -8,6 +8,40 @@
#ifndef _NET_DSA_BRCM_H
#define _NET_DSA_BRCM_H
+#include <linux/list.h>
+#include <linux/types.h>
+
+struct net_device;
+struct sk_buff;
+
+#define BRCM_FA_BRIDGE_GUARDS 128
+
+/* Guard callbacks run in process context. Unregister waits for callers. */
+struct brcm_fa_switch {
+ struct list_head list;
+ struct net_device *conduit;
+ int (*set_guard)(void *priv, unsigned int slot, const u8 *addr, unsigned int port);
+ void *priv;
+};
+
+void brcm_fa_switch_register(struct brcm_fa_switch *sw);
+void brcm_fa_switch_unregister(struct brcm_fa_switch *sw);
+int brcm_fa_bridge_guard(struct net_device *conduit, unsigned int slot,
+ const u8 *addr, unsigned int port);
+
+/* Original FA control packets arrive through the auxiliary CPU port. */
+struct brcm_fa_control {
+ struct list_head list;
+ struct net_device *conduit;
+ void (*receive)(void *priv, struct sk_buff *skb, unsigned int port);
+ void (*state_change)(void *priv, bool ready);
+ void *priv;
+};
+
+void brcm_fa_control_register(struct brcm_fa_control *control);
+void brcm_fa_control_unregister(struct brcm_fa_control *control);
+void brcm_fa_control_set_ready(struct net_device *conduit, bool ready);
+
/* Broadcom tag specific helpers to insert and extract queue/port number */
#define BRCM_TAG_SET_PORT_QUEUE(p, q) ((p) << 8 | q)
#define BRCM_TAG_GET_PORT(v) ((v) >> 8)
diff --git a/include/net/dsa.h b/include/net/dsa.h
index 5d12191b6f6f5..5f926b7fde5bb 100644
--- a/include/net/dsa.h
+++ b/include/net/dsa.h
@@ -62,6 +62,7 @@ struct tc_action;
#define DSA_TAG_PROTO_KSZ8463_VALUE 34
#define DSA_TAG_PROTO_MT7628_VALUE 35
#define DSA_TAG_PROTO_KS8995_VALUE 36
+#define DSA_TAG_PROTO_BRCM_FA_VALUE 37
enum dsa_tag_protocol {
DSA_TAG_PROTO_NONE = DSA_TAG_PROTO_NONE_VALUE,
@@ -69,6 +70,7 @@ enum dsa_tag_protocol {
DSA_TAG_PROTO_BRCM_LEGACY = DSA_TAG_PROTO_BRCM_LEGACY_VALUE,
DSA_TAG_PROTO_BRCM_LEGACY_FCS = DSA_TAG_PROTO_BRCM_LEGACY_FCS_VALUE,
DSA_TAG_PROTO_BRCM_PREPEND = DSA_TAG_PROTO_BRCM_PREPEND_VALUE,
+ DSA_TAG_PROTO_BRCM_FA = DSA_TAG_PROTO_BRCM_FA_VALUE,
DSA_TAG_PROTO_DSA = DSA_TAG_PROTO_DSA_VALUE,
DSA_TAG_PROTO_EDSA = DSA_TAG_PROTO_EDSA_VALUE,
DSA_TAG_PROTO_GSWIP = DSA_TAG_PROTO_GSWIP_VALUE,
diff --git a/net/dsa/Kconfig b/net/dsa/Kconfig
index 4f44bf3ede238..d62de6f716cd9 100644
--- a/net/dsa/Kconfig
+++ b/net/dsa/Kconfig
@@ -69,6 +69,18 @@ config NET_DSA_TAG_BRCM_PREPEND
Broadcom switches which places the tag before the Ethernet header
(prepended).
+config NET_DSA_TAG_BRCM_FA
+ tristate "Tag driver for the Broadcom Northstar flow accelerator"
+ select NET_DSA_TAG_BRCM_COMMON
+ help
+ Support the prepended four-byte FA metadata tag on Northstar GMAC2,
+ which carries the data of switch port 8. The receive format includes
+ a hash, bucket, parser status, and input switch port. The auxiliary
+ GMAC0 connection to switch port 5 uses the ordinary Broadcom tag,
+ embedded after the source address, in both directions.
+ The B53 driver selects this tagger and uses it only for a switch
+ explicitly connected to an accelerator.
+
config NET_DSA_TAG_HELLCREEK
tristate "Tag driver for Hirschmann Hellcreek TSN switches"
help
diff --git a/net/dsa/tag_brcm.c b/net/dsa/tag_brcm.c
index b58669c921474..df1e43f8b11a9 100644
--- a/net/dsa/tag_brcm.c
+++ b/net/dsa/tag_brcm.c
@@ -9,6 +9,7 @@
#include <linux/etherdevice.h>
#include <linux/if_vlan.h>
#include <linux/list.h>
+#include <linux/mutex.h>
#include <linux/slab.h>
#include "tag.h"
@@ -17,6 +18,8 @@
#define BRCM_LEGACY_NAME "brcm-legacy"
#define BRCM_LEGACY_FCS_NAME "brcm-legacy-fcs"
#define BRCM_PREPEND_NAME "brcm-prepend"
+#define BRCM_FA_NAME "brcm-fa"
+#define BRCM_FA_OPCODE_MISS 2
/* Legacy Broadcom tag (6 bytes) */
#define BRCM_LEG_TAG_LEN 6
@@ -84,7 +87,8 @@
#define BRCM_EG_PID_MASK 0x1f
#if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM) || \
- IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_PREPEND)
+ IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_PREPEND) || \
+ IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_FA)
static struct sk_buff *brcm_tag_xmit_ll(struct sk_buff *skb,
struct net_device *dev,
@@ -146,10 +150,11 @@ static struct sk_buff *brcm_tag_xmit_ll(struct sk_buff *skb,
*/
static struct sk_buff *brcm_tag_rcv_ll(struct sk_buff *skb,
struct net_device *dev,
- unsigned int offset)
+ unsigned int offset, bool fa)
{
int source_port;
const u8 *dest;
+ u8 opcode;
u8 *brcm_tag;
if (unlikely(!pskb_may_pull(skb, BRCM_TAG_LEN))) {
@@ -159,8 +164,12 @@ static struct sk_buff *brcm_tag_rcv_ll(struct sk_buff *skb,
brcm_tag = skb->data - offset;
- /* The opcode should never be different than 0b000 */
- if (unlikely((brcm_tag[0] >> BRCM_OPCODE_SHIFT) & BRCM_OPCODE_MASK)) {
+ /* Opcode 2 is a four-byte FA miss descriptor. Its middle bytes
+ * contain the flow hash and parser status, not classification/reason.
+ * Timestamp descriptors (opcode 1, eight bytes) are not supported.
+ */
+ opcode = (brcm_tag[0] >> BRCM_OPCODE_SHIFT) & BRCM_OPCODE_MASK;
+ if (unlikely(opcode && !(fa && opcode == BRCM_FA_OPCODE_MISS))) {
kfree_skb(skb);
return NULL;
}
@@ -168,7 +177,7 @@ static struct sk_buff *brcm_tag_rcv_ll(struct sk_buff *skb,
/* We should never see a reserved reason code without knowing how to
* handle it
*/
- if (unlikely(brcm_tag[2] & BRCM_EG_RC_RSVD)) {
+ if (unlikely(!opcode && (brcm_tag[2] & BRCM_EG_RC_RSVD))) {
kfree_skb(skb);
return NULL;
}
@@ -210,7 +219,7 @@ static struct sk_buff *brcm_tag_rcv(struct sk_buff *skb, struct net_device *dev)
struct sk_buff *nskb;
/* skb->data points to the EtherType, the tag is right before it */
- nskb = brcm_tag_rcv_ll(skb, dev, 2);
+ nskb = brcm_tag_rcv_ll(skb, dev, 2, false);
if (!nskb)
return nskb;
@@ -397,7 +406,7 @@ static struct sk_buff *brcm_tag_rcv_prepend(struct sk_buff *skb,
struct net_device *dev)
{
/* tag is prepended to the packet */
- return brcm_tag_rcv_ll(skb, dev, ETH_HLEN);
+ return brcm_tag_rcv_ll(skb, dev, ETH_HLEN, false);
}
static const struct dsa_device_ops brcm_prepend_netdev_ops = {
@@ -412,7 +421,146 @@ DSA_TAG_DRIVER(brcm_prepend_netdev_ops);
MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM_PREPEND, BRCM_PREPEND_NAME);
#endif
+#if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_FA)
+static LIST_HEAD(brcm_fa_controls);
+static DEFINE_MUTEX(brcm_fa_control_lock);
+
+static LIST_HEAD(brcm_fa_switches);
+static DEFINE_MUTEX(brcm_fa_switch_lock);
+
+void brcm_fa_switch_register(struct brcm_fa_switch *sw)
+{
+ mutex_lock(&brcm_fa_switch_lock);
+ list_add(&sw->list, &brcm_fa_switches);
+ mutex_unlock(&brcm_fa_switch_lock);
+}
+EXPORT_SYMBOL_GPL(brcm_fa_switch_register);
+
+void brcm_fa_switch_unregister(struct brcm_fa_switch *sw)
+{
+ mutex_lock(&brcm_fa_switch_lock);
+ list_del(&sw->list);
+ mutex_unlock(&brcm_fa_switch_lock);
+}
+EXPORT_SYMBOL_GPL(brcm_fa_switch_unregister);
+
+int brcm_fa_bridge_guard(struct net_device *conduit, unsigned int slot,
+ const u8 *addr, unsigned int port)
+{
+ struct brcm_fa_switch *sw;
+ int err = -ENODEV;
+
+ if (slot >= BRCM_FA_BRIDGE_GUARDS)
+ return -EINVAL;
+ /* Separate from the control lock: retiring the data path can release
+ * guards while a control-state callback holds that lock.
+ */
+ mutex_lock(&brcm_fa_switch_lock);
+ list_for_each_entry(sw, &brcm_fa_switches, list) {
+ if (sw->conduit != conduit)
+ continue;
+ err = sw->set_guard(sw->priv, slot, addr, port);
+ break;
+ }
+ mutex_unlock(&brcm_fa_switch_lock);
+ return err;
+}
+EXPORT_SYMBOL_GPL(brcm_fa_bridge_guard);
+
+void brcm_fa_control_register(struct brcm_fa_control *control)
+{
+ mutex_lock(&brcm_fa_control_lock);
+ list_add_rcu(&control->list, &brcm_fa_controls);
+ mutex_unlock(&brcm_fa_control_lock);
+}
+EXPORT_SYMBOL_GPL(brcm_fa_control_register);
+
+void brcm_fa_control_unregister(struct brcm_fa_control *control)
+{
+ mutex_lock(&brcm_fa_control_lock);
+ list_del_rcu(&control->list);
+ mutex_unlock(&brcm_fa_control_lock);
+ synchronize_net();
+}
+EXPORT_SYMBOL_GPL(brcm_fa_control_unregister);
+
+void brcm_fa_control_set_ready(struct net_device *conduit, bool ready)
+{
+ struct brcm_fa_control *control;
+
+ /* State callbacks can sleep while retiring hardware entries. */
+ mutex_lock(&brcm_fa_control_lock);
+ list_for_each_entry(control, &brcm_fa_controls, list)
+ if (control->conduit == conduit && control->state_change)
+ control->state_change(control->priv, ready);
+ mutex_unlock(&brcm_fa_control_lock);
+}
+EXPORT_SYMBOL_GPL(brcm_fa_control_set_ready);
+
+static struct sk_buff *brcm_fa_tag_xmit(struct sk_buff *skb,
+ struct net_device *dev)
+{
+ struct dsa_port *dp = dsa_user_to_port(dev);
+
+ return brcm_tag_xmit_ll(skb, dev, dp->cpu_dp->index == 5 ? 2 * ETH_ALEN : 0);
+}
+
+static struct sk_buff *brcm_fa_tag_rcv(struct sk_buff *skb, struct net_device *dev)
+{
+ struct dsa_port *cpu = dev->dsa_ptr;
+ struct brcm_fa_control *control;
+ struct dsa_port *data_cpu;
+ unsigned int port;
+ u8 reason;
+
+ if (cpu->index != 5)
+ return brcm_tag_rcv_ll(skb, dev, ETH_HLEN, true);
+ if (!pskb_may_pull(skb, BRCM_TAG_LEN))
+ goto drop;
+ reason = skb->data[0]; /* Embedded tag byte 2 after eth_type_trans(). */
+ port = skb->data[1] & BRCM_EG_PID_MASK;
+ skb = brcm_tag_rcv_ll(skb, dev, 2, false);
+ if (!skb)
+ return NULL;
+ dsa_strip_etype_header(skb, BRCM_TAG_LEN);
+ skb_set_mac_header(skb, -ETH_HLEN);
+ if (!(reason & BRCM_EG_RC_PROT_TERM))
+ return skb;
+ /* CFP replaced the complete forwarding map with the auxiliary CPU.
+ * Linux must forward a bridged exception to wired ports as well.
+ */
+ skb->offload_fwd_mark = 0;
+ data_cpu = dsa_to_port(cpu->ds, 8);
+ if (!data_cpu || !dsa_port_is_cpu(data_cpu))
+ return skb;
+ rcu_read_lock();
+ list_for_each_entry_rcu(control, &brcm_fa_controls, list)
+ if (control->conduit == data_cpu->conduit)
+ control->receive(control->priv, skb, port);
+ rcu_read_unlock();
+ return skb;
+drop:
+ kfree_skb(skb);
+ return NULL;
+}
+
+static const struct dsa_device_ops brcm_fa_netdev_ops = {
+ .name = BRCM_FA_NAME,
+ .proto = DSA_TAG_PROTO_BRCM_FA,
+ .xmit = brcm_fa_tag_xmit,
+ .rcv = brcm_fa_tag_rcv,
+ .needed_headroom = BRCM_TAG_LEN,
+ .promisc_on_conduit = true,
+};
+
+DSA_TAG_DRIVER(brcm_fa_netdev_ops);
+MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_BRCM_FA, BRCM_FA_NAME);
+#endif
+
static struct dsa_tag_driver *dsa_tag_driver_array[] = {
+#if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_FA)
+ &DSA_TAG_DRIVER_NAME(brcm_fa_netdev_ops),
+#endif
#if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM)
&DSA_TAG_DRIVER_NAME(brcm_netdev_ops),
#endif
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH net-next v2 3/6] net: flow_offload: allow drivers to validate and invalidate cached paths
2026-12-01 18:22 [PATCH net-next v2 0/6] Add Northstar FA TCP/UDP NAT offload with a bridged LAN Florian Fainelli
2026-12-01 18:22 ` [PATCH net-next v2 1/6] dt-bindings: net: describe the Northstar flow accelerator Florian Fainelli
2026-12-01 18:22 ` [PATCH net-next v2 2/6] net: dsa: add Broadcom FA metadata and IPv4 exception paths Florian Fainelli
@ 2026-12-01 18:22 ` Florian Fainelli
2026-12-01 18:22 ` [PATCH net-next v2 4/6] net: bgmac: add Northstar IPv4 TCP/UDP NAT flowtable offload Florian Fainelli
` (2 subsequent siblings)
5 siblings, 0 replies; 7+ messages in thread
From: Florian Fainelli @ 2026-12-01 18:22 UTC (permalink / raw)
To: netdev
Cc: Florian Fainelli, bridge, coreteam, davem, edumazet, fw, horms,
idosch, kuba, netfilter-devel, pabeni, pablo, phil, razor,
willemb
A classifier can lack the mark, DSCP and output-interface context used
to select its forwarding path. Provide optional owner validation and
invalidation callbacks so an accelerator can validate the actual cached
routes and Ethernet peers instead of treating a partial FIB lookup as
proof that a software path is stale.
Retain direct-path dst references for validating hardware consumers.
Mark references held only for that purpose so packet lookup and GC keep
their existing route-validation policy. Validate without creating a
neighbour, and report missing or unsupported context as unavailable. For
untagged bridge paths, resolve the bridge neighbour through the FDB and
compare the physical transmit port. Return the reverse cached peer MAC
when requested, so ingress guards use the owner's authoritative peer.
Allow the bridge STP accessor under either RTNL or RCU for flow workers.
Notify forwarding caches before FDB movement or deletion and after
publication, including administrative dynamic entries hidden from
switchdev. Track a generation per network namespace for flows traversing
a bridge, sampled before path construction. Reject stale construction,
packet lookup, GC and hardware admission. This also retires software-
only and hardware-capacity fallback owners after a client moves, without
walking flowtables while bridge learning holds its locks. Non-bridge
flows keep their existing semantics.
Invalidation marks the owning connection for teardown. Owners retain the
cookie through an RCU grace period after DESTROY; drivers stop calling
back when their rules are destroyed. Conntrack ownership remains held
until asynchronous deletion has completed.
Signed-off-by: Florian Fainelli <florian.fainelli@broadcom.com>
---
Changes in v2:
- Validate the owner's cached route and neighbour without allocating
entries; missing context is unavailable validation rather than proof of
a stale software path.
- Resolve untagged bridge neighbours through the FDB and return the
authoritative reverse peer MAC for ingress guards. Permit the bridge
STP accessor under RCU as well as RTNL.
- Notify forwarding caches around FDB mutation and publication. Track a
per-network-namespace generation for bridge paths so software-only and
capacity-fallback owners also retire on moves, deletion and aging.
Preserve non-bridge cache semantics.
include/net/flow_offload.h | 14 +++
include/net/netevent.h | 1 +
include/net/netfilter/nf_flow_table.h | 13 ++-
include/net/netns/flow_table.h | 3 +
net/bridge/br_fdb.c | 26 ++++-
net/bridge/br_stp.c | 7 +-
net/netfilter/nf_flow_table_core.c | 57 ++++++++++-
net/netfilter/nf_flow_table_offload.c | 139 ++++++++++++++++++++++++++
net/netfilter/nf_flow_table_path.c | 8 ++
net/netfilter/nft_flow_offload.c | 6 +-
10 files changed, 257 insertions(+), 17 deletions(-)
diff --git a/include/net/flow_offload.h b/include/net/flow_offload.h
index 70a02ee143080..ab5c0677debb4 100644
--- a/include/net/flow_offload.h
+++ b/include/net/flow_offload.h
@@ -697,6 +697,20 @@ struct flow_cls_offload {
bool use_act_stats;
unsigned long cookie;
struct flow_rule *rule;
+ /* Optional owner notification when a rule no longer represents the path.
+ * Call under RCU; the owner retains the cookie and callback until a
+ * grace period after DESTROY. Drivers must stop new calls at DESTROY.
+ * The callback must not sleep or call back into the offload driver.
+ */
+ void (*invalidate)(unsigned long cookie);
+ /* Optional cached-path validation, called under RCU during REPLACE.
+ * Return 0 for a valid path, -ESTALE for a demonstrably stale path,
+ * or -EOPNOTSUPP when validation is unavailable. Do not sleep or
+ * call back into the driver. The callback does not invalidate its owner.
+ * If ingress_mac is non-NULL, also return the peer MAC from the cached
+ * reverse Ethernet path, or -EOPNOTSUPP if it is unavailable.
+ */
+ int (*validate)(unsigned long cookie, u8 *ingress_mac);
struct flow_stats stats;
u32 classid;
};
diff --git a/include/net/netevent.h b/include/net/netevent.h
index 1be3757a8b7f0..68258accc9eab 100644
--- a/include/net/netevent.h
+++ b/include/net/netevent.h
@@ -30,6 +30,7 @@ enum netevent_notif_type {
NETEVENT_IPV4_MPATH_HASH_UPDATE, /* arg is struct net ptr */
NETEVENT_IPV6_MPATH_HASH_UPDATE, /* arg is struct net ptr */
NETEVENT_IPV4_FWD_UPDATE_PRIORITY_UPDATE, /* arg is struct net ptr */
+ NETEVENT_BRIDGE_FDB_UPDATE, /* arg is the bridge net_device ptr */
};
int register_netevent_notifier(struct notifier_block *nb);
diff --git a/include/net/netfilter/nf_flow_table.h b/include/net/netfilter/nf_flow_table.h
index 5b22d31cb1407..c9dcd9bc8d3d3 100644
--- a/include/net/netfilter/nf_flow_table.h
+++ b/include/net/netfilter/nf_flow_table.h
@@ -154,7 +154,8 @@ struct flow_offload_tuple {
encap_num:2,
needs_gso_segment:1,
tun_num:2,
- in_vlan_ingress:2;
+ in_vlan_ingress:2,
+ dst_validation_only:1;
u16 mtu;
u32 dst_cookie;
struct dst_entry *dst_cache;
@@ -190,6 +191,7 @@ enum nf_flow_flags {
NF_FLOW_HW_DEAD,
NF_FLOW_HW_BIDIRECTIONAL,
NF_FLOW_HW_ESTABLISHED,
+ NF_FLOW_BRIDGE,
};
enum flow_offload_type {
@@ -203,6 +205,7 @@ struct flow_offload {
unsigned long flags;
u16 type;
u32 timeout;
+ u32 bridge_genid;
struct rcu_head rcu_head;
};
@@ -210,6 +213,7 @@ struct flow_offload {
#define nf_flowtable_time_stamp (u32)jiffies
unsigned long flow_offload_get_timeout(struct flow_offload *flow);
+bool nf_flow_bridge_stale(const struct flow_offload *flow);
static inline __s32 nf_flow_timeout_delta(unsigned int timeout)
{
@@ -217,6 +221,8 @@ static inline __s32 nf_flow_timeout_delta(unsigned int timeout)
}
struct nf_flow_route {
+ u32 bridge_genid;
+ bool uses_bridge;
struct {
struct dst_entry *dst;
struct {
@@ -305,7 +311,7 @@ nf_flow_table_offload_del_cb(struct nf_flowtable *flow_table,
}
void flow_offload_route_init(struct flow_offload *flow,
- struct nf_flow_route *route);
+ struct nf_flow_route *route, bool keep_dst);
int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow);
void flow_offload_refresh(struct nf_flowtable *flow_table,
@@ -313,7 +319,8 @@ void flow_offload_refresh(struct nf_flowtable *flow_table,
static inline bool nf_flow_dst_check(struct flow_offload_tuple *tuple)
{
- if (!tuple->dst_cache)
+ /* A driver-only reference must not change direct-path/GC semantics. */
+ if (!tuple->dst_cache || tuple->dst_validation_only)
return true;
return dst_check(tuple->dst_cache, tuple->dst_cookie);
diff --git a/include/net/netns/flow_table.h b/include/net/netns/flow_table.h
index 1c5fc657e2675..b286b23116ac1 100644
--- a/include/net/netns/flow_table.h
+++ b/include/net/netns/flow_table.h
@@ -2,6 +2,8 @@
#ifndef __NETNS_FLOW_TABLE_H
#define __NETNS_FLOW_TABLE_H
+#include <linux/atomic.h>
+
struct nf_flow_table_stat {
unsigned int count_wq_add;
unsigned int count_wq_del;
@@ -10,5 +12,6 @@ struct nf_flow_table_stat {
struct netns_ft {
struct nf_flow_table_stat __percpu *stat;
+ atomic_t bridge_genid;
};
#endif
diff --git a/net/bridge/br_fdb.c b/net/bridge/br_fdb.c
index b601245ad9239..922c34fdfecd4 100644
--- a/net/bridge/br_fdb.c
+++ b/net/bridge/br_fdb.c
@@ -20,6 +20,7 @@
#include <linux/atomic.h>
#include <linux/unaligned.h>
#include <linux/if_vlan.h>
+#include <net/netevent.h>
#include <net/switchdev.h>
#include <trace/events/bridge.h>
#include "br_private.h"
@@ -174,6 +175,15 @@ static inline size_t fdb_nlmsg_size(void)
+ nla_total_size(sizeof(u8)); /* NFEA_ACTIVITY_NOTIFY */
}
+/* Forwarding caches must also observe FDB changes which are intentionally
+ * hidden from switchdev, such as software-only administrative entries.
+ * Notify before changing an existing destination and after publication.
+ */
+static void fdb_notify_cache(struct net_bridge *br)
+{
+ call_netevent_notifiers(NETEVENT_BRIDGE_FDB_UPDATE, br->dev);
+}
+
static void fdb_notify(struct net_bridge *br,
const struct net_bridge_fdb_entry *fdb, int type,
bool swdev_notify)
@@ -182,6 +192,7 @@ static void fdb_notify(struct net_bridge *br,
struct sk_buff *skb;
int err = -ENOBUFS;
+ fdb_notify_cache(br);
if (swdev_notify)
br_switchdev_fdb_notify(br, fdb, type);
@@ -315,6 +326,7 @@ static void fdb_del_hw_addr(struct net_bridge *br, const unsigned char *addr)
static void fdb_delete(struct net_bridge *br, struct net_bridge_fdb_entry *f,
bool swdev_notify)
{
+ fdb_notify_cache(br);
trace_fdb_delete(br, f, br_fdb_dst_port(f));
if (test_bit(BR_FDB_STATIC, &f->flags))
@@ -350,8 +362,10 @@ static void fdb_delete_local(struct net_bridge *br,
vg = nbp_vlan_group(op);
if (op != p && ether_addr_equal(op->dev->dev_addr, addr) &&
(!vid || br_vlan_find(vg, vid))) {
+ fdb_notify_cache(br);
br_fdb_dst_write(f, br_port_to_dst(op));
clear_bit(BR_FDB_ADDED_BY_USER, &f->flags);
+ fdb_notify_cache(br);
return;
}
}
@@ -361,8 +375,10 @@ static void fdb_delete_local(struct net_bridge *br,
/* Maybe bridge device has same hw addr? */
if (p && ether_addr_equal(br->dev->dev_addr, addr) &&
(!vid || (v && br_vlan_should_use(v)))) {
+ fdb_notify_cache(br);
br_fdb_dst_write(f, br_port_to_dst(NULL));
clear_bit(BR_FDB_ADDED_BY_USER, &f->flags);
+ fdb_notify_cache(br);
return;
}
@@ -409,6 +425,7 @@ static struct net_bridge_fdb_entry *fdb_create(struct net_bridge *br,
return NULL;
memcpy(fdb->key.addr.addr, addr, ETH_ALEN);
+ fdb_notify_cache(br);
br_fdb_dst_write(fdb, dst);
fdb->key.vlan_id = vid;
fdb->flags = flags;
@@ -1030,6 +1047,7 @@ static void __fdb_update(struct net_bridge *br,
!test_bit(BR_FDB_STICKY, &fdb->flags)))) {
if (source != old_port) {
br_switchdev_fdb_notify(br, fdb, RTM_DELNEIGH);
+ fdb_notify_cache(br);
br_fdb_dst_write(fdb, dst);
fdb_modified = true;
/* take over HW learned entry */
@@ -1194,7 +1212,7 @@ static bool fdb_handle_notify(struct net_bridge_fdb_entry *fdb, u8 notify)
return modified;
}
-static bool fdb_update_dst(struct net_bridge_fdb_entry *fdb,
+static bool fdb_update_dst(struct net_bridge *br, struct net_bridge_fdb_entry *fdb,
struct net_bridge_dst old_dst,
struct net_bridge_dst dst)
{
@@ -1205,7 +1223,7 @@ static bool fdb_update_dst(struct net_bridge_fdb_entry *fdb,
br_fdb_dst_replace(fdb, old_dst, dst);
return false;
}
-
+ fdb_notify_cache(br);
br_fdb_dst_write(fdb, dst);
return true;
}
@@ -1263,7 +1281,7 @@ static int fdb_add_entry(struct net_bridge *br, struct net_bridge_dst dst,
return -EEXIST;
old_dst = br_fdb_dst_read(fdb);
- modified = fdb_update_dst(fdb, old_dst, dst);
+ modified = fdb_update_dst(br, fdb, old_dst, dst);
set_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
if (test_and_clear_bit(BR_FDB_DYNAMIC_LEARNED, &fdb->flags))
@@ -1644,7 +1662,7 @@ int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
WRITE_ONCE(fdb->updated, jiffies);
- modified = fdb_update_dst(fdb, old_dst, dst);
+ modified = fdb_update_dst(br, fdb, old_dst, dst);
if (test_and_set_bit(BR_FDB_ADDED_BY_EXT_LEARN, &fdb->flags)) {
/* Refresh entry */
diff --git a/net/bridge/br_stp.c b/net/bridge/br_stp.c
index c7e7e924f1557..5717bf33c2667 100644
--- a/net/bridge/br_stp.c
+++ b/net/bridge/br_stp.c
@@ -70,17 +70,16 @@ void br_set_state(struct net_bridge_port *p, unsigned int state)
}
}
+/* Caller holds RTNL or RCU. */
u8 br_port_get_stp_state(const struct net_device *dev)
{
struct net_bridge_port *p;
- ASSERT_RTNL();
-
- p = br_port_get_rtnl(dev);
+ p = br_port_get_rtnl_rcu(dev);
if (!p)
return BR_STATE_DISABLED;
- return p->state;
+ return READ_ONCE(p->state);
}
EXPORT_SYMBOL_GPL(br_port_get_stp_state);
diff --git a/net/netfilter/nf_flow_table_core.c b/net/netfilter/nf_flow_table_core.c
index 0efa55de878ee..b5cb465542831 100644
--- a/net/netfilter/nf_flow_table_core.c
+++ b/net/netfilter/nf_flow_table_core.c
@@ -7,6 +7,7 @@
#include <linux/netdevice.h>
#include <net/ip.h>
#include <net/ip6_route.h>
+#include <net/netevent.h>
#include <net/netfilter/nf_tables.h>
#include <net/netfilter/nf_flow_table.h>
#include <net/netfilter/nf_conntrack.h>
@@ -96,7 +97,7 @@ static struct dst_entry *nft_route_dst_fetch(struct nf_flow_route *route,
static int flow_offload_fill_route(struct flow_offload *flow,
struct nf_flow_route *route,
- enum flow_offload_tuple_dir dir)
+ enum flow_offload_tuple_dir dir, bool keep_dst)
{
struct flow_offload_tuple *flow_tuple = &flow->tuplehash[dir].tuple;
struct dst_entry *dst = nft_route_dst_fetch(route, dir);
@@ -127,7 +128,9 @@ static int flow_offload_fill_route(struct flow_offload *flow,
switch (route->tuple[dir].xmit_type) {
case FLOW_OFFLOAD_XMIT_DIRECT:
- if (route->tuple[!dir].in.num_tuns) {
+ flow_tuple->dst_validation_only = keep_dst &&
+ !route->tuple[!dir].in.num_tuns;
+ if (keep_dst || route->tuple[!dir].in.num_tuns) {
flow_tuple->dst_cache = dst;
flow_tuple->dst_cookie =
flow_offload_dst_cookie(flow_tuple);
@@ -162,14 +165,27 @@ static void nft_flow_dst_release(struct flow_offload *flow,
}
void flow_offload_route_init(struct flow_offload *flow,
- struct nf_flow_route *route)
+ struct nf_flow_route *route, bool keep_dst)
{
- flow_offload_fill_route(flow, route, FLOW_OFFLOAD_DIR_ORIGINAL);
- flow_offload_fill_route(flow, route, FLOW_OFFLOAD_DIR_REPLY);
+ flow_offload_fill_route(flow, route, FLOW_OFFLOAD_DIR_ORIGINAL, keep_dst);
+ flow_offload_fill_route(flow, route, FLOW_OFFLOAD_DIR_REPLY, keep_dst);
flow->type = NF_FLOW_OFFLOAD_ROUTE;
+ if (route->uses_bridge) {
+ flow->bridge_genid = route->bridge_genid;
+ __set_bit(NF_FLOW_BRIDGE, &flow->flags);
+ }
}
EXPORT_SYMBOL_GPL(flow_offload_route_init);
+bool nf_flow_bridge_stale(const struct flow_offload *flow)
+{
+ if (!test_bit(NF_FLOW_BRIDGE, &flow->flags))
+ return false;
+
+ return flow->bridge_genid !=
+ (u32)atomic_read(&nf_ct_net(flow->ct)->ft.bridge_genid);
+}
+
static inline bool nf_flow_has_expired(const struct flow_offload *flow)
{
return nf_flow_timeout_delta(flow->timeout) <= 0;
@@ -336,6 +352,9 @@ int flow_offload_add(struct nf_flowtable *flow_table, struct flow_offload *flow)
{
int err;
+ if (nf_flow_bridge_stale(flow))
+ return -ESTALE;
+
flow->timeout = nf_flowtable_time_stamp + flow_offload_get_timeout(flow);
err = rhashtable_insert_fast(&flow_table->rhashtable,
@@ -425,6 +444,11 @@ flow_offload_lookup(struct nf_flowtable *flow_table,
if (test_bit(NF_FLOW_TEARDOWN, &flow->flags))
return NULL;
+ if (unlikely(nf_flow_bridge_stale(flow))) {
+ flow_offload_teardown(flow);
+ return NULL;
+ }
+
if (unlikely(nf_ct_is_dying(flow->ct)))
return NULL;
@@ -595,6 +619,7 @@ static void nf_flow_offload_gc_step(struct nf_flowtable *flow_table,
if (nf_flow_has_expired(flow) ||
nf_ct_is_dying(flow->ct) ||
+ nf_flow_bridge_stale(flow) ||
!nf_flow_dst_check(&flow->tuplehash[FLOW_OFFLOAD_DIR_ORIGINAL].tuple) ||
!nf_flow_dst_check(&flow->tuplehash[FLOW_OFFLOAD_DIR_REPLY].tuple) ||
nf_flow_custom_gc(flow_table, flow)) {
@@ -798,6 +823,7 @@ EXPORT_SYMBOL_GPL(nf_flow_table_free);
static int nf_flow_table_init_net(struct net *net)
{
+ atomic_set(&net->ft.bridge_genid, 0);
net->ft.stat = alloc_percpu(struct nf_flow_table_stat);
return net->ft.stat ? 0 : -ENOMEM;
}
@@ -841,6 +867,24 @@ static struct pernet_operations nf_flow_table_net_ops = {
.exit_batch = nf_flow_table_pernet_exit,
};
+static int nf_flow_table_netevent(struct notifier_block *nb,
+ unsigned long event, void *ptr)
+{
+ struct net_device *dev = ptr;
+
+ if (event == NETEVENT_BRIDGE_FDB_UPDATE) {
+ /* Order the notification against the FDB mutation on either side.
+ * Do not walk flowtables here: learning can hold the bridge lock.
+ */
+ atomic_inc_return(&dev_net(dev)->ft.bridge_genid);
+ }
+ return NOTIFY_DONE;
+}
+
+static struct notifier_block nf_flow_table_netevent_notifier = {
+ .notifier_call = nf_flow_table_netevent,
+};
+
static int __init nf_flow_table_module_init(void)
{
int ret;
@@ -861,6 +905,8 @@ static int __init nf_flow_table_module_init(void)
if (ret)
goto out_bpf;
+ register_netevent_notifier(&nf_flow_table_netevent_notifier);
+
return 0;
out_bpf:
@@ -874,6 +920,7 @@ static int __init nf_flow_table_module_init(void)
static void __exit nf_flow_table_module_exit(void)
{
+ unregister_netevent_notifier(&nf_flow_table_netevent_notifier);
rcu_barrier();
nf_flow_table_offload_exit();
unregister_pernet_subsys(&nf_flow_table_net_ops);
diff --git a/net/netfilter/nf_flow_table_offload.c b/net/netfilter/nf_flow_table_offload.c
index 4365859220e6a..daf42db2b31cf 100644
--- a/net/netfilter/nf_flow_table_offload.c
+++ b/net/netfilter/nf_flow_table_offload.c
@@ -7,7 +7,10 @@
#include <linux/netdevice.h>
#include <linux/tc_act/tc_csum.h>
#include <net/flow_offload.h>
+#include <kunit/static_stub.h>
+#include <net/arp.h>
#include <net/ip_tunnels.h>
+#include <net/route.h>
#include <net/netfilter/nf_flow_table.h>
#include <net/netfilter/nf_tables.h>
#include <net/netfilter/nf_conntrack.h>
@@ -885,6 +888,140 @@ static int nf_flow_offload_alloc(const struct flow_offload_work *offload,
return 0;
}
+static struct flow_offload *nf_flow_offload_cookie_flow(unsigned long cookie)
+{
+ struct flow_offload_tuple *tuple = (struct flow_offload_tuple *)cookie;
+ struct flow_offload_tuple_rhash *tuplehash;
+ struct flow_offload *flow;
+
+ tuplehash = container_of(tuple, struct flow_offload_tuple_rhash, tuple);
+ flow = container_of(tuplehash, struct flow_offload, tuplehash[tuple->dir]);
+ return flow;
+}
+
+static void nf_flow_offload_invalidate(unsigned long cookie)
+{
+ flow_offload_teardown(nf_flow_offload_cookie_flow(cookie));
+}
+
+/* Look up the next hop of a cached IPv4 route without creating an entry:
+ * a neighbour which was garbage collected does not make the path stale.
+ * Must be called under RCU.
+ */
+static struct neighbour *nf_flow_offload_neigh(const struct dst_entry *dst,
+ struct net_device *dev,
+ const struct flow_offload_tuple *other)
+{
+ KUNIT_STATIC_STUB_REDIRECT(nf_flow_offload_neigh, dst, dev, other);
+ return __ipv4_neigh_lookup_noref(dev,
+ (__force u32)rt_nexthop(dst_rtable(dst),
+ other->src_v4.s_addr));
+}
+
+/* A bridge route retains its L3 neighbour on the bridge, while the direct
+ * transmit cache names a bridge port. Resolve that same neighbour through the
+ * current FDB instead of treating the different devices as unavailable context.
+ */
+static int nf_flow_offload_bridge_path(const struct flow_offload_tuple *tuple,
+ struct net_device *dev, const u8 *mac)
+{
+ struct net_device_path_ctx ctx = {
+ .dev = dev,
+ .ether_type = htons(ETH_P_IP),
+ };
+ struct net_device_path_stack stack;
+ const struct net_device_path *path;
+ int err;
+
+ if (!netif_is_bridge_master(dev))
+ return -EOPNOTSUPP;
+ ether_addr_copy(ctx.daddr, mac);
+ if (dev_fill_forward_path(&ctx, &stack) < 0)
+ return -ESTALE;
+ err = -EOPNOTSUPP;
+ path = &stack.path[0];
+ if (stack.num_paths != 2 || path->type != DEV_PATH_BRIDGE ||
+ path->bridge.vlan_mode != DEV_PATH_BR_VLAN_KEEP || path->bridge.vlan_id)
+ goto out;
+ path = &stack.path[1];
+ if (path->type != DEV_PATH_ETHERNET && path->type != DEV_PATH_DSA)
+ goto out;
+ err = path->dev->ifindex == tuple->out.ifidx &&
+ ether_addr_equal(dev->dev_addr, tuple->out.h_source) ? 0 : -ESTALE;
+out:
+ dev_fill_forward_path_release(&stack);
+ return err;
+}
+
+static int __nf_flow_offload_validate(unsigned long cookie)
+{
+ struct flow_offload *flow = nf_flow_offload_cookie_flow(cookie);
+ unsigned int dir;
+
+ if (nf_flow_bridge_stale(flow))
+ return -ESTALE;
+
+ for (dir = 0; dir < FLOW_OFFLOAD_DIR_MAX; dir++) {
+ struct flow_offload_tuple *tuple = &flow->tuplehash[dir].tuple;
+ struct flow_offload_tuple *other = &flow->tuplehash[!dir].tuple;
+ struct dst_entry *dst = tuple->dst_cache;
+ struct net_device *dev;
+ struct neighbour *neigh;
+ u8 mac[ETH_ALEN];
+
+ if (!dst)
+ return -EOPNOTSUPP;
+ /* This is the actual route selected with the packet's policy
+ * context, including mark, DSCP and the reverse lookup's oif.
+ */
+ if (!dst_check(dst, tuple->dst_cookie))
+ return -ESTALE;
+ if (tuple->xmit_type != FLOW_OFFLOAD_XMIT_DIRECT)
+ continue;
+ /* A stacked path may have a different final Ethernet neighbour.
+ * Do not mistake that unsupported context for a stale owner.
+ */
+ dev = dst_dev_rcu(dst);
+ if (tuple->l3proto != NFPROTO_IPV4)
+ return -EOPNOTSUPP;
+ /* Without an entry the path is not proven stale. */
+ neigh = nf_flow_offload_neigh(dst, dev, other);
+ if (!neigh)
+ return -EOPNOTSUPP;
+ neigh_ha_snapshot(mac, neigh, dev);
+ if (!(READ_ONCE(neigh->nud_state) & NUD_VALID) ||
+ !ether_addr_equal(mac, tuple->out.h_dest))
+ return -ESTALE;
+ if (dev->ifindex != tuple->out.ifidx) {
+ int err = nf_flow_offload_bridge_path(tuple, dev, mac);
+
+ if (err)
+ return err;
+ }
+ }
+ return 0;
+}
+
+static int nf_flow_offload_validate(unsigned long cookie, u8 *ingress_mac)
+{
+ int err;
+
+ rcu_read_lock();
+ err = __nf_flow_offload_validate(cookie);
+ if (!err && ingress_mac) {
+ const struct flow_offload_tuple *tuple = (void *)cookie;
+ struct flow_offload *flow = nf_flow_offload_cookie_flow(cookie);
+ const struct flow_offload_tuple *other = &flow->tuplehash[!tuple->dir].tuple;
+
+ if (other->xmit_type != FLOW_OFFLOAD_XMIT_DIRECT)
+ err = -EOPNOTSUPP;
+ else
+ ether_addr_copy(ingress_mac, other->out.h_dest);
+ }
+ rcu_read_unlock();
+ return err;
+}
+
static void nf_flow_offload_init(struct flow_cls_offload *cls_flow,
__be16 proto, int priority,
enum flow_cls_command cmd,
@@ -896,6 +1033,8 @@ static void nf_flow_offload_init(struct flow_cls_offload *cls_flow,
cls_flow->common.extack = extack;
cls_flow->command = cmd;
cls_flow->cookie = (unsigned long)tuple;
+ cls_flow->invalidate = nf_flow_offload_invalidate;
+ cls_flow->validate = nf_flow_offload_validate;
}
static int nf_flow_offload_tuple(struct nf_flowtable *flowtable,
diff --git a/net/netfilter/nf_flow_table_path.c b/net/netfilter/nf_flow_table_path.c
index 841f099434d98..de862e3e7770d 100644
--- a/net/netfilter/nf_flow_table_path.c
+++ b/net/netfilter/nf_flow_table_path.c
@@ -96,6 +96,7 @@ struct nft_forward_info {
u8 h_source[ETH_ALEN];
u8 h_dest[ETH_ALEN];
bool needs_gso_segment;
+ bool uses_bridge;
enum flow_offload_xmit_type xmit_type;
};
@@ -157,6 +158,7 @@ static int nft_dev_path_info(struct net_device_path_stack *stack,
}
break;
case DEV_PATH_BRIDGE:
+ info->uses_bridge = true;
if (is_zero_ether_addr(info->h_source))
memcpy(info->h_source, path->dev->dev_addr, ETH_ALEN);
@@ -265,6 +267,7 @@ static int nft_dev_forward_path(const struct nft_pktinfo *pkt,
route->tuple[dir].xmit_type = info.xmit_type;
}
route->tuple[dir].out.needs_gso_segment = info.needs_gso_segment;
+ route->uses_bridge |= info.uses_bridge;
return 0;
}
@@ -277,6 +280,11 @@ int nft_flow_route(const struct nft_pktinfo *pkt, const struct nf_conn *ct,
struct dst_entry *other_dst = NULL;
struct flowi fl;
+ /* Snapshot before either FDB lookup. An update during path resolution
+ * must invalidate the resulting cache, including software-only owners.
+ */
+ route->bridge_genid = atomic_read_acquire(&nft_net(pkt)->ft.bridge_genid);
+
memset(&fl, 0, sizeof(fl));
switch (nft_pf(pkt)) {
case NFPROTO_IPV4:
diff --git a/net/netfilter/nft_flow_offload.c b/net/netfilter/nft_flow_offload.c
index d3c5651dd6997..05207ca808ee7 100644
--- a/net/netfilter/nft_flow_offload.c
+++ b/net/netfilter/nft_flow_offload.c
@@ -113,7 +113,11 @@ static void nft_flow_offload_eval(const struct nft_expr *expr,
if (!flow)
goto err_flow_alloc;
- flow_offload_route_init(flow, &route);
+ /* Hardware admission may validate the route used to build a direct path.
+ * Keep its reference and cookie instead of reconstructing a lookup with
+ * incomplete policy-routing inputs in the driver.
+ */
+ flow_offload_route_init(flow, &route, nf_flowtable_hw_offload(flowtable));
if (tcph)
flow_offload_ct_tcp(ct);
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH net-next v2 4/6] net: bgmac: add Northstar IPv4 TCP/UDP NAT flowtable offload
2026-12-01 18:22 [PATCH net-next v2 0/6] Add Northstar FA TCP/UDP NAT offload with a bridged LAN Florian Fainelli
` (2 preceding siblings ...)
2026-12-01 18:22 ` [PATCH net-next v2 3/6] net: flow_offload: allow drivers to validate and invalidate cached paths Florian Fainelli
@ 2026-12-01 18:22 ` Florian Fainelli
2026-12-01 18:22 ` [PATCH net-next v2 5/6] net: add KUnit coverage for FA and flowtable path validation Florian Fainelli
2026-12-01 18:22 ` [PATCH net-next v2 6/6] docs: net: document Northstar FA setup and NAT qualification Florian Fainelli
5 siblings, 0 replies; 7+ messages in thread
From: Florian Fainelli @ 2026-12-01 18:22 UTC (permalink / raw)
To: netdev
Cc: Florian Fainelli, andrew+netdev, bcm-kernel-feedback-list, davem,
edumazet, kuba, linux-kernel, pabeni, rafal
Expose the inline BCM4708/BCM5301X accelerator through TC_SETUP_FT on
GMAC2. Learn placement hashes from miss metadata and install exact IPv4
TCP/UDP SNAT or DNAT rules with shared router-MAC and next-hop
resources. Accept two standalone untagged DSA ports, or a standalone WAN
opposite an untagged LAN bridge. Require distinct router MACs, MTU 1500
and one flowtable in the initial namespace. Require an operational GMAC0
exception path for both protocols and the flowtable FIN/RST exclusion
mask for TCP. Reject unsupported or exhausted rules.
Gate lookup synchronously and retire all hardware owners on every IPv4
route, rule or nexthop notification, including unrelated prefixes and
local-table changes. This is deliberately conservative: the driver lacks
enough policy-routing context to prove an event irrelevant. Coalesce
control work, accepting reduced offload residency during unrelated
churn. Track explicit low-MTU prefixes incrementally from notifier
replay and updates; avoid repeated FIB dumps. Bound tracking memory and
latch software fallback if tracking is exhausted.
Use the owner's actual cached-route and neighbour validation as the only
authority for stale-owner teardown. The driver's partial FIB lookup is
an eligibility probe; it lacks mark, DSCP and output-interface context.
A mismatch or unsupported shape declines hardware offload without
tearing down a valid policy-routed software flow. Reject publication
across invalidation generations. Use RCU lookups and tracked netdevice
references. Drive neighbour probes from hardware counter activity,
including the initial pre-wrap jiffies epoch. Retire owners on
neighbour, topology or exception-path changes that withdraw their
hardware paths. Any update of an unknown neighbour or loss of validity
on an FA port retires them, as the driver cannot tell which next hops
are in use, and low-MTU prefixes tracked anywhere disable offload until
reprobe. Probe the next hop of every installed entry, and check its
neighbour again once the entry is published. Flush on NETDEV_PRE_UP, as
a port can receive before NETDEV_UP.
Resolve bridged routes against their bridge neighbour and current FDB
path, using the bridge router MAC. Validate both cached directions and
acquire a source-MAC ingress guard from the authoritative reverse peer
before publishing a NAT row. Keep WLAN and other software-member flows
in software. Retire on FDB, STP, security, VLAN, MAC or membership
changes and preserve fallback on classifier or table exhaustion.
Quiesce lookups using GMAC2 CPU flow control around indirect table
accesses, drain an in-flight frame, check busy and address completion,
and release read mode before resuming traffic. Preserve Broadcom-tag
relocation when disabling lookup. On an uncertain transaction, gate the
parser before releasing pause, retain uncertain resources and reject new
entries until reprobe. Add optional per-device completion fault
injection.
TCP control packets retire both NAT directions before tuple reuse. A
control packet matches either direction of an entry, with exact ports,
and an entry paired with a retiring direction is retired too, even if it
was installed concurrently. Fragments other than the first, and headers
which cannot be parsed, are left to the slow path as in the software
flowtable. Accumulate packet deltas and preserve the final delta for
retired owners; do not fabricate hardware byte counters. Provide debugfs
status and raw table readback. FA2/FA+, PPPoE and board enablement
remain separate work.
The accelerator is inline in the data path of GMAC2 and the DSA tag
format depends on how it was configured, so failing to initialize it
fails the probe of GMAC2. Only the initial namespace can bind a
flowtable. Suspend and resume are not supported, as BCMA has no PM
callbacks.
Signed-off-by: Florian Fainelli <florian.fainelli@broadcom.com>
---
Changes in v2:
- Drive neighbour probes from every installed entry's next hop instead of
a bounded notification cache. Recheck the neighbour after publishing an
entry and before committing its hardware row.
- Match TCP control packets by exact ports in either direction, including
headers with IP options. Leave unparseable/non-initial fragments to the
slow path. Retire a concurrently installed peer when the other
direction is already closing.
- Serialize flow-block bookkeeping, reject non-initial namespaces at bind
time, and invalidate before a newly opened port can receive.
- Use READ_ONCE/WRITE_ONCE for shared MTU-tracking flags, hash debugfs
cookie pointers, and remove the unsupported BCMA suspend hook.
- Accept the bridged LAN router layout, use the bridge router MAC and
owner-derived peer address, publish source guards before NAT rows, and
retire entries on FDB, STP, security, VLAN, MAC and membership changes.
Keep software-member traffic in software.
MAINTAINERS | 1 +
drivers/net/ethernet/broadcom/Kconfig | 26 +
drivers/net/ethernet/broadcom/Makefile | 1 +
drivers/net/ethernet/broadcom/bcm-ns-fa.c | 2214 ++++++++++++++++++++
drivers/net/ethernet/broadcom/bcm-ns-fa.h | 30 +
drivers/net/ethernet/broadcom/bgmac-bcma.c | 5 +
drivers/net/ethernet/broadcom/bgmac.c | 30 +
drivers/net/ethernet/broadcom/bgmac.h | 2 +
8 files changed, 2309 insertions(+)
create mode 100644 drivers/net/ethernet/broadcom/bcm-ns-fa.c
create mode 100644 drivers/net/ethernet/broadcom/bcm-ns-fa.h
diff --git a/MAINTAINERS b/MAINTAINERS
index dce9a88f4aef4..80969b91af402 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5671,6 +5671,7 @@ S: Maintained
F: Documentation/devicetree/bindings/net/brcm,amac.yaml
F: Documentation/devicetree/bindings/net/brcm,bcm4708-fa.yaml
F: Documentation/devicetree/bindings/net/brcm,bcm5301x-gmac.yaml
+F: drivers/net/ethernet/broadcom/bcm-ns-fa*
F: drivers/net/ethernet/broadcom/bgmac*
F: drivers/net/ethernet/broadcom/unimac.h
diff --git a/drivers/net/ethernet/broadcom/Kconfig b/drivers/net/ethernet/broadcom/Kconfig
index f0bac0dd14390..bb86e73757ee1 100644
--- a/drivers/net/ethernet/broadcom/Kconfig
+++ b/drivers/net/ethernet/broadcom/Kconfig
@@ -163,13 +163,38 @@ config BNX2X_SRIOV
config BGMAC
tristate
+ depends on BCM_NS_FA || !BCM_NS_FA
help
This enables the integrated ethernet controller support for many
Broadcom (mostly iProc) SoCs. An appropriate bus interface driver
needs to be enabled to select this.
+config BCM_NS_FA
+ tristate "Broadcom Northstar IPv4 TCP/UDP NAT flow accelerator"
+ depends on ARCH_BCM_5301X || COMPILE_TEST
+ depends on OF && NET_DSA && NF_FLOW_TABLE
+ select NET_DSA_TAG_BRCM_FA
+ help
+ Offload IPv4 TCP/UDP NAT flows from nftables flowtables to the inline
+ accelerator on BCM4708, BCM53011 and BCM53012. Requires GMAC2, a B53 switch
+ using brcm-fa tags, a second CPU link through GMAC0 for IPv4 exceptions,
+ and explicit device tree configuration.
+ Supports standalone ports or a standalone WAN opposite an untagged
+ LAN bridge. WLAN members retain software forwarding. VLANs,
+ VLAN-filtering bridges, PPPoE and double NAT use the software path.
+
+config BCM_NS_FA_FAULT_INJECTION
+ bool "Fault injection for Northstar FA table transactions"
+ depends on BCM_NS_FA && FAULT_INJECTION_DEBUG_FS
+ help
+ Add per-device fail_table controls in debugfs to simulate an uncertain
+ indirect table completion. The failure disables hardware lookup until
+ reprobe and leaves forwarding to software. Intended for driver testing.
+ Injection is disabled by default and can be armed through debugfs.
+
config BGMAC_BCMA
tristate "Broadcom iProc GBit BCMA support"
+ depends on BCM_NS_FA || !BCM_NS_FA
depends on BCMA && BCMA_HOST_SOC
depends on BCM47XX || ARCH_BCM_5301X || COMPILE_TEST
select BGMAC
@@ -183,6 +208,7 @@ config BGMAC_BCMA
config BGMAC_PLATFORM
tristate "Broadcom iProc GBit platform support"
+ depends on BCM_NS_FA || !BCM_NS_FA
depends on ARCH_BCM_IPROC || COMPILE_TEST
select BGMAC
select PHYLIB
diff --git a/drivers/net/ethernet/broadcom/Makefile b/drivers/net/ethernet/broadcom/Makefile
index 10cc1c92ecfc6..1c445d9849efe 100644
--- a/drivers/net/ethernet/broadcom/Makefile
+++ b/drivers/net/ethernet/broadcom/Makefile
@@ -19,3 +19,4 @@ obj-$(CONFIG_SYSTEMPORT) += bcmsysport.o
obj-$(CONFIG_BNXT) += bnxt/
obj-$(CONFIG_BCMASP) += asp2/
obj-$(CONFIG_BNGE) += bnge/
+obj-$(CONFIG_BCM_NS_FA) += bcm-ns-fa.o
diff --git a/drivers/net/ethernet/broadcom/bcm-ns-fa.c b/drivers/net/ethernet/broadcom/bcm-ns-fa.c
new file mode 100644
index 0000000000000..800381a828ef5
--- /dev/null
+++ b/drivers/net/ethernet/broadcom/bcm-ns-fa.c
@@ -0,0 +1,2214 @@
+// SPDX-License-Identifier: GPL-2.0-only AND ISC
+/* Broadcom Northstar inline flow accelerator, GMAC2 / switch IMP0.
+ *
+ * Table formats and access sequence follow the BCM5301X FA theory of
+ * operations and the ISC-licensed 2013 Broadcom ctf_config.c reference.
+ * Copyright (C) 2013 Broadcom Corporation
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/dsa/brcm.h>
+#include <linux/debugfs.h>
+#include <linux/delay.h>
+#include <linux/etherdevice.h>
+#include <linux/fault-inject.h>
+#include <linux/hashtable.h>
+#include <linux/if_bridge.h>
+#include <linux/if_vlan.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>
+#include <linux/ip.h>
+#include <linux/module.h>
+#include <linux/of_address.h>
+#include <linux/seq_file.h>
+#include <linux/tcp.h>
+#include <linux/udp.h>
+#include <linux/workqueue.h>
+#include <linux/unaligned.h>
+#include <linux/tc_act/tc_csum.h>
+#include <net/dsa.h>
+#include <net/arp.h>
+#include <net/flow_offload.h>
+#include <net/fib_notifier.h>
+#include <net/ip.h>
+#include <net/ip_fib.h>
+#include <net/neighbour.h>
+#include <net/netevent.h>
+#include <net/net_namespace.h>
+#include <net/switchdev.h>
+
+#include "bcm-ns-fa.h"
+#include "bgmac.h"
+
+#define FA_CTL 0x00
+#define FA_CTL_BYPASS BIT(1)
+#define FA_CTL_CRC_OVERWRITE BIT(3)
+#define FA_CTL_THRESHOLD GENMASK(12, 4)
+#define FA_CTL_INIT GENMASK(17, 13)
+#define FA_CTL_NO_DA_CHECK BIT(18)
+#define FA_MEM_CTL 0x04
+#define FA_MEM_READ BIT(12)
+#define FA_MEM_TABLE GENMASK(11, 10)
+#define FA_MEM_CURRENT_INDEX GENMASK(22, 13)
+#define FA_BRCM_HDR 0x08
+#define FA_BRCM_HW BIT(0)
+#define FA_BRCM_RX BIT(1)
+#define FA_BRCM_TX BIT(2)
+#define FA_BRCM_PARSE_EXCEPTIONS BIT(3)
+#define FA_L2_SKIP 0x0c
+#define FA_L2_TAG 0x10
+#define FA_L2_ETHERTYPE 0x20
+#define FA_IPV4 0x28
+#define FA_IPV4_CSUM BIT(16)
+#define FA_NAPT 0x2c
+#define FA_NAPT_DEC_TTL BIT(21)
+#define FA_STATUS 0x30
+#define FA_INIT_DONE GENMASK(9, 5)
+#define FA_STATUS_MASK 0x34
+#define FA_RX_STATUS 0x38
+#define FA_HITS 0x3c
+#define FA_MISSES 0x40
+#define FA_ERROR 0x64
+#define FA_ERROR_MASK 0x68
+#define FA_DEBUG_STATUS 0x70
+#define FA_MEM_BUSY BIT(0)
+#define FA_DATA(n) (0xa0 + 4 * (n))
+#define FA_FLOW_TABLE 0
+#define FA_POOL_TABLE 1
+#define FA_NH_TABLE 2
+#define FA_SLOTS 1024
+#define FA_WAYS 4
+#define FA_POOLS 4
+#define FA_NEXTHOPS 128
+#define FA_TAG_LEN 4
+#define FA_MISS_OPCODE 2
+#define FA_FLOW_CLOSING 0
+#define FA_MTU_HASH_BITS 6
+#define FA_MTU_ROUTES_MAX 1024
+
+/* IPv4 flow entry, indexed by DATA word; global bit numbers in AN300 table 3. */
+#define FA_FLOW0_HITS_LO GENMASK(31, 3) /* hits[28:0], bits 31:3 */
+#define FA_FLOW1_HITS_HI GENMASK(2, 0) /* hits[31:29], bits 34:32 */
+#define FA_FLOW1_ACTION GENMASK(4, 3)
+#define FA_FLOW1_ROUTER_MAC GENMASK(7, 6)
+#define FA_FLOW1_NEXT_HOP GENMASK(14, 8)
+#define FA_FLOW1_REMAP_IP_LO GENMASK(31, 15) /* remap_ip[16:0], bits 63:47 */
+#define FA_FLOW2_REMAP_IP_HI GENMASK(14, 0) /* remap_ip[31:17], bits 78:64 */
+#define FA_FLOW2_REMAP_PORT GENMASK(30, 15)
+#define FA_FLOW2_DPORT_LO BIT(31) /* dport[0], bit 95 */
+#define FA_FLOW3_DPORT_HI GENMASK(14, 0) /* dport[15:1], bits 110:96 */
+#define FA_FLOW3_SPORT GENMASK(30, 15)
+#define FA_FLOW3_TCP BIT(31)
+#define FA_FLOW6_DIRECTION BIT(0)
+#define FA_FLOW6_DEST_MAP GENMASK(10, 1)
+#define FA_FLOW6_OPCODE GENMASK(19, 17)
+#define FA_FLOW6_OPCODE_INGRESS 1
+#define FA_FLOW6_VALID BIT(20)
+#define FA_FLOW7_IPV4 BIT(31)
+
+/* Router-MAC pool entry: external in bit 0, MAC in bits 48:1. */
+#define FA_POOL0_EXTERNAL BIT(0)
+#define FA_POOL0_MAC_LO GENMASK(31, 1)
+#define FA_POOL1_MAC_HI GENMASK(16, 0)
+
+/* Next-hop entry: frame type in bit 0, tag op in 2:1, DA in 66:19. */
+#define FA_NH0_TAG_OP GENMASK(2, 1)
+#define FA_NH_TAG_NONE 2
+#define FA_NH0_MAC_LO GENMASK(31, 19)
+#define FA_NH2_MAC_HI GENMASK(2, 0)
+
+/* Flowtable rule validation holds RCU, rather than RTNL. */
+static bool fa_has_upper_dev_rcu(struct net_device *dev)
+{
+ struct list_head *iter = &dev->adj_list.upper;
+
+ return netdev_upper_get_next_dev_rcu(dev, &iter);
+}
+
+/* No implicit padding: also used as the key for the RX hash cache. */
+struct fa_tuple {
+ __be32 src, dst;
+ __be16 sport, dport;
+};
+
+#define FA_ACT_REMAP_IP BIT(0)
+#define FA_ACT_REMAP_PORT BIT(1)
+
+enum fa_direction {
+ FA_DIR_UNSET = -1,
+ FA_DIR_SNAT,
+ FA_DIR_DNAT,
+};
+
+struct fa_rule {
+ struct fa_tuple tuple;
+ struct net_device *in, *out;
+ struct net_device *in_l3, *out_l3;
+ u8 ingress_mac[ETH_ALEN];
+ u8 eth[ETH_ALEN * 2];
+ u8 eth_mask[ETH_ALEN * 2];
+ u32 remap_ip;
+ u16 remap_port;
+ u8 action, l4proto;
+ enum fa_direction direction;
+ __be32 gw; /* Next hop, set by fa_route_check(). */
+};
+
+static int fa_direction(struct fa_rule *rule, enum fa_direction direction)
+{
+ if (rule->direction != FA_DIR_UNSET && rule->direction != direction)
+ return -EOPNOTSUPP;
+ rule->direction = direction;
+ return 0;
+}
+
+static int fa_mangle(struct fa_rule *rule, const struct flow_action_entry *act)
+{
+ u32 mask = act->mangle.mask, val = act->mangle.val;
+ unsigned int offset = act->mangle.offset;
+ enum fa_direction dir;
+
+ switch (act->mangle.htype) {
+ case FLOW_ACT_MANGLE_HDR_TYPE_ETH: {
+ u8 *data = rule->eth, *known = rule->eth_mask;
+ unsigned int i;
+
+ if (offset > sizeof(rule->eth) - sizeof(val))
+ return -EOPNOTSUPP;
+ for (i = 0; i < sizeof(val); i++) {
+ u8 m = ((u8 *)&mask)[i];
+
+ data[offset + i] = (data[offset + i] & m) |
+ (((u8 *)&val)[i] & ~m);
+ known[offset + i] |= ~m;
+ }
+ return 0;
+ }
+ case FLOW_ACT_MANGLE_HDR_TYPE_IP4:
+ if (mask || (offset != offsetof(struct iphdr, saddr) &&
+ offset != offsetof(struct iphdr, daddr)))
+ return -EOPNOTSUPP;
+ dir = offset == offsetof(struct iphdr, daddr) ? FA_DIR_DNAT : FA_DIR_SNAT;
+ if (fa_direction(rule, dir) || (rule->action & FA_ACT_REMAP_IP))
+ return -EOPNOTSUPP;
+ rule->remap_ip = be32_to_cpu((__force __be32)val);
+ rule->action |= FA_ACT_REMAP_IP;
+ return 0;
+ case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
+ case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
+ if ((act->mangle.htype == FLOW_ACT_MANGLE_HDR_TYPE_TCP &&
+ rule->l4proto != IPPROTO_TCP) ||
+ (act->mangle.htype == FLOW_ACT_MANGLE_HDR_TYPE_UDP &&
+ rule->l4proto != IPPROTO_UDP))
+ return -EOPNOTSUPP;
+ if (offset)
+ return -EOPNOTSUPP;
+ mask = be32_to_cpu((__force __be32)mask);
+ val = be32_to_cpu((__force __be32)val);
+ if (mask != 0xffff && mask != 0xffff0000)
+ return -EOPNOTSUPP;
+ dir = mask == 0xffff0000 ? FA_DIR_DNAT : FA_DIR_SNAT;
+ if (fa_direction(rule, dir) || (rule->action & FA_ACT_REMAP_PORT))
+ return -EOPNOTSUPP;
+ rule->remap_port = dir ? val & 0xffff : val >> 16;
+ rule->action |= FA_ACT_REMAP_PORT;
+ return 0;
+ default:
+ return -EOPNOTSUPP;
+ }
+}
+
+struct fa_learned {
+ struct fa_tuple tuple;
+ u8 mac[ETH_ALEN], source[ETH_ALEN];
+ unsigned long generation;
+ u8 hash, port, l4proto;
+ bool valid;
+};
+
+struct fa_mac {
+ u8 addr[ETH_ALEN];
+ bool external;
+ unsigned int refs;
+};
+
+struct fa_client {
+ u8 addr[ETH_ALEN];
+ u8 port;
+ unsigned int refs;
+};
+
+struct fa_neighbour {
+ __be32 address;
+ int ifindex;
+ u8 mac[ETH_ALEN];
+ u8 state;
+ bool valid;
+};
+
+struct fa_flow {
+ struct list_head list;
+ struct rcu_head rcu;
+ void (*invalidate)(unsigned long cookie);
+ struct fa_flow *reverse;
+ struct fa_tuple reply;
+ __be32 gw;
+ unsigned long flags;
+ unsigned long cookie;
+ struct fa_tuple tuple;
+ struct net_device *in, *out, *out_l3;
+ netdevice_tracker in_tracker, out_tracker, out_l3_tracker;
+ u16 client;
+ unsigned long lastused;
+ u64 pending_hits;
+ u32 hits;
+ u8 pool_in, pool_out, nh, l4proto, in_port, out_port;
+ bool installed;
+};
+
+struct fa_mtu_route {
+ struct hlist_node node;
+ u32 dst, tb_id;
+ int prefix_len;
+};
+
+struct bcm_ns_fa {
+ struct fault_attr fail_table;
+ bool fail_table_write_only;
+ unsigned long fault_injections;
+ struct brcm_fa_control control;
+ struct net_device *conduit;
+ void __iomem *regs;
+ struct mutex lock; /* Flow blocks, table port, and software ownership. */
+ spinlock_t learn_lock; /* RX NAPI versus flowtable workqueue. */
+ struct fa_learned *learned;
+ unsigned int learn_next;
+ unsigned int nr_flows;
+ struct fa_flow *flows[FA_SLOTS];
+ struct fa_mac pool[FA_POOLS], nh[FA_NEXTHOPS];
+ struct fa_client clients[BRCM_FA_BRIDGE_GUARDS];
+ struct fa_neighbour neighbours[FA_NEXTHOPS];
+ unsigned int neighbour_next;
+ struct list_head block_list;
+ struct list_head entries; /* RCU readers in NAPI, writers under lock. */
+ struct work_struct control_work;
+ unsigned long control_generation;
+ unsigned long invalidate_generation;
+ unsigned long pending_generation;
+ bool control_pending;
+ bool tcp_ready;
+ bool stopping;
+ bool route_mtu_ok;
+ bool mtu_tracking_failed;
+ DECLARE_HASHTABLE(mtu_routes, FA_MTU_HASH_BITS);
+ unsigned int nr_mtu_routes;
+ unsigned long invalidations;
+ unsigned long neighbour_check;
+ struct notifier_block netdev_nb;
+ struct notifier_block fib_nb;
+ struct notifier_block netevent_nb;
+ struct notifier_block switchdev_nb, switchdev_blocking_nb;
+ struct dentry *debugfs;
+ bool failed;
+ bool enabled;
+};
+
+/* Serialize control changes with the NAPI stop request. */
+static void fa_control_update(struct bcm_ns_fa *fa, u32 set, u32 clear)
+{
+ u32 val;
+
+ spin_lock_bh(&fa->learn_lock);
+ val = readl(fa->regs + FA_CTL);
+ writel((val | set) & ~clear, fa->regs + FA_CTL);
+ readl(fa->regs + FA_CTL);
+ spin_unlock_bh(&fa->learn_lock);
+}
+
+/* Caller holds learn_lock. Stop lookups without bypassing tag relocation:
+ * global bypass wires the switch directly to DMA and breaks brcm-fa frames.
+ */
+static void fa_lookup_update(struct bcm_ns_fa *fa)
+{
+ bool enabled = fa->enabled && fa->tcp_ready && fa->route_mtu_ok && !fa->failed &&
+ !fa->control_pending && !fa->stopping;
+
+ writel(enabled ? ETH_P_IP | ETH_P_IPV6 << 16 : 0, fa->regs + FA_L2_ETHERTYPE);
+ readl(fa->regs + FA_L2_ETHERTYPE);
+}
+
+static void fa_reply_tuple(const struct fa_rule *rule, struct fa_tuple *reply)
+{
+ *reply = (struct fa_tuple){ rule->tuple.dst, rule->tuple.src,
+ rule->tuple.dport, rule->tuple.sport };
+ if (rule->direction == FA_DIR_DNAT) {
+ if (rule->action & FA_ACT_REMAP_IP)
+ reply->src = htonl(rule->remap_ip);
+ if (rule->action & FA_ACT_REMAP_PORT)
+ reply->sport = htons(rule->remap_port);
+ } else {
+ if (rule->action & FA_ACT_REMAP_IP)
+ reply->dst = htonl(rule->remap_ip);
+ if (rule->action & FA_ACT_REMAP_PORT)
+ reply->dport = htons(rule->remap_port);
+ }
+}
+
+static bool fa_control_match(const struct fa_flow *entry,
+ const struct fa_tuple *tuple, unsigned int port)
+{
+ if (entry->l4proto != IPPROTO_TCP)
+ return false;
+ /* The control packet may travel in either direction of the connection. */
+ return (entry->in_port == port &&
+ !memcmp(&entry->tuple, tuple, sizeof(*tuple))) ||
+ (entry->out_port == port &&
+ !memcmp(&entry->reply, tuple, sizeof(*tuple)));
+}
+
+/* Stop data lookups and retire the control-marked entries in process context.
+ * Caller holds learn_lock.
+ */
+static void fa_control_kick(struct bcm_ns_fa *fa)
+{
+ fa->pending_generation++;
+ fa->control_pending = true;
+ fa_lookup_update(fa);
+ queue_work(system_dfl_wq, &fa->control_work);
+}
+
+/* Switch traps bypass FA lookup. Stop subsequent data lookups before Linux
+ * can reuse their NAT tuple; retire both hardware directions in process context.
+ * The publication lock also closes the race with an in-flight insertion.
+ */
+static void fa_tcp_control(struct bcm_ns_fa *fa, const struct fa_tuple *tuple,
+ unsigned int port)
+{
+ struct fa_flow *entry;
+ bool found = false;
+
+ rcu_read_lock();
+ spin_lock_bh(&fa->learn_lock);
+ if (fa->stopping)
+ goto out;
+ fa->control_generation++;
+ list_for_each_entry_rcu(entry, &fa->entries, list) {
+ if (!fa_control_match(entry, tuple, port) ||
+ test_bit(FA_FLOW_CLOSING, &entry->flags))
+ continue;
+ set_bit(FA_FLOW_CLOSING, &entry->flags);
+ found = true;
+ }
+ if (found)
+ fa_control_kick(fa);
+out:
+ spin_unlock_bh(&fa->learn_lock);
+ rcu_read_unlock();
+}
+
+static int fa_wait(struct bcm_ns_fa *fa)
+{
+ u32 val;
+
+ return readl_poll_timeout(fa->regs + FA_DEBUG_STATUS, val,
+ !(val & FA_MEM_BUSY), 1, 1000);
+}
+
+static int fa_wait_index(struct bcm_ns_fa *fa, unsigned int next)
+{
+ u32 val;
+ int err;
+
+ err = readl_poll_timeout(fa->regs + FA_MEM_CTL, val,
+ FIELD_GET(FA_MEM_CURRENT_INDEX, val) == (next & 0x3ff) &&
+ !(readl(fa->regs + FA_DEBUG_STATUS) & FA_MEM_BUSY),
+ 1, 1000);
+ if (err)
+ netdev_err(fa->conduit, "FA index wait: expected %u control %#x busy %#x\n",
+ next, val, readl(fa->regs + FA_DEBUG_STATUS));
+ return err;
+}
+
+static DECLARE_FAULT_ATTR(fa_fail_table_default);
+
+static int fa_table_complete(struct bcm_ns_fa *fa, unsigned int next, bool write)
+{
+ int err = fa_wait_index(fa, next);
+
+ if (!err && IS_ENABLED(CONFIG_BCM_NS_FA_FAULT_INJECTION) &&
+ (!READ_ONCE(fa->fail_table_write_only) || write) &&
+ should_fail(&fa->fail_table, 1)) {
+ fa->fault_injections++;
+ return -ETIMEDOUT;
+ }
+ return err;
+}
+
+static void fa_fail(struct bcm_ns_fa *fa);
+
+struct fa_table_access {
+ u32 mac_control;
+ bool paused;
+};
+
+static int fa_table_begin(struct bcm_ns_fa *fa, struct fa_table_access *access)
+{
+ struct bgmac *bgmac = netdev_priv(fa->conduit);
+ u32 val;
+ int err;
+
+ access->paused = false;
+ if (!netif_running(fa->conduit))
+ return 0;
+ if (readl(fa->regs + FA_CTL) & FA_CTL_BYPASS) {
+ /* Initialization and final release use global bypass. */
+ fsleep(20);
+ return 0;
+ }
+ access->mac_control = bgmac_read(bgmac, BGMAC_DEV_CTL);
+ access->paused = true;
+ /* Keep automatic flow control and request a pause from the CPU too. */
+ val = (access->mac_control & ~BGMAC_DC_FCM_MASK) |
+ FIELD_PREP(BGMAC_DC_FCM_MASK, 2) | BGMAC_DC_CFCO;
+ bgmac_write(bgmac, BGMAC_DEV_CTL, val);
+ err = read_poll_timeout(bgmac_read, val, val & BGMAC_DS_PO, 1, 1000,
+ false, bgmac, BGMAC_DEV_STATUS);
+ if (err)
+ return err;
+ /* Drain a frame already on the switch link before taking the shared
+ * table port. Do not change the FA packet path in the middle of a frame.
+ */
+ fsleep(20);
+ return 0;
+}
+
+static void fa_table_end(struct bcm_ns_fa *fa, struct fa_table_access *access)
+{
+ struct bgmac *bgmac = netdev_priv(fa->conduit);
+
+ if (access->paused) {
+ bgmac_write(bgmac, BGMAC_DEV_CTL, access->mac_control);
+ bgmac_read(bgmac, BGMAC_DEV_CTL);
+ }
+}
+
+/* Reading DATA0 triggers a transaction, including the final read below. */
+static int fa_read(struct bcm_ns_fa *fa, unsigned int table, unsigned int index,
+ u32 data[8])
+{
+ struct fa_table_access access;
+ int i, err;
+
+ err = fa_table_begin(fa, &access);
+ if (err)
+ goto restore;
+ err = fa_wait(fa);
+ if (err)
+ goto restore;
+ writel(FA_MEM_READ | FIELD_PREP(FA_MEM_TABLE, table) | index,
+ fa->regs + FA_MEM_CTL);
+ err = fa_wait_index(fa, index);
+ if (err)
+ goto out;
+ readl(fa->regs + FA_DATA(0));
+ err = fa_table_complete(fa, index + 1, false);
+ if (err)
+ goto out;
+ for (i = 7; i >= 0; i--)
+ data[i] = readl(fa->regs + FA_DATA(i));
+ err = fa_table_complete(fa, index + 2, false);
+out:
+ /* Release the software read port so packet hit counters can update. */
+ writel(0, fa->regs + FA_MEM_CTL);
+ readl(fa->regs + FA_MEM_CTL);
+restore:
+ /* Gate lookups while the switch is still paused on an uncertain access. */
+ if (err)
+ fa_fail(fa);
+ fa_table_end(fa, &access);
+ return err;
+}
+
+static int fa_write(struct bcm_ns_fa *fa, unsigned int table, unsigned int index,
+ const u32 data[8])
+{
+ struct fa_table_access access;
+ int i, err;
+
+ err = fa_table_begin(fa, &access);
+ if (err)
+ goto out;
+ err = fa_wait(fa);
+ if (err)
+ goto out;
+ writel(FIELD_PREP(FA_MEM_TABLE, table) | index, fa->regs + FA_MEM_CTL);
+ err = fa_wait_index(fa, index);
+ if (err)
+ goto out;
+ /* DATA0 commits the complete entry. Never write the valid word last. */
+ for (i = 7; i >= 0; i--)
+ writel(data[i], fa->regs + FA_DATA(i));
+ err = fa_table_complete(fa, index + 1, true);
+out:
+ if (err)
+ fa_fail(fa);
+ fa_table_end(fa, &access);
+ return err;
+}
+
+/* On an uncertain table transaction stop all acceleration. Do not recycle
+ * any table resources until reprobe has reinitialized the hardware.
+ */
+static void fa_fail(struct bcm_ns_fa *fa)
+{
+ if (!fa->failed)
+ netdev_err(fa->conduit, "FA table timeout; offload disabled until reprobe\n");
+ spin_lock_bh(&fa->learn_lock);
+ fa->failed = true;
+ fa_lookup_update(fa);
+ spin_unlock_bh(&fa->learn_lock);
+}
+
+static void fa_mac_encode(const u8 *addr, bool external, bool next_hop, u32 data[8])
+{
+ u64 mac = ether_addr_to_u64(addr);
+
+ if (next_hop) {
+ /* Ethernet II with no VLAN tag; DA spans DATA0 through DATA2. */
+ data[0] = FIELD_PREP(FA_NH0_TAG_OP, FA_NH_TAG_NONE) |
+ FIELD_PREP(FA_NH0_MAC_LO, mac & FIELD_MAX(FA_NH0_MAC_LO));
+ data[1] = mac >> 13;
+ data[2] = FIELD_PREP(FA_NH2_MAC_HI, mac >> 45);
+ } else {
+ data[0] = FIELD_PREP(FA_POOL0_MAC_LO, mac & FIELD_MAX(FA_POOL0_MAC_LO)) |
+ FIELD_PREP(FA_POOL0_EXTERNAL, external);
+ data[1] = FIELD_PREP(FA_POOL1_MAC_HI, mac >> 31);
+ }
+}
+
+static int fa_mac_get(struct bcm_ns_fa *fa, const u8 *addr, bool external,
+ bool next_hop)
+{
+ struct fa_mac *macs = next_hop ? fa->nh : fa->pool;
+ unsigned int count = next_hop ? FA_NEXTHOPS : FA_POOLS;
+ u32 data[8] = {};
+ int i, free = -1, err;
+
+ for (i = 0; i < count; i++) {
+ if (!macs[i].refs) {
+ if (free < 0)
+ free = i;
+ continue;
+ }
+ if (!ether_addr_equal(addr, macs[i].addr))
+ continue;
+ if (!next_hop && external != macs[i].external)
+ return -EOPNOTSUPP;
+ macs[i].refs++;
+ return i;
+ }
+ if (free < 0)
+ return -ENOSPC;
+
+ fa_mac_encode(addr, external, next_hop, data);
+ err = fa_write(fa, next_hop ? FA_NH_TABLE : FA_POOL_TABLE, free, data);
+ if (err) {
+ fa_fail(fa);
+ return err;
+ }
+ ether_addr_copy(macs[free].addr, addr);
+ macs[free].external = external;
+ macs[free].refs = 1;
+ return free;
+}
+
+static int fa_mac_put(struct bcm_ns_fa *fa, unsigned int index, bool next_hop)
+{
+ struct fa_mac *mac = next_hop ? &fa->nh[index] : &fa->pool[index];
+ u32 data[8] = {};
+ int err;
+
+ if (--mac->refs)
+ return 0;
+ err = fa_write(fa, next_hop ? FA_NH_TABLE : FA_POOL_TABLE, index, data);
+ if (err)
+ fa_fail(fa);
+ return err;
+}
+
+static bool fa_receive_tcp_control(struct bcm_ns_fa *fa, const struct iphdr *ip,
+ unsigned int total, unsigned int linear,
+ unsigned int port)
+{
+ unsigned int ihl = ip->ihl * 4;
+ struct fa_tuple tuple;
+ const struct tcphdr *tcp;
+
+ /* Only a first fragment carries the TCP header, and only with it are
+ * the ports and flags known. Like the software flowtable, leave other
+ * fragments, and headers which cannot be parsed, to the slow path.
+ */
+ if (ip->frag_off & htons(IP_OFFSET))
+ return true;
+ if (total < ihl + sizeof(*tcp) || linear < ihl + sizeof(*tcp))
+ return true;
+ tcp = (void *)((u8 *)ip + ihl);
+ if (tcp->doff < 5)
+ return true;
+ if (!tcp->syn && !tcp->fin && !tcp->rst)
+ return false;
+ tuple.src = get_unaligned(&ip->saddr);
+ tuple.dst = get_unaligned(&ip->daddr);
+ tuple.sport = get_unaligned(&tcp->source);
+ tuple.dport = get_unaligned(&tcp->dest);
+ fa_tcp_control(fa, &tuple, port);
+ return true;
+}
+
+static void fa_control_receive(void *priv, struct sk_buff *skb, unsigned int port)
+{
+ struct bcm_ns_fa *fa = priv;
+ const struct iphdr *ip = (void *)skb->data;
+ unsigned int ihl, total;
+
+ if (READ_ONCE(fa->stopping) || eth_hdr(skb)->h_proto != htons(ETH_P_IP) ||
+ skb_headlen(skb) < sizeof(*ip))
+ return;
+ ihl = ip->ihl * 4;
+ total = ntohs(ip->tot_len);
+ if (ip->version != 4 || ip->protocol != IPPROTO_TCP || ihl < sizeof(*ip) ||
+ total < ihl || total > skb->len || skb_headlen(skb) < ihl ||
+ ip_compute_csum(ip, ihl))
+ return;
+ fa_receive_tcp_control(fa, ip, total, skb_headlen(skb), port);
+}
+
+static struct net_device *fa_control_conduit(struct bcm_ns_fa *fa)
+{
+ struct dsa_port *data_cpu = fa->conduit->dsa_ptr, *control_cpu;
+
+ if (!data_cpu)
+ return NULL;
+ control_cpu = dsa_to_port(data_cpu->ds, 5);
+ return control_cpu && dsa_port_is_cpu(control_cpu) ? control_cpu->conduit : NULL;
+}
+
+/* The hardware supplies the exact 97-bit tuple CRC in its miss tag. Learning
+ * it avoids assumptions about serial CRC bit order and seed endianness.
+ * Cache eviction only prevents new offloads; installed entries own their slots.
+ */
+void bcm_ns_fa_rx(struct bcm_ns_fa *fa, struct sk_buff *skb)
+{
+ struct fa_learned *learn;
+ const struct ethhdr *eth;
+ const struct iphdr *ip;
+ const __be16 *ports;
+ struct fa_tuple tuple;
+ unsigned int offset = FA_TAG_LEN + ETH_HLEN, ihl, total;
+ u8 *tag;
+
+ if (!fa || READ_ONCE(fa->stopping) || skb_headlen(skb) < offset + sizeof(*ip))
+ return;
+ tag = skb->data;
+ if (tag[0] >> 5 != FA_MISS_OPCODE && tag[0] >> 5 != 0)
+ return;
+ eth = (void *)(tag + FA_TAG_LEN);
+ ip = (void *)(tag + offset);
+ ihl = ip->ihl * 4;
+ total = ntohs(ip->tot_len);
+ if (eth->h_proto != htons(ETH_P_IP) || ip->version != 4 || ihl < sizeof(*ip) ||
+ (ip->protocol != IPPROTO_TCP && ip->protocol != IPPROTO_UDP) ||
+ total < ihl || total > skb->len - offset || skb_headlen(skb) < offset + ihl ||
+ ip_compute_csum(ip, ihl))
+ return;
+ if (ip->protocol == IPPROTO_TCP &&
+ fa_receive_tcp_control(fa, ip, total, skb_headlen(skb) - offset, tag[3] & 0x1f))
+ return;
+ if (ihl != sizeof(*ip) || (ip->frag_off & htons(IP_MF | IP_OFFSET)) ||
+ total < ihl + sizeof(struct udphdr) ||
+ skb_headlen(skb) < offset + ihl + sizeof(struct udphdr))
+ return;
+ tuple.src = get_unaligned(&ip->saddr);
+ tuple.dst = get_unaligned(&ip->daddr);
+ ports = (void *)(ip + 1);
+ tuple.sport = get_unaligned(ports);
+ tuple.dport = get_unaligned(ports + 1);
+ /* Exception tags are delivered unchanged and contain no usable hash. */
+ if (tag[0] >> 5 != FA_MISS_OPCODE || tag[2])
+ return;
+ spin_lock_bh(&fa->learn_lock);
+ if (fa->control_pending) {
+ spin_unlock_bh(&fa->learn_lock);
+ return;
+ }
+ learn = &fa->learned[fa->learn_next++ % FA_SLOTS];
+ learn->tuple = tuple;
+ learn->l4proto = ip->protocol;
+ learn->generation = ip->protocol == IPPROTO_TCP ? fa->control_generation :
+ fa->invalidate_generation;
+ ether_addr_copy(learn->mac, eth->h_dest);
+ ether_addr_copy(learn->source, eth->h_source);
+ learn->hash = tag[1];
+ learn->port = tag[3] & 0x1f;
+ learn->valid = true;
+ spin_unlock_bh(&fa->learn_lock);
+}
+EXPORT_SYMBOL_GPL(bcm_ns_fa_rx);
+
+static bool fa_learn_matches(const struct fa_learned *learn,
+ const struct fa_rule *rule, unsigned long generation)
+{
+ return learn->valid && learn->l4proto == rule->l4proto &&
+ learn->generation == generation &&
+ !memcmp(&learn->tuple, &rule->tuple, sizeof(learn->tuple));
+}
+
+static int fa_learn_lookup(struct bcm_ns_fa *fa, const struct fa_rule *rule,
+ struct fa_learned *result)
+{
+ int i, err = -EAGAIN;
+
+ spin_lock_bh(&fa->learn_lock);
+ for (i = 0; i < FA_SLOTS; i++) {
+ const struct fa_learned *l = &fa->learned[i];
+
+ if (!fa_learn_matches(l, rule, rule->l4proto == IPPROTO_TCP ?
+ fa->control_generation : fa->invalidate_generation))
+ continue;
+ if (l->port != dsa_port_from_netdev(rule->in)->index ||
+ !ether_addr_equal(l->mac, rule->in_l3->dev_addr))
+ continue;
+ *result = *l;
+ err = 0;
+ }
+ spin_unlock_bh(&fa->learn_lock);
+ return err;
+}
+
+/* Return the untagged L3 interface of a supported physical port. Caller
+ * holds RCU; the bridge itself must not be stacked under another device.
+ */
+static struct net_device *fa_l3_dev(struct net_device *dev)
+{
+ struct net_device *upper, *master;
+ struct list_head *iter;
+
+ master = netdev_master_upper_dev_get_rcu(dev);
+ if (!master)
+ return fa_has_upper_dev_rcu(dev) ? NULL : dev;
+ if (!IS_REACHABLE(CONFIG_BRIDGE) || !netif_is_bridge_master(master) ||
+ dev_net(master) != &init_net || br_vlan_enabled(master) ||
+ fa_has_upper_dev_rcu(master))
+ return NULL;
+ /* A tuple has no ingress port: another active LAN member must not
+ * admit it around bridge source-admission or isolation policy either.
+ */
+ if (br_port_flag_is_set(dev, BR_PORT_LOCKED | BR_PORT_MAB | BR_ISOLATED))
+ return NULL;
+ netdev_for_each_upper_dev_rcu(dev, upper, iter)
+ if (upper != master)
+ return NULL;
+ return master;
+}
+
+static bool fa_switch_port(struct bcm_ns_fa *fa, struct net_device *dev)
+{
+ struct dsa_port *dp;
+
+ if (!dsa_user_dev_check(dev))
+ return false;
+ dp = dsa_port_from_netdev(dev);
+ return dp->ds->index == 0 && dp->index < 8 &&
+ dsa_port_to_conduit(dp) == fa->conduit && dp->cpu_dp->index == 8 &&
+ dp->ds->dst->tag_ops->proto == DSA_TAG_PROTO_BRCM_FA;
+}
+
+static bool fa_port(struct bcm_ns_fa *fa, struct net_device *dev)
+{
+ struct net_device *l3;
+
+ if (!dev || dev_net(dev) != &init_net || !fa_switch_port(fa, dev) ||
+ !netif_running(dev) || !netif_carrier_ok(dev))
+ return false;
+ l3 = fa_l3_dev(dev);
+ if (!l3 || !netif_running(l3) || l3->mtu != ETH_DATA_LEN)
+ return false;
+ if (l3 == dev)
+ return true;
+ if (!IS_REACHABLE(CONFIG_BRIDGE))
+ return false;
+ return br_port_get_stp_state(dev) == BR_STATE_FORWARDING;
+}
+
+static bool fa_bridge(struct bcm_ns_fa *fa, struct net_device *dev)
+{
+ struct net_device *lower;
+ struct list_head *iter;
+
+ if (!netif_is_bridge_master(dev))
+ return false;
+ iter = &dev->adj_list.lower;
+ while ((lower = netdev_next_lower_dev_rcu(dev, &iter)))
+ if (fa_switch_port(fa, lower))
+ return true;
+ return false;
+}
+
+/* Validate the bridge FDB without RTNL or an independently reconstructed FIB
+ * lookup. A software/WLAN egress is a valid fallback but cannot be a FA port.
+ */
+static bool fa_bridge_path(struct net_device *bridge, const u8 *addr,
+ const struct net_device *port)
+{
+ struct net_device_path_ctx ctx = {
+ .dev = bridge,
+ .ether_type = htons(ETH_P_IP),
+ };
+ struct net_device_path_stack stack;
+ bool valid;
+
+ ether_addr_copy(ctx.daddr, addr);
+ if (dev_fill_forward_path(&ctx, &stack) < 0)
+ return false;
+ valid = stack.num_paths == 2 && stack.path[0].type == DEV_PATH_BRIDGE &&
+ stack.path[0].bridge.vlan_mode == DEV_PATH_BR_VLAN_KEEP &&
+ !stack.path[0].bridge.vlan_id && stack.path[1].dev == port;
+ dev_fill_forward_path_release(&stack);
+ return valid;
+}
+
+static int fa_parse(struct bcm_ns_fa *fa, struct flow_cls_offload *cls,
+ struct fa_rule *rule)
+{
+ struct flow_rule *flow = flow_cls_offload_flow_rule(cls);
+ struct flow_match_ipv4_addrs ipv4;
+ struct flow_match_control control;
+ struct flow_match_basic basic;
+ struct flow_match_ports ports;
+ struct flow_match_meta meta;
+ const struct flow_action_entry *act;
+ struct dsa_port *dp;
+ struct net_device *control_dev;
+ u64 allowed;
+ bool csum = false, is_tcp;
+ u32 csum_flags;
+ int i, err;
+
+ allowed = BIT_ULL(FLOW_DISSECTOR_KEY_META) | BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
+ BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) | BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
+ BIT_ULL(FLOW_DISSECTOR_KEY_PORTS);
+ if (flow_rule_match_key(flow, FLOW_DISSECTOR_KEY_TCP)) {
+ struct flow_match_tcp tcp;
+
+ allowed |= BIT_ULL(FLOW_DISSECTOR_KEY_TCP);
+ flow_rule_match_tcp(flow, &tcp);
+ if (tcp.key->flags || tcp.mask->flags !=
+ cpu_to_be16(be32_to_cpu(TCP_FLAG_FIN | TCP_FLAG_RST) >> 16))
+ return -EOPNOTSUPP;
+ }
+ if (cls->common.chain_index || flow->match.dissector->used_keys != allowed)
+ return -EOPNOTSUPP;
+ flow_rule_match_control(flow, &control);
+ flow_rule_match_basic(flow, &basic);
+ flow_rule_match_ipv4_addrs(flow, &ipv4);
+ flow_rule_match_ports(flow, &ports);
+ flow_rule_match_meta(flow, &meta);
+ if (control.key->addr_type != FLOW_DISSECTOR_KEY_IPV4_ADDRS ||
+ control.mask->addr_type != 0xffff || control.mask->flags ||
+ basic.key->n_proto != htons(ETH_P_IP) ||
+ basic.mask->n_proto != htons(0xffff) ||
+ (basic.key->ip_proto != IPPROTO_UDP && basic.key->ip_proto != IPPROTO_TCP) ||
+ basic.mask->ip_proto != 0xff ||
+ ipv4.mask->src != htonl(~0U) || ipv4.mask->dst != htonl(~0U) ||
+ ports.mask->src != htons(0xffff) || ports.mask->dst != htons(0xffff) ||
+ meta.mask->ingress_ifindex != ~0U || meta.mask->ingress_iftype)
+ return -EOPNOTSUPP;
+
+ is_tcp = basic.key->ip_proto == IPPROTO_TCP;
+ if (flow_rule_match_key(flow, FLOW_DISSECTOR_KEY_TCP) != is_tcp)
+ return -EOPNOTSUPP;
+ control_dev = fa_control_conduit(fa);
+ if (!fa->tcp_ready || !control_dev || !netif_running(control_dev) ||
+ !netif_carrier_ok(control_dev) || control_dev->mtu < ETH_DATA_LEN)
+ return -EOPNOTSUPP;
+ csum_flags = TCA_CSUM_UPDATE_FLAG_IPV4HDR |
+ (is_tcp ? TCA_CSUM_UPDATE_FLAG_TCP : TCA_CSUM_UPDATE_FLAG_UDP);
+ rule->l4proto = basic.key->ip_proto;
+ rule->in = dev_get_by_index_rcu(&init_net, meta.key->ingress_ifindex);
+ if (!fa_port(fa, rule->in))
+ return -EOPNOTSUPP;
+ rule->in_l3 = fa_l3_dev(rule->in);
+ rule->tuple.src = ipv4.key->src;
+ rule->tuple.dst = ipv4.key->dst;
+ rule->tuple.sport = ports.key->src;
+ rule->tuple.dport = ports.key->dst;
+ rule->direction = FA_DIR_UNSET;
+
+ flow_action_for_each(i, act, &flow->action) {
+ switch (act->id) {
+ case FLOW_ACTION_MANGLE:
+ err = fa_mangle(rule, act);
+ if (err)
+ return err;
+ break;
+ case FLOW_ACTION_CSUM:
+ if (csum || act->csum_flags != csum_flags)
+ return -EOPNOTSUPP;
+ csum = true;
+ break;
+ case FLOW_ACTION_REDIRECT:
+ if (rule->out || i != flow->action.num_entries - 1)
+ return -EOPNOTSUPP;
+ rule->out = act->dev;
+ break;
+ default:
+ return -EOPNOTSUPP;
+ }
+ }
+ if (!rule->action || !csum || !fa_port(fa, rule->out))
+ return -EOPNOTSUPP;
+ rule->out_l3 = fa_l3_dev(rule->out);
+ if (rule->in_l3 == rule->out_l3 ||
+ (netif_is_bridge_master(rule->in_l3) &&
+ netif_is_bridge_master(rule->out_l3)) ||
+ rule->in == rule->out || rule->in->mtu != rule->out->mtu ||
+ rule->in->mtu != ETH_DATA_LEN ||
+ ether_addr_equal(rule->in_l3->dev_addr, rule->out_l3->dev_addr) ||
+ !ether_addr_equal(rule->out_l3->dev_addr, rule->eth + ETH_ALEN) ||
+ !is_valid_ether_addr(rule->eth) ||
+ memchr_inv(rule->eth_mask, 0xff, sizeof(rule->eth_mask)))
+ return -EOPNOTSUPP;
+ if (dsa_port_from_netdev(rule->in)->ds != dsa_port_from_netdev(rule->out)->ds)
+ return -EOPNOTSUPP;
+
+ /* All active switch ports must belong to the same two L3 domains.
+ * The LAN domain may be one untagged bridge, with software members.
+ */
+ dsa_switch_for_each_user_port(dp, dsa_port_from_netdev(rule->in)->ds)
+ if (dsa_port_to_conduit(dp) == fa->conduit &&
+ netif_running(dp->user) && fa_l3_dev(dp->user) != rule->in_l3 &&
+ fa_l3_dev(dp->user) != rule->out_l3)
+ return -EOPNOTSUPP;
+ return 0;
+}
+
+static int fa_find(struct bcm_ns_fa *fa, unsigned long cookie)
+{
+ int i;
+
+ for (i = 0; i < FA_SLOTS; i++)
+ if (fa->flows[i] && fa->flows[i]->cookie == cookie)
+ return i;
+ return -ENOENT;
+}
+
+/* Hardware traffic must still drive Linux neighbour reachability probes.
+ * Probe the next hop of every installed entry, rather than relying on the
+ * cache of neighbour updates, which other neighbours can evict.
+ */
+static void fa_neighbour_activity(struct bcm_ns_fa *fa)
+{
+ unsigned int i;
+
+ if (fa->failed || fa->stopping || time_before(jiffies, fa->neighbour_check + HZ))
+ return;
+ fa->neighbour_check = jiffies;
+ for (i = 0; i < FA_SLOTS; i++) {
+ struct fa_flow *entry = fa->flows[i];
+ struct neighbour *neigh;
+
+ if (!entry || !entry->installed)
+ continue;
+ rcu_read_lock();
+ neigh = ipv4_neigh_lookup(entry->out_l3, &entry->gw);
+ rcu_read_unlock();
+ if (neigh) {
+ neigh_event_send(neigh, NULL);
+ neigh_release(neigh);
+ }
+ }
+}
+
+static u32 fa_hits(const u32 data[8])
+{
+ return FIELD_GET(FA_FLOW0_HITS_LO, data[0]) |
+ FIELD_GET(FA_FLOW1_HITS_HI, data[1]) << 29;
+}
+
+static int fa_counter_read(struct bcm_ns_fa *fa, unsigned int index)
+{
+ struct fa_flow *entry = fa->flows[index];
+ u32 data[8], hits, delta;
+ int err;
+
+ err = fa_read(fa, FA_FLOW_TABLE, index, data);
+ if (err) {
+ fa_fail(fa);
+ return err;
+ }
+ hits = fa_hits(data);
+ delta = hits - entry->hits;
+ entry->hits = hits;
+ entry->pending_hits += delta;
+ if (delta) {
+ entry->lastused = jiffies;
+ fa_neighbour_activity(fa);
+ }
+ return 0;
+}
+
+/* A source-MAC guard traps the first packet after a wired client moves,
+ * even if the packet would otherwise match a fully accelerated tuple.
+ * References are directional; the two NAT rows can share one guard.
+ */
+static int fa_client_get_port(struct bcm_ns_fa *fa, const u8 *addr, unsigned int port)
+{
+ int i, free = -1, err;
+
+ for (i = 0; i < BRCM_FA_BRIDGE_GUARDS; i++) {
+ struct fa_client *client = &fa->clients[i];
+
+ if (!client->refs) {
+ if (free < 0)
+ free = i;
+ continue;
+ }
+ if (!ether_addr_equal(client->addr, addr))
+ continue;
+ if (client->port != port)
+ return -EAGAIN;
+ client->refs++;
+ return i + 1;
+ }
+ if (free < 0)
+ return -ENOSPC;
+ err = brcm_fa_bridge_guard(fa->conduit, free, addr, port);
+ if (err) {
+ if (err != -ENODEV)
+ fa_fail(fa);
+ return err;
+ }
+ ether_addr_copy(fa->clients[free].addr, addr);
+ fa->clients[free].port = port;
+ fa->clients[free].refs = 1;
+ return free + 1;
+}
+
+static int fa_client_get(struct bcm_ns_fa *fa, const struct fa_rule *rule,
+ const struct fa_learned *learned)
+{
+ const u8 *addr;
+ struct net_device *bridge, *port;
+ int err;
+
+ if (netif_is_bridge_master(rule->in_l3)) {
+ bridge = rule->in_l3;
+ port = rule->in;
+ addr = rule->ingress_mac;
+ if (!ether_addr_equal(addr, learned->source))
+ return -EAGAIN;
+ } else if (netif_is_bridge_master(rule->out_l3)) {
+ bridge = rule->out_l3;
+ port = rule->out;
+ addr = rule->eth;
+ } else {
+ return 0;
+ }
+ rcu_read_lock();
+ err = fa_bridge_path(bridge, addr, port) ? 0 : -EAGAIN;
+ rcu_read_unlock();
+ if (err)
+ return err;
+ return fa_client_get_port(fa, addr, dsa_port_from_netdev(port)->index);
+}
+
+static void fa_client_put(struct bcm_ns_fa *fa, unsigned int client)
+{
+ if (!client || --fa->clients[client - 1].refs)
+ return;
+ if (brcm_fa_bridge_guard(fa->conduit, client - 1, NULL, 0))
+ fa_fail(fa);
+}
+
+static int fa_uninstall(struct bcm_ns_fa *fa, unsigned int index)
+{
+ struct fa_flow *entry = fa->flows[index];
+ u32 data[8] = {};
+ int err;
+
+ if (!entry->installed)
+ return 0;
+ if (!fa->failed) {
+ /* Preserve the final packet delta for the next flowtable poll. */
+ fa_counter_read(fa, index);
+ if (!fa->failed) {
+ err = fa_write(fa, FA_FLOW_TABLE, index, data);
+ if (err)
+ fa_fail(fa);
+ }
+ if (!fa->failed)
+ fa_mac_put(fa, entry->pool_in, false);
+ if (!fa->failed)
+ fa_mac_put(fa, entry->pool_out, false);
+ if (!fa->failed)
+ fa_mac_put(fa, entry->nh, true);
+ }
+ fa_client_put(fa, entry->client);
+ entry->client = 0;
+ entry->installed = false;
+ if (!--fa->nr_flows && !fa->failed)
+ fa_control_update(fa, FA_CTL_NO_DA_CHECK, 0);
+ return fa->failed ? -EIO : 0;
+}
+
+static void fa_control_state_change(void *priv, bool ready)
+{
+ struct bcm_ns_fa *fa = priv;
+ unsigned int i;
+
+ mutex_lock(&fa->lock);
+ spin_lock_bh(&fa->learn_lock);
+ fa->tcp_ready = ready && !READ_ONCE(fa->stopping);
+ if (!fa->tcp_ready) {
+ fa->control_generation++;
+ fa->invalidate_generation++;
+ memset(fa->learned, 0, sizeof(*fa->learned) * FA_SLOTS);
+ }
+ fa_lookup_update(fa);
+ spin_unlock_bh(&fa->learn_lock);
+ if (!fa->tcp_ready)
+ for (i = 0; i < FA_SLOTS; i++) {
+ struct fa_flow *entry = fa->flows[i];
+
+ if (!entry)
+ continue;
+ set_bit(FA_FLOW_CLOSING, &entry->flags);
+ rcu_read_lock();
+ entry->invalidate(entry->cookie);
+ rcu_read_unlock();
+ fa_uninstall(fa, i);
+ }
+ mutex_unlock(&fa->lock);
+}
+
+static int fa_destroy(struct bcm_ns_fa *fa, unsigned int index)
+{
+ struct fa_flow *entry = fa->flows[index];
+ int err = fa_uninstall(fa, index);
+
+ if (entry->reverse)
+ entry->reverse->reverse = NULL;
+ spin_lock_bh(&fa->learn_lock);
+ list_del_rcu(&entry->list);
+ spin_unlock_bh(&fa->learn_lock);
+ netdev_put(entry->in, &entry->in_tracker);
+ netdev_put(entry->out, &entry->out_tracker);
+ netdev_put(entry->out_l3, &entry->out_l3_tracker);
+ fa->flows[index] = NULL;
+ kfree_rcu(entry, rcu);
+ return err;
+}
+
+/* FIB notifications identify the preferred alias for each table/prefix.
+ * Retain only low-MTU prefixes, rather than dumping the FIB after changes.
+ * Writers, including the registration retry reset, hold learn_lock. fa_replace()
+ * and debugfs read the flags without it, so they are accessed with
+ * READ_ONCE()/WRITE_ONCE().
+ */
+static void fa_mtu_route_update(struct bcm_ns_fa *fa, unsigned long event,
+ const struct fib_entry_notifier_info *info)
+{
+ struct fa_mtu_route *route;
+ u32 key = info->dst ^ info->tb_id ^ info->dst_len;
+ bool low = event != FIB_EVENT_ENTRY_DEL && info->fi &&
+ info->fi->fib_mtu && info->fi->fib_mtu < ETH_DATA_LEN;
+
+ hash_for_each_possible(fa->mtu_routes, route, node, key) {
+ if (route->dst != info->dst || route->tb_id != info->tb_id ||
+ route->prefix_len != info->dst_len)
+ continue;
+ if (!low) {
+ hash_del(&route->node);
+ kfree(route);
+ WRITE_ONCE(fa->nr_mtu_routes, fa->nr_mtu_routes - 1);
+ }
+ goto out;
+ }
+ if (!low)
+ goto out;
+ if (fa->nr_mtu_routes == FA_MTU_ROUTES_MAX) {
+ WRITE_ONCE(fa->mtu_tracking_failed, true);
+ goto out;
+ }
+ route = kzalloc_obj(*route, GFP_ATOMIC);
+ if (!route) {
+ /* Never reject a route because an optional offload ran out of memory. */
+ WRITE_ONCE(fa->mtu_tracking_failed, true);
+ goto out;
+ }
+ route->dst = info->dst;
+ route->tb_id = info->tb_id;
+ route->prefix_len = info->dst_len;
+ hash_add(fa->mtu_routes, &route->node, key);
+ WRITE_ONCE(fa->nr_mtu_routes, fa->nr_mtu_routes + 1);
+out:
+ WRITE_ONCE(fa->route_mtu_ok, !fa->nr_mtu_routes && !fa->mtu_tracking_failed);
+}
+
+static void fa_fib_reset(struct notifier_block *nb)
+{
+ struct bcm_ns_fa *fa = container_of(nb, struct bcm_ns_fa, fib_nb);
+ struct fa_mtu_route *route;
+ struct hlist_node *tmp;
+ unsigned int bucket;
+
+ spin_lock_bh(&fa->learn_lock);
+ hash_for_each_safe(fa->mtu_routes, bucket, tmp, route, node) {
+ hash_del(&route->node);
+ kfree(route);
+ }
+ WRITE_ONCE(fa->nr_mtu_routes, 0);
+ WRITE_ONCE(fa->mtu_tracking_failed, false);
+ WRITE_ONCE(fa->route_mtu_ok, true);
+ spin_unlock_bh(&fa->learn_lock);
+}
+
+/* Caller holds RCU and learn_lock. Stop hardware before notifying owners. */
+static void fa_invalidate_locked(struct bcm_ns_fa *fa)
+{
+ struct fa_flow *entry;
+
+ fa->control_generation++;
+ fa->pending_generation++;
+ fa->control_pending = true;
+ fa->invalidations++;
+ fa->invalidate_generation++;
+ fa_lookup_update(fa);
+ list_for_each_entry_rcu(entry, &fa->entries, list) {
+ set_bit(FA_FLOW_CLOSING, &entry->flags);
+ entry->invalidate(entry->cookie);
+ }
+ queue_work(system_dfl_wq, &fa->control_work);
+}
+
+static void fa_invalidate(struct bcm_ns_fa *fa)
+{
+ rcu_read_lock();
+ spin_lock_bh(&fa->learn_lock);
+ if (!fa->stopping)
+ fa_invalidate_locked(fa);
+ spin_unlock_bh(&fa->learn_lock);
+ rcu_read_unlock();
+}
+
+/* FDB changes are notified before a learned client moves or disappears.
+ * Gate the parser synchronously; table retirement itself is deferred.
+ */
+static int fa_switchdev_event(struct bcm_ns_fa *fa, unsigned long event, void *ptr)
+{
+ struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
+ struct net_device *bridge;
+
+ switch (event) {
+ case SWITCHDEV_PORT_ATTR_SET:
+ case SWITCHDEV_PORT_OBJ_ADD:
+ case SWITCHDEV_PORT_OBJ_DEL:
+ break;
+ default:
+ return NOTIFY_DONE;
+ }
+ rcu_read_lock();
+ bridge = netif_is_bridge_master(dev) ? dev : netdev_master_upper_dev_get_rcu(dev);
+ if (dev_net(dev) == &init_net &&
+ (fa_switch_port(fa, dev) || (bridge && fa_bridge(fa, bridge))))
+ fa_invalidate(fa);
+ rcu_read_unlock();
+ return NOTIFY_DONE;
+}
+
+static int fa_switchdev_atomic(struct notifier_block *nb, unsigned long event, void *ptr)
+{
+ struct bcm_ns_fa *fa = container_of(nb, struct bcm_ns_fa, switchdev_nb);
+
+ return fa_switchdev_event(fa, event, ptr);
+}
+
+static int fa_switchdev_blocking(struct notifier_block *nb, unsigned long event, void *ptr)
+{
+ struct bcm_ns_fa *fa = container_of(nb, struct bcm_ns_fa, switchdev_blocking_nb);
+
+ return fa_switchdev_event(fa, event, ptr);
+}
+
+static int fa_fib_event(struct notifier_block *nb, unsigned long event, void *ptr)
+{
+ struct bcm_ns_fa *fa = container_of(nb, struct bcm_ns_fa, fib_nb);
+ struct fib_notifier_info *info = ptr;
+ bool route = false;
+
+ if (info->family != AF_INET)
+ return NOTIFY_DONE;
+ /* Deliberately invalidate all FA-owned flows for every IPv4 route,
+ * rule or nexthop change, including unrelated and local-table routes.
+ * The offload rule lacks the complete policy context needed to prove
+ * that an event cannot affect an installed path.
+ */
+ switch (event) {
+ case FIB_EVENT_ENTRY_REPLACE:
+ case FIB_EVENT_ENTRY_ADD:
+ case FIB_EVENT_ENTRY_APPEND:
+ case FIB_EVENT_ENTRY_DEL:
+ route = true;
+ break;
+ case FIB_EVENT_RULE_ADD:
+ case FIB_EVENT_RULE_DEL:
+ case FIB_EVENT_NH_ADD:
+ case FIB_EVENT_NH_DEL:
+ break;
+ default:
+ return NOTIFY_DONE;
+ }
+ rcu_read_lock();
+ spin_lock_bh(&fa->learn_lock);
+ if (!fa->stopping) {
+ if (route)
+ fa_mtu_route_update(fa, event, ptr);
+ fa_invalidate_locked(fa);
+ }
+ spin_unlock_bh(&fa->learn_lock);
+ rcu_read_unlock();
+ return NOTIFY_DONE;
+}
+
+static int fa_netevent(struct notifier_block *nb, unsigned long event, void *ptr)
+{
+ struct bcm_ns_fa *fa = container_of(nb, struct bcm_ns_fa, netevent_nb);
+ struct fa_neighbour *cached = NULL;
+ struct neighbour *neighbour = ptr;
+ bool invalidate = false;
+ unsigned int i;
+ char mac[ETH_ALEN];
+ __be32 address;
+ u8 state;
+
+ if (event == NETEVENT_BRIDGE_FDB_UPDATE) {
+ struct net_device *dev = ptr;
+
+ rcu_read_lock();
+ if (dev_net(dev) == &init_net && fa_bridge(fa, dev))
+ fa_invalidate(fa);
+ rcu_read_unlock();
+ return NOTIFY_DONE;
+ }
+ if (event == NETEVENT_IPV4_MPATH_HASH_UPDATE ||
+ event == NETEVENT_IPV4_FWD_UPDATE_PRIORITY_UPDATE) {
+ if (ptr == &init_net)
+ fa_invalidate(fa);
+ return NOTIFY_DONE;
+ }
+ if (event == NETEVENT_REDIRECT) {
+ struct netevent_redirect *redirect = ptr;
+
+ if (net_eq(dev_net(redirect->new->dev), &init_net))
+ fa_invalidate(fa);
+ return NOTIFY_DONE;
+ }
+ if (event != NETEVENT_NEIGH_UPDATE || neighbour->tbl->family != AF_INET ||
+ (!fa_port(fa, neighbour->dev) && !fa_bridge(fa, neighbour->dev)))
+ return NOTIFY_DONE;
+ state = READ_ONCE(neighbour->nud_state);
+ address = get_unaligned((__be32 *)neighbour->primary_key);
+ neigh_ha_snapshot(mac, neighbour, neighbour->dev);
+ spin_lock_bh(&fa->learn_lock);
+ for (i = 0; i < ARRAY_SIZE(fa->neighbours); i++) {
+ struct fa_neighbour *candidate = &fa->neighbours[i];
+
+ if (candidate->valid && candidate->ifindex == neighbour->dev->ifindex &&
+ candidate->address == address) {
+ cached = candidate;
+ invalidate = !(state & NUD_VALID) || memcmp(candidate->mac, mac, ETH_ALEN);
+ break;
+ }
+ }
+ if (!cached) {
+ cached = &fa->neighbours[fa->neighbour_next++ % FA_NEXTHOPS];
+ invalidate = cached->valid || !(state & NUD_VALID) || !list_empty(&fa->entries);
+ }
+ *cached = (struct fa_neighbour) {
+ .address = address, .ifindex = neighbour->dev->ifindex,
+ .state = state, .valid = true,
+ };
+ memcpy(cached->mac, mac, ETH_ALEN);
+ spin_unlock_bh(&fa->learn_lock);
+ if (invalidate)
+ fa_invalidate(fa);
+ return NOTIFY_DONE;
+}
+
+static void fa_control_work(struct work_struct *work)
+{
+ struct bcm_ns_fa *fa = container_of(work, struct bcm_ns_fa, control_work);
+ unsigned long generation;
+ unsigned int i;
+
+ mutex_lock(&fa->lock);
+ for (;;) {
+ spin_lock_bh(&fa->learn_lock);
+ generation = fa->pending_generation;
+ spin_unlock_bh(&fa->learn_lock);
+ for (i = 0; i < FA_SLOTS; i++) {
+ struct fa_flow *entry = fa->flows[i];
+
+ if (entry && test_bit(FA_FLOW_CLOSING, &entry->flags) && entry->reverse)
+ set_bit(FA_FLOW_CLOSING, &entry->reverse->flags);
+ }
+ for (i = 0; i < FA_SLOTS; i++)
+ if (fa->flows[i] && test_bit(FA_FLOW_CLOSING, &fa->flows[i]->flags))
+ fa_uninstall(fa, i);
+ spin_lock_bh(&fa->learn_lock);
+ if (generation == fa->pending_generation) {
+ memset(fa->learned, 0, sizeof(*fa->learned) * FA_SLOTS);
+ fa->control_pending = false;
+ fa_lookup_update(fa);
+ spin_unlock_bh(&fa->learn_lock);
+ break;
+ }
+ spin_unlock_bh(&fa->learn_lock);
+ }
+ mutex_unlock(&fa->lock);
+}
+
+static void fa_encode(const struct fa_rule *rule, unsigned int pool,
+ unsigned int nh, unsigned int port, u32 data[8])
+{
+ data[1] = FIELD_PREP(FA_FLOW1_ACTION, rule->action) |
+ FIELD_PREP(FA_FLOW1_ROUTER_MAC, pool) |
+ FIELD_PREP(FA_FLOW1_NEXT_HOP, nh) |
+ FIELD_PREP(FA_FLOW1_REMAP_IP_LO,
+ rule->remap_ip & FIELD_MAX(FA_FLOW1_REMAP_IP_LO));
+ data[2] = FIELD_PREP(FA_FLOW2_REMAP_IP_HI, rule->remap_ip >> 17) |
+ FIELD_PREP(FA_FLOW2_REMAP_PORT, rule->remap_port) |
+ FIELD_PREP(FA_FLOW2_DPORT_LO,
+ be16_to_cpu(rule->tuple.dport) & FIELD_MAX(FA_FLOW2_DPORT_LO));
+ data[3] = FIELD_PREP(FA_FLOW3_DPORT_HI, be16_to_cpu(rule->tuple.dport) >> 1) |
+ FIELD_PREP(FA_FLOW3_SPORT, be16_to_cpu(rule->tuple.sport)) |
+ (rule->l4proto == IPPROTO_TCP ? FA_FLOW3_TCP : 0);
+ data[4] = be32_to_cpu(rule->tuple.dst);
+ data[5] = be32_to_cpu(rule->tuple.src);
+ /* Direct to UniMAC2; ingress opcode 1, one destination switch port. */
+ data[6] = FA_FLOW6_VALID | FIELD_PREP(FA_FLOW6_DIRECTION, rule->direction) |
+ FIELD_PREP(FA_FLOW6_OPCODE, FA_FLOW6_OPCODE_INGRESS) |
+ FIELD_PREP(FA_FLOW6_DEST_MAP, BIT(port));
+ data[7] = FA_FLOW7_IPV4;
+}
+
+static int fa_route_check(struct fa_rule *rule);
+
+static int fa_insert(struct bcm_ns_fa *fa, struct flow_cls_offload *cls,
+ const struct fa_rule *parsed)
+{
+ struct fa_rule rule = *parsed;
+ struct fa_learned learned;
+ struct fa_flow *entry;
+ u32 data[8] = {};
+ int i, slot = -1, pi, po, nh, client, err;
+
+ if (fa->failed)
+ return -EIO;
+ /* A shared block may deliver the same cookie through both ports. */
+ i = fa_find(fa, cls->cookie);
+ if (i >= 0)
+ return test_bit(FA_FLOW_CLOSING, &fa->flows[i]->flags) ? -EOPNOTSUPP : 0;
+ err = fa_learn_lookup(fa, &rule, &learned);
+ if (err)
+ return err;
+ for (i = learned.hash * FA_WAYS; i < (learned.hash + 1) * FA_WAYS; i++) {
+ if (fa->flows[i]) {
+ if (fa->flows[i]->installed && fa->flows[i]->l4proto == rule.l4proto &&
+ !memcmp(&fa->flows[i]->tuple, &rule.tuple, sizeof(rule.tuple)))
+ return -EEXIST;
+ } else if (slot < 0) {
+ slot = i;
+ }
+ }
+ if (slot < 0)
+ return -ENOSPC;
+ entry = kzalloc_obj(*entry);
+ if (!entry)
+ return -ENOMEM;
+ pi = fa_mac_get(fa, learned.mac, rule.direction, false);
+ if (pi < 0) {
+ err = pi;
+ goto free;
+ }
+ po = fa_mac_get(fa, rule.eth + ETH_ALEN, !rule.direction, false);
+ if (po < 0) {
+ err = po;
+ goto put_in;
+ }
+ nh = fa_mac_get(fa, rule.eth, false, true);
+ if (nh < 0) {
+ err = nh;
+ goto put_out;
+ }
+
+ client = fa_client_get(fa, &rule, &learned);
+ if (client < 0) {
+ err = client;
+ goto put_nh;
+ }
+ entry->client = client;
+ entry->cookie = cls->cookie;
+ entry->gw = rule.gw;
+ entry->invalidate = cls->invalidate;
+ entry->tuple = rule.tuple;
+ entry->l4proto = rule.l4proto;
+ entry->pool_in = pi;
+ entry->pool_out = po;
+ entry->nh = nh;
+ entry->in = rule.in;
+ entry->out = rule.out;
+ entry->out_l3 = rule.out_l3;
+ entry->in_port = dsa_port_from_netdev(rule.in)->index;
+ entry->out_port = dsa_port_from_netdev(rule.out)->index;
+ fa_reply_tuple(&rule, &entry->reply);
+ netdev_hold(entry->in, &entry->in_tracker, GFP_KERNEL);
+ netdev_hold(entry->out, &entry->out_tracker, GFP_KERNEL);
+ netdev_hold(entry->out_l3, &entry->out_l3_tracker, GFP_KERNEL);
+ spin_lock_bh(&fa->learn_lock);
+ if (learned.generation != (rule.l4proto == IPPROTO_TCP ? fa->control_generation :
+ fa->invalidate_generation) || fa->control_pending) {
+ spin_unlock_bh(&fa->learn_lock);
+ netdev_put(entry->in, &entry->in_tracker);
+ netdev_put(entry->out, &entry->out_tracker);
+ netdev_put(entry->out_l3, &entry->out_l3_tracker);
+ fa_client_put(fa, entry->client);
+ err = -EAGAIN;
+ goto put_nh;
+ }
+ list_add_rcu(&entry->list, &fa->entries);
+ spin_unlock_bh(&fa->learn_lock);
+ /* Entries are published now, so a later neighbour update retires this
+ * one. Check that the neighbour did not change before it was published.
+ */
+ rcu_read_lock();
+ err = fa_route_check(&rule);
+ rcu_read_unlock();
+ if (err) {
+ spin_lock_bh(&fa->learn_lock);
+ list_del_rcu(&entry->list);
+ spin_unlock_bh(&fa->learn_lock);
+ netdev_put(entry->in, &entry->in_tracker);
+ netdev_put(entry->out, &entry->out_tracker);
+ netdev_put(entry->out_l3, &entry->out_l3_tracker);
+ fa_client_put(fa, entry->client);
+ if (!fa->failed) {
+ fa_mac_put(fa, nh, true);
+ fa_mac_put(fa, po, false);
+ fa_mac_put(fa, pi, false);
+ }
+ kfree_rcu(entry, rcu);
+ return -EAGAIN;
+ }
+ fa_encode(&rule, po, nh, entry->out_port, data);
+ /* Publish to NAPI before committing: a concurrent control trap must
+ * stop lookups even if it arrives while this entry is being written.
+ */
+ fa_control_update(fa, 0, FA_CTL_NO_DA_CHECK);
+ err = fa_write(fa, FA_FLOW_TABLE, slot, data);
+ if (err) {
+ fa_fail(fa);
+ spin_lock_bh(&fa->learn_lock);
+ list_del_rcu(&entry->list);
+ spin_unlock_bh(&fa->learn_lock);
+ netdev_put(entry->in, &entry->in_tracker);
+ netdev_put(entry->out, &entry->out_tracker);
+ netdev_put(entry->out_l3, &entry->out_l3_tracker);
+ fa_client_put(fa, entry->client);
+ kfree_rcu(entry, rcu);
+ return err;
+ }
+ entry->installed = true;
+ if (rule.l4proto == IPPROTO_TCP) {
+ struct fa_flow *other;
+
+ /* Pair with the other direction even if it is being retired:
+ * the connection is then closing, and so is this entry.
+ */
+ list_for_each_entry(other, &fa->entries, list) {
+ if (other == entry || other->l4proto != IPPROTO_TCP || other->reverse ||
+ other->in_port != entry->out_port ||
+ other->out_port != entry->in_port ||
+ memcmp(&other->tuple, &entry->reply, sizeof(other->tuple)) ||
+ memcmp(&other->reply, &entry->tuple, sizeof(other->reply)))
+ continue;
+ entry->reverse = other;
+ other->reverse = entry;
+ if (test_bit(FA_FLOW_CLOSING, &other->flags)) {
+ set_bit(FA_FLOW_CLOSING, &entry->flags);
+ spin_lock_bh(&fa->learn_lock);
+ fa->control_generation++;
+ fa_control_kick(fa);
+ spin_unlock_bh(&fa->learn_lock);
+ }
+ break;
+ }
+ }
+ entry->lastused = jiffies;
+ fa->flows[slot] = entry;
+ fa->nr_flows++;
+ return 0;
+put_nh:
+ if (!fa->failed)
+ fa_mac_put(fa, nh, true);
+put_out:
+ if (!fa->failed)
+ fa_mac_put(fa, po, false);
+put_in:
+ if (!fa->failed)
+ fa_mac_put(fa, pi, false);
+free:
+ kfree(entry);
+ return err;
+}
+
+static int fa_route_supported(const struct fa_rule *rule, const struct fib_result *res)
+{
+ const struct fib_nh_common *nhc;
+
+ if (res->type != RTN_UNICAST || !res->fi)
+ return -EOPNOTSUPP;
+ /* These paths may be valid software flowtable routes. Reject hardware
+ * admission without tearing down their software owner on every retry.
+ */
+ if (res->fi->nh || res->fi->fib_nhs != 1 ||
+ (res->fi->fib_mtu && res->fi->fib_mtu < ETH_DATA_LEN))
+ return -EOPNOTSUPP;
+ nhc = FIB_RES_NHC(*res);
+ if (nhc->nhc_lwtstate || (nhc->nhc_gw_family && nhc->nhc_gw_family != AF_INET))
+ return -EOPNOTSUPP;
+ return FIB_RES_DEV(*res) == rule->out_l3 ? 0 : -EOPNOTSUPP;
+}
+
+/* This lookup is only a conservative hardware eligibility probe. The
+ * classifier lacks the owner's full mark/DSCP/oif context, so a mismatch
+ * cannot prove that a cached software path is stale. The owner validates
+ * its actual cached route separately. Caller holds RCU.
+ */
+static int fa_route_check(struct fa_rule *rule)
+{
+ struct flowi4 fl4 = {
+ .flowi4_iif = rule->in_l3->ifindex,
+ .flowi4_proto = rule->l4proto,
+ .saddr = rule->tuple.src,
+ .daddr = rule->tuple.dst,
+ .fl4_sport = rule->tuple.sport,
+ .fl4_dport = rule->tuple.dport,
+ };
+ struct fib_result res = {};
+ struct fib_nh_common *nhc;
+ struct neighbour *neigh;
+ u8 mac[ETH_ALEN];
+ bool valid;
+ __be32 gw;
+ int err;
+
+ if (rule->direction == FA_DIR_DNAT) {
+ if (rule->action & FA_ACT_REMAP_IP)
+ fl4.daddr = htonl(rule->remap_ip);
+ if (rule->action & FA_ACT_REMAP_PORT)
+ fl4.fl4_dport = htons(rule->remap_port);
+ }
+ if (fib_lookup(&init_net, &fl4, &res, 0))
+ return -EOPNOTSUPP;
+ err = fa_route_supported(rule, &res);
+ if (err)
+ return err;
+ nhc = FIB_RES_NHC(res);
+ gw = nhc->nhc_gw_family == AF_INET ? nhc->nhc_gw.ipv4 : fl4.daddr;
+ rule->gw = gw;
+ neigh = ipv4_neigh_lookup(rule->out_l3, &gw);
+ if (!neigh)
+ return -EOPNOTSUPP;
+ neigh_ha_snapshot(mac, neigh, rule->out_l3);
+ valid = (READ_ONCE(neigh->nud_state) & NUD_VALID) && ether_addr_equal(mac, rule->eth);
+ if (valid && netif_is_bridge_master(rule->out_l3))
+ valid = fa_bridge_path(rule->out_l3, mac, rule->out);
+ neigh_release(neigh);
+ return valid ? 0 : -EOPNOTSUPP;
+}
+
+static int fa_replace(struct bcm_ns_fa *fa, struct flow_cls_offload *cls)
+{
+ struct fa_rule rule = {};
+ netdevice_tracker in_tracker, out_tracker, in_l3_tracker, out_l3_tracker;
+ int err;
+
+ if (!cls->invalidate || !cls->validate || !fa->enabled ||
+ !READ_ONCE(fa->route_mtu_ok) ||
+ READ_ONCE(fa->control_pending))
+ return -EOPNOTSUPP;
+ rcu_read_lock();
+ err = fa_parse(fa, cls, &rule);
+ if (!err)
+ err = cls->validate(cls->cookie, rule.ingress_mac);
+ if (err == -ESTALE) {
+ cls->invalidate(cls->cookie);
+ err = -EOPNOTSUPP;
+ }
+ if (!err)
+ err = fa_route_check(&rule);
+ if (!err) {
+ netdev_hold(rule.in, &in_tracker, GFP_ATOMIC);
+ netdev_hold(rule.out, &out_tracker, GFP_ATOMIC);
+ netdev_hold(rule.in_l3, &in_l3_tracker, GFP_ATOMIC);
+ netdev_hold(rule.out_l3, &out_l3_tracker, GFP_ATOMIC);
+ }
+ rcu_read_unlock();
+ if (err) {
+ NL_SET_ERR_MSG_MOD(cls->common.extack,
+ "FA requires IPv4 TCP/UDP NAT between a WAN and an untagged LAN domain");
+ return err;
+ }
+ err = fa_insert(fa, cls, &rule);
+ netdev_put(rule.in, &in_tracker);
+ netdev_put(rule.out, &out_tracker);
+ netdev_put(rule.in_l3, &in_l3_tracker);
+ netdev_put(rule.out_l3, &out_l3_tracker);
+ return err;
+}
+
+static int fa_stats(struct bcm_ns_fa *fa, struct flow_cls_offload *cls, int slot)
+{
+ struct fa_flow *entry = fa->flows[slot];
+ int err;
+
+ if (fa->failed)
+ return -EIO;
+ if (entry->installed) {
+ err = fa_counter_read(fa, slot);
+ if (err)
+ return err;
+ }
+ /* Hardware clear-on-read gives unreliable hit counts. Read cumulative
+ * packet counters and compute wrapping deltas in fa_counter_read().
+ * Retired TCP rows retain their final delta until it is reported here.
+ */
+ flow_stats_update(&cls->stats, 0, entry->pending_hits, 0, entry->lastused,
+ FLOW_ACTION_HW_STATS_DELAYED);
+ entry->pending_hits = 0;
+ return 0;
+}
+
+static int fa_block_cb(enum tc_setup_type type, void *type_data, void *cb_priv)
+{
+ struct bcm_ns_fa *fa = cb_priv;
+ struct flow_cls_offload *cls = type_data;
+ int slot, err;
+
+ if (type != TC_SETUP_CLSFLOWER)
+ return -EOPNOTSUPP;
+ mutex_lock(&fa->lock);
+ slot = fa_find(fa, cls->cookie);
+ switch (cls->command) {
+ case FLOW_CLS_REPLACE:
+ err = fa_replace(fa, cls);
+ break;
+ case FLOW_CLS_DESTROY:
+ err = slot < 0 ? slot : fa_destroy(fa, slot);
+ break;
+ case FLOW_CLS_STATS:
+ err = slot < 0 ? slot : fa_stats(fa, cls, slot);
+ break;
+ default:
+ err = -EOPNOTSUPP;
+ }
+ mutex_unlock(&fa->lock);
+ return err;
+}
+
+int bcm_ns_fa_setup_tc(struct bcm_ns_fa *fa, enum tc_setup_type type, void *data)
+{
+ struct flow_block_offload *block = data;
+ struct flow_block_cb *cb;
+
+ int err;
+
+ /* Rules are only accepted for the initial namespace, which the one
+ * hardware table cannot be shared with.
+ */
+ if (!fa || type != TC_SETUP_FT || !net_eq(block->net, &init_net) ||
+ block->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
+ return -EOPNOTSUPP;
+ block->driver_block_list = &fa->block_list;
+ /* DSA forwards each user-port bind to the same conduit. Keep one
+ * callback per flowtable, with a reference for each bound user port.
+ */
+ switch (block->command) {
+ case FLOW_BLOCK_BIND:
+ mutex_lock(&fa->lock);
+ cb = flow_block_cb_lookup(block->block, fa_block_cb, fa);
+ if (cb) {
+ flow_block_cb_incref(cb);
+ mutex_unlock(&fa->lock);
+ return 0;
+ }
+ /* Different flowtables have independent cookies and policy. The
+ * hardware cannot partition tables by ingress port.
+ */
+ if (!list_empty(&fa->block_list)) {
+ mutex_unlock(&fa->lock);
+ return -EBUSY;
+ }
+ cb = flow_block_cb_alloc(fa_block_cb, fa, fa, NULL);
+ if (IS_ERR(cb)) {
+ err = PTR_ERR(cb);
+ mutex_unlock(&fa->lock);
+ return err;
+ }
+ flow_block_cb_incref(cb);
+ flow_block_cb_add(cb, block);
+ list_add_tail(&cb->driver_list, &fa->block_list);
+ mutex_unlock(&fa->lock);
+ return 0;
+ case FLOW_BLOCK_UNBIND:
+ mutex_lock(&fa->lock);
+ cb = flow_block_cb_lookup(block->block, fa_block_cb, fa);
+ mutex_unlock(&fa->lock);
+ if (!cb)
+ return -ENOENT;
+ bcm_ns_fa_flush(fa);
+ mutex_lock(&fa->lock);
+ if (!flow_block_cb_decref(cb)) {
+ flow_block_cb_remove(cb, block);
+ list_del(&cb->driver_list);
+ }
+ mutex_unlock(&fa->lock);
+ return 0;
+ default:
+ return -EOPNOTSUPP;
+ }
+}
+EXPORT_SYMBOL_GPL(bcm_ns_fa_setup_tc);
+
+void bcm_ns_fa_flush(struct bcm_ns_fa *fa)
+{
+ int i;
+
+ if (!fa)
+ return;
+ mutex_lock(&fa->lock);
+ for (i = 0; i < FA_SLOTS; i++) {
+ if (fa->flows[i]) {
+ rcu_read_lock();
+ fa->flows[i]->invalidate(fa->flows[i]->cookie);
+ rcu_read_unlock();
+ fa_destroy(fa, i);
+ }
+ }
+ spin_lock_bh(&fa->learn_lock);
+ memset(fa->learned, 0, sizeof(*fa->learned) * FA_SLOTS);
+ spin_unlock_bh(&fa->learn_lock);
+ mutex_unlock(&fa->lock);
+}
+EXPORT_SYMBOL_GPL(bcm_ns_fa_flush);
+
+void bcm_ns_fa_set_offload(struct bcm_ns_fa *fa, bool enable)
+{
+ if (!fa)
+ return;
+ mutex_lock(&fa->lock);
+ fa->enabled = enable;
+ spin_lock_bh(&fa->learn_lock);
+ fa_lookup_update(fa);
+ spin_unlock_bh(&fa->learn_lock);
+ mutex_unlock(&fa->lock);
+ if (!enable)
+ bcm_ns_fa_flush(fa);
+}
+EXPORT_SYMBOL_GPL(bcm_ns_fa_set_offload);
+
+static int fa_netdev_event(struct notifier_block *nb, unsigned long event, void *ptr)
+{
+ struct bcm_ns_fa *fa = container_of(nb, struct bcm_ns_fa, netdev_nb);
+ struct net_device *dev = netdev_notifier_info_to_dev(ptr);
+
+ /* New active ports or upper devices change the lookup domain. Flush
+ * synchronously before the new topology becomes usable: a port can
+ * receive as soon as it is opened, before NETDEV_UP.
+ */
+ if (event != NETDEV_GOING_DOWN && event != NETDEV_DOWN &&
+ event != NETDEV_PRE_CHANGEADDR &&
+ event != NETDEV_CHANGEADDR &&
+ event != NETDEV_CHANGE &&
+ event != NETDEV_CHANGEMTU &&
+ event != NETDEV_PRECHANGEUPPER && event != NETDEV_CHANGEUPPER &&
+ event != NETDEV_PRE_UP && event != NETDEV_UP &&
+ event != NETDEV_UNREGISTER)
+ return NOTIFY_DONE;
+ if ((dev_net(dev) == &init_net &&
+ (event == NETDEV_PRECHANGEUPPER || event == NETDEV_CHANGEUPPER ||
+ netif_is_bridge_master(dev))) ||
+ dev == fa->conduit || dev == fa_control_conduit(fa) ||
+ (dsa_user_dev_check(dev) &&
+ dsa_port_to_conduit(dsa_port_from_netdev(dev)) == fa->conduit))
+ bcm_ns_fa_flush(fa);
+ return NOTIFY_DONE;
+}
+
+static int fa_debug_show(struct seq_file *s, void *unused)
+{
+ struct bcm_ns_fa *fa = s->private;
+ unsigned int count = 0, guards = 0;
+ int i;
+
+ mutex_lock(&fa->lock);
+ seq_printf(s, "failed %u\nhits %u\nmisses %u\nerrors %#x\n",
+ fa->failed, readl(fa->regs + FA_HITS),
+ readl(fa->regs + FA_MISSES), readl(fa->regs + FA_ERROR));
+ seq_printf(s, "tcp_ready %u\n", fa->tcp_ready);
+ seq_printf(s, "route_mtu_ok %u\ninvalidations %lu\n", READ_ONCE(fa->route_mtu_ok),
+ READ_ONCE(fa->invalidations));
+ seq_printf(s, "mtu_routes %u\nmtu_tracking_failed %u\n",
+ READ_ONCE(fa->nr_mtu_routes), READ_ONCE(fa->mtu_tracking_failed));
+ seq_printf(s, "fault_injections %lu\n", fa->fault_injections);
+ for (i = 0; i < BRCM_FA_BRIDGE_GUARDS; i++)
+ guards += !!fa->clients[i].refs;
+ seq_printf(s, "bridge_guards %u\n", guards);
+ seq_puts(s, "slot cookie ingress egress packets\n");
+ for (i = 0; i < FA_SLOTS; i++) {
+ struct fa_flow *entry = fa->flows[i];
+ u32 data[8], hits = 0;
+
+ if (!entry || !entry->installed)
+ continue;
+ count++;
+ if (!fa->failed) {
+ if (fa_read(fa, FA_FLOW_TABLE, i, data))
+ fa_fail(fa);
+ else
+ hits = fa_hits(data);
+ }
+ seq_printf(s, "%u %p %s %s %u\n", i, (void *)entry->cookie,
+ entry->in->name, entry->out->name, hits);
+ }
+ seq_printf(s, "entries %u\n", count);
+ mutex_unlock(&fa->lock);
+ return 0;
+}
+DEFINE_SHOW_ATTRIBUTE(fa_debug);
+
+static int fa_table_show(struct seq_file *s, void *unused)
+{
+ struct bcm_ns_fa *fa = s->private;
+ unsigned int i;
+
+ mutex_lock(&fa->lock);
+ seq_printf(s, "control %#x napt %#x mem_control %#x\n",
+ readl(fa->regs + FA_CTL), readl(fa->regs + FA_NAPT),
+ readl(fa->regs + FA_MEM_CTL));
+ for (i = 0; i < FA_SLOTS; i++) {
+ struct fa_flow *entry = fa->flows[i];
+ u32 data[8] = {};
+
+ if (!entry || !entry->installed)
+ continue;
+ seq_printf(s, "%u cookie %#lx tuple %pI4:%u %pI4:%u proto %s\n", i,
+ entry->cookie, &entry->tuple.src, ntohs(entry->tuple.sport),
+ &entry->tuple.dst, ntohs(entry->tuple.dport),
+ entry->l4proto == IPPROTO_TCP ? "tcp" : "udp");
+ if (fa->failed)
+ continue;
+ if (fa_read(fa, FA_FLOW_TABLE, i, data)) {
+ fa_fail(fa);
+ continue;
+ }
+ seq_printf(s, "raw %u %08x %08x %08x %08x %08x %08x %08x %08x\n",
+ i, data[7], data[6], data[5], data[4], data[3], data[2],
+ data[1], data[0]);
+ }
+ mutex_unlock(&fa->lock);
+ return 0;
+}
+DEFINE_SHOW_ATTRIBUTE(fa_table);
+
+static void fa_release(void *ptr)
+{
+ struct bcm_ns_fa *fa = ptr;
+
+ spin_lock_bh(&fa->learn_lock);
+ fa->stopping = true;
+ writel(readl(fa->regs + FA_CTL) | FA_CTL_BYPASS, fa->regs + FA_CTL);
+ readl(fa->regs + FA_CTL);
+ spin_unlock_bh(&fa->learn_lock);
+ brcm_fa_control_unregister(&fa->control);
+ unregister_fib_notifier(&init_net, &fa->fib_nb);
+ fa_fib_reset(&fa->fib_nb);
+ unregister_netevent_notifier(&fa->netevent_nb);
+ unregister_switchdev_blocking_notifier(&fa->switchdev_blocking_nb);
+ unregister_switchdev_notifier(&fa->switchdev_nb);
+ cancel_work_sync(&fa->control_work);
+ debugfs_remove_recursive(fa->debugfs);
+ unregister_netdevice_notifier(&fa->netdev_nb);
+ bcm_ns_fa_flush(fa);
+}
+
+struct bcm_ns_fa *bcm_ns_fa_init(struct net_device *dev)
+{
+ struct device *parent = dev->dev.parent;
+ struct device_node *np;
+ struct resource res;
+ struct bcm_ns_fa *fa;
+ u32 val;
+ int err;
+
+ np = of_parse_phandle(parent->of_node, "brcm,flow-accelerator", 0);
+ if (!np)
+ return NULL;
+ if (!of_device_is_available(np)) {
+ of_node_put(np);
+ return NULL;
+ }
+ if (!of_device_is_compatible(np, "brcm,bcm4708-fa") &&
+ !of_device_is_compatible(np, "brcm,bcm53011-fa") &&
+ !of_device_is_compatible(np, "brcm,bcm53012-fa")) {
+ of_node_put(np);
+ return ERR_PTR(-ENODEV);
+ }
+ err = of_address_to_resource(np, 0, &res);
+ of_node_put(np);
+ if (err)
+ return ERR_PTR(err);
+ if (resource_size(&res) < FA_DATA(7) + sizeof(u32))
+ return ERR_PTR(-EINVAL);
+ fa = devm_kzalloc(parent, sizeof(*fa), GFP_KERNEL);
+ if (!fa)
+ return ERR_PTR(-ENOMEM);
+ fa->learned = devm_kcalloc(parent, FA_SLOTS, sizeof(*fa->learned), GFP_KERNEL);
+ if (!fa->learned)
+ return ERR_PTR(-ENOMEM);
+ fa->regs = devm_ioremap_resource(parent, &res);
+ if (IS_ERR(fa->regs))
+ return ERR_CAST(fa->regs);
+ fa->conduit = dev;
+ fa->fail_table = fa_fail_table_default;
+#ifdef CONFIG_FAULT_INJECTION
+ fa->fail_table.verbose = 0;
+#endif
+ fa->enabled = true;
+ fa->neighbour_check = jiffies - HZ;
+ mutex_init(&fa->lock);
+ spin_lock_init(&fa->learn_lock);
+ INIT_LIST_HEAD(&fa->block_list);
+ INIT_LIST_HEAD(&fa->entries);
+ INIT_WORK(&fa->control_work, fa_control_work);
+ hash_init(fa->mtu_routes);
+ WRITE_ONCE(fa->route_mtu_ok, true);
+
+ /* DA validation is enabled before publishing the first flow.
+ * Mask interrupts: initialization and indirect accesses use bounded polls.
+ */
+ writel(0, fa->regs + FA_STATUS_MASK);
+ writel(0, fa->regs + FA_ERROR_MASK);
+ writel(FA_INIT_DONE, fa->regs + FA_STATUS);
+ val = FA_CTL_BYPASS | FA_CTL_CRC_OVERWRITE |
+ FIELD_PREP(FA_CTL_THRESHOLD, 0x140);
+ writel(val | FA_CTL_INIT, fa->regs + FA_CTL);
+ err = readl_poll_timeout(fa->regs + FA_STATUS, val,
+ (val & FA_INIT_DONE) == FA_INIT_DONE, 100, 50000);
+ if (err)
+ return ERR_PTR(err);
+ /* The pool has no init command of its own. Clear stale firmware state. */
+ for (val = 0; val < FA_POOLS; val++) {
+ u32 zero[8] = {};
+
+ err = fa_write(fa, FA_POOL_TABLE, val, zero);
+ if (err)
+ return ERR_PTR(err);
+ }
+ /* Standalone traffic carries flooding reasons. TCP control packets
+ * use the auxiliary CPU port and physically bypass this inline path.
+ */
+ writel(FA_BRCM_HW | FA_BRCM_RX | FA_BRCM_TX | FA_BRCM_PARSE_EXCEPTIONS,
+ fa->regs + FA_BRCM_HDR);
+ writel(0, fa->regs + FA_L2_SKIP);
+ /* Do not skip VLAN tags: tagged traffic must miss even if its inner
+ * tuple equals an installed untagged flow.
+ */
+ writel(0, fa->regs + FA_L2_TAG);
+ writel(ETH_P_IP | ETH_P_IPV6 << 16, fa->regs + FA_L2_ETHERTYPE);
+ writel(IPPROTO_TCP | IPPROTO_UDP << 8 | FA_IPV4_CSUM, fa->regs + FA_IPV4);
+ /* Zero seed, CCITT CRC; learned hashes are used for table placement. */
+ writel(FA_NAPT_DEC_TTL, fa->regs + FA_NAPT);
+ writel(0xff, fa->regs + FA_RX_STATUS);
+ fa->netdev_nb.notifier_call = fa_netdev_event;
+ err = register_netdevice_notifier(&fa->netdev_nb);
+ if (err)
+ return ERR_PTR(err);
+ fa->netevent_nb.notifier_call = fa_netevent;
+ err = register_netevent_notifier(&fa->netevent_nb);
+ if (err)
+ goto unregister_netdev;
+ fa->switchdev_nb.notifier_call = fa_switchdev_atomic;
+ err = register_switchdev_notifier(&fa->switchdev_nb);
+ if (err)
+ goto unregister_netevent;
+ fa->switchdev_blocking_nb.notifier_call = fa_switchdev_blocking;
+ err = register_switchdev_blocking_notifier(&fa->switchdev_blocking_nb);
+ if (err)
+ goto unregister_switchdev;
+ fa->fib_nb.notifier_call = fa_fib_event;
+ err = register_fib_notifier(&init_net, &fa->fib_nb, fa_fib_reset, NULL);
+ if (err)
+ goto unregister_switchdev_blocking;
+ /* Finish initial route replay before allowing hardware admission. */
+ fa_invalidate(fa);
+ fa->debugfs = debugfs_create_dir(dev_name(parent), NULL);
+ if (IS_ENABLED(CONFIG_BCM_NS_FA_FAULT_INJECTION)) {
+ fault_create_debugfs_attr("fail_table", fa->debugfs, &fa->fail_table);
+ debugfs_create_bool("fail_table_write_only", 0600, fa->debugfs,
+ &fa->fail_table_write_only);
+ }
+ debugfs_create_file("fa", 0400, fa->debugfs, fa, &fa_debug_fops);
+ debugfs_create_file("fa_table", 0400, fa->debugfs, fa, &fa_table_fops);
+ fa->control.conduit = dev;
+ fa->control.priv = fa;
+ fa->control.receive = fa_control_receive;
+ fa->control.state_change = fa_control_state_change;
+ brcm_fa_control_register(&fa->control);
+ err = devm_add_action_or_reset(parent, fa_release, fa);
+ if (err)
+ return ERR_PTR(err);
+ writel(FA_CTL_NO_DA_CHECK | FA_CTL_CRC_OVERWRITE |
+ FIELD_PREP(FA_CTL_THRESHOLD, 0x140),
+ fa->regs + FA_CTL);
+ return fa;
+unregister_switchdev_blocking:
+ unregister_switchdev_blocking_notifier(&fa->switchdev_blocking_nb);
+unregister_switchdev:
+ unregister_switchdev_notifier(&fa->switchdev_nb);
+unregister_netevent:
+ fa_fib_reset(&fa->fib_nb);
+ WRITE_ONCE(fa->stopping, true);
+ unregister_netevent_notifier(&fa->netevent_nb);
+ cancel_work_sync(&fa->control_work);
+unregister_netdev:
+ unregister_netdevice_notifier(&fa->netdev_nb);
+ return ERR_PTR(err);
+}
+EXPORT_SYMBOL_GPL(bcm_ns_fa_init);
+
+MODULE_DESCRIPTION("Broadcom Northstar IPv4 TCP/UDP NAT flow accelerator");
+MODULE_LICENSE("GPL");
diff --git a/drivers/net/ethernet/broadcom/bcm-ns-fa.h b/drivers/net/ethernet/broadcom/bcm-ns-fa.h
new file mode 100644
index 0000000000000..825b019fb89fe
--- /dev/null
+++ b/drivers/net/ethernet/broadcom/bcm-ns-fa.h
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef _BCM_NS_FA_H
+#define _BCM_NS_FA_H
+
+#include <linux/netdevice.h>
+
+struct bcm_ns_fa;
+
+#if IS_REACHABLE(CONFIG_BCM_NS_FA)
+struct bcm_ns_fa *bcm_ns_fa_init(struct net_device *dev);
+void bcm_ns_fa_rx(struct bcm_ns_fa *fa, struct sk_buff *skb);
+int bcm_ns_fa_setup_tc(struct bcm_ns_fa *fa, enum tc_setup_type type, void *data);
+void bcm_ns_fa_flush(struct bcm_ns_fa *fa);
+void bcm_ns_fa_set_offload(struct bcm_ns_fa *fa, bool enable);
+#else
+static inline struct bcm_ns_fa *bcm_ns_fa_init(struct net_device *dev)
+{
+ return NULL;
+}
+
+static inline void bcm_ns_fa_rx(struct bcm_ns_fa *fa, struct sk_buff *skb) {}
+static inline void bcm_ns_fa_flush(struct bcm_ns_fa *fa) {}
+static inline void bcm_ns_fa_set_offload(struct bcm_ns_fa *fa, bool enable) {}
+static inline int bcm_ns_fa_setup_tc(struct bcm_ns_fa *fa,
+ enum tc_setup_type type, void *data)
+{
+ return -EOPNOTSUPP;
+}
+#endif
+#endif
diff --git a/drivers/net/ethernet/broadcom/bgmac-bcma.c b/drivers/net/ethernet/broadcom/bgmac-bcma.c
index 36f9bad28e6a9..87338e01a825b 100644
--- a/drivers/net/ethernet/broadcom/bgmac-bcma.c
+++ b/drivers/net/ethernet/broadcom/bgmac-bcma.c
@@ -11,6 +11,7 @@
#include <linux/bcma/bcma.h>
#include <linux/brcmphy.h>
#include <linux/etherdevice.h>
+#include <linux/of.h>
#include <linux/of_mdio.h>
#include <linux/of_net.h>
#include "bgmac.h"
@@ -130,6 +131,10 @@ static int bgmac_probe(struct bcma_device *core)
const u8 *mac;
int err;
+ if (of_property_present(core->dev.of_node, "brcm,flow-accelerator") &&
+ core->core_unit != 2)
+ return -EINVAL;
+
bgmac = bgmac_alloc(&core->dev);
if (!bgmac)
return -ENOMEM;
diff --git a/drivers/net/ethernet/broadcom/bgmac.c b/drivers/net/ethernet/broadcom/bgmac.c
index b6d1572417a44..a6d0ed24f7d34 100644
--- a/drivers/net/ethernet/broadcom/bgmac.c
+++ b/drivers/net/ethernet/broadcom/bgmac.c
@@ -495,6 +495,7 @@ static int bgmac_dma_rx_read(struct bgmac *bgmac, struct bgmac_dma_ring *ring,
skb_pull(skb, BGMAC_RX_FRAME_OFFSET +
BGMAC_RX_BUF_OFFSET);
+ bcm_ns_fa_rx(bgmac->fa, skb);
skb_checksum_none_assert(skb);
skb->protocol = eth_type_trans(skb, bgmac->net_dev);
bgmac->net_dev->stats.rx_bytes += len;
@@ -1284,7 +1285,25 @@ static int bgmac_change_mtu(struct net_device *net_dev, int mtu)
return 0;
}
+static int bgmac_set_features(struct net_device *dev, netdev_features_t features)
+{
+ struct bgmac *bgmac = netdev_priv(dev);
+
+ bcm_ns_fa_set_offload(bgmac->fa, features & NETIF_F_HW_TC);
+ return 0;
+}
+
+static int bgmac_setup_tc(struct net_device *dev, enum tc_setup_type type,
+ void *data)
+{
+ struct bgmac *bgmac = netdev_priv(dev);
+
+ return bcm_ns_fa_setup_tc(bgmac->fa, type, data);
+}
+
static const struct net_device_ops bgmac_netdev_ops = {
+ .ndo_set_features = bgmac_set_features,
+ .ndo_setup_tc = bgmac_setup_tc,
.ndo_open = bgmac_open,
.ndo_stop = bgmac_stop,
.ndo_start_xmit = bgmac_start_xmit,
@@ -1572,6 +1591,17 @@ int bgmac_enet_probe(struct bgmac *bgmac)
net_dev->hw_features = net_dev->features;
net_dev->vlan_features = net_dev->features;
+ bgmac->fa = bcm_ns_fa_init(net_dev);
+ if (IS_ERR(bgmac->fa)) {
+ err = PTR_ERR(bgmac->fa);
+ bgmac->fa = NULL;
+ goto err_phy_disconnect;
+ }
+ if (bgmac->fa) {
+ net_dev->features |= NETIF_F_HW_TC;
+ net_dev->hw_features |= NETIF_F_HW_TC;
+ }
+
/* Omit L2 header, VLAN tag and FCS from max MTU size */
net_dev->max_mtu = BGMAC_RX_MAX_FRAME_SIZE - ETH_HLEN - VLAN_HLEN -
ETH_FCS_LEN;
diff --git a/drivers/net/ethernet/broadcom/bgmac.h b/drivers/net/ethernet/broadcom/bgmac.h
index 6fee9a41839c0..788d5ba9c9b5b 100644
--- a/drivers/net/ethernet/broadcom/bgmac.h
+++ b/drivers/net/ethernet/broadcom/bgmac.h
@@ -5,6 +5,7 @@
#include <linux/netdevice.h>
#include "unimac.h"
+#include "bcm-ns-fa.h"
#define BGMAC_DEV_CTL 0x000
#define BGMAC_DC_TSM 0x00000002
@@ -437,6 +438,7 @@ struct bgmac_rx_header {
};
struct bgmac {
+ struct bcm_ns_fa *fa;
union {
struct {
void __iomem *base;
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH net-next v2 5/6] net: add KUnit coverage for FA and flowtable path validation
2026-12-01 18:22 [PATCH net-next v2 0/6] Add Northstar FA TCP/UDP NAT offload with a bridged LAN Florian Fainelli
` (3 preceding siblings ...)
2026-12-01 18:22 ` [PATCH net-next v2 4/6] net: bgmac: add Northstar IPv4 TCP/UDP NAT flowtable offload Florian Fainelli
@ 2026-12-01 18:22 ` Florian Fainelli
2026-12-01 18:22 ` [PATCH net-next v2 6/6] docs: net: document Northstar FA setup and NAT qualification Florian Fainelli
5 siblings, 0 replies; 7+ messages in thread
From: Florian Fainelli @ 2026-12-01 18:22 UTC (permalink / raw)
To: netdev
Cc: Florian Fainelli, andrew, coreteam, davem, edumazet, fw, horms,
jonas.gorski, kuba, linux-kernel, netfilter-devel, olteanv,
pabeni, pablo, phil, willemb
Exercise Northstar NAT fields, byte order, protocol-aware hash learning,
TCP control identity, lookup gating, MTU tracking and owner
invalidation. Check B53 UDF layout and every IPv4 packet length, failed-
guard readiness, and FA/ordinary switch tags.
Test cached-route owner validation with route-cookie expiry, changed or
invalid neighbours, absent route context and unsupported stacked paths.
Require explicit owner validation to detect an expired validation-only
dst even though the generic packet/GC helper ignores that reference.
Add seven scenarios using real route initialization and the private GC
step: ordinary direct software forwarding, validation-only references,
direct tunnels with and without hardware requested, neighbour routes
with and without hardware requested, and XFRM. Check legacy validation
policy, GC teardown and balanced dst references. The validation-only
scenario fails both packet and GC expectations before the policy fix.
The independent netfilter prerequisite already carries its lifetime
tests and standalone configuration. Extend that test configuration's
help for these path cases, embed private tests in their parent units,
and keep the combined FA configuration beside the backend.
Cover bridge path movement, missing FDB entries, owner-derived ingress
peers, software cache invalidation during lookup, GC and construction,
pending hardware admission, source-guard sharing and exhaustion, RCU-
only STP access, auxiliary forwarding marks and the ninth CFP UDF valid
bit.
Signed-off-by: Florian Fainelli <florian.fainelli@broadcom.com>
---
Changes in v2:
- Cover missing-neighbour and unsupported-family validation, exact TCP
control identity in both directions, and all-port bridge guards.
- Give each negative NAT-action case an independent valid starting rule,
refresh checksums when changing test protocols, and provide a working
dummy netdevice transmit callback.
- Skip direct-register cases with indirect MMIO accessors, such as UML.
Update the combined suite help for standalone lifetime tests.
- Add bridge movement and generation tests for construction, packet
lookup, GC and pending hardware admission; cover source-guard
sharing/exhaustion, RCU STP access, auxiliary forwarding marks and the
ninth CFP UDF validity bit. The combined QEMU suite has 37 cases.
MAINTAINERS | 3 +
drivers/net/dsa/b53/Kconfig | 11 +
drivers/net/dsa/b53/b53_common.c | 4 +
drivers/net/dsa/b53/b53_fa_test.c | 124 +++
drivers/net/ethernet/broadcom/.kunitconfig | 24 +
drivers/net/ethernet/broadcom/Kconfig | 13 +
.../net/ethernet/broadcom/bcm-ns-fa-test.c | 795 ++++++++++++++++++
drivers/net/ethernet/broadcom/bcm-ns-fa.c | 4 +
net/dsa/Kconfig | 11 +
net/dsa/tag_brcm.c | 4 +
net/dsa/tag_brcm_test.c | 200 +++++
net/netfilter/Kconfig | 15 +-
net/netfilter/nf_flow_table_core_test.c | 203 +++++
net/netfilter/nf_flow_table_offload.c | 4 +
net/netfilter/nf_flow_table_offload_test.c | 242 ++++++
15 files changed, 1650 insertions(+), 7 deletions(-)
create mode 100644 drivers/net/dsa/b53/b53_fa_test.c
create mode 100644 drivers/net/ethernet/broadcom/.kunitconfig
create mode 100644 drivers/net/ethernet/broadcom/bcm-ns-fa-test.c
create mode 100644 net/dsa/tag_brcm_test.c
create mode 100644 net/netfilter/nf_flow_table_offload_test.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 80969b91af402..c05ac2258889e 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5305,6 +5305,7 @@ F: drivers/net/dsa/bcm_sf2*
F: include/linux/dsa/brcm.h
F: include/linux/platform_data/b53.h
F: net/dsa/tag_brcm.c
+F: net/dsa/tag_brcm_test.c
BROADCOM BCM2711/BCM2835 ARM ARCHITECTURE
M: Florian Fainelli <florian.fainelli@broadcom.com>
@@ -5671,6 +5672,8 @@ S: Maintained
F: Documentation/devicetree/bindings/net/brcm,amac.yaml
F: Documentation/devicetree/bindings/net/brcm,bcm4708-fa.yaml
F: Documentation/devicetree/bindings/net/brcm,bcm5301x-gmac.yaml
+F: drivers/net/dsa/b53/b53_fa_test.c
+F: drivers/net/ethernet/broadcom/.kunitconfig
F: drivers/net/ethernet/broadcom/bcm-ns-fa*
F: drivers/net/ethernet/broadcom/bgmac*
F: drivers/net/ethernet/broadcom/unimac.h
diff --git a/drivers/net/dsa/b53/Kconfig b/drivers/net/dsa/b53/Kconfig
index 8e2a8c20bc4ac..5a03bacef2e55 100644
--- a/drivers/net/dsa/b53/Kconfig
+++ b/drivers/net/dsa/b53/Kconfig
@@ -13,6 +13,17 @@ menuconfig B53
BCM5325E, BCM5365, BCM539x, BCM53115 and BCM53125 as well as BCM63XX
integrated switches.
+config B53_FA_KUNIT_TEST
+ bool "KUnit tests for B53 FA exception guards" if !KUNIT_ALL_TESTS
+ depends on KUNIT=y && B53
+ default KUNIT_ALL_TESTS
+ help
+ Tests are compiled into the parent driver and require built-in KUnit.
+ Verify the original FA classifier field layout and complete packet
+ length range used to divert unsafe packets around the accelerator.
+ Check cross-word destination MAC encoding and every 16-bit IPv4
+ packet length against the standard MTU without accessing hardware.
+
config B53_SPI_DRIVER
tristate "B53 SPI connected switch driver"
depends on B53 && SPI
diff --git a/drivers/net/dsa/b53/b53_common.c b/drivers/net/dsa/b53/b53_common.c
index 234c6a2905e26..1a6fcb9b9a710 100644
--- a/drivers/net/dsa/b53/b53_common.c
+++ b/drivers/net/dsa/b53/b53_common.c
@@ -3893,6 +3893,10 @@ int b53_switch_register(struct b53_device *dev)
}
EXPORT_SYMBOL(b53_switch_register);
+#if IS_ENABLED(CONFIG_B53_FA_KUNIT_TEST)
+#include "b53_fa_test.c"
+#endif
+
MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
MODULE_DESCRIPTION("B53 switch library");
MODULE_LICENSE("Dual BSD/GPL");
diff --git a/drivers/net/dsa/b53/b53_fa_test.c b/drivers/net/dsa/b53/b53_fa_test.c
new file mode 100644
index 0000000000000..9e0e7936da925
--- /dev/null
+++ b/drivers/net/dsa/b53/b53_fa_test.c
@@ -0,0 +1,124 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <kunit/test.h>
+
+static int b53_fa_test_write16(struct b53_device *dev, u8 page, u8 reg, u16 value)
+{
+ u16 *control = dev->priv;
+
+ if (page != B53_CFP_PAGE || reg != B53_CFP_CTRL)
+ return -EINVAL;
+ *control = value;
+ return 0;
+}
+
+static void b53_fa_test_port_guards(struct kunit *test)
+{
+ static const struct b53_io_ops ops = { .write16 = b53_fa_test_write16 };
+ struct b53_device *dev = kunit_kzalloc(test, sizeof(*dev), GFP_KERNEL);
+ struct dsa_switch_tree tree = {};
+ struct dsa_switch ds = { .dst = &tree };
+ struct dsa_port control = {
+ .ds = &ds, .index = 5, .type = DSA_PORT_TYPE_CPU,
+ .conduit_admin_up = true, .conduit_oper_up = true,
+ };
+ u16 map = 0;
+
+ KUNIT_ASSERT_NOT_NULL(test, dev);
+ INIT_LIST_HEAD(&tree.ports);
+ list_add(&control.list, &tree.ports);
+ dev->ds = &ds;
+ dev->ops = &ops;
+ dev->priv = ↦
+ dev->fa_cfp_enabled = true;
+ dev->fa_cfp_ports = BIT(0) | BIT(1);
+ dev->fa_user_ports = BIT(0) | BIT(1);
+ mutex_init(&dev->reg_mutex);
+
+ /* Bridge changes must not resurrect incomplete or failed guards. */
+ KUNIT_ASSERT_EQ(test, b53_fa_update(dev, true), 0);
+ KUNIT_EXPECT_EQ(test, map, (u16)0);
+ dev->fa_guards_ready = true;
+ KUNIT_ASSERT_EQ(test, b53_fa_update(dev, true), 0);
+ KUNIT_EXPECT_EQ(test, map, (u16)(BIT(0) | BIT(1)));
+ /* Incomplete router-domain guards keep every port disabled. */
+ dev->fa_cfp_ports &= ~BIT(1);
+ KUNIT_ASSERT_EQ(test, b53_fa_update(dev, true), 0);
+ KUNIT_EXPECT_EQ(test, map, (u16)0);
+ dev->fa_cfp_ports |= BIT(1);
+ KUNIT_ASSERT_EQ(test, b53_fa_update(dev, true), 0);
+ KUNIT_EXPECT_EQ(test, map, (u16)(BIT(0) | BIT(1)));
+ dev->fa_guards_failed = true;
+ KUNIT_ASSERT_EQ(test, b53_fa_update(dev, true), 0);
+ KUNIT_EXPECT_EQ(test, map, (u16)0);
+ KUNIT_ASSERT_EQ(test, b53_fa_update(dev, true), 0);
+ KUNIT_EXPECT_EQ(test, map, (u16)0);
+ KUNIT_EXPECT_EQ(test, dev->fa_cfp_ports, (u16)(BIT(0) | BIT(1)));
+ dev->fa_guards_failed = false;
+ control.conduit_oper_up = false;
+ KUNIT_ASSERT_EQ(test, b53_fa_update(dev, false), 0);
+ KUNIT_EXPECT_EQ(test, map, (u16)0);
+}
+
+static void b53_fa_test_length_range(struct kunit *test)
+{
+ unsigned int length, i;
+
+ for (length = 0; length <= U16_MAX; length++) {
+ bool trap = false;
+
+ for (i = 0; i < ARRAY_SIZE(b53_fa_lengths); i++)
+ if ((length & b53_fa_lengths[i][1]) == b53_fa_lengths[i][0])
+ trap = true;
+ KUNIT_ASSERT_EQ_MSG(test, trap, length > ETH_DATA_LEN, "length %u", length);
+ }
+}
+
+static void b53_fa_test_udf_layout(struct kunit *test)
+{
+ u32 data[8] = {}, mask[8] = {};
+
+ b53_fa_udf(data, mask, 2, 1501, 0xffff);
+ KUNIT_EXPECT_EQ(test, data[1], 0x0005dd00U);
+ KUNIT_EXPECT_EQ(test, mask[1], 0x00ffff00U);
+ KUNIT_EXPECT_EQ(test, mask[5], (u32)BIT(26));
+ memset(data, 0, sizeof(data));
+ memset(mask, 0, sizeof(mask));
+ b53_fa_udf(data, mask, 3, 0x0200, 0xffff);
+ b53_fa_udf(data, mask, 4, 0x0000, 0xffff);
+ b53_fa_udf(data, mask, 5, 0x0101, 0xffff);
+ KUNIT_EXPECT_EQ(test, data[1], 0U);
+ KUNIT_EXPECT_EQ(test, data[2], 0x01000002U);
+ KUNIT_EXPECT_EQ(test, data[3], 1U);
+ KUNIT_EXPECT_EQ(test, mask[1], 0xff000000U);
+ KUNIT_EXPECT_EQ(test, mask[2], 0xffffffffU);
+ KUNIT_EXPECT_EQ(test, mask[3], 0x000000ffU);
+ KUNIT_EXPECT_EQ(test, mask[5], (u32)(BIT(27) | BIT(28) | BIT(29)));
+}
+
+static void b53_fa_test_source_udf(struct kunit *test)
+{
+ u32 data[8] = {}, mask[8] = {};
+
+ /* The ninth UDF validity bit crosses from DATA5 into DATA6. */
+ b53_fa_udf(data, mask, 8, 0xabcd, 0xffff);
+ KUNIT_EXPECT_EQ(test, data[4], 0x00abcd00U);
+ KUNIT_EXPECT_EQ(test, mask[4], 0x00ffff00U);
+ KUNIT_EXPECT_EQ(test, data[5], 0U);
+ KUNIT_EXPECT_EQ(test, data[6], 1U);
+ KUNIT_EXPECT_EQ(test, mask[6], 1U);
+}
+
+static struct kunit_case b53_fa_cases[] = {
+ KUNIT_CASE(b53_fa_test_port_guards),
+ KUNIT_CASE(b53_fa_test_length_range),
+ KUNIT_CASE(b53_fa_test_udf_layout),
+ KUNIT_CASE(b53_fa_test_source_udf),
+ {}
+};
+
+static struct kunit_suite b53_fa_suite = {
+ .name = "b53-fa-guards",
+ .test_cases = b53_fa_cases,
+};
+
+kunit_test_suite(b53_fa_suite);
diff --git a/drivers/net/ethernet/broadcom/.kunitconfig b/drivers/net/ethernet/broadcom/.kunitconfig
new file mode 100644
index 0000000000000..6ef784dfce24b
--- /dev/null
+++ b/drivers/net/ethernet/broadcom/.kunitconfig
@@ -0,0 +1,24 @@
+CONFIG_KUNIT=y
+CONFIG_DEBUG_KERNEL=y
+CONFIG_NET_DEV_REFCNT_TRACKER=y
+CONFIG_COMPILE_TEST=y
+CONFIG_NET=y
+CONFIG_INET=y
+CONFIG_NETDEVICES=y
+CONFIG_NET_VENDOR_BROADCOM=y
+CONFIG_NETFILTER=y
+CONFIG_NF_CONNTRACK=y
+CONFIG_NF_TABLES=y
+CONFIG_NF_FLOW_TABLE=y
+CONFIG_NF_FLOW_TABLE_KUNIT_TEST=y
+CONFIG_NET_DSA=y
+CONFIG_OF=y
+CONFIG_BCM_NS_FA=y
+CONFIG_BCM_NS_FA_KUNIT_TEST=y
+CONFIG_NET_DSA_TAG_BRCM_FA=y
+CONFIG_NET_DSA_TAG_BRCM_FA_KUNIT_TEST=y
+CONFIG_B53=y
+CONFIG_B53_FA_KUNIT_TEST=y
+CONFIG_VLAN_8021Q=y
+CONFIG_BRIDGE=y
+CONFIG_BRIDGE_VLAN_FILTERING=y
diff --git a/drivers/net/ethernet/broadcom/Kconfig b/drivers/net/ethernet/broadcom/Kconfig
index bb86e73757ee1..6f132061253fb 100644
--- a/drivers/net/ethernet/broadcom/Kconfig
+++ b/drivers/net/ethernet/broadcom/Kconfig
@@ -192,6 +192,19 @@ config BCM_NS_FA_FAULT_INJECTION
reprobe and leaves forwarding to software. Intended for driver testing.
Injection is disabled by default and can be armed through debugfs.
+config BCM_NS_FA_KUNIT_TEST
+ bool "KUnit tests for the Northstar flow accelerator" if !KUNIT_ALL_TESTS
+ depends on KUNIT=y && BCM_NS_FA
+ default KUNIT_ALL_TESTS
+ help
+ Test NAT action validation, table field encoding, and RX hash learning.
+ Exercise upper-device inspection under RCU with real netdevices.
+ Tests are compiled into the parent driver and require built-in KUnit.
+ These tests run without an accelerator and do not simulate table I/O.
+ The few cases which access registers use plain memory instead, and
+ are skipped where MMIO is emulated, such as on UML.
+ Use the hardware validation plan to check actual forwarding.
+
config BGMAC_BCMA
tristate "Broadcom iProc GBit BCMA support"
depends on BCM_NS_FA || !BCM_NS_FA
diff --git a/drivers/net/ethernet/broadcom/bcm-ns-fa-test.c b/drivers/net/ethernet/broadcom/bcm-ns-fa-test.c
new file mode 100644
index 0000000000000..8a3858699dcd6
--- /dev/null
+++ b/drivers/net/ethernet/broadcom/bcm-ns-fa-test.c
@@ -0,0 +1,795 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Included in bcm-ns-fa.c to exercise private table formats and validation. */
+#include <kunit/test.h>
+#include <net/lwtunnel.h>
+#include <net/nexthop.h>
+#include <linux/rtnetlink.h>
+#include <linux/panic.h>
+
+static netdev_tx_t fa_test_xmit(struct sk_buff *skb, struct net_device *dev)
+{
+ dev_kfree_skb_any(skb);
+ return NETDEV_TX_OK;
+}
+
+static const struct net_device_ops fa_test_netdev_ops = {
+ .ndo_start_xmit = fa_test_xmit,
+};
+
+/* Cases which read and write registers cannot run where MMIO is emulated
+ * with indirect accessors, as on UML: they reject plain memory addresses.
+ */
+#define FA_TEST_REQUIRE_DIRECT_MMIO(test) \
+ do { \
+ if (IS_ENABLED(CONFIG_INDIRECT_IOMEM)) \
+ kunit_skip(test, "needs direct MMIO accesses"); \
+ } while (0)
+
+static void fa_test_free_netdev(void *data)
+{
+ struct net_device *dev = data;
+
+ if (dev->reg_state == NETREG_REGISTERED)
+ unregister_netdev(dev);
+ free_netdev(dev);
+}
+
+static void fa_test_upper_device_rcu(struct kunit *test)
+{
+ struct net_device *lower, *upper;
+ bool has_upper;
+ int err;
+
+ lower = alloc_etherdev(0);
+ KUNIT_ASSERT_NOT_NULL(test, lower);
+ err = kunit_add_action_or_reset(test, fa_test_free_netdev, lower);
+ KUNIT_ASSERT_EQ(test, err, 0);
+ upper = alloc_etherdev(0);
+ KUNIT_ASSERT_NOT_NULL(test, upper);
+ err = kunit_add_action_or_reset(test, fa_test_free_netdev, upper);
+ KUNIT_ASSERT_EQ(test, err, 0);
+ lower->netdev_ops = &fa_test_netdev_ops;
+ upper->netdev_ops = &fa_test_netdev_ops;
+ eth_hw_addr_random(lower);
+ eth_hw_addr_random(upper);
+ KUNIT_ASSERT_EQ(test, register_netdev(lower), 0);
+ KUNIT_ASSERT_EQ(test, register_netdev(upper), 0);
+
+ /* Rule workers inspect topology under RCU without taking RTNL. */
+ rcu_read_lock();
+ has_upper = fa_has_upper_dev_rcu(lower);
+ rcu_read_unlock();
+ KUNIT_EXPECT_FALSE(test, has_upper);
+
+ rtnl_lock();
+ err = netdev_upper_dev_link(lower, upper, NULL);
+ rtnl_unlock();
+ KUNIT_ASSERT_EQ(test, err, 0);
+ rcu_read_lock();
+ has_upper = fa_has_upper_dev_rcu(lower);
+ rcu_read_unlock();
+ KUNIT_EXPECT_TRUE(test, has_upper);
+
+ rtnl_lock();
+ netdev_upper_dev_unlink(lower, upper);
+ rtnl_unlock();
+ rcu_read_lock();
+ has_upper = fa_has_upper_dev_rcu(lower);
+ rcu_read_unlock();
+ KUNIT_EXPECT_FALSE(test, has_upper);
+}
+
+static void fa_test_bridge_state_rcu(struct kunit *test)
+{
+ struct net_device *dev;
+ int tainted;
+ u8 state;
+
+ dev = alloc_etherdev(0);
+ KUNIT_ASSERT_NOT_NULL(test, dev);
+ tainted = test_taint(TAINT_WARN);
+ /* FA rule workers hold RCU, not RTNL. A device outside a bridge must
+ * be rejected without requiring an unrelated global configuration lock.
+ */
+ rcu_read_lock();
+ state = br_port_get_stp_state(dev);
+ rcu_read_unlock();
+ KUNIT_EXPECT_EQ(test, state, (u8)BR_STATE_DISABLED);
+ KUNIT_EXPECT_EQ(test, test_taint(TAINT_WARN), tainted);
+ free_netdev(dev);
+}
+
+static u64 fa_test_bits(const u32 *data, unsigned int low, unsigned int width)
+{
+ u64 val = 0;
+ unsigned int i;
+
+ for (i = 0; i < width; i++)
+ if (data[(low + i) / 32] & BIT((low + i) % 32))
+ val |= BIT_ULL(i);
+ return val;
+}
+
+static void fa_test_encode(struct kunit *test)
+{
+ struct fa_rule rule = {
+ .tuple = { cpu_to_be32(0xc0000202), cpu_to_be32(0xc6336402),
+ cpu_to_be16(0x1234), cpu_to_be16(0xabcd) },
+ .remap_ip = 0xcb007109,
+ .remap_port = 0x5678,
+ .action = FA_ACT_REMAP_IP | FA_ACT_REMAP_PORT,
+ .direction = FA_DIR_DNAT,
+ };
+ u32 data[8] = {};
+
+ fa_encode(&rule, 3, 127, 4, data);
+ /* Compare against the bit ranges in the hardware table definition,
+ * including fields that straddle words and host/network byte order.
+ */
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 0, 35), 0ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 35, 2), 3ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 37, 1), 0ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 38, 2), 3ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 40, 7), 127ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 47, 32), 0xcb007109ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 79, 16), 0x5678ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 95, 16), 0xabcdULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 111, 16), 0x1234ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 127, 1), 0ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 128, 32), 0xc6336402ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 160, 32), 0xc0000202ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 192, 1), 1ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 193, 10), 16ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 209, 3), 1ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 212, 1), 1ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 255, 1), 1ULL);
+}
+
+static void fa_test_mac_encode(struct kunit *test)
+{
+ const u8 addr[ETH_ALEN] = { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54 };
+ u32 data[8] = {};
+
+ fa_mac_encode(addr, true, false, data);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 0, 1), 1ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 1, 48), 0xfedcba987654ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 49, 15), 0ULL);
+ memset(data, 0, sizeof(data));
+ fa_mac_encode(addr, false, false, data);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 0, 1), 0ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 1, 48), 0xfedcba987654ULL);
+ memset(data, 0, sizeof(data));
+ fa_mac_encode(addr, false, true, data);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 0, 1), 0ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 1, 2), 2ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 3, 16), 0ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 19, 48), 0xfedcba987654ULL);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 67, 29), 0ULL);
+ /* Hit-count bits must exclude the adjacent timestamp and action bits. */
+ data[0] = U32_MAX;
+ data[1] = U32_MAX;
+ KUNIT_EXPECT_EQ(test, fa_hits(data), U32_MAX);
+ data[0] = 7;
+ data[1] = ~7U;
+ KUNIT_EXPECT_EQ(test, fa_hits(data), 0U);
+}
+
+static void fa_test_nat_actions(struct kunit *test)
+{
+ struct fa_rule rule = { .direction = FA_DIR_UNSET, .l4proto = IPPROTO_UDP };
+ struct flow_action_entry act = {
+ .mangle = {
+ .htype = FLOW_ACT_MANGLE_HDR_TYPE_IP4,
+ .offset = offsetof(struct iphdr, saddr),
+ .val = (__force u32)cpu_to_be32(0xc0000201),
+ },
+ };
+
+ KUNIT_ASSERT_EQ(test, fa_mangle(&rule, &act), 0);
+ KUNIT_EXPECT_EQ(test, rule.remap_ip, 0xc0000201U);
+ KUNIT_EXPECT_EQ(test, rule.direction, 0);
+ act.mangle.htype = FLOW_ACT_MANGLE_HDR_TYPE_UDP;
+ act.mangle.offset = 0;
+ act.mangle.mask = (__force u32)cpu_to_be32(0x0000ffff);
+ act.mangle.val = (__force u32)cpu_to_be32(0xabcd0000);
+ KUNIT_ASSERT_EQ(test, fa_mangle(&rule, &act), 0);
+ KUNIT_EXPECT_EQ(test, rule.remap_port, (u16)0xabcd);
+ KUNIT_EXPECT_EQ(test, rule.action, (u8)3);
+ /* Every negative case starts from its own rule, so that only the check
+ * under test can reject it.
+ */
+ rule = (struct fa_rule){ .direction = FA_DIR_UNSET, .l4proto = IPPROTO_UDP };
+ act.mangle.htype = FLOW_ACT_MANGLE_HDR_TYPE_IP4;
+ act.mangle.offset = offsetof(struct iphdr, saddr);
+ act.mangle.mask = 0;
+ KUNIT_ASSERT_EQ(test, fa_mangle(&rule, &act), 0);
+ /* Opposite-side rewrites cannot share a hardware entry. */
+ act.mangle.htype = FLOW_ACT_MANGLE_HDR_TYPE_UDP;
+ act.mangle.offset = 0;
+ act.mangle.mask = (__force u32)cpu_to_be32(0xffff0000);
+ KUNIT_EXPECT_EQ(test, fa_mangle(&rule, &act), -EOPNOTSUPP);
+ /* A second rewrite of the same field is rejected. */
+ rule = (struct fa_rule){ .direction = FA_DIR_UNSET, .l4proto = IPPROTO_UDP };
+ act.mangle.mask = (__force u32)cpu_to_be32(0x0000ffff);
+ KUNIT_ASSERT_EQ(test, fa_mangle(&rule, &act), 0);
+ KUNIT_EXPECT_EQ(test, fa_mangle(&rule, &act), -EOPNOTSUPP);
+ /* Partial IPv4 masks and arbitrary offsets must not be accepted. */
+ rule = (struct fa_rule){ .direction = FA_DIR_UNSET, .l4proto = IPPROTO_UDP };
+ act.mangle.htype = FLOW_ACT_MANGLE_HDR_TYPE_IP4;
+ act.mangle.offset = offsetof(struct iphdr, daddr);
+ act.mangle.mask = 1;
+ KUNIT_EXPECT_EQ(test, fa_mangle(&rule, &act), -EOPNOTSUPP);
+ rule = (struct fa_rule){ .direction = FA_DIR_UNSET, .l4proto = IPPROTO_UDP };
+ act.mangle.mask = 0;
+ act.mangle.offset = offsetof(struct iphdr, ttl);
+ KUNIT_EXPECT_EQ(test, fa_mangle(&rule, &act), -EOPNOTSUPP);
+}
+
+static void fa_test_ethernet_actions(struct kunit *test)
+{
+ const u8 expected[12] = { 0x02, 1, 2, 3, 4, 5, 0x02, 6, 7, 8, 9, 10 };
+ struct fa_rule rule = {};
+ struct flow_action_entry act = {
+ .mangle.htype = FLOW_ACT_MANGLE_HDR_TYPE_ETH,
+ };
+ int i;
+
+ for (i = 0; i < sizeof(expected); i += 4) {
+ act.mangle.offset = i;
+ memcpy(&act.mangle.val, expected + i, 4);
+ KUNIT_ASSERT_EQ(test, fa_mangle(&rule, &act), 0);
+ }
+ KUNIT_EXPECT_MEMEQ(test, rule.eth, expected, sizeof(expected));
+ KUNIT_EXPECT_PTR_EQ(test, memchr_inv(rule.eth_mask, 0xff, 12), NULL);
+ act.mangle.offset = 9;
+ KUNIT_EXPECT_EQ(test, fa_mangle(&rule, &act), -EOPNOTSUPP);
+}
+
+static void fa_test_rx_learning(struct kunit *test)
+{
+ struct bcm_ns_fa *fa = kunit_kzalloc(test, sizeof(*fa), GFP_KERNEL);
+ struct sk_buff *skb;
+ struct ethhdr *eth;
+ struct iphdr *ip;
+ struct udphdr *udp;
+ u8 *tag;
+
+ KUNIT_ASSERT_NOT_NULL(test, fa);
+ fa->learned = kunit_kcalloc(test, FA_SLOTS, sizeof(*fa->learned), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, fa->learned);
+ spin_lock_init(&fa->learn_lock);
+ INIT_LIST_HEAD(&fa->entries);
+ skb = alloc_skb(64, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ tag = skb_put_zero(skb, 4 + ETH_HLEN + sizeof(*ip) + sizeof(*udp));
+ tag[0] = FA_MISS_OPCODE << 5;
+ tag[1] = 0xfe;
+ tag[3] = 3;
+ eth = (void *)(tag + 4);
+ eth->h_proto = htons(ETH_P_IP);
+ eth->h_dest[0] = 2;
+ ip = (void *)(eth + 1);
+ ip->version = 4;
+ ip->ihl = 5;
+ ip->protocol = IPPROTO_UDP;
+ ip->tot_len = htons(sizeof(*ip) + sizeof(*udp));
+ ip->saddr = htonl(0xc0000201);
+ ip->daddr = htonl(0xc6336402);
+ udp = (void *)(ip + 1);
+ udp->source = htons(1234);
+ udp->dest = htons(5678);
+ ip->check = ip_fast_csum(ip, ip->ihl);
+ bcm_ns_fa_rx(fa, skb);
+ KUNIT_EXPECT_EQ(test, fa->learn_next, 1U);
+ KUNIT_EXPECT_TRUE(test, fa->learned[0].valid);
+ KUNIT_EXPECT_EQ(test, fa->learned[0].hash, (u8)0xfe);
+ KUNIT_EXPECT_EQ(test, fa->learned[0].port, (u8)3);
+ KUNIT_EXPECT_EQ(test, fa->learned[0].tuple.src, htonl(0xc0000201));
+ KUNIT_EXPECT_EQ(test, fa->learned[0].tuple.dport, htons(5678));
+ /* Preserve metadata for the brcm-fa DSA tagger. */
+ KUNIT_EXPECT_EQ(test, tag[0], (u8)(FA_MISS_OPCODE << 5));
+ ip->frag_off = htons(IP_MF);
+ ip->check = 0;
+ ip->check = ip_fast_csum(ip, ip->ihl);
+ bcm_ns_fa_rx(fa, skb);
+ KUNIT_EXPECT_EQ(test, fa->learn_next, 1U);
+ ip->frag_off = 0;
+ ip->check = 0;
+ ip->check = ip_fast_csum(ip, ip->ihl);
+ tag[2] = 1;
+ bcm_ns_fa_rx(fa, skb);
+ KUNIT_EXPECT_EQ(test, fa->learn_next, 1U);
+ tag[2] = 0;
+ ip->protocol = IPPROTO_TCP;
+ ip->check = 0;
+ ip->check = ip_fast_csum(ip, ip->ihl);
+ bcm_ns_fa_rx(fa, skb);
+ KUNIT_EXPECT_EQ(test, fa->learn_next, 1U);
+ ip->protocol = IPPROTO_UDP;
+ ip->check = 0;
+ ip->check = ip_fast_csum(ip, ip->ihl);
+ eth->h_proto = htons(ETH_P_8021Q);
+ bcm_ns_fa_rx(fa, skb);
+ KUNIT_EXPECT_EQ(test, fa->learn_next, 1U);
+ skb_trim(skb, 3);
+ bcm_ns_fa_rx(fa, skb);
+ KUNIT_EXPECT_EQ(test, fa->learn_next, 1U);
+ kfree_skb(skb);
+}
+
+static void fa_test_tcp_encode_and_mangle(struct kunit *test)
+{
+ struct fa_rule rule = { .l4proto = IPPROTO_TCP, .direction = FA_DIR_UNSET };
+ struct flow_action_entry act = {
+ .mangle = {
+ .htype = FLOW_ACT_MANGLE_HDR_TYPE_TCP,
+ .mask = (__force u32)cpu_to_be32(0xffff0000),
+ .val = (__force u32)cpu_to_be32(0x00001234),
+ },
+ };
+ u32 data[8] = {};
+
+ KUNIT_ASSERT_EQ(test, fa_mangle(&rule, &act), 0);
+ KUNIT_EXPECT_EQ(test, rule.remap_port, (u16)0x1234);
+ KUNIT_EXPECT_EQ(test, rule.direction, 1);
+ act.mangle.htype = FLOW_ACT_MANGLE_HDR_TYPE_UDP;
+ KUNIT_EXPECT_EQ(test, fa_mangle(&rule, &act), -EOPNOTSUPP);
+ fa_encode(&rule, 1, 2, 3, data);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 127, 1), 1ULL);
+ rule.l4proto = IPPROTO_UDP;
+ fa_encode(&rule, 1, 2, 3, data);
+ KUNIT_EXPECT_EQ(test, fa_test_bits(data, 127, 1), 0ULL);
+}
+
+static void fa_test_tcp_identity(struct kunit *test)
+{
+ struct fa_rule rule = {
+ .tuple = { cpu_to_be32(0xc0000202), cpu_to_be32(0xc6336402),
+ cpu_to_be16(40000), cpu_to_be16(5001) },
+ .l4proto = IPPROTO_TCP,
+ .remap_ip = 0xcb007101,
+ .remap_port = 45000,
+ .action = FA_ACT_REMAP_IP | FA_ACT_REMAP_PORT,
+ };
+ struct fa_flow entry = { .l4proto = IPPROTO_TCP, .in_port = 3 };
+ struct fa_learned learn = { .valid = true, .l4proto = IPPROTO_UDP };
+ struct fa_tuple reply, control;
+
+ fa_reply_tuple(&rule, &reply);
+ KUNIT_EXPECT_EQ(test, reply.src, rule.tuple.dst);
+ KUNIT_EXPECT_EQ(test, reply.dst, htonl(rule.remap_ip));
+ KUNIT_EXPECT_EQ(test, reply.sport, rule.tuple.dport);
+ KUNIT_EXPECT_EQ(test, reply.dport, htons(rule.remap_port));
+ rule.direction = FA_DIR_DNAT;
+ fa_reply_tuple(&rule, &reply);
+ KUNIT_EXPECT_EQ(test, reply.src, htonl(rule.remap_ip));
+ KUNIT_EXPECT_EQ(test, reply.dst, rule.tuple.src);
+ KUNIT_EXPECT_EQ(test, reply.sport, htons(rule.remap_port));
+ KUNIT_EXPECT_EQ(test, reply.dport, rule.tuple.sport);
+
+ entry.tuple = rule.tuple;
+ entry.reply = reply;
+ entry.out_port = 4;
+ control = rule.tuple;
+ KUNIT_EXPECT_TRUE(test, fa_control_match(&entry, &control, 3));
+ KUNIT_EXPECT_FALSE(test, fa_control_match(&entry, &control, 2));
+ control.sport++;
+ KUNIT_EXPECT_FALSE(test, fa_control_match(&entry, &control, 3));
+ /* The reply direction retires the entry too, on its own ingress port. */
+ control = reply;
+ KUNIT_EXPECT_TRUE(test, fa_control_match(&entry, &control, 4));
+ KUNIT_EXPECT_FALSE(test, fa_control_match(&entry, &control, 3));
+ entry.l4proto = IPPROTO_UDP;
+ KUNIT_EXPECT_FALSE(test, fa_control_match(&entry, &control, 4));
+
+ /* Equal TCP/UDP four-tuples must never share a learned hash. A TCP
+ * control packet also invalidates observations from the old lifetime.
+ */
+ learn.tuple = rule.tuple;
+ KUNIT_EXPECT_FALSE(test, fa_learn_matches(&learn, &rule, 0));
+ learn.l4proto = IPPROTO_TCP;
+ KUNIT_EXPECT_TRUE(test, fa_learn_matches(&learn, &rule, 0));
+ KUNIT_EXPECT_FALSE(test, fa_learn_matches(&learn, &rule, 1));
+ rule.l4proto = IPPROTO_UDP;
+ learn.l4proto = IPPROTO_UDP;
+ /* UDP uses the route/neighbour epoch, independent of TCP controls. */
+ KUNIT_EXPECT_TRUE(test, fa_learn_matches(&learn, &rule, 0));
+ KUNIT_EXPECT_FALSE(test, fa_learn_matches(&learn, &rule, 1));
+}
+
+static void fa_test_tcp_rx_control(struct kunit *test)
+{
+ struct bcm_ns_fa *fa = kunit_kzalloc(test, sizeof(*fa), GFP_KERNEL);
+ struct sk_buff *skb;
+ struct ethhdr *eth;
+ struct iphdr *ip;
+ struct tcphdr *tcp;
+ u8 *tag;
+
+ KUNIT_ASSERT_NOT_NULL(test, fa);
+ fa->learned = kunit_kcalloc(test, FA_SLOTS, sizeof(*fa->learned), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, fa->learned);
+ INIT_LIST_HEAD(&fa->entries);
+ spin_lock_init(&fa->learn_lock);
+ skb = alloc_skb(128, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ tag = skb_put_zero(skb, FA_TAG_LEN + ETH_HLEN + sizeof(*ip) + sizeof(*tcp));
+ tag[0] = FA_MISS_OPCODE << 5;
+ tag[1] = 0xab;
+ tag[3] = 2;
+ eth = (void *)(tag + FA_TAG_LEN);
+ eth->h_proto = htons(ETH_P_IP);
+ ip = (void *)(eth + 1);
+ ip->version = 4;
+ ip->ihl = 5;
+ ip->protocol = IPPROTO_TCP;
+ ip->tot_len = htons(sizeof(*ip) + sizeof(*tcp));
+ ip->saddr = htonl(0xc0000202);
+ ip->daddr = htonl(0xc6336402);
+ ip->check = ip_fast_csum(ip, ip->ihl);
+ tcp = (void *)(ip + 1);
+ tcp->doff = 5;
+ tcp->source = htons(40000);
+ tcp->dest = htons(5001);
+ tcp->syn = 1;
+ bcm_ns_fa_rx(fa, skb);
+ KUNIT_EXPECT_EQ(test, fa->learn_next, 0U);
+ KUNIT_EXPECT_EQ(test, fa->control_generation, 1UL);
+ tcp->syn = 0;
+ tcp->ack = 1;
+ bcm_ns_fa_rx(fa, skb);
+ KUNIT_EXPECT_EQ(test, fa->learn_next, 1U);
+ KUNIT_EXPECT_EQ(test, fa->learned[0].l4proto, (u8)IPPROTO_TCP);
+ KUNIT_EXPECT_EQ(test, fa->learned[0].hash, (u8)0xab);
+ KUNIT_EXPECT_EQ(test, fa->learned[0].generation, 1UL);
+ /* Opcode-zero switch traps contain no hash, but still retire TCP. */
+ tag[0] = 0;
+ tag[1] = 1;
+ tag[2] = BIT(3);
+ tcp->fin = 1;
+ bcm_ns_fa_rx(fa, skb);
+ KUNIT_EXPECT_EQ(test, fa->learn_next, 1U);
+ KUNIT_EXPECT_EQ(test, fa->control_generation, 2UL);
+ tcp->fin = 0;
+ tcp->rst = 1;
+ bcm_ns_fa_rx(fa, skb);
+ KUNIT_EXPECT_EQ(test, fa->control_generation, 3UL);
+ tcp->doff = 4;
+ bcm_ns_fa_rx(fa, skb);
+ KUNIT_EXPECT_EQ(test, fa->control_generation, 3UL);
+ kfree_skb(skb);
+}
+
+static void fa_test_tcp_control_readiness(struct kunit *test)
+{
+ struct bcm_ns_fa *fa = kunit_kzalloc(test, sizeof(*fa), GFP_KERNEL);
+
+ FA_TEST_REQUIRE_DIRECT_MMIO(test);
+ KUNIT_ASSERT_NOT_NULL(test, fa);
+ fa->regs = (void __iomem *)kunit_kzalloc(test, 0xc0, GFP_KERNEL);
+ fa->learned = kunit_kcalloc(test, FA_SLOTS, sizeof(*fa->learned), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, fa->regs);
+ KUNIT_ASSERT_NOT_NULL(test, fa->learned);
+ mutex_init(&fa->lock);
+ spin_lock_init(&fa->learn_lock);
+ fa_control_state_change(fa, true);
+ KUNIT_EXPECT_TRUE(test, fa->tcp_ready);
+ fa_control_state_change(fa, false);
+ KUNIT_EXPECT_FALSE(test, fa->tcp_ready);
+ fa->stopping = true;
+ fa_control_state_change(fa, true);
+ KUNIT_EXPECT_FALSE(test, fa->tcp_ready);
+}
+
+static void fa_test_lookup_gate_preserves_tags(struct kunit *test)
+{
+ struct bcm_ns_fa *fa = kunit_kzalloc(test, sizeof(*fa), GFP_KERNEL);
+ u32 *regs = kunit_kzalloc(test, 0xc0, GFP_KERNEL);
+ u32 control = FA_CTL_CRC_OVERWRITE | FA_CTL_NO_DA_CHECK;
+ u32 header = FA_BRCM_HW | FA_BRCM_RX | FA_BRCM_TX | FA_BRCM_PARSE_EXCEPTIONS;
+
+ FA_TEST_REQUIRE_DIRECT_MMIO(test);
+ KUNIT_ASSERT_NOT_NULL(test, fa);
+ KUNIT_ASSERT_NOT_NULL(test, regs);
+ fa->regs = (void __iomem *)regs;
+ writel(control, fa->regs + FA_CTL);
+ writel(header, fa->regs + FA_BRCM_HDR);
+ fa->enabled = true;
+ fa->tcp_ready = true;
+ fa->route_mtu_ok = true;
+ fa_lookup_update(fa);
+ KUNIT_EXPECT_EQ(test, readl(fa->regs + FA_L2_ETHERTYPE),
+ (u32)(ETH_P_IP | ETH_P_IPV6 << 16));
+ fa->control_pending = true;
+ fa_lookup_update(fa);
+ KUNIT_EXPECT_EQ(test, readl(fa->regs + FA_L2_ETHERTYPE), 0U);
+ fa->control_pending = false;
+ fa_lookup_update(fa);
+ KUNIT_EXPECT_NE(test, readl(fa->regs + FA_L2_ETHERTYPE), 0U);
+ fa->route_mtu_ok = false;
+ fa_lookup_update(fa);
+ KUNIT_EXPECT_EQ(test, readl(fa->regs + FA_L2_ETHERTYPE), 0U);
+ fa->route_mtu_ok = true;
+ fa->tcp_ready = false;
+ fa_lookup_update(fa);
+ KUNIT_EXPECT_EQ(test, readl(fa->regs + FA_L2_ETHERTYPE), 0U);
+ fa->tcp_ready = true;
+ fa->enabled = false;
+ fa_lookup_update(fa);
+ KUNIT_EXPECT_EQ(test, readl(fa->regs + FA_L2_ETHERTYPE), 0U);
+ KUNIT_EXPECT_EQ(test, readl(fa->regs + FA_CTL), control);
+ KUNIT_EXPECT_EQ(test, readl(fa->regs + FA_BRCM_HDR), header);
+}
+
+static void fa_test_mtu_cleanup(void *ptr)
+{
+ struct bcm_ns_fa *fa = ptr;
+
+ fa_fib_reset(&fa->fib_nb);
+}
+
+static void fa_test_route_mtu(struct kunit *test)
+{
+ struct bcm_ns_fa *fa = kunit_kzalloc(test, sizeof(*fa), GFP_KERNEL);
+ struct dst_metrics metrics = {};
+ struct fib_info fi = { .fib_metrics = &metrics };
+ struct fib_entry_notifier_info entry = {
+ .info.family = AF_INET, .fi = &fi, .tb_id = RT_TABLE_MAIN,
+ .dst = 0xc0000200, .dst_len = 24,
+ };
+
+ KUNIT_ASSERT_NOT_NULL(test, fa);
+ spin_lock_init(&fa->learn_lock);
+ hash_init(fa->mtu_routes);
+ KUNIT_ASSERT_EQ(test, kunit_add_action_or_reset(test, fa_test_mtu_cleanup, fa), 0);
+
+ fa_mtu_route_update(fa, FIB_EVENT_ENTRY_REPLACE, &entry);
+ KUNIT_EXPECT_TRUE(test, fa->route_mtu_ok);
+ metrics.metrics[RTAX_MTU - 1] = 1500;
+ fa_mtu_route_update(fa, FIB_EVENT_ENTRY_REPLACE, &entry);
+ KUNIT_EXPECT_TRUE(test, fa->route_mtu_ok);
+ metrics.metrics[RTAX_MTU - 1] = 1200;
+ fa_mtu_route_update(fa, FIB_EVENT_ENTRY_REPLACE, &entry);
+ KUNIT_EXPECT_FALSE(test, fa->route_mtu_ok);
+ KUNIT_EXPECT_EQ(test, fa->nr_mtu_routes, 1U);
+ /* Replays/replacements are idempotent; tables and prefixes are distinct. */
+ fa_mtu_route_update(fa, FIB_EVENT_ENTRY_REPLACE, &entry);
+ KUNIT_EXPECT_EQ(test, fa->nr_mtu_routes, 1U);
+ entry.tb_id++;
+ fa_mtu_route_update(fa, FIB_EVENT_ENTRY_REPLACE, &entry);
+ entry.dst_len++;
+ fa_mtu_route_update(fa, FIB_EVENT_ENTRY_REPLACE, &entry);
+ KUNIT_EXPECT_EQ(test, fa->nr_mtu_routes, 3U);
+ fa_mtu_route_update(fa, FIB_EVENT_ENTRY_DEL, &entry);
+ entry.dst_len--;
+ metrics.metrics[RTAX_MTU - 1] = 1500;
+ fa_mtu_route_update(fa, FIB_EVENT_ENTRY_REPLACE, &entry);
+ KUNIT_EXPECT_FALSE(test, fa->route_mtu_ok);
+ entry.tb_id--;
+ fa_mtu_route_update(fa, FIB_EVENT_ENTRY_REPLACE, &entry);
+ KUNIT_EXPECT_EQ(test, fa->nr_mtu_routes, 0U);
+ KUNIT_EXPECT_TRUE(test, fa->route_mtu_ok);
+ /* A registration retry must discard a partial replay before starting over. */
+ metrics.metrics[RTAX_MTU - 1] = 1000;
+ fa_mtu_route_update(fa, FIB_EVENT_ENTRY_REPLACE, &entry);
+ fa_fib_reset(&fa->fib_nb);
+ KUNIT_EXPECT_TRUE(test, hash_empty(fa->mtu_routes));
+ KUNIT_EXPECT_TRUE(test, fa->route_mtu_ok);
+}
+
+static void fa_test_route_mtu_capacity(struct kunit *test)
+{
+ struct bcm_ns_fa *fa = kunit_kzalloc(test, sizeof(*fa), GFP_KERNEL);
+ struct dst_metrics metrics = {};
+ struct fib_info fi = { .fib_metrics = &metrics };
+ struct fib_entry_notifier_info entry = {
+ .info.family = AF_INET, .fi = &fi, .tb_id = RT_TABLE_MAIN, .dst_len = 32,
+ };
+ unsigned int i;
+
+ KUNIT_ASSERT_NOT_NULL(test, fa);
+ spin_lock_init(&fa->learn_lock);
+ hash_init(fa->mtu_routes);
+ KUNIT_ASSERT_EQ(test, kunit_add_action_or_reset(test, fa_test_mtu_cleanup, fa), 0);
+ /* Ordinary routes consume no tracking memory, regardless of FIB size. */
+ for (i = 0; i < 10000; i++) {
+ entry.dst = i;
+ fa_mtu_route_update(fa, FIB_EVENT_ENTRY_REPLACE, &entry);
+ }
+ KUNIT_EXPECT_EQ(test, fa->nr_mtu_routes, 0U);
+ KUNIT_EXPECT_TRUE(test, fa->route_mtu_ok);
+ metrics.metrics[RTAX_MTU - 1] = 1200;
+ for (i = 0; i <= FA_MTU_ROUTES_MAX; i++) {
+ entry.dst = i;
+ fa_mtu_route_update(fa, FIB_EVENT_ENTRY_REPLACE, &entry);
+ }
+ KUNIT_EXPECT_EQ(test, fa->nr_mtu_routes, (unsigned int)FA_MTU_ROUTES_MAX);
+ KUNIT_EXPECT_TRUE(test, fa->mtu_tracking_failed);
+ for (i = 0; i < FA_MTU_ROUTES_MAX; i++) {
+ entry.dst = i;
+ fa_mtu_route_update(fa, FIB_EVENT_ENTRY_DEL, &entry);
+ }
+ KUNIT_EXPECT_EQ(test, fa->nr_mtu_routes, 0U);
+ KUNIT_EXPECT_FALSE(test, fa->route_mtu_ok);
+ fa_fib_reset(&fa->fib_nb);
+ KUNIT_EXPECT_TRUE(test, fa->route_mtu_ok);
+}
+
+static void fa_test_route_suitability(struct kunit *test)
+{
+ struct dst_metrics metrics = {};
+ struct fib_info fi = { .fib_metrics = &metrics, .fib_nhs = 1 };
+ struct fib_nh_common nhc = {};
+ struct fib_result res = { .fi = &fi, .nhc = &nhc, .type = RTN_UNICAST };
+ struct fa_rule rule = {};
+
+ rule.out = kunit_kzalloc(test, sizeof(*rule.out), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, rule.out);
+ nhc.nhc_dev = rule.out;
+ rule.out_l3 = rule.out;
+ KUNIT_EXPECT_EQ(test, fa_route_supported(&rule, &res), 0);
+ /* A partial driver-side lookup cannot establish a stale owner. */
+ fi.fib_nhs = 2;
+ KUNIT_EXPECT_EQ(test, fa_route_supported(&rule, &res), -EOPNOTSUPP);
+ fi.fib_nhs = 1;
+ fi.nh = kunit_kzalloc(test, sizeof(*fi.nh), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, fi.nh);
+ KUNIT_EXPECT_EQ(test, fa_route_supported(&rule, &res), -EOPNOTSUPP);
+ fi.nh = NULL;
+ nhc.nhc_lwtstate = kunit_kzalloc(test, sizeof(*nhc.nhc_lwtstate), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, nhc.nhc_lwtstate);
+ KUNIT_EXPECT_EQ(test, fa_route_supported(&rule, &res), -EOPNOTSUPP);
+ nhc.nhc_lwtstate = NULL;
+ metrics.metrics[RTAX_MTU - 1] = 1200;
+ KUNIT_EXPECT_EQ(test, fa_route_supported(&rule, &res), -EOPNOTSUPP);
+ metrics.metrics[RTAX_MTU - 1] = 0;
+ nhc.nhc_gw_family = AF_INET6;
+ KUNIT_EXPECT_EQ(test, fa_route_supported(&rule, &res), -EOPNOTSUPP);
+ nhc.nhc_gw_family = 0;
+ nhc.nhc_dev = NULL;
+ KUNIT_EXPECT_EQ(test, fa_route_supported(&rule, &res), -EOPNOTSUPP);
+ nhc.nhc_dev = rule.out;
+ res.type = RTN_BLACKHOLE;
+ KUNIT_EXPECT_EQ(test, fa_route_supported(&rule, &res), -EOPNOTSUPP);
+}
+
+struct fa_test_owner {
+ struct bcm_ns_fa *fa;
+ unsigned int calls;
+ bool lookup_stopped;
+};
+
+static void fa_test_owner_invalidate(unsigned long cookie)
+{
+ struct fa_test_owner *owner = (struct fa_test_owner *)cookie;
+
+ owner->calls++;
+ owner->lookup_stopped = !readl(owner->fa->regs + FA_L2_ETHERTYPE);
+}
+
+static void fa_test_empty_work(struct work_struct *work)
+{
+}
+
+static void fa_test_invalidation(struct kunit *test)
+{
+ struct bcm_ns_fa *fa = kunit_kzalloc(test, sizeof(*fa), GFP_KERNEL);
+ struct fa_test_owner owner = { .fa = fa };
+ struct fa_flow entry = {
+ .cookie = (unsigned long)&owner,
+ .invalidate = fa_test_owner_invalidate,
+ };
+
+ FA_TEST_REQUIRE_DIRECT_MMIO(test);
+ KUNIT_ASSERT_NOT_NULL(test, fa);
+ fa->regs = (void __iomem *)kunit_kzalloc(test, 0xc0, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, fa->regs);
+ spin_lock_init(&fa->learn_lock);
+ INIT_WORK(&fa->control_work, fa_test_empty_work);
+ INIT_LIST_HEAD(&fa->entries);
+ list_add(&entry.list, &fa->entries);
+ fa->enabled = true;
+ fa->tcp_ready = true;
+ fa->route_mtu_ok = true;
+ fa_lookup_update(fa);
+ fa_invalidate(fa);
+ flush_work(&fa->control_work);
+ KUNIT_EXPECT_EQ(test, owner.calls, 1U);
+ KUNIT_EXPECT_TRUE(test, owner.lookup_stopped);
+ KUNIT_EXPECT_TRUE(test, test_bit(FA_FLOW_CLOSING, &entry.flags));
+ KUNIT_EXPECT_TRUE(test, fa->control_pending);
+ KUNIT_EXPECT_TRUE(test, fa->route_mtu_ok);
+ KUNIT_EXPECT_EQ(test, fa->invalidate_generation, 1UL);
+}
+
+struct fa_test_guards {
+ unsigned int adds, deletes;
+};
+
+static int fa_test_set_guard(void *priv, unsigned int slot, const u8 *addr, unsigned int port)
+{
+ struct fa_test_guards *guards = priv;
+
+ if (addr)
+ guards->adds++;
+ else
+ guards->deletes++;
+ return 0;
+}
+
+static void fa_test_unregister_switch(void *priv)
+{
+ brcm_fa_switch_unregister(priv);
+}
+
+static void fa_test_client_guards(struct kunit *test)
+{
+ struct bcm_ns_fa *fa = kunit_kzalloc(test, sizeof(*fa), GFP_KERNEL);
+ struct fa_test_guards *guards = kunit_kzalloc(test, sizeof(*guards), GFP_KERNEL);
+ struct brcm_fa_switch *sw = kunit_kzalloc(test, sizeof(*sw), GFP_KERNEL);
+ u8 addr[ETH_ALEN] = { 2, 0, 0, 0, 0, 1 };
+ int first, shared, i;
+
+ KUNIT_ASSERT_NOT_NULL(test, fa);
+ KUNIT_ASSERT_NOT_NULL(test, guards);
+ KUNIT_ASSERT_NOT_NULL(test, sw);
+ /* The opaque conduit identity need not be registered for the registry. */
+ fa->conduit = kunit_kzalloc(test, sizeof(*fa->conduit), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, fa->conduit);
+ sw->conduit = fa->conduit;
+ sw->set_guard = fa_test_set_guard;
+ sw->priv = guards;
+ brcm_fa_switch_register(sw);
+ KUNIT_ASSERT_EQ(test, kunit_add_action_or_reset(test, fa_test_unregister_switch, sw), 0);
+ first = fa_client_get_port(fa, addr, 0);
+ KUNIT_ASSERT_GT(test, first, 0);
+ shared = fa_client_get_port(fa, addr, 0);
+ KUNIT_EXPECT_EQ(test, shared, first);
+ KUNIT_EXPECT_EQ(test, guards->adds, 1U);
+ /* A move cannot silently replace the guard of another live owner. */
+ KUNIT_EXPECT_EQ(test, fa_client_get_port(fa, addr, 1), -EAGAIN);
+ fa_client_put(fa, shared);
+ KUNIT_EXPECT_EQ(test, guards->deletes, 0U);
+ fa_client_put(fa, first);
+ KUNIT_EXPECT_EQ(test, guards->deletes, 1U);
+
+ for (i = 0; i < BRCM_FA_BRIDGE_GUARDS; i++) {
+ addr[5] = i;
+ KUNIT_ASSERT_GT(test, fa_client_get_port(fa, addr, 0), 0);
+ }
+ addr[4]++;
+ KUNIT_EXPECT_EQ(test, fa_client_get_port(fa, addr, 0), -ENOSPC);
+ for (i = 1; i <= BRCM_FA_BRIDGE_GUARDS; i++)
+ fa_client_put(fa, i);
+ KUNIT_EXPECT_EQ(test, guards->adds, guards->deletes);
+ KUNIT_EXPECT_EQ(test, brcm_fa_bridge_guard(fa->conduit, BRCM_FA_BRIDGE_GUARDS,
+ addr, 0), -EINVAL);
+}
+
+static struct kunit_case fa_test_cases[] = {
+ KUNIT_CASE(fa_test_upper_device_rcu),
+ KUNIT_CASE(fa_test_bridge_state_rcu),
+ KUNIT_CASE(fa_test_client_guards),
+ KUNIT_CASE(fa_test_encode),
+ KUNIT_CASE(fa_test_mac_encode),
+ KUNIT_CASE(fa_test_nat_actions),
+ KUNIT_CASE(fa_test_ethernet_actions),
+ KUNIT_CASE(fa_test_rx_learning),
+ KUNIT_CASE(fa_test_tcp_encode_and_mangle),
+ KUNIT_CASE(fa_test_tcp_identity),
+ KUNIT_CASE(fa_test_tcp_rx_control),
+ KUNIT_CASE(fa_test_tcp_control_readiness),
+ KUNIT_CASE(fa_test_lookup_gate_preserves_tags),
+ KUNIT_CASE(fa_test_route_mtu),
+ KUNIT_CASE(fa_test_route_mtu_capacity),
+ KUNIT_CASE(fa_test_route_suitability),
+ KUNIT_CASE(fa_test_invalidation),
+ {}
+};
+
+static struct kunit_suite fa_test_suite = {
+ .name = "bcm-ns-fa",
+ .test_cases = fa_test_cases,
+};
+
+kunit_test_suite(fa_test_suite);
diff --git a/drivers/net/ethernet/broadcom/bcm-ns-fa.c b/drivers/net/ethernet/broadcom/bcm-ns-fa.c
index 800381a828ef5..c39de5d4a0106 100644
--- a/drivers/net/ethernet/broadcom/bcm-ns-fa.c
+++ b/drivers/net/ethernet/broadcom/bcm-ns-fa.c
@@ -2210,5 +2210,9 @@ struct bcm_ns_fa *bcm_ns_fa_init(struct net_device *dev)
}
EXPORT_SYMBOL_GPL(bcm_ns_fa_init);
+#if IS_ENABLED(CONFIG_BCM_NS_FA_KUNIT_TEST)
+#include "bcm-ns-fa-test.c"
+#endif
+
MODULE_DESCRIPTION("Broadcom Northstar IPv4 TCP/UDP NAT flow accelerator");
MODULE_LICENSE("GPL");
diff --git a/net/dsa/Kconfig b/net/dsa/Kconfig
index d62de6f716cd9..16adbe4d54f7a 100644
--- a/net/dsa/Kconfig
+++ b/net/dsa/Kconfig
@@ -81,6 +81,17 @@ config NET_DSA_TAG_BRCM_FA
The B53 driver selects this tagger and uses it only for a switch
explicitly connected to an accelerator.
+config NET_DSA_TAG_BRCM_FA_KUNIT_TEST
+ bool "KUnit tests for Broadcom FA tags" if !KUNIT_ALL_TESTS
+ depends on KUNIT=y && NET_DSA_TAG_BRCM_FA
+ default KUNIT_ALL_TESTS
+ help
+ Tests are compiled into the parent driver and require built-in KUnit.
+ Verify FA miss demultiplexing, parser-status handling, and rejection
+ of unsupported opcodes without changing ordinary Broadcom tagging.
+ Exercise source-port demultiplexing and consumption of the metadata
+ tag through the receive path, without requiring a physical switch.
+
config NET_DSA_TAG_HELLCREEK
tristate "Tag driver for Hirschmann Hellcreek TSN switches"
help
diff --git a/net/dsa/tag_brcm.c b/net/dsa/tag_brcm.c
index df1e43f8b11a9..e41b409400be7 100644
--- a/net/dsa/tag_brcm.c
+++ b/net/dsa/tag_brcm.c
@@ -577,5 +577,9 @@ static struct dsa_tag_driver *dsa_tag_driver_array[] = {
module_dsa_tag_drivers(dsa_tag_driver_array);
+#if IS_ENABLED(CONFIG_NET_DSA_TAG_BRCM_FA_KUNIT_TEST)
+#include "tag_brcm_test.c"
+#endif
+
MODULE_DESCRIPTION("DSA tag driver for Broadcom switches using in-frame headers");
MODULE_LICENSE("GPL");
diff --git a/net/dsa/tag_brcm_test.c b/net/dsa/tag_brcm_test.c
new file mode 100644
index 0000000000000..e9c71e29a3781
--- /dev/null
+++ b/net/dsa/tag_brcm_test.c
@@ -0,0 +1,200 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/* Included in tag_brcm.c: test the real receive path with a minimal DSA tree. */
+#include <kunit/test.h>
+
+struct brcm_fa_test_ctx {
+ struct dsa_switch_tree tree;
+ struct dsa_switch ds;
+ struct dsa_port cpu, user;
+ struct net_device *conduit, *port;
+};
+
+static void brcm_fa_test_free_netdev(void *ptr)
+{
+ free_netdev(ptr);
+}
+
+static int brcm_fa_test_init(struct kunit *test)
+{
+ struct brcm_fa_test_ctx *ctx;
+ struct dsa_user_priv *priv;
+ int err;
+
+ ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, ctx);
+ ctx->conduit = alloc_etherdev(0);
+ KUNIT_ASSERT_NOT_NULL(test, ctx->conduit);
+ err = kunit_add_action_or_reset(test, brcm_fa_test_free_netdev, ctx->conduit);
+ if (err)
+ return err;
+ ctx->port = alloc_etherdev(sizeof(*priv));
+ KUNIT_ASSERT_NOT_NULL(test, ctx->port);
+ err = kunit_add_action_or_reset(test, brcm_fa_test_free_netdev, ctx->port);
+ if (err)
+ return err;
+ INIT_LIST_HEAD(&ctx->tree.ports);
+ ctx->ds.dst = &ctx->tree;
+ ctx->cpu.index = 8;
+ ctx->cpu.type = DSA_PORT_TYPE_CPU;
+ ctx->cpu.ds = &ctx->ds;
+ ctx->cpu.dst = &ctx->tree;
+ ctx->user.ds = &ctx->ds;
+ ctx->user.dst = &ctx->tree;
+ ctx->user.index = 3;
+ ctx->user.type = DSA_PORT_TYPE_USER;
+ ctx->user.user = ctx->port;
+ list_add(&ctx->user.list, &ctx->tree.ports);
+ ctx->conduit->dsa_ptr = &ctx->cpu;
+ priv = netdev_priv(ctx->port);
+ priv->dp = &ctx->user;
+ test->priv = ctx;
+ return 0;
+}
+
+static struct sk_buff *brcm_fa_test_skb(u8 opcode, u8 status, u8 port)
+{
+ struct sk_buff *skb = alloc_skb(64, GFP_KERNEL);
+ u8 *data;
+
+ if (!skb)
+ return NULL;
+ data = skb_put_zero(skb, 64);
+ data[0] = opcode << BRCM_OPCODE_SHIFT;
+ data[1] = 0xff; /* Hash: not a classification identifier. */
+ data[2] = status;
+ data[3] = port;
+ /* Reproduce eth_type_trans(): the tagger gets data after ETH_HLEN. */
+ skb_reset_mac_header(skb);
+ skb_pull(skb, ETH_HLEN);
+ return skb;
+}
+
+static void brcm_fa_test_miss(struct kunit *test)
+{
+ struct brcm_fa_test_ctx *ctx = test->priv;
+ struct sk_buff *skb, *result;
+ u8 *start;
+
+ /* A parser failure is for the software IP stack to handle; it must
+ * not be confused with the reserved bits in a normal switch reason.
+ */
+ skb = brcm_fa_test_skb(BRCM_FA_OPCODE_MISS, 0xff, 3);
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ start = skb->data;
+ result = brcm_fa_tag_rcv(skb, ctx->conduit);
+ KUNIT_ASSERT_NOT_NULL(test, result);
+ KUNIT_EXPECT_PTR_EQ(test, result->dev, ctx->port);
+ KUNIT_EXPECT_PTR_EQ(test, result->data, start + BRCM_TAG_LEN);
+ KUNIT_EXPECT_EQ(test, result->len, 64U - ETH_HLEN - BRCM_TAG_LEN);
+ kfree_skb(result);
+}
+
+static void brcm_fa_test_opcodes(struct kunit *test)
+{
+ struct brcm_fa_test_ctx *ctx = test->priv;
+ struct sk_buff *skb, *result;
+ int opcode;
+
+ for (opcode = 0; opcode < 8; opcode++) {
+ skb = brcm_fa_test_skb(opcode, 0, 3);
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ result = brcm_fa_tag_rcv(skb, ctx->conduit);
+ if (!opcode || opcode == BRCM_FA_OPCODE_MISS) {
+ KUNIT_ASSERT_NOT_NULL(test, result);
+ kfree_skb(result);
+ } else {
+ KUNIT_EXPECT_PTR_EQ(test, result, NULL);
+ }
+ }
+}
+
+static void brcm_fa_test_strict_normal_tag(struct kunit *test)
+{
+ struct brcm_fa_test_ctx *ctx = test->priv;
+ struct sk_buff *skb;
+
+ skb = brcm_fa_test_skb(BRCM_FA_OPCODE_MISS, 0, 3);
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ KUNIT_EXPECT_PTR_EQ(test, brcm_tag_rcv_ll(skb, ctx->conduit, ETH_HLEN, false), NULL);
+ skb = brcm_fa_test_skb(0, BRCM_EG_RC_RSVD, 3);
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ KUNIT_EXPECT_PTR_EQ(test, brcm_fa_tag_rcv(skb, ctx->conduit), NULL);
+ skb = brcm_fa_test_skb(BRCM_FA_OPCODE_MISS, 0, 7);
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ KUNIT_EXPECT_PTR_EQ(test, brcm_fa_tag_rcv(skb, ctx->conduit), NULL);
+}
+
+struct brcm_fa_control_test {
+ unsigned int calls, port;
+ __be16 protocol;
+};
+
+static void brcm_fa_test_receive(void *priv, struct sk_buff *skb, unsigned int port)
+{
+ struct brcm_fa_control_test *state = priv;
+
+ state->calls++;
+ state->port = port;
+ state->protocol = eth_hdr(skb)->h_proto;
+}
+
+static void brcm_fa_test_auxiliary_control(struct kunit *test)
+{
+ struct brcm_fa_test_ctx *ctx = test->priv;
+ struct brcm_fa_control_test state = {};
+ struct brcm_fa_control control = {
+ .conduit = ctx->port,
+ .receive = brcm_fa_test_receive,
+ .priv = &state,
+ };
+ struct dsa_port *data_cpu;
+ struct sk_buff *skb, *result;
+ u8 *data;
+
+ data_cpu = kunit_kzalloc(test, sizeof(*data_cpu), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, data_cpu);
+ data_cpu->ds = &ctx->ds;
+ data_cpu->type = DSA_PORT_TYPE_CPU;
+ data_cpu->index = 8;
+ data_cpu->conduit = ctx->port;
+ list_add(&data_cpu->list, &ctx->tree.ports);
+ ctx->cpu.index = 5;
+ skb = alloc_skb(64, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ data = skb_put_zero(skb, 64);
+ data[0] = 2;
+ data[6] = 2;
+ data[13] = 1; /* Classification ID. */
+ data[14] = BRCM_EG_RC_PROT_TERM;
+ data[15] = 3;
+ put_unaligned_be16(ETH_P_IP, data + 16);
+ skb_reset_mac_header(skb);
+ skb_pull(skb, ETH_HLEN);
+ brcm_fa_control_register(&control);
+ result = brcm_fa_tag_rcv(skb, ctx->conduit);
+ brcm_fa_control_unregister(&control);
+ KUNIT_ASSERT_NOT_NULL(test, result);
+ KUNIT_EXPECT_PTR_EQ(test, result->dev, ctx->port);
+ KUNIT_EXPECT_EQ(test, result->len, 64U - ETH_HLEN - BRCM_TAG_LEN);
+ KUNIT_EXPECT_FALSE(test, result->offload_fwd_mark);
+ KUNIT_EXPECT_EQ(test, state.calls, 1U);
+ KUNIT_EXPECT_EQ(test, state.port, 3U);
+ KUNIT_EXPECT_EQ(test, state.protocol, htons(ETH_P_IP));
+ kfree_skb(result);
+}
+
+static struct kunit_case brcm_fa_test_cases[] = {
+ KUNIT_CASE(brcm_fa_test_miss),
+ KUNIT_CASE(brcm_fa_test_auxiliary_control),
+ KUNIT_CASE(brcm_fa_test_opcodes),
+ KUNIT_CASE(brcm_fa_test_strict_normal_tag),
+ {}
+};
+
+static struct kunit_suite brcm_fa_test_suite = {
+ .name = "brcm-fa-tag",
+ .init = brcm_fa_test_init,
+ .test_cases = brcm_fa_test_cases,
+};
+
+kunit_test_suite(brcm_fa_test_suite);
diff --git a/net/netfilter/Kconfig b/net/netfilter/Kconfig
index 952724e462043..b80c568cc5da9 100644
--- a/net/netfilter/Kconfig
+++ b/net/netfilter/Kconfig
@@ -719,16 +719,17 @@ config NF_FLOW_TABLE
To compile it as a module, choose M here.
config NF_FLOW_TABLE_KUNIT_TEST
- bool "KUnit tests for flowtable ownership" if !KUNIT_ALL_TESTS
+ bool "KUnit tests for flowtable ownership and cached paths" if !KUNIT_ALL_TESTS
depends on KUNIT=y && NF_FLOW_TABLE
default KUNIT_ALL_TESTS
help
- Test conntrack offload ownership during flowtable teardown.
- Check that GC keeps ownership until the flow is removed from the
- hash tables, after a pending hardware deletion has run, and that
- late teardown notifications cannot release a replacement owner.
- Check that GC does not extend a conntrack timeout repaired by teardown.
- These private tests are embedded in the flowtable core.
+ Test that flowtable GC keeps conntrack ownership until the flow is
+ removed from the hash tables, after a pending hardware deletion has
+ run, that repeated teardown notifications cannot release a
+ replacement owner, and that GC does not extend a timeout repaired by
+ teardown. Also validate cached forwarding paths.
+ Tests cover route expiry, neighbour changes and unsupported contexts.
+ The tests are embedded in their parent translation units.
config NF_FLOW_TABLE_PROCFS
bool "Supply flow table statistics in procfs"
diff --git a/net/netfilter/nf_flow_table_core_test.c b/net/netfilter/nf_flow_table_core_test.c
index 9bc7338add82d..faaedf671f648 100644
--- a/net/netfilter/nf_flow_table_core_test.c
+++ b/net/netfilter/nf_flow_table_core_test.c
@@ -110,10 +110,213 @@ static void nf_flow_test_extend_after_teardown(struct kunit *test)
KUNIT_EXPECT_EQ(test, refcount_read(&ct->ct_general.use), 2);
}
+static struct dst_entry *nf_flow_test_gc_dst_check(struct dst_entry *dst, u32 cookie)
+{
+ return dst->error ? NULL : dst;
+}
+
+static void nf_flow_test_gc_routes(struct kunit *test)
+{
+ static const struct {
+ const char *name;
+ enum flow_offload_xmit_type type;
+ bool keep_dst;
+ bool tunnel;
+ bool check;
+ } cases[] = {
+ { "software direct", FLOW_OFFLOAD_XMIT_DIRECT, false, false, false },
+ { "validation only", FLOW_OFFLOAD_XMIT_DIRECT, true, false, false },
+ { "direct tunnel", FLOW_OFFLOAD_XMIT_DIRECT, false, true, true },
+ { "HW direct tunnel", FLOW_OFFLOAD_XMIT_DIRECT, true, true, true },
+ { "neighbour", FLOW_OFFLOAD_XMIT_NEIGH, false, false, true },
+ { "HW neighbour", FLOW_OFFLOAD_XMIT_NEIGH, true, false, true },
+ { "XFRM", FLOW_OFFLOAD_XMIT_XFRM, true, false, true },
+ };
+ static struct dst_ops ops = {
+ .family = AF_INET,
+ .check = nf_flow_test_gc_dst_check,
+ };
+ static struct nf_flowtable_type type;
+ unsigned int i, dir;
+
+ for (i = 0; i < ARRAY_SIZE(cases); i++) {
+ struct nf_flowtable table = { .type = &type };
+ struct nf_flow_route route = {};
+ struct flow_offload *flow;
+ struct net_device *dev;
+ struct rtable *rt;
+ struct nf_conn *ct;
+
+ flow = kunit_kzalloc(test, sizeof(*flow), GFP_KERNEL);
+ dev = kunit_kzalloc(test, sizeof(*dev), GFP_KERNEL);
+ rt = kunit_kcalloc(test, FLOW_OFFLOAD_DIR_MAX, sizeof(*rt), GFP_KERNEL);
+ ct = kunit_kzalloc(test, sizeof(*ct), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, flow);
+ KUNIT_ASSERT_NOT_NULL(test, dev);
+ KUNIT_ASSERT_NOT_NULL(test, rt);
+ KUNIT_ASSERT_NOT_NULL(test, ct);
+ dev_net_set(dev, &init_net);
+ dev->mtu = 1500;
+ write_pnet(&ct->ct_net, &init_net);
+ ct->tuplehash[0].tuple.dst.protonum = IPPROTO_ICMP;
+ refcount_set(&ct->ct_general.use, 2);
+ flow->ct = ct;
+ flow->timeout = nf_flowtable_time_stamp + NF_FLOW_TIMEOUT;
+ /* Torn down flows are hardware flows with their deletion already
+ * queued, so GC leaves them alone, rather than freeing the test
+ * objects. Others are software flows: GC does nothing for them,
+ * and cannot queue any STATS work, which would outlive the test.
+ */
+ if (cases[i].check) {
+ set_bit(NF_FLOW_HW, &flow->flags);
+ set_bit(NF_FLOW_HW_DYING, &flow->flags);
+ }
+ set_bit(IPS_OFFLOAD_BIT, &ct->status);
+ for (dir = 0; dir < FLOW_OFFLOAD_DIR_MAX; dir++) {
+ struct dst_entry *dst = &rt[dir].dst;
+
+ dst->dev = dev;
+ dst->ops = &ops;
+ dst->obsolete = DST_OBSOLETE_FORCE_CHK;
+ dst_init_metrics(dst, dst_default_metrics.metrics, true);
+ /* One fixture reference and one transferred route reference. */
+ rcuref_init(&dst->__rcuref, 2);
+ flow->tuplehash[dir].tuple.l3proto = NFPROTO_IPV4;
+ route.tuple[dir].dst = dst;
+ route.tuple[dir].xmit_type = cases[i].type;
+ route.tuple[dir].in.num_tuns = cases[i].tunnel;
+ }
+ route.uses_bridge = i == 0;
+ route.bridge_genid = atomic_read(&init_net.ft.bridge_genid);
+ flow_offload_route_init(flow, &route, cases[i].keep_dst);
+ KUNIT_EXPECT_EQ(test, test_bit(NF_FLOW_BRIDGE, &flow->flags), route.uses_bridge);
+ /* Simulate the expired dst returned after an IPv4 generation change. */
+ rt[0].dst.error = ENETUNREACH;
+ KUNIT_EXPECT_EQ_MSG(test,
+ !nf_flow_dst_check(&flow->tuplehash[0].tuple), cases[i].check,
+ "packet path: %s", cases[i].name);
+ nf_flow_offload_gc_step(&table, flow, NULL);
+ KUNIT_EXPECT_EQ_MSG(test, test_bit(NF_FLOW_TEARDOWN, &flow->flags),
+ cases[i].check, "GC path: %s", cases[i].name);
+ flow_offload_route_release(flow);
+ for (dir = 0; dir < FLOW_OFFLOAD_DIR_MAX; dir++)
+ KUNIT_EXPECT_EQ(test, rcuref_read(&rt[dir].dst.__rcuref), 1);
+ }
+}
+
+static void nf_flow_test_bridge_cache(struct kunit *test)
+{
+ static struct nf_flowtable_type type;
+ struct net_device *bridge;
+ struct net *net;
+ unsigned int i, dir;
+
+ net = kunit_kzalloc(test, sizeof(*net), GFP_KERNEL);
+ bridge = kunit_kzalloc(test, sizeof(*bridge), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, net);
+ KUNIT_ASSERT_NOT_NULL(test, bridge);
+ dev_net_set(bridge, net);
+
+ for (i = 0; i < 3; i++) {
+ struct nf_flowtable table = { .type = &type };
+ struct flow_offload_tuple_rhash *tuplehash;
+ struct flow_offload *flow;
+ struct nf_conn *ct;
+ bool bridged = i != 0;
+ int err;
+
+ ct = kunit_kzalloc(test, sizeof(*ct), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, ct);
+ err = rhashtable_init(&table.rhashtable, &nf_flow_offload_rhash_params);
+ KUNIT_ASSERT_EQ(test, err, 0);
+ flow = kzalloc_obj(*flow);
+ if (!flow) {
+ rhashtable_destroy(&table.rhashtable);
+ KUNIT_FAIL(test, "flow allocation failed");
+ return;
+ }
+ write_pnet(&ct->ct_net, net);
+ ct->tuplehash[0].tuple.dst.protonum = IPPROTO_ICMP;
+ refcount_set(&ct->ct_general.use, 2);
+ set_bit(IPS_OFFLOAD_BIT, &ct->status);
+ flow->ct = ct;
+ flow->bridge_genid = atomic_read(&net->ft.bridge_genid);
+ if (bridged)
+ __set_bit(NF_FLOW_BRIDGE, &flow->flags);
+ for (dir = 0; dir < FLOW_OFFLOAD_DIR_MAX; dir++) {
+ flow->tuplehash[dir].tuple.dir = dir;
+ flow->tuplehash[dir].tuple.src_port = htons(10000 + dir);
+ }
+ err = flow_offload_add(&table, flow);
+ KUNIT_EXPECT_EQ(test, err, 0);
+ if (err) {
+ flow_offload_free(flow);
+ goto destroy;
+ }
+ rcu_read_lock();
+ tuplehash = flow_offload_lookup(&table, &flow->tuplehash[0].tuple);
+ KUNIT_EXPECT_NOT_NULL(test, tuplehash);
+ nf_flow_table_netevent(NULL, NETEVENT_BRIDGE_FDB_UPDATE, bridge);
+ KUNIT_EXPECT_EQ(test, nf_flow_bridge_stale(flow), bridged);
+ if (i == 1) {
+ /* A cached reply must miss immediately after a client move. */
+ tuplehash = flow_offload_lookup(&table, &flow->tuplehash[1].tuple);
+ KUNIT_EXPECT_PTR_EQ(test, tuplehash, NULL);
+ KUNIT_EXPECT_TRUE(test, test_bit(NF_FLOW_TEARDOWN, &flow->flags));
+ } else if (!bridged) {
+ tuplehash = flow_offload_lookup(&table, &flow->tuplehash[1].tuple);
+ KUNIT_EXPECT_NOT_NULL(test, tuplehash);
+ KUNIT_EXPECT_FALSE(test, test_bit(NF_FLOW_TEARDOWN, &flow->flags));
+ }
+ if (!bridged)
+ flow_offload_teardown(flow);
+ /* Also exercise GC without a packet-path lookup (i == 2).
+ * Admission stays claimed until both hash entries are removed.
+ */
+ KUNIT_EXPECT_TRUE(test, test_bit(IPS_OFFLOAD_BIT, &ct->status));
+ nf_flow_offload_gc_step(&table, flow, NULL);
+ KUNIT_EXPECT_FALSE(test, test_bit(IPS_OFFLOAD_BIT, &ct->status));
+ tuplehash = flow_offload_lookup(&table, &flow->tuplehash[0].tuple);
+ KUNIT_EXPECT_PTR_EQ(test, tuplehash, NULL);
+ rcu_read_unlock();
+destroy:
+ rcu_barrier();
+ KUNIT_EXPECT_EQ(test, refcount_read(&ct->ct_general.use), 1);
+ rhashtable_destroy(&table.rhashtable);
+ }
+}
+
+static void nf_flow_test_bridge_construction_race(struct kunit *test)
+{
+ struct nf_flowtable table = {};
+ struct flow_offload flow = {};
+ struct net_device *bridge;
+ struct nf_conn *ct;
+ struct net *net;
+
+ net = kunit_kzalloc(test, sizeof(*net), GFP_KERNEL);
+ bridge = kunit_kzalloc(test, sizeof(*bridge), GFP_KERNEL);
+ ct = kunit_kzalloc(test, sizeof(*ct), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, net);
+ KUNIT_ASSERT_NOT_NULL(test, bridge);
+ KUNIT_ASSERT_NOT_NULL(test, ct);
+ dev_net_set(bridge, net);
+ write_pnet(&ct->ct_net, net);
+ flow.ct = ct;
+ flow.bridge_genid = atomic_read_acquire(&net->ft.bridge_genid);
+ __set_bit(NF_FLOW_BRIDGE, &flow.flags);
+ nf_flow_table_netevent(NULL, NETEVENT_BRIDGE_FDB_UPDATE, bridge);
+ /* An FDB mutation between path lookup and publication prevents admission. */
+ KUNIT_EXPECT_EQ(test, flow_offload_add(&table, &flow), -ESTALE);
+}
+
static struct kunit_case nf_flow_lifetime_cases[] = {
KUNIT_CASE(nf_flow_test_teardown_repeat),
KUNIT_CASE(nf_flow_test_hardware_retirement),
KUNIT_CASE(nf_flow_test_extend_after_teardown),
+ KUNIT_CASE(nf_flow_test_gc_routes),
+ KUNIT_CASE(nf_flow_test_bridge_cache),
+ KUNIT_CASE(nf_flow_test_bridge_construction_race),
{}
};
diff --git a/net/netfilter/nf_flow_table_offload.c b/net/netfilter/nf_flow_table_offload.c
index daf42db2b31cf..acb4ea90688e8 100644
--- a/net/netfilter/nf_flow_table_offload.c
+++ b/net/netfilter/nf_flow_table_offload.c
@@ -1463,3 +1463,7 @@ void nf_flow_table_offload_exit(void)
destroy_workqueue(nf_flow_offload_del_wq);
destroy_workqueue(nf_flow_offload_stats_wq);
}
+
+#if IS_ENABLED(CONFIG_NF_FLOW_TABLE_KUNIT_TEST)
+#include "nf_flow_table_offload_test.c"
+#endif
diff --git a/net/netfilter/nf_flow_table_offload_test.c b/net/netfilter/nf_flow_table_offload_test.c
new file mode 100644
index 0000000000000..c6393e6cf74d8
--- /dev/null
+++ b/net/netfilter/nf_flow_table_offload_test.c
@@ -0,0 +1,242 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <kunit/test.h>
+
+struct nf_flow_test_path {
+ struct dst_entry dst;
+ struct neighbour neigh;
+ bool no_neigh;
+};
+
+struct nf_flow_test_ctx {
+ struct flow_offload flow;
+ struct nf_flow_test_path paths[FLOW_OFFLOAD_DIR_MAX];
+ struct net_device *dev;
+ struct net_device bridge;
+ struct net_device *bridge_port;
+ bool bridge_vlan;
+};
+
+static int nf_flow_test_bridge_path(struct net_device_path_ctx *ctx,
+ struct net_device_path *path)
+{
+ struct nf_flow_test_ctx *test_ctx = container_of(ctx->dev, struct nf_flow_test_ctx, bridge);
+
+ if (!test_ctx->bridge_port)
+ return -ENOENT;
+ path->type = DEV_PATH_BRIDGE;
+ path->dev = ctx->dev;
+ path->bridge.vlan_id = test_ctx->bridge_vlan;
+ path->bridge.vlan_mode = DEV_PATH_BR_VLAN_KEEP;
+ ctx->dev = test_ctx->bridge_port;
+ return 0;
+}
+
+static const struct net_device_ops nf_flow_test_bridge_ops = {
+ .ndo_fill_forward_path = nf_flow_test_bridge_path,
+};
+
+static const struct net_device_ops nf_flow_test_port_ops;
+
+static struct dst_entry *nf_flow_test_dst_check(struct dst_entry *dst, u32 cookie)
+{
+ return !dst->error && cookie == 42 ? dst : NULL;
+}
+
+static struct neighbour *nf_flow_test_neigh(const struct dst_entry *dst,
+ struct net_device *dev,
+ const struct flow_offload_tuple *other)
+{
+ struct nf_flow_test_path *path = container_of(dst, struct nf_flow_test_path, dst);
+
+ return path->no_neigh ? NULL : &path->neigh;
+}
+
+static int nf_flow_test_init(struct kunit *test)
+{
+ static struct dst_ops ops = {
+ .family = AF_INET,
+ .check = nf_flow_test_dst_check,
+ };
+ const u8 mac[ETH_ALEN] = { 2, 0, 0, 0, 0, 1 };
+ struct nf_flow_test_ctx *ctx;
+ unsigned int dir;
+
+ ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+ if (!ctx)
+ return -ENOMEM;
+ ctx->dev = kunit_kzalloc(test, sizeof(*ctx->dev), GFP_KERNEL);
+ if (!ctx->dev)
+ return -ENOMEM;
+ ctx->dev->ifindex = 10;
+ ctx->dev->addr_len = ETH_ALEN;
+ ctx->dev->netdev_ops = &nf_flow_test_port_ops;
+ for (dir = 0; dir < FLOW_OFFLOAD_DIR_MAX; dir++) {
+ struct flow_offload_tuple *tuple = &ctx->flow.tuplehash[dir].tuple;
+ struct nf_flow_test_path *path = &ctx->paths[dir];
+
+ path->dst.dev = ctx->dev;
+ path->dst.ops = &ops;
+ path->dst.obsolete = DST_OBSOLETE_FORCE_CHK;
+ path->neigh.dev = ctx->dev;
+ path->neigh.nud_state = NUD_REACHABLE;
+ refcount_set(&path->neigh.refcnt, 1);
+ seqlock_init(&path->neigh.ha_lock);
+ ether_addr_copy(path->neigh.ha, mac);
+ tuple->dir = dir;
+ tuple->dst_cache = &path->dst;
+ tuple->dst_cookie = 42;
+ tuple->dst_validation_only = true;
+ tuple->l3proto = NFPROTO_IPV4;
+ tuple->xmit_type = FLOW_OFFLOAD_XMIT_DIRECT;
+ tuple->out.ifidx = ctx->dev->ifindex;
+ ether_addr_copy(tuple->out.h_dest, mac);
+ }
+ kunit_activate_static_stub(test, nf_flow_offload_neigh, nf_flow_test_neigh);
+ test->priv = ctx;
+ return 0;
+}
+
+static void nf_flow_test_cached_path(struct kunit *test)
+{
+ struct nf_flow_test_ctx *ctx = test->priv;
+ unsigned long cookie = (unsigned long)&ctx->flow.tuplehash[0].tuple;
+
+ /* Validation uses the selected dst and its cookie, without a new FIB
+ * lookup that could omit mark/DSCP/oif from the owner's routing context.
+ */
+ KUNIT_EXPECT_EQ(test, nf_flow_offload_validate(cookie, NULL), 0);
+ ctx->paths[1].dst.error = ENETUNREACH;
+ /* On-demand validation still checks a driver-only reference. */
+ KUNIT_EXPECT_TRUE(test, nf_flow_dst_check(&ctx->flow.tuplehash[1].tuple));
+ KUNIT_EXPECT_EQ(test, nf_flow_offload_validate(cookie, NULL), -ESTALE);
+ ctx->paths[1].dst.error = 0;
+ ctx->flow.tuplehash[1].tuple.dst_cookie = 41;
+ KUNIT_EXPECT_EQ(test, nf_flow_offload_validate(cookie, NULL), -ESTALE);
+ KUNIT_EXPECT_EQ(test, refcount_read(&ctx->paths[0].neigh.refcnt), 1);
+}
+
+static void nf_flow_test_neighbour_change(struct kunit *test)
+{
+ struct nf_flow_test_ctx *ctx = test->priv;
+ unsigned long cookie = (unsigned long)&ctx->flow.tuplehash[1].tuple;
+
+ ctx->paths[0].neigh.ha[5]++;
+ KUNIT_EXPECT_EQ(test, nf_flow_offload_validate(cookie, NULL), -ESTALE);
+ ctx->paths[0].neigh.ha[5]--;
+ ctx->paths[0].neigh.nud_state = NUD_FAILED;
+ KUNIT_EXPECT_EQ(test, nf_flow_offload_validate(cookie, NULL), -ESTALE);
+ /* A missing entry, such as one which was garbage collected, does not
+ * prove the path stale, and validation must not create one.
+ */
+ ctx->paths[0].neigh.nud_state = NUD_REACHABLE;
+ ctx->paths[0].no_neigh = true;
+ KUNIT_EXPECT_EQ(test, nf_flow_offload_validate(cookie, NULL), -EOPNOTSUPP);
+ KUNIT_EXPECT_EQ(test, refcount_read(&ctx->paths[0].neigh.refcnt), 1);
+}
+
+static void nf_flow_test_unsupported_context(struct kunit *test)
+{
+ struct nf_flow_test_ctx *ctx = test->priv;
+ unsigned long cookie = (unsigned long)&ctx->flow.tuplehash[0].tuple;
+ struct dst_entry *dst = ctx->flow.tuplehash[0].tuple.dst_cache;
+
+ ctx->flow.tuplehash[0].tuple.dst_cache = NULL;
+ KUNIT_EXPECT_EQ(test, nf_flow_offload_validate(cookie, NULL), -EOPNOTSUPP);
+ ctx->flow.tuplehash[0].tuple.dst_cache = dst;
+ ctx->flow.tuplehash[0].tuple.out.ifidx++;
+ KUNIT_EXPECT_EQ(test, nf_flow_offload_validate(cookie, NULL), -EOPNOTSUPP);
+ ctx->flow.tuplehash[0].tuple.out.ifidx--;
+ ctx->flow.tuplehash[0].tuple.l3proto = NFPROTO_IPV6;
+ KUNIT_EXPECT_EQ(test, nf_flow_offload_validate(cookie, NULL), -EOPNOTSUPP);
+}
+
+static void nf_flow_test_bridge_validation(struct kunit *test)
+{
+ struct nf_flow_test_ctx *ctx = test->priv;
+ struct flow_offload_tuple *tuple = &ctx->flow.tuplehash[0].tuple;
+ unsigned long cookie = (unsigned long)tuple;
+ const u8 mac[ETH_ALEN] = { 2, 0, 0, 0, 0, 2 };
+
+ ctx->bridge.priv_flags = IFF_EBRIDGE;
+ ctx->bridge.ifindex = 20;
+ ctx->bridge.addr_len = ETH_ALEN;
+ ctx->bridge.netdev_ops = &nf_flow_test_bridge_ops;
+ ctx->bridge_port = ctx->dev;
+ /* dev_addr is normally initialized by alloc_netdev(). */
+ ctx->bridge.dev_addr = mac;
+ ether_addr_copy(tuple->out.h_source, mac);
+ ctx->paths[0].dst.dev = &ctx->bridge;
+ KUNIT_EXPECT_EQ(test, nf_flow_offload_validate(cookie, NULL), 0);
+
+ /* Moving to another wired or wireless port invalidates the old cache. */
+ tuple->out.ifidx++;
+ KUNIT_EXPECT_EQ(test, nf_flow_offload_validate(cookie, NULL), -ESTALE);
+ tuple->out.ifidx--;
+ ctx->bridge_port = NULL;
+ KUNIT_EXPECT_EQ(test, nf_flow_offload_validate(cookie, NULL), -ESTALE);
+ ctx->bridge_port = ctx->dev;
+ ctx->bridge_vlan = true;
+ KUNIT_EXPECT_EQ(test, nf_flow_offload_validate(cookie, NULL), -EOPNOTSUPP);
+ ctx->bridge_vlan = false;
+ tuple->out.h_source[5]++;
+ KUNIT_EXPECT_EQ(test, nf_flow_offload_validate(cookie, NULL), -ESTALE);
+}
+
+static void nf_flow_test_ingress_mac(struct kunit *test)
+{
+ struct nf_flow_test_ctx *ctx = test->priv;
+ struct flow_offload_tuple *tuple = &ctx->flow.tuplehash[0].tuple;
+ u8 mac[ETH_ALEN];
+ unsigned long cookie = (unsigned long)tuple;
+
+ /* The ingress peer comes from the reverse cached route, not the last
+ * observed packet, which might have a different Ethernet source.
+ */
+ ctx->paths[1].neigh.ha[5] = 7;
+ ctx->flow.tuplehash[1].tuple.out.h_dest[5] = 7;
+ KUNIT_ASSERT_EQ(test, nf_flow_offload_validate(cookie, mac), 0);
+ KUNIT_EXPECT_EQ(test, mac[5], 7);
+ cookie = (unsigned long)&ctx->flow.tuplehash[1].tuple;
+ KUNIT_ASSERT_EQ(test, nf_flow_offload_validate(cookie, mac), 0);
+ KUNIT_EXPECT_EQ(test, mac[5], 1);
+ tuple->xmit_type = FLOW_OFFLOAD_XMIT_NEIGH;
+ KUNIT_EXPECT_EQ(test, nf_flow_offload_validate(cookie, mac), -EOPNOTSUPP);
+}
+
+static void nf_flow_test_bridge_generation(struct kunit *test)
+{
+ struct nf_flow_test_ctx *ctx = test->priv;
+ unsigned long cookie = (unsigned long)&ctx->flow.tuplehash[0].tuple;
+ struct nf_conn *ct;
+ struct net *net;
+
+ net = kunit_kzalloc(test, sizeof(*net), GFP_KERNEL);
+ ct = kunit_kzalloc(test, sizeof(*ct), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, net);
+ KUNIT_ASSERT_NOT_NULL(test, ct);
+ write_pnet(&ct->ct_net, net);
+ ctx->flow.ct = ct;
+ __set_bit(NF_FLOW_BRIDGE, &ctx->flow.flags);
+ KUNIT_EXPECT_EQ(test, nf_flow_offload_validate(cookie, NULL), 0);
+ atomic_inc(&net->ft.bridge_genid);
+ /* A pending hardware add must not use the pre-move software owner. */
+ KUNIT_EXPECT_EQ(test, nf_flow_offload_validate(cookie, NULL), -ESTALE);
+}
+
+static struct kunit_case nf_flow_path_cases[] = {
+ KUNIT_CASE(nf_flow_test_cached_path),
+ KUNIT_CASE(nf_flow_test_neighbour_change),
+ KUNIT_CASE(nf_flow_test_unsupported_context),
+ KUNIT_CASE(nf_flow_test_bridge_validation),
+ KUNIT_CASE(nf_flow_test_ingress_mac),
+ KUNIT_CASE(nf_flow_test_bridge_generation),
+ {}
+};
+
+static struct kunit_suite nf_flow_path_suite = {
+ .name = "nf-flow-path",
+ .init = nf_flow_test_init,
+ .test_cases = nf_flow_path_cases,
+};
+
+kunit_test_suite(nf_flow_path_suite);
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH net-next v2 6/6] docs: net: document Northstar FA setup and NAT qualification
2026-12-01 18:22 [PATCH net-next v2 0/6] Add Northstar FA TCP/UDP NAT offload with a bridged LAN Florian Fainelli
` (4 preceding siblings ...)
2026-12-01 18:22 ` [PATCH net-next v2 5/6] net: add KUnit coverage for FA and flowtable path validation Florian Fainelli
@ 2026-12-01 18:22 ` Florian Fainelli
5 siblings, 0 replies; 7+ messages in thread
From: Florian Fainelli @ 2026-12-01 18:22 UTC (permalink / raw)
To: netdev
Cc: Florian Fainelli, corbet, davem, edumazet, horms, kuba, linux-doc,
linux-kernel, pabeni, rdunlap, skhan, willemb
Describe the supported topology, GMAC2 data and GMAC0 exception wiring,
configuration, counters and software fallback. Provide classic/software/
hardware NAT controls and same-live-tuple TTL, MTU and ingress-policy
checks. Cover live route/neighbour changes, reachability probes and
read/insertion/deletion completion injection with reprobe recovery.
Explain that every notified IPv4 route, rule or nexthop event retires
all hardware owners, including unrelated and local-table updates.
Document the resulting offload-residency cost and distinguish actual
cached-path validation from the driver's incomplete FIB eligibility
probe. State the limits of FIB notifications and of the GMAC3-hosted
registers.
Describe a standalone WAN opposite a four-port LAN bridge with software
members, physical-only hardware flowtable devices, source-MAC guards,
FDB invalidation, software-to-wired movement and mixed NAT/L2 tests.
Signed-off-by: Florian Fainelli <florian.fainelli@broadcom.com>
---
Changes in v2:
- Document complete ingress guards, auxiliary VLAN membership, failed-
MAC-change recovery and exact TCP control retirement.
- Clarify GMAC3 register accessibility and bootloader dependencies, the
supported compatibles, and unnotified DSCP-specific route aliases which
must not be used with FA traffic.
- Include both TCP and UDP in the nftables example and run all combined
KUnit suites without the earlier name filter.
- Document bridge setup, wired-only hardware flowtable devices, software-
member NAT/L2 forwarding, FDB movement and mixed soak qualification.
Remove redundant transport protocol matches from the nftables example
so strict checkpatch is clean.
.../ethernet/broadcom/bcm-ns-fa.rst | 638 ++++++++++++++++++
.../device_drivers/ethernet/index.rst | 1 +
MAINTAINERS | 1 +
3 files changed, 640 insertions(+)
create mode 100644 Documentation/networking/device_drivers/ethernet/broadcom/bcm-ns-fa.rst
diff --git a/Documentation/networking/device_drivers/ethernet/broadcom/bcm-ns-fa.rst b/Documentation/networking/device_drivers/ethernet/broadcom/bcm-ns-fa.rst
new file mode 100644
index 0000000000000..a0bbddada2ed5
--- /dev/null
+++ b/Documentation/networking/device_drivers/ethernet/broadcom/bcm-ns-fa.rst
@@ -0,0 +1,638 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+Broadcom Northstar flow accelerator
+===================================
+
+Status and scope
+----------------
+
+The driver supports BCM4708, BCM53011 and BCM53012. IPv4 TCP and UDP
+SNAT/DNAT have been validated on BCM953012ER (BCM53012), including normal
+and software-flowtable controls, external wire checks, hardware table
+pressure, software fallback, natural aging, teardown and recovery.
+
+BCM4708 and BCM53011 remain unvalidated. BCM53012 validation covers the
+limited topology below; maximum throughput has not been established.
+
+The driver accepts nftables flowtable offload through ``TC_SETUP_FT`` on
+bgmac. DSA forwards flowtable binds from its user ports to the GMAC2 conduit.
+The FA is inline on the UniMAC2 / switch IMP0 (port 8) path. GMAC0 and GMAC1
+are pass-through paths and cannot accelerate these flows.
+
+The initial supported configuration is deliberately limited:
+
+* IPv4 TCP/UDP with source NAT or destination NAT, including port translation.
+* Two untagged router interfaces in the initial network namespace: either
+ two standalone DSA ports, or a standalone WAN port and one LAN bridge.
+ The bridge may contain all four LAN switch ports and software ports such
+ as a PCIe WLAN interface. Accelerated ingress and egress must both be
+ DSA ports of the same switch, using GMAC2 as their conduit.
+* Distinct unicast MAC addresses on the LAN and WAN router interfaces.
+* MTU 1500 on both router interfaces and their physical ports, one hardware
+ flowtable, and no upper devices other than the untagged LAN bridge.
+
+IPv6, VLAN devices or VLAN-filtering bridges, PPPoE, tunnels, bonds, hairpin forwarding,
+plain routing, multipath/nexthop-object routes, and simultaneous source and
+destination NAT in one direction
+are rejected for hardware offload. They can still use software forwarding.
+Hardware admission requires a second, operational CPU connection from switch
+port 5 to GMAC0. B53 CFP redirects IPv4 exceptions through this connection,
+physically bypassing FA. This includes TCP SYN/FIN/RST, TTL 0/1, IP lengths
+above 1500, and packets that do not match the input port's router MAC. TCP
+also requires the flowtable FIN/RST exclusion mask. Boards without the
+auxiliary connection retain software forwarding for both protocols.
+
+The hardware has 1024 directional IPv4 entries, arranged in 256 four-way
+buckets, four router MAC entries, and 128 next-hop entries. Capacity exhaustion
+returns an error to the flowtable infrastructure, leaving traffic in software.
+One connection normally needs two directional entries. Next-hop entries are
+shared by destination MAC; router MACs are shared with an internal/external
+side attribute. The driver learns the tuple hash from opcode-2 miss tags,
+so it does not implement an assumed software equivalent of the hardware CRC.
+A finite RX hash cache can also cause an offload attempt to fall back to
+software under miss floods.
+
+The new ``brcm-fa`` DSA tag protocol carries the prepended FA miss descriptor.
+Its hash, bucket, and parser status fields differ from the ordinary switch
+tag. The bgmac receive hook observes the metadata for hash learning; the DSA
+tagger consumes it and selects the input port. Timestamp descriptors are not
+supported. The ordinary ``brcm`` and ``brcm-prepend`` taggers retain their
+opcode validation.
+
+Implementation notes
+--------------------
+
+Table operations use the documented indirect access sequence. DATA0 triggers
+the transfer; reads discard the initial DATA0 value and wait for the fetch
+before consuming DATA7 through DATA0. Every wait is bounded. The driver keeps
+clear-on-read disabled because clearing on a read gives unreliable hit counts.
+Instead it reads cumulative packet counts and computes wrapping deltas in
+software.
+
+The indirect read port is released after each access, including an access
+that times out. Leaving read mode armed prevents per-entry hit counters from
+updating and breaks the flowtable's activity accounting.
+
+Live packet lookups must not overlap software table transactions. On the
+tested BCM53012, that overlap can advance the indirect address more than
+once and persistently copy one flow's key into another slot. Before each
+transaction, the driver requests GMAC2 CPU flow control, waits for pause to
+assert and allows an in-flight frame to drain. It preserves automatic flow
+control and restores the previous MAC control on every exit path. The switch
+CPU port must honor pause, with matching internal link speeds at both ends.
+The FA stays in normal mode so packet and Broadcom-header processing retain
+their usual paths. Completion checks cover both the busy bit and the
+auto-incremented table address. Frequent statistics and debugfs reads also
+consume time on the internal link; include that work in performance tests.
+
+Router destination MAC validation is enabled before publishing a forwarding
+entry. With no installed entries, it is disabled to obtain hashes for the
+first unknown tuples. VLAN TPIDs are disabled in the FA parser so tagged
+packets cannot match an installed untagged tuple merely by sharing its inner
+five-tuple, including the TCP/UDP protocol bit. When both CPU connections
+are explicitly described, B53 CFP reserves rules for TCP SYN/FIN/RST, TTL
+0/1 and IPv4 total lengths above 1500. These redirect to port 5 with a
+classification and termination reason. Nine masked prefixes cover every
+length from 1501 through 65535. Router-MAC guards redirect packets using
+another router interface's destination MAC to port 5. All ports of the LAN
+bridge share its router MAC and routing domain. Other destination MACs
+cannot match the FA router-MAC pool; normal LAN switching and copies to
+software bridge members continue. MAC and bridge membership changes disable
+admission while updating these rules, and a MAC change which fails restores
+them. Both
+meter outcomes use the same action. Ordinary data uses port 8 only; unicast
+and multicast flooding to the auxiliary CPU is disabled. Redirected frames
+stay subject to VLAN filtering, so the auxiliary CPU port follows the VLAN
+membership of port 8. Standalone ports use basic EAP mode, with
+private VLAN membership restricting forwarding to the CPU connections.
+VLAN, EAP and STP filtering remain enabled. CFP classification is disabled
+on CPU ports. Router-domain guards cover every user port, including bridge
+members. FA cannot separate an additional active routing domain, so such a
+configuration is declined for hardware offload.
+
+Each accelerated LAN client also owns a classifier source-MAC guard. It
+redirects IPv4 packets from that MAC on any switch port other than the one
+recorded by the bridge FDB. This makes wired roaming observable even when
+the client continues an already accelerated tuple without ARP or TCP control
+traffic. The first packet reaches Linux bridge learning, which invalidates
+the old forwarding cache before a new hardware egress can be admitted.
+There are 128 shared client guards; exhaustion retains software forwarding.
+Classifier failures disable FA until reprobe. Redirected bridge frames are
+not marked as already forwarded by hardware, so Linux can forward them to
+other wired ports as well as WLAN.
+
+FA must ignore switch exception metadata to accelerate standalone data,
+which normally carries the switch flooding reason. Consequently, an inline
+SYN/FIN/RST trap through port 8 is insufficient: FA would accelerate those
+packets as well. The auxiliary GMAC0 path avoids that lookup entirely. It
+uses embedded Broadcom tags; the port 8 data path uses prepended FA metadata.
+The tagger dispatches control packets to FA before delivering them to Linux.
+The auxiliary conduit uses promiscuous mode to receive the router port MACs.
+Before disabling its CFP redirects, B53 disables hardware lookup, retires
+all entries and blocks admission. Re-enabling the path permits fresh
+connections to offload.
+
+On a valid TCP control packet, NAPI marks the matching direction as closing
+and immediately disables IPv4 lookup in the parser. Broadcom-tag relocation
+remains active so the control packet can traverse the software forwarding
+path. A worker removes both NAT directions before resuming lookups. Global
+bypass is unsuitable here because it also disables tag relocation. Control
+packets match either direction of an entry by their exact ports, including
+packets with IP options. Fragments other than the first, which carry no TCP
+header, are left to the slow path, as in the software flowtable. A direction
+installed while its peer is being retired is retired as well. RCU protects entry
+identity, and a generation check prevents an insertion learned before the
+control packet from racing past it. Closing rows retain their final packet
+delta for software accounting until the flowtable deletes their ownership.
+
+Hardware packet counters refresh flowtable last-used time and packet
+accounting. The hardware has no per-entry byte counter; reported byte totals
+therefore do not include accelerated traffic. Three-bit timestamps are not
+used for aging. Debugfs reads do not consume the driver's accounting deltas.
+
+Netdevice shutdown, address/MTU changes, new active switch ports, and upper
+interface changes flush entries. Bridge FDB updates, including software-only
+administrative moves, deletions and aging, retire all hardware owners. Bridge
+port state, security flags and VLAN changes also invalidate the cache. The
+retained bridge route is validated against its actual neighbour and current
+FDB-selected physical egress. A moved client retires the old software owner
+as well. Neighbor probes use the bridge L3 interface, rather than a member
+port. Accelerated packets do not refresh the software FDB; its normal aging
+may therefore cause retirement and relearning during a long connection.
+Every IPv4 FIB route, rule and nexthop event
+which the kernel notifies flushes all FA-owned flows, including unrelated
+prefixes and local-table entries. This is intentional: the rule does not carry
+enough routing-policy context to prove that an event cannot affect an
+installed path. The kernel only notifies the first alias of a prefix in a
+table, so a change to a route which is not first, such as a route with a
+non-zero DSCP (``tos``) next to the default one for the same prefix, is not
+seen, and neither are the route MTUs of such aliases. Do not use DSCP-specific
+routes for traffic which uses FA. Work is
+coalesced, but even unrelated routing churn can reduce offload residency.
+Redirects and next-hop MAC or validity changes also disable lookup and schedule
+entry retirement. The driver also notifies the flowtable owner to retire
+its cached software path; merely removing hardware rows would leave stale
+direct-transmit routes and destination MACs in use. Generation checks reject observations and publications
+that raced with invalidation. Before admitting a rule, the flowtable owner
+validates its actual cached routes and direct-path neighbours. Hardware
+flowtables retain validation-only route references, so this check preserves the mark,
+DSCP and output-interface context used when those paths were selected.
+These references do not enable automatic route checks for plain direct
+flows in other drivers or hardware-enabled flowtables. Existing direct
+tunnel, neighbour and XFRM route checks keep their previous behaviour.
+A proven stale cached route or neighbour retires the software owner too.
+The driver's separate FIB lookup is only a hardware eligibility probe:
+it lacks the complete policy context, so any mismatch declines hardware
+offload without tearing down a valid software owner. Unsupported route types,
+such as multipath, nexthop objects and lightweight tunnels, decline hardware
+admission without tearing down valid software flowtable forwarding.
+Valid neighbour aging transitions do not unnecessarily flush entries.
+Hardware counter activity drives Linux
+neighbour reachability probes, so an accelerated connection cannot leave a
+stale neighbour untested indefinitely.
+
+The flowtable classifier provides no per-route MTU. The driver therefore
+keeps all flows in software while any IPv4 route in the initial network
+namespace has an explicit MTU below 1500. The initial FIB notifier replay
+seeds a hash of low-MTU table/prefix pairs; route replacements and deletions
+update it incrementally without further FIB dumps. Ordinary routes use no
+tracking memory. If allocation fails or more than 1024 low-MTU prefixes
+need tracking, hardware admission remains disabled until reprobe; route
+updates and software forwarding continue. This conservative restriction also
+applies to unrelated routes. The CFP length trap handles packets exceeding
+the admitted interface MTU, including packets of an already installed tuple.
+
+A table timeout disables parser lookup
+and prevents further hardware insertion until reprobe. Tag relocation remains
+active for software fallback. Reconfiguration can
+leave an existing conntrack entry temporarily labelled HW_OFFLOAD after its
+hardware entries have been flushed. Use fresh connections after topology or
+ruleset changes; the label alone is never proof of forwarding by hardware.
+
+Device tree and kernel configuration
+------------------------------------
+
+Enable at least::
+
+ CONFIG_BGMAC_BCMA=y
+ CONFIG_BCM_NS_FA=y
+ CONFIG_NET_DSA=y
+ CONFIG_B53=y
+ CONFIG_B53_SRAB_DRIVER=y
+ CONFIG_NET_DSA_TAG_BRCM_FA=y
+ CONFIG_NF_CONNTRACK=y
+ CONFIG_NF_TABLES=y
+ CONFIG_NF_FLOW_TABLE=y
+ CONFIG_NF_FLOW_TABLE_INET=y
+ CONFIG_NFT_FLOW_OFFLOAD=y
+ CONFIG_NFT_NAT=y
+ CONFIG_NF_NAT=y
+ CONFIG_DEBUG_FS=y
+
+Modules are supported too. Use the board's existing ARM configuration and
+retain its PHY, switch, filesystem, and boot-device options.
+
+For a BCM53012 board, add the following node under the root address space
+(adjust the compatible to ``brcm,bcm53011-fa`` for BCM53011)::
+
+ / {
+ fa: flow-accelerator@18027c00 {
+ compatible = "brcm,bcm53012-fa";
+ reg = <0x18027c00 0xc0>;
+ };
+ };
+
+ &gmac2 {
+ brcm,flow-accelerator = <&fa>;
+ status = "okay";
+ };
+
+Both tested user ports must use switch port 8 and GMAC2. In the existing B53
+switch node, port 8 must have ``ethernet = <&gmac2>``. Also enable
+switch port 5 with ``ethernet = <&gmac0>``, ``phy-mode = "internal"`` and a
+1 Gbps full-duplex fixed link. Enable GMAC0 and bring its network interface
+up before testing either protocol; retain port 8 as the preferred conduit for
+user ports.
+Remove any other conflicting CPU assignments for this test. B53 selects ``brcm-fa``
+for a BCM5301x switch when the ethernet node of its port 8 has the phandle to
+an enabled accelerator node compatible with ``brcm,bcm4708-fa``,
+``brcm,bcm53011-fa`` or ``brcm,bcm53012-fa``. These are the compatibles which
+the backend accepts, so both agree on whether FA metadata is used. Another
+compatible on an enabled node fails the probe of GMAC2. The binding lists
+the supported SoCs: BCM4708, BCM53011 and BCM53012. Do not set
+``dsa-tag-protocol``: B53 does not implement the runtime protocol-change
+operation. No existing board is enabled automatically by this series.
+
+The FA registers are in the 4 KiB BCMA window of the GMAC3 core, outside the
+0x800 bytes of its own registers. They are managed by the GMAC2 consumer; they
+do not need a separate platform driver or character device. The driver neither
+enables nor resets the GMAC3 core. bgmac only probes GMAC3 when it has a MAC
+address, which would reset the core and the accelerator state: do not
+give GMAC3 one. Whether the registers can be accessed depends on the state in
+which the bootloader leaves that core, which has not been established for all
+boards. Leave the accelerator disabled or omit the GMAC2 phandle to retain the
+existing board configuration.
+
+Standalone WAN and bridged LAN
+------------------------------
+
+A common router configuration keeps the WAN separate and assigns the LAN
+IPv4 address to a bridge. For example, with ``lan1`` through ``lan4`` as
+switch ports, ``wan`` as the standalone WAN, and ``wlan0`` as the WLAN::
+
+ ip link add br-lan type bridge vlan_filtering 0
+ ip link set br-lan address 02:00:00:00:01:01
+ for dev in lan1 lan2 lan3 lan4 wlan0; do
+ ip link set "$dev" master br-lan
+ ip link set "$dev" up
+ done
+ ip address add 192.0.2.1/24 dev br-lan
+ ip link set br-lan up
+ ip link set wan address 02:00:00:00:03:01
+ ip address add 198.51.100.1/24 dev wan
+ ip link set wan up
+
+Remove the LAN IPv4 address from individual member ports before assigning
+it to the bridge. The LAN and WAN router MACs must be distinct. Keep the
+GMAC0 exception connection operational as for the standalone-port setup.
+Use bridge ports in the flowtable and the bridge name in routed firewall
+and NAT rules::
+
+ flowtable ft {
+ hook ingress priority 0;
+ devices = { lan1, lan2, lan3, lan4, wan };
+ flags offload;
+ counter;
+ }
+
+The hardware table can bind only devices which support flowtable offload.
+Keep WLAN devices out of this table: their connections use normal software
+forwarding, including the bridge FDB lookup. For the software-flowtable
+control test, omit ``flags offload`` and add ``wlan0`` to the device list.
+
+For the later test rules, replace LAN ``iifname`` and ``oifname`` matches
+with ``br-lan`` and retain ``wan`` for the WAN. Wired LAN-to-WAN connections
+can use FA in both directions. Connections using a WLAN or another software
+bridge port retain software forwarding; adding that member does not disable
+wired offload. VLAN filtering, stacked bridges, locked or isolated accelerated
+ports, and a second active LAN routing domain are not supported by this
+initial bridge implementation.
+
+Bridge FDB changes invalidate cached bridge paths in software as well as
+the FA entries. In particular, a software-only owner must not keep sending
+replies to a WLAN member after its client moves to a wired port. The next
+packet takes the normal forwarding path until the retired owner has been
+removed, then a fresh cache can resolve the new physical bridge port.
+
+Qualify both protocols and NAT initiation directions using classic,
+software-flowtable and hardware modes. While counters advance, exercise:
+
+* All four wired LAN members, including ordinary LAN-to-LAN switching,
+ unknown-unicast flooding and broadcast delivery to a software member.
+* The bridge MAC differing from every member MAC, and a failed MAC change.
+* Wired client movement while reusing a live tuple without ARP or TCP
+ SYN/FIN/RST, and movement between a wired port and WLAN.
+* FDB replacement, deletion and aging, including ``master dynamic`` entries
+ intentionally omitted from switchdev notifications.
+* Member removal/addition, STP blocking, auxiliary-link loss, and a VLAN
+ filtering toggle. Unsupported configurations must keep working in software.
+* Concurrent WLAN software forwarding and wired FA traffic, checking
+ duplicate delivery, wire NAT/checksum/TTL correctness and raw FA keys.
+* Client-guard and NAT-table exhaustion, teardown, recovery, and a mixed
+ TCP/UDP soak with bridge FDB aging left enabled.
+
+A veth bridge member can test software-member forwarding and movement in
+an automated lab. It does not qualify a real WLAN driver or association
+and roaming behavior; repeat those cases with the intended PCIe radio.
+
+Three-host hardware test
+------------------------
+
+Before any traffic test, read the accelerator control registers at 0x18027c00,
+for example with ``devmem``, after a cold boot, both with GMAC3 enabled and
+disabled in the device tree. A bus error or a timeout during probe means that
+the registers are not accessible in the state in which the bootloader leaves
+that core, and the board cannot use the accelerator.
+
+Use a dedicated test router with serial access and two external Linux hosts.
+Traffic generated on the router itself does not exercise the inline receive
+path. Keep all other switch user ports administratively down. Replace the
+interface names below with those on the board::
+
+ LAN host BCM5301X router WAN host
+ 192.0.2.2/24 -- lan1 192.0.2.1 198.51.100.1 wan -- 198.51.100.2/24
+ \ /
+ GMAC2 / FA
+
+Assign distinct router MACs before bringing up the interfaces::
+
+ ip link set lan1 down
+ ip link set wan down
+ ip link set lan1 address 02:00:00:00:01:01
+ ip link set wan address 02:00:00:00:02:01
+ ip addr add 192.0.2.1/24 dev lan1
+ ip addr add 198.51.100.1/24 dev wan
+ ip link set lan1 up
+ ip link set wan up
+ sysctl -w net.ipv4.ip_forward=1
+
+On the LAN host, assign 192.0.2.2/24 and a default route via 192.0.2.1.
+On the WAN host, assign 198.51.100.2/24 and a default route via 198.51.100.1.
+Do not put the router ports in a bridge or create VLAN uppers. Ensure that
+other firewall rules on this lab router permit the test packets.
+
+Mount debugfs if necessary and locate the FA status file::
+
+ mount -t debugfs none /sys/kernel/debug
+ find /sys/kernel/debug -name fa -type f
+ cat /sys/kernel/debug/<GMAC2-device-name>/fa
+
+The file contains ``failed``, global ``hits`` and ``misses``, error bits,
+and a row for every programmed directional entry. Each row identifies its
+slot, cookie, input/output interfaces, and raw hardware packet count. Save
+snapshots with timestamps before, during, and after every test.
+
+The adjacent ``fa_table`` file shows each software-owned tuple and the eight
+hardware words in DATA7 through DATA0 order. It uses the same lock and
+bounded indirect accesses as statistics collection. Save it when an entry
+stops counting to compare the hardware key against software ownership.
+
+Install a uniquely named test table; do not flush unrelated tables::
+
+ table inet fa_test {
+ flowtable ft {
+ hook ingress priority 0;
+ devices = { lan1, wan };
+ flags offload;
+ counter;
+ }
+ chain forward {
+ type filter hook forward priority 0; policy drop;
+ meta l4proto { tcp, udp } ct state established flow add @ft
+ ct state established,related counter accept
+ iifname "lan1" oifname "wan" meta l4proto { tcp, udp } th dport 5001 counter accept
+ iifname "wan" oifname "lan1" meta l4proto { tcp, udp } th dport 5001 ct status dnat counter accept
+ }
+ chain prerouting {
+ type nat hook prerouting priority dstnat; policy accept;
+ iifname "wan" udp dport 15001 dnat ip to 192.0.2.2:5001
+ iifname "wan" tcp dport 15001 dnat ip to 192.0.2.2:5001
+ }
+ chain postrouting {
+ type nat hook postrouting priority srcnat; policy accept;
+ iifname "lan1" oifname "wan" meta l4proto udp ip saddr 192.0.2.2 snat ip to 198.51.100.1:45000
+ iifname "lan1" oifname "wan" meta l4proto tcp ip saddr 192.0.2.2 snat ip to 198.51.100.1:45000
+ }
+ }
+
+Save this as a ruleset and load it with ``nft -f <file>``. For the two control
+runs described below, edit only this test table. Delete it between phases
+with ``nft delete table inet fa_test``. Delete matching conntrack entries
+between phases, for example::
+
+ conntrack -D -p udp --orig-src 192.0.2.2 --orig-dst 198.51.100.2 --dport 5001
+
+For SNAT, run a UDP or TCP echo server on the WAN host at
+198.51.100.2:5001. Run a client on the LAN host, bind its source to
+192.0.2.2:40000, and connect to that server. Verify the server observes
+the translated peer 198.51.100.1:45000. Include a sequence number in each
+payload and check every returned payload for loss or corruption.
+
+Run three phases with a fresh connection each time:
+
+1. **Normal forwarding:** omit the ``flow add @ft`` rule. Forward-chain
+ counters should grow throughout the test. FA entry counts and hit counts
+ should not grow for this traffic.
+2. **Software flowtable:** restore ``flow add @ft`` and omit ``flags offload``.
+ Expect OFFLOAD and no hardware entries/hit growth for this flow. Reduced
+ forward-chain counters in this phase demonstrate why those counters alone
+ cannot prove hardware offload.
+3. **Hardware flowtable:** restore ``flags offload``. Wait a few seconds for
+ asynchronous rule installation, then compare multiple debugfs snapshots.
+
+The hardware phase passes only when all of the following hold:
+
+* The WAN server sees the peer as 198.51.100.1:45000, proving address and
+ source-port translation. The client receives the original payloads.
+* ``conntrack -L -p udp -o extended`` identifies the expected original and
+ reply tuples and reports HW_OFFLOAD. OFFLOAD alone is insufficient.
+* There are two hardware entry rows for the connection and **both** packet
+ counters advance while the echo stream runs. The netfilter core can mark
+ HW_OFFLOAD when only one direction is accepted, so check each direction.
+* The global FA hit count advances with the transfer, ``failed`` remains 0,
+ and hardware error bits do not increase.
+* Captures on the external hosts show the expected rewritten IPv4/TCP or
+ IPv4/UDP tuples, valid checksums, unchanged payloads, and exactly one TTL
+ decrement.
+* Router CPU load and GMAC DMA packet growth are substantially below the
+ software phase at the same offered packet rate. A router-side capture
+ should see initial misses but few steady-state packets from this stream.
+ Compare external captures; packet-capture placement and GRO can affect
+ counts on the router.
+
+Use ``tcpdump -ni <host-interface> -vv -w <capture.pcap> udp`` on both external
+hosts. If host checksum offload obscures validation, temporarily disable the
+relevant offloads on the test hosts and restore them afterward. Capture from
+the receiving host or a wire tap when checking checksums.
+
+For DNAT, stop the WAN server and run the echo server on the LAN host at
+192.0.2.2:5001. Run the client on the WAN host, bind it to 198.51.100.2:40001,
+and connect it to 198.51.100.1:15001. Verify destination IP/port translation
+on the LAN capture and the source IP/port restoration on the WAN reply.
+Repeat all three phases and require hit growth in both directions.
+
+Stress, teardown, and negative tests
+------------------------------------
+
+* Change SNAT to a port range for parallel sessions. Exercise multiple
+ clients and next hops, hot buckets, more than 128 next-hop MACs, and over
+ 512 bidirectional sessions. Failures to allocate must fall back to software
+ without modifying a different connection's entry. Four-way collisions can
+ exhaust a bucket before the whole table is full.
+* Keep a densely occupied table active while repeatedly reading statistics.
+ Compare the software tuples with the raw entries in ``fa_table`` before
+ and after the test. Every valid hardware key must match its owning slot,
+ cookies must stay stable and every active direction's counter must grow.
+ Intact echoes and HW_OFFLOAD labels alone can conceal damaged entries
+ whose traffic has fallen back to software.
+* Stop the generator and wait longer than the configured UDP conntrack
+ timeout. Hardware rows must disappear. Run sustained traffic longer than
+ that timeout and verify that counter-based last-used refresh keeps the
+ connection alive. Check packet counter wrap handling during a long soak.
+* Delete the test flowtable during traffic. Entries must disappear and
+ forwarding must continue through the remaining firewall/NAT path. Delete
+ only the flow-add rule first if retaining the table is useful for this test.
+* Disable ``hw-tc-offload`` on the conduit with ethtool. Entries must be
+ flushed and new hardware insertions rejected. Re-enable it and use fresh
+ connections.
+* Test port down/up, a third port up, router MAC changes, MTU changes, a bridge
+ or VLAN upper being added, conduit down/up, module unload/reload, and
+ suspend/resume if supported. Require removal of stale entries and clean
+ resource reuse. Start fresh connections after each topology change.
+* Try IPv6, ICMP, GRE, PPPoE, VLAN and QinQ devices, double NAT, and
+ hairpin NAT. They must not create FA entries. Exercise tagged packets whose
+ inner tuple equals a live untagged offload; they must not hit that entry.
+* Send IPv4 fragments, IP options, bad IP checksums, runt/truncated headers,
+ and switch exception traffic. Require software handling or the normal
+ stack/switch drop behavior, never a stale or unrelated hardware hit.
+* Verify UDP zero-checksum packets, nonzero checksums, packets whose updated
+ checksum folds to zero, minimum-size frames, and maximum MTU packets.
+* Repeat SNAT, DNAT, table pressure, software fallback, aging and recovery
+ with TCP. Require the TCP bit in every owned raw table key and advancing
+ counters in both directions. Compare classic, software and hardware modes.
+* While hardware data counters advance, capture SYN/FIN/RST on the GMAC0
+ auxiliary conduit. Require embedded Broadcom tags and delivery to Linux.
+ Close with FIN and RST, reuse the same tuple, and change its NAT
+ mapping; require immediate removal of the old hardware directions, correct
+ new peers and intact framed payloads. Repeat with options and fragmented
+ control packets. An HW_OFFLOAD label alone does not qualify this test.
+ Bring GMAC0 down while TCP data flows. Require removal of its hardware
+ rows, intact software forwarding, and rejection of new TCP hardware
+ entries. Bring GMAC0 up and require fresh connections to offload again.
+* Run TCP and UDP with identical IP addresses and transport ports. Require
+ independent hashes, hardware protocol bits and correct payload delivery;
+ closing TCP must leave the UDP directions installed and advancing.
+* With a development fault-injection mechanism, force an indirect-port busy
+ timeout. Require bounded completion, ``failed 1``, no further insertions,
+ no reuse of uncertain table resources, and working software forwarding.
+ Reprobe before repeating. The driver provides no writable debugfs register
+ interface; do not poke live registers from userspace.
+
+Boundary and invalidation qualification
+---------------------------------------
+
+Run these tests with UDP and TCP, SNAT and DNAT, and classic, software
+flowtable and hardware forwarding. Install both hardware directions and
+verify their raw keys before injecting packets of that same live tuple:
+
+* TTL 0 and 1 must produce the software baseline's ICMP time-exceeded
+ response without forwarding a wrapped TTL. Ordinary traffic must decrement
+ TTL exactly once.
+* An IP length of 1500 must pass. At 1501, DF must produce ICMP fragmentation
+ needed with MTU 1500; without DF, Linux must fragment into packets no
+ larger than 1500. Check transport checksums after reassembly.
+* Inject the tuple on the opposite physical port, addressed both to the
+ original input MAC and the receiving port's MAC. Require the same ingress
+ firewall policy as the software baseline. Also test unrelated unicast,
+ broadcast and multicast destination MACs. These must not enter IPv4
+ forwarding merely because a five-tuple matches.
+
+While a hardware connection is live, replace its neighbour MAC and require
+all subsequently forwarded frames to use the new MAC. Set the neighbour to
+``NUD_FAILED`` while preventing ARP replies and require forwarding to stop.
+Restore reachability and require recovery. Force a valid neighbour to STALE
+while hardware counters advance and require a Linux ARP reachability probe.
+
+Install a blackhole host route and then a source-policy blackhole rule;
+require forwarding to stop after each netlink acknowledgment. Remove each
+change and check payload delivery and fresh hardware admission. Add an MTU
+1200 host route both during an existing connection and before a fresh one.
+Require zero hardware entries, intact small software-forwarded packets and
+ICMP fragmentation-needed with MTU 1200 for a 1280-byte DF probe. Remove
+that route and verify recovery. Keep the flowtable installed throughout;
+recreating it would conceal missing invalidation.
+
+Fault injection qualification
+-----------------------------
+
+A development kernel can enable ``CONFIG_FAULT_INJECTION``,
+``CONFIG_FAULT_INJECTION_DEBUG_FS`` and
+``CONFIG_BCM_NS_FA_FAULT_INJECTION``. The per-device ``fail_table`` directory
+uses the standard fault-injection attributes. The default probability is
+zero. Injection returns a timeout after an indirect transfer has completed,
+which deliberately makes completion uncertain to its caller. This tests
+software handling of uncertain reads and writes, including a write that may
+already have published an entry; it does not emulate a physical bus hang.
+The ordinary hardware busy/address waits remain bounded independently.
+
+For one injected failure, with the device name adjusted as needed::
+
+ D=/sys/kernel/debug/bcma0:5
+ echo 1 > $D/fail_table/times
+ echo 1 > $D/fail_table/interval
+ echo 0 > $D/fail_table/space
+ echo 100 > $D/fail_table/probability
+
+The next table access fails. To restrict injection to a table write, set
+``fail_table_write_only`` to ``Y`` before arming it, then insert a fresh
+connection or delete an installed one. Run read, insertion and deletion
+cases on separate clean probes. Verify ``failed 1`` and exactly one
+``fault_injections`` increment. Lookup must be disabled before the paused
+switch traffic resumes. Broadcom-tag relocation must remain active, and
+uncertain entries and MAC resources must not be reused until reprobe.
+
+Keep UDP and TCP SNAT/DNAT traffic active across each failure. Validate all
+eight wire directions for correct tuples, payloads, checksums and exactly
+one TTL decrement. Require a fresh connection to work in software and an
+ethtool feature toggle to leave the failed state latched. Stop the test,
+reprobe and require clean hardware admission again. Do not poke live
+registers from userspace; no writable register interface is provided.
+
+Automated validation
+--------------------
+
+Run the supplied KUnit configuration from a clean source tree::
+
+ ./tools/testing/kunit/kunit.py run --arch=x86_64 \
+ --kunitconfig=drivers/net/ethernet/broadcom/.kunitconfig \
+ --build_dir=/tmp/bcm-ns-fa-kunit
+
+The backend, switch and tagger provide KUnit suites named ``bcm-ns-fa``,
+``b53-fa-guards`` and ``brcm-fa-tag``. Enable ``CONFIG_BCM_NS_FA_KUNIT_TEST``,
+``CONFIG_NET_DSA_TAG_BRCM_FA_KUNIT_TEST`` and ``CONFIG_B53_FA_KUNIT_TEST``
+with KUnit and their parent drivers. ``CONFIG_NF_FLOW_TABLE_KUNIT_TEST``
+adds the ``nf-flow-lifetime`` and ``nf-flow-path`` suites for flowtable
+ownership and cached route validation, which the configuration also enables,
+so do not filter the suites by name. A few cases which access registers are
+skipped on UML, where MMIO is emulated.
+They cover cross-word field layout and endianness, NAT action rejection,
+miss-hash learning exclusions, opcode validation, source-port demultiplexing,
+consumption of the four-byte metadata tag, route-MTU admission and CFP UDF
+layout. The length-prefix test checks all 65536 possible IPv4 lengths.
+These tests do not simulate the FA.
+
+Build the bgmac, B53, and DSA objects with ``W=1`` for ARM, plus an x86
+COMPILE_TEST configuration; also check the FA-disabled configuration and
+built-in/module combinations. Run ``dt_binding_check`` for both new schemas,
+``checkpatch.pl``, and ``git diff --check``. The hardware matrix above remains
+mandatory even when all automated checks pass.
diff --git a/Documentation/networking/device_drivers/ethernet/index.rst b/Documentation/networking/device_drivers/ethernet/index.rst
index 403ae9c2b06a9..007e5c5c892ac 100644
--- a/Documentation/networking/device_drivers/ethernet/index.rst
+++ b/Documentation/networking/device_drivers/ethernet/index.rst
@@ -18,6 +18,7 @@ Contents:
amd/pds_vdpa
amd/pds_vfio_pci
aquantia/atlantic
+ broadcom/bcm-ns-fa
chelsio/cxgb
dlink/dl2k
davicom/dm9000
diff --git a/MAINTAINERS b/MAINTAINERS
index c05ac2258889e..7fbc78f9dd413 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -5672,6 +5672,7 @@ S: Maintained
F: Documentation/devicetree/bindings/net/brcm,amac.yaml
F: Documentation/devicetree/bindings/net/brcm,bcm4708-fa.yaml
F: Documentation/devicetree/bindings/net/brcm,bcm5301x-gmac.yaml
+F: Documentation/networking/device_drivers/ethernet/broadcom/bcm-ns-fa.rst
F: drivers/net/dsa/b53/b53_fa_test.c
F: drivers/net/ethernet/broadcom/.kunitconfig
F: drivers/net/ethernet/broadcom/bcm-ns-fa*
^ permalink raw reply related [flat|nested] 7+ messages in thread