blob: d14ee71f10a1748e9ff4b3484ae3dc0715aac0dd [file] [log] [blame]
ShreyaPanditacf64cc92012-09-14 17:25:33 -04001""" Defined Some common functions used by Conformance tests -- OF-SWITCH 1.0.0 Testcases """
2
3import sys
4import copy
5import random
6
7import oftest.controller as controller
8import oftest.cstruct as ofp
9import oftest.message as message
10import oftest.dataplane as dataplane
11import oftest.action as action
12import oftest.parse as parse
13import logging
14import types
15import basic
16from testutils import *
ShreyaPandita7a41dce2012-09-20 17:48:00 -040017from time import sleep
ShreyaPanditacf64cc92012-09-14 17:25:33 -040018
19#################### Functions for various types of flow_mod ##########################################################################################
20
21def Exact_Match(self,of_ports,priority=0):
22# Generate ExactMatch flow .
23
24 #Create a simple tcp packet and generate exact flow match from it.
25 Pkt_ExactFlow = simple_tcp_packet()
26 match = parse.packet_to_flow_match(Pkt_ExactFlow)
27 self.assertTrue(match is not None, "Could not generate flow match from pkt")
28 match.in_port = of_ports[0]
29 #match.nw_src = 1
30 match.wildcards=0
31 msg = message.flow_mod()
32 msg.out_port = ofp.OFPP_NONE
33 msg.command = ofp.OFPFC_ADD
34 msg.buffer_id = 0xffffffff
35 msg.match = match
36 if priority != 0 :
37 msg.priority = priority
38
39 act = action.action_output()
40 act.port = of_ports[1]
41 self.assertTrue(msg.actions.add(act), "could not add action")
42
43 rv = self.controller.message_send(msg)
44 self.assertTrue(rv != -1, "Error installing flow mod")
45 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
46
47 return (Pkt_ExactFlow,match)
48
49
50def Match_All_Except_Source_Address(self,of_ports,priority=0):
51# Generate Match_All_Except_Source_Address flow
52
53 #Create a simple tcp packet and generate match all except src address flow.
54 Pkt_WildcardSrc= simple_tcp_packet()
55 match1 = parse.packet_to_flow_match(Pkt_WildcardSrc)
56 self.assertTrue(match1 is not None, "Could not generate flow match from pkt")
57 match1.in_port = of_ports[0]
58 #match1.nw_src = 1
59 match1.wildcards = ofp.OFPFW_DL_SRC
60 msg1 = message.flow_mod()
61 msg1.out_port = ofp.OFPP_NONE
62 msg1.command = ofp.OFPFC_ADD
63 msg1.buffer_id = 0xffffffff
64 msg1.match = match1
65 if priority != 0 :
66 msg1.priority = priority
67
68 act1 = action.action_output()
69 act1.port = of_ports[1]
70 self.assertTrue(msg1.actions.add(act1), "could not add action")
71
72 rv = self.controller.message_send(msg1)
73 self.assertTrue(rv != -1, "Error installing flow mod")
74 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
75
76 return (Pkt_WildcardSrc,match1)
77
78def Wildcard_All(self,of_ports,priority=0):
79# Generate a Wildcard_All Flow
80
81 #Create a simple tcp packet and generate wildcard all flow match from it.
82 Pkt_Wildcard = simple_tcp_packet()
83 match2 = parse.packet_to_flow_match(Pkt_Wildcard)
84 self.assertTrue(match2 is not None, "Could not generate flow match from pkt")
85 match2.wildcards=ofp.OFPFW_ALL
ShreyaPandita7a41dce2012-09-20 17:48:00 -040086 match2.in_port = of_ports[0]
87
ShreyaPanditacf64cc92012-09-14 17:25:33 -040088 msg2 = message.flow_mod()
89 msg2.out_port = ofp.OFPP_NONE
90 msg2.command = ofp.OFPFC_ADD
91 msg2.buffer_id = 0xffffffff
92 msg2.match = match2
93 act2 = action.action_output()
ShreyaPandita7a41dce2012-09-20 17:48:00 -040094 act2.port = of_ports[1]
ShreyaPanditacf64cc92012-09-14 17:25:33 -040095 self.assertTrue(msg2.actions.add(act2), "could not add action")
96 if priority != 0 :
97 msg2.priority = priority
98
99 rv = self.controller.message_send(msg2)
100 self.assertTrue(rv != -1, "Error installing flow mod")
101 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
102
103 return (Pkt_Wildcard,match2)
104
105def Wildcard_All_Except_Ingress(self,of_ports,priority=0):
106# Generate Wildcard_All_Except_Ingress_port flow
107
108
109 #Create a simple tcp packet and generate wildcard all except ingress_port flow.
110 Pkt_MatchIngress = simple_tcp_packet()
111 match3 = parse.packet_to_flow_match(Pkt_MatchIngress)
112 self.assertTrue(match3 is not None, "Could not generate flow match from pkt")
113 match3.wildcards = ofp.OFPFW_ALL-ofp.OFPFW_IN_PORT
114 match3.in_port = of_ports[0]
115
116 msg3 = message.flow_mod()
117 msg3.command = ofp.OFPFC_ADD
118 msg3.match = match3
119 msg3.out_port = of_ports[2] # ignored by flow add,flow modify
120 msg3.cookie = random.randint(0,9007199254740992)
121 msg3.buffer_id = 0xffffffff
122 msg3.idle_timeout = 0
123 msg3.hard_timeout = 0
124 msg3.buffer_id = 0xffffffff
125
126 act3 = action.action_output()
127 act3.port = of_ports[1]
128 self.assertTrue(msg3.actions.add(act3), "could not add action")
129
130 if priority != 0 :
131 msg3.priority = priority
132
133 rv = self.controller.message_send(msg3)
134 self.assertTrue(rv != -1, "Error installing flow mod")
135 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
136
137 return (Pkt_MatchIngress,match3)
138
139
140def Strict_Modify_Flow_Action(self,egress_port,match,priority=0):
141# Strict Modify the flow Action
142
143 #Create a flow_mod message , command MODIFY_STRICT
144 msg5 = message.flow_mod()
145 msg5.match = match
146 msg5.cookie = random.randint(0,9007199254740992)
147 msg5.command = ofp.OFPFC_MODIFY_STRICT
148 msg5.buffer_id = 0xffffffff
149 act5 = action.action_output()
150 act5.port = egress_port
151 self.assertTrue(msg5.actions.add(act5), "could not add action")
152
153 if priority != 0 :
154 msg5.priority = priority
155
156 # Send the flow with action A'
157 rv = self.controller.message_send (msg5)
158 self.assertTrue(rv != -1, "Error installing flow mod")
159 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
160
161def Modify_Flow_Action(self,of_ports,match,priority=0):
162# Modify the flow action
163
164 #Create a flow_mod message , command MODIFY
165 msg8 = message.flow_mod()
166 msg8.match = match
167 msg8.cookie = random.randint(0,9007199254740992)
168 msg8.command = ofp.OFPFC_MODIFY
169 #Will be ignored for flow adds and flow modify (here for test-case Add_Modify_With_Outport)
170 msg8.out_port = of_ports[3]
171 msg8.buffer_id = 0xffffffff
172 act8 = action.action_output()
173 act8.port = of_ports[2]
174 self.assertTrue(msg8.actions.add(act8), "could not add action")
175
176 if priority != 0 :
177 msg8.priority = priority
178
179 # Send the flow with action A'
180 rv = self.controller.message_send (msg8)
181 self.assertTrue(rv != -1, "Error installing flow mod")
182 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
183
184
185########################### Verify Stats Functions ###########################################################################################
186
ShreyaPandita7a41dce2012-09-20 17:48:00 -0400187def Verify_TableStats(self,active_entries=0,):
ShreyaPanditacf64cc92012-09-14 17:25:33 -0400188#Verify Table_Stats
189
190 #Send Table_Stats_Request
191 request = message.table_stats_request()
192 response, pkt = self.controller.transact(request, timeout=1)
193 self.assertTrue(response is not None, "Did not get response")
194 active_count=0
195
196 #Verify active_count in the reply
197 for stat in response.stats:
198 active_count += stat.active_count
199 self.assertTrue(active_entries == active_count,"Incorrect no. of flows in Table")
200
201
ShreyaPandita7a41dce2012-09-20 17:48:00 -0400202def Verify_FlowStats(self,match,byte_count=0,packet_count=0):
203# Verify flow counters : byte_count and packet_count
ShreyaPanditacf64cc92012-09-14 17:25:33 -0400204
ShreyaPandita7a41dce2012-09-20 17:48:00 -0400205 stat_req = message.flow_stats_request()
206 stat_req.match = match
207 stat_req.table_id = 0xff
208 stat_req.out_port = ofp.OFPP_NONE
209 test_timeout = 10
210 all_packets_received = 0
211 for i in range(0,test_timeout):
212
213 response, pkt = self.controller.transact(stat_req,
214 timeout=test_timeout)
215 self.assertTrue(response is not None,
216 "No response to stats request")
217 for obj in response.stats:
218 if ( obj.packet_count == packet_count and obj.byte_count == byte_count ) :
219 all_packets_received = 1
ShreyaPanditacf64cc92012-09-14 17:25:33 -0400220
ShreyaPandita7a41dce2012-09-20 17:48:00 -0400221 if all_packets_received:
222 break
223 sleep(1)
224
225 self.assertTrue(all_packets_received,
226 "Flow counters are incorrect")
ShreyaPanditacf64cc92012-09-14 17:25:33 -0400227
228
229############################## Various delete commands #############################################################################################
230
231def Strict_Delete(self,match,priority=0):
232# Issue Strict Delete
233
234 #Create flow_mod message, command DELETE_STRICT
235 msg4 = message.flow_mod()
236 msg4.out_port = ofp.OFPP_NONE
237 msg4.command = ofp.OFPFC_DELETE_STRICT
238 msg4.buffer_id = 0xffffffff
239 msg4.match = match
240
241 if priority != 0 :
242 msg4.priority = priority
243
244 rv = self.controller.message_send(msg4)
245 self.assertTrue(rv!= -1, "Error installing flow mod")
246 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
247
248
249
250def NonStrict_Delete(self,match,priority=0):
251# Issue Non_Strict Delete
252
253 #Create flow_mod message, command DELETE
254 msg6 = message.flow_mod()
255 msg6.out_port = ofp.OFPP_NONE
256 msg6.command = ofp.OFPFC_DELETE
257 msg6.buffer_id = 0xffffffff
258 msg6.match = match
259
260 if priority != 0 :
261 msg6.priority = priority
262
263 rv = self.controller.message_send(msg6)
264 self.assertTrue(rv != -1, "Error installing flow mod")
265 self.assertEqual(do_barrier(self.controller),0, "Barrier failed")
266
267
268###########################################################################################################################################################
269
270def SendPacket(obj, pkt, ingress_port, egress_port):
271#Send Packets on a specified ingress_port and verify if its recieved on correct egress_port.
272
273 obj.dataplane.send(ingress_port, str(pkt))
274 exp_pkt_arg = pkt
275 exp_port = egress_port
276
277 (rcv_port, rcv_pkt, pkt_time) = obj.dataplane.poll(timeout=2,
278 port_number=exp_port,
279 exp_pkt=exp_pkt_arg)
280 obj.assertTrue(rcv_pkt is not None,
281 "Packet not received on port " + str(egress_port))
282 obj.assertEqual(rcv_port, egress_port,
283 "Packet received on port " + str(rcv_port) +
284 ", expected port " + str(egress_port))
285 obj.assertEqual(str(pkt), str(rcv_pkt),
286 'Response packet does not match send packet')