blob: abf40f54626660fa8150c0cd3733bf8a85a80881 [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
Rich Laned7b0ffa2013-03-08 15:53:42 -08008import ofp
ShreyaPandita60e45542012-09-27 15:11:16 -04009import oftest.dataplane as dataplane
ShreyaPandita60e45542012-09-27 15:11:16 -040010import oftest.parse as parse
11import logging
12import types
ShreyaPandita66de26f2012-10-26 14:44:24 -040013
Rich Laneb90a1c42012-10-05 09:16:05 -070014import oftest.base_tests as base_tests
Rich Laneda3b5ad2012-10-03 09:05:32 -070015from oftest.testutils import *
ShreyaPandita60e45542012-09-27 15:11:16 -040016from time import sleep
17
18#################### Functions for various types of flow_mod ##########################################################################################
19
Shudong Zhou857fb602013-02-06 00:11:38 -080020def match_send_flowadd(self, match, priority, port):
Rich Laneba3f0e22013-03-11 16:43:57 -070021 msg = ofp.message.flow_add()
Shudong Zhou857fb602013-02-06 00:11:38 -080022 msg.out_port = ofp.OFPP_NONE
Shudong Zhou857fb602013-02-06 00:11:38 -080023 # msg.cookie = random.randint(0,9007199254740992)
24 msg.buffer_id = 0xffffffff
25 msg.match = match
26 if priority != None :
27 msg.priority = priority
Rich Lane9d3cc6b2013-03-08 16:33:08 -080028 act = ofp.action.output()
Shudong Zhou857fb602013-02-06 00:11:38 -080029 act.port = port
Rich Lanec495d9e2013-03-08 17:43:36 -080030 msg.actions.append(act)
Shudong Zhou857fb602013-02-06 00:11:38 -080031 self.controller.message_send(msg)
32 do_barrier(self.controller)
33
ShreyaPanditaed209962012-11-04 02:16:48 -050034def exact_match(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -040035# Generate ExactMatch flow .
36
ShreyaPandita4ebbac32012-11-02 13:40:44 -040037 #Create a simple tcp packet and generate exact flow match from it.
38 pkt_exactflow = simple_tcp_packet()
39 match = parse.packet_to_flow_match(pkt_exactflow)
40 self.assertTrue(match is not None, "Could not generate flow match from pkt")
41 match.in_port = of_ports[0]
Rich Laned0478ff2013-03-11 12:46:58 -070042 #match.ipv4_src = 1
ShreyaPandita4ebbac32012-11-02 13:40:44 -040043 match.wildcards=0
Shudong Zhou857fb602013-02-06 00:11:38 -080044 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -040045 return (pkt_exactflow,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -040046
ShreyaPanditaed209962012-11-04 02:16:48 -050047def exact_match_with_prio(self,of_ports,priority=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -040048 # Generate ExactMatch with action output to port 2
ShreyaPandita66de26f2012-10-26 14:44:24 -040049
ShreyaPandita4ebbac32012-11-02 13:40:44 -040050 #Create a simple tcp packet and generate exact flow match from it.
51 pkt_exactflow = simple_tcp_packet()
52 match = parse.packet_to_flow_match(pkt_exactflow)
53 self.assertTrue(match is not None, "Could not generate flow match from pkt")
54 match.in_port = of_ports[0]
Rich Laned0478ff2013-03-11 12:46:58 -070055 #match.ipv4_src = 1
ShreyaPandita4ebbac32012-11-02 13:40:44 -040056 match.wildcards=0
Shudong Zhou857fb602013-02-06 00:11:38 -080057 match_send_flowadd(self, match, priority, of_ports[2])
ShreyaPandita4ebbac32012-11-02 13:40:44 -040058 return (pkt_exactflow,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -040059
ShreyaPandita60e45542012-09-27 15:11:16 -040060
ShreyaPanditaed209962012-11-04 02:16:48 -050061def match_all_except_source_address(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -040062# Generate Match_All_Except_Source_Address flow
63
ShreyaPandita4ebbac32012-11-02 13:40:44 -040064 #Create a simple tcp packet and generate match all except src address flow.
65 pkt_wildcardsrc= simple_tcp_packet()
66 match1 = parse.packet_to_flow_match(pkt_wildcardsrc)
67 self.assertTrue(match1 is not None, "Could not generate flow match from pkt")
68 match1.in_port = of_ports[0]
Rich Laned0478ff2013-03-11 12:46:58 -070069 #match1.ipv4_src = 1
ShreyaPandita4ebbac32012-11-02 13:40:44 -040070 match1.wildcards = ofp.OFPFW_DL_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -080071 match_send_flowadd(self, match1, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -040072 return (pkt_wildcardsrc,match1)
ShreyaPandita66de26f2012-10-26 14:44:24 -040073
ShreyaPandita6fbff252012-11-13 16:56:48 -050074def match_ethernet_src_address(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -040075 #Generate Match_Ethernet_SrC_Address flow
76
ShreyaPandita4ebbac32012-11-02 13:40:44 -040077 #Create a simple tcp packet and generate match on ethernet src address flow
Rich Laned0478ff2013-03-11 12:46:58 -070078 pkt_MatchSrc = simple_eth_packet(eth_src='00:01:01:01:01:01')
ShreyaPandita4ebbac32012-11-02 13:40:44 -040079 match = parse.packet_to_flow_match(pkt_MatchSrc)
80 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita4ebbac32012-11-02 13:40:44 -040081 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -080082 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -040083 return (pkt_MatchSrc,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -040084
ShreyaPanditaed209962012-11-04 02:16:48 -050085def match_ethernet_dst_address(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -040086 #Generate Match_Ethernet_Dst_Address flow
87
ShreyaPandita4ebbac32012-11-02 13:40:44 -040088 #Create a simple tcp packet and generate match on ethernet dst address flow
Rich Laned0478ff2013-03-11 12:46:58 -070089 pkt_matchdst = simple_eth_packet(eth_dst='00:01:01:01:01:01')
ShreyaPandita4ebbac32012-11-02 13:40:44 -040090 match = parse.packet_to_flow_match(pkt_matchdst)
91 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -040092
ShreyaPandita4ebbac32012-11-02 13:40:44 -040093 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_DST
Shudong Zhou857fb602013-02-06 00:11:38 -080094 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -040095 return (pkt_matchdst,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -040096
ShreyaPanditaed209962012-11-04 02:16:48 -050097def wildcard_all(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -040098# Generate a Wildcard_All Flow
99
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400100 #Create a simple tcp packet and generate wildcard all flow match from it.
101 pkt_wildcard = simple_tcp_packet()
102 match2 = parse.packet_to_flow_match(pkt_wildcard)
103 self.assertTrue(match2 is not None, "Could not generate flow match from pkt")
104 match2.wildcards=ofp.OFPFW_ALL
105 match2.in_port = of_ports[0]
Shudong Zhou857fb602013-02-06 00:11:38 -0800106 match_send_flowadd(self, match2, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400107 return (pkt_wildcard,match2)
ShreyaPandita60e45542012-09-27 15:11:16 -0400108
ShreyaPanditaed209962012-11-04 02:16:48 -0500109def wildcard_all_except_ingress(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400110# Generate Wildcard_All_Except_Ingress_port flow
ShreyaPandita60e45542012-09-27 15:11:16 -0400111
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400112 #Create a simple tcp packet and generate wildcard all except ingress_port flow.
113 pkt_matchingress = simple_tcp_packet()
114 match3 = parse.packet_to_flow_match(pkt_matchingress)
115 self.assertTrue(match3 is not None, "Could not generate flow match from pkt")
116 match3.wildcards = ofp.OFPFW_ALL-ofp.OFPFW_IN_PORT
117 match3.in_port = of_ports[0]
Shudong Zhou857fb602013-02-06 00:11:38 -0800118 match_send_flowadd(self, match3, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400119 return (pkt_matchingress,match3)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400120
ShreyaPanditaed209962012-11-04 02:16:48 -0500121def wildcard_all_except_ingress1(self,of_ports,priority=None):
ShreyaPanditada75f752012-10-26 16:26:35 -0400122# Generate Wildcard_All_Except_Ingress_port flow with action output to port egress_port 2
ShreyaPanditada75f752012-10-26 16:26:35 -0400123
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400124 #Create a simple tcp packet and generate wildcard all except ingress_port flow.
125 pkt_matchingress = simple_tcp_packet()
126 match3 = parse.packet_to_flow_match(pkt_matchingress)
127 self.assertTrue(match3 is not None, "Could not generate flow match from pkt")
128 match3.wildcards = ofp.OFPFW_ALL-ofp.OFPFW_IN_PORT
129 match3.in_port = of_ports[0]
Shudong Zhou857fb602013-02-06 00:11:38 -0800130 match_send_flowadd(self, match3, priority, of_ports[2])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400131 return (pkt_matchingress,match3)
ShreyaPanditada75f752012-10-26 16:26:35 -0400132
133
ShreyaPanditaed209962012-11-04 02:16:48 -0500134def match_vlan_id(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400135 #Generate Match_Vlan_Id
136
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400137 #Create a simple tcp packet and generate match on ethernet dst address flow
Rich Laned0478ff2013-03-11 12:46:58 -0700138 pkt_matchvlanid = simple_tcp_packet(dl_vlan_enable=True,vlan_vid=1)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400139 match = parse.packet_to_flow_match(pkt_matchvlanid)
140 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400141
ShreyaPandita6fbff252012-11-13 16:56:48 -0500142 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_DL_VLAN
Shudong Zhou857fb602013-02-06 00:11:38 -0800143 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400144 return (pkt_matchvlanid,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400145
ShreyaPanditaed209962012-11-04 02:16:48 -0500146def match_vlan_pcp(self,of_ports,priority=None):
ShreyaPandita055102a2012-11-28 11:43:45 -0500147 #Generate Match_Vlan_Priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400148
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400149 #Create a simple tcp packet and generate match on ethernet dst address flow
Rich Laned0478ff2013-03-11 12:46:58 -0700150 pkt_matchvlanpcp = simple_tcp_packet(dl_vlan_enable=True,vlan_vid=1,vlan_pcp=5)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400151 match = parse.packet_to_flow_match(pkt_matchvlanpcp)
152 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400153
ShreyaPandita6fbff252012-11-13 16:56:48 -0500154 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 -0800155 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400156 return (pkt_matchvlanpcp,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400157
158
ShreyaPanditaed209962012-11-04 02:16:48 -0500159def match_mul_l2(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400160 #Generate Match_Mul_L2 flow
161
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400162 #Create a simple eth packet and generate match on ethernet protocol flow
Rich Laned0478ff2013-03-11 12:46:58 -0700163 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 -0400164 match = parse.packet_to_flow_match(pkt_mulL2)
165 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400166
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400167 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_DL_DST ^ofp.OFPFW_DL_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -0800168 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400169 return (pkt_mulL2,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400170
171
ShreyaPandita6fbff252012-11-13 16:56:48 -0500172def match_mul_l4(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400173 #Generate Match_Mul_L4 flow
174
175 #Create a simple tcp packet and generate match on tcp protocol flow
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400176 pkt_mulL4 = simple_tcp_packet(tcp_sport=111,tcp_dport=112)
177 match = parse.packet_to_flow_match(pkt_mulL4)
178 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita6fbff252012-11-13 16:56:48 -0500179 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 -0800180 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400181 return (pkt_mulL4,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400182
ShreyaPanditaed209962012-11-04 02:16:48 -0500183def match_ip_tos(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400184 #Generate a Match on IP Type of service flow
185
Shudong Zhou857fb602013-02-06 00:11:38 -0800186 #Create a simple tcp packet and generate match on Type of service
Rich Laneb5c73792012-12-03 17:12:32 -0800187 pkt_iptos = simple_tcp_packet(ip_tos=28)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400188 match = parse.packet_to_flow_match(pkt_iptos)
189 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400190
ShreyaPandita6fbff252012-11-13 16:56:48 -0500191 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_NW_TOS
Shudong Zhou857fb602013-02-06 00:11:38 -0800192 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400193 return (pkt_iptos,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400194
ShreyaPandita6fbff252012-11-13 16:56:48 -0500195def match_ip_protocol(self,of_ports,priority=None):
196 #Generate a Match on IP Protocol
197
198 #Create a simple tcp packet and generate match on Type of service
199 pkt_iptos = simple_tcp_packet()
200 match = parse.packet_to_flow_match(pkt_iptos)
201 self.assertTrue(match is not None, "Could not generate flow match from pkt")
202
203 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO
Shudong Zhou857fb602013-02-06 00:11:38 -0800204 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita6fbff252012-11-13 16:56:48 -0500205 return (pkt_iptos,match)
206
ShreyaPanditaed209962012-11-04 02:16:48 -0500207def match_tcp_src(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400208 #Generate Match_Tcp_Src
209
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400210 #Create a simple tcp packet and generate match on tcp source port flow
211 pkt_matchtSrc = simple_tcp_packet(tcp_sport=111)
212 match = parse.packet_to_flow_match(pkt_matchtSrc)
213 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400214
ShreyaPandita6fbff252012-11-13 16:56:48 -0500215 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -0800216 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400217 return (pkt_matchtSrc,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400218
ShreyaPanditaed209962012-11-04 02:16:48 -0500219def match_tcp_dst(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400220 #Generate Match_Tcp_Dst
221
Shudong Zhou857fb602013-02-06 00:11:38 -0800222 #Create a simple tcp packet and generate match on tcp destination port flow
ShreyaPandita6fbff252012-11-13 16:56:48 -0500223 pkt_matchdst = simple_tcp_packet(tcp_dport=112)
224 match = parse.packet_to_flow_match(pkt_matchdst)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400225 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400226
ShreyaPandita6fbff252012-11-13 16:56:48 -0500227 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO^ofp.OFPFW_TP_DST
Shudong Zhou857fb602013-02-06 00:11:38 -0800228 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita6fbff252012-11-13 16:56:48 -0500229 return (pkt_matchdst,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400230
ShreyaPandita055102a2012-11-28 11:43:45 -0500231
Shudong Zhouc2f18762013-01-11 00:12:44 -0800232def match_udp_src(self,of_ports,priority=None):
233 #Generate Match_Udp_Src
234
235 #Create a simple udp packet and generate match on udp source port flow
236 pkt_matchtSrc = simple_udp_packet(udp_sport=111)
237 match = parse.packet_to_flow_match(pkt_matchtSrc)
238 self.assertTrue(match is not None, "Could not generate flow match from pkt")
239
240 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -0800241 match_send_flowadd(self, match, priority, of_ports[1])
Shudong Zhouc2f18762013-01-11 00:12:44 -0800242 return (pkt_matchtSrc,match)
243
244def match_udp_dst(self,of_ports,priority=None):
245 #Generate Match_Udp_Dst
246
Shudong Zhou857fb602013-02-06 00:11:38 -0800247 #Create a simple udp packet and generate match on udp destination port flow
Shudong Zhouc2f18762013-01-11 00:12:44 -0800248 pkt_matchdst = simple_udp_packet(udp_dport=112)
249 match = parse.packet_to_flow_match(pkt_matchdst)
250 self.assertTrue(match is not None, "Could not generate flow match from pkt")
251
252 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO^ofp.OFPFW_TP_DST
Shudong Zhou857fb602013-02-06 00:11:38 -0800253 match_send_flowadd(self, match, priority, of_ports[1])
Shudong Zhouc2f18762013-01-11 00:12:44 -0800254 return (pkt_matchdst,match)
255
256
257def match_icmp_type(self,of_ports,priority=None):
258 #Generate Match_Icmp_Type
259
260 #Create a simple icmp packet and generate match on icmp type flow
261 pkt_match = simple_icmp_packet(icmp_type=1)
262 match = parse.packet_to_flow_match(pkt_match)
263 self.assertTrue(match is not None, "Could not generate flow match from pkt")
264
265 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -0800266 match_send_flowadd(self, match, priority, of_ports[1])
Shudong Zhouc2f18762013-01-11 00:12:44 -0800267 return (pkt_match, match)
268
269def match_icmp_code(self,of_ports,priority=None):
270 #Generate Match_Icmp_Code
271
272 #Create a simple icmp packet and generate match on icmp code flow
273 pkt_match = simple_icmp_packet(icmp_code=3)
274 match = parse.packet_to_flow_match(pkt_match)
275 self.assertTrue(match is not None, "Could not generate flow match from pkt")
276
277 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_DST
Shudong Zhou857fb602013-02-06 00:11:38 -0800278 match_send_flowadd(self, match, priority, of_ports[1])
Shudong Zhouc2f18762013-01-11 00:12:44 -0800279 return (pkt_match, match)
280
Kiran Poolaff12e482013-07-02 14:19:52 -0700281def match_arp_opcode(self,of_ports,priority=None):
282 #Generate Match_Arp_Opcode
283
284 #Create a simple arp packet and generate match on arp opcode
285 pkt_match = simple_arp_packet(arp_op=1)
286 match = parse.packet_to_flow_match(pkt_match)
287 self.assertTrue(match is not None, "Could not generate flow match from pkt")
288
Kiran Poolaceee1c62013-07-02 14:57:42 -0700289 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO
Kiran Poolaff12e482013-07-02 14:19:52 -0700290 match_send_flowadd(self, match, priority, of_ports[1])
291 return (pkt_match, match)
292
Shudong Zhoudceec932013-02-06 01:12:54 -0800293def match_arp_sender(self,of_ports,priority=None):
294 #Generate Match_Arp_Sender
295
296 #Create a simple icmp packet and generate match on arp sender flow
297 pkt_match = simple_arp_packet()
298 match = parse.packet_to_flow_match(pkt_match)
299 self.assertTrue(match is not None, "Could not generate flow match from pkt")
300
301 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_NW_SRC_MASK
302 match_send_flowadd(self, match, priority, of_ports[1])
303 return (pkt_match, match)
304
305def match_arp_target(self,of_ports,priority=None):
306 #Generate Match_Arp_Target
307
308 #Create a simple icmp packet and generate match on arp target flow
309 pkt_match = simple_arp_packet()
310 match = parse.packet_to_flow_match(pkt_match)
311 self.assertTrue(match is not None, "Could not generate flow match from pkt")
312
313 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_NW_DST_MASK
314 match_send_flowadd(self, match, priority, of_ports[1])
315 return (pkt_match, match)
316
Shudong Zhouc2f18762013-01-11 00:12:44 -0800317
ShreyaPanditaed209962012-11-04 02:16:48 -0500318def match_ethernet_type(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400319 #Generate a Match_Ethernet_Type flow
320
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400321 #Create a simple tcp packet and generate match on ethernet type flow
Rich Laned0478ff2013-03-11 12:46:58 -0700322 pkt_matchtype = simple_eth_packet(eth_type=0x88cc)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400323 match = parse.packet_to_flow_match(pkt_matchtype)
324 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400325
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400326 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE
Shudong Zhou857fb602013-02-06 00:11:38 -0800327 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400328 return (pkt_matchtype,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400329
330
ShreyaPandita60e45542012-09-27 15:11:16 -0400331
ShreyaPandita6fbff252012-11-13 16:56:48 -0500332
ShreyaPanditaed209962012-11-04 02:16:48 -0500333def strict_modify_flow_action(self,egress_port,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400334# Strict Modify the flow Action
335
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400336 #Create a flow_mod message , command MODIFY_STRICT
Rich Laneba3f0e22013-03-11 16:43:57 -0700337 msg5 = ofp.message.flow_modify_strict()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400338 msg5.match = match
339 msg5.cookie = random.randint(0,9007199254740992)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400340 msg5.buffer_id = 0xffffffff
Rich Lane9d3cc6b2013-03-08 16:33:08 -0800341 act5 = ofp.action.output()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400342 act5.port = egress_port
Rich Lanec495d9e2013-03-08 17:43:36 -0800343 msg5.actions.append(act5)
ShreyaPandita60e45542012-09-27 15:11:16 -0400344
ShreyaPanditaed209962012-11-04 02:16:48 -0500345 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400346 msg5.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400347
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400348 # Send the flow with action A'
Rich Lane5c3151c2013-01-03 17:15:41 -0800349 self.controller.message_send (msg5)
Rich Lane3a261d52013-01-03 17:45:08 -0800350 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400351
ShreyaPanditaed209962012-11-04 02:16:48 -0500352def modify_flow_action(self,of_ports,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400353# Modify the flow action
354
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400355 #Create a flow_mod message , command MODIFY
Rich Laneba3f0e22013-03-11 16:43:57 -0700356 msg8 = ofp.message.flow_modify()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400357 msg8.match = match
358 msg8.cookie = random.randint(0,9007199254740992)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400359 #out_port will be ignored for flow adds and flow modify (here for test-case Add_Modify_With_Outport)
360 msg8.out_port = of_ports[3]
361 msg8.buffer_id = 0xffffffff
Rich Lane9d3cc6b2013-03-08 16:33:08 -0800362 act8 = ofp.action.output()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400363 act8.port = of_ports[2]
Rich Lanec495d9e2013-03-08 17:43:36 -0800364 msg8.actions.append(act8)
ShreyaPandita60e45542012-09-27 15:11:16 -0400365
ShreyaPanditaed209962012-11-04 02:16:48 -0500366 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400367 msg8.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400368
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400369 # Send the flow with action A'
Rich Lane5c3151c2013-01-03 17:15:41 -0800370 self.controller.message_send (msg8)
Rich Lane3a261d52013-01-03 17:45:08 -0800371 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400372
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400373def enqueue(self,ingress_port,egress_port,egress_queue_id):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400374#Generate a flow with enqueue action i.e output to a queue configured on a egress_port
375
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400376 pkt = simple_tcp_packet()
377 match = packet_to_flow_match(self, pkt)
378 match.wildcards &= ~ofp.OFPFW_IN_PORT
379 self.assertTrue(match is not None,
380 "Could not generate flow match from pkt")
381
382 match.in_port = ingress_port
Rich Laneba3f0e22013-03-11 16:43:57 -0700383 request = ofp.message.flow_add()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400384 request.match = match
385 request.buffer_id = 0xffffffff
Rich Lane9d3cc6b2013-03-08 16:33:08 -0800386 act = ofp.action.enqueue()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400387 act.port = egress_port
388 act.queue_id = egress_queue_id
Rich Lanec495d9e2013-03-08 17:43:36 -0800389 request.actions.append(act)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400390
391 logging.info("Inserting flow")
Rich Lane5c3151c2013-01-03 17:15:41 -0800392 self.controller.message_send(request)
Rich Lane3a261d52013-01-03 17:45:08 -0800393 do_barrier(self.controller)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400394 return (pkt,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400395
396
ShreyaPandita60e45542012-09-27 15:11:16 -0400397########################### Verify Stats Functions ###########################################################################################
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400398def get_flowstats(self,match):
399 # Generate flow_stats request
400
Rich Lane28fa9272013-03-08 16:00:25 -0800401 stat_req = ofp.message.flow_stats_request()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400402 stat_req.match = match
403 stat_req.table_id = 0xff
404 stat_req.out_port = ofp.OFPP_NONE
405
406 logging.info("Sending stats request")
407 response, pkt = self.controller.transact(stat_req,
408 timeout=5)
409 self.assertTrue(response is not None,"No response to stats request")
ShreyaPandita60e45542012-09-27 15:11:16 -0400410
ShreyaPandita66de26f2012-10-26 14:44:24 -0400411
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400412def get_portstats(self,port_num):
413
414# Return all the port counters in the form a tuple
Rich Lane28fa9272013-03-08 16:00:25 -0800415 port_stats_req = ofp.message.port_stats_request()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400416 port_stats_req.port_no = port_num
417 response,pkt = self.controller.transact(port_stats_req)
418 self.assertTrue(response is not None,"No response received for port stats request")
419 rx_pkts=0
420 tx_pkts=0
421 rx_byts=0
422 tx_byts=0
423 rx_drp =0
424 tx_drp = 0
425 rx_err=0
426 tx_err =0
427 rx_fr_err=0
428 rx_ovr_err=0
429 rx_crc_err=0
430 collisions = 0
431 tx_err=0
432
433
Rich Lane5fd6faf2013-03-11 13:30:20 -0700434 for obj in response.entries:
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400435 rx_pkts += obj.rx_packets
436 tx_pkts += obj.tx_packets
437 rx_byts += obj.rx_bytes
438 tx_byts += obj.tx_bytes
439 rx_drp += obj.rx_dropped
440 tx_drp += obj.tx_dropped
441 rx_err += obj.rx_errors
442 rx_fr_err += obj.rx_frame_err
443 rx_ovr_err += obj.rx_over_err
444 rx_crc_err += obj.rx_crc_err
445 collisions+= obj.collisions
446 tx_err += obj.tx_errors
447
448 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)
449
450def get_queuestats(self,port_num,queue_id):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400451#Generate Queue Stats request
452
Rich Lane28fa9272013-03-08 16:00:25 -0800453 request = ofp.message.queue_stats_request()
ShreyaPandita66de26f2012-10-26 14:44:24 -0400454 request.port_no = port_num
455 request.queue_id = queue_id
456 (queue_stats, p) = self.controller.transact(request)
457 self.assertNotEqual(queue_stats, None, "Queue stats request failed")
458
459 return (queue_stats,p)
460
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400461def get_tablestats(self):
462# Send Table_Stats request (retrieve current table counters )
463
Rich Lane28fa9272013-03-08 16:00:25 -0800464 stat_req = ofp.message.table_stats_request()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400465 response, pkt = self.controller.transact(stat_req,
466 timeout=5)
467 self.assertTrue(response is not None,
468 "No response to stats request")
469 current_lookedup = 0
470 current_matched = 0
471 current_active = 0
472
Rich Lane5fd6faf2013-03-11 13:30:20 -0700473 for obj in response.entries:
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400474 current_lookedup += obj.lookup_count
475 current_matched += obj.matched_count
476 current_active += obj.active_count
477
478 return (current_lookedup,current_matched,current_active)
479
480
481
ShreyaPanditaed209962012-11-04 02:16:48 -0500482def verify_tablestats(self,expect_lookup=None,expect_match=None,expect_active=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400483
Rich Lane28fa9272013-03-08 16:00:25 -0800484 stat_req = ofp.message.table_stats_request()
ShreyaPanditaed209962012-11-04 02:16:48 -0500485
Rich Lane90b3d732012-12-31 10:03:50 -0800486 for i in range(0,100):
ShreyaPandita60e45542012-09-27 15:11:16 -0400487
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400488 logging.info("Sending stats request")
489 # TODO: move REPLY_MORE handling to controller.transact?
ShreyaPandita66de26f2012-10-26 14:44:24 -0400490 response, pkt = self.controller.transact(stat_req,
491 timeout=5)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400492 self.assertTrue(response is not None,"No response to stats request")
ShreyaPandita60e45542012-09-27 15:11:16 -0400493
ShreyaPanditaed209962012-11-04 02:16:48 -0500494 lookedup = 0
495 matched = 0
496 active = 0
497
Rich Lane5fd6faf2013-03-11 13:30:20 -0700498 for item in response.entries:
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400499 lookedup += item.lookup_count
500 matched += item.matched_count
501 active += item.active_count
502
Rich Laneafcd0dd2013-01-03 20:54:56 -0800503 logging.info("Packets Looked up: %d", lookedup)
504 logging.info("Packets matched: %d", matched)
505 logging.info("Active flow entries: %d", active)
ShreyaPanditaed209962012-11-04 02:16:48 -0500506
Rich Lane175f9562013-01-03 20:58:40 -0800507 if (expect_lookup == None or lookedup >= expect_lookup) and \
508 (expect_match == None or matched >= expect_match) and \
509 (expect_active == None or active >= expect_active):
Rich Lane90b3d732012-12-31 10:03:50 -0800510 break
511
512 sleep(0.1)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400513
ShreyaPanditaed209962012-11-04 02:16:48 -0500514 if expect_lookup != None :
Rich Lane3e777792013-01-03 21:30:30 -0800515 self.assertEqual(expect_lookup, lookedup, "lookup counter is not incremented properly")
ShreyaPanditaed209962012-11-04 02:16:48 -0500516 if expect_match != None :
Rich Lane3e777792013-01-03 21:30:30 -0800517 self.assertEqual(expect_match, matched, "matched counter is not incremented properly")
ShreyaPanditaed209962012-11-04 02:16:48 -0500518 if expect_active != None :
Rich Lane3e777792013-01-03 21:30:30 -0800519 self.assertEqual(expect_active, active ,"active counter is not incremented properly")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400520
521
ShreyaPandita60e45542012-09-27 15:11:16 -0400522############################## Various delete commands #############################################################################################
523
ShreyaPanditaed209962012-11-04 02:16:48 -0500524def strict_delete(self,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400525# Issue Strict Delete
526
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400527 #Create flow_mod message, command DELETE_STRICT
Rich Laneba3f0e22013-03-11 16:43:57 -0700528 msg4 = ofp.message.flow_delete_strict()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400529 msg4.out_port = ofp.OFPP_NONE
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400530 msg4.buffer_id = 0xffffffff
531 msg4.match = match
ShreyaPandita60e45542012-09-27 15:11:16 -0400532
ShreyaPanditaed209962012-11-04 02:16:48 -0500533 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400534 msg4.priority = priority
Rich Lane5c3151c2013-01-03 17:15:41 -0800535 self.controller.message_send(msg4)
Rich Lane3a261d52013-01-03 17:45:08 -0800536 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400537
538
539
ShreyaPanditaed209962012-11-04 02:16:48 -0500540def nonstrict_delete(self,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400541# Issue Non_Strict Delete
542
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400543 #Create flow_mod message, command DELETE
Rich Laneba3f0e22013-03-11 16:43:57 -0700544 msg6 = ofp.message.flow_delete()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400545 msg6.out_port = ofp.OFPP_NONE
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400546 msg6.buffer_id = 0xffffffff
547 msg6.match = match
ShreyaPandita60e45542012-09-27 15:11:16 -0400548
ShreyaPanditaed209962012-11-04 02:16:48 -0500549 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400550 msg6.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400551
Rich Lane5c3151c2013-01-03 17:15:41 -0800552 self.controller.message_send(msg6)
Rich Lane3a261d52013-01-03 17:45:08 -0800553 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400554
555
556###########################################################################################################################################################
557
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400558def send_packet(obj, pkt, ingress_port, egress_port):
ShreyaPandita60e45542012-09-27 15:11:16 -0400559#Send Packets on a specified ingress_port and verify if its recieved on correct egress_port.
560
561 obj.dataplane.send(ingress_port, str(pkt))
562 exp_pkt_arg = pkt
563 exp_port = egress_port
564
565 (rcv_port, rcv_pkt, pkt_time) = obj.dataplane.poll(timeout=2,
566 port_number=exp_port,
567 exp_pkt=exp_pkt_arg)
568 obj.assertTrue(rcv_pkt is not None,
569 "Packet not received on port " + str(egress_port))
570 obj.assertEqual(rcv_port, egress_port,
571 "Packet received on port " + str(rcv_port) +
572 ", expected port " + str(egress_port))
573 obj.assertEqual(str(pkt), str(rcv_pkt),
574 'Response packet does not match send packet')
575
576
ShreyaPandita572e64b2012-09-28 14:41:06 -0400577def sw_supported_actions(parent,use_cache=False):
ShreyaPandita60e45542012-09-27 15:11:16 -0400578#Returns the switch's supported actions
579
580 cache_supported_actions = None
581 if cache_supported_actions is None or not use_cache:
Rich Lane28fa9272013-03-08 16:00:25 -0800582 request = ofp.message.features_request()
ShreyaPandita60e45542012-09-27 15:11:16 -0400583 (reply, pkt) = parent.controller.transact(request)
584 parent.assertTrue(reply is not None, "Did not get response to ftr req")
585 cache_supported_actions = reply.actions
586 return cache_supported_actions
587
ShreyaPandita66de26f2012-10-26 14:44:24 -0400588##############################################################################################################################################################
589