-
Notifications
You must be signed in to change notification settings - Fork 2
/
prog.go
322 lines (282 loc) · 8.33 KB
/
prog.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright 2018 Andrei Tudor Călin
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package ebpf
import (
"errors"
"unsafe"
"golang.org/x/sys/unix"
)
// ProgType is the type of an eBPF program.
type ProgType uint32
// Valid eBPF program types.
const (
ProgTypeUnspec ProgType = iota
ProgTypeSocketFilter
ProgTypeKProbe
ProgTypeSchedCLS
ProgTypeSchedACT
ProgTypeTracepoint
ProgTypeXDP
ProgTypePerfEvent
ProgTypeCGroupSKB
ProgTypeCGroupSock
ProgTypeLWTIn
ProgTypeLWTOut
ProgTypeLWTXMit
ProgTypeSockOps
ProgTypeSKSKB
ProgTypeCGroupDevice
ProgTypeSKMsg
ProgTypeRawTracepoint
ProgTypeCGroupSockAddr
ProgTypeLWTSeg6Local
ProgTypeLIRCMode2
ProgTypeSKReusePort
)
// AttachType describes the attach type of an eBPF program.
type AttachType uint32
// Valid program attach types.
const (
AttachTypeCGroupInetIngress AttachType = iota
AttachTypeCGroupInetEgress
AttachTypeCGroupInetSockCreate
AttachTypeCGroupSockOps
AttachTypeSKSKBStreamParser
AttachTypeSKSKBStreamVerdict
AttachTypeCGroupDevice
AttachTypeSKMsgVerdict
AttachTypeCGroupInet4Bind
AttachTypeCGroupInet6Bind
AttachTypeCGroupInet4Connect
AttachTypeCGroupInet6Connect
AttachTypeCGroupInet4PostBind
AttachTypeCGroupInet6PostBind
AttachTypeCGroupUDP4SendMsg
AttachTypeCGroupUDP6SendMsg
AttachTypeLIRCMode2
)
// Prog configures an eBPF program.
type Prog struct {
Type ProgType
License string
KernelVersion uint32
StrictAlignment bool
ObjectName string
IfIndex uint32
ExpectedAttachType AttachType
pfd *progFD
}
// defaultLogBufSize is the log buffer size used by the Linux tools.
// See tools/lib/bpf.h in the Linux kernel source tree. We use it as-is.
// Perhaps it will be configurable one day.
//
// TODO(acln): configurable?
const defaultLogBufSize = 256 * 1024
// BPF_PROG_LOAD flags.
const loadStrictAlignment = 1 << 0
// CGroupAttachFlag is a flag for an AttachCGroup operation.
type CGroupAttachFlag uint32
// cgroup attach flags.
const (
// CGroupAttachAllowNone allows no further bpf programs in the target
// cgroup sub-tree.
CGroupAttachAllowNone CGroupAttachFlag = 0
// CGroupAttachAllowOverride arranges for the program in this cgroup
// to yield to programs installed by sub-cgroups.
CGroupAttachAllowOverride CGroupAttachFlag = 1 << 0
// CGroupAttachAllowMulti arranges for the program in this cgroup
// to run in addition to programs installed by sub-cgroups.
CGroupAttachAllowMulti CGroupAttachFlag = 1 << 1
)
// Load attaches the specified InstructionStream to the Prog
// and loads the program into the kernel.
//
// If the specified InstructionStream uses symbols, all symbols must
// be resolved before calling Load.
//
// If loading the program produces output from the eBPF kernel verifier,
// the output is returned in the log string.
func (p *Prog) Load(s *InstructionStream) (log string, err error) {
if s.empty() {
return "", errors.New("ebpf: empty instruction stream")
}
if s.hasUnresolvedSymbols() {
return "", errors.New("ebpf: unresolved symbols in instruction stream")
}
insns := s.instructions()
logbuf := make([]byte, defaultLogBufSize)
attr := progLoadAttr{
Type: p.Type,
InstructionCount: uint32(len(insns)),
Instructions: iptr(insns),
License: bptr(nullTerminatedString(p.License)),
LogLevel: 1,
LogBufSize: uint32(len(logbuf)),
LogBuf: bptr(logbuf),
KernelVersion: p.KernelVersion,
Name: newObjectName(p.ObjectName),
IfIndex: p.IfIndex,
ExpectedAttachType: p.ExpectedAttachType,
}
if p.StrictAlignment {
attr.Flags = loadStrictAlignment
}
pfd := new(progFD)
err = pfd.Init(&attr)
for i := 0; i < len(logbuf); i++ {
if logbuf[i] == 0 {
log = string(logbuf[:i])
break
}
}
if err != nil {
return log, err
}
p.pfd = pfd
return log, nil
}
// Socket represents a socket an eBPF program can be attached to.
//
// Note that implementations of syscall.RawConn also satisfy Socket.
type Socket interface {
Control(fn func(fd uintptr)) error
}
// RawSocketFD is an implementation of Socket that uses a raw file descriptor.
type RawSocketFD int
// Control calls fn on raw. It always returns nil.
func (raw RawSocketFD) Control(fn func(fd uintptr)) error {
fn(uintptr(raw))
return nil
}
var errProgNotLoaded = errors.New("ebpf: program not loaded")
// AttachToSocket attaches the program to a socket.
//
// It sets the SO_ATTACH_BPF option, at the SOL_SOCKET level.
func (p *Prog) AttachToSocket(sock Socket) error {
if p.pfd == nil {
return errProgNotLoaded
}
var err error
cerr := sock.Control(func(fd uintptr) {
err = p.pfd.AttachToSocket(int(fd))
})
if cerr != nil {
return cerr
}
return err
}
// AttachToCGroup attaches the program to a control group.
//
// TODO(acln): implement this
func (p *Prog) AttachToCGroup(fd int, typ AttachType, flag CGroupAttachFlag) error {
return errNotImplemented
}
// DetachFromSocket detaches the program from the specified socket.
func (p *Prog) DetachFromSocket(sock Socket) error {
if p.pfd == nil {
return errProgNotLoaded
}
var err error
cerr := sock.Control(func(fd uintptr) {
err = p.pfd.DetachFromSocket(int(fd))
})
if cerr != nil {
return cerr
}
return err
}
// TestRun specifies a test run for an eBPF program.
type TestRun struct {
// Input contains the input for the eBPF program.
Input []byte
// Output is the memory area where the output of the
// program will be stored.
//
// TODO(acln): document the ENOSPC
Output []byte
// Repeat configures the number of times the program is to be
// executed. The default value of 0 means one execution.
Repeat uint32
}
// TestResults holds the results of a test run.
type TestResults struct {
// ReturnValue is the return value of the eBPF program.
ReturnValue uint32
// Duration is the total execution time, in nanoseconds.
Duration uint32
// Output is the output slice. It aliases TestRun.Output, but its
// length is set to the length returned by the kernel.
Output []byte
// TestRun is the associated test run configuration.
TestRun TestRun
}
// DoTestRun executes a test run of the program.
func (p *Prog) DoTestRun(tr TestRun) (*TestResults, error) {
if p.pfd == nil {
return nil, errProgNotLoaded
}
return p.pfd.DoTestRun(tr)
}
// Unload unloads the program from the kernel and releases the associated
// file descriptor.
func (p *Prog) Unload() error {
if p.pfd == nil {
return errProgNotLoaded
}
return p.pfd.Close()
}
// progFD is a low level wrapper around a bpf program file descriptor.
type progFD struct {
bfd bpfFD
}
func (pfd *progFD) Init(attr *progLoadAttr) error {
rawfd, err := loadProg(attr)
if err != nil {
return wrapCmdError(cmdProgLoad, err)
}
if err := pfd.bfd.Init(rawfd, unix.Close); err != nil {
return err
}
// TODO(acln): what do we do about the attach type?
return nil
}
func (pfd *progFD) AttachToSocket(sockfd int) error {
return pfd.bfd.ProgAttach(sockfd, unix.SOL_SOCKET)
}
func (pfd *progFD) DetachFromSocket(sockfd int) error {
return pfd.bfd.ProgDetach(sockfd, unix.SOL_SOCKET)
}
func (pfd *progFD) DoTestRun(tr TestRun) (*TestResults, error) {
return pfd.bfd.ProgTestRun(tr)
}
func (pfd *progFD) Close() error {
return pfd.bfd.Close()
}
type progLoadAttr struct {
Type ProgType
InstructionCount uint32
Instructions u64ptr
License u64ptr // pointer to null-terminated string
LogLevel uint32
LogBufSize uint32
LogBuf u64ptr
KernelVersion uint32
Flags uint32
Name objectName
IfIndex uint32
ExpectedAttachType AttachType
}
func loadProg(attr *progLoadAttr) (int, error) {
return bpf(cmdProgLoad, unsafe.Pointer(attr), unsafe.Sizeof(*attr))
}