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