doc.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. /*
  5. Package bpf implements marshaling and unmarshaling of programs for the
  6. Berkeley Packet Filter virtual machine.
  7. BPF's main use is to specify a packet filter for network taps, so that
  8. the kernel doesn't have to expensively copy every packet it sees to
  9. userspace. However, it's been repurposed to other areas where running
  10. user code in-kernel is needed. For example, Linux's seccomp uses BPF
  11. to apply security policies to system calls. For simplicity, this
  12. documentation refers only to packets, but other uses of BPF have their
  13. own data payloads.
  14. BPF programs run in a restricted virtual machine. It has almost no
  15. access to kernel functions, and while conditional branches are
  16. allowed, they can only jump forwards, to guarantee that there are no
  17. infinite loops.
  18. The virtual machine
  19. The BPF VM is an accumulator machine. Its main register, called
  20. register A, is an implicit source and destination in all arithmetic
  21. and logic operations. The machine also has 16 scratch registers for
  22. temporary storage, and an indirection register (register X) for
  23. indirect memory access. All registers are 32 bits wide.
  24. Each run of a BPF program is given one packet, which is placed in the
  25. VM's read-only "main memory". LoadAbsolute and LoadIndirect
  26. instructions can fetch up to 32 bits at a time into register A for
  27. examination.
  28. The goal of a BPF program is to produce and return a verdict (uint32),
  29. which tells the kernel what to do with the packet. In the context of
  30. packet filtering, the returned value is the number of bytes of the
  31. packet to forward to userspace, or 0 to ignore the packet. Other
  32. contexts like seccomp define their own return values.
  33. In order to simplify programs, attempts to read past the end of the
  34. packet terminate the program execution with a verdict of 0 (ignore
  35. packet). This means that the vast majority of BPF programs don't need
  36. to do any explicit bounds checking.
  37. In addition to the bytes of the packet, some BPF programs have access
  38. to extensions, which are essentially calls to kernel utility
  39. functions. Currently, the only extensions supported by this package
  40. are the Linux packet filter extensions.
  41. Examples
  42. This packet filter selects all ARP packets.
  43. bpf.Assemble([]bpf.Instruction{
  44. // Load "EtherType" field from the ethernet header.
  45. bpf.LoadAbsolute{Off: 12, Size: 2},
  46. // Skip over the next instruction if EtherType is not ARP.
  47. bpf.JumpIf{Cond: bpf.JumpNotEqual, Val: 0x0806, SkipTrue: 1},
  48. // Verdict is "send up to 4k of the packet to userspace."
  49. bpf.RetConstant{Val: 4096},
  50. // Verdict is "ignore packet."
  51. bpf.RetConstant{Val: 0},
  52. })
  53. This packet filter captures a random 1% sample of traffic.
  54. bpf.Assemble([]bpf.Instruction{
  55. // Get a 32-bit random number from the Linux kernel.
  56. bpf.LoadExtension{Num: bpf.ExtRand},
  57. // 1% dice roll?
  58. bpf.JumpIf{Cond: bpf.JumpLessThan, Val: 2^32/100, SkipFalse: 1},
  59. // Capture.
  60. bpf.RetConstant{Val: 4096},
  61. // Ignore.
  62. bpf.RetConstant{Val: 0},
  63. })
  64. */
  65. package bpf