coreemu / core

Common Open Research Emulator
BSD 2-Clause "Simplified" License
683 stars 165 forks source link

[BUG] Fail to add link between RJ45 and other node #465

Closed mzy2240 closed 4 years ago

mzy2240 commented 4 years ago

Desktop (please complete the following information):

opt = NodeOptions(name="ens256") rj45 = session.add_node(_type=NodeTypes.RJ45, options=opt) interface = prefixes[0].create_interface(router) session.add_link(router.id, rj45.id, interface_one=interface)

I try to link the RJ45 node to other nodes, and it raises an error. I am using the exact same tap name and I have a working case configged in GUI and I am trying to reproduce the same case but fully by python script. image

bharnden commented 4 years ago

Probably need to see the complete example to really help, hard to say what is going on with that small snnippet.

mzy2240 commented 4 years ago

Here it is:

import time

import datetime
from builtins import range
import sys
import grpc
import subprocess
from queue import Queue
import enum

import parser
import netaddr
from core.emulator.coreemu import CoreEmu
from core.emulator.emudata import IpPrefixes, NodeOptions
from core.emulator.enumerations import NodeTypes, EventTypes
from core.services.utility import UtilService

from core.api.grpc.server import CoreGrpcServer
from core.api.grpc.client import CoreGrpcClient
from core.api.grpc.core_pb2 import NodeType
from random import randint
from core.services.coreservices import ServiceManager
import os
import threading
from threading import Thread
import select

SDT_HOST= "127.0.0.1"
SDT_PORT = 50000
DEAD_TIME = 3
ROUTE_TIME = 3

# Set up the logger
import logging
a_logger = logging.getLogger()
a_logger.setLevel(logging.DEBUG)
output_file_handler = logging.FileHandler("test_static_route.log")
a_logger.addHandler(output_file_handler)

# ip generator
prefixe_0 = IpPrefixes(ip4_prefix="10.110.215.1/24")
prefixe_1 = IpPrefixes(ip4_prefix="172.16.0.0/24")
prefixes = [prefixe_0, prefixe_1]

# create session and node
coreemu = CoreEmu()
session = coreemu.create_session(_id = randint(11, 1000))

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

# create a router
router = session.add_node(_type=NodeTypes.DEFAULT)

# create the rj45 node and connect to the interface
interfaces = []

opt = NodeOptions(name="ens256")
rj45 = session.add_node(_type=NodeTypes.RJ45, options=opt)
interface = prefixes[0].create_interface(router)
session.add_link(router.id, rj45.id, interface_one=interface)

# initialize grpc service
grpc_server = CoreGrpcServer(coreemu)
grpc_address = "localhost:50051"
grpc_thread = threading.Thread(target=grpc_server.listen, args=(grpc_address,), daemon=True)
grpc_thread.start()

# instantiate session
session.instantiate()

while True:
    time.sleep(1)

# shutdown session
coreemu.shutdown()
bharnden commented 4 years ago

Verifying how the GUI has traditionally handled it, the problem is that you need to provide an interface for the RJ45 node as well, in the add link function.

The interface would need to be created like so for now, while clunky, this is what the GUI is currently providing.

rj45_interface = InterfaceData(
    _id=0,
    name="",
    mac="",
    ip4="",
    ip4_mask=0,
    ip6="",
    ip6_mask=0,
)
session.add_link(router.id, rj45.id, interface_one=interface, interface_two=rj45_interface)
mzy2240 commented 4 years ago

It's working now. Thank you @bharnden. I noticed you are about to release the next version, will you simplify the usage of RJ45 node in the next release?

bharnden commented 4 years ago

Changing its needs behind the scenes would likely be fairly painful. In the near term, I may be able to make a change to help creating the interface class in a simpler manner as an alternative.

Thanks for pointing this out.

bharnden commented 4 years ago

develop will have changes so that the following line would be equivalent to needing to provide all parameters

rj45_interface = InterfaceData()