blob: 87df355363da050b4678942adae074f9581d3833 [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
Shudong Zhoudceec932013-02-06 01:12:54 -0800284def match_arp_sender(self,of_ports,priority=None):
285 #Generate Match_Arp_Sender
286
287 #Create a simple icmp packet and generate match on arp sender flow
288 pkt_match = simple_arp_packet()
289 match = parse.packet_to_flow_match(pkt_match)
290 self.assertTrue(match is not None, "Could not generate flow match from pkt")
291
292 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_NW_SRC_MASK
293 match_send_flowadd(self, match, priority, of_ports[1])
294 return (pkt_match, match)
295
296def match_arp_target(self,of_ports,priority=None):
297 #Generate Match_Arp_Target
298
299 #Create a simple icmp packet and generate match on arp target flow
300 pkt_match = simple_arp_packet()
301 match = parse.packet_to_flow_match(pkt_match)
302 self.assertTrue(match is not None, "Could not generate flow match from pkt")
303
304 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_NW_DST_MASK
305 match_send_flowadd(self, match, priority, of_ports[1])
306 return (pkt_match, match)
307
Shudong Zhouc2f18762013-01-11 00:12:44 -0800308
ShreyaPanditaed209962012-11-04 02:16:48 -0500309def match_ethernet_type(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400310 #Generate a Match_Ethernet_Type flow
311
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400312 #Create a simple tcp packet and generate match on ethernet type flow
313 pkt_matchtype = simple_eth_packet(dl_type=0x88cc)
314 match = parse.packet_to_flow_match(pkt_matchtype)
315 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400316
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400317 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE
Shudong Zhou857fb602013-02-06 00:11:38 -0800318 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400319 return (pkt_matchtype,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400320
321
ShreyaPandita60e45542012-09-27 15:11:16 -0400322
ShreyaPandita6fbff252012-11-13 16:56:48 -0500323
ShreyaPanditaed209962012-11-04 02:16:48 -0500324def strict_modify_flow_action(self,egress_port,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400325# Strict Modify the flow Action
326
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400327 #Create a flow_mod message , command MODIFY_STRICT
328 msg5 = message.flow_mod()
329 msg5.match = match
330 msg5.cookie = random.randint(0,9007199254740992)
331 msg5.command = ofp.OFPFC_MODIFY_STRICT
332 msg5.buffer_id = 0xffffffff
333 act5 = action.action_output()
334 act5.port = egress_port
Rich Lanee30455b2013-01-03 16:24:44 -0800335 msg5.actions.add(act5)
ShreyaPandita60e45542012-09-27 15:11:16 -0400336
ShreyaPanditaed209962012-11-04 02:16:48 -0500337 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400338 msg5.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400339
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400340 # Send the flow with action A'
Rich Lane5c3151c2013-01-03 17:15:41 -0800341 self.controller.message_send (msg5)
Rich Lane3a261d52013-01-03 17:45:08 -0800342 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400343
ShreyaPanditaed209962012-11-04 02:16:48 -0500344def modify_flow_action(self,of_ports,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400345# Modify the flow action
346
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400347 #Create a flow_mod message , command MODIFY
348 msg8 = message.flow_mod()
349 msg8.match = match
350 msg8.cookie = random.randint(0,9007199254740992)
351 msg8.command = ofp.OFPFC_MODIFY
352 #out_port will be ignored for flow adds and flow modify (here for test-case Add_Modify_With_Outport)
353 msg8.out_port = of_ports[3]
354 msg8.buffer_id = 0xffffffff
355 act8 = action.action_output()
356 act8.port = of_ports[2]
Rich Lanee30455b2013-01-03 16:24:44 -0800357 msg8.actions.add(act8)
ShreyaPandita60e45542012-09-27 15:11:16 -0400358
ShreyaPanditaed209962012-11-04 02:16:48 -0500359 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400360 msg8.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400361
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400362 # Send the flow with action A'
Rich Lane5c3151c2013-01-03 17:15:41 -0800363 self.controller.message_send (msg8)
Rich Lane3a261d52013-01-03 17:45:08 -0800364 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400365
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400366def enqueue(self,ingress_port,egress_port,egress_queue_id):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400367#Generate a flow with enqueue action i.e output to a queue configured on a egress_port
368
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400369 pkt = simple_tcp_packet()
370 match = packet_to_flow_match(self, pkt)
371 match.wildcards &= ~ofp.OFPFW_IN_PORT
372 self.assertTrue(match is not None,
373 "Could not generate flow match from pkt")
374
375 match.in_port = ingress_port
376 request = message.flow_mod()
377 request.match = match
378 request.buffer_id = 0xffffffff
379 act = action.action_enqueue()
380 act.port = egress_port
381 act.queue_id = egress_queue_id
Rich Lanee30455b2013-01-03 16:24:44 -0800382 request.actions.add(act)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400383
384 logging.info("Inserting flow")
Rich Lane5c3151c2013-01-03 17:15:41 -0800385 self.controller.message_send(request)
Rich Lane3a261d52013-01-03 17:45:08 -0800386 do_barrier(self.controller)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400387 return (pkt,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400388
389
ShreyaPandita60e45542012-09-27 15:11:16 -0400390########################### Verify Stats Functions ###########################################################################################
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400391def get_flowstats(self,match):
392 # Generate flow_stats request
393
394 stat_req = message.flow_stats_request()
395 stat_req.match = match
396 stat_req.table_id = 0xff
397 stat_req.out_port = ofp.OFPP_NONE
398
399 logging.info("Sending stats request")
400 response, pkt = self.controller.transact(stat_req,
401 timeout=5)
402 self.assertTrue(response is not None,"No response to stats request")
ShreyaPandita60e45542012-09-27 15:11:16 -0400403
ShreyaPandita66de26f2012-10-26 14:44:24 -0400404
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400405def get_portstats(self,port_num):
406
407# Return all the port counters in the form a tuple
408 port_stats_req = message.port_stats_request()
409 port_stats_req.port_no = port_num
410 response,pkt = self.controller.transact(port_stats_req)
411 self.assertTrue(response is not None,"No response received for port stats request")
412 rx_pkts=0
413 tx_pkts=0
414 rx_byts=0
415 tx_byts=0
416 rx_drp =0
417 tx_drp = 0
418 rx_err=0
419 tx_err =0
420 rx_fr_err=0
421 rx_ovr_err=0
422 rx_crc_err=0
423 collisions = 0
424 tx_err=0
425
426
427 for obj in response.stats:
428 rx_pkts += obj.rx_packets
429 tx_pkts += obj.tx_packets
430 rx_byts += obj.rx_bytes
431 tx_byts += obj.tx_bytes
432 rx_drp += obj.rx_dropped
433 tx_drp += obj.tx_dropped
434 rx_err += obj.rx_errors
435 rx_fr_err += obj.rx_frame_err
436 rx_ovr_err += obj.rx_over_err
437 rx_crc_err += obj.rx_crc_err
438 collisions+= obj.collisions
439 tx_err += obj.tx_errors
440
441 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)
442
443def get_queuestats(self,port_num,queue_id):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400444#Generate Queue Stats request
445
446 request = message.queue_stats_request()
447 request.port_no = port_num
448 request.queue_id = queue_id
449 (queue_stats, p) = self.controller.transact(request)
450 self.assertNotEqual(queue_stats, None, "Queue stats request failed")
451
452 return (queue_stats,p)
453
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400454def get_tablestats(self):
455# Send Table_Stats request (retrieve current table counters )
456
457 stat_req = message.table_stats_request()
458 response, pkt = self.controller.transact(stat_req,
459 timeout=5)
460 self.assertTrue(response is not None,
461 "No response to stats request")
462 current_lookedup = 0
463 current_matched = 0
464 current_active = 0
465
466 for obj in response.stats:
467 current_lookedup += obj.lookup_count
468 current_matched += obj.matched_count
469 current_active += obj.active_count
470
471 return (current_lookedup,current_matched,current_active)
472
473
474
ShreyaPanditaed209962012-11-04 02:16:48 -0500475def verify_tablestats(self,expect_lookup=None,expect_match=None,expect_active=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400476
477 stat_req = message.table_stats_request()
ShreyaPanditaed209962012-11-04 02:16:48 -0500478
Rich Lane90b3d732012-12-31 10:03:50 -0800479 for i in range(0,100):
ShreyaPandita60e45542012-09-27 15:11:16 -0400480
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400481 logging.info("Sending stats request")
482 # TODO: move REPLY_MORE handling to controller.transact?
ShreyaPandita66de26f2012-10-26 14:44:24 -0400483 response, pkt = self.controller.transact(stat_req,
484 timeout=5)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400485 self.assertTrue(response is not None,"No response to stats request")
ShreyaPandita60e45542012-09-27 15:11:16 -0400486
ShreyaPanditaed209962012-11-04 02:16:48 -0500487 lookedup = 0
488 matched = 0
489 active = 0
490
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400491 for item in response.stats:
492 lookedup += item.lookup_count
493 matched += item.matched_count
494 active += item.active_count
495
Rich Laneafcd0dd2013-01-03 20:54:56 -0800496 logging.info("Packets Looked up: %d", lookedup)
497 logging.info("Packets matched: %d", matched)
498 logging.info("Active flow entries: %d", active)
ShreyaPanditaed209962012-11-04 02:16:48 -0500499
Rich Lane175f9562013-01-03 20:58:40 -0800500 if (expect_lookup == None or lookedup >= expect_lookup) and \
501 (expect_match == None or matched >= expect_match) and \
502 (expect_active == None or active >= expect_active):
Rich Lane90b3d732012-12-31 10:03:50 -0800503 break
504
505 sleep(0.1)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400506
ShreyaPanditaed209962012-11-04 02:16:48 -0500507 if expect_lookup != None :
Rich Lane3e777792013-01-03 21:30:30 -0800508 self.assertEqual(expect_lookup, lookedup, "lookup counter is not incremented properly")
ShreyaPanditaed209962012-11-04 02:16:48 -0500509 if expect_match != None :
Rich Lane3e777792013-01-03 21:30:30 -0800510 self.assertEqual(expect_match, matched, "matched counter is not incremented properly")
ShreyaPanditaed209962012-11-04 02:16:48 -0500511 if expect_active != None :
Rich Lane3e777792013-01-03 21:30:30 -0800512 self.assertEqual(expect_active, active ,"active counter is not incremented properly")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400513
514
ShreyaPanditaed209962012-11-04 02:16:48 -0500515def verify_flowstats(self,match,byte_count=None,packet_count=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400516 # Verify flow counters : byte_count and packet_count
ShreyaPandita60e45542012-09-27 15:11:16 -0400517
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400518 stat_req = message.flow_stats_request()
519 stat_req.match = match
520 stat_req.table_id = 0xff
521 stat_req.out_port = ofp.OFPP_NONE
ShreyaPanditaed209962012-11-04 02:16:48 -0500522
Rich Lane90b3d732012-12-31 10:03:50 -0800523 for i in range(0,100):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400524 logging.info("Sending stats request")
525 # TODO: move REPLY_MORE handling to controller.transact?
ShreyaPandita66de26f2012-10-26 14:44:24 -0400526 response, pkt = self.controller.transact(stat_req,
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400527 timeout=5)
528 self.assertTrue(response is not None,"No response to stats request")
529
ShreyaPanditaed209962012-11-04 02:16:48 -0500530 packet_counter = 0
531 byte_counter = 0
532
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400533 for item in response.stats:
534 packet_counter += item.packet_count
535 byte_counter += item.byte_count
536
Rich Lanec9e41842013-01-03 21:31:42 -0800537 logging.info("Received %d packets", packet_counter)
538 logging.info("Received %d bytes", byte_counter)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400539
Rich Lanec9e41842013-01-03 21:31:42 -0800540 if (packet_count == None or packet_counter >= packet_count) and \
541 (byte_count == None or byte_counter >= byte_count):
Rich Lane90b3d732012-12-31 10:03:50 -0800542 break
543
544 sleep(0.1)
ShreyaPanditaed209962012-11-04 02:16:48 -0500545
546 if packet_count != None :
Rich Lanec9e41842013-01-03 21:31:42 -0800547 self.assertEqual(packet_count, packet_counter, "packet_count counter is not incremented correctly")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400548
ShreyaPanditaed209962012-11-04 02:16:48 -0500549 if byte_count != None :
Rich Lanec9e41842013-01-03 21:31:42 -0800550 self.assertEqual(byte_count, byte_counter, "byte_count counter is not incremented correctly")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400551
552
ShreyaPanditaed209962012-11-04 02:16:48 -0500553def verify_portstats(self, port,tx_packets=None,rx_packets=None,rx_byte=None,tx_byte=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400554
555
556 stat_req = message.port_stats_request()
557 stat_req.port_no = port
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400558
Rich Lane90b3d732012-12-31 10:03:50 -0800559 for i in range(0,100):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400560 logging.info("Sending stats request")
561 response, pkt = self.controller.transact(stat_req,
562 timeout=5)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400563 self.assertTrue(response is not None,
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400564 "No response to stats request")
565 self.assertTrue(len(response.stats) == 1,
566 "Did not receive port stats reply")
ShreyaPanditaed209962012-11-04 02:16:48 -0500567
568 sentp = recvp = 0
569 sentb = recvb = 0
570
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400571 for item in response.stats:
572 sentp += item.tx_packets
573 recvp += item.rx_packets
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400574 recvb += item.rx_bytes
ShreyaPanditaed209962012-11-04 02:16:48 -0500575 sentb += item.tx_bytes
576
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400577
578 logging.info("Sent " + str(sentp) + " packets")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400579 logging.info("Received " + str(recvp) + " packets")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400580 logging.info("Received " + str(recvb) + "bytes")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400581 logging.info("Sent" + str(sentb) + "bytes")
Rich Lane90b3d732012-12-31 10:03:50 -0800582
583 if (tx_packets == None or tx_packets == sentp) and \
584 (rx_packets == None or rx_packets == recvp) and \
585 (tx_byte == None or tx_byte == sentb) and \
586 (rx_byte == None or rx_byte == recvb):
587 break
588
589 sleep(0.1)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400590
ShreyaPanditaa6dfbfc2012-11-05 17:45:22 -0500591
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400592
ShreyaPanditaed209962012-11-04 02:16:48 -0500593 if (tx_packets != None):
594 self.assertEqual(tx_packets,item.tx_packets,"rx_packets counter is not incremented correctly")
595 if (rx_packets != None):
596 self.assertEqual(rx_packets,item.rx_packets,"tx_packets counter is not incremented correctly")
597 if (rx_byte != None):
598 self.assertEqual(rx_byte,item.rx_bytes,"rx_bytes counter is not incremented correctly")
599 if (tx_byte != None):
600 self.assertEqual(tx_byte,item.tx_bytes,"tx_bytes counter is not incremented correctly")
ShreyaPandita60e45542012-09-27 15:11:16 -0400601
ShreyaPandita60e45542012-09-27 15:11:16 -0400602
ShreyaPanditaed209962012-11-04 02:16:48 -0500603def verify_queuestats(self,port_num,queue_id,expect_packet=None,expect_byte=None):
604
605 # Verify queue counters : tx_packets and tx_bytes
606
607 request = message.queue_stats_request()
608 request.port_no = port_num
609 request.queue_id = queue_id
610
Rich Lane90b3d732012-12-31 10:03:50 -0800611 for i in range(0,100):
ShreyaPanditaed209962012-11-04 02:16:48 -0500612
613 logging.info("Sending stats request")
614
615 (queue_stats, p) = self.controller.transact(request)
616 self.assertNotEqual(queue_stats, None, "Queue stats request failed")
617 packet_counter = 0
618 byte_counter = 0
619
ShreyaPanditaa6dfbfc2012-11-05 17:45:22 -0500620 for item in queue_stats.stats:
ShreyaPanditaed209962012-11-04 02:16:48 -0500621 packet_counter += item.tx_packets
622 byte_counter += item.tx_bytes
623
624 logging.info("Transmitted" + str(packet_counter) + " packets")
625 logging.info("Transmitted" + str(byte_counter) + "bytes")
626
Rich Lane90b3d732012-12-31 10:03:50 -0800627 if (expect_packet == None or packet_counter == expect_packet) and \
628 (expect_byte == None or byte_counter == expect_byte):
629 break
ShreyaPanditaed209962012-11-04 02:16:48 -0500630
Rich Lane90b3d732012-12-31 10:03:50 -0800631 sleep(0.1)
ShreyaPanditaed209962012-11-04 02:16:48 -0500632
633 if expect_packet != None :
634 self.assertEqual(packet_counter,expect_packet,"tx_packets counter is not incremented correctly")
635
636 if expect_byte != None :
637 self.assertEqual(byte_counter,expect_byte,"tx_bytes counter is not incremented correctly")
638
639
ShreyaPandita60e45542012-09-27 15:11:16 -0400640############################## Various delete commands #############################################################################################
641
ShreyaPanditaed209962012-11-04 02:16:48 -0500642def strict_delete(self,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400643# Issue Strict Delete
644
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400645 #Create flow_mod message, command DELETE_STRICT
646 msg4 = message.flow_mod()
647 msg4.out_port = ofp.OFPP_NONE
648 msg4.command = ofp.OFPFC_DELETE_STRICT
649 msg4.buffer_id = 0xffffffff
650 msg4.match = match
ShreyaPandita60e45542012-09-27 15:11:16 -0400651
ShreyaPanditaed209962012-11-04 02:16:48 -0500652 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400653 msg4.priority = priority
Rich Lane5c3151c2013-01-03 17:15:41 -0800654 self.controller.message_send(msg4)
Rich Lane3a261d52013-01-03 17:45:08 -0800655 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400656
657
658
ShreyaPanditaed209962012-11-04 02:16:48 -0500659def nonstrict_delete(self,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400660# Issue Non_Strict Delete
661
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400662 #Create flow_mod message, command DELETE
663 msg6 = message.flow_mod()
664 msg6.out_port = ofp.OFPP_NONE
665 msg6.command = ofp.OFPFC_DELETE
666 msg6.buffer_id = 0xffffffff
667 msg6.match = match
ShreyaPandita60e45542012-09-27 15:11:16 -0400668
ShreyaPanditaed209962012-11-04 02:16:48 -0500669 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400670 msg6.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400671
Rich Lane5c3151c2013-01-03 17:15:41 -0800672 self.controller.message_send(msg6)
Rich Lane3a261d52013-01-03 17:45:08 -0800673 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400674
675
676###########################################################################################################################################################
677
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400678def send_packet(obj, pkt, ingress_port, egress_port):
ShreyaPandita60e45542012-09-27 15:11:16 -0400679#Send Packets on a specified ingress_port and verify if its recieved on correct egress_port.
680
681 obj.dataplane.send(ingress_port, str(pkt))
682 exp_pkt_arg = pkt
683 exp_port = egress_port
684
685 (rcv_port, rcv_pkt, pkt_time) = obj.dataplane.poll(timeout=2,
686 port_number=exp_port,
687 exp_pkt=exp_pkt_arg)
688 obj.assertTrue(rcv_pkt is not None,
689 "Packet not received on port " + str(egress_port))
690 obj.assertEqual(rcv_port, egress_port,
691 "Packet received on port " + str(rcv_port) +
692 ", expected port " + str(egress_port))
693 obj.assertEqual(str(pkt), str(rcv_pkt),
694 'Response packet does not match send packet')
695
696
ShreyaPandita572e64b2012-09-28 14:41:06 -0400697def sw_supported_actions(parent,use_cache=False):
ShreyaPandita60e45542012-09-27 15:11:16 -0400698#Returns the switch's supported actions
699
700 cache_supported_actions = None
701 if cache_supported_actions is None or not use_cache:
702 request = message.features_request()
703 (reply, pkt) = parent.controller.transact(request)
704 parent.assertTrue(reply is not None, "Did not get response to ftr req")
705 cache_supported_actions = reply.actions
706 return cache_supported_actions
707
ShreyaPandita66de26f2012-10-26 14:44:24 -0400708##############################################################################################################################################################
709