Skip to content

Commit 0542afb

Browse files
authored
Moved utility functions for multi-npu platforms from sonic-utilities to sonic_device_util.py (#4559)
* Moved utility functions for multi-npu platforms from sonic-utilities config/main.py to here so that they can be used any module * Fix the issue with test run during compilation with acl-uploader PR#908 of sonic-utilities. * Fix get_num_npu as it was retuning string and not int * Address Review Comments * Address Review Comments
1 parent 40bc487 commit 0542afb

1 file changed

Lines changed: 55 additions & 2 deletions

File tree

src/sonic-config-engine/sonic_device_util.py

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import re
66
from natsort import natsorted
77
import glob
8+
from swsssdk import ConfigDBConnector, SonicDBConfig
9+
810
DOCUMENTATION = '''
911
---
1012
module: sonic_device_util
@@ -21,6 +23,9 @@
2123
SONIC_DEVICE_PATH = '/usr/share/sonic/device'
2224
NPU_NAME_PREFIX = 'asic'
2325
NAMESPACE_PATH_GLOB = '/run/netns/*'
26+
ASIC_CONF_FILENAME = 'asic.conf'
27+
FRONTEND_ASIC_SUB_ROLE = 'FrontEnd'
28+
BACKEND_ASIC_SUB_ROLE = 'BackEnd'
2429
def get_machine_info():
2530
if not os.path.isfile('/host/machine.conf'):
2631
return None
@@ -41,7 +46,9 @@ def get_npu_id_from_name(npu_name):
4146

4247
def get_num_npus():
4348
platform = get_platform_info(get_machine_info())
44-
asic_conf_file_path = os.path.join(SONIC_DEVICE_PATH, platform, 'asic.conf')
49+
if not platform:
50+
return 1
51+
asic_conf_file_path = os.path.join(SONIC_DEVICE_PATH, platform, ASIC_CONF_FILENAME)
4552
if not os.path.isfile(asic_conf_file_path):
4653
return 1
4754
with open(asic_conf_file_path) as asic_conf_file:
@@ -51,7 +58,7 @@ def get_num_npus():
5158
continue
5259
if tokens[0].lower() == 'num_asic':
5360
num_npus = tokens[1].strip()
54-
return num_npus
61+
return int(num_npus)
5562

5663
def get_namespaces():
5764
"""
@@ -64,6 +71,52 @@ def get_namespaces():
6471
ns_list.append(ns)
6572
return natsorted(ns_list)
6673

74+
def get_hwsku():
75+
config_db = ConfigDBConnector()
76+
config_db.connect()
77+
metadata = config_db.get_table('DEVICE_METADATA')
78+
return metadata['localhost']['hwsku']
79+
80+
def get_platform():
81+
if not os.path.isfile('/host/machine.conf'):
82+
return ''
83+
84+
with open('/host/machine.conf') as machine_conf:
85+
for line in machine_conf:
86+
tokens = line.split('=')
87+
if tokens[0].strip() == 'onie_platform' or tokens[0].strip() == 'aboot_platform':
88+
return tokens[1].strip()
89+
return ''
90+
91+
def is_multi_npu():
92+
num_npus = get_num_npus()
93+
return (num_npus > 1)
94+
95+
def get_all_namespaces():
96+
"""
97+
In case of Multi-Asic platform, Each ASIC will have a linux network namespace created.
98+
So we loop through the databases in different namespaces and depending on the sub_role
99+
decide whether this is a front end ASIC/namespace or a back end one.
100+
"""
101+
front_ns = []
102+
back_ns = []
103+
num_npus = get_num_npus()
104+
SonicDBConfig.load_sonic_global_db_config()
105+
106+
if is_multi_npu():
107+
for npu in range(num_npus):
108+
namespace = "{}{}".format(NPU_NAME_PREFIX, npu)
109+
config_db = ConfigDBConnector(use_unix_socket_path=True, namespace=namespace)
110+
config_db.connect()
111+
112+
metadata = config_db.get_table('DEVICE_METADATA')
113+
if metadata['localhost']['sub_role'] == FRONTEND_ASIC_SUB_ROLE:
114+
front_ns.append(namespace)
115+
elif metadata['localhost']['sub_role'] == BACKEND_ASIC_SUB_ROLE:
116+
back_ns.append(namespace)
117+
118+
return {'front_ns':front_ns, 'back_ns':back_ns}
119+
67120
def get_platform_info(machine_info):
68121
if machine_info != None:
69122
if machine_info.has_key('onie_platform'):

0 commit comments

Comments
 (0)