blob: f320652ccae85f86493a3411747329e9dc0be457 [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
Shudong Zhouc2f18762013-01-11 00:12:44 -0800457def match_udp_src(self,of_ports,priority=None):
458 #Generate Match_Udp_Src
459
460 #Create a simple udp packet and generate match on udp source port flow
461 pkt_matchtSrc = simple_udp_packet(udp_sport=111)
462 match = parse.packet_to_flow_match(pkt_matchtSrc)
463 self.assertTrue(match is not None, "Could not generate flow match from pkt")
464
465 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_SRC
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
471 if priority != None :
472 msg.priority = priority
473
474 act = action.action_output()
475 act.port = of_ports[1]
476 msg.actions.add(act)
477
478 self.controller.message_send(msg)
479 do_barrier(self.controller)
480
481 return (pkt_matchtSrc,match)
482
483def match_udp_dst(self,of_ports,priority=None):
484 #Generate Match_Udp_Dst
485
486 #Create a simple udp packet and generate match on udp destination port flow
487 pkt_matchdst = simple_udp_packet(udp_dport=112)
488 match = parse.packet_to_flow_match(pkt_matchdst)
489 self.assertTrue(match is not None, "Could not generate flow match from pkt")
490
491 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO^ofp.OFPFW_TP_DST
492 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
497 if priority != None :
498 msg.priority = priority
499 act = action.action_output()
500 act.port = of_ports[1]
501 msg.actions.add(act)
502
503 self.controller.message_send(msg)
504 do_barrier(self.controller)
505
506 return (pkt_matchdst,match)
507
508
509def match_icmp_type(self,of_ports,priority=None):
510 #Generate Match_Icmp_Type
511
512 #Create a simple icmp packet and generate match on icmp type flow
513 pkt_match = simple_icmp_packet(icmp_type=1)
514 match = parse.packet_to_flow_match(pkt_match)
515 self.assertTrue(match is not None, "Could not generate flow match from pkt")
516
517 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_SRC
518 msg = message.flow_mod()
519 msg.out_port = ofp.OFPP_NONE
520 msg.command = ofp.OFPFC_ADD
521 msg.buffer_id = 0xffffffff
522 msg.match = match
523 if priority != None :
524 msg.priority = priority
525
526 act = action.action_output()
527 act.port = of_ports[1]
528 msg.actions.add(act)
529
530 self.controller.message_send(msg)
531 do_barrier(self.controller)
532
533 return (pkt_match, match)
534
535def match_icmp_code(self,of_ports,priority=None):
536 #Generate Match_Icmp_Code
537
538 #Create a simple icmp packet and generate match on icmp code flow
539 pkt_match = simple_icmp_packet(icmp_code=3)
540 match = parse.packet_to_flow_match(pkt_match)
541 self.assertTrue(match is not None, "Could not generate flow match from pkt")
542
543 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_DST
544 msg = message.flow_mod()
545 msg.out_port = ofp.OFPP_NONE
546 msg.command = ofp.OFPFC_ADD
547 msg.buffer_id = 0xffffffff
548 msg.match = match
549 if priority != None :
550 msg.priority = priority
551
552 act = action.action_output()
553 act.port = of_ports[1]
554 msg.actions.add(act)
555
556 self.controller.message_send(msg)
557 do_barrier(self.controller)
558
559 return (pkt_match, match)
560
561
ShreyaPanditaed209962012-11-04 02:16:48 -0500562def match_ethernet_type(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400563 #Generate a Match_Ethernet_Type flow
564
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400565 #Create a simple tcp packet and generate match on ethernet type flow
566 pkt_matchtype = simple_eth_packet(dl_type=0x88cc)
567 match = parse.packet_to_flow_match(pkt_matchtype)
568 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400569
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400570 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE
571 msg = message.flow_mod()
572 msg.out_port = ofp.OFPP_NONE
573 msg.command = ofp.OFPFC_ADD
574 msg.buffer_id = 0xffffffff
575 msg.match = match
ShreyaPanditaed209962012-11-04 02:16:48 -0500576 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400577 msg.priority = priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400578
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400579 act = action.action_output()
580 act.port = of_ports[1]
Rich Lanee30455b2013-01-03 16:24:44 -0800581 msg.actions.add(act)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400582
Rich Lane5c3151c2013-01-03 17:15:41 -0800583 self.controller.message_send(msg)
Rich Lane3a261d52013-01-03 17:45:08 -0800584 do_barrier(self.controller)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400585 return (pkt_matchtype,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400586
587
ShreyaPandita60e45542012-09-27 15:11:16 -0400588
ShreyaPandita6fbff252012-11-13 16:56:48 -0500589
ShreyaPanditaed209962012-11-04 02:16:48 -0500590def strict_modify_flow_action(self,egress_port,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400591# Strict Modify the flow Action
592
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400593 #Create a flow_mod message , command MODIFY_STRICT
594 msg5 = message.flow_mod()
595 msg5.match = match
596 msg5.cookie = random.randint(0,9007199254740992)
597 msg5.command = ofp.OFPFC_MODIFY_STRICT
598 msg5.buffer_id = 0xffffffff
599 act5 = action.action_output()
600 act5.port = egress_port
Rich Lanee30455b2013-01-03 16:24:44 -0800601 msg5.actions.add(act5)
ShreyaPandita60e45542012-09-27 15:11:16 -0400602
ShreyaPanditaed209962012-11-04 02:16:48 -0500603 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400604 msg5.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400605
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400606 # Send the flow with action A'
Rich Lane5c3151c2013-01-03 17:15:41 -0800607 self.controller.message_send (msg5)
Rich Lane3a261d52013-01-03 17:45:08 -0800608 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400609
ShreyaPanditaed209962012-11-04 02:16:48 -0500610def modify_flow_action(self,of_ports,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400611# Modify the flow action
612
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400613 #Create a flow_mod message , command MODIFY
614 msg8 = message.flow_mod()
615 msg8.match = match
616 msg8.cookie = random.randint(0,9007199254740992)
617 msg8.command = ofp.OFPFC_MODIFY
618 #out_port will be ignored for flow adds and flow modify (here for test-case Add_Modify_With_Outport)
619 msg8.out_port = of_ports[3]
620 msg8.buffer_id = 0xffffffff
621 act8 = action.action_output()
622 act8.port = of_ports[2]
Rich Lanee30455b2013-01-03 16:24:44 -0800623 msg8.actions.add(act8)
ShreyaPandita60e45542012-09-27 15:11:16 -0400624
ShreyaPanditaed209962012-11-04 02:16:48 -0500625 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400626 msg8.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400627
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400628 # Send the flow with action A'
Rich Lane5c3151c2013-01-03 17:15:41 -0800629 self.controller.message_send (msg8)
Rich Lane3a261d52013-01-03 17:45:08 -0800630 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400631
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400632def enqueue(self,ingress_port,egress_port,egress_queue_id):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400633#Generate a flow with enqueue action i.e output to a queue configured on a egress_port
634
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400635 pkt = simple_tcp_packet()
636 match = packet_to_flow_match(self, pkt)
637 match.wildcards &= ~ofp.OFPFW_IN_PORT
638 self.assertTrue(match is not None,
639 "Could not generate flow match from pkt")
640
641 match.in_port = ingress_port
642 request = message.flow_mod()
643 request.match = match
644 request.buffer_id = 0xffffffff
645 act = action.action_enqueue()
646 act.port = egress_port
647 act.queue_id = egress_queue_id
Rich Lanee30455b2013-01-03 16:24:44 -0800648 request.actions.add(act)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400649
650 logging.info("Inserting flow")
Rich Lane5c3151c2013-01-03 17:15:41 -0800651 self.controller.message_send(request)
Rich Lane3a261d52013-01-03 17:45:08 -0800652 do_barrier(self.controller)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400653 return (pkt,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400654
655
ShreyaPandita60e45542012-09-27 15:11:16 -0400656########################### Verify Stats Functions ###########################################################################################
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400657def get_flowstats(self,match):
658 # Generate flow_stats request
659
660 stat_req = message.flow_stats_request()
661 stat_req.match = match
662 stat_req.table_id = 0xff
663 stat_req.out_port = ofp.OFPP_NONE
664
665 logging.info("Sending stats request")
666 response, pkt = self.controller.transact(stat_req,
667 timeout=5)
668 self.assertTrue(response is not None,"No response to stats request")
ShreyaPandita60e45542012-09-27 15:11:16 -0400669
ShreyaPandita66de26f2012-10-26 14:44:24 -0400670
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400671def get_portstats(self,port_num):
672
673# Return all the port counters in the form a tuple
674 port_stats_req = message.port_stats_request()
675 port_stats_req.port_no = port_num
676 response,pkt = self.controller.transact(port_stats_req)
677 self.assertTrue(response is not None,"No response received for port stats request")
678 rx_pkts=0
679 tx_pkts=0
680 rx_byts=0
681 tx_byts=0
682 rx_drp =0
683 tx_drp = 0
684 rx_err=0
685 tx_err =0
686 rx_fr_err=0
687 rx_ovr_err=0
688 rx_crc_err=0
689 collisions = 0
690 tx_err=0
691
692
693 for obj in response.stats:
694 rx_pkts += obj.rx_packets
695 tx_pkts += obj.tx_packets
696 rx_byts += obj.rx_bytes
697 tx_byts += obj.tx_bytes
698 rx_drp += obj.rx_dropped
699 tx_drp += obj.tx_dropped
700 rx_err += obj.rx_errors
701 rx_fr_err += obj.rx_frame_err
702 rx_ovr_err += obj.rx_over_err
703 rx_crc_err += obj.rx_crc_err
704 collisions+= obj.collisions
705 tx_err += obj.tx_errors
706
707 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)
708
709def get_queuestats(self,port_num,queue_id):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400710#Generate Queue Stats request
711
712 request = message.queue_stats_request()
713 request.port_no = port_num
714 request.queue_id = queue_id
715 (queue_stats, p) = self.controller.transact(request)
716 self.assertNotEqual(queue_stats, None, "Queue stats request failed")
717
718 return (queue_stats,p)
719
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400720def get_tablestats(self):
721# Send Table_Stats request (retrieve current table counters )
722
723 stat_req = message.table_stats_request()
724 response, pkt = self.controller.transact(stat_req,
725 timeout=5)
726 self.assertTrue(response is not None,
727 "No response to stats request")
728 current_lookedup = 0
729 current_matched = 0
730 current_active = 0
731
732 for obj in response.stats:
733 current_lookedup += obj.lookup_count
734 current_matched += obj.matched_count
735 current_active += obj.active_count
736
737 return (current_lookedup,current_matched,current_active)
738
739
740
ShreyaPanditaed209962012-11-04 02:16:48 -0500741def verify_tablestats(self,expect_lookup=None,expect_match=None,expect_active=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400742
743 stat_req = message.table_stats_request()
ShreyaPanditaed209962012-11-04 02:16:48 -0500744
Rich Lane90b3d732012-12-31 10:03:50 -0800745 for i in range(0,100):
ShreyaPandita60e45542012-09-27 15:11:16 -0400746
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400747 logging.info("Sending stats request")
748 # TODO: move REPLY_MORE handling to controller.transact?
ShreyaPandita66de26f2012-10-26 14:44:24 -0400749 response, pkt = self.controller.transact(stat_req,
750 timeout=5)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400751 self.assertTrue(response is not None,"No response to stats request")
ShreyaPandita60e45542012-09-27 15:11:16 -0400752
ShreyaPanditaed209962012-11-04 02:16:48 -0500753 lookedup = 0
754 matched = 0
755 active = 0
756
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400757 for item in response.stats:
758 lookedup += item.lookup_count
759 matched += item.matched_count
760 active += item.active_count
761
Rich Laneafcd0dd2013-01-03 20:54:56 -0800762 logging.info("Packets Looked up: %d", lookedup)
763 logging.info("Packets matched: %d", matched)
764 logging.info("Active flow entries: %d", active)
ShreyaPanditaed209962012-11-04 02:16:48 -0500765
Rich Lane175f9562013-01-03 20:58:40 -0800766 if (expect_lookup == None or lookedup >= expect_lookup) and \
767 (expect_match == None or matched >= expect_match) and \
768 (expect_active == None or active >= expect_active):
Rich Lane90b3d732012-12-31 10:03:50 -0800769 break
770
771 sleep(0.1)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400772
ShreyaPanditaed209962012-11-04 02:16:48 -0500773 if expect_lookup != None :
Rich Lane3e777792013-01-03 21:30:30 -0800774 self.assertEqual(expect_lookup, lookedup, "lookup counter is not incremented properly")
ShreyaPanditaed209962012-11-04 02:16:48 -0500775 if expect_match != None :
Rich Lane3e777792013-01-03 21:30:30 -0800776 self.assertEqual(expect_match, matched, "matched counter is not incremented properly")
ShreyaPanditaed209962012-11-04 02:16:48 -0500777 if expect_active != None :
Rich Lane3e777792013-01-03 21:30:30 -0800778 self.assertEqual(expect_active, active ,"active counter is not incremented properly")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400779
780
ShreyaPanditaed209962012-11-04 02:16:48 -0500781def verify_flowstats(self,match,byte_count=None,packet_count=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400782 # Verify flow counters : byte_count and packet_count
ShreyaPandita60e45542012-09-27 15:11:16 -0400783
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400784 stat_req = message.flow_stats_request()
785 stat_req.match = match
786 stat_req.table_id = 0xff
787 stat_req.out_port = ofp.OFPP_NONE
ShreyaPanditaed209962012-11-04 02:16:48 -0500788
Rich Lane90b3d732012-12-31 10:03:50 -0800789 for i in range(0,100):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400790 logging.info("Sending stats request")
791 # TODO: move REPLY_MORE handling to controller.transact?
ShreyaPandita66de26f2012-10-26 14:44:24 -0400792 response, pkt = self.controller.transact(stat_req,
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400793 timeout=5)
794 self.assertTrue(response is not None,"No response to stats request")
795
ShreyaPanditaed209962012-11-04 02:16:48 -0500796 packet_counter = 0
797 byte_counter = 0
798
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400799 for item in response.stats:
800 packet_counter += item.packet_count
801 byte_counter += item.byte_count
802
Rich Lanec9e41842013-01-03 21:31:42 -0800803 logging.info("Received %d packets", packet_counter)
804 logging.info("Received %d bytes", byte_counter)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400805
Rich Lanec9e41842013-01-03 21:31:42 -0800806 if (packet_count == None or packet_counter >= packet_count) and \
807 (byte_count == None or byte_counter >= byte_count):
Rich Lane90b3d732012-12-31 10:03:50 -0800808 break
809
810 sleep(0.1)
ShreyaPanditaed209962012-11-04 02:16:48 -0500811
812 if packet_count != None :
Rich Lanec9e41842013-01-03 21:31:42 -0800813 self.assertEqual(packet_count, packet_counter, "packet_count counter is not incremented correctly")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400814
ShreyaPanditaed209962012-11-04 02:16:48 -0500815 if byte_count != None :
Rich Lanec9e41842013-01-03 21:31:42 -0800816 self.assertEqual(byte_count, byte_counter, "byte_count counter is not incremented correctly")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400817
818
ShreyaPanditaed209962012-11-04 02:16:48 -0500819def verify_portstats(self, port,tx_packets=None,rx_packets=None,rx_byte=None,tx_byte=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400820
821
822 stat_req = message.port_stats_request()
823 stat_req.port_no = port
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400824
Rich Lane90b3d732012-12-31 10:03:50 -0800825 for i in range(0,100):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400826 logging.info("Sending stats request")
827 response, pkt = self.controller.transact(stat_req,
828 timeout=5)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400829 self.assertTrue(response is not None,
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400830 "No response to stats request")
831 self.assertTrue(len(response.stats) == 1,
832 "Did not receive port stats reply")
ShreyaPanditaed209962012-11-04 02:16:48 -0500833
834 sentp = recvp = 0
835 sentb = recvb = 0
836
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400837 for item in response.stats:
838 sentp += item.tx_packets
839 recvp += item.rx_packets
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400840 recvb += item.rx_bytes
ShreyaPanditaed209962012-11-04 02:16:48 -0500841 sentb += item.tx_bytes
842
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400843
844 logging.info("Sent " + str(sentp) + " packets")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400845 logging.info("Received " + str(recvp) + " packets")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400846 logging.info("Received " + str(recvb) + "bytes")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400847 logging.info("Sent" + str(sentb) + "bytes")
Rich Lane90b3d732012-12-31 10:03:50 -0800848
849 if (tx_packets == None or tx_packets == sentp) and \
850 (rx_packets == None or rx_packets == recvp) and \
851 (tx_byte == None or tx_byte == sentb) and \
852 (rx_byte == None or rx_byte == recvb):
853 break
854
855 sleep(0.1)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400856
ShreyaPanditaa6dfbfc2012-11-05 17:45:22 -0500857
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400858
ShreyaPanditaed209962012-11-04 02:16:48 -0500859 if (tx_packets != None):
860 self.assertEqual(tx_packets,item.tx_packets,"rx_packets counter is not incremented correctly")
861 if (rx_packets != None):
862 self.assertEqual(rx_packets,item.rx_packets,"tx_packets counter is not incremented correctly")
863 if (rx_byte != None):
864 self.assertEqual(rx_byte,item.rx_bytes,"rx_bytes counter is not incremented correctly")
865 if (tx_byte != None):
866 self.assertEqual(tx_byte,item.tx_bytes,"tx_bytes counter is not incremented correctly")
ShreyaPandita60e45542012-09-27 15:11:16 -0400867
ShreyaPandita60e45542012-09-27 15:11:16 -0400868
ShreyaPanditaed209962012-11-04 02:16:48 -0500869def verify_queuestats(self,port_num,queue_id,expect_packet=None,expect_byte=None):
870
871 # Verify queue counters : tx_packets and tx_bytes
872
873 request = message.queue_stats_request()
874 request.port_no = port_num
875 request.queue_id = queue_id
876
Rich Lane90b3d732012-12-31 10:03:50 -0800877 for i in range(0,100):
ShreyaPanditaed209962012-11-04 02:16:48 -0500878
879 logging.info("Sending stats request")
880
881 (queue_stats, p) = self.controller.transact(request)
882 self.assertNotEqual(queue_stats, None, "Queue stats request failed")
883 packet_counter = 0
884 byte_counter = 0
885
ShreyaPanditaa6dfbfc2012-11-05 17:45:22 -0500886 for item in queue_stats.stats:
ShreyaPanditaed209962012-11-04 02:16:48 -0500887 packet_counter += item.tx_packets
888 byte_counter += item.tx_bytes
889
890 logging.info("Transmitted" + str(packet_counter) + " packets")
891 logging.info("Transmitted" + str(byte_counter) + "bytes")
892
Rich Lane90b3d732012-12-31 10:03:50 -0800893 if (expect_packet == None or packet_counter == expect_packet) and \
894 (expect_byte == None or byte_counter == expect_byte):
895 break
ShreyaPanditaed209962012-11-04 02:16:48 -0500896
Rich Lane90b3d732012-12-31 10:03:50 -0800897 sleep(0.1)
ShreyaPanditaed209962012-11-04 02:16:48 -0500898
899 if expect_packet != None :
900 self.assertEqual(packet_counter,expect_packet,"tx_packets counter is not incremented correctly")
901
902 if expect_byte != None :
903 self.assertEqual(byte_counter,expect_byte,"tx_bytes counter is not incremented correctly")
904
905
ShreyaPandita60e45542012-09-27 15:11:16 -0400906############################## Various delete commands #############################################################################################
907
ShreyaPanditaed209962012-11-04 02:16:48 -0500908def strict_delete(self,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400909# Issue Strict Delete
910
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400911 #Create flow_mod message, command DELETE_STRICT
912 msg4 = message.flow_mod()
913 msg4.out_port = ofp.OFPP_NONE
914 msg4.command = ofp.OFPFC_DELETE_STRICT
915 msg4.buffer_id = 0xffffffff
916 msg4.match = match
ShreyaPandita60e45542012-09-27 15:11:16 -0400917
ShreyaPanditaed209962012-11-04 02:16:48 -0500918 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400919 msg4.priority = priority
Rich Lane5c3151c2013-01-03 17:15:41 -0800920 self.controller.message_send(msg4)
Rich Lane3a261d52013-01-03 17:45:08 -0800921 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400922
923
924
ShreyaPanditaed209962012-11-04 02:16:48 -0500925def nonstrict_delete(self,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400926# Issue Non_Strict Delete
927
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400928 #Create flow_mod message, command DELETE
929 msg6 = message.flow_mod()
930 msg6.out_port = ofp.OFPP_NONE
931 msg6.command = ofp.OFPFC_DELETE
932 msg6.buffer_id = 0xffffffff
933 msg6.match = match
ShreyaPandita60e45542012-09-27 15:11:16 -0400934
ShreyaPanditaed209962012-11-04 02:16:48 -0500935 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400936 msg6.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400937
Rich Lane5c3151c2013-01-03 17:15:41 -0800938 self.controller.message_send(msg6)
Rich Lane3a261d52013-01-03 17:45:08 -0800939 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400940
941
942###########################################################################################################################################################
943
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400944def send_packet(obj, pkt, ingress_port, egress_port):
ShreyaPandita60e45542012-09-27 15:11:16 -0400945#Send Packets on a specified ingress_port and verify if its recieved on correct egress_port.
946
947 obj.dataplane.send(ingress_port, str(pkt))
948 exp_pkt_arg = pkt
949 exp_port = egress_port
950
951 (rcv_port, rcv_pkt, pkt_time) = obj.dataplane.poll(timeout=2,
952 port_number=exp_port,
953 exp_pkt=exp_pkt_arg)
954 obj.assertTrue(rcv_pkt is not None,
955 "Packet not received on port " + str(egress_port))
956 obj.assertEqual(rcv_port, egress_port,
957 "Packet received on port " + str(rcv_port) +
958 ", expected port " + str(egress_port))
959 obj.assertEqual(str(pkt), str(rcv_pkt),
960 'Response packet does not match send packet')
961
962
ShreyaPandita572e64b2012-09-28 14:41:06 -0400963def sw_supported_actions(parent,use_cache=False):
ShreyaPandita60e45542012-09-27 15:11:16 -0400964#Returns the switch's supported actions
965
966 cache_supported_actions = None
967 if cache_supported_actions is None or not use_cache:
968 request = message.features_request()
969 (reply, pkt) = parent.controller.transact(request)
970 parent.assertTrue(reply is not None, "Did not get response to ftr req")
971 cache_supported_actions = reply.actions
972 return cache_supported_actions
973
ShreyaPandita66de26f2012-10-26 14:44:24 -0400974##############################################################################################################################################################
975