1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-11-08 11:16:47 +00:00
webvirtcloud/vrtManager/network.py
catborise 0c9a002ef4 fix(networks): a network's addresses are read as libvirt writes them, and its changes keep each other
Reviewed with Codex against libvirt's network format and driver.

- Addresses: wvmNetwork found a network's <ip> by its text, so one
  written as <ip .../> or <ip><dhcp>... without whitespace had none,
  and a second address of a family overwrote the first. ip_networks
  reads every <ip> in order, its index being libvirt's parentIndex:
  family (ipv4 when written), address, subnet, DHCP (a <dhcp>, also
  with fixed addresses only), first range and fixed addresses. The
  parentIndex was guessed from the number of families, wrong when IPv6
  comes first; changes now name the address by its index.
- One read: the page read the network's XML once per getter, ten or
  more round trips; it reads it once (a cached_property of the
  request's wvmNetwork). A change starts from the saved definition
  read anew (VIR_NETWORK_XML_INACTIVE), not the running one: two QoS
  changes on a running network no longer undo the first. The XML
  editor shows and changes the saved definition, and defines this
  network only (its name, and its UUID if given), read for that with
  no entity expanded and handed to libvirt as written; an XML declaring
  its encoding is taken. The actions go back to the network's page by
  its own URL.
- Fixed addresses are found by the address they had (a hidden field),
  not by the editable box: changing it and pressing Delete deleted
  another entry or none. A change deletes the entry and adds it anew
  from a copy, keeping what the page does not show (a client id, a
  lease), and puts it back if the add fails: libvirt's own change
  finds an entry by any of its MAC, address or name, so it could
  replace another entry, and never by an IPv6 DUID. The address must
  be of the subnet, not the host's, and neither it, the MAC or DUID
  nor the name another entry's; an entry gone meanwhile is refused,
  not added anew; an unchanged one writes no audit row. They can be
  added while the network is stopped; a running network saved with
  other addresses than it runs with is not changed live, as
  parentIndex would name another <ip> of it.
- An address without a netmask or prefix takes libvirt's (IPv4 its
  class's, IPv6 /64), for its checks and the overlap check.
- The DHCP range must be in the subnet, in order and not take in the
  host's address.
- The create form takes a known mode only ("bogus" made an isolated
  network, a missing one a server error); NAT and routed networks need
  an IPv4 subnet, an isolated one may have none, a bridge or macvtap
  network needs its bridge or interface and gets no addresses; IPv6
  only with its box (whose value was "false", so never); no name or
  subnet of another network of the host. "Fixed Addresses" at create
  goes: it made a random-MAC entry for every address of the range,
  for no real VM.
- Refusals of a fixed address, the DHCP range, the XML and a QoS
  removal are messages, not server errors.
- The IPv6 forwarding of a bridge or macvtap network is the outside
  network's, not ROUTE.
- get_networks_info reads each network's XML once and gives its
  autostart and addresses too. Unused can_pxe, DHCP lease helpers,
  get_uuid and the per-family range and host getters go.
2026-10-29 10:09:14 +03:00

396 lines
17 KiB
Python

import copy
import ipaddress
from functools import cached_property
from libvirt import (
VIR_NETWORK_SECTION_IP_DHCP_HOST,
VIR_NETWORK_UPDATE_AFFECT_CONFIG,
VIR_NETWORK_UPDATE_AFFECT_LIVE,
VIR_NETWORK_UPDATE_COMMAND_ADD_LAST,
VIR_NETWORK_UPDATE_COMMAND_DELETE,
VIR_NETWORK_XML_INACTIVE,
libvirtError,
)
from lxml import etree
from vrtManager import util
from vrtManager.connection import wvmConnect
def network_size(subnet, dhcp=None):
"""
Func return gateway, mask and dhcp pool.
"""
net = ipaddress.ip_network(subnet, strict=False)
gateway = str(net.network_address + 1)
if net.version == 4:
mask = str(net.netmask)
dhcp_pool = [str(net.network_address + 2), str(net.broadcast_address - 1)]
else:
mask = str(net.prefixlen)
dhcp_pool = [str(net.network_address + 256), str(net.network_address + 511)]
return (gateway, mask, dhcp_pool) if dhcp else (gateway, mask, None)
def default_prefix(address):
"""The prefix libvirt gives an address written without one: IPv6 /64, IPv4
its class's (A /8, B /16, C /24)."""
address = ipaddress.ip_address(address)
if address.version == 6:
return 64
first = int(address) >> 24
return 8 if first < 128 else 16 if first < 192 else 24
def ip_networks(tree):
"""Each <ip> of a network's XML, in order: its index is libvirt's parentIndex
for changes to it. Its family, address (the host's on the network), subnet
(by its netmask or prefix, or libvirt's default one), whether it serves
DHCP (a <dhcp>, with or without a range: fixed addresses alone are DHCP
too), its first range and its fixed addresses."""
ips = []
for index, ip in enumerate(tree.findall("ip")):
address = ip.get("address")
mask = ip.get("prefix") or ip.get("netmask") or (default_prefix(address) if address else None)
dhcp = ip.find("dhcp")
first = dhcp.find("range") if dhcp is not None else None
ips.append({
"index": index,
"family": ip.get("family", "ipv4"),
"address": address,
"subnet": ipaddress.ip_network(f"{address}/{mask}", strict=False) if mask else None,
"dhcp": dhcp is not None,
"range": (first.get("start"), first.get("end")) if first is not None else None,
"hosts": [{"ip": host.get("ip"), "mac": host.get("mac"), "id": host.get("id"), "name": host.get("name", "")}
for host in (dhcp.findall("host") if dhcp is not None else [])],
})
return ips
class wvmNetworks(wvmConnect):
def get_networks_info(self):
"""Each network's state, autostart, device (its bridge, or a macvtap
network's interface), forward mode and addresses (ip_networks): its
XML is read once."""
networks = []
for name in self.get_networks():
net = self.get_network(name)
tree = etree.fromstring(net.XMLDesc(0))
forward = tree.find("forward")
networks.append({
"name": name,
"status": net.isActive(),
"autostart": net.autostart(),
"device": tree.xpath("string(bridge/@name)") or tree.xpath("string(forward/interface/@dev)"),
"forward": forward.get("mode") if forward is not None else None,
"ips": ip_networks(tree),
})
return networks
def create_network(
self,
name,
forward,
ipv4,
gateway,
mask,
dhcp4,
ipv6,
gateway6,
prefix6,
dhcp6,
bridge,
openvswitch,
):
esc_name = util.xml_escape(str(name))
esc_forward = util.xml_escape(str(forward)) if forward else ""
esc_bridge = util.xml_escape(str(bridge)) if bridge else ""
esc_gateway = util.xml_escape(str(gateway)) if gateway else ""
esc_mask = util.xml_escape(str(mask)) if mask else ""
esc_gateway6 = util.xml_escape(str(gateway6)) if gateway6 else ""
esc_prefix6 = util.xml_escape(str(prefix6)) if prefix6 else ""
xml = f"""
<network>
<name>{esc_name}</name>"""
if forward in ["nat", "route", "bridge"]:
xml += f"""<forward mode='{esc_forward}'/>"""
if forward == "macvtap":
xml += f"""<forward mode='bridge'>
<interface dev='{esc_bridge}'/>
</forward>"""
else:
xml += """<bridge """
if forward in ["nat", "route", "none"]:
xml += """stp='on' delay='0'"""
if forward == "bridge":
xml += f"""name='{esc_bridge}'"""
xml += """/>"""
if openvswitch is True:
xml += """<virtualport type='openvswitch'/>"""
if forward not in ["bridge", "macvtap"]:
if ipv4:
xml += f"""<ip address='{esc_gateway}' netmask='{esc_mask}'>"""
if dhcp4:
dhcp4_0 = util.xml_escape(str(dhcp4[0]))
dhcp4_1 = util.xml_escape(str(dhcp4[1]))
xml += f"""<dhcp>
<range start='{dhcp4_0}' end='{dhcp4_1}' />
</dhcp>"""
xml += """</ip>"""
if ipv6:
xml += f"""<ip family='ipv6' address='{esc_gateway6}' prefix='{esc_prefix6}'>"""
if dhcp6:
dhcp6_0 = util.xml_escape(str(dhcp6[0]))
dhcp6_1 = util.xml_escape(str(dhcp6[1]))
xml += f"""<dhcp>
<range start='{dhcp6_0}' end='{dhcp6_1}' />"""
xml += """</dhcp>"""
xml += """</ip>"""
xml += """</network>"""
net = self.wvm.networkDefineXML(xml)
try:
net.create()
except libvirtError: # e.g. the subnet is in use: the name stays free for another try
net.undefine()
raise
net.setAutostart(1)
class wvmNetwork(wvmConnect):
"""A network of the host. A page reads its running definition once (tree);
a change starts from its saved definition read anew (config), so that two
changes in a row do not undo each other, and forgets what the page read."""
def __init__(self, host, login, passwd, conn, net):
wvmConnect.__init__(self, host, login, passwd, conn)
self.net = self.get_network(net)
@cached_property
def tree(self):
return etree.fromstring(self.net.XMLDesc(0))
def config(self):
return etree.fromstring(self.net.XMLDesc(VIR_NETWORK_XML_INACTIVE))
def config_xml(self):
"""The saved definition, the one the XML editor changes."""
return self.net.XMLDesc(VIR_NETWORK_XML_INACTIVE)
def _define(self, tree):
self.wvm.networkDefineXML(etree.tostring(tree).decode())
self.__dict__.pop("tree", None)
def get_name(self):
return self.net.name()
def get_autostart(self):
return self.net.autostart()
def set_autostart(self, value):
self.net.setAutostart(value)
def is_active(self):
return self.net.isActive()
def get_bridge_device(self):
"""Its bridge, or a macvtap network's interface."""
return self.tree.xpath("string(bridge/@name)") or self.tree.xpath("string(forward/interface/@dev)")
def start(self):
self.net.create()
def _vms_on_it(self, running_only):
name = self.net.name()
return self.domains_using(
lambda domain: domain.xpath("devices/interface[@type='network']/source[@network=$name]", name=name),
running_only,
)
def stop(self):
"""Refused while a running VM is on it: that VM would be cut off."""
if vms := self._vms_on_it(running_only=True):
raise util.OperationError(f"Network {self.net.name()} is in use by the running VMs {', '.join(vms)}; "
"shut them down or remove their network cards first")
self.net.destroy()
def delete(self):
"""Refused while a VM's definition names it: that VM would no longer start."""
if vms := self._vms_on_it(running_only=False):
raise util.OperationError(f"VMs {', '.join(vms)} use network {self.net.name()} and would no longer "
"start; remove their network cards first")
self.net.undefine()
def update(self, command, section, xml, parentIndex):
"""Changes the network's configuration and, while it runs, the running
network too (libvirt refuses a live change on a stopped one)."""
active = self.is_active()
if active and self._addresses(self.tree) != self._addresses(self.config()):
# parentIndex names the same <ip> in both only when they list the same addresses
raise util.OperationError(f"Network {self.net.name()} runs with other addresses than it has saved; "
"stop and start it first")
flags = VIR_NETWORK_UPDATE_AFFECT_CONFIG | (VIR_NETWORK_UPDATE_AFFECT_LIVE if active else 0)
self.net.update(command, section, parentIndex, xml, flags)
self.__dict__.pop("tree", None)
@staticmethod
def _addresses(tree):
return [(ip.get("family", "ipv4"), ip.get("address")) for ip in tree.findall("ip")]
def get_ip_networks(self):
return ip_networks(self.tree)
def get_network_mac(self):
return self.tree.xpath("string(mac/@address)")
def get_network_forward(self):
"""Its forward mode (None: isolated) and the device it forwards through."""
forward = self.tree.find("forward")
return [forward.get("mode"), forward.get("dev")] if forward is not None else [None, None]
def _ip(self, index):
"""The saved <ip> of that index, and what ip_networks tells of it."""
tree = self.config()
ips = tree.findall("ip")
if not 0 <= index < len(ips):
raise util.OperationError(f"Network {self.net.name()} has no such address")
return tree, ips[index], ip_networks(tree)[index]
def _checked_address(self, ip, address, what):
"""address as an address of the <ip> ip (ip_networks): of its family and
subnet, and not the host's own."""
try:
address = ipaddress.ip_address(address)
except ValueError:
raise util.OperationError(f"{address} is not an IP address") from None
if ip["subnet"] is None or address not in ip["subnet"]:
raise util.OperationError(f"The {what} {address} is not in {ip['subnet'] or 'the network'}")
if address == ipaddress.ip_address(ip["address"]):
raise util.OperationError(f"{address} is the host's own address on the network")
return address
def modify_dhcp_range(self, index, range_start, range_end):
"""Refused while the network runs: the running network would keep the old range."""
if self.is_active():
raise util.OperationError(f"Stop network {self.net.name()} to change its DHCP range")
tree, ip, info = self._ip(index)
dhcp_range = ip.find("dhcp/range")
if dhcp_range is None:
raise util.OperationError(f"This address of network {self.net.name()} has no DHCP range")
start = self._checked_address(info, range_start, "range start")
end = self._checked_address(info, range_end, "range end")
if start > end:
raise util.OperationError(f"The range starts at {start}, after its end {end}")
if start <= ipaddress.ip_address(info["address"]) <= end:
raise util.OperationError(f"The range takes in {info['address']}, the host's own address")
dhcp_range.set("start", str(start))
dhcp_range.set("end", str(end))
self._define(tree)
def delete_fixed_address(self, index, address):
_tree, ip, _info = self._ip(index)
host = next((h for h in ip.findall("dhcp/host") if h.get("ip") == address), None)
if host is None:
raise util.OperationError(f"Network {self.net.name()} has no fixed address {address}")
# the host as it is: a host may have only a name and an address
self.update(VIR_NETWORK_UPDATE_COMMAND_DELETE, VIR_NETWORK_SECTION_IP_DHCP_HOST,
etree.tostring(host, with_tail=False).decode(), index)
def modify_fixed_address(self, index, original, name, address, mac_duid):
"""Adds a fixed address, or changes the one that had the address
original, keeping what the page does not show of it (a client id, a
lease time). The address must be of the subnet, not the host's, and
neither it, the MAC (IPv4) or DUID (IPv6) nor the name another entry's.
A change deletes the entry and adds it anew, put back if that fails:
libvirt's own change finds an entry by any of its MAC, address or name,
another entry's too, and an IPv6 one never by its DUID. Returns False
when nothing changes."""
_tree, ip, info = self._ip(index)
address = self._checked_address(info, address, "fixed address")
key = "mac" if info["family"] == "ipv4" else "id"
hosts = ip.findall("dhcp/host")
old = None
if original:
old = next((h for h in hosts if h.get("ip") == original), None)
if old is None:
raise util.OperationError(f"The fixed address {original} is gone meanwhile; reload the page")
for other in hosts:
if other is old:
continue
if other.get("ip") == str(address):
raise util.OperationError(f"{address} is another entry's fixed address")
if mac_duid and other.get(key, "").lower() == mac_duid.lower():
raise util.OperationError(f"{mac_duid} has another fixed address, {other.get('ip')}")
if name and other.get("name") == name:
raise util.OperationError(f"The name {name} has another fixed address, {other.get('ip')}")
new = copy.deepcopy(old) if old is not None else etree.Element("host")
new.tail = None
for attribute, value in ((key, mac_duid), ("name", name), ("ip", str(address))):
if value:
new.set(attribute, value)
elif attribute in new.attrib:
del new.attrib[attribute]
new_xml = etree.tostring(new).decode()
if old is None:
self.update(VIR_NETWORK_UPDATE_COMMAND_ADD_LAST, VIR_NETWORK_SECTION_IP_DHCP_HOST, new_xml, index)
return True
old_xml = etree.tostring(old, with_tail=False).decode()
if new_xml == old_xml:
return False
self.update(VIR_NETWORK_UPDATE_COMMAND_DELETE, VIR_NETWORK_SECTION_IP_DHCP_HOST, old_xml, index)
try:
self.update(VIR_NETWORK_UPDATE_COMMAND_ADD_LAST, VIR_NETWORK_SECTION_IP_DHCP_HOST, new_xml, index)
except libvirtError:
self.update(VIR_NETWORK_UPDATE_COMMAND_ADD_LAST, VIR_NETWORK_SECTION_IP_DHCP_HOST, old_xml, index)
raise
return True
def get_qos(self):
qos_values = {}
for direction in ("inbound", "outbound"):
limit = self.tree.find(f"bandwidth/{direction}")
if limit is not None:
qos_values[direction] = {"average": limit.get("average"), "peak": limit.get("peak"),
"burst": limit.get("burst")}
return qos_values
def set_qos(self, direction, average, peak, burst):
if direction not in ("inbound", "outbound"):
raise ValueError("Direction must be inbound or outbound")
try:
average, peak, burst = int(average), int(peak), int(burst)
except ValueError:
raise ValueError("QoS rates must be whole numbers")
tree = self.config()
band = tree.find("bandwidth")
if band is None:
band = etree.SubElement(tree, "bandwidth")
for old in band.findall(direction):
band.remove(old)
etree.SubElement(band, direction, average=str(average), peak=str(peak), burst=str(burst))
self._define(tree)
def unset_qos(self, direction):
if direction not in ("inbound", "outbound"):
raise ValueError("Direction must be inbound or outbound")
tree = self.config()
for limit in tree.findall(f"bandwidth/{direction}"):
limit.getparent().remove(limit)
self._define(tree)
def edit_network(self, new_xml):
"""Defines this network anew from new_xml: of the same name and, if it
names one, the same UUID, not another network."""
# read for its name and UUID only, its entities not expanded: libvirt gets the text itself
parser = etree.XMLParser(resolve_entities=False, no_network=True)
try:
tree = etree.fromstring(new_xml.encode(), parser) # bytes: the XML may declare its encoding
except etree.XMLSyntaxError as err:
raise util.OperationError(f"The XML is not valid: {err}") from None
if tree.tag != "network" or tree.findtext("name") != self.net.name():
raise util.OperationError(f"The XML must define network {self.net.name()}")
if tree.findtext("uuid") not in (None, self.net.UUIDString()):
raise util.OperationError("The XML must keep the network's UUID")
self.wvm.networkDefineXML(new_xml)
self.__dict__.pop("tree", None)