def _constructPacket(self): """ Build list of packets to be sent and expected """ for idx, pc_info in enumerate(self.ptf_pc_ports): udp_sport = random.randint(0, 65535) udp_dport = random.randint(0, 65535) src_port = self.ptf_pc_ports[pc_info][0] src_ip = self.ptf_pc_ports[pc_info][2] pkt = testutils.simple_udp_packet( eth_dst=self.arp_entry[self.dst_ip], eth_src=self.ptfadapter.dataplane.get_mac(0, src_port), ip_dst=self.dst_ip, ip_src=src_ip, ip_tos=self.dscp << 2, udp_sport=udp_sport, udp_dport=udp_dport, ip_ttl=64) self.pkts.append(pkt) tmp_pkt = testutils.simple_udp_packet( eth_dst=self.arp_entry[self.dst_ip], eth_src=self.ptfadapter.dataplane.get_mac(0, src_port), ip_dst=self.dst_ip, ip_src=src_ip, ip_tos=self.dscp << 2, udp_sport=udp_sport, udp_dport=udp_dport, ip_ttl=63) tmp_pkt = mask.Mask(tmp_pkt) tmp_pkt.set_do_not_care_scapy(packet.IP, "chksum") self.exp_pkts.append(tmp_pkt) self.pkt_map[pkt] = pc_info
def run_test_ipv4(ptfadapter, gather_facts): logger.info("Running test with ipv4 packets") dst_host_ipv4 = str( ip_address(unicode(gather_facts['dst_router_ipv4'])) + 1) pkt = testutils.simple_udp_packet(eth_dst=gather_facts['src_router_mac'], eth_src=gather_facts['src_host_mac'], ip_src=dst_host_ipv4, ip_dst=dst_host_ipv4, ip_ttl=DEFAULT_HLIM_TTL) logger.info("\nSend Packet:\neth_dst: {}, eth_src: {}, ipv6 ip: {}".format( gather_facts['src_router_mac'], gather_facts['src_host_mac'], dst_host_ipv4)) testutils.send(ptfadapter, int(gather_facts['src_port_ids'][0]), pkt) pkt = testutils.simple_udp_packet(eth_dst=gather_facts['dst_host_mac'], eth_src=gather_facts['dst_router_mac'], ip_src=dst_host_ipv4, ip_dst=dst_host_ipv4, ip_ttl=DEFAULT_HLIM_TTL - 1) logger.info( "\nExpect Packet:\neth_dst: {}, eth_src: {}, ipv6 ip: {}".format( gather_facts['dst_host_mac'], gather_facts['dst_router_mac'], dst_host_ipv4)) port_list = [int(port) for port in gather_facts['dst_port_ids']] testutils.verify_packet_any_port(ptfadapter, pkt, port_list, timeout=WAIT_EXPECTED_PACKET_TIMEOUT)
def run_test_ipv4(ptfadapter, facts): logger.info("Running test with ipv4 packets") pkt = testutils.simple_udp_packet(eth_dst=facts['src_router_mac'], eth_src=facts['src_host_mac'], ip_src=facts['dst_host_ipv4'], ip_dst=facts['dst_host_ipv4'], ip_ttl=DEFAULT_HLIM_TTL) logger.info("\nSend Packet:\neth_dst: {}, eth_src: {}, ipv4 ip: {}".format( facts['src_router_mac'], facts['src_host_mac'], facts['dst_host_ipv4'])) testutils.send(ptfadapter, facts['src_port_ids'][0], pkt) exp_pkt = testutils.simple_udp_packet(eth_dst=facts['dst_host_mac'], eth_src=facts['dst_router_mac'], ip_src=facts['dst_host_ipv4'], ip_dst=facts['dst_host_ipv4'], ip_ttl=DEFAULT_HLIM_TTL - 1) logger.info( "\nExpect Packet:\neth_dst: {}, eth_src: {}, ipv4 ip: {}".format( facts['dst_host_mac'], facts['dst_router_mac'], facts['dst_host_ipv4'])) testutils.verify_packet_any_port(ptfadapter, exp_pkt, facts['dst_port_ids'], timeout=WAIT_EXPECTED_PACKET_TIMEOUT)
def runTest(self): pkt1 = testutils.simple_udp_packet(eth_dst="00:11:11:11:11:11") pkt2 = testutils.simple_udp_packet(eth_dst="00:22:22:22:22:22") testutils.send_packet(self, (0, 1), pkt1) testutils.send_packet(self, (0, 1), pkt2) print("Packets sent") # pkt1 will be received on one of ports (0, 1) # pkt2 will be received on one of ports (1, 2, 3) testutils.verify_each_packet_on_multiple_port_lists(self, pkts=[pkt1, pkt2], ports=[[0, 1], [1, 2, 3]], device_number=1) # negative test with self.assertRaises(AssertionError): testutils.send_packet(self, (0, 1), pkt1) testutils.send_packet(self, (0, 1), pkt2) print("Packets sent") # pkt1 will not be received on one of ports (0, 2, 3) # pkt1 will not be received on one of ports (1, 2, 3); it will be pkt2 testutils.verify_each_packet_on_multiple_port_lists( self, pkts=[pkt1, pkt1], ports=[[0, 2, 3], [0, 1]], device_number=1)
def runTest(self): target = gc.Target(device_id=0, pipe_id=0xffff) # Get bfrt_info and set it as part of the test bfrt_info = self.interface.bfrt_info_get(p4_program_name) # Set default output port table_output_port = bfrt_info.table_get("SwitchIngress.output_port") action_data = table_output_port.make_data( action_name="SwitchIngress.set_output_port", data_field_list_in=[gc.DataTuple(name="port_id", val=swports[1])]) table_output_port.default_entry_set(target=target, data=action_data) # Set a default entry in the rewrite table so all packets get # their checksum updated. table_translate = bfrt_info.table_get("SwitchIngress.translate") action_data = table_translate.make_data( action_name="SwitchIngress.snupat", data_field_list_in=[ gc.DataTuple(name="src_addr", val=gc.ipv4_to_bytes("4.3.2.1")), gc.DataTuple(name="src_port", val=gc.to_bytes(0x4321, 2)), gc.DataTuple(name="update", val=0x0) ], ) table_translate.default_entry_set(target=target, data=action_data) try: ipkt = testutils.simple_udp_packet(eth_dst='11:11:11:11:11:11', eth_src='00:00:00:00:00:00', ip_src='1.2.3.4', ip_dst='100.99.98.97', ip_id=101, ip_ttl=64, udp_sport=0x1234, udp_dport=0xabcd, with_udp_chksum=True) epkt = testutils.simple_udp_packet(eth_dst='11:11:11:11:11:11', eth_src='00:00:00:00:00:00', ip_src='4.3.2.1', ip_dst='100.99.98.97', ip_id=101, ip_ttl=64, udp_sport=0x4321, udp_dport=0xabcd, with_udp_chksum=False) # We expect the packet to have the same TCP checksum as the packet # we sent in. Calling str forces scapy to calculate the checksum # for the given layer. epkt[scapy.all.IP].chksum = ipkt[scapy.all.IP].__class__( str(ipkt[scapy.all.IP])).chksum epkt[scapy.all.UDP].chksum = ipkt[scapy.all.UDP].__class__( str(ipkt[scapy.all.UDP])).chksum testutils.send_packet(self, swports[0], ipkt) testutils.verify_packet(self, epkt, swports[1]) finally: table_output_port.default_entry_reset(target) table_translate.default_entry_reset(target)
def _constructPacket(self): """ Build list of packets to be sent and expected """ for idx, intf in enumerate(self.ptf_ports): udp_sport = random.randint(0, 65535) udp_dport = random.randint(0, 65535) src_port = self.ptf_ports[intf][0] src_ip = self.ptf_ports[intf][2] vlan_id = self.ptf_ports[intf][3] pkt = testutils.simple_udp_packet(eth_dst=self.dut_mac, eth_src=self.ptfadapter.dataplane.get_mac(0, src_port), ip_dst=self.dst_ip, ip_src=src_ip, ip_tos=self.dscp << 2, udp_sport=udp_sport, udp_dport=udp_dport, ip_ttl=64 ) self.pkts.append(pkt) tmp_pkt = testutils.simple_udp_packet(eth_dst=self.arp_entry[self.dst_ip], eth_src=self.dut_mac, ip_dst=self.dst_ip, ip_src=src_ip, ip_tos=self.dscp << 2, udp_sport=udp_sport, udp_dport=udp_dport, ip_ttl=63 ) tmp_pkt = mask.Mask(tmp_pkt) tmp_pkt.set_do_not_care_scapy(packet.IP, "chksum") self.exp_pkts.append(tmp_pkt) # if inft is a sub interface, tuple be like ("Eth0.10", "Eth0") # if inft is a general interface, tuple be like ("Eth0", "Eth0") self.pkt_map[pkt] = (intf, get_intf_by_sub_intf(intf, vlan_id))
def runTest(self): pkt = "ab" * 20 pkt = pkt.encode() testutils.send_packet(self, (0, 1), pkt) print("packet sent") testutils.verify_any_packet_any_port(self, pkts=[pkt], ports=[3, 1], device_number=1) # negative test: if the packet is indeed received, but not on one of the # expected ports, the test should fail with self.assertRaises(AssertionError): testutils.send_packet(self, (0, 1), pkt) print("packet sent") testutils.verify_any_packet_any_port(self, pkts=[pkt], ports=[0, 2, 3], device_number=1) print("Verify masked packets") pkt1 = testutils.simple_udp_packet(eth_dst="00:11:11:11:11:11") pkt2 = testutils.simple_udp_packet(eth_dst="00:22:22:22:22:22") exp_pkt = Mask(pkt2) exp_pkt.set_do_not_care_packet(Ether, 'dst') testutils.send_packet(self, (0, 1), pkt1) print("Packet sent") # pkt2 will not be received # pkt2 with masked eth_dst field will match testutils.verify_any_packet_any_port(self, pkts=[pkt2, exp_pkt], ports=[0, 1], device_number=1) # negative tests with self.assertRaises(AssertionError): testutils.send_packet(self, (0, 1), pkt1) print("Packet sent") # incorrect ports testutils.verify_any_packet_any_port(self, pkts=[exp_pkt], ports=[0, 2, 3], device_number=1) with self.assertRaises(AssertionError): testutils.send_packet(self, (0, 1), pkt1) print("Packet sent") # incorrect packet testutils.verify_any_packet_any_port(self, pkts=[pkt2], ports=[0, 1], device_number=1)
def runTest(self): target = gc.Target(device_id=0, pipe_id=0xffff) # Get bfrt_info and set it as part of the test bfrt_info = self.interface.bfrt_info_get(p4_program_name) # Set default output port table_output_port = bfrt_info.table_get("SwitchIngress.output_port") action_data = table_output_port.make_data( action_name="SwitchIngress.set_output_port", data_field_list_in=[gc.DataTuple(name="port_id", val=swports[1])]) table_output_port.default_entry_set(target=target, data=action_data) # Set a default entry in the rewrite table so all packets get # their checksum updated. table_translate = bfrt_info.table_get("SwitchIngress.translate") action_data = table_translate.make_data( action_name="SwitchIngress.supat", data_field_list_in=[ gc.DataTuple(name="src_port", val=gc.to_bytes(0x4321, 2)), gc.DataTuple(name="update", val=0x1) ], ) table_translate.default_entry_set(target=target, data=action_data) try: ipkt = testutils.simple_udp_packet(eth_dst='11:11:11:11:11:11', eth_src='00:00:00:00:00:00', ip_src='1.2.3.4', ip_dst='100.99.98.97', ip_id=101, ip_ttl=64, udp_sport=0x1234, udp_dport=0xabcd, with_udp_chksum=True) epkt = testutils.simple_udp_packet(eth_dst='11:11:11:11:11:11', eth_src='00:00:00:00:00:00', ip_src='1.2.3.4', ip_dst='100.99.98.97', ip_id=101, ip_ttl=64, udp_sport=0x4321, udp_dport=0xabcd, with_udp_chksum=True) testutils.send_packet(self, swports[0], ipkt) testutils.verify_packet(self, epkt, swports[1]) finally: table_output_port.default_entry_reset(target) table_translate.default_entry_reset(target)
def int_udp_packet(pktlen=200, eth_dst='00:01:02:03:04:05', eth_src='00:06:07:08:09:0a', ip_dst='10.10.10.1', ip_src='192.168.0.1', udp_sport=101, udp_dport=4790, with_udp_chksum=False, int_remaining_hop_cnt=64, int_inst_mask=0x8000, int_metadata_stack=[]): pkt = testutils.simple_udp_packet(pktlen=0, eth_dst=eth_dst, eth_src=eth_src, ip_dst=ip_dst, ip_src=ip_src, ip_dscp=DSCP_INT, udp_sport=udp_sport, udp_dport=udp_dport, with_udp_chksum=with_udp_chksum) int_hdr = build_int_hdr(int_remaining_hop_cnt=int_remaining_hop_cnt, int_inst_mask=int_inst_mask, int_metadata_stack=int_metadata_stack) pkt /= int_hdr pkt /= ("".join([chr(x % 256) for x in xrange(pktlen - len(pkt))])) return pkt
def udp_packet(self, setup, direction, ptfadapter, ip_version, src_ip=None, dst_ip=None, sport=1234, dport=80): """Generate a UDP packet for testing.""" src_ip = src_ip or DEFAULT_SRC_IP[ip_version] dst_ip = dst_ip or self.get_dst_ip(direction, ip_version) dst_mac = setup[ "router_mac"] if direction == "uplink->downlink" else setup[ "vlan_mac"] if ip_version == "ipv4": return testutils.simple_udp_packet( eth_dst=dst_mac, eth_src=ptfadapter.dataplane.get_mac(0, 0), ip_dst=dst_ip, ip_src=src_ip, udp_sport=sport, udp_dport=dport, ip_ttl=64) else: return testutils.simple_udpv6_packet( eth_dst=dst_mac, eth_src=ptfadapter.dataplane.get_mac(0, 0), ipv6_dst=dst_ip, ipv6_src=src_ip, udp_sport=sport, udp_dport=dport, ipv6_hlim=64)
def create_packet(eth_dst, eth_src, ip_dst, ip_src, vlan_vid, tr_type, ttl, dl_vlan_enable=False, icmp_type=8, pktlen=100): """ Generate packet to send. Args: eth_dst: Destination Ethernet address eth_src: Source Ethernet address ip_dst: Destination IP address ip_src: Source IP address vlan_vid: VLAN ID tr_type: Type of traffic ttl: Time to live dl_vlan_enable: True if the packet is with vlan, False otherwise icmp_type: ICMP type pktlen: packet length Returns: simple packet """ if 'TCP' in tr_type: return testutils.simple_tcp_packet(eth_dst=eth_dst, eth_src=eth_src, ip_dst=ip_dst, ip_src=ip_src, tcp_sport=TCP_PORT, tcp_dport=TCP_PORT, vlan_vid=vlan_vid, dl_vlan_enable=dl_vlan_enable, ip_ttl=ttl, pktlen=pktlen) elif 'UDP' in tr_type: return testutils.simple_udp_packet(eth_dst=eth_dst, eth_src=eth_src, ip_dst=ip_dst, ip_src=ip_src, udp_sport=UDP_PORT, udp_dport=UDP_PORT, vlan_vid=vlan_vid, dl_vlan_enable=dl_vlan_enable, ip_ttl=ttl, pktlen=pktlen) elif 'ICMP' in tr_type: return testutils.simple_icmp_packet(eth_dst=eth_dst, eth_src=eth_src, ip_dst=ip_dst, ip_src=ip_src, icmp_type=icmp_type, vlan_vid=vlan_vid, dl_vlan_enable=dl_vlan_enable, ip_ttl=ttl, pktlen=pktlen) return None
def runTest(self): target = gc.Target(device_id=0, pipe_id=0xffff) # Get bfrt_info and set it as part of the test bfrt_info = self.interface.bfrt_info_get(p4_program_name) # Set default output port ipv4_match_regular = bfrt_info.table_get("SwitchIngress.ipv4_match_regular") action_data = ipv4_match_regular.make_data( action_name="SwitchIngress.set_output_port", data_field_list_in=[gc.DataTuple(name="port_id", val=swports[0])] ) ipv4_match_regular.default_entry_set( target=target, data=action_data) try: ipkt = testutils.simple_udp_packet(eth_dst='11:11:11:11:11:11', eth_src='22:22:22:22:22:22', ip_src='1.2.3.4', ip_dst='100.99.98.97', ip_id=101, ip_ttl=64, udp_sport=0x1234, udp_dport=0xabcd) testutils.send_packet(self, swports[0], ipkt) testutils.verify_packet(self, ipkt, swports[0]) finally: ipv4_match_regular.default_entry_reset(target)
def runTestIpv4(self): self.test.log("Run IPv4 based test") pkt = simple_udp_packet(eth_dst=self.srcRouterMac, eth_src=self.srcHostMac, ip_src=self.dstHostIpv4, ip_dst=self.dstHostIpv4, ip_ttl=self.pktTtlHlim) send(self.test, int(self.srcPortIds[0]), pkt) pkt = simple_udp_packet(eth_dst=self.dstHostMac, eth_src=self.dstRouterMac, ip_src=self.dstHostIpv4, ip_dst=self.dstHostIpv4, ip_ttl=self.pktTtlHlim-1) verify_packet_any_port(self.test, pkt, [int(port) for port in self.dstPortIds]) self.test.log("IPv4 based test: done")
def contruct_packet(self, port_number): src_mac = self.my_mac[port_number] dst_mac = self.peer_mac[port_number] dst_ip = self.peerip packet = testutils.simple_udp_packet(eth_dst=dst_mac, ip_dst=dst_ip, eth_src=src_mac, udp_dport=161) return packet
def udp_packet(self, setup, direction, ptfadapter): """Generate a UDP packet for testing.""" return testutils.simple_udp_packet( eth_dst=setup["router_mac"], eth_src=ptfadapter.dataplane.get_mac(0, 0), ip_dst=self.get_dst_ip(direction), ip_src=DEFAULT_SRC_IP, udp_sport=1234, udp_dport=80, ip_ttl=64 )
def udp_packet(self, setup, direction, ptfadapter): """ create UDP packet for testing """ return testutils.simple_udp_packet( eth_dst=setup['router_mac'], eth_src=ptfadapter.dataplane.get_mac(0, 0), ip_dst=self.get_dst_ip(setup, direction), ip_src='20.0.0.1', udp_sport=1234, udp_dport=80, ip_ttl=64, )
def runTest(self): pktlen = 1000 udp = testutils.simple_udp_packet() ipv6 = testutils.simple_ipv6ip_packet(inner_frame=udp['UDP']) gre = testutils.simple_grev6_packet(pktlen=pktlen, inner_frame=ipv6["IPv6"]) self.assertEqual(gre['GRE'].proto, 0x86DD) testutils.send_packet(self, (0, 1), gre) print("packet sent") testutils.verify_packet(self, gre, (1, 1)) testutils.verify_no_other_packets(self, 1)
def runTest(self): target = gc.Target(device_id=0, pipe_id=0xffff) # Get bfrt_info and set it as part of the test bfrt_info = self.interface.bfrt_info_get(p4_program_name) ipv4_match_regular = bfrt_info.table_get("SwitchIngress.ipv4_match_regular") ipv4_match_regular.info.key_field_annotation_add('hdr.ethernet.dst_addr', 'mac') ipv4_match_regular.info.key_field_annotation_add('hdr.ethernet.src_addr', 'mac') ipv4_match_regular.info.key_field_annotation_add('hdr.ipv4.dst_addr', 'ipv4') ipv4_match_regular.info.key_field_annotation_add('hdr.ipv4.src_addr', 'ipv4') key_data_regular = ipv4_match_regular.make_key([ gc.KeyTuple(name='hdr.ethernet.dst_addr', value='11:11:11:11:11:11'), gc.KeyTuple(name='hdr.ethernet.src_addr', value='22:22:22:22:22:22'), gc.KeyTuple(name='hdr.ipv4.dst_addr', value='100.99.98.97'), gc.KeyTuple(name='hdr.ipv4.src_addr', value='1.2.3.4'), ]) action_data_regular = ipv4_match_regular.make_data( [gc.DataTuple(name='port_id', val=swports[3])], 'SwitchIngress.set_output_port' ) ipv4_match_regular.entry_add( target, [key_data_regular], [action_data_regular]) try: ipkt = testutils.simple_udp_packet(eth_dst='11:11:11:11:11:11', eth_src='22:22:22:22:22:22', ip_src='1.2.3.4', ip_dst='100.99.98.97', ip_id=101, ip_ttl=64, udp_sport=0x1234, udp_dport=0xabcd) testutils.send_packet(self, swports[0], ipkt) testutils.verify_packet(self, ipkt, swports[3]) finally: ipv4_match_regular.default_entry_reset(target) ipv4_match_regular.entry_del(target, [key_data_regular])
def runTest(self): try: # expected mean and stdev from 32b unsigned uniform random exp_mean = (pow(2, 32) - 1) / 2.0 exp_std = (pow(2, 32) - 1) / math.sqrt(12) # compute mean and std from samples num_samples = 1000 rand_vals = [] print("\nInject %s packets and get random value in srcip field." % num_samples) print("It may take time with model.\n") for i in range(num_samples): ipkt = testutils.simple_udp_packet(eth_dst='11:11:11:11:11:11', eth_src='22:33:44:55:66:77', ip_src='1.2.3.4', ip_dst='100.99.98.97', ip_id=101, ip_ttl=64, udp_sport=0x1234, udp_dport=0xabcd) testutils.send_packet(self, swports[0], ipkt) (rcv_dev, rcv_port, rcv_pkt, pkt_time) = \ testutils.dp_poll(self, dev_id, swports[0], timeout=2) nrcv = ipkt.__class__(rcv_pkt) # print ("\n### Received pkt :\n") # nrcv.show2() # hexdump(nrcv) rand_val = ip2int(nrcv[IP].src) rand_vals.append(rand_val) # print("32b Random value written in ipv4 src ip : " + str(rand_val)) # compare mean and std mean = sum(rand_vals) / float(len(rand_vals)) std = stdev(rand_vals) print(("Expected Mean : " + str(exp_mean))) print(("Observed Mean : " + str(mean))) print(("Expected Stdev : " + str(exp_std))) print(("Observed Stdev : " + str(std))) assert abs(mean - exp_mean) / float(exp_mean) < 0.1 assert abs(std - exp_std) / float(exp_std) < 0.1 finally: pass
def contruct_packet(self, port_number): src_mac = self.my_mac[port_number] packet = testutils.simple_udp_packet(pktlen=100, eth_dst='33:33:00:01:00:02', eth_src=src_mac, dl_vlan_enable=False, vlan_vid=0, vlan_pcp=0, dl_vlan_cfi=0, ip_src='::1', ip_dst='ff02::1::2', ip_tos=0, ip_ttl=64, udp_sport=546, udp_dport=547, ip_ihl=None, ip_options=False, with_udp_chksum=True) return packet
def contruct_packet(self, port_number): src_mac = self.my_mac[port_number] packet = testutils.simple_udp_packet(pktlen=100, eth_dst='ff:ff:ff:ff:ff:ff', eth_src=src_mac, dl_vlan_enable=False, vlan_vid=0, vlan_pcp=0, dl_vlan_cfi=0, ip_src='0.0.0.0', ip_dst='255.255.255.255', ip_tos=0, ip_ttl=64, udp_sport=68, udp_dport=67, ip_ihl=None, ip_options=False, with_udp_chksum=True) return packet
def runTest(self): n3TEID = 0 startIP = IPv4Address('16.0.0.1') endIP = startIP + UE_COUNT - 1 accessIP = IPv4Address('10.128.13.29') enbIP = IPv4Address( '10.27.19.99') # arbitrary ip for nonexistent enodeB # program UPF for downlink traffic by installing PDRs and FARs print("Installing PDRs and FARs...") for i in range(UE_COUNT): # install N6 DL PDR to match UE dst IP pdrDown = self.createPDR( srcIface=CORE, dstIP=int(startIP + i), srcIfaceMask=0xFF, dstIPMask=0xFFFFFFFF, precedence=255, fseID=n3TEID + i + 1, # start from 1 ctrID=0, farID=i, qerIDList=[N6, 1], needDecap=0, ) self.addPDR(pdrDown) # install N6 DL FAR for encap farDown = self.createFAR( farID=i, fseID=n3TEID + i + 1, # start from 1 applyAction=ACTION_FORWARD, dstIntf=DST_ACCESS, tunnelType=0x1, tunnelIP4Src=int(accessIP), tunnelIP4Dst=int(enbIP), # only one eNB to send to downlink tunnelTEID=0, tunnelPort=GTPU_PORT, ) self.addFAR(farDown) # install N6 DL/UL application QER qer = self.createQER( gate=GATE_UNMETER, qerID=N6, fseID=n3TEID + i + 1, # start from 1 qfi=9, ulGbr=0, ulMbr=0, dlGbr=0, dlMbr=0, burstDurationMs=10, ) self.addApplicationQER(qer) # set up trex to send traffic thru UPF print("Setting up TRex client...") vm = STLVM() vm.var( name="dst", min_value=str(startIP), max_value=str(endIP), size=4, op="random", ) vm.write(fv_name="dst", pkt_offset="IP.dst") vm.fix_chksum() pkt = testutils.simple_udp_packet( pktlen=PKT_SIZE, eth_dst=UPF_DEST_MAC, with_udp_chksum=False, ) stream = STLStream( packet=STLPktBuilder(pkt=pkt, vm=vm), mode=STLTXCont(pps=RATE), flow_stats=STLFlowLatencyStats(pg_id=0), ) self.trex_client.add_streams(stream, ports=[BESS_SENDER_PORT]) print("Running traffic...") s_time = time.time() self.trex_client.start( ports=[BESS_SENDER_PORT], mult="1", duration=DURATION, ) self.trex_client.wait_on_traffic(ports=[BESS_SENDER_PORT]) print(f"Duration was {time.time() - s_time}") trex_stats = self.trex_client.get_stats() lat_stats = get_latency_stats(0, trex_stats) flow_stats = get_flow_stats(0, trex_stats) # Verify test results met baseline performance expectations # 0% packet loss self.assertEqual( flow_stats.tx_packets, flow_stats.rx_packets, f"Didn't receive all packets; sent {flow_stats.tx_packets}, received {flow_stats.rx_packets}", ) # 99.9th %ile latency < 1000 us self.assertLessEqual( lat_stats.percentile_99_9, 1000, f"99.9th %ile latency was higher than 1000 us! Was {lat_stats.percentile_99_9} us", ) # jitter < 20 us self.assertLessEqual( lat_stats.jitter, 20, f"Jitter was higher than 20 us! Was {lat_stats.jitter}", ) return
def create_packet(eth_dst, eth_src, ip_dst, ip_src, vlan_vid, tr_type, ttl, dl_vlan_enable=False, icmp_type=8, pktlen=100, ip_tunnel=None): """ Generate packet to send. Args: eth_dst: Destination Ethernet address eth_src: Source Ethernet address ip_dst: Destination IP address ip_src: Source IP address vlan_vid: VLAN ID tr_type: Type of traffic ttl: Time to live dl_vlan_enable: True if the packet is with vlan, False otherwise icmp_type: ICMP type pktlen: packet length ip_tunnel: Tunnel IP address of DUT Returns: simple packet """ if 'TCP' in tr_type: return testutils.simple_tcp_packet(eth_dst=eth_dst, eth_src=eth_src, ip_dst=ip_dst, ip_src=ip_src, tcp_sport=TCP_PORT, tcp_dport=TCP_PORT, vlan_vid=vlan_vid, dl_vlan_enable=dl_vlan_enable, ip_ttl=ttl, pktlen=pktlen) elif 'UDP' in tr_type: return testutils.simple_udp_packet(eth_dst=eth_dst, eth_src=eth_src, ip_dst=ip_dst, ip_src=ip_src, udp_sport=UDP_PORT, udp_dport=UDP_PORT, vlan_vid=vlan_vid, dl_vlan_enable=dl_vlan_enable, ip_ttl=ttl, pktlen=pktlen) elif 'ICMP' in tr_type: return testutils.simple_icmp_packet(eth_dst=eth_dst, eth_src=eth_src, ip_dst=ip_dst, ip_src=ip_src, icmp_type=icmp_type, vlan_vid=vlan_vid, dl_vlan_enable=dl_vlan_enable, ip_ttl=ttl, pktlen=pktlen) elif 'decap' in tr_type: inner_dscp = random.choice(range(0, 33)) inner_ttl = random.choice(range(3, 65)) inner_packet = testutils.simple_tcp_packet( ip_dst=ip_dst, ip_src=ip_src, tcp_sport=TCP_PORT, tcp_dport=TCP_PORT, ip_ttl=inner_ttl, ip_dscp=inner_dscp)[packet.IP] return testutils.simple_ipv4ip_packet(eth_dst=eth_dst, eth_src=eth_src, ip_src='1.1.1.1', ip_dst=ip_tunnel, ip_dscp=inner_dscp, ip_ttl=64, vlan_vid=vlan_vid, dl_vlan_enable=dl_vlan_enable, inner_frame=inner_packet) return None
def test_BUM_in_l2_vni(self, ptfhost, duthost, ptfadapter, pkt_dst_mac, pkt_dst_ip, neighbor_size, access_ports_type, evpn_env): neighbor_list = evpn_neighbor_list[: 1] if neighbor_size == "one_neighbor" else evpn_neighbor_list index_of_port_vxlan = neighbor_list[0][0] vtep_ip = str(neighbor_list[0][1].ip) # send packet from local logging.info("BUM encap: local to remote; BUM decap: local to local") # the port index represented remote vtep in ptf, which will receive vxlan packet received_port_list = [item[0] for item in neighbor_list] # the remainder ports which in the same vlan port_index_list = [ index for index in range(1, 25) if index not in received_port_list ] access_port_list = [self.INDEX_OF_PORT_SEND] if access_ports_type == "normal_port" \ else self.pch_param.member_index_list # remove the port which sends the packet for port in access_port_list: port_index_list.remove(port) # add ports which will receive the untagged packet received_port_list.extend(port_index_list) pkt_untagged = testutils.simple_udp_packet( eth_dst=pkt_dst_mac, eth_src="00:11:22:33:55:66", ip_dst=pkt_dst_ip, ip_src="192.168.100.1", ) # the packet that received in remote expected_packet_list = [] for item in neighbor_list: ptf_vtep_index = item[0] # mac for vxlan tunnel dut_mac = evpn_env.dut_helper.get_index_mac(ptf_vtep_index) ptf_mac = evpn_env.ptf_helper.get_index_mac(ptf_vtep_index) ip = str(item[1].ip) pkt_expected_vxlan = evpn_env.pkt_helper.compose_expected_vxlan_packet( outer_sa=dut_mac, outer_da=ptf_mac, outer_sip=DUT_VTEP_IP, outer_dip=ip, vni=self.vni, pkt=pkt_untagged, GPE_flag=True) expected_packet_list.append(pkt_expected_vxlan) # the packets received in local expected_packet_list.extend( [pkt_untagged] * (len(received_port_list) - len(neighbor_list))) # check packet [remote pkt, local pkt, local pkt, ...] in [port 1, port 2, port 3, ...] and not received in other ports. for port in access_port_list: logging.info( "expected received from port {}".format(received_port_list)) ptfadapter.dataplane.flush() testutils.send(ptfadapter, port, pkt_untagged) testutils.verify_each_packet_on_each_port(ptfadapter, expected_packet_list, received_port_list) # send packet from remote logging.info("BUM decap: remote to local; BUM encap: remote to remote") pkt_vxlan, pkt_untagged = evpn_env.pkt_helper.create_vxlan_packet( outer_da=dut_mac, outer_sa=ptf_mac, outer_dip=DUT_VTEP_IP, outer_sip=vtep_ip, vni=self.vni, inner_da=pkt_dst_mac, inner_dip=pkt_dst_ip) vtep_index_list = [item[0] for item in neighbor_list] # for untagged packet received_port_list = [ index for index in range(1, 25) if index not in vtep_index_list ] expected_packet_list = [pkt_untagged] * len(received_port_list) # check packet is received in all vlan member if access_ports_type == "normal_port": logging.info( "expected received from {}".format(received_port_list)) ptfadapter.dataplane.flush() testutils.send(ptfadapter, index_of_port_vxlan, pkt_vxlan) testutils.verify_each_packet_on_each_port(ptfadapter, expected_packet_list, received_port_list) elif access_ports_type == "with_portchannel": # pkt_num = 20 packet_count = 0 ptfadapter.dataplane.flush() for j in range(0, NUM_CONTINUOUS_PKT_COUNT): pkt_vxlan['UDP'].sport = pkt_vxlan['UDP'].sport + 1 testutils.send(ptfadapter, index_of_port_vxlan, pkt_vxlan) index, _ = evpn_env.pkt_helper.verify_packet_count( pkt_untagged, self.pch_param.member_index_list[0]) packet_count = packet_count + index assert packet_count != 0
def test_unicast_in_l2_vni(self, duthost, ptfadapter, neighbor_size, access_ports_type, evpn_env): neighbor_list = evpn_neighbor_list[: 1] if neighbor_size == "one_neighbor" else evpn_neighbor_list # for packet from local to remote dst_mac_1 = "00:11:22:33:33:33" dst_ip_1 = "192.168.1.10" # for packet from remote to local dst_mac_2 = "00:11:22:33:33:44" dst_ip_2 = "192.168.1.20" access_port_list = [self.INDEX_OF_PORT_SEND] if access_ports_type == "normal_port" \ else self.pch_param.member_index_list # local -> remote # for item in neighbor_list: ptf_vtep_index = item.if_index ptf_vtep_ip = str(item.ip_ptf.ip) ptf_vtep_as = item.as_number_ptf gobgp_port = item.gobgp_port dut_mac = evpn_env.dut_helper.get_index_mac(ptf_vtep_index) ptf_mac = evpn_env.ptf_helper.get_index_mac(ptf_vtep_index) logging.info( "encap test: local to remote, index:{}, gobgp port:{}".format( ptf_vtep_index, gobgp_port)) # let dut learn mac route from remote evpn_env.gobgp_helper.add_type2(dst_mac_1, dst_ip_1, None, as_ptf=ptf_vtep_as, vni=self.vni, vtep_ip=ptf_vtep_ip, gobgp_port=gobgp_port) # tagged packet is for sending pkt_untagged = testutils.simple_udp_packet( eth_dst=dst_mac_1, eth_src="00:11:22:33:55:66", ip_dst=dst_ip_1, ip_src="192.168.100.1", ) # expected packet is for receiving pkt_expected = evpn_env.pkt_helper.compose_expected_vxlan_packet( outer_sa=dut_mac, outer_da=ptf_mac, outer_sip=DUT_VTEP_IP, outer_dip=ptf_vtep_ip, vni=self.vni, pkt=pkt_untagged, GPE_flag=False) for port in access_port_list: ptfadapter.dataplane.flush() testutils.send(ptfadapter, port, pkt_untagged) testutils.verify_packets(ptfadapter, pkt_expected, [ptf_vtep_index]) # recover evpn_env.gobgp_helper.del_type2(dst_mac_1, dst_ip_1, None, as_ptf=ptf_vtep_as, vni=self.vni, vtep_ip=ptf_vtep_ip, gobgp_port=gobgp_port) # remote -> local # # let dut learn mac route from local pkt = testutils.simple_arp_packet( eth_src=dst_mac_2, vlan_vid=self.VLAN_ID, arp_op=1, ip_snd=dst_ip_2, ip_tgt=self.VLAN_IP, hw_snd=dst_mac_2, ) # vxlan packet is for sending for item in neighbor_list: ptf_vtep_index = item.if_index ptf_vtep_ip = str(item.ip_ptf.ip) ptf_vtep_as = item.as_number_ptf gobgp_port = item.gobgp_port dut_mac = evpn_env.dut_helper.get_index_mac(ptf_vtep_index) ptf_mac = evpn_env.ptf_helper.get_index_mac(ptf_vtep_index) pkt_vxlan, pkt_expected = evpn_env.pkt_helper.create_vxlan_packet( outer_da=dut_mac, outer_sa=ptf_mac, outer_dip=DUT_VTEP_IP, outer_sip=ptf_vtep_ip, vni=self.vni, inner_sa="00:11:22:33:55:66", inner_sip="192.168.0.44", inner_da=dst_mac_2, inner_dip=dst_ip_2) # port move from local to remote. mac="00:11:22:33:55:66" because it is the src mac of the packet sent in local->remote logging.info( "decap test: remote to local, index:{}, gobgp port:{}".format( ptf_vtep_index, gobgp_port)) for send_port in access_port_list: testutils.send(ptfadapter, send_port, pkt) evpn_env.gobgp_helper.add_type2("00:11:22:33:55:66", "192.168.0.44", None, as_ptf=ptf_vtep_as, vni=self.vni, vtep_ip=ptf_vtep_ip, gobgp_port=gobgp_port) try: evpn_env.pkt_helper.verify_decap_receive_packet( ptf_vtep_index, access_port_list, pkt_vxlan, pkt_expected) finally: evpn_env.gobgp_helper.del_type2("00:11:22:33:55:66", "192.168.0.44", None, as_ptf=ptf_vtep_as, vni=self.vni, vtep_ip=ptf_vtep_ip, gobgp_port=gobgp_port)
def runTest(self): target = gc.Target(device_id=0, pipe_id=0xffff) # Get bfrt_info and set it as part of the test bfrt_info = self.interface.bfrt_info_get(p4_program_name) # Set default output port table_output_port = bfrt_info.table_get("SwitchIngress.output_port") action_data = table_output_port.make_data( action_name="SwitchIngress.set_output_port", data_field_list_in=[gc.DataTuple(name="port_id", val=swports[1])] ) table_output_port.default_entry_set( target=target, data=action_data) try: random.seed(1) num_trials = 20 for i in range(num_trials): ip_src = int2ip(random.randint(0, 2 ** 32 - 1)) ip_dst = int2ip(random.randint(0, 2 ** 32 - 1)) udp_sport = random.randint(0, 2 ** 16 - 1) udp_dport = random.randint(0, 2 ** 16 - 1) print((" Testing IPv4 and UDP port values: {}, {}, {}, {}". format(ip_src, ip_dst, udp_sport, udp_dport))) ipkt_1 = testutils.simple_udp_packet(eth_dst="22:22:22:22:22:22", eth_src='00:00:00:00:00:00', ip_src=ip_src, ip_dst=ip_dst, ip_id=101, ip_ttl=64, udp_sport=udp_sport, udp_dport=udp_dport) testutils.send_packet(self, swports[0], ipkt_1) (rcv_dev, rcv_port, rcv_pkt, pkt_time) = \ testutils.dp_poll(self, dev_id, swports[1], timeout=2) rpkt_1 = ipkt_1.__class__(rcv_pkt) ipkt_2 = testutils.simple_udp_packet(eth_dst="22:22:22:22:22:22", eth_src='00:00:00:00:00:00', ip_src=ip_dst, ip_dst=ip_src, ip_id=101, ip_ttl=64, udp_sport=udp_dport, udp_dport=udp_sport) testutils.send_packet(self, swports[0], ipkt_2) (rcv_dev, rcv_port, rcv_pkt, pkt_time) = \ testutils.dp_poll(self, dev_id, swports[1], timeout=2) rpkt_2 = ipkt_2.__class__(rcv_pkt) ipkt_3 = testutils.simple_udp_packet(eth_dst="22:22:22:22:22:22", eth_src='00:00:00:00:00:00', ip_src=ip_src, ip_dst=ip_dst, ip_id=101, ip_ttl=64, udp_sport=udp_dport, udp_dport=udp_sport) testutils.send_packet(self, swports[0], ipkt_3) (rcv_dev, rcv_port, rcv_pkt, pkt_time) = \ testutils.dp_poll(self, dev_id, swports[1], timeout=2) rpkt_3 = ipkt_3.__class__(rcv_pkt) ipkt_4 = testutils.simple_udp_packet(eth_dst="22:22:22:22:22:22", eth_src='00:00:00:00:00:00', ip_src=ip_src, ip_dst=ip_dst, ip_id=101, ip_ttl=64, udp_sport=udp_dport, udp_dport=udp_sport) testutils.send_packet(self, swports[0], ipkt_4) (rcv_dev, rcv_port, rcv_pkt, pkt_time) = \ testutils.dp_poll(self, dev_id, swports[1], timeout=2) rpkt_4 = ipkt_4.__class__(rcv_pkt) assert rpkt_1[Ether].src != "00:00:00:00:00:00" assert rpkt_1[Ether].src == rpkt_2[Ether].src == rpkt_3[Ether].src == rpkt_4[Ether].src finally: table_output_port.default_entry_reset(target)
def runTest(self): n3TEID = 0 startIP = IPv4Address('16.0.0.1') endIP = startIP + UE_COUNT - 1 accessIP = IPv4Address('10.128.13.29') enbIP = IPv4Address( '10.27.19.99' ) # arbitrary ip for non-existent eNodeB for gtpu encap # program UPF for downlink traffic by installing PDRs and FARs print("Installing PDRs and FARs...") for i in range(UE_COUNT): # install N6 DL PDR to match UE dst IP pdrDown = self.createPDR( srcIface=CORE, dstIP=int(startIP + i), srcIfaceMask=0xFF, dstIPMask=0xFFFFFFFF, precedence=255, fseID=n3TEID + i + 1, # start from 1 ctrID=0, farID=i, qerIDList=[N6, 1], needDecap=0, ) self.addPDR(pdrDown) # install N6 DL FAR for encap farDown = self.createFAR( farID=i, fseID=n3TEID + i + 1, # start from 1 applyAction=ACTION_FORWARD, dstIntf=DST_ACCESS, tunnelType=0x1, tunnelIP4Src=int(accessIP), tunnelIP4Dst=int(enbIP), # only one eNB to send to downlink tunnelTEID=0, tunnelPort=GTPU_PORT, ) self.addFAR(farDown) # install N6 DL/UL application QER qer = self.createQER( gate=GATE_UNMETER, qerID=N6, fseID=n3TEID + i + 1, # start from 1 qfi=9, ulGbr=0, ulMbr=0, dlGbr=0, dlMbr=0, burstDurationMs=10, ) self.addApplicationQER(qer) # set up trex to send traffic thru UPF print("Setting up TRex client...") vm = STLVM() vm.var( name="dst", min_value=str(startIP), max_value=str(endIP), size=4, op="random", ) vm.write(fv_name="dst", pkt_offset="IP.dst") vm.fix_chksum() pkt = testutils.simple_udp_packet( pktlen=PKT_SIZE, eth_dst=UPF_DEST_MAC, with_udp_chksum=False, ) stream = STLStream( packet=STLPktBuilder(pkt=pkt, vm=vm), mode=STLTXCont(pps=RATE), ) self.trex_client.add_streams(stream, ports=[BESS_SENDER_PORT]) print("Running traffic...") s_time = time.time() self.trex_client.start(ports=[BESS_SENDER_PORT], mult="1", duration=DURATION) # FIXME: pull QoS metrics at end instead of while traffic running time.sleep(DURATION - 5) if self.trex_client.is_traffic_active(): stats = self.getSessionStats(q=[90, 99, 99.9], quiet=True) preQos = stats["preQos"] postDlQos = stats["postDlQos"] postUlQos = stats["postUlQos"] self.trex_client.wait_on_traffic(ports=[BESS_SENDER_PORT]) print(f"Duration was {time.time() - s_time}") trex_stats = self.trex_client.get_stats() sent_packets = trex_stats['total']['opackets'] recv_packets = trex_stats['total']['ipackets'] # 0% packet loss self.assertEqual( sent_packets, recv_packets, f"Didn't receive all packets; sent {sent_packets}, received {recv_packets}", ) for fseid in postDlQos: lat = fseid['latency']['percentileValuesNs'] jitter = fseid['jitter']['percentileValuesNs'] # 99th %ile latency < 100 us self.assertLessEqual( int(lat[1]) / 1000, 100, f"99th %ile latency was higher than 100 us! Was {int(lat[1]) / 1000} us" ) # 99.9th %ile latency < 200 us self.assertLessEqual( int(lat[2]) / 1000, 200, f"99.9th %ile latency was higher than 200 us! Was {int(lat[2]) / 1000} us" ) # 99th% jitter < 100 us self.assertLessEqual( int(jitter[1]) / 1000, 100, f"99th %ile jitter was higher than 100 us! Was {int(jitter[1]) / 1000} us" ) return
def runTest(self): target = gc.Target(device_id=0, pipe_id=0xffff) # Get bfrt_info and set it as part of the test bfrt_info = self.interface.bfrt_info_get(p4_program_name) # Set default output port table_output_port = bfrt_info.table_get("SwitchIngress.output_port") action_data = table_output_port.make_data( action_name="SwitchIngress.set_output_port", data_field_list_in=[gc.DataTuple(name="port_id", val=swports[1])]) table_output_port.default_entry_set(target=target, data=action_data) try: ipkt_payload = struct.pack("I", 0) * 10 ipkt = testutils.simple_udp_packet(eth_dst='11:11:11:11:11:11', eth_src='22:22:22:22:22:22', ip_src='1.2.3.4', ip_dst='100.99.98.97', ip_id=101, ip_ttl=64, udp_sport=0x1234, udp_dport=0xabcd, with_udp_chksum=False, udp_payload=ipkt_payload) testutils.send_packet(self, swports[0], ipkt) (rcv_dev, rcv_port, rcv_pkt, pkt_time) = \ testutils.dp_poll(self, 0, swports[1], timeout=2) nrcv = ipkt.__class__(rcv_pkt) # Parse the payload and extract the timestamps # import pdb; pdb.set_trace() ts_ingress_mac, ts_ingress_global, \ ts_enqueue, ts_dequeue_delta, \ ts_egress_global, ts_egress_tx = \ struct.unpack("!QQIIQQxxxxxxxxxxxxxxxxxx", nrcv.load) ns = 1000000000.0 logger.info("Timestamps") logger.info(" raw values in ns:") logger.info(" ingress mac : {:>15}".format( ts_ingress_mac)) logger.info(" ingress global : {:>15}".format( ts_ingress_global)) logger.info(" traffic manager enqueue : {:>15}".format( ts_enqueue)) logger.info(" traffic manager dequeue delta : {:>15}".format( ts_dequeue_delta)) logger.info(" egress global : {:>15}".format( ts_egress_global)) logger.info(" egress tx (no value in model) : {:>15}".format( ts_egress_tx)) logger.info(" values in s:") logger.info(" ingress mac : {:>15.9f}".format( ts_ingress_mac / ns)) logger.info(" ingress global : {:>15.9f}".format( ts_ingress_global / ns)) logger.info(" traffic manager enqueue : {:>15.9f}".format( ts_enqueue / ns)) logger.info(" traffic manager dequeue delta : {:>15.9f}".format( ts_dequeue_delta / ns)) logger.info(" egress global : {:>15.9f}".format( ts_egress_global / ns)) logger.info(" egress tx (no value in model) : {:>15.9f}".format( ts_egress_tx)) logger.info( "Please note that the timestamps are using the internal time " + "of the model/chip. They are not synchronized with the global time. " "Furthermore, the traffic manager timestamps in the model do not " + "accurately reflect the packet processing. Correct values are shown " + "by the hardware implementation.") finally: table_output_port.default_entry_reset(target)
def runTest(self): pass_cnt = 0 tos = self.dscp << 2 tos_bg = self.dscp_bg << 2 if self.debug: # remove previous debug files files = glob.glob("/tmp/pfc_pause_{}*".format(self.dscp)) for file in files: os.remove(file) current_time = datetime.datetime.now().strftime( "%Y-%m-%d_%H:%M:%S") log_file = open( "/tmp/pfc_pause_{}_{}".format(self.dscp, current_time), "w") """ If DUT needs to learn MAC addresses """ if not self.dut_has_mac: pkt = simple_udp_packet(eth_dst=self.mac_dst, eth_src=self.mac_src, ip_src=self.ip_src, ip_dst=self.ip_dst) send_packet(self, self.port_src, pkt, 5) pkt = simple_udp_packet(eth_dst=self.mac_src, eth_src=self.mac_dst, ip_src=self.ip_dst, ip_dst=self.ip_src) send_packet(self, self.port_dst, pkt, 5) for x in range(self.pkt_count): sport = random.randint(0, 65535) dport = random.randint(0, 65535) pkt = simple_udp_packet(eth_dst=self.mac_dst, eth_src=self.mac_src, ip_src=self.ip_src, ip_dst=self.ip_dst, ip_tos=tos, udp_sport=sport, udp_dport=dport, ip_ttl=64) pkt_bg = simple_udp_packet(eth_dst=self.mac_dst, eth_src=self.mac_src, ip_src=self.ip_src, ip_dst=self.ip_dst, ip_tos=tos_bg, udp_sport=sport, udp_dport=dport, ip_ttl=64) exp_pkt = simple_udp_packet(ip_src=self.ip_src, ip_dst=self.ip_dst, ip_tos=tos_bg, udp_sport=sport, udp_dport=dport, ip_ttl=63) masked_exp_pkt = Mask(exp_pkt) masked_exp_pkt.set_do_not_care_scapy(scapy.Ether, "src") masked_exp_pkt.set_do_not_care_scapy(scapy.Ether, "dst") masked_exp_pkt.set_do_not_care_scapy(scapy.IP, "ttl") masked_exp_pkt.set_do_not_care_scapy(scapy.IP, "chksum") masked_exp_pkt.set_do_not_care_scapy(scapy.IP, "tos") send_packet(self, self.port_src, pkt, 1) send_packet(self, self.port_src, pkt_bg, 1) pkts = capture_matched_packets(self, masked_exp_pkt, self.port_dst) if self.debug: for i, pkt in enumerate(pkts): dump_msg = "Iteration {}:\n Pkt num {}:\n Hex dump: {}\n\n".format( x, i, sc.utils.hexstr(pkt)) log_file.write(dump_msg) time.sleep(self.pkt_intvl) """ If the queue is paused, we should only receive the background packet """ if self.queue_paused: pass_cnt += int( len(pkts) == 1 and scapy.Ether(pkts[0])[scapy.IP].tos == tos_bg) else: pass_cnt += int(len(pkts) == 2) if self.debug: log_file.close() print "Passes: %d / %d" % (pass_cnt, self.pkt_count)
def runTest(self): target = gc.Target(device_id=0, pipe_id=0xffff) # Get bfrt_info and set it as part of the test bfrt_info = self.interface.bfrt_info_get(p4_program_name) # Set default output port table_output_port = bfrt_info.table_get("SwitchIngress.output_port") action_data = table_output_port.make_data( action_name="SwitchIngress.set_output_port", data_field_list_in=[gc.DataTuple(name="port_id", val=swports[1])]) table_output_port.default_entry_set(target=target, data=action_data) # Set a default entry in the rewrite table so all packets get # their checksum updated. table_translate = bfrt_info.table_get("SwitchIngress.translate") action_data = table_translate.make_data( action_name="SwitchIngress.snupat", data_field_list_in=[ gc.DataTuple(name="src_addr", val=gc.ipv4_to_bytes("4.3.2.1")), gc.DataTuple(name="src_port", val=gc.to_bytes(0x4321, 2)), gc.DataTuple(name="update", val=0x1) ], ) table_translate.default_entry_set(target=target, data=action_data) try: # Test special cases # The UDP checksum value 0x0000 denotes the absence of the optional # checksum. Therefore, if the actual checksum value is 0x0000, the # checksum value is flipped to be 0xffff. # Find payload value for UDP checksum 0xffff before translation # Uncomment if to find new payload value when the input values have # changed. # for i in range(2**16): # tpkt = testutils.simple_udp_packet(eth_dst='11:11:11:11:11:11', # eth_src='00:00:00:00:00:00', # ip_src='1.2.3.4', # ip_dst='100.99.98.97', # ip_id=101, # ip_ttl=64, # udp_sport=0x1234, # udp_dport=0xabcd, # udp_payload = struct.pack("!I", i), # with_udp_chksum = True) # # Force checksum calculation # checksum = tpkt[scapy.all.UDP].__class__(str(tpkt[scapy.all.UDP])).chksum # print("Packet: {}, payload: {}, checksum: {}".format(hash(tpkt), # hex(i), # hex(checksum))) # if hex(checksum) == '0xffff': # print("Found payload with hash value 0xffff: {}".format(i)) # break # Payload for UDP checksum 0xffff: 46530, actual UDP csum value: 0x0 # Find payload value for UDP checksum 0xffff after translation # Uncomment if to find new payload value when the input values have # changed. # for i in range(2**16): # tpkt = testutils.simple_udp_packet(eth_dst='11:11:11:11:11:11', # eth_src='00:00:00:00:00:00', # ip_src='4.3.2.1', # ip_dst='100.99.98.97', # ip_id=101, # ip_ttl=64, # udp_sport=0x4321, # udp_dport=0xabcd, # udp_payload = struct.pack("!I", i), # with_udp_chksum = True) # # Force checksum calculation # checksum = tpkt[scapy.all.UDP].__class__(str(tpkt[scapy.all.UDP])).chksum # print("Packet: {}, payload: {}, checksum: {}".format(hash(tpkt), # hex(i), # hex(checksum))) # if hex(checksum) == '0xffff': # print("Found payload with hash value 0xffff: {}".format(i)) # break # Payload for UDP checksum 0xffff: 33495, actual UDP csum value: 0x0 # Send packet with checksum 0xffff, should be updated ipkt = testutils.simple_udp_packet(eth_dst='11:11:11:11:11:11', eth_src='00:00:00:00:00:00', ip_src='1.2.3.4', ip_dst='100.99.98.97', ip_id=101, ip_ttl=64, udp_sport=0x1234, udp_dport=0xabcd, udp_payload=struct.pack( "!I", 46530), with_udp_chksum=True) epkt = testutils.simple_udp_packet(eth_dst='11:11:11:11:11:11', eth_src='00:00:00:00:00:00', ip_src='4.3.2.1', ip_dst='100.99.98.97', ip_id=101, ip_ttl=64, udp_sport=0x4321, udp_dport=0xabcd, udp_payload=struct.pack( "!I", 46530), with_udp_chksum=True) testutils.send_packet(self, swports[0], ipkt) testutils.verify_packet(self, epkt, swports[1]) # Send packet with checksum, that should be updated to be 0x0000, # and therefore requires the P4 program to flips the bits to be 0xffff ipkt = testutils.simple_udp_packet(eth_dst='11:11:11:11:11:11', eth_src='00:00:00:00:00:00', ip_src='1.2.3.4', ip_dst='100.99.98.97', ip_id=101, ip_ttl=64, udp_sport=0x1234, udp_dport=0xabcd, udp_payload=struct.pack( "!I", 33495), with_udp_chksum=True) epkt = testutils.simple_udp_packet(eth_dst='11:11:11:11:11:11', eth_src='00:00:00:00:00:00', ip_src='4.3.2.1', ip_dst='100.99.98.97', ip_id=101, ip_ttl=64, udp_sport=0x4321, udp_dport=0xabcd, udp_payload=struct.pack( "!I", 33495), with_udp_chksum=True) testutils.send_packet(self, swports[0], ipkt) testutils.verify_packet(self, epkt, swports[1]) finally: table_output_port.default_entry_reset(target) table_translate.default_entry_reset(target)