blob: 2b14f60347ba349b4ab033e3ffe073af08cda0b4 [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
macauleyfddc4662015-07-27 17:40:30 +0800837 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):
847 actions.append(ofp.action.set_field(
848 ofp.oxm.exp2ByteValue(exp_type=23, value=32)))
849 actions.append(ofp.action.group(action_group_id))
850 else:
Flavio Castrod80fbc32016-07-25 15:54:26 -0700851 actions.append(ofp.action.set_field(
852 ofp.oxm.exp2ByteValue(exp_type=23, value=32)))
853 actions.append(ofp.action.group(action_group_id))
Flavio Castro8ca52542016-04-11 11:24:49 -0400854 actions.append(ofp.action.copy_ttl_in())
855
Flavio Castrob702a2f2016-04-10 22:01:48 -0400856 request = ofp.message.flow_add(
857 table_id=24,
858 cookie=43,
859 match=match,
860 instructions=[
Flavio Castro8ca52542016-04-11 11:24:49 -0400861 ofp.instruction.apply_actions(actions=actions),
862 ofp.instruction.goto_table(goto_table)
Flavio Castrob702a2f2016-04-10 22:01:48 -0400863 ],
864 buffer_id=ofp.OFP_NO_BUFFER,
865 priority=1)
Flavio Castrob702a2f2016-04-10 22:01:48 -0400866 logging.info("Inserting MPLS flow , label %ld", label)
867 ctrl.message_send(request)
868
869 if send_barrier:
870 do_barrier(ctrl)
871
872 return request
873
macauleyfddc4662015-07-27 17:40:30 +0800874def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False):
875 match = ofp.match()
876 match.oxm_list.append(ofp.oxm.eth_type(0x0800))
877 match.oxm_list.append(ofp.oxm.vlan_vid(vlan_id))
878 if src_ip_mask!=0:
879 match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask))
880 else:
881 match.oxm_list.append(ofp.oxm.ipv4_src(src_ip))
882
883 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
884
885 request = ofp.message.flow_add(
886 table_id=40,
887 cookie=42,
888 match=match,
889 instructions=[
890 ofp.instruction.write_actions(
891 actions=[ofp.action.group(action_group_id)]),
892 ofp.instruction.goto_table(60)
893 ],
894 buffer_id=ofp.OFP_NO_BUFFER,
895 priority=1)
896
897 logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask)
898 ctrl.message_send(request)
899
900 if send_barrier:
901 do_barrier(ctrl)
902
903 return request
macauley_cheng6b133662015-11-09 13:52:39 +0800904
905#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 +0800906def 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 +0800907 match = ofp.match()
908 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
909 match.oxm_list.append(ofp.oxm.ipv4_dst(ip_dst))
910 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
911 match.oxm_list.append(ofp.oxm.tcp_dst(tcp_dst))
912
913 request = ofp.message.flow_add(
914 table_id=28,
915 cookie=42,
916 match=match,
917 instructions=[
918 ofp.instruction.write_actions(
919 actions=[ofp.action.set_field(ofp.oxm.ipv4_dst(set_ip_dst)),
920 ofp.action.set_field(ofp.oxm.tcp_dst(set_tcp_dst)),
921 ofp.action.group(action_group_id)]),
922 ofp.instruction.goto_table(60)
923 ],
924 buffer_id=ofp.OFP_NO_BUFFER,
925 priority=1)
926 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)
927 ctrl.message_send(request)
928 return request
macauley_chengeffc20a2015-11-09 16:14:56 +0800929
930#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
931def add_snat_flow(ctrl, eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src):
932 match = ofp.match()
933 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
934 match.oxm_list.append(ofp.oxm.ipv4_src(ip_src))
935 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
936 match.oxm_list.append(ofp.oxm.tcp_src(tcp_src))
937
938 request = ofp.message.flow_add(
939 table_id=29,
940 cookie=42,
941 match=match,
942 instructions=[
943 ofp.instruction.write_actions(
944 actions=[ofp.action.set_field(ofp.oxm.ipv4_src(set_ip_src)),
945 ofp.action.set_field(ofp.oxm.tcp_src(set_tcp_src))]),
946 ofp.instruction.goto_table(30)
947 ],
948 buffer_id=ofp.OFP_NO_BUFFER,
949 priority=1)
950 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)
951 ctrl.message_send(request)
952 return request
macauley_cheng6b133662015-11-09 13:52:39 +0800953
macauleyfddc4662015-07-27 17:40:30 +0800954def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'):
955 """
956 Command Example:
957 of-agent vtap 10001 ethernet 1/1 vid 1
958 of-agent vtp 10001 vni 10
959 """
960 if vlan != 0:
961 config_vtap_xml="""
962 <config>
963 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
964 <id>capable-switch-1</id>
965 <resources>
966 <port xc:operation="OPERATION">
967 <resource-id >LPORT</resource-id>
968 <features>
969 <current>
970 <rate>10Gb</rate>
971 <medium>fiber</medium>
972 <pause>symmetric</pause>
973 </current>
974 <advertised>
975 <rate>10Gb</rate>
976 <rate>100Gb</rate>
977 <medium>fiber</medium>
978 <pause>symmetric</pause>
979 </advertised>
980 <supported>
981 <rate>10Gb</rate>
982 <rate>100Gb</rate>
983 <medium>fiber</medium>
984 <pause>symmetric</pause>
985 </supported>
986 <advertised-peer>
987 <rate>10Gb</rate>
988 <rate>100Gb</rate>
989 <medium>fiber</medium>
990 <pause>symmetric</pause>
991 </advertised-peer>
992 </features>
993 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
994 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
995 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
996 <ofdpa10:vni>VNID</ofdpa10:vni>
997 </ofdpa10:vtap>
998 </port>
999 </resources>
1000 <logical-switches>
1001 <switch>
1002 <id>DATAPATH_ID</id>
1003 <datapath-id>DATAPATH_ID</datapath-id>
1004 <resources>
1005 <port xc:operation="OPERATION">LPORT</port>
1006 </resources>
1007 </switch>
1008 </logical-switches>
1009 </capable-switch>
1010 </config>
1011 """
1012 else:
1013 config_vtap_xml="""
1014 <config>
1015 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1016 <id>capable-switch-1</id>
1017 <resources>
1018 <port xc:operation="OPERATION">
1019 <resource-id >LPORT</resource-id>
1020 <features>
1021 <current>
1022 <rate>10Gb</rate>
1023 <medium>fiber</medium>
1024 <pause>symmetric</pause>
1025 </current>
1026 <advertised>
1027 <rate>10Gb</rate>
1028 <rate>100Gb</rate>
1029 <medium>fiber</medium>
1030 <pause>symmetric</pause>
1031 </advertised>
1032 <supported>
1033 <rate>10Gb</rate>
1034 <rate>100Gb</rate>
1035 <medium>fiber</medium>
1036 <pause>symmetric</pause>
1037 </supported>
1038 <advertised-peer>
1039 <rate>10Gb</rate>
1040 <rate>100Gb</rate>
1041 <medium>fiber</medium>
1042 <pause>symmetric</pause>
1043 </advertised-peer>
1044 </features>
1045 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1046 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1047 <ofdpa10:vni>VNID</ofdpa10:vni>
1048 </ofdpa10:vtap>
1049 </port>
1050 </resources>
1051 <logical-switches>
1052 <switch>
1053 <id>DATAPATH_ID</id>
1054 <datapath-id>DATAPATH_ID</datapath-id>
1055 <resources>
1056 <port xc:operation="OPERATION">LPORT</port>
1057 </resources>
1058 </switch>
1059 </logical-switches>
1060 </capable-switch>
1061 </config>
1062 """
1063 str_datapath_id_f= "{:016x}".format(dp_id)
1064 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1065 config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +08001066 config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport)))
macauleyfddc4662015-07-27 17:40:30 +08001067 config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port))
1068 config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan))
1069 config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid))
1070 config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation))
1071 return config_vtap_xml
1072
1073def get_vtep_lport_config_xml(dp_id, lport, src_ip, dst_ip, next_hop_id, vnid, udp_src_port=6633, ttl=25, operation='merge'):
1074 """
1075 Command Example:
1076 of-agent vtep 10002 source user-input-src-ip destination user-input-dst-ip udp-source-port 6633 nexthop 2 ttl 25
1077 of-agent vtp 10001 vni 10
1078 """
1079
1080 config_vtep_xml="""
1081 <config>
1082 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1083 <id>capable-switch-1</id>
1084 <resources>
1085 <port xc:operation="OPERATION">
1086 <resource-id>LPORT</resource-id>
1087 <features>
1088 <current>
1089 <rate>10Gb</rate>
1090 <medium>fiber</medium>
1091 <pause>symmetric</pause>
1092 </current>
1093 <advertised>
1094 <rate>10Gb</rate>
1095 <rate>100Gb</rate>
1096 <medium>fiber</medium>
1097 <pause>symmetric</pause>
1098 </advertised>
1099 <supported>
1100 <rate>10Gb</rate>
1101 <rate>100Gb</rate>
1102 <medium>fiber</medium>
1103 <pause>symmetric</pause>
1104 </supported>
1105 <advertised-peer>
1106 <rate>10Gb</rate>
1107 <rate>100Gb</rate>
1108 <medium>fiber</medium>
1109 <pause>symmetric</pause>
1110 </advertised-peer>
1111 </features>
1112 <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01">
1113 <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip>
1114 <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip>
1115 <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port>
macauley25999cf2015-08-07 17:03:24 +08001116 <ofdpa10:vni xc:operation="OPERATION">
1117 <ofdpa10:id>VNID</ofdpa10:id>
1118 </ofdpa10:vni>
macauleyfddc4662015-07-27 17:40:30 +08001119 <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id>
1120 <ofdpa10:ttl>TTL</ofdpa10:ttl>
1121 </ofdpa10:vtep>
1122 </port>
1123 </resources>
1124 <logical-switches>
1125 <switch>
1126 <id>DATAPATH_ID</id>
1127 <datapath-id>DATAPATH_ID</datapath-id>
1128 <resources>
1129 <port xc:operation="OPERATION">LPORT</port>
1130 </resources>
1131 </switch>
1132 </logical-switches>
1133 </capable-switch>
1134 </config>
1135 """
1136 str_datapath_id_f= "{:016x}".format(dp_id)
1137 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1138 config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +08001139 config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport)))
macauleyfddc4662015-07-27 17:40:30 +08001140 config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip))
1141 config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip))
1142 config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port))
1143 config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1144 config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl))
1145 config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid))
1146 config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation))
1147
1148 return config_vtep_xml
1149
1150def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'):
1151 #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2
1152 config_nexthop_xml="""
1153 <config>
1154 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1155 <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1156 <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id>
1157 <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac>
1158 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1159 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1160 </ofdpa10:next-hop>
1161 </of11-config:capable-switch>
1162 </config>
1163 """
1164 config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan))
1165 config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port))
1166 config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1167 config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac))
1168 config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation))
1169 return config_nexthop_xml
1170
1171def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'):
1172 #of-agent vni 10 multicast 224.1.1.1 nexthop 20
1173 if mcast_ipv4!=None:
1174 config_vni_xml="""
1175 <config>
1176 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1177 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1178 <ofdpa10:id>VNID</ofdpa10:id>
1179 <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group>
1180 <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id>
1181 </ofdpa10:vni>
1182 </of11-config:capable-switch>
1183 </config>
1184 """
1185 config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1186 config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4))
1187 else:
1188 config_vni_xml="""
1189 <config>
1190 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1191 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1192 <ofdpa10:id>VNID</ofdpa10:id>
1193 </ofdpa10:vni>
1194 </of11-config:capable-switch>
1195 </config>
1196 """
1197
1198 config_vni_xml=config_vni_xml.replace("VNID", str(vni_id))
1199 config_vni_xml=config_vni_xml.replace("OPERATION", str(operation))
1200 return config_vni_xml
1201
1202def get_featureReplay(self):
1203 req = ofp.message.features_request()
1204 res, raw = self.controller.transact(req)
1205 self.assertIsNotNone(res, "Did not receive a response from the DUT.")
1206 self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY,
1207 ("Unexpected packet type %d received in response to "
1208 "OFPT_FEATURES_REQUEST") % res.type)
1209 return res
1210
1211def send_edit_config(switch_ip, xml, target='runing'):
1212 NETCONF_ACCOUNT="netconfuser"
1213 NETCONF_PASSWD="netconfuser"
1214 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1215 try:
1216 m.edit_config(target='running',
1217 config=xml,
1218 default_operation='merge',
1219 error_option='stop-on-error')
1220
1221 except Exception as e:
1222 logging.info("Fail to set xml %s", xml)
1223 return False
1224
1225 #return m.get_config(source='running').data_xml
1226 return True
1227
1228def send_delete_config(switch_ip, xml, target='runing'):
1229 NETCONF_ACCOUNT="netconfuser"
1230 NETCONF_PASSWD="netconfuser"
1231 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1232 try:
1233 m.edit_config(target='running',
1234 config=xml,
1235 default_operation='delete',
1236 error_option='stop-on-error')
1237
1238 except Exception as e:
1239 logging.info("Fail to set xml %s", xml)
1240 return False
1241
1242 #return m.get_config(source='running').data_xml
1243 return True
1244
1245def get_edit_config(switch_ip, target='runing'):
1246 NETCONF_ACCOUNT="netconfuser"
1247 NETCONF_PASSWD="netconfuser"
1248 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 +08001249 return m.get_config(source='running').data_xml
macauleydbff3272015-07-30 14:07:16 +08001250
macauley_cheng67da9262015-08-31 15:18:41 +08001251
1252"""
1253MPLS
1254"""
1255
1256OFDPA_MPLS_SUBTYPE_SHIFT=24
1257OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1
1258OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2
1259OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3
1260OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4
1261OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5
1262OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6
1263OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8
1264OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10
1265
1266def encode_mpls_interface_group_id(subtype, index):
1267 index=index&0x00ffffff
1268 assert(subtype==0)
1269 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1270
1271def encode_mpls_label_group_id(subtype, index):
1272 index=index&0x00ffffff
1273 assert(subtype <=5 or subtype==0)
1274 #1: l2 vpn label
1275 #2: l3 vpn label
1276 #3: mpls tunnel label 1
1277 #4: mpls tunnel lable 2
1278 #5: mpls swap label
1279 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1280
1281def encode_mpls_forwarding_group_id(subtype, index):
1282 index=index&0x00ffffff
1283 assert(subtype==6 or subtype==8 or subtype==10)
1284 return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1285
1286
1287def add_mpls_intf_group(ctrl, ref_gid, dst_mac, src_mac, vid, index, subtype=0):
1288 action=[]
1289 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
1290 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
Flavio Castroaf2b4502016-02-02 17:41:32 -05001291 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001292 action.append(ofp.action.group(ref_gid))
1293
1294 buckets = [ofp.bucket(actions=action)]
1295
1296 mpls_group_id =encode_mpls_interface_group_id(subtype, index)
1297 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1298 group_id=mpls_group_id,
1299 buckets=buckets
1300 )
1301 ctrl.message_send(request)
1302 return mpls_group_id, request
1303
1304def add_mpls_label_group(ctrl, subtype, index, ref_gid,
1305 lmep_id=-1,
1306 qos_index=-1,
1307 push_l2_header=False,
1308 push_vlan=False,
1309 push_mpls_header=False,
1310 push_cw=False,
1311 set_mpls_label=None,
1312 set_bos=None,
1313 set_tc=None,
1314 set_tc_from_table=False,
1315 cpy_tc_outward=False,
1316 set_ttl=None,
1317 cpy_ttl_outward=False,
1318 oam_lm_tx_count=False,
1319 set_pri_from_table=False
1320 ):
1321 """
1322 @ref_gid: only can be mpls intf group or mpls tunnel label 1/2 group
1323 """
1324 action=[]
1325
1326 if push_vlan== True:
1327 action.append(ofp.action.push_vlan(0x8100))
1328 if push_mpls_header== True:
1329 action.append(ofp.action.push_mpls(0x8847))
1330 if set_mpls_label != None:
1331 action.append(ofp.action.set_field(ofp.oxm.mpls_label(set_mpls_label)))
1332 if set_bos != None:
1333 action.append(ofp.action.set_field(ofp.oxm.mpls_bos(set_bos)))
1334 if set_tc != None:
1335 assert(set_tc_from_table==False)
1336 action.append(ofp.action.set_field(ofp.oxm.mpls_tc(set_tc)))
1337 if set_ttl != None:
1338 action.append(ofp.action.set_mpls_ttl(set_ttl))
1339 if cpy_ttl_outward == True:
1340 action.append(ofp.action.copy_ttl_out())
1341 """
1342 ofdpa experimenter
1343 """
1344 if push_l2_header== True:
1345 action.append(ofp.action.ofdpa_push_l2_header())
1346 if set_tc_from_table== True:
1347 assert(qos_index>=0)
1348 assert(set_tc == None)
1349 action.append(ofp.action.ofdpa_set_tc_from_table(qos_index))
1350 if cpy_tc_outward == True:
1351 action.append(ofp.action.ofdpa_copy_tc_out())
1352 if oam_lm_tx_count == True:
1353 assert(qos_index>=0 and lmep_id>=0)
1354 action.append(ofp.action.ofdpa_oam_lm_tx_count(lmep_id, qos_index))
1355 if set_pri_from_table == True:
1356 assert(qos_index>=0)
1357 action.append(ofp.action.ofdpa_set_qos_from_table(qos_index))
1358 if push_cw == True:
1359 action.append(ofp.action.ofdpa_push_cw())
1360
1361 action.append(ofp.action.group(ref_gid))
1362 buckets = [ofp.bucket(actions=action)]
1363
1364 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1365 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1366 group_id=mpls_group_id,
1367 buckets=buckets
1368 )
1369 ctrl.message_send(request)
1370
1371 return mpls_group_id, request
1372
1373def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids,
1374 watch_port=None,
macauley_chengd17ce512015-08-31 17:45:51 +08001375 watch_group=ofp.OFPP_ANY,
macauley_cheng67da9262015-08-31 15:18:41 +08001376 push_vlan=None,
macauley_chengd17ce512015-08-31 17:45:51 +08001377 pop_vlan=None,
macauley_cheng67da9262015-08-31 15:18:41 +08001378 set_vid=None):
1379 assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP
1380 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP
1381 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG)
macauley_chengd17ce512015-08-31 17:45:51 +08001382
macauley_cheng67da9262015-08-31 15:18:41 +08001383 buckets=[]
1384 if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP:
macauley_chengd17ce512015-08-31 17:45:51 +08001385 group_type = ofp.OFPGT_FF
macauley_cheng67da9262015-08-31 15:18:41 +08001386 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001387 action=[]
1388 action.append(ofp.action.group(gid))
1389 buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action))
1390
macauley_cheng67da9262015-08-31 15:18:41 +08001391 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP:
1392 group_type = ofp.OFPGT_SELECT
1393 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001394 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08001395 action.append(ofp.action.group(gid))
1396 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001397
macauley_cheng67da9262015-08-31 15:18:41 +08001398 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG:
1399 group_type = ofp.OFPGT_INDIRECT
macauley_chengd17ce512015-08-31 17:45:51 +08001400 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08001401 if set_vid!=None:
Flavio Castro91d1a552016-05-17 16:59:44 -07001402 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+set_vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001403 if push_vlan!=None:
1404 action.append(ofp.action.push_vlan(push_vlan))
1405 if pop_vlan!=None:
macauley_chengd17ce512015-08-31 17:45:51 +08001406 action.append(ofp.action.pop_vlan())
macauley_cheng67da9262015-08-31 15:18:41 +08001407 action.append(ofp.action.group(ref_gids[0]))
1408 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001409
1410 mpls_group_id = encode_mpls_forwarding_group_id(subtype, index)
macauley_cheng67da9262015-08-31 15:18:41 +08001411 request = ofp.message.group_add(group_type=group_type,
macauley_cheng67da9262015-08-31 15:18:41 +08001412 group_id=mpls_group_id,
1413 buckets=buckets
1414 )
1415 ctrl.message_send(request)
1416 return mpls_group_id, request
macauley_chengd17ce512015-08-31 17:45:51 +08001417
1418
macauley_cheng67da9262015-08-31 15:18:41 +08001419"""
1420dislay
1421"""
macauleydbff3272015-07-30 14:07:16 +08001422def print_current_table_flow_stat(ctrl, table_id=0xff):
1423 stat_req=ofp.message.flow_stats_request()
1424 response, pkt = ctrl.transact(stat_req)
1425 if response == None:
1426 print "no response"
1427 return None
1428 print len(response.entries)
1429 for obj in response.entries:
1430 print "match ", obj.match
1431 print "cookie", obj.cookie
1432 print "priority", obj.priority
1433 print "idle_timeout", obj.idle_timeout
1434 print "hard_timeout", obj.hard_timeout
1435 #obj.actions
Flavio Castro167f5bd2015-12-02 19:33:53 -05001436 print "packet count: %lx"%obj.packet_count