Matteo Scandolo | a229eca | 2017-08-08 13:05:28 -0700 | [diff] [blame] | 1 | |
| 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 Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 17 | """ |
| 18 | Flow expire test case. |
| 19 | Similar to Flow expire test case in the perl test harness. |
| 20 | |
| 21 | """ |
| 22 | |
| 23 | import logging |
| 24 | |
| 25 | import unittest |
| 26 | import random |
| 27 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 28 | from oftest import config |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 29 | import oftest.controller as controller |
Rich Lane | d7b0ffa | 2013-03-08 15:53:42 -0800 | [diff] [blame] | 30 | import ofp |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 31 | import oftest.dataplane as dataplane |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 32 | import oftest.parse as parse |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 33 | import oftest.base_tests as base_tests |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 34 | |
Rich Lane | da3b5ad | 2012-10-03 09:05:32 -0700 | [diff] [blame] | 35 | from oftest.testutils import * |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 36 | from time import sleep |
| 37 | |
Rich Lane | b90a1c4 | 2012-10-05 09:16:05 -0700 | [diff] [blame] | 38 | class FlowExpire(base_tests.SimpleDataPlane): |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 39 | """ |
| 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 Chiang | e097c6e | 2012-04-03 13:17:42 -0700 | [diff] [blame] | 47 | # TODO: set from command-line parameter |
| 48 | test_timeout = 60 |
| 49 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 50 | of_ports = config["port_map"].keys() |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 51 | of_ports.sort() |
| 52 | self.assertTrue(len(of_ports) > 1, "Not enough ports for test") |
| 53 | |
Rich Lane | 32bf948 | 2013-01-03 17:26:30 -0800 | [diff] [blame] | 54 | delete_all_flows(self.controller) |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 55 | |
| 56 | pkt = simple_tcp_packet() |
Ed Swierk | 99a74de | 2012-08-22 06:40:54 -0700 | [diff] [blame] | 57 | match = packet_to_flow_match(self, pkt) |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 58 | match.wildcards &= ~ofp.OFPFW_IN_PORT |
| 59 | self.assertTrue(match is not None, |
| 60 | "Could not generate flow match from pkt") |
Rich Lane | 9d3cc6b | 2013-03-08 16:33:08 -0800 | [diff] [blame] | 61 | act = ofp.action.output() |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 62 | |
Rich Lane | 477f481 | 2012-10-04 22:49:00 -0700 | [diff] [blame] | 63 | of_ports = config["port_map"].keys() |
Ken Chiang | e097c6e | 2012-04-03 13:17:42 -0700 | [diff] [blame] | 64 | 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 Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 69 | logging.info("Ingress " + str(ingress_port) + |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 70 | " to egress " + str(egress_port)) |
| 71 | |
| 72 | match.in_port = ingress_port |
| 73 | |
Rich Lane | ba3f0e2 | 2013-03-11 16:43:57 -0700 | [diff] [blame] | 74 | request = ofp.message.flow_add() |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 75 | 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 Lane | c495d9e | 2013-03-08 17:43:36 -0800 | [diff] [blame] | 81 | request.actions.append(act) |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 82 | |
Rich Lane | 9a00381 | 2012-10-04 17:17:59 -0700 | [diff] [blame] | 83 | logging.info("Inserting flow") |
Rich Lane | 5c3151c | 2013-01-03 17:15:41 -0800 | [diff] [blame] | 84 | self.controller.message_send(request) |
Rich Lane | 3a261d5 | 2013-01-03 17:45:08 -0800 | [diff] [blame] | 85 | do_barrier(self.controller) |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 86 | |
Ken Chiang | e097c6e | 2012-04-03 13:17:42 -0700 | [diff] [blame] | 87 | (response, pkt) = self.controller.poll(exp_msg=ofp.OFPT_FLOW_REMOVED, |
| 88 | timeout=test_timeout) |
| 89 | |
Dan Talayco | 89d5734 | 2010-06-07 16:24:59 -0700 | [diff] [blame] | 90 | 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 Lane | 2617eb6 | 2015-05-05 14:19:59 -0700 | [diff] [blame] | 102 | sleep(1) |