forked from floodlight/oftest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole_request.py
363 lines (291 loc) · 13.3 KB
/
role_request.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
"""
Test the role request message
"""
import struct
import unittest
import logging
import oftest
from oftest import config
import oftest.controller as controller
import ofp
import oftest.base_tests as base_tests
from oftest.testutils import *
def add_mod64(a, b):
return (a + b) & (2**64-1)
def simple_role_request(test, role, gen=None, con=None):
"""
Send a role request we expect to succeed
"""
if con == None:
con = test.controller
request = ofp.message.role_request(role=role, generation_id=gen)
response, _ = con.transact(request)
test.assertTrue(isinstance(response, ofp.message.role_reply), "Expected a role reply")
if role != ofp.OFPCR_ROLE_NOCHANGE:
test.assertEquals(response.role, role)
if gen != None:
test.assertEquals(response.generation_id, gen)
return response.role, response.generation_id
def failed_role_request(test, role, gen, code, con=None):
"""
Send a role request we expect to fail
"""
if con == None:
con = test.controller
request = ofp.message.role_request(role=role, generation_id=gen)
response, _ = con.transact(request)
test.assertIsInstance(response, ofp.message.role_request_failed_error_msg)
test.assertEqual(response.code, code)
class RoleRequestNochange(base_tests.SimpleDataPlane):
"""
Verify that we can query the switch for our current role and generation ID
The role should default to OFPCR_ROLE_EQUAL.
"""
def runTest(self):
role, gen = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE)
self.assertEqual(role, ofp.OFPCR_ROLE_EQUAL)
# Make sure the generation ID is still the same
role, new_gen = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE)
self.assertEqual(role, ofp.OFPCR_ROLE_EQUAL)
self.assertEqual(new_gen, gen)
class RoleRequestEqualToSlave(base_tests.SimpleDataPlane):
"""
Transition between equal and slave roles and back
"""
def runTest(self):
role, gen0 = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE)
self.assertEqual(role, ofp.OFPCR_ROLE_EQUAL)
# Unchanged generation ID
role, gen1 = simple_role_request(self, ofp.OFPCR_ROLE_SLAVE, gen0)
# Back to equal
simple_role_request(self, ofp.OFPCR_ROLE_EQUAL)
# Smallest greater generation ID
role, gen2 = simple_role_request(self, ofp.OFPCR_ROLE_SLAVE, add_mod64(gen1, 1))
# Back to equal
simple_role_request(self, ofp.OFPCR_ROLE_EQUAL)
# Largest greater generation ID
role, gen3 = simple_role_request(self, ofp.OFPCR_ROLE_SLAVE,
add_mod64(gen2, 2**63-1))
# Back to equal
simple_role_request(self, ofp.OFPCR_ROLE_EQUAL)
# Send least stale generation ID
failed_role_request(self, ofp.OFPCR_ROLE_SLAVE,
add_mod64(gen3, -1),
ofp.OFPRRFC_STALE)
# Check that our role is unchanged
role, gen = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE)
self.assertEqual(role, ofp.OFPCR_ROLE_EQUAL)
self.assertEqual(gen, gen3)
# Send most stale generation ID
failed_role_request(self, ofp.OFPCR_ROLE_SLAVE,
add_mod64(gen3, -(2**63)),
ofp.OFPRRFC_STALE)
# Check that our role is unchanged
role, gen = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE)
self.assertEqual(role, ofp.OFPCR_ROLE_EQUAL)
self.assertEqual(gen, gen3)
class RoleRequestEqualToMaster(base_tests.SimpleDataPlane):
"""
Transition between equal and master roles and back
"""
def runTest(self):
role, gen0 = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE)
self.assertEqual(role, ofp.OFPCR_ROLE_EQUAL)
# Unchanged generation ID
role, gen1 = simple_role_request(self, ofp.OFPCR_ROLE_MASTER, gen0)
# Back to equal
simple_role_request(self, ofp.OFPCR_ROLE_EQUAL)
# Smallest greater generation ID
role, gen2 = simple_role_request(self, ofp.OFPCR_ROLE_MASTER, add_mod64(gen1, 1))
# Back to equal
simple_role_request(self, ofp.OFPCR_ROLE_EQUAL)
# Largest greater generation ID
role, gen3 = simple_role_request(self, ofp.OFPCR_ROLE_MASTER,
add_mod64(gen2, 2**63-1))
# Back to equal
simple_role_request(self, ofp.OFPCR_ROLE_EQUAL)
# Send least stale generation ID
failed_role_request(self, ofp.OFPCR_ROLE_MASTER,
add_mod64(gen3, -1),
ofp.OFPRRFC_STALE)
# Check that our role is unchanged
role, gen = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE)
self.assertEqual(role, ofp.OFPCR_ROLE_EQUAL)
self.assertEqual(gen, gen3)
# Send most stale generation ID
failed_role_request(self, ofp.OFPCR_ROLE_MASTER,
add_mod64(gen3, -(2**63)),
ofp.OFPRRFC_STALE)
# Check that our role is unchanged
role, gen = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE)
self.assertEqual(role, ofp.OFPCR_ROLE_EQUAL)
self.assertEqual(gen, gen3)
class RoleRequestSlaveToMaster(base_tests.SimpleDataPlane):
"""
Transition between slave and master roles and back
"""
def runTest(self):
role, gen0 = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE)
self.assertEqual(role, ofp.OFPCR_ROLE_EQUAL)
# Initial transition to slave
role, gen1 = simple_role_request(self, ofp.OFPCR_ROLE_SLAVE, gen0)
# Unchanged generation ID
role, gen2 = simple_role_request(self, ofp.OFPCR_ROLE_MASTER, gen1)
# Back to slave
simple_role_request(self, ofp.OFPCR_ROLE_SLAVE, gen2)
# Smallest greater generation ID
role, gen3 = simple_role_request(self, ofp.OFPCR_ROLE_MASTER,
add_mod64(gen2, 1))
# Back to slave
simple_role_request(self, ofp.OFPCR_ROLE_SLAVE, gen3)
# Largest greater generation ID
role, gen4 = simple_role_request(self, ofp.OFPCR_ROLE_MASTER,
add_mod64(gen3, 2**63-1))
# Back to slave
simple_role_request(self, ofp.OFPCR_ROLE_SLAVE, gen4)
# Send least stale generation ID
failed_role_request(self, ofp.OFPCR_ROLE_MASTER,
add_mod64(gen4, -1),
ofp.OFPRRFC_STALE)
# Check that our role is unchanged
role, gen = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE)
self.assertEqual(role, ofp.OFPCR_ROLE_SLAVE)
self.assertEqual(gen, gen4)
# Send most stale generation ID
failed_role_request(self, ofp.OFPCR_ROLE_MASTER,
add_mod64(gen4, -(2**63)),
ofp.OFPRRFC_STALE)
# Check that our role is unchanged
role, gen = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE)
self.assertEqual(role, ofp.OFPCR_ROLE_SLAVE)
self.assertEqual(gen, gen4)
class RolePermissions(base_tests.SimpleDataPlane):
"""
Verify that a slave connection cannot modify switch state, but
a master or equal can.
"""
def runTest(self):
delete_all_flows(self.controller)
self.verify_permission(True)
role, gen = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE)
simple_role_request(self, ofp.OFPCR_ROLE_MASTER, gen)
self.verify_permission(True)
simple_role_request(self, ofp.OFPCR_ROLE_SLAVE, gen)
self.verify_permission(False)
simple_role_request(self, ofp.OFPCR_ROLE_EQUAL)
self.verify_permission(True)
def verify_permission(self, perm):
self.controller.message_send(
ofp.message.packet_out(buffer_id=ofp.OFP_NO_BUFFER))
self.controller.message_send(
ofp.message.flow_delete(
buffer_id=ofp.OFP_NO_BUFFER,
out_port=ofp.OFPP_ANY,
out_group=ofp.OFPG_ANY))
self.controller.message_send(
ofp.message.group_mod(
command=ofp.OFPGC_DELETE,
group_id=ofp.OFPG_ALL))
# TODO OFPT_PORT_MOD
# TODO OFPT_TABLE_MOD
do_barrier(self.controller)
err_count = 0
while self.controller.packets:
msg = self.controller.packets.pop(0)[0]
if msg.type == ofp.OFPT_ERROR:
self.assertEquals(msg.err_type, ofp.OFPET_BAD_REQUEST)
self.assertEquals(msg.code, ofp.OFPBRC_IS_SLAVE)
err_count += 1
if perm:
self.assertEquals(err_count, 0, "Expected no errors")
else:
self.assertEquals(err_count, 3, "Expected errors for each message")
class SlaveNoPacketIn(base_tests.SimpleDataPlane):
"""
Verify that slave connections do not receive OFPT_PACKET_IN messages but other roles do.
"""
def runTest(self):
delete_all_flows(self.controller)
ingress_port, = openflow_ports(1)
pkt = str(simple_tcp_packet())
logging.info("Inserting table-miss flow sending all packets to controller")
request = ofp.message.flow_add(
table_id=test_param_get("table", 0),
instructions=[
ofp.instruction.apply_actions(
actions=[
ofp.action.output(
port=ofp.OFPP_CONTROLLER,
max_len=ofp.OFPCML_NO_BUFFER)])],
buffer_id=ofp.OFP_NO_BUFFER,
priority=0)
self.controller.message_send(request)
do_barrier(self.controller)
_, gen = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE)
simple_role_request(self, ofp.OFPCR_ROLE_MASTER, gen)
self.dataplane.send(ingress_port, pkt)
verify_packet_in(self, pkt, ingress_port, ofp.OFPR_NO_MATCH)
simple_role_request(self, ofp.OFPCR_ROLE_SLAVE, gen)
self.dataplane.send(ingress_port, pkt)
verify_no_packet_in(self, pkt, ingress_port)
simple_role_request(self, ofp.OFPCR_ROLE_EQUAL, gen)
self.dataplane.send(ingress_port, pkt)
verify_packet_in(self, pkt, ingress_port, ofp.OFPR_NO_MATCH)
@disabled
class RoleSwitch(unittest.TestCase):
"""
Verify that when a connection becomes a master the existing master is
downgraded to slave.
Requires the switch to attempt to connect in parallel to ports 6653
and 6753 on the configured IP.
"""
def setUp(self):
host = config["controller_host"]
self.controllers = [
controller.Controller(host=host,port=6653),
controller.Controller(host=host,port=6753)
]
def runTest(self):
# Connect and handshake with both controllers
for con in self.controllers:
con.start()
if not con.connect():
raise AssertionError("failed to connect controller %s" % str(con))
reply, _ = con.transact(ofp.message.features_request())
self.assertTrue(isinstance(reply, ofp.message.features_reply))
# Assert initial role and get generation IDs
role, gen0 = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE, con=self.controllers[0])
self.assertEqual(role, ofp.OFPCR_ROLE_EQUAL)
role, gen1 = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE, con=self.controllers[1])
self.assertEqual(role, ofp.OFPCR_ROLE_EQUAL)
# Initial role assignment: controller 0 is master, controller 1 is slave
simple_role_request(self, ofp.OFPCR_ROLE_MASTER, gen0, con=self.controllers[0])
simple_role_request(self, ofp.OFPCR_ROLE_SLAVE, gen1, con=self.controllers[1])
self.verify_role(self.controllers[0], ofp.OFPCR_ROLE_MASTER)
self.verify_role(self.controllers[1], ofp.OFPCR_ROLE_SLAVE)
# Controller 1 requests master
# Controller 0 becomes slave
simple_role_request(self, ofp.OFPCR_ROLE_MASTER, gen1, con=self.controllers[1])
self.verify_role(self.controllers[0], ofp.OFPCR_ROLE_SLAVE)
self.verify_role(self.controllers[1], ofp.OFPCR_ROLE_MASTER)
# Controller 0 requests master
# Controller 1 becomes slave
simple_role_request(self, ofp.OFPCR_ROLE_MASTER, gen0, con=self.controllers[0])
self.verify_role(self.controllers[0], ofp.OFPCR_ROLE_MASTER)
self.verify_role(self.controllers[1], ofp.OFPCR_ROLE_SLAVE)
# Controller 1 requests equal
# Controller 0 remains master
simple_role_request(self, ofp.OFPCR_ROLE_EQUAL, gen1, con=self.controllers[1])
self.verify_role(self.controllers[0], ofp.OFPCR_ROLE_MASTER)
self.verify_role(self.controllers[1], ofp.OFPCR_ROLE_EQUAL)
# Both controllers request slave
simple_role_request(self, ofp.OFPCR_ROLE_SLAVE, gen0, con=self.controllers[0])
simple_role_request(self, ofp.OFPCR_ROLE_SLAVE, gen1, con=self.controllers[1])
self.verify_role(self.controllers[0], ofp.OFPCR_ROLE_SLAVE)
self.verify_role(self.controllers[1], ofp.OFPCR_ROLE_SLAVE)
def verify_role(self, con, role):
rcv_role, _ = simple_role_request(self, ofp.OFPCR_ROLE_NOCHANGE, con=con)
self.assertEqual(rcv_role, role)
def tearDown(self):
for con in self.controllers:
con.shutdown()