Skip to content

Commit

Permalink
fix mtu detection
Browse files Browse the repository at this point in the history
  • Loading branch information
aojea committed Jan 8, 2025
1 parent 2d81eab commit 20ac1d0
Showing 1 changed file with 15 additions and 27 deletions.
42 changes: 15 additions & 27 deletions cmd/cni-kindnet/netdev.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net"
"os"
"runtime"
"sort"
"strconv"

"github.com/vishvananda/netlink"
Expand Down Expand Up @@ -290,38 +291,25 @@ func deletePodInterface(ifName string, netNS string) error {
}

func getDefaultGwInterfaceMTU() int {
routes, err := netlink.RouteList(nil, netlink.FAMILY_ALL)
routes, err := netlink.RouteListFiltered(netlink.FAMILY_ALL, &netlink.Route{Dst: nil}, netlink.RT_FILTER_DST)
if err != nil {
return 0
}

if len(routes) == 0 {
return 0
}
// use the route with higher priority
sort.Slice(routes, func(i, j int) bool {
return routes[i].Priority < routes[j].Priority
})
// use the mtu of the first interface
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 {
logger.Printf("Failed to get interface link for route %v : %v", r, err)
continue
}
return intfLink.Attrs().MTU
}

// 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 {
logger.Printf("Failed to get interface link for route %v : %v", r, err)
continue
}
return intfLink.Attrs().MTU
intfLink, err := netlink.LinkByIndex(r.LinkIndex)
if err != nil {
logger.Printf("Failed to get interface link for route %v : %v", r, err)
continue
}
return intfLink.Attrs().MTU
}
return 0
}

0 comments on commit 20ac1d0

Please sign in to comment.