Skip to content

Commit

Permalink
chore(prog): Add attach/detach legacy api for xdp (#438)
Browse files Browse the repository at this point in the history
The AttachXDP api use bpf link to attach xdp prog, AttachXDPLegacy/DetachXDPLegacy
apis corresponds to bpf_xdp_attach/bpf_xdp_detach, which can be used on
old kernel, so add it.

Signed-off-by: Tao Chen <[email protected]>
  • Loading branch information
chentao-kernel authored Jun 13, 2024
1 parent 2791244 commit d2c4549
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
18 changes: 18 additions & 0 deletions libbpfgo.c
Original file line number Diff line number Diff line change
Expand Up @@ -536,3 +536,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 @@ -122,4 +123,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 @@ -309,6 +309,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

0 comments on commit d2c4549

Please sign in to comment.