Skip to content

Commit

Permalink
feat: add wake on lan support
Browse files Browse the repository at this point in the history
  • Loading branch information
natesales committed Sep 6, 2024
1 parent c9db2ef commit e4b9849
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions ethtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const (
// CMD supported
ETHTOOL_GSET = 0x00000001 /* Get settings. */
ETHTOOL_SSET = 0x00000002 /* Set settings. */
ETHTOOL_GWOL = 0x00000005 /* Get wake-on-lan options. */
ETHTOOL_SWOL = 0x00000006 /* Set wake-on-lan options. */
ETHTOOL_GDRVINFO = 0x00000003 /* Get driver info. */
ETHTOOL_GMSGLVL = 0x00000007 /* Get driver message level */
ETHTOOL_SMSGLVL = 0x00000008 /* Set driver msg level. */
Expand Down Expand Up @@ -207,6 +209,36 @@ type Coalesce struct {
RateSampleInterval uint32
}

// WoL options
const (
WAKE_PHY = 1 << 0
WAKE_UCAST = 1 << 1
WAKE_MCAST = 1 << 2
WAKE_BCAST = 1 << 3
WAKE_ARP = 1 << 4
WAKE_MAGIC = 1 << 5
WAKE_MAGICSECURE = 1 << 6 // only meaningful if WAKE_MAGIC
)

var WoLMap = map[uint32]string{
WAKE_PHY: "p", // Wake on PHY activity
WAKE_UCAST: "u", // Wake on unicast messages
WAKE_MCAST: "m", // Wake on multicast messages
WAKE_BCAST: "b", // Wake on broadcast messages
WAKE_ARP: "a", // Wake on ARP
WAKE_MAGIC: "g", // Wake on MagicPacket™
WAKE_MAGICSECURE: "s", // Enable SecureOn™ password for MagicPacket™
// f Wake on filter(s)
// d Disable (wake on nothing). This option clears all previous options.
}

// WakeOnLan contains WoL config for an interface
type WakeOnLan struct {
Cmd uint32 // ETHTOOL_GWOL or ETHTOOL_SWOL
Supported uint32 // r/o bitmask of WAKE_* flags for supported WoL modes
Opts uint32 // Bitmask of WAKE_* flags for enabled WoL modes
}

const (
SOF_TIMESTAMPING_TX_HARDWARE = (1 << 0)
SOF_TIMESTAMPING_TX_SOFTWARE = (1 << 1)
Expand Down Expand Up @@ -496,6 +528,31 @@ func (e *Ethtool) PermAddr(intf string) (string, error) {
), nil
}

// GetWakeOnLan returns the WoL config for the given interface name.
func (e *Ethtool) GetWakeOnLan(intf string) (WakeOnLan, error) {
wol := WakeOnLan{
Cmd: ETHTOOL_GWOL,
}

if err := e.ioctl(intf, uintptr(unsafe.Pointer(&wol))); err != nil {
return WakeOnLan{}, err
}

return wol, nil
}

// SetWakeOnLan sets the WoL config for the given interface name and
// returns the new WoL config.
func (e *Ethtool) SetWakeOnLan(intf string, wol WakeOnLan) (WakeOnLan, error) {
wol.Cmd = ETHTOOL_SWOL

if err := e.ioctl(intf, uintptr(unsafe.Pointer(&wol))); err != nil {
return WakeOnLan{}, err
}

return wol, nil
}

func (e *Ethtool) ioctl(intf string, data uintptr) error {
var name [IFNAMSIZ]byte
copy(name[:], []byte(intf))
Expand Down

0 comments on commit e4b9849

Please sign in to comment.