56
56
"1x1" : { "lanes" :4 , "speed" :1000 , "step" :4 , "bko" :0 , "name" : "etp" },
57
57
"4x10" : { "lanes" :4 , "speed" :10000 , "step" :1 , "bko" :1 , "name" : "etp" },
58
58
"4x25" : { "lanes" :4 , "speed" :25000 , "step" :1 , "bko" :1 , "name" : "etp" },
59
- "2x50" : { "lanes" :4 , "speed" :25000 , "step" :2 , "bko" :1 , "name" : "etp" },
59
+ "2x50" : { "lanes" :4 , "speed" :50000 , "step" :2 , "bko" :1 , "name" : "etp" },
60
60
}
61
61
62
62
bko_dict_8 = {
@@ -218,7 +218,7 @@ def check_json_lanes_with_bko(self, data, port_idx):
218
218
return entry
219
219
return None
220
220
221
- def write_json_lanes_to_ini_file (self , data , port_idx , port_split , f_out ):
221
+ def write_json_lanes_to_pi_list (self , data , port_idx , port_split , pi_list ):
222
222
# Function to write line of port_config.ini corresponding to a port
223
223
step = self .bko_dict [port_split ]["step" ]
224
224
for i in range (0 ,self .base_lanes ,step ):
@@ -227,18 +227,14 @@ def write_json_lanes_to_ini_file(self, data, port_idx, port_split, f_out):
227
227
curr_speed = curr_port_dict .get ("speed" )
228
228
curr_alias = curr_port_dict .get ("alias" )
229
229
curr_lanes = curr_port_dict .get ("lanes" )
230
- curr_index = port_idx / self .base_lanes
231
- out_str = "{:15s} {:20s} {:11s} {:9s} {:10s}\n " .format (curr_port_str ,curr_lanes ,curr_alias ,str (curr_index ),str (curr_speed ))
232
- if self .print_mode == True :
233
- print (out_str )
234
- else :
235
- f_out .write (out_str )
236
- if self .verbose and (self .print_mode == False ):
237
- print (out_str )
230
+ curr_index = int (port_idx / self .base_lanes ) + 1
231
+ curr_port_info = [curr_port_str , curr_lanes , curr_alias , curr_index , curr_speed ]
232
+ pi_list .append (curr_port_info )
238
233
return
239
234
240
235
def json_file_parser (self , json_file ):
241
236
# Function to generate SKU file from config_db.json file by extracting port related information from the config_db.json file
237
+ pi_list = []
242
238
with open (json_file ) as f :
243
239
data = json .load (f ,object_pairs_hook = OrderedDict )
244
240
meta_dict = data ['DEVICE_METADATA' ]['localhost' ]
@@ -254,6 +250,9 @@ def json_file_parser(self, json_file):
254
250
f_out = open (new_file , 'w' )
255
251
header_str = "#name lanes alias index speed\n "
256
252
f_out .write (header_str )
253
+
254
+ # data['PORT'] is already an OrderedDict, we can not sort it, so we create
255
+ # pi_list - list of port info items and then sort it
257
256
for key , value in data ['PORT' ].items ():
258
257
pattern = '^Ethernet([0-9]{1,})'
259
258
m = re .match (pattern ,key )
@@ -265,16 +264,40 @@ def json_file_parser(self, json_file):
265
264
if port_idx % self .base_lanes == 0 :
266
265
result = self .check_json_lanes_with_bko (data , port_idx )
267
266
if result != None :
268
- self .write_json_lanes_to_ini_file (data ,port_idx ,result ,f_out )
267
+ self .write_json_lanes_to_pi_list (data , port_idx , result , pi_list )
269
268
else :
270
269
continue
270
+
271
+ pi_list .sort (key = lambda x : (int (re .search (('^Ethernet([0-9]{1,})' ),x [0 ]).group (1 )))) # sort the list with interface name
272
+
273
+ for port_info in pi_list :
274
+ out_str = "{:15s} {:20s} {:11s} {:9s} {:10s}\n " .format (port_info [0 ],port_info [1 ],port_info [2 ],str (port_info [3 ]),str (port_info [4 ]))
275
+ if self .print_mode == True :
276
+ print (out_str )
277
+ else :
278
+ f_out .write (out_str )
279
+ if self .verbose and (self .print_mode == False ):
280
+ print (out_str )
271
281
f_out .close ()
272
282
self .port_config_split_analyze (self .ini_file )
273
283
self .form_port_config_dict_from_ini (self .ini_file )
274
284
self .platform_specific ()
275
285
shutil .copy (new_file ,self .ini_file )
276
286
return
277
287
288
+ def parse_platform_from_config_db_file (self , config_file ):
289
+ with open (config_file ) as f :
290
+ data = json .load (f ,object_pairs_hook = OrderedDict )
291
+ meta_dict = data ['DEVICE_METADATA' ]['localhost' ]
292
+ platform = meta_dict .get ("platform" )
293
+ pattern = '^x86_64-mlnx_msn([0-9]{1,}[a-zA-Z]?)-r0'
294
+ m = re .match (pattern ,platform )
295
+ if m is None :
296
+ print ("Platform Name " , platform , " is not valid, Exiting..." , file = sys .stderr )
297
+ exit (1 )
298
+ self .platform = platform
299
+
300
+
278
301
def port_config_split_analyze (self , ini_file ):
279
302
#Internal function to populate fpp_split tuple with from a port information
280
303
new_file = ini_file + ".new"
@@ -398,16 +421,16 @@ def break_in_ini(self, ini_file, port_name, port_split):
398
421
399
422
#find split partition
400
423
for i in range (0 ,base_lanes ,step ):
401
- port_str = "Ethernet{:d}" .format (line_port_index + i / step )
402
- lanes_str = "{:d}" .format (lane_index + i / step )
424
+ port_str = "Ethernet{:d}" .format (line_port_index + i )
425
+ lanes_str = "{:d}" .format (lane_index + i )
403
426
if step > 1 :
404
427
for j in range (1 ,step ):
405
- lanes_str += ",{:d}" .format (lane_index + j )
428
+ lanes_str += ",{:d}" .format (lane_index + i + j )
406
429
if bko == 0 :
407
430
alias_str = "etp{:d}" .format (alias_index )
408
431
else :
409
- alias_str = "etp{:d}{:s}" .format (alias_index ,alias_arr [i / step ])
410
- index_str = "{:d}" .format (alias_index - 1 )
432
+ alias_str = "etp{:d}{:s}" .format (alias_index ,alias_arr [int ( i / step ) ])
433
+ index_str = "{:d}" .format (alias_index )
411
434
lanes_str_result = lanes_str_result + ":" + lanes_str
412
435
out_str = "{:15s} {:20s} {:11s} {:9s} {:10s}\n " .format (port_str ,lanes_str ,alias_str ,index_str ,str (speed ))
413
436
f_out .write (out_str )
@@ -446,7 +469,6 @@ def break_in_cfg(self, cfg_file, port_name, port_split, lanes_str_result):
446
469
447
470
for port_index in range (port_idx ,port_idx + self .base_lanes ):
448
471
port_str = "Ethernet" + str (port_index )
449
- print ("Port String " ,port_str )
450
472
451
473
if data ['PORT' ].get (port_str ) != None :
452
474
port_instance = data ['PORT' ].get (port_str )
@@ -467,15 +489,14 @@ def break_in_cfg(self, cfg_file, port_name, port_split, lanes_str_result):
467
489
bko = self .bko_dict [port_split ]["bko" ]
468
490
469
491
for i in range (0 ,self .base_lanes ,step ):
470
- port_str = "Ethernet{:d}" .format (port_idx + i / step )
492
+ port_str = "Ethernet{:d}" .format (port_idx + i )
471
493
lanes_str = lanes_arr [j ]
472
494
j += 1
473
495
474
496
if bko == 0 :
475
- alias_str = "etp{:d}" .format ((port_idx / self .base_lanes )+ 1 )
497
+ alias_str = "etp{:d}" .format (int (port_idx / self .base_lanes )+ 1 )
476
498
else :
477
- alias_str = "etp{:d}{:s}" .format ((port_idx / self .base_lanes )+ 1 ,alias_arr [i / step ])
478
- print ("i= " ,i ," alias_str= " ,alias_str )
499
+ alias_str = "etp{:d}{:s}" .format (int (port_idx / self .base_lanes )+ 1 ,alias_arr [int (i / step )])
479
500
port_inst ["lanes" ] = lanes_str
480
501
port_inst ["alias" ] = alias_str
481
502
port_inst ["speed" ] = speed * 1000
@@ -487,6 +508,7 @@ def break_in_cfg(self, cfg_file, port_name, port_split, lanes_str_result):
487
508
488
509
with open (new_file , 'w' ) as outfile :
489
510
json .dump (data , outfile , indent = 4 , sort_keys = True )
511
+ shutil .copy (new_file ,cfg_file )
490
512
491
513
print ("--------------------------------------------------------" )
492
514
@@ -555,16 +577,16 @@ def set_lanes(self):
555
577
m = re .match (pattern ,self .default_lanes_per_port [fp - 1 ])
556
578
if (splt == 1 ):
557
579
self .portconfig_dict [idx_arr [0 ]][lanes_index ] = m .group (1 )+ "," + m .group (2 )+ "," + m .group (3 )+ "," + m .group (4 )
558
- self .portconfig_dict [idx_arr [0 ]][index_index ] = str (fp - 1 )
580
+ self .portconfig_dict [idx_arr [0 ]][index_index ] = str (fp )
559
581
self .portconfig_dict [idx_arr [0 ]][name_index ] = "Ethernet" + str ((fp - 1 )* 4 )
560
582
if (self .verbose ):
561
583
print ("set_lanes -> FP: " ,fp , "Split: " ,splt )
562
584
print ("PortConfig_dict " ,idx_arr [0 ],":" , self .portconfig_dict [idx_arr [0 ]])
563
585
elif (splt == 2 ):
564
586
self .portconfig_dict [idx_arr [0 ]][lanes_index ] = m .group (1 )+ "," + m .group (2 )
565
587
self .portconfig_dict [idx_arr [1 ]][lanes_index ] = m .group (3 )+ "," + m .group (4 )
566
- self .portconfig_dict [idx_arr [0 ]][index_index ] = str (fp - 1 )
567
- self .portconfig_dict [idx_arr [1 ]][index_index ] = str (fp - 1 )
588
+ self .portconfig_dict [idx_arr [0 ]][index_index ] = str (fp )
589
+ self .portconfig_dict [idx_arr [1 ]][index_index ] = str (fp )
568
590
self .portconfig_dict [idx_arr [0 ]][name_index ] = "Ethernet" + str ((fp - 1 )* 4 )
569
591
self .portconfig_dict [idx_arr [1 ]][name_index ] = "Ethernet" + str ((fp - 1 )* 4 + 2 )
570
592
if (self .verbose ):
@@ -576,10 +598,10 @@ def set_lanes(self):
576
598
self .portconfig_dict [idx_arr [1 ]][lanes_index ] = m .group (2 )
577
599
self .portconfig_dict [idx_arr [2 ]][lanes_index ] = m .group (3 )
578
600
self .portconfig_dict [idx_arr [3 ]][lanes_index ] = m .group (4 )
579
- self .portconfig_dict [idx_arr [0 ]][index_index ] = str (fp - 1 )
580
- self .portconfig_dict [idx_arr [1 ]][index_index ] = str (fp - 1 )
581
- self .portconfig_dict [idx_arr [2 ]][index_index ] = str (fp - 1 )
582
- self .portconfig_dict [idx_arr [3 ]][index_index ] = str (fp - 1 )
601
+ self .portconfig_dict [idx_arr [0 ]][index_index ] = str (fp )
602
+ self .portconfig_dict [idx_arr [1 ]][index_index ] = str (fp )
603
+ self .portconfig_dict [idx_arr [2 ]][index_index ] = str (fp )
604
+ self .portconfig_dict [idx_arr [3 ]][index_index ] = str (fp )
583
605
self .portconfig_dict [idx_arr [0 ]][name_index ] = "Ethernet" + str ((fp - 1 )* 4 )
584
606
self .portconfig_dict [idx_arr [1 ]][name_index ] = "Ethernet" + str ((fp - 1 )* 4 + 1 )
585
607
self .portconfig_dict [idx_arr [2 ]][name_index ] = "Ethernet" + str ((fp - 1 )* 4 + 2 )
@@ -700,6 +722,7 @@ def main(argv):
700
722
parser .add_argument ('-p' , '--print' , action = 'store_true' , help = 'Print port_config.ini without creating a new SKU' , default = False )
701
723
parser .add_argument ('--verbose' , action = 'store_true' , help = 'Verbose output' , default = False )
702
724
parser .add_argument ('-d' , '--default_sku_path' , action = 'store' ,nargs = 1 , help = 'Specify Default SKU path' , default = None )
725
+ parser .add_argument ('-q' , '--port_split_path' , action = 'store' ,nargs = 1 , help = 'Specify Port split path' , default = None )
703
726
parser .add_argument ('-v' , '--version' , action = 'version' , version = '%(prog)s 1.0' )
704
727
705
728
args = parser .parse_args ()
@@ -736,6 +759,8 @@ def main(argv):
736
759
elif args .minigraph_file :
737
760
sku .minigraph_parser (args .minigraph_file )
738
761
elif args .json_file :
762
+ if sku .platform is None :
763
+ sku .parse_platform_from_config_db_file (args .json_file [0 ])
739
764
if sku .platform in platform_4 :
740
765
sku .base_lanes = 4
741
766
sku .bko_dict = bko_dict_4
@@ -751,6 +776,25 @@ def main(argv):
751
776
sku .json_file_parser (args .json_file [0 ])
752
777
return
753
778
elif args .port_split :
779
+ if args .port_split_path :
780
+ sku .ini_file = args .port_split_path [0 ] + "/port_config.ini"
781
+ sku .cfg_file = args .port_split_path [0 ] + "/config_db.json"
782
+ sku .parse_platform_from_config_db_file (sku .cfg_file )
783
+ else :
784
+ try :
785
+ sku_name = subprocess .check_output ("show platform summary | grep HwSKU " ,shell = True ).rstrip ().split ()[1 ]
786
+ except KeyError :
787
+ print ("Couldn't find HwSku info in Platform summary" , file = sys .stderr )
788
+ exit (1 )
789
+ sku .ini_file = sku .default_sku_path + "/" + sku_name + "/port_config.ini"
790
+ sku .cfg_file = "/etc/sonic/config_db.json"
791
+
792
+ if sku .platform in platform_4 :
793
+ sku .base_lanes = 4
794
+ sku .bko_dict = bko_dict_4
795
+ else :
796
+ sku .base_lanes = 8
797
+ sku .bko_dict = bko_dict_8
754
798
sku .break_a_port (args .port_split [0 ], args .port_split [1 ])
755
799
return
756
800
0 commit comments