Skip to content

Commit

Permalink
Log retry error when setting MAC address
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Panattoni <[email protected]>
  • Loading branch information
zeeke committed Oct 12, 2023
1 parent 03df0d8 commit 1d69ed6
Showing 1 changed file with 45 additions and 28 deletions.
73 changes: 45 additions & 28 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strconv"
"strings"
"time"

"github.com/k8snetworkplumbingwg/sriov-cni/pkg/logging"
)

var (
Expand Down Expand Up @@ -310,21 +312,23 @@ func SetVFEffectiveMAC(netLinkManager NetlinkManager, netDeviceName string, macA
return err
}

return Retry(20, 100*time.Millisecond, func() error {
if err := netLinkManager.LinkSetHardwareAddr(orgLinkObj, hwaddr); err != nil {
return err
}
return retry(20, 100*time.Millisecond, warnOnError(
"SetVFEffectiveMAC",
func() error {
if err := netLinkManager.LinkSetHardwareAddr(orgLinkObj, hwaddr); err != nil {
return err
}

linkObj, err := netLinkManager.LinkByName(netDeviceName)
if err != nil {
return fmt.Errorf("failed to get netlink device with name %s: %q", orgLinkObj.Attrs().Name, err)
}
if linkObj.Attrs().HardwareAddr.String() != macAddress {
return fmt.Errorf("effective mac address is different from requested one")
}
linkObj, err := netLinkManager.LinkByName(netDeviceName)
if err != nil {
return fmt.Errorf("failed to get netlink device with name %s: %q", orgLinkObj.Attrs().Name, err)
}
if linkObj.Attrs().HardwareAddr.String() != macAddress {
return fmt.Errorf("effective mac address is different from requested one")
}

return nil
})
return nil
}))
}

// SetVFHardwareMAC will try to set the hardware mac address on a specific VF ID under a requested PF
Expand All @@ -344,21 +348,22 @@ func SetVFHardwareMAC(netLinkManager NetlinkManager, pfDevice string, vfID int,
return err
}

return Retry(20, 100*time.Millisecond, func() error {
if err := netLinkManager.LinkSetVfHardwareAddr(orgLinkObj, vfID, hwaddr); err != nil {
return err
}
return retry(20, 100*time.Millisecond, warnOnError("SetVFHardwareMAC",
func() error {
if err := netLinkManager.LinkSetVfHardwareAddr(orgLinkObj, vfID, hwaddr); err != nil {
return err
}

linkObj, err := netLinkManager.LinkByName(pfDevice)
if err != nil {
return fmt.Errorf("failed to get netlink device with name %s: %q", orgLinkObj.Attrs().Name, err)
}
if linkObj.Attrs().Vfs[vfID].Mac.String() != macAddress {
return fmt.Errorf("hardware mac address is different from requested one")
}
linkObj, err := netLinkManager.LinkByName(pfDevice)
if err != nil {
return fmt.Errorf("failed to get netlink device with name %s: %q", orgLinkObj.Attrs().Name, err)
}
if linkObj.Attrs().Vfs[vfID].Mac.String() != macAddress {
return fmt.Errorf("hardware mac address is different from requested one")
}

return nil
})
return nil
}))
}

// IsValidMACAddress checks if net.HardwareAddr is a valid MAC address.
Expand Down Expand Up @@ -390,8 +395,8 @@ func IsIPv6(ip net.IP) bool {
return ip.To4() == nil && ip.To16() != nil
}

// Retry retries a given function until no return error; times out after retries*sleep
func Retry(retries int, sleep time.Duration, f func() error) error {
// retry retries a given function until no return error; times out after retries*sleep
func retry(retries int, sleep time.Duration, f func() error) error {
err := error(nil)
for retry := 0; retry < retries; retry++ {
err = f()
Expand All @@ -402,3 +407,15 @@ func Retry(retries int, sleep time.Duration, f func() error) error {
}
return err
}

func warnOnError(callerFn string, f func() error) func() error {
return func() error {
err := f()
if err != nil {
logging.Warning("Delegated function error",
"func", callerFn,
"error", err)
}
return err
}
}

0 comments on commit 1d69ed6

Please sign in to comment.