Skip to content

[switchorch][mirrororch]: Add sampled port mirroring with truncation support#4502

Merged
prsunny merged 41 commits into
sonic-net:masterfrom
Janetxxx:dev/jc/sampled-port-mirror-capability
Jun 18, 2026
Merged

[switchorch][mirrororch]: Add sampled port mirroring with truncation support#4502
prsunny merged 41 commits into
sonic-net:masterfrom
Janetxxx:dev/jc/sampled-port-mirror-capability

Conversation

@Janetxxx

@Janetxxx Janetxxx commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

What I did
Adds sampled port mirroring with packet truncation support. This PR targets and validates the ERSPAN use case, which is the primary motivation for the feature in the HLD (sonic-net/SONiC#2296).

SwitchOrch — capability discovery

During init, queries SAI via sai_query_attribute_capability() and publishes to STATE_DB under SWITCH_CAPABILITY|switch:

  • PORT_INGRESS_SAMPLE_MIRROR_CAPABLE (SAI_PORT_ATTR_INGRESS_SAMPLE_MIRROR_SESSION)
  • PORT_EGRESS_SAMPLE_MIRROR_CAPABLE (SAI_PORT_ATTR_EGRESS_SAMPLE_MIRROR_SESSION)
  • SAMPLEPACKET_TRUNCATION_CAPABLE (SAI_SAMPLEPACKET_ATTR_TRUNCATE_ENABLE)
MirrorOrch — SamplePacket lifecycle
  • Creates/binds a single SAMPLEPACKET object when sample_rate is set. Direction-aware binding: RX uses INGRESS_SAMPLEPACKET_ENABLE + INGRESS_SAMPLE_MIRROR_SESSION, TX uses the EGRESS_* equivalents, and BOTH binds ingress and egress with one SAMPLEPACKET.
  • Truncation implies sampling: truncate_size without an explicit sample_rate defaults the rate to 1 (sample every packet).
  • Capability gating: rejects the session when the required per-direction sampled mirroring (ingress/egress) is not supported; skips truncation if unsupported.
  • Session update is delete-and-recreate only; in-place SET on an existing key is treated as a duplicate and ignored.
  • Cross-feature guard with sFlow: get-before-set on INGRESS_SAMPLEPACKET_ENABLE prevents silent overwrite.
SflowOrch

sflowAddPort() / sflowUpdateSampleDirection()check INGRESS/EGRESS_SAMPLEPACKET_ENABLE before binding and reject if the port's samplepacket slot is already owned by a non-sFlow feature

Why I did it
This is part of the sampled port mirroring with truncation feature. It enables per-port hardware-based sampling on ERSPAN mirror sessions, reducing mirror bandwidth by only mirroring 1 out of every N packets with optional packet truncation.

How I verified it

Unit (mock) tests

switchorch_ut.cpp, mirrororch_ut.cpp:

  • Capability fields written to STATE_DB
  • Sampled vs full mirror path selection
  • SAMPLEPACKET create/remove, MirrorEntry defaults
  • Per-direction capability gating; rejection when ingress/egress sampled mirroring or truncation is unsupported (booleans toggled directly, which a VS test cannot do)
  • Input validation, non-existent session handling, fault-injection on SAMPLEPACKET create/remove
VS integration tests
  • SAMPLEPACKET lifecycle + port attribute binding/cleanup, truncation attrs
  • RX, TX (egress) and BOTH direction binding
  • truncate_size without sample_rate defaults to sample_rate=1
  • Delete-and-recreate updates a session; duplicate SET on an existing key is ignored (no SAI/STATE_DB mutation)
  • Route lifecycle: session stays unprogrammed while inactive, and is torn down / reprogrammed across route withdrawal and restore
  • Backward compat: no SAMPLEPACKET created when sample_rate absent
  • STATE_DB field verification, switch capability values, multi-port binding, LAG members, sFlow conflict rejection

Details if related

Copilot AI review requested due to automatic review settings April 21, 2026 12:25
@Janetxxx
Janetxxx requested a review from prsunny as a code owner April 21, 2026 12:25
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds SwitchOrch startup capability discovery for sampled port mirroring and samplepacket truncation, publishing results into SWITCH_CAPABILITY|switch for consumption by other components (e.g., MirrorOrch/CLI) to enable runtime feature gating.

Changes:

  • Introduces new capability keys for ingress/egress sampled mirror and samplepacket truncation in switchorch.h.
  • Adds SwitchOrch::querySwitchSampledMirrorCapability() to query SAI attribute capabilities and store results in STATE_DB.
  • Exposes new SwitchOrch getters for sampled mirror and truncation support flags.

Reviewed changes

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

File Description
orchagent/switchorch.h Adds new capability key macros, new public support getters, and a private query method declaration.
orchagent/switchorch.cpp Calls the new query at startup and implements SAI capability discovery + STATE_DB publishing.

Comment thread orchagent/switchorch.h Outdated
Comment thread orchagent/switchorch.h Outdated
Comment thread orchagent/switchorch.cpp
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@Janetxxx
Janetxxx force-pushed the dev/jc/sampled-port-mirror-capability branch from 5d5de0a to 3f09e7a Compare April 24, 2026 10:45
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@Janetxxx
Janetxxx force-pushed the dev/jc/sampled-port-mirror-capability branch from 3f09e7a to 239ac81 Compare April 24, 2026 10:54
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@Janetxxx
Janetxxx force-pushed the dev/jc/sampled-port-mirror-capability branch from e839e2e to ae43ab0 Compare April 24, 2026 11:01
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@Janetxxx
Janetxxx force-pushed the dev/jc/sampled-port-mirror-capability branch from ae43ab0 to 06b869a Compare April 28, 2026 08:34
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@Janetxxx
Janetxxx requested a review from r12f April 28, 2026 08:35
@Janetxxx Janetxxx changed the title [switchorch]: Add sampled mirror and truncation capability discovery [switchorch]: Add sampled port mirror and truncation capability discovery Apr 28, 2026
@Janetxxx
Janetxxx force-pushed the dev/jc/sampled-port-mirror-capability branch from 06b869a to 427887c Compare April 29, 2026 08:07
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@Janetxxx Janetxxx changed the title [switchorch]: Add sampled port mirror and truncation capability discovery [switchorch][mirrororch]: Add sampled port mirroring with truncation support Apr 29, 2026
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

…ed ERSPAN

 route/session/sFlow/samplepacket state before and after every test

Signed-off-by: Janet Cui <janet970527@gmail.com>
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Pterosaur
Pterosaur previously approved these changes Jun 9, 2026
Comment thread orchagent/mirrororch.cpp
Comment thread orchagent/mirrororch.cpp
Comment thread orchagent/mirrororch.cpp
Comment thread orchagent/switchorch.cpp
Comment thread orchagent/mirrororch.cpp
Comment thread orchagent/mirrororch.cpp
Comment thread orchagent/mirrororch.cpp
Comment thread orchagent/sfloworch.cpp
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

…p rollback

Signed-off-by: Janet Cui <janet970527@gmail.com>
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@bingwang-ms

Copy link
Copy Markdown
Contributor

Discussed and addressed comments offline. Approving the PR

@prsunny

prsunny commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

@Janetxxx , this is pretty big for 202605 backport. Wouldn't prefer a CP - @r12f , @vaibhavhd , @yxieca for viz

@prsunny

prsunny commented Jun 18, 2026

Copy link
Copy Markdown
Collaborator

@Janetxxx , would you asses the PR for cherry-pick?

  1. No backward compatibility issues
  2. Are these changes enabled under a specific configuration?

@Janetxxx

Copy link
Copy Markdown
Contributor Author

Thanks for your comment @prsunny, I manually built an image with this PR included and ran the existing port mirroring tests span/test_port_mirroring.py on a SN5640 T0 testbed to verify that it didn't cause any backward compatibility issues.
image
image

@Janetxxx

Copy link
Copy Markdown
Contributor Author

@prsunny 2. Yes, these changes are opt-in. Sampled mirroring only activates when users specify sample_rate or truncate_size in the mirror session. Existing sessions without these optional fields continue to work as full mirrors, no behavior change.

image

@Janetxxx

Copy link
Copy Markdown
Contributor Author

Manually cherry-picked into 202607 Azure/sonic-swss.msft#254

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants