blob: d87d7542afd6317c644bb2496109d2959f1f7b5f [file] [log] [blame]
ShreyaPandita055102a2012-11-28 11:43:45 -05001""" Defined Some common functions used by Conformance tests -- OF-SWITCH 1.0.0 Testcases """
ShreyaPandita60e45542012-09-27 15:11:16 -04002
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
ShreyaPandita66de26f2012-10-26 14:44:24 -040015
Rich Laneb90a1c42012-10-05 09:16:05 -070016import oftest.base_tests as base_tests
Rich Laneda3b5ad2012-10-03 09:05:32 -070017from oftest.testutils import *
ShreyaPandita60e45542012-09-27 15:11:16 -040018from time import sleep
19
20#################### Functions for various types of flow_mod ##########################################################################################
21
ShreyaPanditaed209962012-11-04 02:16:48 -050022def exact_match(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -040023# Generate ExactMatch flow .
24
ShreyaPandita4ebbac32012-11-02 13:40:44 -040025 #Create a simple tcp packet and generate exact flow match from it.
26 pkt_exactflow = simple_tcp_packet()
27 match = parse.packet_to_flow_match(pkt_exactflow)
28 self.assertTrue(match is not None, "Could not generate flow match from pkt")
29 match.in_port = of_ports[0]
30 #match.nw_src = 1
31 match.wildcards=0
32 msg = message.flow_mod()
33 msg.out_port = ofp.OFPP_NONE
34 msg.command = ofp.OFPFC_ADD
35 msg.buffer_id = 0xffffffff
36 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -050037 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -040038 msg.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -040039
ShreyaPandita4ebbac32012-11-02 13:40:44 -040040 act = action.action_output()
41 act.port = of_ports[1]
42 self.assertTrue(msg.actions.add(act), "could not add action")
ShreyaPandita60e45542012-09-27 15:11:16 -040043
ShreyaPandita4ebbac32012-11-02 13:40:44 -040044 rv = self.controller.message_send(msg)
45 self.assertTrue(rv != -1, "Error installing flow mod")
46 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPandita60e45542012-09-27 15:11:16 -040047
ShreyaPandita4ebbac32012-11-02 13:40:44 -040048 return (pkt_exactflow,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -040049
ShreyaPanditaed209962012-11-04 02:16:48 -050050def exact_match_with_prio(self,of_ports,priority=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -040051 # Generate ExactMatch with action output to port 2
ShreyaPandita66de26f2012-10-26 14:44:24 -040052
ShreyaPandita4ebbac32012-11-02 13:40:44 -040053 #Create a simple tcp packet and generate exact flow match from it.
54 pkt_exactflow = simple_tcp_packet()
55 match = parse.packet_to_flow_match(pkt_exactflow)
56 self.assertTrue(match is not None, "Could not generate flow match from pkt")
57 match.in_port = of_ports[0]
58 #match.nw_src = 1
59 match.wildcards=0
60 msg = message.flow_mod()
61 msg.out_port = ofp.OFPP_NONE
62 msg.command = ofp.OFPFC_ADD
63 msg.buffer_id = 0xffffffff
64 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -050065 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -040066 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -040067
ShreyaPandita4ebbac32012-11-02 13:40:44 -040068 act = action.action_output()
69 act.port = of_ports[2]
70 self.assertTrue(msg.actions.add(act), "could not add action")
ShreyaPandita66de26f2012-10-26 14:44:24 -040071
ShreyaPandita4ebbac32012-11-02 13:40:44 -040072 rv = self.controller.message_send(msg)
73 self.assertTrue(rv != -1, "Error installing flow mod")
74 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPandita66de26f2012-10-26 14:44:24 -040075
ShreyaPandita4ebbac32012-11-02 13:40:44 -040076 return (pkt_exactflow,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -040077
ShreyaPandita60e45542012-09-27 15:11:16 -040078
ShreyaPanditaed209962012-11-04 02:16:48 -050079def match_all_except_source_address(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -040080# Generate Match_All_Except_Source_Address flow
81
ShreyaPandita4ebbac32012-11-02 13:40:44 -040082 #Create a simple tcp packet and generate match all except src address flow.
83 pkt_wildcardsrc= simple_tcp_packet()
84 match1 = parse.packet_to_flow_match(pkt_wildcardsrc)
85 self.assertTrue(match1 is not None, "Could not generate flow match from pkt")
86 match1.in_port = of_ports[0]
87 #match1.nw_src = 1
88 match1.wildcards = ofp.OFPFW_DL_SRC
89 msg1 = message.flow_mod()
90 msg1.out_port = ofp.OFPP_NONE
91 msg1.command = ofp.OFPFC_ADD
92 msg1.buffer_id = 0xffffffff
93 msg1.match = match1
ShreyaPanditaed209962012-11-04 02:16:48 -050094 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -040095 msg1.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -040096
ShreyaPandita4ebbac32012-11-02 13:40:44 -040097 act1 = action.action_output()
98 act1.port = of_ports[1]
99 self.assertTrue(msg1.actions.add(act1), "could not add action")
ShreyaPandita60e45542012-09-27 15:11:16 -0400100
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400101 rv = self.controller.message_send(msg1)
102 self.assertTrue(rv != -1, "Error installing flow mod")
103 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPandita60e45542012-09-27 15:11:16 -0400104
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400105 return (pkt_wildcardsrc,match1)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400106
ShreyaPandita6fbff252012-11-13 16:56:48 -0500107def match_ethernet_src_address(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400108 #Generate Match_Ethernet_SrC_Address flow
109
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400110 #Create a simple tcp packet and generate match on ethernet src address flow
ShreyaPandita6fbff252012-11-13 16:56:48 -0500111 pkt_MatchSrc = simple_eth_packet(dl_src='00:01:01:01:01:01')
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400112 match = parse.packet_to_flow_match(pkt_MatchSrc)
113 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400114
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400115 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_SRC
ShreyaPandita60e45542012-09-27 15:11:16 -0400116
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400117 msg = message.flow_mod()
118 msg.out_port = ofp.OFPP_NONE
119 msg.command = ofp.OFPFC_ADD
120 msg.buffer_id = 0xffffffff
121 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500122 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400123 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400124
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400125 act = action.action_output()
126 act.port = of_ports[1]
127 self.assertTrue(msg.actions.add(act), "could not add action")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400128
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400129 rv = self.controller.message_send(msg)
130 self.assertTrue(rv != -1, "Error installing flow mod")
131 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400132
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400133 return (pkt_MatchSrc,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400134
ShreyaPanditaed209962012-11-04 02:16:48 -0500135def match_ethernet_dst_address(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400136 #Generate Match_Ethernet_Dst_Address flow
137
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400138 #Create a simple tcp packet and generate match on ethernet dst address flow
ShreyaPandita6fbff252012-11-13 16:56:48 -0500139 pkt_matchdst = simple_eth_packet(dl_dst='00:01:01:01:01:01')
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400140 match = parse.packet_to_flow_match(pkt_matchdst)
141 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400142
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400143 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_DST
144 msg = message.flow_mod()
145 msg.out_port = ofp.OFPP_NONE
146 msg.command = ofp.OFPFC_ADD
147 msg.buffer_id = 0xffffffff
148 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500149 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400150 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400151
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400152 act = action.action_output()
153 act.port = of_ports[1]
154 self.assertTrue(msg.actions.add(act), "could not add action")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400155
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400156 rv = self.controller.message_send(msg)
157 self.assertTrue(rv != -1, "Error installing flow mod")
158 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400159
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400160 return (pkt_matchdst,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400161
ShreyaPanditaed209962012-11-04 02:16:48 -0500162def wildcard_all(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400163# Generate a Wildcard_All Flow
164
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400165 #Create a simple tcp packet and generate wildcard all flow match from it.
166 pkt_wildcard = simple_tcp_packet()
167 match2 = parse.packet_to_flow_match(pkt_wildcard)
168 self.assertTrue(match2 is not None, "Could not generate flow match from pkt")
169 match2.wildcards=ofp.OFPFW_ALL
170 match2.in_port = of_ports[0]
ShreyaPandita60e45542012-09-27 15:11:16 -0400171
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400172 msg2 = message.flow_mod()
173 msg2.out_port = ofp.OFPP_NONE
174 msg2.command = ofp.OFPFC_ADD
175 msg2.buffer_id = 0xffffffff
176 msg2.match = match2
177 act2 = action.action_output()
178 act2.port = of_ports[1]
179 self.assertTrue(msg2.actions.add(act2), "could not add action")
ShreyaPanditaed209962012-11-04 02:16:48 -0500180 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400181 msg2.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400182
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400183 rv = self.controller.message_send(msg2)
184 self.assertTrue(rv != -1, "Error installing flow mod")
185 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPandita60e45542012-09-27 15:11:16 -0400186
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400187 return (pkt_wildcard,match2)
ShreyaPandita60e45542012-09-27 15:11:16 -0400188
ShreyaPanditaed209962012-11-04 02:16:48 -0500189def wildcard_all_except_ingress(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400190# Generate Wildcard_All_Except_Ingress_port flow
191
192
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400193 #Create a simple tcp packet and generate wildcard all except ingress_port flow.
194 pkt_matchingress = simple_tcp_packet()
195 match3 = parse.packet_to_flow_match(pkt_matchingress)
196 self.assertTrue(match3 is not None, "Could not generate flow match from pkt")
197 match3.wildcards = ofp.OFPFW_ALL-ofp.OFPFW_IN_PORT
198 match3.in_port = of_ports[0]
ShreyaPandita60e45542012-09-27 15:11:16 -0400199
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400200 msg3 = message.flow_mod()
201 msg3.command = ofp.OFPFC_ADD
202 msg3.match = match3
203 msg3.out_port = of_ports[2] # ignored by flow add,flow modify
204 msg3.cookie = random.randint(0,9007199254740992)
205 msg3.buffer_id = 0xffffffff
206 msg3.idle_timeout = 0
207 msg3.hard_timeout = 0
208 msg3.buffer_id = 0xffffffff
ShreyaPandita60e45542012-09-27 15:11:16 -0400209
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400210 act3 = action.action_output()
211 act3.port = of_ports[1]
212 self.assertTrue(msg3.actions.add(act3), "could not add action")
ShreyaPandita60e45542012-09-27 15:11:16 -0400213
ShreyaPanditaed209962012-11-04 02:16:48 -0500214 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400215 msg3.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400216
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400217 rv = self.controller.message_send(msg3)
218 self.assertTrue(rv != -1, "Error installing flow mod")
219 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPandita60e45542012-09-27 15:11:16 -0400220
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400221 return (pkt_matchingress,match3)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400222
ShreyaPanditaed209962012-11-04 02:16:48 -0500223def wildcard_all_except_ingress1(self,of_ports,priority=None):
ShreyaPanditada75f752012-10-26 16:26:35 -0400224# Generate Wildcard_All_Except_Ingress_port flow with action output to port egress_port 2
225
226
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400227 #Create a simple tcp packet and generate wildcard all except ingress_port flow.
228 pkt_matchingress = simple_tcp_packet()
229 match3 = parse.packet_to_flow_match(pkt_matchingress)
230 self.assertTrue(match3 is not None, "Could not generate flow match from pkt")
231 match3.wildcards = ofp.OFPFW_ALL-ofp.OFPFW_IN_PORT
232 match3.in_port = of_ports[0]
ShreyaPanditada75f752012-10-26 16:26:35 -0400233
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400234 msg3 = message.flow_mod()
235 msg3.command = ofp.OFPFC_ADD
236 msg3.match = match3
237 msg3.out_port = of_ports[2] # ignored by flow add,flow modify
238 msg3.cookie = random.randint(0,9007199254740992)
239 msg3.buffer_id = 0xffffffff
240 msg3.idle_timeout = 0
241 msg3.hard_timeout = 0
242 msg3.buffer_id = 0xffffffff
ShreyaPanditada75f752012-10-26 16:26:35 -0400243
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400244 act3 = action.action_output()
245 act3.port = of_ports[2]
246 self.assertTrue(msg3.actions.add(act3), "could not add action")
ShreyaPanditaed209962012-11-04 02:16:48 -0500247 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400248 msg3.priority = priority
ShreyaPanditada75f752012-10-26 16:26:35 -0400249
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400250 rv = self.controller.message_send(msg3)
251 self.assertTrue(rv != -1, "Error installing flow mod")
252 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPanditada75f752012-10-26 16:26:35 -0400253
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400254 return (pkt_matchingress,match3)
ShreyaPanditada75f752012-10-26 16:26:35 -0400255
256
ShreyaPanditaed209962012-11-04 02:16:48 -0500257def match_vlan_id(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400258 #Generate Match_Vlan_Id
259
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400260 #Create a simple tcp packet and generate match on ethernet dst address flow
261 pkt_matchvlanid = simple_tcp_packet(dl_vlan_enable=True,dl_vlan=1)
262 match = parse.packet_to_flow_match(pkt_matchvlanid)
263 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400264
ShreyaPandita6fbff252012-11-13 16:56:48 -0500265 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_DL_VLAN
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400266 msg = message.flow_mod()
267 msg.out_port = ofp.OFPP_NONE
268 msg.command = ofp.OFPFC_ADD
269 msg.buffer_id = 0xffffffff
270 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500271 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400272 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400273
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400274 act = action.action_output()
275 act.port = of_ports[1]
276 self.assertTrue(msg.actions.add(act), "could not add action")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400277
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400278 rv = self.controller.message_send(msg)
279 self.assertTrue(rv != -1, "Error installing flow mod")
280 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400281
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400282 return (pkt_matchvlanid,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400283
ShreyaPanditaed209962012-11-04 02:16:48 -0500284def match_vlan_pcp(self,of_ports,priority=None):
ShreyaPandita055102a2012-11-28 11:43:45 -0500285 #Generate Match_Vlan_Priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400286
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400287 #Create a simple tcp packet and generate match on ethernet dst address flow
288 pkt_matchvlanpcp = simple_tcp_packet(dl_vlan_enable=True,dl_vlan=1,dl_vlan_pcp=10)
289 match = parse.packet_to_flow_match(pkt_matchvlanpcp)
290 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400291
ShreyaPandita6fbff252012-11-13 16:56:48 -0500292 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE^ofp.OFPFW_DL_VLAN^ofp.OFPFW_DL_VLAN_PCP
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400293 msg = message.flow_mod()
294 msg.out_port = ofp.OFPP_NONE
295 msg.command = ofp.OFPFC_ADD
296 msg.buffer_id = 0xffffffff
297 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500298 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400299 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400300
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400301 act = action.action_output()
302 act.port = of_ports[1]
303 self.assertTrue(msg.actions.add(act), "could not add action")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400304
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400305 rv = self.controller.message_send(msg)
306 self.assertTrue(rv != -1, "Error installing flow mod")
307 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400308
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400309 return (pkt_matchvlanpcp,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400310
311
ShreyaPanditaed209962012-11-04 02:16:48 -0500312def match_mul_l2(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400313 #Generate Match_Mul_L2 flow
314
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400315 #Create a simple eth packet and generate match on ethernet protocol flow
316 pkt_mulL2 = simple_eth_packet(dl_type=0x88cc,dl_src='00:01:01:01:01:01',dl_dst='00:01:01:01:01:02')
317 match = parse.packet_to_flow_match(pkt_mulL2)
318 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400319
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400320 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_DL_DST ^ofp.OFPFW_DL_SRC
321 msg = message.flow_mod()
322 msg.out_port = ofp.OFPP_NONE
323 msg.command = ofp.OFPFC_ADD
324 msg.buffer_id = 0xffffffff
325 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500326 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400327 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400328
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400329 act = action.action_output()
330 act.port = of_ports[1]
331 self.assertTrue(msg.actions.add(act), "could not add action")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400332
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400333 rv = self.controller.message_send(msg)
334 self.assertTrue(rv != -1, "Error installing flow mod")
335 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400336
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400337 return (pkt_mulL2,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400338
339
ShreyaPandita6fbff252012-11-13 16:56:48 -0500340def match_mul_l4(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400341 #Generate Match_Mul_L4 flow
342
343 #Create a simple tcp packet and generate match on tcp protocol flow
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400344 pkt_mulL4 = simple_tcp_packet(tcp_sport=111,tcp_dport=112)
345 match = parse.packet_to_flow_match(pkt_mulL4)
346 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita6fbff252012-11-13 16:56:48 -0500347 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO^ofp.OFPFW_TP_SRC ^ofp.OFPFW_TP_DST
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400348 msg = message.flow_mod()
349 msg.out_port = ofp.OFPP_NONE
350 msg.command = ofp.OFPFC_ADD
351 msg.buffer_id = 0xffffffff
352 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500353 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400354 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400355
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400356 act = action.action_output()
357 act.port = of_ports[1]
358 self.assertTrue(msg.actions.add(act), "could not add action")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400359
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400360 rv = self.controller.message_send(msg)
361 self.assertTrue(rv != -1, "Error installing flow mod")
362 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400363
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400364 return (pkt_mulL4,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400365
ShreyaPanditaed209962012-11-04 02:16:48 -0500366def match_ip_tos(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400367 #Generate a Match on IP Type of service flow
368
369 #Create a simple tcp packet and generate match on Type of service
ShreyaPandita6fbff252012-11-13 16:56:48 -0500370 pkt_iptos = simple_tcp_packet(ip_tos=30)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400371 match = parse.packet_to_flow_match(pkt_iptos)
372 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400373
ShreyaPandita6fbff252012-11-13 16:56:48 -0500374 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_NW_TOS
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400375 msg = message.flow_mod()
376 msg.out_port = ofp.OFPP_NONE
377 msg.command = ofp.OFPFC_ADD
378 msg.buffer_id = 0xffffffff
379 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500380 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400381 msg.priority = priority
382 act = action.action_output()
383 act.port = of_ports[1]
384 self.assertTrue(msg.actions.add(act), "could not add action")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400385
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400386 rv = self.controller.message_send(msg)
387 self.assertTrue(rv != -1, "Error installing flow mod")
388 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400389
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400390 return (pkt_iptos,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400391
ShreyaPandita055102a2012-11-28 11:43:45 -0500392def match_ip_src(self,of_ports,priority=None):
393 #Generate a Match on IP Type of service flow
394
395 #Create a simple tcp packet and generate match on Type of service
396 pkt_iptos = simple_tcp_packet(ip_src='148.165.130.66')
397 match = parse.packet_to_flow_match(pkt_iptos)
398 self.assertTrue(match is not None, "Could not generate flow match from pkt")
399
400 ofp.OFPFW_NW_SRC_BITS = 32
401 print hex(ofp.OFPFW_NW_SRC_BITS)
402 ofp.OFPFW_NW_SRC_MASK = ((1 << ofp.OFPFW_NW_SRC_BITS) - 1) << ofp.OFPFW_NW_SRC_SHIFT
403 print hex(ofp.OFPFW_NW_SRC_MASK)
404 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_NW_SRC_MASK
405 print hex(match.wildcards)
406
407 msg = message.flow_mod()
408 msg.out_port = ofp.OFPP_NONE
409 msg.command = ofp.OFPFC_ADD
410 msg.buffer_id = 0xffffffff
411 msg.match = match
412 if priority != None :
413 msg.priority = priority
414 act = action.action_output()
415 act.port = of_ports[1]
416 self.assertTrue(msg.actions.add(act), "could not add action")
417
418 rv = self.controller.message_send(msg)
419 self.assertTrue(rv != -1, "Error installing flow mod")
420 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
421
422 return (pkt_iptos,match)
423
424
425
426
427
428
ShreyaPandita6fbff252012-11-13 16:56:48 -0500429def match_ip_protocol(self,of_ports,priority=None):
430 #Generate a Match on IP Protocol
431
432 #Create a simple tcp packet and generate match on Type of service
433 pkt_iptos = simple_tcp_packet()
434 match = parse.packet_to_flow_match(pkt_iptos)
435 self.assertTrue(match is not None, "Could not generate flow match from pkt")
436
437 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO
438 msg = message.flow_mod()
439 msg.out_port = ofp.OFPP_NONE
440 msg.command = ofp.OFPFC_ADD
441 msg.buffer_id = 0xffffffff
442 msg.match = match
443 if priority != None :
444 msg.priority = priority
445 act = action.action_output()
446 act.port = of_ports[1]
447 self.assertTrue(msg.actions.add(act), "could not add action")
448
449 rv = self.controller.message_send(msg)
450 self.assertTrue(rv != -1, "Error installing flow mod")
451 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
452
453 return (pkt_iptos,match)
454
455
ShreyaPanditaed209962012-11-04 02:16:48 -0500456def match_tcp_src(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400457 #Generate Match_Tcp_Src
458
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400459 #Create a simple tcp packet and generate match on tcp source port flow
460 pkt_matchtSrc = simple_tcp_packet(tcp_sport=111)
461 match = parse.packet_to_flow_match(pkt_matchtSrc)
462 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400463
ShreyaPandita6fbff252012-11-13 16:56:48 -0500464 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_SRC
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400465 msg = message.flow_mod()
466 msg.out_port = ofp.OFPP_NONE
467 msg.command = ofp.OFPFC_ADD
468 msg.buffer_id = 0xffffffff
469 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500470 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400471 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400472
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400473 act = action.action_output()
474 act.port = of_ports[1]
475 self.assertTrue(msg.actions.add(act), "could not add action")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400476
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400477 rv = self.controller.message_send(msg)
478 self.assertTrue(rv != -1, "Error installing flow mod")
479 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400480
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400481 return (pkt_matchtSrc,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400482
ShreyaPanditaed209962012-11-04 02:16:48 -0500483def match_tcp_dst(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400484 #Generate Match_Tcp_Dst
485
486 #Create a simple tcp packet and generate match on tcp destination port flow
ShreyaPandita6fbff252012-11-13 16:56:48 -0500487 pkt_matchdst = simple_tcp_packet(tcp_dport=112)
488 match = parse.packet_to_flow_match(pkt_matchdst)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400489 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400490
ShreyaPandita6fbff252012-11-13 16:56:48 -0500491 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO^ofp.OFPFW_TP_DST
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400492 msg = message.flow_mod()
493 msg.out_port = ofp.OFPP_NONE
494 msg.command = ofp.OFPFC_ADD
495 msg.buffer_id = 0xffffffff
496 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500497 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400498 msg.priority = priority
499 act = action.action_output()
500 act.port = of_ports[1]
501 self.assertTrue(msg.actions.add(act), "could not add action")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400502
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400503 rv = self.controller.message_send(msg)
504 self.assertTrue(rv != -1, "Error installing flow mod")
505 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400506
ShreyaPandita6fbff252012-11-13 16:56:48 -0500507 return (pkt_matchdst,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400508
ShreyaPandita055102a2012-11-28 11:43:45 -0500509
ShreyaPanditaed209962012-11-04 02:16:48 -0500510def match_ethernet_type(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400511 #Generate a Match_Ethernet_Type flow
512
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400513 #Create a simple tcp packet and generate match on ethernet type flow
514 pkt_matchtype = simple_eth_packet(dl_type=0x88cc)
515 match = parse.packet_to_flow_match(pkt_matchtype)
516 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400517
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400518 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE
519 msg = message.flow_mod()
520 msg.out_port = ofp.OFPP_NONE
521 msg.command = ofp.OFPFC_ADD
522 msg.buffer_id = 0xffffffff
523 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500524 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400525 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400526
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400527 act = action.action_output()
528 act.port = of_ports[1]
529 self.assertTrue(msg.actions.add(act), "could not add action")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400530
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400531 rv = self.controller.message_send(msg)
532 self.assertTrue(rv != -1, "Error installing flow mod")
533 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400534 return (pkt_matchtype,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400535
536
ShreyaPandita60e45542012-09-27 15:11:16 -0400537
ShreyaPandita6fbff252012-11-13 16:56:48 -0500538
ShreyaPanditaed209962012-11-04 02:16:48 -0500539def strict_modify_flow_action(self,egress_port,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400540# Strict Modify the flow Action
541
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400542 #Create a flow_mod message , command MODIFY_STRICT
543 msg5 = message.flow_mod()
544 msg5.match = match
545 msg5.cookie = random.randint(0,9007199254740992)
546 msg5.command = ofp.OFPFC_MODIFY_STRICT
547 msg5.buffer_id = 0xffffffff
548 act5 = action.action_output()
549 act5.port = egress_port
550 self.assertTrue(msg5.actions.add(act5), "could not add action")
ShreyaPandita60e45542012-09-27 15:11:16 -0400551
ShreyaPanditaed209962012-11-04 02:16:48 -0500552 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400553 msg5.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400554
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400555 # Send the flow with action A'
556 rv = self.controller.message_send (msg5)
557 self.assertTrue(rv != -1, "Error installing flow mod")
558 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPandita60e45542012-09-27 15:11:16 -0400559
ShreyaPanditaed209962012-11-04 02:16:48 -0500560def modify_flow_action(self,of_ports,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400561# Modify the flow action
562
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400563 #Create a flow_mod message , command MODIFY
564 msg8 = message.flow_mod()
565 msg8.match = match
566 msg8.cookie = random.randint(0,9007199254740992)
567 msg8.command = ofp.OFPFC_MODIFY
568 #out_port will be ignored for flow adds and flow modify (here for test-case Add_Modify_With_Outport)
569 msg8.out_port = of_ports[3]
570 msg8.buffer_id = 0xffffffff
571 act8 = action.action_output()
572 act8.port = of_ports[2]
573 self.assertTrue(msg8.actions.add(act8), "could not add action")
ShreyaPandita60e45542012-09-27 15:11:16 -0400574
ShreyaPanditaed209962012-11-04 02:16:48 -0500575 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400576 msg8.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400577
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400578 # Send the flow with action A'
579 rv = self.controller.message_send (msg8)
580 self.assertTrue(rv != -1, "Error installing flow mod")
581 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPandita60e45542012-09-27 15:11:16 -0400582
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400583def enqueue(self,ingress_port,egress_port,egress_queue_id):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400584#Generate a flow with enqueue action i.e output to a queue configured on a egress_port
585
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400586 pkt = simple_tcp_packet()
587 match = packet_to_flow_match(self, pkt)
588 match.wildcards &= ~ofp.OFPFW_IN_PORT
589 self.assertTrue(match is not None,
590 "Could not generate flow match from pkt")
591
592 match.in_port = ingress_port
593 request = message.flow_mod()
594 request.match = match
595 request.buffer_id = 0xffffffff
596 act = action.action_enqueue()
597 act.port = egress_port
598 act.queue_id = egress_queue_id
599 self.assertTrue(request.actions.add(act), "Could not add action")
600
601 logging.info("Inserting flow")
602 rv = self.controller.message_send(request)
603 self.assertTrue(rv != -1, "Error installing flow mod")
604 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
605 return (pkt,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400606
607
ShreyaPandita60e45542012-09-27 15:11:16 -0400608########################### Verify Stats Functions ###########################################################################################
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400609def get_flowstats(self,match):
610 # Generate flow_stats request
611
612 stat_req = message.flow_stats_request()
613 stat_req.match = match
614 stat_req.table_id = 0xff
615 stat_req.out_port = ofp.OFPP_NONE
616
617 logging.info("Sending stats request")
618 response, pkt = self.controller.transact(stat_req,
619 timeout=5)
620 self.assertTrue(response is not None,"No response to stats request")
ShreyaPandita60e45542012-09-27 15:11:16 -0400621
ShreyaPandita66de26f2012-10-26 14:44:24 -0400622
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400623def get_portstats(self,port_num):
624
625# Return all the port counters in the form a tuple
626 port_stats_req = message.port_stats_request()
627 port_stats_req.port_no = port_num
628 response,pkt = self.controller.transact(port_stats_req)
629 self.assertTrue(response is not None,"No response received for port stats request")
630 rx_pkts=0
631 tx_pkts=0
632 rx_byts=0
633 tx_byts=0
634 rx_drp =0
635 tx_drp = 0
636 rx_err=0
637 tx_err =0
638 rx_fr_err=0
639 rx_ovr_err=0
640 rx_crc_err=0
641 collisions = 0
642 tx_err=0
643
644
645 for obj in response.stats:
646 rx_pkts += obj.rx_packets
647 tx_pkts += obj.tx_packets
648 rx_byts += obj.rx_bytes
649 tx_byts += obj.tx_bytes
650 rx_drp += obj.rx_dropped
651 tx_drp += obj.tx_dropped
652 rx_err += obj.rx_errors
653 rx_fr_err += obj.rx_frame_err
654 rx_ovr_err += obj.rx_over_err
655 rx_crc_err += obj.rx_crc_err
656 collisions+= obj.collisions
657 tx_err += obj.tx_errors
658
659 return (rx_pkts,tx_pkts,rx_byts,tx_byts,rx_drp,tx_drp,rx_err,tx_err,rx_fr_err,rx_ovr_err,rx_crc_err,collisions,tx_err)
660
661def get_queuestats(self,port_num,queue_id):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400662#Generate Queue Stats request
663
664 request = message.queue_stats_request()
665 request.port_no = port_num
666 request.queue_id = queue_id
667 (queue_stats, p) = self.controller.transact(request)
668 self.assertNotEqual(queue_stats, None, "Queue stats request failed")
669
670 return (queue_stats,p)
671
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400672def get_tablestats(self):
673# Send Table_Stats request (retrieve current table counters )
674
675 stat_req = message.table_stats_request()
676 response, pkt = self.controller.transact(stat_req,
677 timeout=5)
678 self.assertTrue(response is not None,
679 "No response to stats request")
680 current_lookedup = 0
681 current_matched = 0
682 current_active = 0
683
684 for obj in response.stats:
685 current_lookedup += obj.lookup_count
686 current_matched += obj.matched_count
687 current_active += obj.active_count
688
689 return (current_lookedup,current_matched,current_active)
690
691
692
ShreyaPanditaed209962012-11-04 02:16:48 -0500693def verify_tablestats(self,expect_lookup=None,expect_match=None,expect_active=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400694
695 stat_req = message.table_stats_request()
ShreyaPanditaed209962012-11-04 02:16:48 -0500696
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400697 for i in range(0,60):
ShreyaPandita60e45542012-09-27 15:11:16 -0400698
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400699 logging.info("Sending stats request")
700 # TODO: move REPLY_MORE handling to controller.transact?
ShreyaPandita66de26f2012-10-26 14:44:24 -0400701 response, pkt = self.controller.transact(stat_req,
702 timeout=5)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400703 self.assertTrue(response is not None,"No response to stats request")
ShreyaPandita60e45542012-09-27 15:11:16 -0400704
ShreyaPanditaed209962012-11-04 02:16:48 -0500705 lookedup = 0
706 matched = 0
707 active = 0
ShreyaPanditaa6dfbfc2012-11-05 17:45:22 -0500708
709 sleep(1)
ShreyaPanditaed209962012-11-04 02:16:48 -0500710
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400711 for item in response.stats:
ShreyaPanditaed209962012-11-04 02:16:48 -0500712
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400713 lookedup += item.lookup_count
714 matched += item.matched_count
715 active += item.active_count
716
717 logging.info("Packets Looked up " + str(lookedup) + " packets")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400718 logging.info("Packets matched " + str(matched) + "packets")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400719 logging.info("Active flow entries" + str(active) + "flows")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400720
ShreyaPanditaed209962012-11-04 02:16:48 -0500721 if expect_lookup != None and expect_lookup != lookedup:continue
722 if expect_match != None and expect_match != matched:continue
723 if expect_active != None and expect_active != active:continue
724 break
725
ShreyaPanditaa6dfbfc2012-11-05 17:45:22 -0500726
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400727
ShreyaPanditaed209962012-11-04 02:16:48 -0500728 if expect_lookup != None :
729 self.assertEqual(expect_lookup,item.lookup_count,"lookup counter is not incremented properly")
730 if expect_match != None :
731 self.assertEqual(expect_match,item.matched_count, "matched counter is not incremented properly")
732 if expect_active != None :
733 self.assertEqual(expect_active,item.active_count,"active counter is not incremented properly")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400734
735
ShreyaPanditaed209962012-11-04 02:16:48 -0500736def verify_flowstats(self,match,byte_count=None,packet_count=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400737 # Verify flow counters : byte_count and packet_count
ShreyaPandita60e45542012-09-27 15:11:16 -0400738
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400739 stat_req = message.flow_stats_request()
740 stat_req.match = match
741 stat_req.table_id = 0xff
742 stat_req.out_port = ofp.OFPP_NONE
ShreyaPanditaed209962012-11-04 02:16:48 -0500743
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400744 for i in range(0,60):
745 logging.info("Sending stats request")
746 # TODO: move REPLY_MORE handling to controller.transact?
ShreyaPandita66de26f2012-10-26 14:44:24 -0400747 response, pkt = self.controller.transact(stat_req,
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400748 timeout=5)
749 self.assertTrue(response is not None,"No response to stats request")
750
ShreyaPanditaed209962012-11-04 02:16:48 -0500751 packet_counter = 0
752 byte_counter = 0
753
ShreyaPanditaa6dfbfc2012-11-05 17:45:22 -0500754 sleep(1)
755
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400756 for item in response.stats:
757 packet_counter += item.packet_count
758 byte_counter += item.byte_count
759
760 logging.info("Recieved" + str(item.packet_count) + " packets")
ShreyaPanditaed209962012-11-04 02:16:48 -0500761
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400762 logging.info("Received " + str(item.byte_count) + "bytes")
ShreyaPanditaed209962012-11-04 02:16:48 -0500763
764 if packet_count != None and packet_count != packet_counter: continue
765 if byte_count != None and byte_count != byte_counter: continue
766 break
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400767
ShreyaPanditaa6dfbfc2012-11-05 17:45:22 -0500768
ShreyaPanditaed209962012-11-04 02:16:48 -0500769
770 if packet_count != None :
771 self.assertEqual(packet_count,item.packet_count,"packet_count counter is not incremented correctly")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400772
ShreyaPanditaed209962012-11-04 02:16:48 -0500773 if byte_count != None :
774 self.assertEqual(byte_count,item.byte_count,"byte_count counter is not incremented correctly")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400775
776
ShreyaPanditaed209962012-11-04 02:16:48 -0500777def verify_portstats(self, port,tx_packets=None,rx_packets=None,rx_byte=None,tx_byte=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400778
779
780 stat_req = message.port_stats_request()
781 stat_req.port_no = port
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400782
783 for i in range(0,60):
784 logging.info("Sending stats request")
785 response, pkt = self.controller.transact(stat_req,
786 timeout=5)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400787 self.assertTrue(response is not None,
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400788 "No response to stats request")
789 self.assertTrue(len(response.stats) == 1,
790 "Did not receive port stats reply")
ShreyaPanditaed209962012-11-04 02:16:48 -0500791
792 sentp = recvp = 0
793 sentb = recvb = 0
ShreyaPanditaa6dfbfc2012-11-05 17:45:22 -0500794
795 sleep(1)
ShreyaPanditaed209962012-11-04 02:16:48 -0500796
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400797 for item in response.stats:
798 sentp += item.tx_packets
799 recvp += item.rx_packets
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400800 recvb += item.rx_bytes
ShreyaPanditaed209962012-11-04 02:16:48 -0500801 sentb += item.tx_bytes
802
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400803
804 logging.info("Sent " + str(sentp) + " packets")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400805 logging.info("Received " + str(recvp) + " packets")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400806 logging.info("Received " + str(recvb) + "bytes")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400807 logging.info("Sent" + str(sentb) + "bytes")
ShreyaPanditaed209962012-11-04 02:16:48 -0500808
809 if tx_packets != None and tx_packets != sentp: continue
810 if rx_packets != None and rx_packets != recvp: continue
811 if rx_byte != None and rx_byte != recvb: continue
812 if tx_byte != None and tx_byte != sentb: continue
813
814 break
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400815
ShreyaPanditaa6dfbfc2012-11-05 17:45:22 -0500816
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400817
ShreyaPanditaed209962012-11-04 02:16:48 -0500818 if (tx_packets != None):
819 self.assertEqual(tx_packets,item.tx_packets,"rx_packets counter is not incremented correctly")
820 if (rx_packets != None):
821 self.assertEqual(rx_packets,item.rx_packets,"tx_packets counter is not incremented correctly")
822 if (rx_byte != None):
823 self.assertEqual(rx_byte,item.rx_bytes,"rx_bytes counter is not incremented correctly")
824 if (tx_byte != None):
825 self.assertEqual(tx_byte,item.tx_bytes,"tx_bytes counter is not incremented correctly")
ShreyaPandita60e45542012-09-27 15:11:16 -0400826
ShreyaPandita60e45542012-09-27 15:11:16 -0400827
ShreyaPanditaed209962012-11-04 02:16:48 -0500828def verify_queuestats(self,port_num,queue_id,expect_packet=None,expect_byte=None):
829
830 # Verify queue counters : tx_packets and tx_bytes
831
832 request = message.queue_stats_request()
833 request.port_no = port_num
834 request.queue_id = queue_id
835
836 for i in range(0,60):
837
838 logging.info("Sending stats request")
839
840 (queue_stats, p) = self.controller.transact(request)
841 self.assertNotEqual(queue_stats, None, "Queue stats request failed")
842 packet_counter = 0
843 byte_counter = 0
ShreyaPanditaa6dfbfc2012-11-05 17:45:22 -0500844
845 sleep(1)
ShreyaPanditaed209962012-11-04 02:16:48 -0500846
ShreyaPanditaa6dfbfc2012-11-05 17:45:22 -0500847 for item in queue_stats.stats:
ShreyaPanditaed209962012-11-04 02:16:48 -0500848 packet_counter += item.tx_packets
849 byte_counter += item.tx_bytes
850
851 logging.info("Transmitted" + str(packet_counter) + " packets")
852 logging.info("Transmitted" + str(byte_counter) + "bytes")
853
854 if expect_packet != None and packet_counter != expect_packet: continue
855 if expect_byte != None and byte_counter != expect_byte: continue
856 break
857
ShreyaPanditaa6dfbfc2012-11-05 17:45:22 -0500858
ShreyaPanditaed209962012-11-04 02:16:48 -0500859
860 if expect_packet != None :
861 self.assertEqual(packet_counter,expect_packet,"tx_packets counter is not incremented correctly")
862
863 if expect_byte != None :
864 self.assertEqual(byte_counter,expect_byte,"tx_bytes counter is not incremented correctly")
865
866
ShreyaPandita60e45542012-09-27 15:11:16 -0400867############################## Various delete commands #############################################################################################
868
ShreyaPanditaed209962012-11-04 02:16:48 -0500869def strict_delete(self,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400870# Issue Strict Delete
871
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400872 #Create flow_mod message, command DELETE_STRICT
873 msg4 = message.flow_mod()
874 msg4.out_port = ofp.OFPP_NONE
875 msg4.command = ofp.OFPFC_DELETE_STRICT
876 msg4.buffer_id = 0xffffffff
877 msg4.match = match
ShreyaPandita60e45542012-09-27 15:11:16 -0400878
ShreyaPanditaed209962012-11-04 02:16:48 -0500879 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400880 msg4.priority = priority
881 rv = self.controller.message_send(msg4)
882 self.assertTrue(rv!= -1, "Error installing flow mod")
883 self.assertEqual(do_barrier(self.controller), 0, "Barrier failed")
ShreyaPandita60e45542012-09-27 15:11:16 -0400884
885
886
ShreyaPanditaed209962012-11-04 02:16:48 -0500887def nonstrict_delete(self,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400888# Issue Non_Strict Delete
889
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400890 #Create flow_mod message, command DELETE
891 msg6 = message.flow_mod()
892 msg6.out_port = ofp.OFPP_NONE
893 msg6.command = ofp.OFPFC_DELETE
894 msg6.buffer_id = 0xffffffff
895 msg6.match = match
ShreyaPandita60e45542012-09-27 15:11:16 -0400896
ShreyaPanditaed209962012-11-04 02:16:48 -0500897 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400898 msg6.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400899
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400900 rv = self.controller.message_send(msg6)
901 self.assertTrue(rv != -1, "Error installing flow mod")
902 self.assertEqual(do_barrier(self.controller),0, "Barrier failed")
ShreyaPandita60e45542012-09-27 15:11:16 -0400903
904
905###########################################################################################################################################################
906
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400907def send_packet(obj, pkt, ingress_port, egress_port):
ShreyaPandita60e45542012-09-27 15:11:16 -0400908#Send Packets on a specified ingress_port and verify if its recieved on correct egress_port.
909
910 obj.dataplane.send(ingress_port, str(pkt))
911 exp_pkt_arg = pkt
912 exp_port = egress_port
913
914 (rcv_port, rcv_pkt, pkt_time) = obj.dataplane.poll(timeout=2,
915 port_number=exp_port,
916 exp_pkt=exp_pkt_arg)
917 obj.assertTrue(rcv_pkt is not None,
918 "Packet not received on port " + str(egress_port))
919 obj.assertEqual(rcv_port, egress_port,
920 "Packet received on port " + str(rcv_port) +
921 ", expected port " + str(egress_port))
922 obj.assertEqual(str(pkt), str(rcv_pkt),
923 'Response packet does not match send packet')
924
925
ShreyaPandita572e64b2012-09-28 14:41:06 -0400926def sw_supported_actions(parent,use_cache=False):
ShreyaPandita60e45542012-09-27 15:11:16 -0400927#Returns the switch's supported actions
928
929 cache_supported_actions = None
930 if cache_supported_actions is None or not use_cache:
931 request = message.features_request()
932 (reply, pkt) = parent.controller.transact(request)
933 parent.assertTrue(reply is not None, "Did not get response to ftr req")
934 cache_supported_actions = reply.actions
935 return cache_supported_actions
936
ShreyaPandita66de26f2012-10-26 14:44:24 -0400937##############################################################################################################################################################
938