Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved utility functions for multi-npu platforms from sonic-utilities to sonic_device_util.py #4559

Merged
merged 5 commits into from
May 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

@jleveque jleveque May 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice we use two nomencaltures interchangably: "ASIC" and "NPU". I feel like we should choose one and stick with it throughout all files. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jleveque yes, agree we can mark this as To-Do item as this will require change multiple files. We can update this later.

@judyjoseph @arlakshm @SuvarnaMeenakshi

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