blob: eb62241b7f78f9e3eacdf161220ea37a02ebe199 [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
Saurav Dasb4b841e2018-08-17 15:51:56 -0700163def encode_l2_loadbal_group_id(id):
164 return id + (12 << OFDPA_GROUP_TYPE_SHIFT)
165
macauleyfddc4662015-07-27 17:40:30 +0800166def encode_l2_overlay_group_id(tunnel_id, subtype, index):
167 tunnel_id=tunnel_id&0xffff #16 bits
168 subtype = subtype&3 #2 bits
169 index = index & 0x3f #10 bits
170 return index + (tunnel_id << OFDPA_TUNNEL_ID_SHIFT)+ (subtype<<OFDPA_TUNNEL_SUBTYPE_SHIFT)+(8 << OFDPA_GROUP_TYPE_SHIFT)
macauley97557232015-07-16 17:28:07 +0800171
Saurav Das5d1473d2018-07-12 11:02:56 -0700172def add_l2_unfiltered_group(ctrl, ports, send_barrier=False, allow_vlan_translation=1):
Flavio Castro91d1a552016-05-17 16:59:44 -0700173 # group table
174 # set up untag groups for each port
175 group_id_list=[]
176 msgs=[]
177 for of_port in ports:
178 # do stuff
179 group_id = encode_l2_unfiltered_group_id(of_port)
180 group_id_list.append(group_id)
181 actions = [ofp.action.output(of_port)]
Flavio Castro91d1a552016-05-17 16:59:44 -0700182
Saurav Das5d1473d2018-07-12 11:02:56 -0700183 actions.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=24, value=allow_vlan_translation)))
Flavio Castro91d1a552016-05-17 16:59:44 -0700184 buckets = [ofp.bucket(actions=actions)]
185 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
186 group_id=group_id,
187 buckets=buckets
188 )
189 ctrl.message_send(request)
190 msgs.append(request)
191
192 if send_barrier:
193 do_barrier(ctrl)
194
195 return group_id_list, msgs
196
Pierf6f28162016-09-22 16:30:52 -0700197def add_one_l2_unfiltered_group(ctrl, of_port, send_barrier=False):
198 # group table
199 # set up untag groups for each port
200 group_id = encode_l2_unfiltered_group_id(of_port)
201 actions = [ofp.action.output(of_port)]
202 actions.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=24, value=1)))
203
204 buckets = [ofp.bucket(actions=actions)]
205 request = ofp.message.group_add(
206 group_type=ofp.OFPGT_INDIRECT,
207 group_id=group_id,
208 buckets=buckets
209 )
210 ctrl.message_send(request)
211
212 if send_barrier:
213 do_barrier(ctrl)
214
215 return group_id, request
216
Flavio Castrod4c44d12015-12-08 14:44:18 -0500217def add_l2_interface_group(ctrl, ports, vlan_id=1, is_tagged=False, send_barrier=False):
macauley97557232015-07-16 17:28:07 +0800218 # group table
219 # set up untag groups for each port
macauley41904ed2015-07-16 17:38:35 +0800220 group_id_list=[]
macauley15909e72015-07-17 15:58:57 +0800221 msgs=[]
macauley97557232015-07-16 17:28:07 +0800222 for of_port in ports:
223 # do stuff
224 group_id = encode_l2_interface_group_id(vlan_id, of_port)
macauley41904ed2015-07-16 17:38:35 +0800225 group_id_list.append(group_id)
macauley97557232015-07-16 17:28:07 +0800226 if is_tagged:
227 actions = [
228 ofp.action.output(of_port),
Pierbbdf3782016-08-22 17:58:26 -0700229 ]
macauley97557232015-07-16 17:28:07 +0800230 else:
231 actions = [
232 ofp.action.pop_vlan(),
233 ofp.action.output(of_port),
234 ]
235
236 buckets = [
237 ofp.bucket(actions=actions),
238 ]
239
240 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
241 group_id=group_id,
242 buckets=buckets
243 )
244 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800245 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800246
247 if send_barrier:
248 do_barrier(ctrl)
Pierbbdf3782016-08-22 17:58:26 -0700249
macauley15909e72015-07-17 15:58:57 +0800250 return group_id_list, msgs
macauley97557232015-07-16 17:28:07 +0800251
Flavio Castrod4c44d12015-12-08 14:44:18 -0500252def add_one_l2_interface_group(ctrl, port, vlan_id=1, is_tagged=False, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800253 # group table
254 # set up untag groups for each port
255 group_id = encode_l2_interface_group_id(vlan_id, port)
256
257 if is_tagged:
258 actions = [
259 ofp.action.output(port),
Pierbbdf3782016-08-22 17:58:26 -0700260 ]
macauley0f91a3e2015-07-17 18:09:59 +0800261 else:
262 actions = [
263 ofp.action.pop_vlan(),
264 ofp.action.output(port),
265 ]
266
267 buckets = [
268 ofp.bucket(actions=actions),
269 ]
270
271 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
272 group_id=group_id,
273 buckets=buckets
274 )
275 ctrl.message_send(request)
276
277 if send_barrier:
278 do_barrier(ctrl)
Pierbbdf3782016-08-22 17:58:26 -0700279
macauley0f91a3e2015-07-17 18:09:59 +0800280 return group_id, request
Pierbbdf3782016-08-22 17:58:26 -0700281
macauley97557232015-07-16 17:28:07 +0800282def add_l2_mcast_group(ctrl, ports, vlanid, mcast_grp_index):
283 buckets=[]
284 for of_port in ports:
285 group_id = encode_l2_interface_group_id(vlanid, of_port)
286 action=[ofp.action.group(group_id)]
287 buckets.append(ofp.bucket(actions=action))
288
289 group_id =encode_l2_mcast_group_id(vlanid, mcast_grp_index)
290 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
291 group_id=group_id,
292 buckets=buckets
293 )
294 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800295 return request
macauley97557232015-07-16 17:28:07 +0800296
macauley15909e72015-07-17 15:58:57 +0800297def add_l2_flood_group(ctrl, ports, vlanid, id):
298 buckets=[]
299 for of_port in ports:
300 group_id = encode_l2_interface_group_id(vlanid, of_port)
301 action=[ofp.action.group(group_id)]
302 buckets.append(ofp.bucket(actions=action))
macauley97557232015-07-16 17:28:07 +0800303
macauley15909e72015-07-17 15:58:57 +0800304 group_id =encode_l2_flood_group_id(vlanid, id)
305 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
306 group_id=group_id,
307 buckets=buckets
308 )
309 ctrl.message_send(request)
310 return request
311
Charles Chan00de0422018-10-09 14:44:00 -0700312def add_l2_flood_group_with_gids(ctrl, gids, vlanid, id, send_barrier=False):
Saurav Dasb4b841e2018-08-17 15:51:56 -0700313 buckets=[]
314 for gid in gids:
315 action=[ofp.action.group(gid)]
316 buckets.append(ofp.bucket(actions=action))
317
318 group_id =encode_l2_flood_group_id(vlanid, id)
319 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
320 group_id=group_id,
321 buckets=buckets
322 )
323 ctrl.message_send(request)
Charles Chan00de0422018-10-09 14:44:00 -0700324
325 if send_barrier:
326 do_barrier(ctrl)
327
Saurav Dasb4b841e2018-08-17 15:51:56 -0700328 return request
329
Flavio Castroa7162bb2016-07-25 17:30:30 -0700330def mod_l2_flood_group(ctrl, ports, vlanid, id):
331 buckets=[]
332 for of_port in ports:
333 group_id = encode_l2_interface_group_id(vlanid, of_port)
334 action=[ofp.action.group(group_id)]
335 buckets.append(ofp.bucket(actions=action))
336
337 group_id =encode_l2_flood_group_id(vlanid, id)
338 request = ofp.message.group_modify(group_type=ofp.OFPGT_ALL,
339 group_id=group_id,
340 buckets=buckets
341 )
342 ctrl.message_send(request)
343 return request
344
345
macauley15909e72015-07-17 15:58:57 +0800346def add_l2_rewrite_group(ctrl, port, vlanid, id, src_mac, dst_mac):
347 group_id = encode_l2_interface_group_id(vlanid, port)
348
349 action=[]
350 if src_mac is not None:
351 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
352
353 if dst_mac is not None:
354 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
355
Flavio Castro91d1a552016-05-17 16:59:44 -0700356 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
Pierbbdf3782016-08-22 17:58:26 -0700357
macauley15909e72015-07-17 15:58:57 +0800358 action.append(ofp.action.group(group_id))
Pierbbdf3782016-08-22 17:58:26 -0700359
macauley15909e72015-07-17 15:58:57 +0800360 buckets = [ofp.bucket(actions=action)]
361
362 group_id =encode_l2_rewrite_group_id(id)
363 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
364 group_id=group_id,
365 buckets=buckets
366 )
367 ctrl.message_send(request)
368 return request
Pierbbdf3782016-08-22 17:58:26 -0700369
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700370def add_l3_unicast_group(ctrl, port, vlanid, id, src_mac, dst_mac, send_barrier=False, gid=None):
371
372 if (not gid):
373 group_id = encode_l2_interface_group_id(vlanid, port)
374 else:
375 group_id = gid
macauley15909e72015-07-17 15:58:57 +0800376
377 action=[]
378 if src_mac is not None:
379 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
380
381 if dst_mac is not None:
382 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
383
Flavio Castroaf2b4502016-02-02 17:41:32 -0500384 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
Pierbbdf3782016-08-22 17:58:26 -0700385
macauley15909e72015-07-17 15:58:57 +0800386 action.append(ofp.action.group(group_id))
Pierbbdf3782016-08-22 17:58:26 -0700387
macauley15909e72015-07-17 15:58:57 +0800388 buckets = [ofp.bucket(actions=action)]
389
390 group_id =encode_l3_unicast_group_id(id)
391 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
392 group_id=group_id,
393 buckets=buckets
394 )
395 ctrl.message_send(request)
Pier1e4e98e2016-10-26 14:36:05 -0700396
397 if send_barrier:
398 do_barrier(ctrl)
399
macauley15909e72015-07-17 15:58:57 +0800400 return request
Pierbbdf3782016-08-22 17:58:26 -0700401
macauley15909e72015-07-17 15:58:57 +0800402def add_l3_interface_group(ctrl, port, vlanid, id, src_mac):
403 group_id = encode_l2_interface_group_id(vlanid, port)
404
405 action=[]
406 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
Pierbbdf3782016-08-22 17:58:26 -0700407 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
macauley15909e72015-07-17 15:58:57 +0800408 action.append(ofp.action.group(group_id))
Pierbbdf3782016-08-22 17:58:26 -0700409
macauley15909e72015-07-17 15:58:57 +0800410 buckets = [ofp.bucket(actions=action)]
411
412 group_id =encode_l3_interface_group_id(id)
413 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
414 group_id=group_id,
415 buckets=buckets
416 )
417 ctrl.message_send(request)
418 return request
419
Saurav Dasb4b841e2018-08-17 15:51:56 -0700420
421def add_l2_loadbal_group(ctrl, id, l2_unfil_intf_groups, send_barrier=False):
422 buckets=[]
423 for group in l2_unfil_intf_groups:
424 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
425
426 group_id=encode_l2_loadbal_group_id(id)
427 request = ofp.message.group_add(group_type=ofp.OFPGT_SELECT,
428 group_id=group_id,
429 buckets=buckets
430 )
431 ctrl.message_send(request)
432 if send_barrier:
433 do_barrier(ctrl)
434
435 return group_id, request
436
437def mod_l2_loadbal_group(ctrl, id, l2_unfil_intf_groups, send_barrier=False):
438 buckets=[]
439 for group in l2_unfil_intf_groups:
440 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
441
442 group_id =encode_l2_loadbal_group_id(id)
443 request = ofp.message.group_modify(group_type=ofp.OFPGT_SELECT,
444 group_id=group_id,
445 buckets=buckets
446 )
447 ctrl.message_send(request)
448 return request
449
450
Pier1e4e98e2016-10-26 14:36:05 -0700451def add_l3_ecmp_group(ctrl, id, l3_ucast_groups, send_barrier=False):
macauley15909e72015-07-17 15:58:57 +0800452 buckets=[]
453 for group in l3_ucast_groups:
454 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
455
456 group_id =encode_l3_ecmp_group_id(id)
457 request = ofp.message.group_add(group_type=ofp.OFPGT_SELECT,
458 group_id=group_id,
459 buckets=buckets
460 )
461 ctrl.message_send(request)
Pier1e4e98e2016-10-26 14:36:05 -0700462
463 if send_barrier:
464 do_barrier(ctrl)
465
macauley15909e72015-07-17 15:58:57 +0800466 return request
Flavio Castroa7162bb2016-07-25 17:30:30 -0700467
468def mod_l3_ecmp_group(ctrl, id, l3_ucast_groups):
469 buckets=[]
470 for group in l3_ucast_groups:
471 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
472
473 group_id =encode_l3_ecmp_group_id(id)
474 request = ofp.message.group_modify(group_type=ofp.OFPGT_SELECT,
475 group_id=group_id,
476 buckets=buckets
477 )
478 ctrl.message_send(request)
479 return request
480
macauley15909e72015-07-17 15:58:57 +0800481def add_l3_mcast_group(ctrl, vid, mcast_group_id, groups_on_buckets):
482 buckets=[]
483 for group in groups_on_buckets:
484 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
Pierbbdf3782016-08-22 17:58:26 -0700485
macauley15909e72015-07-17 15:58:57 +0800486 group_id =encode_l3_mcast_group_id(vid, mcast_group_id)
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
macauleyfddc4662015-07-27 17:40:30 +0800493
494def add_l2_overlay_flood_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
495 buckets=[]
496 for port in ports:
497 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
498
499 group_id=encode_l2_overlay_group_id(tunnel_id, 0, index)
500 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
501 group_id=group_id,
502 buckets=buckets
503 )
504 ctrl.message_send(request)
505 return request
506
507def add_l2_overlay_flood_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
508 buckets=[]
509 for port in ports:
510 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
511
512 group_id=encode_l2_overlay_group_id(tunnel_id, 1, index)
513 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
514 group_id=group_id,
515 buckets=buckets
516 )
517 ctrl.message_send(request)
518 return request
519
520def add_l2_overlay_mcast_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
521 buckets=[]
522 for port in ports:
523 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
524
525 group_id=encode_l2_overlay_group_id(tunnel_id, 2, index)
526 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
527 group_id=group_id,
528 buckets=buckets
529 )
530 ctrl.message_send(request)
531 return request
532
533def add_l2_overlay_mcast_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
534 buckets=[]
535 for port in ports:
536 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
537
538 group_id=encode_l2_overlay_group_id(tunnel_id, 3, index)
539 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
540 group_id=group_id,
541 buckets=buckets
542 )
543 ctrl.message_send(request)
544 return request
Pierbbdf3782016-08-22 17:58:26 -0700545
macauleyfddc4662015-07-27 17:40:30 +0800546def add_port_table_flow(ctrl, is_overlay=True):
547 match = ofp.match()
548
549 if is_overlay == True:
550 match.oxm_list.append(ofp.oxm.in_port(0x10000))
macauleydbff3272015-07-30 14:07:16 +0800551 NEXT_TABLE=50
macauleyfddc4662015-07-27 17:40:30 +0800552 else:
553 match.oxm_list.append(ofp.oxm.in_port(0))
Pierbbdf3782016-08-22 17:58:26 -0700554 NEXT_TABLE=10
macauleyfddc4662015-07-27 17:40:30 +0800555
556 request = ofp.message.flow_add(
Pier265ad5f2017-02-28 17:46:28 +0100557 table_id=0,
558 cookie=42,
559 match=match,
560 instructions=[
561 ofp.instruction.goto_table(NEXT_TABLE)
562 ],
563 priority=0)
macauleyfddc4662015-07-27 17:40:30 +0800564 logging.info("Add port table, match port %lx" % 0x10000)
565 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700566
Flavio Castrod8f8af22015-12-02 18:19:26 -0500567def pop_vlan_flow(ctrl, ports, vlan_id=1):
568 # table 10: vlan
569 # goto to table 20
570 msgs=[]
571 for of_port in ports:
572 match = ofp.match()
573 match.oxm_list.append(ofp.oxm.in_port(of_port))
574 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
575 request = ofp.message.flow_add(
576 table_id=10,
577 cookie=42,
578 match=match,
579 instructions=[
580 ofp.instruction.apply_actions(
581 actions=[
582 ofp.action.pop_vlan()
583 ]
584 ),
585 ofp.instruction.goto_table(20)
586 ],
587 priority=0)
588 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
589 ctrl.message_send(request)
590
591
592 return msgs
macauleyfddc4662015-07-27 17:40:30 +0800593
macauley97557232015-07-16 17:28:07 +0800594def add_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
595 # table 10: vlan
596 # goto to table 20
macauley15909e72015-07-17 15:58:57 +0800597 msgs=[]
macauley97557232015-07-16 17:28:07 +0800598 for of_port in ports:
Flavio Castro932014b2016-01-05 18:29:15 -0500599 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
macauley97557232015-07-16 17:28:07 +0800600 match = ofp.match()
601 match.oxm_list.append(ofp.oxm.in_port(of_port))
602 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
603 request = ofp.message.flow_add(
604 table_id=10,
605 cookie=42,
606 match=match,
607 instructions=[
Flavio Castrod8f8af22015-12-02 18:19:26 -0500608 ofp.instruction.apply_actions(
609 actions=[
610 ofp.action.pop_vlan()
611 ]
612 ),
macauley97557232015-07-16 17:28:07 +0800613 ofp.instruction.goto_table(20)
614 ],
615 priority=0)
616 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
617 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700618
macauley97557232015-07-16 17:28:07 +0800619 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
620 match = ofp.match()
621 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500622 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
macauley97557232015-07-16 17:28:07 +0800623 request = ofp.message.flow_add(
624 table_id=10,
625 cookie=42,
626 match=match,
627 instructions=[
628 ofp.instruction.apply_actions(
629 actions=[
Flavio Castroaf2b4502016-02-02 17:41:32 -0500630 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
macauley97557232015-07-16 17:28:07 +0800631 ]
632 ),
633 ofp.instruction.goto_table(20)
634 ],
635 priority=0)
636 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
637 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800638 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800639
Flavio Castro932014b2016-01-05 18:29:15 -0500640 if (flag == 4) :
641 match = ofp.match()
642 match.oxm_list.append(ofp.oxm.in_port(of_port))
643 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1fff))
644 request = ofp.message.flow_add(
645 table_id=10,
646 cookie=42,
647 match=match,
648 instructions=[
649 ofp.instruction.apply_actions(
650 actions=[
Flavio Castroaf2b4502016-02-02 17:41:32 -0500651 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
Flavio Castro932014b2016-01-05 18:29:15 -0500652 ]
653 ),
654 ofp.instruction.goto_table(20)
655 ],
656 priority=0)
657 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
658 ctrl.message_send(request)
659 msgs.append(request)
660
macauley97557232015-07-16 17:28:07 +0800661 if send_barrier:
662 do_barrier(ctrl)
663
macauley15909e72015-07-17 15:58:57 +0800664 return msgs
Pierbbdf3782016-08-22 17:58:26 -0700665
macauley_cheng6e6a6122015-11-16 14:19:18 +0800666def del_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
667 # table 10: vlan
668 # goto to table 20
669 msgs=[]
670 for of_port in ports:
671 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
672 match = ofp.match()
673 match.oxm_list.append(ofp.oxm.in_port(of_port))
674 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
675 request = ofp.message.flow_delete(
676 table_id=10,
677 cookie=42,
678 match=match,
679 priority=0)
680 logging.info("Del vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
681 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800682
macauley_cheng6e6a6122015-11-16 14:19:18 +0800683 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
684 match = ofp.match()
685 match.oxm_list.append(ofp.oxm.in_port(of_port))
686 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0xfff))
687 request = ofp.message.flow_delete(
688 table_id=10,
689 cookie=42,
690 match=match,
691 priority=0)
692 logging.info("Del vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
693 ctrl.message_send(request)
694 msgs.append(request)
695
696 if send_barrier:
697 do_barrier(ctrl)
698
699 return msgs
Pierbbdf3782016-08-22 17:58:26 -0700700
macauley_cheng6b311612015-09-04 11:32:27 +0800701def add_vlan_table_flow_pvid(ctrl, in_port, match_vid=None, pvid=1, send_barrier=False):
702 """it will tag pack as untagged packet wether it has tagg or not"""
703 match = ofp.match()
704 match.oxm_list.append(ofp.oxm.in_port(in_port))
705 actions=[]
706 if match_vid == None:
Pierbbdf3782016-08-22 17:58:26 -0700707 match.oxm_list.append(ofp.oxm.vlan_vid(0))
macauley_cheng6b311612015-09-04 11:32:27 +0800708 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
709 goto_table=20
710 else:
711 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+match_vid, 0x1fff))
712 actions.append(ofp.action.push_vlan(0x8100))
Pierbbdf3782016-08-22 17:58:26 -0700713 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
macauley_cheng6b311612015-09-04 11:32:27 +0800714 goto_table=20
Pierbbdf3782016-08-22 17:58:26 -0700715
macauley_cheng6b311612015-09-04 11:32:27 +0800716 request = ofp.message.flow_add(
717 table_id=10,
718 cookie=42,
719 match=match,
720 instructions=[
721 ofp.instruction.apply_actions(actions=actions)
722 ,ofp.instruction.goto_table(goto_table)
723 ],
724 priority=0)
725 logging.info("Add PVID %d on port %d and go to table %ld" %( pvid, in_port, goto_table))
Pierbbdf3782016-08-22 17:58:26 -0700726 ctrl.message_send(request)
727
macauley_cheng6b311612015-09-04 11:32:27 +0800728 if send_barrier:
729 do_barrier(ctrl)
730
731def add_vlan_table_flow_allow_all_vlan(ctrl, in_port, send_barrier=False):
732 """it st flow allow all vlan tag on this port"""
733 match = ofp.match()
734 match.oxm_list.append(ofp.oxm.in_port(in_port))
735 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1000))
736 request = ofp.message.flow_add(
737 table_id=10,
738 cookie=42,
739 match=match,
740 instructions=[
Pierbbdf3782016-08-22 17:58:26 -0700741 ofp.instruction.goto_table(20)
macauley_cheng6b311612015-09-04 11:32:27 +0800742 ],
743 priority=0)
744 logging.info("Add allow all vlan on port %d " %(in_port))
Pierbbdf3782016-08-22 17:58:26 -0700745 ctrl.message_send(request)
macauley_cheng6b311612015-09-04 11:32:27 +0800746
Pier7b031af2016-08-25 15:00:22 -0700747def 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):
748 # Install a flow for VLAN translation
749 # in VLAN table.
750 # table 10: vlan
751 # goto to table 20
752 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
753 match = ofp.match()
754 match.oxm_list.append(ofp.oxm.in_port(of_port))
755 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
756
757 actions=[]
758 if vrf!=0:
759 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
760 if new_vlan_id != -1:
761 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_vlan_id)))
762
763 request = ofp.message.flow_add(
764 table_id=10,
765 cookie=42,
766 match=match,
767 instructions=[
768 ofp.instruction.apply_actions(
769 actions=actions
770 ),
771 ofp.instruction.goto_table(20)
772 ],
773 priority=0)
774 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
775 ctrl.message_send(request)
776
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700777
778def add_one_egress_vlan_table_flow(ctrl, of_port, match_vlan, inner_vlan, outer_vlan):
779
780 # used for translating single to double tagged packets only
781
782 match = ofp.match()
783 match.oxm_list.append(ofp.oxm.exp4ByteValue(ofp.oxm.OFDPA_EXP_TYPE_ACTSET_OUTPUT, of_port))
784 match.oxm_list.append(ofp.oxm.exp1ByteValue(ofp.oxm.OFDPA_EXP_TYPE_ALLOW_VLAN_TRANSLATION, 1))
785 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+match_vlan,0x1fff))
786
787 actions=[]
788 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+inner_vlan)))
789 actions.append(ofp.action.push_vlan(0x8100))
790 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan)))
791
792 request = ofp.message.flow_add(
793 table_id=EGRESS_VLAN_FLOW_TABLE,
794 cookie=42,
795 match=match,
796 instructions=[
797 ofp.instruction.apply_actions(
798 actions=actions
799 ),
800 ofp.instruction.goto_table(EGRESS_DSCP_TABLE)
801 ],
802 priority=0)
803
804 ctrl.message_send(request)
805
806 return
807
Piere1308762016-09-12 15:29:56 -0700808def 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 -0700809
macauley0f91a3e2015-07-17 18:09:59 +0800810 # goto to table 20
Flavio Castro932014b2016-01-05 18:29:15 -0500811 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
macauley0f91a3e2015-07-17 18:09:59 +0800812 match = ofp.match()
813 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500814 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
macauley7f89d962015-08-06 18:13:48 +0800815
816 actions=[]
Alex Yashchuk9f449462017-12-09 18:27:19 +0200817 if config["switch_type"] != 'xpliant' and vrf != 0:
818 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
Pierbbdf3782016-08-22 17:58:26 -0700819
Flavio Castro6d498522015-12-15 14:05:04 -0500820 #actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id)))
macauley7f89d962015-08-06 18:13:48 +0800821
macauley0f91a3e2015-07-17 18:09:59 +0800822 request = ofp.message.flow_add(
823 table_id=10,
824 cookie=42,
825 match=match,
826 instructions=[
macauley53d90fe2015-08-04 17:34:22 +0800827 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800828 actions=actions
macauley53d90fe2015-08-04 17:34:22 +0800829 ),
830 ofp.instruction.goto_table(20)
macauley0f91a3e2015-07-17 18:09:59 +0800831 ],
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)
Pierbbdf3782016-08-22 17:58:26 -0700835
macauley0f91a3e2015-07-17 18:09:59 +0800836 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700837
macauley0f91a3e2015-07-17 18:09:59 +0800838 match = ofp.match()
839 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500840 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
Pierbbdf3782016-08-22 17:58:26 -0700841
macauley7f89d962015-08-06 18:13:48 +0800842 actions=[]
843 if vrf!=0:
844 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
Pierbbdf3782016-08-22 17:58:26 -0700845
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700846 # actions.append(ofp.action.push_vlan(0x8100))
Flavio Castro91d1a552016-05-17 16:59:44 -0700847 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Pierbbdf3782016-08-22 17:58:26 -0700848
macauley0f91a3e2015-07-17 18:09:59 +0800849 request = ofp.message.flow_add(
850 table_id=10,
851 cookie=42,
852 match=match,
853 instructions=[
854 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800855 actions=actions
macauley0f91a3e2015-07-17 18:09:59 +0800856 ),
857 ofp.instruction.goto_table(20)
858 ],
859 priority=0)
860 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
861 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700862
Flavio Castro932014b2016-01-05 18:29:15 -0500863 if (flag == 4) :
864 match = ofp.match()
865 match.oxm_list.append(ofp.oxm.in_port(of_port))
866 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000,0x1fff))
867
868 actions=[]
869 if vrf!=0:
870 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
871
Flavio Castro91d1a552016-05-17 16:59:44 -0700872 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Flavio Castro932014b2016-01-05 18:29:15 -0500873
874 request = ofp.message.flow_add(
875 table_id=10,
876 cookie=42,
877 match=match,
878 instructions=[
879 ofp.instruction.apply_actions(
880 actions=actions
881 ),
882 ofp.instruction.goto_table(20)
883 ],
884 priority=0)
885 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
886 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800887
Piere1308762016-09-12 15:29:56 -0700888 if (flag == VLAN_TABLE_FLAG_ONLY_STACKED):
889 # This flag is meant to managed stacked vlan packtes
890 # Matches on outer VLAN_ID, set OVID with outer VLAN.
891 # Finally expose inner VLAN_ID with a pop action and
892 # goto VLAN_1_FLOW_TABLE
893 match = ofp.match()
894 match.oxm_list.append(ofp.oxm.in_port(of_port))
895 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
896
897 actions=[]
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700898 # 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 -0700899 actions.append(ofp.action.pop_vlan())
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700900 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 -0700901
902 request = ofp.message.flow_add(
903 table_id=10,
904 cookie=42,
905 match=match,
906 instructions=[
907 ofp.instruction.apply_actions(
908 actions=actions
909 ),
910 ofp.instruction.goto_table(VLAN_1_FLOW_TABLE)
911 ],
912 priority=0)
913 logging.info("Add vlan %d tagged packets on port %d and go to table %d" %( vlan_id, of_port, VLAN_1_FLOW_TABLE))
914 ctrl.message_send(request)
915
916 if (flag == VLAN_TABLE_FLAG_PRIORITY) :
917 match = ofp.match()
918 match.oxm_list.append(ofp.oxm.in_port(of_port))
919 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1fff))
920 request = ofp.message.flow_add(
921 table_id=10,
922 cookie=42,
923 match=match,
924 instructions=[
925 ofp.instruction.apply_actions(
926 actions=[
927 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)),
928 ofp.action.push_vlan(0x8100),
929 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+out_vlan_id)),
930 ]
931 ),
932 ofp.instruction.goto_table(20)
933 ],
934 priority=0)
935 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
936 ctrl.message_send(request)
937
938 if send_barrier:
939 do_barrier(ctrl)
940
941 return request
942
Piercf76e802016-09-19 20:16:35 -0700943def 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):
944 # table 10: vlan
945 # goto to table 13
946 if flag == VLAN_TABLE_FLAG_ONLY_TAG:
947 match = ofp.match()
948 match.oxm_list.append(ofp.oxm.in_port(of_port))
949 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id, 0x1fff))
950
951 actions=[]
952 if vlan_id == -1:
953 actions.append(ofp.action.pop_vlan())
954 if new_vlan_id > 1:
955 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_vlan_id)))
956 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
957 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
958 # 0x0000nnnn is for UNI interfaces
959 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
960
961 request = ofp.message.flow_add(
962 table_id=10,
963 cookie=42,
964 match=match,
965 instructions=[
966 ofp.instruction.apply_actions(
967 actions=actions
968 ),
969 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
970 ],
971 priority=0)
972 logging.info("Add vlan %d tagged packets on port %d and go to table %d" % (vlan_id, of_port, MPLS_L2_PORT_FLOW_TABLE))
973 ctrl.message_send(request)
974
975 if flag == VLAN_TABLE_FLAG_ONLY_UNTAG:
976 match = ofp.match()
977 match.oxm_list.append(ofp.oxm.in_port(of_port))
978 match.oxm_list.append(ofp.oxm.vlan_vid(0))
979
980 actions=[]
981 if vlan_id > 1:
Charles Chanc85f1562018-01-18 15:44:29 -0800982 # actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
983 # actions.append(ofp.action.set_field(ofp.action.push_vlan(0x8100)))
Piercf76e802016-09-19 20:16:35 -0700984 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Charles Chanc85f1562018-01-18 15:44:29 -0800985
Piercf76e802016-09-19 20:16:35 -0700986 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
987 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
988 # 0x0000nnnn is for UNI interfaces
989 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
990
991 request = ofp.message.flow_add(
992 table_id=10,
993 cookie=42,
994 match=match,
995 instructions=[
996 ofp.instruction.apply_actions(
997 actions=actions
998 ),
999 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
1000 ],
1001 priority=0)
1002 logging.info("Add vlan %d untagged packets on port %d and go to table %d" % (vlan_id, of_port, MPLS_L2_PORT_FLOW_TABLE))
1003 ctrl.message_send(request)
1004
1005 if send_barrier:
1006 do_barrier(ctrl)
1007
1008 return request
1009
Piere1308762016-09-12 15:29:56 -07001010def 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 -07001011
Piere1308762016-09-12 15:29:56 -07001012 # table 11: vlan 1 table
1013 # goto to table 20
1014 if flag == VLAN_TABLE_FLAG_ONLY_TAG:
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.push_vlan(0x8100))
1022 if new_outer_vlan_id != -1:
1023 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_outer_vlan_id)))
1024 else:
1025 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan_id)))
1026
1027 request = ofp.message.flow_add(
1028 table_id=11,
1029 cookie=42,
1030 match=match,
1031 instructions=[
1032 ofp.instruction.apply_actions(
1033 actions=actions
1034 ),
1035 ofp.instruction.goto_table(TERMINATION_FLOW_TABLE)
1036 ],
1037 priority=0)
1038 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))
1039 ctrl.message_send(request)
1040
1041 if flag == VLAN_TABLE_FLAG_ONLY_UNTAG:
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -07001042
Piere1308762016-09-12 15:29:56 -07001043 match = ofp.match()
1044 match.oxm_list.append(ofp.oxm.in_port(of_port))
1045 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
1046 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
1047
1048 actions=[]
1049 request = ofp.message.flow_add(
1050 table_id=11,
1051 cookie=42,
1052 match=match,
1053 instructions=[
1054 ofp.instruction.apply_actions(
1055 actions=actions
1056 ),
1057 ofp.instruction.goto_table(TERMINATION_FLOW_TABLE)
1058 ],
1059 priority=0)
1060 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))
1061 ctrl.message_send(request)
1062
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -07001063 if flag == VLAN_TABLE_FLAG_ONLY_POP_VLAN:
1064
1065 print("INSTALLIN IN TABLE 11!")
1066
1067 match = ofp.match()
1068 match.oxm_list.append(ofp.oxm.in_port(of_port))
1069 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
1070 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
1071
1072 actions=[]
1073 actions.append(ofp.action.pop_vlan())
1074
1075 request = ofp.message.flow_add(
1076 table_id=11,
1077 cookie=42,
1078 match=match,
1079 instructions=[
1080 ofp.instruction.apply_actions(
1081 actions=actions
1082 ),
1083 ofp.instruction.goto_table(TERMINATION_FLOW_TABLE)
1084 ],
1085 priority=0)
1086 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))
1087 ctrl.message_send(request)
1088
1089
macauley0f91a3e2015-07-17 18:09:59 +08001090 if send_barrier:
1091 do_barrier(ctrl)
1092
1093 return request
Pierbbdf3782016-08-22 17:58:26 -07001094
Saurav Das5d1473d2018-07-12 11:02:56 -07001095def 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 -08001096
Piercf76e802016-09-19 20:16:35 -07001097 # table 11: vlan 1 table
1098 # goto to table 13
1099 match = ofp.match()
1100 match.oxm_list.append(ofp.oxm.in_port(of_port))
1101 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
1102 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
1103
1104 actions=[]
Saurav Das5d1473d2018-07-12 11:02:56 -07001105
1106 if cross_connect:
1107 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+inner_vlan_id)))
Piercf76e802016-09-19 20:16:35 -07001108 actions.append(ofp.action.push_vlan(0x8100))
1109 if new_outer_vlan_id != -1:
Saurav Das5d1473d2018-07-12 11:02:56 -07001110 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_outer_vlan_id)))
Piercf76e802016-09-19 20:16:35 -07001111 else:
Saurav Das5d1473d2018-07-12 11:02:56 -07001112 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan_id)))
Piercf76e802016-09-19 20:16:35 -07001113
Saurav Das5d1473d2018-07-12 11:02:56 -07001114 if not cross_connect:
1115 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
1116 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
1117 else:
1118 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE_CROSS_CONNECT)))
1119
Piercf76e802016-09-19 20:16:35 -07001120 # 0x0000nnnn is for UNI interfaces
1121 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
1122
1123 request = ofp.message.flow_add(
Saurav Das5d1473d2018-07-12 11:02:56 -07001124 table_id=11,
1125 cookie=42,
1126 match=match,
1127 instructions=[
1128 ofp.instruction.apply_actions(
1129 actions=actions
1130 ),
1131 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
1132 ],
1133 priority=0)
Piercf76e802016-09-19 20:16:35 -07001134 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))
1135 ctrl.message_send(request)
Piercf76e802016-09-19 20:16:35 -07001136 if send_barrier:
1137 do_barrier(ctrl)
1138
1139 return request
1140
macauley97557232015-07-16 17:28:07 +08001141def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False):
1142 match = ofp.match()
castroflavio21894482015-12-08 15:29:55 -05001143 priority=500
macauleyfddc4662015-07-27 17:40:30 +08001144 if dst_mac!=None:
castroflavio21894482015-12-08 15:29:55 -05001145 priority=1000
macauleyfddc4662015-07-27 17:40:30 +08001146 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
1147
macauley97557232015-07-16 17:28:07 +08001148 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauleyfddc4662015-07-27 17:40:30 +08001149
macauley97557232015-07-16 17:28:07 +08001150 request = ofp.message.flow_add(
1151 table_id=50,
1152 cookie=42,
1153 match=match,
1154 instructions=[
1155 ofp.instruction.write_actions(
1156 actions=[
1157 ofp.action.group(group_id)]),
1158 ofp.instruction.goto_table(60)
1159 ],
1160 buffer_id=ofp.OFP_NO_BUFFER,
castroflavio21894482015-12-08 15:29:55 -05001161 priority=priority)
macauley97557232015-07-16 17:28:07 +08001162
1163 logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac)
1164 ctrl.message_send(request)
1165
1166 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001167 do_barrier(ctrl)
macauley15909e72015-07-17 15:58:57 +08001168
Pierbbdf3782016-08-22 17:58:26 -07001169 return request
macauleyfddc4662015-07-27 17:40:30 +08001170
1171def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False):
1172 match = ofp.match()
1173 if dst_mac!=None:
1174 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
1175
1176 match.oxm_list.append(ofp.oxm.tunnel_id(vnid))
1177 if is_group == True:
1178 actions=[ofp.action.group(group_id)]
1179 else:
1180 actions=[ofp.action.output(group_id)]
1181
1182 request = ofp.message.flow_add(
1183 table_id=50,
1184 cookie=42,
1185 match=match,
1186 instructions=[
1187 ofp.instruction.write_actions(
1188 actions=actions),
1189 ofp.instruction.goto_table(60)
1190 ],
1191 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001192 priority=1000)
macauleyfddc4662015-07-27 17:40:30 +08001193
1194 logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac)
1195 ctrl.message_send(request)
1196
1197 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001198 do_barrier(ctrl)
macauleyfddc4662015-07-27 17:40:30 +08001199
Pierbbdf3782016-08-22 17:58:26 -07001200 return request
1201
macauley_cheng6b133662015-11-09 13:52:39 +08001202def add_termination_flow(ctrl, in_port, eth_type, dst_mac, vlanid, goto_table=None, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +08001203 match = ofp.match()
macauley0f91a3e2015-07-17 18:09:59 +08001204 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauleyfddc4662015-07-27 17:40:30 +08001205 if dst_mac[0]&0x01 == 0x01:
1206 match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00]))
1207 goto_table=40
1208 else:
macauley53d90fe2015-08-04 17:34:22 +08001209 if in_port!=0:
1210 match.oxm_list.append(ofp.oxm.in_port(in_port))
macauleyfddc4662015-07-27 17:40:30 +08001211 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
1212 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauley_cheng6b133662015-11-09 13:52:39 +08001213 if goto_table == None:
1214 goto_table=30
macauley0f91a3e2015-07-17 18:09:59 +08001215
1216 request = ofp.message.flow_add(
1217 table_id=20,
1218 cookie=42,
1219 match=match,
1220 instructions=[
1221 ofp.instruction.goto_table(goto_table)
1222 ],
1223 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001224 priority=1)
macauley0f91a3e2015-07-17 18:09:59 +08001225
1226 logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac)
1227 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +08001228 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001229 do_barrier(ctrl)
macauley0f91a3e2015-07-17 18:09:59 +08001230
Pierbbdf3782016-08-22 17:58:26 -07001231 return request
1232
Alex Yashchuk9f449462017-12-09 18:27:19 +02001233def 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 +08001234 match = ofp.match()
1235 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
Alex Yashchuk9f449462017-12-09 18:27:19 +02001236 if config["switch_type"] != 'xpliant' and vrf != 0:
macauley53d90fe2015-08-04 17:34:22 +08001237 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
macauley0f91a3e2015-07-17 18:09:59 +08001238
Flavio Castroaf2b4502016-02-02 17:41:32 -05001239 match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask))
1240
1241 instructions = []
1242 instructions.append(ofp.instruction.goto_table(60))
1243 if send_ctrl:
Pier265ad5f2017-02-28 17:46:28 +01001244 instructions.append(ofp.instruction.write_actions(
Flavio Castroaf2b4502016-02-02 17:41:32 -05001245 actions=[ofp.action.output( port=ofp.OFPP_CONTROLLER,
1246 max_len=ofp.OFPCML_NO_BUFFER)]))
Pierbbdf3782016-08-22 17:58:26 -07001247 else:
Flavio Castroaf2b4502016-02-02 17:41:32 -05001248 instructions.append(ofp.instruction.write_actions(
1249 actions=[ofp.action.group(action_group_id)]))
macauley53d90fe2015-08-04 17:34:22 +08001250
macauley0f91a3e2015-07-17 18:09:59 +08001251 request = ofp.message.flow_add(
1252 table_id=30,
1253 cookie=42,
1254 match=match,
Flavio Castroaf2b4502016-02-02 17:41:32 -05001255 instructions=instructions,
macauley0f91a3e2015-07-17 18:09:59 +08001256 buffer_id=ofp.OFP_NO_BUFFER,
Alex Yashchuk9f449462017-12-09 18:27:19 +02001257 priority=priority)
macauley0f91a3e2015-07-17 18:09:59 +08001258
1259 logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip)
1260 ctrl.message_send(request)
1261
1262 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001263 do_barrier(ctrl)
macauley0f91a3e2015-07-17 18:09:59 +08001264
Flavio Castro9debaaa2016-07-26 19:37:50 -07001265 return request
Flavio Castrod8f8af22015-12-02 18:19:26 -05001266
Andrea Campanellabfb42b32018-04-26 10:57:23 +02001267def add_unicast_blackhole_flow(ctrl, eth_type, dst_ip, mask, vrf=0, send_ctrl=False, send_barrier=False, priority = 1):
1268 match = ofp.match()
1269 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1270 if config["switch_type"] != 'xpliant' and vrf != 0:
1271 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
1272
1273 match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask))
1274
1275 instructions = []
1276 instructions.append(ofp.instruction.goto_table(60))
1277 instructions.append(ofp.instruction.clear_actions( ))
1278
1279 request = ofp.message.flow_add(
1280 table_id=30,
1281 cookie=42,
1282 match=match,
1283 instructions=instructions,
1284 buffer_id=ofp.OFP_NO_BUFFER,
1285 priority=priority)
1286
1287 logging.info("Inserting unicast blackhole routing flow eth_type %lx, dip %ld",eth_type, dst_ip)
1288 ctrl.message_send(request)
1289
1290 if send_barrier:
1291 do_barrier(ctrl)
1292
1293 return request
1294
Pier1e4e98e2016-10-26 14:36:05 -07001295def add_unicast_v6_routing_flow(ctrl, eth_type, dst_ip, mask, action_group_id, vrf=0, send_ctrl=False, send_barrier=False):
1296 match = ofp.match()
1297 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1298 if vrf != 0:
1299 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
1300
1301 match.oxm_list.append(ofp.oxm.ipv6_dst_masked(parse_ipv6(dst_ip), parse_ipv6(mask)))
1302
1303 instructions = []
1304 instructions.append(ofp.instruction.goto_table(60))
1305 if send_ctrl:
Pier265ad5f2017-02-28 17:46:28 +01001306 instructions.append(ofp.instruction.write_actions(
Pier1e4e98e2016-10-26 14:36:05 -07001307 actions=[ofp.action.output( port=ofp.OFPP_CONTROLLER,
1308 max_len=ofp.OFPCML_NO_BUFFER)]))
1309 else:
1310 instructions.append(ofp.instruction.write_actions(
1311 actions=[ofp.action.group(action_group_id)]))
1312
1313 request = ofp.message.flow_add(
1314 table_id=30,
1315 cookie=42,
1316 match=match,
1317 instructions=instructions,
1318 buffer_id=ofp.OFP_NO_BUFFER,
1319 priority=1)
1320
1321 logging.info("Inserting unicast routing flow eth_type %lx, dip %s",eth_type, dst_ip)
1322 ctrl.message_send(request)
1323
1324 if send_barrier:
1325 do_barrier(ctrl)
1326
1327 return request
1328
Andreas Pantelopoulos6c76b942018-01-22 10:03:02 -08001329def 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 -04001330 match = ofp.match()
1331 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1332 match.oxm_list.append(ofp.oxm.mpls_label(label))
1333 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
Pier265ad5f2017-02-28 17:46:28 +01001334 write_actions = []
1335 write_actions.append(ofp.action.group(action_group_id))
1336 apply_actions = []
1337 apply_actions = [ofp.action.dec_mpls_ttl(),
Flavio Castro8ca52542016-04-11 11:24:49 -04001338 ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))]
Pier265ad5f2017-02-28 17:46:28 +01001339 if (goto_table != 29):
1340 apply_actions.append(ofp.action.set_field(
Flavio Castrod80fbc32016-07-25 15:54:26 -07001341 ofp.oxm.exp2ByteValue(exp_type=23, value=32)))
Pier265ad5f2017-02-28 17:46:28 +01001342 apply_actions.append(ofp.action.copy_ttl_in())
Flavio Castro9debaaa2016-07-26 19:37:50 -07001343
Flavio Castrob702a2f2016-04-10 22:01:48 -04001344 request = ofp.message.flow_add(
1345 table_id=24,
1346 cookie=43,
1347 match=match,
1348 instructions=[
Pier265ad5f2017-02-28 17:46:28 +01001349 ofp.instruction.apply_actions(actions=apply_actions),
1350 ofp.instruction.write_actions(actions=write_actions),
Flavio Castro8ca52542016-04-11 11:24:49 -04001351 ofp.instruction.goto_table(goto_table)
Flavio Castrob702a2f2016-04-10 22:01:48 -04001352 ],
1353 buffer_id=ofp.OFP_NO_BUFFER,
1354 priority=1)
Flavio Castrob702a2f2016-04-10 22:01:48 -04001355 ctrl.message_send(request)
1356
1357 if send_barrier:
1358 do_barrier(ctrl)
1359
1360 return request
1361
Alex Yashchuk9f449462017-12-09 18:27:19 +02001362def xpliant_add_mpls_flow(ctrl, action_group_id=0x0, label=100 ,ethertype=0x0800, bos=1, vrf=1, goto_table=27, send_barrier=False):
1363 match = ofp.match()
1364 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1365 match.oxm_list.append(ofp.oxm.mpls_label(label))
1366
1367 apply_actions = []
1368 apply_actions.append(ofp.action.group(action_group_id))
1369 apply_actions.append(ofp.action.dec_mpls_ttl())
1370 if (goto_table != 29):
1371 apply_actions.append(ofp.action.pop_mpls(ethertype))
1372
1373 request = ofp.message.flow_add(
1374 table_id=24,
1375 cookie=43,
1376 match=match,
1377 instructions=[
1378 ofp.instruction.apply_actions(actions=apply_actions),
1379 ],
1380 buffer_id=ofp.OFP_NO_BUFFER,
1381 priority=1)
1382 logging.info("Inserting MPLS flow , label %ld", label)
1383 ctrl.message_send(request)
1384
1385 if send_barrier:
1386 do_barrier(ctrl)
1387
1388 return request
1389
Andreas Pantelopoulos6c76b942018-01-22 10:03:02 -08001390def add_mpls_flow_swap(ctrl, action_group_id, label, ethertype, bos, goto_table=MPLS_TYPE_FLOW_TABLE, of_port=0, send_barrier=False):
1391 match = ofp.match()
1392 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1393 match.oxm_list.append(ofp.oxm.mpls_label(label))
1394 match.oxm_list.append(ofp.oxm.mpls_bos(1))
1395
1396 apply_actions = []
1397 write_actions = []
1398 apply_actions.append(ofp.action.dec_mpls_ttl())
1399 write_actions.append(ofp.action.group(action_group_id))
1400
1401 request = ofp.message.flow_add(
1402 table_id=24,
1403 cookie=43,
1404 match=match,
1405 instructions=[
1406 ofp.instruction.apply_actions(actions=apply_actions),
1407 ofp.instruction.write_actions(actions=write_actions),
1408 ofp.instruction.goto_table(goto_table)
1409 ],
1410 buffer_id=ofp.OFP_NO_BUFFER,
1411 priority=1)
1412
1413 logging.info("Inserting MPLS flow , label %ld", label)
1414 ctrl.message_send(request)
1415
1416 if send_barrier:
1417 do_barrier(ctrl)
1418
1419 return request
1420
1421
Pierf6f28162016-09-22 16:30:52 -07001422def 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 -07001423 match = ofp.match()
1424 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1425 match.oxm_list.append(ofp.oxm.mpls_label(label))
1426 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
1427
Pier265ad5f2017-02-28 17:46:28 +01001428 apply_actions = []
1429 write_actions = []
1430 apply_actions.append(ofp.action.dec_mpls_ttl())
Pierf6f28162016-09-22 16:30:52 -07001431 if popMPLS == True:
Pier265ad5f2017-02-28 17:46:28 +01001432 apply_actions.append(ofp.action.copy_ttl_in())
1433 apply_actions.append(ofp.action.pop_mpls(ethertype))
Pierf6f28162016-09-22 16:30:52 -07001434 if bos==1 and popL2 == True:
Pier265ad5f2017-02-28 17:46:28 +01001435 apply_actions.append(ofp.action.ofdpa_pop_l2_header())
1436 apply_actions.append(ofp.action.ofdpa_pop_cw())
1437 apply_actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
Pierf6f28162016-09-22 16:30:52 -07001438 # 0x0002nnnn is for UNI interfaces
Pier265ad5f2017-02-28 17:46:28 +01001439 apply_actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00020000 + of_port)))
1440 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 -07001441
Pier265ad5f2017-02-28 17:46:28 +01001442 write_actions.append(ofp.action.group(action_group_id))
Pierb5da4c92016-09-21 11:23:35 -07001443
1444 request = ofp.message.flow_add(
Pier265ad5f2017-02-28 17:46:28 +01001445 table_id=24,
1446 cookie=43,
1447 match=match,
1448 instructions=[
1449 ofp.instruction.apply_actions(actions=apply_actions),
1450 ofp.instruction.write_actions(actions=write_actions),
1451 ofp.instruction.goto_table(goto_table)
1452 ],
1453 buffer_id=ofp.OFP_NO_BUFFER,
1454 priority=1)
Pierb5da4c92016-09-21 11:23:35 -07001455 logging.info("Inserting MPLS flow , label %ld", label)
1456 ctrl.message_send(request)
1457
1458 if send_barrier:
1459 do_barrier(ctrl)
1460
1461 return request
1462
macauleyfddc4662015-07-27 17:40:30 +08001463def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False):
1464 match = ofp.match()
1465 match.oxm_list.append(ofp.oxm.eth_type(0x0800))
Alex Yashchuk9f449462017-12-09 18:27:19 +02001466 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000 + vlan_id))
macauleyfddc4662015-07-27 17:40:30 +08001467 if src_ip_mask!=0:
1468 match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask))
1469 else:
1470 match.oxm_list.append(ofp.oxm.ipv4_src(src_ip))
Pierbbdf3782016-08-22 17:58:26 -07001471
macauleyfddc4662015-07-27 17:40:30 +08001472 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
Pierbbdf3782016-08-22 17:58:26 -07001473
macauleyfddc4662015-07-27 17:40:30 +08001474 request = ofp.message.flow_add(
1475 table_id=40,
1476 cookie=42,
1477 match=match,
1478 instructions=[
1479 ofp.instruction.write_actions(
1480 actions=[ofp.action.group(action_group_id)]),
1481 ofp.instruction.goto_table(60)
1482 ],
1483 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001484 priority=1)
macauleyfddc4662015-07-27 17:40:30 +08001485
1486 logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask)
1487 ctrl.message_send(request)
1488
1489 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001490 do_barrier(ctrl)
macauleyfddc4662015-07-27 17:40:30 +08001491
Pierbbdf3782016-08-22 17:58:26 -07001492 return request
macauley_cheng6b133662015-11-09 13:52:39 +08001493
Pier1e4e98e2016-10-26 14:36:05 -07001494def add_acl_rule(ctrl, eth_type=None, ip_proto=None, send_barrier=False):
1495 match = ofp.match()
1496 if eth_type != None:
1497 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1498 if ip_proto != None:
1499 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1500
1501 request = ofp.message.flow_add(
1502 table_id=60,
1503 cookie=42,
1504 match=match,
1505 instructions=[
1506 ofp.instruction.apply_actions(
1507 actions=[ofp.action.output(port=ofp.OFPP_CONTROLLER, max_len=ofp.OFPCML_NO_BUFFER)]
1508 ),
1509 ],
1510 buffer_id=ofp.OFP_NO_BUFFER,
1511 priority=1
1512 )
1513
1514 logging.info("Inserting ACL flow eth_type %lx, ip_proto %ld", eth_type, ip_proto)
1515 ctrl.message_send(request)
1516
1517 if send_barrier:
1518 do_barrier(ctrl)
1519
1520 return request
1521
macauley_cheng6b133662015-11-09 13:52:39 +08001522#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 +08001523def 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 +08001524 match = ofp.match()
1525 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1526 match.oxm_list.append(ofp.oxm.ipv4_dst(ip_dst))
1527 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1528 match.oxm_list.append(ofp.oxm.tcp_dst(tcp_dst))
Pierbbdf3782016-08-22 17:58:26 -07001529
macauley_cheng6b133662015-11-09 13:52:39 +08001530 request = ofp.message.flow_add(
1531 table_id=28,
1532 cookie=42,
1533 match=match,
1534 instructions=[
1535 ofp.instruction.write_actions(
1536 actions=[ofp.action.set_field(ofp.oxm.ipv4_dst(set_ip_dst)),
1537 ofp.action.set_field(ofp.oxm.tcp_dst(set_tcp_dst)),
1538 ofp.action.group(action_group_id)]),
1539 ofp.instruction.goto_table(60)
1540 ],
1541 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001542 priority=1)
macauley_cheng6b133662015-11-09 13:52:39 +08001543 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)
1544 ctrl.message_send(request)
1545 return request
macauley_chengeffc20a2015-11-09 16:14:56 +08001546
1547#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
1548def add_snat_flow(ctrl, eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src):
1549 match = ofp.match()
1550 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1551 match.oxm_list.append(ofp.oxm.ipv4_src(ip_src))
1552 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1553 match.oxm_list.append(ofp.oxm.tcp_src(tcp_src))
Pierbbdf3782016-08-22 17:58:26 -07001554
macauley_chengeffc20a2015-11-09 16:14:56 +08001555 request = ofp.message.flow_add(
1556 table_id=29,
1557 cookie=42,
1558 match=match,
1559 instructions=[
1560 ofp.instruction.write_actions(
1561 actions=[ofp.action.set_field(ofp.oxm.ipv4_src(set_ip_src)),
1562 ofp.action.set_field(ofp.oxm.tcp_src(set_tcp_src))]),
1563 ofp.instruction.goto_table(30)
1564 ],
1565 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001566 priority=1)
macauley_chengeffc20a2015-11-09 16:14:56 +08001567 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)
1568 ctrl.message_send(request)
1569 return request
Pierbbdf3782016-08-22 17:58:26 -07001570
1571def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001572 """
1573 Command Example:
1574 of-agent vtap 10001 ethernet 1/1 vid 1
1575 of-agent vtp 10001 vni 10
1576 """
1577 if vlan != 0:
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:vid>VLAN_ID</ofdpa10:vid>
1613 <ofdpa10:vni>VNID</ofdpa10:vni>
1614 </ofdpa10:vtap>
Pierbbdf3782016-08-22 17:58:26 -07001615 </port>
macauleyfddc4662015-07-27 17:40:30 +08001616 </resources>
1617 <logical-switches>
1618 <switch>
1619 <id>DATAPATH_ID</id>
1620 <datapath-id>DATAPATH_ID</datapath-id>
1621 <resources>
1622 <port xc:operation="OPERATION">LPORT</port>
1623 </resources>
1624 </switch>
1625 </logical-switches>
1626 </capable-switch>
1627 </config>
Pierbbdf3782016-08-22 17:58:26 -07001628 """
macauleyfddc4662015-07-27 17:40:30 +08001629 else:
1630 config_vtap_xml="""
1631 <config>
1632 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1633 <id>capable-switch-1</id>
1634 <resources>
1635 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001636 <resource-id >LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001637 <features>
1638 <current>
1639 <rate>10Gb</rate>
1640 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001641 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001642 </current>
1643 <advertised>
1644 <rate>10Gb</rate>
1645 <rate>100Gb</rate>
1646 <medium>fiber</medium>
1647 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001648 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001649 <supported>
1650 <rate>10Gb</rate>
1651 <rate>100Gb</rate>
1652 <medium>fiber</medium>
1653 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001654 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001655 <advertised-peer>
1656 <rate>10Gb</rate>
1657 <rate>100Gb</rate>
1658 <medium>fiber</medium>
1659 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001660 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001661 </features>
1662 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1663 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1664 <ofdpa10:vni>VNID</ofdpa10:vni>
1665 </ofdpa10:vtap>
Pierbbdf3782016-08-22 17:58:26 -07001666 </port>
macauleyfddc4662015-07-27 17:40:30 +08001667 </resources>
1668 <logical-switches>
1669 <switch>
1670 <id>DATAPATH_ID</id>
1671 <datapath-id>DATAPATH_ID</datapath-id>
1672 <resources>
1673 <port xc:operation="OPERATION">LPORT</port>
1674 </resources>
1675 </switch>
1676 </logical-switches>
1677 </capable-switch>
1678 </config>
Pierbbdf3782016-08-22 17:58:26 -07001679 """
1680 str_datapath_id_f= "{:016x}".format(dp_id)
1681 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1682 config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id)
1683 config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport)))
1684 config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port))
1685 config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan))
macauleyfddc4662015-07-27 17:40:30 +08001686 config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid))
1687 config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation))
1688 return config_vtap_xml
Pierbbdf3782016-08-22 17:58:26 -07001689
1690def 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 +08001691 """
1692 Command Example:
1693 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 -07001694 of-agent vtp 10001 vni 10
macauleyfddc4662015-07-27 17:40:30 +08001695 """
1696
1697 config_vtep_xml="""
1698 <config>
1699 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1700 <id>capable-switch-1</id>
1701 <resources>
1702 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001703 <resource-id>LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001704 <features>
1705 <current>
1706 <rate>10Gb</rate>
1707 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001708 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001709 </current>
1710 <advertised>
1711 <rate>10Gb</rate>
1712 <rate>100Gb</rate>
1713 <medium>fiber</medium>
1714 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001715 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001716 <supported>
1717 <rate>10Gb</rate>
1718 <rate>100Gb</rate>
1719 <medium>fiber</medium>
1720 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001721 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001722 <advertised-peer>
1723 <rate>10Gb</rate>
1724 <rate>100Gb</rate>
1725 <medium>fiber</medium>
1726 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001727 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001728 </features>
Pier265ad5f2017-02-28 17:46:28 +01001729 <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01">
1730 <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip>
1731 <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip>
1732 <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port>
1733 <ofdpa10:vni xc:operation="OPERATION">
macauley25999cf2015-08-07 17:03:24 +08001734 <ofdpa10:id>VNID</ofdpa10:id>
1735 </ofdpa10:vni>
Pier265ad5f2017-02-28 17:46:28 +01001736 <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id>
1737 <ofdpa10:ttl>TTL</ofdpa10:ttl>
1738 </ofdpa10:vtep>
Pierbbdf3782016-08-22 17:58:26 -07001739 </port>
macauleyfddc4662015-07-27 17:40:30 +08001740 </resources>
1741 <logical-switches>
1742 <switch>
1743 <id>DATAPATH_ID</id>
1744 <datapath-id>DATAPATH_ID</datapath-id>
1745 <resources>
1746 <port xc:operation="OPERATION">LPORT</port>
1747 </resources>
1748 </switch>
1749 </logical-switches>
1750 </capable-switch>
Pierbbdf3782016-08-22 17:58:26 -07001751 </config>
macauleyfddc4662015-07-27 17:40:30 +08001752 """
Pierbbdf3782016-08-22 17:58:26 -07001753 str_datapath_id_f= "{:016x}".format(dp_id)
1754 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1755 config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +08001756 config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport)))
Pierbbdf3782016-08-22 17:58:26 -07001757 config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip))
1758 config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip))
1759 config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port))
1760 config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1761 config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl))
macauleyfddc4662015-07-27 17:40:30 +08001762 config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid))
Pierbbdf3782016-08-22 17:58:26 -07001763 config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation))
macauleyfddc4662015-07-27 17:40:30 +08001764
Pierbbdf3782016-08-22 17:58:26 -07001765 return config_vtep_xml
1766
1767def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001768 #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2
1769 config_nexthop_xml="""
1770 <config>
1771 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1772 <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1773 <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id>
1774 <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac>
1775 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1776 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1777 </ofdpa10:next-hop>
1778 </of11-config:capable-switch>
1779 </config>
1780 """
1781 config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan))
Pierbbdf3782016-08-22 17:58:26 -07001782 config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port))
1783 config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1784 config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac))
1785 config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation))
1786 return config_nexthop_xml
macauleyfddc4662015-07-27 17:40:30 +08001787
Pierbbdf3782016-08-22 17:58:26 -07001788def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001789 #of-agent vni 10 multicast 224.1.1.1 nexthop 20
Pierbbdf3782016-08-22 17:58:26 -07001790 if mcast_ipv4!=None:
macauleyfddc4662015-07-27 17:40:30 +08001791 config_vni_xml="""
1792 <config>
1793 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1794 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1795 <ofdpa10:id>VNID</ofdpa10:id>
1796 <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group>
1797 <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id>
1798 </ofdpa10:vni>
1799 </of11-config:capable-switch>
1800 </config>
Pierbbdf3782016-08-22 17:58:26 -07001801 """
1802 config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1803 config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4))
macauleyfddc4662015-07-27 17:40:30 +08001804 else:
1805 config_vni_xml="""
1806 <config>
1807 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1808 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1809 <ofdpa10:id>VNID</ofdpa10:id>
1810 </ofdpa10:vni>
1811 </of11-config:capable-switch>
1812 </config>
Pierbbdf3782016-08-22 17:58:26 -07001813 """
1814
1815 config_vni_xml=config_vni_xml.replace("VNID", str(vni_id))
1816 config_vni_xml=config_vni_xml.replace("OPERATION", str(operation))
macauleyfddc4662015-07-27 17:40:30 +08001817 return config_vni_xml
Pierbbdf3782016-08-22 17:58:26 -07001818
1819def get_featureReplay(self):
macauleyfddc4662015-07-27 17:40:30 +08001820 req = ofp.message.features_request()
1821 res, raw = self.controller.transact(req)
Pierbbdf3782016-08-22 17:58:26 -07001822 self.assertIsNotNone(res, "Did not receive a response from the DUT.")
macauleyfddc4662015-07-27 17:40:30 +08001823 self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY,
1824 ("Unexpected packet type %d received in response to "
1825 "OFPT_FEATURES_REQUEST") % res.type)
Pierbbdf3782016-08-22 17:58:26 -07001826 return res
1827
macauleyfddc4662015-07-27 17:40:30 +08001828def send_edit_config(switch_ip, xml, target='runing'):
1829 NETCONF_ACCOUNT="netconfuser"
1830 NETCONF_PASSWD="netconfuser"
1831 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1832 try:
Pierbbdf3782016-08-22 17:58:26 -07001833 m.edit_config(target='running',
1834 config=xml,
1835 default_operation='merge',
macauleyfddc4662015-07-27 17:40:30 +08001836 error_option='stop-on-error')
1837
1838 except Exception as e:
1839 logging.info("Fail to set xml %s", xml)
1840 return False
1841
Pier265ad5f2017-02-28 17:46:28 +01001842 #return m.get_config(source='running').data_xml
macauleyfddc4662015-07-27 17:40:30 +08001843 return True
1844
1845def send_delete_config(switch_ip, xml, target='runing'):
1846 NETCONF_ACCOUNT="netconfuser"
1847 NETCONF_PASSWD="netconfuser"
1848 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1849 try:
Pierbbdf3782016-08-22 17:58:26 -07001850 m.edit_config(target='running',
1851 config=xml,
1852 default_operation='delete',
macauleyfddc4662015-07-27 17:40:30 +08001853 error_option='stop-on-error')
1854
1855 except Exception as e:
1856 logging.info("Fail to set xml %s", xml)
1857 return False
1858
Pier265ad5f2017-02-28 17:46:28 +01001859 #return m.get_config(source='running').data_xml
macauleyfddc4662015-07-27 17:40:30 +08001860 return True
Pierbbdf3782016-08-22 17:58:26 -07001861
macauleyfddc4662015-07-27 17:40:30 +08001862def get_edit_config(switch_ip, target='runing'):
1863 NETCONF_ACCOUNT="netconfuser"
1864 NETCONF_PASSWD="netconfuser"
1865 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 +01001866 return m.get_config(source='running').data_xml
macauleydbff3272015-07-30 14:07:16 +08001867
macauley_cheng67da9262015-08-31 15:18:41 +08001868
1869"""
1870MPLS
1871"""
1872
1873OFDPA_MPLS_SUBTYPE_SHIFT=24
Pierbbdf3782016-08-22 17:58:26 -07001874OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1
macauley_cheng67da9262015-08-31 15:18:41 +08001875OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2
1876OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3
1877OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4
1878OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5
1879OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6
1880OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8
1881OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10
1882
Piercf76e802016-09-19 20:16:35 -07001883
1884
1885
macauley_cheng67da9262015-08-31 15:18:41 +08001886def encode_mpls_interface_group_id(subtype, index):
1887 index=index&0x00ffffff
1888 assert(subtype==0)
1889 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1890
1891def encode_mpls_label_group_id(subtype, index):
1892 index=index&0x00ffffff
1893 assert(subtype <=5 or subtype==0)
1894 #1: l2 vpn label
1895 #2: l3 vpn label
1896 #3: mpls tunnel label 1
1897 #4: mpls tunnel lable 2
1898 #5: mpls swap label
Pierbbdf3782016-08-22 17:58:26 -07001899 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
macauley_cheng67da9262015-08-31 15:18:41 +08001900
1901def encode_mpls_forwarding_group_id(subtype, index):
1902 index=index&0x00ffffff
1903 assert(subtype==6 or subtype==8 or subtype==10)
Pierbbdf3782016-08-22 17:58:26 -07001904 return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
macauley_cheng67da9262015-08-31 15:18:41 +08001905
1906
Andreas Pantelopoulosaaa02622018-04-04 15:13:03 -07001907def 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 +08001908 action=[]
1909 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
1910 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
Pier265ad5f2017-02-28 17:46:28 +01001911 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001912 action.append(ofp.action.group(ref_gid))
Pierbbdf3782016-08-22 17:58:26 -07001913
macauley_cheng67da9262015-08-31 15:18:41 +08001914 buckets = [ofp.bucket(actions=action)]
1915
1916 mpls_group_id =encode_mpls_interface_group_id(subtype, index)
Andreas Pantelopoulosaaa02622018-04-04 15:13:03 -07001917 if add:
1918 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1919 group_id=mpls_group_id,
1920 buckets=buckets
1921 )
1922 else:
1923 request = ofp.message.group_modify(group_type=ofp.OFPGT_INDIRECT,
1924 group_id=mpls_group_id,
1925 buckets=buckets
1926 )
1927
Andreas Pantelopoulos6c76b942018-01-22 10:03:02 -08001928
1929 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 +08001930 ctrl.message_send(request)
Pier1e4e98e2016-10-26 14:36:05 -07001931
1932 if send_barrier:
1933 do_barrier(ctrl)
macauley_cheng67da9262015-08-31 15:18:41 +08001934 return mpls_group_id, request
1935
Piercf76e802016-09-19 20:16:35 -07001936def add_mpls_tunnel_label_group(
1937 ctrl,
1938 ref_gid,
1939 subtype,
1940 index,
1941 label,
1942 ):
1943
1944 action=[]
1945 action.append(ofp.action.push_mpls(0x8847))
1946 action.append(ofp.action.set_field(ofp.oxm.mpls_label(label)))
1947 action.append(ofp.action.group(ref_gid))
1948 buckets = [ofp.bucket(actions=action)]
1949
1950 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1951 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1952 group_id=mpls_group_id,
1953 buckets=buckets
1954 )
1955 ctrl.message_send(request)
1956
1957 return mpls_group_id, request
1958
Pierb5da4c92016-09-21 11:23:35 -07001959def add_mpls_swap_label_group(
1960 ctrl,
1961 ref_gid,
1962 subtype,
1963 index,
1964 label,
1965 ):
1966
1967 action=[]
1968 action.append(ofp.action.set_field(ofp.oxm.mpls_label(label)))
1969 action.append(ofp.action.group(ref_gid))
1970 buckets = [ofp.bucket(actions=action)]
1971
1972 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1973 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1974 group_id=mpls_group_id,
1975 buckets=buckets
1976 )
Andreas Pantelopoulos6c76b942018-01-22 10:03:02 -08001977 logging.debug("Adding MPLS Swap group %02x, label %d", mpls_group_id, label)
Pierb5da4c92016-09-21 11:23:35 -07001978 ctrl.message_send(request)
1979
1980 return mpls_group_id, request
1981
Pierbbdf3782016-08-22 17:58:26 -07001982def add_mpls_label_group(ctrl, subtype, index, ref_gid,
macauley_cheng67da9262015-08-31 15:18:41 +08001983 lmep_id=-1,
1984 qos_index=-1,
1985 push_l2_header=False,
1986 push_vlan=False,
1987 push_mpls_header=False,
1988 push_cw=False,
1989 set_mpls_label=None,
1990 set_bos=None,
1991 set_tc=None,
1992 set_tc_from_table=False,
1993 cpy_tc_outward=False,
1994 set_ttl=None,
1995 cpy_ttl_outward=False,
1996 oam_lm_tx_count=False,
Pier1e4e98e2016-10-26 14:36:05 -07001997 set_pri_from_table=False,
1998 send_barrier=False
macauley_cheng67da9262015-08-31 15:18:41 +08001999 ):
2000 """
2001 @ref_gid: only can be mpls intf group or mpls tunnel label 1/2 group
Pierbbdf3782016-08-22 17:58:26 -07002002 """
macauley_cheng67da9262015-08-31 15:18:41 +08002003 action=[]
2004
2005 if push_vlan== True:
2006 action.append(ofp.action.push_vlan(0x8100))
2007 if push_mpls_header== True:
2008 action.append(ofp.action.push_mpls(0x8847))
2009 if set_mpls_label != None:
2010 action.append(ofp.action.set_field(ofp.oxm.mpls_label(set_mpls_label)))
2011 if set_bos != None:
2012 action.append(ofp.action.set_field(ofp.oxm.mpls_bos(set_bos)))
2013 if set_tc != None:
2014 assert(set_tc_from_table==False)
2015 action.append(ofp.action.set_field(ofp.oxm.mpls_tc(set_tc)))
2016 if set_ttl != None:
Pierbbdf3782016-08-22 17:58:26 -07002017 action.append(ofp.action.set_mpls_ttl(set_ttl))
macauley_cheng67da9262015-08-31 15:18:41 +08002018 if cpy_ttl_outward == True:
Pierbbdf3782016-08-22 17:58:26 -07002019 action.append(ofp.action.copy_ttl_out())
macauley_cheng67da9262015-08-31 15:18:41 +08002020 """
2021 ofdpa experimenter
Pierbbdf3782016-08-22 17:58:26 -07002022 """
macauley_cheng67da9262015-08-31 15:18:41 +08002023 if push_l2_header== True:
Pierbbdf3782016-08-22 17:58:26 -07002024 action.append(ofp.action.ofdpa_push_l2_header())
macauley_cheng67da9262015-08-31 15:18:41 +08002025 if set_tc_from_table== True:
2026 assert(qos_index>=0)
2027 assert(set_tc == None)
Pierbbdf3782016-08-22 17:58:26 -07002028 action.append(ofp.action.ofdpa_set_tc_from_table(qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08002029 if cpy_tc_outward == True:
Pierbbdf3782016-08-22 17:58:26 -07002030 action.append(ofp.action.ofdpa_copy_tc_out())
macauley_cheng67da9262015-08-31 15:18:41 +08002031 if oam_lm_tx_count == True:
Pierbbdf3782016-08-22 17:58:26 -07002032 assert(qos_index>=0 and lmep_id>=0)
2033 action.append(ofp.action.ofdpa_oam_lm_tx_count(lmep_id, qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08002034 if set_pri_from_table == True:
Pierbbdf3782016-08-22 17:58:26 -07002035 assert(qos_index>=0)
2036 action.append(ofp.action.ofdpa_set_qos_from_table(qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08002037 if push_cw == True:
2038 action.append(ofp.action.ofdpa_push_cw())
Pierbbdf3782016-08-22 17:58:26 -07002039
2040 action.append(ofp.action.group(ref_gid))
macauley_cheng67da9262015-08-31 15:18:41 +08002041 buckets = [ofp.bucket(actions=action)]
Pierbbdf3782016-08-22 17:58:26 -07002042
macauley_cheng67da9262015-08-31 15:18:41 +08002043 mpls_group_id = encode_mpls_label_group_id(subtype, index)
2044 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
2045 group_id=mpls_group_id,
2046 buckets=buckets
2047 )
2048 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -07002049
Pier1e4e98e2016-10-26 14:36:05 -07002050 if send_barrier:
2051 do_barrier(ctrl)
2052
Pierbbdf3782016-08-22 17:58:26 -07002053 return mpls_group_id, request
2054
Saurav Das5d1473d2018-07-12 11:02:56 -07002055def 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 -07002056 """
2057 Only action is Group, which must indicate one of:
2058 MPLS L2 VPN Label or Fast Failover Protection Group.
2059 ref_gid contains this information
2060 """
Piercf76e802016-09-19 20:16:35 -07002061
2062 match = ofp.match()
Pier23784aa2016-09-19 20:08:21 -07002063
Pier265ad5f2017-02-28 17:46:28 +01002064 write_actions = []
2065 write_actions.append(ofp.action.group(ref_gid))
2066 apply_actions = []
Piercf76e802016-09-19 20:16:35 -07002067
Saurav Das5d1473d2018-07-12 11:02:56 -07002068 if goto==MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE:
2069 tunnel_id = tunnel_index + ofp.oxm.TUNNEL_ID_BASE
2070 match.oxm_list.append(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port))
2071 match.oxm_list.append(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE))
2072 assert(qos_index>=0)
2073 apply_actions.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_QOS_INDEX, value=qos_index)))
2074
2075 request = ofp.message.flow_add(
Piercf76e802016-09-19 20:16:35 -07002076 table_id=MPLS_L2_PORT_FLOW_TABLE,
2077 cookie=42,
2078 match=match,
2079 instructions=[
Pier265ad5f2017-02-28 17:46:28 +01002080 ofp.instruction.apply_actions(actions=apply_actions),
2081 ofp.instruction.write_actions(actions=write_actions),
Saurav Das5d1473d2018-07-12 11:02:56 -07002082 ofp.instruction.goto_table(goto)
Pier265ad5f2017-02-28 17:46:28 +01002083 ],
Piercf76e802016-09-19 20:16:35 -07002084 buffer_id=ofp.OFP_NO_BUFFER,
2085 priority=1)
Saurav Das5d1473d2018-07-12 11:02:56 -07002086 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)
2087 ctrl.message_send(request)
Piercf76e802016-09-19 20:16:35 -07002088
Saurav Das5d1473d2018-07-12 11:02:56 -07002089 if goto==ACL_FLOW_TABLE:
2090 tunnel_id = tunnel_index + ofp.oxm.TUNNEL_ID_BASE
2091 match.oxm_list.append(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port))
2092 match.oxm_list.append(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE_CROSS_CONNECT))
2093 request = ofp.message.flow_add(
2094 table_id=MPLS_L2_PORT_FLOW_TABLE,
2095 cookie=42,
2096 match=match,
2097 instructions=[
2098 ofp.instruction.apply_actions(actions=apply_actions),
2099 ofp.instruction.write_actions(actions=write_actions),
2100 ofp.instruction.goto_table(goto)
2101 ],
2102 buffer_id=ofp.OFP_NO_BUFFER,
2103 priority=1)
2104 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)
2105 ctrl.message_send(request)
2106
2107 return request
Piercf76e802016-09-19 20:16:35 -07002108
Pierbbdf3782016-08-22 17:58:26 -07002109def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids,
2110 watch_port=None,
Pier265ad5f2017-02-28 17:46:28 +01002111 watch_group=ofp.OFPP_ANY,
2112 push_vlan=None,
macauley_chengd17ce512015-08-31 17:45:51 +08002113 pop_vlan=None,
macauley_cheng67da9262015-08-31 15:18:41 +08002114 set_vid=None):
2115 assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP
Pier265ad5f2017-02-28 17:46:28 +01002116 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP
2117 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG)
macauley_chengd17ce512015-08-31 17:45:51 +08002118
macauley_cheng67da9262015-08-31 15:18:41 +08002119 buckets=[]
2120 if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP:
macauley_chengd17ce512015-08-31 17:45:51 +08002121 group_type = ofp.OFPGT_FF
macauley_cheng67da9262015-08-31 15:18:41 +08002122 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08002123 action=[]
Pierbbdf3782016-08-22 17:58:26 -07002124 action.append(ofp.action.group(gid))
macauley_chengd17ce512015-08-31 17:45:51 +08002125 buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action))
2126
macauley_cheng67da9262015-08-31 15:18:41 +08002127 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP:
2128 group_type = ofp.OFPGT_SELECT
2129 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08002130 action=[]
Pierbbdf3782016-08-22 17:58:26 -07002131 action.append(ofp.action.group(gid))
macauley_cheng67da9262015-08-31 15:18:41 +08002132 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08002133
macauley_cheng67da9262015-08-31 15:18:41 +08002134 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG:
2135 group_type = ofp.OFPGT_INDIRECT
macauley_chengd17ce512015-08-31 17:45:51 +08002136 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08002137 if set_vid!=None:
Flavio Castro91d1a552016-05-17 16:59:44 -07002138 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+set_vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08002139 if push_vlan!=None:
Pierbbdf3782016-08-22 17:58:26 -07002140 action.append(ofp.action.push_vlan(push_vlan))
macauley_cheng67da9262015-08-31 15:18:41 +08002141 if pop_vlan!=None:
Pierbbdf3782016-08-22 17:58:26 -07002142 action.append(ofp.action.pop_vlan())
2143 action.append(ofp.action.group(ref_gids[0]))
macauley_cheng67da9262015-08-31 15:18:41 +08002144 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08002145
2146 mpls_group_id = encode_mpls_forwarding_group_id(subtype, index)
macauley_cheng67da9262015-08-31 15:18:41 +08002147 request = ofp.message.group_add(group_type=group_type,
macauley_cheng67da9262015-08-31 15:18:41 +08002148 group_id=mpls_group_id,
2149 buckets=buckets
2150 )
2151 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -07002152 return mpls_group_id, request
macauley_chengd17ce512015-08-31 17:45:51 +08002153
Jonghwan Hyunff0dfd52018-03-20 15:04:35 -07002154def add_one_egress_vlan_tpid_table_flow(ctrl, of_port):
2155 # Used for changing ethertype of outer vlan header to 0x88a8
2156
2157 match = ofp.match()
2158 match.oxm_list.append(ofp.oxm.exp4ByteValue(ofp.oxm.OFDPA_EXP_TYPE_ACTSET_OUTPUT, of_port, ONF_EXPERIMENTER_ID))
2159 match.oxm_list.append(ofp.oxm.vlan_vid_masked(ofp.OFPVID_PRESENT, ofp.OFPVID_PRESENT))
2160
2161 actions = []
2162 actions.append(ofp.action.copy_field(
2163 12, 0, 0, ['\x80\x00\x0c\x02', ofp.oxm.exp4ByteReg(oxm_field = 1).pack()])) # VLAN_VID, PACKET_REG(1)
2164 actions.append(ofp.action.pop_vlan())
2165 actions.append(ofp.action.push_vlan(0x88a8))
2166 actions.append(ofp.action.copy_field(
2167 12, 0, 0, [ofp.oxm.exp4ByteReg(oxm_field = 1).pack(), '\x80\x00\x0c\x02'])) # PACKET_REG(1), VLAN_VID
2168
2169 request = ofp.message.flow_add(
2170 table_id=EGRESS_TPID_FLOW_TABLE,
2171 cookie=42,
2172 match=match,
2173 instructions=[
2174 ofp.instruction.apply_actions(
2175 actions=actions
2176 ),
2177 ],
2178 priority=0)
2179
2180 ctrl.message_send(request)
2181
2182 return
macauley_chengd17ce512015-08-31 17:45:51 +08002183
macauley_cheng67da9262015-08-31 15:18:41 +08002184"""
Piercf76e802016-09-19 20:16:35 -07002185display
Pierbbdf3782016-08-22 17:58:26 -07002186"""
macauleydbff3272015-07-30 14:07:16 +08002187def print_current_table_flow_stat(ctrl, table_id=0xff):
2188 stat_req=ofp.message.flow_stats_request()
2189 response, pkt = ctrl.transact(stat_req)
2190 if response == None:
2191 print "no response"
2192 return None
2193 print len(response.entries)
2194 for obj in response.entries:
2195 print "match ", obj.match
2196 print "cookie", obj.cookie
2197 print "priority", obj.priority
2198 print "idle_timeout", obj.idle_timeout
2199 print "hard_timeout", obj.hard_timeout
2200 #obj.actions
Flavio Castro167f5bd2015-12-02 19:33:53 -05002201 print "packet count: %lx"%obj.packet_count
Saurav Dasb4b841e2018-08-17 15:51:56 -07002202