-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprogram.go
135 lines (110 loc) · 2.85 KB
/
program.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package gobpfld
import (
"errors"
"fmt"
"regexp"
"github.com/dylandreimerink/gobpfld/bpfsys"
"github.com/dylandreimerink/gobpfld/bpftypes"
)
func NewBPFProgram(progType bpftypes.BPFProgType) BPFProgram {
ap := NewAbstractBPFProgram()
ap.ProgramType = progType
return BPFProgramFromAbstract(ap)
}
func NewAbstractBPFProgram() AbstractBPFProgram {
return AbstractBPFProgram{
MapFDLocations: make(map[string][]uint64),
Maps: make(map[string]BPFMap),
}
}
func BPFProgramFromAbstract(abstract AbstractBPFProgram) BPFProgram {
switch abstract.ProgramType {
case bpftypes.BPF_PROG_TYPE_XDP:
return &ProgramXDP{
AbstractBPFProgram: abstract,
}
case bpftypes.BPF_PROG_TYPE_SOCKET_FILTER:
return &ProgramSocketFilter{
AbstractBPFProgram: abstract,
}
case bpftypes.BPF_PROG_TYPE_TRACEPOINT:
return &ProgramTracepoint{
AbstractBPFProgram: abstract,
}
case bpftypes.BPF_PROG_TYPE_KPROBE:
return &ProgramKProbe{
AbstractBPFProgram: abstract,
}
default:
panic("unsupported program type")
}
}
type BPFProgram interface {
Fd() (bpfsys.BPFfd, error)
Pin(relativePath string) error
Unpin(relativePath string, deletePin bool) error
GetAbstractProgram() AbstractBPFProgram
}
type ObjName struct {
str string
cname [bpftypes.BPF_OBJ_NAME_LEN]byte
}
func MustNewObjName(initialName string) ObjName {
objN, err := NewObjName(initialName)
if err != nil {
panic(err)
}
return *objN
}
func NewObjName(initialName string) (*ObjName, error) {
on := &ObjName{}
return on, on.SetString(initialName)
}
// ErrObjNameToLarge is returned when a given string or byte slice is to large.
// The kernel limits names to 15 usable bytes plus a null-termination char
var ErrObjNameToLarge = errors.New("object name to large")
func (on *ObjName) SetBytes(strBytes []byte) error {
if len(strBytes) > bpftypes.BPF_OBJ_NAME_LEN-1 {
return fmt.Errorf(
"%w: limit is %d bytes, length: %d",
ErrObjNameToLarge,
bpftypes.BPF_OBJ_NAME_LEN-1,
len(strBytes),
)
}
on.str = string(strBytes)
for i := 0; i < bpftypes.BPF_OBJ_NAME_LEN-1; i++ {
if len(strBytes) > i {
on.cname[i] = strBytes[i]
continue
}
on.cname[i] = 0x00
}
return nil
}
// https://elixir.bootlin.com/linux/v5.12.10/source/kernel/bpf/syscall.c#L719
var objNameRegexp = regexp.MustCompile(`^[a-zA-Z0-9_\.]{1,15}$`)
func (on *ObjName) SetString(str string) error {
if len(str) > 15 {
return ErrObjNameToLarge
}
if !objNameRegexp.MatchString(str) {
return fmt.Errorf("object name must be 1 to 15 alpha numeric, '_', or '.' chars")
}
strBytes := []byte(str)
on.str = str
for i := 0; i < bpftypes.BPF_OBJ_NAME_LEN-1; i++ {
if len(strBytes) > i {
on.cname[i] = strBytes[i]
continue
}
on.cname[i] = 0x00
}
return nil
}
func (on *ObjName) GetCstr() [bpftypes.BPF_OBJ_NAME_LEN]byte {
return on.cname
}
func (on *ObjName) String() string {
return on.str
}