forked from florianl/go-tc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ematch_cmp.go
71 lines (62 loc) · 1.4 KB
/
ematch_cmp.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
package tc
// CmpMatchAlign defines byte alignments.
type CmpMatchAlign uint8
// Various byte alignments.
const (
CmpMatchU8 = CmpMatchAlign(1)
CmpMatchU16 = CmpMatchAlign(2)
CmpMatchU32 = CmpMatchAlign(4)
)
// CmpMatchFlag defines available flags.
type CmpMatchFlag uint8
// Various flags.
const (
CmpMatchTrans = CmpMatchFlag(1)
)
// CmpMatch contains attributes of the cmp match discipline
type CmpMatch struct {
Val uint32
Mask uint32
Off uint16
Align CmpMatchAlign
Flags CmpMatchFlag
Layer EmatchLayer
Opnd EmatchOpnd
}
type cmpMatch struct {
Val uint32
Mask uint32
Off uint16
Opts uint16
}
func unmarshalCmpMatch(data []byte, info *CmpMatch) error {
tmp := cmpMatch{}
if err := unmarshalStruct(data, &tmp); err != nil {
return err
}
info.Val = tmp.Val
info.Mask = tmp.Mask
info.Off = tmp.Off
info.Align = CmpMatchAlign((tmp.Opts) & 0xf)
info.Flags = CmpMatchFlag((tmp.Opts >> 4) & 0xf)
info.Layer = EmatchLayer((tmp.Opts >> 8) & 0xf)
info.Opnd = EmatchOpnd((tmp.Opts >> 12) & 0xf)
return nil
}
func marshalCmpMatch(info *CmpMatch) ([]byte, error) {
if info == nil {
return []byte{}, ErrNoArg
}
var opts uint16
opts |= uint16(info.Align & 0xf)
opts |= uint16(info.Flags&0xf) << 4
opts |= uint16(info.Layer&0xf) << 8
opts |= uint16(info.Opnd&0xf) << 12
tmp := cmpMatch{
Val: info.Val,
Mask: info.Mask,
Off: info.Off,
Opts: opts,
}
return marshalStruct(&tmp)
}