blob: 549382f87c7c7291012a255f67ee75f12a0f51d8 [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
38 global pa_port_map
39 global pa_logger
40 global pa_config
41
42 pa_logger = logging.getLogger("pkt_act")
43 pa_logger.info("Initializing test set")
44 pa_port_map = config["port_map"]
45 pa_config = config
46
47class FlowStats(basic.SimpleDataPlane):
48 """
49 Verify flow stats are properly retrieved.
50
51 Generate a packet
52 Generate and install a matching flow with idle timeout = 1 sec
53 Verify the flow expiration message is received
54 """
55 def runTest(self):
56 global pa_port_map
57
58 of_ports = pa_port_map.keys()
59 of_ports.sort()
60 self.assertTrue(len(of_ports) > 1, "Not enough ports for test")
61
62 rc = delete_all_flows(self.controller, pa_logger)
63 self.assertEqual(rc, 0, "Failed to delete all flows")
64
65 pkt = simple_tcp_packet()
66 match = parse.packet_to_flow_match(pkt)
67 match.wildcards &= ~ofp.OFPFW_IN_PORT
68 self.assertTrue(match is not None,
69 "Could not generate flow match from pkt")
70 act = action.action_output()
71
Dan Talayco39bf6912010-07-08 14:17:52 -070072 ingress_port = of_ports[0];
73 egress_port = of_ports[1];
Dan Talayco89d57342010-06-07 16:24:59 -070074 pa_logger.info("Ingress " + str(ingress_port) +
75 " to egress " + str(egress_port))
76
77 match.in_port = ingress_port
78
79 flow_mod_msg = message.flow_mod()
80 flow_mod_msg.match = match
81 flow_mod_msg.cookie = random.randint(0,9007199254740992)
82 flow_mod_msg.buffer_id = 0xffffffff
83 flow_mod_msg.idle_timeout = 1
84 act.port = egress_port
85 self.assertTrue(flow_mod_msg.actions.add(act), "Could not add action")
86
87 stat_req = message.flow_stats_request()
88 stat_req.match = match
89 stat_req.table_id = 0xff
90 stat_req.out_port = ofp.OFPP_NONE;
91
92 do_barrier(self.controller)
93 pa_logger.info("Sending stats request")
94 rv = self.controller.message_send(stat_req)
95 self.assertTrue(rv != -1, "Error sending flow stat req")
96 do_barrier(self.controller)
97
98 (response, raw) = self.controller.poll(ofp.OFPT_STATS_REPLY, 2)
99
100 pa_logger.info("Inserting flow")
101 rv = self.controller.message_send(flow_mod_msg)
102 self.assertTrue(rv != -1, "Error installing flow mod")
103 do_barrier(self.controller)
104
105 pa_logger.info("Sending packet to dp port " +
106 str(ingress_port))
107 self.dataplane.send(ingress_port, str(pkt))
108 (rcv_port, rcv_pkt, pkt_time) = self.dataplane.poll(timeout=2)
109 self.assertTrue(rcv_pkt is not None, "Did not receive packet")
110 pa_logger.debug("Packet len " + str(len(pkt)) + " in on " +
111 str(rcv_port))
112 self.assertEqual(rcv_port, egress_port, "Unexpected receive port")
113 self.assertEqual(str(pkt), str(rcv_pkt),
114 'Response packet does not match send packet')
115
116 pa_logger.info("Sending stats request")
117 rv = self.controller.message_send(stat_req)
118 self.assertTrue(rv != -1, "Error sending flow stat req")
119 do_barrier(self.controller)
120
121 (response, raw) = self.controller.poll(ofp.OFPT_STATS_REPLY, 2)
122 #print "YYY: Stats reply is \n%s" % (response.show())
123 self.assertTrue(len(response.stats) == 1, "Did not receive flow stats reply")