blob: 1eb2946c736a8cc191f917fe0969bdc849568c1c [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
macauley97557232015-07-16 17:28:07 +0800131def 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
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
macauley0f91a3e2015-07-17 18:09:59 +0800166def 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
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
551
macauley53d90fe2015-08-04 17:34:22 +0800552def 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 +0800553 # table 10: vlan
554 # goto to table 20
555 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
556 match = ofp.match()
557 match.oxm_list.append(ofp.oxm.in_port(of_port))
558 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
macauley7f89d962015-08-06 18:13:48 +0800559
560 actions=[]
561 if vrf!=0:
562 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
563
564 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id)))
565
macauley0f91a3e2015-07-17 18:09:59 +0800566 request = ofp.message.flow_add(
567 table_id=10,
568 cookie=42,
569 match=match,
570 instructions=[
macauley53d90fe2015-08-04 17:34:22 +0800571 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800572 actions=actions
macauley53d90fe2015-08-04 17:34:22 +0800573 ),
574 ofp.instruction.goto_table(20)
macauley0f91a3e2015-07-17 18:09:59 +0800575 ],
576 priority=0)
577 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
578 ctrl.message_send(request)
579
580 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
581 match = ofp.match()
582 match.oxm_list.append(ofp.oxm.in_port(of_port))
583 match.oxm_list.append(ofp.oxm.vlan_vid(0))
macauley53d90fe2015-08-04 17:34:22 +0800584
macauley7f89d962015-08-06 18:13:48 +0800585 actions=[]
586 if vrf!=0:
587 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
588
589 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
590
macauley0f91a3e2015-07-17 18:09:59 +0800591 request = ofp.message.flow_add(
592 table_id=10,
593 cookie=42,
594 match=match,
595 instructions=[
596 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800597 actions=actions
macauley0f91a3e2015-07-17 18:09:59 +0800598 ),
599 ofp.instruction.goto_table(20)
600 ],
601 priority=0)
602 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
603 ctrl.message_send(request)
604
605 if send_barrier:
606 do_barrier(ctrl)
607
608 return request
macauley15909e72015-07-17 15:58:57 +0800609
macauley97557232015-07-16 17:28:07 +0800610def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False):
611 match = ofp.match()
macauleyfddc4662015-07-27 17:40:30 +0800612 if dst_mac!=None:
613 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
614
macauley97557232015-07-16 17:28:07 +0800615 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauleyfddc4662015-07-27 17:40:30 +0800616
macauley97557232015-07-16 17:28:07 +0800617 request = ofp.message.flow_add(
618 table_id=50,
619 cookie=42,
620 match=match,
621 instructions=[
622 ofp.instruction.write_actions(
623 actions=[
624 ofp.action.group(group_id)]),
625 ofp.instruction.goto_table(60)
626 ],
627 buffer_id=ofp.OFP_NO_BUFFER,
628 priority=1000)
629
630 logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac)
631 ctrl.message_send(request)
632
633 if send_barrier:
macauley15909e72015-07-17 15:58:57 +0800634 do_barrier(ctrl)
635
macauley0f91a3e2015-07-17 18:09:59 +0800636 return request
macauleyfddc4662015-07-27 17:40:30 +0800637
638def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False):
639 match = ofp.match()
640 if dst_mac!=None:
641 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
642
643 match.oxm_list.append(ofp.oxm.tunnel_id(vnid))
644 if is_group == True:
645 actions=[ofp.action.group(group_id)]
646 else:
647 actions=[ofp.action.output(group_id)]
648
649 request = ofp.message.flow_add(
650 table_id=50,
651 cookie=42,
652 match=match,
653 instructions=[
654 ofp.instruction.write_actions(
655 actions=actions),
656 ofp.instruction.goto_table(60)
657 ],
658 buffer_id=ofp.OFP_NO_BUFFER,
659 priority=1000)
660
661 logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac)
662 ctrl.message_send(request)
663
664 if send_barrier:
665 do_barrier(ctrl)
666
667 return request
macauley0f91a3e2015-07-17 18:09:59 +0800668
macauley_cheng6b133662015-11-09 13:52:39 +0800669def add_termination_flow(ctrl, in_port, eth_type, dst_mac, vlanid, goto_table=None, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800670 match = ofp.match()
macauley0f91a3e2015-07-17 18:09:59 +0800671 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauleyfddc4662015-07-27 17:40:30 +0800672 if dst_mac[0]&0x01 == 0x01:
673 match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00]))
674 goto_table=40
675 else:
macauley53d90fe2015-08-04 17:34:22 +0800676 if in_port!=0:
677 match.oxm_list.append(ofp.oxm.in_port(in_port))
macauleyfddc4662015-07-27 17:40:30 +0800678 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
679 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauley_cheng6b133662015-11-09 13:52:39 +0800680 if goto_table == None:
681 goto_table=30
macauley0f91a3e2015-07-17 18:09:59 +0800682
683 request = ofp.message.flow_add(
684 table_id=20,
685 cookie=42,
686 match=match,
687 instructions=[
688 ofp.instruction.goto_table(goto_table)
689 ],
690 buffer_id=ofp.OFP_NO_BUFFER,
691 priority=1)
692
693 logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac)
694 ctrl.message_send(request)
695
696 if send_barrier:
697 do_barrier(ctrl)
698
699 return request
700
macauley53d90fe2015-08-04 17:34:22 +0800701def add_unicast_routing_flow(ctrl, eth_type, dst_ip, mask, action_group_id, vrf=0, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800702 match = ofp.match()
703 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauley53d90fe2015-08-04 17:34:22 +0800704 if vrf != 0:
705 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
706
macauleyf8b1acd2015-07-23 15:13:13 +0800707 if mask!=0:
708 match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask))
709 else:
710 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
macauley0f91a3e2015-07-17 18:09:59 +0800711
macauley53d90fe2015-08-04 17:34:22 +0800712
macauley0f91a3e2015-07-17 18:09:59 +0800713 request = ofp.message.flow_add(
714 table_id=30,
715 cookie=42,
716 match=match,
717 instructions=[
718 ofp.instruction.write_actions(
719 actions=[ofp.action.group(action_group_id)]),
720 ofp.instruction.goto_table(60)
721 ],
722 buffer_id=ofp.OFP_NO_BUFFER,
723 priority=1)
724
725 logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip)
726 ctrl.message_send(request)
727
728 if send_barrier:
729 do_barrier(ctrl)
730
macauleyfddc4662015-07-27 17:40:30 +0800731 return request
Flavio Castrod8f8af22015-12-02 18:19:26 -0500732
733def add_mpls_flow(ctrl, action_group_id, label=100 ,ethertype=0x0800, bos=True, send_barrier=False):
734 match = ofp.match()
735 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
736 match.oxm_list.append(ofp.oxm.mpls_label(label))
737 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
738 actions = [ofp.action.dec_mpls_ttl(),
739 ofp.action.copy_ttl_in(),
740 ofp.action.pop_mpls(ethertype)]
741 request = ofp.message.flow_add(
742 table_id=24,
743 cookie=43,
744 match=match,
745 instructions=[
746 ofp.instruction.apply_actions(
747 actions=actions
748 ),
749 ofp.instruction.write_actions(
750 actions=[ofp.action.group(action_group_id)]),
751 ofp.instruction.goto_table(60)
752 ],
753 buffer_id=ofp.OFP_NO_BUFFER,
754 priority=1)
755
756 logging.info("Inserting MPLS flow , label %ld", label)
757 ctrl.message_send(request)
758
759 if send_barrier:
760 do_barrier(ctrl)
761
762 return request
763
764
macauleyfddc4662015-07-27 17:40:30 +0800765def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False):
766 match = ofp.match()
767 match.oxm_list.append(ofp.oxm.eth_type(0x0800))
768 match.oxm_list.append(ofp.oxm.vlan_vid(vlan_id))
769 if src_ip_mask!=0:
770 match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask))
771 else:
772 match.oxm_list.append(ofp.oxm.ipv4_src(src_ip))
773
774 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
775
776 request = ofp.message.flow_add(
777 table_id=40,
778 cookie=42,
779 match=match,
780 instructions=[
781 ofp.instruction.write_actions(
782 actions=[ofp.action.group(action_group_id)]),
783 ofp.instruction.goto_table(60)
784 ],
785 buffer_id=ofp.OFP_NO_BUFFER,
786 priority=1)
787
788 logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask)
789 ctrl.message_send(request)
790
791 if send_barrier:
792 do_barrier(ctrl)
793
794 return request
macauley_cheng6b133662015-11-09 13:52:39 +0800795
796#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 +0800797def 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 +0800798 match = ofp.match()
799 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
800 match.oxm_list.append(ofp.oxm.ipv4_dst(ip_dst))
801 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
802 match.oxm_list.append(ofp.oxm.tcp_dst(tcp_dst))
803
804 request = ofp.message.flow_add(
805 table_id=28,
806 cookie=42,
807 match=match,
808 instructions=[
809 ofp.instruction.write_actions(
810 actions=[ofp.action.set_field(ofp.oxm.ipv4_dst(set_ip_dst)),
811 ofp.action.set_field(ofp.oxm.tcp_dst(set_tcp_dst)),
812 ofp.action.group(action_group_id)]),
813 ofp.instruction.goto_table(60)
814 ],
815 buffer_id=ofp.OFP_NO_BUFFER,
816 priority=1)
817 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)
818 ctrl.message_send(request)
819 return request
macauley_chengeffc20a2015-11-09 16:14:56 +0800820
821#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
822def add_snat_flow(ctrl, eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src):
823 match = ofp.match()
824 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
825 match.oxm_list.append(ofp.oxm.ipv4_src(ip_src))
826 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
827 match.oxm_list.append(ofp.oxm.tcp_src(tcp_src))
828
829 request = ofp.message.flow_add(
830 table_id=29,
831 cookie=42,
832 match=match,
833 instructions=[
834 ofp.instruction.write_actions(
835 actions=[ofp.action.set_field(ofp.oxm.ipv4_src(set_ip_src)),
836 ofp.action.set_field(ofp.oxm.tcp_src(set_tcp_src))]),
837 ofp.instruction.goto_table(30)
838 ],
839 buffer_id=ofp.OFP_NO_BUFFER,
840 priority=1)
841 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)
842 ctrl.message_send(request)
843 return request
macauley_cheng6b133662015-11-09 13:52:39 +0800844
macauleyfddc4662015-07-27 17:40:30 +0800845def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'):
846 """
847 Command Example:
848 of-agent vtap 10001 ethernet 1/1 vid 1
849 of-agent vtp 10001 vni 10
850 """
851 if vlan != 0:
852 config_vtap_xml="""
853 <config>
854 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
855 <id>capable-switch-1</id>
856 <resources>
857 <port xc:operation="OPERATION">
858 <resource-id >LPORT</resource-id>
859 <features>
860 <current>
861 <rate>10Gb</rate>
862 <medium>fiber</medium>
863 <pause>symmetric</pause>
864 </current>
865 <advertised>
866 <rate>10Gb</rate>
867 <rate>100Gb</rate>
868 <medium>fiber</medium>
869 <pause>symmetric</pause>
870 </advertised>
871 <supported>
872 <rate>10Gb</rate>
873 <rate>100Gb</rate>
874 <medium>fiber</medium>
875 <pause>symmetric</pause>
876 </supported>
877 <advertised-peer>
878 <rate>10Gb</rate>
879 <rate>100Gb</rate>
880 <medium>fiber</medium>
881 <pause>symmetric</pause>
882 </advertised-peer>
883 </features>
884 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
885 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
886 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
887 <ofdpa10:vni>VNID</ofdpa10:vni>
888 </ofdpa10:vtap>
889 </port>
890 </resources>
891 <logical-switches>
892 <switch>
893 <id>DATAPATH_ID</id>
894 <datapath-id>DATAPATH_ID</datapath-id>
895 <resources>
896 <port xc:operation="OPERATION">LPORT</port>
897 </resources>
898 </switch>
899 </logical-switches>
900 </capable-switch>
901 </config>
902 """
903 else:
904 config_vtap_xml="""
905 <config>
906 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
907 <id>capable-switch-1</id>
908 <resources>
909 <port xc:operation="OPERATION">
910 <resource-id >LPORT</resource-id>
911 <features>
912 <current>
913 <rate>10Gb</rate>
914 <medium>fiber</medium>
915 <pause>symmetric</pause>
916 </current>
917 <advertised>
918 <rate>10Gb</rate>
919 <rate>100Gb</rate>
920 <medium>fiber</medium>
921 <pause>symmetric</pause>
922 </advertised>
923 <supported>
924 <rate>10Gb</rate>
925 <rate>100Gb</rate>
926 <medium>fiber</medium>
927 <pause>symmetric</pause>
928 </supported>
929 <advertised-peer>
930 <rate>10Gb</rate>
931 <rate>100Gb</rate>
932 <medium>fiber</medium>
933 <pause>symmetric</pause>
934 </advertised-peer>
935 </features>
936 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
937 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
938 <ofdpa10:vni>VNID</ofdpa10:vni>
939 </ofdpa10:vtap>
940 </port>
941 </resources>
942 <logical-switches>
943 <switch>
944 <id>DATAPATH_ID</id>
945 <datapath-id>DATAPATH_ID</datapath-id>
946 <resources>
947 <port xc:operation="OPERATION">LPORT</port>
948 </resources>
949 </switch>
950 </logical-switches>
951 </capable-switch>
952 </config>
953 """
954 str_datapath_id_f= "{:016x}".format(dp_id)
955 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
956 config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +0800957 config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport)))
macauleyfddc4662015-07-27 17:40:30 +0800958 config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port))
959 config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan))
960 config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid))
961 config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation))
962 return config_vtap_xml
963
964def get_vtep_lport_config_xml(dp_id, lport, src_ip, dst_ip, next_hop_id, vnid, udp_src_port=6633, ttl=25, operation='merge'):
965 """
966 Command Example:
967 of-agent vtep 10002 source user-input-src-ip destination user-input-dst-ip udp-source-port 6633 nexthop 2 ttl 25
968 of-agent vtp 10001 vni 10
969 """
970
971 config_vtep_xml="""
972 <config>
973 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
974 <id>capable-switch-1</id>
975 <resources>
976 <port xc:operation="OPERATION">
977 <resource-id>LPORT</resource-id>
978 <features>
979 <current>
980 <rate>10Gb</rate>
981 <medium>fiber</medium>
982 <pause>symmetric</pause>
983 </current>
984 <advertised>
985 <rate>10Gb</rate>
986 <rate>100Gb</rate>
987 <medium>fiber</medium>
988 <pause>symmetric</pause>
989 </advertised>
990 <supported>
991 <rate>10Gb</rate>
992 <rate>100Gb</rate>
993 <medium>fiber</medium>
994 <pause>symmetric</pause>
995 </supported>
996 <advertised-peer>
997 <rate>10Gb</rate>
998 <rate>100Gb</rate>
999 <medium>fiber</medium>
1000 <pause>symmetric</pause>
1001 </advertised-peer>
1002 </features>
1003 <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01">
1004 <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip>
1005 <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip>
1006 <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port>
macauley25999cf2015-08-07 17:03:24 +08001007 <ofdpa10:vni xc:operation="OPERATION">
1008 <ofdpa10:id>VNID</ofdpa10:id>
1009 </ofdpa10:vni>
macauleyfddc4662015-07-27 17:40:30 +08001010 <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id>
1011 <ofdpa10:ttl>TTL</ofdpa10:ttl>
1012 </ofdpa10:vtep>
1013 </port>
1014 </resources>
1015 <logical-switches>
1016 <switch>
1017 <id>DATAPATH_ID</id>
1018 <datapath-id>DATAPATH_ID</datapath-id>
1019 <resources>
1020 <port xc:operation="OPERATION">LPORT</port>
1021 </resources>
1022 </switch>
1023 </logical-switches>
1024 </capable-switch>
1025 </config>
1026 """
1027 str_datapath_id_f= "{:016x}".format(dp_id)
1028 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1029 config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +08001030 config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport)))
macauleyfddc4662015-07-27 17:40:30 +08001031 config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip))
1032 config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip))
1033 config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port))
1034 config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1035 config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl))
1036 config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid))
1037 config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation))
1038
1039 return config_vtep_xml
1040
1041def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'):
1042 #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2
1043 config_nexthop_xml="""
1044 <config>
1045 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1046 <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1047 <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id>
1048 <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac>
1049 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1050 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1051 </ofdpa10:next-hop>
1052 </of11-config:capable-switch>
1053 </config>
1054 """
1055 config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan))
1056 config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port))
1057 config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1058 config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac))
1059 config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation))
1060 return config_nexthop_xml
1061
1062def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'):
1063 #of-agent vni 10 multicast 224.1.1.1 nexthop 20
1064 if mcast_ipv4!=None:
1065 config_vni_xml="""
1066 <config>
1067 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1068 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1069 <ofdpa10:id>VNID</ofdpa10:id>
1070 <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group>
1071 <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id>
1072 </ofdpa10:vni>
1073 </of11-config:capable-switch>
1074 </config>
1075 """
1076 config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1077 config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4))
1078 else:
1079 config_vni_xml="""
1080 <config>
1081 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1082 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1083 <ofdpa10:id>VNID</ofdpa10:id>
1084 </ofdpa10:vni>
1085 </of11-config:capable-switch>
1086 </config>
1087 """
1088
1089 config_vni_xml=config_vni_xml.replace("VNID", str(vni_id))
1090 config_vni_xml=config_vni_xml.replace("OPERATION", str(operation))
1091 return config_vni_xml
1092
1093def get_featureReplay(self):
1094 req = ofp.message.features_request()
1095 res, raw = self.controller.transact(req)
1096 self.assertIsNotNone(res, "Did not receive a response from the DUT.")
1097 self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY,
1098 ("Unexpected packet type %d received in response to "
1099 "OFPT_FEATURES_REQUEST") % res.type)
1100 return res
1101
1102def send_edit_config(switch_ip, xml, target='runing'):
1103 NETCONF_ACCOUNT="netconfuser"
1104 NETCONF_PASSWD="netconfuser"
1105 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1106 try:
1107 m.edit_config(target='running',
1108 config=xml,
1109 default_operation='merge',
1110 error_option='stop-on-error')
1111
1112 except Exception as e:
1113 logging.info("Fail to set xml %s", xml)
1114 return False
1115
1116 #return m.get_config(source='running').data_xml
1117 return True
1118
1119def send_delete_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='delete',
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 get_edit_config(switch_ip, 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:
macauley6f6ceb22015-08-07 09:37:12 +08001140 return m.get_config(source='running').data_xml
macauleydbff3272015-07-30 14:07:16 +08001141
macauley_cheng67da9262015-08-31 15:18:41 +08001142
1143"""
1144MPLS
1145"""
1146
1147OFDPA_MPLS_SUBTYPE_SHIFT=24
1148OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1
1149OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2
1150OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3
1151OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4
1152OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5
1153OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6
1154OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8
1155OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10
1156
1157def encode_mpls_interface_group_id(subtype, index):
1158 index=index&0x00ffffff
1159 assert(subtype==0)
1160 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1161
1162def encode_mpls_label_group_id(subtype, index):
1163 index=index&0x00ffffff
1164 assert(subtype <=5 or subtype==0)
1165 #1: l2 vpn label
1166 #2: l3 vpn label
1167 #3: mpls tunnel label 1
1168 #4: mpls tunnel lable 2
1169 #5: mpls swap label
1170 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1171
1172def encode_mpls_forwarding_group_id(subtype, index):
1173 index=index&0x00ffffff
1174 assert(subtype==6 or subtype==8 or subtype==10)
1175 return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1176
1177
1178def add_mpls_intf_group(ctrl, ref_gid, dst_mac, src_mac, vid, index, subtype=0):
1179 action=[]
1180 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
1181 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
1182 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vid)))
1183 action.append(ofp.action.group(ref_gid))
1184
1185 buckets = [ofp.bucket(actions=action)]
1186
1187 mpls_group_id =encode_mpls_interface_group_id(subtype, index)
1188 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1189 group_id=mpls_group_id,
1190 buckets=buckets
1191 )
1192 ctrl.message_send(request)
1193 return mpls_group_id, request
1194
1195def add_mpls_label_group(ctrl, subtype, index, ref_gid,
1196 lmep_id=-1,
1197 qos_index=-1,
1198 push_l2_header=False,
1199 push_vlan=False,
1200 push_mpls_header=False,
1201 push_cw=False,
1202 set_mpls_label=None,
1203 set_bos=None,
1204 set_tc=None,
1205 set_tc_from_table=False,
1206 cpy_tc_outward=False,
1207 set_ttl=None,
1208 cpy_ttl_outward=False,
1209 oam_lm_tx_count=False,
1210 set_pri_from_table=False
1211 ):
1212 """
1213 @ref_gid: only can be mpls intf group or mpls tunnel label 1/2 group
1214 """
1215 action=[]
1216
1217 if push_vlan== True:
1218 action.append(ofp.action.push_vlan(0x8100))
1219 if push_mpls_header== True:
1220 action.append(ofp.action.push_mpls(0x8847))
1221 if set_mpls_label != None:
1222 action.append(ofp.action.set_field(ofp.oxm.mpls_label(set_mpls_label)))
1223 if set_bos != None:
1224 action.append(ofp.action.set_field(ofp.oxm.mpls_bos(set_bos)))
1225 if set_tc != None:
1226 assert(set_tc_from_table==False)
1227 action.append(ofp.action.set_field(ofp.oxm.mpls_tc(set_tc)))
1228 if set_ttl != None:
1229 action.append(ofp.action.set_mpls_ttl(set_ttl))
1230 if cpy_ttl_outward == True:
1231 action.append(ofp.action.copy_ttl_out())
1232 """
1233 ofdpa experimenter
1234 """
1235 if push_l2_header== True:
1236 action.append(ofp.action.ofdpa_push_l2_header())
1237 if set_tc_from_table== True:
1238 assert(qos_index>=0)
1239 assert(set_tc == None)
1240 action.append(ofp.action.ofdpa_set_tc_from_table(qos_index))
1241 if cpy_tc_outward == True:
1242 action.append(ofp.action.ofdpa_copy_tc_out())
1243 if oam_lm_tx_count == True:
1244 assert(qos_index>=0 and lmep_id>=0)
1245 action.append(ofp.action.ofdpa_oam_lm_tx_count(lmep_id, qos_index))
1246 if set_pri_from_table == True:
1247 assert(qos_index>=0)
1248 action.append(ofp.action.ofdpa_set_qos_from_table(qos_index))
1249 if push_cw == True:
1250 action.append(ofp.action.ofdpa_push_cw())
1251
1252 action.append(ofp.action.group(ref_gid))
1253 buckets = [ofp.bucket(actions=action)]
1254
1255 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1256 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1257 group_id=mpls_group_id,
1258 buckets=buckets
1259 )
1260 ctrl.message_send(request)
1261
1262 return mpls_group_id, request
1263
1264def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids,
1265 watch_port=None,
macauley_chengd17ce512015-08-31 17:45:51 +08001266 watch_group=ofp.OFPP_ANY,
macauley_cheng67da9262015-08-31 15:18:41 +08001267 push_vlan=None,
macauley_chengd17ce512015-08-31 17:45:51 +08001268 pop_vlan=None,
macauley_cheng67da9262015-08-31 15:18:41 +08001269 set_vid=None):
1270 assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP
1271 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP
1272 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG)
macauley_chengd17ce512015-08-31 17:45:51 +08001273
macauley_cheng67da9262015-08-31 15:18:41 +08001274 buckets=[]
1275 if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP:
macauley_chengd17ce512015-08-31 17:45:51 +08001276 group_type = ofp.OFPGT_FF
macauley_cheng67da9262015-08-31 15:18:41 +08001277 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001278 action=[]
1279 action.append(ofp.action.group(gid))
1280 buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action))
1281
macauley_cheng67da9262015-08-31 15:18:41 +08001282 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP:
1283 group_type = ofp.OFPGT_SELECT
1284 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001285 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08001286 action.append(ofp.action.group(gid))
1287 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001288
macauley_cheng67da9262015-08-31 15:18:41 +08001289 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG:
1290 group_type = ofp.OFPGT_INDIRECT
macauley_chengd17ce512015-08-31 17:45:51 +08001291 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08001292 if set_vid!=None:
1293 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(set_vid)))
1294 if push_vlan!=None:
1295 action.append(ofp.action.push_vlan(push_vlan))
1296 if pop_vlan!=None:
macauley_chengd17ce512015-08-31 17:45:51 +08001297 action.append(ofp.action.pop_vlan())
macauley_cheng67da9262015-08-31 15:18:41 +08001298 action.append(ofp.action.group(ref_gids[0]))
1299 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001300
1301 mpls_group_id = encode_mpls_forwarding_group_id(subtype, index)
macauley_cheng67da9262015-08-31 15:18:41 +08001302 request = ofp.message.group_add(group_type=group_type,
macauley_cheng67da9262015-08-31 15:18:41 +08001303 group_id=mpls_group_id,
1304 buckets=buckets
1305 )
1306 ctrl.message_send(request)
1307 return mpls_group_id, request
macauley_chengd17ce512015-08-31 17:45:51 +08001308
1309
macauley_cheng67da9262015-08-31 15:18:41 +08001310"""
1311dislay
1312"""
macauleydbff3272015-07-30 14:07:16 +08001313def print_current_table_flow_stat(ctrl, table_id=0xff):
1314 stat_req=ofp.message.flow_stats_request()
1315 response, pkt = ctrl.transact(stat_req)
1316 if response == None:
1317 print "no response"
1318 return None
1319 print len(response.entries)
1320 for obj in response.entries:
1321 print "match ", obj.match
1322 print "cookie", obj.cookie
1323 print "priority", obj.priority
1324 print "idle_timeout", obj.idle_timeout
1325 print "hard_timeout", obj.hard_timeout
1326 #obj.actions
1327 print "packet count: %lx"%obj.packet_count