From 8588d4508a85c8307440d8aea02bd6ecdcf88240 Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 15:57:39 -0800 Subject: [PATCH 01/22] net: add a flowtable fast-path page Explain how established forwarded flows are added to a flowtable, how the fast path handles a hit, how flows time out or are torn down, and hardware offload. Notes where the in-tree documentation no longer matches the source. --- docs/net/README.md | 1 + docs/net/flowtable.md | 115 ++++++++++++++++++++++++++++++++++++++++++ mkdocs.yml | 1 + 3 files changed, 117 insertions(+) create mode 100644 docs/net/flowtable.md diff --git a/docs/net/README.md b/docs/net/README.md index 3a9a5014..1120dd9e 100644 --- a/docs/net/README.md +++ b/docs/net/README.md @@ -64,6 +64,7 @@ Every packet is carried by an [sk_buff](sk-buff.md) — start there if you read | [Netfilter](netfilter.md) | Hooks, tables, chains | | [nftables vs iptables](nftables-iptables.md) | Why the replacement happened | | [Connection Tracking](conntrack.md) | conntrack entries, NAT, helpers | +| [Flowtable](flowtable.md) | The fast path that lets established flows skip the hook path | | [Traffic Control (tc/qdisc)](tc-qdisc.md) | Queuing disciplines, shaping, fq_codel | ### High performance & offload diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md new file mode 100644 index 00000000..8307a838 --- /dev/null +++ b/docs/net/flowtable.md @@ -0,0 +1,115 @@ +# Flowtable: The Fast Path for Established Flows + +> How the kernel lets established forwarded connections skip most of the netfilter hook path + +## What it does + +A router or NAT gateway sends every forwarded packet through the same hooks: prerouting, routing, forward, postrouting, plus conntrack and NAT. Flowtable skips that work for packets of a flow it already knows. + +The kernel's [flowtable documentation](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst) describes it like this: once the first packets of a flow have gone through the normal forwarding path, a rule can add the flow to a table. The table is consulted at the ingress hook. A packet that matches an entry is rewritten and sent straight out, so it never reaches the hooks after ingress. A packet that doesn't match follows the classic path. + +``` + ingress hook + ┌─────────────────────┐ + │ flowtable lookup │ + └─────────────────────┘ + │ hit │ miss or fall back + ▼ ▼ +┌───────────────────┐ ┌─────────────────────┐ +│ rewrite headers, │ │ classic path: │ +│ send out the │ │ prerouting, forward,│ +│ egress device │ │ postrouting │ +└───────────────────┘ └─────────────────────┘ +``` + +Only flows you select are added. Nothing is offloaded unless a rule says so. + +## Turning it on + +The kernel documentation's example is a flowtable on two devices and one rule in the forward chain: + +``` +table inet x { + flowtable f { + hook ingress priority 0; devices = { eth0, eth1 }; + } + chain y { + type filter hook forward priority 0; policy accept; + ip protocol tcp flow add @f + counter packets 0 bytes 0 + } +} +``` + +`flow add @f` is the nftables statement that calls `nft_flow_offload_eval()` in `net/netfilter/nft_flow_offload.c`. Once a flow is in the table, the `counter` rule stops counting its packets, because they no longer reach the forward chain. Give the flowtable a lower priority number than any nftables ingress chain on the same device so that it runs first. + +## How a flow gets in + +The `flow add` statement runs on a packet in the forward path and checks, in `nft_flow_offload_eval()`, whether the flow is a candidate: + +- **Protocol.** TCP, UDP, or GRE. For TCP the conntrack entry must be established and the packet must not carry FIN or RST. GRE is accepted only without NAT and without a GRE key. +- **No helper and no sequence adjustment.** A flow with a conntrack helper attached, with `IPS_SEQ_ADJUST` set, or with `IPS_NAT_CLASH` set is skipped. Packets that carry a security path (IPsec) or IPv4 options are skipped too (`nft_flow_offload_skip()`). +- **Confirmed.** The conntrack entry must already be confirmed. +- **Only once.** `test_and_set_bit(IPS_OFFLOAD_BIT, ...)` makes the first caller win, so two packets can't add the same flow twice. + +If all of that holds, the kernel looks up the route in both directions, allocates a `struct flow_offload` that holds a reference to the conntrack entry, copies the SNAT and DNAT flags from the conntrack status, and inserts the entry. The entry has two tuples, one per direction, in one resizable hash table. The comment in `flow_offload_add()` notes that garbage collection walks only the original-direction entries, so that one is inserted last. + +For TCP, `flow_offload_ct_tcp()` also sets the "be liberal" flag on the conntrack entry. The comment there gives the reason: conntrack will not see all packets any more, so it can't validate the TCP window. + +## What a hit does + +The IPv4 hook is `nf_flow_offload_ip_hook()` in `net/netfilter/nf_flow_table_ip.c`. For each packet it: + +1. Looks up the packet's tuple in the table. A miss returns `NF_ACCEPT` and the packet carries on down the classic path. +2. Checks the packet against the MTU recorded for the flow. A packet that is too big returns to the classic path, so the usual ICMP error can be generated. +3. Checks TCP flags (see below). +4. Strips any VLAN or PPPoE header, applies the stored NAT rewrite, decrements the TTL, and updates the conntrack counters if the flowtable has `counter`. +5. Builds the Ethernet header itself and calls `dev_queue_xmit()` on the egress device. In neighbour mode the destination MAC comes from the neighbour entry for the next hop. In direct mode the kernel uses MAC addresses stored in the entry. The path code picks direct mode for PPPoE and bridge paths, and when hardware offload is on for an Ethernet device. + +The IPv6 hook, `nf_flow_offload_ipv6_hook()`, does the same. + +## How a flow leaves + +A flow leaves the table in one of these ways, all handled by a garbage-collection worker that runs once a second (`nf_flow_offload_work_gc()`): + +- **It times out.** Each entry gets a deadline when it is added, and every hit pushes the deadline out. The default for TCP and UDP is 30 seconds (`offload_timeout` in `nf_conntrack_proto_tcp.c` and `nf_conntrack_proto_udp.c`). The refresh happens at most once a second per flow. +- **Conntrack entry is dying, or the cached route is no longer valid.** The collector tears the flow down. +- **TCP FIN or RST.** The hook marks the flow `NF_FLOW_CLOSING`. The packet itself still takes the fast path. The flow stays in the software table, but a hardware entry is removed early. A later SYN on a closing flow tears it down, sets the conntrack state to close, and sends that packet to the classic path so conntrack can handle the reconnect. + +While a flow is in the table, conntrack doesn't see its packets, so its timeout would run out. `nf_flow_table_extend_ct_timeout()` pushes the conntrack timeout forward. Its comment explains the choice of a large fixed value: it saves a check of the offload bit in the packet path. + +When a flow is torn down, `flow_offload_fixup_ct()` hands it back to conntrack. For TCP it restores the last state conntrack recorded (or close, if the flow was closing), clears the window-tracking data that went stale because conntrack never saw the packets, and refreshes the conntrack timeout. + +## Hardware offload + +If the device driver supports it, the flowtable can push entries to the NIC or switch. Add `flags offload;` to the flowtable definition. The kernel documentation says a workqueue adds the flows to hardware, so a few packets can still go through the software path until it runs. + +In the code, `net/netfilter/nf_flow_table_offload.c` turns each entry into a flow rule, which is a set of match keys and actions such as header rewrites, and hands it to the driver as `FLOW_CLS_REPLACE`, `FLOW_CLS_DESTROY` or `FLOW_CLS_STATS`. The `NF_FLOW_PENDING` bit marks an entry whose work is queued, and the garbage collector leaves it alone until the work finishes. Pablo Neira Ayuso added this in [`c29f74e0df7a`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c29f74e0df7a02b8303bcdce93a7c0132d62577a) (November 2019). + +`conntrack -L` shows `[OFFLOAD]` for flows in the software table and `[HW_OFFLOAD]` for flows in hardware. + +## Limits + +- Fragments, and packets that exceed the MTU, go through the classic path. The kernel documentation gives this as the reason: the transport header is missing for fragments, so no lookup is possible. +- The table is a cache. The kernel documentation warns that an entry can go stale if the destination MAC address or the egress device changes. It names two risky setups: software mode combined with bridging and IP forwarding, and hardware offload. +- The classic hooks after ingress don't see offloaded packets. A rule there, such as a counter or a log, won't match them. + +## Where the in-tree documentation is behind the code + +The kernel documentation was written for the original design. Two statements in it no longer match the source in the tree I read (v7.4-rc3-803): + +- It says TCP RST and FIN packets are passed up to the classic path. Since [`fdbaf5163331`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fdbaf5163331342e90a2c29b87629021f4c15f0c) ("netfilter: flowtable: add CLOSING state", January 2025), they stay on the fast path and the flow enters the closing state. The commit message explains that the old behaviour "leads to packet races when restoring the conntrack state as well as late flow re-offload situations". +- It says hits are sent with `neigh_xmit()`. The IPv4 and IPv6 code now builds the header and calls `dev_queue_xmit()` as described above. + +## Further reading + +- [`Documentation/networking/nf_flowtable.rst`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst) — the kernel's own description, with the VLAN, PPPoE, bridge and counter details and the versions that added them +- [`net/netfilter/nft_flow_offload.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c) — the `flow add` statement +- [`net/netfilter/nf_flow_table_core.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c) — the table, timeouts and garbage collection +- [`net/netfilter/nf_flow_table_ip.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c) — the IPv4 and IPv6 fast path +- [`net/netfilter/nf_flow_table_offload.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c) — hardware offload +- [`a3c90f7a2323`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a3c90f7a2323b331ae816d5b0633e68148e25d04) — netfilter: nf_tables: flow offload expression (Pablo Neira Ayuso, January 2018) +- LWN articles [738214](https://lwn.net/Articles/738214/) and [742164](https://lwn.net/Articles/742164/), which the kernel documentation cites as its basis +- [Netfilter Architecture](netfilter.md) — the hook framework flowtable bypasses +- [Connection Tracking](conntrack.md) — the entries flowtable keeps alive +- [nftables vs iptables](nftables-iptables.md) — how to write the ruleset diff --git a/mkdocs.yml b/mkdocs.yml index 0a257744..657f4207 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -517,6 +517,7 @@ nav: - Netfilter Architecture: net/netfilter.md - nftables vs iptables: net/nftables-iptables.md - Connection Tracking: net/conntrack.md + - Flowtable Fast Path: net/flowtable.md - XDP & TC: - XDP (eXpress Data Path): net/xdp.md - AF_XDP Sockets: net/af-xdp.md From 2dd27d65f914a8db29d95f534823af0b06314023 Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 16:02:06 -0800 Subject: [PATCH 02/22] net: fix flowtable page after review --- docs/net/flowtable.md | 47 ++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index 8307a838..2dbc0dc1 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -4,15 +4,15 @@ ## What it does -A router or NAT gateway sends every forwarded packet through the same hooks: prerouting, routing, forward, postrouting, plus conntrack and NAT. Flowtable skips that work for packets of a flow it already knows. +A router or NAT gateway sends every forwarded packet through the same hooks: prerouting, the routing decision, forward and postrouting, plus conntrack and NAT. Flowtable skips that work for packets of a flow it already knows. -The kernel's [flowtable documentation](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst) describes it like this: once the first packets of a flow have gone through the normal forwarding path, a rule can add the flow to a table. The table is consulted at the ingress hook. A packet that matches an entry is rewritten and sent straight out, so it never reaches the hooks after ingress. A packet that doesn't match follows the classic path. +The kernel's [flowtable documentation](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst) describes it like this: once the first packets of a flow have gone through the normal forwarding path, a rule can add the flow to a table. The table is consulted at the ingress hook. A packet that matches an entry is rewritten and sent straight out, so it never reaches the IP-layer hooks (prerouting, forward, postrouting). A packet that doesn't match follows the classic path. The same table is also used by the tc `ct` action (`net/sched/act_ct.c`); this page covers the nftables side. ``` - ingress hook - ┌─────────────────────┐ - │ flowtable lookup │ - └─────────────────────┘ + ingress hook + ┌──────────────────────────────┐ + │ flowtable lookup │ + └─┬──────────────────────────┬─┘ │ hit │ miss or fall back ▼ ▼ ┌───────────────────┐ ┌─────────────────────┐ @@ -41,18 +41,18 @@ table inet x { } ``` -`flow add @f` is the nftables statement that calls `nft_flow_offload_eval()` in `net/netfilter/nft_flow_offload.c`. Once a flow is in the table, the `counter` rule stops counting its packets, because they no longer reach the forward chain. Give the flowtable a lower priority number than any nftables ingress chain on the same device so that it runs first. +`flow add @f` is the nftables statement that calls `nft_flow_offload_eval()` in `net/netfilter/nft_flow_offload.c`. Once a flow is in the table, the `counter` rule stops counting its packets, because they no longer reach the forward chain. Give the flowtable a lower priority number than any nftables ingress chain on the same device so that it runs first. The statement is accepted only in a chain on the forward hook (`nft_flow_offload_validate()`), and every device a flow can leave through has to be in the flowtable's device list, or the flow isn't added. ## How a flow gets in The `flow add` statement runs on a packet in the forward path and checks, in `nft_flow_offload_eval()`, whether the flow is a candidate: -- **Protocol.** TCP, UDP, or GRE. For TCP the conntrack entry must be established and the packet must not carry FIN or RST. GRE is accepted only without NAT and without a GRE key. +- **Protocol.** TCP, UDP, or GRE. For TCP the conntrack entry must be established and assured (`nf_conntrack_tcp_established()`) and the packet must not carry FIN or RST. GRE needs `CONFIG_NF_CT_PROTO_GRE` and is accepted only without NAT and without a GRE key; the code comment says "No support for GRE v1". - **No helper and no sequence adjustment.** A flow with a conntrack helper attached, with `IPS_SEQ_ADJUST` set, or with `IPS_NAT_CLASH` set is skipped. Packets that carry a security path (IPsec) or IPv4 options are skipped too (`nft_flow_offload_skip()`). - **Confirmed.** The conntrack entry must already be confirmed. - **Only once.** `test_and_set_bit(IPS_OFFLOAD_BIT, ...)` makes the first caller win, so two packets can't add the same flow twice. -If all of that holds, the kernel looks up the route in both directions, allocates a `struct flow_offload` that holds a reference to the conntrack entry, copies the SNAT and DNAT flags from the conntrack status, and inserts the entry. The entry has two tuples, one per direction, in one resizable hash table. The comment in `flow_offload_add()` notes that garbage collection walks only the original-direction entries, so that one is inserted last. +If all of that holds, the kernel resolves the path in both directions (it reuses the route of the packet being processed and looks up the reverse route), allocates a `struct flow_offload` that holds a reference to the conntrack entry, copies the SNAT and DNAT flags from the conntrack status, and inserts the entry. The entry has two tuples, one per direction, in one resizable hash table. The comment in `flow_offload_add()` notes that garbage collection walks only the original-direction entries, so that one is inserted last. Path resolution can also reject the flow: the egress device it resolves to must be in the flowtable, and the next hop needs a neighbour entry in a valid state (`NUD_VALID`). For TCP, `flow_offload_ct_tcp()` also sets the "be liberal" flag on the conntrack entry. The comment there gives the reason: conntrack will not see all packets any more, so it can't validate the TCP window. @@ -63,28 +63,31 @@ The IPv4 hook is `nf_flow_offload_ip_hook()` in `net/netfilter/nf_flow_table_ip. 1. Looks up the packet's tuple in the table. A miss returns `NF_ACCEPT` and the packet carries on down the classic path. 2. Checks the packet against the MTU recorded for the flow. A packet that is too big returns to the classic path, so the usual ICMP error can be generated. 3. Checks TCP flags (see below). -4. Strips any VLAN or PPPoE header, applies the stored NAT rewrite, decrements the TTL, and updates the conntrack counters if the flowtable has `counter`. -5. Builds the Ethernet header itself and calls `dev_queue_xmit()` on the egress device. In neighbour mode the destination MAC comes from the neighbour entry for the next hop. In direct mode the kernel uses MAC addresses stored in the entry. The path code picks direct mode for PPPoE and bridge paths, and when hardware offload is on for an Ethernet device. +4. Checks that the cached route is still valid. If it isn't, the flow is torn down and the packet goes back to the classic path. +5. Strips any VLAN, PPPoE or IP-in-IP tunnel header, applies the stored NAT rewrite, decrements the TTL, and updates the conntrack counters if the flowtable has `counter`. +6. Pushes any egress VLAN, PPPoE or tunnel header back on, builds the Ethernet header itself and calls `dev_queue_xmit()` on the egress device. In neighbour mode the destination MAC comes from the neighbour entry for the next hop. In direct mode the kernel uses MAC addresses stored in the entry. The path code picks direct mode for PPPoE and bridge paths, and when hardware offload is on for an Ethernet device. A flow whose route has an IPsec transform goes out through `dst_output()` instead. The IPv6 hook, `nf_flow_offload_ipv6_hook()`, does the same. ## How a flow leaves -A flow leaves the table in one of these ways, all handled by a garbage-collection worker that runs once a second (`nf_flow_offload_work_gc()`): +The garbage-collection worker, `nf_flow_offload_work_gc()`, runs once a second and removes entries that have been torn down. Teardown is triggered by: -- **It times out.** Each entry gets a deadline when it is added, and every hit pushes the deadline out. The default for TCP and UDP is 30 seconds (`offload_timeout` in `nf_conntrack_proto_tcp.c` and `nf_conntrack_proto_udp.c`). The refresh happens at most once a second per flow. -- **Conntrack entry is dying, or the cached route is no longer valid.** The collector tears the flow down. -- **TCP FIN or RST.** The hook marks the flow `NF_FLOW_CLOSING`. The packet itself still takes the fast path. The flow stays in the software table, but a hardware entry is removed early. A later SYN on a closing flow tears it down, sets the conntrack state to close, and sends that packet to the classic path so conntrack can handle the reconnect. +- **A timeout.** Each entry gets a deadline when it is added. For software flows every hit pushes the deadline out, at most once a second. The default is 30 seconds for TCP, UDP and GRE; the TCP and UDP values are the sysctls `nf_flowtable_tcp_timeout` and `nf_flowtable_udp_timeout` (`offload_timeout` in `nf_conntrack_proto_tcp.c` and `nf_conntrack_proto_udp.c`). Hardware flows never reach the hook, so the collector asks the driver for statistics and extends the deadline from the last-used time. +- **A dying conntrack entry or a stale route.** The collector tears the flow down. The hook does the same when the egress device or the neighbour entry has gone away. +- **A device going down.** The `NETDEV_DOWN` notifier calls `nf_flow_table_cleanup()`. -While a flow is in the table, conntrack doesn't see its packets, so its timeout would run out. `nf_flow_table_extend_ct_timeout()` pushes the conntrack timeout forward. Its comment explains the choice of a large fixed value: it saves a check of the offload bit in the packet path. +TCP FIN and RST start an early exit rather than an immediate one. The hook marks the flow `NF_FLOW_CLOSING` and the packet still takes the fast path. The flow stays in the software table, but a hardware entry is removed early. A later SYN on a closing flow tears it down, sets the conntrack state to close, and sends that packet to the classic path so conntrack can handle the reconnect. Otherwise the flow leaves by timeout. -When a flow is torn down, `flow_offload_fixup_ct()` hands it back to conntrack. For TCP it restores the last state conntrack recorded (or close, if the flow was closing), clears the window-tracking data that went stale because conntrack never saw the packets, and refreshes the conntrack timeout. +While a flow is in the table, conntrack doesn't see its packets, so its timeout would run out. When less than five minutes remain, `nf_flow_table_extend_ct_timeout()` pushes it forward: a day for UDP and GRE, and for TCP a value that depends on the state (a day when established, five minutes in the closing states, no extension in the SYN states or close). Its comment calls the value "an arbitrary timeout large enough not to ever expire", which saves a check of the offload bit in the packet path. + +When a flow is torn down, `flow_offload_fixup_ct()` hands it back to conntrack. For TCP it keeps the last state conntrack recorded (or sets close if the flow was closing) and clears the window-tracking data, because otherwise, as the code comment says, conntrack's TCP reset validation would fail. TCP and UDP flows also get the conntrack timeout reset to the normal timeout for their state. GRE flows are left as they are. ## Hardware offload If the device driver supports it, the flowtable can push entries to the NIC or switch. Add `flags offload;` to the flowtable definition. The kernel documentation says a workqueue adds the flows to hardware, so a few packets can still go through the software path until it runs. -In the code, `net/netfilter/nf_flow_table_offload.c` turns each entry into a flow rule, which is a set of match keys and actions such as header rewrites, and hands it to the driver as `FLOW_CLS_REPLACE`, `FLOW_CLS_DESTROY` or `FLOW_CLS_STATS`. The `NF_FLOW_PENDING` bit marks an entry whose work is queued, and the garbage collector leaves it alone until the work finishes. Pablo Neira Ayuso added this in [`c29f74e0df7a`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c29f74e0df7a02b8303bcdce93a7c0132d62577a) (November 2019). +In the code, `net/netfilter/nf_flow_table_offload.c` sends the driver three commands: `FLOW_CLS_REPLACE` to add a flow, `FLOW_CLS_DESTROY` to remove it and `FLOW_CLS_STATS` to read its counters. Adding builds a flow rule, which is a set of match keys and actions such as header rewrites. Pablo Neira Ayuso added hardware offload in [`c29f74e0df7a`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c29f74e0df7a02b8303bcdce93a7c0132d62577a) (November 2019). The `NF_FLOW_PENDING` bit marks an entry whose work is queued. Paul Blakey added it as `NF_FLOW_HW_PENDING` in [`2c8897953f3b`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=2c8897953f3b2ff5498f3f275708a742bfcdbc24) (May 2020). The garbage collector's skip of pending entries and the rename came from [`7c549fb7eecd`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7c549fb7eecd01fdba7c0d12c01da876ef8a3214) (September 2026). `conntrack -L` shows `[OFFLOAD]` for flows in the software table and `[HW_OFFLOAD]` for flows in hardware. @@ -92,14 +95,16 @@ In the code, `net/netfilter/nf_flow_table_offload.c` turns each entry into a flo - Fragments, and packets that exceed the MTU, go through the classic path. The kernel documentation gives this as the reason: the transport header is missing for fragments, so no lookup is possible. - The table is a cache. The kernel documentation warns that an entry can go stale if the destination MAC address or the egress device changes. It names two risky setups: software mode combined with bridging and IP forwarding, and hardware offload. -- The classic hooks after ingress don't see offloaded packets. A rule there, such as a counter or a log, won't match them. +- The IP-layer hooks (prerouting, forward, postrouting) don't see offloaded packets. A rule there, such as a counter or a log, won't match them. ## Where the in-tree documentation is behind the code -The kernel documentation was written for the original design. Two statements in it no longer match the source in the tree I read (v7.4-rc3-803): +The kernel documentation was last substantially updated in March 2021 ([`143490cde566`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=143490cde5669e0151dff466a7c2cf70e2884fb7)). As of v7.4-rc3, four statements in it no longer match the source: - It says TCP RST and FIN packets are passed up to the classic path. Since [`fdbaf5163331`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fdbaf5163331342e90a2c29b87629021f4c15f0c) ("netfilter: flowtable: add CLOSING state", January 2025), they stay on the fast path and the flow enters the closing state. The commit message explains that the old behaviour "leads to packet races when restoring the conntrack state as well as late flow re-offload situations". -- It says hits are sent with `neigh_xmit()`. The IPv4 and IPv6 code now builds the header and calls `dev_queue_xmit()` as described above. +- It says hits are sent with `neigh_xmit()`. Since [`b5964aac51e0`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b5964aac51e0c286a50e68225e0dfcf11fb554cb) ("netfilter: flowtable: consolidate xmit path", October 2025), the code builds the header and calls `dev_queue_xmit()`, as described above. +- It says the rule adds the flow on "the TCP syn-ack packet". The code requires an established and assured conntrack entry, which a SYN-ACK can't be. That check came with [`e5eaac2beb54`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e5eaac2beb54f0a16ff851125082d9faeb475572) ("netfilter: flowtable: fix TCP flow teardown", May 2022). +- It says the flowtable supports TCP and UDP. GRE has been accepted since [`4e8d9584d154`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4e8d9584d154479d357327f76d4e49486915c9c9) (February 2022). ## Further reading From 21e153d2cc2657bae356c72f735745f821a3715d Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 16:25:53 -0800 Subject: [PATCH 03/22] net: tighten flowtable page wording --- docs/net/README.md | 2 +- docs/net/flowtable.md | 32 ++++++++++++++++---------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/docs/net/README.md b/docs/net/README.md index 1120dd9e..1e03fd46 100644 --- a/docs/net/README.md +++ b/docs/net/README.md @@ -64,7 +64,7 @@ Every packet is carried by an [sk_buff](sk-buff.md) — start there if you read | [Netfilter](netfilter.md) | Hooks, tables, chains | | [nftables vs iptables](nftables-iptables.md) | Why the replacement happened | | [Connection Tracking](conntrack.md) | conntrack entries, NAT, helpers | -| [Flowtable](flowtable.md) | The fast path that lets established flows skip the hook path | +| [Flowtable](flowtable.md) | The fast path that lets established flows skip most of the hook path | | [Traffic Control (tc/qdisc)](tc-qdisc.md) | Queuing disciplines, shaping, fq_codel | ### High performance & offload diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index 2dbc0dc1..cf3d036c 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -6,7 +6,7 @@ A router or NAT gateway sends every forwarded packet through the same hooks: prerouting, the routing decision, forward and postrouting, plus conntrack and NAT. Flowtable skips that work for packets of a flow it already knows. -The kernel's [flowtable documentation](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst) describes it like this: once the first packets of a flow have gone through the normal forwarding path, a rule can add the flow to a table. The table is consulted at the ingress hook. A packet that matches an entry is rewritten and sent straight out, so it never reaches the IP-layer hooks (prerouting, forward, postrouting). A packet that doesn't match follows the classic path. The same table is also used by the tc `ct` action (`net/sched/act_ct.c`); this page covers the nftables side. +The kernel's [flowtable documentation](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst) describes it like this: once the first packets of a flow have gone through the normal forwarding path, a rule can add the flow to a table. The table is consulted at the ingress hook. A packet that matches an entry is rewritten and sent straight out, so it skips the IP-layer hooks (prerouting, forward and, for most flows, postrouting). A packet that doesn't match follows the classic path. The same infrastructure is also used by the tc `ct` action (`net/sched/act_ct.c`); this page covers the nftables side. ``` ingress hook @@ -41,18 +41,18 @@ table inet x { } ``` -`flow add @f` is the nftables statement that calls `nft_flow_offload_eval()` in `net/netfilter/nft_flow_offload.c`. Once a flow is in the table, the `counter` rule stops counting its packets, because they no longer reach the forward chain. Give the flowtable a lower priority number than any nftables ingress chain on the same device so that it runs first. The statement is accepted only in a chain on the forward hook (`nft_flow_offload_validate()`), and every device a flow can leave through has to be in the flowtable's device list, or the flow isn't added. +`flow add @f` is the nftables statement that calls `nft_flow_offload_eval()` in `net/netfilter/nft_flow_offload.c`. Once a flow is in the table, the `counter` rule stops counting its packets, because they no longer reach the forward chain. Give the flowtable a lower priority number than any nftables ingress chain on the same device so that it runs first. The statement is accepted only in a chain on the forward hook (`nft_flow_offload_validate()`), and for ordinary routed paths every device a flow can leave through has to be in the flowtable's device list, or the flow isn't added. ## How a flow gets in The `flow add` statement runs on a packet in the forward path and checks, in `nft_flow_offload_eval()`, whether the flow is a candidate: -- **Protocol.** TCP, UDP, or GRE. For TCP the conntrack entry must be established and assured (`nf_conntrack_tcp_established()`) and the packet must not carry FIN or RST. GRE needs `CONFIG_NF_CT_PROTO_GRE` and is accepted only without NAT and without a GRE key; the code comment says "No support for GRE v1". +- **Protocol.** TCP, UDP, or GRE. For TCP the conntrack entry must be established and assured (`nf_conntrack_tcp_established()`) and the packet must not carry FIN or RST. GRE needs `CONFIG_NF_CT_PROTO_GRE` and is accepted only without NAT and only for GRE version 0: the code tests the conntrack tuple's GRE key, which conntrack fills in only for version 1 (PPTP), and the comment says "No support for GRE v1". - **No helper and no sequence adjustment.** A flow with a conntrack helper attached, with `IPS_SEQ_ADJUST` set, or with `IPS_NAT_CLASH` set is skipped. Packets that carry a security path (IPsec) or IPv4 options are skipped too (`nft_flow_offload_skip()`). - **Confirmed.** The conntrack entry must already be confirmed. - **Only once.** `test_and_set_bit(IPS_OFFLOAD_BIT, ...)` makes the first caller win, so two packets can't add the same flow twice. -If all of that holds, the kernel resolves the path in both directions (it reuses the route of the packet being processed and looks up the reverse route), allocates a `struct flow_offload` that holds a reference to the conntrack entry, copies the SNAT and DNAT flags from the conntrack status, and inserts the entry. The entry has two tuples, one per direction, in one resizable hash table. The comment in `flow_offload_add()` notes that garbage collection walks only the original-direction entries, so that one is inserted last. Path resolution can also reject the flow: the egress device it resolves to must be in the flowtable, and the next hop needs a neighbour entry in a valid state (`NUD_VALID`). +If all of that holds, the kernel resolves the path in both directions (it reuses the route of the packet being processed and looks up the reverse route), allocates a `struct flow_offload` that holds a reference to the conntrack entry, copies the SNAT and DNAT flags from the conntrack status, and inserts the entry. The entry has two tuples, one per direction, in one resizable hash table. The comment in `flow_offload_add()` notes that garbage collection walks only the original-direction entries, so that one is inserted last. For ordinary routed paths, path resolution can also reject the flow: the egress device it resolves to must be in the flowtable, and on an Ethernet device the next hop needs a neighbour entry in a valid state (`NUD_VALID`). For TCP, `flow_offload_ct_tcp()` also sets the "be liberal" flag on the conntrack entry. The comment there gives the reason: conntrack will not see all packets any more, so it can't validate the TCP window. @@ -63,25 +63,25 @@ The IPv4 hook is `nf_flow_offload_ip_hook()` in `net/netfilter/nf_flow_table_ip. 1. Looks up the packet's tuple in the table. A miss returns `NF_ACCEPT` and the packet carries on down the classic path. 2. Checks the packet against the MTU recorded for the flow. A packet that is too big returns to the classic path, so the usual ICMP error can be generated. 3. Checks TCP flags (see below). -4. Checks that the cached route is still valid. If it isn't, the flow is torn down and the packet goes back to the classic path. +4. Checks that the cached route, if the flow keeps one, is still valid. If it isn't, the flow is torn down and the packet goes back to the classic path. 5. Strips any VLAN, PPPoE or IP-in-IP tunnel header, applies the stored NAT rewrite, decrements the TTL, and updates the conntrack counters if the flowtable has `counter`. -6. Pushes any egress VLAN, PPPoE or tunnel header back on, builds the Ethernet header itself and calls `dev_queue_xmit()` on the egress device. In neighbour mode the destination MAC comes from the neighbour entry for the next hop. In direct mode the kernel uses MAC addresses stored in the entry. The path code picks direct mode for PPPoE and bridge paths, and when hardware offload is on for an Ethernet device. A flow whose route has an IPsec transform goes out through `dst_output()` instead. +6. Pushes any egress VLAN, PPPoE or tunnel header back on, builds the link-layer header with `dev_hard_header()` and calls `dev_queue_xmit()` on the egress device. In neighbour mode the destination MAC comes from the neighbour entry for the next hop. In direct mode the kernel uses MAC addresses stored in the entry. The path code picks direct mode for PPPoE and bridge paths, and when hardware offload is on for an Ethernet device. A flow whose route has an IPsec transform goes out through `dst_output()` instead, which still runs the postrouting hook. -The IPv6 hook, `nf_flow_offload_ipv6_hook()`, does the same. +The IPv6 hook, `nf_flow_offload_ipv6_hook()`, does the same, with the hop limit in place of the TTL and IPv6-in-IPv6 as the tunnel. Any extension header is a miss, because the next header has to be TCP, UDP or GRE. ## How a flow leaves The garbage-collection worker, `nf_flow_offload_work_gc()`, runs once a second and removes entries that have been torn down. Teardown is triggered by: -- **A timeout.** Each entry gets a deadline when it is added. For software flows every hit pushes the deadline out, at most once a second. The default is 30 seconds for TCP, UDP and GRE; the TCP and UDP values are the sysctls `nf_flowtable_tcp_timeout` and `nf_flowtable_udp_timeout` (`offload_timeout` in `nf_conntrack_proto_tcp.c` and `nf_conntrack_proto_udp.c`). Hardware flows never reach the hook, so the collector asks the driver for statistics and extends the deadline from the last-used time. -- **A dying conntrack entry or a stale route.** The collector tears the flow down. The hook does the same when the egress device or the neighbour entry has gone away. +- **A timeout.** Each entry gets a deadline when it is added. For software flows every hit pushes the deadline out, at most once a second. The default is 30 seconds for TCP, UDP and GRE; the TCP and UDP values are the sysctls `nf_flowtable_tcp_timeout` and `nf_flowtable_udp_timeout` (`offload_timeout` in `nf_conntrack_proto_tcp.c` and `nf_conntrack_proto_udp.c`). Packets of a hardware flow mostly bypass the hook, so the collector asks the driver for statistics, once less than 90% of the timeout remains, and extends the deadline from the last-used time. +- **A dying conntrack entry or a stale route.** The collector tears the flow down. The hook tears the flow down, and drops that packet, when the egress device no longer exists or no neighbour entry can be found or created. - **A device going down.** The `NETDEV_DOWN` notifier calls `nf_flow_table_cleanup()`. -TCP FIN and RST start an early exit rather than an immediate one. The hook marks the flow `NF_FLOW_CLOSING` and the packet still takes the fast path. The flow stays in the software table, but a hardware entry is removed early. A later SYN on a closing flow tears it down, sets the conntrack state to close, and sends that packet to the classic path so conntrack can handle the reconnect. Otherwise the flow leaves by timeout. +TCP FIN and RST don't end the flow at once. The hook marks the flow `NF_FLOW_CLOSING` and the packet still takes the fast path. The flow stays in the software table, but a hardware entry is removed early. A later SYN on a closing flow tears it down, sets the conntrack state to close, and sends that packet to the classic path so conntrack can handle the reconnect. Otherwise the flow leaves by timeout. -While a flow is in the table, conntrack doesn't see its packets, so its timeout would run out. When less than five minutes remain, `nf_flow_table_extend_ct_timeout()` pushes it forward: a day for UDP and GRE, and for TCP a value that depends on the state (a day when established, five minutes in the closing states, no extension in the SYN states or close). Its comment calls the value "an arbitrary timeout large enough not to ever expire", which saves a check of the offload bit in the packet path. +While a flow is in the table, conntrack doesn't see its packets, so its timeout would run out. `flow_offload_add()` first sets it to a day. After that the garbage collector calls `nf_flow_table_extend_ct_timeout()`, which pushes it forward when less than five minutes remain: a day for UDP and GRE, and for TCP a value that depends on the state (a day when established, five minutes in the closing states, no extension in the SYN states or close). Its comment calls the value "an arbitrary timeout large enough not to ever expire", which saves a check of the offload bit in the packet path. -When a flow is torn down, `flow_offload_fixup_ct()` hands it back to conntrack. For TCP it keeps the last state conntrack recorded (or sets close if the flow was closing) and clears the window-tracking data, because otherwise, as the code comment says, conntrack's TCP reset validation would fail. TCP and UDP flows also get the conntrack timeout reset to the normal timeout for their state. GRE flows are left as they are. +When a flow is torn down, `flow_offload_fixup_ct()` hands it back to conntrack. For TCP it keeps the last state conntrack recorded (or sets close if the flow was closing) and clears the recorded maximum window, because otherwise, as the code comment says, conntrack's TCP reset validation would fail. TCP and UDP flows also have the conntrack timeout cut back to the normal value for their state, less the flowtable timeout if the flow expired. A closing TCP flow gets the close timeout. GRE flows are left as they are. ## Hardware offload @@ -93,17 +93,17 @@ In the code, `net/netfilter/nf_flow_table_offload.c` sends the driver three comm ## Limits -- Fragments, and packets that exceed the MTU, go through the classic path. The kernel documentation gives this as the reason: the transport header is missing for fragments, so no lookup is possible. +- Fragments, and packets that exceed the MTU, go through the classic path. The kernel documentation gives this as the reason: the transport header is missing for fragments, so no lookup is possible. Packets with IPv4 options, a TTL or hop limit of 1 or less, or GRE other than version 0 also go to the classic path. - The table is a cache. The kernel documentation warns that an entry can go stale if the destination MAC address or the egress device changes. It names two risky setups: software mode combined with bridging and IP forwarding, and hardware offload. -- The IP-layer hooks (prerouting, forward, postrouting) don't see offloaded packets. A rule there, such as a counter or a log, won't match them. +- The IP-layer hooks (prerouting and forward, and postrouting for flows without IPsec) don't see offloaded packets. A rule there, such as a counter or a log, won't match them. ## Where the in-tree documentation is behind the code The kernel documentation was last substantially updated in March 2021 ([`143490cde566`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=143490cde5669e0151dff466a7c2cf70e2884fb7)). As of v7.4-rc3, four statements in it no longer match the source: - It says TCP RST and FIN packets are passed up to the classic path. Since [`fdbaf5163331`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fdbaf5163331342e90a2c29b87629021f4c15f0c) ("netfilter: flowtable: add CLOSING state", January 2025), they stay on the fast path and the flow enters the closing state. The commit message explains that the old behaviour "leads to packet races when restoring the conntrack state as well as late flow re-offload situations". -- It says hits are sent with `neigh_xmit()`. Since [`b5964aac51e0`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b5964aac51e0c286a50e68225e0dfcf11fb554cb) ("netfilter: flowtable: consolidate xmit path", October 2025), the code builds the header and calls `dev_queue_xmit()`, as described above. -- It says the rule adds the flow on "the TCP syn-ack packet". The code requires an established and assured conntrack entry, which a SYN-ACK can't be. That check came with [`e5eaac2beb54`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e5eaac2beb54f0a16ff851125082d9faeb475572) ("netfilter: flowtable: fix TCP flow teardown", May 2022). +- It says hits are sent with `neigh_xmit()`. Direct mode has called `dev_queue_xmit()` since [`7a27f6ab4135`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7a27f6ab41356ecba47ec2bec6d635704c169779) (March 2021), and [`b5964aac51e0`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b5964aac51e0c286a50e68225e0dfcf11fb554cb) ("netfilter: flowtable: consolidate xmit path", October 2025) moved neighbour mode to it too, as described above. +- It says the rule adds the flow on "the TCP syn-ack packet". The code requires an established and assured conntrack entry. A SYN-ACK passes while conntrack still has the entry in SYN_RECV, so it can't qualify. That check came with [`e5eaac2beb54`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e5eaac2beb54f0a16ff851125082d9faeb475572) ("netfilter: flowtable: fix TCP flow teardown", May 2022). - It says the flowtable supports TCP and UDP. GRE has been accepted since [`4e8d9584d154`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4e8d9584d154479d357327f76d4e49486915c9c9) (February 2022). ## Further reading From 6209b80e7027eebd1255e205ded2b4ccfc258a2b Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 16:46:15 -0800 Subject: [PATCH 04/22] net: add operator caveats and release numbers to flowtable page --- docs/net/flowtable.md | 45 +++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index cf3d036c..05ef92ed 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -2,11 +2,15 @@ > How the kernel lets established forwarded connections skip most of the netfilter hook path +This page describes the code as of v7.4-rc3. Behaviour that changed recently is marked with the release that introduced it. + ## What it does A router or NAT gateway sends every forwarded packet through the same hooks: prerouting, the routing decision, forward and postrouting, plus conntrack and NAT. Flowtable skips that work for packets of a flow it already knows. -The kernel's [flowtable documentation](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst) describes it like this: once the first packets of a flow have gone through the normal forwarding path, a rule can add the flow to a table. The table is consulted at the ingress hook. A packet that matches an entry is rewritten and sent straight out, so it skips the IP-layer hooks (prerouting, forward and, for most flows, postrouting). A packet that doesn't match follows the classic path. The same infrastructure is also used by the tc `ct` action (`net/sched/act_ct.c`); this page covers the nftables side. +The kernel's [flowtable documentation](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst) describes it like this: once the first packets of a flow have gone through the normal forwarding path, a rule can add the flow to a table. The table is consulted at the ingress hook. A packet that matches an entry is rewritten and sent straight out, skipping the hooks after ingress. A packet that doesn't match follows the classic path. + +In the current code, a hit skips prerouting, forward and, except for IPsec flows, postrouting. It does not skip everything. The tc ingress hook runs before the flowtable, and because hits go out through `dev_queue_xmit()`, the netdev egress hook, tc egress and the qdisc still run on transmit. The same infrastructure is also used by the tc `ct` action (`net/sched/act_ct.c`); this page covers the nftables side. ``` ingress hook @@ -41,7 +45,11 @@ table inet x { } ``` -`flow add @f` is the nftables statement that calls `nft_flow_offload_eval()` in `net/netfilter/nft_flow_offload.c`. Once a flow is in the table, the `counter` rule stops counting its packets, because they no longer reach the forward chain. Give the flowtable a lower priority number than any nftables ingress chain on the same device so that it runs first. The statement is accepted only in a chain on the forward hook (`nft_flow_offload_validate()`), and for ordinary routed paths every device a flow can leave through has to be in the flowtable's device list, or the flow isn't added. +`flow add @f` is the nftables statement that calls `nft_flow_offload_eval()` in `net/netfilter/nft_flow_offload.c`. Once a flow is in the table, the `counter` rule stops counting the packets that take the fast path, because they no longer reach the forward chain. + +The kernel documentation recommends a lower priority number than any nftables ingress chain on the same device, so that the flowtable runs first. That is a choice: an ingress chain that runs after the flowtable never sees offloaded packets, and one that runs before it sees all of them. + +The statement is accepted only in a chain on the forward hook (`nft_flow_offload_validate()`). For ordinary routed paths, the real lower device behind any VLAN, PPPoE or bridge device has to be in the flowtable's device list, in both directions, or the flow isn't added. The kernel documentation says the same: the real device is sufficient, and the VLAN and PPPoE devices need not be listed. ## How a flow gets in @@ -54,7 +62,7 @@ The `flow add` statement runs on a packet in the forward path and checks, in `nf If all of that holds, the kernel resolves the path in both directions (it reuses the route of the packet being processed and looks up the reverse route), allocates a `struct flow_offload` that holds a reference to the conntrack entry, copies the SNAT and DNAT flags from the conntrack status, and inserts the entry. The entry has two tuples, one per direction, in one resizable hash table. The comment in `flow_offload_add()` notes that garbage collection walks only the original-direction entries, so that one is inserted last. For ordinary routed paths, path resolution can also reject the flow: the egress device it resolves to must be in the flowtable, and on an Ethernet device the next hop needs a neighbour entry in a valid state (`NUD_VALID`). -For TCP, `flow_offload_ct_tcp()` also sets the "be liberal" flag on the conntrack entry. The comment there gives the reason: conntrack will not see all packets any more, so it can't validate the TCP window. +For TCP, `flow_offload_ct_tcp()` also sets the "be liberal" flag on the conntrack entry. The comment there gives the reason: conntrack will not see all packets any more, so it can't validate the TCP window. Nothing in the flowtable code clears that flag, so window tracking stays off for the connection for the rest of its life, including after it returns to conntrack. ## What a hit does @@ -63,8 +71,8 @@ The IPv4 hook is `nf_flow_offload_ip_hook()` in `net/netfilter/nf_flow_table_ip. 1. Looks up the packet's tuple in the table. A miss returns `NF_ACCEPT` and the packet carries on down the classic path. 2. Checks the packet against the MTU recorded for the flow. A packet that is too big returns to the classic path, so the usual ICMP error can be generated. 3. Checks TCP flags (see below). -4. Checks that the cached route, if the flow keeps one, is still valid. If it isn't, the flow is torn down and the packet goes back to the classic path. -5. Strips any VLAN, PPPoE or IP-in-IP tunnel header, applies the stored NAT rewrite, decrements the TTL, and updates the conntrack counters if the flowtable has `counter`. +4. Checks that the cached route, if the flow keeps one, is still valid. If it isn't, the flow is torn down and the packet goes back to the classic path. Direct-mode flows without a tunnel keep no route, so a route or bridge forwarding-table change isn't noticed. The commit that added direct mode says that "if peer is moved to different bridge port, the connection will time out". +5. Strips any VLAN, PPPoE or IP-in-IP tunnel header (IPIP since v6.19, IPv6-in-IPv6 since v7.0), applies the stored NAT rewrite, decrements the TTL, and updates the conntrack counters if the flowtable has `counter`. 6. Pushes any egress VLAN, PPPoE or tunnel header back on, builds the link-layer header with `dev_hard_header()` and calls `dev_queue_xmit()` on the egress device. In neighbour mode the destination MAC comes from the neighbour entry for the next hop. In direct mode the kernel uses MAC addresses stored in the entry. The path code picks direct mode for PPPoE and bridge paths, and when hardware offload is on for an Ethernet device. A flow whose route has an IPsec transform goes out through `dst_output()` instead, which still runs the postrouting hook. The IPv6 hook, `nf_flow_offload_ipv6_hook()`, does the same, with the hop limit in place of the TTL and IPv6-in-IPv6 as the tunnel. Any extension header is a miss, because the next header has to be TCP, UDP or GRE. @@ -73,38 +81,41 @@ The IPv6 hook, `nf_flow_offload_ipv6_hook()`, does the same, with the hop limit The garbage-collection worker, `nf_flow_offload_work_gc()`, runs once a second and removes entries that have been torn down. Teardown is triggered by: -- **A timeout.** Each entry gets a deadline when it is added. For software flows every hit pushes the deadline out, at most once a second. The default is 30 seconds for TCP, UDP and GRE; the TCP and UDP values are the sysctls `nf_flowtable_tcp_timeout` and `nf_flowtable_udp_timeout` (`offload_timeout` in `nf_conntrack_proto_tcp.c` and `nf_conntrack_proto_udp.c`). Packets of a hardware flow mostly bypass the hook, so the collector asks the driver for statistics, once less than 90% of the timeout remains, and extends the deadline from the last-used time. +- **A timeout.** Each entry gets a deadline when it is added. For software flows every hit pushes the deadline out, at most once a second. The default is 30 seconds for TCP, UDP and GRE (GRE has no sysctl); the TCP and UDP values are the sysctls `net.netfilter.nf_flowtable_tcp_timeout` and `net.netfilter.nf_flowtable_udp_timeout` (`offload_timeout` in `nf_conntrack_proto_tcp.c` and `nf_conntrack_proto_udp.c`). Packets of a hardware flow mostly bypass the hook, so the collector asks the driver for statistics, once less than 90% of the timeout remains, and extends the deadline from the last-used time. - **A dying conntrack entry or a stale route.** The collector tears the flow down. The hook tears the flow down, and drops that packet, when the egress device no longer exists or no neighbour entry can be found or created. - **A device going down.** The `NETDEV_DOWN` notifier calls `nf_flow_table_cleanup()`. TCP FIN and RST don't end the flow at once. The hook marks the flow `NF_FLOW_CLOSING` and the packet still takes the fast path. The flow stays in the software table, but a hardware entry is removed early. A later SYN on a closing flow tears it down, sets the conntrack state to close, and sends that packet to the classic path so conntrack can handle the reconnect. Otherwise the flow leaves by timeout. -While a flow is in the table, conntrack doesn't see its packets, so its timeout would run out. `flow_offload_add()` first sets it to a day. After that the garbage collector calls `nf_flow_table_extend_ct_timeout()`, which pushes it forward when less than five minutes remain: a day for UDP and GRE, and for TCP a value that depends on the state (a day when established, five minutes in the closing states, no extension in the SYN states or close). Its comment calls the value "an arbitrary timeout large enough not to ever expire", which saves a check of the offload bit in the packet path. +Teardown clears the offload bit on the conntrack entry, so the next forward-chain packet that matches the `flow add` rule can add the flow again. + +While a flow is in the table, conntrack doesn't see its packets, so its timeout would run out. `flow_offload_add()` first sets it to a day. After that the garbage collector calls `nf_flow_table_extend_ct_timeout()`, which pushes it forward when less than five minutes remain: a day for UDP and GRE (GRE only since the v7.4 release candidates), and for TCP a value that depends on the state (a day when established, five minutes in the closing states, no extension in the SYN states or close). Its comment calls the value "an arbitrary timeout large enough not to ever expire", which saves a check of the offload bit in the packet path. -When a flow is torn down, `flow_offload_fixup_ct()` hands it back to conntrack. For TCP it keeps the last state conntrack recorded (or sets close if the flow was closing) and clears the recorded maximum window, because otherwise, as the code comment says, conntrack's TCP reset validation would fail. TCP and UDP flows also have the conntrack timeout cut back to the normal value for their state, less the flowtable timeout if the flow expired. A closing TCP flow gets the close timeout. GRE flows are left as they are. +When a flow is torn down, `flow_offload_fixup_ct()` hands it back to conntrack. For TCP it keeps the last state conntrack recorded (or sets close if the flow was closing) and clears the recorded maximum window, because otherwise, as the code comment says, conntrack's TCP reset validation would fail. TCP and UDP flows also have the conntrack timeout cut back to the normal value for their state, less the flowtable timeout if the flow expired. A closing TCP flow gets the close timeout. GRE flows are left as they are, so a GRE conntrack entry can keep a timeout of up to a day after it leaves the table. ## Hardware offload If the device driver supports it, the flowtable can push entries to the NIC or switch. Add `flags offload;` to the flowtable definition. The kernel documentation says a workqueue adds the flows to hardware, so a few packets can still go through the software path until it runs. -In the code, `net/netfilter/nf_flow_table_offload.c` sends the driver three commands: `FLOW_CLS_REPLACE` to add a flow, `FLOW_CLS_DESTROY` to remove it and `FLOW_CLS_STATS` to read its counters. Adding builds a flow rule, which is a set of match keys and actions such as header rewrites. Pablo Neira Ayuso added hardware offload in [`c29f74e0df7a`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c29f74e0df7a02b8303bcdce93a7c0132d62577a) (November 2019). The `NF_FLOW_PENDING` bit marks an entry whose work is queued. Paul Blakey added it as `NF_FLOW_HW_PENDING` in [`2c8897953f3b`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=2c8897953f3b2ff5498f3f275708a742bfcdbc24) (May 2020). The garbage collector's skip of pending entries and the rename came from [`7c549fb7eecd`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7c549fb7eecd01fdba7c0d12c01da876ef8a3214) (September 2026). +In the code, `net/netfilter/nf_flow_table_offload.c` sends the driver three commands: `FLOW_CLS_REPLACE` to add a flow, `FLOW_CLS_DESTROY` to remove it and `FLOW_CLS_STATS` to read its counters. Adding builds a flow rule, which is a set of match keys and actions such as header rewrites. Pablo Neira Ayuso added hardware offload in [`c29f74e0df7a`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c29f74e0df7a02b8303bcdce93a7c0132d62577a) (v5.5). The collector skips an entry whose hardware work is still queued (`NF_FLOW_PENDING`). Flows that go through an IP-in-IP tunnel stay in software: [`6c5dcab95f4c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6c5dcab95f4cd42a1648739ec9300fbb4b1a021f) (v7.2) skips the hardware offload for them. -`conntrack -L` shows `[OFFLOAD]` for flows in the software table and `[HW_OFFLOAD]` for flows in hardware. +`conntrack -L` shows `[OFFLOAD]` for flows in the software table and `[HW_OFFLOAD]` for flows in hardware, as the kernel documentation describes. For an offloaded entry the kernel leaves out the timeout and protocol state: ctnetlink skips them (`nf_conntrack_netlink.c`) and `/proc/net/nf_conntrack` skips the timeout (`nf_conntrack_standalone.c`). Per-CPU flowtable statistics are in `/proc/net/stat/nf_flowtable`. ## Limits -- Fragments, and packets that exceed the MTU, go through the classic path. The kernel documentation gives this as the reason: the transport header is missing for fragments, so no lookup is possible. Packets with IPv4 options, a TTL or hop limit of 1 or less, or GRE other than version 0 also go to the classic path. +- Changing or removing rules does not evict flows that are already in the table, including the `flow add` rule itself. A hit never reaches the forward chain, and every software hit pushes the deadline out, so a busy flow doesn't expire. A new drop, reject, rate-limit or mark rule in the forward chain doesn't apply to such flows either; only the stored NAT rewrite is applied. A flow leaves when it goes idle, when its conntrack entry is deleted (for example with `conntrack -D`), when a device goes down, or when the flowtable is deleted. +- Fragments, and packets that exceed the MTU, go through the classic path. The kernel documentation gives this as the reason: the transport header is missing for fragments, so no lookup is possible. Packets with IPv4 options, a TTL or hop limit of 1 or less, or GRE other than version 0 also go to the classic path. The commit that extended the GRE timeout, [`46ecda278f6e`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=46ecda278f6eedffb2898d41e3e56757c996a41f) (October 2026, in the v7.4 release candidates), notes that `ip6gre` tunnels with the default encapsulation limit carry a Destination Options header before GRE, which the flowtable does not parse, so their packets all take the classic path. - The table is a cache. The kernel documentation warns that an entry can go stale if the destination MAC address or the egress device changes. It names two risky setups: software mode combined with bridging and IP forwarding, and hardware offload. - The IP-layer hooks (prerouting and forward, and postrouting for flows without IPsec) don't see offloaded packets. A rule there, such as a counter or a log, won't match them. -## Where the in-tree documentation is behind the code +## Differences from `nf_flowtable.rst` -The kernel documentation was last substantially updated in March 2021 ([`143490cde566`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=143490cde5669e0151dff466a7c2cf70e2884fb7)). As of v7.4-rc3, four statements in it no longer match the source: +The kernel documentation was last changed in March 2021 (v5.13, [`143490cde566`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=143490cde5669e0151dff466a7c2cf70e2884fb7)). As of v7.4-rc3, at least these statements in it no longer match the source: -- It says TCP RST and FIN packets are passed up to the classic path. Since [`fdbaf5163331`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fdbaf5163331342e90a2c29b87629021f4c15f0c) ("netfilter: flowtable: add CLOSING state", January 2025), they stay on the fast path and the flow enters the closing state. The commit message explains that the old behaviour "leads to packet races when restoring the conntrack state as well as late flow re-offload situations". -- It says hits are sent with `neigh_xmit()`. Direct mode has called `dev_queue_xmit()` since [`7a27f6ab4135`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7a27f6ab41356ecba47ec2bec6d635704c169779) (March 2021), and [`b5964aac51e0`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b5964aac51e0c286a50e68225e0dfcf11fb554cb) ("netfilter: flowtable: consolidate xmit path", October 2025) moved neighbour mode to it too, as described above. -- It says the rule adds the flow on "the TCP syn-ack packet". The code requires an established and assured conntrack entry. A SYN-ACK passes while conntrack still has the entry in SYN_RECV, so it can't qualify. That check came with [`e5eaac2beb54`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e5eaac2beb54f0a16ff851125082d9faeb475572) ("netfilter: flowtable: fix TCP flow teardown", May 2022). -- It says the flowtable supports TCP and UDP. GRE has been accepted since [`4e8d9584d154`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4e8d9584d154479d357327f76d4e49486915c9c9) (February 2022). +- It says TCP RST and FIN packets are passed up to the classic path. Since [`fdbaf5163331`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fdbaf5163331342e90a2c29b87629021f4c15f0c) ("netfilter: flowtable: add CLOSING state", v6.14), they stay on the fast path and the flow enters the closing state. The commit message explains that the old behaviour "leads to packet races when restoring the conntrack state as well as late flow re-offload situations". +- It says hits are sent with `neigh_xmit()`. Direct mode has called `dev_queue_xmit()` since [`7a27f6ab4135`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7a27f6ab41356ecba47ec2bec6d635704c169779) (v5.13, the same day as the documentation update), and [`b5964aac51e0`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b5964aac51e0c286a50e68225e0dfcf11fb554cb) ("netfilter: flowtable: consolidate xmit path", v6.19) moved neighbour mode to it too. As a result the netdev egress hook still runs for hits, which the documentation's "you do not see these packets from any of the Netfilter hooks coming after ingress" does not cover. +- It says the rule adds the flow on "the TCP syn-ack packet". The code requires an established and assured conntrack entry. [`e5eaac2beb54`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e5eaac2beb54f0a16ff851125082d9faeb475572) (v5.18) added that check, and its message says the patch makes `nft_flow_offload` "skip packets in SYN_RECV state" and "postpones offload up to the next packet". +- It says the flowtable supports TCP and UDP. GRE has been accepted since [`4e8d9584d154`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4e8d9584d154479d357327f76d4e49486915c9c9) (v5.18). ## Further reading From b46b4033e52e827ff69552f6044aae2d38d2f7b0 Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 17:19:23 -0800 Subject: [PATCH 05/22] net: add flowtable history and hardware driver interface --- docs/net/flowtable.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index 05ef92ed..d18a5385 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -97,7 +97,9 @@ When a flow is torn down, `flow_offload_fixup_ct()` hands it back to conntrack. If the device driver supports it, the flowtable can push entries to the NIC or switch. Add `flags offload;` to the flowtable definition. The kernel documentation says a workqueue adds the flows to hardware, so a few packets can still go through the software path until it runs. -In the code, `net/netfilter/nf_flow_table_offload.c` sends the driver three commands: `FLOW_CLS_REPLACE` to add a flow, `FLOW_CLS_DESTROY` to remove it and `FLOW_CLS_STATS` to read its counters. Adding builds a flow rule, which is a set of match keys and actions such as header rewrites. Pablo Neira Ayuso added hardware offload in [`c29f74e0df7a`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c29f74e0df7a02b8303bcdce93a7c0132d62577a) (v5.5). The collector skips an entry whose hardware work is still queued (`NF_FLOW_PENDING`). Flows that go through an IP-in-IP tunnel stay in software: [`6c5dcab95f4c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6c5dcab95f4cd42a1648739ec9300fbb4b1a021f) (v7.2) skips the hardware offload for them. +In the code, the driver interface is the tc flow-block interface. When the flowtable's ingress hook is registered on a device, the flowtable type's `setup` callback, `nf_flow_table_offload_setup()`, binds a flow block to that device. If the device has `ndo_setup_tc`, the flowtable calls it with `TC_SETUP_FT`. Otherwise it goes through the indirect-block path, `flow_indr_dev_setup_offload()`. The driver adds its callback to the flowtable's block. For each flow, `nf_flow_table_offload.c` then calls every callback with `TC_SETUP_CLSFLOWER` and a `flow_cls_offload` request, the same request type tc flower uses. The command is `FLOW_CLS_REPLACE` to add a flow (carrying a flow rule: match keys plus actions such as header rewrites), `FLOW_CLS_DESTROY` to remove it or `FLOW_CLS_STATS` to read its counters. + +Pablo Neira Ayuso added hardware offload, including `TC_SETUP_FT`, in [`c29f74e0df7a`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c29f74e0df7a02b8303bcdce93a7c0132d62577a) (v5.5). A search of the tree for `TC_SETUP_FT` finds users in the mlx5 representor code, the MediaTek and Airoha Ethernet drivers, DSA user ports and the `netdevsim` test driver. The collector skips an entry whose hardware work is still queued (`NF_FLOW_PENDING`). Flows that go through an IP-in-IP tunnel stay in software: [`6c5dcab95f4c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6c5dcab95f4cd42a1648739ec9300fbb4b1a021f) (v7.2) skips the hardware offload for them. `conntrack -L` shows `[OFFLOAD]` for flows in the software table and `[HW_OFFLOAD]` for flows in hardware, as the kernel documentation describes. For an offloaded entry the kernel leaves out the timeout and protocol state: ctnetlink skips them (`nf_conntrack_netlink.c`) and `/proc/net/nf_conntrack` skips the timeout (`nf_conntrack_standalone.c`). Per-CPU flowtable statistics are in `/proc/net/stat/nf_flowtable`. @@ -108,6 +110,15 @@ In the code, `net/netfilter/nf_flow_table_offload.c` sends the driver three comm - The table is a cache. The kernel documentation warns that an entry can go stale if the destination MAC address or the egress device changes. It names two risky setups: software mode combined with bridging and IP forwarding, and hardware offload. - The IP-layer hooks (prerouting and forward, and postrouting for flows without IPsec) don't see offloaded packets. A rule there, such as a counter or a log, won't match them. +## Where it came from + +The design was posted to netfilter-devel by Pablo Neira Ayuso. LWN reproduces both postings, and the kernel documentation cites them. + +- **3 November 2017**, [`[PATCH RFC,WIP 0/5] Flow offload infrastructure`](https://lwn.net/Articles/738214/) (Message-ID `<20171103152636.9967-1-pablo@netfilter.org>`). The cover letter describes a module with a hook at ingress, populated by an nftables action once a flow is established. It gives the hardware motivation: "Switches and NICs come with built-in flow table, I've been observing out of tree patches in OpenWRT/LEDE to integrate this into Netfilter for a little while." It reports a measurement: "I'm measuring here that the software flow table forwarding path is 2.5 faster than the classic forwarding path in my testbed." Its TODO list included IPv4 only, IPv4 SNAT only, no netns support and no flow counters, and the last patch added `ndo` hooks to populate a hardware flow table. +- **22 December 2017**, [`[PATCH nf-next,v3 0/7] Flow offload infrastructure`](https://lwn.net/Articles/742164/) (Message-ID `<20171222192732.13188-1-pablo@netfilter.org>`). This version added IPv6 and mixed IPv4/IPv6 support, hardware offload in a separate `nf_flow_table_hw` module, port translation and netns support. The hardware patch was tagged RFC, and the cover letter says "I will keep this one back until there's an initial driver that introduces flow table offload support". + +The software flow table was authored on 7 January 2018 ([`ac2a66665e23`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ac2a66665e231847cab11b8c8e844ce43207dd2e) adds the generic infrastructure and [`a3c90f7a2323`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a3c90f7a2323b331ae816d5b0633e68148e25d04) the nftables expression) and first appeared in v4.16. Felix Fietkau reworked the file layout in February 2018, for example in [`1a999d899bc2`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1a999d899bc26abee26d70c10762c81b9d684d2b), which renamed `nf_flow_table.c` to `nf_flow_table_core.c`. Hardware offload reached the tree in v5.5, as described above, through the tc flow-block interface rather than the `ndo` hooks of the 2017 patches. + ## Differences from `nf_flowtable.rst` The kernel documentation was last changed in March 2021 (v5.13, [`143490cde566`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=143490cde5669e0151dff466a7c2cf70e2884fb7)). As of v7.4-rc3, at least these statements in it no longer match the source: @@ -125,7 +136,7 @@ The kernel documentation was last changed in March 2021 (v5.13, [`143490cde566`] - [`net/netfilter/nf_flow_table_ip.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c) — the IPv4 and IPv6 fast path - [`net/netfilter/nf_flow_table_offload.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c) — hardware offload - [`a3c90f7a2323`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a3c90f7a2323b331ae816d5b0633e68148e25d04) — netfilter: nf_tables: flow offload expression (Pablo Neira Ayuso, January 2018) -- LWN articles [738214](https://lwn.net/Articles/738214/) and [742164](https://lwn.net/Articles/742164/), which the kernel documentation cites as its basis +- The 2017 postings on LWN, [RFC of 3 November](https://lwn.net/Articles/738214/) and [v3 of 22 December](https://lwn.net/Articles/742164/) — the cover letters with the original rationale, which the kernel documentation cites - [Netfilter Architecture](netfilter.md) — the hook framework flowtable bypasses - [Connection Tracking](conntrack.md) — the entries flowtable keeps alive - [nftables vs iptables](nftables-iptables.md) — how to write the ruleset From e0035e9d1e8211d7cb35f5c2336d3a6345391895 Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 17:24:51 -0800 Subject: [PATCH 06/22] net: correct flowtable hardware offload history --- docs/net/flowtable.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index d18a5385..982ba528 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -97,9 +97,9 @@ When a flow is torn down, `flow_offload_fixup_ct()` hands it back to conntrack. If the device driver supports it, the flowtable can push entries to the NIC or switch. Add `flags offload;` to the flowtable definition. The kernel documentation says a workqueue adds the flows to hardware, so a few packets can still go through the software path until it runs. -In the code, the driver interface is the tc flow-block interface. When the flowtable's ingress hook is registered on a device, the flowtable type's `setup` callback, `nf_flow_table_offload_setup()`, binds a flow block to that device. If the device has `ndo_setup_tc`, the flowtable calls it with `TC_SETUP_FT`. Otherwise it goes through the indirect-block path, `flow_indr_dev_setup_offload()`. The driver adds its callback to the flowtable's block. For each flow, `nf_flow_table_offload.c` then calls every callback with `TC_SETUP_CLSFLOWER` and a `flow_cls_offload` request, the same request type tc flower uses. The command is `FLOW_CLS_REPLACE` to add a flow (carrying a flow rule: match keys plus actions such as header rewrites), `FLOW_CLS_DESTROY` to remove it or `FLOW_CLS_STATS` to read its counters. +In the code, the driver interface is the tc flow-block interface. For a flowtable with `flags offload`, registering its ingress hook on a device first calls the flowtable type's `setup` callback, `nf_flow_table_offload_setup()`, which binds a flow block to that device. If the device has `ndo_setup_tc`, the flowtable calls it with `TC_SETUP_FT`. Otherwise it goes through the indirect-block path, `flow_indr_dev_setup_offload()`. The driver adds its callback to the flowtable's block. For each flow, `nf_flow_table_offload.c` then calls every callback with `TC_SETUP_CLSFLOWER` and a `flow_cls_offload` request, the same request type tc flower uses. The command is `FLOW_CLS_REPLACE` to add a flow (carrying a flow rule: match keys plus actions such as header rewrites), `FLOW_CLS_DESTROY` to remove it or `FLOW_CLS_STATS` to read its counters. -Pablo Neira Ayuso added hardware offload, including `TC_SETUP_FT`, in [`c29f74e0df7a`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c29f74e0df7a02b8303bcdce93a7c0132d62577a) (v5.5). A search of the tree for `TC_SETUP_FT` finds users in the mlx5 representor code, the MediaTek and Airoha Ethernet drivers, DSA user ports and the `netdevsim` test driver. The collector skips an entry whose hardware work is still queued (`NF_FLOW_PENDING`). Flows that go through an IP-in-IP tunnel stay in software: [`6c5dcab95f4c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6c5dcab95f4cd42a1648739ec9300fbb4b1a021f) (v7.2) skips the hardware offload for them. +Pablo Neira Ayuso added hardware offload in [`c29f74e0df7a`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c29f74e0df7a02b8303bcdce93a7c0132d62577a) (v5.5). That commit called `ndo_setup_tc` with `TC_SETUP_BLOCK` and the callbacks with `TC_SETUP_FT`. The arrangement described above came from two fixes by wenxu in the same release, [`ee1bcfe01251`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ee1bcfe01251e8958b53de45d2b0c85e09dd2719) and [`e052901991ae`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e052901991ae21e15851bfc89c682bfcb39a4dcf). The indirect path was added by wenxu in [`b5140a36da78`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b5140a36da7876bc084a2c680c8dbc7438db2051) (v5.7). A search of the tree for `TC_SETUP_FT` finds the mlx5 representor code, the MediaTek Ethernet and WED drivers, the mt76 Wi-Fi driver, the Airoha Ethernet driver and DSA user ports, which pass the command to the conduit device. `netdevsim` accepts the command but registers no callback. The collector skips an entry whose hardware work is still queued (`NF_FLOW_PENDING`). Flows that go through an IP-in-IP tunnel stay in software: [`6c5dcab95f4c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6c5dcab95f4cd42a1648739ec9300fbb4b1a021f) (v7.2) skips the hardware offload for them. `conntrack -L` shows `[OFFLOAD]` for flows in the software table and `[HW_OFFLOAD]` for flows in hardware, as the kernel documentation describes. For an offloaded entry the kernel leaves out the timeout and protocol state: ctnetlink skips them (`nf_conntrack_netlink.c`) and `/proc/net/nf_conntrack` skips the timeout (`nf_conntrack_standalone.c`). Per-CPU flowtable statistics are in `/proc/net/stat/nf_flowtable`. @@ -112,12 +112,12 @@ Pablo Neira Ayuso added hardware offload, including `TC_SETUP_FT`, in [`c29f74e0 ## Where it came from -The design was posted to netfilter-devel by Pablo Neira Ayuso. LWN reproduces both postings, and the kernel documentation cites them. +The design was posted to netfilter-devel by Pablo Neira Ayuso. LWN reproduces two of the postings, and the kernel documentation cites them. - **3 November 2017**, [`[PATCH RFC,WIP 0/5] Flow offload infrastructure`](https://lwn.net/Articles/738214/) (Message-ID `<20171103152636.9967-1-pablo@netfilter.org>`). The cover letter describes a module with a hook at ingress, populated by an nftables action once a flow is established. It gives the hardware motivation: "Switches and NICs come with built-in flow table, I've been observing out of tree patches in OpenWRT/LEDE to integrate this into Netfilter for a little while." It reports a measurement: "I'm measuring here that the software flow table forwarding path is 2.5 faster than the classic forwarding path in my testbed." Its TODO list included IPv4 only, IPv4 SNAT only, no netns support and no flow counters, and the last patch added `ndo` hooks to populate a hardware flow table. - **22 December 2017**, [`[PATCH nf-next,v3 0/7] Flow offload infrastructure`](https://lwn.net/Articles/742164/) (Message-ID `<20171222192732.13188-1-pablo@netfilter.org>`). This version added IPv6 and mixed IPv4/IPv6 support, hardware offload in a separate `nf_flow_table_hw` module, port translation and netns support. The hardware patch was tagged RFC, and the cover letter says "I will keep this one back until there's an initial driver that introduces flow table offload support". -The software flow table was authored on 7 January 2018 ([`ac2a66665e23`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ac2a66665e231847cab11b8c8e844ce43207dd2e) adds the generic infrastructure and [`a3c90f7a2323`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a3c90f7a2323b331ae816d5b0633e68148e25d04) the nftables expression) and first appeared in v4.16. Felix Fietkau reworked the file layout in February 2018, for example in [`1a999d899bc2`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1a999d899bc26abee26d70c10762c81b9d684d2b), which renamed `nf_flow_table.c` to `nf_flow_table_core.c`. Hardware offload reached the tree in v5.5, as described above, through the tc flow-block interface rather than the `ndo` hooks of the 2017 patches. +The software flow table was merged on 8 January 2018 ([`ac2a66665e23`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ac2a66665e231847cab11b8c8e844ce43207dd2e) adds the generic infrastructure and [`a3c90f7a2323`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a3c90f7a2323b331ae816d5b0633e68148e25d04) the nftables expression) and first appeared in v4.16. Felix Fietkau reorganised the code in a series written in February 2018 and merged for v4.18, for example in [`1a999d899bc2`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1a999d899bc26abee26d70c10762c81b9d684d2b), which renamed `nf_flow_table.c` to `nf_flow_table_core.c`. Hardware offload reached the tree in v5.5 through the tc flow-block interface, with the indirect path following in v5.7, rather than through the `ndo` hooks proposed in the RFC. ## Differences from `nf_flowtable.rst` From 8753ac9c40a846509995f601c6044f5924a58c28 Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 17:57:15 -0800 Subject: [PATCH 07/22] net: restructure flowtable page and fix review findings --- docs/net/flowtable.md | 104 ++++++++++++++++++++++++++++-------------- 1 file changed, 70 insertions(+), 34 deletions(-) diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index 982ba528..1faef9cc 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -6,27 +6,29 @@ This page describes the code as of v7.4-rc3. Behaviour that changed recently is ## What it does -A router or NAT gateway sends every forwarded packet through the same hooks: prerouting, the routing decision, forward and postrouting, plus conntrack and NAT. Flowtable skips that work for packets of a flow it already knows. +A router or NAT gateway sends every forwarded packet through prerouting, the routing decision, forward and postrouting, plus conntrack and NAT. Flowtable skips that work for packets of a flow it already knows. The 2017 proposal that introduced it reported the software fast path as faster than the classic path in the author's testbed ([below](#where-it-came-from)). The kernel's [flowtable documentation](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst) describes it like this: once the first packets of a flow have gone through the normal forwarding path, a rule can add the flow to a table. The table is consulted at the ingress hook. A packet that matches an entry is rewritten and sent straight out, skipping the hooks after ingress. A packet that doesn't match follows the classic path. -In the current code, a hit skips prerouting, forward and, except for IPsec flows, postrouting. It does not skip everything. The tc ingress hook runs before the flowtable, and because hits go out through `dev_queue_xmit()`, the netdev egress hook, tc egress and the qdisc still run on transmit. The same infrastructure is also used by the tc `ct` action (`net/sched/act_ct.c`); this page covers the nftables side. - ``` - ingress hook - ┌──────────────────────────────┐ - │ flowtable lookup │ - └─┬──────────────────────────┬─┘ - │ hit │ miss or fall back - ▼ ▼ -┌───────────────────┐ ┌─────────────────────┐ -│ rewrite headers, │ │ classic path: │ -│ send out the │ │ prerouting, forward,│ -│ egress device │ │ postrouting │ -└───────────────────┘ └─────────────────────┘ + ingress hook + ┌───────────────────────────────┐ + │ flowtable lookup │ + └─┬───────────────────────────┬─┘ + │ hit │ miss or fall back + ▼ ▼ +┌───────────────────┐ ┌───────────────────────┐ +│ rewrite headers, │ │ classic path: │ +│ send out the │ │ prerouting, forward, │ +│ egress device │ │ postrouting │ +└───────────────────┘ └───────────────────────┘ ``` -Only flows you select are added. Nothing is offloaded unless a rule says so. +A `flow add` rule in the forward chain of the classic path is what inserts a flow into the table. Only flows you select are added; nothing is offloaded unless a rule says so. + +In the current code, a hit skips prerouting, forward and, except for IPsec flows, postrouting. It does not skip everything. The tc ingress hook runs before the flowtable. Every hit ends in `dev_queue_xmit()`, so the netdev egress hook, tc egress and the qdisc still run on transmit. + +The same infrastructure is also used by the tc `ct` action (`net/sched/act_ct.c`); this page covers the nftables side. ## Turning it on @@ -45,22 +47,28 @@ table inet x { } ``` -`flow add @f` is the nftables statement that calls `nft_flow_offload_eval()` in `net/netfilter/nft_flow_offload.c`. Once a flow is in the table, the `counter` rule stops counting the packets that take the fast path, because they no longer reach the forward chain. +`flow add @f` is the nftables statement that calls `nft_flow_offload_eval()` in `net/netfilter/nft_flow_offload.c`. It is accepted only in a chain on the forward hook (`nft_flow_offload_validate()`). The example rule matches IPv4 TCP only; match on the protocols you want offloaded. + +Placement matters. The packet that adds the flow keeps going down the chain, but once the flow is in the table, later packets skip the rest of the chain. The `counter` rule in the example stops counting them, and so would any limit, mark or DSCP rule placed after the `flow add` rule. -The kernel documentation recommends a lower priority number than any nftables ingress chain on the same device, so that the flowtable runs first. That is a choice: an ingress chain that runs after the flowtable never sees offloaded packets, and one that runs before it sees all of them. +The flowtable has its own `counter` flag. Without it, conntrack's packet and byte counts stop updating for packets that take the fast path. -The statement is accepted only in a chain on the forward hook (`nft_flow_offload_validate()`). For ordinary routed paths, the real lower device behind any VLAN, PPPoE or bridge device has to be in the flowtable's device list, in both directions, or the flow isn't added. The kernel documentation says the same: the real device is sufficient, and the VLAN and PPPoE devices need not be listed. +The kernel documentation recommends a lower priority number than any nftables ingress chain on the same device, so that the flowtable runs first. That is a choice: an ingress chain that runs after the flowtable never sees offloaded packets, and one that runs before it sees all of them that reach the CPU. + +Except for IPsec routes, the real lower device behind any VLAN, PPPoE or bridge device has to be in the flowtable's device list, in both directions, or the flow isn't added. The kernel documentation says the real device is sufficient and the VLAN and PPPoE devices need not be listed; in the code it is required. ## How a flow gets in The `flow add` statement runs on a packet in the forward path and checks, in `nft_flow_offload_eval()`, whether the flow is a candidate: -- **Protocol.** TCP, UDP, or GRE. For TCP the conntrack entry must be established and assured (`nf_conntrack_tcp_established()`) and the packet must not carry FIN or RST. GRE needs `CONFIG_NF_CT_PROTO_GRE` and is accepted only without NAT and only for GRE version 0: the code tests the conntrack tuple's GRE key, which conntrack fills in only for version 1 (PPTP), and the comment says "No support for GRE v1". +- **Protocol.** TCP, UDP, or GRE (forwarded GRE flows tracked by conntrack). For TCP the conntrack entry must be established and assured (`nf_conntrack_tcp_established()`) and the packet must not carry FIN or RST. UDP and GRE need only a confirmed entry. GRE needs `CONFIG_NF_CT_PROTO_GRE` and is accepted only without NAT and only for GRE version 0: the code tests the conntrack tuple's GRE key, which conntrack fills in only for version 1 (PPTP), and the comment says "No support for GRE v1". - **No helper and no sequence adjustment.** A flow with a conntrack helper attached, with `IPS_SEQ_ADJUST` set, or with `IPS_NAT_CLASH` set is skipped. Packets that carry a security path (IPsec) or IPv4 options are skipped too (`nft_flow_offload_skip()`). - **Confirmed.** The conntrack entry must already be confirmed. - **Only once.** `test_and_set_bit(IPS_OFFLOAD_BIT, ...)` makes the first caller win, so two packets can't add the same flow twice. -If all of that holds, the kernel resolves the path in both directions (it reuses the route of the packet being processed and looks up the reverse route), allocates a `struct flow_offload` that holds a reference to the conntrack entry, copies the SNAT and DNAT flags from the conntrack status, and inserts the entry. The entry has two tuples, one per direction, in one resizable hash table. The comment in `flow_offload_add()` notes that garbage collection walks only the original-direction entries, so that one is inserted last. For ordinary routed paths, path resolution can also reject the flow: the egress device it resolves to must be in the flowtable, and on an Ethernet device the next hop needs a neighbour entry in a valid state (`NUD_VALID`). +If all of that holds, the kernel resolves the path in both directions (it reuses the route of the packet being processed and looks up the reverse route). Except for IPsec routes, path resolution can also reject the flow: the egress device it resolves to must be in the flowtable, and on an Ethernet device the next hop needs a neighbour entry in a valid state (`NUD_VALID`). + +It then allocates a `struct flow_offload` that holds a reference to the conntrack entry, copies the SNAT and DNAT flags from the conntrack status, and inserts the entry. The entry has two tuples, one per direction, in one resizable hash table. The comment in `flow_offload_add()` notes that garbage collection walks only the original-direction entries, so that one is inserted last. For TCP, `flow_offload_ct_tcp()` also sets the "be liberal" flag on the conntrack entry. The comment there gives the reason: conntrack will not see all packets any more, so it can't validate the TCP window. Nothing in the flowtable code clears that flag, so window tracking stays off for the connection for the rest of its life, including after it returns to conntrack. @@ -72,10 +80,12 @@ The IPv4 hook is `nf_flow_offload_ip_hook()` in `net/netfilter/nf_flow_table_ip. 2. Checks the packet against the MTU recorded for the flow. A packet that is too big returns to the classic path, so the usual ICMP error can be generated. 3. Checks TCP flags (see below). 4. Checks that the cached route, if the flow keeps one, is still valid. If it isn't, the flow is torn down and the packet goes back to the classic path. Direct-mode flows without a tunnel keep no route, so a route or bridge forwarding-table change isn't noticed. The commit that added direct mode says that "if peer is moved to different bridge port, the connection will time out". -5. Strips any VLAN, PPPoE or IP-in-IP tunnel header (IPIP since v6.19, IPv6-in-IPv6 since v7.0), applies the stored NAT rewrite, decrements the TTL, and updates the conntrack counters if the flowtable has `counter`. -6. Pushes any egress VLAN, PPPoE or tunnel header back on, builds the link-layer header with `dev_hard_header()` and calls `dev_queue_xmit()` on the egress device. In neighbour mode the destination MAC comes from the neighbour entry for the next hop. In direct mode the kernel uses MAC addresses stored in the entry. The path code picks direct mode for PPPoE and bridge paths, and when hardware offload is on for an Ethernet device. A flow whose route has an IPsec transform goes out through `dst_output()` instead, which still runs the postrouting hook. +5. Strips any VLAN, PPPoE or IPIP tunnel header (IPIP since v6.19), applies the stored NAT rewrite, decrements the TTL, and updates the conntrack counters if the flowtable has `counter`. +6. Pushes any egress VLAN, PPPoE or tunnel header back on, builds the link-layer header with `dev_hard_header()` and calls `dev_queue_xmit()` on the egress device. + +In neighbour mode the destination MAC comes from the neighbour entry for the next hop. In direct mode the kernel uses MAC addresses stored in the entry. The path code picks direct mode for PPPoE and bridge paths, and when hardware offload is on for an Ethernet device. A flow whose route has an IPsec transform goes out through `dst_output()` instead, which still runs the postrouting hook. -The IPv6 hook, `nf_flow_offload_ipv6_hook()`, does the same, with the hop limit in place of the TTL and IPv6-in-IPv6 as the tunnel. Any extension header is a miss, because the next header has to be TCP, UDP or GRE. +The IPv6 hook, `nf_flow_offload_ipv6_hook()`, does the same, with the hop limit in place of the TTL. Any extension header is a miss, because the next header has to be TCP, UDP or GRE. It strips IPv6-in-IPv6 tunnel headers (since v7.0). Mixed-family tunnels are not supported: [`d0107f381622`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=d0107f38162279d9cb0e012210731176265f835d) says IPv4 over IPv6 and SIT tunnel offloading "is not enabled yet". ## How a flow leaves @@ -87,44 +97,70 @@ The garbage-collection worker, `nf_flow_offload_work_gc()`, runs once a second a TCP FIN and RST don't end the flow at once. The hook marks the flow `NF_FLOW_CLOSING` and the packet still takes the fast path. The flow stays in the software table, but a hardware entry is removed early. A later SYN on a closing flow tears it down, sets the conntrack state to close, and sends that packet to the classic path so conntrack can handle the reconnect. Otherwise the flow leaves by timeout. -Teardown clears the offload bit on the conntrack entry, so the next forward-chain packet that matches the `flow add` rule can add the flow again. +Teardown clears the offload bit on the conntrack entry. The flow can be added again once the collector has removed the old entry and a later forward-chain packet matches the `flow add` rule. A TCP flow torn down by a SYN is in the close state, so it isn't added again until it is established. -While a flow is in the table, conntrack doesn't see its packets, so its timeout would run out. `flow_offload_add()` first sets it to a day. After that the garbage collector calls `nf_flow_table_extend_ct_timeout()`, which pushes it forward when less than five minutes remain: a day for UDP and GRE (GRE only since the v7.4 release candidates), and for TCP a value that depends on the state (a day when established, five minutes in the closing states, no extension in the SYN states or close). Its comment calls the value "an arbitrary timeout large enough not to ever expire", which saves a check of the offload bit in the packet path. +While a flow is in the table, conntrack doesn't see its packets, so its timeout would run out. `flow_offload_add()` first sets it to a day. After that the garbage collector calls `nf_flow_table_extend_ct_timeout()`, which pushes it forward when less than five minutes remain. The new value depends on the protocol and state: -When a flow is torn down, `flow_offload_fixup_ct()` hands it back to conntrack. For TCP it keeps the last state conntrack recorded (or sets close if the flow was closing) and clears the recorded maximum window, because otherwise, as the code comment says, conntrack's TCP reset validation would fail. TCP and UDP flows also have the conntrack timeout cut back to the normal value for their state, less the flowtable timeout if the flow expired. A closing TCP flow gets the close timeout. GRE flows are left as they are, so a GRE conntrack entry can keep a timeout of up to a day after it leaves the table. +| Flow | Conntrack timeout is extended to | +|------|----------------------------------| +| UDP, GRE (GRE only since the v7.4 release candidates) | a day | +| TCP established | a day | +| TCP FIN_WAIT, CLOSE_WAIT, LAST_ACK, TIME_WAIT | five minutes | +| TCP SYN_SENT, SYN_RECV, CLOSE | no extension | + +The function's comment calls the value "an arbitrary timeout large enough not to ever expire", which saves a check of the offload bit in the packet path. + +When a flow is torn down, `flow_offload_fixup_ct()` hands it back to conntrack. For TCP it keeps the last state conntrack recorded (or sets close if the flow was closing). It clears the recorded maximum window and the max-ack flag, because otherwise, as the code comment says, conntrack's TCP reset validation would fail. TCP and UDP flows also have their conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired, and a closing TCP flow gets the close timeout. The timeout is only ever lowered, so an established TCP entry, whose normal timeout is five days, keeps whatever is left of its day. GRE flows are left as they are, so a GRE conntrack entry can keep a timeout of up to a day after it leaves the table. ## Hardware offload If the device driver supports it, the flowtable can push entries to the NIC or switch. Add `flags offload;` to the flowtable definition. The kernel documentation says a workqueue adds the flows to hardware, so a few packets can still go through the software path until it runs. -In the code, the driver interface is the tc flow-block interface. For a flowtable with `flags offload`, registering its ingress hook on a device first calls the flowtable type's `setup` callback, `nf_flow_table_offload_setup()`, which binds a flow block to that device. If the device has `ndo_setup_tc`, the flowtable calls it with `TC_SETUP_FT`. Otherwise it goes through the indirect-block path, `flow_indr_dev_setup_offload()`. The driver adds its callback to the flowtable's block. For each flow, `nf_flow_table_offload.c` then calls every callback with `TC_SETUP_CLSFLOWER` and a `flow_cls_offload` request, the same request type tc flower uses. The command is `FLOW_CLS_REPLACE` to add a flow (carrying a flow rule: match keys plus actions such as header rewrites), `FLOW_CLS_DESTROY` to remove it or `FLOW_CLS_STATS` to read its counters. +The driver interface is the tc flow-block interface: + +- **Binding.** For a flowtable with `flags offload`, registering its ingress hook on a device first calls the flowtable type's `setup` callback, `nf_flow_table_offload_setup()`, which binds a flow block to that device. If the device has `ndo_setup_tc`, the flowtable calls it with `TC_SETUP_FT`. Otherwise it goes through the indirect-block path, `flow_indr_dev_setup_offload()`. The driver adds its callback to the flowtable's block. +- **Per flow.** `nf_flow_table_offload.c` calls every callback with `TC_SETUP_CLSFLOWER` and a `flow_cls_offload` request, the same request type tc flower uses. The command is `FLOW_CLS_REPLACE` to add a flow (carrying a flow rule: match keys plus actions such as header rewrites), `FLOW_CLS_DESTROY` to remove it or `FLOW_CLS_STATS` to read its counters. +- **Drivers.** The in-tree drivers that handle `TC_SETUP_FT` are the mlx5 representor code, the MediaTek Ethernet and WED drivers, the mt76 Wi-Fi driver and the Airoha Ethernet driver. DSA user ports pass the command to the conduit device, and `netdevsim` accepts it but registers no callback. + +Some operator-visible consequences: -Pablo Neira Ayuso added hardware offload in [`c29f74e0df7a`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c29f74e0df7a02b8303bcdce93a7c0132d62577a) (v5.5). That commit called `ndo_setup_tc` with `TC_SETUP_BLOCK` and the callbacks with `TC_SETUP_FT`. The arrangement described above came from two fixes by wenxu in the same release, [`ee1bcfe01251`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ee1bcfe01251e8958b53de45d2b0c85e09dd2719) and [`e052901991ae`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e052901991ae21e15851bfc89c682bfcb39a4dcf). The indirect path was added by wenxu in [`b5140a36da78`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b5140a36da7876bc084a2c680c8dbc7438db2051) (v5.7). A search of the tree for `TC_SETUP_FT` finds the mlx5 representor code, the MediaTek Ethernet and WED drivers, the mt76 Wi-Fi driver, the Airoha Ethernet driver and DSA user ports, which pass the command to the conduit device. `netdevsim` accepts the command but registers no callback. The collector skips an entry whose hardware work is still queued (`NF_FLOW_PENDING`). Flows that go through an IP-in-IP tunnel stay in software: [`6c5dcab95f4c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6c5dcab95f4cd42a1648739ec9300fbb4b1a021f) (v7.2) skips the hardware offload for them. +- A device that can't offload makes the flowtable fail to load: the `setup` error is returned before the hook is registered. The `offload` flag can't be toggled on an existing flowtable. +- With `flags offload`, every Ethernet path uses direct mode, so a flow without a tunnel caches no route and a route change isn't noticed until the flow goes idle. +- Flows that go through an IP-in-IP tunnel stay in software: [`6c5dcab95f4c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6c5dcab95f4cd42a1648739ec9300fbb4b1a021f) (v7.2) skips the hardware offload for them. +- The collector skips an entry whose hardware work is still queued (`NF_FLOW_PENDING`). -`conntrack -L` shows `[OFFLOAD]` for flows in the software table and `[HW_OFFLOAD]` for flows in hardware, as the kernel documentation describes. For an offloaded entry the kernel leaves out the timeout and protocol state: ctnetlink skips them (`nf_conntrack_netlink.c`) and `/proc/net/nf_conntrack` skips the timeout (`nf_conntrack_standalone.c`). Per-CPU flowtable statistics are in `/proc/net/stat/nf_flowtable`. +## Inspecting + +`conntrack -L` shows `[OFFLOAD]` for flows in the software table and `[HW_OFFLOAD]` for flows in hardware, as the kernel documentation describes. For an offloaded entry the kernel leaves out the timeout and protocol state: ctnetlink skips them (`nf_conntrack_netlink.c`) and `/proc/net/nf_conntrack` skips the timeout and the TCP state (`nf_conntrack_standalone.c`, `nf_conntrack_proto_tcp.c`). + +With `CONFIG_NF_FLOW_TABLE_PROCFS`, `/proc/net/stat/nf_flowtable` shows per-CPU counts of queued hardware add, delete and stats work. It has no hit or miss counters. The two timeout sysctls are described above. ## Limits -- Changing or removing rules does not evict flows that are already in the table, including the `flow add` rule itself. A hit never reaches the forward chain, and every software hit pushes the deadline out, so a busy flow doesn't expire. A new drop, reject, rate-limit or mark rule in the forward chain doesn't apply to such flows either; only the stored NAT rewrite is applied. A flow leaves when it goes idle, when its conntrack entry is deleted (for example with `conntrack -D`), when a device goes down, or when the flowtable is deleted. +- Changing or removing rules does not evict flows that are already in the table, including the `flow add` rule itself. A hit never reaches the forward chain, and every software hit pushes the deadline out, so a busy flow doesn't expire. A new drop, reject, rate-limit or mark rule in the forward chain doesn't apply to such flows either; only the stored NAT rewrite is applied. Flows leave when they go idle, when their conntrack entry is deleted (for example with `conntrack -D`), when a device goes down or the route goes stale, or when the flowtable is deleted. A reload that starts with `flush ruleset` deletes the flowtable, which tears down every flow in it, so they are evaluated again. - Fragments, and packets that exceed the MTU, go through the classic path. The kernel documentation gives this as the reason: the transport header is missing for fragments, so no lookup is possible. Packets with IPv4 options, a TTL or hop limit of 1 or less, or GRE other than version 0 also go to the classic path. The commit that extended the GRE timeout, [`46ecda278f6e`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=46ecda278f6eedffb2898d41e3e56757c996a41f) (October 2026, in the v7.4 release candidates), notes that `ip6gre` tunnels with the default encapsulation limit carry a Destination Options header before GRE, which the flowtable does not parse, so their packets all take the classic path. - The table is a cache. The kernel documentation warns that an entry can go stale if the destination MAC address or the egress device changes. It names two risky setups: software mode combined with bridging and IP forwarding, and hardware offload. - The IP-layer hooks (prerouting and forward, and postrouting for flows without IPsec) don't see offloaded packets. A rule there, such as a counter or a log, won't match them. +- IPsec: packets that carry a security path are never added. A flow whose route has an IPsec transform goes out through `dst_output()`, so postrouting still runs, and the device-list and neighbour checks don't apply to it. ## Where it came from The design was posted to netfilter-devel by Pablo Neira Ayuso. LWN reproduces two of the postings, and the kernel documentation cites them. - **3 November 2017**, [`[PATCH RFC,WIP 0/5] Flow offload infrastructure`](https://lwn.net/Articles/738214/) (Message-ID `<20171103152636.9967-1-pablo@netfilter.org>`). The cover letter describes a module with a hook at ingress, populated by an nftables action once a flow is established. It gives the hardware motivation: "Switches and NICs come with built-in flow table, I've been observing out of tree patches in OpenWRT/LEDE to integrate this into Netfilter for a little while." It reports a measurement: "I'm measuring here that the software flow table forwarding path is 2.5 faster than the classic forwarding path in my testbed." Its TODO list included IPv4 only, IPv4 SNAT only, no netns support and no flow counters, and the last patch added `ndo` hooks to populate a hardware flow table. -- **22 December 2017**, [`[PATCH nf-next,v3 0/7] Flow offload infrastructure`](https://lwn.net/Articles/742164/) (Message-ID `<20171222192732.13188-1-pablo@netfilter.org>`). This version added IPv6 and mixed IPv4/IPv6 support, hardware offload in a separate `nf_flow_table_hw` module, port translation and netns support. The hardware patch was tagged RFC, and the cover letter says "I will keep this one back until there's an initial driver that introduces flow table offload support". +- **22 December 2017**, [`[PATCH nf-next,v3 0/7] Flow offload infrastructure`](https://lwn.net/Articles/742164/) (Message-ID `<20171222192732.13188-1-pablo@netfilter.org>`). The cover letter lists IPv6 and mixed IPv4/IPv6 support, hardware offload in a separate `nf_flow_table_hw` module, port translation and netns support. The hardware patch was tagged RFC, and the cover letter says "I will keep this one back until there's an initial driver that introduces flow table offload support". + +The software flow table was committed on 8 January 2018 ([`ac2a66665e23`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ac2a66665e231847cab11b8c8e844ce43207dd2e) adds the generic infrastructure and [`a3c90f7a2323`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a3c90f7a2323b331ae816d5b0633e68148e25d04) the nftables expression) and first appeared in v4.16. Felix Fietkau reorganised the code in a series written in February 2018 and merged for v4.18, for example in [`1a999d899bc2`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1a999d899bc26abee26d70c10762c81b9d684d2b), which renamed `nf_flow_table.c` to `nf_flow_table_core.c`. -The software flow table was merged on 8 January 2018 ([`ac2a66665e23`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ac2a66665e231847cab11b8c8e844ce43207dd2e) adds the generic infrastructure and [`a3c90f7a2323`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a3c90f7a2323b331ae816d5b0633e68148e25d04) the nftables expression) and first appeared in v4.16. Felix Fietkau reorganised the code in a series written in February 2018 and merged for v4.18, for example in [`1a999d899bc2`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1a999d899bc26abee26d70c10762c81b9d684d2b), which renamed `nf_flow_table.c` to `nf_flow_table_core.c`. Hardware offload reached the tree in v5.5 through the tc flow-block interface, with the indirect path following in v5.7, rather than through the `ndo` hooks proposed in the RFC. +Hardware offload reached the tree in v5.5 through the tc flow-block interface rather than through the dedicated `ndo` hooks proposed in the RFC. Pablo Neira Ayuso added it in [`c29f74e0df7a`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c29f74e0df7a02b8303bcdce93a7c0132d62577a), which called `ndo_setup_tc` with `TC_SETUP_BLOCK` and the callbacks with `TC_SETUP_FT`. The arrangement described above came from two fixes by wenxu in the same release, [`ee1bcfe01251`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ee1bcfe01251e8958b53de45d2b0c85e09dd2719) and [`e052901991ae`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e052901991ae21e15851bfc89c682bfcb39a4dcf). wenxu added the indirect path in [`b5140a36da78`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b5140a36da7876bc084a2c680c8dbc7438db2051) (v5.7). ## Differences from `nf_flowtable.rst` -The kernel documentation was last changed in March 2021 (v5.13, [`143490cde566`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=143490cde5669e0151dff466a7c2cf70e2884fb7)). As of v7.4-rc3, at least these statements in it no longer match the source: +The kernel documentation was last updated with new content in March 2021 (v5.13, [`143490cde566`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=143490cde5669e0151dff466a7c2cf70e2884fb7)); a build-warning fix followed the next day. As of v7.4-rc3, at least these statements in it no longer match the source: - It says TCP RST and FIN packets are passed up to the classic path. Since [`fdbaf5163331`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fdbaf5163331342e90a2c29b87629021f4c15f0c) ("netfilter: flowtable: add CLOSING state", v6.14), they stay on the fast path and the flow enters the closing state. The commit message explains that the old behaviour "leads to packet races when restoring the conntrack state as well as late flow re-offload situations". -- It says hits are sent with `neigh_xmit()`. Direct mode has called `dev_queue_xmit()` since [`7a27f6ab4135`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7a27f6ab41356ecba47ec2bec6d635704c169779) (v5.13, the same day as the documentation update), and [`b5964aac51e0`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b5964aac51e0c286a50e68225e0dfcf11fb554cb) ("netfilter: flowtable: consolidate xmit path", v6.19) moved neighbour mode to it too. As a result the netdev egress hook still runs for hits, which the documentation's "you do not see these packets from any of the Netfilter hooks coming after ingress" does not cover. +- It says hits are sent with `neigh_xmit()`. Direct mode has called `dev_queue_xmit()` since [`7a27f6ab4135`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7a27f6ab41356ecba47ec2bec6d635704c169779) (v5.13, the same day as the documentation update), and [`b5964aac51e0`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b5964aac51e0c286a50e68225e0dfcf11fb554cb) ("netfilter: flowtable: consolidate xmit path", v6.19) moved neighbour mode to it too. +- It says packets are not seen "from any of the Netfilter hooks coming after ingress". The netdev egress hook, added in [`42df6e1d221d`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=42df6e1d221dddc0f2acf2be37e68d553ad65f96) (v5.16, after the documentation was written), does see them, because every hit ends in `dev_queue_xmit()`. - It says the rule adds the flow on "the TCP syn-ack packet". The code requires an established and assured conntrack entry. [`e5eaac2beb54`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e5eaac2beb54f0a16ff851125082d9faeb475572) (v5.18) added that check, and its message says the patch makes `nft_flow_offload` "skip packets in SYN_RECV state" and "postpones offload up to the next packet". - It says the flowtable supports TCP and UDP. GRE has been accepted since [`4e8d9584d154`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4e8d9584d154479d357327f76d4e49486915c9c9) (v5.18). From 8f43eea3069e511d61eea6ed94446f425f23b5a1 Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 18:19:55 -0800 Subject: [PATCH 08/22] net: correct flowtable placement, hardware and history details --- docs/net/flowtable.md | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index 1faef9cc..d4c6b566 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -8,7 +8,7 @@ This page describes the code as of v7.4-rc3. Behaviour that changed recently is A router or NAT gateway sends every forwarded packet through prerouting, the routing decision, forward and postrouting, plus conntrack and NAT. Flowtable skips that work for packets of a flow it already knows. The 2017 proposal that introduced it reported the software fast path as faster than the classic path in the author's testbed ([below](#where-it-came-from)). -The kernel's [flowtable documentation](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst) describes it like this: once the first packets of a flow have gone through the normal forwarding path, a rule can add the flow to a table. The table is consulted at the ingress hook. A packet that matches an entry is rewritten and sent straight out, skipping the hooks after ingress. A packet that doesn't match follows the classic path. +The kernel's [flowtable documentation](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst) describes it like this: once the first packets of a flow have gone through the normal forwarding path, a rule can add the flow to a table. The table is consulted at the ingress hook (XDP programs can also look it up with the `bpf_xdp_flow_lookup` kfunc, added in [`391bb6594fd3`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=391bb6594fd3a567efb1cd3efc8136c78c4c9e31), v6.11). A packet that matches an entry is rewritten and sent straight out, skipping the hooks after ingress. A packet that doesn't match follows the classic path. ``` ingress hook @@ -26,7 +26,7 @@ The kernel's [flowtable documentation](https://git.kernel.org/pub/scm/linux/kern A `flow add` rule in the forward chain of the classic path is what inserts a flow into the table. Only flows you select are added; nothing is offloaded unless a rule says so. -In the current code, a hit skips prerouting, forward and, except for IPsec flows, postrouting. It does not skip everything. The tc ingress hook runs before the flowtable. Every hit ends in `dev_queue_xmit()`, so the netdev egress hook, tc egress and the qdisc still run on transmit. +In the current code, a hit skips prerouting, forward and, except for IPsec flows, postrouting. It does not skip everything. The tc ingress hook runs before the flowtable. Software hits that aren't IPsec flows end in `dev_queue_xmit()`, so the netdev egress hook, tc egress and the qdisc still run on transmit. Packets that hardware forwards never reach the CPU, so they pass none of these. The same infrastructure is also used by the tc `ct` action (`net/sched/act_ct.c`); this page covers the nftables side. @@ -49,13 +49,13 @@ table inet x { `flow add @f` is the nftables statement that calls `nft_flow_offload_eval()` in `net/netfilter/nft_flow_offload.c`. It is accepted only in a chain on the forward hook (`nft_flow_offload_validate()`). The example rule matches IPv4 TCP only; match on the protocols you want offloaded. -Placement matters. The packet that adds the flow keeps going down the chain, but once the flow is in the table, later packets skip the rest of the chain. The `counter` rule in the example stops counting them, and so would any limit, mark or DSCP rule placed after the `flow add` rule. +Once a flow is in the table, its later packets are handled at ingress and never reach the forward chain at all. Every rule in the prerouting, forward and postrouting hooks stops seeing them, whether it sits before or after the `flow add` rule: the `counter` rule in the example stops counting, and so would a limit, mark or DSCP rule. Placement decides only which packets reach `flow add` in the first place: a rule earlier in the chain that accepts or drops a packet means `flow add` never runs for it. The packet that adds the flow keeps going down the chain. -The flowtable has its own `counter` flag. Without it, conntrack's packet and byte counts stop updating for packets that take the fast path. +The flowtable has its own `counter` flag. Without it, conntrack's packet and byte counts stop updating for packets that take the fast path. With it, they update only when conntrack accounting is enabled (`net.netfilter.nf_conntrack_acct`). The kernel documentation recommends a lower priority number than any nftables ingress chain on the same device, so that the flowtable runs first. That is a choice: an ingress chain that runs after the flowtable never sees offloaded packets, and one that runs before it sees all of them that reach the CPU. -Except for IPsec routes, the real lower device behind any VLAN, PPPoE or bridge device has to be in the flowtable's device list, in both directions, or the flow isn't added. The kernel documentation says the real device is sufficient and the VLAN and PPPoE devices need not be listed; in the code it is required. +Except for IPsec routes, the real lower device behind any VLAN, PPPoE or bridge device has to be in the flowtable's device list, in both directions, or the flow isn't added. As the kernel documentation says, listing the real device is enough and the VLAN and PPPoE devices need not be listed. The code also requires it: listing only a VLAN, PPPoE or bridge device is not enough. ## How a flow gets in @@ -70,7 +70,7 @@ If all of that holds, the kernel resolves the path in both directions (it reuses It then allocates a `struct flow_offload` that holds a reference to the conntrack entry, copies the SNAT and DNAT flags from the conntrack status, and inserts the entry. The entry has two tuples, one per direction, in one resizable hash table. The comment in `flow_offload_add()` notes that garbage collection walks only the original-direction entries, so that one is inserted last. -For TCP, `flow_offload_ct_tcp()` also sets the "be liberal" flag on the conntrack entry. The comment there gives the reason: conntrack will not see all packets any more, so it can't validate the TCP window. Nothing in the flowtable code clears that flag, so window tracking stays off for the connection for the rest of its life, including after it returns to conntrack. +For TCP, `flow_offload_ct_tcp()` also sets the "be liberal" flag on the conntrack entry. The comment there gives the reason: conntrack will not see all packets any more, so it can't validate the TCP window. Nothing in the flowtable code clears that flag, so window tracking normally stays off for the rest of the connection's life, including after it returns to conntrack. ## What a hit does @@ -92,25 +92,25 @@ The IPv6 hook, `nf_flow_offload_ipv6_hook()`, does the same, with the hop limit The garbage-collection worker, `nf_flow_offload_work_gc()`, runs once a second and removes entries that have been torn down. Teardown is triggered by: - **A timeout.** Each entry gets a deadline when it is added. For software flows every hit pushes the deadline out, at most once a second. The default is 30 seconds for TCP, UDP and GRE (GRE has no sysctl); the TCP and UDP values are the sysctls `net.netfilter.nf_flowtable_tcp_timeout` and `net.netfilter.nf_flowtable_udp_timeout` (`offload_timeout` in `nf_conntrack_proto_tcp.c` and `nf_conntrack_proto_udp.c`). Packets of a hardware flow mostly bypass the hook, so the collector asks the driver for statistics, once less than 90% of the timeout remains, and extends the deadline from the last-used time. -- **A dying conntrack entry or a stale route.** The collector tears the flow down. The hook tears the flow down, and drops that packet, when the egress device no longer exists or no neighbour entry can be found or created. +- **A dying conntrack entry or a stale route.** The collector tears the flow down (for a stale route only since v7.3, [`edd51a233438`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=edd51a23343870dbd7cedf6e2765c13cf7a5ccbc); before that only the hook noticed one). The hook tears the flow down, and drops that packet, when the egress device no longer exists or no neighbour entry can be found or created. - **A device going down.** The `NETDEV_DOWN` notifier calls `nf_flow_table_cleanup()`. -TCP FIN and RST don't end the flow at once. The hook marks the flow `NF_FLOW_CLOSING` and the packet still takes the fast path. The flow stays in the software table, but a hardware entry is removed early. A later SYN on a closing flow tears it down, sets the conntrack state to close, and sends that packet to the classic path so conntrack can handle the reconnect. Otherwise the flow leaves by timeout. +TCP FIN and RST don't end the flow at once (since v6.14). The hook marks the flow `NF_FLOW_CLOSING` and the packet still takes the fast path. The flow stays in the software table, but a hardware entry is removed early. A later SYN on a closing flow tears it down, sets the conntrack state to close, and sends that packet to the classic path so conntrack can handle the reconnect. Otherwise the flow leaves by timeout. -Teardown clears the offload bit on the conntrack entry. The flow can be added again once the collector has removed the old entry and a later forward-chain packet matches the `flow add` rule. A TCP flow torn down by a SYN is in the close state, so it isn't added again until it is established. +Teardown clears the offload bit on the conntrack entry. A later forward-chain packet that matches the `flow add` rule can add the flow again. A TCP flow torn down by a SYN is in the close state, so it isn't added again until it is established. While a flow is in the table, conntrack doesn't see its packets, so its timeout would run out. `flow_offload_add()` first sets it to a day. After that the garbage collector calls `nf_flow_table_extend_ct_timeout()`, which pushes it forward when less than five minutes remain. The new value depends on the protocol and state: | Flow | Conntrack timeout is extended to | |------|----------------------------------| -| UDP, GRE (GRE only since the v7.4 release candidates) | a day | +| UDP, GRE | a day | | TCP established | a day | | TCP FIN_WAIT, CLOSE_WAIT, LAST_ACK, TIME_WAIT | five minutes | | TCP SYN_SENT, SYN_RECV, CLOSE | no extension | -The function's comment calls the value "an arbitrary timeout large enough not to ever expire", which saves a check of the offload bit in the packet path. +The extension logic dates from [`03428ca5cee9`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=03428ca5cee9f0792edc996c06ce4514816af1fb) (v6.14). It did not handle GRE: an offloaded GRE flow hit a `WARN_ON_ONCE()` and got a one-jiffy timeout until [`46ecda278f6e`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=46ecda278f6eedffb2898d41e3e56757c996a41f) (v7.4-rc3). The function's comment calls the value "an arbitrary timeout large enough not to ever expire", which saves a check of the offload bit in the packet path. -When a flow is torn down, `flow_offload_fixup_ct()` hands it back to conntrack. For TCP it keeps the last state conntrack recorded (or sets close if the flow was closing). It clears the recorded maximum window and the max-ack flag, because otherwise, as the code comment says, conntrack's TCP reset validation would fail. TCP and UDP flows also have their conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired, and a closing TCP flow gets the close timeout. The timeout is only ever lowered, so an established TCP entry, whose normal timeout is five days, keeps whatever is left of its day. GRE flows are left as they are, so a GRE conntrack entry can keep a timeout of up to a day after it leaves the table. +When a flow is torn down, `flow_offload_fixup_ct()` hands it back to conntrack. For TCP it keeps the last state conntrack recorded (or sets close if the flow was closing). It clears the recorded maximum window and the max-ack flag. As the code comment says, without clearing the max-ack flag conntrack's TCP reset validation would fail. TCP and UDP flows also have their conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired, and a closing TCP flow gets the close timeout. The timeout is only ever lowered, so an established TCP entry, whose normal timeout is five days, keeps whatever is left of its day. GRE flows are left as they are, so a GRE conntrack entry can keep a timeout of up to a day after it leaves the table. ## Hardware offload @@ -118,7 +118,7 @@ If the device driver supports it, the flowtable can push entries to the NIC or s The driver interface is the tc flow-block interface: -- **Binding.** For a flowtable with `flags offload`, registering its ingress hook on a device first calls the flowtable type's `setup` callback, `nf_flow_table_offload_setup()`, which binds a flow block to that device. If the device has `ndo_setup_tc`, the flowtable calls it with `TC_SETUP_FT`. Otherwise it goes through the indirect-block path, `flow_indr_dev_setup_offload()`. The driver adds its callback to the flowtable's block. +- **Binding.** Registering a flowtable's ingress hook on a device first calls the flowtable type's `setup` callback, `nf_flow_table_offload_setup()`. Without `flags offload` that only sets up the XDP path. With it, the callback binds a flow block to that device. If the device has `ndo_setup_tc`, the flowtable calls it with `TC_SETUP_FT`. Otherwise it goes through the indirect-block path, `flow_indr_dev_setup_offload()`. The driver adds its callback to the flowtable's block. - **Per flow.** `nf_flow_table_offload.c` calls every callback with `TC_SETUP_CLSFLOWER` and a `flow_cls_offload` request, the same request type tc flower uses. The command is `FLOW_CLS_REPLACE` to add a flow (carrying a flow rule: match keys plus actions such as header rewrites), `FLOW_CLS_DESTROY` to remove it or `FLOW_CLS_STATS` to read its counters. - **Drivers.** The in-tree drivers that handle `TC_SETUP_FT` are the mlx5 representor code, the MediaTek Ethernet and WED drivers, the mt76 Wi-Fi driver and the Airoha Ethernet driver. DSA user ports pass the command to the conduit device, and `netdevsim` accepts it but registers no callback. @@ -126,22 +126,23 @@ Some operator-visible consequences: - A device that can't offload makes the flowtable fail to load: the `setup` error is returned before the hook is registered. The `offload` flag can't be toggled on an existing flowtable. - With `flags offload`, every Ethernet path uses direct mode, so a flow without a tunnel caches no route and a route change isn't noticed until the flow goes idle. -- Flows that go through an IP-in-IP tunnel stay in software: [`6c5dcab95f4c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6c5dcab95f4cd42a1648739ec9300fbb4b1a021f) (v7.2) skips the hardware offload for them. +- Flows that go through an IPIP or IPv6-in-IPv6 tunnel stay in software: [`6c5dcab95f4c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6c5dcab95f4cd42a1648739ec9300fbb4b1a021f) (v7.2) skips the hardware offload for them. +- A hardware add that fails isn't reported: the flow keeps running in software. `[HW_OFFLOAD]` is set if the driver accepted at least one direction. - The collector skips an entry whose hardware work is still queued (`NF_FLOW_PENDING`). ## Inspecting `conntrack -L` shows `[OFFLOAD]` for flows in the software table and `[HW_OFFLOAD]` for flows in hardware, as the kernel documentation describes. For an offloaded entry the kernel leaves out the timeout and protocol state: ctnetlink skips them (`nf_conntrack_netlink.c`) and `/proc/net/nf_conntrack` skips the timeout and the TCP state (`nf_conntrack_standalone.c`, `nf_conntrack_proto_tcp.c`). -With `CONFIG_NF_FLOW_TABLE_PROCFS`, `/proc/net/stat/nf_flowtable` shows per-CPU counts of queued hardware add, delete and stats work. It has no hit or miss counters. The two timeout sysctls are described above. +With `CONFIG_NF_FLOW_TABLE_PROCFS`, `/proc/net/stat/nf_flowtable` shows per-CPU counters of pending (queued, not yet completed) hardware add, delete and stats work; sum them across CPUs. It has no hit or miss counters. The two timeout sysctls are described above. ## Limits - Changing or removing rules does not evict flows that are already in the table, including the `flow add` rule itself. A hit never reaches the forward chain, and every software hit pushes the deadline out, so a busy flow doesn't expire. A new drop, reject, rate-limit or mark rule in the forward chain doesn't apply to such flows either; only the stored NAT rewrite is applied. Flows leave when they go idle, when their conntrack entry is deleted (for example with `conntrack -D`), when a device goes down or the route goes stale, or when the flowtable is deleted. A reload that starts with `flush ruleset` deletes the flowtable, which tears down every flow in it, so they are evaluated again. -- Fragments, and packets that exceed the MTU, go through the classic path. The kernel documentation gives this as the reason: the transport header is missing for fragments, so no lookup is possible. Packets with IPv4 options, a TTL or hop limit of 1 or less, or GRE other than version 0 also go to the classic path. The commit that extended the GRE timeout, [`46ecda278f6e`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=46ecda278f6eedffb2898d41e3e56757c996a41f) (October 2026, in the v7.4 release candidates), notes that `ip6gre` tunnels with the default encapsulation limit carry a Destination Options header before GRE, which the flowtable does not parse, so their packets all take the classic path. +- Fragments, and packets that exceed the MTU, go through the classic path. The kernel documentation gives this as the reason: the transport header is missing for fragments, so no lookup is possible. Packets with IPv4 options, a TTL or hop limit of 1 or less, or GRE other than version 0 also go to the classic path. The commit that extended the GRE timeout, [`46ecda278f6e`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=46ecda278f6eedffb2898d41e3e56757c996a41f) (v7.4-rc3), notes that `ip6gre` tunnels with the default encapsulation limit carry a Destination Options header before GRE, which the flowtable does not parse, so their packets all take the classic path. - The table is a cache. The kernel documentation warns that an entry can go stale if the destination MAC address or the egress device changes. It names two risky setups: software mode combined with bridging and IP forwarding, and hardware offload. - The IP-layer hooks (prerouting and forward, and postrouting for flows without IPsec) don't see offloaded packets. A rule there, such as a counter or a log, won't match them. -- IPsec: packets that carry a security path are never added. A flow whose route has an IPsec transform goes out through `dst_output()`, so postrouting still runs, and the device-list and neighbour checks don't apply to it. +- IPsec: packets that carry a security path never trigger an add. A flow whose route has an IPsec transform goes out through `dst_output()`, so postrouting still runs, and the device-list and neighbour checks don't apply to it. ## Where it came from @@ -168,6 +169,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, - [`Documentation/networking/nf_flowtable.rst`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst) — the kernel's own description, with the VLAN, PPPoE, bridge and counter details and the versions that added them - [`net/netfilter/nft_flow_offload.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c) — the `flow add` statement +- [`net/netfilter/nf_flow_table_path.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c) — route and device resolution when a flow is added - [`net/netfilter/nf_flow_table_core.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c) — the table, timeouts and garbage collection - [`net/netfilter/nf_flow_table_ip.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c) — the IPv4 and IPv6 fast path - [`net/netfilter/nf_flow_table_offload.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c) — hardware offload From 080f186c8c2544aaeaf7df16712f6b3deb4a5d24 Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 18:42:03 -0800 Subject: [PATCH 09/22] net: fix flowtable GRE history and hook wording --- docs/net/flowtable.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index d4c6b566..2949e834 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -8,7 +8,7 @@ This page describes the code as of v7.4-rc3. Behaviour that changed recently is A router or NAT gateway sends every forwarded packet through prerouting, the routing decision, forward and postrouting, plus conntrack and NAT. Flowtable skips that work for packets of a flow it already knows. The 2017 proposal that introduced it reported the software fast path as faster than the classic path in the author's testbed ([below](#where-it-came-from)). -The kernel's [flowtable documentation](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst) describes it like this: once the first packets of a flow have gone through the normal forwarding path, a rule can add the flow to a table. The table is consulted at the ingress hook (XDP programs can also look it up with the `bpf_xdp_flow_lookup` kfunc, added in [`391bb6594fd3`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=391bb6594fd3a567efb1cd3efc8136c78c4c9e31), v6.11). A packet that matches an entry is rewritten and sent straight out, skipping the hooks after ingress. A packet that doesn't match follows the classic path. +The kernel's [flowtable documentation](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst) describes it like this: once the first packets of a flow have gone through the normal forwarding path, a rule can add the flow to a table. The table is consulted at the ingress hook. A packet that matches an entry is rewritten and sent straight out, skipping the hooks after ingress. A packet that doesn't match follows the classic path. For flowtables without `flags offload`, XDP programs can also look the table up with the `bpf_xdp_flow_lookup` kfunc (added in [`391bb6594fd3`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=391bb6594fd3a567efb1cd3efc8136c78c4c9e31), v6.11). ``` ingress hook @@ -49,9 +49,9 @@ table inet x { `flow add @f` is the nftables statement that calls `nft_flow_offload_eval()` in `net/netfilter/nft_flow_offload.c`. It is accepted only in a chain on the forward hook (`nft_flow_offload_validate()`). The example rule matches IPv4 TCP only; match on the protocols you want offloaded. -Once a flow is in the table, its later packets are handled at ingress and never reach the forward chain at all. Every rule in the prerouting, forward and postrouting hooks stops seeing them, whether it sits before or after the `flow add` rule: the `counter` rule in the example stops counting, and so would a limit, mark or DSCP rule. Placement decides only which packets reach `flow add` in the first place: a rule earlier in the chain that accepts or drops a packet means `flow add` never runs for it. The packet that adds the flow keeps going down the chain. +Once a flow is in the table, its later packets that hit the table are handled at ingress and never reach the forward chain at all. Every rule in the prerouting, forward and, except for IPsec flows, postrouting hooks stops seeing them, whether it sits before or after the `flow add` rule: the `counter` rule in the example stops counting, and so would a limit, mark or DSCP rule. Placement decides only which packets reach `flow add` in the first place: a rule earlier in the chain that accepts or drops a packet means `flow add` never runs for it. The packet that adds the flow keeps going down the chain. -The flowtable has its own `counter` flag. Without it, conntrack's packet and byte counts stop updating for packets that take the fast path. With it, they update only when conntrack accounting is enabled (`net.netfilter.nf_conntrack_acct`). +The flowtable has its own `counter` flag. Without it, conntrack's packet and byte counts stop updating for packets that take the fast path. With it, they update only for conntrack entries created while accounting was enabled (`net.netfilter.nf_conntrack_acct`). The kernel documentation recommends a lower priority number than any nftables ingress chain on the same device, so that the flowtable runs first. That is a choice: an ingress chain that runs after the flowtable never sees offloaded packets, and one that runs before it sees all of them that reach the CPU. @@ -108,7 +108,7 @@ While a flow is in the table, conntrack doesn't see its packets, so its timeout | TCP FIN_WAIT, CLOSE_WAIT, LAST_ACK, TIME_WAIT | five minutes | | TCP SYN_SENT, SYN_RECV, CLOSE | no extension | -The extension logic dates from [`03428ca5cee9`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=03428ca5cee9f0792edc996c06ce4514816af1fb) (v6.14). It did not handle GRE: an offloaded GRE flow hit a `WARN_ON_ONCE()` and got a one-jiffy timeout until [`46ecda278f6e`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=46ecda278f6eedffb2898d41e3e56757c996a41f) (v7.4-rc3). The function's comment calls the value "an arbitrary timeout large enough not to ever expire", which saves a check of the offload bit in the packet path. +The extension logic dates from [`03428ca5cee9`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=03428ca5cee9f0792edc996c06ce4514816af1fb) (v6.14) but never took effect until [`53b3e60edb67`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=53b3e60edb674b442b2b3bbdba484667b0f47a5d) (v7.2): it compared a relative value with the absolute `ct->timeout`, so the update never succeeded. It also did not handle GRE. Once a GRE entry's timeout fell below five minutes it hit a `WARN_ON_ONCE()`, and from v7.2 it also got a one-jiffy timeout, until [`46ecda278f6e`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=46ecda278f6eedffb2898d41e3e56757c996a41f) (v7.4-rc3). The function's comment calls the value "an arbitrary timeout large enough not to ever expire", which saves a check of the offload bit in the packet path. When a flow is torn down, `flow_offload_fixup_ct()` hands it back to conntrack. For TCP it keeps the last state conntrack recorded (or sets close if the flow was closing). It clears the recorded maximum window and the max-ack flag. As the code comment says, without clearing the max-ack flag conntrack's TCP reset validation would fail. TCP and UDP flows also have their conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired, and a closing TCP flow gets the close timeout. The timeout is only ever lowered, so an established TCP entry, whose normal timeout is five days, keeps whatever is left of its day. GRE flows are left as they are, so a GRE conntrack entry can keep a timeout of up to a day after it leaves the table. @@ -127,7 +127,7 @@ Some operator-visible consequences: - A device that can't offload makes the flowtable fail to load: the `setup` error is returned before the hook is registered. The `offload` flag can't be toggled on an existing flowtable. - With `flags offload`, every Ethernet path uses direct mode, so a flow without a tunnel caches no route and a route change isn't noticed until the flow goes idle. - Flows that go through an IPIP or IPv6-in-IPv6 tunnel stay in software: [`6c5dcab95f4c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6c5dcab95f4cd42a1648739ec9300fbb4b1a021f) (v7.2) skips the hardware offload for them. -- A hardware add that fails isn't reported: the flow keeps running in software. `[HW_OFFLOAD]` is set if the driver accepted at least one direction. +- A hardware add that fails isn't reported: the flow keeps running in software, and the add is retried on later software hits. `[HW_OFFLOAD]` is set if the driver accepted at least one direction. - The collector skips an entry whose hardware work is still queued (`NF_FLOW_PENDING`). ## Inspecting @@ -161,7 +161,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, - It says TCP RST and FIN packets are passed up to the classic path. Since [`fdbaf5163331`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fdbaf5163331342e90a2c29b87629021f4c15f0c) ("netfilter: flowtable: add CLOSING state", v6.14), they stay on the fast path and the flow enters the closing state. The commit message explains that the old behaviour "leads to packet races when restoring the conntrack state as well as late flow re-offload situations". - It says hits are sent with `neigh_xmit()`. Direct mode has called `dev_queue_xmit()` since [`7a27f6ab4135`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7a27f6ab41356ecba47ec2bec6d635704c169779) (v5.13, the same day as the documentation update), and [`b5964aac51e0`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b5964aac51e0c286a50e68225e0dfcf11fb554cb) ("netfilter: flowtable: consolidate xmit path", v6.19) moved neighbour mode to it too. -- It says packets are not seen "from any of the Netfilter hooks coming after ingress". The netdev egress hook, added in [`42df6e1d221d`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=42df6e1d221dddc0f2acf2be37e68d553ad65f96) (v5.16, after the documentation was written), does see them, because every hit ends in `dev_queue_xmit()`. +- It says packets are not seen "from any of the Netfilter hooks coming after ingress". The netdev egress hook, added in [`42df6e1d221d`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=42df6e1d221dddc0f2acf2be37e68d553ad65f96) (v5.16, after the documentation was written), does see them, because every software hit that isn't an IPsec flow ends in `dev_queue_xmit()`. - It says the rule adds the flow on "the TCP syn-ack packet". The code requires an established and assured conntrack entry. [`e5eaac2beb54`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e5eaac2beb54f0a16ff851125082d9faeb475572) (v5.18) added that check, and its message says the patch makes `nft_flow_offload` "skip packets in SYN_RECV state" and "postpones offload up to the next packet". - It says the flowtable supports TCP and UDP. GRE has been accepted since [`4e8d9584d154`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4e8d9584d154479d357327f76d4e49486915c9c9) (v5.18). From a32f57e530fdb8461056d644080a982dcc252fdb Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 18:59:54 -0800 Subject: [PATCH 10/22] net: cite sources for every statement on the flowtable page Link each behaviour claim to the function or line at v7.4-rc3, each doc claim to nf_flowtable.rst, and each history claim to its commit. Make IPsec handling per direction, mark the v7.2 device-list change, and move the GRE timeout history into its own note. --- docs/net/flowtable.md | 145 ++++++++++++++++++++++-------------------- 1 file changed, 77 insertions(+), 68 deletions(-) diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index 2949e834..1be19bc6 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -2,13 +2,11 @@ > How the kernel lets established forwarded connections skip most of the netfilter hook path -This page describes the code as of v7.4-rc3. Behaviour that changed recently is marked with the release that introduced it. +Source links point at Linux v7.4-rc3, the tree this page was checked against. Behaviour that changed recently is marked with the release that introduced it and links to the commit. "The kernel documentation" is [`nf_flowtable.rst`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3). ## What it does -A router or NAT gateway sends every forwarded packet through prerouting, the routing decision, forward and postrouting, plus conntrack and NAT. Flowtable skips that work for packets of a flow it already knows. The 2017 proposal that introduced it reported the software fast path as faster than the classic path in the author's testbed ([below](#where-it-came-from)). - -The kernel's [flowtable documentation](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst) describes it like this: once the first packets of a flow have gone through the normal forwarding path, a rule can add the flow to a table. The table is consulted at the ingress hook. A packet that matches an entry is rewritten and sent straight out, skipping the hooks after ingress. A packet that doesn't match follows the classic path. For flowtables without `flags offload`, XDP programs can also look the table up with the `bpf_xdp_flow_lookup` kfunc (added in [`391bb6594fd3`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=391bb6594fd3a567efb1cd3efc8136c78c4c9e31), v6.11). +The kernel documentation describes the design: once the first packet of a flow has gone through the IP forwarding path, a rule can add the flow to a table ([nf_flowtable.rst:15-19](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n15)). A packet that finds a matching entry is transmitted without the classic forwarding path, and a packet that doesn't follows the classic path ([nf_flowtable.rst:20-26](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n20)). The lookup runs in a hook at ingress on the devices listed in the flowtable ([`nf_flow_offload_ip_hook()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n904) for IPv4, [`nf_flow_offload_ipv6_hook()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1233) for IPv6). ``` ingress hook @@ -24,15 +22,20 @@ The kernel's [flowtable documentation](https://git.kernel.org/pub/scm/linux/kern └───────────────────┘ └───────────────────────┘ ``` -A `flow add` rule in the forward chain of the classic path is what inserts a flow into the table. Only flows you select are added; nothing is offloaded unless a rule says so. +The diagram is simplified from Fig. 1 of the kernel documentation ([nf_flowtable.rst:37-70](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n37)). In that figure a `flow add` rule in the forward chain of the classic path is what inserts a flow into the table. + +Which hooks a hit passes, from the source: -In the current code, a hit skips prerouting, forward and, except for IPsec flows, postrouting. It does not skip everything. The tc ingress hook runs before the flowtable. Software hits that aren't IPsec flows end in `dev_queue_xmit()`, so the netdev egress hook, tc egress and the qdisc still run on transmit. Packets that hardware forwards never reach the CPU, so they pass none of these. +- The tc ingress hook runs before the netfilter ingress hook where the flowtable sits ([dev.c:6290](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c?h=v7.4-rc3#n6290), [dev.c:6298](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c?h=v7.4-rc3#n6298)). +- A software hit is transmitted with `dev_queue_xmit()` ([`__nf_flow_queue_xmit()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n776)), which runs the netfilter egress hook and tc egress ([dev.c:5024](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c?h=v7.4-rc3#n5024), [dev.c:5032](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c?h=v7.4-rc3#n5032)). +- A hit whose route has an IPsec transform is transmitted with `dst_output()` ([`nf_flow_xmit_xfrm()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n300)), which runs the postrouting hook ([xfrm4_output.c:33](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/xfrm4_output.c?h=v7.4-rc3#n33)). +- The kernel documentation says hits are not seen by "any of the Netfilter hooks coming after ingress" ([nf_flowtable.rst:22-23](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n22)). The source differs; see [Differences](#differences-from-nf_flowtablerst). -The same infrastructure is also used by the tc `ct` action (`net/sched/act_ct.c`); this page covers the nftables side. +The tc `ct` action uses the same infrastructure ([act_ct.c:55](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/sched/act_ct.c?h=v7.4-rc3#n55)). This page covers the nftables side. ## Turning it on -The kernel documentation's example is a flowtable on two devices and one rule in the forward chain: +The kernel documentation's example is a flowtable on two devices and one rule in the forward chain ([nf_flowtable.rst:87-98](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n87)): ``` table inet x { @@ -47,106 +50,110 @@ table inet x { } ``` -`flow add @f` is the nftables statement that calls `nft_flow_offload_eval()` in `net/netfilter/nft_flow_offload.c`. It is accepted only in a chain on the forward hook (`nft_flow_offload_validate()`). The example rule matches IPv4 TCP only; match on the protocols you want offloaded. +- **The statement.** `flow add @f` is evaluated by [`nft_flow_offload_eval()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n50). It is accepted only in a chain on the forward hook ([`nft_flow_offload_validate()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n138)). The example rule matches IPv4 TCP. +- **What a hit skips.** A hit is consumed at ingress with `NF_STOLEN` ([`nf_flow_queue_xmit()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n822)), so a flow's later packets don't reach the prerouting, forward and postrouting chains. This is why the kernel documentation says the `counter` rule stops being updated ([nf_flowtable.rst:105-109](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n105)). A rule that gives a packet a verdict earlier in the chain means `flow add` never runs for it. The packet that adds the flow continues down the chain, because [`nft_flow_offload_eval()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n50) sets `NFT_BREAK` only on failure. +- **The `counter` flag.** Fast-path packets update conntrack's counters only if the flowtable has the `counter` flag ([nf_flow_table_ip.c:520](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n520); documented in [nf_flowtable.rst:165-181](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n172)). The update takes effect only for conntrack entries that have the accounting extension ([nf_conntrack_acct.h:36](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_conntrack_acct.h?h=v7.4-rc3#n36)), which is added when the sysctl `net.netfilter.nf_conntrack_acct` is set ([nf_conntrack_core.c:1789](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_core.c?h=v7.4-rc3#n1789)). +- **Priority.** The kernel documentation advises a flowtable priority number smaller than that of any nftables ingress chain on the same device ([nf_flowtable.rst:99-103](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n99)). Netfilter runs lower numbers first, and a hit returns `NF_STOLEN`, so a chain that runs after the flowtable doesn't see offloaded packets. +- **Device list.** Path resolution follows VLAN, PPPoE and bridge devices down to the real device ([`nft_dev_fill_forward_path()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n45)) and requires that device to be in the flowtable's list ([nf_flow_table_path.c:198](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n198)). The check runs for both directions and is skipped for IPsec routes ([nf_flow_table_path.c:314-320](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n314)). The kernel documentation says the real device is sufficient and the VLAN and PPPoE devices need not be listed ([nf_flowtable.rst:129-130](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n129)). Since v7.2 ([`871df5007eda`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=871df5007edaacaf2eac58a2871af6044a444ad4)), a failed path discovery makes `flow add` give up; the commit message says "bail out to classic forwarding path rather than providing incomplete forwarding path". -Once a flow is in the table, its later packets that hit the table are handled at ingress and never reach the forward chain at all. Every rule in the prerouting, forward and, except for IPsec flows, postrouting hooks stops seeing them, whether it sits before or after the `flow add` rule: the `counter` rule in the example stops counting, and so would a limit, mark or DSCP rule. Placement decides only which packets reach `flow add` in the first place: a rule earlier in the chain that accepts or drops a packet means `flow add` never runs for it. The packet that adds the flow keeps going down the chain. - -The flowtable has its own `counter` flag. Without it, conntrack's packet and byte counts stop updating for packets that take the fast path. With it, they update only for conntrack entries created while accounting was enabled (`net.netfilter.nf_conntrack_acct`). +## How a flow gets in -The kernel documentation recommends a lower priority number than any nftables ingress chain on the same device, so that the flowtable runs first. That is a choice: an ingress chain that runs after the flowtable never sees offloaded packets, and one that runs before it sees all of them that reach the CPU. +[`nft_flow_offload_eval()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n50) runs on a packet in the forward path and checks: -Except for IPsec routes, the real lower device behind any VLAN, PPPoE or bridge device has to be in the flowtable's device list, in both directions, or the flow isn't added. As the kernel documentation says, listing the real device is enough and the VLAN and PPPoE devices need not be listed. The code also requires it: listing only a VLAN, PPPoE or bridge device is not enough. +- **Protocol.** TCP, UDP or GRE. For TCP the conntrack entry must be established and assured ([`nf_conntrack_tcp_established()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_conntrack_l4proto.h?h=v7.4-rc3#n206)) and the packet must not carry FIN or RST. UDP and GRE need no state check. GRE needs `CONFIG_NF_CT_PROTO_GRE` and is skipped when NAT is in use or when the conntrack tuple has a GRE key, which conntrack fills in only for GRE version 1 ([nf_conntrack_proto_gre.c:226-245](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_gre.c?h=v7.4-rc3#n226)); the code comment says "No support for GRE v1". The hook then matches only GRE version 0 ([nf_flow_table_ip.c:270](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n270)). +- **Skipped flows and packets.** A flow with a conntrack helper attached, with `IPS_SEQ_ADJUST` set or with `IPS_NAT_CLASH` set is skipped. Packets that carry a security path (IPsec) or IPv4 options are skipped ([`nft_flow_offload_skip()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n24)). +- **Confirmed.** The conntrack entry must be confirmed. +- **Only once.** `test_and_set_bit(IPS_OFFLOAD_BIT, ...)` makes the first caller win. -## How a flow gets in +If all of that holds, [`nft_flow_route()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n272) resolves the path: it reuses the route of the packet being processed for one direction and looks up the reverse route ([nf_flow_table_path.c:305](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n305)). For routes without an IPsec transform it also requires the egress device to be in the flowtable and, on an Ethernet device, a neighbour entry in state `NUD_VALID` ([nf_flow_table_path.c:61-77](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n61)). [`flow_offload_alloc()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n53) then allocates the entry with a reference to the conntrack entry and the SNAT and DNAT flags, and [`flow_offload_add()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n335) inserts its two tuples, one per direction, into one resizable hash table. The comment in [`flow_offload_add()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n335) says garbage collection walks only the original-direction entries, so that one is inserted last. -The `flow add` statement runs on a packet in the forward path and checks, in `nft_flow_offload_eval()`, whether the flow is a candidate: +For TCP, [`flow_offload_ct_tcp()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n41) sets the "be liberal" flag on the conntrack entry; its comment says conntrack will not see all packets any more, so it can't validate the TCP window. The flowtable code never clears the flag ([nft_flow_offload.c:45-46](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n45) is the only place it is set in the flowtable code). -- **Protocol.** TCP, UDP, or GRE (forwarded GRE flows tracked by conntrack). For TCP the conntrack entry must be established and assured (`nf_conntrack_tcp_established()`) and the packet must not carry FIN or RST. UDP and GRE need only a confirmed entry. GRE needs `CONFIG_NF_CT_PROTO_GRE` and is accepted only without NAT and only for GRE version 0: the code tests the conntrack tuple's GRE key, which conntrack fills in only for version 1 (PPTP), and the comment says "No support for GRE v1". -- **No helper and no sequence adjustment.** A flow with a conntrack helper attached, with `IPS_SEQ_ADJUST` set, or with `IPS_NAT_CLASH` set is skipped. Packets that carry a security path (IPsec) or IPv4 options are skipped too (`nft_flow_offload_skip()`). -- **Confirmed.** The conntrack entry must already be confirmed. -- **Only once.** `test_and_set_bit(IPS_OFFLOAD_BIT, ...)` makes the first caller win, so two packets can't add the same flow twice. +## Transmit modes -If all of that holds, the kernel resolves the path in both directions (it reuses the route of the packet being processed and looks up the reverse route). Except for IPsec routes, path resolution can also reject the flow: the egress device it resolves to must be in the flowtable, and on an Ethernet device the next hop needs a neighbour entry in a valid state (`NUD_VALID`). +Each direction of a flow has a transmit type chosen at add time ([nf_flow_table_path.c:19](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n19)): -It then allocates a `struct flow_offload` that holds a reference to the conntrack entry, copies the SNAT and DNAT flags from the conntrack status, and inserts the entry. The entry has two tuples, one per direction, in one resizable hash table. The comment in `flow_offload_add()` notes that garbage collection walks only the original-direction entries, so that one is inserted last. +- **Neighbour.** The default. The destination MAC comes from the neighbour entry for the next hop ([`nf_flow_queue_xmit4()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n838)). +- **Direct.** The entry stores the MAC addresses. The path code picks it for PPPoE paths ([nf_flow_table_path.c:155](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n155)), bridge paths ([nf_flow_table_path.c:187](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n187)), and Ethernet devices when hardware offload is on ([nf_flow_table_path.c:196](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n196)). +- **IPsec (xfrm).** Chosen for a route that has an IPsec transform. The route is the one the packet being processed carries; the reverse direction comes from a plain route lookup and gets its own type ([`nft_flow_route()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n272)). The hit goes out through `dst_output()` ([`nf_flow_xmit_xfrm()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n300)). -For TCP, `flow_offload_ct_tcp()` also sets the "be liberal" flag on the conntrack entry. The comment there gives the reason: conntrack will not see all packets any more, so it can't validate the TCP window. Nothing in the flowtable code clears that flag, so window tracking normally stays off for the rest of the connection's life, including after it returns to conntrack. +A flow caches its route in the entry, except in direct mode without a tunnel, where the route is released ([nf_flow_table_core.c:129-136](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n129)). ## What a hit does -The IPv4 hook is `nf_flow_offload_ip_hook()` in `net/netfilter/nf_flow_table_ip.c`. For each packet it: +For each packet, [`nf_flow_offload_forward()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n475) (called from [`nf_flow_offload_ip_hook()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n904)): -1. Looks up the packet's tuple in the table. A miss returns `NF_ACCEPT` and the packet carries on down the classic path. -2. Checks the packet against the MTU recorded for the flow. A packet that is too big returns to the classic path, so the usual ICMP error can be generated. -3. Checks TCP flags (see below). -4. Checks that the cached route, if the flow keeps one, is still valid. If it isn't, the flow is torn down and the packet goes back to the classic path. Direct-mode flows without a tunnel keep no route, so a route or bridge forwarding-table change isn't noticed. The commit that added direct mode says that "if peer is moved to different bridge port, the connection will time out". -5. Strips any VLAN, PPPoE or IPIP tunnel header (IPIP since v6.19), applies the stored NAT rewrite, decrements the TTL, and updates the conntrack counters if the flowtable has `counter`. -6. Pushes any egress VLAN, PPPoE or tunnel header back on, builds the link-layer header with `dev_hard_header()` and calls `dev_queue_xmit()` on the egress device. +1. Looks up the packet's tuple. A miss returns `NF_ACCEPT` and the packet follows the classic path. +2. Checks the packet against the MTU recorded for the flow. A packet that is too big goes to the classic path ([`nf_flow_exceeds_mtu()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n289)); the kernel documentation gives the reason as the packet-too-big ICMP error ([nf_flowtable.rst:78-79](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n78)). +3. Checks TCP flags ([`nf_flow_state_check()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n25); see [How a flow leaves](#how-a-flow-leaves)). +4. Checks the cached route, if the flow keeps one ([`nf_flow_dst_check()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_flow_table.h?h=v7.4-rc3#n314) returns true when there is none). A stale route tears the flow down and the packet goes to the classic path. Direct-mode flows without a tunnel keep no route, so no route change is noticed; the commit that added direct mode, [`7a27f6ab4135`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7a27f6ab41356ecba47ec2bec6d635704c169779), says "if peer is moved to different bridge port, the connection will time out". +5. Refreshes the deadline ([`flow_offload_refresh()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n367)), strips any VLAN, PPPoE or IPIP tunnel header (IPIP since v6.19, [`ab427db17885`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ab427db17885814069bae891834f20842f0ac3a4)), applies the stored NAT rewrite, decrements the TTL, and updates conntrack's counters if the flowtable has `counter`. +6. Pushes any egress VLAN, PPPoE or tunnel header back on, builds the link-layer header with `dev_hard_header()` and transmits as described in [Transmit modes](#transmit-modes) ([`__nf_flow_queue_xmit()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n776)). -In neighbour mode the destination MAC comes from the neighbour entry for the next hop. In direct mode the kernel uses MAC addresses stored in the entry. The path code picks direct mode for PPPoE and bridge paths, and when hardware offload is on for an Ethernet device. A flow whose route has an IPsec transform goes out through `dst_output()` instead, which still runs the postrouting hook. +[`nf_flow_offload_ipv6_hook()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1233) does the same with the hop limit in place of the TTL ([nf_flow_table_ip.c:1069](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1069)). The next header has to be TCP, UDP or GRE, so a packet with any extension header is a miss ([nf_flow_table_ip.c:1050-1067](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1050)). It strips IPv6-in-IPv6 tunnel headers since v7.0 ([`d98103575dcd`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=d98103575dcdd3a730e0901ab457791a9ac6930c)). Mixed-family tunnels are not supported: [`d0107f381622`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=d0107f38162279d9cb0e012210731176265f835d) says IPv4 over IPv6 and SIT tunnel offloading "is not enabled yet". -The IPv6 hook, `nf_flow_offload_ipv6_hook()`, does the same, with the hop limit in place of the TTL. Any extension header is a miss, because the next header has to be TCP, UDP or GRE. It strips IPv6-in-IPv6 tunnel headers (since v7.0). Mixed-family tunnels are not supported: [`d0107f381622`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=d0107f38162279d9cb0e012210731176265f835d) says IPv4 over IPv6 and SIT tunnel offloading "is not enabled yet". +Packets that the hook doesn't take go to the classic path: fragments and packets with IPv4 options ([nf_flow_table_ip.c:230](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n230)), a TTL or hop limit of 1 or less ([nf_flow_table_ip.c:253](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n253)), and GRE other than version 0. ## How a flow leaves -The garbage-collection worker, `nf_flow_offload_work_gc()`, runs once a second and removes entries that have been torn down. Teardown is triggered by: +[`nf_flow_offload_work_gc()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n620) runs once a second ([nf_flow_table_core.c:626](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n626)) and removes entries that have been torn down. Teardown is triggered by ([`nf_flow_offload_gc_step()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n576)): -- **A timeout.** Each entry gets a deadline when it is added. For software flows every hit pushes the deadline out, at most once a second. The default is 30 seconds for TCP, UDP and GRE (GRE has no sysctl); the TCP and UDP values are the sysctls `net.netfilter.nf_flowtable_tcp_timeout` and `net.netfilter.nf_flowtable_udp_timeout` (`offload_timeout` in `nf_conntrack_proto_tcp.c` and `nf_conntrack_proto_udp.c`). Packets of a hardware flow mostly bypass the hook, so the collector asks the driver for statistics, once less than 90% of the timeout remains, and extends the deadline from the last-used time. -- **A dying conntrack entry or a stale route.** The collector tears the flow down (for a stale route only since v7.3, [`edd51a233438`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=edd51a23343870dbd7cedf6e2765c13cf7a5ccbc); before that only the hook noticed one). The hook tears the flow down, and drops that packet, when the egress device no longer exists or no neighbour entry can be found or created. -- **A device going down.** The `NETDEV_DOWN` notifier calls `nf_flow_table_cleanup()`. +- **A timeout.** Each entry has a deadline. A software hit pushes it out, at most once a second ([nf_flow_table_core.c:373](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n373)). The default is 30 seconds for TCP, UDP ([nf_conntrack_proto_tcp.c:1658](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n1658), [nf_conntrack_proto_udp.c:204](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_udp.c?h=v7.4-rc3#n204)) and GRE ([nf_flow_table.h:209](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_flow_table.h?h=v7.4-rc3#n209)). The TCP and UDP values are the sysctls `net.netfilter.nf_flowtable_tcp_timeout` and `net.netfilter.nf_flowtable_udp_timeout` ([nf_conntrack_standalone.c:789](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_standalone.c?h=v7.4-rc3#n789), [nf_conntrack_standalone.c:839](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_standalone.c?h=v7.4-rc3#n839)); GRE has none. For hardware flows, the collector asks the driver for statistics once less than 90% of the timeout remains and extends the deadline from the last-used time ([`nf_flow_offload_stats()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1149), [`flow_offload_work_stats()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1011)). +- **A dying conntrack entry.** `nf_ct_is_dying()` ([`nf_flow_offload_gc_step()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n576)). +- **A stale route**, for flows that keep one. The collector checks this since v7.3 ([`edd51a233438`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=edd51a23343870dbd7cedf6e2765c13cf7a5ccbc)); the hook checks it too ([`nf_flow_offload_forward()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n475)). +- **A missing device or neighbour.** The hook tears the flow down and drops that packet ([`nf_flow_queue_xmit4()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n838)). +- **A device going down.** The `NETDEV_DOWN` notifier calls [`nf_flow_table_cleanup()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n760) ([nft_flow_offload.c:248](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n248)). +- **Deleting the flowtable**, which includes `flush ruleset` ([`nf_flow_table_free()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n773)). -TCP FIN and RST don't end the flow at once (since v6.14). The hook marks the flow `NF_FLOW_CLOSING` and the packet still takes the fast path. The flow stays in the software table, but a hardware entry is removed early. A later SYN on a closing flow tears it down, sets the conntrack state to close, and sends that packet to the classic path so conntrack can handle the reconnect. Otherwise the flow leaves by timeout. +**TCP FIN and RST** don't end the flow at once (since v6.14, [`fdbaf5163331`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fdbaf5163331342e90a2c29b87629021f4c15f0c)). [`nf_flow_state_check()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n25) marks the flow `NF_FLOW_CLOSING` and the packet still takes the fast path; the collector removes a hardware entry early ([`nf_flow_offload_gc_step()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n576)). A later SYN on a closing flow tears it down, sets the conntrack state to close and sends that packet to the classic path ([`nf_flow_state_check()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n25), [`flow_offload_fixup_tcp()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n178)). -Teardown clears the offload bit on the conntrack entry. A later forward-chain packet that matches the `flow add` rule can add the flow again. A TCP flow torn down by a SYN is in the close state, so it isn't added again until it is established. +**Adding again.** [`flow_offload_teardown()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n399) clears the offload bit on the conntrack entry ([nf_flow_table_core.c:401](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n401)), so a later forward-chain packet that matches the `flow add` rule can add the flow again. -While a flow is in the table, conntrack doesn't see its packets, so its timeout would run out. `flow_offload_add()` first sets it to a day. After that the garbage collector calls `nf_flow_table_extend_ct_timeout()`, which pushes it forward when less than five minutes remain. The new value depends on the protocol and state: +**Conntrack timeout.** While a flow is in the table, conntrack may see few or none of its packets. [`flow_offload_add()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n335) first sets the conntrack timeout to a day ([nf_flow_table_core.c:358](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n358)). Afterwards the collector calls [`nf_flow_table_extend_ct_timeout()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n517), which extends it when less than five minutes remain, by an amount that depends on the protocol and TCP state ([`nf_flow_table_tcp_timeout()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n479)). The function's comment calls the value "an arbitrary timeout large enough not to ever expire". | Flow | Conntrack timeout is extended to | |------|----------------------------------| | UDP, GRE | a day | | TCP established | a day | | TCP FIN_WAIT, CLOSE_WAIT, LAST_ACK, TIME_WAIT | five minutes | -| TCP SYN_SENT, SYN_RECV, CLOSE | no extension | - -The extension logic dates from [`03428ca5cee9`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=03428ca5cee9f0792edc996c06ce4514816af1fb) (v6.14) but never took effect until [`53b3e60edb67`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=53b3e60edb674b442b2b3bbdba484667b0f47a5d) (v7.2): it compared a relative value with the absolute `ct->timeout`, so the update never succeeded. It also did not handle GRE. Once a GRE entry's timeout fell below five minutes it hit a `WARN_ON_ONCE()`, and from v7.2 it also got a one-jiffy timeout, until [`46ecda278f6e`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=46ecda278f6eedffb2898d41e3e56757c996a41f) (v7.4-rc3). The function's comment calls the value "an arbitrary timeout large enough not to ever expire", which saves a check of the offload bit in the packet path. +| Other TCP states (SYN_SENT, SYN_RECV, CLOSE, ...) | no extension | -When a flow is torn down, `flow_offload_fixup_ct()` hands it back to conntrack. For TCP it keeps the last state conntrack recorded (or sets close if the flow was closing). It clears the recorded maximum window and the max-ack flag. As the code comment says, without clearing the max-ack flag conntrack's TCP reset validation would fail. TCP and UDP flows also have their conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired, and a closing TCP flow gets the close timeout. The timeout is only ever lowered, so an established TCP entry, whose normal timeout is five days, keeps whatever is left of its day. GRE flows are left as they are, so a GRE conntrack entry can keep a timeout of up to a day after it leaves the table. +**Hand-back to conntrack.** [`flow_offload_fixup_ct()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n201) runs at teardown. For TCP it keeps the last conntrack state, or sets close if the flow was closing, and clears the recorded maximum window and the max-ack flag; the code comment says that otherwise conntrack's TCP reset validation would fail ([`flow_offload_fixup_tcp()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n178)). TCP and UDP flows have the conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired; a closing TCP flow gets the close timeout unconditionally ([nf_flow_table_core.c:250-252](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n250)). Except for that case the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n64)), keeps whatever is left of its day. GRE flows return before any timeout change ([nf_flow_table_core.c:240-241](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n240)). ## Hardware offload -If the device driver supports it, the flowtable can push entries to the NIC or switch. Add `flags offload;` to the flowtable definition. The kernel documentation says a workqueue adds the flows to hardware, so a few packets can still go through the software path until it runs. +The kernel documentation says a flowtable can offload flows to a device that supports it, enabled with `flags offload`, and that a workqueue adds the flows, so a few packets can still take the software path first ([nf_flowtable.rst:183-201](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n183)). The driver interface is the tc flow-block interface: -- **Binding.** Registering a flowtable's ingress hook on a device first calls the flowtable type's `setup` callback, `nf_flow_table_offload_setup()`. Without `flags offload` that only sets up the XDP path. With it, the callback binds a flow block to that device. If the device has `ndo_setup_tc`, the flowtable calls it with `TC_SETUP_FT`. Otherwise it goes through the indirect-block path, `flow_indr_dev_setup_offload()`. The driver adds its callback to the flowtable's block. -- **Per flow.** `nf_flow_table_offload.c` calls every callback with `TC_SETUP_CLSFLOWER` and a `flow_cls_offload` request, the same request type tc flower uses. The command is `FLOW_CLS_REPLACE` to add a flow (carrying a flow rule: match keys plus actions such as header rewrites), `FLOW_CLS_DESTROY` to remove it or `FLOW_CLS_STATS` to read its counters. -- **Drivers.** The in-tree drivers that handle `TC_SETUP_FT` are the mlx5 representor code, the MediaTek Ethernet and WED drivers, the mt76 Wi-Fi driver and the Airoha Ethernet driver. DSA user ports pass the command to the conduit device, and `netdevsim` accepts it but registers no callback. +- **Binding.** Registering a flowtable's ingress hook on a device first calls the flowtable type's `setup` callback, [`nf_flow_table_offload_setup()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1271) ([nf_tables_api.c:9068](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9068)). Without `flags offload` it only sets up the XDP path ([nf_flow_table_offload.c:1279-1280](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1279)); XDP programs can then look the table up with [`bpf_xdp_flow_lookup`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_bpf.c?h=v7.4-rc3#n59) ([`391bb6594fd3`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=391bb6594fd3a567efb1cd3efc8136c78c4c9e31), v6.11). With `flags offload`, if the device has `ndo_setup_tc` the flowtable calls it with `TC_SETUP_FT`, and otherwise it uses the indirect-block path `flow_indr_dev_setup_offload()` ([`nf_flow_table_offload_cmd()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1252), [`nf_flow_table_indr_offload_cmd()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1239)). The driver adds its callback to the flowtable's block. +- **Per flow.** [`nf_flow_offload_tuple()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n901) calls every callback with `TC_SETUP_CLSFLOWER` and a `flow_cls_offload` request, the request type tc flower uses. The command is `FLOW_CLS_REPLACE` to add a flow (with a flow rule: match keys plus actions such as header rewrites), `FLOW_CLS_DESTROY` or `FLOW_CLS_STATS`. +- **In-tree drivers that handle `TC_SETUP_FT`** as of v7.4-rc3: the mlx5 representor code ([rep/tc.c:261](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mellanox/mlx5/core/en/rep/tc.c?h=v7.4-rc3#n261)), the MediaTek Ethernet and WED drivers ([mtk_ppe_offload.c:674](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mediatek/mtk_ppe_offload.c?h=v7.4-rc3#n674), [mtk_wed.c:2790](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mediatek/mtk_wed.c?h=v7.4-rc3#n2790)), the mt76 Wi-Fi driver ([npu.c:385](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/wireless/mediatek/mt76/npu.c?h=v7.4-rc3#n385)) and the Airoha Ethernet driver ([airoha_eth.c:3674](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/airoha/airoha_eth.c?h=v7.4-rc3#n3674)). DSA user ports pass the command to the conduit device ([dsa/user.c:1729](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/dsa/user.c?h=v7.4-rc3#n1729)). `netdevsim` returns 0 and registers no callback ([netdevsim/tc.c:92](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/netdevsim/tc.c?h=v7.4-rc3#n92)). -Some operator-visible consequences: +Behaviour with `flags offload`, from the source: -- A device that can't offload makes the flowtable fail to load: the `setup` error is returned before the hook is registered. The `offload` flag can't be toggled on an existing flowtable. -- With `flags offload`, every Ethernet path uses direct mode, so a flow without a tunnel caches no route and a route change isn't noticed until the flow goes idle. -- Flows that go through an IPIP or IPv6-in-IPv6 tunnel stay in software: [`6c5dcab95f4c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6c5dcab95f4cd42a1648739ec9300fbb4b1a021f) (v7.2) skips the hardware offload for them. -- A hardware add that fails isn't reported: the flow keeps running in software, and the add is retried on later software hits. `[HW_OFFLOAD]` is set if the driver accepted at least one direction. -- The collector skips an entry whose hardware work is still queued (`NF_FLOW_PENDING`). +- A device that can't offload makes the flowtable fail to load for devices present when it is created: the `setup` error is returned before the hook is registered ([nf_tables_api.c:9068-9071](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9068)). A change of the `offload` flag on an existing flowtable is rejected with `-EOPNOTSUPP` ([nf_tables_api.c:9196-9203](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9196)). +- Every Ethernet path uses direct mode ([nf_flow_table_path.c:194-196](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n194)), so a flow without a tunnel caches no route. +- A hardware add that fails isn't reported. The flow stays in software with `NF_FLOW_HW` set, `[HW_OFFLOAD]` is set if the driver accepted at least one direction ([`flow_offload_rule_add()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n957), [`flow_offload_work_add()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n973)), and the add is queued again on later software hits, at most once a second and not for closing flows ([nf_flow_table_core.c:373-383](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n373)). +- Flows through an IPIP or IPv6-in-IPv6 tunnel stay in software ([`nf_flow_offload_unsupported()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1105); [`6c5dcab95f4c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6c5dcab95f4cd42a1648739ec9300fbb4b1a021f), v7.2). +- The collector skips an entry whose hardware work is still queued ([nf_flow_table_core.c:581](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n581)). ## Inspecting -`conntrack -L` shows `[OFFLOAD]` for flows in the software table and `[HW_OFFLOAD]` for flows in hardware, as the kernel documentation describes. For an offloaded entry the kernel leaves out the timeout and protocol state: ctnetlink skips them (`nf_conntrack_netlink.c`) and `/proc/net/nf_conntrack` skips the timeout and the TCP state (`nf_conntrack_standalone.c`, `nf_conntrack_proto_tcp.c`). - -With `CONFIG_NF_FLOW_TABLE_PROCFS`, `/proc/net/stat/nf_flowtable` shows per-CPU counters of pending (queued, not yet completed) hardware add, delete and stats work; sum them across CPUs. It has no hit or miss counters. The two timeout sysctls are described above. +- **Conntrack listing.** `conntrack -L` shows `[OFFLOAD]` for software flows and `[HW_OFFLOAD]` for hardware flows ([nf_flowtable.rst:111-113](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n111), [nf_flowtable.rst:202-206](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n202); the tags are printed at [nf_conntrack_standalone.c:366](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_standalone.c?h=v7.4-rc3#n366)). +- **What offloaded entries omit.** ctnetlink skips the timeout and protocol info ([nf_conntrack_netlink.c:579](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_netlink.c?h=v7.4-rc3#n579)), and `/proc/net/nf_conntrack` skips the timeout and the TCP state ([nf_conntrack_standalone.c:339](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_standalone.c?h=v7.4-rc3#n339), [`tcp_print_conntrack()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n266)). +- **Statistics.** With `CONFIG_NF_FLOW_TABLE_PROCFS` ([Kconfig:721](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/Kconfig?h=v7.4-rc3#n721)), [`/proc/net/stat/nf_flowtable`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_procfs.c?h=v7.4-rc3#n43) shows per-CPU counters of pending hardware add, delete and stats work. They are incremented when work is queued and decremented when it completes ([nf_flow_table_offload.c:1047-1077](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1047)). There are no hit or miss counters. ## Limits -- Changing or removing rules does not evict flows that are already in the table, including the `flow add` rule itself. A hit never reaches the forward chain, and every software hit pushes the deadline out, so a busy flow doesn't expire. A new drop, reject, rate-limit or mark rule in the forward chain doesn't apply to such flows either; only the stored NAT rewrite is applied. Flows leave when they go idle, when their conntrack entry is deleted (for example with `conntrack -D`), when a device goes down or the route goes stale, or when the flowtable is deleted. A reload that starts with `flush ruleset` deletes the flowtable, which tears down every flow in it, so they are evaluated again. -- Fragments, and packets that exceed the MTU, go through the classic path. The kernel documentation gives this as the reason: the transport header is missing for fragments, so no lookup is possible. Packets with IPv4 options, a TTL or hop limit of 1 or less, or GRE other than version 0 also go to the classic path. The commit that extended the GRE timeout, [`46ecda278f6e`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=46ecda278f6eedffb2898d41e3e56757c996a41f) (v7.4-rc3), notes that `ip6gre` tunnels with the default encapsulation limit carry a Destination Options header before GRE, which the flowtable does not parse, so their packets all take the classic path. -- The table is a cache. The kernel documentation warns that an entry can go stale if the destination MAC address or the egress device changes. It names two risky setups: software mode combined with bridging and IP forwarding, and hardware offload. -- The IP-layer hooks (prerouting and forward, and postrouting for flows without IPsec) don't see offloaded packets. A rule there, such as a counter or a log, won't match them. -- IPsec: packets that carry a security path never trigger an add. A flow whose route has an IPsec transform goes out through `dst_output()`, so postrouting still runs, and the device-list and neighbour checks don't apply to it. +- **Rule changes.** Changing or deleting a rule is not among the triggers listed in [How a flow leaves](#how-a-flow-leaves). A hit never reaches the forward chain, and a software hit pushes the deadline out ([`flow_offload_refresh()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n367)). Only the stored NAT rewrite is applied to a hit ([`nf_flow_offload_forward()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n475)). +- **Cache.** The kernel documentation says the table behaves like a cache and that entries can go stale if the destination MAC address or the egress device changes. It names software mode combined with bridging and IP forwarding, and hardware offload, as the risky setups ([nf_flowtable.rst:214-222](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n214)). +- **IPsec.** A packet that carries a security path never triggers an add ([`nft_flow_offload_skip()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n24)). In the direction whose route has an IPsec transform the device-list and neighbour checks are skipped ([nf_flow_table_path.c:314-320](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n314)); the other direction gets them like any other flow. +- **ip6gre.** The commit that extended the GRE timeout, [`46ecda278f6e`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=46ecda278f6eedffb2898d41e3e56757c996a41f), says `ip6gre` tunnels with the default encapsulation limit carry a Destination Options header before GRE, which the flowtable doesn't parse, so every packet of the flow takes the classic path. ## Where it came from -The design was posted to netfilter-devel by Pablo Neira Ayuso. LWN reproduces two of the postings, and the kernel documentation cites them. +The design was posted to netfilter-devel by Pablo Neira Ayuso. LWN reproduces two of the postings, and the kernel documentation cites them ([nf_flowtable.rst:224-235](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n224)). - **3 November 2017**, [`[PATCH RFC,WIP 0/5] Flow offload infrastructure`](https://lwn.net/Articles/738214/) (Message-ID `<20171103152636.9967-1-pablo@netfilter.org>`). The cover letter describes a module with a hook at ingress, populated by an nftables action once a flow is established. It gives the hardware motivation: "Switches and NICs come with built-in flow table, I've been observing out of tree patches in OpenWRT/LEDE to integrate this into Netfilter for a little while." It reports a measurement: "I'm measuring here that the software flow table forwarding path is 2.5 faster than the classic forwarding path in my testbed." Its TODO list included IPv4 only, IPv4 SNAT only, no netns support and no flow counters, and the last patch added `ndo` hooks to populate a hardware flow table. - **22 December 2017**, [`[PATCH nf-next,v3 0/7] Flow offload infrastructure`](https://lwn.net/Articles/742164/) (Message-ID `<20171222192732.13188-1-pablo@netfilter.org>`). The cover letter lists IPv6 and mixed IPv4/IPv6 support, hardware offload in a separate `nf_flow_table_hw` module, port translation and netns support. The hardware patch was tagged RFC, and the cover letter says "I will keep this one back until there's an initial driver that introduces flow table offload support". @@ -155,15 +162,17 @@ The software flow table was committed on 8 January 2018 ([`ac2a66665e23`](https: Hardware offload reached the tree in v5.5 through the tc flow-block interface rather than through the dedicated `ndo` hooks proposed in the RFC. Pablo Neira Ayuso added it in [`c29f74e0df7a`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c29f74e0df7a02b8303bcdce93a7c0132d62577a), which called `ndo_setup_tc` with `TC_SETUP_BLOCK` and the callbacks with `TC_SETUP_FT`. The arrangement described above came from two fixes by wenxu in the same release, [`ee1bcfe01251`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ee1bcfe01251e8958b53de45d2b0c85e09dd2719) and [`e052901991ae`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e052901991ae21e15851bfc89c682bfcb39a4dcf). wenxu added the indirect path in [`b5140a36da78`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b5140a36da7876bc084a2c680c8dbc7438db2051) (v5.7). +**GRE timeout history.** The conntrack timeout extension was reworked in [`03428ca5cee9`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=03428ca5cee9f0792edc996c06ce4514816af1fb) (v6.14), but it never took effect until [`53b3e60edb67`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=53b3e60edb674b442b2b3bbdba484667b0f47a5d) (v7.2): it compared a relative value with the absolute `ct->timeout`, so the update never succeeded. It also did not handle GRE. Once a GRE entry's timeout fell below five minutes it hit a `WARN_ON_ONCE()`, and from v7.2 it also got a one-jiffy timeout, until [`46ecda278f6e`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=46ecda278f6eedffb2898d41e3e56757c996a41f) (v7.4-rc3). The packets that bring the timeout down are those that still go through conntrack; the commit message gives packets with a TTL of 1 or less as an example. These three fixes carry `Cc: stable`, so the mainline release numbers above are not the only ones with the fix. + ## Differences from `nf_flowtable.rst` -The kernel documentation was last updated with new content in March 2021 (v5.13, [`143490cde566`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=143490cde5669e0151dff466a7c2cf70e2884fb7)); a build-warning fix followed the next day. As of v7.4-rc3, at least these statements in it no longer match the source: +The kernel documentation was last updated with new content in March 2021 (v5.13, [`143490cde566`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=143490cde5669e0151dff466a7c2cf70e2884fb7)); a build-warning fix followed the next day ([`794d9b25817a`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=794d9b25817a813f1d2fb8e133ba6d2f53920853)). As of v7.4-rc3, at least these statements in it no longer match the source: -- It says TCP RST and FIN packets are passed up to the classic path. Since [`fdbaf5163331`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fdbaf5163331342e90a2c29b87629021f4c15f0c) ("netfilter: flowtable: add CLOSING state", v6.14), they stay on the fast path and the flow enters the closing state. The commit message explains that the old behaviour "leads to packet races when restoring the conntrack state as well as late flow re-offload situations". -- It says hits are sent with `neigh_xmit()`. Direct mode has called `dev_queue_xmit()` since [`7a27f6ab4135`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7a27f6ab41356ecba47ec2bec6d635704c169779) (v5.13, the same day as the documentation update), and [`b5964aac51e0`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b5964aac51e0c286a50e68225e0dfcf11fb554cb) ("netfilter: flowtable: consolidate xmit path", v6.19) moved neighbour mode to it too. -- It says packets are not seen "from any of the Netfilter hooks coming after ingress". The netdev egress hook, added in [`42df6e1d221d`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=42df6e1d221dddc0f2acf2be37e68d553ad65f96) (v5.16, after the documentation was written), does see them, because every software hit that isn't an IPsec flow ends in `dev_queue_xmit()`. -- It says the rule adds the flow on "the TCP syn-ack packet". The code requires an established and assured conntrack entry. [`e5eaac2beb54`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e5eaac2beb54f0a16ff851125082d9faeb475572) (v5.18) added that check, and its message says the patch makes `nft_flow_offload` "skip packets in SYN_RECV state" and "postpones offload up to the next packet". -- It says the flowtable supports TCP and UDP. GRE has been accepted since [`4e8d9584d154`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4e8d9584d154479d357327f76d4e49486915c9c9) (v5.18). +- It says TCP RST and FIN packets are passed up to the classic path ([nf_flowtable.rst:77-78](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n77)). Since [`fdbaf5163331`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fdbaf5163331342e90a2c29b87629021f4c15f0c) ("netfilter: flowtable: add CLOSING state", v6.14), they stay on the fast path and the flow enters the closing state. The commit message explains that the old behaviour "leads to packet races when restoring the conntrack state as well as late flow re-offload situations". +- It says hits are sent with `neigh_xmit()` ([nf_flowtable.rst:21](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n21), [nf_flowtable.rst:74](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n74)). Direct mode has called `dev_queue_xmit()` since [`7a27f6ab4135`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7a27f6ab41356ecba47ec2bec6d635704c169779) (v5.13, the same day as the documentation update), and [`b5964aac51e0`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b5964aac51e0c286a50e68225e0dfcf11fb554cb) ("netfilter: flowtable: consolidate xmit path", v6.19) moved neighbour mode to it too. +- It says packets are not seen "from any of the Netfilter hooks coming after ingress" ([nf_flowtable.rst:22-23](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n22)). The netfilter egress hook, added in [`42df6e1d221d`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=42df6e1d221dddc0f2acf2be37e68d553ad65f96) (v5.16, after the documentation was written), does see software hits, because they end in `dev_queue_xmit()`. +- It says the rule adds the flow on "the TCP syn-ack packet" ([nf_flowtable.rst:105-106](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n105)). The code requires an established and assured conntrack entry ([`nf_conntrack_tcp_established()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_conntrack_l4proto.h?h=v7.4-rc3#n206)). [`e5eaac2beb54`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e5eaac2beb54f0a16ff851125082d9faeb475572) (v5.18) added that check, and its message says the patch makes `nft_flow_offload` "skip packets in SYN_RECV state" and "postpones offload up to the next packet". +- It says the flowtable supports TCP and UDP ([nf_flowtable.rst:10](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n10)). GRE has been accepted since [`4e8d9584d154`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4e8d9584d154479d357327f76d4e49486915c9c9) (v5.18). ## Further reading From 9349fcc31b02adaeaa047ebd37ddecebd41f50cd Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 19:11:16 -0800 Subject: [PATCH 11/22] net: switch flowtable page to reference links and fix review findings Correct the stable-tag count, the postrouting and NFT_BREAK statements, closing-flow and neighbour behaviour, and the v7.2 device-list scope. Cite the remaining claims and pin all links to v7.4-rc3. --- docs/net/flowtable.md | 332 +++++++++++++++++++++++++++++++----------- 1 file changed, 250 insertions(+), 82 deletions(-) diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index 1be19bc6..3fa7533e 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -2,11 +2,11 @@ > How the kernel lets established forwarded connections skip most of the netfilter hook path -Source links point at Linux v7.4-rc3, the tree this page was checked against. Behaviour that changed recently is marked with the release that introduced it and links to the commit. "The kernel documentation" is [`nf_flowtable.rst`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3). +Source links point at Linux v7.4-rc3, the tree this page was checked against. Behaviour that changed recently is marked with the release that introduced it and links to the commit. "The kernel documentation" is [`nf_flowtable.rst`][rst]. ## What it does -The kernel documentation describes the design: once the first packet of a flow has gone through the IP forwarding path, a rule can add the flow to a table ([nf_flowtable.rst:15-19](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n15)). A packet that finds a matching entry is transmitted without the classic forwarding path, and a packet that doesn't follows the classic path ([nf_flowtable.rst:20-26](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n20)). The lookup runs in a hook at ingress on the devices listed in the flowtable ([`nf_flow_offload_ip_hook()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n904) for IPv4, [`nf_flow_offload_ipv6_hook()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1233) for IPv6). +The kernel documentation describes the design: once the first packet of a flow has gone through the IP forwarding path, a rule can add the flow to a table ([nf_flowtable.rst:15-19][rst15]). A packet that finds a matching entry is transmitted without the classic forwarding path, and a packet that doesn't follows the classic path ([nf_flowtable.rst:20-26][rst20]). The lookup runs in a hook at ingress on the devices listed in the flowtable: [`nf_flow_offload_ip_hook()`][iphook] for IPv4 and [`nf_flow_offload_ipv6_hook()`][ip6hook] for IPv6, registered through the flowtable types ([nf_flow_table_inet.c:71-94][inet]). ``` ingress hook @@ -22,20 +22,21 @@ The kernel documentation describes the design: once the first packet of a flow h └───────────────────┘ └───────────────────────┘ ``` -The diagram is simplified from Fig. 1 of the kernel documentation ([nf_flowtable.rst:37-70](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n37)). In that figure a `flow add` rule in the forward chain of the classic path is what inserts a flow into the table. +The diagram is simplified from Fig. 1 of the kernel documentation ([nf_flowtable.rst:37-70][rst37]). The figure labels the rule that adds an entry "flow offload"; the example below spells it `flow add`. Which hooks a hit passes, from the source: -- The tc ingress hook runs before the netfilter ingress hook where the flowtable sits ([dev.c:6290](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c?h=v7.4-rc3#n6290), [dev.c:6298](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c?h=v7.4-rc3#n6298)). -- A software hit is transmitted with `dev_queue_xmit()` ([`__nf_flow_queue_xmit()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n776)), which runs the netfilter egress hook and tc egress ([dev.c:5024](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c?h=v7.4-rc3#n5024), [dev.c:5032](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c?h=v7.4-rc3#n5032)). -- A hit whose route has an IPsec transform is transmitted with `dst_output()` ([`nf_flow_xmit_xfrm()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n300)), which runs the postrouting hook ([xfrm4_output.c:33](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/xfrm4_output.c?h=v7.4-rc3#n33)). -- The kernel documentation says hits are not seen by "any of the Netfilter hooks coming after ingress" ([nf_flowtable.rst:22-23](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n22)). The source differs; see [Differences](#differences-from-nf_flowtablerst). +- The tc ingress hook runs before the netfilter ingress hook where the flowtable sits ([dev.c:6290][dev6290], [dev.c:6298][dev6298]). +- A software hit is transmitted with `dev_queue_xmit()` ([`__nf_flow_queue_xmit()`][xmit]), which runs the netfilter egress hook and tc egress ([dev.c:5024][dev5024], [dev.c:5032][dev5032]). +- A hit whose route has an IPsec transform is transmitted with `dst_output()` ([`nf_flow_xmit_xfrm()`][xfrm]), which runs the postrouting hook ([xfrm4_output.c:33][xfrm4]). -The tc `ct` action uses the same infrastructure ([act_ct.c:55](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/sched/act_ct.c?h=v7.4-rc3#n55)). This page covers the nftables side. +The kernel documentation says "you do not see these packets from any of the Netfilter hooks coming after ingress" ([nf_flowtable.rst:22-23][rst22]). The source differs; see [Differences](#differences-from-nf_flowtablerst). + +The tc `ct` action uses the same infrastructure ([act_ct.c:55][actct55]). This page covers the nftables side. ## Turning it on -The kernel documentation's example is a flowtable on two devices and one rule in the forward chain ([nf_flowtable.rst:87-98](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n87)): +The kernel documentation's example is a flowtable on two devices and one rule in the forward chain ([nf_flowtable.rst:87-98][rst87]): ``` table inet x { @@ -50,140 +51,307 @@ table inet x { } ``` -- **The statement.** `flow add @f` is evaluated by [`nft_flow_offload_eval()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n50). It is accepted only in a chain on the forward hook ([`nft_flow_offload_validate()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n138)). The example rule matches IPv4 TCP. -- **What a hit skips.** A hit is consumed at ingress with `NF_STOLEN` ([`nf_flow_queue_xmit()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n822)), so a flow's later packets don't reach the prerouting, forward and postrouting chains. This is why the kernel documentation says the `counter` rule stops being updated ([nf_flowtable.rst:105-109](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n105)). A rule that gives a packet a verdict earlier in the chain means `flow add` never runs for it. The packet that adds the flow continues down the chain, because [`nft_flow_offload_eval()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n50) sets `NFT_BREAK` only on failure. -- **The `counter` flag.** Fast-path packets update conntrack's counters only if the flowtable has the `counter` flag ([nf_flow_table_ip.c:520](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n520); documented in [nf_flowtable.rst:165-181](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n172)). The update takes effect only for conntrack entries that have the accounting extension ([nf_conntrack_acct.h:36](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_conntrack_acct.h?h=v7.4-rc3#n36)), which is added when the sysctl `net.netfilter.nf_conntrack_acct` is set ([nf_conntrack_core.c:1789](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_core.c?h=v7.4-rc3#n1789)). -- **Priority.** The kernel documentation advises a flowtable priority number smaller than that of any nftables ingress chain on the same device ([nf_flowtable.rst:99-103](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n99)). Netfilter runs lower numbers first, and a hit returns `NF_STOLEN`, so a chain that runs after the flowtable doesn't see offloaded packets. -- **Device list.** Path resolution follows VLAN, PPPoE and bridge devices down to the real device ([`nft_dev_fill_forward_path()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n45)) and requires that device to be in the flowtable's list ([nf_flow_table_path.c:198](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n198)). The check runs for both directions and is skipped for IPsec routes ([nf_flow_table_path.c:314-320](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n314)). The kernel documentation says the real device is sufficient and the VLAN and PPPoE devices need not be listed ([nf_flowtable.rst:129-130](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n129)). Since v7.2 ([`871df5007eda`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=871df5007edaacaf2eac58a2871af6044a444ad4)), a failed path discovery makes `flow add` give up; the commit message says "bail out to classic forwarding path rather than providing incomplete forwarding path". +- **The statement.** `flow add @f` is evaluated by [`nft_flow_offload_eval()`][eval]. It is accepted only in a chain on the forward hook ([`nft_flow_offload_validate()`][validate]). The example rule matches IPv4 TCP. +- **What a hit skips.** A hit is consumed with `NF_STOLEN` ([`nf_flow_queue_xmit()`][qxmit]), so the later packets of a flow that hit the table don't reach the prerouting and forward chains, and reach postrouting only for IPsec hits (above). The kernel documentation says the `counter` rule stops being updated ([nf_flowtable.rst:105-109][rst105]). +- **Rule evaluation.** A rule that gives a packet a verdict earlier in the chain means `flow add` never runs for it. `flow add` returns no verdict when it adds the flow. On every other path it sets `NFT_BREAK` ([`nft_flow_offload_eval()`][eval]), which skips the rest of that rule and continues with the next rule ([nf_tables_core.c:292-295][nftcore]). So statements after `flow add` in the same rule run only for the packet that adds the flow. +- **The `counter` flag.** Fast-path packets update conntrack's counters only if the flowtable has the `counter` flag ([nf_flow_table_ip.c:520][ctcount]; documented in [nf_flowtable.rst:165-181][rst165]). The update reaches only conntrack entries that have the accounting extension ([`nf_ct_acct_add()`][acctadd]), which is added when the sysctl `net.netfilter.nf_conntrack_acct` is set ([nf_conntrack_acct.h:36][acct], [nf_conntrack_core.c:1789][acctext], [sysctl][acctsysctl]). +- **Priority.** The kernel documentation says to "make sure the flowtable priority is smaller than the nftables ingress chain hence the flowtable runs before in the pipeline" ([nf_flowtable.rst:99-103][rst99]). A hit returns `NF_STOLEN`, so an ingress chain that runs after the flowtable doesn't see it. +- **Device list.** Path resolution follows VLAN, PPPoE and bridge devices down to the real device ([`nft_dev_fill_forward_path()`][fillpath]) and requires that device to be in the flowtable's list ([nf_flow_table_path.c:198][pathdev]). The check runs for both directions and is skipped for IPsec routes ([nf_flow_table_path.c:314-320][pathskip]). The kernel documentation says the real device is sufficient and the VLAN and PPPoE devices need not be listed ([nf_flowtable.rst:129-130][rst129]). Since v7.2 ([`871df5007eda`][c871df]), a failed path discovery or a device that isn't in the flowtable makes `flow add` give up. The commit message says "If forward path discovery fails for any reason or netdevice is not registered for this flowtable, then bail out to classic forwarding path rather than providing incomplete forwarding path." ## How a flow gets in -[`nft_flow_offload_eval()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n50) runs on a packet in the forward path and checks: +[`nft_flow_offload_eval()`][eval] runs on a packet in the forward path and checks: -- **Protocol.** TCP, UDP or GRE. For TCP the conntrack entry must be established and assured ([`nf_conntrack_tcp_established()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_conntrack_l4proto.h?h=v7.4-rc3#n206)) and the packet must not carry FIN or RST. UDP and GRE need no state check. GRE needs `CONFIG_NF_CT_PROTO_GRE` and is skipped when NAT is in use or when the conntrack tuple has a GRE key, which conntrack fills in only for GRE version 1 ([nf_conntrack_proto_gre.c:226-245](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_gre.c?h=v7.4-rc3#n226)); the code comment says "No support for GRE v1". The hook then matches only GRE version 0 ([nf_flow_table_ip.c:270](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n270)). -- **Skipped flows and packets.** A flow with a conntrack helper attached, with `IPS_SEQ_ADJUST` set or with `IPS_NAT_CLASH` set is skipped. Packets that carry a security path (IPsec) or IPv4 options are skipped ([`nft_flow_offload_skip()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n24)). -- **Confirmed.** The conntrack entry must be confirmed. -- **Only once.** `test_and_set_bit(IPS_OFFLOAD_BIT, ...)` makes the first caller win. +- **Protocol.** TCP, UDP or GRE. For TCP the conntrack entry must be established and assured ([`nf_conntrack_tcp_established()`][tcpest]; the status bit is described as "Conntrack should never be early-expired" in [nf_conntrack_common.h:51][uapiass]) and the packet must not carry FIN or RST. UDP and GRE need no state check. GRE needs `CONFIG_NF_CT_PROTO_GRE` and is skipped when NAT is in use or when the conntrack tuple has a GRE key, which conntrack fills in only for GRE version 1 ([nf_conntrack_proto_gre.c:226-245][grekey]); the code comment says "No support for GRE v1". The hook then matches only GRE version 0 ([nf_flow_table_ip.c:270][gre0]). +- **Skipped flows and packets.** A flow with a conntrack helper attached, with `IPS_SEQ_ADJUST` set ("Connection needs TCP sequence adjusted", [nf_conntrack_common.h:70][uapiseq]) or with `IPS_NAT_CLASH` set is skipped ([nft_flow_offload.c:98-99][evalcheck]). Packets that carry a security path (IPsec) or IPv4 options are skipped ([`nft_flow_offload_skip()`][skip]). +- **Confirmed.** The conntrack entry must be confirmed ([nft_flow_offload.c:102][evalconf]), which the header describes as "originating packet has left box" ([nf_conntrack_common.h:55][uapiconf]). +- **Only once.** `test_and_set_bit(IPS_OFFLOAD_BIT, ...)` makes the first caller win ([nft_flow_offload.c:105][evalonce]). -If all of that holds, [`nft_flow_route()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n272) resolves the path: it reuses the route of the packet being processed for one direction and looks up the reverse route ([nf_flow_table_path.c:305](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n305)). For routes without an IPsec transform it also requires the egress device to be in the flowtable and, on an Ethernet device, a neighbour entry in state `NUD_VALID` ([nf_flow_table_path.c:61-77](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n61)). [`flow_offload_alloc()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n53) then allocates the entry with a reference to the conntrack entry and the SNAT and DNAT flags, and [`flow_offload_add()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n335) inserts its two tuples, one per direction, into one resizable hash table. The comment in [`flow_offload_add()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n335) says garbage collection walks only the original-direction entries, so that one is inserted last. +If all of that holds, [`nft_flow_route()`][route] resolves the path: it reuses the route of the packet being processed for one direction and looks up the reverse route ([nf_flow_table_path.c:305][pathrev]). Only the packet's own direction can carry an IPsec transform; [`b5964aac51e0`][cb596] says "the existing basic xfrm offload (which only works in one direction)". For routes without an IPsec transform the resolution also requires the egress device to be in the flowtable and, on an Ethernet device, a neighbour entry in a valid state (`NUD_VALID`, [nf_flow_table_path.c:61-77][pathnud]). [`flow_offload_alloc()`][alloc] then allocates the entry with a reference to the conntrack entry and the SNAT and DNAT flags, and [`flow_offload_add()`][add] inserts its two tuples, one per direction, into one resizable hash table. The comment in `flow_offload_add()` says garbage collection walks only the original-direction entries, so that one is inserted last. -For TCP, [`flow_offload_ct_tcp()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n41) sets the "be liberal" flag on the conntrack entry; its comment says conntrack will not see all packets any more, so it can't validate the TCP window. The flowtable code never clears the flag ([nft_flow_offload.c:45-46](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n45) is the only place it is set in the flowtable code). +For TCP, [`flow_offload_ct_tcp()`][cttcp] sets the "be liberal" flag on the conntrack entry; its comment says conntrack will not see all packets any more, so it can't validate the TCP window. In the flowtable files the flag is set only at [nft_flow_offload.c:45-46][liberal] and cleared nowhere; the tc `ct` action sets it too ([act_ct.c:441-442][actct441]). ## Transmit modes -Each direction of a flow has a transmit type chosen at add time ([nf_flow_table_path.c:19](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n19)): +Each direction of a flow has a transmit type chosen at add time ([`nft_xmit_type()`][xmittype]): -- **Neighbour.** The default. The destination MAC comes from the neighbour entry for the next hop ([`nf_flow_queue_xmit4()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n838)). -- **Direct.** The entry stores the MAC addresses. The path code picks it for PPPoE paths ([nf_flow_table_path.c:155](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n155)), bridge paths ([nf_flow_table_path.c:187](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n187)), and Ethernet devices when hardware offload is on ([nf_flow_table_path.c:196](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n196)). -- **IPsec (xfrm).** Chosen for a route that has an IPsec transform. The route is the one the packet being processed carries; the reverse direction comes from a plain route lookup and gets its own type ([`nft_flow_route()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n272)). The hit goes out through `dst_output()` ([`nf_flow_xmit_xfrm()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n300)). +- **Neighbour.** The default. The destination MAC comes from the neighbour entry for the next hop ([`nf_flow_queue_xmit4()`][xmit4]). +- **Direct.** The entry stores the MAC addresses. The path code picks it for PPPoE paths ([nf_flow_table_path.c:155][pathppp]), bridge paths ([nf_flow_table_path.c:187][pathbr]), and Ethernet devices when hardware offload is on ([nf_flow_table_path.c:194-196][pathhw]). +- **IPsec (xfrm).** Chosen for a route that has an IPsec transform. The hit goes out through `dst_output()` ([`nf_flow_xmit_xfrm()`][xfrm]). -A flow caches its route in the entry, except in direct mode without a tunnel, where the route is released ([nf_flow_table_core.c:129-136](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n129)). +A flow caches its route in the entry, except in direct mode without a tunnel, where the route is released ([nf_flow_table_core.c:129-136][coredst]). ## What a hit does -For each packet, [`nf_flow_offload_forward()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n475) (called from [`nf_flow_offload_ip_hook()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n904)): +[`nf_flow_offload_ip_hook()`][iphook] looks up the packet's tuple. A miss returns `NF_ACCEPT` and the packet follows the classic path. On a hit, [`nf_flow_offload_forward()`][forward]: + +1. Checks the packet against the MTU recorded for the flow. A packet that is too big goes to the classic path ([`nf_flow_exceeds_mtu()`][mtu]); the kernel documentation gives the reason as the packet-too-big ICMP error ([nf_flowtable.rst:78-79][rst78]). +2. Checks TCP flags ([`nf_flow_state_check()`][state]; see [How a flow leaves](#how-a-flow-leaves)). +3. Checks the cached route, if the flow keeps one ([`nf_flow_dst_check()`][dstchk] returns true when there is none). A stale route tears the flow down and the packet goes to the classic path. Direct-mode flows without a tunnel keep no route, so no route change is noticed. In the bridge case, the commit that added direct mode, [`7a27f6ab4135`][c7a27], says "if peer is moved to different bridge port, the connection will time out". +4. Refreshes the deadline ([`flow_offload_refresh()`][refresh]), strips any VLAN, PPPoE or IPIP tunnel header (IPIP since v6.19, [`ab427db17885`][cab427]), applies the stored NAT rewrite, decrements the TTL, and updates conntrack's counters if the flowtable has `counter`. -1. Looks up the packet's tuple. A miss returns `NF_ACCEPT` and the packet follows the classic path. -2. Checks the packet against the MTU recorded for the flow. A packet that is too big goes to the classic path ([`nf_flow_exceeds_mtu()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n289)); the kernel documentation gives the reason as the packet-too-big ICMP error ([nf_flowtable.rst:78-79](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n78)). -3. Checks TCP flags ([`nf_flow_state_check()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n25); see [How a flow leaves](#how-a-flow-leaves)). -4. Checks the cached route, if the flow keeps one ([`nf_flow_dst_check()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_flow_table.h?h=v7.4-rc3#n314) returns true when there is none). A stale route tears the flow down and the packet goes to the classic path. Direct-mode flows without a tunnel keep no route, so no route change is noticed; the commit that added direct mode, [`7a27f6ab4135`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7a27f6ab41356ecba47ec2bec6d635704c169779), says "if peer is moved to different bridge port, the connection will time out". -5. Refreshes the deadline ([`flow_offload_refresh()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n367)), strips any VLAN, PPPoE or IPIP tunnel header (IPIP since v6.19, [`ab427db17885`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ab427db17885814069bae891834f20842f0ac3a4)), applies the stored NAT rewrite, decrements the TTL, and updates conntrack's counters if the flowtable has `counter`. -6. Pushes any egress VLAN, PPPoE or tunnel header back on, builds the link-layer header with `dev_hard_header()` and transmits as described in [Transmit modes](#transmit-modes) ([`__nf_flow_queue_xmit()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n776)). +The hook then transmits as described in [Transmit modes](#transmit-modes): [`nf_flow_queue_xmit4()`][xmit4] pushes any egress VLAN, PPPoE or tunnel header back on, builds the link-layer header with `dev_hard_header()` and calls `dev_queue_xmit()`. IPsec hits skip that and go out through `dst_output()`. -[`nf_flow_offload_ipv6_hook()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1233) does the same with the hop limit in place of the TTL ([nf_flow_table_ip.c:1069](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1069)). The next header has to be TCP, UDP or GRE, so a packet with any extension header is a miss ([nf_flow_table_ip.c:1050-1067](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1050)). It strips IPv6-in-IPv6 tunnel headers since v7.0 ([`d98103575dcd`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=d98103575dcdd3a730e0901ab457791a9ac6930c)). Mixed-family tunnels are not supported: [`d0107f381622`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=d0107f38162279d9cb0e012210731176265f835d) says IPv4 over IPv6 and SIT tunnel offloading "is not enabled yet". +[`nf_flow_offload_ipv6_hook()`][ip6hook] does the same, with the hop limit in place of the TTL ([nf_flow_table_ip.c:1069][ip6ttl] checks it, [nf_flow_table_ip.c:1145][ip6dec] decrements it). The next header has to be TCP, UDP or GRE, so a packet with any extension header is a miss ([nf_flow_table_ip.c:1050-1067][ip6ext]). It strips IPv6-in-IPv6 tunnel headers since v7.0 ([`d98103575dcd`][cd9810]). Mixed-family tunnels are not supported: the IPv4 hook accepts only IPIP ([nf_flow_table_ip.c:331][ipip]), the IPv6 hook only IPv6-in-IPv6 ([nf_flow_table_ip.c:356][ip6ip6]), and [`d0107f381622`][cd0107] says IPv4 over IPv6 and SIT tunnel offloading "is not enabled yet". -Packets that the hook doesn't take go to the classic path: fragments and packets with IPv4 options ([nf_flow_table_ip.c:230](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n230)), a TTL or hop limit of 1 or less ([nf_flow_table_ip.c:253](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n253)), and GRE other than version 0. +Packets that the hook doesn't take go to the classic path: fragments and packets with IPv4 options ([nf_flow_table_ip.c:230][frag]), a TTL or hop limit of 1 or less ([nf_flow_table_ip.c:253][ttl], [nf_flow_table_ip.c:1069][ip6ttl]), and GRE other than version 0 ([nf_flow_table_ip.c:270][gre0]). ## How a flow leaves -[`nf_flow_offload_work_gc()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n620) runs once a second ([nf_flow_table_core.c:626](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n626)) and removes entries that have been torn down. Teardown is triggered by ([`nf_flow_offload_gc_step()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n576)): +[`nf_flow_offload_work_gc()`][gcwork] re-queues itself one second after each run ([nf_flow_table_core.c:626][gcrequeue]) and removes entries that have been torn down. Teardown is triggered by: -- **A timeout.** Each entry has a deadline. A software hit pushes it out, at most once a second ([nf_flow_table_core.c:373](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n373)). The default is 30 seconds for TCP, UDP ([nf_conntrack_proto_tcp.c:1658](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n1658), [nf_conntrack_proto_udp.c:204](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_udp.c?h=v7.4-rc3#n204)) and GRE ([nf_flow_table.h:209](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_flow_table.h?h=v7.4-rc3#n209)). The TCP and UDP values are the sysctls `net.netfilter.nf_flowtable_tcp_timeout` and `net.netfilter.nf_flowtable_udp_timeout` ([nf_conntrack_standalone.c:789](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_standalone.c?h=v7.4-rc3#n789), [nf_conntrack_standalone.c:839](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_standalone.c?h=v7.4-rc3#n839)); GRE has none. For hardware flows, the collector asks the driver for statistics once less than 90% of the timeout remains and extends the deadline from the last-used time ([`nf_flow_offload_stats()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1149), [`flow_offload_work_stats()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1011)). -- **A dying conntrack entry.** `nf_ct_is_dying()` ([`nf_flow_offload_gc_step()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n576)). -- **A stale route**, for flows that keep one. The collector checks this since v7.3 ([`edd51a233438`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=edd51a23343870dbd7cedf6e2765c13cf7a5ccbc)); the hook checks it too ([`nf_flow_offload_forward()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n475)). -- **A missing device or neighbour.** The hook tears the flow down and drops that packet ([`nf_flow_queue_xmit4()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n838)). -- **A device going down.** The `NETDEV_DOWN` notifier calls [`nf_flow_table_cleanup()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n760) ([nft_flow_offload.c:248](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n248)). -- **Deleting the flowtable**, which includes `flush ruleset` ([`nf_flow_table_free()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n773)). +- **A timeout.** Each entry has a deadline. A software hit pushes it out, at most once a second ([nf_flow_table_core.c:373][corelimit]); this also applies to flows that are closing. The default is 30 seconds for TCP ([nf_conntrack_proto_tcp.c:1658][tcpoff]) and UDP ([nf_conntrack_proto_udp.c:204][udpoff]), set by the sysctls `net.netfilter.nf_flowtable_tcp_timeout` and `net.netfilter.nf_flowtable_udp_timeout` ([nf_conntrack_standalone.c:789][sysctltcp], [nf_conntrack_standalone.c:839][sysctludp]), and 30 seconds for GRE, which has no sysctl ([`flow_offload_get_timeout()`][gettimeout], [nf_flow_table.h:209][flowtimeout]). For hardware flows, the collector asks the driver for statistics once less than 90% of the timeout remains and extends the deadline from the last-used time ([`nf_flow_offload_stats()`][hwstats], [`flow_offload_work_stats()`][hwstatwork]). +- **A dying conntrack entry**, checked by `nf_ct_is_dying()` ([`nf_flow_offload_gc_step()`][gcstep]); lookups also miss ([`flow_offload_lookup()`][lookup]). +- **A stale route**, for flows that keep one. The collector checks this since v7.3 ([`edd51a233438`][cedd51]); the hook checks it too ([`nf_flow_offload_forward()`][forward]). +- **A missing output device, or a neighbour lookup error.** The hook tears the flow down and drops that packet ([nf_flow_table_ip.c:870-880][neigherr]). A missing neighbour entry is created by the lookup, so only a failure to create it counts. +- **A device going down.** The `NETDEV_DOWN` notifier ([nft_flow_offload.c:248][notifier]) calls [`nf_flow_table_cleanup()`][cleanup], which tears down the flows that enter through that device ([`nf_flow_table_do_cleanup()`][docleanup]). +- **Deleting the flowtable** ([`nf_flow_table_free()`][free]). -**TCP FIN and RST** don't end the flow at once (since v6.14, [`fdbaf5163331`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fdbaf5163331342e90a2c29b87629021f4c15f0c)). [`nf_flow_state_check()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n25) marks the flow `NF_FLOW_CLOSING` and the packet still takes the fast path; the collector removes a hardware entry early ([`nf_flow_offload_gc_step()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n576)). A later SYN on a closing flow tears it down, sets the conntrack state to close and sends that packet to the classic path ([`nf_flow_state_check()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n25), [`flow_offload_fixup_tcp()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n178)). +**TCP FIN and RST** don't end the flow at once (since v6.14, [`fdbaf5163331`][cfdbaf]). [`nf_flow_state_check()`][state] marks the flow `NF_FLOW_CLOSING` and the packet still takes the fast path; the collector removes a hardware entry early ([`nf_flow_offload_gc_step()`][gcstep]). Software hits keep pushing the deadline, so the flow leaves when the timeout expires after the last packet, and [`flow_offload_fixup_ct()`][fixup] then gives conntrack the close state and timeout. A later SYN on a closing flow tears it down at once, sets the conntrack state to close and sends that packet to the classic path ([`nf_flow_state_check()`][state], [`flow_offload_fixup_tcp()`][fixtcp]). -**Adding again.** [`flow_offload_teardown()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n399) clears the offload bit on the conntrack entry ([nf_flow_table_core.c:401](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n401)), so a later forward-chain packet that matches the `flow add` rule can add the flow again. +**Adding again.** [`flow_offload_teardown()`][tear] clears the offload bit on the conntrack entry ([nf_flow_table_core.c:401][coreclear]), so a later forward-chain packet that matches the `flow add` rule can add the flow again. -**Conntrack timeout.** While a flow is in the table, conntrack may see few or none of its packets. [`flow_offload_add()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n335) first sets the conntrack timeout to a day ([nf_flow_table_core.c:358](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n358)). Afterwards the collector calls [`nf_flow_table_extend_ct_timeout()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n517), which extends it when less than five minutes remain, by an amount that depends on the protocol and TCP state ([`nf_flow_table_tcp_timeout()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n479)). The function's comment calls the value "an arbitrary timeout large enough not to ever expire". +**Conntrack timeout.** While a flow is in the table, conntrack may see few or none of its packets; the comment in [`nf_flow_table_extend_ct_timeout()`][ext] says so ([nf_flow_table_core.c:506-513][gccomment]). [`flow_offload_add()`][add] first sets the conntrack timeout to a day ([nf_flow_table_core.c:358][coreday]). Afterwards the collector calls [`nf_flow_table_extend_ct_timeout()`][ext], which sets a new timeout when less than five minutes remain. The value depends on the protocol and TCP state ([`nf_flow_table_tcp_timeout()`][tcpto]): -| Flow | Conntrack timeout is extended to | -|------|----------------------------------| +| Flow | Conntrack timeout is set to | +|------|-----------------------------| | UDP, GRE | a day | | TCP established | a day | | TCP FIN_WAIT, CLOSE_WAIT, LAST_ACK, TIME_WAIT | five minutes | | Other TCP states (SYN_SENT, SYN_RECV, CLOSE, ...) | no extension | -**Hand-back to conntrack.** [`flow_offload_fixup_ct()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n201) runs at teardown. For TCP it keeps the last conntrack state, or sets close if the flow was closing, and clears the recorded maximum window and the max-ack flag; the code comment says that otherwise conntrack's TCP reset validation would fail ([`flow_offload_fixup_tcp()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n178)). TCP and UDP flows have the conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired; a closing TCP flow gets the close timeout unconditionally ([nf_flow_table_core.c:250-252](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n250)). Except for that case the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n64)), keeps whatever is left of its day. GRE flows return before any timeout change ([nf_flow_table_core.c:240-241](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n240)). +The comment in [`nf_flow_table_extend_ct_timeout()`][ext] calls the value "an arbitrary timeout large enough not to ever expire". + +**Hand-back to conntrack.** [`flow_offload_fixup_ct()`][fixup] runs at teardown. For TCP it keeps the last conntrack state, or sets close if the flow was closing, and clears the recorded maximum window and the max-ack flag; the code comment says that otherwise conntrack's TCP reset validation would fail ([`flow_offload_fixup_tcp()`][fixtcp]). TCP and UDP flows have the conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired; a closing TCP flow gets the close timeout unconditionally ([nf_flow_table_core.c:250-252][corefix]). Except for that case the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64][tcp5d]), keeps whatever is left of its day. GRE flows return before any timeout change ([nf_flow_table_core.c:240-241][coregre]). ## Hardware offload -The kernel documentation says a flowtable can offload flows to a device that supports it, enabled with `flags offload`, and that a workqueue adds the flows, so a few packets can still take the software path first ([nf_flowtable.rst:183-201](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n183)). +The kernel documentation says a flowtable can offload flows to a device that supports it, enabled with `flags offload`, and that a workqueue adds the flows, so a few packets can still take the software path first ([nf_flowtable.rst:183-201][rst183]). The driver interface is the tc flow-block interface: -- **Binding.** Registering a flowtable's ingress hook on a device first calls the flowtable type's `setup` callback, [`nf_flow_table_offload_setup()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1271) ([nf_tables_api.c:9068](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9068)). Without `flags offload` it only sets up the XDP path ([nf_flow_table_offload.c:1279-1280](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1279)); XDP programs can then look the table up with [`bpf_xdp_flow_lookup`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_bpf.c?h=v7.4-rc3#n59) ([`391bb6594fd3`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=391bb6594fd3a567efb1cd3efc8136c78c4c9e31), v6.11). With `flags offload`, if the device has `ndo_setup_tc` the flowtable calls it with `TC_SETUP_FT`, and otherwise it uses the indirect-block path `flow_indr_dev_setup_offload()` ([`nf_flow_table_offload_cmd()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1252), [`nf_flow_table_indr_offload_cmd()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1239)). The driver adds its callback to the flowtable's block. -- **Per flow.** [`nf_flow_offload_tuple()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n901) calls every callback with `TC_SETUP_CLSFLOWER` and a `flow_cls_offload` request, the request type tc flower uses. The command is `FLOW_CLS_REPLACE` to add a flow (with a flow rule: match keys plus actions such as header rewrites), `FLOW_CLS_DESTROY` or `FLOW_CLS_STATS`. -- **In-tree drivers that handle `TC_SETUP_FT`** as of v7.4-rc3: the mlx5 representor code ([rep/tc.c:261](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mellanox/mlx5/core/en/rep/tc.c?h=v7.4-rc3#n261)), the MediaTek Ethernet and WED drivers ([mtk_ppe_offload.c:674](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mediatek/mtk_ppe_offload.c?h=v7.4-rc3#n674), [mtk_wed.c:2790](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mediatek/mtk_wed.c?h=v7.4-rc3#n2790)), the mt76 Wi-Fi driver ([npu.c:385](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/wireless/mediatek/mt76/npu.c?h=v7.4-rc3#n385)) and the Airoha Ethernet driver ([airoha_eth.c:3674](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/airoha/airoha_eth.c?h=v7.4-rc3#n3674)). DSA user ports pass the command to the conduit device ([dsa/user.c:1729](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/dsa/user.c?h=v7.4-rc3#n1729)). `netdevsim` returns 0 and registers no callback ([netdevsim/tc.c:92](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/netdevsim/tc.c?h=v7.4-rc3#n92)). +- **Binding.** Registering a flowtable's ingress hook on a device first calls the flowtable type's `setup` callback, [`nf_flow_table_offload_setup()`][setup] ([nf_tables_api.c:9068][apisetup]). Without `flags offload` it only sets up the XDP path ([nf_flow_table_offload.c:1279-1280][offsetup]); XDP programs can then look the table up with [`bpf_xdp_flow_lookup`][bpf] ([`391bb6594fd3`][c391bb], v6.11). With `flags offload`, if the device has `ndo_setup_tc` the flowtable calls it with `TC_SETUP_FT` ([`nf_flow_table_offload_cmd()`][offcmd]); otherwise it uses the indirect-block path `flow_indr_dev_setup_offload()` ([`nf_flow_table_indr_offload_cmd()`][indrcmd]). The driver's callback is added to the flowtable's block ([nf_flow_table_offload.c:1193][splice]). +- **Per flow.** [`nf_flow_offload_tuple()`][offtuple] calls every callback with `TC_SETUP_CLSFLOWER` and a `flow_cls_offload` request, the request type tc flower uses. The command is `FLOW_CLS_REPLACE` to add a flow (with a flow rule: match keys plus actions such as header rewrites), `FLOW_CLS_DESTROY` or `FLOW_CLS_STATS`. +- **In-tree drivers that handle `TC_SETUP_FT`** as of v7.4-rc3: the mlx5 representor code ([rep/tc.c:261][mlx5]), the MediaTek Ethernet and WED drivers ([mtk_ppe_offload.c:674][mtkppe], [mtk_wed.c:2790][mtkwed]), the mt76 Wi-Fi driver ([npu.c:385][mt76]) and the Airoha Ethernet driver ([airoha_eth.c:3674][airoha]). DSA user ports pass the command to the conduit device ([dsa/user.c:1729][dsa]). `netdevsim` returns 0 and registers no callback ([netdevsim/tc.c:92][netdevsim]). Behaviour with `flags offload`, from the source: -- A device that can't offload makes the flowtable fail to load for devices present when it is created: the `setup` error is returned before the hook is registered ([nf_tables_api.c:9068-9071](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9068)). A change of the `offload` flag on an existing flowtable is rejected with `-EOPNOTSUPP` ([nf_tables_api.c:9196-9203](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9196)). -- Every Ethernet path uses direct mode ([nf_flow_table_path.c:194-196](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n194)), so a flow without a tunnel caches no route. -- A hardware add that fails isn't reported. The flow stays in software with `NF_FLOW_HW` set, `[HW_OFFLOAD]` is set if the driver accepted at least one direction ([`flow_offload_rule_add()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n957), [`flow_offload_work_add()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n973)), and the add is queued again on later software hits, at most once a second and not for closing flows ([nf_flow_table_core.c:373-383](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n373)). -- Flows through an IPIP or IPv6-in-IPv6 tunnel stay in software ([`nf_flow_offload_unsupported()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1105); [`6c5dcab95f4c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6c5dcab95f4cd42a1648739ec9300fbb4b1a021f), v7.2). -- The collector skips an entry whose hardware work is still queued ([nf_flow_table_core.c:581](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n581)). +- A device that can't offload makes the flowtable fail to load for devices present when it is created: the `setup` error is returned before the hook is registered ([nf_tables_api.c:9068][apisetup]). A change of the `offload` flag on an existing flowtable is rejected with `-EOPNOTSUPP` ([nf_tables_api.c:9200-9203][apitoggle]). +- Every Ethernet-device path except the IPsec direction uses direct mode ([nf_flow_table_path.c:194-196][pathhw]), so a flow without a tunnel caches no route. +- A hardware add that fails isn't reported. The flow stays in software with `NF_FLOW_HW` set, `[HW_OFFLOAD]` is set if the driver accepted at least one direction ([`flow_offload_rule_add()`][ruleadd], [`flow_offload_work_add()`][workadd]), and the add is queued again on later software hits, at most once a second and not for closing flows ([nf_flow_table_core.c:373-383][corelimit]). +- Flows through an IPIP or IPv6-in-IPv6 tunnel stay in software ([`nf_flow_offload_unsupported()`][unsup]; [`6c5dcab95f4c`][c6c5d], v7.2). +- The collector skips an entry whose hardware work is still queued ([nf_flow_table_core.c:581][gcpending]). ## Inspecting -- **Conntrack listing.** `conntrack -L` shows `[OFFLOAD]` for software flows and `[HW_OFFLOAD]` for hardware flows ([nf_flowtable.rst:111-113](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n111), [nf_flowtable.rst:202-206](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n202); the tags are printed at [nf_conntrack_standalone.c:366](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_standalone.c?h=v7.4-rc3#n366)). -- **What offloaded entries omit.** ctnetlink skips the timeout and protocol info ([nf_conntrack_netlink.c:579](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_netlink.c?h=v7.4-rc3#n579)), and `/proc/net/nf_conntrack` skips the timeout and the TCP state ([nf_conntrack_standalone.c:339](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_standalone.c?h=v7.4-rc3#n339), [`tcp_print_conntrack()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n266)). -- **Statistics.** With `CONFIG_NF_FLOW_TABLE_PROCFS` ([Kconfig:721](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/Kconfig?h=v7.4-rc3#n721)), [`/proc/net/stat/nf_flowtable`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_procfs.c?h=v7.4-rc3#n43) shows per-CPU counters of pending hardware add, delete and stats work. They are incremented when work is queued and decremented when it completes ([nf_flow_table_offload.c:1047-1077](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1047)). There are no hit or miss counters. +- **Conntrack listing.** `conntrack -L` shows `[OFFLOAD]` for software flows and `[HW_OFFLOAD]` for hardware flows ([nf_flowtable.rst:111-113][rst111], [nf_flowtable.rst:202-206][rst202]). `/proc/net/nf_conntrack` prints the same tags ([nf_conntrack_standalone.c:364-366][proctag]). +- **What offloaded entries omit.** ctnetlink skips the timeout and protocol info ([nf_conntrack_netlink.c:579][nlnk]), and `/proc/net/nf_conntrack` skips the timeout and the TCP state ([nf_conntrack_standalone.c:339][procnf], [`tcp_print_conntrack()`][tcpprint]). +- **Statistics.** With `CONFIG_NF_FLOW_TABLE_PROCFS` ([Kconfig:721][kconfig]), [`/proc/net/stat/nf_flowtable`][procfs] shows per-CPU counters of pending hardware add, delete and stats work. They are incremented when work is queued and decremented when it completes ([nf_flow_table_offload.c:1047-1077][statinc]). There are no hit or miss counters. ## Limits -- **Rule changes.** Changing or deleting a rule is not among the triggers listed in [How a flow leaves](#how-a-flow-leaves). A hit never reaches the forward chain, and a software hit pushes the deadline out ([`flow_offload_refresh()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n367)). Only the stored NAT rewrite is applied to a hit ([`nf_flow_offload_forward()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n475)). -- **Cache.** The kernel documentation says the table behaves like a cache and that entries can go stale if the destination MAC address or the egress device changes. It names software mode combined with bridging and IP forwarding, and hardware offload, as the risky setups ([nf_flowtable.rst:214-222](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n214)). -- **IPsec.** A packet that carries a security path never triggers an add ([`nft_flow_offload_skip()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n24)). In the direction whose route has an IPsec transform the device-list and neighbour checks are skipped ([nf_flow_table_path.c:314-320](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n314)); the other direction gets them like any other flow. -- **ip6gre.** The commit that extended the GRE timeout, [`46ecda278f6e`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=46ecda278f6eedffb2898d41e3e56757c996a41f), says `ip6gre` tunnels with the default encapsulation limit carry a Destination Options header before GRE, which the flowtable doesn't parse, so every packet of the flow takes the classic path. +- **Rule changes.** Removing a rule runs the expression's `deactivate` and `destroy` callbacks, which release its references to the flowtable and to conntrack and don't touch flow entries ([nft_flow_offload.c:185-206][deact]). A hit never reaches the forward chain, and a software hit pushes the deadline out ([`flow_offload_refresh()`][refresh]). Only the stored NAT rewrite is applied to a hit ([`nf_flow_offload_forward()`][forward]). The ways a flow leaves are listed in [How a flow leaves](#how-a-flow-leaves). +- **Cache.** The kernel documentation says the table behaves like a cache and that entries might get stale if the destination MAC address or the egress device changes, which "might be a problem" with software mode combined with bridging and IP forwarding, and with hardware offload ([nf_flowtable.rst:214-222][rst214]). +- **IPsec.** A packet that carries a security path never triggers an add ([`nft_flow_offload_skip()`][skip]). In the direction whose route has an IPsec transform the device-list and neighbour checks are skipped ([nf_flow_table_path.c:314-320][pathskip]); the other direction gets them like any other flow. +- **ip6gre.** The commit that extended the GRE timeout, [`46ecda278f6e`][c46ecd], says `ip6gre` tunnels with the default encapsulation limit carry a Destination Options header before GRE, which the flowtable doesn't parse, so every packet of the flow takes the classic path. ## Where it came from -The design was posted to netfilter-devel by Pablo Neira Ayuso. LWN reproduces two of the postings, and the kernel documentation cites them ([nf_flowtable.rst:224-235](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n224)). +The design was posted to netfilter-devel by Pablo Neira Ayuso. LWN reproduces two of the postings, and the kernel documentation cites them ([nf_flowtable.rst:224-235][rst224]). -- **3 November 2017**, [`[PATCH RFC,WIP 0/5] Flow offload infrastructure`](https://lwn.net/Articles/738214/) (Message-ID `<20171103152636.9967-1-pablo@netfilter.org>`). The cover letter describes a module with a hook at ingress, populated by an nftables action once a flow is established. It gives the hardware motivation: "Switches and NICs come with built-in flow table, I've been observing out of tree patches in OpenWRT/LEDE to integrate this into Netfilter for a little while." It reports a measurement: "I'm measuring here that the software flow table forwarding path is 2.5 faster than the classic forwarding path in my testbed." Its TODO list included IPv4 only, IPv4 SNAT only, no netns support and no flow counters, and the last patch added `ndo` hooks to populate a hardware flow table. -- **22 December 2017**, [`[PATCH nf-next,v3 0/7] Flow offload infrastructure`](https://lwn.net/Articles/742164/) (Message-ID `<20171222192732.13188-1-pablo@netfilter.org>`). The cover letter lists IPv6 and mixed IPv4/IPv6 support, hardware offload in a separate `nf_flow_table_hw` module, port translation and netns support. The hardware patch was tagged RFC, and the cover letter says "I will keep this one back until there's an initial driver that introduces flow table offload support". +- **3 November 2017**, [`[PATCH RFC,WIP 0/5] Flow offload infrastructure`][lwn1] (Message-ID `<20171103152636.9967-1-pablo@netfilter.org>`). The cover letter describes a module with a hook at ingress, populated by an nftables action once a flow is established. It gives the hardware motivation: "Switches and NICs come with built-in flow table, I've been observing out of tree patches in OpenWRT/LEDE to integrate this into Netfilter for a little while." It reports a measurement: "I'm measuring here that the software flow table forwarding path is 2.5 faster than the classic forwarding path in my testbed." Its TODO list included IPv4 only, IPv4 SNAT only, no netns support and no flow counters, and the last patch added `ndo` hooks to populate a hardware flow table. +- **22 December 2017**, [`[PATCH nf-next,v3 0/7] Flow offload infrastructure`][lwn2] (Message-ID `<20171222192732.13188-1-pablo@netfilter.org>`). The cover letter lists IPv6 and mixed IPv4/IPv6 support, hardware offload in a separate `nf_flow_table_hw` module, port translation and netns support. The hardware patch was tagged RFC, and the cover letter says "I will keep this one back until there's an initial driver that introduces flow table offload support". -The software flow table was committed on 8 January 2018 ([`ac2a66665e23`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ac2a66665e231847cab11b8c8e844ce43207dd2e) adds the generic infrastructure and [`a3c90f7a2323`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a3c90f7a2323b331ae816d5b0633e68148e25d04) the nftables expression) and first appeared in v4.16. Felix Fietkau reorganised the code in a series written in February 2018 and merged for v4.18, for example in [`1a999d899bc2`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1a999d899bc26abee26d70c10762c81b9d684d2b), which renamed `nf_flow_table.c` to `nf_flow_table_core.c`. +The software flow table was committed on 8 January 2018 ([`ac2a66665e23`][cac2a] adds the generic infrastructure and [`a3c90f7a2323`][ca3c9] the nftables expression) and first appeared in v4.16. Felix Fietkau reorganised the code in a series written in February 2018 and merged for v4.18, for example in [`1a999d899bc2`][c1a99], which renamed `nf_flow_table.c` to `nf_flow_table_core.c`. -Hardware offload reached the tree in v5.5 through the tc flow-block interface rather than through the dedicated `ndo` hooks proposed in the RFC. Pablo Neira Ayuso added it in [`c29f74e0df7a`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c29f74e0df7a02b8303bcdce93a7c0132d62577a), which called `ndo_setup_tc` with `TC_SETUP_BLOCK` and the callbacks with `TC_SETUP_FT`. The arrangement described above came from two fixes by wenxu in the same release, [`ee1bcfe01251`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ee1bcfe01251e8958b53de45d2b0c85e09dd2719) and [`e052901991ae`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e052901991ae21e15851bfc89c682bfcb39a4dcf). wenxu added the indirect path in [`b5140a36da78`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b5140a36da7876bc084a2c680c8dbc7438db2051) (v5.7). +Hardware offload reached the tree in v5.5 through the tc flow-block interface rather than through the dedicated `ndo` hooks proposed in the RFC. Pablo Neira Ayuso added it in [`c29f74e0df7a`][cc29f], which called `ndo_setup_tc` with `TC_SETUP_BLOCK` and the callbacks with `TC_SETUP_FT`. The arrangement described above came from two fixes by wenxu in the same release, [`ee1bcfe01251`][cee1b] and [`e052901991ae`][ce052]. wenxu added the indirect path in [`b5140a36da78`][cb514] (v5.7). -**GRE timeout history.** The conntrack timeout extension was reworked in [`03428ca5cee9`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=03428ca5cee9f0792edc996c06ce4514816af1fb) (v6.14), but it never took effect until [`53b3e60edb67`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=53b3e60edb674b442b2b3bbdba484667b0f47a5d) (v7.2): it compared a relative value with the absolute `ct->timeout`, so the update never succeeded. It also did not handle GRE. Once a GRE entry's timeout fell below five minutes it hit a `WARN_ON_ONCE()`, and from v7.2 it also got a one-jiffy timeout, until [`46ecda278f6e`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=46ecda278f6eedffb2898d41e3e56757c996a41f) (v7.4-rc3). The packets that bring the timeout down are those that still go through conntrack; the commit message gives packets with a TTL of 1 or less as an example. These three fixes carry `Cc: stable`, so the mainline release numbers above are not the only ones with the fix. +**GRE timeout history.** The conntrack timeout extension was reworked in [`03428ca5cee9`][c0342] (v6.14), but it never took effect until [`53b3e60edb67`][c53b3] (v7.2): it compared a relative value with the absolute `ct->timeout`, so the update never succeeded. It also did not handle GRE. Once a GRE entry's timeout fell below five minutes it hit a `WARN_ON_ONCE()`, and from v7.2, when the update started to succeed, the default branch set the timeout to one jiffy; [`46ecda278f6e`][c46ecd] (v7.4-rc3) says "the default branch sets the new timeout to 1 jiffy, so the conntrack entry expires right away" and extends GRE like UDP. The two fixes, `53b3e60edb67` and `46ecda278f6e`, carry `Cc: stable`, so the mainline release numbers above are not the only ones with the fix. The packets that bring a GRE entry's timeout down are those that still go through conntrack; the commit message gives packets with a TTL of 1 or less as an example. ## Differences from `nf_flowtable.rst` -The kernel documentation was last updated with new content in March 2021 (v5.13, [`143490cde566`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=143490cde5669e0151dff466a7c2cf70e2884fb7)); a build-warning fix followed the next day ([`794d9b25817a`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=794d9b25817a813f1d2fb8e133ba6d2f53920853)). As of v7.4-rc3, at least these statements in it no longer match the source: +The kernel documentation was last updated with new content in March 2021 (v5.13, [`143490cde566`][c1434]); a build-warning fix followed the next day ([`794d9b25817a`][c794d]). As of v7.4-rc3, at least these statements in it no longer match the source: -- It says TCP RST and FIN packets are passed up to the classic path ([nf_flowtable.rst:77-78](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n77)). Since [`fdbaf5163331`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fdbaf5163331342e90a2c29b87629021f4c15f0c) ("netfilter: flowtable: add CLOSING state", v6.14), they stay on the fast path and the flow enters the closing state. The commit message explains that the old behaviour "leads to packet races when restoring the conntrack state as well as late flow re-offload situations". -- It says hits are sent with `neigh_xmit()` ([nf_flowtable.rst:21](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n21), [nf_flowtable.rst:74](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n74)). Direct mode has called `dev_queue_xmit()` since [`7a27f6ab4135`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7a27f6ab41356ecba47ec2bec6d635704c169779) (v5.13, the same day as the documentation update), and [`b5964aac51e0`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b5964aac51e0c286a50e68225e0dfcf11fb554cb) ("netfilter: flowtable: consolidate xmit path", v6.19) moved neighbour mode to it too. -- It says packets are not seen "from any of the Netfilter hooks coming after ingress" ([nf_flowtable.rst:22-23](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n22)). The netfilter egress hook, added in [`42df6e1d221d`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=42df6e1d221dddc0f2acf2be37e68d553ad65f96) (v5.16, after the documentation was written), does see software hits, because they end in `dev_queue_xmit()`. -- It says the rule adds the flow on "the TCP syn-ack packet" ([nf_flowtable.rst:105-106](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n105)). The code requires an established and assured conntrack entry ([`nf_conntrack_tcp_established()`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_conntrack_l4proto.h?h=v7.4-rc3#n206)). [`e5eaac2beb54`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e5eaac2beb54f0a16ff851125082d9faeb475572) (v5.18) added that check, and its message says the patch makes `nft_flow_offload` "skip packets in SYN_RECV state" and "postpones offload up to the next packet". -- It says the flowtable supports TCP and UDP ([nf_flowtable.rst:10](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n10)). GRE has been accepted since [`4e8d9584d154`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4e8d9584d154479d357327f76d4e49486915c9c9) (v5.18). +- It says TCP RST and FIN packets are passed up to the classic path ([nf_flowtable.rst:77-78][rst77]). Since [`fdbaf5163331`][cfdbaf] ("netfilter: flowtable: add CLOSING state", v6.14), they stay on the fast path and the flow enters the closing state. The commit message explains that the old behaviour "leads to packet races when restoring the conntrack state as well as late flow re-offload situations". +- It says hits are sent with `neigh_xmit()` ([nf_flowtable.rst:21][rst21], [nf_flowtable.rst:74][rst74]). Direct mode has called `dev_queue_xmit()` since [`7a27f6ab4135`][c7a27] (v5.13, the same day as the documentation update), and [`b5964aac51e0`][cb596] ("netfilter: flowtable: consolidate xmit path", v6.19) moved neighbour mode to it too. +- It says packets are not seen "from any of the Netfilter hooks coming after ingress" ([nf_flowtable.rst:22-23][rst22]). The netfilter egress hook, added in [`42df6e1d221d`][c42df] (v5.16, after the documentation was written), does see software hits, because they end in `dev_queue_xmit()`. An IPsec hit also runs postrouting, because it goes out through `dst_output()`. +- It says the rule adds the flow on "the TCP syn-ack packet" ([nf_flowtable.rst:105-106][rst105]). The code requires an established and assured conntrack entry ([`nf_conntrack_tcp_established()`][tcpest]). [`e5eaac2beb54`][ce5ea] (v5.18) added that check, and its message says the patch makes `nft_flow_offload` "skip packets in SYN_RECV state" and "postpones offload up to the next packet". +- It says the flowtable supports TCP and UDP ([nf_flowtable.rst:10][rst10]). GRE has been accepted since [`4e8d9584d154`][c4e8d] (v5.18). ## Further reading -- [`Documentation/networking/nf_flowtable.rst`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst) — the kernel's own description, with the VLAN, PPPoE, bridge and counter details and the versions that added them -- [`net/netfilter/nft_flow_offload.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c) — the `flow add` statement -- [`net/netfilter/nf_flow_table_path.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c) — route and device resolution when a flow is added -- [`net/netfilter/nf_flow_table_core.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c) — the table, timeouts and garbage collection -- [`net/netfilter/nf_flow_table_ip.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c) — the IPv4 and IPv6 fast path -- [`net/netfilter/nf_flow_table_offload.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c) — hardware offload +- [`Documentation/networking/nf_flowtable.rst`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3) — the kernel's own description, with the VLAN, PPPoE, bridge and counter details and the versions that added them +- [`net/netfilter/nft_flow_offload.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3) — the `flow add` statement +- [`net/netfilter/nf_flow_table_path.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3) — route and device resolution when a flow is added +- [`net/netfilter/nf_flow_table_core.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3) — the table, timeouts and garbage collection +- [`net/netfilter/nf_flow_table_ip.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3) — the IPv4 and IPv6 fast path +- [`net/netfilter/nf_flow_table_offload.c`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3) — hardware offload - [`a3c90f7a2323`](https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a3c90f7a2323b331ae816d5b0633e68148e25d04) — netfilter: nf_tables: flow offload expression (Pablo Neira Ayuso, January 2018) - The 2017 postings on LWN, [RFC of 3 November](https://lwn.net/Articles/738214/) and [v3 of 22 December](https://lwn.net/Articles/742164/) — the cover letters with the original rationale, which the kernel documentation cites - [Netfilter Architecture](netfilter.md) — the hook framework flowtable bypasses - [Connection Tracking](conntrack.md) — the entries flowtable keeps alive - [nftables vs iptables](nftables-iptables.md) — how to write the ruleset + +[acct]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_conntrack_acct.h?h=v7.4-rc3#n36 +[acctadd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_core.c?h=v7.4-rc3#n929 +[acctext]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_core.c?h=v7.4-rc3#n1789 +[acctsysctl]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_standalone.c?h=v7.4-rc3#n691 +[actct441]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/sched/act_ct.c?h=v7.4-rc3#n441 +[actct55]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/sched/act_ct.c?h=v7.4-rc3#n55 +[add]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n335 +[airoha]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/airoha/airoha_eth.c?h=v7.4-rc3#n3674 +[alloc]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n53 +[apisetup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9068 +[apitoggle]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9200 +[bpf]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_bpf.c?h=v7.4-rc3#n59 +[c0342]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=03428ca5cee9f0792edc996c06ce4514816af1fb +[c1434]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=143490cde5669e0151dff466a7c2cf70e2884fb7 +[c1a99]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1a999d899bc26abee26d70c10762c81b9d684d2b +[c391bb]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=391bb6594fd3a567efb1cd3efc8136c78c4c9e31 +[c42df]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=42df6e1d221dddc0f2acf2be37e68d553ad65f96 +[c46ecd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=46ecda278f6eedffb2898d41e3e56757c996a41f +[c4e8d]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4e8d9584d154479d357327f76d4e49486915c9c9 +[c53b3]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=53b3e60edb674b442b2b3bbdba484667b0f47a5d +[c6c5d]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6c5dcab95f4cd42a1648739ec9300fbb4b1a021f +[c794d]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=794d9b25817a813f1d2fb8e133ba6d2f53920853 +[c7a27]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7a27f6ab41356ecba47ec2bec6d635704c169779 +[c871df]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=871df5007edaacaf2eac58a2871af6044a444ad4 +[ca3c9]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a3c90f7a2323b331ae816d5b0633e68148e25d04 +[cab427]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ab427db17885814069bae891834f20842f0ac3a4 +[cac2a]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ac2a66665e231847cab11b8c8e844ce43207dd2e +[cb514]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b5140a36da7876bc084a2c680c8dbc7438db2051 +[cb596]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=b5964aac51e0c286a50e68225e0dfcf11fb554cb +[cc29f]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c29f74e0df7a02b8303bcdce93a7c0132d62577a +[cd0107]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=d0107f38162279d9cb0e012210731176265f835d +[cd9810]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=d98103575dcdd3a730e0901ab457791a9ac6930c +[ce052]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e052901991ae21e15851bfc89c682bfcb39a4dcf +[ce5ea]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=e5eaac2beb54f0a16ff851125082d9faeb475572 +[cedd51]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=edd51a23343870dbd7cedf6e2765c13cf7a5ccbc +[cee1b]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ee1bcfe01251e8958b53de45d2b0c85e09dd2719 +[cfdbaf]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fdbaf5163331342e90a2c29b87629021f4c15f0c +[cleanup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n760 +[coreclear]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n401 +[coreday]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n358 +[coredst]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n129 +[corefix]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n250 +[coregre]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n240 +[corelimit]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n373 +[ctcount]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n520 +[cttcp]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n41 +[deact]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n185 +[dev5024]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c?h=v7.4-rc3#n5024 +[dev5032]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c?h=v7.4-rc3#n5032 +[dev6290]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c?h=v7.4-rc3#n6290 +[dev6298]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c?h=v7.4-rc3#n6298 +[docleanup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n741 +[dsa]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/dsa/user.c?h=v7.4-rc3#n1729 +[dstchk]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_flow_table.h?h=v7.4-rc3#n314 +[eval]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n50 +[evalcheck]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n98 +[evalconf]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n102 +[evalonce]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n105 +[ext]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n517 +[fillpath]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n45 +[fixtcp]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n178 +[fixup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n201 +[flowtimeout]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_flow_table.h?h=v7.4-rc3#n209 +[forward]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n475 +[frag]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n230 +[free]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n773 +[gccomment]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n506 +[gcpending]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n581 +[gcrequeue]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n626 +[gcstep]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n576 +[gcwork]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n620 +[gettimeout]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n316 +[gre0]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n270 +[grekey]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_gre.c?h=v7.4-rc3#n226 +[hwstats]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1149 +[hwstatwork]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1011 +[indrcmd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1239 +[inet]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_inet.c?h=v7.4-rc3#n71 +[ip6dec]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1145 +[ip6ext]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1050 +[ip6hook]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1233 +[ip6ip6]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n356 +[ip6ttl]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1069 +[iphook]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n904 +[ipip]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n331 +[kconfig]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/Kconfig?h=v7.4-rc3#n721 +[liberal]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n45 +[lookup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n408 +[lwn1]: https://lwn.net/Articles/738214/ +[lwn2]: https://lwn.net/Articles/742164/ +[mlx5]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mellanox/mlx5/core/en/rep/tc.c?h=v7.4-rc3#n261 +[mt76]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/wireless/mediatek/mt76/npu.c?h=v7.4-rc3#n385 +[mtkppe]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mediatek/mtk_ppe_offload.c?h=v7.4-rc3#n674 +[mtkwed]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mediatek/mtk_wed.c?h=v7.4-rc3#n2790 +[mtu]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n289 +[neigherr]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n870 +[netdevsim]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/netdevsim/tc.c?h=v7.4-rc3#n92 +[nftcore]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_core.c?h=v7.4-rc3#n292 +[nlnk]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_netlink.c?h=v7.4-rc3#n579 +[notifier]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n248 +[offcmd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1252 +[offsetup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1279 +[offtuple]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n901 +[pathbr]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n187 +[pathdev]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n198 +[pathhw]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n194 +[pathnud]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n61 +[pathppp]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n155 +[pathrev]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n305 +[pathskip]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n314 +[procfs]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_procfs.c?h=v7.4-rc3#n43 +[procnf]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_standalone.c?h=v7.4-rc3#n339 +[proctag]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_standalone.c?h=v7.4-rc3#n364 +[qxmit]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n822 +[refresh]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n367 +[route]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n272 +[rst]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3 +[rst10]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n10 +[rst105]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n105 +[rst111]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n111 +[rst129]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n129 +[rst15]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n15 +[rst165]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n165 +[rst183]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n183 +[rst20]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n20 +[rst202]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n202 +[rst21]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n21 +[rst214]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n214 +[rst22]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n22 +[rst224]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n224 +[rst37]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n37 +[rst74]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n74 +[rst77]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n77 +[rst78]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n78 +[rst87]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n87 +[rst99]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n99 +[ruleadd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n957 +[setup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1271 +[skip]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n24 +[splice]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1193 +[state]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n25 +[statinc]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1047 +[sysctltcp]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_standalone.c?h=v7.4-rc3#n789 +[sysctludp]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_standalone.c?h=v7.4-rc3#n839 +[tcp5d]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n64 +[tcpest]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_conntrack_l4proto.h?h=v7.4-rc3#n206 +[tcpoff]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n1658 +[tcpprint]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n266 +[tcpto]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n479 +[tear]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n399 +[ttl]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n253 +[uapiass]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/netfilter/nf_conntrack_common.h?h=v7.4-rc3#n51 +[uapiconf]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/netfilter/nf_conntrack_common.h?h=v7.4-rc3#n55 +[uapiseq]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/netfilter/nf_conntrack_common.h?h=v7.4-rc3#n70 +[udpoff]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_udp.c?h=v7.4-rc3#n204 +[unsup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1105 +[validate]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n138 +[workadd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n973 +[xfrm]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n300 +[xfrm4]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/xfrm4_output.c?h=v7.4-rc3#n33 +[xmit]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n776 +[xmit4]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n838 +[xmittype]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n19 From b15e32c3294ed8506435e123eef9ca3357d0207c Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 19:22:34 -0800 Subject: [PATCH 12/22] net: complete IPsec, hardware and rule-change details on flowtable page Cover the encapsulated-packet hooks, cite the one-direction IPsec claim, show the flowtable counter and offload options, fix the direct-mode and device-registration statements, and attribute the GRE timeout history. --- docs/net/flowtable.md | 89 +++++++++++++++++++++++++++++++------------ 1 file changed, 65 insertions(+), 24 deletions(-) diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index 3fa7533e..580c7e77 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -6,7 +6,7 @@ Source links point at Linux v7.4-rc3, the tree this page was checked against. Be ## What it does -The kernel documentation describes the design: once the first packet of a flow has gone through the IP forwarding path, a rule can add the flow to a table ([nf_flowtable.rst:15-19][rst15]). A packet that finds a matching entry is transmitted without the classic forwarding path, and a packet that doesn't follows the classic path ([nf_flowtable.rst:20-26][rst20]). The lookup runs in a hook at ingress on the devices listed in the flowtable: [`nf_flow_offload_ip_hook()`][iphook] for IPv4 and [`nf_flow_offload_ipv6_hook()`][ip6hook] for IPv6, registered through the flowtable types ([nf_flow_table_inet.c:71-94][inet]). +The kernel documentation describes the design: once the first packet of a flow has gone through the IP forwarding path, a rule can add the flow to a table ([nf_flowtable.rst:15-19][rst15]). A packet that finds a matching entry is transmitted without the classic forwarding path, and a packet that doesn't follows the classic path ([nf_flowtable.rst:20-26][rst20]). The lookup runs in a hook at ingress on the devices listed in the flowtable: [`nf_flow_offload_ip_hook()`][iphook] for IPv4 and [`nf_flow_offload_ipv6_hook()`][ip6hook] for IPv6, registered through the flowtable types ([nf_flow_table_inet.c:68-96][inet68]). ``` ingress hook @@ -28,11 +28,11 @@ Which hooks a hit passes, from the source: - The tc ingress hook runs before the netfilter ingress hook where the flowtable sits ([dev.c:6290][dev6290], [dev.c:6298][dev6298]). - A software hit is transmitted with `dev_queue_xmit()` ([`__nf_flow_queue_xmit()`][xmit]), which runs the netfilter egress hook and tc egress ([dev.c:5024][dev5024], [dev.c:5032][dev5032]). -- A hit whose route has an IPsec transform is transmitted with `dst_output()` ([`nf_flow_xmit_xfrm()`][xfrm]), which runs the postrouting hook ([xfrm4_output.c:33][xfrm4]). +- A hit whose route has an IPsec transform is transmitted with `dst_output()` ([`nf_flow_xmit_xfrm()`][xfrm]), which runs the postrouting hook ([xfrm4_output.c:33][xfrm4], [xfrm6_output.c:114][xfrm6]). After the transform, `xfrm_output_resume()` runs the local-out hook and then postrouting again for the encapsulated packet ([xfrm_output.c:593][xfrmres]). The kernel documentation says "you do not see these packets from any of the Netfilter hooks coming after ingress" ([nf_flowtable.rst:22-23][rst22]). The source differs; see [Differences](#differences-from-nf_flowtablerst). -The tc `ct` action uses the same infrastructure ([act_ct.c:55][actct55]). This page covers the nftables side. +The tc `ct` action uses the same infrastructure ([act_ct.c:453][actct453] calls `flow_offload_add()`). This page covers the nftables side. ## Turning it on @@ -53,11 +53,27 @@ table inet x { - **The statement.** `flow add @f` is evaluated by [`nft_flow_offload_eval()`][eval]. It is accepted only in a chain on the forward hook ([`nft_flow_offload_validate()`][validate]). The example rule matches IPv4 TCP. - **What a hit skips.** A hit is consumed with `NF_STOLEN` ([`nf_flow_queue_xmit()`][qxmit]), so the later packets of a flow that hit the table don't reach the prerouting and forward chains, and reach postrouting only for IPsec hits (above). The kernel documentation says the `counter` rule stops being updated ([nf_flowtable.rst:105-109][rst105]). -- **Rule evaluation.** A rule that gives a packet a verdict earlier in the chain means `flow add` never runs for it. `flow add` returns no verdict when it adds the flow. On every other path it sets `NFT_BREAK` ([`nft_flow_offload_eval()`][eval]), which skips the rest of that rule and continues with the next rule ([nf_tables_core.c:292-295][nftcore]). So statements after `flow add` in the same rule run only for the packet that adds the flow. -- **The `counter` flag.** Fast-path packets update conntrack's counters only if the flowtable has the `counter` flag ([nf_flow_table_ip.c:520][ctcount]; documented in [nf_flowtable.rst:165-181][rst165]). The update reaches only conntrack entries that have the accounting extension ([`nf_ct_acct_add()`][acctadd]), which is added when the sysctl `net.netfilter.nf_conntrack_acct` is set ([nf_conntrack_acct.h:36][acct], [nf_conntrack_core.c:1789][acctext], [sysctl][acctsysctl]). -- **Priority.** The kernel documentation says to "make sure the flowtable priority is smaller than the nftables ingress chain hence the flowtable runs before in the pipeline" ([nf_flowtable.rst:99-103][rst99]). A hit returns `NF_STOLEN`, so an ingress chain that runs after the flowtable doesn't see it. +- **Rule evaluation.** An earlier rule that accepts or drops the packet ends evaluation, so `flow add` never runs for it ([nf_tables_core.c:306][nftverdict]). `flow add` returns no verdict when it adds the flow. On every other path it sets `NFT_BREAK` ([`nft_flow_offload_eval()`][eval]), which skips the rest of that rule and continues with the next rule ([nf_tables_core.c:292-295][nftcore]). So statements after `flow add` in the same rule run only for the packet that adds the flow. +- **The `counter` flag.** Fast-path packets update conntrack's counters only if the flowtable has the `counter` flag ([nf_flow_table_ip.c:520][ctcount]; documented in [nf_flowtable.rst:165-181][rst165]). The update reaches only conntrack entries that have the accounting extension ([`nf_ct_acct_add()`][acctfn]; the extension is added when the entry is created), which is added when the sysctl `net.netfilter.nf_conntrack_acct` is set ([nf_conntrack_acct.h:36][acct], [nf_conntrack_core.c:1789][acctext], [sysctl][acctsysctl]). +- **Priority.** The kernel documentation says to "make sure the flowtable priority is smaller than the nftables ingress chain hence the flowtable runs before in the pipeline" ([nf_flowtable.rst:99-103][rst99]). A hit returns `NF_STOLEN`, which ends hook traversal ([nf_hook_slow()][nfstolen]), so an ingress chain that runs after the flowtable doesn't see it. - **Device list.** Path resolution follows VLAN, PPPoE and bridge devices down to the real device ([`nft_dev_fill_forward_path()`][fillpath]) and requires that device to be in the flowtable's list ([nf_flow_table_path.c:198][pathdev]). The check runs for both directions and is skipped for IPsec routes ([nf_flow_table_path.c:314-320][pathskip]). The kernel documentation says the real device is sufficient and the VLAN and PPPoE devices need not be listed ([nf_flowtable.rst:129-130][rst129]). Since v7.2 ([`871df5007eda`][c871df]), a failed path discovery or a device that isn't in the flowtable makes `flow add` give up. The commit message says "If forward path discovery fails for any reason or netdevice is not registered for this flowtable, then bail out to classic forwarding path rather than providing incomplete forwarding path." +Two flowtable options appear in the kernel documentation: `counter` ([nf_flowtable.rst:165-181][rst165]) and `flags offload` ([nf_flowtable.rst:183-196][rst183]): + +``` + flowtable f { + hook ingress priority 0; devices = { eth0, eth1 }; + counter + } + + flowtable f { + hook ingress priority 0; devices = { eth0, eth1 }; + flags offload; + } +``` + +The kernel options are `CONFIG_NF_FLOW_TABLE` ([Kconfig:711][kcft]), `CONFIG_NF_FLOW_TABLE_INET` ([Kconfig:703][kcinet]) and `CONFIG_NFT_FLOW_OFFLOAD` ([Kconfig:504][kcnft]). + ## How a flow gets in [`nft_flow_offload_eval()`][eval] runs on a packet in the forward path and checks: @@ -67,9 +83,15 @@ table inet x { - **Confirmed.** The conntrack entry must be confirmed ([nft_flow_offload.c:102][evalconf]), which the header describes as "originating packet has left box" ([nf_conntrack_common.h:55][uapiconf]). - **Only once.** `test_and_set_bit(IPS_OFFLOAD_BIT, ...)` makes the first caller win ([nft_flow_offload.c:105][evalonce]). -If all of that holds, [`nft_flow_route()`][route] resolves the path: it reuses the route of the packet being processed for one direction and looks up the reverse route ([nf_flow_table_path.c:305][pathrev]). Only the packet's own direction can carry an IPsec transform; [`b5964aac51e0`][cb596] says "the existing basic xfrm offload (which only works in one direction)". For routes without an IPsec transform the resolution also requires the egress device to be in the flowtable and, on an Ethernet device, a neighbour entry in a valid state (`NUD_VALID`, [nf_flow_table_path.c:61-77][pathnud]). [`flow_offload_alloc()`][alloc] then allocates the entry with a reference to the conntrack entry and the SNAT and DNAT flags, and [`flow_offload_add()`][add] inserts its two tuples, one per direction, into one resizable hash table. The comment in `flow_offload_add()` says garbage collection walks only the original-direction entries, so that one is inserted last. +If all of that holds: + +1. [`nft_flow_route()`][route] resolves the path. It reuses the route of the packet being processed for one direction and looks up the reverse route ([nf_flow_table_path.c:305][pathrev]). +2. Only the packet's own direction can carry an IPsec transform. The reverse lookup does no IPsec lookup in IPv4 unless the flow key's protocol is set ([route.c:2984][route2984]), which `nft_flow_route()` doesn't do, and none in IPv6 ([ipv6/netfilter.c:102][v6route]). [`b5964aac51e0`][cb596] says "the existing basic xfrm offload (which only works in one direction)". +3. For routes without an IPsec transform the resolution also requires the egress device to be in the flowtable and, on an Ethernet device, a neighbour entry in a valid state (`NUD_VALID`, [nf_flow_table_path.c:61-77][pathnud]). +4. [`flow_offload_alloc()`][alloc] allocates the entry with a reference to the conntrack entry and the SNAT and DNAT flags. +5. [`flow_offload_add()`][add] inserts the entry's two tuples, one per direction, into one resizable hash table. Its comment says garbage collection walks only the original-direction entries, so that one is inserted last. -For TCP, [`flow_offload_ct_tcp()`][cttcp] sets the "be liberal" flag on the conntrack entry; its comment says conntrack will not see all packets any more, so it can't validate the TCP window. In the flowtable files the flag is set only at [nft_flow_offload.c:45-46][liberal] and cleared nowhere; the tc `ct` action sets it too ([act_ct.c:441-442][actct441]). +For TCP, [`flow_offload_ct_tcp()`][cttcp] sets the "be liberal" flag on the conntrack entry; its comment says conntrack will not see all packets any more, so it can "disable tcp window validation". In the flowtable files the flag is set only at [nft_flow_offload.c:45-46][liberal] and cleared nowhere; the tc `ct` action sets it too ([act_ct.c:441-442][actct441]). ## Transmit modes @@ -94,17 +116,17 @@ The hook then transmits as described in [Transmit modes](#transmit-modes): [`nf_ [`nf_flow_offload_ipv6_hook()`][ip6hook] does the same, with the hop limit in place of the TTL ([nf_flow_table_ip.c:1069][ip6ttl] checks it, [nf_flow_table_ip.c:1145][ip6dec] decrements it). The next header has to be TCP, UDP or GRE, so a packet with any extension header is a miss ([nf_flow_table_ip.c:1050-1067][ip6ext]). It strips IPv6-in-IPv6 tunnel headers since v7.0 ([`d98103575dcd`][cd9810]). Mixed-family tunnels are not supported: the IPv4 hook accepts only IPIP ([nf_flow_table_ip.c:331][ipip]), the IPv6 hook only IPv6-in-IPv6 ([nf_flow_table_ip.c:356][ip6ip6]), and [`d0107f381622`][cd0107] says IPv4 over IPv6 and SIT tunnel offloading "is not enabled yet". -Packets that the hook doesn't take go to the classic path: fragments and packets with IPv4 options ([nf_flow_table_ip.c:230][frag]), a TTL or hop limit of 1 or less ([nf_flow_table_ip.c:253][ttl], [nf_flow_table_ip.c:1069][ip6ttl]), and GRE other than version 0 ([nf_flow_table_ip.c:270][gre0]). +Packets that the hook doesn't take go to the classic path: fragments and packets with IPv4 options ([nf_flow_table_ip.c:230][frag]), a TTL or hop limit of 1 or less ([nf_flow_table_ip.c:253][ttl], [nf_flow_table_ip.c:1069][ip6ttl]), and GRE other than version 0 ([nf_flow_table_ip.c:270][gre0], [nf_flow_table_ip.c:1086][gre06]). ## How a flow leaves -[`nf_flow_offload_work_gc()`][gcwork] re-queues itself one second after each run ([nf_flow_table_core.c:626][gcrequeue]) and removes entries that have been torn down. Teardown is triggered by: +The garbage collector, [`nf_flow_offload_work_gc()`][gcwork], re-queues itself one second after each run ([nf_flow_table_core.c:626][gcrequeue]) and removes entries that have been torn down. Teardown is triggered by: -- **A timeout.** Each entry has a deadline. A software hit pushes it out, at most once a second ([nf_flow_table_core.c:373][corelimit]); this also applies to flows that are closing. The default is 30 seconds for TCP ([nf_conntrack_proto_tcp.c:1658][tcpoff]) and UDP ([nf_conntrack_proto_udp.c:204][udpoff]), set by the sysctls `net.netfilter.nf_flowtable_tcp_timeout` and `net.netfilter.nf_flowtable_udp_timeout` ([nf_conntrack_standalone.c:789][sysctltcp], [nf_conntrack_standalone.c:839][sysctludp]), and 30 seconds for GRE, which has no sysctl ([`flow_offload_get_timeout()`][gettimeout], [nf_flow_table.h:209][flowtimeout]). For hardware flows, the collector asks the driver for statistics once less than 90% of the timeout remains and extends the deadline from the last-used time ([`nf_flow_offload_stats()`][hwstats], [`flow_offload_work_stats()`][hwstatwork]). +- **A timeout.** Each entry has a deadline. A software hit pushes it out, at most once a second ([nf_flow_table_core.c:373][corelimit]); this also applies to flows that are closing (the refresh writes the deadline before it checks for `NF_FLOW_CLOSING`). The default is 30 seconds for TCP ([nf_conntrack_proto_tcp.c:1658][tcpoff]) and UDP ([nf_conntrack_proto_udp.c:204][udpoff]), set by the sysctls `net.netfilter.nf_flowtable_tcp_timeout` and `net.netfilter.nf_flowtable_udp_timeout` ([nf_conntrack_standalone.c:789][sysctltcp], [nf_conntrack_standalone.c:839][sysctludp]), and 30 seconds for GRE, which has no sysctl ([`flow_offload_get_timeout()`][gettimeout], [nf_flow_table.h:209][flowtimeout]). For hardware flows, the collector asks the driver for statistics once less than 90% of the timeout remains and extends the deadline from the last-used time ([`nf_flow_offload_stats()`][hwstats], [`flow_offload_work_stats()`][hwstatwork]). - **A dying conntrack entry**, checked by `nf_ct_is_dying()` ([`nf_flow_offload_gc_step()`][gcstep]); lookups also miss ([`flow_offload_lookup()`][lookup]). - **A stale route**, for flows that keep one. The collector checks this since v7.3 ([`edd51a233438`][cedd51]); the hook checks it too ([`nf_flow_offload_forward()`][forward]). -- **A missing output device, or a neighbour lookup error.** The hook tears the flow down and drops that packet ([nf_flow_table_ip.c:870-880][neigherr]). A missing neighbour entry is created by the lookup, so only a failure to create it counts. -- **A device going down.** The `NETDEV_DOWN` notifier ([nft_flow_offload.c:248][notifier]) calls [`nf_flow_table_cleanup()`][cleanup], which tears down the flows that enter through that device ([`nf_flow_table_do_cleanup()`][docleanup]). +- **A missing output device, or a neighbour lookup error.** The hook tears the flow down and drops that packet ([nf_flow_table_ip.c:870-880][neigherr]). A missing neighbour entry is created by the lookup ([`ip_neigh_gw4()`][neigh4]), so only a failure to create it counts. +- **A device going down.** The `NETDEV_DOWN` notifier ([nft_flow_offload.c:248][notifier]) calls [`nf_flow_table_cleanup()`][cleanup], which tears down the flows that use that device in either direction ([`nf_flow_table_do_cleanup()`][docleanup]). - **Deleting the flowtable** ([`nf_flow_table_free()`][free]). **TCP FIN and RST** don't end the flow at once (since v6.14, [`fdbaf5163331`][cfdbaf]). [`nf_flow_state_check()`][state] marks the flow `NF_FLOW_CLOSING` and the packet still takes the fast path; the collector removes a hardware entry early ([`nf_flow_offload_gc_step()`][gcstep]). Software hits keep pushing the deadline, so the flow leaves when the timeout expires after the last packet, and [`flow_offload_fixup_ct()`][fixup] then gives conntrack the close state and timeout. A later SYN on a closing flow tears it down at once, sets the conntrack state to close and sends that packet to the classic path ([`nf_flow_state_check()`][state], [`flow_offload_fixup_tcp()`][fixtcp]). @@ -131,26 +153,26 @@ The kernel documentation says a flowtable can offload flows to a device that sup The driver interface is the tc flow-block interface: - **Binding.** Registering a flowtable's ingress hook on a device first calls the flowtable type's `setup` callback, [`nf_flow_table_offload_setup()`][setup] ([nf_tables_api.c:9068][apisetup]). Without `flags offload` it only sets up the XDP path ([nf_flow_table_offload.c:1279-1280][offsetup]); XDP programs can then look the table up with [`bpf_xdp_flow_lookup`][bpf] ([`391bb6594fd3`][c391bb], v6.11). With `flags offload`, if the device has `ndo_setup_tc` the flowtable calls it with `TC_SETUP_FT` ([`nf_flow_table_offload_cmd()`][offcmd]); otherwise it uses the indirect-block path `flow_indr_dev_setup_offload()` ([`nf_flow_table_indr_offload_cmd()`][indrcmd]). The driver's callback is added to the flowtable's block ([nf_flow_table_offload.c:1193][splice]). -- **Per flow.** [`nf_flow_offload_tuple()`][offtuple] calls every callback with `TC_SETUP_CLSFLOWER` and a `flow_cls_offload` request, the request type tc flower uses. The command is `FLOW_CLS_REPLACE` to add a flow (with a flow rule: match keys plus actions such as header rewrites), `FLOW_CLS_DESTROY` or `FLOW_CLS_STATS`. -- **In-tree drivers that handle `TC_SETUP_FT`** as of v7.4-rc3: the mlx5 representor code ([rep/tc.c:261][mlx5]), the MediaTek Ethernet and WED drivers ([mtk_ppe_offload.c:674][mtkppe], [mtk_wed.c:2790][mtkwed]), the mt76 Wi-Fi driver ([npu.c:385][mt76]) and the Airoha Ethernet driver ([airoha_eth.c:3674][airoha]). DSA user ports pass the command to the conduit device ([dsa/user.c:1729][dsa]). `netdevsim` returns 0 and registers no callback ([netdevsim/tc.c:92][netdevsim]). +- **Per flow.** [`nf_flow_offload_tuple()`][offtuple] calls every callback with `TC_SETUP_CLSFLOWER` and a `flow_cls_offload` request, the request type tc flower uses. The command is `FLOW_CLS_REPLACE` to add a flow (with a flow rule built by [`nf_flow_rule_route_ipv4()`][rulefn] and [`nf_flow_rule_match()`][rulematch]: match keys plus actions), `FLOW_CLS_DESTROY` or `FLOW_CLS_STATS`. +- **In-tree drivers that handle `TC_SETUP_FT`** as of v7.4-rc3: the mlx5 representor code ([rep/tc.c:261][mlx5], [rep/tc.c:675][mlx5ind]), the MediaTek Ethernet and WED drivers ([mtk_ppe_offload.c:674][mtkppe], [mtk_wed.c:2790][mtkwed]), the mt76 Wi-Fi driver ([npu.c:385][mt76]) and the Airoha Ethernet driver ([airoha_eth.c:3674][airoha]). DSA user ports pass the command to the conduit device ([dsa/user.c:1729][dsa]). `netdevsim` returns 0 and registers no callback ([netdevsim/tc.c:92][netdevsim]). Behaviour with `flags offload`, from the source: -- A device that can't offload makes the flowtable fail to load for devices present when it is created: the `setup` error is returned before the hook is registered ([nf_tables_api.c:9068][apisetup]). A change of the `offload` flag on an existing flowtable is rejected with `-EOPNOTSUPP` ([nf_tables_api.c:9200-9203][apitoggle]). -- Every Ethernet-device path except the IPsec direction uses direct mode ([nf_flow_table_path.c:194-196][pathhw]), so a flow without a tunnel caches no route. +- A device that can't offload makes creating the flowtable, or adding the device to it, fail: the `setup` error is returned before the hook is registered ([nf_tables_api.c:9068][apisetup], [nf_tables_api.c:9104-9106][apiregall]). A device that registers later is handled by the netdev notifier, which returns `NOTIFY_BAD` when registering the hook fails ([nf_tables_api.c:9889][apinotif]). A change of the `offload` flag on an existing flowtable is rejected with `-EOPNOTSUPP` ([nf_tables_api.c:9200-9203][apitoggle]). +- A direction that goes through a valid Ethernet device uses direct mode ([nf_flow_table_path.c:194-196][pathhw]), so a direction without a tunnel caches no route. The IPsec direction still caches its route ([nf_flow_table_core.c:143-147][corexfrm]). - A hardware add that fails isn't reported. The flow stays in software with `NF_FLOW_HW` set, `[HW_OFFLOAD]` is set if the driver accepted at least one direction ([`flow_offload_rule_add()`][ruleadd], [`flow_offload_work_add()`][workadd]), and the add is queued again on later software hits, at most once a second and not for closing flows ([nf_flow_table_core.c:373-383][corelimit]). - Flows through an IPIP or IPv6-in-IPv6 tunnel stay in software ([`nf_flow_offload_unsupported()`][unsup]; [`6c5dcab95f4c`][c6c5d], v7.2). - The collector skips an entry whose hardware work is still queued ([nf_flow_table_core.c:581][gcpending]). ## Inspecting -- **Conntrack listing.** `conntrack -L` shows `[OFFLOAD]` for software flows and `[HW_OFFLOAD]` for hardware flows ([nf_flowtable.rst:111-113][rst111], [nf_flowtable.rst:202-206][rst202]). `/proc/net/nf_conntrack` prints the same tags ([nf_conntrack_standalone.c:364-366][proctag]). +- **Conntrack listing.** `conntrack -L` shows `[OFFLOAD]` for software flows and `[HW_OFFLOAD]` for hardware flows ([nf_flowtable.rst:111-113][rst111], [nf_flowtable.rst:202-206][rst202]). `/proc/net/nf_conntrack` (with `CONFIG_NF_CONNTRACK_PROCFS`, [nf_conntrack_standalone.c:34][kcprocfs]) prints the same tags ([nf_conntrack_standalone.c:364-366][proctag]). - **What offloaded entries omit.** ctnetlink skips the timeout and protocol info ([nf_conntrack_netlink.c:579][nlnk]), and `/proc/net/nf_conntrack` skips the timeout and the TCP state ([nf_conntrack_standalone.c:339][procnf], [`tcp_print_conntrack()`][tcpprint]). -- **Statistics.** With `CONFIG_NF_FLOW_TABLE_PROCFS` ([Kconfig:721][kconfig]), [`/proc/net/stat/nf_flowtable`][procfs] shows per-CPU counters of pending hardware add, delete and stats work. They are incremented when work is queued and decremented when it completes ([nf_flow_table_offload.c:1047-1077][statinc]). There are no hit or miss counters. +- **Statistics.** With `CONFIG_NF_FLOW_TABLE_PROCFS` ([Kconfig:721][kconfig]), [`/proc/net/stat/nf_flowtable`][procfs] shows per-CPU counters of pending hardware add, delete and stats work; the file is created at [nf_flow_table_procfs.c:71][procfscreate] and its show function prints only those three columns. They are incremented when work is queued and decremented when it completes ([nf_flow_table_offload.c:1047-1077][statinc]). There are no hit or miss counters. ## Limits -- **Rule changes.** Removing a rule runs the expression's `deactivate` and `destroy` callbacks, which release its references to the flowtable and to conntrack and don't touch flow entries ([nft_flow_offload.c:185-206][deact]). A hit never reaches the forward chain, and a software hit pushes the deadline out ([`flow_offload_refresh()`][refresh]). Only the stored NAT rewrite is applied to a hit ([`nf_flow_offload_forward()`][forward]). The ways a flow leaves are listed in [How a flow leaves](#how-a-flow-leaves). +- **Rule changes.** Removing a rule runs the expression's `deactivate` and `destroy` callbacks, which release the expression's use of the flowtable and its conntrack hook registration for the netns, and don't touch flow entries ([nft_flow_offload.c:185-206][deact]). A hit never reaches the forward chain, and a software hit pushes the deadline out ([`flow_offload_refresh()`][refresh]). Only the stored NAT rewrite is applied to a hit ([`nf_flow_offload_forward()`][forward]). So a flow already in the table keeps being forwarded after the `flow add` rule is removed or a drop rule is added, as long as packets keep arriving. The ways a flow leaves are listed in [How a flow leaves](#how-a-flow-leaves). - **Cache.** The kernel documentation says the table behaves like a cache and that entries might get stale if the destination MAC address or the egress device changes, which "might be a problem" with software mode combined with bridging and IP forwarding, and with hardware offload ([nf_flowtable.rst:214-222][rst214]). - **IPsec.** A packet that carries a security path never triggers an add ([`nft_flow_offload_skip()`][skip]). In the direction whose route has an IPsec transform the device-list and neighbour checks are skipped ([nf_flow_table_path.c:314-320][pathskip]); the other direction gets them like any other flow. - **ip6gre.** The commit that extended the GRE timeout, [`46ecda278f6e`][c46ecd], says `ip6gre` tunnels with the default encapsulation limit carry a Destination Options header before GRE, which the flowtable doesn't parse, so every packet of the flow takes the classic path. @@ -166,7 +188,7 @@ The software flow table was committed on 8 January 2018 ([`ac2a66665e23`][cac2a] Hardware offload reached the tree in v5.5 through the tc flow-block interface rather than through the dedicated `ndo` hooks proposed in the RFC. Pablo Neira Ayuso added it in [`c29f74e0df7a`][cc29f], which called `ndo_setup_tc` with `TC_SETUP_BLOCK` and the callbacks with `TC_SETUP_FT`. The arrangement described above came from two fixes by wenxu in the same release, [`ee1bcfe01251`][cee1b] and [`e052901991ae`][ce052]. wenxu added the indirect path in [`b5140a36da78`][cb514] (v5.7). -**GRE timeout history.** The conntrack timeout extension was reworked in [`03428ca5cee9`][c0342] (v6.14), but it never took effect until [`53b3e60edb67`][c53b3] (v7.2): it compared a relative value with the absolute `ct->timeout`, so the update never succeeded. It also did not handle GRE. Once a GRE entry's timeout fell below five minutes it hit a `WARN_ON_ONCE()`, and from v7.2, when the update started to succeed, the default branch set the timeout to one jiffy; [`46ecda278f6e`][c46ecd] (v7.4-rc3) says "the default branch sets the new timeout to 1 jiffy, so the conntrack entry expires right away" and extends GRE like UDP. The two fixes, `53b3e60edb67` and `46ecda278f6e`, carry `Cc: stable`, so the mainline release numbers above are not the only ones with the fix. The packets that bring a GRE entry's timeout down are those that still go through conntrack; the commit message gives packets with a TTL of 1 or less as an example. +**GRE timeout history.** The conntrack timeout extension was reworked in [`03428ca5cee9`][c0342] (v6.14), but it never took effect until [`53b3e60edb67`][c53b3] (v7.2): it compared a relative value with the absolute `ct->timeout`, so the update never succeeded. It also did not handle GRE. Once a GRE entry's timeout fell below five minutes it hit a `WARN_ON_ONCE()`. The default branch, `new_timeout = true` (one jiffy) in `03428ca5cee9`, took effect once `53b3e60edb67` made the update succeed. [`46ecda278f6e`][c46ecd] (v7.4-rc3) says "the default branch sets the new timeout to 1 jiffy, so the conntrack entry expires right away" and extends GRE like UDP. Both fixes carry `Cc: stable@vger.kernel.org`. The packets that bring a GRE entry's timeout down are those that still go through conntrack; the commit message gives packets with a TTL of 1 or less as an example. ## Differences from `nf_flowtable.rst` @@ -193,14 +215,16 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, - [nftables vs iptables](nftables-iptables.md) — how to write the ruleset [acct]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_conntrack_acct.h?h=v7.4-rc3#n36 -[acctadd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_core.c?h=v7.4-rc3#n929 [acctext]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_core.c?h=v7.4-rc3#n1789 +[acctfn]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_core.c?h=v7.4-rc3#n924 [acctsysctl]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_standalone.c?h=v7.4-rc3#n691 [actct441]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/sched/act_ct.c?h=v7.4-rc3#n441 -[actct55]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/sched/act_ct.c?h=v7.4-rc3#n55 +[actct453]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/sched/act_ct.c?h=v7.4-rc3#n453 [add]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n335 [airoha]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/airoha/airoha_eth.c?h=v7.4-rc3#n3674 [alloc]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n53 +[apinotif]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9889 +[apiregall]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9104 [apisetup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9068 [apitoggle]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9200 [bpf]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_bpf.c?h=v7.4-rc3#n59 @@ -236,6 +260,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [corefix]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n250 [coregre]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n240 [corelimit]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n373 +[corexfrm]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n143 [ctcount]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n520 [cttcp]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n41 [deact]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n185 @@ -265,11 +290,12 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [gcwork]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n620 [gettimeout]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n316 [gre0]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n270 +[gre06]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1086 [grekey]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_gre.c?h=v7.4-rc3#n226 [hwstats]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1149 [hwstatwork]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1011 [indrcmd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1239 -[inet]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_inet.c?h=v7.4-rc3#n71 +[inet68]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_inet.c?h=v7.4-rc3#n68 [ip6dec]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1145 [ip6ext]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1050 [ip6hook]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1233 @@ -277,19 +303,27 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [ip6ttl]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1069 [iphook]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n904 [ipip]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n331 +[kcft]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/Kconfig?h=v7.4-rc3#n711 +[kcinet]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/Kconfig?h=v7.4-rc3#n703 +[kcnft]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/Kconfig?h=v7.4-rc3#n504 [kconfig]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/Kconfig?h=v7.4-rc3#n721 +[kcprocfs]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_standalone.c?h=v7.4-rc3#n34 [liberal]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n45 [lookup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n408 [lwn1]: https://lwn.net/Articles/738214/ [lwn2]: https://lwn.net/Articles/742164/ [mlx5]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mellanox/mlx5/core/en/rep/tc.c?h=v7.4-rc3#n261 +[mlx5ind]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mellanox/mlx5/core/en/rep/tc.c?h=v7.4-rc3#n675 [mt76]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/wireless/mediatek/mt76/npu.c?h=v7.4-rc3#n385 [mtkppe]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mediatek/mtk_ppe_offload.c?h=v7.4-rc3#n674 [mtkwed]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mediatek/mtk_wed.c?h=v7.4-rc3#n2790 [mtu]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n289 +[neigh4]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/route.h?h=v7.4-rc3#n408 [neigherr]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n870 [netdevsim]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/netdevsim/tc.c?h=v7.4-rc3#n92 +[nfstolen]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/core.c?h=v7.4-rc3#n649 [nftcore]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_core.c?h=v7.4-rc3#n292 +[nftverdict]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_core.c?h=v7.4-rc3#n306 [nlnk]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_netlink.c?h=v7.4-rc3#n579 [notifier]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n248 [offcmd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1252 @@ -303,11 +337,13 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [pathrev]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n305 [pathskip]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n314 [procfs]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_procfs.c?h=v7.4-rc3#n43 +[procfscreate]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_procfs.c?h=v7.4-rc3#n71 [procnf]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_standalone.c?h=v7.4-rc3#n339 [proctag]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_standalone.c?h=v7.4-rc3#n364 [qxmit]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n822 [refresh]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n367 [route]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n272 +[route2984]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/route.c?h=v7.4-rc3#n2984 [rst]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3 [rst10]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n10 [rst105]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n105 @@ -329,6 +365,8 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [rst87]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n87 [rst99]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n99 [ruleadd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n957 +[rulefn]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n743 +[rulematch]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n94 [setup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1271 [skip]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n24 [splice]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1193 @@ -348,10 +386,13 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [uapiseq]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/netfilter/nf_conntrack_common.h?h=v7.4-rc3#n70 [udpoff]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_udp.c?h=v7.4-rc3#n204 [unsup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1105 +[v6route]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv6/netfilter.c?h=v7.4-rc3#n102 [validate]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n138 [workadd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n973 [xfrm]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n300 [xfrm4]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/xfrm4_output.c?h=v7.4-rc3#n33 +[xfrm6]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv6/xfrm6_output.c?h=v7.4-rc3#n114 +[xfrmres]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/xfrm/xfrm_output.c?h=v7.4-rc3#n593 [xmit]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n776 [xmit4]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n838 [xmittype]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n19 From 7be11d4d500cd4551e142b2986bdabe83762ecc6 Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 19:34:12 -0800 Subject: [PATCH 13/22] net: add flowtable lifecycle, scope device and conntrack statements Add a lifecycle diagram, state which drop rules and devices the statements cover, record the notifier and hook-allocation behaviour, and split the dense hand-back and GRE history paragraphs. --- docs/net/README.md | 2 +- docs/net/flowtable.md | 94 ++++++++++++++++++++++++++++++++----------- 2 files changed, 71 insertions(+), 25 deletions(-) diff --git a/docs/net/README.md b/docs/net/README.md index 1e03fd46..01cbd71b 100644 --- a/docs/net/README.md +++ b/docs/net/README.md @@ -64,7 +64,7 @@ Every packet is carried by an [sk_buff](sk-buff.md) — start there if you read | [Netfilter](netfilter.md) | Hooks, tables, chains | | [nftables vs iptables](nftables-iptables.md) | Why the replacement happened | | [Connection Tracking](conntrack.md) | conntrack entries, NAT, helpers | -| [Flowtable](flowtable.md) | The fast path that lets established flows skip most of the hook path | +| [Flowtable](flowtable.md) | The fast path that lets forwarded flows already in a flow table skip most of the hook path | | [Traffic Control (tc/qdisc)](tc-qdisc.md) | Queuing disciplines, shaping, fq_codel | ### High performance & offload diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index 580c7e77..021acaed 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -1,8 +1,8 @@ -# Flowtable: The Fast Path for Established Flows +# Flowtable: The Fast Path for Forwarded Flows -> How the kernel lets established forwarded connections skip most of the netfilter hook path +> How the kernel lets forwarded connections that are already in a flow table skip most of the netfilter hook path -Source links point at Linux v7.4-rc3, the tree this page was checked against. Behaviour that changed recently is marked with the release that introduced it and links to the commit. "The kernel documentation" is [`nf_flowtable.rst`][rst]. +Source links point at Linux v7.4-rc3, the tree this page was checked against. Release numbers are mainline releases. Behaviour that changed recently is marked with the release that introduced it and links to the commit. "The kernel documentation" is [`nf_flowtable.rst`][rst]. ## What it does @@ -28,7 +28,7 @@ Which hooks a hit passes, from the source: - The tc ingress hook runs before the netfilter ingress hook where the flowtable sits ([dev.c:6290][dev6290], [dev.c:6298][dev6298]). - A software hit is transmitted with `dev_queue_xmit()` ([`__nf_flow_queue_xmit()`][xmit]), which runs the netfilter egress hook and tc egress ([dev.c:5024][dev5024], [dev.c:5032][dev5032]). -- A hit whose route has an IPsec transform is transmitted with `dst_output()` ([`nf_flow_xmit_xfrm()`][xfrm]), which runs the postrouting hook ([xfrm4_output.c:33][xfrm4], [xfrm6_output.c:114][xfrm6]). After the transform, `xfrm_output_resume()` runs the local-out hook and then postrouting again for the encapsulated packet ([xfrm_output.c:593][xfrmres]). +- A hit whose route has an IPsec transform is transmitted with `dst_output()` ([`nf_flow_xmit_xfrm()`][xfrm]), which runs the postrouting hook ([xfrm4_output.c:33][xfrm4], [xfrm6_output.c:114][xfrm6]). After the transform, `xfrm_output_resume()` runs the local-out hook ([xfrm_output.c:600][xfrmlo]) and postrouting again for the encapsulated packet ([xfrm_output.c:608][xfrmpost]). IPsec packet offload in tunnel mode goes out through `xfrm_dev_direct_output()` instead ([xfrm_output.c:786][xfrmdirect]). The kernel documentation says "you do not see these packets from any of the Netfilter hooks coming after ingress" ([nf_flowtable.rst:22-23][rst22]). The source differs; see [Differences](#differences-from-nf_flowtablerst). @@ -53,40 +53,47 @@ table inet x { - **The statement.** `flow add @f` is evaluated by [`nft_flow_offload_eval()`][eval]. It is accepted only in a chain on the forward hook ([`nft_flow_offload_validate()`][validate]). The example rule matches IPv4 TCP. - **What a hit skips.** A hit is consumed with `NF_STOLEN` ([`nf_flow_queue_xmit()`][qxmit]), so the later packets of a flow that hit the table don't reach the prerouting and forward chains, and reach postrouting only for IPsec hits (above). The kernel documentation says the `counter` rule stops being updated ([nf_flowtable.rst:105-109][rst105]). -- **Rule evaluation.** An earlier rule that accepts or drops the packet ends evaluation, so `flow add` never runs for it ([nf_tables_core.c:306][nftverdict]). `flow add` returns no verdict when it adds the flow. On every other path it sets `NFT_BREAK` ([`nft_flow_offload_eval()`][eval]), which skips the rest of that rule and continues with the next rule ([nf_tables_core.c:292-295][nftcore]). So statements after `flow add` in the same rule run only for the packet that adds the flow. -- **The `counter` flag.** Fast-path packets update conntrack's counters only if the flowtable has the `counter` flag ([nf_flow_table_ip.c:520][ctcount]; documented in [nf_flowtable.rst:165-181][rst165]). The update reaches only conntrack entries that have the accounting extension ([`nf_ct_acct_add()`][acctfn]; the extension is added when the entry is created), which is added when the sysctl `net.netfilter.nf_conntrack_acct` is set ([nf_conntrack_acct.h:36][acct], [nf_conntrack_core.c:1789][acctext], [sysctl][acctsysctl]). -- **Priority.** The kernel documentation says to "make sure the flowtable priority is smaller than the nftables ingress chain hence the flowtable runs before in the pipeline" ([nf_flowtable.rst:99-103][rst99]). A hit returns `NF_STOLEN`, which ends hook traversal ([nf_hook_slow()][nfstolen]), so an ingress chain that runs after the flowtable doesn't see it. -- **Device list.** Path resolution follows VLAN, PPPoE and bridge devices down to the real device ([`nft_dev_fill_forward_path()`][fillpath]) and requires that device to be in the flowtable's list ([nf_flow_table_path.c:198][pathdev]). The check runs for both directions and is skipped for IPsec routes ([nf_flow_table_path.c:314-320][pathskip]). The kernel documentation says the real device is sufficient and the VLAN and PPPoE devices need not be listed ([nf_flowtable.rst:129-130][rst129]). Since v7.2 ([`871df5007eda`][c871df]), a failed path discovery or a device that isn't in the flowtable makes `flow add` give up. The commit message says "If forward path discovery fails for any reason or netdevice is not registered for this flowtable, then bail out to classic forwarding path rather than providing incomplete forwarding path." +- **Rule evaluation.** An earlier rule that accepts or drops the packet ends evaluation of that chain ([nf_tables_core.c:306][nftverdict]), so `flow add` never runs for it in that chain. `flow add` returns no verdict when it adds the flow. On every other path it sets `NFT_BREAK` ([`nft_flow_offload_eval()`][eval]), which skips the rest of that rule and continues with the next rule ([nf_tables_core.c:292-295][nftcore]). So statements after `flow add` in the same rule run only for the packet that adds the flow. +- **The `counter` flag.** Software hits update conntrack's counters only if the flowtable has the `counter` flag ([nf_flow_table_ip.c:520][ctcount]; documented in [nf_flowtable.rst:165-181][rst165]). The update reaches only conntrack entries that have the accounting extension ([`nf_ct_acct_add()`][acctfn]), which is added when the entry is created and only if the sysctl `net.netfilter.nf_conntrack_acct` is set ([nf_conntrack_acct.h:36][acct], [nf_conntrack_core.c:1789][acctext], [sysctl][acctsysctl]). +- **Priority.** The kernel documentation says to "make sure the flowtable priority is smaller than the nftables ingress chain hence the flowtable runs before in the pipeline" ([nf_flowtable.rst:99-103][rst99]). A hit returns `NF_STOLEN`, which ends hook traversal ([`nf_hook_slow()`][nfstolen]), so an ingress chain that runs after the flowtable doesn't see it. +- **Device list.** Path resolution follows VLAN, PPPoE and bridge devices down to the real device ([`nft_dev_fill_forward_path()`][fillpath]) and requires that device to be in the flowtable's list ([nf_flow_table_path.c:198][pathdev]). The check runs for both directions and is skipped for IPsec routes ([nf_flow_table_path.c:314-320][pathskip]). The kernel documentation says the real device is sufficient and the VLAN and PPPoE devices need not be listed ([nf_flowtable.rst:129-130][rst129]). +- **Since v7.2** ([`871df5007eda`][c871df]), a failed path discovery or a device that isn't in the flowtable makes `flow add` give up. The commit message says "If forward path discovery fails for any reason or netdevice is not registered for this flowtable, then bail out to classic forwarding path rather than providing incomplete forwarding path." A device name in the flowtable that matches no existing device is accepted and creates no hook ([nf_tables_api.c:2456][hookalloc]), so no device of that name is in the flowtable. Two flowtable options appear in the kernel documentation: `counter` ([nf_flowtable.rst:165-181][rst165]) and `flags offload` ([nf_flowtable.rst:183-196][rst183]): ``` +table inet x { flowtable f { hook ingress priority 0; devices = { eth0, eth1 }; counter } +} +``` +``` +table inet x { flowtable f { hook ingress priority 0; devices = { eth0, eth1 }; flags offload; } +} ``` -The kernel options are `CONFIG_NF_FLOW_TABLE` ([Kconfig:711][kcft]), `CONFIG_NF_FLOW_TABLE_INET` ([Kconfig:703][kcinet]) and `CONFIG_NFT_FLOW_OFFLOAD` ([Kconfig:504][kcnft]). +The kernel options are `CONFIG_NF_FLOW_TABLE` ([Kconfig:711][kcft]; it depends on `NETFILTER_INGRESS`, `NF_CONNTRACK` and `NF_TABLES`), `CONFIG_NF_FLOW_TABLE_INET` ([Kconfig:703][kcinet]; [nf_flow_table_inet.c:68-96][inet68] registers the inet, IPv4 and IPv6 flowtable types) and `CONFIG_NFT_FLOW_OFFLOAD` ([Kconfig:504][kcnft]). ## How a flow gets in [`nft_flow_offload_eval()`][eval] runs on a packet in the forward path and checks: -- **Protocol.** TCP, UDP or GRE. For TCP the conntrack entry must be established and assured ([`nf_conntrack_tcp_established()`][tcpest]; the status bit is described as "Conntrack should never be early-expired" in [nf_conntrack_common.h:51][uapiass]) and the packet must not carry FIN or RST. UDP and GRE need no state check. GRE needs `CONFIG_NF_CT_PROTO_GRE` and is skipped when NAT is in use or when the conntrack tuple has a GRE key, which conntrack fills in only for GRE version 1 ([nf_conntrack_proto_gre.c:226-245][grekey]); the code comment says "No support for GRE v1". The hook then matches only GRE version 0 ([nf_flow_table_ip.c:270][gre0]). -- **Skipped flows and packets.** A flow with a conntrack helper attached, with `IPS_SEQ_ADJUST` set ("Connection needs TCP sequence adjusted", [nf_conntrack_common.h:70][uapiseq]) or with `IPS_NAT_CLASH` set is skipped ([nft_flow_offload.c:98-99][evalcheck]). Packets that carry a security path (IPsec) or IPv4 options are skipped ([`nft_flow_offload_skip()`][skip]). +- **Protocol.** TCP, UDP or GRE. For TCP the conntrack entry must be established and assured ([`nf_conntrack_tcp_established()`][tcpest]; the status bit is described as "Conntrack should never be early-expired" in [nf_conntrack_common.h:51][uapiass]) and the packet must not carry FIN or RST. UDP and GRE need no state check. GRE needs `CONFIG_NF_CT_PROTO_GRE` ([nft_flow_offload.c:81][grecfg]) and is skipped when NAT is in use or when the conntrack tuple has a GRE key, which conntrack fills in only for GRE version 1 ([nf_conntrack_proto_gre.c:226-245][grekey]); the code comment says "No support for GRE v1". The hook then matches only GRE version 0 ([nf_flow_table_ip.c:270][gre0]). +- **Skipped flows and packets.** A flow with a conntrack helper attached, with `IPS_SEQ_ADJUST` set ("Connection needs TCP sequence adjusted", [nf_conntrack_common.h:70][uapiseq]) or with `IPS_NAT_CLASH` set ("a conntrack entry that clashed with an existing entry on insert", [nf_conntrack_common.h:99][uapiclash]) is skipped ([nft_flow_offload.c:98-99][evalcheck]). Packets that carry a security path (IPsec) or IPv4 options are skipped ([`nft_flow_offload_skip()`][skip]). - **Confirmed.** The conntrack entry must be confirmed ([nft_flow_offload.c:102][evalconf]), which the header describes as "originating packet has left box" ([nf_conntrack_common.h:55][uapiconf]). - **Only once.** `test_and_set_bit(IPS_OFFLOAD_BIT, ...)` makes the first caller win ([nft_flow_offload.c:105][evalonce]). If all of that holds: 1. [`nft_flow_route()`][route] resolves the path. It reuses the route of the packet being processed for one direction and looks up the reverse route ([nf_flow_table_path.c:305][pathrev]). -2. Only the packet's own direction can carry an IPsec transform. The reverse lookup does no IPsec lookup in IPv4 unless the flow key's protocol is set ([route.c:2984][route2984]), which `nft_flow_route()` doesn't do, and none in IPv6 ([ipv6/netfilter.c:102][v6route]). [`b5964aac51e0`][cb596] says "the existing basic xfrm offload (which only works in one direction)". +2. Only the packet's own direction can carry an IPsec transform. Its route is the one the forward path attached to the packet ([ip_forward.c:121][ipfwd]). The reverse lookup does no IPsec lookup in IPv4 unless the flow key's protocol is set ([route.c:2984][route2984]), and `nft_flow_route()` clears the key without setting it ([nf_flow_table_path.c:280][routefl]); IPv6 does none ([ipv6/netfilter.c:102][v6route]). [`b5964aac51e0`][cb596] says "the existing basic xfrm offload (which only works in one direction)". 3. For routes without an IPsec transform the resolution also requires the egress device to be in the flowtable and, on an Ethernet device, a neighbour entry in a valid state (`NUD_VALID`, [nf_flow_table_path.c:61-77][pathnud]). 4. [`flow_offload_alloc()`][alloc] allocates the entry with a reference to the conntrack entry and the SNAT and DNAT flags. 5. [`flow_offload_add()`][add] inserts the entry's two tuples, one per direction, into one resizable hash table. Its comment says garbage collection walks only the original-direction entries, so that one is inserted last. @@ -98,7 +105,7 @@ For TCP, [`flow_offload_ct_tcp()`][cttcp] sets the "be liberal" flag on the conn Each direction of a flow has a transmit type chosen at add time ([`nft_xmit_type()`][xmittype]): - **Neighbour.** The default. The destination MAC comes from the neighbour entry for the next hop ([`nf_flow_queue_xmit4()`][xmit4]). -- **Direct.** The entry stores the MAC addresses. The path code picks it for PPPoE paths ([nf_flow_table_path.c:155][pathppp]), bridge paths ([nf_flow_table_path.c:187][pathbr]), and Ethernet devices when hardware offload is on ([nf_flow_table_path.c:194-196][pathhw]). +- **Direct.** The entry stores the MAC addresses ([nf_flow_table_core.c:137-141][macstore]). The path code picks it for PPPoE paths ([nf_flow_table_path.c:155][pathppp]), bridge paths ([nf_flow_table_path.c:187][pathbr]), and Ethernet devices when hardware offload is on ([nf_flow_table_path.c:194-196][pathhw]). - **IPsec (xfrm).** Chosen for a route that has an IPsec transform. The hit goes out through `dst_output()` ([`nf_flow_xmit_xfrm()`][xfrm]). A flow caches its route in the entry, except in direct mode without a tunnel, where the route is released ([nf_flow_table_core.c:129-136][coredst]). @@ -114,7 +121,7 @@ A flow caches its route in the entry, except in direct mode without a tunnel, wh The hook then transmits as described in [Transmit modes](#transmit-modes): [`nf_flow_queue_xmit4()`][xmit4] pushes any egress VLAN, PPPoE or tunnel header back on, builds the link-layer header with `dev_hard_header()` and calls `dev_queue_xmit()`. IPsec hits skip that and go out through `dst_output()`. -[`nf_flow_offload_ipv6_hook()`][ip6hook] does the same, with the hop limit in place of the TTL ([nf_flow_table_ip.c:1069][ip6ttl] checks it, [nf_flow_table_ip.c:1145][ip6dec] decrements it). The next header has to be TCP, UDP or GRE, so a packet with any extension header is a miss ([nf_flow_table_ip.c:1050-1067][ip6ext]). It strips IPv6-in-IPv6 tunnel headers since v7.0 ([`d98103575dcd`][cd9810]). Mixed-family tunnels are not supported: the IPv4 hook accepts only IPIP ([nf_flow_table_ip.c:331][ipip]), the IPv6 hook only IPv6-in-IPv6 ([nf_flow_table_ip.c:356][ip6ip6]), and [`d0107f381622`][cd0107] says IPv4 over IPv6 and SIT tunnel offloading "is not enabled yet". +[`nf_flow_offload_ipv6_hook()`][ip6hook] does the same, with the hop limit in place of the TTL ([nf_flow_table_ip.c:1069][ip6ttl] checks it, [nf_flow_table_ip.c:1145][ip6dec] decrements it). The next header has to be TCP, UDP or GRE, so a packet with any extension header is a miss ([nf_flow_table_ip.c:1050-1067][ip6ext]). It strips IPv6-in-IPv6 tunnel headers since v7.0 ([`d98103575dcd`][cd9810]). Mixed-family tunnels are not supported: the IPv4 path strips only IPIP headers ([nf_flow_table_ip.c:331][ipip]), the IPv6 path only IPv6-in-IPv6 headers ([nf_flow_table_ip.c:356][ip6ip6]), and [`d0107f381622`][cd0107] says IPv4 over IPv6 and SIT tunnel offloading "is not enabled yet". Packets that the hook doesn't take go to the classic path: fragments and packets with IPv4 options ([nf_flow_table_ip.c:230][frag]), a TTL or hop limit of 1 or less ([nf_flow_table_ip.c:253][ttl], [nf_flow_table_ip.c:1069][ip6ttl]), and GRE other than version 0 ([nf_flow_table_ip.c:270][gre0], [nf_flow_table_ip.c:1086][gre06]). @@ -125,11 +132,27 @@ The garbage collector, [`nf_flow_offload_work_gc()`][gcwork], re-queues itself o - **A timeout.** Each entry has a deadline. A software hit pushes it out, at most once a second ([nf_flow_table_core.c:373][corelimit]); this also applies to flows that are closing (the refresh writes the deadline before it checks for `NF_FLOW_CLOSING`). The default is 30 seconds for TCP ([nf_conntrack_proto_tcp.c:1658][tcpoff]) and UDP ([nf_conntrack_proto_udp.c:204][udpoff]), set by the sysctls `net.netfilter.nf_flowtable_tcp_timeout` and `net.netfilter.nf_flowtable_udp_timeout` ([nf_conntrack_standalone.c:789][sysctltcp], [nf_conntrack_standalone.c:839][sysctludp]), and 30 seconds for GRE, which has no sysctl ([`flow_offload_get_timeout()`][gettimeout], [nf_flow_table.h:209][flowtimeout]). For hardware flows, the collector asks the driver for statistics once less than 90% of the timeout remains and extends the deadline from the last-used time ([`nf_flow_offload_stats()`][hwstats], [`flow_offload_work_stats()`][hwstatwork]). - **A dying conntrack entry**, checked by `nf_ct_is_dying()` ([`nf_flow_offload_gc_step()`][gcstep]); lookups also miss ([`flow_offload_lookup()`][lookup]). - **A stale route**, for flows that keep one. The collector checks this since v7.3 ([`edd51a233438`][cedd51]); the hook checks it too ([`nf_flow_offload_forward()`][forward]). -- **A missing output device, or a neighbour lookup error.** The hook tears the flow down and drops that packet ([nf_flow_table_ip.c:870-880][neigherr]). A missing neighbour entry is created by the lookup ([`ip_neigh_gw4()`][neigh4]), so only a failure to create it counts. +- **A missing output device, or a neighbour lookup error.** The hook tears the flow down and drops that packet ([nf_flow_table_ip.c:870-880][neigherr] in neighbour mode, [nf_flow_table_ip.c:885-889][neighdir] in direct mode for a missing device). A missing neighbour entry is created by the lookup ([`ip_neigh_gw4()`][neigh4]), so only a failure to create it counts. - **A device going down.** The `NETDEV_DOWN` notifier ([nft_flow_offload.c:248][notifier]) calls [`nf_flow_table_cleanup()`][cleanup], which tears down the flows that use that device in either direction ([`nf_flow_table_do_cleanup()`][docleanup]). - **Deleting the flowtable** ([`nf_flow_table_free()`][free]). -**TCP FIN and RST** don't end the flow at once (since v6.14, [`fdbaf5163331`][cfdbaf]). [`nf_flow_state_check()`][state] marks the flow `NF_FLOW_CLOSING` and the packet still takes the fast path; the collector removes a hardware entry early ([`nf_flow_offload_gc_step()`][gcstep]). Software hits keep pushing the deadline, so the flow leaves when the timeout expires after the last packet, and [`flow_offload_fixup_ct()`][fixup] then gives conntrack the close state and timeout. A later SYN on a closing flow tears it down at once, sets the conntrack state to close and sends that packet to the classic path ([`nf_flow_state_check()`][state], [`flow_offload_fixup_tcp()`][fixtcp]). +``` + flow add + │ + ▼ + in table ◄── software hits refresh the deadline + │ │ + │ └── TCP FIN or RST ──► closing (hits continue) + │ │ + ▼ ▼ + torn down ◄── timeout, dying conntrack entry, stale route, + │ device down, flowtable deleted, SYN on a closing flow + │ lookups miss, conntrack entry fixed up, offload bit cleared + ▼ + removed by the garbage collector, up to a second later +``` + +**TCP FIN and RST** don't end the flow at once (since v6.14, [`fdbaf5163331`][cfdbaf]). [`nf_flow_state_check()`][state] marks the flow `NF_FLOW_CLOSING` and the packet still takes the fast path; [`nf_flow_offload_gc_step()`][gcstep] deletes the hardware rule early and the flow stays in the software table. Software hits keep pushing the deadline, so the flow leaves when the timeout expires after the last packet, and [`flow_offload_fixup_ct()`][fixup] then gives conntrack the close state and timeout. A later SYN on a closing flow tears it down at once, sets the conntrack state to close and sends that packet to the classic path ([`nf_flow_state_check()`][state], [`flow_offload_fixup_tcp()`][fixtcp]). **Adding again.** [`flow_offload_teardown()`][tear] clears the offload bit on the conntrack entry ([nf_flow_table_core.c:401][coreclear]), so a later forward-chain packet that matches the `flow add` rule can add the flow again. @@ -144,7 +167,11 @@ The garbage collector, [`nf_flow_offload_work_gc()`][gcwork], re-queues itself o The comment in [`nf_flow_table_extend_ct_timeout()`][ext] calls the value "an arbitrary timeout large enough not to ever expire". -**Hand-back to conntrack.** [`flow_offload_fixup_ct()`][fixup] runs at teardown. For TCP it keeps the last conntrack state, or sets close if the flow was closing, and clears the recorded maximum window and the max-ack flag; the code comment says that otherwise conntrack's TCP reset validation would fail ([`flow_offload_fixup_tcp()`][fixtcp]). TCP and UDP flows have the conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired; a closing TCP flow gets the close timeout unconditionally ([nf_flow_table_core.c:250-252][corefix]). Except for that case the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64][tcp5d]), keeps whatever is left of its day. GRE flows return before any timeout change ([nf_flow_table_core.c:240-241][coregre]). +**Hand-back to conntrack.** [`flow_offload_fixup_ct()`][fixup] runs at teardown. + +- **TCP state.** It keeps the last conntrack state, or sets close if the flow was closing, and clears the recorded maximum window and the max-ack flag; the code comment says that otherwise conntrack's TCP reset validation would fail ([`flow_offload_fixup_tcp()`][fixtcp]). +- **Timeout.** TCP and UDP flows have the conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired; a closing TCP flow gets the close timeout unconditionally ([nf_flow_table_core.c:250-252][corefix]). Except for that case the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64][tcp5d]), keeps the timeout it has left, at most a day, until a packet goes through conntrack again: conntrack refreshes the timeout of any entry it sees and does not check the offload bit ([`__nf_ct_refresh_acct()`][refreshacct]). +- **GRE.** GRE flows return before any timeout change ([nf_flow_table_core.c:240-241][coregre]). ## Hardware offload @@ -152,13 +179,14 @@ The kernel documentation says a flowtable can offload flows to a device that sup The driver interface is the tc flow-block interface: -- **Binding.** Registering a flowtable's ingress hook on a device first calls the flowtable type's `setup` callback, [`nf_flow_table_offload_setup()`][setup] ([nf_tables_api.c:9068][apisetup]). Without `flags offload` it only sets up the XDP path ([nf_flow_table_offload.c:1279-1280][offsetup]); XDP programs can then look the table up with [`bpf_xdp_flow_lookup`][bpf] ([`391bb6594fd3`][c391bb], v6.11). With `flags offload`, if the device has `ndo_setup_tc` the flowtable calls it with `TC_SETUP_FT` ([`nf_flow_table_offload_cmd()`][offcmd]); otherwise it uses the indirect-block path `flow_indr_dev_setup_offload()` ([`nf_flow_table_indr_offload_cmd()`][indrcmd]). The driver's callback is added to the flowtable's block ([nf_flow_table_offload.c:1193][splice]). +- **Binding.** Registering a flowtable's ingress hook on a device first calls the flowtable type's `setup` callback, [`nf_flow_table_offload_setup()`][setup] ([nf_tables_api.c:9068][apisetup]). Without `flags offload` it only sets up the XDP path ([nf_flow_table_offload.c:1279-1280][offsetup]); XDP programs can then look the table up with [`bpf_xdp_flow_lookup`][bpf], which therefore sees only flowtables without `flags offload` ([`391bb6594fd3`][c391bb], v6.11). With `flags offload`, if the device has `ndo_setup_tc` the flowtable calls it with `TC_SETUP_FT` ([`nf_flow_table_offload_cmd()`][offcmd]); otherwise it uses the indirect-block path `flow_indr_dev_setup_offload()` ([`nf_flow_table_indr_offload_cmd()`][indrcmd]). The driver's callback is added to the flowtable's block ([nf_flow_table_offload.c:1193][splice]). - **Per flow.** [`nf_flow_offload_tuple()`][offtuple] calls every callback with `TC_SETUP_CLSFLOWER` and a `flow_cls_offload` request, the request type tc flower uses. The command is `FLOW_CLS_REPLACE` to add a flow (with a flow rule built by [`nf_flow_rule_route_ipv4()`][rulefn] and [`nf_flow_rule_match()`][rulematch]: match keys plus actions), `FLOW_CLS_DESTROY` or `FLOW_CLS_STATS`. - **In-tree drivers that handle `TC_SETUP_FT`** as of v7.4-rc3: the mlx5 representor code ([rep/tc.c:261][mlx5], [rep/tc.c:675][mlx5ind]), the MediaTek Ethernet and WED drivers ([mtk_ppe_offload.c:674][mtkppe], [mtk_wed.c:2790][mtkwed]), the mt76 Wi-Fi driver ([npu.c:385][mt76]) and the Airoha Ethernet driver ([airoha_eth.c:3674][airoha]). DSA user ports pass the command to the conduit device ([dsa/user.c:1729][dsa]). `netdevsim` returns 0 and registers no callback ([netdevsim/tc.c:92][netdevsim]). Behaviour with `flags offload`, from the source: -- A device that can't offload makes creating the flowtable, or adding the device to it, fail: the `setup` error is returned before the hook is registered ([nf_tables_api.c:9068][apisetup], [nf_tables_api.c:9104-9106][apiregall]). A device that registers later is handled by the netdev notifier, which returns `NOTIFY_BAD` when registering the hook fails ([nf_tables_api.c:9889][apinotif]). A change of the `offload` flag on an existing flowtable is rejected with `-EOPNOTSUPP` ([nf_tables_api.c:9200-9203][apitoggle]). +- If the driver rejects `TC_SETUP_FT` ([`nf_flow_table_offload_cmd()`][offcmd]), or no indirect-block driver binds (`-EOPNOTSUPP`, [flow_offload.c:628][flowoffeop]), creating the flowtable or adding the device to it fails: the `setup` error is returned before the hook is registered ([nf_tables_api.c:9068][apisetup], [nf_tables_api.c:9104-9106][apiregall]). `netdevsim` returns 0 and registers no callback, so with it the flowtable loads and no flow is added in hardware ([`flow_offload_rule_add()`][ruleadd] returns `-ENOENT` when nothing was added). +- A device that registers later is handled by the netdev notifier, which returns `NOTIFY_BAD` when registering the hook fails ([nf_tables_api.c:9908-9913][apinotif2]); `register_netdevice()` then unregisters the device ([dev.c:11778][devreg]). A change of the `offload` flag on an existing flowtable is rejected with `-EOPNOTSUPP` ([nf_tables_api.c:9200-9203][apitoggle]). - A direction that goes through a valid Ethernet device uses direct mode ([nf_flow_table_path.c:194-196][pathhw]), so a direction without a tunnel caches no route. The IPsec direction still caches its route ([nf_flow_table_core.c:143-147][corexfrm]). - A hardware add that fails isn't reported. The flow stays in software with `NF_FLOW_HW` set, `[HW_OFFLOAD]` is set if the driver accepted at least one direction ([`flow_offload_rule_add()`][ruleadd], [`flow_offload_work_add()`][workadd]), and the add is queued again on later software hits, at most once a second and not for closing flows ([nf_flow_table_core.c:373-383][corelimit]). - Flows through an IPIP or IPv6-in-IPv6 tunnel stay in software ([`nf_flow_offload_unsupported()`][unsup]; [`6c5dcab95f4c`][c6c5d], v7.2). @@ -167,12 +195,13 @@ Behaviour with `flags offload`, from the source: ## Inspecting - **Conntrack listing.** `conntrack -L` shows `[OFFLOAD]` for software flows and `[HW_OFFLOAD]` for hardware flows ([nf_flowtable.rst:111-113][rst111], [nf_flowtable.rst:202-206][rst202]). `/proc/net/nf_conntrack` (with `CONFIG_NF_CONNTRACK_PROCFS`, [nf_conntrack_standalone.c:34][kcprocfs]) prints the same tags ([nf_conntrack_standalone.c:364-366][proctag]). +- **Checking that a flow was added.** The `[OFFLOAD]` tag in `conntrack -L` is how the kernel documentation says to identify offloaded flows ([nf_flowtable.rst:111-113][rst111]); the page has no other signal to offer, since there are no hit or miss counters. A flow without the tag was not added; the reasons are listed in [How a flow gets in](#how-a-flow-gets-in). - **What offloaded entries omit.** ctnetlink skips the timeout and protocol info ([nf_conntrack_netlink.c:579][nlnk]), and `/proc/net/nf_conntrack` skips the timeout and the TCP state ([nf_conntrack_standalone.c:339][procnf], [`tcp_print_conntrack()`][tcpprint]). - **Statistics.** With `CONFIG_NF_FLOW_TABLE_PROCFS` ([Kconfig:721][kconfig]), [`/proc/net/stat/nf_flowtable`][procfs] shows per-CPU counters of pending hardware add, delete and stats work; the file is created at [nf_flow_table_procfs.c:71][procfscreate] and its show function prints only those three columns. They are incremented when work is queued and decremented when it completes ([nf_flow_table_offload.c:1047-1077][statinc]). There are no hit or miss counters. ## Limits -- **Rule changes.** Removing a rule runs the expression's `deactivate` and `destroy` callbacks, which release the expression's use of the flowtable and its conntrack hook registration for the netns, and don't touch flow entries ([nft_flow_offload.c:185-206][deact]). A hit never reaches the forward chain, and a software hit pushes the deadline out ([`flow_offload_refresh()`][refresh]). Only the stored NAT rewrite is applied to a hit ([`nf_flow_offload_forward()`][forward]). So a flow already in the table keeps being forwarded after the `flow add` rule is removed or a drop rule is added, as long as packets keep arriving. The ways a flow leaves are listed in [How a flow leaves](#how-a-flow-leaves). +- **Rule changes.** Removing a rule runs the expression's `deactivate` and `destroy` callbacks, which release the expression's use of the flowtable and its conntrack hook registration for the netns, and don't touch flow entries ([nft_flow_offload.c:185-206][deact]). A hit never reaches the forward chain, and a software hit pushes the deadline out ([`flow_offload_refresh()`][refresh]). Only the stored NAT rewrite is applied to a hit ([`nf_flow_offload_forward()`][forward]). So a flow already in the table keeps being forwarded after the `flow add` rule is removed or a drop rule is added to the prerouting, forward or postrouting chains, as long as packets keep arriving; hardware flows get their deadline from driver statistics instead ([`flow_offload_work_stats()`][hwstatwork]). The ways a flow leaves are listed in [How a flow leaves](#how-a-flow-leaves). - **Cache.** The kernel documentation says the table behaves like a cache and that entries might get stale if the destination MAC address or the egress device changes, which "might be a problem" with software mode combined with bridging and IP forwarding, and with hardware offload ([nf_flowtable.rst:214-222][rst214]). - **IPsec.** A packet that carries a security path never triggers an add ([`nft_flow_offload_skip()`][skip]). In the direction whose route has an IPsec transform the device-list and neighbour checks are skipped ([nf_flow_table_path.c:314-320][pathskip]); the other direction gets them like any other flow. - **ip6gre.** The commit that extended the GRE timeout, [`46ecda278f6e`][c46ecd], says `ip6gre` tunnels with the default encapsulation limit carry a Destination Options header before GRE, which the flowtable doesn't parse, so every packet of the flow takes the classic path. @@ -188,7 +217,12 @@ The software flow table was committed on 8 January 2018 ([`ac2a66665e23`][cac2a] Hardware offload reached the tree in v5.5 through the tc flow-block interface rather than through the dedicated `ndo` hooks proposed in the RFC. Pablo Neira Ayuso added it in [`c29f74e0df7a`][cc29f], which called `ndo_setup_tc` with `TC_SETUP_BLOCK` and the callbacks with `TC_SETUP_FT`. The arrangement described above came from two fixes by wenxu in the same release, [`ee1bcfe01251`][cee1b] and [`e052901991ae`][ce052]. wenxu added the indirect path in [`b5140a36da78`][cb514] (v5.7). -**GRE timeout history.** The conntrack timeout extension was reworked in [`03428ca5cee9`][c0342] (v6.14), but it never took effect until [`53b3e60edb67`][c53b3] (v7.2): it compared a relative value with the absolute `ct->timeout`, so the update never succeeded. It also did not handle GRE. Once a GRE entry's timeout fell below five minutes it hit a `WARN_ON_ONCE()`. The default branch, `new_timeout = true` (one jiffy) in `03428ca5cee9`, took effect once `53b3e60edb67` made the update succeed. [`46ecda278f6e`][c46ecd] (v7.4-rc3) says "the default branch sets the new timeout to 1 jiffy, so the conntrack entry expires right away" and extends GRE like UDP. Both fixes carry `Cc: stable@vger.kernel.org`. The packets that bring a GRE entry's timeout down are those that still go through conntrack; the commit message gives packets with a TTL of 1 or less as an example. +**GRE timeout history.** + +- [`03428ca5cee9`][c0342] (v6.14) reworked the conntrack timeout extension. For GRE it fell into the default branch, which set `new_timeout = true` (one jiffy) and hit `WARN_ON_ONCE()` once a GRE entry's timeout fell below five minutes. +- The extension never took effect until [`53b3e60edb67`][c53b3] (v7.2): it compared a relative value with the absolute `ct->timeout`, so the update never succeeded. Once it did, the default branch's one-jiffy timeout applied to GRE. +- [`46ecda278f6e`][c46ecd] (v7.4-rc3) says "the default branch sets the new timeout to 1 jiffy, so the conntrack entry expires right away" and extends GRE like UDP. The packets that bring a GRE entry's timeout down are those that still go through conntrack; the commit message gives packets with a TTL of 1 or less as an example. +- Both fixes carry `Cc: stable@vger.kernel.org`. ## Differences from `nf_flowtable.rst` @@ -198,7 +232,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, - It says hits are sent with `neigh_xmit()` ([nf_flowtable.rst:21][rst21], [nf_flowtable.rst:74][rst74]). Direct mode has called `dev_queue_xmit()` since [`7a27f6ab4135`][c7a27] (v5.13, the same day as the documentation update), and [`b5964aac51e0`][cb596] ("netfilter: flowtable: consolidate xmit path", v6.19) moved neighbour mode to it too. - It says packets are not seen "from any of the Netfilter hooks coming after ingress" ([nf_flowtable.rst:22-23][rst22]). The netfilter egress hook, added in [`42df6e1d221d`][c42df] (v5.16, after the documentation was written), does see software hits, because they end in `dev_queue_xmit()`. An IPsec hit also runs postrouting, because it goes out through `dst_output()`. - It says the rule adds the flow on "the TCP syn-ack packet" ([nf_flowtable.rst:105-106][rst105]). The code requires an established and assured conntrack entry ([`nf_conntrack_tcp_established()`][tcpest]). [`e5eaac2beb54`][ce5ea] (v5.18) added that check, and its message says the patch makes `nft_flow_offload` "skip packets in SYN_RECV state" and "postpones offload up to the next packet". -- It says the flowtable supports TCP and UDP ([nf_flowtable.rst:10][rst10]). GRE has been accepted since [`4e8d9584d154`][c4e8d] (v5.18). +- It lists TCP and UDP as the supported layer 4 protocols ([nf_flowtable.rst:10][rst10]). GRE has also been accepted since [`4e8d9584d154`][c4e8d] (v5.18). ## Further reading @@ -223,7 +257,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [add]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n335 [airoha]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/airoha/airoha_eth.c?h=v7.4-rc3#n3674 [alloc]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n53 -[apinotif]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9889 +[apinotif2]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9908 [apiregall]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9104 [apisetup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9068 [apitoggle]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9200 @@ -268,6 +302,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [dev5032]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c?h=v7.4-rc3#n5032 [dev6290]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c?h=v7.4-rc3#n6290 [dev6298]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c?h=v7.4-rc3#n6298 +[devreg]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c?h=v7.4-rc3#n11778 [docleanup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n741 [dsa]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/dsa/user.c?h=v7.4-rc3#n1729 [dstchk]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_flow_table.h?h=v7.4-rc3#n314 @@ -279,6 +314,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [fillpath]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n45 [fixtcp]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n178 [fixup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n201 +[flowoffeop]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/flow_offload.c?h=v7.4-rc3#n628 [flowtimeout]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_flow_table.h?h=v7.4-rc3#n209 [forward]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n475 [frag]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n230 @@ -291,7 +327,9 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [gettimeout]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n316 [gre0]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n270 [gre06]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1086 +[grecfg]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n81 [grekey]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_gre.c?h=v7.4-rc3#n226 +[hookalloc]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n2456 [hwstats]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1149 [hwstatwork]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1011 [indrcmd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1239 @@ -301,6 +339,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [ip6hook]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1233 [ip6ip6]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n356 [ip6ttl]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1069 +[ipfwd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/ip_forward.c?h=v7.4-rc3#n121 [iphook]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n904 [ipip]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n331 [kcft]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/Kconfig?h=v7.4-rc3#n711 @@ -312,6 +351,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [lookup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n408 [lwn1]: https://lwn.net/Articles/738214/ [lwn2]: https://lwn.net/Articles/742164/ +[macstore]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n137 [mlx5]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mellanox/mlx5/core/en/rep/tc.c?h=v7.4-rc3#n261 [mlx5ind]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mellanox/mlx5/core/en/rep/tc.c?h=v7.4-rc3#n675 [mt76]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/wireless/mediatek/mt76/npu.c?h=v7.4-rc3#n385 @@ -319,6 +359,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [mtkwed]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mediatek/mtk_wed.c?h=v7.4-rc3#n2790 [mtu]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n289 [neigh4]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/route.h?h=v7.4-rc3#n408 +[neighdir]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n885 [neigherr]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n870 [netdevsim]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/netdevsim/tc.c?h=v7.4-rc3#n92 [nfstolen]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/core.c?h=v7.4-rc3#n649 @@ -342,8 +383,10 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [proctag]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_standalone.c?h=v7.4-rc3#n364 [qxmit]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n822 [refresh]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n367 +[refreshacct]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_core.c?h=v7.4-rc3#n2089 [route]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n272 [route2984]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/route.c?h=v7.4-rc3#n2984 +[routefl]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n280 [rst]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3 [rst10]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n10 [rst105]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n105 @@ -382,6 +425,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [tear]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n399 [ttl]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n253 [uapiass]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/netfilter/nf_conntrack_common.h?h=v7.4-rc3#n51 +[uapiclash]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/netfilter/nf_conntrack_common.h?h=v7.4-rc3#n102 [uapiconf]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/netfilter/nf_conntrack_common.h?h=v7.4-rc3#n55 [uapiseq]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/netfilter/nf_conntrack_common.h?h=v7.4-rc3#n70 [udpoff]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_udp.c?h=v7.4-rc3#n204 @@ -392,7 +436,9 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [xfrm]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n300 [xfrm4]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/xfrm4_output.c?h=v7.4-rc3#n33 [xfrm6]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv6/xfrm6_output.c?h=v7.4-rc3#n114 -[xfrmres]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/xfrm/xfrm_output.c?h=v7.4-rc3#n593 +[xfrmdirect]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/xfrm/xfrm_output.c?h=v7.4-rc3#n786 +[xfrmlo]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/xfrm/xfrm_output.c?h=v7.4-rc3#n600 +[xfrmpost]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/xfrm/xfrm_output.c?h=v7.4-rc3#n608 [xmit]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n776 [xmit4]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n838 [xmittype]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n19 From fca0e3438fc19cd854b64ebb89397da4c880931c Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 20:07:49 -0800 Subject: [PATCH 14/22] net: scope IPsec, hardware-removal and re-add statements on flowtable page Fix the drop-rule and tag statements, qualify hardware removal timing and device-down matching, correct the GRE initializer wording, cite IPv6 equivalents and add the XDP lookup build condition. --- docs/net/flowtable.md | 59 ++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index 021acaed..5419af26 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -1,6 +1,6 @@ # Flowtable: The Fast Path for Forwarded Flows -> How the kernel lets forwarded connections that are already in a flow table skip most of the netfilter hook path +> How the kernel lets forwarded flows that are already in a flow table skip most of the netfilter hook path Source links point at Linux v7.4-rc3, the tree this page was checked against. Release numbers are mainline releases. Behaviour that changed recently is marked with the release that introduced it and links to the commit. "The kernel documentation" is [`nf_flowtable.rst`][rst]. @@ -27,8 +27,8 @@ The diagram is simplified from Fig. 1 of the kernel documentation ([nf_flowtable Which hooks a hit passes, from the source: - The tc ingress hook runs before the netfilter ingress hook where the flowtable sits ([dev.c:6290][dev6290], [dev.c:6298][dev6298]). -- A software hit is transmitted with `dev_queue_xmit()` ([`__nf_flow_queue_xmit()`][xmit]), which runs the netfilter egress hook and tc egress ([dev.c:5024][dev5024], [dev.c:5032][dev5032]). -- A hit whose route has an IPsec transform is transmitted with `dst_output()` ([`nf_flow_xmit_xfrm()`][xfrm]), which runs the postrouting hook ([xfrm4_output.c:33][xfrm4], [xfrm6_output.c:114][xfrm6]). After the transform, `xfrm_output_resume()` runs the local-out hook ([xfrm_output.c:600][xfrmlo]) and postrouting again for the encapsulated packet ([xfrm_output.c:608][xfrmpost]). IPsec packet offload in tunnel mode goes out through `xfrm_dev_direct_output()` instead ([xfrm_output.c:786][xfrmdirect]). +- A software hit that isn't an IPsec flow is transmitted with `dev_queue_xmit()` ([`__nf_flow_queue_xmit()`][xmit]), which runs the netfilter egress hook and tc egress ([dev.c:5024][dev5024], [dev.c:5032][dev5032]). +- A hit whose route has an IPsec transform is transmitted with `dst_output()` ([`nf_flow_xmit_xfrm()`][xfrm]), which runs the postrouting hook ([xfrm4_output.c:33][xfrm4], [xfrm6_output.c:114][xfrm6]). After the transform, `xfrm_output_resume()` runs the local-out hook ([xfrm_output.c:600][xfrmlo]) and postrouting again for the encapsulated packet: with a single transform it comes from `dst_output()` ([xfrm_output.c:604-605][xfrmpost2]), and the `nf_hook()` call handles a nested transform ([xfrm_output.c:608][xfrmpost]). IPsec packet offload in tunnel mode goes out through `xfrm_dev_direct_output()` instead ([xfrm_output.c:786][xfrmdirect]). The kernel documentation says "you do not see these packets from any of the Netfilter hooks coming after ingress" ([nf_flowtable.rst:22-23][rst22]). The source differs; see [Differences](#differences-from-nf_flowtablerst). @@ -53,11 +53,11 @@ table inet x { - **The statement.** `flow add @f` is evaluated by [`nft_flow_offload_eval()`][eval]. It is accepted only in a chain on the forward hook ([`nft_flow_offload_validate()`][validate]). The example rule matches IPv4 TCP. - **What a hit skips.** A hit is consumed with `NF_STOLEN` ([`nf_flow_queue_xmit()`][qxmit]), so the later packets of a flow that hit the table don't reach the prerouting and forward chains, and reach postrouting only for IPsec hits (above). The kernel documentation says the `counter` rule stops being updated ([nf_flowtable.rst:105-109][rst105]). -- **Rule evaluation.** An earlier rule that accepts or drops the packet ends evaluation of that chain ([nf_tables_core.c:306][nftverdict]), so `flow add` never runs for it in that chain. `flow add` returns no verdict when it adds the flow. On every other path it sets `NFT_BREAK` ([`nft_flow_offload_eval()`][eval]), which skips the rest of that rule and continues with the next rule ([nf_tables_core.c:292-295][nftcore]). So statements after `flow add` in the same rule run only for the packet that adds the flow. +- **Rule evaluation.** An earlier rule that accepts or drops the packet ends evaluation of the base chain, including any chain it jumped to ([nf_tables_core.c:306-312][nftverdict]), so `flow add` never runs for it in that chain. `flow add` returns no verdict when it adds the flow. On every other path it sets `NFT_BREAK` ([`nft_flow_offload_eval()`][eval]), which skips the rest of that rule and continues with the next rule ([nf_tables_core.c:292-295][nftcore]). So statements after `flow add` in the same rule run only for the packet that adds the flow. - **The `counter` flag.** Software hits update conntrack's counters only if the flowtable has the `counter` flag ([nf_flow_table_ip.c:520][ctcount]; documented in [nf_flowtable.rst:165-181][rst165]). The update reaches only conntrack entries that have the accounting extension ([`nf_ct_acct_add()`][acctfn]), which is added when the entry is created and only if the sysctl `net.netfilter.nf_conntrack_acct` is set ([nf_conntrack_acct.h:36][acct], [nf_conntrack_core.c:1789][acctext], [sysctl][acctsysctl]). - **Priority.** The kernel documentation says to "make sure the flowtable priority is smaller than the nftables ingress chain hence the flowtable runs before in the pipeline" ([nf_flowtable.rst:99-103][rst99]). A hit returns `NF_STOLEN`, which ends hook traversal ([`nf_hook_slow()`][nfstolen]), so an ingress chain that runs after the flowtable doesn't see it. - **Device list.** Path resolution follows VLAN, PPPoE and bridge devices down to the real device ([`nft_dev_fill_forward_path()`][fillpath]) and requires that device to be in the flowtable's list ([nf_flow_table_path.c:198][pathdev]). The check runs for both directions and is skipped for IPsec routes ([nf_flow_table_path.c:314-320][pathskip]). The kernel documentation says the real device is sufficient and the VLAN and PPPoE devices need not be listed ([nf_flowtable.rst:129-130][rst129]). -- **Since v7.2** ([`871df5007eda`][c871df]), a failed path discovery or a device that isn't in the flowtable makes `flow add` give up. The commit message says "If forward path discovery fails for any reason or netdevice is not registered for this flowtable, then bail out to classic forwarding path rather than providing incomplete forwarding path." A device name in the flowtable that matches no existing device is accepted and creates no hook ([nf_tables_api.c:2456][hookalloc]), so no device of that name is in the flowtable. +- **Since v7.2** ([`871df5007eda`][c871df]), a failed path discovery or a device that isn't in the flowtable makes `flow add` give up. The commit message says "If forward path discovery fails for any reason or netdevice is not registered for this flowtable, then bail out to classic forwarding path rather than providing incomplete forwarding path." A device name in the flowtable that matches no existing device is accepted and registers no hook until a device with that name appears ([nf_tables_api.c:2456][hookalloc]; since v6.16, [`73319a8ee18b`][c73319] and [`a331b78a5525`][ca331]). Two flowtable options appear in the kernel documentation: `counter` ([nf_flowtable.rst:165-181][rst165]) and `flags offload` ([nf_flowtable.rst:183-196][rst183]): @@ -79,21 +79,21 @@ table inet x { } ``` -The kernel options are `CONFIG_NF_FLOW_TABLE` ([Kconfig:711][kcft]; it depends on `NETFILTER_INGRESS`, `NF_CONNTRACK` and `NF_TABLES`), `CONFIG_NF_FLOW_TABLE_INET` ([Kconfig:703][kcinet]; [nf_flow_table_inet.c:68-96][inet68] registers the inet, IPv4 and IPv6 flowtable types) and `CONFIG_NFT_FLOW_OFFLOAD` ([Kconfig:504][kcnft]). +The kernel options are `CONFIG_NF_FLOW_TABLE` ([Kconfig:711][kcft]; it depends on `NETFILTER_INGRESS`, `NF_CONNTRACK` and `NF_TABLES`), `CONFIG_NF_FLOW_TABLE_INET` ([Kconfig:703][kcinet]; [nf_flow_table_inet.c:68-96][inet68] defines the inet, IPv4 and IPv6 flowtable types) and `CONFIG_NFT_FLOW_OFFLOAD` ([Kconfig:504][kcnft]). ## How a flow gets in [`nft_flow_offload_eval()`][eval] runs on a packet in the forward path and checks: - **Protocol.** TCP, UDP or GRE. For TCP the conntrack entry must be established and assured ([`nf_conntrack_tcp_established()`][tcpest]; the status bit is described as "Conntrack should never be early-expired" in [nf_conntrack_common.h:51][uapiass]) and the packet must not carry FIN or RST. UDP and GRE need no state check. GRE needs `CONFIG_NF_CT_PROTO_GRE` ([nft_flow_offload.c:81][grecfg]) and is skipped when NAT is in use or when the conntrack tuple has a GRE key, which conntrack fills in only for GRE version 1 ([nf_conntrack_proto_gre.c:226-245][grekey]); the code comment says "No support for GRE v1". The hook then matches only GRE version 0 ([nf_flow_table_ip.c:270][gre0]). -- **Skipped flows and packets.** A flow with a conntrack helper attached, with `IPS_SEQ_ADJUST` set ("Connection needs TCP sequence adjusted", [nf_conntrack_common.h:70][uapiseq]) or with `IPS_NAT_CLASH` set ("a conntrack entry that clashed with an existing entry on insert", [nf_conntrack_common.h:99][uapiclash]) is skipped ([nft_flow_offload.c:98-99][evalcheck]). Packets that carry a security path (IPsec) or IPv4 options are skipped ([`nft_flow_offload_skip()`][skip]). +- **Skipped flows and packets.** A flow with a conntrack helper attached, with `IPS_SEQ_ADJUST` set ("Connection needs TCP sequence adjusted", [nf_conntrack_common.h:70][uapiseq]) or with `IPS_NAT_CLASH` set ("a conntrack entry that clashed with an existing entry on insert", [nf_conntrack_common.h:102-103][uapiclash]) is skipped ([nft_flow_offload.c:98-99][evalcheck]). Packets that carry a security path (IPsec) or IPv4 options are skipped ([`nft_flow_offload_skip()`][skip]). - **Confirmed.** The conntrack entry must be confirmed ([nft_flow_offload.c:102][evalconf]), which the header describes as "originating packet has left box" ([nf_conntrack_common.h:55][uapiconf]). - **Only once.** `test_and_set_bit(IPS_OFFLOAD_BIT, ...)` makes the first caller win ([nft_flow_offload.c:105][evalonce]). If all of that holds: 1. [`nft_flow_route()`][route] resolves the path. It reuses the route of the packet being processed for one direction and looks up the reverse route ([nf_flow_table_path.c:305][pathrev]). -2. Only the packet's own direction can carry an IPsec transform. Its route is the one the forward path attached to the packet ([ip_forward.c:121][ipfwd]). The reverse lookup does no IPsec lookup in IPv4 unless the flow key's protocol is set ([route.c:2984][route2984]), and `nft_flow_route()` clears the key without setting it ([nf_flow_table_path.c:280][routefl]); IPv6 does none ([ipv6/netfilter.c:102][v6route]). [`b5964aac51e0`][cb596] says "the existing basic xfrm offload (which only works in one direction)". +2. Only the packet's own direction can carry an IPsec transform. Its route is the one the forward path attached to the packet ([ip_forward.c:121][ipfwd], [ip6_output.c:612][ip6fwd]). The reverse lookup does no IPsec lookup in IPv4 unless the flow key's protocol is set ([route.c:2984][route2984]), and `nft_flow_route()` clears the key without setting it ([nf_flow_table_path.c:280][routefl]); IPv6 does none ([ipv6/netfilter.c:102][v6route]). [`b5964aac51e0`][cb596] says "the existing basic xfrm offload (which only works in one direction)". 3. For routes without an IPsec transform the resolution also requires the egress device to be in the flowtable and, on an Ethernet device, a neighbour entry in a valid state (`NUD_VALID`, [nf_flow_table_path.c:61-77][pathnud]). 4. [`flow_offload_alloc()`][alloc] allocates the entry with a reference to the conntrack entry and the SNAT and DNAT flags. 5. [`flow_offload_add()`][add] inserts the entry's two tuples, one per direction, into one resizable hash table. Its comment says garbage collection walks only the original-direction entries, so that one is inserted last. @@ -132,8 +132,8 @@ The garbage collector, [`nf_flow_offload_work_gc()`][gcwork], re-queues itself o - **A timeout.** Each entry has a deadline. A software hit pushes it out, at most once a second ([nf_flow_table_core.c:373][corelimit]); this also applies to flows that are closing (the refresh writes the deadline before it checks for `NF_FLOW_CLOSING`). The default is 30 seconds for TCP ([nf_conntrack_proto_tcp.c:1658][tcpoff]) and UDP ([nf_conntrack_proto_udp.c:204][udpoff]), set by the sysctls `net.netfilter.nf_flowtable_tcp_timeout` and `net.netfilter.nf_flowtable_udp_timeout` ([nf_conntrack_standalone.c:789][sysctltcp], [nf_conntrack_standalone.c:839][sysctludp]), and 30 seconds for GRE, which has no sysctl ([`flow_offload_get_timeout()`][gettimeout], [nf_flow_table.h:209][flowtimeout]). For hardware flows, the collector asks the driver for statistics once less than 90% of the timeout remains and extends the deadline from the last-used time ([`nf_flow_offload_stats()`][hwstats], [`flow_offload_work_stats()`][hwstatwork]). - **A dying conntrack entry**, checked by `nf_ct_is_dying()` ([`nf_flow_offload_gc_step()`][gcstep]); lookups also miss ([`flow_offload_lookup()`][lookup]). - **A stale route**, for flows that keep one. The collector checks this since v7.3 ([`edd51a233438`][cedd51]); the hook checks it too ([`nf_flow_offload_forward()`][forward]). -- **A missing output device, or a neighbour lookup error.** The hook tears the flow down and drops that packet ([nf_flow_table_ip.c:870-880][neigherr] in neighbour mode, [nf_flow_table_ip.c:885-889][neighdir] in direct mode for a missing device). A missing neighbour entry is created by the lookup ([`ip_neigh_gw4()`][neigh4]), so only a failure to create it counts. -- **A device going down.** The `NETDEV_DOWN` notifier ([nft_flow_offload.c:248][notifier]) calls [`nf_flow_table_cleanup()`][cleanup], which tears down the flows that use that device in either direction ([`nf_flow_table_do_cleanup()`][docleanup]). +- **A missing output device, or a neighbour lookup error.** The hook tears the flow down and drops that packet ([nf_flow_table_ip.c:870-880][neigherr] in neighbour mode, [nf_flow_table_ip.c:885-889][neighdir] in direct mode for a missing device; the IPv6 lookup is [`ip_neigh_gw6()`][neigh6]). A missing neighbour entry is created by the lookup ([`ip_neigh_gw4()`][neigh4]), so only a failure to create it counts. +- **A device going down.** The `NETDEV_DOWN` notifier ([nft_flow_offload.c:248][notifier]) calls [`nf_flow_table_cleanup()`][cleanup], which tears down the flows whose input device in either direction is that device ([`nf_flow_table_do_cleanup()`][docleanup]); a VLAN, PPPoE or bridge device going down doesn't match, because the recorded input device is the real one ([nf_flow_table_path.c:241][pathin]). - **Deleting the flowtable** ([`nf_flow_table_free()`][free]). ``` @@ -144,19 +144,22 @@ The garbage collector, [`nf_flow_offload_work_gc()`][gcwork], re-queues itself o │ │ │ └── TCP FIN or RST ──► closing (hits continue) │ │ - ▼ ▼ + └───────────────┬──────────────┘ + ▼ torn down ◄── timeout, dying conntrack entry, stale route, + │ missing output device or neighbour error, │ device down, flowtable deleted, SYN on a closing flow │ lookups miss, conntrack entry fixed up, offload bit cleared ▼ - removed by the garbage collector, up to a second later + removed by the garbage collector on its next run (about a second); + hardware flows are removed after the driver delete completes ``` **TCP FIN and RST** don't end the flow at once (since v6.14, [`fdbaf5163331`][cfdbaf]). [`nf_flow_state_check()`][state] marks the flow `NF_FLOW_CLOSING` and the packet still takes the fast path; [`nf_flow_offload_gc_step()`][gcstep] deletes the hardware rule early and the flow stays in the software table. Software hits keep pushing the deadline, so the flow leaves when the timeout expires after the last packet, and [`flow_offload_fixup_ct()`][fixup] then gives conntrack the close state and timeout. A later SYN on a closing flow tears it down at once, sets the conntrack state to close and sends that packet to the classic path ([`nf_flow_state_check()`][state], [`flow_offload_fixup_tcp()`][fixtcp]). -**Adding again.** [`flow_offload_teardown()`][tear] clears the offload bit on the conntrack entry ([nf_flow_table_core.c:401][coreclear]), so a later forward-chain packet that matches the `flow add` rule can add the flow again. +**Adding again.** [`flow_offload_teardown()`][tear] clears the offload bit on the conntrack entry ([nf_flow_table_core.c:401][coreclear]), so a later forward-chain packet that matches the `flow add` rule can add the flow again, if it passes the checks in [How a flow gets in](#how-a-flow-gets-in) again. A TCP flow torn down by a SYN is in the close state, so it isn't added again until it is established. -**Conntrack timeout.** While a flow is in the table, conntrack may see few or none of its packets; the comment in [`nf_flow_table_extend_ct_timeout()`][ext] says so ([nf_flow_table_core.c:506-513][gccomment]). [`flow_offload_add()`][add] first sets the conntrack timeout to a day ([nf_flow_table_core.c:358][coreday]). Afterwards the collector calls [`nf_flow_table_extend_ct_timeout()`][ext], which sets a new timeout when less than five minutes remain. The value depends on the protocol and TCP state ([`nf_flow_table_tcp_timeout()`][tcpto]): +**Conntrack timeout.** While a flow is in the table, conntrack may see few or none of its packets; the comment in [`nf_flow_table_extend_ct_timeout()`][ext] says "nf_conntrack might not see any packets anymore" ([nf_flow_table_core.c:506-513][gccomment]). [`flow_offload_add()`][add] first sets the conntrack timeout to a day ([nf_flow_table_core.c:358][coreday]). Afterwards the collector calls [`nf_flow_table_extend_ct_timeout()`][ext], which sets a new timeout when less than five minutes remain. The value depends on the protocol and TCP state ([`nf_flow_table_tcp_timeout()`][tcpto]): | Flow | Conntrack timeout is set to | |------|-----------------------------| @@ -170,7 +173,7 @@ The comment in [`nf_flow_table_extend_ct_timeout()`][ext] calls the value "an ar **Hand-back to conntrack.** [`flow_offload_fixup_ct()`][fixup] runs at teardown. - **TCP state.** It keeps the last conntrack state, or sets close if the flow was closing, and clears the recorded maximum window and the max-ack flag; the code comment says that otherwise conntrack's TCP reset validation would fail ([`flow_offload_fixup_tcp()`][fixtcp]). -- **Timeout.** TCP and UDP flows have the conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired; a closing TCP flow gets the close timeout unconditionally ([nf_flow_table_core.c:250-252][corefix]). Except for that case the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64][tcp5d]), keeps the timeout it has left, at most a day, until a packet goes through conntrack again: conntrack refreshes the timeout of any entry it sees and does not check the offload bit ([`__nf_ct_refresh_acct()`][refreshacct]). +- **Timeout.** TCP and UDP flows have the conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired; a closing TCP flow gets the close timeout unconditionally ([nf_flow_table_core.c:250-252][corefix]). Except for that case the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64][tcp5d]), keeps the timeout it has left, at most a day, until a packet goes through conntrack again. Conntrack then refreshes the timeout to the established value, unless the entry has a fixed timeout ([`__nf_ct_refresh_acct()`][refreshacct], [nf_conntrack_core.c:2094-2096][refreshfix]). - **GRE.** GRE flows return before any timeout change ([nf_flow_table_core.c:240-241][coregre]). ## Hardware offload @@ -179,15 +182,15 @@ The kernel documentation says a flowtable can offload flows to a device that sup The driver interface is the tc flow-block interface: -- **Binding.** Registering a flowtable's ingress hook on a device first calls the flowtable type's `setup` callback, [`nf_flow_table_offload_setup()`][setup] ([nf_tables_api.c:9068][apisetup]). Without `flags offload` it only sets up the XDP path ([nf_flow_table_offload.c:1279-1280][offsetup]); XDP programs can then look the table up with [`bpf_xdp_flow_lookup`][bpf], which therefore sees only flowtables without `flags offload` ([`391bb6594fd3`][c391bb], v6.11). With `flags offload`, if the device has `ndo_setup_tc` the flowtable calls it with `TC_SETUP_FT` ([`nf_flow_table_offload_cmd()`][offcmd]); otherwise it uses the indirect-block path `flow_indr_dev_setup_offload()` ([`nf_flow_table_indr_offload_cmd()`][indrcmd]). The driver's callback is added to the flowtable's block ([nf_flow_table_offload.c:1193][splice]). +- **Binding.** Registering a flowtable's ingress hook on a device first calls the flowtable type's `setup` callback, [`nf_flow_table_offload_setup()`][setup] ([nf_tables_api.c:9068][apisetup]). Without `flags offload` it only sets up the XDP path ([nf_flow_table_offload.c:1279-1280][offsetup]); XDP programs can then look the table up with [`bpf_xdp_flow_lookup`][bpf], which therefore sees only flowtables without `flags offload` ([`391bb6594fd3`][c391bb], v6.11). That file is built only with `CONFIG_DEBUG_INFO_BTF` or `CONFIG_DEBUG_INFO_BTF_MODULES` ([Makefile:147-150][mkbpf]), and the lookup returns the first flowtable bound to the device ([nf_flow_table_xdp.c:36-42][xdpft]). With `flags offload`, if the device has `ndo_setup_tc` the flowtable calls it with `TC_SETUP_FT` ([`nf_flow_table_offload_cmd()`][offcmd]); otherwise it uses the indirect-block path `flow_indr_dev_setup_offload()` ([`nf_flow_table_indr_offload_cmd()`][indrcmd]). The driver's callback is added to the flowtable's block ([nf_flow_table_offload.c:1193][splice]). - **Per flow.** [`nf_flow_offload_tuple()`][offtuple] calls every callback with `TC_SETUP_CLSFLOWER` and a `flow_cls_offload` request, the request type tc flower uses. The command is `FLOW_CLS_REPLACE` to add a flow (with a flow rule built by [`nf_flow_rule_route_ipv4()`][rulefn] and [`nf_flow_rule_match()`][rulematch]: match keys plus actions), `FLOW_CLS_DESTROY` or `FLOW_CLS_STATS`. - **In-tree drivers that handle `TC_SETUP_FT`** as of v7.4-rc3: the mlx5 representor code ([rep/tc.c:261][mlx5], [rep/tc.c:675][mlx5ind]), the MediaTek Ethernet and WED drivers ([mtk_ppe_offload.c:674][mtkppe], [mtk_wed.c:2790][mtkwed]), the mt76 Wi-Fi driver ([npu.c:385][mt76]) and the Airoha Ethernet driver ([airoha_eth.c:3674][airoha]). DSA user ports pass the command to the conduit device ([dsa/user.c:1729][dsa]). `netdevsim` returns 0 and registers no callback ([netdevsim/tc.c:92][netdevsim]). Behaviour with `flags offload`, from the source: -- If the driver rejects `TC_SETUP_FT` ([`nf_flow_table_offload_cmd()`][offcmd]), or no indirect-block driver binds (`-EOPNOTSUPP`, [flow_offload.c:628][flowoffeop]), creating the flowtable or adding the device to it fails: the `setup` error is returned before the hook is registered ([nf_tables_api.c:9068][apisetup], [nf_tables_api.c:9104-9106][apiregall]). `netdevsim` returns 0 and registers no callback, so with it the flowtable loads and no flow is added in hardware ([`flow_offload_rule_add()`][ruleadd] returns `-ENOENT` when nothing was added). -- A device that registers later is handled by the netdev notifier, which returns `NOTIFY_BAD` when registering the hook fails ([nf_tables_api.c:9908-9913][apinotif2]); `register_netdevice()` then unregisters the device ([dev.c:11778][devreg]). A change of the `offload` flag on an existing flowtable is rejected with `-EOPNOTSUPP` ([nf_tables_api.c:9200-9203][apitoggle]). -- A direction that goes through a valid Ethernet device uses direct mode ([nf_flow_table_path.c:194-196][pathhw]), so a direction without a tunnel caches no route. The IPsec direction still caches its route ([nf_flow_table_core.c:143-147][corexfrm]). +- If the driver rejects `TC_SETUP_FT` ([`nf_flow_table_offload_cmd()`][offcmd]), or no indirect-block driver binds (`-EOPNOTSUPP`, [flow_offload.c:628][flowoffeop]), creating the flowtable or adding the device to it fails: the `setup` error is returned before the hook is registered ([nf_tables_api.c:9068][apisetup], [nf_tables_api.c:9104-9106][apiregall]). With a driver that accepts `TC_SETUP_FT` but registers no callback, such as `netdevsim`, the flowtable loads and no flow is added in hardware ([`flow_offload_rule_add()`][ruleadd] returns `-ENOENT` when nothing was added). +- A device that registers later is handled by the netdev notifier, which returns `NOTIFY_BAD` when registering the hook fails ([nf_tables_api.c:9908-9913][apinotif2]); `register_netdevice()` then unregisters the device ([dev.c:11778-11785][devreg]). A change of the `offload` flag on an existing flowtable is rejected with `-EOPNOTSUPP` ([nf_tables_api.c:9200-9203][apitoggle]). +- A direction that goes through a valid Ethernet device uses direct mode ([nf_flow_table_path.c:194-196][pathhw]), which caches no route unless the direction has a tunnel. A direction through a device that isn't a valid Ethernet device stays in neighbour mode, and the IPsec direction does too; both cache their route ([nf_flow_table_core.c:143-147][corexfrm]). - A hardware add that fails isn't reported. The flow stays in software with `NF_FLOW_HW` set, `[HW_OFFLOAD]` is set if the driver accepted at least one direction ([`flow_offload_rule_add()`][ruleadd], [`flow_offload_work_add()`][workadd]), and the add is queued again on later software hits, at most once a second and not for closing flows ([nf_flow_table_core.c:373-383][corelimit]). - Flows through an IPIP or IPv6-in-IPv6 tunnel stay in software ([`nf_flow_offload_unsupported()`][unsup]; [`6c5dcab95f4c`][c6c5d], v7.2). - The collector skips an entry whose hardware work is still queued ([nf_flow_table_core.c:581][gcpending]). @@ -195,13 +198,13 @@ Behaviour with `flags offload`, from the source: ## Inspecting - **Conntrack listing.** `conntrack -L` shows `[OFFLOAD]` for software flows and `[HW_OFFLOAD]` for hardware flows ([nf_flowtable.rst:111-113][rst111], [nf_flowtable.rst:202-206][rst202]). `/proc/net/nf_conntrack` (with `CONFIG_NF_CONNTRACK_PROCFS`, [nf_conntrack_standalone.c:34][kcprocfs]) prints the same tags ([nf_conntrack_standalone.c:364-366][proctag]). -- **Checking that a flow was added.** The `[OFFLOAD]` tag in `conntrack -L` is how the kernel documentation says to identify offloaded flows ([nf_flowtable.rst:111-113][rst111]); the page has no other signal to offer, since there are no hit or miss counters. A flow without the tag was not added; the reasons are listed in [How a flow gets in](#how-a-flow-gets-in). +- **Checking that a flow was added.** The `[OFFLOAD]` tag in `conntrack -L` is how the kernel documentation says to identify offloaded flows, and `[HW_OFFLOAD]` marks hardware flows ([nf_flowtable.rst:111-113][rst111]). A flow without either tag is not in the table: it was never added (the reasons are in [How a flow gets in](#how-a-flow-gets-in)), or it has been torn down. - **What offloaded entries omit.** ctnetlink skips the timeout and protocol info ([nf_conntrack_netlink.c:579][nlnk]), and `/proc/net/nf_conntrack` skips the timeout and the TCP state ([nf_conntrack_standalone.c:339][procnf], [`tcp_print_conntrack()`][tcpprint]). - **Statistics.** With `CONFIG_NF_FLOW_TABLE_PROCFS` ([Kconfig:721][kconfig]), [`/proc/net/stat/nf_flowtable`][procfs] shows per-CPU counters of pending hardware add, delete and stats work; the file is created at [nf_flow_table_procfs.c:71][procfscreate] and its show function prints only those three columns. They are incremented when work is queued and decremented when it completes ([nf_flow_table_offload.c:1047-1077][statinc]). There are no hit or miss counters. ## Limits -- **Rule changes.** Removing a rule runs the expression's `deactivate` and `destroy` callbacks, which release the expression's use of the flowtable and its conntrack hook registration for the netns, and don't touch flow entries ([nft_flow_offload.c:185-206][deact]). A hit never reaches the forward chain, and a software hit pushes the deadline out ([`flow_offload_refresh()`][refresh]). Only the stored NAT rewrite is applied to a hit ([`nf_flow_offload_forward()`][forward]). So a flow already in the table keeps being forwarded after the `flow add` rule is removed or a drop rule is added to the prerouting, forward or postrouting chains, as long as packets keep arriving; hardware flows get their deadline from driver statistics instead ([`flow_offload_work_stats()`][hwstatwork]). The ways a flow leaves are listed in [How a flow leaves](#how-a-flow-leaves). +- **Rule changes.** Removing a rule runs the expression's `deactivate` and `destroy` callbacks, which release the expression's use of the flowtable and its conntrack hook registration for the netns, and don't touch flow entries ([nft_flow_offload.c:185-206][deact]). A hit never reaches the forward chain, and a software hit pushes the deadline out ([`flow_offload_refresh()`][refresh]). Only the stored NAT rewrite is applied to a hit ([`nf_flow_offload_forward()`][forward]). So a flow already in the table keeps being forwarded after the `flow add` rule is removed or a drop rule is added to the prerouting or forward chains, or to postrouting for a flow without IPsec, as long as packets keep arriving; hardware flows get their deadline from driver statistics instead ([`flow_offload_work_stats()`][hwstatwork]). The ways a flow leaves are listed in [How a flow leaves](#how-a-flow-leaves). - **Cache.** The kernel documentation says the table behaves like a cache and that entries might get stale if the destination MAC address or the egress device changes, which "might be a problem" with software mode combined with bridging and IP forwarding, and with hardware offload ([nf_flowtable.rst:214-222][rst214]). - **IPsec.** A packet that carries a security path never triggers an add ([`nft_flow_offload_skip()`][skip]). In the direction whose route has an IPsec transform the device-list and neighbour checks are skipped ([nf_flow_table_path.c:314-320][pathskip]); the other direction gets them like any other flow. - **ip6gre.** The commit that extended the GRE timeout, [`46ecda278f6e`][c46ecd], says `ip6gre` tunnels with the default encapsulation limit carry a Destination Options header before GRE, which the flowtable doesn't parse, so every packet of the flow takes the classic path. @@ -219,7 +222,7 @@ Hardware offload reached the tree in v5.5 through the tc flow-block interface ra **GRE timeout history.** -- [`03428ca5cee9`][c0342] (v6.14) reworked the conntrack timeout extension. For GRE it fell into the default branch, which set `new_timeout = true` (one jiffy) and hit `WARN_ON_ONCE()` once a GRE entry's timeout fell below five minutes. +- [`03428ca5cee9`][c0342] (v6.14) reworked the conntrack timeout extension. For GRE it fell into the default branch, which left `new_timeout` at its initial value `true` (one jiffy) and hit `WARN_ON_ONCE()` once a GRE entry's timeout fell below five minutes. - The extension never took effect until [`53b3e60edb67`][c53b3] (v7.2): it compared a relative value with the absolute `ct->timeout`, so the update never succeeded. Once it did, the default branch's one-jiffy timeout applied to GRE. - [`46ecda278f6e`][c46ecd] (v7.4-rc3) says "the default branch sets the new timeout to 1 jiffy, so the conntrack entry expires right away" and extends GRE like UDP. The packets that bring a GRE entry's timeout down are those that still go through conntrack; the commit message gives packets with a TTL of 1 or less as an example. - Both fixes carry `Cc: stable@vger.kernel.org`. @@ -230,7 +233,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, - It says TCP RST and FIN packets are passed up to the classic path ([nf_flowtable.rst:77-78][rst77]). Since [`fdbaf5163331`][cfdbaf] ("netfilter: flowtable: add CLOSING state", v6.14), they stay on the fast path and the flow enters the closing state. The commit message explains that the old behaviour "leads to packet races when restoring the conntrack state as well as late flow re-offload situations". - It says hits are sent with `neigh_xmit()` ([nf_flowtable.rst:21][rst21], [nf_flowtable.rst:74][rst74]). Direct mode has called `dev_queue_xmit()` since [`7a27f6ab4135`][c7a27] (v5.13, the same day as the documentation update), and [`b5964aac51e0`][cb596] ("netfilter: flowtable: consolidate xmit path", v6.19) moved neighbour mode to it too. -- It says packets are not seen "from any of the Netfilter hooks coming after ingress" ([nf_flowtable.rst:22-23][rst22]). The netfilter egress hook, added in [`42df6e1d221d`][c42df] (v5.16, after the documentation was written), does see software hits, because they end in `dev_queue_xmit()`. An IPsec hit also runs postrouting, because it goes out through `dst_output()`. +- It says packets are not seen "from any of the Netfilter hooks coming after ingress" ([nf_flowtable.rst:22-23][rst22]). The netfilter egress hook, added in [`42df6e1d221d`][c42df] (v5.16, after the documentation was written), does see software hits that aren't IPsec flows, because they end in `dev_queue_xmit()`. An IPsec hit also runs postrouting, because it goes out through `dst_output()`, which it has done since [`589b474a4b7c`][c589b] (v5.3), before the documentation was last updated. - It says the rule adds the flow on "the TCP syn-ack packet" ([nf_flowtable.rst:105-106][rst105]). The code requires an established and assured conntrack entry ([`nf_conntrack_tcp_established()`][tcpest]). [`e5eaac2beb54`][ce5ea] (v5.18) added that check, and its message says the patch makes `nft_flow_offload` "skip packets in SYN_RECV state" and "postpones offload up to the next packet". - It lists TCP and UDP as the supported layer 4 protocols ([nf_flowtable.rst:10][rst10]). GRE has also been accepted since [`4e8d9584d154`][c4e8d] (v5.18). @@ -270,10 +273,13 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [c46ecd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=46ecda278f6eedffb2898d41e3e56757c996a41f [c4e8d]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4e8d9584d154479d357327f76d4e49486915c9c9 [c53b3]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=53b3e60edb674b442b2b3bbdba484667b0f47a5d +[c589b]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=589b474a4b7ce409d6821ef17234a995841bd131 [c6c5d]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6c5dcab95f4cd42a1648739ec9300fbb4b1a021f +[c73319]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=73319a8ee18b9cf0b2dac87f8521595e0381ba0c [c794d]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=794d9b25817a813f1d2fb8e133ba6d2f53920853 [c7a27]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7a27f6ab41356ecba47ec2bec6d635704c169779 [c871df]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=871df5007edaacaf2eac58a2871af6044a444ad4 +[ca331]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a331b78a552551d0e404e58e6390b1c828d6af8f [ca3c9]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a3c90f7a2323b331ae816d5b0633e68148e25d04 [cab427]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ab427db17885814069bae891834f20842f0ac3a4 [cac2a]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ac2a66665e231847cab11b8c8e844ce43207dd2e @@ -336,6 +342,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [inet68]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_inet.c?h=v7.4-rc3#n68 [ip6dec]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1145 [ip6ext]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1050 +[ip6fwd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv6/ip6_output.c?h=v7.4-rc3#n612 [ip6hook]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1233 [ip6ip6]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n356 [ip6ttl]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1069 @@ -352,6 +359,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [lwn1]: https://lwn.net/Articles/738214/ [lwn2]: https://lwn.net/Articles/742164/ [macstore]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n137 +[mkbpf]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/Makefile?h=v7.4-rc3#n147 [mlx5]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mellanox/mlx5/core/en/rep/tc.c?h=v7.4-rc3#n261 [mlx5ind]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mellanox/mlx5/core/en/rep/tc.c?h=v7.4-rc3#n675 [mt76]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/wireless/mediatek/mt76/npu.c?h=v7.4-rc3#n385 @@ -359,6 +367,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [mtkwed]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/mediatek/mtk_wed.c?h=v7.4-rc3#n2790 [mtu]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n289 [neigh4]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/route.h?h=v7.4-rc3#n408 +[neigh6]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1204 [neighdir]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n885 [neigherr]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n870 [netdevsim]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/netdevsim/tc.c?h=v7.4-rc3#n92 @@ -373,6 +382,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [pathbr]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n187 [pathdev]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n198 [pathhw]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n194 +[pathin]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n241 [pathnud]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n61 [pathppp]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n155 [pathrev]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n305 @@ -384,6 +394,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [qxmit]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n822 [refresh]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n367 [refreshacct]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_core.c?h=v7.4-rc3#n2089 +[refreshfix]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_core.c?h=v7.4-rc3#n2094 [route]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n272 [route2984]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/route.c?h=v7.4-rc3#n2984 [routefl]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n280 @@ -433,12 +444,14 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [v6route]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv6/netfilter.c?h=v7.4-rc3#n102 [validate]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n138 [workadd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n973 +[xdpft]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_xdp.c?h=v7.4-rc3#n36 [xfrm]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n300 [xfrm4]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/xfrm4_output.c?h=v7.4-rc3#n33 [xfrm6]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv6/xfrm6_output.c?h=v7.4-rc3#n114 [xfrmdirect]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/xfrm/xfrm_output.c?h=v7.4-rc3#n786 [xfrmlo]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/xfrm/xfrm_output.c?h=v7.4-rc3#n600 [xfrmpost]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/xfrm/xfrm_output.c?h=v7.4-rc3#n608 +[xfrmpost2]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/xfrm/xfrm_output.c?h=v7.4-rc3#n604 [xmit]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n776 [xmit4]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n838 [xmittype]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n19 From 8757f5c46a02d5494d75bd88df34dec16de00578 Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 20:21:00 -0800 Subject: [PATCH 15/22] net: scope flowtable statements per direction and trim IPsec edge cases State IPsec, route caching and postrouting per direction, correct the hook-registration and XDP commit attributions, note that a flow with an IPsec direction is not added in hardware, and fix the lifecycle diagram. --- docs/net/flowtable.md | 69 +++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index 5419af26..cc301388 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -27,8 +27,8 @@ The diagram is simplified from Fig. 1 of the kernel documentation ([nf_flowtable Which hooks a hit passes, from the source: - The tc ingress hook runs before the netfilter ingress hook where the flowtable sits ([dev.c:6290][dev6290], [dev.c:6298][dev6298]). -- A software hit that isn't an IPsec flow is transmitted with `dev_queue_xmit()` ([`__nf_flow_queue_xmit()`][xmit]), which runs the netfilter egress hook and tc egress ([dev.c:5024][dev5024], [dev.c:5032][dev5032]). -- A hit whose route has an IPsec transform is transmitted with `dst_output()` ([`nf_flow_xmit_xfrm()`][xfrm]), which runs the postrouting hook ([xfrm4_output.c:33][xfrm4], [xfrm6_output.c:114][xfrm6]). After the transform, `xfrm_output_resume()` runs the local-out hook ([xfrm_output.c:600][xfrmlo]) and postrouting again for the encapsulated packet: with a single transform it comes from `dst_output()` ([xfrm_output.c:604-605][xfrmpost2]), and the `nf_hook()` call handles a nested transform ([xfrm_output.c:608][xfrmpost]). IPsec packet offload in tunnel mode goes out through `xfrm_dev_direct_output()` instead ([xfrm_output.c:786][xfrmdirect]). +- A software hit in a direction without an IPsec transform is transmitted with `dev_queue_xmit()` ([`__nf_flow_queue_xmit()`][xmit]), which runs the netfilter egress hook and tc egress ([dev.c:5024][dev5024], [dev.c:5032][dev5032]). +- A hit in a direction whose route has an IPsec transform is transmitted with `dst_output()` ([`nf_flow_xmit_xfrm()`][xfrm]), which runs the postrouting hook ([xfrm4_output.c:33][xfrm4], [xfrm6_output.c:114][xfrm6]). The transform itself is applied in `xfrm_output_resume()`, which runs the local-out hook ([xfrm_output.c:600][xfrmlo]). The kernel documentation says "you do not see these packets from any of the Netfilter hooks coming after ingress" ([nf_flowtable.rst:22-23][rst22]). The source differs; see [Differences](#differences-from-nf_flowtablerst). @@ -52,12 +52,13 @@ table inet x { ``` - **The statement.** `flow add @f` is evaluated by [`nft_flow_offload_eval()`][eval]. It is accepted only in a chain on the forward hook ([`nft_flow_offload_validate()`][validate]). The example rule matches IPv4 TCP. -- **What a hit skips.** A hit is consumed with `NF_STOLEN` ([`nf_flow_queue_xmit()`][qxmit]), so the later packets of a flow that hit the table don't reach the prerouting and forward chains, and reach postrouting only for IPsec hits (above). The kernel documentation says the `counter` rule stops being updated ([nf_flowtable.rst:105-109][rst105]). +- **What a hit skips.** A hit is consumed with `NF_STOLEN` ([`nf_flow_queue_xmit()`][qxmit], [`nf_flow_xmit_xfrm()`][xfrm]), so the later packets of a flow that hit the table don't reach the prerouting and forward chains, and reach postrouting only in a direction with an IPsec transform (above). The kernel documentation says the `counter` rule stops being updated ([nf_flowtable.rst:105-109][rst105]). - **Rule evaluation.** An earlier rule that accepts or drops the packet ends evaluation of the base chain, including any chain it jumped to ([nf_tables_core.c:306-312][nftverdict]), so `flow add` never runs for it in that chain. `flow add` returns no verdict when it adds the flow. On every other path it sets `NFT_BREAK` ([`nft_flow_offload_eval()`][eval]), which skips the rest of that rule and continues with the next rule ([nf_tables_core.c:292-295][nftcore]). So statements after `flow add` in the same rule run only for the packet that adds the flow. - **The `counter` flag.** Software hits update conntrack's counters only if the flowtable has the `counter` flag ([nf_flow_table_ip.c:520][ctcount]; documented in [nf_flowtable.rst:165-181][rst165]). The update reaches only conntrack entries that have the accounting extension ([`nf_ct_acct_add()`][acctfn]), which is added when the entry is created and only if the sysctl `net.netfilter.nf_conntrack_acct` is set ([nf_conntrack_acct.h:36][acct], [nf_conntrack_core.c:1789][acctext], [sysctl][acctsysctl]). - **Priority.** The kernel documentation says to "make sure the flowtable priority is smaller than the nftables ingress chain hence the flowtable runs before in the pipeline" ([nf_flowtable.rst:99-103][rst99]). A hit returns `NF_STOLEN`, which ends hook traversal ([`nf_hook_slow()`][nfstolen]), so an ingress chain that runs after the flowtable doesn't see it. - **Device list.** Path resolution follows VLAN, PPPoE and bridge devices down to the real device ([`nft_dev_fill_forward_path()`][fillpath]) and requires that device to be in the flowtable's list ([nf_flow_table_path.c:198][pathdev]). The check runs for both directions and is skipped for IPsec routes ([nf_flow_table_path.c:314-320][pathskip]). The kernel documentation says the real device is sufficient and the VLAN and PPPoE devices need not be listed ([nf_flowtable.rst:129-130][rst129]). -- **Since v7.2** ([`871df5007eda`][c871df]), a failed path discovery or a device that isn't in the flowtable makes `flow add` give up. The commit message says "If forward path discovery fails for any reason or netdevice is not registered for this flowtable, then bail out to classic forwarding path rather than providing incomplete forwarding path." A device name in the flowtable that matches no existing device is accepted and registers no hook until a device with that name appears ([nf_tables_api.c:2456][hookalloc]; since v6.16, [`73319a8ee18b`][c73319] and [`a331b78a5525`][ca331]). + Since v7.2 ([`871df5007eda`][c871df]), a failed path discovery or a device that isn't in the flowtable makes `flow add` give up. The commit message says "If forward path discovery fails for any reason or netdevice is not registered for this flowtable, then bail out to classic forwarding path rather than providing incomplete forwarding path." +- **Missing devices.** A device name in the flowtable that matches no existing device is accepted ([nf_tables_api.c:2456][hookalloc]; since v6.16, [`6d07a289504a`][c6d07]) and registers no hook until a device with that name appears ([`a331b78a5525`][ca331]). Two flowtable options appear in the kernel documentation: `counter` ([nf_flowtable.rst:165-181][rst165]) and `flags offload` ([nf_flowtable.rst:183-196][rst183]): @@ -108,15 +109,15 @@ Each direction of a flow has a transmit type chosen at add time ([`nft_xmit_type - **Direct.** The entry stores the MAC addresses ([nf_flow_table_core.c:137-141][macstore]). The path code picks it for PPPoE paths ([nf_flow_table_path.c:155][pathppp]), bridge paths ([nf_flow_table_path.c:187][pathbr]), and Ethernet devices when hardware offload is on ([nf_flow_table_path.c:194-196][pathhw]). - **IPsec (xfrm).** Chosen for a route that has an IPsec transform. The hit goes out through `dst_output()` ([`nf_flow_xmit_xfrm()`][xfrm]). -A flow caches its route in the entry, except in direct mode without a tunnel, where the route is released ([nf_flow_table_core.c:129-136][coredst]). +Each direction caches its route in the entry, except in direct mode without a tunnel, where the route is released ([nf_flow_table_core.c:129-136][coredst]). ## What a hit does [`nf_flow_offload_ip_hook()`][iphook] looks up the packet's tuple. A miss returns `NF_ACCEPT` and the packet follows the classic path. On a hit, [`nf_flow_offload_forward()`][forward]: -1. Checks the packet against the MTU recorded for the flow. A packet that is too big goes to the classic path ([`nf_flow_exceeds_mtu()`][mtu]); the kernel documentation gives the reason as the packet-too-big ICMP error ([nf_flowtable.rst:78-79][rst78]). +1. Checks the packet against the MTU recorded for the packet's direction. A packet that is too big goes to the classic path ([`nf_flow_exceeds_mtu()`][mtu]); the kernel documentation gives the reason as the packet-too-big ICMP error ([nf_flowtable.rst:78-79][rst78]). 2. Checks TCP flags ([`nf_flow_state_check()`][state]; see [How a flow leaves](#how-a-flow-leaves)). -3. Checks the cached route, if the flow keeps one ([`nf_flow_dst_check()`][dstchk] returns true when there is none). A stale route tears the flow down and the packet goes to the classic path. Direct-mode flows without a tunnel keep no route, so no route change is noticed. In the bridge case, the commit that added direct mode, [`7a27f6ab4135`][c7a27], says "if peer is moved to different bridge port, the connection will time out". +3. Checks the cached route, if the flow keeps one ([`nf_flow_dst_check()`][dstchk] returns true when there is none). A stale route tears the flow down and the packet goes to the classic path. A direction in direct mode without a tunnel keeps no route, so the hook notices no route change for it ([nf_flow_table_core.c:129-136][coredst]); the collector checks the routes of both directions ([`nf_flow_offload_gc_step()`][gcstep]). In the bridge case, the commit that added direct mode, [`7a27f6ab4135`][c7a27], says "if peer is moved to different bridge port, the connection will time out". 4. Refreshes the deadline ([`flow_offload_refresh()`][refresh]), strips any VLAN, PPPoE or IPIP tunnel header (IPIP since v6.19, [`ab427db17885`][cab427]), applies the stored NAT rewrite, decrements the TTL, and updates conntrack's counters if the flowtable has `counter`. The hook then transmits as described in [Transmit modes](#transmit-modes): [`nf_flow_queue_xmit4()`][xmit4] pushes any egress VLAN, PPPoE or tunnel header back on, builds the link-layer header with `dev_hard_header()` and calls `dev_queue_xmit()`. IPsec hits skip that and go out through `dst_output()`. @@ -130,10 +131,10 @@ Packets that the hook doesn't take go to the classic path: fragments and packets The garbage collector, [`nf_flow_offload_work_gc()`][gcwork], re-queues itself one second after each run ([nf_flow_table_core.c:626][gcrequeue]) and removes entries that have been torn down. Teardown is triggered by: - **A timeout.** Each entry has a deadline. A software hit pushes it out, at most once a second ([nf_flow_table_core.c:373][corelimit]); this also applies to flows that are closing (the refresh writes the deadline before it checks for `NF_FLOW_CLOSING`). The default is 30 seconds for TCP ([nf_conntrack_proto_tcp.c:1658][tcpoff]) and UDP ([nf_conntrack_proto_udp.c:204][udpoff]), set by the sysctls `net.netfilter.nf_flowtable_tcp_timeout` and `net.netfilter.nf_flowtable_udp_timeout` ([nf_conntrack_standalone.c:789][sysctltcp], [nf_conntrack_standalone.c:839][sysctludp]), and 30 seconds for GRE, which has no sysctl ([`flow_offload_get_timeout()`][gettimeout], [nf_flow_table.h:209][flowtimeout]). For hardware flows, the collector asks the driver for statistics once less than 90% of the timeout remains and extends the deadline from the last-used time ([`nf_flow_offload_stats()`][hwstats], [`flow_offload_work_stats()`][hwstatwork]). -- **A dying conntrack entry**, checked by `nf_ct_is_dying()` ([`nf_flow_offload_gc_step()`][gcstep]); lookups also miss ([`flow_offload_lookup()`][lookup]). +- **A dying conntrack entry**, checked by `nf_ct_is_dying()` ([`nf_flow_offload_gc_step()`][gcstep]); software lookups also miss ([`flow_offload_lookup()`][lookup]). - **A stale route**, for flows that keep one. The collector checks this since v7.3 ([`edd51a233438`][cedd51]); the hook checks it too ([`nf_flow_offload_forward()`][forward]). - **A missing output device, or a neighbour lookup error.** The hook tears the flow down and drops that packet ([nf_flow_table_ip.c:870-880][neigherr] in neighbour mode, [nf_flow_table_ip.c:885-889][neighdir] in direct mode for a missing device; the IPv6 lookup is [`ip_neigh_gw6()`][neigh6]). A missing neighbour entry is created by the lookup ([`ip_neigh_gw4()`][neigh4]), so only a failure to create it counts. -- **A device going down.** The `NETDEV_DOWN` notifier ([nft_flow_offload.c:248][notifier]) calls [`nf_flow_table_cleanup()`][cleanup], which tears down the flows whose input device in either direction is that device ([`nf_flow_table_do_cleanup()`][docleanup]); a VLAN, PPPoE or bridge device going down doesn't match, because the recorded input device is the real one ([nf_flow_table_path.c:241][pathin]). +- **A device going down.** The `NETDEV_DOWN` notifier ([nft_flow_offload.c:248][notifier]) calls [`nf_flow_table_cleanup()`][cleanup], which tears down the flows whose input device in either direction is that device ([`nf_flow_table_do_cleanup()`][docleanup]); a VLAN, PPPoE or bridge device going down doesn't match, because the recorded input device is the real one ([nf_flow_table_path.c:241][pathin]), except opposite an IPsec direction, where the route's own device is recorded as is ([nf_flow_table_path.c:31][pathdef]). - **Deleting the flowtable** ([`nf_flow_table_free()`][free]). ``` @@ -144,20 +145,23 @@ The garbage collector, [`nf_flow_offload_work_gc()`][gcwork], re-queues itself o │ │ │ └── TCP FIN or RST ──► closing (hits continue) │ │ - └───────────────┬──────────────┘ - ▼ - torn down ◄── timeout, dying conntrack entry, stale route, - │ missing output device or neighbour error, - │ device down, flowtable deleted, SYN on a closing flow - │ lookups miss, conntrack entry fixed up, offload bit cleared + ├──────────────────────────────┘ + │ timeout, dying conntrack entry, stale route, + │ missing output device or neighbour error, SYN on a closing flow, + │ device down, flowtable deleted ▼ - removed by the garbage collector on its next run (about a second); - hardware flows are removed after the driver delete completes + torn down: software lookups miss, TCP and UDP conntrack entry fixed up, + offload bit cleared + │ + ▼ + removed by the garbage collector: in the same run if it did the teardown, + otherwise on its next run (about a second); hardware flows after the driver + delete completes; at once for device down and flowtable deletion ``` **TCP FIN and RST** don't end the flow at once (since v6.14, [`fdbaf5163331`][cfdbaf]). [`nf_flow_state_check()`][state] marks the flow `NF_FLOW_CLOSING` and the packet still takes the fast path; [`nf_flow_offload_gc_step()`][gcstep] deletes the hardware rule early and the flow stays in the software table. Software hits keep pushing the deadline, so the flow leaves when the timeout expires after the last packet, and [`flow_offload_fixup_ct()`][fixup] then gives conntrack the close state and timeout. A later SYN on a closing flow tears it down at once, sets the conntrack state to close and sends that packet to the classic path ([`nf_flow_state_check()`][state], [`flow_offload_fixup_tcp()`][fixtcp]). -**Adding again.** [`flow_offload_teardown()`][tear] clears the offload bit on the conntrack entry ([nf_flow_table_core.c:401][coreclear]), so a later forward-chain packet that matches the `flow add` rule can add the flow again, if it passes the checks in [How a flow gets in](#how-a-flow-gets-in) again. A TCP flow torn down by a SYN is in the close state, so it isn't added again until it is established. +**Adding again.** [`flow_offload_teardown()`][tear] clears the offload bit on the conntrack entry ([nf_flow_table_core.c:401][coreclear]), so a later forward-chain packet that matches the `flow add` rule can add the flow again, if it passes the checks in [How a flow gets in](#how-a-flow-gets-in) again. A closing TCP flow gets the close state at teardown ([nf_flow_table_core.c:218-221][closestate]), so it isn't added again until it is established. **Conntrack timeout.** While a flow is in the table, conntrack may see few or none of its packets; the comment in [`nf_flow_table_extend_ct_timeout()`][ext] says "nf_conntrack might not see any packets anymore" ([nf_flow_table_core.c:506-513][gccomment]). [`flow_offload_add()`][add] first sets the conntrack timeout to a day ([nf_flow_table_core.c:358][coreday]). Afterwards the collector calls [`nf_flow_table_extend_ct_timeout()`][ext], which sets a new timeout when less than five minutes remain. The value depends on the protocol and TCP state ([`nf_flow_table_tcp_timeout()`][tcpto]): @@ -173,7 +177,7 @@ The comment in [`nf_flow_table_extend_ct_timeout()`][ext] calls the value "an ar **Hand-back to conntrack.** [`flow_offload_fixup_ct()`][fixup] runs at teardown. - **TCP state.** It keeps the last conntrack state, or sets close if the flow was closing, and clears the recorded maximum window and the max-ack flag; the code comment says that otherwise conntrack's TCP reset validation would fail ([`flow_offload_fixup_tcp()`][fixtcp]). -- **Timeout.** TCP and UDP flows have the conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired; a closing TCP flow gets the close timeout unconditionally ([nf_flow_table_core.c:250-252][corefix]). Except for that case the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64][tcp5d]), keeps the timeout it has left, at most a day, until a packet goes through conntrack again. Conntrack then refreshes the timeout to the established value, unless the entry has a fixed timeout ([`__nf_ct_refresh_acct()`][refreshacct], [nf_conntrack_core.c:2094-2096][refreshfix]). +- **Timeout.** TCP and UDP flows have the conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired; a closing TCP flow gets the close timeout unconditionally ([nf_flow_table_core.c:250-252][corefix]). Except for that case the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64][tcp5d]), keeps the timeout it has left, at most a day, until a packet goes through conntrack again. Conntrack then sets the timeout for the TCP state of that packet ([nf_conntrack_proto_tcp.c:1321][tcpstate]), unless the entry has a fixed timeout ([`__nf_ct_refresh_acct()`][refreshacct], [nf_conntrack_core.c:2094-2096][refreshfix]). - **GRE.** GRE flows return before any timeout change ([nf_flow_table_core.c:240-241][coregre]). ## Hardware offload @@ -182,15 +186,16 @@ The kernel documentation says a flowtable can offload flows to a device that sup The driver interface is the tc flow-block interface: -- **Binding.** Registering a flowtable's ingress hook on a device first calls the flowtable type's `setup` callback, [`nf_flow_table_offload_setup()`][setup] ([nf_tables_api.c:9068][apisetup]). Without `flags offload` it only sets up the XDP path ([nf_flow_table_offload.c:1279-1280][offsetup]); XDP programs can then look the table up with [`bpf_xdp_flow_lookup`][bpf], which therefore sees only flowtables without `flags offload` ([`391bb6594fd3`][c391bb], v6.11). That file is built only with `CONFIG_DEBUG_INFO_BTF` or `CONFIG_DEBUG_INFO_BTF_MODULES` ([Makefile:147-150][mkbpf]), and the lookup returns the first flowtable bound to the device ([nf_flow_table_xdp.c:36-42][xdpft]). With `flags offload`, if the device has `ndo_setup_tc` the flowtable calls it with `TC_SETUP_FT` ([`nf_flow_table_offload_cmd()`][offcmd]); otherwise it uses the indirect-block path `flow_indr_dev_setup_offload()` ([`nf_flow_table_indr_offload_cmd()`][indrcmd]). The driver's callback is added to the flowtable's block ([nf_flow_table_offload.c:1193][splice]). -- **Per flow.** [`nf_flow_offload_tuple()`][offtuple] calls every callback with `TC_SETUP_CLSFLOWER` and a `flow_cls_offload` request, the request type tc flower uses. The command is `FLOW_CLS_REPLACE` to add a flow (with a flow rule built by [`nf_flow_rule_route_ipv4()`][rulefn] and [`nf_flow_rule_match()`][rulematch]: match keys plus actions), `FLOW_CLS_DESTROY` or `FLOW_CLS_STATS`. +- **Binding.** Registering a flowtable's ingress hook on a device first calls the flowtable type's `setup` callback, [`nf_flow_table_offload_setup()`][setup] ([nf_tables_api.c:9068][apisetup]). Without `flags offload` it only sets up the XDP path ([nf_flow_table_offload.c:1279-1280][offsetup]); With `flags offload`, if the device has `ndo_setup_tc` the flowtable calls it with `TC_SETUP_FT` ([`nf_flow_table_offload_cmd()`][offcmd]); otherwise it uses the indirect-block path `flow_indr_dev_setup_offload()` ([`nf_flow_table_indr_offload_cmd()`][indrcmd]). The driver's callback is added to the flowtable's block ([nf_flow_table_offload.c:1193][splice]). +- **XDP lookup.** XDP programs can look a flowtable up with [`bpf_xdp_flow_lookup`][bpf] ([`391bb6594fd3`][c391bb], v6.11). It sees only flowtables without `flags offload` ([`89cc8f1c5f22`][c89cc], v6.11), returns the first flowtable bound to the device ([nf_flow_table_xdp.c:36-42][xdpft]), and `nf_flow_table_bpf.c` is built with `CONFIG_DEBUG_INFO_BTF` when the flowtable is built in and `CONFIG_DEBUG_INFO_BTF_MODULES` when it is a module ([Makefile:147-150][mkbpf]). +- **Per flow.** [`nf_flow_offload_tuple()`][offtuple] calls every callback with `TC_SETUP_CLSFLOWER` and a `flow_cls_offload` request, the request type tc flower uses. The command is `FLOW_CLS_REPLACE` to add a flow (with a flow rule built by [`nf_flow_rule_route_ipv4()`][rulefn], or the IPv6 and inet equivalents named in [nf_flow_table_inet.c:68-96][inet68], with [`nf_flow_rule_match()`][rulematch]: match keys plus actions), `FLOW_CLS_DESTROY` or `FLOW_CLS_STATS`. - **In-tree drivers that handle `TC_SETUP_FT`** as of v7.4-rc3: the mlx5 representor code ([rep/tc.c:261][mlx5], [rep/tc.c:675][mlx5ind]), the MediaTek Ethernet and WED drivers ([mtk_ppe_offload.c:674][mtkppe], [mtk_wed.c:2790][mtkwed]), the mt76 Wi-Fi driver ([npu.c:385][mt76]) and the Airoha Ethernet driver ([airoha_eth.c:3674][airoha]). DSA user ports pass the command to the conduit device ([dsa/user.c:1729][dsa]). `netdevsim` returns 0 and registers no callback ([netdevsim/tc.c:92][netdevsim]). Behaviour with `flags offload`, from the source: - If the driver rejects `TC_SETUP_FT` ([`nf_flow_table_offload_cmd()`][offcmd]), or no indirect-block driver binds (`-EOPNOTSUPP`, [flow_offload.c:628][flowoffeop]), creating the flowtable or adding the device to it fails: the `setup` error is returned before the hook is registered ([nf_tables_api.c:9068][apisetup], [nf_tables_api.c:9104-9106][apiregall]). With a driver that accepts `TC_SETUP_FT` but registers no callback, such as `netdevsim`, the flowtable loads and no flow is added in hardware ([`flow_offload_rule_add()`][ruleadd] returns `-ENOENT` when nothing was added). - A device that registers later is handled by the netdev notifier, which returns `NOTIFY_BAD` when registering the hook fails ([nf_tables_api.c:9908-9913][apinotif2]); `register_netdevice()` then unregisters the device ([dev.c:11778-11785][devreg]). A change of the `offload` flag on an existing flowtable is rejected with `-EOPNOTSUPP` ([nf_tables_api.c:9200-9203][apitoggle]). -- A direction that goes through a valid Ethernet device uses direct mode ([nf_flow_table_path.c:194-196][pathhw]), which caches no route unless the direction has a tunnel. A direction through a device that isn't a valid Ethernet device stays in neighbour mode, and the IPsec direction does too; both cache their route ([nf_flow_table_core.c:143-147][corexfrm]). +- A direction that goes through a valid Ethernet device uses direct mode ([nf_flow_table_path.c:194-196][pathhw]), which caches no route unless the direction has a tunnel. A direction through a device that isn't a valid Ethernet device stays in neighbour mode, and an IPsec direction stays in xfrm mode; both cache their route ([nf_flow_table_core.c:143-147][corexfrm]). A flow with an IPsec direction is not added in hardware: building the rule fails for the xfrm transmit type ([nf_flow_table_offload.c:262-263][hwxfrm], [nf_flow_table_offload.c:873-883][hwalloc]). - A hardware add that fails isn't reported. The flow stays in software with `NF_FLOW_HW` set, `[HW_OFFLOAD]` is set if the driver accepted at least one direction ([`flow_offload_rule_add()`][ruleadd], [`flow_offload_work_add()`][workadd]), and the add is queued again on later software hits, at most once a second and not for closing flows ([nf_flow_table_core.c:373-383][corelimit]). - Flows through an IPIP or IPv6-in-IPv6 tunnel stay in software ([`nf_flow_offload_unsupported()`][unsup]; [`6c5dcab95f4c`][c6c5d], v7.2). - The collector skips an entry whose hardware work is still queued ([nf_flow_table_core.c:581][gcpending]). @@ -198,13 +203,14 @@ Behaviour with `flags offload`, from the source: ## Inspecting - **Conntrack listing.** `conntrack -L` shows `[OFFLOAD]` for software flows and `[HW_OFFLOAD]` for hardware flows ([nf_flowtable.rst:111-113][rst111], [nf_flowtable.rst:202-206][rst202]). `/proc/net/nf_conntrack` (with `CONFIG_NF_CONNTRACK_PROCFS`, [nf_conntrack_standalone.c:34][kcprocfs]) prints the same tags ([nf_conntrack_standalone.c:364-366][proctag]). -- **Checking that a flow was added.** The `[OFFLOAD]` tag in `conntrack -L` is how the kernel documentation says to identify offloaded flows, and `[HW_OFFLOAD]` marks hardware flows ([nf_flowtable.rst:111-113][rst111]). A flow without either tag is not in the table: it was never added (the reasons are in [How a flow gets in](#how-a-flow-gets-in)), or it has been torn down. +- **Checking that a flow was added.** The `[OFFLOAD]` tag in `conntrack -L` is how the kernel documentation says to identify offloaded flows, and `[HW_OFFLOAD]` marks hardware flows ([nf_flowtable.rst:111-113][rst111], [nf_flowtable.rst:202-206][rst202b]). The tag follows `IPS_OFFLOAD_BIT`, which `flow add` sets ([nft_flow_offload.c:105][evalonce2]) and teardown clears ([nf_flow_table_core.c:401][coreclear]). A flow without either tag is not being forwarded by the flowtable: it was never added (the reasons are in [How a flow gets in](#how-a-flow-gets-in)), or it has been torn down. - **What offloaded entries omit.** ctnetlink skips the timeout and protocol info ([nf_conntrack_netlink.c:579][nlnk]), and `/proc/net/nf_conntrack` skips the timeout and the TCP state ([nf_conntrack_standalone.c:339][procnf], [`tcp_print_conntrack()`][tcpprint]). - **Statistics.** With `CONFIG_NF_FLOW_TABLE_PROCFS` ([Kconfig:721][kconfig]), [`/proc/net/stat/nf_flowtable`][procfs] shows per-CPU counters of pending hardware add, delete and stats work; the file is created at [nf_flow_table_procfs.c:71][procfscreate] and its show function prints only those three columns. They are incremented when work is queued and decremented when it completes ([nf_flow_table_offload.c:1047-1077][statinc]). There are no hit or miss counters. ## Limits -- **Rule changes.** Removing a rule runs the expression's `deactivate` and `destroy` callbacks, which release the expression's use of the flowtable and its conntrack hook registration for the netns, and don't touch flow entries ([nft_flow_offload.c:185-206][deact]). A hit never reaches the forward chain, and a software hit pushes the deadline out ([`flow_offload_refresh()`][refresh]). Only the stored NAT rewrite is applied to a hit ([`nf_flow_offload_forward()`][forward]). So a flow already in the table keeps being forwarded after the `flow add` rule is removed or a drop rule is added to the prerouting or forward chains, or to postrouting for a flow without IPsec, as long as packets keep arriving; hardware flows get their deadline from driver statistics instead ([`flow_offload_work_stats()`][hwstatwork]). The ways a flow leaves are listed in [How a flow leaves](#how-a-flow-leaves). +- **Rule changes.** Removing a rule runs the expression's `deactivate` and `destroy` callbacks, which release the expression's use of the flowtable and its conntrack hook registration for the netns, and don't touch flow entries ([nft_flow_offload.c:185-206][deact]). The ways a flow leaves are in [How a flow leaves](#how-a-flow-leaves). +- **Rules a hit doesn't reach.** A hit never reaches the forward chain, and a software hit pushes the deadline out ([`flow_offload_refresh()`][refresh]). Of the ruleset's actions, only the stored NAT rewrite is applied to a hit ([`nf_flow_offload_forward()`][forward]). So a flow already in the table keeps being forwarded after the `flow add` rule is removed or a drop rule is added to the prerouting or forward chains, or to postrouting for a direction without an IPsec transform, as long as packets keep arriving; hardware flows get their deadline from driver statistics instead ([`flow_offload_work_stats()`][hwstatwork]). Packets that miss the table, such as fragments and over-MTU packets, still reach those chains. - **Cache.** The kernel documentation says the table behaves like a cache and that entries might get stale if the destination MAC address or the egress device changes, which "might be a problem" with software mode combined with bridging and IP forwarding, and with hardware offload ([nf_flowtable.rst:214-222][rst214]). - **IPsec.** A packet that carries a security path never triggers an add ([`nft_flow_offload_skip()`][skip]). In the direction whose route has an IPsec transform the device-list and neighbour checks are skipped ([nf_flow_table_path.c:314-320][pathskip]); the other direction gets them like any other flow. - **ip6gre.** The commit that extended the GRE timeout, [`46ecda278f6e`][c46ecd], says `ip6gre` tunnels with the default encapsulation limit carry a Destination Options header before GRE, which the flowtable doesn't parse, so every packet of the flow takes the classic path. @@ -233,7 +239,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, - It says TCP RST and FIN packets are passed up to the classic path ([nf_flowtable.rst:77-78][rst77]). Since [`fdbaf5163331`][cfdbaf] ("netfilter: flowtable: add CLOSING state", v6.14), they stay on the fast path and the flow enters the closing state. The commit message explains that the old behaviour "leads to packet races when restoring the conntrack state as well as late flow re-offload situations". - It says hits are sent with `neigh_xmit()` ([nf_flowtable.rst:21][rst21], [nf_flowtable.rst:74][rst74]). Direct mode has called `dev_queue_xmit()` since [`7a27f6ab4135`][c7a27] (v5.13, the same day as the documentation update), and [`b5964aac51e0`][cb596] ("netfilter: flowtable: consolidate xmit path", v6.19) moved neighbour mode to it too. -- It says packets are not seen "from any of the Netfilter hooks coming after ingress" ([nf_flowtable.rst:22-23][rst22]). The netfilter egress hook, added in [`42df6e1d221d`][c42df] (v5.16, after the documentation was written), does see software hits that aren't IPsec flows, because they end in `dev_queue_xmit()`. An IPsec hit also runs postrouting, because it goes out through `dst_output()`, which it has done since [`589b474a4b7c`][c589b] (v5.3), before the documentation was last updated. +- It says packets are not seen "from any of the Netfilter hooks coming after ingress" ([nf_flowtable.rst:22-23][rst22]). The netfilter egress hook, added in [`42df6e1d221d`][c42df] (v5.16, after the documentation was written), does see software hits in a direction without an IPsec transform, because they end in `dev_queue_xmit()`. An IPsec hit also runs postrouting, because it goes out through `dst_output()`, which it has done since [`589b474a4b7c`][c589b] (v5.3), before the documentation was last updated. - It says the rule adds the flow on "the TCP syn-ack packet" ([nf_flowtable.rst:105-106][rst105]). The code requires an established and assured conntrack entry ([`nf_conntrack_tcp_established()`][tcpest]). [`e5eaac2beb54`][ce5ea] (v5.18) added that check, and its message says the patch makes `nft_flow_offload` "skip packets in SYN_RECV state" and "postpones offload up to the next packet". - It lists TCP and UDP as the supported layer 4 protocols ([nf_flowtable.rst:10][rst10]). GRE has also been accepted since [`4e8d9584d154`][c4e8d] (v5.18). @@ -275,10 +281,11 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [c53b3]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=53b3e60edb674b442b2b3bbdba484667b0f47a5d [c589b]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=589b474a4b7ce409d6821ef17234a995841bd131 [c6c5d]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6c5dcab95f4cd42a1648739ec9300fbb4b1a021f -[c73319]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=73319a8ee18b9cf0b2dac87f8521595e0381ba0c +[c6d07]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=6d07a289504a246aa97cdac0fec61a15834801b7 [c794d]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=794d9b25817a813f1d2fb8e133ba6d2f53920853 [c7a27]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7a27f6ab41356ecba47ec2bec6d635704c169779 [c871df]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=871df5007edaacaf2eac58a2871af6044a444ad4 +[c89cc]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=89cc8f1c5f22568142b7ad118c738204708e4207 [ca331]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a331b78a552551d0e404e58e6390b1c828d6af8f [ca3c9]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a3c90f7a2323b331ae816d5b0633e68148e25d04 [cab427]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ab427db17885814069bae891834f20842f0ac3a4 @@ -294,6 +301,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [cee1b]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ee1bcfe01251e8958b53de45d2b0c85e09dd2719 [cfdbaf]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fdbaf5163331342e90a2c29b87629021f4c15f0c [cleanup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n760 +[closestate]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n218 [coreclear]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n401 [coreday]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n358 [coredst]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n129 @@ -316,6 +324,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [evalcheck]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n98 [evalconf]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n102 [evalonce]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n105 +[evalonce2]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n105 [ext]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n517 [fillpath]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n45 [fixtcp]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n178 @@ -336,8 +345,10 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [grecfg]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n81 [grekey]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_gre.c?h=v7.4-rc3#n226 [hookalloc]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n2456 +[hwalloc]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n873 [hwstats]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1149 [hwstatwork]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1011 +[hwxfrm]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n262 [indrcmd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1239 [inet68]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_inet.c?h=v7.4-rc3#n68 [ip6dec]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1145 @@ -380,6 +391,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [offsetup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1279 [offtuple]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n901 [pathbr]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n187 +[pathdef]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n31 [pathdev]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n198 [pathhw]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n194 [pathin]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n241 @@ -408,6 +420,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [rst183]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n183 [rst20]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n20 [rst202]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n202 +[rst202b]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n202 [rst21]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n21 [rst214]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n214 [rst22]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n22 @@ -432,6 +445,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [tcpest]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_conntrack_l4proto.h?h=v7.4-rc3#n206 [tcpoff]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n1658 [tcpprint]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n266 +[tcpstate]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n1321 [tcpto]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n479 [tear]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n399 [ttl]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n253 @@ -448,10 +462,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [xfrm]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n300 [xfrm4]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/xfrm4_output.c?h=v7.4-rc3#n33 [xfrm6]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv6/xfrm6_output.c?h=v7.4-rc3#n114 -[xfrmdirect]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/xfrm/xfrm_output.c?h=v7.4-rc3#n786 [xfrmlo]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/xfrm/xfrm_output.c?h=v7.4-rc3#n600 -[xfrmpost]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/xfrm/xfrm_output.c?h=v7.4-rc3#n608 -[xfrmpost2]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/xfrm/xfrm_output.c?h=v7.4-rc3#n604 [xmit]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n776 [xmit4]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n838 [xmittype]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n19 From 294f353bb61120ca462d8ebfe89031b00bc535eb Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 20:34:59 -0800 Subject: [PATCH 16/22] net: fix flowtable route, removal-timing and rule-change statements Scope the route check to the packet's direction, correct the hardware removal timing on device down, split the rule-change bullets, and cite the local-out hooks and the tag bits. --- docs/net/README.md | 2 +- docs/net/flowtable.md | 42 +++++++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/docs/net/README.md b/docs/net/README.md index 01cbd71b..1c46663b 100644 --- a/docs/net/README.md +++ b/docs/net/README.md @@ -64,7 +64,7 @@ Every packet is carried by an [sk_buff](sk-buff.md) — start there if you read | [Netfilter](netfilter.md) | Hooks, tables, chains | | [nftables vs iptables](nftables-iptables.md) | Why the replacement happened | | [Connection Tracking](conntrack.md) | conntrack entries, NAT, helpers | -| [Flowtable](flowtable.md) | The fast path that lets forwarded flows already in a flow table skip most of the hook path | +| [Flowtable](flowtable.md) | The fast path that lets forwarded flows skip most of the hook path | | [Traffic Control (tc/qdisc)](tc-qdisc.md) | Queuing disciplines, shaping, fq_codel | ### High performance & offload diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index cc301388..7940edf7 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -28,7 +28,7 @@ Which hooks a hit passes, from the source: - The tc ingress hook runs before the netfilter ingress hook where the flowtable sits ([dev.c:6290][dev6290], [dev.c:6298][dev6298]). - A software hit in a direction without an IPsec transform is transmitted with `dev_queue_xmit()` ([`__nf_flow_queue_xmit()`][xmit]), which runs the netfilter egress hook and tc egress ([dev.c:5024][dev5024], [dev.c:5032][dev5032]). -- A hit in a direction whose route has an IPsec transform is transmitted with `dst_output()` ([`nf_flow_xmit_xfrm()`][xfrm]), which runs the postrouting hook ([xfrm4_output.c:33][xfrm4], [xfrm6_output.c:114][xfrm6]). The transform itself is applied in `xfrm_output_resume()`, which runs the local-out hook ([xfrm_output.c:600][xfrmlo]). +- A hit in a direction whose route has an IPsec transform is transmitted with `dst_output()` ([`nf_flow_xmit_xfrm()`][xfrm]), which runs the postrouting hook ([xfrm4_output.c:33][xfrm4], [xfrm6_output.c:114][xfrm6]). The transform itself is applied in `xfrm_output_resume()`, which calls `local_out`, and that runs the local-out hook ([xfrm_output.c:600][xfrmlo], [ip_output.c:120][iplo], [output_core.c:116][ip6lo]). The kernel documentation says "you do not see these packets from any of the Netfilter hooks coming after ingress" ([nf_flowtable.rst:22-23][rst22]). The source differs; see [Differences](#differences-from-nf_flowtablerst). @@ -54,10 +54,10 @@ table inet x { - **The statement.** `flow add @f` is evaluated by [`nft_flow_offload_eval()`][eval]. It is accepted only in a chain on the forward hook ([`nft_flow_offload_validate()`][validate]). The example rule matches IPv4 TCP. - **What a hit skips.** A hit is consumed with `NF_STOLEN` ([`nf_flow_queue_xmit()`][qxmit], [`nf_flow_xmit_xfrm()`][xfrm]), so the later packets of a flow that hit the table don't reach the prerouting and forward chains, and reach postrouting only in a direction with an IPsec transform (above). The kernel documentation says the `counter` rule stops being updated ([nf_flowtable.rst:105-109][rst105]). - **Rule evaluation.** An earlier rule that accepts or drops the packet ends evaluation of the base chain, including any chain it jumped to ([nf_tables_core.c:306-312][nftverdict]), so `flow add` never runs for it in that chain. `flow add` returns no verdict when it adds the flow. On every other path it sets `NFT_BREAK` ([`nft_flow_offload_eval()`][eval]), which skips the rest of that rule and continues with the next rule ([nf_tables_core.c:292-295][nftcore]). So statements after `flow add` in the same rule run only for the packet that adds the flow. -- **The `counter` flag.** Software hits update conntrack's counters only if the flowtable has the `counter` flag ([nf_flow_table_ip.c:520][ctcount]; documented in [nf_flowtable.rst:165-181][rst165]). The update reaches only conntrack entries that have the accounting extension ([`nf_ct_acct_add()`][acctfn]), which is added when the entry is created and only if the sysctl `net.netfilter.nf_conntrack_acct` is set ([nf_conntrack_acct.h:36][acct], [nf_conntrack_core.c:1789][acctext], [sysctl][acctsysctl]). +- **The `counter` flag.** Software hits update conntrack's counters only if the flowtable has the `counter` flag ([nf_flow_table_ip.c:520][ctcount] for IPv4, [nf_flow_table_ip.c:1148][ctcount6] for IPv6; documented in [nf_flowtable.rst:165-181][rst165]). The update reaches only conntrack entries that have the accounting extension ([`nf_ct_acct_add()`][acctfn]), which is added when the entry is created and only if the sysctl `net.netfilter.nf_conntrack_acct` is set ([nf_conntrack_acct.h:36][acct], [nf_conntrack_core.c:1789][acctext], [sysctl][acctsysctl]). - **Priority.** The kernel documentation says to "make sure the flowtable priority is smaller than the nftables ingress chain hence the flowtable runs before in the pipeline" ([nf_flowtable.rst:99-103][rst99]). A hit returns `NF_STOLEN`, which ends hook traversal ([`nf_hook_slow()`][nfstolen]), so an ingress chain that runs after the flowtable doesn't see it. - **Device list.** Path resolution follows VLAN, PPPoE and bridge devices down to the real device ([`nft_dev_fill_forward_path()`][fillpath]) and requires that device to be in the flowtable's list ([nf_flow_table_path.c:198][pathdev]). The check runs for both directions and is skipped for IPsec routes ([nf_flow_table_path.c:314-320][pathskip]). The kernel documentation says the real device is sufficient and the VLAN and PPPoE devices need not be listed ([nf_flowtable.rst:129-130][rst129]). - Since v7.2 ([`871df5007eda`][c871df]), a failed path discovery or a device that isn't in the flowtable makes `flow add` give up. The commit message says "If forward path discovery fails for any reason or netdevice is not registered for this flowtable, then bail out to classic forwarding path rather than providing incomplete forwarding path." +- **Failed path discovery.** Since v7.2 ([`871df5007eda`][c871df]), a failed path discovery or a device that isn't in the flowtable makes `flow add` give up. The commit message says "If forward path discovery fails for any reason or netdevice is not registered for this flowtable, then bail out to classic forwarding path rather than providing incomplete forwarding path." - **Missing devices.** A device name in the flowtable that matches no existing device is accepted ([nf_tables_api.c:2456][hookalloc]; since v6.16, [`6d07a289504a`][c6d07]) and registers no hook until a device with that name appears ([`a331b78a5525`][ca331]). Two flowtable options appear in the kernel documentation: `counter` ([nf_flowtable.rst:165-181][rst165]) and `flags offload` ([nf_flowtable.rst:183-196][rst183]): @@ -117,7 +117,7 @@ Each direction caches its route in the entry, except in direct mode without a tu 1. Checks the packet against the MTU recorded for the packet's direction. A packet that is too big goes to the classic path ([`nf_flow_exceeds_mtu()`][mtu]); the kernel documentation gives the reason as the packet-too-big ICMP error ([nf_flowtable.rst:78-79][rst78]). 2. Checks TCP flags ([`nf_flow_state_check()`][state]; see [How a flow leaves](#how-a-flow-leaves)). -3. Checks the cached route, if the flow keeps one ([`nf_flow_dst_check()`][dstchk] returns true when there is none). A stale route tears the flow down and the packet goes to the classic path. A direction in direct mode without a tunnel keeps no route, so the hook notices no route change for it ([nf_flow_table_core.c:129-136][coredst]); the collector checks the routes of both directions ([`nf_flow_offload_gc_step()`][gcstep]). In the bridge case, the commit that added direct mode, [`7a27f6ab4135`][c7a27], says "if peer is moved to different bridge port, the connection will time out". +3. Checks the cached route of the packet's direction, if that direction keeps one ([`nf_flow_dst_check()`][dstchk] returns true when there is none). A stale route tears the flow down and the packet goes to the classic path. A direction in direct mode without a tunnel keeps no route ([nf_flow_table_core.c:129-136][coredst]), so neither the hook nor the collector notices a route change for it. In the bridge case, the commit that added direct mode, [`7a27f6ab4135`][c7a27], says "if peer is moved to different bridge port, the connection will time out". 4. Refreshes the deadline ([`flow_offload_refresh()`][refresh]), strips any VLAN, PPPoE or IPIP tunnel header (IPIP since v6.19, [`ab427db17885`][cab427]), applies the stored NAT rewrite, decrements the TTL, and updates conntrack's counters if the flowtable has `counter`. The hook then transmits as described in [Transmit modes](#transmit-modes): [`nf_flow_queue_xmit4()`][xmit4] pushes any egress VLAN, PPPoE or tunnel header back on, builds the link-layer header with `dev_hard_header()` and calls `dev_queue_xmit()`. IPsec hits skip that and go out through `dst_output()`. @@ -132,9 +132,9 @@ The garbage collector, [`nf_flow_offload_work_gc()`][gcwork], re-queues itself o - **A timeout.** Each entry has a deadline. A software hit pushes it out, at most once a second ([nf_flow_table_core.c:373][corelimit]); this also applies to flows that are closing (the refresh writes the deadline before it checks for `NF_FLOW_CLOSING`). The default is 30 seconds for TCP ([nf_conntrack_proto_tcp.c:1658][tcpoff]) and UDP ([nf_conntrack_proto_udp.c:204][udpoff]), set by the sysctls `net.netfilter.nf_flowtable_tcp_timeout` and `net.netfilter.nf_flowtable_udp_timeout` ([nf_conntrack_standalone.c:789][sysctltcp], [nf_conntrack_standalone.c:839][sysctludp]), and 30 seconds for GRE, which has no sysctl ([`flow_offload_get_timeout()`][gettimeout], [nf_flow_table.h:209][flowtimeout]). For hardware flows, the collector asks the driver for statistics once less than 90% of the timeout remains and extends the deadline from the last-used time ([`nf_flow_offload_stats()`][hwstats], [`flow_offload_work_stats()`][hwstatwork]). - **A dying conntrack entry**, checked by `nf_ct_is_dying()` ([`nf_flow_offload_gc_step()`][gcstep]); software lookups also miss ([`flow_offload_lookup()`][lookup]). -- **A stale route**, for flows that keep one. The collector checks this since v7.3 ([`edd51a233438`][cedd51]); the hook checks it too ([`nf_flow_offload_forward()`][forward]). +- **A stale route**, for a direction that keeps one. The collector tears the flow down if either direction's route is stale (since v7.3, [`edd51a233438`][cedd51]); the hook checks the packet's direction ([`nf_flow_offload_forward()`][forward]). - **A missing output device, or a neighbour lookup error.** The hook tears the flow down and drops that packet ([nf_flow_table_ip.c:870-880][neigherr] in neighbour mode, [nf_flow_table_ip.c:885-889][neighdir] in direct mode for a missing device; the IPv6 lookup is [`ip_neigh_gw6()`][neigh6]). A missing neighbour entry is created by the lookup ([`ip_neigh_gw4()`][neigh4]), so only a failure to create it counts. -- **A device going down.** The `NETDEV_DOWN` notifier ([nft_flow_offload.c:248][notifier]) calls [`nf_flow_table_cleanup()`][cleanup], which tears down the flows whose input device in either direction is that device ([`nf_flow_table_do_cleanup()`][docleanup]); a VLAN, PPPoE or bridge device going down doesn't match, because the recorded input device is the real one ([nf_flow_table_path.c:241][pathin]), except opposite an IPsec direction, where the route's own device is recorded as is ([nf_flow_table_path.c:31][pathdef]). +- **A device going down.** The `NETDEV_DOWN` notifier ([nft_flow_offload.c:248][notifier]) calls [`nf_flow_table_cleanup()`][cleanup], which tears down the flows whose input device in either direction is that device ([`nf_flow_table_do_cleanup()`][docleanup]); a VLAN, PPPoE or bridge device going down doesn't match, because the recorded input device is the real one ([nf_flow_table_path.c:241][pathin]), except opposite an IPsec direction, where the route's own device is recorded as is ([nf_flow_table_path.c:31][pathdef]). Such a flow can still leave through the stale-route check if the direction keeps a route. Unbinding an indirect-block driver also tears down the flows of its device ([nf_flow_table_offload.c:1231][indrclean]). - **Deleting the flowtable** ([`nf_flow_table_free()`][free]). ``` @@ -156,18 +156,19 @@ The garbage collector, [`nf_flow_offload_work_gc()`][gcwork], re-queues itself o ▼ removed by the garbage collector: in the same run if it did the teardown, otherwise on its next run (about a second); hardware flows after the driver - delete completes; at once for device down and flowtable deletion + delete completes; at once for software flows on device down, and for all + flows on flowtable deletion ``` **TCP FIN and RST** don't end the flow at once (since v6.14, [`fdbaf5163331`][cfdbaf]). [`nf_flow_state_check()`][state] marks the flow `NF_FLOW_CLOSING` and the packet still takes the fast path; [`nf_flow_offload_gc_step()`][gcstep] deletes the hardware rule early and the flow stays in the software table. Software hits keep pushing the deadline, so the flow leaves when the timeout expires after the last packet, and [`flow_offload_fixup_ct()`][fixup] then gives conntrack the close state and timeout. A later SYN on a closing flow tears it down at once, sets the conntrack state to close and sends that packet to the classic path ([`nf_flow_state_check()`][state], [`flow_offload_fixup_tcp()`][fixtcp]). -**Adding again.** [`flow_offload_teardown()`][tear] clears the offload bit on the conntrack entry ([nf_flow_table_core.c:401][coreclear]), so a later forward-chain packet that matches the `flow add` rule can add the flow again, if it passes the checks in [How a flow gets in](#how-a-flow-gets-in) again. A closing TCP flow gets the close state at teardown ([nf_flow_table_core.c:218-221][closestate]), so it isn't added again until it is established. +**Adding again.** [`flow_offload_teardown()`][tear] clears the offload bit on the conntrack entry ([nf_flow_table_core.c:401][coreclear]), so a later forward-chain packet that matches the `flow add` rule can add the flow again, if it passes the checks in [How a flow gets in](#how-a-flow-gets-in) again. A closing TCP flow gets the close state at teardown ([nf_flow_table_core.c:218-221][closestate]), so it isn't added again until it is established ([`nf_conntrack_tcp_established()`][tcpest]). **Conntrack timeout.** While a flow is in the table, conntrack may see few or none of its packets; the comment in [`nf_flow_table_extend_ct_timeout()`][ext] says "nf_conntrack might not see any packets anymore" ([nf_flow_table_core.c:506-513][gccomment]). [`flow_offload_add()`][add] first sets the conntrack timeout to a day ([nf_flow_table_core.c:358][coreday]). Afterwards the collector calls [`nf_flow_table_extend_ct_timeout()`][ext], which sets a new timeout when less than five minutes remain. The value depends on the protocol and TCP state ([`nf_flow_table_tcp_timeout()`][tcpto]): | Flow | Conntrack timeout is set to | |------|-----------------------------| -| UDP, GRE | a day | +| UDP, GRE (GRE since v7.4-rc3, [`46ecda278f6e`][c46ecd]) | a day | | TCP established | a day | | TCP FIN_WAIT, CLOSE_WAIT, LAST_ACK, TIME_WAIT | five minutes | | Other TCP states (SYN_SENT, SYN_RECV, CLOSE, ...) | no extension | @@ -177,7 +178,7 @@ The comment in [`nf_flow_table_extend_ct_timeout()`][ext] calls the value "an ar **Hand-back to conntrack.** [`flow_offload_fixup_ct()`][fixup] runs at teardown. - **TCP state.** It keeps the last conntrack state, or sets close if the flow was closing, and clears the recorded maximum window and the max-ack flag; the code comment says that otherwise conntrack's TCP reset validation would fail ([`flow_offload_fixup_tcp()`][fixtcp]). -- **Timeout.** TCP and UDP flows have the conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired; a closing TCP flow gets the close timeout unconditionally ([nf_flow_table_core.c:250-252][corefix]). Except for that case the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64][tcp5d]), keeps the timeout it has left, at most a day, until a packet goes through conntrack again. Conntrack then sets the timeout for the TCP state of that packet ([nf_conntrack_proto_tcp.c:1321][tcpstate]), unless the entry has a fixed timeout ([`__nf_ct_refresh_acct()`][refreshacct], [nf_conntrack_core.c:2094-2096][refreshfix]). +- **Timeout.** TCP and UDP flows have the conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired; a closing TCP flow gets the close timeout unconditionally ([nf_flow_table_core.c:250-252][corefix]). Except for that case the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64][tcp5d]), keeps the timeout it has left (a day or less, unless a packet of the flow went through conntrack while it was in the table) until a packet goes through conntrack again. Conntrack then sets a timeout for the TCP state of that packet ([nf_conntrack_proto_tcp.c:1325-1340][tcpstate2], [nf_conntrack_proto_tcp.c:1381][tcprefresh]), unless the entry has a fixed timeout ([`__nf_ct_refresh_acct()`][refreshacct], [nf_conntrack_core.c:2094-2096][refreshfix]). - **GRE.** GRE flows return before any timeout change ([nf_flow_table_core.c:240-241][coregre]). ## Hardware offload @@ -186,16 +187,17 @@ The kernel documentation says a flowtable can offload flows to a device that sup The driver interface is the tc flow-block interface: -- **Binding.** Registering a flowtable's ingress hook on a device first calls the flowtable type's `setup` callback, [`nf_flow_table_offload_setup()`][setup] ([nf_tables_api.c:9068][apisetup]). Without `flags offload` it only sets up the XDP path ([nf_flow_table_offload.c:1279-1280][offsetup]); With `flags offload`, if the device has `ndo_setup_tc` the flowtable calls it with `TC_SETUP_FT` ([`nf_flow_table_offload_cmd()`][offcmd]); otherwise it uses the indirect-block path `flow_indr_dev_setup_offload()` ([`nf_flow_table_indr_offload_cmd()`][indrcmd]). The driver's callback is added to the flowtable's block ([nf_flow_table_offload.c:1193][splice]). -- **XDP lookup.** XDP programs can look a flowtable up with [`bpf_xdp_flow_lookup`][bpf] ([`391bb6594fd3`][c391bb], v6.11). It sees only flowtables without `flags offload` ([`89cc8f1c5f22`][c89cc], v6.11), returns the first flowtable bound to the device ([nf_flow_table_xdp.c:36-42][xdpft]), and `nf_flow_table_bpf.c` is built with `CONFIG_DEBUG_INFO_BTF` when the flowtable is built in and `CONFIG_DEBUG_INFO_BTF_MODULES` when it is a module ([Makefile:147-150][mkbpf]). +- **Binding.** Registering a flowtable's ingress hook on a device first calls the flowtable type's `setup` callback, [`nf_flow_table_offload_setup()`][setup] ([nf_tables_api.c:9068][apisetup]). Without `flags offload` it only sets up the XDP path ([nf_flow_table_offload.c:1279-1280][offsetup]). With `flags offload`, if the device has `ndo_setup_tc` the flowtable calls it with `TC_SETUP_FT` ([`nf_flow_table_offload_cmd()`][offcmd]); otherwise it uses the indirect-block path `flow_indr_dev_setup_offload()` ([`nf_flow_table_indr_offload_cmd()`][indrcmd]). The driver's callback is added to the flowtable's block ([nf_flow_table_offload.c:1193][splice]). - **Per flow.** [`nf_flow_offload_tuple()`][offtuple] calls every callback with `TC_SETUP_CLSFLOWER` and a `flow_cls_offload` request, the request type tc flower uses. The command is `FLOW_CLS_REPLACE` to add a flow (with a flow rule built by [`nf_flow_rule_route_ipv4()`][rulefn], or the IPv6 and inet equivalents named in [nf_flow_table_inet.c:68-96][inet68], with [`nf_flow_rule_match()`][rulematch]: match keys plus actions), `FLOW_CLS_DESTROY` or `FLOW_CLS_STATS`. - **In-tree drivers that handle `TC_SETUP_FT`** as of v7.4-rc3: the mlx5 representor code ([rep/tc.c:261][mlx5], [rep/tc.c:675][mlx5ind]), the MediaTek Ethernet and WED drivers ([mtk_ppe_offload.c:674][mtkppe], [mtk_wed.c:2790][mtkwed]), the mt76 Wi-Fi driver ([npu.c:385][mt76]) and the Airoha Ethernet driver ([airoha_eth.c:3674][airoha]). DSA user ports pass the command to the conduit device ([dsa/user.c:1729][dsa]). `netdevsim` returns 0 and registers no callback ([netdevsim/tc.c:92][netdevsim]). +**XDP lookup.** XDP programs can look a flowtable up with [`bpf_xdp_flow_lookup`][bpf] ([`391bb6594fd3`][c391bb], v6.11). It sees only flowtables without `flags offload` ([`89cc8f1c5f22`][c89cc], v6.11), returns the first flowtable bound to the device ([nf_flow_table_xdp.c:36-42][xdpft]), and `nf_flow_table_bpf.c` is built with `CONFIG_DEBUG_INFO_BTF` when the flowtable is built in and `CONFIG_DEBUG_INFO_BTF_MODULES` when it is a module ([Makefile:147-150][mkbpf]). + Behaviour with `flags offload`, from the source: - If the driver rejects `TC_SETUP_FT` ([`nf_flow_table_offload_cmd()`][offcmd]), or no indirect-block driver binds (`-EOPNOTSUPP`, [flow_offload.c:628][flowoffeop]), creating the flowtable or adding the device to it fails: the `setup` error is returned before the hook is registered ([nf_tables_api.c:9068][apisetup], [nf_tables_api.c:9104-9106][apiregall]). With a driver that accepts `TC_SETUP_FT` but registers no callback, such as `netdevsim`, the flowtable loads and no flow is added in hardware ([`flow_offload_rule_add()`][ruleadd] returns `-ENOENT` when nothing was added). - A device that registers later is handled by the netdev notifier, which returns `NOTIFY_BAD` when registering the hook fails ([nf_tables_api.c:9908-9913][apinotif2]); `register_netdevice()` then unregisters the device ([dev.c:11778-11785][devreg]). A change of the `offload` flag on an existing flowtable is rejected with `-EOPNOTSUPP` ([nf_tables_api.c:9200-9203][apitoggle]). -- A direction that goes through a valid Ethernet device uses direct mode ([nf_flow_table_path.c:194-196][pathhw]), which caches no route unless the direction has a tunnel. A direction through a device that isn't a valid Ethernet device stays in neighbour mode, and an IPsec direction stays in xfrm mode; both cache their route ([nf_flow_table_core.c:143-147][corexfrm]). A flow with an IPsec direction is not added in hardware: building the rule fails for the xfrm transmit type ([nf_flow_table_offload.c:262-263][hwxfrm], [nf_flow_table_offload.c:873-883][hwalloc]). +- A direction that goes through a valid Ethernet device uses direct mode ([nf_flow_table_path.c:194-196][pathhw]), which caches no route unless the direction has a tunnel. A direction through a device that isn't a valid Ethernet device stays in neighbour mode (unless a PPPoE or bridge path puts it in direct mode), and an IPsec direction stays in xfrm mode; both cache their route ([nf_flow_table_core.c:143-147][corexfrm]). A flow with an IPsec direction is not added in hardware: building the rule fails for the xfrm transmit type ([nf_flow_table_offload.c:262-263][hwxfrm], [nf_flow_table_offload.c:873-883][hwalloc]). - A hardware add that fails isn't reported. The flow stays in software with `NF_FLOW_HW` set, `[HW_OFFLOAD]` is set if the driver accepted at least one direction ([`flow_offload_rule_add()`][ruleadd], [`flow_offload_work_add()`][workadd]), and the add is queued again on later software hits, at most once a second and not for closing flows ([nf_flow_table_core.c:373-383][corelimit]). - Flows through an IPIP or IPv6-in-IPv6 tunnel stay in software ([`nf_flow_offload_unsupported()`][unsup]; [`6c5dcab95f4c`][c6c5d], v7.2). - The collector skips an entry whose hardware work is still queued ([nf_flow_table_core.c:581][gcpending]). @@ -203,14 +205,15 @@ Behaviour with `flags offload`, from the source: ## Inspecting - **Conntrack listing.** `conntrack -L` shows `[OFFLOAD]` for software flows and `[HW_OFFLOAD]` for hardware flows ([nf_flowtable.rst:111-113][rst111], [nf_flowtable.rst:202-206][rst202]). `/proc/net/nf_conntrack` (with `CONFIG_NF_CONNTRACK_PROCFS`, [nf_conntrack_standalone.c:34][kcprocfs]) prints the same tags ([nf_conntrack_standalone.c:364-366][proctag]). -- **Checking that a flow was added.** The `[OFFLOAD]` tag in `conntrack -L` is how the kernel documentation says to identify offloaded flows, and `[HW_OFFLOAD]` marks hardware flows ([nf_flowtable.rst:111-113][rst111], [nf_flowtable.rst:202-206][rst202b]). The tag follows `IPS_OFFLOAD_BIT`, which `flow add` sets ([nft_flow_offload.c:105][evalonce2]) and teardown clears ([nf_flow_table_core.c:401][coreclear]). A flow without either tag is not being forwarded by the flowtable: it was never added (the reasons are in [How a flow gets in](#how-a-flow-gets-in)), or it has been torn down. +- **Checking that a flow was added.** `[OFFLOAD]` follows `IPS_OFFLOAD_BIT`, which `flow add` sets ([nft_flow_offload.c:105][evalonce2]) and teardown clears ([nf_flow_table_core.c:401][coreclear]). `[HW_OFFLOAD]` follows `IPS_HW_OFFLOAD_BIT`, which the hardware add sets ([`flow_offload_work_add()`][workadd]) and the hardware delete clears ([nf_flow_table_offload.c:994][hwdel]). A flow without either tag is not being forwarded by the flowtable: it was never added (the reasons are in [How a flow gets in](#how-a-flow-gets-in)), or it has been torn down. - **What offloaded entries omit.** ctnetlink skips the timeout and protocol info ([nf_conntrack_netlink.c:579][nlnk]), and `/proc/net/nf_conntrack` skips the timeout and the TCP state ([nf_conntrack_standalone.c:339][procnf], [`tcp_print_conntrack()`][tcpprint]). - **Statistics.** With `CONFIG_NF_FLOW_TABLE_PROCFS` ([Kconfig:721][kconfig]), [`/proc/net/stat/nf_flowtable`][procfs] shows per-CPU counters of pending hardware add, delete and stats work; the file is created at [nf_flow_table_procfs.c:71][procfscreate] and its show function prints only those three columns. They are incremented when work is queued and decremented when it completes ([nf_flow_table_offload.c:1047-1077][statinc]). There are no hit or miss counters. ## Limits - **Rule changes.** Removing a rule runs the expression's `deactivate` and `destroy` callbacks, which release the expression's use of the flowtable and its conntrack hook registration for the netns, and don't touch flow entries ([nft_flow_offload.c:185-206][deact]). The ways a flow leaves are in [How a flow leaves](#how-a-flow-leaves). -- **Rules a hit doesn't reach.** A hit never reaches the forward chain, and a software hit pushes the deadline out ([`flow_offload_refresh()`][refresh]). Of the ruleset's actions, only the stored NAT rewrite is applied to a hit ([`nf_flow_offload_forward()`][forward]). So a flow already in the table keeps being forwarded after the `flow add` rule is removed or a drop rule is added to the prerouting or forward chains, or to postrouting for a direction without an IPsec transform, as long as packets keep arriving; hardware flows get their deadline from driver statistics instead ([`flow_offload_work_stats()`][hwstatwork]). Packets that miss the table, such as fragments and over-MTU packets, still reach those chains. +- **Rules a hit doesn't reach.** A hit never reaches the prerouting and forward chains, and a software hit pushes the deadline out ([`flow_offload_refresh()`][refresh]). The stored NAT rewrite is applied to a hit ([`nf_flow_offload_forward()`][forward]). Packets the hook passes to the classic path, such as fragments and over-MTU packets, still reach those chains. +- **Consequence.** A flow already in the table keeps being forwarded after the `flow add` rule is removed or a drop rule is added to the prerouting or forward chains, or to postrouting for a direction without an IPsec transform, as long as packets keep arriving; hardware flows get their deadline from driver statistics instead ([`flow_offload_work_stats()`][hwstatwork]). - **Cache.** The kernel documentation says the table behaves like a cache and that entries might get stale if the destination MAC address or the egress device changes, which "might be a problem" with software mode combined with bridging and IP forwarding, and with hardware offload ([nf_flowtable.rst:214-222][rst214]). - **IPsec.** A packet that carries a security path never triggers an add ([`nft_flow_offload_skip()`][skip]). In the direction whose route has an IPsec transform the device-list and neighbour checks are skipped ([nf_flow_table_path.c:314-320][pathskip]); the other direction gets them like any other flow. - **ip6gre.** The commit that extended the GRE timeout, [`46ecda278f6e`][c46ecd], says `ip6gre` tunnels with the default encapsulation limit carry a Destination Options header before GRE, which the flowtable doesn't parse, so every packet of the flow takes the classic path. @@ -310,6 +313,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [corelimit]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n373 [corexfrm]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n143 [ctcount]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n520 +[ctcount6]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1148 [cttcp]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n41 [deact]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n185 [dev5024]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c?h=v7.4-rc3#n5024 @@ -346,9 +350,11 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [grekey]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_gre.c?h=v7.4-rc3#n226 [hookalloc]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n2456 [hwalloc]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n873 +[hwdel]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n994 [hwstats]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1149 [hwstatwork]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1011 [hwxfrm]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n262 +[indrclean]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1231 [indrcmd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_offload.c?h=v7.4-rc3#n1239 [inet68]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_inet.c?h=v7.4-rc3#n68 [ip6dec]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1145 @@ -356,10 +362,12 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [ip6fwd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv6/ip6_output.c?h=v7.4-rc3#n612 [ip6hook]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1233 [ip6ip6]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n356 +[ip6lo]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv6/output_core.c?h=v7.4-rc3#n116 [ip6ttl]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n1069 [ipfwd]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/ip_forward.c?h=v7.4-rc3#n121 [iphook]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n904 [ipip]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n331 +[iplo]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/ip_output.c?h=v7.4-rc3#n120 [kcft]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/Kconfig?h=v7.4-rc3#n711 [kcinet]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/Kconfig?h=v7.4-rc3#n703 [kcnft]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/Kconfig?h=v7.4-rc3#n504 @@ -420,7 +428,6 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [rst183]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n183 [rst20]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n20 [rst202]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n202 -[rst202b]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n202 [rst21]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n21 [rst214]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n214 [rst22]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/nf_flowtable.rst?h=v7.4-rc3#n22 @@ -445,7 +452,8 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [tcpest]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/net/netfilter/nf_conntrack_l4proto.h?h=v7.4-rc3#n206 [tcpoff]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n1658 [tcpprint]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n266 -[tcpstate]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n1321 +[tcprefresh]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n1381 +[tcpstate2]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n1325 [tcpto]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n479 [tear]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n399 [ttl]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n253 From 7b92d655e4616b7fe2da4d2719a4f6fbf4a57eab Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 20:46:48 -0800 Subject: [PATCH 17/22] net: tighten flowtable page wording after review Scope the NF_STOLEN, tag and conntrack-refresh statements, drop an uncited stale-route sentence, split the dense timeout bullet and reuse one reference id. --- docs/net/flowtable.md | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index 7940edf7..ce4052cb 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -24,7 +24,7 @@ The kernel documentation describes the design: once the first packet of a flow h The diagram is simplified from Fig. 1 of the kernel documentation ([nf_flowtable.rst:37-70][rst37]). The figure labels the rule that adds an entry "flow offload"; the example below spells it `flow add`. -Which hooks a hit passes, from the source: +Which hooks a software hit passes, from the source: - The tc ingress hook runs before the netfilter ingress hook where the flowtable sits ([dev.c:6290][dev6290], [dev.c:6298][dev6298]). - A software hit in a direction without an IPsec transform is transmitted with `dev_queue_xmit()` ([`__nf_flow_queue_xmit()`][xmit]), which runs the netfilter egress hook and tc egress ([dev.c:5024][dev5024], [dev.c:5032][dev5032]). @@ -36,7 +36,7 @@ The tc `ct` action uses the same infrastructure ([act_ct.c:453][actct453] calls ## Turning it on -The kernel documentation's example is a flowtable on two devices and one rule in the forward chain ([nf_flowtable.rst:87-98][rst87]): +The kernel documentation's example is a flowtable on two devices and one rule in the forward chain ([nf_flowtable.rst:87-96][rst87]): ``` table inet x { @@ -52,10 +52,10 @@ table inet x { ``` - **The statement.** `flow add @f` is evaluated by [`nft_flow_offload_eval()`][eval]. It is accepted only in a chain on the forward hook ([`nft_flow_offload_validate()`][validate]). The example rule matches IPv4 TCP. -- **What a hit skips.** A hit is consumed with `NF_STOLEN` ([`nf_flow_queue_xmit()`][qxmit], [`nf_flow_xmit_xfrm()`][xfrm]), so the later packets of a flow that hit the table don't reach the prerouting and forward chains, and reach postrouting only in a direction with an IPsec transform (above). The kernel documentation says the `counter` rule stops being updated ([nf_flowtable.rst:105-109][rst105]). +- **What a hit skips.** A hit that is transmitted is consumed with `NF_STOLEN` ([`nf_flow_queue_xmit()`][qxmit], [`nf_flow_xmit_xfrm()`][xfrm]), so the later packets of a flow that hit the table don't reach the prerouting and forward chains, and reach postrouting only in a direction with an IPsec transform (above). The kernel documentation says the `counter` rule stops being updated ([nf_flowtable.rst:105-109][rst105]). - **Rule evaluation.** An earlier rule that accepts or drops the packet ends evaluation of the base chain, including any chain it jumped to ([nf_tables_core.c:306-312][nftverdict]), so `flow add` never runs for it in that chain. `flow add` returns no verdict when it adds the flow. On every other path it sets `NFT_BREAK` ([`nft_flow_offload_eval()`][eval]), which skips the rest of that rule and continues with the next rule ([nf_tables_core.c:292-295][nftcore]). So statements after `flow add` in the same rule run only for the packet that adds the flow. -- **The `counter` flag.** Software hits update conntrack's counters only if the flowtable has the `counter` flag ([nf_flow_table_ip.c:520][ctcount] for IPv4, [nf_flow_table_ip.c:1148][ctcount6] for IPv6; documented in [nf_flowtable.rst:165-181][rst165]). The update reaches only conntrack entries that have the accounting extension ([`nf_ct_acct_add()`][acctfn]), which is added when the entry is created and only if the sysctl `net.netfilter.nf_conntrack_acct` is set ([nf_conntrack_acct.h:36][acct], [nf_conntrack_core.c:1789][acctext], [sysctl][acctsysctl]). -- **Priority.** The kernel documentation says to "make sure the flowtable priority is smaller than the nftables ingress chain hence the flowtable runs before in the pipeline" ([nf_flowtable.rst:99-103][rst99]). A hit returns `NF_STOLEN`, which ends hook traversal ([`nf_hook_slow()`][nfstolen]), so an ingress chain that runs after the flowtable doesn't see it. +- **The `counter` flag.** Software hits update conntrack's counters only if the flowtable has the `counter` flag ([nf_flow_table_ip.c:520][ctcount] for IPv4, [nf_flow_table_ip.c:1148][ctcount6] for IPv6; documented in [nf_flowtable.rst:165-181][rst165]). The update reaches only conntrack entries that have the accounting extension ([`nf_ct_acct_add()`][acctfn]), which is added when the entry is created and only if the sysctl `net.netfilter.nf_conntrack_acct` is set ([nf_conntrack_acct.h:36][acct], [nf_conntrack_core.c:1789][acctext], [nf_conntrack_standalone.c:691][acctsysctl]). +- **Priority.** The kernel documentation says to "make sure the flowtable priority is smaller than the nftables ingress chain hence the flowtable runs before in the pipeline" ([nf_flowtable.rst:99-103][rst99]). A hit that is transmitted returns `NF_STOLEN`, which ends hook traversal ([`nf_hook_slow()`][nfstolen]), so an ingress chain that runs after the flowtable doesn't see it. - **Device list.** Path resolution follows VLAN, PPPoE and bridge devices down to the real device ([`nft_dev_fill_forward_path()`][fillpath]) and requires that device to be in the flowtable's list ([nf_flow_table_path.c:198][pathdev]). The check runs for both directions and is skipped for IPsec routes ([nf_flow_table_path.c:314-320][pathskip]). The kernel documentation says the real device is sufficient and the VLAN and PPPoE devices need not be listed ([nf_flowtable.rst:129-130][rst129]). - **Failed path discovery.** Since v7.2 ([`871df5007eda`][c871df]), a failed path discovery or a device that isn't in the flowtable makes `flow add` give up. The commit message says "If forward path discovery fails for any reason or netdevice is not registered for this flowtable, then bail out to classic forwarding path rather than providing incomplete forwarding path." - **Missing devices.** A device name in the flowtable that matches no existing device is accepted ([nf_tables_api.c:2456][hookalloc]; since v6.16, [`6d07a289504a`][c6d07]) and registers no hook until a device with that name appears ([`a331b78a5525`][ca331]). @@ -117,7 +117,7 @@ Each direction caches its route in the entry, except in direct mode without a tu 1. Checks the packet against the MTU recorded for the packet's direction. A packet that is too big goes to the classic path ([`nf_flow_exceeds_mtu()`][mtu]); the kernel documentation gives the reason as the packet-too-big ICMP error ([nf_flowtable.rst:78-79][rst78]). 2. Checks TCP flags ([`nf_flow_state_check()`][state]; see [How a flow leaves](#how-a-flow-leaves)). -3. Checks the cached route of the packet's direction, if that direction keeps one ([`nf_flow_dst_check()`][dstchk] returns true when there is none). A stale route tears the flow down and the packet goes to the classic path. A direction in direct mode without a tunnel keeps no route ([nf_flow_table_core.c:129-136][coredst]), so neither the hook nor the collector notices a route change for it. In the bridge case, the commit that added direct mode, [`7a27f6ab4135`][c7a27], says "if peer is moved to different bridge port, the connection will time out". +3. Checks the cached route of the packet's direction, if that direction keeps one ([`nf_flow_dst_check()`][dstchk] returns true when there is none). A stale route tears the flow down and the packet goes to the classic path. A direction in direct mode without a tunnel keeps no route ([nf_flow_table_core.c:129-136][coredst]), so neither the hook nor the garbage collector notices a route change for it. In the bridge case, the commit that added direct mode, [`7a27f6ab4135`][c7a27], says "if peer is moved to different bridge port, the connection will time out". 4. Refreshes the deadline ([`flow_offload_refresh()`][refresh]), strips any VLAN, PPPoE or IPIP tunnel header (IPIP since v6.19, [`ab427db17885`][cab427]), applies the stored NAT rewrite, decrements the TTL, and updates conntrack's counters if the flowtable has `counter`. The hook then transmits as described in [Transmit modes](#transmit-modes): [`nf_flow_queue_xmit4()`][xmit4] pushes any egress VLAN, PPPoE or tunnel header back on, builds the link-layer header with `dev_hard_header()` and calls `dev_queue_xmit()`. IPsec hits skip that and go out through `dst_output()`. @@ -134,7 +134,7 @@ The garbage collector, [`nf_flow_offload_work_gc()`][gcwork], re-queues itself o - **A dying conntrack entry**, checked by `nf_ct_is_dying()` ([`nf_flow_offload_gc_step()`][gcstep]); software lookups also miss ([`flow_offload_lookup()`][lookup]). - **A stale route**, for a direction that keeps one. The collector tears the flow down if either direction's route is stale (since v7.3, [`edd51a233438`][cedd51]); the hook checks the packet's direction ([`nf_flow_offload_forward()`][forward]). - **A missing output device, or a neighbour lookup error.** The hook tears the flow down and drops that packet ([nf_flow_table_ip.c:870-880][neigherr] in neighbour mode, [nf_flow_table_ip.c:885-889][neighdir] in direct mode for a missing device; the IPv6 lookup is [`ip_neigh_gw6()`][neigh6]). A missing neighbour entry is created by the lookup ([`ip_neigh_gw4()`][neigh4]), so only a failure to create it counts. -- **A device going down.** The `NETDEV_DOWN` notifier ([nft_flow_offload.c:248][notifier]) calls [`nf_flow_table_cleanup()`][cleanup], which tears down the flows whose input device in either direction is that device ([`nf_flow_table_do_cleanup()`][docleanup]); a VLAN, PPPoE or bridge device going down doesn't match, because the recorded input device is the real one ([nf_flow_table_path.c:241][pathin]), except opposite an IPsec direction, where the route's own device is recorded as is ([nf_flow_table_path.c:31][pathdef]). Such a flow can still leave through the stale-route check if the direction keeps a route. Unbinding an indirect-block driver also tears down the flows of its device ([nf_flow_table_offload.c:1231][indrclean]). +- **A device going down.** The `NETDEV_DOWN` notifier ([nft_flow_offload.c:248][notifier]) calls [`nf_flow_table_cleanup()`][cleanup], which tears down the flows whose input device in either direction is that device ([`nf_flow_table_do_cleanup()`][docleanup]); a VLAN, PPPoE or bridge device going down doesn't match, because the recorded input device is the real one ([nf_flow_table_path.c:241][pathin]), except opposite an IPsec direction, where the route's own device is recorded as is ([nf_flow_table_path.c:31][pathdef]). Unregistering an indirect-block driver also tears down the flows of the device it was bound to ([nf_flow_table_offload.c:1231][indrclean]). - **Deleting the flowtable** ([`nf_flow_table_free()`][free]). ``` @@ -155,7 +155,7 @@ The garbage collector, [`nf_flow_offload_work_gc()`][gcwork], re-queues itself o │ ▼ removed by the garbage collector: in the same run if it did the teardown, - otherwise on its next run (about a second); hardware flows after the driver + otherwise within about a second; hardware flows after the driver delete completes; at once for software flows on device down, and for all flows on flowtable deletion ``` @@ -178,7 +178,9 @@ The comment in [`nf_flow_table_extend_ct_timeout()`][ext] calls the value "an ar **Hand-back to conntrack.** [`flow_offload_fixup_ct()`][fixup] runs at teardown. - **TCP state.** It keeps the last conntrack state, or sets close if the flow was closing, and clears the recorded maximum window and the max-ack flag; the code comment says that otherwise conntrack's TCP reset validation would fail ([`flow_offload_fixup_tcp()`][fixtcp]). -- **Timeout.** TCP and UDP flows have the conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired; a closing TCP flow gets the close timeout unconditionally ([nf_flow_table_core.c:250-252][corefix]). Except for that case the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64][tcp5d]), keeps the timeout it has left (a day or less, unless a packet of the flow went through conntrack while it was in the table) until a packet goes through conntrack again. Conntrack then sets a timeout for the TCP state of that packet ([nf_conntrack_proto_tcp.c:1325-1340][tcpstate2], [nf_conntrack_proto_tcp.c:1381][tcprefresh]), unless the entry has a fixed timeout ([`__nf_ct_refresh_acct()`][refreshacct], [nf_conntrack_core.c:2094-2096][refreshfix]). +- **Timeout.** TCP and UDP flows have the conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired; a closing TCP flow gets the close timeout unconditionally ([nf_flow_table_core.c:250-252][corefix]). +- **Established TCP.** Except for that case the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64][tcp5d]), keeps the timeout it has left (a day or less, unless a packet of the flow went through conntrack while it was in the table) until a packet goes through conntrack again. +- **Next conntrack packet.** Conntrack then sets a timeout for the TCP state of a packet it accepts ([nf_conntrack_proto_tcp.c:1325-1340][tcpstate2], [nf_conntrack_proto_tcp.c:1381][tcprefresh]), unless the entry has a fixed timeout ([`__nf_ct_refresh_acct()`][refreshacct], [nf_conntrack_core.c:2094-2096][refreshfix]). - **GRE.** GRE flows return before any timeout change ([nf_flow_table_core.c:240-241][coregre]). ## Hardware offload @@ -196,7 +198,7 @@ The driver interface is the tc flow-block interface: Behaviour with `flags offload`, from the source: - If the driver rejects `TC_SETUP_FT` ([`nf_flow_table_offload_cmd()`][offcmd]), or no indirect-block driver binds (`-EOPNOTSUPP`, [flow_offload.c:628][flowoffeop]), creating the flowtable or adding the device to it fails: the `setup` error is returned before the hook is registered ([nf_tables_api.c:9068][apisetup], [nf_tables_api.c:9104-9106][apiregall]). With a driver that accepts `TC_SETUP_FT` but registers no callback, such as `netdevsim`, the flowtable loads and no flow is added in hardware ([`flow_offload_rule_add()`][ruleadd] returns `-ENOENT` when nothing was added). -- A device that registers later is handled by the netdev notifier, which returns `NOTIFY_BAD` when registering the hook fails ([nf_tables_api.c:9908-9913][apinotif2]); `register_netdevice()` then unregisters the device ([dev.c:11778-11785][devreg]). A change of the `offload` flag on an existing flowtable is rejected with `-EOPNOTSUPP` ([nf_tables_api.c:9200-9203][apitoggle]). +- A device that registers later, or is renamed to a listed name, is handled by the netdev notifier, which returns `NOTIFY_BAD` when registering the hook fails ([nf_tables_api.c:9908-9913][apinotif2]); `register_netdevice()` then unregisters the device ([dev.c:11778-11785][devreg]). A change of the `offload` flag on an existing flowtable is rejected with `-EOPNOTSUPP` ([nf_tables_api.c:9200-9203][apitoggle]). - A direction that goes through a valid Ethernet device uses direct mode ([nf_flow_table_path.c:194-196][pathhw]), which caches no route unless the direction has a tunnel. A direction through a device that isn't a valid Ethernet device stays in neighbour mode (unless a PPPoE or bridge path puts it in direct mode), and an IPsec direction stays in xfrm mode; both cache their route ([nf_flow_table_core.c:143-147][corexfrm]). A flow with an IPsec direction is not added in hardware: building the rule fails for the xfrm transmit type ([nf_flow_table_offload.c:262-263][hwxfrm], [nf_flow_table_offload.c:873-883][hwalloc]). - A hardware add that fails isn't reported. The flow stays in software with `NF_FLOW_HW` set, `[HW_OFFLOAD]` is set if the driver accepted at least one direction ([`flow_offload_rule_add()`][ruleadd], [`flow_offload_work_add()`][workadd]), and the add is queued again on later software hits, at most once a second and not for closing flows ([nf_flow_table_core.c:373-383][corelimit]). - Flows through an IPIP or IPv6-in-IPv6 tunnel stay in software ([`nf_flow_offload_unsupported()`][unsup]; [`6c5dcab95f4c`][c6c5d], v7.2). @@ -205,7 +207,7 @@ Behaviour with `flags offload`, from the source: ## Inspecting - **Conntrack listing.** `conntrack -L` shows `[OFFLOAD]` for software flows and `[HW_OFFLOAD]` for hardware flows ([nf_flowtable.rst:111-113][rst111], [nf_flowtable.rst:202-206][rst202]). `/proc/net/nf_conntrack` (with `CONFIG_NF_CONNTRACK_PROCFS`, [nf_conntrack_standalone.c:34][kcprocfs]) prints the same tags ([nf_conntrack_standalone.c:364-366][proctag]). -- **Checking that a flow was added.** `[OFFLOAD]` follows `IPS_OFFLOAD_BIT`, which `flow add` sets ([nft_flow_offload.c:105][evalonce2]) and teardown clears ([nf_flow_table_core.c:401][coreclear]). `[HW_OFFLOAD]` follows `IPS_HW_OFFLOAD_BIT`, which the hardware add sets ([`flow_offload_work_add()`][workadd]) and the hardware delete clears ([nf_flow_table_offload.c:994][hwdel]). A flow without either tag is not being forwarded by the flowtable: it was never added (the reasons are in [How a flow gets in](#how-a-flow-gets-in)), or it has been torn down. +- **Checking that a flow was added.** `[OFFLOAD]` (shown when `[HW_OFFLOAD]` is not) follows `IPS_OFFLOAD_BIT`, which `flow add` sets ([nft_flow_offload.c:105][evalonce]) and teardown clears ([nf_flow_table_core.c:401][coreclear]). `[HW_OFFLOAD]` follows `IPS_HW_OFFLOAD_BIT`, which the hardware add sets ([`flow_offload_work_add()`][workadd]) and the hardware delete clears ([nf_flow_table_offload.c:994][hwdel]). A flow without either tag is not being forwarded by the flowtable: it was never added (the reasons are in [How a flow gets in](#how-a-flow-gets-in)), or it has been torn down. - **What offloaded entries omit.** ctnetlink skips the timeout and protocol info ([nf_conntrack_netlink.c:579][nlnk]), and `/proc/net/nf_conntrack` skips the timeout and the TCP state ([nf_conntrack_standalone.c:339][procnf], [`tcp_print_conntrack()`][tcpprint]). - **Statistics.** With `CONFIG_NF_FLOW_TABLE_PROCFS` ([Kconfig:721][kconfig]), [`/proc/net/stat/nf_flowtable`][procfs] shows per-CPU counters of pending hardware add, delete and stats work; the file is created at [nf_flow_table_procfs.c:71][procfscreate] and its show function prints only those three columns. They are incremented when work is queued and decremented when it completes ([nf_flow_table_offload.c:1047-1077][statinc]). There are no hit or miss counters. @@ -222,7 +224,8 @@ Behaviour with `flags offload`, from the source: The design was posted to netfilter-devel by Pablo Neira Ayuso. LWN reproduces two of the postings, and the kernel documentation cites them ([nf_flowtable.rst:224-235][rst224]). -- **3 November 2017**, [`[PATCH RFC,WIP 0/5] Flow offload infrastructure`][lwn1] (Message-ID `<20171103152636.9967-1-pablo@netfilter.org>`). The cover letter describes a module with a hook at ingress, populated by an nftables action once a flow is established. It gives the hardware motivation: "Switches and NICs come with built-in flow table, I've been observing out of tree patches in OpenWRT/LEDE to integrate this into Netfilter for a little while." It reports a measurement: "I'm measuring here that the software flow table forwarding path is 2.5 faster than the classic forwarding path in my testbed." Its TODO list included IPv4 only, IPv4 SNAT only, no netns support and no flow counters, and the last patch added `ndo` hooks to populate a hardware flow table. +- **3 November 2017**, [`[PATCH RFC,WIP 0/5] Flow offload infrastructure`][lwn1] (Message-ID `<20171103152636.9967-1-pablo@netfilter.org>`). The cover letter describes a module with a hook at ingress, populated by an nftables action once a flow is established. It gives the hardware motivation: "Switches and NICs come with built-in flow table, I've been observing out of tree patches in OpenWRT/LEDE to integrate this into Netfilter for a little while." It reports a measurement: "I'm measuring here that the software flow table forwarding path is 2.5 faster than the classic forwarding path in my testbed." + Its TODO list included IPv4 only, IPv4 SNAT only, no netns support and no flow counters, and the last patch added `ndo` hooks to populate a hardware flow table. - **22 December 2017**, [`[PATCH nf-next,v3 0/7] Flow offload infrastructure`][lwn2] (Message-ID `<20171222192732.13188-1-pablo@netfilter.org>`). The cover letter lists IPv6 and mixed IPv4/IPv6 support, hardware offload in a separate `nf_flow_table_hw` module, port translation and netns support. The hardware patch was tagged RFC, and the cover letter says "I will keep this one back until there's an initial driver that introduces flow table offload support". The software flow table was committed on 8 January 2018 ([`ac2a66665e23`][cac2a] adds the generic infrastructure and [`a3c90f7a2323`][ca3c9] the nftables expression) and first appeared in v4.16. Felix Fietkau reorganised the code in a series written in February 2018 and merged for v4.18, for example in [`1a999d899bc2`][c1a99], which renamed `nf_flow_table.c` to `nf_flow_table_core.c`. @@ -328,7 +331,6 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [evalcheck]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n98 [evalconf]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n102 [evalonce]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n105 -[evalonce2]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nft_flow_offload.c?h=v7.4-rc3#n105 [ext]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n517 [fillpath]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n45 [fixtcp]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n178 From 78c129c0a26079fb6b12c72aaaf5d3b432d769ee Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 20:48:52 -0800 Subject: [PATCH 18/22] net: correct flowtable rename, conntrack and tag wording State that a failed rename is rolled back, scope the conntrack refresh to packets treated as valid, fix a dangling reference after the bullet split, and extend the tag citation range. --- docs/net/flowtable.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index ce4052cb..a70442ea 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -134,7 +134,7 @@ The garbage collector, [`nf_flow_offload_work_gc()`][gcwork], re-queues itself o - **A dying conntrack entry**, checked by `nf_ct_is_dying()` ([`nf_flow_offload_gc_step()`][gcstep]); software lookups also miss ([`flow_offload_lookup()`][lookup]). - **A stale route**, for a direction that keeps one. The collector tears the flow down if either direction's route is stale (since v7.3, [`edd51a233438`][cedd51]); the hook checks the packet's direction ([`nf_flow_offload_forward()`][forward]). - **A missing output device, or a neighbour lookup error.** The hook tears the flow down and drops that packet ([nf_flow_table_ip.c:870-880][neigherr] in neighbour mode, [nf_flow_table_ip.c:885-889][neighdir] in direct mode for a missing device; the IPv6 lookup is [`ip_neigh_gw6()`][neigh6]). A missing neighbour entry is created by the lookup ([`ip_neigh_gw4()`][neigh4]), so only a failure to create it counts. -- **A device going down.** The `NETDEV_DOWN` notifier ([nft_flow_offload.c:248][notifier]) calls [`nf_flow_table_cleanup()`][cleanup], which tears down the flows whose input device in either direction is that device ([`nf_flow_table_do_cleanup()`][docleanup]); a VLAN, PPPoE or bridge device going down doesn't match, because the recorded input device is the real one ([nf_flow_table_path.c:241][pathin]), except opposite an IPsec direction, where the route's own device is recorded as is ([nf_flow_table_path.c:31][pathdef]). Unregistering an indirect-block driver also tears down the flows of the device it was bound to ([nf_flow_table_offload.c:1231][indrclean]). +- **A device going down.** The `NETDEV_DOWN` notifier ([nft_flow_offload.c:248][notifier]) calls [`nf_flow_table_cleanup()`][cleanup], which tears down the flows whose input device in either direction is that device ([`nf_flow_table_do_cleanup()`][docleanup]); a VLAN, PPPoE or bridge device going down doesn't match, because the recorded input device is the real one ([nf_flow_table_path.c:241][pathin]), except opposite an IPsec direction, where the route's own device is recorded as is ([nf_flow_table_path.c:31][pathdef]). Unregistering an indirect-block driver also tears down the flows whose input device in either direction is the device it was bound to ([nf_flow_table_offload.c:1231][indrclean]). - **Deleting the flowtable** ([`nf_flow_table_free()`][free]). ``` @@ -179,8 +179,8 @@ The comment in [`nf_flow_table_extend_ct_timeout()`][ext] calls the value "an ar - **TCP state.** It keeps the last conntrack state, or sets close if the flow was closing, and clears the recorded maximum window and the max-ack flag; the code comment says that otherwise conntrack's TCP reset validation would fail ([`flow_offload_fixup_tcp()`][fixtcp]). - **Timeout.** TCP and UDP flows have the conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired; a closing TCP flow gets the close timeout unconditionally ([nf_flow_table_core.c:250-252][corefix]). -- **Established TCP.** Except for that case the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64][tcp5d]), keeps the timeout it has left (a day or less, unless a packet of the flow went through conntrack while it was in the table) until a packet goes through conntrack again. -- **Next conntrack packet.** Conntrack then sets a timeout for the TCP state of a packet it accepts ([nf_conntrack_proto_tcp.c:1325-1340][tcpstate2], [nf_conntrack_proto_tcp.c:1381][tcprefresh]), unless the entry has a fixed timeout ([`__nf_ct_refresh_acct()`][refreshacct], [nf_conntrack_core.c:2094-2096][refreshfix]). +- **Established TCP.** Except for a closing TCP flow, the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64][tcp5d]), keeps the timeout it has left (a day or less, unless a packet of the flow went through conntrack while it was in the table) until a packet goes through conntrack again. +- **Next conntrack packet.** Conntrack then sets a timeout for the TCP state of a packet it treats as valid ([nf_conntrack_proto_tcp.c:1325-1340][tcpstate2], [nf_conntrack_proto_tcp.c:1381][tcprefresh]), unless the entry has a fixed timeout ([`__nf_ct_refresh_acct()`][refreshacct], [nf_conntrack_core.c:2094-2096][refreshfix]). - **GRE.** GRE flows return before any timeout change ([nf_flow_table_core.c:240-241][coregre]). ## Hardware offload @@ -198,7 +198,7 @@ The driver interface is the tc flow-block interface: Behaviour with `flags offload`, from the source: - If the driver rejects `TC_SETUP_FT` ([`nf_flow_table_offload_cmd()`][offcmd]), or no indirect-block driver binds (`-EOPNOTSUPP`, [flow_offload.c:628][flowoffeop]), creating the flowtable or adding the device to it fails: the `setup` error is returned before the hook is registered ([nf_tables_api.c:9068][apisetup], [nf_tables_api.c:9104-9106][apiregall]). With a driver that accepts `TC_SETUP_FT` but registers no callback, such as `netdevsim`, the flowtable loads and no flow is added in hardware ([`flow_offload_rule_add()`][ruleadd] returns `-ENOENT` when nothing was added). -- A device that registers later, or is renamed to a listed name, is handled by the netdev notifier, which returns `NOTIFY_BAD` when registering the hook fails ([nf_tables_api.c:9908-9913][apinotif2]); `register_netdevice()` then unregisters the device ([dev.c:11778-11785][devreg]). A change of the `offload` flag on an existing flowtable is rejected with `-EOPNOTSUPP` ([nf_tables_api.c:9200-9203][apitoggle]). +- A device that registers later, or is renamed to a listed name, is handled by the netdev notifier, which returns `NOTIFY_BAD` when registering the hook fails ([nf_tables_api.c:9906-9913][apinotif2]). `register_netdevice()` then unregisters a new device ([dev.c:11778-11785][devreg]), and a failed rename is rolled back ([dev.c:1569-1583][renameroll]). A change of the `offload` flag on an existing flowtable is rejected with `-EOPNOTSUPP` ([nf_tables_api.c:9200-9203][apitoggle]). - A direction that goes through a valid Ethernet device uses direct mode ([nf_flow_table_path.c:194-196][pathhw]), which caches no route unless the direction has a tunnel. A direction through a device that isn't a valid Ethernet device stays in neighbour mode (unless a PPPoE or bridge path puts it in direct mode), and an IPsec direction stays in xfrm mode; both cache their route ([nf_flow_table_core.c:143-147][corexfrm]). A flow with an IPsec direction is not added in hardware: building the rule fails for the xfrm transmit type ([nf_flow_table_offload.c:262-263][hwxfrm], [nf_flow_table_offload.c:873-883][hwalloc]). - A hardware add that fails isn't reported. The flow stays in software with `NF_FLOW_HW` set, `[HW_OFFLOAD]` is set if the driver accepted at least one direction ([`flow_offload_rule_add()`][ruleadd], [`flow_offload_work_add()`][workadd]), and the add is queued again on later software hits, at most once a second and not for closing flows ([nf_flow_table_core.c:373-383][corelimit]). - Flows through an IPIP or IPv6-in-IPv6 tunnel stay in software ([`nf_flow_offload_unsupported()`][unsup]; [`6c5dcab95f4c`][c6c5d], v7.2). @@ -206,7 +206,7 @@ Behaviour with `flags offload`, from the source: ## Inspecting -- **Conntrack listing.** `conntrack -L` shows `[OFFLOAD]` for software flows and `[HW_OFFLOAD]` for hardware flows ([nf_flowtable.rst:111-113][rst111], [nf_flowtable.rst:202-206][rst202]). `/proc/net/nf_conntrack` (with `CONFIG_NF_CONNTRACK_PROCFS`, [nf_conntrack_standalone.c:34][kcprocfs]) prints the same tags ([nf_conntrack_standalone.c:364-366][proctag]). +- **Conntrack listing.** `conntrack -L` shows `[OFFLOAD]` for software flows and `[HW_OFFLOAD]` for hardware flows ([nf_flowtable.rst:111-113][rst111], [nf_flowtable.rst:202-206][rst202]). `/proc/net/nf_conntrack` (with `CONFIG_NF_CONNTRACK_PROCFS`, [nf_conntrack_standalone.c:34][kcprocfs]) prints the same tags ([nf_conntrack_standalone.c:364-367][proctag]). - **Checking that a flow was added.** `[OFFLOAD]` (shown when `[HW_OFFLOAD]` is not) follows `IPS_OFFLOAD_BIT`, which `flow add` sets ([nft_flow_offload.c:105][evalonce]) and teardown clears ([nf_flow_table_core.c:401][coreclear]). `[HW_OFFLOAD]` follows `IPS_HW_OFFLOAD_BIT`, which the hardware add sets ([`flow_offload_work_add()`][workadd]) and the hardware delete clears ([nf_flow_table_offload.c:994][hwdel]). A flow without either tag is not being forwarded by the flowtable: it was never added (the reasons are in [How a flow gets in](#how-a-flow-gets-in)), or it has been torn down. - **What offloaded entries omit.** ctnetlink skips the timeout and protocol info ([nf_conntrack_netlink.c:579][nlnk]), and `/proc/net/nf_conntrack` skips the timeout and the TCP state ([nf_conntrack_standalone.c:339][procnf], [`tcp_print_conntrack()`][tcpprint]). - **Statistics.** With `CONFIG_NF_FLOW_TABLE_PROCFS` ([Kconfig:721][kconfig]), [`/proc/net/stat/nf_flowtable`][procfs] shows per-CPU counters of pending hardware add, delete and stats work; the file is created at [nf_flow_table_procfs.c:71][procfscreate] and its show function prints only those three columns. They are incremented when work is queued and decremented when it completes ([nf_flow_table_offload.c:1047-1077][statinc]). There are no hit or miss counters. @@ -224,8 +224,7 @@ Behaviour with `flags offload`, from the source: The design was posted to netfilter-devel by Pablo Neira Ayuso. LWN reproduces two of the postings, and the kernel documentation cites them ([nf_flowtable.rst:224-235][rst224]). -- **3 November 2017**, [`[PATCH RFC,WIP 0/5] Flow offload infrastructure`][lwn1] (Message-ID `<20171103152636.9967-1-pablo@netfilter.org>`). The cover letter describes a module with a hook at ingress, populated by an nftables action once a flow is established. It gives the hardware motivation: "Switches and NICs come with built-in flow table, I've been observing out of tree patches in OpenWRT/LEDE to integrate this into Netfilter for a little while." It reports a measurement: "I'm measuring here that the software flow table forwarding path is 2.5 faster than the classic forwarding path in my testbed." - Its TODO list included IPv4 only, IPv4 SNAT only, no netns support and no flow counters, and the last patch added `ndo` hooks to populate a hardware flow table. +- **3 November 2017**, [`[PATCH RFC,WIP 0/5] Flow offload infrastructure`][lwn1] (Message-ID `<20171103152636.9967-1-pablo@netfilter.org>`). The cover letter describes a module with a hook at ingress, populated by an nftables action once a flow is established. It gives the hardware motivation: "Switches and NICs come with built-in flow table, I've been observing out of tree patches in OpenWRT/LEDE to integrate this into Netfilter for a little while." It reports a measurement: "I'm measuring here that the software flow table forwarding path is 2.5 faster than the classic forwarding path in my testbed." Its TODO list included IPv4 only, IPv4 SNAT only, no netns support and no flow counters, and the last patch added `ndo` hooks to populate a hardware flow table. - **22 December 2017**, [`[PATCH nf-next,v3 0/7] Flow offload infrastructure`][lwn2] (Message-ID `<20171222192732.13188-1-pablo@netfilter.org>`). The cover letter lists IPv6 and mixed IPv4/IPv6 support, hardware offload in a separate `nf_flow_table_hw` module, port translation and netns support. The hardware patch was tagged RFC, and the cover letter says "I will keep this one back until there's an initial driver that introduces flow table offload support". The software flow table was committed on 8 January 2018 ([`ac2a66665e23`][cac2a] adds the generic infrastructure and [`a3c90f7a2323`][ca3c9] the nftables expression) and first appeared in v4.16. Felix Fietkau reorganised the code in a series written in February 2018 and merged for v4.18, for example in [`1a999d899bc2`][c1a99], which renamed `nf_flow_table.c` to `nf_flow_table_core.c`. @@ -417,6 +416,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [refresh]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n367 [refreshacct]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_core.c?h=v7.4-rc3#n2089 [refreshfix]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_core.c?h=v7.4-rc3#n2094 +[renameroll]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/core/dev.c?h=v7.4-rc3#n1569 [route]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n272 [route2984]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/ipv4/route.c?h=v7.4-rc3#n2984 [routefl]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_path.c?h=v7.4-rc3#n280 From bec86dcaff351cdc308f980fdd405ecffa338197 Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 20:50:27 -0800 Subject: [PATCH 19/22] net: scope the conntrack timeout refresh wording on flowtable page Say that the TCP tracker normally sets a timeout and that packets returning earlier set none, and anchor the notifier citation at its first line. --- docs/net/flowtable.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index a70442ea..69618acf 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -180,7 +180,7 @@ The comment in [`nf_flow_table_extend_ct_timeout()`][ext] calls the value "an ar - **TCP state.** It keeps the last conntrack state, or sets close if the flow was closing, and clears the recorded maximum window and the max-ack flag; the code comment says that otherwise conntrack's TCP reset validation would fail ([`flow_offload_fixup_tcp()`][fixtcp]). - **Timeout.** TCP and UDP flows have the conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired; a closing TCP flow gets the close timeout unconditionally ([nf_flow_table_core.c:250-252][corefix]). - **Established TCP.** Except for a closing TCP flow, the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64][tcp5d]), keeps the timeout it has left (a day or less, unless a packet of the flow went through conntrack while it was in the table) until a packet goes through conntrack again. -- **Next conntrack packet.** Conntrack then sets a timeout for the TCP state of a packet it treats as valid ([nf_conntrack_proto_tcp.c:1325-1340][tcpstate2], [nf_conntrack_proto_tcp.c:1381][tcprefresh]), unless the entry has a fixed timeout ([`__nf_ct_refresh_acct()`][refreshacct], [nf_conntrack_core.c:2094-2096][refreshfix]). +- **Next conntrack packet.** When a packet goes through conntrack again, the TCP tracker normally sets a timeout for the TCP state ([nf_conntrack_proto_tcp.c:1325-1340][tcpstate2], [nf_conntrack_proto_tcp.c:1381][tcprefresh]), unless the entry has a fixed timeout ([`__nf_ct_refresh_acct()`][refreshacct], [nf_conntrack_core.c:2094-2096][refreshfix]). Packets that return earlier, such as invalid or ignored ones, set none. - **GRE.** GRE flows return before any timeout change ([nf_flow_table_core.c:240-241][coregre]). ## Hardware offload @@ -271,7 +271,7 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [add]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n335 [airoha]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/airoha/airoha_eth.c?h=v7.4-rc3#n3674 [alloc]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n53 -[apinotif2]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9908 +[apinotif2]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9906 [apiregall]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9104 [apisetup]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9068 [apitoggle]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_tables_api.c?h=v7.4-rc3#n9200 From aaba446919ef8ffd3f041fbdb113474cb4e25532 Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 20:51:26 -0800 Subject: [PATCH 20/22] net: state only what holds for the conntrack timeout refresh Say that the TCP tracker can set the timeout again when conntrack processes another packet, and keep the fixed-timeout exception. --- docs/net/flowtable.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index 69618acf..1b07d3bf 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -180,7 +180,7 @@ The comment in [`nf_flow_table_extend_ct_timeout()`][ext] calls the value "an ar - **TCP state.** It keeps the last conntrack state, or sets close if the flow was closing, and clears the recorded maximum window and the max-ack flag; the code comment says that otherwise conntrack's TCP reset validation would fail ([`flow_offload_fixup_tcp()`][fixtcp]). - **Timeout.** TCP and UDP flows have the conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired; a closing TCP flow gets the close timeout unconditionally ([nf_flow_table_core.c:250-252][corefix]). - **Established TCP.** Except for a closing TCP flow, the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64][tcp5d]), keeps the timeout it has left (a day or less, unless a packet of the flow went through conntrack while it was in the table) until a packet goes through conntrack again. -- **Next conntrack packet.** When a packet goes through conntrack again, the TCP tracker normally sets a timeout for the TCP state ([nf_conntrack_proto_tcp.c:1325-1340][tcpstate2], [nf_conntrack_proto_tcp.c:1381][tcprefresh]), unless the entry has a fixed timeout ([`__nf_ct_refresh_acct()`][refreshacct], [nf_conntrack_core.c:2094-2096][refreshfix]). Packets that return earlier, such as invalid or ignored ones, set none. +- **Next conntrack packet.** When conntrack processes another packet of the flow, its TCP tracker can set the timeout again; the choice is made in [nf_conntrack_proto_tcp.c:1325-1340][tcpstate2] and applied at [nf_conntrack_proto_tcp.c:1381][tcprefresh]. Entries with a fixed timeout are left alone ([`__nf_ct_refresh_acct()`][refreshacct], [nf_conntrack_core.c:2094-2096][refreshfix]). - **GRE.** GRE flows return before any timeout change ([nf_flow_table_core.c:240-241][coregre]). ## Hardware offload From c82e16e308cf730309bcbf3b651b01fd1c6568f6 Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 20:52:30 -0800 Subject: [PATCH 21/22] net: make the conntrack timeout refresh sentence an example Say the TCP tracker can set the timeout again, for example at the refresh, and scope the fixed-timeout exception to that refresh. --- docs/net/flowtable.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index 1b07d3bf..186306ed 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -180,7 +180,7 @@ The comment in [`nf_flow_table_extend_ct_timeout()`][ext] calls the value "an ar - **TCP state.** It keeps the last conntrack state, or sets close if the flow was closing, and clears the recorded maximum window and the max-ack flag; the code comment says that otherwise conntrack's TCP reset validation would fail ([`flow_offload_fixup_tcp()`][fixtcp]). - **Timeout.** TCP and UDP flows have the conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired; a closing TCP flow gets the close timeout unconditionally ([nf_flow_table_core.c:250-252][corefix]). - **Established TCP.** Except for a closing TCP flow, the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64][tcp5d]), keeps the timeout it has left (a day or less, unless a packet of the flow went through conntrack while it was in the table) until a packet goes through conntrack again. -- **Next conntrack packet.** When conntrack processes another packet of the flow, its TCP tracker can set the timeout again; the choice is made in [nf_conntrack_proto_tcp.c:1325-1340][tcpstate2] and applied at [nf_conntrack_proto_tcp.c:1381][tcprefresh]. Entries with a fixed timeout are left alone ([`__nf_ct_refresh_acct()`][refreshacct], [nf_conntrack_core.c:2094-2096][refreshfix]). +- **Next conntrack packet.** When conntrack processes another packet of a TCP flow, its TCP tracker can set the timeout again, for example with the refresh at [nf_conntrack_proto_tcp.c:1381][tcprefresh]. That refresh leaves entries with a fixed timeout alone ([`__nf_ct_refresh_acct()`][refreshacct], [nf_conntrack_core.c:2094-2096][refreshfix]). - **GRE.** GRE flows return before any timeout change ([nf_flow_table_core.c:240-241][coregre]). ## Hardware offload @@ -455,7 +455,6 @@ The kernel documentation was last updated with new content in March 2021 (v5.13, [tcpoff]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n1658 [tcpprint]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n266 [tcprefresh]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n1381 -[tcpstate2]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_conntrack_proto_tcp.c?h=v7.4-rc3#n1325 [tcpto]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n479 [tear]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_core.c?h=v7.4-rc3#n399 [ttl]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/net/netfilter/nf_flow_table_ip.c?h=v7.4-rc3#n253 From a08ac70c91e7e40117a1a1113457179e3c5d5283 Mon Sep 17 00:00:00 2001 From: Laveesh Bansal Date: Sat, 28 Nov 2026 20:53:16 -0800 Subject: [PATCH 22/22] net: say the refresh leaves a fixed timeout unchanged --- docs/net/flowtable.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/net/flowtable.md b/docs/net/flowtable.md index 186306ed..0b70ab96 100644 --- a/docs/net/flowtable.md +++ b/docs/net/flowtable.md @@ -180,7 +180,7 @@ The comment in [`nf_flow_table_extend_ct_timeout()`][ext] calls the value "an ar - **TCP state.** It keeps the last conntrack state, or sets close if the flow was closing, and clears the recorded maximum window and the max-ack flag; the code comment says that otherwise conntrack's TCP reset validation would fail ([`flow_offload_fixup_tcp()`][fixtcp]). - **Timeout.** TCP and UDP flows have the conntrack timeout lowered to the normal value for their state, less the flowtable timeout if the flow expired; a closing TCP flow gets the close timeout unconditionally ([nf_flow_table_core.c:250-252][corefix]). - **Established TCP.** Except for a closing TCP flow, the timeout is only lowered, so an established TCP entry, whose default timeout is five days ([nf_conntrack_proto_tcp.c:64][tcp5d]), keeps the timeout it has left (a day or less, unless a packet of the flow went through conntrack while it was in the table) until a packet goes through conntrack again. -- **Next conntrack packet.** When conntrack processes another packet of a TCP flow, its TCP tracker can set the timeout again, for example with the refresh at [nf_conntrack_proto_tcp.c:1381][tcprefresh]. That refresh leaves entries with a fixed timeout alone ([`__nf_ct_refresh_acct()`][refreshacct], [nf_conntrack_core.c:2094-2096][refreshfix]). +- **Next conntrack packet.** When conntrack processes another packet of a TCP flow, its TCP tracker can set the timeout again, for example with the refresh at [nf_conntrack_proto_tcp.c:1381][tcprefresh]. That refresh does not change the timeout of entries with a fixed timeout ([`__nf_ct_refresh_acct()`][refreshacct], [nf_conntrack_core.c:2094-2096][refreshfix]). - **GRE.** GRE flows return before any timeout change ([nf_flow_table_core.c:240-241][coregre]). ## Hardware offload