forked from mdlayher/ethtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
288 lines (245 loc) · 7.48 KB
/
client.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
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
277
278
279
280
281
282
283
284
285
286
287
288
package ethtool
import (
"fmt"
)
//go:generate stringer -type=Duplex,Port -output=string.go
//go:generate go run mklinkmodes.go
var (
_ error = &Error{}
// Ensure compatibility with Go 1.13+ errors package.
_ interface{ Unwrap() error } = &Error{}
)
// An Error is an error value produced by the kernel due to a bad ethtool
// netlink request. Typically the Err will be of type *netlink.OpError.
type Error struct {
Message string
Err error
}
// Error implements error.
func (e *Error) Error() string {
// This typically wraps a *netlink.OpError which will contain the error
// string anyway, so just return the inner error's string.
return e.Err.Error()
}
// Unwrap unwraps the internal Err field for use with errors.Unwrap.
func (e *Error) Unwrap() error { return e.Err }
// A Client can manipulate the ethtool netlink interface.
type Client struct {
// The operating system-specific client.
c *client
}
// New creates a Client which can issue ethtool commands.
func New() (*Client, error) {
c, err := newClient()
if err != nil {
return nil, err
}
return &Client{c: c}, nil
}
// An Interface is an ethtool netlink Ethernet interface. Interfaces are used to
// identify an Ethernet interface being queried by its index and/or name.
type Interface struct {
// Callers may choose to set either Index, Name, or both fields. Note that
// if both are set, the kernel will verify that both Index and Name are
// associated with the same interface. If they are not, an error will be
// returned.
Index int
Name string
}
// LinkInfo contains link settings for an Ethernet interface.
type LinkInfo struct {
Interface Interface
Port Port
}
// A Port is the port type for a LinkInfo structure.
type Port int
// Possible Port type values.
const (
TwistedPair Port = 0x00
AUI Port = 0x01
MII Port = 0x02
Fibre Port = 0x03
BNC Port = 0x04
DirectAttach Port = 0x05
None Port = 0xef
Other Port = 0xff
)
// LinkInfos fetches LinkInfo structures for each ethtool-supported interface
// on this system.
func (c *Client) LinkInfos() ([]*LinkInfo, error) {
return c.c.LinkInfos()
}
// LinkInfo fetches LinkInfo for the specified Interface.
//
// If the requested device does not exist or is not supported by the ethtool
// interface, an error compatible with errors.Is(err, os.ErrNotExist) will be
// returned.
func (c *Client) LinkInfo(ifi Interface) (*LinkInfo, error) {
return c.c.LinkInfo(ifi)
}
// LinkMode contains link mode information for an Ethernet interface.
type LinkMode struct {
Interface Interface
SpeedMegabits int
Ours, Peer []AdvertisedLinkMode
Duplex Duplex
}
// A Duplex is the link duplex type for a LinkMode structure.
type Duplex int
// Possible Duplex type values.
const (
Half Duplex = 0x00
Full Duplex = 0x01
Unknown Duplex = 0xff
)
// An AdvertisedLinkMode is a link mode that an interface advertises it is
// capable of using.
type AdvertisedLinkMode struct {
Index int
Name string
}
// LinkModes fetches LinkMode structures for each ethtool-supported interface
// on this system.
func (c *Client) LinkModes() ([]*LinkMode, error) {
return c.c.LinkModes()
}
// LinkMode fetches LinkMode data for the specified Interface.
//
// If the requested device does not exist or is not supported by the ethtool
// interface, an error compatible with errors.Is(err, os.ErrNotExist) will be
// returned.
func (c *Client) LinkMode(ifi Interface) (*LinkMode, error) {
return c.c.LinkMode(ifi)
}
// LinkState contains link state information for an Ethernet interface.
type LinkState struct {
Interface Interface
Link bool
}
// LinkStates fetches LinkState structures for each ethtool-supported interface
// on this system.
func (c *Client) LinkStates() ([]*LinkState, error) {
return c.c.LinkStates()
}
// LinkState fetches LinkState data for the specified Interface.
//
// If the requested device does not exist or is not supported by the ethtool
// interface, an error compatible with errors.Is(err, os.ErrNotExist) will be
// returned.
func (c *Client) LinkState(ifi Interface) (*LinkState, error) {
return c.c.LinkState(ifi)
}
// FEC fetches the forward error correction (FEC) setting for the specified
// Interface.
func (c *Client) FEC(ifi Interface) (*FEC, error) {
return c.c.FEC(ifi)
}
// SetFEC sets the forward error correction (FEC) parameters for the Interface
// in fec.
//
// Setting FEC parameters requires elevated privileges and if the caller
// does not have permission, an error compatible with errors.Is(err,
// os.ErrPermission) will be returned.
//
// If the requested device does not exist or is not supported by the ethtool
// interface, an error compatible with errors.Is(err, os.ErrNotExist) will be
// returned.
func (c *Client) SetFEC(fec FEC) error {
return c.c.SetFEC(fec)
}
// A FEC contains the forward error correction (FEC) parameters for an
// interface.
type FEC struct {
Interface Interface
Modes FECModes
Active FECMode
Auto bool
}
// A FECMode is a FEC mode bit value (single element bitmask) specifying the
// active mode of an interface.
type FECMode int
// A FECModes is a FEC mode bitmask of mode(s) supported by an interface.
type FECModes FECMode
// A WakeOnLAN contains the Wake-on-LAN parameters for an interface.
type WakeOnLAN struct {
Interface Interface
Modes WOLMode
}
// A WOLMode is a Wake-on-LAN mode bitmask of mode(s) supported by an interface.
type WOLMode int
// Possible Wake-on-LAN mode bit flags.
const (
PHY WOLMode = 1 << 0
Unicast WOLMode = 1 << 1
Multicast WOLMode = 1 << 2
Broadcast WOLMode = 1 << 3
ARP WOLMode = 1 << 4
Magic WOLMode = 1 << 5
MagicSecure WOLMode = 1 << 6
Filter WOLMode = 1 << 7
)
// String returns the string representation of a WOLMode bitmask.
func (m WOLMode) String() string {
names := []string{
"PHY",
"Unicast",
"Multicast",
"Broadcast",
"ARP",
"Magic",
"MagicSecure",
"Filter",
}
var s string
left := uint(m)
for i, name := range names {
if m&(1<<uint(i)) != 0 {
if s != "" {
s += "|"
}
s += name
left ^= (1 << uint(i))
}
}
if s == "" && left == 0 {
s = "0"
}
if left > 0 {
if s != "" {
s += "|"
}
s += fmt.Sprintf("%#x", left)
}
return s
}
// WakeOnLANs fetches WakeOnLAN information for each ethtool-supported interface
// on this system.
func (c *Client) WakeOnLANs() ([]*WakeOnLAN, error) {
return c.c.WakeOnLANs()
}
// WakeOnLAN fetches WakeOnLAN parameters for the specified Interface.
//
// Fetching Wake-on-LAN information requires elevated privileges and if the
// caller does not have permission, an error compatible with errors.Is(err,
// os.ErrPermission) will be returned.
//
// If the requested device does not exist or is not supported by the ethtool
// interface, an error compatible with errors.Is(err, os.ErrNotExist) will be
// returned.
func (c *Client) WakeOnLAN(ifi Interface) (*WakeOnLAN, error) {
return c.c.WakeOnLAN(ifi)
}
// SetWakeOnLAN sets the WakeOnLAN parameters for the Interface in wol.
//
// Setting Wake-on-LAN parameters requires elevated privileges and if the caller
// does not have permission, an error compatible with errors.Is(err,
// os.ErrPermission) will be returned.
//
// If the requested device does not exist or is not supported by the ethtool
// interface, an error compatible with errors.Is(err, os.ErrNotExist) will be
// returned.
func (c *Client) SetWakeOnLAN(wol WakeOnLAN) error {
return c.c.SetWakeOnLAN(wol)
}
// Close cleans up the Client's resources.
func (c *Client) Close() error { return c.c.Close() }