ShreyaPandita | cf64cc9 | 2012-09-14 17:25:33 -0400 | [diff] [blame] | 1 | """ Defined Some common functions used by Conformance tests -- OF-SWITCH 1.0.0 Testcases """ |
| 2 | |
| 3 | import sys |
| 4 | import copy |
| 5 | import random |
| 6 | |
| 7 | import oftest.controller as controller |
| 8 | import oftest.cstruct as ofp |
| 9 | import oftest.message as message |
| 10 | import oftest.dataplane as dataplane |
| 11 | import oftest.action as action |
| 12 | import oftest.parse as parse |
| 13 | import logging |
| 14 | import types |
| 15 | import basic |
| 16 | from testutils import * |
| 17 | |
| 18 | #################### Functions for various types of flow_mod ########################################################################################## |
| 19 | |
| 20 | def Exact_Match(self,of_ports,priority=0): |
| 21 | # Generate ExactMatch flow . |
| 22 | |
| 23 | #Create a simple tcp packet and generate exact flow match from it. |
| 24 | Pkt_ExactFlow = simple_tcp_packet() |
| 25 | match = parse.packet_to_flow_match(Pkt_ExactFlow) |
| 26 | self.assertTrue(match is not None, "Could not generate flow match from pkt") |
| 27 | match.in_port = of_ports[0] |
| 28 | #match.nw_src = 1 |
| 29 | match.wildcards=0 |
| 30 | msg = message.flow_mod() |
| 31 | msg.out_port = ofp.OFPP_NONE |
| 32 | msg.command = ofp.OFPFC_ADD |
| 33 | msg.buffer_id = 0xffffffff |
| 34 | msg.match = match |
| 35 | if priority != 0 : |
| 36 | msg.priority = priority |
| 37 | |
| 38 | act = action.action_output() |
| 39 | act.port = of_ports[1] |
| 40 | self.assertTrue(msg.actions.add(act), "could not add action") |
| 41 | |
| 42 | rv = self.controller.message_send(msg) |
| 43 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 44 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
| 45 | |
| 46 | return (Pkt_ExactFlow,match) |
| 47 | |
| 48 | |
| 49 | def Match_All_Except_Source_Address(self,of_ports,priority=0): |
| 50 | # Generate Match_All_Except_Source_Address flow |
| 51 | |
| 52 | #Create a simple tcp packet and generate match all except src address flow. |
| 53 | Pkt_WildcardSrc= simple_tcp_packet() |
| 54 | match1 = parse.packet_to_flow_match(Pkt_WildcardSrc) |
| 55 | self.assertTrue(match1 is not None, "Could not generate flow match from pkt") |
| 56 | match1.in_port = of_ports[0] |
| 57 | #match1.nw_src = 1 |
| 58 | match1.wildcards = ofp.OFPFW_DL_SRC |
| 59 | msg1 = message.flow_mod() |
| 60 | msg1.out_port = ofp.OFPP_NONE |
| 61 | msg1.command = ofp.OFPFC_ADD |
| 62 | msg1.buffer_id = 0xffffffff |
| 63 | msg1.match = match1 |
| 64 | if priority != 0 : |
| 65 | msg1.priority = priority |
| 66 | |
| 67 | act1 = action.action_output() |
| 68 | act1.port = of_ports[1] |
| 69 | self.assertTrue(msg1.actions.add(act1), "could not add action") |
| 70 | |
| 71 | rv = self.controller.message_send(msg1) |
| 72 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 73 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
| 74 | |
| 75 | return (Pkt_WildcardSrc,match1) |
| 76 | |
| 77 | def Wildcard_All(self,of_ports,priority=0): |
| 78 | # Generate a Wildcard_All Flow |
| 79 | |
| 80 | #Create a simple tcp packet and generate wildcard all flow match from it. |
| 81 | Pkt_Wildcard = simple_tcp_packet() |
| 82 | match2 = parse.packet_to_flow_match(Pkt_Wildcard) |
| 83 | self.assertTrue(match2 is not None, "Could not generate flow match from pkt") |
| 84 | match2.wildcards=ofp.OFPFW_ALL |
| 85 | msg2 = message.flow_mod() |
| 86 | msg2.out_port = ofp.OFPP_NONE |
| 87 | msg2.command = ofp.OFPFC_ADD |
| 88 | msg2.buffer_id = 0xffffffff |
| 89 | msg2.match = match2 |
| 90 | act2 = action.action_output() |
| 91 | act2.port = of_ports[2] |
| 92 | self.assertTrue(msg2.actions.add(act2), "could not add action") |
| 93 | if priority != 0 : |
| 94 | msg2.priority = priority |
| 95 | |
| 96 | rv = self.controller.message_send(msg2) |
| 97 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 98 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
| 99 | |
| 100 | return (Pkt_Wildcard,match2) |
| 101 | |
| 102 | def Wildcard_All_Except_Ingress(self,of_ports,priority=0): |
| 103 | # Generate Wildcard_All_Except_Ingress_port flow |
| 104 | |
| 105 | |
| 106 | #Create a simple tcp packet and generate wildcard all except ingress_port flow. |
| 107 | Pkt_MatchIngress = simple_tcp_packet() |
| 108 | match3 = parse.packet_to_flow_match(Pkt_MatchIngress) |
| 109 | self.assertTrue(match3 is not None, "Could not generate flow match from pkt") |
| 110 | match3.wildcards = ofp.OFPFW_ALL-ofp.OFPFW_IN_PORT |
| 111 | match3.in_port = of_ports[0] |
| 112 | |
| 113 | msg3 = message.flow_mod() |
| 114 | msg3.command = ofp.OFPFC_ADD |
| 115 | msg3.match = match3 |
| 116 | msg3.out_port = of_ports[2] # ignored by flow add,flow modify |
| 117 | msg3.cookie = random.randint(0,9007199254740992) |
| 118 | msg3.buffer_id = 0xffffffff |
| 119 | msg3.idle_timeout = 0 |
| 120 | msg3.hard_timeout = 0 |
| 121 | msg3.buffer_id = 0xffffffff |
| 122 | |
| 123 | act3 = action.action_output() |
| 124 | act3.port = of_ports[1] |
| 125 | self.assertTrue(msg3.actions.add(act3), "could not add action") |
| 126 | |
| 127 | if priority != 0 : |
| 128 | msg3.priority = priority |
| 129 | |
| 130 | rv = self.controller.message_send(msg3) |
| 131 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 132 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
| 133 | |
| 134 | return (Pkt_MatchIngress,match3) |
| 135 | |
| 136 | |
| 137 | def Strict_Modify_Flow_Action(self,egress_port,match,priority=0): |
| 138 | # Strict Modify the flow Action |
| 139 | |
| 140 | #Create a flow_mod message , command MODIFY_STRICT |
| 141 | msg5 = message.flow_mod() |
| 142 | msg5.match = match |
| 143 | msg5.cookie = random.randint(0,9007199254740992) |
| 144 | msg5.command = ofp.OFPFC_MODIFY_STRICT |
| 145 | msg5.buffer_id = 0xffffffff |
| 146 | act5 = action.action_output() |
| 147 | act5.port = egress_port |
| 148 | self.assertTrue(msg5.actions.add(act5), "could not add action") |
| 149 | |
| 150 | if priority != 0 : |
| 151 | msg5.priority = priority |
| 152 | |
| 153 | # Send the flow with action A' |
| 154 | rv = self.controller.message_send (msg5) |
| 155 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 156 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
| 157 | |
| 158 | def Modify_Flow_Action(self,of_ports,match,priority=0): |
| 159 | # Modify the flow action |
| 160 | |
| 161 | #Create a flow_mod message , command MODIFY |
| 162 | msg8 = message.flow_mod() |
| 163 | msg8.match = match |
| 164 | msg8.cookie = random.randint(0,9007199254740992) |
| 165 | msg8.command = ofp.OFPFC_MODIFY |
| 166 | #Will be ignored for flow adds and flow modify (here for test-case Add_Modify_With_Outport) |
| 167 | msg8.out_port = of_ports[3] |
| 168 | msg8.buffer_id = 0xffffffff |
| 169 | act8 = action.action_output() |
| 170 | act8.port = of_ports[2] |
| 171 | self.assertTrue(msg8.actions.add(act8), "could not add action") |
| 172 | |
| 173 | if priority != 0 : |
| 174 | msg8.priority = priority |
| 175 | |
| 176 | # Send the flow with action A' |
| 177 | rv = self.controller.message_send (msg8) |
| 178 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 179 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
| 180 | |
| 181 | |
| 182 | ########################### Verify Stats Functions ########################################################################################### |
| 183 | |
| 184 | def Verify_TableStats(self,active_entries=0): |
| 185 | #Verify Table_Stats |
| 186 | |
| 187 | #Send Table_Stats_Request |
| 188 | request = message.table_stats_request() |
| 189 | response, pkt = self.controller.transact(request, timeout=1) |
| 190 | self.assertTrue(response is not None, "Did not get response") |
| 191 | active_count=0 |
| 192 | |
| 193 | #Verify active_count in the reply |
| 194 | for stat in response.stats: |
| 195 | active_count += stat.active_count |
| 196 | self.assertTrue(active_entries == active_count,"Incorrect no. of flows in Table") |
| 197 | |
| 198 | |
| 199 | def Verify_FlowStats(self,match,stats_byte_count=0,stats_packet_count=0): |
| 200 | # Verify Flow_Stats |
| 201 | |
| 202 | # Send Flow_Stats_Request |
| 203 | request = message.flow_stats_request() |
| 204 | request.out_port = ofp.OFPP_NONE |
| 205 | request.table_id = 0xff |
| 206 | request.match = match |
| 207 | response, pkt = self.controller.transact(request, timeout=1) |
| 208 | self.assertTrue(response is not None, "Did not get response") |
| 209 | byte_count = 0 |
| 210 | packet_count = 0 |
| 211 | |
| 212 | #Verify byte_count and packet_count in the reply |
| 213 | for stat in response.stats: |
| 214 | byte_count += stat.byte_count |
| 215 | packet_count += stat.packet_count |
| 216 | self.assertEqual(stats_byte_count,byte_count, |
| 217 | "Byte counter is incorrect") |
| 218 | self.assertEqual(stats_packet_count,packet_count, |
| 219 | "Packet counter is incorrect") |
| 220 | |
| 221 | |
| 222 | ############################## Various delete commands ############################################################################################# |
| 223 | |
| 224 | def Strict_Delete(self,match,priority=0): |
| 225 | # Issue Strict Delete |
| 226 | |
| 227 | #Create flow_mod message, command DELETE_STRICT |
| 228 | msg4 = message.flow_mod() |
| 229 | msg4.out_port = ofp.OFPP_NONE |
| 230 | msg4.command = ofp.OFPFC_DELETE_STRICT |
| 231 | msg4.buffer_id = 0xffffffff |
| 232 | msg4.match = match |
| 233 | |
| 234 | if priority != 0 : |
| 235 | msg4.priority = priority |
| 236 | |
| 237 | rv = self.controller.message_send(msg4) |
| 238 | self.assertTrue(rv!= -1, "Error installing flow mod") |
| 239 | self.assertEqual(do_barrier(self.controller), 0, "Barrier failed") |
| 240 | |
| 241 | |
| 242 | |
| 243 | def NonStrict_Delete(self,match,priority=0): |
| 244 | # Issue Non_Strict Delete |
| 245 | |
| 246 | #Create flow_mod message, command DELETE |
| 247 | msg6 = message.flow_mod() |
| 248 | msg6.out_port = ofp.OFPP_NONE |
| 249 | msg6.command = ofp.OFPFC_DELETE |
| 250 | msg6.buffer_id = 0xffffffff |
| 251 | msg6.match = match |
| 252 | |
| 253 | if priority != 0 : |
| 254 | msg6.priority = priority |
| 255 | |
| 256 | rv = self.controller.message_send(msg6) |
| 257 | self.assertTrue(rv != -1, "Error installing flow mod") |
| 258 | self.assertEqual(do_barrier(self.controller),0, "Barrier failed") |
| 259 | |
| 260 | |
| 261 | ########################################################################################################################################################### |
| 262 | |
| 263 | def SendPacket(obj, pkt, ingress_port, egress_port): |
| 264 | #Send Packets on a specified ingress_port and verify if its recieved on correct egress_port. |
| 265 | |
| 266 | obj.dataplane.send(ingress_port, str(pkt)) |
| 267 | exp_pkt_arg = pkt |
| 268 | exp_port = egress_port |
| 269 | |
| 270 | (rcv_port, rcv_pkt, pkt_time) = obj.dataplane.poll(timeout=2, |
| 271 | port_number=exp_port, |
| 272 | exp_pkt=exp_pkt_arg) |
| 273 | obj.assertTrue(rcv_pkt is not None, |
| 274 | "Packet not received on port " + str(egress_port)) |
| 275 | obj.assertEqual(rcv_port, egress_port, |
| 276 | "Packet received on port " + str(rcv_port) + |
| 277 | ", expected port " + str(egress_port)) |
| 278 | obj.assertEqual(str(pkt), str(rcv_pkt), |
| 279 | 'Response packet does not match send packet') |