不同的以太網接入設備,一幀能傳輸的數據量是有差異的。
普通的以太網卡,一幀最多能夠傳輸 1500 字節的數據;而某些虛擬設備,傳輸能力要打些折扣。此外,鏈路層除了以太網還有其他協議,這些協議中數據幀傳輸能力也有差異。
最大傳輸單元
如果待發送的數據超過幀的最大承載能力,就需要先對數據進行分片,然后再通過若干個幀進行傳輸。
下面是一個典型例子,待發送的數據總共 4000 字節,假設以太網設備一幀最多只能承載 1500 字節。很明顯,數據需要划分成 3 片,再通過 3 個幀進行發送:
換句話講,我們需要知道接入設備一幀最多能發送多少數據。這個參數在網絡領域被稱為 最大傳輸單元 ( maximum transmission unit ),簡稱 MTU 。MTU 描述鏈路層能夠傳輸的最大數據單元。
查看、設置MTU
我們知道,在 Linux 系統可以用 ifconfig 或者 ip 這兩個命令來查看網卡信息,其中包括 MTU 。
接下來,我們打開 docker 進入 netbox 環境進行演示:
docker run --name netbox --rm -it --privileged --cap-add=NET_ADMIN --cap-add=SYS_ADMIN -v /data -h netbox fasionchan/netbox:0.5 bash /script/netbox.sh
先以 ifconfig 命令為例:
root@netbox [ ~ ] ➜ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.2 netmask 255.255.255.0 broadcast 0.0.0.0
ether 5a:ff:7e:28:81:bc txqueuelen 1000 (Ethernet)
RX packets 24 bytes 2165 (2.1 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 27 bytes 2164 (2.1 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ip 命令也可以,我們更推薦使用這個:
root@netbox [ ~ ] ➜ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: tunl0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ipip 0.0.0.0 brd 0.0.0.0
3: ip6tnl0@NONE: <NOARP> mtu 1452 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/tunnel6 :: brd ::
6: eth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
link/ether 5a:ff:7e:28:81:bc brd ff:ff:ff:ff:ff:ff link-netnsid 0
我們可以用 ip 命令,來修改 eth0 的 MTU , 以修改 eth0 網卡 MTU 為例:
root@netbox [ ~ ] ➜ ip link set eth0 mtu 68
不同的接入設備,支持的 MTU 范圍不一樣。如果我們將 MTU 設置得太小,設備將報錯:
root@netbox [ ~ ] ➜ ip link set eth0 mtu 40
Error: mtu less than device minimum.
MTU 對發送環節的影響
我們以兩台用網線直接相連的主機作為實驗環境,來觀察網卡 MTU 對發送、接收以太網幀的影響:
實驗環境同樣通過 docker 來啟動:
docker run --name mtu-lab --rm -it --privileged --cap-add=NET_ADMIN --cap-add=SYS_ADMIN -v /data -h mtu-lab fasionchan/netbox:0.5 bash /script/cable-lab.sh
我們將 ant 主機 eth0 網卡的 MTU 設置成 68 ,發現數據大於 68 字節的以太網幀將無法發送:
root@ant [ ~ ] ➜ ip link set eth0 mtu 68
root@ant [ ~ ] ➜ sendether -i eth0 -t 32:65:21:d3:01:2f -d 'hello, world!hello, world!hello, world!hello, world!hello, world!hello, world!hello, world!hello, world!hello, world!hello, world!'
Fail to send ethernet frame: : Message too long
將 MTU 恢復為默認值 1500 以后,可以成功發送:
root@ant [ ~ ] ➜ ip link set eth0 mtu 1500
root@ant [ ~ ] ➜ sendether -i eth0 -t 32:65:21:d3:01:2f -d 'hello, world!hello, world!hello, world!hello, world!hello, world!hello, world!hello, world!hello, world!hello, world!hello, world!'
這就是 MTU 對發送環節的影響:如果數據量大於 MTU ,則無法通過單個以太網幀發送出去,只能以 MTU 為單位對數據進行分片,再封裝成若干個幀進行發送。
MTU 對接收環節的影響
那么,MTU 對接收環節有什么影響呢?
我們將 bee 主機 eth0 網卡的 MTU 設置成 68 ,並啟動 tcpdump 進行抓包:
root@ant [ ~ ] ➜ ip link set eth0 mtu 68
root@ant [ ~ ] ➜ tcpdump -ni eth0
接着,我們在主機 ant 上,向主機 bee 發送一個數據大於 68 字節的以太網幀:
root@ant [ ~ ] ➜ sendether -i eth0 -t 32:65:21:d3:01:2f -d 'hello, world!hello, world!hello, world!hello, world!hello, world!hello, world!hello, world!hello, world!hello, world!hello, world!'
然而,主機 ant 上的 tcpdump 程序告訴我們,eth0 網卡並沒有收到這個幀!由此可見,以太網幀數據長度一旦大於接收設備 MTU ,將被丟棄。
結論
- 待發送以太網幀數據長度大於發送設備 MTU ,則無法發送;
- 待接收以太網幀數據長度大於接收設備 MTU ,則無法接收;
【小菜學網絡】系列文章首發於公眾號【小菜學編程】,敬請關注: