-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathq_dsmark.go
77 lines (69 loc) · 2.08 KB
/
q_dsmark.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 (
tcaDsmarkUnspec = iota
tcaDsmarkIndices
tcaDsmarkDefaultIndex
tcaDsmarkSetTCIndex
tcaDsmarkMask
tcaDsmarkValue
)
// Dsmark contains attributes of the dsmark discipline
type Dsmark struct {
Indices *uint16
DefaultIndex *uint16
SetTCIndex *bool
Mask *uint8
Value *uint8
}
// unmarshalDsmark parses the Dsmark-encoded data and stores the result in the value pointed to by info.
func unmarshalDsmark(data []byte, info *Dsmark) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
for ad.Next() {
switch ad.Type() {
case tcaDsmarkIndices:
info.Indices = uint16Ptr(ad.Uint16())
case tcaDsmarkDefaultIndex:
info.DefaultIndex = uint16Ptr(ad.Uint16())
case tcaDsmarkSetTCIndex:
info.SetTCIndex = boolPtr(ad.Flag())
case tcaDsmarkMask:
info.Mask = uint8Ptr(ad.Uint8())
case tcaDsmarkValue:
info.Value = uint8Ptr(ad.Uint8())
default:
return fmt.Errorf("UnmarshalDsmark()\t%d\n\t%v", ad.Type(), ad.Bytes())
}
}
return ad.Err()
}
// marshalDsmark returns the binary encoding of Qfq
func marshalDsmark(info *Dsmark) ([]byte, error) {
options := []tcOption{}
if info == nil {
return []byte{}, fmt.Errorf("Dsmark: %w", ErrNoArg)
}
// TODO: improve logic and check combinations
if info.Indices != nil {
options = append(options, tcOption{Interpretation: vtUint16, Type: tcaDsmarkIndices, Data: uint16Value(info.Indices)})
}
if info.DefaultIndex != nil {
options = append(options, tcOption{Interpretation: vtUint16, Type: tcaDsmarkDefaultIndex, Data: uint16Value(info.DefaultIndex)})
}
if info.Mask != nil {
options = append(options, tcOption{Interpretation: vtUint8, Type: tcaDsmarkMask, Data: uint8Value(info.Mask)})
}
if info.Value != nil {
options = append(options, tcOption{Interpretation: vtUint8, Type: tcaDsmarkValue, Data: uint8Value(info.Value)})
}
if info.SetTCIndex != nil {
options = append(options, tcOption{Interpretation: vtFlag, Type: tcaDsmarkSetTCIndex, Data: boolValue(info.SetTCIndex)})
}
return marshalAttributes(options)
}