Skip to content

[multi-asic][cli][chassis-db] Avoid connecting to chassis db when cli commands are executed from linecards #8065

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

Merged
merged 2 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
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
40 changes: 40 additions & 0 deletions src/sonic-py-common/sonic_py_common/device_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
NPU_NAME_PREFIX = "asic"
NAMESPACE_PATH_GLOB = "/run/netns/*"
ASIC_CONF_FILENAME = "asic.conf"
PLATFORM_ENV_CONF_FILENAME = "platform_env.conf"
FRONTEND_ASIC_SUB_ROLE = "FrontEnd"
BACKEND_ASIC_SUB_ROLE = "BackEnd"

Expand Down Expand Up @@ -164,6 +165,29 @@ def get_asic_conf_file_path():
return None


def get_platform_env_conf_file_path():
"""
Retrieves the path to the PLATFORM ENV conguration file on the device

Returns:
A string containing the path to the PLATFORM ENV conguration file on success,
None on failure
"""
platform_env_conf_path_candidates = []

platform_env_conf_path_candidates.append(os.path.join(CONTAINER_PLATFORM_PATH, PLATFORM_ENV_CONF_FILENAME))

platform = get_platform()
if platform:
platform_env_conf_path_candidates.append(os.path.join(HOST_DEVICE_PATH, platform, PLATFORM_ENV_CONF_FILENAME))

for platform_env_conf_file_path in platform_env_conf_path_candidates:
if os.path.isfile(platform_env_conf_file_path):
return platform_env_conf_file_path

return None


def get_path_to_platform_dir():
"""
Retreives the paths to the device's platform directory
Expand Down Expand Up @@ -374,6 +398,22 @@ def is_multi_npu():
return (num_npus > 1)


def is_supervisor():
platform_env_conf_file_path = get_platform_env_conf_file_path()
if platform_env_conf_file_path is None:
return False
with open(platform_env_conf_file_path) as platform_env_conf_file:
for line in platform_env_conf_file:
tokens = line.split('=')
if len(tokens) < 2:
continue
if tokens[0].lower() == 'supervisor':
val = tokens[1].strip()
if val == '1':
return True
return False


def get_npu_id_from_name(npu_name):
if npu_name.startswith(NPU_NAME_PREFIX):
return npu_name[len(NPU_NAME_PREFIX):]
Expand Down
17 changes: 15 additions & 2 deletions src/sonic-py-common/sonic_py_common/multi_asic.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .device_info import CONTAINER_PLATFORM_PATH
from .device_info import HOST_DEVICE_PATH
from .device_info import get_platform
from .device_info import is_supervisor

ASIC_NAME_PREFIX = 'asic'
NAMESPACE_PATH_GLOB = '/run/netns/*'
Expand Down Expand Up @@ -45,7 +46,11 @@ def connect_config_db_for_ns(namespace=DEFAULT_NAMESPACE):
def connect_to_all_dbs_for_ns(namespace=DEFAULT_NAMESPACE):
"""
The function connects to the DBs for a given namespace and
returns the handle
returns the handle

For voq chassis systems, the db list includes databases from
supervisor card. Avoid connecting to these databases from linecards

If no namespace is provided, it will connect to the db in the
default namespace.
In case of multi ASIC, the default namespace is the
Expand All @@ -56,7 +61,15 @@ def connect_to_all_dbs_for_ns(namespace=DEFAULT_NAMESPACE):
handle to all the dbs for a namespaces
"""
db = swsscommon.SonicV2Connector(namespace=namespace)
for db_id in db.get_db_list():
db_list = list(db.get_db_list())
if not is_supervisor():
try:
db_list.remove('CHASSIS_APP_DB')
db_list.remove('CHASSIS_STATE_DB')
Copy link
Contributor

Choose a reason for hiding this comment

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

Good to have an function returning back a list of chassis db instances in supervisor.
But it is ok to do it later as well.

except Exception:
pass

for db_id in db_list:
db.connect(db_id)
return db

Expand Down