-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtracer_streaming.go
161 lines (134 loc) · 3.4 KB
/
tracer_streaming.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
package conntracer
/*
#cgo CFLAGS: -I${SRCDIR}/include
#cgo LDFLAGS: -lelf -lz
#include <sys/resource.h>
#include <arpa/inet.h>
#include <errno.h>
#include <bpf/libbpf.h>
#include <bpf/bpf.h>
#include "conntracer_streaming.skel.h"
#include "conntracer.h"
extern int handleFlow(void *ctx, void *data, size_t size);
int libbpf_print_fn(enum libbpf_print_level level,
const char *format, va_list args)
{
// Ignore debug-level libbpf logs
if (level > LIBBPF_INFO) {
return 0;
}
return vfprintf(stderr, format, args);
}
void set_print_fn() {
libbpf_set_print(libbpf_print_fn);
}
struct ring_buffer * new_ring_buf(int map_fd) {
struct ring_buffer *rb = NULL;
rb = ring_buffer__new(map_fd, handleFlow, NULL, NULL);
if (rb < 0) {
fprintf(stderr, "failed to cretae ring buffer!\n");
return NULL;
}
return rb;
}
*/
import "C"
import (
"errors"
"fmt"
"syscall"
"time"
)
const (
// BPFRingbufPollingInterval is an interval of polling events in the ringbuffer.
BPFRingbufPollingInterval = 50 * time.Millisecond
)
// TracerStreaming is an object for state retention without aggregation.
type TracerStreaming struct {
obj *C.struct_conntracer_streaming_bpf
rb *C.struct_ring_buffer
stopChan chan struct{}
statsFd int
}
// NewTracerStreaming loads tracer without aggregation
func NewTracerStreaming(param *TracerParam) (*TracerStreaming, error) {
C.set_print_fn()
// Bump RLIMIT_MEMLOCK to allow BPF sub-system to do anything
if err := bumpMemlockRlimit(); err != nil {
return nil, err
}
obj := C.conntracer_streaming_bpf__open_and_load()
if obj == nil {
return nil, errors.New("failed to open and load BPF object")
}
ret, err := C.conntracer_streaming_bpf__attach(obj)
if ret != 0 {
C.conntracer_streaming_bpf__destroy(obj)
return nil, fmt.Errorf("failed to attach BPF programs: %v", err)
}
// Set up BPF ring buffer polling.
rb := C.new_ring_buf(C.bpf_map__fd(obj.maps.flows))
if rb == nil {
return nil, fmt.Errorf("failed to create ring buffer")
}
t := &TracerStreaming{
obj: obj,
rb: rb,
stopChan: make(chan struct{}),
}
if param.Stats {
fd, err := enableBPFStats()
if err != nil {
return nil, err
}
t.statsFd = fd
}
return t, nil
}
// TODO: sync.Pool
var globalFlowChan chan *Flow
// Start starts loop of polling events from kernel.
func (t *TracerStreaming) Start(fc chan *Flow) error {
globalFlowChan = fc
if err := initializeUDPPortBindingMap(t.udpPortBindingMapFD()); err != nil {
return err
}
tick := time.NewTicker(BPFRingbufPollingInterval)
defer tick.Stop()
for {
select {
case <-t.stopChan:
return nil
case <-tick.C:
n := C.ring_buffer__poll(t.rb, 10 /* timeout, ms */)
if n < 0 {
/* Ctrl-C will cause -EINTR */
if syscall.Errno(-n) == syscall.EINTR {
break
}
return fmt.Errorf("error polling ring buffer: %d", n)
}
}
}
return nil
}
// Stop stop loop of polling events.
func (t *TracerStreaming) Stop() {
t.stopChan <- struct{}{}
}
// Close closes tracer.
func (t *TracerStreaming) Close() {
close(t.stopChan)
if t.statsFd != 0 {
syscall.Close(t.statsFd)
}
C.ring_buffer__free(t.rb)
C.conntracer_streaming_bpf__destroy(t.obj)
}
func (t *TracerStreaming) udpPortBindingMapFD() C.int {
return C.bpf_map__fd(t.obj.maps.udp_port_binding)
}
// GetStats fetches stats of BPF program.
func (t *TracerStreaming) GetStats() (map[int]*BpfProgramStats, error) {
return getBPFAllStats(t.obj.obj)
}