2023-01-17 20:59:04 +00:00
|
|
|
package link
|
|
|
|
|
|
|
|
import (
|
2023-04-03 09:16:17 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2023-01-17 20:59:04 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/cilium/ebpf"
|
2023-04-03 09:16:17 +00:00
|
|
|
"github.com/cilium/ebpf/btf"
|
2023-01-17 20:59:04 +00:00
|
|
|
"github.com/cilium/ebpf/internal"
|
2023-04-03 09:16:17 +00:00
|
|
|
"github.com/cilium/ebpf/internal/sys"
|
2023-01-17 20:59:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var ErrNotSupported = internal.ErrNotSupported
|
|
|
|
|
|
|
|
// Link represents a Program attached to a BPF hook.
|
|
|
|
type Link interface {
|
|
|
|
// Replace the current program with a new program.
|
|
|
|
//
|
|
|
|
// Passing a nil program is an error. May return an error wrapping ErrNotSupported.
|
|
|
|
Update(*ebpf.Program) error
|
|
|
|
|
|
|
|
// Persist a link by pinning it into a bpffs.
|
|
|
|
//
|
|
|
|
// May return an error wrapping ErrNotSupported.
|
|
|
|
Pin(string) error
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
// Undo a previous call to Pin.
|
|
|
|
//
|
|
|
|
// May return an error wrapping ErrNotSupported.
|
|
|
|
Unpin() error
|
|
|
|
|
2023-01-17 20:59:04 +00:00
|
|
|
// Close frees resources.
|
|
|
|
//
|
2023-04-03 09:16:17 +00:00
|
|
|
// The link will be broken unless it has been successfully pinned.
|
|
|
|
// A link may continue past the lifetime of the process if Close is
|
2023-01-17 20:59:04 +00:00
|
|
|
// not called.
|
|
|
|
Close() error
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
// Info returns metadata on a link.
|
|
|
|
//
|
|
|
|
// May return an error wrapping ErrNotSupported.
|
|
|
|
Info() (*Info, error)
|
|
|
|
|
2023-01-17 20:59:04 +00:00
|
|
|
// Prevent external users from implementing this interface.
|
|
|
|
isLink()
|
|
|
|
}
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
// LoadPinnedLink loads a link that was persisted into a bpffs.
|
|
|
|
func LoadPinnedLink(fileName string, opts *ebpf.LoadPinOptions) (Link, error) {
|
|
|
|
raw, err := loadPinnedRawLink(fileName, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return wrapRawLink(raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wrap a RawLink in a more specific type if possible.
|
|
|
|
//
|
|
|
|
// The function takes ownership of raw and closes it on error.
|
|
|
|
func wrapRawLink(raw *RawLink) (Link, error) {
|
|
|
|
info, err := raw.Info()
|
|
|
|
if err != nil {
|
|
|
|
raw.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch info.Type {
|
|
|
|
case RawTracepointType:
|
|
|
|
return &rawTracepoint{*raw}, nil
|
|
|
|
case TracingType:
|
|
|
|
return &tracing{*raw}, nil
|
|
|
|
case CgroupType:
|
|
|
|
return &linkCgroup{*raw}, nil
|
|
|
|
case IterType:
|
|
|
|
return &Iter{*raw}, nil
|
|
|
|
case NetNsType:
|
|
|
|
return &NetNsLink{*raw}, nil
|
|
|
|
default:
|
|
|
|
return raw, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 20:59:04 +00:00
|
|
|
// ID uniquely identifies a BPF link.
|
2023-04-03 09:16:17 +00:00
|
|
|
type ID = sys.LinkID
|
2023-01-17 20:59:04 +00:00
|
|
|
|
|
|
|
// RawLinkOptions control the creation of a raw link.
|
|
|
|
type RawLinkOptions struct {
|
|
|
|
// File descriptor to attach to. This differs for each attach type.
|
|
|
|
Target int
|
|
|
|
// Program to attach.
|
|
|
|
Program *ebpf.Program
|
|
|
|
// Attach must match the attach type of Program.
|
|
|
|
Attach ebpf.AttachType
|
2023-04-03 09:16:17 +00:00
|
|
|
// BTF is the BTF of the attachment target.
|
|
|
|
BTF btf.TypeID
|
|
|
|
// Flags control the attach behaviour.
|
|
|
|
Flags uint32
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
// Info contains metadata on a link.
|
|
|
|
type Info struct {
|
2023-01-17 20:59:04 +00:00
|
|
|
Type Type
|
|
|
|
ID ID
|
|
|
|
Program ebpf.ProgramID
|
2023-04-03 09:16:17 +00:00
|
|
|
extra interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type TracingInfo sys.TracingLinkInfo
|
|
|
|
type CgroupInfo sys.CgroupLinkInfo
|
|
|
|
type NetNsInfo sys.NetNsLinkInfo
|
|
|
|
type XDPInfo sys.XDPLinkInfo
|
|
|
|
|
|
|
|
// Tracing returns tracing type-specific link info.
|
|
|
|
//
|
|
|
|
// Returns nil if the type-specific link info isn't available.
|
|
|
|
func (r Info) Tracing() *TracingInfo {
|
|
|
|
e, _ := r.extra.(*TracingInfo)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cgroup returns cgroup type-specific link info.
|
|
|
|
//
|
|
|
|
// Returns nil if the type-specific link info isn't available.
|
|
|
|
func (r Info) Cgroup() *CgroupInfo {
|
|
|
|
e, _ := r.extra.(*CgroupInfo)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetNs returns netns type-specific link info.
|
|
|
|
//
|
|
|
|
// Returns nil if the type-specific link info isn't available.
|
|
|
|
func (r Info) NetNs() *NetNsInfo {
|
|
|
|
e, _ := r.extra.(*NetNsInfo)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExtraNetNs returns XDP type-specific link info.
|
|
|
|
//
|
|
|
|
// Returns nil if the type-specific link info isn't available.
|
|
|
|
func (r Info) XDP() *XDPInfo {
|
|
|
|
e, _ := r.extra.(*XDPInfo)
|
|
|
|
return e
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RawLink is the low-level API to bpf_link.
|
|
|
|
//
|
|
|
|
// You should consider using the higher level interfaces in this
|
|
|
|
// package instead.
|
|
|
|
type RawLink struct {
|
2023-04-03 09:16:17 +00:00
|
|
|
fd *sys.FD
|
|
|
|
pinnedPath string
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AttachRawLink creates a raw link.
|
|
|
|
func AttachRawLink(opts RawLinkOptions) (*RawLink, error) {
|
|
|
|
if err := haveBPFLink(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Target < 0 {
|
2023-04-03 09:16:17 +00:00
|
|
|
return nil, fmt.Errorf("invalid target: %s", sys.ErrClosedFd)
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
progFd := opts.Program.FD()
|
|
|
|
if progFd < 0 {
|
2023-04-03 09:16:17 +00:00
|
|
|
return nil, fmt.Errorf("invalid program: %s", sys.ErrClosedFd)
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
attr := sys.LinkCreateAttr{
|
|
|
|
TargetFd: uint32(opts.Target),
|
|
|
|
ProgFd: uint32(progFd),
|
|
|
|
AttachType: sys.AttachType(opts.Attach),
|
|
|
|
TargetBtfId: uint32(opts.BTF),
|
|
|
|
Flags: opts.Flags,
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
2023-04-03 09:16:17 +00:00
|
|
|
fd, err := sys.LinkCreate(&attr)
|
2023-01-17 20:59:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't create link: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
return &RawLink{fd, ""}, nil
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
func loadPinnedRawLink(fileName string, opts *ebpf.LoadPinOptions) (*RawLink, error) {
|
|
|
|
fd, err := sys.ObjGet(&sys.ObjGetAttr{
|
|
|
|
Pathname: sys.NewStringPointer(fileName),
|
|
|
|
FileFlags: opts.Marshal(),
|
|
|
|
})
|
2023-01-17 20:59:04 +00:00
|
|
|
if err != nil {
|
2023-04-03 09:16:17 +00:00
|
|
|
return nil, fmt.Errorf("load pinned link: %w", err)
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
return &RawLink{fd, fileName}, nil
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *RawLink) isLink() {}
|
|
|
|
|
|
|
|
// FD returns the raw file descriptor.
|
|
|
|
func (l *RawLink) FD() int {
|
2023-04-03 09:16:17 +00:00
|
|
|
return l.fd.Int()
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close breaks the link.
|
|
|
|
//
|
|
|
|
// Use Pin if you want to make the link persistent.
|
|
|
|
func (l *RawLink) Close() error {
|
|
|
|
return l.fd.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pin persists a link past the lifetime of the process.
|
|
|
|
//
|
|
|
|
// Calling Close on a pinned Link will not break the link
|
|
|
|
// until the pin is removed.
|
|
|
|
func (l *RawLink) Pin(fileName string) error {
|
2023-04-03 09:16:17 +00:00
|
|
|
if err := internal.Pin(l.pinnedPath, fileName, l.fd); err != nil {
|
|
|
|
return err
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
2023-04-03 09:16:17 +00:00
|
|
|
l.pinnedPath = fileName
|
2023-01-17 20:59:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
// Unpin implements the Link interface.
|
|
|
|
func (l *RawLink) Unpin() error {
|
|
|
|
if err := internal.Unpin(l.pinnedPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
l.pinnedPath = ""
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update implements the Link interface.
|
2023-01-17 20:59:04 +00:00
|
|
|
func (l *RawLink) Update(new *ebpf.Program) error {
|
|
|
|
return l.UpdateArgs(RawLinkUpdateOptions{
|
|
|
|
New: new,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// RawLinkUpdateOptions control the behaviour of RawLink.UpdateArgs.
|
|
|
|
type RawLinkUpdateOptions struct {
|
|
|
|
New *ebpf.Program
|
|
|
|
Old *ebpf.Program
|
|
|
|
Flags uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateArgs updates a link based on args.
|
|
|
|
func (l *RawLink) UpdateArgs(opts RawLinkUpdateOptions) error {
|
|
|
|
newFd := opts.New.FD()
|
|
|
|
if newFd < 0 {
|
2023-04-03 09:16:17 +00:00
|
|
|
return fmt.Errorf("invalid program: %s", sys.ErrClosedFd)
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var oldFd int
|
|
|
|
if opts.Old != nil {
|
|
|
|
oldFd = opts.Old.FD()
|
|
|
|
if oldFd < 0 {
|
2023-04-03 09:16:17 +00:00
|
|
|
return fmt.Errorf("invalid replacement program: %s", sys.ErrClosedFd)
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
attr := sys.LinkUpdateAttr{
|
|
|
|
LinkFd: l.fd.Uint(),
|
|
|
|
NewProgFd: uint32(newFd),
|
|
|
|
OldProgFd: uint32(oldFd),
|
|
|
|
Flags: opts.Flags,
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
2023-04-03 09:16:17 +00:00
|
|
|
return sys.LinkUpdate(&attr)
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info returns metadata about the link.
|
2023-04-03 09:16:17 +00:00
|
|
|
func (l *RawLink) Info() (*Info, error) {
|
|
|
|
var info sys.LinkInfo
|
|
|
|
|
|
|
|
if err := sys.ObjInfo(l.fd, &info); err != nil {
|
2023-01-17 20:59:04 +00:00
|
|
|
return nil, fmt.Errorf("link info: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
var extra interface{}
|
|
|
|
switch info.Type {
|
|
|
|
case CgroupType:
|
|
|
|
extra = &CgroupInfo{}
|
|
|
|
case IterType:
|
|
|
|
// not supported
|
|
|
|
case NetNsType:
|
|
|
|
extra = &NetNsInfo{}
|
|
|
|
case RawTracepointType:
|
|
|
|
// not supported
|
|
|
|
case TracingType:
|
|
|
|
extra = &TracingInfo{}
|
|
|
|
case XDPType:
|
|
|
|
extra = &XDPInfo{}
|
|
|
|
case PerfEventType:
|
|
|
|
// no extra
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown link info type: %d", info.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Type != RawTracepointType && info.Type != IterType && info.Type != PerfEventType {
|
|
|
|
buf := bytes.NewReader(info.Extra[:])
|
|
|
|
err := binary.Read(buf, internal.NativeEndian, extra)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can not read extra link info: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Info{
|
|
|
|
info.Type,
|
|
|
|
info.Id,
|
|
|
|
ebpf.ProgramID(info.ProgId),
|
|
|
|
extra,
|
2023-01-17 20:59:04 +00:00
|
|
|
}, nil
|
|
|
|
}
|