Skip to content

Commit

Permalink
decouple mtu calculation for the main route
Browse files Browse the repository at this point in the history
  • Loading branch information
aojea committed May 26, 2024
1 parent 2750aa8 commit e509b96
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 24 deletions.
16 changes: 0 additions & 16 deletions cmd/kindnetd/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package main

import (
"io"
"net"
"os"
"reflect"
"text/template"
Expand Down Expand Up @@ -62,21 +61,6 @@ func ComputeCNIConfigInputs(node *corev1.Node) CNIConfigInputs {
}
}

// computeBridgeMTU finds the mtu for the eth0 interface
// otherwise it defaults to ptp default behavior of being set by kernel
func computeBridgeMTU() (int, error) {
interfaces, err := net.Interfaces()
if err != nil {
return 0, err
}
for _, inter := range interfaces {
if inter.Name == "eth0" {
return inter.MTU, nil
}
}
return 0, errors.New("Found no eth0 device")
}

// cniConfigPath is where kindnetd will write the computed CNI config
const cniConfigPath = "/etc/cni/net.d/10-kindnet.conflist"

Expand Down
17 changes: 9 additions & 8 deletions cmd/kindnetd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"os/signal"
"time"

utilnet "github.com/aojea/kindnet/pkg/net"

"golang.org/x/sys/unix"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/sets"
Expand All @@ -49,15 +51,14 @@ import (
// TODO: improve logging & error handling

// IPFamily defines kindnet networking operating model
type IPFamily string
type IPFamily int

const (
// IPv4Family sets IPFamily to ipv4
IPv4Family IPFamily = "ipv4"
// IPv6Family sets IPFamily to ipv6
IPv6Family IPFamily = "ipv6"
// DualStackFamily sets ClusterIPFamily to DualStack
DualStackFamily IPFamily = "dualstack"
// Family type definitions
AllFamily IPFamily = unix.AF_UNSPEC
IPv4Family IPFamily = unix.AF_INET
IPv6Family IPFamily = unix.AF_INET6
DualStackFamily IPFamily = 12 // AF_INET + AF_INET6
)

func main() {
Expand Down Expand Up @@ -111,7 +112,7 @@ func main() {
))
}

mtu, err := computeBridgeMTU()
mtu, err := utilnet.GetMTU(int(AllFamily))
klog.Infof("setting mtu %d for CNI \n", mtu)
if err != nil {
klog.Infof("Failed to get MTU size from interface eth0, using kernel default MTU size error:%v", err)
Expand Down
73 changes: 73 additions & 0 deletions pkg/net/host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package net

import (
"fmt"
"log"
"net"

"github.com/vishvananda/netlink"
)

// GetMTU returns the MTU used for the IP family
func GetMTU(ipFamily int) (int, error) {
iface, err := getDefaultGwIf(ipFamily)
if err != nil {
return 0, err
}
mtu, err := getInterfaceMTU(iface)
if err != nil {
return 0, err
}
return mtu, nil
}

// getInterfaceMTU finds the mtu for the interface
func getInterfaceMTU(iface string) (int, error) {
interfaces, err := net.Interfaces()
if err != nil {
return 0, err
}
for _, inter := range interfaces {
if inter.Name == iface {
return inter.MTU, nil
}
}
return 0, fmt.Errorf("no %s device found", iface)
}

func getDefaultGwIf(ipFamily int) (string, error) {
routes, err := netlink.RouteList(nil, ipFamily)
if err != nil {
return "", err
}

for _, r := range routes {
// no multipath
if len(r.MultiPath) == 0 {
if r.Gw == nil {
continue
}
intfLink, err := netlink.LinkByIndex(r.LinkIndex)
if err != nil {
log.Printf("Failed to get interface link for route %v : %v", r, err)
continue
}
return intfLink.Attrs().Name, nil
}

// multipath, use the first valid entry
// xref: https://github.com/vishvananda/netlink/blob/6ffafa9fc19b848776f4fd608c4ad09509aaacb4/route.go#L137-L145
for _, nh := range r.MultiPath {
if nh.Gw == nil {
continue
}
intfLink, err := netlink.LinkByIndex(r.LinkIndex)
if err != nil {
log.Printf("Failed to get interface link for route %v : %v", r, err)
continue
}
return intfLink.Attrs().Name, nil
}
}
return "", fmt.Errorf("not routes found")
}

0 comments on commit e509b96

Please sign in to comment.