forked from florianl/go-tc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
m_action.go
345 lines (326 loc) · 8.33 KB
/
m_action.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
package tc
import (
"fmt"
"os"
"github.com/mdlayher/netlink"
)
const (
tcaActUnspec = iota
tcaActKind
tcaActOptions
tcaActIndex
tcaActStats
tcaActPad
tcaActCookie
)
const (
ActBind = 1
ActNoBind = 0
ActUnbind = 1
ActNoUnbind = 0
ActReplace = 1
ActNoReplace = 0
)
const (
ActOk = 0
ActReclassify = 1
ActShot = 2
ActPipe = 3
ActStolen = 4
ActQueued = 5
ActRepeat = 6
ActRedirect = 7
ActTrap = 8
)
// Action represents action attributes of various filters and classes
type Action struct {
Kind string
Index uint32
Stats *GenStats
Cookie *[]byte
Bpf *ActBpf
ConnMark *Connmark
CSum *Csum
Defact *Defact
Gate *Gate
Ife *Ife
Ipt *Ipt
Mirred *Mirred
Nat *Nat
Sample *Sample
VLan *VLan
Police *Police
TunnelKey *TunnelKey
Gact *Gact
}
func unmarshalActions(data []byte, actions *[]*Action) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
ad.ByteOrder = nativeEndian
for ad.Next() {
action := &Action{}
if err := unmarshalAction(ad.Bytes(), action); err != nil {
return err
}
*actions = append(*actions, action)
}
return nil
}
// unmarshalAction parses the Action-encoded data and stores the result in the value pointed to by info.
func unmarshalAction(data []byte, info *Action) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
var actOptions []byte
ad.ByteOrder = nativeEndian
for ad.Next() {
switch ad.Type() {
case tcaActKind:
info.Kind = ad.String()
case tcaActIndex:
info.Index = ad.Uint32()
case tcaActOptions:
actOptions = ad.Bytes()
case tcaActCookie:
tmp := ad.Bytes()
info.Cookie = &tmp
case tcaActStats:
stats := &GenStats{}
if err := unmarshalGenStats(ad.Bytes(), stats); err != nil {
return err
}
info.Stats = stats
case tcaActPad:
// padding does not contain data, we just skip it
default:
// Igore and keep going if an unknown type is parsed.
// TODO: this Fprintln should be logging if we actually need to log this. Logging might be excessive and it's not known if this is important info.
fmt.Fprintln(os.Stderr, "unmarshalAction() ignoring data with unknown type:", ad.Type(), ad.Bytes())
}
}
if len(actOptions) > 0 {
if err := extractActOptions(actOptions, info, info.Kind); err != nil {
return err
}
}
return nil
}
func marshalActions(info []*Action) ([]byte, error) {
options := []tcOption{}
for i, action := range info {
data, err := marshalAction(action)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: uint16(i + 1), Data: data})
}
return marshalAttributes(options)
}
// marshalAction returns the binary encoding of Action
func marshalAction(info *Action) ([]byte, error) {
options := []tcOption{}
if info == nil {
return []byte{}, fmt.Errorf("Action: %w", ErrNoArg)
}
if len(info.Kind) == 0 {
return []byte{}, fmt.Errorf("kind is missing")
}
// TODO: improve logic and check combinations
switch info.Kind {
case "bpf":
data, err := marshalActBpf(info.Bpf)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaActOptions, Data: data})
case "connmark":
data, err := marshalConnmark(info.ConnMark)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaActOptions, Data: data})
case "csum":
data, err := marshalCsum(info.CSum)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaActOptions, Data: data})
case "defact":
data, err := marshalDefact(info.Defact)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaActOptions, Data: data})
case "gate":
data, err := marshalGate(info.Gate)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaActOptions, Data: data})
case "ife":
data, err := marshalIfe(info.Ife)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaActOptions, Data: data})
case "ipt":
data, err := marshalIpt(info.Ipt)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaActOptions, Data: data})
case "mirred":
data, err := marshalMirred(info.Mirred)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaActOptions, Data: data})
case "nat":
data, err := marshalNat(info.Nat)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaActOptions, Data: data})
case "sample":
data, err := marshalSample(info.Sample)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaActOptions, Data: data})
case "vlan":
data, err := marshalVlan(info.VLan)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaActOptions, Data: data})
case "police":
data, err := marshalPolice(info.Police)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaActOptions, Data: data})
case "tunnel_key":
data, err := marshalTunnelKey(info.TunnelKey)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaActOptions, Data: data})
case "gact":
data, err := marshalGact(info.Gact)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaActOptions, Data: data})
default:
return []byte{}, fmt.Errorf("unknown kind '%s'", info.Kind)
}
options = append(options, tcOption{Interpretation: vtString, Type: tcaActKind, Data: info.Kind})
if info.Index != 0 {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaActIndex, Data: info.Index})
}
if info.Stats != nil {
data, err := marshalGenStats(info.Stats)
if err != nil {
return []byte{}, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaActStats, Data: data})
}
if info.Cookie != nil {
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaActCookie, Data: bytesValue(info.Cookie)})
}
return marshalAttributes(options)
}
func extractActOptions(data []byte, act *Action, kind string) error {
switch kind {
case "bpf":
info := &ActBpf{}
if err := unmarshalActBpf(data, info); err != nil {
return err
}
act.Bpf = info
case "connmark":
info := &Connmark{}
if err := unmarshalConnmark(data, info); err != nil {
return err
}
act.ConnMark = info
case "csum":
info := &Csum{}
if err := unmarshalCsum(data, info); err != nil {
return err
}
act.CSum = info
case "defact":
info := &Defact{}
if err := unmarshalDefact(data, info); err != nil {
return err
}
act.Defact = info
case "gate":
info := &Gate{}
if err := unmarshalGate(data, info); err != nil {
return err
}
act.Gate = info
case "ife":
info := &Ife{}
if err := unmarshalIfe(data, info); err != nil {
return err
}
act.Ife = info
case "ipt":
info := &Ipt{}
if err := unmarshalIpt(data, info); err != nil {
return err
}
act.Ipt = info
case "mirred":
info := &Mirred{}
if err := unmarshalMirred(data, info); err != nil {
return err
}
act.Mirred = info
case "nat":
info := &Nat{}
if err := unmarshalNat(data, info); err != nil {
return err
}
act.Nat = info
case "sample":
info := &Sample{}
if err := unmarshalSample(data, info); err != nil {
return err
}
act.Sample = info
case "vlan":
info := &VLan{}
if err := unmarshalVLan(data, info); err != nil {
return err
}
act.VLan = info
case "police":
info := &Police{}
if err := unmarshalPolice(data, info); err != nil {
return err
}
act.Police = info
case "tunnel_key":
info := &TunnelKey{}
if err := unmarshalTunnelKey(data, info); err != nil {
return err
}
act.TunnelKey = info
case "gact":
info := &Gact{}
if err := unmarshalGact(data, info); err != nil {
return err
}
act.Gact = info
default:
return fmt.Errorf("extractActOptions(): unsupported kind: %s", kind)
}
return nil
}