forked from florianl/go-tc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
attributeTcMsg.go
341 lines (321 loc) · 6.91 KB
/
attributeTcMsg.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
package tc
import (
"fmt"
"github.com/mdlayher/netlink"
)
func extractTcmsgAttributes(data []byte, info *Attribute) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
var kind string
ad.ByteOrder = nativeEndian
for ad.Next() {
switch ad.Type() {
case tcaKind:
info.Kind = ad.String()
kind = ad.String()
case tcaOptions:
extractTCAOptions(ad.Bytes(), info, kind)
case tcaChain:
info.Chain = ad.Uint32()
case tcaXstats:
tcstats := &Stats{}
extractTCStats(ad.Bytes(), tcstats)
info.XStats = tcstats
case tcaStats:
tcstats := &Stats{}
extractTCStats(ad.Bytes(), tcstats)
info.Stats = tcstats
case tcaStats2:
tcstats2 := &Stats2{}
extractTCStats2(ad.Bytes(), tcstats2)
info.Stats2 = tcstats2
case tcaHwOffload:
info.HwOffload = ad.Uint8()
case tcaEgressBlock:
info.EgressBlock = ad.Uint32()
case tcaIngressBlock:
info.IngressBlock = ad.Uint32()
default:
return fmt.Errorf("extractTcmsgAttributes()\t%d\n\t%v", ad.Type(), ad.Bytes())
}
}
return nil
}
func extractTCAOptions(data []byte, tc *Attribute, kind string) error {
switch kind {
case "fq_codel":
info := &FqCodel{}
if err := extractFqCodelOptions(data, info); err != nil {
return err
}
tc.FqCodel = info
case "clsact":
return extractClsact(data)
case "bpf":
info := &BPF{}
if err := extractBpfOptions(data, info); err != nil {
return err
}
tc.BPF = info
default:
return fmt.Errorf("extractTCAOptions(): unsupported kind: %s", kind)
}
return nil
}
func extractBpfOptions(data []byte, info *BPF) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
ad.ByteOrder = nativeEndian
for ad.Next() {
switch ad.Type() {
case tcaBpfAct:
action := &Action{}
if err := extractBPFAction(ad.Bytes(), action); err != nil {
return err
}
info.Action = action
case tcaBpfClassid:
info.ClassID = ad.Uint32()
case tcaBpfOpsLen:
info.OpsLen = ad.Uint16()
case tcaBpfOps:
info.Ops = ad.Bytes()
case tcaBpfFd:
info.FD = ad.Uint32()
case tcaBpfName:
info.Name = ad.String()
case tcaBpfFlags:
info.Flags = ad.Uint32()
case tcaBpfFlagsGen:
info.FlagsGen = ad.Uint32()
case tcaBpfTag:
info.Tag = ad.Bytes()
case tcaBpfID:
info.ID = ad.Uint32()
default:
return fmt.Errorf("extractBpfOptions()\t%d\n\t%v", ad.Type(), ad.Bytes())
}
}
return nil
}
// Actual attributes are nested inside and the nested bit is not set :-/
func extractBPFAction(data []byte, action *Action) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
ad.ByteOrder = nativeEndian
ad.Next()
return extractTcAction(ad.Bytes(), action)
}
func extractTcAction(data []byte, act *Action) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
ad.ByteOrder = nativeEndian
for ad.Next() {
switch ad.Type() {
case tcaActKind:
act.Kind = ad.String()
case tcaActOptions:
options := &BPFActionOptions{}
if err := extractActBpfOptions(ad.Bytes(), options); err != nil {
return err
}
act.BPFOptions = options
case tcaActStats:
stats := &ActionStats{}
if err := extractActStats(ad.Bytes(), stats); err != nil {
return err
}
act.Statistics = stats
default:
return fmt.Errorf("extractTcAction()\t%d\n\t%v", ad.Type(), ad.Bytes())
}
}
return nil
}
func extractActStats(data []byte, stats *ActionStats) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
ad.ByteOrder = nativeEndian
for ad.Next() {
switch ad.Type() {
case tcaStatsBasic:
info := &GenStatsBasic{}
if err := extractGnetStatsBasic(ad.Bytes(), info); err != nil {
return err
}
stats.Basic = info
case tcaStatsRateEst:
info := &GenStatsRateEst{}
if err := extractGenStatsRateEst(ad.Bytes(), info); err != nil {
return err
}
stats.RateEst = info
case tcaStatsQueue:
info := &GenStatsQueue{}
if err := extractGnetStatsQueue(ad.Bytes(), info); err != nil {
return err
}
stats.Queue = info
case tcaStatsRateEst64:
info := &GenStatsRateEst64{}
if err := extractGenStatsRateEst64(ad.Bytes(), info); err != nil {
return err
}
stats.RateEst64 = info
case tcaStatsBasicHw:
// ignore it for the moment. TODO
default:
return fmt.Errorf("extractActStats()\t%d\t%v", ad.Type(), ad.Bytes())
}
}
return nil
}
func extractActBpfOptions(data []byte, attr *BPFActionOptions) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
ad.ByteOrder = nativeEndian
for ad.Next() {
switch ad.Type() {
case tcaActBpfTm:
info := &Tcft{}
if err := extractTcft(ad.Bytes(), info); err != nil {
return err
}
attr.Tcft = info
case tcaActBpfParms:
/* struct tc_act_bpf */
case tcaActBpfOpsLen:
attr.OpsLen = ad.Uint16()
case tcaActBpfOps:
attr.Ops = ad.Bytes()
case tcaActBpfFD:
attr.FD = ad.Uint32()
case tcaActBpfName:
attr.Name = ad.String()
default:
return fmt.Errorf("extractActBpfOptions()\t%d\n\t%v", ad.Type(), ad.Bytes())
}
}
return nil
}
func extractClsact(data []byte) error {
return fmt.Errorf("extractClsact()\t%v", data)
}
func extractFqCodelOptions(data []byte, info *FqCodel) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
ad.ByteOrder = nativeEndian
for ad.Next() {
switch ad.Type() {
case tcaFqCodelTarget:
info.Target = ad.Uint32()
case tcaFqCodelLimit:
info.Limit = ad.Uint32()
case tcaFqCodelInterval:
info.Interval = ad.Uint32()
case tcaFqCodelEcn:
info.ECN = ad.Uint32()
case tcaFqCodelFlows:
info.Flows = ad.Uint32()
case tcaFqCodelQuantum:
info.Quantum = ad.Uint32()
case tcaFqCodelCeThreshold:
info.CEThreshold = ad.Uint32()
case tcaFqCodelDropBatchSize:
info.DropBatchSize = ad.Uint32()
case tcaFqCodelMemoryLimit:
info.MemoryLimit = ad.Uint32()
default:
return fmt.Errorf("extractFqCodelOptions()\t%d\n\t%v", ad.Type(), ad.Bytes())
}
}
return nil
}
const (
tcaUnspec = iota
tcaKind
tcaOptions
tcaStats
tcaXstats
tcaRate
tcaFcnt
tcaStats2
tcaStab
tcaPad
tcaDumpInvisible
tcaChain
tcaHwOffload
tcaIngressBlock
tcaEgressBlock
)
const (
tcaFqCodelUnspec = iota
tcaFqCodelTarget
tcaFqCodelLimit
tcaFqCodelInterval
tcaFqCodelEcn
tcaFqCodelFlows
tcaFqCodelQuantum
tcaFqCodelCeThreshold
tcaFqCodelDropBatchSize
tcaFqCodelMemoryLimit
)
const (
tcaBpfUnspec = iota
tcaBpfAct
tcaBpfPolice
tcaBpfClassid
tcaBpfOpsLen
tcaBpfOps
tcaBpfFd
tcaBpfName
tcaBpfFlags
tcaBpfFlagsGen
tcaBpfTag
tcaBpfID
)
const (
tcaActUnspec = iota
tcaActKind
tcaActOptions
tcaActIndex
tcaActStats
tcaActPad
tcaActCookie
)
const (
tcaStatsUnspec = iota
tcaStatsBasic
tcaStatsRateEst
tcaStatsQueue
tcaStatsApp
tcaStatsRateEst64
tcaStatsPAD
tcaStatsBasicHw
)
const (
tcaActBpfUnspec = iota
tcaActBpfTm
tcaActBpfParms
tcaActBpfOpsLen
tcaActBpfOps
tcaActBpfFD
tcaActBpfName
tcaActBpfPad
tcaActBpfTag
tcaActBpfID
)