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