def stream_iterator(self): while True: try: p = self.stream_requests.get() except Exception as e: Log.error("Error in stream_iterator", e) if p is None: break yield p
def set_forwarding_pipeline_config(self): """ Set forwarding pipeline on the switch based on p4info file :return: """ try: self.__connection.SetForwardingPipelineConfig(p4info=self.__p4info_helper.p4info, prog_name=self.prog_name, bin_path=self.bin_path, cxt_json_path=self.cxt_json_path) Event.trigger("switch_arbitrated") except Exception as e: Log.error("Error in forwarding pipeline", e) Log.info("Forwarding pipeline set.")
def set_forwarding_pipeline_config(self): """ Set forwarding pipeline on the switch based on p4info file :return: """ try: self.__connection.SetForwardingPipelineConfig(p4info=self.__p4info_helper.p4info, bmv2_json_file_path=self.__bmv2_file_path.encode()) Event.trigger("switch_arbitrated") except Exception as e: Log.error("Error in forwarding pipeline", e) Log.info("Forwarding pipeline set.")
def remove_from_group(switch, member, group): """ Removes a host from a given multicast group :param switch: switch which got the unsub message :param member: host which unsubscribes :param group: multicast group :return: """ try: GroupManager.group_to_member.get(switch, {}).get(group, []).remove(member) except ValueError as e: Log.error("try to remove", member, "from", switch, "and group", group, e, GroupManager.group_to_member) pass
def add_flood_node(self): """ Add mc mc nodes """ p = Popen([self.cli, '--thrift-port', str(self.thrift_port)], stdout=PIPE, stdin=PIPE, stderr=STDOUT) out, err = p.communicate( input="mc_node_create 0 " + " ".join(map(str, list(range(self.max_port))))) if err: Log.error(err)
def init_switches(controller=None, topology_controller=None, pd=None): """ Connect to switches and set forwarding pipeline :param controller: base controller who handles connections :param topology_controller: topology controller, who will send topology packets :return: """ controller.connect_and_arbitrate(grpc_port=Configuration.get('grpc_port'), device_id=Configuration.get('device_id')) controller.set_forwarding_pipeline_config() try: pd.setPorts(Configuration.get("ports")) pd.setFlood(Configuration.get("ports")) pd.setPortMonitor(Configuration.get("ports")) pd.configureTopologyPackets() except Exception as e: Log.error(e) Configuration.set('system_done', True)
def delete_table_entry(self, table_name=None, entry=None): """ Deletes an table entry on the switch :param table_name: table name :param entry: table entry :return: """ try: table_entry = self.__p4info_helper.buildTableEntry( table_name=table_name, match_fields=entry.match_fields, priority=entry.priority) self.__connection.DeleteTableEntry(table_entry) Log.info("Remove entry:", table_name, entry.match_fields) return True except Exception as e: Log.error("Error in table delete", table_name, entry.match_fields, entry.priority, e) return False
def connect_and_arbitrate(self, grpc_port=0, device_id=0): """ Connect and arbitrate to the switch :param grpc_port: grpc port of the p4 switch :param device_id: device id of the p4 switch :return: """ i = Configuration.get('bfr_id') try: # add connection to switch self.__connection = self.__add_switch_connection(name='s{0}'.format(i), address='127.0.0.1:{0}'.format(grpc_port), device_id=device_id) # start packet in thread self.__connection.start_thread() if self.__connection.MasterArbitrationUpdate(): base_mac = int('20:00:00:00:00:00'.translate(None, ":,-"), 16) real_mac = format(base_mac + i, 'x') mac = ":".join(real_mac[i:i + 2] for i in range(0, len(real_mac), 2)) Configuration.set('name', 's{0}'.format(i).encode('utf-8')) Event.trigger("new_switch_connection", name='s{0}'.format(i), device=Switch(name='s{0}'.format(i).encode('utf-8'), ip='20.0.{0}.0'.format(i).encode('utf-8'), mac=mac.encode('utf-8'), bfr_id=i)) Log.info("Arbitration done. Connected to swtich") Event.trigger("arbitration_done") else: Log.error("Master arbitration failed") except Exception as e: Log.error(e)
def add_table_entry(self, table_name=None, entry=None): """ Adds an table entry and locks write request to prevent threading problems :param table_name: table name :param entry: Table entry :return: """ try: table_entry = self.__p4info_helper.buildTableEntry( table_name=table_name, match_fields=entry.match_fields, action_name=entry.action_name, action_params=entry.action_params, priority=entry.priority) self.__connection.WriteTableEntry(table_entry) Log.info("Add entry:", table_name, entry) return True except Exception as e: Log.error(e, "for", table_name, entry.match_fields, entry.action_name, entry.action_params, entry.priority) return False