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