Skip to content

[yang] Require VLAN IP prefixes for DHCP relay servers - #31138

Draft
Xichen96 wants to merge 2 commits into
sonic-net:masterfrom
Xichen96:dev/xichenlin/dhcp-relay-ip-yang
Draft

Xichen96 wants to merge 2 commits into
sonic-net:masterfrom
Xichen96:dev/xichenlin/dhcp-relay-ip-yang

Conversation

@Xichen96

Copy link
Copy Markdown
Contributor

Why I did it

DHCP server configuration can pass YANG validation without an IP address on the corresponding VLAN. A bare L2 VLAN is valid, but its existence alone does not establish the interface-address dependency needed by DHCP relay.

vlanid is not an address-readiness check: it is optional, and a VLAN can be created from its name without that field. This change therefore validates the VLAN IP dependency instead of requiring vlanid or VLAN membership.

Work item tracking
  • Microsoft ADO (number only): 40921487

How I did it

Added must constraints requiring a configured VLAN_INTERFACE prefix on the same VLAN:

DHCP server field Required prefix
VLAN.dhcp_servers IPv4
VLAN.dhcpv6_servers IPv6
DHCPV4_RELAY.dhcpv4_servers IPv4
DHCP_RELAY.dhcpv6_servers IPv6

The check uses the prefix itself, not the optional family attribute. It accepts multiple addresses as long as a matching-family prefix exists. Bare VLANs without DHCP servers remain valid, vlanid remains optional, and existing minimum-server requirements are unchanged.

Updated existing fixtures with their required VLAN IP dependencies and added 14 focused cases for missing IPs, opposite-family IPs, IPs on another VLAN, empty legacy server lists and DHCPv6 options without servers.

This PR changes only YANG validation. It does not enable global CLI validation, alter CLI writers or intercept direct CONFIG_DB edits. Those paths can bypass YANG and require separate enforcement work.

How to verify it

Run the existing sonic-yang-models YANG model tests, including the VLAN and DHCPv4/DHCPv6 relay fixtures. Verify that DHCP server configuration fails without a same-VLAN, matching-family prefix, while existing valid configurations and bare VLANs without DHCP remain accepted.

For GCU, verify rejection of DHCP-before-IP and last-required-IP removal, and acceptance of combined DHCP/IP additions and IP replacement.

Which release branch to backport (provide reason below if selected)

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

No backport requested; master only.

Tracking issue/work item for backport/cherry-pick request (GitHub issue or Microsoft ADO): N/A
Failure type: pre-existing validation gap

Tested branch

  • master
  • 202305
  • 202311
  • 202405
  • 202411
  • 202505
  • 202511
  • 202512
  • 202605
  • 202608
  • 202611
  • N/A

Test result

Current-master model tests are pending PR CI.

Before preparing this PR, a prototype of the four constraints was exercised on SONiC.20260510.25. Model validation rejected missing/opposite-family IPs and accepted matching-family IPs without vlanid. GCU rejected missing-IP DHCP and last-IP removal, and accepted combined additions and IP replacements. This is prototype compatibility evidence, not a claim that the current PR head has passed master or release-branch CI.

Description for the changelog

Require a matching-family VLAN interface IP prefix for legacy and native DHCP relay server configuration.

Link to config_db schema for YANG module changes

A picture of a cute animal (not mandatory but encouraged)

Removing a VLAN or sub-interface (config vlan del, config interface
breakout, GCU) finds the rows that depend on it only through YANG
leafrefs. These fields named a local VLAN, and the BGP neighbor keys also
a sub-interface, by string pattern or plain string, so deleting the
interface left their rows behind, and nothing refused a row naming a VLAN
that never existed:

- BGP_NEIGHBOR, BGP_INTERNAL_NEIGHBOR, BGP_VOQ_CHASSIS_NEIGHBOR neighbor
- BGP local_addr (sonic-bgp-common)
- DHCPV4_RELAY name and source_interface
- DHCP_SERVER_IPV4 name
- ROUTE_MAP match_interface and match_neighbor
- RADIUS_SERVER src_intf
- NEIGH port
- MCLAG_UNIQUE_IP if_name
- STP_VLAN name
- DASH_HA_GLOBAL_CONFIG dpu_vlan

Each pattern becomes a leafref to VLAN or VLAN_SUB_INTERFACE, and the
commented-out leafrefs and imports left "until libyang back-links issue
is resolved" are restored: libyang3 resolves a leafref inside a union per
member. None of the changed modules is in the import closure of
sonic-vlan or sonic-vlan-sub-interface.

Where the old pattern stopped at Vlan4094, a must keeps Vlan4095 refused,
because the VLAN name pattern accepts it and vlanid is optional. NEIGH
port, STP_VLAN name and dpu_vlan accepted Vlan4095 before and still do.

Fixtures that named a VLAN without defining it gain the VLAN row (the
DHCP relay, server and match cases, and the sonic-yang-mgmt DHCP match
fixture), and sample_config_db.json gains Vlan222, which its
DHCPV4_RELAY already named.

sonic-yang-mgmt gains a static SonicYang.union_member_types(type_obj),
the compiled member types of a union ([] for a non-union).
libyang-python's Type.union_types() zips compiled members with parsed
ones, and libyang compiles a nested union such as inet:ip-address flat,
so it drops the trailing compiled members: the leafrefs these unions now
end with. sonic-utilities needs them to find what references a VLAN and
goes through sonic_yang rather than importing libyang. The yJson view's
union types use it too, so they keep those members.

Signed-off-by: Brad House <bhouse@nexthop.ai>
@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.sonic-buildimage

@azure-pipelines

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

@Xichen96
Xichen96 requested a balanced review from Copilot November 27, 2026 05:32

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.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

1 open issue
What changed in this PR

Adds YANG validation to ensure DHCP relay/server configuration is only accepted when the corresponding VLAN has a same-VLAN, matching-family interface IP prefix configured—closing a validation gap where DHCP settings could be applied to L2-only VLANs.

Changes:

  • Add must constraints in VLAN and DHCP relay YANG modules to require VLAN interface IP prefixes (IPv4/IPv6 as appropriate).
  • Import sonic-vlan into DHCPv4/DHCPv6 relay modules and add new revisions documenting the behavior.
  • Update existing test fixtures and add new negative/edge test cases covering missing/wrong-family/wrong-VLAN IP scenarios.
File Description
src/​sonic-yang-models/​yang-models/​sonic-vlan.yang Adds must constraints requiring VLAN interface IPv4/IPv6 prefixes when DHCP server leaf-lists are configured.
src/​sonic-yang-models/​yang-models/​sonic-dhcpv6-relay.yang Imports VLAN model and requires same-VLAN IPv6 prefix for DHCPv6 relay servers.
src/​sonic-yang-models/​yang-models/​sonic-dhcpv4-relay.yang Imports VLAN model and requires same-VLAN IPv4 prefix for DHCPv4 relay servers.
src/​sonic-yang-models/​tests/​yang_model_tests/​tests_config/​vlan.json Updates VLAN fixtures to include VLAN_INTERFACE prefixes; adds new negative cases.
src/​sonic-yang-models/​tests/​yang_model_tests/​tests_config/​dhcpv6_relay.json Updates fixtures to include VLAN + IPv6 prefixes; adds negative cases and options-without-servers case.
src/​sonic-yang-models/​tests/​yang_model_tests/​tests_config/​dhcpv4_relay.json Updates fixtures to include VLAN + IPv4 prefixes; adds negative cases.
src/​sonic-yang-models/​tests/​yang_model_tests/​tests/​vlan.json Adds expected error assertions for new VLAN DHCP server validation cases.
src/​sonic-yang-models/​tests/​yang_model_tests/​tests/​dhcpv6_relay.json Adds expected error assertions for new DHCPv6 relay validation cases.
src/​sonic-yang-models/​tests/​yang_model_tests/​tests/​dhcpv4_relay.json Adds expected error assertions for new DHCPv4 relay validation cases.

Review effort: Lite

Reject DHCP server configuration unless the same VLAN has a configured
VLAN_INTERFACE prefix of the corresponding address family. Apply the
dependency to legacy VLAN fields and native DHCPv4/DHCPv6 relay tables.

Keep vlanid optional and preserve bare VLANs without DHCP servers. Add
focused dependency tests and provide valid IP dependencies in existing
fixtures so their original checks remain meaningful.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7a24f7e2-7126-444f-bc0c-4ee2c7d97330
Signed-off-by: Xichen Lin <lukelin0907@gmail.com>
@Xichen96
Xichen96 force-pushed the dev/xichenlin/dhcp-relay-ip-yang branch from 6986bc3 to 934c9de Compare November 27, 2026 08:40
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run Azure.sonic-buildimage

@azure-pipelines

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

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