/
stats.go
101 lines (82 loc) · 2.24 KB
/
stats.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
package ebpf
import (
"github.com/rs/zerolog/log"
)
type UpfXdpActionStatistic struct {
BpfObjects *BpfObjects
}
type UpfCounters struct {
RxArp uint64
RxIcmp uint64
RxIcmp6 uint64
RxIp4 uint64
RxIp6 uint64
RxTcp uint64
RxUdp uint64
RxOther uint64
RxGtpEcho uint64
RxGtpPdu uint64
RxGtpOther uint64
RxGtpUnexp uint64
}
type UpfStatistic struct {
Counters UpfCounters
XdpStats [5]uint64
}
func (current *UpfCounters) Add(new UpfCounters) {
current.RxArp += new.RxArp
current.RxIcmp += new.RxIcmp
current.RxIcmp6 += new.RxIcmp6
current.RxIp4 += new.RxIp4
current.RxIp6 += new.RxIp6
current.RxTcp += new.RxTcp
current.RxUdp += new.RxUdp
current.RxOther += new.RxOther
current.RxGtpEcho += new.RxGtpEcho
current.RxGtpPdu += new.RxGtpPdu
current.RxGtpOther += new.RxGtpOther
}
// Getters for the upf_xdp_statistic (xdp_action)
func (stat *UpfXdpActionStatistic) getUpfXdpStatisticField(field uint32) uint64 {
var statistics []IpEntrypointUpfStatistic
err := stat.BpfObjects.UpfExtStat.Lookup(uint32(0), &statistics)
if err != nil {
log.Info().Msg(err.Error())
return 0
}
var totalValue uint64 = 0
for _, statistic := range statistics {
totalValue += statistic.XdpActions[field]
}
return totalValue
}
func (stat *UpfXdpActionStatistic) GetAborted() uint64 {
return stat.getUpfXdpStatisticField(uint32(0))
}
func (stat *UpfXdpActionStatistic) GetDrop() uint64 {
return stat.getUpfXdpStatisticField(uint32(1))
}
func (stat *UpfXdpActionStatistic) GetPass() uint64 {
return stat.getUpfXdpStatisticField(uint32(2))
}
func (stat *UpfXdpActionStatistic) GetTx() uint64 {
return stat.getUpfXdpStatisticField(uint32(3))
}
func (stat *UpfXdpActionStatistic) GetRedirect() uint64 {
return stat.getUpfXdpStatisticField(uint32(4))
}
// Getters for the upf_ext_stat (upf_counters)
// #TODO: Do not retrieve the whole struct each time.
func (stat *UpfXdpActionStatistic) GetUpfExtStatField() UpfCounters {
var statistics []IpEntrypointUpfStatistic
var counters UpfCounters
err := stat.BpfObjects.UpfExtStat.Lookup(uint32(0), &statistics)
if err != nil {
log.Info().Msg(err.Error())
return counters
}
for _, statistic := range statistics {
counters.Add(statistic.UpfCounters)
}
return counters
}