Skip to content

Commit

Permalink
fix encap ip when the tunnel interface has multiple addresses (#2340)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzujian committed Feb 15, 2023
1 parent 746e5d0 commit f83af74
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/daemon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ func (config *Configuration) initNicConfig(nicBridgeMappings map[string]string)
klog.Errorf("failed to find iface %s, %v", tunnelNic, err)
return err
}
srcIPs, err := getSrcIPsByRoutes(iface)
if err != nil {
return fmt.Errorf("failed to get src IPs by routes on interface %s: %v", iface.Name, err)
}
addrs, err := iface.Addrs()
if err != nil {
return fmt.Errorf("failed to get iface addr. %v", err)
Expand All @@ -210,14 +214,16 @@ func (config *Configuration) initNicConfig(nicBridgeMappings map[string]string)
if ip := net.ParseIP(ipStr); ip == nil || ip.IsLinkLocalUnicast() {
continue
}
encapIP = ipStr
break
if len(srcIPs) == 0 || util.ContainsString(srcIPs, ipStr) {
encapIP = ipStr
break
}
}
if len(encapIP) == 0 {
return fmt.Errorf("iface %s has no valid IP address", tunnelNic)
}

klog.Infof("use %s as tunnel interface", iface.Name)
klog.Infof("use %s on %s as tunnel address", encapIP, iface.Name)
mtu = iface.MTU
config.tunnelIface = iface.Name
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/daemon/config_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,25 @@ import (

const defaultBindSocket = "/run/openvswitch/kube-ovn-daemon.sock"

func getSrcIPsByRoutes(iface *net.Interface) ([]string, error) {
link, err := netlink.LinkByName(iface.Name)
if err != nil {
return nil, fmt.Errorf("failed to get link %s: %v", iface.Name, err)
}
routes, err := netlink.RouteList(link, netlink.FAMILY_ALL)
if err != nil {
return nil, fmt.Errorf("failed to get routes on link %s: %v", iface.Name, err)
}

srcIPs := make([]string, 0, 2)
for _, r := range routes {
if r.Src != nil && r.Scope == netlink.SCOPE_LINK {
srcIPs = append(srcIPs, r.Src.String())
}
}
return srcIPs, nil
}

func getIfaceByIP(ip string) (string, int, error) {
links, err := netlink.LinkList()
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions pkg/daemon/config_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package daemon
import (
"context"
"fmt"
"net"
"strings"

"github.com/Microsoft/hcsshim"
Expand All @@ -16,6 +17,11 @@ import (

const defaultBindSocket = util.WindowsListenPipe

func getSrcIPsByRoutes(iface *net.Interface) ([]string, error) {
// to be implemented in the future
return nil, nil
}

func getIfaceByIP(ip string) (string, int, error) {
iface, err := util.GetInterfaceByIP(ip)
if err != nil {
Expand Down

0 comments on commit f83af74

Please sign in to comment.