blob: e5e48eedea9d7bfded75a086a8ad585b6725537e [file] [log] [blame]
Matteo Scandoloa229eca2017-08-08 13:05:28 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Dan Talayco89d57342010-06-07 16:24:59 -070017"""
18Flow expire test case.
19Similar to Flow expire test case in the perl test harness.
20
21"""
22
23import logging
24
25import unittest
26import random
27
Rich Lane477f4812012-10-04 22:49:00 -070028from oftest import config
Dan Talayco89d57342010-06-07 16:24:59 -070029import oftest.controller as controller
Rich Laned7b0ffa2013-03-08 15:53:42 -080030import ofp
Dan Talayco89d57342010-06-07 16:24:59 -070031import oftest.dataplane as dataplane
Dan Talayco89d57342010-06-07 16:24:59 -070032import oftest.parse as parse
Rich Laneb90a1c42012-10-05 09:16:05 -070033import oftest.base_tests as base_tests
Dan Talayco89d57342010-06-07 16:24:59 -070034
Rich Laneda3b5ad2012-10-03 09:05:32 -070035from oftest.testutils import *
Dan Talayco89d57342010-06-07 16:24:59 -070036from time import sleep
37
Rich Laneb90a1c42012-10-05 09:16:05 -070038class FlowExpire(base_tests.SimpleDataPlane):
Dan Talayco89d57342010-06-07 16:24:59 -070039 """
40 Verify flow expire messages are properly generated.
41
42 Generate a packet
43 Generate and install a matching flow with idle timeout = 1 sec
44 Verify the flow expiration message is received
45 """
46 def runTest(self):
Ken Chiange097c6e2012-04-03 13:17:42 -070047 # TODO: set from command-line parameter
48 test_timeout = 60
49
Rich Lane477f4812012-10-04 22:49:00 -070050 of_ports = config["port_map"].keys()
Dan Talayco89d57342010-06-07 16:24:59 -070051 of_ports.sort()
52 self.assertTrue(len(of_ports) > 1, "Not enough ports for test")
53
Rich Lane32bf9482013-01-03 17:26:30 -080054 delete_all_flows(self.controller)
Dan Talayco89d57342010-06-07 16:24:59 -070055
56 pkt = simple_tcp_packet()
Ed Swierk99a74de2012-08-22 06:40:54 -070057 match = packet_to_flow_match(self, pkt)
Dan Talayco89d57342010-06-07 16:24:59 -070058 match.wildcards &= ~ofp.OFPFW_IN_PORT
59 self.assertTrue(match is not None,
60 "Could not generate flow match from pkt")
Rich Lane9d3cc6b2013-03-08 16:33:08 -080061 act = ofp.action.output()
Dan Talayco89d57342010-06-07 16:24:59 -070062
Rich Lane477f4812012-10-04 22:49:00 -070063 of_ports = config["port_map"].keys()
Ken Chiange097c6e2012-04-03 13:17:42 -070064 of_ports.sort()
65 self.assertTrue(len(of_ports) > 1, "Not enough ports for test")
66
67 ingress_port = of_ports[0]
68 egress_port = of_ports[1]
Rich Lane9a003812012-10-04 17:17:59 -070069 logging.info("Ingress " + str(ingress_port) +
Dan Talayco89d57342010-06-07 16:24:59 -070070 " to egress " + str(egress_port))
71
72 match.in_port = ingress_port
73
Rich Laneba3f0e22013-03-11 16:43:57 -070074 request = ofp.message.flow_add()
Dan Talayco89d57342010-06-07 16:24:59 -070075 request.match = match
76 request.cookie = random.randint(0,9007199254740992)
77 request.buffer_id = 0xffffffff
78 request.idle_timeout = 1
79 request.flags |= ofp.OFPFF_SEND_FLOW_REM
80 act.port = egress_port
Rich Lanec495d9e2013-03-08 17:43:36 -080081 request.actions.append(act)
Dan Talayco89d57342010-06-07 16:24:59 -070082
Rich Lane9a003812012-10-04 17:17:59 -070083 logging.info("Inserting flow")
Rich Lane5c3151c2013-01-03 17:15:41 -080084 self.controller.message_send(request)
Rich Lane3a261d52013-01-03 17:45:08 -080085 do_barrier(self.controller)
Dan Talayco89d57342010-06-07 16:24:59 -070086
Ken Chiange097c6e2012-04-03 13:17:42 -070087 (response, pkt) = self.controller.poll(exp_msg=ofp.OFPT_FLOW_REMOVED,
88 timeout=test_timeout)
89
Dan Talayco89d57342010-06-07 16:24:59 -070090 self.assertTrue(response is not None,
91 'Did not receive flow removed message ')
92
93 self.assertEqual(request.cookie, response.cookie,
94 'Cookies do not match')
95
96 self.assertEqual(ofp.OFPRR_IDLE_TIMEOUT, response.reason,
97 'Flow table entry removal reason is not idle_timeout')
98
99 self.assertEqual(match, response.match,
100 'Flow table entry does not match')
101
Rich Lane2617eb62015-05-05 14:19:59 -0700102 sleep(1)