def generate_hashed_packet_to_server(ptfadapter, duthost, hash_key, target_server_ip): """ Generate a packet to server based on hash. The value of field in packet is filled with random value according to hash_key """ src_mac = ptfadapter.dataplane.get_mac(0, 0) ip_dst = target_server_ip SRC_IP_RANGE = [unicode('1.0.0.0'), unicode('200.255.255.255')] ip_src = random_ip( SRC_IP_RANGE[0], SRC_IP_RANGE[1]) if 'src-ip' in hash_key else SRC_IP_RANGE[0] sport = random.randint(1, 65535) if 'src-port' in hash_key else 1234 dport = random.randint(1, 65535) if 'dst-port' in hash_key else 80 dst_mac = duthost.facts["router_mac"] send_pkt = testutils.simple_tcp_packet(pktlen=128, eth_dst=dst_mac, eth_src=src_mac, dl_vlan_enable=False, vlan_vid=0, vlan_pcp=0, ip_src=ip_src, ip_dst=ip_dst, tcp_sport=sport, tcp_dport=dport, ip_ttl=64) exp_pkt = mask.Mask(send_pkt) exp_pkt.set_do_not_care_scapy(scapyall.Ether, 'dst') exp_pkt.set_do_not_care_scapy(scapyall.Ether, "src") exp_pkt.set_do_not_care_scapy(scapyall.IP, "ttl") exp_pkt.set_do_not_care_scapy(scapyall.IP, "chksum") inner_packet = send_pkt[IP] inner_packet.ttl = inner_packet.ttl - 1 exp_tunnel_pkt = testutils.simple_ipv4ip_packet(eth_dst=dst_mac, eth_src=src_mac, ip_src="10.1.0.32", ip_dst="10.1.0.33", inner_frame=inner_packet) send_pkt.ttl = 64 exp_tunnel_pkt[TCP] = inner_packet[TCP] exp_tunnel_pkt = mask.Mask(exp_tunnel_pkt) exp_tunnel_pkt.set_do_not_care_scapy(scapyall.Ether, "dst") exp_tunnel_pkt.set_do_not_care_scapy(scapyall.Ether, "src") exp_tunnel_pkt.set_do_not_care_scapy( scapyall.IP, "id") # since src and dst changed, ID would change too exp_tunnel_pkt.set_do_not_care_scapy( scapyall.IP, "ttl") # ttl in outer packet is set to 255 exp_tunnel_pkt.set_do_not_care_scapy( scapyall.IP, "chksum") # checksum would differ as the IP header is not the same return send_pkt, exp_pkt, exp_tunnel_pkt
def __init__(self, ptfadapter, exp_pkt, dst_port_number, match_fields=None, ignore_fields=None): """ Initialize an object for finding packets in the buffer Args: ptfadapter: PTF adapter exp_pkt: Expected packet dst_port_number: Destination port number match_fields: List of packet fields that should be matched ignore_fields: List of packet fields that should be ignored """ self.received_pkt = None self.received_pkt_diff = [] self.ptfadapter = ptfadapter self.pkt = exp_pkt self.dst_port_number = dst_port_number if match_fields is None: match_fields = [] self.match_fields = match_fields if ignore_fields is None: ignore_fields = [] self.ignore_fields = ignore_fields self.masked_exp_pkt = mask.Mask(self.pkt) self.pkt_dict = convert_pkt_to_dict(self.pkt) self.__ignore_fields()
def generate_and_verify_traffic(duthost, ptfadapter, ip_dst, expected_ports, ipv6=False): if ipv6: pkt = testutils.simple_tcpv6_packet( eth_dst=duthost.facts["router_mac"], eth_src=ptfadapter.dataplane.get_mac(0, 0), ipv6_src='2001:db8:85a3::8a2e:370:7334', ipv6_dst=ip_dst, ipv6_hlim=64, tcp_sport=1234, tcp_dport=4321) else: pkt = testutils.simple_tcp_packet( eth_dst=duthost.facts["router_mac"], eth_src=ptfadapter.dataplane.get_mac(0, 0), ip_src='1.1.1.1', ip_dst=ip_dst, ip_ttl=64, tcp_sport=1234, tcp_dport=4321) exp_pkt = pkt.copy() exp_pkt = mask.Mask(exp_pkt) exp_pkt.set_do_not_care_scapy(packet.Ether, 'dst') exp_pkt.set_do_not_care_scapy(packet.Ether, 'src') if ipv6: exp_pkt.set_do_not_care_scapy(packet.IPv6, 'hlim') exp_pkt.set_do_not_care_scapy(packet.IPv6, 'chksum') else: exp_pkt.set_do_not_care_scapy(packet.IP, 'ttl') exp_pkt.set_do_not_care_scapy(packet.IP, 'chksum') testutils.send(ptfadapter, 5, pkt) testutils.verify_packet_any_port(ptfadapter, exp_pkt, ports=expected_ports)
def expected_mask_ip_packet(self, pkt): """ return mask for ip packet """ epkt = pkt.copy() exp_pkt = pkt.copy() exp_pkt['IP'].ttl = 62 pkt1 = exp_pkt['IP'] exp_pkt['Ethernet'].type = 0x0800 exp_pkt['Ethernet'].remove_payload() exp_pkt /= pkt1 exp_pkt = mask.Mask(exp_pkt) exp_pkt = mask.Mask(exp_pkt) exp_pkt.set_do_not_care_scapy(packet.Ether, 'dst') exp_pkt.set_do_not_care_scapy(packet.Ether, 'src') exp_pkt.set_do_not_care_scapy(packet.IP, 'chksum') return exp_pkt
def generate_and_verify_traffic(duthost1, duthost2, ptfadapter, ptfhost, src_port, dst_ip, router_mac, get_routes, collect, down_link_on_dut=None, pkt_action=ACTION_FORWARD): """ Generate traffic, send and verify it Args: duthost1: DUT host object duthost2: DUT host object ptfadapter: PTF adapter ptfhost: PTF host object src_port: Source port from which pkt will be sent dst_ip: Destination ip address get_routes: Dict with routes for each DUT collect: Fixture which collects main info about link connection down_link_on_dut: Name of DUT on which link is down pkt_action: Action to verify, forward or drop """ router1_mac = duthost1.facts["router_mac"] router2_mac = duthost2.facts["router_mac"] dst_ports = get_dst_port(duthost1, duthost2, get_routes, dst_ip, collect) src_port = get_port_number(ptfhost, src_port) pkt = craft_pkt(ptfadapter, router_mac, src_port, dst_ip) expected_src_mac = router1_mac if dst_ports == collect[ duthost1.hostname]['vm_link_on_ptf'] else router2_mac exp_pkt = pkt.copy() if down_link_on_dut: exp_ttl = predict_exp_ttl(duthost1, duthost2, dst_ip, down_link_on_dut) exp_pkt[packet.IP].ttl = exp_ttl exp_pkt[packet.Ether].src = unicode(expected_src_mac) exp_pkt = mask.Mask(exp_pkt) exp_pkt.set_do_not_care_scapy(packet.Ether, "dst") exp_pkt.set_do_not_care_scapy(packet.IP, "id") exp_pkt.set_do_not_care_scapy(packet.IP, "chksum") exp_pkt.set_do_not_care_scapy(packet.TCP, "chksum") if not down_link_on_dut: exp_pkt.set_do_not_care_scapy(packet.IP, "ttl") ptfadapter.dataplane.flush() time.sleep(2) logger.info( "Sending pkt from port {} to dst_ip = {}, expected dst_port = {}". format(src_port, dst_ip, dst_ports)) logger.info(pkt.sprintf("%Ether.src% %IP.src% -> %Ether.dst% %IP.dst%")) testutils.send(ptfadapter, src_port, pkt) if pkt_action == ACTION_FORWARD: testutils.verify_packet(ptfadapter, exp_pkt, dst_ports) elif pkt_action == ACTION_DROP: testutils.verify_no_packet(ptfadapter, exp_pkt, dst_ports)
def create_exp_pkt(pkt, ttl): exp_pkt = pkt.copy() exp_pkt[scapy.IP].ttl = ttl exp_pkt = mask.Mask(exp_pkt, ignore_extra_bytes=True) exp_pkt.set_do_not_care_scapy(packet.Ether, "dst") exp_pkt.set_do_not_care_scapy(packet.Ether, "src") return exp_pkt
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 compose_expected_vxlan_packet(self, outer_da, outer_sa, outer_dip, outer_sip, vni, pkt, GPE_flag=False): exp_pkt = testutils.simple_vxlan_packet( eth_dst=outer_da, eth_src=outer_sa, ip_dst=outer_dip, ip_src=outer_sip, vxlan_vni=vni, inner_frame=pkt, ) if GPE_flag: exp_pkt["Ethernet"]["IP"]["UDP"]["VXLAN"].flags = 0x0a exp_pkt = mask.Mask(exp_pkt) exp_pkt.set_do_not_care_scapy(scapy.UDP, 'sport') exp_pkt.set_do_not_care_scapy(scapy.UDP, 'chksum') exp_pkt.set_do_not_care_scapy(scapy.IP, 'ttl') exp_pkt.set_do_not_care_scapy(scapy.IP, 'chksum') exp_pkt.set_do_not_care_scapy(scapy.IP, 'id') return exp_pkt
def _build_tunnel_packet(outer_src_ip, outer_dst_ip, inner_packet=None): """Build the expected tunnel packet.""" if inner_packet is None: exp_pkt = testutils.simple_ip_packet( ip_src=outer_src_ip, ip_dst=outer_dst_ip, pktlen=20 ) else: exp_pkt = testutils.simple_ipv4ip_packet( ip_src=outer_src_ip, ip_dst=outer_dst_ip, inner_frame=inner_packet ) exp_pkt = mask.Mask(exp_pkt) exp_pkt.set_do_not_care_scapy(Ether, "dst") exp_pkt.set_do_not_care_scapy(Ether, "src") exp_pkt.set_do_not_care_scapy(IP, "ihl") exp_pkt.set_do_not_care_scapy(IP, "tos") exp_pkt.set_do_not_care_scapy(IP, "len") exp_pkt.set_do_not_care_scapy(IP, "id") exp_pkt.set_do_not_care_scapy(IP, "flags") exp_pkt.set_do_not_care_scapy(IP, "frag") exp_pkt.set_do_not_care_scapy(IP, "ttl") exp_pkt.set_do_not_care_scapy(IP, "proto") exp_pkt.set_do_not_care_scapy(IP, "chksum") if inner_packet is None: exp_pkt.set_ignore_extra_bytes() return exp_pkt
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 generate_and_verify_traffic(duthost, ptfadapter, src_port, dst_port, ip_src, ip_dst, pkt_action): """ Send ICMP request packet from PTF to DUT and verify that DUT sends/doesn't send ICMP reply packet to PTF. Args: duthost: DUT host object ptfadapter: PTF adapter src_port: Port of PTF dst_port: Port of DUT ip_src: Source IP address of PTF ip_dst: Destination IP address of DUT pkt_action: Packet action (forwarded or drop) """ router_mac = get_mac_dut(duthost, dst_port) src_port_number = int(get_port_number(src_port)) src_mac = ptfadapter.dataplane.get_mac(0, src_port_number) # Get VLAN ID from name of sub-port vlan_vid = int(src_port.split('.')[1]) ip_src = ip_src.split('/')[0] ip_dst = ip_dst.split('/')[0] # Create ICMP request packet pkt = create_packet(eth_dst=router_mac, eth_src=src_mac, ip_src=ip_src, ip_dst=ip_dst, vlan_vid=vlan_vid, dl_vlan_enable=True) # Define ICMP reply packet exp_pkt = create_packet(eth_src=router_mac, eth_dst=src_mac, ip_src=ip_dst, ip_dst=ip_src, vlan_vid=vlan_vid, dl_vlan_enable=True, icmp_type=0) masked_exp_pkt = mask.Mask(exp_pkt) masked_exp_pkt.set_do_not_care_scapy(packet.IP, "id") masked_exp_pkt.set_do_not_care_scapy(packet.IP, "chksum") masked_exp_pkt.set_do_not_care_scapy(packet.IP, "ttl") masked_exp_pkt.set_do_not_care_scapy(packet.ICMP, "chksum") ptfadapter.dataplane.flush() testutils.send_packet(ptfadapter, src_port_number, pkt) dst_port_list = [src_port_number] if pkt_action == ACTION_FWD: testutils.verify_packet_any_port(ptfadapter, masked_exp_pkt, dst_port_list) elif pkt_action == ACTION_DROP: testutils.verify_no_packet_any(ptfadapter, masked_exp_pkt, dst_port_list)
def expected_mask_routed_packet(self, pkt): """Generate the expected mask for a routed packet.""" exp_pkt = pkt.copy() exp_pkt["IP"].ttl -= 1 exp_pkt = mask.Mask(exp_pkt) exp_pkt.set_do_not_care_scapy(packet.Ether, "dst") exp_pkt.set_do_not_care_scapy(packet.Ether, "src") exp_pkt.set_do_not_care_scapy(packet.IP, "chksum") return exp_pkt
def expected_packet_mask(pkt): """ Return mask for sniffing packet """ exp_pkt = pkt.copy() exp_pkt = mask.Mask(exp_pkt) exp_pkt.set_do_not_care_scapy(packet.Ether, 'dst') exp_pkt.set_do_not_care_scapy(packet.Ether, 'src') exp_pkt.set_do_not_care_scapy(packet.IP, 'ttl') exp_pkt.set_do_not_care_scapy(packet.IP, 'chksum') return exp_pkt
def expected_mask_routed_packet(self, pkt): """ return mask for routed packet """ exp_pkt = pkt.copy() exp_pkt['IP'].ttl -= 1 exp_pkt = mask.Mask(exp_pkt) exp_pkt.set_do_not_care_scapy(packet.Ether, 'dst') exp_pkt.set_do_not_care_scapy(packet.Ether, 'src') exp_pkt.set_do_not_care_scapy(packet.IP, 'chksum') return exp_pkt
def expected_mask_mpls_swap_packet(self, pkt, exp_label): """ return mask for mpls swap packet """ exp_pkt = pkt.copy() exp_pkt['MPLS'].ttl -= 1 exp_pkt['MPLS'].label = exp_label exp_pkt = mask.Mask(exp_pkt) exp_pkt.set_do_not_care_scapy(packet.Ether, 'dst') exp_pkt.set_do_not_care_scapy(packet.Ether, 'src') exp_pkt.set_do_not_care_scapy(packet.IP, 'chksum') return exp_pkt
def build_expected_packet_to_server(encapsulated_packet): """Build packet expected to be received by server from the tunnel packet.""" inner_packet = encapsulated_packet[IP].payload[IP].copy() # use dummy mac address that will be ignored in mask inner_packet = Ether(src="aa:bb:cc:dd:ee:ff", dst="aa:bb:cc:dd:ee:ff") / inner_packet exp_pkt = mask.Mask(inner_packet) exp_pkt.set_do_not_care_scapy(Ether, "dst") exp_pkt.set_do_not_care_scapy(Ether, "src") exp_pkt.set_do_not_care_scapy(IP, "tos") exp_pkt.set_do_not_care_scapy(IP, "ttl") exp_pkt.set_do_not_care_scapy(IP, "chksum") return exp_pkt
def packets_for_test(request, ptfadapter, duthost, config_facts, tbinfo, ip_and_intf_info): ip_version = request.param src_addr_v4, src_addr_v6, ptf_intf = ip_and_intf_info ptf_intf_index = int(ptf_intf.replace('eth', '')) ptf_intf_mac = ptfadapter.dataplane.get_mac(0, ptf_intf_index) vlans = config_facts['VLAN'] topology = tbinfo['topo']['name'] dut_mac = '' for vlan_details in vlans.values(): if 'dualtor' in topology: dut_mac = vlan_details['mac'].lower() else: dut_mac = duthost.shell( 'sonic-cfggen -d -v \'DEVICE_METADATA.localhost.mac\'' )["stdout_lines"][0].decode("utf-8") break if ip_version == 'v4': tgt_addr = increment_ipv4_addr(src_addr_v4) out_pkt = testutils.simple_arp_packet(eth_dst='ff:ff:ff:ff:ff:ff', eth_src=ptf_intf_mac, ip_snd=src_addr_v4, ip_tgt=tgt_addr, arp_op=1, hw_snd=ptf_intf_mac) exp_pkt = testutils.simple_arp_packet(eth_dst=ptf_intf_mac, eth_src=dut_mac, ip_snd=tgt_addr, ip_tgt=src_addr_v4, arp_op=2, hw_snd=dut_mac, hw_tgt=ptf_intf_mac) elif ip_version == 'v6': tgt_addr = increment_ipv6_addr(src_addr_v6) ll_src_addr = generate_link_local_addr(ptf_intf_mac) multicast_tgt_addr = in6_getnsma(inet_pton(socket.AF_INET6, tgt_addr)) multicast_tgt_mac = in6_getnsmac(multicast_tgt_addr) out_pkt = Ether(src=ptf_intf_mac, dst=multicast_tgt_mac) out_pkt /= IPv6(dst=inet_ntop(socket.AF_INET6, multicast_tgt_addr), src=ll_src_addr) out_pkt /= ICMPv6ND_NS(tgt=tgt_addr) out_pkt /= ICMPv6NDOptSrcLLAddr(lladdr=ptf_intf_mac) exp_pkt = Ether(src=dut_mac, dst=ptf_intf_mac) exp_pkt /= IPv6(dst=ll_src_addr, src=generate_link_local_addr(dut_mac)) exp_pkt /= ICMPv6ND_NA(tgt=tgt_addr, S=1, R=1, O=0) exp_pkt /= ICMPv6NDOptSrcLLAddr(type=2, lladdr=dut_mac) exp_pkt = mask.Mask(exp_pkt) exp_pkt.set_do_not_care_scapy(packet.IPv6, 'fl') return ip_version, out_pkt, exp_pkt
def test_forward_ip_packet_recomputed_0xffff_chksum(self, duthost, ptfadapter, common_param): # GIVEN a ip packet, after forwarded(ttl-1) by DUT, # it's checksum will be 0xffff after wrongly incrementally recomputed # ref to https://datatracker.ietf.org/doc/html/rfc1624 # HC' = HC(0xff00) + m(0x7a2f) + ~m'(~0x792f)= 0xffff # WHEN send the packet to DUT # THEN DUT recompute new checksum correctly and forward packet as expected. (peer_ip_ifaces_pair, ptf_port_idx, pc_ports_map, ptf_indices) = common_param pkt = testutils.simple_ip_packet( eth_dst=duthost.facts["router_mac"], eth_src=ptfadapter.dataplane.get_mac(0, ptf_port_idx), pktlen=1246, ip_src="10.250.40.40", ip_dst="10.156.190.188", ip_proto=47, ip_tos=0x84, ip_id=0, ip_ihl=5, ip_ttl=122, ) pkt.payload.flags = 2 exp_pkt = pkt.copy() exp_pkt.payload.ttl = 121 exp_pkt.payload.chksum = 0x0001 exp_pkt = mask.Mask(exp_pkt) exp_pkt.set_do_not_care_scapy(packet.Ether, 'dst') exp_pkt.set_do_not_care_scapy(packet.Ether, 'src') out_ifaces = TestIPPacket.parse_interfaces(duthost.command("show ip route 10.156.190.188")["stdout_lines"], pc_ports_map) out_ptf_indices = map(lambda iface: ptf_indices[iface], out_ifaces) duthost.command("portstat -c") ptfadapter.dataplane.flush() testutils.send(ptfadapter, ptf_port_idx, pkt, self.PKT_NUM) time.sleep(5) match_cnt = testutils.count_matched_packets_all_ports(ptfadapter, exp_pkt, ports=out_ptf_indices) portstat_out = parse_portstat(duthost.command("portstat")["stdout_lines"]) rx_ok = int(portstat_out[peer_ip_ifaces_pair[0][1][0]]["rx_ok"].replace(",", "")) rx_drp = int(portstat_out[peer_ip_ifaces_pair[0][1][0]]["rx_drp"].replace(",", "")) tx_ok = TestIPPacket.sum_portstat_ifaces_counts(portstat_out, out_ifaces, "tx_ok") tx_drp = TestIPPacket.sum_portstat_ifaces_counts(portstat_out, out_ifaces, "tx_drp") pytest_assert(match_cnt == self.PKT_NUM, "Packet lost") pytest_assert(self.PKT_NUM_MIN <= rx_ok <= self.PKT_NUM_MAX, "rx_ok unexpected") pytest_assert(self.PKT_NUM_MIN <= tx_ok <= self.PKT_NUM_MAX, "tx_ok unexpected") pytest_assert(rx_drp <= self.PKT_NUM_ZERO, "rx_drp unexpected") pytest_assert(tx_drp <= self.PKT_NUM_ZERO, "tx_drp unexpected")
def generate_hashed_packet_to_server(ptfadapter, duthost, hash_key, target_server_ip): """ Generate a packet to server based on hash. The value of field in packet is filled with random value according to hash_key """ src_mac = ptfadapter.dataplane.get_mac(0, 0) ip_dst = target_server_ip SRC_IP_RANGE = [unicode('1.0.0.0'), unicode('200.255.255.255')] ip_src = random_ip( SRC_IP_RANGE[0], SRC_IP_RANGE[1]) if 'src-ip' in hash_key else SRC_IP_RANGE[0] sport = random.randint(1, 65535) if 'src-port' in hash_key else 1234 dport = random.randint(1, 65535) if 'dst-port' in hash_key else 80 dst_mac = duthost.facts["router_mac"] pkt = testutils.simple_tcp_packet(pktlen=128, eth_dst=dst_mac, eth_src=src_mac, dl_vlan_enable=False, vlan_vid=0, vlan_pcp=0, ip_src=ip_src, ip_dst=ip_dst, tcp_sport=sport, tcp_dport=dport, ip_ttl=64) exp_pkt = mask.Mask(pkt) exp_pkt.set_do_not_care_scapy(scapyall.Ether, 'dst') exp_pkt.set_do_not_care_scapy(scapyall.Ether, "src") exp_pkt.set_do_not_care_scapy(scapyall.IP, "ihl") exp_pkt.set_do_not_care_scapy(scapyall.IP, "tos") exp_pkt.set_do_not_care_scapy(scapyall.IP, "len") exp_pkt.set_do_not_care_scapy(scapyall.IP, "id") exp_pkt.set_do_not_care_scapy(scapyall.IP, "flags") exp_pkt.set_do_not_care_scapy(scapyall.IP, "frag") exp_pkt.set_do_not_care_scapy(scapyall.IP, "ttl") exp_pkt.set_do_not_care_scapy(scapyall.IP, "proto") exp_pkt.set_do_not_care_scapy(scapyall.IP, "chksum") exp_pkt.set_do_not_care_scapy(scapyall.TCP, "sport") exp_pkt.set_do_not_care_scapy(scapyall.TCP, "seq") exp_pkt.set_do_not_care_scapy(scapyall.TCP, "ack") exp_pkt.set_do_not_care_scapy(scapyall.TCP, "reserved") exp_pkt.set_do_not_care_scapy(scapyall.TCP, "dataofs") exp_pkt.set_do_not_care_scapy(scapyall.TCP, "window") exp_pkt.set_do_not_care_scapy(scapyall.TCP, "chksum") exp_pkt.set_do_not_care_scapy(scapyall.TCP, "urgptr") return pkt, exp_pkt
def test_forward_ip_packet_with_0xffff_chksum_drop(self, duthost, ptfadapter, common_param): # GIVEN a ip packet with checksum 0x0000(compute from scratch) # WHEN manually set checksum as 0xffff and send the packet to DUT # THEN DUT should drop packet with 0xffff and add drop count (peer_ip_ifaces_pair, ptf_port_idx, pc_ports_map, ptf_indices) = common_param pkt = testutils.simple_ip_packet( eth_dst=duthost.facts["router_mac"], eth_src=ptfadapter.dataplane.get_mac(0, ptf_port_idx), pktlen=1246, ip_src="10.250.136.195", ip_dst="10.156.94.34", ip_proto=47, ip_tos=0x84, ip_id=0, ip_ihl=5, ip_ttl=121, ) pkt.payload.flags = 2 pkt.payload.chksum = 0xffff exp_pkt = pkt.copy() exp_pkt.payload.ttl = 120 exp_pkt.payload.chksum = 0x0100 exp_pkt = mask.Mask(exp_pkt) exp_pkt.set_do_not_care_scapy(packet.Ether, 'dst') exp_pkt.set_do_not_care_scapy(packet.Ether, 'src') out_ifaces = TestIPPacket.parse_interfaces(duthost.command("show ip route 10.156.94.34")["stdout_lines"], pc_ports_map) out_ptf_indices = map(lambda iface: ptf_indices[iface], out_ifaces) duthost.command("portstat -c") ptfadapter.dataplane.flush() testutils.send(ptfadapter, ptf_port_idx, pkt, self.PKT_NUM) time.sleep(5) match_cnt = testutils.count_matched_packets_all_ports(ptfadapter, exp_pkt, ports=out_ptf_indices) portstat_out = parse_portstat(duthost.command("portstat")["stdout_lines"]) rx_ok = int(portstat_out[peer_ip_ifaces_pair[0][1][0]]["rx_ok"].replace(",", "")) rx_drp = int(portstat_out[peer_ip_ifaces_pair[0][1][0]]["rx_drp"].replace(",", "")) tx_ok = TestIPPacket.sum_portstat_ifaces_counts(portstat_out, out_ifaces, "tx_ok") tx_drp = TestIPPacket.sum_portstat_ifaces_counts(portstat_out, out_ifaces, "tx_drp") pytest_assert(match_cnt == 0, "Packet not dropped") pytest_assert(self.PKT_NUM_MIN <= rx_ok <= self.PKT_NUM_MAX, "rx_ok unexpected") pytest_assert(self.PKT_NUM_MIN <= rx_drp <= self.PKT_NUM_MAX, "rx_drp unexpected") pytest_assert(tx_drp <= self.PKT_NUM_ZERO, "tx_drp unexpected") pytest_assert(tx_ok <= self.PKT_NUM_ZERO, "tx_ok unexpected")
def expected_mask_routed_packet(self, pkt, ip_version): """Generate the expected mask for a routed packet.""" exp_pkt = pkt.copy() exp_pkt = mask.Mask(exp_pkt) exp_pkt.set_do_not_care_scapy(packet.Ether, "dst") exp_pkt.set_do_not_care_scapy(packet.Ether, "src") if ip_version == "ipv4": exp_pkt.set_do_not_care_scapy(packet.IP, "chksum") # In multi-asic we cannot determine this so ignore. exp_pkt.set_do_not_care_scapy(packet.IP, 'ttl') else: # In multi-asic we cannot determine this so ignore. exp_pkt.set_do_not_care_scapy(packet.IPv6, 'hlim') return exp_pkt
def expected_mask_routed_packet(self, pkt, ip_version): """Generate the expected mask for a routed packet.""" exp_pkt = pkt.copy() if ip_version == "ipv4": exp_pkt["IP"].ttl -= 1 else: exp_pkt["IPv6"].hlim -= 1 exp_pkt = mask.Mask(exp_pkt) exp_pkt.set_do_not_care_scapy(packet.Ether, "dst") exp_pkt.set_do_not_care_scapy(packet.Ether, "src") if ip_version == "ipv4": exp_pkt.set_do_not_care_scapy(packet.IP, "chksum") return exp_pkt
def build_packet_to_server(duthost, ptfadapter, target_server_ip): """Build packet and expected mask packet destinated to server.""" pkt_dscp = random.choice(range(0, 33)) pkt_ttl = random.choice(range(3, 65)) pkt = testutils.simple_ip_packet(eth_dst=duthost.facts["router_mac"], eth_src=ptfadapter.dataplane.get_mac( 0, 0), ip_src="1.1.1.1", ip_dst=target_server_ip, ip_dscp=pkt_dscp, ip_ttl=pkt_ttl) logging.info("the packet destinated to server %s:\n%s", target_server_ip, dump_scapy_packet_show_output(pkt)) exp_pkt = mask.Mask(pkt) exp_pkt.set_do_not_care_scapy(scapyall.Ether, "dst") exp_pkt.set_do_not_care_scapy(scapyall.Ether, "src") exp_pkt.set_do_not_care_scapy(scapyall.IP, "tos") exp_pkt.set_do_not_care_scapy(scapyall.IP, "ttl") exp_pkt.set_do_not_care_scapy(scapyall.IP, "chksum") return pkt, exp_pkt
def test_forward_normal_ip_packet(self, duthost, ptfadapter, common_param): # GIVEN a random normal ip packet # WHEN send the packet to DUT # THEN DUT should forward it as normal ip packet, nothing change but ttl-1 (peer_ip_ifaces_pair, ptf_port_idx, pc_ports_map, ptf_indices) = common_param pkt = testutils.simple_ip_packet( eth_dst=duthost.facts["router_mac"], eth_src=ptfadapter.dataplane.get_mac(0, ptf_port_idx), ip_src=peer_ip_ifaces_pair[0][0], ip_dst=peer_ip_ifaces_pair[1][0]) exp_pkt = pkt.copy() exp_pkt.payload.ttl = pkt.payload.ttl - 1 exp_pkt = mask.Mask(exp_pkt) exp_pkt.set_do_not_care_scapy(packet.Ether, 'dst') exp_pkt.set_do_not_care_scapy(packet.Ether, 'src') out_ifaces = TestIPPacket.parse_interfaces( duthost.command("show ip route %s" % peer_ip_ifaces_pair[1][0])["stdout_lines"], pc_ports_map) out_ptf_indices = map(lambda iface: ptf_indices[iface], out_ifaces) duthost.command("portstat -c") ptfadapter.dataplane.flush() testutils.send(ptfadapter, ptf_port_idx, pkt, self.PKT_NUM) time.sleep(5) match_cnt = testutils.count_matched_packets_all_ports(ptfadapter, exp_pkt, ports=out_ptf_indices) portstat_out = parse_portstat(duthost.command("portstat")["stdout_lines"]) rx_ok = int(portstat_out[peer_ip_ifaces_pair[0][1][0]]["rx_ok"].replace(",", "")) rx_drp = int(portstat_out[peer_ip_ifaces_pair[0][1][0]]["rx_drp"].replace(",", "")) tx_ok = TestIPPacket.sum_portstat_ifaces_counts(portstat_out, out_ifaces, "tx_ok") tx_drp = TestIPPacket.sum_portstat_ifaces_counts(portstat_out, out_ifaces, "tx_drp") pytest_assert(match_cnt == self.PKT_NUM, "Packet lost") pytest_assert(self.PKT_NUM_MIN <= rx_ok <= self.PKT_NUM_MAX, "rx_ok unexpected") pytest_assert(self.PKT_NUM_MIN <= tx_ok <= self.PKT_NUM_MAX, "tx_ok unexpected") pytest_assert(rx_drp <= self.PKT_NUM_ZERO, "rx_drp unexpected") pytest_assert(tx_drp <= self.PKT_NUM_ZERO, "tx_drp unexpected")
def runTest(self): stf_parser = STFParser() # the test will have the same name as the file stf_file = FILE_DIR.joinpath(f"{FILE_NAME}.stf") parsed_stf, _ = stf_parser.parse(filename=stf_file) input_pkts = [] expect_pkts = [] for entry in parsed_stf: stf_class = entry[0] stf_port = entry[1] stf_bytes = entry[2] if stf_class == "packet": input_pkts.append((stf_port, stf_bytes)) if stf_class == "expect": expect_pkts.append((stf_port, stf_bytes)) for input_pkt in input_pkts: input_port = input_pkt[0] input_bytes = bytes.fromhex(input_pkt[1]) testutils.send_packet(self, input_port, input_bytes) try: for expect_pkt in expect_pkts: expect_port = expect_pkt[0] expect_bytes = list(expect_pkt[1]) dont_care_bits = [] for idx, hexbit in enumerate(expect_bytes): if hexbit == "*": dont_care_bits.append(idx * 4) expect_bytes[idx] = "0" expect_bytes = "".join(expect_bytes) expect_bytes = bytes.fromhex(expect_bytes) pkt = mask.Mask(expect_bytes) pkt.set_ignore_extra_bytes() for dont_care_bit in dont_care_bits: pkt.set_do_not_care(dont_care_bit, 4) testutils.verify_packet(self, pkt, expect_port) except AssertionError as e: with open(f"{FILE_NAME}_ptf_err.log", 'w+') as err_file: err_file.write(str(e)) err_file.flush() raise AssertionError("Test failed, see error above")
def test_po_update_io_no_loss(duthosts, enum_rand_one_per_hwsku_frontend_hostname, enum_frontend_asic_index, tbinfo, reload_testbed, ptfadapter): # GIVEN a lag topology, keep sending packets between 2 port channels # WHEN delete/add different members of a port channel # THEN no packets shall loss duthost = duthosts[enum_rand_one_per_hwsku_frontend_hostname] asichost = duthost.asic_instance(enum_frontend_asic_index) mg_facts = duthost.get_extended_minigraph_facts(tbinfo) if len(mg_facts["minigraph_portchannel_interfaces"]) < 2: pytest.skip( "Skip test due to there isn't enough port channel exists in current topology." ) # generate ip-pc pairs, be like:[("10.0.0.56", "10.0.0.57", "PortChannel0001")] peer_ip_pc_pair = [(pc["addr"], pc["peer_addr"], pc["attachto"]) for pc in mg_facts["minigraph_portchannel_interfaces"] if ipaddress.ip_address(pc['peer_addr']).version == 4] # generate pc tuples, fill in members, # be like:[("10.0.0.56", "10.0.0.57", "PortChannel0001", ["Ethernet48", "Ethernet52"])] pcs = [(pair[0], pair[1], pair[2], mg_facts["minigraph_portchannels"][pair[2]]["members"]) for pair in peer_ip_pc_pair if len(mg_facts["minigraph_portchannels"][pair[2]]["members"]) >= 2] if len(pcs) < 2: pytest.skip( "Skip test due to there is no enough port channel with at least 2 members exists in current topology." ) selected_pcs = random.sample(pcs, k=2) in_pc = selected_pcs[0] out_pc = selected_pcs[1] # use first port of in_pc as input port # all ports in out_pc will be output/forward ports pc, pc_members = out_pc[2], out_pc[3] in_ptf_index = mg_facts["minigraph_ptf_indices"][in_pc[3][0]] out_ptf_indices = map(lambda port: mg_facts["minigraph_ptf_indices"][port], out_pc[3]) logging.info( "selected_pcs is: %s, in_ptf_index is %s, out_ptf_indices is %s" % (selected_pcs, in_ptf_index, out_ptf_indices)) tmp_pc = "PortChannel999" pc_ip = out_pc[0] in_peer_ip = in_pc[1] out_peer_ip = out_pc[1] # Step 1: Remove port channel members from port channel for member in pc_members: asichost.config_portchannel_member(pc, member, "del") # Step 2: Remove port channel ip from port channel asichost.config_ip_intf(pc, pc_ip + "/31", "remove") time.sleep(30) int_facts = asichost.interface_facts()['ansible_facts'] pytest_assert(not int_facts['ansible_interface_facts'][pc]['link']) pytest_assert( wait_until(120, 10, 0, asichost.check_bgp_statistic, 'ipv4_idle', 1)) # Step 3: Create tmp port channel with default min-links(1) asichost.config_portchannel(tmp_pc, "add") # Step 4: Add port channel members to tmp port channel for member in pc_members: asichost.config_portchannel_member(tmp_pc, member, "add") # Step 5: Add port channel ip to tmp port channel asichost.config_ip_intf(tmp_pc, pc_ip + "/31", "add") int_facts = asichost.interface_facts()['ansible_facts'] pytest_assert(int_facts['ansible_interface_facts'][tmp_pc]['ipv4'] ['address'] == pc_ip) time.sleep(30) int_facts = asichost.interface_facts()['ansible_facts'] pytest_assert(int_facts['ansible_interface_facts'][tmp_pc]['link']) pytest_assert( wait_until(120, 10, 0, asichost.check_bgp_statistic, 'ipv4_idle', 0)) # Keep sending packets, and add/del different members during that time, observe whether packets lose pkt = testutils.simple_ip_packet(eth_dst=duthost.facts["router_mac"], eth_src=ptfadapter.dataplane.get_mac( 0, in_ptf_index), ip_src=in_peer_ip, ip_dst=out_peer_ip) exp_pkt = pkt.copy() exp_pkt = mask.Mask(exp_pkt) exp_pkt.set_do_not_care_scapy(packet.Ether, 'dst') exp_pkt.set_do_not_care_scapy(packet.Ether, 'src') exp_pkt.set_do_not_care_scapy(packet.IP, 'chksum') exp_pkt.set_do_not_care_scapy(packet.IP, 'ttl') ptfadapter.dataplane.flush() member_update_finished_flag = Queue(1) packet_sending_flag = Queue(1) def del_add_members(): # wait for packets sending started, then starts to update pc members while packet_sending_flag.empty() or (not packet_sending_flag.get()): time.sleep(0.5) asichost.config_portchannel_member(tmp_pc, pc_members[0], "del") time.sleep(2) asichost.config_portchannel_member(tmp_pc, pc_members[0], "add") time.sleep(2) asichost.config_portchannel_member(tmp_pc, pc_members[1], "del") time.sleep(2) asichost.config_portchannel_member(tmp_pc, pc_members[1], "add") time.sleep(5) member_update_finished_flag.put(True) t = threading.Thread(target=del_add_members, name="del_add_members_thread") t.start() t_max = time.time() + 60 send_count = 0 stop_sending = False while not stop_sending: # After 100 packets send, awake del_add_members thread, it happens only once. if send_count == 100: packet_sending_flag.put(True) testutils.send(ptfadapter, in_ptf_index, pkt) send_count += 1 member_update_thread_finished = (not member_update_finished_flag.empty( )) and member_update_finished_flag.get() reach_max_time = time.time() > t_max stop_sending = reach_max_time or member_update_thread_finished t.join(20) match_cnt = testutils.count_matched_packets_all_ports( ptfadapter, exp_pkt, ports=out_ptf_indices) pytest_assert(match_cnt > 0, "Packets not send") pytest_assert(match_cnt == send_count, "Packets lost during pc members add/removal")
def generate_and_verify_icmp_traffic(duthost, ptfadapter, src_port, dst_port, ip_src, ip_dst, pkt_action, tr_type, ttl=64, untagged_icmp_request=False): """ Send ICMP request packet from PTF to DUT and verify that DUT sends/doesn't send ICMP reply packet to PTF. Args: duthost: DUT host object ptfadapter: PTF adapter src_port: Port of PTF dst_port: Port of DUT ip_src: Source IP address of PTF ip_dst: Destination IP address of DUT pkt_action: Packet action (forwarded or drop) tr_type: Type of traffic (TCP or UDP) ttl: Time to live untagged_icmp_request: send untagged ICMP request if True """ vlan_vid = None dl_vlan_enable = False router_mac = duthost.facts['router_mac'] src_port_number = int(get_port_number(src_port)) src_mac = ptfadapter.dataplane.get_mac(0, src_port_number) # Get VLAN ID from name of sub-port if '.' in src_port: vlan_vid = int(src_port.split('.')[1]) dl_vlan_enable = True ip_src = ip_src.split('/')[0] ip_dst = ip_dst.split('/')[0] # Create ICMP request packet pkt = create_packet(eth_dst=router_mac, eth_src=src_mac, ip_src=ip_src, ip_dst=ip_dst, vlan_vid=vlan_vid, dl_vlan_enable=not untagged_icmp_request and dl_vlan_enable, tr_type=tr_type, ttl=64) # Define ICMP reply packet exp_pkt = create_packet(eth_src=router_mac, eth_dst=src_mac, ip_src=ip_dst, ip_dst=ip_src, vlan_vid=vlan_vid, dl_vlan_enable=dl_vlan_enable, icmp_type=0, tr_type=tr_type, ttl=ttl) masked_exp_pkt = mask.Mask(exp_pkt) masked_exp_pkt.set_do_not_care_scapy(packet.IP, "id") masked_exp_pkt.set_do_not_care_scapy(packet.IP, "chksum") masked_exp_pkt.set_do_not_care_scapy(packet.ICMP, "chksum") ptfadapter.dataplane.flush() time.sleep(1) testutils.send_packet(ptfadapter, src_port_number, pkt) dst_port_list = [src_port_number] if pkt_action == ACTION_FWD: testutils.verify_packet_any_port(ptfadapter, masked_exp_pkt, dst_port_list) elif pkt_action == ACTION_DROP: testutils.verify_no_packet_any(ptfadapter, masked_exp_pkt, dst_port_list)
def tunnelVniTest(self): ''' Test verifies corrent tunnel VNI behaviour. ''' print("tunnelVniTest") service_vm_ip = "200.200.200.2" service_vtep_ip = "30.30.30.3" service_vni = 3000 service_vni2 = 4000 try: urif1 = sai_thrift_create_router_interface( self.client, type=SAI_ROUTER_INTERFACE_TYPE_PORT, port_id=self.port25, virtual_router_id=self.uvrf) uneighbor1 = sai_thrift_neighbor_entry_t(rif_id=urif1, ip_address=sai_ipaddress( self.tunnel_ip)) sai_thrift_create_neighbor_entry( self.client, uneighbor1, dst_mac_address=self.underlay_neighbor_mac) unhop1 = sai_thrift_create_next_hop(self.client, ip=sai_ipaddress('10.10.10.1'), router_interface_id=urif1, type=SAI_NEXT_HOP_TYPE_IP) tunnel_route1 = sai_thrift_route_entry_t( vr_id=self.uvrf, destination=sai_ipprefix('10.10.10.1/32')) sai_thrift_create_route_entry(self.client, tunnel_route1, next_hop_id=unhop1) urif2 = sai_thrift_create_router_interface( self.client, type=SAI_ROUTER_INTERFACE_TYPE_PORT, port_id=self.port26, virtual_router_id=self.uvrf) uneighbor2 = sai_thrift_neighbor_entry_t( rif_id=urif2, ip_address=sai_ipaddress(service_vtep_ip)) sai_thrift_create_neighbor_entry( self.client, uneighbor2, dst_mac_address=self.underlay_neighbor_mac2) unhop2 = sai_thrift_create_next_hop(self.client, ip=sai_ipaddress('30.30.30.3'), router_interface_id=urif2, type=SAI_NEXT_HOP_TYPE_IP) tunnel_route2 = sai_thrift_route_entry_t( vr_id=self.uvrf, destination=sai_ipprefix('30.30.30.3/32')) sai_thrift_create_route_entry(self.client, tunnel_route2, next_hop_id=unhop2) tunnel_nexthop1 = sai_thrift_create_next_hop( self.client, type=SAI_NEXT_HOP_TYPE_TUNNEL_ENCAP, tunnel_id=self.tunnel, ip=sai_ipaddress(self.tunnel_ip), tunnel_vni=2000, tunnel_mac=self.inner_dmac) tunnel_nexthop2 = sai_thrift_create_next_hop( self.client, type=SAI_NEXT_HOP_TYPE_TUNNEL_ENCAP, tunnel_id=self.tunnel, ip=sai_ipaddress(service_vtep_ip), tunnel_vni=service_vni, tunnel_mac=self.inner_dmac2) customer_route1 = sai_thrift_route_entry_t( vr_id=self.ovrf, destination=sai_ipprefix(self.vm_ip + '/32')) sai_thrift_create_route_entry(self.client, customer_route1, next_hop_id=tunnel_nexthop1) customer_route2 = sai_thrift_route_entry_t( vr_id=self.ovrf, destination=sai_ipprefix(service_vm_ip + '/32')) sai_thrift_create_route_entry(self.client, customer_route2, next_hop_id=tunnel_nexthop2) pkt = simple_tcp_packet(eth_dst=ROUTER_MAC, eth_src='00:22:22:22:22:22', ip_dst=self.vm_ip, ip_src=self.customer_ip, ip_id=108, ip_ttl=64) inner_pkt = simple_tcp_packet(eth_dst=self.inner_dmac, eth_src=ROUTER_MAC, ip_dst=self.vm_ip, ip_src=self.customer_ip, ip_id=108, ip_ttl=63) vxlan_pkt = mask.Mask( simple_vxlan_packet(eth_src=ROUTER_MAC, eth_dst=self.underlay_neighbor_mac, ip_id=0, ip_src=self.my_lb_ip, ip_dst=self.tunnel_ip, ip_ttl=64, ip_flags=0x2, with_udp_chksum=False, vxlan_vni=self.vni, inner_frame=inner_pkt)) vxlan_pkt.set_do_not_care_scapy(UDP, 'sport') send_packet(self, self.dev_port24, pkt) verify_packet(self, vxlan_pkt, self.dev_port25) pkt = simple_tcp_packet(eth_dst=ROUTER_MAC, eth_src='00:22:22:22:22:22', ip_dst=service_vm_ip, ip_src=self.customer_ip, ip_id=108, ip_ttl=64) inner_pkt = simple_tcp_packet(eth_dst=self.inner_dmac2, eth_src=ROUTER_MAC, ip_dst=service_vm_ip, ip_src=self.customer_ip, ip_id=108, ip_ttl=63) vxlan_pkt = mask.Mask( simple_vxlan_packet(eth_src=ROUTER_MAC, eth_dst=self.underlay_neighbor_mac2, ip_id=0, ip_src=self.my_lb_ip, ip_dst=service_vtep_ip, ip_ttl=64, ip_flags=0x2, with_udp_chksum=False, vxlan_vni=service_vni, inner_frame=inner_pkt)) vxlan_pkt.set_do_not_care_scapy(UDP, 'sport') send_packet(self, self.dev_port24, pkt) verify_packet(self, vxlan_pkt, self.dev_port26) pkt = simple_tcp_packet(eth_dst=ROUTER_MAC, eth_src='00:22:22:22:22:22', ip_dst=service_vm_ip, ip_src=self.customer_ip, ip_id=108, ip_ttl=64) inner_pkt = simple_tcp_packet(eth_dst=self.inner_dmac2, eth_src=ROUTER_MAC, ip_dst=service_vm_ip, ip_src=self.customer_ip, ip_id=108, ip_ttl=63) vxlan_pkt = mask.Mask( simple_vxlan_packet(eth_src=ROUTER_MAC, eth_dst=self.underlay_neighbor_mac2, ip_id=0, ip_src=self.my_lb_ip, ip_dst=service_vtep_ip, ip_ttl=64, ip_flags=0x2, with_udp_chksum=False, vxlan_vni=service_vni2, inner_frame=inner_pkt)) print("Updating nexthop tunnel {} -> {}".format( service_vni, service_vni2)) sai_thrift_set_next_hop_attribute(self.client, tunnel_nexthop2, tunnel_vni=4000) print( sai_thrift_get_next_hop_attribute(self.client, tunnel_nexthop2, tunnel_vni=True)) vxlan_pkt.set_do_not_care_scapy(UDP, 'sport') send_packet(self, self.dev_port24, pkt) verify_packet(self, vxlan_pkt, self.dev_port26) finally: sai_thrift_remove_route_entry(self.client, customer_route2) sai_thrift_remove_route_entry(self.client, customer_route1) sai_thrift_remove_route_entry(self.client, tunnel_route2) sai_thrift_remove_route_entry(self.client, tunnel_route1) sai_thrift_remove_next_hop(self.client, tunnel_nexthop2) sai_thrift_remove_next_hop(self.client, tunnel_nexthop1) sai_thrift_remove_next_hop(self.client, unhop2) sai_thrift_remove_next_hop(self.client, unhop1) sai_thrift_remove_neighbor_entry(self.client, uneighbor2) sai_thrift_remove_neighbor_entry(self.client, uneighbor1) sai_thrift_remove_router_interface(self.client, urif2) sai_thrift_remove_router_interface(self.client, urif1)
def verify_upstream_traffic(host, ptfadapter, tbinfo, itfs, server_ip, pkt_num=100, drop=False): """ @summary: Helper function for verifying upstream packets @param host: The dut host @param ptfadapter: The ptfadapter fixture @param tbinfo: The tbinfo fixture @param ifts: The interface name on DUT @param server_ip: The IP address of server @param pkt_num: The number of packets to generete and tx @param drop: Packets are expected to be dropped if drop is True, and vice versa @return: No return value. An exception will be raised if verify fails. """ random_ip = generate_ip_through_default_route(host).split('/')[0] vlan_table = host.get_running_config_facts()['VLAN'] vlan_name = list(vlan_table.keys())[0] vlan_mac = host.get_dut_iface_mac(vlan_name) router_mac = host.facts['router_mac'] # Generate packets from server to a random IP address, which goes default routes pkt = testutils.simple_ip_packet(eth_dst=vlan_mac, ip_src=server_ip, ip_dst=random_ip) # Generate packet forwarded to portchannels pkt_copy = pkt.copy() pkt_copy[Ether].src = router_mac exp_pkt = mask.Mask(pkt_copy) exp_pkt.set_do_not_care_scapy(Ether, "dst") exp_pkt.set_do_not_care_scapy(IP, "dst") exp_pkt.set_do_not_care_scapy(IP, "ihl") exp_pkt.set_do_not_care_scapy(IP, "tos") exp_pkt.set_do_not_care_scapy(IP, "len") exp_pkt.set_do_not_care_scapy(IP, "id") exp_pkt.set_do_not_care_scapy(IP, "flags") exp_pkt.set_do_not_care_scapy(IP, "frag") exp_pkt.set_do_not_care_scapy(IP, "ttl") exp_pkt.set_do_not_care_scapy(IP, "proto") exp_pkt.set_do_not_care_scapy(IP, "chksum") exp_pkt.set_ignore_extra_bytes() port_channels = get_t1_ptf_pc_ports(host, tbinfo) rx_ports = [] for v in port_channels.values(): rx_ports += v rx_ports = [int(x.strip('eth')) for x in rx_ports] mg_facts = host.get_extended_minigraph_facts(tbinfo) tx_port = mg_facts['minigraph_ptf_indices'][itfs] logger.info( "Verifying upstream traffic. packet number = {} interface = {} server_ip = {} expect_drop = {}" .format(pkt_num, itfs, server_ip, drop)) for i in range(0, pkt_num): ptfadapter.dataplane.flush() testutils.send(ptfadapter, tx_port, pkt, count=1) if drop: testutils.verify_no_packet_any(ptfadapter, exp_pkt, rx_ports) else: testutils.verify_packet_any_port(ptfadapter, exp_pkt, rx_ports)
def craft_packet(src_mac, dst_mac, dst_ip, ip_version, stage, tagged_mode, vlan_id=10, outer_vlan_id=0, pkt_type=None): """ Generate IPV4/IPV6 packets with single or double Vlan Header Args: src_mac: Source MAC address dst_mac: Dest MAC address dst_ip: IP address of packet ip_version: Ip version of packet that should be generated stage: ingress or egress tagged_mode: TAGGED or UNTAGGED vlan_id: Vlan Id number dl_vlan_outer: Outer Vlan ID pkt_type: packet type to be created Returns: QinQ or TCP packet """ DUMMY_IP = '8.8.8.8' exp_pkt_with_tag = tagged_mode in [TYPE_TAGGED, TYPE_COMBINE_TAGGED] if ip_version == IPV4: if pkt_type == 'qinq': pkt = testutils.simple_qinq_tcp_packet(eth_src=src_mac, eth_dst=dst_mac, dl_vlan_outer=outer_vlan_id, vlan_vid=vlan_id, ip_src=DUMMY_IP, ip_dst=dst_ip) if exp_pkt_with_tag: exp_pkt = testutils.simple_tcp_packet( pktlen=96, # Default len (100) - Dot1Q len (4) eth_src=src_mac, eth_dst=dst_mac, dl_vlan_enable=True, vlan_vid=vlan_id, ip_src=DUMMY_IP, ip_dst=dst_ip) else: exp_pkt = pkt else: pkt = testutils.simple_tcp_packet(eth_src=src_mac, eth_dst=dst_mac, ip_src=DUMMY_IP, ip_dst=dst_ip) if exp_pkt_with_tag: exp_pkt = testutils.simple_tcp_packet( pktlen=104, # Default len(100) + Dot1Q len (4) eth_src=src_mac, eth_dst=dst_mac, dl_vlan_enable=True, vlan_vid=outer_vlan_id, ip_src=DUMMY_IP, ip_dst=dst_ip) else: exp_pkt = pkt.copy() exp_pkt = mask.Mask(exp_pkt) exp_pkt.set_do_not_care_scapy(Ether, 'src') exp_pkt.set_do_not_care_scapy(Ether, 'dst') exp_pkt.set_do_not_care_scapy(IP, 'ttl') exp_pkt.set_do_not_care_scapy(IP, 'chksum') else: pkt = testutils.simple_tcpv6_packet(eth_src=src_mac, eth_dst=dst_mac, dl_vlan_enable=True, vlan_vid=outer_vlan_id, ipv6_dst=dst_ip) if exp_pkt_with_tag: exp_pkt = testutils.simple_tcpv6_packet( pktlen=96, # Default len (100) - Dot1Q len (4) eth_src=src_mac, eth_dst=dst_mac, ipv6_dst=dst_ip) else: exp_pkt = pkt return pkt, exp_pkt