Example #1
0
    def __init__(self,
                 *,
                 recycling_interval: int = DEFAULT_IP_RECYCLE_INTERVAL,
                 persist_to_redis: bool = True,
                 redis_port: int = 6379):
        """ Initializes a new IP allocator

        Args:
            recycling_interval (number): minimum time, in seconds, before a
                released IP is recycled and freed into the pool of available
                IPs.

                Default: None, no recycling will occur automatically.
            persist_to_redis (bool): store all state in local process if falsy,
                else write state to Redis service
        """
        logging.debug('Persist to Redis: %s', persist_to_redis)
        self._lock = threading.RLock()  # re-entrant locks

        self._recycle_timer = None  # reference to recycle timer
        self._recycling_interval_seconds = recycling_interval

        if not persist_to_redis:
            self._assigned_ip_blocks = set()  # {ip_block}
            self._ip_states = defaultdict(dict)  # {state=>{ip=>ip_desc}}
            self._sid_ips_map = defaultdict(IPDesc)  # {SID=>IPDesc}
        else:
            if not redis_port:
                raise ValueError(
                    'Must specify a redis_port in mobilityd config.')
            client = redis.Redis(host='localhost', port=redis_port)
            self._assigned_ip_blocks = store.AssignedIpBlocksSet(client)
            self._ip_states = store.defaultdict_key(
                lambda key: store.ip_states(client, key))
            self._sid_ips_map = store.IPDescDict(client)
Example #2
0
    def __init__(self,
                 *,
                 config,
                 recycling_interval: int = DEFAULT_IP_RECYCLE_INTERVAL):
        """ Initializes a new IP allocator

        Args:
            recycling_interval (number): minimum time, in seconds, before a
                released IP is recycled and freed into the pool of available
                IPs.

                Default: None, no recycling will occur automatically.
        """

        persist_to_redis = config.get('persist_to_redis', True)
        redis_port = config.get('redis_port', 6379)
        if 'allocator_type' in config:
            if config['allocator_type'] == 'ip_pool':
                self.allocator_type = IPAllocatorType.IP_POOL
            elif config['allocator_type'] == 'dhcp':
                self.allocator_type = IPAllocatorType.DHCP
            else:
                raise ArgumentError("unknown allocator config {}"
                                    .format(config['allocator_type']))
        else:
            self.allocator_type = IPAllocatorType.IP_POOL

        logging.debug('Persist to Redis: %s', persist_to_redis)
        self._lock = threading.RLock()  # re-entrant locks

        self._recycle_timer = None  # reference to recycle timer
        self._recycling_interval_seconds = recycling_interval

        if not persist_to_redis:
            self._assigned_ip_blocks = set()  # {ip_block}
            self.sid_ips_map = defaultdict(IPDesc)  # {SID=>IPDesc}
        else:
            if not redis_port:
                raise ValueError(
                    'Must specify a redis_port in mobilityd config.')
            client = get_default_client()
            self._assigned_ip_blocks = store.AssignedIpBlocksSet(client)
            self.sid_ips_map = store.IPDescDict(client)

        self.ip_state_map = IpDescriptorMap(persist_to_redis, redis_port)
        if self.allocator_type == IPAllocatorType.IP_POOL:
            self.ip_allocator = IpAllocatorStatic(self._assigned_ip_blocks,
                                                  self.ip_state_map,
                                                  self.sid_ips_map)
        elif self.allocator_type == IPAllocatorType.DHCP:
            dhcp_store = store.MacToIP()  # mac => DHCP_State
            dhcp_gw_info = UplinkGatewayInfo(store.GatewayInfoMap())
            iface = config.get('dhcp_iface', 'dhcp0')
            retry_limit = config.get('retry_limit', 300)
            self.ip_allocator = IPAllocatorDHCP(self._assigned_ip_blocks,
                                                self.ip_state_map,
                                                iface=iface,
                                                retry_limit=retry_limit,
                                                dhcp_store=dhcp_store,
                                                gw_info=dhcp_gw_info)
Example #3
0
    def __init__(self,
                 *,
                 config,
                 mconfig,
                 subscriberdb_rpc_stub=None,
                 recycling_interval: int = DEFAULT_IP_RECYCLE_INTERVAL):
        """ Initializes a new IP allocator

        Args:
            recycling_interval (number): minimum time, in seconds, before a
                released IP is recycled and freed into the pool of available
                IPs.

                Default: None, no recycling will occur automatically.
        """

        persist_to_redis = config.get('persist_to_redis', True)
        redis_port = config.get('redis_port', 6379)

        self.allocator_type = mconfig.ip_allocator_type
        logging.debug('Persist to Redis: %s', persist_to_redis)
        self._lock = threading.RLock()  # re-entrant locks

        self._recycle_timer = None  # reference to recycle timer
        self._recycling_interval_seconds = recycling_interval
        self.static_ip_enabled = mconfig.static_ip_enabled

        if not persist_to_redis:
            self._assigned_ip_blocks = set()  # {ip_block}
            self.sid_ips_map = defaultdict(IPDesc)  # {SID=>IPDesc}
            self._dhcp_gw_info = UplinkGatewayInfo(defaultdict(str))
            self._dhcp_store = {}  # mac => DHCP_State
        else:
            if not redis_port:
                raise ValueError(
                    'Must specify a redis_port in mobilityd config.')
            client = get_default_client()
            self._assigned_ip_blocks = store.AssignedIpBlocksSet(client)
            self.sid_ips_map = store.IPDescDict(client)
            self._dhcp_gw_info = UplinkGatewayInfo(store.GatewayInfoMap())
            self._dhcp_store = store.MacToIP()  # mac => DHCP_State

        self.ip_state_map = IpDescriptorMap(persist_to_redis, redis_port)
        logging.info("Using allocator: %s static ip: %s", self.allocator_type,
                     self.static_ip_enabled)

        if self.allocator_type == MobilityD.IP_POOL:
            self._dhcp_gw_info.read_default_gw()
            ip_allocator = IpAllocatorPool(self._assigned_ip_blocks,
                                           self.ip_state_map, self.sid_ips_map)
        elif self.allocator_type == MobilityD.DHCP:
            iface = config.get('dhcp_iface', 'dhcp0')
            retry_limit = config.get('retry_limit', 300)
            ip_allocator = IPAllocatorDHCP(self._assigned_ip_blocks,
                                           self.ip_state_map,
                                           iface=iface,
                                           retry_limit=retry_limit,
                                           dhcp_store=self._dhcp_store,
                                           gw_info=self._dhcp_gw_info)

        if self.static_ip_enabled:
            self.ip_allocator = IPAllocatorStaticWrapper(
                subscriberdb_rpc_stub=subscriberdb_rpc_stub,
                ip_allocator=ip_allocator)
        else:
            self.ip_allocator = ip_allocator