blob: d12c38874bca0aca59c4808f91355c69983571b8 [file] [log] [blame]
ShreyaPandita60e45542012-09-27 15:11:16 -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
Rich Laneda3b5ad2012-10-03 09:05:32 -070016from oftest.testutils import *
ShreyaPandita60e45542012-09-27 15:11:16 -040017from time import sleep
18
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
86 match2.in_port = of_ports[0]
87
88 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()
94 act2.port = of_ports[1]
95 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
187def Verify_TableStats(self,active_entries=0,):
188#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
202def Verify_FlowStats(self,match,byte_count=0,packet_count=0):
203# Verify flow counters : byte_count and packet_count
204
205 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
220
221 if all_packets_received:
222 break
223 sleep(1)
224
225 self.assertTrue(all_packets_received,
226 "Flow counters are incorrect")
227
228def Verify_PortStats(self,in_port,rx_dropped):
229#Verify Port counters like rx_dropped
230
231 port_stats_req = message.port_stats_request()
232 port_stats_req.port_no = in_port
233 resp,pkt = self.controller.transact(port_stats_req)
234 self.assertTrue(resp is not None,"No response received for port stats request")
235 self.assertTrue(resp.rx_dropped == rx_dropped, "Packets not dropped")
236
237
238
239############################## Various delete commands #############################################################################################
240
241def Strict_Delete(self,match,priority=0):
242# Issue Strict Delete
243
244 #Create flow_mod message, command DELETE_STRICT
245 msg4 = message.flow_mod()
246 msg4.out_port = ofp.OFPP_NONE
247 msg4.command = ofp.OFPFC_DELETE_STRICT
248 msg4.buffer_id = 0xffffffff
249 msg4.match = match
250
251 if priority != 0 :
252 msg4.priority = priority
253
254 rv = self.controller.message_send(msg4)
255 self.assertTrue(rv!= -1, "Error installing flow mod")
256 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
257
258
259
260def NonStrict_Delete(self,match,priority=0):
261# Issue Non_Strict Delete
262
263 #Create flow_mod message, command DELETE
264 msg6 = message.flow_mod()
265 msg6.out_port = ofp.OFPP_NONE
266 msg6.command = ofp.OFPFC_DELETE
267 msg6.buffer_id = 0xffffffff
268 msg6.match = match
269
270 if priority != 0 :
271 msg6.priority = priority
272
273 rv = self.controller.message_send(msg6)
274 self.assertTrue(rv != -1, "Error installing flow mod")
275 self.assertEqual(do_barrier(self.controller),0, "Barrier failed")
276
277
278###########################################################################################################################################################
279
280def SendPacket(obj, pkt, ingress_port, egress_port):
281#Send Packets on a specified ingress_port and verify if its recieved on correct egress_port.
282
283 obj.dataplane.send(ingress_port, str(pkt))
284 exp_pkt_arg = pkt
285 exp_port = egress_port
286
287 (rcv_port, rcv_pkt, pkt_time) = obj.dataplane.poll(timeout=2,
288 port_number=exp_port,
289 exp_pkt=exp_pkt_arg)
290 obj.assertTrue(rcv_pkt is not None,
291 "Packet not received on port " + str(egress_port))
292 obj.assertEqual(rcv_port, egress_port,
293 "Packet received on port " + str(rcv_port) +
294 ", expected port " + str(egress_port))
295 obj.assertEqual(str(pkt), str(rcv_pkt),
296 'Response packet does not match send packet')
297
298
ShreyaPandita572e64b2012-09-28 14:41:06 -0400299def sw_supported_actions(parent,use_cache=False):
ShreyaPandita60e45542012-09-27 15:11:16 -0400300#Returns the switch's supported actions
301
302 cache_supported_actions = None
303 if cache_supported_actions is None or not use_cache:
304 request = message.features_request()
305 (reply, pkt) = parent.controller.transact(request)
306 parent.assertTrue(reply is not None, "Did not get response to ftr req")
307 cache_supported_actions = reply.actions
308 return cache_supported_actions
309