Set MTU to 1280 for containers. (#38)

- Needed for alpine containers in some instances.
- This should be configurable. Hardcoding in linkSet for now.
This commit is contained in:
J Logan
2025-06-08 21:02:23 -07:00
committed by GitHub
parent 3fd2c220a2
commit e285dc0dd4
3 changed files with 18 additions and 3 deletions
@@ -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<UInt32>.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)
}
@@ -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
}
@@ -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,