Esempio n. 1
0
 def init_store(
     self,
     client: redis.Redis,
     persist_to_redis: bool,
     redis_port: int,
 ):
     if not persist_to_redis:
         self.ip_state_map = IpDescriptorMap(defaultdict(dict))
         self.ipv6_state_map = IpDescriptorMap(defaultdict(dict))
         self.assigned_ip_blocks = set()  # {ip_block}
         self.sid_ips_map = defaultdict(IPDesc)  # {SID=>IPDesc}
         self.dhcp_gw_info = UplinkGatewayInfo(defaultdict(GWInfo))
         self.dhcp_store = {}  # mac => DHCP_State
         self.allocated_iid = {}  # {ipv6 interface identifiers}
         self.sid_session_prefix_allocated = {}  # SID => session prefix
     else:
         if not redis_port:
             raise ValueError(
                 'Must specify a redis_port in mobilityd config.', )
         self.ip_state_map = IpDescriptorMap(
             defaultdict_key(lambda key: ip_states(client, key)), )
         self.ipv6_state_map = IpDescriptorMap(
             defaultdict_key(lambda key: ip_states(client, key)), )
         self.assigned_ip_blocks = AssignedIpBlocksSet(client)
         self.sid_ips_map = IPDescDict(client)
         self.dhcp_gw_info = UplinkGatewayInfo(GatewayInfoMap())
         self.dhcp_store = MacToIP()  # mac => DHCP_State
         self.allocated_iid = AllocatedIID()
         self.sid_session_prefix_allocated = AllocatedSessionPrefix()
Esempio n. 2
0
    def set_deafult_gw(self, args):
        """
        Set GW for given uplink network
        Args:
            args: IP address of GW.

        Returns:
        """

        gw_ip = ip_address(args.ip)
        gw_info = UplinkGatewayInfo()
        gw_info.update_ip(str(gw_ip))
        print("set Default gw IP to %s" % gw_info.get_gw_ip())
Esempio n. 3
0
    def set_deafult_gw_mac(self, args):
        """
        Set mac address of the GW
        Args:
            args: mac address
        Returns:
        """

        gw_mac = ip_address(args.ip)
        gw_info = UplinkGatewayInfo()
        gw_info.update_mac(str(gw_mac))

        print("set Default gw mac to %s" % gw_info.get_gw_mac())
Esempio n. 4
0
    def list_all_record(self, args):
        """
                Print list DHCP and GW records.
        Args:
            args: None

        Returns: None
        """

        for k, v in self.dhcp_client_state.items():
            print("mac: %s DHCP state: %s" % (k.replace('_', ':'), str(v)))

        gw_info = UplinkGatewayInfo(store.GatewayInfoMap())
        print("Default GW: %s" % gw_info.get_gw_ip())
        print("GW Mac address: %s" % gw_info.get_gw_mac())
Esempio n. 5
0
    def setUp(self):
        self._br = "t_up_br0"
        try:
            subprocess.check_call(["pkill", "dnsmasq"])
        except subprocess.CalledProcessError:
            pass

        setup_dhcp_server = SCRIPT_PATH + "scripts/setup-test-dhcp-srv.sh"
        subprocess.check_call([setup_dhcp_server, "t0"])

        setup_uplink_br = [SCRIPT_PATH + "scripts/setup-uplink-br.sh",
                           self._br,
                           DHCP_IFACE,
                           "8A:00:00:00:00:01"]
        subprocess.check_call(setup_uplink_br)

        self.dhcp_wait = threading.Condition()
        self.dhcp_store = {}
        self.gw_info_map = {}
        self.gw_info = UplinkGatewayInfo(self.gw_info_map)
        self._dhcp_client = DHCPClient(dhcp_wait=self.dhcp_wait,
                                       dhcp_store=self.dhcp_store,
                                       gw_info=self.gw_info,
                                       iface="t_dhcp0",
                                       lease_renew_wait_min=1)
        self._dhcp_client.run()
Esempio n. 6
0
    def _setup_non_nat_monitoring(self, datapath):
        """
        Setup egress flow to forward traffic to internet GW.
        Start a thread to figure out MAC address of uplink NAT gw.

        :param datapath: datapath to install flows.
        :return: None
        """
        if self._gw_mac_monitor is not None:
            # No need to multiple probes here.
            return
        if self.config.enable_nat is True:
            self.logger.info("Nat is on")
            return
        elif self.config.setup_type != 'LTE':
            self.logger.info("No GW MAC probe for %s", self.config.setup_type)
            return
        else:
            self.logger.info("Non nat conf: Frequency:%s, egress port: %s, uplink: %s",
                             self.config.non_mat_gw_probe_frequency,
                             self.config.non_nat_arp_egress_port,
                             self._uplink_port)

        self._dhcp_gw_info = UplinkGatewayInfo(store.GatewayInfoMap())
        self._gw_mac_monitor = Thread(target=self._monitor_and_update,
                                      args=(datapath,))
        self._gw_mac_monitor.setDaemon(True)
        self._gw_mac_monitor.start()
        threading.Event().wait(1)
Esempio n. 7
0
    def setUp(self):
        self._br = "dh_br0"
        try:
            subprocess.check_call(["pkill", "dnsmasq"])
        except subprocess.CalledProcessError:
            pass

        self.dhcp_wait = threading.Condition()
        self.pkt_list_lock = threading.Condition()
        self.dhcp_store = {}
        self.gw_info_map = {}
        self.gw_info = UplinkGatewayInfo(self.gw_info_map)
Esempio n. 8
0
 def init_store(
     self,
     client: redis.Redis,
 ):
     self.ip_state_map = IpDescriptorMap(
         defaultdict_key(lambda key: ip_states(client, key)), )
     self.ipv6_state_map = IpDescriptorMap(
         defaultdict_key(lambda key: ip_states(client, key)), )
     self.assigned_ip_blocks = AssignedIpBlocksSet(client)
     self.sid_ips_map = IPDescDict(client)
     self.dhcp_gw_info = UplinkGatewayInfo(GatewayInfoMap(client))
     self.dhcp_store = MacToIP(client)  # mac => DHCP_State
     self.allocated_iid = AllocatedIID(client)
     self.sid_session_prefix_allocated = AllocatedSessionPrefix(client)
Esempio n. 9
0
    def setUp(self):
        self._br = "dh_br0"
        self.vlan_sw = "vlan_sw"
        self.up_link_port = ""

        try:
            subprocess.check_call(["pkill", "dnsmasq"])
        except subprocess.CalledProcessError:
            pass

        self.dhcp_wait = threading.Condition()
        self.pkt_list_lock = threading.Condition()
        self.dhcp_store = {}
        self.gw_info_map = {}
        self.gw_info = UplinkGatewayInfo(self.gw_info_map)
        self._sniffer = None
        self._last_xid = -1
Esempio n. 10
0
 def init_store(
     self,
     client: redis.Redis,
 ):
     get_ip_states: Callable[[IPState], Dict[
         str, IPDesc]] = lambda key: ip_states(client, key)
     self.ip_state_map = IpDescriptorMap(
         defaultdict_key(get_ip_states),  # type: ignore[arg-type]
     )
     self.ipv6_state_map = IpDescriptorMap(
         defaultdict_key(get_ip_states),  # type: ignore[arg-type]
     )
     self.assigned_ip_blocks = AssignedIpBlocksSet(client)
     self.sid_ips_map = IPDescDict(client)
     self.dhcp_gw_info = UplinkGatewayInfo(GatewayInfoMap(client))
     self.dhcp_store = MacToIP(client)  # mac => DHCP_State
     self.allocated_iid = AllocatedIID(client)
     self.sid_session_prefix_allocated = AllocatedSessionPrefix(client)
Esempio n. 11
0
class DefGwTest(unittest.TestCase):
    """
    Validate default router setting.
    """
    def setUp(self):
        self.gw_store = defaultdict(str)
        self.dhcp_gw_info = UplinkGatewayInfo(self.gw_store)

    def test_gw_ip_for_DHCP(self):
        self.assertEqual(self.dhcp_gw_info.getIP(), None)
        self.assertEqual(self.dhcp_gw_info.getMac(), None)

    def test_gw_ip_for_Ip_pool(self):
        self.dhcp_gw_info.read_default_gw()

        def_gw_cmd = "ip route show |grep default| awk '{print $3}'"
        p = subprocess.Popen([def_gw_cmd], stdout=subprocess.PIPE, shell=True)
        def_ip = p.stdout.read().decode("utf-8").strip()
        self.assertEqual(self.dhcp_gw_info.getIP(), str(def_ip))
        self.assertEqual(self.dhcp_gw_info.getMac(), None)
Esempio n. 12
0
 def setUp(self):
     self.gw_store = defaultdict(str)
     self.dhcp_gw_info = UplinkGatewayInfo(self.gw_store)
Esempio n. 13
0
class DefGwTest(unittest.TestCase):
    """
    Validate default router setting.
    """

    def setUp(self):
        self.gw_store = defaultdict(str)
        self.dhcp_gw_info = UplinkGatewayInfo(self.gw_store)

    def test_gw_ip_for_DHCP(self):
        self.assertEqual(self.dhcp_gw_info.get_gw_ip(), None)
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(), None)

    def test_gw_ip_for_Ip_pool(self):
        self.dhcp_gw_info.read_default_gw()

        def_gw_cmd = "ip route show |grep default| awk '{print $3}'"
        p = subprocess.Popen(
            [def_gw_cmd],
            stdout=subprocess.PIPE,
            shell=True,
        )
        def_ip = p.stdout.read().decode("utf-8").strip()

        self.assertEqual(self.dhcp_gw_info.get_gw_ip(), str(def_ip))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(), '')
        mac1 = "11:22:33:44:55:66"
        self.dhcp_gw_info.update_mac(def_ip, mac1)
        self.assertEqual(self.dhcp_gw_info.get_gw_ip(), str(def_ip))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(), mac1)

        # updating IP with same address shld keep mac
        self.dhcp_gw_info.update_ip(def_ip)
        self.assertEqual(self.dhcp_gw_info.get_gw_ip(), str(def_ip))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(), mac1)

        ip1 = "1.2.3.4"
        self.dhcp_gw_info.update_ip(ip1)
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(), '')

    def test_vlan_gw_info(self):
        ip1 = "1.2.3.4"
        vlan1 = "1"
        self.dhcp_gw_info.update_ip(ip1, vlan1)
        self.assertEqual(self.dhcp_gw_info.get_gw_ip(vlan1), str(ip1))
        mac1 = "11:22:33:44:55:66"
        self.dhcp_gw_info.update_mac(ip1, mac1, vlan1)
        self.assertEqual(self.dhcp_gw_info.get_gw_ip(vlan1), str(ip1))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(vlan1), mac1)
        # updating IP with same address shld keep mac
        self.dhcp_gw_info.update_mac(ip1, mac1, vlan1)
        self.assertEqual(self.dhcp_gw_info.get_gw_ip(vlan1), str(ip1))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(vlan1), mac1)

        # any IP update shld invalidate MAC address.
        ip2 = "2.2.3.4"
        self.dhcp_gw_info.update_ip(ip2, vlan1)
        self.assertEqual(self.dhcp_gw_info.get_gw_ip(vlan1), str(ip2))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(vlan1), '')

    def test_vlan_gw_info_none(self):
        ip1 = "1.2.3.4"
        vlan1 = "1"
        mac1 = "11:22:33:44:55:66"

        self.dhcp_gw_info.update_mac(ip1, mac1, vlan1)
        self.assertEqual(self.dhcp_gw_info.get_gw_ip(vlan1), str(ip1))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(vlan1), mac1)

        # check None IP or mac updates
        self.dhcp_gw_info.update_ip(None, vlan1)
        self.assertEqual(self.dhcp_gw_info.get_gw_ip(vlan1), str(ip1))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(vlan1), mac1)

        self.dhcp_gw_info.update_mac(ip1, None, vlan1)
        self.assertEqual(self.dhcp_gw_info.get_gw_ip(vlan1), str(ip1))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(vlan1), mac1)

        self.dhcp_gw_info.update_mac(None, mac1, vlan1)
        self.assertEqual(self.dhcp_gw_info.get_gw_ip(vlan1), str(ip1))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(vlan1), mac1)

        self.dhcp_gw_info.update_mac(ip1, '', vlan1)
        self.assertEqual(self.dhcp_gw_info.get_gw_ip(vlan1), str(ip1))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(vlan1), mac1)

        vlan2 = None
        mac2 = "22:22:33:44:55:66"

        self.dhcp_gw_info.update_mac(ip1, mac2, vlan2)
        self.assertEqual(self.dhcp_gw_info.get_gw_ip(vlan2), str(ip1))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(vlan2), mac2)

    def test_vlan_gw_info_list(self):
        ip1 = "1.2.3.4"
        vlan1 = "1"
        gw1 = _get_gw_info(ip1, '', vlan1)
        self.dhcp_gw_info.update_ip(ip1, vlan1)

        ip2 = "2.2.3.4"
        vlan2 = "2"
        gw2 = _get_gw_info(ip2, '', vlan2)
        self.dhcp_gw_info.update_ip(ip2, vlan2)

        ip3 = "3.2.3.4"
        gw3 = _get_gw_info(ip3)
        self.dhcp_gw_info.update_ip(ip3)

        ip4 = "4.2.3.4"
        vlan4 = "4"
        mac4 = "11:22:33:44:55:66"
        gw4 = _get_gw_info(ip4, mac4, vlan4)
        self.dhcp_gw_info.update_mac(ip4, mac4, vlan4)

        ip5 = "2020::1"
        vlan5 = "4"
        mac5 = "11:22:33:44:55:66"
        gw5 = _get_gw_info(ip5, mac5, vlan5)
        self.dhcp_gw_info.update_mac(ip5, mac5, vlan5, IPAddress.IPV6)

        gw_list = self.dhcp_gw_info.get_all_router_ips()

        expected = gw_list_to_set([gw1, gw2, gw3, gw4, gw5])
        self.assertEqual(gw_list_to_set(gw_list), expected)
Esempio n. 14
0
class DefGwTestIpv6(unittest.TestCase):
    """
    Validate v6 router setting.
    """

    @classmethod
    def setUpClass(cls, *_):
        """
        Starts the thread which launches ryu apps

        Create a testing bridge, add a port, setup the port interfaces. Then
        launch the ryu apps for testing pipelined. Gets the references
        to apps launched by using futures.
        """
        super(DefGwTestIpv6, cls).setUpClass()
        uplink_gw.netifaces = MockedNetInterface()

    def setUp(self):
        self.gw_store = defaultdict(str)
        self.dhcp_gw_info = UplinkGatewayInfo(self.gw_store)

    def test_gw_ip_for_DHCP(self):
        self.assertEqual(self.dhcp_gw_info.get_gw_ip(), None)
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(), None)

    def test_gw_ip_for_Ip_pool(self):
        self.dhcp_gw_info.read_default_gw()
        def_ip = '10.0.2.3'

        self.assertEqual(self.dhcp_gw_info.get_gw_ip(), str(def_ip))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(), '')
        mac1 = "11:22:33:44:55:66"
        self.dhcp_gw_info.update_mac(def_ip, mac1)
        self.assertEqual(self.dhcp_gw_info.get_gw_ip(), str(def_ip))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(), mac1)

        # updating IP with same address shld keep mac
        self.dhcp_gw_info.update_ip(def_ip)
        self.assertEqual(self.dhcp_gw_info.get_gw_ip(), str(def_ip))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(), mac1)

        ip1 = "1.2.3.4"
        self.dhcp_gw_info.update_ip(ip1)
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(), '')

    def test_gw_ip_for_Ip_pool_v6(self):
        self.dhcp_gw_info.read_default_gw_v6()
        def_ip_v6 = '2002::1'

        self.assertEqual(self.dhcp_gw_info.get_gw_ip(version=IPAddress.IPV6), str(def_ip_v6))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(version=IPAddress.IPV6), '')
        mac6 = "11:22:33:44:55:66"
        self.dhcp_gw_info.update_mac(def_ip_v6, mac6, version=IPAddress.IPV6)
        self.assertEqual(self.dhcp_gw_info.get_gw_ip(version=IPAddress.IPV6), str(def_ip_v6))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(version=IPAddress.IPV6), mac6)

        self.dhcp_gw_info.read_default_gw()
        def_ip = '10.0.2.3'

        self.assertEqual(self.dhcp_gw_info.get_gw_ip(), str(def_ip))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(), '')
        mac4 = "11:22:33:44:55:44"
        self.dhcp_gw_info.update_mac(def_ip, mac4)
        self.assertEqual(self.dhcp_gw_info.get_gw_ip(), str(def_ip))
        self.assertEqual(self.dhcp_gw_info.get_gw_mac(), mac4)

        ip4 = def_ip
        mac4 = mac4
        gw4 = _get_gw_info(ip4, mac4)

        ip6 = def_ip_v6
        mac6 = mac6
        gw6 = _get_gw_info(ip6, mac6)

        gw_list = self.dhcp_gw_info.get_all_router_ips()

        expected = gw_list_to_set([gw4, gw6])
        self.assertEqual(gw_list_to_set(gw_list), expected)
Esempio n. 15
0
    def test_ip_alloc(self):
        sid1 = "IMSI02917"
        ip1 = self._dhcp_allocator.alloc_ip_address(sid1)
        threading.Event().wait(2)
        gw_map = store.GatewayInfoMap()
        dhcp_gw_info = UplinkGatewayInfo(gw_map)
        dhcp_store = store.MacToIP()  # mac => DHCP_State

        self.assertEqual(str(dhcp_gw_info.getIP()), "192.168.128.211")
        self._dhcp_allocator.release_ip_address(sid1, ip1)

        # wait for DHCP release
        threading.Event().wait(7)
        mac1 = create_mac_from_sid(sid1)
        dhcp_state1 = dhcp_store.get(mac1.as_redis_key())

        self.assertEqual(dhcp_state1.state, DHCPState.RELEASE)

        ip1_1 = self._dhcp_allocator.alloc_ip_address(sid1)
        threading.Event().wait(2)
        self.assertEqual(str(ip1), str(ip1_1))

        self._dhcp_allocator.release_ip_address(sid1, ip1_1)
        threading.Event().wait(5)
        self.assertEqual(self._dhcp_allocator.list_added_ip_blocks(), [])

        ip1 = self._dhcp_allocator.alloc_ip_address("IMSI02918")
        self.assertEqual(str(ip1), "192.168.128.146")
        self.assertEqual(self._dhcp_allocator.list_added_ip_blocks(),
                         [ip_network('192.168.128.0/24')])

        ip2 = self._dhcp_allocator.alloc_ip_address("IMSI029192")
        self.assertNotEqual(ip1, ip2)

        ip3 = self._dhcp_allocator.alloc_ip_address("IMSI0432")
        self.assertNotEqual(ip1, ip3)
        self.assertNotEqual(ip2, ip3)
        # release unallocated IP of SID
        self._dhcp_allocator.ip_allocator.release_ip("IMSI033", ip3,
                                                     ip_network("1.1.1.0/24"))
        self.assertEqual(self._dhcp_allocator.list_added_ip_blocks(),
                         [ip_network('192.168.128.0/24')])

        sid4 = "IMSI54321"
        ip4 = self._dhcp_allocator.alloc_ip_address(sid4)
        threading.Event().wait(1)
        self._dhcp_allocator.release_ip_address(sid4, ip4)
        self.assertEqual(self._dhcp_allocator.list_added_ip_blocks(),
                         [ip_network('192.168.128.0/24')])

        # wait for DHCP release
        threading.Event().wait(7)
        mac4 = create_mac_from_sid(sid4)
        dhcp_state = dhcp_store.get(mac4.as_redis_key())

        self.assertEqual(dhcp_state.state, DHCPState.RELEASE)
        ip4_2 = self._dhcp_allocator.alloc_ip_address(sid4)
        self.assertEqual(ip4, ip4_2)

        try:
            self._dhcp_allocator.release_ip_address(sid1, ip1)
            self.assertEqual("should not", "reach here")
        except MappingNotFoundError:
            pass