-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathattr_report.go
282 lines (245 loc) · 7.21 KB
/
attr_report.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
package gtp5gnl
import (
"time"
"unsafe"
"github.com/khirono/go-nl"
)
// for UPF Usage Statistic
const (
USTAT_UL_VOL_RX = iota + 1
USTAT_UL_VOL_TX
USTAT_DL_VOL_RX
USTAT_DL_VOL_TX
USTAT_UL_PKT_RX
USTAT_UL_PKT_TX
USTAT_DL_PKT_RX
USTAT_DL_PKT_TX
)
type UsageStatistic struct {
TotalVolRx uint64
TotalVolTx uint64
UlVolRx uint64
UlVolTx uint64
DlVolRx uint64
DlVolTx uint64
TotalPktRx uint64
TotalPktTx uint64
UlPktRx uint64
UlPktTx uint64
DlPktRx uint64
DlPktTx uint64
}
const (
UR = iota + 5
)
const (
UR_URRID = iota + 3
UR_USAGE_REPORT_TRIGGER
UR_URSEQN
UR_VOLUME_MEASUREMENT
UR_QUERY_URR_REFERENCE
UR_START_TIME
UR_END_TIME
UR_SEID
)
const (
UR_VOLUME_MEASUREMENT_FLAGS = iota + 1
UR_VOLUME_MEASUREMENT_TOVOL
UR_VOLUME_MEASUREMENT_UVOL
UR_VOLUME_MEASUREMENT_DVOL
UR_VOLUME_MEASUREMENT_TOPACKET
UR_VOLUME_MEASUREMENT_UPACKET
UR_VOLUME_MEASUREMENT_DPACKET
)
const (
TOVOL uint8 = 1 << iota
ULVOL
DLVOL
TONOP
ULNOP
DLNOP
)
type USAReport struct {
URRID uint32
URSEQN uint32
USARTrigger uint32
VolMeasurement VolumeMeasurement
QueryUrrRef uint32
StartTime time.Time
EndTime time.Time
SEID uint64
}
type VolumeMeasurement struct {
Flag uint8
TotalVolume uint64
UplinkVolume uint64
DownlinkVolume uint64
TotalPktNum uint64
UplinkPktNum uint64
DownlinkPktNum uint64
}
// The maximun netlink message size is 16K, and the body for the attibutes are 7856 Bytes
const (
MAX_NETLINK_MSG_BODY_SIZE = 7856
NETLIMK_ATTR_HDR_SIZE = 4
)
// The netlink attribute size of UR need to count the UR header(4) + size of the attributes (and it's header) in UR
func MaxNetlinkUsageReportNum() int {
size := NETLIMK_ATTR_HDR_SIZE // UR attr header
size += NETLIMK_ATTR_HDR_SIZE // UR_URRID attr header
size += int(unsafe.Sizeof(uint32(0))) // UR_URRID attr data
size += NETLIMK_ATTR_HDR_SIZE // UR_USAGE_REPORT_TRIGGER attr header
size += int(unsafe.Sizeof(uint32(0))) // UR_USAGE_REPORT_TRIGGER attr data
size += NETLIMK_ATTR_HDR_SIZE // UR_URSEQN attr header
size += int(unsafe.Sizeof(uint32(0))) // UR_URSEQN attr data
size += NETLIMK_ATTR_HDR_SIZE // UR_VOLUME_MEASUREMENT attr header
size += NETLIMK_ATTR_HDR_SIZE // UR_VOLUME_MEASUREMENT_TOVOL attr header
size += int(unsafe.Sizeof(uint64(0))) // UR_VOLUME_MEASUREMENT_TOVOL attr data
size += NETLIMK_ATTR_HDR_SIZE // UR_VOLUME_MEASUREMENT_UVOL attr header
size += int(unsafe.Sizeof(uint64(0))) // UR_VOLUME_MEASUREMENT_UVOL attr data
size += NETLIMK_ATTR_HDR_SIZE // UR_VOLUME_MEASUREMENT_DVOL attr header
size += int(unsafe.Sizeof(uint64(0))) // UR_VOLUME_MEASUREMENT_DVOL attr data
size += NETLIMK_ATTR_HDR_SIZE // UR_VOLUME_MEASUREMENT_TOPACKET attr header
size += int(unsafe.Sizeof(uint64(0))) // UR_VOLUME_MEASUREMENT_TOPACKET attr data
size += NETLIMK_ATTR_HDR_SIZE // UR_VOLUME_MEASUREMENT_UPACKET attr header
size += int(unsafe.Sizeof(uint64(0))) // UR_VOLUME_MEASUREMENT_UPACKET attr data
size += NETLIMK_ATTR_HDR_SIZE // UR_VOLUME_MEASUREMENT_DPACKET attr header
size += int(unsafe.Sizeof(uint64(0))) // UR_VOLUME_MEASUREMENT_DPACKET attr data
size += NETLIMK_ATTR_HDR_SIZE // UR_START_TIME attr header
size += int(unsafe.Sizeof(uint64(0))) // UR_START_TIME attr data
size += NETLIMK_ATTR_HDR_SIZE // UR_END_TIME attr header
size += int(unsafe.Sizeof(uint64(0))) // UR_END_TIME attr data
size += NETLIMK_ATTR_HDR_SIZE // UR_SEID attr header
size += int(unsafe.Sizeof(uint64(0))) // UR_SEID attr data
return MAX_NETLINK_MSG_BODY_SIZE / size
}
func decodeVolumeMeasurement(b []byte) (VolumeMeasurement, error) {
var VolMeasurement VolumeMeasurement
for len(b) > 0 {
hdr, n, err := nl.DecodeAttrHdr(b)
if err != nil {
return VolMeasurement, err
}
attrLen := int(hdr.Len)
switch hdr.MaskedType() {
case UR_VOLUME_MEASUREMENT_TOVOL:
v := native.Uint64(b[n:attrLen])
VolMeasurement.TotalVolume = v
VolMeasurement.Flag |= TOVOL
case UR_VOLUME_MEASUREMENT_UVOL:
v := native.Uint64(b[n:attrLen])
VolMeasurement.UplinkVolume = v
VolMeasurement.Flag |= ULVOL
case UR_VOLUME_MEASUREMENT_DVOL:
v := native.Uint64(b[n:attrLen])
VolMeasurement.DownlinkVolume = v
VolMeasurement.Flag |= DLVOL
case UR_VOLUME_MEASUREMENT_TOPACKET:
v := native.Uint64(b[n:attrLen])
VolMeasurement.TotalPktNum = v
VolMeasurement.Flag |= TONOP
case UR_VOLUME_MEASUREMENT_UPACKET:
v := native.Uint64(b[n:attrLen])
VolMeasurement.UplinkPktNum = v
VolMeasurement.Flag |= ULNOP
case UR_VOLUME_MEASUREMENT_DPACKET:
v := native.Uint64(b[n:attrLen])
VolMeasurement.DownlinkPktNum = v
VolMeasurement.Flag |= DLNOP
default:
return VolMeasurement, nil
}
b = b[hdr.Len.Align():]
}
return VolMeasurement, nil
}
func DecodeUsageStatistic(b []byte) (*UsageStatistic, error) {
ustat := new(UsageStatistic)
for len(b) > 0 {
hdr, n, err := nl.DecodeAttrHdr(b)
if err != nil {
return nil, err
}
attrLen := int(hdr.Len)
switch hdr.MaskedType() {
case USTAT_UL_VOL_RX:
ustat.UlVolRx = native.Uint64(b[n:attrLen])
case USTAT_UL_VOL_TX:
ustat.UlVolTx = native.Uint64(b[n:attrLen])
case USTAT_DL_VOL_RX:
ustat.DlVolRx = native.Uint64(b[n:attrLen])
case USTAT_DL_VOL_TX:
ustat.DlVolTx = native.Uint64(b[n:attrLen])
case USTAT_UL_PKT_RX:
ustat.UlPktRx = native.Uint64(b[n:attrLen])
case USTAT_UL_PKT_TX:
ustat.UlPktTx = native.Uint64(b[n:attrLen])
case USTAT_DL_PKT_RX:
ustat.DlPktRx = native.Uint64(b[n:attrLen])
case USTAT_DL_PKT_TX:
ustat.DlPktTx = native.Uint64(b[n:attrLen])
}
b = b[hdr.Len.Align():]
}
// total volume count
ustat.TotalVolRx = ustat.UlVolRx + ustat.DlVolRx
ustat.TotalVolTx = ustat.UlVolTx + ustat.DlVolTx
// total packet count
ustat.TotalPktRx = ustat.UlPktRx + ustat.DlPktRx
ustat.TotalPktTx = ustat.UlPktTx + ustat.DlPktTx
return ustat, nil
}
func DecodeAllUSAReports(b []byte) ([]USAReport, error) {
var usars []USAReport
for len(b) > 0 {
hdr, n, err := nl.DecodeAttrHdr(b)
if err != nil {
return nil, err
}
attrLen := int(hdr.Len)
switch hdr.MaskedType() {
case UR:
r, err := decodeUSAReport(b[n:attrLen])
if err != nil {
return nil, err
}
usars = append(usars, *r)
}
b = b[hdr.Len.Align():]
}
return usars, nil
}
func decodeUSAReport(b []byte) (*USAReport, error) {
report := new(USAReport)
for len(b) > 0 {
hdr, n, err := nl.DecodeAttrHdr(b)
if err != nil {
return nil, err
}
attrLen := int(hdr.Len)
switch hdr.MaskedType() {
case UR_URRID:
report.URRID = native.Uint32(b[n:attrLen])
case UR_USAGE_REPORT_TRIGGER:
report.USARTrigger = native.Uint32(b[n:attrLen])
case UR_URSEQN:
report.URSEQN = native.Uint32(b[n:attrLen])
case UR_VOLUME_MEASUREMENT:
volMeasurement, err := decodeVolumeMeasurement(b[n:attrLen])
if err != nil {
return nil, err
}
report.VolMeasurement = volMeasurement
case UR_START_TIME:
v := native.Uint64(b[n:attrLen])
report.StartTime = time.Unix(0, int64(v))
case UR_END_TIME:
v := native.Uint64(b[n:attrLen])
report.EndTime = time.Unix(0, int64(v))
case UR_SEID:
report.SEID = native.Uint64(b[n:attrLen])
}
b = b[hdr.Len.Align():]
}
return report, nil
}