Skip to content

Commit

Permalink
add ovn0 ipv6 link local address
Browse files Browse the repository at this point in the history
Signed-off-by: clyi <clyi@alauda.io>
  • Loading branch information
changluyi committed Sep 22, 2024
1 parent 1fe9e7c commit bac9278
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions pkg/daemon/ovs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,29 @@ func configureMirrorLink(portName string, _ int) error {
return nil
}

// Convert MAC address to EUI-64 and generate link-local IPv6 address
func macToLinkLocalIPv6(mac net.HardwareAddr) (net.IP, error) {
if len(mac) != 6 {
return nil, fmt.Errorf("invalid MAC address length")
}

// Create EUI-64 format
eui64 := make([]byte, 8)
copy(eui64[0:3], mac[0:3]) // Copy the first 3 bytes
eui64[3] = 0xff // Insert ff
eui64[4] = 0xfe // Insert fe
copy(eui64[5:], mac[3:]) // Copy the last 3 bytes

// Flip the 7th bit of the first byte
eui64[0] ^= 0x02

// Prepend the link-local prefix
linkLocalIPv6 := net.IP{0xfe, 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
copy(linkLocalIPv6[8:], eui64)

return linkLocalIPv6, nil
}

func configureNic(link, ip string, macAddr net.HardwareAddr, mtu int, detectIPConflict, setUfoOff bool) error {
nodeLink, err := netlink.LinkByName(link)
if err != nil {
Expand Down Expand Up @@ -868,14 +891,32 @@ func configureNic(link, ip string, macAddr net.HardwareAddr, mtu int, detectIPCo
if err != nil {
return fmt.Errorf("can not get addr %s: %v", nodeLink, err)
}

needAddIPv6LinkLocal := true
for _, ipAddr := range ipAddrs {
if ipAddr.IP.IsLinkLocalUnicast() {
// skip 169.254.0.0/16 and fe80::/10
if util.CheckProtocol(ipAddr.IP.String()) == kubeovnv1.ProtocolIPv6 {
needAddIPv6LinkLocal = false
}
continue
}
ipDelMap[ipAddr.IPNet.String()] = ipAddr
}

if needAddIPv6LinkLocal {
linkLocal, err := macToLinkLocalIPv6(macAddr)
if err != nil {
return fmt.Errorf("failed to generate link-local address: %v", err)
}
ipAddMap[linkLocal.String()] = netlink.Addr{
IPNet: &net.IPNet{
IP: linkLocal,
Mask: net.CIDRMask(64, 128),
},
}
}

for _, ipStr := range strings.Split(ip, ",") {
// Do not reassign same address for link
if _, ok := ipDelMap[ipStr]; ok {
Expand All @@ -896,6 +937,7 @@ func configureNic(link, ip string, macAddr net.HardwareAddr, mtu int, detectIPCo
return fmt.Errorf("delete address %s: %v", addr, err)
}
}

for ip, addr := range ipAddMap {
if detectIPConflict && addr.IP.To4() != nil {
ip := addr.IP.String()
Expand Down

0 comments on commit bac9278

Please sign in to comment.