blob: 0dafb5d7b71869e1e986d8dd2964630a8e4d607e [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(
Pier265ad5f2017-02-28 17:46:28 +0100475 table_id=0,
476 cookie=42,
477 match=match,
478 instructions=[
479 ofp.instruction.goto_table(NEXT_TABLE)
480 ],
481 priority=0)
macauleyfddc4662015-07-27 17:40:30 +0800482 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:
Pier265ad5f2017-02-28 17:46:28 +01001090 instructions.append(ofp.instruction.write_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:
Pier265ad5f2017-02-28 17:46:28 +01001124 instructions.append(ofp.instruction.write_actions(
Pier1e4e98e2016-10-26 14:36:05 -07001125 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))
Pier265ad5f2017-02-28 17:46:28 +01001152 write_actions = []
1153 write_actions.append(ofp.action.group(action_group_id))
1154 apply_actions = []
1155 apply_actions = [ofp.action.dec_mpls_ttl(),
Flavio Castro8ca52542016-04-11 11:24:49 -04001156 ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))]
Pier265ad5f2017-02-28 17:46:28 +01001157 if (goto_table != 29):
1158 apply_actions.append(ofp.action.set_field(
Flavio Castrod80fbc32016-07-25 15:54:26 -07001159 ofp.oxm.exp2ByteValue(exp_type=23, value=32)))
Pier265ad5f2017-02-28 17:46:28 +01001160 apply_actions.append(ofp.action.copy_ttl_in())
Flavio Castro9debaaa2016-07-26 19:37:50 -07001161
Flavio Castrob702a2f2016-04-10 22:01:48 -04001162 request = ofp.message.flow_add(
1163 table_id=24,
1164 cookie=43,
1165 match=match,
1166 instructions=[
Pier265ad5f2017-02-28 17:46:28 +01001167 ofp.instruction.apply_actions(actions=apply_actions),
1168 ofp.instruction.write_actions(actions=write_actions),
Flavio Castro8ca52542016-04-11 11:24:49 -04001169 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
Pier265ad5f2017-02-28 17:46:28 +01001187 apply_actions = []
1188 write_actions = []
1189 apply_actions.append(ofp.action.dec_mpls_ttl())
Pierf6f28162016-09-22 16:30:52 -07001190 if popMPLS == True:
Pier265ad5f2017-02-28 17:46:28 +01001191 apply_actions.append(ofp.action.copy_ttl_in())
1192 apply_actions.append(ofp.action.pop_mpls(ethertype))
Pierf6f28162016-09-22 16:30:52 -07001193 if bos==1 and popL2 == True:
Pier265ad5f2017-02-28 17:46:28 +01001194 apply_actions.append(ofp.action.ofdpa_pop_l2_header())
1195 apply_actions.append(ofp.action.ofdpa_pop_cw())
1196 apply_actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
Pierf6f28162016-09-22 16:30:52 -07001197 # 0x0002nnnn is for UNI interfaces
Pier265ad5f2017-02-28 17:46:28 +01001198 apply_actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00020000 + of_port)))
1199 apply_actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
1200 write_actions.append(ofp.action.group(action_group_id))
Pierb5da4c92016-09-21 11:23:35 -07001201
1202 request = ofp.message.flow_add(
Pier265ad5f2017-02-28 17:46:28 +01001203 table_id=24,
1204 cookie=43,
1205 match=match,
1206 instructions=[
1207 ofp.instruction.apply_actions(actions=apply_actions),
1208 ofp.instruction.write_actions(actions=write_actions),
1209 ofp.instruction.goto_table(goto_table)
1210 ],
1211 buffer_id=ofp.OFP_NO_BUFFER,
1212 priority=1)
Pierb5da4c92016-09-21 11:23:35 -07001213 logging.info("Inserting MPLS flow , label %ld", label)
1214 ctrl.message_send(request)
1215
1216 if send_barrier:
1217 do_barrier(ctrl)
1218
1219 return request
1220
macauleyfddc4662015-07-27 17:40:30 +08001221def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False):
1222 match = ofp.match()
1223 match.oxm_list.append(ofp.oxm.eth_type(0x0800))
Pierbbdf3782016-08-22 17:58:26 -07001224 match.oxm_list.append(ofp.oxm.vlan_vid(vlan_id))
macauleyfddc4662015-07-27 17:40:30 +08001225 if src_ip_mask!=0:
1226 match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask))
1227 else:
1228 match.oxm_list.append(ofp.oxm.ipv4_src(src_ip))
Pierbbdf3782016-08-22 17:58:26 -07001229
macauleyfddc4662015-07-27 17:40:30 +08001230 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
Pierbbdf3782016-08-22 17:58:26 -07001231
macauleyfddc4662015-07-27 17:40:30 +08001232 request = ofp.message.flow_add(
1233 table_id=40,
1234 cookie=42,
1235 match=match,
1236 instructions=[
1237 ofp.instruction.write_actions(
1238 actions=[ofp.action.group(action_group_id)]),
1239 ofp.instruction.goto_table(60)
1240 ],
1241 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001242 priority=1)
macauleyfddc4662015-07-27 17:40:30 +08001243
1244 logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask)
1245 ctrl.message_send(request)
1246
1247 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001248 do_barrier(ctrl)
macauleyfddc4662015-07-27 17:40:30 +08001249
Pierbbdf3782016-08-22 17:58:26 -07001250 return request
macauley_cheng6b133662015-11-09 13:52:39 +08001251
Pier1e4e98e2016-10-26 14:36:05 -07001252def add_acl_rule(ctrl, eth_type=None, ip_proto=None, send_barrier=False):
1253 match = ofp.match()
1254 if eth_type != None:
1255 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1256 if ip_proto != None:
1257 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1258
1259 request = ofp.message.flow_add(
1260 table_id=60,
1261 cookie=42,
1262 match=match,
1263 instructions=[
1264 ofp.instruction.apply_actions(
1265 actions=[ofp.action.output(port=ofp.OFPP_CONTROLLER, max_len=ofp.OFPCML_NO_BUFFER)]
1266 ),
1267 ],
1268 buffer_id=ofp.OFP_NO_BUFFER,
1269 priority=1
1270 )
1271
1272 logging.info("Inserting ACL flow eth_type %lx, ip_proto %ld", eth_type, ip_proto)
1273 ctrl.message_send(request)
1274
1275 if send_barrier:
1276 do_barrier(ctrl)
1277
1278 return request
1279
macauley_cheng6b133662015-11-09 13:52:39 +08001280#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 +08001281def 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 +08001282 match = ofp.match()
1283 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1284 match.oxm_list.append(ofp.oxm.ipv4_dst(ip_dst))
1285 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1286 match.oxm_list.append(ofp.oxm.tcp_dst(tcp_dst))
Pierbbdf3782016-08-22 17:58:26 -07001287
macauley_cheng6b133662015-11-09 13:52:39 +08001288 request = ofp.message.flow_add(
1289 table_id=28,
1290 cookie=42,
1291 match=match,
1292 instructions=[
1293 ofp.instruction.write_actions(
1294 actions=[ofp.action.set_field(ofp.oxm.ipv4_dst(set_ip_dst)),
1295 ofp.action.set_field(ofp.oxm.tcp_dst(set_tcp_dst)),
1296 ofp.action.group(action_group_id)]),
1297 ofp.instruction.goto_table(60)
1298 ],
1299 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001300 priority=1)
macauley_cheng6b133662015-11-09 13:52:39 +08001301 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)
1302 ctrl.message_send(request)
1303 return request
macauley_chengeffc20a2015-11-09 16:14:56 +08001304
1305#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
1306def add_snat_flow(ctrl, eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src):
1307 match = ofp.match()
1308 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1309 match.oxm_list.append(ofp.oxm.ipv4_src(ip_src))
1310 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1311 match.oxm_list.append(ofp.oxm.tcp_src(tcp_src))
Pierbbdf3782016-08-22 17:58:26 -07001312
macauley_chengeffc20a2015-11-09 16:14:56 +08001313 request = ofp.message.flow_add(
1314 table_id=29,
1315 cookie=42,
1316 match=match,
1317 instructions=[
1318 ofp.instruction.write_actions(
1319 actions=[ofp.action.set_field(ofp.oxm.ipv4_src(set_ip_src)),
1320 ofp.action.set_field(ofp.oxm.tcp_src(set_tcp_src))]),
1321 ofp.instruction.goto_table(30)
1322 ],
1323 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001324 priority=1)
macauley_chengeffc20a2015-11-09 16:14:56 +08001325 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)
1326 ctrl.message_send(request)
1327 return request
Pierbbdf3782016-08-22 17:58:26 -07001328
1329def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001330 """
1331 Command Example:
1332 of-agent vtap 10001 ethernet 1/1 vid 1
1333 of-agent vtp 10001 vni 10
1334 """
1335 if vlan != 0:
1336 config_vtap_xml="""
1337 <config>
1338 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1339 <id>capable-switch-1</id>
1340 <resources>
1341 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001342 <resource-id >LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001343 <features>
1344 <current>
1345 <rate>10Gb</rate>
1346 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001347 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001348 </current>
1349 <advertised>
1350 <rate>10Gb</rate>
1351 <rate>100Gb</rate>
1352 <medium>fiber</medium>
1353 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001354 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001355 <supported>
1356 <rate>10Gb</rate>
1357 <rate>100Gb</rate>
1358 <medium>fiber</medium>
1359 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001360 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001361 <advertised-peer>
1362 <rate>10Gb</rate>
1363 <rate>100Gb</rate>
1364 <medium>fiber</medium>
1365 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001366 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001367 </features>
1368 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1369 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1370 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1371 <ofdpa10:vni>VNID</ofdpa10:vni>
1372 </ofdpa10:vtap>
Pierbbdf3782016-08-22 17:58:26 -07001373 </port>
macauleyfddc4662015-07-27 17:40:30 +08001374 </resources>
1375 <logical-switches>
1376 <switch>
1377 <id>DATAPATH_ID</id>
1378 <datapath-id>DATAPATH_ID</datapath-id>
1379 <resources>
1380 <port xc:operation="OPERATION">LPORT</port>
1381 </resources>
1382 </switch>
1383 </logical-switches>
1384 </capable-switch>
1385 </config>
Pierbbdf3782016-08-22 17:58:26 -07001386 """
macauleyfddc4662015-07-27 17:40:30 +08001387 else:
1388 config_vtap_xml="""
1389 <config>
1390 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1391 <id>capable-switch-1</id>
1392 <resources>
1393 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001394 <resource-id >LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001395 <features>
1396 <current>
1397 <rate>10Gb</rate>
1398 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001399 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001400 </current>
1401 <advertised>
1402 <rate>10Gb</rate>
1403 <rate>100Gb</rate>
1404 <medium>fiber</medium>
1405 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001406 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001407 <supported>
1408 <rate>10Gb</rate>
1409 <rate>100Gb</rate>
1410 <medium>fiber</medium>
1411 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001412 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001413 <advertised-peer>
1414 <rate>10Gb</rate>
1415 <rate>100Gb</rate>
1416 <medium>fiber</medium>
1417 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001418 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001419 </features>
1420 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1421 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1422 <ofdpa10:vni>VNID</ofdpa10:vni>
1423 </ofdpa10:vtap>
Pierbbdf3782016-08-22 17:58:26 -07001424 </port>
macauleyfddc4662015-07-27 17:40:30 +08001425 </resources>
1426 <logical-switches>
1427 <switch>
1428 <id>DATAPATH_ID</id>
1429 <datapath-id>DATAPATH_ID</datapath-id>
1430 <resources>
1431 <port xc:operation="OPERATION">LPORT</port>
1432 </resources>
1433 </switch>
1434 </logical-switches>
1435 </capable-switch>
1436 </config>
Pierbbdf3782016-08-22 17:58:26 -07001437 """
1438 str_datapath_id_f= "{:016x}".format(dp_id)
1439 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1440 config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id)
1441 config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport)))
1442 config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port))
1443 config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan))
macauleyfddc4662015-07-27 17:40:30 +08001444 config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid))
1445 config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation))
1446 return config_vtap_xml
Pierbbdf3782016-08-22 17:58:26 -07001447
1448def 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 +08001449 """
1450 Command Example:
1451 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 -07001452 of-agent vtp 10001 vni 10
macauleyfddc4662015-07-27 17:40:30 +08001453 """
1454
1455 config_vtep_xml="""
1456 <config>
1457 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1458 <id>capable-switch-1</id>
1459 <resources>
1460 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001461 <resource-id>LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001462 <features>
1463 <current>
1464 <rate>10Gb</rate>
1465 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001466 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001467 </current>
1468 <advertised>
1469 <rate>10Gb</rate>
1470 <rate>100Gb</rate>
1471 <medium>fiber</medium>
1472 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001473 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001474 <supported>
1475 <rate>10Gb</rate>
1476 <rate>100Gb</rate>
1477 <medium>fiber</medium>
1478 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001479 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001480 <advertised-peer>
1481 <rate>10Gb</rate>
1482 <rate>100Gb</rate>
1483 <medium>fiber</medium>
1484 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001485 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001486 </features>
Pier265ad5f2017-02-28 17:46:28 +01001487 <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01">
1488 <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip>
1489 <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip>
1490 <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port>
1491 <ofdpa10:vni xc:operation="OPERATION">
macauley25999cf2015-08-07 17:03:24 +08001492 <ofdpa10:id>VNID</ofdpa10:id>
1493 </ofdpa10:vni>
Pier265ad5f2017-02-28 17:46:28 +01001494 <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id>
1495 <ofdpa10:ttl>TTL</ofdpa10:ttl>
1496 </ofdpa10:vtep>
Pierbbdf3782016-08-22 17:58:26 -07001497 </port>
macauleyfddc4662015-07-27 17:40:30 +08001498 </resources>
1499 <logical-switches>
1500 <switch>
1501 <id>DATAPATH_ID</id>
1502 <datapath-id>DATAPATH_ID</datapath-id>
1503 <resources>
1504 <port xc:operation="OPERATION">LPORT</port>
1505 </resources>
1506 </switch>
1507 </logical-switches>
1508 </capable-switch>
Pierbbdf3782016-08-22 17:58:26 -07001509 </config>
macauleyfddc4662015-07-27 17:40:30 +08001510 """
Pierbbdf3782016-08-22 17:58:26 -07001511 str_datapath_id_f= "{:016x}".format(dp_id)
1512 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1513 config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +08001514 config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport)))
Pierbbdf3782016-08-22 17:58:26 -07001515 config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip))
1516 config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip))
1517 config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port))
1518 config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1519 config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl))
macauleyfddc4662015-07-27 17:40:30 +08001520 config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid))
Pierbbdf3782016-08-22 17:58:26 -07001521 config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation))
macauleyfddc4662015-07-27 17:40:30 +08001522
Pierbbdf3782016-08-22 17:58:26 -07001523 return config_vtep_xml
1524
1525def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001526 #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2
1527 config_nexthop_xml="""
1528 <config>
1529 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1530 <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1531 <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id>
1532 <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac>
1533 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1534 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1535 </ofdpa10:next-hop>
1536 </of11-config:capable-switch>
1537 </config>
1538 """
1539 config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan))
Pierbbdf3782016-08-22 17:58:26 -07001540 config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port))
1541 config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1542 config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac))
1543 config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation))
1544 return config_nexthop_xml
macauleyfddc4662015-07-27 17:40:30 +08001545
Pierbbdf3782016-08-22 17:58:26 -07001546def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001547 #of-agent vni 10 multicast 224.1.1.1 nexthop 20
Pierbbdf3782016-08-22 17:58:26 -07001548 if mcast_ipv4!=None:
macauleyfddc4662015-07-27 17:40:30 +08001549 config_vni_xml="""
1550 <config>
1551 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1552 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1553 <ofdpa10:id>VNID</ofdpa10:id>
1554 <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group>
1555 <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id>
1556 </ofdpa10:vni>
1557 </of11-config:capable-switch>
1558 </config>
Pierbbdf3782016-08-22 17:58:26 -07001559 """
1560 config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1561 config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4))
macauleyfddc4662015-07-27 17:40:30 +08001562 else:
1563 config_vni_xml="""
1564 <config>
1565 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1566 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1567 <ofdpa10:id>VNID</ofdpa10:id>
1568 </ofdpa10:vni>
1569 </of11-config:capable-switch>
1570 </config>
Pierbbdf3782016-08-22 17:58:26 -07001571 """
1572
1573 config_vni_xml=config_vni_xml.replace("VNID", str(vni_id))
1574 config_vni_xml=config_vni_xml.replace("OPERATION", str(operation))
macauleyfddc4662015-07-27 17:40:30 +08001575 return config_vni_xml
Pierbbdf3782016-08-22 17:58:26 -07001576
1577def get_featureReplay(self):
macauleyfddc4662015-07-27 17:40:30 +08001578 req = ofp.message.features_request()
1579 res, raw = self.controller.transact(req)
Pierbbdf3782016-08-22 17:58:26 -07001580 self.assertIsNotNone(res, "Did not receive a response from the DUT.")
macauleyfddc4662015-07-27 17:40:30 +08001581 self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY,
1582 ("Unexpected packet type %d received in response to "
1583 "OFPT_FEATURES_REQUEST") % res.type)
Pierbbdf3782016-08-22 17:58:26 -07001584 return res
1585
macauleyfddc4662015-07-27 17:40:30 +08001586def send_edit_config(switch_ip, xml, target='runing'):
1587 NETCONF_ACCOUNT="netconfuser"
1588 NETCONF_PASSWD="netconfuser"
1589 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1590 try:
Pierbbdf3782016-08-22 17:58:26 -07001591 m.edit_config(target='running',
1592 config=xml,
1593 default_operation='merge',
macauleyfddc4662015-07-27 17:40:30 +08001594 error_option='stop-on-error')
1595
1596 except Exception as e:
1597 logging.info("Fail to set xml %s", xml)
1598 return False
1599
Pier265ad5f2017-02-28 17:46:28 +01001600 #return m.get_config(source='running').data_xml
macauleyfddc4662015-07-27 17:40:30 +08001601 return True
1602
1603def send_delete_config(switch_ip, xml, target='runing'):
1604 NETCONF_ACCOUNT="netconfuser"
1605 NETCONF_PASSWD="netconfuser"
1606 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1607 try:
Pierbbdf3782016-08-22 17:58:26 -07001608 m.edit_config(target='running',
1609 config=xml,
1610 default_operation='delete',
macauleyfddc4662015-07-27 17:40:30 +08001611 error_option='stop-on-error')
1612
1613 except Exception as e:
1614 logging.info("Fail to set xml %s", xml)
1615 return False
1616
Pier265ad5f2017-02-28 17:46:28 +01001617 #return m.get_config(source='running').data_xml
macauleyfddc4662015-07-27 17:40:30 +08001618 return True
Pierbbdf3782016-08-22 17:58:26 -07001619
macauleyfddc4662015-07-27 17:40:30 +08001620def get_edit_config(switch_ip, target='runing'):
1621 NETCONF_ACCOUNT="netconfuser"
1622 NETCONF_PASSWD="netconfuser"
1623 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
Pier265ad5f2017-02-28 17:46:28 +01001624 return m.get_config(source='running').data_xml
macauleydbff3272015-07-30 14:07:16 +08001625
macauley_cheng67da9262015-08-31 15:18:41 +08001626
1627"""
1628MPLS
1629"""
1630
1631OFDPA_MPLS_SUBTYPE_SHIFT=24
Pierbbdf3782016-08-22 17:58:26 -07001632OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1
macauley_cheng67da9262015-08-31 15:18:41 +08001633OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2
1634OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3
1635OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4
1636OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5
1637OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6
1638OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8
1639OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10
1640
Piercf76e802016-09-19 20:16:35 -07001641
1642
1643
macauley_cheng67da9262015-08-31 15:18:41 +08001644def encode_mpls_interface_group_id(subtype, index):
1645 index=index&0x00ffffff
1646 assert(subtype==0)
1647 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1648
1649def encode_mpls_label_group_id(subtype, index):
1650 index=index&0x00ffffff
1651 assert(subtype <=5 or subtype==0)
1652 #1: l2 vpn label
1653 #2: l3 vpn label
1654 #3: mpls tunnel label 1
1655 #4: mpls tunnel lable 2
1656 #5: mpls swap label
Pierbbdf3782016-08-22 17:58:26 -07001657 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
macauley_cheng67da9262015-08-31 15:18:41 +08001658
1659def encode_mpls_forwarding_group_id(subtype, index):
1660 index=index&0x00ffffff
1661 assert(subtype==6 or subtype==8 or subtype==10)
Pierbbdf3782016-08-22 17:58:26 -07001662 return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
macauley_cheng67da9262015-08-31 15:18:41 +08001663
1664
Pier1e4e98e2016-10-26 14:36:05 -07001665def 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 +08001666 action=[]
1667 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
1668 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
Pier265ad5f2017-02-28 17:46:28 +01001669 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
Pier265ad5f2017-02-28 17:46:28 +01001817 write_actions = []
1818 write_actions.append(ofp.action.group(ref_gid))
1819 apply_actions = []
Piercf76e802016-09-19 20:16:35 -07001820 assert(qos_index>=0)
Pier265ad5f2017-02-28 17:46:28 +01001821 apply_actions.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_QOS_INDEX, value=qos_index)))
Piercf76e802016-09-19 20:16:35 -07001822
1823 request = ofp.message.flow_add(
1824 table_id=MPLS_L2_PORT_FLOW_TABLE,
1825 cookie=42,
1826 match=match,
1827 instructions=[
Pier265ad5f2017-02-28 17:46:28 +01001828 ofp.instruction.apply_actions(actions=apply_actions),
1829 ofp.instruction.write_actions(actions=write_actions),
1830 ofp.instruction.goto_table(MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE)
1831 ],
Piercf76e802016-09-19 20:16:35 -07001832 buffer_id=ofp.OFP_NO_BUFFER,
1833 priority=1)
1834 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)
1835 ctrl.message_send(request)
1836 return request
1837
1838 return
1839
Pierbbdf3782016-08-22 17:58:26 -07001840def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids,
1841 watch_port=None,
Pier265ad5f2017-02-28 17:46:28 +01001842 watch_group=ofp.OFPP_ANY,
1843 push_vlan=None,
macauley_chengd17ce512015-08-31 17:45:51 +08001844 pop_vlan=None,
macauley_cheng67da9262015-08-31 15:18:41 +08001845 set_vid=None):
1846 assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP
Pier265ad5f2017-02-28 17:46:28 +01001847 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP
1848 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG)
macauley_chengd17ce512015-08-31 17:45:51 +08001849
macauley_cheng67da9262015-08-31 15:18:41 +08001850 buckets=[]
1851 if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP:
macauley_chengd17ce512015-08-31 17:45:51 +08001852 group_type = ofp.OFPGT_FF
macauley_cheng67da9262015-08-31 15:18:41 +08001853 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001854 action=[]
Pierbbdf3782016-08-22 17:58:26 -07001855 action.append(ofp.action.group(gid))
macauley_chengd17ce512015-08-31 17:45:51 +08001856 buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action))
1857
macauley_cheng67da9262015-08-31 15:18:41 +08001858 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP:
1859 group_type = ofp.OFPGT_SELECT
1860 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001861 action=[]
Pierbbdf3782016-08-22 17:58:26 -07001862 action.append(ofp.action.group(gid))
macauley_cheng67da9262015-08-31 15:18:41 +08001863 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001864
macauley_cheng67da9262015-08-31 15:18:41 +08001865 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG:
1866 group_type = ofp.OFPGT_INDIRECT
macauley_chengd17ce512015-08-31 17:45:51 +08001867 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08001868 if set_vid!=None:
Flavio Castro91d1a552016-05-17 16:59:44 -07001869 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+set_vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001870 if push_vlan!=None:
Pierbbdf3782016-08-22 17:58:26 -07001871 action.append(ofp.action.push_vlan(push_vlan))
macauley_cheng67da9262015-08-31 15:18:41 +08001872 if pop_vlan!=None:
Pierbbdf3782016-08-22 17:58:26 -07001873 action.append(ofp.action.pop_vlan())
1874 action.append(ofp.action.group(ref_gids[0]))
macauley_cheng67da9262015-08-31 15:18:41 +08001875 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001876
1877 mpls_group_id = encode_mpls_forwarding_group_id(subtype, index)
macauley_cheng67da9262015-08-31 15:18:41 +08001878 request = ofp.message.group_add(group_type=group_type,
macauley_cheng67da9262015-08-31 15:18:41 +08001879 group_id=mpls_group_id,
1880 buckets=buckets
1881 )
1882 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -07001883 return mpls_group_id, request
macauley_chengd17ce512015-08-31 17:45:51 +08001884
1885
macauley_cheng67da9262015-08-31 15:18:41 +08001886"""
Piercf76e802016-09-19 20:16:35 -07001887display
Pierbbdf3782016-08-22 17:58:26 -07001888"""
macauleydbff3272015-07-30 14:07:16 +08001889def print_current_table_flow_stat(ctrl, table_id=0xff):
1890 stat_req=ofp.message.flow_stats_request()
1891 response, pkt = ctrl.transact(stat_req)
1892 if response == None:
1893 print "no response"
1894 return None
1895 print len(response.entries)
1896 for obj in response.entries:
1897 print "match ", obj.match
1898 print "cookie", obj.cookie
1899 print "priority", obj.priority
1900 print "idle_timeout", obj.idle_timeout
1901 print "hard_timeout", obj.hard_timeout
1902 #obj.actions
Flavio Castro167f5bd2015-12-02 19:33:53 -05001903 print "packet count: %lx"%obj.packet_count