blob: 78d183e11dffa2da32ddd5f4af674972051ece1d [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
Saurav Dasb4b841e2018-08-17 15:51:56 -0700312def add_l2_flood_group_with_gids(ctrl, gids, vlanid, id):
313 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)
324 return request
325
Flavio Castroa7162bb2016-07-25 17:30:30 -0700326def mod_l2_flood_group(ctrl, ports, vlanid, id):
327 buckets=[]
328 for of_port in ports:
329 group_id = encode_l2_interface_group_id(vlanid, of_port)
330 action=[ofp.action.group(group_id)]
331 buckets.append(ofp.bucket(actions=action))
332
333 group_id =encode_l2_flood_group_id(vlanid, id)
334 request = ofp.message.group_modify(group_type=ofp.OFPGT_ALL,
335 group_id=group_id,
336 buckets=buckets
337 )
338 ctrl.message_send(request)
339 return request
340
341
macauley15909e72015-07-17 15:58:57 +0800342def add_l2_rewrite_group(ctrl, port, vlanid, id, src_mac, dst_mac):
343 group_id = encode_l2_interface_group_id(vlanid, port)
344
345 action=[]
346 if src_mac is not None:
347 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
348
349 if dst_mac is not None:
350 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
351
Flavio Castro91d1a552016-05-17 16:59:44 -0700352 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
Pierbbdf3782016-08-22 17:58:26 -0700353
macauley15909e72015-07-17 15:58:57 +0800354 action.append(ofp.action.group(group_id))
Pierbbdf3782016-08-22 17:58:26 -0700355
macauley15909e72015-07-17 15:58:57 +0800356 buckets = [ofp.bucket(actions=action)]
357
358 group_id =encode_l2_rewrite_group_id(id)
359 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
360 group_id=group_id,
361 buckets=buckets
362 )
363 ctrl.message_send(request)
364 return request
Pierbbdf3782016-08-22 17:58:26 -0700365
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700366def add_l3_unicast_group(ctrl, port, vlanid, id, src_mac, dst_mac, send_barrier=False, gid=None):
367
368 if (not gid):
369 group_id = encode_l2_interface_group_id(vlanid, port)
370 else:
371 group_id = gid
macauley15909e72015-07-17 15:58:57 +0800372
373 action=[]
374 if src_mac is not None:
375 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
376
377 if dst_mac is not None:
378 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
379
Flavio Castroaf2b4502016-02-02 17:41:32 -0500380 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
Pierbbdf3782016-08-22 17:58:26 -0700381
macauley15909e72015-07-17 15:58:57 +0800382 action.append(ofp.action.group(group_id))
Pierbbdf3782016-08-22 17:58:26 -0700383
macauley15909e72015-07-17 15:58:57 +0800384 buckets = [ofp.bucket(actions=action)]
385
386 group_id =encode_l3_unicast_group_id(id)
387 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
388 group_id=group_id,
389 buckets=buckets
390 )
391 ctrl.message_send(request)
Pier1e4e98e2016-10-26 14:36:05 -0700392
393 if send_barrier:
394 do_barrier(ctrl)
395
macauley15909e72015-07-17 15:58:57 +0800396 return request
Pierbbdf3782016-08-22 17:58:26 -0700397
macauley15909e72015-07-17 15:58:57 +0800398def add_l3_interface_group(ctrl, port, vlanid, id, src_mac):
399 group_id = encode_l2_interface_group_id(vlanid, port)
400
401 action=[]
402 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
Pierbbdf3782016-08-22 17:58:26 -0700403 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
macauley15909e72015-07-17 15:58:57 +0800404 action.append(ofp.action.group(group_id))
Pierbbdf3782016-08-22 17:58:26 -0700405
macauley15909e72015-07-17 15:58:57 +0800406 buckets = [ofp.bucket(actions=action)]
407
408 group_id =encode_l3_interface_group_id(id)
409 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
410 group_id=group_id,
411 buckets=buckets
412 )
413 ctrl.message_send(request)
414 return request
415
Saurav Dasb4b841e2018-08-17 15:51:56 -0700416
417def add_l2_loadbal_group(ctrl, id, l2_unfil_intf_groups, send_barrier=False):
418 buckets=[]
419 for group in l2_unfil_intf_groups:
420 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
421
422 group_id=encode_l2_loadbal_group_id(id)
423 request = ofp.message.group_add(group_type=ofp.OFPGT_SELECT,
424 group_id=group_id,
425 buckets=buckets
426 )
427 ctrl.message_send(request)
428 if send_barrier:
429 do_barrier(ctrl)
430
431 return group_id, request
432
433def mod_l2_loadbal_group(ctrl, id, l2_unfil_intf_groups, send_barrier=False):
434 buckets=[]
435 for group in l2_unfil_intf_groups:
436 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
437
438 group_id =encode_l2_loadbal_group_id(id)
439 request = ofp.message.group_modify(group_type=ofp.OFPGT_SELECT,
440 group_id=group_id,
441 buckets=buckets
442 )
443 ctrl.message_send(request)
444 return request
445
446
Pier1e4e98e2016-10-26 14:36:05 -0700447def add_l3_ecmp_group(ctrl, id, l3_ucast_groups, send_barrier=False):
macauley15909e72015-07-17 15:58:57 +0800448 buckets=[]
449 for group in l3_ucast_groups:
450 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
451
452 group_id =encode_l3_ecmp_group_id(id)
453 request = ofp.message.group_add(group_type=ofp.OFPGT_SELECT,
454 group_id=group_id,
455 buckets=buckets
456 )
457 ctrl.message_send(request)
Pier1e4e98e2016-10-26 14:36:05 -0700458
459 if send_barrier:
460 do_barrier(ctrl)
461
macauley15909e72015-07-17 15:58:57 +0800462 return request
Flavio Castroa7162bb2016-07-25 17:30:30 -0700463
464def mod_l3_ecmp_group(ctrl, id, l3_ucast_groups):
465 buckets=[]
466 for group in l3_ucast_groups:
467 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
468
469 group_id =encode_l3_ecmp_group_id(id)
470 request = ofp.message.group_modify(group_type=ofp.OFPGT_SELECT,
471 group_id=group_id,
472 buckets=buckets
473 )
474 ctrl.message_send(request)
475 return request
476
macauley15909e72015-07-17 15:58:57 +0800477def add_l3_mcast_group(ctrl, vid, mcast_group_id, groups_on_buckets):
478 buckets=[]
479 for group in groups_on_buckets:
480 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
Pierbbdf3782016-08-22 17:58:26 -0700481
macauley15909e72015-07-17 15:58:57 +0800482 group_id =encode_l3_mcast_group_id(vid, mcast_group_id)
483 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
484 group_id=group_id,
485 buckets=buckets
486 )
487 ctrl.message_send(request)
488 return request
macauleyfddc4662015-07-27 17:40:30 +0800489
490def add_l2_overlay_flood_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
491 buckets=[]
492 for port in ports:
493 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
494
495 group_id=encode_l2_overlay_group_id(tunnel_id, 0, index)
496 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
497 group_id=group_id,
498 buckets=buckets
499 )
500 ctrl.message_send(request)
501 return request
502
503def add_l2_overlay_flood_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
504 buckets=[]
505 for port in ports:
506 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
507
508 group_id=encode_l2_overlay_group_id(tunnel_id, 1, index)
509 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
510 group_id=group_id,
511 buckets=buckets
512 )
513 ctrl.message_send(request)
514 return request
515
516def add_l2_overlay_mcast_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
517 buckets=[]
518 for port in ports:
519 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
520
521 group_id=encode_l2_overlay_group_id(tunnel_id, 2, index)
522 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
523 group_id=group_id,
524 buckets=buckets
525 )
526 ctrl.message_send(request)
527 return request
528
529def add_l2_overlay_mcast_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
530 buckets=[]
531 for port in ports:
532 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
533
534 group_id=encode_l2_overlay_group_id(tunnel_id, 3, index)
535 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
536 group_id=group_id,
537 buckets=buckets
538 )
539 ctrl.message_send(request)
540 return request
Pierbbdf3782016-08-22 17:58:26 -0700541
macauleyfddc4662015-07-27 17:40:30 +0800542def add_port_table_flow(ctrl, is_overlay=True):
543 match = ofp.match()
544
545 if is_overlay == True:
546 match.oxm_list.append(ofp.oxm.in_port(0x10000))
macauleydbff3272015-07-30 14:07:16 +0800547 NEXT_TABLE=50
macauleyfddc4662015-07-27 17:40:30 +0800548 else:
549 match.oxm_list.append(ofp.oxm.in_port(0))
Pierbbdf3782016-08-22 17:58:26 -0700550 NEXT_TABLE=10
macauleyfddc4662015-07-27 17:40:30 +0800551
552 request = ofp.message.flow_add(
Pier265ad5f2017-02-28 17:46:28 +0100553 table_id=0,
554 cookie=42,
555 match=match,
556 instructions=[
557 ofp.instruction.goto_table(NEXT_TABLE)
558 ],
559 priority=0)
macauleyfddc4662015-07-27 17:40:30 +0800560 logging.info("Add port table, match port %lx" % 0x10000)
561 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700562
Flavio Castrod8f8af22015-12-02 18:19:26 -0500563def pop_vlan_flow(ctrl, ports, vlan_id=1):
564 # table 10: vlan
565 # goto to table 20
566 msgs=[]
567 for of_port in ports:
568 match = ofp.match()
569 match.oxm_list.append(ofp.oxm.in_port(of_port))
570 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
571 request = ofp.message.flow_add(
572 table_id=10,
573 cookie=42,
574 match=match,
575 instructions=[
576 ofp.instruction.apply_actions(
577 actions=[
578 ofp.action.pop_vlan()
579 ]
580 ),
581 ofp.instruction.goto_table(20)
582 ],
583 priority=0)
584 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
585 ctrl.message_send(request)
586
587
588 return msgs
macauleyfddc4662015-07-27 17:40:30 +0800589
macauley97557232015-07-16 17:28:07 +0800590def add_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
591 # table 10: vlan
592 # goto to table 20
macauley15909e72015-07-17 15:58:57 +0800593 msgs=[]
macauley97557232015-07-16 17:28:07 +0800594 for of_port in ports:
Flavio Castro932014b2016-01-05 18:29:15 -0500595 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
macauley97557232015-07-16 17:28:07 +0800596 match = ofp.match()
597 match.oxm_list.append(ofp.oxm.in_port(of_port))
598 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
599 request = ofp.message.flow_add(
600 table_id=10,
601 cookie=42,
602 match=match,
603 instructions=[
Flavio Castrod8f8af22015-12-02 18:19:26 -0500604 ofp.instruction.apply_actions(
605 actions=[
606 ofp.action.pop_vlan()
607 ]
608 ),
macauley97557232015-07-16 17:28:07 +0800609 ofp.instruction.goto_table(20)
610 ],
611 priority=0)
612 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
613 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700614
macauley97557232015-07-16 17:28:07 +0800615 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
616 match = ofp.match()
617 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500618 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
macauley97557232015-07-16 17:28:07 +0800619 request = ofp.message.flow_add(
620 table_id=10,
621 cookie=42,
622 match=match,
623 instructions=[
624 ofp.instruction.apply_actions(
625 actions=[
Flavio Castroaf2b4502016-02-02 17:41:32 -0500626 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
macauley97557232015-07-16 17:28:07 +0800627 ]
628 ),
629 ofp.instruction.goto_table(20)
630 ],
631 priority=0)
632 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
633 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800634 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800635
Flavio Castro932014b2016-01-05 18:29:15 -0500636 if (flag == 4) :
637 match = ofp.match()
638 match.oxm_list.append(ofp.oxm.in_port(of_port))
639 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1fff))
640 request = ofp.message.flow_add(
641 table_id=10,
642 cookie=42,
643 match=match,
644 instructions=[
645 ofp.instruction.apply_actions(
646 actions=[
Flavio Castroaf2b4502016-02-02 17:41:32 -0500647 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
Flavio Castro932014b2016-01-05 18:29:15 -0500648 ]
649 ),
650 ofp.instruction.goto_table(20)
651 ],
652 priority=0)
653 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
654 ctrl.message_send(request)
655 msgs.append(request)
656
macauley97557232015-07-16 17:28:07 +0800657 if send_barrier:
658 do_barrier(ctrl)
659
macauley15909e72015-07-17 15:58:57 +0800660 return msgs
Pierbbdf3782016-08-22 17:58:26 -0700661
macauley_cheng6e6a6122015-11-16 14:19:18 +0800662def del_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
663 # table 10: vlan
664 # goto to table 20
665 msgs=[]
666 for of_port in ports:
667 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
668 match = ofp.match()
669 match.oxm_list.append(ofp.oxm.in_port(of_port))
670 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
671 request = ofp.message.flow_delete(
672 table_id=10,
673 cookie=42,
674 match=match,
675 priority=0)
676 logging.info("Del vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
677 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800678
macauley_cheng6e6a6122015-11-16 14:19:18 +0800679 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
680 match = ofp.match()
681 match.oxm_list.append(ofp.oxm.in_port(of_port))
682 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0xfff))
683 request = ofp.message.flow_delete(
684 table_id=10,
685 cookie=42,
686 match=match,
687 priority=0)
688 logging.info("Del vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
689 ctrl.message_send(request)
690 msgs.append(request)
691
692 if send_barrier:
693 do_barrier(ctrl)
694
695 return msgs
Pierbbdf3782016-08-22 17:58:26 -0700696
macauley_cheng6b311612015-09-04 11:32:27 +0800697def add_vlan_table_flow_pvid(ctrl, in_port, match_vid=None, pvid=1, send_barrier=False):
698 """it will tag pack as untagged packet wether it has tagg or not"""
699 match = ofp.match()
700 match.oxm_list.append(ofp.oxm.in_port(in_port))
701 actions=[]
702 if match_vid == None:
Pierbbdf3782016-08-22 17:58:26 -0700703 match.oxm_list.append(ofp.oxm.vlan_vid(0))
macauley_cheng6b311612015-09-04 11:32:27 +0800704 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
705 goto_table=20
706 else:
707 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+match_vid, 0x1fff))
708 actions.append(ofp.action.push_vlan(0x8100))
Pierbbdf3782016-08-22 17:58:26 -0700709 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
macauley_cheng6b311612015-09-04 11:32:27 +0800710 goto_table=20
Pierbbdf3782016-08-22 17:58:26 -0700711
macauley_cheng6b311612015-09-04 11:32:27 +0800712 request = ofp.message.flow_add(
713 table_id=10,
714 cookie=42,
715 match=match,
716 instructions=[
717 ofp.instruction.apply_actions(actions=actions)
718 ,ofp.instruction.goto_table(goto_table)
719 ],
720 priority=0)
721 logging.info("Add PVID %d on port %d and go to table %ld" %( pvid, in_port, goto_table))
Pierbbdf3782016-08-22 17:58:26 -0700722 ctrl.message_send(request)
723
macauley_cheng6b311612015-09-04 11:32:27 +0800724 if send_barrier:
725 do_barrier(ctrl)
726
727def add_vlan_table_flow_allow_all_vlan(ctrl, in_port, send_barrier=False):
728 """it st flow allow all vlan tag on this port"""
729 match = ofp.match()
730 match.oxm_list.append(ofp.oxm.in_port(in_port))
731 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1000))
732 request = ofp.message.flow_add(
733 table_id=10,
734 cookie=42,
735 match=match,
736 instructions=[
Pierbbdf3782016-08-22 17:58:26 -0700737 ofp.instruction.goto_table(20)
macauley_cheng6b311612015-09-04 11:32:27 +0800738 ],
739 priority=0)
740 logging.info("Add allow all vlan on port %d " %(in_port))
Pierbbdf3782016-08-22 17:58:26 -0700741 ctrl.message_send(request)
macauley_cheng6b311612015-09-04 11:32:27 +0800742
Pier7b031af2016-08-25 15:00:22 -0700743def 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):
744 # Install a flow for VLAN translation
745 # in VLAN table.
746 # table 10: vlan
747 # goto to table 20
748 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
749 match = ofp.match()
750 match.oxm_list.append(ofp.oxm.in_port(of_port))
751 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
752
753 actions=[]
754 if vrf!=0:
755 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
756 if new_vlan_id != -1:
757 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_vlan_id)))
758
759 request = ofp.message.flow_add(
760 table_id=10,
761 cookie=42,
762 match=match,
763 instructions=[
764 ofp.instruction.apply_actions(
765 actions=actions
766 ),
767 ofp.instruction.goto_table(20)
768 ],
769 priority=0)
770 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
771 ctrl.message_send(request)
772
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700773
774def add_one_egress_vlan_table_flow(ctrl, of_port, match_vlan, inner_vlan, outer_vlan):
775
776 # used for translating single to double tagged packets only
777
778 match = ofp.match()
779 match.oxm_list.append(ofp.oxm.exp4ByteValue(ofp.oxm.OFDPA_EXP_TYPE_ACTSET_OUTPUT, of_port))
780 match.oxm_list.append(ofp.oxm.exp1ByteValue(ofp.oxm.OFDPA_EXP_TYPE_ALLOW_VLAN_TRANSLATION, 1))
781 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+match_vlan,0x1fff))
782
783 actions=[]
784 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+inner_vlan)))
785 actions.append(ofp.action.push_vlan(0x8100))
786 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan)))
787
788 request = ofp.message.flow_add(
789 table_id=EGRESS_VLAN_FLOW_TABLE,
790 cookie=42,
791 match=match,
792 instructions=[
793 ofp.instruction.apply_actions(
794 actions=actions
795 ),
796 ofp.instruction.goto_table(EGRESS_DSCP_TABLE)
797 ],
798 priority=0)
799
800 ctrl.message_send(request)
801
802 return
803
Piere1308762016-09-12 15:29:56 -0700804def 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 -0700805
macauley0f91a3e2015-07-17 18:09:59 +0800806 # goto to table 20
Flavio Castro932014b2016-01-05 18:29:15 -0500807 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
macauley0f91a3e2015-07-17 18:09:59 +0800808 match = ofp.match()
809 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500810 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
macauley7f89d962015-08-06 18:13:48 +0800811
812 actions=[]
Alex Yashchuk9f449462017-12-09 18:27:19 +0200813 if config["switch_type"] != 'xpliant' and vrf != 0:
814 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
Pierbbdf3782016-08-22 17:58:26 -0700815
Flavio Castro6d498522015-12-15 14:05:04 -0500816 #actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id)))
macauley7f89d962015-08-06 18:13:48 +0800817
macauley0f91a3e2015-07-17 18:09:59 +0800818 request = ofp.message.flow_add(
819 table_id=10,
820 cookie=42,
821 match=match,
822 instructions=[
macauley53d90fe2015-08-04 17:34:22 +0800823 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800824 actions=actions
macauley53d90fe2015-08-04 17:34:22 +0800825 ),
826 ofp.instruction.goto_table(20)
macauley0f91a3e2015-07-17 18:09:59 +0800827 ],
828 priority=0)
829 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
830 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700831
macauley0f91a3e2015-07-17 18:09:59 +0800832 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700833
macauley0f91a3e2015-07-17 18:09:59 +0800834 match = ofp.match()
835 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500836 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
Pierbbdf3782016-08-22 17:58:26 -0700837
macauley7f89d962015-08-06 18:13:48 +0800838 actions=[]
839 if vrf!=0:
840 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
Pierbbdf3782016-08-22 17:58:26 -0700841
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700842 # actions.append(ofp.action.push_vlan(0x8100))
Flavio Castro91d1a552016-05-17 16:59:44 -0700843 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Pierbbdf3782016-08-22 17:58:26 -0700844
macauley0f91a3e2015-07-17 18:09:59 +0800845 request = ofp.message.flow_add(
846 table_id=10,
847 cookie=42,
848 match=match,
849 instructions=[
850 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800851 actions=actions
macauley0f91a3e2015-07-17 18:09:59 +0800852 ),
853 ofp.instruction.goto_table(20)
854 ],
855 priority=0)
856 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
857 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700858
Flavio Castro932014b2016-01-05 18:29:15 -0500859 if (flag == 4) :
860 match = ofp.match()
861 match.oxm_list.append(ofp.oxm.in_port(of_port))
862 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000,0x1fff))
863
864 actions=[]
865 if vrf!=0:
866 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
867
Flavio Castro91d1a552016-05-17 16:59:44 -0700868 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Flavio Castro932014b2016-01-05 18:29:15 -0500869
870 request = ofp.message.flow_add(
871 table_id=10,
872 cookie=42,
873 match=match,
874 instructions=[
875 ofp.instruction.apply_actions(
876 actions=actions
877 ),
878 ofp.instruction.goto_table(20)
879 ],
880 priority=0)
881 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
882 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800883
Piere1308762016-09-12 15:29:56 -0700884 if (flag == VLAN_TABLE_FLAG_ONLY_STACKED):
885 # This flag is meant to managed stacked vlan packtes
886 # Matches on outer VLAN_ID, set OVID with outer VLAN.
887 # Finally expose inner VLAN_ID with a pop action and
888 # goto VLAN_1_FLOW_TABLE
889 match = ofp.match()
890 match.oxm_list.append(ofp.oxm.in_port(of_port))
891 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
892
893 actions=[]
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700894 # 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 -0700895 actions.append(ofp.action.pop_vlan())
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -0700896 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 -0700897
898 request = ofp.message.flow_add(
899 table_id=10,
900 cookie=42,
901 match=match,
902 instructions=[
903 ofp.instruction.apply_actions(
904 actions=actions
905 ),
906 ofp.instruction.goto_table(VLAN_1_FLOW_TABLE)
907 ],
908 priority=0)
909 logging.info("Add vlan %d tagged packets on port %d and go to table %d" %( vlan_id, of_port, VLAN_1_FLOW_TABLE))
910 ctrl.message_send(request)
911
912 if (flag == VLAN_TABLE_FLAG_PRIORITY) :
913 match = ofp.match()
914 match.oxm_list.append(ofp.oxm.in_port(of_port))
915 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1fff))
916 request = ofp.message.flow_add(
917 table_id=10,
918 cookie=42,
919 match=match,
920 instructions=[
921 ofp.instruction.apply_actions(
922 actions=[
923 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)),
924 ofp.action.push_vlan(0x8100),
925 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+out_vlan_id)),
926 ]
927 ),
928 ofp.instruction.goto_table(20)
929 ],
930 priority=0)
931 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
932 ctrl.message_send(request)
933
934 if send_barrier:
935 do_barrier(ctrl)
936
937 return request
938
Piercf76e802016-09-19 20:16:35 -0700939def 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):
940 # table 10: vlan
941 # goto to table 13
942 if flag == VLAN_TABLE_FLAG_ONLY_TAG:
943 match = ofp.match()
944 match.oxm_list.append(ofp.oxm.in_port(of_port))
945 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id, 0x1fff))
946
947 actions=[]
948 if vlan_id == -1:
949 actions.append(ofp.action.pop_vlan())
950 if new_vlan_id > 1:
951 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_vlan_id)))
952 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
953 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
954 # 0x0000nnnn is for UNI interfaces
955 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
956
957 request = ofp.message.flow_add(
958 table_id=10,
959 cookie=42,
960 match=match,
961 instructions=[
962 ofp.instruction.apply_actions(
963 actions=actions
964 ),
965 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
966 ],
967 priority=0)
968 logging.info("Add vlan %d tagged packets on port %d and go to table %d" % (vlan_id, of_port, MPLS_L2_PORT_FLOW_TABLE))
969 ctrl.message_send(request)
970
971 if flag == VLAN_TABLE_FLAG_ONLY_UNTAG:
972 match = ofp.match()
973 match.oxm_list.append(ofp.oxm.in_port(of_port))
974 match.oxm_list.append(ofp.oxm.vlan_vid(0))
975
976 actions=[]
977 if vlan_id > 1:
Charles Chanc85f1562018-01-18 15:44:29 -0800978 # actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
979 # actions.append(ofp.action.set_field(ofp.action.push_vlan(0x8100)))
Piercf76e802016-09-19 20:16:35 -0700980 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Charles Chanc85f1562018-01-18 15:44:29 -0800981
Piercf76e802016-09-19 20:16:35 -0700982 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
983 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
984 # 0x0000nnnn is for UNI interfaces
985 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
986
987 request = ofp.message.flow_add(
988 table_id=10,
989 cookie=42,
990 match=match,
991 instructions=[
992 ofp.instruction.apply_actions(
993 actions=actions
994 ),
995 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
996 ],
997 priority=0)
998 logging.info("Add vlan %d untagged packets on port %d and go to table %d" % (vlan_id, of_port, MPLS_L2_PORT_FLOW_TABLE))
999 ctrl.message_send(request)
1000
1001 if send_barrier:
1002 do_barrier(ctrl)
1003
1004 return request
1005
Piere1308762016-09-12 15:29:56 -07001006def 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 -07001007
Piere1308762016-09-12 15:29:56 -07001008 # table 11: vlan 1 table
1009 # goto to table 20
1010 if flag == VLAN_TABLE_FLAG_ONLY_TAG:
1011 match = ofp.match()
1012 match.oxm_list.append(ofp.oxm.in_port(of_port))
1013 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
1014 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
1015
1016 actions=[]
1017 actions.append(ofp.action.push_vlan(0x8100))
1018 if new_outer_vlan_id != -1:
1019 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_outer_vlan_id)))
1020 else:
1021 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan_id)))
1022
1023 request = ofp.message.flow_add(
1024 table_id=11,
1025 cookie=42,
1026 match=match,
1027 instructions=[
1028 ofp.instruction.apply_actions(
1029 actions=actions
1030 ),
1031 ofp.instruction.goto_table(TERMINATION_FLOW_TABLE)
1032 ],
1033 priority=0)
1034 logging.info("Add vlan 1 double tagged %d-%d packets on port %d and go to table %d" %( outer_vlan_id, inner_vlan_id, of_port, TERMINATION_FLOW_TABLE))
1035 ctrl.message_send(request)
1036
1037 if flag == VLAN_TABLE_FLAG_ONLY_UNTAG:
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -07001038
Piere1308762016-09-12 15:29:56 -07001039 match = ofp.match()
1040 match.oxm_list.append(ofp.oxm.in_port(of_port))
1041 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
1042 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
1043
1044 actions=[]
1045 request = ofp.message.flow_add(
1046 table_id=11,
1047 cookie=42,
1048 match=match,
1049 instructions=[
1050 ofp.instruction.apply_actions(
1051 actions=actions
1052 ),
1053 ofp.instruction.goto_table(TERMINATION_FLOW_TABLE)
1054 ],
1055 priority=0)
1056 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))
1057 ctrl.message_send(request)
1058
Andreas Pantelopoulosf83e0212018-03-18 20:44:05 -07001059 if flag == VLAN_TABLE_FLAG_ONLY_POP_VLAN:
1060
1061 print("INSTALLIN IN TABLE 11!")
1062
1063 match = ofp.match()
1064 match.oxm_list.append(ofp.oxm.in_port(of_port))
1065 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
1066 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
1067
1068 actions=[]
1069 actions.append(ofp.action.pop_vlan())
1070
1071 request = ofp.message.flow_add(
1072 table_id=11,
1073 cookie=42,
1074 match=match,
1075 instructions=[
1076 ofp.instruction.apply_actions(
1077 actions=actions
1078 ),
1079 ofp.instruction.goto_table(TERMINATION_FLOW_TABLE)
1080 ],
1081 priority=0)
1082 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))
1083 ctrl.message_send(request)
1084
1085
macauley0f91a3e2015-07-17 18:09:59 +08001086 if send_barrier:
1087 do_barrier(ctrl)
1088
1089 return request
Pierbbdf3782016-08-22 17:58:26 -07001090
Saurav Das5d1473d2018-07-12 11:02:56 -07001091def 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 -08001092
Piercf76e802016-09-19 20:16:35 -07001093 # table 11: vlan 1 table
1094 # goto to table 13
1095 match = ofp.match()
1096 match.oxm_list.append(ofp.oxm.in_port(of_port))
1097 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
1098 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
1099
1100 actions=[]
Saurav Das5d1473d2018-07-12 11:02:56 -07001101
1102 if cross_connect:
1103 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+inner_vlan_id)))
Piercf76e802016-09-19 20:16:35 -07001104 actions.append(ofp.action.push_vlan(0x8100))
1105 if new_outer_vlan_id != -1:
Saurav Das5d1473d2018-07-12 11:02:56 -07001106 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_outer_vlan_id)))
Piercf76e802016-09-19 20:16:35 -07001107 else:
Saurav Das5d1473d2018-07-12 11:02:56 -07001108 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan_id)))
Piercf76e802016-09-19 20:16:35 -07001109
Saurav Das5d1473d2018-07-12 11:02:56 -07001110 if not cross_connect:
1111 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
1112 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
1113 else:
1114 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE_CROSS_CONNECT)))
1115
Piercf76e802016-09-19 20:16:35 -07001116 # 0x0000nnnn is for UNI interfaces
1117 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
1118
1119 request = ofp.message.flow_add(
Saurav Das5d1473d2018-07-12 11:02:56 -07001120 table_id=11,
1121 cookie=42,
1122 match=match,
1123 instructions=[
1124 ofp.instruction.apply_actions(
1125 actions=actions
1126 ),
1127 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
1128 ],
1129 priority=0)
Piercf76e802016-09-19 20:16:35 -07001130 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))
1131 ctrl.message_send(request)
Piercf76e802016-09-19 20:16:35 -07001132 if send_barrier:
1133 do_barrier(ctrl)
1134
1135 return request
1136
macauley97557232015-07-16 17:28:07 +08001137def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False):
1138 match = ofp.match()
castroflavio21894482015-12-08 15:29:55 -05001139 priority=500
macauleyfddc4662015-07-27 17:40:30 +08001140 if dst_mac!=None:
castroflavio21894482015-12-08 15:29:55 -05001141 priority=1000
macauleyfddc4662015-07-27 17:40:30 +08001142 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
1143
macauley97557232015-07-16 17:28:07 +08001144 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauleyfddc4662015-07-27 17:40:30 +08001145
macauley97557232015-07-16 17:28:07 +08001146 request = ofp.message.flow_add(
1147 table_id=50,
1148 cookie=42,
1149 match=match,
1150 instructions=[
1151 ofp.instruction.write_actions(
1152 actions=[
1153 ofp.action.group(group_id)]),
1154 ofp.instruction.goto_table(60)
1155 ],
1156 buffer_id=ofp.OFP_NO_BUFFER,
castroflavio21894482015-12-08 15:29:55 -05001157 priority=priority)
macauley97557232015-07-16 17:28:07 +08001158
1159 logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac)
1160 ctrl.message_send(request)
1161
1162 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001163 do_barrier(ctrl)
macauley15909e72015-07-17 15:58:57 +08001164
Pierbbdf3782016-08-22 17:58:26 -07001165 return request
macauleyfddc4662015-07-27 17:40:30 +08001166
1167def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False):
1168 match = ofp.match()
1169 if dst_mac!=None:
1170 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
1171
1172 match.oxm_list.append(ofp.oxm.tunnel_id(vnid))
1173 if is_group == True:
1174 actions=[ofp.action.group(group_id)]
1175 else:
1176 actions=[ofp.action.output(group_id)]
1177
1178 request = ofp.message.flow_add(
1179 table_id=50,
1180 cookie=42,
1181 match=match,
1182 instructions=[
1183 ofp.instruction.write_actions(
1184 actions=actions),
1185 ofp.instruction.goto_table(60)
1186 ],
1187 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001188 priority=1000)
macauleyfddc4662015-07-27 17:40:30 +08001189
1190 logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac)
1191 ctrl.message_send(request)
1192
1193 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001194 do_barrier(ctrl)
macauleyfddc4662015-07-27 17:40:30 +08001195
Pierbbdf3782016-08-22 17:58:26 -07001196 return request
1197
macauley_cheng6b133662015-11-09 13:52:39 +08001198def add_termination_flow(ctrl, in_port, eth_type, dst_mac, vlanid, goto_table=None, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +08001199 match = ofp.match()
macauley0f91a3e2015-07-17 18:09:59 +08001200 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauleyfddc4662015-07-27 17:40:30 +08001201 if dst_mac[0]&0x01 == 0x01:
1202 match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00]))
1203 goto_table=40
1204 else:
macauley53d90fe2015-08-04 17:34:22 +08001205 if in_port!=0:
1206 match.oxm_list.append(ofp.oxm.in_port(in_port))
macauleyfddc4662015-07-27 17:40:30 +08001207 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
1208 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauley_cheng6b133662015-11-09 13:52:39 +08001209 if goto_table == None:
1210 goto_table=30
macauley0f91a3e2015-07-17 18:09:59 +08001211
1212 request = ofp.message.flow_add(
1213 table_id=20,
1214 cookie=42,
1215 match=match,
1216 instructions=[
1217 ofp.instruction.goto_table(goto_table)
1218 ],
1219 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001220 priority=1)
macauley0f91a3e2015-07-17 18:09:59 +08001221
1222 logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac)
1223 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +08001224 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001225 do_barrier(ctrl)
macauley0f91a3e2015-07-17 18:09:59 +08001226
Pierbbdf3782016-08-22 17:58:26 -07001227 return request
1228
Alex Yashchuk9f449462017-12-09 18:27:19 +02001229def 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 +08001230 match = ofp.match()
1231 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
Alex Yashchuk9f449462017-12-09 18:27:19 +02001232 if config["switch_type"] != 'xpliant' and vrf != 0:
macauley53d90fe2015-08-04 17:34:22 +08001233 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
macauley0f91a3e2015-07-17 18:09:59 +08001234
Flavio Castroaf2b4502016-02-02 17:41:32 -05001235 match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask))
1236
1237 instructions = []
1238 instructions.append(ofp.instruction.goto_table(60))
1239 if send_ctrl:
Pier265ad5f2017-02-28 17:46:28 +01001240 instructions.append(ofp.instruction.write_actions(
Flavio Castroaf2b4502016-02-02 17:41:32 -05001241 actions=[ofp.action.output( port=ofp.OFPP_CONTROLLER,
1242 max_len=ofp.OFPCML_NO_BUFFER)]))
Pierbbdf3782016-08-22 17:58:26 -07001243 else:
Flavio Castroaf2b4502016-02-02 17:41:32 -05001244 instructions.append(ofp.instruction.write_actions(
1245 actions=[ofp.action.group(action_group_id)]))
macauley53d90fe2015-08-04 17:34:22 +08001246
macauley0f91a3e2015-07-17 18:09:59 +08001247 request = ofp.message.flow_add(
1248 table_id=30,
1249 cookie=42,
1250 match=match,
Flavio Castroaf2b4502016-02-02 17:41:32 -05001251 instructions=instructions,
macauley0f91a3e2015-07-17 18:09:59 +08001252 buffer_id=ofp.OFP_NO_BUFFER,
Alex Yashchuk9f449462017-12-09 18:27:19 +02001253 priority=priority)
macauley0f91a3e2015-07-17 18:09:59 +08001254
1255 logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip)
1256 ctrl.message_send(request)
1257
1258 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001259 do_barrier(ctrl)
macauley0f91a3e2015-07-17 18:09:59 +08001260
Flavio Castro9debaaa2016-07-26 19:37:50 -07001261 return request
Flavio Castrod8f8af22015-12-02 18:19:26 -05001262
Andrea Campanellabfb42b32018-04-26 10:57:23 +02001263def add_unicast_blackhole_flow(ctrl, eth_type, dst_ip, mask, vrf=0, send_ctrl=False, send_barrier=False, priority = 1):
1264 match = ofp.match()
1265 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1266 if config["switch_type"] != 'xpliant' and vrf != 0:
1267 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
1268
1269 match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask))
1270
1271 instructions = []
1272 instructions.append(ofp.instruction.goto_table(60))
1273 instructions.append(ofp.instruction.clear_actions( ))
1274
1275 request = ofp.message.flow_add(
1276 table_id=30,
1277 cookie=42,
1278 match=match,
1279 instructions=instructions,
1280 buffer_id=ofp.OFP_NO_BUFFER,
1281 priority=priority)
1282
1283 logging.info("Inserting unicast blackhole routing flow eth_type %lx, dip %ld",eth_type, dst_ip)
1284 ctrl.message_send(request)
1285
1286 if send_barrier:
1287 do_barrier(ctrl)
1288
1289 return request
1290
Pier1e4e98e2016-10-26 14:36:05 -07001291def add_unicast_v6_routing_flow(ctrl, eth_type, dst_ip, mask, action_group_id, vrf=0, send_ctrl=False, send_barrier=False):
1292 match = ofp.match()
1293 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1294 if vrf != 0:
1295 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
1296
1297 match.oxm_list.append(ofp.oxm.ipv6_dst_masked(parse_ipv6(dst_ip), parse_ipv6(mask)))
1298
1299 instructions = []
1300 instructions.append(ofp.instruction.goto_table(60))
1301 if send_ctrl:
Pier265ad5f2017-02-28 17:46:28 +01001302 instructions.append(ofp.instruction.write_actions(
Pier1e4e98e2016-10-26 14:36:05 -07001303 actions=[ofp.action.output( port=ofp.OFPP_CONTROLLER,
1304 max_len=ofp.OFPCML_NO_BUFFER)]))
1305 else:
1306 instructions.append(ofp.instruction.write_actions(
1307 actions=[ofp.action.group(action_group_id)]))
1308
1309 request = ofp.message.flow_add(
1310 table_id=30,
1311 cookie=42,
1312 match=match,
1313 instructions=instructions,
1314 buffer_id=ofp.OFP_NO_BUFFER,
1315 priority=1)
1316
1317 logging.info("Inserting unicast routing flow eth_type %lx, dip %s",eth_type, dst_ip)
1318 ctrl.message_send(request)
1319
1320 if send_barrier:
1321 do_barrier(ctrl)
1322
1323 return request
1324
Andreas Pantelopoulos6c76b942018-01-22 10:03:02 -08001325def 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 -04001326 match = ofp.match()
1327 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1328 match.oxm_list.append(ofp.oxm.mpls_label(label))
1329 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
Pier265ad5f2017-02-28 17:46:28 +01001330 write_actions = []
1331 write_actions.append(ofp.action.group(action_group_id))
1332 apply_actions = []
1333 apply_actions = [ofp.action.dec_mpls_ttl(),
Flavio Castro8ca52542016-04-11 11:24:49 -04001334 ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))]
Pier265ad5f2017-02-28 17:46:28 +01001335 if (goto_table != 29):
1336 apply_actions.append(ofp.action.set_field(
Flavio Castrod80fbc32016-07-25 15:54:26 -07001337 ofp.oxm.exp2ByteValue(exp_type=23, value=32)))
Pier265ad5f2017-02-28 17:46:28 +01001338 apply_actions.append(ofp.action.copy_ttl_in())
Flavio Castro9debaaa2016-07-26 19:37:50 -07001339
Flavio Castrob702a2f2016-04-10 22:01:48 -04001340 request = ofp.message.flow_add(
1341 table_id=24,
1342 cookie=43,
1343 match=match,
1344 instructions=[
Pier265ad5f2017-02-28 17:46:28 +01001345 ofp.instruction.apply_actions(actions=apply_actions),
1346 ofp.instruction.write_actions(actions=write_actions),
Flavio Castro8ca52542016-04-11 11:24:49 -04001347 ofp.instruction.goto_table(goto_table)
Flavio Castrob702a2f2016-04-10 22:01:48 -04001348 ],
1349 buffer_id=ofp.OFP_NO_BUFFER,
1350 priority=1)
Flavio Castrob702a2f2016-04-10 22:01:48 -04001351 ctrl.message_send(request)
1352
1353 if send_barrier:
1354 do_barrier(ctrl)
1355
1356 return request
1357
Alex Yashchuk9f449462017-12-09 18:27:19 +02001358def xpliant_add_mpls_flow(ctrl, action_group_id=0x0, label=100 ,ethertype=0x0800, bos=1, vrf=1, goto_table=27, send_barrier=False):
1359 match = ofp.match()
1360 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1361 match.oxm_list.append(ofp.oxm.mpls_label(label))
1362
1363 apply_actions = []
1364 apply_actions.append(ofp.action.group(action_group_id))
1365 apply_actions.append(ofp.action.dec_mpls_ttl())
1366 if (goto_table != 29):
1367 apply_actions.append(ofp.action.pop_mpls(ethertype))
1368
1369 request = ofp.message.flow_add(
1370 table_id=24,
1371 cookie=43,
1372 match=match,
1373 instructions=[
1374 ofp.instruction.apply_actions(actions=apply_actions),
1375 ],
1376 buffer_id=ofp.OFP_NO_BUFFER,
1377 priority=1)
1378 logging.info("Inserting MPLS flow , label %ld", label)
1379 ctrl.message_send(request)
1380
1381 if send_barrier:
1382 do_barrier(ctrl)
1383
1384 return request
1385
Andreas Pantelopoulos6c76b942018-01-22 10:03:02 -08001386def add_mpls_flow_swap(ctrl, action_group_id, label, ethertype, bos, goto_table=MPLS_TYPE_FLOW_TABLE, of_port=0, send_barrier=False):
1387 match = ofp.match()
1388 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1389 match.oxm_list.append(ofp.oxm.mpls_label(label))
1390 match.oxm_list.append(ofp.oxm.mpls_bos(1))
1391
1392 apply_actions = []
1393 write_actions = []
1394 apply_actions.append(ofp.action.dec_mpls_ttl())
1395 write_actions.append(ofp.action.group(action_group_id))
1396
1397 request = ofp.message.flow_add(
1398 table_id=24,
1399 cookie=43,
1400 match=match,
1401 instructions=[
1402 ofp.instruction.apply_actions(actions=apply_actions),
1403 ofp.instruction.write_actions(actions=write_actions),
1404 ofp.instruction.goto_table(goto_table)
1405 ],
1406 buffer_id=ofp.OFP_NO_BUFFER,
1407 priority=1)
1408
1409 logging.info("Inserting MPLS flow , label %ld", label)
1410 ctrl.message_send(request)
1411
1412 if send_barrier:
1413 do_barrier(ctrl)
1414
1415 return request
1416
1417
Pierf6f28162016-09-22 16:30:52 -07001418def 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 -07001419 match = ofp.match()
1420 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1421 match.oxm_list.append(ofp.oxm.mpls_label(label))
1422 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
1423
Pier265ad5f2017-02-28 17:46:28 +01001424 apply_actions = []
1425 write_actions = []
1426 apply_actions.append(ofp.action.dec_mpls_ttl())
Pierf6f28162016-09-22 16:30:52 -07001427 if popMPLS == True:
Pier265ad5f2017-02-28 17:46:28 +01001428 apply_actions.append(ofp.action.copy_ttl_in())
1429 apply_actions.append(ofp.action.pop_mpls(ethertype))
Pierf6f28162016-09-22 16:30:52 -07001430 if bos==1 and popL2 == True:
Pier265ad5f2017-02-28 17:46:28 +01001431 apply_actions.append(ofp.action.ofdpa_pop_l2_header())
1432 apply_actions.append(ofp.action.ofdpa_pop_cw())
1433 apply_actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
Pierf6f28162016-09-22 16:30:52 -07001434 # 0x0002nnnn is for UNI interfaces
Pier265ad5f2017-02-28 17:46:28 +01001435 apply_actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00020000 + of_port)))
1436 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 -07001437
Pier265ad5f2017-02-28 17:46:28 +01001438 write_actions.append(ofp.action.group(action_group_id))
Pierb5da4c92016-09-21 11:23:35 -07001439
1440 request = ofp.message.flow_add(
Pier265ad5f2017-02-28 17:46:28 +01001441 table_id=24,
1442 cookie=43,
1443 match=match,
1444 instructions=[
1445 ofp.instruction.apply_actions(actions=apply_actions),
1446 ofp.instruction.write_actions(actions=write_actions),
1447 ofp.instruction.goto_table(goto_table)
1448 ],
1449 buffer_id=ofp.OFP_NO_BUFFER,
1450 priority=1)
Pierb5da4c92016-09-21 11:23:35 -07001451 logging.info("Inserting MPLS flow , label %ld", label)
1452 ctrl.message_send(request)
1453
1454 if send_barrier:
1455 do_barrier(ctrl)
1456
1457 return request
1458
macauleyfddc4662015-07-27 17:40:30 +08001459def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False):
1460 match = ofp.match()
1461 match.oxm_list.append(ofp.oxm.eth_type(0x0800))
Alex Yashchuk9f449462017-12-09 18:27:19 +02001462 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000 + vlan_id))
macauleyfddc4662015-07-27 17:40:30 +08001463 if src_ip_mask!=0:
1464 match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask))
1465 else:
1466 match.oxm_list.append(ofp.oxm.ipv4_src(src_ip))
Pierbbdf3782016-08-22 17:58:26 -07001467
macauleyfddc4662015-07-27 17:40:30 +08001468 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
Pierbbdf3782016-08-22 17:58:26 -07001469
macauleyfddc4662015-07-27 17:40:30 +08001470 request = ofp.message.flow_add(
1471 table_id=40,
1472 cookie=42,
1473 match=match,
1474 instructions=[
1475 ofp.instruction.write_actions(
1476 actions=[ofp.action.group(action_group_id)]),
1477 ofp.instruction.goto_table(60)
1478 ],
1479 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001480 priority=1)
macauleyfddc4662015-07-27 17:40:30 +08001481
1482 logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask)
1483 ctrl.message_send(request)
1484
1485 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001486 do_barrier(ctrl)
macauleyfddc4662015-07-27 17:40:30 +08001487
Pierbbdf3782016-08-22 17:58:26 -07001488 return request
macauley_cheng6b133662015-11-09 13:52:39 +08001489
Pier1e4e98e2016-10-26 14:36:05 -07001490def add_acl_rule(ctrl, eth_type=None, ip_proto=None, send_barrier=False):
1491 match = ofp.match()
1492 if eth_type != None:
1493 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1494 if ip_proto != None:
1495 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1496
1497 request = ofp.message.flow_add(
1498 table_id=60,
1499 cookie=42,
1500 match=match,
1501 instructions=[
1502 ofp.instruction.apply_actions(
1503 actions=[ofp.action.output(port=ofp.OFPP_CONTROLLER, max_len=ofp.OFPCML_NO_BUFFER)]
1504 ),
1505 ],
1506 buffer_id=ofp.OFP_NO_BUFFER,
1507 priority=1
1508 )
1509
1510 logging.info("Inserting ACL flow eth_type %lx, ip_proto %ld", eth_type, ip_proto)
1511 ctrl.message_send(request)
1512
1513 if send_barrier:
1514 do_barrier(ctrl)
1515
1516 return request
1517
macauley_cheng6b133662015-11-09 13:52:39 +08001518#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 +08001519def 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 +08001520 match = ofp.match()
1521 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1522 match.oxm_list.append(ofp.oxm.ipv4_dst(ip_dst))
1523 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1524 match.oxm_list.append(ofp.oxm.tcp_dst(tcp_dst))
Pierbbdf3782016-08-22 17:58:26 -07001525
macauley_cheng6b133662015-11-09 13:52:39 +08001526 request = ofp.message.flow_add(
1527 table_id=28,
1528 cookie=42,
1529 match=match,
1530 instructions=[
1531 ofp.instruction.write_actions(
1532 actions=[ofp.action.set_field(ofp.oxm.ipv4_dst(set_ip_dst)),
1533 ofp.action.set_field(ofp.oxm.tcp_dst(set_tcp_dst)),
1534 ofp.action.group(action_group_id)]),
1535 ofp.instruction.goto_table(60)
1536 ],
1537 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001538 priority=1)
macauley_cheng6b133662015-11-09 13:52:39 +08001539 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)
1540 ctrl.message_send(request)
1541 return request
macauley_chengeffc20a2015-11-09 16:14:56 +08001542
1543#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
1544def add_snat_flow(ctrl, eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src):
1545 match = ofp.match()
1546 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1547 match.oxm_list.append(ofp.oxm.ipv4_src(ip_src))
1548 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1549 match.oxm_list.append(ofp.oxm.tcp_src(tcp_src))
Pierbbdf3782016-08-22 17:58:26 -07001550
macauley_chengeffc20a2015-11-09 16:14:56 +08001551 request = ofp.message.flow_add(
1552 table_id=29,
1553 cookie=42,
1554 match=match,
1555 instructions=[
1556 ofp.instruction.write_actions(
1557 actions=[ofp.action.set_field(ofp.oxm.ipv4_src(set_ip_src)),
1558 ofp.action.set_field(ofp.oxm.tcp_src(set_tcp_src))]),
1559 ofp.instruction.goto_table(30)
1560 ],
1561 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001562 priority=1)
macauley_chengeffc20a2015-11-09 16:14:56 +08001563 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)
1564 ctrl.message_send(request)
1565 return request
Pierbbdf3782016-08-22 17:58:26 -07001566
1567def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001568 """
1569 Command Example:
1570 of-agent vtap 10001 ethernet 1/1 vid 1
1571 of-agent vtp 10001 vni 10
1572 """
1573 if vlan != 0:
1574 config_vtap_xml="""
1575 <config>
1576 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1577 <id>capable-switch-1</id>
1578 <resources>
1579 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001580 <resource-id >LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001581 <features>
1582 <current>
1583 <rate>10Gb</rate>
1584 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001585 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001586 </current>
1587 <advertised>
1588 <rate>10Gb</rate>
1589 <rate>100Gb</rate>
1590 <medium>fiber</medium>
1591 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001592 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001593 <supported>
1594 <rate>10Gb</rate>
1595 <rate>100Gb</rate>
1596 <medium>fiber</medium>
1597 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001598 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001599 <advertised-peer>
1600 <rate>10Gb</rate>
1601 <rate>100Gb</rate>
1602 <medium>fiber</medium>
1603 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001604 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001605 </features>
1606 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1607 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1608 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1609 <ofdpa10:vni>VNID</ofdpa10:vni>
1610 </ofdpa10:vtap>
Pierbbdf3782016-08-22 17:58:26 -07001611 </port>
macauleyfddc4662015-07-27 17:40:30 +08001612 </resources>
1613 <logical-switches>
1614 <switch>
1615 <id>DATAPATH_ID</id>
1616 <datapath-id>DATAPATH_ID</datapath-id>
1617 <resources>
1618 <port xc:operation="OPERATION">LPORT</port>
1619 </resources>
1620 </switch>
1621 </logical-switches>
1622 </capable-switch>
1623 </config>
Pierbbdf3782016-08-22 17:58:26 -07001624 """
macauleyfddc4662015-07-27 17:40:30 +08001625 else:
1626 config_vtap_xml="""
1627 <config>
1628 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1629 <id>capable-switch-1</id>
1630 <resources>
1631 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001632 <resource-id >LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001633 <features>
1634 <current>
1635 <rate>10Gb</rate>
1636 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001637 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001638 </current>
1639 <advertised>
1640 <rate>10Gb</rate>
1641 <rate>100Gb</rate>
1642 <medium>fiber</medium>
1643 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001644 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001645 <supported>
1646 <rate>10Gb</rate>
1647 <rate>100Gb</rate>
1648 <medium>fiber</medium>
1649 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001650 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001651 <advertised-peer>
1652 <rate>10Gb</rate>
1653 <rate>100Gb</rate>
1654 <medium>fiber</medium>
1655 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001656 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001657 </features>
1658 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1659 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1660 <ofdpa10:vni>VNID</ofdpa10:vni>
1661 </ofdpa10:vtap>
Pierbbdf3782016-08-22 17:58:26 -07001662 </port>
macauleyfddc4662015-07-27 17:40:30 +08001663 </resources>
1664 <logical-switches>
1665 <switch>
1666 <id>DATAPATH_ID</id>
1667 <datapath-id>DATAPATH_ID</datapath-id>
1668 <resources>
1669 <port xc:operation="OPERATION">LPORT</port>
1670 </resources>
1671 </switch>
1672 </logical-switches>
1673 </capable-switch>
1674 </config>
Pierbbdf3782016-08-22 17:58:26 -07001675 """
1676 str_datapath_id_f= "{:016x}".format(dp_id)
1677 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1678 config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id)
1679 config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport)))
1680 config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port))
1681 config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan))
macauleyfddc4662015-07-27 17:40:30 +08001682 config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid))
1683 config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation))
1684 return config_vtap_xml
Pierbbdf3782016-08-22 17:58:26 -07001685
1686def 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 +08001687 """
1688 Command Example:
1689 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 -07001690 of-agent vtp 10001 vni 10
macauleyfddc4662015-07-27 17:40:30 +08001691 """
1692
1693 config_vtep_xml="""
1694 <config>
1695 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1696 <id>capable-switch-1</id>
1697 <resources>
1698 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001699 <resource-id>LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001700 <features>
1701 <current>
1702 <rate>10Gb</rate>
1703 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001704 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001705 </current>
1706 <advertised>
1707 <rate>10Gb</rate>
1708 <rate>100Gb</rate>
1709 <medium>fiber</medium>
1710 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001711 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001712 <supported>
1713 <rate>10Gb</rate>
1714 <rate>100Gb</rate>
1715 <medium>fiber</medium>
1716 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001717 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001718 <advertised-peer>
1719 <rate>10Gb</rate>
1720 <rate>100Gb</rate>
1721 <medium>fiber</medium>
1722 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001723 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001724 </features>
Pier265ad5f2017-02-28 17:46:28 +01001725 <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01">
1726 <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip>
1727 <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip>
1728 <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port>
1729 <ofdpa10:vni xc:operation="OPERATION">
macauley25999cf2015-08-07 17:03:24 +08001730 <ofdpa10:id>VNID</ofdpa10:id>
1731 </ofdpa10:vni>
Pier265ad5f2017-02-28 17:46:28 +01001732 <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id>
1733 <ofdpa10:ttl>TTL</ofdpa10:ttl>
1734 </ofdpa10:vtep>
Pierbbdf3782016-08-22 17:58:26 -07001735 </port>
macauleyfddc4662015-07-27 17:40:30 +08001736 </resources>
1737 <logical-switches>
1738 <switch>
1739 <id>DATAPATH_ID</id>
1740 <datapath-id>DATAPATH_ID</datapath-id>
1741 <resources>
1742 <port xc:operation="OPERATION">LPORT</port>
1743 </resources>
1744 </switch>
1745 </logical-switches>
1746 </capable-switch>
Pierbbdf3782016-08-22 17:58:26 -07001747 </config>
macauleyfddc4662015-07-27 17:40:30 +08001748 """
Pierbbdf3782016-08-22 17:58:26 -07001749 str_datapath_id_f= "{:016x}".format(dp_id)
1750 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1751 config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +08001752 config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport)))
Pierbbdf3782016-08-22 17:58:26 -07001753 config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip))
1754 config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip))
1755 config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port))
1756 config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1757 config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl))
macauleyfddc4662015-07-27 17:40:30 +08001758 config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid))
Pierbbdf3782016-08-22 17:58:26 -07001759 config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation))
macauleyfddc4662015-07-27 17:40:30 +08001760
Pierbbdf3782016-08-22 17:58:26 -07001761 return config_vtep_xml
1762
1763def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001764 #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2
1765 config_nexthop_xml="""
1766 <config>
1767 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1768 <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1769 <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id>
1770 <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac>
1771 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1772 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1773 </ofdpa10:next-hop>
1774 </of11-config:capable-switch>
1775 </config>
1776 """
1777 config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan))
Pierbbdf3782016-08-22 17:58:26 -07001778 config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port))
1779 config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1780 config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac))
1781 config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation))
1782 return config_nexthop_xml
macauleyfddc4662015-07-27 17:40:30 +08001783
Pierbbdf3782016-08-22 17:58:26 -07001784def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001785 #of-agent vni 10 multicast 224.1.1.1 nexthop 20
Pierbbdf3782016-08-22 17:58:26 -07001786 if mcast_ipv4!=None:
macauleyfddc4662015-07-27 17:40:30 +08001787 config_vni_xml="""
1788 <config>
1789 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1790 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1791 <ofdpa10:id>VNID</ofdpa10:id>
1792 <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group>
1793 <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id>
1794 </ofdpa10:vni>
1795 </of11-config:capable-switch>
1796 </config>
Pierbbdf3782016-08-22 17:58:26 -07001797 """
1798 config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1799 config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4))
macauleyfddc4662015-07-27 17:40:30 +08001800 else:
1801 config_vni_xml="""
1802 <config>
1803 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1804 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1805 <ofdpa10:id>VNID</ofdpa10:id>
1806 </ofdpa10:vni>
1807 </of11-config:capable-switch>
1808 </config>
Pierbbdf3782016-08-22 17:58:26 -07001809 """
1810
1811 config_vni_xml=config_vni_xml.replace("VNID", str(vni_id))
1812 config_vni_xml=config_vni_xml.replace("OPERATION", str(operation))
macauleyfddc4662015-07-27 17:40:30 +08001813 return config_vni_xml
Pierbbdf3782016-08-22 17:58:26 -07001814
1815def get_featureReplay(self):
macauleyfddc4662015-07-27 17:40:30 +08001816 req = ofp.message.features_request()
1817 res, raw = self.controller.transact(req)
Pierbbdf3782016-08-22 17:58:26 -07001818 self.assertIsNotNone(res, "Did not receive a response from the DUT.")
macauleyfddc4662015-07-27 17:40:30 +08001819 self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY,
1820 ("Unexpected packet type %d received in response to "
1821 "OFPT_FEATURES_REQUEST") % res.type)
Pierbbdf3782016-08-22 17:58:26 -07001822 return res
1823
macauleyfddc4662015-07-27 17:40:30 +08001824def send_edit_config(switch_ip, xml, target='runing'):
1825 NETCONF_ACCOUNT="netconfuser"
1826 NETCONF_PASSWD="netconfuser"
1827 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1828 try:
Pierbbdf3782016-08-22 17:58:26 -07001829 m.edit_config(target='running',
1830 config=xml,
1831 default_operation='merge',
macauleyfddc4662015-07-27 17:40:30 +08001832 error_option='stop-on-error')
1833
1834 except Exception as e:
1835 logging.info("Fail to set xml %s", xml)
1836 return False
1837
Pier265ad5f2017-02-28 17:46:28 +01001838 #return m.get_config(source='running').data_xml
macauleyfddc4662015-07-27 17:40:30 +08001839 return True
1840
1841def send_delete_config(switch_ip, xml, target='runing'):
1842 NETCONF_ACCOUNT="netconfuser"
1843 NETCONF_PASSWD="netconfuser"
1844 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1845 try:
Pierbbdf3782016-08-22 17:58:26 -07001846 m.edit_config(target='running',
1847 config=xml,
1848 default_operation='delete',
macauleyfddc4662015-07-27 17:40:30 +08001849 error_option='stop-on-error')
1850
1851 except Exception as e:
1852 logging.info("Fail to set xml %s", xml)
1853 return False
1854
Pier265ad5f2017-02-28 17:46:28 +01001855 #return m.get_config(source='running').data_xml
macauleyfddc4662015-07-27 17:40:30 +08001856 return True
Pierbbdf3782016-08-22 17:58:26 -07001857
macauleyfddc4662015-07-27 17:40:30 +08001858def get_edit_config(switch_ip, target='runing'):
1859 NETCONF_ACCOUNT="netconfuser"
1860 NETCONF_PASSWD="netconfuser"
1861 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 +01001862 return m.get_config(source='running').data_xml
macauleydbff3272015-07-30 14:07:16 +08001863
macauley_cheng67da9262015-08-31 15:18:41 +08001864
1865"""
1866MPLS
1867"""
1868
1869OFDPA_MPLS_SUBTYPE_SHIFT=24
Pierbbdf3782016-08-22 17:58:26 -07001870OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1
macauley_cheng67da9262015-08-31 15:18:41 +08001871OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2
1872OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3
1873OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4
1874OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5
1875OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6
1876OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8
1877OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10
1878
Piercf76e802016-09-19 20:16:35 -07001879
1880
1881
macauley_cheng67da9262015-08-31 15:18:41 +08001882def encode_mpls_interface_group_id(subtype, index):
1883 index=index&0x00ffffff
1884 assert(subtype==0)
1885 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1886
1887def encode_mpls_label_group_id(subtype, index):
1888 index=index&0x00ffffff
1889 assert(subtype <=5 or subtype==0)
1890 #1: l2 vpn label
1891 #2: l3 vpn label
1892 #3: mpls tunnel label 1
1893 #4: mpls tunnel lable 2
1894 #5: mpls swap label
Pierbbdf3782016-08-22 17:58:26 -07001895 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
macauley_cheng67da9262015-08-31 15:18:41 +08001896
1897def encode_mpls_forwarding_group_id(subtype, index):
1898 index=index&0x00ffffff
1899 assert(subtype==6 or subtype==8 or subtype==10)
Pierbbdf3782016-08-22 17:58:26 -07001900 return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
macauley_cheng67da9262015-08-31 15:18:41 +08001901
1902
Andreas Pantelopoulosaaa02622018-04-04 15:13:03 -07001903def 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 +08001904 action=[]
1905 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
1906 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
Pier265ad5f2017-02-28 17:46:28 +01001907 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001908 action.append(ofp.action.group(ref_gid))
Pierbbdf3782016-08-22 17:58:26 -07001909
macauley_cheng67da9262015-08-31 15:18:41 +08001910 buckets = [ofp.bucket(actions=action)]
1911
1912 mpls_group_id =encode_mpls_interface_group_id(subtype, index)
Andreas Pantelopoulosaaa02622018-04-04 15:13:03 -07001913 if add:
1914 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1915 group_id=mpls_group_id,
1916 buckets=buckets
1917 )
1918 else:
1919 request = ofp.message.group_modify(group_type=ofp.OFPGT_INDIRECT,
1920 group_id=mpls_group_id,
1921 buckets=buckets
1922 )
1923
Andreas Pantelopoulos6c76b942018-01-22 10:03:02 -08001924
1925 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 +08001926 ctrl.message_send(request)
Pier1e4e98e2016-10-26 14:36:05 -07001927
1928 if send_barrier:
1929 do_barrier(ctrl)
macauley_cheng67da9262015-08-31 15:18:41 +08001930 return mpls_group_id, request
1931
Piercf76e802016-09-19 20:16:35 -07001932def add_mpls_tunnel_label_group(
1933 ctrl,
1934 ref_gid,
1935 subtype,
1936 index,
1937 label,
1938 ):
1939
1940 action=[]
1941 action.append(ofp.action.push_mpls(0x8847))
1942 action.append(ofp.action.set_field(ofp.oxm.mpls_label(label)))
1943 action.append(ofp.action.group(ref_gid))
1944 buckets = [ofp.bucket(actions=action)]
1945
1946 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1947 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1948 group_id=mpls_group_id,
1949 buckets=buckets
1950 )
1951 ctrl.message_send(request)
1952
1953 return mpls_group_id, request
1954
Pierb5da4c92016-09-21 11:23:35 -07001955def add_mpls_swap_label_group(
1956 ctrl,
1957 ref_gid,
1958 subtype,
1959 index,
1960 label,
1961 ):
1962
1963 action=[]
1964 action.append(ofp.action.set_field(ofp.oxm.mpls_label(label)))
1965 action.append(ofp.action.group(ref_gid))
1966 buckets = [ofp.bucket(actions=action)]
1967
1968 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1969 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1970 group_id=mpls_group_id,
1971 buckets=buckets
1972 )
Andreas Pantelopoulos6c76b942018-01-22 10:03:02 -08001973 logging.debug("Adding MPLS Swap group %02x, label %d", mpls_group_id, label)
Pierb5da4c92016-09-21 11:23:35 -07001974 ctrl.message_send(request)
1975
1976 return mpls_group_id, request
1977
Pierbbdf3782016-08-22 17:58:26 -07001978def add_mpls_label_group(ctrl, subtype, index, ref_gid,
macauley_cheng67da9262015-08-31 15:18:41 +08001979 lmep_id=-1,
1980 qos_index=-1,
1981 push_l2_header=False,
1982 push_vlan=False,
1983 push_mpls_header=False,
1984 push_cw=False,
1985 set_mpls_label=None,
1986 set_bos=None,
1987 set_tc=None,
1988 set_tc_from_table=False,
1989 cpy_tc_outward=False,
1990 set_ttl=None,
1991 cpy_ttl_outward=False,
1992 oam_lm_tx_count=False,
Pier1e4e98e2016-10-26 14:36:05 -07001993 set_pri_from_table=False,
1994 send_barrier=False
macauley_cheng67da9262015-08-31 15:18:41 +08001995 ):
1996 """
1997 @ref_gid: only can be mpls intf group or mpls tunnel label 1/2 group
Pierbbdf3782016-08-22 17:58:26 -07001998 """
macauley_cheng67da9262015-08-31 15:18:41 +08001999 action=[]
2000
2001 if push_vlan== True:
2002 action.append(ofp.action.push_vlan(0x8100))
2003 if push_mpls_header== True:
2004 action.append(ofp.action.push_mpls(0x8847))
2005 if set_mpls_label != None:
2006 action.append(ofp.action.set_field(ofp.oxm.mpls_label(set_mpls_label)))
2007 if set_bos != None:
2008 action.append(ofp.action.set_field(ofp.oxm.mpls_bos(set_bos)))
2009 if set_tc != None:
2010 assert(set_tc_from_table==False)
2011 action.append(ofp.action.set_field(ofp.oxm.mpls_tc(set_tc)))
2012 if set_ttl != None:
Pierbbdf3782016-08-22 17:58:26 -07002013 action.append(ofp.action.set_mpls_ttl(set_ttl))
macauley_cheng67da9262015-08-31 15:18:41 +08002014 if cpy_ttl_outward == True:
Pierbbdf3782016-08-22 17:58:26 -07002015 action.append(ofp.action.copy_ttl_out())
macauley_cheng67da9262015-08-31 15:18:41 +08002016 """
2017 ofdpa experimenter
Pierbbdf3782016-08-22 17:58:26 -07002018 """
macauley_cheng67da9262015-08-31 15:18:41 +08002019 if push_l2_header== True:
Pierbbdf3782016-08-22 17:58:26 -07002020 action.append(ofp.action.ofdpa_push_l2_header())
macauley_cheng67da9262015-08-31 15:18:41 +08002021 if set_tc_from_table== True:
2022 assert(qos_index>=0)
2023 assert(set_tc == None)
Pierbbdf3782016-08-22 17:58:26 -07002024 action.append(ofp.action.ofdpa_set_tc_from_table(qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08002025 if cpy_tc_outward == True:
Pierbbdf3782016-08-22 17:58:26 -07002026 action.append(ofp.action.ofdpa_copy_tc_out())
macauley_cheng67da9262015-08-31 15:18:41 +08002027 if oam_lm_tx_count == True:
Pierbbdf3782016-08-22 17:58:26 -07002028 assert(qos_index>=0 and lmep_id>=0)
2029 action.append(ofp.action.ofdpa_oam_lm_tx_count(lmep_id, qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08002030 if set_pri_from_table == True:
Pierbbdf3782016-08-22 17:58:26 -07002031 assert(qos_index>=0)
2032 action.append(ofp.action.ofdpa_set_qos_from_table(qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08002033 if push_cw == True:
2034 action.append(ofp.action.ofdpa_push_cw())
Pierbbdf3782016-08-22 17:58:26 -07002035
2036 action.append(ofp.action.group(ref_gid))
macauley_cheng67da9262015-08-31 15:18:41 +08002037 buckets = [ofp.bucket(actions=action)]
Pierbbdf3782016-08-22 17:58:26 -07002038
macauley_cheng67da9262015-08-31 15:18:41 +08002039 mpls_group_id = encode_mpls_label_group_id(subtype, index)
2040 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
2041 group_id=mpls_group_id,
2042 buckets=buckets
2043 )
2044 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -07002045
Pier1e4e98e2016-10-26 14:36:05 -07002046 if send_barrier:
2047 do_barrier(ctrl)
2048
Pierbbdf3782016-08-22 17:58:26 -07002049 return mpls_group_id, request
2050
Saurav Das5d1473d2018-07-12 11:02:56 -07002051def 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 -07002052 """
2053 Only action is Group, which must indicate one of:
2054 MPLS L2 VPN Label or Fast Failover Protection Group.
2055 ref_gid contains this information
2056 """
Piercf76e802016-09-19 20:16:35 -07002057
2058 match = ofp.match()
Pier23784aa2016-09-19 20:08:21 -07002059
Pier265ad5f2017-02-28 17:46:28 +01002060 write_actions = []
2061 write_actions.append(ofp.action.group(ref_gid))
2062 apply_actions = []
Piercf76e802016-09-19 20:16:35 -07002063
Saurav Das5d1473d2018-07-12 11:02:56 -07002064 if goto==MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE:
2065 tunnel_id = tunnel_index + ofp.oxm.TUNNEL_ID_BASE
2066 match.oxm_list.append(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port))
2067 match.oxm_list.append(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE))
2068 assert(qos_index>=0)
2069 apply_actions.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_QOS_INDEX, value=qos_index)))
2070
2071 request = ofp.message.flow_add(
Piercf76e802016-09-19 20:16:35 -07002072 table_id=MPLS_L2_PORT_FLOW_TABLE,
2073 cookie=42,
2074 match=match,
2075 instructions=[
Pier265ad5f2017-02-28 17:46:28 +01002076 ofp.instruction.apply_actions(actions=apply_actions),
2077 ofp.instruction.write_actions(actions=write_actions),
Saurav Das5d1473d2018-07-12 11:02:56 -07002078 ofp.instruction.goto_table(goto)
Pier265ad5f2017-02-28 17:46:28 +01002079 ],
Piercf76e802016-09-19 20:16:35 -07002080 buffer_id=ofp.OFP_NO_BUFFER,
2081 priority=1)
Saurav Das5d1473d2018-07-12 11:02:56 -07002082 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)
2083 ctrl.message_send(request)
Piercf76e802016-09-19 20:16:35 -07002084
Saurav Das5d1473d2018-07-12 11:02:56 -07002085 if goto==ACL_FLOW_TABLE:
2086 tunnel_id = tunnel_index + ofp.oxm.TUNNEL_ID_BASE
2087 match.oxm_list.append(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port))
2088 match.oxm_list.append(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE_CROSS_CONNECT))
2089 request = ofp.message.flow_add(
2090 table_id=MPLS_L2_PORT_FLOW_TABLE,
2091 cookie=42,
2092 match=match,
2093 instructions=[
2094 ofp.instruction.apply_actions(actions=apply_actions),
2095 ofp.instruction.write_actions(actions=write_actions),
2096 ofp.instruction.goto_table(goto)
2097 ],
2098 buffer_id=ofp.OFP_NO_BUFFER,
2099 priority=1)
2100 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)
2101 ctrl.message_send(request)
2102
2103 return request
Piercf76e802016-09-19 20:16:35 -07002104
Pierbbdf3782016-08-22 17:58:26 -07002105def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids,
2106 watch_port=None,
Pier265ad5f2017-02-28 17:46:28 +01002107 watch_group=ofp.OFPP_ANY,
2108 push_vlan=None,
macauley_chengd17ce512015-08-31 17:45:51 +08002109 pop_vlan=None,
macauley_cheng67da9262015-08-31 15:18:41 +08002110 set_vid=None):
2111 assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP
Pier265ad5f2017-02-28 17:46:28 +01002112 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP
2113 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG)
macauley_chengd17ce512015-08-31 17:45:51 +08002114
macauley_cheng67da9262015-08-31 15:18:41 +08002115 buckets=[]
2116 if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP:
macauley_chengd17ce512015-08-31 17:45:51 +08002117 group_type = ofp.OFPGT_FF
macauley_cheng67da9262015-08-31 15:18:41 +08002118 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08002119 action=[]
Pierbbdf3782016-08-22 17:58:26 -07002120 action.append(ofp.action.group(gid))
macauley_chengd17ce512015-08-31 17:45:51 +08002121 buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action))
2122
macauley_cheng67da9262015-08-31 15:18:41 +08002123 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP:
2124 group_type = ofp.OFPGT_SELECT
2125 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08002126 action=[]
Pierbbdf3782016-08-22 17:58:26 -07002127 action.append(ofp.action.group(gid))
macauley_cheng67da9262015-08-31 15:18:41 +08002128 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08002129
macauley_cheng67da9262015-08-31 15:18:41 +08002130 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG:
2131 group_type = ofp.OFPGT_INDIRECT
macauley_chengd17ce512015-08-31 17:45:51 +08002132 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08002133 if set_vid!=None:
Flavio Castro91d1a552016-05-17 16:59:44 -07002134 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+set_vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08002135 if push_vlan!=None:
Pierbbdf3782016-08-22 17:58:26 -07002136 action.append(ofp.action.push_vlan(push_vlan))
macauley_cheng67da9262015-08-31 15:18:41 +08002137 if pop_vlan!=None:
Pierbbdf3782016-08-22 17:58:26 -07002138 action.append(ofp.action.pop_vlan())
2139 action.append(ofp.action.group(ref_gids[0]))
macauley_cheng67da9262015-08-31 15:18:41 +08002140 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08002141
2142 mpls_group_id = encode_mpls_forwarding_group_id(subtype, index)
macauley_cheng67da9262015-08-31 15:18:41 +08002143 request = ofp.message.group_add(group_type=group_type,
macauley_cheng67da9262015-08-31 15:18:41 +08002144 group_id=mpls_group_id,
2145 buckets=buckets
2146 )
2147 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -07002148 return mpls_group_id, request
macauley_chengd17ce512015-08-31 17:45:51 +08002149
Jonghwan Hyunff0dfd52018-03-20 15:04:35 -07002150def add_one_egress_vlan_tpid_table_flow(ctrl, of_port):
2151 # Used for changing ethertype of outer vlan header to 0x88a8
2152
2153 match = ofp.match()
2154 match.oxm_list.append(ofp.oxm.exp4ByteValue(ofp.oxm.OFDPA_EXP_TYPE_ACTSET_OUTPUT, of_port, ONF_EXPERIMENTER_ID))
2155 match.oxm_list.append(ofp.oxm.vlan_vid_masked(ofp.OFPVID_PRESENT, ofp.OFPVID_PRESENT))
2156
2157 actions = []
2158 actions.append(ofp.action.copy_field(
2159 12, 0, 0, ['\x80\x00\x0c\x02', ofp.oxm.exp4ByteReg(oxm_field = 1).pack()])) # VLAN_VID, PACKET_REG(1)
2160 actions.append(ofp.action.pop_vlan())
2161 actions.append(ofp.action.push_vlan(0x88a8))
2162 actions.append(ofp.action.copy_field(
2163 12, 0, 0, [ofp.oxm.exp4ByteReg(oxm_field = 1).pack(), '\x80\x00\x0c\x02'])) # PACKET_REG(1), VLAN_VID
2164
2165 request = ofp.message.flow_add(
2166 table_id=EGRESS_TPID_FLOW_TABLE,
2167 cookie=42,
2168 match=match,
2169 instructions=[
2170 ofp.instruction.apply_actions(
2171 actions=actions
2172 ),
2173 ],
2174 priority=0)
2175
2176 ctrl.message_send(request)
2177
2178 return
macauley_chengd17ce512015-08-31 17:45:51 +08002179
macauley_cheng67da9262015-08-31 15:18:41 +08002180"""
Piercf76e802016-09-19 20:16:35 -07002181display
Pierbbdf3782016-08-22 17:58:26 -07002182"""
macauleydbff3272015-07-30 14:07:16 +08002183def print_current_table_flow_stat(ctrl, table_id=0xff):
2184 stat_req=ofp.message.flow_stats_request()
2185 response, pkt = ctrl.transact(stat_req)
2186 if response == None:
2187 print "no response"
2188 return None
2189 print len(response.entries)
2190 for obj in response.entries:
2191 print "match ", obj.match
2192 print "cookie", obj.cookie
2193 print "priority", obj.priority
2194 print "idle_timeout", obj.idle_timeout
2195 print "hard_timeout", obj.hard_timeout
2196 #obj.actions
Flavio Castro167f5bd2015-12-02 19:33:53 -05002197 print "packet count: %lx"%obj.packet_count
Saurav Dasb4b841e2018-08-17 15:51:56 -07002198