forked from google/gopacket
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsflow.go
2568 lines (2324 loc) · 97.6 KB
/
sflow.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2014 Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
/*
This layer decodes SFlow version 5 datagrams.
The specification can be found here: http://sflow.org/sflow_version_5.txt
Additional developer information about sflow can be found at:
http://sflow.org/developers/specifications.php
And SFlow in general:
http://sflow.org/index.php
Two forms of sample data are defined: compact and expanded. The
Specification has this to say:
Compact and expand forms of counter and flow samples are defined.
An agent must not mix compact/expanded encodings. If an agent
will never use ifIndex numbers >= 2^24 then it must use compact
encodings for all interfaces. Otherwise the expanded formats must
be used for all interfaces.
This decoder only supports the compact form, because that is the only
one for which data was available.
The datagram is composed of one or more samples of type flow or counter,
and each sample is composed of one or more records describing the sample.
A sample is a single instance of sampled inforamtion, and each record in
the sample gives additional / supplimentary information about the sample.
The following sample record types are supported:
Raw Packet Header
opaque = flow_data; enterprise = 0; format = 1
Extended Switch Data
opaque = flow_data; enterprise = 0; format = 1001
Extended Router Data
opaque = flow_data; enterprise = 0; format = 1002
Extended Gateway Data
opaque = flow_data; enterprise = 0; format = 1003
Extended User Data
opaque = flow_data; enterprise = 0; format = 1004
Extended URL Data
opaque = flow_data; enterprise = 0; format = 1005
The following types of counter records are supported:
Generic Interface Counters - see RFC 2233
opaque = counter_data; enterprise = 0; format = 1
Ethernet Interface Counters - see RFC 2358
opaque = counter_data; enterprise = 0; format = 2
SFlow is encoded using XDR (RFC4506). There are a few places
where the standard 4-byte fields are partitioned into two
bitfields of different lengths. I'm not sure why the designers
chose to pack together two values like this in some places, and
in others they use the entire 4-byte value to store a number that
will never be more than a few bits. In any case, there are a couple
of types defined to handle the decoding of these bitfields, and
that's why they're there. */
package layers
import (
"encoding/binary"
"errors"
"fmt"
"net"
"github.com/kubeshark/gopacket"
"github.com/kubeshark/tracerproto/pkg/unixpacket"
)
// SFlowRecord holds both flow sample records and counter sample records.
// A Record is the structure that actually holds the sampled data
// and / or counters.
type SFlowRecord interface {
}
// SFlowDataSource encodes a 2-bit SFlowSourceFormat in its most significant
// 2 bits, and an SFlowSourceValue in its least significant 30 bits.
// These types and values define the meaning of the inteface information
// presented in the sample metadata.
type SFlowDataSource int32
func (sdc SFlowDataSource) decode() (SFlowSourceFormat, SFlowSourceValue) {
leftField := sdc >> 30
rightField := uint32(0x3FFFFFFF) & uint32(sdc)
return SFlowSourceFormat(leftField), SFlowSourceValue(rightField)
}
type SFlowDataSourceExpanded struct {
SourceIDClass SFlowSourceFormat
SourceIDIndex SFlowSourceValue
}
func (sdce SFlowDataSourceExpanded) decode() (SFlowSourceFormat, SFlowSourceValue) {
leftField := sdce.SourceIDClass >> 30
rightField := uint32(0x3FFFFFFF) & uint32(sdce.SourceIDIndex)
return SFlowSourceFormat(leftField), SFlowSourceValue(rightField)
}
type SFlowSourceFormat uint32
type SFlowSourceValue uint32
const (
SFlowTypeSingleInterface SFlowSourceFormat = 0
SFlowTypePacketDiscarded SFlowSourceFormat = 1
SFlowTypeMultipleDestinations SFlowSourceFormat = 2
)
func (sdf SFlowSourceFormat) String() string {
switch sdf {
case SFlowTypeSingleInterface:
return "Single Interface"
case SFlowTypePacketDiscarded:
return "Packet Discarded"
case SFlowTypeMultipleDestinations:
return "Multiple Destinations"
default:
return "UNKNOWN"
}
}
func decodeSFlow(data []byte, p gopacket.PacketBuilder) error {
s := &SFlowDatagram{}
err := s.DecodeFromBytes(data, p)
if err != nil {
return err
}
p.AddLayer(s)
p.SetApplicationLayer(s)
return nil
}
// SFlowDatagram is the outermost container which holds some basic information
// about the reporting agent, and holds at least one sample record
type SFlowDatagram struct {
BaseLayer
DatagramVersion uint32
AgentAddress net.IP
SubAgentID uint32
SequenceNumber uint32
AgentUptime uint32
SampleCount uint32
FlowSamples []SFlowFlowSample
CounterSamples []SFlowCounterSample
}
// An SFlow datagram's outer container has the following
// structure:
// 0 15 31
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int sFlow version (2|4|5) |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int IP version of the Agent (1=v4|2=v6) |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// / Agent IP address (v4=4byte|v6=16byte) /
// / /
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int sub agent id |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int datagram sequence number |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int switch uptime in ms |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int n samples in datagram |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// / n samples /
// / /
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// SFlowDataFormat encodes the EnterpriseID in the most
// significant 12 bits, and the SampleType in the least significant
// 20 bits.
type SFlowDataFormat uint32
func (sdf SFlowDataFormat) decode() (SFlowEnterpriseID, SFlowSampleType) {
leftField := sdf >> 12
rightField := uint32(0xFFF) & uint32(sdf)
return SFlowEnterpriseID(leftField), SFlowSampleType(rightField)
}
// SFlowEnterpriseID is used to differentiate between the
// official SFlow standard, and other, vendor-specific
// types of flow data. (Similiar to SNMP's enterprise MIB
// OIDs) Only the office SFlow Enterprise ID is decoded
// here.
type SFlowEnterpriseID uint32
const (
SFlowStandard SFlowEnterpriseID = 0
)
func (eid SFlowEnterpriseID) String() string {
switch eid {
case SFlowStandard:
return "Standard SFlow"
default:
return ""
}
}
func (eid SFlowEnterpriseID) GetType() SFlowEnterpriseID {
return SFlowStandard
}
// SFlowSampleType specifies the type of sample. Only flow samples
// and counter samples are supported
type SFlowSampleType uint32
const (
SFlowTypeFlowSample SFlowSampleType = 1
SFlowTypeCounterSample SFlowSampleType = 2
SFlowTypeExpandedFlowSample SFlowSampleType = 3
SFlowTypeExpandedCounterSample SFlowSampleType = 4
)
func (st SFlowSampleType) GetType() SFlowSampleType {
switch st {
case SFlowTypeFlowSample:
return SFlowTypeFlowSample
case SFlowTypeCounterSample:
return SFlowTypeCounterSample
case SFlowTypeExpandedFlowSample:
return SFlowTypeExpandedFlowSample
case SFlowTypeExpandedCounterSample:
return SFlowTypeExpandedCounterSample
default:
panic("Invalid Sample Type")
}
}
func (st SFlowSampleType) String() string {
switch st {
case SFlowTypeFlowSample:
return "Flow Sample"
case SFlowTypeCounterSample:
return "Counter Sample"
case SFlowTypeExpandedFlowSample:
return "Expanded Flow Sample"
case SFlowTypeExpandedCounterSample:
return "Expanded Counter Sample"
default:
return ""
}
}
func (s *SFlowDatagram) LayerType() gopacket.LayerType { return LayerTypeSFlow }
func (d *SFlowDatagram) Payload() []byte { return nil }
func (d *SFlowDatagram) CanDecode() gopacket.LayerClass { return LayerTypeSFlow }
func (d *SFlowDatagram) NextLayerType() gopacket.LayerType { return gopacket.LayerTypePayload }
// SFlowIPType determines what form the IP address being decoded will
// take. This is an XDR union type allowing for both IPv4 and IPv6
type SFlowIPType uint32
const (
SFlowIPv4 SFlowIPType = 1
SFlowIPv6 SFlowIPType = 2
)
func (s SFlowIPType) String() string {
switch s {
case SFlowIPv4:
return "IPv4"
case SFlowIPv6:
return "IPv6"
default:
return ""
}
}
func (s SFlowIPType) Length() int {
switch s {
case SFlowIPv4:
return 4
case SFlowIPv6:
return 16
default:
return 0
}
}
func (s *SFlowDatagram) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
var agentAddressType SFlowIPType
data, s.DatagramVersion = data[4:], binary.BigEndian.Uint32(data[:4])
data, agentAddressType = data[4:], SFlowIPType(binary.BigEndian.Uint32(data[:4]))
data, s.AgentAddress = data[agentAddressType.Length():], data[:agentAddressType.Length()]
data, s.SubAgentID = data[4:], binary.BigEndian.Uint32(data[:4])
data, s.SequenceNumber = data[4:], binary.BigEndian.Uint32(data[:4])
data, s.AgentUptime = data[4:], binary.BigEndian.Uint32(data[:4])
data, s.SampleCount = data[4:], binary.BigEndian.Uint32(data[:4])
if s.SampleCount < 1 {
return fmt.Errorf("SFlow Datagram has invalid sample length: %d", s.SampleCount)
}
for i := uint32(0); i < s.SampleCount; i++ {
sdf := SFlowDataFormat(binary.BigEndian.Uint32(data[:4]))
_, sampleType := sdf.decode()
switch sampleType {
case SFlowTypeFlowSample:
if flowSample, err := decodeFlowSample(&data, false); err == nil {
s.FlowSamples = append(s.FlowSamples, flowSample)
} else {
return err
}
case SFlowTypeCounterSample:
if counterSample, err := decodeCounterSample(&data, false); err == nil {
s.CounterSamples = append(s.CounterSamples, counterSample)
} else {
return err
}
case SFlowTypeExpandedFlowSample:
if flowSample, err := decodeFlowSample(&data, true); err == nil {
s.FlowSamples = append(s.FlowSamples, flowSample)
} else {
return err
}
case SFlowTypeExpandedCounterSample:
if counterSample, err := decodeCounterSample(&data, true); err == nil {
s.CounterSamples = append(s.CounterSamples, counterSample)
} else {
return err
}
default:
return fmt.Errorf("Unsupported SFlow sample type %d", sampleType)
}
}
return nil
}
// SFlowFlowSample represents a sampled packet and contains
// one or more records describing the packet
type SFlowFlowSample struct {
EnterpriseID SFlowEnterpriseID
Format SFlowSampleType
SampleLength uint32
SequenceNumber uint32
SourceIDClass SFlowSourceFormat
SourceIDIndex SFlowSourceValue
SamplingRate uint32
SamplePool uint32
Dropped uint32
InputInterfaceFormat uint32
InputInterface uint32
OutputInterfaceFormat uint32
OutputInterface uint32
RecordCount uint32
Records []SFlowRecord
}
// Flow samples have the following structure. Note
// the bit fields to encode the Enterprise ID and the
// Flow record format: type 1
// 0 15 31
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | 20 bit Interprise (0) |12 bit format |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | sample length |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int sample sequence number |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |id type | src id index value |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int sampling rate |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int sample pool |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int drops |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int input ifIndex |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int output ifIndex |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int number of records |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// / flow records /
// / /
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// Flow samples have the following structure.
// Flow record format: type 3
// 0 15 31
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | 20 bit Interprise (0) |12 bit format |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | sample length |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int sample sequence number |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int src id type |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int src id index value |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int sampling rate |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int sample pool |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int drops |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int input interface format |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int input interface value |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int output interface format |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int output interface value |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int number of records |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// / flow records /
// / /
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
type SFlowFlowDataFormat uint32
func (fdf SFlowFlowDataFormat) decode() (SFlowEnterpriseID, SFlowFlowRecordType) {
leftField := fdf >> 12
rightField := uint32(0xFFF) & uint32(fdf)
return SFlowEnterpriseID(leftField), SFlowFlowRecordType(rightField)
}
func (fs SFlowFlowSample) GetRecords() []SFlowRecord {
return fs.Records
}
func (fs SFlowFlowSample) GetType() SFlowSampleType {
return SFlowTypeFlowSample
}
func skipRecord(data *[]byte) {
recordLength := int(binary.BigEndian.Uint32((*data)[4:]))
*data = (*data)[(recordLength+((4-recordLength)%4))+8:]
}
func decodeFlowSample(data *[]byte, expanded bool) (SFlowFlowSample, error) {
s := SFlowFlowSample{}
var sdf SFlowDataFormat
*data, sdf = (*data)[4:], SFlowDataFormat(binary.BigEndian.Uint32((*data)[:4]))
var sdc SFlowDataSource
s.EnterpriseID, s.Format = sdf.decode()
if len(*data) < 4 {
return SFlowFlowSample{}, errors.New("ethernet counters too small")
}
*data, s.SampleLength = (*data)[4:], binary.BigEndian.Uint32((*data)[:4])
if len(*data) < 4 {
return SFlowFlowSample{}, errors.New("ethernet counters too small")
}
*data, s.SequenceNumber = (*data)[4:], binary.BigEndian.Uint32((*data)[:4])
if expanded {
if len(*data) < 4 {
return SFlowFlowSample{}, errors.New("ethernet counters too small")
}
*data, s.SourceIDClass = (*data)[4:], SFlowSourceFormat(binary.BigEndian.Uint32((*data)[:4]))
if len(*data) < 4 {
return SFlowFlowSample{}, errors.New("ethernet counters too small")
}
*data, s.SourceIDIndex = (*data)[4:], SFlowSourceValue(binary.BigEndian.Uint32((*data)[:4]))
} else {
if len(*data) < 4 {
return SFlowFlowSample{}, errors.New("ethernet counters too small")
}
*data, sdc = (*data)[4:], SFlowDataSource(binary.BigEndian.Uint32((*data)[:4]))
s.SourceIDClass, s.SourceIDIndex = sdc.decode()
}
if len(*data) < 4 {
return SFlowFlowSample{}, errors.New("ethernet counters too small")
}
*data, s.SamplingRate = (*data)[4:], binary.BigEndian.Uint32((*data)[:4])
if len(*data) < 4 {
return SFlowFlowSample{}, errors.New("ethernet counters too small")
}
*data, s.SamplePool = (*data)[4:], binary.BigEndian.Uint32((*data)[:4])
if len(*data) < 4 {
return SFlowFlowSample{}, errors.New("ethernet counters too small")
}
*data, s.Dropped = (*data)[4:], binary.BigEndian.Uint32((*data)[:4])
if expanded {
if len(*data) < 4 {
return SFlowFlowSample{}, errors.New("ethernet counters too small")
}
*data, s.InputInterfaceFormat = (*data)[4:], binary.BigEndian.Uint32((*data)[:4])
if len(*data) < 4 {
return SFlowFlowSample{}, errors.New("ethernet counters too small")
}
*data, s.InputInterface = (*data)[4:], binary.BigEndian.Uint32((*data)[:4])
if len(*data) < 4 {
return SFlowFlowSample{}, errors.New("ethernet counters too small")
}
*data, s.OutputInterfaceFormat = (*data)[4:], binary.BigEndian.Uint32((*data)[:4])
if len(*data) < 4 {
return SFlowFlowSample{}, errors.New("ethernet counters too small")
}
*data, s.OutputInterface = (*data)[4:], binary.BigEndian.Uint32((*data)[:4])
} else {
if len(*data) < 4 {
return SFlowFlowSample{}, errors.New("ethernet counters too small")
}
*data, s.InputInterface = (*data)[4:], binary.BigEndian.Uint32((*data)[:4])
if len(*data) < 4 {
return SFlowFlowSample{}, errors.New("ethernet counters too small")
}
*data, s.OutputInterface = (*data)[4:], binary.BigEndian.Uint32((*data)[:4])
}
if len(*data) < 4 {
return SFlowFlowSample{}, errors.New("ethernet counters too small")
}
*data, s.RecordCount = (*data)[4:], binary.BigEndian.Uint32((*data)[:4])
for i := uint32(0); i < s.RecordCount; i++ {
rdf := SFlowFlowDataFormat(binary.BigEndian.Uint32((*data)[:4]))
enterpriseID, flowRecordType := rdf.decode()
// Try to decode when EnterpriseID is 0 signaling
// default sflow structs are used according specification
// Unexpected behavior detected for e.g. with pmacct
if enterpriseID == 0 {
switch flowRecordType {
case SFlowTypeRawPacketFlow:
if record, err := decodeRawPacketFlowRecord(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeExtendedUserFlow:
if record, err := decodeExtendedUserFlow(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeExtendedUrlFlow:
if record, err := decodeExtendedURLRecord(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeExtendedSwitchFlow:
if record, err := decodeExtendedSwitchFlowRecord(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeExtendedRouterFlow:
if record, err := decodeExtendedRouterFlowRecord(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeExtendedGatewayFlow:
if record, err := decodeExtendedGatewayFlowRecord(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeEthernetFrameFlow:
if record, err := decodeEthernetFrameFlowRecord(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeIpv4Flow:
if record, err := decodeSFlowIpv4Record(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeIpv6Flow:
if record, err := decodeSFlowIpv6Record(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeExtendedMlpsFlow:
// TODO
skipRecord(data)
return s, errors.New("skipping TypeExtendedMlpsFlow")
case SFlowTypeExtendedNatFlow:
// TODO
skipRecord(data)
return s, errors.New("skipping TypeExtendedNatFlow")
case SFlowTypeExtendedMlpsTunnelFlow:
// TODO
skipRecord(data)
return s, errors.New("skipping TypeExtendedMlpsTunnelFlow")
case SFlowTypeExtendedMlpsVcFlow:
// TODO
skipRecord(data)
return s, errors.New("skipping TypeExtendedMlpsVcFlow")
case SFlowTypeExtendedMlpsFecFlow:
// TODO
skipRecord(data)
return s, errors.New("skipping TypeExtendedMlpsFecFlow")
case SFlowTypeExtendedMlpsLvpFecFlow:
// TODO
skipRecord(data)
return s, errors.New("skipping TypeExtendedMlpsLvpFecFlow")
case SFlowTypeExtendedVlanFlow:
// TODO
skipRecord(data)
return s, errors.New("skipping TypeExtendedVlanFlow")
case SFlowTypeExtendedIpv4TunnelEgressFlow:
if record, err := decodeExtendedIpv4TunnelEgress(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeExtendedIpv4TunnelIngressFlow:
if record, err := decodeExtendedIpv4TunnelIngress(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeExtendedIpv6TunnelEgressFlow:
if record, err := decodeExtendedIpv6TunnelEgress(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeExtendedIpv6TunnelIngressFlow:
if record, err := decodeExtendedIpv6TunnelIngress(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeExtendedDecapsulateEgressFlow:
if record, err := decodeExtendedDecapsulateEgress(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeExtendedDecapsulateIngressFlow:
if record, err := decodeExtendedDecapsulateIngress(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeExtendedVniEgressFlow:
if record, err := decodeExtendedVniEgress(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeExtendedVniIngressFlow:
if record, err := decodeExtendedVniIngress(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
default:
return s, fmt.Errorf("Unsupported flow record type: %d", flowRecordType)
}
} else {
skipRecord(data)
}
}
return s, nil
}
// Counter samples report information about various counter
// objects. Typically these are items like IfInOctets, or
// CPU / Memory stats, etc. SFlow will report these at regular
// intervals as configured on the agent. If one were sufficiently
// industrious, this could be used to replace the typical
// SNMP polling used for such things.
type SFlowCounterSample struct {
EnterpriseID SFlowEnterpriseID
Format SFlowSampleType
SampleLength uint32
SequenceNumber uint32
SourceIDClass SFlowSourceFormat
SourceIDIndex SFlowSourceValue
RecordCount uint32
Records []SFlowRecord
}
// Counter samples have the following structure:
// 0 15 31
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int sample sequence number |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// |id type | src id index value |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | int number of records |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// / counter records /
// / /
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
type SFlowCounterDataFormat uint32
func (cdf SFlowCounterDataFormat) decode() (SFlowEnterpriseID, SFlowCounterRecordType) {
leftField := cdf >> 12
rightField := uint32(0xFFF) & uint32(cdf)
return SFlowEnterpriseID(leftField), SFlowCounterRecordType(rightField)
}
// GetRecords will return a slice of interface types
// representing records. A type switch can be used to
// get at the underlying SFlowCounterRecordType.
func (cs SFlowCounterSample) GetRecords() []SFlowRecord {
return cs.Records
}
// GetType will report the type of sample. Only the
// compact form of counter samples is supported
func (cs SFlowCounterSample) GetType() SFlowSampleType {
return SFlowTypeCounterSample
}
type SFlowCounterRecordType uint32
const (
SFlowTypeGenericInterfaceCounters SFlowCounterRecordType = 1
SFlowTypeEthernetInterfaceCounters SFlowCounterRecordType = 2
SFlowTypeTokenRingInterfaceCounters SFlowCounterRecordType = 3
SFlowType100BaseVGInterfaceCounters SFlowCounterRecordType = 4
SFlowTypeVLANCounters SFlowCounterRecordType = 5
SFlowTypeLACPCounters SFlowCounterRecordType = 7
SFlowTypeProcessorCounters SFlowCounterRecordType = 1001
SFlowTypeOpenflowPortCounters SFlowCounterRecordType = 1004
SFlowTypePORTNAMECounters SFlowCounterRecordType = 1005
SFLowTypeAPPRESOURCESCounters SFlowCounterRecordType = 2203
SFlowTypeOVSDPCounters SFlowCounterRecordType = 2207
)
func (cr SFlowCounterRecordType) String() string {
switch cr {
case SFlowTypeGenericInterfaceCounters:
return "Generic Interface Counters"
case SFlowTypeEthernetInterfaceCounters:
return "Ethernet Interface Counters"
case SFlowTypeTokenRingInterfaceCounters:
return "Token Ring Interface Counters"
case SFlowType100BaseVGInterfaceCounters:
return "100BaseVG Interface Counters"
case SFlowTypeVLANCounters:
return "VLAN Counters"
case SFlowTypeLACPCounters:
return "LACP Counters"
case SFlowTypeProcessorCounters:
return "Processor Counters"
case SFlowTypeOpenflowPortCounters:
return "Openflow Port Counters"
case SFlowTypePORTNAMECounters:
return "PORT NAME Counters"
case SFLowTypeAPPRESOURCESCounters:
return "App Resources Counters"
case SFlowTypeOVSDPCounters:
return "OVSDP Counters"
default:
return ""
}
}
func decodeCounterSample(data *[]byte, expanded bool) (SFlowCounterSample, error) {
s := SFlowCounterSample{}
var sdc SFlowDataSource
var sdce SFlowDataSourceExpanded
var sdf SFlowDataFormat
*data, sdf = (*data)[4:], SFlowDataFormat(binary.BigEndian.Uint32((*data)[:4]))
s.EnterpriseID, s.Format = sdf.decode()
*data, s.SampleLength = (*data)[4:], binary.BigEndian.Uint32((*data)[:4])
*data, s.SequenceNumber = (*data)[4:], binary.BigEndian.Uint32((*data)[:4])
if expanded {
*data, sdce = (*data)[8:], SFlowDataSourceExpanded{SFlowSourceFormat(binary.BigEndian.Uint32((*data)[:4])), SFlowSourceValue(binary.BigEndian.Uint32((*data)[4:8]))}
s.SourceIDClass, s.SourceIDIndex = sdce.decode()
} else {
*data, sdc = (*data)[4:], SFlowDataSource(binary.BigEndian.Uint32((*data)[:4]))
s.SourceIDClass, s.SourceIDIndex = sdc.decode()
}
*data, s.RecordCount = (*data)[4:], binary.BigEndian.Uint32((*data)[:4])
for i := uint32(0); i < s.RecordCount; i++ {
cdf := SFlowCounterDataFormat(binary.BigEndian.Uint32((*data)[:4]))
_, counterRecordType := cdf.decode()
switch counterRecordType {
case SFlowTypeGenericInterfaceCounters:
if record, err := decodeGenericInterfaceCounters(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeEthernetInterfaceCounters:
if record, err := decodeEthernetCounters(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeTokenRingInterfaceCounters:
skipRecord(data)
return s, errors.New("skipping TypeTokenRingInterfaceCounters")
case SFlowType100BaseVGInterfaceCounters:
skipRecord(data)
return s, errors.New("skipping Type100BaseVGInterfaceCounters")
case SFlowTypeVLANCounters:
if record, err := decodeVLANCounters(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeLACPCounters:
if record, err := decodeLACPCounters(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeProcessorCounters:
if record, err := decodeProcessorCounters(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeOpenflowPortCounters:
if record, err := decodeOpenflowportCounters(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypePORTNAMECounters:
if record, err := decodePortnameCounters(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFLowTypeAPPRESOURCESCounters:
if record, err := decodeAppresourcesCounters(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
case SFlowTypeOVSDPCounters:
if record, err := decodeOVSDPCounters(data); err == nil {
s.Records = append(s.Records, record)
} else {
return s, err
}
default:
return s, fmt.Errorf("Invalid counter record type: %d", counterRecordType)
}
}
return s, nil
}
// SFlowBaseFlowRecord holds the fields common to all records
// of type SFlowFlowRecordType
type SFlowBaseFlowRecord struct {
EnterpriseID SFlowEnterpriseID
Format SFlowFlowRecordType
FlowDataLength uint32
}
func (bfr SFlowBaseFlowRecord) GetType() SFlowFlowRecordType {
return bfr.Format
}
// SFlowFlowRecordType denotes what kind of Flow Record is
// represented. See RFC 3176
type SFlowFlowRecordType uint32
const (
SFlowTypeRawPacketFlow SFlowFlowRecordType = 1
SFlowTypeEthernetFrameFlow SFlowFlowRecordType = 2
SFlowTypeIpv4Flow SFlowFlowRecordType = 3
SFlowTypeIpv6Flow SFlowFlowRecordType = 4
SFlowTypeExtendedSwitchFlow SFlowFlowRecordType = 1001
SFlowTypeExtendedRouterFlow SFlowFlowRecordType = 1002
SFlowTypeExtendedGatewayFlow SFlowFlowRecordType = 1003
SFlowTypeExtendedUserFlow SFlowFlowRecordType = 1004
SFlowTypeExtendedUrlFlow SFlowFlowRecordType = 1005
SFlowTypeExtendedMlpsFlow SFlowFlowRecordType = 1006
SFlowTypeExtendedNatFlow SFlowFlowRecordType = 1007
SFlowTypeExtendedMlpsTunnelFlow SFlowFlowRecordType = 1008
SFlowTypeExtendedMlpsVcFlow SFlowFlowRecordType = 1009
SFlowTypeExtendedMlpsFecFlow SFlowFlowRecordType = 1010
SFlowTypeExtendedMlpsLvpFecFlow SFlowFlowRecordType = 1011
SFlowTypeExtendedVlanFlow SFlowFlowRecordType = 1012
SFlowTypeExtendedIpv4TunnelEgressFlow SFlowFlowRecordType = 1023
SFlowTypeExtendedIpv4TunnelIngressFlow SFlowFlowRecordType = 1024
SFlowTypeExtendedIpv6TunnelEgressFlow SFlowFlowRecordType = 1025
SFlowTypeExtendedIpv6TunnelIngressFlow SFlowFlowRecordType = 1026
SFlowTypeExtendedDecapsulateEgressFlow SFlowFlowRecordType = 1027
SFlowTypeExtendedDecapsulateIngressFlow SFlowFlowRecordType = 1028
SFlowTypeExtendedVniEgressFlow SFlowFlowRecordType = 1029
SFlowTypeExtendedVniIngressFlow SFlowFlowRecordType = 1030
)
func (rt SFlowFlowRecordType) String() string {
switch rt {
case SFlowTypeRawPacketFlow:
return "Raw Packet Flow Record"
case SFlowTypeEthernetFrameFlow:
return "Ethernet Frame Flow Record"
case SFlowTypeIpv4Flow:
return "IPv4 Flow Record"
case SFlowTypeIpv6Flow:
return "IPv6 Flow Record"
case SFlowTypeExtendedSwitchFlow:
return "Extended Switch Flow Record"
case SFlowTypeExtendedRouterFlow:
return "Extended Router Flow Record"
case SFlowTypeExtendedGatewayFlow:
return "Extended Gateway Flow Record"
case SFlowTypeExtendedUserFlow:
return "Extended User Flow Record"
case SFlowTypeExtendedUrlFlow:
return "Extended URL Flow Record"
case SFlowTypeExtendedMlpsFlow:
return "Extended MPLS Flow Record"
case SFlowTypeExtendedNatFlow:
return "Extended NAT Flow Record"
case SFlowTypeExtendedMlpsTunnelFlow:
return "Extended MPLS Tunnel Flow Record"
case SFlowTypeExtendedMlpsVcFlow:
return "Extended MPLS VC Flow Record"
case SFlowTypeExtendedMlpsFecFlow:
return "Extended MPLS FEC Flow Record"
case SFlowTypeExtendedMlpsLvpFecFlow:
return "Extended MPLS LVP FEC Flow Record"
case SFlowTypeExtendedVlanFlow:
return "Extended VLAN Flow Record"
case SFlowTypeExtendedIpv4TunnelEgressFlow:
return "Extended IPv4 Tunnel Egress Record"
case SFlowTypeExtendedIpv4TunnelIngressFlow:
return "Extended IPv4 Tunnel Ingress Record"
case SFlowTypeExtendedIpv6TunnelEgressFlow:
return "Extended IPv6 Tunnel Egress Record"
case SFlowTypeExtendedIpv6TunnelIngressFlow:
return "Extended IPv6 Tunnel Ingress Record"
case SFlowTypeExtendedDecapsulateEgressFlow:
return "Extended Decapsulate Egress Record"
case SFlowTypeExtendedDecapsulateIngressFlow:
return "Extended Decapsulate Ingress Record"
case SFlowTypeExtendedVniEgressFlow:
return "Extended VNI Ingress Record"
case SFlowTypeExtendedVniIngressFlow:
return "Extended VNI Ingress Record"
default:
return ""
}
}
// SFlowRawPacketFlowRecords hold information about a sampled
// packet grabbed as it transited the agent. This is
// perhaps the most useful and interesting record type,
// as it holds the headers of the sampled packet and
// can be used to build up a complete picture of the
// traffic patterns on a network.
//
// The raw packet header is sent back into gopacket for
// decoding, and the resulting gopackt.Packet is stored
// in the Header member
type SFlowRawPacketFlowRecord struct {
SFlowBaseFlowRecord
HeaderProtocol SFlowRawHeaderProtocol
FrameLength uint32
PayloadRemoved uint32
HeaderLength uint32
Header gopacket.Packet
}
// Raw packet record types have the following structure:
// 0 15 31
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | 20 bit Interprise (0) |12 bit format |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | record length |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | Header Protocol |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | Frame Length |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
// | Payload Removed |
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+