Skip to content

Commit 47f8c01

Browse files
committed
fix static mac pod conflict with gateway mac (#5623)
* fix static mac pod conflict with gateway mac Signed-off-by: clyi <[email protected]> --------- Signed-off-by: clyi <[email protected]>
1 parent 2898de1 commit 47f8c01

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

pkg/controller/subnet.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,18 @@ func (c *Controller) handleAddOrUpdateSubnet(key string) error {
815815
return err
816816
}
817817

818+
// Record the gateway MAC in ipam if router port exists
819+
if needRouter {
820+
routerPortName := ovs.LogicalRouterPortName(vpc.Status.Router, subnet.Name)
821+
if lrp, err := c.OVNNbClient.GetLogicalRouterPort(routerPortName, true); err == nil && lrp != nil && lrp.MAC != "" {
822+
if err := c.ipam.RecordGatewayMAC(subnet.Name, lrp.MAC); err != nil {
823+
klog.Warningf("failed to record gateway MAC %s for subnet %s: %v", lrp.MAC, subnet.Name, err)
824+
}
825+
} else {
826+
klog.V(3).Infof("router port %s not found or has no MAC, skipping gateway MAC record", routerPortName)
827+
}
828+
}
829+
818830
multicastSnoopFlag := map[string]string{"mcast_snoop": "true", "mcast_querier": "false"}
819831
if subnet.Spec.EnableMulicastSnoop {
820832
if err := c.OVNNbClient.LogicalSwitchUpdateOtherConfig(subnet.Name, ovsdb.MutateOperationInsert, multicastSnoopFlag); err != nil {

pkg/ipam/ipam.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,17 @@ func (ipam *IPAM) IPPoolStatistics(subnet, ippool string) (
438438
}
439439
return s.IPPoolStatistics(ippool)
440440
}
441+
442+
func (ipam *IPAM) RecordGatewayMAC(subnetName, gatewayMAC string) error {
443+
ipam.mutex.Lock()
444+
defer ipam.mutex.Unlock()
445+
446+
subnet, ok := ipam.Subnets[subnetName]
447+
if !ok {
448+
return fmt.Errorf("subnet %s not found in ipam", subnetName)
449+
}
450+
451+
subnet.GatewayMAC = gatewayMAC
452+
klog.Infof("recorded gateway MAC %s for subnet %s", gatewayMAC, subnetName)
453+
return nil
454+
}

pkg/ipam/subnet.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type Subnet struct {
3737
PodToNicList map[string][]string
3838
V4Gw string
3939
V6Gw string
40+
GatewayMAC string
4041

4142
IPPools map[string]*IPPool
4243
}
@@ -133,8 +134,14 @@ func (s *Subnet) GetRandomMac(podName, nicName string) string {
133134
if mac, ok := s.NicToMac[nicName]; ok {
134135
return mac
135136
}
137+
138+
var exclusionMACs []string
139+
if s.GatewayMAC != "" {
140+
exclusionMACs = append(exclusionMACs, s.GatewayMAC)
141+
}
142+
136143
for {
137-
mac := util.GenerateMac()
144+
mac := util.GenerateMacWithExclusion(exclusionMACs)
138145
if _, ok := s.MacToPod[mac]; !ok {
139146
s.MacToPod[mac] = podName
140147
s.NicToMac[nicName] = mac
@@ -152,6 +159,11 @@ func (s *Subnet) GetStaticMac(podName, nicName, mac string, checkConflict bool)
152159
klog.Errorf("mac %s has been allocated to pod %s", mac, p)
153160
return ErrConflict
154161
}
162+
// Check if static MAC conflicts with gateway MAC
163+
if s.GatewayMAC != "" && mac == s.GatewayMAC {
164+
klog.Errorf("static MAC %s conflicts with gateway MAC", mac)
165+
return ErrConflict
166+
}
155167
}
156168
s.MacToPod[mac] = podName
157169
s.NicToMac[nicName] = mac

pkg/util/net.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"math/big"
99
"net"
1010
"os"
11+
"slices"
1112
"strconv"
1213
"strings"
1314
"time"
@@ -47,6 +48,15 @@ func GenerateMac() string {
4748
return net.HardwareAddr(buf).String()
4849
}
4950

51+
func GenerateMacWithExclusion(exclusionMACs []string) string {
52+
for {
53+
mac := GenerateMac()
54+
if !slices.Contains(exclusionMACs, mac) {
55+
return mac
56+
}
57+
}
58+
}
59+
5060
func IP2BigInt(ipStr string) *big.Int {
5161
ipBigInt := big.NewInt(0)
5262
if CheckProtocol(ipStr) == kubeovnv1.ProtocolIPv4 {

0 commit comments

Comments
 (0)