def testPacketOut(self): pktout = OpenflowHeader.build(OpenflowType.PacketOut, 43) pktout[1].buffer = 0xffffffff pktout[1].in_port = 4 pktout[1].packet = Ethernet(src="11:22:33:44:55:66", dst="aa:bb:cc:dd:ee:ff") + \ IPv4(src="1.2.3.4", dst="5.6.7.8") + ICMP() pktout[1].actions.append(ActionOutput(port=OpenflowPort.Flood)) self._storePkt(pktout) b = pktout.to_bytes() pktout2 = Packet.from_bytes(b, OpenflowHeader) self.assertEqual(pktout, pktout2)
def testPacketMatch2(self): pkt = Ethernet(src="30:00:00:00:00:03", dst="30:00:00:00:00:02") + \ IPv4(src="10.0.42.200", dst="172.16.42.2", protocol=1) + \ ICMP() m = OpenflowMatch(dl_src="30:00:00:00:00:03", dl_dst="30:00:00:00:00:02", \ nw_src="10.0.42.200", nw_dst="172.16.42.2", \ nw_proto=IPProtocol.ICMP, \ tp_src=8, tp_dst=0, \ dl_type=EtherType.IP, dl_vlan=65535, dl_vlan_pcp=0, \ in_port=2) self.assertTrue(m.matches_packet(pkt))
def testPacketIn(self): pktin = OpenflowHeader.build(OpenflowType.PacketIn, 44) pktin[1].packet = Ethernet(src="11:22:33:44:55:66", dst="aa:bb:cc:dd:ee:ff") + \ IPv4(src="1.2.3.4", dst="5.6.7.8") + ICMP() pktin[1].in_port = OpenflowPort.NoPort self._storePkt(pktin) b = pktin.to_bytes() pktin2 = Packet.from_bytes(b, OpenflowHeader) self.assertEqual(pktin, pktin2) pktin = OpenflowHeader.build(OpenflowType.PacketIn, 44) pktin[1].packet = Ethernet(src="11:22:33:44:55:66", dst="aa:bb:cc:dd:ee:ff") + \ IPv4(src="1.2.3.4", dst="5.6.7.8") + ICMP() pktin[1].in_port = 42 pktin[1].buffer_id = 1334 pktin[1].reason = OpenflowPacketInReason.NoMatch self._storePkt(pktin) b = pktin.to_bytes() pktin2 = Packet.from_bytes(b, OpenflowHeader) self.assertEqual(pktin, pktin2)
def switchy_main(net): my_interfaces = net.interfaces() e = Ethernet() e.dst = 'ff:ff:ff:ff:ff:ff' e.src = '08:00:27:d3:9b:7d' e.ethertype = EtherType.IP ip = IPv4() ip.dstip = '192.168.100.100' icmp = ICMP() for intf in my_interfaces: net.send_packet(intf.name, e + ip + icmp) net.shutdown()
def testBuildMatch(self): pkt = Ethernet(src="11:22:33:44:55:66", dst="aa:bb:cc:dd:ee:ff") + \ IPv4(src="1.2.3.4", dst="5.6.7.8", protocol=6) + \ TCP(srcport=4000, dstport=10000) m = OpenflowMatch.build_from_packet(pkt) self.assertEqual(m.dl_src, "11:22:33:44:55:66") self.assertEqual(m.dl_dst, "aa:bb:cc:dd:ee:ff") self.assertEqual(m.dl_vlan, 0) self.assertEqual(m.dl_vlan_pcp, 0) self.assertEqual(m.nw_proto.value, 6) self.assertEqual(m.nw_tos, 0) self.assertEqual(str(m.nw_src), "1.2.3.4") self.assertEqual(str(m.nw_dst), "5.6.7.8") self.assertEqual(m.tp_src, 4000) self.assertEqual(m.tp_dst, 10000)
def testPacketMatch(self): pkt = Ethernet(src="11:22:33:44:55:66", dst="aa:bb:cc:dd:ee:ff") + \ IPv4(src="1.2.3.4", dst="5.6.7.8", protocol=6) + \ TCP(srcport=4000, dstport=10000) m = OpenflowMatch.build_from_packet(pkt) self.assertTrue(m.matches_packet(pkt)) pkt[TCP].srcport = 42 self.assertFalse(m.matches_packet(pkt)) m.add_wildcard(OpenflowWildcard.TpSrc) self.assertTrue(m.matches_packet(pkt)) pkt[IPv4].src = "1.2.3.0" self.assertFalse(m.matches_packet(pkt)) m.nwsrc_wildcard = 8 self.assertTrue(m.matches_packet(pkt))
def _storePkt(self, ofhdr, dstport=6633): # comment the return to save pcap packets for analysis return pkt = (Ethernet(src="11:22:33:44:55:66") + IPv4() + TCP()) + ofhdr xname = pkt[OpenflowHeader].type.name dumper = PcapDumper("oftests_{}.pcap".format(xname)) pkt[Ethernet].dst = ''.join(list(reversed("11:22:33:44:55:66"))) pkt[IPv4].protocol = 6 pkt[IPv4].src = "149.43.80.25" pkt[IPv4].dst = "149.43.80.25" pkt[IPv4].ttl = 5 pkt[IPv4].ipid = 42 pkt[TCP].dstport = dstport pkt[TCP].srcport = 5555 pkt[TCP].window = 1000 pkt[TCP].seq = 42 pkt[TCP].ack = 500 pkt[TCP].PSH = 1 pkt[TCP].ACK = 1 dumper.write_packet(pkt.to_bytes(), time()) dumper.close()
def testMatchOverlap1(self): m = OpenflowMatch() p = Ethernet() + IPv4() + ICMP() self.assertTrue(m.overlaps_with(m)) m.wildcard_all() self.assertTrue(m.matches_packet(p))