Documentation
¶
Index ¶
- Constants
- Variables
- func CleanNetworkPid(name string)
- func IsNetworkDaemonAlive(name string) bool
- func LookupIPByMAC(mac string) string
- func LookupIPForMode(mode, mac string) string
- func NetworkDir(name string) (string, error)
- func NetworkLogPath(name string) (string, error)
- func NetworkServiceName(name string) string
- func NetworksDir() (string, error)
- func ReadNetworkPid(name string) (int, error)
- func RunNetworkDaemon(name, mode string, verbose bool) error
- func StartNetworkDaemon(name, mode string, verbose bool) error
- func StopNetworkDaemon(name string) (bool, error)
- func VmenetForMAC(mac string) string
- func WriteNetworkConfig(name string, cfg NetworkConfig) error
- func WriteNetworkPid(name string, pid int) error
- type NetStats
- type NetworkConfig
- type UserNetwork
Constants ¶
const ( NetworkNAT = "nat" NetworkHost = "host" NetworkIsolated = "isolated" NetworkNone = "none" NetworkUser = "user" // user-mode networking in the VM daemon (gvisor-tap-vsock) )
Network mode constants.
const ( UserSubnet = "192.168.127.0/24" UserGatewayIP = "192.168.127.1" // DHCP and DNS server UserGuestIP = "192.168.127.2" // static lease for the VM's MAC UserHostIP = "192.168.127.254" // NATed to the Mac's 127.0.0.1 // UserHostName and UserGatewayName resolve to UserHostIP through the // gateway's DNS server, in the guest and in its containers and pods, as // with Docker Desktop. UserHostName = "host.docker.internal" UserGatewayName = "gateway.docker.internal" )
User-mode network addressing. Every VM in user mode gets its own private copy of this network, so the addresses are the same for all of them.
Variables ¶
var ValidNetworkModes = []string{NetworkNAT, NetworkShared, NetworkHost, NetworkIsolated, NetworkNone, NetworkUser}
ValidNetworkModes is the set of accepted --network-mode values.
Functions ¶
func IsNetworkDaemonAlive ¶
IsNetworkDaemonAlive checks if the daemon for the given network is running.
func LookupIPByMAC ¶
lookupIPByMAC reads the macOS DHCP server leases file to find the IP address assigned to the given MAC address. This is more reliable than the ARP table, which only contains entries for recent IP traffic.
The leases file at /var/db/dhcpd_leases has entries like:
{
ip_address=192.168.64.14
hw_address=1,ce:b7:d1:c3:89:1d
identifier=1,ce:b7:d1:c3:89:1d
lease=0x69db5aec
}
The hw_address is prefixed with "1," (ethernet type) and uses shortened octets (e.g. "e:3d:be" instead of "0e:3d:be"). We normalize both sides for comparison. LookupIPByMAC reads the macOS DHCP server leases file to find the IP address assigned to the given MAC address.
func LookupIPForMode ¶
LookupIPForMode picks the right IP discovery strategy based on network mode. - nat/shared: DHCP leases file (macOS DHCP server assigns IPs) - host: DHCP leases first (router assigns), ARP table fallback - isolated/none: no IP expected
func NetworkDir ¶
NetworkDir returns the path to the named network's directory.
func NetworkLogPath ¶
NetworkLogPath returns the path to the network daemon's log file.
func NetworkServiceName ¶
NetworkServiceName returns the launchd label and Mach service name used by the daemon for the named network.
func NetworksDir ¶
NetworksDir returns the parent directory for all named networks.
func ReadNetworkPid ¶
ReadNetworkPid reads the daemon PID for a named network. Returns 0 if the PID file doesn't exist.
func RunNetworkDaemon ¶
RunNetworkDaemon is the main loop for the network daemon process. It is started by launchd (see StartNetworkDaemon), creates the vmnet network, publishes it on the network's Mach service with vmnet.ShareNetwork, and keeps it alive until it receives SIGTERM or SIGINT. VM daemons join the network with joinNamedNetwork.
func StartNetworkDaemon ¶
StartNetworkDaemon loads the launchd job for the named network, which starts `monolith __netd <name> <mode>` from the current executable. Any stale job with the same label is unloaded first. It returns once launchd accepts the job; callers should wait for IsNetworkDaemonAlive.
func StopNetworkDaemon ¶
StopNetworkDaemon unloads the launchd job for the named network, which sends the daemon SIGTERM, and waits for it to exit. It reports whether a running daemon was stopped.
func VmenetForMAC ¶
VmenetForMAC finds the vmenet<N> interface associated with the given guest MAC address by parsing the bridge100 address cache from `ifconfig bridge100`.
The address cache section looks like:
Address cache:
ce:b7:d1:c3:89:1d Vlan1 vmenet0 1116 flags=0<>
Returns "" if no matching interface is found.
func WriteNetworkConfig ¶
func WriteNetworkConfig(name string, cfg NetworkConfig) error
WriteNetworkConfig writes a named network's configuration.
func WriteNetworkPid ¶
WriteNetworkPid writes the daemon PID file.
Types ¶
type NetStats ¶
NetStats holds per-VM network counters from the host's vmenet interface. RX/TX are from the guest's perspective (RX = bytes received by guest).
func NetworkStats ¶
NetworkStats returns per-VM network counters by querying the given vmenet interface via `netstat -I <iface> -b`.
Note: from the host's perspective, Ibytes = bytes received from guest (guest TX) and Obytes = bytes sent to guest (guest RX). We swap them so the returned values are from the guest's perspective.
netstat output:
Name Mtu Network Address Ipkts Ierrs Ibytes Opkts Oerrs Obytes Coll vmenet0 1500 <Link#27> ba:23:... 9 0 1286 156 0 24693 0
type NetworkConfig ¶
type NetworkConfig struct {
Mode string `json:"mode"` // "shared" or "isolated"
Subnet string `json:"subnet"` // actual IPv4 subnet allocated by vmnet (e.g. "192.168.64.0/24"), filled after creation
UserSubnet string `json:"user_subnet,omitempty"` // caller-requested IPv4 subnet (CIDR); empty means vmnet picks
}
NetworkConfig holds the persisted configuration for a named network.
func ReadNetworkConfig ¶
func ReadNetworkConfig(name string) (NetworkConfig, error)
ReadNetworkConfig reads a named network's configuration.
type UserNetwork ¶
type UserNetwork struct {
// contains filtered or unexported fields
}
UserNetwork is a user-mode virtual network for one VM, implemented by gvisor-tap-vsock inside the VM daemon. The guest's NIC is attached to one end of a datagram socket pair carrying raw Ethernet frames; a userspace TCP/IP stack on the other end terminates the guest's connections and re-opens them from the daemon process. Outbound traffic therefore looks like any other app's traffic to macOS, VPNs and endpoint filters, which is what lets it work where vmnet's NAT forwarding is blocked. The guest is not reachable from the Mac by IP; DialGuest connects to it instead.
func ConfigureNetwork ¶
func ConfigureNetwork(vmConfig *vz.VirtualMachineConfiguration, mode, networkName, hostInterface, macAddr string, userHostAccess bool) (string, *UserNetwork, error)
ConfigureNetwork sets up the VM's network device based on the config. macAddr is the VM's persisted MAC; when empty a random locally administered one is generated. A stable MAC is required to restore a saved VM state and keeps the VM's DHCP address stable. It returns the MAC address string (even for "none" mode, for state tracking) and attaches the network device to the VM configuration when applicable. In "user" mode it also returns the running UserNetwork, which the caller must Close when the VM stops; userHostAccess is passed to NewUserNetwork.
func NewUserNetwork ¶
func NewUserNetwork(mac string, hostAccess bool) (*UserNetwork, error)
NewUserNetwork creates the virtual network and starts serving it. mac is the VM's MAC address, which gets the static DHCP lease UserGuestIP. hostAccess maps UserHostIP (and host.docker.internal) to the Mac's 127.0.0.1; sandboxes leave it off so guests can't reach services that listen only on the Mac's loopback.
func (*UserNetwork) DialGuest ¶
DialGuest opens a TCP connection to port on the guest through the virtual network, as if from the gateway.
func (*UserNetwork) ExposeUDP ¶
func (u *UserNetwork) ExposeUDP(hostPort, guestPort int) error
ExposeUDP forwards UDP on 127.0.0.1:hostPort on the Mac to guestPort on the guest, using gvisor-tap-vsock's port forwarder (it tracks a session per client address). TCP goes through DialGuest instead.
func (*UserNetwork) UnexposeUDP ¶
func (u *UserNetwork) UnexposeUDP(hostPort int) error
UnexposeUDP stops a forward started by ExposeUDP.