prog.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. package ebpf
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "errors"
  6. "fmt"
  7. "math"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. "github.com/cilium/ebpf/asm"
  12. "github.com/cilium/ebpf/internal"
  13. "github.com/cilium/ebpf/internal/btf"
  14. "github.com/cilium/ebpf/internal/unix"
  15. )
  16. // ErrNotSupported is returned whenever the kernel doesn't support a feature.
  17. var ErrNotSupported = internal.ErrNotSupported
  18. // ProgramID represents the unique ID of an eBPF program.
  19. type ProgramID uint32
  20. const (
  21. // Number of bytes to pad the output buffer for BPF_PROG_TEST_RUN.
  22. // This is currently the maximum of spare space allocated for SKB
  23. // and XDP programs, and equal to XDP_PACKET_HEADROOM + NET_IP_ALIGN.
  24. outputPad = 256 + 2
  25. )
  26. // DefaultVerifierLogSize is the default number of bytes allocated for the
  27. // verifier log.
  28. const DefaultVerifierLogSize = 64 * 1024
  29. // ProgramOptions control loading a program into the kernel.
  30. type ProgramOptions struct {
  31. // Controls the detail emitted by the kernel verifier. Set to non-zero
  32. // to enable logging.
  33. LogLevel uint32
  34. // Controls the output buffer size for the verifier. Defaults to
  35. // DefaultVerifierLogSize.
  36. LogSize int
  37. }
  38. // ProgramSpec defines a Program.
  39. type ProgramSpec struct {
  40. // Name is passed to the kernel as a debug aid. Must only contain
  41. // alpha numeric and '_' characters.
  42. Name string
  43. // Type determines at which hook in the kernel a program will run.
  44. Type ProgramType
  45. AttachType AttachType
  46. // Name of a kernel data structure to attach to. It's interpretation
  47. // depends on Type and AttachType.
  48. AttachTo string
  49. Instructions asm.Instructions
  50. // License of the program. Some helpers are only available if
  51. // the license is deemed compatible with the GPL.
  52. //
  53. // See https://www.kernel.org/doc/html/latest/process/license-rules.html#id1
  54. License string
  55. // Version used by tracing programs.
  56. //
  57. // Deprecated: superseded by BTF.
  58. KernelVersion uint32
  59. // The BTF associated with this program. Changing Instructions
  60. // will most likely invalidate the contained data, and may
  61. // result in errors when attempting to load it into the kernel.
  62. BTF *btf.Program
  63. // The byte order this program was compiled for, may be nil.
  64. ByteOrder binary.ByteOrder
  65. }
  66. // Copy returns a copy of the spec.
  67. func (ps *ProgramSpec) Copy() *ProgramSpec {
  68. if ps == nil {
  69. return nil
  70. }
  71. cpy := *ps
  72. cpy.Instructions = make(asm.Instructions, len(ps.Instructions))
  73. copy(cpy.Instructions, ps.Instructions)
  74. return &cpy
  75. }
  76. // Tag calculates the kernel tag for a series of instructions.
  77. //
  78. // Use asm.Instructions.Tag if you need to calculate for non-native endianness.
  79. func (ps *ProgramSpec) Tag() (string, error) {
  80. return ps.Instructions.Tag(internal.NativeEndian)
  81. }
  82. // Program represents BPF program loaded into the kernel.
  83. //
  84. // It is not safe to close a Program which is used by other goroutines.
  85. type Program struct {
  86. // Contains the output of the kernel verifier if enabled,
  87. // otherwise it is empty.
  88. VerifierLog string
  89. fd *internal.FD
  90. name string
  91. pinnedPath string
  92. typ ProgramType
  93. }
  94. // NewProgram creates a new Program.
  95. //
  96. // Loading a program for the first time will perform
  97. // feature detection by loading small, temporary programs.
  98. func NewProgram(spec *ProgramSpec) (*Program, error) {
  99. return NewProgramWithOptions(spec, ProgramOptions{})
  100. }
  101. // NewProgramWithOptions creates a new Program.
  102. //
  103. // Loading a program for the first time will perform
  104. // feature detection by loading small, temporary programs.
  105. func NewProgramWithOptions(spec *ProgramSpec, opts ProgramOptions) (*Program, error) {
  106. btfs := make(btfHandleCache)
  107. defer btfs.close()
  108. return newProgramWithOptions(spec, opts, btfs)
  109. }
  110. func newProgramWithOptions(spec *ProgramSpec, opts ProgramOptions, btfs btfHandleCache) (*Program, error) {
  111. if len(spec.Instructions) == 0 {
  112. return nil, errors.New("Instructions cannot be empty")
  113. }
  114. if len(spec.License) == 0 {
  115. return nil, errors.New("License cannot be empty")
  116. }
  117. if spec.ByteOrder != nil && spec.ByteOrder != internal.NativeEndian {
  118. return nil, fmt.Errorf("can't load %s program on %s", spec.ByteOrder, internal.NativeEndian)
  119. }
  120. insns := make(asm.Instructions, len(spec.Instructions))
  121. copy(insns, spec.Instructions)
  122. if err := fixupJumpsAndCalls(insns); err != nil {
  123. return nil, err
  124. }
  125. buf := bytes.NewBuffer(make([]byte, 0, len(spec.Instructions)*asm.InstructionSize))
  126. err := insns.Marshal(buf, internal.NativeEndian)
  127. if err != nil {
  128. return nil, err
  129. }
  130. bytecode := buf.Bytes()
  131. insCount := uint32(len(bytecode) / asm.InstructionSize)
  132. attr := &bpfProgLoadAttr{
  133. progType: spec.Type,
  134. expectedAttachType: spec.AttachType,
  135. insCount: insCount,
  136. instructions: internal.NewSlicePointer(bytecode),
  137. license: internal.NewStringPointer(spec.License),
  138. kernelVersion: spec.KernelVersion,
  139. }
  140. if haveObjName() == nil {
  141. attr.progName = newBPFObjName(spec.Name)
  142. }
  143. var btfDisabled bool
  144. if spec.BTF != nil {
  145. if relos, err := btf.ProgramRelocations(spec.BTF, nil); err != nil {
  146. return nil, fmt.Errorf("CO-RE relocations: %s", err)
  147. } else if len(relos) > 0 {
  148. return nil, fmt.Errorf("applying CO-RE relocations: %w", ErrNotSupported)
  149. }
  150. handle, err := btfs.load(btf.ProgramSpec(spec.BTF))
  151. btfDisabled = errors.Is(err, btf.ErrNotSupported)
  152. if err != nil && !btfDisabled {
  153. return nil, fmt.Errorf("load BTF: %w", err)
  154. }
  155. if handle != nil {
  156. attr.progBTFFd = uint32(handle.FD())
  157. recSize, bytes, err := btf.ProgramLineInfos(spec.BTF)
  158. if err != nil {
  159. return nil, fmt.Errorf("get BTF line infos: %w", err)
  160. }
  161. attr.lineInfoRecSize = recSize
  162. attr.lineInfoCnt = uint32(uint64(len(bytes)) / uint64(recSize))
  163. attr.lineInfo = internal.NewSlicePointer(bytes)
  164. recSize, bytes, err = btf.ProgramFuncInfos(spec.BTF)
  165. if err != nil {
  166. return nil, fmt.Errorf("get BTF function infos: %w", err)
  167. }
  168. attr.funcInfoRecSize = recSize
  169. attr.funcInfoCnt = uint32(uint64(len(bytes)) / uint64(recSize))
  170. attr.funcInfo = internal.NewSlicePointer(bytes)
  171. }
  172. }
  173. if spec.AttachTo != "" {
  174. target, err := resolveBTFType(spec.AttachTo, spec.Type, spec.AttachType)
  175. if err != nil {
  176. return nil, err
  177. }
  178. if target != nil {
  179. attr.attachBTFID = target.ID()
  180. }
  181. }
  182. logSize := DefaultVerifierLogSize
  183. if opts.LogSize > 0 {
  184. logSize = opts.LogSize
  185. }
  186. var logBuf []byte
  187. if opts.LogLevel > 0 {
  188. logBuf = make([]byte, logSize)
  189. attr.logLevel = opts.LogLevel
  190. attr.logSize = uint32(len(logBuf))
  191. attr.logBuf = internal.NewSlicePointer(logBuf)
  192. }
  193. fd, err := bpfProgLoad(attr)
  194. if err == nil {
  195. return &Program{internal.CString(logBuf), fd, spec.Name, "", spec.Type}, nil
  196. }
  197. logErr := err
  198. if opts.LogLevel == 0 {
  199. // Re-run with the verifier enabled to get better error messages.
  200. logBuf = make([]byte, logSize)
  201. attr.logLevel = 1
  202. attr.logSize = uint32(len(logBuf))
  203. attr.logBuf = internal.NewSlicePointer(logBuf)
  204. _, logErr = bpfProgLoad(attr)
  205. }
  206. if errors.Is(logErr, unix.EPERM) && logBuf[0] == 0 {
  207. // EPERM due to RLIMIT_MEMLOCK happens before the verifier, so we can
  208. // check that the log is empty to reduce false positives.
  209. return nil, fmt.Errorf("load program: RLIMIT_MEMLOCK may be too low: %w", logErr)
  210. }
  211. err = internal.ErrorWithLog(err, logBuf, logErr)
  212. if btfDisabled {
  213. return nil, fmt.Errorf("load program without BTF: %w", err)
  214. }
  215. return nil, fmt.Errorf("load program: %w", err)
  216. }
  217. // NewProgramFromFD creates a program from a raw fd.
  218. //
  219. // You should not use fd after calling this function.
  220. //
  221. // Requires at least Linux 4.10.
  222. func NewProgramFromFD(fd int) (*Program, error) {
  223. if fd < 0 {
  224. return nil, errors.New("invalid fd")
  225. }
  226. return newProgramFromFD(internal.NewFD(uint32(fd)))
  227. }
  228. // NewProgramFromID returns the program for a given id.
  229. //
  230. // Returns ErrNotExist, if there is no eBPF program with the given id.
  231. func NewProgramFromID(id ProgramID) (*Program, error) {
  232. fd, err := bpfObjGetFDByID(internal.BPF_PROG_GET_FD_BY_ID, uint32(id))
  233. if err != nil {
  234. return nil, fmt.Errorf("get program by id: %w", err)
  235. }
  236. return newProgramFromFD(fd)
  237. }
  238. func newProgramFromFD(fd *internal.FD) (*Program, error) {
  239. info, err := newProgramInfoFromFd(fd)
  240. if err != nil {
  241. fd.Close()
  242. return nil, fmt.Errorf("discover program type: %w", err)
  243. }
  244. return &Program{"", fd, "", "", info.Type}, nil
  245. }
  246. func (p *Program) String() string {
  247. if p.name != "" {
  248. return fmt.Sprintf("%s(%s)#%v", p.typ, p.name, p.fd)
  249. }
  250. return fmt.Sprintf("%s(%v)", p.typ, p.fd)
  251. }
  252. // Type returns the underlying type of the program.
  253. func (p *Program) Type() ProgramType {
  254. return p.typ
  255. }
  256. // Info returns metadata about the program.
  257. //
  258. // Requires at least 4.10.
  259. func (p *Program) Info() (*ProgramInfo, error) {
  260. return newProgramInfoFromFd(p.fd)
  261. }
  262. // FD gets the file descriptor of the Program.
  263. //
  264. // It is invalid to call this function after Close has been called.
  265. func (p *Program) FD() int {
  266. fd, err := p.fd.Value()
  267. if err != nil {
  268. // Best effort: -1 is the number most likely to be an
  269. // invalid file descriptor.
  270. return -1
  271. }
  272. return int(fd)
  273. }
  274. // Clone creates a duplicate of the Program.
  275. //
  276. // Closing the duplicate does not affect the original, and vice versa.
  277. //
  278. // Cloning a nil Program returns nil.
  279. func (p *Program) Clone() (*Program, error) {
  280. if p == nil {
  281. return nil, nil
  282. }
  283. dup, err := p.fd.Dup()
  284. if err != nil {
  285. return nil, fmt.Errorf("can't clone program: %w", err)
  286. }
  287. return &Program{p.VerifierLog, dup, p.name, "", p.typ}, nil
  288. }
  289. // Pin persists the Program on the BPF virtual file system past the lifetime of
  290. // the process that created it
  291. //
  292. // This requires bpffs to be mounted above fileName. See https://docs.cilium.io/en/k8s-doc/admin/#admin-mount-bpffs
  293. func (p *Program) Pin(fileName string) error {
  294. if err := pin(p.pinnedPath, fileName, p.fd); err != nil {
  295. return err
  296. }
  297. p.pinnedPath = fileName
  298. return nil
  299. }
  300. // Unpin removes the persisted state for the Program from the BPF virtual filesystem.
  301. //
  302. // Failed calls to Unpin will not alter the state returned by IsPinned.
  303. //
  304. // Unpinning an unpinned Program returns nil.
  305. func (p *Program) Unpin() error {
  306. if err := unpin(p.pinnedPath); err != nil {
  307. return err
  308. }
  309. p.pinnedPath = ""
  310. return nil
  311. }
  312. // IsPinned returns true if the Program has a non-empty pinned path.
  313. func (p *Program) IsPinned() bool {
  314. if p.pinnedPath == "" {
  315. return false
  316. }
  317. return true
  318. }
  319. // Close unloads the program from the kernel.
  320. func (p *Program) Close() error {
  321. if p == nil {
  322. return nil
  323. }
  324. return p.fd.Close()
  325. }
  326. // Test runs the Program in the kernel with the given input and returns the
  327. // value returned by the eBPF program. outLen may be zero.
  328. //
  329. // Note: the kernel expects at least 14 bytes input for an ethernet header for
  330. // XDP and SKB programs.
  331. //
  332. // This function requires at least Linux 4.12.
  333. func (p *Program) Test(in []byte) (uint32, []byte, error) {
  334. ret, out, _, err := p.testRun(in, 1, nil)
  335. if err != nil {
  336. return ret, nil, fmt.Errorf("can't test program: %w", err)
  337. }
  338. return ret, out, nil
  339. }
  340. // Benchmark runs the Program with the given input for a number of times
  341. // and returns the time taken per iteration.
  342. //
  343. // Returns the result of the last execution of the program and the time per
  344. // run or an error. reset is called whenever the benchmark syscall is
  345. // interrupted, and should be set to testing.B.ResetTimer or similar.
  346. //
  347. // Note: profiling a call to this function will skew it's results, see
  348. // https://github.com/cilium/ebpf/issues/24
  349. //
  350. // This function requires at least Linux 4.12.
  351. func (p *Program) Benchmark(in []byte, repeat int, reset func()) (uint32, time.Duration, error) {
  352. ret, _, total, err := p.testRun(in, repeat, reset)
  353. if err != nil {
  354. return ret, total, fmt.Errorf("can't benchmark program: %w", err)
  355. }
  356. return ret, total, nil
  357. }
  358. var haveProgTestRun = internal.FeatureTest("BPF_PROG_TEST_RUN", "4.12", func() error {
  359. prog, err := NewProgram(&ProgramSpec{
  360. Type: SocketFilter,
  361. Instructions: asm.Instructions{
  362. asm.LoadImm(asm.R0, 0, asm.DWord),
  363. asm.Return(),
  364. },
  365. License: "MIT",
  366. })
  367. if err != nil {
  368. // This may be because we lack sufficient permissions, etc.
  369. return err
  370. }
  371. defer prog.Close()
  372. // Programs require at least 14 bytes input
  373. in := make([]byte, 14)
  374. attr := bpfProgTestRunAttr{
  375. fd: uint32(prog.FD()),
  376. dataSizeIn: uint32(len(in)),
  377. dataIn: internal.NewSlicePointer(in),
  378. }
  379. err = bpfProgTestRun(&attr)
  380. if errors.Is(err, unix.EINVAL) {
  381. // Check for EINVAL specifically, rather than err != nil since we
  382. // otherwise misdetect due to insufficient permissions.
  383. return internal.ErrNotSupported
  384. }
  385. if errors.Is(err, unix.EINTR) {
  386. // We know that PROG_TEST_RUN is supported if we get EINTR.
  387. return nil
  388. }
  389. return err
  390. })
  391. func (p *Program) testRun(in []byte, repeat int, reset func()) (uint32, []byte, time.Duration, error) {
  392. if uint(repeat) > math.MaxUint32 {
  393. return 0, nil, 0, fmt.Errorf("repeat is too high")
  394. }
  395. if len(in) == 0 {
  396. return 0, nil, 0, fmt.Errorf("missing input")
  397. }
  398. if uint(len(in)) > math.MaxUint32 {
  399. return 0, nil, 0, fmt.Errorf("input is too long")
  400. }
  401. if err := haveProgTestRun(); err != nil {
  402. return 0, nil, 0, err
  403. }
  404. // Older kernels ignore the dataSizeOut argument when copying to user space.
  405. // Combined with things like bpf_xdp_adjust_head() we don't really know what the final
  406. // size will be. Hence we allocate an output buffer which we hope will always be large
  407. // enough, and panic if the kernel wrote past the end of the allocation.
  408. // See https://patchwork.ozlabs.org/cover/1006822/
  409. out := make([]byte, len(in)+outputPad)
  410. fd, err := p.fd.Value()
  411. if err != nil {
  412. return 0, nil, 0, err
  413. }
  414. attr := bpfProgTestRunAttr{
  415. fd: fd,
  416. dataSizeIn: uint32(len(in)),
  417. dataSizeOut: uint32(len(out)),
  418. dataIn: internal.NewSlicePointer(in),
  419. dataOut: internal.NewSlicePointer(out),
  420. repeat: uint32(repeat),
  421. }
  422. for {
  423. err = bpfProgTestRun(&attr)
  424. if err == nil {
  425. break
  426. }
  427. if errors.Is(err, unix.EINTR) {
  428. if reset != nil {
  429. reset()
  430. }
  431. continue
  432. }
  433. return 0, nil, 0, fmt.Errorf("can't run test: %w", err)
  434. }
  435. if int(attr.dataSizeOut) > cap(out) {
  436. // Houston, we have a problem. The program created more data than we allocated,
  437. // and the kernel wrote past the end of our buffer.
  438. panic("kernel wrote past end of output buffer")
  439. }
  440. out = out[:int(attr.dataSizeOut)]
  441. total := time.Duration(attr.duration) * time.Nanosecond
  442. return attr.retval, out, total, nil
  443. }
  444. func unmarshalProgram(buf []byte) (*Program, error) {
  445. if len(buf) != 4 {
  446. return nil, errors.New("program id requires 4 byte value")
  447. }
  448. // Looking up an entry in a nested map or prog array returns an id,
  449. // not an fd.
  450. id := internal.NativeEndian.Uint32(buf)
  451. return NewProgramFromID(ProgramID(id))
  452. }
  453. func marshalProgram(p *Program, length int) ([]byte, error) {
  454. if length != 4 {
  455. return nil, fmt.Errorf("can't marshal program to %d bytes", length)
  456. }
  457. value, err := p.fd.Value()
  458. if err != nil {
  459. return nil, err
  460. }
  461. buf := make([]byte, 4)
  462. internal.NativeEndian.PutUint32(buf, value)
  463. return buf, nil
  464. }
  465. // Attach a Program.
  466. //
  467. // Deprecated: use link.RawAttachProgram instead.
  468. func (p *Program) Attach(fd int, typ AttachType, flags AttachFlags) error {
  469. if fd < 0 {
  470. return errors.New("invalid fd")
  471. }
  472. pfd, err := p.fd.Value()
  473. if err != nil {
  474. return err
  475. }
  476. attr := internal.BPFProgAttachAttr{
  477. TargetFd: uint32(fd),
  478. AttachBpfFd: pfd,
  479. AttachType: uint32(typ),
  480. AttachFlags: uint32(flags),
  481. }
  482. return internal.BPFProgAttach(&attr)
  483. }
  484. // Detach a Program.
  485. //
  486. // Deprecated: use link.RawDetachProgram instead.
  487. func (p *Program) Detach(fd int, typ AttachType, flags AttachFlags) error {
  488. if fd < 0 {
  489. return errors.New("invalid fd")
  490. }
  491. if flags != 0 {
  492. return errors.New("flags must be zero")
  493. }
  494. pfd, err := p.fd.Value()
  495. if err != nil {
  496. return err
  497. }
  498. attr := internal.BPFProgDetachAttr{
  499. TargetFd: uint32(fd),
  500. AttachBpfFd: pfd,
  501. AttachType: uint32(typ),
  502. }
  503. return internal.BPFProgDetach(&attr)
  504. }
  505. // LoadPinnedProgram loads a Program from a BPF file.
  506. //
  507. // Requires at least Linux 4.11.
  508. func LoadPinnedProgram(fileName string) (*Program, error) {
  509. fd, err := internal.BPFObjGet(fileName)
  510. if err != nil {
  511. return nil, err
  512. }
  513. info, err := newProgramInfoFromFd(fd)
  514. if err != nil {
  515. _ = fd.Close()
  516. return nil, fmt.Errorf("info for %s: %w", fileName, err)
  517. }
  518. return &Program{"", fd, filepath.Base(fileName), "", info.Type}, nil
  519. }
  520. // SanitizeName replaces all invalid characters in name with replacement.
  521. // Passing a negative value for replacement will delete characters instead
  522. // of replacing them. Use this to automatically generate valid names for maps
  523. // and programs at runtime.
  524. //
  525. // The set of allowed characters depends on the running kernel version.
  526. // Dots are only allowed as of kernel 5.2.
  527. func SanitizeName(name string, replacement rune) string {
  528. return strings.Map(func(char rune) rune {
  529. if invalidBPFObjNameChar(char) {
  530. return replacement
  531. }
  532. return char
  533. }, name)
  534. }
  535. // ProgramGetNextID returns the ID of the next eBPF program.
  536. //
  537. // Returns ErrNotExist, if there is no next eBPF program.
  538. func ProgramGetNextID(startID ProgramID) (ProgramID, error) {
  539. id, err := objGetNextID(internal.BPF_PROG_GET_NEXT_ID, uint32(startID))
  540. return ProgramID(id), err
  541. }
  542. // ID returns the systemwide unique ID of the program.
  543. //
  544. // Deprecated: use ProgramInfo.ID() instead.
  545. func (p *Program) ID() (ProgramID, error) {
  546. info, err := bpfGetProgInfoByFD(p.fd)
  547. if err != nil {
  548. return ProgramID(0), err
  549. }
  550. return ProgramID(info.id), nil
  551. }
  552. func findKernelType(name string, typ btf.Type) error {
  553. kernel, err := btf.LoadKernelSpec()
  554. if err != nil {
  555. return fmt.Errorf("can't load kernel spec: %w", err)
  556. }
  557. return kernel.FindType(name, typ)
  558. }
  559. func resolveBTFType(name string, progType ProgramType, attachType AttachType) (btf.Type, error) {
  560. type match struct {
  561. p ProgramType
  562. a AttachType
  563. }
  564. target := match{progType, attachType}
  565. switch target {
  566. case match{LSM, AttachLSMMac}:
  567. var target btf.Func
  568. err := findKernelType("bpf_lsm_"+name, &target)
  569. if errors.Is(err, btf.ErrNotFound) {
  570. return nil, &internal.UnsupportedFeatureError{
  571. Name: name + " LSM hook",
  572. }
  573. }
  574. if err != nil {
  575. return nil, fmt.Errorf("resolve BTF for LSM hook %s: %w", name, err)
  576. }
  577. return &target, nil
  578. case match{Tracing, AttachTraceIter}:
  579. var target btf.Func
  580. err := findKernelType("bpf_iter_"+name, &target)
  581. if errors.Is(err, btf.ErrNotFound) {
  582. return nil, &internal.UnsupportedFeatureError{
  583. Name: name + " iterator",
  584. }
  585. }
  586. if err != nil {
  587. return nil, fmt.Errorf("resolve BTF for iterator %s: %w", name, err)
  588. }
  589. return &target, nil
  590. default:
  591. return nil, nil
  592. }
  593. }