From e56c3e766fae7479d2f4360837e0380416e5248d Mon Sep 17 00:00:00 2001 From: judyjoseph <53951155+judyjoseph@users.noreply.github.com> Date: Fri, 7 May 2021 09:47:26 -0700 Subject: [PATCH] Port the fix to get the PCI_id from asic.conf file from 201911 --> master branch (#7513) Port the fix to get the pci_device ID from asic.conf file and update the "asic_id" field in DEVICE_METADATA with the pci_device_Id. Ref: https://github.com/Azure/sonic-buildimage/pull/4705 --- src/sonic-config-engine/sonic-cfggen | 9 ++++++-- .../sonic_py_common/multi_asic.py | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/sonic-config-engine/sonic-cfggen b/src/sonic-config-engine/sonic-cfggen index d895c0dde12c..c0d16ea9691e 100755 --- a/src/sonic-config-engine/sonic-cfggen +++ b/src/sonic-config-engine/sonic-cfggen @@ -41,7 +41,7 @@ from functools import partial from minigraph import minigraph_encoder, parse_xml, parse_device_desc_xml, parse_asic_sub_role from portconfig import get_port_config, get_breakout_mode from redis_bcc import RedisBytecodeCache -from sonic_py_common.multi_asic import get_asic_id_from_name +from sonic_py_common.multi_asic import get_asic_id_from_name, get_asic_device_id from sonic_py_common import device_info from swsscommon.swsscommon import SonicV2Connector, ConfigDBConnector, SonicDBConfig, ConfigDBPipeConnector @@ -380,7 +380,12 @@ def main(): }}} # The ID needs to be passed to the SAI to identify the asic. if asic_name is not None: - hardware_data['DEVICE_METADATA']['localhost'].update(asic_id=asic_id) + device_id = get_asic_device_id(asic_id) + # if the device_id obtained is None, exit with error + if device_id is None: + print('Failed to get device ID for', asic_name, file=sys.stderr) + sys.exit(1) + hardware_data['DEVICE_METADATA']['localhost'].update(asic_id=device_id) deep_update(data, hardware_data) paths = ['/', '/usr/share/sonic/templates'] diff --git a/src/sonic-py-common/sonic_py_common/multi_asic.py b/src/sonic-py-common/sonic_py_common/multi_asic.py index 3bb2984c8b95..1666fb47df1c 100644 --- a/src/sonic-py-common/sonic_py_common/multi_asic.py +++ b/src/sonic-py-common/sonic_py_common/multi_asic.py @@ -137,6 +137,29 @@ def get_asic_id_from_name(asic_name): else: raise ValueError('Unknown asic namespace name {}'.format(asic_name)) +def get_asic_device_id(asic_id): + # Get asic.conf file + asic_conf_file_path = get_asic_conf_file_path() + + if asic_conf_file_path is None: + return None + + # In a multi-asic device we need to have the file "asic.conf" updated with the asic instance + # and the corresponding device id which could be pci_id. Below is an eg: for a 2 ASIC platform/sku. + # DEV_ID_ASIC_0=03:00.0 + # DEV_ID_ASIC_1=04:00.0 + device_str = "DEV_ID_ASIC_{}".format(asic_id) + + with open(asic_conf_file_path) as asic_conf_file: + for line in asic_conf_file: + tokens = line.split('=') + if len(tokens) < 2: + continue + if tokens[0] == device_str: + device_id = tokens[1].strip() + return device_id + + return None def get_current_namespace(pid=None): """