From 8d59c9195a509b0a33e4c67077f077b7bb73c6dc Mon Sep 17 00:00:00 2001 From: Christopher Dziomba Date: Tue, 7 Nov 2023 11:51:38 +0100 Subject: [PATCH 1/2] Generate Bridge MAC from Underlay RMAC --- pkg/nl/create.go | 13 ++++++++++++- pkg/nl/layer2.go | 2 +- pkg/nl/layer3.go | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pkg/nl/create.go b/pkg/nl/create.go index 86f90b9c..f4f11385 100644 --- a/pkg/nl/create.go +++ b/pkg/nl/create.go @@ -36,7 +36,7 @@ func (n *NetlinkManager) createVRF(vrfName string, table int) (*netlink.Vrf, err return &netlinkVrf, nil } -func (n *NetlinkManager) createBridge(bridgeName string, macAddress *net.HardwareAddr, masterIdx, mtu int) (*netlink.Bridge, error) { +func (n *NetlinkManager) createBridge(bridgeName string, macAddress *net.HardwareAddr, masterIdx, mtu int, underlayRMAC bool) (*netlink.Bridge, error) { netlinkBridge := netlink.Bridge{ LinkAttrs: netlink.LinkAttrs{ Name: bridgeName, @@ -48,6 +48,17 @@ func (n *NetlinkManager) createBridge(bridgeName string, macAddress *net.Hardwar } if macAddress != nil { netlinkBridge.LinkAttrs.HardwareAddr = *macAddress + } else if underlayRMAC { + _, vxlanIP, err := getInterfaceAndIP(underlayLoopback) + if err != nil { + return nil, err + } + + generatedMac, err := generateMAC(vxlanIP) + if err != nil { + return nil, err + } + netlinkBridge.LinkAttrs.HardwareAddr = generatedMac } if err := netlink.LinkAdd(&netlinkBridge); err != nil { diff --git a/pkg/nl/layer2.go b/pkg/nl/layer2.go index f5d8258d..fb96e7b7 100644 --- a/pkg/nl/layer2.go +++ b/pkg/nl/layer2.go @@ -100,7 +100,7 @@ func (n *NetlinkManager) CreateL2(info *Layer2Information) error { } func (n *NetlinkManager) setupBridge(info *Layer2Information, masterIdx int) (*netlink.Bridge, error) { - bridge, err := n.createBridge(fmt.Sprintf("%s%d", layer2Prefix, info.VlanID), info.AnycastMAC, masterIdx, info.MTU) + bridge, err := n.createBridge(fmt.Sprintf("%s%d", layer2Prefix, info.VlanID), info.AnycastMAC, masterIdx, info.MTU, false) if err != nil { return nil, err } diff --git a/pkg/nl/layer3.go b/pkg/nl/layer3.go index e9896402..3574d9ab 100644 --- a/pkg/nl/layer3.go +++ b/pkg/nl/layer3.go @@ -39,7 +39,7 @@ func (n *NetlinkManager) CreateL3(info VRFInformation) error { return err } - bridge, err := n.createBridge(bridgePrefix+info.Name, nil, vrf.Attrs().Index, defaultMtu) + bridge, err := n.createBridge(bridgePrefix+info.Name, nil, vrf.Attrs().Index, defaultMtu, true) if err != nil { return err } From 58fda8a5ce398f8271e7eb729e103903584cf605 Mon Sep 17 00:00:00 2001 From: Christopher Dziomba Date: Tue, 7 Nov 2023 12:08:26 +0100 Subject: [PATCH 2/2] Rename function to getUnderlayInterfaceAndIP and remove params --- pkg/nl/create.go | 10 +++++----- pkg/nl/manager.go | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/nl/create.go b/pkg/nl/create.go index f4f11385..42f40dbd 100644 --- a/pkg/nl/create.go +++ b/pkg/nl/create.go @@ -49,7 +49,7 @@ func (n *NetlinkManager) createBridge(bridgeName string, macAddress *net.Hardwar if macAddress != nil { netlinkBridge.LinkAttrs.HardwareAddr = *macAddress } else if underlayRMAC { - _, vxlanIP, err := getInterfaceAndIP(underlayLoopback) + _, vxlanIP, err := getUnderlayInterfaceAndIP() if err != nil { return nil, err } @@ -72,7 +72,7 @@ func (n *NetlinkManager) createBridge(bridgeName string, macAddress *net.Hardwar } func (n *NetlinkManager) createVXLAN(vxlanName string, bridgeIdx, vni, mtu int, hairpin, neighSuppression bool) (*netlink.Vxlan, error) { - vxlanIf, vxlanIP, err := getInterfaceAndIP(underlayLoopback) + vxlanIf, vxlanIP, err := getUnderlayInterfaceAndIP() if err != nil { return nil, err } @@ -166,7 +166,7 @@ func (*NetlinkManager) setUp(intfName string) error { } func generateUnderlayMAC() (net.HardwareAddr, error) { - _, vxlanIP, err := getInterfaceAndIP(underlayLoopback) + _, vxlanIP, err := getUnderlayInterfaceAndIP() if err != nil { return nil, err } @@ -178,8 +178,8 @@ func generateUnderlayMAC() (net.HardwareAddr, error) { return generatedMac, nil } -func getInterfaceAndIP(name string) (int, net.IP, error) { - dummy := netlink.Dummy{LinkAttrs: netlink.LinkAttrs{Name: name}} +func getUnderlayInterfaceAndIP() (int, net.IP, error) { + dummy := netlink.Dummy{LinkAttrs: netlink.LinkAttrs{Name: underlayLoopback}} addresses, err := netlink.AddrList(&dummy, netlink.FAMILY_V4) if err != nil { diff --git a/pkg/nl/manager.go b/pkg/nl/manager.go index 7fcd2dcb..8873b742 100644 --- a/pkg/nl/manager.go +++ b/pkg/nl/manager.go @@ -29,6 +29,6 @@ type NetlinkManager struct { } func (*NetlinkManager) GetUnderlayIP() (net.IP, error) { - _, ip, err := getInterfaceAndIP(underlayLoopback) + _, ip, err := getUnderlayInterfaceAndIP() return ip, err }