blob: 55f8ebc0221b0b64e4026a0953e35ebfc24d4f3f [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
20from testutils import *
21from time import sleep
22
23#@var port_map Local copy of the configuration map from OF port
24# numbers to OS interfaces
25pa_port_map = None
26#@var pa_logger Local logger object
27pa_logger = None
28#@var pa_config Local copy of global configuration data
29pa_config = None
30
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
Dan Talayco89d57342010-06-07 16:24:59 -070040 global pa_port_map
41 global pa_logger
42 global pa_config
43
44 pa_logger = logging.getLogger("pkt_act")
45 pa_logger.info("Initializing test set")
46 pa_port_map = config["port_map"]
47 pa_config = config
48
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):
58 global pa_port_map
59
60 of_ports = pa_port_map.keys()
61 of_ports.sort()
62 self.assertTrue(len(of_ports) > 1, "Not enough ports for test")
63
64 rc = delete_all_flows(self.controller, pa_logger)
65 self.assertEqual(rc, 0, "Failed to delete all flows")
66
67 pkt = simple_tcp_packet()
68 match = parse.packet_to_flow_match(pkt)
69 match.wildcards &= ~ofp.OFPFW_IN_PORT
70 self.assertTrue(match is not None,
71 "Could not generate flow match from pkt")
72 act = action.action_output()
73
74 ingress_port = pa_config["base_of_port"]
75 egress_port = (pa_config["base_of_port"] + 1) % len(of_ports)
76 pa_logger.info("Ingress " + str(ingress_port) +
77 " to egress " + str(egress_port))
78
79 match.in_port = ingress_port
80
81 request = message.flow_mod()
82 request.match = match
83 request.cookie = random.randint(0,9007199254740992)
84 request.buffer_id = 0xffffffff
85 request.idle_timeout = 1
86 request.flags |= ofp.OFPFF_SEND_FLOW_REM
87 act.port = egress_port
88 self.assertTrue(request.actions.add(act), "Could not add action")
89
90 pa_logger.info("Inserting flow")
91 rv = self.controller.message_send(request)
92 self.assertTrue(rv != -1, "Error installing flow mod")
93 do_barrier(self.controller)
94
95 (response, raw) = self.controller.poll(ofp.OFPT_FLOW_REMOVED, 2)
96
97 self.assertTrue(response is not None,
98 'Did not receive flow removed message ')
99
100 self.assertEqual(request.cookie, response.cookie,
101 'Cookies do not match')
102
103 self.assertEqual(ofp.OFPRR_IDLE_TIMEOUT, response.reason,
104 'Flow table entry removal reason is not idle_timeout')
105
106 self.assertEqual(match, response.match,
107 'Flow table entry does not match')
108