Open networks, no NAT



In Base image Fedora 45, saturn and jupiter were cloned from one sealed image onto libvirt's default network. That network is NAT: the guests can reach out, but nothing can reach in, and every guest's traffic leaves with the host's address. That's fine for a laptop VM. It's wrong for a cluster spread over machines, where a node on one machine must reach a node on another by its own address.
This post gives each "machine" a network of its own and routes between them, with no NAT and no tunnel. f45-min stands in for two hypervisors, machine 1 and machine 11, each with a bridge, a subnet and one guest, and it is also the router between them. What lives behind each machine, the router learns from the machine itself, over BGP.
All commands run on f45-min, against the system connection:
$ export LIBVIRT_DEFAULT_URI=qemu:///system
An address that says where it is
The lab uses an address plan where every address names the machine it lives on. The second octet is the plane, the third is the machine:
| plane | block | machine 1 | machine 11 |
|---|---|---|---|
| guests | 10.17.m.0/24 | 10.17.1.0/24 (saturn .10) | 10.17.11.0/24 (jupiter .10) |
| nodes | 10.49.m.0/24 | 10.49.1.0/24 | 10.49.11.0/24 |
| load balancers | 10.33.0.0/24 | shared, announced by BGP |
The node plane is where the Kubernetes nodes will live in the next post: inside each guest, routed to through it. Read 10.49.11.3 and you know it's a node on machine 11 without looking anything up.
Why not NAT, and why not route
libvirt offers three forwarding modes that matter here:
- nat masquerades everything leaving the bridge, and libvirt's firewall rules reject traffic coming in. A connection from another machine to a guest fails as "connection refused", which looks like a dead service rather than a network that won't let you in.
- route forwards without masquerading, but libvirt still writes firewall rules that only accept traffic for the network's own subnet. A subnet behind a guest, like the node plane, is dropped.
- open writes no firewall rules at all. libvirt makes the bridge, runs DHCP and DNS on it, and leaves forwarding to you.
Open is the only one that routes a subnet behind a guest. The price is that you own the firewall.
Two open networks
Each network is a bridge, a subnet with DHCP reservations, and a DNS zone. Both networks carry the same lab.internal records, because each runs its own dnsmasq and a guest asks only the one on its bridge:
$ cat lab-m1.xml
<network>
<name>lab-m1</name>
<forward mode='open'/>
<bridge name='br-lab-m1' stp='on' delay='0'/>
<domain name='lab.internal' localOnly='yes'/>
<dns>
<host ip='10.49.1.2'><hostname>api.lab.internal</hostname></host>
<host ip='10.17.1.10'><hostname>saturn</hostname></host>
<host ip='10.17.11.10'><hostname>jupiter</hostname></host>
<host ip='10.17.1.1'><hostname>registry</hostname></host>
</dns>
<ip address='10.17.1.1' netmask='255.255.255.0'>
<dhcp>
<range start='10.17.1.100' end='10.17.1.200'/>
<host mac='52:54:00:17:01:10' name='saturn' ip='10.17.1.10'/>
</dhcp>
</ip>
</network>
$ virsh net-define lab-m1.xml && virsh net-autostart lab-m1 && virsh net-start lab-m1
lab-m11 is the same with 11 in place of 1 and jupiter's reservation.
The firewall that open mode leaves to you
The firewall here is firewalld, Fedora's and RHEL's default. With plain nftables the same decisions are two accept rules in the forward chain; what matters is which traffic may cross, not the tool.
firewalld puts an interface it doesn't know into the default zone, public, which doesn't forward. Name both bridges into the libvirt zone explicitly:
$ sudo firewall-cmd --permanent --zone=libvirt --add-interface=br-lab-m1
$ sudo firewall-cmd --permanent --zone=libvirt --add-interface=br-lab-m11
Zones decide what may reach the host itself. Forwarding between zones takes a policy. Two cover every libvirt bridge, at priority -1 so they're checked before libvirt's own: lab-in lets the outside reach the guests, and nothing else, and lab-out lets the guests reach anywhere. firewall-cmd takes one policy option per call, hence the loops:
$ sudo firewall-cmd --permanent --new-policy lab-in
$ for o in '--add-ingress-zone public' '--add-egress-zone libvirt' '--set-target ACCEPT' '--set-priority -1'; do sudo firewall-cmd --permanent --policy lab-in $o; done
$ sudo firewall-cmd --permanent --new-policy lab-out
$ for o in '--add-ingress-zone libvirt' '--add-egress-zone ANY' '--set-target ACCEPT' '--set-priority -1'; do sudo firewall-cmd --permanent --policy lab-out $o; done
$ sudo firewall-cmd --reload
$ sudo firewall-cmd --get-active-zones
libvirt
interfaces: br-lab-m1 br-lab-m11 virbr0
public (default)
interfaces: eth0
$ sudo firewall-cmd --info-policy lab-in | grep -E 'target|zones'
target: ACCEPT
ingress-zones: public
egress-zones: libvirt
Give lab-in the guests' zone as its egress, not ANY. With ANY, anything arriving on the uplink may be forwarded into any zone the host has, not only to the guests, and the host routes for whoever reaches it.
--permanent changes only the saved configuration. Until the reload, the running firewall knows nothing of it, so always check the runtime state, as above, rather than the files.
Masquerade, but not for the lab
The guests still need the internet, so traffic leaving the host for the outside world is masqueraded. Traffic between the lab's own planes must keep its source address. One nft table per network says both, in that order:
$ cat kvm-lab-m1.nft
table ip kvm_lab_m1
delete table ip kvm_lab_m1
table ip kvm_lab_m1 {
chain postrouting {
type nat hook postrouting priority srcnat; policy accept;
ip saddr { 10.17.1.0/24, 10.49.1.0/24 } ip daddr { 10.16.0.0/12, 10.32.0.0/11 } return
ip saddr { 10.17.1.0/24, 10.49.1.0/24 } oifname != "br-lab-m1" masquerade
}
}
The exemptions are whole blocks, 10.16.0.0/12 and 10.32.0.0/11, not a list of subnets: a machine added later is already covered. nft merges the two into one range when it loads them:
$ sudo nft list table ip kvm_lab_m1
table ip kvm_lab_m1 {
chain postrouting {
type nat hook postrouting priority srcnat; policy accept;
ip saddr { 10.17.1.0/24, 10.49.1.0/24 } ip daddr { 10.16.0.0-10.63.255.255 } return
ip saddr { 10.17.1.0/24, 10.49.1.0/24 } oifname != "br-lab-m1" masquerade
}
}
The first two lines of the file make it safe to load twice: create the table if it's missing, delete it, then define it whole. A oneshot unit, kvm-lab-m1-nft.service, loads it at boot, after firewalld, which would otherwise flush it.
The host forwards between its bridges and out of them, so forwarding is on, set where sysctl keeps such settings rather than as a side effect of some unit:
$ cat 90-lab-forward.conf
net.ipv4.ip_forward = 1
$ sudo cp 90-lab-forward.conf /etc/sysctl.d/ && sudo sysctl --load /etc/sysctl.d/90-lab-forward.conf
A guard for the planes
The lab reuses the addresses of my real network, and its real cluster sits behind the LAN router. A lab address with no lab route would follow f45-min's default route out to the LAN and reach the real thing. A last-resort route for each block makes that fail on f45-min instead:
$ cat lab-planes-guard.service
[Unit]
Description=Keep lab address planes from leaking to the LAN
Before=bird.service
After=network-online.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/sbin/ip route replace unreachable 10.16.0.0/12 metric 4000
ExecStart=/usr/sbin/ip route replace unreachable 10.32.0.0/11 metric 4000
[Install]
WantedBy=multi-user.target
Any more specific route wins over it, so the lab's own subnets are unaffected, and it's in place before the router below starts. Anything else in those blocks now fails at once:
$ ip route get 10.33.0.99
RTNETLINK answers: No route to host
The router learns what's behind each machine
f45-min knows the guest subnets, because the bridges are on them. The node planes are behind the guests, and only each guest knows whether its plane exists. So each machine announces its own, and the router learns it over BGP, with BFD under each session to notice a machine that goes silent within a second. Nothing about a machine is written down on the router except that it is a peer.
The router here is BIRD:
$ sudo dnf install -y -q bird
$ cat bird.conf
log syslog all;
router id 10.17.1.1;
protocol device {}
# what the machines announce goes into the kernel
protocol kernel {
ipv4 { import none; export where source = RTS_BGP; };
}
protocol bfd {
interface "br-lab-*" { min rx interval 300 ms; min tx interval 300 ms; multiplier 3; };
}
# a machine may announce node-plane /24s, nothing else
filter planes_in {
if net ~ [ 10.48.0.0/12{24,24} ] then accept;
reject;
}
protocol bgp saturn {
local 10.17.1.1 as 64512; strict bind;
neighbor 10.17.1.10 as 65201;
bfd on; connect retry time 5;
ipv4 { import filter planes_in; export none; };
}
protocol bgp jupiter {
local 10.17.11.1 as 64512; strict bind;
neighbor 10.17.11.10 as 65211;
bfd on; connect retry time 5;
ipv4 { import filter planes_in; export none; };
}
$ sudo cp bird.conf /etc/bird.conf && sudo bird -p -c /etc/bird.conf && echo config ok
config ok
What each part is for:
- The filter accepts node-plane
/24s and rejects everything else. A machine can't announce a route to anywhere else, by mistake or otherwise. The router exports nothing back: the guests' default route already points at it. bfd onat 300 ms × 3 tears a session down 0.9 s after a machine stops answering, and its routes go with it. Without BFD, BGP waits out its hold time, minutes by default.connect retry time 5retries a lost session every 5 s, so it comes back within seconds of the machine instead of after BGP's default 120 s.strict bindbinds each session to its bridge address. The speakers that announce load-balancer addresses later in the series listen on the same addresses, and BIRD shares a listening socket only between protocols that agree on it.
strict bind needs the bridge addresses to exist when BIRD starts, and on a loaded host the bridges can come up late. A drop-in makes BIRD wait for both, with no start timeout:
$ cat bird-bridges.conf
[Unit]
Wants=virtnetworkd.service
After=virtnetworkd.service lab-planes-guard.service
[Service]
TimeoutStartSec=infinity
ExecStartPre=/bin/sh -c 'until ip -4 addr show dev br-lab-m1 | grep -q inet && ip -4 addr show dev br-lab-m11 | grep -q inet; do sleep 1; done'
$ sudo mkdir -p /etc/systemd/system/bird.service.d && sudo cp bird-bridges.conf /etc/systemd/system/bird.service.d/bridges.conf
The libvirt zone lets in only what it names, so BGP and BFD are added to it, and BIRD starts:
$ sudo firewall-cmd --permanent --zone=libvirt --add-service=bgp && sudo firewall-cmd --permanent --zone=libvirt --add-port=3784/udp && sudo firewall-cmd --reload
$ sudo systemctl daemon-reload && sudo systemctl enable --now bird
$ sudo birdc show protocols
Name Proto Table State Since Info
device1 Device --- up 06:05:56.647
kernel1 Kernel master4 up 06:05:56.647
bfd1 BFD --- up 06:05:56.647
saturn BGP --- start 06:05:56.647 Idle
jupiter BGP --- start 06:05:56.647 Idle
Idle, because the machines don't exist yet on these networks.
Each machine announces itself
saturn and jupiter are made again, the way the last post made them, on lab-m1 and lab-m11 instead of the default network. The clones from the last post and their reservations on the default network go first:
$ for g in saturn jupiter; do virsh destroy $g; virsh undefine $g --remove-all-storage; done
$ virsh net-update default delete ip-dhcp-host "<host mac='52:54:00:45:00:10'/>" --live --config
$ virsh net-update default delete ip-dhcp-host "<host mac='52:54:00:45:00:11'/>" --live --config
The base image carries FRR with its BGP and BFD daemons on and no configuration. Each clone's seed brings that configuration, so user-data is now per machine: what the last post's had, plus the machine's frr.conf, the firewall openings for BGP and BFD, and the start of the service:
$ cat saturn.user-data
...
write_files:
- path: /etc/frr/frr.conf
owner: frr:frr
permissions: "0640"
content: |
hostname saturn
! this machine's node plane, and nothing else
ip prefix-list PLANES seq 10 permit 10.49.1.0/24
route-map PLANES permit 10
match ip address prefix-list PLANES
bfd
peer 10.17.1.1
receive-interval 300
transmit-interval 300
detect-multiplier 3
router bgp 65201
bgp router-id 10.17.1.10
no bgp ebgp-requires-policy
neighbor 10.17.1.1 remote-as 64512
neighbor 10.17.1.1 bfd
neighbor 10.17.1.1 timers connect 5
address-family ipv4 unicast
redistribute kernel route-map PLANES
exit-address-family
runcmd:
- [firewall-cmd, --permanent, --add-service=bgp]
- [firewall-cmd, --permanent, --add-port=3784/udp]
- [firewall-cmd, --reload]
- [systemctl, enable, --now, frr]
The node plane will be a kernel route on the machine, into the namespace where its nodes run, so FRR redistributes kernel routes. The prefix-list is not decoration: a machine has more kernel routes than its node plane, and without the list it would offer all of them. The router would reject them, but a machine should say only what is true of it. jupiter's is the same with 11 in place of 1 and AS 65211.
The guests get the room the next post's Kubernetes nodes need, saturn 7 GB and jupiter 5 GB, 4 vCPUs each:
$ virsh vol-create-as images saturn.vda.x86_64.qcow2 30G --format qcow2 \
--backing-vol f45-base.vda.x86_64.qcow2 --backing-vol-format qcow2
$ virt-install --name saturn --memory 7168 --vcpus 4 --cpu host-passthrough --os-variant fedora-unknown --graphics none \
--network network=lab-m1,mac=52:54:00:17:01:10 \
--disk=/storage/images/saturn.vda.x86_64.qcow2,bus=virtio,format=qcow2,cache=none,io=native \
--cloud-init user-data=./saturn.user-data,meta-data=./saturn.meta-data \
--events on_reboot=restart --import --noautoconsole && virsh autostart saturn
--events on_reboot=restart and virsh autostart keep a machine running when it reboots itself and bring it back when f45-min does. jupiter is made the same way on lab-m11.
Sessions up, nothing to announce
Once cloud-init has run on each, a minute or two after first boot, both machines are peers, with BFD under each session:
$ sudo birdc show protocols
Name Proto Table State Since Info
...
saturn BGP --- up 06:08:47.036 Established
jupiter BGP --- up 06:09:40.718 Established
$ sudo birdc show bfd sessions
IP address Interface State Since Interval Timeout
10.17.1.10 br-lab-m1 Up 06:08:47.030 0.300 0.900
10.17.11.10 br-lab-m11 Up 06:09:40.712 0.300 0.900
$ sudo birdc show protocols all saturn | grep -E 'BGP state|Neighbor AS|Routes:'
BGP state: Established
Neighbor AS: 65201
Routes: 0 imported, 0 exported, 0 preferred
No routes, and that is correct: the node planes don't exist yet. There are no nodes, so there is nothing to route to, and the router has no route pretending otherwise. An address in a node plane fails on f45-min, at the guard:
$ ip route get 10.49.1.2
RTNETLINK answers: No route to host
When the first node starts on saturn in the next post, its plane appears as a kernel route there, FRR announces it, and f45-min routes to it. When the last node goes, the route goes with it. A hand-written route can't do that: it stays, pointing at a plane that's gone, and the traffic it attracts disappears into the machine.
Across the machines, by real address
Each guest has one interface and a default route to its own bridge:
[fedora@saturn ~]$ ip route
default via 10.17.1.1 dev eth0 proto dhcp src 10.17.1.10 metric 100
10.17.1.0/24 dev eth0 proto kernel scope link src 10.17.1.10 metric 100
[fedora@saturn ~]$ sudo vtysh -c 'show bgp ipv4 unicast summary'
BGP router identifier 10.17.1.10, local AS number 65201 VRF default vrf-id 0
...
Neighbor V AS MsgRcvd MsgSent TblVer InQ OutQ Up/Down State/PfxRcd PfxSnt Desc
10.17.1.1 4 64512 9 7 0 0 0 00:02:20 0 0 N/A
[fedora@saturn ~]$ ping -c 3 jupiter.lab.internal
64 bytes from jupiter.lab.internal (10.17.11.10): icmp_seq=1 ttl=63 time=1.29 ms
The TTL of 63 is the one routed hop through f45-min. What NAT would have hidden is the source. Jumping through saturn to jupiter, jupiter sees the connection come from saturn's own address:
$ ssh -J saturn.lab.internal jupiter.lab.internal 'echo $SSH_CONNECTION'
10.17.1.10 54482 10.17.11.10 22
More than one box
On one f45-min both bridges are local, and f45-min is the only router. With real hypervisors the router is the network's own, and every machine peers with it the same way. A machine added later brings its own frr.conf and becomes one more peer on the router; nothing else changes.
The usual alternative is static peer routes: every hypervisor carries a route to every other's guest and node subnets. They're added when a machine joins and never removed when one leaves, so they pile up pointing at nothing. On a network I've run this on, a check found 7 of 10 of them dead. Announced routes can't outlive what they point at.
Next, the guests become Kubernetes nodes: one cluster across both machines, on these routed planes, with no tunnel.