2019-11-04 09:03:13 +00:00
|
|
|
from xml.etree import ElementTree
|
2020-11-05 09:34:31 +00:00
|
|
|
|
2015-02-27 08:53:51 +00:00
|
|
|
from libvirt import VIR_INTERFACE_XML_INACTIVE
|
2022-11-02 13:05:41 +00:00
|
|
|
|
2020-04-24 16:34:29 +00:00
|
|
|
from vrtManager import util
|
2020-11-05 09:34:31 +00:00
|
|
|
from vrtManager.connection import wvmConnect
|
2015-02-27 08:53:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
class wvmInterfaces(wvmConnect):
|
|
|
|
def define_iface(self, xml, flag=0):
|
|
|
|
self.wvm.interfaceDefineXML(xml, flag)
|
|
|
|
|
2020-11-05 09:34:31 +00:00
|
|
|
def create_iface(
|
2022-11-02 13:05:41 +00:00
|
|
|
self,
|
|
|
|
name,
|
|
|
|
itype,
|
|
|
|
mode,
|
|
|
|
netdev,
|
|
|
|
ipv4_type,
|
|
|
|
ipv4_addr,
|
|
|
|
ipv4_gw,
|
|
|
|
ipv6_type,
|
|
|
|
ipv6_addr,
|
|
|
|
ipv6_gw,
|
|
|
|
stp,
|
|
|
|
delay,
|
2020-11-05 09:34:31 +00:00
|
|
|
):
|
2020-05-21 13:17:25 +00:00
|
|
|
xml = f"""<interface type='{itype}' name='{name}'>
|
|
|
|
<start mode='{mode}'/>"""
|
2020-11-05 09:34:31 +00:00
|
|
|
if ipv4_type == "dhcp":
|
2015-02-27 08:53:51 +00:00
|
|
|
xml += """<protocol family='ipv4'>
|
|
|
|
<dhcp/>
|
|
|
|
</protocol>"""
|
2020-11-05 09:34:31 +00:00
|
|
|
if ipv4_type == "static":
|
|
|
|
address, prefix = ipv4_addr.split("/")
|
2020-05-21 13:17:25 +00:00
|
|
|
xml += f"""<protocol family='ipv4'>
|
|
|
|
<ip address='{address}' prefix='{prefix}'/>
|
|
|
|
<route gateway='{ipv4_gw}'/>
|
|
|
|
</protocol>"""
|
2020-11-05 09:34:31 +00:00
|
|
|
if ipv6_type == "dhcp":
|
2015-02-27 08:53:51 +00:00
|
|
|
xml += """<protocol family='ipv6'>
|
|
|
|
<dhcp/>
|
|
|
|
</protocol>"""
|
2020-11-05 09:34:31 +00:00
|
|
|
if ipv6_type == "static":
|
|
|
|
address, prefix = ipv6_addr.split("/")
|
2020-05-21 13:17:25 +00:00
|
|
|
xml += f"""<protocol family='ipv6'>
|
|
|
|
<ip address='{address}' prefix='{prefix}'/>
|
|
|
|
<route gateway='{ipv6_gw}'/>
|
|
|
|
</protocol>"""
|
2020-11-05 09:34:31 +00:00
|
|
|
if itype == "bridge":
|
2020-05-21 13:17:25 +00:00
|
|
|
xml += f"""<bridge stp='{stp}' delay='{delay}'>
|
|
|
|
<interface name='{netdev}' type='ethernet'/>
|
|
|
|
</bridge>"""
|
2015-02-27 08:53:51 +00:00
|
|
|
xml += """</interface>"""
|
|
|
|
self.define_iface(xml)
|
|
|
|
iface = self.get_iface(name)
|
|
|
|
iface.create()
|
|
|
|
|
|
|
|
|
|
|
|
class wvmInterface(wvmConnect):
|
|
|
|
def __init__(self, host, login, passwd, conn, iface):
|
|
|
|
wvmConnect.__init__(self, host, login, passwd, conn)
|
|
|
|
self.iface = self.get_iface(iface)
|
|
|
|
|
|
|
|
def _XMLDesc(self, flags=0):
|
|
|
|
return self.iface.XMLDesc(flags)
|
|
|
|
|
|
|
|
def get_start_mode(self):
|
|
|
|
try:
|
|
|
|
xml = self._XMLDesc(VIR_INTERFACE_XML_INACTIVE)
|
|
|
|
return util.get_xml_path(xml, "/interface/start/@mode")
|
2022-08-22 12:12:33 +00:00
|
|
|
except Exception:
|
2015-02-27 08:53:51 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
def is_active(self):
|
|
|
|
return self.iface.isActive()
|
|
|
|
|
|
|
|
def get_mac(self):
|
|
|
|
mac = self.iface.MACString()
|
2022-08-22 12:12:33 +00:00
|
|
|
return mac or None
|
2015-02-27 08:53:51 +00:00
|
|
|
|
|
|
|
def get_type(self):
|
|
|
|
xml = self._XMLDesc()
|
|
|
|
return util.get_xml_path(xml, "/interface/@type")
|
|
|
|
|
|
|
|
def get_ipv4_type(self):
|
|
|
|
try:
|
|
|
|
xml = self._XMLDesc(VIR_INTERFACE_XML_INACTIVE)
|
2022-11-02 13:05:41 +00:00
|
|
|
ipaddr = util.get_xml_path(
|
|
|
|
xml, "/interface/protocol[@family='ipv4']/ip/@address"
|
|
|
|
)
|
2022-08-22 12:12:33 +00:00
|
|
|
return "static" if ipaddr else "dhcp"
|
|
|
|
except Exception:
|
2015-02-27 08:53:51 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
def get_ipv4(self):
|
|
|
|
xml = self._XMLDesc()
|
2022-11-02 13:05:41 +00:00
|
|
|
int_ipv4_ip = util.get_xml_path(
|
|
|
|
xml, "/interface/protocol[@family='ipv4']/ip/@address"
|
|
|
|
)
|
|
|
|
int_ipv4_mask = util.get_xml_path(
|
|
|
|
xml, "/interface/protocol[@family='ipv4']/ip/@prefix"
|
|
|
|
)
|
2015-02-27 08:53:51 +00:00
|
|
|
if not int_ipv4_ip or not int_ipv4_mask:
|
|
|
|
return None
|
|
|
|
else:
|
2022-08-22 12:12:33 +00:00
|
|
|
return f"{int_ipv4_ip}/{int_ipv4_mask}"
|
2015-02-27 08:53:51 +00:00
|
|
|
|
|
|
|
def get_ipv6_type(self):
|
|
|
|
try:
|
|
|
|
xml = self._XMLDesc(VIR_INTERFACE_XML_INACTIVE)
|
2022-11-02 13:05:41 +00:00
|
|
|
ipaddr = util.get_xml_path(
|
|
|
|
xml, "/interface/protocol[@family='ipv6']/ip/@address"
|
|
|
|
)
|
2022-08-22 12:12:33 +00:00
|
|
|
return "static" if ipaddr else "dhcp"
|
|
|
|
except Exception:
|
2015-02-27 08:53:51 +00:00
|
|
|
return None
|
|
|
|
|
|
|
|
def get_ipv6(self):
|
|
|
|
xml = self._XMLDesc()
|
2022-11-02 13:05:41 +00:00
|
|
|
int_ipv6_ip = util.get_xml_path(
|
|
|
|
xml, "/interface/protocol[@family='ipv6']/ip/@address"
|
|
|
|
)
|
|
|
|
int_ipv6_mask = util.get_xml_path(
|
|
|
|
xml, "/interface/protocol[@family='ipv6']/ip/@prefix"
|
|
|
|
)
|
2015-02-27 08:53:51 +00:00
|
|
|
if not int_ipv6_ip or not int_ipv6_mask:
|
|
|
|
return None
|
|
|
|
else:
|
2022-08-22 12:12:33 +00:00
|
|
|
return f"{int_ipv6_ip}/{int_ipv6_mask}"
|
2015-02-27 08:53:51 +00:00
|
|
|
|
|
|
|
def get_bridge(self):
|
2019-11-04 09:03:13 +00:00
|
|
|
bridge = None
|
2022-08-22 12:12:33 +00:00
|
|
|
if self.get_type() != "bridge":
|
2019-11-04 09:03:13 +00:00
|
|
|
return None
|
2022-08-22 12:12:33 +00:00
|
|
|
bridge = util.get_xml_path(self._XMLDesc(), "/interface/bridge/interface/@name")
|
|
|
|
for iface in self.get_bridge_slave_ifaces():
|
|
|
|
if iface.get("state") == "up" and iface.get("speed") != "unknown":
|
|
|
|
bridge = iface.get("name")
|
|
|
|
return bridge
|
|
|
|
return bridge
|
2019-11-04 09:03:13 +00:00
|
|
|
|
|
|
|
def get_bridge_slave_ifaces(self):
|
2022-08-22 12:12:33 +00:00
|
|
|
if self.get_type() != "bridge":
|
2015-02-27 08:53:51 +00:00
|
|
|
return None
|
|
|
|
|
2022-08-22 12:12:33 +00:00
|
|
|
ifaces = []
|
|
|
|
tree = ElementTree.fromstring(self._XMLDesc())
|
|
|
|
for iface in tree.findall("./bridge/"):
|
|
|
|
address = state = speed = None
|
|
|
|
name = iface.get("name")
|
|
|
|
if_type = iface.get("type")
|
|
|
|
link = iface.find("link")
|
|
|
|
if link is not None:
|
|
|
|
state = link.get("state")
|
|
|
|
speed = link.get("speed")
|
|
|
|
mac = iface.find("mac")
|
|
|
|
if mac is not None:
|
|
|
|
address = mac.get("address")
|
2022-11-02 13:05:41 +00:00
|
|
|
ifaces.append(
|
|
|
|
{
|
|
|
|
"name": name,
|
|
|
|
"type": if_type,
|
|
|
|
"state": state,
|
|
|
|
"speed": speed,
|
|
|
|
"mac": address,
|
|
|
|
}
|
|
|
|
)
|
2022-08-22 12:12:33 +00:00
|
|
|
return ifaces
|
|
|
|
|
2021-02-12 13:05:16 +00:00
|
|
|
def get_details(self):
|
|
|
|
mac = self.get_mac()
|
|
|
|
itype = self.get_type()
|
|
|
|
state = self.is_active()
|
2021-03-30 12:43:58 +00:00
|
|
|
return {"name": self.iface.name(), "type": itype, "state": state, "mac": mac}
|
2021-02-12 13:05:16 +00:00
|
|
|
|
2015-02-27 08:53:51 +00:00
|
|
|
def stop_iface(self):
|
|
|
|
self.iface.destroy()
|
|
|
|
|
|
|
|
def start_iface(self):
|
|
|
|
self.iface.create()
|
|
|
|
|
|
|
|
def delete_iface(self):
|
|
|
|
self.iface.undefine()
|