blob: d31c3a36d46d7eaae559a5677b3fadb3095c397d [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
21
macauleye8b140e2015-08-03 13:35:45 +080022PORT_FLOW_TABLE=0
23VLAN_FLOW_TABLE=10
24TERMINATION_FLOW_TABLE=20
25UCAST_ROUTING_FLOW_TABLE=30
26MCAST_ROUTING_FLOW_TABLE=40
27BRIDGE_FLOW_TABLE=50
28ACL_FLOW_TABLE=60
29
30def convertIP4toStr(ip_addr):
31 a=(ip_addr&0xff000000)>>24
32 b=(ip_addr&0x00ff0000)>>16
33 c=(ip_addr&0x0000ff00)>>8
34 d=(ip_addr&0x000000ff)
35 return str(a)+"."+str(b)+"."+str(c)+"."+str(d)
36
37def convertMACtoStr(mac):
38 if not isinstance(mac, list):
39 assert(0)
40
41 return ':'.join(['%02X' % x for x in mac])
42
macauley7f89d962015-08-06 18:13:48 +080043def getSwitchCpuMACFromDPID(dpid):
44 str_datapath_id_f= "{:016x}".format(dpid)
45 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
46 switch_cpu_mac_str=str_datapath_id[6:]
47 switch_cpu_mac = switch_cpu_mac_str.split(":")
48 switch_cpu_mac=[int(switch_cpu_mac[i],16) for i in range(0, len(switch_cpu_mac))]
49
50 return switch_cpu_mac_str, switch_cpu_mac
macauley_cheng67da9262015-08-31 15:18:41 +080051
52def DumpGroup(stats, verify_group_stats, always_show=True):
53 if(len(stats) > len(verify_group_stats)):
54 min_len = len(verify_group_stats)
55 print "Stats Len is not the same, stats>verify_group_stats"
56 if(len(stats)< len(verify_group_stats)):
57 min_len = len(stats)
58 print "Stats Len is not the same, stats<verify_group_stats"
59 else:
60 min_len = len(stats)
macauleye8b140e2015-08-03 13:35:45 +080061
macauley_cheng67da9262015-08-31 15:18:41 +080062 print "\r\n"
63 for i in range(min_len):
64 gs = stats[i]
65 gv = verify_group_stats[i]
66 print "FromSwtich:(GID=%lx, TYPE=%lx)\r\nVerify :(GID=%lx, TYPE=%lx)"%(gs.group_id, gs.group_type, gv.group_id, gv.group_type)
67 if(len(gs.buckets) != len(gv.buckets)):
68 print "buckets len is not the same gs %lx, gv %lx",(len(gs.buckets), len(gv.buckets))
69
70 for j in range(len(gs.buckets)):
71 b1=gs.buckets[j]
72 b2=gv.buckets[j]
73 if(len(b1.actions) != len(b2.actions)):
74 print "action len is not the same"
75
76 for k in range(len(b1.actions)):
77 a1=b1.actions[k]
78 a2=b2.actions[k]
79 if(always_show == True):
80 print "a1:"+a1.show()
81 print "a2:"+a2.show()
82
83def AssertGroup(self, stats, verify_group_stats):
84 self.assertTrue(len(stats) ==len(verify_group_stats), "stats len is not the same")
85
86 for i in range(len(stats)):
87 gs = stats[i]
88 gv = verify_group_stats[i]
89 self.assertTrue(len(gs.buckets) == len(gv.buckets), "buckets len is not the same")
90
91 for j in range(len(gs.buckets)):
92 b1=gs.buckets[j]
93 b2=gv.buckets[j]
94 self.assertTrue(len(b1.actions) == len(b2.actions), "action len is not the same")
95
96 for k in range(len(b1.actions)):
97 a1=b1.actions[k]
98 a2=b2.actions[k]
99 self.assertEquals(a1, a2, "action is not the same")
100
macauley97557232015-07-16 17:28:07 +0800101def encode_l2_interface_group_id(vlan, id):
102 return id + (vlan << OFDPA_VLAN_ID_SHIFT)
103
104def encode_l2_rewrite_group_id(id):
105 return id + (1 << OFDPA_GROUP_TYPE_SHIFT)
106
107def encode_l3_unicast_group_id(id):
108 return id + (2 << OFDPA_GROUP_TYPE_SHIFT)
109
110def encode_l2_mcast_group_id(vlan, id):
111 return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (3 << OFDPA_GROUP_TYPE_SHIFT)
112
113def encode_l2_flood_group_id(vlan, id):
114 return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (4 << OFDPA_GROUP_TYPE_SHIFT)
115
116def encode_l3_interface_group_id(id):
117 return id + (5 << OFDPA_GROUP_TYPE_SHIFT)
118
119def encode_l3_mcast_group_id(vlan, id):
120 return id + (vlan << OFDPA_VLAN_ID_SHIFT)+(6 << OFDPA_GROUP_TYPE_SHIFT)
121
122def encode_l3_ecmp_group_id(id):
123 return id + (7 << OFDPA_GROUP_TYPE_SHIFT)
124
Flavio Castro91d1a552016-05-17 16:59:44 -0700125def encode_l2_unfiltered_group_id(id):
126 return id + (11 << OFDPA_GROUP_TYPE_SHIFT)
127
macauleyfddc4662015-07-27 17:40:30 +0800128def encode_l2_overlay_group_id(tunnel_id, subtype, index):
129 tunnel_id=tunnel_id&0xffff #16 bits
130 subtype = subtype&3 #2 bits
131 index = index & 0x3f #10 bits
132 return index + (tunnel_id << OFDPA_TUNNEL_ID_SHIFT)+ (subtype<<OFDPA_TUNNEL_SUBTYPE_SHIFT)+(8 << OFDPA_GROUP_TYPE_SHIFT)
macauley97557232015-07-16 17:28:07 +0800133
Flavio Castro91d1a552016-05-17 16:59:44 -0700134def add_l2_unfiltered_group(ctrl, ports, send_barrier=False):
135 # group table
136 # set up untag groups for each port
137 group_id_list=[]
138 msgs=[]
139 for of_port in ports:
140 # do stuff
141 group_id = encode_l2_unfiltered_group_id(of_port)
142 group_id_list.append(group_id)
143 actions = [ofp.action.output(of_port)]
144 actions.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=24, value=1)))
145
146 buckets = [ofp.bucket(actions=actions)]
147 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
148 group_id=group_id,
149 buckets=buckets
150 )
151 ctrl.message_send(request)
152 msgs.append(request)
153
154 if send_barrier:
155 do_barrier(ctrl)
156
157 return group_id_list, msgs
158
Flavio Castrod4c44d12015-12-08 14:44:18 -0500159def add_l2_interface_group(ctrl, ports, vlan_id=1, is_tagged=False, send_barrier=False):
macauley97557232015-07-16 17:28:07 +0800160 # group table
161 # set up untag groups for each port
macauley41904ed2015-07-16 17:38:35 +0800162 group_id_list=[]
macauley15909e72015-07-17 15:58:57 +0800163 msgs=[]
macauley97557232015-07-16 17:28:07 +0800164 for of_port in ports:
165 # do stuff
166 group_id = encode_l2_interface_group_id(vlan_id, of_port)
macauley41904ed2015-07-16 17:38:35 +0800167 group_id_list.append(group_id)
macauley97557232015-07-16 17:28:07 +0800168 if is_tagged:
169 actions = [
170 ofp.action.output(of_port),
171 ]
172 else:
173 actions = [
174 ofp.action.pop_vlan(),
175 ofp.action.output(of_port),
176 ]
177
178 buckets = [
179 ofp.bucket(actions=actions),
180 ]
181
182 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
183 group_id=group_id,
184 buckets=buckets
185 )
186 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800187 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800188
189 if send_barrier:
190 do_barrier(ctrl)
macauley41904ed2015-07-16 17:38:35 +0800191
macauley15909e72015-07-17 15:58:57 +0800192 return group_id_list, msgs
macauley97557232015-07-16 17:28:07 +0800193
Flavio Castrod4c44d12015-12-08 14:44:18 -0500194def add_one_l2_interface_group(ctrl, port, vlan_id=1, is_tagged=False, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800195 # group table
196 # set up untag groups for each port
197 group_id = encode_l2_interface_group_id(vlan_id, port)
198
199 if is_tagged:
200 actions = [
201 ofp.action.output(port),
202 ]
203 else:
204 actions = [
205 ofp.action.pop_vlan(),
206 ofp.action.output(port),
207 ]
208
209 buckets = [
210 ofp.bucket(actions=actions),
211 ]
212
213 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
214 group_id=group_id,
215 buckets=buckets
216 )
217 ctrl.message_send(request)
218
219 if send_barrier:
220 do_barrier(ctrl)
221
222 return group_id, request
223
macauley97557232015-07-16 17:28:07 +0800224def add_l2_mcast_group(ctrl, ports, vlanid, mcast_grp_index):
225 buckets=[]
226 for of_port in ports:
227 group_id = encode_l2_interface_group_id(vlanid, of_port)
228 action=[ofp.action.group(group_id)]
229 buckets.append(ofp.bucket(actions=action))
230
231 group_id =encode_l2_mcast_group_id(vlanid, mcast_grp_index)
232 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
233 group_id=group_id,
234 buckets=buckets
235 )
236 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800237 return request
macauley97557232015-07-16 17:28:07 +0800238
macauley15909e72015-07-17 15:58:57 +0800239def add_l2_flood_group(ctrl, ports, vlanid, id):
240 buckets=[]
241 for of_port in ports:
242 group_id = encode_l2_interface_group_id(vlanid, of_port)
243 action=[ofp.action.group(group_id)]
244 buckets.append(ofp.bucket(actions=action))
macauley97557232015-07-16 17:28:07 +0800245
macauley15909e72015-07-17 15:58:57 +0800246 group_id =encode_l2_flood_group_id(vlanid, id)
247 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
248 group_id=group_id,
249 buckets=buckets
250 )
251 ctrl.message_send(request)
252 return request
253
Flavio Castroa7162bb2016-07-25 17:30:30 -0700254def mod_l2_flood_group(ctrl, ports, vlanid, id):
255 buckets=[]
256 for of_port in ports:
257 group_id = encode_l2_interface_group_id(vlanid, of_port)
258 action=[ofp.action.group(group_id)]
259 buckets.append(ofp.bucket(actions=action))
260
261 group_id =encode_l2_flood_group_id(vlanid, id)
262 request = ofp.message.group_modify(group_type=ofp.OFPGT_ALL,
263 group_id=group_id,
264 buckets=buckets
265 )
266 ctrl.message_send(request)
267 return request
268
269
macauley15909e72015-07-17 15:58:57 +0800270def add_l2_rewrite_group(ctrl, port, vlanid, id, src_mac, dst_mac):
271 group_id = encode_l2_interface_group_id(vlanid, port)
272
273 action=[]
274 if src_mac is not None:
275 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
276
277 if dst_mac is not None:
278 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
279
Flavio Castro91d1a552016-05-17 16:59:44 -0700280 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
macauley_cheng93f3fa52015-09-02 17:57:31 +0800281
macauley15909e72015-07-17 15:58:57 +0800282 action.append(ofp.action.group(group_id))
283
284 buckets = [ofp.bucket(actions=action)]
285
286 group_id =encode_l2_rewrite_group_id(id)
287 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
288 group_id=group_id,
289 buckets=buckets
290 )
291 ctrl.message_send(request)
292 return request
293
294def add_l3_unicast_group(ctrl, port, vlanid, id, src_mac, dst_mac):
295 group_id = encode_l2_interface_group_id(vlanid, port)
296
297 action=[]
298 if src_mac is not None:
299 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
300
301 if dst_mac is not None:
302 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
303
Flavio Castroaf2b4502016-02-02 17:41:32 -0500304 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
macauley15909e72015-07-17 15:58:57 +0800305
306 action.append(ofp.action.group(group_id))
307
308 buckets = [ofp.bucket(actions=action)]
309
310 group_id =encode_l3_unicast_group_id(id)
311 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
312 group_id=group_id,
313 buckets=buckets
314 )
315 ctrl.message_send(request)
316 return request
317
318def add_l3_interface_group(ctrl, port, vlanid, id, src_mac):
319 group_id = encode_l2_interface_group_id(vlanid, port)
320
321 action=[]
322 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
Flavio Castro8ca52542016-04-11 11:24:49 -0400323 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
macauley15909e72015-07-17 15:58:57 +0800324 action.append(ofp.action.group(group_id))
325
326 buckets = [ofp.bucket(actions=action)]
327
328 group_id =encode_l3_interface_group_id(id)
329 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
330 group_id=group_id,
331 buckets=buckets
332 )
333 ctrl.message_send(request)
334 return request
335
336def add_l3_ecmp_group(ctrl, id, l3_ucast_groups):
337 buckets=[]
338 for group in l3_ucast_groups:
339 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
340
341 group_id =encode_l3_ecmp_group_id(id)
342 request = ofp.message.group_add(group_type=ofp.OFPGT_SELECT,
343 group_id=group_id,
344 buckets=buckets
345 )
346 ctrl.message_send(request)
347 return request
Flavio Castroa7162bb2016-07-25 17:30:30 -0700348
349def mod_l3_ecmp_group(ctrl, id, l3_ucast_groups):
350 buckets=[]
351 for group in l3_ucast_groups:
352 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
353
354 group_id =encode_l3_ecmp_group_id(id)
355 request = ofp.message.group_modify(group_type=ofp.OFPGT_SELECT,
356 group_id=group_id,
357 buckets=buckets
358 )
359 ctrl.message_send(request)
360 return request
361
macauley15909e72015-07-17 15:58:57 +0800362def add_l3_mcast_group(ctrl, vid, mcast_group_id, groups_on_buckets):
363 buckets=[]
364 for group in groups_on_buckets:
365 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
366
367 group_id =encode_l3_mcast_group_id(vid, mcast_group_id)
368 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
369 group_id=group_id,
370 buckets=buckets
371 )
372 ctrl.message_send(request)
373 return request
macauleyfddc4662015-07-27 17:40:30 +0800374
375def add_l2_overlay_flood_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
376 buckets=[]
377 for port in ports:
378 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
379
380 group_id=encode_l2_overlay_group_id(tunnel_id, 0, index)
381 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
382 group_id=group_id,
383 buckets=buckets
384 )
385 ctrl.message_send(request)
386 return request
387
388def add_l2_overlay_flood_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
389 buckets=[]
390 for port in ports:
391 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
392
393 group_id=encode_l2_overlay_group_id(tunnel_id, 1, index)
394 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
395 group_id=group_id,
396 buckets=buckets
397 )
398 ctrl.message_send(request)
399 return request
400
401def add_l2_overlay_mcast_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
402 buckets=[]
403 for port in ports:
404 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
405
406 group_id=encode_l2_overlay_group_id(tunnel_id, 2, index)
407 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
408 group_id=group_id,
409 buckets=buckets
410 )
411 ctrl.message_send(request)
412 return request
413
414def add_l2_overlay_mcast_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
415 buckets=[]
416 for port in ports:
417 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
418
419 group_id=encode_l2_overlay_group_id(tunnel_id, 3, index)
420 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
421 group_id=group_id,
422 buckets=buckets
423 )
424 ctrl.message_send(request)
425 return request
426
427def add_port_table_flow(ctrl, is_overlay=True):
428 match = ofp.match()
429
430 if is_overlay == True:
431 match.oxm_list.append(ofp.oxm.in_port(0x10000))
macauleydbff3272015-07-30 14:07:16 +0800432 NEXT_TABLE=50
macauleyfddc4662015-07-27 17:40:30 +0800433 else:
434 match.oxm_list.append(ofp.oxm.in_port(0))
macauleydbff3272015-07-30 14:07:16 +0800435 NEXT_TABLE=10
macauleyfddc4662015-07-27 17:40:30 +0800436
437 request = ofp.message.flow_add(
438 table_id=0,
439 cookie=42,
440 match=match,
441 instructions=[
macauleydbff3272015-07-30 14:07:16 +0800442 ofp.instruction.goto_table(NEXT_TABLE)
macauleyfddc4662015-07-27 17:40:30 +0800443 ],
444 priority=0)
445 logging.info("Add port table, match port %lx" % 0x10000)
446 ctrl.message_send(request)
macauleydbff3272015-07-30 14:07:16 +0800447
Flavio Castrod8f8af22015-12-02 18:19:26 -0500448
449def pop_vlan_flow(ctrl, ports, vlan_id=1):
450 # table 10: vlan
451 # goto to table 20
452 msgs=[]
453 for of_port in ports:
454 match = ofp.match()
455 match.oxm_list.append(ofp.oxm.in_port(of_port))
456 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
457 request = ofp.message.flow_add(
458 table_id=10,
459 cookie=42,
460 match=match,
461 instructions=[
462 ofp.instruction.apply_actions(
463 actions=[
464 ofp.action.pop_vlan()
465 ]
466 ),
467 ofp.instruction.goto_table(20)
468 ],
469 priority=0)
470 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
471 ctrl.message_send(request)
472
473
474 return msgs
macauleyfddc4662015-07-27 17:40:30 +0800475
macauley97557232015-07-16 17:28:07 +0800476def add_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
477 # table 10: vlan
478 # goto to table 20
macauley15909e72015-07-17 15:58:57 +0800479 msgs=[]
macauley97557232015-07-16 17:28:07 +0800480 for of_port in ports:
Flavio Castro932014b2016-01-05 18:29:15 -0500481 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
macauley97557232015-07-16 17:28:07 +0800482 match = ofp.match()
483 match.oxm_list.append(ofp.oxm.in_port(of_port))
484 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
485 request = ofp.message.flow_add(
486 table_id=10,
487 cookie=42,
488 match=match,
489 instructions=[
Flavio Castrod8f8af22015-12-02 18:19:26 -0500490 ofp.instruction.apply_actions(
491 actions=[
492 ofp.action.pop_vlan()
493 ]
494 ),
macauley97557232015-07-16 17:28:07 +0800495 ofp.instruction.goto_table(20)
496 ],
497 priority=0)
498 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
499 ctrl.message_send(request)
500
501 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
502 match = ofp.match()
503 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500504 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
macauley97557232015-07-16 17:28:07 +0800505 request = ofp.message.flow_add(
506 table_id=10,
507 cookie=42,
508 match=match,
509 instructions=[
510 ofp.instruction.apply_actions(
511 actions=[
Flavio Castroaf2b4502016-02-02 17:41:32 -0500512 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
macauley97557232015-07-16 17:28:07 +0800513 ]
514 ),
515 ofp.instruction.goto_table(20)
516 ],
517 priority=0)
518 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
519 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800520 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800521
Flavio Castro932014b2016-01-05 18:29:15 -0500522 if (flag == 4) :
523 match = ofp.match()
524 match.oxm_list.append(ofp.oxm.in_port(of_port))
525 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1fff))
526 request = ofp.message.flow_add(
527 table_id=10,
528 cookie=42,
529 match=match,
530 instructions=[
531 ofp.instruction.apply_actions(
532 actions=[
Flavio Castroaf2b4502016-02-02 17:41:32 -0500533 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
Flavio Castro932014b2016-01-05 18:29:15 -0500534 ]
535 ),
536 ofp.instruction.goto_table(20)
537 ],
538 priority=0)
539 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
540 ctrl.message_send(request)
541 msgs.append(request)
542
macauley97557232015-07-16 17:28:07 +0800543 if send_barrier:
544 do_barrier(ctrl)
545
macauley15909e72015-07-17 15:58:57 +0800546 return msgs
macauley_cheng6e6a6122015-11-16 14:19:18 +0800547
548def del_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
549 # table 10: vlan
550 # goto to table 20
551 msgs=[]
552 for of_port in ports:
553 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
554 match = ofp.match()
555 match.oxm_list.append(ofp.oxm.in_port(of_port))
556 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
557 request = ofp.message.flow_delete(
558 table_id=10,
559 cookie=42,
560 match=match,
561 priority=0)
562 logging.info("Del vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
563 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800564
macauley_cheng6e6a6122015-11-16 14:19:18 +0800565 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
566 match = ofp.match()
567 match.oxm_list.append(ofp.oxm.in_port(of_port))
568 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0xfff))
569 request = ofp.message.flow_delete(
570 table_id=10,
571 cookie=42,
572 match=match,
573 priority=0)
574 logging.info("Del vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
575 ctrl.message_send(request)
576 msgs.append(request)
577
578 if send_barrier:
579 do_barrier(ctrl)
580
581 return msgs
582
macauley_cheng6b311612015-09-04 11:32:27 +0800583def add_vlan_table_flow_pvid(ctrl, in_port, match_vid=None, pvid=1, send_barrier=False):
584 """it will tag pack as untagged packet wether it has tagg or not"""
585 match = ofp.match()
586 match.oxm_list.append(ofp.oxm.in_port(in_port))
587 actions=[]
588 if match_vid == None:
589 match.oxm_list.append(ofp.oxm.vlan_vid(0))
590 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
591 goto_table=20
592 else:
593 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+match_vid, 0x1fff))
594 actions.append(ofp.action.push_vlan(0x8100))
595 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
596 goto_table=20
597
598 request = ofp.message.flow_add(
599 table_id=10,
600 cookie=42,
601 match=match,
602 instructions=[
603 ofp.instruction.apply_actions(actions=actions)
604 ,ofp.instruction.goto_table(goto_table)
605 ],
606 priority=0)
607 logging.info("Add PVID %d on port %d and go to table %ld" %( pvid, in_port, goto_table))
608 ctrl.message_send(request)
609
610 if send_barrier:
611 do_barrier(ctrl)
612
613def add_vlan_table_flow_allow_all_vlan(ctrl, in_port, send_barrier=False):
614 """it st flow allow all vlan tag on this port"""
615 match = ofp.match()
616 match.oxm_list.append(ofp.oxm.in_port(in_port))
617 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1000))
618 request = ofp.message.flow_add(
619 table_id=10,
620 cookie=42,
621 match=match,
622 instructions=[
623 ofp.instruction.goto_table(20)
624 ],
625 priority=0)
626 logging.info("Add allow all vlan on port %d " %(in_port))
627 ctrl.message_send(request)
628
macauley53d90fe2015-08-04 17:34:22 +0800629def add_one_vlan_table_flow(ctrl, of_port, vlan_id=1, vrf=0, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800630 # table 10: vlan
631 # goto to table 20
Flavio Castro932014b2016-01-05 18:29:15 -0500632 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
macauley0f91a3e2015-07-17 18:09:59 +0800633 match = ofp.match()
634 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500635 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
macauley7f89d962015-08-06 18:13:48 +0800636
637 actions=[]
638 if vrf!=0:
639 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
640
Flavio Castro6d498522015-12-15 14:05:04 -0500641 #actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id)))
macauley7f89d962015-08-06 18:13:48 +0800642
macauley0f91a3e2015-07-17 18:09:59 +0800643 request = ofp.message.flow_add(
644 table_id=10,
645 cookie=42,
646 match=match,
647 instructions=[
macauley53d90fe2015-08-04 17:34:22 +0800648 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800649 actions=actions
macauley53d90fe2015-08-04 17:34:22 +0800650 ),
651 ofp.instruction.goto_table(20)
macauley0f91a3e2015-07-17 18:09:59 +0800652 ],
653 priority=0)
654 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
655 ctrl.message_send(request)
656
657 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
658 match = ofp.match()
659 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500660 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
macauley53d90fe2015-08-04 17:34:22 +0800661
macauley7f89d962015-08-06 18:13:48 +0800662 actions=[]
663 if vrf!=0:
664 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
665
Flavio Castro91d1a552016-05-17 16:59:44 -0700666 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
macauley7f89d962015-08-06 18:13:48 +0800667
macauley0f91a3e2015-07-17 18:09:59 +0800668 request = ofp.message.flow_add(
669 table_id=10,
670 cookie=42,
671 match=match,
672 instructions=[
673 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800674 actions=actions
macauley0f91a3e2015-07-17 18:09:59 +0800675 ),
676 ofp.instruction.goto_table(20)
677 ],
678 priority=0)
679 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
680 ctrl.message_send(request)
Flavio Castro932014b2016-01-05 18:29:15 -0500681
682 if (flag == 4) :
683 match = ofp.match()
684 match.oxm_list.append(ofp.oxm.in_port(of_port))
685 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000,0x1fff))
686
687 actions=[]
688 if vrf!=0:
689 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
690
Flavio Castro91d1a552016-05-17 16:59:44 -0700691 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Flavio Castro932014b2016-01-05 18:29:15 -0500692
693 request = ofp.message.flow_add(
694 table_id=10,
695 cookie=42,
696 match=match,
697 instructions=[
698 ofp.instruction.apply_actions(
699 actions=actions
700 ),
701 ofp.instruction.goto_table(20)
702 ],
703 priority=0)
704 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
705 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800706
707 if send_barrier:
708 do_barrier(ctrl)
709
710 return request
macauley15909e72015-07-17 15:58:57 +0800711
macauley97557232015-07-16 17:28:07 +0800712def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False):
713 match = ofp.match()
castroflavio21894482015-12-08 15:29:55 -0500714 priority=500
macauleyfddc4662015-07-27 17:40:30 +0800715 if dst_mac!=None:
castroflavio21894482015-12-08 15:29:55 -0500716 priority=1000
macauleyfddc4662015-07-27 17:40:30 +0800717 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
718
macauley97557232015-07-16 17:28:07 +0800719 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauleyfddc4662015-07-27 17:40:30 +0800720
macauley97557232015-07-16 17:28:07 +0800721 request = ofp.message.flow_add(
722 table_id=50,
723 cookie=42,
724 match=match,
725 instructions=[
726 ofp.instruction.write_actions(
727 actions=[
728 ofp.action.group(group_id)]),
729 ofp.instruction.goto_table(60)
730 ],
731 buffer_id=ofp.OFP_NO_BUFFER,
castroflavio21894482015-12-08 15:29:55 -0500732 priority=priority)
macauley97557232015-07-16 17:28:07 +0800733
734 logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac)
735 ctrl.message_send(request)
736
737 if send_barrier:
macauley15909e72015-07-17 15:58:57 +0800738 do_barrier(ctrl)
739
macauley0f91a3e2015-07-17 18:09:59 +0800740 return request
macauleyfddc4662015-07-27 17:40:30 +0800741
742def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False):
743 match = ofp.match()
744 if dst_mac!=None:
745 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
746
747 match.oxm_list.append(ofp.oxm.tunnel_id(vnid))
748 if is_group == True:
749 actions=[ofp.action.group(group_id)]
750 else:
751 actions=[ofp.action.output(group_id)]
752
753 request = ofp.message.flow_add(
754 table_id=50,
755 cookie=42,
756 match=match,
757 instructions=[
758 ofp.instruction.write_actions(
759 actions=actions),
760 ofp.instruction.goto_table(60)
761 ],
762 buffer_id=ofp.OFP_NO_BUFFER,
763 priority=1000)
764
765 logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac)
766 ctrl.message_send(request)
767
768 if send_barrier:
769 do_barrier(ctrl)
770
771 return request
macauley0f91a3e2015-07-17 18:09:59 +0800772
macauley_cheng6b133662015-11-09 13:52:39 +0800773def add_termination_flow(ctrl, in_port, eth_type, dst_mac, vlanid, goto_table=None, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800774 match = ofp.match()
macauley0f91a3e2015-07-17 18:09:59 +0800775 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauleyfddc4662015-07-27 17:40:30 +0800776 if dst_mac[0]&0x01 == 0x01:
777 match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00]))
778 goto_table=40
779 else:
macauley53d90fe2015-08-04 17:34:22 +0800780 if in_port!=0:
781 match.oxm_list.append(ofp.oxm.in_port(in_port))
macauleyfddc4662015-07-27 17:40:30 +0800782 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
783 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauley_cheng6b133662015-11-09 13:52:39 +0800784 if goto_table == None:
785 goto_table=30
macauley0f91a3e2015-07-17 18:09:59 +0800786
787 request = ofp.message.flow_add(
788 table_id=20,
789 cookie=42,
790 match=match,
791 instructions=[
792 ofp.instruction.goto_table(goto_table)
793 ],
794 buffer_id=ofp.OFP_NO_BUFFER,
795 priority=1)
796
797 logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac)
798 ctrl.message_send(request)
799
800 if send_barrier:
801 do_barrier(ctrl)
802
803 return request
804
Flavio Castroaf2b4502016-02-02 17:41:32 -0500805def 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 +0800806 match = ofp.match()
807 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauley53d90fe2015-08-04 17:34:22 +0800808 if vrf != 0:
809 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
macauley0f91a3e2015-07-17 18:09:59 +0800810
Flavio Castroaf2b4502016-02-02 17:41:32 -0500811 match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask))
812
813 instructions = []
814 instructions.append(ofp.instruction.goto_table(60))
815 if send_ctrl:
Flavio Castro91d1a552016-05-17 16:59:44 -0700816 instructions.append(ofp.instruction.write_actions(
Flavio Castroaf2b4502016-02-02 17:41:32 -0500817 actions=[ofp.action.output( port=ofp.OFPP_CONTROLLER,
818 max_len=ofp.OFPCML_NO_BUFFER)]))
819 else:
820 instructions.append(ofp.instruction.write_actions(
821 actions=[ofp.action.group(action_group_id)]))
macauley53d90fe2015-08-04 17:34:22 +0800822
macauley0f91a3e2015-07-17 18:09:59 +0800823 request = ofp.message.flow_add(
824 table_id=30,
825 cookie=42,
826 match=match,
Flavio Castroaf2b4502016-02-02 17:41:32 -0500827 instructions=instructions,
macauley0f91a3e2015-07-17 18:09:59 +0800828 buffer_id=ofp.OFP_NO_BUFFER,
829 priority=1)
830
831 logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip)
832 ctrl.message_send(request)
833
834 if send_barrier:
835 do_barrier(ctrl)
836
Flavio Castro9debaaa2016-07-26 19:37:50 -0700837 return request
Flavio Castrod8f8af22015-12-02 18:19:26 -0500838
Flavio Castro8ca52542016-04-11 11:24:49 -0400839def 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 -0400840 match = ofp.match()
841 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
842 match.oxm_list.append(ofp.oxm.mpls_label(label))
843 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
844 actions = [ofp.action.dec_mpls_ttl(),
Flavio Castro8ca52542016-04-11 11:24:49 -0400845 ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))]
846 if (goto_table == 29):
Flavio Castro8ca52542016-04-11 11:24:49 -0400847 actions.append(ofp.action.group(action_group_id))
848 else:
Flavio Castrod80fbc32016-07-25 15:54:26 -0700849 actions.append(ofp.action.set_field(
850 ofp.oxm.exp2ByteValue(exp_type=23, value=32)))
851 actions.append(ofp.action.group(action_group_id))
Flavio Castro8ca52542016-04-11 11:24:49 -0400852 actions.append(ofp.action.copy_ttl_in())
Flavio Castro9debaaa2016-07-26 19:37:50 -0700853
Flavio Castrob702a2f2016-04-10 22:01:48 -0400854 request = ofp.message.flow_add(
855 table_id=24,
856 cookie=43,
857 match=match,
858 instructions=[
Flavio Castro8ca52542016-04-11 11:24:49 -0400859 ofp.instruction.apply_actions(actions=actions),
860 ofp.instruction.goto_table(goto_table)
Flavio Castrob702a2f2016-04-10 22:01:48 -0400861 ],
862 buffer_id=ofp.OFP_NO_BUFFER,
863 priority=1)
Flavio Castrob702a2f2016-04-10 22:01:48 -0400864 logging.info("Inserting MPLS flow , label %ld", label)
865 ctrl.message_send(request)
866
867 if send_barrier:
868 do_barrier(ctrl)
869
870 return request
871
macauleyfddc4662015-07-27 17:40:30 +0800872def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False):
873 match = ofp.match()
874 match.oxm_list.append(ofp.oxm.eth_type(0x0800))
875 match.oxm_list.append(ofp.oxm.vlan_vid(vlan_id))
876 if src_ip_mask!=0:
877 match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask))
878 else:
879 match.oxm_list.append(ofp.oxm.ipv4_src(src_ip))
880
881 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
882
883 request = ofp.message.flow_add(
884 table_id=40,
885 cookie=42,
886 match=match,
887 instructions=[
888 ofp.instruction.write_actions(
889 actions=[ofp.action.group(action_group_id)]),
890 ofp.instruction.goto_table(60)
891 ],
892 buffer_id=ofp.OFP_NO_BUFFER,
893 priority=1)
894
895 logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask)
896 ctrl.message_send(request)
897
898 if send_barrier:
899 do_barrier(ctrl)
900
901 return request
macauley_cheng6b133662015-11-09 13:52:39 +0800902
903#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 +0800904def 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 +0800905 match = ofp.match()
906 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
907 match.oxm_list.append(ofp.oxm.ipv4_dst(ip_dst))
908 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
909 match.oxm_list.append(ofp.oxm.tcp_dst(tcp_dst))
910
911 request = ofp.message.flow_add(
912 table_id=28,
913 cookie=42,
914 match=match,
915 instructions=[
916 ofp.instruction.write_actions(
917 actions=[ofp.action.set_field(ofp.oxm.ipv4_dst(set_ip_dst)),
918 ofp.action.set_field(ofp.oxm.tcp_dst(set_tcp_dst)),
919 ofp.action.group(action_group_id)]),
920 ofp.instruction.goto_table(60)
921 ],
922 buffer_id=ofp.OFP_NO_BUFFER,
923 priority=1)
924 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)
925 ctrl.message_send(request)
926 return request
macauley_chengeffc20a2015-11-09 16:14:56 +0800927
928#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
929def add_snat_flow(ctrl, eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src):
930 match = ofp.match()
931 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
932 match.oxm_list.append(ofp.oxm.ipv4_src(ip_src))
933 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
934 match.oxm_list.append(ofp.oxm.tcp_src(tcp_src))
935
936 request = ofp.message.flow_add(
937 table_id=29,
938 cookie=42,
939 match=match,
940 instructions=[
941 ofp.instruction.write_actions(
942 actions=[ofp.action.set_field(ofp.oxm.ipv4_src(set_ip_src)),
943 ofp.action.set_field(ofp.oxm.tcp_src(set_tcp_src))]),
944 ofp.instruction.goto_table(30)
945 ],
946 buffer_id=ofp.OFP_NO_BUFFER,
947 priority=1)
948 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)
949 ctrl.message_send(request)
950 return request
macauley_cheng6b133662015-11-09 13:52:39 +0800951
macauleyfddc4662015-07-27 17:40:30 +0800952def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'):
953 """
954 Command Example:
955 of-agent vtap 10001 ethernet 1/1 vid 1
956 of-agent vtp 10001 vni 10
957 """
958 if vlan != 0:
959 config_vtap_xml="""
960 <config>
961 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
962 <id>capable-switch-1</id>
963 <resources>
964 <port xc:operation="OPERATION">
965 <resource-id >LPORT</resource-id>
966 <features>
967 <current>
968 <rate>10Gb</rate>
969 <medium>fiber</medium>
970 <pause>symmetric</pause>
971 </current>
972 <advertised>
973 <rate>10Gb</rate>
974 <rate>100Gb</rate>
975 <medium>fiber</medium>
976 <pause>symmetric</pause>
977 </advertised>
978 <supported>
979 <rate>10Gb</rate>
980 <rate>100Gb</rate>
981 <medium>fiber</medium>
982 <pause>symmetric</pause>
983 </supported>
984 <advertised-peer>
985 <rate>10Gb</rate>
986 <rate>100Gb</rate>
987 <medium>fiber</medium>
988 <pause>symmetric</pause>
989 </advertised-peer>
990 </features>
991 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
992 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
993 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
994 <ofdpa10:vni>VNID</ofdpa10:vni>
995 </ofdpa10:vtap>
996 </port>
997 </resources>
998 <logical-switches>
999 <switch>
1000 <id>DATAPATH_ID</id>
1001 <datapath-id>DATAPATH_ID</datapath-id>
1002 <resources>
1003 <port xc:operation="OPERATION">LPORT</port>
1004 </resources>
1005 </switch>
1006 </logical-switches>
1007 </capable-switch>
1008 </config>
1009 """
1010 else:
1011 config_vtap_xml="""
1012 <config>
1013 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1014 <id>capable-switch-1</id>
1015 <resources>
1016 <port xc:operation="OPERATION">
1017 <resource-id >LPORT</resource-id>
1018 <features>
1019 <current>
1020 <rate>10Gb</rate>
1021 <medium>fiber</medium>
1022 <pause>symmetric</pause>
1023 </current>
1024 <advertised>
1025 <rate>10Gb</rate>
1026 <rate>100Gb</rate>
1027 <medium>fiber</medium>
1028 <pause>symmetric</pause>
1029 </advertised>
1030 <supported>
1031 <rate>10Gb</rate>
1032 <rate>100Gb</rate>
1033 <medium>fiber</medium>
1034 <pause>symmetric</pause>
1035 </supported>
1036 <advertised-peer>
1037 <rate>10Gb</rate>
1038 <rate>100Gb</rate>
1039 <medium>fiber</medium>
1040 <pause>symmetric</pause>
1041 </advertised-peer>
1042 </features>
1043 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1044 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1045 <ofdpa10:vni>VNID</ofdpa10:vni>
1046 </ofdpa10:vtap>
1047 </port>
1048 </resources>
1049 <logical-switches>
1050 <switch>
1051 <id>DATAPATH_ID</id>
1052 <datapath-id>DATAPATH_ID</datapath-id>
1053 <resources>
1054 <port xc:operation="OPERATION">LPORT</port>
1055 </resources>
1056 </switch>
1057 </logical-switches>
1058 </capable-switch>
1059 </config>
1060 """
1061 str_datapath_id_f= "{:016x}".format(dp_id)
1062 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1063 config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +08001064 config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport)))
macauleyfddc4662015-07-27 17:40:30 +08001065 config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port))
1066 config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan))
1067 config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid))
1068 config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation))
1069 return config_vtap_xml
1070
1071def get_vtep_lport_config_xml(dp_id, lport, src_ip, dst_ip, next_hop_id, vnid, udp_src_port=6633, ttl=25, operation='merge'):
1072 """
1073 Command Example:
1074 of-agent vtep 10002 source user-input-src-ip destination user-input-dst-ip udp-source-port 6633 nexthop 2 ttl 25
1075 of-agent vtp 10001 vni 10
1076 """
1077
1078 config_vtep_xml="""
1079 <config>
1080 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1081 <id>capable-switch-1</id>
1082 <resources>
1083 <port xc:operation="OPERATION">
1084 <resource-id>LPORT</resource-id>
1085 <features>
1086 <current>
1087 <rate>10Gb</rate>
1088 <medium>fiber</medium>
1089 <pause>symmetric</pause>
1090 </current>
1091 <advertised>
1092 <rate>10Gb</rate>
1093 <rate>100Gb</rate>
1094 <medium>fiber</medium>
1095 <pause>symmetric</pause>
1096 </advertised>
1097 <supported>
1098 <rate>10Gb</rate>
1099 <rate>100Gb</rate>
1100 <medium>fiber</medium>
1101 <pause>symmetric</pause>
1102 </supported>
1103 <advertised-peer>
1104 <rate>10Gb</rate>
1105 <rate>100Gb</rate>
1106 <medium>fiber</medium>
1107 <pause>symmetric</pause>
1108 </advertised-peer>
1109 </features>
1110 <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01">
1111 <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip>
1112 <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip>
1113 <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port>
macauley25999cf2015-08-07 17:03:24 +08001114 <ofdpa10:vni xc:operation="OPERATION">
1115 <ofdpa10:id>VNID</ofdpa10:id>
1116 </ofdpa10:vni>
macauleyfddc4662015-07-27 17:40:30 +08001117 <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id>
1118 <ofdpa10:ttl>TTL</ofdpa10:ttl>
1119 </ofdpa10:vtep>
1120 </port>
1121 </resources>
1122 <logical-switches>
1123 <switch>
1124 <id>DATAPATH_ID</id>
1125 <datapath-id>DATAPATH_ID</datapath-id>
1126 <resources>
1127 <port xc:operation="OPERATION">LPORT</port>
1128 </resources>
1129 </switch>
1130 </logical-switches>
1131 </capable-switch>
1132 </config>
1133 """
1134 str_datapath_id_f= "{:016x}".format(dp_id)
1135 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1136 config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +08001137 config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport)))
macauleyfddc4662015-07-27 17:40:30 +08001138 config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip))
1139 config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip))
1140 config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port))
1141 config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1142 config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl))
1143 config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid))
1144 config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation))
1145
1146 return config_vtep_xml
1147
1148def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'):
1149 #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2
1150 config_nexthop_xml="""
1151 <config>
1152 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1153 <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1154 <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id>
1155 <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac>
1156 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1157 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1158 </ofdpa10:next-hop>
1159 </of11-config:capable-switch>
1160 </config>
1161 """
1162 config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan))
1163 config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port))
1164 config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1165 config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac))
1166 config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation))
1167 return config_nexthop_xml
1168
1169def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'):
1170 #of-agent vni 10 multicast 224.1.1.1 nexthop 20
1171 if mcast_ipv4!=None:
1172 config_vni_xml="""
1173 <config>
1174 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1175 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1176 <ofdpa10:id>VNID</ofdpa10:id>
1177 <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group>
1178 <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id>
1179 </ofdpa10:vni>
1180 </of11-config:capable-switch>
1181 </config>
1182 """
1183 config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1184 config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4))
1185 else:
1186 config_vni_xml="""
1187 <config>
1188 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1189 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1190 <ofdpa10:id>VNID</ofdpa10:id>
1191 </ofdpa10:vni>
1192 </of11-config:capable-switch>
1193 </config>
1194 """
1195
1196 config_vni_xml=config_vni_xml.replace("VNID", str(vni_id))
1197 config_vni_xml=config_vni_xml.replace("OPERATION", str(operation))
1198 return config_vni_xml
1199
1200def get_featureReplay(self):
1201 req = ofp.message.features_request()
1202 res, raw = self.controller.transact(req)
1203 self.assertIsNotNone(res, "Did not receive a response from the DUT.")
1204 self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY,
1205 ("Unexpected packet type %d received in response to "
1206 "OFPT_FEATURES_REQUEST") % res.type)
1207 return res
1208
1209def send_edit_config(switch_ip, xml, target='runing'):
1210 NETCONF_ACCOUNT="netconfuser"
1211 NETCONF_PASSWD="netconfuser"
1212 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1213 try:
1214 m.edit_config(target='running',
1215 config=xml,
1216 default_operation='merge',
1217 error_option='stop-on-error')
1218
1219 except Exception as e:
1220 logging.info("Fail to set xml %s", xml)
1221 return False
1222
1223 #return m.get_config(source='running').data_xml
1224 return True
1225
1226def send_delete_config(switch_ip, xml, target='runing'):
1227 NETCONF_ACCOUNT="netconfuser"
1228 NETCONF_PASSWD="netconfuser"
1229 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1230 try:
1231 m.edit_config(target='running',
1232 config=xml,
1233 default_operation='delete',
1234 error_option='stop-on-error')
1235
1236 except Exception as e:
1237 logging.info("Fail to set xml %s", xml)
1238 return False
1239
1240 #return m.get_config(source='running').data_xml
1241 return True
1242
1243def get_edit_config(switch_ip, target='runing'):
1244 NETCONF_ACCOUNT="netconfuser"
1245 NETCONF_PASSWD="netconfuser"
1246 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 +08001247 return m.get_config(source='running').data_xml
macauleydbff3272015-07-30 14:07:16 +08001248
macauley_cheng67da9262015-08-31 15:18:41 +08001249
1250"""
1251MPLS
1252"""
1253
1254OFDPA_MPLS_SUBTYPE_SHIFT=24
1255OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1
1256OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2
1257OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3
1258OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4
1259OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5
1260OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6
1261OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8
1262OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10
1263
1264def encode_mpls_interface_group_id(subtype, index):
1265 index=index&0x00ffffff
1266 assert(subtype==0)
1267 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1268
1269def encode_mpls_label_group_id(subtype, index):
1270 index=index&0x00ffffff
1271 assert(subtype <=5 or subtype==0)
1272 #1: l2 vpn label
1273 #2: l3 vpn label
1274 #3: mpls tunnel label 1
1275 #4: mpls tunnel lable 2
1276 #5: mpls swap label
1277 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1278
1279def encode_mpls_forwarding_group_id(subtype, index):
1280 index=index&0x00ffffff
1281 assert(subtype==6 or subtype==8 or subtype==10)
1282 return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1283
1284
1285def add_mpls_intf_group(ctrl, ref_gid, dst_mac, src_mac, vid, index, subtype=0):
1286 action=[]
1287 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
1288 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
Flavio Castroaf2b4502016-02-02 17:41:32 -05001289 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001290 action.append(ofp.action.group(ref_gid))
1291
1292 buckets = [ofp.bucket(actions=action)]
1293
1294 mpls_group_id =encode_mpls_interface_group_id(subtype, index)
1295 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1296 group_id=mpls_group_id,
1297 buckets=buckets
1298 )
1299 ctrl.message_send(request)
1300 return mpls_group_id, request
1301
1302def add_mpls_label_group(ctrl, subtype, index, ref_gid,
1303 lmep_id=-1,
1304 qos_index=-1,
1305 push_l2_header=False,
1306 push_vlan=False,
1307 push_mpls_header=False,
1308 push_cw=False,
1309 set_mpls_label=None,
1310 set_bos=None,
1311 set_tc=None,
1312 set_tc_from_table=False,
1313 cpy_tc_outward=False,
1314 set_ttl=None,
1315 cpy_ttl_outward=False,
1316 oam_lm_tx_count=False,
1317 set_pri_from_table=False
1318 ):
1319 """
1320 @ref_gid: only can be mpls intf group or mpls tunnel label 1/2 group
1321 """
1322 action=[]
1323
1324 if push_vlan== True:
1325 action.append(ofp.action.push_vlan(0x8100))
1326 if push_mpls_header== True:
1327 action.append(ofp.action.push_mpls(0x8847))
1328 if set_mpls_label != None:
1329 action.append(ofp.action.set_field(ofp.oxm.mpls_label(set_mpls_label)))
1330 if set_bos != None:
1331 action.append(ofp.action.set_field(ofp.oxm.mpls_bos(set_bos)))
1332 if set_tc != None:
1333 assert(set_tc_from_table==False)
1334 action.append(ofp.action.set_field(ofp.oxm.mpls_tc(set_tc)))
1335 if set_ttl != None:
1336 action.append(ofp.action.set_mpls_ttl(set_ttl))
1337 if cpy_ttl_outward == True:
1338 action.append(ofp.action.copy_ttl_out())
1339 """
1340 ofdpa experimenter
1341 """
1342 if push_l2_header== True:
1343 action.append(ofp.action.ofdpa_push_l2_header())
1344 if set_tc_from_table== True:
1345 assert(qos_index>=0)
1346 assert(set_tc == None)
1347 action.append(ofp.action.ofdpa_set_tc_from_table(qos_index))
1348 if cpy_tc_outward == True:
1349 action.append(ofp.action.ofdpa_copy_tc_out())
1350 if oam_lm_tx_count == True:
1351 assert(qos_index>=0 and lmep_id>=0)
1352 action.append(ofp.action.ofdpa_oam_lm_tx_count(lmep_id, qos_index))
1353 if set_pri_from_table == True:
1354 assert(qos_index>=0)
1355 action.append(ofp.action.ofdpa_set_qos_from_table(qos_index))
1356 if push_cw == True:
1357 action.append(ofp.action.ofdpa_push_cw())
1358
1359 action.append(ofp.action.group(ref_gid))
1360 buckets = [ofp.bucket(actions=action)]
1361
1362 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1363 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1364 group_id=mpls_group_id,
1365 buckets=buckets
1366 )
1367 ctrl.message_send(request)
1368
1369 return mpls_group_id, request
1370
1371def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids,
1372 watch_port=None,
macauley_chengd17ce512015-08-31 17:45:51 +08001373 watch_group=ofp.OFPP_ANY,
macauley_cheng67da9262015-08-31 15:18:41 +08001374 push_vlan=None,
macauley_chengd17ce512015-08-31 17:45:51 +08001375 pop_vlan=None,
macauley_cheng67da9262015-08-31 15:18:41 +08001376 set_vid=None):
1377 assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP
1378 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP
1379 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG)
macauley_chengd17ce512015-08-31 17:45:51 +08001380
macauley_cheng67da9262015-08-31 15:18:41 +08001381 buckets=[]
1382 if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP:
macauley_chengd17ce512015-08-31 17:45:51 +08001383 group_type = ofp.OFPGT_FF
macauley_cheng67da9262015-08-31 15:18:41 +08001384 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001385 action=[]
1386 action.append(ofp.action.group(gid))
1387 buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action))
1388
macauley_cheng67da9262015-08-31 15:18:41 +08001389 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP:
1390 group_type = ofp.OFPGT_SELECT
1391 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001392 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08001393 action.append(ofp.action.group(gid))
1394 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001395
macauley_cheng67da9262015-08-31 15:18:41 +08001396 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG:
1397 group_type = ofp.OFPGT_INDIRECT
macauley_chengd17ce512015-08-31 17:45:51 +08001398 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08001399 if set_vid!=None:
Flavio Castro91d1a552016-05-17 16:59:44 -07001400 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+set_vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001401 if push_vlan!=None:
1402 action.append(ofp.action.push_vlan(push_vlan))
1403 if pop_vlan!=None:
macauley_chengd17ce512015-08-31 17:45:51 +08001404 action.append(ofp.action.pop_vlan())
macauley_cheng67da9262015-08-31 15:18:41 +08001405 action.append(ofp.action.group(ref_gids[0]))
1406 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001407
1408 mpls_group_id = encode_mpls_forwarding_group_id(subtype, index)
macauley_cheng67da9262015-08-31 15:18:41 +08001409 request = ofp.message.group_add(group_type=group_type,
macauley_cheng67da9262015-08-31 15:18:41 +08001410 group_id=mpls_group_id,
1411 buckets=buckets
1412 )
1413 ctrl.message_send(request)
1414 return mpls_group_id, request
macauley_chengd17ce512015-08-31 17:45:51 +08001415
1416
macauley_cheng67da9262015-08-31 15:18:41 +08001417"""
1418dislay
1419"""
macauleydbff3272015-07-30 14:07:16 +08001420def print_current_table_flow_stat(ctrl, table_id=0xff):
1421 stat_req=ofp.message.flow_stats_request()
1422 response, pkt = ctrl.transact(stat_req)
1423 if response == None:
1424 print "no response"
1425 return None
1426 print len(response.entries)
1427 for obj in response.entries:
1428 print "match ", obj.match
1429 print "cookie", obj.cookie
1430 print "priority", obj.priority
1431 print "idle_timeout", obj.idle_timeout
1432 print "hard_timeout", obj.hard_timeout
1433 #obj.actions
Flavio Castro167f5bd2015-12-02 19:33:53 -05001434 print "packet count: %lx"%obj.packet_count