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

feat: add AttachStructOps() #476

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
12 changes: 12 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package libbpfgo
import "C"

import (
"errors"
"fmt"
"syscall"
"unsafe"
Expand Down Expand Up @@ -73,6 +74,17 @@ func (m *BPFMap) ReuseFD(fd int) error {
return nil
}

func (m *BPFMap) AttachStructOps() error {
if m.Type().String() != MapTypeStructOps.String() {
return errors.New("Map type should be BPF_MAP_TYPE_STRUCT_OPS")
}
linkC, errno := C.bpf_map__attach_struct_ops(m.bpfMap)
if linkC == nil {
return fmt.Errorf("Map attach failed: %v", &errno)
}
return nil
Copy link
Member

Choose a reason for hiding this comment

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

We need to take care of this pointer as bpf_link__detach_struct_ops() receives it, so AttachStructOps() should return a *BPFLink.

P.S.: bpf_link__detach_struct_ops() wrapper should be implemented as well. Take a look at the BPFLink type - currently it holds and handles only BPFProg, but you might change it to accomodate a BPFMap as well, I believe.

Copy link
Author

Choose a reason for hiding this comment

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

Hi @geyslan
bpf_link__detach_struct_ops is not public API in the current, do you have any idea on how to handle this?
thanks!

Copy link
Member

Choose a reason for hiding this comment

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

You're right, it's not indeed. Well, you may have access to it by using link.detach func pointer as bpf_link is returned with it assigned to our bpf_link__detach_struct_ops.

image

Copy link
Author

Choose a reason for hiding this comment

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

@geyslan
If my understanding is correct, the bpf_link__detach_struct_ops() will be invoked when link.Destroy() called.
image
image

So we don't need to add wrapped function for detaching strectOps map, right?

}

func (m *BPFMap) Name() string {
return C.GoString(C.bpf_map__name(m.bpfMap))
}
Expand Down
5 changes: 4 additions & 1 deletion selftest/common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ LDFLAGS =
CGO_CFLAGS_STATIC = "-I$(abspath $(OUTPUT)) -I$(abspath ../common)"
CGO_LDFLAGS_STATIC = "$(shell PKG_CONFIG_PATH=$(LIBBPF_OBJDIR) $(PKGCONFIG) --static --libs libbpf)"
CGO_EXTLDFLAGS_STATIC = '-w -extldflags "-static"'
SCX_FLAGS=-mcpu=v3 -mlittle-endian \
-I ../../libbpf/src/usr/include -I ../../libbpf/include/uapi \
-I /lib/modules/$(shell uname -r)/build/tools/sched_ext/include/scx -I /lib/modules/$(shell uname -r)/build/tools/sched_ext/include/bpf-compat

CGO_CFLAGS_DYN = "-I. -I/usr/include/"
CGO_LDFLAGS_DYN = "$(shell $(PKGCONFIG) --shared --libs libbpf)"
Expand Down Expand Up @@ -52,7 +55,7 @@ outputdir:
## test bpf dependency

$(MAIN).bpf.o: $(MAIN).bpf.c
$(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) -I$(OUTPUT) -I$(abspath ../common) -c $< -o $@
$(CLANG) $(CFLAGS) -target bpf -D__TARGET_ARCH_$(ARCH) $(SCX_FLAGS) -I$(OUTPUT) -I$(abspath ../common) -c $< -o $@

## test

Expand Down
Loading
Loading