Skip to content

Commit

Permalink
use map for module BTF
Browse files Browse the repository at this point in the history
  • Loading branch information
brycekahle committed Jan 30, 2024
1 parent 1205e87 commit e914652
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
4 changes: 2 additions & 2 deletions btf/core_reloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestCORERelocationLoad(t *testing.T) {
}

prog, err := ebpf.NewProgramWithOptions(progSpec, ebpf.ProgramOptions{
KernelTypes: []*btf.Spec{spec.Types},
KernelTypes: spec.Types,
})
testutils.SkipIfNotSupported(t, err)

Expand Down Expand Up @@ -89,7 +89,7 @@ func TestCORERelocationRead(t *testing.T) {
for _, progSpec := range spec.Programs {
t.Run(progSpec.Name, func(t *testing.T) {
prog, err := ebpf.NewProgramWithOptions(progSpec, ebpf.ProgramOptions{
KernelTypes: []*btf.Spec{targetSpec},
KernelTypes: targetSpec,
})
testutils.SkipIfNotSupported(t, err)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion elf_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ func TestLibBPFCompat(t *testing.T) {
}

opts := opts // copy
opts.Programs.KernelTypes = []*btf.Spec{btfSpec}
opts.Programs.KernelTypes = btfSpec
load(t, spec, opts, valid)
})
}
Expand Down
22 changes: 20 additions & 2 deletions prog.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,15 @@ type ProgramOptions struct {
// This is useful in environments where the kernel BTF is not available
// (containers) or where it is in a non-standard location. Defaults to
// use the kernel BTF from a well-known location if nil.
KernelTypes []*btf.Spec
KernelTypes *btf.Spec

// Type information used for CO-RE relocations of kernel modules,
// indexed by module name.
//
// This is useful in environments where the kernel BTF is not available
// (containers) or where it is in a non-standard location. Defaults to
// use the kernel module BTF from a well-known location if nil.
KernelModuleTypes map[string]*btf.Spec
}

// ProgramSpec defines a Program.
Expand Down Expand Up @@ -248,8 +256,18 @@ func newProgramWithOptions(spec *ProgramSpec, opts ProgramOptions) (*Program, er
return nil, fmt.Errorf("kernel module search: %w", err)
}

var targets []*btf.Spec
if opts.KernelTypes != nil {
targets = append(targets, opts.KernelTypes)
}
if kmodName != "" && opts.KernelModuleTypes != nil {
if modBTF, ok := opts.KernelModuleTypes[kmodName]; ok {
targets = append(targets, modBTF)
}
}

var b btf.Builder
if err := applyRelocations(insns, opts.KernelTypes, kmodName, spec.ByteOrder, &b); err != nil {
if err := applyRelocations(insns, targets, kmodName, spec.ByteOrder, &b); err != nil {
return nil, fmt.Errorf("apply CO-RE relocations: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion prog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ func TestProgramKernelTypes(t *testing.T) {
},
License: "MIT",
}, ProgramOptions{
KernelTypes: []*btf.Spec{btfSpec},
KernelTypes: btfSpec,
})
testutils.SkipIfNotSupported(t, err)
if err != nil {
Expand Down

0 comments on commit e914652

Please sign in to comment.