Skip to content

Commit

Permalink
features: Make Main Package Feature Tests Public
Browse files Browse the repository at this point in the history
Move all feature teats in the main package to a new
internal directory, "internal/features", which can be used
by the main package and also mirrored in the "features"
package for public use.

Signed-off-by: Nate Sweet <[email protected]>
  • Loading branch information
nathanjsweet committed Jan 10, 2025
1 parent b805828 commit aeafd17
Show file tree
Hide file tree
Showing 15 changed files with 702 additions and 526 deletions.
5 changes: 3 additions & 2 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/features"
"github.com/cilium/ebpf/internal/kallsyms"
"github.com/cilium/ebpf/internal/kconfig"
"github.com/cilium/ebpf/internal/linux"
Expand Down Expand Up @@ -508,7 +509,7 @@ func (cl *collectionLoader) loadMap(mapName string) (*Map, error) {
// Defer setting the mmapable flag on maps until load time. This avoids the
// MapSpec having different flags on some kernel versions. Also avoid running
// syscalls during ELF loading, so platforms like wasm can also parse an ELF.
if isDataSection(mapSpec.Name) && haveMmapableMaps() == nil {
if isDataSection(mapSpec.Name) && features.HaveMmapableMaps() == nil {
mapSpec.Flags |= sys.BPF_F_MMAPABLE
}

Expand Down Expand Up @@ -730,7 +731,7 @@ func resolveKconfig(m *MapSpec) error {
return fmt.Errorf("variable %s must be an integer, got %s", n, v.Type)
}
var value uint64 = 1
if err := haveSyscallWrapper(); errors.Is(err, ErrNotSupported) {
if err := features.HaveSyscallWrapper(); errors.Is(err, ErrNotSupported) {
value = 0
} else if err != nil {
return fmt.Errorf("unable to derive a value for LINUX_HAS_SYSCALL_WRAPPER: %w", err)
Expand Down
80 changes: 80 additions & 0 deletions features/syscalls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package features

import internalFeatures "github.com/cilium/ebpf/internal/features"

// HaveNestedMaps returns a nil error if nested maps are supported.
func HaveNestedMaps() error {
return internalFeatures.HaveNestedMaps()
}

// HaveMapMutabilityModifiers returns a nil error if map
// mutability modifiers are supported.
func HaveMapMutabilityModifiers() error {
return internalFeatures.HaveMapMutabilityModifiers()
}

// HaveMmapableMaps returns a nil error if mmapable maps
// are supported.
func HaveMmapableMaps() error {
return internalFeatures.HaveMmapableMaps()
}

// HaveInnerMaps returns a nil error if inner maps are supported.
func HaveInnerMaps() error {
return internalFeatures.HaveInnerMaps()
}

// HaveNoPreallocMaps returns a nil error if the flag for
// creating maps that are not pre-allocated is supported.
func HaveNoPreallocMaps() error {
return internalFeatures.HaveNoPreallocMaps()
}

// HaveObjName returns a nil error if object names are supported
func HaveObjName() error {
return internalFeatures.HaveObjName()
}

// ObjNameAllowsDot returns a nil error if object names support
// the dot character, i.e. ".".
func ObjNameAllowsDot() error {
return internalFeatures.ObjNameAllowsDot()
}

// HaveBatchAPI returns a nil error if batch operations are supported
func HaveBatchAPI() error {
return internalFeatures.HaveBatchAPI()
}

// HaveProbeReadKernel returns a nil error if kprobes are supported.
func HaveProbeReadKernel() error {
return internalFeatures.HaveProbeReadKernel()
}

// HaveBPFToBPFCalls returns a nil error if bpf programs can call other bpf
// programs.
func HaveBPFToBPFCalls() error {
return internalFeatures.HaveBPFToBPFCalls()
}

// HaveSyscallWrapper returns a nil error if syscall wrapper is not supported.
func HaveSyscallWrapper() error {
return internalFeatures.HaveSyscallWrapper()
}

// HaveProgramExtInfos returns a nil error if program BTF is supported
func HaveProgramExtInfos() error {
return internalFeatures.HaveProgramExtInfos()
}

// HaveProgramInfoMapIDs returns a nil error if retrieving map ids from
// program's object info is supported.
func HaveProgramInfoMapIDs() error {
return internalFeatures.HaveProgramInfoMapIDs()
}

// HaveProgTestRun returns a nil error if the bpf command
// PROG_TEST_RUN is supported.
func HaveProgTestRun() error {
return internalFeatures.HaveProgTestRun()
}
30 changes: 2 additions & 28 deletions info.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/features"
"github.com/cilium/ebpf/internal/sys"
"github.com/cilium/ebpf/internal/unix"
)
Expand Down Expand Up @@ -277,7 +278,7 @@ func newProgramInfoFromFd(fd *sys.FD) (*ProgramInfo, error) {
info2.NrMapIds = info.NrMapIds
info2.MapIds = sys.NewSlicePointer(pi.maps)
makeSecondCall = true
} else if haveProgramInfoMapIDs() == nil {
} else if features.HaveProgramInfoMapIDs() == nil {
// This program really has no associated maps.
pi.maps = make([]MapID, 0)
} else {
Expand Down Expand Up @@ -766,30 +767,3 @@ func EnableStats(which uint32) (io.Closer, error) {
}
return fd, nil
}

var haveProgramInfoMapIDs = internal.NewFeatureTest("map IDs in program info", func() error {
prog, err := progLoad(asm.Instructions{
asm.LoadImm(asm.R0, 0, asm.DWord),
asm.Return(),
}, SocketFilter, "MIT")
if err != nil {
return err
}
defer prog.Close()

err = sys.ObjInfo(prog, &sys.ProgInfo{
// NB: Don't need to allocate MapIds since the program isn't using
// any maps.
NrMapIds: 1,
})
if errors.Is(err, unix.EINVAL) {
// Most likely the syscall doesn't exist.
return internal.ErrNotSupported
}
if errors.Is(err, unix.E2BIG) {
// We've hit check_uarg_tail_zero on older kernels.
return internal.ErrNotSupported
}

return err
}, "4.15")
3 changes: 2 additions & 1 deletion info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/features"
"github.com/cilium/ebpf/internal/sys"
"github.com/cilium/ebpf/internal/testutils"
)
Expand Down Expand Up @@ -461,7 +462,7 @@ func testStats(prog *Program) error {
}

func TestHaveProgramInfoMapIDs(t *testing.T) {
testutils.CheckFeatureTest(t, haveProgramInfoMapIDs)
testutils.CheckFeatureTest(t, features.HaveProgramInfoMapIDs)
}

func TestProgInfoExtBTF(t *testing.T) {
Expand Down
19 changes: 19 additions & 0 deletions internal/elf.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,22 @@ func (se *SafeELFFile) SectionsByType(typ elf.SectionType) []*elf.Section {
}
return sections
}

// InvalidBPFObjNameChar returns true if char may not appear in
// a BPF object name.
func InvalidBPFObjNameChar(char rune, dotAllowed bool) bool {
switch {
case char >= 'A' && char <= 'Z':
return false
case char >= 'a' && char <= 'z':
return false
case char >= '0' && char <= '9':
return false
case dotAllowed && char == '.':
return false
case char == '_':
return false
default:
return true
}
}
Loading

0 comments on commit aeafd17

Please sign in to comment.