def match_payload(pkt): pkt = scapy.Ether(pkt).load if self.asic_type in ["mellanox"]: pkt = pkt[22:] # Mask the Mellanox specific inner header pkt = scapy.Ether(pkt) return dataplane.match_exp_pkt(payload_mask, pkt)
def match_erspan_III_pkt(exp_pkt, pkt, ignore_tstamp=True): """ Compare ERSPAN_III packets, ignore the timestamp value. Just make sure it is non-zero """ if ignore_tstamp: erspan3 = pkt.getlayer(ERSPAN_III) if erspan3 == None: #self.logger.error("No ERSPAN pkt received") return False if erspan3.timestamp == 0: #self.logger.error("Invalid ERSPAN timestamp") return False #fix the exp_pkt timestamp and compare exp_erspan3 = exp_pkt.getlayer(ERSPAN_III) if erspan3 == None: #self.logger.error("Test user error - exp_pkt is not ERSPAN_III packet") return False exp_erspan3.timestamp = 0 erspan3.timestamp = 0 return dataplane.match_exp_pkt(exp_pkt, pkt)
def sendReceive(self, pkt2send, src_port, destination_ports): """ @summary Send packet and verify it is received/not received on the expected ports """ testutils.send_packet(self, src_port, pkt2send) (index, rcv_pkt, received) = self.receivePacketOnPorts(destination_ports) self.tests_total += 1 if not received: return False scapy_pkt = scapy.Ether(rcv_pkt) if scapy.IP not in scapy_pkt: return False if self.expected_dst_mac and scapy_pkt.dst != self.expected_dst_mac: return False if scapy_pkt[scapy.IP].src != self.session_src_ip: return False if scapy_pkt[scapy.IP].dst != self.session_dst_ip: return False if scapy_pkt[scapy.IP].ttl != self.session_ttl: return False # TODO: Fanout modifies DSCP. TOS value is olways 0. #if (scapy_pkt[scapy.IP].tos >> 2) != self.session_dscp: # return False payload = str(scapy_pkt[scapy.GRE].payload) if self.asic_type in ["mellanox"]: payload = str(scapy_pkt[scapy.GRE].payload)[22:] if self.asic_type in ["barefoot"]: payload = str(scapy_pkt[scapy.GRE].payload)[12:] inner_pkt = scapy.Ether(payload) if self.mirror_stage == 'egress': pkt2send[ 'IP'].ttl -= 1 # expect mirrored packet on egress has TTL decremented masked_inner_pkt = Mask(inner_pkt) masked_inner_pkt.set_do_not_care_scapy(scapy.Ether, "dst") masked_inner_pkt.set_do_not_care_scapy(scapy.Ether, "src") if scapy.IP in inner_pkt: masked_inner_pkt.set_do_not_care_scapy(scapy.IP, "chksum") if scapy.TCP in inner_pkt: masked_inner_pkt.set_do_not_care_scapy(scapy.TCP, "chksum") return dataplane.match_exp_pkt(masked_inner_pkt, pkt2send)
def runSendReceiveTest(self, pkt2send, src_port, destination_ports): """ @summary Send packet and verify it is received/not received on the expected ports """ testutils.send_packet(self, src_port, pkt2send) (index, rcv_pkt, received) = self.receivePacketOnPorts(destination_ports) self.tests_total += 1 if not received: return False scapy_pkt = scapy.Ether(rcv_pkt) if scapy.IP not in scapy_pkt: return False if self.expected_dst_mac and scapy_pkt.dst != self.expected_dst_mac: return False if scapy_pkt[scapy.IP].src != self.session_src_ip: return False if scapy_pkt[scapy.IP].dst != self.session_dst_ip: return False if scapy_pkt[scapy.IP].ttl != self.session_ttl: return False # TODO: Fanout modifies DSCP. TOS value is olways 0. #if (scapy_pkt[scapy.IP].tos >> 2) != self.session_dscp: # return False payload = str(scapy_pkt[scapy.GRE].payload) if self.hwsku in [ "ACS-MSN2700", "ACS-MSN2100", "ACS-MSN2410", "ACS-MSN2740", "Mellanox-SN2700" ]: payload = str(scapy_pkt[scapy.GRE].payload)[22:] if self.asic_type in ["barefoot"]: payload = str(scapy_pkt[scapy.GRE].payload)[12:] inner_pkt = scapy.Ether(payload) masked_inner_pkt = Mask(inner_pkt) if scapy.IP in inner_pkt: masked_inner_pkt.set_do_not_care_scapy(scapy.IP, "chksum") if scapy.TCP in inner_pkt: masked_inner_pkt.set_do_not_care_scapy(scapy.TCP, "chksum") return dataplane.match_exp_pkt(masked_inner_pkt, pkt2send)
def verify_packet_in(self, exp_pkt, exp_in_port, timeout=2): pkt_in_msg = self.get_packet_in(timeout=timeout) in_port_ = stringify(exp_in_port, 2) rx_in_port_ = pkt_in_msg.metadata[0].value if in_port_ != rx_in_port_: rx_inport = struct.unpack("!h", rx_in_port_)[0] self.fail("Wrong packet-in ingress port, expected {} but received was {}" .format(exp_in_port, rx_inport)) rx_pkt = Ether(pkt_in_msg.payload) if not match_exp_pkt(exp_pkt, rx_pkt): self.fail("Received packet-in is not the expected one\n" + format_pkt_match(rx_pkt, exp_pkt))
def verify_packet_list(test, port_ll, pkt_ll): more_to_rx = False for port_list in port_ll: if len(port_list) != 0: more_to_rx = True while more_to_rx: found_port = False found_pkt = False (rcv_device, rcv_port, rcv_pkt, pkt_time) = test.dataplane.poll(timeout=1.0) #print "Rx on port", rcv_port #print format_packet( rcv_pkt ) if rcv_port is None: print "Didn't receive packet!!!" print "Expected ports remaining:", port_ll test.assertTrue(rcv_port is not None) # See if the received port+packet pair is in any of the lists passed in. for port_list, pkt_list in zip(port_ll, pkt_ll): if rcv_port in port_list: found_port = True for exp_pkt in pkt_list: if dataplane.match_exp_pkt(exp_pkt, rcv_pkt): pkt_list.remove(exp_pkt) found_pkt = True break if found_pkt: port_list.remove(rcv_port) break if found_port != True or found_pkt != True: print "Unexpected Rx: port", rcv_port print format_packet(rcv_pkt) print "Expected the following:" for port_list, pkt_list in zip(port_ll, pkt_ll): print " Ports:", sorted(port_list) for pkt in pkt_list: print " Pkt: ", format_packet(pkt) test.assertTrue(found_port == True, "Unexpected port %r" % rcv_port) test.assertTrue(found_pkt == True, "Unexpected pkt on port %r" % rcv_port) more_to_rx = False for port_list in port_ll: if len(port_list) != 0: more_to_rx = True (rcv_device, rcv_port, rcv_pkt, pkt_time) = test.dataplane.poll(timeout=0.1) if rcv_port != None: print "Extra Rx: port", rcv_port, "Packet", format_packet(rcv_pkt) test.assertTrue(rcv_pkt == None, "Receive extra packet")
def match_payload(pkt): if self.asic_type in ["mellanox"]: pkt = scapy.Ether(pkt).load pkt = pkt[22:] # Mask the Mellanox specific inner header pkt = scapy.Ether(pkt) elif self.asic_type == "barefoot": pkt = scapy.Ether(pkt).load else: pkt = scapy.Ether(pkt)[scapy.GRE].payload return dataplane.match_exp_pkt(payload_mask, pkt)
def __exit__(self, exc_type, exc_value, traceback): self.dump_utility.__exit__(exc_type, exc_value, traceback) logging.info("the expected packet:\n%s", str(self.exp_pkt)) self.matched_packets = [p for p in self.captured_packets if match_exp_pkt(self.exp_pkt, p)] logging.info("received %d matched packets", len(self.matched_packets)) if self.matched_packets: logging.info( "display the most recent matched captured packet:\n%s", self._list_layer_str(self.matched_packets[-1]) ) if self.existing and not self.matched_packets: raise ValueError("Failed to find expected packet.") if not self.existing and self.matched_packets: raise ValueError("Found expected packet.")
def verify_packet_in(self, exp_pkt, exp_in_port, timeout=2): in_port_ = stringify(exp_in_port, 2) exp_pkt_in = p4runtime_pb2.PacketIn() exp_pkt_in.payload = str(exp_pkt) ingress_physical_port = exp_pkt_in.metadata.add() ingress_physical_port.metadata_id = 0 ingress_physical_port.value = in_port_ if self.generate_tv: tvutils.add_packet_in_expectation(self.tc, exp_pkt_in) else: pkt_in_msg = self.get_packet_in(timeout=timeout) rx_in_port_ = pkt_in_msg.metadata[0].value if in_port_ != rx_in_port_: rx_inport = struct.unpack("!h", rx_in_port_)[0] self.fail("Wrong packet-in ingress port, expected {} but received was {}" .format(exp_in_port, rx_inport)) rx_pkt = Ether(pkt_in_msg.payload) if not match_exp_pkt(exp_pkt, rx_pkt): self.fail("Received packet-in is not the expected one\n" + format_pkt_match(rx_pkt, exp_pkt))
def runSendReceiveTest(self, pkt2send, src_port, destination_ports): """ @summary Send packet and verify it is received/not received on the expected ports """ testutils.send_packet(self, src_port, pkt2send) (index, rcv_pkt, received) = self.receivePacketOnPorts(destination_ports) self.tests_total += 1 if not received: return False scapy_pkt = scapy.Ether(rcv_pkt) if scapy.IP not in scapy_pkt: return False if self.expected_dst_mac and scapy_pkt.dst != self.expected_dst_mac: return False if scapy_pkt[scapy.IP].src != self.session_src_ip: return False if scapy_pkt[scapy.IP].dst != self.session_dst_ip: return False if scapy_pkt[scapy.IP].ttl != self.session_ttl: return False # TODO: Fanout modifies DSCP. TOS value is olways 0. #if (scapy_pkt[scapy.IP].tos >> 2) != self.session_dscp: # return False payload = str(scapy_pkt[scapy.GRE].payload)[22:] inner_pkt = scapy.Ether(payload) return dataplane.match_exp_pkt(pkt2send, inner_pkt)