Skip to content

Commit

Permalink
Generates random MAC address when effective virtual function MAC addr…
Browse files Browse the repository at this point in the history
…ess is zero.

This is to set VF effective MAC addresses when the VF effective MAC address is all zero for some vendor NICs.
  • Loading branch information
vrindle authored and bn222 committed Jun 22, 2022
1 parent eba4823 commit d09c699
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ func configSriovDevice(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetwor
return err
}
}

if err = setVfAdminMac(addr, pfLink, vfLink); err != nil {
glog.Errorf("configSriovDevice(): fail to configure VF admin mac address for device %s %q", addr, err)
return err
Expand Down Expand Up @@ -375,6 +376,17 @@ func configSriovDevice(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetwor
return nil
}

func generateRandomMacAddressVfs() ([]byte, error) {
buf :=make([]byte,6)
_, err := rand.Read(buf)
if err != nil {
return buf, err
}
// Set the local bit
buf[0] = (buf[0] & 0xfe) | 2
return buf, nil
}

func setSriovNumVfs(pciAddr string, numVfs int) error {
glog.V(2).Infof("setSriovNumVfs(): set NumVfs for device %s", pciAddr)
numVfsFilePath := filepath.Join(sysBusPciDevices, pciAddr, numVfsFile)
Expand Down Expand Up @@ -600,6 +612,35 @@ func vfIsReady(pciAddr string) (netlink.Link, error) {
return vfLink, nil
}

func isValidAddress(address []byte) bool {
invalidAddresses := [][]byte {
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
}
for _,invalidAddress := range invalidAddresses {
if bytes.Equal(address, invalidAddress) {
return false
}
}
return true
}

func getEffectiveMacAddress(hwAddr net.HardwareAddr) (net.HardwareAddr, error) {
newAddr := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
zeroAddr := net.HardwareAddr(newAddr[:])
if bytes.Equal(hwAddr,zeroAddr) {
for isValidAddress(newAddr) == false {
newAddr2, err := generateRandomMacAddressVfs()
newAddr = newAddr2
if err != nil {
return nil, err
}
}
return net.HardwareAddr(newAddr[:]), nil
}
return hwAddr, nil
}

func setVfAdminMac(vfAddr string, pfLink, vfLink netlink.Link) error {
glog.Infof("setVfAdminMac(): VF %s", vfAddr)

Expand All @@ -608,8 +649,12 @@ func setVfAdminMac(vfAddr string, pfLink, vfLink netlink.Link) error {
glog.Errorf("setVfAdminMac(): unable to get VF id %+v %q", vfAddr, err)
return err
}
hwAddr, err := getEffectiveMacAddress(vfLink.Attrs().HardwareAddr)
if err != nil {
return err
}

if err := netlink.LinkSetVfHardwareAddr(pfLink, vfID, vfLink.Attrs().HardwareAddr); err != nil {
if err := netlink.LinkSetVfHardwareAddr(pfLink, vfID, hwAddr); err != nil {
return err
}

Expand Down

0 comments on commit d09c699

Please sign in to comment.