def update(self, *args, **kwargs): """ Update bier tables, bift Is called on certain events :return: """ if "port_update" in kwargs: try: d = Configuration.get('update') if "bier" not in d: return if "sleep" in kwargs: time.sleep(kwargs.get("sleep")) except ConfigurationNotFound: pass srcDevice = kwargs.get('src_device') for switch in self._baseController.get_connections(): self.update_bier_forwarding_entries(switch=switch) Log.async_info("Updated BIER entries.")
def start(self): """ Start grpc server This grpc server will be used to connect the local controller with the global """ proto.connection_pb2_grpc.add_GlobalServerServicer_to_server(GlobalServer(), self.server) self.server.add_insecure_port('[::]:' + str(self.listen_port)) Log.async_info("Start GRPC Server on port", self.listen_port) self.server.start()
def PortMessage(self, request, context): """ This method receives a port message """ Log.async_info("Got port message") Log.async_debug(request) # this event is not catched yet # for demonstration purpose, the topology doesn't get updated # on a link failure Event.trigger("port_message", message=request) return proto.connection_pb2.Status(code=1, message="Accepted")
def load_static_rules(self): """ Load static rules from json file specified in config """ valid_entries = defaultdict(list) for switch in Configuration.get('switches'): if "static_rules" in switch and Configuration.get( 'static_rules') == True: data = Configuration.load(switch['static_rules'])["entries"] for entry in data: if entry['table'] != "ingress.ipv4_c.ipv4": continue e = TableEntry( switch=entry["switch"], match_fields={ "hdr.ipv4.dstAddr": (str(entry["match_fields"][0]), int(entry["match_fields"][1])), "meta.ports.status": (BierComputation.id_to_bitstring( id=int(entry["match_fields"][2])), int(entry["match_fields"][3])) }, action_name=entry["action_name"], action_params={"port": int(entry["action_params"])}, priority=1) TableEntryManager.handle_table_entry( self.table_manager, table_name=entry["table"], table_entry=e) valid_entries[entry["switch"]].append(e.match_fields) Log.async_info("Static rules for IPv4 loaded.") return valid_entries
def connect(self): """ All switches grpc addresses are in ascending order. Connect until a connection can't be established :return: """ for switch in Configuration.get("switches"): try: self.__connections[switch["name"]] = SwitchConnection( grpc_address='127.0.0.1:{0}'.format( switch["local_controller_port"])) Log.async_debug("Connected to controller on port", switch["local_controller_port"]) Event.trigger('switch_connected', name=switch["name"]) except grpc.RpcError as e: raise SwitchConnectionFailed(switch["name"], switch["local_controller_port"]) Log.async_info("Connected to", len(self.__connections), "controller") Configuration.set('connected', True)
def update_ipv4_rules(self, *args, **kwargs): """ Update ipv4 forwarding entries triggered by event :return: """ if "port_update" in kwargs: try: d = Configuration.get('update') if "ipv4" not in d: return if "sleep" in kwargs: time.sleep(kwargs.get("sleep")) except ConfigurationNotFound: pass entries = defaultdict(list) for switch in self._baseController.get_connections(): entries[switch].extend(self.update_ipv4_entries(switch=switch)) static_rules = self.load_static_rules() for switch in entries: v_entries = entries.get(switch) v_entries.extend(static_rules[switch]) self.table_manager.remove_invalid_entries( switch=switch, table_name="ingress.ipv4_c.ipv4", valid_entries=v_entries) Log.async_info("IP rules update.")
def CheckConnection(self, request, context): Log.async_info("Local controller connected to global server") return proto.connection_pb2.Status(code=1, message="Connected")
def load_static_rules(*args): Log.async_info("Write static rules...") for arg in args: arg.load_static_rules()
def ShutdownAllSwitchConnections(): for c in connections: Log.async_info("Shutting down connection", c.name) c.shutdown()