blob: d38bb78ad1767c2cda12423b5e3c8b6970e53634 [file] [log] [blame]
macauley97557232015-07-16 17:28:07 +08001import logging
2
3from oftest import config
4import oftest.base_tests as base_tests
5import ofp
6import time
7from oftest.testutils import *
8
macauleyfddc4662015-07-27 17:40:30 +08009from ncclient import manager
10import ncclient
11
macauley97557232015-07-16 17:28:07 +080012OFDPA_GROUP_TYPE_SHIFT=28
13OFDPA_VLAN_ID_SHIFT =16
macauleyfddc4662015-07-27 17:40:30 +080014OFDPA_TUNNEL_ID_SHIFT =12
15OFDPA_TUNNEL_SUBTYPE_SHIFT=10
macauley97557232015-07-16 17:28:07 +080016
17#VLAN_TABLE_FLAGS
18VLAN_TABLE_FLAG_ONLY_UNTAG=1
19VLAN_TABLE_FLAG_ONLY_TAG =2
20VLAN_TABLE_FLAG_ONLY_BOTH =3
21
macauleye8b140e2015-08-03 13:35:45 +080022PORT_FLOW_TABLE=0
23VLAN_FLOW_TABLE=10
24TERMINATION_FLOW_TABLE=20
25UCAST_ROUTING_FLOW_TABLE=30
26MCAST_ROUTING_FLOW_TABLE=40
27BRIDGE_FLOW_TABLE=50
28ACL_FLOW_TABLE=60
29
30def convertIP4toStr(ip_addr):
31 a=(ip_addr&0xff000000)>>24
32 b=(ip_addr&0x00ff0000)>>16
33 c=(ip_addr&0x0000ff00)>>8
34 d=(ip_addr&0x000000ff)
35 return str(a)+"."+str(b)+"."+str(c)+"."+str(d)
36
37def convertMACtoStr(mac):
38 if not isinstance(mac, list):
39 assert(0)
40
41 return ':'.join(['%02X' % x for x in mac])
42
macauley7f89d962015-08-06 18:13:48 +080043def getSwitchCpuMACFromDPID(dpid):
44 str_datapath_id_f= "{:016x}".format(dpid)
45 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
46 switch_cpu_mac_str=str_datapath_id[6:]
47 switch_cpu_mac = switch_cpu_mac_str.split(":")
48 switch_cpu_mac=[int(switch_cpu_mac[i],16) for i in range(0, len(switch_cpu_mac))]
49
50 return switch_cpu_mac_str, switch_cpu_mac
macauley_cheng67da9262015-08-31 15:18:41 +080051
52def DumpGroup(stats, verify_group_stats, always_show=True):
53 if(len(stats) > len(verify_group_stats)):
54 min_len = len(verify_group_stats)
55 print "Stats Len is not the same, stats>verify_group_stats"
56 if(len(stats)< len(verify_group_stats)):
57 min_len = len(stats)
58 print "Stats Len is not the same, stats<verify_group_stats"
59 else:
60 min_len = len(stats)
macauleye8b140e2015-08-03 13:35:45 +080061
macauley_cheng67da9262015-08-31 15:18:41 +080062 print "\r\n"
63 for i in range(min_len):
64 gs = stats[i]
65 gv = verify_group_stats[i]
66 print "FromSwtich:(GID=%lx, TYPE=%lx)\r\nVerify :(GID=%lx, TYPE=%lx)"%(gs.group_id, gs.group_type, gv.group_id, gv.group_type)
67 if(len(gs.buckets) != len(gv.buckets)):
68 print "buckets len is not the same gs %lx, gv %lx",(len(gs.buckets), len(gv.buckets))
69
70 for j in range(len(gs.buckets)):
71 b1=gs.buckets[j]
72 b2=gv.buckets[j]
73 if(len(b1.actions) != len(b2.actions)):
74 print "action len is not the same"
75
76 for k in range(len(b1.actions)):
77 a1=b1.actions[k]
78 a2=b2.actions[k]
79 if(always_show == True):
80 print "a1:"+a1.show()
81 print "a2:"+a2.show()
82
83def AssertGroup(self, stats, verify_group_stats):
84 self.assertTrue(len(stats) ==len(verify_group_stats), "stats len is not the same")
85
86 for i in range(len(stats)):
87 gs = stats[i]
88 gv = verify_group_stats[i]
89 self.assertTrue(len(gs.buckets) == len(gv.buckets), "buckets len is not the same")
90
91 for j in range(len(gs.buckets)):
92 b1=gs.buckets[j]
93 b2=gv.buckets[j]
94 self.assertTrue(len(b1.actions) == len(b2.actions), "action len is not the same")
95
96 for k in range(len(b1.actions)):
97 a1=b1.actions[k]
98 a2=b2.actions[k]
99 self.assertEquals(a1, a2, "action is not the same")
100
macauley97557232015-07-16 17:28:07 +0800101def encode_l2_interface_group_id(vlan, id):
102 return id + (vlan << OFDPA_VLAN_ID_SHIFT)
103
104def encode_l2_rewrite_group_id(id):
105 return id + (1 << OFDPA_GROUP_TYPE_SHIFT)
106
107def encode_l3_unicast_group_id(id):
108 return id + (2 << OFDPA_GROUP_TYPE_SHIFT)
109
110def encode_l2_mcast_group_id(vlan, id):
111 return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (3 << OFDPA_GROUP_TYPE_SHIFT)
112
113def encode_l2_flood_group_id(vlan, id):
114 return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (4 << OFDPA_GROUP_TYPE_SHIFT)
115
116def encode_l3_interface_group_id(id):
117 return id + (5 << OFDPA_GROUP_TYPE_SHIFT)
118
119def encode_l3_mcast_group_id(vlan, id):
120 return id + (vlan << OFDPA_VLAN_ID_SHIFT)+(6 << OFDPA_GROUP_TYPE_SHIFT)
121
122def encode_l3_ecmp_group_id(id):
123 return id + (7 << OFDPA_GROUP_TYPE_SHIFT)
124
macauleyfddc4662015-07-27 17:40:30 +0800125def encode_l2_overlay_group_id(tunnel_id, subtype, index):
126 tunnel_id=tunnel_id&0xffff #16 bits
127 subtype = subtype&3 #2 bits
128 index = index & 0x3f #10 bits
129 return index + (tunnel_id << OFDPA_TUNNEL_ID_SHIFT)+ (subtype<<OFDPA_TUNNEL_SUBTYPE_SHIFT)+(8 << OFDPA_GROUP_TYPE_SHIFT)
macauley97557232015-07-16 17:28:07 +0800130
Flavio Castrod4c44d12015-12-08 14:44:18 -0500131def add_l2_interface_group(ctrl, ports, vlan_id=1, is_tagged=False, send_barrier=False):
macauley97557232015-07-16 17:28:07 +0800132 # group table
133 # set up untag groups for each port
macauley41904ed2015-07-16 17:38:35 +0800134 group_id_list=[]
macauley15909e72015-07-17 15:58:57 +0800135 msgs=[]
macauley97557232015-07-16 17:28:07 +0800136 for of_port in ports:
137 # do stuff
138 group_id = encode_l2_interface_group_id(vlan_id, of_port)
macauley41904ed2015-07-16 17:38:35 +0800139 group_id_list.append(group_id)
macauley97557232015-07-16 17:28:07 +0800140 if is_tagged:
141 actions = [
142 ofp.action.output(of_port),
143 ]
144 else:
145 actions = [
146 ofp.action.pop_vlan(),
147 ofp.action.output(of_port),
148 ]
149
150 buckets = [
151 ofp.bucket(actions=actions),
152 ]
153
154 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
155 group_id=group_id,
156 buckets=buckets
157 )
158 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800159 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800160
161 if send_barrier:
162 do_barrier(ctrl)
macauley41904ed2015-07-16 17:38:35 +0800163
macauley15909e72015-07-17 15:58:57 +0800164 return group_id_list, msgs
macauley97557232015-07-16 17:28:07 +0800165
Flavio Castrod4c44d12015-12-08 14:44:18 -0500166def add_one_l2_interface_group(ctrl, port, vlan_id=1, is_tagged=False, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800167 # group table
168 # set up untag groups for each port
169 group_id = encode_l2_interface_group_id(vlan_id, port)
170
171 if is_tagged:
172 actions = [
173 ofp.action.output(port),
174 ]
175 else:
176 actions = [
177 ofp.action.pop_vlan(),
178 ofp.action.output(port),
179 ]
180
181 buckets = [
182 ofp.bucket(actions=actions),
183 ]
184
185 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
186 group_id=group_id,
187 buckets=buckets
188 )
189 ctrl.message_send(request)
190
191 if send_barrier:
192 do_barrier(ctrl)
193
194 return group_id, request
195
macauley97557232015-07-16 17:28:07 +0800196def add_l2_mcast_group(ctrl, ports, vlanid, mcast_grp_index):
197 buckets=[]
198 for of_port in ports:
199 group_id = encode_l2_interface_group_id(vlanid, of_port)
200 action=[ofp.action.group(group_id)]
201 buckets.append(ofp.bucket(actions=action))
202
203 group_id =encode_l2_mcast_group_id(vlanid, mcast_grp_index)
204 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
205 group_id=group_id,
206 buckets=buckets
207 )
208 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800209 return request
macauley97557232015-07-16 17:28:07 +0800210
macauley15909e72015-07-17 15:58:57 +0800211def add_l2_flood_group(ctrl, ports, vlanid, id):
212 buckets=[]
213 for of_port in ports:
214 group_id = encode_l2_interface_group_id(vlanid, of_port)
215 action=[ofp.action.group(group_id)]
216 buckets.append(ofp.bucket(actions=action))
macauley97557232015-07-16 17:28:07 +0800217
macauley15909e72015-07-17 15:58:57 +0800218 group_id =encode_l2_flood_group_id(vlanid, id)
219 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
220 group_id=group_id,
221 buckets=buckets
222 )
223 ctrl.message_send(request)
224 return request
225
226def add_l2_rewrite_group(ctrl, port, vlanid, id, src_mac, dst_mac):
227 group_id = encode_l2_interface_group_id(vlanid, port)
228
229 action=[]
230 if src_mac is not None:
231 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
232
233 if dst_mac is not None:
234 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
235
macauley_cheng93f3fa52015-09-02 17:57:31 +0800236 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vlanid)))
237
macauley15909e72015-07-17 15:58:57 +0800238 action.append(ofp.action.group(group_id))
239
240 buckets = [ofp.bucket(actions=action)]
241
242 group_id =encode_l2_rewrite_group_id(id)
243 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
244 group_id=group_id,
245 buckets=buckets
246 )
247 ctrl.message_send(request)
248 return request
249
250def add_l3_unicast_group(ctrl, port, vlanid, id, src_mac, dst_mac):
251 group_id = encode_l2_interface_group_id(vlanid, port)
252
253 action=[]
254 if src_mac is not None:
255 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
256
257 if dst_mac is not None:
258 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
259
Flavio Castroaf2b4502016-02-02 17:41:32 -0500260 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
macauley15909e72015-07-17 15:58:57 +0800261
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:
Flavio Castro932014b2016-01-05 18:29:15 -0500424 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
macauley97557232015-07-16 17:28:07 +0800425 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))
Flavio Castro12296312015-12-15 17:48:26 -0500447 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
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=[
Flavio Castroaf2b4502016-02-02 17:41:32 -0500455 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
macauley97557232015-07-16 17:28:07 +0800456 ]
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
Flavio Castro932014b2016-01-05 18:29:15 -0500465 if (flag == 4) :
466 match = ofp.match()
467 match.oxm_list.append(ofp.oxm.in_port(of_port))
468 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1fff))
469 request = ofp.message.flow_add(
470 table_id=10,
471 cookie=42,
472 match=match,
473 instructions=[
474 ofp.instruction.apply_actions(
475 actions=[
Flavio Castroaf2b4502016-02-02 17:41:32 -0500476 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
Flavio Castro932014b2016-01-05 18:29:15 -0500477 ]
478 ),
479 ofp.instruction.goto_table(20)
480 ],
481 priority=0)
482 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
483 ctrl.message_send(request)
484 msgs.append(request)
485
macauley97557232015-07-16 17:28:07 +0800486 if send_barrier:
487 do_barrier(ctrl)
488
macauley15909e72015-07-17 15:58:57 +0800489 return msgs
macauley_cheng6e6a6122015-11-16 14:19:18 +0800490
491def del_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
492 # table 10: vlan
493 # goto to table 20
494 msgs=[]
495 for of_port in ports:
496 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
497 match = ofp.match()
498 match.oxm_list.append(ofp.oxm.in_port(of_port))
499 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
500 request = ofp.message.flow_delete(
501 table_id=10,
502 cookie=42,
503 match=match,
504 priority=0)
505 logging.info("Del vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
506 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800507
macauley_cheng6e6a6122015-11-16 14:19:18 +0800508 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
509 match = ofp.match()
510 match.oxm_list.append(ofp.oxm.in_port(of_port))
511 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0xfff))
512 request = ofp.message.flow_delete(
513 table_id=10,
514 cookie=42,
515 match=match,
516 priority=0)
517 logging.info("Del vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
518 ctrl.message_send(request)
519 msgs.append(request)
520
521 if send_barrier:
522 do_barrier(ctrl)
523
524 return msgs
525
macauley_cheng6b311612015-09-04 11:32:27 +0800526def add_vlan_table_flow_pvid(ctrl, in_port, match_vid=None, pvid=1, send_barrier=False):
527 """it will tag pack as untagged packet wether it has tagg or not"""
528 match = ofp.match()
529 match.oxm_list.append(ofp.oxm.in_port(in_port))
530 actions=[]
531 if match_vid == None:
532 match.oxm_list.append(ofp.oxm.vlan_vid(0))
533 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
534 goto_table=20
535 else:
536 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+match_vid, 0x1fff))
537 actions.append(ofp.action.push_vlan(0x8100))
538 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
539 goto_table=20
540
541 request = ofp.message.flow_add(
542 table_id=10,
543 cookie=42,
544 match=match,
545 instructions=[
546 ofp.instruction.apply_actions(actions=actions)
547 ,ofp.instruction.goto_table(goto_table)
548 ],
549 priority=0)
550 logging.info("Add PVID %d on port %d and go to table %ld" %( pvid, in_port, goto_table))
551 ctrl.message_send(request)
552
553 if send_barrier:
554 do_barrier(ctrl)
555
556def add_vlan_table_flow_allow_all_vlan(ctrl, in_port, send_barrier=False):
557 """it st flow allow all vlan tag on this port"""
558 match = ofp.match()
559 match.oxm_list.append(ofp.oxm.in_port(in_port))
560 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1000))
561 request = ofp.message.flow_add(
562 table_id=10,
563 cookie=42,
564 match=match,
565 instructions=[
566 ofp.instruction.goto_table(20)
567 ],
568 priority=0)
569 logging.info("Add allow all vlan on port %d " %(in_port))
570 ctrl.message_send(request)
571
macauley53d90fe2015-08-04 17:34:22 +0800572def 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 +0800573 # table 10: vlan
574 # goto to table 20
Flavio Castro932014b2016-01-05 18:29:15 -0500575 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
macauley0f91a3e2015-07-17 18:09:59 +0800576 match = ofp.match()
577 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500578 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
macauley7f89d962015-08-06 18:13:48 +0800579
580 actions=[]
581 if vrf!=0:
582 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
583
Flavio Castro6d498522015-12-15 14:05:04 -0500584 #actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id)))
macauley7f89d962015-08-06 18:13:48 +0800585
macauley0f91a3e2015-07-17 18:09:59 +0800586 request = ofp.message.flow_add(
587 table_id=10,
588 cookie=42,
589 match=match,
590 instructions=[
macauley53d90fe2015-08-04 17:34:22 +0800591 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800592 actions=actions
macauley53d90fe2015-08-04 17:34:22 +0800593 ),
594 ofp.instruction.goto_table(20)
macauley0f91a3e2015-07-17 18:09:59 +0800595 ],
596 priority=0)
597 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
598 ctrl.message_send(request)
599
600 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
601 match = ofp.match()
602 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500603 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
macauley53d90fe2015-08-04 17:34:22 +0800604
macauley7f89d962015-08-06 18:13:48 +0800605 actions=[]
606 if vrf!=0:
607 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
608
Flavio Castro89933f22016-02-03 15:53:16 -0500609 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(vlan_id)))
macauley7f89d962015-08-06 18:13:48 +0800610
macauley0f91a3e2015-07-17 18:09:59 +0800611 request = ofp.message.flow_add(
612 table_id=10,
613 cookie=42,
614 match=match,
615 instructions=[
616 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800617 actions=actions
macauley0f91a3e2015-07-17 18:09:59 +0800618 ),
619 ofp.instruction.goto_table(20)
620 ],
621 priority=0)
622 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
623 ctrl.message_send(request)
Flavio Castro932014b2016-01-05 18:29:15 -0500624
625 if (flag == 4) :
626 match = ofp.match()
627 match.oxm_list.append(ofp.oxm.in_port(of_port))
628 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000,0x1fff))
629
630 actions=[]
631 if vrf!=0:
632 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
633
Flavio Castro89933f22016-02-03 15:53:16 -0500634 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id)))
Flavio Castro932014b2016-01-05 18:29:15 -0500635
636 request = ofp.message.flow_add(
637 table_id=10,
638 cookie=42,
639 match=match,
640 instructions=[
641 ofp.instruction.apply_actions(
642 actions=actions
643 ),
644 ofp.instruction.goto_table(20)
645 ],
646 priority=0)
647 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
648 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800649
650 if send_barrier:
651 do_barrier(ctrl)
652
653 return request
macauley15909e72015-07-17 15:58:57 +0800654
macauley97557232015-07-16 17:28:07 +0800655def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False):
656 match = ofp.match()
castroflavio21894482015-12-08 15:29:55 -0500657 priority=500
macauleyfddc4662015-07-27 17:40:30 +0800658 if dst_mac!=None:
castroflavio21894482015-12-08 15:29:55 -0500659 priority=1000
macauleyfddc4662015-07-27 17:40:30 +0800660 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
661
macauley97557232015-07-16 17:28:07 +0800662 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauleyfddc4662015-07-27 17:40:30 +0800663
macauley97557232015-07-16 17:28:07 +0800664 request = ofp.message.flow_add(
665 table_id=50,
666 cookie=42,
667 match=match,
668 instructions=[
669 ofp.instruction.write_actions(
670 actions=[
671 ofp.action.group(group_id)]),
672 ofp.instruction.goto_table(60)
673 ],
674 buffer_id=ofp.OFP_NO_BUFFER,
castroflavio21894482015-12-08 15:29:55 -0500675 priority=priority)
macauley97557232015-07-16 17:28:07 +0800676
677 logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac)
678 ctrl.message_send(request)
679
680 if send_barrier:
macauley15909e72015-07-17 15:58:57 +0800681 do_barrier(ctrl)
682
macauley0f91a3e2015-07-17 18:09:59 +0800683 return request
macauleyfddc4662015-07-27 17:40:30 +0800684
685def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False):
686 match = ofp.match()
687 if dst_mac!=None:
688 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
689
690 match.oxm_list.append(ofp.oxm.tunnel_id(vnid))
691 if is_group == True:
692 actions=[ofp.action.group(group_id)]
693 else:
694 actions=[ofp.action.output(group_id)]
695
696 request = ofp.message.flow_add(
697 table_id=50,
698 cookie=42,
699 match=match,
700 instructions=[
701 ofp.instruction.write_actions(
702 actions=actions),
703 ofp.instruction.goto_table(60)
704 ],
705 buffer_id=ofp.OFP_NO_BUFFER,
706 priority=1000)
707
708 logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac)
709 ctrl.message_send(request)
710
711 if send_barrier:
712 do_barrier(ctrl)
713
714 return request
macauley0f91a3e2015-07-17 18:09:59 +0800715
macauley_cheng6b133662015-11-09 13:52:39 +0800716def add_termination_flow(ctrl, in_port, eth_type, dst_mac, vlanid, goto_table=None, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800717 match = ofp.match()
macauley0f91a3e2015-07-17 18:09:59 +0800718 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauleyfddc4662015-07-27 17:40:30 +0800719 if dst_mac[0]&0x01 == 0x01:
720 match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00]))
721 goto_table=40
722 else:
macauley53d90fe2015-08-04 17:34:22 +0800723 if in_port!=0:
724 match.oxm_list.append(ofp.oxm.in_port(in_port))
macauleyfddc4662015-07-27 17:40:30 +0800725 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
726 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauley_cheng6b133662015-11-09 13:52:39 +0800727 if goto_table == None:
728 goto_table=30
macauley0f91a3e2015-07-17 18:09:59 +0800729
730 request = ofp.message.flow_add(
731 table_id=20,
732 cookie=42,
733 match=match,
734 instructions=[
735 ofp.instruction.goto_table(goto_table)
736 ],
737 buffer_id=ofp.OFP_NO_BUFFER,
738 priority=1)
739
740 logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac)
741 ctrl.message_send(request)
742
743 if send_barrier:
744 do_barrier(ctrl)
745
746 return request
747
Flavio Castroaf2b4502016-02-02 17:41:32 -0500748def add_unicast_routing_flow(ctrl, eth_type, dst_ip, mask, action_group_id, vrf=0, send_ctrl=False, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800749 match = ofp.match()
750 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauley53d90fe2015-08-04 17:34:22 +0800751 if vrf != 0:
752 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
macauley0f91a3e2015-07-17 18:09:59 +0800753
Flavio Castroaf2b4502016-02-02 17:41:32 -0500754 match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask))
755
756 instructions = []
757 instructions.append(ofp.instruction.goto_table(60))
758 if send_ctrl:
759 instructions.append(ofp.instruction.apply_actions(
760 actions=[ofp.action.output( port=ofp.OFPP_CONTROLLER,
761 max_len=ofp.OFPCML_NO_BUFFER)]))
762 else:
763 instructions.append(ofp.instruction.write_actions(
764 actions=[ofp.action.group(action_group_id)]))
macauley53d90fe2015-08-04 17:34:22 +0800765
macauley0f91a3e2015-07-17 18:09:59 +0800766 request = ofp.message.flow_add(
767 table_id=30,
768 cookie=42,
769 match=match,
Flavio Castroaf2b4502016-02-02 17:41:32 -0500770 instructions=instructions,
macauley0f91a3e2015-07-17 18:09:59 +0800771 buffer_id=ofp.OFP_NO_BUFFER,
772 priority=1)
773
774 logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip)
775 ctrl.message_send(request)
776
777 if send_barrier:
778 do_barrier(ctrl)
779
macauleyfddc4662015-07-27 17:40:30 +0800780 return request
Flavio Castrod8f8af22015-12-02 18:19:26 -0500781
Flavio Castrob702a2f2016-04-10 22:01:48 -0400782def add_mpls(ctrl, action_group_id, label=100 ,ethertype=0x0800, bos=1, vrf=1, send_barrier=False):
783 match = ofp.match()
784 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
785 match.oxm_list.append(ofp.oxm.mpls_label(label))
786 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
787 actions = [ofp.action.dec_mpls_ttl(),
788 #ofp.action.copy_ttl_in(),
789 ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)),
790 ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=23, value=32)),
791 ofp.action.group(action_group_id)]
792 #ofp.action.pop_mpls(ethertype)]
793
794 request = ofp.message.flow_add(
795 table_id=24,
796 cookie=43,
797 match=match,
798 instructions=[
799 ofp.instruction.apply_actions(
800 actions=actions
801 ),
802 #ofp.instruction.write_actions(
803 ofp.instruction.goto_table(29)
804 ],
805 buffer_id=ofp.OFP_NO_BUFFER,
806 priority=1)
807
808 logging.info("Inserting MPLS flow , label %ld", label)
809 ctrl.message_send(request)
810
811 if send_barrier:
812 do_barrier(ctrl)
813
814 return request
815
816
817
818def add_mpls_flow(ctrl, action_group_id=0x0, label=100 ,ethertype=0x0800, bos=1, vrf=1, send_barrier=False):
Flavio Castrod8f8af22015-12-02 18:19:26 -0500819 match = ofp.match()
820 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
821 match.oxm_list.append(ofp.oxm.mpls_label(label))
822 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
823 actions = [ofp.action.dec_mpls_ttl(),
824 ofp.action.copy_ttl_in(),
Flavio Castrobcc3dbc2016-04-06 11:05:03 -0400825 ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)),
Flavio Castrob702a2f2016-04-10 22:01:48 -0400826 ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=23, value=32))]
827 #ofp.action.group(action_group_id)]
828 #ofp.action.pop_mpls(ethertype)]
Flavio Castrobcc3dbc2016-04-06 11:05:03 -0400829
Flavio Castrod8f8af22015-12-02 18:19:26 -0500830 request = ofp.message.flow_add(
831 table_id=24,
832 cookie=43,
833 match=match,
834 instructions=[
835 ofp.instruction.apply_actions(
836 actions=actions
837 ),
Flavio Castrobcc3dbc2016-04-06 11:05:03 -0400838 #ofp.instruction.write_actions(
839 # actions=[ofp.action.group(action_group_id)]),
840 ofp.instruction.goto_table(27)
Flavio Castrod8f8af22015-12-02 18:19:26 -0500841 ],
842 buffer_id=ofp.OFP_NO_BUFFER,
843 priority=1)
844
845 logging.info("Inserting MPLS flow , label %ld", label)
846 ctrl.message_send(request)
847
848 if send_barrier:
849 do_barrier(ctrl)
850
851 return request
852
853
macauleyfddc4662015-07-27 17:40:30 +0800854def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False):
855 match = ofp.match()
856 match.oxm_list.append(ofp.oxm.eth_type(0x0800))
857 match.oxm_list.append(ofp.oxm.vlan_vid(vlan_id))
858 if src_ip_mask!=0:
859 match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask))
860 else:
861 match.oxm_list.append(ofp.oxm.ipv4_src(src_ip))
862
863 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
864
865 request = ofp.message.flow_add(
866 table_id=40,
867 cookie=42,
868 match=match,
869 instructions=[
870 ofp.instruction.write_actions(
871 actions=[ofp.action.group(action_group_id)]),
872 ofp.instruction.goto_table(60)
873 ],
874 buffer_id=ofp.OFP_NO_BUFFER,
875 priority=1)
876
877 logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask)
878 ctrl.message_send(request)
879
880 if send_barrier:
881 do_barrier(ctrl)
882
883 return request
macauley_cheng6b133662015-11-09 13:52:39 +0800884
885#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 +0800886def 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 +0800887 match = ofp.match()
888 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
889 match.oxm_list.append(ofp.oxm.ipv4_dst(ip_dst))
890 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
891 match.oxm_list.append(ofp.oxm.tcp_dst(tcp_dst))
892
893 request = ofp.message.flow_add(
894 table_id=28,
895 cookie=42,
896 match=match,
897 instructions=[
898 ofp.instruction.write_actions(
899 actions=[ofp.action.set_field(ofp.oxm.ipv4_dst(set_ip_dst)),
900 ofp.action.set_field(ofp.oxm.tcp_dst(set_tcp_dst)),
901 ofp.action.group(action_group_id)]),
902 ofp.instruction.goto_table(60)
903 ],
904 buffer_id=ofp.OFP_NO_BUFFER,
905 priority=1)
906 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)
907 ctrl.message_send(request)
908 return request
macauley_chengeffc20a2015-11-09 16:14:56 +0800909
910#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
911def add_snat_flow(ctrl, eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src):
912 match = ofp.match()
913 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
914 match.oxm_list.append(ofp.oxm.ipv4_src(ip_src))
915 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
916 match.oxm_list.append(ofp.oxm.tcp_src(tcp_src))
917
918 request = ofp.message.flow_add(
919 table_id=29,
920 cookie=42,
921 match=match,
922 instructions=[
923 ofp.instruction.write_actions(
924 actions=[ofp.action.set_field(ofp.oxm.ipv4_src(set_ip_src)),
925 ofp.action.set_field(ofp.oxm.tcp_src(set_tcp_src))]),
926 ofp.instruction.goto_table(30)
927 ],
928 buffer_id=ofp.OFP_NO_BUFFER,
929 priority=1)
930 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)
931 ctrl.message_send(request)
932 return request
macauley_cheng6b133662015-11-09 13:52:39 +0800933
macauleyfddc4662015-07-27 17:40:30 +0800934def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'):
935 """
936 Command Example:
937 of-agent vtap 10001 ethernet 1/1 vid 1
938 of-agent vtp 10001 vni 10
939 """
940 if vlan != 0:
941 config_vtap_xml="""
942 <config>
943 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
944 <id>capable-switch-1</id>
945 <resources>
946 <port xc:operation="OPERATION">
947 <resource-id >LPORT</resource-id>
948 <features>
949 <current>
950 <rate>10Gb</rate>
951 <medium>fiber</medium>
952 <pause>symmetric</pause>
953 </current>
954 <advertised>
955 <rate>10Gb</rate>
956 <rate>100Gb</rate>
957 <medium>fiber</medium>
958 <pause>symmetric</pause>
959 </advertised>
960 <supported>
961 <rate>10Gb</rate>
962 <rate>100Gb</rate>
963 <medium>fiber</medium>
964 <pause>symmetric</pause>
965 </supported>
966 <advertised-peer>
967 <rate>10Gb</rate>
968 <rate>100Gb</rate>
969 <medium>fiber</medium>
970 <pause>symmetric</pause>
971 </advertised-peer>
972 </features>
973 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
974 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
975 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
976 <ofdpa10:vni>VNID</ofdpa10:vni>
977 </ofdpa10:vtap>
978 </port>
979 </resources>
980 <logical-switches>
981 <switch>
982 <id>DATAPATH_ID</id>
983 <datapath-id>DATAPATH_ID</datapath-id>
984 <resources>
985 <port xc:operation="OPERATION">LPORT</port>
986 </resources>
987 </switch>
988 </logical-switches>
989 </capable-switch>
990 </config>
991 """
992 else:
993 config_vtap_xml="""
994 <config>
995 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
996 <id>capable-switch-1</id>
997 <resources>
998 <port xc:operation="OPERATION">
999 <resource-id >LPORT</resource-id>
1000 <features>
1001 <current>
1002 <rate>10Gb</rate>
1003 <medium>fiber</medium>
1004 <pause>symmetric</pause>
1005 </current>
1006 <advertised>
1007 <rate>10Gb</rate>
1008 <rate>100Gb</rate>
1009 <medium>fiber</medium>
1010 <pause>symmetric</pause>
1011 </advertised>
1012 <supported>
1013 <rate>10Gb</rate>
1014 <rate>100Gb</rate>
1015 <medium>fiber</medium>
1016 <pause>symmetric</pause>
1017 </supported>
1018 <advertised-peer>
1019 <rate>10Gb</rate>
1020 <rate>100Gb</rate>
1021 <medium>fiber</medium>
1022 <pause>symmetric</pause>
1023 </advertised-peer>
1024 </features>
1025 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1026 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1027 <ofdpa10:vni>VNID</ofdpa10:vni>
1028 </ofdpa10:vtap>
1029 </port>
1030 </resources>
1031 <logical-switches>
1032 <switch>
1033 <id>DATAPATH_ID</id>
1034 <datapath-id>DATAPATH_ID</datapath-id>
1035 <resources>
1036 <port xc:operation="OPERATION">LPORT</port>
1037 </resources>
1038 </switch>
1039 </logical-switches>
1040 </capable-switch>
1041 </config>
1042 """
1043 str_datapath_id_f= "{:016x}".format(dp_id)
1044 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1045 config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +08001046 config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport)))
macauleyfddc4662015-07-27 17:40:30 +08001047 config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port))
1048 config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan))
1049 config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid))
1050 config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation))
1051 return config_vtap_xml
1052
1053def get_vtep_lport_config_xml(dp_id, lport, src_ip, dst_ip, next_hop_id, vnid, udp_src_port=6633, ttl=25, operation='merge'):
1054 """
1055 Command Example:
1056 of-agent vtep 10002 source user-input-src-ip destination user-input-dst-ip udp-source-port 6633 nexthop 2 ttl 25
1057 of-agent vtp 10001 vni 10
1058 """
1059
1060 config_vtep_xml="""
1061 <config>
1062 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1063 <id>capable-switch-1</id>
1064 <resources>
1065 <port xc:operation="OPERATION">
1066 <resource-id>LPORT</resource-id>
1067 <features>
1068 <current>
1069 <rate>10Gb</rate>
1070 <medium>fiber</medium>
1071 <pause>symmetric</pause>
1072 </current>
1073 <advertised>
1074 <rate>10Gb</rate>
1075 <rate>100Gb</rate>
1076 <medium>fiber</medium>
1077 <pause>symmetric</pause>
1078 </advertised>
1079 <supported>
1080 <rate>10Gb</rate>
1081 <rate>100Gb</rate>
1082 <medium>fiber</medium>
1083 <pause>symmetric</pause>
1084 </supported>
1085 <advertised-peer>
1086 <rate>10Gb</rate>
1087 <rate>100Gb</rate>
1088 <medium>fiber</medium>
1089 <pause>symmetric</pause>
1090 </advertised-peer>
1091 </features>
1092 <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01">
1093 <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip>
1094 <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip>
1095 <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port>
macauley25999cf2015-08-07 17:03:24 +08001096 <ofdpa10:vni xc:operation="OPERATION">
1097 <ofdpa10:id>VNID</ofdpa10:id>
1098 </ofdpa10:vni>
macauleyfddc4662015-07-27 17:40:30 +08001099 <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id>
1100 <ofdpa10:ttl>TTL</ofdpa10:ttl>
1101 </ofdpa10:vtep>
1102 </port>
1103 </resources>
1104 <logical-switches>
1105 <switch>
1106 <id>DATAPATH_ID</id>
1107 <datapath-id>DATAPATH_ID</datapath-id>
1108 <resources>
1109 <port xc:operation="OPERATION">LPORT</port>
1110 </resources>
1111 </switch>
1112 </logical-switches>
1113 </capable-switch>
1114 </config>
1115 """
1116 str_datapath_id_f= "{:016x}".format(dp_id)
1117 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1118 config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +08001119 config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport)))
macauleyfddc4662015-07-27 17:40:30 +08001120 config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip))
1121 config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip))
1122 config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port))
1123 config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1124 config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl))
1125 config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid))
1126 config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation))
1127
1128 return config_vtep_xml
1129
1130def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'):
1131 #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2
1132 config_nexthop_xml="""
1133 <config>
1134 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1135 <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1136 <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id>
1137 <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac>
1138 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1139 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1140 </ofdpa10:next-hop>
1141 </of11-config:capable-switch>
1142 </config>
1143 """
1144 config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan))
1145 config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port))
1146 config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1147 config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac))
1148 config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation))
1149 return config_nexthop_xml
1150
1151def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'):
1152 #of-agent vni 10 multicast 224.1.1.1 nexthop 20
1153 if mcast_ipv4!=None:
1154 config_vni_xml="""
1155 <config>
1156 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1157 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1158 <ofdpa10:id>VNID</ofdpa10:id>
1159 <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group>
1160 <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id>
1161 </ofdpa10:vni>
1162 </of11-config:capable-switch>
1163 </config>
1164 """
1165 config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1166 config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4))
1167 else:
1168 config_vni_xml="""
1169 <config>
1170 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1171 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1172 <ofdpa10:id>VNID</ofdpa10:id>
1173 </ofdpa10:vni>
1174 </of11-config:capable-switch>
1175 </config>
1176 """
1177
1178 config_vni_xml=config_vni_xml.replace("VNID", str(vni_id))
1179 config_vni_xml=config_vni_xml.replace("OPERATION", str(operation))
1180 return config_vni_xml
1181
1182def get_featureReplay(self):
1183 req = ofp.message.features_request()
1184 res, raw = self.controller.transact(req)
1185 self.assertIsNotNone(res, "Did not receive a response from the DUT.")
1186 self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY,
1187 ("Unexpected packet type %d received in response to "
1188 "OFPT_FEATURES_REQUEST") % res.type)
1189 return res
1190
1191def send_edit_config(switch_ip, xml, target='runing'):
1192 NETCONF_ACCOUNT="netconfuser"
1193 NETCONF_PASSWD="netconfuser"
1194 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1195 try:
1196 m.edit_config(target='running',
1197 config=xml,
1198 default_operation='merge',
1199 error_option='stop-on-error')
1200
1201 except Exception as e:
1202 logging.info("Fail to set xml %s", xml)
1203 return False
1204
1205 #return m.get_config(source='running').data_xml
1206 return True
1207
1208def send_delete_config(switch_ip, xml, target='runing'):
1209 NETCONF_ACCOUNT="netconfuser"
1210 NETCONF_PASSWD="netconfuser"
1211 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1212 try:
1213 m.edit_config(target='running',
1214 config=xml,
1215 default_operation='delete',
1216 error_option='stop-on-error')
1217
1218 except Exception as e:
1219 logging.info("Fail to set xml %s", xml)
1220 return False
1221
1222 #return m.get_config(source='running').data_xml
1223 return True
1224
1225def get_edit_config(switch_ip, target='runing'):
1226 NETCONF_ACCOUNT="netconfuser"
1227 NETCONF_PASSWD="netconfuser"
1228 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 +08001229 return m.get_config(source='running').data_xml
macauleydbff3272015-07-30 14:07:16 +08001230
macauley_cheng67da9262015-08-31 15:18:41 +08001231
1232"""
1233MPLS
1234"""
1235
1236OFDPA_MPLS_SUBTYPE_SHIFT=24
1237OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1
1238OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2
1239OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3
1240OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4
1241OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5
1242OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6
1243OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8
1244OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10
1245
1246def encode_mpls_interface_group_id(subtype, index):
1247 index=index&0x00ffffff
1248 assert(subtype==0)
1249 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1250
1251def encode_mpls_label_group_id(subtype, index):
1252 index=index&0x00ffffff
1253 assert(subtype <=5 or subtype==0)
1254 #1: l2 vpn label
1255 #2: l3 vpn label
1256 #3: mpls tunnel label 1
1257 #4: mpls tunnel lable 2
1258 #5: mpls swap label
1259 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1260
1261def encode_mpls_forwarding_group_id(subtype, index):
1262 index=index&0x00ffffff
1263 assert(subtype==6 or subtype==8 or subtype==10)
1264 return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1265
1266
1267def add_mpls_intf_group(ctrl, ref_gid, dst_mac, src_mac, vid, index, subtype=0):
1268 action=[]
1269 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
1270 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
Flavio Castroaf2b4502016-02-02 17:41:32 -05001271 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001272 action.append(ofp.action.group(ref_gid))
1273
1274 buckets = [ofp.bucket(actions=action)]
1275
1276 mpls_group_id =encode_mpls_interface_group_id(subtype, index)
1277 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1278 group_id=mpls_group_id,
1279 buckets=buckets
1280 )
1281 ctrl.message_send(request)
1282 return mpls_group_id, request
1283
1284def add_mpls_label_group(ctrl, subtype, index, ref_gid,
1285 lmep_id=-1,
1286 qos_index=-1,
1287 push_l2_header=False,
1288 push_vlan=False,
1289 push_mpls_header=False,
1290 push_cw=False,
1291 set_mpls_label=None,
1292 set_bos=None,
1293 set_tc=None,
1294 set_tc_from_table=False,
1295 cpy_tc_outward=False,
1296 set_ttl=None,
1297 cpy_ttl_outward=False,
1298 oam_lm_tx_count=False,
1299 set_pri_from_table=False
1300 ):
1301 """
1302 @ref_gid: only can be mpls intf group or mpls tunnel label 1/2 group
1303 """
1304 action=[]
1305
1306 if push_vlan== True:
1307 action.append(ofp.action.push_vlan(0x8100))
1308 if push_mpls_header== True:
1309 action.append(ofp.action.push_mpls(0x8847))
1310 if set_mpls_label != None:
1311 action.append(ofp.action.set_field(ofp.oxm.mpls_label(set_mpls_label)))
1312 if set_bos != None:
1313 action.append(ofp.action.set_field(ofp.oxm.mpls_bos(set_bos)))
1314 if set_tc != None:
1315 assert(set_tc_from_table==False)
1316 action.append(ofp.action.set_field(ofp.oxm.mpls_tc(set_tc)))
1317 if set_ttl != None:
1318 action.append(ofp.action.set_mpls_ttl(set_ttl))
1319 if cpy_ttl_outward == True:
1320 action.append(ofp.action.copy_ttl_out())
1321 """
1322 ofdpa experimenter
1323 """
1324 if push_l2_header== True:
1325 action.append(ofp.action.ofdpa_push_l2_header())
1326 if set_tc_from_table== True:
1327 assert(qos_index>=0)
1328 assert(set_tc == None)
1329 action.append(ofp.action.ofdpa_set_tc_from_table(qos_index))
1330 if cpy_tc_outward == True:
1331 action.append(ofp.action.ofdpa_copy_tc_out())
1332 if oam_lm_tx_count == True:
1333 assert(qos_index>=0 and lmep_id>=0)
1334 action.append(ofp.action.ofdpa_oam_lm_tx_count(lmep_id, qos_index))
1335 if set_pri_from_table == True:
1336 assert(qos_index>=0)
1337 action.append(ofp.action.ofdpa_set_qos_from_table(qos_index))
1338 if push_cw == True:
1339 action.append(ofp.action.ofdpa_push_cw())
1340
1341 action.append(ofp.action.group(ref_gid))
1342 buckets = [ofp.bucket(actions=action)]
1343
1344 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1345 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1346 group_id=mpls_group_id,
1347 buckets=buckets
1348 )
1349 ctrl.message_send(request)
1350
1351 return mpls_group_id, request
1352
1353def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids,
1354 watch_port=None,
macauley_chengd17ce512015-08-31 17:45:51 +08001355 watch_group=ofp.OFPP_ANY,
macauley_cheng67da9262015-08-31 15:18:41 +08001356 push_vlan=None,
macauley_chengd17ce512015-08-31 17:45:51 +08001357 pop_vlan=None,
macauley_cheng67da9262015-08-31 15:18:41 +08001358 set_vid=None):
1359 assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP
1360 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP
1361 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG)
macauley_chengd17ce512015-08-31 17:45:51 +08001362
macauley_cheng67da9262015-08-31 15:18:41 +08001363 buckets=[]
1364 if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP:
macauley_chengd17ce512015-08-31 17:45:51 +08001365 group_type = ofp.OFPGT_FF
macauley_cheng67da9262015-08-31 15:18:41 +08001366 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001367 action=[]
1368 action.append(ofp.action.group(gid))
1369 buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action))
1370
macauley_cheng67da9262015-08-31 15:18:41 +08001371 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP:
1372 group_type = ofp.OFPGT_SELECT
1373 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001374 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08001375 action.append(ofp.action.group(gid))
1376 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001377
macauley_cheng67da9262015-08-31 15:18:41 +08001378 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG:
1379 group_type = ofp.OFPGT_INDIRECT
macauley_chengd17ce512015-08-31 17:45:51 +08001380 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08001381 if set_vid!=None:
1382 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(set_vid)))
1383 if push_vlan!=None:
1384 action.append(ofp.action.push_vlan(push_vlan))
1385 if pop_vlan!=None:
macauley_chengd17ce512015-08-31 17:45:51 +08001386 action.append(ofp.action.pop_vlan())
macauley_cheng67da9262015-08-31 15:18:41 +08001387 action.append(ofp.action.group(ref_gids[0]))
1388 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001389
1390 mpls_group_id = encode_mpls_forwarding_group_id(subtype, index)
macauley_cheng67da9262015-08-31 15:18:41 +08001391 request = ofp.message.group_add(group_type=group_type,
macauley_cheng67da9262015-08-31 15:18:41 +08001392 group_id=mpls_group_id,
1393 buckets=buckets
1394 )
1395 ctrl.message_send(request)
1396 return mpls_group_id, request
macauley_chengd17ce512015-08-31 17:45:51 +08001397
1398
macauley_cheng67da9262015-08-31 15:18:41 +08001399"""
1400dislay
1401"""
macauleydbff3272015-07-30 14:07:16 +08001402def print_current_table_flow_stat(ctrl, table_id=0xff):
1403 stat_req=ofp.message.flow_stats_request()
1404 response, pkt = ctrl.transact(stat_req)
1405 if response == None:
1406 print "no response"
1407 return None
1408 print len(response.entries)
1409 for obj in response.entries:
1410 print "match ", obj.match
1411 print "cookie", obj.cookie
1412 print "priority", obj.priority
1413 print "idle_timeout", obj.idle_timeout
1414 print "hard_timeout", obj.hard_timeout
1415 #obj.actions
Flavio Castro167f5bd2015-12-02 19:33:53 -05001416 print "packet count: %lx"%obj.packet_count