forked from florianl/go-tc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathf_tcindex.go
80 lines (71 loc) · 2.12 KB
/
f_tcindex.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
package tc
import (
"fmt"
"github.com/mdlayher/netlink"
)
const (
tcaTcIndexUnspec = iota
tcaTcIndexHash
tcaTcIndexMask
tcaTcIndexShift
tcaTcIndexFallThrough
tcaTcIndexClassID
tcaTcIndexPolice
tcaTcIndexAct
)
// TcIndex contains attributes of the tcIndex discipline
type TcIndex struct {
Hash *uint32
Mask *uint16
Shift *uint32
FallThrough *uint32
ClassID *uint32
}
// marshalTcIndex returns the binary encoding of TcIndex
func marshalTcIndex(info *TcIndex) ([]byte, error) {
options := []tcOption{}
if info == nil {
return []byte{}, fmt.Errorf("TcIndex: %w", ErrNoArg)
}
// TODO: improve logic and check combinations
if info.Hash != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaTcIndexHash, Data: uint32Value(info.Hash)})
}
if info.Mask != nil {
options = append(options, tcOption{Interpretation: vtUint16, Type: tcaTcIndexMask, Data: uint16Value(info.Mask)})
}
if info.Shift != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaTcIndexShift, Data: uint32Value(info.Shift)})
}
if info.FallThrough != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaTcIndexFallThrough, Data: uint32Value(info.FallThrough)})
}
if info.ClassID != nil {
options = append(options, tcOption{Interpretation: vtUint32, Type: tcaTcIndexClassID, Data: uint32Value(info.ClassID)})
}
return marshalAttributes(options)
}
// unmarshalTcIndex parses the TcIndex-encoded data and stores the result in the value pointed to by info.
func unmarshalTcIndex(data []byte, info *TcIndex) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
for ad.Next() {
switch ad.Type() {
case tcaTcIndexHash:
info.Hash = uint32Ptr(ad.Uint32())
case tcaTcIndexMask:
info.Mask = uint16Ptr(ad.Uint16())
case tcaTcIndexShift:
info.Shift = uint32Ptr(ad.Uint32())
case tcaTcIndexFallThrough:
info.FallThrough = uint32Ptr(ad.Uint32())
case tcaTcIndexClassID:
info.ClassID = uint32Ptr(ad.Uint32())
default:
return fmt.Errorf("unmarshalTcIndex()\t%d\n\t%v", ad.Type(), ad.Bytes())
}
}
return ad.Err()
}