2023-01-17 23:59:04 +03:00
|
|
|
package link
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/cilium/ebpf"
|
|
|
|
"github.com/cilium/ebpf/asm"
|
|
|
|
"github.com/cilium/ebpf/internal"
|
2023-04-03 12:16:17 +03:00
|
|
|
"github.com/cilium/ebpf/internal/sys"
|
2023-01-17 23:59:04 +03:00
|
|
|
"github.com/cilium/ebpf/internal/unix"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Type is the kind of link.
|
2023-04-03 12:16:17 +03:00
|
|
|
type Type = sys.LinkType
|
2023-01-17 23:59:04 +03:00
|
|
|
|
|
|
|
// Valid link types.
|
|
|
|
const (
|
2023-04-03 12:16:17 +03:00
|
|
|
UnspecifiedType = sys.BPF_LINK_TYPE_UNSPEC
|
|
|
|
RawTracepointType = sys.BPF_LINK_TYPE_RAW_TRACEPOINT
|
|
|
|
TracingType = sys.BPF_LINK_TYPE_TRACING
|
|
|
|
CgroupType = sys.BPF_LINK_TYPE_CGROUP
|
|
|
|
IterType = sys.BPF_LINK_TYPE_ITER
|
|
|
|
NetNsType = sys.BPF_LINK_TYPE_NETNS
|
|
|
|
XDPType = sys.BPF_LINK_TYPE_XDP
|
|
|
|
PerfEventType = sys.BPF_LINK_TYPE_PERF_EVENT
|
2023-01-17 23:59:04 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var haveProgAttach = internal.FeatureTest("BPF_PROG_ATTACH", "4.10", func() error {
|
|
|
|
prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
|
2023-04-03 12:16:17 +03:00
|
|
|
Type: ebpf.CGroupSKB,
|
|
|
|
License: "MIT",
|
2023-01-17 23:59:04 +03:00
|
|
|
Instructions: asm.Instructions{
|
|
|
|
asm.Mov.Imm(asm.R0, 0),
|
|
|
|
asm.Return(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return internal.ErrNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
// BPF_PROG_ATTACH was introduced at the same time as CGgroupSKB,
|
|
|
|
// so being able to load the program is enough to infer that we
|
|
|
|
// have the syscall.
|
|
|
|
prog.Close()
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
var haveProgAttachReplace = internal.FeatureTest("BPF_PROG_ATTACH atomic replacement", "5.5", func() error {
|
|
|
|
if err := haveProgAttach(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
|
|
|
|
Type: ebpf.CGroupSKB,
|
|
|
|
AttachType: ebpf.AttachCGroupInetIngress,
|
|
|
|
License: "MIT",
|
|
|
|
Instructions: asm.Instructions{
|
|
|
|
asm.Mov.Imm(asm.R0, 0),
|
|
|
|
asm.Return(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return internal.ErrNotSupported
|
|
|
|
}
|
|
|
|
defer prog.Close()
|
|
|
|
|
|
|
|
// We know that we have BPF_PROG_ATTACH since we can load CGroupSKB programs.
|
|
|
|
// If passing BPF_F_REPLACE gives us EINVAL we know that the feature isn't
|
|
|
|
// present.
|
2023-04-03 12:16:17 +03:00
|
|
|
attr := sys.ProgAttachAttr{
|
2023-01-17 23:59:04 +03:00
|
|
|
// We rely on this being checked after attachFlags.
|
|
|
|
TargetFd: ^uint32(0),
|
|
|
|
AttachBpfFd: uint32(prog.FD()),
|
|
|
|
AttachType: uint32(ebpf.AttachCGroupInetIngress),
|
|
|
|
AttachFlags: uint32(flagReplace),
|
|
|
|
}
|
|
|
|
|
2023-04-03 12:16:17 +03:00
|
|
|
err = sys.ProgAttach(&attr)
|
2023-01-17 23:59:04 +03:00
|
|
|
if errors.Is(err, unix.EINVAL) {
|
|
|
|
return internal.ErrNotSupported
|
|
|
|
}
|
|
|
|
if errors.Is(err, unix.EBADF) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
var haveBPFLink = internal.FeatureTest("bpf_link", "5.7", func() error {
|
2023-04-03 12:16:17 +03:00
|
|
|
attr := sys.LinkCreateAttr{
|
2023-01-17 23:59:04 +03:00
|
|
|
// This is a hopefully invalid file descriptor, which triggers EBADF.
|
2023-04-03 12:16:17 +03:00
|
|
|
TargetFd: ^uint32(0),
|
|
|
|
ProgFd: ^uint32(0),
|
|
|
|
AttachType: sys.AttachType(ebpf.AttachCGroupInetIngress),
|
2023-01-17 23:59:04 +03:00
|
|
|
}
|
2023-04-03 12:16:17 +03:00
|
|
|
_, err := sys.LinkCreate(&attr)
|
2023-01-17 23:59:04 +03:00
|
|
|
if errors.Is(err, unix.EINVAL) {
|
|
|
|
return internal.ErrNotSupported
|
|
|
|
}
|
|
|
|
if errors.Is(err, unix.EBADF) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|