Skip to content

Commit

Permalink
Merge pull request #325 from vrindle/generate_random_mac_address_for_vfs
Browse files Browse the repository at this point in the history
Generate random mac address when virtual mac address is zero.
  • Loading branch information
bn222 authored Jun 23, 2022
2 parents eba4823 + 24a50ba commit 20ba341
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) {
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

2 comments on commit 20ba341

@adrianchiris
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bn222 Why was this merged ? there should be approval from more than company on these type of things.

@bn222
Copy link
Collaborator Author

@bn222 bn222 commented on 20ba341 Jun 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adrianchiris For the record, we discussed offline. This will be reverted asap here:
#327

Please sign in to comment.