Skip to content

Commit

Permalink
OCPBUGS-38468: use default route mtu
Browse files Browse the repository at this point in the history
Signed-off-by: Evgeny Slutsky <[email protected]>
  • Loading branch information
eslutsky committed Dec 13, 2024
1 parent 47bbf17 commit c934c8f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pkg/config/ovn/ovn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"regexp"

"github.com/openshift/microshift/pkg/util"
"k8s.io/klog/v2"
"sigs.k8s.io/yaml"
)
Expand Down Expand Up @@ -70,10 +71,10 @@ func (o *OVNKubernetesConfig) validateConfig() error {
return nil
}

// getClusterMTU retrieves MTU from ovn-kubernetes gateway interface "br-ex",
// and falls back to use 1500 when "br-ex" mtu is unable to get or less than 0.
// getClusterMTU retrieves MTU from the default route network interface,
// and falls back to use 1500 when unable to get the mtu or less than 0.
func (o *OVNKubernetesConfig) getClusterMTU(multinode bool) {
link, err := net.InterfaceByName(OVNGatewayInterface)
link, err := util.FindDefaultRouteInterface()
if err == nil && link.MTU > 0 {
o.MTU = link.MTU
} else {
Expand Down
54 changes: 54 additions & 0 deletions pkg/util/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,57 @@ func GetHostIPv6(ipHint string) (string, error) {

return "", fmt.Errorf("unable to find host IPv6 address")
}
func FindDefaultRouteInterface() (*tcpnet.Interface, error) {
nodeIP, err := GetHostIP("")
if err != nil {
return nil, fmt.Errorf("failed to get host IP: %v", err)
}

ip := tcpnet.ParseIP(nodeIP)
if ip == nil {
return nil, fmt.Errorf("invalid IP address: %s", nodeIP)
}

ifaces, err := tcpnet.Interfaces()

if err != nil {
return nil, err
}

isDown := func(iface tcpnet.Interface) bool {
return iface.Flags&1 == 0
}

for _, iface := range ifaces {
if isDown(iface) {
continue
}
found, err := ipAddrExistsAtInterface(ip, iface)
if err != nil {
return nil, err
}
if !found {
continue
}

return &iface, nil
}
return nil, fmt.Errorf("no usable interface found")
}

func ipAddrExistsAtInterface(ipAddr tcpnet.IP, iface tcpnet.Interface) (bool, error) {
addrs, err := iface.Addrs()

if err != nil {
return false, err
}

for _, a := range addrs {
if ipnet, ok := a.(*tcpnet.IPNet); ok {
if ipnet.IP.Equal(ipAddr) {
return true, nil
}
}
}
return false, nil
}

0 comments on commit c934c8f

Please sign in to comment.