Ejemplo n.º 1
0
class InoutTest(BaseMagmaTest.MagmaControllerTest):
    def setUp(self):
        super(InoutTest, self).setUp()
        self.apps_under_test = ['pipelined.app.base', 'pipelined.app.inout']

    def _generate_topology(self):
        # import here, after we've checked the environment
        from ovstest import util
        from magma.pkt_tester.topology_builder import TopologyBuilder

        self._topo_builder = TopologyBuilder()

        # set up a simple topology
        bridge = self._topo_builder.create_bridge(self.TEST_BRIDGE)
        self._port_no = {}
        for iface_name, ip_address in self.TEST_IPS.items():
            port = self._topo_builder.bind(iface_name, bridge)
            self._topo_builder.create_interface(iface_name,
                                                ip_address,
                                                self.TEST_NETMASK)
            self._port_no[iface_name] = port.port_no

        self.assertFalse(self._topo_builder.invalid_devices())

    def test_add_inout_flows(self):
        from ovstest import util
        from magma.pkt_tester.topology_builder import OvsException

        self._generate_topology()
        self.controller_thread.start()
        self._wait_for_controller()

        # clear out any existing in_blocks and set up for the test
        in_net = self.TEST_NETS[self.SRC_PORT]
        del self.mc.in_blocks[:]
        self.mc.in_blocks.append(in_net)

        # clear out existing net block to of port mappings
        for k in list(self.mc.IPBLOCK_TO_OFPORT.keys()):
            del self.mc.IPBLOCK_TO_OFPORT[k]

        self.mc.IPBLOCK_TO_OFPORT[in_net] = self._port_no[self.SRC_PORT]

        self._setup_ovs()
        self._wait_for_datapath()

        ret, out, err = util.start_process(["ovs-ofctl", "dump-flows",
                                           self.TEST_BRIDGE])
        flow_string = str(out)

        # check if the flows we expect are loaded
        # directions are tagged properly, and resubmit to right table
        expected = "nw_dst=%s actions=set_field:0->metadata,resubmit(,1)" % in_net
        self.assertTrue(expected in flow_string)

        expected = "nw_src=%s actions=set_field:0x10->metadata,resubmit(,1)" % in_net
        self.assertTrue(expected in flow_string)
Ejemplo n.º 2
0
class MagmaControllerPktTest(BaseMagmaTest.MagmaControllerTest):
    def setUp(self):
        super(MagmaControllerPktTest, self).setUp()
        self.apps_under_test = ['pipelined.app.base']

    def _generate_topology(self):
        # import here, after we've checked the environment
        from ovstest import util
        from magma.pkt_tester.topology_builder import TopologyBuilder

        self._topo_builder = TopologyBuilder()

        # set up a simple topology
        bridge = self._topo_builder.create_bridge(self.TEST_BRIDGE)
        self._port_no = {}
        for iface_name, ip_address in self.TEST_IPS.items():
            port = self._topo_builder.bind(iface_name, bridge)
            self._topo_builder.create_interface(iface_name, ip_address,
                                                self.TEST_NETMASK)
            self._port_no[iface_name] = port.port_no

        self.assertFalse(self._topo_builder.invalid_devices())

    def test_delete_all_flows(self):
        # import here, after we've checked the environment
        from ovstest import util
        from magma.pkt_tester.topology_builder import OvsException

        # basic setup
        self._generate_topology()
        self.controller_thread.start()
        self._wait_for_controller()
        self._setup_ovs()
        self._wait_for_datapath()

        # add flows to the test bridge
        for iface in self.TEST_IPS:
            port = self._port_no[iface]
            flow = "in_port=%d,actions=output:%d" % (port, port)
            ret, out, err = util.start_process(
                ["ovs-ofctl", "add-flow", self.TEST_BRIDGE, flow])
            ret, out, err = util.start_process(
                ["ovs-ofctl", "dump-flows", self.TEST_BRIDGE])

        self.mc.reset_all_flows(list(self.mc.datapaths.values())[0])

        time.sleep(1.5)  # we gotta wait a while in practice :-(

        ret, out, err = util.start_process(
            ["ovs-ofctl", "dump-flows", self.TEST_BRIDGE])

        flows = out.decode("utf-8").strip().split('\n')

        # when no flows, you get just one element containing "NXST_FLOW"
        self.assertEqual(len(flows), 1)

    def test_delete_table_flows(self):
        # import here, after we've checked the environment
        from ovstest import util
        from magma.pkt_tester.topology_builder import OvsException

        # basic setup
        self._generate_topology()
        self.controller_thread.start()
        self._wait_for_controller()
        self._setup_ovs()
        self._wait_for_datapath()

        # add flows to the test bridge
        for iface in self.TEST_IPS:
            port = self._port_no[iface]
            flow = "in_port=%d,table=5,actions=output:%d" % (port, port)
            ret, out, err = util.start_process(
                ["ovs-ofctl", "add-flow", self.TEST_BRIDGE, flow])
            flow = "in_port=%d,table=6,actions=output:%d" % (port, port)
            ret, out, err = util.start_process(
                ["ovs-ofctl", "add-flow", self.TEST_BRIDGE, flow])
            ret, out, err = util.start_process(
                ["ovs-ofctl", "dump-flows", self.TEST_BRIDGE])

        dp = list(self.mc.datapaths.values())[0]
        self.mc.delete_all_table_flows(dp, table=5)
        time.sleep(1.5)
        ret, out, err = util.start_process(
            ["ovs-ofctl", "dump-flows", self.TEST_BRIDGE])

        self.assertTrue("table=6" in str(out))
        self.assertFalse("table=5" in str(out))
Ejemplo n.º 3
0
class TestTopologyBuilder(unittest.TestCase):
    """
    Test class to test the topology builder utility.
    """
    TEST_BRIDGE_NAME = "test_br"
    TEST_INT_PREFIX = "test_int"
    TEST_NETMASK = "255.255.255.0"
    TEST_IP_PREFIX = "192.168.70."

    def setUp(self):
        # Sanity check the env.
        if not check_env():
            raise SkipTest("Environment does not support this test")

        self._topology_builder = None

    def tearDown(self):
        self._topology_builder.destroy()

    def test_create_ovs_topology(self):
        """
        Creates a test topology with a test bridge and two interfaces with ip
        addresses assigned.
        """
        from magma.pkt_tester.topology_builder import TopologyBuilder

        self._topology_builder = TopologyBuilder()

        bridge = self._topology_builder.create_bridge(self.TEST_BRIDGE_NAME)
        for i in range(0, 2):
            ip_address = self.TEST_IP_PREFIX + str(i + 2)
            iface_name = self.TEST_INT_PREFIX + str(i)
            self._topology_builder.bind(iface_name, bridge)
            self._topology_builder.create_interface(
                iface_name,
                ip_address,
                self.TEST_NETMASK,
            )

        self.assertFalse(self._topology_builder.invalid_devices())

    def test_ports(self):
        """
        Simple validator for port methods.
        """
        from magma.pkt_tester.topology_builder import (
            TopologyBuilder,
            UseAfterFreeException,
        )
        self._topology_builder = TopologyBuilder()
        bridge = self._topology_builder.create_bridge(self.TEST_BRIDGE_NAME)

        ip_address = self.TEST_IP_PREFIX + "2"
        iface_name = self.TEST_INT_PREFIX + "0"

        port = self._topology_builder.bind(iface_name, bridge)
        self._topology_builder.create_interface(
            iface_name,
            ip_address,
            self.TEST_NETMASK,
        )

        self.assertEqual(port.bridge_name, self.TEST_BRIDGE_NAME)
        self.assertEqual(port.iface_name, iface_name)
        self.assertFalse(self._topology_builder.invalid_devices())
        # Verify that the port number read back is > 0.
        self.assertTrue(port.port_no >= 0)
        port.destroy(free_resource=False)
        self.assertRaises(UseAfterFreeException, port.destroy)
        self.assertRaises(UseAfterFreeException, port.sanity_check)

        # Cleanup
        self._topology_builder.destroy()

    def test_iface(self):
        """
        Simple validator for iface methods
        """
        from magma.pkt_tester.topology_builder import (
            TopologyBuilder,
            UseAfterFreeException,
        )
        self._topology_builder = TopologyBuilder()
        ip_address = self.TEST_IP_PREFIX + "2"
        iface_name = self.TEST_INT_PREFIX + "0"
        bridge = self._topology_builder.create_bridge(self.TEST_BRIDGE_NAME)
        self._topology_builder.bind(iface_name, bridge)
        iface = self._topology_builder.create_interface(
            iface_name,
            ip_address,
            self.TEST_NETMASK,
        )
        self.assertEqual(iface.name, iface_name)
        self.assertEqual(iface.ip_address, ip_address)
        self.assertEqual(iface.netmask, self.TEST_NETMASK)

        iface.destroy()

        self.assertRaises(UseAfterFreeException, iface.up)
        self.assertRaises(UseAfterFreeException, iface.sanity_check)

    def test_bridge(self):
        """
        Simple validator for bridge methods
        """
        from magma.pkt_tester.topology_builder import (
            TopologyBuilder,
            UseAfterFreeException,
        )
        self._topology_builder = TopologyBuilder()
        bridge = self._topology_builder.create_bridge(self.TEST_BRIDGE_NAME)

        self.assertEqual(bridge.name, self.TEST_BRIDGE_NAME)
        bridge.destroy()
        iface_name = self.TEST_INT_PREFIX + "0"
        self.assertRaises(
            UseAfterFreeException,
            bridge.add_virtual_port,
            iface_name,
            "internal",
        )
        self.assertRaises(
            UseAfterFreeException,
            bridge.add_physical_port,
            "eth0",
        )
        self.assertRaises(UseAfterFreeException, bridge.sanity_check)
Ejemplo n.º 4
0
class MeterTest(BaseMagmaTest.MagmaControllerTest):
    def setUp(self):
        super(MeterTest, self).setUp()
        self.apps_under_test = ['pipelined.app.meter']

    def _generate_topology(self):
        # import here, after we've checked the environment
        from ovstest import util
        from magma.pkt_tester.topology_builder import TopologyBuilder

        self._topo_builder = TopologyBuilder()

        # set up a simple topology
        bridge = self._topo_builder.create_bridge(self.TEST_BRIDGE)
        self._port_no = {}
        for iface_name, ip_address in self.TEST_IPS.items():
            port = self._topo_builder.bind(iface_name, bridge)
            self._topo_builder.create_interface(iface_name,
                                                ip_address,
                                                self.TEST_NETMASK)
            self._port_no[iface_name] = port.port_no

        self.assertFalse(self._topo_builder.invalid_devices())

    def test_add_meter_flows(self):
        from ovstest import util
        from magma.pkt_tester.topology_builder import OvsException

        self._generate_topology()
        self.controller_thread.start()
        self._wait_for_controller("MeterController")

        # clear out any existing in_blocks and set up for the test
        in_net = self.TEST_NETS[self.SRC_PORT]

        # clear out existing net block to of port mappings
        for k in list(self.mc.IPBLOCK_TO_OFPORT.keys()):
            del self.mc.IPBLOCK_TO_OFPORT[k]

        self.mc.IPBLOCK_TO_OFPORT[in_net] = self._port_no[self.SRC_PORT]

        self._setup_ovs()
        self._wait_for_datapath()

        ret, out, err = util.start_process(["ovs-ofctl", "dump-flows",
                                           self.TEST_BRIDGE])

        dpid = list(self.mc.datapaths.keys())[0]
        self.mc._poll_stats(self.mc.datapaths[dpid])

        time.sleep(0.5) # give the vswitch some time to respond

        # check if we're tracking usage for each user
        # it should be zero since there's no traffic
        for sid in self.mc.ip_to_sid.values():
            self.assertTrue(sid in self.mc.usage)
            ur = self.mc.usage[sid]
            self.assertTrue(ur.bytes_tx == 0)
            self.assertTrue(ur.bytes_rx == 0)
            self.assertTrue(ur.pkts_tx == 0)
            self.assertTrue(ur.pkts_rx == 0)