-
Notifications
You must be signed in to change notification settings - Fork 48
/
f_flow.go
135 lines (127 loc) · 3.85 KB
/
f_flow.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
package tc
import (
"fmt"
"github.com/mdlayher/netlink"
)
const (
tcaFlowUnspec = iota
tcaFlowKeys
tcaFlowMode
tcaFlowBaseClass
tcaFlowRShift
tcaFlowAddend
tcaFlowMask
tcaFlowXOR
tcaFlowDivisor
tcaFlowAct
tcaFlowPolice
tcaFlowEMatches
tcaFlowPerTurb
)
// Flow contains attributes of the flow discipline
type Flow struct {
Keys *uint32
Mode *uint32
BaseClass *uint32
RShift *uint32
Addend *uint32
Mask *uint32
XOR *uint32
Divisor *uint32
PerTurb *uint32
Ematch *Ematch
Actions *[]*Action
}
// unmarshalFlow parses the Flow-encoded data and stores the result in the value pointed to by info.
func unmarshalFlow(data []byte, info *Flow) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
var multiError error
for ad.Next() {
switch ad.Type() {
case tcaFlowKeys:
info.Keys = uint32Ptr(ad.Uint32())
case tcaFlowMode:
info.Mode = uint32Ptr(ad.Uint32())
case tcaFlowBaseClass:
info.BaseClass = uint32Ptr(ad.Uint32())
case tcaFlowRShift:
info.RShift = uint32Ptr(ad.Uint32())
case tcaFlowAddend:
info.Addend = uint32Ptr(ad.Uint32())
case tcaFlowMask:
info.Mask = uint32Ptr(ad.Uint32())
case tcaFlowXOR:
info.XOR = uint32Ptr(ad.Uint32())
case tcaFlowDivisor:
info.Divisor = uint32Ptr(ad.Uint32())
case tcaFlowPerTurb:
info.PerTurb = uint32Ptr(ad.Uint32())
case tcaFlowEMatches:
ematch := &Ematch{}
err := unmarshalEmatch(ad.Bytes(), ematch)
multiError = concatError(multiError, err)
info.Ematch = ematch
case tcaFlowAct:
actions := &[]*Action{}
err := unmarshalActions(ad.Bytes(), actions)
multiError = concatError(multiError, err)
info.Actions = actions
default:
return fmt.Errorf("unmarshalFlow()\t%d\n\t%v", ad.Type(), ad.Bytes())
}
}
return concatError(multiError, ad.Err())
}
// marshalFlow returns the binary encoding of Flow
func marshalFlow(info *Flow) ([]byte, error) {
options := []tcOption{}
if info == nil {
return []byte{}, fmt.Errorf("Flow: %w", ErrNoArg)
}
var multiError error
// TODO: improve logic and check combinations
if info.Keys != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaFlowKeys, Data: uint32Value(info.Keys)})
}
if info.Mode != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaFlowMode, Data: uint32Value(info.Mode)})
}
if info.BaseClass != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaFlowBaseClass, Data: uint32Value(info.BaseClass)})
}
if info.RShift != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaFlowRShift, Data: uint32Value(info.RShift)})
}
if info.Addend != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaFlowAddend, Data: uint32Value(info.Addend)})
}
if info.Mask != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaFlowMask, Data: uint32Value(info.Mask)})
}
if info.XOR != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaFlowXOR, Data: uint32Value(info.XOR)})
}
if info.Divisor != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaFlowDivisor, Data: uint32Value(info.Divisor)})
}
if info.PerTurb != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaFlowPerTurb, Data: uint32Value(info.PerTurb)})
}
if info.Ematch != nil {
data, err := marshalEmatch(info.Ematch)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaFlowEMatches, Data: data})
}
if info.Actions != nil {
data, err := marshalActions(0, *info.Actions)
multiError = concatError(multiError, err)
options = append(options, tcOption{Interpretation: vtBytes, Type: tcaFlowAct, Data: data})
}
if multiError != nil {
return []byte{}, multiError
}
return marshalAttributes(options)
}