blob: 75a1df5d3d7a7184f0bf4e766d720cf7d6fcd6d9 [file] [log] [blame]
macauley97557232015-07-16 17:28:07 +08001import logging
2
3from oftest import config
4import oftest.base_tests as base_tests
5import ofp
6import time
7from oftest.testutils import *
8
macauleyfddc4662015-07-27 17:40:30 +08009from ncclient import manager
10import ncclient
11
macauley97557232015-07-16 17:28:07 +080012OFDPA_GROUP_TYPE_SHIFT=28
13OFDPA_VLAN_ID_SHIFT =16
macauleyfddc4662015-07-27 17:40:30 +080014OFDPA_TUNNEL_ID_SHIFT =12
15OFDPA_TUNNEL_SUBTYPE_SHIFT=10
macauley97557232015-07-16 17:28:07 +080016
17#VLAN_TABLE_FLAGS
18VLAN_TABLE_FLAG_ONLY_UNTAG=1
19VLAN_TABLE_FLAG_ONLY_TAG =2
20VLAN_TABLE_FLAG_ONLY_BOTH =3
Piere1308762016-09-12 15:29:56 -070021VLAN_TABLE_FLAG_ONLY_STACKED=5
22VLAN_TABLE_FLAG_PRIORITY=6
23VLAN_TABLE_FLAG_ONLY_UNTAG_PRIORITY=7
macauley97557232015-07-16 17:28:07 +080024
macauleye8b140e2015-08-03 13:35:45 +080025PORT_FLOW_TABLE=0
26VLAN_FLOW_TABLE=10
Piere1308762016-09-12 15:29:56 -070027VLAN_1_FLOW_TABLE=11
Piercf76e802016-09-19 20:16:35 -070028MPLS_L2_PORT_FLOW_TABLE=13
29MPLS_L2_PORT_DSCP_TRUST_FLOW_TABLE=15
30MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE=16
macauleye8b140e2015-08-03 13:35:45 +080031TERMINATION_FLOW_TABLE=20
Piercf76e802016-09-19 20:16:35 -070032MPLS_TYPE_FLOW_TABLE=29
macauleye8b140e2015-08-03 13:35:45 +080033UCAST_ROUTING_FLOW_TABLE=30
34MCAST_ROUTING_FLOW_TABLE=40
35BRIDGE_FLOW_TABLE=50
36ACL_FLOW_TABLE=60
37
38def convertIP4toStr(ip_addr):
39 a=(ip_addr&0xff000000)>>24
40 b=(ip_addr&0x00ff0000)>>16
41 c=(ip_addr&0x0000ff00)>>8
42 d=(ip_addr&0x000000ff)
43 return str(a)+"."+str(b)+"."+str(c)+"."+str(d)
44
45def convertMACtoStr(mac):
46 if not isinstance(mac, list):
47 assert(0)
48
49 return ':'.join(['%02X' % x for x in mac])
50
macauley7f89d962015-08-06 18:13:48 +080051def getSwitchCpuMACFromDPID(dpid):
52 str_datapath_id_f= "{:016x}".format(dpid)
53 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
54 switch_cpu_mac_str=str_datapath_id[6:]
55 switch_cpu_mac = switch_cpu_mac_str.split(":")
56 switch_cpu_mac=[int(switch_cpu_mac[i],16) for i in range(0, len(switch_cpu_mac))]
57
58 return switch_cpu_mac_str, switch_cpu_mac
Pierbbdf3782016-08-22 17:58:26 -070059
macauley_cheng67da9262015-08-31 15:18:41 +080060def DumpGroup(stats, verify_group_stats, always_show=True):
61 if(len(stats) > len(verify_group_stats)):
62 min_len = len(verify_group_stats)
63 print "Stats Len is not the same, stats>verify_group_stats"
64 if(len(stats)< len(verify_group_stats)):
Pierbbdf3782016-08-22 17:58:26 -070065 min_len = len(stats)
macauley_cheng67da9262015-08-31 15:18:41 +080066 print "Stats Len is not the same, stats<verify_group_stats"
Pierbbdf3782016-08-22 17:58:26 -070067 else:
macauley_cheng67da9262015-08-31 15:18:41 +080068 min_len = len(stats)
macauleye8b140e2015-08-03 13:35:45 +080069
macauley_cheng67da9262015-08-31 15:18:41 +080070 print "\r\n"
71 for i in range(min_len):
72 gs = stats[i]
Pierbbdf3782016-08-22 17:58:26 -070073 gv = verify_group_stats[i]
macauley_cheng67da9262015-08-31 15:18:41 +080074 print "FromSwtich:(GID=%lx, TYPE=%lx)\r\nVerify :(GID=%lx, TYPE=%lx)"%(gs.group_id, gs.group_type, gv.group_id, gv.group_type)
75 if(len(gs.buckets) != len(gv.buckets)):
76 print "buckets len is not the same gs %lx, gv %lx",(len(gs.buckets), len(gv.buckets))
77
78 for j in range(len(gs.buckets)):
79 b1=gs.buckets[j]
Pierbbdf3782016-08-22 17:58:26 -070080 b2=gv.buckets[j]
macauley_cheng67da9262015-08-31 15:18:41 +080081 if(len(b1.actions) != len(b2.actions)):
82 print "action len is not the same"
83
84 for k in range(len(b1.actions)):
85 a1=b1.actions[k]
86 a2=b2.actions[k]
87 if(always_show == True):
88 print "a1:"+a1.show()
Pierbbdf3782016-08-22 17:58:26 -070089 print "a2:"+a2.show()
macauley_cheng67da9262015-08-31 15:18:41 +080090
91def AssertGroup(self, stats, verify_group_stats):
92 self.assertTrue(len(stats) ==len(verify_group_stats), "stats len is not the same")
93
94 for i in range(len(stats)):
95 gs = stats[i]
Pierbbdf3782016-08-22 17:58:26 -070096 gv = verify_group_stats[i]
macauley_cheng67da9262015-08-31 15:18:41 +080097 self.assertTrue(len(gs.buckets) == len(gv.buckets), "buckets len is not the same")
98
99 for j in range(len(gs.buckets)):
100 b1=gs.buckets[j]
Pierbbdf3782016-08-22 17:58:26 -0700101 b2=gv.buckets[j]
macauley_cheng67da9262015-08-31 15:18:41 +0800102 self.assertTrue(len(b1.actions) == len(b2.actions), "action len is not the same")
103
104 for k in range(len(b1.actions)):
105 a1=b1.actions[k]
106 a2=b2.actions[k]
107 self.assertEquals(a1, a2, "action is not the same")
Pierbbdf3782016-08-22 17:58:26 -0700108
macauley97557232015-07-16 17:28:07 +0800109def encode_l2_interface_group_id(vlan, id):
110 return id + (vlan << OFDPA_VLAN_ID_SHIFT)
111
112def encode_l2_rewrite_group_id(id):
113 return id + (1 << OFDPA_GROUP_TYPE_SHIFT)
114
115def encode_l3_unicast_group_id(id):
116 return id + (2 << OFDPA_GROUP_TYPE_SHIFT)
117
118def encode_l2_mcast_group_id(vlan, id):
119 return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (3 << OFDPA_GROUP_TYPE_SHIFT)
120
121def encode_l2_flood_group_id(vlan, id):
122 return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (4 << OFDPA_GROUP_TYPE_SHIFT)
Pierbbdf3782016-08-22 17:58:26 -0700123
macauley97557232015-07-16 17:28:07 +0800124def encode_l3_interface_group_id(id):
125 return id + (5 << OFDPA_GROUP_TYPE_SHIFT)
126
127def encode_l3_mcast_group_id(vlan, id):
128 return id + (vlan << OFDPA_VLAN_ID_SHIFT)+(6 << OFDPA_GROUP_TYPE_SHIFT)
129
130def encode_l3_ecmp_group_id(id):
131 return id + (7 << OFDPA_GROUP_TYPE_SHIFT)
132
Flavio Castro91d1a552016-05-17 16:59:44 -0700133def encode_l2_unfiltered_group_id(id):
134 return id + (11 << OFDPA_GROUP_TYPE_SHIFT)
135
macauleyfddc4662015-07-27 17:40:30 +0800136def encode_l2_overlay_group_id(tunnel_id, subtype, index):
137 tunnel_id=tunnel_id&0xffff #16 bits
138 subtype = subtype&3 #2 bits
139 index = index & 0x3f #10 bits
140 return index + (tunnel_id << OFDPA_TUNNEL_ID_SHIFT)+ (subtype<<OFDPA_TUNNEL_SUBTYPE_SHIFT)+(8 << OFDPA_GROUP_TYPE_SHIFT)
macauley97557232015-07-16 17:28:07 +0800141
Flavio Castro91d1a552016-05-17 16:59:44 -0700142def add_l2_unfiltered_group(ctrl, ports, send_barrier=False):
143 # group table
144 # set up untag groups for each port
145 group_id_list=[]
146 msgs=[]
147 for of_port in ports:
148 # do stuff
149 group_id = encode_l2_unfiltered_group_id(of_port)
150 group_id_list.append(group_id)
151 actions = [ofp.action.output(of_port)]
152 actions.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=24, value=1)))
153
154 buckets = [ofp.bucket(actions=actions)]
155 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
156 group_id=group_id,
157 buckets=buckets
158 )
159 ctrl.message_send(request)
160 msgs.append(request)
161
162 if send_barrier:
163 do_barrier(ctrl)
164
165 return group_id_list, msgs
166
Flavio Castrod4c44d12015-12-08 14:44:18 -0500167def add_l2_interface_group(ctrl, ports, vlan_id=1, is_tagged=False, send_barrier=False):
macauley97557232015-07-16 17:28:07 +0800168 # group table
169 # set up untag groups for each port
macauley41904ed2015-07-16 17:38:35 +0800170 group_id_list=[]
macauley15909e72015-07-17 15:58:57 +0800171 msgs=[]
macauley97557232015-07-16 17:28:07 +0800172 for of_port in ports:
173 # do stuff
174 group_id = encode_l2_interface_group_id(vlan_id, of_port)
macauley41904ed2015-07-16 17:38:35 +0800175 group_id_list.append(group_id)
macauley97557232015-07-16 17:28:07 +0800176 if is_tagged:
177 actions = [
178 ofp.action.output(of_port),
Pierbbdf3782016-08-22 17:58:26 -0700179 ]
macauley97557232015-07-16 17:28:07 +0800180 else:
181 actions = [
182 ofp.action.pop_vlan(),
183 ofp.action.output(of_port),
184 ]
185
186 buckets = [
187 ofp.bucket(actions=actions),
188 ]
189
190 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
191 group_id=group_id,
192 buckets=buckets
193 )
194 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800195 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800196
197 if send_barrier:
198 do_barrier(ctrl)
Pierbbdf3782016-08-22 17:58:26 -0700199
macauley15909e72015-07-17 15:58:57 +0800200 return group_id_list, msgs
macauley97557232015-07-16 17:28:07 +0800201
Flavio Castrod4c44d12015-12-08 14:44:18 -0500202def add_one_l2_interface_group(ctrl, port, vlan_id=1, is_tagged=False, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800203 # group table
204 # set up untag groups for each port
205 group_id = encode_l2_interface_group_id(vlan_id, port)
206
207 if is_tagged:
208 actions = [
209 ofp.action.output(port),
Pierbbdf3782016-08-22 17:58:26 -0700210 ]
macauley0f91a3e2015-07-17 18:09:59 +0800211 else:
212 actions = [
213 ofp.action.pop_vlan(),
214 ofp.action.output(port),
215 ]
216
217 buckets = [
218 ofp.bucket(actions=actions),
219 ]
220
221 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
222 group_id=group_id,
223 buckets=buckets
224 )
225 ctrl.message_send(request)
226
227 if send_barrier:
228 do_barrier(ctrl)
Pierbbdf3782016-08-22 17:58:26 -0700229
macauley0f91a3e2015-07-17 18:09:59 +0800230 return group_id, request
Pierbbdf3782016-08-22 17:58:26 -0700231
macauley97557232015-07-16 17:28:07 +0800232def add_l2_mcast_group(ctrl, ports, vlanid, mcast_grp_index):
233 buckets=[]
234 for of_port in ports:
235 group_id = encode_l2_interface_group_id(vlanid, of_port)
236 action=[ofp.action.group(group_id)]
237 buckets.append(ofp.bucket(actions=action))
238
239 group_id =encode_l2_mcast_group_id(vlanid, mcast_grp_index)
240 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
241 group_id=group_id,
242 buckets=buckets
243 )
244 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800245 return request
macauley97557232015-07-16 17:28:07 +0800246
macauley15909e72015-07-17 15:58:57 +0800247def add_l2_flood_group(ctrl, ports, vlanid, id):
248 buckets=[]
249 for of_port in ports:
250 group_id = encode_l2_interface_group_id(vlanid, of_port)
251 action=[ofp.action.group(group_id)]
252 buckets.append(ofp.bucket(actions=action))
macauley97557232015-07-16 17:28:07 +0800253
macauley15909e72015-07-17 15:58:57 +0800254 group_id =encode_l2_flood_group_id(vlanid, id)
255 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
256 group_id=group_id,
257 buckets=buckets
258 )
259 ctrl.message_send(request)
260 return request
261
Flavio Castroa7162bb2016-07-25 17:30:30 -0700262def mod_l2_flood_group(ctrl, ports, vlanid, id):
263 buckets=[]
264 for of_port in ports:
265 group_id = encode_l2_interface_group_id(vlanid, of_port)
266 action=[ofp.action.group(group_id)]
267 buckets.append(ofp.bucket(actions=action))
268
269 group_id =encode_l2_flood_group_id(vlanid, id)
270 request = ofp.message.group_modify(group_type=ofp.OFPGT_ALL,
271 group_id=group_id,
272 buckets=buckets
273 )
274 ctrl.message_send(request)
275 return request
276
277
macauley15909e72015-07-17 15:58:57 +0800278def add_l2_rewrite_group(ctrl, port, vlanid, id, src_mac, dst_mac):
279 group_id = encode_l2_interface_group_id(vlanid, port)
280
281 action=[]
282 if src_mac is not None:
283 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
284
285 if dst_mac is not None:
286 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
287
Flavio Castro91d1a552016-05-17 16:59:44 -0700288 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
Pierbbdf3782016-08-22 17:58:26 -0700289
macauley15909e72015-07-17 15:58:57 +0800290 action.append(ofp.action.group(group_id))
Pierbbdf3782016-08-22 17:58:26 -0700291
macauley15909e72015-07-17 15:58:57 +0800292 buckets = [ofp.bucket(actions=action)]
293
294 group_id =encode_l2_rewrite_group_id(id)
295 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
296 group_id=group_id,
297 buckets=buckets
298 )
299 ctrl.message_send(request)
300 return request
Pierbbdf3782016-08-22 17:58:26 -0700301
macauley15909e72015-07-17 15:58:57 +0800302def add_l3_unicast_group(ctrl, port, vlanid, id, src_mac, dst_mac):
303 group_id = encode_l2_interface_group_id(vlanid, port)
304
305 action=[]
306 if src_mac is not None:
307 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
308
309 if dst_mac is not None:
310 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
311
Flavio Castroaf2b4502016-02-02 17:41:32 -0500312 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
Pierbbdf3782016-08-22 17:58:26 -0700313
macauley15909e72015-07-17 15:58:57 +0800314 action.append(ofp.action.group(group_id))
Pierbbdf3782016-08-22 17:58:26 -0700315
macauley15909e72015-07-17 15:58:57 +0800316 buckets = [ofp.bucket(actions=action)]
317
318 group_id =encode_l3_unicast_group_id(id)
319 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
320 group_id=group_id,
321 buckets=buckets
322 )
323 ctrl.message_send(request)
324 return request
Pierbbdf3782016-08-22 17:58:26 -0700325
macauley15909e72015-07-17 15:58:57 +0800326def add_l3_interface_group(ctrl, port, vlanid, id, src_mac):
327 group_id = encode_l2_interface_group_id(vlanid, port)
328
329 action=[]
330 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
Pierbbdf3782016-08-22 17:58:26 -0700331 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
macauley15909e72015-07-17 15:58:57 +0800332 action.append(ofp.action.group(group_id))
Pierbbdf3782016-08-22 17:58:26 -0700333
macauley15909e72015-07-17 15:58:57 +0800334 buckets = [ofp.bucket(actions=action)]
335
336 group_id =encode_l3_interface_group_id(id)
337 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
338 group_id=group_id,
339 buckets=buckets
340 )
341 ctrl.message_send(request)
342 return request
343
344def add_l3_ecmp_group(ctrl, id, l3_ucast_groups):
345 buckets=[]
346 for group in l3_ucast_groups:
347 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
348
349 group_id =encode_l3_ecmp_group_id(id)
350 request = ofp.message.group_add(group_type=ofp.OFPGT_SELECT,
351 group_id=group_id,
352 buckets=buckets
353 )
354 ctrl.message_send(request)
355 return request
Flavio Castroa7162bb2016-07-25 17:30:30 -0700356
357def mod_l3_ecmp_group(ctrl, id, l3_ucast_groups):
358 buckets=[]
359 for group in l3_ucast_groups:
360 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
361
362 group_id =encode_l3_ecmp_group_id(id)
363 request = ofp.message.group_modify(group_type=ofp.OFPGT_SELECT,
364 group_id=group_id,
365 buckets=buckets
366 )
367 ctrl.message_send(request)
368 return request
369
macauley15909e72015-07-17 15:58:57 +0800370def add_l3_mcast_group(ctrl, vid, mcast_group_id, groups_on_buckets):
371 buckets=[]
372 for group in groups_on_buckets:
373 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
Pierbbdf3782016-08-22 17:58:26 -0700374
macauley15909e72015-07-17 15:58:57 +0800375 group_id =encode_l3_mcast_group_id(vid, mcast_group_id)
376 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
377 group_id=group_id,
378 buckets=buckets
379 )
380 ctrl.message_send(request)
381 return request
macauleyfddc4662015-07-27 17:40:30 +0800382
383def add_l2_overlay_flood_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
384 buckets=[]
385 for port in ports:
386 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
387
388 group_id=encode_l2_overlay_group_id(tunnel_id, 0, index)
389 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
390 group_id=group_id,
391 buckets=buckets
392 )
393 ctrl.message_send(request)
394 return request
395
396def add_l2_overlay_flood_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
397 buckets=[]
398 for port in ports:
399 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
400
401 group_id=encode_l2_overlay_group_id(tunnel_id, 1, index)
402 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
403 group_id=group_id,
404 buckets=buckets
405 )
406 ctrl.message_send(request)
407 return request
408
409def add_l2_overlay_mcast_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
410 buckets=[]
411 for port in ports:
412 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
413
414 group_id=encode_l2_overlay_group_id(tunnel_id, 2, index)
415 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
416 group_id=group_id,
417 buckets=buckets
418 )
419 ctrl.message_send(request)
420 return request
421
422def add_l2_overlay_mcast_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
423 buckets=[]
424 for port in ports:
425 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
426
427 group_id=encode_l2_overlay_group_id(tunnel_id, 3, index)
428 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
429 group_id=group_id,
430 buckets=buckets
431 )
432 ctrl.message_send(request)
433 return request
Pierbbdf3782016-08-22 17:58:26 -0700434
macauleyfddc4662015-07-27 17:40:30 +0800435def add_port_table_flow(ctrl, is_overlay=True):
436 match = ofp.match()
437
438 if is_overlay == True:
439 match.oxm_list.append(ofp.oxm.in_port(0x10000))
macauleydbff3272015-07-30 14:07:16 +0800440 NEXT_TABLE=50
macauleyfddc4662015-07-27 17:40:30 +0800441 else:
442 match.oxm_list.append(ofp.oxm.in_port(0))
Pierbbdf3782016-08-22 17:58:26 -0700443 NEXT_TABLE=10
macauleyfddc4662015-07-27 17:40:30 +0800444
445 request = ofp.message.flow_add(
446 table_id=0,
447 cookie=42,
448 match=match,
449 instructions=[
macauleydbff3272015-07-30 14:07:16 +0800450 ofp.instruction.goto_table(NEXT_TABLE)
macauleyfddc4662015-07-27 17:40:30 +0800451 ],
452 priority=0)
453 logging.info("Add port table, match port %lx" % 0x10000)
454 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700455
Flavio Castrod8f8af22015-12-02 18:19:26 -0500456def pop_vlan_flow(ctrl, ports, vlan_id=1):
457 # table 10: vlan
458 # goto to table 20
459 msgs=[]
460 for of_port in ports:
461 match = ofp.match()
462 match.oxm_list.append(ofp.oxm.in_port(of_port))
463 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
464 request = ofp.message.flow_add(
465 table_id=10,
466 cookie=42,
467 match=match,
468 instructions=[
469 ofp.instruction.apply_actions(
470 actions=[
471 ofp.action.pop_vlan()
472 ]
473 ),
474 ofp.instruction.goto_table(20)
475 ],
476 priority=0)
477 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
478 ctrl.message_send(request)
479
480
481 return msgs
macauleyfddc4662015-07-27 17:40:30 +0800482
macauley97557232015-07-16 17:28:07 +0800483def add_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
484 # table 10: vlan
485 # goto to table 20
macauley15909e72015-07-17 15:58:57 +0800486 msgs=[]
macauley97557232015-07-16 17:28:07 +0800487 for of_port in ports:
Flavio Castro932014b2016-01-05 18:29:15 -0500488 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
macauley97557232015-07-16 17:28:07 +0800489 match = ofp.match()
490 match.oxm_list.append(ofp.oxm.in_port(of_port))
491 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
492 request = ofp.message.flow_add(
493 table_id=10,
494 cookie=42,
495 match=match,
496 instructions=[
Flavio Castrod8f8af22015-12-02 18:19:26 -0500497 ofp.instruction.apply_actions(
498 actions=[
499 ofp.action.pop_vlan()
500 ]
501 ),
macauley97557232015-07-16 17:28:07 +0800502 ofp.instruction.goto_table(20)
503 ],
504 priority=0)
505 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
506 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700507
macauley97557232015-07-16 17:28:07 +0800508 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
509 match = ofp.match()
510 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500511 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
macauley97557232015-07-16 17:28:07 +0800512 request = ofp.message.flow_add(
513 table_id=10,
514 cookie=42,
515 match=match,
516 instructions=[
517 ofp.instruction.apply_actions(
518 actions=[
Flavio Castroaf2b4502016-02-02 17:41:32 -0500519 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
macauley97557232015-07-16 17:28:07 +0800520 ]
521 ),
522 ofp.instruction.goto_table(20)
523 ],
524 priority=0)
525 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
526 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800527 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800528
Flavio Castro932014b2016-01-05 18:29:15 -0500529 if (flag == 4) :
530 match = ofp.match()
531 match.oxm_list.append(ofp.oxm.in_port(of_port))
532 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1fff))
533 request = ofp.message.flow_add(
534 table_id=10,
535 cookie=42,
536 match=match,
537 instructions=[
538 ofp.instruction.apply_actions(
539 actions=[
Flavio Castroaf2b4502016-02-02 17:41:32 -0500540 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
Flavio Castro932014b2016-01-05 18:29:15 -0500541 ]
542 ),
543 ofp.instruction.goto_table(20)
544 ],
545 priority=0)
546 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
547 ctrl.message_send(request)
548 msgs.append(request)
549
macauley97557232015-07-16 17:28:07 +0800550 if send_barrier:
551 do_barrier(ctrl)
552
macauley15909e72015-07-17 15:58:57 +0800553 return msgs
Pierbbdf3782016-08-22 17:58:26 -0700554
macauley_cheng6e6a6122015-11-16 14:19:18 +0800555def del_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
556 # table 10: vlan
557 # goto to table 20
558 msgs=[]
559 for of_port in ports:
560 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
561 match = ofp.match()
562 match.oxm_list.append(ofp.oxm.in_port(of_port))
563 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
564 request = ofp.message.flow_delete(
565 table_id=10,
566 cookie=42,
567 match=match,
568 priority=0)
569 logging.info("Del vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
570 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800571
macauley_cheng6e6a6122015-11-16 14:19:18 +0800572 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
573 match = ofp.match()
574 match.oxm_list.append(ofp.oxm.in_port(of_port))
575 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0xfff))
576 request = ofp.message.flow_delete(
577 table_id=10,
578 cookie=42,
579 match=match,
580 priority=0)
581 logging.info("Del vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
582 ctrl.message_send(request)
583 msgs.append(request)
584
585 if send_barrier:
586 do_barrier(ctrl)
587
588 return msgs
Pierbbdf3782016-08-22 17:58:26 -0700589
macauley_cheng6b311612015-09-04 11:32:27 +0800590def add_vlan_table_flow_pvid(ctrl, in_port, match_vid=None, pvid=1, send_barrier=False):
591 """it will tag pack as untagged packet wether it has tagg or not"""
592 match = ofp.match()
593 match.oxm_list.append(ofp.oxm.in_port(in_port))
594 actions=[]
595 if match_vid == None:
Pierbbdf3782016-08-22 17:58:26 -0700596 match.oxm_list.append(ofp.oxm.vlan_vid(0))
macauley_cheng6b311612015-09-04 11:32:27 +0800597 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
598 goto_table=20
599 else:
600 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+match_vid, 0x1fff))
601 actions.append(ofp.action.push_vlan(0x8100))
Pierbbdf3782016-08-22 17:58:26 -0700602 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
macauley_cheng6b311612015-09-04 11:32:27 +0800603 goto_table=20
Pierbbdf3782016-08-22 17:58:26 -0700604
macauley_cheng6b311612015-09-04 11:32:27 +0800605 request = ofp.message.flow_add(
606 table_id=10,
607 cookie=42,
608 match=match,
609 instructions=[
610 ofp.instruction.apply_actions(actions=actions)
611 ,ofp.instruction.goto_table(goto_table)
612 ],
613 priority=0)
614 logging.info("Add PVID %d on port %d and go to table %ld" %( pvid, in_port, goto_table))
Pierbbdf3782016-08-22 17:58:26 -0700615 ctrl.message_send(request)
616
macauley_cheng6b311612015-09-04 11:32:27 +0800617 if send_barrier:
618 do_barrier(ctrl)
619
620def add_vlan_table_flow_allow_all_vlan(ctrl, in_port, send_barrier=False):
621 """it st flow allow all vlan tag on this port"""
622 match = ofp.match()
623 match.oxm_list.append(ofp.oxm.in_port(in_port))
624 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1000))
625 request = ofp.message.flow_add(
626 table_id=10,
627 cookie=42,
628 match=match,
629 instructions=[
Pierbbdf3782016-08-22 17:58:26 -0700630 ofp.instruction.goto_table(20)
macauley_cheng6b311612015-09-04 11:32:27 +0800631 ],
632 priority=0)
633 logging.info("Add allow all vlan on port %d " %(in_port))
Pierbbdf3782016-08-22 17:58:26 -0700634 ctrl.message_send(request)
macauley_cheng6b311612015-09-04 11:32:27 +0800635
Pier7b031af2016-08-25 15:00:22 -0700636def 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):
637 # Install a flow for VLAN translation
638 # in VLAN table.
639 # table 10: vlan
640 # goto to table 20
641 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
642 match = ofp.match()
643 match.oxm_list.append(ofp.oxm.in_port(of_port))
644 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
645
646 actions=[]
647 if vrf!=0:
648 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
649 if new_vlan_id != -1:
650 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_vlan_id)))
651
652 request = ofp.message.flow_add(
653 table_id=10,
654 cookie=42,
655 match=match,
656 instructions=[
657 ofp.instruction.apply_actions(
658 actions=actions
659 ),
660 ofp.instruction.goto_table(20)
661 ],
662 priority=0)
663 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
664 ctrl.message_send(request)
665
Piere1308762016-09-12 15:29:56 -0700666def 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):
macauley0f91a3e2015-07-17 18:09:59 +0800667 # table 10: vlan
668 # goto to table 20
Flavio Castro932014b2016-01-05 18:29:15 -0500669 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
macauley0f91a3e2015-07-17 18:09:59 +0800670 match = ofp.match()
671 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500672 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
macauley7f89d962015-08-06 18:13:48 +0800673
674 actions=[]
675 if vrf!=0:
676 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
Pierb5da4c92016-09-21 11:23:35 -0700677 if mpls_type!=None:
678 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=mpls_type)))
Pierbbdf3782016-08-22 17:58:26 -0700679
Flavio Castro6d498522015-12-15 14:05:04 -0500680 #actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id)))
macauley7f89d962015-08-06 18:13:48 +0800681
macauley0f91a3e2015-07-17 18:09:59 +0800682 request = ofp.message.flow_add(
683 table_id=10,
684 cookie=42,
685 match=match,
686 instructions=[
macauley53d90fe2015-08-04 17:34:22 +0800687 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800688 actions=actions
macauley53d90fe2015-08-04 17:34:22 +0800689 ),
690 ofp.instruction.goto_table(20)
macauley0f91a3e2015-07-17 18:09:59 +0800691 ],
692 priority=0)
693 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
694 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700695
macauley0f91a3e2015-07-17 18:09:59 +0800696 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
697 match = ofp.match()
698 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500699 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
Pierbbdf3782016-08-22 17:58:26 -0700700
macauley7f89d962015-08-06 18:13:48 +0800701 actions=[]
702 if vrf!=0:
703 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
Pierbbdf3782016-08-22 17:58:26 -0700704
Flavio Castro91d1a552016-05-17 16:59:44 -0700705 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Pierbbdf3782016-08-22 17:58:26 -0700706
macauley0f91a3e2015-07-17 18:09:59 +0800707 request = ofp.message.flow_add(
708 table_id=10,
709 cookie=42,
710 match=match,
711 instructions=[
712 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800713 actions=actions
macauley0f91a3e2015-07-17 18:09:59 +0800714 ),
715 ofp.instruction.goto_table(20)
716 ],
717 priority=0)
718 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
719 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700720
Flavio Castro932014b2016-01-05 18:29:15 -0500721 if (flag == 4) :
722 match = ofp.match()
723 match.oxm_list.append(ofp.oxm.in_port(of_port))
724 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000,0x1fff))
725
726 actions=[]
727 if vrf!=0:
728 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
729
Flavio Castro91d1a552016-05-17 16:59:44 -0700730 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Flavio Castro932014b2016-01-05 18:29:15 -0500731
732 request = ofp.message.flow_add(
733 table_id=10,
734 cookie=42,
735 match=match,
736 instructions=[
737 ofp.instruction.apply_actions(
738 actions=actions
739 ),
740 ofp.instruction.goto_table(20)
741 ],
742 priority=0)
743 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
744 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800745
Piere1308762016-09-12 15:29:56 -0700746 if (flag == VLAN_TABLE_FLAG_ONLY_STACKED):
747 # This flag is meant to managed stacked vlan packtes
748 # Matches on outer VLAN_ID, set OVID with outer VLAN.
749 # Finally expose inner VLAN_ID with a pop action and
750 # goto VLAN_1_FLOW_TABLE
751 match = ofp.match()
752 match.oxm_list.append(ofp.oxm.in_port(of_port))
753 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
754
755 actions=[]
756 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_OVID, value=0x1000+vlan_id)))
757 actions.append(ofp.action.pop_vlan())
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(VLAN_1_FLOW_TABLE)
768 ],
769 priority=0)
770 logging.info("Add vlan %d tagged packets on port %d and go to table %d" %( vlan_id, of_port, VLAN_1_FLOW_TABLE))
771 ctrl.message_send(request)
772
773 if (flag == VLAN_TABLE_FLAG_PRIORITY) :
774 match = ofp.match()
775 match.oxm_list.append(ofp.oxm.in_port(of_port))
776 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1fff))
777 request = ofp.message.flow_add(
778 table_id=10,
779 cookie=42,
780 match=match,
781 instructions=[
782 ofp.instruction.apply_actions(
783 actions=[
784 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)),
785 ofp.action.push_vlan(0x8100),
786 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+out_vlan_id)),
787 ]
788 ),
789 ofp.instruction.goto_table(20)
790 ],
791 priority=0)
792 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
793 ctrl.message_send(request)
794
795 if send_barrier:
796 do_barrier(ctrl)
797
798 return request
799
Piercf76e802016-09-19 20:16:35 -0700800def 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):
801 # table 10: vlan
802 # goto to table 13
803 if flag == VLAN_TABLE_FLAG_ONLY_TAG:
804 match = ofp.match()
805 match.oxm_list.append(ofp.oxm.in_port(of_port))
806 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id, 0x1fff))
807
808 actions=[]
809 if vlan_id == -1:
810 actions.append(ofp.action.pop_vlan())
811 if new_vlan_id > 1:
812 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_vlan_id)))
813 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
814 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
815 # 0x0000nnnn is for UNI interfaces
816 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
817
818 request = ofp.message.flow_add(
819 table_id=10,
820 cookie=42,
821 match=match,
822 instructions=[
823 ofp.instruction.apply_actions(
824 actions=actions
825 ),
826 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
827 ],
828 priority=0)
829 logging.info("Add vlan %d tagged packets on port %d and go to table %d" % (vlan_id, of_port, MPLS_L2_PORT_FLOW_TABLE))
830 ctrl.message_send(request)
831
832 if flag == VLAN_TABLE_FLAG_ONLY_UNTAG:
833 match = ofp.match()
834 match.oxm_list.append(ofp.oxm.in_port(of_port))
835 match.oxm_list.append(ofp.oxm.vlan_vid(0))
836
837 actions=[]
838 if vlan_id > 1:
839 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
840 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
841 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
842 # 0x0000nnnn is for UNI interfaces
843 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
844
845 request = ofp.message.flow_add(
846 table_id=10,
847 cookie=42,
848 match=match,
849 instructions=[
850 ofp.instruction.apply_actions(
851 actions=actions
852 ),
853 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
854 ],
855 priority=0)
856 logging.info("Add vlan %d untagged packets on port %d and go to table %d" % (vlan_id, of_port, MPLS_L2_PORT_FLOW_TABLE))
857 ctrl.message_send(request)
858
859 if send_barrier:
860 do_barrier(ctrl)
861
862 return request
863
Piere1308762016-09-12 15:29:56 -0700864def 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):
865 # table 11: vlan 1 table
866 # goto to table 20
867 if flag == VLAN_TABLE_FLAG_ONLY_TAG:
868 match = ofp.match()
869 match.oxm_list.append(ofp.oxm.in_port(of_port))
870 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
871 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
872
873 actions=[]
874 actions.append(ofp.action.push_vlan(0x8100))
875 if new_outer_vlan_id != -1:
876 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_outer_vlan_id)))
877 else:
878 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan_id)))
879
880 request = ofp.message.flow_add(
881 table_id=11,
882 cookie=42,
883 match=match,
884 instructions=[
885 ofp.instruction.apply_actions(
886 actions=actions
887 ),
888 ofp.instruction.goto_table(TERMINATION_FLOW_TABLE)
889 ],
890 priority=0)
891 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))
892 ctrl.message_send(request)
893
894 if flag == VLAN_TABLE_FLAG_ONLY_UNTAG:
895 match = ofp.match()
896 match.oxm_list.append(ofp.oxm.in_port(of_port))
897 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
898 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
899
900 actions=[]
901 request = ofp.message.flow_add(
902 table_id=11,
903 cookie=42,
904 match=match,
905 instructions=[
906 ofp.instruction.apply_actions(
907 actions=actions
908 ),
909 ofp.instruction.goto_table(TERMINATION_FLOW_TABLE)
910 ],
911 priority=0)
912 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))
913 ctrl.message_send(request)
914
macauley0f91a3e2015-07-17 18:09:59 +0800915 if send_barrier:
916 do_barrier(ctrl)
917
918 return request
Pierbbdf3782016-08-22 17:58:26 -0700919
Piercf76e802016-09-19 20:16:35 -0700920def add_one_vlan_1_table_flow_pw(ctrl, of_port, tunnel_index, new_outer_vlan_id=-1, outer_vlan_id=1, inner_vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_TAG, send_barrier=False):
921 # table 11: vlan 1 table
922 # goto to table 13
923 match = ofp.match()
924 match.oxm_list.append(ofp.oxm.in_port(of_port))
925 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
926 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
927
928 actions=[]
929 actions.append(ofp.action.push_vlan(0x8100))
930 if new_outer_vlan_id != -1:
931 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_outer_vlan_id)))
932 else:
933 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan_id)))
934
935 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
936 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
937 # 0x0000nnnn is for UNI interfaces
938 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
939
940 request = ofp.message.flow_add(
941 table_id=11,
942 cookie=42,
943 match=match,
944 instructions=[
945 ofp.instruction.apply_actions(
946 actions=actions
947 ),
948 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
949 ],
950 priority=0)
951 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))
952 ctrl.message_send(request)
953
954 if send_barrier:
955 do_barrier(ctrl)
956
957 return request
958
macauley97557232015-07-16 17:28:07 +0800959def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False):
960 match = ofp.match()
castroflavio21894482015-12-08 15:29:55 -0500961 priority=500
macauleyfddc4662015-07-27 17:40:30 +0800962 if dst_mac!=None:
castroflavio21894482015-12-08 15:29:55 -0500963 priority=1000
macauleyfddc4662015-07-27 17:40:30 +0800964 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
965
macauley97557232015-07-16 17:28:07 +0800966 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauleyfddc4662015-07-27 17:40:30 +0800967
macauley97557232015-07-16 17:28:07 +0800968 request = ofp.message.flow_add(
969 table_id=50,
970 cookie=42,
971 match=match,
972 instructions=[
973 ofp.instruction.write_actions(
974 actions=[
975 ofp.action.group(group_id)]),
976 ofp.instruction.goto_table(60)
977 ],
978 buffer_id=ofp.OFP_NO_BUFFER,
castroflavio21894482015-12-08 15:29:55 -0500979 priority=priority)
macauley97557232015-07-16 17:28:07 +0800980
981 logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac)
982 ctrl.message_send(request)
983
984 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -0700985 do_barrier(ctrl)
macauley15909e72015-07-17 15:58:57 +0800986
Pierbbdf3782016-08-22 17:58:26 -0700987 return request
macauleyfddc4662015-07-27 17:40:30 +0800988
989def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False):
990 match = ofp.match()
991 if dst_mac!=None:
992 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
993
994 match.oxm_list.append(ofp.oxm.tunnel_id(vnid))
995 if is_group == True:
996 actions=[ofp.action.group(group_id)]
997 else:
998 actions=[ofp.action.output(group_id)]
999
1000 request = ofp.message.flow_add(
1001 table_id=50,
1002 cookie=42,
1003 match=match,
1004 instructions=[
1005 ofp.instruction.write_actions(
1006 actions=actions),
1007 ofp.instruction.goto_table(60)
1008 ],
1009 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001010 priority=1000)
macauleyfddc4662015-07-27 17:40:30 +08001011
1012 logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac)
1013 ctrl.message_send(request)
1014
1015 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001016 do_barrier(ctrl)
macauleyfddc4662015-07-27 17:40:30 +08001017
Pierbbdf3782016-08-22 17:58:26 -07001018 return request
1019
macauley_cheng6b133662015-11-09 13:52:39 +08001020def add_termination_flow(ctrl, in_port, eth_type, dst_mac, vlanid, goto_table=None, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +08001021 match = ofp.match()
macauley0f91a3e2015-07-17 18:09:59 +08001022 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauleyfddc4662015-07-27 17:40:30 +08001023 if dst_mac[0]&0x01 == 0x01:
1024 match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00]))
1025 goto_table=40
1026 else:
macauley53d90fe2015-08-04 17:34:22 +08001027 if in_port!=0:
1028 match.oxm_list.append(ofp.oxm.in_port(in_port))
macauleyfddc4662015-07-27 17:40:30 +08001029 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
1030 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauley_cheng6b133662015-11-09 13:52:39 +08001031 if goto_table == None:
1032 goto_table=30
macauley0f91a3e2015-07-17 18:09:59 +08001033
1034 request = ofp.message.flow_add(
1035 table_id=20,
1036 cookie=42,
1037 match=match,
1038 instructions=[
1039 ofp.instruction.goto_table(goto_table)
1040 ],
1041 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001042 priority=1)
macauley0f91a3e2015-07-17 18:09:59 +08001043
1044 logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac)
1045 ctrl.message_send(request)
1046
1047 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001048 do_barrier(ctrl)
macauley0f91a3e2015-07-17 18:09:59 +08001049
Pierbbdf3782016-08-22 17:58:26 -07001050 return request
1051
Flavio Castroaf2b4502016-02-02 17:41:32 -05001052def add_unicast_routing_flow(ctrl, eth_type, dst_ip, mask, action_group_id, vrf=0, send_ctrl=False, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +08001053 match = ofp.match()
1054 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauley53d90fe2015-08-04 17:34:22 +08001055 if vrf != 0:
1056 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
macauley0f91a3e2015-07-17 18:09:59 +08001057
Flavio Castroaf2b4502016-02-02 17:41:32 -05001058 match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask))
1059
1060 instructions = []
1061 instructions.append(ofp.instruction.goto_table(60))
1062 if send_ctrl:
Piere0918ec2016-09-09 20:06:05 -07001063 instructions.append(ofp.instruction.apply_actions(
Flavio Castroaf2b4502016-02-02 17:41:32 -05001064 actions=[ofp.action.output( port=ofp.OFPP_CONTROLLER,
1065 max_len=ofp.OFPCML_NO_BUFFER)]))
Pierbbdf3782016-08-22 17:58:26 -07001066 else:
Flavio Castroaf2b4502016-02-02 17:41:32 -05001067 instructions.append(ofp.instruction.write_actions(
1068 actions=[ofp.action.group(action_group_id)]))
macauley53d90fe2015-08-04 17:34:22 +08001069
macauley0f91a3e2015-07-17 18:09:59 +08001070 request = ofp.message.flow_add(
1071 table_id=30,
1072 cookie=42,
1073 match=match,
Flavio Castroaf2b4502016-02-02 17:41:32 -05001074 instructions=instructions,
macauley0f91a3e2015-07-17 18:09:59 +08001075 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001076 priority=1)
macauley0f91a3e2015-07-17 18:09:59 +08001077
1078 logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip)
1079 ctrl.message_send(request)
1080
1081 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001082 do_barrier(ctrl)
macauley0f91a3e2015-07-17 18:09:59 +08001083
Flavio Castro9debaaa2016-07-26 19:37:50 -07001084 return request
Flavio Castrod8f8af22015-12-02 18:19:26 -05001085
Flavio Castro8ca52542016-04-11 11:24:49 -04001086def add_mpls_flow(ctrl, action_group_id=0x0, label=100 ,ethertype=0x0800, bos=1, vrf=1, goto_table=27, send_barrier=False):
Flavio Castrob702a2f2016-04-10 22:01:48 -04001087 match = ofp.match()
1088 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1089 match.oxm_list.append(ofp.oxm.mpls_label(label))
1090 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
Pierb5da4c92016-09-21 11:23:35 -07001091 actions = []
Flavio Castrob702a2f2016-04-10 22:01:48 -04001092 actions = [ofp.action.dec_mpls_ttl(),
Flavio Castro8ca52542016-04-11 11:24:49 -04001093 ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))]
1094 if (goto_table == 29):
Flavio Castro8ca52542016-04-11 11:24:49 -04001095 actions.append(ofp.action.group(action_group_id))
1096 else:
Flavio Castrod80fbc32016-07-25 15:54:26 -07001097 actions.append(ofp.action.set_field(
1098 ofp.oxm.exp2ByteValue(exp_type=23, value=32)))
1099 actions.append(ofp.action.group(action_group_id))
Flavio Castro8ca52542016-04-11 11:24:49 -04001100 actions.append(ofp.action.copy_ttl_in())
Flavio Castro9debaaa2016-07-26 19:37:50 -07001101
Flavio Castrob702a2f2016-04-10 22:01:48 -04001102 request = ofp.message.flow_add(
1103 table_id=24,
1104 cookie=43,
1105 match=match,
1106 instructions=[
Flavio Castro8ca52542016-04-11 11:24:49 -04001107 ofp.instruction.apply_actions(actions=actions),
1108 ofp.instruction.goto_table(goto_table)
Flavio Castrob702a2f2016-04-10 22:01:48 -04001109 ],
1110 buffer_id=ofp.OFP_NO_BUFFER,
1111 priority=1)
Flavio Castrob702a2f2016-04-10 22:01:48 -04001112 logging.info("Inserting MPLS flow , label %ld", label)
1113 ctrl.message_send(request)
1114
1115 if send_barrier:
1116 do_barrier(ctrl)
1117
1118 return request
1119
Pierb5da4c92016-09-21 11:23:35 -07001120def add_mpls_flow_pw(ctrl, action_group_id, label, ethertype, bos, goto_table=MPLS_TYPE_FLOW_TABLE, pop=True, send_barrier=False):
1121 match = ofp.match()
1122 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1123 match.oxm_list.append(ofp.oxm.mpls_label(label))
1124 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
1125
1126 actions = []
1127 actions.append(ofp.action.dec_mpls_ttl())
1128 if pop == True:
1129 actions.append(ofp.action.copy_ttl_in())
1130 actions.append(ofp.action.pop_mpls(ethertype))
1131 actions.append(ofp.action.group(action_group_id))
1132
1133 request = ofp.message.flow_add(
1134 table_id=24,
1135 cookie=43,
1136 match=match,
1137 instructions=[
1138 ofp.instruction.apply_actions(actions=actions),
1139 ofp.instruction.goto_table(goto_table)
1140 ],
1141 buffer_id=ofp.OFP_NO_BUFFER,
1142 priority=1
1143 )
1144 logging.info("Inserting MPLS flow , label %ld", label)
1145 ctrl.message_send(request)
1146
1147 if send_barrier:
1148 do_barrier(ctrl)
1149
1150 return request
1151
macauleyfddc4662015-07-27 17:40:30 +08001152def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False):
1153 match = ofp.match()
1154 match.oxm_list.append(ofp.oxm.eth_type(0x0800))
Pierbbdf3782016-08-22 17:58:26 -07001155 match.oxm_list.append(ofp.oxm.vlan_vid(vlan_id))
macauleyfddc4662015-07-27 17:40:30 +08001156 if src_ip_mask!=0:
1157 match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask))
1158 else:
1159 match.oxm_list.append(ofp.oxm.ipv4_src(src_ip))
Pierbbdf3782016-08-22 17:58:26 -07001160
macauleyfddc4662015-07-27 17:40:30 +08001161 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
Pierbbdf3782016-08-22 17:58:26 -07001162
macauleyfddc4662015-07-27 17:40:30 +08001163 request = ofp.message.flow_add(
1164 table_id=40,
1165 cookie=42,
1166 match=match,
1167 instructions=[
1168 ofp.instruction.write_actions(
1169 actions=[ofp.action.group(action_group_id)]),
1170 ofp.instruction.goto_table(60)
1171 ],
1172 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001173 priority=1)
macauleyfddc4662015-07-27 17:40:30 +08001174
1175 logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask)
1176 ctrl.message_send(request)
1177
1178 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001179 do_barrier(ctrl)
macauleyfddc4662015-07-27 17:40:30 +08001180
Pierbbdf3782016-08-22 17:58:26 -07001181 return request
macauley_cheng6b133662015-11-09 13:52:39 +08001182
1183#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 +08001184def 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 +08001185 match = ofp.match()
1186 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1187 match.oxm_list.append(ofp.oxm.ipv4_dst(ip_dst))
1188 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1189 match.oxm_list.append(ofp.oxm.tcp_dst(tcp_dst))
Pierbbdf3782016-08-22 17:58:26 -07001190
macauley_cheng6b133662015-11-09 13:52:39 +08001191 request = ofp.message.flow_add(
1192 table_id=28,
1193 cookie=42,
1194 match=match,
1195 instructions=[
1196 ofp.instruction.write_actions(
1197 actions=[ofp.action.set_field(ofp.oxm.ipv4_dst(set_ip_dst)),
1198 ofp.action.set_field(ofp.oxm.tcp_dst(set_tcp_dst)),
1199 ofp.action.group(action_group_id)]),
1200 ofp.instruction.goto_table(60)
1201 ],
1202 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001203 priority=1)
macauley_cheng6b133662015-11-09 13:52:39 +08001204 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)
1205 ctrl.message_send(request)
1206 return request
macauley_chengeffc20a2015-11-09 16:14:56 +08001207
1208#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
1209def add_snat_flow(ctrl, eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src):
1210 match = ofp.match()
1211 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1212 match.oxm_list.append(ofp.oxm.ipv4_src(ip_src))
1213 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1214 match.oxm_list.append(ofp.oxm.tcp_src(tcp_src))
Pierbbdf3782016-08-22 17:58:26 -07001215
macauley_chengeffc20a2015-11-09 16:14:56 +08001216 request = ofp.message.flow_add(
1217 table_id=29,
1218 cookie=42,
1219 match=match,
1220 instructions=[
1221 ofp.instruction.write_actions(
1222 actions=[ofp.action.set_field(ofp.oxm.ipv4_src(set_ip_src)),
1223 ofp.action.set_field(ofp.oxm.tcp_src(set_tcp_src))]),
1224 ofp.instruction.goto_table(30)
1225 ],
1226 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001227 priority=1)
macauley_chengeffc20a2015-11-09 16:14:56 +08001228 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)
1229 ctrl.message_send(request)
1230 return request
Pierbbdf3782016-08-22 17:58:26 -07001231
1232def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001233 """
1234 Command Example:
1235 of-agent vtap 10001 ethernet 1/1 vid 1
1236 of-agent vtp 10001 vni 10
1237 """
1238 if vlan != 0:
1239 config_vtap_xml="""
1240 <config>
1241 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1242 <id>capable-switch-1</id>
1243 <resources>
1244 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001245 <resource-id >LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001246 <features>
1247 <current>
1248 <rate>10Gb</rate>
1249 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001250 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001251 </current>
1252 <advertised>
1253 <rate>10Gb</rate>
1254 <rate>100Gb</rate>
1255 <medium>fiber</medium>
1256 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001257 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001258 <supported>
1259 <rate>10Gb</rate>
1260 <rate>100Gb</rate>
1261 <medium>fiber</medium>
1262 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001263 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001264 <advertised-peer>
1265 <rate>10Gb</rate>
1266 <rate>100Gb</rate>
1267 <medium>fiber</medium>
1268 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001269 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001270 </features>
1271 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1272 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1273 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1274 <ofdpa10:vni>VNID</ofdpa10:vni>
1275 </ofdpa10:vtap>
Pierbbdf3782016-08-22 17:58:26 -07001276 </port>
macauleyfddc4662015-07-27 17:40:30 +08001277 </resources>
1278 <logical-switches>
1279 <switch>
1280 <id>DATAPATH_ID</id>
1281 <datapath-id>DATAPATH_ID</datapath-id>
1282 <resources>
1283 <port xc:operation="OPERATION">LPORT</port>
1284 </resources>
1285 </switch>
1286 </logical-switches>
1287 </capable-switch>
1288 </config>
Pierbbdf3782016-08-22 17:58:26 -07001289 """
macauleyfddc4662015-07-27 17:40:30 +08001290 else:
1291 config_vtap_xml="""
1292 <config>
1293 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1294 <id>capable-switch-1</id>
1295 <resources>
1296 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001297 <resource-id >LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001298 <features>
1299 <current>
1300 <rate>10Gb</rate>
1301 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001302 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001303 </current>
1304 <advertised>
1305 <rate>10Gb</rate>
1306 <rate>100Gb</rate>
1307 <medium>fiber</medium>
1308 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001309 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001310 <supported>
1311 <rate>10Gb</rate>
1312 <rate>100Gb</rate>
1313 <medium>fiber</medium>
1314 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001315 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001316 <advertised-peer>
1317 <rate>10Gb</rate>
1318 <rate>100Gb</rate>
1319 <medium>fiber</medium>
1320 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001321 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001322 </features>
1323 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1324 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1325 <ofdpa10:vni>VNID</ofdpa10:vni>
1326 </ofdpa10:vtap>
Pierbbdf3782016-08-22 17:58:26 -07001327 </port>
macauleyfddc4662015-07-27 17:40:30 +08001328 </resources>
1329 <logical-switches>
1330 <switch>
1331 <id>DATAPATH_ID</id>
1332 <datapath-id>DATAPATH_ID</datapath-id>
1333 <resources>
1334 <port xc:operation="OPERATION">LPORT</port>
1335 </resources>
1336 </switch>
1337 </logical-switches>
1338 </capable-switch>
1339 </config>
Pierbbdf3782016-08-22 17:58:26 -07001340 """
1341 str_datapath_id_f= "{:016x}".format(dp_id)
1342 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1343 config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id)
1344 config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport)))
1345 config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port))
1346 config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan))
macauleyfddc4662015-07-27 17:40:30 +08001347 config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid))
1348 config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation))
1349 return config_vtap_xml
Pierbbdf3782016-08-22 17:58:26 -07001350
1351def 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 +08001352 """
1353 Command Example:
1354 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 -07001355 of-agent vtp 10001 vni 10
macauleyfddc4662015-07-27 17:40:30 +08001356 """
1357
1358 config_vtep_xml="""
1359 <config>
1360 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1361 <id>capable-switch-1</id>
1362 <resources>
1363 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001364 <resource-id>LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001365 <features>
1366 <current>
1367 <rate>10Gb</rate>
1368 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001369 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001370 </current>
1371 <advertised>
1372 <rate>10Gb</rate>
1373 <rate>100Gb</rate>
1374 <medium>fiber</medium>
1375 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001376 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001377 <supported>
1378 <rate>10Gb</rate>
1379 <rate>100Gb</rate>
1380 <medium>fiber</medium>
1381 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001382 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001383 <advertised-peer>
1384 <rate>10Gb</rate>
1385 <rate>100Gb</rate>
1386 <medium>fiber</medium>
1387 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001388 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001389 </features>
1390 <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01">
1391 <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip>
1392 <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip>
1393 <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port>
macauley25999cf2015-08-07 17:03:24 +08001394 <ofdpa10:vni xc:operation="OPERATION">
1395 <ofdpa10:id>VNID</ofdpa10:id>
1396 </ofdpa10:vni>
macauleyfddc4662015-07-27 17:40:30 +08001397 <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id>
1398 <ofdpa10:ttl>TTL</ofdpa10:ttl>
1399 </ofdpa10:vtep>
Pierbbdf3782016-08-22 17:58:26 -07001400 </port>
macauleyfddc4662015-07-27 17:40:30 +08001401 </resources>
1402 <logical-switches>
1403 <switch>
1404 <id>DATAPATH_ID</id>
1405 <datapath-id>DATAPATH_ID</datapath-id>
1406 <resources>
1407 <port xc:operation="OPERATION">LPORT</port>
1408 </resources>
1409 </switch>
1410 </logical-switches>
1411 </capable-switch>
Pierbbdf3782016-08-22 17:58:26 -07001412 </config>
macauleyfddc4662015-07-27 17:40:30 +08001413 """
Pierbbdf3782016-08-22 17:58:26 -07001414 str_datapath_id_f= "{:016x}".format(dp_id)
1415 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1416 config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +08001417 config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport)))
Pierbbdf3782016-08-22 17:58:26 -07001418 config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip))
1419 config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip))
1420 config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port))
1421 config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1422 config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl))
macauleyfddc4662015-07-27 17:40:30 +08001423 config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid))
Pierbbdf3782016-08-22 17:58:26 -07001424 config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation))
macauleyfddc4662015-07-27 17:40:30 +08001425
Pierbbdf3782016-08-22 17:58:26 -07001426 return config_vtep_xml
1427
1428def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001429 #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2
1430 config_nexthop_xml="""
1431 <config>
1432 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1433 <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1434 <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id>
1435 <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac>
1436 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1437 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1438 </ofdpa10:next-hop>
1439 </of11-config:capable-switch>
1440 </config>
1441 """
1442 config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan))
Pierbbdf3782016-08-22 17:58:26 -07001443 config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port))
1444 config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1445 config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac))
1446 config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation))
1447 return config_nexthop_xml
macauleyfddc4662015-07-27 17:40:30 +08001448
Pierbbdf3782016-08-22 17:58:26 -07001449def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001450 #of-agent vni 10 multicast 224.1.1.1 nexthop 20
Pierbbdf3782016-08-22 17:58:26 -07001451 if mcast_ipv4!=None:
macauleyfddc4662015-07-27 17:40:30 +08001452 config_vni_xml="""
1453 <config>
1454 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1455 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1456 <ofdpa10:id>VNID</ofdpa10:id>
1457 <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group>
1458 <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id>
1459 </ofdpa10:vni>
1460 </of11-config:capable-switch>
1461 </config>
Pierbbdf3782016-08-22 17:58:26 -07001462 """
1463 config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1464 config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4))
macauleyfddc4662015-07-27 17:40:30 +08001465 else:
1466 config_vni_xml="""
1467 <config>
1468 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1469 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1470 <ofdpa10:id>VNID</ofdpa10:id>
1471 </ofdpa10:vni>
1472 </of11-config:capable-switch>
1473 </config>
Pierbbdf3782016-08-22 17:58:26 -07001474 """
1475
1476 config_vni_xml=config_vni_xml.replace("VNID", str(vni_id))
1477 config_vni_xml=config_vni_xml.replace("OPERATION", str(operation))
macauleyfddc4662015-07-27 17:40:30 +08001478 return config_vni_xml
Pierbbdf3782016-08-22 17:58:26 -07001479
1480def get_featureReplay(self):
macauleyfddc4662015-07-27 17:40:30 +08001481 req = ofp.message.features_request()
1482 res, raw = self.controller.transact(req)
Pierbbdf3782016-08-22 17:58:26 -07001483 self.assertIsNotNone(res, "Did not receive a response from the DUT.")
macauleyfddc4662015-07-27 17:40:30 +08001484 self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY,
1485 ("Unexpected packet type %d received in response to "
1486 "OFPT_FEATURES_REQUEST") % res.type)
Pierbbdf3782016-08-22 17:58:26 -07001487 return res
1488
macauleyfddc4662015-07-27 17:40:30 +08001489def send_edit_config(switch_ip, xml, target='runing'):
1490 NETCONF_ACCOUNT="netconfuser"
1491 NETCONF_PASSWD="netconfuser"
1492 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1493 try:
Pierbbdf3782016-08-22 17:58:26 -07001494 m.edit_config(target='running',
1495 config=xml,
1496 default_operation='merge',
macauleyfddc4662015-07-27 17:40:30 +08001497 error_option='stop-on-error')
1498
1499 except Exception as e:
1500 logging.info("Fail to set xml %s", xml)
1501 return False
1502
1503 #return m.get_config(source='running').data_xml
1504 return True
1505
1506def send_delete_config(switch_ip, xml, target='runing'):
1507 NETCONF_ACCOUNT="netconfuser"
1508 NETCONF_PASSWD="netconfuser"
1509 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1510 try:
Pierbbdf3782016-08-22 17:58:26 -07001511 m.edit_config(target='running',
1512 config=xml,
1513 default_operation='delete',
macauleyfddc4662015-07-27 17:40:30 +08001514 error_option='stop-on-error')
1515
1516 except Exception as e:
1517 logging.info("Fail to set xml %s", xml)
1518 return False
1519
1520 #return m.get_config(source='running').data_xml
1521 return True
Pierbbdf3782016-08-22 17:58:26 -07001522
macauleyfddc4662015-07-27 17:40:30 +08001523def get_edit_config(switch_ip, target='runing'):
1524 NETCONF_ACCOUNT="netconfuser"
1525 NETCONF_PASSWD="netconfuser"
1526 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
macauley6f6ceb22015-08-07 09:37:12 +08001527 return m.get_config(source='running').data_xml
macauleydbff3272015-07-30 14:07:16 +08001528
macauley_cheng67da9262015-08-31 15:18:41 +08001529
1530"""
1531MPLS
1532"""
1533
1534OFDPA_MPLS_SUBTYPE_SHIFT=24
Pierbbdf3782016-08-22 17:58:26 -07001535OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1
macauley_cheng67da9262015-08-31 15:18:41 +08001536OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2
1537OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3
1538OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4
1539OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5
1540OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6
1541OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8
1542OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10
1543
Piercf76e802016-09-19 20:16:35 -07001544
1545
1546
macauley_cheng67da9262015-08-31 15:18:41 +08001547def encode_mpls_interface_group_id(subtype, index):
1548 index=index&0x00ffffff
1549 assert(subtype==0)
1550 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1551
1552def encode_mpls_label_group_id(subtype, index):
1553 index=index&0x00ffffff
1554 assert(subtype <=5 or subtype==0)
1555 #1: l2 vpn label
1556 #2: l3 vpn label
1557 #3: mpls tunnel label 1
1558 #4: mpls tunnel lable 2
1559 #5: mpls swap label
Pierbbdf3782016-08-22 17:58:26 -07001560 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
macauley_cheng67da9262015-08-31 15:18:41 +08001561
1562def encode_mpls_forwarding_group_id(subtype, index):
1563 index=index&0x00ffffff
1564 assert(subtype==6 or subtype==8 or subtype==10)
Pierbbdf3782016-08-22 17:58:26 -07001565 return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
macauley_cheng67da9262015-08-31 15:18:41 +08001566
1567
1568def add_mpls_intf_group(ctrl, ref_gid, dst_mac, src_mac, vid, index, subtype=0):
1569 action=[]
1570 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
1571 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
Piercf76e802016-09-19 20:16:35 -07001572 if vid != 1:
1573 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001574 action.append(ofp.action.group(ref_gid))
Pierbbdf3782016-08-22 17:58:26 -07001575
macauley_cheng67da9262015-08-31 15:18:41 +08001576 buckets = [ofp.bucket(actions=action)]
1577
1578 mpls_group_id =encode_mpls_interface_group_id(subtype, index)
1579 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1580 group_id=mpls_group_id,
1581 buckets=buckets
1582 )
1583 ctrl.message_send(request)
1584 return mpls_group_id, request
1585
Piercf76e802016-09-19 20:16:35 -07001586def add_mpls_tunnel_label_group(
1587 ctrl,
1588 ref_gid,
1589 subtype,
1590 index,
1591 label,
1592 ):
1593
1594 action=[]
1595 action.append(ofp.action.push_mpls(0x8847))
1596 action.append(ofp.action.set_field(ofp.oxm.mpls_label(label)))
1597 action.append(ofp.action.group(ref_gid))
1598 buckets = [ofp.bucket(actions=action)]
1599
1600 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1601 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1602 group_id=mpls_group_id,
1603 buckets=buckets
1604 )
1605 ctrl.message_send(request)
1606
1607 return mpls_group_id, request
1608
Pierb5da4c92016-09-21 11:23:35 -07001609def add_mpls_swap_label_group(
1610 ctrl,
1611 ref_gid,
1612 subtype,
1613 index,
1614 label,
1615 ):
1616
1617 action=[]
1618 action.append(ofp.action.set_field(ofp.oxm.mpls_label(label)))
1619 action.append(ofp.action.group(ref_gid))
1620 buckets = [ofp.bucket(actions=action)]
1621
1622 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1623 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1624 group_id=mpls_group_id,
1625 buckets=buckets
1626 )
1627 ctrl.message_send(request)
1628
1629 return mpls_group_id, request
1630
Pierbbdf3782016-08-22 17:58:26 -07001631def add_mpls_label_group(ctrl, subtype, index, ref_gid,
macauley_cheng67da9262015-08-31 15:18:41 +08001632 lmep_id=-1,
1633 qos_index=-1,
1634 push_l2_header=False,
1635 push_vlan=False,
1636 push_mpls_header=False,
1637 push_cw=False,
1638 set_mpls_label=None,
1639 set_bos=None,
1640 set_tc=None,
1641 set_tc_from_table=False,
1642 cpy_tc_outward=False,
1643 set_ttl=None,
1644 cpy_ttl_outward=False,
1645 oam_lm_tx_count=False,
1646 set_pri_from_table=False
1647 ):
1648 """
1649 @ref_gid: only can be mpls intf group or mpls tunnel label 1/2 group
Pierbbdf3782016-08-22 17:58:26 -07001650 """
macauley_cheng67da9262015-08-31 15:18:41 +08001651 action=[]
1652
1653 if push_vlan== True:
1654 action.append(ofp.action.push_vlan(0x8100))
1655 if push_mpls_header== True:
1656 action.append(ofp.action.push_mpls(0x8847))
1657 if set_mpls_label != None:
1658 action.append(ofp.action.set_field(ofp.oxm.mpls_label(set_mpls_label)))
1659 if set_bos != None:
1660 action.append(ofp.action.set_field(ofp.oxm.mpls_bos(set_bos)))
1661 if set_tc != None:
1662 assert(set_tc_from_table==False)
1663 action.append(ofp.action.set_field(ofp.oxm.mpls_tc(set_tc)))
1664 if set_ttl != None:
Pierbbdf3782016-08-22 17:58:26 -07001665 action.append(ofp.action.set_mpls_ttl(set_ttl))
macauley_cheng67da9262015-08-31 15:18:41 +08001666 if cpy_ttl_outward == True:
Pierbbdf3782016-08-22 17:58:26 -07001667 action.append(ofp.action.copy_ttl_out())
macauley_cheng67da9262015-08-31 15:18:41 +08001668 """
1669 ofdpa experimenter
Pierbbdf3782016-08-22 17:58:26 -07001670 """
macauley_cheng67da9262015-08-31 15:18:41 +08001671 if push_l2_header== True:
Pierbbdf3782016-08-22 17:58:26 -07001672 action.append(ofp.action.ofdpa_push_l2_header())
macauley_cheng67da9262015-08-31 15:18:41 +08001673 if set_tc_from_table== True:
1674 assert(qos_index>=0)
1675 assert(set_tc == None)
Pierbbdf3782016-08-22 17:58:26 -07001676 action.append(ofp.action.ofdpa_set_tc_from_table(qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08001677 if cpy_tc_outward == True:
Pierbbdf3782016-08-22 17:58:26 -07001678 action.append(ofp.action.ofdpa_copy_tc_out())
macauley_cheng67da9262015-08-31 15:18:41 +08001679 if oam_lm_tx_count == True:
Pierbbdf3782016-08-22 17:58:26 -07001680 assert(qos_index>=0 and lmep_id>=0)
1681 action.append(ofp.action.ofdpa_oam_lm_tx_count(lmep_id, qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08001682 if set_pri_from_table == True:
Pierbbdf3782016-08-22 17:58:26 -07001683 assert(qos_index>=0)
1684 action.append(ofp.action.ofdpa_set_qos_from_table(qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08001685 if push_cw == True:
1686 action.append(ofp.action.ofdpa_push_cw())
Pierbbdf3782016-08-22 17:58:26 -07001687
1688 action.append(ofp.action.group(ref_gid))
macauley_cheng67da9262015-08-31 15:18:41 +08001689 buckets = [ofp.bucket(actions=action)]
Pierbbdf3782016-08-22 17:58:26 -07001690
macauley_cheng67da9262015-08-31 15:18:41 +08001691 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1692 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1693 group_id=mpls_group_id,
1694 buckets=buckets
1695 )
1696 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -07001697
1698 return mpls_group_id, request
1699
Piercf76e802016-09-19 20:16:35 -07001700def add_mpls_l2_port_flow(ctrl, of_port, mpls_l2_port, tunnel_index, ref_gid, qos_index=0):
1701 """
1702 Only action is Group, which must indicate one of:
1703 MPLS L2 VPN Label or Fast Failover Protection Group.
1704 ref_gid contains this information
1705 """
1706 tunnel_id = tunnel_index + ofp.oxm.TUNNEL_ID_BASE
1707
1708 match = ofp.match()
1709 match.oxm_list.append(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port))
1710 match.oxm_list.append(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE))
1711
1712 action = []
1713 action.append(ofp.action.group(ref_gid))
1714 assert(qos_index>=0)
1715 action.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_QOS_INDEX, value=qos_index)))
1716
1717 request = ofp.message.flow_add(
1718 table_id=MPLS_L2_PORT_FLOW_TABLE,
1719 cookie=42,
1720 match=match,
1721 instructions=[
1722 ofp.instruction.write_actions(
1723 actions=action
1724 ),
1725 ofp.instruction.goto_table(MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE) ],
1726 buffer_id=ofp.OFP_NO_BUFFER,
1727 priority=1)
1728 logging.info("Inserting flow %d mpls_l2_port, %d tunnel_id, action %x group and go to table %d", mpls_l2_port, tunnel_id, ref_gid, MPLS_L2_PORT_DSCP_TRUST_FLOW_TABLE)
1729 ctrl.message_send(request)
1730 return request
1731
1732 return
1733
Pierbbdf3782016-08-22 17:58:26 -07001734def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids,
1735 watch_port=None,
1736 watch_group=ofp.OFPP_ANY,
macauley_cheng67da9262015-08-31 15:18:41 +08001737 push_vlan=None,
macauley_chengd17ce512015-08-31 17:45:51 +08001738 pop_vlan=None,
macauley_cheng67da9262015-08-31 15:18:41 +08001739 set_vid=None):
1740 assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP
1741 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP
1742 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG)
macauley_chengd17ce512015-08-31 17:45:51 +08001743
macauley_cheng67da9262015-08-31 15:18:41 +08001744 buckets=[]
1745 if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP:
macauley_chengd17ce512015-08-31 17:45:51 +08001746 group_type = ofp.OFPGT_FF
macauley_cheng67da9262015-08-31 15:18:41 +08001747 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001748 action=[]
Pierbbdf3782016-08-22 17:58:26 -07001749 action.append(ofp.action.group(gid))
macauley_chengd17ce512015-08-31 17:45:51 +08001750 buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action))
1751
macauley_cheng67da9262015-08-31 15:18:41 +08001752 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP:
1753 group_type = ofp.OFPGT_SELECT
1754 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001755 action=[]
Pierbbdf3782016-08-22 17:58:26 -07001756 action.append(ofp.action.group(gid))
macauley_cheng67da9262015-08-31 15:18:41 +08001757 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001758
macauley_cheng67da9262015-08-31 15:18:41 +08001759 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG:
1760 group_type = ofp.OFPGT_INDIRECT
macauley_chengd17ce512015-08-31 17:45:51 +08001761 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08001762 if set_vid!=None:
Flavio Castro91d1a552016-05-17 16:59:44 -07001763 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+set_vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001764 if push_vlan!=None:
Pierbbdf3782016-08-22 17:58:26 -07001765 action.append(ofp.action.push_vlan(push_vlan))
macauley_cheng67da9262015-08-31 15:18:41 +08001766 if pop_vlan!=None:
Pierbbdf3782016-08-22 17:58:26 -07001767 action.append(ofp.action.pop_vlan())
1768 action.append(ofp.action.group(ref_gids[0]))
macauley_cheng67da9262015-08-31 15:18:41 +08001769 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001770
1771 mpls_group_id = encode_mpls_forwarding_group_id(subtype, index)
macauley_cheng67da9262015-08-31 15:18:41 +08001772 request = ofp.message.group_add(group_type=group_type,
macauley_cheng67da9262015-08-31 15:18:41 +08001773 group_id=mpls_group_id,
1774 buckets=buckets
1775 )
1776 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -07001777 return mpls_group_id, request
macauley_chengd17ce512015-08-31 17:45:51 +08001778
1779
macauley_cheng67da9262015-08-31 15:18:41 +08001780"""
Piercf76e802016-09-19 20:16:35 -07001781display
Pierbbdf3782016-08-22 17:58:26 -07001782"""
macauleydbff3272015-07-30 14:07:16 +08001783def print_current_table_flow_stat(ctrl, table_id=0xff):
1784 stat_req=ofp.message.flow_stats_request()
1785 response, pkt = ctrl.transact(stat_req)
1786 if response == None:
1787 print "no response"
1788 return None
1789 print len(response.entries)
1790 for obj in response.entries:
1791 print "match ", obj.match
1792 print "cookie", obj.cookie
1793 print "priority", obj.priority
1794 print "idle_timeout", obj.idle_timeout
1795 print "hard_timeout", obj.hard_timeout
1796 #obj.actions
Flavio Castro167f5bd2015-12-02 19:33:53 -05001797 print "packet count: %lx"%obj.packet_count