build(deps): bump github.com/containerd/cgroups/v3 from 3.0.2 to 3.0.3
Bumps [github.com/containerd/cgroups/v3](https://github.com/containerd/cgroups) from 3.0.2 to 3.0.3. - [Release notes](https://github.com/containerd/cgroups/releases) - [Commits](https://github.com/containerd/cgroups/compare/v3.0.2...v3.0.3) --- updated-dependencies: - dependency-name: github.com/containerd/cgroups/v3 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com>
This commit is contained in:
		
							
								
								
									
										391
									
								
								vendor/github.com/cilium/ebpf/prog.go
									
									
									
										generated
									
									
										vendored
									
									
								
							
							
						
						
									
										391
									
								
								vendor/github.com/cilium/ebpf/prog.go
									
									
									
										generated
									
									
										vendored
									
									
								
							@@ -10,6 +10,7 @@ import (
 | 
			
		||||
	"runtime"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
	"unsafe"
 | 
			
		||||
 | 
			
		||||
	"github.com/cilium/ebpf/asm"
 | 
			
		||||
	"github.com/cilium/ebpf/btf"
 | 
			
		||||
@@ -35,16 +36,44 @@ const (
 | 
			
		||||
// verifier log.
 | 
			
		||||
const DefaultVerifierLogSize = 64 * 1024
 | 
			
		||||
 | 
			
		||||
// maxVerifierLogSize is the maximum size of verifier log buffer the kernel
 | 
			
		||||
// will accept before returning EINVAL.
 | 
			
		||||
const maxVerifierLogSize = math.MaxUint32 >> 2
 | 
			
		||||
 | 
			
		||||
// 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.
 | 
			
		||||
	// Bitmap controlling the detail emitted by the kernel's eBPF verifier log.
 | 
			
		||||
	// LogLevel-type values can be ORed together to request specific kinds of
 | 
			
		||||
	// verifier output. See the documentation on [ebpf.LogLevel] for details.
 | 
			
		||||
	//
 | 
			
		||||
	//  opts.LogLevel = (ebpf.LogLevelBranch | ebpf.LogLevelStats)
 | 
			
		||||
	//
 | 
			
		||||
	// If left to its default value, the program will first be loaded without
 | 
			
		||||
	// verifier output enabled. Upon error, the program load will be repeated
 | 
			
		||||
	// with LogLevelBranch and the given (or default) LogSize value.
 | 
			
		||||
	//
 | 
			
		||||
	// Setting this to a non-zero value will unconditionally enable the verifier
 | 
			
		||||
	// log, populating the [ebpf.Program.VerifierLog] field on successful loads
 | 
			
		||||
	// and including detailed verifier errors if the program is rejected. This
 | 
			
		||||
	// will always allocate an output buffer, but will result in only a single
 | 
			
		||||
	// attempt at loading the program.
 | 
			
		||||
	LogLevel LogLevel
 | 
			
		||||
 | 
			
		||||
	// Controls the output buffer size for the verifier log, in bytes. See the
 | 
			
		||||
	// documentation on ProgramOptions.LogLevel for details about how this value
 | 
			
		||||
	// is used.
 | 
			
		||||
	//
 | 
			
		||||
	// If this value is set too low to fit the verifier log, the resulting
 | 
			
		||||
	// [ebpf.VerifierError]'s Truncated flag will be true, and the error string
 | 
			
		||||
	// will also contain a hint to that effect.
 | 
			
		||||
	//
 | 
			
		||||
	// Defaults to DefaultVerifierLogSize.
 | 
			
		||||
	LogSize int
 | 
			
		||||
	// Type information used for CO-RE relocations and when attaching to
 | 
			
		||||
	// kernel functions.
 | 
			
		||||
 | 
			
		||||
	// Disables the verifier log completely, regardless of other options.
 | 
			
		||||
	LogDisabled bool
 | 
			
		||||
 | 
			
		||||
	// Type information used for CO-RE relocations.
 | 
			
		||||
	//
 | 
			
		||||
	// This is useful in environments where the kernel BTF is not available
 | 
			
		||||
	// (containers) or where it is in a non-standard location. Defaults to
 | 
			
		||||
@@ -74,7 +103,7 @@ type ProgramSpec struct {
 | 
			
		||||
	// The program to attach to. Must be provided manually.
 | 
			
		||||
	AttachTarget *Program
 | 
			
		||||
 | 
			
		||||
	// The name of the ELF section this program orininated from.
 | 
			
		||||
	// The name of the ELF section this program originated from.
 | 
			
		||||
	SectionName string
 | 
			
		||||
 | 
			
		||||
	Instructions asm.Instructions
 | 
			
		||||
@@ -95,11 +124,6 @@ type ProgramSpec struct {
 | 
			
		||||
	// detect this value automatically.
 | 
			
		||||
	KernelVersion uint32
 | 
			
		||||
 | 
			
		||||
	// 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.
 | 
			
		||||
	BTF *btf.Spec
 | 
			
		||||
 | 
			
		||||
	// The byte order this program was compiled for, may be nil.
 | 
			
		||||
	ByteOrder binary.ByteOrder
 | 
			
		||||
}
 | 
			
		||||
@@ -123,6 +147,10 @@ func (ps *ProgramSpec) Tag() (string, error) {
 | 
			
		||||
	return ps.Instructions.Tag(internal.NativeEndian)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// VerifierError is returned by [NewProgram] and [NewProgramWithOptions] if a
 | 
			
		||||
// program is rejected by the verifier.
 | 
			
		||||
//
 | 
			
		||||
// Use [errors.As] to access the error.
 | 
			
		||||
type VerifierError = internal.VerifierError
 | 
			
		||||
 | 
			
		||||
// Program represents BPF program loaded into the kernel.
 | 
			
		||||
@@ -141,7 +169,10 @@ type Program struct {
 | 
			
		||||
 | 
			
		||||
// NewProgram creates a new Program.
 | 
			
		||||
//
 | 
			
		||||
// See NewProgramWithOptions for details.
 | 
			
		||||
// See [NewProgramWithOptions] for details.
 | 
			
		||||
//
 | 
			
		||||
// Returns a [VerifierError] containing the full verifier log if the program is
 | 
			
		||||
// rejected by the kernel.
 | 
			
		||||
func NewProgram(spec *ProgramSpec) (*Program, error) {
 | 
			
		||||
	return NewProgramWithOptions(spec, ProgramOptions{})
 | 
			
		||||
}
 | 
			
		||||
@@ -151,24 +182,21 @@ func NewProgram(spec *ProgramSpec) (*Program, error) {
 | 
			
		||||
// Loading a program for the first time will perform
 | 
			
		||||
// feature detection by loading small, temporary programs.
 | 
			
		||||
//
 | 
			
		||||
// Returns an error wrapping VerifierError if the program or its BTF is rejected
 | 
			
		||||
// by the kernel.
 | 
			
		||||
// Returns a [VerifierError] containing the full verifier log if the program is
 | 
			
		||||
// rejected by the kernel.
 | 
			
		||||
func NewProgramWithOptions(spec *ProgramSpec, opts ProgramOptions) (*Program, error) {
 | 
			
		||||
	if spec == nil {
 | 
			
		||||
		return nil, errors.New("can't load a program from a nil spec")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	handles := newHandleCache()
 | 
			
		||||
	defer handles.close()
 | 
			
		||||
 | 
			
		||||
	prog, err := newProgramWithOptions(spec, opts, handles)
 | 
			
		||||
	prog, err := newProgramWithOptions(spec, opts)
 | 
			
		||||
	if errors.Is(err, asm.ErrUnsatisfiedMapReference) {
 | 
			
		||||
		return nil, fmt.Errorf("cannot load program without loading its whole collection: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
	return prog, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func newProgramWithOptions(spec *ProgramSpec, opts ProgramOptions, handles *handleCache) (*Program, error) {
 | 
			
		||||
func newProgramWithOptions(spec *ProgramSpec, opts ProgramOptions) (*Program, error) {
 | 
			
		||||
	if len(spec.Instructions) == 0 {
 | 
			
		||||
		return nil, errors.New("instructions cannot be empty")
 | 
			
		||||
	}
 | 
			
		||||
@@ -181,6 +209,10 @@ func newProgramWithOptions(spec *ProgramSpec, opts ProgramOptions, handles *hand
 | 
			
		||||
		return nil, fmt.Errorf("can't load %s program on %s", spec.ByteOrder, internal.NativeEndian)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if opts.LogSize < 0 {
 | 
			
		||||
		return nil, errors.New("ProgramOptions.LogSize must be a positive value; disable verifier logs using ProgramOptions.LogDisabled")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 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.
 | 
			
		||||
@@ -206,47 +238,54 @@ func newProgramWithOptions(spec *ProgramSpec, opts ProgramOptions, handles *hand
 | 
			
		||||
		attr.ProgName = sys.NewObjName(spec.Name)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	kernelTypes := opts.KernelTypes
 | 
			
		||||
 | 
			
		||||
	insns := make(asm.Instructions, len(spec.Instructions))
 | 
			
		||||
	copy(insns, spec.Instructions)
 | 
			
		||||
 | 
			
		||||
	var btfDisabled bool
 | 
			
		||||
	if spec.BTF != nil {
 | 
			
		||||
		if err := applyRelocations(insns, spec.BTF, kernelTypes); err != nil {
 | 
			
		||||
			return nil, fmt.Errorf("apply CO-RE relocations: %w", err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		handle, err := handles.btfHandle(spec.BTF)
 | 
			
		||||
		btfDisabled = errors.Is(err, btf.ErrNotSupported)
 | 
			
		||||
		if err != nil && !btfDisabled {
 | 
			
		||||
			return nil, fmt.Errorf("load BTF: %w", err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if handle != nil {
 | 
			
		||||
			attr.ProgBtfFd = uint32(handle.FD())
 | 
			
		||||
 | 
			
		||||
			fib, lib, err := btf.MarshalExtInfos(insns, spec.BTF.TypeID)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return nil, err
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			attr.FuncInfoRecSize = btf.FuncInfoSize
 | 
			
		||||
			attr.FuncInfoCnt = uint32(len(fib)) / btf.FuncInfoSize
 | 
			
		||||
			attr.FuncInfo = sys.NewSlicePointer(fib)
 | 
			
		||||
 | 
			
		||||
			attr.LineInfoRecSize = btf.LineInfoSize
 | 
			
		||||
			attr.LineInfoCnt = uint32(len(lib)) / btf.LineInfoSize
 | 
			
		||||
			attr.LineInfo = sys.NewSlicePointer(lib)
 | 
			
		||||
		}
 | 
			
		||||
	handle, fib, lib, err := btf.MarshalExtInfos(insns)
 | 
			
		||||
	if err != nil && !errors.Is(err, btf.ErrNotSupported) {
 | 
			
		||||
		return nil, fmt.Errorf("load ext_infos: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
	if handle != nil {
 | 
			
		||||
		defer handle.Close()
 | 
			
		||||
 | 
			
		||||
		attr.ProgBtfFd = uint32(handle.FD())
 | 
			
		||||
 | 
			
		||||
		attr.FuncInfoRecSize = btf.FuncInfoSize
 | 
			
		||||
		attr.FuncInfoCnt = uint32(len(fib)) / btf.FuncInfoSize
 | 
			
		||||
		attr.FuncInfo = sys.NewSlicePointer(fib)
 | 
			
		||||
 | 
			
		||||
		attr.LineInfoRecSize = btf.LineInfoSize
 | 
			
		||||
		attr.LineInfoCnt = uint32(len(lib)) / btf.LineInfoSize
 | 
			
		||||
		attr.LineInfo = sys.NewSlicePointer(lib)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := applyRelocations(insns, opts.KernelTypes, spec.ByteOrder); err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("apply CO-RE relocations: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	kconfig, err := resolveKconfigReferences(insns)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("resolve .kconfig: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
	defer kconfig.Close()
 | 
			
		||||
 | 
			
		||||
	if err := fixupAndValidate(insns); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	handles, err := fixupKfuncs(insns)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("fixing up kfuncs: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
	defer handles.close()
 | 
			
		||||
 | 
			
		||||
	if len(handles) > 0 {
 | 
			
		||||
		fdArray := handles.fdArray()
 | 
			
		||||
		attr.FdArray = sys.NewPointer(unsafe.Pointer(&fdArray[0]))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	buf := bytes.NewBuffer(make([]byte, 0, insns.Size()))
 | 
			
		||||
	err := insns.Marshal(buf, internal.NativeEndian)
 | 
			
		||||
	err = insns.Marshal(buf, internal.NativeEndian)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -261,28 +300,32 @@ func newProgramWithOptions(spec *ProgramSpec, opts ProgramOptions, handles *hand
 | 
			
		||||
			return nil, fmt.Errorf("attach %s/%s: %w", spec.Type, spec.AttachType, err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		attr.AttachBtfId = uint32(targetID)
 | 
			
		||||
		attr.AttachProgFd = uint32(spec.AttachTarget.FD())
 | 
			
		||||
		attr.AttachBtfId = targetID
 | 
			
		||||
		attr.AttachBtfObjFd = uint32(spec.AttachTarget.FD())
 | 
			
		||||
		defer runtime.KeepAlive(spec.AttachTarget)
 | 
			
		||||
	} else if spec.AttachTo != "" {
 | 
			
		||||
		targetID, err := findTargetInKernel(kernelTypes, spec.AttachTo, spec.Type, spec.AttachType)
 | 
			
		||||
		module, targetID, err := findProgramTargetInKernel(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)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		attr.AttachBtfId = uint32(targetID)
 | 
			
		||||
		attr.AttachBtfId = targetID
 | 
			
		||||
		if module != nil {
 | 
			
		||||
			attr.AttachBtfObjFd = uint32(module.FD())
 | 
			
		||||
			defer module.Close()
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	logSize := DefaultVerifierLogSize
 | 
			
		||||
	if opts.LogSize > 0 {
 | 
			
		||||
		logSize = opts.LogSize
 | 
			
		||||
	if opts.LogSize == 0 {
 | 
			
		||||
		opts.LogSize = DefaultVerifierLogSize
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// The caller requested a specific verifier log level. Set up the log buffer.
 | 
			
		||||
	var logBuf []byte
 | 
			
		||||
	if opts.LogLevel > 0 {
 | 
			
		||||
		logBuf = make([]byte, logSize)
 | 
			
		||||
	if !opts.LogDisabled && opts.LogLevel != 0 {
 | 
			
		||||
		logBuf = make([]byte, opts.LogSize)
 | 
			
		||||
		attr.LogLevel = opts.LogLevel
 | 
			
		||||
		attr.LogSize = uint32(len(logBuf))
 | 
			
		||||
		attr.LogBuf = sys.NewSlicePointer(logBuf)
 | 
			
		||||
@@ -293,13 +336,19 @@ func newProgramWithOptions(spec *ProgramSpec, opts ProgramOptions, handles *hand
 | 
			
		||||
		return &Program{unix.ByteSliceToString(logBuf), fd, spec.Name, "", spec.Type}, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if opts.LogLevel == 0 && opts.LogSize >= 0 {
 | 
			
		||||
		// Re-run with the verifier enabled to get better error messages.
 | 
			
		||||
		logBuf = make([]byte, logSize)
 | 
			
		||||
		attr.LogLevel = 1
 | 
			
		||||
	// An error occurred loading the program, but the caller did not explicitly
 | 
			
		||||
	// enable the verifier log. Re-run with branch-level verifier logs enabled to
 | 
			
		||||
	// obtain more info. Preserve the original error to return it to the caller.
 | 
			
		||||
	// An undersized log buffer will result in ENOSPC regardless of the underlying
 | 
			
		||||
	// cause.
 | 
			
		||||
	var err2 error
 | 
			
		||||
	if !opts.LogDisabled && opts.LogLevel == 0 {
 | 
			
		||||
		logBuf = make([]byte, opts.LogSize)
 | 
			
		||||
		attr.LogLevel = LogLevelBranch
 | 
			
		||||
		attr.LogSize = uint32(len(logBuf))
 | 
			
		||||
		attr.LogBuf = sys.NewSlicePointer(logBuf)
 | 
			
		||||
		_, _ = sys.ProgLoad(attr)
 | 
			
		||||
 | 
			
		||||
		_, err2 = sys.ProgLoad(attr)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	switch {
 | 
			
		||||
@@ -318,13 +367,14 @@ func newProgramWithOptions(spec *ProgramSpec, opts ProgramOptions, handles *hand
 | 
			
		||||
				return nil, fmt.Errorf("load program: %w", err)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if opts.LogSize > maxVerifierLogSize {
 | 
			
		||||
			return nil, fmt.Errorf("load program: %w (ProgramOptions.LogSize exceeds maximum value of %d)", err, maxVerifierLogSize)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = internal.ErrorWithLog(err, logBuf)
 | 
			
		||||
	if btfDisabled {
 | 
			
		||||
		return nil, fmt.Errorf("load program: %w (BTF disabled)", err)
 | 
			
		||||
	}
 | 
			
		||||
	return nil, fmt.Errorf("load program: %w", err)
 | 
			
		||||
	truncated := errors.Is(err, unix.ENOSPC) || errors.Is(err2, unix.ENOSPC)
 | 
			
		||||
	return nil, internal.ErrorWithLog("load program", err, logBuf, truncated)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewProgramFromFD creates a program from a raw fd.
 | 
			
		||||
@@ -362,7 +412,7 @@ func newProgramFromFD(fd *sys.FD) (*Program, error) {
 | 
			
		||||
		return nil, fmt.Errorf("discover program type: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &Program{"", fd, "", "", info.Type}, nil
 | 
			
		||||
	return &Program{"", fd, info.Name, "", info.Type}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (p *Program) String() string {
 | 
			
		||||
@@ -433,7 +483,8 @@ func (p *Program) Clone() (*Program, error) {
 | 
			
		||||
// 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
 | 
			
		||||
// This requires bpffs to be mounted above fileName.
 | 
			
		||||
// See https://docs.cilium.io/en/stable/network/kubernetes/configuration/#mounting-bpffs-with-systemd
 | 
			
		||||
func (p *Program) Pin(fileName string) error {
 | 
			
		||||
	if err := internal.Pin(p.pinnedPath, fileName, p.fd); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
@@ -474,6 +525,9 @@ func (p *Program) Close() error {
 | 
			
		||||
// Various options for Run'ing a Program
 | 
			
		||||
type RunOptions struct {
 | 
			
		||||
	// Program's data input. Required field.
 | 
			
		||||
	//
 | 
			
		||||
	// The kernel expects at least 14 bytes input for an ethernet header for
 | 
			
		||||
	// XDP and SKB programs.
 | 
			
		||||
	Data []byte
 | 
			
		||||
	// Program's data after Program has run. Caller must allocate. Optional field.
 | 
			
		||||
	DataOut []byte
 | 
			
		||||
@@ -481,7 +535,10 @@ type RunOptions struct {
 | 
			
		||||
	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.
 | 
			
		||||
	// Minimum number of times to run Program. Optional field. Defaults to 1.
 | 
			
		||||
	//
 | 
			
		||||
	// The program may be executed more often than this due to interruptions, e.g.
 | 
			
		||||
	// when runtime.AllThreadsSyscall is invoked.
 | 
			
		||||
	Repeat uint32
 | 
			
		||||
	// Optional flags.
 | 
			
		||||
	Flags uint32
 | 
			
		||||
@@ -490,6 +547,8 @@ type RunOptions struct {
 | 
			
		||||
	CPU uint32
 | 
			
		||||
	// Called whenever the syscall is interrupted, and should be set to testing.B.ResetTimer
 | 
			
		||||
	// or similar. Typically used during benchmarking. Optional field.
 | 
			
		||||
	//
 | 
			
		||||
	// Deprecated: use [testing.B.ReportMetric] with unit "ns/op" instead.
 | 
			
		||||
	Reset func()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -517,9 +576,9 @@ func (p *Program) Test(in []byte) (uint32, []byte, error) {
 | 
			
		||||
		Repeat:  1,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ret, _, err := p.testRun(&opts)
 | 
			
		||||
	ret, _, err := p.run(&opts)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return ret, nil, fmt.Errorf("can't test program: %w", err)
 | 
			
		||||
		return ret, nil, fmt.Errorf("test program: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
	return ret, opts.DataOut, nil
 | 
			
		||||
}
 | 
			
		||||
@@ -528,9 +587,9 @@ func (p *Program) Test(in []byte) (uint32, []byte, error) {
 | 
			
		||||
//
 | 
			
		||||
// Note: the same restrictions from Test apply.
 | 
			
		||||
func (p *Program) Run(opts *RunOptions) (uint32, error) {
 | 
			
		||||
	ret, _, err := p.testRun(opts)
 | 
			
		||||
	ret, _, err := p.run(opts)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return ret, fmt.Errorf("can't test program: %w", err)
 | 
			
		||||
		return ret, fmt.Errorf("run program: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
	return ret, nil
 | 
			
		||||
}
 | 
			
		||||
@@ -542,9 +601,6 @@ func (p *Program) Run(opts *RunOptions) (uint32, error) {
 | 
			
		||||
// 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
 | 
			
		||||
//
 | 
			
		||||
// This function requires at least Linux 4.12.
 | 
			
		||||
func (p *Program) Benchmark(in []byte, repeat int, reset func()) (uint32, time.Duration, error) {
 | 
			
		||||
	if uint(repeat) > math.MaxUint32 {
 | 
			
		||||
@@ -557,14 +613,14 @@ func (p *Program) Benchmark(in []byte, repeat int, reset func()) (uint32, time.D
 | 
			
		||||
		Reset:  reset,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	ret, total, err := p.testRun(&opts)
 | 
			
		||||
	ret, total, err := p.run(&opts)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return ret, total, fmt.Errorf("can't benchmark program: %w", err)
 | 
			
		||||
		return ret, total, fmt.Errorf("benchmark program: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
	return ret, total, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var haveProgTestRun = internal.FeatureTest("BPF_PROG_TEST_RUN", "4.12", func() error {
 | 
			
		||||
var haveProgRun = internal.NewFeatureTest("BPF_PROG_RUN", "4.12", func() error {
 | 
			
		||||
	prog, err := NewProgram(&ProgramSpec{
 | 
			
		||||
		// SocketFilter does not require privileges on newer kernels.
 | 
			
		||||
		Type: SocketFilter,
 | 
			
		||||
@@ -580,8 +636,7 @@ var haveProgTestRun = internal.FeatureTest("BPF_PROG_TEST_RUN", "4.12", func() e
 | 
			
		||||
	}
 | 
			
		||||
	defer prog.Close()
 | 
			
		||||
 | 
			
		||||
	// Programs require at least 14 bytes input
 | 
			
		||||
	in := make([]byte, 14)
 | 
			
		||||
	in := internal.EmptyBPFContext
 | 
			
		||||
	attr := sys.ProgRunAttr{
 | 
			
		||||
		ProgFd:     uint32(prog.FD()),
 | 
			
		||||
		DataSizeIn: uint32(len(in)),
 | 
			
		||||
@@ -599,7 +654,7 @@ var haveProgTestRun = internal.FeatureTest("BPF_PROG_TEST_RUN", "4.12", func() e
 | 
			
		||||
		// We know that PROG_TEST_RUN is supported if we get EINTR.
 | 
			
		||||
		return nil
 | 
			
		||||
 | 
			
		||||
	case errors.Is(err, unix.ENOTSUPP):
 | 
			
		||||
	case errors.Is(err, sys.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.
 | 
			
		||||
@@ -609,12 +664,12 @@ var haveProgTestRun = internal.FeatureTest("BPF_PROG_TEST_RUN", "4.12", func() e
 | 
			
		||||
	return err
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
func (p *Program) testRun(opts *RunOptions) (uint32, time.Duration, error) {
 | 
			
		||||
func (p *Program) run(opts *RunOptions) (uint32, time.Duration, error) {
 | 
			
		||||
	if uint(len(opts.Data)) > math.MaxUint32 {
 | 
			
		||||
		return 0, 0, fmt.Errorf("input is too long")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := haveProgTestRun(); err != nil {
 | 
			
		||||
	if err := haveProgRun(); err != nil {
 | 
			
		||||
		return 0, 0, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -647,24 +702,45 @@ func (p *Program) testRun(opts *RunOptions) (uint32, time.Duration, error) {
 | 
			
		||||
		Cpu:         opts.CPU,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if attr.Repeat == 0 {
 | 
			
		||||
		attr.Repeat = 1
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
retry:
 | 
			
		||||
	for {
 | 
			
		||||
		err := sys.ProgRun(&attr)
 | 
			
		||||
		if err == nil {
 | 
			
		||||
			break
 | 
			
		||||
			break retry
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if errors.Is(err, unix.EINTR) {
 | 
			
		||||
			if attr.Repeat == 1 {
 | 
			
		||||
				// Older kernels check whether enough repetitions have been
 | 
			
		||||
				// executed only after checking for pending signals.
 | 
			
		||||
				//
 | 
			
		||||
				//     run signal? done? run ...
 | 
			
		||||
				//
 | 
			
		||||
				// As a result we can get EINTR for repeat==1 even though
 | 
			
		||||
				// the program was run exactly once. Treat this as a
 | 
			
		||||
				// successful run instead.
 | 
			
		||||
				//
 | 
			
		||||
				// Since commit 607b9cc92bd7 ("bpf: Consolidate shared test timing code")
 | 
			
		||||
				// the conditions are reversed:
 | 
			
		||||
				//     run done? signal? ...
 | 
			
		||||
				break retry
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			if opts.Reset != nil {
 | 
			
		||||
				opts.Reset()
 | 
			
		||||
			}
 | 
			
		||||
			continue
 | 
			
		||||
			continue retry
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if errors.Is(err, unix.ENOTSUPP) {
 | 
			
		||||
			return 0, 0, fmt.Errorf("kernel doesn't support testing program type %s: %w", p.Type(), ErrNotSupported)
 | 
			
		||||
		if errors.Is(err, sys.ENOTSUPP) {
 | 
			
		||||
			return 0, 0, fmt.Errorf("kernel doesn't support running %s: %w", p.Type(), ErrNotSupported)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return 0, 0, fmt.Errorf("can't run test: %w", err)
 | 
			
		||||
		return 0, 0, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if opts.DataOut != nil {
 | 
			
		||||
@@ -726,7 +802,14 @@ func LoadPinnedProgram(fileName string, opts *LoadPinOptions) (*Program, error)
 | 
			
		||||
		return nil, fmt.Errorf("info for %s: %w", fileName, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &Program{"", fd, filepath.Base(fileName), fileName, info.Type}, nil
 | 
			
		||||
	var progName string
 | 
			
		||||
	if haveObjName() == nil {
 | 
			
		||||
		progName = info.Name
 | 
			
		||||
	} else {
 | 
			
		||||
		progName = filepath.Base(fileName)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &Program{"", fd, progName, fileName, info.Type}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// SanitizeName replaces all invalid characters in name with replacement.
 | 
			
		||||
@@ -770,11 +853,15 @@ 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.
 | 
			
		||||
// name, 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) {
 | 
			
		||||
// The attach target may be in a loaded kernel module.
 | 
			
		||||
// In that case the returned handle will be non-nil.
 | 
			
		||||
// The caller is responsible for closing the handle.
 | 
			
		||||
//
 | 
			
		||||
// Returns errUnrecognizedAttachType if the combination of progType and attachType
 | 
			
		||||
// is not recognised.
 | 
			
		||||
func findProgramTargetInKernel(name string, progType ProgramType, attachType AttachType) (*btf.Handle, btf.TypeID, error) {
 | 
			
		||||
	type match struct {
 | 
			
		||||
		p ProgramType
 | 
			
		||||
		a AttachType
 | 
			
		||||
@@ -782,59 +869,123 @@ func findTargetInKernel(spec *btf.Spec, name string, progType ProgramType, attac
 | 
			
		||||
 | 
			
		||||
	var (
 | 
			
		||||
		typeName, featureName string
 | 
			
		||||
		isBTFTypeFunc         = true
 | 
			
		||||
		target                btf.Type
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	switch (match{progType, attachType}) {
 | 
			
		||||
	case match{LSM, AttachLSMMac}:
 | 
			
		||||
		typeName = "bpf_lsm_" + name
 | 
			
		||||
		featureName = name + " LSM hook"
 | 
			
		||||
		target = (*btf.Func)(nil)
 | 
			
		||||
	case match{Tracing, AttachTraceIter}:
 | 
			
		||||
		typeName = "bpf_iter_" + name
 | 
			
		||||
		featureName = name + " iterator"
 | 
			
		||||
		target = (*btf.Func)(nil)
 | 
			
		||||
	case match{Tracing, AttachTraceFEntry}:
 | 
			
		||||
		typeName = name
 | 
			
		||||
		featureName = fmt.Sprintf("fentry %s", name)
 | 
			
		||||
		target = (*btf.Func)(nil)
 | 
			
		||||
	case match{Tracing, AttachTraceFExit}:
 | 
			
		||||
		typeName = name
 | 
			
		||||
		featureName = fmt.Sprintf("fexit %s", name)
 | 
			
		||||
		target = (*btf.Func)(nil)
 | 
			
		||||
	case match{Tracing, AttachModifyReturn}:
 | 
			
		||||
		typeName = name
 | 
			
		||||
		featureName = fmt.Sprintf("fmod_ret %s", name)
 | 
			
		||||
		target = (*btf.Func)(nil)
 | 
			
		||||
	case match{Tracing, AttachTraceRawTp}:
 | 
			
		||||
		typeName = fmt.Sprintf("btf_trace_%s", name)
 | 
			
		||||
		featureName = fmt.Sprintf("raw_tp %s", name)
 | 
			
		||||
		isBTFTypeFunc = false
 | 
			
		||||
		target = (*btf.Typedef)(nil)
 | 
			
		||||
	default:
 | 
			
		||||
		return 0, errUnrecognizedAttachType
 | 
			
		||||
		return nil, 0, errUnrecognizedAttachType
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	spec, err := maybeLoadKernelBTF(spec)
 | 
			
		||||
	spec, err := btf.LoadKernelSpec()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return 0, fmt.Errorf("load kernel spec: %w", err)
 | 
			
		||||
		return nil, 0, fmt.Errorf("load kernel spec: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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
 | 
			
		||||
	spec, module, err := findTargetInKernel(spec, typeName, &target)
 | 
			
		||||
	if errors.Is(err, btf.ErrNotFound) {
 | 
			
		||||
		return nil, 0, &internal.UnsupportedFeatureError{Name: featureName}
 | 
			
		||||
	}
 | 
			
		||||
	// See cilium/ebpf#894. Until we can disambiguate between equally-named kernel
 | 
			
		||||
	// symbols, we should explicitly refuse program loads. They will not reliably
 | 
			
		||||
	// do what the caller intended.
 | 
			
		||||
	if errors.Is(err, btf.ErrMultipleMatches) {
 | 
			
		||||
		return nil, 0, fmt.Errorf("attaching to ambiguous kernel symbol is not supported: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		if errors.Is(err, btf.ErrNotFound) {
 | 
			
		||||
			return 0, &internal.UnsupportedFeatureError{
 | 
			
		||||
				Name: featureName,
 | 
			
		||||
			}
 | 
			
		||||
		return nil, 0, fmt.Errorf("find target for %s: %w", featureName, err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	id, err := spec.TypeID(target)
 | 
			
		||||
	return module, id, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// findTargetInKernel attempts to find a named type in the current kernel.
 | 
			
		||||
//
 | 
			
		||||
// target will point at the found type after a successful call. Searches both
 | 
			
		||||
// vmlinux and any loaded modules.
 | 
			
		||||
//
 | 
			
		||||
// Returns a non-nil handle if the type was found in a module, or btf.ErrNotFound
 | 
			
		||||
// if the type wasn't found at all.
 | 
			
		||||
func findTargetInKernel(kernelSpec *btf.Spec, typeName string, target *btf.Type) (*btf.Spec, *btf.Handle, error) {
 | 
			
		||||
	err := kernelSpec.TypeByName(typeName, target)
 | 
			
		||||
	if errors.Is(err, btf.ErrNotFound) {
 | 
			
		||||
		spec, module, err := findTargetInModule(kernelSpec, typeName, target)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, nil, fmt.Errorf("find target in modules: %w", err)
 | 
			
		||||
		}
 | 
			
		||||
		return 0, fmt.Errorf("find target for %s: %w", featureName, err)
 | 
			
		||||
		return spec, module, nil
 | 
			
		||||
	}
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, nil, fmt.Errorf("find target in vmlinux: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
	return kernelSpec, nil, err
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// findTargetInModule attempts to find a named type in any loaded module.
 | 
			
		||||
//
 | 
			
		||||
// base must contain the kernel's types and is used to parse kmod BTF. Modules
 | 
			
		||||
// are searched in the order they were loaded.
 | 
			
		||||
//
 | 
			
		||||
// Returns btf.ErrNotFound if the target can't be found in any module.
 | 
			
		||||
func findTargetInModule(base *btf.Spec, typeName string, target *btf.Type) (*btf.Spec, *btf.Handle, error) {
 | 
			
		||||
	it := new(btf.HandleIterator)
 | 
			
		||||
	defer it.Handle.Close()
 | 
			
		||||
 | 
			
		||||
	for it.Next() {
 | 
			
		||||
		info, err := it.Handle.Info()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, nil, fmt.Errorf("get info for BTF ID %d: %w", it.ID, err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if !info.IsModule() {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		spec, err := it.Handle.Spec(base)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, nil, fmt.Errorf("parse types for module %s: %w", info.Name, err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		err = spec.TypeByName(typeName, target)
 | 
			
		||||
		if errors.Is(err, btf.ErrNotFound) {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, nil, fmt.Errorf("lookup type in module %s: %w", info.Name, err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return spec, it.Take(), nil
 | 
			
		||||
	}
 | 
			
		||||
	if err := it.Err(); err != nil {
 | 
			
		||||
		return nil, nil, fmt.Errorf("iterate modules: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return spec.TypeID(target)
 | 
			
		||||
	return nil, nil, btf.ErrNotFound
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// find an attach target type in a program.
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user