From a6877ad1b5e737703c2b2dea49e3c2a20ca5f7ae Mon Sep 17 00:00:00 2001 From: judyjoseph <53951155+judyjoseph@users.noreply.github.com> Date: Wed, 17 Jun 2020 18:23:08 -0700 Subject: [PATCH] Using (pci) device id to identify the ASIC on sai_switch_create (#4705) * Update to sonic_cfggen and utilities to populate the (pci) device id in the "asic_id" field in the DEVICE_METADATA. This Id is parsed from the file "asic.conf" file in the device/ dir. The format of entries are eg: for a 2 ASIC platform. DEV_ID_ASIC_0=03:00.0 DEV_ID_ASIC_1=04:00.0 Going forward will use this device id as the asic instance ID passed to syncd/sai while doing create_switch. Current support is limited, supports only one TD2 platform. --- src/sonic-config-engine/sonic-cfggen | 8 +++++- src/sonic-config-engine/sonic_device_util.py | 26 ++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/sonic-config-engine/sonic-cfggen b/src/sonic-config-engine/sonic-cfggen index 2e0ad43d58..24375940ad 100755 --- a/src/sonic-config-engine/sonic-cfggen +++ b/src/sonic-config-engine/sonic-cfggen @@ -43,6 +43,7 @@ from sonic_device_util import get_machine_info from sonic_device_util import get_platform_info from sonic_device_util import get_system_mac from sonic_device_util import get_npu_id_from_name +from sonic_device_util import get_npu_device_id from config_samples import generate_sample_config from config_samples import get_available_config from swsssdk import SonicV2Connector, ConfigDBConnector, SonicDBConfig @@ -304,7 +305,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_npu_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) if args.template is not None: diff --git a/src/sonic-config-engine/sonic_device_util.py b/src/sonic-config-engine/sonic_device_util.py index cf1edfd59b..1bdf33162d 100644 --- a/src/sonic-config-engine/sonic_device_util.py +++ b/src/sonic-config-engine/sonic_device_util.py @@ -44,6 +44,32 @@ def get_npu_id_from_name(npu_name): else: return None +def get_npu_device_id(npu_id): + platform = get_platform_info(get_machine_info()) + if not platform: + return None + + asic_conf_file_path = os.path.join(SONIC_DEVICE_PATH, platform, ASIC_CONF_FILENAME) + if not os.path.isfile(asic_conf_file_path): + return None + + # In a multi-npu 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(npu_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_num_npus(): platform = get_platform_info(get_machine_info()) if not platform: