Skip to content

Commit 941d347

Browse files
committed
Make API more decent
Remove "bpf" prefixes from most functions and structs Provide API module for common structs and host-side operations Provide RT module for kernel-side structs and operations Provide Host module for host-side structs and operations Update to GPUCompiler 0.9 and Julia 1.6 Add root-only tests Add probe helpers Use 1-based indexing for maps
1 parent 99d2311 commit 941d347

19 files changed

+1027
-358
lines changed

FILTER_LICENSES.md

Lines changed: 0 additions & 20 deletions
This file was deleted.

Manifest.toml

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# This file is machine-generated - editing it directly is not advised
22

3+
[[ArgTools]]
4+
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"
5+
6+
[[Artifacts]]
7+
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
8+
39
[[Base64]]
410
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
511

@@ -32,24 +38,38 @@ uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"
3238
deps = ["Random", "Serialization", "Sockets"]
3339
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"
3440

41+
[[Downloads]]
42+
deps = ["ArgTools", "LibCURL", "NetworkOptions"]
43+
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
44+
3545
[[GPUCompiler]]
36-
deps = ["DataStructures", "InteractiveUtils", "LLVM", "Libdl", "TimerOutputs", "UUIDs"]
37-
path = "/home/jpsamaroo/.julia/dev/GPUCompiler-bpf/"
46+
deps = ["DataStructures", "InteractiveUtils", "LLVM", "Libdl", "Logging", "Scratch", "Serialization", "TimerOutputs", "UUIDs"]
47+
git-tree-sha1 = "80100dc01602c15fe1abf83b70bf2cd8961fb0e5"
48+
repo-rev = "master"
49+
repo-url = "https://github.com/JuliaGPU/GPUCompiler.jl.git"
3850
uuid = "61eb1bfa-7361-4325-ad38-22787b887f55"
39-
version = "0.7.1"
51+
version = "0.9.0"
4052

4153
[[InteractiveUtils]]
4254
deps = ["Markdown"]
4355
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
4456

4557
[[LLVM]]
4658
deps = ["CEnum", "Libdl", "Printf", "Unicode"]
47-
git-tree-sha1 = "70070a0131f17fcffc5fc004f5f73f037bd217c5"
59+
git-tree-sha1 = "000a737732aa4eb996414c4685368f6a74b41d14"
4860
uuid = "929cbde3-209d-540e-8aea-75f648917ca0"
49-
version = "3.2.0"
61+
version = "3.4.0"
62+
63+
[[LibCURL]]
64+
deps = ["LibCURL_jll", "MozillaCACerts_jll"]
65+
uuid = "b27032c2-a3e7-50c8-80cd-2d36dbcbfd21"
66+
67+
[[LibCURL_jll]]
68+
deps = ["Libdl"]
69+
uuid = "deac9b47-8bc7-5906-a0fe-35ac56dc84c0"
5070

5171
[[LibGit2]]
52-
deps = ["Printf"]
72+
deps = ["NetworkOptions", "Printf"]
5373
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"
5474

5575
[[Libdl]]
@@ -69,21 +89,27 @@ uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
6989
[[Mmap]]
7090
uuid = "a63ad114-7e13-5084-954f-fe012c677804"
7191

92+
[[MozillaCACerts_jll]]
93+
uuid = "14a3606d-f60d-562e-9121-12d972cd8159"
94+
95+
[[NetworkOptions]]
96+
uuid = "ca575930-c2e3-43a9-ace4-1e988b2c1908"
97+
7298
[[OrderedCollections]]
7399
git-tree-sha1 = "293b70ac1780f9584c89268a6e2a560d938a7065"
74100
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
75101
version = "1.3.0"
76102

77103
[[Pkg]]
78-
deps = ["Dates", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "UUIDs"]
104+
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs"]
79105
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
80106

81107
[[Printf]]
82108
deps = ["Unicode"]
83109
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"
84110

85111
[[REPL]]
86-
deps = ["InteractiveUtils", "Markdown", "Sockets"]
112+
deps = ["InteractiveUtils", "Markdown", "Sockets", "Unicode"]
87113
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
88114

89115
[[Random]]
@@ -93,6 +119,12 @@ uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
93119
[[SHA]]
94120
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
95121

122+
[[Scratch]]
123+
deps = ["Dates"]
124+
git-tree-sha1 = "ad4b278adb62d185bbcb6864dc24959ab0627bf6"
125+
uuid = "6c6a2e73-6563-6170-7368-637461726353"
126+
version = "1.0.3"
127+
96128
[[Serialization]]
97129
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
98130

@@ -111,8 +143,16 @@ uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
111143
deps = ["LinearAlgebra", "SparseArrays"]
112144
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
113145

146+
[[TOML]]
147+
deps = ["Dates"]
148+
uuid = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
149+
150+
[[Tar]]
151+
deps = ["ArgTools", "SHA"]
152+
uuid = "a4e569a6-e804-4fa4-b0f3-eef7a1d5b13e"
153+
114154
[[Test]]
115-
deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
155+
deps = ["InteractiveUtils", "Logging", "Random", "Serialization"]
116156
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
117157

118158
[[TimerOutputs]]

Project.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ LLVM = "929cbde3-209d-540e-8aea-75f648917ca0"
1010
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
1111

1212
[compat]
13-
GPUCompiler = "0.7"
14-
LLVM = "3"
15-
julia = "1.3"
13+
GPUCompiler = "0.9"
14+
LLVM = "3.4"
15+
julia = "1.6"
1616

1717
[extras]
1818
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,29 @@ wrappers to the [libbpf](https://github.com/libbpf/libbpf) library are provided
55
to make it easy to load eBPF programs into the Linux kernel in for a variety of
66
use cases.
77

8+
## Filter Licensing
9+
10+
Disclaimer: I am not a lawyer, so the following information may be completely
11+
incorrect. If you are a lawyer or have equivalent experience, and believe the
12+
following information to be misleading or incorrect, please file an issue/PR.
13+
14+
Because the Linux kernel exposes BPF helpers functions which are only
15+
available to GPL-licensed programs, BPFnative provides the option to allow BPF
16+
kernels to be generated under the GPL license, or whatever license is
17+
provided to `bpffunction()`. The subsequently generated kernel and
18+
source that generates it would then be considered to be licensed as
19+
specified. The default license is the empty string "", which may be construed
20+
to imply a lack of a license (proprietary).
21+
22+
BPFnative itself is of course just a compiler, so it may retain its MIT
23+
license. However, users should keep in mind that whatever license they specify
24+
to `bpffunction()` is the license that they must adhere to. This means
25+
that, for example, if a user were to generate a GPL-licensed BPF kernel with
26+
BPFnative's compiler, the user would be obligated to adhere to the terms of
27+
the GPL license, and specifically, would be required to provide the source
28+
code used to generate their BPF kernel, as well as distributing a copy or
29+
reference to the GPL license itself.
30+
831
## Acknowledgments
932
Thanks to @vchuravy for MCAnalyzer.jl, and @maleadt for CUDAnative.jl, both of
1033
which this package's code is derived from and inspired by! Also thanks to both

src/BPFnative.jl

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,34 @@ using InteractiveUtils
55
using Libdl
66
using GPUCompiler
77

8-
include("c_helpers.jl")
8+
# Host API
9+
module API
10+
include("common.jl")
911
include("libbpf.jl")
10-
include("maps.jl")
12+
end
13+
14+
# Runtime API
15+
module RT
16+
import ..API
17+
include("runtime_maps.jl")
18+
include("misc.jl")
19+
end
20+
21+
# Host API
22+
module Host
23+
import ..API
24+
include("syscall.jl")
25+
include("host_maps.jl")
26+
end
27+
28+
# Compiler
1129
include("compiler.jl")
12-
include("execution.jl")
1330
include("reflection.jl")
1431

15-
# Some useful utilities
32+
# Easy-to-use probes
33+
include("probes.jl")
34+
35+
# Useful utilities
1636
include("xdp.jl")
1737

1838
end # module

src/c_helpers.jl renamed to src/common.jl

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
module API
2-
31
export BPFCmd, BPFMapType, BPFProgType, BPFAttachType, BPFHelper
42

53
@enum BPFCmd begin
@@ -257,4 +255,35 @@ end
257255
sk_assign
258256
end
259257

258+
## Perf/Ptrace
259+
260+
# TODO: pt_regs for other architectures
261+
struct pt_regs
262+
ebx::Clong
263+
ecx::Clong
264+
edx::Clong
265+
esi::Clong
266+
edi::Clong
267+
ebp::Clong
268+
eax::Clong
269+
xds::Cint
270+
xes::Cint
271+
xfs::Cint
272+
xgs::Cint
273+
orig_eax::Clong
274+
eip::Clong
275+
xcs::Cint
276+
eflags::Clong
277+
esp::Clong
278+
xss::Cint
279+
end
280+
281+
## XDP
282+
283+
struct xdp_md
284+
data::UInt32
285+
data_end::UInt32
286+
data_meta::UInt32
287+
ingress_ifindex::UInt32
288+
rx_queue_index::UInt32
260289
end

src/compiler.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,51 @@
1+
export bpffunction, bpfasm
2+
3+
# Compilation support
4+
15
struct BPFCompilerParams <: AbstractCompilerParams end
26

37
BPFCompilerJob = CompilerJob{BPFCompilerTarget,BPFCompilerParams}
48

59
GPUCompiler.runtime_module(::BPFCompilerJob) = BPFnative
10+
11+
12+
## host-side API
13+
14+
"""
15+
bpffunction(f, tt=Tuple{}; kwargs...)
16+
17+
Compiles a function invocation into eBPF bytecode.
18+
19+
The following keyword arguments are supported:
20+
- `name`: override the name that the kernel will have in the generated code
21+
- `license::String=""`: license for the kernel source code and resulting object
22+
- `prog_section::String=""`: ELF section that the kernel will be placed in
23+
- `btf::Bool=true`: Whether to generate BTF debuginfo or not
24+
25+
The output of this function is automatically cached, i.e. you can simply call
26+
`bpffunction` in a hot path without degrading performance. New code will be
27+
generated automatically when the function changes or when different types or
28+
keyword arguments are provided.
29+
"""
30+
function bpffunction(f::Core.Function, tt::Type=Tuple{}; name=nothing, kwargs...)
31+
source = FunctionSpec(f, tt, false, name)
32+
GPUCompiler.cached_compilation(bpffunction_cache,
33+
bpffunction_compile,
34+
bpffunction_link,
35+
source; kwargs...)
36+
end
37+
38+
const bpffunction_cache = Dict{UInt,Any}()
39+
40+
# actual compilation
41+
function bpffunction_compile(source::FunctionSpec; format=:obj, license="",
42+
prog_section="prog", btf=true, kwargs...)
43+
# compile to BPF
44+
target = BPFCompilerTarget(; license, prog_section)
45+
params = BPFCompilerParams()
46+
job = CompilerJob(target, source, params)
47+
args = GPUCompiler.compile(format, job; validate=true, libraries=false, strip=!btf)
48+
format == :llvm && return collect.(codeunits.(string.(args))) # TODO: Make more efficient
49+
return collect(codeunits(args[1]))
50+
end
51+
bpffunction_link(@nospecialize(source::FunctionSpec), exe; kwargs...) = exe

src/execution.jl

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)