blob: 705cab07bf08114f44ec29d3e9fed9a0bb7d3172 [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
Rich Lane477f4812012-10-04 22:49:00 -070012from oftest import config
Dan Talayco89d57342010-06-07 16:24:59 -070013import oftest.controller as controller
14import oftest.cstruct as ofp
15import oftest.message as message
16import oftest.dataplane as dataplane
17import oftest.action as action
18import oftest.parse as parse
Rich Laneb90a1c42012-10-05 09:16:05 -070019import oftest.base_tests as base_tests
Dan Talayco89d57342010-06-07 16:24:59 -070020
Rich Laneda3b5ad2012-10-03 09:05:32 -070021from oftest.testutils import *
Dan Talayco89d57342010-06-07 16:24:59 -070022from time import sleep
23
Rich Laneb90a1c42012-10-05 09:16:05 -070024class FlowExpire(base_tests.SimpleDataPlane):
Dan Talayco89d57342010-06-07 16:24:59 -070025 """
26 Verify flow expire messages are properly generated.
27
28 Generate a packet
29 Generate and install a matching flow with idle timeout = 1 sec
30 Verify the flow expiration message is received
31 """
32 def runTest(self):
Ken Chiange097c6e2012-04-03 13:17:42 -070033 # TODO: set from command-line parameter
34 test_timeout = 60
35
Rich Lane477f4812012-10-04 22:49:00 -070036 of_ports = config["port_map"].keys()
Dan Talayco89d57342010-06-07 16:24:59 -070037 of_ports.sort()
38 self.assertTrue(len(of_ports) > 1, "Not enough ports for test")
39
Rich Lane9a003812012-10-04 17:17:59 -070040 rc = delete_all_flows(self.controller)
Dan Talayco89d57342010-06-07 16:24:59 -070041 self.assertEqual(rc, 0, "Failed to delete all flows")
42
43 pkt = simple_tcp_packet()
Ed Swierk99a74de2012-08-22 06:40:54 -070044 match = packet_to_flow_match(self, pkt)
Dan Talayco89d57342010-06-07 16:24:59 -070045 match.wildcards &= ~ofp.OFPFW_IN_PORT
46 self.assertTrue(match is not None,
47 "Could not generate flow match from pkt")
48 act = action.action_output()
49
Rich Lane477f4812012-10-04 22:49:00 -070050 of_ports = config["port_map"].keys()
Ken Chiange097c6e2012-04-03 13:17:42 -070051 of_ports.sort()
52 self.assertTrue(len(of_ports) > 1, "Not enough ports for test")
53
54 ingress_port = of_ports[0]
55 egress_port = of_ports[1]
Rich Lane9a003812012-10-04 17:17:59 -070056 logging.info("Ingress " + str(ingress_port) +
Dan Talayco89d57342010-06-07 16:24:59 -070057 " to egress " + str(egress_port))
58
59 match.in_port = ingress_port
60
61 request = message.flow_mod()
62 request.match = match
63 request.cookie = random.randint(0,9007199254740992)
64 request.buffer_id = 0xffffffff
65 request.idle_timeout = 1
66 request.flags |= ofp.OFPFF_SEND_FLOW_REM
67 act.port = egress_port
Rich Lanee30455b2013-01-03 16:24:44 -080068 request.actions.add(act)
Dan Talayco89d57342010-06-07 16:24:59 -070069
Rich Lane9a003812012-10-04 17:17:59 -070070 logging.info("Inserting flow")
Rich Lane5c3151c2013-01-03 17:15:41 -080071 self.controller.message_send(request)
Dan Talayco0fc08bd2012-04-09 16:56:18 -070072 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
Dan Talayco89d57342010-06-07 16:24:59 -070073
Ken Chiange097c6e2012-04-03 13:17:42 -070074 (response, pkt) = self.controller.poll(exp_msg=ofp.OFPT_FLOW_REMOVED,
75 timeout=test_timeout)
76
Dan Talayco89d57342010-06-07 16:24:59 -070077 self.assertTrue(response is not None,
78 'Did not receive flow removed message ')
79
80 self.assertEqual(request.cookie, response.cookie,
81 'Cookies do not match')
82
83 self.assertEqual(ofp.OFPRR_IDLE_TIMEOUT, response.reason,
84 'Flow table entry removal reason is not idle_timeout')
85
86 self.assertEqual(match, response.match,
87 'Flow table entry does not match')
88