blob: e5a7e41a471d089af04ecab3b03fc3765bfb0cb5 [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
166def add_one_l2_interface_grouop(ctrl, port, vlan_id=1, is_tagged=False, send_barrier=False):
167 # 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))
424 request = ofp.message.flow_add(
425 table_id=10,
426 cookie=42,
427 match=match,
428 instructions=[
429 ofp.instruction.goto_table(20)
430 ],
431 priority=0)
432 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
433 ctrl.message_send(request)
434
435 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
436 match = ofp.match()
437 match.oxm_list.append(ofp.oxm.in_port(of_port))
438 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0xfff))
439 request = ofp.message.flow_add(
440 table_id=10,
441 cookie=42,
442 match=match,
443 instructions=[
444 ofp.instruction.apply_actions(
445 actions=[
446 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
447 ]
448 ),
449 ofp.instruction.goto_table(20)
450 ],
451 priority=0)
452 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
453 ctrl.message_send(request)
454 msgs.append(request)
455
456 if send_barrier:
457 do_barrier(ctrl)
458
459 return msgs
460
461def add_vlan_table_flow_pvid(ctrl, in_port, match_vid=None, pvid=1, send_barrier=False):
462 """it will tag pack as untagged packet wether it has tagg or not"""
463 match = ofp.match()
464 match.oxm_list.append(ofp.oxm.in_port(in_port))
465 actions=[]
466 if match_vid == None:
467 match.oxm_list.append(ofp.oxm.vlan_vid(0))
468 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
469 goto_table=20
470 else:
471 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+match_vid, 0x1fff))
472 actions.append(ofp.action.push_vlan(0x8100))
473 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
474 goto_table=20
475
476 request = ofp.message.flow_add(
477 table_id=10,
478 cookie=42,
479 match=match,
480 instructions=[
481 ofp.instruction.apply_actions(actions=actions)
482 ,ofp.instruction.goto_table(goto_table)
483 ],
484 priority=0)
485 logging.info("Add PVID %d on port %d and go to table %ld" %( pvid, in_port, goto_table))
486 ctrl.message_send(request)
487
488 if send_barrier:
489 do_barrier(ctrl)
490
491def add_vlan_table_flow_allow_all_vlan(ctrl, in_port, send_barrier=False):
492 """it st flow allow all vlan tag on this port"""
493 match = ofp.match()
494 match.oxm_list.append(ofp.oxm.in_port(in_port))
495 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1000))
496 request = ofp.message.flow_add(
497 table_id=10,
498 cookie=42,
499 match=match,
500 instructions=[
501 ofp.instruction.goto_table(20)
502 ],
503 priority=0)
504 logging.info("Add allow all vlan on port %d " %(in_port))
505 ctrl.message_send(request)
506
507
508def add_one_vlan_table_flow(ctrl, of_port, vlan_id=1, vrf=0, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
509 # table 10: vlan
510 # goto to table 20
511 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
512 match = ofp.match()
513 match.oxm_list.append(ofp.oxm.in_port(of_port))
514 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
515
516 actions=[]
517 if vrf!=0:
518 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
519
520 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id)))
521
522 request = ofp.message.flow_add(
523 table_id=10,
524 cookie=42,
525 match=match,
526 instructions=[
527 ofp.instruction.apply_actions(
528 actions=actions
529 ),
530 ofp.instruction.goto_table(20)
531 ],
532 priority=0)
533 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
534 ctrl.message_send(request)
535
536 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
537 match = ofp.match()
538 match.oxm_list.append(ofp.oxm.in_port(of_port))
539 match.oxm_list.append(ofp.oxm.vlan_vid(0))
540
541 actions=[]
542 if vrf!=0:
543 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
544
545 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
546
547 request = ofp.message.flow_add(
548 table_id=10,
549 cookie=42,
550 match=match,
551 instructions=[
552 ofp.instruction.apply_actions(
553 actions=actions
554 ),
555 ofp.instruction.goto_table(20)
556 ],
557 priority=0)
558 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
559 ctrl.message_send(request)
560
561 if send_barrier:
562 do_barrier(ctrl)
563
564 return request
565
566def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False):
567 match = ofp.match()
568 if dst_mac!=None:
569 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
570
571 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
572
573 request = ofp.message.flow_add(
574 table_id=50,
575 cookie=42,
576 match=match,
577 instructions=[
578 ofp.instruction.write_actions(
579 actions=[
580 ofp.action.group(group_id)]),
581 ofp.instruction.goto_table(60)
582 ],
583 buffer_id=ofp.OFP_NO_BUFFER,
584 priority=1000)
585
586 logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac)
587 ctrl.message_send(request)
588
589 if send_barrier:
590 do_barrier(ctrl)
591
592 return request
593
594def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False):
595 match = ofp.match()
596 if dst_mac!=None:
597 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
598
599 match.oxm_list.append(ofp.oxm.tunnel_id(vnid))
600 if is_group == True:
601 actions=[ofp.action.group(group_id)]
602 else:
603 actions=[ofp.action.output(group_id)]
604
605 request = ofp.message.flow_add(
606 table_id=50,
607 cookie=42,
608 match=match,
609 instructions=[
610 ofp.instruction.write_actions(
611 actions=actions),
612 ofp.instruction.goto_table(60)
613 ],
614 buffer_id=ofp.OFP_NO_BUFFER,
615 priority=1000)
616
617 logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac)
618 ctrl.message_send(request)
619
620 if send_barrier:
621 do_barrier(ctrl)
622
623 return request
624
625def add_termination_flow(ctrl, in_port, eth_type, dst_mac, vlanid, goto_table=None, send_barrier=False):
626 match = ofp.match()
627 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
628 if dst_mac[0]&0x01 == 0x01:
629 match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00]))
630 goto_table=40
631 else:
632 if in_port!=0:
633 match.oxm_list.append(ofp.oxm.in_port(in_port))
634 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
635 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
636 if goto_table == None:
637 goto_table=30
638
639 request = ofp.message.flow_add(
640 table_id=20,
641 cookie=42,
642 match=match,
643 instructions=[
644 ofp.instruction.goto_table(goto_table)
645 ],
646 buffer_id=ofp.OFP_NO_BUFFER,
647 priority=1)
648
649 logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac)
650 ctrl.message_send(request)
651
652 if send_barrier:
653 do_barrier(ctrl)
654
655 return request
656
657def add_unicast_routing_flow(ctrl, eth_type, dst_ip, mask, action_group_id, vrf=0, send_barrier=False):
658 match = ofp.match()
659 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
660 if vrf != 0:
661 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
662
663 if mask!=0:
664 match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask))
665 else:
666 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
667
668
669 request = ofp.message.flow_add(
670 table_id=30,
671 cookie=42,
672 match=match,
673 instructions=[
674 ofp.instruction.write_actions(
675 actions=[ofp.action.group(action_group_id)]),
676 ofp.instruction.goto_table(60)
677 ],
678 buffer_id=ofp.OFP_NO_BUFFER,
679 priority=1)
680
681 logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip)
682 ctrl.message_send(request)
683
684 if send_barrier:
685 do_barrier(ctrl)
686
687 return request
688
689def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False):
690 match = ofp.match()
691 match.oxm_list.append(ofp.oxm.eth_type(0x0800))
692 match.oxm_list.append(ofp.oxm.vlan_vid(vlan_id))
693 if src_ip_mask!=0:
694 match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask))
695 else:
696 match.oxm_list.append(ofp.oxm.ipv4_src(src_ip))
697
698 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
699
700 request = ofp.message.flow_add(
701 table_id=40,
702 cookie=42,
703 match=match,
704 instructions=[
705 ofp.instruction.write_actions(
706 actions=[ofp.action.group(action_group_id)]),
707 ofp.instruction.goto_table(60)
708 ],
709 buffer_id=ofp.OFP_NO_BUFFER,
710 priority=1)
711
712 logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask)
713 ctrl.message_send(request)
714
715 if send_barrier:
716 do_barrier(ctrl)
717
718 return request
719
720#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
721def add_dnat_flow(ctrl, eth_type, ip_dst, ip_proto, tcp_dst, set_ip_dst, set_tcp_dst, action_group_id):
722 match = ofp.match()
723 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
724 match.oxm_list.append(ofp.oxm.ipv4_dst(ip_dst))
725 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
726 match.oxm_list.append(ofp.oxm.tcp_dst(tcp_dst))
727
728 request = ofp.message.flow_add(
729 table_id=28,
730 cookie=42,
731 match=match,
732 instructions=[
733 ofp.instruction.write_actions(
734 actions=[ofp.action.set_field(ofp.oxm.ipv4_dst(set_ip_dst)),
735 ofp.action.set_field(ofp.oxm.tcp_dst(set_tcp_dst)),
736 ofp.action.group(action_group_id)]),
737 ofp.instruction.goto_table(60)
738 ],
739 buffer_id=ofp.OFP_NO_BUFFER,
740 priority=1)
741 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)
742 ctrl.message_send(request)
743 return request
744
745#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
746def add_snat_flow(ctrl, eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src):
747 match = ofp.match()
748 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
749 match.oxm_list.append(ofp.oxm.ipv4_src(ip_src))
750 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
751 match.oxm_list.append(ofp.oxm.tcp_src(tcp_src))
752
753 request = ofp.message.flow_add(
754 table_id=29,
755 cookie=42,
756 match=match,
757 instructions=[
758 ofp.instruction.write_actions(
759 actions=[ofp.action.set_field(ofp.oxm.ipv4_src(set_ip_src)),
760 ofp.action.set_field(ofp.oxm.tcp_src(set_tcp_src))]),
761 ofp.instruction.goto_table(30)
762 ],
763 buffer_id=ofp.OFP_NO_BUFFER,
764 priority=1)
765 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)
766 ctrl.message_send(request)
767 return request
768
769def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'):
770 """
771 Command Example:
772 of-agent vtap 10001 ethernet 1/1 vid 1
773 of-agent vtp 10001 vni 10
774 """
775 if vlan != 0:
776 config_vtap_xml="""
777 <config>
778 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
779 <id>capable-switch-1</id>
780 <resources>
781 <port xc:operation="OPERATION">
782 <resource-id >LPORT</resource-id>
783 <features>
784 <current>
785 <rate>10Gb</rate>
786 <medium>fiber</medium>
787 <pause>symmetric</pause>
788 </current>
789 <advertised>
790 <rate>10Gb</rate>
791 <rate>100Gb</rate>
792 <medium>fiber</medium>
793 <pause>symmetric</pause>
794 </advertised>
795 <supported>
796 <rate>10Gb</rate>
797 <rate>100Gb</rate>
798 <medium>fiber</medium>
799 <pause>symmetric</pause>
800 </supported>
801 <advertised-peer>
802 <rate>10Gb</rate>
803 <rate>100Gb</rate>
804 <medium>fiber</medium>
805 <pause>symmetric</pause>
806 </advertised-peer>
807 </features>
808 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
809 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
810 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
811 <ofdpa10:vni>VNID</ofdpa10:vni>
812 </ofdpa10:vtap>
813 </port>
814 </resources>
815 <logical-switches>
816 <switch>
817 <id>DATAPATH_ID</id>
818 <datapath-id>DATAPATH_ID</datapath-id>
819 <resources>
820 <port xc:operation="OPERATION">LPORT</port>
821 </resources>
822 </switch>
823 </logical-switches>
824 </capable-switch>
825 </config>
826 """
827 else:
828 config_vtap_xml="""
829 <config>
830 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
831 <id>capable-switch-1</id>
832 <resources>
833 <port xc:operation="OPERATION">
834 <resource-id >LPORT</resource-id>
835 <features>
836 <current>
837 <rate>10Gb</rate>
838 <medium>fiber</medium>
839 <pause>symmetric</pause>
840 </current>
841 <advertised>
842 <rate>10Gb</rate>
843 <rate>100Gb</rate>
844 <medium>fiber</medium>
845 <pause>symmetric</pause>
846 </advertised>
847 <supported>
848 <rate>10Gb</rate>
849 <rate>100Gb</rate>
850 <medium>fiber</medium>
851 <pause>symmetric</pause>
852 </supported>
853 <advertised-peer>
854 <rate>10Gb</rate>
855 <rate>100Gb</rate>
856 <medium>fiber</medium>
857 <pause>symmetric</pause>
858 </advertised-peer>
859 </features>
860 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
861 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
862 <ofdpa10:vni>VNID</ofdpa10:vni>
863 </ofdpa10:vtap>
864 </port>
865 </resources>
866 <logical-switches>
867 <switch>
868 <id>DATAPATH_ID</id>
869 <datapath-id>DATAPATH_ID</datapath-id>
870 <resources>
871 <port xc:operation="OPERATION">LPORT</port>
872 </resources>
873 </switch>
874 </logical-switches>
875 </capable-switch>
876 </config>
877 """
878 str_datapath_id_f= "{:016x}".format(dp_id)
879 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
880 config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id)
881 config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport)))
882 config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port))
883 config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan))
884 config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid))
885 config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation))
886 return config_vtap_xml
887
888def get_vtep_lport_config_xml(dp_id, lport, src_ip, dst_ip, next_hop_id, vnid, udp_src_port=6633, ttl=25, operation='merge'):
889 """
890 Command Example:
891 of-agent vtep 10002 source user-input-src-ip destination user-input-dst-ip udp-source-port 6633 nexthop 2 ttl 25
892 of-agent vtp 10001 vni 10
893 """
894
895 config_vtep_xml="""
896 <config>
897 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
898 <id>capable-switch-1</id>
899 <resources>
900 <port xc:operation="OPERATION">
901 <resource-id>LPORT</resource-id>
902 <features>
903 <current>
904 <rate>10Gb</rate>
905 <medium>fiber</medium>
906 <pause>symmetric</pause>
907 </current>
908 <advertised>
909 <rate>10Gb</rate>
910 <rate>100Gb</rate>
911 <medium>fiber</medium>
912 <pause>symmetric</pause>
913 </advertised>
914 <supported>
915 <rate>10Gb</rate>
916 <rate>100Gb</rate>
917 <medium>fiber</medium>
918 <pause>symmetric</pause>
919 </supported>
920 <advertised-peer>
921 <rate>10Gb</rate>
922 <rate>100Gb</rate>
923 <medium>fiber</medium>
924 <pause>symmetric</pause>
925 </advertised-peer>
926 </features>
927 <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01">
928 <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip>
929 <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip>
930 <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port>
931 <ofdpa10:vni xc:operation="OPERATION">
932 <ofdpa10:id>VNID</ofdpa10:id>
933 </ofdpa10:vni>
934 <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id>
935 <ofdpa10:ttl>TTL</ofdpa10:ttl>
936 </ofdpa10:vtep>
937 </port>
938 </resources>
939 <logical-switches>
940 <switch>
941 <id>DATAPATH_ID</id>
942 <datapath-id>DATAPATH_ID</datapath-id>
943 <resources>
944 <port xc:operation="OPERATION">LPORT</port>
945 </resources>
946 </switch>
947 </logical-switches>
948 </capable-switch>
949 </config>
950 """
951 str_datapath_id_f= "{:016x}".format(dp_id)
952 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
953 config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id)
954 config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport)))
955 config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip))
956 config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip))
957 config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port))
958 config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id))
959 config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl))
960 config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid))
961 config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation))
962
963 return config_vtep_xml
964
965def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'):
966 #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2
967 config_nexthop_xml="""
968 <config>
969 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
970 <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
971 <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id>
972 <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac>
973 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
974 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
975 </ofdpa10:next-hop>
976 </of11-config:capable-switch>
977 </config>
978 """
979 config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan))
980 config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port))
981 config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id))
982 config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac))
983 config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation))
984 return config_nexthop_xml
985
986def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'):
987 #of-agent vni 10 multicast 224.1.1.1 nexthop 20
988 if mcast_ipv4!=None:
989 config_vni_xml="""
990 <config>
991 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
992 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
993 <ofdpa10:id>VNID</ofdpa10:id>
994 <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group>
995 <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id>
996 </ofdpa10:vni>
997 </of11-config:capable-switch>
998 </config>
999 """
1000 config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1001 config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4))
1002 else:
1003 config_vni_xml="""
1004 <config>
1005 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1006 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1007 <ofdpa10:id>VNID</ofdpa10:id>
1008 </ofdpa10:vni>
1009 </of11-config:capable-switch>
1010 </config>
1011 """
1012
1013 config_vni_xml=config_vni_xml.replace("VNID", str(vni_id))
1014 config_vni_xml=config_vni_xml.replace("OPERATION", str(operation))
1015 return config_vni_xml
1016
1017def get_featureReplay(self):
1018 req = ofp.message.features_request()
1019 res, raw = self.controller.transact(req)
1020 self.assertIsNotNone(res, "Did not receive a response from the DUT.")
1021 self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY,
1022 ("Unexpected packet type %d received in response to "
1023 "OFPT_FEATURES_REQUEST") % res.type)
1024 return res
1025
1026def send_edit_config(switch_ip, xml, target='runing'):
1027 NETCONF_ACCOUNT="netconfuser"
1028 NETCONF_PASSWD="netconfuser"
1029 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1030 try:
1031 m.edit_config(target='running',
1032 config=xml,
1033 default_operation='merge',
1034 error_option='stop-on-error')
1035
1036 except Exception as e:
1037 logging.info("Fail to set xml %s", xml)
1038 return False
1039
1040 #return m.get_config(source='running').data_xml
1041 return True
1042
1043def send_delete_config(switch_ip, xml, target='runing'):
1044 NETCONF_ACCOUNT="netconfuser"
1045 NETCONF_PASSWD="netconfuser"
1046 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1047 try:
1048 m.edit_config(target='running',
1049 config=xml,
1050 default_operation='delete',
1051 error_option='stop-on-error')
1052
1053 except Exception as e:
1054 logging.info("Fail to set xml %s", xml)
1055 return False
1056
1057 #return m.get_config(source='running').data_xml
1058 return True
1059
1060def get_edit_config(switch_ip, target='runing'):
1061 NETCONF_ACCOUNT="netconfuser"
1062 NETCONF_PASSWD="netconfuser"
1063 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1064 return m.get_config(source='running').data_xml
1065
1066
1067"""
1068MPLS
1069"""
1070
1071OFDPA_MPLS_SUBTYPE_SHIFT=24
1072OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1
1073OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2
1074OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3
1075OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4
1076OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5
1077OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6
1078OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8
1079OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10
1080
1081def encode_mpls_interface_group_id(subtype, index):
1082 index=index&0x00ffffff
1083 assert(subtype==0)
1084 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1085
1086def encode_mpls_label_group_id(subtype, index):
1087 index=index&0x00ffffff
1088 assert(subtype <=5 or subtype==0)
1089 #1: l2 vpn label
1090 #2: l3 vpn label
1091 #3: mpls tunnel label 1
1092 #4: mpls tunnel lable 2
1093 #5: mpls swap label
1094 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1095
1096def encode_mpls_forwarding_group_id(subtype, index):
1097 index=index&0x00ffffff
1098 assert(subtype==6 or subtype==8 or subtype==10)
1099 return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1100
1101
1102def add_mpls_intf_group(ctrl, ref_gid, dst_mac, src_mac, vid, index, subtype=0):
1103 action=[]
1104 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
1105 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
1106 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vid)))
1107 action.append(ofp.action.group(ref_gid))
1108
1109 buckets = [ofp.bucket(actions=action)]
1110
1111 mpls_group_id =encode_mpls_interface_group_id(subtype, index)
1112 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1113 group_id=mpls_group_id,
1114 buckets=buckets
1115 )
1116 ctrl.message_send(request)
1117 return mpls_group_id, request
1118
1119def add_mpls_label_group(ctrl, subtype, index, ref_gid,
1120 lmep_id=-1,
1121 qos_index=-1,
1122 push_l2_header=False,
1123 push_vlan=False,
1124 push_mpls_header=False,
1125 push_cw=False,
1126 set_mpls_label=None,
1127 set_bos=None,
1128 set_tc=None,
1129 set_tc_from_table=False,
1130 cpy_tc_outward=False,
1131 set_ttl=None,
1132 cpy_ttl_outward=False,
1133 oam_lm_tx_count=False,
1134 set_pri_from_table=False
1135 ):
1136 """
1137 @ref_gid: only can be mpls intf group or mpls tunnel label 1/2 group
1138 """
1139 action=[]
1140
1141 if push_vlan== True:
1142 action.append(ofp.action.push_vlan(0x8100))
1143 if push_mpls_header== True:
1144 action.append(ofp.action.push_mpls(0x8847))
1145 if set_mpls_label != None:
1146 action.append(ofp.action.set_field(ofp.oxm.mpls_label(set_mpls_label)))
1147 if set_bos != None:
1148 action.append(ofp.action.set_field(ofp.oxm.mpls_bos(set_bos)))
1149 if set_tc != None:
1150 assert(set_tc_from_table==False)
1151 action.append(ofp.action.set_field(ofp.oxm.mpls_tc(set_tc)))
1152 if set_ttl != None:
1153 action.append(ofp.action.set_mpls_ttl(set_ttl))
1154 if cpy_ttl_outward == True:
1155 action.append(ofp.action.copy_ttl_out())
1156 """
1157 ofdpa experimenter
1158 """
1159 if push_l2_header== True:
1160 action.append(ofp.action.ofdpa_push_l2_header())
1161 if set_tc_from_table== True:
1162 assert(qos_index>=0)
1163 assert(set_tc == None)
1164 action.append(ofp.action.ofdpa_set_tc_from_table(qos_index))
1165 if cpy_tc_outward == True:
1166 action.append(ofp.action.ofdpa_copy_tc_out())
1167 if oam_lm_tx_count == True:
1168 assert(qos_index>=0 and lmep_id>=0)
1169 action.append(ofp.action.ofdpa_oam_lm_tx_count(lmep_id, qos_index))
1170 if set_pri_from_table == True:
1171 assert(qos_index>=0)
1172 action.append(ofp.action.ofdpa_set_qos_from_table(qos_index))
1173 if push_cw == True:
1174 action.append(ofp.action.ofdpa_push_cw())
1175
1176 action.append(ofp.action.group(ref_gid))
1177 buckets = [ofp.bucket(actions=action)]
1178
1179 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1180 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1181 group_id=mpls_group_id,
1182 buckets=buckets
1183 )
1184 ctrl.message_send(request)
1185
1186 return mpls_group_id, request
1187
1188def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids,
1189 watch_port=None,
1190 watch_group=ofp.OFPP_ANY,
1191 push_vlan=None,
1192 pop_vlan=None,
1193 set_vid=None):
1194 assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP
1195 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP
1196 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG)
1197
1198 buckets=[]
1199 if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP:
1200 group_type = ofp.OFPGT_FF
1201 for gid in ref_gids:
1202 action=[]
1203 action.append(ofp.action.group(gid))
1204 buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action))
1205
1206 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP:
1207 group_type = ofp.OFPGT_SELECT
1208 for gid in ref_gids:
1209 action=[]
1210 action.append(ofp.action.group(gid))
1211 buckets.append(ofp.bucket(actions=action))
1212
1213 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG:
1214 group_type = ofp.OFPGT_INDIRECT
1215 action=[]
1216 if set_vid!=None:
1217 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(set_vid)))
1218 if push_vlan!=None:
1219 action.append(ofp.action.push_vlan(push_vlan))
1220 if pop_vlan!=None:
1221 action.append(ofp.action.pop_vlan())
1222 action.append(ofp.action.group(ref_gids[0]))
1223 buckets.append(ofp.bucket(actions=action))
1224
1225 mpls_group_id = encode_mpls_forwarding_group_id(subtype, index)
1226 request = ofp.message.group_add(group_type=group_type,
1227 group_id=mpls_group_id,
1228 buckets=buckets
1229 )
1230 ctrl.message_send(request)
1231 return mpls_group_id, request
1232
1233
1234"""
1235dislay
1236"""
1237def print_current_table_flow_stat(ctrl, table_id=0xff):
1238 stat_req=ofp.message.flow_stats_request()
1239 response, pkt = ctrl.transact(stat_req)
1240 if response == None:
1241 print "no response"
1242 return None
1243 print len(response.entries)
1244 for obj in response.entries:
1245 print "match ", obj.match
1246 print "cookie", obj.cookie
1247 print "priority", obj.priority
1248 print "idle_timeout", obj.idle_timeout
1249 print "hard_timeout", obj.hard_timeout
1250 #obj.actions
Flavio Castroe0b46012015-12-02 14:42:27 -05001251 print "packet count: %lx"%obj.packet_count