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

Change BPFMap method Update to unsafe.Pointer #32

Merged
merged 2 commits into from
Jul 8, 2021
Merged

Change BPFMap method Update to unsafe.Pointer #32

merged 2 commits into from
Jul 8, 2021

Conversation

grantseltzer
Copy link
Contributor

@grantseltzer grantseltzer commented Jul 7, 2021

This makes a breaking change to the libbpfgo API in which
the BPFMap.Update() method takes two unsafe.Pointer instead
of two interface{}. This is so that libbpfgo does not have
to know or care about the type of the passed value. If bpf
and libbpf can support the datatype, so should libbpfgo.

It's important to note that it would then be on the user to
correctly pass arrays/slices (reference must be passed to the
first element, not the array/slice itself).

This also adds a selftest but I haven't added it to any build
script/Makefile, as I'm waiting for #28 to merge, then I will
rebase and update accordingly.

We should heavily consider the implications of this breaking
API change.

cc @derekparker

Signed-off-by: grantseltzer [email protected]

@grantseltzer grantseltzer requested a review from yanivagman July 7, 2021 21:20
This makes a breaking change to the libbpfgo API in which
the BPFMap.Update() method takes two unsafe.Pointer instead
of two interface{}. This is so that libbpfgo does not have
to know or care about the type of the passed value. If bpf
and libbpf can support the datatype, so should libbpfgo.

It's important to note that it would then be on the user to
correctly pass arrays/slices (reference must be passed to the
first element, not the array/slice itself).

Signed-off-by: grantseltzer <[email protected]>
// valuePtr := unsafe.Pointer(&value[0])
// bpfmap.Update(keyPtr, valuePtr)
//
func (b *BPFMap) Update(key, value unsafe.Pointer) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the rest of the BPFMap also be updated to reflect this change? E.g. GetValue and DeleteKey? That way you could delete the entire GetUnsafePointer function (note this will also be a breaking API change as that method is current exported).

I assume there aren't any bad implications from a consumer trying to use a pointer to a struct or an array/slice as a key, correct (or anything that specifically wasn't allowed before)?

Copy link
Collaborator

@yanivagman yanivagman Jul 8, 2021

Choose a reason for hiding this comment

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

Should the rest of the BPFMap also be updated to reflect this change? E.g. GetValue and DeleteKey? That way you could delete the entire GetUnsafePointer function

+1

I assume there aren't any bad implications from a consumer trying to use a pointer to a struct or an array/slice as a key, correct (or anything that specifically wasn't allowed before)?

With this change, the same semantics as of libbpf are applied. This means that the kernel will try to read from the given pointers (key and value) the same type that was defined for the map. It is now the responsibility of the consumer to make sure that the pointer given to this function is valid and points to the correct data type, or else, bad things might happen.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated GetValue and DeleteKey accordingly.

@derekparker
Copy link
Contributor

Well, technically this lib is still pre 1.0 so breaking changes can happen! Alternative could be a new method such as UpdateUnsafe or something along those lines.

@grantseltzer
Copy link
Contributor Author

I think since it's pre-1.0 (for both libbpfgo and libbpf) we should just break the API, but we should make an announcement, cut another release, and reach out to people we know are using libbpfgo as best as we can to make sure it's well known.

@derekparker
Copy link
Contributor

I've tested this change locally with some work I'm doing with Delve and this change now allows me to pass a struct between the userspace program and the eBPF program using an eBPF map and everything seems to work great.

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.

Allow BPFMap.Update() to take arbitrary types as keys/values
3 participants