/
main.go
173 lines (138 loc) · 4.4 KB
/
main.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
package main
import (
"bytes"
"embed"
"fmt"
"os"
"os/signal"
"runtime"
"time"
"github.com/dylandreimerink/gobpfld"
"github.com/dylandreimerink/gobpfld/bpftypes"
"golang.org/x/sys/unix"
)
//go:embed src/xdp
var f embed.FS
func main() {
elfFileBytes, err := f.ReadFile("src/xdp")
if err != nil {
fmt.Fprintf(os.Stderr, "error opening ELF file: %s\n", err.Error())
os.Exit(1)
}
elf, err := gobpfld.LoadProgramFromELF(bytes.NewReader(elfFileBytes), gobpfld.ELFParseSettings{})
if err != nil {
fmt.Fprintf(os.Stderr, "error while reading ELF file: %s\n", err.Error())
os.Exit(1)
}
tailProgs := []string{"ipv4_prog", "ipv6_prog", "tcp_prog", "udp_prog"}
entryProgram := elf.Programs["entry_prog"].(*gobpfld.ProgramXDP)
log, err := entryProgram.Load(gobpfld.ProgXDPLoadOpts{
VerifierLogLevel: bpftypes.BPFLogLevelVerbose,
})
fmt.Printf("BPF Verifier log:\n%s\n", log)
if err != nil {
fmt.Fprintf(os.Stderr, "error while loading program 'entry': %s\n", err.Error())
os.Exit(1)
}
// Before we attach the entry program to the network device we load all tail programs and set their fd's
// in the 'tails' map.
tailMap := elf.Maps["tails"].(*gobpfld.ProgArrayMap)
for i, progName := range tailProgs {
tailProg := elf.Programs[progName].(*gobpfld.ProgramXDP)
log, err := tailProg.Load(gobpfld.ProgXDPLoadOpts{
VerifierLogLevel: bpftypes.BPFLogLevelVerbose,
})
fmt.Printf("BPF Verifier log:\n%s\n", log)
if err != nil {
fmt.Fprintf(os.Stderr, "error while loading program '%s': %s\n", progName, err.Error())
os.Exit(1)
}
err = tailMap.Set(int32(i), tailProg)
if err != nil {
fmt.Fprintf(os.Stderr, "error while setting prog array fd '%s': %s\n", progName, err.Error())
os.Exit(1)
}
}
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, unix.SIGTERM, unix.SIGINT)
err = entryProgram.Attach(gobpfld.ProgXDPAttachOpts{
InterfaceName: "lo",
Replace: true,
})
if err != nil {
fmt.Fprintf(os.Stderr, "error while attaching program to loopback device: %s\n", err.Error())
os.Exit(1)
}
// We need to know how many CPU's this machine has to size our value buffers correctly
// runtime.NumCPU returns the usable number of CPU's for this process.
// This number can be different from the number available to the kernel if this process
// has custom CPU affinity / scheduling. To avoid this the /proc/cpuinfo "file" should be
// parsed which seems the most reliable method for CPU count detection.
// But this is not (yet) included in gobpfld.
numCPUs := runtime.NumCPU()
ipStats := elf.Maps["ip_proto_stats"].(*gobpfld.HashMap)
tcpStats := elf.Maps["tcp_stats"].(*gobpfld.HashMap)
udpStats := elf.Maps["udp_stats"].(*gobpfld.HashMap)
type trafficStats struct {
packets uint64
bytes uint64
}
ticker := time.Tick(1 * time.Second)
for {
select {
case <-ticker:
printStats := func(k, v interface{}) error {
var sum trafficStats
for _, row := range *v.(*[]trafficStats) {
sum.bytes += row.bytes
sum.packets += row.packets
}
var key interface{}
switch t := k.(type) {
case *uint8:
key = *t
case *uint16:
key = *t
}
fmt.Printf(" %d: pkts: %d, bytes: %d\n", key, sum.packets, sum.bytes)
return nil
}
var (
protoNum uint8
portNum uint16
)
stats := make([]trafficStats, numCPUs)
fmt.Println("-------------------------")
fmt.Println("IP Proto stats:")
iter := ipStats.Iterator()
// gobpfld.MapIterForEach(ipStats.Iterator(), &protoNum, &stats, printStats)
err := iter.Init(&protoNum, &stats)
if err != nil {
fmt.Fprintf(os.Stderr, "init: %s\n", err.Error())
continue
}
var updated bool
for updated, err = iter.Next(); updated && err == nil; updated, err = iter.Next() {
err = printStats(&protoNum, &stats)
if err != nil {
fmt.Fprintf(os.Stderr, "printStats: %s\n", err.Error())
continue
}
}
fmt.Println("TCP Proto stats:")
gobpfld.MapIterForEach(tcpStats.Iterator(), &portNum, &stats, printStats)
fmt.Println("UDP Proto stats:")
gobpfld.MapIterForEach(udpStats.Iterator(), &portNum, &stats, printStats)
case <-sigChan:
fmt.Println("Detaching XPD program and stopping")
err = entryProgram.XDPLinkDetach(gobpfld.BPFProgramXDPLinkDetachSettings{
All: true,
})
if err != nil {
fmt.Fprintf(os.Stderr, "error while detaching program: %s\n", err.Error())
os.Exit(1)
}
os.Exit(0)
}
}
}