ILD

基于tun/udp的VPN的MTU计算
作者:Yuan Jianpeng 邮箱:yuanjp89@163.com
发布时间:2019-5-24 站点:Inside Linux Development

MTU指的是以太帧的payload的最大长度,为1500。


以太帧的最大长度要再加上14个字节的以太头,4个字节的校验,最大变成1518,如果在算上4个字节的VLAN,那么可以达到1522。



以太帧最短是64个字节。


Linux使用ping测试MTU,

ping -M do -s 1470  8.8.8.8

-M 是路径MTU发现选项,do表示禁止分片

-s packetsize,整个包的大小,要加上14个字节的以太头,20个字节的IP头,8个字节的ICMP头,


packetsize默认为56,加上20+8,则以太payload 84。


1500-28=1472.

用1472去ping,外网报错,太长。

1
2
3
4
 ping -M do -s 1472 baidu.com
PING baidu.com (123.125.114.144) 1472(1500) bytes of data.
ping: local error: Message too long, mtu=1492
ping: local error: Message too long, mtu=1492

显示MTU为1492,


用1472去ping网关的LAN口是能通的,说明是WAN是某种拨号方式,占用了一些字节,限制了MTU。

1
2
3
4
$ ping -M do  -s 1472 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 1472(1500) bytes of data.
1480 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.993 ms
1480 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.772 ms


改成1492-28=1464,在去ping外网,则ok

$ ping -M do  -s 1464 baidu.com

PING baidu.com (123.125.114.144) 1464(1492) bytes of data.

1472 bytes from 123.125.114.144: icmp_seq=1 ttl=52 time=48.1 ms

1472 bytes from 123.125.114.144: icmp_seq=2 ttl=52 time=48.3 ms


但是增加到1466,就不能通了。


TUN+UDP VPN MTU计算

由于TUN读出来的是不带以太头的,所以 IP头20,UDP头8,VPN私有头6字节,所以TUN的MTU。

    MTU (tun) = MTU (eth) - 20 - 8 - 6

如上,如果是1492的话,那么,tun的MTU应该设置成 1492 - 20 - 8 - 6 = 1458


https://searchnetworking.techtarget.com/answer/Minimum-and-maximum-Ethernet-frame-sizes

https://en.wikipedia.org/wiki/Ethernet_frame

https://en.wikipedia.org/wiki/Maximum_transmission_unit


Copyright © linuxdev.cc 2017-2024. Some Rights Reserved.