blob: 67a71916d24ecc967c6bb486c86ab6683f662ea9 [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
macauleyfddc4662015-07-27 17:40:30 +0800125def 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)
macauley97557232015-07-16 17:28:07 +0800130
Flavio Castrod4c44d12015-12-08 14:44:18 -0500131def add_l2_interface_group(ctrl, ports, vlan_id=1, is_tagged=False, send_barrier=False):
macauley97557232015-07-16 17:28:07 +0800132 # group table
133 # set up untag groups for each port
macauley41904ed2015-07-16 17:38:35 +0800134 group_id_list=[]
macauley15909e72015-07-17 15:58:57 +0800135 msgs=[]
macauley97557232015-07-16 17:28:07 +0800136 for of_port in ports:
137 # do stuff
138 group_id = encode_l2_interface_group_id(vlan_id, of_port)
macauley41904ed2015-07-16 17:38:35 +0800139 group_id_list.append(group_id)
macauley97557232015-07-16 17:28:07 +0800140 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)
macauley15909e72015-07-17 15:58:57 +0800159 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800160
161 if send_barrier:
162 do_barrier(ctrl)
macauley41904ed2015-07-16 17:38:35 +0800163
macauley15909e72015-07-17 15:58:57 +0800164 return group_id_list, msgs
macauley97557232015-07-16 17:28:07 +0800165
Flavio Castrod4c44d12015-12-08 14:44:18 -0500166def add_one_l2_interface_group(ctrl, port, vlan_id=1, is_tagged=False, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800167 # 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
macauley97557232015-07-16 17:28:07 +0800196def 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)
macauley15909e72015-07-17 15:58:57 +0800209 return request
macauley97557232015-07-16 17:28:07 +0800210
macauley15909e72015-07-17 15:58:57 +0800211def 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))
macauley97557232015-07-16 17:28:07 +0800217
macauley15909e72015-07-17 15:58:57 +0800218 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
macauley_cheng93f3fa52015-09-02 17:57:31 +0800236 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vlanid)))
237
macauley15909e72015-07-17 15:58:57 +0800238 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
macauleyfddc4662015-07-27 17:40:30 +0800317
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))
macauleydbff3272015-07-30 14:07:16 +0800375 NEXT_TABLE=50
macauleyfddc4662015-07-27 17:40:30 +0800376 else:
377 match.oxm_list.append(ofp.oxm.in_port(0))
macauleydbff3272015-07-30 14:07:16 +0800378 NEXT_TABLE=10
macauleyfddc4662015-07-27 17:40:30 +0800379
380 request = ofp.message.flow_add(
381 table_id=0,
382 cookie=42,
383 match=match,
384 instructions=[
macauleydbff3272015-07-30 14:07:16 +0800385 ofp.instruction.goto_table(NEXT_TABLE)
macauleyfddc4662015-07-27 17:40:30 +0800386 ],
387 priority=0)
388 logging.info("Add port table, match port %lx" % 0x10000)
389 ctrl.message_send(request)
macauleydbff3272015-07-30 14:07:16 +0800390
Flavio Castrod8f8af22015-12-02 18:19:26 -0500391
392def pop_vlan_flow(ctrl, ports, vlan_id=1):
393 # table 10: vlan
394 # goto to table 20
395 msgs=[]
396 for of_port in ports:
397 match = ofp.match()
398 match.oxm_list.append(ofp.oxm.in_port(of_port))
399 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
400 request = ofp.message.flow_add(
401 table_id=10,
402 cookie=42,
403 match=match,
404 instructions=[
405 ofp.instruction.apply_actions(
406 actions=[
407 ofp.action.pop_vlan()
408 ]
409 ),
410 ofp.instruction.goto_table(20)
411 ],
412 priority=0)
413 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
414 ctrl.message_send(request)
415
416
417 return msgs
macauleyfddc4662015-07-27 17:40:30 +0800418
macauley97557232015-07-16 17:28:07 +0800419def add_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
420 # table 10: vlan
421 # goto to table 20
macauley15909e72015-07-17 15:58:57 +0800422 msgs=[]
macauley97557232015-07-16 17:28:07 +0800423 for of_port in ports:
424 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
425 match = ofp.match()
426 match.oxm_list.append(ofp.oxm.in_port(of_port))
427 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
428 request = ofp.message.flow_add(
429 table_id=10,
430 cookie=42,
431 match=match,
432 instructions=[
Flavio Castrod8f8af22015-12-02 18:19:26 -0500433 ofp.instruction.apply_actions(
434 actions=[
435 ofp.action.pop_vlan()
436 ]
437 ),
macauley97557232015-07-16 17:28:07 +0800438 ofp.instruction.goto_table(20)
439 ],
440 priority=0)
441 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
442 ctrl.message_send(request)
443
444 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
445 match = ofp.match()
446 match.oxm_list.append(ofp.oxm.in_port(of_port))
macauley_cheng6b311612015-09-04 11:32:27 +0800447 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0xfff))
macauley97557232015-07-16 17:28:07 +0800448 request = ofp.message.flow_add(
449 table_id=10,
450 cookie=42,
451 match=match,
452 instructions=[
453 ofp.instruction.apply_actions(
454 actions=[
455 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
456 ]
457 ),
458 ofp.instruction.goto_table(20)
459 ],
460 priority=0)
461 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
462 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800463 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800464
465 if send_barrier:
466 do_barrier(ctrl)
467
macauley15909e72015-07-17 15:58:57 +0800468 return msgs
macauley_cheng6e6a6122015-11-16 14:19:18 +0800469
470def del_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
471 # table 10: vlan
472 # goto to table 20
473 msgs=[]
474 for of_port in ports:
475 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
476 match = ofp.match()
477 match.oxm_list.append(ofp.oxm.in_port(of_port))
478 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
479 request = ofp.message.flow_delete(
480 table_id=10,
481 cookie=42,
482 match=match,
483 priority=0)
484 logging.info("Del vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
485 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800486
macauley_cheng6e6a6122015-11-16 14:19:18 +0800487 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
488 match = ofp.match()
489 match.oxm_list.append(ofp.oxm.in_port(of_port))
490 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0xfff))
491 request = ofp.message.flow_delete(
492 table_id=10,
493 cookie=42,
494 match=match,
495 priority=0)
496 logging.info("Del vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
497 ctrl.message_send(request)
498 msgs.append(request)
499
500 if send_barrier:
501 do_barrier(ctrl)
502
503 return msgs
504
macauley_cheng6b311612015-09-04 11:32:27 +0800505def add_vlan_table_flow_pvid(ctrl, in_port, match_vid=None, pvid=1, send_barrier=False):
506 """it will tag pack as untagged packet wether it has tagg or not"""
507 match = ofp.match()
508 match.oxm_list.append(ofp.oxm.in_port(in_port))
509 actions=[]
510 if match_vid == None:
511 match.oxm_list.append(ofp.oxm.vlan_vid(0))
512 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
513 goto_table=20
514 else:
515 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+match_vid, 0x1fff))
516 actions.append(ofp.action.push_vlan(0x8100))
517 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
518 goto_table=20
519
520 request = ofp.message.flow_add(
521 table_id=10,
522 cookie=42,
523 match=match,
524 instructions=[
525 ofp.instruction.apply_actions(actions=actions)
526 ,ofp.instruction.goto_table(goto_table)
527 ],
528 priority=0)
529 logging.info("Add PVID %d on port %d and go to table %ld" %( pvid, in_port, goto_table))
530 ctrl.message_send(request)
531
532 if send_barrier:
533 do_barrier(ctrl)
534
535def add_vlan_table_flow_allow_all_vlan(ctrl, in_port, send_barrier=False):
536 """it st flow allow all vlan tag on this port"""
537 match = ofp.match()
538 match.oxm_list.append(ofp.oxm.in_port(in_port))
539 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1000))
540 request = ofp.message.flow_add(
541 table_id=10,
542 cookie=42,
543 match=match,
544 instructions=[
545 ofp.instruction.goto_table(20)
546 ],
547 priority=0)
548 logging.info("Add allow all vlan on port %d " %(in_port))
549 ctrl.message_send(request)
550
castroflavio8941f9a2015-12-11 16:10:20 -0500551def add_untag_vlan_table_flow(ctrl, in_port, send_barrier=False):
552 """it a flow to allow all vlan untag on this port"""
553 match = ofp.match()
554 match.oxm_list.append(ofp.oxm.in_port(in_port))
555 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x0fff))
556 request = ofp.message.flow_add(
557 table_id=10,
558 cookie=42,
559 match=match,
560 instructions=[
561 ofp.instruction.goto_table(20)
562 ],
563 priority=0)
564 logging.info("Add allow all untag on port %d " %(in_port))
565 ctrl.message_send(request)
macauley_cheng6b311612015-09-04 11:32:27 +0800566
macauley53d90fe2015-08-04 17:34:22 +0800567def 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 +0800568 # table 10: vlan
569 # goto to table 20
570 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
571 match = ofp.match()
572 match.oxm_list.append(ofp.oxm.in_port(of_port))
573 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
macauley7f89d962015-08-06 18:13:48 +0800574
575 actions=[]
576 if vrf!=0:
577 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
578
579 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id)))
580
macauley0f91a3e2015-07-17 18:09:59 +0800581 request = ofp.message.flow_add(
582 table_id=10,
583 cookie=42,
584 match=match,
585 instructions=[
macauley53d90fe2015-08-04 17:34:22 +0800586 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800587 actions=actions
macauley53d90fe2015-08-04 17:34:22 +0800588 ),
589 ofp.instruction.goto_table(20)
macauley0f91a3e2015-07-17 18:09:59 +0800590 ],
591 priority=0)
592 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
593 ctrl.message_send(request)
594
595 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
596 match = ofp.match()
597 match.oxm_list.append(ofp.oxm.in_port(of_port))
598 match.oxm_list.append(ofp.oxm.vlan_vid(0))
macauley53d90fe2015-08-04 17:34:22 +0800599
macauley7f89d962015-08-06 18:13:48 +0800600 actions=[]
601 if vrf!=0:
602 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
603
604 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
605
macauley0f91a3e2015-07-17 18:09:59 +0800606 request = ofp.message.flow_add(
607 table_id=10,
608 cookie=42,
609 match=match,
610 instructions=[
611 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800612 actions=actions
macauley0f91a3e2015-07-17 18:09:59 +0800613 ),
614 ofp.instruction.goto_table(20)
615 ],
616 priority=0)
617 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
618 ctrl.message_send(request)
619
620 if send_barrier:
621 do_barrier(ctrl)
622
623 return request
macauley15909e72015-07-17 15:58:57 +0800624
macauley97557232015-07-16 17:28:07 +0800625def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False):
626 match = ofp.match()
castroflavio21894482015-12-08 15:29:55 -0500627 priority=500
macauleyfddc4662015-07-27 17:40:30 +0800628 if dst_mac!=None:
castroflavio21894482015-12-08 15:29:55 -0500629 priority=1000
macauleyfddc4662015-07-27 17:40:30 +0800630 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
631
macauley97557232015-07-16 17:28:07 +0800632 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauleyfddc4662015-07-27 17:40:30 +0800633
macauley97557232015-07-16 17:28:07 +0800634 request = ofp.message.flow_add(
635 table_id=50,
636 cookie=42,
637 match=match,
638 instructions=[
639 ofp.instruction.write_actions(
640 actions=[
641 ofp.action.group(group_id)]),
642 ofp.instruction.goto_table(60)
643 ],
644 buffer_id=ofp.OFP_NO_BUFFER,
castroflavio21894482015-12-08 15:29:55 -0500645 priority=priority)
macauley97557232015-07-16 17:28:07 +0800646
647 logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac)
648 ctrl.message_send(request)
649
650 if send_barrier:
macauley15909e72015-07-17 15:58:57 +0800651 do_barrier(ctrl)
652
macauley0f91a3e2015-07-17 18:09:59 +0800653 return request
macauleyfddc4662015-07-27 17:40:30 +0800654
655def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False):
656 match = ofp.match()
657 if dst_mac!=None:
658 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
659
660 match.oxm_list.append(ofp.oxm.tunnel_id(vnid))
661 if is_group == True:
662 actions=[ofp.action.group(group_id)]
663 else:
664 actions=[ofp.action.output(group_id)]
665
666 request = ofp.message.flow_add(
667 table_id=50,
668 cookie=42,
669 match=match,
670 instructions=[
671 ofp.instruction.write_actions(
672 actions=actions),
673 ofp.instruction.goto_table(60)
674 ],
675 buffer_id=ofp.OFP_NO_BUFFER,
676 priority=1000)
677
678 logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac)
679 ctrl.message_send(request)
680
681 if send_barrier:
682 do_barrier(ctrl)
683
684 return request
macauley0f91a3e2015-07-17 18:09:59 +0800685
macauley_cheng6b133662015-11-09 13:52:39 +0800686def add_termination_flow(ctrl, in_port, eth_type, dst_mac, vlanid, goto_table=None, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800687 match = ofp.match()
macauley0f91a3e2015-07-17 18:09:59 +0800688 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauleyfddc4662015-07-27 17:40:30 +0800689 if dst_mac[0]&0x01 == 0x01:
690 match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00]))
691 goto_table=40
692 else:
macauley53d90fe2015-08-04 17:34:22 +0800693 if in_port!=0:
694 match.oxm_list.append(ofp.oxm.in_port(in_port))
macauleyfddc4662015-07-27 17:40:30 +0800695 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
696 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauley_cheng6b133662015-11-09 13:52:39 +0800697 if goto_table == None:
698 goto_table=30
macauley0f91a3e2015-07-17 18:09:59 +0800699
700 request = ofp.message.flow_add(
701 table_id=20,
702 cookie=42,
703 match=match,
704 instructions=[
705 ofp.instruction.goto_table(goto_table)
706 ],
707 buffer_id=ofp.OFP_NO_BUFFER,
708 priority=1)
709
710 logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac)
711 ctrl.message_send(request)
712
713 if send_barrier:
714 do_barrier(ctrl)
715
716 return request
717
macauley53d90fe2015-08-04 17:34:22 +0800718def add_unicast_routing_flow(ctrl, eth_type, dst_ip, mask, action_group_id, vrf=0, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800719 match = ofp.match()
720 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauley53d90fe2015-08-04 17:34:22 +0800721 if vrf != 0:
722 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
723
macauleyf8b1acd2015-07-23 15:13:13 +0800724 if mask!=0:
725 match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask))
726 else:
727 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
macauley0f91a3e2015-07-17 18:09:59 +0800728
macauley53d90fe2015-08-04 17:34:22 +0800729
macauley0f91a3e2015-07-17 18:09:59 +0800730 request = ofp.message.flow_add(
731 table_id=30,
732 cookie=42,
733 match=match,
734 instructions=[
735 ofp.instruction.write_actions(
736 actions=[ofp.action.group(action_group_id)]),
737 ofp.instruction.goto_table(60)
738 ],
739 buffer_id=ofp.OFP_NO_BUFFER,
740 priority=1)
741
742 logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip)
743 ctrl.message_send(request)
744
745 if send_barrier:
746 do_barrier(ctrl)
747
macauleyfddc4662015-07-27 17:40:30 +0800748 return request
Flavio Castrod8f8af22015-12-02 18:19:26 -0500749
Flavio Castro167f5bd2015-12-02 19:33:53 -0500750def add_mpls_flow(ctrl, action_group_id, label=100 ,ethertype=0x0800, bos=1, send_barrier=False):
Flavio Castrod8f8af22015-12-02 18:19:26 -0500751 match = ofp.match()
752 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
753 match.oxm_list.append(ofp.oxm.mpls_label(label))
754 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
755 actions = [ofp.action.dec_mpls_ttl(),
756 ofp.action.copy_ttl_in(),
757 ofp.action.pop_mpls(ethertype)]
758 request = ofp.message.flow_add(
759 table_id=24,
760 cookie=43,
761 match=match,
762 instructions=[
763 ofp.instruction.apply_actions(
764 actions=actions
765 ),
766 ofp.instruction.write_actions(
767 actions=[ofp.action.group(action_group_id)]),
768 ofp.instruction.goto_table(60)
769 ],
770 buffer_id=ofp.OFP_NO_BUFFER,
771 priority=1)
772
773 logging.info("Inserting MPLS flow , label %ld", label)
774 ctrl.message_send(request)
775
776 if send_barrier:
777 do_barrier(ctrl)
778
779 return request
780
781
macauleyfddc4662015-07-27 17:40:30 +0800782def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False):
783 match = ofp.match()
784 match.oxm_list.append(ofp.oxm.eth_type(0x0800))
785 match.oxm_list.append(ofp.oxm.vlan_vid(vlan_id))
786 if src_ip_mask!=0:
787 match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask))
788 else:
789 match.oxm_list.append(ofp.oxm.ipv4_src(src_ip))
790
791 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
792
793 request = ofp.message.flow_add(
794 table_id=40,
795 cookie=42,
796 match=match,
797 instructions=[
798 ofp.instruction.write_actions(
799 actions=[ofp.action.group(action_group_id)]),
800 ofp.instruction.goto_table(60)
801 ],
802 buffer_id=ofp.OFP_NO_BUFFER,
803 priority=1)
804
805 logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask)
806 ctrl.message_send(request)
807
808 if send_barrier:
809 do_barrier(ctrl)
810
811 return request
macauley_cheng6b133662015-11-09 13:52:39 +0800812
813#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 +0800814def 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 +0800815 match = ofp.match()
816 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
817 match.oxm_list.append(ofp.oxm.ipv4_dst(ip_dst))
818 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
819 match.oxm_list.append(ofp.oxm.tcp_dst(tcp_dst))
820
821 request = ofp.message.flow_add(
822 table_id=28,
823 cookie=42,
824 match=match,
825 instructions=[
826 ofp.instruction.write_actions(
827 actions=[ofp.action.set_field(ofp.oxm.ipv4_dst(set_ip_dst)),
828 ofp.action.set_field(ofp.oxm.tcp_dst(set_tcp_dst)),
829 ofp.action.group(action_group_id)]),
830 ofp.instruction.goto_table(60)
831 ],
832 buffer_id=ofp.OFP_NO_BUFFER,
833 priority=1)
834 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)
835 ctrl.message_send(request)
836 return request
macauley_chengeffc20a2015-11-09 16:14:56 +0800837
838#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
839def add_snat_flow(ctrl, eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src):
840 match = ofp.match()
841 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
842 match.oxm_list.append(ofp.oxm.ipv4_src(ip_src))
843 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
844 match.oxm_list.append(ofp.oxm.tcp_src(tcp_src))
845
846 request = ofp.message.flow_add(
847 table_id=29,
848 cookie=42,
849 match=match,
850 instructions=[
851 ofp.instruction.write_actions(
852 actions=[ofp.action.set_field(ofp.oxm.ipv4_src(set_ip_src)),
853 ofp.action.set_field(ofp.oxm.tcp_src(set_tcp_src))]),
854 ofp.instruction.goto_table(30)
855 ],
856 buffer_id=ofp.OFP_NO_BUFFER,
857 priority=1)
858 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)
859 ctrl.message_send(request)
860 return request
macauley_cheng6b133662015-11-09 13:52:39 +0800861
macauleyfddc4662015-07-27 17:40:30 +0800862def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'):
863 """
864 Command Example:
865 of-agent vtap 10001 ethernet 1/1 vid 1
866 of-agent vtp 10001 vni 10
867 """
868 if vlan != 0:
869 config_vtap_xml="""
870 <config>
871 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
872 <id>capable-switch-1</id>
873 <resources>
874 <port xc:operation="OPERATION">
875 <resource-id >LPORT</resource-id>
876 <features>
877 <current>
878 <rate>10Gb</rate>
879 <medium>fiber</medium>
880 <pause>symmetric</pause>
881 </current>
882 <advertised>
883 <rate>10Gb</rate>
884 <rate>100Gb</rate>
885 <medium>fiber</medium>
886 <pause>symmetric</pause>
887 </advertised>
888 <supported>
889 <rate>10Gb</rate>
890 <rate>100Gb</rate>
891 <medium>fiber</medium>
892 <pause>symmetric</pause>
893 </supported>
894 <advertised-peer>
895 <rate>10Gb</rate>
896 <rate>100Gb</rate>
897 <medium>fiber</medium>
898 <pause>symmetric</pause>
899 </advertised-peer>
900 </features>
901 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
902 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
903 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
904 <ofdpa10:vni>VNID</ofdpa10:vni>
905 </ofdpa10:vtap>
906 </port>
907 </resources>
908 <logical-switches>
909 <switch>
910 <id>DATAPATH_ID</id>
911 <datapath-id>DATAPATH_ID</datapath-id>
912 <resources>
913 <port xc:operation="OPERATION">LPORT</port>
914 </resources>
915 </switch>
916 </logical-switches>
917 </capable-switch>
918 </config>
919 """
920 else:
921 config_vtap_xml="""
922 <config>
923 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
924 <id>capable-switch-1</id>
925 <resources>
926 <port xc:operation="OPERATION">
927 <resource-id >LPORT</resource-id>
928 <features>
929 <current>
930 <rate>10Gb</rate>
931 <medium>fiber</medium>
932 <pause>symmetric</pause>
933 </current>
934 <advertised>
935 <rate>10Gb</rate>
936 <rate>100Gb</rate>
937 <medium>fiber</medium>
938 <pause>symmetric</pause>
939 </advertised>
940 <supported>
941 <rate>10Gb</rate>
942 <rate>100Gb</rate>
943 <medium>fiber</medium>
944 <pause>symmetric</pause>
945 </supported>
946 <advertised-peer>
947 <rate>10Gb</rate>
948 <rate>100Gb</rate>
949 <medium>fiber</medium>
950 <pause>symmetric</pause>
951 </advertised-peer>
952 </features>
953 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
954 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
955 <ofdpa10:vni>VNID</ofdpa10:vni>
956 </ofdpa10:vtap>
957 </port>
958 </resources>
959 <logical-switches>
960 <switch>
961 <id>DATAPATH_ID</id>
962 <datapath-id>DATAPATH_ID</datapath-id>
963 <resources>
964 <port xc:operation="OPERATION">LPORT</port>
965 </resources>
966 </switch>
967 </logical-switches>
968 </capable-switch>
969 </config>
970 """
971 str_datapath_id_f= "{:016x}".format(dp_id)
972 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
973 config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +0800974 config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport)))
macauleyfddc4662015-07-27 17:40:30 +0800975 config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port))
976 config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan))
977 config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid))
978 config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation))
979 return config_vtap_xml
980
981def get_vtep_lport_config_xml(dp_id, lport, src_ip, dst_ip, next_hop_id, vnid, udp_src_port=6633, ttl=25, operation='merge'):
982 """
983 Command Example:
984 of-agent vtep 10002 source user-input-src-ip destination user-input-dst-ip udp-source-port 6633 nexthop 2 ttl 25
985 of-agent vtp 10001 vni 10
986 """
987
988 config_vtep_xml="""
989 <config>
990 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
991 <id>capable-switch-1</id>
992 <resources>
993 <port xc:operation="OPERATION">
994 <resource-id>LPORT</resource-id>
995 <features>
996 <current>
997 <rate>10Gb</rate>
998 <medium>fiber</medium>
999 <pause>symmetric</pause>
1000 </current>
1001 <advertised>
1002 <rate>10Gb</rate>
1003 <rate>100Gb</rate>
1004 <medium>fiber</medium>
1005 <pause>symmetric</pause>
1006 </advertised>
1007 <supported>
1008 <rate>10Gb</rate>
1009 <rate>100Gb</rate>
1010 <medium>fiber</medium>
1011 <pause>symmetric</pause>
1012 </supported>
1013 <advertised-peer>
1014 <rate>10Gb</rate>
1015 <rate>100Gb</rate>
1016 <medium>fiber</medium>
1017 <pause>symmetric</pause>
1018 </advertised-peer>
1019 </features>
1020 <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01">
1021 <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip>
1022 <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip>
1023 <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port>
macauley25999cf2015-08-07 17:03:24 +08001024 <ofdpa10:vni xc:operation="OPERATION">
1025 <ofdpa10:id>VNID</ofdpa10:id>
1026 </ofdpa10:vni>
macauleyfddc4662015-07-27 17:40:30 +08001027 <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id>
1028 <ofdpa10:ttl>TTL</ofdpa10:ttl>
1029 </ofdpa10:vtep>
1030 </port>
1031 </resources>
1032 <logical-switches>
1033 <switch>
1034 <id>DATAPATH_ID</id>
1035 <datapath-id>DATAPATH_ID</datapath-id>
1036 <resources>
1037 <port xc:operation="OPERATION">LPORT</port>
1038 </resources>
1039 </switch>
1040 </logical-switches>
1041 </capable-switch>
1042 </config>
1043 """
1044 str_datapath_id_f= "{:016x}".format(dp_id)
1045 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1046 config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +08001047 config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport)))
macauleyfddc4662015-07-27 17:40:30 +08001048 config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip))
1049 config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip))
1050 config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port))
1051 config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1052 config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl))
1053 config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid))
1054 config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation))
1055
1056 return config_vtep_xml
1057
1058def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'):
1059 #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2
1060 config_nexthop_xml="""
1061 <config>
1062 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1063 <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1064 <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id>
1065 <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac>
1066 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1067 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1068 </ofdpa10:next-hop>
1069 </of11-config:capable-switch>
1070 </config>
1071 """
1072 config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan))
1073 config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port))
1074 config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1075 config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac))
1076 config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation))
1077 return config_nexthop_xml
1078
1079def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'):
1080 #of-agent vni 10 multicast 224.1.1.1 nexthop 20
1081 if mcast_ipv4!=None:
1082 config_vni_xml="""
1083 <config>
1084 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1085 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1086 <ofdpa10:id>VNID</ofdpa10:id>
1087 <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group>
1088 <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id>
1089 </ofdpa10:vni>
1090 </of11-config:capable-switch>
1091 </config>
1092 """
1093 config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1094 config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4))
1095 else:
1096 config_vni_xml="""
1097 <config>
1098 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1099 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1100 <ofdpa10:id>VNID</ofdpa10:id>
1101 </ofdpa10:vni>
1102 </of11-config:capable-switch>
1103 </config>
1104 """
1105
1106 config_vni_xml=config_vni_xml.replace("VNID", str(vni_id))
1107 config_vni_xml=config_vni_xml.replace("OPERATION", str(operation))
1108 return config_vni_xml
1109
1110def get_featureReplay(self):
1111 req = ofp.message.features_request()
1112 res, raw = self.controller.transact(req)
1113 self.assertIsNotNone(res, "Did not receive a response from the DUT.")
1114 self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY,
1115 ("Unexpected packet type %d received in response to "
1116 "OFPT_FEATURES_REQUEST") % res.type)
1117 return res
1118
1119def send_edit_config(switch_ip, xml, target='runing'):
1120 NETCONF_ACCOUNT="netconfuser"
1121 NETCONF_PASSWD="netconfuser"
1122 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1123 try:
1124 m.edit_config(target='running',
1125 config=xml,
1126 default_operation='merge',
1127 error_option='stop-on-error')
1128
1129 except Exception as e:
1130 logging.info("Fail to set xml %s", xml)
1131 return False
1132
1133 #return m.get_config(source='running').data_xml
1134 return True
1135
1136def send_delete_config(switch_ip, xml, target='runing'):
1137 NETCONF_ACCOUNT="netconfuser"
1138 NETCONF_PASSWD="netconfuser"
1139 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1140 try:
1141 m.edit_config(target='running',
1142 config=xml,
1143 default_operation='delete',
1144 error_option='stop-on-error')
1145
1146 except Exception as e:
1147 logging.info("Fail to set xml %s", xml)
1148 return False
1149
1150 #return m.get_config(source='running').data_xml
1151 return True
1152
1153def get_edit_config(switch_ip, target='runing'):
1154 NETCONF_ACCOUNT="netconfuser"
1155 NETCONF_PASSWD="netconfuser"
1156 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 +08001157 return m.get_config(source='running').data_xml
macauleydbff3272015-07-30 14:07:16 +08001158
macauley_cheng67da9262015-08-31 15:18:41 +08001159
1160"""
1161MPLS
1162"""
1163
1164OFDPA_MPLS_SUBTYPE_SHIFT=24
1165OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1
1166OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2
1167OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3
1168OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4
1169OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5
1170OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6
1171OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8
1172OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10
1173
1174def encode_mpls_interface_group_id(subtype, index):
1175 index=index&0x00ffffff
1176 assert(subtype==0)
1177 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1178
1179def encode_mpls_label_group_id(subtype, index):
1180 index=index&0x00ffffff
1181 assert(subtype <=5 or subtype==0)
1182 #1: l2 vpn label
1183 #2: l3 vpn label
1184 #3: mpls tunnel label 1
1185 #4: mpls tunnel lable 2
1186 #5: mpls swap label
1187 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1188
1189def encode_mpls_forwarding_group_id(subtype, index):
1190 index=index&0x00ffffff
1191 assert(subtype==6 or subtype==8 or subtype==10)
1192 return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1193
1194
1195def add_mpls_intf_group(ctrl, ref_gid, dst_mac, src_mac, vid, index, subtype=0):
1196 action=[]
1197 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
1198 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
1199 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vid)))
1200 action.append(ofp.action.group(ref_gid))
1201
1202 buckets = [ofp.bucket(actions=action)]
1203
1204 mpls_group_id =encode_mpls_interface_group_id(subtype, index)
1205 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1206 group_id=mpls_group_id,
1207 buckets=buckets
1208 )
1209 ctrl.message_send(request)
1210 return mpls_group_id, request
1211
1212def add_mpls_label_group(ctrl, subtype, index, ref_gid,
1213 lmep_id=-1,
1214 qos_index=-1,
1215 push_l2_header=False,
1216 push_vlan=False,
1217 push_mpls_header=False,
1218 push_cw=False,
1219 set_mpls_label=None,
1220 set_bos=None,
1221 set_tc=None,
1222 set_tc_from_table=False,
1223 cpy_tc_outward=False,
1224 set_ttl=None,
1225 cpy_ttl_outward=False,
1226 oam_lm_tx_count=False,
1227 set_pri_from_table=False
1228 ):
1229 """
1230 @ref_gid: only can be mpls intf group or mpls tunnel label 1/2 group
1231 """
1232 action=[]
1233
1234 if push_vlan== True:
1235 action.append(ofp.action.push_vlan(0x8100))
1236 if push_mpls_header== True:
1237 action.append(ofp.action.push_mpls(0x8847))
1238 if set_mpls_label != None:
1239 action.append(ofp.action.set_field(ofp.oxm.mpls_label(set_mpls_label)))
1240 if set_bos != None:
1241 action.append(ofp.action.set_field(ofp.oxm.mpls_bos(set_bos)))
1242 if set_tc != None:
1243 assert(set_tc_from_table==False)
1244 action.append(ofp.action.set_field(ofp.oxm.mpls_tc(set_tc)))
1245 if set_ttl != None:
1246 action.append(ofp.action.set_mpls_ttl(set_ttl))
1247 if cpy_ttl_outward == True:
1248 action.append(ofp.action.copy_ttl_out())
1249 """
1250 ofdpa experimenter
1251 """
1252 if push_l2_header== True:
1253 action.append(ofp.action.ofdpa_push_l2_header())
1254 if set_tc_from_table== True:
1255 assert(qos_index>=0)
1256 assert(set_tc == None)
1257 action.append(ofp.action.ofdpa_set_tc_from_table(qos_index))
1258 if cpy_tc_outward == True:
1259 action.append(ofp.action.ofdpa_copy_tc_out())
1260 if oam_lm_tx_count == True:
1261 assert(qos_index>=0 and lmep_id>=0)
1262 action.append(ofp.action.ofdpa_oam_lm_tx_count(lmep_id, qos_index))
1263 if set_pri_from_table == True:
1264 assert(qos_index>=0)
1265 action.append(ofp.action.ofdpa_set_qos_from_table(qos_index))
1266 if push_cw == True:
1267 action.append(ofp.action.ofdpa_push_cw())
1268
1269 action.append(ofp.action.group(ref_gid))
1270 buckets = [ofp.bucket(actions=action)]
1271
1272 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1273 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1274 group_id=mpls_group_id,
1275 buckets=buckets
1276 )
1277 ctrl.message_send(request)
1278
1279 return mpls_group_id, request
1280
1281def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids,
1282 watch_port=None,
macauley_chengd17ce512015-08-31 17:45:51 +08001283 watch_group=ofp.OFPP_ANY,
macauley_cheng67da9262015-08-31 15:18:41 +08001284 push_vlan=None,
macauley_chengd17ce512015-08-31 17:45:51 +08001285 pop_vlan=None,
macauley_cheng67da9262015-08-31 15:18:41 +08001286 set_vid=None):
1287 assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP
1288 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP
1289 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG)
macauley_chengd17ce512015-08-31 17:45:51 +08001290
macauley_cheng67da9262015-08-31 15:18:41 +08001291 buckets=[]
1292 if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP:
macauley_chengd17ce512015-08-31 17:45:51 +08001293 group_type = ofp.OFPGT_FF
macauley_cheng67da9262015-08-31 15:18:41 +08001294 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001295 action=[]
1296 action.append(ofp.action.group(gid))
1297 buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action))
1298
macauley_cheng67da9262015-08-31 15:18:41 +08001299 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP:
1300 group_type = ofp.OFPGT_SELECT
1301 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001302 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08001303 action.append(ofp.action.group(gid))
1304 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001305
macauley_cheng67da9262015-08-31 15:18:41 +08001306 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG:
1307 group_type = ofp.OFPGT_INDIRECT
macauley_chengd17ce512015-08-31 17:45:51 +08001308 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08001309 if set_vid!=None:
1310 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(set_vid)))
1311 if push_vlan!=None:
1312 action.append(ofp.action.push_vlan(push_vlan))
1313 if pop_vlan!=None:
macauley_chengd17ce512015-08-31 17:45:51 +08001314 action.append(ofp.action.pop_vlan())
macauley_cheng67da9262015-08-31 15:18:41 +08001315 action.append(ofp.action.group(ref_gids[0]))
1316 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001317
1318 mpls_group_id = encode_mpls_forwarding_group_id(subtype, index)
macauley_cheng67da9262015-08-31 15:18:41 +08001319 request = ofp.message.group_add(group_type=group_type,
macauley_cheng67da9262015-08-31 15:18:41 +08001320 group_id=mpls_group_id,
1321 buckets=buckets
1322 )
1323 ctrl.message_send(request)
1324 return mpls_group_id, request
macauley_chengd17ce512015-08-31 17:45:51 +08001325
1326
macauley_cheng67da9262015-08-31 15:18:41 +08001327"""
1328dislay
1329"""
macauleydbff3272015-07-30 14:07:16 +08001330def print_current_table_flow_stat(ctrl, table_id=0xff):
1331 stat_req=ofp.message.flow_stats_request()
1332 response, pkt = ctrl.transact(stat_req)
1333 if response == None:
1334 print "no response"
1335 return None
1336 print len(response.entries)
1337 for obj in response.entries:
1338 print "match ", obj.match
1339 print "cookie", obj.cookie
1340 print "priority", obj.priority
1341 print "idle_timeout", obj.idle_timeout
1342 print "hard_timeout", obj.hard_timeout
1343 #obj.actions
Flavio Castro167f5bd2015-12-02 19:33:53 -05001344 print "packet count: %lx"%obj.packet_count