Documentation
¶
Overview ¶
Package netplane connects the emulator process to the container networks it creates (Section 3.3). Every container the emulator runs is attached to an internal "services" network on which the emulator's own endpoints (gateway, metadata, DNS, registry, ...) are reachable at the network's gateway address through forwarders, and optionally to an "external" network that provides internet egress (public IPs). On a VM runtime the VM relay (ADR 0005) listens for those endpoints instead, and carries the emulator's dials to container addresses (DialContext).
Index ¶
- Variables
- func Contains(cidr, ip string) bool
- func HostPort(addr string) (string, int)
- func ListenUDP(ctx context.Context, host, port string) (net.PacketConn, error)
- func Splice(a, b net.Conn)
- type Exposed
- type Net
- type Plane
- func (p *Plane) Addr(ctx context.Context, name string) (string, error)
- func (p *Plane) Close(ctx context.Context)
- func (p *Plane) DialContext(ctx context.Context, network, addr string) (net.Conn, error)
- func (p *Plane) Expose(name, addr, target string, fallback bool) (*Exposed, error)
- func (p *Plane) ExposeConn(addr string, fallback bool, ...) (*Exposed, error)
- func (p *Plane) External(ctx context.Context) (Net, error)
- func (p *Plane) HostAddress(ctx context.Context, n Net) (string, error)
- func (p *Plane) Offer(ctx context.Context, hostAddr string, port int, udp bool) error
- func (p *Plane) Relay() *vmrelay.Relay
- func (p *Plane) RelayDecision() (vmrelay.Decision, bool)
- func (p *Plane) Reset(ctx context.Context) error
- func (p *Plane) Runtime() *runtime.Manager
- func (p *Plane) Serve(l net.Listener, targets func() []string) *Exposed
- func (p *Plane) ServeDial(l net.Listener, dial func(ctx context.Context) (net.Conn, error)) *Exposed
- func (p *Plane) Services(ctx context.Context) (Net, error)
- func (p *Plane) Withdraw(ctx context.Context, hostAddr string, port int, udp bool) error
Constants ¶
This section is empty.
Variables ¶
var ErrInContainer = errors.New("the Host attachment needs the emulator on the host; it runs in a container")
ErrInContainer is returned when the emulator runs in a container (ADR 0003): its loopback is not the host's, so it has no Host attachment.
Functions ¶
func ListenUDP ¶ added in v0.4.0
ListenUDP binds a UDP socket for DNS: the emulator's server and its forwarder on the services network. Port 5353 is also the mDNS port: avahi-daemon, systemd-resolved and browsers hold UDP 0.0.0.0:5353 with SO_REUSEADDR on most Linux desktops, as mDNSResponder and browsers do on macOS, so a plain bind of 127.0.0.1:5353 (or of the services gateway) fails with "address already in use". Darwin behaves the same as Linux here (checked on macOS 26.6; TestStartBesideMDNS runs on both). On Windows, Chrome and the DNS Client service hold the port the same way and the plain bind fails with WSAEACCES instead. Setting SO_REUSEADDR on our socket lets it coexist; unicast queries to the specific bind address reach our socket while multicast mDNS traffic keeps going to the wildcard sockets. On a wildcard bind we would share (and steal) mDNS traffic, so reuse is only enabled for specific addresses. A wildcard bind is dual-stack ([::]); on macOS, which keeps the IPv4 and IPv6 port spaces apart, it still fails because mDNS daemons hold the port on IPv6 too (mDNSResponder and browsers do). Beside an IPv4-only holder it would bind but receive only IPv6 traffic (issue #262). The TCP listener on the same port is bound first without reuse, so a second emulator instance still fails cleanly.
Types ¶
type Exposed ¶ added in v0.6.0
type Exposed struct {
// contains filtered or unexported fields
}
Exposed is a host port that Expose serves.
type Net ¶
type Net struct {
// Name is the runtime network name.
Name string
// Subnet is the IPv4 CIDR.
Subnet string
// Gateway is the host-side address on the network.
Gateway string
// Bridge is the host interface holding Gateway, as the runtime
// reports it ("" if it doesn't).
Bridge string
}
Net describes a container network.
func (Net) PickAddr ¶ added in v0.12.0
PickAddr chooses a fixed address for a container on n: preferred while it is a host address inside n's subnet, otherwise a random host address in the upper half of the subnet, where it rarely meets the addresses the runtime hands out from the bottom of the range. The runtime may still refuse it as taken; the caller then picks again.
type Plane ¶
type Plane struct {
// contains filtered or unexported fields
}
Plane manages the services and external networks of one instance.
func New ¶
func New(rt *runtime.Manager, endpoints func() map[string]string, bind string, log *slog.Logger) *Plane
New returns a plane. endpoints returns the current host endpoint map (name → host:port), as recorded by emu.Endpoints; bind is the address the Instance binds them on (--bind).
func (*Plane) Addr ¶
Addr returns the address at which containers on the services network reach the named emulator endpoint ("gateway", "metadata", "dns", "ar", ...), starting a forwarder on the services gateway, or having the VM relay listen, if needed. The port is the same as the host endpoint's when it is free.
func (*Plane) Close ¶
Close stops all forwarders and removes the VM relay, for as long as ctx allows. Networks, and a relay ctx cuts short, are left to runtime cleanup.
func (*Plane) DialContext ¶ added in v0.6.0
DialContext is the ContainerDialer: it connects to an address on one of the Instance's container networks, through the VM relay when there is one and directly otherwise. Addresses elsewhere are always dialed directly.
func (*Plane) Expose ¶ added in v0.6.0
Expose binds addr, a loopback ip:port on the host, or a free port on the same IP when addr is taken and fallback is set, and pipes each connection it accepts to target, a container address, through DialContext. On a VM runtime this is how host clients reach a container port that appears while the Instance runs, which the runtime cannot publish then. While target cannot be reached, connections are accepted and closed, and a warning naming name, if set, and target is logged once; without a name, each failure is logged at Debug.
func (*Plane) ExposeConn ¶ added in v0.12.0
func (p *Plane) ExposeConn(addr string, fallback bool, dial func(ctx context.Context, c net.Conn) (net.Conn, error)) (*Exposed, error)
ExposeConn binds addr as Expose does and pipes each connection c it accepts to the connection dial opens for it, which may write to it first; dial sets its own timeouts.
func (*Plane) External ¶
External returns the external (egress-capable) network, creating it on first use.
func (*Plane) HostAddress ¶ added in v0.7.0
HostAddress returns the Host address of the container network n: the address the VM relay holds on it, joining it first if needed, or n's gateway where the host routes to it.
func (*Plane) Offer ¶ added in v0.7.0
Offer serves port, over UDP when udp and TCP otherwise, at hostAddr (a HostAddress), forwarding to the same port on the host's 127.0.0.1. It is idempotent, and a restarted relay offers it again. Where the host routes to the network, a port that a host process holds on the wildcard address cannot be offered: the error names that bind.
func (*Plane) RelayDecision ¶ added in v0.6.0
RelayDecision reports whether the Instance uses the VM relay, once the services network exists.
func (*Plane) Reset ¶ added in v0.6.0
Reset removes the VM relay; the next use of the plane starts a new one, which listens for the same endpoints on the same addresses.
func (*Plane) Serve ¶ added in v0.6.0
Serve pipes each connection l accepts to the first of targets, container addresses, that DialContext reaches; targets is read for each connection. It owns l.
func (*Plane) ServeDial ¶ added in v0.7.0
func (p *Plane) ServeDial(l net.Listener, dial func(ctx context.Context) (net.Conn, error)) *Exposed
ServeDial pipes each connection l accepts to the connection dial opens for it; dial sets its own timeouts. It owns l.