Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing program type feature probes #890

Merged
merged 2 commits into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion btf/ext_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (ei *ExtInfos) Assign(insns asm.Instructions, section string) {
iter := insns.Iterate()
for iter.Next() {
if len(funcInfos) > 0 && funcInfos[0].offset == iter.Offset {
iter.Ins.Metadata.Set(funcInfoMeta{}, funcInfos[0].fn)
*iter.Ins = WithFuncMetadata(*iter.Ins, funcInfos[0].fn)
funcInfos = funcInfos[1:]
}

Expand Down
6 changes: 6 additions & 0 deletions btf/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,12 @@ func FuncMetadata(ins *asm.Instruction) *Func {
return fn
}

// WithFuncMetadata adds a btf.Func to the Metadata of asm.Instruction.
func WithFuncMetadata(ins asm.Instruction, fn *Func) asm.Instruction {
ins.Metadata.Set(funcInfoMeta{}, fn)
return ins
}

func (f *Func) Format(fs fmt.State, verb rune) {
formatType(fs, verb, f, f.Linkage, "proto=", f.Type)
}
Expand Down
57 changes: 53 additions & 4 deletions features/prog.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/sys"
"github.com/cilium/ebpf/internal/unix"
Expand Down Expand Up @@ -92,7 +93,16 @@ var haveProgramTypeMatrix = internal.FeatureMatrix[ebpf.ProgramType]{
})
},
},
// ebpf.Tracing: {Version: "5.5"},
ebpf.Tracing: {
Version: "5.5",
Fn: func() error {
return probeProgram(&ebpf.ProgramSpec{
Type: ebpf.Tracing,
AttachType: ebpf.AttachTraceFEntry,
AttachTo: "bpf_init",
})
},
},
ebpf.StructOps: {
Version: "5.6",
Fn: func() error {
Expand All @@ -107,16 +117,55 @@ var haveProgramTypeMatrix = internal.FeatureMatrix[ebpf.ProgramType]{
return err
},
},
// ebpf.Extension: {Version: "5.6"},
ebpf.Extension: {
Version: "5.6",
Fn: func() error {
// create btf.Func to add to first ins of target and extension so both progs are btf powered
btfFn := btf.Func{
Name: "a",
Type: &btf.FuncProto{
Return: &btf.Int{},
},
Linkage: btf.GlobalFunc,
}
insns := asm.Instructions{
btf.WithFuncMetadata(asm.Mov.Imm(asm.R0, 0), &btfFn),
asm.Return(),
}

// create target prog
prog, err := ebpf.NewProgramWithOptions(
&ebpf.ProgramSpec{
Type: ebpf.XDP,
Instructions: insns,
},
ebpf.ProgramOptions{
rgo3 marked this conversation as resolved.
Show resolved Hide resolved
LogDisabled: true,
},
)
if err != nil {
return err
}
defer prog.Close()

// probe for Extension prog with target
return probeProgram(&ebpf.ProgramSpec{
ti-mo marked this conversation as resolved.
Show resolved Hide resolved
Type: ebpf.Extension,
Instructions: insns,
AttachTarget: prog,
AttachTo: btfFn.Name,
})
},
},
ebpf.LSM: {
Version: "5.7",
Fn: func() error {
return probeProgram((&ebpf.ProgramSpec{
return probeProgram(&ebpf.ProgramSpec{
Type: ebpf.LSM,
AttachType: ebpf.AttachLSMMac,
AttachTo: "file_mprotect",
License: "GPL",
}))
})
},
},
ebpf.SkLookup: {
Expand Down