Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix encap ip when the tunnel interface has multiple addresses #2340

Merged
merged 1 commit into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions pkg/daemon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,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 @@ -213,14 +217,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