Skip to content

Conversation

@stephenxs
Copy link
Collaborator

@stephenxs stephenxs commented Oct 11, 2025

What I did

Fetch capability of ingress/egress mirror before configuring it and avoid configuring ingress/egress mirror on a platform that does not support it.

How I did it

Check the capability in PORT_INGRESS_MIRROR_CAPABLE and PORT_EGRESS_MIRROR_CAPABLE in STATE_DB table SWITCH_CAPABILITY.
The capability of ingress/egress mirror is inserted to STATE_DB by orchagent during initialization.

How to verify it

Manual test and unit test

Previous command output (if the output of a command-line utility has changed)

New command output (if the output of a command-line utility has changed)

@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Signed-off-by: Stephen Sun <[email protected]>
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@stephenxs
Copy link
Collaborator Author

Failure is caused by rebasing the commit to the latest master. Investigating.

@stephenxs
Copy link
Collaborator Author

Failure is caused by rebasing the commit to the latest master. Investigating.

Depends on sonic-net/sonic-swss#3934

Signed-off-by: Stephen Sun <[email protected]>
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@stephenxs stephenxs changed the title Fetch capability of mirror before configuring it. Fetch capability of mirror before configuring it Oct 20, 2025
@stephenxs stephenxs marked this pull request as ready for review October 23, 2025 06:03
@stephenxs stephenxs requested a review from bingwang-ms October 24, 2025 06:07
bingwang-ms
bingwang-ms previously approved these changes Nov 4, 2025
@bingwang-ms
Copy link
Contributor

Depends on sonic-net/sonic-swss#3934

@bingwang-ms
Copy link
Contributor

@qiluo-msft Can you help review this change? Thanks

config/main.py Outdated
def is_port_mirror_capability_supported(direction, namespace=None):
""" Check if port mirror capability is supported for the given direction """
try:
state_db = SonicV2Connector(host='127.0.0.1')
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you use unix socket which has better performance?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed

config/main.py Outdated
return False

return True
except Exception:
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you use more specific exception type?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed as we do not expect any exception here.

@qiluo-msft qiluo-msft requested a review from Copilot November 4, 2025 01:08
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds ASIC capability checking for port mirror direction support before allowing mirror session configuration. The change prevents users from configuring mirror sessions with directions (rx/tx/both) that are not supported by the underlying ASIC hardware.

  • Added is_port_mirror_capability_supported() function to query STATE_DB for ASIC capabilities
  • Integrated capability validation into validate_mirror_session_config()
  • Added comprehensive test coverage for capability checking functionality

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
config/main.py Added capability checking function and validation logic to prevent unsupported mirror directions
tests/config_mirror_session_test.py Added test cases for capability checking and removed trailing whitespace

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

config/main.py Outdated
def is_port_mirror_capability_supported(direction, namespace=None):
""" Check if port mirror capability is supported for the given direction """
try:
state_db = SonicV2Connector(host='127.0.0.1')
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

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

The function accepts a namespace parameter but never uses it. Based on other functions in the codebase (e.g., lines 6188-6190), the namespace should be used when initializing SonicV2Connector. Either use the namespace parameter to create the connector with use_unix_socket_path=True and the appropriate namespace, or remove the unused parameter.

Suggested change
state_db = SonicV2Connector(host='127.0.0.1')
if namespace is not None:
state_db = SonicV2Connector(use_unix_socket_path=True, namespace=namespace)
else:
state_db = SonicV2Connector(host='127.0.0.1')

Copilot uses AI. Check for mistakes.
config/main.py Outdated
Comment on lines 1128 to 1145

# If no direction is specified, check both ingress and egress capabilities
if not direction:
ingress_supported = state_db.get(state_db.STATE_DB, entry_name, "PORT_INGRESS_MIRROR_CAPABLE")
egress_supported = state_db.get(state_db.STATE_DB, entry_name, "PORT_EGRESS_MIRROR_CAPABLE")
return ingress_supported == "true" and egress_supported == "true"

if direction in ['rx', 'both']:
ingress_supported = state_db.get(state_db.STATE_DB, entry_name, "PORT_INGRESS_MIRROR_CAPABLE")
if ingress_supported != "true":
return False

if direction in ['tx', 'both']:
egress_supported = state_db.get(state_db.STATE_DB, entry_name, "PORT_EGRESS_MIRROR_CAPABLE")
if egress_supported != "true":
return False

return True
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

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

The state_db connection is never explicitly closed. Based on patterns elsewhere in the codebase (e.g., line 6195), the connection should be closed after use with state_db.close(state_db.STATE_DB) to prevent resource leaks.

Suggested change
# If no direction is specified, check both ingress and egress capabilities
if not direction:
ingress_supported = state_db.get(state_db.STATE_DB, entry_name, "PORT_INGRESS_MIRROR_CAPABLE")
egress_supported = state_db.get(state_db.STATE_DB, entry_name, "PORT_EGRESS_MIRROR_CAPABLE")
return ingress_supported == "true" and egress_supported == "true"
if direction in ['rx', 'both']:
ingress_supported = state_db.get(state_db.STATE_DB, entry_name, "PORT_INGRESS_MIRROR_CAPABLE")
if ingress_supported != "true":
return False
if direction in ['tx', 'both']:
egress_supported = state_db.get(state_db.STATE_DB, entry_name, "PORT_EGRESS_MIRROR_CAPABLE")
if egress_supported != "true":
return False
return True
try:
# If no direction is specified, check both ingress and egress capabilities
if not direction:
ingress_supported = state_db.get(state_db.STATE_DB, entry_name, "PORT_INGRESS_MIRROR_CAPABLE")
egress_supported = state_db.get(state_db.STATE_DB, entry_name, "PORT_EGRESS_MIRROR_CAPABLE")
return ingress_supported == "true" and egress_supported == "true"
if direction in ['rx', 'both']:
ingress_supported = state_db.get(state_db.STATE_DB, entry_name, "PORT_INGRESS_MIRROR_CAPABLE")
if ingress_supported != "true":
return False
if direction in ['tx', 'both']:
egress_supported = state_db.get(state_db.STATE_DB, entry_name, "PORT_EGRESS_MIRROR_CAPABLE")
if egress_supported != "true":
return False
return True
finally:
state_db.close(state_db.STATE_DB)

Copilot uses AI. Check for mistakes.
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@stephenxs stephenxs requested a review from qiluo-msft November 4, 2025 07:20
@stephenxs
Copy link
Collaborator Author

It's weird that pretest passed locally but failed in the github

stephens@675ff3c81f2e:/sonic/src/sonic-utilities/config$ git commit -s
[INFO] Initializing environment for https://github.com/PyCQA/flake8.
[INFO] Installing environment for https://github.com/PyCQA/flake8.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...
flake8...................................................................Passed
[detached HEAD 8bf2b0d7] Fix community review comments
 1 file changed, 19 insertions(+), 23 deletions(-)
stephens@675ff3c81f2e:/sonic/src/sonic-utilities/config$ git log

Signed-off-by: Stephen Sun <[email protected]>
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Signed-off-by: Stephen Sun <[email protected]>
@mssonicbld
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@liat-grozovik
Copy link
Collaborator

@qiluo-msft can you please check if you are ok with comments handling and approve this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants