Skip to content

[dhcp_relay] sonic dhcp relay agent for IPv4#67

Merged
Blueve merged 20 commits into
sonic-net:masterfrom
cshivashgit:sonic-dhcp4relay
Nov 5, 2025
Merged

[dhcp_relay] sonic dhcp relay agent for IPv4#67
Blueve merged 20 commits into
sonic-net:masterfrom
cshivashgit:sonic-dhcp4relay

Conversation

@cshivashgit

@cshivashgit cshivashgit commented Apr 30, 2025

Copy link
Copy Markdown
Contributor

Why I did it
DHCP Relay agent for IPv4

How I did it
Merging dhcp4 changes into this repo, created dhcp6[existing code] and dhcp4[new code] folders

HLD link:
https://github.com/sonic-net/SONiC/blob/2eb20e2d2bcc181ab8cf1f502173ac0dbd528698/doc/DHCPv4_relay/DHCPv4-relay-agent-High-Level-Design.md

@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).

@ashutosh-agrawal

Copy link
Copy Markdown
Member

@yaqiangz Appreciate if you can provide feedback on the directory/file structure.

@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).

…and dhcp4relay[new code] folders

Co-authored-by: Akshath TR <akshath.t.r@gmail.com>
Co-authored-by: Ashutosh Agrawal <ashu@cisco.com>
@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).

Comment thread dhcp4relay/src/dhcp4relay.cpp Outdated
Comment thread dhcp4relay/src/dhcp4relay_mgr.cpp Outdated
Comment thread dhcp4relay/src/dhcp4relay_stats.cpp Outdated
Comment thread dhcp4relay/src/dhcp4relay_stats.cpp Outdated
Comment thread dhcp4relay/src/dhcp4relay.cpp Outdated
Comment thread dhcp4relay/src/dhcp4relay.cpp
Comment thread dhcp4relay/src/dhcp4relay.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).

Move pcappp_v24.09.zip and patch folder inside dhcp4relay directory
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

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

Blueve
Blueve previously approved these changes Nov 3, 2025
@Blueve

Blueve commented Nov 3, 2025

Copy link
Copy Markdown

Close and re-open to rerun PR test

@Blueve Blueve closed this Nov 3, 2025
@Blueve Blueve reopened this Nov 3, 2025
@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: Ashutosh Agrawal <ashu@cisco.com>
Fixed a typo to retrigger PR checks
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

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

@Blueve
Blueve merged commit 8772dd5 into sonic-net:master Nov 5, 2025
8 checks passed

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.

Why is there a random zip file added here? If this is actually needed as a build dependency, compile it as a separate application or add it into the slave container.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@saiarcot895 Since, we are applying a minor patch on top of the downloaded PCAP++ source code, we didn't include it in the slave container. But I've modified the build process to now download the file instead of including it in the code. Please review the changes in this PR

ashutosh-agrawal added a commit to cshivashgit/sonic-dhcp-relay that referenced this pull request Dec 8, 2025
- Replace 17MB pcappp_v24.09.zip binary with wget download from GitHub
- Download from official release: github.com/seladb/PcapPlusPlus/archive/refs/tags/v24.09.tar.gz
- Add SHA256 checksum verification (b14998ecf5718c2bfb6f709060e53f7ef67639b98bf2222a3cd23d820d70acdc)
- Fix patch file paths to apply cleanly (remove PcapPlusPlus-24.09/ prefix)
- Add .gitignore to exclude downloaded artifacts

Addresses review comment from saiarcot895 on PR sonic-net#67
ashutosh-agrawal added a commit to cshivashgit/sonic-dhcp-relay that referenced this pull request Dec 8, 2025
- Replace 17MB pcappp_v24.09.zip binary with wget download from GitHub
- Download from official release: github.com/seladb/PcapPlusPlus/archive/refs/tags/v24.09.tar.gz
- Add SHA256 checksum verification (b14998ecf5718c2bfb6f709060e53f7ef67639b98bf2222a3cd23d820d70acdc)
- Fix patch file paths to apply cleanly (remove PcapPlusPlus-24.09/ prefix)
- Add .gitignore to exclude downloaded artifacts

Addresses review comment from saiarcot895 on PR sonic-net#67
Blueve pushed a commit that referenced this pull request Dec 11, 2025
* [sonic-dhcp4relay]: Defect fixes

Below sonic-mgmt issues are fixed:
1. Random source port
2. Padding issues
3. DHCP Server fixes

Signed-off-by: Shivashankar CR <shivashankar.c.r@gmail.com>

* [sonic-dhcp4relay] Smart switch support

Signed-off-by: Shivashankar CR <shivashankar.c.r@gmail.com>

* Remove binary zip file and download PcapPlusPlus during build

- Replace 17MB pcappp_v24.09.zip binary with wget download from GitHub
- Download from official release: github.com/seladb/PcapPlusPlus/archive/refs/tags/v24.09.tar.gz
- Add SHA256 checksum verification (b14998ecf5718c2bfb6f709060e53f7ef67639b98bf2222a3cd23d820d70acdc)
- Fix patch file paths to apply cleanly (remove PcapPlusPlus-24.09/ prefix)
- Add .gitignore to exclude downloaded artifacts

Addresses review comment from saiarcot895 on PR #67

---------

Signed-off-by: Shivashankar CR <shivashankar.c.r@gmail.com>
Co-authored-by: Ashutosh Agrawal <ashu@cisco.com>
Xichen96 added a commit to Xichen96/sonic-dhcp-relay that referenced this pull request Apr 25, 2026
… <Vrf>" (socket not rebound to new VRF)

Bug
---
On a SONiC device with dhcp_relay configured on a VLAN, moving that
VLAN to a non-default VRF at runtime via

    config vrf bind <Vlan> <Vrf>

leaves the dhcp_relay container running but functionally broken:
client DISCOVER/REQUEST still reach the relay (they arrive on the
client-facing VLAN, which is a kernel-level event), but every relayed
unicast to the upstream DHCP server is sent out the wrong interface
(typically eth0 in the management VRF, since that is what the upstream
socket is still bound to). The server's OFFER/ACK either never reach
the device or arrive on an interface the relay is not listening on,
so DHCP discovery for any client behind that VLAN times out
indefinitely.

The bug is silent: no error log, no counter increment, no relay
restart. The only externally visible symptom is "clients in the new
VRF cannot get a lease". Restarting the dhcp_relay container (or
`config reload`) papers over the issue, because the daemon's startup
path opens its sockets via a separate, correct CONFIG_DB lookup.

Impact
------
* Any production workflow that relies on rebinding a VLAN's VRF
  without restarting dhcp_relay is broken. This includes day-2
  multi-tenant onboarding, customer VRF migrations, and the
  sonic-mgmt test `tests/dhcp_relay/test_dhcpv4_relay.py::
  test_dhcp_relay_with_non_default_vrf`, which currently has to work
  around the bug by explicitly restarting dhcp_relay after VRF
  binding.
* The bug has been latent since the field was first introduced (PRs
  sonic-net#67 and sonic-net#84) — every release that ships sonic-dhcp-relay >= those
  PRs is affected.

Root cause
----------
process_vlan_interface_notification() in dhcp4relay_mgr.cpp is the
runtime handler for VLAN_INTERFACE updates from CONFIG_DB. For the
bare-key form ("VLAN_INTERFACE|Vlan1000") it iterates the entry's
fields and copies a vrf name into the outbound relay-message:

    if (fvField(fv) == "vrf") {       // <-- wrong field name
        vrf = fvValue(fv);
        break;
    }

The schema field name is "vrf_name", not "vrf" — the same module's
own startup-time lookup at dhcp4relay.cpp:864 and dhcp4relay_mgr.cpp:382
correctly uses "vrf_name". The comparison therefore never matches,
the outbound message's vrf stays empty, and the consumer in
dhcp4relay.cpp at the equivalent of:

    if ((msg->vrf.empty()) || ...) {
        return;
    }

short-circuits, so handle_server_sock() — which is what would close
the upstream socket and re-open it with
`setsockopt(SO_BINDTODEVICE, vrf)` — is never called. The socket
remains bound to whichever VRF was current at startup.

Fix
---
* Read field VRF_NAME_FIELD ("vrf_name") instead of the typo'd
  "vrf" in the bare-key branch of
  process_vlan_interface_notification(). This makes the runtime
  rebind path consistent with the startup path and with the schema.

* Pull the five reused CONFIG_DB field-name string literals
  ("vrf_name", "server_vrf", "source_interface",
  "link_selection", "state") into named macros in dhcp4relay.h.
  These literals were previously duplicated across the two .cpp
  files, which is exactly how the typo above slipped through review:
  the broken site was the only place writing the field name, so a
  reader had nothing to compare against. Centralizing them lets the
  compiler and a single grep enforce consistency.

The IP-suffix branch ("VLAN_INTERFACE|Vlan1000|<ip>/<mask>") is left
exactly as upstream — those events do not carry vrf_name and the
bare-key event is what drives the rebind. Limiting the change to the
buggy branch avoids any spurious extra socket rebinds on plain
`config interface ip add/remove`.

Validation
----------
Reproduced in KVM testbed with sonic-mgmt
`test_dhcp_relay_with_non_default_vrf` (4 cases). Without the fix,
all 4 fail at the PTF check `expected 48, got 0` (no DHCP packet
makes it back). With the fix applied to the dhcp_relay deb on the
DUT, all 4 pass, and the test-side `restart_dhcp_service`
workaround can be removed.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Xichen96 <lukelin0907@gmail.com>
mssonicbld added a commit that referenced this pull request Apr 28, 2026
… <Vrf>" (socket not rebound to new VRF) (#107)

 ### Why I did it
 On a SONiC device with dhcp_relay running on a VLAN, moving that VLAN to a non-default VRF at runtime via `config vrf bind <Vlan> <Vrf>` silently breaks DHCP: the relay's upstream socket stays bound to the original VRF, so OFFER/ACK never reach clients. The bug is silent — no log, no counter, no restart. Restarting dhcp_relay (or `config reload`) papers
over it because the startup path uses the correct CONFIG_DB lookup. Bug has been latent since PR #67 / #84.

 ### How I did it
 `process_vlan_interface_notification()` in `dhcp4relay_mgr.cpp` reads field `"vrf"` from the `VLAN_INTERFACE` update, but the schema field is `"vrf_name"` (the same module's startup path uses the correct name). The mismatch leaves `msg->vrf` empty, the consumer in `dhcp4relay.cpp` short-circuits at `if (msg->vrf.empty()) return;`, and
`handle_server_sock()` — which would `setsockopt(SO_BINDTODEVICE, vrf)` — never runs.

 Fix: read `VRF_NAME_FIELD` instead of the typo, and pull the five reused CONFIG_DB field-name literals (`vrf_name`, `server_vrf`, `source_interface`, `link_selection`, `state`) into named macros in `dhcp4relay.h` so this typo class cannot recur silently. The IP-suffix branch is left untouched — bare-key event drives the rebind.

 ### How to verify it
 sonic-mgmt `tests/dhcp_relay/test_dhcpv4_relay.py::test_dhcp_relay_with_non_default_vrf` (4 cases). Without the fix all 4 fail with PTF `expected 48, got 0`. With the fix applied to the on-DUT dhcp_relay deb, all 4 pass; the test-side `restart_dhcp_service` workaround can be removed.

Signed-off-by: Sonic Build Admin <sonicbld@microsoft.com>
@sumati3031-arista

Copy link
Copy Markdown

@cshivashgit after the introduction of sonic relay agent, we are seeing a lot of failures in dhcp_relay testcases
We want to understand if you are aware and if any work is in progress to fix those failures.
I have proposed fix for a few in the following PRs.
sonic-net/sonic-dhcpmon#81
sonic-net/sonic-mgmt#25394

@ashutosh-agrawal

Copy link
Copy Markdown
Member

@sumati3031-arista We are constantly working on improving quality and stability of dhcp_relay. We have multiple merged PRs since the original design and have two more open PRs. Please help with the review:

#112
#109

For any other issues, please feel free to tag @cshivashgit or myself and we will look at it promptly.

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.