-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathq_codel.go
77 lines (69 loc) · 2.04 KB
/
q_codel.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
package tc
import (
"fmt"
"github.com/mdlayher/netlink"
)
const (
tcaCodelUnspec = iota
tcaCodelTarget
tcaCodelLimit
tcaCodelInterval
tcaCodelECN
tcaCodelCEThreshold
)
// Codel contains attributes of the codel discipline
type Codel struct {
Target *uint32
Limit *uint32
Interval *uint32
ECN *uint32
CEThreshold *uint32
}
// unmarshalCodel parses the Codel-encoded data and stores the result in the value pointed to by info.
func unmarshalCodel(data []byte, info *Codel) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
for ad.Next() {
switch ad.Type() {
case tcaCodelTarget:
info.Target = uint32Ptr(ad.Uint32())
case tcaCodelLimit:
info.Limit = uint32Ptr(ad.Uint32())
case tcaCodelInterval:
info.Interval = uint32Ptr(ad.Uint32())
case tcaCodelECN:
info.ECN = uint32Ptr(ad.Uint32())
case tcaCodelCEThreshold:
info.CEThreshold = uint32Ptr(ad.Uint32())
default:
return fmt.Errorf("unmarshalCodel()\t%d\n\t%v", ad.Type(), ad.Bytes())
}
}
return ad.Err()
}
// marshalCodel returns the binary encoding of Red
func marshalCodel(info *Codel) ([]byte, error) {
options := []tcOption{}
if info == nil {
return []byte{}, fmt.Errorf("Codel: %w", ErrNoArg)
}
// TODO: improve logic and check combinations
if info.Target != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCodelTarget, Data: uint32Value(info.Target)})
}
if info.Limit != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCodelLimit, Data: uint32Value(info.Limit)})
}
if info.Interval != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCodelInterval, Data: uint32Value(info.Interval)})
}
if info.ECN != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCodelECN, Data: uint32Value(info.ECN)})
}
if info.CEThreshold != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaCodelCEThreshold, Data: uint32Value(info.CEThreshold)})
}
return marshalAttributes(options)
}