Ejemplo n.º 1
0
def ping(from_node: CoreNode, to_node: CoreNode, ip_prefixes: IpPrefixes):
    address = ip_prefixes.ip4_address(to_node.id)
    try:
        from_node.cmd(f"ping -c 1 {address}")
        status = 0
    except CoreCommandError as e:
        status = e.returncode
    return status
Ejemplo n.º 2
0
def main():
    # ip generator for example
    prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16")

    # create emulator instance for creating sessions and utility methods
    coreemu = CoreEmu()
    session = coreemu.create_session()

    # must be in configuration state for nodes to start, when using "node_add" below
    session.set_state(EventTypes.CONFIGURATION_STATE)

    # create emane network node, emane determines connectivity based on
    # location, so the session and nodes must be configured to provide one
    session.set_location(47.57917, -122.13232, 2.00000, 1.0)
    options = NodeOptions()
    options.set_position(80, 50)
    emane_network = session.add_node(EmaneNet, options=options, _id=100)
    session.emane.set_model(emane_network, EmaneIeee80211abgModel)

    # create nodes
    options = NodeOptions(model="mdr")
    for i in range(NODES):
        node = session.add_node(CoreNode, options=options)
        node.setposition(x=150 * (i + 1), y=150)
        interface = prefixes.create_iface(node)
        session.add_link(node.id, emane_network.id, iface1_data=interface)

    # instantiate session
    session.instantiate()

    # OSPF MDR requires some time for routes to be created
    logging.info("waiting %s seconds for OSPF MDR to create routes",
                 EMANE_DELAY)
    time.sleep(EMANE_DELAY)

    # get nodes to run example
    first_node = session.get_node(1, CoreNode)
    last_node = session.get_node(NODES, CoreNode)
    address = prefixes.ip4_address(first_node.id)
    logging.info("node %s pinging %s", last_node.name, address)
    output = last_node.cmd(f"ping -c 3 {address}")
    logging.info(output)

    # shutdown session
    coreemu.shutdown()
Ejemplo n.º 3
0
Archivo: wlan.py Proyecto: umr-ds/core
def main():
    # ip generator for example
    prefixes = IpPrefixes("10.83.0.0/16")

    # create emulator instance for creating sessions and utility methods
    coreemu = CoreEmu()
    session = coreemu.create_session()

    # must be in configuration state for nodes to start, when using "node_add" below
    session.set_state(EventTypes.CONFIGURATION_STATE)

    # create wlan network node
    wlan = session.add_node(WlanNode, _id=100)
    session.mobility.set_model(wlan, BasicRangeModel)

    # create nodes, must set a position for wlan basic range model
    options = NodeOptions(model="mdr")
    options.set_position(0, 0)
    for _ in range(NODES):
        node = session.add_node(CoreNode, options=options)
        interface = prefixes.create_iface(node)
        session.add_link(node.id, wlan.id, iface1_data=interface)

    # instantiate session
    session.instantiate()

    # get nodes for example run
    first_node = session.get_node(1, CoreNode)
    last_node = session.get_node(NODES, CoreNode)
    address = prefixes.ip4_address(first_node.id)
    logging.info("node %s pinging %s", last_node.name, address)
    output = last_node.cmd(f"ping -c 3 {address}")
    logging.info(output)

    # shutdown session
    coreemu.shutdown()
Ejemplo n.º 4
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('--is_virtual',
                        dest='is_virtual',
                        help='with Kronos',
                        default="False")

    args = parser.parse_args()

    if args.is_virtual == "True":
        is_virtual = True
    else:
        is_virtual = False

    # Kronos specific parameters
    total_num_dilated_executables = 6  # (2 plcs + 2 communication modules + 2 hmis )
    run_time_secs = 5
    rel_cpu_speed = 1.0
    num_insns_per_round = 1000000

    plc_spec_directory = os.path.dirname(os.path.realpath(__file__))
    plc1_spec_file = f"{plc_spec_directory}/plc1_system_specification.prototxt"
    plc2_spec_file = f"{plc_spec_directory}/plc2_system_specification.prototxt"

    NUM_PLCS = 2
    NUM_HMIS = 2

    # ip generator for example
    prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16")

    # create emulator instance for creating sessions and utility methods
    coreemu = CoreEmu()
    session = coreemu.create_session()

    # must be in configuration state for nodes to start, when using "node_add" below
    session.set_state(EventTypes.CONFIGURATION_STATE)

    # create switch network node
    switch = session.add_node(SwitchNode, _id=100)

    # create nodes
    for _ in range(NUM_PLCS + NUM_HMIS):
        node = session.add_node(CoreNode)
        interface = prefixes.create_iface(node)
        session.add_link(node.id,
                         switch.id,
                         iface1_data=interface,
                         options=LinkOptions(delay=1000))  # delay in us

    # instantiate session
    session.instantiate()

    node_ifaces = []

    # get nodes to run example
    plc1_node = session.get_node(1, CoreNode)
    node_ifaces.extend(
        [x.localname for x in plc1_node.get_ifaces(control=False)])

    plc2_node = session.get_node(2, CoreNode)
    node_ifaces.extend(
        [x.localname for x in plc2_node.get_ifaces(control=False)])

    hmi1_node = session.get_node(3, CoreNode)
    node_ifaces.extend(
        [x.localname for x in hmi1_node.get_ifaces(control=False)])

    hmi2_node = session.get_node(4, CoreNode)
    node_ifaces.extend(
        [x.localname for x in hmi2_node.get_ifaces(control=False)])

    print("node-ifaces ", node_ifaces)
    plc1_ip_address = prefixes.ip4_address(plc1_node.id)
    plc2_ip_address = prefixes.ip4_address(plc2_node.id)

    print(f"PLC-1 IP: {plc1_ip_address}, PLC-2 IP: {plc2_ip_address}")

    # Clear any existing log files
    if os.path.exists("/tmp/pc_grpc_server_log.txt"):
        os.remove("/tmp/pc_grpc_server_log.txt")
    if os.path.exists("/tmp/plc1_log.txt"):
        os.remove("/tmp/plc1_log.txt")
    if os.path.exists("/tmp/plc2_log.txt"):
        os.remove("/tmp/plc2_log.txt")
    if os.path.exists("/tmp/comm_module1_log.txt"):
        os.remove("/tmp/comm_module1_log.txt")
    if os.path.exists("/tmp/comm_module2_log.txt"):
        os.remove("/tmp/comm_module2_log.txt")
    if os.path.exists("/tmp/hmi1.txt"):
        os.remove("/tmp/hmi1.txt")
    if os.path.exists("/tmp/hmi2.txt"):
        os.remove("/tmp/hmi2.txt")

    fd1 = os.open("/tmp/pc_grpc_server_log.txt", os.O_RDWR | os.O_CREAT)

    pendulum_sim = PendulumSystemSimulator()
    if args.is_virtual == "True":
        is_virtual = True
    else:
        is_virtual = False

    # Create an emulation driver. Register pendulum system simulator with it.
    emulation = EmulationDriver(
        number_dilated_nodes=total_num_dilated_executables,
        is_virtual=is_virtual,
        n_insns_per_round=num_insns_per_round,
        rel_cpu_speed=rel_cpu_speed,
        physical_system_sim_driver=pendulum_sim)

    # Start pc_grpc_server, all PLCs and all communication modules here
    emulation.start_grpc_server(plc_spec_directory, fd1)

    if is_virtual:
        emulation.add_interfaces_to_vt_control(node_ifaces)

    # Retrieve command strings to run PLCs/HMIs/Communication Modules
    plc1_cmd = emulation.get_plc_exec_command(
        path_to_plc_specification_file=plc1_spec_file,
        log_file_path="/tmp/plc1_log.txt")
    plc2_cmd = emulation.get_plc_exec_command(
        path_to_plc_specification_file=plc2_spec_file,
        log_file_path="/tmp/plc2_log.txt")

    comm_module1_cmd = emulation.wrap_command(
        get_comm_module_start_command(plc1_spec_file, plc1_ip_address,
                                      "/tmp/comm_module1_log.txt"))
    comm_module2_cmd = emulation.wrap_command(
        get_comm_module_start_command(plc2_spec_file, plc2_ip_address,
                                      "/tmp/comm_module2_log.txt"))

    hmi1_cmd = emulation.wrap_command(
        get_example_hmi_start_command(plc1_ip_address, "/tmp/hmi1.txt"))
    hmi2_cmd = emulation.wrap_command(
        get_example_hmi_start_command(plc2_ip_address, "/tmp/hmi2.txt"))

    print("Starting PLCs ...")
    plc1_node.cmd(plc1_cmd, wait=False)
    plc2_node.cmd(plc2_cmd, wait=False)
    print("Starting PLC Modbus Comm modules ...")
    plc1_node.cmd(comm_module1_cmd, wait=False)
    plc2_node.cmd(comm_module2_cmd, wait=False)
    print("Starting HMI ...")
    hmi1_node.cmd(hmi1_cmd, wait=False)
    hmi2_node.cmd(hmi2_cmd, wait=False)

    # Wait until all processes have started and registered themselves
    emulation.wait_for_initialization()

    # Register an interrupt signal handler.
    signal.signal(signal.SIGINT, handler)

    total_time_elapsed = 0.0
    while total_time_elapsed <= run_time_secs:

        emulation.run_for(0.01)
        total_time_elapsed += 0.01
        if is_virtual:
            print("Time Elapsed: ", total_time_elapsed)
        if stop == True:
            break

    print("Stopping Emulation ...")
    sys.stdout.flush()
    emulation.stop_exp()

    os.close(fd1)

    # shutdown session
    coreemu.shutdown()

    print("Emulation finished ! ")