Matteo Scandolo | a229eca | 2017-08-08 13:05:28 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Shudong Zhou | dc276ae | 2012-10-15 16:02:56 -0700 | [diff] [blame] | 17 | """ |
Rich Lane | 5252a51 | 2013-04-04 13:19:22 -0700 | [diff] [blame] | 18 | Test the Nicira controller role extension |
Shudong Zhou | dc276ae | 2012-10-15 16:02:56 -0700 | [diff] [blame] | 19 | """ |
| 20 | import struct |
Rich Lane | cf5dbde | 2013-03-28 13:53:05 -0700 | [diff] [blame] | 21 | import unittest |
Shudong Zhou | dc276ae | 2012-10-15 16:02:56 -0700 | [diff] [blame] | 22 | import logging |
| 23 | |
Rich Lane | 2c232ca | 2013-03-28 16:53:09 -0700 | [diff] [blame] | 24 | import oftest |
Shudong Zhou | dc276ae | 2012-10-15 16:02:56 -0700 | [diff] [blame] | 25 | from oftest import config |
| 26 | import oftest.controller as controller |
Rich Lane | d7b0ffa | 2013-03-08 15:53:42 -0800 | [diff] [blame] | 27 | import ofp |
Shudong Zhou | dc276ae | 2012-10-15 16:02:56 -0700 | [diff] [blame] | 28 | import oftest.base_tests as base_tests |
| 29 | |
| 30 | from oftest.testutils import * |
| 31 | |
Rich Lane | cb816fd | 2013-03-26 21:53:47 -0700 | [diff] [blame] | 32 | NX_ROLE_OTHER = 0 |
| 33 | NX_ROLE_MASTER = 1 |
| 34 | NX_ROLE_SLAVE = 2 |
Shudong Zhou | dc276ae | 2012-10-15 16:02:56 -0700 | [diff] [blame] | 35 | |
Rich Lane | cf5dbde | 2013-03-28 13:53:05 -0700 | [diff] [blame] | 36 | def set_role(test, role, con=None): |
| 37 | if con == None: |
| 38 | con = test.controller |
Rich Lane | f41eea6 | 2013-03-27 16:05:43 -0700 | [diff] [blame] | 39 | request = ofp.message.nicira_controller_role_request(role=role) |
Rich Lane | cf5dbde | 2013-03-28 13:53:05 -0700 | [diff] [blame] | 40 | response, _ = con.transact(request) |
Rich Lane | f41eea6 | 2013-03-27 16:05:43 -0700 | [diff] [blame] | 41 | test.assertTrue(isinstance(response, ofp.message.nicira_controller_role_reply), "Expected a role reply") |
| 42 | test.assertEquals(response.role, role) |
| 43 | |
Rich Lane | cb816fd | 2013-03-26 21:53:47 -0700 | [diff] [blame] | 44 | class AnyReply(base_tests.SimpleDataPlane): |
Shudong Zhou | dc276ae | 2012-10-15 16:02:56 -0700 | [diff] [blame] | 45 | """ |
Rich Lane | cb816fd | 2013-03-26 21:53:47 -0700 | [diff] [blame] | 46 | Verify that a role request gets either a role reply or an error. |
Shudong Zhou | dc276ae | 2012-10-15 16:02:56 -0700 | [diff] [blame] | 47 | |
Rich Lane | cb816fd | 2013-03-26 21:53:47 -0700 | [diff] [blame] | 48 | This test should pass on any switch, no matter whether it implements |
| 49 | the extension. |
| 50 | """ |
Shudong Zhou | dc276ae | 2012-10-15 16:02:56 -0700 | [diff] [blame] | 51 | def runTest(self): |
Rich Lane | 4b60145 | 2013-03-11 23:37:06 -0700 | [diff] [blame] | 52 | request = ofp.message.nicira_controller_role_request(role=NX_ROLE_MASTER) |
Shudong Zhou | dc276ae | 2012-10-15 16:02:56 -0700 | [diff] [blame] | 53 | response, pkt = self.controller.transact(request) |
| 54 | self.assertTrue(response is not None, "No reply to Nicira role request") |
Rich Lane | cb816fd | 2013-03-26 21:53:47 -0700 | [diff] [blame] | 55 | if isinstance(response, ofp.message.nicira_controller_role_reply): |
| 56 | logging.info("Role reply received") |
| 57 | logging.info(response.show()) |
| 58 | self.assertEquals(response.role, NX_ROLE_MASTER) |
Rich Lane | e226f04 | 2013-10-21 06:22:03 -0700 | [diff] [blame] | 59 | elif isinstance(response, ofp.message.bad_request_error_msg): |
Rich Lane | cb816fd | 2013-03-26 21:53:47 -0700 | [diff] [blame] | 60 | logging.info("Error message received") |
| 61 | logging.info(response.show()) |
Rich Lane | cb816fd | 2013-03-26 21:53:47 -0700 | [diff] [blame] | 62 | self.assertEquals(response.code, ofp.OFPBRC_BAD_VENDOR) |
| 63 | else: |
| 64 | raise AssertionError("Unexpected reply type") |
Rich Lane | f41eea6 | 2013-03-27 16:05:43 -0700 | [diff] [blame] | 65 | |
| 66 | @nonstandard |
| 67 | class RolePermissions(base_tests.SimpleDataPlane): |
| 68 | """ |
| 69 | Verify that a slave connection cannot modify switch state, but |
| 70 | a master or equal can. |
| 71 | """ |
| 72 | def runTest(self): |
| 73 | self.features_reply, _ = self.controller.transact(ofp.message.features_request()) |
| 74 | delete_all_flows(self.controller) |
| 75 | self.verify_permission(True) |
| 76 | |
| 77 | set_role(self, NX_ROLE_MASTER) |
| 78 | self.verify_permission(True) |
| 79 | |
| 80 | set_role(self, NX_ROLE_SLAVE) |
| 81 | self.verify_permission(False) |
| 82 | |
| 83 | set_role(self, NX_ROLE_OTHER) |
| 84 | self.verify_permission(True) |
| 85 | |
| 86 | def verify_permission(self, perm): |
| 87 | port = self.features_reply.ports[0] |
| 88 | |
| 89 | self.controller.message_send(ofp.message.port_mod(port_no=port.port_no, hw_addr=port.hw_addr)) |
| 90 | self.controller.message_send(ofp.message.packet_out(buffer_id=0xffffffff)) |
| 91 | self.controller.message_send(ofp.message.flow_add(buffer_id=0xffffffff)) |
| 92 | do_barrier(self.controller) |
| 93 | |
| 94 | err_count = 0 |
| 95 | while self.controller.packets: |
| 96 | msg = self.controller.packets.pop(0)[0] |
Rich Lane | e226f04 | 2013-10-21 06:22:03 -0700 | [diff] [blame] | 97 | if msg.type == ofp.OFPT_ERROR: |
Rich Lane | f41eea6 | 2013-03-27 16:05:43 -0700 | [diff] [blame] | 98 | self.assertEquals(msg.err_type, ofp.OFPET_BAD_REQUEST) |
| 99 | self.assertEquals(msg.code, ofp.OFPBRC_EPERM) |
| 100 | err_count += 1 |
| 101 | |
| 102 | if perm: |
| 103 | self.assertEquals(err_count, 0, "Expected no errors") |
| 104 | else: |
| 105 | self.assertEquals(err_count, 3, "Expected errors for each message") |
Rich Lane | 13c9159 | 2013-03-27 17:03:56 -0700 | [diff] [blame] | 106 | |
| 107 | @nonstandard |
| 108 | class SlaveNoPacketIn(base_tests.SimpleDataPlane): |
| 109 | """ |
| 110 | Verify that slave connections do not receive OFPT_PACKET_IN messages but other roles do. |
| 111 | """ |
| 112 | def runTest(self): |
| 113 | delete_all_flows(self.controller) |
Rich Lane | 4c504f3 | 2013-06-07 17:24:14 -0700 | [diff] [blame] | 114 | ingress_port = config["port_map"].keys()[0] |
| 115 | pkt = str(simple_tcp_packet()) |
Rich Lane | 13c9159 | 2013-03-27 17:03:56 -0700 | [diff] [blame] | 116 | |
| 117 | set_role(self, NX_ROLE_MASTER) |
Rich Lane | 4c504f3 | 2013-06-07 17:24:14 -0700 | [diff] [blame] | 118 | self.dataplane.send(ingress_port, pkt) |
| 119 | verify_packet_in(self, pkt, ingress_port, ofp.OFPR_NO_MATCH) |
Rich Lane | 13c9159 | 2013-03-27 17:03:56 -0700 | [diff] [blame] | 120 | |
| 121 | set_role(self, NX_ROLE_SLAVE) |
Rich Lane | 4c504f3 | 2013-06-07 17:24:14 -0700 | [diff] [blame] | 122 | self.dataplane.send(ingress_port, pkt) |
| 123 | verify_no_packet_in(self, pkt, ingress_port) |
Rich Lane | 13c9159 | 2013-03-27 17:03:56 -0700 | [diff] [blame] | 124 | |
| 125 | set_role(self, NX_ROLE_OTHER) |
Rich Lane | 4c504f3 | 2013-06-07 17:24:14 -0700 | [diff] [blame] | 126 | self.dataplane.send(ingress_port, pkt) |
| 127 | verify_packet_in(self, pkt, ingress_port, ofp.OFPR_NO_MATCH) |
Rich Lane | cf5dbde | 2013-03-28 13:53:05 -0700 | [diff] [blame] | 128 | |
| 129 | @nonstandard |
| 130 | @disabled |
| 131 | class RoleSwitch(unittest.TestCase): |
| 132 | """ |
| 133 | Verify that when a connection becomes a master the existing master is |
| 134 | downgraded to slave. |
| 135 | |
| 136 | Requires the switch to attempt to connect in parallel to ports 6633 |
| 137 | and 6634 on the configured IP. |
| 138 | """ |
| 139 | |
| 140 | def setUp(self): |
| 141 | host = config["controller_host"] |
| 142 | self.controllers = [ |
| 143 | controller.Controller(host=host,port=6633), |
| 144 | controller.Controller(host=host,port=6634) |
| 145 | ] |
| 146 | |
| 147 | def runTest(self): |
| 148 | # Connect and handshake with both controllers |
| 149 | for con in self.controllers: |
| 150 | con.start() |
| 151 | if not con.connect(): |
| 152 | raise AssertionError("failed to connect controller %s" % str(con)) |
| 153 | reply, _ = con.transact(ofp.message.features_request()) |
| 154 | self.assertTrue(isinstance(reply, ofp.message.features_reply)) |
| 155 | |
| 156 | # Initial role assignment, controller 0 is master |
| 157 | set_role(self, NX_ROLE_MASTER, con=self.controllers[0]) |
| 158 | set_role(self, NX_ROLE_SLAVE, con=self.controllers[1]) |
| 159 | self.verify_role(self.controllers[0], True) |
| 160 | self.verify_role(self.controllers[1], False) |
| 161 | |
| 162 | # Controller 1 becomes master |
| 163 | set_role(self, NX_ROLE_MASTER, con=self.controllers[1]) |
| 164 | self.verify_role(self.controllers[0], False) |
| 165 | self.verify_role(self.controllers[1], True) |
| 166 | |
| 167 | # Controller 0 becomes master |
| 168 | set_role(self, NX_ROLE_MASTER, con=self.controllers[0]) |
| 169 | self.verify_role(self.controllers[0], True) |
| 170 | self.verify_role(self.controllers[1], False) |
| 171 | |
| 172 | # Controller 1 becomes equal |
| 173 | set_role(self, NX_ROLE_OTHER, con=self.controllers[1]) |
| 174 | self.verify_role(self.controllers[0], True) |
| 175 | self.verify_role(self.controllers[1], True) |
| 176 | |
| 177 | # Both controllers become slaves |
| 178 | set_role(self, NX_ROLE_SLAVE, con=self.controllers[0]) |
| 179 | set_role(self, NX_ROLE_SLAVE, con=self.controllers[1]) |
| 180 | self.verify_role(self.controllers[0], False) |
| 181 | self.verify_role(self.controllers[1], False) |
| 182 | |
| 183 | def verify_role(self, con, master): |
| 184 | con.message_send(ofp.message.flow_add(buffer_id=0xffffffff)) |
| 185 | do_barrier(con) |
| 186 | |
| 187 | err_count = 0 |
| 188 | while con.packets: |
| 189 | msg = con.packets.pop(0)[0] |
Rich Lane | e226f04 | 2013-10-21 06:22:03 -0700 | [diff] [blame] | 190 | if msg.type == ofp.OFPT_ERROR: |
Rich Lane | cf5dbde | 2013-03-28 13:53:05 -0700 | [diff] [blame] | 191 | self.assertEquals(msg.err_type, ofp.OFPET_BAD_REQUEST) |
| 192 | self.assertEquals(msg.code, ofp.OFPBRC_EPERM) |
| 193 | err_count += 1 |
| 194 | |
| 195 | if master: |
| 196 | self.assertEquals(err_count, 0, "Expected no errors") |
| 197 | else: |
| 198 | self.assertEquals(err_count, 1, "Expected errors for each message") |
| 199 | |
| 200 | def tearDown(self): |
| 201 | for con in self.controllers: |
| 202 | con.shutdown() |
Rich Lane | 2c232ca | 2013-03-28 16:53:09 -0700 | [diff] [blame] | 203 | |
| 204 | @nonstandard |
| 205 | @disabled |
| 206 | class EqualAsyncMessages(unittest.TestCase): |
| 207 | """ |
| 208 | Verify that 'equal' controllers all get async events. |
| 209 | |
| 210 | Requires the switch to attempt to connect in parallel to ports 6633 |
| 211 | and 6634 on the configured IP. |
| 212 | """ |
| 213 | |
| 214 | def setUp(self): |
| 215 | host = config["controller_host"] |
| 216 | self.controllers = [ |
| 217 | controller.Controller(host=host,port=6633), |
| 218 | controller.Controller(host=host,port=6634) |
| 219 | ] |
| 220 | self.dataplane = oftest.dataplane_instance |
| 221 | self.dataplane.flush() |
| 222 | |
| 223 | def runTest(self): |
| 224 | # Connect and handshake with both controllers |
| 225 | for con in self.controllers: |
| 226 | con.start() |
| 227 | if not con.connect(): |
| 228 | raise AssertionError("failed to connect controller %s" % str(con)) |
| 229 | reply, _ = con.transact(ofp.message.features_request()) |
| 230 | self.assertTrue(isinstance(reply, ofp.message.features_reply)) |
| 231 | |
| 232 | delete_all_flows(self.controllers[0]) |
| 233 | do_barrier(self.controllers[0]) |
| 234 | |
| 235 | pkt = str(simple_tcp_packet()) |
| 236 | ingress_port = config["port_map"].keys()[0] |
| 237 | self.dataplane.send(ingress_port, pkt) |
| 238 | |
| 239 | for con in self.controllers: |
Rich Lane | 4c504f3 | 2013-06-07 17:24:14 -0700 | [diff] [blame] | 240 | verify_packet_in(self, pkt, ingress_port, ofp.OFPR_NO_MATCH, controller=con) |
Rich Lane | 2c232ca | 2013-03-28 16:53:09 -0700 | [diff] [blame] | 241 | |
| 242 | def tearDown(self): |
| 243 | for con in self.controllers: |
| 244 | con.shutdown() |
| 245 | con.join() |
| 246 | del self.controllers |