| 1 |
|
| 2 |
|
| 3 |
package link |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
|
| 9 |
"github.com/cilium/ebpf" |
| 10 |
"github.com/cilium/ebpf/btf" |
| 11 |
"github.com/cilium/ebpf/internal/sys" |
| 12 |
"github.com/cilium/ebpf/internal/unix" |
| 13 |
) |
| 14 |
|
| 15 |
type tracing struct { |
| 16 |
RawLink |
| 17 |
} |
| 18 |
|
| 19 |
func (f *tracing) Update(_ *ebpf.Program) error { |
| 20 |
return fmt.Errorf("tracing update: %w", ErrNotSupported) |
| 21 |
} |
| 22 |
|
| 23 |
func (f *tracing) Info() (*Info, error) { |
| 24 |
var info sys.TracingLinkInfo |
| 25 |
if err := sys.ObjInfo(f.fd, &info); err != nil { |
| 26 |
return nil, fmt.Errorf("tracing link info: %s", err) |
| 27 |
} |
| 28 |
extra := &TracingInfo{ |
| 29 |
TargetObjectId: info.TargetObjId, |
| 30 |
TargetBtfId: info.TargetBtfId, |
| 31 |
AttachType: info.AttachType, |
| 32 |
} |
| 33 |
|
| 34 |
return &Info{ |
| 35 |
info.Type, |
| 36 |
info.Id, |
| 37 |
ebpf.ProgramID(info.ProgId), |
| 38 |
extra, |
| 39 |
}, nil |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
func AttachFreplace(targetProg *ebpf.Program, name string, prog *ebpf.Program) (Link, error) { |
| 52 |
if (name == "") != (targetProg == nil) { |
| 53 |
return nil, fmt.Errorf("must provide both or neither of name and targetProg: %w", errInvalidInput) |
| 54 |
} |
| 55 |
if prog == nil { |
| 56 |
return nil, fmt.Errorf("prog cannot be nil: %w", errInvalidInput) |
| 57 |
} |
| 58 |
if prog.Type() != ebpf.Extension { |
| 59 |
return nil, fmt.Errorf("eBPF program type %s is not an Extension: %w", prog.Type(), errInvalidInput) |
| 60 |
} |
| 61 |
|
| 62 |
var ( |
| 63 |
target int |
| 64 |
typeID btf.TypeID |
| 65 |
) |
| 66 |
if targetProg != nil { |
| 67 |
btfHandle, err := targetProg.Handle() |
| 68 |
if err != nil { |
| 69 |
return nil, err |
| 70 |
} |
| 71 |
defer btfHandle.Close() |
| 72 |
|
| 73 |
spec, err := btfHandle.Spec(nil) |
| 74 |
if err != nil { |
| 75 |
return nil, err |
| 76 |
} |
| 77 |
|
| 78 |
var function *btf.Func |
| 79 |
if err := spec.TypeByName(name, &function); err != nil { |
| 80 |
return nil, err |
| 81 |
} |
| 82 |
|
| 83 |
target = targetProg.FD() |
| 84 |
typeID, err = spec.TypeID(function) |
| 85 |
if err != nil { |
| 86 |
return nil, err |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
link, err := AttachRawLink(RawLinkOptions{ |
| 91 |
Target: target, |
| 92 |
Program: prog, |
| 93 |
Attach: ebpf.AttachNone, |
| 94 |
BTF: typeID, |
| 95 |
}) |
| 96 |
if errors.Is(err, sys.ENOTSUPP) { |
| 97 |
|
| 98 |
return nil, fmt.Errorf("create raw tracepoint: %w", ErrNotSupported) |
| 99 |
} |
| 100 |
if err != nil { |
| 101 |
return nil, err |
| 102 |
} |
| 103 |
|
| 104 |
return &tracing{*link}, nil |
| 105 |
} |
| 106 |
|
| 107 |
type TracingOptions struct { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
Program *ebpf.Program |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
AttachType ebpf.AttachType |
| 119 |
|
| 120 |
|
| 121 |
Cookie uint64 |
| 122 |
} |
| 123 |
|
| 124 |
type LSMOptions struct { |
| 125 |
|
| 126 |
|
| 127 |
Program *ebpf.Program |
| 128 |
|
| 129 |
|
| 130 |
Cookie uint64 |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
func attachBTFID(program *ebpf.Program, at ebpf.AttachType, cookie uint64) (Link, error) { |
| 135 |
if program.FD() < 0 { |
| 136 |
return nil, fmt.Errorf("invalid program %w", sys.ErrClosedFd) |
| 137 |
} |
| 138 |
|
| 139 |
var ( |
| 140 |
fd *sys.FD |
| 141 |
err error |
| 142 |
) |
| 143 |
switch at { |
| 144 |
case ebpf.AttachTraceFEntry, ebpf.AttachTraceFExit, ebpf.AttachTraceRawTp, |
| 145 |
ebpf.AttachModifyReturn, ebpf.AttachLSMMac: |
| 146 |
|
| 147 |
fd, err = sys.LinkCreateTracing(&sys.LinkCreateTracingAttr{ |
| 148 |
ProgFd: uint32(program.FD()), |
| 149 |
AttachType: sys.AttachType(at), |
| 150 |
Cookie: cookie, |
| 151 |
}) |
| 152 |
if err == nil { |
| 153 |
break |
| 154 |
} |
| 155 |
if !errors.Is(err, unix.EINVAL) && !errors.Is(err, sys.ENOTSUPP) { |
| 156 |
return nil, fmt.Errorf("create tracing link: %w", err) |
| 157 |
} |
| 158 |
fallthrough |
| 159 |
case ebpf.AttachNone: |
| 160 |
|
| 161 |
if cookie > 0 { |
| 162 |
return nil, fmt.Errorf("create raw tracepoint with cookie: %w", ErrNotSupported) |
| 163 |
} |
| 164 |
|
| 165 |
fd, err = sys.RawTracepointOpen(&sys.RawTracepointOpenAttr{ |
| 166 |
ProgFd: uint32(program.FD()), |
| 167 |
}) |
| 168 |
if errors.Is(err, sys.ENOTSUPP) { |
| 169 |
|
| 170 |
return nil, fmt.Errorf("create raw tracepoint: %w", ErrNotSupported) |
| 171 |
} |
| 172 |
if err != nil { |
| 173 |
return nil, fmt.Errorf("create raw tracepoint: %w", err) |
| 174 |
} |
| 175 |
default: |
| 176 |
return nil, fmt.Errorf("invalid attach type: %s", at.String()) |
| 177 |
} |
| 178 |
|
| 179 |
raw := RawLink{fd: fd} |
| 180 |
info, err := raw.Info() |
| 181 |
if err != nil { |
| 182 |
raw.Close() |
| 183 |
return nil, err |
| 184 |
} |
| 185 |
|
| 186 |
if info.Type == RawTracepointType { |
| 187 |
|
| 188 |
|
| 189 |
return &rawTracepoint{raw}, nil |
| 190 |
} |
| 191 |
return &tracing{raw}, nil |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
func AttachTracing(opts TracingOptions) (Link, error) { |
| 198 |
if t := opts.Program.Type(); t != ebpf.Tracing { |
| 199 |
return nil, fmt.Errorf("invalid program type %s, expected Tracing", t) |
| 200 |
} |
| 201 |
|
| 202 |
switch opts.AttachType { |
| 203 |
case ebpf.AttachTraceFEntry, ebpf.AttachTraceFExit, ebpf.AttachModifyReturn, |
| 204 |
ebpf.AttachTraceRawTp, ebpf.AttachNone: |
| 205 |
default: |
| 206 |
return nil, fmt.Errorf("invalid attach type: %s", opts.AttachType.String()) |
| 207 |
} |
| 208 |
|
| 209 |
return attachBTFID(opts.Program, opts.AttachType, opts.Cookie) |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
func AttachLSM(opts LSMOptions) (Link, error) { |
| 215 |
if t := opts.Program.Type(); t != ebpf.LSM { |
| 216 |
return nil, fmt.Errorf("invalid program type %s, expected LSM", t) |
| 217 |
} |
| 218 |
|
| 219 |
return attachBTFID(opts.Program, ebpf.AttachLSMMac, opts.Cookie) |
| 220 |
} |