forked from florianl/go-tc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nest.go
112 lines (102 loc) · 2.81 KB
/
nest.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
package tc
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/mdlayher/netlink"
)
type valueType int
const (
vtUint8 valueType = iota
vtUint16
vtUint32
vtUint64
vtString
vtBytes
vtFlag
vtInt8
vtInt16
vtInt32
vtInt64
vtUint16Be
vtUint32Be
vtUint64Be
)
type tcOption struct {
Interpretation valueType
Type uint16
Data interface{}
}
// NLA_F_NESTED from include/uapi/linux/netlink.h
const nlaFNnested = (1 << 15)
func marshalAttributes(options []tcOption) ([]byte, error) {
ad := netlink.NewAttributeEncoder()
for _, option := range options {
switch option.Interpretation {
case vtUint8:
ad.Uint8(option.Type, (option.Data).(uint8))
case vtUint16:
ad.Uint16(option.Type, (option.Data).(uint16))
case vtUint32:
ad.Uint32(option.Type, (option.Data).(uint32))
case vtUint64:
ad.Uint64(option.Type, (option.Data).(uint64))
case vtString:
ad.String(option.Type, (option.Data).(string))
case vtBytes:
ad.Bytes(option.Type, (option.Data).([]byte))
case vtFlag:
ad.Flag(option.Type, true)
case vtInt8:
data := bytes.NewBuffer(make([]byte, 0, 1))
if err := binary.Write(data, nativeEndian, (option.Data).(int8)); err != nil {
return []byte{}, err
}
ad.Bytes(option.Type, data.Bytes())
case vtInt16:
data := bytes.NewBuffer(make([]byte, 0, 2))
if err := binary.Write(data, nativeEndian, (option.Data).(int16)); err != nil {
return []byte{}, err
}
ad.Bytes(option.Type, data.Bytes())
case vtInt32:
data := bytes.NewBuffer(make([]byte, 0, 4))
if err := binary.Write(data, nativeEndian, (option.Data).(int32)); err != nil {
return []byte{}, err
}
ad.Bytes(option.Type, data.Bytes())
case vtInt64:
data := bytes.NewBuffer(make([]byte, 0, 8))
if err := binary.Write(data, nativeEndian, (option.Data).(int64)); err != nil {
return []byte{}, err
}
ad.Bytes(option.Type, data.Bytes())
case vtUint16Be:
data := bytes.NewBuffer(make([]byte, 0, 2))
if err := binary.Write(data, binary.BigEndian, (option.Data).(uint16)); err != nil {
return []byte{}, err
}
ad.Bytes(option.Type, data.Bytes())
case vtUint32Be:
data := bytes.NewBuffer(make([]byte, 0, 4))
if err := binary.Write(data, binary.BigEndian, (option.Data).(uint32)); err != nil {
return []byte{}, err
}
ad.Bytes(option.Type, data.Bytes())
case vtUint64Be:
data := bytes.NewBuffer(make([]byte, 0, 8))
if err := binary.Write(data, binary.BigEndian, (option.Data).(uint64)); err != nil {
return []byte{}, err
}
ad.Bytes(option.Type, data.Bytes())
default:
return []byte{}, fmt.Errorf("unknown interpretation: %d", option.Interpretation)
}
}
return ad.Encode()
}
func unmarshalNetlinkAttribute(data []byte, val interface{}) error {
buf := bytes.NewReader(data)
err := binary.Read(buf, nativeEndian, val)
return err
}