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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
package ethtool
import (
"fmt"
"math/big"
)
//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
Autoneg Autoneg
}
// 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
)
// Autoneg is the auto-negotiation status for a link.
type Autoneg uint8
// Possible Autoneg type values.
const (
AutonegOff Autoneg = 0x00
AutonegOn Autoneg = 0x01
)
// String implements fmt.Stringer.
func (a Autoneg) String() string {
switch a {
case AutonegOff:
return "Off"
case AutonegOn:
return "On"
default:
return "Invalid"
}
}
// 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)
}
// LinkModeUpdate represents the properties of an interface link to be updated.
// Only non-nil values will be modified.
type LinkModeUpdate struct {
SpeedMegabits *int
Duplex *Duplex
Autoneg *Autoneg
Advertise *big.Int
}
// UpdateLinkMode updates the given Interface with the non-nil link mode properties in
// the LinkModeUpdate.
func (c *Client) UpdateLinkMode(ifi Interface, lmu *LinkModeUpdate) error {
return c.c.UpdateLinkMode(ifi, lmu)
}
// 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)
}
// PrivateFlags is a list of driver-specific flags which are either on or off.
// These are used to control behavior specific to a specific driver or device
// for which no generic API exists.
//
// The flags which go in here are mostly undocumented other than in kernel
// source code, you can get the list of supported flags by calling
// PrivateFlags() and then searching for the returned names in Linux kernel
// sources.
//
// This is technically a bitset but as the bit positions are not stable across
// kernel versions there is no reason to use that functionality, thus it is not
// exposed.
//
// Note that these flags are in practice not fully covered by Linux's userspace
// ABI guarantees, it should be expected that a flag can go away.
type PrivateFlags struct {
Interface Interface
// Flags is a map of flag names to their active state, i.e. if the flag
// is on or off.
Flags map[string]bool
}
// AllPrivateFlags returns Private Flags for each ethtool-supported interface
// on this system.
func (c *Client) AllPrivateFlags() ([]*PrivateFlags, error) {
return c.c.AllPrivateFlags()
}
// PrivateFlags returns Private Flags for a single interface. See the type for
// a more in-depth explanation.
//
// 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) PrivateFlags(ifi Interface) (*PrivateFlags, error) {
return c.c.PrivateFlags(ifi)
}
// SetPrivateFlags attempts to set the given private flags on the given
// interface. Flags does not need to contain the all flags, those not
// in it are left as-is.
//
// Setting Private Flags requires elevated privileges and if the caller
// does not have permission, an error compatible with errors.Is(err,
// os.ErrPermission) will be returned.
//
// Note that not all flags can be changed in all interface states, some might
// only be settable if the interface is down or are only settable once.
//
// 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) SetPrivateFlags(p PrivateFlags) error {
return c.c.SetPrivateFlags(p)
}
// Close cleans up the Client's resources.
func (c *Client) Close() error { return c.c.Close() }
|