syscalls.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package link
  2. import (
  3. "errors"
  4. "unsafe"
  5. "github.com/cilium/ebpf"
  6. "github.com/cilium/ebpf/asm"
  7. "github.com/cilium/ebpf/internal"
  8. "github.com/cilium/ebpf/internal/unix"
  9. )
  10. // Type is the kind of link.
  11. type Type uint32
  12. // Valid link types.
  13. //
  14. // Equivalent to enum bpf_link_type.
  15. const (
  16. UnspecifiedType Type = iota
  17. RawTracepointType
  18. TracingType
  19. CgroupType
  20. IterType
  21. NetNsType
  22. XDPType
  23. )
  24. var haveProgAttach = internal.FeatureTest("BPF_PROG_ATTACH", "4.10", func() error {
  25. prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
  26. Type: ebpf.CGroupSKB,
  27. AttachType: ebpf.AttachCGroupInetIngress,
  28. License: "MIT",
  29. Instructions: asm.Instructions{
  30. asm.Mov.Imm(asm.R0, 0),
  31. asm.Return(),
  32. },
  33. })
  34. if err != nil {
  35. return internal.ErrNotSupported
  36. }
  37. // BPF_PROG_ATTACH was introduced at the same time as CGgroupSKB,
  38. // so being able to load the program is enough to infer that we
  39. // have the syscall.
  40. prog.Close()
  41. return nil
  42. })
  43. var haveProgAttachReplace = internal.FeatureTest("BPF_PROG_ATTACH atomic replacement", "5.5", func() error {
  44. if err := haveProgAttach(); err != nil {
  45. return err
  46. }
  47. prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
  48. Type: ebpf.CGroupSKB,
  49. AttachType: ebpf.AttachCGroupInetIngress,
  50. License: "MIT",
  51. Instructions: asm.Instructions{
  52. asm.Mov.Imm(asm.R0, 0),
  53. asm.Return(),
  54. },
  55. })
  56. if err != nil {
  57. return internal.ErrNotSupported
  58. }
  59. defer prog.Close()
  60. // We know that we have BPF_PROG_ATTACH since we can load CGroupSKB programs.
  61. // If passing BPF_F_REPLACE gives us EINVAL we know that the feature isn't
  62. // present.
  63. attr := internal.BPFProgAttachAttr{
  64. // We rely on this being checked after attachFlags.
  65. TargetFd: ^uint32(0),
  66. AttachBpfFd: uint32(prog.FD()),
  67. AttachType: uint32(ebpf.AttachCGroupInetIngress),
  68. AttachFlags: uint32(flagReplace),
  69. }
  70. err = internal.BPFProgAttach(&attr)
  71. if errors.Is(err, unix.EINVAL) {
  72. return internal.ErrNotSupported
  73. }
  74. if errors.Is(err, unix.EBADF) {
  75. return nil
  76. }
  77. return err
  78. })
  79. type bpfLinkCreateAttr struct {
  80. progFd uint32
  81. targetFd uint32
  82. attachType ebpf.AttachType
  83. flags uint32
  84. }
  85. func bpfLinkCreate(attr *bpfLinkCreateAttr) (*internal.FD, error) {
  86. ptr, err := internal.BPF(internal.BPF_LINK_CREATE, unsafe.Pointer(attr), unsafe.Sizeof(*attr))
  87. if err != nil {
  88. return nil, err
  89. }
  90. return internal.NewFD(uint32(ptr)), nil
  91. }
  92. type bpfLinkUpdateAttr struct {
  93. linkFd uint32
  94. newProgFd uint32
  95. flags uint32
  96. oldProgFd uint32
  97. }
  98. func bpfLinkUpdate(attr *bpfLinkUpdateAttr) error {
  99. _, err := internal.BPF(internal.BPF_LINK_UPDATE, unsafe.Pointer(attr), unsafe.Sizeof(*attr))
  100. return err
  101. }
  102. var haveBPFLink = internal.FeatureTest("bpf_link", "5.7", func() error {
  103. prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
  104. Type: ebpf.CGroupSKB,
  105. AttachType: ebpf.AttachCGroupInetIngress,
  106. License: "MIT",
  107. Instructions: asm.Instructions{
  108. asm.Mov.Imm(asm.R0, 0),
  109. asm.Return(),
  110. },
  111. })
  112. if err != nil {
  113. return internal.ErrNotSupported
  114. }
  115. defer prog.Close()
  116. attr := bpfLinkCreateAttr{
  117. // This is a hopefully invalid file descriptor, which triggers EBADF.
  118. targetFd: ^uint32(0),
  119. progFd: uint32(prog.FD()),
  120. attachType: ebpf.AttachCGroupInetIngress,
  121. }
  122. _, err = bpfLinkCreate(&attr)
  123. if errors.Is(err, unix.EINVAL) {
  124. return internal.ErrNotSupported
  125. }
  126. if errors.Is(err, unix.EBADF) {
  127. return nil
  128. }
  129. return err
  130. })
  131. type bpfIterCreateAttr struct {
  132. linkFd uint32
  133. flags uint32
  134. }
  135. func bpfIterCreate(attr *bpfIterCreateAttr) (*internal.FD, error) {
  136. ptr, err := internal.BPF(internal.BPF_ITER_CREATE, unsafe.Pointer(attr), unsafe.Sizeof(*attr))
  137. if err == nil {
  138. return internal.NewFD(uint32(ptr)), nil
  139. }
  140. return nil, err
  141. }
  142. type bpfRawTracepointOpenAttr struct {
  143. name internal.Pointer
  144. fd uint32
  145. _ uint32
  146. }
  147. func bpfRawTracepointOpen(attr *bpfRawTracepointOpenAttr) (*internal.FD, error) {
  148. ptr, err := internal.BPF(internal.BPF_RAW_TRACEPOINT_OPEN, unsafe.Pointer(attr), unsafe.Sizeof(*attr))
  149. if err == nil {
  150. return internal.NewFD(uint32(ptr)), nil
  151. }
  152. return nil, err
  153. }