Skip to content

F #329: Restore GATEWAY_IFACE to choose the default-route NIC - #425

Open
pgehres wants to merge 1 commit into
OpenNebula:masterfrom
pgehres:gateway-iface
Open

pgehres wants to merge 1 commit into
OpenNebula:masterfrom
pgehres:gateway-iface

Conversation

@pgehres

@pgehres pgehres commented Nov 15, 2026

Copy link
Copy Markdown

Description

Closes #329. Related: OpenNebula/one#2083.

A VM with NICs on more than one virtual network that defines GATEWAY gets one default route per NIC. OpenNebula core copies each vnet's GATEWAY into ETHx_GATEWAY (and overwrites a user-set empty ETHx_GATEWAY/NIC GATEWAY=""), and every netcfg-* backend installs a default route for each non-empty one. The result is two or more competing default routes, sometimes on different metrics, and asymmetric routing.

The old addon-context-linux handled this with the VM-level context variable GATEWAY_IFACE (ETH0 or 0). Only the selected interface got a gateway (OpenNebula/addon-context-linux#2). The variable was dropped without notice when loc-10-network was refactored into loc-10-network.d/ (F #227, 2021). It is still in old docs and people still use it.

This PR brings it back in one place, setup_iface_vars() in loc-10-network.d/functions. Every backend (interfaces, netplan, networkd, nm, scripts, bsd) gets gateway/ip6_gateway from there, so they all inherit the behaviour:

  • If GATEWAY_IFACE is set and doesn't match the interface being configured, gateway and ip6_gateway are cleared for that interface. Accepted forms: ETH1, eth1, 1 (the 1 form keeps compatibility with the old implementation). Surrounding whitespace is ignored. The old version only affected IPv4. This one covers IPv6 too, which is what users expect from "the gateway interface".
  • The value is always the context NIC name: ETHn is the NIC with NIC_ID n, the same prefix as ETHn_IP/ETHn_GATEWAY. It is never the guest's device name. one-context maps ETHn to the guest device by MAC (get_dev), and the check compares against the context name before the guest name is involved. So GATEWAY_IFACE=ETH0 picks NIC 0 whether the guest calls it eth0, ens3 or enp1s0. Lowercase eth0 is accepted only for convenience and still means ETH0, not a kernel device called eth0. After NIC hot-plug, NIC IDs can have gaps or be reused, so onevm show is the reference for which ETHn is which.
  • gateway/ip6_gateway are whitespace-trimmed. The long-standing workaround from Add option to remove gateway when adding additional nic one#2083 is to set NIC GATEWAY=" " (a single space survives core as ETHx_GATEWAY=" "). Before this PR it produced an invalid gateway stanza in /etc/network/interfaces (and the same in the other backends). Now it means "no gateway".
  • loc-10-network-pci (PCI passthrough NICs) honours GATEWAY_IFACE too, matched by full name (e.g. PCI0). Setting GATEWAY_IFACE=PCI0 therefore keeps the only default route on the PCI NIC, and GATEWAY_IFACE=ETH0 stops PCI NICs from adding their own.

Not affected:

  • ETHx_ROUTES static routes. They are read separately and still apply on every NIC.
  • The OneGate proxy link route (missing_onegate_proxy_route). It doesn't depend on gateway and is still added on the first configured NIC.
  • METHOD=dhcp / IP6_METHOD=auto|dhcp interfaces. Their default route comes from DHCP/RA, not from context, so GATEWAY_IFACE doesn't filter it. This matches the old behaviour.
  • Behaviour with GATEWAY_IFACE unset. Nothing changes, apart from the whitespace trim.

Docs: GATEWAY_IFACE should be added back to the context variables reference in OpenNebula/docs (Network Configuration section). I can open a separate PR there.

Branches to which this PR applies

  • master
  • one-X.Y

Testing

There is no automated harness for context-linux. I tested it in two ways.

1. Offline config generation. I sourced functions + netcfg-{interfaces,netplan} with a stubbed get_interface_mac, using two NICs that both have IPv4 and IPv6 gateways (ETH1 uses legacy GATEWAY6) and a link-local ONEGATE_ENDPOINT:

Context eth0 default eth1 default OneGate route
(unset) v4 + v6 v4 + v6 eth0
GATEWAY_IFACE=ETH1 — v4 + v6 eth0
GATEWAY_IFACE=eth0 v4 + v6 — eth0
GATEWAY_IFACE=1 — v4 + v6 eth0
GATEWAY_IFACE=" Eth1 " — v4 + v6 eth0
GATEWAY_IFACE=PCI0 — — eth0
ETH1_GATEWAY=" " v4 + v6 v6 only, no broken gateway line eth0

On master the last case emits gateway (gateway keyword followed by blanks) for eth1. shellcheck output for the touched files is unchanged.

2. On a real VM. OpenNebula 7.2 with a Debian 13 guest and two NICs on vnets that both define GATEWAY (eth0 192.168.10.0/24, eth1 192.168.9.0/24). I installed the released one-context_7.4.2-0.deb, whose loc-10-network* files are byte-identical to master, then overlaid the two patched files. Each row is a full reboot after onevm updateconf:

one-context NETCFG_TYPE Context Result
7.2.0 stock (default → interfaces) GATEWAY_IFACE="ETH0" Ignored: gateway lines for both NICs. ifup eth1 fails with RTNETLINK answers: File exists and networking.service is failed.
7.4.2 stock (default → netplan) GATEWAY_IFACE="ETH0" Ignored: two default routes, and via 192.168.9.1 dev eth1 is listed first.
7.4.2 + patch (default → netplan) GATEWAY_IFACE="ETH0" One default route, dev eth0. Both links routable configured.
7.4.2 + patch (default → netplan) GATEWAY_IFACE="ETH1" One default route, dev eth1.
7.4.2 + patch interfaces (unset) Same as before the patch: both gateway lines, ifup eth1 fails.
7.4.2 + patch interfaces GATEWAY_IFACE="0" One gateway line (eth0). networking.service active.
7.4.2 stock interfaces NIC 1 GATEWAY=" " (→ ETH1_GATEWAY=" ") ifup: /etc/network/interfaces:18: option with empty value → couldn't read interfaces file. No interface comes up, and the VM is unreachable.
7.4.2 + patch interfaces NIC 1 GATEWAY=" " No eth1 gateway line, networking.service active, single default route via eth0.

To reproduce: put GATEWAY_IFACE = "ETH0" in a two-NIC VM's CONTEXT, reboot, and check ip route show default, ip -6 route show default and the generated /etc/netplan/50-one-context.yaml or /etc/network/interfaces.

New features

  • Yes: VM-level context variable GATEWAY_IFACE (context NIC name ETHx/ethx/x, or PCIx; not the guest device name) limits context-provided default routes (IPv4 and IPv6) to the selected interface.

🤖 Generated with Claude Code

The VM-level GATEWAY_IFACE context variable (ETH0/eth0/0, or PCI0)
was supported by addon-context-linux but silently dropped by the
loc-10-network refactor (F OpenNebula#227). Without it, a VM with NICs on several
vnets that define GATEWAY gets a default route per NIC.

Re-implement it centrally in setup_iface_vars() so every netcfg backend
honours it (IPv4 and IPv6), and in loc-10-network-pci for PCI NICs.

Also trim whitespace from the gateways so the NIC GATEWAY=" "
workaround (OpenNebula/one#2083) yields no gateway instead of an
invalid "gateway " line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Peter Gehres <357174+pgehres@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GATEWAY_IFACE not implemented in netplan and networkd network backends

1 participant