Skip to content

Commit

Permalink
Moved utility functions for multi-npu platforms from sonic-utilities …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
abdosi authored May 13, 2020
1 parent 40bc487 commit 0542afb
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions src/sonic-config-engine/sonic_device_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import re
from natsort import natsorted
import glob
from swsssdk import ConfigDBConnector, SonicDBConfig

DOCUMENTATION = '''
---
module: sonic_device_util
Expand All @@ -21,6 +23,9 @@
SONIC_DEVICE_PATH = '/usr/share/sonic/device'
NPU_NAME_PREFIX = 'asic'
NAMESPACE_PATH_GLOB = '/run/netns/*'
ASIC_CONF_FILENAME = 'asic.conf'
FRONTEND_ASIC_SUB_ROLE = 'FrontEnd'
BACKEND_ASIC_SUB_ROLE = 'BackEnd'
def get_machine_info():
if not os.path.isfile('/host/machine.conf'):
return None
Expand All @@ -41,7 +46,9 @@ def get_npu_id_from_name(npu_name):

def get_num_npus():
platform = get_platform_info(get_machine_info())
asic_conf_file_path = os.path.join(SONIC_DEVICE_PATH, platform, 'asic.conf')
if not platform:
return 1
asic_conf_file_path = os.path.join(SONIC_DEVICE_PATH, platform, ASIC_CONF_FILENAME)
if not os.path.isfile(asic_conf_file_path):
return 1
with open(asic_conf_file_path) as asic_conf_file:
Expand All @@ -51,7 +58,7 @@ def get_num_npus():
continue
if tokens[0].lower() == 'num_asic':
num_npus = tokens[1].strip()
return num_npus
return int(num_npus)

def get_namespaces():
"""
Expand All @@ -64,6 +71,52 @@ def get_namespaces():
ns_list.append(ns)
return natsorted(ns_list)

def get_hwsku():
config_db = ConfigDBConnector()
config_db.connect()
metadata = config_db.get_table('DEVICE_METADATA')
return metadata['localhost']['hwsku']

def get_platform():
if not os.path.isfile('/host/machine.conf'):
return ''

with open('/host/machine.conf') as machine_conf:
for line in machine_conf:
tokens = line.split('=')
if tokens[0].strip() == 'onie_platform' or tokens[0].strip() == 'aboot_platform':
return tokens[1].strip()
return ''

def is_multi_npu():
num_npus = get_num_npus()
return (num_npus > 1)

def get_all_namespaces():
"""
In case of Multi-Asic platform, Each ASIC will have a linux network namespace created.
So we loop through the databases in different namespaces and depending on the sub_role
decide whether this is a front end ASIC/namespace or a back end one.
"""
front_ns = []
back_ns = []
num_npus = get_num_npus()
SonicDBConfig.load_sonic_global_db_config()

if is_multi_npu():
for npu in range(num_npus):
namespace = "{}{}".format(NPU_NAME_PREFIX, npu)
config_db = ConfigDBConnector(use_unix_socket_path=True, namespace=namespace)
config_db.connect()

metadata = config_db.get_table('DEVICE_METADATA')
if metadata['localhost']['sub_role'] == FRONTEND_ASIC_SUB_ROLE:
front_ns.append(namespace)
elif metadata['localhost']['sub_role'] == BACKEND_ASIC_SUB_ROLE:
back_ns.append(namespace)

return {'front_ns':front_ns, 'back_ns':back_ns}

def get_platform_info(machine_info):
if machine_info != None:
if machine_info.has_key('onie_platform'):
Expand Down

0 comments on commit 0542afb

Please sign in to comment.