Dan Talayco | c901f4d | 2010-03-07 21:55:45 -0800 | [diff] [blame] | 1 | |
Dan Talayco | d2ca103 | 2010-03-10 14:40:26 -0800 | [diff] [blame] | 2 | import sys |
| 3 | |
| 4 | try: |
| 5 | import scapy.all as scapy |
| 6 | except: |
| 7 | try: |
| 8 | import scapy as scapy |
| 9 | except: |
| 10 | sys.exit("Need to install scapy for packet parsing") |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 11 | |
Dan Talayco | c901f4d | 2010-03-07 21:55:45 -0800 | [diff] [blame] | 12 | import oftest.controller as controller |
| 13 | import oftest.cstruct as ofp |
| 14 | import oftest.message as message |
| 15 | import oftest.dataplane as dataplane |
| 16 | import oftest.action as action |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 17 | import oftest.parse as parse |
Dan Talayco | c901f4d | 2010-03-07 21:55:45 -0800 | [diff] [blame] | 18 | import logging |
| 19 | |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 20 | def delete_all_flows(ctrl, logger): |
| 21 | """ |
| 22 | Delete all flows on the switch |
| 23 | @param ctrl The controller object for the test |
| 24 | @param logger Logging object |
| 25 | """ |
| 26 | |
Dan Talayco | c901f4d | 2010-03-07 21:55:45 -0800 | [diff] [blame] | 27 | logger.info("Deleting all flows") |
| 28 | msg = message.flow_mod() |
| 29 | msg.match.wildcards = ofp.OFPFW_ALL |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 30 | msg.out_port = ofp.OFPP_NONE |
Dan Talayco | c901f4d | 2010-03-07 21:55:45 -0800 | [diff] [blame] | 31 | msg.command = ofp.OFPFC_DELETE |
| 32 | msg.buffer_id = 0xffffffff |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 33 | return ctrl.message_send(msg) |
| 34 | |
| 35 | def simple_tcp_packet(pktlen=100, |
| 36 | dl_dst='00:01:02:03:04:05', |
| 37 | dl_src='00:06:07:08:09:0a', |
Tatsuya Yabe | 460321e | 2010-05-25 17:50:49 -0700 | [diff] [blame] | 38 | dl_vlan_enable=False, |
| 39 | dl_vlan=0, |
| 40 | dl_vlan_pcp=0, |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 41 | ip_src='192.168.0.1', |
| 42 | ip_dst='192.168.0.2', |
Tatsuya Yabe | 460321e | 2010-05-25 17:50:49 -0700 | [diff] [blame] | 43 | ip_tos=0, |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 44 | tcp_sport=1234, |
| 45 | tcp_dport=80 |
| 46 | ): |
| 47 | """ |
| 48 | Return a simple dataplane TCP packet |
| 49 | |
| 50 | Supports a few parameters: |
| 51 | @param len Length of packet in bytes w/o CRC |
| 52 | @param dl_dst Destinatino MAC |
| 53 | @param dl_src Source MAC |
Tatsuya Yabe | 460321e | 2010-05-25 17:50:49 -0700 | [diff] [blame] | 54 | @param dl_vlan_enable True if the packet is with vlan, False otherwise |
| 55 | @param dl_vlan VLAN ID |
| 56 | @param dl_vlan_pcp VLAN priority |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 57 | @param ip_src IP source |
| 58 | @param ip_dst IP destination |
Tatsuya Yabe | 460321e | 2010-05-25 17:50:49 -0700 | [diff] [blame] | 59 | @param ip_tos IP ToS |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 60 | @param tcp_dport TCP destination port |
| 61 | @param ip_sport TCP source port |
| 62 | |
| 63 | Generates a simple TCP request. Users |
| 64 | shouldn't assume anything about this packet other than that |
| 65 | it is a valid ethernet/IP/TCP frame. |
| 66 | """ |
Tatsuya Yabe | 460321e | 2010-05-25 17:50:49 -0700 | [diff] [blame] | 67 | if (dl_vlan_enable): |
| 68 | pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \ |
| 69 | scapy.Dot1Q(prio=dl_vlan_pcp, id=0, vlan=dl_vlan)/ \ |
| 70 | scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos)/ \ |
| 71 | scapy.TCP(sport=tcp_sport, dport=tcp_dport) |
| 72 | else: |
| 73 | pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \ |
| 74 | scapy.IP(src=ip_src, dst=ip_dst, tos=ip_tos)/ \ |
| 75 | scapy.TCP(sport=tcp_sport, dport=tcp_dport) |
| 76 | |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 77 | pkt = pkt/("D" * (pktlen - len(pkt))) |
| 78 | |
| 79 | return pkt |
| 80 | |
| 81 | def do_barrier(ctrl): |
| 82 | b = message.barrier_request() |
| 83 | ctrl.transact(b) |