blob: ef13c43155be56b1c98fd76c3ec40e58c6d3dcae [file] [log] [blame]
Matteo Scandoloa229eca2017-08-08 13:05:28 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
macauley97557232015-07-16 17:28:07 +080016import logging
17
18from oftest import config
19import oftest.base_tests as base_tests
20import ofp
21import time
22from oftest.testutils import *
Pier1e4e98e2016-10-26 14:36:05 -070023from oftest.parse import parse_ipv6
macauley97557232015-07-16 17:28:07 +080024
macauleyfddc4662015-07-27 17:40:30 +080025from ncclient import manager
26import ncclient
27
macauley97557232015-07-16 17:28:07 +080028OFDPA_GROUP_TYPE_SHIFT=28
29OFDPA_VLAN_ID_SHIFT =16
macauleyfddc4662015-07-27 17:40:30 +080030OFDPA_TUNNEL_ID_SHIFT =12
31OFDPA_TUNNEL_SUBTYPE_SHIFT=10
macauley97557232015-07-16 17:28:07 +080032
33#VLAN_TABLE_FLAGS
34VLAN_TABLE_FLAG_ONLY_UNTAG=1
35VLAN_TABLE_FLAG_ONLY_TAG =2
36VLAN_TABLE_FLAG_ONLY_BOTH =3
Piere1308762016-09-12 15:29:56 -070037VLAN_TABLE_FLAG_ONLY_STACKED=5
38VLAN_TABLE_FLAG_PRIORITY=6
39VLAN_TABLE_FLAG_ONLY_UNTAG_PRIORITY=7
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -070040VLAN_TABLE_FLAG_ONLY_POP_VLAN=8
macauley97557232015-07-16 17:28:07 +080041
macauleye8b140e2015-08-03 13:35:45 +080042PORT_FLOW_TABLE=0
43VLAN_FLOW_TABLE=10
Piere1308762016-09-12 15:29:56 -070044VLAN_1_FLOW_TABLE=11
Piercf76e802016-09-19 20:16:35 -070045MPLS_L2_PORT_FLOW_TABLE=13
46MPLS_L2_PORT_DSCP_TRUST_FLOW_TABLE=15
47MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE=16
macauleye8b140e2015-08-03 13:35:45 +080048TERMINATION_FLOW_TABLE=20
Piercf76e802016-09-19 20:16:35 -070049MPLS_TYPE_FLOW_TABLE=29
macauleye8b140e2015-08-03 13:35:45 +080050UCAST_ROUTING_FLOW_TABLE=30
51MCAST_ROUTING_FLOW_TABLE=40
52BRIDGE_FLOW_TABLE=50
53ACL_FLOW_TABLE=60
Jonghwan Hyunff0dfd52018-03-20 15:04:35 -070054EGRESS_TPID_FLOW_TABLE = 235
55
56ONF_EXPERIMENTER_ID = 0x4F4E4600
macauleye8b140e2015-08-03 13:35:45 +080057
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -070058EGRESS_VLAN_FLOW_TABLE=210
59EGRESS_VLAN_1_FLOW_TABLE=211
60EGRESS_MAINTENANCE_POINT_FLOW_TABLE=226
61EGRESS_DSCP_TABLE=230
62EGRESS_TPID_TABLE=235
63EGRESS_SOURCE_MAC_LEARNING_TABLE=254
64
macauleye8b140e2015-08-03 13:35:45 +080065def convertIP4toStr(ip_addr):
66 a=(ip_addr&0xff000000)>>24
67 b=(ip_addr&0x00ff0000)>>16
68 c=(ip_addr&0x0000ff00)>>8
69 d=(ip_addr&0x000000ff)
70 return str(a)+"."+str(b)+"."+str(c)+"."+str(d)
71
72def convertMACtoStr(mac):
73 if not isinstance(mac, list):
74 assert(0)
75
76 return ':'.join(['%02X' % x for x in mac])
77
macauley7f89d962015-08-06 18:13:48 +080078def getSwitchCpuMACFromDPID(dpid):
79 str_datapath_id_f= "{:016x}".format(dpid)
80 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
81 switch_cpu_mac_str=str_datapath_id[6:]
82 switch_cpu_mac = switch_cpu_mac_str.split(":")
83 switch_cpu_mac=[int(switch_cpu_mac[i],16) for i in range(0, len(switch_cpu_mac))]
84
85 return switch_cpu_mac_str, switch_cpu_mac
Pierbbdf3782016-08-22 17:58:26 -070086
macauley_cheng67da9262015-08-31 15:18:41 +080087def DumpGroup(stats, verify_group_stats, always_show=True):
88 if(len(stats) > len(verify_group_stats)):
89 min_len = len(verify_group_stats)
90 print "Stats Len is not the same, stats>verify_group_stats"
91 if(len(stats)< len(verify_group_stats)):
Pierbbdf3782016-08-22 17:58:26 -070092 min_len = len(stats)
macauley_cheng67da9262015-08-31 15:18:41 +080093 print "Stats Len is not the same, stats<verify_group_stats"
Pierbbdf3782016-08-22 17:58:26 -070094 else:
macauley_cheng67da9262015-08-31 15:18:41 +080095 min_len = len(stats)
macauleye8b140e2015-08-03 13:35:45 +080096
macauley_cheng67da9262015-08-31 15:18:41 +080097 print "\r\n"
98 for i in range(min_len):
99 gs = stats[i]
Pierbbdf3782016-08-22 17:58:26 -0700100 gv = verify_group_stats[i]
macauley_cheng67da9262015-08-31 15:18:41 +0800101 print "FromSwtich:(GID=%lx, TYPE=%lx)\r\nVerify :(GID=%lx, TYPE=%lx)"%(gs.group_id, gs.group_type, gv.group_id, gv.group_type)
102 if(len(gs.buckets) != len(gv.buckets)):
103 print "buckets len is not the same gs %lx, gv %lx",(len(gs.buckets), len(gv.buckets))
104
105 for j in range(len(gs.buckets)):
106 b1=gs.buckets[j]
Pierbbdf3782016-08-22 17:58:26 -0700107 b2=gv.buckets[j]
macauley_cheng67da9262015-08-31 15:18:41 +0800108 if(len(b1.actions) != len(b2.actions)):
109 print "action len is not the same"
110
111 for k in range(len(b1.actions)):
112 a1=b1.actions[k]
113 a2=b2.actions[k]
114 if(always_show == True):
115 print "a1:"+a1.show()
Pierbbdf3782016-08-22 17:58:26 -0700116 print "a2:"+a2.show()
macauley_cheng67da9262015-08-31 15:18:41 +0800117
118def AssertGroup(self, stats, verify_group_stats):
119 self.assertTrue(len(stats) ==len(verify_group_stats), "stats len is not the same")
120
121 for i in range(len(stats)):
122 gs = stats[i]
Pierbbdf3782016-08-22 17:58:26 -0700123 gv = verify_group_stats[i]
macauley_cheng67da9262015-08-31 15:18:41 +0800124 self.assertTrue(len(gs.buckets) == len(gv.buckets), "buckets len is not the same")
125
126 for j in range(len(gs.buckets)):
127 b1=gs.buckets[j]
Pierbbdf3782016-08-22 17:58:26 -0700128 b2=gv.buckets[j]
macauley_cheng67da9262015-08-31 15:18:41 +0800129 self.assertTrue(len(b1.actions) == len(b2.actions), "action len is not the same")
130
131 for k in range(len(b1.actions)):
132 a1=b1.actions[k]
133 a2=b2.actions[k]
134 self.assertEquals(a1, a2, "action is not the same")
Pierbbdf3782016-08-22 17:58:26 -0700135
macauley97557232015-07-16 17:28:07 +0800136def encode_l2_interface_group_id(vlan, id):
137 return id + (vlan << OFDPA_VLAN_ID_SHIFT)
138
139def encode_l2_rewrite_group_id(id):
140 return id + (1 << OFDPA_GROUP_TYPE_SHIFT)
141
142def encode_l3_unicast_group_id(id):
143 return id + (2 << OFDPA_GROUP_TYPE_SHIFT)
144
145def encode_l2_mcast_group_id(vlan, id):
146 return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (3 << OFDPA_GROUP_TYPE_SHIFT)
147
148def encode_l2_flood_group_id(vlan, id):
149 return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (4 << OFDPA_GROUP_TYPE_SHIFT)
Pierbbdf3782016-08-22 17:58:26 -0700150
macauley97557232015-07-16 17:28:07 +0800151def encode_l3_interface_group_id(id):
152 return id + (5 << OFDPA_GROUP_TYPE_SHIFT)
153
154def encode_l3_mcast_group_id(vlan, id):
155 return id + (vlan << OFDPA_VLAN_ID_SHIFT)+(6 << OFDPA_GROUP_TYPE_SHIFT)
156
157def encode_l3_ecmp_group_id(id):
158 return id + (7 << OFDPA_GROUP_TYPE_SHIFT)
159
Flavio Castro91d1a552016-05-17 16:59:44 -0700160def encode_l2_unfiltered_group_id(id):
161 return id + (11 << OFDPA_GROUP_TYPE_SHIFT)
162
macauleyfddc4662015-07-27 17:40:30 +0800163def encode_l2_overlay_group_id(tunnel_id, subtype, index):
164 tunnel_id=tunnel_id&0xffff #16 bits
165 subtype = subtype&3 #2 bits
166 index = index & 0x3f #10 bits
167 return index + (tunnel_id << OFDPA_TUNNEL_ID_SHIFT)+ (subtype<<OFDPA_TUNNEL_SUBTYPE_SHIFT)+(8 << OFDPA_GROUP_TYPE_SHIFT)
macauley97557232015-07-16 17:28:07 +0800168
Saurav Das5d1473d2018-07-12 11:02:56 -0700169def add_l2_unfiltered_group(ctrl, ports, send_barrier=False, allow_vlan_translation=1):
Flavio Castro91d1a552016-05-17 16:59:44 -0700170 # group table
171 # set up untag groups for each port
172 group_id_list=[]
173 msgs=[]
174 for of_port in ports:
175 # do stuff
176 group_id = encode_l2_unfiltered_group_id(of_port)
177 group_id_list.append(group_id)
178 actions = [ofp.action.output(of_port)]
Flavio Castro91d1a552016-05-17 16:59:44 -0700179
Saurav Das5d1473d2018-07-12 11:02:56 -0700180 actions.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=24, value=allow_vlan_translation)))
Flavio Castro91d1a552016-05-17 16:59:44 -0700181 buckets = [ofp.bucket(actions=actions)]
182 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
183 group_id=group_id,
184 buckets=buckets
185 )
186 ctrl.message_send(request)
187 msgs.append(request)
188
189 if send_barrier:
190 do_barrier(ctrl)
191
192 return group_id_list, msgs
193
Pierf6f28162016-09-22 16:30:52 -0700194def add_one_l2_unfiltered_group(ctrl, of_port, send_barrier=False):
195 # group table
196 # set up untag groups for each port
197 group_id = encode_l2_unfiltered_group_id(of_port)
198 actions = [ofp.action.output(of_port)]
199 actions.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=24, value=1)))
200
201 buckets = [ofp.bucket(actions=actions)]
202 request = ofp.message.group_add(
203 group_type=ofp.OFPGT_INDIRECT,
204 group_id=group_id,
205 buckets=buckets
206 )
207 ctrl.message_send(request)
208
209 if send_barrier:
210 do_barrier(ctrl)
211
212 return group_id, request
213
Flavio Castrod4c44d12015-12-08 14:44:18 -0500214def add_l2_interface_group(ctrl, ports, vlan_id=1, is_tagged=False, send_barrier=False):
macauley97557232015-07-16 17:28:07 +0800215 # group table
216 # set up untag groups for each port
macauley41904ed2015-07-16 17:38:35 +0800217 group_id_list=[]
macauley15909e72015-07-17 15:58:57 +0800218 msgs=[]
macauley97557232015-07-16 17:28:07 +0800219 for of_port in ports:
220 # do stuff
221 group_id = encode_l2_interface_group_id(vlan_id, of_port)
macauley41904ed2015-07-16 17:38:35 +0800222 group_id_list.append(group_id)
macauley97557232015-07-16 17:28:07 +0800223 if is_tagged:
224 actions = [
225 ofp.action.output(of_port),
Pierbbdf3782016-08-22 17:58:26 -0700226 ]
macauley97557232015-07-16 17:28:07 +0800227 else:
228 actions = [
229 ofp.action.pop_vlan(),
230 ofp.action.output(of_port),
231 ]
232
233 buckets = [
234 ofp.bucket(actions=actions),
235 ]
236
237 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
238 group_id=group_id,
239 buckets=buckets
240 )
241 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800242 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800243
244 if send_barrier:
245 do_barrier(ctrl)
Pierbbdf3782016-08-22 17:58:26 -0700246
macauley15909e72015-07-17 15:58:57 +0800247 return group_id_list, msgs
macauley97557232015-07-16 17:28:07 +0800248
Flavio Castrod4c44d12015-12-08 14:44:18 -0500249def add_one_l2_interface_group(ctrl, port, vlan_id=1, is_tagged=False, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800250 # group table
251 # set up untag groups for each port
252 group_id = encode_l2_interface_group_id(vlan_id, port)
253
254 if is_tagged:
255 actions = [
256 ofp.action.output(port),
Pierbbdf3782016-08-22 17:58:26 -0700257 ]
macauley0f91a3e2015-07-17 18:09:59 +0800258 else:
259 actions = [
260 ofp.action.pop_vlan(),
261 ofp.action.output(port),
262 ]
263
264 buckets = [
265 ofp.bucket(actions=actions),
266 ]
267
268 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
269 group_id=group_id,
270 buckets=buckets
271 )
272 ctrl.message_send(request)
273
274 if send_barrier:
275 do_barrier(ctrl)
Pierbbdf3782016-08-22 17:58:26 -0700276
macauley0f91a3e2015-07-17 18:09:59 +0800277 return group_id, request
Pierbbdf3782016-08-22 17:58:26 -0700278
macauley97557232015-07-16 17:28:07 +0800279def add_l2_mcast_group(ctrl, ports, vlanid, mcast_grp_index):
280 buckets=[]
281 for of_port in ports:
282 group_id = encode_l2_interface_group_id(vlanid, of_port)
283 action=[ofp.action.group(group_id)]
284 buckets.append(ofp.bucket(actions=action))
285
286 group_id =encode_l2_mcast_group_id(vlanid, mcast_grp_index)
287 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
288 group_id=group_id,
289 buckets=buckets
290 )
291 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800292 return request
macauley97557232015-07-16 17:28:07 +0800293
macauley15909e72015-07-17 15:58:57 +0800294def add_l2_flood_group(ctrl, ports, vlanid, id):
295 buckets=[]
296 for of_port in ports:
297 group_id = encode_l2_interface_group_id(vlanid, of_port)
298 action=[ofp.action.group(group_id)]
299 buckets.append(ofp.bucket(actions=action))
macauley97557232015-07-16 17:28:07 +0800300
macauley15909e72015-07-17 15:58:57 +0800301 group_id =encode_l2_flood_group_id(vlanid, id)
302 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
303 group_id=group_id,
304 buckets=buckets
305 )
306 ctrl.message_send(request)
307 return request
308
Flavio Castroa7162bb2016-07-25 17:30:30 -0700309def mod_l2_flood_group(ctrl, ports, vlanid, id):
310 buckets=[]
311 for of_port in ports:
312 group_id = encode_l2_interface_group_id(vlanid, of_port)
313 action=[ofp.action.group(group_id)]
314 buckets.append(ofp.bucket(actions=action))
315
316 group_id =encode_l2_flood_group_id(vlanid, id)
317 request = ofp.message.group_modify(group_type=ofp.OFPGT_ALL,
318 group_id=group_id,
319 buckets=buckets
320 )
321 ctrl.message_send(request)
322 return request
323
324
macauley15909e72015-07-17 15:58:57 +0800325def add_l2_rewrite_group(ctrl, port, vlanid, id, src_mac, dst_mac):
326 group_id = encode_l2_interface_group_id(vlanid, port)
327
328 action=[]
329 if src_mac is not None:
330 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
331
332 if dst_mac is not None:
333 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
334
Flavio Castro91d1a552016-05-17 16:59:44 -0700335 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
Pierbbdf3782016-08-22 17:58:26 -0700336
macauley15909e72015-07-17 15:58:57 +0800337 action.append(ofp.action.group(group_id))
Pierbbdf3782016-08-22 17:58:26 -0700338
macauley15909e72015-07-17 15:58:57 +0800339 buckets = [ofp.bucket(actions=action)]
340
341 group_id =encode_l2_rewrite_group_id(id)
342 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
343 group_id=group_id,
344 buckets=buckets
345 )
346 ctrl.message_send(request)
347 return request
Pierbbdf3782016-08-22 17:58:26 -0700348
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700349def add_l3_unicast_group(ctrl, port, vlanid, id, src_mac, dst_mac, send_barrier=False, gid=None):
350
351 if (not gid):
352 group_id = encode_l2_interface_group_id(vlanid, port)
353 else:
354 group_id = gid
macauley15909e72015-07-17 15:58:57 +0800355
356 action=[]
357 if src_mac is not None:
358 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
359
360 if dst_mac is not None:
361 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
362
Flavio Castroaf2b4502016-02-02 17:41:32 -0500363 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
Pierbbdf3782016-08-22 17:58:26 -0700364
macauley15909e72015-07-17 15:58:57 +0800365 action.append(ofp.action.group(group_id))
Pierbbdf3782016-08-22 17:58:26 -0700366
macauley15909e72015-07-17 15:58:57 +0800367 buckets = [ofp.bucket(actions=action)]
368
369 group_id =encode_l3_unicast_group_id(id)
370 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
371 group_id=group_id,
372 buckets=buckets
373 )
374 ctrl.message_send(request)
Pier1e4e98e2016-10-26 14:36:05 -0700375
376 if send_barrier:
377 do_barrier(ctrl)
378
macauley15909e72015-07-17 15:58:57 +0800379 return request
Pierbbdf3782016-08-22 17:58:26 -0700380
macauley15909e72015-07-17 15:58:57 +0800381def add_l3_interface_group(ctrl, port, vlanid, id, src_mac):
382 group_id = encode_l2_interface_group_id(vlanid, port)
383
384 action=[]
385 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
Pierbbdf3782016-08-22 17:58:26 -0700386 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
macauley15909e72015-07-17 15:58:57 +0800387 action.append(ofp.action.group(group_id))
Pierbbdf3782016-08-22 17:58:26 -0700388
macauley15909e72015-07-17 15:58:57 +0800389 buckets = [ofp.bucket(actions=action)]
390
391 group_id =encode_l3_interface_group_id(id)
392 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
393 group_id=group_id,
394 buckets=buckets
395 )
396 ctrl.message_send(request)
397 return request
398
Pier1e4e98e2016-10-26 14:36:05 -0700399def add_l3_ecmp_group(ctrl, id, l3_ucast_groups, send_barrier=False):
macauley15909e72015-07-17 15:58:57 +0800400 buckets=[]
401 for group in l3_ucast_groups:
402 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
403
404 group_id =encode_l3_ecmp_group_id(id)
405 request = ofp.message.group_add(group_type=ofp.OFPGT_SELECT,
406 group_id=group_id,
407 buckets=buckets
408 )
409 ctrl.message_send(request)
Pier1e4e98e2016-10-26 14:36:05 -0700410
411 if send_barrier:
412 do_barrier(ctrl)
413
macauley15909e72015-07-17 15:58:57 +0800414 return request
Flavio Castroa7162bb2016-07-25 17:30:30 -0700415
416def mod_l3_ecmp_group(ctrl, id, l3_ucast_groups):
417 buckets=[]
418 for group in l3_ucast_groups:
419 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
420
421 group_id =encode_l3_ecmp_group_id(id)
422 request = ofp.message.group_modify(group_type=ofp.OFPGT_SELECT,
423 group_id=group_id,
424 buckets=buckets
425 )
426 ctrl.message_send(request)
427 return request
428
macauley15909e72015-07-17 15:58:57 +0800429def add_l3_mcast_group(ctrl, vid, mcast_group_id, groups_on_buckets):
430 buckets=[]
431 for group in groups_on_buckets:
432 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
Pierbbdf3782016-08-22 17:58:26 -0700433
macauley15909e72015-07-17 15:58:57 +0800434 group_id =encode_l3_mcast_group_id(vid, mcast_group_id)
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
macauleyfddc4662015-07-27 17:40:30 +0800441
442def add_l2_overlay_flood_over_unicast_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, 0, 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
454
455def add_l2_overlay_flood_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
456 buckets=[]
457 for port in ports:
458 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
459
460 group_id=encode_l2_overlay_group_id(tunnel_id, 1, index)
461 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
462 group_id=group_id,
463 buckets=buckets
464 )
465 ctrl.message_send(request)
466 return request
467
468def add_l2_overlay_mcast_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
469 buckets=[]
470 for port in ports:
471 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
472
473 group_id=encode_l2_overlay_group_id(tunnel_id, 2, index)
474 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
475 group_id=group_id,
476 buckets=buckets
477 )
478 ctrl.message_send(request)
479 return request
480
481def add_l2_overlay_mcast_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
482 buckets=[]
483 for port in ports:
484 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
485
486 group_id=encode_l2_overlay_group_id(tunnel_id, 3, index)
487 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
488 group_id=group_id,
489 buckets=buckets
490 )
491 ctrl.message_send(request)
492 return request
Pierbbdf3782016-08-22 17:58:26 -0700493
macauleyfddc4662015-07-27 17:40:30 +0800494def add_port_table_flow(ctrl, is_overlay=True):
495 match = ofp.match()
496
497 if is_overlay == True:
498 match.oxm_list.append(ofp.oxm.in_port(0x10000))
macauleydbff3272015-07-30 14:07:16 +0800499 NEXT_TABLE=50
macauleyfddc4662015-07-27 17:40:30 +0800500 else:
501 match.oxm_list.append(ofp.oxm.in_port(0))
Pierbbdf3782016-08-22 17:58:26 -0700502 NEXT_TABLE=10
macauleyfddc4662015-07-27 17:40:30 +0800503
504 request = ofp.message.flow_add(
Pier265ad5f2017-02-28 17:46:28 +0100505 table_id=0,
506 cookie=42,
507 match=match,
508 instructions=[
509 ofp.instruction.goto_table(NEXT_TABLE)
510 ],
511 priority=0)
macauleyfddc4662015-07-27 17:40:30 +0800512 logging.info("Add port table, match port %lx" % 0x10000)
513 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700514
Flavio Castrod8f8af22015-12-02 18:19:26 -0500515def pop_vlan_flow(ctrl, ports, vlan_id=1):
516 # table 10: vlan
517 # goto to table 20
518 msgs=[]
519 for of_port in ports:
520 match = ofp.match()
521 match.oxm_list.append(ofp.oxm.in_port(of_port))
522 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
523 request = ofp.message.flow_add(
524 table_id=10,
525 cookie=42,
526 match=match,
527 instructions=[
528 ofp.instruction.apply_actions(
529 actions=[
530 ofp.action.pop_vlan()
531 ]
532 ),
533 ofp.instruction.goto_table(20)
534 ],
535 priority=0)
536 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
537 ctrl.message_send(request)
538
539
540 return msgs
macauleyfddc4662015-07-27 17:40:30 +0800541
macauley97557232015-07-16 17:28:07 +0800542def add_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
543 # table 10: vlan
544 # goto to table 20
macauley15909e72015-07-17 15:58:57 +0800545 msgs=[]
macauley97557232015-07-16 17:28:07 +0800546 for of_port in ports:
Flavio Castro932014b2016-01-05 18:29:15 -0500547 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
macauley97557232015-07-16 17:28:07 +0800548 match = ofp.match()
549 match.oxm_list.append(ofp.oxm.in_port(of_port))
550 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
551 request = ofp.message.flow_add(
552 table_id=10,
553 cookie=42,
554 match=match,
555 instructions=[
Flavio Castrod8f8af22015-12-02 18:19:26 -0500556 ofp.instruction.apply_actions(
557 actions=[
558 ofp.action.pop_vlan()
559 ]
560 ),
macauley97557232015-07-16 17:28:07 +0800561 ofp.instruction.goto_table(20)
562 ],
563 priority=0)
564 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
565 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700566
macauley97557232015-07-16 17:28:07 +0800567 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
568 match = ofp.match()
569 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500570 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
macauley97557232015-07-16 17:28:07 +0800571 request = ofp.message.flow_add(
572 table_id=10,
573 cookie=42,
574 match=match,
575 instructions=[
576 ofp.instruction.apply_actions(
577 actions=[
Flavio Castroaf2b4502016-02-02 17:41:32 -0500578 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
macauley97557232015-07-16 17:28:07 +0800579 ]
580 ),
581 ofp.instruction.goto_table(20)
582 ],
583 priority=0)
584 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
585 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800586 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800587
Flavio Castro932014b2016-01-05 18:29:15 -0500588 if (flag == 4) :
589 match = ofp.match()
590 match.oxm_list.append(ofp.oxm.in_port(of_port))
591 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1fff))
592 request = ofp.message.flow_add(
593 table_id=10,
594 cookie=42,
595 match=match,
596 instructions=[
597 ofp.instruction.apply_actions(
598 actions=[
Flavio Castroaf2b4502016-02-02 17:41:32 -0500599 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
Flavio Castro932014b2016-01-05 18:29:15 -0500600 ]
601 ),
602 ofp.instruction.goto_table(20)
603 ],
604 priority=0)
605 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
606 ctrl.message_send(request)
607 msgs.append(request)
608
macauley97557232015-07-16 17:28:07 +0800609 if send_barrier:
610 do_barrier(ctrl)
611
macauley15909e72015-07-17 15:58:57 +0800612 return msgs
Pierbbdf3782016-08-22 17:58:26 -0700613
macauley_cheng6e6a6122015-11-16 14:19:18 +0800614def del_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
615 # table 10: vlan
616 # goto to table 20
617 msgs=[]
618 for of_port in ports:
619 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
620 match = ofp.match()
621 match.oxm_list.append(ofp.oxm.in_port(of_port))
622 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
623 request = ofp.message.flow_delete(
624 table_id=10,
625 cookie=42,
626 match=match,
627 priority=0)
628 logging.info("Del vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
629 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800630
macauley_cheng6e6a6122015-11-16 14:19:18 +0800631 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
632 match = ofp.match()
633 match.oxm_list.append(ofp.oxm.in_port(of_port))
634 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0xfff))
635 request = ofp.message.flow_delete(
636 table_id=10,
637 cookie=42,
638 match=match,
639 priority=0)
640 logging.info("Del vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
641 ctrl.message_send(request)
642 msgs.append(request)
643
644 if send_barrier:
645 do_barrier(ctrl)
646
647 return msgs
Pierbbdf3782016-08-22 17:58:26 -0700648
macauley_cheng6b311612015-09-04 11:32:27 +0800649def add_vlan_table_flow_pvid(ctrl, in_port, match_vid=None, pvid=1, send_barrier=False):
650 """it will tag pack as untagged packet wether it has tagg or not"""
651 match = ofp.match()
652 match.oxm_list.append(ofp.oxm.in_port(in_port))
653 actions=[]
654 if match_vid == None:
Pierbbdf3782016-08-22 17:58:26 -0700655 match.oxm_list.append(ofp.oxm.vlan_vid(0))
macauley_cheng6b311612015-09-04 11:32:27 +0800656 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
657 goto_table=20
658 else:
659 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+match_vid, 0x1fff))
660 actions.append(ofp.action.push_vlan(0x8100))
Pierbbdf3782016-08-22 17:58:26 -0700661 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
macauley_cheng6b311612015-09-04 11:32:27 +0800662 goto_table=20
Pierbbdf3782016-08-22 17:58:26 -0700663
macauley_cheng6b311612015-09-04 11:32:27 +0800664 request = ofp.message.flow_add(
665 table_id=10,
666 cookie=42,
667 match=match,
668 instructions=[
669 ofp.instruction.apply_actions(actions=actions)
670 ,ofp.instruction.goto_table(goto_table)
671 ],
672 priority=0)
673 logging.info("Add PVID %d on port %d and go to table %ld" %( pvid, in_port, goto_table))
Pierbbdf3782016-08-22 17:58:26 -0700674 ctrl.message_send(request)
675
macauley_cheng6b311612015-09-04 11:32:27 +0800676 if send_barrier:
677 do_barrier(ctrl)
678
679def add_vlan_table_flow_allow_all_vlan(ctrl, in_port, send_barrier=False):
680 """it st flow allow all vlan tag on this port"""
681 match = ofp.match()
682 match.oxm_list.append(ofp.oxm.in_port(in_port))
683 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1000))
684 request = ofp.message.flow_add(
685 table_id=10,
686 cookie=42,
687 match=match,
688 instructions=[
Pierbbdf3782016-08-22 17:58:26 -0700689 ofp.instruction.goto_table(20)
macauley_cheng6b311612015-09-04 11:32:27 +0800690 ],
691 priority=0)
692 logging.info("Add allow all vlan on port %d " %(in_port))
Pierbbdf3782016-08-22 17:58:26 -0700693 ctrl.message_send(request)
macauley_cheng6b311612015-09-04 11:32:27 +0800694
Pier7b031af2016-08-25 15:00:22 -0700695def 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):
696 # Install a flow for VLAN translation
697 # in VLAN table.
698 # table 10: vlan
699 # goto to table 20
700 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
701 match = ofp.match()
702 match.oxm_list.append(ofp.oxm.in_port(of_port))
703 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
704
705 actions=[]
706 if vrf!=0:
707 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
708 if new_vlan_id != -1:
709 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_vlan_id)))
710
711 request = ofp.message.flow_add(
712 table_id=10,
713 cookie=42,
714 match=match,
715 instructions=[
716 ofp.instruction.apply_actions(
717 actions=actions
718 ),
719 ofp.instruction.goto_table(20)
720 ],
721 priority=0)
722 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
723 ctrl.message_send(request)
724
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700725
726def add_one_egress_vlan_table_flow(ctrl, of_port, match_vlan, inner_vlan, outer_vlan):
727
728 # used for translating single to double tagged packets only
729
730 match = ofp.match()
731 match.oxm_list.append(ofp.oxm.exp4ByteValue(ofp.oxm.OFDPA_EXP_TYPE_ACTSET_OUTPUT, of_port))
732 match.oxm_list.append(ofp.oxm.exp1ByteValue(ofp.oxm.OFDPA_EXP_TYPE_ALLOW_VLAN_TRANSLATION, 1))
733 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+match_vlan,0x1fff))
734
735 actions=[]
736 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+inner_vlan)))
737 actions.append(ofp.action.push_vlan(0x8100))
738 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan)))
739
740 request = ofp.message.flow_add(
741 table_id=EGRESS_VLAN_FLOW_TABLE,
742 cookie=42,
743 match=match,
744 instructions=[
745 ofp.instruction.apply_actions(
746 actions=actions
747 ),
748 ofp.instruction.goto_table(EGRESS_DSCP_TABLE)
749 ],
750 priority=0)
751
752 ctrl.message_send(request)
753
754 return
755
Piere1308762016-09-12 15:29:56 -0700756def 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):
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700757
macauley0f91a3e2015-07-17 18:09:59 +0800758 # goto to table 20
Flavio Castro932014b2016-01-05 18:29:15 -0500759 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
macauley0f91a3e2015-07-17 18:09:59 +0800760 match = ofp.match()
761 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500762 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
macauley7f89d962015-08-06 18:13:48 +0800763
764 actions=[]
Alex Yashchuk9f449462017-12-09 18:27:19 +0200765 if config["switch_type"] != 'xpliant' and vrf != 0:
766 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
Pierbbdf3782016-08-22 17:58:26 -0700767
Flavio Castro6d498522015-12-15 14:05:04 -0500768 #actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id)))
macauley7f89d962015-08-06 18:13:48 +0800769
macauley0f91a3e2015-07-17 18:09:59 +0800770 request = ofp.message.flow_add(
771 table_id=10,
772 cookie=42,
773 match=match,
774 instructions=[
macauley53d90fe2015-08-04 17:34:22 +0800775 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800776 actions=actions
macauley53d90fe2015-08-04 17:34:22 +0800777 ),
778 ofp.instruction.goto_table(20)
macauley0f91a3e2015-07-17 18:09:59 +0800779 ],
780 priority=0)
781 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
782 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700783
macauley0f91a3e2015-07-17 18:09:59 +0800784 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700785
macauley0f91a3e2015-07-17 18:09:59 +0800786 match = ofp.match()
787 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500788 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
Pierbbdf3782016-08-22 17:58:26 -0700789
macauley7f89d962015-08-06 18:13:48 +0800790 actions=[]
791 if vrf!=0:
792 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
Pierbbdf3782016-08-22 17:58:26 -0700793
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700794 # actions.append(ofp.action.push_vlan(0x8100))
Flavio Castro91d1a552016-05-17 16:59:44 -0700795 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Pierbbdf3782016-08-22 17:58:26 -0700796
macauley0f91a3e2015-07-17 18:09:59 +0800797 request = ofp.message.flow_add(
798 table_id=10,
799 cookie=42,
800 match=match,
801 instructions=[
802 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800803 actions=actions
macauley0f91a3e2015-07-17 18:09:59 +0800804 ),
805 ofp.instruction.goto_table(20)
806 ],
807 priority=0)
808 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
809 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700810
Flavio Castro932014b2016-01-05 18:29:15 -0500811 if (flag == 4) :
812 match = ofp.match()
813 match.oxm_list.append(ofp.oxm.in_port(of_port))
814 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000,0x1fff))
815
816 actions=[]
817 if vrf!=0:
818 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
819
Flavio Castro91d1a552016-05-17 16:59:44 -0700820 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Flavio Castro932014b2016-01-05 18:29:15 -0500821
822 request = ofp.message.flow_add(
823 table_id=10,
824 cookie=42,
825 match=match,
826 instructions=[
827 ofp.instruction.apply_actions(
828 actions=actions
829 ),
830 ofp.instruction.goto_table(20)
831 ],
832 priority=0)
833 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
834 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800835
Piere1308762016-09-12 15:29:56 -0700836 if (flag == VLAN_TABLE_FLAG_ONLY_STACKED):
837 # This flag is meant to managed stacked vlan packtes
838 # Matches on outer VLAN_ID, set OVID with outer VLAN.
839 # Finally expose inner VLAN_ID with a pop action and
840 # goto VLAN_1_FLOW_TABLE
841 match = ofp.match()
842 match.oxm_list.append(ofp.oxm.in_port(of_port))
843 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
844
845 actions=[]
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700846 # actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_OVID, value=0x1000+vlan_id)))
Piere1308762016-09-12 15:29:56 -0700847 actions.append(ofp.action.pop_vlan())
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700848 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_OVID, value=0x1000+vlan_id)))
Piere1308762016-09-12 15:29:56 -0700849
850 request = ofp.message.flow_add(
851 table_id=10,
852 cookie=42,
853 match=match,
854 instructions=[
855 ofp.instruction.apply_actions(
856 actions=actions
857 ),
858 ofp.instruction.goto_table(VLAN_1_FLOW_TABLE)
859 ],
860 priority=0)
861 logging.info("Add vlan %d tagged packets on port %d and go to table %d" %( vlan_id, of_port, VLAN_1_FLOW_TABLE))
862 ctrl.message_send(request)
863
864 if (flag == VLAN_TABLE_FLAG_PRIORITY) :
865 match = ofp.match()
866 match.oxm_list.append(ofp.oxm.in_port(of_port))
867 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1fff))
868 request = ofp.message.flow_add(
869 table_id=10,
870 cookie=42,
871 match=match,
872 instructions=[
873 ofp.instruction.apply_actions(
874 actions=[
875 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)),
876 ofp.action.push_vlan(0x8100),
877 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+out_vlan_id)),
878 ]
879 ),
880 ofp.instruction.goto_table(20)
881 ],
882 priority=0)
883 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
884 ctrl.message_send(request)
885
886 if send_barrier:
887 do_barrier(ctrl)
888
889 return request
890
Piercf76e802016-09-19 20:16:35 -0700891def 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):
892 # table 10: vlan
893 # goto to table 13
894 if flag == VLAN_TABLE_FLAG_ONLY_TAG:
895 match = ofp.match()
896 match.oxm_list.append(ofp.oxm.in_port(of_port))
897 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id, 0x1fff))
898
899 actions=[]
900 if vlan_id == -1:
901 actions.append(ofp.action.pop_vlan())
902 if new_vlan_id > 1:
903 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_vlan_id)))
904 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
905 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
906 # 0x0000nnnn is for UNI interfaces
907 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
908
909 request = ofp.message.flow_add(
910 table_id=10,
911 cookie=42,
912 match=match,
913 instructions=[
914 ofp.instruction.apply_actions(
915 actions=actions
916 ),
917 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
918 ],
919 priority=0)
920 logging.info("Add vlan %d tagged packets on port %d and go to table %d" % (vlan_id, of_port, MPLS_L2_PORT_FLOW_TABLE))
921 ctrl.message_send(request)
922
923 if flag == VLAN_TABLE_FLAG_ONLY_UNTAG:
924 match = ofp.match()
925 match.oxm_list.append(ofp.oxm.in_port(of_port))
926 match.oxm_list.append(ofp.oxm.vlan_vid(0))
927
928 actions=[]
929 if vlan_id > 1:
Charles Chanc85f1562018-01-18 15:44:29 -0800930 # actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
931 # actions.append(ofp.action.set_field(ofp.action.push_vlan(0x8100)))
Piercf76e802016-09-19 20:16:35 -0700932 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Charles Chanc85f1562018-01-18 15:44:29 -0800933
Piercf76e802016-09-19 20:16:35 -0700934 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
935 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
936 # 0x0000nnnn is for UNI interfaces
937 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
938
939 request = ofp.message.flow_add(
940 table_id=10,
941 cookie=42,
942 match=match,
943 instructions=[
944 ofp.instruction.apply_actions(
945 actions=actions
946 ),
947 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
948 ],
949 priority=0)
950 logging.info("Add vlan %d untagged packets on port %d and go to table %d" % (vlan_id, of_port, MPLS_L2_PORT_FLOW_TABLE))
951 ctrl.message_send(request)
952
953 if send_barrier:
954 do_barrier(ctrl)
955
956 return request
957
Piere1308762016-09-12 15:29:56 -0700958def 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):
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700959
Piere1308762016-09-12 15:29:56 -0700960 # table 11: vlan 1 table
961 # goto to table 20
962 if flag == VLAN_TABLE_FLAG_ONLY_TAG:
963 match = ofp.match()
964 match.oxm_list.append(ofp.oxm.in_port(of_port))
965 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
966 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
967
968 actions=[]
969 actions.append(ofp.action.push_vlan(0x8100))
970 if new_outer_vlan_id != -1:
971 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_outer_vlan_id)))
972 else:
973 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan_id)))
974
975 request = ofp.message.flow_add(
976 table_id=11,
977 cookie=42,
978 match=match,
979 instructions=[
980 ofp.instruction.apply_actions(
981 actions=actions
982 ),
983 ofp.instruction.goto_table(TERMINATION_FLOW_TABLE)
984 ],
985 priority=0)
986 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))
987 ctrl.message_send(request)
988
989 if flag == VLAN_TABLE_FLAG_ONLY_UNTAG:
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700990
Piere1308762016-09-12 15:29:56 -0700991 match = ofp.match()
992 match.oxm_list.append(ofp.oxm.in_port(of_port))
993 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
994 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
995
996 actions=[]
997 request = ofp.message.flow_add(
998 table_id=11,
999 cookie=42,
1000 match=match,
1001 instructions=[
1002 ofp.instruction.apply_actions(
1003 actions=actions
1004 ),
1005 ofp.instruction.goto_table(TERMINATION_FLOW_TABLE)
1006 ],
1007 priority=0)
1008 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))
1009 ctrl.message_send(request)
1010
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -07001011 if flag == VLAN_TABLE_FLAG_ONLY_POP_VLAN:
1012
1013 print("INSTALLIN IN TABLE 11!")
1014
1015 match = ofp.match()
1016 match.oxm_list.append(ofp.oxm.in_port(of_port))
1017 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
1018 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
1019
1020 actions=[]
1021 actions.append(ofp.action.pop_vlan())
1022
1023 request = ofp.message.flow_add(
1024 table_id=11,
1025 cookie=42,
1026 match=match,
1027 instructions=[
1028 ofp.instruction.apply_actions(
1029 actions=actions
1030 ),
1031 ofp.instruction.goto_table(TERMINATION_FLOW_TABLE)
1032 ],
1033 priority=0)
1034 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))
1035 ctrl.message_send(request)
1036
1037
macauley0f91a3e2015-07-17 18:09:59 +08001038 if send_barrier:
1039 do_barrier(ctrl)
1040
1041 return request
Pierbbdf3782016-08-22 17:58:26 -07001042
Saurav Das5d1473d2018-07-12 11:02:56 -07001043def 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, cross_connect=False, send_barrier=False):
Charles Chanc85f1562018-01-18 15:44:29 -08001044
Piercf76e802016-09-19 20:16:35 -07001045 # table 11: vlan 1 table
1046 # goto to table 13
1047 match = ofp.match()
1048 match.oxm_list.append(ofp.oxm.in_port(of_port))
1049 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
1050 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
1051
1052 actions=[]
Saurav Das5d1473d2018-07-12 11:02:56 -07001053
1054 if cross_connect:
1055 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+inner_vlan_id)))
Piercf76e802016-09-19 20:16:35 -07001056 actions.append(ofp.action.push_vlan(0x8100))
1057 if new_outer_vlan_id != -1:
Saurav Das5d1473d2018-07-12 11:02:56 -07001058 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_outer_vlan_id)))
Piercf76e802016-09-19 20:16:35 -07001059 else:
Saurav Das5d1473d2018-07-12 11:02:56 -07001060 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan_id)))
Piercf76e802016-09-19 20:16:35 -07001061
Saurav Das5d1473d2018-07-12 11:02:56 -07001062 if not cross_connect:
1063 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
1064 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
1065 else:
1066 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE_CROSS_CONNECT)))
1067
Piercf76e802016-09-19 20:16:35 -07001068 # 0x0000nnnn is for UNI interfaces
1069 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
1070
1071 request = ofp.message.flow_add(
Saurav Das5d1473d2018-07-12 11:02:56 -07001072 table_id=11,
1073 cookie=42,
1074 match=match,
1075 instructions=[
1076 ofp.instruction.apply_actions(
1077 actions=actions
1078 ),
1079 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
1080 ],
1081 priority=0)
Piercf76e802016-09-19 20:16:35 -07001082 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))
1083 ctrl.message_send(request)
Piercf76e802016-09-19 20:16:35 -07001084 if send_barrier:
1085 do_barrier(ctrl)
1086
1087 return request
1088
macauley97557232015-07-16 17:28:07 +08001089def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False):
1090 match = ofp.match()
castroflavio21894482015-12-08 15:29:55 -05001091 priority=500
macauleyfddc4662015-07-27 17:40:30 +08001092 if dst_mac!=None:
castroflavio21894482015-12-08 15:29:55 -05001093 priority=1000
macauleyfddc4662015-07-27 17:40:30 +08001094 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
1095
macauley97557232015-07-16 17:28:07 +08001096 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauleyfddc4662015-07-27 17:40:30 +08001097
macauley97557232015-07-16 17:28:07 +08001098 request = ofp.message.flow_add(
1099 table_id=50,
1100 cookie=42,
1101 match=match,
1102 instructions=[
1103 ofp.instruction.write_actions(
1104 actions=[
1105 ofp.action.group(group_id)]),
1106 ofp.instruction.goto_table(60)
1107 ],
1108 buffer_id=ofp.OFP_NO_BUFFER,
castroflavio21894482015-12-08 15:29:55 -05001109 priority=priority)
macauley97557232015-07-16 17:28:07 +08001110
1111 logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac)
1112 ctrl.message_send(request)
1113
1114 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001115 do_barrier(ctrl)
macauley15909e72015-07-17 15:58:57 +08001116
Pierbbdf3782016-08-22 17:58:26 -07001117 return request
macauleyfddc4662015-07-27 17:40:30 +08001118
1119def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False):
1120 match = ofp.match()
1121 if dst_mac!=None:
1122 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
1123
1124 match.oxm_list.append(ofp.oxm.tunnel_id(vnid))
1125 if is_group == True:
1126 actions=[ofp.action.group(group_id)]
1127 else:
1128 actions=[ofp.action.output(group_id)]
1129
1130 request = ofp.message.flow_add(
1131 table_id=50,
1132 cookie=42,
1133 match=match,
1134 instructions=[
1135 ofp.instruction.write_actions(
1136 actions=actions),
1137 ofp.instruction.goto_table(60)
1138 ],
1139 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001140 priority=1000)
macauleyfddc4662015-07-27 17:40:30 +08001141
1142 logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac)
1143 ctrl.message_send(request)
1144
1145 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001146 do_barrier(ctrl)
macauleyfddc4662015-07-27 17:40:30 +08001147
Pierbbdf3782016-08-22 17:58:26 -07001148 return request
1149
macauley_cheng6b133662015-11-09 13:52:39 +08001150def add_termination_flow(ctrl, in_port, eth_type, dst_mac, vlanid, goto_table=None, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +08001151 match = ofp.match()
macauley0f91a3e2015-07-17 18:09:59 +08001152 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauleyfddc4662015-07-27 17:40:30 +08001153 if dst_mac[0]&0x01 == 0x01:
1154 match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00]))
1155 goto_table=40
1156 else:
macauley53d90fe2015-08-04 17:34:22 +08001157 if in_port!=0:
1158 match.oxm_list.append(ofp.oxm.in_port(in_port))
macauleyfddc4662015-07-27 17:40:30 +08001159 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
1160 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauley_cheng6b133662015-11-09 13:52:39 +08001161 if goto_table == None:
1162 goto_table=30
macauley0f91a3e2015-07-17 18:09:59 +08001163
1164 request = ofp.message.flow_add(
1165 table_id=20,
1166 cookie=42,
1167 match=match,
1168 instructions=[
1169 ofp.instruction.goto_table(goto_table)
1170 ],
1171 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001172 priority=1)
macauley0f91a3e2015-07-17 18:09:59 +08001173
1174 logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac)
1175 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +08001176 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001177 do_barrier(ctrl)
macauley0f91a3e2015-07-17 18:09:59 +08001178
Pierbbdf3782016-08-22 17:58:26 -07001179 return request
1180
Alex Yashchuk9f449462017-12-09 18:27:19 +02001181def add_unicast_routing_flow(ctrl, eth_type, dst_ip, mask, action_group_id, vrf=0, send_ctrl=False, send_barrier=False, priority = 1):
macauley0f91a3e2015-07-17 18:09:59 +08001182 match = ofp.match()
1183 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
Alex Yashchuk9f449462017-12-09 18:27:19 +02001184 if config["switch_type"] != 'xpliant' and vrf != 0:
macauley53d90fe2015-08-04 17:34:22 +08001185 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
macauley0f91a3e2015-07-17 18:09:59 +08001186
Flavio Castroaf2b4502016-02-02 17:41:32 -05001187 match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask))
1188
1189 instructions = []
1190 instructions.append(ofp.instruction.goto_table(60))
1191 if send_ctrl:
Pier265ad5f2017-02-28 17:46:28 +01001192 instructions.append(ofp.instruction.write_actions(
Flavio Castroaf2b4502016-02-02 17:41:32 -05001193 actions=[ofp.action.output( port=ofp.OFPP_CONTROLLER,
1194 max_len=ofp.OFPCML_NO_BUFFER)]))
Pierbbdf3782016-08-22 17:58:26 -07001195 else:
Flavio Castroaf2b4502016-02-02 17:41:32 -05001196 instructions.append(ofp.instruction.write_actions(
1197 actions=[ofp.action.group(action_group_id)]))
macauley53d90fe2015-08-04 17:34:22 +08001198
macauley0f91a3e2015-07-17 18:09:59 +08001199 request = ofp.message.flow_add(
1200 table_id=30,
1201 cookie=42,
1202 match=match,
Flavio Castroaf2b4502016-02-02 17:41:32 -05001203 instructions=instructions,
macauley0f91a3e2015-07-17 18:09:59 +08001204 buffer_id=ofp.OFP_NO_BUFFER,
Alex Yashchuk9f449462017-12-09 18:27:19 +02001205 priority=priority)
macauley0f91a3e2015-07-17 18:09:59 +08001206
1207 logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip)
1208 ctrl.message_send(request)
1209
1210 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001211 do_barrier(ctrl)
macauley0f91a3e2015-07-17 18:09:59 +08001212
Flavio Castro9debaaa2016-07-26 19:37:50 -07001213 return request
Flavio Castrod8f8af22015-12-02 18:19:26 -05001214
Andrea Campanellabfb42b32018-04-26 10:57:23 +02001215def add_unicast_blackhole_flow(ctrl, eth_type, dst_ip, mask, vrf=0, send_ctrl=False, send_barrier=False, priority = 1):
1216 match = ofp.match()
1217 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1218 if config["switch_type"] != 'xpliant' and vrf != 0:
1219 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
1220
1221 match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask))
1222
1223 instructions = []
1224 instructions.append(ofp.instruction.goto_table(60))
1225 instructions.append(ofp.instruction.clear_actions( ))
1226
1227 request = ofp.message.flow_add(
1228 table_id=30,
1229 cookie=42,
1230 match=match,
1231 instructions=instructions,
1232 buffer_id=ofp.OFP_NO_BUFFER,
1233 priority=priority)
1234
1235 logging.info("Inserting unicast blackhole routing flow eth_type %lx, dip %ld",eth_type, dst_ip)
1236 ctrl.message_send(request)
1237
1238 if send_barrier:
1239 do_barrier(ctrl)
1240
1241 return request
1242
Pier1e4e98e2016-10-26 14:36:05 -07001243def add_unicast_v6_routing_flow(ctrl, eth_type, dst_ip, mask, action_group_id, vrf=0, send_ctrl=False, send_barrier=False):
1244 match = ofp.match()
1245 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1246 if vrf != 0:
1247 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
1248
1249 match.oxm_list.append(ofp.oxm.ipv6_dst_masked(parse_ipv6(dst_ip), parse_ipv6(mask)))
1250
1251 instructions = []
1252 instructions.append(ofp.instruction.goto_table(60))
1253 if send_ctrl:
Pier265ad5f2017-02-28 17:46:28 +01001254 instructions.append(ofp.instruction.write_actions(
Pier1e4e98e2016-10-26 14:36:05 -07001255 actions=[ofp.action.output( port=ofp.OFPP_CONTROLLER,
1256 max_len=ofp.OFPCML_NO_BUFFER)]))
1257 else:
1258 instructions.append(ofp.instruction.write_actions(
1259 actions=[ofp.action.group(action_group_id)]))
1260
1261 request = ofp.message.flow_add(
1262 table_id=30,
1263 cookie=42,
1264 match=match,
1265 instructions=instructions,
1266 buffer_id=ofp.OFP_NO_BUFFER,
1267 priority=1)
1268
1269 logging.info("Inserting unicast routing flow eth_type %lx, dip %s",eth_type, dst_ip)
1270 ctrl.message_send(request)
1271
1272 if send_barrier:
1273 do_barrier(ctrl)
1274
1275 return request
1276
Andreas Pantelopoulos6c76b942018-01-22 10:03:02 -08001277def add_mpls_flow(ctrl, action_group_id=0x0, label=100 ,ethertype=0x0800, bos=1, vrf=1, goto_table=27, dec_ttl=False, send_barrier=False):
Flavio Castrob702a2f2016-04-10 22:01:48 -04001278 match = ofp.match()
1279 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1280 match.oxm_list.append(ofp.oxm.mpls_label(label))
1281 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
Pier265ad5f2017-02-28 17:46:28 +01001282 write_actions = []
1283 write_actions.append(ofp.action.group(action_group_id))
1284 apply_actions = []
1285 apply_actions = [ofp.action.dec_mpls_ttl(),
Flavio Castro8ca52542016-04-11 11:24:49 -04001286 ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))]
Pier265ad5f2017-02-28 17:46:28 +01001287 if (goto_table != 29):
1288 apply_actions.append(ofp.action.set_field(
Flavio Castrod80fbc32016-07-25 15:54:26 -07001289 ofp.oxm.exp2ByteValue(exp_type=23, value=32)))
Pier265ad5f2017-02-28 17:46:28 +01001290 apply_actions.append(ofp.action.copy_ttl_in())
Flavio Castro9debaaa2016-07-26 19:37:50 -07001291
Flavio Castrob702a2f2016-04-10 22:01:48 -04001292 request = ofp.message.flow_add(
1293 table_id=24,
1294 cookie=43,
1295 match=match,
1296 instructions=[
Pier265ad5f2017-02-28 17:46:28 +01001297 ofp.instruction.apply_actions(actions=apply_actions),
1298 ofp.instruction.write_actions(actions=write_actions),
Flavio Castro8ca52542016-04-11 11:24:49 -04001299 ofp.instruction.goto_table(goto_table)
Flavio Castrob702a2f2016-04-10 22:01:48 -04001300 ],
1301 buffer_id=ofp.OFP_NO_BUFFER,
1302 priority=1)
Flavio Castrob702a2f2016-04-10 22:01:48 -04001303 ctrl.message_send(request)
1304
1305 if send_barrier:
1306 do_barrier(ctrl)
1307
1308 return request
1309
Alex Yashchuk9f449462017-12-09 18:27:19 +02001310def xpliant_add_mpls_flow(ctrl, action_group_id=0x0, label=100 ,ethertype=0x0800, bos=1, vrf=1, goto_table=27, send_barrier=False):
1311 match = ofp.match()
1312 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1313 match.oxm_list.append(ofp.oxm.mpls_label(label))
1314
1315 apply_actions = []
1316 apply_actions.append(ofp.action.group(action_group_id))
1317 apply_actions.append(ofp.action.dec_mpls_ttl())
1318 if (goto_table != 29):
1319 apply_actions.append(ofp.action.pop_mpls(ethertype))
1320
1321 request = ofp.message.flow_add(
1322 table_id=24,
1323 cookie=43,
1324 match=match,
1325 instructions=[
1326 ofp.instruction.apply_actions(actions=apply_actions),
1327 ],
1328 buffer_id=ofp.OFP_NO_BUFFER,
1329 priority=1)
1330 logging.info("Inserting MPLS flow , label %ld", label)
1331 ctrl.message_send(request)
1332
1333 if send_barrier:
1334 do_barrier(ctrl)
1335
1336 return request
1337
Andreas Pantelopoulos6c76b942018-01-22 10:03:02 -08001338def add_mpls_flow_swap(ctrl, action_group_id, label, ethertype, bos, goto_table=MPLS_TYPE_FLOW_TABLE, of_port=0, send_barrier=False):
1339 match = ofp.match()
1340 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1341 match.oxm_list.append(ofp.oxm.mpls_label(label))
1342 match.oxm_list.append(ofp.oxm.mpls_bos(1))
1343
1344 apply_actions = []
1345 write_actions = []
1346 apply_actions.append(ofp.action.dec_mpls_ttl())
1347 write_actions.append(ofp.action.group(action_group_id))
1348
1349 request = ofp.message.flow_add(
1350 table_id=24,
1351 cookie=43,
1352 match=match,
1353 instructions=[
1354 ofp.instruction.apply_actions(actions=apply_actions),
1355 ofp.instruction.write_actions(actions=write_actions),
1356 ofp.instruction.goto_table(goto_table)
1357 ],
1358 buffer_id=ofp.OFP_NO_BUFFER,
1359 priority=1)
1360
1361 logging.info("Inserting MPLS flow , label %ld", label)
1362 ctrl.message_send(request)
1363
1364 if send_barrier:
1365 do_barrier(ctrl)
1366
1367 return request
1368
1369
Pierf6f28162016-09-22 16:30:52 -07001370def 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):
Pierb5da4c92016-09-21 11:23:35 -07001371 match = ofp.match()
1372 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1373 match.oxm_list.append(ofp.oxm.mpls_label(label))
1374 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
1375
Pier265ad5f2017-02-28 17:46:28 +01001376 apply_actions = []
1377 write_actions = []
1378 apply_actions.append(ofp.action.dec_mpls_ttl())
Pierf6f28162016-09-22 16:30:52 -07001379 if popMPLS == True:
Pier265ad5f2017-02-28 17:46:28 +01001380 apply_actions.append(ofp.action.copy_ttl_in())
1381 apply_actions.append(ofp.action.pop_mpls(ethertype))
Pierf6f28162016-09-22 16:30:52 -07001382 if bos==1 and popL2 == True:
Pier265ad5f2017-02-28 17:46:28 +01001383 apply_actions.append(ofp.action.ofdpa_pop_l2_header())
1384 apply_actions.append(ofp.action.ofdpa_pop_cw())
1385 apply_actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
Pierf6f28162016-09-22 16:30:52 -07001386 # 0x0002nnnn is for UNI interfaces
Pier265ad5f2017-02-28 17:46:28 +01001387 apply_actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00020000 + of_port)))
1388 apply_actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
Andreas Pantelopoulosaaa02622018-04-04 15:13:03 -07001389
Pier265ad5f2017-02-28 17:46:28 +01001390 write_actions.append(ofp.action.group(action_group_id))
Pierb5da4c92016-09-21 11:23:35 -07001391
1392 request = ofp.message.flow_add(
Pier265ad5f2017-02-28 17:46:28 +01001393 table_id=24,
1394 cookie=43,
1395 match=match,
1396 instructions=[
1397 ofp.instruction.apply_actions(actions=apply_actions),
1398 ofp.instruction.write_actions(actions=write_actions),
1399 ofp.instruction.goto_table(goto_table)
1400 ],
1401 buffer_id=ofp.OFP_NO_BUFFER,
1402 priority=1)
Pierb5da4c92016-09-21 11:23:35 -07001403 logging.info("Inserting MPLS flow , label %ld", label)
1404 ctrl.message_send(request)
1405
1406 if send_barrier:
1407 do_barrier(ctrl)
1408
1409 return request
1410
macauleyfddc4662015-07-27 17:40:30 +08001411def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False):
1412 match = ofp.match()
1413 match.oxm_list.append(ofp.oxm.eth_type(0x0800))
Alex Yashchuk9f449462017-12-09 18:27:19 +02001414 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000 + vlan_id))
macauleyfddc4662015-07-27 17:40:30 +08001415 if src_ip_mask!=0:
1416 match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask))
1417 else:
1418 match.oxm_list.append(ofp.oxm.ipv4_src(src_ip))
Pierbbdf3782016-08-22 17:58:26 -07001419
macauleyfddc4662015-07-27 17:40:30 +08001420 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
Pierbbdf3782016-08-22 17:58:26 -07001421
macauleyfddc4662015-07-27 17:40:30 +08001422 request = ofp.message.flow_add(
1423 table_id=40,
1424 cookie=42,
1425 match=match,
1426 instructions=[
1427 ofp.instruction.write_actions(
1428 actions=[ofp.action.group(action_group_id)]),
1429 ofp.instruction.goto_table(60)
1430 ],
1431 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001432 priority=1)
macauleyfddc4662015-07-27 17:40:30 +08001433
1434 logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask)
1435 ctrl.message_send(request)
1436
1437 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001438 do_barrier(ctrl)
macauleyfddc4662015-07-27 17:40:30 +08001439
Pierbbdf3782016-08-22 17:58:26 -07001440 return request
macauley_cheng6b133662015-11-09 13:52:39 +08001441
Pier1e4e98e2016-10-26 14:36:05 -07001442def add_acl_rule(ctrl, eth_type=None, ip_proto=None, send_barrier=False):
1443 match = ofp.match()
1444 if eth_type != None:
1445 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1446 if ip_proto != None:
1447 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1448
1449 request = ofp.message.flow_add(
1450 table_id=60,
1451 cookie=42,
1452 match=match,
1453 instructions=[
1454 ofp.instruction.apply_actions(
1455 actions=[ofp.action.output(port=ofp.OFPP_CONTROLLER, max_len=ofp.OFPCML_NO_BUFFER)]
1456 ),
1457 ],
1458 buffer_id=ofp.OFP_NO_BUFFER,
1459 priority=1
1460 )
1461
1462 logging.info("Inserting ACL flow eth_type %lx, ip_proto %ld", eth_type, ip_proto)
1463 ctrl.message_send(request)
1464
1465 if send_barrier:
1466 do_barrier(ctrl)
1467
1468 return request
1469
macauley_cheng6b133662015-11-09 13:52:39 +08001470#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_chengeffc20a2015-11-09 16:14:56 +08001471def add_dnat_flow(ctrl, eth_type, ip_dst, ip_proto, tcp_dst, set_ip_dst, set_tcp_dst, action_group_id):
macauley_cheng6b133662015-11-09 13:52:39 +08001472 match = ofp.match()
1473 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1474 match.oxm_list.append(ofp.oxm.ipv4_dst(ip_dst))
1475 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1476 match.oxm_list.append(ofp.oxm.tcp_dst(tcp_dst))
Pierbbdf3782016-08-22 17:58:26 -07001477
macauley_cheng6b133662015-11-09 13:52:39 +08001478 request = ofp.message.flow_add(
1479 table_id=28,
1480 cookie=42,
1481 match=match,
1482 instructions=[
1483 ofp.instruction.write_actions(
1484 actions=[ofp.action.set_field(ofp.oxm.ipv4_dst(set_ip_dst)),
1485 ofp.action.set_field(ofp.oxm.tcp_dst(set_tcp_dst)),
1486 ofp.action.group(action_group_id)]),
1487 ofp.instruction.goto_table(60)
1488 ],
1489 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001490 priority=1)
macauley_cheng6b133662015-11-09 13:52:39 +08001491 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)
1492 ctrl.message_send(request)
1493 return request
macauley_chengeffc20a2015-11-09 16:14:56 +08001494
1495#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
1496def add_snat_flow(ctrl, eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src):
1497 match = ofp.match()
1498 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1499 match.oxm_list.append(ofp.oxm.ipv4_src(ip_src))
1500 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1501 match.oxm_list.append(ofp.oxm.tcp_src(tcp_src))
Pierbbdf3782016-08-22 17:58:26 -07001502
macauley_chengeffc20a2015-11-09 16:14:56 +08001503 request = ofp.message.flow_add(
1504 table_id=29,
1505 cookie=42,
1506 match=match,
1507 instructions=[
1508 ofp.instruction.write_actions(
1509 actions=[ofp.action.set_field(ofp.oxm.ipv4_src(set_ip_src)),
1510 ofp.action.set_field(ofp.oxm.tcp_src(set_tcp_src))]),
1511 ofp.instruction.goto_table(30)
1512 ],
1513 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001514 priority=1)
macauley_chengeffc20a2015-11-09 16:14:56 +08001515 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)
1516 ctrl.message_send(request)
1517 return request
Pierbbdf3782016-08-22 17:58:26 -07001518
1519def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001520 """
1521 Command Example:
1522 of-agent vtap 10001 ethernet 1/1 vid 1
1523 of-agent vtp 10001 vni 10
1524 """
1525 if vlan != 0:
1526 config_vtap_xml="""
1527 <config>
1528 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1529 <id>capable-switch-1</id>
1530 <resources>
1531 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001532 <resource-id >LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001533 <features>
1534 <current>
1535 <rate>10Gb</rate>
1536 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001537 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001538 </current>
1539 <advertised>
1540 <rate>10Gb</rate>
1541 <rate>100Gb</rate>
1542 <medium>fiber</medium>
1543 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001544 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001545 <supported>
1546 <rate>10Gb</rate>
1547 <rate>100Gb</rate>
1548 <medium>fiber</medium>
1549 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001550 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001551 <advertised-peer>
1552 <rate>10Gb</rate>
1553 <rate>100Gb</rate>
1554 <medium>fiber</medium>
1555 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001556 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001557 </features>
1558 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1559 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1560 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1561 <ofdpa10:vni>VNID</ofdpa10:vni>
1562 </ofdpa10:vtap>
Pierbbdf3782016-08-22 17:58:26 -07001563 </port>
macauleyfddc4662015-07-27 17:40:30 +08001564 </resources>
1565 <logical-switches>
1566 <switch>
1567 <id>DATAPATH_ID</id>
1568 <datapath-id>DATAPATH_ID</datapath-id>
1569 <resources>
1570 <port xc:operation="OPERATION">LPORT</port>
1571 </resources>
1572 </switch>
1573 </logical-switches>
1574 </capable-switch>
1575 </config>
Pierbbdf3782016-08-22 17:58:26 -07001576 """
macauleyfddc4662015-07-27 17:40:30 +08001577 else:
1578 config_vtap_xml="""
1579 <config>
1580 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1581 <id>capable-switch-1</id>
1582 <resources>
1583 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001584 <resource-id >LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001585 <features>
1586 <current>
1587 <rate>10Gb</rate>
1588 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001589 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001590 </current>
1591 <advertised>
1592 <rate>10Gb</rate>
1593 <rate>100Gb</rate>
1594 <medium>fiber</medium>
1595 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001596 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001597 <supported>
1598 <rate>10Gb</rate>
1599 <rate>100Gb</rate>
1600 <medium>fiber</medium>
1601 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001602 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001603 <advertised-peer>
1604 <rate>10Gb</rate>
1605 <rate>100Gb</rate>
1606 <medium>fiber</medium>
1607 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001608 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001609 </features>
1610 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1611 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1612 <ofdpa10:vni>VNID</ofdpa10:vni>
1613 </ofdpa10:vtap>
Pierbbdf3782016-08-22 17:58:26 -07001614 </port>
macauleyfddc4662015-07-27 17:40:30 +08001615 </resources>
1616 <logical-switches>
1617 <switch>
1618 <id>DATAPATH_ID</id>
1619 <datapath-id>DATAPATH_ID</datapath-id>
1620 <resources>
1621 <port xc:operation="OPERATION">LPORT</port>
1622 </resources>
1623 </switch>
1624 </logical-switches>
1625 </capable-switch>
1626 </config>
Pierbbdf3782016-08-22 17:58:26 -07001627 """
1628 str_datapath_id_f= "{:016x}".format(dp_id)
1629 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1630 config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id)
1631 config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport)))
1632 config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port))
1633 config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan))
macauleyfddc4662015-07-27 17:40:30 +08001634 config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid))
1635 config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation))
1636 return config_vtap_xml
Pierbbdf3782016-08-22 17:58:26 -07001637
1638def get_vtep_lport_config_xml(dp_id, lport, src_ip, dst_ip, next_hop_id, vnid, udp_src_port=6633, ttl=25, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001639 """
1640 Command Example:
1641 of-agent vtep 10002 source user-input-src-ip destination user-input-dst-ip udp-source-port 6633 nexthop 2 ttl 25
Pierbbdf3782016-08-22 17:58:26 -07001642 of-agent vtp 10001 vni 10
macauleyfddc4662015-07-27 17:40:30 +08001643 """
1644
1645 config_vtep_xml="""
1646 <config>
1647 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1648 <id>capable-switch-1</id>
1649 <resources>
1650 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001651 <resource-id>LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001652 <features>
1653 <current>
1654 <rate>10Gb</rate>
1655 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001656 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001657 </current>
1658 <advertised>
1659 <rate>10Gb</rate>
1660 <rate>100Gb</rate>
1661 <medium>fiber</medium>
1662 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001663 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001664 <supported>
1665 <rate>10Gb</rate>
1666 <rate>100Gb</rate>
1667 <medium>fiber</medium>
1668 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001669 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001670 <advertised-peer>
1671 <rate>10Gb</rate>
1672 <rate>100Gb</rate>
1673 <medium>fiber</medium>
1674 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001675 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001676 </features>
Pier265ad5f2017-02-28 17:46:28 +01001677 <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01">
1678 <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip>
1679 <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip>
1680 <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port>
1681 <ofdpa10:vni xc:operation="OPERATION">
macauley25999cf2015-08-07 17:03:24 +08001682 <ofdpa10:id>VNID</ofdpa10:id>
1683 </ofdpa10:vni>
Pier265ad5f2017-02-28 17:46:28 +01001684 <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id>
1685 <ofdpa10:ttl>TTL</ofdpa10:ttl>
1686 </ofdpa10:vtep>
Pierbbdf3782016-08-22 17:58:26 -07001687 </port>
macauleyfddc4662015-07-27 17:40:30 +08001688 </resources>
1689 <logical-switches>
1690 <switch>
1691 <id>DATAPATH_ID</id>
1692 <datapath-id>DATAPATH_ID</datapath-id>
1693 <resources>
1694 <port xc:operation="OPERATION">LPORT</port>
1695 </resources>
1696 </switch>
1697 </logical-switches>
1698 </capable-switch>
Pierbbdf3782016-08-22 17:58:26 -07001699 </config>
macauleyfddc4662015-07-27 17:40:30 +08001700 """
Pierbbdf3782016-08-22 17:58:26 -07001701 str_datapath_id_f= "{:016x}".format(dp_id)
1702 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1703 config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +08001704 config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport)))
Pierbbdf3782016-08-22 17:58:26 -07001705 config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip))
1706 config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip))
1707 config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port))
1708 config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1709 config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl))
macauleyfddc4662015-07-27 17:40:30 +08001710 config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid))
Pierbbdf3782016-08-22 17:58:26 -07001711 config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation))
macauleyfddc4662015-07-27 17:40:30 +08001712
Pierbbdf3782016-08-22 17:58:26 -07001713 return config_vtep_xml
1714
1715def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001716 #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2
1717 config_nexthop_xml="""
1718 <config>
1719 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1720 <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1721 <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id>
1722 <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac>
1723 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1724 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1725 </ofdpa10:next-hop>
1726 </of11-config:capable-switch>
1727 </config>
1728 """
1729 config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan))
Pierbbdf3782016-08-22 17:58:26 -07001730 config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port))
1731 config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1732 config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac))
1733 config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation))
1734 return config_nexthop_xml
macauleyfddc4662015-07-27 17:40:30 +08001735
Pierbbdf3782016-08-22 17:58:26 -07001736def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001737 #of-agent vni 10 multicast 224.1.1.1 nexthop 20
Pierbbdf3782016-08-22 17:58:26 -07001738 if mcast_ipv4!=None:
macauleyfddc4662015-07-27 17:40:30 +08001739 config_vni_xml="""
1740 <config>
1741 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1742 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1743 <ofdpa10:id>VNID</ofdpa10:id>
1744 <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group>
1745 <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id>
1746 </ofdpa10:vni>
1747 </of11-config:capable-switch>
1748 </config>
Pierbbdf3782016-08-22 17:58:26 -07001749 """
1750 config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1751 config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4))
macauleyfddc4662015-07-27 17:40:30 +08001752 else:
1753 config_vni_xml="""
1754 <config>
1755 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1756 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1757 <ofdpa10:id>VNID</ofdpa10:id>
1758 </ofdpa10:vni>
1759 </of11-config:capable-switch>
1760 </config>
Pierbbdf3782016-08-22 17:58:26 -07001761 """
1762
1763 config_vni_xml=config_vni_xml.replace("VNID", str(vni_id))
1764 config_vni_xml=config_vni_xml.replace("OPERATION", str(operation))
macauleyfddc4662015-07-27 17:40:30 +08001765 return config_vni_xml
Pierbbdf3782016-08-22 17:58:26 -07001766
1767def get_featureReplay(self):
macauleyfddc4662015-07-27 17:40:30 +08001768 req = ofp.message.features_request()
1769 res, raw = self.controller.transact(req)
Pierbbdf3782016-08-22 17:58:26 -07001770 self.assertIsNotNone(res, "Did not receive a response from the DUT.")
macauleyfddc4662015-07-27 17:40:30 +08001771 self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY,
1772 ("Unexpected packet type %d received in response to "
1773 "OFPT_FEATURES_REQUEST") % res.type)
Pierbbdf3782016-08-22 17:58:26 -07001774 return res
1775
macauleyfddc4662015-07-27 17:40:30 +08001776def send_edit_config(switch_ip, xml, target='runing'):
1777 NETCONF_ACCOUNT="netconfuser"
1778 NETCONF_PASSWD="netconfuser"
1779 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1780 try:
Pierbbdf3782016-08-22 17:58:26 -07001781 m.edit_config(target='running',
1782 config=xml,
1783 default_operation='merge',
macauleyfddc4662015-07-27 17:40:30 +08001784 error_option='stop-on-error')
1785
1786 except Exception as e:
1787 logging.info("Fail to set xml %s", xml)
1788 return False
1789
Pier265ad5f2017-02-28 17:46:28 +01001790 #return m.get_config(source='running').data_xml
macauleyfddc4662015-07-27 17:40:30 +08001791 return True
1792
1793def send_delete_config(switch_ip, xml, target='runing'):
1794 NETCONF_ACCOUNT="netconfuser"
1795 NETCONF_PASSWD="netconfuser"
1796 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1797 try:
Pierbbdf3782016-08-22 17:58:26 -07001798 m.edit_config(target='running',
1799 config=xml,
1800 default_operation='delete',
macauleyfddc4662015-07-27 17:40:30 +08001801 error_option='stop-on-error')
1802
1803 except Exception as e:
1804 logging.info("Fail to set xml %s", xml)
1805 return False
1806
Pier265ad5f2017-02-28 17:46:28 +01001807 #return m.get_config(source='running').data_xml
macauleyfddc4662015-07-27 17:40:30 +08001808 return True
Pierbbdf3782016-08-22 17:58:26 -07001809
macauleyfddc4662015-07-27 17:40:30 +08001810def get_edit_config(switch_ip, target='runing'):
1811 NETCONF_ACCOUNT="netconfuser"
1812 NETCONF_PASSWD="netconfuser"
1813 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
Pier265ad5f2017-02-28 17:46:28 +01001814 return m.get_config(source='running').data_xml
macauleydbff3272015-07-30 14:07:16 +08001815
macauley_cheng67da9262015-08-31 15:18:41 +08001816
1817"""
1818MPLS
1819"""
1820
1821OFDPA_MPLS_SUBTYPE_SHIFT=24
Pierbbdf3782016-08-22 17:58:26 -07001822OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1
macauley_cheng67da9262015-08-31 15:18:41 +08001823OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2
1824OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3
1825OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4
1826OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5
1827OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6
1828OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8
1829OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10
1830
Piercf76e802016-09-19 20:16:35 -07001831
1832
1833
macauley_cheng67da9262015-08-31 15:18:41 +08001834def encode_mpls_interface_group_id(subtype, index):
1835 index=index&0x00ffffff
1836 assert(subtype==0)
1837 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1838
1839def encode_mpls_label_group_id(subtype, index):
1840 index=index&0x00ffffff
1841 assert(subtype <=5 or subtype==0)
1842 #1: l2 vpn label
1843 #2: l3 vpn label
1844 #3: mpls tunnel label 1
1845 #4: mpls tunnel lable 2
1846 #5: mpls swap label
Pierbbdf3782016-08-22 17:58:26 -07001847 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
macauley_cheng67da9262015-08-31 15:18:41 +08001848
1849def encode_mpls_forwarding_group_id(subtype, index):
1850 index=index&0x00ffffff
1851 assert(subtype==6 or subtype==8 or subtype==10)
Pierbbdf3782016-08-22 17:58:26 -07001852 return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
macauley_cheng67da9262015-08-31 15:18:41 +08001853
1854
Andreas Pantelopoulosaaa02622018-04-04 15:13:03 -07001855def add_mpls_intf_group(ctrl, ref_gid, dst_mac, src_mac, vid, index, subtype=0, send_barrier=False, add=True):
macauley_cheng67da9262015-08-31 15:18:41 +08001856 action=[]
1857 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
1858 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
Pier265ad5f2017-02-28 17:46:28 +01001859 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001860 action.append(ofp.action.group(ref_gid))
Pierbbdf3782016-08-22 17:58:26 -07001861
macauley_cheng67da9262015-08-31 15:18:41 +08001862 buckets = [ofp.bucket(actions=action)]
1863
1864 mpls_group_id =encode_mpls_interface_group_id(subtype, index)
Andreas Pantelopoulosaaa02622018-04-04 15:13:03 -07001865 if add:
1866 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1867 group_id=mpls_group_id,
1868 buckets=buckets
1869 )
1870 else:
1871 request = ofp.message.group_modify(group_type=ofp.OFPGT_INDIRECT,
1872 group_id=mpls_group_id,
1873 buckets=buckets
1874 )
1875
Andreas Pantelopoulos6c76b942018-01-22 10:03:02 -08001876
1877 logging.debug("Adding MPLS interface group %02x, src_mac %s , dst_mac %s , vid %d", mpls_group_id, src_mac, dst_mac, vid)
macauley_cheng67da9262015-08-31 15:18:41 +08001878 ctrl.message_send(request)
Pier1e4e98e2016-10-26 14:36:05 -07001879
1880 if send_barrier:
1881 do_barrier(ctrl)
macauley_cheng67da9262015-08-31 15:18:41 +08001882 return mpls_group_id, request
1883
Piercf76e802016-09-19 20:16:35 -07001884def add_mpls_tunnel_label_group(
1885 ctrl,
1886 ref_gid,
1887 subtype,
1888 index,
1889 label,
1890 ):
1891
1892 action=[]
1893 action.append(ofp.action.push_mpls(0x8847))
1894 action.append(ofp.action.set_field(ofp.oxm.mpls_label(label)))
1895 action.append(ofp.action.group(ref_gid))
1896 buckets = [ofp.bucket(actions=action)]
1897
1898 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1899 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1900 group_id=mpls_group_id,
1901 buckets=buckets
1902 )
1903 ctrl.message_send(request)
1904
1905 return mpls_group_id, request
1906
Pierb5da4c92016-09-21 11:23:35 -07001907def add_mpls_swap_label_group(
1908 ctrl,
1909 ref_gid,
1910 subtype,
1911 index,
1912 label,
1913 ):
1914
1915 action=[]
1916 action.append(ofp.action.set_field(ofp.oxm.mpls_label(label)))
1917 action.append(ofp.action.group(ref_gid))
1918 buckets = [ofp.bucket(actions=action)]
1919
1920 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1921 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1922 group_id=mpls_group_id,
1923 buckets=buckets
1924 )
Andreas Pantelopoulos6c76b942018-01-22 10:03:02 -08001925 logging.debug("Adding MPLS Swap group %02x, label %d", mpls_group_id, label)
Pierb5da4c92016-09-21 11:23:35 -07001926 ctrl.message_send(request)
1927
1928 return mpls_group_id, request
1929
Pierbbdf3782016-08-22 17:58:26 -07001930def add_mpls_label_group(ctrl, subtype, index, ref_gid,
macauley_cheng67da9262015-08-31 15:18:41 +08001931 lmep_id=-1,
1932 qos_index=-1,
1933 push_l2_header=False,
1934 push_vlan=False,
1935 push_mpls_header=False,
1936 push_cw=False,
1937 set_mpls_label=None,
1938 set_bos=None,
1939 set_tc=None,
1940 set_tc_from_table=False,
1941 cpy_tc_outward=False,
1942 set_ttl=None,
1943 cpy_ttl_outward=False,
1944 oam_lm_tx_count=False,
Pier1e4e98e2016-10-26 14:36:05 -07001945 set_pri_from_table=False,
1946 send_barrier=False
macauley_cheng67da9262015-08-31 15:18:41 +08001947 ):
1948 """
1949 @ref_gid: only can be mpls intf group or mpls tunnel label 1/2 group
Pierbbdf3782016-08-22 17:58:26 -07001950 """
macauley_cheng67da9262015-08-31 15:18:41 +08001951 action=[]
1952
1953 if push_vlan== True:
1954 action.append(ofp.action.push_vlan(0x8100))
1955 if push_mpls_header== True:
1956 action.append(ofp.action.push_mpls(0x8847))
1957 if set_mpls_label != None:
1958 action.append(ofp.action.set_field(ofp.oxm.mpls_label(set_mpls_label)))
1959 if set_bos != None:
1960 action.append(ofp.action.set_field(ofp.oxm.mpls_bos(set_bos)))
1961 if set_tc != None:
1962 assert(set_tc_from_table==False)
1963 action.append(ofp.action.set_field(ofp.oxm.mpls_tc(set_tc)))
1964 if set_ttl != None:
Pierbbdf3782016-08-22 17:58:26 -07001965 action.append(ofp.action.set_mpls_ttl(set_ttl))
macauley_cheng67da9262015-08-31 15:18:41 +08001966 if cpy_ttl_outward == True:
Pierbbdf3782016-08-22 17:58:26 -07001967 action.append(ofp.action.copy_ttl_out())
macauley_cheng67da9262015-08-31 15:18:41 +08001968 """
1969 ofdpa experimenter
Pierbbdf3782016-08-22 17:58:26 -07001970 """
macauley_cheng67da9262015-08-31 15:18:41 +08001971 if push_l2_header== True:
Pierbbdf3782016-08-22 17:58:26 -07001972 action.append(ofp.action.ofdpa_push_l2_header())
macauley_cheng67da9262015-08-31 15:18:41 +08001973 if set_tc_from_table== True:
1974 assert(qos_index>=0)
1975 assert(set_tc == None)
Pierbbdf3782016-08-22 17:58:26 -07001976 action.append(ofp.action.ofdpa_set_tc_from_table(qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08001977 if cpy_tc_outward == True:
Pierbbdf3782016-08-22 17:58:26 -07001978 action.append(ofp.action.ofdpa_copy_tc_out())
macauley_cheng67da9262015-08-31 15:18:41 +08001979 if oam_lm_tx_count == True:
Pierbbdf3782016-08-22 17:58:26 -07001980 assert(qos_index>=0 and lmep_id>=0)
1981 action.append(ofp.action.ofdpa_oam_lm_tx_count(lmep_id, qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08001982 if set_pri_from_table == True:
Pierbbdf3782016-08-22 17:58:26 -07001983 assert(qos_index>=0)
1984 action.append(ofp.action.ofdpa_set_qos_from_table(qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08001985 if push_cw == True:
1986 action.append(ofp.action.ofdpa_push_cw())
Pierbbdf3782016-08-22 17:58:26 -07001987
1988 action.append(ofp.action.group(ref_gid))
macauley_cheng67da9262015-08-31 15:18:41 +08001989 buckets = [ofp.bucket(actions=action)]
Pierbbdf3782016-08-22 17:58:26 -07001990
macauley_cheng67da9262015-08-31 15:18:41 +08001991 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1992 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1993 group_id=mpls_group_id,
1994 buckets=buckets
1995 )
1996 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -07001997
Pier1e4e98e2016-10-26 14:36:05 -07001998 if send_barrier:
1999 do_barrier(ctrl)
2000
Pierbbdf3782016-08-22 17:58:26 -07002001 return mpls_group_id, request
2002
Saurav Das5d1473d2018-07-12 11:02:56 -07002003def add_mpls_l2_port_flow(ctrl, of_port, mpls_l2_port, tunnel_index, ref_gid, qos_index=0, goto=MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE):
Piercf76e802016-09-19 20:16:35 -07002004 """
2005 Only action is Group, which must indicate one of:
2006 MPLS L2 VPN Label or Fast Failover Protection Group.
2007 ref_gid contains this information
2008 """
Piercf76e802016-09-19 20:16:35 -07002009
2010 match = ofp.match()
Pier23784aa2016-09-19 20:08:21 -07002011
Pier265ad5f2017-02-28 17:46:28 +01002012 write_actions = []
2013 write_actions.append(ofp.action.group(ref_gid))
2014 apply_actions = []
Piercf76e802016-09-19 20:16:35 -07002015
Saurav Das5d1473d2018-07-12 11:02:56 -07002016 if goto==MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE:
2017 tunnel_id = tunnel_index + ofp.oxm.TUNNEL_ID_BASE
2018 match.oxm_list.append(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port))
2019 match.oxm_list.append(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE))
2020 assert(qos_index>=0)
2021 apply_actions.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_QOS_INDEX, value=qos_index)))
2022
2023 request = ofp.message.flow_add(
Piercf76e802016-09-19 20:16:35 -07002024 table_id=MPLS_L2_PORT_FLOW_TABLE,
2025 cookie=42,
2026 match=match,
2027 instructions=[
Pier265ad5f2017-02-28 17:46:28 +01002028 ofp.instruction.apply_actions(actions=apply_actions),
2029 ofp.instruction.write_actions(actions=write_actions),
Saurav Das5d1473d2018-07-12 11:02:56 -07002030 ofp.instruction.goto_table(goto)
Pier265ad5f2017-02-28 17:46:28 +01002031 ],
Piercf76e802016-09-19 20:16:35 -07002032 buffer_id=ofp.OFP_NO_BUFFER,
2033 priority=1)
Saurav Das5d1473d2018-07-12 11:02:56 -07002034 logging.info("Inserting flow for Pseudowire Initiation %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)
2035 ctrl.message_send(request)
Piercf76e802016-09-19 20:16:35 -07002036
Saurav Das5d1473d2018-07-12 11:02:56 -07002037 if goto==ACL_FLOW_TABLE:
2038 tunnel_id = tunnel_index + ofp.oxm.TUNNEL_ID_BASE
2039 match.oxm_list.append(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port))
2040 match.oxm_list.append(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE_CROSS_CONNECT))
2041 request = ofp.message.flow_add(
2042 table_id=MPLS_L2_PORT_FLOW_TABLE,
2043 cookie=42,
2044 match=match,
2045 instructions=[
2046 ofp.instruction.apply_actions(actions=apply_actions),
2047 ofp.instruction.write_actions(actions=write_actions),
2048 ofp.instruction.goto_table(goto)
2049 ],
2050 buffer_id=ofp.OFP_NO_BUFFER,
2051 priority=1)
2052 logging.info("Inserting flow for VLAN Cross Connect %d mpls_l2_port, %d tunnel_id, action %x group and go to table %d", mpls_l2_port, tunnel_id, ref_gid, ACL_FLOW_TABLE)
2053 ctrl.message_send(request)
2054
2055 return request
Piercf76e802016-09-19 20:16:35 -07002056
Pierbbdf3782016-08-22 17:58:26 -07002057def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids,
2058 watch_port=None,
Pier265ad5f2017-02-28 17:46:28 +01002059 watch_group=ofp.OFPP_ANY,
2060 push_vlan=None,
macauley_chengd17ce512015-08-31 17:45:51 +08002061 pop_vlan=None,
macauley_cheng67da9262015-08-31 15:18:41 +08002062 set_vid=None):
2063 assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP
Pier265ad5f2017-02-28 17:46:28 +01002064 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP
2065 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG)
macauley_chengd17ce512015-08-31 17:45:51 +08002066
macauley_cheng67da9262015-08-31 15:18:41 +08002067 buckets=[]
2068 if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP:
macauley_chengd17ce512015-08-31 17:45:51 +08002069 group_type = ofp.OFPGT_FF
macauley_cheng67da9262015-08-31 15:18:41 +08002070 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08002071 action=[]
Pierbbdf3782016-08-22 17:58:26 -07002072 action.append(ofp.action.group(gid))
macauley_chengd17ce512015-08-31 17:45:51 +08002073 buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action))
2074
macauley_cheng67da9262015-08-31 15:18:41 +08002075 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP:
2076 group_type = ofp.OFPGT_SELECT
2077 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08002078 action=[]
Pierbbdf3782016-08-22 17:58:26 -07002079 action.append(ofp.action.group(gid))
macauley_cheng67da9262015-08-31 15:18:41 +08002080 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08002081
macauley_cheng67da9262015-08-31 15:18:41 +08002082 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG:
2083 group_type = ofp.OFPGT_INDIRECT
macauley_chengd17ce512015-08-31 17:45:51 +08002084 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08002085 if set_vid!=None:
Flavio Castro91d1a552016-05-17 16:59:44 -07002086 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+set_vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08002087 if push_vlan!=None:
Pierbbdf3782016-08-22 17:58:26 -07002088 action.append(ofp.action.push_vlan(push_vlan))
macauley_cheng67da9262015-08-31 15:18:41 +08002089 if pop_vlan!=None:
Pierbbdf3782016-08-22 17:58:26 -07002090 action.append(ofp.action.pop_vlan())
2091 action.append(ofp.action.group(ref_gids[0]))
macauley_cheng67da9262015-08-31 15:18:41 +08002092 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08002093
2094 mpls_group_id = encode_mpls_forwarding_group_id(subtype, index)
macauley_cheng67da9262015-08-31 15:18:41 +08002095 request = ofp.message.group_add(group_type=group_type,
macauley_cheng67da9262015-08-31 15:18:41 +08002096 group_id=mpls_group_id,
2097 buckets=buckets
2098 )
2099 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -07002100 return mpls_group_id, request
macauley_chengd17ce512015-08-31 17:45:51 +08002101
Jonghwan Hyunff0dfd52018-03-20 15:04:35 -07002102def add_one_egress_vlan_tpid_table_flow(ctrl, of_port):
2103 # Used for changing ethertype of outer vlan header to 0x88a8
2104
2105 match = ofp.match()
2106 match.oxm_list.append(ofp.oxm.exp4ByteValue(ofp.oxm.OFDPA_EXP_TYPE_ACTSET_OUTPUT, of_port, ONF_EXPERIMENTER_ID))
2107 match.oxm_list.append(ofp.oxm.vlan_vid_masked(ofp.OFPVID_PRESENT, ofp.OFPVID_PRESENT))
2108
2109 actions = []
2110 actions.append(ofp.action.copy_field(
2111 12, 0, 0, ['\x80\x00\x0c\x02', ofp.oxm.exp4ByteReg(oxm_field = 1).pack()])) # VLAN_VID, PACKET_REG(1)
2112 actions.append(ofp.action.pop_vlan())
2113 actions.append(ofp.action.push_vlan(0x88a8))
2114 actions.append(ofp.action.copy_field(
2115 12, 0, 0, [ofp.oxm.exp4ByteReg(oxm_field = 1).pack(), '\x80\x00\x0c\x02'])) # PACKET_REG(1), VLAN_VID
2116
2117 request = ofp.message.flow_add(
2118 table_id=EGRESS_TPID_FLOW_TABLE,
2119 cookie=42,
2120 match=match,
2121 instructions=[
2122 ofp.instruction.apply_actions(
2123 actions=actions
2124 ),
2125 ],
2126 priority=0)
2127
2128 ctrl.message_send(request)
2129
2130 return
macauley_chengd17ce512015-08-31 17:45:51 +08002131
macauley_cheng67da9262015-08-31 15:18:41 +08002132"""
Piercf76e802016-09-19 20:16:35 -07002133display
Pierbbdf3782016-08-22 17:58:26 -07002134"""
macauleydbff3272015-07-30 14:07:16 +08002135def print_current_table_flow_stat(ctrl, table_id=0xff):
2136 stat_req=ofp.message.flow_stats_request()
2137 response, pkt = ctrl.transact(stat_req)
2138 if response == None:
2139 print "no response"
2140 return None
2141 print len(response.entries)
2142 for obj in response.entries:
2143 print "match ", obj.match
2144 print "cookie", obj.cookie
2145 print "priority", obj.priority
2146 print "idle_timeout", obj.idle_timeout
2147 print "hard_timeout", obj.hard_timeout
2148 #obj.actions
Flavio Castro167f5bd2015-12-02 19:33:53 -05002149 print "packet count: %lx"%obj.packet_count