Repository navigation
Expand file tree
/
Copy pathinit.lua
More file actions
149 lines (134 loc) · 6.11 KB
/
Copy pathinit.lua
File metadata and controls
149 lines (134 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
--
-- SPDX-FileCopyrightText: (c) 2026 Ring Zero Desenvolvimento de Software LTDA
-- SPDX-License-Identifier: MIT OR GPL-2.0-only
--
---
-- Compiles a Lua program file into a BPF ELF object.
--
-- A program file's body is compile-time Lua: `lunatic` compiles the file, and the body runs once,
-- on the host, from that bytecode in the CLI's Lua, and hands the functions it declares to a
-- program constructor. Those functions are the program: `compile` reads their bytecode, lowers it,
-- and returns an object `bpftool prog load` accepts. Anything the subset refuses is an error
-- naming the Lua file and line. `lunatik bpf` calls `compile` and writes the object, which
-- `lunatik run --dev` loads and attaches; the guide page *Compiling Lua to eBPF* says what a
-- program file may hold, what a program returns and when it stops.
--
-- The compiler compiles for the kernel `lunatik.config` describes, which `make` writes from the
-- tree Lunatik is built against: that kernel's version and byte order. It reads that kernel's types
-- from the BTF file it is handed, or from `/sys/kernel/btf/vmlinux`, the running kernel's, and the
-- kernel scripts the body requires from the directory it is handed, or from `/lib/modules/lua`, so
-- a build host compiles for the kernel an image ships when `make` was handed that kernel's tree and
-- the compiler its BTF and the image's kernel scripts.
--
-- Experimental: the compiler, `lunatik bpf`, `lunatik run --dev` and the modules a program file's
-- body requires may change in a minor release.
-- @module luaebpf
-- @usage
-- local luaebpf = require("luaebpf")
--
-- local object = luaebpf.compile("filter.bpf.lua")
-- local image = luaebpf.compile("filter.bpf.lua", "build/vmlinux.btf", "staging/lib/modules/lua")
local LIBPATH <const> = select(2, ...):match("^(.*/)") .. "lib/?.lua"
-- compile opens the BTF a body reads its layouts from, so both reach one bpf.vmlinux, loaded from LIBPATH
local function fromlib(name)
return dofile(assert(package.searchpath(name, LIBPATH)))
end
package.preload["bpf.vmlinux"] = fromlib
local maps = require("luaebpf.maps")
local programs = require("luaebpf.programs")
local proto = require("luaebpf.proto")
local emit = require("luaebpf.emit")
local writer = require("luaebpf.writer")
local errors = require("luaebpf.errors")
local vmlinux = require("bpf.vmlinux")
local DROP <const> = "LUAEBPF_DROP"
-- the host's path, through which a body still reaches the compiler's own modules
local HOSTPATH <const> = package.path
local function own(name)
if name:find("^luaebpf%.") then
local path, err = package.searchpath(name, HOSTPATH)
if path == nil then
return err
end
return assert(loadfile(path)), path
end
end
-- the compiler's luaebpf.*, ahead of the kernel scripts' searcher
table.insert(package.searchers, #package.searchers, own)
local luaebpf = {}
local function modnames()
local names = {}
for modname in pairs(package.loaded) do
names[modname] = true
end
return names
end
local function unload(kept)
for modname in pairs(package.loaded) do
if not kept[modname] then
package.loaded[modname] = nil
end
end
end
local function name(program, chunk)
return program.name or (chunk:match("([^/]+)$"):gsub("%.bpf%.lua$", ""):gsub("%.lua$", ""))
end
---
-- Compiles the program file at `path`. A module an earlier call's body loaded is loaded again, so
-- this call's body reads the kernel scripts it names.
-- @function luaebpf.compile
-- @tparam string path the program file, usually named `<something>.bpf.lua`
-- @tparam[opt] string btffile the kernel's BTF as a raw BTF file, the bytes
-- `/sys/kernel/btf/vmlinux` serves, read once a layout is asked for; that file when absent
-- @tparam[opt] string scripts the directory the body's `require` reads the kernel scripts from, laid
-- out as `/lib/modules/lua` is; that directory when absent
-- @treturn string the BPF ELF object
-- @treturn table a line per call into the kernel Lua runtime the compiler met, in that order,
-- `<file>:<line>: calls the runtime '<name>'`
-- @raise `<file>:<line>: <reason>` for every construct the subset refuses,
-- `<path>: no program declared` when the body hands nothing to a constructor,
-- `<btffile>: <reason>`, `<btffile> carries no BTF header` and `<btffile> is shorter than its
-- BTF header says` when a layout is asked of a file that cannot be read, holds no BTF or is cut
-- short, and `<btffile> is a <order>-endian BTF, for a <order>-endian object` when the BTF a
-- layout was read from is not in the object's byte order
function luaebpf.compile(path, btffile, scripts)
local chunk = proto.load(path)
programs.reset()
maps.reset()
vmlinux.open(btffile)
proto.scripts(scripts)
local host, cpath, kept = package.path, package.cpath, modnames()
package.path, package.cpath = LIBPATH, "" -- no host module, Lua or C, stands in for a kernel script
local ok, message = pcall(chunk)
package.path, package.cpath = host, cpath
unload(kept)
if not ok then
error(message, 0)
end
local declared = programs.declared()
if #declared == 0 then
error(string.format("%s: no program declared", path), 0)
end
for _, program in ipairs(declared) do
program.name = name(program, path)
end
local object, calls = writer.write(declared, os.getenv(DROP))
vmlinux.check(emit.bigendian)
return object, calls
end
---
-- Reads back the errors of a compiled program `lunatik run` attached, from the maps it pinned under
-- the script's root: how many times the program stopped at each place, a line a stop logs.
-- @function luaebpf.errors
-- @tparam string root the script's root, `/sys/fs/bpf/lunatik/<script>`
-- @treturn table|nil a table per place, `{file, line, reason, count}`, in the order the compiler met
-- the places, the count summed over the CPUs; an empty array where the root pins no
-- `luaebpf_errors`, as a program that stops nowhere declares none
-- @treturn string the message, where a pin cannot be read
-- @usage
-- local places = assert(luaebpf.errors("/sys/fs/bpf/lunatik/noudp"))
-- for _, place in ipairs(places) do
-- print(place.count, place.file, place.line, place.reason) --> 3 noudp.bpf.lua 8 out of bounds
-- end
luaebpf.errors = errors.read
return luaebpf