def get_stats(self, cookie: int = 0, cookie_mask: int = 0): """ Use Ryu API to send a stats request containing cookie and cookie mask, retrieve a response and convert to a Rule Record Table """ parser = self._datapath.ofproto_parser message = parser.OFPFlowStatsRequest(datapath=self._datapath, cookie=cookie, cookie_mask=cookie_mask) try: response = ofctl_api.send_msg(self, message, reply_cls=parser.OFPFlowStatsReply, reply_multi=False) if response == None: self.logger.error( "No rule records match the specified cookie and cookie mask" ) return RuleRecordTable() else: usage = self._get_usage_from_flow_stat(response.body) record_table = RuleRecordTable(records=usage.values(), epoch=global_epoch) return record_table except (InvalidDatapath, OFError, UnexpectedMultiReply): self.logger.error( "Could not obtain rule records due to either InvalidDatapath, OFError or UnexpectedMultiReply" ) return RuleRecordTable()
def _report_usage(self, usage): """ Report usage to sessiond using rpc """ record_table = RuleRecordTable( records=usage.values(), epoch=global_epoch, update_rule_versions=self._ovs_restarted, ) if self._print_grpc_payload: record_msg = 'Sending RPC payload: {0}{{\n{1}}}'.format( record_table.DESCRIPTOR.name, str(record_table), ) self.logger.info(record_msg) future = self.sessiond.ReportRuleStats.future( record_table, self.SESSIOND_RPC_TIMEOUT, ) future.add_done_callback( lambda future: self.loop.call_soon_threadsafe( self._report_usage_done, future, usage.values(), ), )
def get_policy_usage(self, fut): if not self._relay_enabled: fut.set_exception(RelayDisabledException()) return record_table = RuleRecordTable(records=self.total_usage.values()) fut.set_result(record_table)
def get_stats(self, cookie: int = 0, cookie_mask: int = 0): """ Use Ryu API to send a stats request containing cookie and cookie mask, retrieve a response and convert to a Rule Record Table and remove old flows """ if not self._datapath: self.logger.error( "Could not initialize datapath for stats retrieval") return RuleRecordTable() parser = self._datapath.ofproto_parser message = parser.OFPFlowStatsRequest( datapath=self._datapath, table_id=self.tbl_num, cookie=cookie, cookie_mask=cookie_mask, ) try: response = ofctl_api.send_msg( self, message, reply_cls=parser.OFPFlowStatsReply, reply_multi=True, ) if not response: self.logger.error( "No rule records match the specified cookie and cookie mask" ) return RuleRecordTable() aggregated_msgs = [] for r in response: aggregated_msgs += r.body usage = self._get_usage_from_flow_stat(aggregated_msgs) self.loop.call_soon_threadsafe(self._delete_old_flows, usage.values()) record_table = RuleRecordTable( records=usage.values(), epoch=global_epoch, ) return record_table except (InvalidDatapath, OFError, UnexpectedMultiReply): self.logger.error( "Could not obtain rule records due to either InvalidDatapath, OFError or UnexpectedMultiReply" ) return RuleRecordTable()
def _report_usage(self, delta_usage): """ Report usage to sessiond using rpc """ record_table = RuleRecordTable(records=delta_usage.values()) future = self.sessiond.ReportRuleStats.future( record_table, self.SESSIOND_RPC_TIMEOUT) future.add_done_callback(lambda future: self.loop.call_soon_threadsafe( self._report_usage_done, future, delta_usage))
def GetPolicyUsage(self, request, context): """ Get policy usage stats """ self._log_grpc_payload(request) if not self._service_manager.is_app_enabled( EnforcementStatsController.APP_NAME): context.set_code(grpc.StatusCode.UNAVAILABLE) context.set_details('Service not enabled!') return None fut = Future() self._loop.call_soon_threadsafe( self._enforcement_stats.get_policy_usage, fut) try: return fut.result(timeout=self._call_timeout) except concurrent.futures.TimeoutError: logging.error("GetPolicyUsage processing timed out") return RuleRecordTable()
def GetStats(self, request, context): """ Invokes API that returns a RuleRecordTable filtering records based on cookie and cookie mask request parameters """ self._log_grpc_payload(request) if not self._service_manager.is_app_enabled( EnforcementController.APP_NAME): context.set_code(grpc.StatusCode.UNAVAILABLE) context.set_details('Service not enabled!') return None fut = Future() self._loop.call_soon_threadsafe(self.get_stats, request, fut) try: return fut.result(timeout=self._call_timeout) except concurrent.futures.TimeoutError: logging.error("Get Stats request processing timed out") return RuleRecordTable()
def get_policy_usage(self, fut): record_table = RuleRecordTable( records=self.total_usage.values(), epoch=global_epoch) fut.set_result(record_table)