Skip to contentSkip to conversation

flowtable: consolidate xmit path

PATCH In review Pablo Neira Ayuso 2 participants 9 messages Oct 10, 2025 – Oct 27, 2025

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

cover flowtable: consolidate xmit path

Hi,

This series aims to consolidate direct and neigh xmit paths, the dst_check for stale routes as well as the neighbour lookup are still performed to detect network topology updates.

Patch #1 move the path discovery code to its own file, as more new
topologies can be offloaded in the future. This is a preparation patch.

Patch #2 consolidates the neigh and direct xmit path. This patch relaxes
too a check for neigh xmit in both directions which is needed by the existing basic xfrm offload.

Patch #3 inlines vlan encapsulation to the flowtable xmit path, no
indirection to the vlan device is required.

Patch #4 also inlines pppoe encapsulation which skips passing the
packet to userspace pppd for encapsulation.

Patch #5 remove hw_ifidx which was introduced to make the hardware
offload happy.

I have picked up and rebase original patches for vlan and pppoe posted on the mailing list by wenxu.

As for IPIP support (layer 3 tunnel), this would also allow to inline encapsulation. This needs a mtu check before encapsulation to push back the packet to classic path if fragmentation after encapsulation is needed. This is not included in this series.

This adds an extra lookup by the index for the device for the neigh xmit case from flowtable datapath, I did not collect numbers on this yet.

This is patch nft_flowtable.sh selftests.

Pablo Neira Ayuso (5):
netfilter: flowtable: move path discovery infrastructure to its own file
netfilter: flowtable: consolidate xmit path
netfilter: flowtable: inline vlan encapsulation in xmit path
netfilter: flowtable: inline pppoe encapsulation in xmit path
netfilter: flowtable: remove hw_ifidx

 include/net/netfilter/nf_flow_table.h |   8 +-
 net/netfilter/Makefile                |   1 +
 net/netfilter/nf_flow_table_core.c    |   2 +-
 net/netfilter/nf_flow_table_ip.c      | 149 +++++++++++----
 net/netfilter/nf_flow_table_offload.c |   2 +-
 net/netfilter/nf_flow_table_path.c    | 259 ++++++++++++++++++++++++++
 net/netfilter/nft_flow_offload.c      | 252 -------------------------
 7 files changed, 383 insertions(+), 290 deletions(-)
 create mode 100644 net/netfilter/nf_flow_table_path.c
Signature · 1 line
2.30.2
author maintainer
#1 lore raw

PATCH 1/5 netfilter: flowtable: move path discovery infrastructure to its own file

This file contains the path discovery that is run from the forward chain for the packet offloading the flow into the flowtable. This consists of a series of calls to dev_fill_forward_path() for each device stack.

More topologies may be supported in the future, so move this code to its own file to separate it from the nftables flow_offload expression.

---
 include/net/netfilter/nf_flow_table.h |   6 +
 net/netfilter/Makefile                |   1 +
 net/netfilter/nf_flow_table_path.c    | 267 ++++++++++++++++++++++++++
 net/netfilter/nft_flow_offload.c      | 252 ------------------------
 4 files changed, 274 insertions(+), 252 deletions(-)
 create mode 100644 net/netfilter/nf_flow_table_path.c
include/net/netfilter/nf_flow_table.h +6 -0
index c003cd194fa2..e9f72d2558e9 100644
@@ -222,6 +222,12 @@ struct nf_flow_route {
 struct flow_offload *flow_offload_alloc(struct nf_conn *ct);
 void flow_offload_free(struct flow_offload *flow);
 
+struct nft_flowtable;
+struct nft_pktinfo;
+int nft_flow_route(const struct nft_pktinfo *pkt, const struct nf_conn *ct,
+		   struct nf_flow_route *route, enum ip_conntrack_dir dir,
+		   struct nft_flowtable *ft);
+
 static inline int
 nf_flow_table_offload_add_cb(struct nf_flowtable *flow_table,
 			     flow_setup_cb_t *cb, void *cb_priv)
net/netfilter/Makefile +1 -0
index e43e20f529f8..6bfc250e474f 100644
@@ -141,6 +141,7 @@ obj-$(CONFIG_NFT_FWD_NETDEV)	+= nft_fwd_netdev.o
 # flow table infrastructure
 obj-$(CONFIG_NF_FLOW_TABLE)	+= nf_flow_table.o
 nf_flow_table-objs		:= nf_flow_table_core.o nf_flow_table_ip.o \
+				   nf_flow_table_path.o \
 				   nf_flow_table_offload.o nf_flow_table_xdp.o
 nf_flow_table-$(CONFIG_NF_FLOW_TABLE_PROCFS) += nf_flow_table_procfs.o
 ifeq ($(CONFIG_NF_FLOW_TABLE),m)
net/netfilter/nf_flow_table_path.c new file +267 -0

267 lines not on this page · raw message

net/netfilter/nft_flow_offload.c +0 -252

258 lines not on this page · raw message

Signature · 1 line
2.30.2
maintainer
#2 lore raw

Re: [PATCH nf-next 1/5] netfilter: flowtable: move path discovery infrastructure to its own file

net/netfilter/nf_flow_table_path.c line 1

Quoted 10 lines · Pablo Neira Ayuso

This file contains the path discovery that is run from the forward chain for the packet offloading the flow into the flowtable. This consists of a series of calls to dev_fill_forward_path() for each device stack.

index 000000000000..159aa5c8da60

net/netfilter/nf_flow_table_path.c new file +1 -0
@@ -0,0 +1,267 @@
+// SPDX-License-Identifier: GPL-2.0-only

Wouly you mind adding above explanation here as a comment?

author maintainer
#3 lore raw

PATCH 2/5 netfilter: flowtable: consolidate xmit path

In reply to Pablo Neira Ayuso

Use dev_queue_xmit() for the XMIT_NEIGH case. Store the interface index of the real device behind the vlan/pppoe device, this introduces an extra lookup for the real device in the xmit path because rt->dst.dev provides the vlan/pppoe device.

XMIT_NEIGH now looks more similar to XMIT_DIRECT but the check for stale dst and the neighbour lookup still remain in place which is convenient to deal with network topology changes.

Note that nft_flow_route() needs to relax the check for _XMIT_NEIGH so the existing basic xfrm offload (which only works in one direction) does not break.

---
 include/net/netfilter/nf_flow_table.h |  1 +
 net/netfilter/nf_flow_table_core.c    |  1 +
 net/netfilter/nf_flow_table_ip.c      | 87 ++++++++++++++++-----------
 net/netfilter/nf_flow_table_path.c    |  7 +--
 4 files changed, 57 insertions(+), 39 deletions(-)
include/net/netfilter/nf_flow_table.h +1 -0
index e9f72d2558e9..efede742106c 100644
@@ -140,6 +140,7 @@ struct flow_offload_tuple {
 	u16				mtu;
 	union {
 		struct {
+			u32		ifidx;
 			struct dst_entry *dst_cache;
 			u32		dst_cookie;
 		};
net/netfilter/nf_flow_table_core.c +1 -0
index 9441ac3d8c1a..98d7b3708602 100644
@@ -132,6 +132,7 @@ static int flow_offload_fill_route(struct flow_offload *flow,
 		break;
 	case FLOW_OFFLOAD_XMIT_XFRM:
 	case FLOW_OFFLOAD_XMIT_NEIGH:
+		flow_tuple->ifidx = route->tuple[dir].out.ifindex;
 		flow_tuple->dst_cache = dst;
 		flow_tuple->dst_cookie = flow_offload_dst_cookie(flow_tuple);
 		break;
net/netfilter/nf_flow_table_ip.c +52 -35

139 lines not on this page · raw message

net/netfilter/nf_flow_table_path.c +3 -4
index 159aa5c8da60..15c042cab9fb 100644
@@ -204,11 +204,11 @@ static void nft_dev_forward_path(struct nf_flow_route *route,
 	}
 	route->tuple[!dir].in.num_encaps = info.num_encaps;
 	route->tuple[!dir].in.ingress_vlans = info.ingress_vlans;
+	route->tuple[dir].out.ifindex = info.outdev->ifindex;
 
 	if (info.xmit_type == FLOW_OFFLOAD_XMIT_DIRECT) {
 		memcpy(route->tuple[dir].out.h_source, info.h_source, ETH_ALEN);
 		memcpy(route->tuple[dir].out.h_dest, info.h_dest, ETH_ALEN);
-		route->tuple[dir].out.ifindex = info.outdev->ifindex;
 		route->tuple[dir].out.hw_ifindex = info.hw_outdev->ifindex;
 		route->tuple[dir].xmit_type = info.xmit_type;
 	}
@@ -256,11 +256,10 @@ int nft_flow_route(const struct nft_pktinfo *pkt, const struct nf_conn *ct,
 	nft_default_forward_path(route, this_dst, dir);
 	nft_default_forward_path(route, other_dst, !dir);
 
-	if (route->tuple[dir].xmit_type	== FLOW_OFFLOAD_XMIT_NEIGH &&
-	    route->tuple[!dir].xmit_type == FLOW_OFFLOAD_XMIT_NEIGH) {
+	if (route->tuple[dir].xmit_type	== FLOW_OFFLOAD_XMIT_NEIGH)
 		nft_dev_forward_path(route, ct, dir, ft);
+	if (route->tuple[!dir].xmit_type == FLOW_OFFLOAD_XMIT_NEIGH)
 		nft_dev_forward_path(route, ct, !dir, ft);
-	}
 
 	return 0;
 }
Signature · 1 line
2.30.2
maintainer
#4 lore raw

Re: [PATCH nf-next 2/5] netfilter: flowtable: consolidate xmit path

Quoted 4 lines · Pablo Neira Ayuso

Use dev_queue_xmit() for the XMIT_NEIGH case. Store the interface index of the real device behind the vlan/pppoe device, this introduces an extra lookup for the real device in the xmit path because rt->dst.dev provides the vlan/pppoe device.

Will this scale? netdev_by_index only has a fixed table of 256 slots, so with 8k vlans or so this will have a 30-ish netdev list walk.

[ EDIT: I see now that nf_flow_queue_xmit() already does that.
So I guess its either not an issue or not yet and it can be optimized later. So disregard this ]

net/netfilter/nf_flow_table_ip.c nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb, · line 456

Quoted 6 lines
net/netfilter/nf_flow_table_ip.c +1 -3
 	case FLOW_OFFLOAD_XMIT_NEIGH:
 		rt = dst_rtable(tuplehash->tuple.dst_cache);
-		outdev = rt->dst.dev;
-		skb->dev = outdev;
-		nexthop = rt_nexthop(rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);
+		xmit.outdev = dev_get_by_index_rcu(state->net, tuplehash->tuple.ifidx);

Why do this if we already have dst_cache?

The above explanation (rt->dst.dev could be a tunnel device, whereas the latter fetches phyical / lowest device) from the commit message makes that clear; but I think a short comment would help.

author maintainer
#5 lore raw

Re: [PATCH nf-next 2/5] netfilter: flowtable: consolidate xmit path

Quoted 6 lines · Florian Westphal
Quoted 4 lines · Pablo Neira Ayuso

Use dev_queue_xmit() for the XMIT_NEIGH case. Store the interface index of the real device behind the vlan/pppoe device, this introduces an extra lookup for the real device in the xmit path because rt->dst.dev provides the vlan/pppoe device.

Quoted 6 lines

Will this scale? netdev_by_index only has a fixed table of 256 slots, so with 8k vlans or so this will have a 30-ish netdev list walk.

[ EDIT: I see now that nf_flow_queue_xmit() already does that.
So I guess its either not an issue or not yet and it can be optimized later. So disregard this ]

Yes, that will need a look closer or later.

Quoted 8 lines

net/netfilter/nf_flow_table_ip.c nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb, · line 456

Quoted 6 lines
net/netfilter/nf_flow_table_ip.c +1 -3
 	case FLOW_OFFLOAD_XMIT_NEIGH:
 		rt = dst_rtable(tuplehash->tuple.dst_cache);
-		outdev = rt->dst.dev;
-		skb->dev = outdev;
-		nexthop = rt_nexthop(rt, flow->tuplehash[!dir].tuple.src_v4.s_addr);
+		xmit.outdev = dev_get_by_index_rcu(state->net, tuplehash->tuple.ifidx);

Why do this if we already have dst_cache?

This is to skip one level of indirection, ie. dst_cache points to the vlan device, not the real device. The idea is that the flowtable gains control on the xmit path so the flowtable pushes l2/l3 headers and send the packets directly to the "real" netdevice.

Quoted 3 lines

The above explanation (rt->dst.dev could be a tunnel device, whereas the latter fetches phyical / lowest device) from the commit message makes that clear; but I think a short comment would help.

Yes, I can add a comment on this.

Thanks.

author maintainer
#6 lore raw

PATCH 3/5 netfilter: flowtable: inline vlan encapsulation in xmit path

In reply to Pablo Neira Ayuso

Push the vlan header from the flowtable xmit path, instead of passing the packet to the vlan device.

This is based on a patch originally written by wenxu.

---
 net/netfilter/nf_flow_table_ip.c   | 20 ++++++++++++++++++++
 net/netfilter/nf_flow_table_path.c |  7 ++++---
 2 files changed, 24 insertions(+), 3 deletions(-)
net/netfilter/nf_flow_table_ip.c +20 -0
index 8b74fb34998e..2d11c46a925c 100644
@@ -413,6 +413,23 @@ static int nf_flow_offload_forward(struct nf_flowtable_ctx *ctx,
 	return 1;
 }
 
+static int nf_flow_encap_push(struct sk_buff *skb, struct flow_offload_tuple *tuple)
+{
+	int i;
+
+	for (i = 0; i < tuple->encap_num; i++) {
+		switch (tuple->encap[i].proto) {
+		case htons(ETH_P_8021Q):
+		case htons(ETH_P_8021AD):
+			if (skb_vlan_push(skb, tuple->encap[i].proto, tuple->encap[i].id) < 0)
+				return -1;
+			break;
+		}
+	}
+
+	return 0;
+}
+
 unsigned int
 nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
 			const struct nf_hook_state *state)
@@ -450,6 +467,9 @@ nf_flow_offload_ip_hook(void *priv, struct sk_buff *skb,
 	dir = tuplehash->tuple.dir;
 	flow = container_of(tuplehash, struct flow_offload, tuplehash[dir]);
 
+	if (nf_flow_encap_push(skb, &flow->tuplehash[!dir].tuple) < 0)
+		return NF_DROP;
+
 	switch (tuplehash->tuple.xmit_type) {
 	case FLOW_OFFLOAD_XMIT_NEIGH:
 		rt = dst_rtable(tuplehash->tuple.dst_cache);
net/netfilter/nf_flow_table_path.c +4 -3
index 15c042cab9fb..1cb04c3e6dde 100644
@@ -119,13 +119,14 @@ static void nft_dev_path_info(const struct net_device_path_stack *stack,
 				info->indev = NULL;
 				break;
 			}
-			if (!info->outdev)
-				info->outdev = path->dev;
 			info->encap[info->num_encaps].id = path->encap.id;
 			info->encap[info->num_encaps].proto = path->encap.proto;
 			info->num_encaps++;
-			if (path->type == DEV_PATH_PPPOE)
+			if (path->type == DEV_PATH_PPPOE) {
+				if (!info->outdev)
+					info->outdev = path->dev;
 				memcpy(info->h_dest, path->encap.h_dest, ETH_ALEN);
+			}
 			break;
 		case DEV_PATH_BRIDGE:
 			if (is_zero_ether_addr(info->h_source))
Signature · 1 line
2.30.2
author maintainer
#7 lore raw

PATCH 4/5 netfilter: flowtable: inline pppoe encapsulation in xmit path

In reply to Pablo Neira Ayuso

Push the pppoe header from the flowtable xmit path, instead of passing the packet to the pppoe device which delivers the packet to the userspace pppd daemon for encapsulation.

This is based on a patch originally written by wenxu.

---
 net/netfilter/nf_flow_table_ip.c   | 42 ++++++++++++++++++++++++++++++
 net/netfilter/nf_flow_table_path.c |  9 ++-----
 2 files changed, 44 insertions(+), 7 deletions(-)
net/netfilter/nf_flow_table_ip.c +42 -0
index 2d11c46a925c..be21600cff53 100644
@@ -413,6 +413,44 @@ static int nf_flow_offload_forward(struct nf_flowtable_ctx *ctx,
 	return 1;
 }
 
+static int nf_flow_pppoe_push(struct sk_buff *skb, u16 id)
+{
+	int data_len = skb->len + sizeof(__be16);
+	struct ppp_hdr {
+		struct pppoe_hdr hdr;
+		__be16 proto;
+	} *ph;
+	__be16 proto;
+
+	if (skb_cow_head(skb, PPPOE_SES_HLEN))
+		return -1;
+
+	switch (skb->protocol) {
+	case htons(ETH_P_IP):
+		proto = htons(PPP_IP);
+		break;
+	case htons(ETH_P_IPV6):
+		proto = htons(PPP_IPV6);
+		break;
+	default:
+		return -1;
+	}
+
+	__skb_push(skb, PPPOE_SES_HLEN);
+	skb_reset_network_header(skb);
+
+	ph = (struct ppp_hdr *)(skb->data);
+	ph->hdr.ver	= 1;
+	ph->hdr.type	= 1;
+	ph->hdr.code	= 0;
+	ph->hdr.sid	= htons(id);
+	ph->hdr.length	= htons(data_len);
+	ph->proto	= proto;
+	skb->protocol	= htons(ETH_P_PPP_SES);
+
+	return 0;
+}
+
 static int nf_flow_encap_push(struct sk_buff *skb, struct flow_offload_tuple *tuple)
 {
 	int i;
@@ -424,6 +462,10 @@ static int nf_flow_encap_push(struct sk_buff *skb, struct flow_offload_tuple *tu
 			if (skb_vlan_push(skb, tuple->encap[i].proto, tuple->encap[i].id) < 0)
 				return -1;
 			break;
+		case htons(ETH_P_PPP_SES):
+			if (nf_flow_pppoe_push(skb, tuple->encap[i].id) < 0)
+				return -1;
+			break;
 		}
 	}
 
net/netfilter/nf_flow_table_path.c +2 -7
index 1cb04c3e6dde..7ba6a0c4e5d8 100644
@@ -122,11 +122,8 @@ static void nft_dev_path_info(const struct net_device_path_stack *stack,
 			info->encap[info->num_encaps].id = path->encap.id;
 			info->encap[info->num_encaps].proto = path->encap.proto;
 			info->num_encaps++;
-			if (path->type == DEV_PATH_PPPOE) {
-				if (!info->outdev)
-					info->outdev = path->dev;
+			if (path->type == DEV_PATH_PPPOE)
 				memcpy(info->h_dest, path->encap.h_dest, ETH_ALEN);
-			}
 			break;
 		case DEV_PATH_BRIDGE:
 			if (is_zero_ether_addr(info->h_source))
@@ -154,9 +151,7 @@ static void nft_dev_path_info(const struct net_device_path_stack *stack,
 			break;
 		}
 	}
-	if (!info->outdev)
-		info->outdev = info->indev;
-
+	info->outdev = info->indev;
 	info->hw_outdev = info->indev;
 
 	if (nf_flowtable_hw_offload(flowtable) &&
Signature · 1 line
2.30.2
author maintainer
#8 lore raw

PATCH 5/5 netfilter: flowtable: remove hw_ifidx

In reply to Pablo Neira Ayuso

hw_ifidx was originally introduced to store the real netdevice as a requirement for the hardware offload support in:

73f97025a972 ("netfilter: nft_flow_offload: use direct xmit if hardware offload is enabled")

Since ("netfilter: flowtable: consolidate xmit path"), ifidx and hw_ifidx points to the real device in the xmit path, remove it.

---
 include/net/netfilter/nf_flow_table.h | 1 -
 net/netfilter/nf_flow_table_core.c    | 1 -
 net/netfilter/nf_flow_table_offload.c | 2 +-
 net/netfilter/nf_flow_table_path.c    | 3 ---
 4 files changed, 1 insertion(+), 6 deletions(-)
include/net/netfilter/nf_flow_table.h +0 -1
index efede742106c..89cfe7228398 100644
@@ -146,7 +146,6 @@ struct flow_offload_tuple {
 		};
 		struct {
 			u32		ifidx;
-			u32		hw_ifidx;
 			u8		h_source[ETH_ALEN];
 			u8		h_dest[ETH_ALEN];
 		} out;
net/netfilter/nf_flow_table_core.c +0 -1
index 98d7b3708602..6c6a5165f993 100644
@@ -127,7 +127,6 @@ static int flow_offload_fill_route(struct flow_offload *flow,
 		memcpy(flow_tuple->out.h_source, route->tuple[dir].out.h_source,
 		       ETH_ALEN);
 		flow_tuple->out.ifidx = route->tuple[dir].out.ifindex;
-		flow_tuple->out.hw_ifidx = route->tuple[dir].out.hw_ifindex;
 		dst_release(dst);
 		break;
 	case FLOW_OFFLOAD_XMIT_XFRM:
net/netfilter/nf_flow_table_offload.c +1 -1
index e06bc36f49fe..d8f7bfd60ac6 100644
@@ -555,7 +555,7 @@ static void flow_offload_redirect(struct net *net,
 	switch (this_tuple->xmit_type) {
 	case FLOW_OFFLOAD_XMIT_DIRECT:
 		this_tuple = &flow->tuplehash[dir].tuple;
-		ifindex = this_tuple->out.hw_ifidx;
+		ifindex = this_tuple->out.ifidx;
 		break;
 	case FLOW_OFFLOAD_XMIT_NEIGH:
 		other_tuple = &flow->tuplehash[!dir].tuple;
net/netfilter/nf_flow_table_path.c +0 -3
index 7ba6a0c4e5d8..50b2b7d0c579 100644
@@ -75,7 +75,6 @@ static int nft_dev_fill_forward_path(const struct nf_flow_route *route,
 struct nft_forward_info {
 	const struct net_device *indev;
 	const struct net_device *outdev;
-	const struct net_device *hw_outdev;
 	struct id {
 		__u16	id;
 		__be16	proto;
@@ -152,7 +151,6 @@ static void nft_dev_path_info(const struct net_device_path_stack *stack,
 		}
 	}
 	info->outdev = info->indev;
-	info->hw_outdev = info->indev;
 
 	if (nf_flowtable_hw_offload(flowtable) &&
 	    nft_is_valid_ether_device(info->indev))
@@ -205,7 +203,6 @@ static void nft_dev_forward_path(struct nf_flow_route *route,
 	if (info.xmit_type == FLOW_OFFLOAD_XMIT_DIRECT) {
 		memcpy(route->tuple[dir].out.h_source, info.h_source, ETH_ALEN);
 		memcpy(route->tuple[dir].out.h_dest, info.h_dest, ETH_ALEN);
-		route->tuple[dir].out.hw_ifindex = info.hw_outdev->ifindex;
 		route->tuple[dir].xmit_type = info.xmit_type;
 	}
 }
Signature · 1 line
2.30.2