Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 1 | """ |
Dan Talayco | 79f3608 | 2010-03-11 16:53:53 -0800 | [diff] [blame] | 2 | Basic protocol and dataplane test cases |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 3 | |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 4 | It is recommended that these definitions be kept in their own |
| 5 | namespace as different groups of tests will likely define |
| 6 | similar identifiers. |
| 7 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 8 | Current Assumptions: |
| 9 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 10 | The switch is actively attempting to contact the controller at the address |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 11 | indicated in oftest.config. |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 12 | |
| 13 | """ |
| 14 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 15 | import time |
| 16 | import sys |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 17 | import logging |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 18 | |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 19 | import unittest |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 20 | import random |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 21 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 22 | from oftest import config |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 23 | import oftest.controller as controller |
| 24 | import oftest.cstruct as ofp |
| 25 | import oftest.message as message |
| 26 | import oftest.dataplane as dataplane |
| 27 | import oftest.action as action |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 28 | import oftest.base_tests as base_tests |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 29 | |
Dan Talayco | e605b1b | 2012-09-18 06:56:20 -0700 | [diff] [blame] | 30 | import oftest.illegal_message as illegal_message |
| 31 | |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 32 | from oftest.testutils import * |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 33 | |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 34 | TEST_VID_DEFAULT = 2 |
| 35 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 36 | class Echo(base_tests.SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 37 | """ |
| 38 | Test echo response with no data |
| 39 | """ |
| 40 | def runTest(self): |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 41 | request = message.echo_request() |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 42 | response, pkt = self.controller.transact(request) |
Dan Talayco | 97d4f36 | 2012-09-18 03:22:09 -0700 | [diff] [blame] | 43 | self.assertTrue(response is not None, |
| 44 | "Did not get echo reply") |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 45 | self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY, |
Dan Talayco | a92e75b | 2010-02-16 20:53:56 -0800 | [diff] [blame] | 46 | 'response is not echo_reply') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 47 | self.assertEqual(request.header.xid, response.header.xid, |
| 48 | 'response xid != request xid') |
| 49 | self.assertEqual(len(response.data), 0, 'response data non-empty') |
| 50 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 51 | class EchoWithData(base_tests.SimpleProtocol): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 52 | """ |
| 53 | Test echo response with short string data |
| 54 | """ |
| 55 | def runTest(self): |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 56 | request = message.echo_request() |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 57 | request.data = 'OpenFlow Will Rule The World' |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 58 | response, pkt = self.controller.transact(request) |
Dan Talayco | 97d4f36 | 2012-09-18 03:22:09 -0700 | [diff] [blame] | 59 | self.assertTrue(response is not None, |
| 60 | "Did not get echo reply (with data)") |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 61 | self.assertEqual(response.header.type, ofp.OFPT_ECHO_REPLY, |
Dan Talayco | a92e75b | 2010-02-16 20:53:56 -0800 | [diff] [blame] | 62 | 'response is not echo_reply') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 63 | self.assertEqual(request.header.xid, response.header.xid, |
| 64 | 'response xid != request xid') |
| 65 | self.assertEqual(request.data, response.data, |
| 66 | 'response data does not match request') |
| 67 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 68 | class PacketIn(base_tests.SimpleDataPlane): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 69 | """ |
| 70 | Test packet in function |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 71 | |
| 72 | Send a packet to each dataplane port and verify that a packet |
| 73 | in message is received from the controller for each |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 74 | """ |
| 75 | def runTest(self): |
| 76 | # Construct packet to send to dataplane |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 77 | # Send packet to dataplane, once to each port |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 78 | # Poll controller with expect message type packet in |
Dan Talayco | e226eb1 | 2010-02-18 23:06:30 -0800 | [diff] [blame] | 79 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 80 | rc = delete_all_flows(self.controller) |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 81 | self.assertEqual(rc, 0, "Failed to delete all flows") |
Dan Talayco | 0fc08bd | 2012-04-09 16:56:18 -0700 | [diff] [blame] | 82 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 83 | |
Rich Lane | 2014f9b | 2012-10-05 15:29:40 -0700 | [diff] [blame] | 84 | vid = test_param_get('vid', default=TEST_VID_DEFAULT) |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 85 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 86 | for of_port in config["port_map"].keys(): |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 87 | for pkt, pt in [ |
| 88 | (simple_tcp_packet(), "simple TCP packet"), |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 89 | (simple_tcp_packet(dl_vlan_enable=True,dl_vlan=vid,pktlen=108), |
| 90 | "simple tagged TCP packet"), |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 91 | (simple_eth_packet(), "simple Ethernet packet"), |
| 92 | (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]: |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 93 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 94 | logging.info("PKT IN test with %s, port %s" % (pt, of_port)) |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 95 | self.dataplane.send(of_port, str(pkt)) |
| 96 | #@todo Check for unexpected messages? |
| 97 | count = 0 |
| 98 | while True: |
Dan Talayco | c689a79 | 2012-09-28 14:22:53 -0700 | [diff] [blame] | 99 | (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN) |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 100 | if not response: # Timeout |
| 101 | break |
Ed Swierk | 506614a | 2012-03-29 08:16:59 -0700 | [diff] [blame] | 102 | if dataplane.match_exp_pkt(pkt, response.data): # Got match |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 103 | break |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 104 | if not config["relax"]: # Only one attempt to match |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 105 | break |
| 106 | count += 1 |
| 107 | if count > 10: # Too many tries |
| 108 | break |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 109 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 110 | self.assertTrue(response is not None, |
| 111 | 'Packet in message not received on port ' + |
| 112 | str(of_port)) |
Ed Swierk | 506614a | 2012-03-29 08:16:59 -0700 | [diff] [blame] | 113 | if not dataplane.match_exp_pkt(pkt, response.data): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 114 | logging.debug("Sent %s" % format_packet(pkt)) |
| 115 | logging.debug("Resp %s" % format_packet(response.data)) |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 116 | self.assertTrue(False, |
| 117 | 'Response packet does not match send packet' + |
| 118 | ' for port ' + str(of_port)) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 119 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 120 | class PacketInDefaultDrop(base_tests.SimpleDataPlane): |
Ed Swierk | 3ae7f71 | 2012-08-22 06:45:25 -0700 | [diff] [blame] | 121 | """ |
| 122 | Test packet in function |
| 123 | |
| 124 | Send a packet to each dataplane port and verify that a packet |
| 125 | in message is received from the controller for each |
| 126 | """ |
Rich Lane | d1d9c28 | 2012-10-04 22:07:10 -0700 | [diff] [blame] | 127 | |
| 128 | priority = -1 |
| 129 | |
Ed Swierk | 3ae7f71 | 2012-08-22 06:45:25 -0700 | [diff] [blame] | 130 | def runTest(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 131 | rc = delete_all_flows(self.controller) |
Ed Swierk | 3ae7f71 | 2012-08-22 06:45:25 -0700 | [diff] [blame] | 132 | self.assertEqual(rc, 0, "Failed to delete all flows") |
| 133 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
| 134 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 135 | for of_port in config["port_map"].keys(): |
Ed Swierk | 3ae7f71 | 2012-08-22 06:45:25 -0700 | [diff] [blame] | 136 | pkt = simple_tcp_packet() |
| 137 | self.dataplane.send(of_port, str(pkt)) |
| 138 | count = 0 |
| 139 | while True: |
Dan Talayco | c689a79 | 2012-09-28 14:22:53 -0700 | [diff] [blame] | 140 | (response, raw) = self.controller.poll(ofp.OFPT_PACKET_IN) |
Ed Swierk | 3ae7f71 | 2012-08-22 06:45:25 -0700 | [diff] [blame] | 141 | if not response: # Timeout |
| 142 | break |
| 143 | if dataplane.match_exp_pkt(pkt, response.data): # Got match |
| 144 | break |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 145 | if not config["relax"]: # Only one attempt to match |
Ed Swierk | 3ae7f71 | 2012-08-22 06:45:25 -0700 | [diff] [blame] | 146 | break |
| 147 | count += 1 |
| 148 | if count > 10: # Too many tries |
| 149 | break |
| 150 | |
| 151 | self.assertTrue(response is None, |
| 152 | 'Packet in message received on port ' + |
| 153 | str(of_port)) |
| 154 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 155 | class PacketInBroadcastCheck(base_tests.SimpleDataPlane): |
Dan Talayco | 1f648cb | 2012-05-03 09:37:56 -0700 | [diff] [blame] | 156 | """ |
| 157 | Check if bcast pkts leak when no flows are present |
| 158 | |
| 159 | Clear the flow table |
| 160 | Send in a broadcast pkt |
| 161 | Look for the packet on other dataplane ports. |
| 162 | """ |
Rich Lane | d1d9c28 | 2012-10-04 22:07:10 -0700 | [diff] [blame] | 163 | |
| 164 | priority = -1 |
| 165 | |
Dan Talayco | 1f648cb | 2012-05-03 09:37:56 -0700 | [diff] [blame] | 166 | def runTest(self): |
| 167 | # Need at least two ports |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 168 | self.assertTrue(len(config["port_map"]) > 1, "Too few ports for test") |
Dan Talayco | 1f648cb | 2012-05-03 09:37:56 -0700 | [diff] [blame] | 169 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 170 | rc = delete_all_flows(self.controller) |
Dan Talayco | 1f648cb | 2012-05-03 09:37:56 -0700 | [diff] [blame] | 171 | self.assertEqual(rc, 0, "Failed to delete all flows") |
| 172 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
| 173 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 174 | of_ports = config["port_map"].keys() |
Dan Talayco | 1f648cb | 2012-05-03 09:37:56 -0700 | [diff] [blame] | 175 | d_port = of_ports[0] |
| 176 | pkt = simple_eth_packet(dl_dst='ff:ff:ff:ff:ff:ff') |
| 177 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 178 | logging.info("BCast Leak Test, send to port %s" % d_port) |
Dan Talayco | 1f648cb | 2012-05-03 09:37:56 -0700 | [diff] [blame] | 179 | self.dataplane.send(d_port, str(pkt)) |
| 180 | |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 181 | (of_port, pkt_in, pkt_time) = self.dataplane.poll(exp_pkt=pkt) |
Dan Talayco | 1f648cb | 2012-05-03 09:37:56 -0700 | [diff] [blame] | 182 | self.assertTrue(pkt_in is None, |
| 183 | 'BCast packet received on port ' + str(of_port)) |
| 184 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 185 | class PacketOut(base_tests.SimpleDataPlane): |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 186 | """ |
| 187 | Test packet out function |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 188 | |
| 189 | Send packet out message to controller for each dataplane port and |
| 190 | verify the packet appears on the appropriate dataplane port |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 191 | """ |
| 192 | def runTest(self): |
| 193 | # Construct packet to send to dataplane |
| 194 | # Send packet to dataplane |
| 195 | # Poll controller with expect message type packet in |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 196 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 197 | rc = delete_all_flows(self.controller) |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 198 | self.assertEqual(rc, 0, "Failed to delete all flows") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 199 | |
| 200 | # These will get put into function |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 201 | of_ports = config["port_map"].keys() |
Dan Talayco | 4837010 | 2010-03-03 15:17:33 -0800 | [diff] [blame] | 202 | of_ports.sort() |
| 203 | for dp_port in of_ports: |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 204 | for outpkt, opt in [ |
| 205 | (simple_tcp_packet(), "simple TCP packet"), |
| 206 | (simple_eth_packet(), "simple Ethernet packet"), |
| 207 | (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]: |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 208 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 209 | logging.info("PKT OUT test with %s, port %s" % (opt, dp_port)) |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 210 | msg = message.packet_out() |
Rich Lane | afcf467 | 2012-11-14 13:19:27 -0800 | [diff] [blame] | 211 | msg.in_port = ofp.OFPP_NONE |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 212 | msg.data = str(outpkt) |
| 213 | act = action.action_output() |
| 214 | act.port = dp_port |
Rich Lane | e30455b | 2013-01-03 16:24:44 -0800 | [diff] [blame^] | 215 | msg.actions.add(act) |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 216 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 217 | logging.info("PacketOut to: " + str(dp_port)) |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 218 | rv = self.controller.message_send(msg) |
| 219 | self.assertTrue(rv == 0, "Error sending out message") |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 220 | |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 221 | exp_pkt_arg = None |
| 222 | exp_port = None |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 223 | if config["relax"]: |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 224 | exp_pkt_arg = outpkt |
| 225 | exp_port = dp_port |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 226 | (of_port, pkt, pkt_time) = self.dataplane.poll(port_number=exp_port, |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 227 | exp_pkt=exp_pkt_arg) |
| 228 | |
| 229 | self.assertTrue(pkt is not None, 'Packet not received') |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 230 | logging.info("PacketOut: got pkt from " + str(of_port)) |
Ed Swierk | 0aeff8c | 2012-03-23 20:27:18 -0700 | [diff] [blame] | 231 | if of_port is not None: |
| 232 | self.assertEqual(of_port, dp_port, "Unexpected receive port") |
Ed Swierk | 506614a | 2012-03-29 08:16:59 -0700 | [diff] [blame] | 233 | if not dataplane.match_exp_pkt(outpkt, pkt): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 234 | logging.debug("Sent %s" % format_packet(outpkt)) |
| 235 | logging.debug("Resp %s" % format_packet( |
Dan Talayco | 2baf8b5 | 2012-03-30 09:55:42 -0700 | [diff] [blame] | 236 | str(pkt)[:len(str(outpkt))])) |
Dan Talayco | dc6fca3 | 2012-03-30 10:05:49 -0700 | [diff] [blame] | 237 | self.assertEqual(str(outpkt), str(pkt)[:len(str(outpkt))], |
| 238 | 'Response packet does not match send packet') |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 239 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 240 | class PacketOutMC(base_tests.SimpleDataPlane): |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 241 | """ |
| 242 | Test packet out to multiple output ports |
| 243 | |
| 244 | Send packet out message to controller for 1 to N dataplane ports and |
| 245 | verify the packet appears on the appropriate ports |
| 246 | """ |
| 247 | def runTest(self): |
| 248 | # Construct packet to send to dataplane |
| 249 | # Send packet to dataplane |
| 250 | # Poll controller with expect message type packet in |
| 251 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 252 | rc = delete_all_flows(self.controller) |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 253 | self.assertEqual(rc, 0, "Failed to delete all flows") |
| 254 | |
| 255 | # These will get put into function |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 256 | of_ports = config["port_map"].keys() |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 257 | random.shuffle(of_ports) |
| 258 | for num_ports in range(1,len(of_ports)+1): |
| 259 | for outpkt, opt in [ |
| 260 | (simple_tcp_packet(), "simple TCP packet"), |
| 261 | (simple_eth_packet(), "simple Ethernet packet"), |
| 262 | (simple_eth_packet(pktlen=40), "tiny Ethernet packet")]: |
| 263 | |
| 264 | dp_ports = of_ports[0:num_ports] |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 265 | logging.info("PKT OUT test with " + opt + |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 266 | ", ports " + str(dp_ports)) |
| 267 | msg = message.packet_out() |
Rich Lane | afcf467 | 2012-11-14 13:19:27 -0800 | [diff] [blame] | 268 | msg.in_port = ofp.OFPP_NONE |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 269 | msg.data = str(outpkt) |
| 270 | act = action.action_output() |
| 271 | for i in range(0,num_ports): |
| 272 | act.port = dp_ports[i] |
Rich Lane | e30455b | 2013-01-03 16:24:44 -0800 | [diff] [blame^] | 273 | msg.actions.add(act) |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 274 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 275 | logging.info("PacketOut to: " + str(dp_ports)) |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 276 | rv = self.controller.message_send(msg) |
| 277 | self.assertTrue(rv == 0, "Error sending out message") |
| 278 | |
| 279 | receive_pkt_check(self.dataplane, outpkt, dp_ports, |
| 280 | set(of_ports).difference(dp_ports), |
Rich Lane | 2014f9b | 2012-10-05 15:29:40 -0700 | [diff] [blame] | 281 | self) |
Ken Chiang | 1bf0160 | 2012-04-04 10:48:23 -0700 | [diff] [blame] | 282 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 283 | class FlowStatsGet(base_tests.SimpleProtocol): |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 284 | """ |
| 285 | Get stats |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 286 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 287 | Simply verify stats get transaction |
| 288 | """ |
Rich Lane | d1d9c28 | 2012-10-04 22:07:10 -0700 | [diff] [blame] | 289 | |
| 290 | priority = -1 |
| 291 | |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 292 | def runTest(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 293 | logging.info("Running StatsGet") |
| 294 | logging.info("Inserting trial flow") |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 295 | request = flow_mod_gen(config["port_map"], True) |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 296 | rv = self.controller.message_send(request) |
| 297 | self.assertTrue(rv != -1, "Failed to insert test flow") |
| 298 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 299 | logging.info("Sending flow request") |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 300 | request = message.flow_stats_request() |
| 301 | request.out_port = ofp.OFPP_NONE |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 302 | request.table_id = 0xff |
| 303 | request.match.wildcards = 0 # ofp.OFPFW_ALL |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 304 | response, pkt = self.controller.transact(request) |
Dan Talayco | 97d4f36 | 2012-09-18 03:22:09 -0700 | [diff] [blame] | 305 | self.assertTrue(response is not None, |
| 306 | "Did not get response for flow stats") |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 307 | logging.debug(response.show()) |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 308 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 309 | class TableStatsGet(base_tests.SimpleProtocol): |
Dan Talayco | 79c6c4d | 2010-06-08 14:01:53 -0700 | [diff] [blame] | 310 | """ |
| 311 | Get table stats |
| 312 | |
| 313 | Simply verify table stats get transaction |
| 314 | """ |
| 315 | def runTest(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 316 | logging.info("Running TableStatsGet") |
| 317 | logging.info("Inserting trial flow") |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 318 | request = flow_mod_gen(config["port_map"], True) |
Dan Talayco | 79c6c4d | 2010-06-08 14:01:53 -0700 | [diff] [blame] | 319 | rv = self.controller.message_send(request) |
| 320 | self.assertTrue(rv != -1, "Failed to insert test flow") |
| 321 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 322 | logging.info("Sending table stats request") |
Dan Talayco | 79c6c4d | 2010-06-08 14:01:53 -0700 | [diff] [blame] | 323 | request = message.table_stats_request() |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 324 | response, pkt = self.controller.transact(request) |
Dan Talayco | 97d4f36 | 2012-09-18 03:22:09 -0700 | [diff] [blame] | 325 | self.assertTrue(response is not None, |
| 326 | "Did not get reply for table stats") |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 327 | logging.debug(response.show()) |
Dan Talayco | 79c6c4d | 2010-06-08 14:01:53 -0700 | [diff] [blame] | 328 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 329 | class DescStatsGet(base_tests.SimpleProtocol): |
Ed Swierk | ae74c36 | 2012-04-02 08:21:41 -0700 | [diff] [blame] | 330 | """ |
| 331 | Get stats |
| 332 | |
| 333 | Simply verify stats get transaction |
| 334 | """ |
| 335 | def runTest(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 336 | logging.info("Running DescStatsGet") |
Ed Swierk | ae74c36 | 2012-04-02 08:21:41 -0700 | [diff] [blame] | 337 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 338 | logging.info("Sending stats request") |
Ed Swierk | ae74c36 | 2012-04-02 08:21:41 -0700 | [diff] [blame] | 339 | request = message.desc_stats_request() |
Rich Lane | c8aaa3e | 2012-07-26 19:28:02 -0700 | [diff] [blame] | 340 | response, pkt = self.controller.transact(request) |
Dan Talayco | 97d4f36 | 2012-09-18 03:22:09 -0700 | [diff] [blame] | 341 | self.assertTrue(response is not None, |
| 342 | "Did not get reply for desc stats") |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 343 | logging.debug(response.show()) |
Ed Swierk | ae74c36 | 2012-04-02 08:21:41 -0700 | [diff] [blame] | 344 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 345 | class FlowMod(base_tests.SimpleProtocol): |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 346 | """ |
| 347 | Insert a flow |
| 348 | |
| 349 | Simple verification of a flow mod transaction |
| 350 | """ |
| 351 | |
| 352 | def runTest(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 353 | logging.info("Running " + str(self)) |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 354 | request = flow_mod_gen(config["port_map"], True) |
Dan Talayco | 6ce963a | 2010-03-07 21:58:13 -0800 | [diff] [blame] | 355 | rv = self.controller.message_send(request) |
Dan Talayco | 41eae8b | 2010-03-10 13:57:06 -0800 | [diff] [blame] | 356 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 357 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 358 | class PortConfigMod(base_tests.SimpleProtocol): |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 359 | """ |
| 360 | Modify a bit in port config and verify changed |
| 361 | |
| 362 | Get the switch configuration, modify the port configuration |
| 363 | and write it back; get the config again and verify changed. |
| 364 | Then set it back to the way it was. |
| 365 | """ |
| 366 | |
| 367 | def runTest(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 368 | logging.info("Running " + str(self)) |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 369 | for of_port, ifname in config["port_map"].items(): # Grab first port |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 370 | break |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 371 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 372 | (hw_addr, port_config, advert) = \ |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 373 | port_config_get(self.controller, of_port) |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 374 | self.assertTrue(port_config is not None, "Did not get port config") |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 375 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 376 | logging.debug("No flood bit port " + str(of_port) + " is now " + |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 377 | str(port_config & ofp.OFPPC_NO_FLOOD)) |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 378 | |
| 379 | rv = port_config_set(self.controller, of_port, |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 380 | port_config ^ ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD) |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 381 | self.assertTrue(rv != -1, "Error sending port mod") |
Rich Lane | bbd4cf9 | 2012-10-11 23:44:42 -0700 | [diff] [blame] | 382 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 383 | |
| 384 | # Verify change took place with same feature request |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 385 | (hw_addr, port_config2, advert) = \ |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 386 | port_config_get(self.controller, of_port) |
| 387 | logging.debug("No flood bit port " + str(of_port) + " is now " + |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 388 | str(port_config2 & ofp.OFPPC_NO_FLOOD)) |
| 389 | self.assertTrue(port_config2 is not None, "Did not get port config2") |
| 390 | self.assertTrue(port_config2 & ofp.OFPPC_NO_FLOOD != |
| 391 | port_config & ofp.OFPPC_NO_FLOOD, |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 392 | "Bit change did not take") |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 393 | # Set it back |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 394 | rv = port_config_set(self.controller, of_port, port_config, |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 395 | ofp.OFPPC_NO_FLOOD) |
Dan Talayco | 9f47f4d | 2010-06-03 13:54:37 -0700 | [diff] [blame] | 396 | self.assertTrue(rv != -1, "Error sending port mod") |
Rich Lane | bbd4cf9 | 2012-10-11 23:44:42 -0700 | [diff] [blame] | 397 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
Dan Talayco | b3f43fe | 2010-05-13 14:24:20 -0700 | [diff] [blame] | 398 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 399 | class PortConfigModErr(base_tests.SimpleProtocol): |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 400 | """ |
| 401 | Modify a bit in port config on an invalid port and verify |
| 402 | error message is received. |
| 403 | """ |
| 404 | |
| 405 | def runTest(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 406 | logging.info("Running " + str(self)) |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 407 | |
| 408 | # pick a random bad port number |
| 409 | bad_port = random.randint(1, ofp.OFPP_MAX) |
| 410 | count = 0 |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 411 | while (count < 50) and (bad_port in config["port_map"].keys()): |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 412 | bad_port = random.randint(1, ofp.OFPP_MAX) |
| 413 | count = count + 1 |
| 414 | self.assertTrue(count < 50, "Error selecting bad port") |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 415 | logging.info("Select " + str(bad_port) + " as invalid port") |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 416 | |
| 417 | rv = port_config_set(self.controller, bad_port, |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 418 | ofp.OFPPC_NO_FLOOD, ofp.OFPPC_NO_FLOOD) |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 419 | self.assertTrue(rv != -1, "Error sending port mod") |
| 420 | |
| 421 | # poll for error message |
| 422 | while True: |
Dan Talayco | c689a79 | 2012-09-28 14:22:53 -0700 | [diff] [blame] | 423 | (response, raw) = self.controller.poll(ofp.OFPT_ERROR) |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 424 | if not response: # Timeout |
| 425 | break |
| 426 | if response.code == ofp.OFPPMFC_BAD_PORT: |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 427 | logging.info("Received error message with OFPPMFC_BAD_PORT code") |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 428 | break |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 429 | if not config["relax"]: # Only one attempt to match |
Ken Chiang | aeb23d6 | 2012-08-23 21:20:07 -0700 | [diff] [blame] | 430 | break |
| 431 | count += 1 |
| 432 | if count > 10: # Too many tries |
| 433 | break |
| 434 | |
| 435 | self.assertTrue(response is not None, 'Did not receive error message') |
| 436 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 437 | class BadMessage(base_tests.SimpleProtocol): |
Dan Talayco | e605b1b | 2012-09-18 06:56:20 -0700 | [diff] [blame] | 438 | """ |
| 439 | Send a message with a bad type and verify an error is returned |
| 440 | """ |
| 441 | |
| 442 | def runTest(self): |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 443 | logging.info("Running " + str(self)) |
Dan Talayco | e605b1b | 2012-09-18 06:56:20 -0700 | [diff] [blame] | 444 | request = illegal_message.illegal_message_type() |
| 445 | |
Dan Talayco | c689a79 | 2012-09-28 14:22:53 -0700 | [diff] [blame] | 446 | reply, pkt = self.controller.transact(request) |
Dan Talayco | e605b1b | 2012-09-18 06:56:20 -0700 | [diff] [blame] | 447 | self.assertTrue(reply is not None, "Did not get response to bad req") |
| 448 | self.assertTrue(reply.header.type == ofp.OFPT_ERROR, |
| 449 | "reply not an error message") |
| 450 | self.assertTrue(reply.type == ofp.OFPET_BAD_REQUEST, |
| 451 | "reply error type is not bad request") |
| 452 | self.assertTrue(reply.code == ofp.OFPBRC_BAD_TYPE, |
| 453 | "reply error code is not bad type") |
| 454 | |
Dan Talayco | dba244e | 2010-02-15 14:08:53 -0800 | [diff] [blame] | 455 | if __name__ == "__main__": |
Dan Talayco | 2c0dba3 | 2010-03-06 22:47:06 -0800 | [diff] [blame] | 456 | print "Please run through oft script: ./oft --test_spec=basic" |