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

Add BPF map reuse function #346

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions libbpfgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,15 @@ func (b *BPFMap) Type() MapType {
return MapType(C.bpf_map__type(b.bpfMap))
}

func (b *BPFMap) MapReuseFd(fd int) error {
errC := C.bpf_map__reuse_fd(b.bpfMap, C.int(fd))
if errC != 0 {
return fmt.Errorf("could not reuse bpf map fd %d: %w", fd, syscall.Errno(-errC))
}
b.fd = C.int(fd)
Copy link
Member

Choose a reason for hiding this comment

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

The fd to be updated is an open/dupped one, not the provided. I also noticed that the map name needs to be updated.

Well, my current work is changing BPFMap to:

image

https://github.com/aquasecurity/libbpfgo/pull/356/files#diff-5ed7da7003773d8e0a4dca00d4fc08fc5582695eefe22a400db15edfd5aab840R20-R24

As I'm introducing a bunch of changes, I'm going to stop to create conflicts to you 😅, cherry-picking your contribution and doing the required adjustments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for taking care of this, did not know about the refactor!

return nil
javierhonduco marked this conversation as resolved.
Show resolved Hide resolved
}

// SetType is used to set the type of a bpf map that isn't associated
// with a file descriptor already. If the map is already associated
// with a file descriptor the libbpf API will return error code EBUSY
Expand Down
2 changes: 1 addition & 1 deletion selftest/common/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ okcontinue() { okay "$1"; }
kern_version() {
_oper=$1; _version=$2; _notfatal=$3;
_given=$(($(echo $_version | sed 's:\.::g')))
_current=$(($(uname -r | cut -d'.' -f1,2 | sed 's:\.::g')))
_current=$(($(uname -r | cut -d'.' -f1,2,3 | sed 's:\.::g')))
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 this check to make it work with my release 6.3.11-200.fc38.x86_64

Copy link
Member

Choose a reason for hiding this comment

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

echo '6.3.11-200.fc38.x86_64' | cut -d'.' -f1,2 | sed 's:\.::g'
63

It seems ok, we need to merge only the first and second field of the numeric version.

Could you please recheck this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On #356

[javierhonduco@fedora reuse-fd]$ ./run.sh 
[!] ERROR: kernel 64 not gt than 418

(Now with kernel 6.4.6-200.fc38.x86_64)

Copy link
Member

Choose a reason for hiding this comment

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

Hmmm, it seems a logic issue. I'm going to take a look at this later. Thanks.


[[ "$_version" == "" ]] && errexit "no kernel version given"

Expand Down
4 changes: 0 additions & 4 deletions selftest/create-map/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ func main() {
}
defer bpfModule.Close()

bpfModule.BPFLoadObject()
opts := bpf.BPFMapCreateOpts{}
opts.Size = uint64(unsafe.Sizeof(opts))
geyslan marked this conversation as resolved.
Show resolved Hide resolved

m, err := bpf.CreateMap(bpf.MapTypeHash, "foobar", 4, 4, 420, nil)
if err != nil {
log.Fatal(err)
Expand Down
7 changes: 7 additions & 0 deletions selftest/map-update/main.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ struct {
__uint(max_entries, 1 << 24);
} tester SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, u32);
__type(value, struct value);
__uint(max_entries, 1 << 24);
} tester_reused SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
__uint(key_size, sizeof(u32));
Expand Down
17 changes: 17 additions & 0 deletions selftest/map-update/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ func main() {
os.Exit(-1)
}

testerMap2, err := bpfModule.GetMap("tester_reused")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}

err = testerMap2.MapReuseFd(testerMap.FileDescriptor())
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}

if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}

eventsChannel := make(chan []byte)
lostChannel := make(chan uint64)
pb, err := bpfModule.InitPerfBuf("events", eventsChannel, lostChannel, 1)
Expand Down
Loading