From 191c7244e75cbaa9d2d2fe6a6427050090a98c90 Mon Sep 17 00:00:00 2001 From: John Logan Date: Sun, 8 Jun 2025 16:09:16 -0700 Subject: [PATCH] Set MTU to 1280 for containers. - Needed for alpine containers in some instances. - This should be configurable. Hardcoding in linkSet for now. --- .../ContainerizationNetlink/NetlinkSession.swift | 16 +++++++++++++++- Sources/ContainerizationNetlink/Types.swift | 1 + .../NetlinkSessionTest.swift | 4 ++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Sources/ContainerizationNetlink/NetlinkSession.swift b/Sources/ContainerizationNetlink/NetlinkSession.swift index 383df15b..28f28d57 100644 --- a/Sources/ContainerizationNetlink/NetlinkSession.swift +++ b/Sources/ContainerizationNetlink/NetlinkSession.swift @@ -23,6 +23,7 @@ import Logging /// core high-level type offered to perform actions to the netlink surface in the kernel. public struct NetlinkSession { private static let receiveDataLength = 65536 + private static let mtu: UInt32 = 1280 private let socket: any NetlinkSocket private let log: Logger @@ -70,7 +71,9 @@ public struct NetlinkSession { public func linkSet(interface: String, up: Bool) throws { // ip link set dev [interface] [up|down] let interfaceIndex = try getInterfaceIndex(interface) - let requestSize = NetlinkMessageHeader.size + InterfaceInfo.size + let mtuAttr = RTAttribute( + len: UInt16(RTAttribute.size + MemoryLayout.size), type: LinkAttributeType.IFLA_MTU) + let requestSize = NetlinkMessageHeader.size + InterfaceInfo.size + mtuAttr.paddedLen var requestBuffer = [UInt8](repeating: 0, count: requestSize) var requestOffset = 0 @@ -89,6 +92,17 @@ public struct NetlinkSession { change: InterfaceFlags.DEFAULT_CHANGE) requestOffset = try requestInfo.appendBuffer(&requestBuffer, offset: requestOffset) + requestOffset = try mtuAttr.appendBuffer(&requestBuffer, offset: requestOffset) + guard + let newRequestOffset = requestBuffer.copyIn( + as: UInt32.self, + value: Self.mtu, + offset: requestOffset) + else { + throw NetlinkDataError.sendMarshalFailure + } + requestOffset = newRequestOffset + guard requestOffset == requestSize else { throw Error.unexpectedOffset(offset: requestOffset, size: requestSize) } diff --git a/Sources/ContainerizationNetlink/Types.swift b/Sources/ContainerizationNetlink/Types.swift index d119f7f1..e66215f0 100644 --- a/Sources/ContainerizationNetlink/Types.swift +++ b/Sources/ContainerizationNetlink/Types.swift @@ -78,6 +78,7 @@ struct InterfaceFlags { struct LinkAttributeType { static let IFLA_EXT_IFNAME: UInt16 = 3 + static let IFLA_MTU: UInt16 = 4 static let IFLA_EXT_MASK: UInt16 = 29 } diff --git a/Tests/ContainerizationNetlinkTests/NetlinkSessionTest.swift b/Tests/ContainerizationNetlinkTests/NetlinkSessionTest.swift index a1efdbd3..e9bf3f17 100644 --- a/Tests/ContainerizationNetlinkTests/NetlinkSessionTest.swift +++ b/Tests/ContainerizationNetlinkTests/NetlinkSessionTest.swift @@ -36,7 +36,7 @@ struct NetlinkSessionTest { ]) // Network down for interface. - let expectedDownRequest = "2000000010000500000000000cc00cc0110000000200000000000000ffffffff" + let expectedDownRequest = "2800000010000500000000000cc00cc0110000000200000000000000ffffffff0800040000050000" mockSocket.responses.append([ 0x24, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x0c, 0xc0, 0x0c, 0xc0, @@ -71,7 +71,7 @@ struct NetlinkSessionTest { ]) // Network up for interface. - let expectedUpRequest = "200000001000050000000000c00cc00c110000000200000001000000ffffffff" + let expectedUpRequest = "280000001000050000000000c00cc00c110000000200000001000000ffffffff0800040000050000" mockSocket.responses.append([ 0x24, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x0c, 0xc0, 0x0c,