Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add wake on lan support #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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