-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.go
300 lines (262 loc) · 5.67 KB
/
data.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package main
import (
"encoding/json"
"math"
"net"
"sync"
"time"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
)
// Data holds the information obtained during capture.
type Data struct {
sync.Mutex
IP IPData
Meta MetaData `json:"-"`
TCP4 map[TCP4FlowKey]*TCPFlowData `json:"-"`
TCP6 map[TCP6FlowKey]*TCPFlowData `json:"-"`
}
func NewData() *Data {
return &Data{
TCP4: make(map[TCP4FlowKey]*TCPFlowData),
TCP6: make(map[TCP6FlowKey]*TCPFlowData),
}
}
type IPData struct {
Packets uint64
Bytes uint64
}
type MetaData struct {
ParseStartTime time.Time
ParseEndTime time.Time
CaptureStartTime time.Time
CaptureEndTime time.Time
PCAPStats *pcap.Stats `json:",omitempty"`
}
type TCPOneWayData struct {
Initialized bool `json:"-"`
FinSeen bool `json:"-"`
CE uint64
SCE uint64
ESCE uint64
ECE uint64
CWR uint64
Segments uint64
DataSegments uint64
Acks uint64
AckedBytes uint64
SackedBytes uint64
ESCEAckedBytes uint64
DuplicateAcks uint64
Gaps uint64
GapBytes uint64
LateSegments uint64
RetransmittedSegments uint64
FirstAckTime time.Time
LastAckTime time.Time
PriorPacketTime time.Time `json:"-"`
PriorSCETime time.Time `json:"-"`
SCERunCount uint `json:"-"`
SCERunLength Float64Data
IPG DurationData
SCEIPG DurationData
SeqTimes map[uint32]time.Time `json:"-"`
SeqRTT DurationData
TSValTimes map[uint32]time.Time `json:"-"`
TSValRTT DurationData
SackedBytesCtr uint32 `json:"-"`
PriorAck uint32 `json:"-"`
ExpSeq uint32 `json:"-"`
HiTSVal uint32 `json:"-"`
}
func NewTCPOneWayData() *TCPOneWayData {
return &TCPOneWayData{
TSValTimes: make(map[uint32]time.Time),
SeqTimes: make(map[uint32]time.Time),
}
}
type TCPFlowData struct {
Index int `json:"-"`
SrcIP net.IP
SrcPort layers.TCPPort
DstIP net.IP
DstPort layers.TCPPort
ECNInitiated bool
ECNAccepted bool
Up *TCPOneWayData
Down *TCPOneWayData
}
type TCP4FlowKey struct {
SrcIP [4]byte
SrcPort layers.TCPPort
DstIP [4]byte
DstPort layers.TCPPort
}
func (k TCP4FlowKey) Reverse() TCP4FlowKey {
return TCP4FlowKey{k.DstIP, k.DstPort, k.SrcIP, k.SrcPort}
}
type TCP6FlowKey struct {
SrcIP [16]byte
SrcPort layers.TCPPort
DstIP [16]byte
DstPort layers.TCPPort
}
func (k TCP6FlowKey) Reverse() TCP6FlowKey {
return TCP6FlowKey{k.DstIP, k.DstPort, k.SrcIP, k.SrcPort}
}
// DurationData records min, max, mean and variance for a time.Duration.
type DurationData struct {
N uint64
Min time.Duration
Max time.Duration
m float64
s float64
mean float64
}
func (u *DurationData) Push(d time.Duration) {
if u.N == 0 {
u.Min = d
u.Max = d
} else {
if d < u.Min {
u.Min = d
}
if d > u.Max {
u.Max = d
}
}
u.N++
om := u.mean
fd := float64(d)
u.mean += (fd - om) / float64(u.N)
u.s += (fd - om) * (fd - u.mean)
}
func (d *DurationData) IsZero() bool {
return d.N == 0
}
func (d *DurationData) Mean() time.Duration {
return time.Duration(d.mean)
}
func (d *DurationData) Variance() float64 {
if d.N > 1 {
return d.s / float64(d.N-1)
}
return 0
}
func (d *DurationData) Burstiness() float64 {
if d.mean != 0 {
return d.Variance() / d.mean
}
return 0
}
func (d *DurationData) Stddev() time.Duration {
return time.Duration(math.Sqrt(d.Variance()))
}
func (d *DurationData) MarshalJSON() ([]byte, error) {
type DurationDataJSON struct {
N uint64
Min float64
Max float64
Mean float64
Stddev float64
Variance float64
Burstiness float64
}
if d.N == 0 {
return json.Marshal(struct{}{})
}
j := DurationDataJSON{
d.N,
durToMs(d.Min),
durToMs(d.Max),
nsToMs(d.mean),
nsToMs(math.Sqrt(d.Variance())),
nsToMs(nsToMs(d.Variance())),
nsToMs(d.Burstiness()),
}
return json.Marshal(j)
}
func nsToMs(ns float64) float64 {
return ns / 1000000
}
func durToMs(d time.Duration) float64 {
return nsToMs(float64(d.Nanoseconds()))
}
// Float64Data records min, max, mean and variance for a float64.
type Float64Data struct {
N uint64
Min float64
Max float64
m float64
s float64
mean float64
}
func (d *Float64Data) Push(f float64) {
if d.N == 0 {
d.Min = f
d.Max = f
} else {
if f < d.Min {
d.Min = f
}
if f > d.Max {
d.Max = f
}
}
d.N++
om := d.mean
fd := f
d.mean += (fd - om) / float64(d.N)
d.s += (fd - om) * (fd - d.mean)
}
func (d *Float64Data) IsZero() bool {
return d.N == 0
}
func (d *Float64Data) Mean() float64 {
return d.mean
}
func (d *Float64Data) Variance() float64 {
if d.N > 1 {
return d.s / float64(d.N-1)
}
return 0
}
func (d *Float64Data) Burstiness() float64 {
if d.mean != 0 {
return d.Variance() / d.mean
}
return 0
}
func (d *Float64Data) Stddev() float64 {
return math.Sqrt(d.Variance())
}
func (d *Float64Data) MarshalJSON() ([]byte, error) {
type Float64DataJSON struct {
N uint64
Min float64
Max float64
Mean float64
Stddev float64
Variance float64
Burstiness float64
}
if d.N == 0 {
return json.Marshal(struct{}{})
}
j := Float64DataJSON{
d.N,
d.Min,
d.Max,
d.Mean(),
d.Stddev(),
d.Variance(),
d.Burstiness(),
}
return json.Marshal(j)
}
// Gap stores a hole in the received packets.
type Gap struct {
Seq uint32
EndSeq uint32
TSVal uint32
}