forked from floodlight/oftest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic.py
549 lines (461 loc) · 19.5 KB
/
basic.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# Distributed under the OpenFlow Software License (see LICENSE)
# Copyright (c) 2010 The Board of Trustees of The Leland Stanford Junior University
# Copyright (c) 2012, 2013 Big Switch Networks, Inc.
# Copyright (c) 2012, 2013 CPqD
# Copyright (c) 2012, 2013 Ericsson
"""
Basic test cases
Test cases in other modules depend on this functionality.
"""
import logging
from oftest import config
import oftest.base_tests as base_tests
import ofp
from oftest.testutils import *
@group('smoke')
class Echo(base_tests.SimpleProtocol):
"""
Test echo response with no data
"""
def runTest(self):
request = ofp.message.echo_request()
response, pkt = self.controller.transact(request)
self.assertTrue(response is not None,
"Did not get echo reply")
self.assertEqual(response.type, ofp.OFPT_ECHO_REPLY,
'response is not echo_reply')
self.assertEqual(request.xid, response.xid,
'response xid != request xid')
self.assertEqual(len(response.data), 0, 'response data non-empty')
class EchoWithData(base_tests.SimpleProtocol):
"""
Test echo response with short string data
"""
def runTest(self):
data = 'OpenFlow Will Rule The World'
request = ofp.message.echo_request(data=data)
response, _ = self.controller.transact(request)
self.assertTrue(response is not None,
"Did not get echo reply")
self.assertEqual(response.type, ofp.OFPT_ECHO_REPLY,
'response is not echo_reply')
self.assertEqual(request.xid, response.xid,
'response xid != request xid')
self.assertEqual(request.data, response.data,
'response data != request data')
class FeaturesRequest(base_tests.SimpleProtocol):
"""
Test features_request to make sure we get a response
Does NOT test the contents; just that we get a response
"""
def runTest(self):
request = ofp.message.features_request()
response,_ = self.controller.transact(request)
self.assertTrue(response is not None,
'Did not get features reply')
class OutputExact(base_tests.SimpleDataPlane):
"""
Test output function for an exact-match flow
For each port A, adds a flow directing matching packets to that port.
Then, for all other ports B != A, verifies that sending a matching packet
to B results in an output to A.
"""
def runTest(self):
ports = sorted(config["port_map"].keys())
delete_all_flows(self.controller)
parsed_pkt = simple_tcp_packet()
pkt = str(parsed_pkt)
match = packet_to_flow_match(self, parsed_pkt)
for out_port in ports:
request = ofp.message.flow_add(
table_id=0,
cookie=42,
match=match,
instructions=[
ofp.instruction.apply_actions(
actions=[
ofp.action.output(
port=out_port,
max_len=ofp.OFPCML_NO_BUFFER)])],
buffer_id=ofp.OFP_NO_BUFFER,
priority=1000)
logging.info("Inserting flow sending matching packets to port %d", out_port)
self.controller.message_send(request)
do_barrier(self.controller)
for in_port in ports:
if in_port == out_port:
continue
logging.info("OutputExact test, ports %d to %d", in_port, out_port)
self.dataplane.send(in_port, pkt)
receive_pkt_verify(self, [out_port], pkt, in_port)
class OutputWildcard(base_tests.SimpleDataPlane):
"""
Test output function for a match-all (but not table-miss) flow
For each port A, adds a flow directing all packets to that port.
Then, for all other ports B != A, verifies that sending a packet
to B results in an output to A.
"""
def runTest(self):
ports = sorted(config["port_map"].keys())
delete_all_flows(self.controller)
pkt = str(simple_tcp_packet())
for out_port in ports:
request = ofp.message.flow_add(
table_id=0,
cookie=42,
instructions=[
ofp.instruction.apply_actions(
actions=[
ofp.action.output(
port=out_port,
max_len=ofp.OFPCML_NO_BUFFER)])],
buffer_id=ofp.OFP_NO_BUFFER,
priority=1000)
logging.info("Inserting flow sending all packets to port %d", out_port)
self.controller.message_send(request)
do_barrier(self.controller)
for in_port in ports:
if in_port == out_port:
continue
logging.info("OutputWildcard test, ports %d to %d", in_port, out_port)
self.dataplane.send(in_port, pkt)
receive_pkt_verify(self, [out_port], pkt, in_port)
class PacketInExact(base_tests.SimpleDataPlane):
"""
Test packet in function for an exact-match flow
Send a packet to each dataplane port and verify that a packet
in message is received from the controller for each
"""
def runTest(self):
delete_all_flows(self.controller)
parsed_pkt = simple_tcp_packet()
pkt = str(parsed_pkt)
match = packet_to_flow_match(self, parsed_pkt)
request = ofp.message.flow_add(
table_id=0,
cookie=42,
match=match,
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=1000)
logging.info("Inserting flow sending matching packets to controller")
self.controller.message_send(request)
do_barrier(self.controller)
for of_port in config["port_map"].keys():
logging.info("PacketInExact test, port %d", of_port)
self.dataplane.send(of_port, pkt)
verify_packet_in(self, pkt, of_port, ofp.OFPR_ACTION)
class PacketInWildcard(base_tests.SimpleDataPlane):
"""
Test packet in function for a match-all flow
Send a packet to each dataplane port and verify that a packet
in message is received from the controller for each
"""
def runTest(self):
delete_all_flows(self.controller)
pkt = str(simple_tcp_packet())
request = ofp.message.flow_add(
table_id=0,
cookie=42,
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=1000)
logging.info("Inserting flow sending all packets to controller")
self.controller.message_send(request)
do_barrier(self.controller)
for of_port in config["port_map"].keys():
logging.info("PacketInWildcard test, port %d", of_port)
self.dataplane.send(of_port, pkt)
verify_packet_in(self, pkt, of_port, ofp.OFPR_ACTION)
class PacketInMiss(base_tests.SimpleDataPlane):
"""
Test packet in function for a table-miss flow
Send a packet to each dataplane port and verify that a packet
in message is received from the controller for each
"""
def runTest(self):
delete_all_flows(self.controller)
parsed_pkt = simple_tcp_packet()
pkt = str(parsed_pkt)
request = ofp.message.flow_add(
table_id=0,
cookie=42,
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)
logging.info("Inserting table-miss flow sending all packets to controller")
self.controller.message_send(request)
do_barrier(self.controller)
for of_port in config["port_map"].keys():
logging.info("PacketInMiss test, port %d", of_port)
self.dataplane.send(of_port, pkt)
verify_packet_in(self, pkt, of_port, ofp.OFPR_NO_MATCH)
class PacketOut(base_tests.SimpleDataPlane):
"""
Test packet out function
Send packet out message to controller for each dataplane port and
verify the packet appears on the appropriate dataplane port
"""
def runTest(self):
pkt = str(simple_tcp_packet())
for of_port in config["port_map"].keys():
msg = ofp.message.packet_out(
in_port=ofp.OFPP_CONTROLLER,
actions=[ofp.action.output(port=of_port)],
buffer_id=ofp.OFP_NO_BUFFER,
data=pkt)
logging.info("PacketOut test, port %d", of_port)
self.controller.message_send(msg)
receive_pkt_verify(self, [of_port], pkt, ofp.OFPP_CONTROLLER)
class FlowRemoveAll(base_tests.SimpleProtocol):
"""
Remove all flows; required for almost all tests
Add a bunch of flows, remove them, and then make sure there are no flows left
This is an intentionally naive test to see if the baseline functionality works
and should be a precondition to any more complicated deletion test (e.g.,
delete_strict vs. delete)
"""
def runTest(self):
for i in range(1,5):
logging.debug("Adding flow %d", i)
request = ofp.message.flow_add(
buffer_id=ofp.OFP_NO_BUFFER,
priority=i*1000)
self.controller.message_send(request)
do_barrier(self.controller)
delete_all_flows(self.controller)
logging.info("Sending flow stats request")
stats = get_flow_stats(self, ofp.match())
self.assertEqual(len(stats), 0, "Expected empty flow stats reply")
## Multipart messages
class DescStats(base_tests.SimpleProtocol):
"""
Switch description multipart transaction
Only verifies we get a single reply.
"""
def runTest(self):
request = ofp.message.desc_stats_request()
logging.info("Sending desc stats request")
response, _ = self.controller.transact(request)
self.assertTrue(response != None, "No response to desc stats request")
logging.info(response.show())
self.assertEquals(response.flags, 0, "Unexpected bit set in desc stats reply flags")
class FlowStats(base_tests.SimpleProtocol):
"""
Flow stats multipart transaction
Only verifies we get a reply.
"""
def runTest(self):
logging.info("Sending flow stats request")
stats = get_flow_stats(self, ofp.match())
logging.info("Received %d flow stats entries", len(stats))
for entry in stats:
logging.info(entry.show())
class AggregateStats(base_tests.SimpleProtocol):
"""
Aggregate flow stats multipart transaction
Only verifies we get a single reply.
"""
def runTest(self):
request = ofp.message.aggregate_stats_request(
table_id=ofp.OFPTT_ALL,
out_port=ofp.OFPP_ANY,
out_group=ofp.OFPG_ANY,
cookie=0,
cookie_mask=0)
logging.info("Sending aggregate flow stats request")
response, _ = self.controller.transact(request)
self.assertTrue(response != None, "No response to aggregate stats request")
logging.info(response.show())
self.assertEquals(response.flags, 0, "Unexpected bit set in aggregate stats reply flags")
class TableStats(base_tests.SimpleProtocol):
"""
Table stats multipart transaction
Only verifies we get a reply.
"""
def runTest(self):
logging.info("Sending table stats request")
stats = get_stats(self, ofp.message.table_stats_request())
logging.info("Received %d table stats entries", len(stats))
for entry in stats:
logging.info(entry.show())
class PortStats(base_tests.SimpleProtocol):
"""
Port stats multipart transaction
Only verifies we get a reply.
"""
def runTest(self):
request = ofp.message.port_stats_request(port_no=ofp.OFPP_ANY)
logging.info("Sending port stats request")
stats = get_stats(self, request)
logging.info("Received %d port stats entries", len(stats))
for entry in stats:
logging.info(entry.show())
class QueueStats(base_tests.SimpleProtocol):
"""
Queue stats multipart transaction
Only verifies we get a reply.
"""
def runTest(self):
request = ofp.message.queue_stats_request(port_no=ofp.OFPP_ANY,
queue_id=ofp.OFPQ_ALL)
logging.info("Sending queue stats request")
stats = get_stats(self, request)
logging.info("Received %d queue stats entries", len(stats))
for entry in stats:
logging.info(entry.show())
class GroupStats(base_tests.SimpleProtocol):
"""
Group stats multipart transaction
Only verifies we get a reply.
"""
def runTest(self):
request = ofp.message.group_stats_request(group_id=ofp.OFPG_ALL)
logging.info("Sending group stats request")
stats = get_stats(self, request)
logging.info("Received %d group stats entries", len(stats))
for entry in stats:
logging.info(entry.show())
class GroupDescStats(base_tests.SimpleProtocol):
"""
Group description multipart transaction
Only verifies we get a reply.
"""
def runTest(self):
request = ofp.message.group_desc_stats_request()
logging.info("Sending group desc stats request")
stats = get_stats(self, request)
logging.info("Received %d group desc stats entries", len(stats))
for entry in stats:
logging.info(entry.show())
class GroupFeaturesStats(base_tests.SimpleProtocol):
"""
Group features multipart transaction
Only verifies we get a single reply.
"""
def runTest(self):
request = ofp.message.group_features_stats_request()
logging.info("Sending group features stats request")
response, _ = self.controller.transact(request)
self.assertTrue(response != None, "No response to group features stats request")
logging.info(response.show())
self.assertEquals(response.flags, 0, "Unexpected bit set in group features stats reply flags")
class MeterStats(base_tests.SimpleProtocol):
"""
Meter stats multipart transaction
Only verifies we get a reply.
"""
def runTest(self):
request = ofp.message.meter_stats_request(meter_id=ofp.OFPM_ALL)
logging.info("Sending meter stats request")
stats = get_stats(self, request)
logging.info("Received %d meter stats entries", len(stats))
for entry in stats:
logging.info(entry.show())
class MeterConfigStats(base_tests.SimpleProtocol):
"""
Meter config multipart transaction
Only verifies we get a reply.
"""
def runTest(self):
request = ofp.message.meter_config_stats_request(meter_id=ofp.OFPM_ALL)
logging.info("Sending meter config stats request")
stats = get_stats(self, request)
logging.info("Received %d meter config stats entries", len(stats))
for entry in stats:
logging.info(entry.show())
class MeterFeaturesStats(base_tests.SimpleProtocol):
"""
Meter features multipart transaction
Only verifies we get a single reply.
"""
def runTest(self):
request = ofp.message.meter_features_stats_request()
logging.info("Sending meter features stats request")
response, _ = self.controller.transact(request)
self.assertTrue(response != None, "No response to meter features stats request")
logging.info(response.show())
self.assertEquals(response.flags, 0, "Unexpected bit set in meter features stats reply flags")
@disabled # pyloxi does not yet support table features
class TableFeaturesStats(base_tests.SimpleProtocol):
"""
Table features multipart transaction
Only verifies we get a reply.
"""
def runTest(self):
logging.info("Sending table features stats request")
stats = get_stats(self, ofp.message.table_features_stats_request())
logging.info("Received %d table features stats entries", len(stats))
for entry in stats:
logging.info(entry.show())
class PortDescStats(base_tests.SimpleProtocol):
"""
Port description multipart transaction
Only verifies we get a reply.
"""
def runTest(self):
logging.info("Sending port desc stats request")
stats = get_stats(self, ofp.message.port_desc_stats_request())
logging.info("Received %d port desc stats entries", len(stats))
for entry in stats:
logging.info(entry.show())
class PortConfigMod(base_tests.SimpleProtocol):
"""
Modify a bit in port config and verify changed
Get the switch configuration, modify the port configuration
and write it back; get the config again and verify changed.
Then set it back to the way it was.
"""
def runTest(self):
logging.info("Running " + str(self))
for of_port, _ in config["port_map"].items(): # Grab first port
break
(_, config1, _) = \
port_config_get(self.controller, of_port)
self.assertTrue(config is not None, "Did not get port config")
logging.debug("OFPPC_NO_PACKET_IN bit port " + str(of_port) + " is now " +
str(config1 & ofp.OFPPC_NO_PACKET_IN))
rv = port_config_set(self.controller, of_port,
config1 ^ ofp.OFPPC_NO_PACKET_IN,
ofp.OFPPC_NO_PACKET_IN)
self.assertTrue(rv != -1, "Error sending port mod")
# Verify change took place with same feature request
(_, config2, _) = port_config_get(self.controller, of_port)
self.assertTrue(config2 is not None, "Did not get port config2")
logging.debug("OFPPC_NO_PACKET_IN bit port " + str(of_port) + " is now " +
str(config2 & ofp.OFPPC_NO_PACKET_IN))
self.assertTrue(config2 & ofp.OFPPC_NO_PACKET_IN !=
config1 & ofp.OFPPC_NO_PACKET_IN,
"Bit change did not take")
# Set it back
rv = port_config_set(self.controller, of_port, config1,
ofp.OFPPC_NO_PACKET_IN)
self.assertTrue(rv != -1, "Error sending port mod")
class AsyncConfigGet(base_tests.SimpleProtocol):
"""
Verify initial async config
Other tests rely on connections starting with these values.
"""
def runTest(self):
logging.info("Sending get async config request")
response, _ = self.controller.transact(ofp.message.async_get_request())
self.assertTrue(response != None, "No response to get async config request")
logging.info(response.show())
self.assertEquals(response.packet_in_mask_equal_master & 0x07, 0x07)
self.assertEquals(response.port_status_mask_equal_master & 0x07, 0x07)
self.assertEquals(response.flow_removed_mask_equal_master & 0x0f, 0x0f)