blob: a729f7909f49ef5340b1c4bf0dd616bb12d9941e [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 *
Pier1e4e98e2016-10-26 14:36:05 -07008from oftest.parse import parse_ipv6
macauley97557232015-07-16 17:28:07 +08009
macauleyfddc4662015-07-27 17:40:30 +080010from ncclient import manager
11import ncclient
12
macauley97557232015-07-16 17:28:07 +080013OFDPA_GROUP_TYPE_SHIFT=28
14OFDPA_VLAN_ID_SHIFT =16
macauleyfddc4662015-07-27 17:40:30 +080015OFDPA_TUNNEL_ID_SHIFT =12
16OFDPA_TUNNEL_SUBTYPE_SHIFT=10
macauley97557232015-07-16 17:28:07 +080017
18#VLAN_TABLE_FLAGS
19VLAN_TABLE_FLAG_ONLY_UNTAG=1
20VLAN_TABLE_FLAG_ONLY_TAG =2
21VLAN_TABLE_FLAG_ONLY_BOTH =3
Piere1308762016-09-12 15:29:56 -070022VLAN_TABLE_FLAG_ONLY_STACKED=5
23VLAN_TABLE_FLAG_PRIORITY=6
24VLAN_TABLE_FLAG_ONLY_UNTAG_PRIORITY=7
macauley97557232015-07-16 17:28:07 +080025
macauleye8b140e2015-08-03 13:35:45 +080026PORT_FLOW_TABLE=0
27VLAN_FLOW_TABLE=10
Piere1308762016-09-12 15:29:56 -070028VLAN_1_FLOW_TABLE=11
Piercf76e802016-09-19 20:16:35 -070029MPLS_L2_PORT_FLOW_TABLE=13
30MPLS_L2_PORT_DSCP_TRUST_FLOW_TABLE=15
31MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE=16
macauleye8b140e2015-08-03 13:35:45 +080032TERMINATION_FLOW_TABLE=20
Piercf76e802016-09-19 20:16:35 -070033MPLS_TYPE_FLOW_TABLE=29
macauleye8b140e2015-08-03 13:35:45 +080034UCAST_ROUTING_FLOW_TABLE=30
35MCAST_ROUTING_FLOW_TABLE=40
36BRIDGE_FLOW_TABLE=50
37ACL_FLOW_TABLE=60
38
39def convertIP4toStr(ip_addr):
40 a=(ip_addr&0xff000000)>>24
41 b=(ip_addr&0x00ff0000)>>16
42 c=(ip_addr&0x0000ff00)>>8
43 d=(ip_addr&0x000000ff)
44 return str(a)+"."+str(b)+"."+str(c)+"."+str(d)
45
46def convertMACtoStr(mac):
47 if not isinstance(mac, list):
48 assert(0)
49
50 return ':'.join(['%02X' % x for x in mac])
51
macauley7f89d962015-08-06 18:13:48 +080052def getSwitchCpuMACFromDPID(dpid):
53 str_datapath_id_f= "{:016x}".format(dpid)
54 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
55 switch_cpu_mac_str=str_datapath_id[6:]
56 switch_cpu_mac = switch_cpu_mac_str.split(":")
57 switch_cpu_mac=[int(switch_cpu_mac[i],16) for i in range(0, len(switch_cpu_mac))]
58
59 return switch_cpu_mac_str, switch_cpu_mac
Pierbbdf3782016-08-22 17:58:26 -070060
macauley_cheng67da9262015-08-31 15:18:41 +080061def DumpGroup(stats, verify_group_stats, always_show=True):
62 if(len(stats) > len(verify_group_stats)):
63 min_len = len(verify_group_stats)
64 print "Stats Len is not the same, stats>verify_group_stats"
65 if(len(stats)< len(verify_group_stats)):
Pierbbdf3782016-08-22 17:58:26 -070066 min_len = len(stats)
macauley_cheng67da9262015-08-31 15:18:41 +080067 print "Stats Len is not the same, stats<verify_group_stats"
Pierbbdf3782016-08-22 17:58:26 -070068 else:
macauley_cheng67da9262015-08-31 15:18:41 +080069 min_len = len(stats)
macauleye8b140e2015-08-03 13:35:45 +080070
macauley_cheng67da9262015-08-31 15:18:41 +080071 print "\r\n"
72 for i in range(min_len):
73 gs = stats[i]
Pierbbdf3782016-08-22 17:58:26 -070074 gv = verify_group_stats[i]
macauley_cheng67da9262015-08-31 15:18:41 +080075 print "FromSwtich:(GID=%lx, TYPE=%lx)\r\nVerify :(GID=%lx, TYPE=%lx)"%(gs.group_id, gs.group_type, gv.group_id, gv.group_type)
76 if(len(gs.buckets) != len(gv.buckets)):
77 print "buckets len is not the same gs %lx, gv %lx",(len(gs.buckets), len(gv.buckets))
78
79 for j in range(len(gs.buckets)):
80 b1=gs.buckets[j]
Pierbbdf3782016-08-22 17:58:26 -070081 b2=gv.buckets[j]
macauley_cheng67da9262015-08-31 15:18:41 +080082 if(len(b1.actions) != len(b2.actions)):
83 print "action len is not the same"
84
85 for k in range(len(b1.actions)):
86 a1=b1.actions[k]
87 a2=b2.actions[k]
88 if(always_show == True):
89 print "a1:"+a1.show()
Pierbbdf3782016-08-22 17:58:26 -070090 print "a2:"+a2.show()
macauley_cheng67da9262015-08-31 15:18:41 +080091
92def AssertGroup(self, stats, verify_group_stats):
93 self.assertTrue(len(stats) ==len(verify_group_stats), "stats len is not the same")
94
95 for i in range(len(stats)):
96 gs = stats[i]
Pierbbdf3782016-08-22 17:58:26 -070097 gv = verify_group_stats[i]
macauley_cheng67da9262015-08-31 15:18:41 +080098 self.assertTrue(len(gs.buckets) == len(gv.buckets), "buckets len is not the same")
99
100 for j in range(len(gs.buckets)):
101 b1=gs.buckets[j]
Pierbbdf3782016-08-22 17:58:26 -0700102 b2=gv.buckets[j]
macauley_cheng67da9262015-08-31 15:18:41 +0800103 self.assertTrue(len(b1.actions) == len(b2.actions), "action len is not the same")
104
105 for k in range(len(b1.actions)):
106 a1=b1.actions[k]
107 a2=b2.actions[k]
108 self.assertEquals(a1, a2, "action is not the same")
Pierbbdf3782016-08-22 17:58:26 -0700109
macauley97557232015-07-16 17:28:07 +0800110def encode_l2_interface_group_id(vlan, id):
111 return id + (vlan << OFDPA_VLAN_ID_SHIFT)
112
113def encode_l2_rewrite_group_id(id):
114 return id + (1 << OFDPA_GROUP_TYPE_SHIFT)
115
116def encode_l3_unicast_group_id(id):
117 return id + (2 << OFDPA_GROUP_TYPE_SHIFT)
118
119def encode_l2_mcast_group_id(vlan, id):
120 return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (3 << OFDPA_GROUP_TYPE_SHIFT)
121
122def encode_l2_flood_group_id(vlan, id):
123 return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (4 << OFDPA_GROUP_TYPE_SHIFT)
Pierbbdf3782016-08-22 17:58:26 -0700124
macauley97557232015-07-16 17:28:07 +0800125def encode_l3_interface_group_id(id):
126 return id + (5 << OFDPA_GROUP_TYPE_SHIFT)
127
128def encode_l3_mcast_group_id(vlan, id):
129 return id + (vlan << OFDPA_VLAN_ID_SHIFT)+(6 << OFDPA_GROUP_TYPE_SHIFT)
130
131def encode_l3_ecmp_group_id(id):
132 return id + (7 << OFDPA_GROUP_TYPE_SHIFT)
133
Flavio Castro91d1a552016-05-17 16:59:44 -0700134def encode_l2_unfiltered_group_id(id):
135 return id + (11 << OFDPA_GROUP_TYPE_SHIFT)
136
macauleyfddc4662015-07-27 17:40:30 +0800137def encode_l2_overlay_group_id(tunnel_id, subtype, index):
138 tunnel_id=tunnel_id&0xffff #16 bits
139 subtype = subtype&3 #2 bits
140 index = index & 0x3f #10 bits
141 return index + (tunnel_id << OFDPA_TUNNEL_ID_SHIFT)+ (subtype<<OFDPA_TUNNEL_SUBTYPE_SHIFT)+(8 << OFDPA_GROUP_TYPE_SHIFT)
macauley97557232015-07-16 17:28:07 +0800142
Flavio Castro91d1a552016-05-17 16:59:44 -0700143def add_l2_unfiltered_group(ctrl, ports, send_barrier=False):
144 # group table
145 # set up untag groups for each port
146 group_id_list=[]
147 msgs=[]
148 for of_port in ports:
149 # do stuff
150 group_id = encode_l2_unfiltered_group_id(of_port)
151 group_id_list.append(group_id)
152 actions = [ofp.action.output(of_port)]
153 actions.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=24, value=1)))
154
155 buckets = [ofp.bucket(actions=actions)]
156 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
157 group_id=group_id,
158 buckets=buckets
159 )
160 ctrl.message_send(request)
161 msgs.append(request)
162
163 if send_barrier:
164 do_barrier(ctrl)
165
166 return group_id_list, msgs
167
Pierf6f28162016-09-22 16:30:52 -0700168def add_one_l2_unfiltered_group(ctrl, of_port, send_barrier=False):
169 # group table
170 # set up untag groups for each port
171 group_id = encode_l2_unfiltered_group_id(of_port)
172 actions = [ofp.action.output(of_port)]
173 actions.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=24, value=1)))
174
175 buckets = [ofp.bucket(actions=actions)]
176 request = ofp.message.group_add(
177 group_type=ofp.OFPGT_INDIRECT,
178 group_id=group_id,
179 buckets=buckets
180 )
181 ctrl.message_send(request)
182
183 if send_barrier:
184 do_barrier(ctrl)
185
186 return group_id, request
187
Flavio Castrod4c44d12015-12-08 14:44:18 -0500188def add_l2_interface_group(ctrl, ports, vlan_id=1, is_tagged=False, send_barrier=False):
macauley97557232015-07-16 17:28:07 +0800189 # group table
190 # set up untag groups for each port
macauley41904ed2015-07-16 17:38:35 +0800191 group_id_list=[]
macauley15909e72015-07-17 15:58:57 +0800192 msgs=[]
macauley97557232015-07-16 17:28:07 +0800193 for of_port in ports:
194 # do stuff
195 group_id = encode_l2_interface_group_id(vlan_id, of_port)
macauley41904ed2015-07-16 17:38:35 +0800196 group_id_list.append(group_id)
macauley97557232015-07-16 17:28:07 +0800197 if is_tagged:
198 actions = [
199 ofp.action.output(of_port),
Pierbbdf3782016-08-22 17:58:26 -0700200 ]
macauley97557232015-07-16 17:28:07 +0800201 else:
202 actions = [
203 ofp.action.pop_vlan(),
204 ofp.action.output(of_port),
205 ]
206
207 buckets = [
208 ofp.bucket(actions=actions),
209 ]
210
211 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
212 group_id=group_id,
213 buckets=buckets
214 )
215 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800216 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800217
218 if send_barrier:
219 do_barrier(ctrl)
Pierbbdf3782016-08-22 17:58:26 -0700220
macauley15909e72015-07-17 15:58:57 +0800221 return group_id_list, msgs
macauley97557232015-07-16 17:28:07 +0800222
Flavio Castrod4c44d12015-12-08 14:44:18 -0500223def add_one_l2_interface_group(ctrl, port, vlan_id=1, is_tagged=False, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800224 # group table
225 # set up untag groups for each port
226 group_id = encode_l2_interface_group_id(vlan_id, port)
227
228 if is_tagged:
229 actions = [
230 ofp.action.output(port),
Pierbbdf3782016-08-22 17:58:26 -0700231 ]
macauley0f91a3e2015-07-17 18:09:59 +0800232 else:
233 actions = [
234 ofp.action.pop_vlan(),
235 ofp.action.output(port),
236 ]
237
238 buckets = [
239 ofp.bucket(actions=actions),
240 ]
241
242 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
243 group_id=group_id,
244 buckets=buckets
245 )
246 ctrl.message_send(request)
247
248 if send_barrier:
249 do_barrier(ctrl)
Pierbbdf3782016-08-22 17:58:26 -0700250
macauley0f91a3e2015-07-17 18:09:59 +0800251 return group_id, request
Pierbbdf3782016-08-22 17:58:26 -0700252
macauley97557232015-07-16 17:28:07 +0800253def add_l2_mcast_group(ctrl, ports, vlanid, mcast_grp_index):
254 buckets=[]
255 for of_port in ports:
256 group_id = encode_l2_interface_group_id(vlanid, of_port)
257 action=[ofp.action.group(group_id)]
258 buckets.append(ofp.bucket(actions=action))
259
260 group_id =encode_l2_mcast_group_id(vlanid, mcast_grp_index)
261 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
262 group_id=group_id,
263 buckets=buckets
264 )
265 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800266 return request
macauley97557232015-07-16 17:28:07 +0800267
macauley15909e72015-07-17 15:58:57 +0800268def add_l2_flood_group(ctrl, ports, vlanid, id):
269 buckets=[]
270 for of_port in ports:
271 group_id = encode_l2_interface_group_id(vlanid, of_port)
272 action=[ofp.action.group(group_id)]
273 buckets.append(ofp.bucket(actions=action))
macauley97557232015-07-16 17:28:07 +0800274
macauley15909e72015-07-17 15:58:57 +0800275 group_id =encode_l2_flood_group_id(vlanid, id)
276 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
277 group_id=group_id,
278 buckets=buckets
279 )
280 ctrl.message_send(request)
281 return request
282
Flavio Castroa7162bb2016-07-25 17:30:30 -0700283def mod_l2_flood_group(ctrl, ports, vlanid, id):
284 buckets=[]
285 for of_port in ports:
286 group_id = encode_l2_interface_group_id(vlanid, of_port)
287 action=[ofp.action.group(group_id)]
288 buckets.append(ofp.bucket(actions=action))
289
290 group_id =encode_l2_flood_group_id(vlanid, id)
291 request = ofp.message.group_modify(group_type=ofp.OFPGT_ALL,
292 group_id=group_id,
293 buckets=buckets
294 )
295 ctrl.message_send(request)
296 return request
297
298
macauley15909e72015-07-17 15:58:57 +0800299def add_l2_rewrite_group(ctrl, port, vlanid, id, src_mac, dst_mac):
300 group_id = encode_l2_interface_group_id(vlanid, port)
301
302 action=[]
303 if src_mac is not None:
304 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
305
306 if dst_mac is not None:
307 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
308
Flavio Castro91d1a552016-05-17 16:59:44 -0700309 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
Pierbbdf3782016-08-22 17:58:26 -0700310
macauley15909e72015-07-17 15:58:57 +0800311 action.append(ofp.action.group(group_id))
Pierbbdf3782016-08-22 17:58:26 -0700312
macauley15909e72015-07-17 15:58:57 +0800313 buckets = [ofp.bucket(actions=action)]
314
315 group_id =encode_l2_rewrite_group_id(id)
316 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
317 group_id=group_id,
318 buckets=buckets
319 )
320 ctrl.message_send(request)
321 return request
Pierbbdf3782016-08-22 17:58:26 -0700322
Pier1e4e98e2016-10-26 14:36:05 -0700323def add_l3_unicast_group(ctrl, port, vlanid, id, src_mac, dst_mac, send_barrier=False):
macauley15909e72015-07-17 15:58:57 +0800324 group_id = encode_l2_interface_group_id(vlanid, port)
325
326 action=[]
327 if src_mac is not None:
328 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
329
330 if dst_mac is not None:
331 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
332
Flavio Castroaf2b4502016-02-02 17:41:32 -0500333 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
Pierbbdf3782016-08-22 17:58:26 -0700334
macauley15909e72015-07-17 15:58:57 +0800335 action.append(ofp.action.group(group_id))
Pierbbdf3782016-08-22 17:58:26 -0700336
macauley15909e72015-07-17 15:58:57 +0800337 buckets = [ofp.bucket(actions=action)]
338
339 group_id =encode_l3_unicast_group_id(id)
340 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
341 group_id=group_id,
342 buckets=buckets
343 )
344 ctrl.message_send(request)
Pier1e4e98e2016-10-26 14:36:05 -0700345
346 if send_barrier:
347 do_barrier(ctrl)
348
macauley15909e72015-07-17 15:58:57 +0800349 return request
Pierbbdf3782016-08-22 17:58:26 -0700350
macauley15909e72015-07-17 15:58:57 +0800351def add_l3_interface_group(ctrl, port, vlanid, id, src_mac):
352 group_id = encode_l2_interface_group_id(vlanid, port)
353
354 action=[]
355 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
Pierbbdf3782016-08-22 17:58:26 -0700356 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
macauley15909e72015-07-17 15:58:57 +0800357 action.append(ofp.action.group(group_id))
Pierbbdf3782016-08-22 17:58:26 -0700358
macauley15909e72015-07-17 15:58:57 +0800359 buckets = [ofp.bucket(actions=action)]
360
361 group_id =encode_l3_interface_group_id(id)
362 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
363 group_id=group_id,
364 buckets=buckets
365 )
366 ctrl.message_send(request)
367 return request
368
Pier1e4e98e2016-10-26 14:36:05 -0700369def add_l3_ecmp_group(ctrl, id, l3_ucast_groups, send_barrier=False):
macauley15909e72015-07-17 15:58:57 +0800370 buckets=[]
371 for group in l3_ucast_groups:
372 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
373
374 group_id =encode_l3_ecmp_group_id(id)
375 request = ofp.message.group_add(group_type=ofp.OFPGT_SELECT,
376 group_id=group_id,
377 buckets=buckets
378 )
379 ctrl.message_send(request)
Pier1e4e98e2016-10-26 14:36:05 -0700380
381 if send_barrier:
382 do_barrier(ctrl)
383
macauley15909e72015-07-17 15:58:57 +0800384 return request
Flavio Castroa7162bb2016-07-25 17:30:30 -0700385
386def mod_l3_ecmp_group(ctrl, id, l3_ucast_groups):
387 buckets=[]
388 for group in l3_ucast_groups:
389 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
390
391 group_id =encode_l3_ecmp_group_id(id)
392 request = ofp.message.group_modify(group_type=ofp.OFPGT_SELECT,
393 group_id=group_id,
394 buckets=buckets
395 )
396 ctrl.message_send(request)
397 return request
398
macauley15909e72015-07-17 15:58:57 +0800399def add_l3_mcast_group(ctrl, vid, mcast_group_id, groups_on_buckets):
400 buckets=[]
401 for group in groups_on_buckets:
402 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
Pierbbdf3782016-08-22 17:58:26 -0700403
macauley15909e72015-07-17 15:58:57 +0800404 group_id =encode_l3_mcast_group_id(vid, mcast_group_id)
405 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
406 group_id=group_id,
407 buckets=buckets
408 )
409 ctrl.message_send(request)
410 return request
macauleyfddc4662015-07-27 17:40:30 +0800411
412def add_l2_overlay_flood_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
413 buckets=[]
414 for port in ports:
415 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
416
417 group_id=encode_l2_overlay_group_id(tunnel_id, 0, index)
418 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
419 group_id=group_id,
420 buckets=buckets
421 )
422 ctrl.message_send(request)
423 return request
424
425def add_l2_overlay_flood_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
426 buckets=[]
427 for port in ports:
428 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
429
430 group_id=encode_l2_overlay_group_id(tunnel_id, 1, index)
431 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
432 group_id=group_id,
433 buckets=buckets
434 )
435 ctrl.message_send(request)
436 return request
437
438def add_l2_overlay_mcast_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
439 buckets=[]
440 for port in ports:
441 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
442
443 group_id=encode_l2_overlay_group_id(tunnel_id, 2, index)
444 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
445 group_id=group_id,
446 buckets=buckets
447 )
448 ctrl.message_send(request)
449 return request
450
451def add_l2_overlay_mcast_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
452 buckets=[]
453 for port in ports:
454 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
455
456 group_id=encode_l2_overlay_group_id(tunnel_id, 3, index)
457 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
458 group_id=group_id,
459 buckets=buckets
460 )
461 ctrl.message_send(request)
462 return request
Pierbbdf3782016-08-22 17:58:26 -0700463
macauleyfddc4662015-07-27 17:40:30 +0800464def add_port_table_flow(ctrl, is_overlay=True):
465 match = ofp.match()
466
467 if is_overlay == True:
468 match.oxm_list.append(ofp.oxm.in_port(0x10000))
macauleydbff3272015-07-30 14:07:16 +0800469 NEXT_TABLE=50
macauleyfddc4662015-07-27 17:40:30 +0800470 else:
471 match.oxm_list.append(ofp.oxm.in_port(0))
Pierbbdf3782016-08-22 17:58:26 -0700472 NEXT_TABLE=10
macauleyfddc4662015-07-27 17:40:30 +0800473
474 request = ofp.message.flow_add(
475 table_id=0,
476 cookie=42,
477 match=match,
478 instructions=[
macauleydbff3272015-07-30 14:07:16 +0800479 ofp.instruction.goto_table(NEXT_TABLE)
macauleyfddc4662015-07-27 17:40:30 +0800480 ],
481 priority=0)
482 logging.info("Add port table, match port %lx" % 0x10000)
483 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700484
Flavio Castrod8f8af22015-12-02 18:19:26 -0500485def pop_vlan_flow(ctrl, ports, vlan_id=1):
486 # table 10: vlan
487 # goto to table 20
488 msgs=[]
489 for of_port in ports:
490 match = ofp.match()
491 match.oxm_list.append(ofp.oxm.in_port(of_port))
492 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
493 request = ofp.message.flow_add(
494 table_id=10,
495 cookie=42,
496 match=match,
497 instructions=[
498 ofp.instruction.apply_actions(
499 actions=[
500 ofp.action.pop_vlan()
501 ]
502 ),
503 ofp.instruction.goto_table(20)
504 ],
505 priority=0)
506 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
507 ctrl.message_send(request)
508
509
510 return msgs
macauleyfddc4662015-07-27 17:40:30 +0800511
macauley97557232015-07-16 17:28:07 +0800512def add_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
513 # table 10: vlan
514 # goto to table 20
macauley15909e72015-07-17 15:58:57 +0800515 msgs=[]
macauley97557232015-07-16 17:28:07 +0800516 for of_port in ports:
Flavio Castro932014b2016-01-05 18:29:15 -0500517 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
macauley97557232015-07-16 17:28:07 +0800518 match = ofp.match()
519 match.oxm_list.append(ofp.oxm.in_port(of_port))
520 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
521 request = ofp.message.flow_add(
522 table_id=10,
523 cookie=42,
524 match=match,
525 instructions=[
Flavio Castrod8f8af22015-12-02 18:19:26 -0500526 ofp.instruction.apply_actions(
527 actions=[
528 ofp.action.pop_vlan()
529 ]
530 ),
macauley97557232015-07-16 17:28:07 +0800531 ofp.instruction.goto_table(20)
532 ],
533 priority=0)
534 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
535 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700536
macauley97557232015-07-16 17:28:07 +0800537 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
538 match = ofp.match()
539 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500540 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
macauley97557232015-07-16 17:28:07 +0800541 request = ofp.message.flow_add(
542 table_id=10,
543 cookie=42,
544 match=match,
545 instructions=[
546 ofp.instruction.apply_actions(
547 actions=[
Flavio Castroaf2b4502016-02-02 17:41:32 -0500548 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
macauley97557232015-07-16 17:28:07 +0800549 ]
550 ),
551 ofp.instruction.goto_table(20)
552 ],
553 priority=0)
554 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
555 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800556 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800557
Flavio Castro932014b2016-01-05 18:29:15 -0500558 if (flag == 4) :
559 match = ofp.match()
560 match.oxm_list.append(ofp.oxm.in_port(of_port))
561 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1fff))
562 request = ofp.message.flow_add(
563 table_id=10,
564 cookie=42,
565 match=match,
566 instructions=[
567 ofp.instruction.apply_actions(
568 actions=[
Flavio Castroaf2b4502016-02-02 17:41:32 -0500569 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
Flavio Castro932014b2016-01-05 18:29:15 -0500570 ]
571 ),
572 ofp.instruction.goto_table(20)
573 ],
574 priority=0)
575 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
576 ctrl.message_send(request)
577 msgs.append(request)
578
macauley97557232015-07-16 17:28:07 +0800579 if send_barrier:
580 do_barrier(ctrl)
581
macauley15909e72015-07-17 15:58:57 +0800582 return msgs
Pierbbdf3782016-08-22 17:58:26 -0700583
macauley_cheng6e6a6122015-11-16 14:19:18 +0800584def del_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
585 # table 10: vlan
586 # goto to table 20
587 msgs=[]
588 for of_port in ports:
589 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
590 match = ofp.match()
591 match.oxm_list.append(ofp.oxm.in_port(of_port))
592 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
593 request = ofp.message.flow_delete(
594 table_id=10,
595 cookie=42,
596 match=match,
597 priority=0)
598 logging.info("Del vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
599 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800600
macauley_cheng6e6a6122015-11-16 14:19:18 +0800601 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
602 match = ofp.match()
603 match.oxm_list.append(ofp.oxm.in_port(of_port))
604 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0xfff))
605 request = ofp.message.flow_delete(
606 table_id=10,
607 cookie=42,
608 match=match,
609 priority=0)
610 logging.info("Del vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
611 ctrl.message_send(request)
612 msgs.append(request)
613
614 if send_barrier:
615 do_barrier(ctrl)
616
617 return msgs
Pierbbdf3782016-08-22 17:58:26 -0700618
macauley_cheng6b311612015-09-04 11:32:27 +0800619def add_vlan_table_flow_pvid(ctrl, in_port, match_vid=None, pvid=1, send_barrier=False):
620 """it will tag pack as untagged packet wether it has tagg or not"""
621 match = ofp.match()
622 match.oxm_list.append(ofp.oxm.in_port(in_port))
623 actions=[]
624 if match_vid == None:
Pierbbdf3782016-08-22 17:58:26 -0700625 match.oxm_list.append(ofp.oxm.vlan_vid(0))
macauley_cheng6b311612015-09-04 11:32:27 +0800626 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
627 goto_table=20
628 else:
629 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+match_vid, 0x1fff))
630 actions.append(ofp.action.push_vlan(0x8100))
Pierbbdf3782016-08-22 17:58:26 -0700631 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
macauley_cheng6b311612015-09-04 11:32:27 +0800632 goto_table=20
Pierbbdf3782016-08-22 17:58:26 -0700633
macauley_cheng6b311612015-09-04 11:32:27 +0800634 request = ofp.message.flow_add(
635 table_id=10,
636 cookie=42,
637 match=match,
638 instructions=[
639 ofp.instruction.apply_actions(actions=actions)
640 ,ofp.instruction.goto_table(goto_table)
641 ],
642 priority=0)
643 logging.info("Add PVID %d on port %d and go to table %ld" %( pvid, in_port, goto_table))
Pierbbdf3782016-08-22 17:58:26 -0700644 ctrl.message_send(request)
645
macauley_cheng6b311612015-09-04 11:32:27 +0800646 if send_barrier:
647 do_barrier(ctrl)
648
649def add_vlan_table_flow_allow_all_vlan(ctrl, in_port, send_barrier=False):
650 """it st flow allow all vlan tag on this port"""
651 match = ofp.match()
652 match.oxm_list.append(ofp.oxm.in_port(in_port))
653 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1000))
654 request = ofp.message.flow_add(
655 table_id=10,
656 cookie=42,
657 match=match,
658 instructions=[
Pierbbdf3782016-08-22 17:58:26 -0700659 ofp.instruction.goto_table(20)
macauley_cheng6b311612015-09-04 11:32:27 +0800660 ],
661 priority=0)
662 logging.info("Add allow all vlan on port %d " %(in_port))
Pierbbdf3782016-08-22 17:58:26 -0700663 ctrl.message_send(request)
macauley_cheng6b311612015-09-04 11:32:27 +0800664
Pier7b031af2016-08-25 15:00:22 -0700665def 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):
666 # Install a flow for VLAN translation
667 # in VLAN table.
668 # table 10: vlan
669 # goto to table 20
670 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
671 match = ofp.match()
672 match.oxm_list.append(ofp.oxm.in_port(of_port))
673 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
674
675 actions=[]
676 if vrf!=0:
677 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
678 if new_vlan_id != -1:
679 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_vlan_id)))
680
681 request = ofp.message.flow_add(
682 table_id=10,
683 cookie=42,
684 match=match,
685 instructions=[
686 ofp.instruction.apply_actions(
687 actions=actions
688 ),
689 ofp.instruction.goto_table(20)
690 ],
691 priority=0)
692 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
693 ctrl.message_send(request)
694
Piere1308762016-09-12 15:29:56 -0700695def 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 +0800696 # table 10: vlan
697 # goto to table 20
Flavio Castro932014b2016-01-05 18:29:15 -0500698 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
macauley0f91a3e2015-07-17 18:09:59 +0800699 match = ofp.match()
700 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500701 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
macauley7f89d962015-08-06 18:13:48 +0800702
703 actions=[]
704 if vrf!=0:
Pier23784aa2016-09-19 20:08:21 -0700705 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
Pierbbdf3782016-08-22 17:58:26 -0700706
Flavio Castro6d498522015-12-15 14:05:04 -0500707 #actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id)))
macauley7f89d962015-08-06 18:13:48 +0800708
macauley0f91a3e2015-07-17 18:09:59 +0800709 request = ofp.message.flow_add(
710 table_id=10,
711 cookie=42,
712 match=match,
713 instructions=[
macauley53d90fe2015-08-04 17:34:22 +0800714 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800715 actions=actions
macauley53d90fe2015-08-04 17:34:22 +0800716 ),
717 ofp.instruction.goto_table(20)
macauley0f91a3e2015-07-17 18:09:59 +0800718 ],
719 priority=0)
720 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
721 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700722
macauley0f91a3e2015-07-17 18:09:59 +0800723 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
724 match = ofp.match()
725 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500726 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
Pierbbdf3782016-08-22 17:58:26 -0700727
macauley7f89d962015-08-06 18:13:48 +0800728 actions=[]
729 if vrf!=0:
730 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
Pierbbdf3782016-08-22 17:58:26 -0700731
Flavio Castro91d1a552016-05-17 16:59:44 -0700732 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Pierbbdf3782016-08-22 17:58:26 -0700733
macauley0f91a3e2015-07-17 18:09:59 +0800734 request = ofp.message.flow_add(
735 table_id=10,
736 cookie=42,
737 match=match,
738 instructions=[
739 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800740 actions=actions
macauley0f91a3e2015-07-17 18:09:59 +0800741 ),
742 ofp.instruction.goto_table(20)
743 ],
744 priority=0)
745 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
746 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700747
Flavio Castro932014b2016-01-05 18:29:15 -0500748 if (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,0x1fff))
752
753 actions=[]
754 if vrf!=0:
755 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
756
Flavio Castro91d1a552016-05-17 16:59:44 -0700757 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Flavio Castro932014b2016-01-05 18:29:15 -0500758
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)
macauley0f91a3e2015-07-17 18:09:59 +0800772
Piere1308762016-09-12 15:29:56 -0700773 if (flag == VLAN_TABLE_FLAG_ONLY_STACKED):
774 # This flag is meant to managed stacked vlan packtes
775 # Matches on outer VLAN_ID, set OVID with outer VLAN.
776 # Finally expose inner VLAN_ID with a pop action and
777 # goto VLAN_1_FLOW_TABLE
778 match = ofp.match()
779 match.oxm_list.append(ofp.oxm.in_port(of_port))
780 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
781
782 actions=[]
783 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_OVID, value=0x1000+vlan_id)))
784 actions.append(ofp.action.pop_vlan())
785
786 request = ofp.message.flow_add(
787 table_id=10,
788 cookie=42,
789 match=match,
790 instructions=[
791 ofp.instruction.apply_actions(
792 actions=actions
793 ),
794 ofp.instruction.goto_table(VLAN_1_FLOW_TABLE)
795 ],
796 priority=0)
797 logging.info("Add vlan %d tagged packets on port %d and go to table %d" %( vlan_id, of_port, VLAN_1_FLOW_TABLE))
798 ctrl.message_send(request)
799
800 if (flag == VLAN_TABLE_FLAG_PRIORITY) :
801 match = ofp.match()
802 match.oxm_list.append(ofp.oxm.in_port(of_port))
803 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1fff))
804 request = ofp.message.flow_add(
805 table_id=10,
806 cookie=42,
807 match=match,
808 instructions=[
809 ofp.instruction.apply_actions(
810 actions=[
811 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)),
812 ofp.action.push_vlan(0x8100),
813 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+out_vlan_id)),
814 ]
815 ),
816 ofp.instruction.goto_table(20)
817 ],
818 priority=0)
819 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
820 ctrl.message_send(request)
821
822 if send_barrier:
823 do_barrier(ctrl)
824
825 return request
826
Piercf76e802016-09-19 20:16:35 -0700827def 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):
828 # table 10: vlan
829 # goto to table 13
830 if flag == VLAN_TABLE_FLAG_ONLY_TAG:
831 match = ofp.match()
832 match.oxm_list.append(ofp.oxm.in_port(of_port))
833 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id, 0x1fff))
834
835 actions=[]
836 if vlan_id == -1:
837 actions.append(ofp.action.pop_vlan())
838 if new_vlan_id > 1:
839 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_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 tagged 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 flag == VLAN_TABLE_FLAG_ONLY_UNTAG:
860 match = ofp.match()
861 match.oxm_list.append(ofp.oxm.in_port(of_port))
862 match.oxm_list.append(ofp.oxm.vlan_vid(0))
863
864 actions=[]
865 if vlan_id > 1:
866 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
867 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
868 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
869 # 0x0000nnnn is for UNI interfaces
870 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
871
872 request = ofp.message.flow_add(
873 table_id=10,
874 cookie=42,
875 match=match,
876 instructions=[
877 ofp.instruction.apply_actions(
878 actions=actions
879 ),
880 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
881 ],
882 priority=0)
883 logging.info("Add vlan %d untagged packets on port %d and go to table %d" % (vlan_id, of_port, MPLS_L2_PORT_FLOW_TABLE))
884 ctrl.message_send(request)
885
886 if send_barrier:
887 do_barrier(ctrl)
888
889 return request
890
Piere1308762016-09-12 15:29:56 -0700891def 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):
892 # table 11: vlan 1 table
893 # goto to table 20
894 if flag == VLAN_TABLE_FLAG_ONLY_TAG:
895 match = ofp.match()
896 match.oxm_list.append(ofp.oxm.in_port(of_port))
897 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+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 actions.append(ofp.action.push_vlan(0x8100))
902 if new_outer_vlan_id != -1:
903 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_outer_vlan_id)))
904 else:
905 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan_id)))
906
907 request = ofp.message.flow_add(
908 table_id=11,
909 cookie=42,
910 match=match,
911 instructions=[
912 ofp.instruction.apply_actions(
913 actions=actions
914 ),
915 ofp.instruction.goto_table(TERMINATION_FLOW_TABLE)
916 ],
917 priority=0)
918 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))
919 ctrl.message_send(request)
920
921 if flag == VLAN_TABLE_FLAG_ONLY_UNTAG:
922 match = ofp.match()
923 match.oxm_list.append(ofp.oxm.in_port(of_port))
924 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
925 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
926
927 actions=[]
928 request = ofp.message.flow_add(
929 table_id=11,
930 cookie=42,
931 match=match,
932 instructions=[
933 ofp.instruction.apply_actions(
934 actions=actions
935 ),
936 ofp.instruction.goto_table(TERMINATION_FLOW_TABLE)
937 ],
938 priority=0)
939 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))
940 ctrl.message_send(request)
941
macauley0f91a3e2015-07-17 18:09:59 +0800942 if send_barrier:
943 do_barrier(ctrl)
944
945 return request
Pierbbdf3782016-08-22 17:58:26 -0700946
Piercf76e802016-09-19 20:16:35 -0700947def 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):
948 # table 11: vlan 1 table
949 # goto to table 13
950 match = ofp.match()
951 match.oxm_list.append(ofp.oxm.in_port(of_port))
952 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
953 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
954
955 actions=[]
956 actions.append(ofp.action.push_vlan(0x8100))
957 if new_outer_vlan_id != -1:
958 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_outer_vlan_id)))
959 else:
960 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan_id)))
961
962 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
963 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
964 # 0x0000nnnn is for UNI interfaces
965 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
966
967 request = ofp.message.flow_add(
968 table_id=11,
969 cookie=42,
970 match=match,
971 instructions=[
972 ofp.instruction.apply_actions(
973 actions=actions
974 ),
975 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
976 ],
977 priority=0)
978 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))
979 ctrl.message_send(request)
980
981 if send_barrier:
982 do_barrier(ctrl)
983
984 return request
985
macauley97557232015-07-16 17:28:07 +0800986def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False):
987 match = ofp.match()
castroflavio21894482015-12-08 15:29:55 -0500988 priority=500
macauleyfddc4662015-07-27 17:40:30 +0800989 if dst_mac!=None:
castroflavio21894482015-12-08 15:29:55 -0500990 priority=1000
macauleyfddc4662015-07-27 17:40:30 +0800991 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
992
macauley97557232015-07-16 17:28:07 +0800993 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauleyfddc4662015-07-27 17:40:30 +0800994
macauley97557232015-07-16 17:28:07 +0800995 request = ofp.message.flow_add(
996 table_id=50,
997 cookie=42,
998 match=match,
999 instructions=[
1000 ofp.instruction.write_actions(
1001 actions=[
1002 ofp.action.group(group_id)]),
1003 ofp.instruction.goto_table(60)
1004 ],
1005 buffer_id=ofp.OFP_NO_BUFFER,
castroflavio21894482015-12-08 15:29:55 -05001006 priority=priority)
macauley97557232015-07-16 17:28:07 +08001007
1008 logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac)
1009 ctrl.message_send(request)
1010
1011 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001012 do_barrier(ctrl)
macauley15909e72015-07-17 15:58:57 +08001013
Pierbbdf3782016-08-22 17:58:26 -07001014 return request
macauleyfddc4662015-07-27 17:40:30 +08001015
1016def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False):
1017 match = ofp.match()
1018 if dst_mac!=None:
1019 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
1020
1021 match.oxm_list.append(ofp.oxm.tunnel_id(vnid))
1022 if is_group == True:
1023 actions=[ofp.action.group(group_id)]
1024 else:
1025 actions=[ofp.action.output(group_id)]
1026
1027 request = ofp.message.flow_add(
1028 table_id=50,
1029 cookie=42,
1030 match=match,
1031 instructions=[
1032 ofp.instruction.write_actions(
1033 actions=actions),
1034 ofp.instruction.goto_table(60)
1035 ],
1036 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001037 priority=1000)
macauleyfddc4662015-07-27 17:40:30 +08001038
1039 logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac)
1040 ctrl.message_send(request)
1041
1042 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001043 do_barrier(ctrl)
macauleyfddc4662015-07-27 17:40:30 +08001044
Pierbbdf3782016-08-22 17:58:26 -07001045 return request
1046
macauley_cheng6b133662015-11-09 13:52:39 +08001047def add_termination_flow(ctrl, in_port, eth_type, dst_mac, vlanid, goto_table=None, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +08001048 match = ofp.match()
macauley0f91a3e2015-07-17 18:09:59 +08001049 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauleyfddc4662015-07-27 17:40:30 +08001050 if dst_mac[0]&0x01 == 0x01:
1051 match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00]))
1052 goto_table=40
1053 else:
macauley53d90fe2015-08-04 17:34:22 +08001054 if in_port!=0:
1055 match.oxm_list.append(ofp.oxm.in_port(in_port))
macauleyfddc4662015-07-27 17:40:30 +08001056 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
1057 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauley_cheng6b133662015-11-09 13:52:39 +08001058 if goto_table == None:
1059 goto_table=30
macauley0f91a3e2015-07-17 18:09:59 +08001060
1061 request = ofp.message.flow_add(
1062 table_id=20,
1063 cookie=42,
1064 match=match,
1065 instructions=[
1066 ofp.instruction.goto_table(goto_table)
1067 ],
1068 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001069 priority=1)
macauley0f91a3e2015-07-17 18:09:59 +08001070
1071 logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac)
1072 ctrl.message_send(request)
1073
1074 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001075 do_barrier(ctrl)
macauley0f91a3e2015-07-17 18:09:59 +08001076
Pierbbdf3782016-08-22 17:58:26 -07001077 return request
1078
Flavio Castroaf2b4502016-02-02 17:41:32 -05001079def 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 +08001080 match = ofp.match()
1081 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauley53d90fe2015-08-04 17:34:22 +08001082 if vrf != 0:
1083 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
macauley0f91a3e2015-07-17 18:09:59 +08001084
Flavio Castroaf2b4502016-02-02 17:41:32 -05001085 match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask))
1086
1087 instructions = []
1088 instructions.append(ofp.instruction.goto_table(60))
1089 if send_ctrl:
Piere0918ec2016-09-09 20:06:05 -07001090 instructions.append(ofp.instruction.apply_actions(
Flavio Castroaf2b4502016-02-02 17:41:32 -05001091 actions=[ofp.action.output( port=ofp.OFPP_CONTROLLER,
1092 max_len=ofp.OFPCML_NO_BUFFER)]))
Pierbbdf3782016-08-22 17:58:26 -07001093 else:
Flavio Castroaf2b4502016-02-02 17:41:32 -05001094 instructions.append(ofp.instruction.write_actions(
1095 actions=[ofp.action.group(action_group_id)]))
macauley53d90fe2015-08-04 17:34:22 +08001096
macauley0f91a3e2015-07-17 18:09:59 +08001097 request = ofp.message.flow_add(
1098 table_id=30,
1099 cookie=42,
1100 match=match,
Flavio Castroaf2b4502016-02-02 17:41:32 -05001101 instructions=instructions,
macauley0f91a3e2015-07-17 18:09:59 +08001102 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001103 priority=1)
macauley0f91a3e2015-07-17 18:09:59 +08001104
1105 logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip)
1106 ctrl.message_send(request)
1107
1108 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001109 do_barrier(ctrl)
macauley0f91a3e2015-07-17 18:09:59 +08001110
Flavio Castro9debaaa2016-07-26 19:37:50 -07001111 return request
Flavio Castrod8f8af22015-12-02 18:19:26 -05001112
Pier1e4e98e2016-10-26 14:36:05 -07001113def add_unicast_v6_routing_flow(ctrl, eth_type, dst_ip, mask, action_group_id, vrf=0, send_ctrl=False, send_barrier=False):
1114 match = ofp.match()
1115 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1116 if vrf != 0:
1117 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
1118
1119 match.oxm_list.append(ofp.oxm.ipv6_dst_masked(parse_ipv6(dst_ip), parse_ipv6(mask)))
1120
1121 instructions = []
1122 instructions.append(ofp.instruction.goto_table(60))
1123 if send_ctrl:
1124 instructions.append(ofp.instruction.apply_actions(
1125 actions=[ofp.action.output( port=ofp.OFPP_CONTROLLER,
1126 max_len=ofp.OFPCML_NO_BUFFER)]))
1127 else:
1128 instructions.append(ofp.instruction.write_actions(
1129 actions=[ofp.action.group(action_group_id)]))
1130
1131 request = ofp.message.flow_add(
1132 table_id=30,
1133 cookie=42,
1134 match=match,
1135 instructions=instructions,
1136 buffer_id=ofp.OFP_NO_BUFFER,
1137 priority=1)
1138
1139 logging.info("Inserting unicast routing flow eth_type %lx, dip %s",eth_type, dst_ip)
1140 ctrl.message_send(request)
1141
1142 if send_barrier:
1143 do_barrier(ctrl)
1144
1145 return request
1146
Flavio Castro8ca52542016-04-11 11:24:49 -04001147def 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 -04001148 match = ofp.match()
1149 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1150 match.oxm_list.append(ofp.oxm.mpls_label(label))
1151 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
Pierb5da4c92016-09-21 11:23:35 -07001152 actions = []
Flavio Castrob702a2f2016-04-10 22:01:48 -04001153 actions = [ofp.action.dec_mpls_ttl(),
Flavio Castro8ca52542016-04-11 11:24:49 -04001154 ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))]
1155 if (goto_table == 29):
Flavio Castro8ca52542016-04-11 11:24:49 -04001156 actions.append(ofp.action.group(action_group_id))
1157 else:
Flavio Castrod80fbc32016-07-25 15:54:26 -07001158 actions.append(ofp.action.set_field(
1159 ofp.oxm.exp2ByteValue(exp_type=23, value=32)))
1160 actions.append(ofp.action.group(action_group_id))
Flavio Castro8ca52542016-04-11 11:24:49 -04001161 actions.append(ofp.action.copy_ttl_in())
Flavio Castro9debaaa2016-07-26 19:37:50 -07001162
Flavio Castrob702a2f2016-04-10 22:01:48 -04001163 request = ofp.message.flow_add(
1164 table_id=24,
1165 cookie=43,
1166 match=match,
1167 instructions=[
Flavio Castro8ca52542016-04-11 11:24:49 -04001168 ofp.instruction.apply_actions(actions=actions),
1169 ofp.instruction.goto_table(goto_table)
Flavio Castrob702a2f2016-04-10 22:01:48 -04001170 ],
1171 buffer_id=ofp.OFP_NO_BUFFER,
1172 priority=1)
Flavio Castrob702a2f2016-04-10 22:01:48 -04001173 logging.info("Inserting MPLS flow , label %ld", label)
1174 ctrl.message_send(request)
1175
1176 if send_barrier:
1177 do_barrier(ctrl)
1178
1179 return request
1180
Pierf6f28162016-09-22 16:30:52 -07001181def 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 -07001182 match = ofp.match()
1183 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1184 match.oxm_list.append(ofp.oxm.mpls_label(label))
1185 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
1186
1187 actions = []
1188 actions.append(ofp.action.dec_mpls_ttl())
Pierf6f28162016-09-22 16:30:52 -07001189 if popMPLS == True:
Pierb5da4c92016-09-21 11:23:35 -07001190 actions.append(ofp.action.copy_ttl_in())
1191 actions.append(ofp.action.pop_mpls(ethertype))
Pierf6f28162016-09-22 16:30:52 -07001192 if bos==1 and popL2 == True:
1193 actions.append(ofp.action.ofdpa_pop_l2_header())
1194 actions.append(ofp.action.ofdpa_pop_cw())
1195 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
1196 # 0x0002nnnn is for UNI interfaces
1197 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00020000 + of_port)))
1198 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
Pierb5da4c92016-09-21 11:23:35 -07001199 actions.append(ofp.action.group(action_group_id))
1200
1201 request = ofp.message.flow_add(
1202 table_id=24,
1203 cookie=43,
1204 match=match,
1205 instructions=[
1206 ofp.instruction.apply_actions(actions=actions),
1207 ofp.instruction.goto_table(goto_table)
1208 ],
1209 buffer_id=ofp.OFP_NO_BUFFER,
1210 priority=1
1211 )
1212 logging.info("Inserting MPLS flow , label %ld", label)
1213 ctrl.message_send(request)
1214
1215 if send_barrier:
1216 do_barrier(ctrl)
1217
1218 return request
1219
macauleyfddc4662015-07-27 17:40:30 +08001220def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False):
1221 match = ofp.match()
1222 match.oxm_list.append(ofp.oxm.eth_type(0x0800))
Pierbbdf3782016-08-22 17:58:26 -07001223 match.oxm_list.append(ofp.oxm.vlan_vid(vlan_id))
macauleyfddc4662015-07-27 17:40:30 +08001224 if src_ip_mask!=0:
1225 match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask))
1226 else:
1227 match.oxm_list.append(ofp.oxm.ipv4_src(src_ip))
Pierbbdf3782016-08-22 17:58:26 -07001228
macauleyfddc4662015-07-27 17:40:30 +08001229 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
Pierbbdf3782016-08-22 17:58:26 -07001230
macauleyfddc4662015-07-27 17:40:30 +08001231 request = ofp.message.flow_add(
1232 table_id=40,
1233 cookie=42,
1234 match=match,
1235 instructions=[
1236 ofp.instruction.write_actions(
1237 actions=[ofp.action.group(action_group_id)]),
1238 ofp.instruction.goto_table(60)
1239 ],
1240 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001241 priority=1)
macauleyfddc4662015-07-27 17:40:30 +08001242
1243 logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask)
1244 ctrl.message_send(request)
1245
1246 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001247 do_barrier(ctrl)
macauleyfddc4662015-07-27 17:40:30 +08001248
Pierbbdf3782016-08-22 17:58:26 -07001249 return request
macauley_cheng6b133662015-11-09 13:52:39 +08001250
Pier1e4e98e2016-10-26 14:36:05 -07001251def add_acl_rule(ctrl, eth_type=None, ip_proto=None, send_barrier=False):
1252 match = ofp.match()
1253 if eth_type != None:
1254 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1255 if ip_proto != None:
1256 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1257
1258 request = ofp.message.flow_add(
1259 table_id=60,
1260 cookie=42,
1261 match=match,
1262 instructions=[
1263 ofp.instruction.apply_actions(
1264 actions=[ofp.action.output(port=ofp.OFPP_CONTROLLER, max_len=ofp.OFPCML_NO_BUFFER)]
1265 ),
1266 ],
1267 buffer_id=ofp.OFP_NO_BUFFER,
1268 priority=1
1269 )
1270
1271 logging.info("Inserting ACL flow eth_type %lx, ip_proto %ld", eth_type, ip_proto)
1272 ctrl.message_send(request)
1273
1274 if send_barrier:
1275 do_barrier(ctrl)
1276
1277 return request
1278
macauley_cheng6b133662015-11-09 13:52:39 +08001279#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 +08001280def 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 +08001281 match = ofp.match()
1282 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1283 match.oxm_list.append(ofp.oxm.ipv4_dst(ip_dst))
1284 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1285 match.oxm_list.append(ofp.oxm.tcp_dst(tcp_dst))
Pierbbdf3782016-08-22 17:58:26 -07001286
macauley_cheng6b133662015-11-09 13:52:39 +08001287 request = ofp.message.flow_add(
1288 table_id=28,
1289 cookie=42,
1290 match=match,
1291 instructions=[
1292 ofp.instruction.write_actions(
1293 actions=[ofp.action.set_field(ofp.oxm.ipv4_dst(set_ip_dst)),
1294 ofp.action.set_field(ofp.oxm.tcp_dst(set_tcp_dst)),
1295 ofp.action.group(action_group_id)]),
1296 ofp.instruction.goto_table(60)
1297 ],
1298 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001299 priority=1)
macauley_cheng6b133662015-11-09 13:52:39 +08001300 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)
1301 ctrl.message_send(request)
1302 return request
macauley_chengeffc20a2015-11-09 16:14:56 +08001303
1304#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
1305def add_snat_flow(ctrl, eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src):
1306 match = ofp.match()
1307 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1308 match.oxm_list.append(ofp.oxm.ipv4_src(ip_src))
1309 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1310 match.oxm_list.append(ofp.oxm.tcp_src(tcp_src))
Pierbbdf3782016-08-22 17:58:26 -07001311
macauley_chengeffc20a2015-11-09 16:14:56 +08001312 request = ofp.message.flow_add(
1313 table_id=29,
1314 cookie=42,
1315 match=match,
1316 instructions=[
1317 ofp.instruction.write_actions(
1318 actions=[ofp.action.set_field(ofp.oxm.ipv4_src(set_ip_src)),
1319 ofp.action.set_field(ofp.oxm.tcp_src(set_tcp_src))]),
1320 ofp.instruction.goto_table(30)
1321 ],
1322 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001323 priority=1)
macauley_chengeffc20a2015-11-09 16:14:56 +08001324 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)
1325 ctrl.message_send(request)
1326 return request
Pierbbdf3782016-08-22 17:58:26 -07001327
1328def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001329 """
1330 Command Example:
1331 of-agent vtap 10001 ethernet 1/1 vid 1
1332 of-agent vtp 10001 vni 10
1333 """
1334 if vlan != 0:
1335 config_vtap_xml="""
1336 <config>
1337 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1338 <id>capable-switch-1</id>
1339 <resources>
1340 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001341 <resource-id >LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001342 <features>
1343 <current>
1344 <rate>10Gb</rate>
1345 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001346 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001347 </current>
1348 <advertised>
1349 <rate>10Gb</rate>
1350 <rate>100Gb</rate>
1351 <medium>fiber</medium>
1352 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001353 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001354 <supported>
1355 <rate>10Gb</rate>
1356 <rate>100Gb</rate>
1357 <medium>fiber</medium>
1358 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001359 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001360 <advertised-peer>
1361 <rate>10Gb</rate>
1362 <rate>100Gb</rate>
1363 <medium>fiber</medium>
1364 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001365 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001366 </features>
1367 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1368 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1369 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1370 <ofdpa10:vni>VNID</ofdpa10:vni>
1371 </ofdpa10:vtap>
Pierbbdf3782016-08-22 17:58:26 -07001372 </port>
macauleyfddc4662015-07-27 17:40:30 +08001373 </resources>
1374 <logical-switches>
1375 <switch>
1376 <id>DATAPATH_ID</id>
1377 <datapath-id>DATAPATH_ID</datapath-id>
1378 <resources>
1379 <port xc:operation="OPERATION">LPORT</port>
1380 </resources>
1381 </switch>
1382 </logical-switches>
1383 </capable-switch>
1384 </config>
Pierbbdf3782016-08-22 17:58:26 -07001385 """
macauleyfddc4662015-07-27 17:40:30 +08001386 else:
1387 config_vtap_xml="""
1388 <config>
1389 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1390 <id>capable-switch-1</id>
1391 <resources>
1392 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001393 <resource-id >LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001394 <features>
1395 <current>
1396 <rate>10Gb</rate>
1397 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001398 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001399 </current>
1400 <advertised>
1401 <rate>10Gb</rate>
1402 <rate>100Gb</rate>
1403 <medium>fiber</medium>
1404 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001405 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001406 <supported>
1407 <rate>10Gb</rate>
1408 <rate>100Gb</rate>
1409 <medium>fiber</medium>
1410 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001411 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001412 <advertised-peer>
1413 <rate>10Gb</rate>
1414 <rate>100Gb</rate>
1415 <medium>fiber</medium>
1416 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001417 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001418 </features>
1419 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1420 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1421 <ofdpa10:vni>VNID</ofdpa10:vni>
1422 </ofdpa10:vtap>
Pierbbdf3782016-08-22 17:58:26 -07001423 </port>
macauleyfddc4662015-07-27 17:40:30 +08001424 </resources>
1425 <logical-switches>
1426 <switch>
1427 <id>DATAPATH_ID</id>
1428 <datapath-id>DATAPATH_ID</datapath-id>
1429 <resources>
1430 <port xc:operation="OPERATION">LPORT</port>
1431 </resources>
1432 </switch>
1433 </logical-switches>
1434 </capable-switch>
1435 </config>
Pierbbdf3782016-08-22 17:58:26 -07001436 """
1437 str_datapath_id_f= "{:016x}".format(dp_id)
1438 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1439 config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id)
1440 config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport)))
1441 config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port))
1442 config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan))
macauleyfddc4662015-07-27 17:40:30 +08001443 config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid))
1444 config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation))
1445 return config_vtap_xml
Pierbbdf3782016-08-22 17:58:26 -07001446
1447def 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 +08001448 """
1449 Command Example:
1450 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 -07001451 of-agent vtp 10001 vni 10
macauleyfddc4662015-07-27 17:40:30 +08001452 """
1453
1454 config_vtep_xml="""
1455 <config>
1456 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1457 <id>capable-switch-1</id>
1458 <resources>
1459 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001460 <resource-id>LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001461 <features>
1462 <current>
1463 <rate>10Gb</rate>
1464 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001465 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001466 </current>
1467 <advertised>
1468 <rate>10Gb</rate>
1469 <rate>100Gb</rate>
1470 <medium>fiber</medium>
1471 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001472 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001473 <supported>
1474 <rate>10Gb</rate>
1475 <rate>100Gb</rate>
1476 <medium>fiber</medium>
1477 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001478 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001479 <advertised-peer>
1480 <rate>10Gb</rate>
1481 <rate>100Gb</rate>
1482 <medium>fiber</medium>
1483 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001484 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001485 </features>
1486 <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01">
1487 <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip>
1488 <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip>
1489 <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port>
macauley25999cf2015-08-07 17:03:24 +08001490 <ofdpa10:vni xc:operation="OPERATION">
1491 <ofdpa10:id>VNID</ofdpa10:id>
1492 </ofdpa10:vni>
macauleyfddc4662015-07-27 17:40:30 +08001493 <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id>
1494 <ofdpa10:ttl>TTL</ofdpa10:ttl>
1495 </ofdpa10:vtep>
Pierbbdf3782016-08-22 17:58:26 -07001496 </port>
macauleyfddc4662015-07-27 17:40:30 +08001497 </resources>
1498 <logical-switches>
1499 <switch>
1500 <id>DATAPATH_ID</id>
1501 <datapath-id>DATAPATH_ID</datapath-id>
1502 <resources>
1503 <port xc:operation="OPERATION">LPORT</port>
1504 </resources>
1505 </switch>
1506 </logical-switches>
1507 </capable-switch>
Pierbbdf3782016-08-22 17:58:26 -07001508 </config>
macauleyfddc4662015-07-27 17:40:30 +08001509 """
Pierbbdf3782016-08-22 17:58:26 -07001510 str_datapath_id_f= "{:016x}".format(dp_id)
1511 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1512 config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +08001513 config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport)))
Pierbbdf3782016-08-22 17:58:26 -07001514 config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip))
1515 config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip))
1516 config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port))
1517 config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1518 config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl))
macauleyfddc4662015-07-27 17:40:30 +08001519 config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid))
Pierbbdf3782016-08-22 17:58:26 -07001520 config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation))
macauleyfddc4662015-07-27 17:40:30 +08001521
Pierbbdf3782016-08-22 17:58:26 -07001522 return config_vtep_xml
1523
1524def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001525 #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2
1526 config_nexthop_xml="""
1527 <config>
1528 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1529 <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1530 <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id>
1531 <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac>
1532 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1533 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1534 </ofdpa10:next-hop>
1535 </of11-config:capable-switch>
1536 </config>
1537 """
1538 config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan))
Pierbbdf3782016-08-22 17:58:26 -07001539 config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port))
1540 config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1541 config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac))
1542 config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation))
1543 return config_nexthop_xml
macauleyfddc4662015-07-27 17:40:30 +08001544
Pierbbdf3782016-08-22 17:58:26 -07001545def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001546 #of-agent vni 10 multicast 224.1.1.1 nexthop 20
Pierbbdf3782016-08-22 17:58:26 -07001547 if mcast_ipv4!=None:
macauleyfddc4662015-07-27 17:40:30 +08001548 config_vni_xml="""
1549 <config>
1550 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1551 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1552 <ofdpa10:id>VNID</ofdpa10:id>
1553 <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group>
1554 <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id>
1555 </ofdpa10:vni>
1556 </of11-config:capable-switch>
1557 </config>
Pierbbdf3782016-08-22 17:58:26 -07001558 """
1559 config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1560 config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4))
macauleyfddc4662015-07-27 17:40:30 +08001561 else:
1562 config_vni_xml="""
1563 <config>
1564 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1565 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1566 <ofdpa10:id>VNID</ofdpa10:id>
1567 </ofdpa10:vni>
1568 </of11-config:capable-switch>
1569 </config>
Pierbbdf3782016-08-22 17:58:26 -07001570 """
1571
1572 config_vni_xml=config_vni_xml.replace("VNID", str(vni_id))
1573 config_vni_xml=config_vni_xml.replace("OPERATION", str(operation))
macauleyfddc4662015-07-27 17:40:30 +08001574 return config_vni_xml
Pierbbdf3782016-08-22 17:58:26 -07001575
1576def get_featureReplay(self):
macauleyfddc4662015-07-27 17:40:30 +08001577 req = ofp.message.features_request()
1578 res, raw = self.controller.transact(req)
Pierbbdf3782016-08-22 17:58:26 -07001579 self.assertIsNotNone(res, "Did not receive a response from the DUT.")
macauleyfddc4662015-07-27 17:40:30 +08001580 self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY,
1581 ("Unexpected packet type %d received in response to "
1582 "OFPT_FEATURES_REQUEST") % res.type)
Pierbbdf3782016-08-22 17:58:26 -07001583 return res
1584
macauleyfddc4662015-07-27 17:40:30 +08001585def send_edit_config(switch_ip, xml, target='runing'):
1586 NETCONF_ACCOUNT="netconfuser"
1587 NETCONF_PASSWD="netconfuser"
1588 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1589 try:
Pierbbdf3782016-08-22 17:58:26 -07001590 m.edit_config(target='running',
1591 config=xml,
1592 default_operation='merge',
macauleyfddc4662015-07-27 17:40:30 +08001593 error_option='stop-on-error')
1594
1595 except Exception as e:
1596 logging.info("Fail to set xml %s", xml)
1597 return False
1598
1599 #return m.get_config(source='running').data_xml
1600 return True
1601
1602def send_delete_config(switch_ip, xml, target='runing'):
1603 NETCONF_ACCOUNT="netconfuser"
1604 NETCONF_PASSWD="netconfuser"
1605 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1606 try:
Pierbbdf3782016-08-22 17:58:26 -07001607 m.edit_config(target='running',
1608 config=xml,
1609 default_operation='delete',
macauleyfddc4662015-07-27 17:40:30 +08001610 error_option='stop-on-error')
1611
1612 except Exception as e:
1613 logging.info("Fail to set xml %s", xml)
1614 return False
1615
1616 #return m.get_config(source='running').data_xml
1617 return True
Pierbbdf3782016-08-22 17:58:26 -07001618
macauleyfddc4662015-07-27 17:40:30 +08001619def get_edit_config(switch_ip, target='runing'):
1620 NETCONF_ACCOUNT="netconfuser"
1621 NETCONF_PASSWD="netconfuser"
1622 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 +08001623 return m.get_config(source='running').data_xml
macauleydbff3272015-07-30 14:07:16 +08001624
macauley_cheng67da9262015-08-31 15:18:41 +08001625
1626"""
1627MPLS
1628"""
1629
1630OFDPA_MPLS_SUBTYPE_SHIFT=24
Pierbbdf3782016-08-22 17:58:26 -07001631OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1
macauley_cheng67da9262015-08-31 15:18:41 +08001632OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2
1633OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3
1634OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4
1635OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5
1636OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6
1637OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8
1638OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10
1639
Piercf76e802016-09-19 20:16:35 -07001640
1641
1642
macauley_cheng67da9262015-08-31 15:18:41 +08001643def encode_mpls_interface_group_id(subtype, index):
1644 index=index&0x00ffffff
1645 assert(subtype==0)
1646 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1647
1648def encode_mpls_label_group_id(subtype, index):
1649 index=index&0x00ffffff
1650 assert(subtype <=5 or subtype==0)
1651 #1: l2 vpn label
1652 #2: l3 vpn label
1653 #3: mpls tunnel label 1
1654 #4: mpls tunnel lable 2
1655 #5: mpls swap label
Pierbbdf3782016-08-22 17:58:26 -07001656 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
macauley_cheng67da9262015-08-31 15:18:41 +08001657
1658def encode_mpls_forwarding_group_id(subtype, index):
1659 index=index&0x00ffffff
1660 assert(subtype==6 or subtype==8 or subtype==10)
Pierbbdf3782016-08-22 17:58:26 -07001661 return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
macauley_cheng67da9262015-08-31 15:18:41 +08001662
1663
Pier1e4e98e2016-10-26 14:36:05 -07001664def add_mpls_intf_group(ctrl, ref_gid, dst_mac, src_mac, vid, index, subtype=0, send_barrier=False):
macauley_cheng67da9262015-08-31 15:18:41 +08001665 action=[]
1666 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
1667 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
Piercf76e802016-09-19 20:16:35 -07001668 if vid != 1:
1669 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001670 action.append(ofp.action.group(ref_gid))
Pierbbdf3782016-08-22 17:58:26 -07001671
macauley_cheng67da9262015-08-31 15:18:41 +08001672 buckets = [ofp.bucket(actions=action)]
1673
1674 mpls_group_id =encode_mpls_interface_group_id(subtype, index)
1675 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1676 group_id=mpls_group_id,
1677 buckets=buckets
1678 )
1679 ctrl.message_send(request)
Pier1e4e98e2016-10-26 14:36:05 -07001680
1681 if send_barrier:
1682 do_barrier(ctrl)
1683
macauley_cheng67da9262015-08-31 15:18:41 +08001684 return mpls_group_id, request
1685
Piercf76e802016-09-19 20:16:35 -07001686def add_mpls_tunnel_label_group(
1687 ctrl,
1688 ref_gid,
1689 subtype,
1690 index,
1691 label,
1692 ):
1693
1694 action=[]
1695 action.append(ofp.action.push_mpls(0x8847))
1696 action.append(ofp.action.set_field(ofp.oxm.mpls_label(label)))
1697 action.append(ofp.action.group(ref_gid))
1698 buckets = [ofp.bucket(actions=action)]
1699
1700 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1701 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1702 group_id=mpls_group_id,
1703 buckets=buckets
1704 )
1705 ctrl.message_send(request)
1706
1707 return mpls_group_id, request
1708
Pierb5da4c92016-09-21 11:23:35 -07001709def add_mpls_swap_label_group(
1710 ctrl,
1711 ref_gid,
1712 subtype,
1713 index,
1714 label,
1715 ):
1716
1717 action=[]
1718 action.append(ofp.action.set_field(ofp.oxm.mpls_label(label)))
1719 action.append(ofp.action.group(ref_gid))
1720 buckets = [ofp.bucket(actions=action)]
1721
1722 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1723 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1724 group_id=mpls_group_id,
1725 buckets=buckets
1726 )
1727 ctrl.message_send(request)
1728
1729 return mpls_group_id, request
1730
Pierbbdf3782016-08-22 17:58:26 -07001731def add_mpls_label_group(ctrl, subtype, index, ref_gid,
macauley_cheng67da9262015-08-31 15:18:41 +08001732 lmep_id=-1,
1733 qos_index=-1,
1734 push_l2_header=False,
1735 push_vlan=False,
1736 push_mpls_header=False,
1737 push_cw=False,
1738 set_mpls_label=None,
1739 set_bos=None,
1740 set_tc=None,
1741 set_tc_from_table=False,
1742 cpy_tc_outward=False,
1743 set_ttl=None,
1744 cpy_ttl_outward=False,
1745 oam_lm_tx_count=False,
Pier1e4e98e2016-10-26 14:36:05 -07001746 set_pri_from_table=False,
1747 send_barrier=False
macauley_cheng67da9262015-08-31 15:18:41 +08001748 ):
1749 """
1750 @ref_gid: only can be mpls intf group or mpls tunnel label 1/2 group
Pierbbdf3782016-08-22 17:58:26 -07001751 """
macauley_cheng67da9262015-08-31 15:18:41 +08001752 action=[]
1753
1754 if push_vlan== True:
1755 action.append(ofp.action.push_vlan(0x8100))
1756 if push_mpls_header== True:
1757 action.append(ofp.action.push_mpls(0x8847))
1758 if set_mpls_label != None:
1759 action.append(ofp.action.set_field(ofp.oxm.mpls_label(set_mpls_label)))
1760 if set_bos != None:
1761 action.append(ofp.action.set_field(ofp.oxm.mpls_bos(set_bos)))
1762 if set_tc != None:
1763 assert(set_tc_from_table==False)
1764 action.append(ofp.action.set_field(ofp.oxm.mpls_tc(set_tc)))
1765 if set_ttl != None:
Pierbbdf3782016-08-22 17:58:26 -07001766 action.append(ofp.action.set_mpls_ttl(set_ttl))
macauley_cheng67da9262015-08-31 15:18:41 +08001767 if cpy_ttl_outward == True:
Pierbbdf3782016-08-22 17:58:26 -07001768 action.append(ofp.action.copy_ttl_out())
macauley_cheng67da9262015-08-31 15:18:41 +08001769 """
1770 ofdpa experimenter
Pierbbdf3782016-08-22 17:58:26 -07001771 """
macauley_cheng67da9262015-08-31 15:18:41 +08001772 if push_l2_header== True:
Pierbbdf3782016-08-22 17:58:26 -07001773 action.append(ofp.action.ofdpa_push_l2_header())
macauley_cheng67da9262015-08-31 15:18:41 +08001774 if set_tc_from_table== True:
1775 assert(qos_index>=0)
1776 assert(set_tc == None)
Pierbbdf3782016-08-22 17:58:26 -07001777 action.append(ofp.action.ofdpa_set_tc_from_table(qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08001778 if cpy_tc_outward == True:
Pierbbdf3782016-08-22 17:58:26 -07001779 action.append(ofp.action.ofdpa_copy_tc_out())
macauley_cheng67da9262015-08-31 15:18:41 +08001780 if oam_lm_tx_count == True:
Pierbbdf3782016-08-22 17:58:26 -07001781 assert(qos_index>=0 and lmep_id>=0)
1782 action.append(ofp.action.ofdpa_oam_lm_tx_count(lmep_id, qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08001783 if set_pri_from_table == True:
Pierbbdf3782016-08-22 17:58:26 -07001784 assert(qos_index>=0)
1785 action.append(ofp.action.ofdpa_set_qos_from_table(qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08001786 if push_cw == True:
1787 action.append(ofp.action.ofdpa_push_cw())
Pierbbdf3782016-08-22 17:58:26 -07001788
1789 action.append(ofp.action.group(ref_gid))
macauley_cheng67da9262015-08-31 15:18:41 +08001790 buckets = [ofp.bucket(actions=action)]
Pierbbdf3782016-08-22 17:58:26 -07001791
macauley_cheng67da9262015-08-31 15:18:41 +08001792 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1793 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1794 group_id=mpls_group_id,
1795 buckets=buckets
1796 )
1797 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -07001798
Pier1e4e98e2016-10-26 14:36:05 -07001799 if send_barrier:
1800 do_barrier(ctrl)
1801
Pierbbdf3782016-08-22 17:58:26 -07001802 return mpls_group_id, request
1803
Piercf76e802016-09-19 20:16:35 -07001804def add_mpls_l2_port_flow(ctrl, of_port, mpls_l2_port, tunnel_index, ref_gid, qos_index=0):
1805 """
1806 Only action is Group, which must indicate one of:
1807 MPLS L2 VPN Label or Fast Failover Protection Group.
1808 ref_gid contains this information
1809 """
1810 tunnel_id = tunnel_index + ofp.oxm.TUNNEL_ID_BASE
1811
1812 match = ofp.match()
1813 match.oxm_list.append(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port))
1814 match.oxm_list.append(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE))
1815
Pier23784aa2016-09-19 20:08:21 -07001816
Piercf76e802016-09-19 20:16:35 -07001817 action = []
1818 action.append(ofp.action.group(ref_gid))
1819 assert(qos_index>=0)
1820 action.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_QOS_INDEX, value=qos_index)))
1821
1822 request = ofp.message.flow_add(
1823 table_id=MPLS_L2_PORT_FLOW_TABLE,
1824 cookie=42,
1825 match=match,
1826 instructions=[
1827 ofp.instruction.write_actions(
1828 actions=action
1829 ),
1830 ofp.instruction.goto_table(MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE) ],
1831 buffer_id=ofp.OFP_NO_BUFFER,
1832 priority=1)
1833 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)
1834 ctrl.message_send(request)
1835 return request
1836
1837 return
1838
Pierbbdf3782016-08-22 17:58:26 -07001839def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids,
1840 watch_port=None,
1841 watch_group=ofp.OFPP_ANY,
macauley_cheng67da9262015-08-31 15:18:41 +08001842 push_vlan=None,
macauley_chengd17ce512015-08-31 17:45:51 +08001843 pop_vlan=None,
macauley_cheng67da9262015-08-31 15:18:41 +08001844 set_vid=None):
1845 assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP
1846 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP
1847 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG)
macauley_chengd17ce512015-08-31 17:45:51 +08001848
macauley_cheng67da9262015-08-31 15:18:41 +08001849 buckets=[]
1850 if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP:
macauley_chengd17ce512015-08-31 17:45:51 +08001851 group_type = ofp.OFPGT_FF
macauley_cheng67da9262015-08-31 15:18:41 +08001852 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001853 action=[]
Pierbbdf3782016-08-22 17:58:26 -07001854 action.append(ofp.action.group(gid))
macauley_chengd17ce512015-08-31 17:45:51 +08001855 buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action))
1856
macauley_cheng67da9262015-08-31 15:18:41 +08001857 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP:
1858 group_type = ofp.OFPGT_SELECT
1859 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001860 action=[]
Pierbbdf3782016-08-22 17:58:26 -07001861 action.append(ofp.action.group(gid))
macauley_cheng67da9262015-08-31 15:18:41 +08001862 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001863
macauley_cheng67da9262015-08-31 15:18:41 +08001864 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG:
1865 group_type = ofp.OFPGT_INDIRECT
macauley_chengd17ce512015-08-31 17:45:51 +08001866 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08001867 if set_vid!=None:
Flavio Castro91d1a552016-05-17 16:59:44 -07001868 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+set_vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001869 if push_vlan!=None:
Pierbbdf3782016-08-22 17:58:26 -07001870 action.append(ofp.action.push_vlan(push_vlan))
macauley_cheng67da9262015-08-31 15:18:41 +08001871 if pop_vlan!=None:
Pierbbdf3782016-08-22 17:58:26 -07001872 action.append(ofp.action.pop_vlan())
1873 action.append(ofp.action.group(ref_gids[0]))
macauley_cheng67da9262015-08-31 15:18:41 +08001874 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001875
1876 mpls_group_id = encode_mpls_forwarding_group_id(subtype, index)
macauley_cheng67da9262015-08-31 15:18:41 +08001877 request = ofp.message.group_add(group_type=group_type,
macauley_cheng67da9262015-08-31 15:18:41 +08001878 group_id=mpls_group_id,
1879 buckets=buckets
1880 )
1881 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -07001882 return mpls_group_id, request
macauley_chengd17ce512015-08-31 17:45:51 +08001883
1884
macauley_cheng67da9262015-08-31 15:18:41 +08001885"""
Piercf76e802016-09-19 20:16:35 -07001886display
Pierbbdf3782016-08-22 17:58:26 -07001887"""
macauleydbff3272015-07-30 14:07:16 +08001888def print_current_table_flow_stat(ctrl, table_id=0xff):
1889 stat_req=ofp.message.flow_stats_request()
1890 response, pkt = ctrl.transact(stat_req)
1891 if response == None:
1892 print "no response"
1893 return None
1894 print len(response.entries)
1895 for obj in response.entries:
1896 print "match ", obj.match
1897 print "cookie", obj.cookie
1898 print "priority", obj.priority
1899 print "idle_timeout", obj.idle_timeout
1900 print "hard_timeout", obj.hard_timeout
1901 #obj.actions
Flavio Castro167f5bd2015-12-02 19:33:53 -05001902 print "packet count: %lx"%obj.packet_count