luaebpf

bpf.tcModule

Declares a TC program in a program file.

The body of a program file runs on the host under lunatik bpf; the function it hands to program is compiled to eBPF and runs in the kernel on each packet the device receives, or sends. It takes one argument, the context, and returns a verdict, a value of linux.tc.action, as a kernel script’s tc callback does: OK, SHOT and REDIRECT act, and the rest decides nothing. Where it computes a verdict that is no action, and where Lua would raise, at a read past the packet or a division by 0, the program stops and returns TC_ACT_SHOT, which drops the packet, since TC has no verdict for a program error, and logs the error Lua raises there, <file>:<line>: <reason>, to the kernel’s trace ring through bpf_trace_printk, ten lines every five seconds at most, and counts every stop at that line, which lunatik bpf --errors reports; where it returns no value, it returns the default verdict its constructor names. A verdict the compiler holds that is no action, and a boolean, are refused, “the verdict 9 is not a TC action” and “the verdict may be a boolean here; return an action”.

The program’s argument is spelled as the kernel’s skb object: #skb is the packet’s length, skb:ifindex() its device’s index, skb:hash() its flow hash and skb:priority() its priority, which skb:priority(v) sets to a value from 0 up to U32_MAX, returning it. A value outside them is refused where the compiler holds it, and stops the program where the program computes it. Any other method is refused, skb:mark() among them. skb.data/skb.data_end are the packet’s bounds rather than numbers, and skb:data() is the whole packet from skb->data, #skb bytes long, read as ctx:packet() is in bpf.xdp whether the skb is linear or not; a layer is refused.

A call into a kernel Lua runtime lunatik.runtime names, r:run(a, b), lowers to bpf_luatc_run: it answers the verdict the callback tc.attach registered returns, or nil, and the program tests the answer before it reads it. The kernel loads a program that calls it, or that may stop, only under a license it counts as GPL-compatible.

Usage

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

return tc.program(function(skb)
    if #skb > 1500 then
        skb:priority(1)
    end
    return action.OK
end, {egress = true, default = action.OK})

Functions

bpf.tc.program (fn, opts) Declares fn as a TC program.

Functions in detail

bpf.tc.program (fn, opts)
Declares fn as a TC program.

Parameters

  • fn function the program; it takes the context and returns a verdict.
  • opts table egress, which attaches the program on the way out rather than in, default, required, the verdict the program returns where its function returns no value, an action, name, what the program is called in the object, cut to the fifteen characters BPF_OBJ_NAME_LEN leaves and made unique against the names already taken, and license, a string, the license the object declares for every program of the file, Dual MIT/GPL where it names none.

Returns

    table the declared program

Raises

tc.program takes a Lua function, or <file>:<line>: tc.program's default verdict is not an integer, which a missing default raises too, or is not a TC action, or <file>:<line>: tc.program's license is not a string or '<license>' is not '<before>', the license of the programs before it, the line where fn is defined