blob: 6b497cc3ea71b48eafc20e33cec1d4e3adf81260 [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
Rich Laned7b0ffa2013-03-08 15:53:42 -080014import ofp
Dan Talayco89d57342010-06-07 16:24:59 -070015import 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 Lane32bf9482013-01-03 17:26:30 -080040 delete_all_flows(self.controller)
Dan Talayco89d57342010-06-07 16:24:59 -070041
42 pkt = simple_tcp_packet()
Ed Swierk99a74de2012-08-22 06:40:54 -070043 match = packet_to_flow_match(self, pkt)
Dan Talayco89d57342010-06-07 16:24:59 -070044 match.wildcards &= ~ofp.OFPFW_IN_PORT
45 self.assertTrue(match is not None,
46 "Could not generate flow match from pkt")
47 act = action.action_output()
48
Rich Lane477f4812012-10-04 22:49:00 -070049 of_ports = config["port_map"].keys()
Ken Chiange097c6e2012-04-03 13:17:42 -070050 of_ports.sort()
51 self.assertTrue(len(of_ports) > 1, "Not enough ports for test")
52
53 ingress_port = of_ports[0]
54 egress_port = of_ports[1]
Rich Lane9a003812012-10-04 17:17:59 -070055 logging.info("Ingress " + str(ingress_port) +
Dan Talayco89d57342010-06-07 16:24:59 -070056 " to egress " + str(egress_port))
57
58 match.in_port = ingress_port
59
60 request = message.flow_mod()
61 request.match = match
62 request.cookie = random.randint(0,9007199254740992)
63 request.buffer_id = 0xffffffff
64 request.idle_timeout = 1
65 request.flags |= ofp.OFPFF_SEND_FLOW_REM
66 act.port = egress_port
Rich Lanee30455b2013-01-03 16:24:44 -080067 request.actions.add(act)
Dan Talayco89d57342010-06-07 16:24:59 -070068
Rich Lane9a003812012-10-04 17:17:59 -070069 logging.info("Inserting flow")
Rich Lane5c3151c2013-01-03 17:15:41 -080070 self.controller.message_send(request)
Rich Lane3a261d52013-01-03 17:45:08 -080071 do_barrier(self.controller)
Dan Talayco89d57342010-06-07 16:24:59 -070072
Ken Chiange097c6e2012-04-03 13:17:42 -070073 (response, pkt) = self.controller.poll(exp_msg=ofp.OFPT_FLOW_REMOVED,
74 timeout=test_timeout)
75
Dan Talayco89d57342010-06-07 16:24:59 -070076 self.assertTrue(response is not None,
77 'Did not receive flow removed message ')
78
79 self.assertEqual(request.cookie, response.cookie,
80 'Cookies do not match')
81
82 self.assertEqual(ofp.OFPRR_IDLE_TIMEOUT, response.reason,
83 'Flow table entry removal reason is not idle_timeout')
84
85 self.assertEqual(match, response.match,
86 'Flow table entry does not match')
87