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. i = 51 add = '127.0.0.1:' + str(50000 + i) s1 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name=1, address='127.0.0.1:' + str(50007 + i), device_id=0, proto_dump_file='../P4/logs/s1-p4runtime-requests.txt') s2 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name=2, address='127.0.0.1:50052', device_id=1, proto_dump_file='../P4/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): p4info_helper = p4runtime_lib.helper.P4InfoHelper(p4info_file_path) try: 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') s1.MasterArbitrationUpdate() s2.MasterArbitrationUpdate() s3.MasterArbitrationUpdate() s4.MasterArbitrationUpdate() 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" s4.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s4" register_entry = p4info_helper.buildRegisterEntry( register_name = "ingress_register", index = 0, data = "\000" ) s1.WriteRegisterEntry(register_entry) print "Write device_id to register on s1" ############################################################################# writeIPRules(p4info_helper, sw=s1, dst_ip="10.0.1.1", mask=32, nhop_ip="10.0.1.1") writeIPRules(p4info_helper, sw=s1, dst_ip="10.0.1.2", mask=32, nhop_ip="10.0.1.2") writeIPRules(p4info_helper, sw=s1, dst_ip="10.0.4.0", mask=24, nhop_ip="140.116.82.2") writeNhopRules(p4info_helper, sw=s1, nhop_ip="140.116.82.2", port=3, dmac="00:00:02:02:02:02") writeNhopRules(p4info_helper, sw=s1, nhop_ip="10.0.1.1", port=1, dmac="00:00:00:00:01:01") writeNhopRules(p4info_helper, sw=s1, nhop_ip="10.0.1.2", port=2, dmac="00:00:00:00:01:02") writeIPRules(p4info_helper, sw=s2, dst_ip="10.0.1.0", mask=24, nhop_ip="140.116.82.1") writeIPRules(p4info_helper, sw=s2, dst_ip="10.0.4.0", mask=24, nhop_ip="140.116.82.3") writeNhopRules(p4info_helper, sw=s2, nhop_ip="140.116.82.1", port=1, dmac="00:00:01:01:01:01") writeNhopRules(p4info_helper, sw=s2, nhop_ip="140.116.82.3", port=2, dmac="00:00:03:03:03:03") writeIPRules(p4info_helper, sw=s3, dst_ip="10.0.1.0", mask=24, nhop_ip="140.116.82.2") writeIPRules(p4info_helper, sw=s3, dst_ip="10.0.4.0", mask=24, nhop_ip="140.116.82.4") writeNhopRules(p4info_helper, sw=s3, nhop_ip="140.116.82.2", port=1, dmac="00:00:02:02:02:02") writeNhopRules(p4info_helper, sw=s3, nhop_ip="140.116.82.4", port=2, dmac="00:00:04:04:04:04") writeIPRules(p4info_helper, sw=s4, dst_ip="10.0.4.3", mask=32, nhop_ip="10.0.4.3") writeIPRules(p4info_helper, sw=s4, dst_ip="10.0.4.4", mask=32, nhop_ip="10.0.4.4") writeIPRules(p4info_helper, sw=s4, dst_ip="10.0.1.0", mask=24, nhop_ip="140.116.82.3") writeNhopRules(p4info_helper, sw=s4, nhop_ip="140.116.82.3", port=3, dmac="00:00:03:03:03:03") writeNhopRules(p4info_helper, sw=s4, nhop_ip="10.0.4.3", port=1, dmac="00:00:00:00:04:03") writeNhopRules(p4info_helper, sw=s4, nhop_ip="10.0.4.4", port=2, dmac="00:00:00:00:04:04") ############################################################################# 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) try: s1 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name='s1', address='127.0.0.1:50051', device_id=0, proto_dump_file='logs/s1-p4runtime-requests.txt') s1.MasterArbitrationUpdate() 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(len(classfication)): a = protocol[i] id = len(a) - 1 del a[1:id] if (len(a) == 1): a.append(a[0]) b = srouce[i] id = len(b) - 1 del b[1:id] if (len(b) == 1): b.append(b[0]) c = dstination[i] id = len(c) - 1 del c[1:id] if (len(c) == 1): c.append(c[0]) ind = int(classfication[i]) ac = action[ind] a = [i + 1 for i in a] b = [i + 1 for i in b] c = [i + 1 for i in c] if ac == 0: writeactionrule(p4info_helper, s1, a, b, c, "MyIngress.drop", 0) else: writeactionrule(p4info_helper, s1, a, b, c, "MyIngress.ipv4_forward", ac) if len(proto) != 0: proto.append(0) proto.append(32) proto.sort() for i in range(len(proto) - 1): writefeature1rule(p4info_helper, s1, proto[i:i + 2], i + 1) else: writefeature1rule(p4info_helper, s1, [0, 32], 1) if len(src) != 0: src.append(0) src.append(65535) src.sort() for i in range(len(src) - 1): writefeature2rule(p4info_helper, s1, src[i:i + 2], i + 1) if len(dst) != 0: dst.append(0) dst.append(65535) dst.sort() for i in range(len(dst) - 1): writefeature3rule(p4info_helper, s1, dst[i:i + 2], i + 1) 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) try: 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') s1.MasterArbitrationUpdate() s2.MasterArbitrationUpdate() s3.MasterArbitrationUpdate() s4.MasterArbitrationUpdate() s5.MasterArbitrationUpdate() s6.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" s4.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s4" s5.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s5" s6.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s6" # Write rules for tracing the traffic for s in [s1, s2, s3, s4, s5, s6]: writeTraceRules(p4info_helper, sw=s) # Write rules for forward the traffic to h1 h1_ip_addr = "10.0.1.1" writeIpv4Rules(p4info_helper, s1, dst_eth_addr="08:00:00:00:01:11", dst_ip_addr=h1_ip_addr, egress_port=1) writeIpv4Rules(p4info_helper, s2, dst_eth_addr="08:00:00:00:01:00", dst_ip_addr=h1_ip_addr, egress_port=3) writeIpv4Rules(p4info_helper, s3, dst_eth_addr="08:00:00:00:01:00", dst_ip_addr=h1_ip_addr, egress_port=3) writeIpv4Rules(p4info_helper, s4, dst_eth_addr="08:00:00:00:01:00", dst_ip_addr=h1_ip_addr, egress_port=3) writeIpv4Rules(p4info_helper, s5, dst_eth_addr="08:00:00:00:01:00", dst_ip_addr=h1_ip_addr, egress_port=1) writeIpv4Rules(p4info_helper, s6, dst_eth_addr="08:00:00:00:01:00", dst_ip_addr=h1_ip_addr, egress_port=1) h2_ip_addr = "10.0.2.2" writeIpv4Rules(p4info_helper, s1, dst_eth_addr="08:00:00:00:02:22", dst_ip_addr=h2_ip_addr, egress_port=2) writeIpv4Rules(p4info_helper, s2, dst_eth_addr="08:00:00:00:02:00", dst_ip_addr=h2_ip_addr, egress_port=3) writeIpv4Rules(p4info_helper, s3, dst_eth_addr="08:00:00:00:02:00", dst_ip_addr=h2_ip_addr, egress_port=3) writeIpv4Rules(p4info_helper, s4, dst_eth_addr="08:00:00:00:02:00", dst_ip_addr=h2_ip_addr, egress_port=3) writeIpv4Rules(p4info_helper, s5, dst_eth_addr="08:00:00:00:02:00", dst_ip_addr=h2_ip_addr, egress_port=1) writeIpv4Rules(p4info_helper, s6, dst_eth_addr="08:00:00:00:02:00", dst_ip_addr=h2_ip_addr, egress_port=1) h3_ip_addr = "10.0.3.3" writeIpv4Rules(p4info_helper, s1, dst_eth_addr="08:00:00:00:03:00", dst_ip_addr=h3_ip_addr, egress_port=3) writeIpv4Rules(p4info_helper, s2, dst_eth_addr="08:00:00:00:03:33", dst_ip_addr=h3_ip_addr, egress_port=1) writeIpv4Rules(p4info_helper, s3, dst_eth_addr="08:00:00:00:03:00", dst_ip_addr=h3_ip_addr, egress_port=3) writeIpv4Rules(p4info_helper, s4, dst_eth_addr="08:00:00:00:03:00", dst_ip_addr=h3_ip_addr, egress_port=3) writeIpv4Rules(p4info_helper, s5, dst_eth_addr="08:00:00:00:03:00", dst_ip_addr=h3_ip_addr, egress_port=2) writeIpv4Rules(p4info_helper, s6, dst_eth_addr="08:00:00:00:03:00", dst_ip_addr=h3_ip_addr, egress_port=2) h4_ip_addr = "10.0.4.4" writeIpv4Rules(p4info_helper, s1, dst_eth_addr="08:00:00:00:04:00", dst_ip_addr=h4_ip_addr, egress_port=3) writeIpv4Rules(p4info_helper, s2, dst_eth_addr="08:00:00:00:04:44", dst_ip_addr=h4_ip_addr, egress_port=2) writeIpv4Rules(p4info_helper, s3, dst_eth_addr="08:00:00:00:04:00", dst_ip_addr=h4_ip_addr, egress_port=3) writeIpv4Rules(p4info_helper, s4, dst_eth_addr="08:00:00:00:04:00", dst_ip_addr=h4_ip_addr, egress_port=3) writeIpv4Rules(p4info_helper, s5, dst_eth_addr="08:00:00:00:04:00", dst_ip_addr=h4_ip_addr, egress_port=2) writeIpv4Rules(p4info_helper, s6, dst_eth_addr="08:00:00:00:04:00", dst_ip_addr=h4_ip_addr, egress_port=2) h5_ip_addr = "10.0.5.5" writeIpv4Rules(p4info_helper, s1, dst_eth_addr="08:00:00:00:05:00", dst_ip_addr=h5_ip_addr, egress_port=4) writeIpv4Rules(p4info_helper, s2, dst_eth_addr="08:00:00:00:05:00", dst_ip_addr=h5_ip_addr, egress_port=4) writeIpv4Rules(p4info_helper, s3, dst_eth_addr="08:00:00:00:05:55", dst_ip_addr=h5_ip_addr, egress_port=1) writeIpv4Rules(p4info_helper, s4, dst_eth_addr="08:00:00:00:05:00", dst_ip_addr=h5_ip_addr, egress_port=4) writeIpv4Rules(p4info_helper, s5, dst_eth_addr="08:00:00:00:05:00", dst_ip_addr=h5_ip_addr, egress_port=3) writeIpv4Rules(p4info_helper, s6, dst_eth_addr="08:00:00:00:05:00", dst_ip_addr=h5_ip_addr, egress_port=3) h6_ip_addr = "10.0.6.6" writeIpv4Rules(p4info_helper, s1, dst_eth_addr="08:00:00:00:06:00", dst_ip_addr=h6_ip_addr, egress_port=4) writeIpv4Rules(p4info_helper, s2, dst_eth_addr="08:00:00:00:06:00", dst_ip_addr=h6_ip_addr, egress_port=4) writeIpv4Rules(p4info_helper, s3, dst_eth_addr="08:00:00:00:06:66", dst_ip_addr=h6_ip_addr, egress_port=2) writeIpv4Rules(p4info_helper, s4, dst_eth_addr="08:00:00:00:06:00", dst_ip_addr=h6_ip_addr, egress_port=4) writeIpv4Rules(p4info_helper, s5, dst_eth_addr="08:00:00:00:06:00", dst_ip_addr=h6_ip_addr, egress_port=3) writeIpv4Rules(p4info_helper, s6, dst_eth_addr="08:00:00:00:06:00", dst_ip_addr=h6_ip_addr, egress_port=3) h7_ip_addr = "10.0.7.7" writeIpv4Rules(p4info_helper, s1, dst_eth_addr="08:00:00:00:07:00", dst_ip_addr=h7_ip_addr, egress_port=4) writeIpv4Rules(p4info_helper, s2, dst_eth_addr="08:00:00:00:07:00", dst_ip_addr=h7_ip_addr, egress_port=4) writeIpv4Rules(p4info_helper, s3, dst_eth_addr="08:00:00:00:07:00", dst_ip_addr=h7_ip_addr, egress_port=4) writeIpv4Rules(p4info_helper, s4, dst_eth_addr="08:00:00:00:07:77", dst_ip_addr=h7_ip_addr, egress_port=1) writeIpv4Rules(p4info_helper, s5, dst_eth_addr="08:00:00:00:07:00", dst_ip_addr=h7_ip_addr, egress_port=4) writeIpv4Rules(p4info_helper, s6, dst_eth_addr="08:00:00:00:07:00", dst_ip_addr=h7_ip_addr, egress_port=4) h8_ip_addr = "10.0.8.8" writeIpv4Rules(p4info_helper, s1, dst_eth_addr="08:00:00:00:08:00", dst_ip_addr=h8_ip_addr, egress_port=4) writeIpv4Rules(p4info_helper, s2, dst_eth_addr="08:00:00:00:08:00", dst_ip_addr=h8_ip_addr, egress_port=4) writeIpv4Rules(p4info_helper, s3, dst_eth_addr="08:00:00:00:08:00", dst_ip_addr=h8_ip_addr, egress_port=4) writeIpv4Rules(p4info_helper, s4, dst_eth_addr="08:00:00:00:08:88", dst_ip_addr=h8_ip_addr, egress_port=2) writeIpv4Rules(p4info_helper, s5, dst_eth_addr="08:00:00:00:08:00", dst_ip_addr=h8_ip_addr, egress_port=4) writeIpv4Rules(p4info_helper, s6, dst_eth_addr="08:00:00:00:08:00", dst_ip_addr=h8_ip_addr, egress_port=4) readTableRules(p4info_helper, s1) readTableRules(p4info_helper, s2) readTableRules(p4info_helper, s3) readTableRules(p4info_helper, s4) readTableRules(p4info_helper, s5) readTableRules(p4info_helper, s6) 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') # 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" # Write table entries writeTableRules(p4info_helper, sw=s1, dst_ip_addr="10.0.1.1", mask=32, port=1) writeTableRules(p4info_helper, sw=s1, dst_ip_addr="10.0.1.2", mask=32, port=2) writeTableRules(p4info_helper, sw=s1, dst_ip_addr="10.0.1.3", mask=32, port=3) writeTableRules(p4info_helper, sw=s1, dst_ip_addr="10.0.1.4", mask=32, port=4) readTableRules(p4info_helper, s1) # 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) ''' while True: sleep(2) print '\n----- Reading direct counters -----' printDirectCounter(p4info_helper, s1, table_name="MyIngress.ipv4_lpm") 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') 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): p4info_helper = p4runtime_lib.helper.P4InfoHelper(p4info_file_path) port_map = {} arp_rules = {} flag = 0 bcast = "ff:ff:ff:ff:ff:ff" try: # connect to grpc server s1 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 MasterArbitrationUpdate to switch content = s1.MasterArbitrationUpdate() #SetForwardingPipleine using learn_switch.p4 s1.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForwardingPipelineConfig on s1" #write multicast group entry 1 to switch #Question: Is there any p4 runtime rpc for controller to know the port status of switch, so i can dynamically configure this entry? mc_group_entry = p4info_helper.buildMCEntry(mc_group_id=1, replicas={ 1: 1, 2: 2, 3: 3, 4: 4 }) s1.WritePRE(mc_group=mc_group_entry) print "Installed Multicast group on s1" #for use cases that host already knows destination MAC port_map = { '00:00:00:00:01:01': '\x00\x01', '00:00:00:00:01:02': '\x00\x02', '00:00:00:00:01:03': '\x00\x03', '00:00:00:00:01:04': '\x00\x04' } arp_rules = { '\x00\x01': [], '\x00\x02': [], '\x00\x03': [], '\x00\x04': [] } accumulation = 0 counter = 0 while True: #keep listening to Packet-in event sent from switch content = s1.ReadPacketIn() #begin = time.time() if content.WhichOneof('update') == 'packet': packet = content.packet.payload #hex_packet = binascii.hexlify(content.packet.payload) pkt = Ether(_pkt=packet) metadata = content.packet.metadata for meta in metadata: metadata_id = meta.metadata_id if metadata_id == 1: #ignore padding field value = meta.value #print "Metadata_id = %d, value = %d%d\n" %(metadata_id, ord(value[0]), ord(value[1])) #print "Raw packet data: %s\n" % hex_packet pkt_eth_src = pkt.getlayer(Ether).src pkt_eth_dst = pkt.getlayer(Ether).dst ether_type = pkt.getlayer(Ether).type if ether_type == 2048 or ether_type == 2054: #learn_switch is only capable of dealing with ip or arp packets #port_map.setdefault(pkt_eth_src, value) #arp_rules.setdefault(value, []) pkt.hide_defaults() pkt.show() ''' if pkt_eth_dst == bcast: #arp_request packet processing if bcast not in arp_rules: #controller need to record written rules to avoid grpc error writeARPFlood(p4info_helper, sw=s1, in_port=value, dst_mac=bcast) arp_rules[value].append(bcast) #packet out original packet packet_out = p4info_helper.buildPacketOut( payload = packet, metadata = { 1 : "\000\000", 2 : "\000\001" } ) s1.WritePacketOut(packet_out) else: #arp_reply/ipv4 packet processing if pkt_eth_dst not in arp_rules[value]: writeARPReply(p4info_helper, sw=s1, in_port=value, dst_mac=pkt_eth_dst, port=port_map[pkt_eth_dst]) arp_rules[value].append(pkt_eth_dst) if pkt_eth_src not in arp_rules[port_map[pkt_eth_dst]]: writeARPReply(p4info_helper, sw=s1, in_port=port_map[pkt_eth_dst], dst_mac=pkt_eth_src, port=port_map[pkt_eth_src]) arp_rules[port_map[pkt_eth_dst]].append(pkt_eth_src) #packet out original packet packet_out = p4info_helper.buildPacketOut( payload = packet, metadata = { 1: port_map[pkt_eth_dst], 2: "\000\000" } ) s1.WritePacketOut(packet_out) #print "port_map:%s\n" % port_map #print "arp_rules:%s\n" % arp_rules counter += 1 end = time.time() delta = end - begin accumulation = accumulation + delta avg = accumulation / counter print "Time delta for processing a packet-in message: %.9f" % (delta) print "avg time processing a packet-in message: %.9f\n" % (avg) ''' 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 # - then need to read from the file compile from P4 Program, which call .p4info p4info_helper = p4runtime_lib.helper.P4InfoHelper(p4info_file_path) try: """ Main lab """ 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') # for s3 s3 = p4runtime_lib.bmv2.Bmv2SwitchConnection( name="s3", address='127.0.0.1:50053', device_id=2, proto_dump_file='logs/s3-p4runtime-requests.txt') # MasterArbitrationUpdate process s1.MasterArbitrationUpdate() s2.MasterArbitrationUpdate() s3.MasterArbitrationUpdate() s1.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForardingPipelineConfig on s1" s2.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForardingPipelineConfig on s2" s3.SetForwardingPipelineConfig(p4info=p4info_helper.p4info, bmv2_json_file_path=bmv2_file_path) print "Installed P4 Program using SetForardingPipelineConfig on s3" # - s1 writeForwardRules(p4info_helper, ingress_sw=s1, dst_eth_addr="00:00:00:00:01:01", port=1, dst_ip_addr="10.0.1.1") writeForwardRules(p4info_helper, ingress_sw=s1, dst_eth_addr="00:00:00:02:02:00", port=2, dst_ip_addr="10.0.2.2") writeForwardRules(p4info_helper, ingress_sw=s1, dst_eth_addr="00:00:00:03:03:00", port=3, dst_ip_addr="10.0.3.3") # - s2 writeForwardRules(p4info_helper, ingress_sw=s2, dst_eth_addr="00:00:00:01:02:00", port=2, dst_ip_addr="10.0.1.1") writeForwardRules(p4info_helper, ingress_sw=s2, dst_eth_addr="00:00:00:00:02:02", port=1, dst_ip_addr="10.0.2.2") writeForwardRules(p4info_helper, ingress_sw=s2, dst_eth_addr="00:00:00:03:03:00", port=3, dst_ip_addr="10.0.3.3") # - s1 writeForwardRules(p4info_helper, ingress_sw=s3, dst_eth_addr="00:00:00:01:03:00", port=2, dst_ip_addr="10.0.1.1") writeForwardRules(p4info_helper, ingress_sw=s3, dst_eth_addr="00:00:00:02:03:00", port=3, dst_ip_addr="10.0.2.2") writeForwardRules(p4info_helper, ingress_sw=s3, dst_eth_addr="00:00:00:00:03:03", port=1, dst_ip_addr="10.0.3.3") readTableRules(p4info_helper, s1) readTableRules(p4info_helper, s2) readTableRules(p4info_helper, s3) except KeyboardInterrupt: # using ctrl + c to exit print "Shutting down." except grpc.RpcError as e: printGrpcError(e) # Then close all the connections ShutdownAllSwitchConnections()
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()