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

PinType: change from int to uint32 #1179

Merged
merged 1 commit into from
Oct 20, 2023
Merged

Conversation

ti-mo
Copy link
Collaborator

@ti-mo ti-mo commented Oct 20, 2023

For supporting custom pinning behaviours in Cilium, we're looking to extend the MapSpec.Pinning flag and treat it as a bitfield internally. libbpf and ebpf-go currently only support LIBBPF_PIN_BY_NAME and ebpf.PinByName respectively, which has a value of 1, so it's still up in the air whether or not this could officially evolve into a bitfield in the future, since doing so can be done while maintaining backwards compatibility.

In Cilium's case, we reserve the lower 4 bits for the upstream enum values and mask out the remaining bits before calling NewCollection. The upper bits are used for specifying flags to request specific behaviours, e.g. to ignore a pinned map during loading, and explicitly overwriting its pin at a later stage, when the entrypoint program(s) were successfully attached. Useful for working with prog arrays that need to remain pinned.

We want to keep using the existing ebpf.PinType type for this, but defining masks using a signed type needs to be done with care, so ideally PinType would be unsigned.

A few more data points on why this change is warranted:

  • bpf_elf_map always had 'unsigned int' fields only, including 'pinning'
  • libbpf reads the BTF map def 'pinning' field into a __u32
  • Realistically, enum libbpf_pin_type will not acquire any negative values

@ti-mo ti-mo requested a review from lmb October 20, 2023 08:10
For supporting custom pinning behaviours in Cilium, we're looking to extend
the MapSpec.Pinning flag and treat it as a bitfield internally. libbpf and
ebpf-go currently only support LIBBPF_PIN_BY_NAME and ebpf.PinByName
respectively, which has a value of 1, so it's still up in the air whether or
not this could officially evolve into a bitfield in the future, since doing
so can be done while maintaining backwards compatibility.

In Cilium's case, we reserve the lower 4 bits for the upstream enum values and
mask out the remaining bits before calling NewCollection. The upper bits are
used for specifying flags to request specific behaviours, e.g. to ignore a
pinned map during loading, and explicitly overwriting its pin at a later stage,
when the entrypoint program(s) were successfully attached. Useful for working
with prog arrays that need to remain pinned.

We want to keep using the existing ebpf.PinType type for this, but defining
masks using a signed type needs to be done with care, so ideally PinType would
be unsigned.

A few more data points on why this change is warranted:

- bpf_elf_map always had 'unsigned int' fields only, including 'pinning'
- libbpf reads the BTF map def 'pinning' field into a __u32
- Realistically, `enum libbpf_pin_type` will not acquire any negative values

Signed-off-by: Timo Beckers <[email protected]>
@ti-mo ti-mo force-pushed the tb/pinning-unsigned branch from 761ca89 to d929345 Compare October 20, 2023 08:13
Copy link
Collaborator

@lmb lmb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for digging out the references!

@lmb lmb merged commit 0d47e51 into cilium:main Oct 20, 2023
11 checks passed
@ti-mo ti-mo deleted the tb/pinning-unsigned branch October 20, 2023 09:35
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.

2 participants