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