Skip to content

Networking ​

A lab wires its nodes together with two kinds of network: switches and links. A switch is a shared network that the Mac is part of, with optional NAT, DHCP and DNS. A link is a cable between exactly two NICs, with nothing else on it. This page shows when to use which and how to change the wiring while a lab runs.

SwitchLink
Backed bya vmnet networka socketpair
Membersany number of NICs, plus the Macexactly two NICs, on two different nodes
The Mac takes partyes, it has the first addressno, frames go straight from VM to VM
DHCP, DNS, NATbuilt in, each can be turned offnone: configure addresses in the guest
Port forwardingyes, on NAT switchesno
Limitsvmnet documents 4 per VM and 32 in total (warnings)none
Good formanagement access, internet, services the Mac usesrouting labs, point-to-point cables, VLAN trunks, bonding

Interfaces ​

Each node lists its NICs under interfaces. The list order is the PCI order in the guest.

yaml
interfaces:
  - { name: mgmt, attach: switch/mgmt, mac: "52:54:00:12:01:00" }
  - { name: p2p, attach: link/a-b }
  - { name: spare }                       # a free NIC: not plugged into anything
KeyMeaning
nameLabel used by vz (vz port down a/p2p). Not pushed into the guest. Letters, digits, ., _, -, up to 15 characters, starting with a letter.
attachswitch/<name> or link/<name>. Leave it out for a free NIC.
macOptional fixed MAC.
connectedfalse starts with the cable unplugged. Default true. Not allowed on a free NIC.

Stable MAC addresses ​

Without mac, vz derives a MAC from the lab, node and interface names: a locally administered address that starts with 02:. The same names give the same MAC on every Mac, so leases and guest config survive restarts. Renaming the lab, node or interface changes it.

Set mac yourself when cloud-init or a DHCP reservation needs to know it. A MAC must be unicast (even first byte) and unique in the lab. The examples use 52:54:00:NN:<node>:<nic>.

Switches ​

yaml
switches:
  mgmt:
    nat4: true
    ipv4Subnet: 10.101.0.0/24
    dhcpReservations:
      - { mac: "52:54:00:01:01:00", ipv4: 10.101.0.11 }
KeyDefaultMeaning
nat4falseIPv4 NAT to the internet
nat6falseIPv6 NAT to the internet
ipv4Subnetchosen by vmnetSubnet in CIDR form, /30 or larger. The Mac gets the first address.
ipv6Prefixchosen by vmnetIPv6 prefix, for example fd00:120::/64
mtu15001280 to 65535
dhcptrueBuilt-in DHCP server
dhcpReservations[]Optional fixed IPv4 addresses by MAC
dnsProxytrueBuilt-in DNS proxy on the Mac's address
routerAdvertisementtrueIPv6 router advertisements
portForwarding[]Mac ports forwarded to guests, NAT switches only

Shared or host-only ​

The NAT keys decide the vmnet mode:

  • nat4 or nat6 is true: shared mode. The VMs reach the internet through the Mac.
  • both are false: host mode. The VMs reach each other and the Mac, nothing else. Use it for anything that must stay offline (example 03).

In shared mode, the family you leave false gets no NAT: nat6: true alone gives IPv6 NAT without IPv4 NAT.

Subnets ​

  • The Mac always has the first host address (10.101.0.1 in 10.101.0.0/24). Point guests at it as their gateway and DNS server.
  • Subnets of switches in the same lab must not overlap. Labs running at the same time must not overlap either: vz only checks one lab file, and vmnet can then fail to create the second lab's switch (VMNET_FAILURE or VMNET_SHARING_SERVICE_BUSY).
  • Writing host bits (10.101.0.5/24) is a warning; the network address is used.
  • Without ipv4Subnet, vmnet picks one. That is fine for a quick test, but fixed subnets make addresses predictable.

DHCP reservations ​

Reservations are optional. Without them, every NIC on a switch with dhcp: true still gets an address: the next free one in the switch's DHCP pool. You only learn it after the guest has booted, from vz node ls (source lease or hostname, see Find a node's address).

Reserve an address when you want to know it before vz up: to SSH to a fixed address, for a port forward, an Ansible inventory or a route in another node. That is why the examples reserve one for every switch NIC.

yaml
nodes:
  - name: web
    interfaces:
      - { name: mgmt, attach: switch/mgmt, mac: "52:54:00:05:01:00" }
switches:
  mgmt:
    nat4: true
    ipv4Subnet: 10.105.0.0/24
    dhcpReservations:
      - { mac: "52:54:00:05:01:00", ipv4: 10.105.0.11 }

vz checks every reservation:

  • an interface on that switch must have the same mac (set it on the interface),
  • each MAC and each address is reserved once,
  • the address is inside ipv4Subnet, and is not the network, broadcast or the Mac's address,
  • dhcp must be true.

The guest must identify itself by its MAC for the reservation to apply. See cloud-init.

No DHCP or no DNS ​

Example 04 turns DHCP off and configures every address in cloud-init. Example 23 has one switch per feature: dhcp: false, dnsProxy: false, routerAdvertisement: false, IPv6 and mtu: 9000.

Port forwarding ​

Publish a guest port on the Mac. Only NAT switches can forward.

yaml
switches:
  mgmt:
    nat4: true
    ipv4Subnet: 10.105.0.0/24
    portForwarding:
      - { protocol: tcp, hostPort: 8080, address: 10.105.0.11, guestPort: 80 }
      - { protocol: tcp, hostPort: 2222, address: 10.105.0.11, guestPort: 22 }
    dhcpReservations:
      - { mac: "52:54:00:05:01:00", ipv4: 10.105.0.11 }
bash
curl http://localhost:8080
ssh -p 2222 lab@localhost
KeyMeaning
protocoltcp or udp
familyipv4 or ipv6. Optional: inferred from address. IPv4 needs nat4: true, IPv6 needs nat6: true.
hostPortPort on the Mac, 1 to 65535. Each protocol, family and port can be forwarded once across the lab.
addressGuest address. Reserve it with DHCP so the right node has it (otherwise a warning).
guestPortPort in the guest

vmnet's forwarding does not answer on the loopback interface. For each TCP rule, vz also listens on 127.0.0.1 and ::1 and passes connections to the guest, so localhost works. If that local port is already taken, vz logs it in the lab log and leaves the port to vmnet. UDP rules are handled by vmnet only, so use the Mac's network address rather than localhost for UDP.

yaml
nodes:
  - name: a
    interfaces:
      - { name: p2p, attach: link/a-b, mac: "52:54:00:12:01:01" }
  - name: b
    interfaces:
      - { name: p2p, attach: link/a-b, mac: "52:54:00:12:02:01" }

links:
  a-b: {}

A link is a virtual cable. Every frame one end sends arrives unchanged at the other end, and no vz code runs in the data path. The rules:

  • exactly two interfaces attach to it, on two different nodes (use a switch for more),
  • there is no DHCP and no Mac on it: set addresses in each guest's network-config, or run your own DHCP server in a node,
  • an unused link is a warning.
KeyDefaultMeaning
mtu1500MTU on both ends, 1280 to 65535
buffer4MiBAdvanced: socket send buffer (the receive buffer is twice that). 1MiB to 256MiB is expected.

Example 17 puts a mtu: 9000 link next to a standard one. Routing labs such as 13, 14 and 30 are built almost only from links.

Find a node's address ​

bash
vz node ls            # alias: vz node status

For each NIC on a switch, vz reports the address the guest is using, from the most to the least reliable source:

SourceMeaning
reservationmacOS's DHCP server holds the NIC's reservation (/etc/bootptab), so this is the address the guest gets. The guest may still be booting.
leasea DHCP lease in /var/db/dhcpd_leases whose client ID is this MAC
hostnamethe newest lease in the switch subnet with the node's name as hostname (guests that send a DUID). Only used when the switch has a known subnet.
reservedthe reservation from the lab file, which macOS's DHCP server no longer holds, and no lease was found

A lease for the same MAC can stay in the lease file after the guest moved to its reservation, so a held reservation wins over a lease.

Notes that can follow the table:

  • ... does not hold its reservation or ... is using <ip> from the DHCP pool: the reservation is gone from /etc/bootptab, usually after a node restart while another NAT lab runs. vz down and vz up restore it. See Troubleshooting.
  • ... is using <ip>, not its reservation <ip>: the guest's DHCP client id is not its MAC: add dhcp-identifier: mac to the guest's network config.

NICs on links have no address in this table: vz cannot see inside a cable. Use the console. vz node ls --json includes ip and ipSource for scripts.

Change the wiring at runtime ​

Unplug and plug cables on a running lab, the way you would pull a cable from a real switch:

bash
vz link down a-b            # unplug both ends of a link
vz link up a-b
vz port down vm2/mgmt       # unplug one NIC (switch or link)
vz port up vm2/mgmt

The guest sees the carrier go down and up. Routing labs use this to watch protocols converge (OSPF failover, bonding).

To start with a cable unplugged, set connected: false on the interface:

yaml
interfaces:
  - { name: spare, attach: switch/mgmt, mac: "52:54:00:22:01:01", connected: false }
bash
vz port up tiny/spare

When the two ends of a link disagree about connected, vz validate warns.

NOTE

link and port change the running lab only. They are not written to the lab file, so the next vz up starts from connected as the file says.

See the topology ​

bash
vz topology

topology reads the lab file only, so it works before vz up. It prints three tables:

  • Switches: name, mode (shared or host), MTU, IPv4 subnet, and IPv6 prefix when any switch has one,
  • Links: name, MTU and both ends,
  • Ports: every NIC with what it is attached to (switch/<name>, link/<name> or free) and its MAC.

See also ​