blob: ccbad2068acbc9633f8c33533d85f12db495aafb [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.dataplane as dataplane
Dan Talayco89d57342010-06-07 16:24:59 -070016import oftest.parse as parse
Rich Laneb90a1c42012-10-05 09:16:05 -070017import oftest.base_tests as base_tests
Dan Talayco89d57342010-06-07 16:24:59 -070018
Rich Laneda3b5ad2012-10-03 09:05:32 -070019from oftest.testutils import *
Dan Talayco89d57342010-06-07 16:24:59 -070020from time import sleep
21
Rich Laneb90a1c42012-10-05 09:16:05 -070022class FlowExpire(base_tests.SimpleDataPlane):
Dan Talayco89d57342010-06-07 16:24:59 -070023 """
24 Verify flow expire messages are properly generated.
25
26 Generate a packet
27 Generate and install a matching flow with idle timeout = 1 sec
28 Verify the flow expiration message is received
29 """
30 def runTest(self):
Ken Chiange097c6e2012-04-03 13:17:42 -070031 # TODO: set from command-line parameter
32 test_timeout = 60
33
Rich Lane477f4812012-10-04 22:49:00 -070034 of_ports = config["port_map"].keys()
Dan Talayco89d57342010-06-07 16:24:59 -070035 of_ports.sort()
36 self.assertTrue(len(of_ports) > 1, "Not enough ports for test")
37
Rich Lane32bf9482013-01-03 17:26:30 -080038 delete_all_flows(self.controller)
Dan Talayco89d57342010-06-07 16:24:59 -070039
40 pkt = simple_tcp_packet()
Ed Swierk99a74de2012-08-22 06:40:54 -070041 match = packet_to_flow_match(self, pkt)
Dan Talayco89d57342010-06-07 16:24:59 -070042 match.wildcards &= ~ofp.OFPFW_IN_PORT
43 self.assertTrue(match is not None,
44 "Could not generate flow match from pkt")
Rich Lane9d3cc6b2013-03-08 16:33:08 -080045 act = ofp.action.output()
Dan Talayco89d57342010-06-07 16:24:59 -070046
Rich Lane477f4812012-10-04 22:49:00 -070047 of_ports = config["port_map"].keys()
Ken Chiange097c6e2012-04-03 13:17:42 -070048 of_ports.sort()
49 self.assertTrue(len(of_ports) > 1, "Not enough ports for test")
50
51 ingress_port = of_ports[0]
52 egress_port = of_ports[1]
Rich Lane9a003812012-10-04 17:17:59 -070053 logging.info("Ingress " + str(ingress_port) +
Dan Talayco89d57342010-06-07 16:24:59 -070054 " to egress " + str(egress_port))
55
56 match.in_port = ingress_port
57
Rich Laneba3f0e22013-03-11 16:43:57 -070058 request = ofp.message.flow_add()
Dan Talayco89d57342010-06-07 16:24:59 -070059 request.match = match
60 request.cookie = random.randint(0,9007199254740992)
61 request.buffer_id = 0xffffffff
62 request.idle_timeout = 1
63 request.flags |= ofp.OFPFF_SEND_FLOW_REM
64 act.port = egress_port
Rich Lanec495d9e2013-03-08 17:43:36 -080065 request.actions.append(act)
Dan Talayco89d57342010-06-07 16:24:59 -070066
Rich Lane9a003812012-10-04 17:17:59 -070067 logging.info("Inserting flow")
Rich Lane5c3151c2013-01-03 17:15:41 -080068 self.controller.message_send(request)
Rich Lane3a261d52013-01-03 17:45:08 -080069 do_barrier(self.controller)
Dan Talayco89d57342010-06-07 16:24:59 -070070
Ken Chiange097c6e2012-04-03 13:17:42 -070071 (response, pkt) = self.controller.poll(exp_msg=ofp.OFPT_FLOW_REMOVED,
72 timeout=test_timeout)
73
Dan Talayco89d57342010-06-07 16:24:59 -070074 self.assertTrue(response is not None,
75 'Did not receive flow removed message ')
76
77 self.assertEqual(request.cookie, response.cookie,
78 'Cookies do not match')
79
80 self.assertEqual(ofp.OFPRR_IDLE_TIMEOUT, response.reason,
81 'Flow table entry removal reason is not idle_timeout')
82
83 self.assertEqual(match, response.match,
84 'Flow table entry does not match')
85
Rich Lane2617eb62015-05-05 14:19:59 -070086 sleep(1)