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