forked from florianl/go-tc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
q_cake.go
183 lines (175 loc) · 4.91 KB
/
q_cake.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
package tc
import (
"fmt"
"github.com/mdlayher/netlink"
)
const (
tcaCakeUnspec = iota
tcaCakePad
tcaCakeBaseRate64
tcaCakeDiffServMode
tcaCakeAtm
tcaCakeFlowMode
tcaCakeOverhead
tcaCakeRtt
tcaCakeTarget
tcaCakeAutorate
tcaCakeMemory
tcaCakeNat
tcaCakeRaw
tcaCakeWash
tcaCakeMpu
tcaCakeIngress
tcaCakeAckFilter
tcaCakeSplitGso
tcaCakeFwMark
)
// Cake contains attributes of the cake discipline.
// http://man7.org/linux/man-pages/man8/tc-cake.8.html
type Cake struct {
BaseRate *uint64
DiffServMode *uint32
Atm *uint32
FlowMode *uint32
Overhead *uint32
Rtt *uint32
Target *uint32
Autorate *uint32
Memory *uint32
Nat *uint32
Raw *uint32
Wash *uint32
Mpu *uint32
Ingress *uint32
AckFilter *uint32
SplitGso *uint32
FwMark *uint32
}
// unmarshalCake parses the Cake-encoded data and stores the result in the value pointed to by info.
func unmarshalCake(data []byte, info *Cake) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
ad.ByteOrder = nativeEndian
for ad.Next() {
switch ad.Type() {
case tcaCakeBaseRate64:
tmp := ad.Uint64()
info.BaseRate = &tmp
case tcaCakeDiffServMode:
tmp := ad.Uint32()
info.DiffServMode = &tmp
case tcaCakeAtm:
tmp := ad.Uint32()
info.Atm = &tmp
case tcaCakeFlowMode:
tmp := ad.Uint32()
info.FlowMode = &tmp
case tcaCakeOverhead:
tmp := ad.Uint32()
info.Overhead = &tmp
case tcaCakeRtt:
tmp := ad.Uint32()
info.Rtt = &tmp
case tcaCakeTarget:
tmp := ad.Uint32()
info.Target = &tmp
case tcaCakeAutorate:
tmp := ad.Uint32()
info.Autorate = &tmp
case tcaCakeMemory:
tmp := ad.Uint32()
info.Memory = &tmp
case tcaCakeNat:
tmp := ad.Uint32()
info.Nat = &tmp
case tcaCakeRaw:
tmp := ad.Uint32()
info.Raw = &tmp
case tcaCakeWash:
tmp := ad.Uint32()
info.Wash = &tmp
case tcaCakeMpu:
tmp := ad.Uint32()
info.Mpu = &tmp
case tcaCakeIngress:
tmp := ad.Uint32()
info.Ingress = &tmp
case tcaCakeAckFilter:
tmp := ad.Uint32()
info.AckFilter = &tmp
case tcaCakeSplitGso:
tmp := ad.Uint32()
info.SplitGso = &tmp
case tcaCakeFwMark:
tmp := ad.Uint32()
info.FwMark = &tmp
case tcaCakePad:
// padding does not contain data, we just skip it
default:
return fmt.Errorf("unmarshalCake()\t%d\n\t%v", ad.Type(), ad.Bytes())
}
}
return nil
}
// marshalCake returns the binary encoding of Red
func marshalCake(info *Cake) ([]byte, error) {
options := []tcOption{}
if info == nil {
return []byte{}, fmt.Errorf("Cake: %w", ErrNoArg)
}
// TODO: improve logic and check combinations
if info.BaseRate != nil {
options = append(options, tcOption{Interpretation: vtUint64, Type: tcaCakeBaseRate64, Data: *info.BaseRate})
}
if info.DiffServMode != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCakeDiffServMode, Data: *info.DiffServMode})
}
if info.Atm != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCakeAtm, Data: *info.Atm})
}
if info.FlowMode != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCakeFlowMode, Data: *info.FlowMode})
}
if info.Overhead != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCakeOverhead, Data: *info.Overhead})
}
if info.Rtt != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCakeRtt, Data: *info.Rtt})
}
if info.Target != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCakeTarget, Data: *info.Target})
}
if info.Autorate != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCakeAutorate, Data: *info.Autorate})
}
if info.Memory != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCakeMemory, Data: *info.Memory})
}
if info.Nat != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCakeNat, Data: *info.Nat})
}
if info.Raw != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCakeRaw, Data: *info.Raw})
}
if info.Wash != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCakeWash, Data: *info.Wash})
}
if info.Mpu != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCakeMpu, Data: *info.Mpu})
}
if info.Ingress != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCakeIngress, Data: *info.Ingress})
}
if info.AckFilter != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCakeAckFilter, Data: *info.AckFilter})
}
if info.SplitGso != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCakeSplitGso, Data: *info.SplitGso})
}
if info.FwMark != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCakeFwMark, Data: *info.FwMark})
}
return marshalAttributes(options)
}