def __init__(self, switch_config, thrift_ip, thrift_port): # Initialize own variables self.miss_counter = switch_config["miss_counter"] self.mismatch_counter = switch_config["mismatch_counter"] self.ip2hc_counter = switch_config["ip2hc_counter"] self.ip2hc_counter_bitmap = switch_config["ip2hc_counter_bitmap"] self.ip2hc_mat = switch_config["ip2hc_mat"] self.read_hc_function = switch_config["read_hc_function"] self.nethcf_state = switch_config["nethcf_state"] self.dirty_flag = switch_config["dirty_flag"] self.dirty_bitmap = switch_config["dirty_bitmap"] self.error_hint_str = ("Please check whether the switch " "is well configured and running.") # Initialize API of BMv2 runtime pre = runtime_CLI.PreType.SimplePreLAG services = runtime_CLI.RuntimeAPI.get_thrift_services(pre) services.extend(SimpleSwitchAPI.get_thrift_services()) try: standard_client, mc_client, sswitch_client = \ runtime_CLI.thrift_connect(thrift_ip, thrift_port, services) except: print(self.error_hint_str) exit(-1) else: runtime_CLI.load_json_config(standard_client) self.runtime_api = SimpleSwitchAPI(pre, standard_client, mc_client, sswitch_client)
def __init__(self, thrift_ip='localhost', thrift_port=9090): pre = runtime_CLI.PreType.SimplePreLAG services = runtime_CLI.RuntimeAPI.get_thrift_services(pre) services.extend(SimpleSwitchAPI.get_thrift_services()) standard_client, mc_client, sswitch_client = runtime_CLI.thrift_connect( thrift_ip, thrift_port, services) runtime_CLI.load_json_config(standard_client) self.ssapi = SimpleSwitchAPI(pre, standard_client, mc_client, sswitch_client) self.json_str = runtime_CLI.utils.get_json_config( standard_client, None) self.json_ = json.loads(self.json_str) self.register_names = { it["name"]: it["size"] for it in self.json_["register_arrays"] } self.counter_names = { it["name"]: it["size"] for it in self.json_["counter_arrays"] } print()
def main(): args = get_parser().parse_args() standard_client, mc_client = thrift_connect( args.thrift_ip, args.thrift_port, RuntimeAPI.get_thrift_services(args.pre)) load_json_config(standard_client, args.json) PrettyAPI(args.pre, standard_client, mc_client).cmdloop()
def configure_runtime_api(): sys.path.append('../bmv2/tools') from bm_runtime.simple_pre import SimplePre from bm_runtime.standard import Standard import bmpy_utils as utils from runtime_CLI import RuntimeAPI, load_json_config client, mc_client = utils.thrift_connect( "localhost", 22222, [("standard", Standard.Client), ("simple_pre", SimplePre.Client)]) load_json_config(client, None) return RuntimeAPI(SimplePre, client, mc_client)
def main(): args = runtime_CLI.get_parser().parse_args() args.pre = runtime_CLI.PreType.SimplePreLAG services = runtime_CLI.RuntimeAPI.get_thrift_services(args.pre) services.extend(PsaSwitchAPI.get_thrift_services()) standard_client, mc_client, pswitch_client = runtime_CLI.thrift_connect( args.thrift_ip, args.thrift_port, services) runtime_CLI.load_json_config(standard_client, args.json, load_json_psa) PsaSwitchAPI(args.pre, standard_client, mc_client, pswitch_client).cmdloop()
def main(): args = runtime_CLI.get_parser().parse_args() args.pre = runtime_CLI.PreType.SimplePreLAG services = runtime_CLI.RuntimeAPI.get_thrift_services(args.pre) services.extend(SimpleSwitchAPI.get_thrift_services()) standard_client, mc_client, sswitch_client = runtime_CLI.thrift_connect( args.thrift_ip, args.thrift_port, services ) runtime_CLI.load_json_config(standard_client, args.json) SimpleSwitchAPI(args.pre, standard_client, mc_client, sswitch_client).cmdloop()
def main(): print("Jiarong: Enter sswitch_CLI.py") args = runtime_CLI.get_parser().parse_args() args.pre = runtime_CLI.PreType.SimplePreLAG services = runtime_CLI.RuntimeAPI.get_thrift_services(args.pre) services.extend(SimpleSwitchAPI.get_thrift_services()) standard_client, mc_client, sswitch_client = runtime_CLI.thrift_connect( args.thrift_ip, args.thrift_port, services) runtime_CLI.load_json_config(standard_client, args.json) SimpleSwitchAPI(args.pre, standard_client, mc_client, sswitch_client).cmdloop()
def __init__(self, thrift_ip='localhost', thrift_port=9090): pre = runtime_CLI.PreType.SimplePreLAG services = runtime_CLI.RuntimeAPI.get_thrift_services(pre) services.extend(SimpleSwitchAPI.get_thrift_services()) standard_client, mc_client, sswitch_client = runtime_CLI.thrift_connect( thrift_ip, thrift_port, services) runtime_CLI.load_json_config(standard_client) self.ssapi = SimpleSwitchAPI(pre, standard_client, mc_client, sswitch_client) self.json_str = runtime_CLI.utils.get_json_config(standard_client, None) self.json_ = json.loads(self.json_str) self.register_names = {it["name"]: it["size"] for it in self.json_["register_arrays"]} self.counter_names = {it["name"]: it["size"] for it in self.json_["counter_arrays"]} print()
def send_commands(thrift_port, json, commands): if not has_api: print( "Commands requested, but Runtime CLI not present! Ensure $BMV2_REPO/tools is on PYTHONPATH" ) raise Exception("Could not execute commands: Runtime API not present") services = RuntimeAPI.get_thrift_services('SimplePre') services.extend(SimpleSwitchAPI.get_thrift_services()) standard_client, mc_client, sswitch_client = thrift_connect( 'localhost', thrift_port, services) load_json_config(standard_client, json) api = SimpleSwitchAPI('SimplePre', standard_client, mc_client, sswitch_client) for command in commands: api.onecmd(command)
def main(): # Init Log global process log = p4Logging() # Init the runtime API pre = runtime_CLI.PreType.SimplePreLAG services = runtime_CLI.RuntimeAPI.get_thrift_services(pre) services.extend(SimpleSwitchAPI.get_thrift_services()) standard_client, mc_client, sswitch_client = runtime_CLI.thrift_connect( 'localhost', 9090, services) runtime_CLI.load_json_config(standard_client, "") cli = SimpleSwitchAPI(pre, standard_client, mc_client, sswitch_client) # Add default rules on the switch process = ProcessPacketOut(cli, log) process.add_default_rules() #Start sniff sniff(iface="s1-eth3", prn=process_packet, store=0)
def main(): args = runtime_CLI.get_parser().parse_args() args.pre = runtime_CLI.PreType.SimplePreLAG services = runtime_CLI.RuntimeAPI.get_thrift_services(args.pre) services.extend(SimpleSwitchAPI.get_thrift_services()) standard_client, mc_client, sswitch_client = runtime_CLI.thrift_connect( args.thrift_ip, args.thrift_port, services) runtime_CLI.load_json_config(standard_client, args.json) if args.command != []: for cmd in args.command: SimpleSwitchAPI(args.pre, standard_client, mc_client, sswitch_client).onecmd(cmd) else: SimpleSwitchAPI(args.pre, standard_client, mc_client, sswitch_client).cmdloop()
def main(): global rAPI, rndDesc args = get_parser().parse_args() standard_client, mc_client = thrift_connect( args.thrift_ip, args.thrift_port, RuntimeAPI.get_thrift_services(args.pre)) load_json_config(standard_client, args.json) rAPI = RuntimeAPI(args.pre, standard_client, mc_client) # PRNG rndDesc = Random.new() print "sniffing on %s" % args.iface sys.stdout.flush() sniff(iface=args.iface, prn=handle_pkt(rAPI)) rndDesc.close()
def ConnectToThrift(self, ip, port, services, json): standard_client, mc_client = cli.thrift_connect( ip, port, cli.RuntimeAPI.get_thrift_services(services)) cli.load_json_config(standard_client, json) return standard_client, mc_client
default='veth250') args = parser.parse_args() #don't care about multicast in this example args.pre = runtime_CLI.PreType.None services = runtime_CLI.RuntimeAPI.get_thrift_services(args.pre) services.extend(SimpleSwitchAPI.get_thrift_services()) # standard client deals in tables (add, delete, get) # mc_client deals with multicast groups # sswitch_client deals with mirroring # for this very simple example, focus is on standard_client standard_client, mc_client, sswitch_client = runtime_CLI.thrift_connect( args.thrift_ip, args.thrift_port, services) # load table info from compiled json runtime_CLI.load_json_config(standard_client, args.json) def get_table(table_name): key = (runtime_CLI.ResType.table, table_name) if key in runtime_CLI.SUFFIX_LOOKUP_MAP: return runtime_CLI.SUFFIX_LOOKUP_MAP[(runtime_CLI.ResType.table, table_name)] raise Exception("no table " + table_name + " found") def i2bytes(nr, bw): return runtime_CLI.int_to_bytes(nr, (bw + 7) / 8) # relating a P4 table to a runtime table. Let:
def __init__(self, json_file, folder, num_ports=8): self.modified_tables = [] self.json_file = json_file self.json_file_abspath = os.path.abspath(json_file) self.num_ports = num_ports self.pcap_filename_prefix = 'pcap' # TBD: See bmv2stf.py for ideas on running multiple # simple_switch processes in parallel on the same machine. # This code does not support that yet. self.folder = folder self.thrift_port_num = 9090 # See step (1) above intf_args = [] self.intf_info = {} for i in range(self.num_ports): intf_args.append('-i') intf_args.append(self.intf_num_to_simple_switch_arg(i)) self.intf_info[i] = { 'pcap_in_fname': self.intf_num_to_filename(i, 'in'), 'pcap_out_fname': self.intf_num_to_filename(i, 'out') } logging.debug("Creating named pipe '%s'" "" % (self.intf_info[i]['pcap_in_fname'])) os.mkfifo(self.intf_info[i]['pcap_in_fname']) # Workaround for problem that I have only seen on some # systems, but not others, for reasons that I don't # understand. The symptom of the problem is that when running # pytest, even as root, multiple of the test cases fail with # "Exception: Initializing simple_switch failed", and the # following output labeled "Captured stderr call". # Nanomsg returned a exception when trying to bind to address 'ipc:///tmp/bmv2-0-notifications.ipc'. # The exception is: Address already in use # This may happen if # 1) the address provided is invalid, # 2) another instance of bmv2 is running and using the same address, or # 3) you have insufficent permissions (e.g. you are using an IPC socket on Unix, the file already exists and you don't have permission to access it) # I have tried adding debug messages to a few places in the # simple_switch executable to discover why this failure # occurs, but haven't discovered a reason for it. Removing # this file seems to avoid the problem. ipc_fname = '/tmp/bmv2-0-notifications.ipc' if os.path.exists(ipc_fname): logf_append('Found file %s -- try to remove it' % (ipc_fname)) os.remove('/tmp/bmv2-0-notifications.ipc') if os.path.exists(ipc_fname): logf_append('After trying to remove file %s it still exists' '' % (ipc_fname)) else: logf_append('File %s successfully removed' % (ipc_fname)) else: logf_append('No file found: %s -- good' % (ipc_fname)) # Start simple_switch. See step (2) above. ss_cmd_args = ([ 'simple_switch', '--log-console', '--thrift-port', str(self.thrift_port_num), '--use-files', '0' ] + intf_args + [self.json_file_abspath]) logging.debug("Starting simple_switch in directory %s with args: %s" "" % (self.folder, ss_cmd_args)) self.proc = subprocess.Popen( ss_cmd_args, stdout=subprocess.PIPE, cwd=self.folder) # See step (3) above. # p4c/backends/bmv2/bmv2stf.py has scary-looking "DANGER" # comment about opening the named fifos in the same order that # simple_switch does, else things can deadlock. Not sure why # deadlock might occur, but may as well do it the same order # here. for i in sorted(self.intf_info): self.open_pcap_in_intf(i) # Wait for simple_switch to finish initializing # See step (4) above. init_done = False ss_ready_msg = 'Pcap reader: starting PcapFilesReader' logging.debug("Waiting for this line of output from simple_switch: %s", ss_ready_msg) for line in iter(self.proc.stdout.readline, ''): logging.debug("Line from simple_switch log: %s", line.rstrip()) if ss_ready_msg in str(line): logging.debug("Found expected log line. Considering" " simple_switch initialized enough to continue") init_done = True break if not init_done: raise Exception('Initializing simple_switch failed') # See step (5) above. # XXX: read params from config pre = PreType.SimplePreLAG num_tries = 0 while True: try: standard_client, mc_client = thrift_connect( 'localhost', str(self.thrift_port_num), RuntimeAPI.get_thrift_services(pre)) except: num_tries += 1 time.sleep(1) logging.debug("Failed thrift_connect attempt try #%d" "" % (num_tries)) if num_tries == 3: logging.error("Failed 3 times in a row. Giving up.") sys.exit(1) else: break load_json_config(standard_client) self.api = RuntimeAPI(pre, standard_client, mc_client)