From b034ba6cc3e9b43096bfaa37c50c565280d7b13a Mon Sep 17 00:00:00 2001 From: Blake Harnden <32446120+bharnden@users.noreply.github.com> Date: Fri, 29 May 2020 22:48:00 -0700 Subject: [PATCH] turned transport type usages of raw and virtual across the board to an enumerated type --- daemon/core/emane/commeffect.py | 7 +++-- daemon/core/emane/emanemanager.py | 5 +-- daemon/core/emane/emanemodel.py | 8 ++--- daemon/core/emane/nodes.py | 10 ++++-- daemon/core/emulator/enumerations.py | 5 +++ daemon/core/nodes/interface.py | 46 +++++++++++++--------------- daemon/core/nodes/physical.py | 4 +-- daemon/core/xml/emanexml.py | 25 +++++++-------- 8 files changed, 61 insertions(+), 49 deletions(-) diff --git a/daemon/core/emane/commeffect.py b/daemon/core/emane/commeffect.py index f98f2454..99fdb9b1 100644 --- a/daemon/core/emane/commeffect.py +++ b/daemon/core/emane/commeffect.py @@ -11,6 +11,7 @@ from lxml import etree from core.config import ConfigGroup, Configuration from core.emane import emanemanifest, emanemodel from core.emane.nodes import EmaneNet +from core.emulator.enumerations import TransportType from core.nodes.interface import CoreInterface from core.xml import emanexml @@ -79,9 +80,9 @@ class EmaneCommEffectModel(emanemodel.EmaneModel): # create and write nem document nem_element = etree.Element("nem", name=f"{self.name} NEM", type="unstructured") - transport_type = "virtual" - if interface and interface.transport_type == "raw": - transport_type = "raw" + transport_type = TransportType.VIRTUAL + if interface and interface.transport_type == TransportType.RAW: + transport_type = TransportType.RAW transport_file = emanexml.transport_file_name(self.id, transport_type) etree.SubElement(nem_element, "transport", definition=transport_file) diff --git a/daemon/core/emane/emanemanager.py b/daemon/core/emane/emanemanager.py index d5c787f5..438fde00 100644 --- a/daemon/core/emane/emanemanager.py +++ b/daemon/core/emane/emanemanager.py @@ -30,6 +30,7 @@ from core.errors import CoreCommandError, CoreError from core.nodes.base import CoreNode, NodeBase from core.nodes.interface import CoreInterface from core.nodes.network import CtrlNet +from core.nodes.physical import Rj45Node from core.xml import emanexml if TYPE_CHECKING: @@ -596,7 +597,7 @@ class EmaneManager(ModelManager): run_emane_on_host = False for node in self.getnodes(): - if hasattr(node, "transport_type") and node.transport_type == "raw": + if isinstance(node, Rj45Node): run_emane_on_host = True continue path = self.session.session_dir @@ -655,7 +656,7 @@ class EmaneManager(ModelManager): kill_transortd = "killall -q emanetransportd" stop_emane_on_host = False for node in self.getnodes(): - if hasattr(node, "transport_type") and node.transport_type == "raw": + if isinstance(node, Rj45Node): stop_emane_on_host = True continue diff --git a/daemon/core/emane/emanemodel.py b/daemon/core/emane/emanemodel.py index 4104d3d5..3a21643b 100644 --- a/daemon/core/emane/emanemodel.py +++ b/daemon/core/emane/emanemodel.py @@ -8,7 +8,7 @@ from typing import Dict, List from core.config import ConfigGroup, Configuration from core.emane import emanemanifest from core.emane.nodes import EmaneNet -from core.emulator.enumerations import ConfigDataTypes +from core.emulator.enumerations import ConfigDataTypes, TransportType from core.errors import CoreError from core.location.mobility import WirelessModel from core.nodes.interface import CoreInterface @@ -111,9 +111,9 @@ class EmaneModel(WirelessModel): server = interface.node.server # check if this is external - transport_type = "virtual" - if interface and interface.transport_type == "raw": - transport_type = "raw" + transport_type = TransportType.VIRTUAL + if interface and interface.transport_type == TransportType.RAW: + transport_type = TransportType.RAW transport_name = emanexml.transport_file_name(self.id, transport_type) # create nem xml file diff --git a/daemon/core/emane/nodes.py b/daemon/core/emane/nodes.py index d8a58806..be398329 100644 --- a/daemon/core/emane/nodes.py +++ b/daemon/core/emane/nodes.py @@ -8,7 +8,13 @@ from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Type from core.emulator.data import LinkData from core.emulator.distributed import DistributedServer -from core.emulator.enumerations import LinkTypes, MessageFlags, NodeTypes, RegisterTlvs +from core.emulator.enumerations import ( + LinkTypes, + MessageFlags, + NodeTypes, + RegisterTlvs, + TransportType, +) from core.nodes.base import CoreNetworkBase from core.nodes.interface import CoreInterface @@ -173,7 +179,7 @@ class EmaneNet(CoreNetworkBase): emanetransportd terminates. """ for netif in self.netifs(): - if "virtual" in netif.transport_type.lower(): + if netif.transport_type == TransportType.VIRTUAL: netif.shutdown() netif.poshook = None diff --git a/daemon/core/emulator/enumerations.py b/daemon/core/emulator/enumerations.py index 2c6e14db..f210c992 100644 --- a/daemon/core/emulator/enumerations.py +++ b/daemon/core/emulator/enumerations.py @@ -117,3 +117,8 @@ class ExceptionLevels(Enum): ERROR = 2 WARNING = 3 NOTICE = 4 + + +class TransportType(Enum): + RAW = "raw" + VIRTUAL = "virtual" diff --git a/daemon/core/nodes/interface.py b/daemon/core/nodes/interface.py index 97b494b7..16c242e9 100644 --- a/daemon/core/nodes/interface.py +++ b/daemon/core/nodes/interface.py @@ -4,12 +4,12 @@ virtual ethernet classes that implement the interfaces available under Linux. import logging import time -from typing import TYPE_CHECKING, Callable, Dict, List, Tuple +from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Tuple from core import utils -from core.emulator.enumerations import MessageFlags +from core.emulator.enumerations import MessageFlags, TransportType from core.errors import CoreCommandError -from core.nodes.netclient import get_net_client +from core.nodes.netclient import LinuxNetClient, get_net_client if TYPE_CHECKING: from core.emulator.distributed import DistributedServer @@ -42,32 +42,30 @@ class CoreInterface: :param server: remote server node will run on, default is None for localhost """ - self.session = session - self.node = node - self.name = name - self.localname = localname - self.up = False - if not isinstance(mtu, int): - raise ValueError - self.mtu = mtu - self.net = None - self.othernet = None + self.session: "Session" = session + self.node: "CoreNode" = node + self.name: str = name + self.localname: str = localname + self.up: bool = False + self.mtu: int = mtu + self.net: Optional[CoreNetworkBase] = None + self.othernet: Optional[CoreNetworkBase] = None self._params = {} - self.addrlist = [] - self.hwaddr = None + self.addrlist: List[str] = [] + self.hwaddr: Optional[str] = None # placeholder position hook - self.poshook = lambda x: None + self.poshook: Callable[[CoreInterface], None] = lambda x: None # used with EMANE - self.transport_type = None + self.transport_type: Optional[TransportType] = None # node interface index - self.netindex = None + self.netindex: Optional[int] = None # net interface index - self.netifi = None + self.netifi: Optional[int] = None # index used to find flow data - self.flow_id = None - self.server = server + self.flow_id: Optional[int] = None + self.server: Optional["DistributedServer"] = server use_ovs = session.options.get_config("ovs") == "True" - self.net_client = get_net_client(use_ovs, self.host_cmd) + self.net_client: LinuxNetClient = get_net_client(use_ovs, self.host_cmd) def host_cmd( self, @@ -330,7 +328,7 @@ class TunTap(CoreInterface): :param start: start flag """ super().__init__(session, node, name, localname, mtu, server) - self.transport_type = "virtual" + self.transport_type = TransportType.VIRTUAL if start: self.startup() @@ -516,7 +514,7 @@ class GreTap(CoreInterface): sessionid = session.short_session_id() localname = f"gt.{self.id}.{sessionid}" super().__init__(session, node, name, localname, mtu, server) - self.transport_type = "raw" + self.transport_type = TransportType.RAW if not start: return if remoteip is None: diff --git a/daemon/core/nodes/physical.py b/daemon/core/nodes/physical.py index 2fc743fa..e5db8a80 100644 --- a/daemon/core/nodes/physical.py +++ b/daemon/core/nodes/physical.py @@ -10,7 +10,7 @@ from typing import IO, TYPE_CHECKING, List, Optional, Tuple from core import utils from core.constants import MOUNT_BIN, UMOUNT_BIN from core.emulator.distributed import DistributedServer -from core.emulator.enumerations import NodeTypes +from core.emulator.enumerations import NodeTypes, TransportType from core.errors import CoreCommandError, CoreError from core.nodes.base import CoreNetworkBase, CoreNodeBase from core.nodes.interface import CoreInterface, Veth @@ -281,7 +281,7 @@ class Rj45Node(CoreNodeBase): """ super().__init__(session, _id, name, start, server) self.interface = CoreInterface(session, self, name, name, mtu, server) - self.interface.transport_type = "raw" + self.interface.transport_type = TransportType.RAW self.lock: threading.RLock = threading.RLock() self.ifindex: Optional[int] = None self.old_up: bool = False diff --git a/daemon/core/xml/emanexml.py b/daemon/core/xml/emanexml.py index 2c5cc9c0..2589edd9 100644 --- a/daemon/core/xml/emanexml.py +++ b/daemon/core/xml/emanexml.py @@ -9,6 +9,7 @@ from core import utils from core.config import Configuration from core.emane.nodes import EmaneNet from core.emulator.distributed import DistributedServer +from core.emulator.enumerations import TransportType from core.nodes.interface import CoreInterface from core.nodes.network import CtrlNet from core.xml import corexml @@ -182,7 +183,7 @@ def build_node_platform_xml( transport_type = netif.transport_type if not transport_type: logging.info("warning: %s interface type unsupported!", netif.name) - transport_type = "raw" + transport_type = TransportType.RAW transport_file = transport_file_name(node.id, transport_type) transport_element = etree.SubElement( nem_element, "transport", definition=transport_file @@ -196,7 +197,7 @@ def build_node_platform_xml( # merging code key = netif.node.id - if netif.transport_type == "raw": + if netif.transport_type == TransportType.RAW: key = "host" otadev = control_net.brname eventdev = control_net.brname @@ -276,8 +277,8 @@ def build_xml_files(emane_manager: "EmaneManager", node: EmaneNet) -> None: # build XML for specific interface (NEM) configs need_virtual = False need_raw = False - vtype = "virtual" - rtype = "raw" + vtype = TransportType.VIRTUAL + rtype = TransportType.RAW for netif in node.netifs(): # check for interface specific emane configuration and write xml files @@ -286,7 +287,7 @@ def build_xml_files(emane_manager: "EmaneManager", node: EmaneNet) -> None: node.model.build_xml_files(config, netif) # check transport type needed for interface - if "virtual" in netif.transport_type: + if netif.transport_type == TransportType.VIRTUAL: need_virtual = True vtype = netif.transport_type else: @@ -301,7 +302,7 @@ def build_xml_files(emane_manager: "EmaneManager", node: EmaneNet) -> None: def build_transport_xml( - emane_manager: "EmaneManager", node: EmaneNet, transport_type: str + emane_manager: "EmaneManager", node: EmaneNet, transport_type: TransportType ) -> None: """ Build transport xml file for node and transport type. @@ -314,8 +315,8 @@ def build_transport_xml( """ transport_element = etree.Element( "transport", - name=f"{transport_type.capitalize()} Transport", - library=f"trans{transport_type.lower()}", + name=f"{transport_type.value.capitalize()} Transport", + library=f"trans{transport_type.value.lower()}", ) # add bitrate @@ -325,7 +326,7 @@ def build_transport_xml( config = emane_manager.get_configs(node.id, node.model.name) flowcontrol = config.get("flowcontrolenable", "0") == "1" - if "virtual" in transport_type.lower(): + if transport_type == TransportType.VIRTUAL: device_path = "/dev/net/tun_flowctl" if not os.path.exists(device_path): device_path = "/dev/net/tun" @@ -482,7 +483,7 @@ def create_event_service_xml( create_file(event_element, "emaneeventmsgsvc", file_path, server) -def transport_file_name(node_id: int, transport_type: str) -> str: +def transport_file_name(node_id: int, transport_type: TransportType) -> str: """ Create name for a transport xml file. @@ -490,7 +491,7 @@ def transport_file_name(node_id: int, transport_type: str) -> str: :param transport_type: transport type to generate transport file :return: """ - return f"n{node_id}trans{transport_type}.xml" + return f"n{node_id}trans{transport_type.value}.xml" def _basename(emane_model: "EmaneModel", interface: CoreInterface = None) -> str: @@ -521,7 +522,7 @@ def nem_file_name(emane_model: "EmaneModel", interface: CoreInterface = None) -> """ basename = _basename(emane_model, interface) append = "" - if interface and interface.transport_type == "raw": + if interface and interface.transport_type == TransportType.RAW: append = "_raw" return f"{basename}nem{append}.xml"