link.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package link
  2. import (
  3. "fmt"
  4. "unsafe"
  5. "github.com/cilium/ebpf"
  6. "github.com/cilium/ebpf/internal"
  7. )
  8. var ErrNotSupported = internal.ErrNotSupported
  9. // Link represents a Program attached to a BPF hook.
  10. type Link interface {
  11. // Replace the current program with a new program.
  12. //
  13. // Passing a nil program is an error. May return an error wrapping ErrNotSupported.
  14. Update(*ebpf.Program) error
  15. // Persist a link by pinning it into a bpffs.
  16. //
  17. // May return an error wrapping ErrNotSupported.
  18. Pin(string) error
  19. // Close frees resources.
  20. //
  21. // The link will be broken unless it has been pinned. A link
  22. // may continue past the lifetime of the process if Close is
  23. // not called.
  24. Close() error
  25. // Prevent external users from implementing this interface.
  26. isLink()
  27. }
  28. // ID uniquely identifies a BPF link.
  29. type ID uint32
  30. // RawLinkOptions control the creation of a raw link.
  31. type RawLinkOptions struct {
  32. // File descriptor to attach to. This differs for each attach type.
  33. Target int
  34. // Program to attach.
  35. Program *ebpf.Program
  36. // Attach must match the attach type of Program.
  37. Attach ebpf.AttachType
  38. }
  39. // RawLinkInfo contains metadata on a link.
  40. type RawLinkInfo struct {
  41. Type Type
  42. ID ID
  43. Program ebpf.ProgramID
  44. }
  45. // RawLink is the low-level API to bpf_link.
  46. //
  47. // You should consider using the higher level interfaces in this
  48. // package instead.
  49. type RawLink struct {
  50. fd *internal.FD
  51. }
  52. // AttachRawLink creates a raw link.
  53. func AttachRawLink(opts RawLinkOptions) (*RawLink, error) {
  54. if err := haveBPFLink(); err != nil {
  55. return nil, err
  56. }
  57. if opts.Target < 0 {
  58. return nil, fmt.Errorf("invalid target: %s", internal.ErrClosedFd)
  59. }
  60. progFd := opts.Program.FD()
  61. if progFd < 0 {
  62. return nil, fmt.Errorf("invalid program: %s", internal.ErrClosedFd)
  63. }
  64. attr := bpfLinkCreateAttr{
  65. targetFd: uint32(opts.Target),
  66. progFd: uint32(progFd),
  67. attachType: opts.Attach,
  68. }
  69. fd, err := bpfLinkCreate(&attr)
  70. if err != nil {
  71. return nil, fmt.Errorf("can't create link: %s", err)
  72. }
  73. return &RawLink{fd}, nil
  74. }
  75. // LoadPinnedRawLink loads a persisted link from a bpffs.
  76. func LoadPinnedRawLink(fileName string) (*RawLink, error) {
  77. return loadPinnedRawLink(fileName, UnspecifiedType)
  78. }
  79. func loadPinnedRawLink(fileName string, typ Type) (*RawLink, error) {
  80. fd, err := internal.BPFObjGet(fileName)
  81. if err != nil {
  82. return nil, fmt.Errorf("load pinned link: %s", err)
  83. }
  84. link := &RawLink{fd}
  85. if typ == UnspecifiedType {
  86. return link, nil
  87. }
  88. info, err := link.Info()
  89. if err != nil {
  90. link.Close()
  91. return nil, fmt.Errorf("get pinned link info: %s", err)
  92. }
  93. if info.Type != typ {
  94. link.Close()
  95. return nil, fmt.Errorf("link type %v doesn't match %v", info.Type, typ)
  96. }
  97. return link, nil
  98. }
  99. func (l *RawLink) isLink() {}
  100. // FD returns the raw file descriptor.
  101. func (l *RawLink) FD() int {
  102. fd, err := l.fd.Value()
  103. if err != nil {
  104. return -1
  105. }
  106. return int(fd)
  107. }
  108. // Close breaks the link.
  109. //
  110. // Use Pin if you want to make the link persistent.
  111. func (l *RawLink) Close() error {
  112. return l.fd.Close()
  113. }
  114. // Pin persists a link past the lifetime of the process.
  115. //
  116. // Calling Close on a pinned Link will not break the link
  117. // until the pin is removed.
  118. func (l *RawLink) Pin(fileName string) error {
  119. if err := internal.BPFObjPin(fileName, l.fd); err != nil {
  120. return fmt.Errorf("can't pin link: %s", err)
  121. }
  122. return nil
  123. }
  124. // Update implements Link.
  125. func (l *RawLink) Update(new *ebpf.Program) error {
  126. return l.UpdateArgs(RawLinkUpdateOptions{
  127. New: new,
  128. })
  129. }
  130. // RawLinkUpdateOptions control the behaviour of RawLink.UpdateArgs.
  131. type RawLinkUpdateOptions struct {
  132. New *ebpf.Program
  133. Old *ebpf.Program
  134. Flags uint32
  135. }
  136. // UpdateArgs updates a link based on args.
  137. func (l *RawLink) UpdateArgs(opts RawLinkUpdateOptions) error {
  138. newFd := opts.New.FD()
  139. if newFd < 0 {
  140. return fmt.Errorf("invalid program: %s", internal.ErrClosedFd)
  141. }
  142. var oldFd int
  143. if opts.Old != nil {
  144. oldFd = opts.Old.FD()
  145. if oldFd < 0 {
  146. return fmt.Errorf("invalid replacement program: %s", internal.ErrClosedFd)
  147. }
  148. }
  149. linkFd, err := l.fd.Value()
  150. if err != nil {
  151. return fmt.Errorf("can't update link: %s", err)
  152. }
  153. attr := bpfLinkUpdateAttr{
  154. linkFd: linkFd,
  155. newProgFd: uint32(newFd),
  156. oldProgFd: uint32(oldFd),
  157. flags: opts.Flags,
  158. }
  159. return bpfLinkUpdate(&attr)
  160. }
  161. // struct bpf_link_info
  162. type bpfLinkInfo struct {
  163. typ uint32
  164. id uint32
  165. prog_id uint32
  166. }
  167. // Info returns metadata about the link.
  168. func (l *RawLink) Info() (*RawLinkInfo, error) {
  169. var info bpfLinkInfo
  170. err := internal.BPFObjGetInfoByFD(l.fd, unsafe.Pointer(&info), unsafe.Sizeof(info))
  171. if err != nil {
  172. return nil, fmt.Errorf("link info: %s", err)
  173. }
  174. return &RawLinkInfo{
  175. Type(info.typ),
  176. ID(info.id),
  177. ebpf.ProgramID(info.prog_id),
  178. }, nil
  179. }