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

chore(prog): Add attach/detach legacy api for xdp #438

Merged
merged 1 commit into from
Jun 13, 2024
Merged
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
18 changes: 18 additions & 0 deletions libbpfgo.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,21 @@ __u32 cgo_bpf_tc_opts_priority(struct bpf_tc_opts *opts)

return opts->priority;
}

struct bpf_xdp_attach_opts *cgo_bpf_xdp_attach_opts_new(__u32 fd)
{
struct bpf_xdp_attach_opts *opts;
opts = calloc(1, sizeof(*opts));

if (!opts)
return NULL;
opts->sz = sizeof(*opts);
opts->old_prog_fd = fd;

return opts;
}

void cgo_bpf_xdp_attach_opts_free(struct bpf_xdp_attach_opts *opts)
{
free(opts);
}
8 changes: 7 additions & 1 deletion libbpfgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

#include <bpf/bpf.h>
#include <bpf/libbpf.h>
#include <linux/bpf.h> // uapi
#include <linux/bpf.h> // uapi
#include <linux/if_link.h> // uapi

void cgo_libbpf_set_print_fn();

Expand Down Expand Up @@ -108,4 +109,9 @@ __u32 cgo_bpf_tc_opts_prog_id(struct bpf_tc_opts *opts);
__u32 cgo_bpf_tc_opts_handle(struct bpf_tc_opts *opts);
__u32 cgo_bpf_tc_opts_priority(struct bpf_tc_opts *opts);

// bpf_xdp_attach_opts

struct bpf_xdp_attach_opts *cgo_bpf_xdp_attach_opts_new(__u32 fd);
void cgo_bpf_xdp_attach_opts_free(struct bpf_xdp_attach_opts *opts);

#endif
16 changes: 16 additions & 0 deletions prog-common.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,19 @@ const (
BPFFAllowMulti AttachFlag = C.BPF_F_ALLOW_MULTI
BPFFReplace AttachFlag = C.BPF_F_REPLACE
)

//
// XDPFlags
//

type XDPFlags uint32

const (
XDPFlagsUpdateIfNoExist XDPFlags = C.XDP_FLAGS_UPDATE_IF_NOEXIST
XDPFlagsSkbMode XDPFlags = C.XDP_FLAGS_SKB_MODE
XDPFlagsDrvMode XDPFlags = C.XDP_FLAGS_DRV_MODE
XDPFlagsHwMode XDPFlags = C.XDP_FLAGS_HW_MODE
XDPFlagsReplace XDPFlags = C.XDP_FLAGS_REPLACE
XDPFlagsModes XDPFlags = C.XDP_FLAGS_MODES
XDPFlagsMask XDPFlags = C.XDP_FLAGS_MASK
)
38 changes: 38 additions & 0 deletions prog.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,44 @@ func (p *BPFProg) AttachXDP(deviceName string) (*BPFLink, error) {
return bpfLink, nil
}

func (p *BPFProg) AttachXDPLegacy(deviceName string, flag XDPFlags) error {
optsC, errno := C.cgo_bpf_xdp_attach_opts_new(C.uint32_t(0))
if optsC == nil {
return fmt.Errorf("failed to create xdp attach opts:%w", errno)
}
defer C.cgo_bpf_xdp_attach_opts_free(optsC)
iface, err := net.InterfaceByName(deviceName)
if err != nil {
return fmt.Errorf("failed to find device by name %s: %w", deviceName, err)
}
var retC C.int
retC, errno = C.bpf_xdp_attach(C.int(iface.Index), C.int(p.FileDescriptor()), C.uint32_t(flag), optsC)
if retC < 0 {
return fmt.Errorf("failed to attach xdp: %w", errno)
}

return nil
}

func (p *BPFProg) DetachXDPLegacy(deviceName string, flag XDPFlags) error {
optsC, errno := C.cgo_bpf_xdp_attach_opts_new(C.uint32_t(p.FileDescriptor()))
if optsC == nil {
return fmt.Errorf("failed to create xdp attach opts:%w", errno)
}
defer C.cgo_bpf_xdp_attach_opts_free(optsC)
iface, err := net.InterfaceByName(deviceName)
if err != nil {
return fmt.Errorf("failed to find device by name %s: %w", deviceName, err)
}
var retC C.int
retC, errno = C.bpf_xdp_detach(C.int(iface.Index), C.uint32_t(flag), optsC)
if retC < 0 {
return fmt.Errorf("failed to detach xdp: %w", errno)
}

return nil
}

func (p *BPFProg) AttachTracepoint(category, name string) (*BPFLink, error) {
tpCategoryC := C.CString(category)
defer C.free(unsafe.Pointer(tpCategoryC))
Expand Down
11 changes: 11 additions & 0 deletions selftest/xdp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ func main() {
os.Exit(-1)
}

err = xdpProg.AttachXDPLegacy(deviceName, bpf.XDPFlagsReplace)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
err = xdpProg.DetachXDPLegacy(deviceName, bpf.XDPFlagsReplace)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}

_, err = xdpProg.AttachXDP(deviceName)
if err != nil {
fmt.Fprintln(os.Stderr, err)
Expand Down
Loading