Skip to content

Commit

Permalink
features: rename HaveProgType API
Browse files Browse the repository at this point in the history
In order to be in line with other naming schemes throughout
the library this commit deprecates HaveProgType() in favor
of HaveProgamType().

Signed-off-by: Robin Gögge <r.goegge@gmail.com>
  • Loading branch information
rgo3 authored and ti-mo committed May 23, 2022
1 parent d1edf5a commit 951bb28
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
27 changes: 16 additions & 11 deletions features/prog.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func init() {
pc.progTypes = make(map[ebpf.ProgramType]error)
pc.types = make(map[ebpf.ProgramType]error)
pc.helpers = make(map[ebpf.ProgramType]map[asm.BuiltinFunc]error)
allocHelperCache()
}
Expand All @@ -31,8 +31,8 @@ var (
)

type progCache struct {
typeMu sync.Mutex
progTypes map[ebpf.ProgramType]error
typeMu sync.Mutex
types map[ebpf.ProgramType]error

helperMu sync.Mutex
helpers map[ebpf.ProgramType]map[asm.BuiltinFunc]error
Expand Down Expand Up @@ -95,17 +95,22 @@ func createProgLoadAttr(pt ebpf.ProgramType, helper asm.BuiltinFunc) (*sys.ProgL

// HaveProgType probes the running kernel for the availability of the specified program type.
//
// Deprecated: use HaveProgramType() instead.
var HaveProgType = HaveProgramType

// HaveProgramType probes the running kernel for the availability of the specified program type.
//
// See the package documentation for the meaning of the error return value.
func HaveProgType(pt ebpf.ProgramType) error {
if err := validateProgType(pt); err != nil {
func HaveProgramType(pt ebpf.ProgramType) error {
if err := validateProgramType(pt); err != nil {
return err
}

return haveProgType(pt)
return haveProgramType(pt)

}

func validateProgType(pt ebpf.ProgramType) error {
func validateProgramType(pt ebpf.ProgramType) error {
if pt > pt.Max() {
return os.ErrInvalid
}
Expand All @@ -120,10 +125,10 @@ func validateProgType(pt ebpf.ProgramType) error {
return nil
}

func haveProgType(pt ebpf.ProgramType) error {
func haveProgramType(pt ebpf.ProgramType) error {
pc.typeMu.Lock()
defer pc.typeMu.Unlock()
if err, ok := pc.progTypes[pt]; ok {
if err, ok := pc.types[pt]; ok {
return err
}

Expand Down Expand Up @@ -154,7 +159,7 @@ func haveProgType(pt ebpf.ProgramType) error {
fd.Close()
}

pc.progTypes[pt] = err
pc.types[pt] = err

return err
}
Expand All @@ -173,7 +178,7 @@ func haveProgType(pt ebpf.ProgramType) error {
//
// Probe results are cached and persist throughout any process capability changes.
func HaveProgramHelper(pt ebpf.ProgramType, helper asm.BuiltinFunc) error {
if err := validateProgType(pt); err != nil {
if err := validateProgramType(pt); err != nil {
return err
}

Expand Down
12 changes: 6 additions & 6 deletions features/prog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var progTypeMinVersion = map[ebpf.ProgramType]string{
ebpf.Syscall: "5.14",
}

func TestHaveProgType(t *testing.T) {
func TestHaveProgramType(t *testing.T) {
for progType := ebpf.UnspecifiedProgram + 1; progType <= progType.Max(); progType++ {
// Need inner loop copy to make use of t.Parallel()
pt := progType
Expand All @@ -69,7 +69,7 @@ func TestHaveProgType(t *testing.T) {
}
testutils.SkipOnOldKernel(t, minVersion, feature)

if err := HaveProgType(pt); err != nil {
if err := HaveProgramType(pt); err != nil {
if pt == ebpf.LircMode2 {
// CI kernels are built with CONFIG_BPF_LIRC_MODE2, but some
// mainstream distro's don't ship with it. Make this prog type
Expand All @@ -84,14 +84,14 @@ func TestHaveProgType(t *testing.T) {
}
}

func TestHaveProgTypeUnsupported(t *testing.T) {
if err := haveProgType(ebpf.ProgramType(math.MaxUint32)); err != ebpf.ErrNotSupported {
func TestHaveProgramTypeUnsupported(t *testing.T) {
if err := haveProgramType(ebpf.ProgramType(math.MaxUint32)); err != ebpf.ErrNotSupported {
t.Fatalf("Expected ebpf.ErrNotSupported but was: %v", err)
}
}

func TestHaveProgTypeInvalid(t *testing.T) {
if err := HaveProgType(ebpf.ProgramType(math.MaxUint32)); err != os.ErrInvalid {
func TestHaveProgramTypeInvalid(t *testing.T) {
if err := HaveProgramType(ebpf.ProgramType(math.MaxUint32)); err != os.ErrInvalid {
t.Fatalf("Expected os.ErrInvalid but was: %v", err)
}
}
Expand Down

0 comments on commit 951bb28

Please sign in to comment.