blob: 9be5fdd5ddb5c66d0e8dba893c040b615840d207 [file] [log] [blame]
Dan Talayco89d57342010-06-07 16:24:59 -07001"""
2Flow expire test case.
3Similar to Flow expire test case in the perl test harness.
4
5"""
6
7import logging
8
9import unittest
10import random
11
12import oftest.controller as controller
13import oftest.cstruct as ofp
14import oftest.message as message
15import oftest.dataplane as dataplane
16import oftest.action as action
17import oftest.parse as parse
18import basic
19
Rich Laneda3b5ad2012-10-03 09:05:32 -070020from oftest.testutils import *
Dan Talayco89d57342010-06-07 16:24:59 -070021from time import sleep
22
23#@var port_map Local copy of the configuration map from OF port
24# numbers to OS interfaces
Ken Chiange097c6e2012-04-03 13:17:42 -070025fe_port_map = None
26#@var fe_logger Local logger object
27fe_logger = None
28#@var fe_config Local copy of global configuration data
29fe_config = None
Dan Talayco89d57342010-06-07 16:24:59 -070030
31def test_set_init(config):
32 """
33 Set up function for packet action test classes
34
35 @param config The configuration dictionary; see oft
36 """
37
Ed Swierk6ccbb072012-03-19 14:48:40 -070038 basic.test_set_init(config)
39
Ken Chiange097c6e2012-04-03 13:17:42 -070040 global fe_port_map
41 global fe_logger
42 global fe_config
Dan Talayco89d57342010-06-07 16:24:59 -070043
Ken Chiange097c6e2012-04-03 13:17:42 -070044 fe_logger = logging.getLogger("flow_expire")
45 fe_logger.info("Initializing test set")
46 fe_port_map = config["port_map"]
47 fe_config = config
Dan Talayco89d57342010-06-07 16:24:59 -070048
49class FlowExpire(basic.SimpleDataPlane):
50 """
51 Verify flow expire messages are properly generated.
52
53 Generate a packet
54 Generate and install a matching flow with idle timeout = 1 sec
55 Verify the flow expiration message is received
56 """
57 def runTest(self):
Ken Chiange097c6e2012-04-03 13:17:42 -070058 global fe_port_map
Dan Talayco89d57342010-06-07 16:24:59 -070059
Ken Chiange097c6e2012-04-03 13:17:42 -070060 # TODO: set from command-line parameter
61 test_timeout = 60
62
63 of_ports = fe_port_map.keys()
Dan Talayco89d57342010-06-07 16:24:59 -070064 of_ports.sort()
65 self.assertTrue(len(of_ports) > 1, "Not enough ports for test")
66
Ken Chiange097c6e2012-04-03 13:17:42 -070067 rc = delete_all_flows(self.controller, fe_logger)
Dan Talayco89d57342010-06-07 16:24:59 -070068 self.assertEqual(rc, 0, "Failed to delete all flows")
69
70 pkt = simple_tcp_packet()
Ed Swierk99a74de2012-08-22 06:40:54 -070071 match = packet_to_flow_match(self, pkt)
Dan Talayco89d57342010-06-07 16:24:59 -070072 match.wildcards &= ~ofp.OFPFW_IN_PORT
73 self.assertTrue(match is not None,
74 "Could not generate flow match from pkt")
75 act = action.action_output()
76
Ken Chiange097c6e2012-04-03 13:17:42 -070077 of_ports = fe_port_map.keys()
78 of_ports.sort()
79 self.assertTrue(len(of_ports) > 1, "Not enough ports for test")
80
81 ingress_port = of_ports[0]
82 egress_port = of_ports[1]
83 fe_logger.info("Ingress " + str(ingress_port) +
Dan Talayco89d57342010-06-07 16:24:59 -070084 " to egress " + str(egress_port))
85
86 match.in_port = ingress_port
87
88 request = message.flow_mod()
89 request.match = match
90 request.cookie = random.randint(0,9007199254740992)
91 request.buffer_id = 0xffffffff
92 request.idle_timeout = 1
93 request.flags |= ofp.OFPFF_SEND_FLOW_REM
94 act.port = egress_port
95 self.assertTrue(request.actions.add(act), "Could not add action")
96
Ken Chiange097c6e2012-04-03 13:17:42 -070097 fe_logger.info("Inserting flow")
Dan Talayco89d57342010-06-07 16:24:59 -070098 rv = self.controller.message_send(request)
99 self.assertTrue(rv != -1, "Error installing flow mod")
Dan Talayco0fc08bd2012-04-09 16:56:18 -0700100 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
Dan Talayco89d57342010-06-07 16:24:59 -0700101
Ken Chiange097c6e2012-04-03 13:17:42 -0700102 (response, pkt) = self.controller.poll(exp_msg=ofp.OFPT_FLOW_REMOVED,
103 timeout=test_timeout)
104
Dan Talayco89d57342010-06-07 16:24:59 -0700105 self.assertTrue(response is not None,
106 'Did not receive flow removed message ')
107
108 self.assertEqual(request.cookie, response.cookie,
109 'Cookies do not match')
110
111 self.assertEqual(ofp.OFPRR_IDLE_TIMEOUT, response.reason,
112 'Flow table entry removal reason is not idle_timeout')
113
114 self.assertEqual(match, response.match,
115 'Flow table entry does not match')
116