def generate_and_verify_decap_traffic(duthost, ptfadapter, src_port, dst_port, ip_src, ip_dst, tr_type, ip_tunnel=None): """ Send encapsulated packet from PTF to DUT and verify that DUT sends/doesn't send TCP/UDP packet to PTF. Args: duthost: DUT host object ptfadapter: PTF adapter src_port: Source port of PTF dst_port: Destination port of PTF ip_src: Source IP address of PTF ip_dst: Destination IP address of PTF tr_type: Type of traffic (TCP or UDP) ip_tunnel: Tunnel IP address of DUT """ router_mac = duthost.facts['router_mac'] src_port_number = int(get_port_number(src_port)) dst_port_number = int(get_port_number(dst_port)) ip_src = ip_src.split('/')[0] ip_dst = ip_dst.split('/')[0] ip_tunnel = ip_tunnel.split('/')[0] # Define encapsulated packet pkt = create_packet(eth_dst=router_mac, eth_src=ptfadapter.dataplane.get_mac( 0, src_port_number), ip_src=ip_src, ip_dst=ip_dst, ip_tunnel=ip_tunnel, vlan_vid=int(src_port.split('.')[1]), dl_vlan_enable=True, tr_type=tr_type, ttl=64) # Build expected packet inner_packet = pkt[packet.IP].payload[packet.IP].copy() exp_pkt = Ether(src=router_mac, dst=ptfadapter.dataplane.get_mac( 0, dst_port_number)) / Dot1Q( vlan=int(dst_port.split('.')[1])) / inner_packet exp_pkt['IP'].ttl -= 1 update_dut_arp_table(duthost, ip_dst) ptfadapter.dataplane.flush() time.sleep(1) testutils.send_packet(ptfadapter, src_port_number, pkt) pkt_filter = FilterPktBuffer(ptfadapter=ptfadapter, exp_pkt=exp_pkt, dst_port_numbers=dst_port_number, match_fields=[("802.1Q", "vlan"), ("Ethernet", "src"), ("Ethernet", "dst"), ("IP", "src"), ("IP", "dst")], ignore_fields=[]) pkt_in_buffer = pkt_filter.filter_pkt_in_buffer() pytest_assert(pkt_in_buffer is True, "Expected packet not available:\n{}".format(pkt_in_buffer))
def generate_and_verify_balancing_traffic(duthost, ptfhost, ptfadapter, src_port, dst_port, ip_src, ip_dst, pktlen, ttl): """ Send TCP packets and verify balancing range between sub-ports. Args: duthost: DUT host object ptfhost: PTF 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) pktlen: packet length ttl: Time to live """ 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) ip_src = '10.0.0.1' ip_dst = ip_dst.split('/')[0] vlan_vid = None dl_vlan_enable = False send_pkt_length = pktlen if constants.VLAN_SUB_INTERFACE_SEPARATOR in src_port: vlan_vid = int( src_port.split(constants.VLAN_SUB_INTERFACE_SEPARATOR)[1]) dl_vlan_enable = True send_pkt_length += len(scapyall.Dot1Q()) pkt = create_packet(eth_src=src_mac, eth_dst=router_mac, ip_src=ip_src, ip_dst=ip_dst, vlan_vid=vlan_vid, dl_vlan_enable=dl_vlan_enable, tr_type='TCP', ttl=64, pktlen=send_pkt_length) ptfadapter.dataplane.flush() time.sleep(2) for _ in range(BALANCING_TEST_TIMES * len(dst_port)): testutils.send_packet(ptfadapter, src_port_number, pkt) pkt['IP'].src = str( ipaddress.ip_address(pkt['IP'].src.encode().decode()) + 1) exp_pkt = create_packet(eth_src=router_mac, eth_dst=src_mac, ip_src=ip_src, ip_dst=ip_dst, vlan_vid=None, dl_vlan_enable=False, tr_type='TCP', ttl=ttl, pktlen=pktlen) ifaces_map = ptfhost.host.options['variable_manager'].extra_vars[ 'ifaces_map'] config_port_indices = {v: k for k, v in ifaces_map.items()} dst_port_numbers = [ config_port_indices[k] for k in config_port_indices if k in dst_port ] ignore_fields = [("Ether", "dst"), ("IP", "src"), ("IP", "chksum"), ("TCP", "chksum")] if dl_vlan_enable: ignore_fields.append(("Ether", "type")) pkt_filter = FilterPktBuffer(ptfadapter=ptfadapter, exp_pkt=exp_pkt, dst_port_numbers=dst_port_numbers, match_fields=[("Ethernet", "src"), ("IP", "dst"), ('TCP', "dport")], ignore_fields=ignore_fields) pkt_in_buffer = pkt_filter.filter_pkt_in_buffer() pytest_assert(pkt_in_buffer is True, "Expected packet not available:\n{}".format(pkt_in_buffer)) pytest_assert(check_balancing(pkt_filter.matched_index), "Balancing error:\n{}".format(pkt_filter.matched_index))
def generate_and_verify_tcp_udp_traffic(duthost, ptfadapter, src_port, dst_port, ip_src, ip_dst, tr_type, pktlen, ttl): """ Send TCP/UDP packet from PTF to DUT and verify that DUT sends/doesn't send TCP/UDP 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) pktlen: packet length ttl: Time to live """ src_vlan_vid = None dst_vlan_vid = None src_dl_vlan_enable = False dst_dl_vlan_enable = False router_mac = duthost.facts['router_mac'] src_port_number = int(get_port_number(src_port)) dst_port_number = int(get_port_number(dst_port)) src_mac = ptfadapter.dataplane.get_mac(0, src_port_number) dst_mac = ptfadapter.dataplane.get_mac(0, dst_port_number) # Get VLAN ID from name of sub-port if '.' in src_port: src_vlan_vid = int(src_port.split('.')[1]) src_dl_vlan_enable = True if '.' in dst_port: dst_vlan_vid = int(dst_port.split('.')[1]) dst_dl_vlan_enable = True ip_src = ip_src.split('/')[0] ip_dst = ip_dst.split('/')[0] pkt = create_packet(eth_src=src_mac, eth_dst=router_mac, ip_src=ip_src, ip_dst=ip_dst, vlan_vid=src_vlan_vid, dl_vlan_enable=src_dl_vlan_enable, tr_type=tr_type, ttl=64) exp_pkt = create_packet(eth_src=router_mac, eth_dst=dst_mac, ip_src=ip_src, ip_dst=ip_dst, vlan_vid=dst_vlan_vid, dl_vlan_enable=dst_dl_vlan_enable, tr_type=tr_type, ttl=ttl, pktlen=pktlen) ptfadapter.dataplane.flush() time.sleep(1) testutils.send_packet(ptfadapter, src_port_number, pkt) pkt_filter = FilterPktBuffer(ptfadapter=ptfadapter, exp_pkt=exp_pkt, dst_port_numbers=dst_port_number, match_fields=[("802.1Q", "vlan"), ("Ethernet", "src"), ("Ethernet", "dst"), ("IP", "src"), ("IP", "dst"), (tr_type, "dport")], ignore_fields=[]) pkt_in_buffer = pkt_filter.filter_pkt_in_buffer() pytest_assert(pkt_in_buffer is True, "Expected packet not available:\n{}".format(pkt_in_buffer))