Skip to content

[dhcp-server] match condition based DHCP server tests - #29054

Open
nishant111 wants to merge 6 commits into
sonic-net:masterfrom
nishant111:nishant/dhcp-match-tests
Open

nishant111 wants to merge 6 commits into
sonic-net:masterfrom
nishant111:nishant/dhcp-match-tests

Conversation

@nishant111

Copy link
Copy Markdown
Contributor

Description of PR

Summary:
Adds sonic-mgmt test coverage for the IPv4 match-condition (MATCH mode) DHCP server feature, implementing the test plan in #28933. This covers address assignment, runtime config changes, CLI/GCU validation, lease clear, multi-VLAN isolation, and scale/flood.

The PR has two commits:

  1. [dhcp_server] Add MATCH mode DHCP server tests: shared helpers plus the functional (F), config-change (C) and CLI validation (V) modules.
  2. [dhcp_server] Add MATCH mode lease clear, multi-VLAN and stress tests: the lease clear (L), multi-VLAN (M) and stress (S) modules, plus a PTF flood generator.

Dependencies (sonic-buildimage):

Fixes # (issue)

Type of change

  • Bug fix
  • Testbed and Framework(new/improvement)
  • [ x] New Test case
    • Skipped for non-supported platforms
  • Test case improvement

Back port request

  • 202311
  • 202405
  • 202411
  • 202505
  • 202511
  • 202512
  • 202605

Tracking issue/work item for backport/cherry-pick request (GitHub issue or Microsoft ADO):
Failure type:

Tested branch

  • [x ] master
  • 202311
  • 202405
  • 202411
  • 202505
  • 202511
  • 202512
  • 202605
  • N/A

Test result

  • master: SONiC.master.0-c0666d2c8 (KVM vlab-01, vms-kvm-t0), with the dhcp_server container built from sonic-buildimage#30361. Results below cover both isc-relay-agent and sonic-relay-agent where the module is relay-parametrized.
Module File Result
F: functional / address assignment test_dhcp_server_match.py isc: 18/18 passed; sonic-relay: 17 passed, 1 skipped (F14 PortChannel, see below)
C: runtime config change test_dhcp_server_match_config_change.py 18/18 passed on each relay (including config_reload, service restart and cold reboot persistence)
V: CLI/GCU validation test_dhcp_server_match_cli.py All passed
L: lease clear test_dhcp_server_lease_clear.py 16/16 passed (PORT and MATCH modes, including 2 cold reboots)
M: multi-VLAN test_dhcp_server_match_multi_vlans.py 4/4 passed
S01: scale (24 ports, 120 bindings) test_dhcp_server_match_stress.py 2/2 passed; Kea converged 0.4 s after GCU apply
S02: DISCOVER flood (100 pps, 60 s) test_dhcp_server_match_stress.py Passed on both relays: 0% loss; average latency about 5 ms (sonic-relay) and 72 ms (isc)

Approach

What is the motivation for this PR?

MATCH mode lets the SONiC DHCP server pick an address pool by matching named conditions: the circuit-id port identity from Option 82 and the vendor class from Option 60, used alone or combined, with the most specific binding winning. This PR adds end-to-end coverage for the feature and the lease clear CLI, as defined in the MATCH test plan (#28933).

How did you do it?

  • dhcp_server_match_common.py: shared fixtures and helpers. They cover:
    • discovering the MATCH test environment (VLAN, PTF ports, prefix);
    • building MatchConfig objects and applying them through CLI or GCU;
    • snapshotting and restoring the existing DHCP server config;
    • building Option 82/60 DISCOVER/REQUEST packets and checking OFFER/ACK;
    • reading Kea pools and leases (lease4-get-all) and State DB leases;
    • a multi-VLAN GCU patch builder and a second_vlan context manager that moves PTF members into a temporary Vlan4001 and restores them afterwards.
  • dhcp_server_test_common.py: clean_dhcp_server_config now also deletes DHCP_SERVER_IPV4_BINDING and DHCP_SERVER_IPV4_MATCH, ahead of ranges and ports, so dependent entries are removed first.
  • F (test_dhcp_server_match.py):
    • circuit-id on the wire;
    • port-only, Option 60-only and combined bindings;
    • specificity across CLI/GCU and binding order;
    • pool exhaustion, shared pools, multiple ranges;
    • Option 60 byte matching and changes between messages;
    • RENEW/RELEASE, client MAC move, customized options;
    • PortChannel circuit-id.
  • C (test_dhcp_server_match_config_change.py):
    • binding add/update/delete, condition and range updates;
    • mode switch, interface enable/disable;
    • VLAN member and prefix changes;
    • invalid config keeping the last known good Kea config, missing references;
    • persistence (config reload, service restart, cold reboot);
    • critical process restart.
  • V (test_dhcp_server_match_cli.py):
    • show output;
    • CLI and GCU rejection of invalid input (multi-prefix, mode switch, rules in PORT mode);
    • CLI order independence.
  • L (test_dhcp_server_lease_clear.py):
    • clearing a single IP and --all, the latter also across a second VLAN;
    • persistence across Kea restart, service restart and cold reboot;
    • device replacement;
    • negative cases, including feature disabled, invalid IP and non-root;
    • stale State DB entry cleanup;
    • concurrent clear contention on the lock.
  • M (test_dhcp_server_match_multi_vlans.py):
    • MATCH and PORT VLANs side by side, where an invalid update on one VLAN must not affect the other's pools, leases, RENEW or new offers;
    • one match condition shared across two VLANs, each with its own pool.
  • S (test_dhcp_server_match_stress.py and dhcp_server_match_flood.py):
    • a scale test with 120 bindings across 24 ports, checking Kea convergence;
    • a PTF-side scapy flood that sends DISCOVERs at a fixed rate and matches OFFERs by xid/chaddr. It reports loss, wrong pool and latency, and the test asserts 0% loss and that DHCP server/relay processes did not restart.

How did you verify/test it?

  • Ran every module on a KVM vms-kvm-t0 testbed (vlab-01) running master with the dhcp_server container from sonic-buildimage#30361.
  • Ran relay-parametrized cases with both isc-relay-agent and sonic-relay-agent.
  • After each run, checked the DUT was restored: original VLAN membership, original DHCP server config, saved config matching running config, no leftover test leases.
  • flake8 is clean (--max-line-length=120).

Any platform specific information?

Supported testbed topology if it's a new test case?

mx (validated on a t0 KVM testbed)

Documentation

Test plan: #28933 (docs/testplan/IPv4-Match-Condition-Based-DHCP-Server-test-plan.md)
HLD: Port-based DHCP server HLD

nishant111 and others added 2 commits November 5, 2026 03:52
Add sonic-mgmt tests for MATCH condition based DHCPv4 address assignment
(test plan sonic-net#28933):

- test_dhcp_server_match.py (F01-F14): circuit_id, option60 and combined
  bindings, specificity, pool exhaustion, shared pools, multiple ranges,
  option60 byte matching, renew/release, MAC move, customized options and
  PortChannel circuit IDs.
- test_dhcp_server_match_config_change.py (C01-C12): runtime binding,
  match, range, mode, interface state, VLAN member and prefix changes,
  last-known-good recovery, missing references, persistence across config
  reload, service restart and cold reboot, and critical process restart.
- test_dhcp_server_match_cli.py (V01-V06): show output, CLI and GCU
  rejection of invalid configuration, PORT mode non-regression and order
  independence.
- dhcp_server_match_common.py: shared MATCH helpers. Kea leases are read
  from the memfile CSV so the tests do not depend on the lease_cmds hook.
- dhcp_server_test_common.py: clean MATCH binding and match tables.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Nishant Sharma <nshntsharma86@gmail.com>
Implement the remaining modules of the IPv4 match-condition DHCP server
test plan:

- test_dhcp_server_lease_clear.py (L01-L07): single-IP and --all clear,
  persistence across Kea/service restart and cold reboot, device
  replacement, negative/feature-disabled cases, stale State DB entry
  and concurrent clear lock contention, in PORT and MATCH modes.
- test_dhcp_server_match_multi_vlans.py (M01-M02): per-VLAN isolation
  (MATCH and PORT side by side, invalid update on one VLAN does not
  affect the other) and a shared match condition across VLANs.
- test_dhcp_server_match_stress.py (S01-S02): 120-binding scale
  convergence and a 100 pps DISCOVER flood with 0% loss check, run
  with both isc and sonic relay agents.
- dhcp_server_match_flood.py: PTF-side flood generator/correlator.
- dhcp_server_match_common.py: multi-VLAN GCU helpers, second_vlan
  context manager and State DB / Kea lease readers.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Nishant Sharma <nshntsharma86@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 5, 2026 05:37
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

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

@github-actions github-actions Bot added triage-Cisco PR assigned to Cisco for triage (assign_triage_labels workflow) triage-Microsoft PR assigned to Microsoft for triage (assign_triage_labels workflow) labels Nov 5, 2026

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.

🟡 Changes recommended

Unresolved test defects can cause premature failures, unnecessary skips, or false passes through insufficient address allocation, stale lease handling, missing runtime checks, and unreliable flood timing and restart detection.

8 open issues
What changed in this PR

Adds end-to-end coverage for IPv4 DHCP server MATCH mode in sonic-mgmt, implementing the scenarios from test plan #28933 alongside existing PORT-mode tests.

Changes:

  • Adds address-assignment, runtime configuration, CLI/GCU validation, lease-clear, and multi-VLAN tests.
  • Introduces shared configuration, DHCP client, lease-inspection, and cleanup helpers.
  • Adds scale tests and a PTF DISCOVER flood generator with response and latency checks.
File Description
tests/​dhcp_server/​test_dhcp_server_match.py Tests matching, specificity, allocation, and client behavior.
tests/​dhcp_server/​test_dhcp_server_match_stress.py Adds scale and flood scenarios.
tests/​dhcp_server/​test_dhcp_server_match_multi_vlans.py Tests VLAN isolation and PORT/MATCH coexistence.
tests/​dhcp_server/​test_dhcp_server_match_config_change.py Tests runtime updates, persistence, and recovery.
tests/​dhcp_server/​test_dhcp_server_match_cli.py Validates CLI output, rejection, and configuration ordering.
tests/​dhcp_server/​test_dhcp_server_lease_clear.py Tests lease deletion, persistence, and concurrent clears.
tests/​dhcp_server/​dhcp_server_test_common.py Extends cleanup to MATCH bindings and conditions.
tests/​dhcp_server/​dhcp_server_match_flood.py Generates DISCOVER traffic and measures responses.
tests/​dhcp_server/​dhcp_server_match_common.py Provides shared setup, configuration, client, and verification helpers.

Review effort: Balanced

- Select the test address window from the longest run of addresses
  without an active lease, ignoring released, deleted and expired Kea
  leases, and skip S01 when the VLAN cannot hold every scale binding.
- Skip MATCH tests unless both the YANG model and the dhcp_server
  runtime support MATCH mode, checked before the DUT is changed.
- Wait for the State DB lease to converge on the expected address and
  lifetime, so a lease left from a previous pool is not returned.
- Include the container start time in the DHCP server process snapshot
  and check the relay container and its processes across the flood.
- Timestamp each DISCOVER immediately before it is sent, and fail the
  flood when sending exceeds the scheduled duration; report rates from
  the measured duration.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Nishant Sharma <nshntsharma86@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 02:35
@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.

🟡 Changes recommended

Unresolved critical issues risk deleting pre-existing network configuration, logging authentication secrets, including gateway addresses in DHCP ranges, and falsely passing checks despite wrong-VLAN or wrong-pool replies.

6 open issues
2 previously missed issues

In code that hasn't changed since last review

Medium severity Register DHCP MATCH modules in the KVM PR gate

tests/​dhcp_server/​test_dhcp_server_match.py:22

Please register these new functional modules in the KVM PR-gate manifest. tests/kvmtest.sh explicitly enumerates its test suites and contains no DHCP server MATCH entries, so these cases are not run by the gate.

Medium severity Include configuration apply time in the convergence timeout

tests/​dhcp_server/​test_dhcp_server_match_stress.py:83

The convergence timer starts after the synchronous GCU command returns, although Kea can compile and activate the configuration while that command is running. Consequently, an update taking more than the 30-second limit can still pass because only the remaining wait is measured. Start timing before applying the configuration so the assertion includes that work.

8 resolved since last review

Review effort: Balanced

@nishant111
nishant111 marked this pull request as ready for review November 25, 2026 03:40
@nishant111
nishant111 requested a review from Xichen96 as a code owner November 25, 2026 03:40
…ests

- Do not log Config DB dumps (sonic-cfggen --print-data, saved config).
- Treat VLAN SVI addresses as occupied when choosing the free pool window.
- Skip second_vlan when the VLAN already exists instead of clobbering it.
- Assert DHCP replies arrive on a member port of the client's VLAN.
- Keep every reply per xid in the flood script; any invalid reply fails.
- Require PortChannel0999 to be absent before the PortChannel test.
- Exclude Ansible's own "Invoked with" syslog entries in wait_for_syslog.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Nishant Sharma <nshntsharma86@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 25, 2026 07:05
@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.

🔵 Needs a closer look

The tests can miss lease-clear locking and convergence-time regressions, and all six new modules lack registration for ongoing PR-gate coverage.

1 open issue
3 previously missed issues

In code that hasn't changed since last review

Medium severity Test lease-clear locking with deterministic contention

tests/​dhcp_server/​test_dhcp_server_lease_clear.py:333

This test can pass with the lease-clear lock bypassed: both commands may succeed on distinct IPs and leave the expected final lease map. Add deterministic contention by holding the lease-clear lock in a separate process and waiting for confirmation that it is held. Assert that a clear reports clear-in-progress without changing leases, and release the lock in cleanup.

Medium severity Add new test modules to the PR test manifest

tests/​dhcp_server/​test_dhcp_server_match.py:62

The six new test modules are absent from .azure-pipelines/pr_test_scripts.yaml, which .azure-pipelines/test_plan.py uses by default for PR runs, so they will not receive ongoing PR-gate coverage. Register the functional, config-change, CLI, multi-VLAN, stress, and lease-clear modules in the applicable manifest test set.

Medium severity Start convergence timing before configuration apply

tests/​dhcp_server/​test_dhcp_server_match_stress.py:83

Start the convergence timer before cfg.apply(...). Although wait=False skips the explicit pool wait, the GCU command still runs synchronously, so Kea can spend the entire compilation/reload interval converging before this timer starts. A 40-second apply followed by a 0.4-second pool check currently passes the 30-second limit and reports 0.4 seconds, masking the scale regression this assertion is intended to catch.

5 resolved since last review

Review effort: Balanced

- L07: hold the lease-clear lock from a separate DUT process and verify that
  single-IP and --all clears report clear-in-progress without changing leases,
  then succeed once the lock is released.
- S01: start the convergence timer before the synchronous GCU apply.
- Register the new MATCH and lease-clear modules in pr_test_scripts.yaml.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Nishant Sharma <nshntsharma86@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 26, 2026 04:05
@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.

🔵 Needs a closer look

Human assessment is needed because the review evidence identifies gaps in CI coverage, failure handling, and state verification that could leave test failures or inconsistent DHCP state undetected.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@StormLiangMS StormLiangMS left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@nishant111 — one remaining correctness blocker in reply-delivery validation; see the inline comment.

Reviewed 654ea6daf1783b9c9444b74af93950be20814067. The new revision addresses the prior scale-timing, lease-clear-locking, and PR-test-registration findings.

DCO / CLA / static checks: passing. CI: build 1288258 is red; inspected failures report Celery timeouts, testbed sanity failures, and an unrelated ARP test failure. I have not established a DHCP regression from those failures.

Please address the client-port false pass before merge.

DhcpClient.exchange polled every PTF port, so a reply delivered only to
another member of the same VLAN was accepted as if the client had received
it. Poll the client's own port instead; flooded copies on other ports are
ignored, and a matching reply seen only on another port fails the test.

Add unit tests with a fake dataplane covering a reply on the wrong
same-VLAN port only, a flooded copy before the client's reply, no reply,
and expect_no_offer staying strict.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Nishant Sharma <nshntsharma86@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 27, 2026 01:26
@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.

🔵 Needs a closer look

Human assessment is needed because omitted findings identify risks of deleting real client leases and leaving relay selection or DHCP configuration unrestored.

No open issues 🎉

Review effort: Balanced

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

Labels

triage-Cisco PR assigned to Cisco for triage (assign_triage_labels workflow) triage-Microsoft PR assigned to Microsoft for triage (assign_triage_labels workflow)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants