blob: 15e6e99909e978e4445e11fd9a0257a615ffb85b [file] [log] [blame]
macauley97557232015-07-16 17:28:07 +08001import logging
2
3from oftest import config
4import oftest.base_tests as base_tests
5import ofp
6import time
7from oftest.testutils import *
8
macauleyfddc4662015-07-27 17:40:30 +08009from ncclient import manager
10import ncclient
11
macauley97557232015-07-16 17:28:07 +080012OFDPA_GROUP_TYPE_SHIFT=28
13OFDPA_VLAN_ID_SHIFT =16
macauleyfddc4662015-07-27 17:40:30 +080014OFDPA_TUNNEL_ID_SHIFT =12
15OFDPA_TUNNEL_SUBTYPE_SHIFT=10
macauley97557232015-07-16 17:28:07 +080016
17#VLAN_TABLE_FLAGS
18VLAN_TABLE_FLAG_ONLY_UNTAG=1
19VLAN_TABLE_FLAG_ONLY_TAG =2
20VLAN_TABLE_FLAG_ONLY_BOTH =3
Piere1308762016-09-12 15:29:56 -070021VLAN_TABLE_FLAG_ONLY_STACKED=5
22VLAN_TABLE_FLAG_PRIORITY=6
23VLAN_TABLE_FLAG_ONLY_UNTAG_PRIORITY=7
macauley97557232015-07-16 17:28:07 +080024
macauleye8b140e2015-08-03 13:35:45 +080025PORT_FLOW_TABLE=0
26VLAN_FLOW_TABLE=10
Piere1308762016-09-12 15:29:56 -070027VLAN_1_FLOW_TABLE=11
Piercf76e802016-09-19 20:16:35 -070028MPLS_L2_PORT_FLOW_TABLE=13
29MPLS_L2_PORT_DSCP_TRUST_FLOW_TABLE=15
30MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE=16
macauleye8b140e2015-08-03 13:35:45 +080031TERMINATION_FLOW_TABLE=20
Piercf76e802016-09-19 20:16:35 -070032MPLS_TYPE_FLOW_TABLE=29
macauleye8b140e2015-08-03 13:35:45 +080033UCAST_ROUTING_FLOW_TABLE=30
34MCAST_ROUTING_FLOW_TABLE=40
35BRIDGE_FLOW_TABLE=50
36ACL_FLOW_TABLE=60
37
38def convertIP4toStr(ip_addr):
39 a=(ip_addr&0xff000000)>>24
40 b=(ip_addr&0x00ff0000)>>16
41 c=(ip_addr&0x0000ff00)>>8
42 d=(ip_addr&0x000000ff)
43 return str(a)+"."+str(b)+"."+str(c)+"."+str(d)
44
45def convertMACtoStr(mac):
46 if not isinstance(mac, list):
47 assert(0)
48
49 return ':'.join(['%02X' % x for x in mac])
50
macauley7f89d962015-08-06 18:13:48 +080051def getSwitchCpuMACFromDPID(dpid):
52 str_datapath_id_f= "{:016x}".format(dpid)
53 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
54 switch_cpu_mac_str=str_datapath_id[6:]
55 switch_cpu_mac = switch_cpu_mac_str.split(":")
56 switch_cpu_mac=[int(switch_cpu_mac[i],16) for i in range(0, len(switch_cpu_mac))]
57
58 return switch_cpu_mac_str, switch_cpu_mac
Pierbbdf3782016-08-22 17:58:26 -070059
macauley_cheng67da9262015-08-31 15:18:41 +080060def DumpGroup(stats, verify_group_stats, always_show=True):
61 if(len(stats) > len(verify_group_stats)):
62 min_len = len(verify_group_stats)
63 print "Stats Len is not the same, stats>verify_group_stats"
64 if(len(stats)< len(verify_group_stats)):
Pierbbdf3782016-08-22 17:58:26 -070065 min_len = len(stats)
macauley_cheng67da9262015-08-31 15:18:41 +080066 print "Stats Len is not the same, stats<verify_group_stats"
Pierbbdf3782016-08-22 17:58:26 -070067 else:
macauley_cheng67da9262015-08-31 15:18:41 +080068 min_len = len(stats)
macauleye8b140e2015-08-03 13:35:45 +080069
macauley_cheng67da9262015-08-31 15:18:41 +080070 print "\r\n"
71 for i in range(min_len):
72 gs = stats[i]
Pierbbdf3782016-08-22 17:58:26 -070073 gv = verify_group_stats[i]
macauley_cheng67da9262015-08-31 15:18:41 +080074 print "FromSwtich:(GID=%lx, TYPE=%lx)\r\nVerify :(GID=%lx, TYPE=%lx)"%(gs.group_id, gs.group_type, gv.group_id, gv.group_type)
75 if(len(gs.buckets) != len(gv.buckets)):
76 print "buckets len is not the same gs %lx, gv %lx",(len(gs.buckets), len(gv.buckets))
77
78 for j in range(len(gs.buckets)):
79 b1=gs.buckets[j]
Pierbbdf3782016-08-22 17:58:26 -070080 b2=gv.buckets[j]
macauley_cheng67da9262015-08-31 15:18:41 +080081 if(len(b1.actions) != len(b2.actions)):
82 print "action len is not the same"
83
84 for k in range(len(b1.actions)):
85 a1=b1.actions[k]
86 a2=b2.actions[k]
87 if(always_show == True):
88 print "a1:"+a1.show()
Pierbbdf3782016-08-22 17:58:26 -070089 print "a2:"+a2.show()
macauley_cheng67da9262015-08-31 15:18:41 +080090
91def AssertGroup(self, stats, verify_group_stats):
92 self.assertTrue(len(stats) ==len(verify_group_stats), "stats len is not the same")
93
94 for i in range(len(stats)):
95 gs = stats[i]
Pierbbdf3782016-08-22 17:58:26 -070096 gv = verify_group_stats[i]
macauley_cheng67da9262015-08-31 15:18:41 +080097 self.assertTrue(len(gs.buckets) == len(gv.buckets), "buckets len is not the same")
98
99 for j in range(len(gs.buckets)):
100 b1=gs.buckets[j]
Pierbbdf3782016-08-22 17:58:26 -0700101 b2=gv.buckets[j]
macauley_cheng67da9262015-08-31 15:18:41 +0800102 self.assertTrue(len(b1.actions) == len(b2.actions), "action len is not the same")
103
104 for k in range(len(b1.actions)):
105 a1=b1.actions[k]
106 a2=b2.actions[k]
107 self.assertEquals(a1, a2, "action is not the same")
Pierbbdf3782016-08-22 17:58:26 -0700108
macauley97557232015-07-16 17:28:07 +0800109def encode_l2_interface_group_id(vlan, id):
110 return id + (vlan << OFDPA_VLAN_ID_SHIFT)
111
112def encode_l2_rewrite_group_id(id):
113 return id + (1 << OFDPA_GROUP_TYPE_SHIFT)
114
115def encode_l3_unicast_group_id(id):
116 return id + (2 << OFDPA_GROUP_TYPE_SHIFT)
117
118def encode_l2_mcast_group_id(vlan, id):
119 return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (3 << OFDPA_GROUP_TYPE_SHIFT)
120
121def encode_l2_flood_group_id(vlan, id):
122 return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (4 << OFDPA_GROUP_TYPE_SHIFT)
Pierbbdf3782016-08-22 17:58:26 -0700123
macauley97557232015-07-16 17:28:07 +0800124def encode_l3_interface_group_id(id):
125 return id + (5 << OFDPA_GROUP_TYPE_SHIFT)
126
127def encode_l3_mcast_group_id(vlan, id):
128 return id + (vlan << OFDPA_VLAN_ID_SHIFT)+(6 << OFDPA_GROUP_TYPE_SHIFT)
129
130def encode_l3_ecmp_group_id(id):
131 return id + (7 << OFDPA_GROUP_TYPE_SHIFT)
132
Flavio Castro91d1a552016-05-17 16:59:44 -0700133def encode_l2_unfiltered_group_id(id):
134 return id + (11 << OFDPA_GROUP_TYPE_SHIFT)
135
macauleyfddc4662015-07-27 17:40:30 +0800136def encode_l2_overlay_group_id(tunnel_id, subtype, index):
137 tunnel_id=tunnel_id&0xffff #16 bits
138 subtype = subtype&3 #2 bits
139 index = index & 0x3f #10 bits
140 return index + (tunnel_id << OFDPA_TUNNEL_ID_SHIFT)+ (subtype<<OFDPA_TUNNEL_SUBTYPE_SHIFT)+(8 << OFDPA_GROUP_TYPE_SHIFT)
macauley97557232015-07-16 17:28:07 +0800141
Flavio Castro91d1a552016-05-17 16:59:44 -0700142def add_l2_unfiltered_group(ctrl, ports, send_barrier=False):
143 # group table
144 # set up untag groups for each port
145 group_id_list=[]
146 msgs=[]
147 for of_port in ports:
148 # do stuff
149 group_id = encode_l2_unfiltered_group_id(of_port)
150 group_id_list.append(group_id)
151 actions = [ofp.action.output(of_port)]
152 actions.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=24, value=1)))
153
154 buckets = [ofp.bucket(actions=actions)]
155 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
156 group_id=group_id,
157 buckets=buckets
158 )
159 ctrl.message_send(request)
160 msgs.append(request)
161
162 if send_barrier:
163 do_barrier(ctrl)
164
165 return group_id_list, msgs
166
Pierf6f28162016-09-22 16:30:52 -0700167def add_one_l2_unfiltered_group(ctrl, of_port, send_barrier=False):
168 # group table
169 # set up untag groups for each port
170 group_id = encode_l2_unfiltered_group_id(of_port)
171 actions = [ofp.action.output(of_port)]
172 actions.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=24, value=1)))
173
174 buckets = [ofp.bucket(actions=actions)]
175 request = ofp.message.group_add(
176 group_type=ofp.OFPGT_INDIRECT,
177 group_id=group_id,
178 buckets=buckets
179 )
180 ctrl.message_send(request)
181
182 if send_barrier:
183 do_barrier(ctrl)
184
185 return group_id, request
186
Flavio Castrod4c44d12015-12-08 14:44:18 -0500187def add_l2_interface_group(ctrl, ports, vlan_id=1, is_tagged=False, send_barrier=False):
macauley97557232015-07-16 17:28:07 +0800188 # group table
189 # set up untag groups for each port
macauley41904ed2015-07-16 17:38:35 +0800190 group_id_list=[]
macauley15909e72015-07-17 15:58:57 +0800191 msgs=[]
macauley97557232015-07-16 17:28:07 +0800192 for of_port in ports:
193 # do stuff
194 group_id = encode_l2_interface_group_id(vlan_id, of_port)
macauley41904ed2015-07-16 17:38:35 +0800195 group_id_list.append(group_id)
macauley97557232015-07-16 17:28:07 +0800196 if is_tagged:
197 actions = [
198 ofp.action.output(of_port),
Pierbbdf3782016-08-22 17:58:26 -0700199 ]
macauley97557232015-07-16 17:28:07 +0800200 else:
201 actions = [
202 ofp.action.pop_vlan(),
203 ofp.action.output(of_port),
204 ]
205
206 buckets = [
207 ofp.bucket(actions=actions),
208 ]
209
210 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
211 group_id=group_id,
212 buckets=buckets
213 )
214 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800215 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800216
217 if send_barrier:
218 do_barrier(ctrl)
Pierbbdf3782016-08-22 17:58:26 -0700219
macauley15909e72015-07-17 15:58:57 +0800220 return group_id_list, msgs
macauley97557232015-07-16 17:28:07 +0800221
Flavio Castrod4c44d12015-12-08 14:44:18 -0500222def add_one_l2_interface_group(ctrl, port, vlan_id=1, is_tagged=False, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800223 # group table
224 # set up untag groups for each port
225 group_id = encode_l2_interface_group_id(vlan_id, port)
226
227 if is_tagged:
228 actions = [
229 ofp.action.output(port),
Pierbbdf3782016-08-22 17:58:26 -0700230 ]
macauley0f91a3e2015-07-17 18:09:59 +0800231 else:
232 actions = [
233 ofp.action.pop_vlan(),
234 ofp.action.output(port),
235 ]
236
237 buckets = [
238 ofp.bucket(actions=actions),
239 ]
240
241 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
242 group_id=group_id,
243 buckets=buckets
244 )
245 ctrl.message_send(request)
246
247 if send_barrier:
248 do_barrier(ctrl)
Pierbbdf3782016-08-22 17:58:26 -0700249
macauley0f91a3e2015-07-17 18:09:59 +0800250 return group_id, request
Pierbbdf3782016-08-22 17:58:26 -0700251
macauley97557232015-07-16 17:28:07 +0800252def add_l2_mcast_group(ctrl, ports, vlanid, mcast_grp_index):
253 buckets=[]
254 for of_port in ports:
255 group_id = encode_l2_interface_group_id(vlanid, of_port)
256 action=[ofp.action.group(group_id)]
257 buckets.append(ofp.bucket(actions=action))
258
259 group_id =encode_l2_mcast_group_id(vlanid, mcast_grp_index)
260 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
261 group_id=group_id,
262 buckets=buckets
263 )
264 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800265 return request
macauley97557232015-07-16 17:28:07 +0800266
macauley15909e72015-07-17 15:58:57 +0800267def add_l2_flood_group(ctrl, ports, vlanid, id):
268 buckets=[]
269 for of_port in ports:
270 group_id = encode_l2_interface_group_id(vlanid, of_port)
271 action=[ofp.action.group(group_id)]
272 buckets.append(ofp.bucket(actions=action))
macauley97557232015-07-16 17:28:07 +0800273
macauley15909e72015-07-17 15:58:57 +0800274 group_id =encode_l2_flood_group_id(vlanid, id)
275 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
276 group_id=group_id,
277 buckets=buckets
278 )
279 ctrl.message_send(request)
280 return request
281
Flavio Castroa7162bb2016-07-25 17:30:30 -0700282def mod_l2_flood_group(ctrl, ports, vlanid, id):
283 buckets=[]
284 for of_port in ports:
285 group_id = encode_l2_interface_group_id(vlanid, of_port)
286 action=[ofp.action.group(group_id)]
287 buckets.append(ofp.bucket(actions=action))
288
289 group_id =encode_l2_flood_group_id(vlanid, id)
290 request = ofp.message.group_modify(group_type=ofp.OFPGT_ALL,
291 group_id=group_id,
292 buckets=buckets
293 )
294 ctrl.message_send(request)
295 return request
296
297
macauley15909e72015-07-17 15:58:57 +0800298def add_l2_rewrite_group(ctrl, port, vlanid, id, src_mac, dst_mac):
299 group_id = encode_l2_interface_group_id(vlanid, port)
300
301 action=[]
302 if src_mac is not None:
303 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
304
305 if dst_mac is not None:
306 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
307
Flavio Castro91d1a552016-05-17 16:59:44 -0700308 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
Pierbbdf3782016-08-22 17:58:26 -0700309
macauley15909e72015-07-17 15:58:57 +0800310 action.append(ofp.action.group(group_id))
Pierbbdf3782016-08-22 17:58:26 -0700311
macauley15909e72015-07-17 15:58:57 +0800312 buckets = [ofp.bucket(actions=action)]
313
314 group_id =encode_l2_rewrite_group_id(id)
315 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
316 group_id=group_id,
317 buckets=buckets
318 )
319 ctrl.message_send(request)
320 return request
Pierbbdf3782016-08-22 17:58:26 -0700321
macauley15909e72015-07-17 15:58:57 +0800322def add_l3_unicast_group(ctrl, port, vlanid, id, src_mac, dst_mac):
323 group_id = encode_l2_interface_group_id(vlanid, port)
324
325 action=[]
326 if src_mac is not None:
327 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
328
329 if dst_mac is not None:
330 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
331
Flavio Castroaf2b4502016-02-02 17:41:32 -0500332 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
Pierbbdf3782016-08-22 17:58:26 -0700333
macauley15909e72015-07-17 15:58:57 +0800334 action.append(ofp.action.group(group_id))
Pierbbdf3782016-08-22 17:58:26 -0700335
macauley15909e72015-07-17 15:58:57 +0800336 buckets = [ofp.bucket(actions=action)]
337
338 group_id =encode_l3_unicast_group_id(id)
339 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
340 group_id=group_id,
341 buckets=buckets
342 )
343 ctrl.message_send(request)
344 return request
Pierbbdf3782016-08-22 17:58:26 -0700345
macauley15909e72015-07-17 15:58:57 +0800346def add_l3_interface_group(ctrl, port, vlanid, id, src_mac):
347 group_id = encode_l2_interface_group_id(vlanid, port)
348
349 action=[]
350 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
Pierbbdf3782016-08-22 17:58:26 -0700351 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
macauley15909e72015-07-17 15:58:57 +0800352 action.append(ofp.action.group(group_id))
Pierbbdf3782016-08-22 17:58:26 -0700353
macauley15909e72015-07-17 15:58:57 +0800354 buckets = [ofp.bucket(actions=action)]
355
356 group_id =encode_l3_interface_group_id(id)
357 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
358 group_id=group_id,
359 buckets=buckets
360 )
361 ctrl.message_send(request)
362 return request
363
364def add_l3_ecmp_group(ctrl, id, l3_ucast_groups):
365 buckets=[]
366 for group in l3_ucast_groups:
367 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
368
369 group_id =encode_l3_ecmp_group_id(id)
370 request = ofp.message.group_add(group_type=ofp.OFPGT_SELECT,
371 group_id=group_id,
372 buckets=buckets
373 )
374 ctrl.message_send(request)
375 return request
Flavio Castroa7162bb2016-07-25 17:30:30 -0700376
377def mod_l3_ecmp_group(ctrl, id, l3_ucast_groups):
378 buckets=[]
379 for group in l3_ucast_groups:
380 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
381
382 group_id =encode_l3_ecmp_group_id(id)
383 request = ofp.message.group_modify(group_type=ofp.OFPGT_SELECT,
384 group_id=group_id,
385 buckets=buckets
386 )
387 ctrl.message_send(request)
388 return request
389
macauley15909e72015-07-17 15:58:57 +0800390def add_l3_mcast_group(ctrl, vid, mcast_group_id, groups_on_buckets):
391 buckets=[]
392 for group in groups_on_buckets:
393 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
Pierbbdf3782016-08-22 17:58:26 -0700394
macauley15909e72015-07-17 15:58:57 +0800395 group_id =encode_l3_mcast_group_id(vid, mcast_group_id)
396 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
397 group_id=group_id,
398 buckets=buckets
399 )
400 ctrl.message_send(request)
401 return request
macauleyfddc4662015-07-27 17:40:30 +0800402
403def add_l2_overlay_flood_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
404 buckets=[]
405 for port in ports:
406 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
407
408 group_id=encode_l2_overlay_group_id(tunnel_id, 0, index)
409 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
410 group_id=group_id,
411 buckets=buckets
412 )
413 ctrl.message_send(request)
414 return request
415
416def add_l2_overlay_flood_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
417 buckets=[]
418 for port in ports:
419 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
420
421 group_id=encode_l2_overlay_group_id(tunnel_id, 1, index)
422 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
423 group_id=group_id,
424 buckets=buckets
425 )
426 ctrl.message_send(request)
427 return request
428
429def add_l2_overlay_mcast_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
430 buckets=[]
431 for port in ports:
432 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
433
434 group_id=encode_l2_overlay_group_id(tunnel_id, 2, index)
435 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
436 group_id=group_id,
437 buckets=buckets
438 )
439 ctrl.message_send(request)
440 return request
441
442def add_l2_overlay_mcast_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
443 buckets=[]
444 for port in ports:
445 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
446
447 group_id=encode_l2_overlay_group_id(tunnel_id, 3, index)
448 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
449 group_id=group_id,
450 buckets=buckets
451 )
452 ctrl.message_send(request)
453 return request
Pierbbdf3782016-08-22 17:58:26 -0700454
macauleyfddc4662015-07-27 17:40:30 +0800455def add_port_table_flow(ctrl, is_overlay=True):
456 match = ofp.match()
457
458 if is_overlay == True:
459 match.oxm_list.append(ofp.oxm.in_port(0x10000))
macauleydbff3272015-07-30 14:07:16 +0800460 NEXT_TABLE=50
macauleyfddc4662015-07-27 17:40:30 +0800461 else:
462 match.oxm_list.append(ofp.oxm.in_port(0))
Pierbbdf3782016-08-22 17:58:26 -0700463 NEXT_TABLE=10
macauleyfddc4662015-07-27 17:40:30 +0800464
465 request = ofp.message.flow_add(
466 table_id=0,
467 cookie=42,
468 match=match,
469 instructions=[
macauleydbff3272015-07-30 14:07:16 +0800470 ofp.instruction.goto_table(NEXT_TABLE)
macauleyfddc4662015-07-27 17:40:30 +0800471 ],
472 priority=0)
473 logging.info("Add port table, match port %lx" % 0x10000)
474 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700475
Flavio Castrod8f8af22015-12-02 18:19:26 -0500476def pop_vlan_flow(ctrl, ports, vlan_id=1):
477 # table 10: vlan
478 # goto to table 20
479 msgs=[]
480 for of_port in ports:
481 match = ofp.match()
482 match.oxm_list.append(ofp.oxm.in_port(of_port))
483 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
484 request = ofp.message.flow_add(
485 table_id=10,
486 cookie=42,
487 match=match,
488 instructions=[
489 ofp.instruction.apply_actions(
490 actions=[
491 ofp.action.pop_vlan()
492 ]
493 ),
494 ofp.instruction.goto_table(20)
495 ],
496 priority=0)
497 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
498 ctrl.message_send(request)
499
500
501 return msgs
macauleyfddc4662015-07-27 17:40:30 +0800502
macauley97557232015-07-16 17:28:07 +0800503def add_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
504 # table 10: vlan
505 # goto to table 20
macauley15909e72015-07-17 15:58:57 +0800506 msgs=[]
macauley97557232015-07-16 17:28:07 +0800507 for of_port in ports:
Flavio Castro932014b2016-01-05 18:29:15 -0500508 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
macauley97557232015-07-16 17:28:07 +0800509 match = ofp.match()
510 match.oxm_list.append(ofp.oxm.in_port(of_port))
511 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
512 request = ofp.message.flow_add(
513 table_id=10,
514 cookie=42,
515 match=match,
516 instructions=[
Flavio Castrod8f8af22015-12-02 18:19:26 -0500517 ofp.instruction.apply_actions(
518 actions=[
519 ofp.action.pop_vlan()
520 ]
521 ),
macauley97557232015-07-16 17:28:07 +0800522 ofp.instruction.goto_table(20)
523 ],
524 priority=0)
525 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
526 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700527
macauley97557232015-07-16 17:28:07 +0800528 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
529 match = ofp.match()
530 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500531 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
macauley97557232015-07-16 17:28:07 +0800532 request = ofp.message.flow_add(
533 table_id=10,
534 cookie=42,
535 match=match,
536 instructions=[
537 ofp.instruction.apply_actions(
538 actions=[
Flavio Castroaf2b4502016-02-02 17:41:32 -0500539 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
macauley97557232015-07-16 17:28:07 +0800540 ]
541 ),
542 ofp.instruction.goto_table(20)
543 ],
544 priority=0)
545 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
546 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800547 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800548
Flavio Castro932014b2016-01-05 18:29:15 -0500549 if (flag == 4) :
550 match = ofp.match()
551 match.oxm_list.append(ofp.oxm.in_port(of_port))
552 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1fff))
553 request = ofp.message.flow_add(
554 table_id=10,
555 cookie=42,
556 match=match,
557 instructions=[
558 ofp.instruction.apply_actions(
559 actions=[
Flavio Castroaf2b4502016-02-02 17:41:32 -0500560 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
Flavio Castro932014b2016-01-05 18:29:15 -0500561 ]
562 ),
563 ofp.instruction.goto_table(20)
564 ],
565 priority=0)
566 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
567 ctrl.message_send(request)
568 msgs.append(request)
569
macauley97557232015-07-16 17:28:07 +0800570 if send_barrier:
571 do_barrier(ctrl)
572
macauley15909e72015-07-17 15:58:57 +0800573 return msgs
Pierbbdf3782016-08-22 17:58:26 -0700574
macauley_cheng6e6a6122015-11-16 14:19:18 +0800575def del_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
576 # table 10: vlan
577 # goto to table 20
578 msgs=[]
579 for of_port in ports:
580 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
581 match = ofp.match()
582 match.oxm_list.append(ofp.oxm.in_port(of_port))
583 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
584 request = ofp.message.flow_delete(
585 table_id=10,
586 cookie=42,
587 match=match,
588 priority=0)
589 logging.info("Del vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
590 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800591
macauley_cheng6e6a6122015-11-16 14:19:18 +0800592 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
593 match = ofp.match()
594 match.oxm_list.append(ofp.oxm.in_port(of_port))
595 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0xfff))
596 request = ofp.message.flow_delete(
597 table_id=10,
598 cookie=42,
599 match=match,
600 priority=0)
601 logging.info("Del vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
602 ctrl.message_send(request)
603 msgs.append(request)
604
605 if send_barrier:
606 do_barrier(ctrl)
607
608 return msgs
Pierbbdf3782016-08-22 17:58:26 -0700609
macauley_cheng6b311612015-09-04 11:32:27 +0800610def add_vlan_table_flow_pvid(ctrl, in_port, match_vid=None, pvid=1, send_barrier=False):
611 """it will tag pack as untagged packet wether it has tagg or not"""
612 match = ofp.match()
613 match.oxm_list.append(ofp.oxm.in_port(in_port))
614 actions=[]
615 if match_vid == None:
Pierbbdf3782016-08-22 17:58:26 -0700616 match.oxm_list.append(ofp.oxm.vlan_vid(0))
macauley_cheng6b311612015-09-04 11:32:27 +0800617 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
618 goto_table=20
619 else:
620 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+match_vid, 0x1fff))
621 actions.append(ofp.action.push_vlan(0x8100))
Pierbbdf3782016-08-22 17:58:26 -0700622 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
macauley_cheng6b311612015-09-04 11:32:27 +0800623 goto_table=20
Pierbbdf3782016-08-22 17:58:26 -0700624
macauley_cheng6b311612015-09-04 11:32:27 +0800625 request = ofp.message.flow_add(
626 table_id=10,
627 cookie=42,
628 match=match,
629 instructions=[
630 ofp.instruction.apply_actions(actions=actions)
631 ,ofp.instruction.goto_table(goto_table)
632 ],
633 priority=0)
634 logging.info("Add PVID %d on port %d and go to table %ld" %( pvid, in_port, goto_table))
Pierbbdf3782016-08-22 17:58:26 -0700635 ctrl.message_send(request)
636
macauley_cheng6b311612015-09-04 11:32:27 +0800637 if send_barrier:
638 do_barrier(ctrl)
639
640def add_vlan_table_flow_allow_all_vlan(ctrl, in_port, send_barrier=False):
641 """it st flow allow all vlan tag on this port"""
642 match = ofp.match()
643 match.oxm_list.append(ofp.oxm.in_port(in_port))
644 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1000))
645 request = ofp.message.flow_add(
646 table_id=10,
647 cookie=42,
648 match=match,
649 instructions=[
Pierbbdf3782016-08-22 17:58:26 -0700650 ofp.instruction.goto_table(20)
macauley_cheng6b311612015-09-04 11:32:27 +0800651 ],
652 priority=0)
653 logging.info("Add allow all vlan on port %d " %(in_port))
Pierbbdf3782016-08-22 17:58:26 -0700654 ctrl.message_send(request)
macauley_cheng6b311612015-09-04 11:32:27 +0800655
Pier7b031af2016-08-25 15:00:22 -0700656def 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):
657 # Install a flow for VLAN translation
658 # in VLAN table.
659 # table 10: vlan
660 # goto to table 20
661 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
662 match = ofp.match()
663 match.oxm_list.append(ofp.oxm.in_port(of_port))
664 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
665
666 actions=[]
667 if vrf!=0:
668 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
669 if new_vlan_id != -1:
670 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_vlan_id)))
671
672 request = ofp.message.flow_add(
673 table_id=10,
674 cookie=42,
675 match=match,
676 instructions=[
677 ofp.instruction.apply_actions(
678 actions=actions
679 ),
680 ofp.instruction.goto_table(20)
681 ],
682 priority=0)
683 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
684 ctrl.message_send(request)
685
Piere1308762016-09-12 15:29:56 -0700686def 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 +0800687 # table 10: vlan
688 # goto to table 20
Flavio Castro932014b2016-01-05 18:29:15 -0500689 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
macauley0f91a3e2015-07-17 18:09:59 +0800690 match = ofp.match()
691 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500692 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
macauley7f89d962015-08-06 18:13:48 +0800693
694 actions=[]
695 if vrf!=0:
696 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
Pierb5da4c92016-09-21 11:23:35 -0700697 if mpls_type!=None:
698 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=mpls_type)))
Pierbbdf3782016-08-22 17:58:26 -0700699
Flavio Castro6d498522015-12-15 14:05:04 -0500700 #actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id)))
macauley7f89d962015-08-06 18:13:48 +0800701
macauley0f91a3e2015-07-17 18:09:59 +0800702 request = ofp.message.flow_add(
703 table_id=10,
704 cookie=42,
705 match=match,
706 instructions=[
macauley53d90fe2015-08-04 17:34:22 +0800707 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800708 actions=actions
macauley53d90fe2015-08-04 17:34:22 +0800709 ),
710 ofp.instruction.goto_table(20)
macauley0f91a3e2015-07-17 18:09:59 +0800711 ],
712 priority=0)
713 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
714 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700715
macauley0f91a3e2015-07-17 18:09:59 +0800716 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
717 match = ofp.match()
718 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500719 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
Pierbbdf3782016-08-22 17:58:26 -0700720
macauley7f89d962015-08-06 18:13:48 +0800721 actions=[]
722 if vrf!=0:
723 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
Pierbbdf3782016-08-22 17:58:26 -0700724
Flavio Castro91d1a552016-05-17 16:59:44 -0700725 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Pierbbdf3782016-08-22 17:58:26 -0700726
macauley0f91a3e2015-07-17 18:09:59 +0800727 request = ofp.message.flow_add(
728 table_id=10,
729 cookie=42,
730 match=match,
731 instructions=[
732 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800733 actions=actions
macauley0f91a3e2015-07-17 18:09:59 +0800734 ),
735 ofp.instruction.goto_table(20)
736 ],
737 priority=0)
738 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
739 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700740
Flavio Castro932014b2016-01-05 18:29:15 -0500741 if (flag == 4) :
742 match = ofp.match()
743 match.oxm_list.append(ofp.oxm.in_port(of_port))
744 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000,0x1fff))
745
746 actions=[]
747 if vrf!=0:
748 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
749
Flavio Castro91d1a552016-05-17 16:59:44 -0700750 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Flavio Castro932014b2016-01-05 18:29:15 -0500751
752 request = ofp.message.flow_add(
753 table_id=10,
754 cookie=42,
755 match=match,
756 instructions=[
757 ofp.instruction.apply_actions(
758 actions=actions
759 ),
760 ofp.instruction.goto_table(20)
761 ],
762 priority=0)
763 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
764 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800765
Piere1308762016-09-12 15:29:56 -0700766 if (flag == VLAN_TABLE_FLAG_ONLY_STACKED):
767 # This flag is meant to managed stacked vlan packtes
768 # Matches on outer VLAN_ID, set OVID with outer VLAN.
769 # Finally expose inner VLAN_ID with a pop action and
770 # goto VLAN_1_FLOW_TABLE
771 match = ofp.match()
772 match.oxm_list.append(ofp.oxm.in_port(of_port))
773 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
774
775 actions=[]
776 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_OVID, value=0x1000+vlan_id)))
777 actions.append(ofp.action.pop_vlan())
778
779 request = ofp.message.flow_add(
780 table_id=10,
781 cookie=42,
782 match=match,
783 instructions=[
784 ofp.instruction.apply_actions(
785 actions=actions
786 ),
787 ofp.instruction.goto_table(VLAN_1_FLOW_TABLE)
788 ],
789 priority=0)
790 logging.info("Add vlan %d tagged packets on port %d and go to table %d" %( vlan_id, of_port, VLAN_1_FLOW_TABLE))
791 ctrl.message_send(request)
792
793 if (flag == VLAN_TABLE_FLAG_PRIORITY) :
794 match = ofp.match()
795 match.oxm_list.append(ofp.oxm.in_port(of_port))
796 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1fff))
797 request = ofp.message.flow_add(
798 table_id=10,
799 cookie=42,
800 match=match,
801 instructions=[
802 ofp.instruction.apply_actions(
803 actions=[
804 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)),
805 ofp.action.push_vlan(0x8100),
806 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+out_vlan_id)),
807 ]
808 ),
809 ofp.instruction.goto_table(20)
810 ],
811 priority=0)
812 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
813 ctrl.message_send(request)
814
815 if send_barrier:
816 do_barrier(ctrl)
817
818 return request
819
Piercf76e802016-09-19 20:16:35 -0700820def 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):
821 # table 10: vlan
822 # goto to table 13
823 if flag == VLAN_TABLE_FLAG_ONLY_TAG:
824 match = ofp.match()
825 match.oxm_list.append(ofp.oxm.in_port(of_port))
826 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id, 0x1fff))
827
828 actions=[]
829 if vlan_id == -1:
830 actions.append(ofp.action.pop_vlan())
831 if new_vlan_id > 1:
832 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_vlan_id)))
833 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
834 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
835 # 0x0000nnnn is for UNI interfaces
836 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
837
838 request = ofp.message.flow_add(
839 table_id=10,
840 cookie=42,
841 match=match,
842 instructions=[
843 ofp.instruction.apply_actions(
844 actions=actions
845 ),
846 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
847 ],
848 priority=0)
849 logging.info("Add vlan %d tagged packets on port %d and go to table %d" % (vlan_id, of_port, MPLS_L2_PORT_FLOW_TABLE))
850 ctrl.message_send(request)
851
852 if flag == VLAN_TABLE_FLAG_ONLY_UNTAG:
853 match = ofp.match()
854 match.oxm_list.append(ofp.oxm.in_port(of_port))
855 match.oxm_list.append(ofp.oxm.vlan_vid(0))
856
857 actions=[]
858 if vlan_id > 1:
859 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
860 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
861 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
862 # 0x0000nnnn is for UNI interfaces
863 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
864
865 request = ofp.message.flow_add(
866 table_id=10,
867 cookie=42,
868 match=match,
869 instructions=[
870 ofp.instruction.apply_actions(
871 actions=actions
872 ),
873 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
874 ],
875 priority=0)
876 logging.info("Add vlan %d untagged packets on port %d and go to table %d" % (vlan_id, of_port, MPLS_L2_PORT_FLOW_TABLE))
877 ctrl.message_send(request)
878
879 if send_barrier:
880 do_barrier(ctrl)
881
882 return request
883
Piere1308762016-09-12 15:29:56 -0700884def 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):
885 # table 11: vlan 1 table
886 # goto to table 20
887 if flag == VLAN_TABLE_FLAG_ONLY_TAG:
888 match = ofp.match()
889 match.oxm_list.append(ofp.oxm.in_port(of_port))
890 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
891 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
892
893 actions=[]
894 actions.append(ofp.action.push_vlan(0x8100))
895 if new_outer_vlan_id != -1:
896 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_outer_vlan_id)))
897 else:
898 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan_id)))
899
900 request = ofp.message.flow_add(
901 table_id=11,
902 cookie=42,
903 match=match,
904 instructions=[
905 ofp.instruction.apply_actions(
906 actions=actions
907 ),
908 ofp.instruction.goto_table(TERMINATION_FLOW_TABLE)
909 ],
910 priority=0)
911 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))
912 ctrl.message_send(request)
913
914 if flag == VLAN_TABLE_FLAG_ONLY_UNTAG:
915 match = ofp.match()
916 match.oxm_list.append(ofp.oxm.in_port(of_port))
917 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
918 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
919
920 actions=[]
921 request = ofp.message.flow_add(
922 table_id=11,
923 cookie=42,
924 match=match,
925 instructions=[
926 ofp.instruction.apply_actions(
927 actions=actions
928 ),
929 ofp.instruction.goto_table(TERMINATION_FLOW_TABLE)
930 ],
931 priority=0)
932 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))
933 ctrl.message_send(request)
934
macauley0f91a3e2015-07-17 18:09:59 +0800935 if send_barrier:
936 do_barrier(ctrl)
937
938 return request
Pierbbdf3782016-08-22 17:58:26 -0700939
Piercf76e802016-09-19 20:16:35 -0700940def 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):
941 # table 11: vlan 1 table
942 # goto to table 13
943 match = ofp.match()
944 match.oxm_list.append(ofp.oxm.in_port(of_port))
945 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
946 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
947
948 actions=[]
949 actions.append(ofp.action.push_vlan(0x8100))
950 if new_outer_vlan_id != -1:
951 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_outer_vlan_id)))
952 else:
953 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan_id)))
954
955 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
956 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
957 # 0x0000nnnn is for UNI interfaces
958 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
959
960 request = ofp.message.flow_add(
961 table_id=11,
962 cookie=42,
963 match=match,
964 instructions=[
965 ofp.instruction.apply_actions(
966 actions=actions
967 ),
968 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
969 ],
970 priority=0)
971 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))
972 ctrl.message_send(request)
973
974 if send_barrier:
975 do_barrier(ctrl)
976
977 return request
978
macauley97557232015-07-16 17:28:07 +0800979def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False):
980 match = ofp.match()
castroflavio21894482015-12-08 15:29:55 -0500981 priority=500
macauleyfddc4662015-07-27 17:40:30 +0800982 if dst_mac!=None:
castroflavio21894482015-12-08 15:29:55 -0500983 priority=1000
macauleyfddc4662015-07-27 17:40:30 +0800984 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
985
macauley97557232015-07-16 17:28:07 +0800986 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauleyfddc4662015-07-27 17:40:30 +0800987
macauley97557232015-07-16 17:28:07 +0800988 request = ofp.message.flow_add(
989 table_id=50,
990 cookie=42,
991 match=match,
992 instructions=[
993 ofp.instruction.write_actions(
994 actions=[
995 ofp.action.group(group_id)]),
996 ofp.instruction.goto_table(60)
997 ],
998 buffer_id=ofp.OFP_NO_BUFFER,
castroflavio21894482015-12-08 15:29:55 -0500999 priority=priority)
macauley97557232015-07-16 17:28:07 +08001000
1001 logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac)
1002 ctrl.message_send(request)
1003
1004 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001005 do_barrier(ctrl)
macauley15909e72015-07-17 15:58:57 +08001006
Pierbbdf3782016-08-22 17:58:26 -07001007 return request
macauleyfddc4662015-07-27 17:40:30 +08001008
1009def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False):
1010 match = ofp.match()
1011 if dst_mac!=None:
1012 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
1013
1014 match.oxm_list.append(ofp.oxm.tunnel_id(vnid))
1015 if is_group == True:
1016 actions=[ofp.action.group(group_id)]
1017 else:
1018 actions=[ofp.action.output(group_id)]
1019
1020 request = ofp.message.flow_add(
1021 table_id=50,
1022 cookie=42,
1023 match=match,
1024 instructions=[
1025 ofp.instruction.write_actions(
1026 actions=actions),
1027 ofp.instruction.goto_table(60)
1028 ],
1029 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001030 priority=1000)
macauleyfddc4662015-07-27 17:40:30 +08001031
1032 logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac)
1033 ctrl.message_send(request)
1034
1035 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001036 do_barrier(ctrl)
macauleyfddc4662015-07-27 17:40:30 +08001037
Pierbbdf3782016-08-22 17:58:26 -07001038 return request
1039
macauley_cheng6b133662015-11-09 13:52:39 +08001040def add_termination_flow(ctrl, in_port, eth_type, dst_mac, vlanid, goto_table=None, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +08001041 match = ofp.match()
macauley0f91a3e2015-07-17 18:09:59 +08001042 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauleyfddc4662015-07-27 17:40:30 +08001043 if dst_mac[0]&0x01 == 0x01:
1044 match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00]))
1045 goto_table=40
1046 else:
macauley53d90fe2015-08-04 17:34:22 +08001047 if in_port!=0:
1048 match.oxm_list.append(ofp.oxm.in_port(in_port))
macauleyfddc4662015-07-27 17:40:30 +08001049 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
1050 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauley_cheng6b133662015-11-09 13:52:39 +08001051 if goto_table == None:
1052 goto_table=30
macauley0f91a3e2015-07-17 18:09:59 +08001053
1054 request = ofp.message.flow_add(
1055 table_id=20,
1056 cookie=42,
1057 match=match,
1058 instructions=[
1059 ofp.instruction.goto_table(goto_table)
1060 ],
1061 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001062 priority=1)
macauley0f91a3e2015-07-17 18:09:59 +08001063
1064 logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac)
1065 ctrl.message_send(request)
1066
1067 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001068 do_barrier(ctrl)
macauley0f91a3e2015-07-17 18:09:59 +08001069
Pierbbdf3782016-08-22 17:58:26 -07001070 return request
1071
Flavio Castroaf2b4502016-02-02 17:41:32 -05001072def 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 +08001073 match = ofp.match()
1074 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauley53d90fe2015-08-04 17:34:22 +08001075 if vrf != 0:
1076 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
macauley0f91a3e2015-07-17 18:09:59 +08001077
Flavio Castroaf2b4502016-02-02 17:41:32 -05001078 match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask))
1079
1080 instructions = []
1081 instructions.append(ofp.instruction.goto_table(60))
1082 if send_ctrl:
Piere0918ec2016-09-09 20:06:05 -07001083 instructions.append(ofp.instruction.apply_actions(
Flavio Castroaf2b4502016-02-02 17:41:32 -05001084 actions=[ofp.action.output( port=ofp.OFPP_CONTROLLER,
1085 max_len=ofp.OFPCML_NO_BUFFER)]))
Pierbbdf3782016-08-22 17:58:26 -07001086 else:
Flavio Castroaf2b4502016-02-02 17:41:32 -05001087 instructions.append(ofp.instruction.write_actions(
1088 actions=[ofp.action.group(action_group_id)]))
macauley53d90fe2015-08-04 17:34:22 +08001089
macauley0f91a3e2015-07-17 18:09:59 +08001090 request = ofp.message.flow_add(
1091 table_id=30,
1092 cookie=42,
1093 match=match,
Flavio Castroaf2b4502016-02-02 17:41:32 -05001094 instructions=instructions,
macauley0f91a3e2015-07-17 18:09:59 +08001095 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001096 priority=1)
macauley0f91a3e2015-07-17 18:09:59 +08001097
1098 logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip)
1099 ctrl.message_send(request)
1100
1101 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001102 do_barrier(ctrl)
macauley0f91a3e2015-07-17 18:09:59 +08001103
Flavio Castro9debaaa2016-07-26 19:37:50 -07001104 return request
Flavio Castrod8f8af22015-12-02 18:19:26 -05001105
Flavio Castro8ca52542016-04-11 11:24:49 -04001106def 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 -04001107 match = ofp.match()
1108 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1109 match.oxm_list.append(ofp.oxm.mpls_label(label))
1110 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
Pierb5da4c92016-09-21 11:23:35 -07001111 actions = []
Flavio Castrob702a2f2016-04-10 22:01:48 -04001112 actions = [ofp.action.dec_mpls_ttl(),
Flavio Castro8ca52542016-04-11 11:24:49 -04001113 ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))]
1114 if (goto_table == 29):
Flavio Castro8ca52542016-04-11 11:24:49 -04001115 actions.append(ofp.action.group(action_group_id))
1116 else:
Flavio Castrod80fbc32016-07-25 15:54:26 -07001117 actions.append(ofp.action.set_field(
1118 ofp.oxm.exp2ByteValue(exp_type=23, value=32)))
1119 actions.append(ofp.action.group(action_group_id))
Flavio Castro8ca52542016-04-11 11:24:49 -04001120 actions.append(ofp.action.copy_ttl_in())
Flavio Castro9debaaa2016-07-26 19:37:50 -07001121
Flavio Castrob702a2f2016-04-10 22:01:48 -04001122 request = ofp.message.flow_add(
1123 table_id=24,
1124 cookie=43,
1125 match=match,
1126 instructions=[
Flavio Castro8ca52542016-04-11 11:24:49 -04001127 ofp.instruction.apply_actions(actions=actions),
1128 ofp.instruction.goto_table(goto_table)
Flavio Castrob702a2f2016-04-10 22:01:48 -04001129 ],
1130 buffer_id=ofp.OFP_NO_BUFFER,
1131 priority=1)
Flavio Castrob702a2f2016-04-10 22:01:48 -04001132 logging.info("Inserting MPLS flow , label %ld", label)
1133 ctrl.message_send(request)
1134
1135 if send_barrier:
1136 do_barrier(ctrl)
1137
1138 return request
1139
Pierf6f28162016-09-22 16:30:52 -07001140def 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 -07001141 match = ofp.match()
1142 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1143 match.oxm_list.append(ofp.oxm.mpls_label(label))
1144 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
1145
1146 actions = []
1147 actions.append(ofp.action.dec_mpls_ttl())
Pierf6f28162016-09-22 16:30:52 -07001148 if popMPLS == True:
Pierb5da4c92016-09-21 11:23:35 -07001149 actions.append(ofp.action.copy_ttl_in())
1150 actions.append(ofp.action.pop_mpls(ethertype))
Pierf6f28162016-09-22 16:30:52 -07001151 if bos==1 and popL2 == True:
1152 actions.append(ofp.action.ofdpa_pop_l2_header())
1153 actions.append(ofp.action.ofdpa_pop_cw())
1154 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
1155 # 0x0002nnnn is for UNI interfaces
1156 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00020000 + of_port)))
1157 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 -07001158 actions.append(ofp.action.group(action_group_id))
1159
1160 request = ofp.message.flow_add(
1161 table_id=24,
1162 cookie=43,
1163 match=match,
1164 instructions=[
1165 ofp.instruction.apply_actions(actions=actions),
1166 ofp.instruction.goto_table(goto_table)
1167 ],
1168 buffer_id=ofp.OFP_NO_BUFFER,
1169 priority=1
1170 )
1171 logging.info("Inserting MPLS flow , label %ld", label)
1172 ctrl.message_send(request)
1173
1174 if send_barrier:
1175 do_barrier(ctrl)
1176
1177 return request
1178
macauleyfddc4662015-07-27 17:40:30 +08001179def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False):
1180 match = ofp.match()
1181 match.oxm_list.append(ofp.oxm.eth_type(0x0800))
Pierbbdf3782016-08-22 17:58:26 -07001182 match.oxm_list.append(ofp.oxm.vlan_vid(vlan_id))
macauleyfddc4662015-07-27 17:40:30 +08001183 if src_ip_mask!=0:
1184 match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask))
1185 else:
1186 match.oxm_list.append(ofp.oxm.ipv4_src(src_ip))
Pierbbdf3782016-08-22 17:58:26 -07001187
macauleyfddc4662015-07-27 17:40:30 +08001188 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
Pierbbdf3782016-08-22 17:58:26 -07001189
macauleyfddc4662015-07-27 17:40:30 +08001190 request = ofp.message.flow_add(
1191 table_id=40,
1192 cookie=42,
1193 match=match,
1194 instructions=[
1195 ofp.instruction.write_actions(
1196 actions=[ofp.action.group(action_group_id)]),
1197 ofp.instruction.goto_table(60)
1198 ],
1199 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001200 priority=1)
macauleyfddc4662015-07-27 17:40:30 +08001201
1202 logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask)
1203 ctrl.message_send(request)
1204
1205 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001206 do_barrier(ctrl)
macauleyfddc4662015-07-27 17:40:30 +08001207
Pierbbdf3782016-08-22 17:58:26 -07001208 return request
macauley_cheng6b133662015-11-09 13:52:39 +08001209
1210#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 +08001211def 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 +08001212 match = ofp.match()
1213 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1214 match.oxm_list.append(ofp.oxm.ipv4_dst(ip_dst))
1215 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1216 match.oxm_list.append(ofp.oxm.tcp_dst(tcp_dst))
Pierbbdf3782016-08-22 17:58:26 -07001217
macauley_cheng6b133662015-11-09 13:52:39 +08001218 request = ofp.message.flow_add(
1219 table_id=28,
1220 cookie=42,
1221 match=match,
1222 instructions=[
1223 ofp.instruction.write_actions(
1224 actions=[ofp.action.set_field(ofp.oxm.ipv4_dst(set_ip_dst)),
1225 ofp.action.set_field(ofp.oxm.tcp_dst(set_tcp_dst)),
1226 ofp.action.group(action_group_id)]),
1227 ofp.instruction.goto_table(60)
1228 ],
1229 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001230 priority=1)
macauley_cheng6b133662015-11-09 13:52:39 +08001231 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)
1232 ctrl.message_send(request)
1233 return request
macauley_chengeffc20a2015-11-09 16:14:56 +08001234
1235#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
1236def add_snat_flow(ctrl, eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src):
1237 match = ofp.match()
1238 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1239 match.oxm_list.append(ofp.oxm.ipv4_src(ip_src))
1240 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1241 match.oxm_list.append(ofp.oxm.tcp_src(tcp_src))
Pierbbdf3782016-08-22 17:58:26 -07001242
macauley_chengeffc20a2015-11-09 16:14:56 +08001243 request = ofp.message.flow_add(
1244 table_id=29,
1245 cookie=42,
1246 match=match,
1247 instructions=[
1248 ofp.instruction.write_actions(
1249 actions=[ofp.action.set_field(ofp.oxm.ipv4_src(set_ip_src)),
1250 ofp.action.set_field(ofp.oxm.tcp_src(set_tcp_src))]),
1251 ofp.instruction.goto_table(30)
1252 ],
1253 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001254 priority=1)
macauley_chengeffc20a2015-11-09 16:14:56 +08001255 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)
1256 ctrl.message_send(request)
1257 return request
Pierbbdf3782016-08-22 17:58:26 -07001258
1259def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001260 """
1261 Command Example:
1262 of-agent vtap 10001 ethernet 1/1 vid 1
1263 of-agent vtp 10001 vni 10
1264 """
1265 if vlan != 0:
1266 config_vtap_xml="""
1267 <config>
1268 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1269 <id>capable-switch-1</id>
1270 <resources>
1271 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001272 <resource-id >LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001273 <features>
1274 <current>
1275 <rate>10Gb</rate>
1276 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001277 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001278 </current>
1279 <advertised>
1280 <rate>10Gb</rate>
1281 <rate>100Gb</rate>
1282 <medium>fiber</medium>
1283 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001284 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001285 <supported>
1286 <rate>10Gb</rate>
1287 <rate>100Gb</rate>
1288 <medium>fiber</medium>
1289 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001290 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001291 <advertised-peer>
1292 <rate>10Gb</rate>
1293 <rate>100Gb</rate>
1294 <medium>fiber</medium>
1295 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001296 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001297 </features>
1298 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1299 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1300 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1301 <ofdpa10:vni>VNID</ofdpa10:vni>
1302 </ofdpa10:vtap>
Pierbbdf3782016-08-22 17:58:26 -07001303 </port>
macauleyfddc4662015-07-27 17:40:30 +08001304 </resources>
1305 <logical-switches>
1306 <switch>
1307 <id>DATAPATH_ID</id>
1308 <datapath-id>DATAPATH_ID</datapath-id>
1309 <resources>
1310 <port xc:operation="OPERATION">LPORT</port>
1311 </resources>
1312 </switch>
1313 </logical-switches>
1314 </capable-switch>
1315 </config>
Pierbbdf3782016-08-22 17:58:26 -07001316 """
macauleyfddc4662015-07-27 17:40:30 +08001317 else:
1318 config_vtap_xml="""
1319 <config>
1320 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1321 <id>capable-switch-1</id>
1322 <resources>
1323 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001324 <resource-id >LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001325 <features>
1326 <current>
1327 <rate>10Gb</rate>
1328 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001329 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001330 </current>
1331 <advertised>
1332 <rate>10Gb</rate>
1333 <rate>100Gb</rate>
1334 <medium>fiber</medium>
1335 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001336 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001337 <supported>
1338 <rate>10Gb</rate>
1339 <rate>100Gb</rate>
1340 <medium>fiber</medium>
1341 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001342 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001343 <advertised-peer>
1344 <rate>10Gb</rate>
1345 <rate>100Gb</rate>
1346 <medium>fiber</medium>
1347 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001348 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001349 </features>
1350 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1351 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1352 <ofdpa10:vni>VNID</ofdpa10:vni>
1353 </ofdpa10:vtap>
Pierbbdf3782016-08-22 17:58:26 -07001354 </port>
macauleyfddc4662015-07-27 17:40:30 +08001355 </resources>
1356 <logical-switches>
1357 <switch>
1358 <id>DATAPATH_ID</id>
1359 <datapath-id>DATAPATH_ID</datapath-id>
1360 <resources>
1361 <port xc:operation="OPERATION">LPORT</port>
1362 </resources>
1363 </switch>
1364 </logical-switches>
1365 </capable-switch>
1366 </config>
Pierbbdf3782016-08-22 17:58:26 -07001367 """
1368 str_datapath_id_f= "{:016x}".format(dp_id)
1369 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1370 config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id)
1371 config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport)))
1372 config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port))
1373 config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan))
macauleyfddc4662015-07-27 17:40:30 +08001374 config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid))
1375 config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation))
1376 return config_vtap_xml
Pierbbdf3782016-08-22 17:58:26 -07001377
1378def 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 +08001379 """
1380 Command Example:
1381 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 -07001382 of-agent vtp 10001 vni 10
macauleyfddc4662015-07-27 17:40:30 +08001383 """
1384
1385 config_vtep_xml="""
1386 <config>
1387 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1388 <id>capable-switch-1</id>
1389 <resources>
1390 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001391 <resource-id>LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001392 <features>
1393 <current>
1394 <rate>10Gb</rate>
1395 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001396 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001397 </current>
1398 <advertised>
1399 <rate>10Gb</rate>
1400 <rate>100Gb</rate>
1401 <medium>fiber</medium>
1402 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001403 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001404 <supported>
1405 <rate>10Gb</rate>
1406 <rate>100Gb</rate>
1407 <medium>fiber</medium>
1408 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001409 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001410 <advertised-peer>
1411 <rate>10Gb</rate>
1412 <rate>100Gb</rate>
1413 <medium>fiber</medium>
1414 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001415 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001416 </features>
1417 <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01">
1418 <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip>
1419 <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip>
1420 <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port>
macauley25999cf2015-08-07 17:03:24 +08001421 <ofdpa10:vni xc:operation="OPERATION">
1422 <ofdpa10:id>VNID</ofdpa10:id>
1423 </ofdpa10:vni>
macauleyfddc4662015-07-27 17:40:30 +08001424 <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id>
1425 <ofdpa10:ttl>TTL</ofdpa10:ttl>
1426 </ofdpa10:vtep>
Pierbbdf3782016-08-22 17:58:26 -07001427 </port>
macauleyfddc4662015-07-27 17:40:30 +08001428 </resources>
1429 <logical-switches>
1430 <switch>
1431 <id>DATAPATH_ID</id>
1432 <datapath-id>DATAPATH_ID</datapath-id>
1433 <resources>
1434 <port xc:operation="OPERATION">LPORT</port>
1435 </resources>
1436 </switch>
1437 </logical-switches>
1438 </capable-switch>
Pierbbdf3782016-08-22 17:58:26 -07001439 </config>
macauleyfddc4662015-07-27 17:40:30 +08001440 """
Pierbbdf3782016-08-22 17:58:26 -07001441 str_datapath_id_f= "{:016x}".format(dp_id)
1442 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1443 config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +08001444 config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport)))
Pierbbdf3782016-08-22 17:58:26 -07001445 config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip))
1446 config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip))
1447 config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port))
1448 config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1449 config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl))
macauleyfddc4662015-07-27 17:40:30 +08001450 config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid))
Pierbbdf3782016-08-22 17:58:26 -07001451 config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation))
macauleyfddc4662015-07-27 17:40:30 +08001452
Pierbbdf3782016-08-22 17:58:26 -07001453 return config_vtep_xml
1454
1455def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001456 #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2
1457 config_nexthop_xml="""
1458 <config>
1459 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1460 <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1461 <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id>
1462 <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac>
1463 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1464 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1465 </ofdpa10:next-hop>
1466 </of11-config:capable-switch>
1467 </config>
1468 """
1469 config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan))
Pierbbdf3782016-08-22 17:58:26 -07001470 config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port))
1471 config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1472 config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac))
1473 config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation))
1474 return config_nexthop_xml
macauleyfddc4662015-07-27 17:40:30 +08001475
Pierbbdf3782016-08-22 17:58:26 -07001476def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001477 #of-agent vni 10 multicast 224.1.1.1 nexthop 20
Pierbbdf3782016-08-22 17:58:26 -07001478 if mcast_ipv4!=None:
macauleyfddc4662015-07-27 17:40:30 +08001479 config_vni_xml="""
1480 <config>
1481 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1482 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1483 <ofdpa10:id>VNID</ofdpa10:id>
1484 <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group>
1485 <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id>
1486 </ofdpa10:vni>
1487 </of11-config:capable-switch>
1488 </config>
Pierbbdf3782016-08-22 17:58:26 -07001489 """
1490 config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1491 config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4))
macauleyfddc4662015-07-27 17:40:30 +08001492 else:
1493 config_vni_xml="""
1494 <config>
1495 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1496 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1497 <ofdpa10:id>VNID</ofdpa10:id>
1498 </ofdpa10:vni>
1499 </of11-config:capable-switch>
1500 </config>
Pierbbdf3782016-08-22 17:58:26 -07001501 """
1502
1503 config_vni_xml=config_vni_xml.replace("VNID", str(vni_id))
1504 config_vni_xml=config_vni_xml.replace("OPERATION", str(operation))
macauleyfddc4662015-07-27 17:40:30 +08001505 return config_vni_xml
Pierbbdf3782016-08-22 17:58:26 -07001506
1507def get_featureReplay(self):
macauleyfddc4662015-07-27 17:40:30 +08001508 req = ofp.message.features_request()
1509 res, raw = self.controller.transact(req)
Pierbbdf3782016-08-22 17:58:26 -07001510 self.assertIsNotNone(res, "Did not receive a response from the DUT.")
macauleyfddc4662015-07-27 17:40:30 +08001511 self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY,
1512 ("Unexpected packet type %d received in response to "
1513 "OFPT_FEATURES_REQUEST") % res.type)
Pierbbdf3782016-08-22 17:58:26 -07001514 return res
1515
macauleyfddc4662015-07-27 17:40:30 +08001516def send_edit_config(switch_ip, xml, target='runing'):
1517 NETCONF_ACCOUNT="netconfuser"
1518 NETCONF_PASSWD="netconfuser"
1519 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1520 try:
Pierbbdf3782016-08-22 17:58:26 -07001521 m.edit_config(target='running',
1522 config=xml,
1523 default_operation='merge',
macauleyfddc4662015-07-27 17:40:30 +08001524 error_option='stop-on-error')
1525
1526 except Exception as e:
1527 logging.info("Fail to set xml %s", xml)
1528 return False
1529
1530 #return m.get_config(source='running').data_xml
1531 return True
1532
1533def send_delete_config(switch_ip, xml, target='runing'):
1534 NETCONF_ACCOUNT="netconfuser"
1535 NETCONF_PASSWD="netconfuser"
1536 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1537 try:
Pierbbdf3782016-08-22 17:58:26 -07001538 m.edit_config(target='running',
1539 config=xml,
1540 default_operation='delete',
macauleyfddc4662015-07-27 17:40:30 +08001541 error_option='stop-on-error')
1542
1543 except Exception as e:
1544 logging.info("Fail to set xml %s", xml)
1545 return False
1546
1547 #return m.get_config(source='running').data_xml
1548 return True
Pierbbdf3782016-08-22 17:58:26 -07001549
macauleyfddc4662015-07-27 17:40:30 +08001550def get_edit_config(switch_ip, target='runing'):
1551 NETCONF_ACCOUNT="netconfuser"
1552 NETCONF_PASSWD="netconfuser"
1553 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 +08001554 return m.get_config(source='running').data_xml
macauleydbff3272015-07-30 14:07:16 +08001555
macauley_cheng67da9262015-08-31 15:18:41 +08001556
1557"""
1558MPLS
1559"""
1560
1561OFDPA_MPLS_SUBTYPE_SHIFT=24
Pierbbdf3782016-08-22 17:58:26 -07001562OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1
macauley_cheng67da9262015-08-31 15:18:41 +08001563OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2
1564OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3
1565OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4
1566OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5
1567OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6
1568OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8
1569OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10
1570
Piercf76e802016-09-19 20:16:35 -07001571
1572
1573
macauley_cheng67da9262015-08-31 15:18:41 +08001574def encode_mpls_interface_group_id(subtype, index):
1575 index=index&0x00ffffff
1576 assert(subtype==0)
1577 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1578
1579def encode_mpls_label_group_id(subtype, index):
1580 index=index&0x00ffffff
1581 assert(subtype <=5 or subtype==0)
1582 #1: l2 vpn label
1583 #2: l3 vpn label
1584 #3: mpls tunnel label 1
1585 #4: mpls tunnel lable 2
1586 #5: mpls swap label
Pierbbdf3782016-08-22 17:58:26 -07001587 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
macauley_cheng67da9262015-08-31 15:18:41 +08001588
1589def encode_mpls_forwarding_group_id(subtype, index):
1590 index=index&0x00ffffff
1591 assert(subtype==6 or subtype==8 or subtype==10)
Pierbbdf3782016-08-22 17:58:26 -07001592 return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
macauley_cheng67da9262015-08-31 15:18:41 +08001593
1594
1595def add_mpls_intf_group(ctrl, ref_gid, dst_mac, src_mac, vid, index, subtype=0):
1596 action=[]
1597 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
1598 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
Piercf76e802016-09-19 20:16:35 -07001599 if vid != 1:
1600 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001601 action.append(ofp.action.group(ref_gid))
Pierbbdf3782016-08-22 17:58:26 -07001602
macauley_cheng67da9262015-08-31 15:18:41 +08001603 buckets = [ofp.bucket(actions=action)]
1604
1605 mpls_group_id =encode_mpls_interface_group_id(subtype, index)
1606 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1607 group_id=mpls_group_id,
1608 buckets=buckets
1609 )
1610 ctrl.message_send(request)
1611 return mpls_group_id, request
1612
Piercf76e802016-09-19 20:16:35 -07001613def add_mpls_tunnel_label_group(
1614 ctrl,
1615 ref_gid,
1616 subtype,
1617 index,
1618 label,
1619 ):
1620
1621 action=[]
1622 action.append(ofp.action.push_mpls(0x8847))
1623 action.append(ofp.action.set_field(ofp.oxm.mpls_label(label)))
1624 action.append(ofp.action.group(ref_gid))
1625 buckets = [ofp.bucket(actions=action)]
1626
1627 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1628 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1629 group_id=mpls_group_id,
1630 buckets=buckets
1631 )
1632 ctrl.message_send(request)
1633
1634 return mpls_group_id, request
1635
Pierb5da4c92016-09-21 11:23:35 -07001636def add_mpls_swap_label_group(
1637 ctrl,
1638 ref_gid,
1639 subtype,
1640 index,
1641 label,
1642 ):
1643
1644 action=[]
1645 action.append(ofp.action.set_field(ofp.oxm.mpls_label(label)))
1646 action.append(ofp.action.group(ref_gid))
1647 buckets = [ofp.bucket(actions=action)]
1648
1649 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1650 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1651 group_id=mpls_group_id,
1652 buckets=buckets
1653 )
1654 ctrl.message_send(request)
1655
1656 return mpls_group_id, request
1657
Pierbbdf3782016-08-22 17:58:26 -07001658def add_mpls_label_group(ctrl, subtype, index, ref_gid,
macauley_cheng67da9262015-08-31 15:18:41 +08001659 lmep_id=-1,
1660 qos_index=-1,
1661 push_l2_header=False,
1662 push_vlan=False,
1663 push_mpls_header=False,
1664 push_cw=False,
1665 set_mpls_label=None,
1666 set_bos=None,
1667 set_tc=None,
1668 set_tc_from_table=False,
1669 cpy_tc_outward=False,
1670 set_ttl=None,
1671 cpy_ttl_outward=False,
1672 oam_lm_tx_count=False,
1673 set_pri_from_table=False
1674 ):
1675 """
1676 @ref_gid: only can be mpls intf group or mpls tunnel label 1/2 group
Pierbbdf3782016-08-22 17:58:26 -07001677 """
macauley_cheng67da9262015-08-31 15:18:41 +08001678 action=[]
1679
1680 if push_vlan== True:
1681 action.append(ofp.action.push_vlan(0x8100))
1682 if push_mpls_header== True:
1683 action.append(ofp.action.push_mpls(0x8847))
1684 if set_mpls_label != None:
1685 action.append(ofp.action.set_field(ofp.oxm.mpls_label(set_mpls_label)))
1686 if set_bos != None:
1687 action.append(ofp.action.set_field(ofp.oxm.mpls_bos(set_bos)))
1688 if set_tc != None:
1689 assert(set_tc_from_table==False)
1690 action.append(ofp.action.set_field(ofp.oxm.mpls_tc(set_tc)))
1691 if set_ttl != None:
Pierbbdf3782016-08-22 17:58:26 -07001692 action.append(ofp.action.set_mpls_ttl(set_ttl))
macauley_cheng67da9262015-08-31 15:18:41 +08001693 if cpy_ttl_outward == True:
Pierbbdf3782016-08-22 17:58:26 -07001694 action.append(ofp.action.copy_ttl_out())
macauley_cheng67da9262015-08-31 15:18:41 +08001695 """
1696 ofdpa experimenter
Pierbbdf3782016-08-22 17:58:26 -07001697 """
macauley_cheng67da9262015-08-31 15:18:41 +08001698 if push_l2_header== True:
Pierbbdf3782016-08-22 17:58:26 -07001699 action.append(ofp.action.ofdpa_push_l2_header())
macauley_cheng67da9262015-08-31 15:18:41 +08001700 if set_tc_from_table== True:
1701 assert(qos_index>=0)
1702 assert(set_tc == None)
Pierbbdf3782016-08-22 17:58:26 -07001703 action.append(ofp.action.ofdpa_set_tc_from_table(qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08001704 if cpy_tc_outward == True:
Pierbbdf3782016-08-22 17:58:26 -07001705 action.append(ofp.action.ofdpa_copy_tc_out())
macauley_cheng67da9262015-08-31 15:18:41 +08001706 if oam_lm_tx_count == True:
Pierbbdf3782016-08-22 17:58:26 -07001707 assert(qos_index>=0 and lmep_id>=0)
1708 action.append(ofp.action.ofdpa_oam_lm_tx_count(lmep_id, qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08001709 if set_pri_from_table == True:
Pierbbdf3782016-08-22 17:58:26 -07001710 assert(qos_index>=0)
1711 action.append(ofp.action.ofdpa_set_qos_from_table(qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08001712 if push_cw == True:
1713 action.append(ofp.action.ofdpa_push_cw())
Pierbbdf3782016-08-22 17:58:26 -07001714
1715 action.append(ofp.action.group(ref_gid))
macauley_cheng67da9262015-08-31 15:18:41 +08001716 buckets = [ofp.bucket(actions=action)]
Pierbbdf3782016-08-22 17:58:26 -07001717
macauley_cheng67da9262015-08-31 15:18:41 +08001718 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1719 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1720 group_id=mpls_group_id,
1721 buckets=buckets
1722 )
1723 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -07001724
1725 return mpls_group_id, request
1726
Piercf76e802016-09-19 20:16:35 -07001727def add_mpls_l2_port_flow(ctrl, of_port, mpls_l2_port, tunnel_index, ref_gid, qos_index=0):
1728 """
1729 Only action is Group, which must indicate one of:
1730 MPLS L2 VPN Label or Fast Failover Protection Group.
1731 ref_gid contains this information
1732 """
1733 tunnel_id = tunnel_index + ofp.oxm.TUNNEL_ID_BASE
1734
1735 match = ofp.match()
1736 match.oxm_list.append(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port))
1737 match.oxm_list.append(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE))
1738
1739 action = []
1740 action.append(ofp.action.group(ref_gid))
1741 assert(qos_index>=0)
1742 action.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_QOS_INDEX, value=qos_index)))
1743
1744 request = ofp.message.flow_add(
1745 table_id=MPLS_L2_PORT_FLOW_TABLE,
1746 cookie=42,
1747 match=match,
1748 instructions=[
1749 ofp.instruction.write_actions(
1750 actions=action
1751 ),
1752 ofp.instruction.goto_table(MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE) ],
1753 buffer_id=ofp.OFP_NO_BUFFER,
1754 priority=1)
1755 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)
1756 ctrl.message_send(request)
1757 return request
1758
1759 return
1760
Pierbbdf3782016-08-22 17:58:26 -07001761def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids,
1762 watch_port=None,
1763 watch_group=ofp.OFPP_ANY,
macauley_cheng67da9262015-08-31 15:18:41 +08001764 push_vlan=None,
macauley_chengd17ce512015-08-31 17:45:51 +08001765 pop_vlan=None,
macauley_cheng67da9262015-08-31 15:18:41 +08001766 set_vid=None):
1767 assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP
1768 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP
1769 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG)
macauley_chengd17ce512015-08-31 17:45:51 +08001770
macauley_cheng67da9262015-08-31 15:18:41 +08001771 buckets=[]
1772 if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP:
macauley_chengd17ce512015-08-31 17:45:51 +08001773 group_type = ofp.OFPGT_FF
macauley_cheng67da9262015-08-31 15:18:41 +08001774 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001775 action=[]
Pierbbdf3782016-08-22 17:58:26 -07001776 action.append(ofp.action.group(gid))
macauley_chengd17ce512015-08-31 17:45:51 +08001777 buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action))
1778
macauley_cheng67da9262015-08-31 15:18:41 +08001779 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP:
1780 group_type = ofp.OFPGT_SELECT
1781 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001782 action=[]
Pierbbdf3782016-08-22 17:58:26 -07001783 action.append(ofp.action.group(gid))
macauley_cheng67da9262015-08-31 15:18:41 +08001784 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001785
macauley_cheng67da9262015-08-31 15:18:41 +08001786 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG:
1787 group_type = ofp.OFPGT_INDIRECT
macauley_chengd17ce512015-08-31 17:45:51 +08001788 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08001789 if set_vid!=None:
Flavio Castro91d1a552016-05-17 16:59:44 -07001790 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+set_vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001791 if push_vlan!=None:
Pierbbdf3782016-08-22 17:58:26 -07001792 action.append(ofp.action.push_vlan(push_vlan))
macauley_cheng67da9262015-08-31 15:18:41 +08001793 if pop_vlan!=None:
Pierbbdf3782016-08-22 17:58:26 -07001794 action.append(ofp.action.pop_vlan())
1795 action.append(ofp.action.group(ref_gids[0]))
macauley_cheng67da9262015-08-31 15:18:41 +08001796 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001797
1798 mpls_group_id = encode_mpls_forwarding_group_id(subtype, index)
macauley_cheng67da9262015-08-31 15:18:41 +08001799 request = ofp.message.group_add(group_type=group_type,
macauley_cheng67da9262015-08-31 15:18:41 +08001800 group_id=mpls_group_id,
1801 buckets=buckets
1802 )
1803 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -07001804 return mpls_group_id, request
macauley_chengd17ce512015-08-31 17:45:51 +08001805
1806
macauley_cheng67da9262015-08-31 15:18:41 +08001807"""
Piercf76e802016-09-19 20:16:35 -07001808display
Pierbbdf3782016-08-22 17:58:26 -07001809"""
macauleydbff3272015-07-30 14:07:16 +08001810def print_current_table_flow_stat(ctrl, table_id=0xff):
1811 stat_req=ofp.message.flow_stats_request()
1812 response, pkt = ctrl.transact(stat_req)
1813 if response == None:
1814 print "no response"
1815 return None
1816 print len(response.entries)
1817 for obj in response.entries:
1818 print "match ", obj.match
1819 print "cookie", obj.cookie
1820 print "priority", obj.priority
1821 print "idle_timeout", obj.idle_timeout
1822 print "hard_timeout", obj.hard_timeout
1823 #obj.actions
Flavio Castro167f5bd2015-12-02 19:33:53 -05001824 print "packet count: %lx"%obj.packet_count