blob: 278f15c894bc2fee89067da7d2dc19925d6d7af7 [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
Shudong Zhou857fb602013-02-06 00:11:38 -080022def match_send_flowadd(self, match, priority, port):
23 msg = message.flow_mod()
24 msg.out_port = ofp.OFPP_NONE
25 msg.command = ofp.OFPFC_ADD
26 # msg.cookie = random.randint(0,9007199254740992)
27 msg.buffer_id = 0xffffffff
28 msg.match = match
29 if priority != None :
30 msg.priority = priority
31 act = action.action_output()
32 act.port = port
33 msg.actions.add(act)
34 self.controller.message_send(msg)
35 do_barrier(self.controller)
36
ShreyaPanditaed209962012-11-04 02:16:48 -050037def exact_match(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -040038# Generate ExactMatch flow .
39
ShreyaPandita4ebbac32012-11-02 13:40:44 -040040 #Create a simple tcp packet and generate exact flow match from it.
41 pkt_exactflow = simple_tcp_packet()
42 match = parse.packet_to_flow_match(pkt_exactflow)
43 self.assertTrue(match is not None, "Could not generate flow match from pkt")
44 match.in_port = of_ports[0]
45 #match.nw_src = 1
46 match.wildcards=0
Shudong Zhou857fb602013-02-06 00:11:38 -080047 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -040048 return (pkt_exactflow,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -040049
ShreyaPanditaed209962012-11-04 02:16:48 -050050def exact_match_with_prio(self,of_ports,priority=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -040051 # Generate ExactMatch with action output to port 2
ShreyaPandita66de26f2012-10-26 14:44:24 -040052
ShreyaPandita4ebbac32012-11-02 13:40:44 -040053 #Create a simple tcp packet and generate exact flow match from it.
54 pkt_exactflow = simple_tcp_packet()
55 match = parse.packet_to_flow_match(pkt_exactflow)
56 self.assertTrue(match is not None, "Could not generate flow match from pkt")
57 match.in_port = of_ports[0]
58 #match.nw_src = 1
59 match.wildcards=0
Shudong Zhou857fb602013-02-06 00:11:38 -080060 match_send_flowadd(self, match, priority, of_ports[2])
ShreyaPandita4ebbac32012-11-02 13:40:44 -040061 return (pkt_exactflow,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -040062
ShreyaPandita60e45542012-09-27 15:11:16 -040063
ShreyaPanditaed209962012-11-04 02:16:48 -050064def match_all_except_source_address(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -040065# Generate Match_All_Except_Source_Address flow
66
ShreyaPandita4ebbac32012-11-02 13:40:44 -040067 #Create a simple tcp packet and generate match all except src address flow.
68 pkt_wildcardsrc= simple_tcp_packet()
69 match1 = parse.packet_to_flow_match(pkt_wildcardsrc)
70 self.assertTrue(match1 is not None, "Could not generate flow match from pkt")
71 match1.in_port = of_ports[0]
72 #match1.nw_src = 1
73 match1.wildcards = ofp.OFPFW_DL_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -080074 match_send_flowadd(self, match1, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -040075 return (pkt_wildcardsrc,match1)
ShreyaPandita66de26f2012-10-26 14:44:24 -040076
ShreyaPandita6fbff252012-11-13 16:56:48 -050077def match_ethernet_src_address(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -040078 #Generate Match_Ethernet_SrC_Address flow
79
ShreyaPandita4ebbac32012-11-02 13:40:44 -040080 #Create a simple tcp packet and generate match on ethernet src address flow
ShreyaPandita6fbff252012-11-13 16:56:48 -050081 pkt_MatchSrc = simple_eth_packet(dl_src='00:01:01:01:01:01')
ShreyaPandita4ebbac32012-11-02 13:40:44 -040082 match = parse.packet_to_flow_match(pkt_MatchSrc)
83 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita4ebbac32012-11-02 13:40:44 -040084 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -080085 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -040086 return (pkt_MatchSrc,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -040087
ShreyaPanditaed209962012-11-04 02:16:48 -050088def match_ethernet_dst_address(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -040089 #Generate Match_Ethernet_Dst_Address flow
90
ShreyaPandita4ebbac32012-11-02 13:40:44 -040091 #Create a simple tcp packet and generate match on ethernet dst address flow
ShreyaPandita6fbff252012-11-13 16:56:48 -050092 pkt_matchdst = simple_eth_packet(dl_dst='00:01:01:01:01:01')
ShreyaPandita4ebbac32012-11-02 13:40:44 -040093 match = parse.packet_to_flow_match(pkt_matchdst)
94 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -040095
ShreyaPandita4ebbac32012-11-02 13:40:44 -040096 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_DST
Shudong Zhou857fb602013-02-06 00:11:38 -080097 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -040098 return (pkt_matchdst,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -040099
ShreyaPanditaed209962012-11-04 02:16:48 -0500100def wildcard_all(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400101# Generate a Wildcard_All Flow
102
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400103 #Create a simple tcp packet and generate wildcard all flow match from it.
104 pkt_wildcard = simple_tcp_packet()
105 match2 = parse.packet_to_flow_match(pkt_wildcard)
106 self.assertTrue(match2 is not None, "Could not generate flow match from pkt")
107 match2.wildcards=ofp.OFPFW_ALL
108 match2.in_port = of_ports[0]
Shudong Zhou857fb602013-02-06 00:11:38 -0800109 match_send_flowadd(self, match2, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400110 return (pkt_wildcard,match2)
ShreyaPandita60e45542012-09-27 15:11:16 -0400111
ShreyaPanditaed209962012-11-04 02:16:48 -0500112def wildcard_all_except_ingress(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400113# Generate Wildcard_All_Except_Ingress_port flow
ShreyaPandita60e45542012-09-27 15:11:16 -0400114
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400115 #Create a simple tcp packet and generate wildcard all except ingress_port flow.
116 pkt_matchingress = simple_tcp_packet()
117 match3 = parse.packet_to_flow_match(pkt_matchingress)
118 self.assertTrue(match3 is not None, "Could not generate flow match from pkt")
119 match3.wildcards = ofp.OFPFW_ALL-ofp.OFPFW_IN_PORT
120 match3.in_port = of_ports[0]
Shudong Zhou857fb602013-02-06 00:11:38 -0800121 match_send_flowadd(self, match3, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400122 return (pkt_matchingress,match3)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400123
ShreyaPanditaed209962012-11-04 02:16:48 -0500124def wildcard_all_except_ingress1(self,of_ports,priority=None):
ShreyaPanditada75f752012-10-26 16:26:35 -0400125# Generate Wildcard_All_Except_Ingress_port flow with action output to port egress_port 2
ShreyaPanditada75f752012-10-26 16:26:35 -0400126
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400127 #Create a simple tcp packet and generate wildcard all except ingress_port flow.
128 pkt_matchingress = simple_tcp_packet()
129 match3 = parse.packet_to_flow_match(pkt_matchingress)
130 self.assertTrue(match3 is not None, "Could not generate flow match from pkt")
131 match3.wildcards = ofp.OFPFW_ALL-ofp.OFPFW_IN_PORT
132 match3.in_port = of_ports[0]
Shudong Zhou857fb602013-02-06 00:11:38 -0800133 match_send_flowadd(self, match3, priority, of_ports[2])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400134 return (pkt_matchingress,match3)
ShreyaPanditada75f752012-10-26 16:26:35 -0400135
136
ShreyaPanditaed209962012-11-04 02:16:48 -0500137def match_vlan_id(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400138 #Generate Match_Vlan_Id
139
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400140 #Create a simple tcp packet and generate match on ethernet dst address flow
141 pkt_matchvlanid = simple_tcp_packet(dl_vlan_enable=True,dl_vlan=1)
142 match = parse.packet_to_flow_match(pkt_matchvlanid)
143 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400144
ShreyaPandita6fbff252012-11-13 16:56:48 -0500145 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_DL_VLAN
Shudong Zhou857fb602013-02-06 00:11:38 -0800146 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400147 return (pkt_matchvlanid,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400148
ShreyaPanditaed209962012-11-04 02:16:48 -0500149def match_vlan_pcp(self,of_ports,priority=None):
ShreyaPandita055102a2012-11-28 11:43:45 -0500150 #Generate Match_Vlan_Priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400151
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400152 #Create a simple tcp packet and generate match on ethernet dst address flow
Rich Laneba9eee82012-12-07 22:44:24 -0800153 pkt_matchvlanpcp = simple_tcp_packet(dl_vlan_enable=True,dl_vlan=1,dl_vlan_pcp=5)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400154 match = parse.packet_to_flow_match(pkt_matchvlanpcp)
155 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400156
ShreyaPandita6fbff252012-11-13 16:56:48 -0500157 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE^ofp.OFPFW_DL_VLAN^ofp.OFPFW_DL_VLAN_PCP
Shudong Zhou857fb602013-02-06 00:11:38 -0800158 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400159 return (pkt_matchvlanpcp,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400160
161
ShreyaPanditaed209962012-11-04 02:16:48 -0500162def match_mul_l2(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400163 #Generate Match_Mul_L2 flow
164
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400165 #Create a simple eth packet and generate match on ethernet protocol flow
166 pkt_mulL2 = simple_eth_packet(dl_type=0x88cc,dl_src='00:01:01:01:01:01',dl_dst='00:01:01:01:01:02')
167 match = parse.packet_to_flow_match(pkt_mulL2)
168 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400169
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400170 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_DL_DST ^ofp.OFPFW_DL_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -0800171 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400172 return (pkt_mulL2,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400173
174
ShreyaPandita6fbff252012-11-13 16:56:48 -0500175def match_mul_l4(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400176 #Generate Match_Mul_L4 flow
177
178 #Create a simple tcp packet and generate match on tcp protocol flow
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400179 pkt_mulL4 = simple_tcp_packet(tcp_sport=111,tcp_dport=112)
180 match = parse.packet_to_flow_match(pkt_mulL4)
181 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita6fbff252012-11-13 16:56:48 -0500182 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO^ofp.OFPFW_TP_SRC ^ofp.OFPFW_TP_DST
Shudong Zhou857fb602013-02-06 00:11:38 -0800183 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400184 return (pkt_mulL4,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400185
ShreyaPanditaed209962012-11-04 02:16:48 -0500186def match_ip_tos(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400187 #Generate a Match on IP Type of service flow
188
Shudong Zhou857fb602013-02-06 00:11:38 -0800189 #Create a simple tcp packet and generate match on Type of service
Rich Laneb5c73792012-12-03 17:12:32 -0800190 pkt_iptos = simple_tcp_packet(ip_tos=28)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400191 match = parse.packet_to_flow_match(pkt_iptos)
192 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400193
ShreyaPandita6fbff252012-11-13 16:56:48 -0500194 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_NW_TOS
Shudong Zhou857fb602013-02-06 00:11:38 -0800195 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400196 return (pkt_iptos,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400197
ShreyaPandita6fbff252012-11-13 16:56:48 -0500198def match_ip_protocol(self,of_ports,priority=None):
199 #Generate a Match on IP Protocol
200
201 #Create a simple tcp packet and generate match on Type of service
202 pkt_iptos = simple_tcp_packet()
203 match = parse.packet_to_flow_match(pkt_iptos)
204 self.assertTrue(match is not None, "Could not generate flow match from pkt")
205
206 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO
Shudong Zhou857fb602013-02-06 00:11:38 -0800207 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita6fbff252012-11-13 16:56:48 -0500208 return (pkt_iptos,match)
209
ShreyaPanditaed209962012-11-04 02:16:48 -0500210def match_tcp_src(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400211 #Generate Match_Tcp_Src
212
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400213 #Create a simple tcp packet and generate match on tcp source port flow
214 pkt_matchtSrc = simple_tcp_packet(tcp_sport=111)
215 match = parse.packet_to_flow_match(pkt_matchtSrc)
216 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400217
ShreyaPandita6fbff252012-11-13 16:56:48 -0500218 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -0800219 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400220 return (pkt_matchtSrc,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400221
ShreyaPanditaed209962012-11-04 02:16:48 -0500222def match_tcp_dst(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400223 #Generate Match_Tcp_Dst
224
Shudong Zhou857fb602013-02-06 00:11:38 -0800225 #Create a simple tcp packet and generate match on tcp destination port flow
ShreyaPandita6fbff252012-11-13 16:56:48 -0500226 pkt_matchdst = simple_tcp_packet(tcp_dport=112)
227 match = parse.packet_to_flow_match(pkt_matchdst)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400228 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400229
ShreyaPandita6fbff252012-11-13 16:56:48 -0500230 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO^ofp.OFPFW_TP_DST
Shudong Zhou857fb602013-02-06 00:11:38 -0800231 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita6fbff252012-11-13 16:56:48 -0500232 return (pkt_matchdst,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400233
ShreyaPandita055102a2012-11-28 11:43:45 -0500234
Shudong Zhouc2f18762013-01-11 00:12:44 -0800235def match_udp_src(self,of_ports,priority=None):
236 #Generate Match_Udp_Src
237
238 #Create a simple udp packet and generate match on udp source port flow
239 pkt_matchtSrc = simple_udp_packet(udp_sport=111)
240 match = parse.packet_to_flow_match(pkt_matchtSrc)
241 self.assertTrue(match is not None, "Could not generate flow match from pkt")
242
243 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -0800244 match_send_flowadd(self, match, priority, of_ports[1])
Shudong Zhouc2f18762013-01-11 00:12:44 -0800245 return (pkt_matchtSrc,match)
246
247def match_udp_dst(self,of_ports,priority=None):
248 #Generate Match_Udp_Dst
249
Shudong Zhou857fb602013-02-06 00:11:38 -0800250 #Create a simple udp packet and generate match on udp destination port flow
Shudong Zhouc2f18762013-01-11 00:12:44 -0800251 pkt_matchdst = simple_udp_packet(udp_dport=112)
252 match = parse.packet_to_flow_match(pkt_matchdst)
253 self.assertTrue(match is not None, "Could not generate flow match from pkt")
254
255 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO^ofp.OFPFW_TP_DST
Shudong Zhou857fb602013-02-06 00:11:38 -0800256 match_send_flowadd(self, match, priority, of_ports[1])
Shudong Zhouc2f18762013-01-11 00:12:44 -0800257 return (pkt_matchdst,match)
258
259
260def match_icmp_type(self,of_ports,priority=None):
261 #Generate Match_Icmp_Type
262
263 #Create a simple icmp packet and generate match on icmp type flow
264 pkt_match = simple_icmp_packet(icmp_type=1)
265 match = parse.packet_to_flow_match(pkt_match)
266 self.assertTrue(match is not None, "Could not generate flow match from pkt")
267
268 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -0800269 match_send_flowadd(self, match, priority, of_ports[1])
Shudong Zhouc2f18762013-01-11 00:12:44 -0800270 return (pkt_match, match)
271
272def match_icmp_code(self,of_ports,priority=None):
273 #Generate Match_Icmp_Code
274
275 #Create a simple icmp packet and generate match on icmp code flow
276 pkt_match = simple_icmp_packet(icmp_code=3)
277 match = parse.packet_to_flow_match(pkt_match)
278 self.assertTrue(match is not None, "Could not generate flow match from pkt")
279
280 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_DST
Shudong Zhou857fb602013-02-06 00:11:38 -0800281 match_send_flowadd(self, match, priority, of_ports[1])
Shudong Zhouc2f18762013-01-11 00:12:44 -0800282 return (pkt_match, match)
283
284
ShreyaPanditaed209962012-11-04 02:16:48 -0500285def match_ethernet_type(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400286 #Generate a Match_Ethernet_Type flow
287
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400288 #Create a simple tcp packet and generate match on ethernet type flow
289 pkt_matchtype = simple_eth_packet(dl_type=0x88cc)
290 match = parse.packet_to_flow_match(pkt_matchtype)
291 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400292
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400293 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE
Shudong Zhou857fb602013-02-06 00:11:38 -0800294 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400295 return (pkt_matchtype,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400296
297
ShreyaPandita60e45542012-09-27 15:11:16 -0400298
ShreyaPandita6fbff252012-11-13 16:56:48 -0500299
ShreyaPanditaed209962012-11-04 02:16:48 -0500300def strict_modify_flow_action(self,egress_port,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400301# Strict Modify the flow Action
302
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400303 #Create a flow_mod message , command MODIFY_STRICT
304 msg5 = message.flow_mod()
305 msg5.match = match
306 msg5.cookie = random.randint(0,9007199254740992)
307 msg5.command = ofp.OFPFC_MODIFY_STRICT
308 msg5.buffer_id = 0xffffffff
309 act5 = action.action_output()
310 act5.port = egress_port
Rich Lanee30455b2013-01-03 16:24:44 -0800311 msg5.actions.add(act5)
ShreyaPandita60e45542012-09-27 15:11:16 -0400312
ShreyaPanditaed209962012-11-04 02:16:48 -0500313 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400314 msg5.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400315
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400316 # Send the flow with action A'
Rich Lane5c3151c2013-01-03 17:15:41 -0800317 self.controller.message_send (msg5)
Rich Lane3a261d52013-01-03 17:45:08 -0800318 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400319
ShreyaPanditaed209962012-11-04 02:16:48 -0500320def modify_flow_action(self,of_ports,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400321# Modify the flow action
322
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400323 #Create a flow_mod message , command MODIFY
324 msg8 = message.flow_mod()
325 msg8.match = match
326 msg8.cookie = random.randint(0,9007199254740992)
327 msg8.command = ofp.OFPFC_MODIFY
328 #out_port will be ignored for flow adds and flow modify (here for test-case Add_Modify_With_Outport)
329 msg8.out_port = of_ports[3]
330 msg8.buffer_id = 0xffffffff
331 act8 = action.action_output()
332 act8.port = of_ports[2]
Rich Lanee30455b2013-01-03 16:24:44 -0800333 msg8.actions.add(act8)
ShreyaPandita60e45542012-09-27 15:11:16 -0400334
ShreyaPanditaed209962012-11-04 02:16:48 -0500335 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400336 msg8.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400337
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400338 # Send the flow with action A'
Rich Lane5c3151c2013-01-03 17:15:41 -0800339 self.controller.message_send (msg8)
Rich Lane3a261d52013-01-03 17:45:08 -0800340 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400341
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400342def enqueue(self,ingress_port,egress_port,egress_queue_id):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400343#Generate a flow with enqueue action i.e output to a queue configured on a egress_port
344
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400345 pkt = simple_tcp_packet()
346 match = packet_to_flow_match(self, pkt)
347 match.wildcards &= ~ofp.OFPFW_IN_PORT
348 self.assertTrue(match is not None,
349 "Could not generate flow match from pkt")
350
351 match.in_port = ingress_port
352 request = message.flow_mod()
353 request.match = match
354 request.buffer_id = 0xffffffff
355 act = action.action_enqueue()
356 act.port = egress_port
357 act.queue_id = egress_queue_id
Rich Lanee30455b2013-01-03 16:24:44 -0800358 request.actions.add(act)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400359
360 logging.info("Inserting flow")
Rich Lane5c3151c2013-01-03 17:15:41 -0800361 self.controller.message_send(request)
Rich Lane3a261d52013-01-03 17:45:08 -0800362 do_barrier(self.controller)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400363 return (pkt,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400364
365
ShreyaPandita60e45542012-09-27 15:11:16 -0400366########################### Verify Stats Functions ###########################################################################################
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400367def get_flowstats(self,match):
368 # Generate flow_stats request
369
370 stat_req = message.flow_stats_request()
371 stat_req.match = match
372 stat_req.table_id = 0xff
373 stat_req.out_port = ofp.OFPP_NONE
374
375 logging.info("Sending stats request")
376 response, pkt = self.controller.transact(stat_req,
377 timeout=5)
378 self.assertTrue(response is not None,"No response to stats request")
ShreyaPandita60e45542012-09-27 15:11:16 -0400379
ShreyaPandita66de26f2012-10-26 14:44:24 -0400380
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400381def get_portstats(self,port_num):
382
383# Return all the port counters in the form a tuple
384 port_stats_req = message.port_stats_request()
385 port_stats_req.port_no = port_num
386 response,pkt = self.controller.transact(port_stats_req)
387 self.assertTrue(response is not None,"No response received for port stats request")
388 rx_pkts=0
389 tx_pkts=0
390 rx_byts=0
391 tx_byts=0
392 rx_drp =0
393 tx_drp = 0
394 rx_err=0
395 tx_err =0
396 rx_fr_err=0
397 rx_ovr_err=0
398 rx_crc_err=0
399 collisions = 0
400 tx_err=0
401
402
403 for obj in response.stats:
404 rx_pkts += obj.rx_packets
405 tx_pkts += obj.tx_packets
406 rx_byts += obj.rx_bytes
407 tx_byts += obj.tx_bytes
408 rx_drp += obj.rx_dropped
409 tx_drp += obj.tx_dropped
410 rx_err += obj.rx_errors
411 rx_fr_err += obj.rx_frame_err
412 rx_ovr_err += obj.rx_over_err
413 rx_crc_err += obj.rx_crc_err
414 collisions+= obj.collisions
415 tx_err += obj.tx_errors
416
417 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)
418
419def get_queuestats(self,port_num,queue_id):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400420#Generate Queue Stats request
421
422 request = message.queue_stats_request()
423 request.port_no = port_num
424 request.queue_id = queue_id
425 (queue_stats, p) = self.controller.transact(request)
426 self.assertNotEqual(queue_stats, None, "Queue stats request failed")
427
428 return (queue_stats,p)
429
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400430def get_tablestats(self):
431# Send Table_Stats request (retrieve current table counters )
432
433 stat_req = message.table_stats_request()
434 response, pkt = self.controller.transact(stat_req,
435 timeout=5)
436 self.assertTrue(response is not None,
437 "No response to stats request")
438 current_lookedup = 0
439 current_matched = 0
440 current_active = 0
441
442 for obj in response.stats:
443 current_lookedup += obj.lookup_count
444 current_matched += obj.matched_count
445 current_active += obj.active_count
446
447 return (current_lookedup,current_matched,current_active)
448
449
450
ShreyaPanditaed209962012-11-04 02:16:48 -0500451def verify_tablestats(self,expect_lookup=None,expect_match=None,expect_active=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400452
453 stat_req = message.table_stats_request()
ShreyaPanditaed209962012-11-04 02:16:48 -0500454
Rich Lane90b3d732012-12-31 10:03:50 -0800455 for i in range(0,100):
ShreyaPandita60e45542012-09-27 15:11:16 -0400456
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400457 logging.info("Sending stats request")
458 # TODO: move REPLY_MORE handling to controller.transact?
ShreyaPandita66de26f2012-10-26 14:44:24 -0400459 response, pkt = self.controller.transact(stat_req,
460 timeout=5)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400461 self.assertTrue(response is not None,"No response to stats request")
ShreyaPandita60e45542012-09-27 15:11:16 -0400462
ShreyaPanditaed209962012-11-04 02:16:48 -0500463 lookedup = 0
464 matched = 0
465 active = 0
466
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400467 for item in response.stats:
468 lookedup += item.lookup_count
469 matched += item.matched_count
470 active += item.active_count
471
Rich Laneafcd0dd2013-01-03 20:54:56 -0800472 logging.info("Packets Looked up: %d", lookedup)
473 logging.info("Packets matched: %d", matched)
474 logging.info("Active flow entries: %d", active)
ShreyaPanditaed209962012-11-04 02:16:48 -0500475
Rich Lane175f9562013-01-03 20:58:40 -0800476 if (expect_lookup == None or lookedup >= expect_lookup) and \
477 (expect_match == None or matched >= expect_match) and \
478 (expect_active == None or active >= expect_active):
Rich Lane90b3d732012-12-31 10:03:50 -0800479 break
480
481 sleep(0.1)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400482
ShreyaPanditaed209962012-11-04 02:16:48 -0500483 if expect_lookup != None :
Rich Lane3e777792013-01-03 21:30:30 -0800484 self.assertEqual(expect_lookup, lookedup, "lookup counter is not incremented properly")
ShreyaPanditaed209962012-11-04 02:16:48 -0500485 if expect_match != None :
Rich Lane3e777792013-01-03 21:30:30 -0800486 self.assertEqual(expect_match, matched, "matched counter is not incremented properly")
ShreyaPanditaed209962012-11-04 02:16:48 -0500487 if expect_active != None :
Rich Lane3e777792013-01-03 21:30:30 -0800488 self.assertEqual(expect_active, active ,"active counter is not incremented properly")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400489
490
ShreyaPanditaed209962012-11-04 02:16:48 -0500491def verify_flowstats(self,match,byte_count=None,packet_count=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400492 # Verify flow counters : byte_count and packet_count
ShreyaPandita60e45542012-09-27 15:11:16 -0400493
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400494 stat_req = message.flow_stats_request()
495 stat_req.match = match
496 stat_req.table_id = 0xff
497 stat_req.out_port = ofp.OFPP_NONE
ShreyaPanditaed209962012-11-04 02:16:48 -0500498
Rich Lane90b3d732012-12-31 10:03:50 -0800499 for i in range(0,100):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400500 logging.info("Sending stats request")
501 # TODO: move REPLY_MORE handling to controller.transact?
ShreyaPandita66de26f2012-10-26 14:44:24 -0400502 response, pkt = self.controller.transact(stat_req,
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400503 timeout=5)
504 self.assertTrue(response is not None,"No response to stats request")
505
ShreyaPanditaed209962012-11-04 02:16:48 -0500506 packet_counter = 0
507 byte_counter = 0
508
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400509 for item in response.stats:
510 packet_counter += item.packet_count
511 byte_counter += item.byte_count
512
Rich Lanec9e41842013-01-03 21:31:42 -0800513 logging.info("Received %d packets", packet_counter)
514 logging.info("Received %d bytes", byte_counter)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400515
Rich Lanec9e41842013-01-03 21:31:42 -0800516 if (packet_count == None or packet_counter >= packet_count) and \
517 (byte_count == None or byte_counter >= byte_count):
Rich Lane90b3d732012-12-31 10:03:50 -0800518 break
519
520 sleep(0.1)
ShreyaPanditaed209962012-11-04 02:16:48 -0500521
522 if packet_count != None :
Rich Lanec9e41842013-01-03 21:31:42 -0800523 self.assertEqual(packet_count, packet_counter, "packet_count counter is not incremented correctly")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400524
ShreyaPanditaed209962012-11-04 02:16:48 -0500525 if byte_count != None :
Rich Lanec9e41842013-01-03 21:31:42 -0800526 self.assertEqual(byte_count, byte_counter, "byte_count counter is not incremented correctly")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400527
528
ShreyaPanditaed209962012-11-04 02:16:48 -0500529def verify_portstats(self, port,tx_packets=None,rx_packets=None,rx_byte=None,tx_byte=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400530
531
532 stat_req = message.port_stats_request()
533 stat_req.port_no = port
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400534
Rich Lane90b3d732012-12-31 10:03:50 -0800535 for i in range(0,100):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400536 logging.info("Sending stats request")
537 response, pkt = self.controller.transact(stat_req,
538 timeout=5)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400539 self.assertTrue(response is not None,
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400540 "No response to stats request")
541 self.assertTrue(len(response.stats) == 1,
542 "Did not receive port stats reply")
ShreyaPanditaed209962012-11-04 02:16:48 -0500543
544 sentp = recvp = 0
545 sentb = recvb = 0
546
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400547 for item in response.stats:
548 sentp += item.tx_packets
549 recvp += item.rx_packets
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400550 recvb += item.rx_bytes
ShreyaPanditaed209962012-11-04 02:16:48 -0500551 sentb += item.tx_bytes
552
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400553
554 logging.info("Sent " + str(sentp) + " packets")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400555 logging.info("Received " + str(recvp) + " packets")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400556 logging.info("Received " + str(recvb) + "bytes")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400557 logging.info("Sent" + str(sentb) + "bytes")
Rich Lane90b3d732012-12-31 10:03:50 -0800558
559 if (tx_packets == None or tx_packets == sentp) and \
560 (rx_packets == None or rx_packets == recvp) and \
561 (tx_byte == None or tx_byte == sentb) and \
562 (rx_byte == None or rx_byte == recvb):
563 break
564
565 sleep(0.1)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400566
ShreyaPanditaa6dfbfc2012-11-05 17:45:22 -0500567
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400568
ShreyaPanditaed209962012-11-04 02:16:48 -0500569 if (tx_packets != None):
570 self.assertEqual(tx_packets,item.tx_packets,"rx_packets counter is not incremented correctly")
571 if (rx_packets != None):
572 self.assertEqual(rx_packets,item.rx_packets,"tx_packets counter is not incremented correctly")
573 if (rx_byte != None):
574 self.assertEqual(rx_byte,item.rx_bytes,"rx_bytes counter is not incremented correctly")
575 if (tx_byte != None):
576 self.assertEqual(tx_byte,item.tx_bytes,"tx_bytes counter is not incremented correctly")
ShreyaPandita60e45542012-09-27 15:11:16 -0400577
ShreyaPandita60e45542012-09-27 15:11:16 -0400578
ShreyaPanditaed209962012-11-04 02:16:48 -0500579def verify_queuestats(self,port_num,queue_id,expect_packet=None,expect_byte=None):
580
581 # Verify queue counters : tx_packets and tx_bytes
582
583 request = message.queue_stats_request()
584 request.port_no = port_num
585 request.queue_id = queue_id
586
Rich Lane90b3d732012-12-31 10:03:50 -0800587 for i in range(0,100):
ShreyaPanditaed209962012-11-04 02:16:48 -0500588
589 logging.info("Sending stats request")
590
591 (queue_stats, p) = self.controller.transact(request)
592 self.assertNotEqual(queue_stats, None, "Queue stats request failed")
593 packet_counter = 0
594 byte_counter = 0
595
ShreyaPanditaa6dfbfc2012-11-05 17:45:22 -0500596 for item in queue_stats.stats:
ShreyaPanditaed209962012-11-04 02:16:48 -0500597 packet_counter += item.tx_packets
598 byte_counter += item.tx_bytes
599
600 logging.info("Transmitted" + str(packet_counter) + " packets")
601 logging.info("Transmitted" + str(byte_counter) + "bytes")
602
Rich Lane90b3d732012-12-31 10:03:50 -0800603 if (expect_packet == None or packet_counter == expect_packet) and \
604 (expect_byte == None or byte_counter == expect_byte):
605 break
ShreyaPanditaed209962012-11-04 02:16:48 -0500606
Rich Lane90b3d732012-12-31 10:03:50 -0800607 sleep(0.1)
ShreyaPanditaed209962012-11-04 02:16:48 -0500608
609 if expect_packet != None :
610 self.assertEqual(packet_counter,expect_packet,"tx_packets counter is not incremented correctly")
611
612 if expect_byte != None :
613 self.assertEqual(byte_counter,expect_byte,"tx_bytes counter is not incremented correctly")
614
615
ShreyaPandita60e45542012-09-27 15:11:16 -0400616############################## Various delete commands #############################################################################################
617
ShreyaPanditaed209962012-11-04 02:16:48 -0500618def strict_delete(self,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400619# Issue Strict Delete
620
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400621 #Create flow_mod message, command DELETE_STRICT
622 msg4 = message.flow_mod()
623 msg4.out_port = ofp.OFPP_NONE
624 msg4.command = ofp.OFPFC_DELETE_STRICT
625 msg4.buffer_id = 0xffffffff
626 msg4.match = match
ShreyaPandita60e45542012-09-27 15:11:16 -0400627
ShreyaPanditaed209962012-11-04 02:16:48 -0500628 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400629 msg4.priority = priority
Rich Lane5c3151c2013-01-03 17:15:41 -0800630 self.controller.message_send(msg4)
Rich Lane3a261d52013-01-03 17:45:08 -0800631 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400632
633
634
ShreyaPanditaed209962012-11-04 02:16:48 -0500635def nonstrict_delete(self,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400636# Issue Non_Strict Delete
637
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400638 #Create flow_mod message, command DELETE
639 msg6 = message.flow_mod()
640 msg6.out_port = ofp.OFPP_NONE
641 msg6.command = ofp.OFPFC_DELETE
642 msg6.buffer_id = 0xffffffff
643 msg6.match = match
ShreyaPandita60e45542012-09-27 15:11:16 -0400644
ShreyaPanditaed209962012-11-04 02:16:48 -0500645 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400646 msg6.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400647
Rich Lane5c3151c2013-01-03 17:15:41 -0800648 self.controller.message_send(msg6)
Rich Lane3a261d52013-01-03 17:45:08 -0800649 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400650
651
652###########################################################################################################################################################
653
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400654def send_packet(obj, pkt, ingress_port, egress_port):
ShreyaPandita60e45542012-09-27 15:11:16 -0400655#Send Packets on a specified ingress_port and verify if its recieved on correct egress_port.
656
657 obj.dataplane.send(ingress_port, str(pkt))
658 exp_pkt_arg = pkt
659 exp_port = egress_port
660
661 (rcv_port, rcv_pkt, pkt_time) = obj.dataplane.poll(timeout=2,
662 port_number=exp_port,
663 exp_pkt=exp_pkt_arg)
664 obj.assertTrue(rcv_pkt is not None,
665 "Packet not received on port " + str(egress_port))
666 obj.assertEqual(rcv_port, egress_port,
667 "Packet received on port " + str(rcv_port) +
668 ", expected port " + str(egress_port))
669 obj.assertEqual(str(pkt), str(rcv_pkt),
670 'Response packet does not match send packet')
671
672
ShreyaPandita572e64b2012-09-28 14:41:06 -0400673def sw_supported_actions(parent,use_cache=False):
ShreyaPandita60e45542012-09-27 15:11:16 -0400674#Returns the switch's supported actions
675
676 cache_supported_actions = None
677 if cache_supported_actions is None or not use_cache:
678 request = message.features_request()
679 (reply, pkt) = parent.controller.transact(request)
680 parent.assertTrue(reply is not None, "Did not get response to ftr req")
681 cache_supported_actions = reply.actions
682 return cache_supported_actions
683
ShreyaPandita66de26f2012-10-26 14:44:24 -0400684##############################################################################################################################################################
685