forked from Cistern/sflow
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflow_sample.go
More file actions
193 lines (161 loc) · 3.76 KB
/
flow_sample.go
File metadata and controls
193 lines (161 loc) · 3.76 KB
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
package sflow
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"github.com/fstelzer/sflow/records"
"io"
)
type FlowSample struct {
SequenceNum uint32
SourceIdType byte
SourceIdIndexVal uint32 // NOTE: this is 3 bytes in the datagram
SamplingRate uint32
SamplePool uint32
Drops uint32
Input uint32
Output uint32
numRecords uint32
Records []records.Record
}
func (s FlowSample) String() string {
type X FlowSample
x := X(s)
return fmt.Sprintf("FlowSample: %+v", x)
}
// SampleType returns the type of sFlow sample.
func (s *FlowSample) SampleType() int {
return TypeFlowSample
}
func (s *FlowSample) GetRecords() []records.Record {
return s.Records
}
func decodeFlowSample(r io.ReadSeeker) (Sample, error) {
s := &FlowSample{}
var err error
err = binary.Read(r, binary.BigEndian, &s.SequenceNum)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.BigEndian, &s.SourceIdType)
if err != nil {
return nil, err
}
var srcIdIndexVal [3]byte
n, err := r.Read(srcIdIndexVal[:])
if err != nil {
return nil, err
}
if n != 3 {
return nil, errors.New("sflow: counter sample decoding error")
}
s.SourceIdIndexVal = uint32(srcIdIndexVal[2]) |
uint32(srcIdIndexVal[1]<<8) |
uint32(srcIdIndexVal[0]<<16)
err = binary.Read(r, binary.BigEndian, &s.SamplingRate)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.BigEndian, &s.SamplePool)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.BigEndian, &s.Drops)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.BigEndian, &s.Input)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.BigEndian, &s.Output)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.BigEndian, &s.numRecords)
if err != nil {
return nil, err
}
for i := uint32(0); i < s.numRecords; i++ {
format, length := uint32(0), uint32(0)
err = binary.Read(r, binary.BigEndian, &format)
if err != nil {
return nil, err
}
err = binary.Read(r, binary.BigEndian, &length)
if err != nil {
return nil, err
}
var rec records.Record
//fmt.Printf("sflow: Decoding record type %d with length %d\n", format, length)
if rec, err = records.DecodeFlow(r, format); err != nil {
//return nil, err
_, err := r.Seek(int64(length), 1)
if err != nil {
return nil, err
}
continue
}
s.Records = append(s.Records, rec)
}
return s, nil
}
func (s *FlowSample) encode(w io.Writer) error {
var err error
// We first need to encode the records.
buf := &bytes.Buffer{}
for _, rec := range s.Records {
err = rec.Encode(buf)
if err != nil {
return records.ErrEncodingRecord
}
}
// Fields
encodedSampleSize := uint32(4 + 1 + 3 + 4 + 4 + 4 + 4 + 4 + 4)
// Encoded records
encodedSampleSize += uint32(buf.Len())
err = binary.Write(w, binary.BigEndian, uint32(s.SampleType()))
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, encodedSampleSize)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, s.SequenceNum)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian,
uint32(s.SourceIdType)|s.SourceIdIndexVal<<24)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, s.SamplingRate)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, s.SamplePool)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, s.Drops)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, s.Input)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, s.Output)
if err != nil {
return err
}
err = binary.Write(w, binary.BigEndian, uint32(len(s.Records)))
if err != nil {
return err
}
_, err = io.Copy(w, buf)
return err
}