-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathactions.go
159 lines (140 loc) · 3.32 KB
/
actions.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package tc
import (
"fmt"
"github.com/florianl/go-tc/internal/unix"
"github.com/mdlayher/netlink"
)
// Actions represents the actions part of rtnetlink
type Actions struct {
Tc
}
// tcamsg is Actions specific
type tcaMsg struct {
Family uint8
Pad1 uint8
Pad2 uint16
}
// Actions allows to read and alter actions
func (tc *Tc) Actions() *Actions {
return &Actions{*tc}
}
// Add creates a new actions
func (a *Actions) Add(info []*Action) error {
if len(info) == 0 {
return ErrNoArg
}
options, err := validateActionsObject(unix.RTM_NEWACTION, info)
if err != nil {
return err
}
return a.action(unix.RTM_NEWACTION, netlink.Create|netlink.Excl, tcaMsg{
Family: unix.AF_UNSPEC,
}, options)
}
// Replace add/remove an actions. If the node does not exist yet it is created
func (a *Actions) Replace(info []*Action) error {
if len(info) == 0 {
return ErrNoArg
}
options, err := validateActionsObject(unix.RTM_NEWACTION, info)
if err != nil {
return err
}
return a.action(unix.RTM_NEWACTION, netlink.Create, tcaMsg{
Family: unix.AF_UNSPEC,
}, options)
}
// Delete removes an actions
func (a *Actions) Delete(info []*Action) error {
if len(info) == 0 {
return ErrNoArg
}
options, err := validateActionsObject(unix.RTM_DELACTION, info)
if err != nil {
return err
}
return a.action(unix.RTM_DELACTION, netlink.HeaderFlags(0), tcaMsg{
Family: unix.AF_UNSPEC,
}, options)
}
// Get fetches all actions
func (a *Actions) Get(actions []*Action) ([]*Action, error) {
var results []*Action
var data []byte
tcminfo, err := marshalStruct(tcaMsg{
Family: unix.AF_UNSPEC,
})
if err != nil {
return results, err
}
data = append(data, tcminfo...)
options, err := validateActionsObject(unix.RTM_GETACTION, actions)
if err != nil {
return results, err
}
attrs, err := marshalAttributes(options)
if err != nil {
return results, err
}
data = append(data, attrs...)
req := netlink.Message{
Header: netlink.Header{
Type: netlink.HeaderType(unix.RTM_GETACTION),
Flags: netlink.Request | netlink.Dump,
},
Data: data,
}
msgs, err := a.query(req)
if err != nil {
return results, err
}
for _, msg := range msgs {
// The first 4 bytes contain tcaMsg - which is skipped here.
if err := unmarshalRoot(msg.Data[4:], &results); err != nil {
return results, err
}
}
return results, nil
}
func validateActionsObject(cmd int, info []*Action) ([]tcOption, error) {
options := []tcOption{}
data, err := marshalActions(cmd, info)
if err != nil {
return options, err
}
options = append(options, tcOption{Interpretation: vtBytes, Type: 1 /*TCA_ROOT_TAB*/, Data: data})
return options, nil
}
const (
tcaRootUnspec = iota
tcaRootTab
tcaRootFlags
tcaRootCount
tcaRootTimeDelta
tcaRootExtWarnMsg
)
func unmarshalRoot(data []byte, actions *[]*Action) error {
ad, err := netlink.NewAttributeDecoder(data)
if err != nil {
return err
}
var multiError error
for ad.Next() {
switch ad.Type() {
case tcaRootTab:
err := unmarshalActions(ad.Bytes(), actions)
multiError = concatError(multiError, err)
case tcaRootFlags:
_ = ad.Uint64()
case tcaRootCount:
_ = ad.Uint32()
case tcaRootTimeDelta:
_ = ad.Uint32()
case tcaRootExtWarnMsg:
_ = ad.String()
default:
return fmt.Errorf("unmarshalRoot()\t%d\n\t%v", ad.Type(), ad.Bytes())
}
}
return concatError(multiError, ad.Err())
}