-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
183 lines (157 loc) · 4.45 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
174
175
176
177
178
179
180
181
182
183
package main
import (
"bytes"
"encoding/binary"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/link"
"github.com/cilium/ebpf/ringbuf"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/sys/unix"
)
const (
bpfProgramPath = "./bpf/latency.o"
memLockLimit = 100 * 1024 * 1024 // 100MB
)
type LatencyT struct {
TimestampIn uint64
TimestampOut uint64
Delta uint64
Layer3 L3
}
type L3 struct {
SrcIP uint32
DstIP uint32
HProto uint8
// add padding to match the size of the struct in the BPF program
_ [3]byte
}
var (
PacketsCount = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "packets_count",
Help: "Number of packets received",
},
[]string{"src_ip", "dst_ip"},
)
LatencyHistogram = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "latency_histogram",
Help: "Latency histogram",
Buckets: prometheus.DefBuckets,
},
[]string{"src_ip", "dst_ip"},
)
)
func init() {
prometheus.MustRegister(PacketsCount)
prometheus.MustRegister(LatencyHistogram)
}
func main() {
// Set the RLIMIT_MEMLOCK resource limit
var rLimit unix.Rlimit
rLimit.Cur = memLockLimit
rLimit.Max = memLockLimit
if err := unix.Setrlimit(unix.RLIMIT_MEMLOCK, &rLimit); err != nil {
log.Fatalf("Failed to set RLIMIT_MEMLOCK: %v", err)
}
// Parse the ELF file containing the BPF program
spec, err := ebpf.LoadCollectionSpec(bpfProgramPath)
if err != nil {
log.Fatalf("Failed to load BPF program: %v", err)
}
// Load the BPF program into the kernel
coll, err := ebpf.NewCollection(spec)
if err != nil {
log.Fatalf("Failed to create BPF collection: %v", err)
}
defer coll.Close()
// Attach BPF programs to kprobe receive events
tp_rcv, err := link.Kprobe("ip_rcv", coll.Programs["ip_rcv"], &link.KprobeOptions{})
if err != nil {
log.Fatalf("Failed to attach trace_ip: %v", err)
}
defer tp_rcv.Close()
// Attach BPF programs to kprobe return events
tp_ret, err := link.Kprobe("ip_rcv_finish", coll.Programs["ip_rcv_finish"], &link.KprobeOptions{})
if err != nil {
log.Fatalf("Failed to attach trace_ip_output: %v", err)
}
// Set up ring buffer to read data from BPF program
reader, err := ringbuf.NewReader(coll.Maps["events"])
if err != nil {
log.Fatalf("Failed to get ring: %v", err)
}
// Handle signals for graceful shutdown
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
// Goroutine to handle graceful shutdown on receiving a signal
go func() {
<-sig
tp_rcv.Close()
tp_ret.Close()
coll.Close()
os.Exit(0)
}()
go func() {
// Read and print the output from the eBPF program
var event LatencyT
for {
// Read data from the ring buffer
data, err := reader.Read()
if err != nil {
log.Fatalf("Failed to read from ring buffer: %v", err)
}
if err := binary.Read(bytes.NewReader(data.RawSample), binary.LittleEndian, &event); err != nil {
log.Printf("Failed to parse ring event: %v", err)
continue
}
// Convert IP addresses to string format
srcIP := toIpV4(event.Layer3.SrcIP)
dstIP := toIpV4(event.Layer3.DstIP)
// Increment Prometheus metric
PacketsCount.WithLabelValues(srcIP, dstIP).Inc()
LatencyHistogram.WithLabelValues(srcIP, dstIP).Observe(float64(event.Delta))
// Print the output
fmt.Printf("TimestampIn: %s, TimestampOut: %s, Delta: %d, SrcIP: %s, DstIP: %s, HProto: %s\n", timestampToString(event.TimestampIn), timestampToString(event.TimestampOut), event.Delta, srcIP, dstIP, protoToString(event.Layer3.HProto))
}
}()
// Start Prometheus HTTP server
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":2112", nil))
}
func toIpV4(ip uint32) string {
ipOut := make(net.IP, 4) // Create a 4-byte IP address
binary.LittleEndian.PutUint32(ipOut, ip) // Convert uint32 to byte slice in little-endian order
return ipOut.String() // Convert IP address to string format
}
func protoToString(protocol uint8) string {
switch protocol {
case 1:
return "ICMP"
case 2:
return "IGMP"
case 6:
return "TCP"
case 17:
return "UDP"
case 89:
return "OSPF"
default:
return "Unknown"
}
}
func timestampToString(timestamp uint64) string {
// Convert the timestamp to a time.Time object
t := time.Unix(0, int64(timestamp))
// Format the time.Time object to a human-readable string
return t.Format(time.RFC3339)
}