Skip to content

Commit

Permalink
add callback based constant editor
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcacheux committed Jan 2, 2025
1 parent fb57b17 commit 26f47a2
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions constant_editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type ConstantEditor struct {
// Value - Value to write in the eBPF bytecode. When using the asm load method, the Value has to be a `uint64`.
Value interface{}

ValueCallback func(prog *ebpf.ProgramSpec) interface{}

// FailOnMissing - If FailOMissing is set to true, the constant edition process will return an error if the constant
// was missing in at least one program
FailOnMissing bool
Expand All @@ -29,6 +31,13 @@ type ConstantEditor struct {
ProbeIdentificationPairs []ProbeIdentificationPair
}

func (ce *ConstantEditor) GetValue(prog *ebpf.ProgramSpec) interface{} {
if ce.ValueCallback != nil {
return ce.ValueCallback(prog)
}
return ce.Value
}

// editConstants - newEditor the programs in the CollectionSpec with the provided constant editors. Tries with the BTF global
// variable first, and fall back to the asm method if BTF is not available.
func (m *Manager) editConstants() error {
Expand All @@ -40,7 +49,7 @@ func (m *Manager) editConstants() error {
continue
}
constant := map[string]interface{}{
editor.Name: editor.Value,
editor.Name: editor.GetValue(nil), // in BTF mode, we don't have access to the constant name
}
if err := m.collectionSpec.RewriteConstants(constant); err != nil && editor.FailOnMissing {
return err
Expand Down Expand Up @@ -88,7 +97,7 @@ func (m *Manager) editConstants() error {
edit = newEditor(&prog.Instructions)
}

if err := m.editConstantWithEditor(edit, constantEditor); err != nil {
if err := m.editConstantWithEditor(prog, edit, constantEditor); err != nil {
return fmt.Errorf("couldn't edit %s in %s: %w", constantEditor.Name, section, err)
}
}
Expand All @@ -100,11 +109,11 @@ func (m *Manager) editConstants() error {
// editConstant - newEditor the provided program with the provided constant using the asm method.
func (m *Manager) editConstant(prog *ebpf.ProgramSpec, editor ConstantEditor) error {
edit := newEditor(&prog.Instructions)
return m.editConstantWithEditor(edit, editor)
return m.editConstantWithEditor(prog, edit, editor)
}

func (m *Manager) editConstantWithEditor(edit *editor, editor ConstantEditor) error {
data, ok := (editor.Value).(uint64)
func (m *Manager) editConstantWithEditor(prog *ebpf.ProgramSpec, edit *editor, editor ConstantEditor) error {
data, ok := (editor.GetValue(prog)).(uint64)
if !ok {
return fmt.Errorf("with the asm method, the constant value has to be of type uint64")
}
Expand Down

0 comments on commit 26f47a2

Please sign in to comment.