forked from sonic-net/sonic-mgmt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnappi_fixtures.py
More file actions
executable file
·1605 lines (1376 loc) · 66.6 KB
/
Copy pathsnappi_fixtures.py
File metadata and controls
executable file
·1605 lines (1376 loc) · 66.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
This module contains the snappi fixture in the snappi_tests directory.
"""
import pytest
import time
import logging
import snappi
import sys
import random
from tests.common.helpers.assertions import pytest_require
from tests.common.errors import RunAnsibleModuleFail
from ipaddress import ip_address, IPv4Address, IPv6Address
from tests.common.fixtures.conn_graph_facts import conn_graph_facts, fanout_graph_facts # noqa: F401
from tests.common.snappi_tests.common_helpers import get_addrs_in_subnet, get_peer_snappi_chassis, \
get_ipv6_addrs_in_subnet, parse_override
from tests.common.snappi_tests.snappi_helpers import SnappiFanoutManager, get_snappi_port_location
from tests.common.snappi_tests.port import SnappiPortConfig, SnappiPortType
from tests.common.helpers.assertions import pytest_assert
from tests.common.snappi_tests.variables import pfcQueueGroupSize, pfcQueueValueDict, dut_ip_start, snappi_ip_start, \
prefix_length, dut_ipv6_start, snappi_ipv6_start, v6_prefix_length
logger = logging.getLogger(__name__)
@pytest.fixture(scope="module")
def snappi_api_serv_ip(tbinfo):
"""
In a Snappi testbed, there is no PTF docker.
Hence, we use ptf_ip field to store snappi API server.
This fixture returns the IP address of the snappi API server.
Args:
tbinfo (pytest fixture): fixture provides information about testbed
Returns:
snappi API server IP
"""
return tbinfo['ptf_ip']
@pytest.fixture(scope="module")
def snappi_api_serv_port(duthosts, rand_one_dut_hostname):
"""
This fixture returns the TCP Port of the Snappi API server.
Args:
duthost (pytest fixture): The duthost fixture.
Returns:
snappi API server port.
"""
duthost = duthosts[rand_one_dut_hostname]
return (duthost.host.options['variable_manager'].
_hostvars[duthost.hostname]['snappi_api_server']['rest_port'])
@pytest.fixture(scope='module')
def snappi_api(snappi_api_serv_ip,
snappi_api_serv_port):
"""
Fixture for session handle,
for creating snappi objects and making API calls.
Args:
snappi_api_serv_ip (pytest fixture): snappi_api_serv_ip fixture
snappi_api_serv_port (pytest fixture): snappi_api_serv_port fixture.
"""
location = "https://" + snappi_api_serv_ip + ":" + str(snappi_api_serv_port)
# TODO: Currently extension is defaulted to ixnetwork.
# Going forward, we should be able to specify extension
# from command line while running pytest.
api = snappi.api(location=location, ext="ixnetwork")
# TODO - Uncomment to use. Prefer to use environment vars to retrieve this information
# api._username = "<please mention the username if other than default username>"
# api._password = "<please mention the password if other than default password>"
yield api
if getattr(api, 'assistant', None) is not None:
api.assistant.Session.remove()
def __gen_mac(id):
"""
Generate a MAC address
Args:
id (int): Snappi port ID
Returns:
MAC address (string)
"""
return '00:11:22:33:44:{:02d}'.format(id)
def __gen_pc_mac(id):
"""
Generate a MAC address for a portchannel interface
Args:
id (int): portchannel ID
Returns:
MAC address (string)
"""
return '10:22:33:44:55:{:02d}'.format(id)
def __valid_ipv4_addr(ip):
"""
Determine if a input string is a valid IPv4 address
Args:
ip (unicode str): input IP address
Returns:
True if the input is a valid IPv4 adress or False otherwise
"""
try:
return True if type(ip_address(ip)) is IPv4Address else False
except ValueError:
return False
def __l3_intf_config(config, port_config_list, duthost, snappi_ports):
"""
Generate Snappi configuration of layer 3 interfaces
Args:
config (obj): Snappi API config of the testbed
port_config_list (list): list of Snappi port configuration information
duthost (object): device under test
snappi_ports (list): list of Snappi port information
Returns:
True if we successfully generate configuration or False
"""
mg_facts = duthost.minigraph_facts(host=duthost.hostname)['ansible_facts']
if 'minigraph_interfaces' in mg_facts:
l3_intf_facts = mg_facts['minigraph_interfaces']
else:
return True
if len(l3_intf_facts) == 0:
return True
l3_intf = {}
for v in l3_intf_facts:
if __valid_ipv4_addr(v['addr']):
l3_intf[v['attachto']] = v
dut_mac = str(duthost.facts['router_mac'])
for k, v in list(l3_intf.items()):
intf = str(k)
gw_addr = str(v['addr'])
prefix = str(v['prefixlen'])
ip = str(v['peer_addr'])
port_ids = [id for id, snappi_port in enumerate(snappi_ports)
if snappi_port['peer_port'] == intf]
if len(port_ids) != 1:
return False
port_id = port_ids[0]
mac = __gen_mac(port_id)
device = config.devices.device(
name='Device Port {}'.format(port_id))[-1]
ethernet = device.ethernets.add()
ethernet.name = 'Ethernet Port {}'.format(port_id)
ethernet.connection.port_name = config.ports[port_id].name
ethernet.mac = mac
ip_stack = ethernet.ipv4_addresses.add()
ip_stack.name = 'Ipv4 Port {}'.format(port_id)
ip_stack.address = ip
ip_stack.prefix = int(prefix)
ip_stack.gateway = gw_addr
port_config = SnappiPortConfig(id=port_id,
ip=ip,
mac=mac,
gw=gw_addr,
gw_mac=dut_mac,
prefix_len=prefix,
port_type=SnappiPortType.IPInterface,
peer_port=intf)
port_config_list.append(port_config)
return True
def __vlan_intf_config(config, port_config_list, duthost, snappi_ports):
"""
Generate Snappi configuration of Vlan interfaces
Args:
config (obj): Snappi API config of the testbed
port_config_list (list): list of Snappi port configuration information
duthost (object): device under test
snappi_ports (list): list of Snappi port information
Returns:
True if we successfully generate configuration or False
"""
mg_facts = duthost.minigraph_facts(host=duthost.hostname)['ansible_facts']
if 'minigraph_vlans' in mg_facts:
vlan_facts = mg_facts['minigraph_vlans']
else:
return True
if len(vlan_facts) == 0:
return True
vlan_member = {}
for k, v in list(vlan_facts.items()):
vlan_member[k] = v['members']
vlan_intf_facts = mg_facts['minigraph_vlan_interfaces']
vlan_intf = {}
for v in vlan_intf_facts:
if __valid_ipv4_addr(v['addr']):
vlan_intf[v['attachto']] = v
dut_mac = str(duthost.facts['router_mac'])
""" For each Vlan """
for vlan in vlan_member:
phy_intfs = vlan_member[vlan]
gw_addr = str(vlan_intf[vlan]['addr'])
prefix = str(vlan_intf[vlan]['prefixlen'])
vlan_subnet = '{}/{}'.format(gw_addr, prefix)
vlan_ip_addrs = get_addrs_in_subnet(vlan_subnet, len(phy_intfs))
""" For each physical interface attached to this Vlan """
for i in range(len(phy_intfs)):
phy_intf = phy_intfs[i]
vlan_ip_addr = vlan_ip_addrs[i]
port_ids = [id for id, snappi_port in enumerate(snappi_ports)
if snappi_port['peer_port'] == phy_intf]
if len(port_ids) != 1:
return False
port_id = port_ids[0]
mac = __gen_mac(port_id)
device = config.devices.device(
name='Device Port {}'.format(port_id))[-1]
ethernet = device.ethernets.add()
ethernet.name = 'Ethernet Port {}'.format(port_id)
ethernet.connection.port_name = config.ports[port_id].name
ethernet.mac = mac
ip_stack = ethernet.ipv4_addresses.add()
ip_stack.name = 'Ipv4 Port {}'.format(port_id)
ip_stack.address = vlan_ip_addr
ip_stack.prefix = int(prefix)
ip_stack.gateway = gw_addr
port_config = SnappiPortConfig(id=port_id,
ip=vlan_ip_addr,
mac=mac,
gw=gw_addr,
gw_mac=dut_mac,
prefix_len=prefix,
port_type=SnappiPortType.VlanMember,
peer_port=phy_intf)
port_config_list.append(port_config)
return True
def __portchannel_intf_config(config, port_config_list, duthost, snappi_ports):
"""
Generate Snappi configuration of portchannel interfaces
Args:
config (obj): Snappi API config of the testbed
port_config_list (list): list of Snappi port configuration information
duthost (object): device under test
snappi_ports (list): list of Snappi port information
Returns:
True if we successfully generate configuration or False
"""
mg_facts = duthost.minigraph_facts(host=duthost.hostname)['ansible_facts']
if 'minigraph_portchannels' in mg_facts:
pc_facts = mg_facts['minigraph_portchannels']
else:
return True
if len(pc_facts) == 0:
return True
pc_member = {}
for k, v in list(pc_facts.items()):
pc_member[k] = v['members']
pc_intf_facts = mg_facts['minigraph_portchannel_interfaces']
pc_intf = {}
for v in pc_intf_facts:
if __valid_ipv4_addr(v['addr']):
pc_intf[v['attachto']] = v
dut_mac = str(duthost.facts['router_mac'])
""" For each port channel """
pc_id = 0
for pc in pc_member:
phy_intfs = pc_member[pc]
gw_addr = str(pc_intf[pc]['addr'])
prefix = str(pc_intf[pc]['prefixlen'])
pc_ip_addr = str(pc_intf[pc]['peer_addr'])
lag = config.lags.lag(name='Lag {}'.format(pc))[-1]
lag.protocol.lacp.actor_system_id = '00:00:00:00:00:01'
lag.protocol.lacp.actor_system_priority = 1
lag.protocol.lacp.actor_key = 1
for i in range(len(phy_intfs)):
phy_intf = phy_intfs[i]
port_ids = [id for id, snappi_port in enumerate(snappi_ports)
if snappi_port['peer_port'] == phy_intf]
if len(port_ids) != 1:
return False
port_id = port_ids[0]
mac = __gen_mac(port_id)
lp = lag.ports.port(port_name=config.ports[port_id].name)[-1]
lp.lacp.actor_port_number = 1
lp.lacp.actor_port_priority = 1
lp.ethernet.name = 'Ethernet Port {}'.format(port_id)
lp.ethernet.mac = mac
port_config = SnappiPortConfig(id=port_id,
ip=pc_ip_addr,
mac=mac,
gw=gw_addr,
gw_mac=dut_mac,
prefix_len=prefix,
port_type=SnappiPortType.PortChannelMember,
peer_port=phy_intf)
port_config_list.append(port_config)
device = config.devices.device(name='Device {}'.format(pc))[-1]
ethernet = device.ethernets.add()
ethernet.connection.port_name = lag.name
ethernet.name = 'Ethernet {}'.format(pc)
ethernet.mac = __gen_pc_mac(pc_id)
ip_stack = ethernet.ipv4_addresses.add()
ip_stack.name = 'Ipv4 {}'.format(pc)
ip_stack.address = pc_ip_addr
ip_stack.prefix = int(prefix)
ip_stack.gateway = gw_addr
pc_id = pc_id + 1
return True
@pytest.fixture(scope="function")
def snappi_testbed_config(conn_graph_facts, fanout_graph_facts, # noqa: F811
duthosts, rand_one_dut_hostname, snappi_api):
"""
Geenrate snappi API config and port config information for the testbed
Args:
conn_graph_facts (pytest fixture)
fanout_graph_facts (pytest fixture)
duthosts (pytest fixture): list of DUTs
rand_one_dut_hostname (pytest fixture): DUT hostname
snappi_api(pytest fixture): Snappi API fixture
Returns:
- config (obj): Snappi API config of the testbed
- port_config_list (list): list of port configuration information
"""
# As of now both single dut and multidut fixtures are being called from the same test,
# When this function is called for T2 testbed, just return empty.
if is_snappi_multidut(duthosts):
return None, []
duthost = duthosts[rand_one_dut_hostname]
""" Generate L1 config """
snappi_fanout = get_peer_snappi_chassis(conn_data=conn_graph_facts,
dut_hostname=duthost.hostname)
pytest_assert(snappi_fanout is not None, 'Fail to get snappi_fanout')
snappi_fanout_id = list(fanout_graph_facts.keys()).index(snappi_fanout)
snappi_fanout_list = SnappiFanoutManager(fanout_graph_facts)
snappi_fanout_list.get_fanout_device_details(device_number=snappi_fanout_id)
snappi_ports = snappi_fanout_list.get_ports(peer_device=duthost.hostname)
port_speed = None
""" L1 config """
config = snappi_api.config()
for i in range(len(snappi_ports)):
config.ports.port(name='Port {}'.format(i),
location=get_snappi_port_location(snappi_ports[i]))
if port_speed is None:
port_speed = int(snappi_ports[i]['speed'])
pytest_assert(port_speed == int(snappi_ports[i]['speed']),
'Ports have different link speeds')
speed_gbps = int(port_speed/1000)
config.options.port_options.location_preemption = True
l1_config = config.layer1.layer1()[-1]
l1_config.name = 'L1 config'
l1_config.port_names = [port.name for port in config.ports]
l1_config.speed = 'speed_{}_gbps'.format(speed_gbps)
l1_config.ieee_media_defaults = False
l1_config.auto_negotiate = False
l1_config.auto_negotiation.link_training = True
l1_config.auto_negotiation.rs_fec = True
pfc = l1_config.flow_control.ieee_802_1qbb
pfc.pfc_delay = 0
if pfcQueueGroupSize == 8:
pfc.pfc_class_0 = 0
pfc.pfc_class_1 = 1
pfc.pfc_class_2 = 2
pfc.pfc_class_3 = 3
pfc.pfc_class_4 = 4
pfc.pfc_class_5 = 5
pfc.pfc_class_6 = 6
pfc.pfc_class_7 = 7
elif pfcQueueGroupSize == 4:
pfc.pfc_class_0 = pfcQueueValueDict[0]
pfc.pfc_class_1 = pfcQueueValueDict[1]
pfc.pfc_class_2 = pfcQueueValueDict[2]
pfc.pfc_class_3 = pfcQueueValueDict[3]
pfc.pfc_class_4 = pfcQueueValueDict[4]
pfc.pfc_class_5 = pfcQueueValueDict[5]
pfc.pfc_class_6 = pfcQueueValueDict[6]
pfc.pfc_class_7 = pfcQueueValueDict[7]
else:
pytest_assert(False, 'pfcQueueGroupSize value is not 4 or 8')
port_config_list = []
config_result = __vlan_intf_config(config=config,
port_config_list=port_config_list,
duthost=duthost,
snappi_ports=snappi_ports)
pytest_assert(config_result is True, 'Fail to configure Vlan interfaces')
config_result = __portchannel_intf_config(config=config,
port_config_list=port_config_list,
duthost=duthost,
snappi_ports=snappi_ports)
pytest_assert(config_result is True, 'Fail to configure portchannel interfaces')
config_result = __l3_intf_config(config=config,
port_config_list=port_config_list,
duthost=duthost,
snappi_ports=snappi_ports)
pytest_assert(config_result is True, 'Fail to configure L3 interfaces')
return config, port_config_list
@pytest.fixture(scope="module")
def tgen_ports(duthost, conn_graph_facts, fanout_graph_facts): # noqa: F811
"""
Populate tgen ports info of T0 testbed and returns as a list
Args:
duthost (pytest fixture): duthost fixture
conn_graph_facts (pytest fixture): connection graph
fanout_graph_facts (pytest fixture): fanout graph
Return:
[{'card_id': '1',
'ip': '22.1.1.2',
'ipv6': '3001::2',
'ipv6_prefix': u'64',
'location': '10.36.78.238;1;2',
'peer_device': 'sonic-s6100-dut',
'peer_ip': u'22.1.1.1',
'peer_ipv6': u'3001::1',
'peer_port': 'Ethernet8',
'port_id': '2',
'prefix': u'24',
'speed': 'speed_400_gbps'},
{'card_id': '1',
'ip': '21.1.1.2',
'ipv6': '2001::2',
'ipv6_prefix': u'64',
'location': '10.36.78.238;1;1',
'peer_device': 'sonic-s6100-dut',
'peer_ip': u'21.1.1.1',
'peer_ipv6': u'2001::1',
'peer_port': 'Ethernet0',
'port_id': '1',
'prefix': u'24',
'speed': 'speed_400_gbps'}]
"""
speed_type = {'50000': 'speed_50_gbps',
'100000': 'speed_100_gbps',
'200000': 'speed_200_gbps',
'400000': 'speed_400_gbps'}
snappi_fanout = get_peer_snappi_chassis(conn_data=conn_graph_facts,
dut_hostname=duthost.hostname)
snappi_fanout_id = list(fanout_graph_facts.keys()).index(snappi_fanout)
snappi_fanout_list = SnappiFanoutManager(fanout_graph_facts)
snappi_fanout_list.get_fanout_device_details(device_number=snappi_fanout_id)
snappi_ports = snappi_fanout_list.get_ports(peer_device=duthost.hostname)
port_speed = None
for i in range(len(snappi_ports)):
if port_speed is None:
port_speed = int(snappi_ports[i]['speed'])
elif port_speed != int(snappi_ports[i]['speed']):
""" All the ports should have the same bandwidth """
return None
config_facts = duthost.config_facts(host=duthost.hostname,
source="running")['ansible_facts']
for port in snappi_ports:
port['location'] = get_snappi_port_location(port)
port['speed'] = speed_type[port['speed']]
try:
for port in snappi_ports:
peer_port = port['peer_port']
int_addrs = list(config_facts['INTERFACE'][peer_port].keys())
ipv4_subnet = [ele for ele in int_addrs if "." in ele][0]
if not ipv4_subnet:
raise Exception("IPv4 is not configured on the interface {}".format(peer_port))
port['peer_ip'], port['prefix'] = ipv4_subnet.split("/")
port['ip'] = get_addrs_in_subnet(ipv4_subnet, 1)[0]
ipv6_subnet = [ele for ele in int_addrs if ":" in ele][0]
if not ipv6_subnet:
raise Exception("IPv6 is not configured on the interface {}".format(peer_port))
port['peer_ipv6'], port['ipv6_prefix'] = ipv6_subnet.split("/")
port['ipv6'] = get_ipv6_addrs_in_subnet(ipv6_subnet, 1)[0]
except Exception:
snappi_ports = pre_configure_dut_interface(duthost, snappi_ports)
logger.info(snappi_ports)
return snappi_ports
def snappi_multi_base_config(duthost_list,
snappi_ports,
snappi_api,
setup=True):
"""
Generate snappi API config and port config information for the testbed
This function takes care of mixed-speed interfaces by removing assert and printing info log.
l1_config is added to both the snappi_ports instead of just one.
Args:
duthost_list (pytest fixture): list of DUTs
snappi_ports: list of snappi ports
snappi_api(pytest fixture): Snappi API fixture
setup (bool): Indicates if functionality is called to create or clear the setup.
Returns:
- config (obj): Snappi API config of the testbed
- port_config_list (list): list of port configuration information
- snappi_ports (list): list of snappi_ports selected for the test.
"""
""" Generate L1 config """
config = snappi_api.config()
tgen_ports = [port['location'] for port in snappi_ports]
new_snappi_ports = [dict(list(sp.items()) + [('port_id', i)])
for i, sp in enumerate(snappi_ports) if sp['location'] in tgen_ports]
# Printing info level if ingress and egress interfaces are of different speeds.
if (len(set([sp['speed'] for sp in new_snappi_ports])) > 1):
logger.info('Rx and Tx ports have different link speeds')
[config.ports.port(name='Port {}'.format(sp['port_id']), location=sp['location']) for sp in new_snappi_ports]
# Generating L1 config for both the snappi_ports.
for port in config.ports:
for index, snappi_port in enumerate(new_snappi_ports):
if snappi_port['location'] == port.location:
l1_config = config.layer1.layer1()[-1]
l1_config.name = 'L1 config {}'.format(index)
l1_config.port_names = [port.name]
l1_config.speed = 'speed_'+str(int(int(snappi_port['speed'])/1000))+'_gbps'
l1_config.ieee_media_defaults = False
l1_config.auto_negotiate = False
l1_config.auto_negotiation.link_training = False
l1_config.auto_negotiation.rs_fec = True
pfc = l1_config.flow_control.ieee_802_1qbb
pfc.pfc_delay = 0
if pfcQueueGroupSize == 8:
pfc.pfc_class_0 = 0
pfc.pfc_class_1 = 1
pfc.pfc_class_2 = 2
pfc.pfc_class_3 = 3
pfc.pfc_class_4 = 4
pfc.pfc_class_5 = 5
pfc.pfc_class_6 = 6
pfc.pfc_class_7 = 7
elif pfcQueueGroupSize == 4:
pfc.pfc_class_0 = pfcQueueValueDict[0]
pfc.pfc_class_1 = pfcQueueValueDict[1]
pfc.pfc_class_2 = pfcQueueValueDict[2]
pfc.pfc_class_3 = pfcQueueValueDict[3]
pfc.pfc_class_4 = pfcQueueValueDict[4]
pfc.pfc_class_5 = pfcQueueValueDict[5]
pfc.pfc_class_6 = pfcQueueValueDict[6]
pfc.pfc_class_7 = pfcQueueValueDict[7]
else:
pytest_assert(False, 'pfcQueueGroupSize value is not 4 or 8')
port_config_list = []
return (setup_dut_ports(
setup=setup,
duthost_list=duthost_list,
config=config,
port_config_list=port_config_list,
snappi_ports=new_snappi_ports))
def snappi_dut_base_config(duthost_list,
snappi_ports,
snappi_api,
setup=True):
"""
Generate snappi API config and port config information for the testbed
Args:
duthost_list (pytest fixture): list of DUTs
snappi_ports: list of snappi ports
snappi_api(pytest fixture): Snappi API fixture
Returns:
- config (obj): Snappi API config of the testbed
- port_config_list (list): list of port configuration information
"""
""" Generate L1 config """
config = snappi_api.config()
tgen_ports = [port['location'] for port in snappi_ports]
new_snappi_ports = [dict(list(sp.items()) + [('port_id', i)])
for i, sp in enumerate(snappi_ports) if sp['location'] in tgen_ports]
pytest_assert(len(set([sp['speed'] for sp in new_snappi_ports])) == 1, 'Ports have different link speeds')
[config.ports.port(name='Port {}'.format(sp['port_id']), location=sp['location']) for sp in new_snappi_ports]
speed_gbps = int(int(new_snappi_ports[0]['speed'])/1000)
config.options.port_options.location_preemption = True
l1_config = config.layer1.layer1()[-1]
l1_config.name = 'L1 config'
l1_config.port_names = [port.name for port in config.ports]
l1_config.speed = 'speed_{}_gbps'.format(speed_gbps)
l1_config.ieee_media_defaults = False
l1_config.auto_negotiate = False
if is_snappi_multidut(duthost_list):
l1_config.auto_negotiation.link_training = False
else:
l1_config.auto_negotiation.link_training = True
l1_config.auto_negotiation.rs_fec = True
pfc = l1_config.flow_control.ieee_802_1qbb
pfc.pfc_delay = 0
if pfcQueueGroupSize == 8:
pfc.pfc_class_0 = 0
pfc.pfc_class_1 = 1
pfc.pfc_class_2 = 2
pfc.pfc_class_3 = 3
pfc.pfc_class_4 = 4
pfc.pfc_class_5 = 5
pfc.pfc_class_6 = 6
pfc.pfc_class_7 = 7
elif pfcQueueGroupSize == 4:
pfc.pfc_class_0 = pfcQueueValueDict[0]
pfc.pfc_class_1 = pfcQueueValueDict[1]
pfc.pfc_class_2 = pfcQueueValueDict[2]
pfc.pfc_class_3 = pfcQueueValueDict[3]
pfc.pfc_class_4 = pfcQueueValueDict[4]
pfc.pfc_class_5 = pfcQueueValueDict[5]
pfc.pfc_class_6 = pfcQueueValueDict[6]
pfc.pfc_class_7 = pfcQueueValueDict[7]
else:
pytest_assert(False, 'pfcQueueGroupSize value is not 4 or 8')
port_config_list = []
return (setup_dut_ports(
setup=setup,
duthost_list=duthost_list,
config=config,
port_config_list=port_config_list,
snappi_ports=new_snappi_ports))
def setup_dut_ports(
setup,
duthost_list,
config,
port_config_list,
snappi_ports):
for index, duthost in enumerate(duthost_list):
config_result = __vlan_intf_config(config=config,
port_config_list=port_config_list,
duthost=duthost,
snappi_ports=snappi_ports)
pytest_assert(config_result is True, 'Fail to configure Vlan interfaces')
for index, duthost in enumerate(duthost_list):
config_result = __portchannel_intf_config(config=config,
port_config_list=port_config_list,
duthost=duthost,
snappi_ports=snappi_ports)
pytest_assert(config_result is True, 'Fail to configure portchannel interfaces')
if is_snappi_multidut(duthost_list):
for index, duthost in enumerate(duthost_list):
config_result = __intf_config_multidut(
config=config,
port_config_list=port_config_list,
duthost=duthost,
snappi_ports=snappi_ports,
setup=setup)
pytest_assert(config_result is True, 'Fail to configure multidut L3 interfaces')
else:
for index, duthost in enumerate(duthost_list):
config_result = __l3_intf_config(config=config,
port_config_list=port_config_list,
duthost=duthost,
snappi_ports=snappi_ports)
pytest_assert(config_result is True, 'Fail to configure L3 interfaces')
return config, port_config_list, snappi_ports
def get_tgen_peer_ports(snappi_ports, hostname):
ports = [(port['location'], port['peer_port']) for port in snappi_ports if port['peer_device'] == hostname]
return ports
def __intf_config(config, port_config_list, duthost, snappi_ports):
"""
Generate Snappi configuration of Vlan interfaces
Args:
config (obj): Snappi API config of the testbed
port_config_list (list): list of Snappi port configuration information
duthost (object): device under test
snappi_ports (list): list of Snappi port information
Returns:
True if we successfully generate configuration or False
"""
mg_facts = duthost.minigraph_facts(host=duthost.hostname)['ansible_facts']
if 'minigraph_vlans' in mg_facts:
vlan_facts = mg_facts['minigraph_vlans']
else:
return True
if len(vlan_facts) == 0:
return True
vlan_member = {}
for k, v in vlan_facts.items():
vlan_member[k] = v['members']
vlan_intf_facts = mg_facts['minigraph_vlan_interfaces']
vlan_intf = {}
for v in vlan_intf_facts:
if __valid_ipv4_addr(v['addr']):
vlan_intf[v['attachto']] = v
dut_mac = str(duthost.facts['router_mac'])
""" For each Vlan """
for vlan in vlan_member:
phy_intfs = vlan_member[vlan]
gw_addr = str(vlan_intf[vlan]['addr'])
prefix = str(vlan_intf[vlan]['prefixlen'])
vlan_subnet = '{}/{}'.format(gw_addr, prefix)
vlan_ip_addrs = get_addrs_in_subnet(vlan_subnet, len(phy_intfs))
""" For each physical interface attached to this Vlan """
for i in range(len(phy_intfs)):
phy_intf = phy_intfs[i]
vlan_ip_addr = vlan_ip_addrs[i]
port_ids = [id for id, snappi_port in enumerate(snappi_ports)
if snappi_port['peer_port'] == phy_intf]
if len(port_ids) != 1:
return False
port_id = port_ids[0]
mac = __gen_mac(port_id)
device = config.devices.device(
name='Device Port {}'.format(port_id))[-1]
ethernet = device.ethernets.add()
ethernet.name = 'Ethernet Port {}'.format(port_id)
ethernet.connection.port_name = config.ports[port_id].name
ethernet.mac = mac
ip_stack = ethernet.ipv4_addresses.add()
ip_stack.name = 'Ipv4 Port {}'.format(port_id)
ip_stack.address = vlan_ip_addr
ip_stack.prefix = int(prefix)
ip_stack.gateway = gw_addr
port_config = SnappiPortConfig(id=port_id,
ip=vlan_ip_addr,
mac=mac,
gw=gw_addr,
gw_mac=dut_mac,
prefix_len=prefix,
port_type=SnappiPortType.VlanMember,
peer_port=phy_intf)
port_config_list.append(port_config)
return True
def __intf_config_multidut(config, port_config_list, duthost, snappi_ports, setup=True):
"""
Configures interfaces of the DUT
Args:
config (obj): Snappi API config of the testbed
port_config_list (list): list of Snappi port configuration information
duthost (object): device under test
snappi_ports (list): list of Snappi port information
setup: Setting up or teardown? True or False
Returns:
True if we successfully configure the interfaces or False
"""
dutIps = create_ip_list(dut_ip_start, len(snappi_ports), mask=prefix_length)
tgenIps = create_ip_list(snappi_ip_start, len(snappi_ports), mask=prefix_length)
ports = [port for port in snappi_ports if port['peer_device'] == duthost.hostname]
for port in ports:
port_id = port['port_id']
dutIp = dutIps[port_id]
tgenIp = tgenIps[port_id]
mac = __gen_mac(port_id)
logger.info('Configuring Dut: {} with port {} with IP {}/{}'.format(
duthost.hostname,
port['peer_port'],
dutIp,
prefix_length))
if setup:
cmd = "add"
else:
cmd = "remove"
if not setup:
static_routes_cisco_8000(tgenIp, duthost, port['peer_port'], port['asic_value'], setup)
if port['asic_value'] is None:
duthost.command('sudo config interface ip {} {} {}/{} \n' .format(
cmd,
port['peer_port'],
dutIp,
prefix_length))
else:
duthost.command('sudo config interface -n {} ip {} {} {}/{} \n' .format(
port['asic_value'],
cmd,
port['peer_port'],
dutIp,
prefix_length))
if setup:
static_routes_cisco_8000(tgenIp, duthost, port['peer_port'], port['asic_value'], setup)
if setup is False:
continue
port['intf_config_changed'] = True
device = config.devices.device(name='Device Port {}'.format(port_id))[-1]
ethernet = device.ethernets.add()
ethernet.name = 'Ethernet Port {}'.format(port_id)
ethernet.connection.port_name = config.ports[port_id].name
ethernet.mac = mac
ip_stack = ethernet.ipv4_addresses.add()
ip_stack.name = 'Ipv4 Port {}'.format(port_id)
ip_stack.address = tgenIp
ip_stack.prefix = prefix_length
ip_stack.gateway = dutIp
port_config = SnappiPortConfig(
id=port_id,
ip=tgenIp,
mac=mac,
gw=dutIp,
gw_mac=duthost.get_dut_iface_mac(port['peer_port']),
prefix_len=prefix_length,
port_type=SnappiPortType.IPInterface,
peer_port=port['peer_port']
)
port_config_list.append(port_config)
return True
def create_ip_list(value, count, mask=32, incr=0):
'''
Create a list of ips based on the count provided
Parameters:
value: start value of the list
count: number of ips required
mask: subnet mask for the ips to be created
incr: increment value of the ip
'''
if sys.version_info.major == 2:
value = unicode(value) # noqa: F821
ip_list = [value]
for i in range(1, count):
if ip_address(value).version == 4:
incr1 = pow(2, (32 - int(mask))) + incr
value = (IPv4Address(value) + incr1).compressed
elif ip_address(value).version == 6:
if mask == 32:
mask = 64
incr1 = pow(2, (128 - int(mask))) + incr
value = (IPv6Address(value) + incr1).compressed
ip_list.append(value)
return ip_list
def cleanup_config(duthost_list, snappi_ports):
for index, duthost in enumerate(duthost_list):
port_count = len(snappi_ports)
dutIps = create_ip_list(dut_ip_start, port_count, mask=prefix_length)
for port in snappi_ports:
if port['peer_device'] == duthost.hostname and port['intf_config_changed']:
port_id = port['port_id']
dutIp = dutIps[port_id]
logger.info('Removing Configuration on Dut: {} with port {} with ip :{}/{}'.format(
duthost.hostname,
port['peer_port'],
dutIp,
prefix_length))
if port['asic_value'] is None:
duthost.command('sudo config interface ip remove {} {}/{} \n' .format(
port['peer_port'],
dutIp,
prefix_length))
else:
duthost.command('sudo config interface -n {} ip remove {} {}/{} \n' .format(
port['asic_value'],
port['peer_port'],
dutIp,
prefix_length))
port['intf_config_changed'] = False
def pre_configure_dut_interface(duthost, snappi_ports):
"""
Populate tgen ports info of T0 testbed and returns as a list
Args:
duthost (pytest fixture): duthost fixture
snappi_ports: list of snappi ports
"""
dutIps = create_ip_list(dut_ip_start, len(snappi_ports), mask=prefix_length)
tgenIps = create_ip_list(snappi_ip_start, len(snappi_ports), mask=prefix_length)
dutv6Ips = create_ip_list(dut_ipv6_start, len(snappi_ports), mask=v6_prefix_length)
tgenv6Ips = create_ip_list(snappi_ipv6_start, len(snappi_ports), mask=v6_prefix_length)
snappi_ports_dut = []
for port in snappi_ports:
if port['peer_device'] == duthost.hostname:
snappi_ports_dut.append(port)
for port in snappi_ports_dut:
port_id = int(port['port_id'])-1
port['peer_ip'] = dutIps[port_id]
port['prefix'] = prefix_length
port['ip'] = tgenIps[port_id]
port['peer_ipv6'] = dutv6Ips[port_id]
port['ipv6_prefix'] = v6_prefix_length
port['ipv6'] = tgenv6Ips[port_id]
try:
logger.info('Pre-Configuring Dut: {} with port {} with IP {}/{}'.format(
duthost.hostname,
port['peer_port'],
dutIps[port_id],
prefix_length))
duthost.command('sudo config interface ip add {} {}/{} \n' .format(
port['peer_port'],
dutIps[port_id],
prefix_length))
logger.info('Pre-Configuring Dut: {} with port {} with IPv6 {}/{}'.format(
duthost.hostname,
port['peer_port'],
dutv6Ips[port_id],
v6_prefix_length))
duthost.command('sudo config interface ip add {} {}/{} \n' .format(
port['peer_port'],
dutv6Ips[port_id],
v6_prefix_length))
except Exception:
pytest_assert(False, "Unable to configure ip on the interface {}".format(port['peer_port']))
return snappi_ports_dut
@pytest.fixture(scope="module")