def ConfigureNetwork(p4info_file="build/data_plane.p4info", bmv2_json="build/data_plane.json"): p4info_helper = helper.P4InfoHelper(p4info_file) threads = [] print "Connecting to P4Runtime server on s1..." sw1 = bmv2.Bmv2SwitchConnection('s1', "127.0.0.1:50051", 0) sw1.MasterArbitrationUpdate() sw1.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_json) t = threading.Thread(target=RunControlPlane, args=(sw1, 1, p4info_helper)) t.start() threads.append(t) print "Connecting to P4Runtime server on s2..." sw2 = bmv2.Bmv2SwitchConnection('s2', "127.0.0.1:50052", 1) sw2.MasterArbitrationUpdate() sw2.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_json) t = threading.Thread(target=RunControlPlane, args=(sw2, 2, p4info_helper)) t.start() threads.append(t) print "Connecting to P4Runtime server on s3..." sw3 = bmv2.Bmv2SwitchConnection('s3', "127.0.0.1:50053", 2) sw3.MasterArbitrationUpdate() sw3.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_json) t = threading.Thread(target=RunControlPlane, args=(sw3, 3, p4info_helper)) t.start() threads.append(t) for t in threads: t.join()
def configure_switch(sw_name, addr, device_id, p4info_helper, bmv2_json_fpath, proto_dump_fpath=None): #info('Configure P4 switch') #p4info_helper = helper.P4InfoHelper(p4info_fpath) #info("Connecting to P4Runtime server on %s..." % addr) if proto_dump_fpath: sw = bmv2.Bmv2SwitchConnection(name=sw_name, address=addr, device_id=device_id, proto_dump_file=proto_dump_fpath) else: sw = bmv2.Bmv2SwitchConnection(name=sw_name, address=addr, device_id=device_id) try: sw.MasterArbitrationUpdate() #info("Setting pipeline config") sw.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_json_fpath) return sw except: sw.shutdown()
def ConfigureNetwork(p4info_file="build/data_plane.p4info", bmv2_json="build/data_plane.json", topology_json="topology2.json"): p4info_helper = helper.P4InfoHelper(p4info_file) with open(topology_json, 'r') as f: routers = json.load(f, object_pairs_hook=OrderedDict)['routers'] threads = [] port = 50051 id_num = 0 for name, config in routers.items(): config = byteify(config) print "Connecting to P4Runtime server on {}...".format(name) r = bmv2.Bmv2SwitchConnection(name, "127.0.0.1:" + str(port), id_num) r.MasterArbitrationUpdate() r.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_json) t = threading.Thread(target=RunControlPlane, args=(r, config, p4info_helper)) t.start() threads.append(t) port += 1 id_num += 1 for t in threads: t.join()
def read_switch_table(addr, device_id, table_id): # establish a connection to the switch sw = bmv2.Bmv2SwitchConnection(address=addr, device_id=device_id) response = sw.ReadTableEntries(table_id=table_id) try: print(next(response)) except: print("end")
def program_switch(addr, device_id, sw_conf_file, workdir, proto_dump_fpath): sw_conf = json_load_byteified(sw_conf_file) try: check_switch_conf(sw_conf=sw_conf, workdir=workdir) except ConfException as e: error("While parsing input runtime configuration: %s" % str(e)) return info('Using P4Info file %s...' % sw_conf['p4info']) p4info_fpath = os.path.join(workdir, sw_conf['p4info']) p4info_helper = helper.P4InfoHelper(p4info_fpath) target = sw_conf['target'] info("Connecting to P4Runtime server on %s (%s)..." % (addr, target)) if target == "bmv2": sw = bmv2.Bmv2SwitchConnection(address=addr, device_id=device_id, proto_dump_file=proto_dump_fpath) else: raise Exception("Don't know how to connect to target %s" % target) try: sw.MasterArbitrationUpdate() if target == "bmv2": info("Setting pipeline config (%s)..." % sw_conf['bmv2_json']) bmv2_json_fpath = os.path.join(workdir, sw_conf['bmv2_json']) sw.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_json_fpath) else: raise Exception("Should not be here") if 'table_entries' in sw_conf: table_entries = sw_conf['table_entries'] info("Inserting %d table entries..." % len(table_entries)) for entry in table_entries: info(tableEntryToString(entry)) insertTableEntry(sw, entry, p4info_helper) if 'multicast_group_entries' in sw_conf: group_entries = sw_conf['multicast_group_entries'] info("Inserting %d group entries..." % len(group_entries)) for entry in group_entries: info(groupEntryToString(entry)) insertMulticastGroupEntry(sw, entry, p4info_helper) if 'clone_session_entries' in sw_conf: clone_entries = sw_conf['clone_session_entries'] info("Inserting %d clone entries..." % len(clone_entries)) for entry in clone_entries: info(cloneEntryToString(entry)) insertCloneGroupEntry(sw, entry, p4info_helper) finally: sw.shutdown()
def install_s1__entry_rule(): # DO NOT USE # ALREADY DONE IN s1-runtime.json # INTENDED AS AN EXAMPLE FOR FUTURE DEVELOPMENT #!/usr/bin/env python2 import grpc import os from time import sleep # Import P4Runtime lib from parent utils dir # Probably there's a better way of doing this. sys.path.append( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'utils/p4runtime_lib')) import bmv2 from error_utils import printGrpcError from switch import ShutdownAllSwitchConnections import helper p4info_file_path = "build/Raft.p4.p4info.txt" p4info_helper = helper.P4InfoHelper(p4info_file_path) s1 = bmv2.Bmv2SwitchConnection( name='s1', address='127.0.0.1:50051', device_id=0, proto_dump_file='logs/s1-p4runtime-requests_my_py.txt') s1.MasterArbitrationUpdate() table_entry = p4info_helper.buildTableEntry( table_name="MyIngress.leader", match_fields={ "meta.raft_metadata.role": 2, "hdr.raft.messageType": 2 }, action_name="MyIngress.spread_new_request", action_params={}) table_entry2 = p4info_helper.buildTableEntry( table_name="MyIngress.follower", match_fields={ "meta.raft_metadata.role": 0, "hdr.raft.messageType": 10, "hdr.ipv4.dstAddr": ["10.0.1.254", 32] }, action_name="MyIngress.follower_timeout", action_params={}) s1.WriteTableEntry(table_entry) s1.WriteTableEntry(table_entry2) print("Installed leader table entry rule on {}".format(s1.name))
def program_switch(addr, device_id, sw_conf_file, workdir, proto_dump_fpath): sw_conf = json_load_byteified(sw_conf_file) try: check_switch_conf(sw_conf=sw_conf, workdir=workdir) except ConfException as e: error("While parsing input runtime configuration: %s" % str(e)) return info('Using P4Info file %s...' % sw_conf['p4info']) # basic.p4info p4info_fpath = os.path.join(workdir, sw_conf['p4info']) p4info_helper = helper.P4InfoHelper(p4info_fpath) target = sw_conf['target'] info("Connecting to P4Runtime server on %s (%s)..." % (addr, target)) if target == "bmv2": sw = bmv2.Bmv2SwitchConnection( address=addr, device_id=device_id, # Bmv2SwitchConnection extends SwitchConnection proto_dump_file=proto_dump_fpath) # p4info is not set else: raise Exception("Don't know how to connect to target %s" % target) try: sw.MasterArbitrationUpdate() if target == "bmv2": info("Setting pipeline config (%s)..." % sw_conf['bmv2_json']) # p4c compiled json file bmv2_json_fpath = os.path.join(workdir, sw_conf['bmv2_json']) print("bmv2_json_fpath---", bmv2_json_fpath) # set p4info and p4c compiled json file to switch sw.SetForwardingPipelineConfig( p4info=p4info_helper.p4info, # p4info is in bmv2_json_file_path=bmv2_json_fpath ) # set p4c compiled json file else: raise Exception("Should not be here") if 'table_entries' in sw_conf: table_entries = sw_conf['table_entries'] info("Inserting %d table entries..." % len(table_entries)) for entry in table_entries: info(tableEntryToString(entry)) insertTableEntry(sw, entry, p4info_helper) finally: #print("continue") sw.shutdown()
def reset_switch(addr, device_id, sw_conf_file, workdir, proto_dump_fpath): sw_conf = json_load_byteified(sw_conf_file) try: check_switch_conf(sw_conf=sw_conf, workdir=workdir) except ConfException as e: error("While parsing input runtime configuration: %s" % str(e)) return p4info_fpath = os.path.join(workdir, sw_conf['p4info']) p4info_helper = helper.P4InfoHelper(p4info_fpath) target = sw_conf['target'] if target == "bmv2": sw = bmv2.Bmv2SwitchConnection(address=addr, device_id=device_id, proto_dump_file=proto_dump_fpath) else: raise Exception("Don't know how to connect to target %s" % target) try: sw.MasterArbitrationUpdate() if target == "bmv2": bmv2_json_fpath = os.path.join(workdir, sw_conf['bmv2_json']) sw.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_json_fpath) else: raise Exception("Should not be here") if 'table_entries' in sw_conf: table_entries = sw_conf['table_entries'] for entry in table_entries: insertTableEntry(sw, entry, p4info_helper) deleteTableEntry(sw, entry, p4info_helper) finally: sw.shutdown()
help="path to P4Runtime protobuf description (text)", type=str, action="store", default="build/basic.p4info") args = parser.parse_args() if not os.path.exists(args.p4info_file): parser.error("File %s does not exist!" % args.p4info_file) if not os.path.exists(args.bmv2_json): parser.error("File %s does not exist!" % args.bmv2_json) p4info_helper = helper.P4InfoHelper(args.p4info_file) threads = [] print "Connecting to P4Runtime server on s1..." sw1 = bmv2.Bmv2SwitchConnection('s1', "127.0.0.1:50051", 0) sw1.MasterArbitrationUpdate() sw1.SetForwardingPipelineConfig(p4info = p4info_helper.p4info, bmv2_json_file_path = args.bmv2_json) t = threading.Thread(target=ProgramSwitch, args=(sw1, 1, p4info_helper)) t.start() threads.append(t) print "Connecting to P4Runtime server on s2..." sw2 = bmv2.Bmv2SwitchConnection('s2', "127.0.0.1:50052", 1) sw2.MasterArbitrationUpdate() sw2.SetForwardingPipelineConfig(p4info = p4info_helper.p4info, bmv2_json_file_path = args.bmv2_json) t = threading.Thread(target=ProgramSwitch, args=(sw2, 2, p4info_helper)) t.start() threads.append(t)
# Import P4Runtime lib from parent utils dir # Probably there's a better way of doing this. sys.path.append( os.path.join(os.path.dirname(os.path.abspath(__file__)), 'utils/p4runtime_lib')) import bmv2 from error_utils import printGrpcError from switch import ShutdownAllSwitchConnections import helper p4info_file_path = "build/Raft.p4.p4info.txt" p4info_helper = helper.P4InfoHelper(p4info_file_path) s1 = bmv2.Bmv2SwitchConnection( name='s1', address='127.0.0.1:50051', device_id=0, proto_dump_file='logs/s1-p4runtime-requests_my_py.txt') s1.MasterArbitrationUpdate() table_entry2 = p4info_helper.buildTableEntry( table_name="MyIngress.follower", match_fields={ "meta.raft_metadata.role": 0, "hdr.raft.messageType": 10, "hdr.ipv4.dstAddr": [0x0, 0xa, 1] }, action_name="MyIngress.follower_timeout", action_params={})