From: Florian Fainelli <florian.fainelli@broadcom.com>
To: netdev@vger.kernel.org
Cc: "Florian Fainelli" <florian.fainelli@broadcom.com>,
"Rafał Miłecki" <rafal@milecki.pl>,
"Broadcom internal kernel review list"
<bcm-kernel-feedback-list@broadcom.com>,
"Andrew Lunn" <andrew+netdev@lunn.ch>,
"David S. Miller" <davem@davemloft.net>,
"Eric Dumazet" <edumazet@kernel.org>,
"Jakub Kicinski" <kuba@kernel.org>,
"Paolo Abeni" <pabeni@redhat.com>,
"Rob Herring" <robh@kernel.org>,
"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
"Conor Dooley" <conor+dt@kernel.org>,
"Simon Horman" <horms@kernel.org>,
"Jonathan Corbet" <corbet@lwn.net>,
"Jonas Gorski" <jonas.gorski@gmail.com>,
"Vladimir Oltean" <olteanv@gmail.com>,
"Pablo Neira Ayuso" <pablo@netfilter.org>,
"Florian Westphal" <fw@strlen.de>, "Phil Sutter" <phil@nwl.cc>,
devicetree@vger.kernel.org (open list:OPEN FIRMWARE AND
FLATTENED DEVICE TREE BINDINGS),
linux-kernel@vger.kernel.org (open list),
linux-doc@vger.kernel.org (open list:DOCUMENTATION),
netfilter-devel@vger.kernel.org (open list:NETFILTER),
coreteam@netfilter.org (open list:NETFILTER)
Subject: [PATCH net-next 6/9] net: flow_offload: allow drivers to validate and invalidate cached paths
Date: Mon, 16 Nov 2026 14:25:16 -0800 [thread overview]
Message-ID: <20261116222519.1416501-7-florian.fainelli@broadcom.com> (raw)
In-Reply-To: <20261116222519.1416501-1-florian.fainelli@broadcom.com>
A classifier does not necessarily contain the mark, DSCP and output-
interface inputs used to select its forwarding path. A partial driver
FIB lookup cannot establish that a cached software route is stale.
Provide optional owner validation and invalidation callbacks. Validation
runs under RCU without sleeping and distinguishes a proven stale path
from unavailable context. Retain direct-path dst references for hardware
flowtables and validate the actual cached routes, cookies and direct
Ethernet neighbours in both directions on demand.
Mark references retained only for driver validation. They must not opt
plain direct flows into packet-path or GC route checks after unrelated
IPv4 generation changes. Keep the existing checks for direct tunnel,
neighbour and XFRM routes. The explicit owner callback bypasses that
policy and validates the retained dst itself. Other hardware users and
tables without a validating driver keep their existing automatic-check
behaviour. Software-only direct flows retain their previous reference
policy. Release retained references with the flow.
Netfilter implements invalidation by marking the owning connection for
teardown. Owners keep cookies valid through an RCU grace period after
DESTROY; drivers stop issuing notifications when their rules are
destroyed.
Signed-off-by: Florian Fainelli <florian.fainelli@broadcom.com>
---
include/net/flow_offload.h | 12 ++++++
include/net/netfilter/nf_flow_table.h | 8 ++--
net/netfilter/nf_flow_table_core.c | 12 +++---
net/netfilter/nf_flow_table_offload.c | 58 +++++++++++++++++++++++++++
net/netfilter/nft_flow_offload.c | 6 ++-
5 files changed, 87 insertions(+), 9 deletions(-)
diff --git a/include/net/flow_offload.h b/include/net/flow_offload.h
index 70a02ee14308..09301a924014 100644
--- a/include/net/flow_offload.h
+++ b/include/net/flow_offload.h
@@ -697,6 +697,18 @@ 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.
+ */
+ int (*validate)(unsigned long cookie);
struct flow_stats stats;
u32 classid;
};
diff --git a/include/net/netfilter/nf_flow_table.h b/include/net/netfilter/nf_flow_table.h
index 5b22d31cb140..99cb087ff0b2 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;
@@ -305,7 +306,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 +314,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/net/netfilter/nf_flow_table_core.c b/net/netfilter/nf_flow_table_core.c
index 4c82097cd50b..b16a336101af 100644
--- a/net/netfilter/nf_flow_table_core.c
+++ b/net/netfilter/nf_flow_table_core.c
@@ -96,7 +96,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 +127,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,10 +164,10 @@ 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;
}
EXPORT_SYMBOL_GPL(flow_offload_route_init);
diff --git a/net/netfilter/nf_flow_table_offload.c b/net/netfilter/nf_flow_table_offload.c
index 4365859220e6..1f3adf70f598 100644
--- a/net/netfilter/nf_flow_table_offload.c
+++ b/net/netfilter/nf_flow_table_offload.c
@@ -885,6 +885,62 @@ 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));
+}
+
+static int nf_flow_offload_validate(unsigned long cookie)
+{
+ struct flow_offload *flow = nf_flow_offload_cookie_flow(cookie);
+ unsigned int dir;
+
+ 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 neighbour *neigh;
+ u8 mac[ETH_ALEN];
+ bool valid;
+
+ 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.
+ */
+ if (dst->dev->ifindex != tuple->out.ifidx)
+ return -EOPNOTSUPP;
+ neigh = dst_neigh_lookup(dst, &other->src_v4);
+ if (!neigh)
+ return -ESTALE;
+ neigh_ha_snapshot(mac, neigh, dst->dev);
+ valid = (READ_ONCE(neigh->nud_state) & NUD_VALID) &&
+ ether_addr_equal(mac, tuple->out.h_dest);
+ neigh_release(neigh);
+ if (!valid)
+ return -ESTALE;
+ }
+ return 0;
+}
+
static void nf_flow_offload_init(struct flow_cls_offload *cls_flow,
__be16 proto, int priority,
enum flow_cls_command cmd,
@@ -896,6 +952,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/nft_flow_offload.c b/net/netfilter/nft_flow_offload.c
index d3c5651dd699..05207ca808ee 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);
--
2.53.0
next prev parent reply other threads:[~2026-11-16 22:26 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 22:25 [PATCH net-next 0/9] net: bgmac: add Northstar IPv4 TCP/UDP NAT flowtable offload Florian Fainelli
2026-11-16 22:25 ` [PATCH net-next 1/9] net: ethernet: bgmac: validate RX length against the DMA packet capacity Florian Fainelli
2026-11-16 22:25 ` [PATCH net-next 2/9] netfilter: flowtable: reject non-host IPv4 packets before lookup Florian Fainelli
2026-11-16 22:25 ` [PATCH net-next 3/9] netfilter: flowtable: retain offload ownership until deletion completes Florian Fainelli
2026-11-16 22:25 ` [PATCH net-next 4/9] dt-bindings: net: describe the Northstar flow accelerator Florian Fainelli
2026-11-16 22:25 ` [PATCH net-next 5/9] net: dsa: add Broadcom FA metadata and IPv4 exception paths Florian Fainelli
2026-11-16 22:25 ` Florian Fainelli [this message]
2026-11-16 22:25 ` [PATCH net-next 7/9] net: bgmac: add Northstar IPv4 TCP/UDP NAT flowtable offload Florian Fainelli
2026-11-16 22:25 ` [PATCH net-next 8/9] net: add KUnit coverage for FA and flowtable path validation Florian Fainelli
2026-11-16 22:25 ` [PATCH net-next 9/9] docs: net: document Northstar FA setup and NAT qualification Florian Fainelli
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261116222519.1416501-7-florian.fainelli@broadcom.com \
--to=florian.fainelli@broadcom.com \
--cc=andrew+netdev@lunn.ch \
--cc=bcm-kernel-feedback-list@broadcom.com \
--cc=conor+dt@kernel.org \
--cc=corbet@lwn.net \
--cc=coreteam@netfilter.org \
--cc=davem@davemloft.net \
--cc=devicetree@vger.kernel.org \
--cc=edumazet@kernel.org \
--cc=fw@strlen.de \
--cc=horms@kernel.org \
--cc=jonas.gorski@gmail.com \
--cc=krzk+dt@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=netfilter-devel@vger.kernel.org \
--cc=olteanv@gmail.com \
--cc=pabeni@redhat.com \
--cc=pablo@netfilter.org \
--cc=phil@nwl.cc \
--cc=rafal@milecki.pl \
--cc=robh@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox