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 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 131 | def add_l2_interface_grouop(ctrl, ports, vlan_id=1, is_tagged=False, send_barrier=False): |
| 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 | |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 166 | def add_one_l2_interface_grouop(ctrl, port, vlan_id=1, is_tagged=False, send_barrier=False): |
| 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 | |
| 391 | |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 392 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 393 | def add_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False): |
| 394 | # table 10: vlan |
| 395 | # goto to table 20 |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 396 | msgs=[] |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 397 | for of_port in ports: |
| 398 | if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH): |
| 399 | match = ofp.match() |
| 400 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 401 | match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id)) |
| 402 | request = ofp.message.flow_add( |
| 403 | table_id=10, |
| 404 | cookie=42, |
| 405 | match=match, |
| 406 | instructions=[ |
| 407 | ofp.instruction.goto_table(20) |
| 408 | ], |
| 409 | priority=0) |
| 410 | logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port)) |
| 411 | ctrl.message_send(request) |
| 412 | |
| 413 | if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH): |
| 414 | match = ofp.match() |
| 415 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 416 | match.oxm_list.append(ofp.oxm.vlan_vid(0)) |
| 417 | request = ofp.message.flow_add( |
| 418 | table_id=10, |
| 419 | cookie=42, |
| 420 | match=match, |
| 421 | instructions=[ |
| 422 | ofp.instruction.apply_actions( |
| 423 | actions=[ |
| 424 | ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)) |
| 425 | ] |
| 426 | ), |
| 427 | ofp.instruction.goto_table(20) |
| 428 | ], |
| 429 | priority=0) |
| 430 | logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port)) |
| 431 | ctrl.message_send(request) |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 432 | msgs.append(request) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 433 | |
| 434 | if send_barrier: |
| 435 | do_barrier(ctrl) |
| 436 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 437 | return msgs |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 438 | |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 439 | 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] | 440 | # table 10: vlan |
| 441 | # goto to table 20 |
| 442 | if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH): |
| 443 | match = ofp.match() |
| 444 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 445 | match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id)) |
macauley | 7f89d96 | 2015-08-06 18:13:48 +0800 | [diff] [blame] | 446 | |
| 447 | actions=[] |
| 448 | if vrf!=0: |
| 449 | actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))) |
| 450 | |
| 451 | actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id))) |
| 452 | |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 453 | request = ofp.message.flow_add( |
| 454 | table_id=10, |
| 455 | cookie=42, |
| 456 | match=match, |
| 457 | instructions=[ |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 458 | ofp.instruction.apply_actions( |
macauley | 7f89d96 | 2015-08-06 18:13:48 +0800 | [diff] [blame] | 459 | actions=actions |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 460 | ), |
| 461 | ofp.instruction.goto_table(20) |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 462 | ], |
| 463 | priority=0) |
| 464 | logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port)) |
| 465 | ctrl.message_send(request) |
| 466 | |
| 467 | if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH): |
| 468 | match = ofp.match() |
| 469 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 470 | match.oxm_list.append(ofp.oxm.vlan_vid(0)) |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 471 | |
macauley | 7f89d96 | 2015-08-06 18:13:48 +0800 | [diff] [blame] | 472 | actions=[] |
| 473 | if vrf!=0: |
| 474 | actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))) |
| 475 | |
| 476 | actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))) |
| 477 | |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 478 | request = ofp.message.flow_add( |
| 479 | table_id=10, |
| 480 | cookie=42, |
| 481 | match=match, |
| 482 | instructions=[ |
| 483 | ofp.instruction.apply_actions( |
macauley | 7f89d96 | 2015-08-06 18:13:48 +0800 | [diff] [blame] | 484 | actions=actions |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 485 | ), |
| 486 | ofp.instruction.goto_table(20) |
| 487 | ], |
| 488 | priority=0) |
| 489 | logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port)) |
| 490 | ctrl.message_send(request) |
| 491 | |
| 492 | if send_barrier: |
| 493 | do_barrier(ctrl) |
| 494 | |
| 495 | return request |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 496 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 497 | def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False): |
| 498 | match = ofp.match() |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 499 | if dst_mac!=None: |
| 500 | match.oxm_list.append(ofp.oxm.eth_dst(dst_mac)) |
| 501 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 502 | match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid)) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 503 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 504 | request = ofp.message.flow_add( |
| 505 | table_id=50, |
| 506 | cookie=42, |
| 507 | match=match, |
| 508 | instructions=[ |
| 509 | ofp.instruction.write_actions( |
| 510 | actions=[ |
| 511 | ofp.action.group(group_id)]), |
| 512 | ofp.instruction.goto_table(60) |
| 513 | ], |
| 514 | buffer_id=ofp.OFP_NO_BUFFER, |
| 515 | priority=1000) |
| 516 | |
| 517 | logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac) |
| 518 | ctrl.message_send(request) |
| 519 | |
| 520 | if send_barrier: |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 521 | do_barrier(ctrl) |
| 522 | |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 523 | return request |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 524 | |
| 525 | def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False): |
| 526 | match = ofp.match() |
| 527 | if dst_mac!=None: |
| 528 | match.oxm_list.append(ofp.oxm.eth_dst(dst_mac)) |
| 529 | |
| 530 | match.oxm_list.append(ofp.oxm.tunnel_id(vnid)) |
| 531 | if is_group == True: |
| 532 | actions=[ofp.action.group(group_id)] |
| 533 | else: |
| 534 | actions=[ofp.action.output(group_id)] |
| 535 | |
| 536 | request = ofp.message.flow_add( |
| 537 | table_id=50, |
| 538 | cookie=42, |
| 539 | match=match, |
| 540 | instructions=[ |
| 541 | ofp.instruction.write_actions( |
| 542 | actions=actions), |
| 543 | ofp.instruction.goto_table(60) |
| 544 | ], |
| 545 | buffer_id=ofp.OFP_NO_BUFFER, |
| 546 | priority=1000) |
| 547 | |
| 548 | logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac) |
| 549 | ctrl.message_send(request) |
| 550 | |
| 551 | if send_barrier: |
| 552 | do_barrier(ctrl) |
| 553 | |
| 554 | return request |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 555 | |
| 556 | def add_termination_flow(ctrl, in_port, eth_type, dst_mac, vlanid, send_barrier=False): |
| 557 | match = ofp.match() |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 558 | match.oxm_list.append(ofp.oxm.eth_type(eth_type)) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 559 | if dst_mac[0]&0x01 == 0x01: |
| 560 | match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00])) |
| 561 | goto_table=40 |
| 562 | else: |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 563 | if in_port!=0: |
| 564 | match.oxm_list.append(ofp.oxm.in_port(in_port)) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 565 | match.oxm_list.append(ofp.oxm.eth_dst(dst_mac)) |
| 566 | match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid)) |
| 567 | goto_table=30 |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 568 | |
| 569 | request = ofp.message.flow_add( |
| 570 | table_id=20, |
| 571 | cookie=42, |
| 572 | match=match, |
| 573 | instructions=[ |
| 574 | ofp.instruction.goto_table(goto_table) |
| 575 | ], |
| 576 | buffer_id=ofp.OFP_NO_BUFFER, |
| 577 | priority=1) |
| 578 | |
| 579 | logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac) |
| 580 | ctrl.message_send(request) |
| 581 | |
| 582 | if send_barrier: |
| 583 | do_barrier(ctrl) |
| 584 | |
| 585 | return request |
| 586 | |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 587 | 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] | 588 | match = ofp.match() |
| 589 | match.oxm_list.append(ofp.oxm.eth_type(eth_type)) |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 590 | if vrf != 0: |
| 591 | match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf)) |
| 592 | |
macauley | f8b1acd | 2015-07-23 15:13:13 +0800 | [diff] [blame] | 593 | if mask!=0: |
| 594 | match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask)) |
| 595 | else: |
| 596 | match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip)) |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 597 | |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 598 | |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 599 | request = ofp.message.flow_add( |
| 600 | table_id=30, |
| 601 | cookie=42, |
| 602 | match=match, |
| 603 | instructions=[ |
| 604 | ofp.instruction.write_actions( |
| 605 | actions=[ofp.action.group(action_group_id)]), |
| 606 | ofp.instruction.goto_table(60) |
| 607 | ], |
| 608 | buffer_id=ofp.OFP_NO_BUFFER, |
| 609 | priority=1) |
| 610 | |
| 611 | logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip) |
| 612 | ctrl.message_send(request) |
| 613 | |
| 614 | if send_barrier: |
| 615 | do_barrier(ctrl) |
| 616 | |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 617 | return request |
| 618 | |
| 619 | def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False): |
| 620 | match = ofp.match() |
| 621 | match.oxm_list.append(ofp.oxm.eth_type(0x0800)) |
| 622 | match.oxm_list.append(ofp.oxm.vlan_vid(vlan_id)) |
| 623 | if src_ip_mask!=0: |
| 624 | match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask)) |
| 625 | else: |
| 626 | match.oxm_list.append(ofp.oxm.ipv4_src(src_ip)) |
| 627 | |
| 628 | match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip)) |
| 629 | |
| 630 | request = ofp.message.flow_add( |
| 631 | table_id=40, |
| 632 | cookie=42, |
| 633 | match=match, |
| 634 | instructions=[ |
| 635 | ofp.instruction.write_actions( |
| 636 | actions=[ofp.action.group(action_group_id)]), |
| 637 | ofp.instruction.goto_table(60) |
| 638 | ], |
| 639 | buffer_id=ofp.OFP_NO_BUFFER, |
| 640 | priority=1) |
| 641 | |
| 642 | logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask) |
| 643 | ctrl.message_send(request) |
| 644 | |
| 645 | if send_barrier: |
| 646 | do_barrier(ctrl) |
| 647 | |
| 648 | return request |
| 649 | |
| 650 | def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'): |
| 651 | """ |
| 652 | Command Example: |
| 653 | of-agent vtap 10001 ethernet 1/1 vid 1 |
| 654 | of-agent vtp 10001 vni 10 |
| 655 | """ |
| 656 | if vlan != 0: |
| 657 | config_vtap_xml=""" |
| 658 | <config> |
| 659 | <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 660 | <id>capable-switch-1</id> |
| 661 | <resources> |
| 662 | <port xc:operation="OPERATION"> |
| 663 | <resource-id >LPORT</resource-id> |
| 664 | <features> |
| 665 | <current> |
| 666 | <rate>10Gb</rate> |
| 667 | <medium>fiber</medium> |
| 668 | <pause>symmetric</pause> |
| 669 | </current> |
| 670 | <advertised> |
| 671 | <rate>10Gb</rate> |
| 672 | <rate>100Gb</rate> |
| 673 | <medium>fiber</medium> |
| 674 | <pause>symmetric</pause> |
| 675 | </advertised> |
| 676 | <supported> |
| 677 | <rate>10Gb</rate> |
| 678 | <rate>100Gb</rate> |
| 679 | <medium>fiber</medium> |
| 680 | <pause>symmetric</pause> |
| 681 | </supported> |
| 682 | <advertised-peer> |
| 683 | <rate>10Gb</rate> |
| 684 | <rate>100Gb</rate> |
| 685 | <medium>fiber</medium> |
| 686 | <pause>symmetric</pause> |
| 687 | </advertised-peer> |
| 688 | </features> |
| 689 | <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 690 | <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port> |
| 691 | <ofdpa10:vid>VLAN_ID</ofdpa10:vid> |
| 692 | <ofdpa10:vni>VNID</ofdpa10:vni> |
| 693 | </ofdpa10:vtap> |
| 694 | </port> |
| 695 | </resources> |
| 696 | <logical-switches> |
| 697 | <switch> |
| 698 | <id>DATAPATH_ID</id> |
| 699 | <datapath-id>DATAPATH_ID</datapath-id> |
| 700 | <resources> |
| 701 | <port xc:operation="OPERATION">LPORT</port> |
| 702 | </resources> |
| 703 | </switch> |
| 704 | </logical-switches> |
| 705 | </capable-switch> |
| 706 | </config> |
| 707 | """ |
| 708 | else: |
| 709 | config_vtap_xml=""" |
| 710 | <config> |
| 711 | <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 712 | <id>capable-switch-1</id> |
| 713 | <resources> |
| 714 | <port xc:operation="OPERATION"> |
| 715 | <resource-id >LPORT</resource-id> |
| 716 | <features> |
| 717 | <current> |
| 718 | <rate>10Gb</rate> |
| 719 | <medium>fiber</medium> |
| 720 | <pause>symmetric</pause> |
| 721 | </current> |
| 722 | <advertised> |
| 723 | <rate>10Gb</rate> |
| 724 | <rate>100Gb</rate> |
| 725 | <medium>fiber</medium> |
| 726 | <pause>symmetric</pause> |
| 727 | </advertised> |
| 728 | <supported> |
| 729 | <rate>10Gb</rate> |
| 730 | <rate>100Gb</rate> |
| 731 | <medium>fiber</medium> |
| 732 | <pause>symmetric</pause> |
| 733 | </supported> |
| 734 | <advertised-peer> |
| 735 | <rate>10Gb</rate> |
| 736 | <rate>100Gb</rate> |
| 737 | <medium>fiber</medium> |
| 738 | <pause>symmetric</pause> |
| 739 | </advertised-peer> |
| 740 | </features> |
| 741 | <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 742 | <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port> |
| 743 | <ofdpa10:vni>VNID</ofdpa10:vni> |
| 744 | </ofdpa10:vtap> |
| 745 | </port> |
| 746 | </resources> |
| 747 | <logical-switches> |
| 748 | <switch> |
| 749 | <id>DATAPATH_ID</id> |
| 750 | <datapath-id>DATAPATH_ID</datapath-id> |
| 751 | <resources> |
| 752 | <port xc:operation="OPERATION">LPORT</port> |
| 753 | </resources> |
| 754 | </switch> |
| 755 | </logical-switches> |
| 756 | </capable-switch> |
| 757 | </config> |
| 758 | """ |
| 759 | str_datapath_id_f= "{:016x}".format(dp_id) |
| 760 | str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)]) |
| 761 | config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id) |
macauley | 25999cf | 2015-08-07 17:03:24 +0800 | [diff] [blame] | 762 | config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport))) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 763 | config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port)) |
| 764 | config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan)) |
| 765 | config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid)) |
| 766 | config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation)) |
| 767 | return config_vtap_xml |
| 768 | |
| 769 | 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'): |
| 770 | """ |
| 771 | Command Example: |
| 772 | of-agent vtep 10002 source user-input-src-ip destination user-input-dst-ip udp-source-port 6633 nexthop 2 ttl 25 |
| 773 | of-agent vtp 10001 vni 10 |
| 774 | """ |
| 775 | |
| 776 | config_vtep_xml=""" |
| 777 | <config> |
| 778 | <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 779 | <id>capable-switch-1</id> |
| 780 | <resources> |
| 781 | <port xc:operation="OPERATION"> |
| 782 | <resource-id>LPORT</resource-id> |
| 783 | <features> |
| 784 | <current> |
| 785 | <rate>10Gb</rate> |
| 786 | <medium>fiber</medium> |
| 787 | <pause>symmetric</pause> |
| 788 | </current> |
| 789 | <advertised> |
| 790 | <rate>10Gb</rate> |
| 791 | <rate>100Gb</rate> |
| 792 | <medium>fiber</medium> |
| 793 | <pause>symmetric</pause> |
| 794 | </advertised> |
| 795 | <supported> |
| 796 | <rate>10Gb</rate> |
| 797 | <rate>100Gb</rate> |
| 798 | <medium>fiber</medium> |
| 799 | <pause>symmetric</pause> |
| 800 | </supported> |
| 801 | <advertised-peer> |
| 802 | <rate>10Gb</rate> |
| 803 | <rate>100Gb</rate> |
| 804 | <medium>fiber</medium> |
| 805 | <pause>symmetric</pause> |
| 806 | </advertised-peer> |
| 807 | </features> |
| 808 | <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01"> |
| 809 | <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip> |
| 810 | <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip> |
| 811 | <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port> |
macauley | 25999cf | 2015-08-07 17:03:24 +0800 | [diff] [blame] | 812 | <ofdpa10:vni xc:operation="OPERATION"> |
| 813 | <ofdpa10:id>VNID</ofdpa10:id> |
| 814 | </ofdpa10:vni> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 815 | <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id> |
| 816 | <ofdpa10:ttl>TTL</ofdpa10:ttl> |
| 817 | </ofdpa10:vtep> |
| 818 | </port> |
| 819 | </resources> |
| 820 | <logical-switches> |
| 821 | <switch> |
| 822 | <id>DATAPATH_ID</id> |
| 823 | <datapath-id>DATAPATH_ID</datapath-id> |
| 824 | <resources> |
| 825 | <port xc:operation="OPERATION">LPORT</port> |
| 826 | </resources> |
| 827 | </switch> |
| 828 | </logical-switches> |
| 829 | </capable-switch> |
| 830 | </config> |
| 831 | """ |
| 832 | str_datapath_id_f= "{:016x}".format(dp_id) |
| 833 | str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)]) |
| 834 | config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id) |
macauley | 25999cf | 2015-08-07 17:03:24 +0800 | [diff] [blame] | 835 | config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport))) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 836 | config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip)) |
| 837 | config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip)) |
| 838 | config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port)) |
| 839 | config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id)) |
| 840 | config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl)) |
| 841 | config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid)) |
| 842 | config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation)) |
| 843 | |
| 844 | return config_vtep_xml |
| 845 | |
| 846 | def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'): |
| 847 | #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2 |
| 848 | config_nexthop_xml=""" |
| 849 | <config> |
| 850 | <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 851 | <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 852 | <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id> |
| 853 | <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac> |
| 854 | <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port> |
| 855 | <ofdpa10:vid>VLAN_ID</ofdpa10:vid> |
| 856 | </ofdpa10:next-hop> |
| 857 | </of11-config:capable-switch> |
| 858 | </config> |
| 859 | """ |
| 860 | config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan)) |
| 861 | config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port)) |
| 862 | config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id)) |
| 863 | config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac)) |
| 864 | config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation)) |
| 865 | return config_nexthop_xml |
| 866 | |
| 867 | def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'): |
| 868 | #of-agent vni 10 multicast 224.1.1.1 nexthop 20 |
| 869 | if mcast_ipv4!=None: |
| 870 | config_vni_xml=""" |
| 871 | <config> |
| 872 | <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 873 | <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 874 | <ofdpa10:id>VNID</ofdpa10:id> |
| 875 | <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group> |
| 876 | <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id> |
| 877 | </ofdpa10:vni> |
| 878 | </of11-config:capable-switch> |
| 879 | </config> |
| 880 | """ |
| 881 | config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id)) |
| 882 | config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4)) |
| 883 | else: |
| 884 | config_vni_xml=""" |
| 885 | <config> |
| 886 | <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 887 | <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 888 | <ofdpa10:id>VNID</ofdpa10:id> |
| 889 | </ofdpa10:vni> |
| 890 | </of11-config:capable-switch> |
| 891 | </config> |
| 892 | """ |
| 893 | |
| 894 | config_vni_xml=config_vni_xml.replace("VNID", str(vni_id)) |
| 895 | config_vni_xml=config_vni_xml.replace("OPERATION", str(operation)) |
| 896 | return config_vni_xml |
| 897 | |
| 898 | def get_featureReplay(self): |
| 899 | req = ofp.message.features_request() |
| 900 | res, raw = self.controller.transact(req) |
| 901 | self.assertIsNotNone(res, "Did not receive a response from the DUT.") |
| 902 | self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY, |
| 903 | ("Unexpected packet type %d received in response to " |
| 904 | "OFPT_FEATURES_REQUEST") % res.type) |
| 905 | return res |
| 906 | |
| 907 | def send_edit_config(switch_ip, xml, target='runing'): |
| 908 | NETCONF_ACCOUNT="netconfuser" |
| 909 | NETCONF_PASSWD="netconfuser" |
| 910 | with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m: |
| 911 | try: |
| 912 | m.edit_config(target='running', |
| 913 | config=xml, |
| 914 | default_operation='merge', |
| 915 | error_option='stop-on-error') |
| 916 | |
| 917 | except Exception as e: |
| 918 | logging.info("Fail to set xml %s", xml) |
| 919 | return False |
| 920 | |
| 921 | #return m.get_config(source='running').data_xml |
| 922 | return True |
| 923 | |
| 924 | def send_delete_config(switch_ip, xml, target='runing'): |
| 925 | NETCONF_ACCOUNT="netconfuser" |
| 926 | NETCONF_PASSWD="netconfuser" |
| 927 | with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m: |
| 928 | try: |
| 929 | m.edit_config(target='running', |
| 930 | config=xml, |
| 931 | default_operation='delete', |
| 932 | error_option='stop-on-error') |
| 933 | |
| 934 | except Exception as e: |
| 935 | logging.info("Fail to set xml %s", xml) |
| 936 | return False |
| 937 | |
| 938 | #return m.get_config(source='running').data_xml |
| 939 | return True |
| 940 | |
| 941 | def get_edit_config(switch_ip, target='runing'): |
| 942 | NETCONF_ACCOUNT="netconfuser" |
| 943 | NETCONF_PASSWD="netconfuser" |
| 944 | 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] | 945 | return m.get_config(source='running').data_xml |
macauley | dbff327 | 2015-07-30 14:07:16 +0800 | [diff] [blame] | 946 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 947 | |
| 948 | """ |
| 949 | MPLS |
| 950 | """ |
| 951 | |
| 952 | OFDPA_MPLS_SUBTYPE_SHIFT=24 |
| 953 | OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1 |
| 954 | OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2 |
| 955 | OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3 |
| 956 | OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4 |
| 957 | OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5 |
| 958 | OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6 |
| 959 | OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8 |
| 960 | OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10 |
| 961 | |
| 962 | def encode_mpls_interface_group_id(subtype, index): |
| 963 | index=index&0x00ffffff |
| 964 | assert(subtype==0) |
| 965 | return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT) |
| 966 | |
| 967 | def encode_mpls_label_group_id(subtype, index): |
| 968 | index=index&0x00ffffff |
| 969 | assert(subtype <=5 or subtype==0) |
| 970 | #1: l2 vpn label |
| 971 | #2: l3 vpn label |
| 972 | #3: mpls tunnel label 1 |
| 973 | #4: mpls tunnel lable 2 |
| 974 | #5: mpls swap label |
| 975 | return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT) |
| 976 | |
| 977 | def encode_mpls_forwarding_group_id(subtype, index): |
| 978 | index=index&0x00ffffff |
| 979 | assert(subtype==6 or subtype==8 or subtype==10) |
| 980 | return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT) |
| 981 | |
| 982 | |
| 983 | def add_mpls_intf_group(ctrl, ref_gid, dst_mac, src_mac, vid, index, subtype=0): |
| 984 | action=[] |
| 985 | action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac))) |
| 986 | action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac))) |
| 987 | action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vid))) |
| 988 | action.append(ofp.action.group(ref_gid)) |
| 989 | |
| 990 | buckets = [ofp.bucket(actions=action)] |
| 991 | |
| 992 | mpls_group_id =encode_mpls_interface_group_id(subtype, index) |
| 993 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 994 | group_id=mpls_group_id, |
| 995 | buckets=buckets |
| 996 | ) |
| 997 | ctrl.message_send(request) |
| 998 | return mpls_group_id, request |
| 999 | |
| 1000 | def add_mpls_label_group(ctrl, subtype, index, ref_gid, |
| 1001 | lmep_id=-1, |
| 1002 | qos_index=-1, |
| 1003 | push_l2_header=False, |
| 1004 | push_vlan=False, |
| 1005 | push_mpls_header=False, |
| 1006 | push_cw=False, |
| 1007 | set_mpls_label=None, |
| 1008 | set_bos=None, |
| 1009 | set_tc=None, |
| 1010 | set_tc_from_table=False, |
| 1011 | cpy_tc_outward=False, |
| 1012 | set_ttl=None, |
| 1013 | cpy_ttl_outward=False, |
| 1014 | oam_lm_tx_count=False, |
| 1015 | set_pri_from_table=False |
| 1016 | ): |
| 1017 | """ |
| 1018 | @ref_gid: only can be mpls intf group or mpls tunnel label 1/2 group |
| 1019 | """ |
| 1020 | action=[] |
| 1021 | |
| 1022 | if push_vlan== True: |
| 1023 | action.append(ofp.action.push_vlan(0x8100)) |
| 1024 | if push_mpls_header== True: |
| 1025 | action.append(ofp.action.push_mpls(0x8847)) |
| 1026 | if set_mpls_label != None: |
| 1027 | action.append(ofp.action.set_field(ofp.oxm.mpls_label(set_mpls_label))) |
| 1028 | if set_bos != None: |
| 1029 | action.append(ofp.action.set_field(ofp.oxm.mpls_bos(set_bos))) |
| 1030 | if set_tc != None: |
| 1031 | assert(set_tc_from_table==False) |
| 1032 | action.append(ofp.action.set_field(ofp.oxm.mpls_tc(set_tc))) |
| 1033 | if set_ttl != None: |
| 1034 | action.append(ofp.action.set_mpls_ttl(set_ttl)) |
| 1035 | if cpy_ttl_outward == True: |
| 1036 | action.append(ofp.action.copy_ttl_out()) |
| 1037 | """ |
| 1038 | ofdpa experimenter |
| 1039 | """ |
| 1040 | if push_l2_header== True: |
| 1041 | action.append(ofp.action.ofdpa_push_l2_header()) |
| 1042 | if set_tc_from_table== True: |
| 1043 | assert(qos_index>=0) |
| 1044 | assert(set_tc == None) |
| 1045 | action.append(ofp.action.ofdpa_set_tc_from_table(qos_index)) |
| 1046 | if cpy_tc_outward == True: |
| 1047 | action.append(ofp.action.ofdpa_copy_tc_out()) |
| 1048 | if oam_lm_tx_count == True: |
| 1049 | assert(qos_index>=0 and lmep_id>=0) |
| 1050 | action.append(ofp.action.ofdpa_oam_lm_tx_count(lmep_id, qos_index)) |
| 1051 | if set_pri_from_table == True: |
| 1052 | assert(qos_index>=0) |
| 1053 | action.append(ofp.action.ofdpa_set_qos_from_table(qos_index)) |
| 1054 | if push_cw == True: |
| 1055 | action.append(ofp.action.ofdpa_push_cw()) |
| 1056 | |
| 1057 | action.append(ofp.action.group(ref_gid)) |
| 1058 | buckets = [ofp.bucket(actions=action)] |
| 1059 | |
| 1060 | mpls_group_id = encode_mpls_label_group_id(subtype, index) |
| 1061 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 1062 | group_id=mpls_group_id, |
| 1063 | buckets=buckets |
| 1064 | ) |
| 1065 | ctrl.message_send(request) |
| 1066 | |
| 1067 | return mpls_group_id, request |
| 1068 | |
| 1069 | def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids, |
| 1070 | watch_port=None, |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1071 | watch_group=ofp.OFPP_ANY, |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1072 | push_vlan=None, |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1073 | pop_vlan=None, |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1074 | set_vid=None): |
| 1075 | assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP |
| 1076 | or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP |
| 1077 | or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG) |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1078 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1079 | buckets=[] |
| 1080 | if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP: |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1081 | group_type = ofp.OFPGT_FF |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1082 | for gid in ref_gids: |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1083 | action=[] |
| 1084 | action.append(ofp.action.group(gid)) |
| 1085 | buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action)) |
| 1086 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1087 | elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP: |
| 1088 | group_type = ofp.OFPGT_SELECT |
| 1089 | for gid in ref_gids: |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1090 | action=[] |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1091 | action.append(ofp.action.group(gid)) |
| 1092 | buckets.append(ofp.bucket(actions=action)) |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1093 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1094 | elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG: |
| 1095 | group_type = ofp.OFPGT_INDIRECT |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1096 | action=[] |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1097 | if set_vid!=None: |
| 1098 | action.append(ofp.action.set_field(ofp.oxm.vlan_vid(set_vid))) |
| 1099 | if push_vlan!=None: |
| 1100 | action.append(ofp.action.push_vlan(push_vlan)) |
| 1101 | if pop_vlan!=None: |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1102 | action.append(ofp.action.pop_vlan()) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1103 | action.append(ofp.action.group(ref_gids[0])) |
| 1104 | buckets.append(ofp.bucket(actions=action)) |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1105 | |
| 1106 | mpls_group_id = encode_mpls_forwarding_group_id(subtype, index) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1107 | request = ofp.message.group_add(group_type=group_type, |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1108 | group_id=mpls_group_id, |
| 1109 | buckets=buckets |
| 1110 | ) |
| 1111 | ctrl.message_send(request) |
| 1112 | return mpls_group_id, request |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1113 | |
| 1114 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1115 | """ |
| 1116 | dislay |
| 1117 | """ |
macauley | dbff327 | 2015-07-30 14:07:16 +0800 | [diff] [blame] | 1118 | def print_current_table_flow_stat(ctrl, table_id=0xff): |
| 1119 | stat_req=ofp.message.flow_stats_request() |
| 1120 | response, pkt = ctrl.transact(stat_req) |
| 1121 | if response == None: |
| 1122 | print "no response" |
| 1123 | return None |
| 1124 | print len(response.entries) |
| 1125 | for obj in response.entries: |
| 1126 | print "match ", obj.match |
| 1127 | print "cookie", obj.cookie |
| 1128 | print "priority", obj.priority |
| 1129 | print "idle_timeout", obj.idle_timeout |
| 1130 | print "hard_timeout", obj.hard_timeout |
| 1131 | #obj.actions |
| 1132 | print "packet count: %lx"%obj.packet_count |