def runTest(self): pktlen = 70 pkt = testutils.simple_ipv4ip_packet(pktlen=pktlen) pkt2 = testutils.simple_ipv4ip_packet(pktlen=pktlen, inner_frame=pkt["IP"]) testutils.send_packet(self, (0, 1), pkt2) print("packet sent") testutils.verify_packet(self, pkt2, (1, 1)) testutils.verify_no_other_packets(self, 1)
def create_pkt(eth_src, eth_dst, ip_src, ip_dst, payload=None): pkt = testutils.simple_ipv4ip_packet(eth_src=eth_src, eth_dst=eth_dst, ip_src=ip_src, ip_dst=ip_dst, inner_frame=payload) return pkt
def create_ipv4_inner_pkt_only(self, src_ip, dst_ip, tos, encap=False, ttl=64): """Creates an IP only packet for the test @param src_ip: source ip @param dst_ip: destination ip @param tos: type of service field @param encap: build encapsulated packet. If @encap is True the return packet would be: IP(@src_ip, @dst_ip, @tos) / IP(dst_ip=4.4.4.4, src_ip=3.3.3.3) / TCP() @param ttl: ttl field """ inner_pkt = simple_ip_only_packet(ip_dst=dst_ip, ip_src=src_ip, ip_ttl=ttl, ip_tos=tos) if encap: inner_pkt2 = self.create_ipv4_inner_pkt_only( self.DEFAULT_INNER2_V4_PKT_SRC_IP, self.DEFAULT_INNER2_V4_PKT_DST_IP, 0) inner_pkt = simple_ipv4ip_packet( ip_src=src_ip, ip_dst=dst_ip, ip_tos=tos, ip_ttl=ttl, inner_frame=inner_pkt2).getlayer( scapy.IP) # get only the IP layer return inner_pkt
def build_encapsulated_packet(rand_selected_interface, ptfadapter, rand_selected_dut, tunnel_traffic_monitor): """Build the encapsulated packet sent from T1 to ToR.""" tor = rand_selected_dut _, server_ips = rand_selected_interface server_ipv4 = server_ips["server_ipv4"].split("/")[0] config_facts = tor.get_running_config_facts() try: peer_ipv4_address = [_["address_ipv4"] for _ in config_facts["PEER_SWITCH"].values()][0] except IndexError: raise ValueError("Failed to get peer ToR address from CONFIG_DB") tor_ipv4_address = [_ for _ in config_facts["LOOPBACK_INTERFACE"]["Loopback0"] if is_ipv4_address(_.split("/")[0])][0] tor_ipv4_address = tor_ipv4_address.split("/")[0] inner_dscp = random.choice(range(0, 33)) inner_ttl = random.choice(range(3, 65)) inner_packet = testutils.simple_ip_packet( ip_src="1.1.1.1", ip_dst=server_ipv4, ip_dscp=inner_dscp, ip_ttl=inner_ttl )[IP] packet = testutils.simple_ipv4ip_packet( eth_dst=tor.facts["router_mac"], eth_src=ptfadapter.dataplane.get_mac(0, 0), ip_src=peer_ipv4_address, ip_dst=tor_ipv4_address, ip_dscp=inner_dscp, ip_ttl=255, inner_frame=inner_packet ) logging.info("the encapsulated packet to send:\n%s", tunnel_traffic_monitor._dump_show_str(packet)) return packet
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 runTest(self): pktlen = 70 pkt = testutils.simple_ipv4ip_packet(pktlen=pktlen) self.assertEqual(len(pkt), pktlen) testutils.send_packet(self, (0, 1), pkt) print("packet sent") testutils.verify_packet(self, pkt, (1, 1)) testutils.verify_no_other_packets(self, 1)
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 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 create_encap_packet(self, dst_ip, outer_pkt='ipv4', triple_encap=False, outer_ttl=None, inner_ttl=None): """Creates an IPv4/IPv6 encapsulated packet in @outer_pkt packet @param dst_ip: Destination IP for inner packet. Depending @dst_ip IPv4 or IPv6 packet will be created @param outer_pkt: Outer packet type to encapsulate inner packet in (ipv4/ipv6) @param triple_encap: Whether to build triple encapsulated packet @outer_ttl: TTL for the outer layer @inner_ttl: TTL for the inner layer @return: built packet and expected packet to match after decapsulation """ src_mac = self.dataplane.get_mac(0, 0) dst_mac = '00:11:22:33:44:55' router_mac = self.test_params['router_mac'] # Set DSCP value for the inner layer dscp_in = self.DSCP_RANGE[self.dscp_in_idx] self.dscp_in_idx = (self.dscp_in_idx + 1) % len( self.DSCP_RANGE) # Next packet will use a different DSCP # TC for IPv6, ToS for IPv4 tc_in = tos_in = dscp_in << 2 # Set DSCP value for the outer layer dscp_out = self.DSCP_RANGE[self.dscp_out_idx] self.dscp_out_idx = (self.dscp_out_idx + 1) % len( self.DSCP_RANGE) # Next packet will use a different DSCP # TC for IPv6, ToS for IPv4 tc_out = tos_out = dscp_out << 2 if "pipe" == self.test_params['dscp_mode']: exp_tc = exp_tos = tc_in elif "uniform" == self.test_params['dscp_mode']: exp_tc = exp_tos = tc_out else: print("ERROR: no dscp is configured") exit() # Set TTL value for the outer layer if outer_ttl is None: outer_ttl = self.TTL_RANGE[self.ttl_out_idx] self.ttl_out_idx = (self.ttl_out_idx + 1) % len( self.TTL_RANGE) # Next packet will use a different TTL # Set TTL value for the inner layer if inner_ttl is None: inner_ttl = self.TTL_RANGE[self.ttl_in_idx] self.ttl_in_idx = (self.ttl_in_idx + 1) % len( self.TTL_RANGE) # Next packet will use a different TTL if "pipe" == self.test_params['ttl_mode']: exp_ttl = inner_ttl - 1 elif "uniform" == self.test_params["ttl_mode"]: exp_ttl = outer_ttl - 1 else: print("ERROR: unexpected ttl_mode is configured") exit() if ipaddress.ip_address(unicode(dst_ip)).version == 6: inner_src_ip = self.DEFAULT_INNER_V6_PKT_SRC_IP # build inner packet, if triple_encap is True inner_pkt would be double encapsulated inner_pkt = self.create_ipv6_inner_pkt_only(inner_src_ip, dst_ip, tos_in, triple_encap, hlim=inner_ttl) # build expected packet based on inner packet # set the correct L2 fields exp_pkt = scapy.Ether(dst=dst_mac, src=router_mac) / inner_pkt # set expected TC value exp_pkt['IPv6'].tc = exp_tc # decrement TTL exp_pkt['IPv6'].hlim = exp_ttl else: inner_src_ip = self.DEFAULT_INNER_V4_PKT_SRC_IP # build inner packet, if triple_encap is True inner_pkt would be double encapsulated inner_pkt = self.create_ipv4_inner_pkt_only(inner_src_ip, dst_ip, tos_in, triple_encap, ttl=inner_ttl) # build expected packet based on inner packet # set the correct L2 fields exp_pkt = scapy.Ether(dst=dst_mac, src=router_mac) / inner_pkt # set expected ToS value exp_pkt['IP'].tos = exp_tos # decrement TTL exp_pkt['IP'].ttl = exp_ttl if outer_pkt == 'ipv4': pkt = simple_ipv4ip_packet(eth_dst=router_mac, eth_src=src_mac, ip_src='1.1.1.1', ip_dst=self.test_params['lo_ip'], ip_tos=tos_out, ip_ttl=outer_ttl, inner_frame=inner_pkt) elif outer_pkt == 'ipv6': pkt = simple_ipv6ip_packet(eth_dst=router_mac, eth_src=src_mac, ipv6_src='1::1', ipv6_dst=self.test_params['lo_ipv6'], ipv6_tc=tc_out, ipv6_hlim=outer_ttl, inner_frame=inner_pkt) else: raise Exception("ERROR: invalid outer packet type ", outer_pkt) return pkt, exp_pkt