cilium/ebpf

AttachXDP failed on Ubuntu 20.04, kernel version is 5.4

loveyacper opened this issue · 3 comments

Describe the bug

features.HaveProgramType(ebpf.XDP) return success, but link.AttachXDP failed.

How to reproduce

Env: Ubuntu 20.04, kernel version 5.4
Code:

if err := features.HaveProgramType(ebpf.XDP); err == nil {
  lk, linkErr := link.AttachXDP(link.XDPOptions{
			Program:   objs.TrafficStat,
			Interface: iface.Index,
		})
  log.Info("Attach XDP result: %v", linkErr)
} 

Result:

Attach XDP result: bpf_link not supported (requires >= v5.7)

Version information

github.com/cilium/ebpf v0.14.0

In features/prog.go, it says XDP is supported from version 4.8 :

var haveProgramTypeMatrix = internal.FeatureMatrix[ebpf.ProgramType]{
	...
	ebpf.XDP:           {Version: "4.8"},
        ...
}

But AttachXDP call haveBPFLink, which report error if kernel version is older than 5.7 :

var haveBPFLink = internal.NewFeatureTest("bpf_link", "5.7", func() error {
	...
        return err
})

Hi @loveyacper

The eBPF program type XDP was introduced earlier (with 4.8 iirc) than bpf_link (5.7). As a consequence, bpf_link is expected to not be available on a 5.4 kernel.
You can still load XDP programs, but you have to use another method, like ip link set .... This package cilium/ebpf focuses on everything around the eBPF syscall - therefore it does not cover the loading via ip link set ....
A package, that supports loading of XDP on kernels that do not have support for bpf_link is jsimonetti/rtnetlink/v2#LinkService.Set.
Feel free to checkout https://gist.github.com/florianl/935cfcbeeee209537d8303165dab41e0 as an example for this.

@florianl I tried and it worked. I appreciate your helpful and timely reply.