link.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package link
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "fmt"
  6. "github.com/cilium/ebpf"
  7. "github.com/cilium/ebpf/btf"
  8. "github.com/cilium/ebpf/internal"
  9. "github.com/cilium/ebpf/internal/sys"
  10. )
  11. var ErrNotSupported = internal.ErrNotSupported
  12. // Link represents a Program attached to a BPF hook.
  13. type Link interface {
  14. // Replace the current program with a new program.
  15. //
  16. // Passing a nil program is an error. May return an error wrapping ErrNotSupported.
  17. Update(*ebpf.Program) error
  18. // Persist a link by pinning it into a bpffs.
  19. //
  20. // May return an error wrapping ErrNotSupported.
  21. Pin(string) error
  22. // Undo a previous call to Pin.
  23. //
  24. // May return an error wrapping ErrNotSupported.
  25. Unpin() error
  26. // Close frees resources.
  27. //
  28. // The link will be broken unless it has been successfully pinned.
  29. // A link may continue past the lifetime of the process if Close is
  30. // not called.
  31. Close() error
  32. // Info returns metadata on a link.
  33. //
  34. // May return an error wrapping ErrNotSupported.
  35. Info() (*Info, error)
  36. // Prevent external users from implementing this interface.
  37. isLink()
  38. }
  39. // LoadPinnedLink loads a link that was persisted into a bpffs.
  40. func LoadPinnedLink(fileName string, opts *ebpf.LoadPinOptions) (Link, error) {
  41. raw, err := loadPinnedRawLink(fileName, opts)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return wrapRawLink(raw)
  46. }
  47. // wrap a RawLink in a more specific type if possible.
  48. //
  49. // The function takes ownership of raw and closes it on error.
  50. func wrapRawLink(raw *RawLink) (Link, error) {
  51. info, err := raw.Info()
  52. if err != nil {
  53. raw.Close()
  54. return nil, err
  55. }
  56. switch info.Type {
  57. case RawTracepointType:
  58. return &rawTracepoint{*raw}, nil
  59. case TracingType:
  60. return &tracing{*raw}, nil
  61. case CgroupType:
  62. return &linkCgroup{*raw}, nil
  63. case IterType:
  64. return &Iter{*raw}, nil
  65. case NetNsType:
  66. return &NetNsLink{*raw}, nil
  67. default:
  68. return raw, nil
  69. }
  70. }
  71. // ID uniquely identifies a BPF link.
  72. type ID = sys.LinkID
  73. // RawLinkOptions control the creation of a raw link.
  74. type RawLinkOptions struct {
  75. // File descriptor to attach to. This differs for each attach type.
  76. Target int
  77. // Program to attach.
  78. Program *ebpf.Program
  79. // Attach must match the attach type of Program.
  80. Attach ebpf.AttachType
  81. // BTF is the BTF of the attachment target.
  82. BTF btf.TypeID
  83. // Flags control the attach behaviour.
  84. Flags uint32
  85. }
  86. // Info contains metadata on a link.
  87. type Info struct {
  88. Type Type
  89. ID ID
  90. Program ebpf.ProgramID
  91. extra interface{}
  92. }
  93. type TracingInfo sys.TracingLinkInfo
  94. type CgroupInfo sys.CgroupLinkInfo
  95. type NetNsInfo sys.NetNsLinkInfo
  96. type XDPInfo sys.XDPLinkInfo
  97. // Tracing returns tracing type-specific link info.
  98. //
  99. // Returns nil if the type-specific link info isn't available.
  100. func (r Info) Tracing() *TracingInfo {
  101. e, _ := r.extra.(*TracingInfo)
  102. return e
  103. }
  104. // Cgroup returns cgroup type-specific link info.
  105. //
  106. // Returns nil if the type-specific link info isn't available.
  107. func (r Info) Cgroup() *CgroupInfo {
  108. e, _ := r.extra.(*CgroupInfo)
  109. return e
  110. }
  111. // NetNs returns netns type-specific link info.
  112. //
  113. // Returns nil if the type-specific link info isn't available.
  114. func (r Info) NetNs() *NetNsInfo {
  115. e, _ := r.extra.(*NetNsInfo)
  116. return e
  117. }
  118. // ExtraNetNs returns XDP type-specific link info.
  119. //
  120. // Returns nil if the type-specific link info isn't available.
  121. func (r Info) XDP() *XDPInfo {
  122. e, _ := r.extra.(*XDPInfo)
  123. return e
  124. }
  125. // RawLink is the low-level API to bpf_link.
  126. //
  127. // You should consider using the higher level interfaces in this
  128. // package instead.
  129. type RawLink struct {
  130. fd *sys.FD
  131. pinnedPath string
  132. }
  133. // AttachRawLink creates a raw link.
  134. func AttachRawLink(opts RawLinkOptions) (*RawLink, error) {
  135. if err := haveBPFLink(); err != nil {
  136. return nil, err
  137. }
  138. if opts.Target < 0 {
  139. return nil, fmt.Errorf("invalid target: %s", sys.ErrClosedFd)
  140. }
  141. progFd := opts.Program.FD()
  142. if progFd < 0 {
  143. return nil, fmt.Errorf("invalid program: %s", sys.ErrClosedFd)
  144. }
  145. attr := sys.LinkCreateAttr{
  146. TargetFd: uint32(opts.Target),
  147. ProgFd: uint32(progFd),
  148. AttachType: sys.AttachType(opts.Attach),
  149. TargetBtfId: uint32(opts.BTF),
  150. Flags: opts.Flags,
  151. }
  152. fd, err := sys.LinkCreate(&attr)
  153. if err != nil {
  154. return nil, fmt.Errorf("can't create link: %s", err)
  155. }
  156. return &RawLink{fd, ""}, nil
  157. }
  158. func loadPinnedRawLink(fileName string, opts *ebpf.LoadPinOptions) (*RawLink, error) {
  159. fd, err := sys.ObjGet(&sys.ObjGetAttr{
  160. Pathname: sys.NewStringPointer(fileName),
  161. FileFlags: opts.Marshal(),
  162. })
  163. if err != nil {
  164. return nil, fmt.Errorf("load pinned link: %w", err)
  165. }
  166. return &RawLink{fd, fileName}, nil
  167. }
  168. func (l *RawLink) isLink() {}
  169. // FD returns the raw file descriptor.
  170. func (l *RawLink) FD() int {
  171. return l.fd.Int()
  172. }
  173. // Close breaks the link.
  174. //
  175. // Use Pin if you want to make the link persistent.
  176. func (l *RawLink) Close() error {
  177. return l.fd.Close()
  178. }
  179. // Pin persists a link past the lifetime of the process.
  180. //
  181. // Calling Close on a pinned Link will not break the link
  182. // until the pin is removed.
  183. func (l *RawLink) Pin(fileName string) error {
  184. if err := internal.Pin(l.pinnedPath, fileName, l.fd); err != nil {
  185. return err
  186. }
  187. l.pinnedPath = fileName
  188. return nil
  189. }
  190. // Unpin implements the Link interface.
  191. func (l *RawLink) Unpin() error {
  192. if err := internal.Unpin(l.pinnedPath); err != nil {
  193. return err
  194. }
  195. l.pinnedPath = ""
  196. return nil
  197. }
  198. // Update implements the Link interface.
  199. func (l *RawLink) Update(new *ebpf.Program) error {
  200. return l.UpdateArgs(RawLinkUpdateOptions{
  201. New: new,
  202. })
  203. }
  204. // RawLinkUpdateOptions control the behaviour of RawLink.UpdateArgs.
  205. type RawLinkUpdateOptions struct {
  206. New *ebpf.Program
  207. Old *ebpf.Program
  208. Flags uint32
  209. }
  210. // UpdateArgs updates a link based on args.
  211. func (l *RawLink) UpdateArgs(opts RawLinkUpdateOptions) error {
  212. newFd := opts.New.FD()
  213. if newFd < 0 {
  214. return fmt.Errorf("invalid program: %s", sys.ErrClosedFd)
  215. }
  216. var oldFd int
  217. if opts.Old != nil {
  218. oldFd = opts.Old.FD()
  219. if oldFd < 0 {
  220. return fmt.Errorf("invalid replacement program: %s", sys.ErrClosedFd)
  221. }
  222. }
  223. attr := sys.LinkUpdateAttr{
  224. LinkFd: l.fd.Uint(),
  225. NewProgFd: uint32(newFd),
  226. OldProgFd: uint32(oldFd),
  227. Flags: opts.Flags,
  228. }
  229. return sys.LinkUpdate(&attr)
  230. }
  231. // Info returns metadata about the link.
  232. func (l *RawLink) Info() (*Info, error) {
  233. var info sys.LinkInfo
  234. if err := sys.ObjInfo(l.fd, &info); err != nil {
  235. return nil, fmt.Errorf("link info: %s", err)
  236. }
  237. var extra interface{}
  238. switch info.Type {
  239. case CgroupType:
  240. extra = &CgroupInfo{}
  241. case IterType:
  242. // not supported
  243. case NetNsType:
  244. extra = &NetNsInfo{}
  245. case RawTracepointType:
  246. // not supported
  247. case TracingType:
  248. extra = &TracingInfo{}
  249. case XDPType:
  250. extra = &XDPInfo{}
  251. case PerfEventType:
  252. // no extra
  253. default:
  254. return nil, fmt.Errorf("unknown link info type: %d", info.Type)
  255. }
  256. if info.Type != RawTracepointType && info.Type != IterType && info.Type != PerfEventType {
  257. buf := bytes.NewReader(info.Extra[:])
  258. err := binary.Read(buf, internal.NativeEndian, extra)
  259. if err != nil {
  260. return nil, fmt.Errorf("can not read extra link info: %w", err)
  261. }
  262. }
  263. return &Info{
  264. info.Type,
  265. info.Id,
  266. ebpf.ProgramID(info.ProgId),
  267. extra,
  268. }, nil
  269. }