-
Notifications
You must be signed in to change notification settings - Fork 842
Expand file tree
/
Copy pathethtool_features_linux.go
More file actions
276 lines (258 loc) · 7.95 KB
/
Copy pathethtool_features_linux.go
File metadata and controls
276 lines (258 loc) · 7.95 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package netlink
import (
"fmt"
"sort"
"strings"
"syscall"
"github.com/vishvananda/netlink/nl"
"golang.org/x/sys/unix"
)
// NetDevFeature contains the state of one netdevice feature.
type NetDevFeature struct {
Hardware bool
Wanted bool
Active bool
NoChange bool
}
func parseNetDevFeatureBitset(attr syscall.NetlinkRouteAttr) (map[string]bool, error) {
attrs, err := nl.ParseRouteAttr(attr.Value)
if err != nil {
return nil, err
}
noMask := false
var bits *syscall.NetlinkRouteAttr
for i := range attrs {
typeID := attrs[i].Attr.Type & nl.NLA_TYPE_MASK
switch typeID {
case nl.ETHTOOL_A_BITSET_NOMASK:
if len(attrs[i].Value) != 0 {
return nil, fmt.Errorf("netlink: ethtool bitset nomask flag has %d bytes, want 0", len(attrs[i].Value))
}
noMask = true
case nl.ETHTOOL_A_BITSET_BITS:
bits = &attrs[i]
case nl.ETHTOOL_A_BITSET_VALUE, nl.ETHTOOL_A_BITSET_MASK:
return nil, fmt.Errorf("netlink: compact ethtool feature bitset is not supported")
}
}
if bits == nil {
return nil, fmt.Errorf("netlink: ethtool feature bitset has no named bits")
}
entries, err := nl.ParseRouteAttr(bits.Value)
if err != nil {
return nil, err
}
result := make(map[string]bool, len(entries))
for _, entry := range entries {
if entry.Attr.Type&nl.NLA_TYPE_MASK != nl.ETHTOOL_A_BITSET_BITS_BIT {
continue
}
fields, err := nl.ParseRouteAttr(entry.Value)
if err != nil {
return nil, err
}
name := ""
namePresent := false
value := noMask
for _, field := range fields {
switch field.Attr.Type & nl.NLA_TYPE_MASK {
case nl.ETHTOOL_A_BITSET_BIT_NAME:
namePresent = true
nameBytes := field.Value
if len(nameBytes) != 0 && nameBytes[len(nameBytes)-1] == 0 {
nameBytes = nameBytes[:len(nameBytes)-1]
}
if strings.IndexByte(string(nameBytes), 0) >= 0 {
return nil, fmt.Errorf("netlink: malformed ethtool feature name %x", field.Value)
}
name = string(nameBytes)
case nl.ETHTOOL_A_BITSET_BIT_VALUE:
if len(field.Value) != 0 {
return nil, fmt.Errorf("netlink: ethtool feature value flag has %d bytes, want 0", len(field.Value))
}
value = true
}
}
if !namePresent {
return nil, fmt.Errorf("netlink: ethtool feature bit has no name")
}
if name == "" {
continue
}
if _, exists := result[name]; exists {
return nil, fmt.Errorf("netlink: duplicate ethtool feature %q", name)
}
result[name] = value
}
return result, nil
}
func parseNetDevFeatures(attrs []syscall.NetlinkRouteAttr) (map[string]NetDevFeature, error) {
result := make(map[string]NetDevFeature)
for _, attr := range attrs {
typeID := attr.Attr.Type & nl.NLA_TYPE_MASK
if typeID < nl.ETHTOOL_A_FEATURES_HW || typeID > nl.ETHTOOL_A_FEATURES_NOCHANGE {
continue
}
values, err := parseNetDevFeatureBitset(attr)
if err != nil {
return nil, err
}
for name, value := range values {
feature := result[name]
switch typeID {
case nl.ETHTOOL_A_FEATURES_HW:
feature.Hardware = value
case nl.ETHTOOL_A_FEATURES_WANTED:
feature.Wanted = value
case nl.ETHTOOL_A_FEATURES_ACTIVE:
feature.Active = value
case nl.ETHTOOL_A_FEATURES_NOCHANGE:
feature.NoChange = value
}
result[name] = feature
}
}
return result, nil
}
func netDevFeaturesSetError(unapplied map[string]bool) error {
if len(unapplied) == 0 {
return nil
}
names := make([]string, 0, len(unapplied))
for name := range unapplied {
names = append(names, name)
}
sort.Strings(names)
changes := make([]string, 0, len(names))
for _, name := range names {
state := "off"
if unapplied[name] {
state = "on"
}
changes = append(changes, name+"="+state)
}
return fmt.Errorf("netlink: ethtool did not apply requested feature changes: %s", strings.Join(changes, ", "))
}
func verifyNetDevFeatureStates(config map[string]bool, features map[string]NetDevFeature) error {
unapplied := make(map[string]bool)
for name, requested := range config {
feature, exists := features[name]
if !exists || feature.Active != requested {
unapplied[name] = requested
}
}
return netDevFeaturesSetError(unapplied)
}
func parseNetDevFeaturesSetReply(attrs []syscall.NetlinkRouteAttr) error {
var unapplied map[string]bool
wantedSeen := false
activeSeen := false
for _, attr := range attrs {
typeID := attr.Attr.Type & nl.NLA_TYPE_MASK
switch typeID {
case nl.ETHTOOL_A_FEATURES_WANTED:
if wantedSeen {
return fmt.Errorf("netlink: duplicate ethtool features set wanted bitset")
}
wantedSeen = true
values, err := parseNetDevFeatureBitset(attr)
if err != nil {
return err
}
unapplied = values
case nl.ETHTOOL_A_FEATURES_ACTIVE:
if activeSeen {
return fmt.Errorf("netlink: duplicate ethtool features set active bitset")
}
activeSeen = true
if _, err := parseNetDevFeatureBitset(attr); err != nil {
return err
}
}
}
if !wantedSeen {
return fmt.Errorf("netlink: ethtool features set reply has no wanted bitset")
}
if !activeSeen {
return fmt.Errorf("netlink: ethtool features set reply has no active bitset")
}
return netDevFeaturesSetError(unapplied)
}
// NetDevFeaturesGet returns the named feature states for ifIndex.
func NetDevFeaturesGet(ifIndex int) (map[string]NetDevFeature, error) {
return pkgHandle().NetDevFeaturesGet(ifIndex)
}
// NetDevFeaturesGet returns the named feature states for ifIndex.
func (h *Handle) NetDevFeaturesGet(ifIndex int) (map[string]NetDevFeature, error) {
header, err := newEthtoolHeader(nl.ETHTOOL_A_FEATURES_HEADER, ifIndex)
if err != nil {
return nil, err
}
msgs, err := h.ethtoolRequest(nl.ETHTOOL_MSG_FEATURES_GET, unix.NLM_F_ACK, []*nl.RtAttr{header})
if err != nil {
return nil, err
}
if len(msgs) != 1 {
return nil, fmt.Errorf("netlink: expected one ethtool features response, got %d", len(msgs))
}
return parseNetDevFeatures(msgs[0])
}
func newNetDevFeaturesSetAttrs(ifIndex int, config map[string]bool) ([]*nl.RtAttr, error) {
header, err := newEthtoolHeader(nl.ETHTOOL_A_FEATURES_HEADER, ifIndex)
if err != nil {
return nil, err
}
if len(config) == 0 {
return nil, fmt.Errorf("netlink: ethtool feature configuration is empty")
}
names := make([]string, 0, len(config))
for name := range config {
if name == "" || strings.IndexByte(name, 0) >= 0 {
return nil, fmt.Errorf("netlink: invalid ethtool feature name %q", name)
}
names = append(names, name)
}
sort.Strings(names)
wanted := nl.NewRtAttr(unix.NLA_F_NESTED|nl.ETHTOOL_A_FEATURES_WANTED, nil)
bits := wanted.AddRtAttr(unix.NLA_F_NESTED|nl.ETHTOOL_A_BITSET_BITS, nil)
for _, name := range names {
bit := bits.AddRtAttr(unix.NLA_F_NESTED|nl.ETHTOOL_A_BITSET_BITS_BIT, nil)
bit.AddRtAttr(nl.ETHTOOL_A_BITSET_BIT_NAME, nl.ZeroTerminated(name))
if config[name] {
bit.AddRtAttr(nl.ETHTOOL_A_BITSET_BIT_VALUE, nil)
}
}
return []*nl.RtAttr{header, wanted}, nil
}
// NetDevFeaturesSet applies named feature changes to ifIndex. It returns an
// error if any requested state is not active. Features absent from config are
// left unchanged.
func NetDevFeaturesSet(ifIndex int, config map[string]bool) error {
return pkgHandle().NetDevFeaturesSet(ifIndex, config)
}
// NetDevFeaturesSet applies named feature changes to ifIndex. It returns an
// error if any requested state is not active. Features absent from config are
// left unchanged.
func (h *Handle) NetDevFeaturesSet(ifIndex int, config map[string]bool) error {
attrs, err := newNetDevFeaturesSetAttrs(ifIndex, config)
if err != nil {
return err
}
msgs, err := h.ethtoolRequest(nl.ETHTOOL_MSG_FEATURES_SET, unix.NLM_F_ACK, attrs)
if err != nil {
return err
}
switch len(msgs) {
case 0:
// Older kernels omit the reply when the wanted bitmap is unchanged.
features, err := h.NetDevFeaturesGet(ifIndex)
if err != nil {
return fmt.Errorf("netlink: verify ethtool feature changes: %w", err)
}
return verifyNetDevFeatureStates(config, features)
case 1:
return parseNetDevFeaturesSetReply(msgs[0])
default:
return fmt.Errorf("netlink: expected at most one ethtool features set response, got %d", len(msgs))
}
}