def get_subscriber_table(self): chan = ServiceRegistry.get_rpc_channel('mobilityd', ServiceRegistry.LOCAL) client = MobilityServiceStub(chan) table = client.GetSubscriberIPTable(Void()) return table.entries
async def get_ping_targets(self, service_loop) -> PingedTargets: """ Sends gRPC call to mobilityd to get all subscribers table. Returns: List of [Subscriber ID => IP address, APN] entries """ ping_addresses = self.ping_addresses.copy() ping_targets = self.ping_targets.copy() try: mobilityd_chan = ServiceRegistry.get_rpc_channel( 'mobilityd', ServiceRegistry.LOCAL, ) mobilityd_stub = MobilityServiceStub(mobilityd_chan) response = await grpc_async_wrapper( mobilityd_stub.GetSubscriberIPTable.future( Void(), 10, ), service_loop, ) for sub in response.entries: ip = _get_addr_from_subscribers(sub.ip) ping_addresses.append(ip) ping_targets[sub.sid.id] = ip except grpc.RpcError as err: logging.error( "GetSubscribers Error for %s! %s", err.code(), err.details(), extra=SEND_TO_ERROR_MONITORING, ) return PingedTargets(ping_targets, ping_addresses)
def setUp(self): # Bind the rpc server to a free port thread_pool = futures.ThreadPoolExecutor(max_workers=10) self._rpc_server = grpc.server(thread_pool) port = self._rpc_server.add_insecure_port('0.0.0.0:0') # Create a mock "mconfig" for the servicer to use mconfig = unittest.mock.Mock() mconfig.ip_block = None # Add the servicer config = {'persist_to_redis': False, 'redis_port': None} self._servicer = MobilityServiceRpcServicer(mconfig, config) self._servicer.add_to_server(self._rpc_server) self._rpc_server.start() # Create a rpc stub channel = grpc.insecure_channel('0.0.0.0:{}'.format(port)) self._stub = MobilityServiceStub(channel) # variables shared across tests self._netaddr = '192.168.0.0' self._prefix_len = 31 ip_bytes = bytes(map(int, self._netaddr.split('.'))) self._block_msg = IPBlock(version=IPBlock.IPV4, net_address=ip_bytes, prefix_len=self._prefix_len) self._block = ipaddress.ip_network("%s/%s" % (self._netaddr, self._prefix_len)) self._sid0 = SIDUtils.to_pb('IMSI0') self._sid1 = SIDUtils.to_pb('IMSI1') self._sid2 = SIDUtils.to_pb('IMSI2')
def load(self): """ Instantiates and schedules the Ryu app eventlets in the service eventloop. """ # Some setups might not use REDIS if (self._magma_service.config['redis_enabled']): # Wait for redis as multiple controllers rely on it while not redisAvailable(self.rule_id_mapper.redis_cli): logging.warning("Pipelined waiting for redis...") time.sleep(1) else: self.rule_id_mapper._rule_nums_by_rule = {} self.rule_id_mapper._rules_by_rule_num = {} self.session_rule_version_mapper._version_by_imsi_and_rule = {} self.interface_to_prefix_mapper._prefix_by_interface = {} manager = AppManager.get_instance() manager.load_apps([app.module for app in self._apps]) contexts = manager.create_contexts() contexts['rule_id_mapper'] = self.rule_id_mapper contexts[ 'session_rule_version_mapper'] = self.session_rule_version_mapper contexts[ 'interface_to_prefix_mapper'] = self.interface_to_prefix_mapper contexts['app_futures'] = {app.name: Future() for app in self._apps} contexts['internal_ip_allocator'] = \ InternalIPAllocator(self._magma_service.config) contexts['config'] = self._magma_service.config contexts['mconfig'] = self._magma_service.mconfig contexts['loop'] = self._magma_service.loop contexts['service_manager'] = self sessiond_chan = ServiceRegistry.get_rpc_channel( 'sessiond', ServiceRegistry.LOCAL) mobilityd_chan = ServiceRegistry.get_rpc_channel( 'mobilityd', ServiceRegistry.LOCAL) contexts['rpc_stubs'] = { 'mobilityd': MobilityServiceStub(mobilityd_chan), 'sessiond': LocalSessionManagerStub(sessiond_chan), } # Instantiate and schedule apps for app in manager.instantiate_apps(**contexts): # Wrap the eventlet in asyncio so it will stop when the loop is # stopped future = aioeventlet.wrap_greenthread(app, self._magma_service.loop) # Schedule the eventlet for evaluation in service loop asyncio.ensure_future(future) # In development mode, run server so that if environment.is_dev_mode(): server_thread = of_rest_server.start(manager) future = aioeventlet.wrap_greenthread(server_thread, self._magma_service.loop) asyncio.ensure_future(future)
def main(): parser = create_parser() # Parse the args args = parser.parse_args() if not args.cmd: parser.print_usage() exit(1) print('Preparing %s load test...' % args.cmd) client = MobilityServiceStub( ServiceRegistry.get_rpc_channel( MOBILITYD_SERVICE_NAME, ServiceRegistry.LOCAL, ), ) if args.cmd == 'allocate': _cleanup_subs() _setup_ip_block(client) input_file = 'allocate_data.json' request_type = 'AllocateIPAddress' _build_allocate_ip_data(args.num, input_file) benchmark_grpc_request( proto_path=PROTO_PATH, full_request_type=make_full_request_type( MOBILITYD_SERVICE_RPC_PATH, request_type, ), input_file=input_file, output_file=make_output_file_path(request_type), num_reqs=args.num, address=MOBILITYD_PORT, import_path=args.import_path, ) _cleanup_subs() elif args.cmd == 'release': input_file = 'release_data.json' request_type = 'ReleaseIPAddress' _build_release_ip_data(client, input_file) benchmark_grpc_request( proto_path=PROTO_PATH, full_request_type=make_full_request_type( MOBILITYD_SERVICE_RPC_PATH, request_type, ), input_file=input_file, output_file=make_output_file_path(request_type), num_reqs=args.num, address=MOBILITYD_PORT, import_path=args.import_path, ) print('Done')
def setUp(self): # Bind the rpc server to a free port thread_pool = futures.ThreadPoolExecutor(max_workers=10) self._rpc_server = grpc.server(thread_pool) port = self._rpc_server.add_insecure_port('0.0.0.0:0') store = MobilityStore(get_default_client(), False, 3980) store.dhcp_gw_info.read_default_gw() ip_allocator = IpAllocatorPool(store) ipv6_allocator = IPv6AllocatorPool( store, session_prefix_alloc_mode='RANDOM', ) self._allocator = IPAddressManager( ip_allocator, ipv6_allocator, store, ) # Add the servicer self._servicer = MobilityServiceRpcServicer(self._allocator, False) self._servicer.add_to_server(self._rpc_server) self._rpc_server.start() # Create a rpc stub channel = grpc.insecure_channel('0.0.0.0:{}'.format(port)) self._stub = MobilityServiceStub(channel) # variables shared across tests self._netaddr = '192.168.0.0' self._prefix_len = 28 ip_bytes = bytes(map(int, self._netaddr.split('.'))) self._block_msg = IPBlock( version=IPBlock.IPV4, net_address=ip_bytes, prefix_len=self._prefix_len, ) self._ipv6_block = ipaddress.ip_network('fdee:5:6c::/48') self._ipv6_netaddr = self._ipv6_block.network_address.packed self._ipv6_block_msg = IPBlock( version=IPBlock.IPV6, net_address=self._ipv6_netaddr, prefix_len=self._ipv6_block.prefixlen, ) self._block = ipaddress.ip_network( "%s/%s" % (self._netaddr, self._prefix_len), ) self._sid0 = SIDUtils.to_pb('IMSI0') self._sid1 = SIDUtils.to_pb('IMSI1') self._sid2 = SIDUtils.to_pb('IMSI2') self._apn0 = 'Internet' self._apn1 = 'IMS'
def set_mobilityd_gw_info(ip: IPAddress, mac: str, vlan: str): """ Make RPC call to 'SetGatewayInfo' method of local mobilityD service """ try: chan = ServiceRegistry.get_rpc_channel(SERVICE_NAME, ServiceRegistry.LOCAL) except ValueError: logging.error('Cant get RPC channel to %s', SERVICE_NAME) return client = MobilityServiceStub(chan) try: gwinfo = GWInfo(ip=ip, mac=mac, vlan=vlan) client.SetGatewayInfo(gwinfo) except grpc.RpcError as err: logging.error("SetGatewayInfo error[%s] %s", err.code(), err.details())
def mobilityd_list_ip_blocks(): """ Make RPC call to query all ip-blocks. """ try: chan = ServiceRegistry.get_rpc_channel(SERVICE_NAME, ServiceRegistry.LOCAL) except ValueError: logging.error('Cant get RPC channel to %s', SERVICE_NAME) return client = MobilityServiceStub(chan) try: resp = client.ListAddedIPv4Blocks(Void()) return resp except grpc.RpcError as err: logging.error("List IpBlock error[%s] %s", err.code(), err.details())
def set_mobilityd_gw_info(gwinfo: GWInfo): """ Make RPC call to 'SetGatewayInfo' method of local mobilityD service """ try: chan = ServiceRegistry.get_rpc_channel(SERVICE_NAME, ServiceRegistry.LOCAL) except ValueError: logging.error('Cant get RPC channel to %s', SERVICE_NAME) return client = MobilityServiceStub(chan) try: client.SetGatewayInfo(gwinfo) except grpc.RpcError as err: logging.error("GetGatewayInfoRequest error[%s] %s", err.code(), err.details())
def load(self): """ Instantiates and schedules the Ryu app eventlets in the service eventloop. """ manager = AppManager.get_instance() manager.load_apps([app.module for app in self._apps]) contexts = manager.create_contexts() contexts['rule_id_mapper'] = RuleIDToNumMapper() contexts[ 'session_rule_version_mapper'] = self.session_rule_version_mapper contexts['app_futures'] = {app.name: Future() for app in self._apps} contexts['config'] = self._magma_service.config contexts['mconfig'] = self._magma_service.mconfig contexts['loop'] = self._magma_service.loop contexts['service_manager'] = self records_chan = ServiceRegistry.get_rpc_channel('meteringd_records', ServiceRegistry.CLOUD) sessiond_chan = ServiceRegistry.get_rpc_channel( 'sessiond', ServiceRegistry.LOCAL) mobilityd_chan = ServiceRegistry.get_rpc_channel( 'mobilityd', ServiceRegistry.LOCAL) contexts['rpc_stubs'] = { 'metering_cloud': MeteringdRecordsControllerStub(records_chan), 'mobilityd': MobilityServiceStub(mobilityd_chan), 'sessiond': LocalSessionManagerStub(sessiond_chan), } # Instantiate and schedule apps for app in manager.instantiate_apps(**contexts): # Wrap the eventlet in asyncio so it will stop when the loop is # stopped future = aioeventlet.wrap_greenthread(app, self._magma_service.loop) # Schedule the eventlet for evaluation in service loop asyncio.ensure_future(future) # In development mode, run server so that if environment.is_dev_mode(): server_thread = of_rest_server.start(manager) future = aioeventlet.wrap_greenthread(server_thread, self._magma_service.loop) asyncio.ensure_future(future)
def get_allocated_ips(): chan = ServiceRegistry.get_rpc_channel('mobilityd', ServiceRegistry.LOCAL) client = MobilityServiceStub(chan) res = [] list_blocks_resp = client.ListAddedIPv4Blocks(Void()) for block_msg in list_blocks_resp.ip_block_list: list_ips_resp = client.ListAllocatedIPs(block_msg) for ip_msg in list_ips_resp.ip_list: if ip_msg.version == IPAddress.IPV4: ip = ipaddress.IPv4Address(ip_msg.address) elif ip_msg.address == IPAddress.IPV6: ip = ipaddress.IPv6Address(ip_msg.address) else: continue res.append(ip) return res
async def _get_subscribers(self) -> List[IPAddress]: """ Sends gRPC call to mobilityd to get all subscribers table. Returns: List of [Subscriber ID => IP address, APN] entries """ try: mobilityd_chan = ServiceRegistry.get_rpc_channel( 'mobilityd', ServiceRegistry.LOCAL) mobilityd_stub = MobilityServiceStub(mobilityd_chan) response = await grpc_async_wrapper( mobilityd_stub.GetSubscriberIPTable.future( Void(), TIMEOUT_SECS), self._loop) return response.entries except grpc.RpcError as err: logging.error("GetSubscribers Error for %s! %s", err.code(), err.details()) return []
def get_mobilityd_gw_info() -> List[GWInfo]: """ Make RPC call to 'GetGatewayInfo' method of local mobilityD service """ try: chan = ServiceRegistry.get_rpc_channel(SERVICE_NAME, ServiceRegistry.LOCAL) except ValueError: logging.error('Cant get RPC channel to %s', SERVICE_NAME) return GWInfo() client = MobilityServiceStub(chan) try: return client.ListGatewayInfo(Void()).gw_list except grpc.RpcError as err: logging.error("ListGatewayInfo error[%s] %s", err.code(), err.details()) return []
async def get_ping_targets(self, service_loop) -> List[IPAddress]: """ Sends gRPC call to mobilityd to get all subscribers table. Returns: List of [Subscriber ID => IP address, APN] entries """ try: mobilityd_chan = ServiceRegistry.get_rpc_channel( 'mobilityd', ServiceRegistry.LOCAL) mobilityd_stub = MobilityServiceStub(mobilityd_chan) response = await grpc_async_wrapper( mobilityd_stub.GetSubscriberIPTable.future(Void(), 10), service_loop) for sub in response.entries: ip = _get_addr_from_subscribers(sub.ip) self.ping_addresses.append(ip) self.ping_targets[sub.sid.id] = ip except grpc.RpcError as err: logging.error("GetSubscribers Error for %s! %s", err.code(), err.details()) return self.ping_targets, self.ping_addresses
def get_mobilityd_gw_info() -> List[GWInfo]: """ Make RPC call to 'GetGatewayInfo' method of local mobilityD service """ try: chan = ServiceRegistry.get_rpc_channel( SERVICE_NAME, ServiceRegistry.LOCAL, ) except ValueError: logging.error('Cant get RPC channel to %s', SERVICE_NAME) return GWInfo() client = MobilityServiceStub(chan) try: return client.ListGatewayInfo(Void()).gw_list except grpc.RpcError as err: logging.error( "ListGatewayInfo error[%s] %s", err.code(), err.details(), extra=EXCLUDE_FROM_ERROR_MONITORING if indicates_connection_error(err) else None, ) return []
def __init__(self): """ Init the gRPC stub. """ self._mobility_stub = MobilityServiceStub(get_rpc_channel("mobilityd"))