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 |
Pier | e130876 | 2016-09-12 15:29:56 -0700 | [diff] [blame] | 21 | VLAN_TABLE_FLAG_ONLY_STACKED=5 |
| 22 | VLAN_TABLE_FLAG_PRIORITY=6 |
| 23 | VLAN_TABLE_FLAG_ONLY_UNTAG_PRIORITY=7 |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 24 | |
macauley | e8b140e | 2015-08-03 13:35:45 +0800 | [diff] [blame] | 25 | PORT_FLOW_TABLE=0 |
| 26 | VLAN_FLOW_TABLE=10 |
Pier | e130876 | 2016-09-12 15:29:56 -0700 | [diff] [blame] | 27 | VLAN_1_FLOW_TABLE=11 |
Pier | cf76e80 | 2016-09-19 20:16:35 -0700 | [diff] [blame] | 28 | MPLS_L2_PORT_FLOW_TABLE=13 |
| 29 | MPLS_L2_PORT_DSCP_TRUST_FLOW_TABLE=15 |
| 30 | MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE=16 |
macauley | e8b140e | 2015-08-03 13:35:45 +0800 | [diff] [blame] | 31 | TERMINATION_FLOW_TABLE=20 |
Pier | cf76e80 | 2016-09-19 20:16:35 -0700 | [diff] [blame] | 32 | MPLS_TYPE_FLOW_TABLE=29 |
macauley | e8b140e | 2015-08-03 13:35:45 +0800 | [diff] [blame] | 33 | UCAST_ROUTING_FLOW_TABLE=30 |
| 34 | MCAST_ROUTING_FLOW_TABLE=40 |
| 35 | BRIDGE_FLOW_TABLE=50 |
| 36 | ACL_FLOW_TABLE=60 |
| 37 | |
| 38 | def convertIP4toStr(ip_addr): |
| 39 | a=(ip_addr&0xff000000)>>24 |
| 40 | b=(ip_addr&0x00ff0000)>>16 |
| 41 | c=(ip_addr&0x0000ff00)>>8 |
| 42 | d=(ip_addr&0x000000ff) |
| 43 | return str(a)+"."+str(b)+"."+str(c)+"."+str(d) |
| 44 | |
| 45 | def convertMACtoStr(mac): |
| 46 | if not isinstance(mac, list): |
| 47 | assert(0) |
| 48 | |
| 49 | return ':'.join(['%02X' % x for x in mac]) |
| 50 | |
macauley | 7f89d96 | 2015-08-06 18:13:48 +0800 | [diff] [blame] | 51 | def getSwitchCpuMACFromDPID(dpid): |
| 52 | str_datapath_id_f= "{:016x}".format(dpid) |
| 53 | str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)]) |
| 54 | switch_cpu_mac_str=str_datapath_id[6:] |
| 55 | switch_cpu_mac = switch_cpu_mac_str.split(":") |
| 56 | switch_cpu_mac=[int(switch_cpu_mac[i],16) for i in range(0, len(switch_cpu_mac))] |
| 57 | |
| 58 | return switch_cpu_mac_str, switch_cpu_mac |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 59 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 60 | def DumpGroup(stats, verify_group_stats, always_show=True): |
| 61 | if(len(stats) > len(verify_group_stats)): |
| 62 | min_len = len(verify_group_stats) |
| 63 | print "Stats Len is not the same, stats>verify_group_stats" |
| 64 | if(len(stats)< len(verify_group_stats)): |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 65 | min_len = len(stats) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 66 | print "Stats Len is not the same, stats<verify_group_stats" |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 67 | else: |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 68 | min_len = len(stats) |
macauley | e8b140e | 2015-08-03 13:35:45 +0800 | [diff] [blame] | 69 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 70 | print "\r\n" |
| 71 | for i in range(min_len): |
| 72 | gs = stats[i] |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 73 | gv = verify_group_stats[i] |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 74 | print "FromSwtich:(GID=%lx, TYPE=%lx)\r\nVerify :(GID=%lx, TYPE=%lx)"%(gs.group_id, gs.group_type, gv.group_id, gv.group_type) |
| 75 | if(len(gs.buckets) != len(gv.buckets)): |
| 76 | print "buckets len is not the same gs %lx, gv %lx",(len(gs.buckets), len(gv.buckets)) |
| 77 | |
| 78 | for j in range(len(gs.buckets)): |
| 79 | b1=gs.buckets[j] |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 80 | b2=gv.buckets[j] |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 81 | if(len(b1.actions) != len(b2.actions)): |
| 82 | print "action len is not the same" |
| 83 | |
| 84 | for k in range(len(b1.actions)): |
| 85 | a1=b1.actions[k] |
| 86 | a2=b2.actions[k] |
| 87 | if(always_show == True): |
| 88 | print "a1:"+a1.show() |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 89 | print "a2:"+a2.show() |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 90 | |
| 91 | def AssertGroup(self, stats, verify_group_stats): |
| 92 | self.assertTrue(len(stats) ==len(verify_group_stats), "stats len is not the same") |
| 93 | |
| 94 | for i in range(len(stats)): |
| 95 | gs = stats[i] |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 96 | gv = verify_group_stats[i] |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 97 | self.assertTrue(len(gs.buckets) == len(gv.buckets), "buckets len is not the same") |
| 98 | |
| 99 | for j in range(len(gs.buckets)): |
| 100 | b1=gs.buckets[j] |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 101 | b2=gv.buckets[j] |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 102 | self.assertTrue(len(b1.actions) == len(b2.actions), "action len is not the same") |
| 103 | |
| 104 | for k in range(len(b1.actions)): |
| 105 | a1=b1.actions[k] |
| 106 | a2=b2.actions[k] |
| 107 | self.assertEquals(a1, a2, "action is not the same") |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 108 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 109 | def encode_l2_interface_group_id(vlan, id): |
| 110 | return id + (vlan << OFDPA_VLAN_ID_SHIFT) |
| 111 | |
| 112 | def encode_l2_rewrite_group_id(id): |
| 113 | return id + (1 << OFDPA_GROUP_TYPE_SHIFT) |
| 114 | |
| 115 | def encode_l3_unicast_group_id(id): |
| 116 | return id + (2 << OFDPA_GROUP_TYPE_SHIFT) |
| 117 | |
| 118 | def encode_l2_mcast_group_id(vlan, id): |
| 119 | return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (3 << OFDPA_GROUP_TYPE_SHIFT) |
| 120 | |
| 121 | def encode_l2_flood_group_id(vlan, id): |
| 122 | return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (4 << OFDPA_GROUP_TYPE_SHIFT) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 123 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 124 | def encode_l3_interface_group_id(id): |
| 125 | return id + (5 << OFDPA_GROUP_TYPE_SHIFT) |
| 126 | |
| 127 | def encode_l3_mcast_group_id(vlan, id): |
| 128 | return id + (vlan << OFDPA_VLAN_ID_SHIFT)+(6 << OFDPA_GROUP_TYPE_SHIFT) |
| 129 | |
| 130 | def encode_l3_ecmp_group_id(id): |
| 131 | return id + (7 << OFDPA_GROUP_TYPE_SHIFT) |
| 132 | |
Flavio Castro | 91d1a55 | 2016-05-17 16:59:44 -0700 | [diff] [blame] | 133 | def encode_l2_unfiltered_group_id(id): |
| 134 | return id + (11 << OFDPA_GROUP_TYPE_SHIFT) |
| 135 | |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 136 | def encode_l2_overlay_group_id(tunnel_id, subtype, index): |
| 137 | tunnel_id=tunnel_id&0xffff #16 bits |
| 138 | subtype = subtype&3 #2 bits |
| 139 | index = index & 0x3f #10 bits |
| 140 | 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] | 141 | |
Flavio Castro | 91d1a55 | 2016-05-17 16:59:44 -0700 | [diff] [blame] | 142 | def add_l2_unfiltered_group(ctrl, ports, send_barrier=False): |
| 143 | # group table |
| 144 | # set up untag groups for each port |
| 145 | group_id_list=[] |
| 146 | msgs=[] |
| 147 | for of_port in ports: |
| 148 | # do stuff |
| 149 | group_id = encode_l2_unfiltered_group_id(of_port) |
| 150 | group_id_list.append(group_id) |
| 151 | actions = [ofp.action.output(of_port)] |
| 152 | actions.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=24, value=1))) |
| 153 | |
| 154 | buckets = [ofp.bucket(actions=actions)] |
| 155 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 156 | group_id=group_id, |
| 157 | buckets=buckets |
| 158 | ) |
| 159 | ctrl.message_send(request) |
| 160 | msgs.append(request) |
| 161 | |
| 162 | if send_barrier: |
| 163 | do_barrier(ctrl) |
| 164 | |
| 165 | return group_id_list, msgs |
| 166 | |
Pier | f6f2816 | 2016-09-22 16:30:52 -0700 | [diff] [blame^] | 167 | def add_one_l2_unfiltered_group(ctrl, of_port, send_barrier=False): |
| 168 | # group table |
| 169 | # set up untag groups for each port |
| 170 | group_id = encode_l2_unfiltered_group_id(of_port) |
| 171 | actions = [ofp.action.output(of_port)] |
| 172 | actions.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=24, value=1))) |
| 173 | |
| 174 | buckets = [ofp.bucket(actions=actions)] |
| 175 | request = ofp.message.group_add( |
| 176 | group_type=ofp.OFPGT_INDIRECT, |
| 177 | group_id=group_id, |
| 178 | buckets=buckets |
| 179 | ) |
| 180 | ctrl.message_send(request) |
| 181 | |
| 182 | if send_barrier: |
| 183 | do_barrier(ctrl) |
| 184 | |
| 185 | return group_id, request |
| 186 | |
Flavio Castro | d4c44d1 | 2015-12-08 14:44:18 -0500 | [diff] [blame] | 187 | 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] | 188 | # group table |
| 189 | # set up untag groups for each port |
macauley | 41904ed | 2015-07-16 17:38:35 +0800 | [diff] [blame] | 190 | group_id_list=[] |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 191 | msgs=[] |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 192 | for of_port in ports: |
| 193 | # do stuff |
| 194 | group_id = encode_l2_interface_group_id(vlan_id, of_port) |
macauley | 41904ed | 2015-07-16 17:38:35 +0800 | [diff] [blame] | 195 | group_id_list.append(group_id) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 196 | if is_tagged: |
| 197 | actions = [ |
| 198 | ofp.action.output(of_port), |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 199 | ] |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 200 | else: |
| 201 | actions = [ |
| 202 | ofp.action.pop_vlan(), |
| 203 | ofp.action.output(of_port), |
| 204 | ] |
| 205 | |
| 206 | buckets = [ |
| 207 | ofp.bucket(actions=actions), |
| 208 | ] |
| 209 | |
| 210 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 211 | group_id=group_id, |
| 212 | buckets=buckets |
| 213 | ) |
| 214 | ctrl.message_send(request) |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 215 | msgs.append(request) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 216 | |
| 217 | if send_barrier: |
| 218 | do_barrier(ctrl) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 219 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 220 | return group_id_list, msgs |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 221 | |
Flavio Castro | d4c44d1 | 2015-12-08 14:44:18 -0500 | [diff] [blame] | 222 | 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] | 223 | # group table |
| 224 | # set up untag groups for each port |
| 225 | group_id = encode_l2_interface_group_id(vlan_id, port) |
| 226 | |
| 227 | if is_tagged: |
| 228 | actions = [ |
| 229 | ofp.action.output(port), |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 230 | ] |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 231 | else: |
| 232 | actions = [ |
| 233 | ofp.action.pop_vlan(), |
| 234 | ofp.action.output(port), |
| 235 | ] |
| 236 | |
| 237 | buckets = [ |
| 238 | ofp.bucket(actions=actions), |
| 239 | ] |
| 240 | |
| 241 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 242 | group_id=group_id, |
| 243 | buckets=buckets |
| 244 | ) |
| 245 | ctrl.message_send(request) |
| 246 | |
| 247 | if send_barrier: |
| 248 | do_barrier(ctrl) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 249 | |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 250 | return group_id, request |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 251 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 252 | def add_l2_mcast_group(ctrl, ports, vlanid, mcast_grp_index): |
| 253 | buckets=[] |
| 254 | for of_port in ports: |
| 255 | group_id = encode_l2_interface_group_id(vlanid, of_port) |
| 256 | action=[ofp.action.group(group_id)] |
| 257 | buckets.append(ofp.bucket(actions=action)) |
| 258 | |
| 259 | group_id =encode_l2_mcast_group_id(vlanid, mcast_grp_index) |
| 260 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 261 | group_id=group_id, |
| 262 | buckets=buckets |
| 263 | ) |
| 264 | ctrl.message_send(request) |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 265 | return request |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 266 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 267 | def add_l2_flood_group(ctrl, ports, vlanid, id): |
| 268 | buckets=[] |
| 269 | for of_port in ports: |
| 270 | group_id = encode_l2_interface_group_id(vlanid, of_port) |
| 271 | action=[ofp.action.group(group_id)] |
| 272 | buckets.append(ofp.bucket(actions=action)) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 273 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 274 | group_id =encode_l2_flood_group_id(vlanid, id) |
| 275 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 276 | group_id=group_id, |
| 277 | buckets=buckets |
| 278 | ) |
| 279 | ctrl.message_send(request) |
| 280 | return request |
| 281 | |
Flavio Castro | a7162bb | 2016-07-25 17:30:30 -0700 | [diff] [blame] | 282 | def mod_l2_flood_group(ctrl, ports, vlanid, id): |
| 283 | buckets=[] |
| 284 | for of_port in ports: |
| 285 | group_id = encode_l2_interface_group_id(vlanid, of_port) |
| 286 | action=[ofp.action.group(group_id)] |
| 287 | buckets.append(ofp.bucket(actions=action)) |
| 288 | |
| 289 | group_id =encode_l2_flood_group_id(vlanid, id) |
| 290 | request = ofp.message.group_modify(group_type=ofp.OFPGT_ALL, |
| 291 | group_id=group_id, |
| 292 | buckets=buckets |
| 293 | ) |
| 294 | ctrl.message_send(request) |
| 295 | return request |
| 296 | |
| 297 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 298 | def add_l2_rewrite_group(ctrl, port, vlanid, id, src_mac, dst_mac): |
| 299 | group_id = encode_l2_interface_group_id(vlanid, port) |
| 300 | |
| 301 | action=[] |
| 302 | if src_mac is not None: |
| 303 | action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac))) |
| 304 | |
| 305 | if dst_mac is not None: |
| 306 | action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac))) |
| 307 | |
Flavio Castro | 91d1a55 | 2016-05-17 16:59:44 -0700 | [diff] [blame] | 308 | action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid))) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 309 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 310 | action.append(ofp.action.group(group_id)) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 311 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 312 | buckets = [ofp.bucket(actions=action)] |
| 313 | |
| 314 | group_id =encode_l2_rewrite_group_id(id) |
| 315 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 316 | group_id=group_id, |
| 317 | buckets=buckets |
| 318 | ) |
| 319 | ctrl.message_send(request) |
| 320 | return request |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 321 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 322 | def add_l3_unicast_group(ctrl, port, vlanid, id, src_mac, dst_mac): |
| 323 | group_id = encode_l2_interface_group_id(vlanid, port) |
| 324 | |
| 325 | action=[] |
| 326 | if src_mac is not None: |
| 327 | action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac))) |
| 328 | |
| 329 | if dst_mac is not None: |
| 330 | action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac))) |
| 331 | |
Flavio Castro | af2b450 | 2016-02-02 17:41:32 -0500 | [diff] [blame] | 332 | action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid))) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 333 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 334 | action.append(ofp.action.group(group_id)) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 335 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 336 | buckets = [ofp.bucket(actions=action)] |
| 337 | |
| 338 | group_id =encode_l3_unicast_group_id(id) |
| 339 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 340 | group_id=group_id, |
| 341 | buckets=buckets |
| 342 | ) |
| 343 | ctrl.message_send(request) |
| 344 | return request |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 345 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 346 | def add_l3_interface_group(ctrl, port, vlanid, id, src_mac): |
| 347 | group_id = encode_l2_interface_group_id(vlanid, port) |
| 348 | |
| 349 | action=[] |
| 350 | action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac))) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 351 | action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid))) |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 352 | action.append(ofp.action.group(group_id)) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 353 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 354 | buckets = [ofp.bucket(actions=action)] |
| 355 | |
| 356 | group_id =encode_l3_interface_group_id(id) |
| 357 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 358 | group_id=group_id, |
| 359 | buckets=buckets |
| 360 | ) |
| 361 | ctrl.message_send(request) |
| 362 | return request |
| 363 | |
| 364 | def add_l3_ecmp_group(ctrl, id, l3_ucast_groups): |
| 365 | buckets=[] |
| 366 | for group in l3_ucast_groups: |
| 367 | buckets.append(ofp.bucket(actions=[ofp.action.group(group)])) |
| 368 | |
| 369 | group_id =encode_l3_ecmp_group_id(id) |
| 370 | request = ofp.message.group_add(group_type=ofp.OFPGT_SELECT, |
| 371 | group_id=group_id, |
| 372 | buckets=buckets |
| 373 | ) |
| 374 | ctrl.message_send(request) |
| 375 | return request |
Flavio Castro | a7162bb | 2016-07-25 17:30:30 -0700 | [diff] [blame] | 376 | |
| 377 | def mod_l3_ecmp_group(ctrl, id, l3_ucast_groups): |
| 378 | buckets=[] |
| 379 | for group in l3_ucast_groups: |
| 380 | buckets.append(ofp.bucket(actions=[ofp.action.group(group)])) |
| 381 | |
| 382 | group_id =encode_l3_ecmp_group_id(id) |
| 383 | request = ofp.message.group_modify(group_type=ofp.OFPGT_SELECT, |
| 384 | group_id=group_id, |
| 385 | buckets=buckets |
| 386 | ) |
| 387 | ctrl.message_send(request) |
| 388 | return request |
| 389 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 390 | def add_l3_mcast_group(ctrl, vid, mcast_group_id, groups_on_buckets): |
| 391 | buckets=[] |
| 392 | for group in groups_on_buckets: |
| 393 | buckets.append(ofp.bucket(actions=[ofp.action.group(group)])) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 394 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 395 | group_id =encode_l3_mcast_group_id(vid, mcast_group_id) |
| 396 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 397 | group_id=group_id, |
| 398 | buckets=buckets |
| 399 | ) |
| 400 | ctrl.message_send(request) |
| 401 | return request |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 402 | |
| 403 | def add_l2_overlay_flood_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index): |
| 404 | buckets=[] |
| 405 | for port in ports: |
| 406 | buckets.append(ofp.bucket(actions=[ofp.action.output(port)])) |
| 407 | |
| 408 | group_id=encode_l2_overlay_group_id(tunnel_id, 0, index) |
| 409 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 410 | group_id=group_id, |
| 411 | buckets=buckets |
| 412 | ) |
| 413 | ctrl.message_send(request) |
| 414 | return request |
| 415 | |
| 416 | def add_l2_overlay_flood_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index): |
| 417 | buckets=[] |
| 418 | for port in ports: |
| 419 | buckets.append(ofp.bucket(actions=[ofp.action.output(port)])) |
| 420 | |
| 421 | group_id=encode_l2_overlay_group_id(tunnel_id, 1, index) |
| 422 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 423 | group_id=group_id, |
| 424 | buckets=buckets |
| 425 | ) |
| 426 | ctrl.message_send(request) |
| 427 | return request |
| 428 | |
| 429 | def add_l2_overlay_mcast_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index): |
| 430 | buckets=[] |
| 431 | for port in ports: |
| 432 | buckets.append(ofp.bucket(actions=[ofp.action.output(port)])) |
| 433 | |
| 434 | group_id=encode_l2_overlay_group_id(tunnel_id, 2, index) |
| 435 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 436 | group_id=group_id, |
| 437 | buckets=buckets |
| 438 | ) |
| 439 | ctrl.message_send(request) |
| 440 | return request |
| 441 | |
| 442 | def add_l2_overlay_mcast_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index): |
| 443 | buckets=[] |
| 444 | for port in ports: |
| 445 | buckets.append(ofp.bucket(actions=[ofp.action.output(port)])) |
| 446 | |
| 447 | group_id=encode_l2_overlay_group_id(tunnel_id, 3, index) |
| 448 | request = ofp.message.group_add(group_type=ofp.OFPGT_ALL, |
| 449 | group_id=group_id, |
| 450 | buckets=buckets |
| 451 | ) |
| 452 | ctrl.message_send(request) |
| 453 | return request |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 454 | |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 455 | def add_port_table_flow(ctrl, is_overlay=True): |
| 456 | match = ofp.match() |
| 457 | |
| 458 | if is_overlay == True: |
| 459 | match.oxm_list.append(ofp.oxm.in_port(0x10000)) |
macauley | dbff327 | 2015-07-30 14:07:16 +0800 | [diff] [blame] | 460 | NEXT_TABLE=50 |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 461 | else: |
| 462 | match.oxm_list.append(ofp.oxm.in_port(0)) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 463 | NEXT_TABLE=10 |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 464 | |
| 465 | request = ofp.message.flow_add( |
| 466 | table_id=0, |
| 467 | cookie=42, |
| 468 | match=match, |
| 469 | instructions=[ |
macauley | dbff327 | 2015-07-30 14:07:16 +0800 | [diff] [blame] | 470 | ofp.instruction.goto_table(NEXT_TABLE) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 471 | ], |
| 472 | priority=0) |
| 473 | logging.info("Add port table, match port %lx" % 0x10000) |
| 474 | ctrl.message_send(request) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 475 | |
Flavio Castro | d8f8af2 | 2015-12-02 18:19:26 -0500 | [diff] [blame] | 476 | def pop_vlan_flow(ctrl, ports, vlan_id=1): |
| 477 | # table 10: vlan |
| 478 | # goto to table 20 |
| 479 | msgs=[] |
| 480 | for of_port in ports: |
| 481 | match = ofp.match() |
| 482 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 483 | match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id)) |
| 484 | request = ofp.message.flow_add( |
| 485 | table_id=10, |
| 486 | cookie=42, |
| 487 | match=match, |
| 488 | instructions=[ |
| 489 | ofp.instruction.apply_actions( |
| 490 | actions=[ |
| 491 | ofp.action.pop_vlan() |
| 492 | ] |
| 493 | ), |
| 494 | ofp.instruction.goto_table(20) |
| 495 | ], |
| 496 | priority=0) |
| 497 | logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port)) |
| 498 | ctrl.message_send(request) |
| 499 | |
| 500 | |
| 501 | return msgs |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 502 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 503 | def add_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False): |
| 504 | # table 10: vlan |
| 505 | # goto to table 20 |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 506 | msgs=[] |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 507 | for of_port in ports: |
Flavio Castro | 932014b | 2016-01-05 18:29:15 -0500 | [diff] [blame] | 508 | 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] | 509 | match = ofp.match() |
| 510 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 511 | match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id)) |
| 512 | request = ofp.message.flow_add( |
| 513 | table_id=10, |
| 514 | cookie=42, |
| 515 | match=match, |
| 516 | instructions=[ |
Flavio Castro | d8f8af2 | 2015-12-02 18:19:26 -0500 | [diff] [blame] | 517 | ofp.instruction.apply_actions( |
| 518 | actions=[ |
| 519 | ofp.action.pop_vlan() |
| 520 | ] |
| 521 | ), |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 522 | ofp.instruction.goto_table(20) |
| 523 | ], |
| 524 | priority=0) |
| 525 | logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port)) |
| 526 | ctrl.message_send(request) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 527 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 528 | if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH): |
| 529 | match = ofp.match() |
| 530 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
Flavio Castro | 1229631 | 2015-12-15 17:48:26 -0500 | [diff] [blame] | 531 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff)) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 532 | request = ofp.message.flow_add( |
| 533 | table_id=10, |
| 534 | cookie=42, |
| 535 | match=match, |
| 536 | instructions=[ |
| 537 | ofp.instruction.apply_actions( |
| 538 | actions=[ |
Flavio Castro | af2b450 | 2016-02-02 17:41:32 -0500 | [diff] [blame] | 539 | ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 540 | ] |
| 541 | ), |
| 542 | ofp.instruction.goto_table(20) |
| 543 | ], |
| 544 | priority=0) |
| 545 | logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port)) |
| 546 | ctrl.message_send(request) |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 547 | msgs.append(request) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 548 | |
Flavio Castro | 932014b | 2016-01-05 18:29:15 -0500 | [diff] [blame] | 549 | if (flag == 4) : |
| 550 | match = ofp.match() |
| 551 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 552 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1fff)) |
| 553 | request = ofp.message.flow_add( |
| 554 | table_id=10, |
| 555 | cookie=42, |
| 556 | match=match, |
| 557 | instructions=[ |
| 558 | ofp.instruction.apply_actions( |
| 559 | actions=[ |
Flavio Castro | af2b450 | 2016-02-02 17:41:32 -0500 | [diff] [blame] | 560 | ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)) |
Flavio Castro | 932014b | 2016-01-05 18:29:15 -0500 | [diff] [blame] | 561 | ] |
| 562 | ), |
| 563 | ofp.instruction.goto_table(20) |
| 564 | ], |
| 565 | priority=0) |
| 566 | logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port)) |
| 567 | ctrl.message_send(request) |
| 568 | msgs.append(request) |
| 569 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 570 | if send_barrier: |
| 571 | do_barrier(ctrl) |
| 572 | |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 573 | return msgs |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 574 | |
macauley_cheng | 6e6a612 | 2015-11-16 14:19:18 +0800 | [diff] [blame] | 575 | def del_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False): |
| 576 | # table 10: vlan |
| 577 | # goto to table 20 |
| 578 | msgs=[] |
| 579 | for of_port in ports: |
| 580 | if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH): |
| 581 | match = ofp.match() |
| 582 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 583 | match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id)) |
| 584 | request = ofp.message.flow_delete( |
| 585 | table_id=10, |
| 586 | cookie=42, |
| 587 | match=match, |
| 588 | priority=0) |
| 589 | logging.info("Del vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port)) |
| 590 | ctrl.message_send(request) |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 591 | |
macauley_cheng | 6e6a612 | 2015-11-16 14:19:18 +0800 | [diff] [blame] | 592 | if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH): |
| 593 | match = ofp.match() |
| 594 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 595 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0xfff)) |
| 596 | request = ofp.message.flow_delete( |
| 597 | table_id=10, |
| 598 | cookie=42, |
| 599 | match=match, |
| 600 | priority=0) |
| 601 | logging.info("Del vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port)) |
| 602 | ctrl.message_send(request) |
| 603 | msgs.append(request) |
| 604 | |
| 605 | if send_barrier: |
| 606 | do_barrier(ctrl) |
| 607 | |
| 608 | return msgs |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 609 | |
macauley_cheng | 6b31161 | 2015-09-04 11:32:27 +0800 | [diff] [blame] | 610 | def add_vlan_table_flow_pvid(ctrl, in_port, match_vid=None, pvid=1, send_barrier=False): |
| 611 | """it will tag pack as untagged packet wether it has tagg or not""" |
| 612 | match = ofp.match() |
| 613 | match.oxm_list.append(ofp.oxm.in_port(in_port)) |
| 614 | actions=[] |
| 615 | if match_vid == None: |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 616 | match.oxm_list.append(ofp.oxm.vlan_vid(0)) |
macauley_cheng | 6b31161 | 2015-09-04 11:32:27 +0800 | [diff] [blame] | 617 | actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid))) |
| 618 | goto_table=20 |
| 619 | else: |
| 620 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+match_vid, 0x1fff)) |
| 621 | actions.append(ofp.action.push_vlan(0x8100)) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 622 | actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid))) |
macauley_cheng | 6b31161 | 2015-09-04 11:32:27 +0800 | [diff] [blame] | 623 | goto_table=20 |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 624 | |
macauley_cheng | 6b31161 | 2015-09-04 11:32:27 +0800 | [diff] [blame] | 625 | request = ofp.message.flow_add( |
| 626 | table_id=10, |
| 627 | cookie=42, |
| 628 | match=match, |
| 629 | instructions=[ |
| 630 | ofp.instruction.apply_actions(actions=actions) |
| 631 | ,ofp.instruction.goto_table(goto_table) |
| 632 | ], |
| 633 | priority=0) |
| 634 | logging.info("Add PVID %d on port %d and go to table %ld" %( pvid, in_port, goto_table)) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 635 | ctrl.message_send(request) |
| 636 | |
macauley_cheng | 6b31161 | 2015-09-04 11:32:27 +0800 | [diff] [blame] | 637 | if send_barrier: |
| 638 | do_barrier(ctrl) |
| 639 | |
| 640 | def add_vlan_table_flow_allow_all_vlan(ctrl, in_port, send_barrier=False): |
| 641 | """it st flow allow all vlan tag on this port""" |
| 642 | match = ofp.match() |
| 643 | match.oxm_list.append(ofp.oxm.in_port(in_port)) |
| 644 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1000)) |
| 645 | request = ofp.message.flow_add( |
| 646 | table_id=10, |
| 647 | cookie=42, |
| 648 | match=match, |
| 649 | instructions=[ |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 650 | ofp.instruction.goto_table(20) |
macauley_cheng | 6b31161 | 2015-09-04 11:32:27 +0800 | [diff] [blame] | 651 | ], |
| 652 | priority=0) |
| 653 | logging.info("Add allow all vlan on port %d " %(in_port)) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 654 | ctrl.message_send(request) |
macauley_cheng | 6b31161 | 2015-09-04 11:32:27 +0800 | [diff] [blame] | 655 | |
Pier | 7b031af | 2016-08-25 15:00:22 -0700 | [diff] [blame] | 656 | def add_one_vlan_table_flow_translation(ctrl, of_port, vlan_id=1, new_vlan_id=-1, vrf=0, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False): |
| 657 | # Install a flow for VLAN translation |
| 658 | # in VLAN table. |
| 659 | # table 10: vlan |
| 660 | # goto to table 20 |
| 661 | if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4): |
| 662 | match = ofp.match() |
| 663 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 664 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff)) |
| 665 | |
| 666 | actions=[] |
| 667 | if vrf!=0: |
| 668 | actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))) |
| 669 | if new_vlan_id != -1: |
| 670 | actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_vlan_id))) |
| 671 | |
| 672 | request = ofp.message.flow_add( |
| 673 | table_id=10, |
| 674 | cookie=42, |
| 675 | match=match, |
| 676 | instructions=[ |
| 677 | ofp.instruction.apply_actions( |
| 678 | actions=actions |
| 679 | ), |
| 680 | ofp.instruction.goto_table(20) |
| 681 | ], |
| 682 | priority=0) |
| 683 | logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port)) |
| 684 | ctrl.message_send(request) |
| 685 | |
Pier | e130876 | 2016-09-12 15:29:56 -0700 | [diff] [blame] | 686 | def add_one_vlan_table_flow(ctrl, of_port, out_vlan_id=1, 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] | 687 | # table 10: vlan |
| 688 | # goto to table 20 |
Flavio Castro | 932014b | 2016-01-05 18:29:15 -0500 | [diff] [blame] | 689 | 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] | 690 | match = ofp.match() |
| 691 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
Flavio Castro | 1229631 | 2015-12-15 17:48:26 -0500 | [diff] [blame] | 692 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff)) |
macauley | 7f89d96 | 2015-08-06 18:13:48 +0800 | [diff] [blame] | 693 | |
| 694 | actions=[] |
| 695 | if vrf!=0: |
| 696 | actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))) |
Pier | b5da4c9 | 2016-09-21 11:23:35 -0700 | [diff] [blame] | 697 | if mpls_type!=None: |
| 698 | actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=mpls_type))) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 699 | |
Flavio Castro | 6d49852 | 2015-12-15 14:05:04 -0500 | [diff] [blame] | 700 | #actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id))) |
macauley | 7f89d96 | 2015-08-06 18:13:48 +0800 | [diff] [blame] | 701 | |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 702 | request = ofp.message.flow_add( |
| 703 | table_id=10, |
| 704 | cookie=42, |
| 705 | match=match, |
| 706 | instructions=[ |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 707 | ofp.instruction.apply_actions( |
macauley | 7f89d96 | 2015-08-06 18:13:48 +0800 | [diff] [blame] | 708 | actions=actions |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 709 | ), |
| 710 | ofp.instruction.goto_table(20) |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 711 | ], |
| 712 | priority=0) |
| 713 | logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port)) |
| 714 | ctrl.message_send(request) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 715 | |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 716 | if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH): |
| 717 | match = ofp.match() |
| 718 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
Flavio Castro | 1229631 | 2015-12-15 17:48:26 -0500 | [diff] [blame] | 719 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff)) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 720 | |
macauley | 7f89d96 | 2015-08-06 18:13:48 +0800 | [diff] [blame] | 721 | actions=[] |
| 722 | if vrf!=0: |
| 723 | actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 724 | |
Flavio Castro | 91d1a55 | 2016-05-17 16:59:44 -0700 | [diff] [blame] | 725 | actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 726 | |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 727 | request = ofp.message.flow_add( |
| 728 | table_id=10, |
| 729 | cookie=42, |
| 730 | match=match, |
| 731 | instructions=[ |
| 732 | ofp.instruction.apply_actions( |
macauley | 7f89d96 | 2015-08-06 18:13:48 +0800 | [diff] [blame] | 733 | actions=actions |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 734 | ), |
| 735 | ofp.instruction.goto_table(20) |
| 736 | ], |
| 737 | priority=0) |
| 738 | logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port)) |
| 739 | ctrl.message_send(request) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 740 | |
Flavio Castro | 932014b | 2016-01-05 18:29:15 -0500 | [diff] [blame] | 741 | if (flag == 4) : |
| 742 | match = ofp.match() |
| 743 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 744 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000,0x1fff)) |
| 745 | |
| 746 | actions=[] |
| 747 | if vrf!=0: |
| 748 | actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))) |
| 749 | |
Flavio Castro | 91d1a55 | 2016-05-17 16:59:44 -0700 | [diff] [blame] | 750 | 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] | 751 | |
| 752 | request = ofp.message.flow_add( |
| 753 | table_id=10, |
| 754 | cookie=42, |
| 755 | match=match, |
| 756 | instructions=[ |
| 757 | ofp.instruction.apply_actions( |
| 758 | actions=actions |
| 759 | ), |
| 760 | ofp.instruction.goto_table(20) |
| 761 | ], |
| 762 | priority=0) |
| 763 | logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port)) |
| 764 | ctrl.message_send(request) |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 765 | |
Pier | e130876 | 2016-09-12 15:29:56 -0700 | [diff] [blame] | 766 | if (flag == VLAN_TABLE_FLAG_ONLY_STACKED): |
| 767 | # This flag is meant to managed stacked vlan packtes |
| 768 | # Matches on outer VLAN_ID, set OVID with outer VLAN. |
| 769 | # Finally expose inner VLAN_ID with a pop action and |
| 770 | # goto VLAN_1_FLOW_TABLE |
| 771 | match = ofp.match() |
| 772 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 773 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff)) |
| 774 | |
| 775 | actions=[] |
| 776 | actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_OVID, value=0x1000+vlan_id))) |
| 777 | actions.append(ofp.action.pop_vlan()) |
| 778 | |
| 779 | request = ofp.message.flow_add( |
| 780 | table_id=10, |
| 781 | cookie=42, |
| 782 | match=match, |
| 783 | instructions=[ |
| 784 | ofp.instruction.apply_actions( |
| 785 | actions=actions |
| 786 | ), |
| 787 | ofp.instruction.goto_table(VLAN_1_FLOW_TABLE) |
| 788 | ], |
| 789 | priority=0) |
| 790 | logging.info("Add vlan %d tagged packets on port %d and go to table %d" %( vlan_id, of_port, VLAN_1_FLOW_TABLE)) |
| 791 | ctrl.message_send(request) |
| 792 | |
| 793 | if (flag == VLAN_TABLE_FLAG_PRIORITY) : |
| 794 | match = ofp.match() |
| 795 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 796 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1fff)) |
| 797 | request = ofp.message.flow_add( |
| 798 | table_id=10, |
| 799 | cookie=42, |
| 800 | match=match, |
| 801 | instructions=[ |
| 802 | ofp.instruction.apply_actions( |
| 803 | actions=[ |
| 804 | ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)), |
| 805 | ofp.action.push_vlan(0x8100), |
| 806 | ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+out_vlan_id)), |
| 807 | ] |
| 808 | ), |
| 809 | ofp.instruction.goto_table(20) |
| 810 | ], |
| 811 | priority=0) |
| 812 | logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port)) |
| 813 | ctrl.message_send(request) |
| 814 | |
| 815 | if send_barrier: |
| 816 | do_barrier(ctrl) |
| 817 | |
| 818 | return request |
| 819 | |
Pier | cf76e80 | 2016-09-19 20:16:35 -0700 | [diff] [blame] | 820 | def add_one_vlan_table_flow_pw(ctrl, of_port, tunnel_index, new_vlan_id=1, vlan_id=1, vrf=0, flag=VLAN_TABLE_FLAG_ONLY_TAG, send_barrier=False): |
| 821 | # table 10: vlan |
| 822 | # goto to table 13 |
| 823 | if flag == VLAN_TABLE_FLAG_ONLY_TAG: |
| 824 | match = ofp.match() |
| 825 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 826 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id, 0x1fff)) |
| 827 | |
| 828 | actions=[] |
| 829 | if vlan_id == -1: |
| 830 | actions.append(ofp.action.pop_vlan()) |
| 831 | if new_vlan_id > 1: |
| 832 | actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_vlan_id))) |
| 833 | actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS))) |
| 834 | actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE))) |
| 835 | # 0x0000nnnn is for UNI interfaces |
| 836 | actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port))) |
| 837 | |
| 838 | request = ofp.message.flow_add( |
| 839 | table_id=10, |
| 840 | cookie=42, |
| 841 | match=match, |
| 842 | instructions=[ |
| 843 | ofp.instruction.apply_actions( |
| 844 | actions=actions |
| 845 | ), |
| 846 | ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE) |
| 847 | ], |
| 848 | priority=0) |
| 849 | logging.info("Add vlan %d tagged packets on port %d and go to table %d" % (vlan_id, of_port, MPLS_L2_PORT_FLOW_TABLE)) |
| 850 | ctrl.message_send(request) |
| 851 | |
| 852 | if flag == VLAN_TABLE_FLAG_ONLY_UNTAG: |
| 853 | match = ofp.match() |
| 854 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 855 | match.oxm_list.append(ofp.oxm.vlan_vid(0)) |
| 856 | |
| 857 | actions=[] |
| 858 | if vlan_id > 1: |
| 859 | actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))) |
| 860 | actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS))) |
| 861 | actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE))) |
| 862 | # 0x0000nnnn is for UNI interfaces |
| 863 | actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port))) |
| 864 | |
| 865 | request = ofp.message.flow_add( |
| 866 | table_id=10, |
| 867 | cookie=42, |
| 868 | match=match, |
| 869 | instructions=[ |
| 870 | ofp.instruction.apply_actions( |
| 871 | actions=actions |
| 872 | ), |
| 873 | ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE) |
| 874 | ], |
| 875 | priority=0) |
| 876 | logging.info("Add vlan %d untagged packets on port %d and go to table %d" % (vlan_id, of_port, MPLS_L2_PORT_FLOW_TABLE)) |
| 877 | ctrl.message_send(request) |
| 878 | |
| 879 | if send_barrier: |
| 880 | do_barrier(ctrl) |
| 881 | |
| 882 | return request |
| 883 | |
Pier | e130876 | 2016-09-12 15:29:56 -0700 | [diff] [blame] | 884 | def add_one_vlan_1_table_flow(ctrl, of_port, new_outer_vlan_id=-1, outer_vlan_id=1, inner_vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_TAG, send_barrier=False): |
| 885 | # table 11: vlan 1 table |
| 886 | # goto to table 20 |
| 887 | if flag == VLAN_TABLE_FLAG_ONLY_TAG: |
| 888 | match = ofp.match() |
| 889 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 890 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff)) |
| 891 | match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id)) |
| 892 | |
| 893 | actions=[] |
| 894 | actions.append(ofp.action.push_vlan(0x8100)) |
| 895 | if new_outer_vlan_id != -1: |
| 896 | actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_outer_vlan_id))) |
| 897 | else: |
| 898 | actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan_id))) |
| 899 | |
| 900 | request = ofp.message.flow_add( |
| 901 | table_id=11, |
| 902 | cookie=42, |
| 903 | match=match, |
| 904 | instructions=[ |
| 905 | ofp.instruction.apply_actions( |
| 906 | actions=actions |
| 907 | ), |
| 908 | ofp.instruction.goto_table(TERMINATION_FLOW_TABLE) |
| 909 | ], |
| 910 | priority=0) |
| 911 | logging.info("Add vlan 1 double tagged %d-%d packets on port %d and go to table %d" %( outer_vlan_id, inner_vlan_id, of_port, TERMINATION_FLOW_TABLE)) |
| 912 | ctrl.message_send(request) |
| 913 | |
| 914 | if flag == VLAN_TABLE_FLAG_ONLY_UNTAG: |
| 915 | match = ofp.match() |
| 916 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 917 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff)) |
| 918 | match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id)) |
| 919 | |
| 920 | actions=[] |
| 921 | request = ofp.message.flow_add( |
| 922 | table_id=11, |
| 923 | cookie=42, |
| 924 | match=match, |
| 925 | instructions=[ |
| 926 | ofp.instruction.apply_actions( |
| 927 | actions=actions |
| 928 | ), |
| 929 | ofp.instruction.goto_table(TERMINATION_FLOW_TABLE) |
| 930 | ], |
| 931 | priority=0) |
| 932 | logging.info("Add vlan 1 double tagged %d-%d packets on port %d and go to table %d" %( outer_vlan_id, inner_vlan_id, of_port, TERMINATION_FLOW_TABLE)) |
| 933 | ctrl.message_send(request) |
| 934 | |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 935 | if send_barrier: |
| 936 | do_barrier(ctrl) |
| 937 | |
| 938 | return request |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 939 | |
Pier | cf76e80 | 2016-09-19 20:16:35 -0700 | [diff] [blame] | 940 | def add_one_vlan_1_table_flow_pw(ctrl, of_port, tunnel_index, new_outer_vlan_id=-1, outer_vlan_id=1, inner_vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_TAG, send_barrier=False): |
| 941 | # table 11: vlan 1 table |
| 942 | # goto to table 13 |
| 943 | match = ofp.match() |
| 944 | match.oxm_list.append(ofp.oxm.in_port(of_port)) |
| 945 | match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff)) |
| 946 | match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id)) |
| 947 | |
| 948 | actions=[] |
| 949 | actions.append(ofp.action.push_vlan(0x8100)) |
| 950 | if new_outer_vlan_id != -1: |
| 951 | actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_outer_vlan_id))) |
| 952 | else: |
| 953 | actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan_id))) |
| 954 | |
| 955 | actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS))) |
| 956 | actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE))) |
| 957 | # 0x0000nnnn is for UNI interfaces |
| 958 | actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port))) |
| 959 | |
| 960 | request = ofp.message.flow_add( |
| 961 | table_id=11, |
| 962 | cookie=42, |
| 963 | match=match, |
| 964 | instructions=[ |
| 965 | ofp.instruction.apply_actions( |
| 966 | actions=actions |
| 967 | ), |
| 968 | ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE) |
| 969 | ], |
| 970 | priority=0) |
| 971 | logging.info("Add vlan 1 double tagged %d-%d packets on port %d and go to table %d" %( outer_vlan_id, inner_vlan_id, of_port, MPLS_L2_PORT_FLOW_TABLE)) |
| 972 | ctrl.message_send(request) |
| 973 | |
| 974 | if send_barrier: |
| 975 | do_barrier(ctrl) |
| 976 | |
| 977 | return request |
| 978 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 979 | def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False): |
| 980 | match = ofp.match() |
castroflavio | 2189448 | 2015-12-08 15:29:55 -0500 | [diff] [blame] | 981 | priority=500 |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 982 | if dst_mac!=None: |
castroflavio | 2189448 | 2015-12-08 15:29:55 -0500 | [diff] [blame] | 983 | priority=1000 |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 984 | match.oxm_list.append(ofp.oxm.eth_dst(dst_mac)) |
| 985 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 986 | match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid)) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 987 | |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 988 | request = ofp.message.flow_add( |
| 989 | table_id=50, |
| 990 | cookie=42, |
| 991 | match=match, |
| 992 | instructions=[ |
| 993 | ofp.instruction.write_actions( |
| 994 | actions=[ |
| 995 | ofp.action.group(group_id)]), |
| 996 | ofp.instruction.goto_table(60) |
| 997 | ], |
| 998 | buffer_id=ofp.OFP_NO_BUFFER, |
castroflavio | 2189448 | 2015-12-08 15:29:55 -0500 | [diff] [blame] | 999 | priority=priority) |
macauley | 9755723 | 2015-07-16 17:28:07 +0800 | [diff] [blame] | 1000 | |
| 1001 | logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac) |
| 1002 | ctrl.message_send(request) |
| 1003 | |
| 1004 | if send_barrier: |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1005 | do_barrier(ctrl) |
macauley | 15909e7 | 2015-07-17 15:58:57 +0800 | [diff] [blame] | 1006 | |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1007 | return request |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1008 | |
| 1009 | def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False): |
| 1010 | match = ofp.match() |
| 1011 | if dst_mac!=None: |
| 1012 | match.oxm_list.append(ofp.oxm.eth_dst(dst_mac)) |
| 1013 | |
| 1014 | match.oxm_list.append(ofp.oxm.tunnel_id(vnid)) |
| 1015 | if is_group == True: |
| 1016 | actions=[ofp.action.group(group_id)] |
| 1017 | else: |
| 1018 | actions=[ofp.action.output(group_id)] |
| 1019 | |
| 1020 | request = ofp.message.flow_add( |
| 1021 | table_id=50, |
| 1022 | cookie=42, |
| 1023 | match=match, |
| 1024 | instructions=[ |
| 1025 | ofp.instruction.write_actions( |
| 1026 | actions=actions), |
| 1027 | ofp.instruction.goto_table(60) |
| 1028 | ], |
| 1029 | buffer_id=ofp.OFP_NO_BUFFER, |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1030 | priority=1000) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1031 | |
| 1032 | logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac) |
| 1033 | ctrl.message_send(request) |
| 1034 | |
| 1035 | if send_barrier: |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1036 | do_barrier(ctrl) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1037 | |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1038 | return request |
| 1039 | |
macauley_cheng | 6b13366 | 2015-11-09 13:52:39 +0800 | [diff] [blame] | 1040 | 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] | 1041 | match = ofp.match() |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 1042 | match.oxm_list.append(ofp.oxm.eth_type(eth_type)) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1043 | if dst_mac[0]&0x01 == 0x01: |
| 1044 | match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00])) |
| 1045 | goto_table=40 |
| 1046 | else: |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 1047 | if in_port!=0: |
| 1048 | match.oxm_list.append(ofp.oxm.in_port(in_port)) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1049 | match.oxm_list.append(ofp.oxm.eth_dst(dst_mac)) |
| 1050 | match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid)) |
macauley_cheng | 6b13366 | 2015-11-09 13:52:39 +0800 | [diff] [blame] | 1051 | if goto_table == None: |
| 1052 | goto_table=30 |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 1053 | |
| 1054 | request = ofp.message.flow_add( |
| 1055 | table_id=20, |
| 1056 | cookie=42, |
| 1057 | match=match, |
| 1058 | instructions=[ |
| 1059 | ofp.instruction.goto_table(goto_table) |
| 1060 | ], |
| 1061 | buffer_id=ofp.OFP_NO_BUFFER, |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1062 | priority=1) |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 1063 | |
| 1064 | logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac) |
| 1065 | ctrl.message_send(request) |
| 1066 | |
| 1067 | if send_barrier: |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1068 | do_barrier(ctrl) |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 1069 | |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1070 | return request |
| 1071 | |
Flavio Castro | af2b450 | 2016-02-02 17:41:32 -0500 | [diff] [blame] | 1072 | 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] | 1073 | match = ofp.match() |
| 1074 | match.oxm_list.append(ofp.oxm.eth_type(eth_type)) |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 1075 | if vrf != 0: |
| 1076 | 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] | 1077 | |
Flavio Castro | af2b450 | 2016-02-02 17:41:32 -0500 | [diff] [blame] | 1078 | match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask)) |
| 1079 | |
| 1080 | instructions = [] |
| 1081 | instructions.append(ofp.instruction.goto_table(60)) |
| 1082 | if send_ctrl: |
Pier | e0918ec | 2016-09-09 20:06:05 -0700 | [diff] [blame] | 1083 | instructions.append(ofp.instruction.apply_actions( |
Flavio Castro | af2b450 | 2016-02-02 17:41:32 -0500 | [diff] [blame] | 1084 | actions=[ofp.action.output( port=ofp.OFPP_CONTROLLER, |
| 1085 | max_len=ofp.OFPCML_NO_BUFFER)])) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1086 | else: |
Flavio Castro | af2b450 | 2016-02-02 17:41:32 -0500 | [diff] [blame] | 1087 | instructions.append(ofp.instruction.write_actions( |
| 1088 | actions=[ofp.action.group(action_group_id)])) |
macauley | 53d90fe | 2015-08-04 17:34:22 +0800 | [diff] [blame] | 1089 | |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 1090 | request = ofp.message.flow_add( |
| 1091 | table_id=30, |
| 1092 | cookie=42, |
| 1093 | match=match, |
Flavio Castro | af2b450 | 2016-02-02 17:41:32 -0500 | [diff] [blame] | 1094 | instructions=instructions, |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 1095 | buffer_id=ofp.OFP_NO_BUFFER, |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1096 | priority=1) |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 1097 | |
| 1098 | logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip) |
| 1099 | ctrl.message_send(request) |
| 1100 | |
| 1101 | if send_barrier: |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1102 | do_barrier(ctrl) |
macauley | 0f91a3e | 2015-07-17 18:09:59 +0800 | [diff] [blame] | 1103 | |
Flavio Castro | 9debaaa | 2016-07-26 19:37:50 -0700 | [diff] [blame] | 1104 | return request |
Flavio Castro | d8f8af2 | 2015-12-02 18:19:26 -0500 | [diff] [blame] | 1105 | |
Flavio Castro | 8ca5254 | 2016-04-11 11:24:49 -0400 | [diff] [blame] | 1106 | 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] | 1107 | match = ofp.match() |
| 1108 | match.oxm_list.append(ofp.oxm.eth_type(0x8847)) |
| 1109 | match.oxm_list.append(ofp.oxm.mpls_label(label)) |
| 1110 | match.oxm_list.append(ofp.oxm.mpls_bos(bos)) |
Pier | b5da4c9 | 2016-09-21 11:23:35 -0700 | [diff] [blame] | 1111 | actions = [] |
Flavio Castro | b702a2f | 2016-04-10 22:01:48 -0400 | [diff] [blame] | 1112 | actions = [ofp.action.dec_mpls_ttl(), |
Flavio Castro | 8ca5254 | 2016-04-11 11:24:49 -0400 | [diff] [blame] | 1113 | ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))] |
| 1114 | if (goto_table == 29): |
Flavio Castro | 8ca5254 | 2016-04-11 11:24:49 -0400 | [diff] [blame] | 1115 | actions.append(ofp.action.group(action_group_id)) |
| 1116 | else: |
Flavio Castro | d80fbc3 | 2016-07-25 15:54:26 -0700 | [diff] [blame] | 1117 | actions.append(ofp.action.set_field( |
| 1118 | ofp.oxm.exp2ByteValue(exp_type=23, value=32))) |
| 1119 | actions.append(ofp.action.group(action_group_id)) |
Flavio Castro | 8ca5254 | 2016-04-11 11:24:49 -0400 | [diff] [blame] | 1120 | actions.append(ofp.action.copy_ttl_in()) |
Flavio Castro | 9debaaa | 2016-07-26 19:37:50 -0700 | [diff] [blame] | 1121 | |
Flavio Castro | b702a2f | 2016-04-10 22:01:48 -0400 | [diff] [blame] | 1122 | request = ofp.message.flow_add( |
| 1123 | table_id=24, |
| 1124 | cookie=43, |
| 1125 | match=match, |
| 1126 | instructions=[ |
Flavio Castro | 8ca5254 | 2016-04-11 11:24:49 -0400 | [diff] [blame] | 1127 | ofp.instruction.apply_actions(actions=actions), |
| 1128 | ofp.instruction.goto_table(goto_table) |
Flavio Castro | b702a2f | 2016-04-10 22:01:48 -0400 | [diff] [blame] | 1129 | ], |
| 1130 | buffer_id=ofp.OFP_NO_BUFFER, |
| 1131 | priority=1) |
Flavio Castro | b702a2f | 2016-04-10 22:01:48 -0400 | [diff] [blame] | 1132 | logging.info("Inserting MPLS flow , label %ld", label) |
| 1133 | ctrl.message_send(request) |
| 1134 | |
| 1135 | if send_barrier: |
| 1136 | do_barrier(ctrl) |
| 1137 | |
| 1138 | return request |
| 1139 | |
Pier | f6f2816 | 2016-09-22 16:30:52 -0700 | [diff] [blame^] | 1140 | def add_mpls_flow_pw(ctrl, action_group_id, label, ethertype, bos, tunnel_index, goto_table=MPLS_TYPE_FLOW_TABLE, popMPLS=True, popL2=False, of_port=0, send_barrier=False): |
Pier | b5da4c9 | 2016-09-21 11:23:35 -0700 | [diff] [blame] | 1141 | match = ofp.match() |
| 1142 | match.oxm_list.append(ofp.oxm.eth_type(0x8847)) |
| 1143 | match.oxm_list.append(ofp.oxm.mpls_label(label)) |
| 1144 | match.oxm_list.append(ofp.oxm.mpls_bos(bos)) |
| 1145 | |
| 1146 | actions = [] |
| 1147 | actions.append(ofp.action.dec_mpls_ttl()) |
Pier | f6f2816 | 2016-09-22 16:30:52 -0700 | [diff] [blame^] | 1148 | if popMPLS == True: |
Pier | b5da4c9 | 2016-09-21 11:23:35 -0700 | [diff] [blame] | 1149 | actions.append(ofp.action.copy_ttl_in()) |
| 1150 | actions.append(ofp.action.pop_mpls(ethertype)) |
Pier | f6f2816 | 2016-09-22 16:30:52 -0700 | [diff] [blame^] | 1151 | if bos==1 and popL2 == True: |
| 1152 | actions.append(ofp.action.ofdpa_pop_l2_header()) |
| 1153 | actions.append(ofp.action.ofdpa_pop_cw()) |
| 1154 | actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE))) |
| 1155 | # 0x0002nnnn is for UNI interfaces |
| 1156 | actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00020000 + of_port))) |
| 1157 | actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS))) |
Pier | b5da4c9 | 2016-09-21 11:23:35 -0700 | [diff] [blame] | 1158 | actions.append(ofp.action.group(action_group_id)) |
| 1159 | |
| 1160 | request = ofp.message.flow_add( |
| 1161 | table_id=24, |
| 1162 | cookie=43, |
| 1163 | match=match, |
| 1164 | instructions=[ |
| 1165 | ofp.instruction.apply_actions(actions=actions), |
| 1166 | ofp.instruction.goto_table(goto_table) |
| 1167 | ], |
| 1168 | buffer_id=ofp.OFP_NO_BUFFER, |
| 1169 | priority=1 |
| 1170 | ) |
| 1171 | logging.info("Inserting MPLS flow , label %ld", label) |
| 1172 | ctrl.message_send(request) |
| 1173 | |
| 1174 | if send_barrier: |
| 1175 | do_barrier(ctrl) |
| 1176 | |
| 1177 | return request |
| 1178 | |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1179 | def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False): |
| 1180 | match = ofp.match() |
| 1181 | match.oxm_list.append(ofp.oxm.eth_type(0x0800)) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1182 | match.oxm_list.append(ofp.oxm.vlan_vid(vlan_id)) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1183 | if src_ip_mask!=0: |
| 1184 | match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask)) |
| 1185 | else: |
| 1186 | match.oxm_list.append(ofp.oxm.ipv4_src(src_ip)) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1187 | |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1188 | match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip)) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1189 | |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1190 | request = ofp.message.flow_add( |
| 1191 | table_id=40, |
| 1192 | cookie=42, |
| 1193 | match=match, |
| 1194 | instructions=[ |
| 1195 | ofp.instruction.write_actions( |
| 1196 | actions=[ofp.action.group(action_group_id)]), |
| 1197 | ofp.instruction.goto_table(60) |
| 1198 | ], |
| 1199 | buffer_id=ofp.OFP_NO_BUFFER, |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1200 | priority=1) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1201 | |
| 1202 | logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask) |
| 1203 | ctrl.message_send(request) |
| 1204 | |
| 1205 | if send_barrier: |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1206 | do_barrier(ctrl) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1207 | |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1208 | return request |
macauley_cheng | 6b13366 | 2015-11-09 13:52:39 +0800 | [diff] [blame] | 1209 | |
| 1210 | #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] | 1211 | 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] | 1212 | match = ofp.match() |
| 1213 | match.oxm_list.append(ofp.oxm.eth_type(eth_type)) |
| 1214 | match.oxm_list.append(ofp.oxm.ipv4_dst(ip_dst)) |
| 1215 | match.oxm_list.append(ofp.oxm.ip_proto(ip_proto)) |
| 1216 | match.oxm_list.append(ofp.oxm.tcp_dst(tcp_dst)) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1217 | |
macauley_cheng | 6b13366 | 2015-11-09 13:52:39 +0800 | [diff] [blame] | 1218 | request = ofp.message.flow_add( |
| 1219 | table_id=28, |
| 1220 | cookie=42, |
| 1221 | match=match, |
| 1222 | instructions=[ |
| 1223 | ofp.instruction.write_actions( |
| 1224 | actions=[ofp.action.set_field(ofp.oxm.ipv4_dst(set_ip_dst)), |
| 1225 | ofp.action.set_field(ofp.oxm.tcp_dst(set_tcp_dst)), |
| 1226 | ofp.action.group(action_group_id)]), |
| 1227 | ofp.instruction.goto_table(60) |
| 1228 | ], |
| 1229 | buffer_id=ofp.OFP_NO_BUFFER, |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1230 | priority=1) |
macauley_cheng | 6b13366 | 2015-11-09 13:52:39 +0800 | [diff] [blame] | 1231 | 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) |
| 1232 | ctrl.message_send(request) |
| 1233 | return request |
macauley_cheng | effc20a | 2015-11-09 16:14:56 +0800 | [diff] [blame] | 1234 | |
| 1235 | #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 |
| 1236 | def add_snat_flow(ctrl, eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src): |
| 1237 | match = ofp.match() |
| 1238 | match.oxm_list.append(ofp.oxm.eth_type(eth_type)) |
| 1239 | match.oxm_list.append(ofp.oxm.ipv4_src(ip_src)) |
| 1240 | match.oxm_list.append(ofp.oxm.ip_proto(ip_proto)) |
| 1241 | match.oxm_list.append(ofp.oxm.tcp_src(tcp_src)) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1242 | |
macauley_cheng | effc20a | 2015-11-09 16:14:56 +0800 | [diff] [blame] | 1243 | request = ofp.message.flow_add( |
| 1244 | table_id=29, |
| 1245 | cookie=42, |
| 1246 | match=match, |
| 1247 | instructions=[ |
| 1248 | ofp.instruction.write_actions( |
| 1249 | actions=[ofp.action.set_field(ofp.oxm.ipv4_src(set_ip_src)), |
| 1250 | ofp.action.set_field(ofp.oxm.tcp_src(set_tcp_src))]), |
| 1251 | ofp.instruction.goto_table(30) |
| 1252 | ], |
| 1253 | buffer_id=ofp.OFP_NO_BUFFER, |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1254 | priority=1) |
macauley_cheng | effc20a | 2015-11-09 16:14:56 +0800 | [diff] [blame] | 1255 | 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) |
| 1256 | ctrl.message_send(request) |
| 1257 | return request |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1258 | |
| 1259 | def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'): |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1260 | """ |
| 1261 | Command Example: |
| 1262 | of-agent vtap 10001 ethernet 1/1 vid 1 |
| 1263 | of-agent vtp 10001 vni 10 |
| 1264 | """ |
| 1265 | if vlan != 0: |
| 1266 | config_vtap_xml=""" |
| 1267 | <config> |
| 1268 | <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 1269 | <id>capable-switch-1</id> |
| 1270 | <resources> |
| 1271 | <port xc:operation="OPERATION"> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1272 | <resource-id >LPORT</resource-id> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1273 | <features> |
| 1274 | <current> |
| 1275 | <rate>10Gb</rate> |
| 1276 | <medium>fiber</medium> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1277 | <pause>symmetric</pause> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1278 | </current> |
| 1279 | <advertised> |
| 1280 | <rate>10Gb</rate> |
| 1281 | <rate>100Gb</rate> |
| 1282 | <medium>fiber</medium> |
| 1283 | <pause>symmetric</pause> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1284 | </advertised> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1285 | <supported> |
| 1286 | <rate>10Gb</rate> |
| 1287 | <rate>100Gb</rate> |
| 1288 | <medium>fiber</medium> |
| 1289 | <pause>symmetric</pause> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1290 | </supported> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1291 | <advertised-peer> |
| 1292 | <rate>10Gb</rate> |
| 1293 | <rate>100Gb</rate> |
| 1294 | <medium>fiber</medium> |
| 1295 | <pause>symmetric</pause> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1296 | </advertised-peer> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1297 | </features> |
| 1298 | <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 1299 | <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port> |
| 1300 | <ofdpa10:vid>VLAN_ID</ofdpa10:vid> |
| 1301 | <ofdpa10:vni>VNID</ofdpa10:vni> |
| 1302 | </ofdpa10:vtap> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1303 | </port> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1304 | </resources> |
| 1305 | <logical-switches> |
| 1306 | <switch> |
| 1307 | <id>DATAPATH_ID</id> |
| 1308 | <datapath-id>DATAPATH_ID</datapath-id> |
| 1309 | <resources> |
| 1310 | <port xc:operation="OPERATION">LPORT</port> |
| 1311 | </resources> |
| 1312 | </switch> |
| 1313 | </logical-switches> |
| 1314 | </capable-switch> |
| 1315 | </config> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1316 | """ |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1317 | else: |
| 1318 | config_vtap_xml=""" |
| 1319 | <config> |
| 1320 | <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 1321 | <id>capable-switch-1</id> |
| 1322 | <resources> |
| 1323 | <port xc:operation="OPERATION"> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1324 | <resource-id >LPORT</resource-id> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1325 | <features> |
| 1326 | <current> |
| 1327 | <rate>10Gb</rate> |
| 1328 | <medium>fiber</medium> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1329 | <pause>symmetric</pause> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1330 | </current> |
| 1331 | <advertised> |
| 1332 | <rate>10Gb</rate> |
| 1333 | <rate>100Gb</rate> |
| 1334 | <medium>fiber</medium> |
| 1335 | <pause>symmetric</pause> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1336 | </advertised> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1337 | <supported> |
| 1338 | <rate>10Gb</rate> |
| 1339 | <rate>100Gb</rate> |
| 1340 | <medium>fiber</medium> |
| 1341 | <pause>symmetric</pause> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1342 | </supported> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1343 | <advertised-peer> |
| 1344 | <rate>10Gb</rate> |
| 1345 | <rate>100Gb</rate> |
| 1346 | <medium>fiber</medium> |
| 1347 | <pause>symmetric</pause> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1348 | </advertised-peer> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1349 | </features> |
| 1350 | <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 1351 | <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port> |
| 1352 | <ofdpa10:vni>VNID</ofdpa10:vni> |
| 1353 | </ofdpa10:vtap> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1354 | </port> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1355 | </resources> |
| 1356 | <logical-switches> |
| 1357 | <switch> |
| 1358 | <id>DATAPATH_ID</id> |
| 1359 | <datapath-id>DATAPATH_ID</datapath-id> |
| 1360 | <resources> |
| 1361 | <port xc:operation="OPERATION">LPORT</port> |
| 1362 | </resources> |
| 1363 | </switch> |
| 1364 | </logical-switches> |
| 1365 | </capable-switch> |
| 1366 | </config> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1367 | """ |
| 1368 | str_datapath_id_f= "{:016x}".format(dp_id) |
| 1369 | str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)]) |
| 1370 | config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id) |
| 1371 | config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport))) |
| 1372 | config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port)) |
| 1373 | config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan)) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1374 | config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid)) |
| 1375 | config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation)) |
| 1376 | return config_vtap_xml |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1377 | |
| 1378 | 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'): |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1379 | """ |
| 1380 | Command Example: |
| 1381 | of-agent vtep 10002 source user-input-src-ip destination user-input-dst-ip udp-source-port 6633 nexthop 2 ttl 25 |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1382 | of-agent vtp 10001 vni 10 |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1383 | """ |
| 1384 | |
| 1385 | config_vtep_xml=""" |
| 1386 | <config> |
| 1387 | <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 1388 | <id>capable-switch-1</id> |
| 1389 | <resources> |
| 1390 | <port xc:operation="OPERATION"> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1391 | <resource-id>LPORT</resource-id> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1392 | <features> |
| 1393 | <current> |
| 1394 | <rate>10Gb</rate> |
| 1395 | <medium>fiber</medium> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1396 | <pause>symmetric</pause> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1397 | </current> |
| 1398 | <advertised> |
| 1399 | <rate>10Gb</rate> |
| 1400 | <rate>100Gb</rate> |
| 1401 | <medium>fiber</medium> |
| 1402 | <pause>symmetric</pause> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1403 | </advertised> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1404 | <supported> |
| 1405 | <rate>10Gb</rate> |
| 1406 | <rate>100Gb</rate> |
| 1407 | <medium>fiber</medium> |
| 1408 | <pause>symmetric</pause> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1409 | </supported> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1410 | <advertised-peer> |
| 1411 | <rate>10Gb</rate> |
| 1412 | <rate>100Gb</rate> |
| 1413 | <medium>fiber</medium> |
| 1414 | <pause>symmetric</pause> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1415 | </advertised-peer> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1416 | </features> |
| 1417 | <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01"> |
| 1418 | <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip> |
| 1419 | <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip> |
| 1420 | <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port> |
macauley | 25999cf | 2015-08-07 17:03:24 +0800 | [diff] [blame] | 1421 | <ofdpa10:vni xc:operation="OPERATION"> |
| 1422 | <ofdpa10:id>VNID</ofdpa10:id> |
| 1423 | </ofdpa10:vni> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1424 | <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id> |
| 1425 | <ofdpa10:ttl>TTL</ofdpa10:ttl> |
| 1426 | </ofdpa10:vtep> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1427 | </port> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1428 | </resources> |
| 1429 | <logical-switches> |
| 1430 | <switch> |
| 1431 | <id>DATAPATH_ID</id> |
| 1432 | <datapath-id>DATAPATH_ID</datapath-id> |
| 1433 | <resources> |
| 1434 | <port xc:operation="OPERATION">LPORT</port> |
| 1435 | </resources> |
| 1436 | </switch> |
| 1437 | </logical-switches> |
| 1438 | </capable-switch> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1439 | </config> |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1440 | """ |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1441 | str_datapath_id_f= "{:016x}".format(dp_id) |
| 1442 | str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)]) |
| 1443 | config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id) |
macauley | 25999cf | 2015-08-07 17:03:24 +0800 | [diff] [blame] | 1444 | config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport))) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1445 | config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip)) |
| 1446 | config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip)) |
| 1447 | config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port)) |
| 1448 | config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id)) |
| 1449 | config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl)) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1450 | config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid)) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1451 | config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation)) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1452 | |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1453 | return config_vtep_xml |
| 1454 | |
| 1455 | def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'): |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1456 | #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2 |
| 1457 | config_nexthop_xml=""" |
| 1458 | <config> |
| 1459 | <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 1460 | <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 1461 | <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id> |
| 1462 | <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac> |
| 1463 | <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port> |
| 1464 | <ofdpa10:vid>VLAN_ID</ofdpa10:vid> |
| 1465 | </ofdpa10:next-hop> |
| 1466 | </of11-config:capable-switch> |
| 1467 | </config> |
| 1468 | """ |
| 1469 | config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan)) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1470 | config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port)) |
| 1471 | config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id)) |
| 1472 | config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac)) |
| 1473 | config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation)) |
| 1474 | return config_nexthop_xml |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1475 | |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1476 | def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'): |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1477 | #of-agent vni 10 multicast 224.1.1.1 nexthop 20 |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1478 | if mcast_ipv4!=None: |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1479 | config_vni_xml=""" |
| 1480 | <config> |
| 1481 | <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 1482 | <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 1483 | <ofdpa10:id>VNID</ofdpa10:id> |
| 1484 | <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group> |
| 1485 | <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id> |
| 1486 | </ofdpa10:vni> |
| 1487 | </of11-config:capable-switch> |
| 1488 | </config> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1489 | """ |
| 1490 | config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id)) |
| 1491 | config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4)) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1492 | else: |
| 1493 | config_vni_xml=""" |
| 1494 | <config> |
| 1495 | <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0"> |
| 1496 | <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION"> |
| 1497 | <ofdpa10:id>VNID</ofdpa10:id> |
| 1498 | </ofdpa10:vni> |
| 1499 | </of11-config:capable-switch> |
| 1500 | </config> |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1501 | """ |
| 1502 | |
| 1503 | config_vni_xml=config_vni_xml.replace("VNID", str(vni_id)) |
| 1504 | config_vni_xml=config_vni_xml.replace("OPERATION", str(operation)) |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1505 | return config_vni_xml |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1506 | |
| 1507 | def get_featureReplay(self): |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1508 | req = ofp.message.features_request() |
| 1509 | res, raw = self.controller.transact(req) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1510 | self.assertIsNotNone(res, "Did not receive a response from the DUT.") |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1511 | self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY, |
| 1512 | ("Unexpected packet type %d received in response to " |
| 1513 | "OFPT_FEATURES_REQUEST") % res.type) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1514 | return res |
| 1515 | |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1516 | def send_edit_config(switch_ip, xml, target='runing'): |
| 1517 | NETCONF_ACCOUNT="netconfuser" |
| 1518 | NETCONF_PASSWD="netconfuser" |
| 1519 | with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m: |
| 1520 | try: |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1521 | m.edit_config(target='running', |
| 1522 | config=xml, |
| 1523 | default_operation='merge', |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1524 | error_option='stop-on-error') |
| 1525 | |
| 1526 | except Exception as e: |
| 1527 | logging.info("Fail to set xml %s", xml) |
| 1528 | return False |
| 1529 | |
| 1530 | #return m.get_config(source='running').data_xml |
| 1531 | return True |
| 1532 | |
| 1533 | def send_delete_config(switch_ip, xml, target='runing'): |
| 1534 | NETCONF_ACCOUNT="netconfuser" |
| 1535 | NETCONF_PASSWD="netconfuser" |
| 1536 | with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m: |
| 1537 | try: |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1538 | m.edit_config(target='running', |
| 1539 | config=xml, |
| 1540 | default_operation='delete', |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1541 | error_option='stop-on-error') |
| 1542 | |
| 1543 | except Exception as e: |
| 1544 | logging.info("Fail to set xml %s", xml) |
| 1545 | return False |
| 1546 | |
| 1547 | #return m.get_config(source='running').data_xml |
| 1548 | return True |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1549 | |
macauley | fddc466 | 2015-07-27 17:40:30 +0800 | [diff] [blame] | 1550 | def get_edit_config(switch_ip, target='runing'): |
| 1551 | NETCONF_ACCOUNT="netconfuser" |
| 1552 | NETCONF_PASSWD="netconfuser" |
| 1553 | 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] | 1554 | return m.get_config(source='running').data_xml |
macauley | dbff327 | 2015-07-30 14:07:16 +0800 | [diff] [blame] | 1555 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1556 | |
| 1557 | """ |
| 1558 | MPLS |
| 1559 | """ |
| 1560 | |
| 1561 | OFDPA_MPLS_SUBTYPE_SHIFT=24 |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1562 | OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1 |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1563 | OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2 |
| 1564 | OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3 |
| 1565 | OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4 |
| 1566 | OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5 |
| 1567 | OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6 |
| 1568 | OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8 |
| 1569 | OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10 |
| 1570 | |
Pier | cf76e80 | 2016-09-19 20:16:35 -0700 | [diff] [blame] | 1571 | |
| 1572 | |
| 1573 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1574 | def encode_mpls_interface_group_id(subtype, index): |
| 1575 | index=index&0x00ffffff |
| 1576 | assert(subtype==0) |
| 1577 | return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT) |
| 1578 | |
| 1579 | def encode_mpls_label_group_id(subtype, index): |
| 1580 | index=index&0x00ffffff |
| 1581 | assert(subtype <=5 or subtype==0) |
| 1582 | #1: l2 vpn label |
| 1583 | #2: l3 vpn label |
| 1584 | #3: mpls tunnel label 1 |
| 1585 | #4: mpls tunnel lable 2 |
| 1586 | #5: mpls swap label |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1587 | return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1588 | |
| 1589 | def encode_mpls_forwarding_group_id(subtype, index): |
| 1590 | index=index&0x00ffffff |
| 1591 | assert(subtype==6 or subtype==8 or subtype==10) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1592 | return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1593 | |
| 1594 | |
| 1595 | def add_mpls_intf_group(ctrl, ref_gid, dst_mac, src_mac, vid, index, subtype=0): |
| 1596 | action=[] |
| 1597 | action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac))) |
| 1598 | action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac))) |
Pier | cf76e80 | 2016-09-19 20:16:35 -0700 | [diff] [blame] | 1599 | if vid != 1: |
| 1600 | action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vid))) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1601 | action.append(ofp.action.group(ref_gid)) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1602 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1603 | buckets = [ofp.bucket(actions=action)] |
| 1604 | |
| 1605 | mpls_group_id =encode_mpls_interface_group_id(subtype, index) |
| 1606 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 1607 | group_id=mpls_group_id, |
| 1608 | buckets=buckets |
| 1609 | ) |
| 1610 | ctrl.message_send(request) |
| 1611 | return mpls_group_id, request |
| 1612 | |
Pier | cf76e80 | 2016-09-19 20:16:35 -0700 | [diff] [blame] | 1613 | def add_mpls_tunnel_label_group( |
| 1614 | ctrl, |
| 1615 | ref_gid, |
| 1616 | subtype, |
| 1617 | index, |
| 1618 | label, |
| 1619 | ): |
| 1620 | |
| 1621 | action=[] |
| 1622 | action.append(ofp.action.push_mpls(0x8847)) |
| 1623 | action.append(ofp.action.set_field(ofp.oxm.mpls_label(label))) |
| 1624 | action.append(ofp.action.group(ref_gid)) |
| 1625 | buckets = [ofp.bucket(actions=action)] |
| 1626 | |
| 1627 | mpls_group_id = encode_mpls_label_group_id(subtype, index) |
| 1628 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 1629 | group_id=mpls_group_id, |
| 1630 | buckets=buckets |
| 1631 | ) |
| 1632 | ctrl.message_send(request) |
| 1633 | |
| 1634 | return mpls_group_id, request |
| 1635 | |
Pier | b5da4c9 | 2016-09-21 11:23:35 -0700 | [diff] [blame] | 1636 | def add_mpls_swap_label_group( |
| 1637 | ctrl, |
| 1638 | ref_gid, |
| 1639 | subtype, |
| 1640 | index, |
| 1641 | label, |
| 1642 | ): |
| 1643 | |
| 1644 | action=[] |
| 1645 | action.append(ofp.action.set_field(ofp.oxm.mpls_label(label))) |
| 1646 | action.append(ofp.action.group(ref_gid)) |
| 1647 | buckets = [ofp.bucket(actions=action)] |
| 1648 | |
| 1649 | mpls_group_id = encode_mpls_label_group_id(subtype, index) |
| 1650 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 1651 | group_id=mpls_group_id, |
| 1652 | buckets=buckets |
| 1653 | ) |
| 1654 | ctrl.message_send(request) |
| 1655 | |
| 1656 | return mpls_group_id, request |
| 1657 | |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1658 | def add_mpls_label_group(ctrl, subtype, index, ref_gid, |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1659 | lmep_id=-1, |
| 1660 | qos_index=-1, |
| 1661 | push_l2_header=False, |
| 1662 | push_vlan=False, |
| 1663 | push_mpls_header=False, |
| 1664 | push_cw=False, |
| 1665 | set_mpls_label=None, |
| 1666 | set_bos=None, |
| 1667 | set_tc=None, |
| 1668 | set_tc_from_table=False, |
| 1669 | cpy_tc_outward=False, |
| 1670 | set_ttl=None, |
| 1671 | cpy_ttl_outward=False, |
| 1672 | oam_lm_tx_count=False, |
| 1673 | set_pri_from_table=False |
| 1674 | ): |
| 1675 | """ |
| 1676 | @ref_gid: only can be mpls intf group or mpls tunnel label 1/2 group |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1677 | """ |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1678 | action=[] |
| 1679 | |
| 1680 | if push_vlan== True: |
| 1681 | action.append(ofp.action.push_vlan(0x8100)) |
| 1682 | if push_mpls_header== True: |
| 1683 | action.append(ofp.action.push_mpls(0x8847)) |
| 1684 | if set_mpls_label != None: |
| 1685 | action.append(ofp.action.set_field(ofp.oxm.mpls_label(set_mpls_label))) |
| 1686 | if set_bos != None: |
| 1687 | action.append(ofp.action.set_field(ofp.oxm.mpls_bos(set_bos))) |
| 1688 | if set_tc != None: |
| 1689 | assert(set_tc_from_table==False) |
| 1690 | action.append(ofp.action.set_field(ofp.oxm.mpls_tc(set_tc))) |
| 1691 | if set_ttl != None: |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1692 | action.append(ofp.action.set_mpls_ttl(set_ttl)) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1693 | if cpy_ttl_outward == True: |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1694 | action.append(ofp.action.copy_ttl_out()) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1695 | """ |
| 1696 | ofdpa experimenter |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1697 | """ |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1698 | if push_l2_header== True: |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1699 | action.append(ofp.action.ofdpa_push_l2_header()) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1700 | if set_tc_from_table== True: |
| 1701 | assert(qos_index>=0) |
| 1702 | assert(set_tc == None) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1703 | action.append(ofp.action.ofdpa_set_tc_from_table(qos_index)) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1704 | if cpy_tc_outward == True: |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1705 | action.append(ofp.action.ofdpa_copy_tc_out()) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1706 | if oam_lm_tx_count == True: |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1707 | assert(qos_index>=0 and lmep_id>=0) |
| 1708 | action.append(ofp.action.ofdpa_oam_lm_tx_count(lmep_id, qos_index)) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1709 | if set_pri_from_table == True: |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1710 | assert(qos_index>=0) |
| 1711 | action.append(ofp.action.ofdpa_set_qos_from_table(qos_index)) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1712 | if push_cw == True: |
| 1713 | action.append(ofp.action.ofdpa_push_cw()) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1714 | |
| 1715 | action.append(ofp.action.group(ref_gid)) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1716 | buckets = [ofp.bucket(actions=action)] |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1717 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1718 | mpls_group_id = encode_mpls_label_group_id(subtype, index) |
| 1719 | request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT, |
| 1720 | group_id=mpls_group_id, |
| 1721 | buckets=buckets |
| 1722 | ) |
| 1723 | ctrl.message_send(request) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1724 | |
| 1725 | return mpls_group_id, request |
| 1726 | |
Pier | cf76e80 | 2016-09-19 20:16:35 -0700 | [diff] [blame] | 1727 | def add_mpls_l2_port_flow(ctrl, of_port, mpls_l2_port, tunnel_index, ref_gid, qos_index=0): |
| 1728 | """ |
| 1729 | Only action is Group, which must indicate one of: |
| 1730 | MPLS L2 VPN Label or Fast Failover Protection Group. |
| 1731 | ref_gid contains this information |
| 1732 | """ |
| 1733 | tunnel_id = tunnel_index + ofp.oxm.TUNNEL_ID_BASE |
| 1734 | |
| 1735 | match = ofp.match() |
| 1736 | match.oxm_list.append(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)) |
| 1737 | match.oxm_list.append(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)) |
| 1738 | |
| 1739 | action = [] |
| 1740 | action.append(ofp.action.group(ref_gid)) |
| 1741 | assert(qos_index>=0) |
| 1742 | action.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_QOS_INDEX, value=qos_index))) |
| 1743 | |
| 1744 | request = ofp.message.flow_add( |
| 1745 | table_id=MPLS_L2_PORT_FLOW_TABLE, |
| 1746 | cookie=42, |
| 1747 | match=match, |
| 1748 | instructions=[ |
| 1749 | ofp.instruction.write_actions( |
| 1750 | actions=action |
| 1751 | ), |
| 1752 | ofp.instruction.goto_table(MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE) ], |
| 1753 | buffer_id=ofp.OFP_NO_BUFFER, |
| 1754 | priority=1) |
| 1755 | logging.info("Inserting flow %d mpls_l2_port, %d tunnel_id, action %x group and go to table %d", mpls_l2_port, tunnel_id, ref_gid, MPLS_L2_PORT_DSCP_TRUST_FLOW_TABLE) |
| 1756 | ctrl.message_send(request) |
| 1757 | return request |
| 1758 | |
| 1759 | return |
| 1760 | |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1761 | def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids, |
| 1762 | watch_port=None, |
| 1763 | watch_group=ofp.OFPP_ANY, |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1764 | push_vlan=None, |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1765 | pop_vlan=None, |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1766 | set_vid=None): |
| 1767 | assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP |
| 1768 | or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP |
| 1769 | or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG) |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1770 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1771 | buckets=[] |
| 1772 | if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP: |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1773 | group_type = ofp.OFPGT_FF |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1774 | for gid in ref_gids: |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1775 | action=[] |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1776 | action.append(ofp.action.group(gid)) |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1777 | buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action)) |
| 1778 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1779 | elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP: |
| 1780 | group_type = ofp.OFPGT_SELECT |
| 1781 | for gid in ref_gids: |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1782 | action=[] |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1783 | action.append(ofp.action.group(gid)) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1784 | buckets.append(ofp.bucket(actions=action)) |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1785 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1786 | elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG: |
| 1787 | group_type = ofp.OFPGT_INDIRECT |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1788 | action=[] |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1789 | if set_vid!=None: |
Flavio Castro | 91d1a55 | 2016-05-17 16:59:44 -0700 | [diff] [blame] | 1790 | 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] | 1791 | if push_vlan!=None: |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1792 | action.append(ofp.action.push_vlan(push_vlan)) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1793 | if pop_vlan!=None: |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1794 | action.append(ofp.action.pop_vlan()) |
| 1795 | action.append(ofp.action.group(ref_gids[0])) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1796 | buckets.append(ofp.bucket(actions=action)) |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1797 | |
| 1798 | mpls_group_id = encode_mpls_forwarding_group_id(subtype, index) |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1799 | request = ofp.message.group_add(group_type=group_type, |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1800 | group_id=mpls_group_id, |
| 1801 | buckets=buckets |
| 1802 | ) |
| 1803 | ctrl.message_send(request) |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1804 | return mpls_group_id, request |
macauley_cheng | d17ce51 | 2015-08-31 17:45:51 +0800 | [diff] [blame] | 1805 | |
| 1806 | |
macauley_cheng | 67da926 | 2015-08-31 15:18:41 +0800 | [diff] [blame] | 1807 | """ |
Pier | cf76e80 | 2016-09-19 20:16:35 -0700 | [diff] [blame] | 1808 | display |
Pier | bbdf378 | 2016-08-22 17:58:26 -0700 | [diff] [blame] | 1809 | """ |
macauley | dbff327 | 2015-07-30 14:07:16 +0800 | [diff] [blame] | 1810 | def print_current_table_flow_stat(ctrl, table_id=0xff): |
| 1811 | stat_req=ofp.message.flow_stats_request() |
| 1812 | response, pkt = ctrl.transact(stat_req) |
| 1813 | if response == None: |
| 1814 | print "no response" |
| 1815 | return None |
| 1816 | print len(response.entries) |
| 1817 | for obj in response.entries: |
| 1818 | print "match ", obj.match |
| 1819 | print "cookie", obj.cookie |
| 1820 | print "priority", obj.priority |
| 1821 | print "idle_timeout", obj.idle_timeout |
| 1822 | print "hard_timeout", obj.hard_timeout |
| 1823 | #obj.actions |
Flavio Castro | 167f5bd | 2015-12-02 19:33:53 -0500 | [diff] [blame] | 1824 | print "packet count: %lx"%obj.packet_count |