diff --git a/daemon/core/api/grpc/client.py b/daemon/core/api/grpc/client.py index 91c4b679..a51f3724 100644 --- a/daemon/core/api/grpc/client.py +++ b/daemon/core/api/grpc/client.py @@ -9,8 +9,8 @@ from contextlib import contextmanager import grpc import netaddr +from core import utils from core.api.grpc import core_pb2, core_pb2_grpc -from core.nodes.ipaddress import MacAddress class InterfaceHelper: @@ -88,7 +88,7 @@ class InterfaceHelper: # random mac if not mac: - mac = MacAddress.random() + mac = utils.random_mac() return core_pb2.Interface( id=interface_id, diff --git a/daemon/core/api/grpc/grpcutils.py b/daemon/core/api/grpc/grpcutils.py index a3b25541..5468e617 100644 --- a/daemon/core/api/grpc/grpcutils.py +++ b/daemon/core/api/grpc/grpcutils.py @@ -6,7 +6,6 @@ from core.api.grpc import core_pb2 from core.emulator.emudata import InterfaceData, LinkOptions, NodeOptions from core.emulator.enumerations import LinkTypes, NodeTypes from core.nodes.base import CoreNetworkBase -from core.nodes.ipaddress import MacAddress WORKERS = 10 @@ -57,8 +56,6 @@ def link_interface(interface_proto): mac = interface_proto.mac if mac == "": mac = None - else: - mac = MacAddress.from_string(mac) interface = InterfaceData( _id=interface_proto.id, name=name, diff --git a/daemon/core/api/tlv/coreapi.py b/daemon/core/api/tlv/coreapi.py index 9851bb55..b72c186b 100644 --- a/daemon/core/api/tlv/coreapi.py +++ b/daemon/core/api/tlv/coreapi.py @@ -5,10 +5,13 @@ types and objects used for parsing and building CORE API messages. CORE API messaging is leveraged for communication with the GUI. """ +import binascii import socket import struct from enum import Enum +import netaddr + from core.api.tlv import structutils from core.emulator.enumerations import ( ConfigTlvs, @@ -24,7 +27,6 @@ from core.emulator.enumerations import ( RegisterTlvs, SessionTlvs, ) -from core.nodes.ipaddress import MacAddress class CoreTlvData: @@ -323,7 +325,7 @@ class CoreTlvDataMacAddr(CoreTlvDataObj): """ data_format = "!2x8s" - data_type = MacAddress.from_string + data_type = str pad_len = 2 @staticmethod @@ -331,23 +333,27 @@ class CoreTlvDataMacAddr(CoreTlvDataObj): """ Retrieve Ipv6 address value from object. - :param core.nodes.ipaddress.MacAddress obj: mac address to get value from - :return: + :param str obj: mac address to get value from + :return: packed mac address + :rtype: bytes """ # extend to 64 bits - return b"\0\0" + obj.addr + return b"\0\0" + netaddr.EUI(obj).packed @staticmethod def new_obj(value): """ Retrieve mac address from a string representation. - :param str value: value to get Ipv4 address from - :return: Ipv4 address - :rtype: core.nodes.ipaddress.MacAddress + :param bytes value: value to get Ipv4 address from + :return: mac address + :rtype: str """ # only use 48 bits - return MacAddress(address=value[2:]) + value = binascii.hexlify(value[2:]).decode() + mac = netaddr.EUI(value) + mac.dialect = netaddr.mac_unix + return str(mac) class CoreTlv: diff --git a/daemon/core/emulator/emudata.py b/daemon/core/emulator/emudata.py index 54f7921d..8929f72a 100644 --- a/daemon/core/emulator/emudata.py +++ b/daemon/core/emulator/emudata.py @@ -1,8 +1,8 @@ import netaddr +from core import utils from core.emane.nodes import EmaneNet from core.emulator.enumerations import LinkTypes -from core.nodes.ipaddress import MacAddress from core.nodes.physical import PhysicalNode @@ -226,7 +226,7 @@ class IpPrefixes: # random mac if not mac: - mac = MacAddress.random() + mac = utils.random_mac() return InterfaceData( _id=inteface_id, @@ -250,7 +250,7 @@ class InterfaceData: :param int _id: interface id :param str name: name for interface - :param core.nodes.ipaddress.MacAddress mac: mac address + :param str mac: mac address :param str ip4: ipv4 address :param int ip4_mask: ipv4 bit mask :param str ip6: ipv6 address diff --git a/daemon/core/emulator/session.py b/daemon/core/emulator/session.py index f9d5b9fc..a81ba103 100644 --- a/daemon/core/emulator/session.py +++ b/daemon/core/emulator/session.py @@ -33,7 +33,6 @@ from core.location.event import EventLoop from core.location.mobility import BasicRangeModel, MobilityManager from core.nodes.base import CoreNetworkBase, CoreNode, CoreNodeBase from core.nodes.docker import DockerNode -from core.nodes.ipaddress import MacAddress from core.nodes.lxd import LxcNode from core.nodes.network import ( CtrlNet, @@ -1778,7 +1777,7 @@ class Session: net=control_net, ifindex=control_net.CTRLIF_IDX_BASE + net_index, ifname=f"ctrl{net_index}", - hwaddr=MacAddress.random(), + hwaddr=utils.random_mac(), addrlist=addrlist, ) node.netif(interface1).control = True diff --git a/daemon/core/nodes/base.py b/daemon/core/nodes/base.py index 93a71ee4..c3c1524d 100644 --- a/daemon/core/nodes/base.py +++ b/daemon/core/nodes/base.py @@ -725,14 +725,14 @@ class CoreNode(CoreNodeBase): Set hardware addres for an interface. :param int ifindex: index of interface to set hardware address for - :param core.nodes.ipaddress.MacAddress addr: hardware address to set + :param str addr: hardware address to set :return: nothing :raises CoreCommandError: when a non-zero exit status occurs """ interface = self._netif[ifindex] interface.sethwaddr(addr) if self.up: - self.node_net_client.device_mac(interface.name, str(addr)) + self.node_net_client.device_mac(interface.name, addr) def addaddr(self, ifindex, addr): """ @@ -787,7 +787,7 @@ class CoreNode(CoreNodeBase): :param core.nodes.base.CoreNetworkBase net: network to associate with :param list addrlist: addresses to add on the interface - :param core.nodes.ipaddress.MacAddress hwaddr: hardware address to set for interface + :param str hwaddr: hardware address to set for interface :param int ifindex: index of interface to create :param str ifname: name for interface :return: interface index diff --git a/daemon/core/nodes/interface.py b/daemon/core/nodes/interface.py index 84e8f399..884ce4e9 100644 --- a/daemon/core/nodes/interface.py +++ b/daemon/core/nodes/interface.py @@ -130,7 +130,7 @@ class CoreInterface: """ Set hardware address. - :param core.nodes.ipaddress.MacAddress addr: hardware address to set to. + :param str addr: hardware address to set to. :return: nothing """ self.hwaddr = addr diff --git a/daemon/core/nodes/ipaddress.py b/daemon/core/nodes/ipaddress.py deleted file mode 100644 index ea219f47..00000000 --- a/daemon/core/nodes/ipaddress.py +++ /dev/null @@ -1,55 +0,0 @@ -""" -Helper objects for dealing with IPv4/v6 addresses. -""" - -import random -import struct - - -class MacAddress: - """ - Provides mac address utilities for use within core. - """ - - def __init__(self, address): - """ - Creates a MacAddress instance. - - :param bytes address: mac address - """ - self.addr = address - - def __str__(self): - """ - Create a string representation of a MacAddress. - - :return: string representation - :rtype: str - """ - return ":".join(f"{x:02x}" for x in bytearray(self.addr)) - - @classmethod - def from_string(cls, s): - """ - Create a mac address object from a string. - - :param s: string representation of a mac address - :return: mac address class - :rtype: MacAddress - """ - addr = b"".join(bytes([int(x, 16)]) for x in s.split(":")) - return cls(addr) - - @classmethod - def random(cls): - """ - Create a random mac address. - - :return: random mac address - :rtype: MacAddress - """ - tmp = random.randint(0, 0xFFFFFF) - # use the Xen OID 00:16:3E - tmp |= 0x00163E << 24 - tmpbytes = struct.pack("!Q", tmp) - return cls(tmpbytes[2:]) diff --git a/daemon/core/utils.py b/daemon/core/utils.py index 413df156..13d75887 100644 --- a/daemon/core/utils.py +++ b/daemon/core/utils.py @@ -11,10 +11,13 @@ import json import logging import logging.config import os +import random import shlex import sys from subprocess import PIPE, STDOUT, Popen +import netaddr + from core.errors import CoreCommandError DEVNULL = open(os.devnull, "wb") @@ -408,3 +411,17 @@ def threadpool(funcs, workers=10): except Exception as e: exceptions.append(e) return results, exceptions + + +def random_mac(): + """ + Create a random mac address using Xen OID 00:16:3E. + + :return: random mac address + :rtype: str + """ + value = random.randint(0, 0xFFFFFF) + value |= 0x00163E << 24 + mac = netaddr.EUI(value) + mac.dialect = netaddr.mac_unix + return str(mac) diff --git a/daemon/core/xml/corexml.py b/daemon/core/xml/corexml.py index db6d8bd7..ff64d2ce 100644 --- a/daemon/core/xml/corexml.py +++ b/daemon/core/xml/corexml.py @@ -8,7 +8,6 @@ from core.emane.nodes import EmaneNet from core.emulator.emudata import InterfaceData, LinkOptions, NodeOptions from core.emulator.enumerations import NodeTypes from core.nodes.base import CoreNetworkBase -from core.nodes.ipaddress import MacAddress from core.nodes.network import CtrlNet @@ -48,8 +47,6 @@ def create_interface_data(interface_element): interface_id = int(interface_element.get("id")) name = interface_element.get("name") mac = interface_element.get("mac") - if mac: - mac = MacAddress.from_string(mac) ip4 = interface_element.get("ip4") ip4_mask = get_int(interface_element, "ip4_mask") ip6 = interface_element.get("ip6") diff --git a/daemon/core/xml/emanexml.py b/daemon/core/xml/emanexml.py index c97c176f..a62b54e5 100644 --- a/daemon/core/xml/emanexml.py +++ b/daemon/core/xml/emanexml.py @@ -5,7 +5,6 @@ from tempfile import NamedTemporaryFile from lxml import etree from core import utils -from core.nodes.ipaddress import MacAddress from core.xml import corexml _hwaddr_prefix = "02:02" @@ -208,7 +207,7 @@ def build_node_platform_xml(emane_manager, control_net, node, nem_id, platform_x node.setnemid(netif, nem_id) macstr = _hwaddr_prefix + ":00:00:" macstr += f"{(nem_id >> 8) & 0xFF:02X}:{nem_id & 0xFF:02X}" - netif.sethwaddr(MacAddress.from_string(macstr)) + netif.sethwaddr(macstr) # increment nem id nem_id += 1