Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Sources/Containerization/Agent/Vminitd.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,11 @@ extension Vminitd: VirtualMachineAgent {
_ = try await client.deleteProcess(request)
}

public func up(name: String) async throws {
public func up(name: String, mtu: UInt32? = nil) async throws {
Comment thread
realrajaryan marked this conversation as resolved.
let request = Com_Apple_Containerization_Sandbox_V3_IpLinkSetRequest.with {
$0.interface = name
$0.up = true
if let mtu { $0.mtu = mtu }
}
_ = try await client.ipLinkSet(request)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Containerization/LinuxContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ extension LinuxContainer {
for (index, i) in self.interfaces.enumerated() {
let name = "eth\(index)"
try await agent.addressAdd(name: name, address: i.address)
try await agent.up(name: name)
try await agent.up(name: name, mtu: 1280)
if let gateway = i.gateway {
try await agent.routeAddDefault(name: name, gateway: gateway)
}
Expand Down
21 changes: 21 additions & 0 deletions Sources/Containerization/SandboxContext/SandboxContext.pb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,20 @@ public struct Com_Apple_Containerization_Sandbox_V3_IpLinkSetRequest: Sendable {

public var up: Bool = false

public var mtu: UInt32 {
get {return _mtu ?? 0}
set {_mtu = newValue}
}
/// Returns true if `mtu` has been explicitly set.
public var hasMtu: Bool {return self._mtu != nil}
/// Clears the value of `mtu`. Subsequent reads from it will return its default value.
public mutating func clearMtu() {self._mtu = nil}

public var unknownFields = SwiftProtobuf.UnknownStorage()

public init() {}

fileprivate var _mtu: UInt32? = nil
}

public struct Com_Apple_Containerization_Sandbox_V3_IpLinkSetResponse: Sendable {
Expand Down Expand Up @@ -1992,6 +2003,7 @@ extension Com_Apple_Containerization_Sandbox_V3_IpLinkSetRequest: SwiftProtobuf.
public static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
1: .same(proto: "interface"),
2: .same(proto: "up"),
3: .same(proto: "mtu"),
]

public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
Expand All @@ -2002,24 +2014,33 @@ extension Com_Apple_Containerization_Sandbox_V3_IpLinkSetRequest: SwiftProtobuf.
switch fieldNumber {
case 1: try { try decoder.decodeSingularStringField(value: &self.interface) }()
case 2: try { try decoder.decodeSingularBoolField(value: &self.up) }()
case 3: try { try decoder.decodeSingularUInt32Field(value: &self._mtu) }()
default: break
}
}
}

public func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
// The use of inline closures is to circumvent an issue where the compiler
// allocates stack space for every if/case branch local when no optimizations
// are enabled. https://github.com/apple/swift-protobuf/issues/1034 and
// https://github.com/apple/swift-protobuf/issues/1182
if !self.interface.isEmpty {
try visitor.visitSingularStringField(value: self.interface, fieldNumber: 1)
}
if self.up != false {
try visitor.visitSingularBoolField(value: self.up, fieldNumber: 2)
}
try { if let v = self._mtu {
try visitor.visitSingularUInt32Field(value: v, fieldNumber: 3)
} }()
try unknownFields.traverse(visitor: &visitor)
}

public static func ==(lhs: Com_Apple_Containerization_Sandbox_V3_IpLinkSetRequest, rhs: Com_Apple_Containerization_Sandbox_V3_IpLinkSetRequest) -> Bool {
if lhs.interface != rhs.interface {return false}
if lhs.up != rhs.up {return false}
if lhs._mtu != rhs._mtu {return false}
if lhs.unknownFields != rhs.unknownFields {return false}
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ message MkdirResponse {}
message IpLinkSetRequest {
string interface = 1;
bool up = 2;
optional uint32 mtu = 3;
}

message IpLinkSetResponse {}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Containerization/VirtualMachineAgent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public protocol VirtualMachineAgent: Sendable {
func deleteProcess(id: String, containerID: String?) async throws

// Networking
func up(name: String) async throws
func up(name: String, mtu: UInt32?) async throws
func down(name: String) async throws
func addressAdd(name: String, address: String) async throws
func routeAddDefault(name: String, gateway: String) async throws
Expand Down
31 changes: 18 additions & 13 deletions Sources/ContainerizationNetlink/NetlinkSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,17 @@ public struct NetlinkSession {
/// - Parameters:
/// - interface: The name of the interface.
/// - up: The value to set the interface state to.
public func linkSet(interface: String, up: Bool) throws {
public func linkSet(interface: String, up: Bool, mtu: UInt32? = nil) throws {
// ip link set dev [interface] [up|down]
let interfaceIndex = try getInterfaceIndex(interface)
let mtuAttr = RTAttribute(
len: UInt16(RTAttribute.size + MemoryLayout<UInt32>.size), type: LinkAttributeType.IFLA_MTU)
let requestSize = NetlinkMessageHeader.size + InterfaceInfo.size + mtuAttr.paddedLen
// build the attribute only when mtu is supplied
let attr: RTAttribute? =
(mtu != nil)
? RTAttribute(
len: UInt16(RTAttribute.size + MemoryLayout<UInt32>.size),
type: LinkAttributeType.IFLA_MTU)
: nil
let requestSize = NetlinkMessageHeader.size + InterfaceInfo.size + (attr?.paddedLen ?? 0)
var requestBuffer = [UInt8](repeating: 0, count: requestSize)
var requestOffset = 0

Expand All @@ -91,16 +96,16 @@ 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
if let attr = attr, let m = mtu {
requestOffset = try attr.appendBuffer(&requestBuffer, offset: requestOffset)
guard
let newRequestOffset =
requestBuffer.copyIn(as: UInt32.self, value: m, offset: requestOffset)
else {
throw NetlinkDataError.sendMarshalFailure
}
requestOffset = newRequestOffset
}
requestOffset = newRequestOffset

guard requestOffset == requestSize else {
throw Error.unexpectedOffset(offset: requestOffset, size: requestSize)
Expand Down
Loading