blob: 176ef287451bbc7ad9aa09757f33b95510c4e9b5 [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:
Pier23784aa2016-09-19 20:08:21 -0700696 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
Pierbbdf3782016-08-22 17:58:26 -0700697
Flavio Castro6d498522015-12-15 14:05:04 -0500698 #actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id)))
macauley7f89d962015-08-06 18:13:48 +0800699
macauley0f91a3e2015-07-17 18:09:59 +0800700 request = ofp.message.flow_add(
701 table_id=10,
702 cookie=42,
703 match=match,
704 instructions=[
macauley53d90fe2015-08-04 17:34:22 +0800705 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800706 actions=actions
macauley53d90fe2015-08-04 17:34:22 +0800707 ),
708 ofp.instruction.goto_table(20)
macauley0f91a3e2015-07-17 18:09:59 +0800709 ],
710 priority=0)
711 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
712 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700713
macauley0f91a3e2015-07-17 18:09:59 +0800714 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
715 match = ofp.match()
716 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500717 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
Pierbbdf3782016-08-22 17:58:26 -0700718
macauley7f89d962015-08-06 18:13:48 +0800719 actions=[]
720 if vrf!=0:
721 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
Pierbbdf3782016-08-22 17:58:26 -0700722
Flavio Castro91d1a552016-05-17 16:59:44 -0700723 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Pierbbdf3782016-08-22 17:58:26 -0700724
macauley0f91a3e2015-07-17 18:09:59 +0800725 request = ofp.message.flow_add(
726 table_id=10,
727 cookie=42,
728 match=match,
729 instructions=[
730 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800731 actions=actions
macauley0f91a3e2015-07-17 18:09:59 +0800732 ),
733 ofp.instruction.goto_table(20)
734 ],
735 priority=0)
736 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
737 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -0700738
Flavio Castro932014b2016-01-05 18:29:15 -0500739 if (flag == 4) :
740 match = ofp.match()
741 match.oxm_list.append(ofp.oxm.in_port(of_port))
742 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000,0x1fff))
743
744 actions=[]
745 if vrf!=0:
746 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
747
Flavio Castro91d1a552016-05-17 16:59:44 -0700748 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Flavio Castro932014b2016-01-05 18:29:15 -0500749
750 request = ofp.message.flow_add(
751 table_id=10,
752 cookie=42,
753 match=match,
754 instructions=[
755 ofp.instruction.apply_actions(
756 actions=actions
757 ),
758 ofp.instruction.goto_table(20)
759 ],
760 priority=0)
761 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
762 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800763
Piere1308762016-09-12 15:29:56 -0700764 if (flag == VLAN_TABLE_FLAG_ONLY_STACKED):
765 # This flag is meant to managed stacked vlan packtes
766 # Matches on outer VLAN_ID, set OVID with outer VLAN.
767 # Finally expose inner VLAN_ID with a pop action and
768 # goto VLAN_1_FLOW_TABLE
769 match = ofp.match()
770 match.oxm_list.append(ofp.oxm.in_port(of_port))
771 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
772
773 actions=[]
774 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_OVID, value=0x1000+vlan_id)))
775 actions.append(ofp.action.pop_vlan())
776
777 request = ofp.message.flow_add(
778 table_id=10,
779 cookie=42,
780 match=match,
781 instructions=[
782 ofp.instruction.apply_actions(
783 actions=actions
784 ),
785 ofp.instruction.goto_table(VLAN_1_FLOW_TABLE)
786 ],
787 priority=0)
788 logging.info("Add vlan %d tagged packets on port %d and go to table %d" %( vlan_id, of_port, VLAN_1_FLOW_TABLE))
789 ctrl.message_send(request)
790
791 if (flag == VLAN_TABLE_FLAG_PRIORITY) :
792 match = ofp.match()
793 match.oxm_list.append(ofp.oxm.in_port(of_port))
794 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1fff))
795 request = ofp.message.flow_add(
796 table_id=10,
797 cookie=42,
798 match=match,
799 instructions=[
800 ofp.instruction.apply_actions(
801 actions=[
802 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)),
803 ofp.action.push_vlan(0x8100),
804 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+out_vlan_id)),
805 ]
806 ),
807 ofp.instruction.goto_table(20)
808 ],
809 priority=0)
810 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
811 ctrl.message_send(request)
812
813 if send_barrier:
814 do_barrier(ctrl)
815
816 return request
817
Piercf76e802016-09-19 20:16:35 -0700818def 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):
819 # table 10: vlan
820 # goto to table 13
821 if flag == VLAN_TABLE_FLAG_ONLY_TAG:
822 match = ofp.match()
823 match.oxm_list.append(ofp.oxm.in_port(of_port))
824 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id, 0x1fff))
825
826 actions=[]
827 if vlan_id == -1:
828 actions.append(ofp.action.pop_vlan())
829 if new_vlan_id > 1:
830 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_vlan_id)))
831 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
832 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
833 # 0x0000nnnn is for UNI interfaces
834 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
835
836 request = ofp.message.flow_add(
837 table_id=10,
838 cookie=42,
839 match=match,
840 instructions=[
841 ofp.instruction.apply_actions(
842 actions=actions
843 ),
844 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
845 ],
846 priority=0)
847 logging.info("Add vlan %d tagged packets on port %d and go to table %d" % (vlan_id, of_port, MPLS_L2_PORT_FLOW_TABLE))
848 ctrl.message_send(request)
849
850 if flag == VLAN_TABLE_FLAG_ONLY_UNTAG:
851 match = ofp.match()
852 match.oxm_list.append(ofp.oxm.in_port(of_port))
853 match.oxm_list.append(ofp.oxm.vlan_vid(0))
854
855 actions=[]
856 if vlan_id > 1:
857 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
858 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
859 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
860 # 0x0000nnnn is for UNI interfaces
861 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
862
863 request = ofp.message.flow_add(
864 table_id=10,
865 cookie=42,
866 match=match,
867 instructions=[
868 ofp.instruction.apply_actions(
869 actions=actions
870 ),
871 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
872 ],
873 priority=0)
874 logging.info("Add vlan %d untagged packets on port %d and go to table %d" % (vlan_id, of_port, MPLS_L2_PORT_FLOW_TABLE))
875 ctrl.message_send(request)
876
877 if send_barrier:
878 do_barrier(ctrl)
879
880 return request
881
Piere1308762016-09-12 15:29:56 -0700882def 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):
883 # table 11: vlan 1 table
884 # goto to table 20
885 if flag == VLAN_TABLE_FLAG_ONLY_TAG:
886 match = ofp.match()
887 match.oxm_list.append(ofp.oxm.in_port(of_port))
888 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
889 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
890
891 actions=[]
892 actions.append(ofp.action.push_vlan(0x8100))
893 if new_outer_vlan_id != -1:
894 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_outer_vlan_id)))
895 else:
896 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan_id)))
897
898 request = ofp.message.flow_add(
899 table_id=11,
900 cookie=42,
901 match=match,
902 instructions=[
903 ofp.instruction.apply_actions(
904 actions=actions
905 ),
906 ofp.instruction.goto_table(TERMINATION_FLOW_TABLE)
907 ],
908 priority=0)
909 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))
910 ctrl.message_send(request)
911
912 if flag == VLAN_TABLE_FLAG_ONLY_UNTAG:
913 match = ofp.match()
914 match.oxm_list.append(ofp.oxm.in_port(of_port))
915 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
916 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
917
918 actions=[]
919 request = ofp.message.flow_add(
920 table_id=11,
921 cookie=42,
922 match=match,
923 instructions=[
924 ofp.instruction.apply_actions(
925 actions=actions
926 ),
927 ofp.instruction.goto_table(TERMINATION_FLOW_TABLE)
928 ],
929 priority=0)
930 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))
931 ctrl.message_send(request)
932
macauley0f91a3e2015-07-17 18:09:59 +0800933 if send_barrier:
934 do_barrier(ctrl)
935
936 return request
Pierbbdf3782016-08-22 17:58:26 -0700937
Piercf76e802016-09-19 20:16:35 -0700938def 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):
939 # table 11: vlan 1 table
940 # goto to table 13
941 match = ofp.match()
942 match.oxm_list.append(ofp.oxm.in_port(of_port))
943 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+inner_vlan_id,0x1fff))
944 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_OVID, 0x1000+outer_vlan_id))
945
946 actions=[]
947 actions.append(ofp.action.push_vlan(0x8100))
948 if new_outer_vlan_id != -1:
949 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+new_outer_vlan_id)))
950 else:
951 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+outer_vlan_id)))
952
953 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_TYPE, value=ofp.oxm.VPWS)))
954 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
955 # 0x0000nnnn is for UNI interfaces
956 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port)))
957
958 request = ofp.message.flow_add(
959 table_id=11,
960 cookie=42,
961 match=match,
962 instructions=[
963 ofp.instruction.apply_actions(
964 actions=actions
965 ),
966 ofp.instruction.goto_table(MPLS_L2_PORT_FLOW_TABLE)
967 ],
968 priority=0)
969 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))
970 ctrl.message_send(request)
971
972 if send_barrier:
973 do_barrier(ctrl)
974
975 return request
976
macauley97557232015-07-16 17:28:07 +0800977def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False):
978 match = ofp.match()
castroflavio21894482015-12-08 15:29:55 -0500979 priority=500
macauleyfddc4662015-07-27 17:40:30 +0800980 if dst_mac!=None:
castroflavio21894482015-12-08 15:29:55 -0500981 priority=1000
macauleyfddc4662015-07-27 17:40:30 +0800982 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
983
macauley97557232015-07-16 17:28:07 +0800984 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauleyfddc4662015-07-27 17:40:30 +0800985
macauley97557232015-07-16 17:28:07 +0800986 request = ofp.message.flow_add(
987 table_id=50,
988 cookie=42,
989 match=match,
990 instructions=[
991 ofp.instruction.write_actions(
992 actions=[
993 ofp.action.group(group_id)]),
994 ofp.instruction.goto_table(60)
995 ],
996 buffer_id=ofp.OFP_NO_BUFFER,
castroflavio21894482015-12-08 15:29:55 -0500997 priority=priority)
macauley97557232015-07-16 17:28:07 +0800998
999 logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac)
1000 ctrl.message_send(request)
1001
1002 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001003 do_barrier(ctrl)
macauley15909e72015-07-17 15:58:57 +08001004
Pierbbdf3782016-08-22 17:58:26 -07001005 return request
macauleyfddc4662015-07-27 17:40:30 +08001006
1007def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False):
1008 match = ofp.match()
1009 if dst_mac!=None:
1010 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
1011
1012 match.oxm_list.append(ofp.oxm.tunnel_id(vnid))
1013 if is_group == True:
1014 actions=[ofp.action.group(group_id)]
1015 else:
1016 actions=[ofp.action.output(group_id)]
1017
1018 request = ofp.message.flow_add(
1019 table_id=50,
1020 cookie=42,
1021 match=match,
1022 instructions=[
1023 ofp.instruction.write_actions(
1024 actions=actions),
1025 ofp.instruction.goto_table(60)
1026 ],
1027 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001028 priority=1000)
macauleyfddc4662015-07-27 17:40:30 +08001029
1030 logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac)
1031 ctrl.message_send(request)
1032
1033 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001034 do_barrier(ctrl)
macauleyfddc4662015-07-27 17:40:30 +08001035
Pierbbdf3782016-08-22 17:58:26 -07001036 return request
1037
macauley_cheng6b133662015-11-09 13:52:39 +08001038def add_termination_flow(ctrl, in_port, eth_type, dst_mac, vlanid, goto_table=None, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +08001039 match = ofp.match()
macauley0f91a3e2015-07-17 18:09:59 +08001040 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauleyfddc4662015-07-27 17:40:30 +08001041 if dst_mac[0]&0x01 == 0x01:
1042 match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00]))
1043 goto_table=40
1044 else:
macauley53d90fe2015-08-04 17:34:22 +08001045 if in_port!=0:
1046 match.oxm_list.append(ofp.oxm.in_port(in_port))
macauleyfddc4662015-07-27 17:40:30 +08001047 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
1048 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauley_cheng6b133662015-11-09 13:52:39 +08001049 if goto_table == None:
1050 goto_table=30
macauley0f91a3e2015-07-17 18:09:59 +08001051
1052 request = ofp.message.flow_add(
1053 table_id=20,
1054 cookie=42,
1055 match=match,
1056 instructions=[
1057 ofp.instruction.goto_table(goto_table)
1058 ],
1059 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001060 priority=1)
macauley0f91a3e2015-07-17 18:09:59 +08001061
1062 logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac)
1063 ctrl.message_send(request)
1064
1065 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001066 do_barrier(ctrl)
macauley0f91a3e2015-07-17 18:09:59 +08001067
Pierbbdf3782016-08-22 17:58:26 -07001068 return request
1069
Flavio Castroaf2b4502016-02-02 17:41:32 -05001070def 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 +08001071 match = ofp.match()
1072 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauley53d90fe2015-08-04 17:34:22 +08001073 if vrf != 0:
1074 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
macauley0f91a3e2015-07-17 18:09:59 +08001075
Flavio Castroaf2b4502016-02-02 17:41:32 -05001076 match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask))
1077
1078 instructions = []
1079 instructions.append(ofp.instruction.goto_table(60))
1080 if send_ctrl:
Piere0918ec2016-09-09 20:06:05 -07001081 instructions.append(ofp.instruction.apply_actions(
Flavio Castroaf2b4502016-02-02 17:41:32 -05001082 actions=[ofp.action.output( port=ofp.OFPP_CONTROLLER,
1083 max_len=ofp.OFPCML_NO_BUFFER)]))
Pierbbdf3782016-08-22 17:58:26 -07001084 else:
Flavio Castroaf2b4502016-02-02 17:41:32 -05001085 instructions.append(ofp.instruction.write_actions(
1086 actions=[ofp.action.group(action_group_id)]))
macauley53d90fe2015-08-04 17:34:22 +08001087
macauley0f91a3e2015-07-17 18:09:59 +08001088 request = ofp.message.flow_add(
1089 table_id=30,
1090 cookie=42,
1091 match=match,
Flavio Castroaf2b4502016-02-02 17:41:32 -05001092 instructions=instructions,
macauley0f91a3e2015-07-17 18:09:59 +08001093 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001094 priority=1)
macauley0f91a3e2015-07-17 18:09:59 +08001095
1096 logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip)
1097 ctrl.message_send(request)
1098
1099 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001100 do_barrier(ctrl)
macauley0f91a3e2015-07-17 18:09:59 +08001101
Flavio Castro9debaaa2016-07-26 19:37:50 -07001102 return request
Flavio Castrod8f8af22015-12-02 18:19:26 -05001103
Flavio Castro8ca52542016-04-11 11:24:49 -04001104def 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 -04001105 match = ofp.match()
1106 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1107 match.oxm_list.append(ofp.oxm.mpls_label(label))
1108 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
Pierb5da4c92016-09-21 11:23:35 -07001109 actions = []
Flavio Castrob702a2f2016-04-10 22:01:48 -04001110 actions = [ofp.action.dec_mpls_ttl(),
Flavio Castro8ca52542016-04-11 11:24:49 -04001111 ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))]
1112 if (goto_table == 29):
Flavio Castro8ca52542016-04-11 11:24:49 -04001113 actions.append(ofp.action.group(action_group_id))
1114 else:
Flavio Castrod80fbc32016-07-25 15:54:26 -07001115 actions.append(ofp.action.set_field(
1116 ofp.oxm.exp2ByteValue(exp_type=23, value=32)))
1117 actions.append(ofp.action.group(action_group_id))
Flavio Castro8ca52542016-04-11 11:24:49 -04001118 actions.append(ofp.action.copy_ttl_in())
Flavio Castro9debaaa2016-07-26 19:37:50 -07001119
Flavio Castrob702a2f2016-04-10 22:01:48 -04001120 request = ofp.message.flow_add(
1121 table_id=24,
1122 cookie=43,
1123 match=match,
1124 instructions=[
Flavio Castro8ca52542016-04-11 11:24:49 -04001125 ofp.instruction.apply_actions(actions=actions),
1126 ofp.instruction.goto_table(goto_table)
Flavio Castrob702a2f2016-04-10 22:01:48 -04001127 ],
1128 buffer_id=ofp.OFP_NO_BUFFER,
1129 priority=1)
Flavio Castrob702a2f2016-04-10 22:01:48 -04001130 logging.info("Inserting MPLS flow , label %ld", label)
1131 ctrl.message_send(request)
1132
1133 if send_barrier:
1134 do_barrier(ctrl)
1135
1136 return request
1137
Pierf6f28162016-09-22 16:30:52 -07001138def 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 -07001139 match = ofp.match()
1140 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
1141 match.oxm_list.append(ofp.oxm.mpls_label(label))
1142 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
1143
1144 actions = []
1145 actions.append(ofp.action.dec_mpls_ttl())
Pierf6f28162016-09-22 16:30:52 -07001146 if popMPLS == True:
Pierb5da4c92016-09-21 11:23:35 -07001147 actions.append(ofp.action.copy_ttl_in())
1148 actions.append(ofp.action.pop_mpls(ethertype))
Pierf6f28162016-09-22 16:30:52 -07001149 if bos==1 and popL2 == True:
1150 actions.append(ofp.action.ofdpa_pop_l2_header())
1151 actions.append(ofp.action.ofdpa_pop_cw())
1152 actions.append(ofp.action.set_field(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE)))
1153 # 0x0002nnnn is for UNI interfaces
1154 actions.append(ofp.action.set_field(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00020000 + of_port)))
1155 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 -07001156 actions.append(ofp.action.group(action_group_id))
1157
1158 request = ofp.message.flow_add(
1159 table_id=24,
1160 cookie=43,
1161 match=match,
1162 instructions=[
1163 ofp.instruction.apply_actions(actions=actions),
1164 ofp.instruction.goto_table(goto_table)
1165 ],
1166 buffer_id=ofp.OFP_NO_BUFFER,
1167 priority=1
1168 )
1169 logging.info("Inserting MPLS flow , label %ld", label)
1170 ctrl.message_send(request)
1171
1172 if send_barrier:
1173 do_barrier(ctrl)
1174
1175 return request
1176
macauleyfddc4662015-07-27 17:40:30 +08001177def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False):
1178 match = ofp.match()
1179 match.oxm_list.append(ofp.oxm.eth_type(0x0800))
Pierbbdf3782016-08-22 17:58:26 -07001180 match.oxm_list.append(ofp.oxm.vlan_vid(vlan_id))
macauleyfddc4662015-07-27 17:40:30 +08001181 if src_ip_mask!=0:
1182 match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask))
1183 else:
1184 match.oxm_list.append(ofp.oxm.ipv4_src(src_ip))
Pierbbdf3782016-08-22 17:58:26 -07001185
macauleyfddc4662015-07-27 17:40:30 +08001186 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
Pierbbdf3782016-08-22 17:58:26 -07001187
macauleyfddc4662015-07-27 17:40:30 +08001188 request = ofp.message.flow_add(
1189 table_id=40,
1190 cookie=42,
1191 match=match,
1192 instructions=[
1193 ofp.instruction.write_actions(
1194 actions=[ofp.action.group(action_group_id)]),
1195 ofp.instruction.goto_table(60)
1196 ],
1197 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001198 priority=1)
macauleyfddc4662015-07-27 17:40:30 +08001199
1200 logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask)
1201 ctrl.message_send(request)
1202
1203 if send_barrier:
Pierbbdf3782016-08-22 17:58:26 -07001204 do_barrier(ctrl)
macauleyfddc4662015-07-27 17:40:30 +08001205
Pierbbdf3782016-08-22 17:58:26 -07001206 return request
macauley_cheng6b133662015-11-09 13:52:39 +08001207
1208#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 +08001209def 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 +08001210 match = ofp.match()
1211 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1212 match.oxm_list.append(ofp.oxm.ipv4_dst(ip_dst))
1213 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1214 match.oxm_list.append(ofp.oxm.tcp_dst(tcp_dst))
Pierbbdf3782016-08-22 17:58:26 -07001215
macauley_cheng6b133662015-11-09 13:52:39 +08001216 request = ofp.message.flow_add(
1217 table_id=28,
1218 cookie=42,
1219 match=match,
1220 instructions=[
1221 ofp.instruction.write_actions(
1222 actions=[ofp.action.set_field(ofp.oxm.ipv4_dst(set_ip_dst)),
1223 ofp.action.set_field(ofp.oxm.tcp_dst(set_tcp_dst)),
1224 ofp.action.group(action_group_id)]),
1225 ofp.instruction.goto_table(60)
1226 ],
1227 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001228 priority=1)
macauley_cheng6b133662015-11-09 13:52:39 +08001229 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)
1230 ctrl.message_send(request)
1231 return request
macauley_chengeffc20a2015-11-09 16:14:56 +08001232
1233#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
1234def add_snat_flow(ctrl, eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src):
1235 match = ofp.match()
1236 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
1237 match.oxm_list.append(ofp.oxm.ipv4_src(ip_src))
1238 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
1239 match.oxm_list.append(ofp.oxm.tcp_src(tcp_src))
Pierbbdf3782016-08-22 17:58:26 -07001240
macauley_chengeffc20a2015-11-09 16:14:56 +08001241 request = ofp.message.flow_add(
1242 table_id=29,
1243 cookie=42,
1244 match=match,
1245 instructions=[
1246 ofp.instruction.write_actions(
1247 actions=[ofp.action.set_field(ofp.oxm.ipv4_src(set_ip_src)),
1248 ofp.action.set_field(ofp.oxm.tcp_src(set_tcp_src))]),
1249 ofp.instruction.goto_table(30)
1250 ],
1251 buffer_id=ofp.OFP_NO_BUFFER,
Pierbbdf3782016-08-22 17:58:26 -07001252 priority=1)
macauley_chengeffc20a2015-11-09 16:14:56 +08001253 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)
1254 ctrl.message_send(request)
1255 return request
Pierbbdf3782016-08-22 17:58:26 -07001256
1257def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001258 """
1259 Command Example:
1260 of-agent vtap 10001 ethernet 1/1 vid 1
1261 of-agent vtp 10001 vni 10
1262 """
1263 if vlan != 0:
1264 config_vtap_xml="""
1265 <config>
1266 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1267 <id>capable-switch-1</id>
1268 <resources>
1269 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001270 <resource-id >LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001271 <features>
1272 <current>
1273 <rate>10Gb</rate>
1274 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001275 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001276 </current>
1277 <advertised>
1278 <rate>10Gb</rate>
1279 <rate>100Gb</rate>
1280 <medium>fiber</medium>
1281 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001282 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001283 <supported>
1284 <rate>10Gb</rate>
1285 <rate>100Gb</rate>
1286 <medium>fiber</medium>
1287 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001288 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001289 <advertised-peer>
1290 <rate>10Gb</rate>
1291 <rate>100Gb</rate>
1292 <medium>fiber</medium>
1293 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001294 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001295 </features>
1296 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1297 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1298 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1299 <ofdpa10:vni>VNID</ofdpa10:vni>
1300 </ofdpa10:vtap>
Pierbbdf3782016-08-22 17:58:26 -07001301 </port>
macauleyfddc4662015-07-27 17:40:30 +08001302 </resources>
1303 <logical-switches>
1304 <switch>
1305 <id>DATAPATH_ID</id>
1306 <datapath-id>DATAPATH_ID</datapath-id>
1307 <resources>
1308 <port xc:operation="OPERATION">LPORT</port>
1309 </resources>
1310 </switch>
1311 </logical-switches>
1312 </capable-switch>
1313 </config>
Pierbbdf3782016-08-22 17:58:26 -07001314 """
macauleyfddc4662015-07-27 17:40:30 +08001315 else:
1316 config_vtap_xml="""
1317 <config>
1318 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1319 <id>capable-switch-1</id>
1320 <resources>
1321 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001322 <resource-id >LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001323 <features>
1324 <current>
1325 <rate>10Gb</rate>
1326 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001327 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001328 </current>
1329 <advertised>
1330 <rate>10Gb</rate>
1331 <rate>100Gb</rate>
1332 <medium>fiber</medium>
1333 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001334 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001335 <supported>
1336 <rate>10Gb</rate>
1337 <rate>100Gb</rate>
1338 <medium>fiber</medium>
1339 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001340 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001341 <advertised-peer>
1342 <rate>10Gb</rate>
1343 <rate>100Gb</rate>
1344 <medium>fiber</medium>
1345 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001346 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001347 </features>
1348 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1349 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1350 <ofdpa10:vni>VNID</ofdpa10:vni>
1351 </ofdpa10:vtap>
Pierbbdf3782016-08-22 17:58:26 -07001352 </port>
macauleyfddc4662015-07-27 17:40:30 +08001353 </resources>
1354 <logical-switches>
1355 <switch>
1356 <id>DATAPATH_ID</id>
1357 <datapath-id>DATAPATH_ID</datapath-id>
1358 <resources>
1359 <port xc:operation="OPERATION">LPORT</port>
1360 </resources>
1361 </switch>
1362 </logical-switches>
1363 </capable-switch>
1364 </config>
Pierbbdf3782016-08-22 17:58:26 -07001365 """
1366 str_datapath_id_f= "{:016x}".format(dp_id)
1367 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1368 config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id)
1369 config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport)))
1370 config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port))
1371 config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan))
macauleyfddc4662015-07-27 17:40:30 +08001372 config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid))
1373 config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation))
1374 return config_vtap_xml
Pierbbdf3782016-08-22 17:58:26 -07001375
1376def 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 +08001377 """
1378 Command Example:
1379 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 -07001380 of-agent vtp 10001 vni 10
macauleyfddc4662015-07-27 17:40:30 +08001381 """
1382
1383 config_vtep_xml="""
1384 <config>
1385 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1386 <id>capable-switch-1</id>
1387 <resources>
1388 <port xc:operation="OPERATION">
Pierbbdf3782016-08-22 17:58:26 -07001389 <resource-id>LPORT</resource-id>
macauleyfddc4662015-07-27 17:40:30 +08001390 <features>
1391 <current>
1392 <rate>10Gb</rate>
1393 <medium>fiber</medium>
Pierbbdf3782016-08-22 17:58:26 -07001394 <pause>symmetric</pause>
macauleyfddc4662015-07-27 17:40:30 +08001395 </current>
1396 <advertised>
1397 <rate>10Gb</rate>
1398 <rate>100Gb</rate>
1399 <medium>fiber</medium>
1400 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001401 </advertised>
macauleyfddc4662015-07-27 17:40:30 +08001402 <supported>
1403 <rate>10Gb</rate>
1404 <rate>100Gb</rate>
1405 <medium>fiber</medium>
1406 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001407 </supported>
macauleyfddc4662015-07-27 17:40:30 +08001408 <advertised-peer>
1409 <rate>10Gb</rate>
1410 <rate>100Gb</rate>
1411 <medium>fiber</medium>
1412 <pause>symmetric</pause>
Pierbbdf3782016-08-22 17:58:26 -07001413 </advertised-peer>
macauleyfddc4662015-07-27 17:40:30 +08001414 </features>
1415 <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01">
1416 <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip>
1417 <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip>
1418 <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port>
macauley25999cf2015-08-07 17:03:24 +08001419 <ofdpa10:vni xc:operation="OPERATION">
1420 <ofdpa10:id>VNID</ofdpa10:id>
1421 </ofdpa10:vni>
macauleyfddc4662015-07-27 17:40:30 +08001422 <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id>
1423 <ofdpa10:ttl>TTL</ofdpa10:ttl>
1424 </ofdpa10:vtep>
Pierbbdf3782016-08-22 17:58:26 -07001425 </port>
macauleyfddc4662015-07-27 17:40:30 +08001426 </resources>
1427 <logical-switches>
1428 <switch>
1429 <id>DATAPATH_ID</id>
1430 <datapath-id>DATAPATH_ID</datapath-id>
1431 <resources>
1432 <port xc:operation="OPERATION">LPORT</port>
1433 </resources>
1434 </switch>
1435 </logical-switches>
1436 </capable-switch>
Pierbbdf3782016-08-22 17:58:26 -07001437 </config>
macauleyfddc4662015-07-27 17:40:30 +08001438 """
Pierbbdf3782016-08-22 17:58:26 -07001439 str_datapath_id_f= "{:016x}".format(dp_id)
1440 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1441 config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +08001442 config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport)))
Pierbbdf3782016-08-22 17:58:26 -07001443 config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip))
1444 config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip))
1445 config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port))
1446 config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1447 config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl))
macauleyfddc4662015-07-27 17:40:30 +08001448 config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid))
Pierbbdf3782016-08-22 17:58:26 -07001449 config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation))
macauleyfddc4662015-07-27 17:40:30 +08001450
Pierbbdf3782016-08-22 17:58:26 -07001451 return config_vtep_xml
1452
1453def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001454 #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2
1455 config_nexthop_xml="""
1456 <config>
1457 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1458 <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1459 <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id>
1460 <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac>
1461 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1462 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1463 </ofdpa10:next-hop>
1464 </of11-config:capable-switch>
1465 </config>
1466 """
1467 config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan))
Pierbbdf3782016-08-22 17:58:26 -07001468 config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port))
1469 config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1470 config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac))
1471 config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation))
1472 return config_nexthop_xml
macauleyfddc4662015-07-27 17:40:30 +08001473
Pierbbdf3782016-08-22 17:58:26 -07001474def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'):
macauleyfddc4662015-07-27 17:40:30 +08001475 #of-agent vni 10 multicast 224.1.1.1 nexthop 20
Pierbbdf3782016-08-22 17:58:26 -07001476 if mcast_ipv4!=None:
macauleyfddc4662015-07-27 17:40:30 +08001477 config_vni_xml="""
1478 <config>
1479 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1480 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1481 <ofdpa10:id>VNID</ofdpa10:id>
1482 <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group>
1483 <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id>
1484 </ofdpa10:vni>
1485 </of11-config:capable-switch>
1486 </config>
Pierbbdf3782016-08-22 17:58:26 -07001487 """
1488 config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1489 config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4))
macauleyfddc4662015-07-27 17:40:30 +08001490 else:
1491 config_vni_xml="""
1492 <config>
1493 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1494 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1495 <ofdpa10:id>VNID</ofdpa10:id>
1496 </ofdpa10:vni>
1497 </of11-config:capable-switch>
1498 </config>
Pierbbdf3782016-08-22 17:58:26 -07001499 """
1500
1501 config_vni_xml=config_vni_xml.replace("VNID", str(vni_id))
1502 config_vni_xml=config_vni_xml.replace("OPERATION", str(operation))
macauleyfddc4662015-07-27 17:40:30 +08001503 return config_vni_xml
Pierbbdf3782016-08-22 17:58:26 -07001504
1505def get_featureReplay(self):
macauleyfddc4662015-07-27 17:40:30 +08001506 req = ofp.message.features_request()
1507 res, raw = self.controller.transact(req)
Pierbbdf3782016-08-22 17:58:26 -07001508 self.assertIsNotNone(res, "Did not receive a response from the DUT.")
macauleyfddc4662015-07-27 17:40:30 +08001509 self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY,
1510 ("Unexpected packet type %d received in response to "
1511 "OFPT_FEATURES_REQUEST") % res.type)
Pierbbdf3782016-08-22 17:58:26 -07001512 return res
1513
macauleyfddc4662015-07-27 17:40:30 +08001514def send_edit_config(switch_ip, xml, target='runing'):
1515 NETCONF_ACCOUNT="netconfuser"
1516 NETCONF_PASSWD="netconfuser"
1517 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1518 try:
Pierbbdf3782016-08-22 17:58:26 -07001519 m.edit_config(target='running',
1520 config=xml,
1521 default_operation='merge',
macauleyfddc4662015-07-27 17:40:30 +08001522 error_option='stop-on-error')
1523
1524 except Exception as e:
1525 logging.info("Fail to set xml %s", xml)
1526 return False
1527
1528 #return m.get_config(source='running').data_xml
1529 return True
1530
1531def send_delete_config(switch_ip, xml, target='runing'):
1532 NETCONF_ACCOUNT="netconfuser"
1533 NETCONF_PASSWD="netconfuser"
1534 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1535 try:
Pierbbdf3782016-08-22 17:58:26 -07001536 m.edit_config(target='running',
1537 config=xml,
1538 default_operation='delete',
macauleyfddc4662015-07-27 17:40:30 +08001539 error_option='stop-on-error')
1540
1541 except Exception as e:
1542 logging.info("Fail to set xml %s", xml)
1543 return False
1544
1545 #return m.get_config(source='running').data_xml
1546 return True
Pierbbdf3782016-08-22 17:58:26 -07001547
macauleyfddc4662015-07-27 17:40:30 +08001548def get_edit_config(switch_ip, target='runing'):
1549 NETCONF_ACCOUNT="netconfuser"
1550 NETCONF_PASSWD="netconfuser"
1551 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 +08001552 return m.get_config(source='running').data_xml
macauleydbff3272015-07-30 14:07:16 +08001553
macauley_cheng67da9262015-08-31 15:18:41 +08001554
1555"""
1556MPLS
1557"""
1558
1559OFDPA_MPLS_SUBTYPE_SHIFT=24
Pierbbdf3782016-08-22 17:58:26 -07001560OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1
macauley_cheng67da9262015-08-31 15:18:41 +08001561OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2
1562OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3
1563OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4
1564OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5
1565OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6
1566OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8
1567OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10
1568
Piercf76e802016-09-19 20:16:35 -07001569
1570
1571
macauley_cheng67da9262015-08-31 15:18:41 +08001572def encode_mpls_interface_group_id(subtype, index):
1573 index=index&0x00ffffff
1574 assert(subtype==0)
1575 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1576
1577def encode_mpls_label_group_id(subtype, index):
1578 index=index&0x00ffffff
1579 assert(subtype <=5 or subtype==0)
1580 #1: l2 vpn label
1581 #2: l3 vpn label
1582 #3: mpls tunnel label 1
1583 #4: mpls tunnel lable 2
1584 #5: mpls swap label
Pierbbdf3782016-08-22 17:58:26 -07001585 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
macauley_cheng67da9262015-08-31 15:18:41 +08001586
1587def encode_mpls_forwarding_group_id(subtype, index):
1588 index=index&0x00ffffff
1589 assert(subtype==6 or subtype==8 or subtype==10)
Pierbbdf3782016-08-22 17:58:26 -07001590 return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
macauley_cheng67da9262015-08-31 15:18:41 +08001591
1592
1593def add_mpls_intf_group(ctrl, ref_gid, dst_mac, src_mac, vid, index, subtype=0):
1594 action=[]
1595 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
1596 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
Piercf76e802016-09-19 20:16:35 -07001597 if vid != 1:
1598 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001599 action.append(ofp.action.group(ref_gid))
Pierbbdf3782016-08-22 17:58:26 -07001600
macauley_cheng67da9262015-08-31 15:18:41 +08001601 buckets = [ofp.bucket(actions=action)]
1602
1603 mpls_group_id =encode_mpls_interface_group_id(subtype, index)
1604 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1605 group_id=mpls_group_id,
1606 buckets=buckets
1607 )
1608 ctrl.message_send(request)
1609 return mpls_group_id, request
1610
Piercf76e802016-09-19 20:16:35 -07001611def add_mpls_tunnel_label_group(
1612 ctrl,
1613 ref_gid,
1614 subtype,
1615 index,
1616 label,
1617 ):
1618
1619 action=[]
1620 action.append(ofp.action.push_mpls(0x8847))
1621 action.append(ofp.action.set_field(ofp.oxm.mpls_label(label)))
1622 action.append(ofp.action.group(ref_gid))
1623 buckets = [ofp.bucket(actions=action)]
1624
1625 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1626 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1627 group_id=mpls_group_id,
1628 buckets=buckets
1629 )
1630 ctrl.message_send(request)
1631
1632 return mpls_group_id, request
1633
Pierb5da4c92016-09-21 11:23:35 -07001634def add_mpls_swap_label_group(
1635 ctrl,
1636 ref_gid,
1637 subtype,
1638 index,
1639 label,
1640 ):
1641
1642 action=[]
1643 action.append(ofp.action.set_field(ofp.oxm.mpls_label(label)))
1644 action.append(ofp.action.group(ref_gid))
1645 buckets = [ofp.bucket(actions=action)]
1646
1647 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1648 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1649 group_id=mpls_group_id,
1650 buckets=buckets
1651 )
1652 ctrl.message_send(request)
1653
1654 return mpls_group_id, request
1655
Pierbbdf3782016-08-22 17:58:26 -07001656def add_mpls_label_group(ctrl, subtype, index, ref_gid,
macauley_cheng67da9262015-08-31 15:18:41 +08001657 lmep_id=-1,
1658 qos_index=-1,
1659 push_l2_header=False,
1660 push_vlan=False,
1661 push_mpls_header=False,
1662 push_cw=False,
1663 set_mpls_label=None,
1664 set_bos=None,
1665 set_tc=None,
1666 set_tc_from_table=False,
1667 cpy_tc_outward=False,
1668 set_ttl=None,
1669 cpy_ttl_outward=False,
1670 oam_lm_tx_count=False,
1671 set_pri_from_table=False
1672 ):
1673 """
1674 @ref_gid: only can be mpls intf group or mpls tunnel label 1/2 group
Pierbbdf3782016-08-22 17:58:26 -07001675 """
macauley_cheng67da9262015-08-31 15:18:41 +08001676 action=[]
1677
1678 if push_vlan== True:
1679 action.append(ofp.action.push_vlan(0x8100))
1680 if push_mpls_header== True:
1681 action.append(ofp.action.push_mpls(0x8847))
1682 if set_mpls_label != None:
1683 action.append(ofp.action.set_field(ofp.oxm.mpls_label(set_mpls_label)))
1684 if set_bos != None:
1685 action.append(ofp.action.set_field(ofp.oxm.mpls_bos(set_bos)))
1686 if set_tc != None:
1687 assert(set_tc_from_table==False)
1688 action.append(ofp.action.set_field(ofp.oxm.mpls_tc(set_tc)))
1689 if set_ttl != None:
Pierbbdf3782016-08-22 17:58:26 -07001690 action.append(ofp.action.set_mpls_ttl(set_ttl))
macauley_cheng67da9262015-08-31 15:18:41 +08001691 if cpy_ttl_outward == True:
Pierbbdf3782016-08-22 17:58:26 -07001692 action.append(ofp.action.copy_ttl_out())
macauley_cheng67da9262015-08-31 15:18:41 +08001693 """
1694 ofdpa experimenter
Pierbbdf3782016-08-22 17:58:26 -07001695 """
macauley_cheng67da9262015-08-31 15:18:41 +08001696 if push_l2_header== True:
Pierbbdf3782016-08-22 17:58:26 -07001697 action.append(ofp.action.ofdpa_push_l2_header())
macauley_cheng67da9262015-08-31 15:18:41 +08001698 if set_tc_from_table== True:
1699 assert(qos_index>=0)
1700 assert(set_tc == None)
Pierbbdf3782016-08-22 17:58:26 -07001701 action.append(ofp.action.ofdpa_set_tc_from_table(qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08001702 if cpy_tc_outward == True:
Pierbbdf3782016-08-22 17:58:26 -07001703 action.append(ofp.action.ofdpa_copy_tc_out())
macauley_cheng67da9262015-08-31 15:18:41 +08001704 if oam_lm_tx_count == True:
Pierbbdf3782016-08-22 17:58:26 -07001705 assert(qos_index>=0 and lmep_id>=0)
1706 action.append(ofp.action.ofdpa_oam_lm_tx_count(lmep_id, qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08001707 if set_pri_from_table == True:
Pierbbdf3782016-08-22 17:58:26 -07001708 assert(qos_index>=0)
1709 action.append(ofp.action.ofdpa_set_qos_from_table(qos_index))
macauley_cheng67da9262015-08-31 15:18:41 +08001710 if push_cw == True:
1711 action.append(ofp.action.ofdpa_push_cw())
Pierbbdf3782016-08-22 17:58:26 -07001712
1713 action.append(ofp.action.group(ref_gid))
macauley_cheng67da9262015-08-31 15:18:41 +08001714 buckets = [ofp.bucket(actions=action)]
Pierbbdf3782016-08-22 17:58:26 -07001715
macauley_cheng67da9262015-08-31 15:18:41 +08001716 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1717 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1718 group_id=mpls_group_id,
1719 buckets=buckets
1720 )
1721 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -07001722
1723 return mpls_group_id, request
1724
Piercf76e802016-09-19 20:16:35 -07001725def add_mpls_l2_port_flow(ctrl, of_port, mpls_l2_port, tunnel_index, ref_gid, qos_index=0):
1726 """
1727 Only action is Group, which must indicate one of:
1728 MPLS L2 VPN Label or Fast Failover Protection Group.
1729 ref_gid contains this information
1730 """
1731 tunnel_id = tunnel_index + ofp.oxm.TUNNEL_ID_BASE
1732
1733 match = ofp.match()
1734 match.oxm_list.append(ofp.oxm.exp4ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_MPLS_L2_PORT, value=0x00000000 + of_port))
1735 match.oxm_list.append(ofp.oxm.tunnel_id(tunnel_index + ofp.oxm.TUNNEL_ID_BASE))
1736
Pier23784aa2016-09-19 20:08:21 -07001737
Piercf76e802016-09-19 20:16:35 -07001738 action = []
1739 action.append(ofp.action.group(ref_gid))
1740 assert(qos_index>=0)
1741 action.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=ofp.oxm.OFDPA_EXP_TYPE_QOS_INDEX, value=qos_index)))
1742
1743 request = ofp.message.flow_add(
1744 table_id=MPLS_L2_PORT_FLOW_TABLE,
1745 cookie=42,
1746 match=match,
1747 instructions=[
1748 ofp.instruction.write_actions(
1749 actions=action
1750 ),
1751 ofp.instruction.goto_table(MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE) ],
1752 buffer_id=ofp.OFP_NO_BUFFER,
1753 priority=1)
1754 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)
1755 ctrl.message_send(request)
1756 return request
1757
1758 return
1759
Pierbbdf3782016-08-22 17:58:26 -07001760def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids,
1761 watch_port=None,
1762 watch_group=ofp.OFPP_ANY,
macauley_cheng67da9262015-08-31 15:18:41 +08001763 push_vlan=None,
macauley_chengd17ce512015-08-31 17:45:51 +08001764 pop_vlan=None,
macauley_cheng67da9262015-08-31 15:18:41 +08001765 set_vid=None):
1766 assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP
1767 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP
1768 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG)
macauley_chengd17ce512015-08-31 17:45:51 +08001769
macauley_cheng67da9262015-08-31 15:18:41 +08001770 buckets=[]
1771 if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP:
macauley_chengd17ce512015-08-31 17:45:51 +08001772 group_type = ofp.OFPGT_FF
macauley_cheng67da9262015-08-31 15:18:41 +08001773 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001774 action=[]
Pierbbdf3782016-08-22 17:58:26 -07001775 action.append(ofp.action.group(gid))
macauley_chengd17ce512015-08-31 17:45:51 +08001776 buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action))
1777
macauley_cheng67da9262015-08-31 15:18:41 +08001778 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP:
1779 group_type = ofp.OFPGT_SELECT
1780 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001781 action=[]
Pierbbdf3782016-08-22 17:58:26 -07001782 action.append(ofp.action.group(gid))
macauley_cheng67da9262015-08-31 15:18:41 +08001783 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001784
macauley_cheng67da9262015-08-31 15:18:41 +08001785 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG:
1786 group_type = ofp.OFPGT_INDIRECT
macauley_chengd17ce512015-08-31 17:45:51 +08001787 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08001788 if set_vid!=None:
Flavio Castro91d1a552016-05-17 16:59:44 -07001789 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+set_vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001790 if push_vlan!=None:
Pierbbdf3782016-08-22 17:58:26 -07001791 action.append(ofp.action.push_vlan(push_vlan))
macauley_cheng67da9262015-08-31 15:18:41 +08001792 if pop_vlan!=None:
Pierbbdf3782016-08-22 17:58:26 -07001793 action.append(ofp.action.pop_vlan())
1794 action.append(ofp.action.group(ref_gids[0]))
macauley_cheng67da9262015-08-31 15:18:41 +08001795 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001796
1797 mpls_group_id = encode_mpls_forwarding_group_id(subtype, index)
macauley_cheng67da9262015-08-31 15:18:41 +08001798 request = ofp.message.group_add(group_type=group_type,
macauley_cheng67da9262015-08-31 15:18:41 +08001799 group_id=mpls_group_id,
1800 buckets=buckets
1801 )
1802 ctrl.message_send(request)
Pierbbdf3782016-08-22 17:58:26 -07001803 return mpls_group_id, request
macauley_chengd17ce512015-08-31 17:45:51 +08001804
1805
macauley_cheng67da9262015-08-31 15:18:41 +08001806"""
Piercf76e802016-09-19 20:16:35 -07001807display
Pierbbdf3782016-08-22 17:58:26 -07001808"""
macauleydbff3272015-07-30 14:07:16 +08001809def print_current_table_flow_stat(ctrl, table_id=0xff):
1810 stat_req=ofp.message.flow_stats_request()
1811 response, pkt = ctrl.transact(stat_req)
1812 if response == None:
1813 print "no response"
1814 return None
1815 print len(response.entries)
1816 for obj in response.entries:
1817 print "match ", obj.match
1818 print "cookie", obj.cookie
1819 print "priority", obj.priority
1820 print "idle_timeout", obj.idle_timeout
1821 print "hard_timeout", obj.hard_timeout
1822 #obj.actions
Flavio Castro167f5bd2015-12-02 19:33:53 -05001823 print "packet count: %lx"%obj.packet_count