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 |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 3 | import copy |
Dan Talayco | d2ca103 | 2010-03-10 14:40:26 -0800 | [diff] [blame] | 4 | |
| 5 | try: |
| 6 | import scapy.all as scapy |
| 7 | except: |
| 8 | try: |
| 9 | import scapy as scapy |
| 10 | except: |
| 11 | sys.exit("Need to install scapy for packet parsing") |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 12 | |
Dan Talayco | c901f4d | 2010-03-07 21:55:45 -0800 | [diff] [blame] | 13 | import oftest.controller as controller |
| 14 | import oftest.cstruct as ofp |
| 15 | import oftest.message as message |
| 16 | import oftest.dataplane as dataplane |
| 17 | import oftest.action as action |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 18 | import oftest.parse as parse |
Dan Talayco | c901f4d | 2010-03-07 21:55:45 -0800 | [diff] [blame] | 19 | import logging |
| 20 | |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 21 | def delete_all_flows(ctrl, logger): |
| 22 | """ |
| 23 | Delete all flows on the switch |
| 24 | @param ctrl The controller object for the test |
| 25 | @param logger Logging object |
| 26 | """ |
| 27 | |
Dan Talayco | c901f4d | 2010-03-07 21:55:45 -0800 | [diff] [blame] | 28 | logger.info("Deleting all flows") |
| 29 | msg = message.flow_mod() |
| 30 | msg.match.wildcards = ofp.OFPFW_ALL |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 31 | msg.out_port = ofp.OFPP_NONE |
Dan Talayco | c901f4d | 2010-03-07 21:55:45 -0800 | [diff] [blame] | 32 | msg.command = ofp.OFPFC_DELETE |
| 33 | msg.buffer_id = 0xffffffff |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 34 | return ctrl.message_send(msg) |
| 35 | |
| 36 | def simple_tcp_packet(pktlen=100, |
| 37 | dl_dst='00:01:02:03:04:05', |
| 38 | dl_src='00:06:07:08:09:0a', |
| 39 | ip_src='192.168.0.1', |
| 40 | ip_dst='192.168.0.2', |
| 41 | tcp_sport=1234, |
| 42 | tcp_dport=80 |
| 43 | ): |
| 44 | """ |
| 45 | Return a simple dataplane TCP packet |
| 46 | |
| 47 | Supports a few parameters: |
| 48 | @param len Length of packet in bytes w/o CRC |
| 49 | @param dl_dst Destinatino MAC |
| 50 | @param dl_src Source MAC |
| 51 | @param ip_src IP source |
| 52 | @param ip_dst IP destination |
| 53 | @param tcp_dport TCP destination port |
| 54 | @param ip_sport TCP source port |
| 55 | |
| 56 | Generates a simple TCP request. Users |
| 57 | shouldn't assume anything about this packet other than that |
| 58 | it is a valid ethernet/IP/TCP frame. |
| 59 | """ |
| 60 | pkt = scapy.Ether(dst=dl_dst, src=dl_src)/ \ |
| 61 | scapy.IP(src=ip_src, dst=ip_dst)/ \ |
| 62 | scapy.TCP(sport=tcp_sport, dport=tcp_dport) |
| 63 | pkt = pkt/("D" * (pktlen - len(pkt))) |
| 64 | |
| 65 | return pkt |
| 66 | |
| 67 | def do_barrier(ctrl): |
| 68 | b = message.barrier_request() |
| 69 | ctrl.transact(b) |
Dan Talayco | 92c9912 | 2010-06-03 13:53:18 -0700 | [diff] [blame] | 70 | |
| 71 | |
| 72 | def port_config_get(controller, port_no, logger): |
| 73 | """ |
| 74 | Get a port's configuration |
| 75 | |
| 76 | Gets the switch feature configuration and grabs one port's |
| 77 | configuration |
| 78 | |
| 79 | @returns (hwaddr, config, advert) The hwaddress, configuration and |
| 80 | advertised values |
| 81 | """ |
| 82 | request = message.features_request() |
| 83 | reply, pkt = controller.transact(request, timeout=2) |
| 84 | logger.debug(reply.show()) |
| 85 | if reply is None: |
| 86 | logger.warn("Get feature request failed") |
| 87 | return None, None, None |
| 88 | for idx in range(len(reply.ports)): |
| 89 | if reply.ports[idx].port_no == port_no: |
| 90 | return (reply.ports[idx].hw_addr, reply.ports[idx].config, |
| 91 | reply.ports[idx].advertised) |
| 92 | |
| 93 | logger.warn("Did not find port number for port config") |
| 94 | return None, None, None |
| 95 | |
| 96 | def port_config_set(controller, port_no, config, mask, logger): |
| 97 | """ |
| 98 | Set the port configuration according the given parameters |
| 99 | |
| 100 | Gets the switch feature configuration and updates one port's |
| 101 | configuration value according to config and mask |
| 102 | """ |
| 103 | logger.info("Setting port " + str(port_no) + " to config " + str(config)) |
| 104 | request = message.features_request() |
| 105 | reply, pkt = controller.transact(request, timeout=2) |
| 106 | if reply is None: |
| 107 | return -1 |
| 108 | logger.debug(reply.show()) |
| 109 | for idx in range(len(reply.ports)): |
| 110 | if reply.ports[idx].port_no == port_no: |
| 111 | break |
| 112 | if idx >= len(reply.ports): |
| 113 | return -1 |
| 114 | mod = message.port_mod() |
| 115 | mod.port_no = port_no |
| 116 | mod.hw_addr = reply.ports[idx].hw_addr |
| 117 | mod.config = config |
| 118 | mod.mask = mask |
| 119 | mod.advertise = reply.ports[idx].advertised |
| 120 | rv = controller.message_send(mod) |
| 121 | return rv |
| 122 | |
| 123 | def receive_pkt_check(dataplane, pkt, yes_ports, no_ports, assert_if, logger): |
| 124 | """ |
| 125 | Check for proper receive packets across all ports |
| 126 | @param dataplane The dataplane object |
| 127 | @param pkt Expected packet; may be None if yes_ports is empty |
| 128 | @param yes_ports Set or list of ports that should recieve packet |
| 129 | @param no_ports Set or list of ports that should not receive packet |
| 130 | @param assert_if Object that implements assertXXX |
| 131 | """ |
| 132 | for ofport in yes_ports: |
| 133 | logger.debug("Checking for pkt on port " + str(ofport)) |
| 134 | (rcv_port, rcv_pkt, pkt_time) = dataplane.poll( |
| 135 | port_number=ofport, timeout=1) |
| 136 | assert_if.assertTrue(rcv_pkt is not None, |
| 137 | "Did not receive pkt on " + str(ofport)) |
| 138 | assert_if.assertEqual(str(pkt), str(rcv_pkt), |
| 139 | "Response packet does not match send packet " + |
| 140 | "on port " + str(ofport)) |
| 141 | |
| 142 | for ofport in no_ports: |
| 143 | logger.debug("Negative check for pkt on port " + str(ofport)) |
| 144 | (rcv_port, rcv_pkt, pkt_time) = dataplane.poll( |
| 145 | port_number=ofport, timeout=1) |
| 146 | assert_if.assertTrue(rcv_pkt is None, |
| 147 | "Unexpected pkt on port " + str(ofport)) |