blob: c1d53434cf662771c1f4c0b92fdcc79f521dff84 [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]
Rich Lanee30455b2013-01-03 16:24:44 -080042 msg.actions.add(act)
ShreyaPandita60e45542012-09-27 15:11:16 -040043
Rich Lane5c3151c2013-01-03 17:15:41 -080044 self.controller.message_send(msg)
Rich Lane3a261d52013-01-03 17:45:08 -080045 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -040046
ShreyaPandita4ebbac32012-11-02 13:40:44 -040047 return (pkt_exactflow,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -040048
ShreyaPanditaed209962012-11-04 02:16:48 -050049def exact_match_with_prio(self,of_ports,priority=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -040050 # Generate ExactMatch with action output to port 2
ShreyaPandita66de26f2012-10-26 14:44:24 -040051
ShreyaPandita4ebbac32012-11-02 13:40:44 -040052 #Create a simple tcp packet and generate exact flow match from it.
53 pkt_exactflow = simple_tcp_packet()
54 match = parse.packet_to_flow_match(pkt_exactflow)
55 self.assertTrue(match is not None, "Could not generate flow match from pkt")
56 match.in_port = of_ports[0]
57 #match.nw_src = 1
58 match.wildcards=0
59 msg = message.flow_mod()
60 msg.out_port = ofp.OFPP_NONE
61 msg.command = ofp.OFPFC_ADD
62 msg.buffer_id = 0xffffffff
63 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -050064 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -040065 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -040066
ShreyaPandita4ebbac32012-11-02 13:40:44 -040067 act = action.action_output()
68 act.port = of_ports[2]
Rich Lanee30455b2013-01-03 16:24:44 -080069 msg.actions.add(act)
ShreyaPandita66de26f2012-10-26 14:44:24 -040070
Rich Lane5c3151c2013-01-03 17:15:41 -080071 self.controller.message_send(msg)
Rich Lane3a261d52013-01-03 17:45:08 -080072 do_barrier(self.controller)
ShreyaPandita66de26f2012-10-26 14:44:24 -040073
ShreyaPandita4ebbac32012-11-02 13:40:44 -040074 return (pkt_exactflow,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -040075
ShreyaPandita60e45542012-09-27 15:11:16 -040076
ShreyaPanditaed209962012-11-04 02:16:48 -050077def match_all_except_source_address(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -040078# Generate Match_All_Except_Source_Address flow
79
ShreyaPandita4ebbac32012-11-02 13:40:44 -040080 #Create a simple tcp packet and generate match all except src address flow.
81 pkt_wildcardsrc= simple_tcp_packet()
82 match1 = parse.packet_to_flow_match(pkt_wildcardsrc)
83 self.assertTrue(match1 is not None, "Could not generate flow match from pkt")
84 match1.in_port = of_ports[0]
85 #match1.nw_src = 1
86 match1.wildcards = ofp.OFPFW_DL_SRC
87 msg1 = message.flow_mod()
88 msg1.out_port = ofp.OFPP_NONE
89 msg1.command = ofp.OFPFC_ADD
90 msg1.buffer_id = 0xffffffff
91 msg1.match = match1
ShreyaPanditaed209962012-11-04 02:16:48 -050092 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -040093 msg1.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -040094
ShreyaPandita4ebbac32012-11-02 13:40:44 -040095 act1 = action.action_output()
96 act1.port = of_ports[1]
Rich Lanee30455b2013-01-03 16:24:44 -080097 msg1.actions.add(act1)
ShreyaPandita60e45542012-09-27 15:11:16 -040098
Rich Lane5c3151c2013-01-03 17:15:41 -080099 self.controller.message_send(msg1)
Rich Lane3a261d52013-01-03 17:45:08 -0800100 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400101
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400102 return (pkt_wildcardsrc,match1)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400103
ShreyaPandita6fbff252012-11-13 16:56:48 -0500104def match_ethernet_src_address(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400105 #Generate Match_Ethernet_SrC_Address flow
106
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400107 #Create a simple tcp packet and generate match on ethernet src address flow
ShreyaPandita6fbff252012-11-13 16:56:48 -0500108 pkt_MatchSrc = simple_eth_packet(dl_src='00:01:01:01:01:01')
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400109 match = parse.packet_to_flow_match(pkt_MatchSrc)
110 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400111
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400112 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_SRC
ShreyaPandita60e45542012-09-27 15:11:16 -0400113
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400114 msg = message.flow_mod()
115 msg.out_port = ofp.OFPP_NONE
116 msg.command = ofp.OFPFC_ADD
117 msg.buffer_id = 0xffffffff
118 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500119 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400120 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400121
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400122 act = action.action_output()
123 act.port = of_ports[1]
Rich Lanee30455b2013-01-03 16:24:44 -0800124 msg.actions.add(act)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400125
Rich Lane5c3151c2013-01-03 17:15:41 -0800126 self.controller.message_send(msg)
Rich Lane3a261d52013-01-03 17:45:08 -0800127 do_barrier(self.controller)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400128
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400129 return (pkt_MatchSrc,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400130
ShreyaPanditaed209962012-11-04 02:16:48 -0500131def match_ethernet_dst_address(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400132 #Generate Match_Ethernet_Dst_Address flow
133
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400134 #Create a simple tcp packet and generate match on ethernet dst address flow
ShreyaPandita6fbff252012-11-13 16:56:48 -0500135 pkt_matchdst = simple_eth_packet(dl_dst='00:01:01:01:01:01')
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400136 match = parse.packet_to_flow_match(pkt_matchdst)
137 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400138
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400139 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_DST
140 msg = message.flow_mod()
141 msg.out_port = ofp.OFPP_NONE
142 msg.command = ofp.OFPFC_ADD
143 msg.buffer_id = 0xffffffff
144 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500145 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400146 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400147
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400148 act = action.action_output()
149 act.port = of_ports[1]
Rich Lanee30455b2013-01-03 16:24:44 -0800150 msg.actions.add(act)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400151
Rich Lane5c3151c2013-01-03 17:15:41 -0800152 self.controller.message_send(msg)
Rich Lane3a261d52013-01-03 17:45:08 -0800153 do_barrier(self.controller)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400154
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400155 return (pkt_matchdst,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400156
ShreyaPanditaed209962012-11-04 02:16:48 -0500157def wildcard_all(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400158# Generate a Wildcard_All Flow
159
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400160 #Create a simple tcp packet and generate wildcard all flow match from it.
161 pkt_wildcard = simple_tcp_packet()
162 match2 = parse.packet_to_flow_match(pkt_wildcard)
163 self.assertTrue(match2 is not None, "Could not generate flow match from pkt")
164 match2.wildcards=ofp.OFPFW_ALL
165 match2.in_port = of_ports[0]
ShreyaPandita60e45542012-09-27 15:11:16 -0400166
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400167 msg2 = message.flow_mod()
168 msg2.out_port = ofp.OFPP_NONE
169 msg2.command = ofp.OFPFC_ADD
170 msg2.buffer_id = 0xffffffff
171 msg2.match = match2
172 act2 = action.action_output()
173 act2.port = of_ports[1]
Rich Lanee30455b2013-01-03 16:24:44 -0800174 msg2.actions.add(act2)
ShreyaPanditaed209962012-11-04 02:16:48 -0500175 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400176 msg2.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400177
Rich Lane5c3151c2013-01-03 17:15:41 -0800178 self.controller.message_send(msg2)
Rich Lane3a261d52013-01-03 17:45:08 -0800179 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400180
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400181 return (pkt_wildcard,match2)
ShreyaPandita60e45542012-09-27 15:11:16 -0400182
ShreyaPanditaed209962012-11-04 02:16:48 -0500183def wildcard_all_except_ingress(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400184# Generate Wildcard_All_Except_Ingress_port flow
185
186
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400187 #Create a simple tcp packet and generate wildcard all except ingress_port flow.
188 pkt_matchingress = simple_tcp_packet()
189 match3 = parse.packet_to_flow_match(pkt_matchingress)
190 self.assertTrue(match3 is not None, "Could not generate flow match from pkt")
191 match3.wildcards = ofp.OFPFW_ALL-ofp.OFPFW_IN_PORT
192 match3.in_port = of_ports[0]
ShreyaPandita60e45542012-09-27 15:11:16 -0400193
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400194 msg3 = message.flow_mod()
195 msg3.command = ofp.OFPFC_ADD
196 msg3.match = match3
197 msg3.out_port = of_ports[2] # ignored by flow add,flow modify
198 msg3.cookie = random.randint(0,9007199254740992)
199 msg3.buffer_id = 0xffffffff
200 msg3.idle_timeout = 0
201 msg3.hard_timeout = 0
202 msg3.buffer_id = 0xffffffff
ShreyaPandita60e45542012-09-27 15:11:16 -0400203
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400204 act3 = action.action_output()
205 act3.port = of_ports[1]
Rich Lanee30455b2013-01-03 16:24:44 -0800206 msg3.actions.add(act3)
ShreyaPandita60e45542012-09-27 15:11:16 -0400207
ShreyaPanditaed209962012-11-04 02:16:48 -0500208 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400209 msg3.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400210
Rich Lane5c3151c2013-01-03 17:15:41 -0800211 self.controller.message_send(msg3)
Rich Lane3a261d52013-01-03 17:45:08 -0800212 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400213
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400214 return (pkt_matchingress,match3)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400215
ShreyaPanditaed209962012-11-04 02:16:48 -0500216def wildcard_all_except_ingress1(self,of_ports,priority=None):
ShreyaPanditada75f752012-10-26 16:26:35 -0400217# Generate Wildcard_All_Except_Ingress_port flow with action output to port egress_port 2
218
219
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400220 #Create a simple tcp packet and generate wildcard all except ingress_port flow.
221 pkt_matchingress = simple_tcp_packet()
222 match3 = parse.packet_to_flow_match(pkt_matchingress)
223 self.assertTrue(match3 is not None, "Could not generate flow match from pkt")
224 match3.wildcards = ofp.OFPFW_ALL-ofp.OFPFW_IN_PORT
225 match3.in_port = of_ports[0]
ShreyaPanditada75f752012-10-26 16:26:35 -0400226
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400227 msg3 = message.flow_mod()
228 msg3.command = ofp.OFPFC_ADD
229 msg3.match = match3
230 msg3.out_port = of_ports[2] # ignored by flow add,flow modify
231 msg3.cookie = random.randint(0,9007199254740992)
232 msg3.buffer_id = 0xffffffff
233 msg3.idle_timeout = 0
234 msg3.hard_timeout = 0
235 msg3.buffer_id = 0xffffffff
ShreyaPanditada75f752012-10-26 16:26:35 -0400236
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400237 act3 = action.action_output()
238 act3.port = of_ports[2]
Rich Lanee30455b2013-01-03 16:24:44 -0800239 msg3.actions.add(act3)
ShreyaPanditaed209962012-11-04 02:16:48 -0500240 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400241 msg3.priority = priority
ShreyaPanditada75f752012-10-26 16:26:35 -0400242
Rich Lane5c3151c2013-01-03 17:15:41 -0800243 self.controller.message_send(msg3)
Rich Lane3a261d52013-01-03 17:45:08 -0800244 do_barrier(self.controller)
ShreyaPanditada75f752012-10-26 16:26:35 -0400245
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400246 return (pkt_matchingress,match3)
ShreyaPanditada75f752012-10-26 16:26:35 -0400247
248
ShreyaPanditaed209962012-11-04 02:16:48 -0500249def match_vlan_id(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400250 #Generate Match_Vlan_Id
251
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400252 #Create a simple tcp packet and generate match on ethernet dst address flow
253 pkt_matchvlanid = simple_tcp_packet(dl_vlan_enable=True,dl_vlan=1)
254 match = parse.packet_to_flow_match(pkt_matchvlanid)
255 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400256
ShreyaPandita6fbff252012-11-13 16:56:48 -0500257 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_DL_VLAN
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400258 msg = message.flow_mod()
259 msg.out_port = ofp.OFPP_NONE
260 msg.command = ofp.OFPFC_ADD
261 msg.buffer_id = 0xffffffff
262 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500263 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400264 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400265
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400266 act = action.action_output()
267 act.port = of_ports[1]
Rich Lanee30455b2013-01-03 16:24:44 -0800268 msg.actions.add(act)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400269
Rich Lane5c3151c2013-01-03 17:15:41 -0800270 self.controller.message_send(msg)
Rich Lane3a261d52013-01-03 17:45:08 -0800271 do_barrier(self.controller)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400272
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400273 return (pkt_matchvlanid,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400274
ShreyaPanditaed209962012-11-04 02:16:48 -0500275def match_vlan_pcp(self,of_ports,priority=None):
ShreyaPandita055102a2012-11-28 11:43:45 -0500276 #Generate Match_Vlan_Priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400277
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400278 #Create a simple tcp packet and generate match on ethernet dst address flow
Rich Laneba9eee82012-12-07 22:44:24 -0800279 pkt_matchvlanpcp = simple_tcp_packet(dl_vlan_enable=True,dl_vlan=1,dl_vlan_pcp=5)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400280 match = parse.packet_to_flow_match(pkt_matchvlanpcp)
281 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400282
ShreyaPandita6fbff252012-11-13 16:56:48 -0500283 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE^ofp.OFPFW_DL_VLAN^ofp.OFPFW_DL_VLAN_PCP
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400284 msg = message.flow_mod()
285 msg.out_port = ofp.OFPP_NONE
286 msg.command = ofp.OFPFC_ADD
287 msg.buffer_id = 0xffffffff
288 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500289 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400290 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400291
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400292 act = action.action_output()
293 act.port = of_ports[1]
Rich Lanee30455b2013-01-03 16:24:44 -0800294 msg.actions.add(act)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400295
Rich Lane5c3151c2013-01-03 17:15:41 -0800296 self.controller.message_send(msg)
Rich Lane3a261d52013-01-03 17:45:08 -0800297 do_barrier(self.controller)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400298
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400299 return (pkt_matchvlanpcp,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400300
301
ShreyaPanditaed209962012-11-04 02:16:48 -0500302def match_mul_l2(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400303 #Generate Match_Mul_L2 flow
304
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400305 #Create a simple eth packet and generate match on ethernet protocol flow
306 pkt_mulL2 = simple_eth_packet(dl_type=0x88cc,dl_src='00:01:01:01:01:01',dl_dst='00:01:01:01:01:02')
307 match = parse.packet_to_flow_match(pkt_mulL2)
308 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400309
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400310 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_DL_DST ^ofp.OFPFW_DL_SRC
311 msg = message.flow_mod()
312 msg.out_port = ofp.OFPP_NONE
313 msg.command = ofp.OFPFC_ADD
314 msg.buffer_id = 0xffffffff
315 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500316 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400317 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400318
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400319 act = action.action_output()
320 act.port = of_ports[1]
Rich Lanee30455b2013-01-03 16:24:44 -0800321 msg.actions.add(act)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400322
Rich Lane5c3151c2013-01-03 17:15:41 -0800323 self.controller.message_send(msg)
Rich Lane3a261d52013-01-03 17:45:08 -0800324 do_barrier(self.controller)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400325
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400326 return (pkt_mulL2,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400327
328
ShreyaPandita6fbff252012-11-13 16:56:48 -0500329def match_mul_l4(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400330 #Generate Match_Mul_L4 flow
331
332 #Create a simple tcp packet and generate match on tcp protocol flow
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400333 pkt_mulL4 = simple_tcp_packet(tcp_sport=111,tcp_dport=112)
334 match = parse.packet_to_flow_match(pkt_mulL4)
335 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita6fbff252012-11-13 16:56:48 -0500336 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 -0400337 msg = message.flow_mod()
338 msg.out_port = ofp.OFPP_NONE
339 msg.command = ofp.OFPFC_ADD
340 msg.buffer_id = 0xffffffff
341 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500342 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400343 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400344
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400345 act = action.action_output()
346 act.port = of_ports[1]
Rich Lanee30455b2013-01-03 16:24:44 -0800347 msg.actions.add(act)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400348
Rich Lane5c3151c2013-01-03 17:15:41 -0800349 self.controller.message_send(msg)
Rich Lane3a261d52013-01-03 17:45:08 -0800350 do_barrier(self.controller)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400351
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400352 return (pkt_mulL4,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400353
ShreyaPanditaed209962012-11-04 02:16:48 -0500354def match_ip_tos(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400355 #Generate a Match on IP Type of service flow
356
357 #Create a simple tcp packet and generate match on Type of service
Rich Laneb5c73792012-12-03 17:12:32 -0800358 pkt_iptos = simple_tcp_packet(ip_tos=28)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400359 match = parse.packet_to_flow_match(pkt_iptos)
360 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400361
ShreyaPandita6fbff252012-11-13 16:56:48 -0500362 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_NW_TOS
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400363 msg = message.flow_mod()
364 msg.out_port = ofp.OFPP_NONE
365 msg.command = ofp.OFPFC_ADD
366 msg.buffer_id = 0xffffffff
367 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500368 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400369 msg.priority = priority
370 act = action.action_output()
371 act.port = of_ports[1]
Rich Lanee30455b2013-01-03 16:24:44 -0800372 msg.actions.add(act)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400373
Rich Lane5c3151c2013-01-03 17:15:41 -0800374 self.controller.message_send(msg)
Rich Lane3a261d52013-01-03 17:45:08 -0800375 do_barrier(self.controller)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400376
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400377 return (pkt_iptos,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400378
ShreyaPandita6fbff252012-11-13 16:56:48 -0500379def match_ip_protocol(self,of_ports,priority=None):
380 #Generate a Match on IP Protocol
381
382 #Create a simple tcp packet and generate match on Type of service
383 pkt_iptos = simple_tcp_packet()
384 match = parse.packet_to_flow_match(pkt_iptos)
385 self.assertTrue(match is not None, "Could not generate flow match from pkt")
386
387 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO
388 msg = message.flow_mod()
389 msg.out_port = ofp.OFPP_NONE
390 msg.command = ofp.OFPFC_ADD
391 msg.buffer_id = 0xffffffff
392 msg.match = match
393 if priority != None :
394 msg.priority = priority
395 act = action.action_output()
396 act.port = of_ports[1]
Rich Lanee30455b2013-01-03 16:24:44 -0800397 msg.actions.add(act)
ShreyaPandita6fbff252012-11-13 16:56:48 -0500398
Rich Lane5c3151c2013-01-03 17:15:41 -0800399 self.controller.message_send(msg)
Rich Lane3a261d52013-01-03 17:45:08 -0800400 do_barrier(self.controller)
ShreyaPandita6fbff252012-11-13 16:56:48 -0500401
402 return (pkt_iptos,match)
403
404
ShreyaPanditaed209962012-11-04 02:16:48 -0500405def match_tcp_src(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400406 #Generate Match_Tcp_Src
407
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400408 #Create a simple tcp packet and generate match on tcp source port flow
409 pkt_matchtSrc = simple_tcp_packet(tcp_sport=111)
410 match = parse.packet_to_flow_match(pkt_matchtSrc)
411 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400412
ShreyaPandita6fbff252012-11-13 16:56:48 -0500413 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_SRC
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400414 msg = message.flow_mod()
415 msg.out_port = ofp.OFPP_NONE
416 msg.command = ofp.OFPFC_ADD
417 msg.buffer_id = 0xffffffff
418 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500419 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400420 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400421
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400422 act = action.action_output()
423 act.port = of_ports[1]
Rich Lanee30455b2013-01-03 16:24:44 -0800424 msg.actions.add(act)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400425
Rich Lane5c3151c2013-01-03 17:15:41 -0800426 self.controller.message_send(msg)
Rich Lane3a261d52013-01-03 17:45:08 -0800427 do_barrier(self.controller)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400428
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400429 return (pkt_matchtSrc,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400430
ShreyaPanditaed209962012-11-04 02:16:48 -0500431def match_tcp_dst(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400432 #Generate Match_Tcp_Dst
433
434 #Create a simple tcp packet and generate match on tcp destination port flow
ShreyaPandita6fbff252012-11-13 16:56:48 -0500435 pkt_matchdst = simple_tcp_packet(tcp_dport=112)
436 match = parse.packet_to_flow_match(pkt_matchdst)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400437 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400438
ShreyaPandita6fbff252012-11-13 16:56:48 -0500439 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO^ofp.OFPFW_TP_DST
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400440 msg = message.flow_mod()
441 msg.out_port = ofp.OFPP_NONE
442 msg.command = ofp.OFPFC_ADD
443 msg.buffer_id = 0xffffffff
444 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500445 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400446 msg.priority = priority
447 act = action.action_output()
448 act.port = of_ports[1]
Rich Lanee30455b2013-01-03 16:24:44 -0800449 msg.actions.add(act)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400450
Rich Lane5c3151c2013-01-03 17:15:41 -0800451 self.controller.message_send(msg)
Rich Lane3a261d52013-01-03 17:45:08 -0800452 do_barrier(self.controller)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400453
ShreyaPandita6fbff252012-11-13 16:56:48 -0500454 return (pkt_matchdst,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400455
ShreyaPandita055102a2012-11-28 11:43:45 -0500456
ShreyaPanditaed209962012-11-04 02:16:48 -0500457def match_ethernet_type(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400458 #Generate a Match_Ethernet_Type flow
459
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400460 #Create a simple tcp packet and generate match on ethernet type flow
461 pkt_matchtype = simple_eth_packet(dl_type=0x88cc)
462 match = parse.packet_to_flow_match(pkt_matchtype)
463 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400464
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400465 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE
466 msg = message.flow_mod()
467 msg.out_port = ofp.OFPP_NONE
468 msg.command = ofp.OFPFC_ADD
469 msg.buffer_id = 0xffffffff
470 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500471 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400472 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400473
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400474 act = action.action_output()
475 act.port = of_ports[1]
Rich Lanee30455b2013-01-03 16:24:44 -0800476 msg.actions.add(act)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400477
Rich Lane5c3151c2013-01-03 17:15:41 -0800478 self.controller.message_send(msg)
Rich Lane3a261d52013-01-03 17:45:08 -0800479 do_barrier(self.controller)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400480 return (pkt_matchtype,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400481
482
ShreyaPandita60e45542012-09-27 15:11:16 -0400483
ShreyaPandita6fbff252012-11-13 16:56:48 -0500484
ShreyaPanditaed209962012-11-04 02:16:48 -0500485def strict_modify_flow_action(self,egress_port,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400486# Strict Modify the flow Action
487
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400488 #Create a flow_mod message , command MODIFY_STRICT
489 msg5 = message.flow_mod()
490 msg5.match = match
491 msg5.cookie = random.randint(0,9007199254740992)
492 msg5.command = ofp.OFPFC_MODIFY_STRICT
493 msg5.buffer_id = 0xffffffff
494 act5 = action.action_output()
495 act5.port = egress_port
Rich Lanee30455b2013-01-03 16:24:44 -0800496 msg5.actions.add(act5)
ShreyaPandita60e45542012-09-27 15:11:16 -0400497
ShreyaPanditaed209962012-11-04 02:16:48 -0500498 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400499 msg5.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400500
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400501 # Send the flow with action A'
Rich Lane5c3151c2013-01-03 17:15:41 -0800502 self.controller.message_send (msg5)
Rich Lane3a261d52013-01-03 17:45:08 -0800503 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400504
ShreyaPanditaed209962012-11-04 02:16:48 -0500505def modify_flow_action(self,of_ports,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400506# Modify the flow action
507
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400508 #Create a flow_mod message , command MODIFY
509 msg8 = message.flow_mod()
510 msg8.match = match
511 msg8.cookie = random.randint(0,9007199254740992)
512 msg8.command = ofp.OFPFC_MODIFY
513 #out_port will be ignored for flow adds and flow modify (here for test-case Add_Modify_With_Outport)
514 msg8.out_port = of_ports[3]
515 msg8.buffer_id = 0xffffffff
516 act8 = action.action_output()
517 act8.port = of_ports[2]
Rich Lanee30455b2013-01-03 16:24:44 -0800518 msg8.actions.add(act8)
ShreyaPandita60e45542012-09-27 15:11:16 -0400519
ShreyaPanditaed209962012-11-04 02:16:48 -0500520 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400521 msg8.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400522
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400523 # Send the flow with action A'
Rich Lane5c3151c2013-01-03 17:15:41 -0800524 self.controller.message_send (msg8)
Rich Lane3a261d52013-01-03 17:45:08 -0800525 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400526
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400527def enqueue(self,ingress_port,egress_port,egress_queue_id):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400528#Generate a flow with enqueue action i.e output to a queue configured on a egress_port
529
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400530 pkt = simple_tcp_packet()
531 match = packet_to_flow_match(self, pkt)
532 match.wildcards &= ~ofp.OFPFW_IN_PORT
533 self.assertTrue(match is not None,
534 "Could not generate flow match from pkt")
535
536 match.in_port = ingress_port
537 request = message.flow_mod()
538 request.match = match
539 request.buffer_id = 0xffffffff
540 act = action.action_enqueue()
541 act.port = egress_port
542 act.queue_id = egress_queue_id
Rich Lanee30455b2013-01-03 16:24:44 -0800543 request.actions.add(act)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400544
545 logging.info("Inserting flow")
Rich Lane5c3151c2013-01-03 17:15:41 -0800546 self.controller.message_send(request)
Rich Lane3a261d52013-01-03 17:45:08 -0800547 do_barrier(self.controller)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400548 return (pkt,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400549
550
ShreyaPandita60e45542012-09-27 15:11:16 -0400551########################### Verify Stats Functions ###########################################################################################
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400552def get_flowstats(self,match):
553 # Generate flow_stats request
554
555 stat_req = message.flow_stats_request()
556 stat_req.match = match
557 stat_req.table_id = 0xff
558 stat_req.out_port = ofp.OFPP_NONE
559
560 logging.info("Sending stats request")
561 response, pkt = self.controller.transact(stat_req,
562 timeout=5)
563 self.assertTrue(response is not None,"No response to stats request")
ShreyaPandita60e45542012-09-27 15:11:16 -0400564
ShreyaPandita66de26f2012-10-26 14:44:24 -0400565
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400566def get_portstats(self,port_num):
567
568# Return all the port counters in the form a tuple
569 port_stats_req = message.port_stats_request()
570 port_stats_req.port_no = port_num
571 response,pkt = self.controller.transact(port_stats_req)
572 self.assertTrue(response is not None,"No response received for port stats request")
573 rx_pkts=0
574 tx_pkts=0
575 rx_byts=0
576 tx_byts=0
577 rx_drp =0
578 tx_drp = 0
579 rx_err=0
580 tx_err =0
581 rx_fr_err=0
582 rx_ovr_err=0
583 rx_crc_err=0
584 collisions = 0
585 tx_err=0
586
587
588 for obj in response.stats:
589 rx_pkts += obj.rx_packets
590 tx_pkts += obj.tx_packets
591 rx_byts += obj.rx_bytes
592 tx_byts += obj.tx_bytes
593 rx_drp += obj.rx_dropped
594 tx_drp += obj.tx_dropped
595 rx_err += obj.rx_errors
596 rx_fr_err += obj.rx_frame_err
597 rx_ovr_err += obj.rx_over_err
598 rx_crc_err += obj.rx_crc_err
599 collisions+= obj.collisions
600 tx_err += obj.tx_errors
601
602 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)
603
604def get_queuestats(self,port_num,queue_id):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400605#Generate Queue Stats request
606
607 request = message.queue_stats_request()
608 request.port_no = port_num
609 request.queue_id = queue_id
610 (queue_stats, p) = self.controller.transact(request)
611 self.assertNotEqual(queue_stats, None, "Queue stats request failed")
612
613 return (queue_stats,p)
614
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400615def get_tablestats(self):
616# Send Table_Stats request (retrieve current table counters )
617
618 stat_req = message.table_stats_request()
619 response, pkt = self.controller.transact(stat_req,
620 timeout=5)
621 self.assertTrue(response is not None,
622 "No response to stats request")
623 current_lookedup = 0
624 current_matched = 0
625 current_active = 0
626
627 for obj in response.stats:
628 current_lookedup += obj.lookup_count
629 current_matched += obj.matched_count
630 current_active += obj.active_count
631
632 return (current_lookedup,current_matched,current_active)
633
634
635
ShreyaPanditaed209962012-11-04 02:16:48 -0500636def verify_tablestats(self,expect_lookup=None,expect_match=None,expect_active=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400637
638 stat_req = message.table_stats_request()
ShreyaPanditaed209962012-11-04 02:16:48 -0500639
Rich Lane90b3d732012-12-31 10:03:50 -0800640 for i in range(0,100):
ShreyaPandita60e45542012-09-27 15:11:16 -0400641
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400642 logging.info("Sending stats request")
643 # TODO: move REPLY_MORE handling to controller.transact?
ShreyaPandita66de26f2012-10-26 14:44:24 -0400644 response, pkt = self.controller.transact(stat_req,
645 timeout=5)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400646 self.assertTrue(response is not None,"No response to stats request")
ShreyaPandita60e45542012-09-27 15:11:16 -0400647
ShreyaPanditaed209962012-11-04 02:16:48 -0500648 lookedup = 0
649 matched = 0
650 active = 0
651
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400652 for item in response.stats:
653 lookedup += item.lookup_count
654 matched += item.matched_count
655 active += item.active_count
656
Rich Laneafcd0dd2013-01-03 20:54:56 -0800657 logging.info("Packets Looked up: %d", lookedup)
658 logging.info("Packets matched: %d", matched)
659 logging.info("Active flow entries: %d", active)
ShreyaPanditaed209962012-11-04 02:16:48 -0500660
Rich Lane175f9562013-01-03 20:58:40 -0800661 if (expect_lookup == None or lookedup >= expect_lookup) and \
662 (expect_match == None or matched >= expect_match) and \
663 (expect_active == None or active >= expect_active):
Rich Lane90b3d732012-12-31 10:03:50 -0800664 break
665
666 sleep(0.1)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400667
ShreyaPanditaed209962012-11-04 02:16:48 -0500668 if expect_lookup != None :
669 self.assertEqual(expect_lookup,item.lookup_count,"lookup counter is not incremented properly")
670 if expect_match != None :
671 self.assertEqual(expect_match,item.matched_count, "matched counter is not incremented properly")
672 if expect_active != None :
673 self.assertEqual(expect_active,item.active_count,"active counter is not incremented properly")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400674
675
ShreyaPanditaed209962012-11-04 02:16:48 -0500676def verify_flowstats(self,match,byte_count=None,packet_count=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400677 # Verify flow counters : byte_count and packet_count
ShreyaPandita60e45542012-09-27 15:11:16 -0400678
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400679 stat_req = message.flow_stats_request()
680 stat_req.match = match
681 stat_req.table_id = 0xff
682 stat_req.out_port = ofp.OFPP_NONE
ShreyaPanditaed209962012-11-04 02:16:48 -0500683
Rich Lane90b3d732012-12-31 10:03:50 -0800684 for i in range(0,100):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400685 logging.info("Sending stats request")
686 # TODO: move REPLY_MORE handling to controller.transact?
ShreyaPandita66de26f2012-10-26 14:44:24 -0400687 response, pkt = self.controller.transact(stat_req,
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400688 timeout=5)
689 self.assertTrue(response is not None,"No response to stats request")
690
ShreyaPanditaed209962012-11-04 02:16:48 -0500691 packet_counter = 0
692 byte_counter = 0
693
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400694 for item in response.stats:
695 packet_counter += item.packet_count
696 byte_counter += item.byte_count
697
698 logging.info("Recieved" + str(item.packet_count) + " packets")
ShreyaPanditaed209962012-11-04 02:16:48 -0500699
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400700 logging.info("Received " + str(item.byte_count) + "bytes")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400701
Rich Lane90b3d732012-12-31 10:03:50 -0800702 if (packet_count == None or packet_count == packet_counter) and \
703 (byte_count == None or byte_count == byte_counter):
704 break
705
706 sleep(0.1)
ShreyaPanditaed209962012-11-04 02:16:48 -0500707
708 if packet_count != None :
709 self.assertEqual(packet_count,item.packet_count,"packet_count counter is not incremented correctly")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400710
ShreyaPanditaed209962012-11-04 02:16:48 -0500711 if byte_count != None :
712 self.assertEqual(byte_count,item.byte_count,"byte_count counter is not incremented correctly")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400713
714
ShreyaPanditaed209962012-11-04 02:16:48 -0500715def verify_portstats(self, port,tx_packets=None,rx_packets=None,rx_byte=None,tx_byte=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400716
717
718 stat_req = message.port_stats_request()
719 stat_req.port_no = port
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400720
Rich Lane90b3d732012-12-31 10:03:50 -0800721 for i in range(0,100):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400722 logging.info("Sending stats request")
723 response, pkt = self.controller.transact(stat_req,
724 timeout=5)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400725 self.assertTrue(response is not None,
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400726 "No response to stats request")
727 self.assertTrue(len(response.stats) == 1,
728 "Did not receive port stats reply")
ShreyaPanditaed209962012-11-04 02:16:48 -0500729
730 sentp = recvp = 0
731 sentb = recvb = 0
732
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400733 for item in response.stats:
734 sentp += item.tx_packets
735 recvp += item.rx_packets
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400736 recvb += item.rx_bytes
ShreyaPanditaed209962012-11-04 02:16:48 -0500737 sentb += item.tx_bytes
738
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400739
740 logging.info("Sent " + str(sentp) + " packets")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400741 logging.info("Received " + str(recvp) + " packets")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400742 logging.info("Received " + str(recvb) + "bytes")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400743 logging.info("Sent" + str(sentb) + "bytes")
Rich Lane90b3d732012-12-31 10:03:50 -0800744
745 if (tx_packets == None or tx_packets == sentp) and \
746 (rx_packets == None or rx_packets == recvp) and \
747 (tx_byte == None or tx_byte == sentb) and \
748 (rx_byte == None or rx_byte == recvb):
749 break
750
751 sleep(0.1)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400752
ShreyaPanditaa6dfbfc2012-11-05 17:45:22 -0500753
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400754
ShreyaPanditaed209962012-11-04 02:16:48 -0500755 if (tx_packets != None):
756 self.assertEqual(tx_packets,item.tx_packets,"rx_packets counter is not incremented correctly")
757 if (rx_packets != None):
758 self.assertEqual(rx_packets,item.rx_packets,"tx_packets counter is not incremented correctly")
759 if (rx_byte != None):
760 self.assertEqual(rx_byte,item.rx_bytes,"rx_bytes counter is not incremented correctly")
761 if (tx_byte != None):
762 self.assertEqual(tx_byte,item.tx_bytes,"tx_bytes counter is not incremented correctly")
ShreyaPandita60e45542012-09-27 15:11:16 -0400763
ShreyaPandita60e45542012-09-27 15:11:16 -0400764
ShreyaPanditaed209962012-11-04 02:16:48 -0500765def verify_queuestats(self,port_num,queue_id,expect_packet=None,expect_byte=None):
766
767 # Verify queue counters : tx_packets and tx_bytes
768
769 request = message.queue_stats_request()
770 request.port_no = port_num
771 request.queue_id = queue_id
772
Rich Lane90b3d732012-12-31 10:03:50 -0800773 for i in range(0,100):
ShreyaPanditaed209962012-11-04 02:16:48 -0500774
775 logging.info("Sending stats request")
776
777 (queue_stats, p) = self.controller.transact(request)
778 self.assertNotEqual(queue_stats, None, "Queue stats request failed")
779 packet_counter = 0
780 byte_counter = 0
781
ShreyaPanditaa6dfbfc2012-11-05 17:45:22 -0500782 for item in queue_stats.stats:
ShreyaPanditaed209962012-11-04 02:16:48 -0500783 packet_counter += item.tx_packets
784 byte_counter += item.tx_bytes
785
786 logging.info("Transmitted" + str(packet_counter) + " packets")
787 logging.info("Transmitted" + str(byte_counter) + "bytes")
788
Rich Lane90b3d732012-12-31 10:03:50 -0800789 if (expect_packet == None or packet_counter == expect_packet) and \
790 (expect_byte == None or byte_counter == expect_byte):
791 break
ShreyaPanditaed209962012-11-04 02:16:48 -0500792
Rich Lane90b3d732012-12-31 10:03:50 -0800793 sleep(0.1)
ShreyaPanditaed209962012-11-04 02:16:48 -0500794
795 if expect_packet != None :
796 self.assertEqual(packet_counter,expect_packet,"tx_packets counter is not incremented correctly")
797
798 if expect_byte != None :
799 self.assertEqual(byte_counter,expect_byte,"tx_bytes counter is not incremented correctly")
800
801
ShreyaPandita60e45542012-09-27 15:11:16 -0400802############################## Various delete commands #############################################################################################
803
ShreyaPanditaed209962012-11-04 02:16:48 -0500804def strict_delete(self,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400805# Issue Strict Delete
806
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400807 #Create flow_mod message, command DELETE_STRICT
808 msg4 = message.flow_mod()
809 msg4.out_port = ofp.OFPP_NONE
810 msg4.command = ofp.OFPFC_DELETE_STRICT
811 msg4.buffer_id = 0xffffffff
812 msg4.match = match
ShreyaPandita60e45542012-09-27 15:11:16 -0400813
ShreyaPanditaed209962012-11-04 02:16:48 -0500814 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400815 msg4.priority = priority
Rich Lane5c3151c2013-01-03 17:15:41 -0800816 self.controller.message_send(msg4)
Rich Lane3a261d52013-01-03 17:45:08 -0800817 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400818
819
820
ShreyaPanditaed209962012-11-04 02:16:48 -0500821def nonstrict_delete(self,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400822# Issue Non_Strict Delete
823
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400824 #Create flow_mod message, command DELETE
825 msg6 = message.flow_mod()
826 msg6.out_port = ofp.OFPP_NONE
827 msg6.command = ofp.OFPFC_DELETE
828 msg6.buffer_id = 0xffffffff
829 msg6.match = match
ShreyaPandita60e45542012-09-27 15:11:16 -0400830
ShreyaPanditaed209962012-11-04 02:16:48 -0500831 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400832 msg6.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400833
Rich Lane5c3151c2013-01-03 17:15:41 -0800834 self.controller.message_send(msg6)
Rich Lane3a261d52013-01-03 17:45:08 -0800835 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400836
837
838###########################################################################################################################################################
839
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400840def send_packet(obj, pkt, ingress_port, egress_port):
ShreyaPandita60e45542012-09-27 15:11:16 -0400841#Send Packets on a specified ingress_port and verify if its recieved on correct egress_port.
842
843 obj.dataplane.send(ingress_port, str(pkt))
844 exp_pkt_arg = pkt
845 exp_port = egress_port
846
847 (rcv_port, rcv_pkt, pkt_time) = obj.dataplane.poll(timeout=2,
848 port_number=exp_port,
849 exp_pkt=exp_pkt_arg)
850 obj.assertTrue(rcv_pkt is not None,
851 "Packet not received on port " + str(egress_port))
852 obj.assertEqual(rcv_port, egress_port,
853 "Packet received on port " + str(rcv_port) +
854 ", expected port " + str(egress_port))
855 obj.assertEqual(str(pkt), str(rcv_pkt),
856 'Response packet does not match send packet')
857
858
ShreyaPandita572e64b2012-09-28 14:41:06 -0400859def sw_supported_actions(parent,use_cache=False):
ShreyaPandita60e45542012-09-27 15:11:16 -0400860#Returns the switch's supported actions
861
862 cache_supported_actions = None
863 if cache_supported_actions is None or not use_cache:
864 request = message.features_request()
865 (reply, pkt) = parent.controller.transact(request)
866 parent.assertTrue(reply is not None, "Did not get response to ftr req")
867 cache_supported_actions = reply.actions
868 return cache_supported_actions
869
ShreyaPandita66de26f2012-10-26 14:44:24 -0400870##############################################################################################################################################################
871