示例#1
0
文件: test_links.py 项目: gsomlo/core
    def test_link_update(self, session, ip_prefixes):
        # given
        node_one = session.add_node()
        node_two = session.add_node(_type=NodeTypes.SWITCH)
        interface_one = ip_prefixes.create_interface(node_one)
        session.add_link(node_one.objid, node_two.objid, interface_one)
        interface = node_one.netif(interface_one.id)
        output = utils.check_cmd(["tc", "qdisc", "show", "dev", interface.localname])
        assert "delay" not in output
        assert "rate" not in output
        assert "loss" not in output
        assert "duplicate" not in output

        # when
        link_options = LinkOptions()
        link_options.delay = 50
        link_options.bandwidth = 5000000
        link_options.per = 25
        link_options.dup = 25
        session.update_link(node_one.objid, node_two.objid,
                            interface_one_id=interface_one.id, link_options=link_options)

        # then
        output = utils.check_cmd(["tc", "qdisc", "show", "dev", interface.localname])
        assert "delay" in output
        assert "rate" in output
        assert "loss" in output
        assert "duplicate" in output
示例#2
0
def add_link_data(
    link_proto: core_pb2.Link
) -> Tuple[InterfaceData, InterfaceData, LinkOptions]:
    """
    Convert link proto to link interfaces and options data.

    :param link_proto: link  proto
    :return: link interfaces and options
    """
    interface_one = link_interface(link_proto.interface_one)
    interface_two = link_interface(link_proto.interface_two)

    link_type = None
    link_type_value = link_proto.type
    if link_type_value is not None:
        link_type = LinkTypes(link_type_value)

    options = LinkOptions(_type=link_type)
    options_data = link_proto.options
    if options_data:
        options.delay = options_data.delay
        options.bandwidth = options_data.bandwidth
        options.per = options_data.per
        options.dup = options_data.dup
        options.jitter = options_data.jitter
        options.mer = options_data.mer
        options.burst = options_data.burst
        options.mburst = options_data.mburst
        options.unidirectional = options_data.unidirectional
        options.key = options_data.key
        options.opaque = options_data.opaque

    return interface_one, interface_two, options
示例#3
0
文件: test_links.py 项目: yrs1/core
    def test_link_update(self, session, ip_prefixes):
        # given
        node_one = session.add_node()
        node_two = session.add_node(_type=NodeTypes.SWITCH)
        interface_one = ip_prefixes.create_interface(node_one)
        session.add_link(node_one.objid, node_two.objid, interface_one)
        interface = node_one.netif(interface_one.id)
        output = utils.check_cmd(
            ["tc", "qdisc", "show", "dev", interface.localname])
        assert "delay" not in output
        assert "rate" not in output
        assert "loss" not in output
        assert "duplicate" not in output

        # when
        link_options = LinkOptions()
        link_options.delay = 50
        link_options.bandwidth = 5000000
        link_options.per = 25
        link_options.dup = 25
        session.update_link(node_one.objid,
                            node_two.objid,
                            interface_one_id=interface_one.id,
                            link_options=link_options)

        # then
        output = utils.check_cmd(
            ["tc", "qdisc", "show", "dev", interface.localname])
        assert "delay" in output
        assert "rate" in output
        assert "loss" in output
        assert "duplicate" in output
示例#4
0
文件: test_links.py 项目: gsomlo/core
    def test_link_delay(self, session, ip_prefixes):
        """
        Test ptp node network with modifying link packet delay.

        :param core.emulator.coreemu.EmuSession session: session for test
        :param ip_prefixes: generates ip addresses for nodes
        """

        # create link network
        node_one, node_two = create_ptp_network(session, ip_prefixes)

        # run ping for delay information
        stdout = ping_output(node_one, node_two, ip_prefixes)
        assert stdout
        rtt_line = stdout.split("\n")[-1]
        rtt_values = rtt_line.split("=")[1].split("ms")[0].strip()
        rtt_avg = float(rtt_values.split("/")[2])
        assert 0 <= rtt_avg <= 0.2

        # change delay in microseconds
        link_options = LinkOptions()
        link_options.delay = 1000000
        session.update_link(node_one.objid, node_two.objid, link_options=link_options)

        # run ping for delay information again
        stdout = ping_output(node_one, node_two, ip_prefixes)
        assert stdout
        rtt_line = stdout.split("\n")[-1]
        rtt_values = rtt_line.split("=")[1].split("ms")[0].strip()
        rtt_avg = float(rtt_values.split("/")[2])
        assert 1800 <= rtt_avg <= 2200
示例#5
0
文件: test_links.py 项目: yrs1/core
    def test_link_delay(self, session, ip_prefixes):
        """
        Test ptp node network with modifying link packet delay.

        :param core.emulator.coreemu.EmuSession session: session for test
        :param ip_prefixes: generates ip addresses for nodes
        """

        # create link network
        node_one, node_two = create_ptp_network(session, ip_prefixes)

        # run ping for delay information
        stdout = ping_output(node_one, node_two, ip_prefixes)
        assert stdout
        rtt_line = stdout.split("\n")[-1]
        rtt_values = rtt_line.split("=")[1].split("ms")[0].strip()
        rtt_avg = float(rtt_values.split("/")[2])
        assert 0 <= rtt_avg <= 0.2

        # change delay in microseconds
        link_options = LinkOptions()
        link_options.delay = 1000000
        session.update_link(node_one.objid,
                            node_two.objid,
                            link_options=link_options)

        # run ping for delay information again
        stdout = ping_output(node_one, node_two, ip_prefixes)
        assert stdout
        rtt_line = stdout.split("\n")[-1]
        rtt_values = rtt_line.split("=")[1].split("ms")[0].strip()
        rtt_avg = float(rtt_values.split("/")[2])
        assert 1800 <= rtt_avg <= 2200
示例#6
0
def get_LinkOptions(link_params):
    '''This class is used derive new LinkOptions() from the LinkEvent object.
    LinkOptions are used by CORE to specify delays, bandwidth etc for a specific link.

    :returns: linkOptions, a convenience object used by core to specify link values (delay, bandwidth, jitter etc.)
    :rtype: LinkOptions
    '''

    linkOptions = LinkOptions()
    for param, value in link_params.items():
        parameter = param.lower()

        if (value):
            newValue = float(value)
        else:
            newValue = value

        if (parameter == "session"):
            linkOptions.session = newValue
        elif (parameter == "delay"):
            linkOptions.delay = newValue
        elif (parameter == "bandwidth"):
            linkOptions.bandwidth = newValue
        # Packet Loss Rate
        elif (parameter == "loss"):
            linkOptions.per = newValue
        # Packet duplication rate
        elif (parameter == "duplication" or parameter == "dup"):
            linkOptions.dup = newValue
        elif (parameter == "jitter"):
            linkOptions.jitter = newValue

    return linkOptions
示例#7
0
    def read_links(self):
        link_elements = self.scenario.find("links")
        if link_elements is None:
            return

        node_sets = set()
        for link_element in link_elements.iterchildren():
            node_one = get_int(link_element, "node_one")
            node_two = get_int(link_element, "node_two")
            node_set = frozenset((node_one, node_two))

            interface_one_element = link_element.find("interface_one")
            interface_one = None
            if interface_one_element is not None:
                interface_one = create_interface_data(interface_one_element)

            interface_two_element = link_element.find("interface_two")
            interface_two = None
            if interface_two_element is not None:
                interface_two = create_interface_data(interface_two_element)

            options_element = link_element.find("options")
            link_options = LinkOptions()
            if options_element is not None:
                link_options.bandwidth = get_int(options_element, "bandwidth")
                link_options.burst = get_int(options_element, "burst")
                link_options.delay = get_int(options_element, "delay")
                link_options.dup = get_int(options_element, "dup")
                link_options.mer = get_int(options_element, "mer")
                link_options.mburst = get_int(options_element, "mburst")
                link_options.jitter = get_int(options_element, "jitter")
                link_options.key = get_int(options_element, "key")
                link_options.per = get_int(options_element, "per")
                link_options.unidirectional = get_int(options_element,
                                                      "unidirectional")
                link_options.session = options_element.get("session")
                link_options.emulation_id = get_int(options_element,
                                                    "emulation_id")
                link_options.network_id = get_int(options_element,
                                                  "network_id")
                link_options.opaque = options_element.get("opaque")
                link_options.gui_attributes = options_element.get(
                    "gui_attributes")

            if link_options.unidirectional == 1 and node_set in node_sets:
                logging.info("updating link node_one(%s) node_two(%s): %s",
                             node_one, node_two, link_options)
                self.session.update_link(node_one, node_two, interface_one.id,
                                         interface_two.id, link_options)
            else:
                logging.info("adding link node_one(%s) node_two(%s): %s",
                             node_one, node_two, link_options)
                self.session.add_link(node_one, node_two, interface_one,
                                      interface_two, link_options)

            node_sets.add(node_set)
示例#8
0
    def get_LinkOptions(self):
        '''This method is used derive new LinkOptions() from the LinkEvent object.
        LinkOptions are used by CORE to specify delays, bandwidth etc for a specific link.

        :returns: linkOptions, a convenience object used by core to specify link values (delay, bandwidth, jitter etc.)
        :rtype: core.emulator.emudata.LinkOptions
        '''

        linkOptions = LinkOptions()
        for param, value in self.link_params.items():
            #print ("param = {} -- Value = {}".format(param, value))
            parameter = param.lower()
            newValue = value
            if (parameter == "session"):
                linkOptions.session = newValue
            elif (parameter == "delay"):
                linkOptions.delay = newValue
            elif (parameter == "bandwidth"):
                linkOptions.bandwidth = float(newValue)
            # Packet Loss Rate
            elif (parameter == "loss"):
                linkOptions.per = newValue
            # Packet duplication rate
            elif (parameter == "duplication"):
                linkOptions.dup = newValue
            elif (parameter == "jitter"):
                linkOptions.jitter = newValue
            # elif (parameter == "mer"):
            #     linkOptions.mer = newValue
            # elif (parameter == "burst"):
            #     linkOptions.burst = newValue
            # elif (parameter == "mburst"):
            #     linkOptions.mburst = newValue
            # elif (parameter == "gui_attributes"):
            #     linkOptions.gui_attributes = newValue
            # elif (parameter == "unidirectional"):
            #     linkOptions.unidirectional = newValue
            # elif (parameter == "emulation_id"):
            #     linkOptions.emulation_id = newValue
            # elif (parameter == "network_id"):
            #     linkOptions.network_id = newValue
            # elif (parameter == "key"):
            #     linkOptions.key = newValue
            # elif (parameter == "opaque"):
            #     linkOptions.opaque = newValue

        return linkOptions
示例#9
0
文件: corexml.py 项目: gsomlo/core
    def read_links(self):
        link_elements = self.scenario.find("links")
        if link_elements is None:
            return

        for link_element in link_elements.iterchildren():
            node_one = get_int(link_element, "node_one")
            node_two = get_int(link_element, "node_two")

            interface_one_element = link_element.find("interface_one")
            interface_one = None
            if interface_one_element is not None:
                interface_one = create_interface_data(interface_one_element)

            interface_two_element = link_element.find("interface_two")
            interface_two = None
            if interface_two_element is not None:
                interface_two = create_interface_data(interface_two_element)

            options_element = link_element.find("options")
            link_options = LinkOptions()
            if options_element is not None:
                link_options.bandwidth = get_float(options_element, "bandwidth")
                link_options.burst = get_float(options_element, "burst")
                link_options.delay = get_float(options_element, "delay")
                link_options.dup = get_float(options_element, "dup")
                link_options.mer = get_float(options_element, "mer")
                link_options.mburst = get_float(options_element, "mburst")
                link_options.jitter = get_float(options_element, "jitter")
                link_options.key = get_float(options_element, "key")
                link_options.per = get_float(options_element, "per")
                link_options.unidirectional = get_int(options_element, "unidirectional")
                link_options.session = options_element.get("session")
                link_options.emulation_id = get_int(options_element, "emulation_id")
                link_options.network_id = get_int(options_element, "network_id")
                link_options.opaque = options_element.get("opaque")
                link_options.gui_attributes = options_element.get("gui_attributes")

            logger.info("reading link node_one(%s) node_two(%s)", node_one, node_two)
            self.session.add_link(node_one, node_two, interface_one, interface_two, link_options)
示例#10
0
    def test_link_update(self, session: Session, ip_prefixes: IpPrefixes):
        # given
        delay = 50
        bandwidth = 5000000
        per = 25
        dup = 25
        jitter = 10
        node_one = session.add_node(CoreNode)
        node_two = session.add_node(SwitchNode)
        interface_one_data = ip_prefixes.create_interface(node_one)
        session.add_link(node_one.id, node_two.id, interface_one_data)
        interface_one = node_one.netif(interface_one_data.id)
        assert interface_one.getparam("delay") != delay
        assert interface_one.getparam("bw") != bandwidth
        assert interface_one.getparam("loss") != per
        assert interface_one.getparam("duplicate") != dup
        assert interface_one.getparam("jitter") != jitter

        # when
        link_options = LinkOptions()
        link_options.delay = delay
        link_options.bandwidth = bandwidth
        link_options.per = per
        link_options.dup = dup
        link_options.jitter = jitter
        session.update_link(
            node_one.id,
            node_two.id,
            interface_one_id=interface_one_data.id,
            options=link_options,
        )

        # then
        assert interface_one.getparam("delay") == delay
        assert interface_one.getparam("bw") == bandwidth
        assert interface_one.getparam("loss") == per
        assert interface_one.getparam("duplicate") == dup
        assert interface_one.getparam("jitter") == jitter
示例#11
0
文件: server.py 项目: yanhc519/core
 def EditLink(self, request, context):
     logging.debug("edit link: %s", request)
     session = self.get_session(request.session_id, context)
     node_one_id = request.node_one_id
     node_two_id = request.node_two_id
     interface_one_id = request.interface_one_id
     interface_two_id = request.interface_two_id
     options_data = request.options
     link_options = LinkOptions()
     link_options.delay = options_data.delay
     link_options.bandwidth = options_data.bandwidth
     link_options.per = options_data.per
     link_options.dup = options_data.dup
     link_options.jitter = options_data.jitter
     link_options.mer = options_data.mer
     link_options.burst = options_data.burst
     link_options.mburst = options_data.mburst
     link_options.unidirectional = options_data.unidirectional
     link_options.key = options_data.key
     link_options.opaque = options_data.opaque
     session.update_link(node_one_id, node_two_id, interface_one_id,
                         interface_two_id, link_options)
     return core_pb2.EditLinkResponse(result=True)
示例#12
0
    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(_type=NodeTypes.SWITCH)

    print "Everything is set up now."

    # create nodes
    for _ in xrange(2):
        node = session.add_node()
        interface = prefixes.create_interface(node)
        link_opts = LinkOptions()
        link_opts.delay = {{delay}}
        link_opts.bandwidth = {{mean_bw}}
        session.add_link(node.objid,
                         switch.objid,
                         interface_one=interface,
                         link_options=link_opts)

    print "Links are set up."

    # instantiate session
    session.instantiate()

    # get nodes to run example
    first_node = session.get_object(2)
    last_node = session.get_object(3)
示例#13
0
文件: server.py 项目: yanhc519/core
    def AddLink(self, request, context):
        logging.debug("add link: %s", request)
        session = self.get_session(request.session_id, context)

        # validate node exist
        self.get_node(session, request.link.node_one_id, context)
        self.get_node(session, request.link.node_two_id, context)
        node_one_id = request.link.node_one_id
        node_two_id = request.link.node_two_id

        interface_one = None
        interface_one_data = request.link.interface_one
        if interface_one_data:
            name = interface_one_data.name
            if name == "":
                name = None
            mac = interface_one_data.mac
            if mac == "":
                mac = None
            else:
                mac = MacAddress.from_string(mac)
            interface_one = InterfaceData(
                _id=interface_one_data.id,
                name=name,
                mac=mac,
                ip4=interface_one_data.ip4,
                ip4_mask=interface_one_data.ip4mask,
                ip6=interface_one_data.ip6,
                ip6_mask=interface_one_data.ip6mask,
            )

        interface_two = None
        interface_two_data = request.link.interface_two
        if interface_two_data:
            name = interface_two_data.name
            if name == "":
                name = None
            mac = interface_two_data.mac
            if mac == "":
                mac = None
            else:
                mac = MacAddress.from_string(mac)
            interface_two = InterfaceData(
                _id=interface_two_data.id,
                name=name,
                mac=mac,
                ip4=interface_two_data.ip4,
                ip4_mask=interface_two_data.ip4mask,
                ip6=interface_two_data.ip6,
                ip6_mask=interface_two_data.ip6mask,
            )

        link_type = None
        link_type_value = request.link.type
        if link_type_value is not None:
            link_type = LinkTypes(link_type_value)

        options_data = request.link.options
        link_options = LinkOptions(_type=link_type)
        if options_data:
            link_options.delay = options_data.delay
            link_options.bandwidth = options_data.bandwidth
            link_options.per = options_data.per
            link_options.dup = options_data.dup
            link_options.jitter = options_data.jitter
            link_options.mer = options_data.mer
            link_options.burst = options_data.burst
            link_options.mburst = options_data.mburst
            link_options.unidirectional = options_data.unidirectional
            link_options.key = options_data.key
            link_options.opaque = options_data.opaque

        session.add_link(node_one_id,
                         node_two_id,
                         interface_one,
                         interface_two,
                         link_options=link_options)
        return core_pb2.AddLinkResponse(result=True)
示例#14
0
    def test_link_options_bidirectional(self, session, tmpdir, ip_prefixes):
        """
        Test xml client methods for a ptp network.

        :param session: session for test
        :param tmpdir: tmpdir to create data in
        :param ip_prefixes: generates ip addresses for nodes
        """
        # create nodes
        node_one = session.add_node()
        interface_one = ip_prefixes.create_interface(node_one)
        node_two = session.add_node()
        interface_two = ip_prefixes.create_interface(node_two)

        # create link
        link_options_one = LinkOptions()
        link_options_one.unidirectional = 1
        link_options_one.bandwidth = 5000
        link_options_one.delay = 10
        link_options_one.per = 10.5
        link_options_one.dup = 5
        link_options_one.jitter = 5
        session.add_link(node_one.id, node_two.id, interface_one,
                         interface_two, link_options_one)
        link_options_two = LinkOptions()
        link_options_two.unidirectional = 1
        link_options_two.bandwidth = 10000
        link_options_two.delay = 20
        link_options_two.per = 10
        link_options_two.dup = 10
        link_options_two.jitter = 10
        session.update_link(
            node_two.id,
            node_one.id,
            interface_two.id,
            interface_one.id,
            link_options_two,
        )

        # instantiate session
        session.instantiate()

        # get ids for nodes
        n1_id = node_one.id
        n2_id = node_two.id

        # save xml
        xml_file = tmpdir.join("session.xml")
        file_path = xml_file.strpath
        session.save_xml(file_path)

        # verify xml file was created and can be parsed
        assert xml_file.isfile()
        assert ElementTree.parse(file_path)

        # stop current session, clearing data
        session.shutdown()

        # verify nodes have been removed from session
        with pytest.raises(CoreError):
            assert not session.get_node(n1_id)
        with pytest.raises(CoreError):
            assert not session.get_node(n2_id)

        # load saved xml
        session.open_xml(file_path, start=True)

        # verify nodes have been recreated
        assert session.get_node(n1_id)
        assert session.get_node(n2_id)
        links = []
        for node_id in session.nodes:
            node = session.nodes[node_id]
            links += node.all_link_data(0)
        assert len(links) == 2
        link_one = links[0]
        link_two = links[1]
        assert link_options_one.bandwidth == link_one.bandwidth
        assert link_options_one.delay == link_one.delay
        assert link_options_one.per == link_one.per
        assert link_options_one.dup == link_one.dup
        assert link_options_one.jitter == link_one.jitter
        assert link_options_two.bandwidth == link_two.bandwidth
        assert link_options_two.delay == link_two.delay
        assert link_options_two.per == link_two.per
        assert link_options_two.dup == link_two.dup
        assert link_options_two.jitter == link_two.jitter
示例#15
0
    def test_link_options(self, session, tmpdir, ip_prefixes):
        """
        Test xml client methods for a ptp network.

        :param session: session for test
        :param tmpdir: tmpdir to create data in
        :param ip_prefixes: generates ip addresses for nodes
        """
        # create nodes
        node_one = session.add_node()
        interface_one = ip_prefixes.create_interface(node_one)
        switch = session.add_node(_type=NodeTypes.SWITCH)

        # create link
        link_options = LinkOptions()
        link_options.per = 10.5
        link_options.bandwidth = 50000
        link_options.jitter = 10
        link_options.delay = 30
        link_options.dup = 5
        session.add_link(node_one.id,
                         switch.id,
                         interface_one,
                         link_options=link_options)

        # instantiate session
        session.instantiate()

        # get ids for nodes
        n1_id = node_one.id
        n2_id = switch.id

        # save xml
        xml_file = tmpdir.join("session.xml")
        file_path = xml_file.strpath
        session.save_xml(file_path)

        # verify xml file was created and can be parsed
        assert xml_file.isfile()
        assert ElementTree.parse(file_path)

        # stop current session, clearing data
        session.shutdown()

        # verify nodes have been removed from session
        with pytest.raises(CoreError):
            assert not session.get_node(n1_id)
        with pytest.raises(CoreError):
            assert not session.get_node(n2_id)

        # load saved xml
        session.open_xml(file_path, start=True)

        # verify nodes have been recreated
        assert session.get_node(n1_id)
        assert session.get_node(n2_id)
        links = []
        for node_id in session.nodes:
            node = session.nodes[node_id]
            links += node.all_link_data(0)
        link = links[0]
        assert link_options.per == link.per
        assert link_options.bandwidth == link.bandwidth
        assert link_options.jitter == link.jitter
        assert link_options.delay == link.delay
        assert link_options.dup == link.dup