diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml new file mode 100644 index 0000000..e283ab9 --- /dev/null +++ b/.github/workflows/golangci-lint.yml @@ -0,0 +1,26 @@ +name: golangci-lint +on: + push: + branches: + - main + - master + pull_request: + +permissions: + contents: read + # Optional: allow read access to pull request. Use with `only-new-issues` option. + # pull-requests: read + +jobs: + golangci: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: stable + - name: golangci-lint + uses: golangci/golangci-lint-action@v6 + with: + version: v1.60 diff --git a/.golangci.yml b/.golangci.yml index 77ccf92..65552c9 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,11 +1,15 @@ linters: - disable: - - gosimple - - unused enable: + - gosimple - gci - gofmt - misspell + - goimports + - staticcheck + - errcheck + - govet + - misspell + - gocritic linters-settings: gci: sections: diff --git a/ethtool.go b/ethtool.go index d4002d5..62df2c1 100644 --- a/ethtool.go +++ b/ethtool.go @@ -61,8 +61,8 @@ const ( ETHTOOL_GMSGLVL = 0x00000007 /* Get driver message level */ ETHTOOL_SMSGLVL = 0x00000008 /* Set driver msg level. */ - /* Get link status for host, i.e. whether the interface *and* the - * physical port (if there is one) are up (ethtool_value). */ + // Get link status for host, i.e. whether the interface *and* the + // physical port (if there is one) are up (ethtool_value). ETHTOOL_GLINK = 0x0000000a ETHTOOL_GCOALESCE = 0x0000000e /* Get coalesce config */ ETHTOOL_SCOALESCE = 0x0000000f /* Set coalesce config */ @@ -239,23 +239,25 @@ type WakeOnLan struct { Opts uint32 // Bitmask of WAKE_* flags for enabled WoL modes } +// Timestamping options +// see: https://www.kernel.org/doc/Documentation/networking/timestamping.txt const ( - SOF_TIMESTAMPING_TX_HARDWARE = (1 << 0) - SOF_TIMESTAMPING_TX_SOFTWARE = (1 << 1) - SOF_TIMESTAMPING_RX_HARDWARE = (1 << 2) - SOF_TIMESTAMPING_RX_SOFTWARE = (1 << 3) - SOF_TIMESTAMPING_SOFTWARE = (1 << 4) - SOF_TIMESTAMPING_SYS_HARDWARE = (1 << 5) - SOF_TIMESTAMPING_RAW_HARDWARE = (1 << 6) - SOF_TIMESTAMPING_OPT_ID = (1 << 7) - SOF_TIMESTAMPING_TX_SCHED = (1 << 8) - SOF_TIMESTAMPING_TX_ACK = (1 << 9) - SOF_TIMESTAMPING_OPT_CMSG = (1 << 10) - SOF_TIMESTAMPING_OPT_TSONLY = (1 << 11) - SOF_TIMESTAMPING_OPT_STATS = (1 << 12) - SOF_TIMESTAMPING_OPT_PKTINFO = (1 << 13) - SOF_TIMESTAMPING_OPT_TX_SWHW = (1 << 14) - SOF_TIMESTAMPING_BIND_PHC = (1 << 15) + SOF_TIMESTAMPING_TX_HARDWARE = (1 << 0) /* Request tx timestamps generated by the network adapter. */ + SOF_TIMESTAMPING_TX_SOFTWARE = (1 << 1) /* Request tx timestamps when data leaves the kernel. */ + SOF_TIMESTAMPING_RX_HARDWARE = (1 << 2) /* Request rx timestamps generated by the network adapter. */ + SOF_TIMESTAMPING_RX_SOFTWARE = (1 << 3) /* Request rx timestamps when data enters the kernel. */ + SOF_TIMESTAMPING_SOFTWARE = (1 << 4) /* Report any software timestamps when available. */ + SOF_TIMESTAMPING_SYS_HARDWARE = (1 << 5) /* This option is deprecated and ignored. */ + SOF_TIMESTAMPING_RAW_HARDWARE = (1 << 6) /* Report hardware timestamps. */ + SOF_TIMESTAMPING_OPT_ID = (1 << 7) /* Generate a unique identifier along with each packet. */ + SOF_TIMESTAMPING_TX_SCHED = (1 << 8) /* Request tx timestamps prior to entering the packet scheduler. */ + SOF_TIMESTAMPING_TX_ACK = (1 << 9) /* Request tx timestamps when all data in the send buffer has been acknowledged. */ + SOF_TIMESTAMPING_OPT_CMSG = (1 << 10) /* Support recv() cmsg for all timestamped packets. */ + SOF_TIMESTAMPING_OPT_TSONLY = (1 << 11) /* Applies to transmit timestamps only. */ + SOF_TIMESTAMPING_OPT_STATS = (1 << 12) /* Optional stats that are obtained along with the transmit timestamps. */ + SOF_TIMESTAMPING_OPT_PKTINFO = (1 << 13) /* Enable the SCM_TIMESTAMPING_PKTINFO control message for incoming packets with hardware timestamps. */ + SOF_TIMESTAMPING_OPT_TX_SWHW = (1 << 14) /* Request both hardware and software timestamps for outgoing packets when SOF_TIMESTAMPING_TX_HARDWARE and SOF_TIMESTAMPING_TX_SOFTWARE are enabled at the same time. */ + SOF_TIMESTAMPING_BIND_PHC = (1 << 15) /* Bind the socket to a specific PTP Hardware Clock. */ ) const ( @@ -311,6 +313,7 @@ const ( HWTSTAMP_FILTER_NTP_ALL /* NTP, UDP, all versions and packet modes */ ) +// TimestampingInformation contains PTP timetstapming information type TimestampingInformation struct { Cmd uint32 SoTimestamping uint32 /* SOF_TIMESTAMPING_* bitmask */ @@ -381,6 +384,7 @@ type Pause struct { TxPause uint32 } +// Ethtool is a struct that contains the file descriptor for the ethtool type Ethtool struct { fd int } @@ -389,7 +393,7 @@ type Ethtool struct { func goString(s []byte) string { strEnd := bytes.IndexByte(s, 0) if strEnd == -1 { - return string(s[:]) + return string(s) } return string(s[:strEnd]) } @@ -729,6 +733,7 @@ func isFeatureBitSet(blocks [MAX_FEATURE_BLOCKS]ethtoolGetFeaturesBlock, index u return (blocks)[index/32].active&(1<<(index%32)) != 0 } +// FeatureState contains the state of a feature. type FeatureState struct { Available bool Requested bool @@ -950,7 +955,7 @@ func (e *Ethtool) UpdatePrivFlags(intf string, config map[string]bool) error { return e.ioctl(intf, uintptr(unsafe.Pointer(&update))) } -// Get state of a link. +// LinkState get the state of a link. func (e *Ethtool) LinkState(intf string) (uint32, error) { x := ethtoolLink{ cmd: ETHTOOL_GLINK, diff --git a/ethtool_cmd.go b/ethtool_cmd.go index e94d6dd..09499fe 100644 --- a/ethtool_cmd.go +++ b/ethtool_cmd.go @@ -33,7 +33,9 @@ import ( "golang.org/x/sys/unix" ) -type EthtoolCmd struct { /* ethtool.c: struct ethtool_cmd */ +// EthtoolCmd is the Go version of the Linux kerne ethtool_cmd struct +// see ethtool.c +type EthtoolCmd struct { Cmd uint32 Supported uint32 Advertising uint32 @@ -102,10 +104,6 @@ func (f *EthtoolCmd) reflect(retv *map[string]uint64) { default: (*retv)[typeField.Name+"_unknown_type"] = 0 } - - // tag := typeField.Tag - // fmt.Printf("Field Name: %s,\t Field Value: %v,\t Tag Value: %s\n", - // typeField.Name, valueField.Interface(), tag.Get("tag_name")) } } @@ -198,6 +196,7 @@ func (e *Ethtool) CmdGetMapped(intf string) (map[string]uint64, error) { return result, nil } +// CmdGetMapped returns the interface settings in a map func CmdGetMapped(intf string) (map[string]uint64, error) { e, err := NewEthtool() if err != nil { diff --git a/ethtool_test.go b/ethtool_test.go index b2820bb..ba7429d 100644 --- a/ethtool_test.go +++ b/ethtool_test.go @@ -35,7 +35,7 @@ func TestGoString(t *testing.T) { } for want, s := range testcases { - got := goString(s[:]) + got := goString(s) if got != want { t.Errorf("String conversion failed, got: %s, want: %s.", got, want) }