Linux MTU 修改会影响两个方向吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6360916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:33:17  来源:igfitidea点击:

Do MTU modifications impact both directions?

linuxnetworkingmtuifconfig

提问by cateof

ifconfig 1.2.3.4 mtu 1492

ifconfig 1.2.3.4 mtu 1492

This will set MTU to 1492 for incoming, outgoing packets or both? I think it is only for incoming

这会将传入、传出数据包或两者的 MTU 设置为 1492?我认为它仅用于传入

采纳答案by Edward Thomson

TLDR:Both. It will only transmit packets with a payload length less than or equal to that size. Similarly, it will only accept packets with a payload length within your MTU. If a device sends a larger packet, it should respond with an ICMP unreachable (oversized) message.

TLDR:两者都有。它只会传输有效载荷长度小于或等于该大小的数据包。同样,它只会接受负载长度在您的 MTU 内的数据包。如果设备发送一个更大的数据包,它应该以 ICMP 不可达(超大)消息响应。

The nitty gritty:Tuning the MTU for your device is useful because other hops between you and your destination may encapsulate your packet in another form (for example, a VPN or PPPoE.) This layer around your packet results in a bigger packet being sent along the wire. If this new, larger packet exceeds the maximum size of the layer, then the packet will be split into multiple packets (in a perfect world) or will be dropped entirely (in the real world.)

本质:为您的设备调整 MTU 非常有用,因为您和目的地之间的其他跃点可能会以另一种形式(例如 VPN 或 PPPoE)封装您的数据包。围绕数据包的这一层会导致发送更大的数据包电线。如果这个新的、更大的数据包超过了层的最大大小,那么数据包将被分成多个数据包(在完美世界中)或将被完全丢弃(在现实世界中)。

As a practical example, consider having a computer connected over ethernet to an ADSL modem that speaks PPPoE to an ISP. Ethernet allows for a 1500 byte payload, of which 8 bytes will be used by PPPoE. Now we're down to 1492 bytes that can be delivered in a single packet to your ISP. If you were to send a full-size ethernet payload of 1500 bytes, it would get "fragmented" by your router and split into two packets (one with a 1492 byte payload, the other with an 8 byte payload.)

作为一个实际示例,考虑将计算机通过以太网连接到 ADSL 调制解调器,该调制解调器与 ISP 进行 PPPoE 通信。以太网允许 1500 字节的有效载荷,其中 8 个字节将被 PPPoE 使用。现在我们减少到 1492 字节,可以在单个数据包中传送到您的 ISP。如果您要发送 1500 字节的全尺寸以太网负载,它会被您的路由器“分段”并分成两个数据包(一个具有 1492 字节的负载,另一个具有 8 字节的负载。)

The problem comes when you want to send more data over this connection - lets say you wanted to send 3000 bytes: your computer would split this up based on your MTU - in this case, two packets of 1500 bytes each, and send them to your ADSL modem which would then split them up so that it can fulfill its MTU. Now your 3000 byte data has been fragmented into four packets: two with a payload of 1492 bytes and two with a payload of 8 bytes. This is obviously inefficient, we really only need three packets to send this data. Had your computer been configured with the correct MTU for the network, it would have sent this as three packets in the first place (two 1492 byte packets and one 16 byte packet.)

当您想通过此连接发送更多数据时会出现问题 - 假设您想发送 3000 字节:您的计算机将根据您的 MTU 将其拆分 - 在这种情况下,两个数据包每个 1500 字节,并将它们发送到您的ADSL 调制解调器然后将它们分开,以便它可以实现其 MTU。现在您的 3000 字节数据已被分成四个数据包:两个具有 1492 字节的有效载荷,两个具有 8 字节的有效载荷。这显然是低效的,我们真的只需要三个数据包就可以发送这个数据。如果您的计算机为网络配置了正确的 MTU,它首先会发送三个数据包(两个 1492 字节数据包和一个 16 字节数据包)。

To avoid this inefficiency, many IP stacks flip a bit in the IP header called "Don't Fragment." In this case, we would have sent our first 1500 byte packet to the ADSL modem and it would have rejected the packet, replying with an Internet Control (ICMP) message informing us that our packet is too large. We then would have retried the transmission with a smaller packet. This is called Path MTU discovery. Similarly, a layer below, at the TCP layer, another factor in avoiding fragmentation is the MSS (Maximum Segment Size) option where both hosts reply with the maximum size packet they can transfer without fragmenting. This is typically computed from the MTU.

为了避免这种低效率,许多 IP 堆栈在称为“Don't Fragment”的 IP 标头中稍微翻转。在这种情况下,我们会将第一个 1500 字节的数据包发送到 ADSL 调制解调器,它会拒绝该数据包,回复 Internet 控制 (ICMP) 消息,通知我们我们的数据包太大。然后,我们将使用较小的数据包重试传输。这称为路径 MTU 发现。类似地,在 TCP 层的下面一层,避免分段的另一个因素是 MSS(最大分段大小)选项,其中两个主机都回复它们可以在不分段的情况下传输的最大大小的数据包。这通常是根据 MTU 计算得出的。

The problem here arises when misconfigured firewalls drop all ICMP traffic. When you connect to (say) a web server, you build a TCP session and send that you're willing to accept TCP packets based on your 1500 byte MTU (since you're connected over ethernet to your router.) If the foreign web server wanted to send you a lot of data, they would split this into chunks that (when combined with the TCP and IP headers) came out to 1500 byte payloads and send them to you. Your ISP would receive one of these and then try to wrap it into a PPPoE packet to send to your ADSL modem, but it would be too large to send. So it would reply with an ICMP unreachable, which would (in a perfect world) cause the remote computer to downsize its MSS for the connection and retransmit. If there was a broken firewall in the way, however, this ICMP message would never be reached by the foreign web server and this packet would never make it to you.

当错误配置的防火墙丢弃所有 ICMP 流量时,就会出现这里的问题。当你连接到(比如)一个网络服务器时,你建立一个 TCP 会话并发送你愿意接受基于你的 1500 字节 MTU 的 TCP 数据包(因为你通过以太网连接到你的路由器。)如果外部网络服务器想要向您发送大量数据,他们会将其拆分为多个块(与 TCP 和 IP 标头结合时)输出为 1500 字节的有效负载并将它们发送给您。您的 ISP 会收到其中之一,然后尝试将其包装成 PPPoE 数据包以发送到您的 ADSL 调制解调器,但它会太大而无法发送。所以它会回复一个 ICMP 不可达,这会(在一个完美的世界中)导致远程计算机缩小其 MSS 以进行连接并重新传输。然而,如果有一道坏掉的防火墙,

Ultimately setting your MTU on your ethernet device is desirable to send the right size frames to your ADSL modem (to avoid it asking you to retransmit with a smaller frame), but it's critical to influence the MSS size you send to remote hosts when building TCP connections.

最终在以太网设备上设置 MTU 是为了将正确大小的帧发送到 ADSL 调制解调器(以避免它要求您使用较小的帧重新传输),但在构建 TCP 时影响发送到远程主机的 MSS 大小至关重要连接。

回答by Spike Gronim

It's the Maximum Transmission Unit, so it definitely sets the outgoing maximum packet size. I'm not sure if will reject incoming packets larger than the MTU.

它是最大传输单元,因此它肯定会设置传出的最大数据包大小。我不确定是否会拒绝大于 MTU 的传入数据包。

回答by Mike Pennington

ifconfig ... mtu <value>sets the MTU for layer2 payloads sent out the interface, and will reject larger layer2 payloads received on this interface. You mustensure your MTU matches on both sides of an ethernet link; you should nothave mismatched mtu values anywhere in the same ethernet broadcast domain. Note that the ethernet headers are not included in the MTU you are setting.

ifconfig ... mtu <value>为从接口发出的第 2 层负载设置 MTU,并将拒绝在此接口上接收到的更大的第 2 层负载。您必须确保您的 MTU 在以太网链路的两端匹配;你不应该在同任何地方不匹配的MTU值以太网广播域。请注意,您正在设置的 MTU 中不包含以太网标头。

Also, ifconfighas not been maintained in linux for agesand is old and deprecated; sadly linux distributions still include it because they're afraid of breaking old scripts. This has the very negative effect of encouraging people to continue using it. You should be using the iproute2family of commands:

此外,ifconfig在 linux 中已经很久没有维护了,并且已经过时且已弃用;遗憾的是 linux 发行版仍然包含它,因为他们害怕破坏旧脚本。这具有鼓励人们继续使用它的非常负面的影响。您应该使用iproute2命令系列:

[mpenning@hotcoffee ~]$ sudo ip link set mtu 1492 eth0
[mpenning@hotcoffee ~]$ ip link show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1492 qdisc mq state UP qlen 1000
    link/ether 00:1e:c9:cd:46:c8 brd ff:ff:ff:ff:ff:ff
[mpenning@hotcoffee ~]$ 

回答by Oren N

Large incoming packets may be dropped based on the interface MTU size.

可能会根据接口 MTU 大小丢弃大型传入数据包。

For example, the default MTU 1500 on Linux 2.6 CentOS (tested with Ethernet controller: Intel Corporation 80003ES2LAN Gigabit Ethernet Controller (Copper) (rev 01)) drops Jumbo packets >1504. Errors appear in ifconfig and there are rx_long_length_errors indications for this in ethtool -S output. Increasing MTU indicates Jumbo packets should be supported. The threshold for when to drop packets based on their size being too large appears to depend on MTU (-4096, -8192, etc.)

例如,Linux 2.6 CentOS(使用以太网控制器测试:Intel Corporation 80003ES2LAN Gigabit Ethernet Controller (Copper) (rev 01))上的默认 MTU 1500 会丢弃 >1504 的巨型数据包。错误出现在 ifconfig 中,并且在 ethtool -S 输出中有 rx_long_length_errors 指示。增加 MTU 表示应支持 Jumbo 数据包。何时根据数据包过大丢弃数据包的阈值似乎取决于 MTU(-4096、-8192 等)

Oren

奥伦

回答by HonanLi

There is no doubt that MTU configured by ifconfig impacts Tx ip fragmentation, I have no more comments.

毫无疑问,ifconfig 配置的 MTU 会影响 Tx ip 分片,我没有更多评论。

But for Rx direction, I find whether the parameter impacts incoming IP packets, it depends. Different manufacturer behaves differently. I tested all the devices on hand and found 3 cases below.

但是对于 Rx 方向,我发现该参数是否影响传入的 IP 数据包,这取决于。不同的制造商表现不同。我测试了手头的所有设备,发现以下 3 种情况。

Test case:

测试用例:

Device0 eth0 (192.168.225.1, mtu 2000)<--ETH cable-->Device1 eth0 (192.168.225.34, mtu MTU_SIZE)

Device0 eth0 (192.168.225.1, mtu 2000)<--ETH 网线-->Device1 eth0 (192.168.225.34, mtu MTU_SIZE)

On Device0 ping 192.168.225.34 -s ICMP_SIZE, Checking how MTU_SIZE impacts Rx of Device1.

在 Device0 ping 192.168.225.34 -s ICMP_SIZE,检查 MTU_SIZE 如何影响 Device1 的 Rx。

case 1:

案例1

Device1 = Linux 4.4.0 with Intel I218-LM:

设备 1 = 带有 Intel I218-LM 的 Linux 4.4.0:

When MTU_SIZE=1500, ping succeeds at ICMP_SIZE=1476, fails at ICMP_SIZE=1477 and above. It seems that there is a PRACTICAL MTU=1504 (20B(IP header)+8B(ICMP header)+1476B(ICMP data)).

当 MTU_SIZE=1500 时,在 ICMP_SIZE=1476 时 ping 成功,在 ICMP_SIZE=1477 及以上时失败。似乎有一个PRACTICAL MTU=1504(20B(IP头)+8B(ICMP头)+1476B(ICMP数据))。

When MTU_SIZE=1490, ping succeeds at ICMP_SIZE=1476, fails at ICMP_SIZE=1477 and above, behave the same as MTU_SIZE=1500.

当 MTU_SIZE=1490 时,在 ICMP_SIZE=1476 时 ping 成功,在 ICMP_SIZE=1477 及以上时失败,行为与 MTU_SIZE=1500 相同。

When MTU_SIZE=1501, ping succeeds at ICMP_SIZE=1476, 1478, 1600, 1900. It seems that jumbo frame is switched on once MTU_SIZE is set >1500 and there is no 1504 restriction any more.

当 MTU_SIZE=1501 时,在 ICMP_SIZE=1476、1478、1600、1900 时 ping 成功。似乎一旦 MTU_SIZE 设置为 >1500 就打开了巨型帧,并且不再有 1504 限制。

case 2:

案例2

Device1 = Linux 3.18.31 with Qualcomm Atheros AR8151 v2.0 Gigabit Ethernet:

Device1 = Linux 3.18.31 with Qualcomm Atheros AR8151 v2.0 千兆以太网:

When MTU_SIZE=1500, ping succeeds at ICMP_SIZE=1476, fails at ICMP_SIZE=1477 and above.

当 MTU_SIZE=1500 时,在 ICMP_SIZE=1476 时 ping 成功,在 ICMP_SIZE=1477 及以上时失败。

When MTU_SIZE=1490, ping succeeds at ICMP_SIZE=1466, fails at ICMP_SIZE=1467 and above.

当 MTU_SIZE=1490 时,在 ICMP_SIZE=1466 时 ping 成功,在 ICMP_SIZE=1467 及以上时失败。

When MTU_SIZE=1501, ping succeeds at ICMP_SIZE=1477, fails at ICMP_SIZE=1478 and above.

当 MTU_SIZE=1501 时,在 ICMP_SIZE=1477 时 ping 成功,在 ICMP_SIZE=1478 及以上时失败。

When MTU_SIZE=500, ping succeeds at ICMP_SIZE=476, fails at ICMP_SIZE=477 and above.

当 MTU_SIZE=500 时,在 ICMP_SIZE=476 时 ping 成功,在 ICMP_SIZE=477 及以上时失败。

When MTU_SIZE=1900, ping succeeds at ICMP_SIZE=1876, fails at ICMP_SIZE=1877 and above.

当 MTU_SIZE=1900 时,在 ICMP_SIZE=1876 时 ping 成功,在 ICMP_SIZE=1877 及以上时失败。

This case behaves exactly as Edward Thomson said, except that in my test the PRACTICAL MTU=MTU_SIZE+4.

这种情况与 Edward Thomson 所说的完全一样,除了在我的测试中 PRACTICAL MTU=MTU_SIZE+4。

case 3:

案例3

Device1 = Linux 4.4.50 with Raspberry Pi 2 Module B ETH:

设备 1 = Linux 4.4.50 与 Raspberry Pi 2 模块 B ETH:

When MTU_SIZE=1500, ping succeeds at ICMP_SIZE=1472, fails at ICMP_SIZE=1473 and above. So there is a PRACTICAL MTU=1500 (20B(IP header)+8B(ICMP header)+1472B(ICMP data)) working there.

当 MTU_SIZE=1500 时,在 ICMP_SIZE=1472 时 ping 成功,在 ICMP_SIZE=1473 及以上时失败。所以有一个实用的 MTU=1500(20B(IP 标头)+8B(ICMP 标头)+1472B(ICMP 数据))在那里工作。

When MTU_SIZE=1490, behave the same as MTU_SIZE=1500.

当 MTU_SIZE=1490 时,行为与 MTU_SIZE=1500 相同。

When MTU_SIZE=1501, behave the same as MTU_SIZE=1500.

当 MTU_SIZE=1501 时,行为与 MTU_SIZE=1500 相同。

When MTU_SIZE=2000, behave the same as MTU_SIZE=1500.

当 MTU_SIZE=2000 时,行为与 MTU_SIZE=1500 相同。

When MTU_SIZE=500, behave the same as MTU_SIZE=1500.

当 MTU_SIZE=500 时,行为与 MTU_SIZE=1500 相同。

This case behaves exactly as Ron Maupin said in Why MTU configuration doesn't take effect on receiving direction?.

这种情况与 Ron Maupin 在为什么 MTU 配置对接收方向不起作用中所说的完全一样.

To sum it all, in real world, after you set ifconfig mtu,

总而言之,在现实世界中,在设置 ifconfig mtu 之后,

sometimes the Rx IP packts get dropped when exceed 1504 , no matter what MTU value you set (except that the jumbo frame is enabled).

有时 Rx IP 数据包在超过 1504 时会被丢弃,无论您设置什么 MTU 值(除了启用巨型帧)。

sometimes the Rx IP packts get dropped when exceed the MTU+4 you set on receiving device.

有时,当超过您在接收设备上设置的 MTU+4 时,Rx IP 数据包会被丢弃。

sometimes the Rx IP packts get dropped when exceed 1500, no matter what MTU value you set.

有时 Rx IP 数据包在超过 1500 时会被丢弃,无论您设置什么 MTU 值。

... ...

……