blob: 92b3024f33c66ded355578486e3fe285b029a2ca [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 Lane28fa9272013-03-08 16:00:25 -080021 msg = ofp.message.flow_mod()
Shudong Zhou857fb602013-02-06 00:11:38 -080022 msg.out_port = ofp.OFPP_NONE
23 msg.command = ofp.OFPFC_ADD
24 # msg.cookie = random.randint(0,9007199254740992)
25 msg.buffer_id = 0xffffffff
26 msg.match = match
27 if priority != None :
28 msg.priority = priority
Rich Lane9d3cc6b2013-03-08 16:33:08 -080029 act = ofp.action.output()
Shudong Zhou857fb602013-02-06 00:11:38 -080030 act.port = port
Rich Lanec495d9e2013-03-08 17:43:36 -080031 msg.actions.append(act)
Shudong Zhou857fb602013-02-06 00:11:38 -080032 self.controller.message_send(msg)
33 do_barrier(self.controller)
34
ShreyaPanditaed209962012-11-04 02:16:48 -050035def exact_match(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -040036# Generate ExactMatch flow .
37
ShreyaPandita4ebbac32012-11-02 13:40:44 -040038 #Create a simple tcp packet and generate exact flow match from it.
39 pkt_exactflow = simple_tcp_packet()
40 match = parse.packet_to_flow_match(pkt_exactflow)
41 self.assertTrue(match is not None, "Could not generate flow match from pkt")
42 match.in_port = of_ports[0]
Rich Laned0478ff2013-03-11 12:46:58 -070043 #match.ipv4_src = 1
ShreyaPandita4ebbac32012-11-02 13:40:44 -040044 match.wildcards=0
Shudong Zhou857fb602013-02-06 00:11:38 -080045 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -040046 return (pkt_exactflow,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -040047
ShreyaPanditaed209962012-11-04 02:16:48 -050048def exact_match_with_prio(self,of_ports,priority=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -040049 # Generate ExactMatch with action output to port 2
ShreyaPandita66de26f2012-10-26 14:44:24 -040050
ShreyaPandita4ebbac32012-11-02 13:40:44 -040051 #Create a simple tcp packet and generate exact flow match from it.
52 pkt_exactflow = simple_tcp_packet()
53 match = parse.packet_to_flow_match(pkt_exactflow)
54 self.assertTrue(match is not None, "Could not generate flow match from pkt")
55 match.in_port = of_ports[0]
Rich Laned0478ff2013-03-11 12:46:58 -070056 #match.ipv4_src = 1
ShreyaPandita4ebbac32012-11-02 13:40:44 -040057 match.wildcards=0
Shudong Zhou857fb602013-02-06 00:11:38 -080058 match_send_flowadd(self, match, priority, of_ports[2])
ShreyaPandita4ebbac32012-11-02 13:40:44 -040059 return (pkt_exactflow,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -040060
ShreyaPandita60e45542012-09-27 15:11:16 -040061
ShreyaPanditaed209962012-11-04 02:16:48 -050062def match_all_except_source_address(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -040063# Generate Match_All_Except_Source_Address flow
64
ShreyaPandita4ebbac32012-11-02 13:40:44 -040065 #Create a simple tcp packet and generate match all except src address flow.
66 pkt_wildcardsrc= simple_tcp_packet()
67 match1 = parse.packet_to_flow_match(pkt_wildcardsrc)
68 self.assertTrue(match1 is not None, "Could not generate flow match from pkt")
69 match1.in_port = of_ports[0]
Rich Laned0478ff2013-03-11 12:46:58 -070070 #match1.ipv4_src = 1
ShreyaPandita4ebbac32012-11-02 13:40:44 -040071 match1.wildcards = ofp.OFPFW_DL_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -080072 match_send_flowadd(self, match1, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -040073 return (pkt_wildcardsrc,match1)
ShreyaPandita66de26f2012-10-26 14:44:24 -040074
ShreyaPandita6fbff252012-11-13 16:56:48 -050075def match_ethernet_src_address(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -040076 #Generate Match_Ethernet_SrC_Address flow
77
ShreyaPandita4ebbac32012-11-02 13:40:44 -040078 #Create a simple tcp packet and generate match on ethernet src address flow
Rich Laned0478ff2013-03-11 12:46:58 -070079 pkt_MatchSrc = simple_eth_packet(eth_src='00:01:01:01:01:01')
ShreyaPandita4ebbac32012-11-02 13:40:44 -040080 match = parse.packet_to_flow_match(pkt_MatchSrc)
81 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita4ebbac32012-11-02 13:40:44 -040082 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -080083 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -040084 return (pkt_MatchSrc,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -040085
ShreyaPanditaed209962012-11-04 02:16:48 -050086def match_ethernet_dst_address(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -040087 #Generate Match_Ethernet_Dst_Address flow
88
ShreyaPandita4ebbac32012-11-02 13:40:44 -040089 #Create a simple tcp packet and generate match on ethernet dst address flow
Rich Laned0478ff2013-03-11 12:46:58 -070090 pkt_matchdst = simple_eth_packet(eth_dst='00:01:01:01:01:01')
ShreyaPandita4ebbac32012-11-02 13:40:44 -040091 match = parse.packet_to_flow_match(pkt_matchdst)
92 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -040093
ShreyaPandita4ebbac32012-11-02 13:40:44 -040094 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_DST
Shudong Zhou857fb602013-02-06 00:11:38 -080095 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -040096 return (pkt_matchdst,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -040097
ShreyaPanditaed209962012-11-04 02:16:48 -050098def wildcard_all(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -040099# Generate a Wildcard_All Flow
100
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400101 #Create a simple tcp packet and generate wildcard all flow match from it.
102 pkt_wildcard = simple_tcp_packet()
103 match2 = parse.packet_to_flow_match(pkt_wildcard)
104 self.assertTrue(match2 is not None, "Could not generate flow match from pkt")
105 match2.wildcards=ofp.OFPFW_ALL
106 match2.in_port = of_ports[0]
Shudong Zhou857fb602013-02-06 00:11:38 -0800107 match_send_flowadd(self, match2, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400108 return (pkt_wildcard,match2)
ShreyaPandita60e45542012-09-27 15:11:16 -0400109
ShreyaPanditaed209962012-11-04 02:16:48 -0500110def wildcard_all_except_ingress(self,of_ports,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400111# Generate Wildcard_All_Except_Ingress_port flow
ShreyaPandita60e45542012-09-27 15:11:16 -0400112
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400113 #Create a simple tcp packet and generate wildcard all except ingress_port flow.
114 pkt_matchingress = simple_tcp_packet()
115 match3 = parse.packet_to_flow_match(pkt_matchingress)
116 self.assertTrue(match3 is not None, "Could not generate flow match from pkt")
117 match3.wildcards = ofp.OFPFW_ALL-ofp.OFPFW_IN_PORT
118 match3.in_port = of_ports[0]
Shudong Zhou857fb602013-02-06 00:11:38 -0800119 match_send_flowadd(self, match3, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400120 return (pkt_matchingress,match3)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400121
ShreyaPanditaed209962012-11-04 02:16:48 -0500122def wildcard_all_except_ingress1(self,of_ports,priority=None):
ShreyaPanditada75f752012-10-26 16:26:35 -0400123# Generate Wildcard_All_Except_Ingress_port flow with action output to port egress_port 2
ShreyaPanditada75f752012-10-26 16:26:35 -0400124
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400125 #Create a simple tcp packet and generate wildcard all except ingress_port flow.
126 pkt_matchingress = simple_tcp_packet()
127 match3 = parse.packet_to_flow_match(pkt_matchingress)
128 self.assertTrue(match3 is not None, "Could not generate flow match from pkt")
129 match3.wildcards = ofp.OFPFW_ALL-ofp.OFPFW_IN_PORT
130 match3.in_port = of_ports[0]
Shudong Zhou857fb602013-02-06 00:11:38 -0800131 match_send_flowadd(self, match3, priority, of_ports[2])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400132 return (pkt_matchingress,match3)
ShreyaPanditada75f752012-10-26 16:26:35 -0400133
134
ShreyaPanditaed209962012-11-04 02:16:48 -0500135def match_vlan_id(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400136 #Generate Match_Vlan_Id
137
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400138 #Create a simple tcp packet and generate match on ethernet dst address flow
Rich Laned0478ff2013-03-11 12:46:58 -0700139 pkt_matchvlanid = simple_tcp_packet(dl_vlan_enable=True,vlan_vid=1)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400140 match = parse.packet_to_flow_match(pkt_matchvlanid)
141 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400142
ShreyaPandita6fbff252012-11-13 16:56:48 -0500143 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_DL_VLAN
Shudong Zhou857fb602013-02-06 00:11:38 -0800144 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400145 return (pkt_matchvlanid,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400146
ShreyaPanditaed209962012-11-04 02:16:48 -0500147def match_vlan_pcp(self,of_ports,priority=None):
ShreyaPandita055102a2012-11-28 11:43:45 -0500148 #Generate Match_Vlan_Priority
ShreyaPandita66de26f2012-10-26 14:44:24 -0400149
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400150 #Create a simple tcp packet and generate match on ethernet dst address flow
Rich Laned0478ff2013-03-11 12:46:58 -0700151 pkt_matchvlanpcp = simple_tcp_packet(dl_vlan_enable=True,vlan_vid=1,vlan_pcp=5)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400152 match = parse.packet_to_flow_match(pkt_matchvlanpcp)
153 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400154
ShreyaPandita6fbff252012-11-13 16:56:48 -0500155 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 -0800156 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400157 return (pkt_matchvlanpcp,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400158
159
ShreyaPanditaed209962012-11-04 02:16:48 -0500160def match_mul_l2(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400161 #Generate Match_Mul_L2 flow
162
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400163 #Create a simple eth packet and generate match on ethernet protocol flow
Rich Laned0478ff2013-03-11 12:46:58 -0700164 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 -0400165 match = parse.packet_to_flow_match(pkt_mulL2)
166 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400167
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400168 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_DL_DST ^ofp.OFPFW_DL_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -0800169 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400170 return (pkt_mulL2,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400171
172
ShreyaPandita6fbff252012-11-13 16:56:48 -0500173def match_mul_l4(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400174 #Generate Match_Mul_L4 flow
175
176 #Create a simple tcp packet and generate match on tcp protocol flow
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400177 pkt_mulL4 = simple_tcp_packet(tcp_sport=111,tcp_dport=112)
178 match = parse.packet_to_flow_match(pkt_mulL4)
179 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita6fbff252012-11-13 16:56:48 -0500180 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 -0800181 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400182 return (pkt_mulL4,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400183
ShreyaPanditaed209962012-11-04 02:16:48 -0500184def match_ip_tos(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400185 #Generate a Match on IP Type of service flow
186
Shudong Zhou857fb602013-02-06 00:11:38 -0800187 #Create a simple tcp packet and generate match on Type of service
Rich Laneb5c73792012-12-03 17:12:32 -0800188 pkt_iptos = simple_tcp_packet(ip_tos=28)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400189 match = parse.packet_to_flow_match(pkt_iptos)
190 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400191
ShreyaPandita6fbff252012-11-13 16:56:48 -0500192 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_NW_TOS
Shudong Zhou857fb602013-02-06 00:11:38 -0800193 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400194 return (pkt_iptos,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400195
ShreyaPandita6fbff252012-11-13 16:56:48 -0500196def match_ip_protocol(self,of_ports,priority=None):
197 #Generate a Match on IP Protocol
198
199 #Create a simple tcp packet and generate match on Type of service
200 pkt_iptos = simple_tcp_packet()
201 match = parse.packet_to_flow_match(pkt_iptos)
202 self.assertTrue(match is not None, "Could not generate flow match from pkt")
203
204 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO
Shudong Zhou857fb602013-02-06 00:11:38 -0800205 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita6fbff252012-11-13 16:56:48 -0500206 return (pkt_iptos,match)
207
ShreyaPanditaed209962012-11-04 02:16:48 -0500208def match_tcp_src(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400209 #Generate Match_Tcp_Src
210
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400211 #Create a simple tcp packet and generate match on tcp source port flow
212 pkt_matchtSrc = simple_tcp_packet(tcp_sport=111)
213 match = parse.packet_to_flow_match(pkt_matchtSrc)
214 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400215
ShreyaPandita6fbff252012-11-13 16:56:48 -0500216 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -0800217 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400218 return (pkt_matchtSrc,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400219
ShreyaPanditaed209962012-11-04 02:16:48 -0500220def match_tcp_dst(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400221 #Generate Match_Tcp_Dst
222
Shudong Zhou857fb602013-02-06 00:11:38 -0800223 #Create a simple tcp packet and generate match on tcp destination port flow
ShreyaPandita6fbff252012-11-13 16:56:48 -0500224 pkt_matchdst = simple_tcp_packet(tcp_dport=112)
225 match = parse.packet_to_flow_match(pkt_matchdst)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400226 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400227
ShreyaPandita6fbff252012-11-13 16:56:48 -0500228 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO^ofp.OFPFW_TP_DST
Shudong Zhou857fb602013-02-06 00:11:38 -0800229 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita6fbff252012-11-13 16:56:48 -0500230 return (pkt_matchdst,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400231
ShreyaPandita055102a2012-11-28 11:43:45 -0500232
Shudong Zhouc2f18762013-01-11 00:12:44 -0800233def match_udp_src(self,of_ports,priority=None):
234 #Generate Match_Udp_Src
235
236 #Create a simple udp packet and generate match on udp source port flow
237 pkt_matchtSrc = simple_udp_packet(udp_sport=111)
238 match = parse.packet_to_flow_match(pkt_matchtSrc)
239 self.assertTrue(match is not None, "Could not generate flow match from pkt")
240
241 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -0800242 match_send_flowadd(self, match, priority, of_ports[1])
Shudong Zhouc2f18762013-01-11 00:12:44 -0800243 return (pkt_matchtSrc,match)
244
245def match_udp_dst(self,of_ports,priority=None):
246 #Generate Match_Udp_Dst
247
Shudong Zhou857fb602013-02-06 00:11:38 -0800248 #Create a simple udp packet and generate match on udp destination port flow
Shudong Zhouc2f18762013-01-11 00:12:44 -0800249 pkt_matchdst = simple_udp_packet(udp_dport=112)
250 match = parse.packet_to_flow_match(pkt_matchdst)
251 self.assertTrue(match is not None, "Could not generate flow match from pkt")
252
253 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE^ofp.OFPFW_NW_PROTO^ofp.OFPFW_TP_DST
Shudong Zhou857fb602013-02-06 00:11:38 -0800254 match_send_flowadd(self, match, priority, of_ports[1])
Shudong Zhouc2f18762013-01-11 00:12:44 -0800255 return (pkt_matchdst,match)
256
257
258def match_icmp_type(self,of_ports,priority=None):
259 #Generate Match_Icmp_Type
260
261 #Create a simple icmp packet and generate match on icmp type flow
262 pkt_match = simple_icmp_packet(icmp_type=1)
263 match = parse.packet_to_flow_match(pkt_match)
264 self.assertTrue(match is not None, "Could not generate flow match from pkt")
265
266 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_SRC
Shudong Zhou857fb602013-02-06 00:11:38 -0800267 match_send_flowadd(self, match, priority, of_ports[1])
Shudong Zhouc2f18762013-01-11 00:12:44 -0800268 return (pkt_match, match)
269
270def match_icmp_code(self,of_ports,priority=None):
271 #Generate Match_Icmp_Code
272
273 #Create a simple icmp packet and generate match on icmp code flow
274 pkt_match = simple_icmp_packet(icmp_code=3)
275 match = parse.packet_to_flow_match(pkt_match)
276 self.assertTrue(match is not None, "Could not generate flow match from pkt")
277
278 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_TP_DST
Shudong Zhou857fb602013-02-06 00:11:38 -0800279 match_send_flowadd(self, match, priority, of_ports[1])
Shudong Zhouc2f18762013-01-11 00:12:44 -0800280 return (pkt_match, match)
281
Shudong Zhoudceec932013-02-06 01:12:54 -0800282def match_arp_sender(self,of_ports,priority=None):
283 #Generate Match_Arp_Sender
284
285 #Create a simple icmp packet and generate match on arp sender flow
286 pkt_match = simple_arp_packet()
287 match = parse.packet_to_flow_match(pkt_match)
288 self.assertTrue(match is not None, "Could not generate flow match from pkt")
289
290 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_NW_SRC_MASK
291 match_send_flowadd(self, match, priority, of_ports[1])
292 return (pkt_match, match)
293
294def match_arp_target(self,of_ports,priority=None):
295 #Generate Match_Arp_Target
296
297 #Create a simple icmp packet and generate match on arp target flow
298 pkt_match = simple_arp_packet()
299 match = parse.packet_to_flow_match(pkt_match)
300 self.assertTrue(match is not None, "Could not generate flow match from pkt")
301
302 match.wildcards = ofp.OFPFW_ALL^ofp.OFPFW_DL_TYPE ^ofp.OFPFW_NW_PROTO ^ofp.OFPFW_NW_DST_MASK
303 match_send_flowadd(self, match, priority, of_ports[1])
304 return (pkt_match, match)
305
Shudong Zhouc2f18762013-01-11 00:12:44 -0800306
ShreyaPanditaed209962012-11-04 02:16:48 -0500307def match_ethernet_type(self,of_ports,priority=None):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400308 #Generate a Match_Ethernet_Type flow
309
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400310 #Create a simple tcp packet and generate match on ethernet type flow
Rich Laned0478ff2013-03-11 12:46:58 -0700311 pkt_matchtype = simple_eth_packet(eth_type=0x88cc)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400312 match = parse.packet_to_flow_match(pkt_matchtype)
313 self.assertTrue(match is not None, "Could not generate flow match from pkt")
ShreyaPandita66de26f2012-10-26 14:44:24 -0400314
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400315 match.wildcards = ofp.OFPFW_ALL ^ofp.OFPFW_DL_TYPE
Shudong Zhou857fb602013-02-06 00:11:38 -0800316 match_send_flowadd(self, match, priority, of_ports[1])
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400317 return (pkt_matchtype,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400318
319
ShreyaPandita60e45542012-09-27 15:11:16 -0400320
ShreyaPandita6fbff252012-11-13 16:56:48 -0500321
ShreyaPanditaed209962012-11-04 02:16:48 -0500322def strict_modify_flow_action(self,egress_port,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400323# Strict Modify the flow Action
324
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400325 #Create a flow_mod message , command MODIFY_STRICT
Rich Lane28fa9272013-03-08 16:00:25 -0800326 msg5 = ofp.message.flow_mod()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400327 msg5.match = match
328 msg5.cookie = random.randint(0,9007199254740992)
329 msg5.command = ofp.OFPFC_MODIFY_STRICT
330 msg5.buffer_id = 0xffffffff
Rich Lane9d3cc6b2013-03-08 16:33:08 -0800331 act5 = ofp.action.output()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400332 act5.port = egress_port
Rich Lanec495d9e2013-03-08 17:43:36 -0800333 msg5.actions.append(act5)
ShreyaPandita60e45542012-09-27 15:11:16 -0400334
ShreyaPanditaed209962012-11-04 02:16:48 -0500335 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400336 msg5.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400337
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400338 # Send the flow with action A'
Rich Lane5c3151c2013-01-03 17:15:41 -0800339 self.controller.message_send (msg5)
Rich Lane3a261d52013-01-03 17:45:08 -0800340 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400341
ShreyaPanditaed209962012-11-04 02:16:48 -0500342def modify_flow_action(self,of_ports,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400343# Modify the flow action
344
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400345 #Create a flow_mod message , command MODIFY
Rich Lane28fa9272013-03-08 16:00:25 -0800346 msg8 = ofp.message.flow_mod()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400347 msg8.match = match
348 msg8.cookie = random.randint(0,9007199254740992)
349 msg8.command = ofp.OFPFC_MODIFY
350 #out_port will be ignored for flow adds and flow modify (here for test-case Add_Modify_With_Outport)
351 msg8.out_port = of_ports[3]
352 msg8.buffer_id = 0xffffffff
Rich Lane9d3cc6b2013-03-08 16:33:08 -0800353 act8 = ofp.action.output()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400354 act8.port = of_ports[2]
Rich Lanec495d9e2013-03-08 17:43:36 -0800355 msg8.actions.append(act8)
ShreyaPandita60e45542012-09-27 15:11:16 -0400356
ShreyaPanditaed209962012-11-04 02:16:48 -0500357 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400358 msg8.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400359
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400360 # Send the flow with action A'
Rich Lane5c3151c2013-01-03 17:15:41 -0800361 self.controller.message_send (msg8)
Rich Lane3a261d52013-01-03 17:45:08 -0800362 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400363
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400364def enqueue(self,ingress_port,egress_port,egress_queue_id):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400365#Generate a flow with enqueue action i.e output to a queue configured on a egress_port
366
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400367 pkt = simple_tcp_packet()
368 match = packet_to_flow_match(self, pkt)
369 match.wildcards &= ~ofp.OFPFW_IN_PORT
370 self.assertTrue(match is not None,
371 "Could not generate flow match from pkt")
372
373 match.in_port = ingress_port
Rich Lane28fa9272013-03-08 16:00:25 -0800374 request = ofp.message.flow_mod()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400375 request.match = match
376 request.buffer_id = 0xffffffff
Rich Lane9d3cc6b2013-03-08 16:33:08 -0800377 act = ofp.action.enqueue()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400378 act.port = egress_port
379 act.queue_id = egress_queue_id
Rich Lanec495d9e2013-03-08 17:43:36 -0800380 request.actions.append(act)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400381
382 logging.info("Inserting flow")
Rich Lane5c3151c2013-01-03 17:15:41 -0800383 self.controller.message_send(request)
Rich Lane3a261d52013-01-03 17:45:08 -0800384 do_barrier(self.controller)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400385 return (pkt,match)
ShreyaPandita66de26f2012-10-26 14:44:24 -0400386
387
ShreyaPandita60e45542012-09-27 15:11:16 -0400388########################### Verify Stats Functions ###########################################################################################
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400389def get_flowstats(self,match):
390 # Generate flow_stats request
391
Rich Lane28fa9272013-03-08 16:00:25 -0800392 stat_req = ofp.message.flow_stats_request()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400393 stat_req.match = match
394 stat_req.table_id = 0xff
395 stat_req.out_port = ofp.OFPP_NONE
396
397 logging.info("Sending stats request")
398 response, pkt = self.controller.transact(stat_req,
399 timeout=5)
400 self.assertTrue(response is not None,"No response to stats request")
ShreyaPandita60e45542012-09-27 15:11:16 -0400401
ShreyaPandita66de26f2012-10-26 14:44:24 -0400402
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400403def get_portstats(self,port_num):
404
405# Return all the port counters in the form a tuple
Rich Lane28fa9272013-03-08 16:00:25 -0800406 port_stats_req = ofp.message.port_stats_request()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400407 port_stats_req.port_no = port_num
408 response,pkt = self.controller.transact(port_stats_req)
409 self.assertTrue(response is not None,"No response received for port stats request")
410 rx_pkts=0
411 tx_pkts=0
412 rx_byts=0
413 tx_byts=0
414 rx_drp =0
415 tx_drp = 0
416 rx_err=0
417 tx_err =0
418 rx_fr_err=0
419 rx_ovr_err=0
420 rx_crc_err=0
421 collisions = 0
422 tx_err=0
423
424
Rich Lane5fd6faf2013-03-11 13:30:20 -0700425 for obj in response.entries:
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400426 rx_pkts += obj.rx_packets
427 tx_pkts += obj.tx_packets
428 rx_byts += obj.rx_bytes
429 tx_byts += obj.tx_bytes
430 rx_drp += obj.rx_dropped
431 tx_drp += obj.tx_dropped
432 rx_err += obj.rx_errors
433 rx_fr_err += obj.rx_frame_err
434 rx_ovr_err += obj.rx_over_err
435 rx_crc_err += obj.rx_crc_err
436 collisions+= obj.collisions
437 tx_err += obj.tx_errors
438
439 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)
440
441def get_queuestats(self,port_num,queue_id):
ShreyaPandita66de26f2012-10-26 14:44:24 -0400442#Generate Queue Stats request
443
Rich Lane28fa9272013-03-08 16:00:25 -0800444 request = ofp.message.queue_stats_request()
ShreyaPandita66de26f2012-10-26 14:44:24 -0400445 request.port_no = port_num
446 request.queue_id = queue_id
447 (queue_stats, p) = self.controller.transact(request)
448 self.assertNotEqual(queue_stats, None, "Queue stats request failed")
449
450 return (queue_stats,p)
451
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400452def get_tablestats(self):
453# Send Table_Stats request (retrieve current table counters )
454
Rich Lane28fa9272013-03-08 16:00:25 -0800455 stat_req = ofp.message.table_stats_request()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400456 response, pkt = self.controller.transact(stat_req,
457 timeout=5)
458 self.assertTrue(response is not None,
459 "No response to stats request")
460 current_lookedup = 0
461 current_matched = 0
462 current_active = 0
463
Rich Lane5fd6faf2013-03-11 13:30:20 -0700464 for obj in response.entries:
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400465 current_lookedup += obj.lookup_count
466 current_matched += obj.matched_count
467 current_active += obj.active_count
468
469 return (current_lookedup,current_matched,current_active)
470
471
472
ShreyaPanditaed209962012-11-04 02:16:48 -0500473def verify_tablestats(self,expect_lookup=None,expect_match=None,expect_active=None):
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400474
Rich Lane28fa9272013-03-08 16:00:25 -0800475 stat_req = ofp.message.table_stats_request()
ShreyaPanditaed209962012-11-04 02:16:48 -0500476
Rich Lane90b3d732012-12-31 10:03:50 -0800477 for i in range(0,100):
ShreyaPandita60e45542012-09-27 15:11:16 -0400478
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400479 logging.info("Sending stats request")
480 # TODO: move REPLY_MORE handling to controller.transact?
ShreyaPandita66de26f2012-10-26 14:44:24 -0400481 response, pkt = self.controller.transact(stat_req,
482 timeout=5)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400483 self.assertTrue(response is not None,"No response to stats request")
ShreyaPandita60e45542012-09-27 15:11:16 -0400484
ShreyaPanditaed209962012-11-04 02:16:48 -0500485 lookedup = 0
486 matched = 0
487 active = 0
488
Rich Lane5fd6faf2013-03-11 13:30:20 -0700489 for item in response.entries:
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400490 lookedup += item.lookup_count
491 matched += item.matched_count
492 active += item.active_count
493
Rich Laneafcd0dd2013-01-03 20:54:56 -0800494 logging.info("Packets Looked up: %d", lookedup)
495 logging.info("Packets matched: %d", matched)
496 logging.info("Active flow entries: %d", active)
ShreyaPanditaed209962012-11-04 02:16:48 -0500497
Rich Lane175f9562013-01-03 20:58:40 -0800498 if (expect_lookup == None or lookedup >= expect_lookup) and \
499 (expect_match == None or matched >= expect_match) and \
500 (expect_active == None or active >= expect_active):
Rich Lane90b3d732012-12-31 10:03:50 -0800501 break
502
503 sleep(0.1)
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400504
ShreyaPanditaed209962012-11-04 02:16:48 -0500505 if expect_lookup != None :
Rich Lane3e777792013-01-03 21:30:30 -0800506 self.assertEqual(expect_lookup, lookedup, "lookup counter is not incremented properly")
ShreyaPanditaed209962012-11-04 02:16:48 -0500507 if expect_match != None :
Rich Lane3e777792013-01-03 21:30:30 -0800508 self.assertEqual(expect_match, matched, "matched counter is not incremented properly")
ShreyaPanditaed209962012-11-04 02:16:48 -0500509 if expect_active != None :
Rich Lane3e777792013-01-03 21:30:30 -0800510 self.assertEqual(expect_active, active ,"active counter is not incremented properly")
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400511
512
ShreyaPandita60e45542012-09-27 15:11:16 -0400513############################## Various delete commands #############################################################################################
514
ShreyaPanditaed209962012-11-04 02:16:48 -0500515def strict_delete(self,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400516# Issue Strict Delete
517
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400518 #Create flow_mod message, command DELETE_STRICT
Rich Lane28fa9272013-03-08 16:00:25 -0800519 msg4 = ofp.message.flow_mod()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400520 msg4.out_port = ofp.OFPP_NONE
521 msg4.command = ofp.OFPFC_DELETE_STRICT
522 msg4.buffer_id = 0xffffffff
523 msg4.match = match
ShreyaPandita60e45542012-09-27 15:11:16 -0400524
ShreyaPanditaed209962012-11-04 02:16:48 -0500525 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400526 msg4.priority = priority
Rich Lane5c3151c2013-01-03 17:15:41 -0800527 self.controller.message_send(msg4)
Rich Lane3a261d52013-01-03 17:45:08 -0800528 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400529
530
531
ShreyaPanditaed209962012-11-04 02:16:48 -0500532def nonstrict_delete(self,match,priority=None):
ShreyaPandita60e45542012-09-27 15:11:16 -0400533# Issue Non_Strict Delete
534
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400535 #Create flow_mod message, command DELETE
Rich Lane28fa9272013-03-08 16:00:25 -0800536 msg6 = ofp.message.flow_mod()
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400537 msg6.out_port = ofp.OFPP_NONE
538 msg6.command = ofp.OFPFC_DELETE
539 msg6.buffer_id = 0xffffffff
540 msg6.match = match
ShreyaPandita60e45542012-09-27 15:11:16 -0400541
ShreyaPanditaed209962012-11-04 02:16:48 -0500542 if priority != None :
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400543 msg6.priority = priority
ShreyaPandita60e45542012-09-27 15:11:16 -0400544
Rich Lane5c3151c2013-01-03 17:15:41 -0800545 self.controller.message_send(msg6)
Rich Lane3a261d52013-01-03 17:45:08 -0800546 do_barrier(self.controller)
ShreyaPandita60e45542012-09-27 15:11:16 -0400547
548
549###########################################################################################################################################################
550
ShreyaPandita4ebbac32012-11-02 13:40:44 -0400551def send_packet(obj, pkt, ingress_port, egress_port):
ShreyaPandita60e45542012-09-27 15:11:16 -0400552#Send Packets on a specified ingress_port and verify if its recieved on correct egress_port.
553
554 obj.dataplane.send(ingress_port, str(pkt))
555 exp_pkt_arg = pkt
556 exp_port = egress_port
557
558 (rcv_port, rcv_pkt, pkt_time) = obj.dataplane.poll(timeout=2,
559 port_number=exp_port,
560 exp_pkt=exp_pkt_arg)
561 obj.assertTrue(rcv_pkt is not None,
562 "Packet not received on port " + str(egress_port))
563 obj.assertEqual(rcv_port, egress_port,
564 "Packet received on port " + str(rcv_port) +
565 ", expected port " + str(egress_port))
566 obj.assertEqual(str(pkt), str(rcv_pkt),
567 'Response packet does not match send packet')
568
569
ShreyaPandita572e64b2012-09-28 14:41:06 -0400570def sw_supported_actions(parent,use_cache=False):
ShreyaPandita60e45542012-09-27 15:11:16 -0400571#Returns the switch's supported actions
572
573 cache_supported_actions = None
574 if cache_supported_actions is None or not use_cache:
Rich Lane28fa9272013-03-08 16:00:25 -0800575 request = ofp.message.features_request()
ShreyaPandita60e45542012-09-27 15:11:16 -0400576 (reply, pkt) = parent.controller.transact(request)
577 parent.assertTrue(reply is not None, "Did not get response to ftr req")
578 cache_supported_actions = reply.actions
579 return cache_supported_actions
580
ShreyaPandita66de26f2012-10-26 14:44:24 -0400581##############################################################################################################################################################
582