macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 1 | import logging |
| 2 | |
| 3 | from oftest import config |
| 4 | import oftest.base_tests as base_tests |
| 5 | import ofp |
| 6 | import time |
| 7 | from oftest.testutils import * |
| 8 | |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 9 | from ncclient import manager |
| 10 | import ncclient |
| 11 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 12 | OFDPA_GROUP_TYPE_SHIFT=28 |
| 13 | OFDPA_VLAN_ID_SHIFT =16 |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 14 | OFDPA_TUNNEL_ID_SHIFT =12 |
| 15 | OFDPA_TUNNEL_SUBTYPE_SHIFT=10 |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 16 | |
| 17 | #VLAN_TABLE_FLAGS |
| 18 | VLAN_TABLE_FLAG_ONLY_UNTAG=1 |
| 19 | VLAN_TABLE_FLAG_ONLY_TAG =2 |
| 20 | VLAN_TABLE_FLAG_ONLY_BOTH =3 |
| 21 | |
macauley | e8b140e | 2015-08-03 13:35:45 +0800 | [diff] [blame] | 22 | PORT_FLOW_TABLE=0 |
| 23 | VLAN_FLOW_TABLE=10 |
| 24 | TERMINATION_FLOW_TABLE=20 |
| 25 | UCAST_ROUTING_FLOW_TABLE=30 |
| 26 | MCAST_ROUTING_FLOW_TABLE=40 |
| 27 | BRIDGE_FLOW_TABLE=50 |
| 28 | ACL_FLOW_TABLE=60 |
| 29 | |
| 30 | def convertIP4toStr(ip_addr): |
| 31 | a=(ip_addr&0xff000000)>>24 |
| 32 | b=(ip_addr&0x00ff0000)>>16 |
| 33 | c=(ip_addr&0x0000ff00)>>8 |
| 34 | d=(ip_addr&0x000000ff) |
| 35 | return str(a)+"."+str(b)+"."+str(c)+"."+str(d) |
| 36 | |
| 37 | def convertMACtoStr(mac): |
| 38 | if not isinstance(mac, list): |
| 39 | assert(0) |
| 40 | |
| 41 | return ':'.join(['%02X' % x for x in mac]) |
| 42 | |
macauley | 7f89d96 | 2015-08-06 18:13:48 +0800 | [diff] [blame] | 43 | def getSwitchCpuMACFromDPID(dpid): |
| 44 | str_datapath_id_f= "{:016x}".format(dpid) |
| 45 | str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)]) |
| 46 | switch_cpu_mac_str=str_datapath_id[6:] |
| 47 | switch_cpu_mac = switch_cpu_mac_str.split(":") |
| 48 | switch_cpu_mac=[int(switch_cpu_mac[i],16) for i in range(0, len(switch_cpu_mac))] |
| 49 | |
| 50 | return switch_cpu_mac_str, switch_cpu_mac |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 51 | |
| 52 | def DumpGroup(stats, verify_group_stats, always_show=True): |
| 53 | if(len(stats) > len(verify_group_stats)): |
| 54 | min_len = len(verify_group_stats) |
| 55 | print "Stats Len is not the same, stats>verify_group_stats" |
| 56 | if(len(stats)< len(verify_group_stats)): |
| 57 | min_len = len(stats) |
| 58 | print "Stats Len is not the same, stats<verify_group_stats" |
| 59 | else: |
| 60 | min_len = len(stats) |
macauley | e8b140e | 2015-08-03 13:35:45 +0800 | [diff] [blame] | 61 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 62 | print "\r\n" |
| 63 | for i in range(min_len): |
| 64 | gs = stats[i] |
| 65 | gv = verify_group_stats[i] |
| 66 | print "FromSwtich:(GID=%lx, TYPE=%lx)\r\nVerify :(GID=%lx, TYPE=%lx)"%(gs.group_id, gs.group_type, gv.group_id, gv.group_type) |
| 67 | if(len(gs.buckets) != len(gv.buckets)): |
| 68 | print "buckets len is not the same gs %lx, gv %lx",(len(gs.buckets), len(gv.buckets)) |
| 69 | |
| 70 | for j in range(len(gs.buckets)): |
| 71 | b1=gs.buckets[j] |
| 72 | b2=gv.buckets[j] |
| 73 | if(len(b1.actions) != len(b2.actions)): |
| 74 | print "action len is not the same" |
| 75 | |
| 76 | for k in range(len(b1.actions)): |
| 77 | a1=b1.actions[k] |
| 78 | a2=b2.actions[k] |
| 79 | if(always_show == True): |
| 80 | print "a1:"+a1.show() |
| 81 | print "a2:"+a2.show() |
| 82 | |
| 83 | def AssertGroup(self, stats, verify_group_stats): |
| 84 | self.assertTrue(len(stats) ==len(verify_group_stats), "stats len is not the same") |
| 85 | |
| 86 | for i in range(len(stats)): |
| 87 | gs = stats[i] |
| 88 | gv = verify_group_stats[i] |
| 89 | self.assertTrue(len(gs.buckets) == len(gv.buckets), "buckets len is not the same") |
| 90 | |
| 91 | for j in range(len(gs.buckets)): |
| 92 | b1=gs.buckets[j] |
| 93 | b2=gv.buckets[j] |
| 94 | self.assertTrue(len(b1.actions) == len(b2.actions), "action len is not the same") |
| 95 | |
| 96 | for k in range(len(b1.actions)): |
| 97 | a1=b1.actions[k] |
| 98 | a2=b2.actions[k] |
| 99 | self.assertEquals(a1, a2, "action is not the same") |
| 100 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 101 | def encode_l2_interface_group_id(vlan, id): |
| 102 | return id + (vlan << OFDPA_VLAN_ID_SHIFT) |
| 103 | |
| 104 | def encode_l2_rewrite_group_id(id): |
| 105 | return id + (1 << OFDPA_GROUP_TYPE_SHIFT) |
| 106 | |
| 107 | def encode_l3_unicast_group_id(id): |
| 108 | return id + (2 << OFDPA_GROUP_TYPE_SHIFT) |
| 109 | |
| 110 | def encode_l2_mcast_group_id(vlan, id): |
| 111 | return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (3 << OFDPA_GROUP_TYPE_SHIFT) |
| 112 | |
| 113 | def encode_l2_flood_group_id(vlan, id): |
| 114 | return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (4 << OFDPA_GROUP_TYPE_SHIFT) |
| 115 | |
| 116 | def encode_l3_interface_group_id(id): |
| 117 | return id + (5 << OFDPA_GROUP_TYPE_SHIFT) |
| 118 | |
| 119 | def encode_l3_mcast_group_id(vlan, id): |
| 120 | return id + (vlan << OFDPA_VLAN_ID_SHIFT)+(6 << OFDPA_GROUP_TYPE_SHIFT) |
| 121 | |
| 122 | def encode_l3_ecmp_group_id(id): |
| 123 | return id + (7 << OFDPA_GROUP_TYPE_SHIFT) |
| 124 | |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 125 | def encode_l2_overlay_group_id(tunnel_id, subtype, index): |
| 126 | tunnel_id=tunnel_id&0xffff #16 bits |
| 127 | subtype = subtype&3 #2 bits |
| 128 | index = index & 0x3f #10 bits |
| 129 | return index + (tunnel_id << OFDPA_TUNNEL_ID_SHIFT)+ (subtype<<OFDPA_TUNNEL_SUBTYPE_SHIFT)+(8 << OFDPA_GROUP_TYPE_SHIFT) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 130 | |
Flavio Castro | d4c44d1 | 2015-12-08 14:44:18 -0500 | [diff] [blame] | 131 | def add_l2_interface_group(ctrl, ports, vlan_id=1, is_tagged=False, send_barrier=False): |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 132 | # group table |
| 133 | # set up untag groups for each port |
macauley | 41904ed | 2015-07-16 17:38:35 +0800 | [diff] [blame] | 134 | group_id_list=[] |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 135 | msgs=[] |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 136 | for of_port in ports: |
| 137 | # do stuff |
| 138 | group_id = encode_l2_interface_group_id(vlan_id, of_port) |
macauley | 41904ed | 2015-07-16 17:38:35 +0800 | [diff] [blame] | 139 | group_id_list.append(group_id) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 140 | if is_tagged: |
| 141 | actions = [ |
| 142 | ofp.action.output(of_port), |
| 143 | ] |
| 144 | else: |
| 145 | actions = [ |
| 146 | ofp.action.pop_vlan(), |
| 147 | ofp.action.output(of_port), |
| 148 | ] |
| 149 | |
| 150 | buckets = [ |
| 151 | ofp.bucket(actions=actions), |
| 152 | ] |
| 153 | |
| 154 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 155 | group_id=group_id, |
| 156 | buckets=buckets |
| 157 | ) |
| 158 | ctrl.message_send(request) |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 159 | msgs.append(request) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 160 | |
| 161 | if send_barrier: |
| 162 | do_barrier(ctrl) |
macauley | 41904ed | 2015-07-16 17:38:35 +0800 | [diff] [blame] | 163 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 164 | return group_id_list, msgs |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 165 | |
Flavio Castro | d4c44d1 | 2015-12-08 14:44:18 -0500 | [diff] [blame] | 166 | def add_one_l2_interface_group(ctrl, port, vlan_id=1, is_tagged=False, send_barrier=False): |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 167 | # group table |
| 168 | # set up untag groups for each port |
| 169 | group_id = encode_l2_interface_group_id(vlan_id, port) |
| 170 | |
| 171 | if is_tagged: |
| 172 | actions = [ |
| 173 | ofp.action.output(port), |
| 174 | ] |
| 175 | else: |
| 176 | actions = [ |
| 177 | ofp.action.pop_vlan(), |
| 178 | ofp.action.output(port), |
| 179 | ] |
| 180 | |
| 181 | buckets = [ |
| 182 | ofp.bucket(actions=actions), |
| 183 | ] |
| 184 | |
| 185 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 186 | group_id=group_id, |
| 187 | buckets=buckets |
| 188 | ) |
| 189 | ctrl.message_send(request) |
| 190 | |
| 191 | if send_barrier: |
| 192 | do_barrier(ctrl) |
| 193 | |
| 194 | return group_id, request |
| 195 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 196 | def add_l2_mcast_group(ctrl, ports, vlanid, mcast_grp_index): |
| 197 | buckets=[] |
| 198 | for of_port in ports: |
| 199 | group_id = encode_l2_interface_group_id(vlanid, of_port) |
| 200 | action=[ofp.action.group(group_id)] |
| 201 | buckets.append(ofp.bucket(actions=action)) |
| 202 | |
| 203 | group_id =encode_l2_mcast_group_id(vlanid, mcast_grp_index) |
| 204 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 205 | group_id=group_id, |
| 206 | buckets=buckets |
| 207 | ) |
| 208 | ctrl.message_send(request) |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 209 | return request |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 210 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 211 | def add_l2_flood_group(ctrl, ports, vlanid, id): |
| 212 | buckets=[] |
| 213 | for of_port in ports: |
| 214 | group_id = encode_l2_interface_group_id(vlanid, of_port) |
| 215 | action=[ofp.action.group(group_id)] |
| 216 | buckets.append(ofp.bucket(actions=action)) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 217 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 218 | group_id =encode_l2_flood_group_id(vlanid, id) |
| 219 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 220 | group_id=group_id, |
| 221 | buckets=buckets |
| 222 | ) |
| 223 | ctrl.message_send(request) |
| 224 | return request |
| 225 | |
| 226 | def add_l2_rewrite_group(ctrl, port, vlanid, id, src_mac, dst_mac): |
| 227 | group_id = encode_l2_interface_group_id(vlanid, port) |
| 228 | |
| 229 | action=[] |
| 230 | if src_mac is not None: |
| 231 | action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac))) |
| 232 | |
| 233 | if dst_mac is not None: |
| 234 | action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac))) |
| 235 | |
macauley_cheng | 93f3fa5 | 2015-09-02 17:57:31 +0800 | [diff] [blame] | 236 | action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vlanid))) |
| 237 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 238 | action.append(ofp.action.group(group_id)) |
| 239 | |
| 240 | buckets = [ofp.bucket(actions=action)] |
| 241 | |
| 242 | group_id =encode_l2_rewrite_group_id(id) |
| 243 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 244 | group_id=group_id, |
| 245 | buckets=buckets |
| 246 | ) |
| 247 | ctrl.message_send(request) |
| 248 | return request |
| 249 | |
| 250 | def add_l3_unicast_group(ctrl, port, vlanid, id, src_mac, dst_mac): |
| 251 | group_id = encode_l2_interface_group_id(vlanid, port) |
| 252 | |
| 253 | action=[] |
| 254 | if src_mac is not None: |
| 255 | action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac))) |
| 256 | |
| 257 | if dst_mac is not None: |
| 258 | action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac))) |
| 259 | |
| 260 | action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vlanid))) |
| 261 | |
| 262 | action.append(ofp.action.group(group_id)) |
| 263 | |
| 264 | buckets = [ofp.bucket(actions=action)] |
| 265 | |
| 266 | group_id =encode_l3_unicast_group_id(id) |
| 267 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 268 | group_id=group_id, |
| 269 | buckets=buckets |
| 270 | ) |
| 271 | ctrl.message_send(request) |
| 272 | return request |
| 273 | |
| 274 | def add_l3_interface_group(ctrl, port, vlanid, id, src_mac): |
| 275 | group_id = encode_l2_interface_group_id(vlanid, port) |
| 276 | |
| 277 | action=[] |
| 278 | action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac))) |
| 279 | action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vlanid))) |
| 280 | action.append(ofp.action.group(group_id)) |
| 281 | |
| 282 | buckets = [ofp.bucket(actions=action)] |
| 283 | |
| 284 | group_id =encode_l3_interface_group_id(id) |
| 285 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 286 | group_id=group_id, |
| 287 | buckets=buckets |
| 288 | ) |
| 289 | ctrl.message_send(request) |
| 290 | return request |
| 291 | |
| 292 | def add_l3_ecmp_group(ctrl, id, l3_ucast_groups): |
| 293 | buckets=[] |
| 294 | for group in l3_ucast_groups: |
| 295 | buckets.append(ofp.bucket(actions=[ofp.action.group(group)])) |
| 296 | |
| 297 | group_id =encode_l3_ecmp_group_id(id) |
| 298 | request = ofp.message.group_add(group_type=ofp.OFPGT_SELECT, |
| 299 | group_id=group_id, |
| 300 | buckets=buckets |
| 301 | ) |
| 302 | ctrl.message_send(request) |
| 303 | return request |
| 304 | |
| 305 | def add_l3_mcast_group(ctrl, vid, mcast_group_id, groups_on_buckets): |
| 306 | buckets=[] |
| 307 | for group in groups_on_buckets: |
| 308 | buckets.append(ofp.bucket(actions=[ofp.action.group(group)])) |
| 309 | |
| 310 | group_id =encode_l3_mcast_group_id(vid, mcast_group_id) |
| 311 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 312 | group_id=group_id, |
| 313 | buckets=buckets |
| 314 | ) |
| 315 | ctrl.message_send(request) |
| 316 | return request |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 317 | |
| 318 | def add_l2_overlay_flood_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index): |
| 319 | buckets=[] |
| 320 | for port in ports: |
| 321 | buckets.append(ofp.bucket(actions=[ofp.action.output(port)])) |
| 322 | |
| 323 | group_id=encode_l2_overlay_group_id(tunnel_id, 0, index) |
| 324 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 325 | group_id=group_id, |
| 326 | buckets=buckets |
| 327 | ) |
| 328 | ctrl.message_send(request) |
| 329 | return request |
| 330 | |
| 331 | def add_l2_overlay_flood_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index): |
| 332 | buckets=[] |
| 333 | for port in ports: |
| 334 | buckets.append(ofp.bucket(actions=[ofp.action.output(port)])) |
| 335 | |
| 336 | group_id=encode_l2_overlay_group_id(tunnel_id, 1, index) |
| 337 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 338 | group_id=group_id, |
| 339 | buckets=buckets |
| 340 | ) |
| 341 | ctrl.message_send(request) |
| 342 | return request |
| 343 | |
| 344 | def add_l2_overlay_mcast_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index): |
| 345 | buckets=[] |
| 346 | for port in ports: |
| 347 | buckets.append(ofp.bucket(actions=[ofp.action.output(port)])) |
| 348 | |
| 349 | group_id=encode_l2_overlay_group_id(tunnel_id, 2, index) |
| 350 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 351 | group_id=group_id, |
| 352 | buckets=buckets |
| 353 | ) |
| 354 | ctrl.message_send(request) |
| 355 | return request |
| 356 | |
| 357 | def add_l2_overlay_mcast_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index): |
| 358 | buckets=[] |
| 359 | for port in ports: |
| 360 | buckets.append(ofp.bucket(actions=[ofp.action.output(port)])) |
| 361 | |
| 362 | group_id=encode_l2_overlay_group_id(tunnel_id, 3, index) |
| 363 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 364 | group_id=group_id, |
| 365 | buckets=buckets |
| 366 | ) |
| 367 | ctrl.message_send(request) |
| 368 | return request |
| 369 | |
| 370 | def add_port_table_flow(ctrl, is_overlay=True): |
| 371 | match = ofp.match() |
| 372 | |
| 373 | if is_overlay == True: |
| 374 | match.oxm_list.append(ofp.oxm.in_port(0x10000)) |
macauley | dbff327 | 2015-07-30 14:07:16 +0800 | [diff] [blame] | 375 | NEXT_TABLE=50 |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 376 | else: |
| 377 | match.oxm_list.append(ofp.oxm.in_port(0)) |
macauley | dbff327 | 2015-07-30 14:07:16 +0800 | [diff] [blame] | 378 | NEXT_TABLE=10 |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 379 | |
| 380 | request = ofp.message.flow_add( |
| 381 | table_id=0, |
| 382 | cookie=42, |
| 383 | match=match, |
| 384 | instructions=[ |
macauley | dbff327 | 2015-07-30 14:07:16 +0800 | [diff] [blame] | 385 | ofp.instruction.goto_table(NEXT_TABLE) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 386 | ], |
| 387 | priority=0) |
| 388 | logging.info("Add port table, match port %lx" % 0x10000) |
| 389 | ctrl.message_send(request) |
macauley | dbff327 | 2015-07-30 14:07:16 +0800 | [diff] [blame] | 390 | |
Flavio Castro | d8f8af2 | 2015-12-02 18:19:26 -0500 | [diff] [blame] | 391 | |
| 392 | def pop_vlan_flow(ctrl, ports, vlan_id=1): |
| 393 | # table 10: vlan |
| 394 | # goto to table 20 |
| 395 | msgs=[] |
| 396 | for of_port in ports: |
| 397 | match = ofp.match() |
| 398 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 399 | match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id)) |
| 400 | request = ofp.message.flow_add( |
| 401 | table_id=10, |
| 402 | cookie=42, |
| 403 | match=match, |
| 404 | instructions=[ |
| 405 | ofp.instruction.apply_actions( |
| 406 | actions=[ |
| 407 | ofp.action.pop_vlan() |
| 408 | ] |
| 409 | ), |
| 410 | ofp.instruction.goto_table(20) |
| 411 | ], |
| 412 | priority=0) |
| 413 | logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port)) |
| 414 | ctrl.message_send(request) |
| 415 | |
| 416 | |
| 417 | return msgs |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 418 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 419 | def add_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False): |
| 420 | # table 10: vlan |
| 421 | # goto to table 20 |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 422 | msgs=[] |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 423 | for of_port in ports: |
| 424 | if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH): |
| 425 | match = ofp.match() |
| 426 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 427 | match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id)) |
| 428 | request = ofp.message.flow_add( |
| 429 | table_id=10, |
| 430 | cookie=42, |
| 431 | match=match, |
| 432 | instructions=[ |
Flavio Castro | d8f8af2 | 2015-12-02 18:19:26 -0500 | [diff] [blame] | 433 | ofp.instruction.apply_actions( |
| 434 | actions=[ |
| 435 | ofp.action.pop_vlan() |
| 436 | ] |
| 437 | ), |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 438 | ofp.instruction.goto_table(20) |
| 439 | ], |
| 440 | priority=0) |
| 441 | logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port)) |
| 442 | ctrl.message_send(request) |
| 443 | |
| 444 | if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH): |
| 445 | match = ofp.match() |
| 446 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
Flavio Castro | 1229631 | 2015-12-15 17:48:26 -0500 | [diff] [blame^] | 447 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff)) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 448 | request = ofp.message.flow_add( |
| 449 | table_id=10, |
| 450 | cookie=42, |
| 451 | match=match, |
| 452 | instructions=[ |
| 453 | ofp.instruction.apply_actions( |
| 454 | actions=[ |
Flavio Castro | 1229631 | 2015-12-15 17:48:26 -0500 | [diff] [blame^] | 455 | ofp.action.set_field(ofp.oxm.vlan_vid(vlan_id)) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 456 | ] |
| 457 | ), |
| 458 | ofp.instruction.goto_table(20) |
| 459 | ], |
| 460 | priority=0) |
| 461 | logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port)) |
| 462 | ctrl.message_send(request) |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 463 | msgs.append(request) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 464 | |
| 465 | if send_barrier: |
| 466 | do_barrier(ctrl) |
| 467 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 468 | return msgs |
macauley_cheng | 6e6a612 | 2015-11-16 14:19:18 +0800 | [diff] [blame] | 469 | |
| 470 | def del_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False): |
| 471 | # table 10: vlan |
| 472 | # goto to table 20 |
| 473 | msgs=[] |
| 474 | for of_port in ports: |
| 475 | if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH): |
| 476 | match = ofp.match() |
| 477 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 478 | match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id)) |
| 479 | request = ofp.message.flow_delete( |
| 480 | table_id=10, |
| 481 | cookie=42, |
| 482 | match=match, |
| 483 | priority=0) |
| 484 | logging.info("Del vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port)) |
| 485 | ctrl.message_send(request) |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 486 | |
macauley_cheng | 6e6a612 | 2015-11-16 14:19:18 +0800 | [diff] [blame] | 487 | if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH): |
| 488 | match = ofp.match() |
| 489 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 490 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0xfff)) |
| 491 | request = ofp.message.flow_delete( |
| 492 | table_id=10, |
| 493 | cookie=42, |
| 494 | match=match, |
| 495 | priority=0) |
| 496 | logging.info("Del vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port)) |
| 497 | ctrl.message_send(request) |
| 498 | msgs.append(request) |
| 499 | |
| 500 | if send_barrier: |
| 501 | do_barrier(ctrl) |
| 502 | |
| 503 | return msgs |
| 504 | |
macauley_cheng | 6b31161 | 2015-09-04 11:32:27 +0800 | [diff] [blame] | 505 | def add_vlan_table_flow_pvid(ctrl, in_port, match_vid=None, pvid=1, send_barrier=False): |
| 506 | """it will tag pack as untagged packet wether it has tagg or not""" |
| 507 | match = ofp.match() |
| 508 | match.oxm_list.append(ofp.oxm.in_port(in_port)) |
| 509 | actions=[] |
| 510 | if match_vid == None: |
| 511 | match.oxm_list.append(ofp.oxm.vlan_vid(0)) |
| 512 | actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid))) |
| 513 | goto_table=20 |
| 514 | else: |
| 515 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+match_vid, 0x1fff)) |
| 516 | actions.append(ofp.action.push_vlan(0x8100)) |
| 517 | actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid))) |
| 518 | goto_table=20 |
| 519 | |
| 520 | request = ofp.message.flow_add( |
| 521 | table_id=10, |
| 522 | cookie=42, |
| 523 | match=match, |
| 524 | instructions=[ |
| 525 | ofp.instruction.apply_actions(actions=actions) |
| 526 | ,ofp.instruction.goto_table(goto_table) |
| 527 | ], |
| 528 | priority=0) |
| 529 | logging.info("Add PVID %d on port %d and go to table %ld" %( pvid, in_port, goto_table)) |
| 530 | ctrl.message_send(request) |
| 531 | |
| 532 | if send_barrier: |
| 533 | do_barrier(ctrl) |
| 534 | |
| 535 | def add_vlan_table_flow_allow_all_vlan(ctrl, in_port, send_barrier=False): |
| 536 | """it st flow allow all vlan tag on this port""" |
| 537 | match = ofp.match() |
| 538 | match.oxm_list.append(ofp.oxm.in_port(in_port)) |
| 539 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1000)) |
| 540 | request = ofp.message.flow_add( |
| 541 | table_id=10, |
| 542 | cookie=42, |
| 543 | match=match, |
| 544 | instructions=[ |
| 545 | ofp.instruction.goto_table(20) |
| 546 | ], |
| 547 | priority=0) |
| 548 | logging.info("Add allow all vlan on port %d " %(in_port)) |
| 549 | ctrl.message_send(request) |
| 550 | |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 551 | def add_one_vlan_table_flow(ctrl, of_port, vlan_id=1, vrf=0, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False): |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 552 | # table 10: vlan |
| 553 | # goto to table 20 |
| 554 | if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH): |
| 555 | match = ofp.match() |
| 556 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
Flavio Castro | 1229631 | 2015-12-15 17:48:26 -0500 | [diff] [blame^] | 557 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff)) |
macauley | 7f89d96 | 2015-08-06 18:13:48 +0800 | [diff] [blame] | 558 | |
| 559 | actions=[] |
| 560 | if vrf!=0: |
| 561 | actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))) |
| 562 | |
Flavio Castro | 6d49852 | 2015-12-15 14:05:04 -0500 | [diff] [blame] | 563 | #actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id))) |
macauley | 7f89d96 | 2015-08-06 18:13:48 +0800 | [diff] [blame] | 564 | |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 565 | request = ofp.message.flow_add( |
| 566 | table_id=10, |
| 567 | cookie=42, |
| 568 | match=match, |
| 569 | instructions=[ |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 570 | ofp.instruction.apply_actions( |
macauley | 7f89d96 | 2015-08-06 18:13:48 +0800 | [diff] [blame] | 571 | actions=actions |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 572 | ), |
| 573 | ofp.instruction.goto_table(20) |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 574 | ], |
| 575 | priority=0) |
| 576 | logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port)) |
| 577 | ctrl.message_send(request) |
| 578 | |
| 579 | if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH): |
| 580 | match = ofp.match() |
| 581 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
Flavio Castro | 1229631 | 2015-12-15 17:48:26 -0500 | [diff] [blame^] | 582 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff)) |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 583 | |
macauley | 7f89d96 | 2015-08-06 18:13:48 +0800 | [diff] [blame] | 584 | actions=[] |
| 585 | if vrf!=0: |
| 586 | actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))) |
| 587 | |
Flavio Castro | 1229631 | 2015-12-15 17:48:26 -0500 | [diff] [blame^] | 588 | actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(vlan_id))) |
macauley | 7f89d96 | 2015-08-06 18:13:48 +0800 | [diff] [blame] | 589 | |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 590 | request = ofp.message.flow_add( |
| 591 | table_id=10, |
| 592 | cookie=42, |
| 593 | match=match, |
| 594 | instructions=[ |
| 595 | ofp.instruction.apply_actions( |
macauley | 7f89d96 | 2015-08-06 18:13:48 +0800 | [diff] [blame] | 596 | actions=actions |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 597 | ), |
| 598 | ofp.instruction.goto_table(20) |
| 599 | ], |
| 600 | priority=0) |
| 601 | logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port)) |
| 602 | ctrl.message_send(request) |
| 603 | |
| 604 | if send_barrier: |
| 605 | do_barrier(ctrl) |
| 606 | |
| 607 | return request |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 608 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 609 | def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False): |
| 610 | match = ofp.match() |
castroflavio | 2189448 | 2015-12-08 15:29:55 -0500 | [diff] [blame] | 611 | priority=500 |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 612 | if dst_mac!=None: |
castroflavio | 2189448 | 2015-12-08 15:29:55 -0500 | [diff] [blame] | 613 | priority=1000 |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 614 | match.oxm_list.append(ofp.oxm.eth_dst(dst_mac)) |
| 615 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 616 | match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid)) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 617 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 618 | request = ofp.message.flow_add( |
| 619 | table_id=50, |
| 620 | cookie=42, |
| 621 | match=match, |
| 622 | instructions=[ |
| 623 | ofp.instruction.write_actions( |
| 624 | actions=[ |
| 625 | ofp.action.group(group_id)]), |
| 626 | ofp.instruction.goto_table(60) |
| 627 | ], |
| 628 | buffer_id=ofp.OFP_NO_BUFFER, |
castroflavio | 2189448 | 2015-12-08 15:29:55 -0500 | [diff] [blame] | 629 | priority=priority) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 630 | |
| 631 | logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac) |
| 632 | ctrl.message_send(request) |
| 633 | |
| 634 | if send_barrier: |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 635 | do_barrier(ctrl) |
| 636 | |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 637 | return request |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 638 | |
| 639 | def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False): |
| 640 | match = ofp.match() |
| 641 | if dst_mac!=None: |
| 642 | match.oxm_list.append(ofp.oxm.eth_dst(dst_mac)) |
| 643 | |
| 644 | match.oxm_list.append(ofp.oxm.tunnel_id(vnid)) |
| 645 | if is_group == True: |
| 646 | actions=[ofp.action.group(group_id)] |
| 647 | else: |
| 648 | actions=[ofp.action.output(group_id)] |
| 649 | |
| 650 | request = ofp.message.flow_add( |
| 651 | table_id=50, |
| 652 | cookie=42, |
| 653 | match=match, |
| 654 | instructions=[ |
| 655 | ofp.instruction.write_actions( |
| 656 | actions=actions), |
| 657 | ofp.instruction.goto_table(60) |
| 658 | ], |
| 659 | buffer_id=ofp.OFP_NO_BUFFER, |
| 660 | priority=1000) |
| 661 | |
| 662 | logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac) |
| 663 | ctrl.message_send(request) |
| 664 | |
| 665 | if send_barrier: |
| 666 | do_barrier(ctrl) |
| 667 | |
| 668 | return request |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 669 | |
macauley_cheng | 6b13366 | 2015-11-09 13:52:39 +0800 | [diff] [blame] | 670 | def add_termination_flow(ctrl, in_port, eth_type, dst_mac, vlanid, goto_table=None, send_barrier=False): |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 671 | match = ofp.match() |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 672 | match.oxm_list.append(ofp.oxm.eth_type(eth_type)) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 673 | if dst_mac[0]&0x01 == 0x01: |
| 674 | match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00])) |
| 675 | goto_table=40 |
| 676 | else: |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 677 | if in_port!=0: |
| 678 | match.oxm_list.append(ofp.oxm.in_port(in_port)) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 679 | match.oxm_list.append(ofp.oxm.eth_dst(dst_mac)) |
| 680 | match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid)) |
macauley_cheng | 6b13366 | 2015-11-09 13:52:39 +0800 | [diff] [blame] | 681 | if goto_table == None: |
| 682 | goto_table=30 |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 683 | |
| 684 | request = ofp.message.flow_add( |
| 685 | table_id=20, |
| 686 | cookie=42, |
| 687 | match=match, |
| 688 | instructions=[ |
| 689 | ofp.instruction.goto_table(goto_table) |
| 690 | ], |
| 691 | buffer_id=ofp.OFP_NO_BUFFER, |
| 692 | priority=1) |
| 693 | |
| 694 | logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac) |
| 695 | ctrl.message_send(request) |
| 696 | |
| 697 | if send_barrier: |
| 698 | do_barrier(ctrl) |
| 699 | |
| 700 | return request |
| 701 | |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 702 | def add_unicast_routing_flow(ctrl, eth_type, dst_ip, mask, action_group_id, vrf=0, send_barrier=False): |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 703 | match = ofp.match() |
| 704 | match.oxm_list.append(ofp.oxm.eth_type(eth_type)) |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 705 | if vrf != 0: |
| 706 | match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf)) |
| 707 | |
macauley | f8b1acd | 2015-07-23 15:13:13 +0800 | [diff] [blame] | 708 | if mask!=0: |
| 709 | match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask)) |
| 710 | else: |
| 711 | match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip)) |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 712 | |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 713 | |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 714 | request = ofp.message.flow_add( |
| 715 | table_id=30, |
| 716 | cookie=42, |
| 717 | match=match, |
| 718 | instructions=[ |
| 719 | ofp.instruction.write_actions( |
| 720 | actions=[ofp.action.group(action_group_id)]), |
| 721 | ofp.instruction.goto_table(60) |
| 722 | ], |
| 723 | buffer_id=ofp.OFP_NO_BUFFER, |
| 724 | priority=1) |
| 725 | |
| 726 | logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip) |
| 727 | ctrl.message_send(request) |
| 728 | |
| 729 | if send_barrier: |
| 730 | do_barrier(ctrl) |
| 731 | |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 732 | return request |
Flavio Castro | d8f8af2 | 2015-12-02 18:19:26 -0500 | [diff] [blame] | 733 | |
Flavio Castro | 167f5bd | 2015-12-02 19:33:53 -0500 | [diff] [blame] | 734 | def add_mpls_flow(ctrl, action_group_id, label=100 ,ethertype=0x0800, bos=1, send_barrier=False): |
Flavio Castro | d8f8af2 | 2015-12-02 18:19:26 -0500 | [diff] [blame] | 735 | match = ofp.match() |
| 736 | match.oxm_list.append(ofp.oxm.eth_type(0x8847)) |
| 737 | match.oxm_list.append(ofp.oxm.mpls_label(label)) |
| 738 | match.oxm_list.append(ofp.oxm.mpls_bos(bos)) |
| 739 | actions = [ofp.action.dec_mpls_ttl(), |
| 740 | ofp.action.copy_ttl_in(), |
| 741 | ofp.action.pop_mpls(ethertype)] |
| 742 | request = ofp.message.flow_add( |
| 743 | table_id=24, |
| 744 | cookie=43, |
| 745 | match=match, |
| 746 | instructions=[ |
| 747 | ofp.instruction.apply_actions( |
| 748 | actions=actions |
| 749 | ), |
| 750 | ofp.instruction.write_actions( |
| 751 | actions=[ofp.action.group(action_group_id)]), |
| 752 | ofp.instruction.goto_table(60) |
| 753 | ], |
| 754 | buffer_id=ofp.OFP_NO_BUFFER, |
| 755 | priority=1) |
| 756 | |
| 757 | logging.info("Inserting MPLS flow , label %ld", label) |
| 758 | ctrl.message_send(request) |
| 759 | |
| 760 | if send_barrier: |
| 761 | do_barrier(ctrl) |
| 762 | |
| 763 | return request |
| 764 | |
| 765 | |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 766 | def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False): |
| 767 | match = ofp.match() |
| 768 | match.oxm_list.append(ofp.oxm.eth_type(0x0800)) |
| 769 | match.oxm_list.append(ofp.oxm.vlan_vid(vlan_id)) |
| 770 | if src_ip_mask!=0: |
| 771 | match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask)) |
| 772 | else: |
| 773 | match.oxm_list.append(ofp.oxm.ipv4_src(src_ip)) |
| 774 | |
| 775 | match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip)) |
| 776 | |
| 777 | request = ofp.message.flow_add( |
| 778 | table_id=40, |
| 779 | cookie=42, |
| 780 | match=match, |
| 781 | instructions=[ |
| 782 | ofp.instruction.write_actions( |
| 783 | actions=[ofp.action.group(action_group_id)]), |
| 784 | ofp.instruction.goto_table(60) |
| 785 | ], |
| 786 | buffer_id=ofp.OFP_NO_BUFFER, |
| 787 | priority=1) |
| 788 | |
| 789 | logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask) |
| 790 | ctrl.message_send(request) |
| 791 | |
| 792 | if send_barrier: |
| 793 | do_barrier(ctrl) |
| 794 | |
| 795 | return request |
macauley_cheng | 6b13366 | 2015-11-09 13:52:39 +0800 | [diff] [blame] | 796 | |
| 797 | #dpctl tcp:192.168.1.1:6633 flow-mod table=28,cmd=add,prio=281 eth_type=0x800,ip_dst=100.0.0.1,ip_proto=6,tcp_dst=5000 write:set_field=ip_dst:10.0.0.1,set_field=tcp_dst:2000,group=0x71000001 goto:60 |
macauley_cheng | effc20a | 2015-11-09 16:14:56 +0800 | [diff] [blame] | 798 | def add_dnat_flow(ctrl, eth_type, ip_dst, ip_proto, tcp_dst, set_ip_dst, set_tcp_dst, action_group_id): |
macauley_cheng | 6b13366 | 2015-11-09 13:52:39 +0800 | [diff] [blame] | 799 | match = ofp.match() |
| 800 | match.oxm_list.append(ofp.oxm.eth_type(eth_type)) |
| 801 | match.oxm_list.append(ofp.oxm.ipv4_dst(ip_dst)) |
| 802 | match.oxm_list.append(ofp.oxm.ip_proto(ip_proto)) |
| 803 | match.oxm_list.append(ofp.oxm.tcp_dst(tcp_dst)) |
| 804 | |
| 805 | request = ofp.message.flow_add( |
| 806 | table_id=28, |
| 807 | cookie=42, |
| 808 | match=match, |
| 809 | instructions=[ |
| 810 | ofp.instruction.write_actions( |
| 811 | actions=[ofp.action.set_field(ofp.oxm.ipv4_dst(set_ip_dst)), |
| 812 | ofp.action.set_field(ofp.oxm.tcp_dst(set_tcp_dst)), |
| 813 | ofp.action.group(action_group_id)]), |
| 814 | ofp.instruction.goto_table(60) |
| 815 | ], |
| 816 | buffer_id=ofp.OFP_NO_BUFFER, |
| 817 | priority=1) |
| 818 | logging.info("Inserting DNAT flow eth_type %lx, dip %lx, ip_proto %ld, tcp_dst %ld, SetFeild: Dip %lx, tcp_dst %ld, action_gorup=%lx",eth_type, ip_dst, ip_proto, tcp_dst, set_ip_dst, set_tcp_dst, action_group_id) |
| 819 | ctrl.message_send(request) |
| 820 | return request |
macauley_cheng | effc20a | 2015-11-09 16:14:56 +0800 | [diff] [blame] | 821 | |
| 822 | #dpctl tcp:192.168.1.1:6633 flow-mod table=29,cmd=add,prio=291 eth_type=0x800,ip_src=10.0.0.1,ip_proto=6,tcp_src=2000 write:set_field=ip_src:100.0.0.1,set_field=tcp_src:5000 goto:30 |
| 823 | def add_snat_flow(ctrl, eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src): |
| 824 | match = ofp.match() |
| 825 | match.oxm_list.append(ofp.oxm.eth_type(eth_type)) |
| 826 | match.oxm_list.append(ofp.oxm.ipv4_src(ip_src)) |
| 827 | match.oxm_list.append(ofp.oxm.ip_proto(ip_proto)) |
| 828 | match.oxm_list.append(ofp.oxm.tcp_src(tcp_src)) |
| 829 | |
| 830 | request = ofp.message.flow_add( |
| 831 | table_id=29, |
| 832 | cookie=42, |
| 833 | match=match, |
| 834 | instructions=[ |
| 835 | ofp.instruction.write_actions( |
| 836 | actions=[ofp.action.set_field(ofp.oxm.ipv4_src(set_ip_src)), |
| 837 | ofp.action.set_field(ofp.oxm.tcp_src(set_tcp_src))]), |
| 838 | ofp.instruction.goto_table(30) |
| 839 | ], |
| 840 | buffer_id=ofp.OFP_NO_BUFFER, |
| 841 | priority=1) |
| 842 | logging.info("Inserting DNAT flow eth_type %lx, sip %lx, ip_proto %ld, tcp_src %ld, SetFeild: sip %lx, tcp_src %ld",eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src) |
| 843 | ctrl.message_send(request) |
| 844 | return request |
macauley_cheng | 6b13366 | 2015-11-09 13:52:39 +0800 | [diff] [blame] | 845 | |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 846 | def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'): |
| 847 | """ |
| 848 | Command Example: |
| 849 | of-agent vtap 10001 ethernet 1/1 vid 1 |
| 850 | of-agent vtp 10001 vni 10 |
| 851 | """ |
| 852 | if vlan != 0: |
| 853 | config_vtap_xml=""" |
| 854 | <config> |
| 855 | <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 856 | <id>capable-switch-1</id> |
| 857 | <resources> |
| 858 | <port xc:operation="OPERATION"> |
| 859 | <resource-id >LPORT</resource-id> |
| 860 | <features> |
| 861 | <current> |
| 862 | <rate>10Gb</rate> |
| 863 | <medium>fiber</medium> |
| 864 | <pause>symmetric</pause> |
| 865 | </current> |
| 866 | <advertised> |
| 867 | <rate>10Gb</rate> |
| 868 | <rate>100Gb</rate> |
| 869 | <medium>fiber</medium> |
| 870 | <pause>symmetric</pause> |
| 871 | </advertised> |
| 872 | <supported> |
| 873 | <rate>10Gb</rate> |
| 874 | <rate>100Gb</rate> |
| 875 | <medium>fiber</medium> |
| 876 | <pause>symmetric</pause> |
| 877 | </supported> |
| 878 | <advertised-peer> |
| 879 | <rate>10Gb</rate> |
| 880 | <rate>100Gb</rate> |
| 881 | <medium>fiber</medium> |
| 882 | <pause>symmetric</pause> |
| 883 | </advertised-peer> |
| 884 | </features> |
| 885 | <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 886 | <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port> |
| 887 | <ofdpa10:vid>VLAN_ID</ofdpa10:vid> |
| 888 | <ofdpa10:vni>VNID</ofdpa10:vni> |
| 889 | </ofdpa10:vtap> |
| 890 | </port> |
| 891 | </resources> |
| 892 | <logical-switches> |
| 893 | <switch> |
| 894 | <id>DATAPATH_ID</id> |
| 895 | <datapath-id>DATAPATH_ID</datapath-id> |
| 896 | <resources> |
| 897 | <port xc:operation="OPERATION">LPORT</port> |
| 898 | </resources> |
| 899 | </switch> |
| 900 | </logical-switches> |
| 901 | </capable-switch> |
| 902 | </config> |
| 903 | """ |
| 904 | else: |
| 905 | config_vtap_xml=""" |
| 906 | <config> |
| 907 | <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 908 | <id>capable-switch-1</id> |
| 909 | <resources> |
| 910 | <port xc:operation="OPERATION"> |
| 911 | <resource-id >LPORT</resource-id> |
| 912 | <features> |
| 913 | <current> |
| 914 | <rate>10Gb</rate> |
| 915 | <medium>fiber</medium> |
| 916 | <pause>symmetric</pause> |
| 917 | </current> |
| 918 | <advertised> |
| 919 | <rate>10Gb</rate> |
| 920 | <rate>100Gb</rate> |
| 921 | <medium>fiber</medium> |
| 922 | <pause>symmetric</pause> |
| 923 | </advertised> |
| 924 | <supported> |
| 925 | <rate>10Gb</rate> |
| 926 | <rate>100Gb</rate> |
| 927 | <medium>fiber</medium> |
| 928 | <pause>symmetric</pause> |
| 929 | </supported> |
| 930 | <advertised-peer> |
| 931 | <rate>10Gb</rate> |
| 932 | <rate>100Gb</rate> |
| 933 | <medium>fiber</medium> |
| 934 | <pause>symmetric</pause> |
| 935 | </advertised-peer> |
| 936 | </features> |
| 937 | <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 938 | <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port> |
| 939 | <ofdpa10:vni>VNID</ofdpa10:vni> |
| 940 | </ofdpa10:vtap> |
| 941 | </port> |
| 942 | </resources> |
| 943 | <logical-switches> |
| 944 | <switch> |
| 945 | <id>DATAPATH_ID</id> |
| 946 | <datapath-id>DATAPATH_ID</datapath-id> |
| 947 | <resources> |
| 948 | <port xc:operation="OPERATION">LPORT</port> |
| 949 | </resources> |
| 950 | </switch> |
| 951 | </logical-switches> |
| 952 | </capable-switch> |
| 953 | </config> |
| 954 | """ |
| 955 | str_datapath_id_f= "{:016x}".format(dp_id) |
| 956 | str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)]) |
| 957 | config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id) |
macauley | 25999cf | 2015-08-07 17:03:24 +0800 | [diff] [blame] | 958 | config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport))) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 959 | config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port)) |
| 960 | config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan)) |
| 961 | config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid)) |
| 962 | config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation)) |
| 963 | return config_vtap_xml |
| 964 | |
| 965 | def get_vtep_lport_config_xml(dp_id, lport, src_ip, dst_ip, next_hop_id, vnid, udp_src_port=6633, ttl=25, operation='merge'): |
| 966 | """ |
| 967 | Command Example: |
| 968 | of-agent vtep 10002 source user-input-src-ip destination user-input-dst-ip udp-source-port 6633 nexthop 2 ttl 25 |
| 969 | of-agent vtp 10001 vni 10 |
| 970 | """ |
| 971 | |
| 972 | config_vtep_xml=""" |
| 973 | <config> |
| 974 | <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 975 | <id>capable-switch-1</id> |
| 976 | <resources> |
| 977 | <port xc:operation="OPERATION"> |
| 978 | <resource-id>LPORT</resource-id> |
| 979 | <features> |
| 980 | <current> |
| 981 | <rate>10Gb</rate> |
| 982 | <medium>fiber</medium> |
| 983 | <pause>symmetric</pause> |
| 984 | </current> |
| 985 | <advertised> |
| 986 | <rate>10Gb</rate> |
| 987 | <rate>100Gb</rate> |
| 988 | <medium>fiber</medium> |
| 989 | <pause>symmetric</pause> |
| 990 | </advertised> |
| 991 | <supported> |
| 992 | <rate>10Gb</rate> |
| 993 | <rate>100Gb</rate> |
| 994 | <medium>fiber</medium> |
| 995 | <pause>symmetric</pause> |
| 996 | </supported> |
| 997 | <advertised-peer> |
| 998 | <rate>10Gb</rate> |
| 999 | <rate>100Gb</rate> |
| 1000 | <medium>fiber</medium> |
| 1001 | <pause>symmetric</pause> |
| 1002 | </advertised-peer> |
| 1003 | </features> |
| 1004 | <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01"> |
| 1005 | <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip> |
| 1006 | <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip> |
| 1007 | <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port> |
macauley | 25999cf | 2015-08-07 17:03:24 +0800 | [diff] [blame] | 1008 | <ofdpa10:vni xc:operation="OPERATION"> |
| 1009 | <ofdpa10:id>VNID</ofdpa10:id> |
| 1010 | </ofdpa10:vni> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1011 | <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id> |
| 1012 | <ofdpa10:ttl>TTL</ofdpa10:ttl> |
| 1013 | </ofdpa10:vtep> |
| 1014 | </port> |
| 1015 | </resources> |
| 1016 | <logical-switches> |
| 1017 | <switch> |
| 1018 | <id>DATAPATH_ID</id> |
| 1019 | <datapath-id>DATAPATH_ID</datapath-id> |
| 1020 | <resources> |
| 1021 | <port xc:operation="OPERATION">LPORT</port> |
| 1022 | </resources> |
| 1023 | </switch> |
| 1024 | </logical-switches> |
| 1025 | </capable-switch> |
| 1026 | </config> |
| 1027 | """ |
| 1028 | str_datapath_id_f= "{:016x}".format(dp_id) |
| 1029 | str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)]) |
| 1030 | config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id) |
macauley | 25999cf | 2015-08-07 17:03:24 +0800 | [diff] [blame] | 1031 | config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport))) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1032 | config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip)) |
| 1033 | config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip)) |
| 1034 | config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port)) |
| 1035 | config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id)) |
| 1036 | config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl)) |
| 1037 | config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid)) |
| 1038 | config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation)) |
| 1039 | |
| 1040 | return config_vtep_xml |
| 1041 | |
| 1042 | def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'): |
| 1043 | #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2 |
| 1044 | config_nexthop_xml=""" |
| 1045 | <config> |
| 1046 | <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 1047 | <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 1048 | <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id> |
| 1049 | <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac> |
| 1050 | <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port> |
| 1051 | <ofdpa10:vid>VLAN_ID</ofdpa10:vid> |
| 1052 | </ofdpa10:next-hop> |
| 1053 | </of11-config:capable-switch> |
| 1054 | </config> |
| 1055 | """ |
| 1056 | config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan)) |
| 1057 | config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port)) |
| 1058 | config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id)) |
| 1059 | config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac)) |
| 1060 | config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation)) |
| 1061 | return config_nexthop_xml |
| 1062 | |
| 1063 | def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'): |
| 1064 | #of-agent vni 10 multicast 224.1.1.1 nexthop 20 |
| 1065 | if mcast_ipv4!=None: |
| 1066 | config_vni_xml=""" |
| 1067 | <config> |
| 1068 | <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 1069 | <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 1070 | <ofdpa10:id>VNID</ofdpa10:id> |
| 1071 | <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group> |
| 1072 | <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id> |
| 1073 | </ofdpa10:vni> |
| 1074 | </of11-config:capable-switch> |
| 1075 | </config> |
| 1076 | """ |
| 1077 | config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id)) |
| 1078 | config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4)) |
| 1079 | else: |
| 1080 | config_vni_xml=""" |
| 1081 | <config> |
| 1082 | <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 1083 | <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 1084 | <ofdpa10:id>VNID</ofdpa10:id> |
| 1085 | </ofdpa10:vni> |
| 1086 | </of11-config:capable-switch> |
| 1087 | </config> |
| 1088 | """ |
| 1089 | |
| 1090 | config_vni_xml=config_vni_xml.replace("VNID", str(vni_id)) |
| 1091 | config_vni_xml=config_vni_xml.replace("OPERATION", str(operation)) |
| 1092 | return config_vni_xml |
| 1093 | |
| 1094 | def get_featureReplay(self): |
| 1095 | req = ofp.message.features_request() |
| 1096 | res, raw = self.controller.transact(req) |
| 1097 | self.assertIsNotNone(res, "Did not receive a response from the DUT.") |
| 1098 | self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY, |
| 1099 | ("Unexpected packet type %d received in response to " |
| 1100 | "OFPT_FEATURES_REQUEST") % res.type) |
| 1101 | return res |
| 1102 | |
| 1103 | def send_edit_config(switch_ip, xml, target='runing'): |
| 1104 | NETCONF_ACCOUNT="netconfuser" |
| 1105 | NETCONF_PASSWD="netconfuser" |
| 1106 | with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m: |
| 1107 | try: |
| 1108 | m.edit_config(target='running', |
| 1109 | config=xml, |
| 1110 | default_operation='merge', |
| 1111 | error_option='stop-on-error') |
| 1112 | |
| 1113 | except Exception as e: |
| 1114 | logging.info("Fail to set xml %s", xml) |
| 1115 | return False |
| 1116 | |
| 1117 | #return m.get_config(source='running').data_xml |
| 1118 | return True |
| 1119 | |
| 1120 | def send_delete_config(switch_ip, xml, target='runing'): |
| 1121 | NETCONF_ACCOUNT="netconfuser" |
| 1122 | NETCONF_PASSWD="netconfuser" |
| 1123 | with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m: |
| 1124 | try: |
| 1125 | m.edit_config(target='running', |
| 1126 | config=xml, |
| 1127 | default_operation='delete', |
| 1128 | error_option='stop-on-error') |
| 1129 | |
| 1130 | except Exception as e: |
| 1131 | logging.info("Fail to set xml %s", xml) |
| 1132 | return False |
| 1133 | |
| 1134 | #return m.get_config(source='running').data_xml |
| 1135 | return True |
| 1136 | |
| 1137 | def get_edit_config(switch_ip, target='runing'): |
| 1138 | NETCONF_ACCOUNT="netconfuser" |
| 1139 | NETCONF_PASSWD="netconfuser" |
| 1140 | with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m: |
macauley | 6f6ceb2 | 2015-08-07 09:37:12 +0800 | [diff] [blame] | 1141 | return m.get_config(source='running').data_xml |
macauley | dbff327 | 2015-07-30 14:07:16 +0800 | [diff] [blame] | 1142 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1143 | |
| 1144 | """ |
| 1145 | MPLS |
| 1146 | """ |
| 1147 | |
| 1148 | OFDPA_MPLS_SUBTYPE_SHIFT=24 |
| 1149 | OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1 |
| 1150 | OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2 |
| 1151 | OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3 |
| 1152 | OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4 |
| 1153 | OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5 |
| 1154 | OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6 |
| 1155 | OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8 |
| 1156 | OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10 |
| 1157 | |
| 1158 | def encode_mpls_interface_group_id(subtype, index): |
| 1159 | index=index&0x00ffffff |
| 1160 | assert(subtype==0) |
| 1161 | return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT) |
| 1162 | |
| 1163 | def encode_mpls_label_group_id(subtype, index): |
| 1164 | index=index&0x00ffffff |
| 1165 | assert(subtype <=5 or subtype==0) |
| 1166 | #1: l2 vpn label |
| 1167 | #2: l3 vpn label |
| 1168 | #3: mpls tunnel label 1 |
| 1169 | #4: mpls tunnel lable 2 |
| 1170 | #5: mpls swap label |
| 1171 | return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT) |
| 1172 | |
| 1173 | def encode_mpls_forwarding_group_id(subtype, index): |
| 1174 | index=index&0x00ffffff |
| 1175 | assert(subtype==6 or subtype==8 or subtype==10) |
| 1176 | return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT) |
| 1177 | |
| 1178 | |
| 1179 | def add_mpls_intf_group(ctrl, ref_gid, dst_mac, src_mac, vid, index, subtype=0): |
| 1180 | action=[] |
| 1181 | action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac))) |
| 1182 | action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac))) |
| 1183 | action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vid))) |
| 1184 | action.append(ofp.action.group(ref_gid)) |
| 1185 | |
| 1186 | buckets = [ofp.bucket(actions=action)] |
| 1187 | |
| 1188 | mpls_group_id =encode_mpls_interface_group_id(subtype, index) |
| 1189 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 1190 | group_id=mpls_group_id, |
| 1191 | buckets=buckets |
| 1192 | ) |
| 1193 | ctrl.message_send(request) |
| 1194 | return mpls_group_id, request |
| 1195 | |
| 1196 | def add_mpls_label_group(ctrl, subtype, index, ref_gid, |
| 1197 | lmep_id=-1, |
| 1198 | qos_index=-1, |
| 1199 | push_l2_header=False, |
| 1200 | push_vlan=False, |
| 1201 | push_mpls_header=False, |
| 1202 | push_cw=False, |
| 1203 | set_mpls_label=None, |
| 1204 | set_bos=None, |
| 1205 | set_tc=None, |
| 1206 | set_tc_from_table=False, |
| 1207 | cpy_tc_outward=False, |
| 1208 | set_ttl=None, |
| 1209 | cpy_ttl_outward=False, |
| 1210 | oam_lm_tx_count=False, |
| 1211 | set_pri_from_table=False |
| 1212 | ): |
| 1213 | """ |
| 1214 | @ref_gid: only can be mpls intf group or mpls tunnel label 1/2 group |
| 1215 | """ |
| 1216 | action=[] |
| 1217 | |
| 1218 | if push_vlan== True: |
| 1219 | action.append(ofp.action.push_vlan(0x8100)) |
| 1220 | if push_mpls_header== True: |
| 1221 | action.append(ofp.action.push_mpls(0x8847)) |
| 1222 | if set_mpls_label != None: |
| 1223 | action.append(ofp.action.set_field(ofp.oxm.mpls_label(set_mpls_label))) |
| 1224 | if set_bos != None: |
| 1225 | action.append(ofp.action.set_field(ofp.oxm.mpls_bos(set_bos))) |
| 1226 | if set_tc != None: |
| 1227 | assert(set_tc_from_table==False) |
| 1228 | action.append(ofp.action.set_field(ofp.oxm.mpls_tc(set_tc))) |
| 1229 | if set_ttl != None: |
| 1230 | action.append(ofp.action.set_mpls_ttl(set_ttl)) |
| 1231 | if cpy_ttl_outward == True: |
| 1232 | action.append(ofp.action.copy_ttl_out()) |
| 1233 | """ |
| 1234 | ofdpa experimenter |
| 1235 | """ |
| 1236 | if push_l2_header== True: |
| 1237 | action.append(ofp.action.ofdpa_push_l2_header()) |
| 1238 | if set_tc_from_table== True: |
| 1239 | assert(qos_index>=0) |
| 1240 | assert(set_tc == None) |
| 1241 | action.append(ofp.action.ofdpa_set_tc_from_table(qos_index)) |
| 1242 | if cpy_tc_outward == True: |
| 1243 | action.append(ofp.action.ofdpa_copy_tc_out()) |
| 1244 | if oam_lm_tx_count == True: |
| 1245 | assert(qos_index>=0 and lmep_id>=0) |
| 1246 | action.append(ofp.action.ofdpa_oam_lm_tx_count(lmep_id, qos_index)) |
| 1247 | if set_pri_from_table == True: |
| 1248 | assert(qos_index>=0) |
| 1249 | action.append(ofp.action.ofdpa_set_qos_from_table(qos_index)) |
| 1250 | if push_cw == True: |
| 1251 | action.append(ofp.action.ofdpa_push_cw()) |
| 1252 | |
| 1253 | action.append(ofp.action.group(ref_gid)) |
| 1254 | buckets = [ofp.bucket(actions=action)] |
| 1255 | |
| 1256 | mpls_group_id = encode_mpls_label_group_id(subtype, index) |
| 1257 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 1258 | group_id=mpls_group_id, |
| 1259 | buckets=buckets |
| 1260 | ) |
| 1261 | ctrl.message_send(request) |
| 1262 | |
| 1263 | return mpls_group_id, request |
| 1264 | |
| 1265 | def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids, |
| 1266 | watch_port=None, |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1267 | watch_group=ofp.OFPP_ANY, |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1268 | push_vlan=None, |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1269 | pop_vlan=None, |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1270 | set_vid=None): |
| 1271 | assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP |
| 1272 | or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP |
| 1273 | or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG) |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1274 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1275 | buckets=[] |
| 1276 | if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP: |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1277 | group_type = ofp.OFPGT_FF |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1278 | for gid in ref_gids: |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1279 | action=[] |
| 1280 | action.append(ofp.action.group(gid)) |
| 1281 | buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action)) |
| 1282 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1283 | elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP: |
| 1284 | group_type = ofp.OFPGT_SELECT |
| 1285 | for gid in ref_gids: |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1286 | action=[] |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1287 | action.append(ofp.action.group(gid)) |
| 1288 | buckets.append(ofp.bucket(actions=action)) |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1289 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1290 | elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG: |
| 1291 | group_type = ofp.OFPGT_INDIRECT |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1292 | action=[] |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1293 | if set_vid!=None: |
| 1294 | action.append(ofp.action.set_field(ofp.oxm.vlan_vid(set_vid))) |
| 1295 | if push_vlan!=None: |
| 1296 | action.append(ofp.action.push_vlan(push_vlan)) |
| 1297 | if pop_vlan!=None: |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1298 | action.append(ofp.action.pop_vlan()) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1299 | action.append(ofp.action.group(ref_gids[0])) |
| 1300 | buckets.append(ofp.bucket(actions=action)) |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1301 | |
| 1302 | mpls_group_id = encode_mpls_forwarding_group_id(subtype, index) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1303 | request = ofp.message.group_add(group_type=group_type, |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1304 | group_id=mpls_group_id, |
| 1305 | buckets=buckets |
| 1306 | ) |
| 1307 | ctrl.message_send(request) |
| 1308 | return mpls_group_id, request |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1309 | |
| 1310 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1311 | """ |
| 1312 | dislay |
| 1313 | """ |
macauley | dbff327 | 2015-07-30 14:07:16 +0800 | [diff] [blame] | 1314 | def print_current_table_flow_stat(ctrl, table_id=0xff): |
| 1315 | stat_req=ofp.message.flow_stats_request() |
| 1316 | response, pkt = ctrl.transact(stat_req) |
| 1317 | if response == None: |
| 1318 | print "no response" |
| 1319 | return None |
| 1320 | print len(response.entries) |
| 1321 | for obj in response.entries: |
| 1322 | print "match ", obj.match |
| 1323 | print "cookie", obj.cookie |
| 1324 | print "priority", obj.priority |
| 1325 | print "idle_timeout", obj.idle_timeout |
| 1326 | print "hard_timeout", obj.hard_timeout |
| 1327 | #obj.actions |
Flavio Castro | 167f5bd | 2015-12-02 19:33:53 -0500 | [diff] [blame] | 1328 | print "packet count: %lx"%obj.packet_count |