2023-01-17 20:59:04 +00:00
|
|
|
package link
|
|
|
|
|
|
|
|
import (
|
2023-04-03 09:16:17 +00:00
|
|
|
"errors"
|
2023-01-17 20:59:04 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/cilium/ebpf"
|
2023-04-03 09:16:17 +00:00
|
|
|
"github.com/cilium/ebpf/internal/sys"
|
2023-01-17 20:59:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type RawTracepointOptions struct {
|
|
|
|
// Tracepoint name.
|
|
|
|
Name string
|
|
|
|
// Program must be of type RawTracepoint*
|
|
|
|
Program *ebpf.Program
|
|
|
|
}
|
|
|
|
|
|
|
|
// AttachRawTracepoint links a BPF program to a raw_tracepoint.
|
|
|
|
//
|
|
|
|
// Requires at least Linux 4.17.
|
|
|
|
func AttachRawTracepoint(opts RawTracepointOptions) (Link, error) {
|
|
|
|
if t := opts.Program.Type(); t != ebpf.RawTracepoint && t != ebpf.RawTracepointWritable {
|
|
|
|
return nil, fmt.Errorf("invalid program type %s, expected RawTracepoint(Writable)", t)
|
|
|
|
}
|
|
|
|
if opts.Program.FD() < 0 {
|
2023-04-03 09:16:17 +00:00
|
|
|
return nil, fmt.Errorf("invalid program: %w", sys.ErrClosedFd)
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
fd, err := sys.RawTracepointOpen(&sys.RawTracepointOpenAttr{
|
|
|
|
Name: sys.NewStringPointer(opts.Name),
|
|
|
|
ProgFd: uint32(opts.Program.FD()),
|
2023-01-17 20:59:04 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
err = haveBPFLink()
|
|
|
|
if errors.Is(err, ErrNotSupported) {
|
|
|
|
// Prior to commit 70ed506c3bbc ("bpf: Introduce pinnable bpf_link abstraction")
|
|
|
|
// raw_tracepoints are just a plain fd.
|
|
|
|
return &simpleRawTracepoint{fd}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rawTracepoint{RawLink{fd: fd}}, nil
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
type simpleRawTracepoint struct {
|
|
|
|
fd *sys.FD
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
var _ Link = (*simpleRawTracepoint)(nil)
|
2023-01-17 20:59:04 +00:00
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
func (frt *simpleRawTracepoint) isLink() {}
|
|
|
|
|
|
|
|
func (frt *simpleRawTracepoint) Close() error {
|
|
|
|
return frt.fd.Close()
|
|
|
|
}
|
2023-01-17 20:59:04 +00:00
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
func (frt *simpleRawTracepoint) Update(_ *ebpf.Program) error {
|
|
|
|
return fmt.Errorf("update raw_tracepoint: %w", ErrNotSupported)
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
func (frt *simpleRawTracepoint) Pin(string) error {
|
|
|
|
return fmt.Errorf("pin raw_tracepoint: %w", ErrNotSupported)
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:16:17 +00:00
|
|
|
func (frt *simpleRawTracepoint) Unpin() error {
|
|
|
|
return fmt.Errorf("unpin raw_tracepoint: %w", ErrNotSupported)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (frt *simpleRawTracepoint) Info() (*Info, error) {
|
|
|
|
return nil, fmt.Errorf("can't get raw_tracepoint info: %w", ErrNotSupported)
|
|
|
|
}
|
|
|
|
|
|
|
|
type rawTracepoint struct {
|
|
|
|
RawLink
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Link = (*rawTracepoint)(nil)
|
|
|
|
|
|
|
|
func (rt *rawTracepoint) Update(_ *ebpf.Program) error {
|
|
|
|
return fmt.Errorf("update raw_tracepoint: %w", ErrNotSupported)
|
2023-01-17 20:59:04 +00:00
|
|
|
}
|