blob: 20d8efcfb02f09af18756a68fd9d6f4d552ebaa2 [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
Ken Chiange097c6e2012-04-03 13:17:42 -070026#@var fe_config Local copy of global configuration data
27fe_config = None
Dan Talayco89d57342010-06-07 16:24:59 -070028
29def test_set_init(config):
30 """
31 Set up function for packet action test classes
32
33 @param config The configuration dictionary; see oft
34 """
35
Ed Swierk6ccbb072012-03-19 14:48:40 -070036 basic.test_set_init(config)
37
Ken Chiange097c6e2012-04-03 13:17:42 -070038 global fe_port_map
Ken Chiange097c6e2012-04-03 13:17:42 -070039 global fe_config
Dan Talayco89d57342010-06-07 16:24:59 -070040
Ken Chiange097c6e2012-04-03 13:17:42 -070041 fe_port_map = config["port_map"]
42 fe_config = config
Dan Talayco89d57342010-06-07 16:24:59 -070043
44class FlowExpire(basic.SimpleDataPlane):
45 """
46 Verify flow expire messages are properly generated.
47
48 Generate a packet
49 Generate and install a matching flow with idle timeout = 1 sec
50 Verify the flow expiration message is received
51 """
52 def runTest(self):
Ken Chiange097c6e2012-04-03 13:17:42 -070053 global fe_port_map
Dan Talayco89d57342010-06-07 16:24:59 -070054
Ken Chiange097c6e2012-04-03 13:17:42 -070055 # TODO: set from command-line parameter
56 test_timeout = 60
57
58 of_ports = fe_port_map.keys()
Dan Talayco89d57342010-06-07 16:24:59 -070059 of_ports.sort()
60 self.assertTrue(len(of_ports) > 1, "Not enough ports for test")
61
Rich Lane9a003812012-10-04 17:17:59 -070062 rc = delete_all_flows(self.controller)
Dan Talayco89d57342010-06-07 16:24:59 -070063 self.assertEqual(rc, 0, "Failed to delete all flows")
64
65 pkt = simple_tcp_packet()
Ed Swierk99a74de2012-08-22 06:40:54 -070066 match = packet_to_flow_match(self, pkt)
Dan Talayco89d57342010-06-07 16:24:59 -070067 match.wildcards &= ~ofp.OFPFW_IN_PORT
68 self.assertTrue(match is not None,
69 "Could not generate flow match from pkt")
70 act = action.action_output()
71
Ken Chiange097c6e2012-04-03 13:17:42 -070072 of_ports = fe_port_map.keys()
73 of_ports.sort()
74 self.assertTrue(len(of_ports) > 1, "Not enough ports for test")
75
76 ingress_port = of_ports[0]
77 egress_port = of_ports[1]
Rich Lane9a003812012-10-04 17:17:59 -070078 logging.info("Ingress " + str(ingress_port) +
Dan Talayco89d57342010-06-07 16:24:59 -070079 " to egress " + str(egress_port))
80
81 match.in_port = ingress_port
82
83 request = message.flow_mod()
84 request.match = match
85 request.cookie = random.randint(0,9007199254740992)
86 request.buffer_id = 0xffffffff
87 request.idle_timeout = 1
88 request.flags |= ofp.OFPFF_SEND_FLOW_REM
89 act.port = egress_port
90 self.assertTrue(request.actions.add(act), "Could not add action")
91
Rich Lane9a003812012-10-04 17:17:59 -070092 logging.info("Inserting flow")
Dan Talayco89d57342010-06-07 16:24:59 -070093 rv = self.controller.message_send(request)
94 self.assertTrue(rv != -1, "Error installing flow mod")
Dan Talayco0fc08bd2012-04-09 16:56:18 -070095 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
Dan Talayco89d57342010-06-07 16:24:59 -070096
Ken Chiange097c6e2012-04-03 13:17:42 -070097 (response, pkt) = self.controller.poll(exp_msg=ofp.OFPT_FLOW_REMOVED,
98 timeout=test_timeout)
99
Dan Talayco89d57342010-06-07 16:24:59 -0700100 self.assertTrue(response is not None,
101 'Did not receive flow removed message ')
102
103 self.assertEqual(request.cookie, response.cookie,
104 'Cookies do not match')
105
106 self.assertEqual(ofp.OFPRR_IDLE_TIMEOUT, response.reason,
107 'Flow table entry removal reason is not idle_timeout')
108
109 self.assertEqual(match, response.match,
110 'Flow table entry does not match')
111