blob: e20e0fdd72b0912a908fccc3f4bb9e696c907bc1 [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
43
macauley97557232015-07-16 17:28:07 +080044def encode_l2_interface_group_id(vlan, id):
45 return id + (vlan << OFDPA_VLAN_ID_SHIFT)
46
47def encode_l2_rewrite_group_id(id):
48 return id + (1 << OFDPA_GROUP_TYPE_SHIFT)
49
50def encode_l3_unicast_group_id(id):
51 return id + (2 << OFDPA_GROUP_TYPE_SHIFT)
52
53def encode_l2_mcast_group_id(vlan, id):
54 return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (3 << OFDPA_GROUP_TYPE_SHIFT)
55
56def encode_l2_flood_group_id(vlan, id):
57 return id + (vlan << OFDPA_VLAN_ID_SHIFT) + (4 << OFDPA_GROUP_TYPE_SHIFT)
58
59def encode_l3_interface_group_id(id):
60 return id + (5 << OFDPA_GROUP_TYPE_SHIFT)
61
62def encode_l3_mcast_group_id(vlan, id):
63 return id + (vlan << OFDPA_VLAN_ID_SHIFT)+(6 << OFDPA_GROUP_TYPE_SHIFT)
64
65def encode_l3_ecmp_group_id(id):
66 return id + (7 << OFDPA_GROUP_TYPE_SHIFT)
67
macauleyfddc4662015-07-27 17:40:30 +080068def encode_l2_overlay_group_id(tunnel_id, subtype, index):
69 tunnel_id=tunnel_id&0xffff #16 bits
70 subtype = subtype&3 #2 bits
71 index = index & 0x3f #10 bits
72 return index + (tunnel_id << OFDPA_TUNNEL_ID_SHIFT)+ (subtype<<OFDPA_TUNNEL_SUBTYPE_SHIFT)+(8 << OFDPA_GROUP_TYPE_SHIFT)
macauley97557232015-07-16 17:28:07 +080073
macauley97557232015-07-16 17:28:07 +080074def add_l2_interface_grouop(ctrl, ports, vlan_id=1, is_tagged=False, send_barrier=False):
75 # group table
76 # set up untag groups for each port
macauley41904ed2015-07-16 17:38:35 +080077 group_id_list=[]
macauley15909e72015-07-17 15:58:57 +080078 msgs=[]
macauley97557232015-07-16 17:28:07 +080079 for of_port in ports:
80 # do stuff
81 group_id = encode_l2_interface_group_id(vlan_id, of_port)
macauley41904ed2015-07-16 17:38:35 +080082 group_id_list.append(group_id)
macauley97557232015-07-16 17:28:07 +080083 if is_tagged:
84 actions = [
85 ofp.action.output(of_port),
86 ]
87 else:
88 actions = [
89 ofp.action.pop_vlan(),
90 ofp.action.output(of_port),
91 ]
92
93 buckets = [
94 ofp.bucket(actions=actions),
95 ]
96
97 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
98 group_id=group_id,
99 buckets=buckets
100 )
101 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800102 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800103
104 if send_barrier:
105 do_barrier(ctrl)
macauley41904ed2015-07-16 17:38:35 +0800106
macauley15909e72015-07-17 15:58:57 +0800107 return group_id_list, msgs
macauley97557232015-07-16 17:28:07 +0800108
macauley0f91a3e2015-07-17 18:09:59 +0800109def add_one_l2_interface_grouop(ctrl, port, vlan_id=1, is_tagged=False, send_barrier=False):
110 # group table
111 # set up untag groups for each port
112 group_id = encode_l2_interface_group_id(vlan_id, port)
113
114 if is_tagged:
115 actions = [
116 ofp.action.output(port),
117 ]
118 else:
119 actions = [
120 ofp.action.pop_vlan(),
121 ofp.action.output(port),
122 ]
123
124 buckets = [
125 ofp.bucket(actions=actions),
126 ]
127
128 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
129 group_id=group_id,
130 buckets=buckets
131 )
132 ctrl.message_send(request)
133
134 if send_barrier:
135 do_barrier(ctrl)
136
137 return group_id, request
138
macauley97557232015-07-16 17:28:07 +0800139def add_l2_mcast_group(ctrl, ports, vlanid, mcast_grp_index):
140 buckets=[]
141 for of_port in ports:
142 group_id = encode_l2_interface_group_id(vlanid, of_port)
143 action=[ofp.action.group(group_id)]
144 buckets.append(ofp.bucket(actions=action))
145
146 group_id =encode_l2_mcast_group_id(vlanid, mcast_grp_index)
147 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
148 group_id=group_id,
149 buckets=buckets
150 )
151 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800152 return request
macauley97557232015-07-16 17:28:07 +0800153
macauley15909e72015-07-17 15:58:57 +0800154def add_l2_flood_group(ctrl, ports, vlanid, id):
155 buckets=[]
156 for of_port in ports:
157 group_id = encode_l2_interface_group_id(vlanid, of_port)
158 action=[ofp.action.group(group_id)]
159 buckets.append(ofp.bucket(actions=action))
macauley97557232015-07-16 17:28:07 +0800160
macauley15909e72015-07-17 15:58:57 +0800161 group_id =encode_l2_flood_group_id(vlanid, id)
162 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
163 group_id=group_id,
164 buckets=buckets
165 )
166 ctrl.message_send(request)
167 return request
168
169def add_l2_rewrite_group(ctrl, port, vlanid, id, src_mac, dst_mac):
170 group_id = encode_l2_interface_group_id(vlanid, port)
171
172 action=[]
173 if src_mac is not None:
174 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
175
176 if dst_mac is not None:
177 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
178
179 action.append(ofp.action.group(group_id))
180
181 buckets = [ofp.bucket(actions=action)]
182
183 group_id =encode_l2_rewrite_group_id(id)
184 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
185 group_id=group_id,
186 buckets=buckets
187 )
188 ctrl.message_send(request)
189 return request
190
191def add_l3_unicast_group(ctrl, port, vlanid, id, src_mac, dst_mac):
192 group_id = encode_l2_interface_group_id(vlanid, port)
193
194 action=[]
195 if src_mac is not None:
196 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
197
198 if dst_mac is not None:
199 action.append(ofp.action.set_field(ofp.oxm.eth_dst(dst_mac)))
200
201 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vlanid)))
202
203 action.append(ofp.action.group(group_id))
204
205 buckets = [ofp.bucket(actions=action)]
206
207 group_id =encode_l3_unicast_group_id(id)
208 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
209 group_id=group_id,
210 buckets=buckets
211 )
212 ctrl.message_send(request)
213 return request
214
215def add_l3_interface_group(ctrl, port, vlanid, id, src_mac):
216 group_id = encode_l2_interface_group_id(vlanid, port)
217
218 action=[]
219 action.append(ofp.action.set_field(ofp.oxm.eth_src(src_mac)))
220 action.append(ofp.action.set_field(ofp.oxm.vlan_vid(vlanid)))
221 action.append(ofp.action.group(group_id))
222
223 buckets = [ofp.bucket(actions=action)]
224
225 group_id =encode_l3_interface_group_id(id)
226 request = ofp.message.group_add(group_type=ofp.OFPGT_INDIRECT,
227 group_id=group_id,
228 buckets=buckets
229 )
230 ctrl.message_send(request)
231 return request
232
233def add_l3_ecmp_group(ctrl, id, l3_ucast_groups):
234 buckets=[]
235 for group in l3_ucast_groups:
236 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
237
238 group_id =encode_l3_ecmp_group_id(id)
239 request = ofp.message.group_add(group_type=ofp.OFPGT_SELECT,
240 group_id=group_id,
241 buckets=buckets
242 )
243 ctrl.message_send(request)
244 return request
245
246def add_l3_mcast_group(ctrl, vid, mcast_group_id, groups_on_buckets):
247 buckets=[]
248 for group in groups_on_buckets:
249 buckets.append(ofp.bucket(actions=[ofp.action.group(group)]))
250
251 group_id =encode_l3_mcast_group_id(vid, mcast_group_id)
252 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
253 group_id=group_id,
254 buckets=buckets
255 )
256 ctrl.message_send(request)
257 return request
macauleyfddc4662015-07-27 17:40:30 +0800258
259def add_l2_overlay_flood_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
260 buckets=[]
261 for port in ports:
262 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
263
264 group_id=encode_l2_overlay_group_id(tunnel_id, 0, index)
265 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
266 group_id=group_id,
267 buckets=buckets
268 )
269 ctrl.message_send(request)
270 return request
271
272def add_l2_overlay_flood_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
273 buckets=[]
274 for port in ports:
275 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
276
277 group_id=encode_l2_overlay_group_id(tunnel_id, 1, index)
278 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
279 group_id=group_id,
280 buckets=buckets
281 )
282 ctrl.message_send(request)
283 return request
284
285def add_l2_overlay_mcast_over_unicast_tunnel_group(ctrl, tunnel_id, ports, index):
286 buckets=[]
287 for port in ports:
288 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
289
290 group_id=encode_l2_overlay_group_id(tunnel_id, 2, index)
291 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
292 group_id=group_id,
293 buckets=buckets
294 )
295 ctrl.message_send(request)
296 return request
297
298def add_l2_overlay_mcast_over_mcast_tunnel_group(ctrl, tunnel_id, ports, index):
299 buckets=[]
300 for port in ports:
301 buckets.append(ofp.bucket(actions=[ofp.action.output(port)]))
302
303 group_id=encode_l2_overlay_group_id(tunnel_id, 3, index)
304 request = ofp.message.group_add(group_type=ofp.OFPGT_ALL,
305 group_id=group_id,
306 buckets=buckets
307 )
308 ctrl.message_send(request)
309 return request
310
311def add_port_table_flow(ctrl, is_overlay=True):
312 match = ofp.match()
313
314 if is_overlay == True:
315 match.oxm_list.append(ofp.oxm.in_port(0x10000))
macauleydbff3272015-07-30 14:07:16 +0800316 NEXT_TABLE=50
macauleyfddc4662015-07-27 17:40:30 +0800317 else:
318 match.oxm_list.append(ofp.oxm.in_port(0))
macauleydbff3272015-07-30 14:07:16 +0800319 NEXT_TABLE=10
macauleyfddc4662015-07-27 17:40:30 +0800320
321 request = ofp.message.flow_add(
322 table_id=0,
323 cookie=42,
324 match=match,
325 instructions=[
macauleydbff3272015-07-30 14:07:16 +0800326 ofp.instruction.goto_table(NEXT_TABLE)
macauleyfddc4662015-07-27 17:40:30 +0800327 ],
328 priority=0)
329 logging.info("Add port table, match port %lx" % 0x10000)
330 ctrl.message_send(request)
macauleydbff3272015-07-30 14:07:16 +0800331
332
macauleyfddc4662015-07-27 17:40:30 +0800333
macauley97557232015-07-16 17:28:07 +0800334def add_vlan_table_flow(ctrl, ports, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
335 # table 10: vlan
336 # goto to table 20
macauley15909e72015-07-17 15:58:57 +0800337 msgs=[]
macauley97557232015-07-16 17:28:07 +0800338 for of_port in ports:
339 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
340 match = ofp.match()
341 match.oxm_list.append(ofp.oxm.in_port(of_port))
342 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
343 request = ofp.message.flow_add(
344 table_id=10,
345 cookie=42,
346 match=match,
347 instructions=[
348 ofp.instruction.goto_table(20)
349 ],
350 priority=0)
351 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
352 ctrl.message_send(request)
353
354 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
355 match = ofp.match()
356 match.oxm_list.append(ofp.oxm.in_port(of_port))
357 match.oxm_list.append(ofp.oxm.vlan_vid(0))
358 request = ofp.message.flow_add(
359 table_id=10,
360 cookie=42,
361 match=match,
362 instructions=[
363 ofp.instruction.apply_actions(
364 actions=[
365 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
366 ]
367 ),
368 ofp.instruction.goto_table(20)
369 ],
370 priority=0)
371 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
372 ctrl.message_send(request)
macauley15909e72015-07-17 15:58:57 +0800373 msgs.append(request)
macauley97557232015-07-16 17:28:07 +0800374
375 if send_barrier:
376 do_barrier(ctrl)
377
macauley15909e72015-07-17 15:58:57 +0800378 return msgs
macauley0f91a3e2015-07-17 18:09:59 +0800379
380def add_one_vlan_table_flow(ctrl, of_port, vlan_id=1, flag=VLAN_TABLE_FLAG_ONLY_BOTH, send_barrier=False):
381 # table 10: vlan
382 # goto to table 20
383 if (flag == VLAN_TABLE_FLAG_ONLY_TAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
384 match = ofp.match()
385 match.oxm_list.append(ofp.oxm.in_port(of_port))
386 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlan_id))
387 request = ofp.message.flow_add(
388 table_id=10,
389 cookie=42,
390 match=match,
391 instructions=[
392 ofp.instruction.goto_table(20)
393 ],
394 priority=0)
395 logging.info("Add vlan %d tagged packets on port %d and go to table 20" %( vlan_id, of_port))
396 ctrl.message_send(request)
397
398 if (flag == VLAN_TABLE_FLAG_ONLY_UNTAG) or (flag == VLAN_TABLE_FLAG_ONLY_BOTH):
399 match = ofp.match()
400 match.oxm_list.append(ofp.oxm.in_port(of_port))
401 match.oxm_list.append(ofp.oxm.vlan_vid(0))
402 request = ofp.message.flow_add(
403 table_id=10,
404 cookie=42,
405 match=match,
406 instructions=[
407 ofp.instruction.apply_actions(
408 actions=[
409 ofp.action.set_field(ofp.oxm.vlan_vid(0x1000+vlan_id))
410 ]
411 ),
412 ofp.instruction.goto_table(20)
413 ],
414 priority=0)
415 logging.info("Add vlan %d untagged packets on port %d and go to table 20" % (vlan_id, of_port))
416 ctrl.message_send(request)
417
418 if send_barrier:
419 do_barrier(ctrl)
420
421 return request
macauley15909e72015-07-17 15:58:57 +0800422
macauley97557232015-07-16 17:28:07 +0800423def add_bridge_flow(ctrl, dst_mac, vlanid, group_id, send_barrier=False):
424 match = ofp.match()
macauleyfddc4662015-07-27 17:40:30 +0800425 if dst_mac!=None:
426 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
427
macauley97557232015-07-16 17:28:07 +0800428 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
macauleyfddc4662015-07-27 17:40:30 +0800429
macauley97557232015-07-16 17:28:07 +0800430 request = ofp.message.flow_add(
431 table_id=50,
432 cookie=42,
433 match=match,
434 instructions=[
435 ofp.instruction.write_actions(
436 actions=[
437 ofp.action.group(group_id)]),
438 ofp.instruction.goto_table(60)
439 ],
440 buffer_id=ofp.OFP_NO_BUFFER,
441 priority=1000)
442
443 logging.info("Inserting Brdige flow vlan %d, mac %s", vlanid, dst_mac)
444 ctrl.message_send(request)
445
446 if send_barrier:
macauley15909e72015-07-17 15:58:57 +0800447 do_barrier(ctrl)
448
macauley0f91a3e2015-07-17 18:09:59 +0800449 return request
macauleyfddc4662015-07-27 17:40:30 +0800450
451def add_overlay_bridge_flow(ctrl, dst_mac, vnid, group_id, is_group=True, send_barrier=False):
452 match = ofp.match()
453 if dst_mac!=None:
454 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
455
456 match.oxm_list.append(ofp.oxm.tunnel_id(vnid))
457 if is_group == True:
458 actions=[ofp.action.group(group_id)]
459 else:
460 actions=[ofp.action.output(group_id)]
461
462 request = ofp.message.flow_add(
463 table_id=50,
464 cookie=42,
465 match=match,
466 instructions=[
467 ofp.instruction.write_actions(
468 actions=actions),
469 ofp.instruction.goto_table(60)
470 ],
471 buffer_id=ofp.OFP_NO_BUFFER,
472 priority=1000)
473
474 logging.info("Inserting Brdige flow vnid %d, mac %s", vnid, dst_mac)
475 ctrl.message_send(request)
476
477 if send_barrier:
478 do_barrier(ctrl)
479
480 return request
macauley0f91a3e2015-07-17 18:09:59 +0800481
482def add_termination_flow(ctrl, in_port, eth_type, dst_mac, vlanid, send_barrier=False):
483 match = ofp.match()
macauley0f91a3e2015-07-17 18:09:59 +0800484 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauleyfddc4662015-07-27 17:40:30 +0800485 if dst_mac[0]&0x01 == 0x01:
486 match.oxm_list.append(ofp.oxm.eth_dst_masked(dst_mac, [0xff, 0xff, 0xff, 0x80, 0x00, 0x00]))
487 goto_table=40
488 else:
489 match.oxm_list.append(ofp.oxm.in_port(in_port))
490 match.oxm_list.append(ofp.oxm.eth_dst(dst_mac))
491 match.oxm_list.append(ofp.oxm.vlan_vid(0x1000+vlanid))
492 goto_table=30
macauley0f91a3e2015-07-17 18:09:59 +0800493
494 request = ofp.message.flow_add(
495 table_id=20,
496 cookie=42,
497 match=match,
498 instructions=[
499 ofp.instruction.goto_table(goto_table)
500 ],
501 buffer_id=ofp.OFP_NO_BUFFER,
502 priority=1)
503
504 logging.info("Inserting termination flow inport %d, eth_type %lx, vlan %d, mac %s", in_port, eth_type, vlanid, dst_mac)
505 ctrl.message_send(request)
506
507 if send_barrier:
508 do_barrier(ctrl)
509
510 return request
511
macauleyf8b1acd2015-07-23 15:13:13 +0800512def add_unicast_routing_flow(ctrl, eth_type, dst_ip, mask, action_group_id, send_barrier=False):
macauley0f91a3e2015-07-17 18:09:59 +0800513 match = ofp.match()
514 match.oxm_list.append(ofp.oxm.eth_type(eth_type))
macauleyf8b1acd2015-07-23 15:13:13 +0800515 if mask!=0:
516 match.oxm_list.append(ofp.oxm.ipv4_dst_masked(dst_ip, mask))
517 else:
518 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
macauley0f91a3e2015-07-17 18:09:59 +0800519
520 request = ofp.message.flow_add(
521 table_id=30,
522 cookie=42,
523 match=match,
524 instructions=[
525 ofp.instruction.write_actions(
526 actions=[ofp.action.group(action_group_id)]),
527 ofp.instruction.goto_table(60)
528 ],
529 buffer_id=ofp.OFP_NO_BUFFER,
530 priority=1)
531
532 logging.info("Inserting unicast routing flow eth_type %lx, dip %ld",eth_type, dst_ip)
533 ctrl.message_send(request)
534
535 if send_barrier:
536 do_barrier(ctrl)
537
macauleyfddc4662015-07-27 17:40:30 +0800538 return request
539
540def add_mcast4_routing_flow(ctrl, vlan_id, src_ip, src_ip_mask, dst_ip, action_group_id, send_barrier=False):
541 match = ofp.match()
542 match.oxm_list.append(ofp.oxm.eth_type(0x0800))
543 match.oxm_list.append(ofp.oxm.vlan_vid(vlan_id))
544 if src_ip_mask!=0:
545 match.oxm_list.append(ofp.oxm.ipv4_src_masked(src_ip, src_ip_mask))
546 else:
547 match.oxm_list.append(ofp.oxm.ipv4_src(src_ip))
548
549 match.oxm_list.append(ofp.oxm.ipv4_dst(dst_ip))
550
551 request = ofp.message.flow_add(
552 table_id=40,
553 cookie=42,
554 match=match,
555 instructions=[
556 ofp.instruction.write_actions(
557 actions=[ofp.action.group(action_group_id)]),
558 ofp.instruction.goto_table(60)
559 ],
560 buffer_id=ofp.OFP_NO_BUFFER,
561 priority=1)
562
563 logging.info("Inserting mcast routing flow eth_type %lx, dip %lx, sip %lx, sip_mask %lx",0x0800, dst_ip, src_ip, src_ip_mask)
564 ctrl.message_send(request)
565
566 if send_barrier:
567 do_barrier(ctrl)
568
569 return request
570
571def get_vtap_lport_config_xml(dp_id, lport, phy_port, vlan, vnid, operation='merge'):
572 """
573 Command Example:
574 of-agent vtap 10001 ethernet 1/1 vid 1
575 of-agent vtp 10001 vni 10
576 """
577 if vlan != 0:
578 config_vtap_xml="""
579 <config>
580 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
581 <id>capable-switch-1</id>
582 <resources>
583 <port xc:operation="OPERATION">
584 <resource-id >LPORT</resource-id>
585 <features>
586 <current>
587 <rate>10Gb</rate>
588 <medium>fiber</medium>
589 <pause>symmetric</pause>
590 </current>
591 <advertised>
592 <rate>10Gb</rate>
593 <rate>100Gb</rate>
594 <medium>fiber</medium>
595 <pause>symmetric</pause>
596 </advertised>
597 <supported>
598 <rate>10Gb</rate>
599 <rate>100Gb</rate>
600 <medium>fiber</medium>
601 <pause>symmetric</pause>
602 </supported>
603 <advertised-peer>
604 <rate>10Gb</rate>
605 <rate>100Gb</rate>
606 <medium>fiber</medium>
607 <pause>symmetric</pause>
608 </advertised-peer>
609 </features>
610 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
611 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
612 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
613 <ofdpa10:vni>VNID</ofdpa10:vni>
614 </ofdpa10:vtap>
615 </port>
616 </resources>
617 <logical-switches>
618 <switch>
619 <id>DATAPATH_ID</id>
620 <datapath-id>DATAPATH_ID</datapath-id>
621 <resources>
622 <port xc:operation="OPERATION">LPORT</port>
623 </resources>
624 </switch>
625 </logical-switches>
626 </capable-switch>
627 </config>
628 """
629 else:
630 config_vtap_xml="""
631 <config>
632 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
633 <id>capable-switch-1</id>
634 <resources>
635 <port xc:operation="OPERATION">
636 <resource-id >LPORT</resource-id>
637 <features>
638 <current>
639 <rate>10Gb</rate>
640 <medium>fiber</medium>
641 <pause>symmetric</pause>
642 </current>
643 <advertised>
644 <rate>10Gb</rate>
645 <rate>100Gb</rate>
646 <medium>fiber</medium>
647 <pause>symmetric</pause>
648 </advertised>
649 <supported>
650 <rate>10Gb</rate>
651 <rate>100Gb</rate>
652 <medium>fiber</medium>
653 <pause>symmetric</pause>
654 </supported>
655 <advertised-peer>
656 <rate>10Gb</rate>
657 <rate>100Gb</rate>
658 <medium>fiber</medium>
659 <pause>symmetric</pause>
660 </advertised-peer>
661 </features>
662 <ofdpa10:vtap xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
663 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
664 <ofdpa10:vni>VNID</ofdpa10:vni>
665 </ofdpa10:vtap>
666 </port>
667 </resources>
668 <logical-switches>
669 <switch>
670 <id>DATAPATH_ID</id>
671 <datapath-id>DATAPATH_ID</datapath-id>
672 <resources>
673 <port xc:operation="OPERATION">LPORT</port>
674 </resources>
675 </switch>
676 </logical-switches>
677 </capable-switch>
678 </config>
679 """
680 str_datapath_id_f= "{:016x}".format(dp_id)
681 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
682 config_vtap_xml=config_vtap_xml.replace("DATAPATH_ID", str_datapath_id)
683 config_vtap_xml=config_vtap_xml.replace("LPORT", str(hex(lport)))
684 config_vtap_xml=config_vtap_xml.replace("PHY_PORT", str(phy_port))
685 config_vtap_xml=config_vtap_xml.replace("VLAN_ID", str(vlan))
686 config_vtap_xml=config_vtap_xml.replace("VNID", str(vnid))
687 config_vtap_xml=config_vtap_xml.replace("OPERATION", str(operation))
688 return config_vtap_xml
689
690def get_vtep_lport_config_xml(dp_id, lport, src_ip, dst_ip, next_hop_id, vnid, udp_src_port=6633, ttl=25, operation='merge'):
691 """
692 Command Example:
693 of-agent vtep 10002 source user-input-src-ip destination user-input-dst-ip udp-source-port 6633 nexthop 2 ttl 25
694 of-agent vtp 10001 vni 10
695 """
696
697 config_vtep_xml="""
698 <config>
699 <capable-switch xmlns="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
700 <id>capable-switch-1</id>
701 <resources>
702 <port xc:operation="OPERATION">
703 <resource-id>LPORT</resource-id>
704 <features>
705 <current>
706 <rate>10Gb</rate>
707 <medium>fiber</medium>
708 <pause>symmetric</pause>
709 </current>
710 <advertised>
711 <rate>10Gb</rate>
712 <rate>100Gb</rate>
713 <medium>fiber</medium>
714 <pause>symmetric</pause>
715 </advertised>
716 <supported>
717 <rate>10Gb</rate>
718 <rate>100Gb</rate>
719 <medium>fiber</medium>
720 <pause>symmetric</pause>
721 </supported>
722 <advertised-peer>
723 <rate>10Gb</rate>
724 <rate>100Gb</rate>
725 <medium>fiber</medium>
726 <pause>symmetric</pause>
727 </advertised-peer>
728 </features>
729 <ofdpa10:vtep xmlns:ofdpa10="urn:bcm:ofdpa10:accton01">
730 <ofdpa10:src-ip>SRC_IP</ofdpa10:src-ip>
731 <ofdpa10:dest-ip>DST_IP</ofdpa10:dest-ip>
732 <ofdpa10:udp-src-port>UDP_SRC_PORT</ofdpa10:udp-src-port>
733 <ofdpa10:vni>VNID</ofdpa10:vni>
734 <ofdpa10:nexthop-id>NEXT_HOP_ID</ofdpa10:nexthop-id>
735 <ofdpa10:ttl>TTL</ofdpa10:ttl>
736 </ofdpa10:vtep>
737 </port>
738 </resources>
739 <logical-switches>
740 <switch>
741 <id>DATAPATH_ID</id>
742 <datapath-id>DATAPATH_ID</datapath-id>
743 <resources>
744 <port xc:operation="OPERATION">LPORT</port>
745 </resources>
746 </switch>
747 </logical-switches>
748 </capable-switch>
749 </config>
750 """
751 str_datapath_id_f= "{:016x}".format(dp_id)
752 str_datapath_id=':'.join([str_datapath_id_f[i:i+2] for i in range(0, len(str_datapath_id_f), 2)])
753 config_vtep_xml=config_vtep_xml.replace("DATAPATH_ID", str_datapath_id)
754 config_vtep_xml=config_vtep_xml.replace("LPORT", str(hex(lport)))
755 config_vtep_xml=config_vtep_xml.replace("SRC_IP", str(src_ip))
756 config_vtep_xml=config_vtep_xml.replace("DST_IP", str(dst_ip))
757 config_vtep_xml=config_vtep_xml.replace("UDP_SRC_PORT", str(udp_src_port))
758 config_vtep_xml=config_vtep_xml.replace("NEXT_HOP_ID", str(next_hop_id))
759 config_vtep_xml=config_vtep_xml.replace("TTL", str(ttl))
760 config_vtep_xml=config_vtep_xml.replace("VNID", str(vnid))
761 config_vtep_xml=config_vtep_xml.replace("OPERATION", str(operation))
762
763 return config_vtep_xml
764
765def get_next_hop_config_xml(next_hop_id, dst_mac, phy_port, vlan, operation='merge'):
766 #of-agent nexthop 2 destination user-input-dst-mac ethernet 1/2 vid 2
767 config_nexthop_xml="""
768 <config>
769 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
770 <ofdpa10:next-hop xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
771 <ofdpa10:id>NEXT_HOP_ID</ofdpa10:id>
772 <ofdpa10:dest-mac>DST_MAC</ofdpa10:dest-mac>
773 <ofdpa10:phy-port>PHY_PORT</ofdpa10:phy-port>
774 <ofdpa10:vid>VLAN_ID</ofdpa10:vid>
775 </ofdpa10:next-hop>
776 </of11-config:capable-switch>
777 </config>
778 """
779 config_nexthop_xml=config_nexthop_xml.replace("VLAN_ID", str(vlan))
780 config_nexthop_xml=config_nexthop_xml.replace("PHY_PORT", str(phy_port))
781 config_nexthop_xml=config_nexthop_xml.replace("NEXT_HOP_ID", str(next_hop_id))
782 config_nexthop_xml=config_nexthop_xml.replace("DST_MAC", str(dst_mac))
783 config_nexthop_xml=config_nexthop_xml.replace("OPERATION", str(operation))
784 return config_nexthop_xml
785
786def get_vni_config_xml(vni_id, mcast_ipv4, next_hop_id, operation='merge'):
787 #of-agent vni 10 multicast 224.1.1.1 nexthop 20
788 if mcast_ipv4!=None:
789 config_vni_xml="""
790 <config>
791 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
792 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
793 <ofdpa10:id>VNID</ofdpa10:id>
794 <ofdpa10:vni-multicast-group>MCAST_IP</ofdpa10:vni-multicast-group>
795 <ofdpa10:multicast-group-nexthop-id>NEXT_HOP_ID</ofdpa10:multicast-group-nexthop-id>
796 </ofdpa10:vni>
797 </of11-config:capable-switch>
798 </config>
799 """
800 config_vni_xml=config_vni_xml.replace("NEXT_HOP_ID", str(next_hop_id))
801 config_vni_xml=config_vni_xml.replace("MCAST_IP", str(mcast_ipv4))
802 else:
803 config_vni_xml="""
804 <config>
805 <of11-config:capable-switch xmlns:of11-config="urn:onf:of111:config:yang" xmlns:xc="urn:ietf:params:xml:ns:netconf:base:1.0">
806 <ofdpa10:vni xmlns:ofdpa10="urn:bcm:ofdpa10:accton01" xc:operation="OPERATION">
807 <ofdpa10:id>VNID</ofdpa10:id>
808 </ofdpa10:vni>
809 </of11-config:capable-switch>
810 </config>
811 """
812
813 config_vni_xml=config_vni_xml.replace("VNID", str(vni_id))
814 config_vni_xml=config_vni_xml.replace("OPERATION", str(operation))
815 return config_vni_xml
816
817def get_featureReplay(self):
818 req = ofp.message.features_request()
819 res, raw = self.controller.transact(req)
820 self.assertIsNotNone(res, "Did not receive a response from the DUT.")
821 self.assertEqual(res.type, ofp.OFPT_FEATURES_REPLY,
822 ("Unexpected packet type %d received in response to "
823 "OFPT_FEATURES_REQUEST") % res.type)
824 return res
825
826def send_edit_config(switch_ip, xml, target='runing'):
827 NETCONF_ACCOUNT="netconfuser"
828 NETCONF_PASSWD="netconfuser"
829 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
830 try:
831 m.edit_config(target='running',
832 config=xml,
833 default_operation='merge',
834 error_option='stop-on-error')
835
836 except Exception as e:
837 logging.info("Fail to set xml %s", xml)
838 return False
839
840 #return m.get_config(source='running').data_xml
841 return True
842
843def send_delete_config(switch_ip, xml, target='runing'):
844 NETCONF_ACCOUNT="netconfuser"
845 NETCONF_PASSWD="netconfuser"
846 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
847 try:
848 m.edit_config(target='running',
849 config=xml,
850 default_operation='delete',
851 error_option='stop-on-error')
852
853 except Exception as e:
854 logging.info("Fail to set xml %s", xml)
855 return False
856
857 #return m.get_config(source='running').data_xml
858 return True
859
860def get_edit_config(switch_ip, target='runing'):
861 NETCONF_ACCOUNT="netconfuser"
862 NETCONF_PASSWD="netconfuser"
863 with manager.connect_ssh(host=switch_ip, port=830, username=NETCONF_ACCOUNT, password=NETCONF_PASSWD, hostkey_verify=False ) as m:
864 print m.get_config(source='running').data_xml
macauleydbff3272015-07-30 14:07:16 +0800865
866
867def print_current_table_flow_stat(ctrl, table_id=0xff):
868 stat_req=ofp.message.flow_stats_request()
869 response, pkt = ctrl.transact(stat_req)
870 if response == None:
871 print "no response"
872 return None
873 print len(response.entries)
874 for obj in response.entries:
875 print "match ", obj.match
876 print "cookie", obj.cookie
877 print "priority", obj.priority
878 print "idle_timeout", obj.idle_timeout
879 print "hard_timeout", obj.hard_timeout
880 #obj.actions
881 print "packet count: %lx"%obj.packet_count