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

fix various linting issues #1048

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 7 additions & 5 deletions addr_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,13 @@ func (h *Handle) addrHandle(link Link, addr *Addr, req *nl.NetlinkRequest) error
// value should be "forever". To compensate for that, only add the attributes if at least one of the values is
// non-zero, which means the caller has explicitly set them
if addr.ValidLft > 0 || addr.PreferedLft > 0 {
cachedata := nl.IfaCacheInfo{unix.IfaCacheinfo{
Valid: uint32(addr.ValidLft),
Prefered: uint32(addr.PreferedLft),
}}
req.AddData(nl.NewRtAttr(unix.IFA_CACHEINFO, cachedata.Serialize()))
cacheData := nl.IfaCacheInfo{
IfaCacheinfo: unix.IfaCacheinfo{
Valid: uint32(addr.ValidLft),
Prefered: uint32(addr.PreferedLft),
},
}
req.AddData(nl.NewRtAttr(unix.IFA_CACHEINFO, cacheData.Serialize()))
}

_, err := req.Execute(unix.NETLINK_ROUTE, 0)
Expand Down
9 changes: 3 additions & 6 deletions conntrack_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (h *Handle) ConntrackDeleteFilters(table ConntrackTableType, family InetFam
}
}
if len(errMsgs) > 0 {
return matched, fmt.Errorf(strings.Join(errMsgs, "; "))
return matched, errors.New(strings.Join(errMsgs, "; "))
}
return matched, nil
}
Expand Down Expand Up @@ -500,10 +500,6 @@ func parseBERaw64(r *bytes.Reader, v *uint64) {
binary.Read(r, binary.BigEndian, v)
}

func parseRaw32(r *bytes.Reader, v *uint32) {
binary.Read(r, nl.NativeEndian(), v)
}

func parseByteAndPacketCounters(r *bytes.Reader) (bytes, packets uint64) {
for i := 0; i < 2; i++ {
switch _, t, _ := parseNfAttrTL(r); t {
Expand Down Expand Up @@ -639,9 +635,10 @@ func parseRawData(data []byte) *ConntrackFlow {
if nested, t, l := parseNfAttrTL(reader); nested {
switch t {
case nl.CTA_TUPLE_ORIG:
if nested, t, l = parseNfAttrTL(reader); nested && t == nl.CTA_TUPLE_IP {
if nested, t, _ = parseNfAttrTL(reader); nested && t == nl.CTA_TUPLE_IP {
parseIpTuple(reader, &s.Forward)
}
// TODO(thaJeztah): should this skipNfAttrValue ?
case nl.CTA_TUPLE_REPLY:
if nested, t, l = parseNfAttrTL(reader); nested && t == nl.CTA_TUPLE_IP {
parseIpTuple(reader, &s.Reverse)
Expand Down
23 changes: 11 additions & 12 deletions devlink_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,10 @@ type DevlinkParam struct {
}

// DevlinkParamValue contains values of the parameter
// Data field contains specific type which can be casted by unsing info from the DevlinkParam.Type field
// Data field contains specific type which can be cast using info from the [DevlinkParam.Type] field
type DevlinkParamValue struct {
rawData []byte
Data interface{}
CMODE uint8 // possible values are in nl.DEVLINK_PARAM_CMODE_* constants
Data interface{}
CMODE uint8 // possible values are in nl.DEVLINK_PARAM_CMODE_* constants
}

// parseAttributes parses provided Netlink Attributes and populates DevlinkParam, returns error if occured
Expand Down Expand Up @@ -324,7 +323,7 @@ func (dlpv *DevlinkParamValue) parseAttributes(attrs []syscall.NetlinkRouteAttr,
switch paramType {
case nl.DEVLINK_PARAM_TYPE_U8:
dlpv.Data = uint8(0)
if rawData != nil && len(rawData) == 1 {
if len(rawData) == 1 {
dlpv.Data = uint8(rawData[0])
}
case nl.DEVLINK_PARAM_TYPE_U16:
Expand Down Expand Up @@ -431,13 +430,13 @@ func (d *DevlinkDevice) parseAttributes(attrs []syscall.NetlinkRouteAttr) error
return nil
}

func (dev *DevlinkDevice) parseEswitchAttrs(msgs [][]byte) {
func (d *DevlinkDevice) parseEswitchAttrs(msgs [][]byte) {
m := msgs[0]
attrs, err := nl.ParseRouteAttr(m[nl.SizeofGenlmsg:])
if err != nil {
return
}
dev.parseAttributes(attrs)
d.parseAttributes(attrs)
}

func (h *Handle) getEswitchAttrs(family *GenlFamily, dev *DevlinkDevice) {
Expand Down Expand Up @@ -544,7 +543,7 @@ func (h *Handle) createCmdReq(cmd uint8, bus string, device string) (*GenlFamily
return f, req, nil
}

// DevlinkGetDeviceByName provides a pointer to devlink device and nil error,
// DevLinkGetDeviceByName provides a pointer to devlink device and nil error,
// otherwise returns an error code.
func (h *Handle) DevLinkGetDeviceByName(Bus string, Device string) (*DevlinkDevice, error) {
f, req, err := h.createCmdReq(nl.DEVLINK_CMD_GET, Bus, Device)
Expand All @@ -563,7 +562,7 @@ func (h *Handle) DevLinkGetDeviceByName(Bus string, Device string) (*DevlinkDevi
return dev, err
}

// DevlinkGetDeviceByName provides a pointer to devlink device and nil error,
// DevLinkGetDeviceByName provides a pointer to devlink device and nil error,
// otherwise returns an error code.
func DevLinkGetDeviceByName(Bus string, Device string) (*DevlinkDevice, error) {
return pkgHandle.DevLinkGetDeviceByName(Bus, Device)
Expand Down Expand Up @@ -650,7 +649,7 @@ func parseDevLinkAllPortList(msgs [][]byte) ([]*DevlinkPort, error) {
return ports, nil
}

// DevLinkGetPortList provides a pointer to devlink ports and nil error,
// DevLinkGetAllPortList provides a pointer to devlink ports and nil error,
// otherwise returns an error code.
// If the returned error is [ErrDumpInterrupted], results may be inconsistent
// or incomplete.
Expand All @@ -677,7 +676,7 @@ func (h *Handle) DevLinkGetAllPortList() ([]*DevlinkPort, error) {
return ports, executeErr
}

// DevLinkGetPortList provides a pointer to devlink ports and nil error,
// DevLinkGetAllPortList provides a pointer to devlink ports and nil error,
// otherwise returns an error code.
// If the returned error is [ErrDumpInterrupted], results may be inconsistent
// or incomplete.
Expand All @@ -698,7 +697,7 @@ func parseDevlinkPortMsg(msgs [][]byte) (*DevlinkPort, error) {
return port, nil
}

// DevLinkGetPortByIndexprovides a pointer to devlink device and nil error,
// DevLinkGetPortByIndex a pointer to devlink device and nil error,
// otherwise returns an error code.
func (h *Handle) DevLinkGetPortByIndex(Bus string, Device string, PortIndex uint32) (*DevlinkPort, error) {

Expand Down
5 changes: 3 additions & 2 deletions ioctl_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ const (
ETH_SS_STATS
// ETH_SS_PRIV_FLAGS are driver private flag names
ETH_SS_PRIV_FLAGS
// _ETH_SS_NTUPLE_FILTERS is deprecated
_ETH_SS_NTUPLE_FILTERS

// Deprecated: _ETH_SS_NTUPLE_FILTERS is deprecated
_ETH_SS_NTUPLE_FILTERS //nolint:unused
// ETH_SS_FEATURES are device feature names
ETH_SS_FEATURES
// ETH_SS_RSS_HASH_FUNCS is RSS hush function names
Expand Down
16 changes: 0 additions & 16 deletions link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -2912,22 +2912,6 @@ func parseBondArpIpTargets(value []byte) []net.IP {
return targets
}

func addBondSlaveAttrs(bondSlave *BondSlave, linkInfo *nl.RtAttr) {
data := linkInfo.AddRtAttr(nl.IFLA_INFO_SLAVE_DATA, nil)

data.AddRtAttr(nl.IFLA_BOND_SLAVE_STATE, nl.Uint8Attr(uint8(bondSlave.State)))
data.AddRtAttr(nl.IFLA_BOND_SLAVE_MII_STATUS, nl.Uint8Attr(uint8(bondSlave.MiiStatus)))
data.AddRtAttr(nl.IFLA_BOND_SLAVE_LINK_FAILURE_COUNT, nl.Uint32Attr(bondSlave.LinkFailureCount))
data.AddRtAttr(nl.IFLA_BOND_SLAVE_QUEUE_ID, nl.Uint16Attr(bondSlave.QueueId))
data.AddRtAttr(nl.IFLA_BOND_SLAVE_AD_AGGREGATOR_ID, nl.Uint16Attr(bondSlave.AggregatorId))
data.AddRtAttr(nl.IFLA_BOND_SLAVE_AD_ACTOR_OPER_PORT_STATE, nl.Uint8Attr(bondSlave.AdActorOperPortState))
data.AddRtAttr(nl.IFLA_BOND_SLAVE_AD_PARTNER_OPER_PORT_STATE, nl.Uint16Attr(bondSlave.AdPartnerOperPortState))

if mac := bondSlave.PermHardwareAddr; mac != nil {
data.AddRtAttr(nl.IFLA_BOND_SLAVE_PERM_HWADDR, []byte(mac))
}
}

func parseBondSlaveData(slave LinkSlave, data []syscall.NetlinkRouteAttr) {
bondSlave := slave.(*BondSlave)
for i := range data {
Expand Down
6 changes: 0 additions & 6 deletions neigh_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,6 @@ func (h *Handle) NeighAppend(neigh *Neigh) error {
return h.neighAdd(neigh, unix.NLM_F_CREATE|unix.NLM_F_APPEND)
}

// NeighAppend will append an entry to FDB
// Equivalent to: `bridge fdb append...`
func neighAdd(neigh *Neigh, mode int) error {
return pkgHandle.neighAdd(neigh, mode)
}

// NeighAppend will append an entry to FDB
// Equivalent to: `bridge fdb append...`
func (h *Handle) neighAdd(neigh *Neigh, mode int) error {
Expand Down
4 changes: 2 additions & 2 deletions netlink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func skipUnlessKModuleLoaded(t *testing.T, moduleNames ...string) {

failed := false
for _, name := range moduleNames {
if found, _ := foundRequiredMods[name]; !found {
if !foundRequiredMods[name] {
t.Logf("Test requires missing kmodule %q.", name)
failed = true
}
Expand Down Expand Up @@ -217,7 +217,7 @@ func setUpNamedNetlinkTestWithKModule(t *testing.T, moduleNames ...string) (stri

failed := false
for _, name := range moduleNames {
if found, _ := foundRequiredMods[name]; !found {
if !foundRequiredMods[name] {
t.Logf("Test requires missing kmodule %q.", name)
failed = true
}
Expand Down
24 changes: 14 additions & 10 deletions nl/nl_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@ import (
"golang.org/x/sys/unix"
)

// Family type definitions
const (
// Family type definitions
FAMILY_ALL = unix.AF_UNSPEC
FAMILY_V4 = unix.AF_INET
FAMILY_V6 = unix.AF_INET6
FAMILY_MPLS = unix.AF_MPLS
// Arbitrary set value (greater than default 4k) to allow receiving
// from kernel more verbose messages e.g. for statistics,
)

const (
// RECEIVE_BUFFER_SIZE is an arbitrary set value (greater than default 4k)
// to allow receiving from kernel more verbose messages e.g. for statistics,
// tc rules or filters, or other more memory requiring data.
RECEIVE_BUFFER_SIZE = 65536
// Kernel netlink pid
// PidKernel is the kernel netlink pid.
PidKernel uint32 = 0
SizeofCnMsgOp = 0x18
)
Expand All @@ -42,7 +45,7 @@ var nextSeqNr uint32
// Default netlink socket timeout, 60s
var SocketTimeoutTv = unix.Timeval{Sec: 60, Usec: 0}

// ErrorMessageReporting is the default error message reporting configuration for the new netlink sockets
// EnableErrorMessageReporting is the default error message reporting configuration for the new netlink sockets
var EnableErrorMessageReporting bool = false

// ErrDumpInterrupted is an instance of errDumpInterrupted, used to report that
Expand Down Expand Up @@ -91,15 +94,15 @@ func NativeEndian() binary.ByteOrder {
return nativeEndian
}

// Byte swap a 16 bit value if we aren't big endian
// Swap16 byte-swaps a 16 bit value if we aren't big endian.
func Swap16(i uint16) uint16 {
if NativeEndian() == binary.BigEndian {
return i
}
return (i&0xff00)>>8 | (i&0xff)<<8
}

// Byte swap a 32 bit value if aren't big endian
// Swap32 byte-swaps a 32 bit value if aren't big endian.
func Swap32(i uint32) uint32 {
if NativeEndian() == binary.BigEndian {
return i
Expand Down Expand Up @@ -672,7 +675,8 @@ func dummyMsgIterFunc(msg []byte) bool {
return true
}

// Create a new netlink request from proto and flags
// NewNetlinkRequest creates a new netlink request from proto and flags.
//
// Note the Len value will be inaccurate once data is added until
// the message is serialized
func NewNetlinkRequest(proto, flags int) *NetlinkRequest {
Expand Down Expand Up @@ -784,7 +788,7 @@ func executeInNetns(newNs, curNs netns.NsHandle) (func(), error) {
return restore, nil
}

// Create a netlink socket with a given protocol (e.g. NETLINK_ROUTE)
// Subscribe creates a netlink socket with a given protocol (e.g. NETLINK_ROUTE)
// and subscribe it to multicast groups passed in variable argument list.
// Returns the netlink socket on which Receive() method can be called
// to retrieve the messages from the kernel.
Expand Down Expand Up @@ -1050,7 +1054,7 @@ func ParseRouteAttr(b []byte) ([]syscall.NetlinkRouteAttr, error) {
}

// ParseRouteAttrAsMap parses provided buffer that contains raw RtAttrs and returns a map of parsed
// atttributes indexed by attribute type or error if occured.
// attributes indexed by attribute type or error if occurred.
func ParseRouteAttrAsMap(b []byte) (map[uint16]syscall.NetlinkRouteAttr, error) {
attrMap := make(map[uint16]syscall.NetlinkRouteAttr)

Expand Down
6 changes: 3 additions & 3 deletions nl/tc_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,15 +434,15 @@ const (
TCA_HTB_MAX = TCA_HTB_CEIL64
)

//struct tc_htb_opt {
// struct tc_htb_opt {
// struct tc_ratespec rate;
// struct tc_ratespec ceil;
// __u32 buffer;
// __u32 cbuffer;
// __u32 quantum;
// __u32 level; /* out only */
// __u32 prio;
//};
// };

type TcHtbCopt struct {
Rate TcRateSpec
Expand Down Expand Up @@ -1112,7 +1112,7 @@ const (
TCA_FLOWER_KEY_ENC_OPTS
TCA_FLOWER_KEY_ENC_OPTS_MASK

__TCA_FLOWER_MAX
__TCA_FLOWER_MAX //nolint:unused
)

const TCA_CLS_FLAGS_SKIP_HW = 1 << 0 /* don't offload filter to HW */
Expand Down
16 changes: 0 additions & 16 deletions qdisc_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,22 +769,6 @@ func tick2Time(tick uint32) uint32 {
return uint32(float64(tick) / TickInUsec())
}

func time2Ktime(time uint32) uint32 {
return uint32(float64(time) * ClockFactor())
}

func ktime2Time(ktime uint32) uint32 {
return uint32(float64(ktime) / ClockFactor())
}

func burst(rate uint64, buffer uint32) uint32 {
return uint32(float64(rate) * float64(tick2Time(buffer)) / TIME_UNITS_PER_SEC)
}

func latency(rate uint64, limit, buffer uint32) float64 {
return TIME_UNITS_PER_SEC*(float64(limit)/float64(rate)) - float64(tick2Time(buffer))
}

func Xmittime(rate uint64, size uint32) uint32 {
// https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/tree/tc/tc_core.c#n62
return time2Tick(uint32(TIME_UNITS_PER_SEC * (float64(size) / float64(rate))))
Expand Down
6 changes: 6 additions & 0 deletions route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,9 @@ func BenchmarkRouteListFilteredNew(b *testing.B) {
defer tearDown()

link, err := setUpRoutesBench(b)
if err != nil {
b.Fatal(err)
}

b.ResetTimer()
b.ReportAllocs()
Expand All @@ -1043,6 +1046,9 @@ func BenchmarkRouteListIter(b *testing.B) {
defer tearDown()

link, err := setUpRoutesBench(b)
if err != nil {
b.Fatal(err)
}

b.ResetTimer()
b.ReportAllocs()
Expand Down
4 changes: 2 additions & 2 deletions xfrm_policy_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,8 +344,8 @@ func parseXfrmPolicy(m []byte, family int) (*XfrmPolicy, error) {
for _, attr := range attrs {
switch attr.Attr.Type {
case nl.XFRMA_TMPL:
max := len(attr.Value)
for i := 0; i < max; i += nl.SizeofXfrmUserTmpl {
maxVal := len(attr.Value)
for i := 0; i < maxVal; i += nl.SizeofXfrmUserTmpl {
var resTmpl XfrmPolicyTmpl
tmpl := nl.DeserializeXfrmUserTmpl(attr.Value[i : i+nl.SizeofXfrmUserTmpl])
resTmpl.Dst = tmpl.XfrmId.Daddr.ToIP()
Expand Down