blob: cc1fee92afe310fd3f60d3e2e1a8bf70460ba605 [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
38 global pa_port_map
39 global pa_logger
40 global pa_config
41
42 pa_logger = logging.getLogger("pkt_act")
43 pa_logger.info("Initializing test set")
44 pa_port_map = config["port_map"]
45 pa_config = config
46
47class FlowExpire(basic.SimpleDataPlane):
48 """
49 Verify flow expire messages are properly generated.
50
51 Generate a packet
52 Generate and install a matching flow with idle timeout = 1 sec
53 Verify the flow expiration message is received
54 """
55 def runTest(self):
56 global pa_port_map
57
58 of_ports = pa_port_map.keys()
59 of_ports.sort()
60 self.assertTrue(len(of_ports) > 1, "Not enough ports for test")
61
62 rc = delete_all_flows(self.controller, pa_logger)
63 self.assertEqual(rc, 0, "Failed to delete all flows")
64
65 pkt = simple_tcp_packet()
66 match = parse.packet_to_flow_match(pkt)
67 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
72 ingress_port = pa_config["base_of_port"]
73 egress_port = (pa_config["base_of_port"] + 1) % len(of_ports)
74 pa_logger.info("Ingress " + str(ingress_port) +
75 " to egress " + str(egress_port))
76
77 match.in_port = ingress_port
78
79 request = message.flow_mod()
80 request.match = match
81 request.cookie = random.randint(0,9007199254740992)
82 request.buffer_id = 0xffffffff
83 request.idle_timeout = 1
84 request.flags |= ofp.OFPFF_SEND_FLOW_REM
85 act.port = egress_port
86 self.assertTrue(request.actions.add(act), "Could not add action")
87
88 pa_logger.info("Inserting flow")
89 rv = self.controller.message_send(request)
90 self.assertTrue(rv != -1, "Error installing flow mod")
91 do_barrier(self.controller)
92
93 (response, raw) = self.controller.poll(ofp.OFPT_FLOW_REMOVED, 2)
94
95 self.assertTrue(response is not None,
96 'Did not receive flow removed message ')
97
98 self.assertEqual(request.cookie, response.cookie,
99 'Cookies do not match')
100
101 self.assertEqual(ofp.OFPRR_IDLE_TIMEOUT, response.reason,
102 'Flow table entry removal reason is not idle_timeout')
103
104 self.assertEqual(match, response.match,
105 'Flow table entry does not match')
106