blob: 338ab919730983f04dddfeee9251dabbdb136ae2 [file] [log] [blame]
Flavio Castro96646c62015-11-16 15:05:43 -05001import logging
2
3from oftest import config
4import oftest.base_tests as base_tests
5import ofp
6import time
7from oftest.testutils import *
8
9from ncclient import manager
10import ncclient
11
12OFDPA_GROUP_TYPE_SHIFT=28
13OFDPA_VLAN_ID_SHIFT =16
14OFDPA_TUNNEL_ID_SHIFT =12
15OFDPA_TUNNEL_SUBTYPE_SHIFT=10
16
17#VLAN_TABLE_FLAGS
18VLAN_TABLE_FLAG_ONLY_UNTAG=1
19VLAN_TABLE_FLAG_ONLY_TAG =2
20VLAN_TABLE_FLAG_ONLY_BOTH =3
21
22PORT_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
43def 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
51
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)
61
62 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
101def 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
125def encode_l2_overlay_group_id(tunnel_id, subtype, index):
126 tunnel_id=tunnel_id&0xffff #16 bits
127 subtype = subtype&3 #2 bits
128 index = index & 0x3f #10 bits
129 return index + (tunnel_id << OFDPA_TUNNEL_ID_SHIFT)+ (subtype<<OFDPA_TUNNEL_SUBTYPE_SHIFT)+(8 << OFDPA_GROUP_TYPE_SHIFT)
130
131def add_l2_interface_grouop(ctrl, ports, vlan_id=1, is_tagged=False, send_barrier=False):
132 # group table
133 # set up untag groups for each port
134 group_id_list=[]
135 msgs=[]
136 for of_port in ports:
137 # do stuff
138 group_id = encode_l2_interface_group_id(vlan_id, of_port)
139 group_id_list.append(group_id)
140 if is_tagged:
141 actions = [
142 ofp.action.output(of_port),
143 ]
144 else:
145 actions = [
146 ofp.action.pop_vlan(),
147 ofp.action.output(of_port),
148 ]
149
150 buckets = [
151 ofp.bucket(actions=actions),
152 ]
153
154 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
155 group_id=group_id,
156 buckets=buckets
157 )
158 ctrl.message_send(request)
159 msgs.append(request)
160
161 if send_barrier:
162 do_barrier(ctrl)
163
164 return group_id_list, msgs
165
castroflaviodd171472015-12-08 13:55:58 -0500166def add_one_l2_interface_group(ctrl, port, vlan_id=1, is_tagged=False, send_barrier=False):
Flavio Castro96646c62015-11-16 15:05:43 -0500167 # group table
168 # set up untag groups for each port
169 group_id = encode_l2_interface_group_id(vlan_id, port)
170
171 if is_tagged:
172 actions = [
173 ofp.action.output(port),
174 ]
175 else:
176 actions = [
177 ofp.action.pop_vlan(),
178 ofp.action.output(port),
179 ]
180
181 buckets = [
182 ofp.bucket(actions=actions),
183 ]
184
185 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
186 group_id=group_id,
187 buckets=buckets
188 )
189 ctrl.message_send(request)
190
191 if send_barrier:
192 do_barrier(ctrl)
193
194 return group_id, request
195
196def add_l2_mcast_group(ctrl, ports, vlanid, mcast_grp_index):
197 buckets=[]
198 for of_port in ports:
199 group_id = encode_l2_interface_group_id(vlanid, of_port)
200 action=[ofp.action.group(group_id)]
201 buckets.append(ofp.bucket(actions=action))
202
203 group_id =encode_l2_mcast_group_id(vlanid, mcast_grp_index)
204 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
205 group_id=group_id,
206 buckets=buckets
207 )
208 ctrl.message_send(request)
209 return request
210
211def add_l2_flood_group(ctrl, ports, vlanid, id):
212 buckets=[]
213 for of_port in ports:
214 group_id = encode_l2_interface_group_id(vlanid, of_port)
215 action=[ofp.action.group(group_id)]
216 buckets.append(ofp.bucket(actions=action))
217
218 group_id =encode_l2_flood_group_id(vlanid, id)
219 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
220 group_id=group_id,
221 buckets=buckets
222 )
223 ctrl.message_send(request)
224 return request
225
226def add_l2_rewrite_group(ctrl, port, vlanid, id, src_mac, dst_mac):
227 group_id = encode_l2_interface_group_id(vlanid, port)
228
229 action=[]
230 if src_mac is not None:
231 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
232
233 if dst_mac is not None:
234 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
235
236 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vlanid)))
237
238 action.append(ofp.action.group(group_id))
239
240 buckets = [ofp.bucket(actions=action)]
241
242 group_id =encode_l2_rewrite_group_id(id)
243 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
244 group_id=group_id,
245 buckets=buckets
246 )
247 ctrl.message_send(request)
248 return request
249
250def add_l3_unicast_group(ctrl, port, vlanid, id, src_mac, dst_mac):
251 group_id = encode_l2_interface_group_id(vlanid, port)
252
253 action=[]
254 if src_mac is not None:
255 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
256
257 if dst_mac is not None:
258 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
259
260 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vlanid)))
261
262 action.append(ofp.action.group(group_id))
263
264 buckets = [ofp.bucket(actions=action)]
265
266 group_id =encode_l3_unicast_group_id(id)
267 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
268 group_id=group_id,
269 buckets=buckets
270 )
271 ctrl.message_send(request)
272 return request
273
274def add_l3_interface_group(ctrl, port, vlanid, id, src_mac):
275 group_id = encode_l2_interface_group_id(vlanid, port)
276
277 action=[]
278 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
279 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vlanid)))
280 action.append(ofp.action.group(group_id))
281
282 buckets = [ofp.bucket(actions=action)]
283
284 group_id =encode_l3_interface_group_id(id)
285 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
286 group_id=group_id,
287 buckets=buckets
288 )
289 ctrl.message_send(request)
290 return request
291
292def add_l3_ecmp_group(ctrl, id, l3_ucast_groups):
293 buckets=[]
294 for group in l3_ucast_groups:
295 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
296
297 group_id =encode_l3_ecmp_group_id(id)
298 request = ofp.message.group_add(group_type=ofp.OFPGT_SELECT,
299 group_id=group_id,
300 buckets=buckets
301 )
302 ctrl.message_send(request)
303 return request
304
305def add_l3_mcast_group(ctrl, vid, mcast_group_id, groups_on_buckets):
306 buckets=[]
307 for group in groups_on_buckets:
308 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
309
310 group_id =encode_l3_mcast_group_id(vid, mcast_group_id)
311 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
312 group_id=group_id,
313 buckets=buckets
314 )
315 ctrl.message_send(request)
316 return request
317
318def add_l2_overlay_flood_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
319 buckets=[]
320 for port in ports:
321 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
322
323 group_id=encode_l2_overlay_group_id(tunnel_id, 0, index)
324 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
325 group_id=group_id,
326 buckets=buckets
327 )
328 ctrl.message_send(request)
329 return request
330
331def add_l2_overlay_flood_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
332 buckets=[]
333 for port in ports:
334 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
335
336 group_id=encode_l2_overlay_group_id(tunnel_id, 1, index)
337 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
338 group_id=group_id,
339 buckets=buckets
340 )
341 ctrl.message_send(request)
342 return request
343
344def add_l2_overlay_mcast_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
345 buckets=[]
346 for port in ports:
347 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
348
349 group_id=encode_l2_overlay_group_id(tunnel_id, 2, index)
350 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
351 group_id=group_id,
352 buckets=buckets
353 )
354 ctrl.message_send(request)
355 return request
356
357def add_l2_overlay_mcast_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
358 buckets=[]
359 for port in ports:
360 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
361
362 group_id=encode_l2_overlay_group_id(tunnel_id, 3, index)
363 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
364 group_id=group_id,
365 buckets=buckets
366 )
367 ctrl.message_send(request)
368 return request
369
370def add_port_table_flow(ctrl, is_overlay=True):
371 match = ofp.match()
372
373 if is_overlay == True:
374 match.oxm_list.append(ofp.oxm.in_port(0x10000))
375 NEXT_TABLE=50
376 else:
377 match.oxm_list.append(ofp.oxm.in_port(0))
378 NEXT_TABLE=10
379
380 request = ofp.message.flow_add(
381 table_id=0,
382 cookie=42,
383 match=match,
384 instructions=[
385 ofp.instruction.goto_table(NEXT_TABLE)
386 ],
387 priority=0)
388 logging.info("Add port table, match port %lx" % 0x10000)
389 ctrl.message_send(request)
390
Flavio Castroe0b46012015-12-02 14:42:27 -0500391def pop_vlan_flow(ctrl, ports, vlan_id=1):
392 msgs=[]
393 for of_port in ports:
394 match = ofp.match()
395 match.oxm_list.append(ofp.oxm.in_port(of_port))
396 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
397 request = ofp.message.flow_add(
398 table_id=10,
399 cookie=42,
400 match=match,
401 instructions=[
402 ofp.instruction.apply_actions(
403 actions=[
404 ofp.action.pop_vlan()
405 ]
406 ),
407 ofp.instruction.goto_table(11)
408 ],
409 priority=0)
410 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
411 ctrl.message_send(request)
412
413 return msgs
Flavio Castro96646c62015-11-16 15:05:43 -0500414
415def add_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
416 # table 10: vlan
417 # goto to table 20
418 msgs=[]
419 for of_port in ports:
420 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
421 match = ofp.match()
422 match.oxm_list.append(ofp.oxm.in_port(of_port))
423 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
Flavio Castro34352e72015-12-07 20:01:51 -0500424 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id)))
Flavio Castro96646c62015-11-16 15:05:43 -0500425 request = ofp.message.flow_add(
426 table_id=10,
427 cookie=42,
428 match=match,
429 instructions=[
Flavio Castro34352e72015-12-07 20:01:51 -0500430 ofp.instruction.apply_actions(
431 actions=actions
432 ),
Flavio Castro96646c62015-11-16 15:05:43 -0500433 ofp.instruction.goto_table(20)
434 ],
435 priority=0)
436 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
437 ctrl.message_send(request)
438
439 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
440 match = ofp.match()
441 match.oxm_list.append(ofp.oxm.in_port(of_port))
442 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0xfff))
443 request = ofp.message.flow_add(
444 table_id=10,
445 cookie=42,
446 match=match,
447 instructions=[
448 ofp.instruction.apply_actions(
449 actions=[
450 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
451 ]
452 ),
453 ofp.instruction.goto_table(20)
454 ],
455 priority=0)
456 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
457 ctrl.message_send(request)
458 msgs.append(request)
459
460 if send_barrier:
461 do_barrier(ctrl)
462
463 return msgs
464
465def add_vlan_table_flow_pvid(ctrl, in_port, match_vid=None, pvid=1, send_barrier=False):
466 """it will tag pack as untagged packet wether it has tagg or not"""
467 match = ofp.match()
468 match.oxm_list.append(ofp.oxm.in_port(in_port))
469 actions=[]
470 if match_vid == None:
471 match.oxm_list.append(ofp.oxm.vlan_vid(0))
472 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
473 goto_table=20
474 else:
475 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+match_vid, 0x1fff))
476 actions.append(ofp.action.push_vlan(0x8100))
477 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
478 goto_table=20
479
480 request = ofp.message.flow_add(
481 table_id=10,
482 cookie=42,
483 match=match,
484 instructions=[
485 ofp.instruction.apply_actions(actions=actions)
486 ,ofp.instruction.goto_table(goto_table)
487 ],
488 priority=0)
489 logging.info("Add PVID %d on port %d and go to table %ld" %( pvid, in_port, goto_table))
490 ctrl.message_send(request)
491
492 if send_barrier:
493 do_barrier(ctrl)
494
495def add_vlan_table_flow_allow_all_vlan(ctrl, in_port, send_barrier=False):
496 """it st flow allow all vlan tag on this port"""
497 match = ofp.match()
498 match.oxm_list.append(ofp.oxm.in_port(in_port))
499 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1000))
500 request = ofp.message.flow_add(
501 table_id=10,
502 cookie=42,
503 match=match,
504 instructions=[
505 ofp.instruction.goto_table(20)
506 ],
507 priority=0)
508 logging.info("Add allow all vlan on port %d " %(in_port))
509 ctrl.message_send(request)
510
511
512def add_one_vlan_table_flow(ctrl, of_port, vlan_id=1, vrf=0, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
513 # table 10: vlan
514 # goto to table 20
515 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
516 match = ofp.match()
517 match.oxm_list.append(ofp.oxm.in_port(of_port))
518 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
519
520 actions=[]
521 if vrf!=0:
522 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
523
524 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id)))
525
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=actions
533 ),
534 ofp.instruction.goto_table(20)
535 ],
536 priority=0)
537 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
538 ctrl.message_send(request)
539
540 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
541 match = ofp.match()
542 match.oxm_list.append(ofp.oxm.in_port(of_port))
543 match.oxm_list.append(ofp.oxm.vlan_vid(0))
544
545 actions=[]
546 if vrf!=0:
547 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
548
549 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
550
551 request = ofp.message.flow_add(
552 table_id=10,
553 cookie=42,
554 match=match,
555 instructions=[
556 ofp.instruction.apply_actions(
557 actions=actions
558 ),
559 ofp.instruction.goto_table(20)
560 ],
561 priority=0)
562 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
563 ctrl.message_send(request)
564
565 if send_barrier:
566 do_barrier(ctrl)
567
568 return request
569
570def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False):
571 match = ofp.match()
castroflavio046c1fe2015-12-08 14:33:32 -0500572 priority=1000
Flavio Castro96646c62015-11-16 15:05:43 -0500573 if dst_mac!=None:
574 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
castroflavio046c1fe2015-12-08 14:33:32 -0500575 priority=2000
Flavio Castro96646c62015-11-16 15:05:43 -0500576
577 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
578
579 request = ofp.message.flow_add(
580 table_id=50,
581 cookie=42,
582 match=match,
583 instructions=[
584 ofp.instruction.write_actions(
585 actions=[
586 ofp.action.group(group_id)]),
587 ofp.instruction.goto_table(60)
588 ],
589 buffer_id=ofp.OFP_NO_BUFFER,
castroflavio046c1fe2015-12-08 14:33:32 -0500590 priority=priority)
Flavio Castro96646c62015-11-16 15:05:43 -0500591
592 logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac)
593 ctrl.message_send(request)
594
595 if send_barrier:
596 do_barrier(ctrl)
597
598 return request
599
600def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False):
601 match = ofp.match()
602 if dst_mac!=None:
603 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
604
605 match.oxm_list.append(ofp.oxm.tunnel_id(vnid))
606 if is_group == True:
607 actions=[ofp.action.group(group_id)]
608 else:
609 actions=[ofp.action.output(group_id)]
610
611 request = ofp.message.flow_add(
612 table_id=50,
613 cookie=42,
614 match=match,
615 instructions=[
616 ofp.instruction.write_actions(
617 actions=actions),
618 ofp.instruction.goto_table(60)
619 ],
620 buffer_id=ofp.OFP_NO_BUFFER,
621 priority=1000)
622
623 logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac)
624 ctrl.message_send(request)
625
626 if send_barrier:
627 do_barrier(ctrl)
628
629 return request
630
631def add_termination_flow(ctrl, in_port, eth_type, dst_mac, vlanid, goto_table=None, send_barrier=False):
632 match = ofp.match()
633 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
634 if dst_mac[0]&0x01 == 0x01:
635 match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00]))
636 goto_table=40
637 else:
638 if in_port!=0:
639 match.oxm_list.append(ofp.oxm.in_port(in_port))
640 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
641 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
642 if goto_table == None:
643 goto_table=30
644
645 request = ofp.message.flow_add(
646 table_id=20,
647 cookie=42,
648 match=match,
649 instructions=[
650 ofp.instruction.goto_table(goto_table)
651 ],
652 buffer_id=ofp.OFP_NO_BUFFER,
653 priority=1)
654
655 logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac)
656 ctrl.message_send(request)
657
658 if send_barrier:
659 do_barrier(ctrl)
660
661 return request
662
663def add_unicast_routing_flow(ctrl, eth_type, dst_ip, mask, action_group_id, vrf=0, send_barrier=False):
664 match = ofp.match()
665 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
666 if vrf != 0:
667 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
668
669 if mask!=0:
670 match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask))
671 else:
672 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
673
674
675 request = ofp.message.flow_add(
676 table_id=30,
677 cookie=42,
678 match=match,
679 instructions=[
680 ofp.instruction.write_actions(
681 actions=[ofp.action.group(action_group_id)]),
682 ofp.instruction.goto_table(60)
683 ],
684 buffer_id=ofp.OFP_NO_BUFFER,
685 priority=1)
686
687 logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip)
688 ctrl.message_send(request)
689
690 if send_barrier:
691 do_barrier(ctrl)
692
693 return request
694
695def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False):
696 match = ofp.match()
697 match.oxm_list.append(ofp.oxm.eth_type(0x0800))
698 match.oxm_list.append(ofp.oxm.vlan_vid(vlan_id))
699 if src_ip_mask!=0:
700 match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask))
701 else:
702 match.oxm_list.append(ofp.oxm.ipv4_src(src_ip))
703
704 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
705
706 request = ofp.message.flow_add(
707 table_id=40,
708 cookie=42,
709 match=match,
710 instructions=[
711 ofp.instruction.write_actions(
712 actions=[ofp.action.group(action_group_id)]),
713 ofp.instruction.goto_table(60)
714 ],
715 buffer_id=ofp.OFP_NO_BUFFER,
716 priority=1)
717
718 logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask)
719 ctrl.message_send(request)
720
721 if send_barrier:
722 do_barrier(ctrl)
723
724 return request
725
726#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
727def add_dnat_flow(ctrl, eth_type, ip_dst, ip_proto, tcp_dst, set_ip_dst, set_tcp_dst, action_group_id):
728 match = ofp.match()
729 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
730 match.oxm_list.append(ofp.oxm.ipv4_dst(ip_dst))
731 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
732 match.oxm_list.append(ofp.oxm.tcp_dst(tcp_dst))
733
734 request = ofp.message.flow_add(
735 table_id=28,
736 cookie=42,
737 match=match,
738 instructions=[
739 ofp.instruction.write_actions(
740 actions=[ofp.action.set_field(ofp.oxm.ipv4_dst(set_ip_dst)),
741 ofp.action.set_field(ofp.oxm.tcp_dst(set_tcp_dst)),
742 ofp.action.group(action_group_id)]),
743 ofp.instruction.goto_table(60)
744 ],
745 buffer_id=ofp.OFP_NO_BUFFER,
746 priority=1)
747 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)
748 ctrl.message_send(request)
749 return request
750
751#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
752def add_snat_flow(ctrl, eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src):
753 match = ofp.match()
754 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
755 match.oxm_list.append(ofp.oxm.ipv4_src(ip_src))
756 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
757 match.oxm_list.append(ofp.oxm.tcp_src(tcp_src))
758
759 request = ofp.message.flow_add(
760 table_id=29,
761 cookie=42,
762 match=match,
763 instructions=[
764 ofp.instruction.write_actions(
765 actions=[ofp.action.set_field(ofp.oxm.ipv4_src(set_ip_src)),
766 ofp.action.set_field(ofp.oxm.tcp_src(set_tcp_src))]),
767 ofp.instruction.goto_table(30)
768 ],
769 buffer_id=ofp.OFP_NO_BUFFER,
770 priority=1)
771 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)
772 ctrl.message_send(request)
773 return request
774
775def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'):
776 """
777 Command Example:
778 of-agent vtap 10001 ethernet 1/1 vid 1
779 of-agent vtp 10001 vni 10
780 """
781 if vlan != 0:
782 config_vtap_xml="""
783 <config>
784 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
785 <id>capable-switch-1</id>
786 <resources>
787 <port xc:operation="OPERATION">
788 <resource-id >LPORT</resource-id>
789 <features>
790 <current>
791 <rate>10Gb</rate>
792 <medium>fiber</medium>
793 <pause>symmetric</pause>
794 </current>
795 <advertised>
796 <rate>10Gb</rate>
797 <rate>100Gb</rate>
798 <medium>fiber</medium>
799 <pause>symmetric</pause>
800 </advertised>
801 <supported>
802 <rate>10Gb</rate>
803 <rate>100Gb</rate>
804 <medium>fiber</medium>
805 <pause>symmetric</pause>
806 </supported>
807 <advertised-peer>
808 <rate>10Gb</rate>
809 <rate>100Gb</rate>
810 <medium>fiber</medium>
811 <pause>symmetric</pause>
812 </advertised-peer>
813 </features>
814 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
815 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
816 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
817 <ofdpa10:vni>VNID</ofdpa10:vni>
818 </ofdpa10:vtap>
819 </port>
820 </resources>
821 <logical-switches>
822 <switch>
823 <id>DATAPATH_ID</id>
824 <datapath-id>DATAPATH_ID</datapath-id>
825 <resources>
826 <port xc:operation="OPERATION">LPORT</port>
827 </resources>
828 </switch>
829 </logical-switches>
830 </capable-switch>
831 </config>
832 """
833 else:
834 config_vtap_xml="""
835 <config>
836 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
837 <id>capable-switch-1</id>
838 <resources>
839 <port xc:operation="OPERATION">
840 <resource-id >LPORT</resource-id>
841 <features>
842 <current>
843 <rate>10Gb</rate>
844 <medium>fiber</medium>
845 <pause>symmetric</pause>
846 </current>
847 <advertised>
848 <rate>10Gb</rate>
849 <rate>100Gb</rate>
850 <medium>fiber</medium>
851 <pause>symmetric</pause>
852 </advertised>
853 <supported>
854 <rate>10Gb</rate>
855 <rate>100Gb</rate>
856 <medium>fiber</medium>
857 <pause>symmetric</pause>
858 </supported>
859 <advertised-peer>
860 <rate>10Gb</rate>
861 <rate>100Gb</rate>
862 <medium>fiber</medium>
863 <pause>symmetric</pause>
864 </advertised-peer>
865 </features>
866 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
867 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
868 <ofdpa10:vni>VNID</ofdpa10:vni>
869 </ofdpa10:vtap>
870 </port>
871 </resources>
872 <logical-switches>
873 <switch>
874 <id>DATAPATH_ID</id>
875 <datapath-id>DATAPATH_ID</datapath-id>
876 <resources>
877 <port xc:operation="OPERATION">LPORT</port>
878 </resources>
879 </switch>
880 </logical-switches>
881 </capable-switch>
882 </config>
883 """
884 str_datapath_id_f= "{:016x}".format(dp_id)
885 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
886 config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id)
887 config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport)))
888 config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port))
889 config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan))
890 config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid))
891 config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation))
892 return config_vtap_xml
893
894def get_vtep_lport_config_xml(dp_id, lport, src_ip, dst_ip, next_hop_id, vnid, udp_src_port=6633, ttl=25, operation='merge'):
895 """
896 Command Example:
897 of-agent vtep 10002 source user-input-src-ip destination user-input-dst-ip udp-source-port 6633 nexthop 2 ttl 25
898 of-agent vtp 10001 vni 10
899 """
900
901 config_vtep_xml="""
902 <config>
903 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
904 <id>capable-switch-1</id>
905 <resources>
906 <port xc:operation="OPERATION">
907 <resource-id>LPORT</resource-id>
908 <features>
909 <current>
910 <rate>10Gb</rate>
911 <medium>fiber</medium>
912 <pause>symmetric</pause>
913 </current>
914 <advertised>
915 <rate>10Gb</rate>
916 <rate>100Gb</rate>
917 <medium>fiber</medium>
918 <pause>symmetric</pause>
919 </advertised>
920 <supported>
921 <rate>10Gb</rate>
922 <rate>100Gb</rate>
923 <medium>fiber</medium>
924 <pause>symmetric</pause>
925 </supported>
926 <advertised-peer>
927 <rate>10Gb</rate>
928 <rate>100Gb</rate>
929 <medium>fiber</medium>
930 <pause>symmetric</pause>
931 </advertised-peer>
932 </features>
933 <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01">
934 <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip>
935 <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip>
936 <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port>
937 <ofdpa10:vni xc:operation="OPERATION">
938 <ofdpa10:id>VNID</ofdpa10:id>
939 </ofdpa10:vni>
940 <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id>
941 <ofdpa10:ttl>TTL</ofdpa10:ttl>
942 </ofdpa10:vtep>
943 </port>
944 </resources>
945 <logical-switches>
946 <switch>
947 <id>DATAPATH_ID</id>
948 <datapath-id>DATAPATH_ID</datapath-id>
949 <resources>
950 <port xc:operation="OPERATION">LPORT</port>
951 </resources>
952 </switch>
953 </logical-switches>
954 </capable-switch>
955 </config>
956 """
957 str_datapath_id_f= "{:016x}".format(dp_id)
958 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
959 config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id)
960 config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport)))
961 config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip))
962 config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip))
963 config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port))
964 config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id))
965 config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl))
966 config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid))
967 config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation))
968
969 return config_vtep_xml
970
971def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'):
972 #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2
973 config_nexthop_xml="""
974 <config>
975 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
976 <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
977 <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id>
978 <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac>
979 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
980 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
981 </ofdpa10:next-hop>
982 </of11-config:capable-switch>
983 </config>
984 """
985 config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan))
986 config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port))
987 config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id))
988 config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac))
989 config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation))
990 return config_nexthop_xml
991
992def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'):
993 #of-agent vni 10 multicast 224.1.1.1 nexthop 20
994 if mcast_ipv4!=None:
995 config_vni_xml="""
996 <config>
997 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
998 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
999 <ofdpa10:id>VNID</ofdpa10:id>
1000 <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group>
1001 <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id>
1002 </ofdpa10:vni>
1003 </of11-config:capable-switch>
1004 </config>
1005 """
1006 config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1007 config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4))
1008 else:
1009 config_vni_xml="""
1010 <config>
1011 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1012 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1013 <ofdpa10:id>VNID</ofdpa10:id>
1014 </ofdpa10:vni>
1015 </of11-config:capable-switch>
1016 </config>
1017 """
1018
1019 config_vni_xml=config_vni_xml.replace("VNID", str(vni_id))
1020 config_vni_xml=config_vni_xml.replace("OPERATION", str(operation))
1021 return config_vni_xml
1022
1023def get_featureReplay(self):
1024 req = ofp.message.features_request()
1025 res, raw = self.controller.transact(req)
1026 self.assertIsNotNone(res, "Did not receive a response from the DUT.")
1027 self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY,
1028 ("Unexpected packet type %d received in response to "
1029 "OFPT_FEATURES_REQUEST") % res.type)
1030 return res
1031
1032def send_edit_config(switch_ip, xml, target='runing'):
1033 NETCONF_ACCOUNT="netconfuser"
1034 NETCONF_PASSWD="netconfuser"
1035 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1036 try:
1037 m.edit_config(target='running',
1038 config=xml,
1039 default_operation='merge',
1040 error_option='stop-on-error')
1041
1042 except Exception as e:
1043 logging.info("Fail to set xml %s", xml)
1044 return False
1045
1046 #return m.get_config(source='running').data_xml
1047 return True
1048
1049def send_delete_config(switch_ip, xml, target='runing'):
1050 NETCONF_ACCOUNT="netconfuser"
1051 NETCONF_PASSWD="netconfuser"
1052 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1053 try:
1054 m.edit_config(target='running',
1055 config=xml,
1056 default_operation='delete',
1057 error_option='stop-on-error')
1058
1059 except Exception as e:
1060 logging.info("Fail to set xml %s", xml)
1061 return False
1062
1063 #return m.get_config(source='running').data_xml
1064 return True
1065
1066def get_edit_config(switch_ip, target='runing'):
1067 NETCONF_ACCOUNT="netconfuser"
1068 NETCONF_PASSWD="netconfuser"
1069 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1070 return m.get_config(source='running').data_xml
1071
1072
1073"""
1074MPLS
1075"""
1076
1077OFDPA_MPLS_SUBTYPE_SHIFT=24
1078OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1
1079OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2
1080OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3
1081OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4
1082OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5
1083OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6
1084OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8
1085OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10
1086
1087def encode_mpls_interface_group_id(subtype, index):
1088 index=index&0x00ffffff
1089 assert(subtype==0)
1090 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1091
1092def encode_mpls_label_group_id(subtype, index):
1093 index=index&0x00ffffff
1094 assert(subtype <=5 or subtype==0)
1095 #1: l2 vpn label
1096 #2: l3 vpn label
1097 #3: mpls tunnel label 1
1098 #4: mpls tunnel lable 2
1099 #5: mpls swap label
1100 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1101
1102def encode_mpls_forwarding_group_id(subtype, index):
1103 index=index&0x00ffffff
1104 assert(subtype==6 or subtype==8 or subtype==10)
1105 return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1106
1107
1108def add_mpls_intf_group(ctrl, ref_gid, dst_mac, src_mac, vid, index, subtype=0):
1109 action=[]
1110 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
1111 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
1112 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vid)))
1113 action.append(ofp.action.group(ref_gid))
1114
1115 buckets = [ofp.bucket(actions=action)]
1116
1117 mpls_group_id =encode_mpls_interface_group_id(subtype, index)
1118 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1119 group_id=mpls_group_id,
1120 buckets=buckets
1121 )
1122 ctrl.message_send(request)
1123 return mpls_group_id, request
1124
1125def add_mpls_label_group(ctrl, subtype, index, ref_gid,
1126 lmep_id=-1,
1127 qos_index=-1,
1128 push_l2_header=False,
1129 push_vlan=False,
1130 push_mpls_header=False,
1131 push_cw=False,
1132 set_mpls_label=None,
1133 set_bos=None,
1134 set_tc=None,
1135 set_tc_from_table=False,
1136 cpy_tc_outward=False,
1137 set_ttl=None,
1138 cpy_ttl_outward=False,
1139 oam_lm_tx_count=False,
1140 set_pri_from_table=False
1141 ):
1142 """
1143 @ref_gid: only can be mpls intf group or mpls tunnel label 1/2 group
1144 """
1145 action=[]
1146
1147 if push_vlan== True:
1148 action.append(ofp.action.push_vlan(0x8100))
1149 if push_mpls_header== True:
1150 action.append(ofp.action.push_mpls(0x8847))
1151 if set_mpls_label != None:
1152 action.append(ofp.action.set_field(ofp.oxm.mpls_label(set_mpls_label)))
1153 if set_bos != None:
1154 action.append(ofp.action.set_field(ofp.oxm.mpls_bos(set_bos)))
1155 if set_tc != None:
1156 assert(set_tc_from_table==False)
1157 action.append(ofp.action.set_field(ofp.oxm.mpls_tc(set_tc)))
1158 if set_ttl != None:
1159 action.append(ofp.action.set_mpls_ttl(set_ttl))
1160 if cpy_ttl_outward == True:
1161 action.append(ofp.action.copy_ttl_out())
1162 """
1163 ofdpa experimenter
1164 """
1165 if push_l2_header== True:
1166 action.append(ofp.action.ofdpa_push_l2_header())
1167 if set_tc_from_table== True:
1168 assert(qos_index>=0)
1169 assert(set_tc == None)
1170 action.append(ofp.action.ofdpa_set_tc_from_table(qos_index))
1171 if cpy_tc_outward == True:
1172 action.append(ofp.action.ofdpa_copy_tc_out())
1173 if oam_lm_tx_count == True:
1174 assert(qos_index>=0 and lmep_id>=0)
1175 action.append(ofp.action.ofdpa_oam_lm_tx_count(lmep_id, qos_index))
1176 if set_pri_from_table == True:
1177 assert(qos_index>=0)
1178 action.append(ofp.action.ofdpa_set_qos_from_table(qos_index))
1179 if push_cw == True:
1180 action.append(ofp.action.ofdpa_push_cw())
1181
1182 action.append(ofp.action.group(ref_gid))
1183 buckets = [ofp.bucket(actions=action)]
1184
1185 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1186 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1187 group_id=mpls_group_id,
1188 buckets=buckets
1189 )
1190 ctrl.message_send(request)
1191
1192 return mpls_group_id, request
1193
1194def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids,
1195 watch_port=None,
1196 watch_group=ofp.OFPP_ANY,
1197 push_vlan=None,
1198 pop_vlan=None,
1199 set_vid=None):
1200 assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP
1201 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP
1202 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG)
1203
1204 buckets=[]
1205 if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP:
1206 group_type = ofp.OFPGT_FF
1207 for gid in ref_gids:
1208 action=[]
1209 action.append(ofp.action.group(gid))
1210 buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action))
1211
1212 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP:
1213 group_type = ofp.OFPGT_SELECT
1214 for gid in ref_gids:
1215 action=[]
1216 action.append(ofp.action.group(gid))
1217 buckets.append(ofp.bucket(actions=action))
1218
1219 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG:
1220 group_type = ofp.OFPGT_INDIRECT
1221 action=[]
1222 if set_vid!=None:
1223 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(set_vid)))
1224 if push_vlan!=None:
1225 action.append(ofp.action.push_vlan(push_vlan))
1226 if pop_vlan!=None:
1227 action.append(ofp.action.pop_vlan())
1228 action.append(ofp.action.group(ref_gids[0]))
1229 buckets.append(ofp.bucket(actions=action))
1230
1231 mpls_group_id = encode_mpls_forwarding_group_id(subtype, index)
1232 request = ofp.message.group_add(group_type=group_type,
1233 group_id=mpls_group_id,
1234 buckets=buckets
1235 )
1236 ctrl.message_send(request)
1237 return mpls_group_id, request
1238
1239
1240"""
1241dislay
1242"""
1243def print_current_table_flow_stat(ctrl, table_id=0xff):
1244 stat_req=ofp.message.flow_stats_request()
1245 response, pkt = ctrl.transact(stat_req)
1246 if response == None:
1247 print "no response"
1248 return None
1249 print len(response.entries)
1250 for obj in response.entries:
1251 print "match ", obj.match
1252 print "cookie", obj.cookie
1253 print "priority", obj.priority
1254 print "idle_timeout", obj.idle_timeout
1255 print "hard_timeout", obj.hard_timeout
1256 #obj.actions
Flavio Castroe0b46012015-12-02 14:42:27 -05001257 print "packet count: %lx"%obj.packet_count