Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpf2go: generate Go types from BTF #552

Merged
merged 4 commits into from Feb 16, 2022
Merged

bpf2go: generate Go types from BTF #552

merged 4 commits into from Feb 16, 2022

Conversation

lmb
Copy link
Collaborator

@lmb lmb commented Jan 28, 2022

This PR has been sitting in my repo for too long. The basic idea is to use BTF to generate Go type equivalents of C types. By default we will do this for all types referenced as key or value from BTF map definitions.

Adapting examples would look like this: lmb@1ce17d8

cmd/bpf2go: generate types from BTF

Use GoFormatter to generate type definitions for types used as map keys
and values. Allow users to emit additional types via a command line flag.

collection: expose BTF spec

btf: export UnderlyingType

Export an infallible version of skipQualifiersAndTypedef. Circular
types / very long chains of qualifiers or typedefs are a corner
case that shouldn't really happen, so let's not burden the callers
with this detail.

btf: allow finding type by name alone

Add a function which allows finding a type by name alone. This
is useful for code that doesn't care about the concrete type
returned.

@lmb lmb force-pushed the bpf2go-gen-types branch 3 times, most recently from c1d1fc8 to 97f9460 Compare January 28, 2022 15:52
@danobi
Copy link
Contributor

danobi commented Jan 29, 2022

FWIW, I think it'd be really useful to also generate BTF_KIND_DATASEC types as well. Maybe not in this PR since this library doesn't support reading globals from collections yet.

I added similar stuff in libbpf-rs and it was quite useful there. I miss it a lot in go land :)

@lmb
Copy link
Collaborator Author

lmb commented Feb 1, 2022

Yes, definitely. The code can already emit Datasec, but I don't yet know how to expose a sensible API for that.

@lmb lmb requested a review from ti-mo February 1, 2022 16:43
@ti-mo
Copy link
Collaborator

ti-mo commented Feb 2, 2022

Neat! Looks like you found a use for AnyTypeByName after all. 😄

Add a function which allows finding a type by name alone. This
is useful for code that doesn't care about the concrete type
returned.
Export an infallible version of skipQualifiersAndTypedef. Circular
types / very long chains of qualifiers or typedefs are a corner
case that shouldn't really happen, so let's not burden the callers
with this detail.
Use GoFormatter to generate type definitions for types used as map keys
and values. Allow users to emit additional types via a command line flag.
@lmb lmb merged commit 1b61f07 into cilium:master Feb 16, 2022
@lmb lmb deleted the bpf2go-gen-types branch February 16, 2022 11:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants