forked from skydive-project/skydive
-
Notifications
You must be signed in to change notification settings - Fork 1
/
statistics.go
376 lines (336 loc) · 11.1 KB
/
statistics.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package flow
import (
"bytes"
"crypto/sha1"
"encoding/binary"
"errors"
"fmt"
"net"
"strconv"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
func (x FlowEndpointType) Value() int32 {
return int32(x)
}
func Var8bin(v []byte) []byte {
r := make([]byte, 8)
skip := 8 - len(v)
for i, b := range v {
r[i+skip] = b
}
return r
}
func HashFromValues(ab interface{}, ba interface{}) []byte {
var vab, vba uint64
var binab, binba []byte
hasher := sha1.New()
switch ab.(type) {
case net.HardwareAddr:
binab = ab.(net.HardwareAddr)
binba = ba.(net.HardwareAddr)
vab = binary.BigEndian.Uint64(Var8bin(binab))
vba = binary.BigEndian.Uint64(Var8bin(binba))
case net.IP:
// IP can be IPV4 or IPV6
binab = ab.(net.IP).To16()
binba = ba.(net.IP).To16()
vab = binary.BigEndian.Uint64(Var8bin(binab[:8]))
vba = binary.BigEndian.Uint64(Var8bin(binba[:8]))
if vab == vba {
vab = binary.BigEndian.Uint64(Var8bin(binab[8:]))
vba = binary.BigEndian.Uint64(Var8bin(binba[8:]))
}
case layers.TCPPort:
binab = make([]byte, 2)
binba = make([]byte, 2)
binary.BigEndian.PutUint16(binab, uint16(ab.(layers.TCPPort)))
binary.BigEndian.PutUint16(binba, uint16(ba.(layers.TCPPort)))
vab = uint64(ab.(layers.TCPPort))
vba = uint64(ba.(layers.TCPPort))
case layers.UDPPort:
binab = make([]byte, 2)
binba = make([]byte, 2)
binary.BigEndian.PutUint16(binab, uint16(ab.(layers.UDPPort)))
binary.BigEndian.PutUint16(binba, uint16(ba.(layers.UDPPort)))
vab = uint64(ab.(layers.UDPPort))
vba = uint64(ba.(layers.UDPPort))
case layers.SCTPPort:
binab = make([]byte, 2)
binba = make([]byte, 2)
binary.BigEndian.PutUint16(binab, uint16(ab.(layers.SCTPPort)))
binary.BigEndian.PutUint16(binba, uint16(ba.(layers.SCTPPort)))
vab = uint64(ab.(layers.SCTPPort))
vba = uint64(ba.(layers.SCTPPort))
}
if vab < vba {
hasher.Write(binab)
hasher.Write(binba)
} else {
hasher.Write(binba)
hasher.Write(binab)
}
return hasher.Sum(nil)
}
func (fs *FlowStatistics) Init(now int64, packet *gopacket.Packet, length uint64) {
fs.Start = now
fs.Last = now
fs.Endpoints = []*FlowEndpointsStatistics{}
payloadLen, err := fs.newLinkLayerEndpointStatistics(packet, length)
if err != nil {
return
}
payloadLen, err = fs.newNetworkLayerEndpointStatistics(packet, payloadLen)
if err != nil {
return
}
_, err = fs.newTransportLayerEndpointStatistics(packet, payloadLen)
if err != nil {
return
}
}
func (fs *FlowStatistics) Update(now int64, packet *gopacket.Packet, length uint64) {
fs.Last = now
payloadLen, err := fs.updateLinkLayerStatistics(packet, length)
if err != nil {
return
}
// TODO MPLS ?
payloadLen, err = fs.updateNetworkLayerStatistics(packet, payloadLen)
if err != nil {
return
}
_, err = fs.updateTransportLayerStatistics(packet, payloadLen)
if err != nil {
return
}
}
func (fs *FlowStatistics) DumpInfo(layerSeparator ...string) string {
sep := " | "
if len(layerSeparator) > 0 {
sep = layerSeparator[0]
}
buf := bytes.NewBufferString("")
for _, ep := range fs.Endpoints {
buf.WriteString(fmt.Sprintf("%s\t", ep.Type))
buf.WriteString(fmt.Sprintf("(%d %d)", ep.AB.Packets, ep.AB.Bytes))
buf.WriteString(fmt.Sprintf(" (%d %d)", ep.BA.Packets, ep.BA.Bytes))
buf.WriteString(fmt.Sprintf("\t(%s -> %s)%s", ep.AB.Value, ep.BA.Value, sep))
}
return buf.String()
}
func (fs *FlowStatistics) GetEndpointsType(eptype FlowEndpointType) *FlowEndpointsStatistics {
for _, ep := range fs.Endpoints {
if ep.Type == eptype {
return ep
}
}
return nil
}
func (fs *FlowStatistics) GetLayerHash(ltype FlowEndpointType) (hash []byte) {
e := fs.GetEndpointsType(ltype)
if e == nil {
return
}
return e.Hash
}
func (fs *FlowStatistics) newLinkLayerEndpointStatistics(packet *gopacket.Packet, length uint64) (uint64, error) {
ep := &FlowEndpointsStatistics{}
ep.AB = &FlowEndpointStatistics{}
ep.BA = &FlowEndpointStatistics{}
ethernetLayer := (*packet).Layer(layers.LayerTypeEthernet)
ethernetPacket, ok := ethernetLayer.(*layers.Ethernet)
if !ok {
return 0, errors.New("Unable to decode the ethernet layer")
}
ep.Type = FlowEndpointType_ETHERNET
ep.AB.Value = ethernetPacket.SrcMAC.String()
ep.BA.Value = ethernetPacket.DstMAC.String()
ep.Hash = HashFromValues(ethernetPacket.SrcMAC, ethernetPacket.DstMAC)
fs.Endpoints = append(fs.Endpoints, ep)
return fs.updateLinkLayerStatistics(packet, length)
}
func (fs *FlowStatistics) updateLinkLayerStatistics(packet *gopacket.Packet, length uint64) (uint64, error) {
ep := fs.Endpoints[FlowEndpointLayer_LINK]
ethernetLayer := (*packet).Layer(layers.LayerTypeEthernet)
ethernetPacket, ok := ethernetLayer.(*layers.Ethernet)
if !ok {
return 0, errors.New("Unable to decode the ethernet layer")
}
var e *FlowEndpointStatistics
if ep.AB.Value == ethernetPacket.SrcMAC.String() {
e = ep.AB
} else {
e = ep.BA
}
e.Packets += uint64(1)
// if the length is given use it as the packet can be truncated like in SFlow
if length == 0 {
if ethernetPacket.Length > 0 { // LLC
length = 14 + uint64(ethernetPacket.Length)
} else {
length = 14 + uint64(len(ethernetPacket.Payload))
}
}
e.Bytes += length
return length - 14, nil
}
func (fs *FlowStatistics) newNetworkLayerEndpointStatistics(packet *gopacket.Packet, length uint64) (uint64, error) {
ep := &FlowEndpointsStatistics{}
ep.AB = &FlowEndpointStatistics{}
ep.BA = &FlowEndpointStatistics{}
ipv4Layer := (*packet).Layer(layers.LayerTypeIPv4)
if ipv4Packet, ok := ipv4Layer.(*layers.IPv4); ok {
ep.Type = FlowEndpointType_IPV4
ep.AB.Value = ipv4Packet.SrcIP.String()
ep.BA.Value = ipv4Packet.DstIP.String()
ep.Hash = HashFromValues(ipv4Packet.SrcIP, ipv4Packet.DstIP)
fs.Endpoints = append(fs.Endpoints, ep)
return fs.updateNetworkLayerStatistics(packet, length)
}
ipv6Layer := (*packet).Layer(layers.LayerTypeIPv6)
if ipv6Packet, ok := ipv6Layer.(*layers.IPv6); ok {
ep.Type = FlowEndpointType_IPV6
ep.AB.Value = ipv6Packet.SrcIP.String()
ep.BA.Value = ipv6Packet.DstIP.String()
ep.Hash = HashFromValues(ipv6Packet.SrcIP, ipv6Packet.DstIP)
fs.Endpoints = append(fs.Endpoints, ep)
return fs.updateNetworkLayerStatistics(packet, length)
}
return 0, errors.New("Unable to decode the IP layer")
}
func (fs *FlowStatistics) updateNetworkLayerStatistics(packet *gopacket.Packet, length uint64) (uint64, error) {
ipv4Layer := (*packet).Layer(layers.LayerTypeIPv4)
if ipv4Packet, ok := ipv4Layer.(*layers.IPv4); ok {
ep := fs.Endpoints[FlowEndpointLayer_NETWORK]
var e *FlowEndpointStatistics
if ep.AB.Value == ipv4Packet.SrcIP.String() {
e = ep.AB
} else {
e = ep.BA
}
e.Packets += uint64(1)
e.Bytes += length
return length - uint64(len(ipv4Packet.Contents)), nil
}
ipv6Layer := (*packet).Layer(layers.LayerTypeIPv6)
if ipv6Packet, ok := ipv6Layer.(*layers.IPv6); ok {
ep := fs.Endpoints[FlowEndpointLayer_NETWORK]
var e *FlowEndpointStatistics
if ep.AB.Value == ipv6Packet.SrcIP.String() {
e = ep.AB
} else {
e = ep.BA
}
e.Packets += uint64(1)
e.Bytes += length
return length - uint64(len(ipv6Packet.Contents)), nil
}
return 0, errors.New("Unable to decode the IP layer")
}
func (fs *FlowStatistics) newTransportLayerEndpointStatistics(packet *gopacket.Packet, length uint64) (uint64, error) {
ep := &FlowEndpointsStatistics{}
ep.AB = &FlowEndpointStatistics{}
ep.BA = &FlowEndpointStatistics{}
var transportLayer gopacket.Layer
var ok bool
transportLayer = (*packet).Layer(layers.LayerTypeTCP)
_, ok = transportLayer.(*layers.TCP)
ptype := FlowEndpointType_TCPPORT
if !ok {
transportLayer = (*packet).Layer(layers.LayerTypeUDP)
_, ok = transportLayer.(*layers.UDP)
ptype = FlowEndpointType_UDPPORT
if !ok {
transportLayer = (*packet).Layer(layers.LayerTypeSCTP)
_, ok = transportLayer.(*layers.SCTP)
ptype = FlowEndpointType_SCTPPORT
if !ok {
return 0, errors.New("Unable to decode the transport layer")
}
}
}
ep.Type = ptype
switch ptype {
case FlowEndpointType_TCPPORT:
transportPacket, _ := transportLayer.(*layers.TCP)
ep.AB.Value = strconv.Itoa(int(transportPacket.SrcPort))
ep.BA.Value = strconv.Itoa(int(transportPacket.DstPort))
ep.Hash = HashFromValues(transportPacket.SrcPort, transportPacket.DstPort)
case FlowEndpointType_UDPPORT:
transportPacket, _ := transportLayer.(*layers.UDP)
ep.AB.Value = strconv.Itoa(int(transportPacket.SrcPort))
ep.BA.Value = strconv.Itoa(int(transportPacket.DstPort))
ep.Hash = HashFromValues(transportPacket.SrcPort, transportPacket.DstPort)
case FlowEndpointType_SCTPPORT:
transportPacket, _ := transportLayer.(*layers.SCTP)
ep.AB.Value = strconv.Itoa(int(transportPacket.SrcPort))
ep.BA.Value = strconv.Itoa(int(transportPacket.DstPort))
ep.Hash = HashFromValues(transportPacket.SrcPort, transportPacket.DstPort)
}
fs.Endpoints = append(fs.Endpoints, ep)
return fs.updateTransportLayerStatistics(packet, length)
}
func (fs *FlowStatistics) updateTransportLayerStatistics(packet *gopacket.Packet, length uint64) (uint64, error) {
if len(fs.Endpoints) <= int(FlowEndpointLayer_TRANSPORT) {
return 0, errors.New("Unable to decode the transport layer")
}
ep := fs.Endpoints[FlowEndpointLayer_TRANSPORT]
var transportLayer gopacket.Layer
var srcPort string
switch ep.Type {
case FlowEndpointType_TCPPORT:
transportLayer = (*packet).Layer(layers.LayerTypeTCP)
transportPacket, _ := transportLayer.(*layers.TCP)
srcPort = transportPacket.SrcPort.String()
case FlowEndpointType_UDPPORT:
transportLayer = (*packet).Layer(layers.LayerTypeUDP)
transportPacket, _ := transportLayer.(*layers.UDP)
srcPort = transportPacket.SrcPort.String()
case FlowEndpointType_SCTPPORT:
transportLayer = (*packet).Layer(layers.LayerTypeSCTP)
transportPacket, _ := transportLayer.(*layers.SCTP)
srcPort = transportPacket.SrcPort.String()
}
var e *FlowEndpointStatistics
if ep.AB.Value == srcPort {
e = ep.AB
} else {
e = ep.BA
}
e.Packets += uint64(1)
e.Bytes += length
return length - uint64(len(transportLayer.LayerContents())), nil
}
type FlowSetBandwidth struct {
ABpackets uint64
ABbytes uint64
BApackets uint64
BAbytes uint64
Duration int64
NBFlow uint64
}
func (fsbw FlowSetBandwidth) String() string {
return fmt.Sprintf("dt : %d seconds nbFlow %d\n\t\tAB -> BA\nPackets : %8d %8d\nBytes : %8d %8d\n", fsbw.Duration, fsbw.NBFlow, fsbw.ABpackets, fsbw.BApackets, fsbw.ABbytes, fsbw.BAbytes)
}