Files
napalm-proxmox/napalm_proxmox/uplinks.py
T
Christian Manivong 72c30dd4c2
driver / standard (pull_request) Successful in 18s
driver / lint (pull_request) Successful in 15s
driver / types (pull_request) Successful in 1m7s
driver / test (3.13) (pull_request) Successful in 1m11s
driver / test (3.12) (pull_request) Successful in 1m15s
driver / lowest (pull_request) Successful in 38s
driver / build (pull_request) Successful in 37s
driver / security (pull_request) Successful in 1m21s
CI / driver (pull_request) Successful in 3m5s
driver / publish (pull_request) Skipped
feat: get_network_targets names each bridge's uplinks (NetOrk/netork#1325)
netOrk's VM dialog follows a bridge's physical NICs to the switch port
behind it, to warn when a VM without a VLAN tag would land nowhere. The
driver knows those NICs from the node's network config.

uplinks is read from bridge_ports (Linux bridge) or ovs_ports (OVS
bridge), one group per bridge port: a bond (slaves / ovs_bonds) as its
NICs together, a NIC alone. Internal ports (OVSIntPort) and VLAN
subinterfaces leave out, as they never carry the bridge's untagged
traffic to the switch. A port missing from the config or of a kind the
driver cannot place leaves the key out rather than guessing.
2026-10-30 09:28:07 +01:00

68 lines
2.6 KiB
Python

"""The physical NICs a bridge leaves the node through (``NetworkTargetDict.uplinks``).
One group per way out of the bridge: a bond's slaves together, a single NIC alone.
Read from the node's network config (``GET /nodes/{node}/network``): a Linux
bridge names its ports in ``bridge_ports``, an OVS bridge in ``ovs_ports``. A
Linux bond lists its NICs in ``slaves``, an OVS bond in ``ovs_bonds``. Internal
ports (``OVSIntPort``) and VLAN subinterfaces are no uplinks: the first never
leaves the node, the second leaves it tagged, so the switch port's untagged
VLAN is not the bridge's.
"""
from __future__ import annotations
from typing import Any
#: Types of a port that is a physical NIC of its own.
_NIC_TYPES = frozenset({"eth", "OVSPort"})
#: Types of a port that leaves the node through its member NICs.
_BOND_TYPES = frozenset({"bond", "OVSBond"})
#: Types of a port that is no uplink.
_NOT_UPLINK_TYPES = frozenset({"vlan", "OVSIntPort"})
class _Unknown(Exception):
"""A port whose kind the network config does not tell."""
def _words(value: Any) -> list[str]:
return [w for w in str(value or "").split() if w != "none"]
def _nics_of(port: str, by_name: dict[str, dict[str, Any]], in_bond: bool = False) -> list[str]:
info = by_name.get(port)
if info is None:
raise _Unknown(port)
kind = info.get("type", "")
if kind in _BOND_TYPES:
return _bond_nics(info, by_name)
# An OVS bond's slave can come without a type of its own.
if kind in _NIC_TYPES or (in_bond and not kind):
# A VLAN subinterface added to an OVS bridge is an OVSPort too.
return [] if "." in port else [port]
if kind in _NOT_UPLINK_TYPES:
return []
raise _Unknown(port)
def _bond_nics(bond: dict[str, Any], by_name: dict[str, dict[str, Any]]) -> list[str]:
members = _words(bond.get("slaves") or bond.get("ovs_bonds"))
return [nic for member in members for nic in _nics_of(member, by_name, in_bond=True)]
def bridge_uplinks(bridge: dict[str, Any], network: list[dict[str, Any]]) -> list[list[str]] | None:
"""The physical NICs *bridge* leaves the node through, per port, or None when unknown.
An empty list is a bridge without uplink. None means a port is missing from
the network config or of a kind this cannot place — a guess would send the
caller to the wrong switch port.
"""
by_name = {iface["iface"]: iface for iface in network if iface.get("iface")}
ports = _words(bridge.get("ovs_ports") or bridge.get("bridge_ports"))
try:
groups = [_nics_of(port, by_name) for port in ports]
except _Unknown:
return None
return [group for group in groups if group]