blob: 7ebab4b5be23c44287dfcd56c8c2df7fa98b3f9d [file] [log] [blame]
Dan Talayco89d57342010-06-07 16:24:59 -07001"""
2Flow stats test case.
3Similar to Flow stats test case in the perl test harness.
4
5"""
6
7import logging
8
9import unittest
10import random
11
12import oftest.controller as controller
13import oftest.cstruct as ofp
14import oftest.message as message
15import oftest.dataplane as dataplane
16import oftest.action as action
17import oftest.parse as parse
18import basic
19
20from testutils import *
21from time import sleep
22
23#@var port_map Local copy of the configuration map from OF port
24# numbers to OS interfaces
25pa_port_map = None
26#@var pa_logger Local logger object
27pa_logger = None
28#@var pa_config Local copy of global configuration data
29pa_config = None
30
31def test_set_init(config):
32 """
33 Set up function for packet action test classes
34
35 @param config The configuration dictionary; see oft
36 """
37
Ed Swierk6ccbb072012-03-19 14:48:40 -070038 basic.test_set_init(config)
39
Dan Talayco89d57342010-06-07 16:24:59 -070040 global pa_port_map
41 global pa_logger
42 global pa_config
43
44 pa_logger = logging.getLogger("pkt_act")
45 pa_logger.info("Initializing test set")
46 pa_port_map = config["port_map"]
47 pa_config = config
48
49class FlowStats(basic.SimpleDataPlane):
50 """
51 Verify flow stats are properly retrieved.
52
53 Generate a packet
54 Generate and install a matching flow with idle timeout = 1 sec
55 Verify the flow expiration message is received
56 """
57 def runTest(self):
58 global pa_port_map
59
60 of_ports = pa_port_map.keys()
61 of_ports.sort()
62 self.assertTrue(len(of_ports) > 1, "Not enough ports for test")
63
64 rc = delete_all_flows(self.controller, pa_logger)
65 self.assertEqual(rc, 0, "Failed to delete all flows")
66
67 pkt = simple_tcp_packet()
68 match = parse.packet_to_flow_match(pkt)
69 match.wildcards &= ~ofp.OFPFW_IN_PORT
70 self.assertTrue(match is not None,
71 "Could not generate flow match from pkt")
72 act = action.action_output()
73
Dan Talayco39bf6912010-07-08 14:17:52 -070074 ingress_port = of_ports[0];
75 egress_port = of_ports[1];
Dan Talayco89d57342010-06-07 16:24:59 -070076 pa_logger.info("Ingress " + str(ingress_port) +
77 " to egress " + str(egress_port))
78
79 match.in_port = ingress_port
80
81 flow_mod_msg = message.flow_mod()
82 flow_mod_msg.match = match
83 flow_mod_msg.cookie = random.randint(0,9007199254740992)
84 flow_mod_msg.buffer_id = 0xffffffff
85 flow_mod_msg.idle_timeout = 1
86 act.port = egress_port
87 self.assertTrue(flow_mod_msg.actions.add(act), "Could not add action")
88
89 stat_req = message.flow_stats_request()
90 stat_req.match = match
91 stat_req.table_id = 0xff
92 stat_req.out_port = ofp.OFPP_NONE;
93
94 do_barrier(self.controller)
95 pa_logger.info("Sending stats request")
96 rv = self.controller.message_send(stat_req)
97 self.assertTrue(rv != -1, "Error sending flow stat req")
98 do_barrier(self.controller)
99
100 (response, raw) = self.controller.poll(ofp.OFPT_STATS_REPLY, 2)
101
102 pa_logger.info("Inserting flow")
103 rv = self.controller.message_send(flow_mod_msg)
104 self.assertTrue(rv != -1, "Error installing flow mod")
105 do_barrier(self.controller)
106
107 pa_logger.info("Sending packet to dp port " +
108 str(ingress_port))
109 self.dataplane.send(ingress_port, str(pkt))
110 (rcv_port, rcv_pkt, pkt_time) = self.dataplane.poll(timeout=2)
111 self.assertTrue(rcv_pkt is not None, "Did not receive packet")
112 pa_logger.debug("Packet len " + str(len(pkt)) + " in on " +
113 str(rcv_port))
114 self.assertEqual(rcv_port, egress_port, "Unexpected receive port")
115 self.assertEqual(str(pkt), str(rcv_pkt),
116 'Response packet does not match send packet')
117
118 pa_logger.info("Sending stats request")
119 rv = self.controller.message_send(stat_req)
120 self.assertTrue(rv != -1, "Error sending flow stat req")
121 do_barrier(self.controller)
122
123 (response, raw) = self.controller.poll(ofp.OFPT_STATS_REPLY, 2)
124 #print "YYY: Stats reply is \n%s" % (response.show())
125 self.assertTrue(len(response.stats) == 1, "Did not receive flow stats reply")