Thread View: linux.kernel.netdev
8 messages
8 total messages
Started by Fil Dunsky via B4 Relay
Wed, 25 Nov 2026 20:50
[PATCH RFC net-next 0/7] net: cortina: CS7542 wired IPv4 hardware flowtable offload
Author: Fil Dunsky via B4 Relay
Date: Wed, 25 Nov 2026 20:50
Date: Wed, 25 Nov 2026 20:50
162 lines
20460 bytes
20460 bytes
This RFC connects the CS7542 forwarding engine to nftables hardware
flowtables on Securifi Almond+. It is separate from the CPU Ethernet
bring-up series. The main questions are the flowtable context interface,
QCA four-byte CPU framing, and conservative hardware resource lifetime.
The Ethernet driver retains DMA, MMIO, table and reset ownership. A
loadable frontend translates ordinary flowtable requests; a replaceable
algorithm operates through bounded typed table hooks. The switch format
is selected with the existing dsa-tag-protocol property before ports are
opened. Laboratory marker/observer commands and timed tag switching have
been removed from this composition.
Hardware qualification of the OpenWrt 6.18.64 precursor:
- Separate physical LAN 1 and WAN with normal firewall4 NAT and the LuCI
hardware-flow-offloading option. Server observed the router WAN address.
- UDP IP 512/1400/1500, DF set/clear, exact bidirectional data and TTL 63.
Without offload CPU captured 72/72 marked packets; with offload 4/212.
- The four CPU packets are refresh pairs near 30 and 60 seconds. Sticky HIT
bits do not prove a new lastused timestamp, so the driver conservatively
lets Linux age and relearn a flow rather than inventing activity.
- TCP exchanged 2,293,760 verified bytes over 70 seconds. TCP/UDP conntrack
showed HW_OFFLOAD at 3, 35 and 69 seconds. TCP stacks accepted the data;
this is not an independent checksum capture of every TCP segment.
- Eight connections used 16 selectors sharing one immutable result. Removing
one did not alter the seven remaining selectors or their forwarding.
These are correctness measurements at a controlled rate, not throughput
benchmarks. They were made on the precursor, not these exact Linux bytes.
Validation of this Linux composition:
- Full ARM zImage and modules build, COMPILE_TEST=n, no platform series.
- Seven real sk_buff KUnit tests passed under UML: tag/flow dissection,
VLAN and checksum metadata, clones, port dispatch and nonlinear headers.
- Current table/algorithm and guard code passed 590,845 checks under each
of GCC and Clang, with ASan/UBSan. Tests cover concurrent selectors,
sharing, both removal orders, capacity, transient failures and permanent
failure retention. This is software fault injection, not hardware proof.
Limits and review questions:
- Wired IPv4 TCP/UDP only; Wi-Fi, IPv6 and unsupported policies use CPU.
- There are 16 selectors and eight immutable results. Identical actions can
share/rebind results; incompatible results exhaust this pool until a
verified engine reset. General result reclamation is not implemented.
- HIT accounting can cause a brief CPU refresh on each Linux timeout.
No hardware packet/byte counters are fabricated.
- Ethernet/QCA/MDIO remain built in; suspend and forced module removal are
excluded. The development modules require checked detachment. Should
this replaceable interface be consolidated into the Ethernet driver?
- Is directional route MTU plus established TCP context appropriate in
flow_cls_offload, or should flowtable expose these differently?
- Is dsa-tag-protocol the right selection interface for this parser need?
- What hardware drain evidence is needed before safely replacing different
result records or clearing the sticky HIT bitmap?
Prerequisites, applied in this order on Linux 7.4-rc4:
- Global controls v4:
https://lore.kernel.org/all/20261125-cs7542-syscon-v4-0-3337bb0d3b48@gmail.com/
- Ethernet v2:
https://lore.kernel.org/all/20261125-b4-cs7542-ethernet-v2-0-d0ea3c6eb375@gmail.com/
- MDIO v2:
https://lore.kernel.org/all/20261123-b4-cs7542-mdio-v2-0-30465e831853@gmail.com/
The platform/DTS series is not needed for compilation. A bootable Almond+
configuration additionally needs that board support and qca4 on both CPU
ports. This RFC does not reroll or replace any prerequisite series.
Signed-off-by: Fil Dunsky <filipp.dunsky@gmail.com>
---
Fil Dunsky (7):
dt-bindings: net: dsa: allow the QCA four-byte tag protocol
net: dsa: add a QCA four-byte CPU tagger
netfilter: flowtable: expose route MTU and established TCP context
net: cortina: add a serialized CS7542 forwarding engine owner
net: dsa: qca8k: select the four-byte format before port activation
net: cortina: connect wired IPv4 NAT to nftables hardware flowtables
docs: networking: describe the CS7542 flowtable RFC limits
.../devicetree/bindings/net/dsa/dsa-port.yaml | 1 +
.../ethernet/cortina/cs7542-flowtable.rst | 112 +++
MAINTAINERS | 5 +
drivers/net/dsa/qca/persistent_qca4.h | 178 ++++
drivers/net/dsa/qca/persistent_qca4_mtu.h | 165 ++++
drivers/net/dsa/qca/qca8k-8xxx.c | 123 ++-
drivers/net/dsa/qca/qca8k-common.c | 42 +-
drivers/net/dsa/qca/qca8k-persistent-boot.inc | 632 +++++++++++++
drivers/net/dsa/qca/qca8k-persistent-lifecycle.inc | 394 +++++++++
drivers/net/dsa/qca/qca8k.h | 40 +
drivers/net/ethernet/cortina/Kconfig | 64 ++
drivers/net/ethernet/cortina/Makefile | 14 +-
drivers/net/ethernet/cortina/cs752x_ne.c | 120 +++
drivers/net/ethernet/cortina/cs7542_fe_live.c | 735 ++++++++++++++++
drivers/net/ethernet/cortina/cs7542_ft_live.c | 415 +++++++++
drivers/net/ethernet/cortina/cs_fe_acl_encode.c | 124 +++
drivers/net/ethernet/cortina/cs_fe_acl_guard.c | 324 +++++++
drivers/net/ethernet/cortina/cs_fe_acl_guard.h | 47 +
drivers/net/ethernet/cortina/cs_fe_backend.h | 228 +++++
.../net/ethernet/cortina/cs_fe_backend_algorithm.c | 976 +++++++++++++++++++++
.../net/ethernet/cortina/cs_fe_backend_dispatch.c | 433 +++++++++
.../net/ethernet/cortina/cs_fe_backend_module.c | 164 ++++
drivers/net/ethernet/cortina/cs_fe_encode.c | 562 ++++++++++++
drivers/net/ethernet/cortina/cs_fe_encode.h | 5 +
drivers/net/ethernet/cortina/cs_fe_fields.h | 339 +++++++
drivers/net/ethernet/cortina/cs_fe_format.h | 23 +
drivers/net/ethernet/cortina/cs_fe_hot.c | 84 ++
drivers/net/ethernet/cortina/cs_fe_hot.h | 15 +
drivers/net/ethernet/cortina/cs_fe_hot_access.c | 276 ++++++
drivers/net/ethernet/cortina/cs_fe_hot_access.h | 55 ++
drivers/net/ethernet/cortina/cs_fe_hot_codec.c | 2 +
drivers/net/ethernet/cortina/cs_fe_hot_compare.h | 47 +
drivers/net/ethernet/cortina/cs_fe_hot_internal.h | 44 +
drivers/net/ethernet/cortina/cs_fe_hot_lineage.c | 138 +++
drivers/net/ethernet/cortina/cs_fe_hot_lineage.h | 70 ++
drivers/net/ethernet/cortina/cs_fe_hot_tables.c | 920 +++++++++++++++++++
drivers/net/ethernet/cortina/cs_fe_native.inc | 668 ++++++++++++++
.../net/ethernet/cortina/cs_fe_native_activity.inc | 237 +++++
drivers/net/ethernet/cortina/cs_fe_native_mtu.h | 43 +
drivers/net/ethernet/cortina/cs_fe_native_mtu.inc | 128 +++
drivers/net/ethernet/cortina/cs_fe_native_state.h | 24 +
drivers/net/ethernet/cortina/flowtable/Makefile | 2 +
.../cortina/flowtable/codec/cs_ft_rewrite.c | 234 +++++
.../cortina/flowtable/codec/cs_ft_rewrite.h | 42 +
.../net/ethernet/cortina/flowtable/cs7542_fw4.c | 437 +++++++++
.../ethernet/cortina/flowtable/cs7542_rx_path.c | 225 +++++
.../ethernet/cortina/flowtable/cs7542_rx_path.h | 35 +
drivers/net/ethernet/cortina/flowtable/nf_native.c | 90 ++
drivers/net/ethernet/cortina/flowtable/nf_native.h | 31 +
include/linux/cs7542_fe_backend.h | 65 ++
include/linux/cs7542_fe_codec.h | 145 +++
include/linux/cs7542_fe_live.h | 58 ++
include/linux/cs7542_fe_native.h | 34 +
include/linux/cs7542_ft_live.h | 70 ++
include/linux/dsa/qca8k_persistent.h | 45 +
include/net/dsa.h | 21 +-
include/net/flow_offload.h | 5 +
net/dsa/Kconfig | 30 +
net/dsa/Makefile | 2 +
net/dsa/tag_qca4.c | 165 ++++
net/dsa/tag_qca4_kunit.h | 469 ++++++++++
net/netfilter/nf_flow_table_offload.c | 25 +-
62 files changed, 11218 insertions(+), 28 deletions(-)
---
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
change-id: 20261125-almondplus-offload-rfc-149cfb29ade9
prerequisite-message-id: <20261125-cs7542-syscon-v4-0-3337bb0d3b48@gmail.com>
prerequisite-patch-id: a9208276c628f8741a807d0e5c5945d4bf211c8a
prerequisite-patch-id: 65d46bb3caaa8518ee561f65d3f603ade396fff9
prerequisite-patch-id: a0ed5fbcfa308b16eaf265371a6697ad0aea6802
prerequisite-patch-id: bb8ffd99cb30e5a0b47881f6ba19a36329d5f4f2
prerequisite-patch-id: 7fbe9405265ef3d1ec62811415a46c0061afd7c8
prerequisite-patch-id: 01ece2c91e080d5cae6af81944159373776484ca
prerequisite-message-id: <20261125-b4-cs7542-ethernet-v2-0-d0ea3c6eb375@gmail.com>
prerequisite-patch-id: aecabdb905864eca2f0634c4d08c78194d3ceffc
prerequisite-patch-id: 14f8d8de3d9bff0276c1f0a54e3f42727d40d41a
prerequisite-patch-id: f42fb7a3c96e1ea44a7cf4495929acb9fdc47349
prerequisite-message-id: <20261123-b4-cs7542-mdio-v2-0-30465e831853@gmail.com>
prerequisite-patch-id: 3656dbe3324c7bb9ec757a4c13c990cd28ec3d2a
prerequisite-patch-id: 268165424b18f66611028ebbea04dbb43cd4a7c5
prerequisite-patch-id: 33fc64ae4aa932467903052b4f27bce0f8b5e7c7
Best regards,
--
Fil Dunsky <filipp.dunsky@gmail.com>
[PATCH RFC net-next 2/7] net: dsa: add a QCA four-byte CPU tagger
Author: Fil Dunsky via B4 Relay
Date: Wed, 25 Nov 2026 20:50
Date: Wed, 25 Nov 2026 20:50
842 lines
38764 bytes
38764 bytes
From: Fil Dunsky <filipp.dunsky@gmail.com>
Insert the 0x8100 identifier and the ordinary QCA metadata ahead of the
inner EtherType. Preserve checksum/header offsets, materialize VLAN
metadata inside the CPU tag and validate nonlinear receive headers
before dispatch.
Keep the two-byte format as the default. Add KUnit coverage for tagging,
VLAN and checksum metadata, source-port dispatch, clones and
truncated/nonlinear packets. Seven tests passed under UML.
Signed-off-by: Fil Dunsky <filipp.dunsky@gmail.com>
---
include/net/dsa.h | 21 +--
net/dsa/Kconfig | 30 +++
net/dsa/Makefile | 2 +
net/dsa/tag_qca4.c | 165 +++++++++++++++++
net/dsa/tag_qca4_kunit.h | 469 +++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 671 insertions(+), 16 deletions(-)
diff --git a/include/net/dsa.h b/include/net/dsa.h
index 5d12191b6..7d3c836c2 100644
--- a/include/net/dsa.h
+++ b/include/net/dsa.h
@@ -36,6 +36,7 @@ struct tc_action;
#define DSA_TAG_PROTO_LAN9303_VALUE 8
#define DSA_TAG_PROTO_MTK_VALUE 9
#define DSA_TAG_PROTO_QCA_VALUE 10
+#define DSA_TAG_PROTO_QCA4_VALUE 37
#define DSA_TAG_PROTO_TRAILER_VALUE 11
#define DSA_TAG_PROTO_8021Q_VALUE 12
#define DSA_TAG_PROTO_SJA1105_VALUE 13
@@ -77,6 +78,7 @@ enum dsa_tag_protocol {
DSA_TAG_PROTO_LAN9303 = DSA_TAG_PROTO_LAN9303_VALUE,
DSA_TAG_PROTO_MTK = DSA_TAG_PROTO_MTK_VALUE,
DSA_TAG_PROTO_QCA = DSA_TAG_PROTO_QCA_VALUE,
+ DSA_TAG_PROTO_QCA4 = DSA_TAG_PROTO_QCA4_VALUE,
DSA_TAG_PROTO_TRAILER = DSA_TAG_PROTO_TRAILER_VALUE,
DSA_TAG_PROTO_8021Q = DSA_TAG_PROTO_8021Q_VALUE,
DSA_TAG_PROTO_SJA1105 = DSA_TAG_PROTO_SJA1105_VALUE,
@@ -183,15 +185,12 @@ struct dsa_switch_tree {
#define dsa_lags_foreach_id(_id, _dst) \
for ((_id) = 1; (_id) <= (_dst)->lags_len; (_id)++) \
if ((_dst)->lags[(_id) - 1])
-
#define dsa_lag_foreach_port(_dp, _dst, _lag) \
list_for_each_entry((_dp), &(_dst)->ports, list) \
if (dsa_port_offloads_lag((_dp), (_lag)))
-
#define dsa_hsr_foreach_port(_dp, _ds, _hsr) \
list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr))
-
static inline struct dsa_lag *dsa_lag_by_id(struct dsa_switch_tree *dst,
unsigned int id)
{
@@ -579,47 +578,36 @@ static inline bool dsa_is_user_port(struct dsa_switch *ds, int p)
#define dsa_tree_for_each_user_port(_dp, _dst) \
list_for_each_entry((_dp), &(_dst)->ports, list) \
if (dsa_port_is_user((_dp)))
-
#define dsa_tree_for_each_user_port_continue_reverse(_dp, _dst) \
list_for_each_entry_continue_reverse((_dp), &(_dst)->ports, list) \
if (dsa_port_is_user((_dp)))
-
#define dsa_tree_for_each_cpu_port(_dp, _dst) \
list_for_each_entry((_dp), &(_dst)->ports, list) \
if (dsa_port_is_cpu((_dp)))
-
#define dsa_switch_for_each_port(_dp, _ds) \
list_for_each_entry((_dp), &(_ds)->dst->ports, list) \
if ((_dp)->ds == (_ds))
-
#define dsa_switch_for_each_port_safe(_dp, _next, _ds) \
list_for_each_entry_safe((_dp), (_next), &(_ds)->dst->ports, list) \
if ((_dp)->ds == (_ds))
-
#define dsa_switch_for_each_port_continue_reverse(_dp, _ds) \
list_for_each_entry_continue_reverse((_dp), &(_ds)->dst->ports, list) \
if ((_dp)->ds == (_ds))
-
#define dsa_switch_for_each_available_port(_dp, _ds) \
dsa_switch_for_each_port((_dp), (_ds)) \
if (!dsa_port_is_unused((_dp)))
-
#define dsa_switch_for_each_user_port(_dp, _ds) \
dsa_switch_for_each_port((_dp), (_ds)) \
if (dsa_port_is_user((_dp)))
-
#define dsa_switch_for_each_user_port_continue_reverse(_dp, _ds) \
dsa_switch_for_each_port_continue_reverse((_dp), (_ds)) \
if (dsa_port_is_user((_dp)))
-
#define dsa_switch_for_each_cpu_port(_dp, _ds) \
dsa_switch_for_each_port((_dp), (_ds)) \
if (dsa_port_is_cpu((_dp)))
-
#define dsa_switch_for_each_cpu_port_continue_reverse(_dp, _ds) \
dsa_switch_for_each_port_continue_reverse((_dp), (_ds)) \
if (dsa_port_is_cpu((_dp)))
-
static inline u32 dsa_user_ports(struct dsa_switch *ds)
{
struct dsa_port *dp;
@@ -842,7 +830,6 @@ dsa_tree_offloads_bridge_dev(struct dsa_switch_tree *dst,
#define dsa_switch_for_each_bridge_member(_dp, _ds, _bdev) \
dsa_switch_for_each_user_port(_dp, _ds) \
if (dsa_port_offloads_bridge_dev(_dp, _bdev))
-
static inline u32
dsa_bridge_ports(struct dsa_switch *ds, const struct net_device *bdev)
{
@@ -996,7 +983,6 @@ struct dsa_switch_ops {
struct phy_device *phy);
void (*port_disable)(struct dsa_switch *ds, int port);
-
/*
* Notification for MAC address changes on user ports. Drivers can
* currently only veto operations. They should not use the method to
@@ -1401,10 +1387,12 @@ static inline int dsa_switch_suspend(struct dsa_switch *ds)
{
return 0;
}
+
static inline int dsa_switch_resume(struct dsa_switch *ds)
{
return 0;
}
+
#endif /* CONFIG_PM_SLEEP */
#if IS_ENABLED(CONFIG_NET_DSA)
@@ -1414,6 +1402,7 @@ static inline bool dsa_user_dev_check(const struct net_device *dev)
{
return false;
}
+
#endif
netdev_tx_t dsa_enqueue_skb(struct sk_buff *skb, struct net_device *dev);
diff --git a/net/dsa/Kconfig b/net/dsa/Kconfig
index 4f44bf3ed..99933b270 100644
--- a/net/dsa/Kconfig
+++ b/net/dsa/Kconfig
@@ -174,6 +174,36 @@ config NET_DSA_TAG_QCA
Say Y or M if you want to enable support for tagging frames for
the Qualcomm Atheros QCA8K switches.
+config NET_DSA_TAG_QCA4
+ tristate "Tag driver for QCA four-byte tags"
+ default n
+ help
+ QCA switch CPU framing with identifier 0x8100 and two-byte metadata.
+ Does not enable hardware checksum, GSO, VLAN or NAT offload features.
+ The ordinary QCA two-byte tagger remains the default.
+ Select qca4 on the CPU ports only when the switch driver supports it.
+
+config NET_DSA_TAG_QCA4_KUNIT_TEST
+ bool "KUnit tests for QCA four-byte sk_buff operations"
+ depends on KUNIT=y && NET_DSA_TAG_QCA4=y
+ default n
+ help
+ Exercise real sk_buff allocation, copy-on-write, nonlinear headers,
+ checksum metadata and source-port dispatch for the four-byte tagger.
+ These are software tests and do not require a physical QCA switch.
+ They can run with the standard KUnit test framework.
+
+config NET_DSA_QCA8K_4B_MODE
+ bool "QCA8K four-byte boot format for CS7542 offload"
+ depends on NET_DSA_QCA8K=y && NET_DSA_TAG_QCA4=y
+ depends on !PM && CS7542_FT_LIVE_BRIDGE && MDIO_CS75XX=y
+ default n
+ help
+ Support the four-byte CPU tag requested through dsa-tag-protocol.
+ The current implementation supports the CS7542 dual-conduit topology.
+ Ethernet register access uses MDIO while the four-byte format is active.
+ Both CPU ports must request the same format before the switch starts.
+
config NET_DSA_TAG_RTL4_A
tristate "Tag driver for Realtek 4 byte protocol A tags"
help
diff --git a/net/dsa/Makefile b/net/dsa/Makefile
index 1f9cc30e9..3d5949e50 100644
--- a/net/dsa/Makefile
+++ b/net/dsa/Makefile
@@ -48,3 +48,5 @@ obj-$(CONFIG_NET_DSA_TAG_YT921X) += tag_yt921x.o
# for tracing framework to find trace.h
CFLAGS_trace.o := -I$(src)
+
+obj-$(CONFIG_NET_DSA_TAG_QCA4) += tag_qca4.o
diff --git a/net/dsa/tag_qca4.c b/net/dsa/tag_qca4.c
new file mode 100644
index 000000000..413756679
--- /dev/null
+++ b/net/dsa/tag_qca4.c
@@ -0,0 +1,165 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Private source-review prototype. No offload feature advertisement. */
+#include <linux/bitfield.h>
+#include <linux/etherdevice.h>
+#include <linux/if_vlan.h>
+#include <linux/unaligned.h>
+#include <linux/dsa/tag_qca.h>
+#include <net/dsa.h>
+#include "tag.h"
+
+#define QCA4_PRIVATE_LEN 4
+#define QCA4_PRIVATE_IDENTIFIER 0x8100
+#define QCA4_PRIVATE_NAME "qca4"
+
+/*
+ * Offsets are absolute from skb head. Pushing the outer tag leaves the real
+ * L3/L4 and CHECKSUM_PARTIAL start at their original bytes; their offsets from
+ * skb->data grow by four. Standard core/conduit features own segmentation and
+ * software checksum fallback. GSO/checksum metadata is not rejected or changed.
+ */
+static int qca4_private_insert(struct sk_buff *skb, unsigned long ports)
+{
+ u16 hdr;
+ u8 *tag;
+
+ if (!ports || ports & ~FIELD_MAX(QCA_HDR_XMIT_DP_BIT))
+ return -EINVAL;
+ if (!pskb_may_pull(skb, ETH_HLEN))
+ return -EINVAL;
+ if (skb_cow_head(skb, QCA4_PRIVATE_LEN))
+ return -ENOMEM;
+ skb_push(skb, QCA4_PRIVATE_LEN);
+ dsa_alloc_etype_header(skb, QCA4_PRIVATE_LEN);
+ tag = dsa_etype_header_pos_tx(skb);
+ hdr = FIELD_PREP(QCA_HDR_XMIT_VERSION, QCA_HDR_VERSION) |
+ QCA_HDR_XMIT_FROM_CPU | FIELD_PREP(QCA_HDR_XMIT_DP_BIT, ports);
+ /* Same metadata policy as legacy QCA: no new skb-priority mapping. */
+ put_unaligned_be16(QCA4_PRIVATE_IDENTIFIER, tag);
+ put_unaligned_be16(hdr, tag + 2);
+ skb_reset_mac_header(skb);
+ return 0;
+}
+
+static struct sk_buff *qca4_private_xmit(struct sk_buff *skb,
+ struct net_device *dev)
+{
+ /*
+ * A pending hardware VLAN tag must be inside QCA metadata, never inserted
+ * outside it later by the conduit. This standard helper also handles COW and
+ * header/checksum offsets. No HW VLAN feature is advertised by this tagger.
+ */
+
+ if (skb_vlan_tag_present(skb)) {
+ skb = __vlan_hwaccel_push_inside(skb);
+ if (!skb)
+ return NULL; /* VLAN helper already freed the original. */
+ }
+ if (qca4_private_insert(skb, dsa_xmit_port_mask(skb, dev))) {
+ kfree_skb(skb);
+ return NULL;
+ }
+
+ return skb;
+}
+
+/*
+ * Conduit eth_type_trans pulled fourteen bytes: identifier is data[-2],
+ * metadata is data[0..1], and inner EtherType is data[2..3]. Do not read a
+ * possibly nonlinear metadata/inner EtherType before the full pull check.
+ */
+static int qca4_private_parse(struct sk_buff *skb, u16 *metadata)
+{
+ unsigned int reason;
+ u8 *tag;
+ u16 hdr;
+
+ if (skb_headroom(skb) < ETH_HLEN ||
+ !pskb_may_pull(skb, QCA4_PRIVATE_LEN)) {
+ return -EINVAL;
+ }
+ if (skb_cow_head(skb, 0))
+ return -ENOMEM;
+ tag = dsa_etype_header_pos_rx(skb);
+ if (get_unaligned_be16(tag) != QCA4_PRIVATE_IDENTIFIER)
+ return -EPROTO;
+ hdr = get_unaligned_be16(tag + 2);
+ if (FIELD_GET(QCA_HDR_RECV_VERSION, hdr) != QCA_HDR_VERSION)
+ return -EPROTO;
+ reason = FIELD_GET(QCA_HDR_RECV_TYPE, hdr);
+ /*
+ * These fixed14-byte pseudo-Ethernet formats are disabled in private4 mode.
+ * Never dispatch them into legacy MDIO/MIB callbacks with the wrong layout.
+ * Other normal/trap reasons remain valid, including observed flooding0x19.
+ */
+ if (reason == QCA_HDR_RECV_TYPE_MIB ||
+ reason == QCA_HDR_RECV_TYPE_RW_REG_ACK) {
+ return -EOPNOTSUPP;
+ }
+ *metadata = hdr;
+ return 0;
+}
+
+static struct sk_buff *qca4_private_rcv(struct sk_buff *skb,
+ struct net_device *conduit)
+{
+ struct net_device *user;
+ u16 metadata;
+ int port;
+
+ if (qca4_private_parse(skb, &metadata))
+ goto drop;
+ port = FIELD_GET(QCA_HDR_RECV_SOURCE_PORT, metadata);
+ user = dsa_conduit_find_user(conduit, 0, port);
+ if (!user)
+ goto drop;
+
+ skb->dev = user;
+ skb_pull_rcsum(skb, QCA4_PRIVATE_LEN);
+ dsa_strip_etype_header(skb, QCA4_PRIVATE_LEN);
+
+ /*
+ * DSA core pushes ETH_HLEN and invokes user eth_type_trans, exposing only
+ * the real inner EtherType/VLAN after the complete four-byte tag is gone.
+ */
+ return skb;
+drop:
+ kfree_skb(skb);
+ return NULL;
+}
+
+/*
+ * Called before rcv(): do not assume the metadata or inner EtherType is
+ * linear or even present. Keep the const skb and its layout unchanged.
+ */
+static void qca4_private_flow_dissect(const struct sk_buff *skb, __be16 *proto,
+ int *offset)
+{
+ __be16 temporary;
+ const void *inner;
+
+ *offset = QCA4_PRIVATE_LEN;
+ *proto = 0;
+ inner = skb_header_pointer(skb, 2, sizeof(temporary), &temporary);
+ if (inner)
+ *proto = get_unaligned((const __be16 *)inner);
+}
+
+static const struct dsa_device_ops qca4_private_ops = {
+ .name = QCA4_PRIVATE_NAME,
+ .proto = DSA_TAG_PROTO_QCA4,
+ .xmit = qca4_private_xmit,
+ .rcv = qca4_private_rcv,
+ .flow_dissect = qca4_private_flow_dissect,
+ .needed_headroom = QCA4_PRIVATE_LEN,
+ .promisc_on_conduit = true,
+};
+
+MODULE_DESCRIPTION("Private source-review QCA four-byte tag prototype");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS_DSA_TAG_DRIVER(DSA_TAG_PROTO_QCA4, QCA4_PRIVATE_NAME);
+module_dsa_tag_driver(qca4_private_ops);
+
+#if IS_ENABLED(CONFIG_NET_DSA_TAG_QCA4_KUNIT_TEST)
+#include "tag_qca4_kunit.h"
+#endif
diff --git a/net/dsa/tag_qca4_kunit.h b/net/dsa/tag_qca4_kunit.h
new file mode 100644
index 000000000..cc520b7fb
--- /dev/null
+++ b/net/dsa/tag_qca4_kunit.h
@@ -0,0 +1,469 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Real kernel sk_buff tests; offline QEMU execution only. */
+#include <kunit/test.h>
+#include <linux/highmem.h>
+#include <linux/mm.h>
+#include <net/checksum.h>
+
+struct qca4_test_context {
+ struct dsa_switch ds;
+ struct dsa_switch_tree dst;
+ struct dsa_port cpu, user;
+ struct net_device *conduit, *netdev;
+};
+
+static int qca4_test_init(struct kunit *test)
+{
+ struct qca4_test_context *c =
+ kunit_kzalloc(test, sizeof(*c), GFP_KERNEL);
+ struct dsa_user_priv *p;
+
+ if (!c)
+ return -ENOMEM;
+ c->conduit = alloc_etherdev(0);
+ c->netdev = alloc_etherdev(sizeof(*p));
+ if (!c->conduit || !c->netdev) {
+ if (c->conduit)
+ free_netdev(c->conduit);
+ if (c->netdev)
+ free_netdev(c->netdev);
+ return -ENOMEM;
+ }
+ INIT_LIST_HEAD(&c->dst.ports);
+ c->ds.index = 0;
+ c->ds.dst = &c->dst;
+ c->cpu.ds = &c->ds;
+ c->cpu.dst = &c->dst;
+ c->cpu.type = DSA_PORT_TYPE_CPU;
+ c->cpu.index = 0;
+ c->cpu.conduit = c->conduit;
+ c->cpu.rcv = qca4_private_rcv;
+ c->cpu.tag_ops = &qca4_private_ops;
+ c->conduit->dsa_ptr = &c->cpu;
+ c->user.ds = &c->ds;
+ c->user.dst = &c->dst;
+ c->user.type = DSA_PORT_TYPE_USER;
+ c->user.index = 5;
+ c->user.user = c->netdev;
+ c->user.cpu_dp = &c->cpu;
+ list_add_tail(&c->cpu.list, &c->dst.ports);
+ list_add_tail(&c->user.list, &c->dst.ports);
+ p = netdev_priv(c->netdev);
+ p->dp = &c->user;
+ test->priv = c;
+ return 0;
+}
+
+static void qca4_test_exit(struct kunit *test)
+{
+ struct qca4_test_context *c = test->priv;
+
+ free_netdev(c->netdev);
+ free_netdev(c->conduit);
+}
+
+static struct sk_buff *qca4_test_frame(unsigned int headroom,
+ unsigned int vlans)
+{
+ struct sk_buff *skb = alloc_skb(headroom + 128, GFP_KERNEL);
+ unsigned int len = 96 + 4 * vlans, network = ETH_HLEN + 4 * vlans;
+ u8 *data;
+
+ if (!skb)
+ return NULL;
+ skb_reserve(skb, headroom);
+ data = skb_put_zero(skb, len);
+ eth_broadcast_addr(data);
+ data[6] = 2;
+ data[11] = 1;
+ put_unaligned_be16(vlans == 2 ? ETH_P_8021AD :
+ vlans == 1 ? ETH_P_8021Q :
+ ETH_P_IP,
+ data + 12);
+ if (vlans) {
+ put_unaligned_be16(0xa123, data + 14);
+ put_unaligned_be16(vlans == 2 ? ETH_P_8021Q : ETH_P_IP,
+ data + 16);
+ }
+ if (vlans == 2) {
+ put_unaligned_be16(0x6000, data + 18); /* Priority tag, VID0. */
+ put_unaligned_be16(ETH_P_IP, data + 20);
+ }
+ data[network] = 0x45;
+ data[network + 9] = IPPROTO_TCP;
+ skb_reset_mac_header(skb);
+ skb_set_network_header(skb, network);
+ skb_set_transport_header(skb, network + 20);
+ skb->protocol = htons(vlans == 2 ? ETH_P_8021AD :
+ vlans == 1 ? ETH_P_8021Q :
+ ETH_P_IP);
+ return skb;
+}
+
+static void qca4_test_tx_metadata(struct kunit *test)
+{
+ struct qca4_test_context *c = test->priv;
+ unsigned int vlans, n;
+ static const unsigned int reserves[] = { 0, 2, 16, 32 };
+
+ for (vlans = 0; vlans <= 2; vlans++) {
+ for (n = 0; n < ARRAY_SIZE(reserves); n++) {
+ struct sk_buff *skb = qca4_test_frame(reserves[n],
+ vlans),
+ *original;
+ unsigned int net, trans, start, len;
+ u8 bytes[128];
+
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ skb->ip_summed = CHECKSUM_PARTIAL;
+ skb->csum_start = skb_transport_header(skb) - skb->head;
+ skb->csum_offset = 16;
+ skb->priority = 7;
+ skb_shinfo(skb)->gso_size = 1460;
+ skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
+ net = skb_network_offset(skb);
+ trans = skb_transport_offset(skb);
+ start = skb_checksum_start_offset(skb);
+ len = skb->len;
+ memcpy(bytes, skb->data, len);
+ original = skb_clone(skb, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, original);
+ skb = qca4_private_xmit(skb, c->netdev);
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ KUNIT_EXPECT_EQ(test, skb->len, len + 4);
+ KUNIT_EXPECT_EQ(test, skb_network_offset(skb), net + 4);
+ KUNIT_EXPECT_EQ(test, skb_transport_offset(skb),
+ trans + 4);
+ KUNIT_EXPECT_EQ(test, skb_checksum_start_offset(skb),
+ start + 4);
+ KUNIT_EXPECT_EQ(test, skb->csum_offset, (u16)16);
+ KUNIT_EXPECT_EQ(test, (unsigned int)skb->ip_summed,
+ (unsigned int)CHECKSUM_PARTIAL);
+ KUNIT_EXPECT_EQ(test, skb->priority, 7U);
+ KUNIT_EXPECT_EQ(test, skb_shinfo(skb)->gso_size,
+ (u16)1460);
+ KUNIT_EXPECT_EQ(test, skb_shinfo(skb)->gso_type,
+ SKB_GSO_TCPV4);
+ KUNIT_EXPECT_PTR_EQ(test, skb_mac_header(skb),
+ skb->data);
+ KUNIT_EXPECT_MEMEQ(test, skb->data, bytes, 12);
+ KUNIT_EXPECT_MEMEQ(test, skb->data + 16, bytes + 12,
+ len - 12);
+ KUNIT_EXPECT_MEMEQ(test, original->data, bytes, len);
+ KUNIT_EXPECT_EQ(test,
+ get_unaligned_be16(skb->data + 12),
+ 0x8100);
+ KUNIT_EXPECT_EQ(test,
+ get_unaligned_be16(skb->data + 14),
+ 0x80a0);
+ kfree_skb(original);
+ kfree_skb(skb);
+ }
+ }
+}
+
+static void qca4_test_vlan_metadata(struct kunit *test)
+{
+ struct qca4_test_context *c = test->priv;
+ struct sk_buff *skb = qca4_test_frame(0, 0);
+ unsigned int net, trans, start;
+
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ skb->ip_summed = CHECKSUM_PARTIAL;
+ skb->csum_start = skb_transport_header(skb) - skb->head;
+ skb->csum_offset = 16;
+ skb_shinfo(skb)->gso_size = 1460;
+ skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
+ net = skb_network_offset(skb);
+ trans = skb_transport_offset(skb);
+ start = skb_checksum_start_offset(skb);
+ __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
+ 0xe000); /* PCP7, VID0 */
+ skb = qca4_private_xmit(skb, c->netdev);
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ KUNIT_EXPECT_FALSE(test, skb_vlan_tag_present(skb));
+ KUNIT_EXPECT_EQ(test, skb_network_offset(skb), net + 8);
+ KUNIT_EXPECT_EQ(test, skb_transport_offset(skb), trans + 8);
+ KUNIT_EXPECT_EQ(test, skb_checksum_start_offset(skb), start + 8);
+ KUNIT_EXPECT_EQ(test, (unsigned int)skb->ip_summed,
+ (unsigned int)CHECKSUM_PARTIAL);
+ KUNIT_EXPECT_EQ(test, skb->csum_offset, (u16)16);
+ KUNIT_EXPECT_EQ(test, skb_shinfo(skb)->gso_size, (u16)1460);
+ KUNIT_EXPECT_EQ(test, skb_shinfo(skb)->gso_type, SKB_GSO_TCPV4);
+ KUNIT_EXPECT_EQ(test, get_unaligned_be16(skb->data + 12), 0x8100);
+ KUNIT_EXPECT_EQ(test, get_unaligned_be16(skb->data + 14), 0x80a0);
+ KUNIT_EXPECT_EQ(test, get_unaligned_be16(skb->data + 16), ETH_P_8021Q);
+ KUNIT_EXPECT_EQ(test, get_unaligned_be16(skb->data + 18), 0xe000);
+ KUNIT_EXPECT_EQ(test, get_unaligned_be16(skb->data + 20), ETH_P_IP);
+ kfree_skb(skb);
+}
+
+static struct sk_buff *qca4_test_fragment(struct sk_buff *old,
+ unsigned int linear)
+{
+ struct sk_buff *skb = alloc_skb(32 + linear, GFP_KERNEL);
+ struct page *page;
+ void *mapped;
+ unsigned int bytes = old->len - linear;
+
+ if (!skb)
+ return NULL;
+ page = alloc_page(GFP_KERNEL);
+ if (!page) {
+ kfree_skb(skb);
+ return NULL;
+ }
+ skb_reserve(skb, 16);
+ skb_put_data(skb, old->data, linear);
+ mapped = kmap_local_page(page);
+ memcpy(mapped, old->data + linear, bytes);
+ kunmap_local(mapped);
+ skb_add_rx_frag(skb, 0, page, 0, bytes, PAGE_SIZE);
+ skb_reset_mac_header(skb);
+ return skb;
+}
+
+static void qca4_test_rx_dispatch(struct kunit *test)
+{
+ struct qca4_test_context *c = test->priv;
+ unsigned int vlans, reason, priority, port;
+
+ for (vlans = 0; vlans <= 2; vlans++) {
+ for (reason = 0; reason < 32; reason++) {
+ for (priority = 0; priority < 8; priority++) {
+ for (port = 0; port < 8; port++) {
+ struct sk_buff *skb = qca4_test_frame(16, vlans),
+ *result;
+ u16 hdr =
+ FIELD_PREP(QCA_HDR_RECV_VERSION,
+ QCA_HDR_VERSION) |
+ FIELD_PREP(QCA_HDR_RECV_PRIORITY,
+ priority) |
+ FIELD_PREP(QCA_HDR_RECV_TYPE,
+ reason) |
+ port;
+ bool accept =
+ port == 5 &&
+ reason !=
+ QCA_HDR_RECV_TYPE_MIB &&
+ reason !=
+ QCA_HDR_RECV_TYPE_RW_REG_ACK;
+
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ KUNIT_ASSERT_EQ(test,
+ qca4_private_insert(skb, BIT(5)),
+ 0);
+ put_unaligned_be16(hdr, skb->data + 14);
+ skb->protocol =
+ eth_type_trans(skb, c->conduit);
+ KUNIT_EXPECT_EQ(test, skb->protocol,
+ htons(ETH_P_XDSA));
+ skb->priority = 42;
+ skb->ip_summed = CHECKSUM_COMPLETE;
+ skb->csum = skb_checksum(skb, 0,
+ skb->len, 0);
+ result = qca4_private_rcv(skb,
+ c->conduit);
+ if (!accept) {
+ KUNIT_EXPECT_PTR_EQ(test, result, NULL);
+ continue;
+ }
+ KUNIT_ASSERT_NOT_NULL(test, result);
+ KUNIT_EXPECT_PTR_EQ(test, result->dev,
+ c->netdev);
+ KUNIT_EXPECT_EQ(test, result->priority,
+ 42U);
+ KUNIT_EXPECT_EQ(test, csum_fold(result->csum),
+ csum_fold(skb_checksum(result, 0, result->len,
+ 0)));
+ KUNIT_EXPECT_EQ(test,
+ (unsigned int)result->ip_summed,
+ (unsigned int)CHECKSUM_COMPLETE);
+ skb_push(result, ETH_HLEN);
+ result->protocol = eth_type_trans(result, c->netdev);
+ KUNIT_EXPECT_EQ(test, result->protocol,
+ htons(vlans == 2 ?
+ ETH_P_8021AD :
+ vlans == 1 ? ETH_P_8021Q :
+ ETH_P_IP));
+ kfree_skb(result);
+ }
+ }
+ }
+ }
+}
+
+static void qca4_test_nonlinear_and_truncated(struct kunit *test)
+{
+ struct qca4_test_context *c = test->priv;
+ struct sk_buff *full, *skb, *result;
+ unsigned int n;
+
+ full = qca4_test_frame(16, 2);
+ KUNIT_ASSERT_NOT_NULL(test, full);
+ skb = qca4_test_fragment(full,
+ 10); /* TX Ethernet header crosses fragment. */
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ KUNIT_EXPECT_TRUE(test, skb_is_nonlinear(skb));
+ skb = qca4_private_xmit(skb, c->netdev);
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ KUNIT_EXPECT_EQ(test, get_unaligned_be16(skb->data + 12), 0x8100);
+ kfree_skb(skb);
+ KUNIT_ASSERT_EQ(test, qca4_private_insert(full, BIT(5)), 0);
+ put_unaligned_be16(0xbe45, full->data + 14); /* Actual observed flood metadata. */
+ skb = qca4_test_fragment(full, ETH_HLEN); /* RX metadata+inner EType nonlinear. */
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ skb->protocol = eth_type_trans(skb, c->conduit);
+ result = qca4_private_rcv(skb, c->conduit);
+ KUNIT_ASSERT_NOT_NULL(test, result);
+ KUNIT_EXPECT_PTR_EQ(test, result->dev, c->netdev);
+ kfree_skb(result);
+ /* Wrong identifier. */
+ put_unaligned_be16(0x0081, full->data + 12);
+ full->protocol = eth_type_trans(full, c->conduit);
+ KUNIT_EXPECT_PTR_EQ(test, qca4_private_rcv(full, c->conduit), NULL);
+ for (n = 0; n < 4; n++) {
+ skb = alloc_skb(ETH_HLEN + n, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ skb_put_zero(skb, ETH_HLEN + n);
+ put_unaligned_be16(0x8100, skb->data + 12);
+ skb_reset_mac_header(skb);
+ skb_pull(skb, ETH_HLEN);
+ KUNIT_EXPECT_PTR_EQ(test, qca4_private_rcv(skb, c->conduit),
+ NULL);
+ }
+ for (n = 0; n < ETH_HLEN; n++) {
+ skb = alloc_skb(n, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ skb_put_zero(skb, n);
+ KUNIT_EXPECT_PTR_EQ(test, qca4_private_xmit(skb, c->netdev),
+ NULL);
+ }
+}
+
+static void qca4_test_rx_clone_complete(struct kunit *test)
+{
+ struct qca4_test_context *c = test->priv;
+ struct sk_buff *skb = qca4_test_frame(16, 0), *copy, *result;
+ unsigned int wire_len;
+ u8 before[128];
+
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ KUNIT_ASSERT_EQ(test, qca4_private_insert(skb, BIT(5)), 0);
+ put_unaligned_be16(0xbe45, skb->data + 14);
+ wire_len = skb->len;
+ memcpy(before, skb->data, wire_len);
+ skb->protocol = eth_type_trans(skb, c->conduit);
+ skb->ip_summed = CHECKSUM_COMPLETE;
+ skb->csum = skb_checksum(skb, 0, skb->len, 0);
+ copy = skb_clone(skb, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, copy);
+ result = qca4_private_rcv(copy, c->conduit);
+ KUNIT_ASSERT_NOT_NULL(test, result);
+ KUNIT_EXPECT_MEMEQ(test, skb_mac_header(skb), before, wire_len);
+ KUNIT_EXPECT_EQ(test, csum_fold(result->csum),
+ csum_fold(skb_checksum(result, 0, result->len, 0)));
+ KUNIT_EXPECT_EQ(test, (unsigned int)result->ip_summed,
+ (unsigned int)CHECKSUM_COMPLETE);
+ kfree_skb(result);
+ kfree_skb(skb);
+}
+
+static void qca4_test_masks_and_bad_versions(struct kunit *test)
+{
+ struct qca4_test_context *c = test->priv;
+ struct sk_buff *skb;
+ unsigned int mask, version;
+ int ret;
+
+ for (mask = 0; mask <= 255; mask++) {
+ skb = qca4_test_frame(0, 0);
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ ret = qca4_private_insert(skb, mask);
+ if (!mask || mask > 127) {
+ KUNIT_EXPECT_EQ(test, ret, -EINVAL);
+ } else {
+ KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test,
+ get_unaligned_be16(skb->data + 14),
+ 0x8080 | mask);
+ }
+ kfree_skb(skb);
+ }
+ for (version = 0; version < 4; version++) {
+ if (version == QCA_HDR_VERSION)
+ continue;
+ skb = qca4_test_frame(16, 0);
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ KUNIT_ASSERT_EQ(test, qca4_private_insert(skb, BIT(5)), 0);
+ put_unaligned_be16((version << 14) | 5, skb->data + 14);
+ skb->protocol = eth_type_trans(skb, c->conduit);
+ KUNIT_EXPECT_PTR_EQ(test, qca4_private_rcv(skb, c->conduit),
+ NULL);
+ }
+}
+
+static void qca4_test_flow_dissect(struct kunit *test)
+{
+ static const u8 bytes[] = { 0xbe, 0x45, 0x08, 0x00 };
+ unsigned int length, linear;
+
+ /*
+ * Every truncated length and every possible split across a page fragment.
+ * A one-byte reserve also exercises unaligned linear EtherType access.
+ */
+ for (length = 0; length <= sizeof(bytes); length++) {
+ for (linear = 0; linear <= length; linear++) {
+ struct sk_buff *skb, *full, *clone;
+ unsigned char *data;
+ unsigned int headlen;
+ __be16 proto = htons(0xffff);
+ int offset = -1;
+
+ full = alloc_skb(16, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, full);
+ skb_reserve(full, 1);
+ skb_put_data(full, bytes, length);
+ skb = full;
+ if (linear < length) {
+ skb = qca4_test_fragment(full, linear);
+ kfree_skb(full);
+ KUNIT_ASSERT_NOT_NULL(test, skb);
+ }
+ clone = skb_clone(skb, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, clone);
+ data = clone->data;
+ headlen = skb_headlen(clone);
+ qca4_private_ops.flow_dissect(clone, &proto, &offset);
+ KUNIT_EXPECT_EQ(test, offset, QCA4_PRIVATE_LEN);
+ KUNIT_EXPECT_EQ(test, proto,
+ length == sizeof(bytes) ? htons(ETH_P_IP) : 0);
+ KUNIT_EXPECT_PTR_EQ(test, clone->data, data);
+ KUNIT_EXPECT_EQ(test, clone->len, length);
+ KUNIT_EXPECT_EQ(test, skb_headlen(clone), headlen);
+ KUNIT_EXPECT_MEMEQ(test, skb->data, bytes, headlen);
+ kfree_skb(clone);
+ kfree_skb(skb);
+ }
+ }
+}
+
+static struct kunit_case qca4_private_cases[] = {
+ KUNIT_CASE(qca4_test_flow_dissect),
+ KUNIT_CASE(qca4_test_tx_metadata),
+ KUNIT_CASE(qca4_test_vlan_metadata),
+ KUNIT_CASE(qca4_test_rx_clone_complete),
+ KUNIT_CASE(qca4_test_masks_and_bad_versions),
+ KUNIT_CASE(qca4_test_rx_dispatch),
+ KUNIT_CASE(qca4_test_nonlinear_and_truncated),
+ {}
+};
+
+static struct kunit_suite qca4_private_suite = {
+ .name = "qca4-skb",
+ .init = qca4_test_init,
+ .exit = qca4_test_exit,
+ .test_cases = qca4_private_cases,
+};
+
+kunit_test_suite(qca4_private_suite);
--
2.53.0
[PATCH RFC net-next 6/7] net: cortina: connect wired IPv4 NAT to nftables hardware flowtables
Author: Fil Dunsky via B4 Relay
Date: Wed, 25 Nov 2026 20:50
Date: Wed, 25 Nov 2026 20:50
1203 lines
50949 bytes
50949 bytes
From: Fil Dunsky <filipp.dunsky@gmail.com>
Translate established IPv4 TCP and UDP flowtable requests into checked
CS7542 leases. Derive original Ethernet addresses and QCA metadata from
actual CPU-routed packets, then validate route neighbors, bridge FDB and
the persistent switch-format generation.
Requests outside the supported physical DSA path, namespace, rewrite
policy or MTU are rejected to software. SYN, FIN, IP options, fragments
and low-TTL exceptions remain in the CPU path. Module configuration is
explicit; enabling this driver does not enable a firewall
hardware-offload flag.
Signed-off-by: Fil Dunsky <filipp.dunsky@gmail.com>
---
drivers/net/ethernet/cortina/Kconfig | 11 +
drivers/net/ethernet/cortina/Makefile | 1 +
drivers/net/ethernet/cortina/flowtable/Makefile | 2 +
.../cortina/flowtable/codec/cs_ft_rewrite.c | 234 +++++++++++
.../cortina/flowtable/codec/cs_ft_rewrite.h | 42 ++
.../net/ethernet/cortina/flowtable/cs7542_fw4.c | 437 +++++++++++++++++++++
.../ethernet/cortina/flowtable/cs7542_rx_path.c | 225 +++++++++++
.../ethernet/cortina/flowtable/cs7542_rx_path.h | 35 ++
drivers/net/ethernet/cortina/flowtable/nf_native.c | 90 +++++
drivers/net/ethernet/cortina/flowtable/nf_native.h | 31 ++
10 files changed, 1108 insertions(+)
diff --git a/drivers/net/ethernet/cortina/Kconfig b/drivers/net/ethernet/cortina/Kconfig
index 13df28071..50e627ce3 100644
--- a/drivers/net/ethernet/cortina/Kconfig
+++ b/drivers/net/ethernet/cortina/Kconfig
@@ -88,3 +88,14 @@ config CS7542_FE_WIRED
IPv6, Wi-Fi and unsupported policies continue through the CPU.
TCP requires an established connection; SYN and FIN stay in software.
Route, neighbor, MTU and tag state are checked before admission.
+
+config CS7542_FLOWTABLE
+ tristate "Cortina CS7542 nftables hardware flowtable"
+ depends on CS7542_FE_WIRED && NF_FLOW_TABLE && m
+ depends on NET_DSA_QCA8K_4B_MODE
+ default n
+ help
+ Wired IPv4 TCP/UDP NAT offload through the CS7542 forwarding engine.
+ This RFC frontend is loadable; the Ethernet owner remains built in.
+ Users must explicitly request hardware flowtable offload in nftables.
+ Selecting the driver alone does not change the firewall configuration.
diff --git a/drivers/net/ethernet/cortina/Makefile b/drivers/net/ethernet/cortina/Makefile
index 2bd0572d0..3d6614765 100644
--- a/drivers/net/ethernet/cortina/Makefile
+++ b/drivers/net/ethernet/cortina/Makefile
@@ -16,3 +16,4 @@ endif
obj-$(CONFIG_CS7542_FE_BACKEND) += cs7542_fe_backend.o
cs7542_fe_backend-y := cs_fe_backend_module.o cs_fe_backend_algorithm.o cs_fe_hot_codec.o
+obj-$(CONFIG_CS7542_FLOWTABLE) += flowtable/
diff --git a/drivers/net/ethernet/cortina/flowtable/Makefile b/drivers/net/ethernet/cortina/flowtable/Makefile
new file mode 100644
index 000000000..00bbbc108
--- /dev/null
+++ b/drivers/net/ethernet/cortina/flowtable/Makefile
@@ -0,0 +1,2 @@
+obj-$(CONFIG_CS7542_FLOWTABLE) += cs7542_flowtable.o
+cs7542_flowtable-y := cs7542_fw4.o cs7542_rx_path.o nf_native.o codec/cs_ft_rewrite.o
diff --git a/drivers/net/ethernet/cortina/flowtable/codec/cs_ft_rewrite.c b/drivers/net/ethernet/cortina/flowtable/codec/cs_ft_rewrite.c
new file mode 100644
index 000000000..cd5e4e924
--- /dev/null
+++ b/drivers/net/ethernet/cortina/flowtable/codec/cs_ft_rewrite.c
@@ -0,0 +1,234 @@
+// SPDX-License-Identifier: GPL-2.0
+#ifdef CS_FT_HOST_TEST
+#include "host_shim.h"
+#else
+#include <linux/etherdevice.h>
+#include <linux/ip.h>
+#include <linux/pkt_cls.h>
+#include <linux/tc_act/tc_csum.h>
+#include <net/flow_offload.h>
+#endif
+#include "cs_ft_rewrite.h"
+
+/*
+ * Native u32 mask/value storage is intentional: nf_flow_table_offload copies
+ * network-byte arrays into these scalars. Do not ntohl() them a second time.
+ */
+static int cs_ft_patch(u8 *bytes, u8 *written, unsigned int size,
+ unsigned int offset, u32 mask, u32 value)
+{
+ u8 m[4], v[4];
+ unsigned int i;
+
+ if (offset > size || size - offset < 4)
+ return -EOPNOTSUPP;
+ memcpy(m, &mask, 4);
+ memcpy(v, &value, 4);
+ for (i = 0; i < 4; i++) {
+ if (m[i] != 0 && m[i] != 255)
+ return -EOPNOTSUPP;
+ if (!m[i] && written[offset + i])
+ return -EOPNOTSUPP;
+ if (m[i] && v[i])
+ return -EOPNOTSUPP;
+ }
+ for (i = 0; i < 4; i++)
+ if (!m[i]) {
+ bytes[offset + i] = v[i];
+ written[offset + i] = 1;
+ }
+ return 0;
+}
+
+static int cs_ft_extract_actions(const struct flow_rule *rule, u8 proto,
+ const struct cs_ft_tuple *before,
+ struct cs_ft_rewrite *out)
+{
+ struct cs_ft_rewrite r = { 0 };
+ const struct flow_action_entry *a;
+ u8 eth[12] = { 0 }, eth_written[12] = { 0 };
+ u8 addresses[8], address_written[8] = { 0 }, ports[4],
+ ports_written[4] = { 0 };
+ const u32 csum_flags = TCA_CSUM_UPDATE_FLAG_IPV4HDR |
+ (proto == IPPROTO_TCP ?
+ TCA_CSUM_UPDATE_FLAG_TCP :
+ TCA_CSUM_UPDATE_FLAG_UDP);
+ const enum flow_action_mangle_base port_header =
+ proto == IPPROTO_TCP ? FLOW_ACT_MANGLE_HDR_TYPE_TCP :
+ FLOW_ACT_MANGLE_HDR_TYPE_UDP;
+ unsigned int i, csum = 0, nat = 0;
+ struct net_device *egress = NULL;
+
+ if (!rule || !before || !out || !rule->action.num_entries ||
+ rule->action.num_entries > 16)
+ return -EOPNOTSUPP;
+ memcpy(addresses, &before->src, 4);
+ memcpy(addresses + 4, &before->dst, 4);
+ memcpy(ports, &before->sport, 2);
+ memcpy(ports + 2, &before->dport, 2);
+ flow_action_for_each(i, a, &rule->action) {
+ if (egress)
+ return -EOPNOTSUPP; /* redirect must be last */
+ switch (a->id) {
+ case FLOW_ACTION_MANGLE:
+ if (csum)
+ return -EOPNOTSUPP;
+ switch (a->mangle.htype) {
+ case FLOW_ACT_MANGLE_HDR_TYPE_ETH:
+ if ((a->mangle.offset != 0 &&
+ a->mangle.offset != 4 &&
+ a->mangle.offset != 8) ||
+ cs_ft_patch(eth, eth_written, 12,
+ a->mangle.offset,
+ a->mangle.mask, a->mangle.val))
+ return -EOPNOTSUPP;
+ break;
+ case FLOW_ACT_MANGLE_HDR_TYPE_IP4:
+ if ((a->mangle.offset != 12 &&
+ a->mangle.offset != 16) ||
+ a->mangle.mask ||
+ cs_ft_patch(addresses, address_written, 8,
+ a->mangle.offset - 12,
+ a->mangle.mask, a->mangle.val))
+ return -EOPNOTSUPP;
+ nat = 1;
+ break;
+ case FLOW_ACT_MANGLE_HDR_TYPE_UDP:
+ case FLOW_ACT_MANGLE_HDR_TYPE_TCP:
+ if (a->mangle.htype != port_header ||
+ a->mangle.offset ||
+ cs_ft_patch(ports, ports_written, 4, 0,
+ a->mangle.mask, a->mangle.val))
+ return -EOPNOTSUPP;
+ nat = 1;
+ break;
+ default:
+ return -EOPNOTSUPP;
+ }
+ break;
+ case FLOW_ACTION_CSUM:
+ if (csum++ || !nat || a->csum_flags != csum_flags)
+ return -EOPNOTSUPP;
+ break;
+ case FLOW_ACTION_REDIRECT:
+ if (!a->dev)
+ return -EOPNOTSUPP;
+ egress = a->dev;
+ break;
+ default:
+ return -EOPNOTSUPP;
+ }
+ }
+ if (!egress || (nat && !csum))
+ return -EOPNOTSUPP;
+ for (i = 0; i < 12; i++)
+ if (!eth_written[i])
+ return -EOPNOTSUPP;
+ if (!is_valid_ether_addr(eth) || !is_valid_ether_addr(eth + 6))
+ return -EOPNOTSUPP;
+ r.before = *before;
+ memcpy(r.dmac, eth, 6);
+ memcpy(r.smac, eth + 6, 6);
+ memcpy(&r.after.src, addresses, 4);
+ memcpy(&r.after.dst, addresses + 4, 4);
+ memcpy(&r.after.sport, ports, 2);
+ memcpy(&r.after.dport, ports + 2, 2);
+ r.redirect = egress;
+ r.action_count = rule->action.num_entries;
+ r.nat_action = !!nat;
+ r.csum_action = !!csum;
+ r.csum_flags = csum ? csum_flags : 0;
+ *out = r;
+ return 0;
+}
+
+static int cs_ft_extract_match(const struct flow_rule *rule,
+ struct cs_ft_tuple *tuple, int *ingress,
+ u8 *proto)
+{
+ const u64 required = 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);
+ struct flow_match_meta meta;
+ struct flow_match_control control;
+ struct flow_match_basic basic;
+ struct flow_match_ipv4_addrs ip;
+ struct flow_match_ports ports;
+
+ if (!rule || !tuple || !ingress || !rule->match.dissector ||
+ (rule->match.dissector->used_keys & required) != required)
+ return -EOPNOTSUPP;
+ flow_rule_match_meta(rule, &meta);
+ flow_rule_match_control(rule, &control);
+ flow_rule_match_basic(rule, &basic);
+ flow_rule_match_ipv4_addrs(rule, &ip);
+ flow_rule_match_ports(rule, &ports);
+ if (meta.mask->ingress_ifindex != -1 || meta.mask->ingress_iftype ||
+ meta.mask->l2_miss ||
+ control.key->addr_type != FLOW_DISSECTOR_KEY_IPV4_ADDRS ||
+ control.mask->addr_type != 0xffff || control.mask->flags ||
+ control.mask->thoff || basic.key->n_proto != htons(ETH_P_IP) ||
+ basic.mask->n_proto != htons(0xffff) ||
+ basic.mask->ip_proto != 255 || ip.mask->src != htonl(~0U) ||
+ ip.mask->dst != htonl(~0U) || ports.mask->src != htons(0xffff) ||
+ ports.mask->dst != htons(0xffff))
+ return -EOPNOTSUPP;
+ if (basic.key->ip_proto == IPPROTO_TCP) {
+ struct flow_match_tcp tcp;
+
+ if (!(rule->match.dissector->used_keys &
+ BIT_ULL(FLOW_DISSECTOR_KEY_TCP)))
+ return -EOPNOTSUPP;
+ flow_rule_match_tcp(rule, &tcp);
+ if (tcp.key->flags || tcp.mask->flags != htons(0x05))
+ return -EOPNOTSUPP;
+ } else if (basic.key->ip_proto != IPPROTO_UDP) {
+ return -EOPNOTSUPP;
+ }
+ *proto = basic.key->ip_proto;
+ *tuple = (struct cs_ft_tuple){ ip.key->src, ip.key->dst, ports.key->src,
+ ports.key->dst };
+ *ingress = meta.key->ingress_ifindex;
+ return 0;
+}
+
+int cs_ft_nf_extract(const struct flow_cls_offload *cls, bool routed_nf_context,
+ struct cs_ft_nf_facts *out)
+{
+ struct cs_ft_nf_facts f = { 0 };
+ struct cs_ft_tuple tuple;
+ const struct flow_rule *rule;
+ const u64 required = 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 (!cls || !out || cls->command != FLOW_CLS_REPLACE ||
+ cls->common.chain_index)
+ return -EOPNOTSUPP;
+ rule = flow_cls_offload_flow_rule(cls);
+ if (cs_ft_extract_match(rule, &tuple, &f.ingress_ifindex,
+ &f.ip_proto) ||
+ cs_ft_extract_actions(rule, f.ip_proto, &tuple, &f.rewrite))
+ return -EOPNOTSUPP;
+ f.egress_ifindex = f.rewrite.redirect->ifindex;
+ f.rewrite.redirect =
+ NULL; /* No borrowed netdev pointer escapes diagnostic API. */
+ f.match_used_keys = rule->match.dissector->used_keys;
+ f.extra_match_keys =
+ f.match_used_keys !=
+ (required |
+ (f.ip_proto == IPPROTO_TCP ? BIT_ULL(FLOW_DISSECTOR_KEY_TCP) :
+ 0));
+ f.vlan_offset =
+ !!rule->match.dissector->offset[FLOW_DISSECTOR_KEY_VLAN];
+ f.cvlan_offset =
+ !!rule->match.dissector->offset[FLOW_DISSECTOR_KEY_CVLAN];
+ f.routed_nf_context = routed_nf_context;
+ f.ttl_decrement_from_context = routed_nf_context;
+ /* Unknown ingress encapsulation stays unknown even without VLAN actions. */
+ *out = f;
+ return 0;
+}
diff --git a/drivers/net/ethernet/cortina/flowtable/codec/cs_ft_rewrite.h b/drivers/net/ethernet/cortina/flowtable/codec/cs_ft_rewrite.h
new file mode 100644
index 000000000..9f96b1ea6
--- /dev/null
+++ b/drivers/net/ethernet/cortina/flowtable/codec/cs_ft_rewrite.h
@@ -0,0 +1,42 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef CS_FT_REWRITE_H
+#define CS_FT_REWRITE_H
+#ifndef CS_FT_HOST_TEST
+#include <linux/types.h>
+#include <net/flow_offload.h>
+#endif
+struct cs_ft_tuple {
+ __be32 src, dst;
+ __be16 sport, dport;
+};
+
+struct cs_ft_rewrite {
+ struct cs_ft_tuple before, after;
+ u8 smac[6],
+ dmac[6]; /* Requested OUTPUT MACs; original MACs not in these NF matches. */
+ struct net_device
+ *redirect; /* Borrowed only during the synchronous call. */
+ unsigned int action_count, csum_flags;
+ bool nat_action, csum_action;
+};
+
+/*
+ * Decode facts only: no topology, domain, QCA or hardware admission. Native
+ *
+ * mask/value bytes follow the actual NF generator; no second ntohl().
+ */
+struct cs_ft_nf_facts {
+ struct cs_ft_rewrite rewrite;
+ u8 ip_proto;
+ int ingress_ifindex, egress_ifindex;
+ u64 match_used_keys;
+ bool extra_match_keys, vlan_offset, cvlan_offset;
+ bool routed_nf_context, ttl_decrement_from_context;
+ bool ttl_action_observed; /* false: this supported NF action set has none. */
+ bool customer_encap_proven_plain; /* always false; absence isn't provenance. */
+ bool hardware_admitted; /* always false: diagnostic result only. */
+};
+
+int cs_ft_nf_extract(const struct flow_cls_offload *cls, bool routed_nf_context,
+ struct cs_ft_nf_facts *facts);
+#endif
diff --git a/drivers/net/ethernet/cortina/flowtable/cs7542_fw4.c b/drivers/net/ethernet/cortina/flowtable/cs7542_fw4.c
new file mode 100644
index 000000000..d5a132e87
--- /dev/null
+++ b/drivers/net/ethernet/cortina/flowtable/cs7542_fw4.c
@@ -0,0 +1,437 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Standard nftables flowtable frontend for the CS7542/QCA8337 wired path. */
+#include <linux/bitfield.h>
+#include <linux/cs7542_ft_live.h>
+#include <linux/dsa/qca8k_persistent.h>
+#include <linux/etherdevice.h>
+#include <linux/if_bridge.h>
+#include <linux/list.h>
+#include <linux/module.h>
+#include <linux/netdevice.h>
+#include <linux/of.h>
+#include <linux/rtnetlink.h>
+#include <linux/slab.h>
+#include <net/dsa.h>
+#include <net/flow_offload.h>
+#include <net/net_namespace.h>
+#include <net/arp.h>
+#include <net/ip.h>
+#include <net/route.h>
+#include "nf_native.h"
+
+struct native_binding {
+ struct list_head list;
+ struct cs7542_fe_live *owner;
+ struct net_device *conduit;
+ u64 id, handle, version;
+};
+
+/*
+ * cs7542_ft_live serializes every binding callback and retires them all
+ * before dropping the registration pin. The counters are read from sysfs.
+ */
+static LIST_HEAD(bindings);
+static atomic64_t counts[NATIVE_REASONS];
+static typeof(cs7542_fe_live_replace_native) *replace_native;
+static typeof(cs7542_fe_live_replace_native_tcp) *replace_native_tcp;
+static typeof(qca8k_persistent_status) *persistent_status;
+static typeof(cs7542_fe_live_native_lastused) *native_lastused;
+/*
+ * The private nf_flow_table module supplies the PMTU of this direction.
+ * This frontend can load before the flowtable module.
+ */
+
+static struct native_binding *find_binding(u64 id)
+{
+ struct native_binding *b;
+
+ list_for_each_entry(b, &bindings, list)
+ if (b->id == id)
+ return b;
+ return NULL;
+}
+
+static int native_open(void *ctx, u64 id, struct net_device *conduit)
+{
+ struct native_binding *b;
+ int err;
+
+ b = find_binding(id);
+ if (b)
+ return b->conduit ? -EEXIST : -ESHUTDOWN;
+ b = kzalloc_obj(*b);
+ if (!b)
+ return -ENOMEM;
+ b->owner = cs7542_fe_live_get(conduit);
+ if (IS_ERR(b->owner)) {
+ err = PTR_ERR(b->owner);
+ goto free;
+ }
+ err = cs7542_fe_live_open(b->owner, &b->handle);
+ if (err) {
+ cs7542_fe_live_put(b->owner);
+ goto free;
+ }
+ b->id = id;
+ dev_hold(conduit);
+ b->conduit = conduit;
+ list_add_tail(&b->list, &bindings);
+ return 0;
+free:
+ kfree(b);
+ return err;
+}
+
+static struct dsa_port *get_endpoint(struct net_device *conduit, int ifindex,
+ struct native_endpoint *endpoint)
+{
+ struct net_device *dev = __dev_get_by_index(dev_net(conduit), ifindex);
+ struct net_device *bridge;
+ struct dsa_port *port;
+
+ ASSERT_RTNL();
+ port = dsa_port_from_netdev(dev);
+ if (IS_ERR(port) || !port->ds || !port->ds->dev || !port->cpu_dp ||
+ port->cpu_dp->conduit != conduit || port->cpu_dp->index != 0 ||
+ !of_device_is_compatible(port->ds->dev->of_node, "qca,qca8337") ||
+ port->index < 1 || port->index > 5 || port->lag || port->hsr_dev ||
+ !netif_running(dev) || !netif_carrier_ok(dev) ||
+ dsa_port_is_vlan_filtering(port))
+ return ERR_PTR(-EOPNOTSUPP);
+ bridge = netdev_master_upper_dev_get(dev);
+ if (bridge &&
+ (!netif_is_bridge_master(bridge) || !netif_running(bridge) ||
+ br_vlan_enabled(bridge) ||
+ br_port_get_stp_state(dev) != BR_STATE_FORWARDING ||
+ br_port_flag_is_set(dev, BR_PORT_LOCKED | BR_PORT_MAB |
+ BR_ISOLATED | BR_VLAN_TUNNEL)))
+ return ERR_PTR(-EOPNOTSUPP);
+ endpoint->ifindex = ifindex;
+ endpoint->port = port->index;
+ ether_addr_copy(endpoint->mac,
+ bridge ? bridge->dev_addr : dev->dev_addr);
+ return port;
+}
+
+static bool fdb_matches(const struct dsa_port *port, const u8 addr[ETH_ALEN])
+{
+ struct net_device *bridge = netdev_master_upper_dev_get(port->user);
+
+ return !bridge || br_fdb_find_port(bridge, addr, 0) == port->user;
+}
+
+/*
+ * Re-resolve the unicast next hop on the same Linux L3 device. The nft
+ * action remains authoritative. The lookup may only corroborate it or
+ * reject it, including routes that require unsupported neighbour types.
+ */
+static int native_check_neighbour(const struct cs7542_fe_native_request *r)
+{
+ struct net_device *dev =
+ __dev_get_by_index(&init_net, r->egress_ifindex);
+ struct flowi4 fl = {
+ .daddr = htonl(r->rule.translated.dst),
+ .saddr = htonl(r->rule.original.src),
+ .fl4_sport = htons(r->rule.original.sport),
+ .fl4_dport = htons(r->rule.translated.dport),
+ .flowi4_proto = r->rule.native_l4proto,
+ .flowi4_flags = FLOWI_FLAG_ANYSRC,
+ };
+ struct net_device *l3;
+ struct neighbour *neigh;
+ struct rtable *rt;
+ __be32 next;
+ int err = -ESTALE;
+
+ ASSERT_RTNL();
+ if (!dev)
+ return -ENODEV;
+ l3 = netdev_master_upper_dev_get(dev);
+ if (!l3)
+ l3 = dev;
+ fl.flowi4_oif = l3->ifindex;
+ rt = ip_route_output_key(&init_net, &fl);
+ if (IS_ERR(rt))
+ return PTR_ERR(rt);
+ if (rt->rt_type != RTN_UNICAST || dst_dev(&rt->dst) != l3 ||
+ dst_xfrm(&rt->dst) || rt->dst.lwtstate ||
+ (rt->rt_gw_family && rt->rt_gw_family != AF_INET) ||
+ ip_dst_mtu_maybe_forward(&rt->dst, true) < r->route_mtu)
+ goto put;
+ next = rt_nexthop(rt, fl.daddr);
+ /*
+ * Read an existing entry only. Hardware admission never creates one or
+ * triggers an ARP solicitation to manufacture its own evidence.
+ */
+ neigh = neigh_lookup(arp_table(dev_net(l3)), &next, l3);
+ if (neigh) {
+ read_lock_bh(&neigh->lock);
+ if ((neigh->nud_state & NUD_VALID) &&
+ ether_addr_equal(neigh->ha, r->rule.dmac))
+ err = 0;
+ read_unlock_bh(&neigh->lock);
+ neigh_release(neigh);
+ }
+put:
+ ip_rt_put(rt);
+ return err;
+}
+
+static enum native_reason
+prepare_request(struct native_binding *b, struct flow_cls_offload *cls,
+ struct cs7542_fe_native_request *request)
+{
+ struct qca8k_persistent_status format = {
+ .size = sizeof(format),
+ .version = QCA8K_PERSISTENT_STATUS_VERSION,
+ };
+ struct cs_ft_nf_facts facts;
+ struct native_endpoint in, out;
+ struct cs7542_l4_tuple tuple;
+ struct dsa_port *ip, *op;
+ struct cs7542_rx_path wire;
+ enum native_reason reason;
+ typeof(nf_flow_offload_route_mtu) *route_mtu;
+
+ ASSERT_RTNL();
+ if (cs_ft_nf_extract(cls, true, &facts) || facts.extra_match_keys ||
+ facts.vlan_offset || facts.cvlan_offset ||
+ !facts.rewrite.nat_action)
+ return NATIVE_MATCH;
+ if (facts.ip_proto == IPPROTO_TCP &&
+ (!replace_native_tcp || !cls->nf_tcp_established))
+ return NATIVE_MATCH;
+ if (!net_eq(dev_net(b->conduit), &init_net) ||
+ !netif_running(b->conduit) || !netif_carrier_ok(b->conduit))
+ return NATIVE_PATH;
+ ip = get_endpoint(b->conduit, facts.ingress_ifindex, &in);
+ op = get_endpoint(b->conduit, facts.egress_ifindex, &out);
+ if (IS_ERR(ip) || IS_ERR(op) || ip->ds != op->ds)
+ return NATIVE_PATH;
+ tuple = (struct cs7542_l4_tuple){
+ .src = facts.rewrite.before.src,
+ .dst = facts.rewrite.before.dst,
+ .sport = facts.rewrite.before.sport,
+ .dport = facts.rewrite.before.dport,
+ .ip_proto = htonl(facts.ip_proto),
+ };
+ if (cs7542_rx_path_lookup(b->conduit, in.port, &tuple, &wire))
+ return NATIVE_WIRE;
+ if (!fdb_matches(ip, wire.source) ||
+ !fdb_matches(op, facts.rewrite.dmac))
+ return NATIVE_PATH;
+ reason = native_rule_build(&facts, &in, &out, &wire, request);
+ if (reason != NATIVE_OWNER)
+ return reason;
+ if (!persistent_status || persistent_status(b->conduit, &format) ||
+ format.size != sizeof(format) ||
+ format.version != QCA8K_PERSISTENT_STATUS_VERSION ||
+ !format.epoch || format.requested != 1 || format.known != 1 ||
+ format.coherent != 1 || format.ready != 1 ||
+ format.restore_debt != 1 || format.closing || format.uncertain ||
+ format.first_error || format.held_fault || format.reserved ||
+ format.selected != DSA_TAG_PROTO_QCA4 ||
+ format.tree_protocol != DSA_TAG_PROTO_QCA4)
+ return NATIVE_TAG;
+ request->format_epoch = format.epoch;
+ route_mtu = symbol_get(nf_flow_offload_route_mtu);
+ if (route_mtu) {
+ request->route_mtu = route_mtu(cls);
+ symbol_put(nf_flow_offload_route_mtu);
+ }
+ return NATIVE_OWNER;
+}
+
+static int
+native_replace_checked(struct native_binding *b, struct flow_cls_offload *cls,
+ const struct cs7542_fe_native_request *request,
+ enum native_reason *reason)
+{
+ int err;
+
+ err = native_check_neighbour(request);
+ if (err) {
+ *reason = NATIVE_PATH;
+ return cs7542_fe_live_reject(b->owner, b->handle, cls->cookie,
+ b->version, err);
+ }
+ if (request->rule.native_l4proto == IPPROTO_TCP)
+ err = replace_native_tcp(b->owner, b->handle, cls->cookie,
+ b->version, request, cls);
+ else
+ err = replace_native(b->owner, b->handle, cls->cookie,
+ b->version, request);
+ if (err)
+ return err;
+ /*
+ * Updates during publication are covered by the built-in watcher.
+ * Recheck the gap between the initial validation and publication.
+ */
+ err = native_check_neighbour(request);
+ if (err) {
+ *reason = NATIVE_PATH;
+ return cs7542_fe_live_destroy(b->owner, b->handle, cls->cookie, b->version) ?:
+ err;
+ }
+ *reason = NATIVE_INSTALLED;
+ return 0;
+}
+
+static int native_rule(void *ctx, u64 id, struct flow_cls_offload *cls)
+{
+ struct native_binding *b = find_binding(id);
+ struct cs7542_fe_native_request request;
+ enum native_reason reason;
+ int result;
+
+ if (!b)
+ return -ENOENT;
+ if (!b->conduit)
+ return -ESHUTDOWN;
+ if (cls->command == FLOW_CLS_STATS) {
+ unsigned long lastused;
+
+ if (!native_lastused)
+ return -EOPNOTSUPP;
+ result = native_lastused(b->owner, b->handle, cls->cookie,
+ &lastused);
+ if (!result)
+ flow_stats_update(&cls->stats, 0, 0, 0, lastused,
+ FLOW_ACTION_HW_STATS_DELAYED);
+ return result;
+ }
+ if (b->version == U64_MAX)
+ return -EOVERFLOW;
+ b->version++;
+ if (cls->command == FLOW_CLS_DESTROY)
+ return cs7542_fe_live_destroy(b->owner, b->handle, cls->cookie,
+ b->version);
+ if (cls->command != FLOW_CLS_REPLACE)
+ return -EOPNOTSUPP;
+ /*
+ * flow-block callbacks can race RTNL-held teardown. Never wait on RTNL
+ * while holding the live bridge mutex. Every rejection withdraws an
+ * earlier version of this cookie before returning it to software.
+ */
+ if (!rtnl_trylock()) {
+ reason = NATIVE_PATH;
+ result = cs7542_fe_live_reject(b->owner, b->handle, cls->cookie,
+ b->version, -EBUSY);
+ } else {
+ reason = prepare_request(b, cls, &request);
+ if (reason == NATIVE_OWNER && request.route_mtu &&
+ replace_native) {
+ result = native_replace_checked(b, cls, &request,
+ &reason);
+ } else {
+ result = cs7542_fe_live_reject(b->owner, b->handle,
+ cls->cookie, b->version,
+ -EOPNOTSUPP);
+ }
+ rtnl_unlock();
+ }
+ atomic64_inc(&counts[reason]);
+ return result;
+}
+
+static int native_close(void *ctx, u64 id)
+{
+ struct native_binding *b = find_binding(id);
+ int err;
+
+ if (!b)
+ return 0;
+ /*
+ * A failed hardware cut keeps the block handle and owner reference for
+ * retry. It must never hold unregister_netdevice() hostage.
+ */
+ if (b->conduit) {
+ dev_put(b->conduit);
+ b->conduit = NULL;
+ }
+ err = cs7542_fe_live_close(b->owner, b->handle);
+ if (err)
+ return err;
+ list_del(&b->list);
+ cs7542_fe_live_put(b->owner);
+ kfree(b);
+ return 0;
+}
+
+static int native_detach(void *ctx)
+{
+ /* The bridge calls close() for every open binding before detach(). */
+ return list_empty(&bindings) ? 0 : -EBUSY;
+}
+
+static const struct cs7542_ft_live_ops native_ops = {
+ .api_version = CS7542_FT_LIVE_API_VERSION,
+ .size = sizeof(native_ops),
+ .owner = THIS_MODULE,
+ .name = "cs7542-fw4",
+ .open = native_open,
+ .rule = native_rule,
+ .close = native_close,
+ .detach = native_detach,
+};
+
+static int status_get(char *buf, const struct kernel_param *kp)
+{
+ return sysfs_emit(buf,
+ "owner_api=%u rule=%lld topology=%lld wire=%lld mac=%lld format=%lld owner=%lld installed=%lld tcp_native=%u\n",
+ !!replace_native, atomic64_read(&counts[NATIVE_MATCH]),
+ atomic64_read(&counts[NATIVE_PATH]),
+ atomic64_read(&counts[NATIVE_WIRE]),
+ atomic64_read(&counts[NATIVE_MAC]),
+ atomic64_read(&counts[NATIVE_TAG]),
+ atomic64_read(&counts[NATIVE_OWNER]),
+ atomic64_read(&counts[NATIVE_INSTALLED]), !!replace_native_tcp);
+}
+
+static const struct kernel_param_ops status_ops = { .get = status_get };
+module_param_cb(status, &status_ops, NULL, 0444);
+
+static int __init native_init(void)
+{
+ int err = cs7542_rx_path_init();
+
+ if (err)
+ return err;
+ replace_native = symbol_get(cs7542_fe_live_replace_native);
+ replace_native_tcp = symbol_get(cs7542_fe_live_replace_native_tcp);
+ persistent_status = symbol_get(qca8k_persistent_status);
+ native_lastused = symbol_get(cs7542_fe_live_native_lastused);
+ err = cs7542_ft_live_register(&native_ops, NULL);
+ if (err) {
+ if (native_lastused)
+ symbol_put(cs7542_fe_live_native_lastused);
+ if (persistent_status)
+ symbol_put(qca8k_persistent_status);
+ if (replace_native_tcp)
+ symbol_put(cs7542_fe_live_replace_native_tcp);
+ if (replace_native)
+ symbol_put(cs7542_fe_live_replace_native);
+ cs7542_rx_path_exit();
+ }
+ return err;
+}
+
+static void __exit native_exit(void)
+{
+ /* The bridge's reference pins this module until its callbacks quiesce. */
+ cs7542_rx_path_exit();
+ if (native_lastused)
+ symbol_put(cs7542_fe_live_native_lastused);
+ if (persistent_status)
+ symbol_put(qca8k_persistent_status);
+ if (replace_native_tcp)
+ symbol_put(cs7542_fe_live_replace_native_tcp);
+ if (replace_native)
+ symbol_put(cs7542_fe_live_replace_native);
+}
+
+module_init(native_init);
+module_exit(native_exit);
+MODULE_LICENSE("GPL");
+MODULE_INFO(description,
+ "Cortina CS7542 IPv4 TCP and UDP nftables hardware flowtable frontend");
diff --git a/drivers/net/ethernet/cortina/flowtable/cs7542_rx_path.c b/drivers/net/ethernet/cortina/flowtable/cs7542_rx_path.c
new file mode 100644
index 000000000..20a132852
--- /dev/null
+++ b/drivers/net/ethernet/cortina/flowtable/cs7542_rx_path.c
@@ -0,0 +1,225 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Recover the original Ethernet addresses and switch tag of a CPU-routed flow. */
+#include <linux/bitfield.h>
+#include <linux/dsa/tag_qca.h>
+#include <linux/etherdevice.h>
+#include <linux/if_vlan.h>
+#include <linux/ip.h>
+#include <linux/jhash.h>
+#include <linux/jiffies.h>
+#include <linux/module.h>
+#include <linux/netdevice.h>
+#include <linux/of.h>
+#include <linux/udp.h>
+#include <linux/tcp.h>
+#include <linux/unaligned.h>
+#include <net/ip.h>
+#include <net/net_namespace.h>
+#include "cs7542_rx_path.h"
+
+#define PATH_SLOTS 128
+#define PATH_AGE (2 * HZ)
+
+/*
+ * skb->dev is the DSA conduit. Its WAN user port can have a different
+ * unicast MAC. Resolve the addressed L3 device when nftables supplies the
+ * real source port of the route. Observing a frame never installs a rule.
+ */
+struct path_entry {
+ struct cs7542_l4_tuple tuple;
+ struct cs7542_rx_path path;
+ int ifindex;
+ unsigned long expires;
+ bool ambiguous;
+};
+
+static DEFINE_SPINLOCK(paths_lock);
+static struct path_entry paths[PATH_SLOTS];
+static unsigned int index_of(const struct cs7542_l4_tuple *tuple, int ifindex)
+{
+ static_assert(sizeof(*tuple) == 16);
+ return jhash(tuple, sizeof(*tuple), ifindex) % PATH_SLOTS;
+}
+
+static bool fresh(const struct path_entry *entry)
+{
+ return entry->ifindex && time_before(jiffies, entry->expires);
+}
+
+static void remember(int ifindex, const struct cs7542_l4_tuple *tuple,
+ const struct cs7542_rx_path *path)
+{
+ struct path_entry *entry = &paths[index_of(tuple, ifindex)];
+
+ spin_lock_bh(&paths_lock);
+ if (!fresh(entry)) {
+ entry->ifindex = ifindex;
+ entry->tuple = *tuple;
+ entry->path = *path;
+ entry->ambiguous = false;
+ } else if (entry->ifindex != ifindex ||
+ memcmp(&entry->tuple, tuple, sizeof(*tuple))) {
+ /* Keep a live owner's observation across hash collisions. */
+ goto out;
+ } else if (entry->path.tag != path->tag ||
+ entry->path.tag_len != path->tag_len ||
+ !ether_addr_equal(entry->path.source, path->source) ||
+ !ether_addr_equal(entry->path.destination,
+ path->destination)) {
+ entry->ambiguous = true;
+ }
+ entry->expires = jiffies + PATH_AGE;
+out:
+ spin_unlock_bh(&paths_lock);
+}
+
+int cs7542_rx_path_lookup(struct net_device *conduit, unsigned int port,
+ const struct cs7542_l4_tuple *tuple,
+ struct cs7542_rx_path *out)
+{
+ struct path_entry *entry;
+ int error = -ENOENT;
+
+ if (!conduit || !tuple || !out || !net_eq(dev_net(conduit), &init_net))
+ return -EINVAL;
+ entry = &paths[index_of(tuple, conduit->ifindex)];
+ spin_lock_bh(&paths_lock);
+ if (fresh(entry) && entry->ifindex == conduit->ifindex &&
+ !memcmp(&entry->tuple, tuple, sizeof(*tuple)) &&
+ !entry->ambiguous &&
+ FIELD_GET(QCA_HDR_RECV_SOURCE_PORT, entry->path.tag) == port) {
+ *out = entry->path;
+ error = 0;
+ }
+ spin_unlock_bh(&paths_lock);
+ return error;
+}
+
+static void observe(struct sk_buff *skb, struct net_device *dev)
+{
+ u8 ether[ETH_HLEN + 4] __aligned(4);
+ struct iphdr ip;
+ union {
+ struct udphdr udp;
+ struct tcphdr tcp;
+ } transport;
+ unsigned int l4_length;
+ struct cs7542_l4_tuple tuple;
+ struct cs7542_rx_path path;
+ unsigned int reason;
+ int offset;
+
+ if ((skb->pkt_type != PACKET_HOST &&
+ skb->pkt_type != PACKET_OTHERHOST) ||
+ !net_eq(dev_net(dev), &init_net) || !skb_mac_header_was_set(skb) ||
+ skb_vlan_tag_present(skb) || skb_is_gso(skb))
+ return;
+ offset = skb_mac_header(skb) - skb->data;
+ if (offset != -ETH_HLEN ||
+ skb_copy_bits(skb, offset, ether, sizeof(ether)))
+ return;
+ path.tag_len = get_unaligned_be16(ether + 12) == ETH_P_8021Q ? 4 : 2;
+ path.tag = get_unaligned_be16(ether + 10 + path.tag_len);
+ reason = FIELD_GET(QCA_HDR_RECV_TYPE, path.tag);
+ /*
+ * Ordinary frames sent to the CPU also carry forwarding reason codes.
+ * Both QCA taggers consume register replies and MIB packets themselves.
+ */
+ if (FIELD_GET(QCA_HDR_RECV_VERSION, path.tag) != QCA_HDR_VERSION ||
+ reason == QCA_HDR_RECV_TYPE_MIB ||
+ reason == QCA_HDR_RECV_TYPE_RW_REG_ACK ||
+ (path.tag & QCA_HDR_RECV_FRAME_IS_TAGGED) ||
+ !FIELD_GET(QCA_HDR_RECV_SOURCE_PORT, path.tag) ||
+ get_unaligned_be16(ether + 12 + path.tag_len) != ETH_P_IP ||
+ !is_valid_ether_addr(ether) ||
+ !is_valid_ether_addr(ether + ETH_ALEN))
+ return;
+ if (skb_copy_bits(skb, path.tag_len, &ip, sizeof(ip)) ||
+ ip.version != 4 || ip.ihl != 5 || ip_is_fragment(&ip) ||
+ ip_fast_csum(&ip, 5) || ntohs(ip.tot_len) < sizeof(ip) ||
+ skb->len < path.tag_len + ntohs(ip.tot_len))
+ return;
+ l4_length = ntohs(ip.tot_len) - sizeof(ip);
+ if (ip.protocol == IPPROTO_UDP) {
+ if (l4_length < sizeof(transport.udp) ||
+ skb_copy_bits(skb, path.tag_len + sizeof(ip),
+ &transport.udp, sizeof(transport.udp)) ||
+ ntohs(transport.udp.len) != l4_length)
+ return;
+ tuple.sport = transport.udp.source;
+ tuple.dport = transport.udp.dest;
+ } else if (ip.protocol == IPPROTO_TCP) {
+ /*
+ * Even a handshake can identify a switch peer. Only netfilter can
+ * authorize established data offload via its typed request.
+ */
+ if (l4_length < sizeof(transport.tcp) ||
+ skb_copy_bits(skb, path.tag_len + sizeof(ip),
+ &transport.tcp, sizeof(transport.tcp)) ||
+ transport.tcp.doff < 5 ||
+ transport.tcp.doff * 4 > l4_length)
+ return;
+ tuple.sport = transport.tcp.source;
+ tuple.dport = transport.tcp.dest;
+ } else {
+ return;
+ }
+ tuple.src = ip.saddr;
+ tuple.dst = ip.daddr;
+ tuple.ip_proto = htonl(ip.protocol);
+ ether_addr_copy(path.source, ether + ETH_ALEN);
+ ether_addr_copy(path.destination, ether);
+ remember(dev->ifindex, &tuple, &path);
+}
+
+static int receive(struct sk_buff *skb, struct net_device *dev,
+ struct packet_type *ptype, struct net_device *original)
+{
+ observe(skb, dev);
+ consume_skb(skb);
+ return NET_RX_SUCCESS;
+}
+
+static struct packet_type observer = {
+ .type = cpu_to_be16(ETH_P_XDSA),
+ .func = receive,
+};
+
+static int link_event(struct notifier_block *nb, unsigned long event,
+ void *data)
+{
+ struct net_device *dev = netdev_notifier_info_to_dev(data);
+ unsigned int i;
+
+ if (!net_eq(dev_net(dev), &init_net) ||
+ (event != NETDEV_DOWN && event != NETDEV_CHANGEADDR &&
+ event != NETDEV_UNREGISTER))
+ return NOTIFY_DONE;
+ spin_lock_bh(&paths_lock);
+ for (i = 0; i < ARRAY_SIZE(paths); i++)
+ if (paths[i].ifindex == dev->ifindex)
+ paths[i].ifindex = 0;
+ spin_unlock_bh(&paths_lock);
+ return NOTIFY_DONE;
+}
+
+static struct notifier_block netdev_events = { .notifier_call = link_event };
+
+int cs7542_rx_path_init(void)
+{
+ int error;
+
+ if (!of_machine_is_compatible("securifi,almondplus"))
+ return -ENODEV;
+ error = register_netdevice_notifier(&netdev_events);
+ if (error)
+ return error;
+ dev_add_pack(&observer);
+ return 0;
+}
+
+void cs7542_rx_path_exit(void)
+{
+ dev_remove_pack(&observer);
+ unregister_netdevice_notifier(&netdev_events);
+}
diff --git a/drivers/net/ethernet/cortina/flowtable/cs7542_rx_path.h b/drivers/net/ethernet/cortina/flowtable/cs7542_rx_path.h
new file mode 100644
index 000000000..20f6b4c7c
--- /dev/null
+++ b/drivers/net/ethernet/cortina/flowtable/cs7542_rx_path.h
@@ -0,0 +1,35 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef CS7542_RX_PATH_H
+#define CS7542_RX_PATH_H
+
+#include <linux/if_ether.h>
+#include <linux/types.h>
+
+struct net_device;
+
+struct cs7542_l4_tuple {
+ __be32 src, dst;
+ __be16 sport, dport;
+ /* Network order keeps all 16 hash/key bytes defined on every architecture. */
+ __be32 ip_proto;
+};
+
+struct cs7542_rx_path {
+ u8 source[ETH_ALEN];
+ u8 destination[ETH_ALEN];
+ u16 tag;
+ u8 tag_len;
+};
+
+/*
+ * The caller supplies the flow's live ingress conduit and DSA port.
+ * An observation is input to the classifier, not permission to install a rule.
+ */
+int cs7542_rx_path_lookup(struct net_device *conduit, unsigned int port,
+ const struct cs7542_l4_tuple *tuple,
+ struct cs7542_rx_path *out);
+
+int cs7542_rx_path_init(void);
+void cs7542_rx_path_exit(void);
+
+#endif
diff --git a/drivers/net/ethernet/cortina/flowtable/nf_native.c b/drivers/net/ethernet/cortina/flowtable/nf_native.c
new file mode 100644
index 000000000..7363e8f4c
--- /dev/null
+++ b/drivers/net/ethernet/cortina/flowtable/nf_native.c
@@ -0,0 +1,90 @@
+// SPDX-License-Identifier: GPL-2.0
+#ifdef CS_FT_HOST_TEST
+#include "host_shim.h"
+#define be32_to_cpu(v) ntohl(v)
+#define be16_to_cpu(v) ntohs(v)
+#define ether_addr_equal(a, b) (!memcmp(a, b, 6))
+#define ether_addr_copy(d, s) memcpy(d, s, 6)
+#else
+#include <linux/etherdevice.h>
+#endif
+#include "nf_native.h"
+
+static struct cs_fe_tuple to_tuple(const struct cs_ft_tuple *bytes)
+{
+ return (struct cs_fe_tuple){
+ .src = be32_to_cpu(bytes->src),
+ .dst = be32_to_cpu(bytes->dst),
+ .sport = be16_to_cpu(bytes->sport),
+ .dport = be16_to_cpu(bytes->dport),
+ };
+}
+
+static struct cs_fe_link to_link(const struct native_endpoint *endpoint)
+{
+ struct cs_fe_link link = {
+ .gmac = 1,
+ .cpu_port = 0,
+ .switch_port = endpoint->port,
+ .base_voq = 8,
+ .tag_mode = 4,
+ };
+
+ ether_addr_copy(link.base_mac, endpoint->mac);
+ return link;
+}
+
+enum native_reason native_rule_build(const struct cs_ft_nf_facts *facts,
+ const struct native_endpoint *ingress,
+ const struct native_endpoint *egress,
+ const struct cs7542_rx_path *wire,
+ struct cs7542_fe_native_request *request)
+{
+ const struct cs_ft_rewrite *rw = &facts->rewrite;
+ struct cs7542_fe_native_request next = {
+ .abi = facts->ip_proto == CS_FE_L4_TCP ? CS7542_FE_NATIVE_ABI :
+ 1,
+ .size = sizeof(next),
+ };
+ unsigned int reason;
+
+ if ((facts->ip_proto != CS_FE_L4_UDP &&
+ facts->ip_proto != CS_FE_L4_TCP) ||
+ !facts->routed_nf_context || !facts->ttl_decrement_from_context ||
+ facts->extra_match_keys || facts->vlan_offset ||
+ facts->cvlan_offset || !rw->nat_action || !rw->csum_action)
+ return NATIVE_MATCH;
+ if (facts->ingress_ifindex <= 0 || facts->egress_ifindex <= 0 ||
+ ingress->ifindex != (unsigned int)facts->ingress_ifindex ||
+ egress->ifindex != (unsigned int)facts->egress_ifindex ||
+ ingress->port < 1 || ingress->port > 5 || egress->port < 1 ||
+ egress->port > 5)
+ return NATIVE_PATH;
+ if (!wire || !is_valid_ether_addr(wire->source) ||
+ !is_valid_ether_addr(wire->destination))
+ return NATIVE_WIRE;
+ if (!ether_addr_equal(wire->destination, ingress->mac) ||
+ !ether_addr_equal(rw->smac, egress->mac))
+ return NATIVE_MAC;
+ reason = (wire->tag >> 6) & 31;
+ if (wire->tag_len != 4 || (wire->tag >> 14) != 2 ||
+ (wire->tag & 7) != ingress->port || (wire->tag & 8) ||
+ reason == 1 || reason == 2)
+ return NATIVE_TAG;
+
+ next.ingress_ifindex = ingress->ifindex;
+ next.egress_ifindex = egress->ifindex;
+ ether_addr_copy(next.ingress_smac, wire->source);
+ next.rule.native_l4proto = facts->ip_proto;
+ next.rule.original = to_tuple(&rw->before);
+ next.rule.translated = to_tuple(&rw->after);
+ next.rule.ingress = to_link(ingress);
+ next.rule.ingress.rx_qca_meta = wire->tag;
+ next.rule.egress = to_link(egress);
+ ether_addr_copy(next.rule.smac, rw->smac);
+ ether_addr_copy(next.rule.dmac, rw->dmac);
+ next.rule.decrement_ttl = true;
+ *request = next;
+ /* Owner confirmation is the next step. There is no installed entry yet. */
+ return NATIVE_OWNER;
+}
diff --git a/drivers/net/ethernet/cortina/flowtable/nf_native.h b/drivers/net/ethernet/cortina/flowtable/nf_native.h
new file mode 100644
index 000000000..cedd89727
--- /dev/null
+++ b/drivers/net/ethernet/cortina/flowtable/nf_native.h
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef CS7542_NF_NATIVE_H
+#define CS7542_NF_NATIVE_H
+
+#include <linux/cs7542_fe_native.h>
+#include "cs7542_rx_path.h"
+#include "codec/cs_ft_rewrite.h"
+
+enum native_reason {
+ NATIVE_MATCH,
+ NATIVE_PATH,
+ NATIVE_WIRE,
+ NATIVE_MAC,
+ NATIVE_TAG,
+ NATIVE_OWNER,
+ NATIVE_INSTALLED,
+ NATIVE_REASONS
+};
+
+struct native_endpoint {
+ unsigned int ifindex, port;
+ u8 mac[6];
+};
+
+enum native_reason native_rule_build(const struct cs_ft_nf_facts *facts,
+ const struct native_endpoint *ingress,
+ const struct native_endpoint *egress,
+ const struct cs7542_rx_path *wire,
+ struct cs7542_fe_native_request *request);
+
+#endif
--
2.53.0
Re: [PATCH RFC net-next 0/7] net: cortina: CS7542 wired IPv4 hardware flowtable offload
Author: Andrew Lunn
Date: Wed, 25 Nov 2026 21:10
Date: Wed, 25 Nov 2026 21:10
27 lines
10085 bytes
10085 bytes
On Wed, Nov 25, 2026 at 11:50:28PM +0300, Fil Dunsky via B4 Relay wrote: > This RFC connects the CS7542 forwarding engine to nftables hardware > flowtables on Securifi Almond+. It is separate from the CPU Ethernet > bring-up series. The main questions are the flowtable context interface, > QCA four-byte CPU framing, and conservative hardware resource lifetime. This is v2? Please include a version number in the subject line. Please also list what changed relative to v1. > - Is dsa-tag-protocol the right selection interface for this parser need? > - What hardware drain evidence is needed before safely replacing different > result records or clearing the sticky HIT bitmap? Is this a DSA switch? DSA implies that there is a conduit interface which is used to pass frames to a port of the switch. My first look at this driver did not suggest that. It looked like there where a bunch of DMA channels, not a full network interface? I will look at the code again, but you should decide this quickly, the different between a pure switchdev driver and a DSA driver is quite big. Andrew
Re: [PATCH RFC net-next 2/7] net: dsa: add a QCA four-byte CPU tagger
Author: Andrew Lunn
Date: Wed, 25 Nov 2026 21:15
Date: Wed, 25 Nov 2026 21:15
19 lines
9638 bytes
9638 bytes
> #define dsa_lags_foreach_id(_id, _dst) \ > for ((_id) = 1; (_id) <= (_dst)->lags_len; (_id)++) \ > if ((_dst)->lags[(_id) - 1]) > - > #define dsa_lag_foreach_port(_dp, _dst, _lag) \ > list_for_each_entry((_dp), &(_dst)->ports, list) \ > if (dsa_port_offloads_lag((_dp), (_lag))) > - > #define dsa_hsr_foreach_port(_dp, _ds, _hsr) \ > list_for_each_entry((_dp), &(_ds)->dst->ports, list) \ > if ((_dp)->ds == (_ds) && (_dp)->hsr_dev == (_hsr)) > - > static inline struct dsa_lag *dsa_lag_by_id(struct dsa_switch_tree *dst, > unsigned int id) Did you review your own patches? Andrew
Re: [PATCH RFC net-next 0/7] net: cortina: CS7542 wired IPv4 hardware flowtable offload
Author: Fil Dunsky
Date: Wed, 25 Nov 2026 21:55
Date: Wed, 25 Nov 2026 21:55
53 lines
14081 bytes
14081 bytes
Hi Andrew,
This is the first standalone offload RFC, v1. The prerequisite CPU-only
Ethernet series is v2; this RFC does not replace that series. I should
have made the relationship and architecture much clearer. The next
revision will carry an explicit v2 and a changelog.
There are two separate chips on Almond+:
LAN1..4, WAN
|
QCA8337 external switch (existing qca8k DSA driver)
| CPU port 0 | CPU port 6
| RGMII | RGMII
v v
CS7542 GMAC1 (eth1) CS7542 GMAC0 (eth0)
\ /
CS7542 network/forwarding engine and shared DMA
|
CPU
The proposed CS7542 Ethernet driver exposes two full Ethernet netdevs,
eth1 and eth0, despite sharing the DMA engine. Those are the conduits
for the external QCA8337. I am not modelling the CS7542 forwarding
engine itself as another DSA switch.
The CPU receive path is QCA8337 -> RGMII -> CS7542 DMA -> eth1/eth0 ->
DSA tagger -> lanN/wan. Transmit follows the reverse path. Ordinary L2
switching remains the existing QCA8337 driver's responsibility.
The accelerated routed IPv4 path in this RFC is QCA8337 -> GMAC1 ->
CS7542 FE lookup/NAT rewrite -> GMAC1 -> QCA8337 destination port. It
bypasses the CPU DMA path. The frontend accepts standard nftables
hardware flowtable requests through TC_SETUP_FT; it is not a new L2
switchdev implementation.
This explains the QCA changes in patch 5: its optional four-byte CPU
header adds an identifier before the normal QCA metadata. With the
identifier set to 0x8100, the CS7542 FE parser can locate the inner IPv4
header. It cannot do that with the ordinary two-byte QCA header. The
new tagger therefore describes the external switch's wire format, not
a DSA interface inside CS7542. Header length is global to QCA8337, so
both CPU ports must agree, although the present accelerated path uses
only CPU port 0/GMAC1. Management uses MDIO in this mode.
Your patch-2 comment is also correct: the unrelated blank-line changes
in include/net/dsa.h were my mistake. I have removed them locally;
only the two protocol-ID additions remain. No replacement series is
being sent while this discussion is ongoing.
Thanks,
Fil
Re: [PATCH RFC net-next 0/7] net: cortina: CS7542 wired IPv4 hardware flowtable offload
Author: Andrew Lunn
Date: Wed, 25 Nov 2026 22:10
Date: Wed, 25 Nov 2026 22:10
13 lines
9354 bytes
9354 bytes
On Thu, Nov 26, 2026 at 12:55:27AM +0300, Fil Dunsky wrote:
> Hi Andrew,
>
> This is the first standalone offload RFC, v1. The prerequisite CPU-only
> Ethernet series is v2; this RFC does not replace that series.
Ah.
One patchset at a time please. Get the basic Ethernet support working
first. Then start on the next topic.
Andrew
Re: [PATCH RFC net-next 0/7] net: cortina: CS7542 wired IPv4 hardware flowtable offload
Author: Fil Dunsky
Date: Wed, 25 Nov 2026 22:56
Date: Wed, 25 Nov 2026 22:56
10 lines
12085 bytes
12085 bytes
Hi Andrew, Understood. I will focus on getting the basic Ethernet series reviewed and ready first, and hold further offload RFC revisions until that foundation is settled. Further offload development will stay local in the meantime. Thanks, Fil
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads