Skip to content

Commit d0ce69c

Browse files
committed
Add FileHandleNetworkInterface
FileHandleNetworkInterface uses VZFileHandleNetworkDeviceAttachment to attach the container to a file handle where another service can provide an arbitrary network. This might be an entirely simulated network, a virtual network like a VPN, e.g. using Wireguard, but it could be also a bridged physical network from the host where the service will then take over the responsibility of bridging the traffic between the container and host. We do not make any assumption about IP addresses for now, but instead also assume that the IP address can get assigned via DHCP.
1 parent 17b9885 commit d0ce69c

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

Sources/Containerization/BridgedNetworkInterface.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public final class BridgedNetworkInterface: Interface, Sendable {
2828
public let macAddress: MACAddress?
2929
public let ipv4Address: CIDRv4? = nil
3030
public let ipv4Gateway: IPv4Address? = nil
31-
public let mtu: UInt32 = 1500
3231

3332
public init(hostInterfaceName: String, macAddress: MACAddress? = nil) {
3433
self.hostInterfaceName = hostInterfaceName
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===----------------------------------------------------------------------===//
2+
// Copyright © 2026 Apple Inc. and the Containerization project authors.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// https://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//===----------------------------------------------------------------------===//
16+
17+
#if os(macOS)
18+
19+
import ContainerizationError
20+
import ContainerizationExtras
21+
import Virtualization
22+
23+
/// A network interface that connects the container to an arbitrary FileHandle-backed
24+
/// network service. The IP address might be assigned by the upstream DHCP server or
25+
/// configured inside the container; `ipv4Address` is always nil.
26+
@available(macOS 26, *)
27+
public final class FileHandleNetworkInterface: Interface, Sendable {
28+
public let macAddress: MACAddress?
29+
public let ipv4Address: CIDRv4? = nil
30+
public let ipv4Gateway: IPv4Address? = nil
31+
public let fileHandle: FileHandle
32+
33+
public init(fileHandle: FileHandle, macAddress: MACAddress? = nil) {
34+
self.macAddress = macAddress
35+
self.fileHandle = fileHandle
36+
}
37+
}
38+
39+
@available(macOS 26, *)
40+
extension FileHandleNetworkInterface: VZInterface {
41+
public func device() throws -> VZVirtioNetworkDeviceConfiguration {
42+
let config = VZVirtioNetworkDeviceConfiguration()
43+
config.attachment = VZFileHandleNetworkDeviceAttachment(fileHandle: fileHandle)
44+
if let mac = macAddress, let vzMac = VZMACAddress(string: mac.description) {
45+
config.macAddress = vzMac
46+
}
47+
return config
48+
}
49+
}
50+
51+
#endif

0 commit comments

Comments
 (0)