def insertTableEntry(self, entry=None, table_name=None, match_fields=None, action_name=None, default_action=None, action_params=None, priority=None): if entry is not None: table_name = entry['table'] match_fields = entry.get('match') # None if not found action_name = entry['action_name'] default_action = entry.get('default_action') # None if not found action_params = entry['action_params'] priority = entry.get('priority') # None if not found table_entry = self.p4info_helper.buildTableEntry( table_name=table_name, match_fields=match_fields, default_action=default_action, action_name=action_name, action_params=action_params, priority=priority) try: self.sw_conn.WriteTableEntry(table_entry) except grpc.RpcError as e: printGrpcError(e)
def loadJSON(self): try: self.sw_conn.SetForwardingPipelineConfig( p4info=self.p4info_helper.p4info, bmv2_json_file_path=self.json_path) except grpc.RpcError as e: printGrpcError(e)
def main(p4info_file_path, bmv2_file_path): # Instantiate a P4Runtime helper from the p4info file p4info_helper = p4runtime_lib.helper.P4InfoHelper(p4info_file_path) try: # Create a switch connection object for s1 and s2; # this is backed by a P4Runtime gRPC connection. # Also, dump all P4Runtime messages sent to switch to given txt files. s1 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s1', address='127.0.0.1:50051', device_id=0, proto_dump_file='logs/s1-p4runtime-requests.txt') s2 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s2', address='127.0.0.1:50052', device_id=1, proto_dump_file='logs/s2-p4runtime-requests.txt') # Send master arbitration update message to establish this controller as # master (required by P4Runtime before performing any other write operation) s1.MasterArbitrationUpdate() s2.MasterArbitrationUpdate() # Install the P4 program on the switches s1.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s1" s2.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s2" # Write the rules that tunnel traffic from h1 to h2 writeTunnelRules(p4info_helper, ingress_sw=s1, egress_sw=s2, tunnel_id=100, dst_eth_addr="08:00:00:00:02:22", dst_ip_addr="10.0.2.2") # Write the rules that tunnel traffic from h2 to h1 writeTunnelRules(p4info_helper, ingress_sw=s2, egress_sw=s1, tunnel_id=200, dst_eth_addr="08:00:00:00:01:11", dst_ip_addr="10.0.1.1") # TODO Uncomment the following two lines to read table entries from s1 and s2 readTableRules(p4info_helper, s1) readTableRules(p4info_helper, s2) # Print the tunnel counters every 2 seconds while True: sleep(2) print '\n----- Reading tunnel counters -----' printCounter(p4info_helper, s1, "MyIngress.ingressTunnelCounter", 100) printCounter(p4info_helper, s2, "MyIngress.egressTunnelCounter", 100) printCounter(p4info_helper, s2, "MyIngress.ingressTunnelCounter", 200) printCounter(p4info_helper, s1, "MyIngress.egressTunnelCounter", 200) except KeyboardInterrupt: print " Shutting down." except grpc.RpcError as e: printGrpcError(e) ShutdownAllSwitchConnections()
def main(p4info_file_path, bmv2_file_path): # Instantiate a P4Runtime helper from the p4info file ip2id_l = {} id_manager = IdManager(2**16 - 1) p4info_helper = p4runtime_lib.helper.P4InfoHelper(p4info_file_path) switches_conf = load_switches_conf() pr_rules = load_pr_rules() sdnc_pi_rules = load_sdnc_pkt_in_rules() fwd_rules = load_fwd_rules() pk_rules = load_pk_rules() try: switches = connect_to_switches(switches_conf["switches"]) send_master_arbitration_updates(switches) set_pipelines(switches, p4info_helper, bmv2_file_path) install_direct_forwarding_rules(p4info_helper, switches) install_rules_protected_services(p4info_helper, switches, switches_conf, pr_rules["switches"]) install_port_knock_in_rules(p4info_helper, switches, switches_conf, sdnc_pi_rules["switches"]) install_forwarding_rules(p4info_helper, switches, switches_conf, fwd_rules["switches"]) switch_2 = switches[1] while True: packet_in = switch_2.PacketIn() if packet_in.WhichOneof('update') == 'packet': pkt = Ether(_pkt=packet_in.packet.payload) src_ip = pkt.getlayer(IP).src print("SRC IP: " + str(src_ip)) new_id = id_manager.get_id() print("New ID: " + str(new_id)) ip2id_l[str(src_ip)] = new_id install_pip2id_rules(p4info_helper, switches, src_ip, new_id) install_pk_rules(p4info_helper, switches, switches_conf, pk_rules["switches"]) except KeyboardInterrupt: print(" Shutting down.") except grpc.RpcError as e: printGrpcError(e) ShutdownAllSwitchConnections()
def main(p4info_file_path, bmv2_file_path): p4info_helper = p4runtime_lib.helper.P4InfoHelper(p4info_file_path) pk = PortKnocking() pk.load() switches_conf = load_switches_conf() firewall_rules = load_firewall_rules() forward_rules = load_forward_rules() pk_packet_in_rules = load_pk_packet_in_rules() try: switches = connect_to_switches(switches_conf["switches"]) send_master_arbitration_updates(switches) set_pipelines(switches, p4info_helper, bmv2_file_path) install_direct_forwarding_rules(p4info_helper, switches) install_firewall_rules(p4info_helper, switches, switches_conf["switches"], firewall_rules["switches"]) install_port_knock_in_rules(p4info_helper, switches, switches_conf["switches"], pk_packet_in_rules["switches"]) install_forwarding_rules(p4info_helper, switches, switches_conf["switches"], forward_rules["switches"]) switch_2 = switches[1] while True: packet_in = switch_2.PacketIn() print("Recibido paquete: "+str(packet_in)) if packet_in.WhichOneof('update') == 'packet': pkt = Ether(_pkt=packet_in.packet.payload) src_ip = pkt.getlayer(IP).src dst_ip = pkt.getlayer(IP).dst tcp_dPort = pkt.getlayer(TCP).dport print("SRC IP: " + str(src_ip)) print("DST IP: " + str(dst_ip)) print("TCP DPORT: " + str(tcp_dPort)) if pkt.getlayer(TCP): pk.check(src_ip, dst_ip, tcp_dPort) if pk.has_authed(src_ip, dst_ip): install_allowance_rules(p4info_helper, switch_2, src_ip, dst_ip, pk.config["hidden_services"][dst_ip]) except KeyboardInterrupt: print(" Shutting down.") except grpc.RpcError as e: printGrpcError(e) ShutdownAllSwitchConnections()
def process_new_p4code(topo_file="./topology.json", p4info_file_path="./build/s1_running.p4info", bmv2_file_path="./build/s1_running.json"): # Generate the new P4 code from intent.txt generate_p4code_from_intent() # Compile the P4 code into p4info and BMV2's JSON pipeline config files retval = run_external_program( "p4c-bm2-ss --p4v 16 --p4runtime-file build/" "s1_running.p4info --p4runtime-format " "text -o build/s1_running.json s1_running.p4") # Read the topology and table content definition files with open(topo_file, 'r') as f: topo = json.load(f) switches = topo['switches'] s1_runtime_json = switches['s1']['runtime_json'] with open(s1_runtime_json, 'r') as sw_conf_file: s1_dict = p4runtime_lib.simple_controller.json_load_byteified( sw_conf_file) # Instantiate a P4Runtime helper from the p4info file p4info_helper = p4runtime_lib.helper.P4InfoHelper(p4info_file_path) try: # Create a switch connection object for s1 and s2; # this is backed by a P4Runtime gRPC connection. # Also, dump all P4Runtime messages sent to switch to given txt files. s1 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s1', address='127.0.0.1:50051', device_id=0, proto_dump_file='logs/s1-p4runtime-requests.txt') # Send master arbitration update message to establish this controller as # master (required by P4Runtime before performing any other write operation) s1.MasterArbitrationUpdate() # Install the P4 program on the switch s1.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s1" # Install the table entries to the switch table_entries = s1_dict['table_entries'] print "Inserting %d table entries..." % len(table_entries) for entry in table_entries: print p4runtime_lib.simple_controller.tableEntryToString(entry) p4runtime_lib.simple_controller.insertTableEntry( s1, entry, p4info_helper) print "Installed table entries on s1" except grpc.RpcError as e: printGrpcError(e) ShutdownAllSwitchConnections()
def start(self, controllers): info("Starting P4 switch {}.\n".format(self.name)) args = [self.sw_path] for port, intf in self.intfs.items(): if not intf.IP(): args.extend(['-i', str(port) + "@" + intf.name]) if self.pcap_dump: args.append("--pcap %s" % self.pcap_dump) if self.nanomsg: args.extend(['--nanolog', self.nanomsg]) args.extend(['--device-id', str(self.device_id)]) P4Switch.device_id += 1 if self.json_path: args.append(self.json_path) else: args.append("--no-p4") if self.enable_debugger: args.append("--debugger") if self.log_console: args.append("--log-console") if self.thrift_port: args.append('--thrift-port ' + str(self.thrift_port)) if self.grpc_port: args.append("-- --grpc-server-addr 0.0.0.0:" + str(self.grpc_port)) cmd = ' '.join(args) info(cmd + "\n") pid = None with tempfile.NamedTemporaryFile() as f: self.cmd(cmd + ' >' + self.log_file + ' 2>&1 & echo $! >> ' + f.name) pid = int(f.read()) debug("P4 switch {} PID is {}.\n".format(self.name, pid)) if not self.check_switch_started(pid): error("P4 switch {} did not start correctly.\n".format(self.name)) exit(1) info("P4 switch {} has been started.\n".format(self.name)) if self.start_controller: self.sw_conn = p4runtime_lib.bmv2.Bmv2SwitchConnection( name=self.name, address='127.0.0.1:' + str(self.grpc_port), device_id=self.device_id, proto_dump_file='/tmp/p4app-logs/' + self.name + '-p4runtime-requests.txt') try: self.sw_conn.MasterArbitrationUpdate() except grpc.RpcError as e: printGrpcError(e) if self.p4info_path: self.loadP4Info() self.loadJSON()
def main(p4info_file_path, bmv2_file_path): # Instantiate a P4Runtime helper from the p4info file p4info_helper = p4runtime_lib.helper.P4InfoHelper(p4info_file_path) try: # Create a switch connection object for s1 and s2; # this is backed by a P4Runtime gRPC connection. # Also, dump all P4Runtime messages sent to switch to given txt files. s1 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s1', address='127.0.0.1:50051', device_id=0, proto_dump_file='logs/s1-p4runtime-requests.txt') # Send master arbitration update message to establish this controller as # master (required by P4Runtime before performing any other write operation) s1.MasterArbitrationUpdate() # Install the P4 program on the switches s1.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s1" for i in range(1, 200): writeShadow(p4info_helper, sw_id=s1, dst_ip_addr="10.0." + str(i) + ".3", nf1=1, nf2=2, nf3=3, nf4=4, nf5=5, ttl_rounds=6) ''' for i in range(1,255): for j in range(1, 255): writeShadow(p4info_helper, sw_id=s1, dst_ip_addr="10." + str(j) + "." + str(i) + ".3", catalogue=1) ''' ''' #this installs 2-20 rules for k in range(1, 16): for i in range(1,255): for j in range(1, 255): writeShadow(p4info_helper, sw_id=s1, dst_ip_addr= str(k) + "." + str(j) + "." + str(i) + ".3", catalogue=1) ''' writeEth(p4info_helper, sw_id=s1, dst_ip_addr="10.0.2.2", src_eth_addr="00:00:00:00:02:02" , port=2) writeEth(p4info_helper, sw_id=s1, dst_ip_addr="10.0.1.1", src_eth_addr="00:00:00:00:01:01" , port=1) print " Shutting down." except grpc.RpcError as e: printGrpcError(e) ShutdownAllSwitchConnections()
def setup_connection(p4info_file_path, bmv2_file_path, name, address, device_id): p4info_helper = p4runtime_lib.helper.P4InfoHelper(p4info_file_path) try: s = p4runtime_lib.bmv2.Bmv2SwitchConnection( name=name, address=address, device_id=device_id, ) # Send master arbitration update message to establish this controller as # master (required by P4Runtime before performing any other write operation) # s1.MasterArbitrationUpdate() s.MasterArbitrationUpdate() return s except KeyboardInterrupt: print " Shutting down." except grpc.RpcError as e: printGrpcError(e)
def deleteTableEntry(self, entry=None, table_name=None, match_fields=None, priority=None): if entry is not None: table_name = entry['table'] match_fields = entry.get('match') # None if not found priority = entry.get('priority') # None if not found table_entry = self.p4info_helper.buildTableEntry( table_name=table_name, match_fields=match_fields, priority=priority) try: self.WriteTableEntry(table_entry, update_type=p4runtime_pb2.Update.DELETE) except grpc.RpcError as e: printGrpcError(e)
def main(p4info_file_path, bmv2_file_path, sw_num): # Instantiate a P4Runtime helper from the p4info file p4info_helper = helper.P4InfoHelper(p4info_file_path) s_name = 's' + str(sw_num) print "switch name: ", s_name try: ''' Create a switch connection object for s1 This is backed by a P4Runtime gRPC connection. Also, dump all P4Runtime messages sent to switch to given txt files. In the P4 package here, port no starts from 50051 ''' switch = p4runtime_lib.bmv2.Bmv2SwitchConnection( name=s_name, address='127.0.0.1:5005' + str(sw_num), device_id=0, proto_dump_file='logs/' + s_name + '-p4runtime-requests.txt') ''' Send master arbitration update message to establish this controller as master (required by P4Runtime before performing any other write operation) ''' switch.MasterArbitrationUpdate() print "Master arbitration done..." # readTableRules(p4info_helper, switch, None) # print print "<<<< CONTROLLER READEY! WAITING FOR PACKET_IN >>>>" while True: ''' USING P4RUNTIME AS RECEIVER FOR PACKET_IN IN THE EMBEDDED CONTROLLER ''' packetin = switch.PacketIn() if packetin.WhichOneof('update') == 'packet': packet, pkt_in_metadata, input_port, var_id = packet_in_metadata( packetin) input_port_mask = port_mask(input_port) print ">>>> Reading current entries in \"MyIngress.L2_publish\" table before updating >>>>" current_table = readTableRules(p4info_helper, switch, "MyIngress.L2_publish") print if len(current_table) == 0: try: writeL2Publish(p4info_helper, switch, var_id, input_port_mask) except: print "\nproblem writing table for them!\n" raise else: found = False for tbl_entry in current_table: tmp_match_value = '\x00' + tbl_entry['match'][ 'local_metadata.pubsub_indx'][0] if su("!I", tmp_match_value)[0] == var_id: old_mc_grp_id = su( "!H", tbl_entry['action_params']['st_mc_grp']) new_mc_grp_id = int( old_mc_grp_id[0]) | input_port_mask try: ## DELETE and WRITE print "MODIFY(Delete and Write) for ", var_id, " from ", old_mc_grp_id, " to ", new_mc_grp_id deleteL2Publish(p4info_helper, switch, var_id, old_mc_grp_id) writeL2Publish(p4info_helper, switch, var_id, new_mc_grp_id) ## OR MODIFY # modifyL2Publish(p4info_helper, switch, var_id, new_mc_grp_id) except: print "\nproblem writing table for them!\n" # raise found = True break if not found: try: writeL2Publish(p4info_helper, switch, var_id, input_port_mask) except: print "\nproblem writing table for them!\n" raise ''' USING P4RUNTIME FOR PACKET_OUT IN THE CONTROLLER ''' my_packet_out = p4info_helper.buildPacketOut( payload=packet, metadata={1: pkt_in_metadata[1]}) print "Donig PacketOut.\n" # my_packet_out_dup=copy.deepcopy(my_packet_out) # switch.PacketOut(my_packet_out_dup) switch.PacketOut(my_packet_out) print "PacketOut Done." print "==============================================\n" except KeyboardInterrupt: # using ctrl + c to exit # Then close all the connections print " Shutting down....." except grpc.RpcError as e: printGrpcError(e) ShutdownAllSwitchConnections()
def main(p4info_file_path, bmv2_file_path): # Instantiate a P4Runtime helper from the p4info file p4info_helper = p4runtime_lib.helper.P4InfoHelper(p4info_file_path) try: # Create a switch connection object for s1 and s2; # this is backed by a P4Runtime gRPC connection. # Also, dump all P4Runtime messages sent to switch to given txt files. s1 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s1', address='127.0.0.1:50051', device_id=0, proto_dump_file='logs/s1-p4runtime-requests.txt') s2 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s2', address='127.0.0.1:50052', device_id=1, proto_dump_file='logs/s2-p4runtime-requests.txt') s3 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s3', address='127.0.0.1:50053', device_id=2, proto_dump_file='logs/s3-p4runtime-requests.txt') s4 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s4', address='127.0.0.1:50054', device_id=3, proto_dump_file='logs/s4-p4runtime-requests.txt') s5 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s5', address='127.0.0.1:50055', device_id=4, proto_dump_file='logs/s5-p4runtime-requests.txt') s6 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s6', address='127.0.0.1:50056', device_id=5, proto_dump_file='logs/s6-p4runtime-requests.txt') s7 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s7', address='127.0.0.1:50057', device_id=6, proto_dump_file='logs/s7-p4runtime-requests.txt') s8 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s8', address='127.0.0.1:50058', device_id=7, proto_dump_file='logs/s8-p4runtime-requests.txt') s9 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s9', address='127.0.0.1:50059', device_id=8, proto_dump_file='logs/s9-p4runtime-requests.txt') s10 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s10', address='127.0.0.1:50060', device_id=9, proto_dump_file='logs/s10-p4runtime-requests.txt') s11 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s11', address='127.0.0.1:50061', device_id=10, proto_dump_file='logs/s11-p4runtime-requests.txt') s12 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s12', address='127.0.0.1:50062', device_id=11, proto_dump_file='logs/s12-p4runtime-requests.txt') s13 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s13', address='127.0.0.1:50063', device_id=12, proto_dump_file='logs/s13-p4runtime-requests.txt') s14 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s14', address='127.0.0.1:50064', device_id=13, proto_dump_file='logs/s14-p4runtime-requests.txt') s15 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s15', address='127.0.0.1:50065', device_id=14, proto_dump_file='logs/s15-p4runtime-requests.txt') s16 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s16', address='127.0.0.1:50066', device_id=15, proto_dump_file='logs/s16-p4runtime-requests.txt') s17 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s17', address='127.0.0.1:50067', device_id=16, proto_dump_file='logs/s17-p4runtime-requests.txt') # Send master arbitration update message to establish this controller as # master (required by P4Runtime before performing any other write operation) s1.MasterArbitrationUpdate() s2.MasterArbitrationUpdate() s3.MasterArbitrationUpdate() s4.MasterArbitrationUpdate() s5.MasterArbitrationUpdate() s6.MasterArbitrationUpdate() s7.MasterArbitrationUpdate() s8.MasterArbitrationUpdate() s9.MasterArbitrationUpdate() s10.MasterArbitrationUpdate() s11.MasterArbitrationUpdate() s12.MasterArbitrationUpdate() s13.MasterArbitrationUpdate() s14.MasterArbitrationUpdate() s15.MasterArbitrationUpdate() s16.MasterArbitrationUpdate() s17.MasterArbitrationUpdate() # Install the P4 program on the switches s1.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) s2.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) s3.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) s4.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) s5.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) s6.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) s7.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) s8.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) s9.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) s10.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) s11.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) s12.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) s13.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) s14.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) s15.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) s16.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) s17.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print( "Installed P4 Program using SetForwardingPipelineConfig on s1 - s17" ) # Write the forwarding rules # from switch to host writeHostForwardRules(p4info_helper, ingress_sw=s1, dst_eth_addr="08:00:00:00:10:01", port=1, dst_ip_addr="10.0.1.1") writeHostForwardRules(p4info_helper, ingress_sw=s6, dst_eth_addr="08:00:00:00:11:00", port=2, dst_ip_addr="11.0.0.1") writeHostForwardRules(p4info_helper, ingress_sw=s7, dst_eth_addr="08:00:00:00:11:01", port=2, dst_ip_addr="11.0.1.1") writeHostForwardRules(p4info_helper, ingress_sw=s8, dst_eth_addr="08:00:00:00:11:02", port=2, dst_ip_addr="11.0.2.1") writeHostForwardRules(p4info_helper, ingress_sw=s9, dst_eth_addr="08:00:00:00:11:10", port=2, dst_ip_addr="11.1.0.1") writeHostForwardRules(p4info_helper, ingress_sw=s10, dst_eth_addr="08:00:00:00:11:11", port=2, dst_ip_addr="11.1.1.1") writeHostForwardRules(p4info_helper, ingress_sw=s11, dst_eth_addr="08:00:00:00:11:12", port=2, dst_ip_addr="11.1.2.1") writeHostForwardRules(p4info_helper, ingress_sw=s12, dst_eth_addr="08:00:00:00:11:20", port=2, dst_ip_addr="11.2.0.1") writeHostForwardRules(p4info_helper, ingress_sw=s13, dst_eth_addr="08:00:00:00:11:21", port=2, dst_ip_addr="11.2.1.1") writeHostForwardRules(p4info_helper, ingress_sw=s14, dst_eth_addr="08:00:00:00:11:22", port=2, dst_ip_addr="11.2.2.1") writeHostForwardRules(p4info_helper, ingress_sw=s15, dst_eth_addr="08:00:00:00:11:03", port=2, dst_ip_addr="11.0.3.1") # from host to host # s1 writeTunnelForwardRules(p4info_helper, ingress_sw=s1, port=2, dst_ip_addr="11.0.0.0", prefix=8) # s2 #writeTunnelForwardRules(p4info_helper,ingress_sw=s2,port=1,dst_ip_addr="10.0.0.0",prefix=8) writeTunnelForwardRules(p4info_helper, ingress_sw=s2, port=2, dst_ip_addr="11.0.0.0", prefix=16) writeTunnelForwardRules(p4info_helper, ingress_sw=s2, port=3, dst_ip_addr="11.1.0.0", prefix=16) writeTunnelForwardRules(p4info_helper, ingress_sw=s2, port=4, dst_ip_addr="11.2.0.0", prefix=16) # s3 writeTunnelForwardRules(p4info_helper, ingress_sw=s3, port=1, dst_ip_addr='10.0.0.0', prefix=8) writeTunnelForwardRules(p4info_helper, ingress_sw=s3, port=1, dst_ip_addr='11.1.0.0', prefix=16) writeTunnelForwardRules(p4info_helper, ingress_sw=s3, port=1, dst_ip_addr='11.2.0.0', prefix=16) writeTunnelForwardRules(p4info_helper, ingress_sw=s3, port=2, dst_ip_addr='11.0.0.0', prefix=24) writeTunnelForwardRules(p4info_helper, ingress_sw=s3, port=3, dst_ip_addr='11.0.1.0', prefix=24) writeTunnelForwardRules(p4info_helper, ingress_sw=s3, port=4, dst_ip_addr='11.0.2.0', prefix=24) writeTunnelForwardRules(p4info_helper, ingress_sw=s3, port=5, dst_ip_addr='11.0.3.0', prefix=24) # s4 writeTunnelForwardRules(p4info_helper, ingress_sw=s4, port=1, dst_ip_addr='10.0.0.0', prefix=8) writeTunnelForwardRules(p4info_helper, ingress_sw=s4, port=1, dst_ip_addr='11.0.0.0', prefix=16) writeTunnelForwardRules(p4info_helper, ingress_sw=s4, port=1, dst_ip_addr='11.2.0.0', prefix=16) writeTunnelForwardRules(p4info_helper, ingress_sw=s4, port=2, dst_ip_addr='11.1.0.0', prefix=24) writeTunnelForwardRules(p4info_helper, ingress_sw=s4, port=3, dst_ip_addr='11.1.1.0', prefix=24) writeTunnelForwardRules(p4info_helper, ingress_sw=s4, port=4, dst_ip_addr='11.1.2.0', prefix=24) # s5 writeTunnelForwardRules(p4info_helper, ingress_sw=s5, port=1, dst_ip_addr="10.0.0.0", prefix=8) writeTunnelForwardRules(p4info_helper, ingress_sw=s5, port=1, dst_ip_addr="11.0.0.0", prefix=16) writeTunnelForwardRules(p4info_helper, ingress_sw=s5, port=1, dst_ip_addr="11.1.0.0", prefix=16) writeTunnelForwardRules(p4info_helper, ingress_sw=s5, port=2, dst_ip_addr="11.2.0.0", prefix=24) writeTunnelForwardRules(p4info_helper, ingress_sw=s5, port=3, dst_ip_addr="11.2.1.0", prefix=24) writeTunnelForwardRules(p4info_helper, ingress_sw=s5, port=4, dst_ip_addr="11.2.2.0", prefix=24) # s6-s15 only connect to server(target) writeTunnelForwardRules(p4info_helper, ingress_sw=s6, port=1, dst_ip_addr="10.0.0.0", prefix=8) writeTunnelForwardRules(p4info_helper, ingress_sw=s7, port=1, dst_ip_addr="10.0.0.0", prefix=8) writeTunnelForwardRules(p4info_helper, ingress_sw=s8, port=1, dst_ip_addr="10.0.0.0", prefix=8) writeTunnelForwardRules(p4info_helper, ingress_sw=s9, port=1, dst_ip_addr="10.0.0.0", prefix=8) writeTunnelForwardRules(p4info_helper, ingress_sw=s10, port=1, dst_ip_addr="10.0.0.0", prefix=8) writeTunnelForwardRules(p4info_helper, ingress_sw=s11, port=1, dst_ip_addr="10.0.0.0", prefix=8) writeTunnelForwardRules(p4info_helper, ingress_sw=s12, port=1, dst_ip_addr="10.0.0.0", prefix=8) writeTunnelForwardRules(p4info_helper, ingress_sw=s13, port=1, dst_ip_addr="10.0.0.0", prefix=8) writeTunnelForwardRules(p4info_helper, ingress_sw=s14, port=1, dst_ip_addr="10.0.0.0", prefix=8) writeTunnelForwardRules(p4info_helper, ingress_sw=s15, port=1, dst_ip_addr="10.0.0.0", prefix=8) # s16 writeTunnelForwardRules(p4info_helper, ingress_sw=s16, port=1, dst_ip_addr="10.0.0.0", prefix=8) writeTunnelForwardRules(p4info_helper, ingress_sw=s16, port=2, dst_ip_addr="11.0.0.0", prefix=8) # s17 writeTunnelForwardRules(p4info_helper, ingress_sw=s17, port=1, dst_ip_addr="10.0.0.0", prefix=8) writeTunnelForwardRules(p4info_helper, ingress_sw=s17, port=2, dst_ip_addr="11.0.0.0", prefix=8) # authentication cookie_list = [] for i in range(16): for j in range(16): keyNum = random.randint(0, 2147483647) cookie_list.append(keyNum) writeSecretNumberRule(p4info_helper, ingress_sw=s2, src_ip_addr=i * 256, port=j * 16, key=keyNum) print("Install authentication rule on s2") # digest # insert pinhole SendDigestEntry(p4info_helper, sw=s2, digest_name="auth_digest") # abnormal pinhole SendDigestEntry(p4info_helper, sw=s2, digest_name="abnormal_digest") blacklist = [] while True: print("=========================================") digests = s2.DigestList() # print (digests) # digest if digests.WhichOneof("update") == "digest": digest = digests.digest digest_name = p4info_helper.get_digests_name(digest.digest_id) if digest_name == "auth_digest": srcIP = prettify( digest.data[0].struct.members[0].bitstring) srcPORT = int_value( digest.data[0].struct.members[1].bitstring, 16) print("[auth-success]") # write pinhole writePinholeRule(p4info_helper, ingress_sw=s2, src_ip_addr=srcIP, src_tcp_port=srcPORT, dst_ip_addr="10.0.1.1", dst_tcp_port=5001, egress_port=1) prefix16 = srcIP.replace('.', '+', 1).split('.')[0].replace( '+', '.') if (prefix16 == "11.0"): writePinholeRule(p4info_helper, ingress_sw=s2, src_ip_addr="10.0.1.1", src_tcp_port=5001, dst_ip_addr=srcIP, dst_tcp_port=srcPORT, egress_port=2) elif (prefix16 == "11.1"): writePinholeRule(p4info_helper, ingress_sw=s2, src_ip_addr="10.0.1.1", src_tcp_port=5001, dst_ip_addr=srcIP, dst_tcp_port=srcPORT, egress_port=3) elif (prefix16 == "11.2"): writePinholeRule(p4info_helper, ingress_sw=s2, src_ip_addr="10.0.1.1", src_tcp_port=5001, dst_ip_addr=srcIP, dst_tcp_port=srcPORT, egress_port=4) elif digest_name == "abnormal_digest": srcIP = prettify( digest.data[0].struct.members[0].bitstring) srcPORT = int_value( digest.data[0].struct.members[1].bitstring, 16) print("[abnormal]") # delete pinhole deletePinholeRule(p4info_helper, ingress_sw=s2, src_ip_addr=srcIP, src_tcp_port=srcPORT, dst_ip_addr="10.0.1.1", dst_tcp_port=5001) deletePinholeRule(p4info_helper, ingress_sw=s2, src_ip_addr="10.0.1.1", src_tcp_port=5001, dst_ip_addr=srcIP, dst_tcp_port=srcPORT) # write into blacklist danger = srcIP + ":" + str(srcPORT) if danger in blacklist: writeDropForwardRules(p4info_helper, ingress_sw=s2, src_ip_addr=srcIP, src_tcp_port=srcPORT, TTL=-1) # no timeout (forever) else: blacklist.append(danger) writeDropForwardRules(p4info_helper, ingress_sw=s2, src_ip_addr=srcIP, src_tcp_port=srcPORT, TTL=600000000000) # 10 min # # update cookie # ip_index = int(srcIP.split('.')[2])%16 # port_index = int(srcPORT%256/16) # new_key = random.randint(0,2147483647) # deleteSecretNumberRule(p4info_helper,ingress_sw=s2, # src_ip_addr=ip_index*256,port=port_index*16,key=cookie_list[ip_index*16+port_index]) # writeSecretNumberRule(p4info_helper,ingress_sw=s2,src_ip_addr=ip_index*256,port=port_index*16,key=new_key) # print("[update-cookie]") # print("Update new authentication rule on s2 **.**.*%s.**:**%s*") % (ip_index,port_index) # timeout notification elif digests.WhichOneof("update") == "idle_timeout_notification": notification = digests.idle_timeout_notification for entry in notification.table_entry: table_name = p4info_helper.get_tables_name(entry.table_id) if table_name == "BasicIngress.pinhole": print("[pinhole-entry timeout]") srcIP = prettify(entry.match[0].exact.value) srcPort = int_value(entry.match[1].exact.value, 16) dstIP = prettify(entry.match[2].exact.value) dstPort = int_value(entry.match[3].exact.value, 16) # delete pinhole deletePinholeRule(p4info_helper, ingress_sw=s2, src_ip_addr=srcIP, src_tcp_port=srcPort, dst_ip_addr=dstIP, dst_tcp_port=dstPort) elif table_name == "BasicIngress.drop_blacklist": print("[blacklist-entry timeout]") srcIP = prettify(entry.match[0].lpm.value) srcPort = int_value(entry.match[1].exact.value, 16) # delete black list deleteDropForwardRules(p4info_helper, ingress_sw=s2, src_ip_addr=srcIP, src_tcp_port=srcPORT) except KeyboardInterrupt: print(" Shutting down.") except grpc.RpcError as e: printGrpcError(e) ShutdownAllSwitchConnections()
def main(p4info_file_path, bmv2_file_path): # Instantiate a P4Runtime helper from the p4info file p4info_helper = p4runtime_lib.helper.P4InfoHelper(p4info_file_path) try: # Create switch connection objects for s1, s2, and s3; # this is backed by a P4Runtime gRPC connection. # Also, dump all P4Runtime messages sent to switch to given txt files. s1 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s1', address='127.0.0.1:50051', device_id=0, proto_dump_file='logs/s1-p4runtime-requests.txt') s2 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s2', address='127.0.0.1:50052', device_id=1, proto_dump_file='logs/s2-p4runtime-requests.txt') s3 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s3', address='127.0.0.1:50053', device_id=2, proto_dump_file='logs/s3-p4runtime-requests.txt') # Send master arbitration update message to establish this controller as # master (required by P4Runtime before performing any other write operation) s1.MasterArbitrationUpdate() s2.MasterArbitrationUpdate() s3.MasterArbitrationUpdate() # Install the P4 program on the switches s1.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s1" s2.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s2" s3.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s3" # Switch port mapping: # s1: 1:h1 2:s2 3:s3 # s2: 1:h2 2:s1 3:s3 # s3: 1:h3 2:s1 3:s2 writeForwardingRule(p4info_helper, switch=s1, dst_ip_addr="10.0.1.1", dst_eth_addr="00:00:00:00:01:01", egress_port=1) writeForwardingRule(p4info_helper, switch=s1, dst_ip_addr="10.0.2.2", dst_eth_addr="00:00:00:00:02:02", egress_port=2) writeForwardingRule(p4info_helper, switch=s2, dst_ip_addr="10.0.1.1", dst_eth_addr="00:00:00:00:01:01", egress_port=2) writeForwardingRule(p4info_helper, switch=s2, dst_ip_addr="10.0.2.2", dst_eth_addr="00:00:00:00:02:02", egress_port=1) writeFilteringRules(p4info_helper, switch=s1, protocol="tcp", port=1234) setupMirrorSession(p4info_helper, switch=s1, session_id=1, egress_port=3) readTableRules(p4info_helper, s1) readTableRules(p4info_helper, s2) readTableRules(p4info_helper, s3) except KeyboardInterrupt: print " Shutting down." except grpc.RpcError as e: printGrpcError(e) ShutdownAllSwitchConnections()
def main(p4info_file_path, bmv2_file_path): # Instantiate a P4Runtime helper from the p4info file p4info_helper = p4runtime_lib.helper.P4InfoHelper(p4info_file_path) try: # Create a switch connection object for s1 and s2; # this is backed by a P4Runtime gRPC connection. # Also, dump all P4Runtime messages sent to switch to given txt files. s1 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s1', address='127.0.0.1:50051', device_id=0, proto_dump_file='logs/s1-p4runtime-requests.txt') s2 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s2', address='127.0.0.1:50052', device_id=1, proto_dump_file='logs/s2-p4runtime-requests.txt') s3 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s3', address='127.0.0.1:50053', device_id=2, proto_dump_file='logs/s3-p4runtime-requests.txt') switches = {0: s1, 1: s2, 2: s3} # Send master arbitration update message to establish this controller as # master (required by P4Runtime before performing any other write operation) s1.MasterArbitrationUpdate() s2.MasterArbitrationUpdate() s3.MasterArbitrationUpdate() # Install the P4 program on the switches s1.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s1" s2.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s2" s3.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s3" # deal with table entries TABLE_NAME = "ipv4_lpm" # from_sw:{ to_sw: [rules] } accept_rules = defaultdict(dict) # start print digest in separate threads ready = [False for _ in switches.items()] lock = threading.Lock() for idx, (_, sw) in enumerate(switches.items()): t = threading.Thread(target=printDigests, args=(p4info_helper, sw, idx, lock, ready)) t.start() while True: lock.acquire() result = True for state in ready: result = result and state if result: lock.release() break else: lock.release() while True: rule = raw_input("# Please enter a command: ") rules = rule.split(" ") rule = rules[0] if rule == 'drop': if len(rules) > 1: from_sw_id, to_sw_id = int(rules[1]), int(rules[2]) if to_sw_id not in accept_rules[from_sw_id]: print "There's no allowed traffic from switch %s to %s" % ( from_sw_id, to_sw_id) else: switches[from_sw_id].DeleteTableEntry( accept_rules[from_sw_id][to_sw_id]) accept_rules[from_sw_id].pop(to_sw_id, None) print "Connection from %s to %s dropped" % (from_sw_id, to_sw_id) else: # write all drop table rules writeDropEntry(p4info_helper, s1, TABLE_NAME) writeDropEntry(p4info_helper, s2, TABLE_NAME) writeDropEntry(p4info_helper, s3, TABLE_NAME) print "Installed drop rules on s1, s2, s3" elif rule == 'accept': if s1.device_id not in accept_rules[s1.device_id]: r = writeTableEntry(p4info_helper, s1, TABLE_NAME, "00:00:00:00:01:01", 1, "10.0.1.1") accept_rules[s1.device_id][s1.device_id] = r else: print "Already allow traffic from switch %s to %s" % ( s1.device_id, s1.device_id) if s2.device_id not in accept_rules[s1.device_id]: r = writeTableEntry(p4info_helper, s1, TABLE_NAME, "00:00:00:02:02:00", 2, "10.0.2.2") accept_rules[s1.device_id][s2.device_id] = r else: print "Already allow traffic from switch %s to %s" % ( s1.device_id, s2.device_id) if s3.device_id not in accept_rules[s1.device_id]: r = writeTableEntry(p4info_helper, s1, TABLE_NAME, "00:00:00:03:03:00", 3, "10.0.3.3") accept_rules[s1.device_id][s3.device_id] = r else: print "Already allow traffic from switch %s to %s" % ( s1.device_id, s3.device_id) print "Installed transit rules for s1" if s1.device_id not in accept_rules[s2.device_id]: r = writeTableEntry(p4info_helper, s2, TABLE_NAME, "00:00:00:01:01:00", 2, "10.0.1.1") accept_rules[s2.device_id][s1.device_id] = r else: print "Already allow traffic from switch %s to %s" % ( s2.device_id, s1.device_id) if s2.device_id not in accept_rules[s2.device_id]: r = writeTableEntry(p4info_helper, s2, TABLE_NAME, "00:00:00:00:02:02", 1, "10.0.2.2") accept_rules[s2.device_id][s2.device_id] = r else: print "Already allow traffic from switch %s to %s" % ( s2.device_id, s2.device_id) if s3.device_id not in accept_rules[s2.device_id]: r = writeTableEntry(p4info_helper, s2, TABLE_NAME, "00:00:00:03:03:00", 3, "10.0.3.3") accept_rules[s2.device_id][s3.device_id] = r else: print "Already allow traffic from switch %s to %s" % ( s2.device_id, s3.device_id) print "Installed transit rules for s2" if s1.device_id not in accept_rules[s3.device_id]: r = writeTableEntry(p4info_helper, s3, TABLE_NAME, "00:00:00:01:01:00", 2, "10.0.1.1") accept_rules[s3.device_id][s1.device_id] = r else: print "Already allow traffic from switch %s to %s" % ( s3.device_id, s1.device_id) if s2.device_id not in accept_rules[s3.device_id]: r = writeTableEntry(p4info_helper, s3, TABLE_NAME, "00:00:00:02:02:00", 3, "10.0.2.2") accept_rules[s3.device_id][s2.device_id] = r else: print "Already allow traffic from switch %s to %s" % ( s3.device_id, s2.device_id) if s3.device_id not in accept_rules[s3.device_id]: r = writeTableEntry(p4info_helper, s3, TABLE_NAME, "00:00:00:00:03:03", 1, "10.0.3.3") accept_rules[s3.device_id][s3.device_id] = r else: print "Already allow traffic from switch %s to %s" % ( s3.device_id, s3.device_id) print "Installed transit rules for s3" # read table rules readTableRules(p4info_helper, s1) readTableRules(p4info_helper, s2) readTableRules(p4info_helper, s3) except grpc.RpcError as e: printGrpcError(e) except KeyboardInterrupt: print " Shutting down." ShutdownAllSwitchConnections()
def main(p4info_file_path, bmv2_file_path): # Instantiate a P4Runtime helper from the p4info file p4info_helper = p4runtime_lib.helper.P4InfoHelper(p4info_file_path) try: # Create a switch connection object for s1 and s2; # this is backed by a P4Runtime gRPC connection. # Also, dump all P4Runtime messages sent to switch to given txt files. s1 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s1', address='127.0.0.1:50051', device_id=0, proto_dump_file='logs/s1-p4runtime-requests.txt') s2 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s2', address='127.0.0.1:50052', device_id=1, proto_dump_file='logs/s2-p4runtime-requests.txt') s3 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s3', address='127.0.0.1:50053', device_id=2, proto_dump_file='logs/s3-p4runtime-requests.txt') # Send master arbitration update message to establish this controller as # master (required by P4Runtime before performing any other write operation) s1.MasterArbitrationUpdate() s2.MasterArbitrationUpdate() s3.MasterArbitrationUpdate() # Install the P4 program on the switches s1.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s1" s2.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s2" s3.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s3" # Write the rules that forward traffic from s1 writeForwardingRules(p4info_helper, ingress_sw=s1, port=1, dst_eth_addr="08:00:00:00:01:11", dst_ip_addr="10.0.1.1") writeForwardingRules(p4info_helper, ingress_sw=s1, port=2, dst_eth_addr="08:00:00:00:02:22", dst_ip_addr="10.0.2.2") writeForwardingRules(p4info_helper, ingress_sw=s1, port=3, dst_eth_addr="08:00:00:00:03:33", dst_ip_addr="10.0.3.3") # Write the rules that tunnel traffic from s2 writeForwardingRules(p4info_helper, ingress_sw=s2, port=1, dst_eth_addr="08:00:00:00:02:22", dst_ip_addr="10.0.2.2") writeForwardingRules(p4info_helper, ingress_sw=s2, port=2, dst_eth_addr="08:00:00:00:01:11", dst_ip_addr="10.0.1.1") writeForwardingRules(p4info_helper, ingress_sw=s2, port=3, dst_eth_addr="08:00:00:00:03:33", dst_ip_addr="10.0.3.3") # Write the rules that forward traffic from s3 writeForwardingRules(p4info_helper, ingress_sw=s3, port=1, dst_eth_addr="08:00:00:00:03:33", dst_ip_addr="10.0.3.3") writeForwardingRules(p4info_helper, ingress_sw=s3, port=2, dst_eth_addr="08:00:00:00:01:11", dst_ip_addr="10.0.1.1") writeForwardingRules(p4info_helper, ingress_sw=s3, port=3, dst_eth_addr="08:00:00:00:02:22", dst_ip_addr="10.0.2.2") readTableRules(p4info_helper, s1) readTableRules(p4info_helper, s2) readTableRules(p4info_helper, s3) except KeyboardInterrupt: print " Shutting down." except grpc.RpcError as e: printGrpcError(e) ShutdownAllSwitchConnections()
def main(p4info_file_path, bmv2_file_path): # Instantiate a P4Runtime helper from the p4info file p4info_helper = p4runtime_lib.helper.P4InfoHelper(p4info_file_path) try: # Create a switch connection object for s1 and s2; # this is backed by a P4Runtime gRPC connection. # Also, dump all P4Runtime messages sent to switch to given txt files. s1 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s1', address='127.0.0.1:50052', device_id=1, proto_dump_file='logs/s1-p4runtime-requests.txt') s2 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s2', address='127.0.0.1:50053', device_id=2, proto_dump_file='logs/s2-p4runtime-requests.txt') # Send master arbitration update message to establish this controller as # master (required by P4Runtime before performing any other write operation) s1.MasterArbitrationUpdate() s2.MasterArbitrationUpdate() #s1.MasterArbitrationUpdateComplete() #s2.MasterArbitrationUpdateComplete() # Install the P4 program on the switches s1.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s1" s2.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s2" table_entry = p4info_helper.buildTableEntry( table_name="MyIngress.ipv4_lpm", match_fields={"hdr.ipv4.dstAddr": ("10.0.2.22", 32)}, action_name="MyIngress.ipv4_forward", action_params={ "dstAddr": "08:00:00:00:02:22", "port": 4 }) s2.WriteTableEntry(table_entry) #f = open("/home/guilherme/Documents/p4runtime-grpc-v3/write_request_file.txt") #write_request = f.read() #if write_request is not None: # s2.WriteTableEntry(table_entry) #print s2.WriteTableEntry(table_entry) #print write_request #s2.WriteFlowRule(write_request) # print "Flow rule installed in the switch" # Write the rules that tunnel traffic from h1 to h2 #writeTunnelRules(p4info_helper, ingress_sw=s1, egress_sw=s2, tunnel_id=100, # dst_eth_addr="08:00:00:00:02:22", dst_ip_addr="10.0.2.2") # Write the rules that tunnel traffic from h2 to h1 #writeTunnelRules(p4info_helper, ingress_sw=s2, egress_sw=s1, tunnel_id=200, # dst_eth_addr="08:00:00:00:01:11", dst_ip_addr="10.0.1.1") # TODO Uncomment the following two lines to read table entries from s1 and s2 # readTableRules(p4info_helper, s1) # readTableRules(p4info_helper, s2) # Print the tunnel counters every 2 seconds except KeyboardInterrupt: print " Shutting down." except grpc.RpcError as e: printGrpcError(e) ShutdownAllSwitchConnections()
def main(p4info_file_path_sw, bmv2_file_path_sw, p4info_file_path_tor, bmv2_file_path_tor): # Instantiate a P4Runtime helper from the p4info file p4info_helper_sw = p4runtime_lib.helper.P4InfoHelper(p4info_file_path_sw) p4info_helper_tor = p4runtime_lib.helper.P4InfoHelper(p4info_file_path_tor) try: # Create a switch connection object for s1 and s2; # this is backed by a P4Runtime gRPC connection. # Also, dump all P4Runtime messages sent to switch to given txt files. s1 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s1', address='127.0.0.1:50051', device_id=0, proto_dump_file='logs/s1-p4runtime-requests.txt') s2 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s2', address='127.0.0.1:50052', device_id=1, proto_dump_file='logs/s2-p4runtime-requests.txt') s3 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s3', address='127.0.0.1:50053', device_id=2, proto_dump_file='logs/s3-p4runtime-requests.txt') s4 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s4', address='127.0.0.1:50054', device_id=3, proto_dump_file='logs/s4-p4runtime-requests.txt') stor1 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='stor1', address='127.0.0.1:50055', device_id=4, proto_dump_file='logs/stor1-p4runtime-requests.txt') stor2 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='stor2', address='127.0.0.1:50056', device_id=5, proto_dump_file='logs/stor2-p4runtime-requests.txt') # Send master arbitration update message to establish this controller as # master (required by P4Runtime before performing any other write operation) s1.MasterArbitrationUpdate() s2.MasterArbitrationUpdate() s3.MasterArbitrationUpdate() s4.MasterArbitrationUpdate() stor1.MasterArbitrationUpdate() stor2.MasterArbitrationUpdate() # Install the P4 program on the switches s1.SetForwardingPipelineConfig(p4info=p4info_helper_sw.p4info, bmv2_json_file_path=bmv2_file_path_sw) print "Installed P4 Program using SetForwardingPipelineConfig on s1" s2.SetForwardingPipelineConfig(p4info=p4info_helper_sw.p4info, bmv2_json_file_path=bmv2_file_path_sw) print "Installed P4 Program using SetForwardingPipelineConfig on s2" s3.SetForwardingPipelineConfig(p4info=p4info_helper_sw.p4info, bmv2_json_file_path=bmv2_file_path_sw) print "Installed P4 Program using SetForwardingPipelineConfig on s3" s4.SetForwardingPipelineConfig(p4info=p4info_helper_sw.p4info, bmv2_json_file_path=bmv2_file_path_sw) print "Installed P4 Program using SetForwardingPipelineConfig on s4" stor1.SetForwardingPipelineConfig( p4info=p4info_helper_tor.p4info, bmv2_json_file_path=bmv2_file_path_tor) print "Installed P4 Program using SetForwardingPipelineConfig on stor1" stor2.SetForwardingPipelineConfig( p4info=p4info_helper_tor.p4info, bmv2_json_file_path=bmv2_file_path_tor) print "Installed P4 Program using SetForwardingPipelineConfig on stor2" # Write the rules that tunnel traffic from h1 to h2 '''writeTunnelRules(p4info_helper, ingress_sw=s1, egress_sw=s2, tunnel_id=100, dst_eth_addr="08:00:00:00:02:22", dst_ip_addr="10.0.2.2") # Write the rules that tunnel traffic from h2 to h1 writeTunnelRules(p4info_helper, ingress_sw=s2, egress_sw=s1, tunnel_id=200, dst_eth_addr="08:00:00:00:01:11", dst_ip_addr="10.0.1.1")''' with open("tor1-runtime.json", 'r') as sw_conf_file: sw_conf = json_load_byteified(sw_conf_file) 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(stor1, entry, p4info_helper_tor) with open("tor2-runtime.json", 'r') as sw_conf_file: sw_conf = json_load_byteified(sw_conf_file) 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(stor2, entry, p4info_helper_tor) with open("s1-runtime.json", 'r') as sw_conf_file: sw_conf = json_load_byteified(sw_conf_file) 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(s1, entry, p4info_helper_sw) with open("s2-runtime.json", 'r') as sw_conf_file: sw_conf = json_load_byteified(sw_conf_file) 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(s2, entry, p4info_helper_sw) with open("s3-runtime.json", 'r') as sw_conf_file: sw_conf = json_load_byteified(sw_conf_file) 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(s3, entry, p4info_helper_sw) with open("s4-runtime.json", 'r') as sw_conf_file: sw_conf = json_load_byteified(sw_conf_file) 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(s4, entry, p4info_helper_sw) except KeyboardInterrupt: print " Shutting down." except grpc.RpcError as e: printGrpcError(e) ShutdownAllSwitchConnections()
def updateMulticastGroup(self, mgid=None, ports=None): group = self.p4info_helper.buildMulticastGroup(mgid=mgid, ports=ports) try: self.sw_conn.UpdateMulticastGroup(group) except grpc.RpcError as e: printGrpcError(e)
if __name__ == '__main__': p4info_file_path = './build/simple_recovery.p4.p4info.txt' p4info_helper = p4runtime_lib.helper.P4InfoHelper(p4info_file_path) try: s = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s1', address='127.0.0.1:50051', device_id=0, ) s.MasterArbitrationUpdate() # table_entry = p4info_helper.buildTableEntry( # table_name="MyIngress.ipv4_lpm", # match_fields={ # "hdr.ipv4.dstAddr": ("10.0.2.2", 32) # }, # action_name="MyIngress.try_ipv4_forward", # action_params={ # "port": 3, # } # ) # s.ModifyTableEntry(table_entry) for response in s.ReadRegisters(p4info_helper.get_registers_id("MyIngress.all_ports_status")): for entity in response.entities: registers = entity.register_entry print registers except KeyboardInterrupt: print " Shutting down." except grpc.RpcError as e: printGrpcError(e)