2020-03-10 12:09:25 +00:00
|
|
|
package ebpf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-07-24 07:53:45 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2020-03-10 12:09:25 +00:00
|
|
|
"fmt"
|
|
|
|
"math"
|
2021-04-04 21:11:06 +00:00
|
|
|
"path/filepath"
|
2023-01-30 14:43:31 +00:00
|
|
|
"runtime"
|
2020-03-10 12:09:25 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/cilium/ebpf/asm"
|
2023-01-30 14:43:31 +00:00
|
|
|
"github.com/cilium/ebpf/btf"
|
2020-03-10 12:09:25 +00:00
|
|
|
"github.com/cilium/ebpf/internal"
|
2023-01-30 14:43:31 +00:00
|
|
|
"github.com/cilium/ebpf/internal/sys"
|
2020-03-10 12:09:25 +00:00
|
|
|
"github.com/cilium/ebpf/internal/unix"
|
|
|
|
)
|
|
|
|
|
2020-07-24 07:53:45 +00:00
|
|
|
// ErrNotSupported is returned whenever the kernel doesn't support a feature.
|
|
|
|
var ErrNotSupported = internal.ErrNotSupported
|
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
// ProgramID represents the unique ID of an eBPF program.
|
2020-07-24 07:53:45 +00:00
|
|
|
type ProgramID uint32
|
|
|
|
|
2020-03-10 12:09:25 +00:00
|
|
|
const (
|
|
|
|
// Number of bytes to pad the output buffer for BPF_PROG_TEST_RUN.
|
|
|
|
// This is currently the maximum of spare space allocated for SKB
|
|
|
|
// and XDP programs, and equal to XDP_PACKET_HEADROOM + NET_IP_ALIGN.
|
|
|
|
outputPad = 256 + 2
|
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultVerifierLogSize is the default number of bytes allocated for the
|
|
|
|
// verifier log.
|
|
|
|
const DefaultVerifierLogSize = 64 * 1024
|
|
|
|
|
|
|
|
// ProgramOptions control loading a program into the kernel.
|
|
|
|
type ProgramOptions struct {
|
|
|
|
// Controls the detail emitted by the kernel verifier. Set to non-zero
|
|
|
|
// to enable logging.
|
|
|
|
LogLevel uint32
|
|
|
|
// Controls the output buffer size for the verifier. Defaults to
|
|
|
|
// DefaultVerifierLogSize.
|
|
|
|
LogSize int
|
2023-01-30 14:43:31 +00:00
|
|
|
// Type information used for CO-RE relocations and when attaching to
|
|
|
|
// kernel functions.
|
|
|
|
//
|
2021-07-19 06:57:52 +00:00
|
|
|
// This is useful in environments where the kernel BTF is not available
|
|
|
|
// (containers) or where it is in a non-standard location. Defaults to
|
2023-01-30 14:43:31 +00:00
|
|
|
// use the kernel BTF from a well-known location if nil.
|
|
|
|
KernelTypes *btf.Spec
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
// ProgramSpec defines a Program.
|
2020-03-10 12:09:25 +00:00
|
|
|
type ProgramSpec struct {
|
|
|
|
// Name is passed to the kernel as a debug aid. Must only contain
|
|
|
|
// alpha numeric and '_' characters.
|
2020-07-24 07:53:45 +00:00
|
|
|
Name string
|
2022-02-06 07:29:51 +00:00
|
|
|
|
2020-07-24 07:53:45 +00:00
|
|
|
// Type determines at which hook in the kernel a program will run.
|
2023-01-30 14:43:31 +00:00
|
|
|
Type ProgramType
|
|
|
|
|
|
|
|
// AttachType of the program, needed to differentiate allowed context
|
|
|
|
// accesses in some newer program types like CGroupSockAddr.
|
|
|
|
//
|
|
|
|
// Available on kernels 4.17 and later.
|
2020-07-24 07:53:45 +00:00
|
|
|
AttachType AttachType
|
2023-01-30 14:43:31 +00:00
|
|
|
|
2022-02-06 07:29:51 +00:00
|
|
|
// Name of a kernel data structure or function to attach to. Its
|
|
|
|
// interpretation depends on Type and AttachType.
|
|
|
|
AttachTo string
|
2023-01-30 14:43:31 +00:00
|
|
|
|
2022-02-06 07:29:51 +00:00
|
|
|
// The program to attach to. Must be provided manually.
|
|
|
|
AttachTarget *Program
|
2023-01-30 14:43:31 +00:00
|
|
|
|
|
|
|
// The name of the ELF section this program orininated from.
|
|
|
|
SectionName string
|
|
|
|
|
2020-07-24 07:53:45 +00:00
|
|
|
Instructions asm.Instructions
|
2022-02-06 07:29:51 +00:00
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
// Flags is passed to the kernel and specifies additional program
|
|
|
|
// load attributes.
|
|
|
|
Flags uint32
|
2022-02-06 07:29:51 +00:00
|
|
|
|
2020-07-24 07:53:45 +00:00
|
|
|
// License of the program. Some helpers are only available if
|
|
|
|
// the license is deemed compatible with the GPL.
|
|
|
|
//
|
|
|
|
// See https://www.kernel.org/doc/html/latest/process/license-rules.html#id1
|
|
|
|
License string
|
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
// Version used by Kprobe programs.
|
2020-07-24 07:53:45 +00:00
|
|
|
//
|
2021-04-04 21:11:06 +00:00
|
|
|
// Deprecated on kernels 5.0 and later. Leave empty to let the library
|
|
|
|
// detect this value automatically.
|
2020-03-10 12:09:25 +00:00
|
|
|
KernelVersion uint32
|
2020-07-24 07:53:45 +00:00
|
|
|
|
|
|
|
// The BTF associated with this program. Changing Instructions
|
|
|
|
// will most likely invalidate the contained data, and may
|
|
|
|
// result in errors when attempting to load it into the kernel.
|
2023-01-30 14:43:31 +00:00
|
|
|
BTF *btf.Spec
|
2020-07-24 07:53:45 +00:00
|
|
|
|
|
|
|
// The byte order this program was compiled for, may be nil.
|
|
|
|
ByteOrder binary.ByteOrder
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy returns a copy of the spec.
|
|
|
|
func (ps *ProgramSpec) Copy() *ProgramSpec {
|
|
|
|
if ps == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cpy := *ps
|
|
|
|
cpy.Instructions = make(asm.Instructions, len(ps.Instructions))
|
|
|
|
copy(cpy.Instructions, ps.Instructions)
|
|
|
|
return &cpy
|
|
|
|
}
|
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
// Tag calculates the kernel tag for a series of instructions.
|
|
|
|
//
|
|
|
|
// Use asm.Instructions.Tag if you need to calculate for non-native endianness.
|
|
|
|
func (ps *ProgramSpec) Tag() (string, error) {
|
|
|
|
return ps.Instructions.Tag(internal.NativeEndian)
|
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
type VerifierError = internal.VerifierError
|
|
|
|
|
2020-03-10 12:09:25 +00:00
|
|
|
// Program represents BPF program loaded into the kernel.
|
|
|
|
//
|
|
|
|
// It is not safe to close a Program which is used by other goroutines.
|
|
|
|
type Program struct {
|
|
|
|
// Contains the output of the kernel verifier if enabled,
|
|
|
|
// otherwise it is empty.
|
|
|
|
VerifierLog string
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
fd *sys.FD
|
2020-07-24 07:53:45 +00:00
|
|
|
name string
|
2021-04-04 21:11:06 +00:00
|
|
|
pinnedPath string
|
|
|
|
typ ProgramType
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewProgram creates a new Program.
|
|
|
|
//
|
2023-01-30 14:43:31 +00:00
|
|
|
// See NewProgramWithOptions for details.
|
2020-03-10 12:09:25 +00:00
|
|
|
func NewProgram(spec *ProgramSpec) (*Program, error) {
|
|
|
|
return NewProgramWithOptions(spec, ProgramOptions{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewProgramWithOptions creates a new Program.
|
|
|
|
//
|
|
|
|
// Loading a program for the first time will perform
|
|
|
|
// feature detection by loading small, temporary programs.
|
2023-01-30 14:43:31 +00:00
|
|
|
//
|
|
|
|
// Returns an error wrapping VerifierError if the program or its BTF is rejected
|
|
|
|
// by the kernel.
|
2020-03-10 12:09:25 +00:00
|
|
|
func NewProgramWithOptions(spec *ProgramSpec, opts ProgramOptions) (*Program, error) {
|
2023-01-30 14:43:31 +00:00
|
|
|
if spec == nil {
|
|
|
|
return nil, errors.New("can't load a program from a nil spec")
|
|
|
|
}
|
|
|
|
|
2021-07-19 06:57:52 +00:00
|
|
|
handles := newHandleCache()
|
|
|
|
defer handles.close()
|
2021-04-04 21:11:06 +00:00
|
|
|
|
2021-07-19 06:57:52 +00:00
|
|
|
prog, err := newProgramWithOptions(spec, opts, handles)
|
2023-01-30 14:43:31 +00:00
|
|
|
if errors.Is(err, asm.ErrUnsatisfiedMapReference) {
|
2021-07-19 06:57:52 +00:00
|
|
|
return nil, fmt.Errorf("cannot load program without loading its whole collection: %w", err)
|
|
|
|
}
|
|
|
|
return prog, err
|
2021-04-04 21:11:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 06:57:52 +00:00
|
|
|
func newProgramWithOptions(spec *ProgramSpec, opts ProgramOptions, handles *handleCache) (*Program, error) {
|
2021-04-04 21:11:06 +00:00
|
|
|
if len(spec.Instructions) == 0 {
|
2022-02-06 07:29:51 +00:00
|
|
|
return nil, errors.New("instructions cannot be empty")
|
2021-04-04 21:11:06 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
if spec.Type == UnspecifiedProgram {
|
|
|
|
return nil, errors.New("can't load program of unspecified type")
|
|
|
|
}
|
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
if spec.ByteOrder != nil && spec.ByteOrder != internal.NativeEndian {
|
|
|
|
return nil, fmt.Errorf("can't load %s program on %s", spec.ByteOrder, internal.NativeEndian)
|
2020-07-24 07:53:45 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
// Kernels before 5.0 (6c4fc209fcf9 "bpf: remove useless version check for prog load")
|
|
|
|
// require the version field to be set to the value of the KERNEL_VERSION
|
|
|
|
// macro for kprobe-type programs.
|
|
|
|
// Overwrite Kprobe program version if set to zero or the magic version constant.
|
|
|
|
kv := spec.KernelVersion
|
|
|
|
if spec.Type == Kprobe && (kv == 0 || kv == internal.MagicKernelVersion) {
|
|
|
|
v, err := internal.KernelVersion()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("detecting kernel version: %w", err)
|
|
|
|
}
|
|
|
|
kv = v.Kernel()
|
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
attr := &sys.ProgLoadAttr{
|
|
|
|
ProgType: sys.ProgType(spec.Type),
|
2022-02-06 07:29:51 +00:00
|
|
|
ProgFlags: spec.Flags,
|
2023-01-30 14:43:31 +00:00
|
|
|
ExpectedAttachType: sys.AttachType(spec.AttachType),
|
|
|
|
License: sys.NewStringPointer(spec.License),
|
|
|
|
KernVersion: kv,
|
2021-04-04 21:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if haveObjName() == nil {
|
2023-01-30 14:43:31 +00:00
|
|
|
attr.ProgName = sys.NewObjName(spec.Name)
|
2021-07-19 06:57:52 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
kernelTypes := opts.KernelTypes
|
|
|
|
|
|
|
|
insns := make(asm.Instructions, len(spec.Instructions))
|
|
|
|
copy(insns, spec.Instructions)
|
2021-04-04 21:11:06 +00:00
|
|
|
|
|
|
|
var btfDisabled bool
|
|
|
|
if spec.BTF != nil {
|
2023-01-30 14:43:31 +00:00
|
|
|
if err := applyRelocations(insns, spec.BTF, kernelTypes); err != nil {
|
|
|
|
return nil, fmt.Errorf("apply CO-RE relocations: %w", err)
|
2021-04-04 21:11:06 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
handle, err := handles.btfHandle(spec.BTF)
|
2021-04-04 21:11:06 +00:00
|
|
|
btfDisabled = errors.Is(err, btf.ErrNotSupported)
|
|
|
|
if err != nil && !btfDisabled {
|
|
|
|
return nil, fmt.Errorf("load BTF: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if handle != nil {
|
2023-01-30 14:43:31 +00:00
|
|
|
attr.ProgBtfFd = uint32(handle.FD())
|
2021-04-04 21:11:06 +00:00
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
fib, lib, err := btf.MarshalExtInfos(insns, spec.BTF.TypeID)
|
2021-04-04 21:11:06 +00:00
|
|
|
if err != nil {
|
2023-01-30 14:43:31 +00:00
|
|
|
return nil, err
|
2021-04-04 21:11:06 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
attr.FuncInfoRecSize = btf.FuncInfoSize
|
|
|
|
attr.FuncInfoCnt = uint32(len(fib)) / btf.FuncInfoSize
|
|
|
|
attr.FuncInfo = sys.NewSlicePointer(fib)
|
2021-04-04 21:11:06 +00:00
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
attr.LineInfoRecSize = btf.LineInfoSize
|
|
|
|
attr.LineInfoCnt = uint32(len(lib)) / btf.LineInfoSize
|
|
|
|
attr.LineInfo = sys.NewSlicePointer(lib)
|
|
|
|
}
|
2021-07-19 06:57:52 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
if err := fixupAndValidate(insns); err != nil {
|
2021-07-19 06:57:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
buf := bytes.NewBuffer(make([]byte, 0, insns.Size()))
|
|
|
|
err := insns.Marshal(buf, internal.NativeEndian)
|
2021-07-19 06:57:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bytecode := buf.Bytes()
|
2023-01-30 14:43:31 +00:00
|
|
|
attr.Insns = sys.NewSlicePointer(bytecode)
|
|
|
|
attr.InsnCnt = uint32(len(bytecode) / asm.InstructionSize)
|
2022-02-06 07:29:51 +00:00
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
if spec.AttachTarget != nil {
|
|
|
|
targetID, err := findTargetInProgram(spec.AttachTarget, spec.AttachTo, spec.Type, spec.AttachType)
|
2021-04-04 21:11:06 +00:00
|
|
|
if err != nil {
|
2023-01-30 14:43:31 +00:00
|
|
|
return nil, fmt.Errorf("attach %s/%s: %w", spec.Type, spec.AttachType, err)
|
2021-04-04 21:11:06 +00:00
|
|
|
}
|
2023-01-30 14:43:31 +00:00
|
|
|
|
|
|
|
attr.AttachBtfId = uint32(targetID)
|
|
|
|
attr.AttachProgFd = uint32(spec.AttachTarget.FD())
|
|
|
|
defer runtime.KeepAlive(spec.AttachTarget)
|
|
|
|
} else if spec.AttachTo != "" {
|
|
|
|
targetID, err := findTargetInKernel(kernelTypes, spec.AttachTo, spec.Type, spec.AttachType)
|
|
|
|
if err != nil && !errors.Is(err, errUnrecognizedAttachType) {
|
|
|
|
// We ignore errUnrecognizedAttachType since AttachTo may be non-empty
|
|
|
|
// for programs that don't attach anywhere.
|
|
|
|
return nil, fmt.Errorf("attach %s/%s: %w", spec.Type, spec.AttachType, err)
|
2021-04-04 21:11:06 +00:00
|
|
|
}
|
2023-01-30 14:43:31 +00:00
|
|
|
|
|
|
|
attr.AttachBtfId = uint32(targetID)
|
2021-04-04 21:11:06 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 12:09:25 +00:00
|
|
|
logSize := DefaultVerifierLogSize
|
|
|
|
if opts.LogSize > 0 {
|
|
|
|
logSize = opts.LogSize
|
|
|
|
}
|
|
|
|
|
|
|
|
var logBuf []byte
|
|
|
|
if opts.LogLevel > 0 {
|
|
|
|
logBuf = make([]byte, logSize)
|
2022-02-06 07:29:51 +00:00
|
|
|
attr.LogLevel = opts.LogLevel
|
|
|
|
attr.LogSize = uint32(len(logBuf))
|
2023-01-30 14:43:31 +00:00
|
|
|
attr.LogBuf = sys.NewSlicePointer(logBuf)
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
fd, err := sys.ProgLoad(attr)
|
2020-03-10 12:09:25 +00:00
|
|
|
if err == nil {
|
2023-01-30 14:43:31 +00:00
|
|
|
return &Program{unix.ByteSliceToString(logBuf), fd, spec.Name, "", spec.Type}, nil
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 06:57:52 +00:00
|
|
|
if opts.LogLevel == 0 && opts.LogSize >= 0 {
|
2020-03-10 12:09:25 +00:00
|
|
|
// Re-run with the verifier enabled to get better error messages.
|
|
|
|
logBuf = make([]byte, logSize)
|
2022-02-06 07:29:51 +00:00
|
|
|
attr.LogLevel = 1
|
|
|
|
attr.LogSize = uint32(len(logBuf))
|
2023-01-30 14:43:31 +00:00
|
|
|
attr.LogBuf = sys.NewSlicePointer(logBuf)
|
|
|
|
_, _ = sys.ProgLoad(attr)
|
|
|
|
}
|
2020-03-10 12:09:25 +00:00
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, unix.EPERM):
|
|
|
|
if len(logBuf) > 0 && logBuf[0] == 0 {
|
|
|
|
// EPERM due to RLIMIT_MEMLOCK happens before the verifier, so we can
|
|
|
|
// check that the log is empty to reduce false positives.
|
|
|
|
return nil, fmt.Errorf("load program: %w (MEMLOCK may be too low, consider rlimit.RemoveMemlock)", err)
|
2022-02-06 07:29:51 +00:00
|
|
|
}
|
2020-03-10 12:09:25 +00:00
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
fallthrough
|
|
|
|
|
|
|
|
case errors.Is(err, unix.EINVAL):
|
|
|
|
if hasFunctionReferences(spec.Instructions) {
|
|
|
|
if err := haveBPFToBPFCalls(); err != nil {
|
|
|
|
return nil, fmt.Errorf("load program: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2021-04-04 21:11:06 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
err = internal.ErrorWithLog(err, logBuf)
|
2021-04-04 21:11:06 +00:00
|
|
|
if btfDisabled {
|
2023-01-30 14:43:31 +00:00
|
|
|
return nil, fmt.Errorf("load program: %w (BTF disabled)", err)
|
2021-04-04 21:11:06 +00:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("load program: %w", err)
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewProgramFromFD creates a program from a raw fd.
|
|
|
|
//
|
|
|
|
// You should not use fd after calling this function.
|
|
|
|
//
|
2021-04-04 21:11:06 +00:00
|
|
|
// Requires at least Linux 4.10.
|
2020-03-10 12:09:25 +00:00
|
|
|
func NewProgramFromFD(fd int) (*Program, error) {
|
2023-01-30 14:43:31 +00:00
|
|
|
f, err := sys.NewFD(fd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
return newProgramFromFD(f)
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
// NewProgramFromID returns the program for a given id.
|
|
|
|
//
|
|
|
|
// Returns ErrNotExist, if there is no eBPF program with the given id.
|
|
|
|
func NewProgramFromID(id ProgramID) (*Program, error) {
|
2023-01-30 14:43:31 +00:00
|
|
|
fd, err := sys.ProgGetFdById(&sys.ProgGetFdByIdAttr{
|
|
|
|
Id: uint32(id),
|
|
|
|
})
|
2020-03-10 12:09:25 +00:00
|
|
|
if err != nil {
|
2021-04-04 21:11:06 +00:00
|
|
|
return nil, fmt.Errorf("get program by id: %w", err)
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
return newProgramFromFD(fd)
|
|
|
|
}
|
2020-07-24 07:53:45 +00:00
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
func newProgramFromFD(fd *sys.FD) (*Program, error) {
|
2021-04-04 21:11:06 +00:00
|
|
|
info, err := newProgramInfoFromFd(fd)
|
|
|
|
if err != nil {
|
|
|
|
fd.Close()
|
|
|
|
return nil, fmt.Errorf("discover program type: %w", err)
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
return &Program{"", fd, "", "", info.Type}, nil
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Program) String() string {
|
|
|
|
if p.name != "" {
|
2021-04-04 21:11:06 +00:00
|
|
|
return fmt.Sprintf("%s(%s)#%v", p.typ, p.name, p.fd)
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
2021-04-04 21:11:06 +00:00
|
|
|
return fmt.Sprintf("%s(%v)", p.typ, p.fd)
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
// Type returns the underlying type of the program.
|
|
|
|
func (p *Program) Type() ProgramType {
|
|
|
|
return p.typ
|
|
|
|
}
|
|
|
|
|
|
|
|
// Info returns metadata about the program.
|
|
|
|
//
|
|
|
|
// Requires at least 4.10.
|
|
|
|
func (p *Program) Info() (*ProgramInfo, error) {
|
|
|
|
return newProgramInfoFromFd(p.fd)
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
// Handle returns a reference to the program's type information in the kernel.
|
2020-03-10 12:09:25 +00:00
|
|
|
//
|
2023-01-30 14:43:31 +00:00
|
|
|
// Returns ErrNotSupported if the kernel has no BTF support, or if there is no
|
|
|
|
// BTF associated with the program.
|
|
|
|
func (p *Program) Handle() (*btf.Handle, error) {
|
|
|
|
info, err := p.Info()
|
2020-03-10 12:09:25 +00:00
|
|
|
if err != nil {
|
2023-01-30 14:43:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, ok := info.BTFID()
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("program %s: retrieve BTF ID: %w", p, ErrNotSupported)
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
return btf.NewHandleFromID(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FD gets the file descriptor of the Program.
|
|
|
|
//
|
|
|
|
// It is invalid to call this function after Close has been called.
|
|
|
|
func (p *Program) FD() int {
|
|
|
|
return p.fd.Int()
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clone creates a duplicate of the Program.
|
|
|
|
//
|
|
|
|
// Closing the duplicate does not affect the original, and vice versa.
|
|
|
|
//
|
|
|
|
// Cloning a nil Program returns nil.
|
|
|
|
func (p *Program) Clone() (*Program, error) {
|
|
|
|
if p == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-07-24 07:53:45 +00:00
|
|
|
dup, err := p.fd.Dup()
|
2020-03-10 12:09:25 +00:00
|
|
|
if err != nil {
|
2020-07-24 07:53:45 +00:00
|
|
|
return nil, fmt.Errorf("can't clone program: %w", err)
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
return &Program{p.VerifierLog, dup, p.name, "", p.typ}, nil
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
// Pin persists the Program on the BPF virtual file system past the lifetime of
|
|
|
|
// the process that created it
|
2020-03-10 12:09:25 +00:00
|
|
|
//
|
2021-04-04 21:11:06 +00:00
|
|
|
// Calling Pin on a previously pinned program will overwrite the path, except when
|
|
|
|
// the new path already exists. Re-pinning across filesystems is not supported.
|
|
|
|
//
|
|
|
|
// This requires bpffs to be mounted above fileName. See https://docs.cilium.io/en/k8s-doc/admin/#admin-mount-bpffs
|
2020-03-10 12:09:25 +00:00
|
|
|
func (p *Program) Pin(fileName string) error {
|
2021-04-04 21:11:06 +00:00
|
|
|
if err := internal.Pin(p.pinnedPath, fileName, p.fd); err != nil {
|
|
|
|
return err
|
2020-07-24 07:53:45 +00:00
|
|
|
}
|
2021-04-04 21:11:06 +00:00
|
|
|
p.pinnedPath = fileName
|
2020-07-24 07:53:45 +00:00
|
|
|
return nil
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
// Unpin removes the persisted state for the Program from the BPF virtual filesystem.
|
|
|
|
//
|
|
|
|
// Failed calls to Unpin will not alter the state returned by IsPinned.
|
|
|
|
//
|
|
|
|
// Unpinning an unpinned Program returns nil.
|
|
|
|
func (p *Program) Unpin() error {
|
|
|
|
if err := internal.Unpin(p.pinnedPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p.pinnedPath = ""
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsPinned returns true if the Program has a non-empty pinned path.
|
|
|
|
func (p *Program) IsPinned() bool {
|
|
|
|
return p.pinnedPath != ""
|
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
// Close the Program's underlying file descriptor, which could unload
|
|
|
|
// the program from the kernel if it is not pinned or attached to a
|
|
|
|
// kernel hook.
|
2020-03-10 12:09:25 +00:00
|
|
|
func (p *Program) Close() error {
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-24 07:53:45 +00:00
|
|
|
return p.fd.Close()
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
// Various options for Run'ing a Program
|
|
|
|
type RunOptions struct {
|
|
|
|
// Program's data input. Required field.
|
|
|
|
Data []byte
|
|
|
|
// Program's data after Program has run. Caller must allocate. Optional field.
|
|
|
|
DataOut []byte
|
|
|
|
// Program's context input. Optional field.
|
|
|
|
Context interface{}
|
|
|
|
// Program's context after Program has run. Must be a pointer or slice. Optional field.
|
|
|
|
ContextOut interface{}
|
|
|
|
// Number of times to run Program. Optional field. Defaults to 1.
|
|
|
|
Repeat uint32
|
|
|
|
// Optional flags.
|
|
|
|
Flags uint32
|
|
|
|
// CPU to run Program on. Optional field.
|
|
|
|
// Note not all program types support this field.
|
|
|
|
CPU uint32
|
|
|
|
// Called whenever the syscall is interrupted, and should be set to testing.B.ResetTimer
|
|
|
|
// or similar. Typically used during benchmarking. Optional field.
|
|
|
|
Reset func()
|
|
|
|
}
|
|
|
|
|
2020-03-10 12:09:25 +00:00
|
|
|
// Test runs the Program in the kernel with the given input and returns the
|
|
|
|
// value returned by the eBPF program. outLen may be zero.
|
|
|
|
//
|
|
|
|
// Note: the kernel expects at least 14 bytes input for an ethernet header for
|
|
|
|
// XDP and SKB programs.
|
|
|
|
//
|
|
|
|
// This function requires at least Linux 4.12.
|
|
|
|
func (p *Program) Test(in []byte) (uint32, []byte, error) {
|
2023-01-30 14:43:31 +00:00
|
|
|
// Older kernels ignore the dataSizeOut argument when copying to user space.
|
|
|
|
// Combined with things like bpf_xdp_adjust_head() we don't really know what the final
|
|
|
|
// size will be. Hence we allocate an output buffer which we hope will always be large
|
|
|
|
// enough, and panic if the kernel wrote past the end of the allocation.
|
|
|
|
// See https://patchwork.ozlabs.org/cover/1006822/
|
|
|
|
var out []byte
|
|
|
|
if len(in) > 0 {
|
|
|
|
out = make([]byte, len(in)+outputPad)
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := RunOptions{
|
|
|
|
Data: in,
|
|
|
|
DataOut: out,
|
|
|
|
Repeat: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, _, err := p.testRun(&opts)
|
2020-07-24 07:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return ret, nil, fmt.Errorf("can't test program: %w", err)
|
|
|
|
}
|
2023-01-30 14:43:31 +00:00
|
|
|
return ret, opts.DataOut, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs the Program in kernel with given RunOptions.
|
|
|
|
//
|
|
|
|
// Note: the same restrictions from Test apply.
|
|
|
|
func (p *Program) Run(opts *RunOptions) (uint32, error) {
|
|
|
|
ret, _, err := p.testRun(opts)
|
|
|
|
if err != nil {
|
|
|
|
return ret, fmt.Errorf("can't test program: %w", err)
|
|
|
|
}
|
|
|
|
return ret, nil
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Benchmark runs the Program with the given input for a number of times
|
|
|
|
// and returns the time taken per iteration.
|
|
|
|
//
|
2020-07-24 07:53:45 +00:00
|
|
|
// Returns the result of the last execution of the program and the time per
|
|
|
|
// run or an error. reset is called whenever the benchmark syscall is
|
|
|
|
// interrupted, and should be set to testing.B.ResetTimer or similar.
|
|
|
|
//
|
|
|
|
// Note: profiling a call to this function will skew it's results, see
|
|
|
|
// https://github.com/cilium/ebpf/issues/24
|
2020-03-10 12:09:25 +00:00
|
|
|
//
|
|
|
|
// This function requires at least Linux 4.12.
|
2020-07-24 07:53:45 +00:00
|
|
|
func (p *Program) Benchmark(in []byte, repeat int, reset func()) (uint32, time.Duration, error) {
|
2023-01-30 14:43:31 +00:00
|
|
|
if uint(repeat) > math.MaxUint32 {
|
|
|
|
return 0, 0, fmt.Errorf("repeat is too high")
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := RunOptions{
|
|
|
|
Data: in,
|
|
|
|
Repeat: uint32(repeat),
|
|
|
|
Reset: reset,
|
|
|
|
}
|
|
|
|
|
|
|
|
ret, total, err := p.testRun(&opts)
|
2020-07-24 07:53:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return ret, total, fmt.Errorf("can't benchmark program: %w", err)
|
|
|
|
}
|
|
|
|
return ret, total, nil
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
var haveProgTestRun = internal.FeatureTest("BPF_PROG_TEST_RUN", "4.12", func() error {
|
2020-03-10 12:09:25 +00:00
|
|
|
prog, err := NewProgram(&ProgramSpec{
|
2023-01-30 14:43:31 +00:00
|
|
|
// SocketFilter does not require privileges on newer kernels.
|
2020-03-10 12:09:25 +00:00
|
|
|
Type: SocketFilter,
|
|
|
|
Instructions: asm.Instructions{
|
|
|
|
asm.LoadImm(asm.R0, 0, asm.DWord),
|
|
|
|
asm.Return(),
|
|
|
|
},
|
|
|
|
License: "MIT",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
// This may be because we lack sufficient permissions, etc.
|
2021-04-04 21:11:06 +00:00
|
|
|
return err
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
defer prog.Close()
|
|
|
|
|
|
|
|
// Programs require at least 14 bytes input
|
|
|
|
in := make([]byte, 14)
|
2023-01-30 14:43:31 +00:00
|
|
|
attr := sys.ProgRunAttr{
|
|
|
|
ProgFd: uint32(prog.FD()),
|
|
|
|
DataSizeIn: uint32(len(in)),
|
|
|
|
DataIn: sys.NewSlicePointer(in),
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
err = sys.ProgRun(&attr)
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, unix.EINVAL):
|
2021-04-04 21:11:06 +00:00
|
|
|
// Check for EINVAL specifically, rather than err != nil since we
|
|
|
|
// otherwise misdetect due to insufficient permissions.
|
|
|
|
return internal.ErrNotSupported
|
2023-01-30 14:43:31 +00:00
|
|
|
|
|
|
|
case errors.Is(err, unix.EINTR):
|
2021-04-04 21:11:06 +00:00
|
|
|
// We know that PROG_TEST_RUN is supported if we get EINTR.
|
|
|
|
return nil
|
2020-03-10 12:09:25 +00:00
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
case errors.Is(err, unix.ENOTSUPP):
|
|
|
|
// The first PROG_TEST_RUN patches shipped in 4.12 didn't include
|
|
|
|
// a test runner for SocketFilter. ENOTSUPP means PROG_TEST_RUN is
|
|
|
|
// supported, but not for the program type used in the probe.
|
|
|
|
return nil
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
return err
|
|
|
|
})
|
2020-03-10 12:09:25 +00:00
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
func (p *Program) testRun(opts *RunOptions) (uint32, time.Duration, error) {
|
|
|
|
if uint(len(opts.Data)) > math.MaxUint32 {
|
|
|
|
return 0, 0, fmt.Errorf("input is too long")
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := haveProgTestRun(); err != nil {
|
2023-01-30 14:43:31 +00:00
|
|
|
return 0, 0, err
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
var ctxBytes []byte
|
|
|
|
if opts.Context != nil {
|
|
|
|
ctx := new(bytes.Buffer)
|
|
|
|
if err := binary.Write(ctx, internal.NativeEndian, opts.Context); err != nil {
|
|
|
|
return 0, 0, fmt.Errorf("cannot serialize context: %v", err)
|
|
|
|
}
|
|
|
|
ctxBytes = ctx.Bytes()
|
|
|
|
}
|
2020-03-10 12:09:25 +00:00
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
var ctxOut []byte
|
|
|
|
if opts.ContextOut != nil {
|
|
|
|
ctxOut = make([]byte, binary.Size(opts.ContextOut))
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
attr := sys.ProgRunAttr{
|
|
|
|
ProgFd: p.fd.Uint(),
|
|
|
|
DataSizeIn: uint32(len(opts.Data)),
|
|
|
|
DataSizeOut: uint32(len(opts.DataOut)),
|
|
|
|
DataIn: sys.NewSlicePointer(opts.Data),
|
|
|
|
DataOut: sys.NewSlicePointer(opts.DataOut),
|
|
|
|
Repeat: uint32(opts.Repeat),
|
|
|
|
CtxSizeIn: uint32(len(ctxBytes)),
|
|
|
|
CtxSizeOut: uint32(len(ctxOut)),
|
|
|
|
CtxIn: sys.NewSlicePointer(ctxBytes),
|
|
|
|
CtxOut: sys.NewSlicePointer(ctxOut),
|
|
|
|
Flags: opts.Flags,
|
|
|
|
Cpu: opts.CPU,
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 07:53:45 +00:00
|
|
|
for {
|
2023-01-30 14:43:31 +00:00
|
|
|
err := sys.ProgRun(&attr)
|
2020-07-24 07:53:45 +00:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(err, unix.EINTR) {
|
2023-01-30 14:43:31 +00:00
|
|
|
if opts.Reset != nil {
|
|
|
|
opts.Reset()
|
2020-07-24 07:53:45 +00:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
if errors.Is(err, unix.ENOTSUPP) {
|
|
|
|
return 0, 0, fmt.Errorf("kernel doesn't support testing program type %s: %w", p.Type(), ErrNotSupported)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, 0, fmt.Errorf("can't run test: %w", err)
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
if opts.DataOut != nil {
|
|
|
|
if int(attr.DataSizeOut) > cap(opts.DataOut) {
|
|
|
|
// Houston, we have a problem. The program created more data than we allocated,
|
|
|
|
// and the kernel wrote past the end of our buffer.
|
|
|
|
panic("kernel wrote past end of output buffer")
|
|
|
|
}
|
|
|
|
opts.DataOut = opts.DataOut[:int(attr.DataSizeOut)]
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
if len(ctxOut) != 0 {
|
|
|
|
b := bytes.NewReader(ctxOut)
|
|
|
|
if err := binary.Read(b, internal.NativeEndian, opts.ContextOut); err != nil {
|
|
|
|
return 0, 0, fmt.Errorf("failed to decode ContextOut: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
total := time.Duration(attr.Duration) * time.Nanosecond
|
|
|
|
return attr.Retval, total, nil
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalProgram(buf []byte) (*Program, error) {
|
|
|
|
if len(buf) != 4 {
|
|
|
|
return nil, errors.New("program id requires 4 byte value")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Looking up an entry in a nested map or prog array returns an id,
|
|
|
|
// not an fd.
|
|
|
|
id := internal.NativeEndian.Uint32(buf)
|
2020-07-24 07:53:45 +00:00
|
|
|
return NewProgramFromID(ProgramID(id))
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
func marshalProgram(p *Program, length int) ([]byte, error) {
|
|
|
|
if length != 4 {
|
|
|
|
return nil, fmt.Errorf("can't marshal program to %d bytes", length)
|
|
|
|
}
|
|
|
|
|
2020-03-10 12:09:25 +00:00
|
|
|
buf := make([]byte, 4)
|
2023-01-30 14:43:31 +00:00
|
|
|
internal.NativeEndian.PutUint32(buf, p.fd.Uint())
|
2020-03-10 12:09:25 +00:00
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadPinnedProgram loads a Program from a BPF file.
|
|
|
|
//
|
|
|
|
// Requires at least Linux 4.11.
|
2021-04-04 21:11:06 +00:00
|
|
|
func LoadPinnedProgram(fileName string, opts *LoadPinOptions) (*Program, error) {
|
2023-01-30 14:43:31 +00:00
|
|
|
fd, err := sys.ObjGet(&sys.ObjGetAttr{
|
|
|
|
Pathname: sys.NewStringPointer(fileName),
|
|
|
|
FileFlags: opts.Marshal(),
|
|
|
|
})
|
2020-03-10 12:09:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
info, err := newProgramInfoFromFd(fd)
|
2020-03-10 12:09:25 +00:00
|
|
|
if err != nil {
|
2020-07-24 07:53:45 +00:00
|
|
|
_ = fd.Close()
|
2021-04-04 21:11:06 +00:00
|
|
|
return nil, fmt.Errorf("info for %s: %w", fileName, err)
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
return &Program{"", fd, filepath.Base(fileName), fileName, info.Type}, nil
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 21:11:06 +00:00
|
|
|
// SanitizeName replaces all invalid characters in name with replacement.
|
|
|
|
// Passing a negative value for replacement will delete characters instead
|
|
|
|
// of replacing them. Use this to automatically generate valid names for maps
|
|
|
|
// and programs at runtime.
|
2020-03-10 12:09:25 +00:00
|
|
|
//
|
2021-04-04 21:11:06 +00:00
|
|
|
// The set of allowed characters depends on the running kernel version.
|
|
|
|
// Dots are only allowed as of kernel 5.2.
|
2020-03-10 12:09:25 +00:00
|
|
|
func SanitizeName(name string, replacement rune) string {
|
|
|
|
return strings.Map(func(char rune) rune {
|
|
|
|
if invalidBPFObjNameChar(char) {
|
|
|
|
return replacement
|
|
|
|
}
|
|
|
|
return char
|
|
|
|
}, name)
|
|
|
|
}
|
|
|
|
|
2020-07-24 07:53:45 +00:00
|
|
|
// ProgramGetNextID returns the ID of the next eBPF program.
|
|
|
|
//
|
|
|
|
// Returns ErrNotExist, if there is no next eBPF program.
|
|
|
|
func ProgramGetNextID(startID ProgramID) (ProgramID, error) {
|
2023-01-30 14:43:31 +00:00
|
|
|
attr := &sys.ProgGetNextIdAttr{Id: uint32(startID)}
|
|
|
|
return ProgramID(attr.NextId), sys.ProgGetNextId(attr)
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
// BindMap binds map to the program and is only released once program is released.
|
2021-04-04 21:11:06 +00:00
|
|
|
//
|
2023-01-30 14:43:31 +00:00
|
|
|
// This may be used in cases where metadata should be associated with the program
|
|
|
|
// which otherwise does not contain any references to the map.
|
|
|
|
func (p *Program) BindMap(m *Map) error {
|
|
|
|
attr := &sys.ProgBindMapAttr{
|
|
|
|
ProgFd: uint32(p.FD()),
|
|
|
|
MapFd: uint32(m.FD()),
|
2020-07-24 07:53:45 +00:00
|
|
|
}
|
2023-01-30 14:43:31 +00:00
|
|
|
|
|
|
|
return sys.ProgBindMap(attr)
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
var errUnrecognizedAttachType = errors.New("unrecognized attach type")
|
|
|
|
|
|
|
|
// find an attach target type in the kernel.
|
|
|
|
//
|
|
|
|
// spec may be nil and defaults to the canonical kernel BTF. name together with
|
|
|
|
// progType and attachType determine which type we need to attach to.
|
|
|
|
//
|
|
|
|
// Returns errUnrecognizedAttachType.
|
|
|
|
func findTargetInKernel(spec *btf.Spec, name string, progType ProgramType, attachType AttachType) (btf.TypeID, error) {
|
2020-07-24 07:53:45 +00:00
|
|
|
type match struct {
|
|
|
|
p ProgramType
|
|
|
|
a AttachType
|
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
var (
|
|
|
|
typeName, featureName string
|
|
|
|
isBTFTypeFunc = true
|
|
|
|
)
|
|
|
|
|
2021-07-19 06:57:52 +00:00
|
|
|
switch (match{progType, attachType}) {
|
2021-04-04 21:11:06 +00:00
|
|
|
case match{LSM, AttachLSMMac}:
|
2021-07-19 06:57:52 +00:00
|
|
|
typeName = "bpf_lsm_" + name
|
|
|
|
featureName = name + " LSM hook"
|
2020-07-24 07:53:45 +00:00
|
|
|
case match{Tracing, AttachTraceIter}:
|
2021-07-19 06:57:52 +00:00
|
|
|
typeName = "bpf_iter_" + name
|
|
|
|
featureName = name + " iterator"
|
2023-01-30 14:43:31 +00:00
|
|
|
case match{Tracing, AttachTraceFEntry}:
|
|
|
|
typeName = name
|
|
|
|
featureName = fmt.Sprintf("fentry %s", name)
|
|
|
|
case match{Tracing, AttachTraceFExit}:
|
|
|
|
typeName = name
|
|
|
|
featureName = fmt.Sprintf("fexit %s", name)
|
|
|
|
case match{Tracing, AttachModifyReturn}:
|
2022-02-06 07:29:51 +00:00
|
|
|
typeName = name
|
2023-01-30 14:43:31 +00:00
|
|
|
featureName = fmt.Sprintf("fmod_ret %s", name)
|
|
|
|
case match{Tracing, AttachTraceRawTp}:
|
|
|
|
typeName = fmt.Sprintf("btf_trace_%s", name)
|
|
|
|
featureName = fmt.Sprintf("raw_tp %s", name)
|
|
|
|
isBTFTypeFunc = false
|
2020-07-24 07:53:45 +00:00
|
|
|
default:
|
2023-01-30 14:43:31 +00:00
|
|
|
return 0, errUnrecognizedAttachType
|
2020-07-24 07:53:45 +00:00
|
|
|
}
|
2021-07-19 06:57:52 +00:00
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
spec, err := maybeLoadKernelBTF(spec)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("load kernel spec: %w", err)
|
2021-07-19 06:57:52 +00:00
|
|
|
}
|
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
var target btf.Type
|
|
|
|
if isBTFTypeFunc {
|
|
|
|
var targetFunc *btf.Func
|
|
|
|
err = spec.TypeByName(typeName, &targetFunc)
|
|
|
|
target = targetFunc
|
|
|
|
} else {
|
|
|
|
var targetTypedef *btf.Typedef
|
|
|
|
err = spec.TypeByName(typeName, &targetTypedef)
|
|
|
|
target = targetTypedef
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, btf.ErrNotFound) {
|
|
|
|
return 0, &internal.UnsupportedFeatureError{
|
|
|
|
Name: featureName,
|
|
|
|
}
|
2021-07-19 06:57:52 +00:00
|
|
|
}
|
2023-01-30 14:43:31 +00:00
|
|
|
return 0, fmt.Errorf("find target for %s: %w", featureName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return spec.TypeID(target)
|
|
|
|
}
|
|
|
|
|
|
|
|
// find an attach target type in a program.
|
|
|
|
//
|
|
|
|
// Returns errUnrecognizedAttachType.
|
|
|
|
func findTargetInProgram(prog *Program, name string, progType ProgramType, attachType AttachType) (btf.TypeID, error) {
|
|
|
|
type match struct {
|
|
|
|
p ProgramType
|
|
|
|
a AttachType
|
|
|
|
}
|
|
|
|
|
|
|
|
var typeName string
|
|
|
|
switch (match{progType, attachType}) {
|
|
|
|
case match{Extension, AttachNone}:
|
|
|
|
typeName = name
|
|
|
|
default:
|
|
|
|
return 0, errUnrecognizedAttachType
|
2021-07-19 06:57:52 +00:00
|
|
|
}
|
2023-01-30 14:43:31 +00:00
|
|
|
|
|
|
|
btfHandle, err := prog.Handle()
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("load target BTF: %w", err)
|
|
|
|
}
|
|
|
|
defer btfHandle.Close()
|
|
|
|
|
|
|
|
spec, err := btfHandle.Spec(nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var targetFunc *btf.Func
|
|
|
|
err = spec.TypeByName(typeName, &targetFunc)
|
2021-07-19 06:57:52 +00:00
|
|
|
if err != nil {
|
2023-01-30 14:43:31 +00:00
|
|
|
return 0, fmt.Errorf("find target %s: %w", typeName, err)
|
2021-07-19 06:57:52 +00:00
|
|
|
}
|
2022-02-06 07:29:51 +00:00
|
|
|
|
2023-01-30 14:43:31 +00:00
|
|
|
return spec.TypeID(targetFunc)
|
2020-03-10 12:09:25 +00:00
|
|
|
}
|