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 | |
| 22 | def encode_l2_interface_group_id(vlan, id): |
| 23 | return id + (vlan << OFDPA_VLAN_ID_SHIFT) |
| 24 | |
| 25 | def encode_l2_rewrite_group_id(id): |
| 26 | return id + (1 << OFDPA_GROUP_TYPE_SHIFT) |
| 27 | |
| 28 | def encode_l3_unicast_group_id(id): |
| 29 | return id + (2 << OFDPA_GROUP_TYPE_SHIFT) |
| 30 | |
| 31 | def encode_l2_mcast_group_id(vlan, id): |
| 32 | return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (3 << OFDPA_GROUP_TYPE_SHIFT) |
| 33 | |
| 34 | def encode_l2_flood_group_id(vlan, id): |
| 35 | return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (4 << OFDPA_GROUP_TYPE_SHIFT) |
| 36 | |
| 37 | def encode_l3_interface_group_id(id): |
| 38 | return id + (5 << OFDPA_GROUP_TYPE_SHIFT) |
| 39 | |
| 40 | def encode_l3_mcast_group_id(vlan, id): |
| 41 | return id + (vlan << OFDPA_VLAN_ID_SHIFT)+(6 << OFDPA_GROUP_TYPE_SHIFT) |
| 42 | |
| 43 | def encode_l3_ecmp_group_id(id): |
| 44 | return id + (7 << OFDPA_GROUP_TYPE_SHIFT) |
| 45 | |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame^] | 46 | def encode_l2_overlay_group_id(tunnel_id, subtype, index): |
| 47 | tunnel_id=tunnel_id&0xffff #16 bits |
| 48 | subtype = subtype&3 #2 bits |
| 49 | index = index & 0x3f #10 bits |
| 50 | 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] | 51 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 52 | def add_l2_interface_grouop(ctrl, ports, vlan_id=1, is_tagged=False, send_barrier=False): |
| 53 | # group table |
| 54 | # set up untag groups for each port |
macauley | 41904ed | 2015-07-16 17:38:35 +0800 | [diff] [blame] | 55 | group_id_list=[] |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 56 | msgs=[] |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 57 | for of_port in ports: |
| 58 | # do stuff |
| 59 | group_id = encode_l2_interface_group_id(vlan_id, of_port) |
macauley | 41904ed | 2015-07-16 17:38:35 +0800 | [diff] [blame] | 60 | group_id_list.append(group_id) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 61 | if is_tagged: |
| 62 | actions = [ |
| 63 | ofp.action.output(of_port), |
| 64 | ] |
| 65 | else: |
| 66 | actions = [ |
| 67 | ofp.action.pop_vlan(), |
| 68 | ofp.action.output(of_port), |
| 69 | ] |
| 70 | |
| 71 | buckets = [ |
| 72 | ofp.bucket(actions=actions), |
| 73 | ] |
| 74 | |
| 75 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 76 | group_id=group_id, |
| 77 | buckets=buckets |
| 78 | ) |
| 79 | ctrl.message_send(request) |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 80 | msgs.append(request) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 81 | |
| 82 | if send_barrier: |
| 83 | do_barrier(ctrl) |
macauley | 41904ed | 2015-07-16 17:38:35 +0800 | [diff] [blame] | 84 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 85 | return group_id_list, msgs |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 86 | |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 87 | def add_one_l2_interface_grouop(ctrl, port, vlan_id=1, is_tagged=False, send_barrier=False): |
| 88 | # group table |
| 89 | # set up untag groups for each port |
| 90 | group_id = encode_l2_interface_group_id(vlan_id, port) |
| 91 | |
| 92 | if is_tagged: |
| 93 | actions = [ |
| 94 | ofp.action.output(port), |
| 95 | ] |
| 96 | else: |
| 97 | actions = [ |
| 98 | ofp.action.pop_vlan(), |
| 99 | ofp.action.output(port), |
| 100 | ] |
| 101 | |
| 102 | buckets = [ |
| 103 | ofp.bucket(actions=actions), |
| 104 | ] |
| 105 | |
| 106 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 107 | group_id=group_id, |
| 108 | buckets=buckets |
| 109 | ) |
| 110 | ctrl.message_send(request) |
| 111 | |
| 112 | if send_barrier: |
| 113 | do_barrier(ctrl) |
| 114 | |
| 115 | return group_id, request |
| 116 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 117 | def add_l2_mcast_group(ctrl, ports, vlanid, mcast_grp_index): |
| 118 | buckets=[] |
| 119 | for of_port in ports: |
| 120 | group_id = encode_l2_interface_group_id(vlanid, of_port) |
| 121 | action=[ofp.action.group(group_id)] |
| 122 | buckets.append(ofp.bucket(actions=action)) |
| 123 | |
| 124 | group_id =encode_l2_mcast_group_id(vlanid, mcast_grp_index) |
| 125 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 126 | group_id=group_id, |
| 127 | buckets=buckets |
| 128 | ) |
| 129 | ctrl.message_send(request) |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 130 | return request |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 131 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 132 | def add_l2_flood_group(ctrl, ports, vlanid, id): |
| 133 | buckets=[] |
| 134 | for of_port in ports: |
| 135 | group_id = encode_l2_interface_group_id(vlanid, of_port) |
| 136 | action=[ofp.action.group(group_id)] |
| 137 | buckets.append(ofp.bucket(actions=action)) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 138 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 139 | group_id =encode_l2_flood_group_id(vlanid, id) |
| 140 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 141 | group_id=group_id, |
| 142 | buckets=buckets |
| 143 | ) |
| 144 | ctrl.message_send(request) |
| 145 | return request |
| 146 | |
| 147 | def add_l2_rewrite_group(ctrl, port, vlanid, id, src_mac, dst_mac): |
| 148 | group_id = encode_l2_interface_group_id(vlanid, port) |
| 149 | |
| 150 | action=[] |
| 151 | if src_mac is not None: |
| 152 | action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac))) |
| 153 | |
| 154 | if dst_mac is not None: |
| 155 | action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac))) |
| 156 | |
| 157 | action.append(ofp.action.group(group_id)) |
| 158 | |
| 159 | buckets = [ofp.bucket(actions=action)] |
| 160 | |
| 161 | group_id =encode_l2_rewrite_group_id(id) |
| 162 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 163 | group_id=group_id, |
| 164 | buckets=buckets |
| 165 | ) |
| 166 | ctrl.message_send(request) |
| 167 | return request |
| 168 | |
| 169 | def add_l3_unicast_group(ctrl, port, vlanid, id, src_mac, dst_mac): |
| 170 | group_id = encode_l2_interface_group_id(vlanid, port) |
| 171 | |
| 172 | action=[] |
| 173 | if src_mac is not None: |
| 174 | action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac))) |
| 175 | |
| 176 | if dst_mac is not None: |
| 177 | action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac))) |
| 178 | |
| 179 | action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vlanid))) |
| 180 | |
| 181 | action.append(ofp.action.group(group_id)) |
| 182 | |
| 183 | buckets = [ofp.bucket(actions=action)] |
| 184 | |
| 185 | group_id =encode_l3_unicast_group_id(id) |
| 186 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 187 | group_id=group_id, |
| 188 | buckets=buckets |
| 189 | ) |
| 190 | ctrl.message_send(request) |
| 191 | return request |
| 192 | |
| 193 | def add_l3_interface_group(ctrl, port, vlanid, id, src_mac): |
| 194 | group_id = encode_l2_interface_group_id(vlanid, port) |
| 195 | |
| 196 | action=[] |
| 197 | action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac))) |
| 198 | action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vlanid))) |
| 199 | action.append(ofp.action.group(group_id)) |
| 200 | |
| 201 | buckets = [ofp.bucket(actions=action)] |
| 202 | |
| 203 | group_id =encode_l3_interface_group_id(id) |
| 204 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 205 | group_id=group_id, |
| 206 | buckets=buckets |
| 207 | ) |
| 208 | ctrl.message_send(request) |
| 209 | return request |
| 210 | |
| 211 | def add_l3_ecmp_group(ctrl, id, l3_ucast_groups): |
| 212 | buckets=[] |
| 213 | for group in l3_ucast_groups: |
| 214 | buckets.append(ofp.bucket(actions=[ofp.action.group(group)])) |
| 215 | |
| 216 | group_id =encode_l3_ecmp_group_id(id) |
| 217 | request = ofp.message.group_add(group_type=ofp.OFPGT_SELECT, |
| 218 | group_id=group_id, |
| 219 | buckets=buckets |
| 220 | ) |
| 221 | ctrl.message_send(request) |
| 222 | return request |
| 223 | |
| 224 | def add_l3_mcast_group(ctrl, vid, mcast_group_id, groups_on_buckets): |
| 225 | buckets=[] |
| 226 | for group in groups_on_buckets: |
| 227 | buckets.append(ofp.bucket(actions=[ofp.action.group(group)])) |
| 228 | |
| 229 | group_id =encode_l3_mcast_group_id(vid, mcast_group_id) |
| 230 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 231 | group_id=group_id, |
| 232 | buckets=buckets |
| 233 | ) |
| 234 | ctrl.message_send(request) |
| 235 | return request |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame^] | 236 | |
| 237 | def add_l2_overlay_flood_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index): |
| 238 | buckets=[] |
| 239 | for port in ports: |
| 240 | buckets.append(ofp.bucket(actions=[ofp.action.output(port)])) |
| 241 | |
| 242 | group_id=encode_l2_overlay_group_id(tunnel_id, 0, index) |
| 243 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 244 | group_id=group_id, |
| 245 | buckets=buckets |
| 246 | ) |
| 247 | ctrl.message_send(request) |
| 248 | return request |
| 249 | |
| 250 | def add_l2_overlay_flood_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index): |
| 251 | buckets=[] |
| 252 | for port in ports: |
| 253 | buckets.append(ofp.bucket(actions=[ofp.action.output(port)])) |
| 254 | |
| 255 | group_id=encode_l2_overlay_group_id(tunnel_id, 1, index) |
| 256 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 257 | group_id=group_id, |
| 258 | buckets=buckets |
| 259 | ) |
| 260 | ctrl.message_send(request) |
| 261 | return request |
| 262 | |
| 263 | def add_l2_overlay_mcast_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index): |
| 264 | buckets=[] |
| 265 | for port in ports: |
| 266 | buckets.append(ofp.bucket(actions=[ofp.action.output(port)])) |
| 267 | |
| 268 | group_id=encode_l2_overlay_group_id(tunnel_id, 2, index) |
| 269 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 270 | group_id=group_id, |
| 271 | buckets=buckets |
| 272 | ) |
| 273 | ctrl.message_send(request) |
| 274 | return request |
| 275 | |
| 276 | def add_l2_overlay_mcast_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index): |
| 277 | buckets=[] |
| 278 | for port in ports: |
| 279 | buckets.append(ofp.bucket(actions=[ofp.action.output(port)])) |
| 280 | |
| 281 | group_id=encode_l2_overlay_group_id(tunnel_id, 3, index) |
| 282 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 283 | group_id=group_id, |
| 284 | buckets=buckets |
| 285 | ) |
| 286 | ctrl.message_send(request) |
| 287 | return request |
| 288 | |
| 289 | def add_port_table_flow(ctrl, is_overlay=True): |
| 290 | match = ofp.match() |
| 291 | |
| 292 | if is_overlay == True: |
| 293 | match.oxm_list.append(ofp.oxm.in_port(0x10000)) |
| 294 | else: |
| 295 | match.oxm_list.append(ofp.oxm.in_port(0)) |
| 296 | |
| 297 | request = ofp.message.flow_add( |
| 298 | table_id=0, |
| 299 | cookie=42, |
| 300 | match=match, |
| 301 | instructions=[ |
| 302 | ofp.instruction.goto_table(50) |
| 303 | ], |
| 304 | priority=0) |
| 305 | logging.info("Add port table, match port %lx" % 0x10000) |
| 306 | ctrl.message_send(request) |
| 307 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 308 | def add_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False): |
| 309 | # table 10: vlan |
| 310 | # goto to table 20 |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 311 | msgs=[] |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 312 | for of_port in ports: |
| 313 | if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH): |
| 314 | match = ofp.match() |
| 315 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 316 | match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id)) |
| 317 | request = ofp.message.flow_add( |
| 318 | table_id=10, |
| 319 | cookie=42, |
| 320 | match=match, |
| 321 | instructions=[ |
| 322 | ofp.instruction.goto_table(20) |
| 323 | ], |
| 324 | priority=0) |
| 325 | logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port)) |
| 326 | ctrl.message_send(request) |
| 327 | |
| 328 | if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH): |
| 329 | match = ofp.match() |
| 330 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 331 | match.oxm_list.append(ofp.oxm.vlan_vid(0)) |
| 332 | request = ofp.message.flow_add( |
| 333 | table_id=10, |
| 334 | cookie=42, |
| 335 | match=match, |
| 336 | instructions=[ |
| 337 | ofp.instruction.apply_actions( |
| 338 | actions=[ |
| 339 | ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)) |
| 340 | ] |
| 341 | ), |
| 342 | ofp.instruction.goto_table(20) |
| 343 | ], |
| 344 | priority=0) |
| 345 | logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port)) |
| 346 | ctrl.message_send(request) |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 347 | msgs.append(request) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 348 | |
| 349 | if send_barrier: |
| 350 | do_barrier(ctrl) |
| 351 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 352 | return msgs |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 353 | |
| 354 | def add_one_vlan_table_flow(ctrl, of_port, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False): |
| 355 | # table 10: vlan |
| 356 | # goto to table 20 |
| 357 | if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH): |
| 358 | match = ofp.match() |
| 359 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 360 | match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id)) |
| 361 | request = ofp.message.flow_add( |
| 362 | table_id=10, |
| 363 | cookie=42, |
| 364 | match=match, |
| 365 | instructions=[ |
| 366 | ofp.instruction.goto_table(20) |
| 367 | ], |
| 368 | priority=0) |
| 369 | logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port)) |
| 370 | ctrl.message_send(request) |
| 371 | |
| 372 | if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH): |
| 373 | match = ofp.match() |
| 374 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 375 | match.oxm_list.append(ofp.oxm.vlan_vid(0)) |
| 376 | request = ofp.message.flow_add( |
| 377 | table_id=10, |
| 378 | cookie=42, |
| 379 | match=match, |
| 380 | instructions=[ |
| 381 | ofp.instruction.apply_actions( |
| 382 | actions=[ |
| 383 | ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)) |
| 384 | ] |
| 385 | ), |
| 386 | ofp.instruction.goto_table(20) |
| 387 | ], |
| 388 | priority=0) |
| 389 | logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port)) |
| 390 | ctrl.message_send(request) |
| 391 | |
| 392 | if send_barrier: |
| 393 | do_barrier(ctrl) |
| 394 | |
| 395 | return request |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 396 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 397 | def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False): |
| 398 | match = ofp.match() |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame^] | 399 | if dst_mac!=None: |
| 400 | match.oxm_list.append(ofp.oxm.eth_dst(dst_mac)) |
| 401 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 402 | match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid)) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame^] | 403 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 404 | request = ofp.message.flow_add( |
| 405 | table_id=50, |
| 406 | cookie=42, |
| 407 | match=match, |
| 408 | instructions=[ |
| 409 | ofp.instruction.write_actions( |
| 410 | actions=[ |
| 411 | ofp.action.group(group_id)]), |
| 412 | ofp.instruction.goto_table(60) |
| 413 | ], |
| 414 | buffer_id=ofp.OFP_NO_BUFFER, |
| 415 | priority=1000) |
| 416 | |
| 417 | logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac) |
| 418 | ctrl.message_send(request) |
| 419 | |
| 420 | if send_barrier: |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 421 | do_barrier(ctrl) |
| 422 | |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 423 | return request |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame^] | 424 | |
| 425 | def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False): |
| 426 | match = ofp.match() |
| 427 | if dst_mac!=None: |
| 428 | match.oxm_list.append(ofp.oxm.eth_dst(dst_mac)) |
| 429 | |
| 430 | match.oxm_list.append(ofp.oxm.tunnel_id(vnid)) |
| 431 | if is_group == True: |
| 432 | actions=[ofp.action.group(group_id)] |
| 433 | else: |
| 434 | actions=[ofp.action.output(group_id)] |
| 435 | |
| 436 | request = ofp.message.flow_add( |
| 437 | table_id=50, |
| 438 | cookie=42, |
| 439 | match=match, |
| 440 | instructions=[ |
| 441 | ofp.instruction.write_actions( |
| 442 | actions=actions), |
| 443 | ofp.instruction.goto_table(60) |
| 444 | ], |
| 445 | buffer_id=ofp.OFP_NO_BUFFER, |
| 446 | priority=1000) |
| 447 | |
| 448 | logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac) |
| 449 | ctrl.message_send(request) |
| 450 | |
| 451 | if send_barrier: |
| 452 | do_barrier(ctrl) |
| 453 | |
| 454 | return request |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 455 | |
| 456 | def add_termination_flow(ctrl, in_port, eth_type, dst_mac, vlanid, send_barrier=False): |
| 457 | match = ofp.match() |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 458 | match.oxm_list.append(ofp.oxm.eth_type(eth_type)) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame^] | 459 | if dst_mac[0]&0x01 == 0x01: |
| 460 | match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00])) |
| 461 | goto_table=40 |
| 462 | else: |
| 463 | match.oxm_list.append(ofp.oxm.in_port(in_port)) |
| 464 | match.oxm_list.append(ofp.oxm.eth_dst(dst_mac)) |
| 465 | match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid)) |
| 466 | goto_table=30 |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 467 | |
| 468 | request = ofp.message.flow_add( |
| 469 | table_id=20, |
| 470 | cookie=42, |
| 471 | match=match, |
| 472 | instructions=[ |
| 473 | ofp.instruction.goto_table(goto_table) |
| 474 | ], |
| 475 | buffer_id=ofp.OFP_NO_BUFFER, |
| 476 | priority=1) |
| 477 | |
| 478 | logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac) |
| 479 | ctrl.message_send(request) |
| 480 | |
| 481 | if send_barrier: |
| 482 | do_barrier(ctrl) |
| 483 | |
| 484 | return request |
| 485 | |
macauley | f8b1acd | 2015-07-23 15:13:13 +0800 | [diff] [blame] | 486 | def add_unicast_routing_flow(ctrl, eth_type, dst_ip, mask, action_group_id, send_barrier=False): |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 487 | match = ofp.match() |
| 488 | match.oxm_list.append(ofp.oxm.eth_type(eth_type)) |
macauley | f8b1acd | 2015-07-23 15:13:13 +0800 | [diff] [blame] | 489 | if mask!=0: |
| 490 | match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask)) |
| 491 | else: |
| 492 | match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip)) |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 493 | |
| 494 | request = ofp.message.flow_add( |
| 495 | table_id=30, |
| 496 | cookie=42, |
| 497 | match=match, |
| 498 | instructions=[ |
| 499 | ofp.instruction.write_actions( |
| 500 | actions=[ofp.action.group(action_group_id)]), |
| 501 | ofp.instruction.goto_table(60) |
| 502 | ], |
| 503 | buffer_id=ofp.OFP_NO_BUFFER, |
| 504 | priority=1) |
| 505 | |
| 506 | logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip) |
| 507 | ctrl.message_send(request) |
| 508 | |
| 509 | if send_barrier: |
| 510 | do_barrier(ctrl) |
| 511 | |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame^] | 512 | return request |
| 513 | |
| 514 | def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False): |
| 515 | match = ofp.match() |
| 516 | match.oxm_list.append(ofp.oxm.eth_type(0x0800)) |
| 517 | match.oxm_list.append(ofp.oxm.vlan_vid(vlan_id)) |
| 518 | if src_ip_mask!=0: |
| 519 | match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask)) |
| 520 | else: |
| 521 | match.oxm_list.append(ofp.oxm.ipv4_src(src_ip)) |
| 522 | |
| 523 | match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip)) |
| 524 | |
| 525 | request = ofp.message.flow_add( |
| 526 | table_id=40, |
| 527 | cookie=42, |
| 528 | match=match, |
| 529 | instructions=[ |
| 530 | ofp.instruction.write_actions( |
| 531 | actions=[ofp.action.group(action_group_id)]), |
| 532 | ofp.instruction.goto_table(60) |
| 533 | ], |
| 534 | buffer_id=ofp.OFP_NO_BUFFER, |
| 535 | priority=1) |
| 536 | |
| 537 | logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask) |
| 538 | ctrl.message_send(request) |
| 539 | |
| 540 | if send_barrier: |
| 541 | do_barrier(ctrl) |
| 542 | |
| 543 | return request |
| 544 | |
| 545 | def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'): |
| 546 | """ |
| 547 | Command Example: |
| 548 | of-agent vtap 10001 ethernet 1/1 vid 1 |
| 549 | of-agent vtp 10001 vni 10 |
| 550 | """ |
| 551 | if vlan != 0: |
| 552 | config_vtap_xml=""" |
| 553 | <config> |
| 554 | <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 555 | <id>capable-switch-1</id> |
| 556 | <resources> |
| 557 | <port xc:operation="OPERATION"> |
| 558 | <resource-id >LPORT</resource-id> |
| 559 | <features> |
| 560 | <current> |
| 561 | <rate>10Gb</rate> |
| 562 | <medium>fiber</medium> |
| 563 | <pause>symmetric</pause> |
| 564 | </current> |
| 565 | <advertised> |
| 566 | <rate>10Gb</rate> |
| 567 | <rate>100Gb</rate> |
| 568 | <medium>fiber</medium> |
| 569 | <pause>symmetric</pause> |
| 570 | </advertised> |
| 571 | <supported> |
| 572 | <rate>10Gb</rate> |
| 573 | <rate>100Gb</rate> |
| 574 | <medium>fiber</medium> |
| 575 | <pause>symmetric</pause> |
| 576 | </supported> |
| 577 | <advertised-peer> |
| 578 | <rate>10Gb</rate> |
| 579 | <rate>100Gb</rate> |
| 580 | <medium>fiber</medium> |
| 581 | <pause>symmetric</pause> |
| 582 | </advertised-peer> |
| 583 | </features> |
| 584 | <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 585 | <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port> |
| 586 | <ofdpa10:vid>VLAN_ID</ofdpa10:vid> |
| 587 | <ofdpa10:vni>VNID</ofdpa10:vni> |
| 588 | </ofdpa10:vtap> |
| 589 | </port> |
| 590 | </resources> |
| 591 | <logical-switches> |
| 592 | <switch> |
| 593 | <id>DATAPATH_ID</id> |
| 594 | <datapath-id>DATAPATH_ID</datapath-id> |
| 595 | <resources> |
| 596 | <port xc:operation="OPERATION">LPORT</port> |
| 597 | </resources> |
| 598 | </switch> |
| 599 | </logical-switches> |
| 600 | </capable-switch> |
| 601 | </config> |
| 602 | """ |
| 603 | else: |
| 604 | config_vtap_xml=""" |
| 605 | <config> |
| 606 | <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 607 | <id>capable-switch-1</id> |
| 608 | <resources> |
| 609 | <port xc:operation="OPERATION"> |
| 610 | <resource-id >LPORT</resource-id> |
| 611 | <features> |
| 612 | <current> |
| 613 | <rate>10Gb</rate> |
| 614 | <medium>fiber</medium> |
| 615 | <pause>symmetric</pause> |
| 616 | </current> |
| 617 | <advertised> |
| 618 | <rate>10Gb</rate> |
| 619 | <rate>100Gb</rate> |
| 620 | <medium>fiber</medium> |
| 621 | <pause>symmetric</pause> |
| 622 | </advertised> |
| 623 | <supported> |
| 624 | <rate>10Gb</rate> |
| 625 | <rate>100Gb</rate> |
| 626 | <medium>fiber</medium> |
| 627 | <pause>symmetric</pause> |
| 628 | </supported> |
| 629 | <advertised-peer> |
| 630 | <rate>10Gb</rate> |
| 631 | <rate>100Gb</rate> |
| 632 | <medium>fiber</medium> |
| 633 | <pause>symmetric</pause> |
| 634 | </advertised-peer> |
| 635 | </features> |
| 636 | <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 637 | <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port> |
| 638 | <ofdpa10:vni>VNID</ofdpa10:vni> |
| 639 | </ofdpa10:vtap> |
| 640 | </port> |
| 641 | </resources> |
| 642 | <logical-switches> |
| 643 | <switch> |
| 644 | <id>DATAPATH_ID</id> |
| 645 | <datapath-id>DATAPATH_ID</datapath-id> |
| 646 | <resources> |
| 647 | <port xc:operation="OPERATION">LPORT</port> |
| 648 | </resources> |
| 649 | </switch> |
| 650 | </logical-switches> |
| 651 | </capable-switch> |
| 652 | </config> |
| 653 | """ |
| 654 | str_datapath_id_f= "{:016x}".format(dp_id) |
| 655 | str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)]) |
| 656 | config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id) |
| 657 | config_vtap_xml=config_vtap_xml.replace("LPORT", str(hex(lport))) |
| 658 | config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port)) |
| 659 | config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan)) |
| 660 | config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid)) |
| 661 | config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation)) |
| 662 | return config_vtap_xml |
| 663 | |
| 664 | 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'): |
| 665 | """ |
| 666 | Command Example: |
| 667 | of-agent vtep 10002 source user-input-src-ip destination user-input-dst-ip udp-source-port 6633 nexthop 2 ttl 25 |
| 668 | of-agent vtp 10001 vni 10 |
| 669 | """ |
| 670 | |
| 671 | config_vtep_xml=""" |
| 672 | <config> |
| 673 | <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 674 | <id>capable-switch-1</id> |
| 675 | <resources> |
| 676 | <port xc:operation="OPERATION"> |
| 677 | <resource-id>LPORT</resource-id> |
| 678 | <features> |
| 679 | <current> |
| 680 | <rate>10Gb</rate> |
| 681 | <medium>fiber</medium> |
| 682 | <pause>symmetric</pause> |
| 683 | </current> |
| 684 | <advertised> |
| 685 | <rate>10Gb</rate> |
| 686 | <rate>100Gb</rate> |
| 687 | <medium>fiber</medium> |
| 688 | <pause>symmetric</pause> |
| 689 | </advertised> |
| 690 | <supported> |
| 691 | <rate>10Gb</rate> |
| 692 | <rate>100Gb</rate> |
| 693 | <medium>fiber</medium> |
| 694 | <pause>symmetric</pause> |
| 695 | </supported> |
| 696 | <advertised-peer> |
| 697 | <rate>10Gb</rate> |
| 698 | <rate>100Gb</rate> |
| 699 | <medium>fiber</medium> |
| 700 | <pause>symmetric</pause> |
| 701 | </advertised-peer> |
| 702 | </features> |
| 703 | <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01"> |
| 704 | <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip> |
| 705 | <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip> |
| 706 | <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port> |
| 707 | <ofdpa10:vni>VNID</ofdpa10:vni> |
| 708 | <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id> |
| 709 | <ofdpa10:ttl>TTL</ofdpa10:ttl> |
| 710 | </ofdpa10:vtep> |
| 711 | </port> |
| 712 | </resources> |
| 713 | <logical-switches> |
| 714 | <switch> |
| 715 | <id>DATAPATH_ID</id> |
| 716 | <datapath-id>DATAPATH_ID</datapath-id> |
| 717 | <resources> |
| 718 | <port xc:operation="OPERATION">LPORT</port> |
| 719 | </resources> |
| 720 | </switch> |
| 721 | </logical-switches> |
| 722 | </capable-switch> |
| 723 | </config> |
| 724 | """ |
| 725 | str_datapath_id_f= "{:016x}".format(dp_id) |
| 726 | str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)]) |
| 727 | config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id) |
| 728 | config_vtep_xml=config_vtep_xml.replace("LPORT", str(hex(lport))) |
| 729 | config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip)) |
| 730 | config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip)) |
| 731 | config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port)) |
| 732 | config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id)) |
| 733 | config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl)) |
| 734 | config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid)) |
| 735 | config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation)) |
| 736 | |
| 737 | return config_vtep_xml |
| 738 | |
| 739 | def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'): |
| 740 | #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2 |
| 741 | config_nexthop_xml=""" |
| 742 | <config> |
| 743 | <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 744 | <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 745 | <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id> |
| 746 | <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac> |
| 747 | <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port> |
| 748 | <ofdpa10:vid>VLAN_ID</ofdpa10:vid> |
| 749 | </ofdpa10:next-hop> |
| 750 | </of11-config:capable-switch> |
| 751 | </config> |
| 752 | """ |
| 753 | config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan)) |
| 754 | config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port)) |
| 755 | config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id)) |
| 756 | config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac)) |
| 757 | config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation)) |
| 758 | return config_nexthop_xml |
| 759 | |
| 760 | def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'): |
| 761 | #of-agent vni 10 multicast 224.1.1.1 nexthop 20 |
| 762 | if mcast_ipv4!=None: |
| 763 | config_vni_xml=""" |
| 764 | <config> |
| 765 | <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 766 | <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 767 | <ofdpa10:id>VNID</ofdpa10:id> |
| 768 | <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group> |
| 769 | <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id> |
| 770 | </ofdpa10:vni> |
| 771 | </of11-config:capable-switch> |
| 772 | </config> |
| 773 | """ |
| 774 | config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id)) |
| 775 | config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4)) |
| 776 | else: |
| 777 | config_vni_xml=""" |
| 778 | <config> |
| 779 | <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 780 | <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 781 | <ofdpa10:id>VNID</ofdpa10:id> |
| 782 | </ofdpa10:vni> |
| 783 | </of11-config:capable-switch> |
| 784 | </config> |
| 785 | """ |
| 786 | |
| 787 | config_vni_xml=config_vni_xml.replace("VNID", str(vni_id)) |
| 788 | config_vni_xml=config_vni_xml.replace("OPERATION", str(operation)) |
| 789 | return config_vni_xml |
| 790 | |
| 791 | def get_featureReplay(self): |
| 792 | req = ofp.message.features_request() |
| 793 | res, raw = self.controller.transact(req) |
| 794 | self.assertIsNotNone(res, "Did not receive a response from the DUT.") |
| 795 | self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY, |
| 796 | ("Unexpected packet type %d received in response to " |
| 797 | "OFPT_FEATURES_REQUEST") % res.type) |
| 798 | return res |
| 799 | |
| 800 | def send_edit_config(switch_ip, xml, target='runing'): |
| 801 | NETCONF_ACCOUNT="netconfuser" |
| 802 | NETCONF_PASSWD="netconfuser" |
| 803 | with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m: |
| 804 | try: |
| 805 | m.edit_config(target='running', |
| 806 | config=xml, |
| 807 | default_operation='merge', |
| 808 | error_option='stop-on-error') |
| 809 | |
| 810 | except Exception as e: |
| 811 | logging.info("Fail to set xml %s", xml) |
| 812 | return False |
| 813 | |
| 814 | #return m.get_config(source='running').data_xml |
| 815 | return True |
| 816 | |
| 817 | def send_delete_config(switch_ip, xml, target='runing'): |
| 818 | NETCONF_ACCOUNT="netconfuser" |
| 819 | NETCONF_PASSWD="netconfuser" |
| 820 | with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m: |
| 821 | try: |
| 822 | m.edit_config(target='running', |
| 823 | config=xml, |
| 824 | default_operation='delete', |
| 825 | error_option='stop-on-error') |
| 826 | |
| 827 | except Exception as e: |
| 828 | logging.info("Fail to set xml %s", xml) |
| 829 | return False |
| 830 | |
| 831 | #return m.get_config(source='running').data_xml |
| 832 | return True |
| 833 | |
| 834 | def get_edit_config(switch_ip, target='runing'): |
| 835 | NETCONF_ACCOUNT="netconfuser" |
| 836 | NETCONF_PASSWD="netconfuser" |
| 837 | with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m: |
| 838 | print m.get_config(source='running').data_xml |