-
Notifications
You must be signed in to change notification settings - Fork 48
/
m_action.go
370 lines (349 loc) · 10.4 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
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
package tc
import (
"errors"
"fmt"
"github.com/florianl/go-tc/internal/unix"
"github.com/mdlayher/netlink"
)
const (
tcaActUnspec = iota
tcaActKind
tcaActOptions
tcaActIndex
tcaActStats
tcaActPad
tcaActCookie
tcaActFlags
tcaActHwStats
tcaActUsedHwStats
tcaActInHwCount
)
// Various action binding types.
const (
ActBind = 1
ActNoBind = 0
ActUnbind = 1
ActNoUnbind = 0
ActReplace = 1
ActNoReplace = 0
)
// Various action returns.
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
Flags *uint64 // 32-bit bitfield value; 32-bit bitfield selector
HwStats *uint64 // 32-bit bitfield value; 32-bit bitfield selector
UsedHwStats *uint64 // 32-bit bitfield value; 32-bit bitfield selector
InHwCount *uint32
Bpf *ActBpf
ConnMark *Connmark
CSum *Csum
Ct *Ct
CtInfo *CtInfo
Defact *Defact
Gact *Gact
Gate *Gate
Ife *Ife
Ipt *Ipt
Mirred *Mirred
Nat *Nat
Sample *Sample
VLan *VLan
Police *Police
TunnelKey *TunnelKey
MPLS *MPLS
SkbEdit *SkbEdit
}
func unmarshalActions(data []byte, actions *[]*Action) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
for ad.Next() {
action := &Action{}
if err := unmarshalAction(ad.Bytes(), action); err != nil {
return err
}
*actions = append(*actions, action)
}
return ad.Err()
}
// 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
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 tcaActFlags:
flags := ad.Uint64()
info.Flags = &flags
case tcaActHwStats:
hwStats := ad.Uint64()
info.HwStats = &hwStats
case tcaActUsedHwStats:
usedHwStats := ad.Uint64()
info.UsedHwStats = &usedHwStats
case tcaActInHwCount:
inHwCount := ad.Uint32()
info.InHwCount = &inHwCount
case tcaActPad:
// padding does not contain data, we just skip it
default:
return fmt.Errorf("unmarshalAction()\t%d\n\t%v", ad.Type(), ad.Bytes())
}
}
if len(actOptions) > 0 {
if err := extractActOptions(actOptions, info, info.Kind); err != nil {
return err
}
}
return ad.Err()
}
func marshalActions(cmd int, info []*Action) ([]byte, error) {
options := []tcOption{}
for i, action := range info {
data, err := marshalAction(cmd, action, tcaActOptions|nlaFNnested)
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(cmd int, info *Action, actOption uint16) ([]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")
}
var multiError error
// TODO: improve logic and check combinations
switch info.Kind {
case "bpf":
data, err := marshalActBpf(info.Bpf)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
case "connmark":
data, err := marshalConnmark(info.ConnMark)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
case "csum":
data, err := marshalCsum(info.CSum)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
case "ct":
data, err := marshalCt(info.Ct)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
case "ctinfo":
data, err := marshalCtInfo(info.CtInfo)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
case "defact":
data, err := marshalDefact(info.Defact)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
case "gact":
data, err := marshalGact(info.Gact)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
case "gate":
data, err := marshalGate(info.Gate)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
case "ife":
data, err := marshalIfe(info.Ife)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
case "ipt":
data, err := marshalIpt(info.Ipt)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
case "mirred":
data, err := marshalMirred(info.Mirred)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
case "nat":
data, err := marshalNat(info.Nat)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
case "sample":
data, err := marshalSample(info.Sample)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
case "vlan":
data, err := marshalVlan(info.VLan)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
case "police":
data, err := marshalPolice(info.Police)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
case "tunnel_key":
data, err := marshalTunnelKey(info.TunnelKey)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
case "mpls":
data, err := marshalMPLS(info.MPLS)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
case "skbedit":
data, err := marshalSkbEdit(info.SkbEdit)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: actOption, Data: data})
default:
return []byte{}, fmt.Errorf("unknown kind '%s'", info.Kind)
}
options = append(options, tcOption{Interpretation: vtString, Type: tcaActKind, Data: info.Kind})
if multiError != nil && !errors.Is(multiError, ErrNoArg) && cmd != unix.RTM_DELACTION {
return []byte{}, multiError
}
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 {
var multiError error
var err error
switch kind {
case "bpf":
info := &ActBpf{}
err = unmarshalActBpf(data, info)
multiError = concatError(multiError, err)
act.Bpf = info
case "connmark":
info := &Connmark{}
err = unmarshalConnmark(data, info)
multiError = concatError(multiError, err)
act.ConnMark = info
case "csum":
info := &Csum{}
err = unmarshalCsum(data, info)
multiError = concatError(multiError, err)
act.CSum = info
case "ct":
info := &Ct{}
err = unmarshalCt(data, info)
multiError = concatError(multiError, err)
act.Ct = info
case "ctinfo":
info := &CtInfo{}
err = unmarshalCtInfo(data, info)
multiError = concatError(multiError, err)
act.CtInfo = info
case "defact":
info := &Defact{}
err = unmarshalDefact(data, info)
multiError = concatError(multiError, err)
act.Defact = info
case "gact":
info := &Gact{}
err = unmarshalGact(data, info)
multiError = concatError(multiError, err)
act.Gact = info
case "gate":
info := &Gate{}
err = unmarshalGate(data, info)
multiError = concatError(multiError, err)
act.Gate = info
case "ife":
info := &Ife{}
err = unmarshalIfe(data, info)
multiError = concatError(multiError, err)
act.Ife = info
case "ipt":
info := &Ipt{}
err = unmarshalIpt(data, info)
multiError = concatError(multiError, err)
act.Ipt = info
case "mirred":
info := &Mirred{}
err = unmarshalMirred(data, info)
multiError = concatError(multiError, err)
act.Mirred = info
case "nat":
info := &Nat{}
err = unmarshalNat(data, info)
multiError = concatError(multiError, err)
act.Nat = info
case "sample":
info := &Sample{}
err = unmarshalSample(data, info)
multiError = concatError(multiError, err)
act.Sample = info
case "vlan":
info := &VLan{}
err = unmarshalVLan(data, info)
multiError = concatError(multiError, err)
act.VLan = info
case "police":
info := &Police{}
err = unmarshalPolice(data, info)
multiError = concatError(multiError, err)
act.Police = info
case "tunnel_key":
info := &TunnelKey{}
err = unmarshalTunnelKey(data, info)
multiError = concatError(multiError, err)
act.TunnelKey = info
case "mpls":
info := &MPLS{}
err = unmarshalMPLS(data, info)
multiError = concatError(multiError, err)
act.MPLS = info
case "skbedit":
info := &SkbEdit{}
err = unmarshalSkbEdit(data, info)
multiError = concatError(multiError, err)
act.SkbEdit = info
default:
return fmt.Errorf("extractActOptions(): unsupported kind: %s", kind)
}
return multiError
}