...
1 package link
2
3 import (
4 "fmt"
5
6 "github.com/cilium/ebpf"
7 "github.com/cilium/ebpf/internal/sys"
8 )
9
10 type RawAttachProgramOptions struct {
11
12 Target int
13
14 Program *ebpf.Program
15
16 Replace *ebpf.Program
17
18 Attach ebpf.AttachType
19
20 Flags uint32
21 }
22
23
24
25
26
27 func RawAttachProgram(opts RawAttachProgramOptions) error {
28 if err := haveProgAttach(); err != nil {
29 return err
30 }
31
32 var replaceFd uint32
33 if opts.Replace != nil {
34 replaceFd = uint32(opts.Replace.FD())
35 }
36
37 attr := sys.ProgAttachAttr{
38 TargetFd: uint32(opts.Target),
39 AttachBpfFd: uint32(opts.Program.FD()),
40 ReplaceBpfFd: replaceFd,
41 AttachType: uint32(opts.Attach),
42 AttachFlags: uint32(opts.Flags),
43 }
44
45 if err := sys.ProgAttach(&attr); err != nil {
46 return fmt.Errorf("can't attach program: %w", err)
47 }
48 return nil
49 }
50
51 type RawDetachProgramOptions struct {
52 Target int
53 Program *ebpf.Program
54 Attach ebpf.AttachType
55 }
56
57
58
59
60
61 func RawDetachProgram(opts RawDetachProgramOptions) error {
62 if err := haveProgAttach(); err != nil {
63 return err
64 }
65
66 attr := sys.ProgDetachAttr{
67 TargetFd: uint32(opts.Target),
68 AttachBpfFd: uint32(opts.Program.FD()),
69 AttachType: uint32(opts.Attach),
70 }
71 if err := sys.ProgDetach(&attr); err != nil {
72 return fmt.Errorf("can't detach program: %w", err)
73 }
74
75 return nil
76 }
77
View as plain text