daemon: refactored CoreInterface.mac from a string to a netaddr.EUI object, providing more functionality
This commit is contained in:
parent
cfda9509a2
commit
1829a8e2f8
8 changed files with 25 additions and 23 deletions
|
@ -460,7 +460,7 @@ def iface_to_proto(iface: CoreInterface) -> core_pb2.Interface:
|
|||
id=iface.node_id,
|
||||
net_id=net_id,
|
||||
name=iface.name,
|
||||
mac=iface.mac,
|
||||
mac=str(iface.mac),
|
||||
mtu=iface.mtu,
|
||||
flow_id=iface.flow_id,
|
||||
ip4=ip4,
|
||||
|
|
|
@ -750,7 +750,6 @@ class CoreNode(CoreNodeBase):
|
|||
:return: nothing
|
||||
:raises CoreCommandError: when a non-zero exit status occurs
|
||||
"""
|
||||
mac = utils.validate_mac(mac)
|
||||
iface = self.get_iface(iface_id)
|
||||
iface.set_mac(mac)
|
||||
if self.up:
|
||||
|
@ -1059,7 +1058,7 @@ class CoreNetworkBase(NodeBase):
|
|||
unidirectional = 1
|
||||
|
||||
iface2_data = InterfaceData(
|
||||
id=linked_node.get_iface_id(iface), name=iface.name, mac=iface.mac
|
||||
id=linked_node.get_iface_id(iface), name=iface.name, mac=str(iface.mac)
|
||||
)
|
||||
ip4 = iface.get_ip4()
|
||||
if ip4:
|
||||
|
|
|
@ -56,7 +56,7 @@ class CoreInterface:
|
|||
self._params: Dict[str, float] = {}
|
||||
self.ip4s: List[netaddr.IPNetwork] = []
|
||||
self.ip6s: List[netaddr.IPNetwork] = []
|
||||
self.mac: Optional[str] = None
|
||||
self.mac: Optional[netaddr.EUI] = None
|
||||
# placeholder position hook
|
||||
self.poshook: Callable[[CoreInterface], None] = lambda x: None
|
||||
# used with EMANE
|
||||
|
@ -149,8 +149,8 @@ class CoreInterface:
|
|||
self.ip4s.append(ip)
|
||||
else:
|
||||
self.ip6s.append(ip)
|
||||
except netaddr.AddrFormatError:
|
||||
raise CoreError(f"adding invalid address {ip}")
|
||||
except netaddr.AddrFormatError as e:
|
||||
raise CoreError(f"adding invalid address {ip}: {e}")
|
||||
|
||||
def remove_ip(self, ip: str) -> None:
|
||||
"""
|
||||
|
@ -167,8 +167,8 @@ class CoreInterface:
|
|||
self.ip4s.remove(ip)
|
||||
else:
|
||||
self.ip6s.remove(ip)
|
||||
except (netaddr.AddrFormatError, ValueError):
|
||||
raise CoreError(f"deleting invalid address {ip}")
|
||||
except (netaddr.AddrFormatError, ValueError) as e:
|
||||
raise CoreError(f"deleting invalid address {ip}: {e}")
|
||||
|
||||
def get_ip4(self) -> Optional[netaddr.IPNetwork]:
|
||||
"""
|
||||
|
@ -194,16 +194,21 @@ class CoreInterface:
|
|||
"""
|
||||
return self.ip4s + self.ip6s
|
||||
|
||||
def set_mac(self, mac: str) -> None:
|
||||
def set_mac(self, mac: Optional[str]) -> None:
|
||||
"""
|
||||
Set mac address.
|
||||
|
||||
:param mac: mac address to set
|
||||
:param mac: mac address to set, None for random mac
|
||||
:return: nothing
|
||||
:raises CoreError: when there is an invalid mac address
|
||||
"""
|
||||
if mac is not None:
|
||||
mac = utils.validate_mac(mac)
|
||||
self.mac = mac
|
||||
if mac is None:
|
||||
self.mac = mac
|
||||
else:
|
||||
try:
|
||||
self.mac = netaddr.EUI(mac, dialect=netaddr.mac_unix_expanded)
|
||||
except netaddr.AddrFormatError as e:
|
||||
raise CoreError(f"invalid mac address({mac}): {e}")
|
||||
|
||||
def getparam(self, key: str) -> float:
|
||||
"""
|
||||
|
|
|
@ -878,7 +878,7 @@ class PtpNet(CoreNetwork):
|
|||
unidirectional = 1
|
||||
|
||||
iface1_data = InterfaceData(
|
||||
id=iface1.node.get_iface_id(iface1), name=iface1.name, mac=iface1.mac
|
||||
id=iface1.node.get_iface_id(iface1), name=iface1.name, mac=str(iface1.mac)
|
||||
)
|
||||
ip4 = iface1.get_ip4()
|
||||
if ip4:
|
||||
|
@ -890,7 +890,7 @@ class PtpNet(CoreNetwork):
|
|||
iface1_data.ip6_mask = ip6.prefixlen
|
||||
|
||||
iface2_data = InterfaceData(
|
||||
id=iface2.node.get_iface_id(iface2), name=iface2.name, mac=iface2.mac
|
||||
id=iface2.node.get_iface_id(iface2), name=iface2.name, mac=str(iface2.mac)
|
||||
)
|
||||
ip4 = iface2.get_ip4()
|
||||
if ip4:
|
||||
|
|
|
@ -7,7 +7,6 @@ import os
|
|||
import threading
|
||||
from typing import IO, TYPE_CHECKING, List, Optional, Tuple
|
||||
|
||||
from core import utils
|
||||
from core.constants import MOUNT_BIN, UMOUNT_BIN
|
||||
from core.emulator.data import InterfaceData, LinkOptions
|
||||
from core.emulator.distributed import DistributedServer
|
||||
|
@ -74,7 +73,6 @@ class PhysicalNode(CoreNodeBase):
|
|||
:return: nothing
|
||||
:raises CoreCommandError: when a non-zero exit status occurs
|
||||
"""
|
||||
mac = utils.validate_mac(mac)
|
||||
iface = self.get_iface(iface_id)
|
||||
iface.set_mac(mac)
|
||||
if self.up:
|
||||
|
|
|
@ -69,7 +69,7 @@ class XorpRtrmgr(CoreService):
|
|||
"""
|
||||
helper for adding link-local address entries (required by OSPFv3)
|
||||
"""
|
||||
cfg = "\t address %s {\n" % netaddr.EUI(iface.mac).eui64()
|
||||
cfg = "\t address %s {\n" % iface.mac.eui64()
|
||||
cfg += "\t\tprefix-length: 64\n"
|
||||
cfg += "\t }\n"
|
||||
return cfg
|
||||
|
@ -292,7 +292,7 @@ class XorpRipng(XorpService):
|
|||
for iface in node.get_ifaces(control=False):
|
||||
cfg += "\tinterface %s {\n" % iface.name
|
||||
cfg += "\t vif %s {\n" % iface.name
|
||||
cfg += "\t\taddress %s {\n" % netaddr.EUI(iface.mac).eui64()
|
||||
cfg += "\t\taddress %s {\n" % iface.mac.eui64()
|
||||
cfg += "\t\t disable: false\n"
|
||||
cfg += "\t\t}\n"
|
||||
cfg += "\t }\n"
|
||||
|
|
|
@ -230,9 +230,9 @@ def build_node_platform_xml(
|
|||
platform_element.append(nem_element)
|
||||
|
||||
node.setnemid(iface, nem_id)
|
||||
macstr = _MAC_PREFIX + ":00:00:"
|
||||
macstr += f"{(nem_id >> 8) & 0xFF:02X}:{nem_id & 0xFF:02X}"
|
||||
iface.set_mac(macstr)
|
||||
mac = _MAC_PREFIX + ":00:00:"
|
||||
mac += f"{(nem_id >> 8) & 0xFF:02X}:{nem_id & 0xFF:02X}"
|
||||
iface.set_mac(mac)
|
||||
|
||||
# increment nem id
|
||||
nem_id += 1
|
||||
|
|
|
@ -61,7 +61,7 @@ class TestNodes:
|
|||
node.set_mac(iface.node_id, mac)
|
||||
|
||||
# then
|
||||
assert iface.mac == mac
|
||||
assert str(iface.mac) == mac
|
||||
|
||||
def test_node_set_mac_exception(self, session: Session):
|
||||
# given
|
||||
|
|
Loading…
Reference in a new issue