diff --git a/addr_linux.go b/addr_linux.go index 01c2306c..5120a1d7 100644 --- a/addr_linux.go +++ b/addr_linux.go @@ -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) diff --git a/conntrack_linux.go b/conntrack_linux.go index c566b2a4..e3064c2c 100644 --- a/conntrack_linux.go +++ b/conntrack_linux.go @@ -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 } @@ -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 { @@ -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) diff --git a/devlink_linux.go b/devlink_linux.go index 45d8ee4b..264b31ec 100644 --- a/devlink_linux.go +++ b/devlink_linux.go @@ -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 @@ -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: @@ -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) { @@ -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) @@ -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) @@ -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. @@ -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. @@ -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) { diff --git a/ioctl_linux.go b/ioctl_linux.go index 4d33db5d..a0cf93f1 100644 --- a/ioctl_linux.go +++ b/ioctl_linux.go @@ -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 diff --git a/link_linux.go b/link_linux.go index d6bffded..41cf67e5 100644 --- a/link_linux.go +++ b/link_linux.go @@ -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 { diff --git a/neigh_linux.go b/neigh_linux.go index 1c6f2958..5a40b48e 100644 --- a/neigh_linux.go +++ b/neigh_linux.go @@ -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 { diff --git a/netlink_test.go b/netlink_test.go index ed0059ae..c9bd7b9d 100644 --- a/netlink_test.go +++ b/netlink_test.go @@ -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 } @@ -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 } diff --git a/nl/nl_linux.go b/nl/nl_linux.go index 4d2732a9..d1c4cf3c 100644 --- a/nl/nl_linux.go +++ b/nl/nl_linux.go @@ -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 ) @@ -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 @@ -91,7 +94,7 @@ 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 @@ -99,7 +102,7 @@ func Swap16(i uint16) uint16 { 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 @@ -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 { @@ -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. @@ -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) diff --git a/nl/tc_linux.go b/nl/tc_linux.go index b8f50079..0584d8d4 100644 --- a/nl/tc_linux.go +++ b/nl/tc_linux.go @@ -434,7 +434,7 @@ 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; @@ -442,7 +442,7 @@ const ( // __u32 quantum; // __u32 level; /* out only */ // __u32 prio; -//}; +// }; type TcHtbCopt struct { Rate TcRateSpec @@ -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 */ diff --git a/qdisc_linux.go b/qdisc_linux.go index 22cf0e58..2c4eb5ea 100644 --- a/qdisc_linux.go +++ b/qdisc_linux.go @@ -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)))) diff --git a/route_test.go b/route_test.go index 7e5e6b0e..78dcfbd7 100644 --- a/route_test.go +++ b/route_test.go @@ -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() @@ -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() diff --git a/xfrm_policy_linux.go b/xfrm_policy_linux.go index bf143a1b..e6497b0a 100644 --- a/xfrm_policy_linux.go +++ b/xfrm_policy_linux.go @@ -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()