Skip to content

Commit 66defd3

Browse files
milantracygvisor-bot
authored andcommitted
Consider ethernet header size when returning MTU.
PiperOrigin-RevId: 834925762
1 parent 486eeaa commit 66defd3

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

pkg/tcpip/link/ethernet/ethernet.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ func (e *Endpoint) LinkAddress() tcpip.LinkAddress {
5454

5555
// MTU implements stack.LinkEndpoint.
5656
func (e *Endpoint) MTU() uint32 {
57-
return e.Endpoint.MTU()
57+
// It prevents upper-layers from sending larger than expected packets.
58+
if mtu := e.Endpoint.MTU(); mtu > header.EthernetMinimumSize {
59+
return mtu - header.EthernetMinimumSize
60+
}
61+
return 0
5862
}
5963

6064
// DeliverNetworkPacket implements stack.NetworkDispatcher.

pkg/tcpip/link/ethernet/ethernet_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,21 @@ func TestMTU(t *testing.T) {
143143
maxFrameSize: 0,
144144
expectedMTU: 0,
145145
},
146+
{
147+
maxFrameSize: header.EthernetMinimumSize - 1,
148+
expectedMTU: 0,
149+
},
146150
{
147151
maxFrameSize: header.EthernetMinimumSize,
148-
expectedMTU: header.EthernetMinimumSize,
152+
expectedMTU: 0,
153+
},
154+
{
155+
maxFrameSize: header.EthernetMinimumSize + 1,
156+
expectedMTU: 1,
149157
},
150158
{
151159
maxFrameSize: maxFrameSize,
152-
expectedMTU: maxFrameSize,
160+
expectedMTU: maxFrameSize - header.EthernetMinimumSize,
153161
},
154162
}
155163

test/rtnetlink/linux/setlink_test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ source "$(dirname "$0")/rtnetlink_test.sh"
2020
# Create a new veth pair in the current namespace and change the MTU.
2121
ip link add name test_veth01 type veth peer name test_veth02
2222
ip link set test_veth01 mtu 3000
23-
ip link show test_veth01 | grep -E "mtu 3000"
23+
ip link show test_veth01 | grep -E "mtu (2986|3000)"
2424
ip link del name test_veth01
2525
# Check that test_veth02 has been destroyed.
2626
if ! wait_for ! ip link show test_veth02; then

test/syscalls/linux/socket_netlink_route.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <fcntl.h>
1717
#include <ifaddrs.h>
1818
#include <linux/fib_rules.h>
19+
#include <linux/if_ether.h>
1920
#include <linux/netlink.h>
2021
#include <linux/rtnetlink.h>
2122
#include <linux/veth.h>
@@ -320,7 +321,7 @@ TEST_P(NetlinkSetLinkTest, ChangeMTU) {
320321
EXPECT_NO_ERRNO(NetlinkRequestAckOrError(fd, kSeq, &req, sizeof(req)));
321322

322323
// Update the local loopback_link's MTU to the requested value.
323-
loopback_link.mtu = req.mtu;
324+
loopback_link.mtu = req.mtu - ETH_HLEN;
324325
// Verify the new MTU.
325326
struct searchrequest {
326327
struct nlmsghdr hdr;

0 commit comments

Comments
 (0)