-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/pmtu discovery #64
Open
acooks
wants to merge
4
commits into
ValentinBELYN:main
Choose a base branch
from
acooks:feature/pmtu-discovery
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,91 @@ | ||
import argparse | ||
import ipaddress | ||
from icmplib import ping | ||
|
||
|
||
def findmtu(host, verbose=False, debug=False): | ||
""" | ||
Find the PMTU to the specified host. | ||
Searches the range | ||
Host is an IPv4 or IPv6 address. | ||
""" | ||
|
||
# These headers get added to the ping payload to get to MTU | ||
SIZE_ICMP_HDR = 8 | ||
SIZE_IPV4_HDR = 20 | ||
SIZE_IPV6_HDR = 40 | ||
|
||
# These Ethernet headers are not included in the MTU. | ||
# Subtract them from the size-on-wire to get MTU. | ||
SIZE_ETH2_HDR = 14 | ||
|
||
if isinstance(ipaddress.ip_address(host), ipaddress.IPv6Address): | ||
size_ip_hdr = SIZE_IPV6_HDR | ||
else: | ||
size_ip_hdr = SIZE_IPV4_HDR | ||
|
||
lower = 900 | ||
upper = 10000 | ||
MTU = lower | ||
|
||
if verbose: | ||
print(f"\nhost: {host}") | ||
|
||
result = ping(host) | ||
if not result.is_alive: | ||
return 0 | ||
|
||
timeout = 1.5 * result.max_rtt / 1E3 | ||
interval = 0.5 * result.max_rtt / 1E3 | ||
if verbose: | ||
print(result) | ||
print(f'PMTU discovery timeout: {timeout}, interval: {interval}') | ||
|
||
test_size = 1700 | ||
while upper - lower > 1: | ||
test_MTU = test_size + SIZE_ICMP_HDR + size_ip_hdr | ||
if verbose: | ||
if debug: | ||
s = f" - MTU search range: {upper}-{lower} - {test_MTU+SIZE_ETH2_HDR} bytes on wire " | ||
else: | ||
s = "" | ||
print( | ||
f"checking frame size {test_MTU} {s}", | ||
end="", | ||
flush=True, | ||
) | ||
result = ping( | ||
host, | ||
count=2, | ||
interval=interval, | ||
timeout=timeout, | ||
payload_size=test_size, | ||
pmtudisc_opt="do", | ||
) | ||
if result.is_alive: | ||
lower = test_size | ||
MTU = test_MTU | ||
if verbose: | ||
print("✅") | ||
else: | ||
upper = test_size | ||
if verbose: | ||
print("❌") | ||
|
||
test_size = lower + int((upper - lower) / 2) | ||
|
||
return MTU | ||
|
||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument("-v", "--verbose", help="verbose output", action="store_true") | ||
parser.add_argument("-d", "--debug", help="debug output", action="store_true") | ||
parser.add_argument("hosts", help="one or more hosts to test", nargs='+') | ||
args = parser.parse_args() | ||
|
||
for host in args.hosts: | ||
mtu = findmtu(host, verbose=args.verbose, debug=args.debug) | ||
if mtu: | ||
print(f"host: {host} MTU: {mtu}") | ||
else: | ||
print("{host} is unreachable") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for implementing this feature. I'm currently looking into adding PMTUD to MeshPing, a little network monitoring tool I'm building. I'm currently working on a somewhat-hacky PMTUD implementation, but I'd also love to contribute back to icmplib, hence I found your PR. Your implementation looks a lot cleaner than mine 😄 I'd just like to point out that, rather than guessing and trying new MTU values when you get an error back, the new MTU is actually included in the downstream gateways' ICMP messages, and you can query them from the socket:
Similar for IPv6:
Maybe this would be a nice addition? 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the comment and apologies for the delayed response.
I agree, a brute-force PMTU discovery mechanism like this is not often needed these days.
However, the reason I had for implementing this feature is that there are some network devices, like fibre media converters, that operate as bridges on an Ethernet segment, and are entirely transparent at the IP layer - except when they're dropping large Ethernet frames.
In that scenario, there are no ICMP messages to signal the MTU constraint, and that's the scenario I needed this tool for.