blob: 8c5e388185dc38a5ccd6d3b6b4f7d6b15edbfc18 [file] [log] [blame]
Matteo Scandoloa229eca2017-08-08 13:05:28 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
ShreyaPandita055102a2012-11-28 11:43:45 -050017""" Defined Some common functions used by Conformance tests -- OF-SWITCH 1.0.0 Testcases """
ShreyaPandita60e45542012-09-27 15:11:16 -040018
19import sys
20import copy
21import random
22
23import oftest.controller as controller
Rich Laned7b0ffa2013-03-08 15:53:42 -080024import ofp
ShreyaPandita60e45542012-09-27 15:11:16 -040025import oftest.dataplane as dataplane
ShreyaPandita60e45542012-09-27 15:11:16 -040026import oftest.parse as parse
27import logging
28import types
ShreyaPandita66de26f2012-10-26 14:44:24 -040029
Rich Laneb90a1c42012-10-05 09:16:05 -070030import oftest.base_tests as base_tests
Rich Laneda3b5ad2012-10-03 09:05:32 -070031from oftest.testutils import *
ShreyaPandita60e45542012-09-27 15:11:16 -040032from time import sleep
33
34#################### Functions for various types of flow_mod ##########################################################################################
35
Shudong Zhou857fb602013-02-06 00:11:38 -080036def match_send_flowadd(self, match, priority, port):
Rich Laneba3f0e22013-03-11 16:43:57 -070037 msg = ofp.message.flow_add()
Shudong Zhou857fb602013-02-06 00:11:38 -080038 msg.out_port = ofp.OFPP_NONE
Shudong Zhou857fb602013-02-06 00:11:38 -080039 # msg.cookie = random.randint(0,9007199254740992)
40 msg.buffer_id = 0xffffffff
41 msg.match = match
42 if priority != None :
43 msg.priority = priority
Rich Lane9d3cc6b2013-03-08 16:33:08 -080044 act = ofp.action.output()
Shudong Zhou857fb602013-02-06 00:11:38 -080045 act.port = port
Rich Lanec495d9e2013-03-08 17:43:36 -080046 msg.actions.append(act)
Shudong Zhou857fb602013-02-06 00:11:38 -080047 self.controller.message_send(msg)
48 do_barrier(self.controller)
49
ShreyaPanditaed209962012-11-04 02:16:48 -050050def exact_match(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -040051# Generate ExactMatch flow .
52
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]
Rich Laned0478ff2013-03-11 12:46:58 -070058 #match.ipv4_src = 1
ShreyaPandita4ebbac32012-11-02 13:40:44 -040059 match.wildcards=0
Shudong Zhou857fb602013-02-06 00:11:38 -080060 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -040061 return (pkt_exactflow,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -040062
ShreyaPanditaed209962012-11-04 02:16:48 -050063def exact_match_with_prio(self,of_ports,priority=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -040064 # Generate ExactMatch with action output to port 2
ShreyaPandita66de26f2012-10-26 14:44:24 -040065
ShreyaPandita4ebbac32012-11-02 13:40:44 -040066 #Create a simple tcp packet and generate exact flow match from it.
67 pkt_exactflow = simple_tcp_packet()
68 match = parse.packet_to_flow_match(pkt_exactflow)
69 self.assertTrue(match is not None, "Could not generate flow match from pkt")
70 match.in_port = of_ports[0]
Rich Laned0478ff2013-03-11 12:46:58 -070071 #match.ipv4_src = 1
ShreyaPandita4ebbac32012-11-02 13:40:44 -040072 match.wildcards=0
Shudong Zhou857fb602013-02-06 00:11:38 -080073 match_send_flowadd(self, match, priority, of_ports[2])
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]
Rich Laned0478ff2013-03-11 12:46:58 -070085 #match1.ipv4_src = 1
ShreyaPandita4ebbac32012-11-02 13:40:44 -040086 match1.wildcards = ofp.OFPFW_DL_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -080087 match_send_flowadd(self, match1, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -040088 return (pkt_wildcardsrc,match1)
ShreyaPandita66de26f2012-10-26 14:44:24 -040089
ShreyaPandita6fbff252012-11-13 16:56:48 -050090def match_ethernet_src_address(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -040091 #Generate Match_Ethernet_SrC_Address flow
92
ShreyaPandita4ebbac32012-11-02 13:40:44 -040093 #Create a simple tcp packet and generate match on ethernet src address flow
Rich Laned0478ff2013-03-11 12:46:58 -070094 pkt_MatchSrc = simple_eth_packet(eth_src='00:01:01:01:01:01')
ShreyaPandita4ebbac32012-11-02 13:40:44 -040095 match = parse.packet_to_flow_match(pkt_MatchSrc)
96 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita4ebbac32012-11-02 13:40:44 -040097 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -080098 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -040099 return (pkt_MatchSrc,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400100
ShreyaPanditaed209962012-11-04 02:16:48 -0500101def match_ethernet_dst_address(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400102 #Generate Match_Ethernet_Dst_Address flow
103
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400104 #Create a simple tcp packet and generate match on ethernet dst address flow
Rich Laned0478ff2013-03-11 12:46:58 -0700105 pkt_matchdst = simple_eth_packet(eth_dst='00:01:01:01:01:01')
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400106 match = parse.packet_to_flow_match(pkt_matchdst)
107 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400108
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400109 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_DST
Shudong Zhou857fb602013-02-06 00:11:38 -0800110 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400111 return (pkt_matchdst,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400112
ShreyaPanditaed209962012-11-04 02:16:48 -0500113def wildcard_all(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400114# Generate a Wildcard_All Flow
115
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400116 #Create a simple tcp packet and generate wildcard all flow match from it.
117 pkt_wildcard = simple_tcp_packet()
118 match2 = parse.packet_to_flow_match(pkt_wildcard)
119 self.assertTrue(match2 is not None, "Could not generate flow match from pkt")
120 match2.wildcards=ofp.OFPFW_ALL
121 match2.in_port = of_ports[0]
Shudong Zhou857fb602013-02-06 00:11:38 -0800122 match_send_flowadd(self, match2, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400123 return (pkt_wildcard,match2)
ShreyaPandita60e45542012-09-27 15:11:16 -0400124
ShreyaPanditaed209962012-11-04 02:16:48 -0500125def wildcard_all_except_ingress(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400126# Generate Wildcard_All_Except_Ingress_port flow
ShreyaPandita60e45542012-09-27 15:11:16 -0400127
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400128 #Create a simple tcp packet and generate wildcard all except ingress_port flow.
129 pkt_matchingress = simple_tcp_packet()
130 match3 = parse.packet_to_flow_match(pkt_matchingress)
131 self.assertTrue(match3 is not None, "Could not generate flow match from pkt")
132 match3.wildcards = ofp.OFPFW_ALL-ofp.OFPFW_IN_PORT
133 match3.in_port = of_ports[0]
Shudong Zhou857fb602013-02-06 00:11:38 -0800134 match_send_flowadd(self, match3, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400135 return (pkt_matchingress,match3)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400136
ShreyaPanditaed209962012-11-04 02:16:48 -0500137def wildcard_all_except_ingress1(self,of_ports,priority=None):
ShreyaPanditada75f752012-10-26 16:26:35 -0400138# Generate Wildcard_All_Except_Ingress_port flow with action output to port egress_port 2
ShreyaPanditada75f752012-10-26 16:26:35 -0400139
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400140 #Create a simple tcp packet and generate wildcard all except ingress_port flow.
141 pkt_matchingress = simple_tcp_packet()
142 match3 = parse.packet_to_flow_match(pkt_matchingress)
143 self.assertTrue(match3 is not None, "Could not generate flow match from pkt")
144 match3.wildcards = ofp.OFPFW_ALL-ofp.OFPFW_IN_PORT
145 match3.in_port = of_ports[0]
Shudong Zhou857fb602013-02-06 00:11:38 -0800146 match_send_flowadd(self, match3, priority, of_ports[2])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400147 return (pkt_matchingress,match3)
ShreyaPanditada75f752012-10-26 16:26:35 -0400148
149
ShreyaPanditaed209962012-11-04 02:16:48 -0500150def match_vlan_id(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400151 #Generate Match_Vlan_Id
152
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400153 #Create a simple tcp packet and generate match on ethernet dst address flow
Rich Laned0478ff2013-03-11 12:46:58 -0700154 pkt_matchvlanid = simple_tcp_packet(dl_vlan_enable=True,vlan_vid=1)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400155 match = parse.packet_to_flow_match(pkt_matchvlanid)
156 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400157
ShreyaPandita6fbff252012-11-13 16:56:48 -0500158 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_DL_VLAN
Shudong Zhou857fb602013-02-06 00:11:38 -0800159 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400160 return (pkt_matchvlanid,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400161
ShreyaPanditaed209962012-11-04 02:16:48 -0500162def match_vlan_pcp(self,of_ports,priority=None):
ShreyaPandita055102a2012-11-28 11:43:45 -0500163 #Generate Match_Vlan_Priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400164
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400165 #Create a simple tcp packet and generate match on ethernet dst address flow
Rich Laned0478ff2013-03-11 12:46:58 -0700166 pkt_matchvlanpcp = simple_tcp_packet(dl_vlan_enable=True,vlan_vid=1,vlan_pcp=5)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400167 match = parse.packet_to_flow_match(pkt_matchvlanpcp)
168 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400169
ShreyaPandita6fbff252012-11-13 16:56:48 -0500170 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 -0800171 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400172 return (pkt_matchvlanpcp,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400173
174
ShreyaPanditaed209962012-11-04 02:16:48 -0500175def match_mul_l2(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400176 #Generate Match_Mul_L2 flow
177
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400178 #Create a simple eth packet and generate match on ethernet protocol flow
Rich Laned0478ff2013-03-11 12:46:58 -0700179 pkt_mulL2 = simple_eth_packet(eth_type=0x88cc,eth_src='00:01:01:01:01:01',eth_dst='00:01:01:01:01:02')
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400180 match = parse.packet_to_flow_match(pkt_mulL2)
181 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400182
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400183 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_DL_DST ^ofp.OFPFW_DL_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -0800184 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400185 return (pkt_mulL2,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400186
187
ShreyaPandita6fbff252012-11-13 16:56:48 -0500188def match_mul_l4(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400189 #Generate Match_Mul_L4 flow
190
191 #Create a simple tcp packet and generate match on tcp protocol flow
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400192 pkt_mulL4 = simple_tcp_packet(tcp_sport=111,tcp_dport=112)
193 match = parse.packet_to_flow_match(pkt_mulL4)
194 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita6fbff252012-11-13 16:56:48 -0500195 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 -0800196 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400197 return (pkt_mulL4,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400198
ShreyaPanditaed209962012-11-04 02:16:48 -0500199def match_ip_tos(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400200 #Generate a Match on IP Type of service flow
201
Shudong Zhou857fb602013-02-06 00:11:38 -0800202 #Create a simple tcp packet and generate match on Type of service
Rich Laneb5c73792012-12-03 17:12:32 -0800203 pkt_iptos = simple_tcp_packet(ip_tos=28)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400204 match = parse.packet_to_flow_match(pkt_iptos)
205 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400206
ShreyaPandita6fbff252012-11-13 16:56:48 -0500207 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_NW_TOS
Shudong Zhou857fb602013-02-06 00:11:38 -0800208 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400209 return (pkt_iptos,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400210
ShreyaPandita6fbff252012-11-13 16:56:48 -0500211def match_ip_protocol(self,of_ports,priority=None):
212 #Generate a Match on IP Protocol
213
214 #Create a simple tcp packet and generate match on Type of service
215 pkt_iptos = simple_tcp_packet()
216 match = parse.packet_to_flow_match(pkt_iptos)
217 self.assertTrue(match is not None, "Could not generate flow match from pkt")
218
219 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO
Shudong Zhou857fb602013-02-06 00:11:38 -0800220 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita6fbff252012-11-13 16:56:48 -0500221 return (pkt_iptos,match)
222
ShreyaPanditaed209962012-11-04 02:16:48 -0500223def match_tcp_src(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400224 #Generate Match_Tcp_Src
225
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400226 #Create a simple tcp packet and generate match on tcp source port flow
227 pkt_matchtSrc = simple_tcp_packet(tcp_sport=111)
228 match = parse.packet_to_flow_match(pkt_matchtSrc)
229 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400230
ShreyaPandita6fbff252012-11-13 16:56:48 -0500231 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -0800232 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400233 return (pkt_matchtSrc,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400234
ShreyaPanditaed209962012-11-04 02:16:48 -0500235def match_tcp_dst(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400236 #Generate Match_Tcp_Dst
237
Shudong Zhou857fb602013-02-06 00:11:38 -0800238 #Create a simple tcp packet and generate match on tcp destination port flow
ShreyaPandita6fbff252012-11-13 16:56:48 -0500239 pkt_matchdst = simple_tcp_packet(tcp_dport=112)
240 match = parse.packet_to_flow_match(pkt_matchdst)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400241 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400242
ShreyaPandita6fbff252012-11-13 16:56:48 -0500243 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO^ofp.OFPFW_TP_DST
Shudong Zhou857fb602013-02-06 00:11:38 -0800244 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita6fbff252012-11-13 16:56:48 -0500245 return (pkt_matchdst,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400246
ShreyaPandita055102a2012-11-28 11:43:45 -0500247
Shudong Zhouc2f18762013-01-11 00:12:44 -0800248def match_udp_src(self,of_ports,priority=None):
249 #Generate Match_Udp_Src
250
251 #Create a simple udp packet and generate match on udp source port flow
252 pkt_matchtSrc = simple_udp_packet(udp_sport=111)
253 match = parse.packet_to_flow_match(pkt_matchtSrc)
254 self.assertTrue(match is not None, "Could not generate flow match from pkt")
255
256 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -0800257 match_send_flowadd(self, match, priority, of_ports[1])
Shudong Zhouc2f18762013-01-11 00:12:44 -0800258 return (pkt_matchtSrc,match)
259
260def match_udp_dst(self,of_ports,priority=None):
261 #Generate Match_Udp_Dst
262
Shudong Zhou857fb602013-02-06 00:11:38 -0800263 #Create a simple udp packet and generate match on udp destination port flow
Shudong Zhouc2f18762013-01-11 00:12:44 -0800264 pkt_matchdst = simple_udp_packet(udp_dport=112)
265 match = parse.packet_to_flow_match(pkt_matchdst)
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_DST
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_matchdst,match)
271
272
273def match_icmp_type(self,of_ports,priority=None):
274 #Generate Match_Icmp_Type
275
276 #Create a simple icmp packet and generate match on icmp type flow
277 pkt_match = simple_icmp_packet(icmp_type=1)
278 match = parse.packet_to_flow_match(pkt_match)
279 self.assertTrue(match is not None, "Could not generate flow match from pkt")
280
281 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -0800282 match_send_flowadd(self, match, priority, of_ports[1])
Shudong Zhouc2f18762013-01-11 00:12:44 -0800283 return (pkt_match, match)
284
285def match_icmp_code(self,of_ports,priority=None):
286 #Generate Match_Icmp_Code
287
288 #Create a simple icmp packet and generate match on icmp code flow
289 pkt_match = simple_icmp_packet(icmp_code=3)
290 match = parse.packet_to_flow_match(pkt_match)
291 self.assertTrue(match is not None, "Could not generate flow match from pkt")
292
293 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_DST
Shudong Zhou857fb602013-02-06 00:11:38 -0800294 match_send_flowadd(self, match, priority, of_ports[1])
Shudong Zhouc2f18762013-01-11 00:12:44 -0800295 return (pkt_match, match)
296
Kiran Poolaff12e482013-07-02 14:19:52 -0700297def match_arp_opcode(self,of_ports,priority=None):
298 #Generate Match_Arp_Opcode
299
300 #Create a simple arp packet and generate match on arp opcode
301 pkt_match = simple_arp_packet(arp_op=1)
302 match = parse.packet_to_flow_match(pkt_match)
303 self.assertTrue(match is not None, "Could not generate flow match from pkt")
304
Kiran Poolaceee1c62013-07-02 14:57:42 -0700305 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO
Kiran Poolaff12e482013-07-02 14:19:52 -0700306 match_send_flowadd(self, match, priority, of_ports[1])
307 return (pkt_match, match)
308
Shudong Zhoudceec932013-02-06 01:12:54 -0800309def match_arp_sender(self,of_ports,priority=None):
310 #Generate Match_Arp_Sender
311
312 #Create a simple icmp packet and generate match on arp sender flow
313 pkt_match = simple_arp_packet()
314 match = parse.packet_to_flow_match(pkt_match)
315 self.assertTrue(match is not None, "Could not generate flow match from pkt")
316
317 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_NW_SRC_MASK
318 match_send_flowadd(self, match, priority, of_ports[1])
319 return (pkt_match, match)
320
321def match_arp_target(self,of_ports,priority=None):
322 #Generate Match_Arp_Target
323
324 #Create a simple icmp packet and generate match on arp target flow
325 pkt_match = simple_arp_packet()
326 match = parse.packet_to_flow_match(pkt_match)
327 self.assertTrue(match is not None, "Could not generate flow match from pkt")
328
329 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_NW_DST_MASK
330 match_send_flowadd(self, match, priority, of_ports[1])
331 return (pkt_match, match)
332
Shudong Zhouc2f18762013-01-11 00:12:44 -0800333
ShreyaPanditaed209962012-11-04 02:16:48 -0500334def match_ethernet_type(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400335 #Generate a Match_Ethernet_Type flow
336
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400337 #Create a simple tcp packet and generate match on ethernet type flow
Rich Laned0478ff2013-03-11 12:46:58 -0700338 pkt_matchtype = simple_eth_packet(eth_type=0x88cc)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400339 match = parse.packet_to_flow_match(pkt_matchtype)
340 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400341
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400342 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE
Shudong Zhou857fb602013-02-06 00:11:38 -0800343 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400344 return (pkt_matchtype,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400345
346
ShreyaPandita60e45542012-09-27 15:11:16 -0400347
ShreyaPandita6fbff252012-11-13 16:56:48 -0500348
ShreyaPanditaed209962012-11-04 02:16:48 -0500349def strict_modify_flow_action(self,egress_port,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400350# Strict Modify the flow Action
351
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400352 #Create a flow_mod message , command MODIFY_STRICT
Rich Laneba3f0e22013-03-11 16:43:57 -0700353 msg5 = ofp.message.flow_modify_strict()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400354 msg5.match = match
355 msg5.cookie = random.randint(0,9007199254740992)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400356 msg5.buffer_id = 0xffffffff
Rich Lane9d3cc6b2013-03-08 16:33:08 -0800357 act5 = ofp.action.output()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400358 act5.port = egress_port
Rich Lanec495d9e2013-03-08 17:43:36 -0800359 msg5.actions.append(act5)
ShreyaPandita60e45542012-09-27 15:11:16 -0400360
ShreyaPanditaed209962012-11-04 02:16:48 -0500361 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400362 msg5.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400363
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400364 # Send the flow with action A'
Rich Lane5c3151c2013-01-03 17:15:41 -0800365 self.controller.message_send (msg5)
Rich Lane3a261d52013-01-03 17:45:08 -0800366 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400367
ShreyaPanditaed209962012-11-04 02:16:48 -0500368def modify_flow_action(self,of_ports,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400369# Modify the flow action
370
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400371 #Create a flow_mod message , command MODIFY
Rich Laneba3f0e22013-03-11 16:43:57 -0700372 msg8 = ofp.message.flow_modify()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400373 msg8.match = match
374 msg8.cookie = random.randint(0,9007199254740992)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400375 #out_port will be ignored for flow adds and flow modify (here for test-case Add_Modify_With_Outport)
376 msg8.out_port = of_ports[3]
377 msg8.buffer_id = 0xffffffff
Rich Lane9d3cc6b2013-03-08 16:33:08 -0800378 act8 = ofp.action.output()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400379 act8.port = of_ports[2]
Rich Lanec495d9e2013-03-08 17:43:36 -0800380 msg8.actions.append(act8)
ShreyaPandita60e45542012-09-27 15:11:16 -0400381
ShreyaPanditaed209962012-11-04 02:16:48 -0500382 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400383 msg8.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400384
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400385 # Send the flow with action A'
Rich Lane5c3151c2013-01-03 17:15:41 -0800386 self.controller.message_send (msg8)
Rich Lane3a261d52013-01-03 17:45:08 -0800387 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400388
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400389def enqueue(self,ingress_port,egress_port,egress_queue_id):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400390#Generate a flow with enqueue action i.e output to a queue configured on a egress_port
391
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400392 pkt = simple_tcp_packet()
393 match = packet_to_flow_match(self, pkt)
394 match.wildcards &= ~ofp.OFPFW_IN_PORT
395 self.assertTrue(match is not None,
396 "Could not generate flow match from pkt")
397
398 match.in_port = ingress_port
Rich Laneba3f0e22013-03-11 16:43:57 -0700399 request = ofp.message.flow_add()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400400 request.match = match
401 request.buffer_id = 0xffffffff
Rich Lane9d3cc6b2013-03-08 16:33:08 -0800402 act = ofp.action.enqueue()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400403 act.port = egress_port
404 act.queue_id = egress_queue_id
Rich Lanec495d9e2013-03-08 17:43:36 -0800405 request.actions.append(act)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400406
407 logging.info("Inserting flow")
Rich Lane5c3151c2013-01-03 17:15:41 -0800408 self.controller.message_send(request)
Rich Lane3a261d52013-01-03 17:45:08 -0800409 do_barrier(self.controller)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400410 return (pkt,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400411
412
ShreyaPandita60e45542012-09-27 15:11:16 -0400413########################### Verify Stats Functions ###########################################################################################
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400414def get_flowstats(self,match):
415 # Generate flow_stats request
416
Rich Lane28fa9272013-03-08 16:00:25 -0800417 stat_req = ofp.message.flow_stats_request()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400418 stat_req.match = match
419 stat_req.table_id = 0xff
420 stat_req.out_port = ofp.OFPP_NONE
421
422 logging.info("Sending stats request")
423 response, pkt = self.controller.transact(stat_req,
424 timeout=5)
425 self.assertTrue(response is not None,"No response to stats request")
ShreyaPandita60e45542012-09-27 15:11:16 -0400426
ShreyaPandita66de26f2012-10-26 14:44:24 -0400427
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400428def get_portstats(self,port_num):
429
430# Return all the port counters in the form a tuple
Rich Lane0d4a5582015-04-10 12:25:39 -0700431 entries = get_port_stats(self, port_num)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400432 rx_pkts=0
433 tx_pkts=0
434 rx_byts=0
435 tx_byts=0
436 rx_drp =0
437 tx_drp = 0
438 rx_err=0
439 tx_err =0
440 rx_fr_err=0
441 rx_ovr_err=0
442 rx_crc_err=0
443 collisions = 0
444 tx_err=0
445
446
Rich Lane0d4a5582015-04-10 12:25:39 -0700447 for obj in entries:
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400448 rx_pkts += obj.rx_packets
449 tx_pkts += obj.tx_packets
450 rx_byts += obj.rx_bytes
451 tx_byts += obj.tx_bytes
452 rx_drp += obj.rx_dropped
453 tx_drp += obj.tx_dropped
454 rx_err += obj.rx_errors
455 rx_fr_err += obj.rx_frame_err
456 rx_ovr_err += obj.rx_over_err
457 rx_crc_err += obj.rx_crc_err
458 collisions+= obj.collisions
459 tx_err += obj.tx_errors
460
461 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)
462
463def get_queuestats(self,port_num,queue_id):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400464#Generate Queue Stats request
465
Rich Lane28fa9272013-03-08 16:00:25 -0800466 request = ofp.message.queue_stats_request()
ShreyaPandita66de26f2012-10-26 14:44:24 -0400467 request.port_no = port_num
468 request.queue_id = queue_id
469 (queue_stats, p) = self.controller.transact(request)
470 self.assertNotEqual(queue_stats, None, "Queue stats request failed")
471
472 return (queue_stats,p)
473
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400474def get_tablestats(self):
475# Send Table_Stats request (retrieve current table counters )
476
Rich Lane28fa9272013-03-08 16:00:25 -0800477 stat_req = ofp.message.table_stats_request()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400478 response, pkt = self.controller.transact(stat_req,
479 timeout=5)
480 self.assertTrue(response is not None,
481 "No response to stats request")
482 current_lookedup = 0
483 current_matched = 0
484 current_active = 0
485
Rich Lane5fd6faf2013-03-11 13:30:20 -0700486 for obj in response.entries:
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400487 current_lookedup += obj.lookup_count
488 current_matched += obj.matched_count
489 current_active += obj.active_count
490
491 return (current_lookedup,current_matched,current_active)
492
493
494
ShreyaPanditaed209962012-11-04 02:16:48 -0500495def verify_tablestats(self,expect_lookup=None,expect_match=None,expect_active=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400496
Rich Lane28fa9272013-03-08 16:00:25 -0800497 stat_req = ofp.message.table_stats_request()
ShreyaPanditaed209962012-11-04 02:16:48 -0500498
Rich Lane90b3d732012-12-31 10:03:50 -0800499 for i in range(0,100):
ShreyaPandita60e45542012-09-27 15:11:16 -0400500
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400501 logging.info("Sending stats request")
502 # TODO: move REPLY_MORE handling to controller.transact?
ShreyaPandita66de26f2012-10-26 14:44:24 -0400503 response, pkt = self.controller.transact(stat_req,
504 timeout=5)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400505 self.assertTrue(response is not None,"No response to stats request")
ShreyaPandita60e45542012-09-27 15:11:16 -0400506
ShreyaPanditaed209962012-11-04 02:16:48 -0500507 lookedup = 0
508 matched = 0
509 active = 0
510
Rich Lane5fd6faf2013-03-11 13:30:20 -0700511 for item in response.entries:
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400512 lookedup += item.lookup_count
513 matched += item.matched_count
514 active += item.active_count
515
Rich Laneafcd0dd2013-01-03 20:54:56 -0800516 logging.info("Packets Looked up: %d", lookedup)
517 logging.info("Packets matched: %d", matched)
518 logging.info("Active flow entries: %d", active)
ShreyaPanditaed209962012-11-04 02:16:48 -0500519
Rich Lane175f9562013-01-03 20:58:40 -0800520 if (expect_lookup == None or lookedup >= expect_lookup) and \
521 (expect_match == None or matched >= expect_match) and \
522 (expect_active == None or active >= expect_active):
Rich Lane90b3d732012-12-31 10:03:50 -0800523 break
524
525 sleep(0.1)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400526
ShreyaPanditaed209962012-11-04 02:16:48 -0500527 if expect_lookup != None :
Rich Laneecb5dfd2015-04-23 13:36:41 -0700528 self.assertLessEqual(expect_lookup, lookedup, "lookup counter is not incremented properly")
ShreyaPanditaed209962012-11-04 02:16:48 -0500529 if expect_match != None :
Rich Laneecb5dfd2015-04-23 13:36:41 -0700530 self.assertLessEqual(expect_match, matched, "matched counter is not incremented properly")
ShreyaPanditaed209962012-11-04 02:16:48 -0500531 if expect_active != None :
Rich Laneecb5dfd2015-04-23 13:36:41 -0700532 self.assertLessEqual(expect_active, active ,"active counter is not incremented properly")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400533
534
ShreyaPandita60e45542012-09-27 15:11:16 -0400535############################## Various delete commands #############################################################################################
536
ShreyaPanditaed209962012-11-04 02:16:48 -0500537def strict_delete(self,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400538# Issue Strict Delete
539
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400540 #Create flow_mod message, command DELETE_STRICT
Rich Laneba3f0e22013-03-11 16:43:57 -0700541 msg4 = ofp.message.flow_delete_strict()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400542 msg4.out_port = ofp.OFPP_NONE
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400543 msg4.buffer_id = 0xffffffff
544 msg4.match = match
ShreyaPandita60e45542012-09-27 15:11:16 -0400545
ShreyaPanditaed209962012-11-04 02:16:48 -0500546 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400547 msg4.priority = priority
Rich Lane5c3151c2013-01-03 17:15:41 -0800548 self.controller.message_send(msg4)
Rich Lane3a261d52013-01-03 17:45:08 -0800549 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400550
551
552
ShreyaPanditaed209962012-11-04 02:16:48 -0500553def nonstrict_delete(self,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400554# Issue Non_Strict Delete
555
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400556 #Create flow_mod message, command DELETE
Rich Laneba3f0e22013-03-11 16:43:57 -0700557 msg6 = ofp.message.flow_delete()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400558 msg6.out_port = ofp.OFPP_NONE
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400559 msg6.buffer_id = 0xffffffff
560 msg6.match = match
ShreyaPandita60e45542012-09-27 15:11:16 -0400561
ShreyaPanditaed209962012-11-04 02:16:48 -0500562 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400563 msg6.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400564
Rich Lane5c3151c2013-01-03 17:15:41 -0800565 self.controller.message_send(msg6)
Rich Lane3a261d52013-01-03 17:45:08 -0800566 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400567
568
569###########################################################################################################################################################
570
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400571def send_packet(obj, pkt, ingress_port, egress_port):
ShreyaPandita60e45542012-09-27 15:11:16 -0400572#Send Packets on a specified ingress_port and verify if its recieved on correct egress_port.
573
574 obj.dataplane.send(ingress_port, str(pkt))
575 exp_pkt_arg = pkt
576 exp_port = egress_port
577
578 (rcv_port, rcv_pkt, pkt_time) = obj.dataplane.poll(timeout=2,
579 port_number=exp_port,
580 exp_pkt=exp_pkt_arg)
581 obj.assertTrue(rcv_pkt is not None,
582 "Packet not received on port " + str(egress_port))
583 obj.assertEqual(rcv_port, egress_port,
584 "Packet received on port " + str(rcv_port) +
585 ", expected port " + str(egress_port))
586 obj.assertEqual(str(pkt), str(rcv_pkt),
587 'Response packet does not match send packet')
588
589
ShreyaPandita572e64b2012-09-28 14:41:06 -0400590def sw_supported_actions(parent,use_cache=False):
ShreyaPandita60e45542012-09-27 15:11:16 -0400591#Returns the switch's supported actions
592
593 cache_supported_actions = None
594 if cache_supported_actions is None or not use_cache:
Rich Lane28fa9272013-03-08 16:00:25 -0800595 request = ofp.message.features_request()
ShreyaPandita60e45542012-09-27 15:11:16 -0400596 (reply, pkt) = parent.controller.transact(request)
597 parent.assertTrue(reply is not None, "Did not get response to ftr req")
598 cache_supported_actions = reply.actions
599 return cache_supported_actions
600
ShreyaPandita66de26f2012-10-26 14:44:24 -0400601##############################################################################################################################################################
602