Skip to content

bpf.tc: a TC program sets its packet's mark - #71

Open
lneto wants to merge 1 commit into
masterfrom
tc-mark
Open

lneto wants to merge 1 commit into
masterfrom
tc-mark

Conversation

@lneto

@lneto lneto commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

A compiled TC program cannot set its packet's mark: bpf.tc refuses skb:mark() with "the context has no method 'mark'", though the kernel lets a TC program write the mark as it lets it write the priority.

A TC program now reads the mark with skb:mark() and sets it with skb:mark(v), which answers the value it stored, as skb:priority does and as the kernel's skb object spells the method. A program that picks a flow's path hands the choice to policy routing, an ip rule add fwmark 2 table 2, and one at tcx ingress leaves its verdict in the mark for the netfilter rules that run after it:

local tc     = require("bpf.tc")
local action = require("linux.tc").action

local PASSED <const> = 2

return tc.program(function(skb)
	skb:mark(PASSED)
	return action.OK
end, {default = action.OK})

The value is held to 0 up to 4294967295, as the kernel object holds it: one outside that range is refused at its line when the compiler knows it, 'mark' takes 0 to 4294967295, not -1, and stops the program when the program computes it, which returns TC_ACT_SHOT and logs "out of bounds". The guide lists the method beside skb:priority, and its example of a refused method becomes skb:connmark(), which the context still does not offer.

It needs no kernel past the 6.6 floor: the mark is a field of __sk_buff that 6.6 and 6.8 already let a TC program write. No example uses bpf.tc, so none is run for this change. The tests are in Lunatik's tests/luaebpf/ and come with the Lunatik pull request that moves its luaebpf/ submodule to this change and closes luaebpf#29.
#29, which it does not close

skb:mark() reads the packet's mark and skb:mark(v) sets it to a value
from 0 up to U32_MAX and answers it, as skb:priority does and as the
kernel's skb object spells the method (lib/luaskb.c). The context table
lowers it to a four-byte load and store of __sk_buff's mark, the field
tc_cls_act_is_valid_access lets a TC program write beside priority
(net/core/filter.c, read at 6.6 and 6.8): mark is a context field the
kernel already offers, so no helper or mechanism of the compiler's own is
written for it. A value outside the range is refused where the compiler
holds it and stops the program where the program computes it, through the
store priority already takes.

A program that picks a flow's path hands that choice to policy routing,
an ip rule fwmark, and one at tcx ingress leaves its verdict for the
netfilter rules that run after it. The guide's example of a refused
method becomes skb:connmark(), which the context still does not offer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lneto lneto added complexity: small One line or one file, with a trivial or existing test workflow-reviewed reviewed end to end by a review workflow labels Nov 27, 2026
@lneto

lneto commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Note

This comment was written by an LLM, not a human.

What #29 requires, as the review read the change meeting it:

  • Met: "The need: skb:mark() reads the mark and skb:mark(v) sets it, as skb:priority does." (luaebpf#29, body) lib/bpf/tc.lua:76-77 adds mark = "mark" to methods and mark = true to writable. emit/context.lua contextcall/contextstore lower it as they lower priority: a 4-byte load and store at the field's BTF offset, a constant checked against 0..U32_MAX, and a computed value tested so the program stops outside it. The docs follow in tc.lua's LDoc, guide 08 and 09, api.md and compiler.md. Case: None in either tree yet. The Lunatik PR owes ctx.sh rows, for example "luaebpf: what skb:mark(v) stores is what ctx_out carries back", modelled on the priority row at ctx.sh:77. Measured by hand: ctx_out mark 5 to 12.
  • Met: "With skb:mark(v) the program leaves its verdict in the mark, and a rule after it matches that mark." (luaebpf#29, comment of 2026-11-26) The same store writes sk_buff.mark (6.8 filter.c:9586). sch_handle_ingress runs before the bridge's rx_handler in __netif_receive_skb_core (6.8 net/core/dev.c), and NF_BR_PRE_ROUTING runs from br_input.c, so the netfilter rule reads the mark the program set. Case: None yet: the netfilter-after-tcx case belongs to the Lunatik PR
  • Differs: "A test of it: a TC program sets a mark on the packets it passes, and a netfilter rule after it counts the packets that carry that mark." (luaebpf#29, comment of 2026-11-26) This diff has no test. luaebpf's AGENTS.md (Build, install, test) puts the compiler's tests in Lunatik's tests/luaebpf. The Lunatik PR that moves the luaebpf/ mount to this change carries the test and closes luaebpf: a TC program sets its packet's mark #29. This PR says "luaebpf: a TC program sets its packet's mark #29, which it does not close", so nothing closes narrowed. Case: No case can fail on this branch: the test lives in Lunatik and is not written yet

The matrix the change is held to:

This branch holds no test: luaebpf's AGENTS.md puts the compiler's tests in Lunatik's tests/luaebpf. I measured each case by hand, with the branch's tc.lua laid over the installed luaebpf and bpftool on aarch64 6.8. The test that would cover each case is a cell owed by the Lunatik PR.

skb:mark() read:

  • measured: a u32 load at __sk_buff +8, the offset BTF gives.
  • owed: a ctx.sh row reading back the ctx_in mark, and mark added to btfview.sh SKB_FIELDS.

skb:mark(v), v a constant in range:

  • measured: ctx_in mark 5 came back in ctx_out as 12, returned 0.
  • owed: a ctx.sh skbsetmark row read from ctx_out.

skb:mark(v), v computed in range:

  • measured: the store goes through fitsunsigned, the path priority uses.
  • owed: a ctx.sh row with the top bit set.

skb:mark(v), v computed out of range:

  • measured: -#skb returned 2 (TC_ACT_SHOT) and left the mark at 5.
  • owed: a ctx.sh row and its "out of bounds" log line.

skb:mark(-1), skb:mark(1<<32), skb:mark(true):

  • measured: refused at their line with the messages the guide quotes.
  • owed: ctx.sh rows.

skb:connmark():

  • measured: refused, "the context has no method 'connmark'".
  • owed: a ctx.sh row.

skb.mark = 1:

  • measured: refused, "the context has no field 'mark'".
  • owed: a ctx.sh row.

Second use (tcx ingress on a bridge port, a netfilter rule after it):

  • read in the 6.8 source: sch_handle_ingress runs before rx_handler in __netif_receive_skb_core, and NF_BR_PRE_ROUTING runs in br_input.c, so the mark reaches the rule.
  • owed: the test the issue's comment names. Nothing here ran it.

What this kernel cannot see: big-endian and x86 JIT lowering of the store. It is a BPF_W context access the verifier rewrites per architecture.

Examples: none uses bpf.tc (examples/filter and sniclassify are C), so none was run.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

complexity: small One line or one file, with a trivial or existing test workflow-reviewed reviewed end to end by a review workflow

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant