def get_context(): """ Create the context to be used for nghttpx, other than the one provided by the configs. """ context = {} context['backends'] = [] for service in ServiceRegistry.list_services(): (ip_address, port) = ServiceRegistry.get_service_address(service) backend = {'service': service, 'ip': ip_address, 'port': port} context['backends'].append(backend) # We get the gateway cert after bootstrapping, but we do want nghttpx # to run before that for communication locally. Update the flag for # jinja to act upon. gateway_cert = get_service_config_value('control_proxy', 'gateway_cert', None) if gateway_cert and os.path.exists(gateway_cert): context['use_gateway_cert'] = True else: context['use_gateway_cert'] = False context['dev_mode'] = is_dev_mode() context['allow_http_proxy'] = get_service_config_value( 'control_proxy', 'allow_http_proxy', False) context['http_proxy'] = os.getenv('http_proxy', '') return context
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 = {} self.tunnel_id_mapper._tunnel_map = {} 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['tunnel_id_mapper'] = self.tunnel_id_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(): """ Loads the Ryu apps we want to run from the config file. This should exit on keyboard interrupt. """ # Run asyncio loop in a greenthread so we can evaluate other eventlets # TODO: Remove once Ryu migrates to asyncio asyncio.set_event_loop_policy(aioeventlet.EventLoopPolicy()) service = MagmaService('pipelined') service_config = service.config if environment.is_dev_mode(): of_rest_server.configure(service_config) # Set Ryu config params cfg.CONF.ofp_listen_host = "127.0.0.1" # Load the ryu apps service_manager = ServiceManager(service) service_manager.load() def callback(returncode): if returncode != 0: logging.error( "Failed to set MASQUERADE: %d", returncode ) if service.mconfig.nat_enabled: call_process('iptables -t nat -A POSTROUTING -o %s -j MASQUERADE' % service.config['nat_iface'], callback, service.loop ) service.loop.create_task(monitor_ifaces( service.config['monitored_ifaces'], service.loop), ) manager = AppManager.get_instance() # Add pipelined rpc servicer pipelined_srv = PipelinedRpcServicer( service.loop, manager.applications.get('MeterStatsController', None), manager.applications.get('EnforcementController', None), manager.applications.get('EnforcementStatsController', None), manager.applications.get('DPIController', None), service_manager) pipelined_srv.add_to_server(service.rpc_server) # Run the service loop service.run() # Cleanup the service service.close()
def create_fake_user(args): output('Creating fake_user...') # Create the OVS port and interface run('sudo ovs-vsctl add-port gtp_br0 fake_user -- ' 'set interface fake_user type=internal', ignore_errors=True) run('sudo ifconfig fake_user up %s netmask 255.255.255.0' % args.iface_ip) if environment.is_dev_mode(): # Make sure we can NAT to the internet run('sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE') # Create table 0 flows to set the metadata, and to replace with iface_ip metadata = encode_imsi(args.imsi) add_flow(0, 'ip,ip_src=%s' % args.iface_ip, 'set_field:%s->metadata,resubmit(,1)' % (metadata)) iface_mac = mac_from_iface('fake_user') add_flow(0, 'ip,ip_dst=%s' % args.user_ip, 'set_field:%s->metadata,set_field:%s->ip_dst,' 'set_field:%s->eth_dst,resubmit(,1)' % (metadata, args.iface_ip, iface_mac)) # Create a table 1 flow to set 0x1 as direction bit for pkts from fake_user add_flow(1, 'in_port=fake_user', 'set_field:0x1->reg1,resubmit(,2)') # Create a table 2 flow to respond to ARP requests from the fake_user # This doesn't happen with GTP, since the UEs by default send it to the # tunnel. Here we manually set the next hop as gtp_br0. gateway_mac = mac_from_iface('gtp_br0') add_flow(2, 'arp,reg1=0x1', 'move:NXM_OF_ETH_SRC[]->NXM_OF_ETH_DST[],mod_dl_src:%s,' 'load:0x2->NXM_OF_ARP_OP[],' 'load:0x3243f1395144->NXM_NX_ARP_SHA[],' 'move:NXM_NX_ARP_SHA[]->NXM_NX_ARP_THA[],' 'move:NXM_OF_ARP_TPA[]->NXM_NX_REG0[],' 'move:NXM_OF_ARP_SPA[]->NXM_OF_ARP_TPA[],' 'move:NXM_NX_REG0[]->NXM_OF_ARP_SPA[],IN_PORT' % gateway_mac) # Create a table 20 flow to output the pkts to face_user after swapping # back the destination ip. add_flow(20, 'ip,ip_src=%s' % args.iface_ip, 'set_field:%s->ip_src,output:LOCAL' % (args.user_ip)) add_flow(20, 'ip,ip_dst=%s' % args.iface_ip, 'output:fake_user') # Create session with PCEF if needed if args.pcef: pcef_create_session(args) output('fake_user interface created successfully!!\n', 'green') output(sample_commands(), 'green')
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 main(): """ Loads the Ryu apps we want to run from the config file. This should exit on keyboard interrupt. """ # Run asyncio loop in a greenthread so we can evaluate other eventlets # TODO: Remove once Ryu migrates to asyncio asyncio.set_event_loop_policy(aioeventlet.EventLoopPolicy()) service = MagmaService('pipelined', mconfigs_pb2.PipelineD()) # Optionally pipe errors to Sentry sentry_init(service_name=service.name) service_config = service.config if environment.is_dev_mode(): of_rest_server.configure(service_config) # Set Ryu config params cfg.CONF.ofp_listen_host = "127.0.0.1" # override mconfig using local config. # TODO: move config compilation to separate module. enable_nat = service.config.get('enable_nat', service.mconfig.nat_enabled) service.config['enable_nat'] = enable_nat logging.info("Nat: %s", enable_nat) vlan_tag = service.config.get( 'sgi_management_iface_vlan', service.mconfig.sgi_management_iface_vlan, ) service.config['sgi_management_iface_vlan'] = vlan_tag sgi_ip = service.config.get( 'sgi_management_iface_ip_addr', service.mconfig.sgi_management_iface_ip_addr, ) service.config['sgi_management_iface_ip_addr'] = sgi_ip sgi_gateway_ip = service.config.get( 'sgi_management_iface_gw', service.mconfig.sgi_management_iface_gw, ) service.config['sgi_management_iface_gw'] = sgi_gateway_ip # Keep router mode off for smooth upgrade path service.config['dp_router_enabled'] = service.config.get( 'dp_router_enabled', False, ) if 'virtual_mac' not in service.config: if service.config['dp_router_enabled']: up_bridge_name = service.config.get( 'uplink_bridge', UPLINK_OVS_BRIDGE_NAME, ) mac_addr = get_if_hwaddr(up_bridge_name) else: mac_addr = get_if_hwaddr(service.config.get('bridge_name')) service.config['virtual_mac'] = mac_addr # this is not read from yml file. service.config['uplink_port'] = OFPP_LOCAL uplink_port_name = service.config.get('ovs_uplink_port_name', None) if enable_nat is False and uplink_port_name is not None: service.config['uplink_port'] = BridgeTools.get_ofport( uplink_port_name, ) # header enrichment related configuration. service.config['proxy_port_name'] = PROXY_PORT_NAME he_enabled_flag = False if service.mconfig.he_config: he_enabled_flag = service.mconfig.he_config.enable_header_enrichment he_enabled = service.config.get('he_enabled', he_enabled_flag) service.config['he_enabled'] = he_enabled # monitoring related configuration mtr_interface = service.config.get('mtr_interface', None) if mtr_interface: mtr_ip = get_ip_from_if(mtr_interface) service.config['mtr_ip'] = mtr_ip # Load the ryu apps service_manager = ServiceManager(service) service_manager.load() def callback(returncode): if returncode != 0: logging.error( "Failed to set MASQUERADE: %d", returncode, ) # TODO fix this hack for XWF if enable_nat is True or service.config.get('setup_type') == 'XWF': call_process( 'iptables -t nat -A POSTROUTING -o %s -j MASQUERADE' % service.config['nat_iface'], callback, service.loop, ) service.loop.create_task( monitor_ifaces( service.config['monitored_ifaces'], ), ) manager = AppManager.get_instance() # Add pipelined rpc servicer pipelined_srv = PipelinedRpcServicer( service.loop, manager.applications.get('GYController', None), manager.applications.get('EnforcementController', None), manager.applications.get('EnforcementStatsController', None), manager.applications.get('DPIController', None), manager.applications.get('UEMacAddressController', None), manager.applications.get('CheckQuotaController', None), manager.applications.get('IPFIXController', None), manager.applications.get('VlanLearnController', None), manager.applications.get('TunnelLearnController', None), manager.applications.get('Classifier', None), manager.applications.get('InOutController', None), manager.applications.get('NGServiceController', None), service.config, service_manager, ) pipelined_srv.add_to_server(service.rpc_server) if service.config['setup_type'] == 'CWF': bridge_ip = service.config['bridge_ip_address'] has_quota_port = service.config['has_quota_port'] no_quota_port = service.config['no_quota_port'] def on_exit_server_thread(): service.StopService(None, None) # For CWF start quota check servers start_check_quota_server( run_flask, bridge_ip, has_quota_port, True, on_exit_server_thread, ) start_check_quota_server( run_flask, bridge_ip, no_quota_port, False, on_exit_server_thread, ) if service.config['setup_type'] == 'LTE': polling_interval = service.config.get( 'ovs_gtp_stats_polling_interval', MIN_OVSDB_DUMP_POLLING_INTERVAL, ) collector = GTPStatsCollector( polling_interval, service.loop, ) collector.start() # Run the service loop service.run() # Cleanup the service service.close()
def main(): """ Loads the Ryu apps we want to run from the config file. This should exit on keyboard interrupt. """ # Run asyncio loop in a greenthread so we can evaluate other eventlets # TODO: Remove once Ryu migrates to asyncio asyncio.set_event_loop_policy(aioeventlet.EventLoopPolicy()) service = MagmaService('pipelined', mconfigs_pb2.PipelineD()) service_config = service.config if environment.is_dev_mode(): of_rest_server.configure(service_config) # Set Ryu config params cfg.CONF.ofp_listen_host = "127.0.0.1" # Load the ryu apps service_manager = ServiceManager(service) service_manager.load() def callback(returncode): if returncode != 0: logging.error( "Failed to set MASQUERADE: %d", returncode ) if service.mconfig.nat_enabled: call_process('iptables -t nat -A POSTROUTING -o %s -j MASQUERADE' % service.config['nat_iface'], callback, service.loop ) service.loop.create_task(monitor_ifaces( service.config['monitored_ifaces'], service.loop), ) manager = AppManager.get_instance() # Add pipelined rpc servicer pipelined_srv = PipelinedRpcServicer( service.loop, manager.applications.get('MeterStatsController', None), manager.applications.get('EnforcementController', None), manager.applications.get('EnforcementStatsController', None), manager.applications.get('DPIController', None), manager.applications.get('UEMacAddressController', None), manager.applications.get('CheckQuotaController', None), service_manager) pipelined_srv.add_to_server(service.rpc_server) if service.config['setup_type'] == 'CWF': bridge_ip = service.config['bridge_ip_address'] has_quota_port = service.config['has_quota_port'] no_quota_port = service.config['no_quota_port'] def on_exit_server_thread(): service.StopService(None, None) # For CWF start quota check servers start_check_quota_server(run_flask, bridge_ip, has_quota_port, True, on_exit_server_thread) start_check_quota_server(run_flask, bridge_ip, no_quota_port, False, on_exit_server_thread) # Run the service loop service.run() # Cleanup the service service.close()
def main(): """ Loads the Ryu apps we want to run from the config file. This should exit on keyboard interrupt. """ # Run asyncio loop in a greenthread so we can evaluate other eventlets # TODO: Remove once Ryu migrates to asyncio asyncio.set_event_loop_policy(aioeventlet.EventLoopPolicy()) service = MagmaService('pipelined', mconfigs_pb2.PipelineD()) service_config = service.config if environment.is_dev_mode(): of_rest_server.configure(service_config) # Set Ryu config params cfg.CONF.ofp_listen_host = "127.0.0.1" # override mconfig using local config. # TODO: move config compilation to separate module. enable_nat = service.config.get('enable_nat', service.mconfig.nat_enabled) service.config['enable_nat'] = enable_nat logging.info("Nat: %s", enable_nat) vlan_tag = service.config.get('sgi_management_iface_vlan', service.mconfig.sgi_management_iface_vlan) service.config['sgi_management_iface_vlan'] = vlan_tag sgi_ip = service.config.get('sgi_management_iface_ip_addr', service.mconfig.sgi_management_iface_ip_addr) service.config['sgi_management_iface_ip_addr'] = sgi_ip if 'virtual_mac' not in service.config: service.config['virtual_mac'] = get_if_hwaddr( service.config.get('bridge_name')) # Load the ryu apps service_manager = ServiceManager(service) service_manager.load() def callback(returncode): if returncode != 0: logging.error("Failed to set MASQUERADE: %d", returncode) # TODO fix this hack for XWF if enable_nat is True or service.config.get('setup_type') == 'XWF': call_process( 'iptables -t nat -A POSTROUTING -o %s -j MASQUERADE' % service.config['nat_iface'], callback, service.loop) service.loop.create_task( monitor_ifaces(service.config['monitored_ifaces'], service.loop), ) manager = AppManager.get_instance() # Add pipelined rpc servicer pipelined_srv = PipelinedRpcServicer( service.loop, manager.applications.get('GYController', None), manager.applications.get('EnforcementController', None), manager.applications.get('EnforcementStatsController', None), manager.applications.get('DPIController', None), manager.applications.get('UEMacAddressController', None), manager.applications.get('CheckQuotaController', None), manager.applications.get('IPFIXController', None), manager.applications.get('VlanLearnController', None), manager.applications.get('TunnelLearnController', None), service_manager) pipelined_srv.add_to_server(service.rpc_server) if service.config['setup_type'] == 'CWF': bridge_ip = service.config['bridge_ip_address'] has_quota_port = service.config['has_quota_port'] no_quota_port = service.config['no_quota_port'] def on_exit_server_thread(): service.StopService(None, None) # For CWF start quota check servers start_check_quota_server(run_flask, bridge_ip, has_quota_port, True, on_exit_server_thread) start_check_quota_server(run_flask, bridge_ip, no_quota_port, False, on_exit_server_thread) if service.config['setup_type'] == 'LTE': polling_interval = service.config.get('ovs_gtp_stats_polling_interval', MIN_OVSDB_DUMP_POLLING_INTERVAL) collector = GTPStatsCollector(polling_interval, service.loop) collector.start() # Run the service loop service.run() # Cleanup the service service.close()
def main(): """ Loads the Ryu apps we want to run from the config file. This should exit on keyboard interrupt. """ # Run asyncio loop in a greenthread so we can evaluate other eventlets # TODO: Remove once Ryu migrates to asyncio asyncio.set_event_loop_policy(aioeventlet.EventLoopPolicy()) service = MagmaService('pipelined', mconfigs_pb2.PipelineD()) # Optionally pipe errors to Sentry sentry_init(service_name=service.name, sentry_mconfig=service.shared_mconfig.sentry_config) service_config = service.config if environment.is_dev_mode(): of_rest_server.configure(service_config) # Set Ryu config params cfg.CONF.ofp_listen_host = "127.0.0.1" # override mconfig using local config. # TODO: move config compilation to separate module. enable_nat = service.config.get('enable_nat', service.mconfig.nat_enabled) service.config['enable_nat'] = enable_nat logging.info("Nat: %s", enable_nat) enable5g_features = service.config.get( 'enable5g_features', service.mconfig.enable5g_features, ) service.config['enable5g_features'] = enable5g_features logging.info("enable5g_features: %s", enable5g_features) vlan_tag = service.config.get( 'sgi_management_iface_vlan', service.mconfig.sgi_management_iface_vlan, ) service.config['sgi_management_iface_vlan'] = vlan_tag sgi_ip = service.config.get( 'sgi_management_iface_ip_addr', service.mconfig.sgi_management_iface_ip_addr, ) service.config['sgi_management_iface_ip_addr'] = sgi_ip sgi_gateway_ip = service.config.get( 'sgi_management_iface_gw', service.mconfig.sgi_management_iface_gw, ) service.config['sgi_management_iface_gw'] = sgi_gateway_ip # SGi IPv6 address conf sgi_ipv6 = service.config.get( 'sgi_management_iface_ipv6_addr', service.mconfig.sgi_management_iface_ipv6_addr, ) service.config['sgi_management_iface_ipv6_addr'] = sgi_ipv6 sgi_gateway_ipv6 = service.config.get( 'sgi_management_iface_ipv6_gw', service.mconfig.sgi_management_iface_ipv6_gw, ) service.config['sgi_management_iface_ipv6_gw'] = sgi_gateway_ipv6 # Keep router mode off for smooth upgrade path service.config['dp_router_enabled'] = service.config.get( 'dp_router_enabled', False, ) if 'virtual_mac' not in service.config: if service.config['dp_router_enabled']: up_iface_name = service.config.get('nat_iface', None) mac_addr = get_if_hwaddr(up_iface_name) else: mac_addr = get_if_hwaddr(service.config.get('bridge_name')) service.config['virtual_mac'] = mac_addr # this is not read from yml file. service.config['uplink_port'] = OFPP_LOCAL uplink_port_name = service.config.get('ovs_uplink_port_name', None) if enable_nat is False and uplink_port_name is not None: service.config['uplink_port'] = BridgeTools.get_ofport( uplink_port_name, ) # header enrichment related configuration. service.config['proxy_port_name'] = PROXY_PORT_NAME he_enabled_flag = False if service.mconfig.he_config: he_enabled_flag = service.mconfig.he_config.enable_header_enrichment if he_enabled_flag: bridge = service.config.get('bridge_name') BridgeTools.add_ovs_port(bridge, PROXY_PORT_NAME, PROXY_OF_PORT) he_enabled = service.config.get('he_enabled', he_enabled_flag) service.config['he_enabled'] = he_enabled # tune datapath according to config configure_tso(service.config) setup_sgi_tunnel(service.config, service.loop) tune_datapath(service.config) setup_masquerade_rule(service.config, service.loop) # monitoring related configuration mtr_interface = service.config.get('mtr_interface', None) if mtr_interface: try: service.config['mtr_ip'] = get_ip_from_if(mtr_interface) except ValueError: logging.warning("Unable to set up mtr interface", exc_info=True) # Load the ryu apps service_manager = ServiceManager(service) service_manager.load() service.loop.create_task( monitor_ifaces( service.config['monitored_ifaces'], ), ) manager = AppManager.get_instance() # Add pipelined rpc servicer pipelined_srv = PipelinedRpcServicer( service.loop, manager.applications.get('GYController', None), manager.applications.get('EnforcementController', None), manager.applications.get('EnforcementStatsController', None), manager.applications.get('DPIController', None), manager.applications.get('UEMacAddressController', None), manager.applications.get('CheckQuotaController', None), manager.applications.get('IPFIXController', None), manager.applications.get('VlanLearnController', None), manager.applications.get('TunnelLearnController', None), manager.applications.get('Classifier', None), manager.applications.get('InOutController', None), manager.applications.get('NGServiceController', None), service.config, service_manager, ) pipelined_srv.add_to_server(service.rpc_server) if service.config['setup_type'] == 'CWF': bridge_ip = service.config['bridge_ip_address'] has_quota_port = service.config['has_quota_port'] no_quota_port = service.config['no_quota_port'] def on_exit_server_thread(): service.StopService(None, None) # For CWF start quota check servers start_check_quota_server( run_flask, bridge_ip, has_quota_port, True, on_exit_server_thread, ) start_check_quota_server( run_flask, bridge_ip, no_quota_port, False, on_exit_server_thread, ) if service.config['setup_type'] == 'LTE': polling_interval = service.config.get( 'ovs_gtp_stats_polling_interval', MIN_OVSDB_DUMP_POLLING_INTERVAL, ) collector = GTPStatsCollector( polling_interval, service.loop, ) collector.start() # Run the service loop service.run() # Cleanup the service service.close()