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 Jan 17, 2025
1 parent 6fb3ac9 commit eef83e2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 21 deletions.
29 changes: 8 additions & 21 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 @@ -40,10 +41,6 @@ func (o *OVNKubernetesConfig) Validate() error {
if err != nil {
return fmt.Errorf("failed to validate OVS bridge: %w", err)
}
err = o.validateConfig()
if err != nil {
return fmt.Errorf("failed to validate OVN-K configuration: %w", err)
}
return nil
}

Expand All @@ -56,27 +53,17 @@ func (o *OVNKubernetesConfig) validateOVSBridge() error {
return nil
}

// validateConfig validates the user defined configuration in /etc/microshift/ovn.yaml
func (o *OVNKubernetesConfig) validateConfig() error {
// validate MTU conf
iface, err := net.InterfaceByName(OVNGatewayInterface)
if err != nil {
return fmt.Errorf("failed to find OVN gateway interface %q: %w", OVNGatewayInterface, err)
}

if iface.MTU < o.MTU {
return fmt.Errorf("interface MTU (%d) is too small for specified overlay (%d)", iface.MTU, o.MTU)
}
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)
klog.Infof("getClusterMTU: finding default route interface")
link, err := util.FindDefaultRouteIface()

if err == nil && link.MTU > 0 {
klog.Infof("getClusterMTU: Using Interface %s with MTU %d as source default route.", link.Name, link.MTU)
o.MTU = link.MTU
} else {
klog.Infof("getClusterMTU: Couldnt extract MTU from the default route interface , Using Default MTU.")
o.MTU = defaultMTU
}

Expand Down
60 changes: 60 additions & 0 deletions pkg/util/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ import (

var previousGatewayIP string = ""

type routeStruct struct {
// Name of interface
Iface string

// big-endian hex string
Gateway string
}

// Remember whether we have successfully found the hard-coded nodeIP
// on this host.
var foundHardCodedNodeIP bool
Expand Down Expand Up @@ -266,3 +274,55 @@ func GetHostIPv6(ipHint string) (string, error) {

return "", fmt.Errorf("unable to find host IPv6 address")
}

// Find the Default route Interface based on /proc/net/route or /proc/net/ipv6_route
func FindDefaultRouteIface() (iface *tcpnet.Interface, err error) {

// first try IPv4
parsedStruct, err := findDefaultRouteForFamily(netlink.FAMILY_V4)
if err != nil {

// try IPv6
parsedStruct, err = findDefaultRouteForFamily(netlink.FAMILY_V6)
if err != nil {
return nil, err
}
}

iface, err = tcpnet.InterfaceByName(parsedStruct.Iface)
if err != nil {
return nil, err
}

return iface, nil
}

func findDefaultRouteForFamily(family int) (routeStruct, error) {
handle, err := netlink.NewHandle()
if err != nil {
fmt.Printf("error opening New Handle %w ", err)
}

routeList, err := handle.RouteList(nil, family)
if err != nil {
fmt.Printf("error listing routes %w \n ", err)
}

for _, route := range routeList {

// for Default route the Destination should be nil (0)
if route.Dst == nil {
link, err := handle.LinkByIndex(route.LinkIndex)
if err != nil {
fmt.Printf("error listing Link by Index %w \n ", err)
}
return routeStruct{
Iface: link.Attrs().Name,
Gateway: route.Gw.String(),
}, nil

}

}
return routeStruct{}, fmt.Errorf("no default gateway found")
}

0 comments on commit eef83e2

Please sign in to comment.