/
program.go
207 lines (167 loc) · 4.75 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
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
package main
import (
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
"github.com/cloudflare/cbpfc"
"github.com/pkg/errors"
"golang.org/x/net/bpf"
"golang.org/x/sys/unix"
)
const BPF_F_CURRENT_CPU int64 = 0xFFFFFFFF
type rawMetrics [3]uint64
type rawMetric int
const (
receivedPackets rawMetric = iota
matchedPackets
perfOutputErrors
)
type metrics struct {
receivedPackets uint64
matchedPackets uint64
perfOutputErrors uint64
}
// map for exporting metrics
var metricsSpec = ebpf.MapSpec{
Name: "xdpcap_metrics",
Type: ebpf.PerCPUArray,
KeySize: 4,
ValueSize: uint32(len(rawMetrics{}) * 8),
MaxEntries: 1,
}
// program represents a filter program for a particular XDP action
type program struct {
program *ebpf.Program
metricsMap *ebpf.Map
}
// newProgram builds an eBPF program that copies packets matching a cBPF program to userspace via perf
func newProgram(filter []bpf.Instruction, action xdpAction, perfMap *ebpf.Map, xdpFragsMode bool) (*program, error) {
metricsMap, err := ebpf.NewMap(&metricsSpec)
if err != nil {
return nil, errors.Wrap(err, "creating metrics map")
}
if perfMap.Type() != ebpf.PerfEventArray {
return nil, errors.Errorf("invalid perf map ABI, expected type %s, have %s", ebpf.PerfEventArray, perfMap.Type())
}
// Labels of blocks
const result = "result"
const exit = "exit"
ebpfFilter, err := cbpfc.ToEBPF(filter, cbpfc.EBPFOpts{
PacketStart: asm.R0,
PacketEnd: asm.R1,
Result: asm.R2,
ResultLabel: result,
Working: [4]asm.Register{asm.R2, asm.R3, asm.R4, asm.R5},
StackOffset: 0,
LabelPrefix: "filter",
})
if err != nil {
return nil, errors.Wrap(err, "converting cBPF to eBPF")
}
insns := asm.Instructions{
// Save ctx
asm.Mov.Reg(asm.R6, asm.R1),
// Get the metrics struct
// map fd
asm.LoadMapPtr(asm.R1, metricsMap.FD()),
// index
asm.Mov.Reg(asm.R2, asm.R10),
asm.Add.Imm(asm.R2, -4),
asm.StoreImm(asm.R2, 0, 0, asm.Word),
// call
asm.FnMapLookupElem.Call(),
// Check metrics aren't nil
asm.JEq.Imm(asm.R0, 0, exit),
// Save metrics
asm.Mov.Reg(asm.R7, asm.R0),
// Packet start
asm.LoadMem(asm.R0, asm.R6, 0, asm.Word),
// Packet end
asm.LoadMem(asm.R1, asm.R6, 4, asm.Word),
// Packet length
asm.Mov.Reg(asm.R8, asm.R1),
asm.Sub.Reg(asm.R8, asm.R0),
// Received packets
asm.LoadMem(asm.R2, asm.R7, int16(8*receivedPackets), asm.DWord),
asm.Add.Imm(asm.R2, 1),
asm.StoreMem(asm.R7, int16(8*receivedPackets), asm.R2, asm.DWord),
// Fall through to filter
}
insns = append(insns, ebpfFilter...)
insns = append(insns,
// Packet didn't match filter
asm.JEq.Imm(asm.R2, 0, exit).Sym(result),
// Matched packets
asm.LoadMem(asm.R0, asm.R7, int16(8*matchedPackets), asm.DWord),
asm.Add.Imm(asm.R0, 1),
asm.StoreMem(asm.R7, int16(8*matchedPackets), asm.R0, asm.DWord),
// Perf output
// ctx
asm.Mov.Reg(asm.R1, asm.R6),
// perf map
asm.LoadMapPtr(asm.R2, perfMap.FD()),
// flags (len << 32 | BPF_F_CURRENT_CPU)
asm.Mov.Reg(asm.R3, asm.R8),
asm.LSh.Imm(asm.R3, 32),
asm.LoadImm(asm.R0, BPF_F_CURRENT_CPU, asm.DWord),
asm.Or.Reg(asm.R3, asm.R0),
// perf output data
asm.Mov.Reg(asm.R4, asm.R10),
// <u64 packet length>
asm.Add.Imm(asm.R4, -8),
asm.StoreMem(asm.R4, 0, asm.R8, asm.DWord),
// <u64 action>
asm.Add.Imm(asm.R4, -8),
asm.StoreImm(asm.R4, 0, int64(action), asm.DWord),
// sizeof(data)
asm.Mov.Imm(asm.R5, 2*8),
// call
asm.FnPerfEventOutput.Call(),
// Perf success
asm.JEq.Imm(asm.R0, 0, exit),
// Perf output errors
asm.LoadMem(asm.R0, asm.R7, int16(8*perfOutputErrors), asm.DWord),
asm.Add.Imm(asm.R0, 1),
asm.StoreMem(asm.R7, int16(8*perfOutputErrors), asm.R0, asm.DWord),
// Fall through to exit
)
// Exit with original action - always referred to
insns = append(insns,
asm.Mov.Imm(asm.R0, int32(action)).Sym(exit),
asm.Return(),
)
progSpec := &ebpf.ProgramSpec{
Name: "xdpcap_filter",
Type: ebpf.XDP,
Instructions: insns,
License: "GPL",
}
if xdpFragsMode {
progSpec.Flags = progSpec.Flags | unix.BPF_F_XDP_HAS_FRAGS
}
prog, err := ebpf.NewProgram(progSpec)
if err != nil {
return nil, errors.Wrap(err, "loading filter")
}
return &program{
program: prog,
metricsMap: metricsMap,
}, nil
}
func (p *program) close() {
p.metricsMap.Close()
p.program.Close()
}
func (p *program) metrics() (metrics, error) {
perCpuMetrics := []rawMetrics{}
err := p.metricsMap.Lookup(uint32(0), &perCpuMetrics)
if err != nil {
return metrics{}, errors.Wrap(err, "accessing metrics map")
}
metrics := metrics{}
for _, cpu := range perCpuMetrics {
metrics.receivedPackets += cpu[receivedPackets]
metrics.matchedPackets += cpu[matchedPackets]
metrics.perfOutputErrors += cpu[perfOutputErrors]
}
return metrics, nil
}