Replies: 3 comments 2 replies
-
Thanks for your ideas :) Let me try to break things down: Heuristics for selecting which types to generate
This is not a good assumption when writing kprobes for example. Here you'll often pull in That doesn't mean that the current heuristic is great either: I'd be happy if we could improve it so that less Auto-generate types for enumsThis is a special case of the problem you describe above, no? Maybe this is a good candidate to improve the heuristic which types to emit. Maybe something like Enum names
If I understand you correctly, you'd prefer struct HELLO {
// bla
}
enum {
HELLO = 1,
}; This is valid C that would fail to generate valid Go. The simplest fix to this is using distinct prefixes. Unions
Correct. FWIW you can serialise and de-serialise from these types as well. I realise now that there might be no way to access data in the padding though, since we never read or write padding.
Yeah that might have been the better choice. Unfortunately I wasn't aware of what CGO did! |
Beta Was this translation helpful? Give feedback.
-
Hi @lmb, thank you for looking into this and sharing your perspectives. I'll follow the same structure you've established. Heuristics for selecting which types to generate
I doubt that there will be much of the kernel types in BTF. Definitely not the entire Never used gcc for BPF, but clang does a very good job pruning BTF. Types that remain are:
In the end, we might capture just a few of the kernel types when CO-RE is employed. Is it of a concern if we would've generated Golang types for them by default? We could narrow that down by doing a pass over BTF graph (e.g. only include types needed for I'd like to propose the following:
Enum names
Now that I understand it, I probably like it more than what Unions
Well, on the other hand In my case, the union is defined by me, so I can replace
with
to get the desired effect. Footnotes
|
Beta Was this translation helpful? Give feedback.
-
I got a third-party defined struct that looks like:
and
as mentioned above, the |
Beta Was this translation helpful? Give feedback.
-
I am a newcomer to
cilium/ebpf
. I have experience with coding againstlibbpf
in C. Recently, I did a Golang project with small parts in C. Golang app was building on top of a custom C library exposing app-specific interfaces on top oflibbpf
. It works but developer experience was subpar as one had to be familiar both with cgo and ebpf (2 times as hairy as it needs to be).Super excited about
cilium/ebpf
. Coding against ebpf apis in Golang and having C types mapped in Go is so nice and clean.Playing with
bpf2go
I had a few wtf moments. Maybe my expectations about C types mapping were formed by cgo. Maybe there are good reasons for things to be done in a certain way. I'm sharing in hopes that this input might be useful to improve the developer experience ofcilium/ebpf
.Structure definitions
Unless explicitly requested via
-type
flag OR being used as a K/V type in a map, structures are emitted inline.maps to
It breaks the natural assumption that entities of the same type in C maps to entities of the same type in Go.
Further, is not it natural to assume that a subset of C types as exposed via BTF should all have corresponding Golang types generated?
Sure thing, one can add
-type foo
tobpf2go
invocation and then it appears as expected:Having to update
-type
flags as the code evolves feels somewhat redundant and error-prone.Enum definitions
Unless we pass
-type foo
,translates into
while the natural expectation is to get
I would also argue that prepending the type name
DpFoo
to form constant names (e.g.DpFooHELLO
) is a questionable design choice as constant names on C side typically already have a unique type name-derived prefix or suffix (as all enum constants share the same namespace).Unions
Only the first "branch" is emitted. Nested type declarations are not generated, unless the type is used in a map or mentioned in
-type
flag. It is consistent with type handling elsewhere.So
with
-type bar
results inI don't see a way to access the second branch
d
(besidesunsafe
typecast).For reference, cgo maps union
U
to[sizeof(U)]byte
.Which would've enabled to produce and consume data via serialisation.
Beta Was this translation helpful? Give feedback.
All reactions