Skip to content

[orchagent][MirrorOrch] Add sampled port mirroring support to MirrorOrch#4488

Closed
Janetxxx wants to merge 8 commits into
sonic-net:masterfrom
Janetxxx:dev/jc/sampled-port-mirror
Closed

[orchagent][MirrorOrch] Add sampled port mirroring support to MirrorOrch#4488
Janetxxx wants to merge 8 commits into
sonic-net:masterfrom
Janetxxx:dev/jc/sampled-port-mirror

Conversation

@Janetxxx

@Janetxxx Janetxxx commented Apr 16, 2026

Copy link
Copy Markdown
Contributor

What I did
Depends on: #4502

Adds sampled port mirroring support to MirrorOrch (orchagent), extending mirror session configuration to optionally create and attach a SAI SamplePacket object and to program ports using sampled-mirroring attributes instead of full-mirror attributes.

  • Extend MirrorEntry and CONFIG_DB parsing to support sample_rate and truncate_size.
  • Create/remove SAI SamplePacket objects when sample_rate > 0 and wire them into session activation/deactivation.
  • Update per-port mirroring programming to use SAI_PORT_ATTR_INGRESS_SAMPLEPACKET_ENABLE + SAI_PORT_ATTR_INGRESS_SAMPLE_MIRROR_SESSION for sampled mirroring.

Why I did it
Added sampled port mirroring with truncation support on ERSPAN sessions

How I verified it

Details if related

Copilot AI review requested due to automatic review settings April 16, 2026 07:22
@Janetxxx
Janetxxx requested a review from prsunny as a code owner April 16, 2026 07:22
@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 sampled port mirroring support to MirrorOrch (orchagent), extending mirror session configuration to optionally create and attach a SAI SamplePacket object and to program ports using sampled-mirroring attributes instead of full-mirror attributes.

Changes:

  • Extend MirrorEntry and CONFIG_DB parsing to support sample_rate and truncate_size.
  • Create/remove SAI SamplePacket objects when sample_rate > 0 and wire them into session activation/deactivation.
  • Update per-port mirroring programming to use SAI_PORT_ATTR_INGRESS_SAMPLEPACKET_ENABLE + SAI_PORT_ATTR_INGRESS_SAMPLE_MIRROR_SESSION for sampled mirroring.

Reviewed changes

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

File Description
orchagent/mirrororch.h Adds sampled-mirroring fields to MirrorEntry and declares helper APIs / updated port programming signature.
orchagent/mirrororch.cpp Parses new CONFIG_DB fields, creates/removes SamplePacket, and programs ports for sampled mirroring.

Comment thread orchagent/mirrororch.cpp Outdated
Comment thread orchagent/mirrororch.cpp Outdated
Comment thread orchagent/mirrororch.cpp
Comment thread orchagent/mirrororch.cpp
Comment thread orchagent/mirrororch.cpp Outdated
Comment thread orchagent/mirrororch.cpp Outdated
Comment thread orchagent/mirrororch.cpp
@Janetxxx
Janetxxx removed request for prsunny and r12f April 16, 2026 10:43
@Janetxxx
Janetxxx force-pushed the dev/jc/sampled-port-mirror branch from 01e7dc0 to bc52765 Compare April 16, 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).

@Janetxxx
Janetxxx force-pushed the dev/jc/sampled-port-mirror branch from bc52765 to ccbb144 Compare April 16, 2026 11:23
@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 branch from ccbb144 to 987a4e4 Compare April 16, 2026 11:58
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

- MirrorEntry: add sample_rate, truncate_size, samplepacketId fields
- createEntry: parse sample_rate and truncate_size from CONFIG_DB
- createEntry: validate sampled mirroring only supports RX direction
- activateSession: create SamplePacket (TYPE=MIRROR_SESSION) when sample_rate > 0
- deactivateSession: remove SamplePacket before removing mirror session
- setUnsetPortMirror: sampled path uses INGRESS_SAMPLEPACKET_ENABLE +
  INGRESS_SAMPLE_MIRROR_SESSION instead of INGRESS_MIRROR_SESSION
- setUnsetPortMirror: rollback on partial failure
- createSamplePacket/removeSamplePacket: new helper functions
- setSessionState: include sample_rate and truncate_size

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Janet Cui <janet970527@gmail.com>
@azure-pipelines

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

@Janetxxx
Janetxxx force-pushed the dev/jc/sampled-port-mirror branch from 987a4e4 to 8cfd52d Compare April 16, 2026 11:58
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

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

@banidoru banidoru left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  • Resource leak: samplepacket not cleaned up if port mirror config fails after samplepacket creation succeeds in activateSession
  • Silent fallthrough: egress + sample_rate > 0 silently uses full-mirror path in setUnsetPortMirror — needs defensive guard
  • Undefined OID usage: createSamplePacket may use garbage samplepacketId after SAI failure if handleSaiCreateStatus returns task_success
  • Naming inconsistency: new fields use snake_case vs existing camelCase convention

Comment thread orchagent/mirrororch.cpp
Comment thread orchagent/mirrororch.cpp
Comment thread orchagent/mirrororch.cpp Outdated
Comment thread orchagent/mirrororch.h

@banidoru banidoru left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  • In setUnsetPortMirror, the sample_rate > 0 && ingress check is insufficient. If ingress is false, the function silently falls through to the non-sampled path. Add a guard to explicitly reject sampled egress mirroring.
  • In activateSession, a resource leak can occur. If createSamplePacket succeeds but configurePortMirrorSession fails, the created samplepacket is not cleaned up.
  • In createSamplePacket, if create_samplepacket fails, the code may log a success message and return true if handleSaiCreateStatus returns task_success, leading to the use of an uninitialized samplepacketId.
  • In deactivateSession, if removeSamplePacket fails, the error is logged but the function proceeds. This can lead to a resource leak. The function should return false on failure.

Comment thread orchagent/mirrororch.cpp
Comment thread orchagent/mirrororch.cpp
Comment thread orchagent/mirrororch.cpp Outdated
Comment thread orchagent/mirrororch.cpp

@banidoru banidoru left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  • to_uint<uint32_t> can throw on malformed config input, crashing orchagent instead of returning task_invalid_entry
  • deactivateSession clears ports before removing samplepacket — if samplepacket removal fails, state is half-torn-down with no recovery path
  • createSamplePacket logs success and returns true even when SAI create failed (if handleSaiCreateStatus returns task_success) — OID may be garbage
  • Resource leak: samplepacket not cleaned up when configurePortMirrorSession fails after successful createSamplePacket

Comment thread orchagent/mirrororch.cpp
Comment thread orchagent/mirrororch.cpp
return false;
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Half-deactivated state on samplepacket removal failure: At this point, configurePortMirrorSession(name, session, false) has already succeeded — ports are unconfigured. If removeSamplePacket fails and returns false, the mirror session and samplepacket SAI objects remain, but the ports no longer reference them. A subsequent retry of deactivateSession will attempt configurePortMirrorSession(false) again on already-cleared ports.

Consider restructuring so samplepacket removal failure still proceeds to remove the mirror session (log and continue), or skip port unconfiguration on retry if ports are already cleared.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Still open — this concern (half-deactivated state) is not addressed in the new commits.

Comment thread orchagent/mirrororch.cpp
Comment thread orchagent/mirrororch.cpp
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).

@banidoru banidoru left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-review (iteration 2) — new updateEntry commit reviewed.

  • updateEntry is delete+recreate: This works but is disruptive — it tears down the active mirror session (deactivates ports, removes SAI objects) and recreates from scratch, causing a traffic-mirroring gap. For mutable fields, consider updating in-place; for immutable fields, the delete+recreate approach is acceptable but should be documented.
  • Duplicate entry guard removed from createEntry: The old duplicate check (task_duplicated) was removed. Now createEntry no longer guards against duplicates — it relies on the caller (doTask) to check sessionExists() first. If any other caller invokes createEntry directly on an existing session, it will silently overwrite state. Consider keeping a defensive check or adding an assert.
  • updateEntry doesn't validate delete side-effects: If deleteEntry partially succeeds (e.g., deactivation fails mid-way but entry is removed from map), createEntry will proceed on potentially inconsistent SAI state.
  • All prior review concerns remain unaddressed — resource leaks, rollback gaps, STATE_DB persistence, LAG handling, naming, input validation, unit tests, etc. are still open.

Comment thread orchagent/mirrororch.cpp
Comment thread orchagent/mirrororch.cpp Outdated
Comment thread orchagent/mirrororch.cpp

@banidoru banidoru left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

  • STATE_DB Persistence: and are still not persisted to STATE_DB, which is necessary for warm-restart scenarios.
  • Unit Tests: No new unit tests have been added to cover the new sampled mirroring logic, including success, failure, and rollback paths.
  • LAG Port Handling: The sampled mirroring implementation does not correctly handle LAG ports; it attempts to configure the LAG object itself rather than its members.
  • Resource Leaks: There are multiple paths where objects can be leaked upon partial failures during session activation or deactivation.
  • Error Handling: Several error paths have insufficient or incorrect error handling, such as silent fallthroughs and uncaught exceptions from string-to-integer conversions.

@azure-pipelines

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

@Janetxxx
Janetxxx force-pushed the dev/jc/sampled-port-mirror branch from c979e04 to aa86f38 Compare April 24, 2026 03:12
@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 branch from aa86f38 to 7478d5c Compare April 24, 2026 03:20
@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 branch from 7478d5c to d479e35 Compare April 24, 2026 07:58
@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 branch from d479e35 to 6603185 Compare April 24, 2026 09: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 force-pushed the dev/jc/sampled-port-mirror branch from 6603185 to 1f9eb70 Compare April 24, 2026 09:41
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@Janetxxx
Janetxxx force-pushed the dev/jc/sampled-port-mirror branch from 1f9eb70 to 2935ddc Compare April 24, 2026 09:41
@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).

Signed-off-by: Janet Cui <janet970527@gmail.com>
@Janetxxx
Janetxxx force-pushed the dev/jc/sampled-port-mirror branch from 2935ddc to da47032 Compare April 24, 2026 10:10
@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 changed the title [orchagent] Add sampled port mirroring support to MirrorOrch [orchagent][MirrorOrch] Add sampled port mirroring support to MirrorOrch Apr 29, 2026
@Janetxxx

Janetxxx commented Apr 29, 2026

Copy link
Copy Markdown
Contributor Author

Closing this PR and merging contents into #4502

@Janetxxx Janetxxx closed this Apr 29, 2026
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.

4 participants