prog.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. package ebpf
  2. import (
  3. "bytes"
  4. "fmt"
  5. "math"
  6. "path/filepath"
  7. "strings"
  8. "time"
  9. "unsafe"
  10. "github.com/cilium/ebpf/asm"
  11. "github.com/cilium/ebpf/internal"
  12. "github.com/cilium/ebpf/internal/unix"
  13. "github.com/pkg/errors"
  14. )
  15. var (
  16. errNotSupported = errors.New("ebpf: not supported by kernel")
  17. )
  18. const (
  19. // Number of bytes to pad the output buffer for BPF_PROG_TEST_RUN.
  20. // This is currently the maximum of spare space allocated for SKB
  21. // and XDP programs, and equal to XDP_PACKET_HEADROOM + NET_IP_ALIGN.
  22. outputPad = 256 + 2
  23. )
  24. // DefaultVerifierLogSize is the default number of bytes allocated for the
  25. // verifier log.
  26. const DefaultVerifierLogSize = 64 * 1024
  27. // ProgramOptions control loading a program into the kernel.
  28. type ProgramOptions struct {
  29. // Controls the detail emitted by the kernel verifier. Set to non-zero
  30. // to enable logging.
  31. LogLevel uint32
  32. // Controls the output buffer size for the verifier. Defaults to
  33. // DefaultVerifierLogSize.
  34. LogSize int
  35. }
  36. // ProgramSpec defines a Program
  37. type ProgramSpec struct {
  38. // Name is passed to the kernel as a debug aid. Must only contain
  39. // alpha numeric and '_' characters.
  40. Name string
  41. Type ProgramType
  42. AttachType AttachType
  43. Instructions asm.Instructions
  44. License string
  45. KernelVersion uint32
  46. }
  47. // Copy returns a copy of the spec.
  48. func (ps *ProgramSpec) Copy() *ProgramSpec {
  49. if ps == nil {
  50. return nil
  51. }
  52. cpy := *ps
  53. cpy.Instructions = make(asm.Instructions, len(ps.Instructions))
  54. copy(cpy.Instructions, ps.Instructions)
  55. return &cpy
  56. }
  57. // Program represents BPF program loaded into the kernel.
  58. //
  59. // It is not safe to close a Program which is used by other goroutines.
  60. type Program struct {
  61. // Contains the output of the kernel verifier if enabled,
  62. // otherwise it is empty.
  63. VerifierLog string
  64. fd *bpfFD
  65. name string
  66. abi ProgramABI
  67. }
  68. // NewProgram creates a new Program.
  69. //
  70. // Loading a program for the first time will perform
  71. // feature detection by loading small, temporary programs.
  72. func NewProgram(spec *ProgramSpec) (*Program, error) {
  73. return NewProgramWithOptions(spec, ProgramOptions{})
  74. }
  75. // NewProgramWithOptions creates a new Program.
  76. //
  77. // Loading a program for the first time will perform
  78. // feature detection by loading small, temporary programs.
  79. func NewProgramWithOptions(spec *ProgramSpec, opts ProgramOptions) (*Program, error) {
  80. attr, err := convertProgramSpec(spec, haveObjName.Result())
  81. if err != nil {
  82. return nil, err
  83. }
  84. logSize := DefaultVerifierLogSize
  85. if opts.LogSize > 0 {
  86. logSize = opts.LogSize
  87. }
  88. var logBuf []byte
  89. if opts.LogLevel > 0 {
  90. logBuf = make([]byte, logSize)
  91. attr.logLevel = opts.LogLevel
  92. attr.logSize = uint32(len(logBuf))
  93. attr.logBuf = newPtr(unsafe.Pointer(&logBuf[0]))
  94. }
  95. fd, err := bpfProgLoad(attr)
  96. if err == nil {
  97. prog := newProgram(fd, spec.Name, &ProgramABI{spec.Type})
  98. prog.VerifierLog = convertCString(logBuf)
  99. return prog, nil
  100. }
  101. truncated := errors.Cause(err) == unix.ENOSPC
  102. if opts.LogLevel == 0 {
  103. // Re-run with the verifier enabled to get better error messages.
  104. logBuf = make([]byte, logSize)
  105. attr.logLevel = 1
  106. attr.logSize = uint32(len(logBuf))
  107. attr.logBuf = newPtr(unsafe.Pointer(&logBuf[0]))
  108. _, nerr := bpfProgLoad(attr)
  109. truncated = errors.Cause(nerr) == unix.ENOSPC
  110. }
  111. logs := convertCString(logBuf)
  112. if truncated {
  113. logs += "\n(truncated...)"
  114. }
  115. return nil, &loadError{err, logs}
  116. }
  117. // NewProgramFromFD creates a program from a raw fd.
  118. //
  119. // You should not use fd after calling this function.
  120. func NewProgramFromFD(fd int) (*Program, error) {
  121. if fd < 0 {
  122. return nil, errors.New("invalid fd")
  123. }
  124. bpfFd := newBPFFD(uint32(fd))
  125. info, err := bpfGetProgInfoByFD(bpfFd)
  126. if err != nil {
  127. bpfFd.forget()
  128. return nil, err
  129. }
  130. var name string
  131. if bpfName := convertCString(info.name[:]); bpfName != "" {
  132. name = bpfName
  133. } else {
  134. name = convertCString(info.tag[:])
  135. }
  136. return newProgram(bpfFd, name, newProgramABIFromInfo(info)), nil
  137. }
  138. func newProgram(fd *bpfFD, name string, abi *ProgramABI) *Program {
  139. return &Program{
  140. name: name,
  141. fd: fd,
  142. abi: *abi,
  143. }
  144. }
  145. func convertProgramSpec(spec *ProgramSpec, includeName bool) (*bpfProgLoadAttr, error) {
  146. if len(spec.Instructions) == 0 {
  147. return nil, errors.New("Instructions cannot be empty")
  148. }
  149. if len(spec.License) == 0 {
  150. return nil, errors.New("License cannot be empty")
  151. }
  152. buf := bytes.NewBuffer(make([]byte, 0, len(spec.Instructions)*asm.InstructionSize))
  153. err := spec.Instructions.Marshal(buf, internal.NativeEndian)
  154. if err != nil {
  155. return nil, err
  156. }
  157. bytecode := buf.Bytes()
  158. insCount := uint32(len(bytecode) / asm.InstructionSize)
  159. lic := []byte(spec.License)
  160. attr := &bpfProgLoadAttr{
  161. progType: spec.Type,
  162. expectedAttachType: spec.AttachType,
  163. insCount: insCount,
  164. instructions: newPtr(unsafe.Pointer(&bytecode[0])),
  165. license: newPtr(unsafe.Pointer(&lic[0])),
  166. }
  167. name, err := newBPFObjName(spec.Name)
  168. if err != nil {
  169. return nil, err
  170. }
  171. if includeName {
  172. attr.progName = name
  173. }
  174. return attr, nil
  175. }
  176. func (p *Program) String() string {
  177. if p.name != "" {
  178. return fmt.Sprintf("%s(%s)#%s", p.abi.Type, p.name, p.fd)
  179. }
  180. return fmt.Sprintf("%s#%s", p.abi.Type, p.fd)
  181. }
  182. // ABI gets the ABI of the Program
  183. func (p *Program) ABI() ProgramABI {
  184. return p.abi
  185. }
  186. // FD gets the file descriptor of the Program.
  187. //
  188. // It is invalid to call this function after Close has been called.
  189. func (p *Program) FD() int {
  190. fd, err := p.fd.value()
  191. if err != nil {
  192. // Best effort: -1 is the number most likely to be an
  193. // invalid file descriptor.
  194. return -1
  195. }
  196. return int(fd)
  197. }
  198. // Clone creates a duplicate of the Program.
  199. //
  200. // Closing the duplicate does not affect the original, and vice versa.
  201. //
  202. // Cloning a nil Program returns nil.
  203. func (p *Program) Clone() (*Program, error) {
  204. if p == nil {
  205. return nil, nil
  206. }
  207. dup, err := p.fd.dup()
  208. if err != nil {
  209. return nil, errors.Wrap(err, "can't clone program")
  210. }
  211. return newProgram(dup, p.name, &p.abi), nil
  212. }
  213. // Pin persists the Program past the lifetime of the process that created it
  214. //
  215. // This requires bpffs to be mounted above fileName. See http://cilium.readthedocs.io/en/doc-1.0/kubernetes/install/#mounting-the-bpf-fs-optional
  216. func (p *Program) Pin(fileName string) error {
  217. return errors.Wrap(bpfPinObject(fileName, p.fd), "can't pin program")
  218. }
  219. // Close unloads the program from the kernel.
  220. func (p *Program) Close() error {
  221. if p == nil {
  222. return nil
  223. }
  224. return p.fd.close()
  225. }
  226. // Test runs the Program in the kernel with the given input and returns the
  227. // value returned by the eBPF program. outLen may be zero.
  228. //
  229. // Note: the kernel expects at least 14 bytes input for an ethernet header for
  230. // XDP and SKB programs.
  231. //
  232. // This function requires at least Linux 4.12.
  233. func (p *Program) Test(in []byte) (uint32, []byte, error) {
  234. ret, out, _, err := p.testRun(in, 1)
  235. return ret, out, err
  236. }
  237. // Benchmark runs the Program with the given input for a number of times
  238. // and returns the time taken per iteration.
  239. //
  240. // The returned value is the return value of the last execution of
  241. // the program.
  242. //
  243. // This function requires at least Linux 4.12.
  244. func (p *Program) Benchmark(in []byte, repeat int) (uint32, time.Duration, error) {
  245. ret, _, total, err := p.testRun(in, repeat)
  246. return ret, total, err
  247. }
  248. var noProgTestRun = featureTest{
  249. Fn: func() bool {
  250. prog, err := NewProgram(&ProgramSpec{
  251. Type: SocketFilter,
  252. Instructions: asm.Instructions{
  253. asm.LoadImm(asm.R0, 0, asm.DWord),
  254. asm.Return(),
  255. },
  256. License: "MIT",
  257. })
  258. if err != nil {
  259. // This may be because we lack sufficient permissions, etc.
  260. return false
  261. }
  262. defer prog.Close()
  263. fd, err := prog.fd.value()
  264. if err != nil {
  265. return false
  266. }
  267. // Programs require at least 14 bytes input
  268. in := make([]byte, 14)
  269. attr := bpfProgTestRunAttr{
  270. fd: fd,
  271. dataSizeIn: uint32(len(in)),
  272. dataIn: newPtr(unsafe.Pointer(&in[0])),
  273. }
  274. _, err = bpfCall(_ProgTestRun, unsafe.Pointer(&attr), unsafe.Sizeof(attr))
  275. return errors.Cause(err) == unix.EINVAL
  276. },
  277. }
  278. func (p *Program) testRun(in []byte, repeat int) (uint32, []byte, time.Duration, error) {
  279. if uint(repeat) > math.MaxUint32 {
  280. return 0, nil, 0, fmt.Errorf("repeat is too high")
  281. }
  282. if len(in) == 0 {
  283. return 0, nil, 0, fmt.Errorf("missing input")
  284. }
  285. if uint(len(in)) > math.MaxUint32 {
  286. return 0, nil, 0, fmt.Errorf("input is too long")
  287. }
  288. if noProgTestRun.Result() {
  289. return 0, nil, 0, errNotSupported
  290. }
  291. // Older kernels ignore the dataSizeOut argument when copying to user space.
  292. // Combined with things like bpf_xdp_adjust_head() we don't really know what the final
  293. // size will be. Hence we allocate an output buffer which we hope will always be large
  294. // enough, and panic if the kernel wrote past the end of the allocation.
  295. // See https://patchwork.ozlabs.org/cover/1006822/
  296. out := make([]byte, len(in)+outputPad)
  297. fd, err := p.fd.value()
  298. if err != nil {
  299. return 0, nil, 0, err
  300. }
  301. attr := bpfProgTestRunAttr{
  302. fd: fd,
  303. dataSizeIn: uint32(len(in)),
  304. dataSizeOut: uint32(len(out)),
  305. dataIn: newPtr(unsafe.Pointer(&in[0])),
  306. dataOut: newPtr(unsafe.Pointer(&out[0])),
  307. repeat: uint32(repeat),
  308. }
  309. _, err = bpfCall(_ProgTestRun, unsafe.Pointer(&attr), unsafe.Sizeof(attr))
  310. if err != nil {
  311. return 0, nil, 0, errors.Wrap(err, "can't run test")
  312. }
  313. if int(attr.dataSizeOut) > cap(out) {
  314. // Houston, we have a problem. The program created more data than we allocated,
  315. // and the kernel wrote past the end of our buffer.
  316. panic("kernel wrote past end of output buffer")
  317. }
  318. out = out[:int(attr.dataSizeOut)]
  319. total := time.Duration(attr.duration) * time.Nanosecond
  320. return attr.retval, out, total, nil
  321. }
  322. func unmarshalProgram(buf []byte) (*Program, error) {
  323. if len(buf) != 4 {
  324. return nil, errors.New("program id requires 4 byte value")
  325. }
  326. // Looking up an entry in a nested map or prog array returns an id,
  327. // not an fd.
  328. id := internal.NativeEndian.Uint32(buf)
  329. fd, err := bpfGetProgramFDByID(id)
  330. if err != nil {
  331. return nil, err
  332. }
  333. abi, err := newProgramABIFromFd(fd)
  334. if err != nil {
  335. _ = fd.close()
  336. return nil, err
  337. }
  338. return newProgram(fd, "", abi), nil
  339. }
  340. // MarshalBinary implements BinaryMarshaler.
  341. func (p *Program) MarshalBinary() ([]byte, error) {
  342. value, err := p.fd.value()
  343. if err != nil {
  344. return nil, err
  345. }
  346. buf := make([]byte, 4)
  347. internal.NativeEndian.PutUint32(buf, value)
  348. return buf, nil
  349. }
  350. // Attach a Program to a container object fd
  351. func (p *Program) Attach(fd int, typ AttachType, flags AttachFlags) error {
  352. if fd < 0 {
  353. return errors.New("invalid fd")
  354. }
  355. pfd, err := p.fd.value()
  356. if err != nil {
  357. return err
  358. }
  359. attr := bpfProgAlterAttr{
  360. targetFd: uint32(fd),
  361. attachBpfFd: pfd,
  362. attachType: uint32(typ),
  363. attachFlags: uint32(flags),
  364. }
  365. return bpfProgAlter(_ProgAttach, &attr)
  366. }
  367. // Detach a Program from a container object fd
  368. func (p *Program) Detach(fd int, typ AttachType, flags AttachFlags) error {
  369. if fd < 0 {
  370. return errors.New("invalid fd")
  371. }
  372. pfd, err := p.fd.value()
  373. if err != nil {
  374. return err
  375. }
  376. attr := bpfProgAlterAttr{
  377. targetFd: uint32(fd),
  378. attachBpfFd: pfd,
  379. attachType: uint32(typ),
  380. attachFlags: uint32(flags),
  381. }
  382. return bpfProgAlter(_ProgDetach, &attr)
  383. }
  384. // LoadPinnedProgram loads a Program from a BPF file.
  385. //
  386. // Requires at least Linux 4.13, use LoadPinnedProgramExplicit on
  387. // earlier versions.
  388. func LoadPinnedProgram(fileName string) (*Program, error) {
  389. fd, err := bpfGetObject(fileName)
  390. if err != nil {
  391. return nil, err
  392. }
  393. abi, err := newProgramABIFromFd(fd)
  394. if err != nil {
  395. _ = fd.close()
  396. return nil, err
  397. }
  398. return newProgram(fd, filepath.Base(fileName), abi), nil
  399. }
  400. // LoadPinnedProgramExplicit loads a program with explicit parameters.
  401. func LoadPinnedProgramExplicit(fileName string, abi *ProgramABI) (*Program, error) {
  402. fd, err := bpfGetObject(fileName)
  403. if err != nil {
  404. return nil, err
  405. }
  406. return newProgram(fd, filepath.Base(fileName), abi), nil
  407. }
  408. // SanitizeName replaces all invalid characters in name.
  409. //
  410. // Use this to automatically generate valid names for maps and
  411. // programs at run time.
  412. //
  413. // Passing a negative value for replacement will delete characters
  414. // instead of replacing them.
  415. func SanitizeName(name string, replacement rune) string {
  416. return strings.Map(func(char rune) rune {
  417. if invalidBPFObjNameChar(char) {
  418. return replacement
  419. }
  420. return char
  421. }, name)
  422. }
  423. type loadError struct {
  424. cause error
  425. verifierLog string
  426. }
  427. func (le *loadError) Error() string {
  428. if le.verifierLog == "" {
  429. return fmt.Sprintf("failed to load program: %s", le.cause)
  430. }
  431. return fmt.Sprintf("failed to load program: %s: %s", le.cause, le.verifierLog)
  432. }
  433. func (le *loadError) Cause() error {
  434. return le.cause
  435. }
  436. // IsNotSupported returns true if an error occurred because
  437. // the kernel does not have support for a specific feature.
  438. func IsNotSupported(err error) bool {
  439. return errors.Cause(err) == errNotSupported
  440. }