blob: 63c08532c9ec87ede13da7b23e88bd57c785801a [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
19import basic
20
Rich Laneda3b5ad2012-10-03 09:05:32 -070021from oftest.testutils import *
Dan Talayco89d57342010-06-07 16:24:59 -070022from time import sleep
23
Dan Talayco89d57342010-06-07 16:24:59 -070024class FlowExpire(basic.SimpleDataPlane):
25 """
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
68 self.assertTrue(request.actions.add(act), "Could not add action")
69
Rich Lane9a003812012-10-04 17:17:59 -070070 logging.info("Inserting flow")
Dan Talayco89d57342010-06-07 16:24:59 -070071 rv = self.controller.message_send(request)
72 self.assertTrue(rv != -1, "Error installing flow mod")
Dan Talayco0fc08bd2012-04-09 16:56:18 -070073 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
Dan Talayco89d57342010-06-07 16:24:59 -070074
Ken Chiange097c6e2012-04-03 13:17:42 -070075 (response, pkt) = self.controller.poll(exp_msg=ofp.OFPT_FLOW_REMOVED,
76 timeout=test_timeout)
77
Dan Talayco89d57342010-06-07 16:24:59 -070078 self.assertTrue(response is not None,
79 'Did not receive flow removed message ')
80
81 self.assertEqual(request.cookie, response.cookie,
82 'Cookies do not match')
83
84 self.assertEqual(ofp.OFPRR_IDLE_TIMEOUT, response.reason,
85 'Flow table entry removal reason is not idle_timeout')
86
87 self.assertEqual(match, response.match,
88 'Flow table entry does not match')
89