blob: 659c3a00912b3d568bf28bb4c40a4b2e76dfb216 [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
Flavio Castro91d1a552016-05-17 16:59:44 -0700125def encode_l2_unfiltered_group_id(id):
126 return id + (11 << OFDPA_GROUP_TYPE_SHIFT)
127
macauleyfddc4662015-07-27 17:40:30 +0800128def encode_l2_overlay_group_id(tunnel_id, subtype, index):
129 tunnel_id=tunnel_id&0xffff #16 bits
130 subtype = subtype&3 #2 bits
131 index = index & 0x3f #10 bits
132 return index + (tunnel_id << OFDPA_TUNNEL_ID_SHIFT)+ (subtype<<OFDPA_TUNNEL_SUBTYPE_SHIFT)+(8 << OFDPA_GROUP_TYPE_SHIFT)
macauley97557232015-07-16 17:28:07 +0800133
Flavio Castro91d1a552016-05-17 16:59:44 -0700134def add_l2_unfiltered_group(ctrl, ports, send_barrier=False):
135 # group table
136 # set up untag groups for each port
137 group_id_list=[]
138 msgs=[]
139 for of_port in ports:
140 # do stuff
141 group_id = encode_l2_unfiltered_group_id(of_port)
142 group_id_list.append(group_id)
143 actions = [ofp.action.output(of_port)]
144 actions.append(ofp.action.set_field(ofp.oxm.exp1ByteValue(exp_type=24, value=1)))
145
146 buckets = [ofp.bucket(actions=actions)]
147 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
148 group_id=group_id,
149 buckets=buckets
150 )
151 ctrl.message_send(request)
152 msgs.append(request)
153
154 if send_barrier:
155 do_barrier(ctrl)
156
157 return group_id_list, msgs
158
Flavio Castrod4c44d12015-12-08 14:44:18 -0500159def add_l2_interface_group(ctrl, ports, vlan_id=1, is_tagged=False, send_barrier=False):
macauley97557232015-07-16 17:28:07 +0800160 # group table
161 # set up untag groups for each port
macauley41904ed2015-07-16 17:38:35 +0800162 group_id_list=[]
macauley15909e72015-07-17 15:58:57 +0800163 msgs=[]
macauley97557232015-07-16 17:28:07 +0800164 for of_port in ports:
165 # do stuff
166 group_id = encode_l2_interface_group_id(vlan_id, of_port)
macauley41904ed2015-07-16 17:38:35 +0800167 group_id_list.append(group_id)
macauley97557232015-07-16 17:28:07 +0800168 if is_tagged:
169 actions = [
170 ofp.action.output(of_port),
171 ]
172 else:
173 actions = [
174 ofp.action.pop_vlan(),
175 ofp.action.output(of_port),
176 ]
177
178 buckets = [
179 ofp.bucket(actions=actions),
180 ]
181
182 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
183 group_id=group_id,
184 buckets=buckets
185 )
186 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800187 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800188
189 if send_barrier:
190 do_barrier(ctrl)
macauley41904ed2015-07-16 17:38:35 +0800191
macauley15909e72015-07-17 15:58:57 +0800192 return group_id_list, msgs
macauley97557232015-07-16 17:28:07 +0800193
Flavio Castrod4c44d12015-12-08 14:44:18 -0500194def add_one_l2_interface_group(ctrl, port, vlan_id=1, is_tagged=False, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800195 # group table
196 # set up untag groups for each port
197 group_id = encode_l2_interface_group_id(vlan_id, port)
198
199 if is_tagged:
200 actions = [
201 ofp.action.output(port),
202 ]
203 else:
204 actions = [
205 ofp.action.pop_vlan(),
206 ofp.action.output(port),
207 ]
208
209 buckets = [
210 ofp.bucket(actions=actions),
211 ]
212
213 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
214 group_id=group_id,
215 buckets=buckets
216 )
217 ctrl.message_send(request)
218
219 if send_barrier:
220 do_barrier(ctrl)
221
222 return group_id, request
223
macauley97557232015-07-16 17:28:07 +0800224def add_l2_mcast_group(ctrl, ports, vlanid, mcast_grp_index):
225 buckets=[]
226 for of_port in ports:
227 group_id = encode_l2_interface_group_id(vlanid, of_port)
228 action=[ofp.action.group(group_id)]
229 buckets.append(ofp.bucket(actions=action))
230
231 group_id =encode_l2_mcast_group_id(vlanid, mcast_grp_index)
232 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
233 group_id=group_id,
234 buckets=buckets
235 )
236 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800237 return request
macauley97557232015-07-16 17:28:07 +0800238
macauley15909e72015-07-17 15:58:57 +0800239def add_l2_flood_group(ctrl, ports, vlanid, id):
240 buckets=[]
241 for of_port in ports:
242 group_id = encode_l2_interface_group_id(vlanid, of_port)
243 action=[ofp.action.group(group_id)]
244 buckets.append(ofp.bucket(actions=action))
macauley97557232015-07-16 17:28:07 +0800245
macauley15909e72015-07-17 15:58:57 +0800246 group_id =encode_l2_flood_group_id(vlanid, id)
247 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
248 group_id=group_id,
249 buckets=buckets
250 )
251 ctrl.message_send(request)
252 return request
253
254def add_l2_rewrite_group(ctrl, port, vlanid, id, src_mac, dst_mac):
255 group_id = encode_l2_interface_group_id(vlanid, port)
256
257 action=[]
258 if src_mac is not None:
259 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
260
261 if dst_mac is not None:
262 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
263
Flavio Castro91d1a552016-05-17 16:59:44 -0700264 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
macauley_cheng93f3fa52015-09-02 17:57:31 +0800265
macauley15909e72015-07-17 15:58:57 +0800266 action.append(ofp.action.group(group_id))
267
268 buckets = [ofp.bucket(actions=action)]
269
270 group_id =encode_l2_rewrite_group_id(id)
271 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
272 group_id=group_id,
273 buckets=buckets
274 )
275 ctrl.message_send(request)
276 return request
277
278def add_l3_unicast_group(ctrl, port, vlanid, id, src_mac, dst_mac):
279 group_id = encode_l2_interface_group_id(vlanid, port)
280
281 action=[]
282 if src_mac is not None:
283 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
284
285 if dst_mac is not None:
286 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
287
Flavio Castroaf2b4502016-02-02 17:41:32 -0500288 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
macauley15909e72015-07-17 15:58:57 +0800289
290 action.append(ofp.action.group(group_id))
291
292 buckets = [ofp.bucket(actions=action)]
293
294 group_id =encode_l3_unicast_group_id(id)
295 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
296 group_id=group_id,
297 buckets=buckets
298 )
299 ctrl.message_send(request)
300 return request
301
302def add_l3_interface_group(ctrl, port, vlanid, id, src_mac):
303 group_id = encode_l2_interface_group_id(vlanid, port)
304
305 action=[]
306 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
Flavio Castro8ca52542016-04-11 11:24:49 -0400307 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlanid)))
macauley15909e72015-07-17 15:58:57 +0800308 action.append(ofp.action.group(group_id))
309
310 buckets = [ofp.bucket(actions=action)]
311
312 group_id =encode_l3_interface_group_id(id)
313 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
314 group_id=group_id,
315 buckets=buckets
316 )
317 ctrl.message_send(request)
318 return request
319
320def add_l3_ecmp_group(ctrl, id, l3_ucast_groups):
321 buckets=[]
322 for group in l3_ucast_groups:
323 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
324
325 group_id =encode_l3_ecmp_group_id(id)
326 request = ofp.message.group_add(group_type=ofp.OFPGT_SELECT,
327 group_id=group_id,
328 buckets=buckets
329 )
330 ctrl.message_send(request)
331 return request
332
333def add_l3_mcast_group(ctrl, vid, mcast_group_id, groups_on_buckets):
334 buckets=[]
335 for group in groups_on_buckets:
336 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
337
338 group_id =encode_l3_mcast_group_id(vid, mcast_group_id)
339 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
340 group_id=group_id,
341 buckets=buckets
342 )
343 ctrl.message_send(request)
344 return request
macauleyfddc4662015-07-27 17:40:30 +0800345
346def add_l2_overlay_flood_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
347 buckets=[]
348 for port in ports:
349 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
350
351 group_id=encode_l2_overlay_group_id(tunnel_id, 0, index)
352 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
353 group_id=group_id,
354 buckets=buckets
355 )
356 ctrl.message_send(request)
357 return request
358
359def add_l2_overlay_flood_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
360 buckets=[]
361 for port in ports:
362 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
363
364 group_id=encode_l2_overlay_group_id(tunnel_id, 1, index)
365 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
366 group_id=group_id,
367 buckets=buckets
368 )
369 ctrl.message_send(request)
370 return request
371
372def add_l2_overlay_mcast_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
373 buckets=[]
374 for port in ports:
375 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
376
377 group_id=encode_l2_overlay_group_id(tunnel_id, 2, index)
378 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
379 group_id=group_id,
380 buckets=buckets
381 )
382 ctrl.message_send(request)
383 return request
384
385def add_l2_overlay_mcast_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
386 buckets=[]
387 for port in ports:
388 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
389
390 group_id=encode_l2_overlay_group_id(tunnel_id, 3, index)
391 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
392 group_id=group_id,
393 buckets=buckets
394 )
395 ctrl.message_send(request)
396 return request
397
398def add_port_table_flow(ctrl, is_overlay=True):
399 match = ofp.match()
400
401 if is_overlay == True:
402 match.oxm_list.append(ofp.oxm.in_port(0x10000))
macauleydbff3272015-07-30 14:07:16 +0800403 NEXT_TABLE=50
macauleyfddc4662015-07-27 17:40:30 +0800404 else:
405 match.oxm_list.append(ofp.oxm.in_port(0))
macauleydbff3272015-07-30 14:07:16 +0800406 NEXT_TABLE=10
macauleyfddc4662015-07-27 17:40:30 +0800407
408 request = ofp.message.flow_add(
409 table_id=0,
410 cookie=42,
411 match=match,
412 instructions=[
macauleydbff3272015-07-30 14:07:16 +0800413 ofp.instruction.goto_table(NEXT_TABLE)
macauleyfddc4662015-07-27 17:40:30 +0800414 ],
415 priority=0)
416 logging.info("Add port table, match port %lx" % 0x10000)
417 ctrl.message_send(request)
macauleydbff3272015-07-30 14:07:16 +0800418
Flavio Castrod8f8af22015-12-02 18:19:26 -0500419
420def pop_vlan_flow(ctrl, ports, vlan_id=1):
421 # table 10: vlan
422 # goto to table 20
423 msgs=[]
424 for of_port in ports:
425 match = ofp.match()
426 match.oxm_list.append(ofp.oxm.in_port(of_port))
427 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
428 request = ofp.message.flow_add(
429 table_id=10,
430 cookie=42,
431 match=match,
432 instructions=[
433 ofp.instruction.apply_actions(
434 actions=[
435 ofp.action.pop_vlan()
436 ]
437 ),
438 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
445 return msgs
macauleyfddc4662015-07-27 17:40:30 +0800446
macauley97557232015-07-16 17:28:07 +0800447def add_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
448 # table 10: vlan
449 # goto to table 20
macauley15909e72015-07-17 15:58:57 +0800450 msgs=[]
macauley97557232015-07-16 17:28:07 +0800451 for of_port in ports:
Flavio Castro932014b2016-01-05 18:29:15 -0500452 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
macauley97557232015-07-16 17:28:07 +0800453 match = ofp.match()
454 match.oxm_list.append(ofp.oxm.in_port(of_port))
455 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
456 request = ofp.message.flow_add(
457 table_id=10,
458 cookie=42,
459 match=match,
460 instructions=[
Flavio Castrod8f8af22015-12-02 18:19:26 -0500461 ofp.instruction.apply_actions(
462 actions=[
463 ofp.action.pop_vlan()
464 ]
465 ),
macauley97557232015-07-16 17:28:07 +0800466 ofp.instruction.goto_table(20)
467 ],
468 priority=0)
469 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
470 ctrl.message_send(request)
471
472 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
473 match = ofp.match()
474 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500475 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
macauley97557232015-07-16 17:28:07 +0800476 request = ofp.message.flow_add(
477 table_id=10,
478 cookie=42,
479 match=match,
480 instructions=[
481 ofp.instruction.apply_actions(
482 actions=[
Flavio Castroaf2b4502016-02-02 17:41:32 -0500483 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
macauley97557232015-07-16 17:28:07 +0800484 ]
485 ),
486 ofp.instruction.goto_table(20)
487 ],
488 priority=0)
489 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
490 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800491 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800492
Flavio Castro932014b2016-01-05 18:29:15 -0500493 if (flag == 4) :
494 match = ofp.match()
495 match.oxm_list.append(ofp.oxm.in_port(of_port))
496 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1fff))
497 request = ofp.message.flow_add(
498 table_id=10,
499 cookie=42,
500 match=match,
501 instructions=[
502 ofp.instruction.apply_actions(
503 actions=[
Flavio Castroaf2b4502016-02-02 17:41:32 -0500504 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
Flavio Castro932014b2016-01-05 18:29:15 -0500505 ]
506 ),
507 ofp.instruction.goto_table(20)
508 ],
509 priority=0)
510 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
511 ctrl.message_send(request)
512 msgs.append(request)
513
macauley97557232015-07-16 17:28:07 +0800514 if send_barrier:
515 do_barrier(ctrl)
516
macauley15909e72015-07-17 15:58:57 +0800517 return msgs
macauley_cheng6e6a6122015-11-16 14:19:18 +0800518
519def del_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
520 # table 10: vlan
521 # goto to table 20
522 msgs=[]
523 for of_port in ports:
524 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
525 match = ofp.match()
526 match.oxm_list.append(ofp.oxm.in_port(of_port))
527 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
528 request = ofp.message.flow_delete(
529 table_id=10,
530 cookie=42,
531 match=match,
532 priority=0)
533 logging.info("Del vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
534 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800535
macauley_cheng6e6a6122015-11-16 14:19:18 +0800536 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
537 match = ofp.match()
538 match.oxm_list.append(ofp.oxm.in_port(of_port))
539 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0xfff))
540 request = ofp.message.flow_delete(
541 table_id=10,
542 cookie=42,
543 match=match,
544 priority=0)
545 logging.info("Del vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
546 ctrl.message_send(request)
547 msgs.append(request)
548
549 if send_barrier:
550 do_barrier(ctrl)
551
552 return msgs
553
macauley_cheng6b311612015-09-04 11:32:27 +0800554def add_vlan_table_flow_pvid(ctrl, in_port, match_vid=None, pvid=1, send_barrier=False):
555 """it will tag pack as untagged packet wether it has tagg or not"""
556 match = ofp.match()
557 match.oxm_list.append(ofp.oxm.in_port(in_port))
558 actions=[]
559 if match_vid == None:
560 match.oxm_list.append(ofp.oxm.vlan_vid(0))
561 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
562 goto_table=20
563 else:
564 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+match_vid, 0x1fff))
565 actions.append(ofp.action.push_vlan(0x8100))
566 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+pvid)))
567 goto_table=20
568
569 request = ofp.message.flow_add(
570 table_id=10,
571 cookie=42,
572 match=match,
573 instructions=[
574 ofp.instruction.apply_actions(actions=actions)
575 ,ofp.instruction.goto_table(goto_table)
576 ],
577 priority=0)
578 logging.info("Add PVID %d on port %d and go to table %ld" %( pvid, in_port, goto_table))
579 ctrl.message_send(request)
580
581 if send_barrier:
582 do_barrier(ctrl)
583
584def add_vlan_table_flow_allow_all_vlan(ctrl, in_port, send_barrier=False):
585 """it st flow allow all vlan tag on this port"""
586 match = ofp.match()
587 match.oxm_list.append(ofp.oxm.in_port(in_port))
588 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000, 0x1000))
589 request = ofp.message.flow_add(
590 table_id=10,
591 cookie=42,
592 match=match,
593 instructions=[
594 ofp.instruction.goto_table(20)
595 ],
596 priority=0)
597 logging.info("Add allow all vlan on port %d " %(in_port))
598 ctrl.message_send(request)
599
macauley53d90fe2015-08-04 17:34:22 +0800600def 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 +0800601 # table 10: vlan
602 # goto to table 20
Flavio Castro932014b2016-01-05 18:29:15 -0500603 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH) or (flag == 4):
macauley0f91a3e2015-07-17 18:09:59 +0800604 match = ofp.match()
605 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500606 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000+vlan_id,0x1fff))
macauley7f89d962015-08-06 18:13:48 +0800607
608 actions=[]
609 if vrf!=0:
610 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
611
Flavio Castro6d498522015-12-15 14:05:04 -0500612 #actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(value=vlan_id)))
macauley7f89d962015-08-06 18:13:48 +0800613
macauley0f91a3e2015-07-17 18:09:59 +0800614 request = ofp.message.flow_add(
615 table_id=10,
616 cookie=42,
617 match=match,
618 instructions=[
macauley53d90fe2015-08-04 17:34:22 +0800619 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800620 actions=actions
macauley53d90fe2015-08-04 17:34:22 +0800621 ),
622 ofp.instruction.goto_table(20)
macauley0f91a3e2015-07-17 18:09:59 +0800623 ],
624 priority=0)
625 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
626 ctrl.message_send(request)
627
628 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
629 match = ofp.match()
630 match.oxm_list.append(ofp.oxm.in_port(of_port))
Flavio Castro12296312015-12-15 17:48:26 -0500631 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0, 0x1fff))
macauley53d90fe2015-08-04 17:34:22 +0800632
macauley7f89d962015-08-06 18:13:48 +0800633 actions=[]
634 if vrf!=0:
635 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
636
Flavio Castro91d1a552016-05-17 16:59:44 -0700637 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
macauley7f89d962015-08-06 18:13:48 +0800638
macauley0f91a3e2015-07-17 18:09:59 +0800639 request = ofp.message.flow_add(
640 table_id=10,
641 cookie=42,
642 match=match,
643 instructions=[
644 ofp.instruction.apply_actions(
macauley7f89d962015-08-06 18:13:48 +0800645 actions=actions
macauley0f91a3e2015-07-17 18:09:59 +0800646 ),
647 ofp.instruction.goto_table(20)
648 ],
649 priority=0)
650 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
651 ctrl.message_send(request)
Flavio Castro932014b2016-01-05 18:29:15 -0500652
653 if (flag == 4) :
654 match = ofp.match()
655 match.oxm_list.append(ofp.oxm.in_port(of_port))
656 match.oxm_list.append(ofp.oxm.vlan_vid_masked(0x1000,0x1fff))
657
658 actions=[]
659 if vrf!=0:
660 actions.append(ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf)))
661
Flavio Castro91d1a552016-05-17 16:59:44 -0700662 actions.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id)))
Flavio Castro932014b2016-01-05 18:29:15 -0500663
664 request = ofp.message.flow_add(
665 table_id=10,
666 cookie=42,
667 match=match,
668 instructions=[
669 ofp.instruction.apply_actions(
670 actions=actions
671 ),
672 ofp.instruction.goto_table(20)
673 ],
674 priority=0)
675 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
676 ctrl.message_send(request)
macauley0f91a3e2015-07-17 18:09:59 +0800677
678 if send_barrier:
679 do_barrier(ctrl)
680
681 return request
macauley15909e72015-07-17 15:58:57 +0800682
macauley97557232015-07-16 17:28:07 +0800683def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False):
684 match = ofp.match()
castroflavio21894482015-12-08 15:29:55 -0500685 priority=500
macauleyfddc4662015-07-27 17:40:30 +0800686 if dst_mac!=None:
castroflavio21894482015-12-08 15:29:55 -0500687 priority=1000
macauleyfddc4662015-07-27 17:40:30 +0800688 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
689
macauley97557232015-07-16 17:28:07 +0800690 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauleyfddc4662015-07-27 17:40:30 +0800691
macauley97557232015-07-16 17:28:07 +0800692 request = ofp.message.flow_add(
693 table_id=50,
694 cookie=42,
695 match=match,
696 instructions=[
697 ofp.instruction.write_actions(
698 actions=[
699 ofp.action.group(group_id)]),
700 ofp.instruction.goto_table(60)
701 ],
702 buffer_id=ofp.OFP_NO_BUFFER,
castroflavio21894482015-12-08 15:29:55 -0500703 priority=priority)
macauley97557232015-07-16 17:28:07 +0800704
705 logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac)
706 ctrl.message_send(request)
707
708 if send_barrier:
macauley15909e72015-07-17 15:58:57 +0800709 do_barrier(ctrl)
710
macauley0f91a3e2015-07-17 18:09:59 +0800711 return request
macauleyfddc4662015-07-27 17:40:30 +0800712
713def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False):
714 match = ofp.match()
715 if dst_mac!=None:
716 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
717
718 match.oxm_list.append(ofp.oxm.tunnel_id(vnid))
719 if is_group == True:
720 actions=[ofp.action.group(group_id)]
721 else:
722 actions=[ofp.action.output(group_id)]
723
724 request = ofp.message.flow_add(
725 table_id=50,
726 cookie=42,
727 match=match,
728 instructions=[
729 ofp.instruction.write_actions(
730 actions=actions),
731 ofp.instruction.goto_table(60)
732 ],
733 buffer_id=ofp.OFP_NO_BUFFER,
734 priority=1000)
735
736 logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac)
737 ctrl.message_send(request)
738
739 if send_barrier:
740 do_barrier(ctrl)
741
742 return request
macauley0f91a3e2015-07-17 18:09:59 +0800743
macauley_cheng6b133662015-11-09 13:52:39 +0800744def add_termination_flow(ctrl, in_port, eth_type, dst_mac, vlanid, goto_table=None, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800745 match = ofp.match()
macauley0f91a3e2015-07-17 18:09:59 +0800746 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauleyfddc4662015-07-27 17:40:30 +0800747 if dst_mac[0]&0x01 == 0x01:
748 match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00]))
749 goto_table=40
750 else:
macauley53d90fe2015-08-04 17:34:22 +0800751 if in_port!=0:
752 match.oxm_list.append(ofp.oxm.in_port(in_port))
macauleyfddc4662015-07-27 17:40:30 +0800753 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
754 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauley_cheng6b133662015-11-09 13:52:39 +0800755 if goto_table == None:
756 goto_table=30
macauley0f91a3e2015-07-17 18:09:59 +0800757
758 request = ofp.message.flow_add(
759 table_id=20,
760 cookie=42,
761 match=match,
762 instructions=[
763 ofp.instruction.goto_table(goto_table)
764 ],
765 buffer_id=ofp.OFP_NO_BUFFER,
766 priority=1)
767
768 logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac)
769 ctrl.message_send(request)
770
771 if send_barrier:
772 do_barrier(ctrl)
773
774 return request
775
Flavio Castroaf2b4502016-02-02 17:41:32 -0500776def 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 +0800777 match = ofp.match()
778 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauley53d90fe2015-08-04 17:34:22 +0800779 if vrf != 0:
780 match.oxm_list.append(ofp.oxm.exp2ByteValue(ofp.oxm.OFDPA_EXP_TYPE_VRF, vrf))
macauley0f91a3e2015-07-17 18:09:59 +0800781
Flavio Castroaf2b4502016-02-02 17:41:32 -0500782 match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask))
783
784 instructions = []
785 instructions.append(ofp.instruction.goto_table(60))
786 if send_ctrl:
Flavio Castro91d1a552016-05-17 16:59:44 -0700787 instructions.append(ofp.instruction.write_actions(
Flavio Castroaf2b4502016-02-02 17:41:32 -0500788 actions=[ofp.action.output( port=ofp.OFPP_CONTROLLER,
789 max_len=ofp.OFPCML_NO_BUFFER)]))
790 else:
791 instructions.append(ofp.instruction.write_actions(
792 actions=[ofp.action.group(action_group_id)]))
macauley53d90fe2015-08-04 17:34:22 +0800793
macauley0f91a3e2015-07-17 18:09:59 +0800794 request = ofp.message.flow_add(
795 table_id=30,
796 cookie=42,
797 match=match,
Flavio Castroaf2b4502016-02-02 17:41:32 -0500798 instructions=instructions,
macauley0f91a3e2015-07-17 18:09:59 +0800799 buffer_id=ofp.OFP_NO_BUFFER,
800 priority=1)
801
802 logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip)
803 ctrl.message_send(request)
804
805 if send_barrier:
806 do_barrier(ctrl)
807
macauleyfddc4662015-07-27 17:40:30 +0800808 return request
Flavio Castrod8f8af22015-12-02 18:19:26 -0500809
Flavio Castro8ca52542016-04-11 11:24:49 -0400810def add_mpls_flow(ctrl, action_group_id=0x0, label=100 ,ethertype=0x0800, bos=1, vrf=1, goto_table=27, send_barrier=False):
Flavio Castrob702a2f2016-04-10 22:01:48 -0400811 match = ofp.match()
812 match.oxm_list.append(ofp.oxm.eth_type(0x8847))
813 match.oxm_list.append(ofp.oxm.mpls_label(label))
814 match.oxm_list.append(ofp.oxm.mpls_bos(bos))
815 actions = [ofp.action.dec_mpls_ttl(),
Flavio Castro8ca52542016-04-11 11:24:49 -0400816 ofp.action.set_field(ofp.oxm.exp2ByteValue(exp_type=1, value=vrf))]
817 if (goto_table == 29):
818 actions.append(ofp.action.set_field(
819 ofp.oxm.exp2ByteValue(exp_type=23, value=32)))
820 actions.append(ofp.action.group(action_group_id))
821 else:
822 actions.append(ofp.action.copy_ttl_in())
823
Flavio Castrob702a2f2016-04-10 22:01:48 -0400824 request = ofp.message.flow_add(
825 table_id=24,
826 cookie=43,
827 match=match,
828 instructions=[
Flavio Castro8ca52542016-04-11 11:24:49 -0400829 ofp.instruction.apply_actions(actions=actions),
830 ofp.instruction.goto_table(goto_table)
Flavio Castrob702a2f2016-04-10 22:01:48 -0400831 ],
832 buffer_id=ofp.OFP_NO_BUFFER,
833 priority=1)
Flavio Castrob702a2f2016-04-10 22:01:48 -0400834 logging.info("Inserting MPLS flow , label %ld", label)
835 ctrl.message_send(request)
836
837 if send_barrier:
838 do_barrier(ctrl)
839
840 return request
841
macauleyfddc4662015-07-27 17:40:30 +0800842def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False):
843 match = ofp.match()
844 match.oxm_list.append(ofp.oxm.eth_type(0x0800))
845 match.oxm_list.append(ofp.oxm.vlan_vid(vlan_id))
846 if src_ip_mask!=0:
847 match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask))
848 else:
849 match.oxm_list.append(ofp.oxm.ipv4_src(src_ip))
850
851 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
852
853 request = ofp.message.flow_add(
854 table_id=40,
855 cookie=42,
856 match=match,
857 instructions=[
858 ofp.instruction.write_actions(
859 actions=[ofp.action.group(action_group_id)]),
860 ofp.instruction.goto_table(60)
861 ],
862 buffer_id=ofp.OFP_NO_BUFFER,
863 priority=1)
864
865 logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask)
866 ctrl.message_send(request)
867
868 if send_barrier:
869 do_barrier(ctrl)
870
871 return request
macauley_cheng6b133662015-11-09 13:52:39 +0800872
873#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 +0800874def 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 +0800875 match = ofp.match()
876 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
877 match.oxm_list.append(ofp.oxm.ipv4_dst(ip_dst))
878 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
879 match.oxm_list.append(ofp.oxm.tcp_dst(tcp_dst))
880
881 request = ofp.message.flow_add(
882 table_id=28,
883 cookie=42,
884 match=match,
885 instructions=[
886 ofp.instruction.write_actions(
887 actions=[ofp.action.set_field(ofp.oxm.ipv4_dst(set_ip_dst)),
888 ofp.action.set_field(ofp.oxm.tcp_dst(set_tcp_dst)),
889 ofp.action.group(action_group_id)]),
890 ofp.instruction.goto_table(60)
891 ],
892 buffer_id=ofp.OFP_NO_BUFFER,
893 priority=1)
894 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)
895 ctrl.message_send(request)
896 return request
macauley_chengeffc20a2015-11-09 16:14:56 +0800897
898#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
899def add_snat_flow(ctrl, eth_type, ip_src, ip_proto, tcp_src, set_ip_src, set_tcp_src):
900 match = ofp.match()
901 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
902 match.oxm_list.append(ofp.oxm.ipv4_src(ip_src))
903 match.oxm_list.append(ofp.oxm.ip_proto(ip_proto))
904 match.oxm_list.append(ofp.oxm.tcp_src(tcp_src))
905
906 request = ofp.message.flow_add(
907 table_id=29,
908 cookie=42,
909 match=match,
910 instructions=[
911 ofp.instruction.write_actions(
912 actions=[ofp.action.set_field(ofp.oxm.ipv4_src(set_ip_src)),
913 ofp.action.set_field(ofp.oxm.tcp_src(set_tcp_src))]),
914 ofp.instruction.goto_table(30)
915 ],
916 buffer_id=ofp.OFP_NO_BUFFER,
917 priority=1)
918 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)
919 ctrl.message_send(request)
920 return request
macauley_cheng6b133662015-11-09 13:52:39 +0800921
macauleyfddc4662015-07-27 17:40:30 +0800922def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'):
923 """
924 Command Example:
925 of-agent vtap 10001 ethernet 1/1 vid 1
926 of-agent vtp 10001 vni 10
927 """
928 if vlan != 0:
929 config_vtap_xml="""
930 <config>
931 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
932 <id>capable-switch-1</id>
933 <resources>
934 <port xc:operation="OPERATION">
935 <resource-id >LPORT</resource-id>
936 <features>
937 <current>
938 <rate>10Gb</rate>
939 <medium>fiber</medium>
940 <pause>symmetric</pause>
941 </current>
942 <advertised>
943 <rate>10Gb</rate>
944 <rate>100Gb</rate>
945 <medium>fiber</medium>
946 <pause>symmetric</pause>
947 </advertised>
948 <supported>
949 <rate>10Gb</rate>
950 <rate>100Gb</rate>
951 <medium>fiber</medium>
952 <pause>symmetric</pause>
953 </supported>
954 <advertised-peer>
955 <rate>10Gb</rate>
956 <rate>100Gb</rate>
957 <medium>fiber</medium>
958 <pause>symmetric</pause>
959 </advertised-peer>
960 </features>
961 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
962 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
963 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
964 <ofdpa10:vni>VNID</ofdpa10:vni>
965 </ofdpa10:vtap>
966 </port>
967 </resources>
968 <logical-switches>
969 <switch>
970 <id>DATAPATH_ID</id>
971 <datapath-id>DATAPATH_ID</datapath-id>
972 <resources>
973 <port xc:operation="OPERATION">LPORT</port>
974 </resources>
975 </switch>
976 </logical-switches>
977 </capable-switch>
978 </config>
979 """
980 else:
981 config_vtap_xml="""
982 <config>
983 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
984 <id>capable-switch-1</id>
985 <resources>
986 <port xc:operation="OPERATION">
987 <resource-id >LPORT</resource-id>
988 <features>
989 <current>
990 <rate>10Gb</rate>
991 <medium>fiber</medium>
992 <pause>symmetric</pause>
993 </current>
994 <advertised>
995 <rate>10Gb</rate>
996 <rate>100Gb</rate>
997 <medium>fiber</medium>
998 <pause>symmetric</pause>
999 </advertised>
1000 <supported>
1001 <rate>10Gb</rate>
1002 <rate>100Gb</rate>
1003 <medium>fiber</medium>
1004 <pause>symmetric</pause>
1005 </supported>
1006 <advertised-peer>
1007 <rate>10Gb</rate>
1008 <rate>100Gb</rate>
1009 <medium>fiber</medium>
1010 <pause>symmetric</pause>
1011 </advertised-peer>
1012 </features>
1013 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1014 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1015 <ofdpa10:vni>VNID</ofdpa10:vni>
1016 </ofdpa10:vtap>
1017 </port>
1018 </resources>
1019 <logical-switches>
1020 <switch>
1021 <id>DATAPATH_ID</id>
1022 <datapath-id>DATAPATH_ID</datapath-id>
1023 <resources>
1024 <port xc:operation="OPERATION">LPORT</port>
1025 </resources>
1026 </switch>
1027 </logical-switches>
1028 </capable-switch>
1029 </config>
1030 """
1031 str_datapath_id_f= "{:016x}".format(dp_id)
1032 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1033 config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +08001034 config_vtap_xml=config_vtap_xml.replace("LPORT", str(int(lport)))
macauleyfddc4662015-07-27 17:40:30 +08001035 config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port))
1036 config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan))
1037 config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid))
1038 config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation))
1039 return config_vtap_xml
1040
1041def get_vtep_lport_config_xml(dp_id, lport, src_ip, dst_ip, next_hop_id, vnid, udp_src_port=6633, ttl=25, operation='merge'):
1042 """
1043 Command Example:
1044 of-agent vtep 10002 source user-input-src-ip destination user-input-dst-ip udp-source-port 6633 nexthop 2 ttl 25
1045 of-agent vtp 10001 vni 10
1046 """
1047
1048 config_vtep_xml="""
1049 <config>
1050 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1051 <id>capable-switch-1</id>
1052 <resources>
1053 <port xc:operation="OPERATION">
1054 <resource-id>LPORT</resource-id>
1055 <features>
1056 <current>
1057 <rate>10Gb</rate>
1058 <medium>fiber</medium>
1059 <pause>symmetric</pause>
1060 </current>
1061 <advertised>
1062 <rate>10Gb</rate>
1063 <rate>100Gb</rate>
1064 <medium>fiber</medium>
1065 <pause>symmetric</pause>
1066 </advertised>
1067 <supported>
1068 <rate>10Gb</rate>
1069 <rate>100Gb</rate>
1070 <medium>fiber</medium>
1071 <pause>symmetric</pause>
1072 </supported>
1073 <advertised-peer>
1074 <rate>10Gb</rate>
1075 <rate>100Gb</rate>
1076 <medium>fiber</medium>
1077 <pause>symmetric</pause>
1078 </advertised-peer>
1079 </features>
1080 <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01">
1081 <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip>
1082 <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip>
1083 <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port>
macauley25999cf2015-08-07 17:03:24 +08001084 <ofdpa10:vni xc:operation="OPERATION">
1085 <ofdpa10:id>VNID</ofdpa10:id>
1086 </ofdpa10:vni>
macauleyfddc4662015-07-27 17:40:30 +08001087 <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id>
1088 <ofdpa10:ttl>TTL</ofdpa10:ttl>
1089 </ofdpa10:vtep>
1090 </port>
1091 </resources>
1092 <logical-switches>
1093 <switch>
1094 <id>DATAPATH_ID</id>
1095 <datapath-id>DATAPATH_ID</datapath-id>
1096 <resources>
1097 <port xc:operation="OPERATION">LPORT</port>
1098 </resources>
1099 </switch>
1100 </logical-switches>
1101 </capable-switch>
1102 </config>
1103 """
1104 str_datapath_id_f= "{:016x}".format(dp_id)
1105 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
1106 config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id)
macauley25999cf2015-08-07 17:03:24 +08001107 config_vtep_xml=config_vtep_xml.replace("LPORT", str(int(lport)))
macauleyfddc4662015-07-27 17:40:30 +08001108 config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip))
1109 config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip))
1110 config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port))
1111 config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1112 config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl))
1113 config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid))
1114 config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation))
1115
1116 return config_vtep_xml
1117
1118def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'):
1119 #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2
1120 config_nexthop_xml="""
1121 <config>
1122 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1123 <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1124 <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id>
1125 <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac>
1126 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
1127 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
1128 </ofdpa10:next-hop>
1129 </of11-config:capable-switch>
1130 </config>
1131 """
1132 config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan))
1133 config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port))
1134 config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1135 config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac))
1136 config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation))
1137 return config_nexthop_xml
1138
1139def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'):
1140 #of-agent vni 10 multicast 224.1.1.1 nexthop 20
1141 if mcast_ipv4!=None:
1142 config_vni_xml="""
1143 <config>
1144 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1145 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1146 <ofdpa10:id>VNID</ofdpa10:id>
1147 <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group>
1148 <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id>
1149 </ofdpa10:vni>
1150 </of11-config:capable-switch>
1151 </config>
1152 """
1153 config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id))
1154 config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4))
1155 else:
1156 config_vni_xml="""
1157 <config>
1158 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
1159 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
1160 <ofdpa10:id>VNID</ofdpa10:id>
1161 </ofdpa10:vni>
1162 </of11-config:capable-switch>
1163 </config>
1164 """
1165
1166 config_vni_xml=config_vni_xml.replace("VNID", str(vni_id))
1167 config_vni_xml=config_vni_xml.replace("OPERATION", str(operation))
1168 return config_vni_xml
1169
1170def get_featureReplay(self):
1171 req = ofp.message.features_request()
1172 res, raw = self.controller.transact(req)
1173 self.assertIsNotNone(res, "Did not receive a response from the DUT.")
1174 self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY,
1175 ("Unexpected packet type %d received in response to "
1176 "OFPT_FEATURES_REQUEST") % res.type)
1177 return res
1178
1179def send_edit_config(switch_ip, xml, target='runing'):
1180 NETCONF_ACCOUNT="netconfuser"
1181 NETCONF_PASSWD="netconfuser"
1182 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1183 try:
1184 m.edit_config(target='running',
1185 config=xml,
1186 default_operation='merge',
1187 error_option='stop-on-error')
1188
1189 except Exception as e:
1190 logging.info("Fail to set xml %s", xml)
1191 return False
1192
1193 #return m.get_config(source='running').data_xml
1194 return True
1195
1196def send_delete_config(switch_ip, xml, target='runing'):
1197 NETCONF_ACCOUNT="netconfuser"
1198 NETCONF_PASSWD="netconfuser"
1199 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
1200 try:
1201 m.edit_config(target='running',
1202 config=xml,
1203 default_operation='delete',
1204 error_option='stop-on-error')
1205
1206 except Exception as e:
1207 logging.info("Fail to set xml %s", xml)
1208 return False
1209
1210 #return m.get_config(source='running').data_xml
1211 return True
1212
1213def get_edit_config(switch_ip, target='runing'):
1214 NETCONF_ACCOUNT="netconfuser"
1215 NETCONF_PASSWD="netconfuser"
1216 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 +08001217 return m.get_config(source='running').data_xml
macauleydbff3272015-07-30 14:07:16 +08001218
macauley_cheng67da9262015-08-31 15:18:41 +08001219
1220"""
1221MPLS
1222"""
1223
1224OFDPA_MPLS_SUBTYPE_SHIFT=24
1225OFDPA_MPLS_GROUP_SUBTYPE_L2_VPN_LABEL=1
1226OFDPA_MPLS_GROUP_SUBTYPE_L3_VPN_LABEL=2
1227OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL1=3
1228OFDPA_MPLS_GROUP_SUBTYPE_TUNNEL_LABEL2=4
1229OFDPA_MPLS_GROUP_SUBTYPE_SWAP_LABEL=5
1230OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP=6
1231OFDPA_MPLS_GROUP_SUBTYPE_ECMP=8
1232OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG=10
1233
1234def encode_mpls_interface_group_id(subtype, index):
1235 index=index&0x00ffffff
1236 assert(subtype==0)
1237 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1238
1239def encode_mpls_label_group_id(subtype, index):
1240 index=index&0x00ffffff
1241 assert(subtype <=5 or subtype==0)
1242 #1: l2 vpn label
1243 #2: l3 vpn label
1244 #3: mpls tunnel label 1
1245 #4: mpls tunnel lable 2
1246 #5: mpls swap label
1247 return index + (9 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1248
1249def encode_mpls_forwarding_group_id(subtype, index):
1250 index=index&0x00ffffff
1251 assert(subtype==6 or subtype==8 or subtype==10)
1252 return index + (10 << OFDPA_GROUP_TYPE_SHIFT)+(subtype<<OFDPA_MPLS_SUBTYPE_SHIFT)
1253
1254
1255def add_mpls_intf_group(ctrl, ref_gid, dst_mac, src_mac, vid, index, subtype=0):
1256 action=[]
1257 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
1258 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
Flavio Castroaf2b4502016-02-02 17:41:32 -05001259 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001260 action.append(ofp.action.group(ref_gid))
1261
1262 buckets = [ofp.bucket(actions=action)]
1263
1264 mpls_group_id =encode_mpls_interface_group_id(subtype, index)
1265 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1266 group_id=mpls_group_id,
1267 buckets=buckets
1268 )
1269 ctrl.message_send(request)
1270 return mpls_group_id, request
1271
1272def add_mpls_label_group(ctrl, subtype, index, ref_gid,
1273 lmep_id=-1,
1274 qos_index=-1,
1275 push_l2_header=False,
1276 push_vlan=False,
1277 push_mpls_header=False,
1278 push_cw=False,
1279 set_mpls_label=None,
1280 set_bos=None,
1281 set_tc=None,
1282 set_tc_from_table=False,
1283 cpy_tc_outward=False,
1284 set_ttl=None,
1285 cpy_ttl_outward=False,
1286 oam_lm_tx_count=False,
1287 set_pri_from_table=False
1288 ):
1289 """
1290 @ref_gid: only can be mpls intf group or mpls tunnel label 1/2 group
1291 """
1292 action=[]
1293
1294 if push_vlan== True:
1295 action.append(ofp.action.push_vlan(0x8100))
1296 if push_mpls_header== True:
1297 action.append(ofp.action.push_mpls(0x8847))
1298 if set_mpls_label != None:
1299 action.append(ofp.action.set_field(ofp.oxm.mpls_label(set_mpls_label)))
1300 if set_bos != None:
1301 action.append(ofp.action.set_field(ofp.oxm.mpls_bos(set_bos)))
1302 if set_tc != None:
1303 assert(set_tc_from_table==False)
1304 action.append(ofp.action.set_field(ofp.oxm.mpls_tc(set_tc)))
1305 if set_ttl != None:
1306 action.append(ofp.action.set_mpls_ttl(set_ttl))
1307 if cpy_ttl_outward == True:
1308 action.append(ofp.action.copy_ttl_out())
1309 """
1310 ofdpa experimenter
1311 """
1312 if push_l2_header== True:
1313 action.append(ofp.action.ofdpa_push_l2_header())
1314 if set_tc_from_table== True:
1315 assert(qos_index>=0)
1316 assert(set_tc == None)
1317 action.append(ofp.action.ofdpa_set_tc_from_table(qos_index))
1318 if cpy_tc_outward == True:
1319 action.append(ofp.action.ofdpa_copy_tc_out())
1320 if oam_lm_tx_count == True:
1321 assert(qos_index>=0 and lmep_id>=0)
1322 action.append(ofp.action.ofdpa_oam_lm_tx_count(lmep_id, qos_index))
1323 if set_pri_from_table == True:
1324 assert(qos_index>=0)
1325 action.append(ofp.action.ofdpa_set_qos_from_table(qos_index))
1326 if push_cw == True:
1327 action.append(ofp.action.ofdpa_push_cw())
1328
1329 action.append(ofp.action.group(ref_gid))
1330 buckets = [ofp.bucket(actions=action)]
1331
1332 mpls_group_id = encode_mpls_label_group_id(subtype, index)
1333 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
1334 group_id=mpls_group_id,
1335 buckets=buckets
1336 )
1337 ctrl.message_send(request)
1338
1339 return mpls_group_id, request
1340
1341def add_mpls_forwarding_group(ctrl, subtype, index, ref_gids,
1342 watch_port=None,
macauley_chengd17ce512015-08-31 17:45:51 +08001343 watch_group=ofp.OFPP_ANY,
macauley_cheng67da9262015-08-31 15:18:41 +08001344 push_vlan=None,
macauley_chengd17ce512015-08-31 17:45:51 +08001345 pop_vlan=None,
macauley_cheng67da9262015-08-31 15:18:41 +08001346 set_vid=None):
1347 assert(subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP
1348 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP
1349 or subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG)
macauley_chengd17ce512015-08-31 17:45:51 +08001350
macauley_cheng67da9262015-08-31 15:18:41 +08001351 buckets=[]
1352 if subtype == OFDPA_MPLS_GROUP_SUBTYPE_FAST_FAILOVER_GROUP:
macauley_chengd17ce512015-08-31 17:45:51 +08001353 group_type = ofp.OFPGT_FF
macauley_cheng67da9262015-08-31 15:18:41 +08001354 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001355 action=[]
1356 action.append(ofp.action.group(gid))
1357 buckets.append(ofp.bucket(watch_port=watch_port, watch_group=watch_group,actions=action))
1358
macauley_cheng67da9262015-08-31 15:18:41 +08001359 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_ECMP:
1360 group_type = ofp.OFPGT_SELECT
1361 for gid in ref_gids:
macauley_chengd17ce512015-08-31 17:45:51 +08001362 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08001363 action.append(ofp.action.group(gid))
1364 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001365
macauley_cheng67da9262015-08-31 15:18:41 +08001366 elif subtype == OFDPA_MPLS_GROUP_SUBTYPE_L2_TAG:
1367 group_type = ofp.OFPGT_INDIRECT
macauley_chengd17ce512015-08-31 17:45:51 +08001368 action=[]
macauley_cheng67da9262015-08-31 15:18:41 +08001369 if set_vid!=None:
Flavio Castro91d1a552016-05-17 16:59:44 -07001370 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+set_vid)))
macauley_cheng67da9262015-08-31 15:18:41 +08001371 if push_vlan!=None:
1372 action.append(ofp.action.push_vlan(push_vlan))
1373 if pop_vlan!=None:
macauley_chengd17ce512015-08-31 17:45:51 +08001374 action.append(ofp.action.pop_vlan())
macauley_cheng67da9262015-08-31 15:18:41 +08001375 action.append(ofp.action.group(ref_gids[0]))
1376 buckets.append(ofp.bucket(actions=action))
macauley_chengd17ce512015-08-31 17:45:51 +08001377
1378 mpls_group_id = encode_mpls_forwarding_group_id(subtype, index)
macauley_cheng67da9262015-08-31 15:18:41 +08001379 request = ofp.message.group_add(group_type=group_type,
macauley_cheng67da9262015-08-31 15:18:41 +08001380 group_id=mpls_group_id,
1381 buckets=buckets
1382 )
1383 ctrl.message_send(request)
1384 return mpls_group_id, request
macauley_chengd17ce512015-08-31 17:45:51 +08001385
1386
macauley_cheng67da9262015-08-31 15:18:41 +08001387"""
1388dislay
1389"""
macauleydbff3272015-07-30 14:07:16 +08001390def print_current_table_flow_stat(ctrl, table_id=0xff):
1391 stat_req=ofp.message.flow_stats_request()
1392 response, pkt = ctrl.transact(stat_req)
1393 if response == None:
1394 print "no response"
1395 return None
1396 print len(response.entries)
1397 for obj in response.entries:
1398 print "match ", obj.match
1399 print "cookie", obj.cookie
1400 print "priority", obj.priority
1401 print "idle_timeout", obj.idle_timeout
1402 print "hard_timeout", obj.hard_timeout
1403 #obj.actions
Flavio Castro167f5bd2015-12-02 19:33:53 -05001404 print "packet count: %lx"%obj.packet_count