-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
decouple mtu calculation for the main route
- Loading branch information
Showing
3 changed files
with
82 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |