diff --git a/src/sonic-config-engine/sonic-cfggen b/src/sonic-config-engine/sonic-cfggen index 98ef97f8a8..33133f2de1 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 @@ -377,7 +377,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 3bb2984c8b..1666fb47df 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): """