Skip to content

Commit

Permalink
Merge pull request #47 from gutjuri/bit-masks
Browse files Browse the repository at this point in the history
Add Names for supported capabilities of interfaces
  • Loading branch information
safchain committed Sep 16, 2023
2 parents 1a7dcbb + ffb1f38 commit 5287db7
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
63 changes: 63 additions & 0 deletions ethtool.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,36 @@ const (
PERMADDR_LEN = 32
)

var supportedCapabilities = []struct {
name string
mask uint64
speed uint64
}{
{"10baseT_Half", unix.ETHTOOL_LINK_MODE_10baseT_Half_BIT, 10_000_000},
{"10baseT_Full", unix.ETHTOOL_LINK_MODE_10baseT_Full_BIT, 10_000_000},
{"100baseT_Half", unix.ETHTOOL_LINK_MODE_100baseT_Half_BIT, 100_000_000},
{"100baseT_Full", unix.ETHTOOL_LINK_MODE_100baseT_Full_BIT, 100_000_000},
{"1000baseT_Half", unix.ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 1_000_000_000},
{"1000baseT_Full", unix.ETHTOOL_LINK_MODE_1000baseT_Full_BIT, 1_000_000_000},
{"10000baseT_Full", unix.ETHTOOL_LINK_MODE_10000baseT_Full_BIT, 10_000_000_000},
{"2500baseT_Full", unix.ETHTOOL_LINK_MODE_2500baseT_Full_BIT, 2_500_000_000},
{"1000baseKX_Full", unix.ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, 1_000_000_000},
{"10000baseKX_Full", unix.ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, 10_000_000_000},
{"10000baseKR_Full", unix.ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, 10_000_000_000},
{"10000baseR_FEC", unix.ETHTOOL_LINK_MODE_10000baseR_FEC_BIT, 10_000_000_000},
{"20000baseMLD2_Full", unix.ETHTOOL_LINK_MODE_20000baseMLD2_Full_BIT, 20_000_000_000},
{"20000baseKR2_Full", unix.ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT, 20_000_000_000},
{"40000baseKR4_Full", unix.ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT, 40_000_000_000},
{"40000baseCR4_Full", unix.ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT, 40_000_000_000},
{"40000baseSR4_Full", unix.ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT, 40_000_000_000},
{"40000baseLR4_Full", unix.ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT, 40_000_000_000},
{"56000baseKR4_Full", unix.ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT, 56_000_000_000},
{"56000baseCR4_Full", unix.ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT, 56_000_000_000},
{"56000baseSR4_Full", unix.ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT, 56_000_000_000},
{"56000baseLR4_Full", unix.ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT, 56_000_000_000},
{"25000baseCR_Full", unix.ETHTOOL_LINK_MODE_25000baseCR_Full_BIT, 25_000_000_000},
}

type ifreq struct {
ifr_name [IFNAMSIZ]byte
ifr_data uintptr
Expand Down Expand Up @@ -798,3 +828,36 @@ func PermAddr(intf string) (string, error) {
defer e.Close()
return e.PermAddr(intf)
}

func supportedSpeeds(mask uint64) (ret []struct {
name string
mask uint64
speed uint64
}) {
for _, mode := range supportedCapabilities {
if ((1 << mode.mask) & mask) != 0 {
ret = append(ret, mode)
}
}
return ret
}

// SupportedLinkModes returns the names of the link modes supported by the interface.
func SupportedLinkModes(mask uint64) []string {
var ret []string
for _, mode := range supportedSpeeds(mask) {
ret = append(ret, mode.name)
}
return ret
}

// SupportedSpeed returns the maximum capacity of this interface.
func SupportedSpeed(mask uint64) uint64 {
var ret uint64
for _, mode := range supportedSpeeds(mask) {
if mode.speed > ret {
ret = mode.speed
}
}
return ret
}
17 changes: 17 additions & 0 deletions ethtool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package ethtool

import (
"net"
"reflect"
"testing"
)

Expand Down Expand Up @@ -105,3 +106,19 @@ func TestBusInfo(t *testing.T) {
t.Fatal("Unable to retrieve bus info from any interface of this system.")
}
}

func TestSupportedLinkModes(t *testing.T) {
var cases = []struct {
inputMask uint64
expected []string
}{
{0b01100010_11101111, []string{"10baseT_Half", "10baseT_Full", "100baseT_Half", "100baseT_Full", "1000baseT_Full"}},
}

for _, testcase := range cases {
actual := SupportedLinkModes(testcase.inputMask)
if !reflect.DeepEqual(actual, testcase.expected) {
t.Error("Expected ", testcase.expected, " got ", actual)
}
}
}
1 change: 1 addition & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func main() {
panic(err.Error())
}
fmt.Printf("cmd get: %+v\n", cmdGet)
fmt.Printf("max capacity: %+v\n", ethtool.SupportedSpeed(cmdGet["Supported"]))

msgLvlGet, err := e.MsglvlGet(*name)
if err != nil {
Expand Down

0 comments on commit 5287db7

Please sign in to comment.