Skip to content

Commit 86ed5d5

Browse files
isc-dhcp relay supports vrf
1 parent be9dd15 commit 86ed5d5

File tree

5 files changed

+102
-12
lines changed

5 files changed

+102
-12
lines changed

dockers/docker-dhcp-relay/dhcpv4-relay.agents.j2

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,30 @@
99
{% set _dummy = relay_for_ipv4.update({'flag': False}) %}
1010
[program:isc-dhcpv4-relay-{{ vlan_name }}]
1111
{# We treat this VLAN as a downstream interface (-id), as we only want to listen for requests #}
12-
command=/usr/sbin/dhcrelay -d -m discard -a %%h:%%p %%P --name-alias-map-file /tmp/port-name-alias-map.txt -id {{ vlan_name }}
12+
{% if vlan_name in VLAN_INTERFACE and VLAN_INTERFACE[vlan_name]['vrf_name']|length > 0 %}
13+
{% set non_default_found = true %}
14+
{% set vrf_name = VLAN_INTERFACE[vlan_name]['vrf_name'] %}
15+
{% endif %}
16+
{% if non_default_found %}
17+
command=ip vrf exec {{vrf_name}} /usr/sbin/dhcrelay -d -m discard -a %%h:%%p %%P --name-alias-map-file /tmp/port-name-alias-map.txt -id {{ vlan_name -}}
18+
{% else %}
19+
command=/usr/sbin/dhcrelay -d -m discard -a %%h:%%p %%P --name-alias-map-file /tmp/port-name-alias-map.txt -id {{ vlan_name -}}
20+
{% endif -%}
1321
{#- Dual ToR Option #}
1422
{% if 'subtype' in DEVICE_METADATA['localhost'] and DEVICE_METADATA['localhost']['subtype'] == 'DualToR' %} -U Loopback0 -dt{% endif -%}
1523
{#- si option to use intf addr in relay #}
1624
{% if DEVICE_METADATA['localhost']['deployment_id'] == '8' %} -si{% endif -%}
1725
{#- We treat all other interfaces as upstream interfaces (-iu), as we only want to listen for replies #}
18-
{% for (name, prefix) in VLAN_INTERFACE|pfx_filter %}
19-
{% if prefix | ipv4 and name != vlan_name %} -iu {{ name }}{% endif -%}
20-
{% endfor %}
21-
{% for (name, prefix) in INTERFACE|pfx_filter %}
22-
{% if prefix | ipv4 %} -iu {{ name }}{% endif -%}
23-
{% endfor %}
24-
{% for (name, prefix) in PORTCHANNEL_INTERFACE|pfx_filter %}
25-
{% if prefix | ipv4 %} -iu {{ name }}{% endif -%}
26-
{% endfor %}
26+
{% for (name, prefix) in VLAN_INTERFACE|pfx_filter -%}
27+
{%- if prefix | ipv4 and name != vlan_name %} -iu {{ name }}{% endif -%}
28+
{%- if prefix | ipv4 and name == vlan_name %} -bind {{ prefix.split('/')[0] }}{% endif -%}
29+
{%- endfor -%}
30+
{% for (name, prefix) in INTERFACE|pfx_filter -%}
31+
{%- if prefix | ipv4 %} -iu {{ name }}{% endif -%}
32+
{%- endfor -%}
33+
{% for (name, prefix) in PORTCHANNEL_INTERFACE|pfx_filter -%}
34+
{%- if prefix | ipv4 %} -iu {{ name }}{% endif -%}
35+
{%- endfor -%}
2736
{% for (name, gateway) in VLAN_INTERFACE|get_primary_addr %}
2837
{% if gateway | ipv4 and name == vlan_name %} -pg {{ gateway }}{% endif -%}
2938
{% endfor %}

rules/docker-dhcp-relay.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ endif
4545
endif
4646

4747
$(DOCKER_DHCP_RELAY)_CONTAINER_NAME = dhcp_relay
48-
$(DOCKER_DHCP_RELAY)_CONTAINER_PRIVILEGED = false
48+
$(DOCKER_DHCP_RELAY)_CONTAINER_PRIVILEGED = true
4949
$(DOCKER_DHCP_RELAY)_CONTAINER_VOLUMES += /etc/sonic:/etc/sonic:ro
5050
$(DOCKER_DHCP_RELAY)_CONTAINER_VOLUMES += /etc/localtime:/etc/localtime:ro
5151
$(DOCKER_DHCP_RELAY)_CONTAINER_TMPFS += /tmp/
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
From 58d694a46ad48776c7f20d8f19f453478c1863e2 Mon Sep 17 00:00:00 2001
2+
From: mirror-bot <mirror@github-actions>
3+
Date: Mon, 7 Jul 2025 02:10:44 -0500
4+
Subject: [PATCH] ISC-DHCP-support-dhcp-relay-in-vrf
5+
6+
---
7+
common/socket.c | 8 ++++++++
8+
relay/dhcrelay.c | 13 ++++++++++---
9+
2 files changed, 18 insertions(+), 3 deletions(-)
10+
11+
diff --git a/common/socket.c b/common/socket.c
12+
index 87c62a2..e274d4e 100644
13+
--- a/common/socket.c
14+
+++ b/common/socket.c
15+
@@ -48,6 +48,8 @@
16+
#include <net/if_dl.h>
17+
#include <sys/dlpi.h>
18+
#endif
19+
+char uplink_name[IFNAMSIZ];
20+
+struct in_addr bind_ia;
21+
22+
#ifdef USE_SOCKET_FALLBACK
23+
# if !defined (USE_SOCKET_SEND)
24+
@@ -223,6 +225,12 @@ if_register_socket(struct interface_info *info, int family,
25+
memcpy(&addr->sin_addr,
26+
&local_address,
27+
sizeof(addr->sin_addr));
28+
+ if (0 <= strlen(uplink_name) && 0 == strcmp(info->name, uplink_name)) {
29+
+ memcpy(&addr->sin_addr, &bind_ia, sizeof(bind_ia.s_addr));
30+
+ char str_buf[IFNAMSIZ];
31+
+ inet_ntop(AF_INET, &bind_ia, str_buf, sizeof(str_buf));
32+
+ log_info("bind uplink interface %s to ip:%s success ", uplink_name, str_buf);
33+
+ }
34+
#ifdef HAVE_SA_LEN
35+
addr->sin_len = sizeof(*addr);
36+
#endif
37+
diff --git a/relay/dhcrelay.c b/relay/dhcrelay.c
38+
index a516829..65e4161 100644
39+
--- a/relay/dhcrelay.c
40+
+++ b/relay/dhcrelay.c
41+
@@ -44,7 +44,8 @@ int lexline;
42+
int lexchar;
43+
char *token_line;
44+
char *tlname;
45+
-
46+
+extern char uplink_name [];
47+
+extern struct in_addr bind_ia;
48+
const char *path_dhcrelay_pid = _PATH_DHCRELAY_PID;
49+
isc_boolean_t no_dhcrelay_pid = ISC_FALSE;
50+
/* False (default) => we write and use a pid file */
51+
@@ -508,7 +509,7 @@ main(int argc, char **argv) {
52+
if (++i == argc) {
53+
usage(use_noarg, argv[i-1]);
54+
}
55+
-
56+
+ memcpy(uplink_name, argv[i], strlen(argv[i]));
57+
request_v4_interface(argv[i], INTERFACE_UPSTREAM);
58+
} else if (!strcmp(argv[i], "-id")) {
59+
#ifdef DHCPv6
60+
@@ -724,7 +725,13 @@ main(int argc, char **argv) {
61+
local_family = AF_INET;
62+
#endif
63+
enable_support_for_dual_tor = 1;
64+
- } else if (argv[i][0] == '-') {
65+
+ } else if (!strcmp(argv[i], "-bind")) {
66+
+ if (++i == argc)
67+
+ usage(use_v4command, argv[i-1]);
68+
+ if (inet_pton(AF_INET, argv[i], &bind_ia)) {
69+
+ log_info("bind_ia is %s \n", argv[i]);
70+
+ }
71+
+ } else if (argv[i][0] == '-') {
72+
usage("Unknown command: %s", argv[i]);
73+
} else {
74+
struct hostent *he;
75+
--
76+
2.25.1
77+

src/isc-dhcp/patch/series

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
0015-option-to-set-primary-address-in-interface.patch
1616
0016-Don-t-look-up-the-ifindex-for-fallback.patch
1717
0017-Register-IO-obj-before-create-fd-watch.patch
18+
0018-ISC-DHCP-support-dhcp-relay-in-vrf.patch

src/sonic-dhcp-utilities/dhcp_utilities/dhcprelayd/dhcprelayd.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,10 @@ def _get_dhcp_relay_config(self):
274274
cmds = re.findall(r"\[program:((isc-dhcpv4-relay|dhcpmon)-.+)\]\ncommand=(.+)", content)
275275
for cmd in cmds:
276276
key = "dhcpmon:{}".format(cmd[0]) if "dhcpmon" in cmd[0] else cmd[0]
277-
res[key] = cmd[2].replace("%%", "%").split(" ")
277+
command_str = cmd[2].replace("%%", "%")
278+
if key.startswith("isc-dhcpv4-relay"):
279+
command_str = re.sub(r"^ip vrf exec \S+\s*", "", command_str)
280+
res[key] = command_str.split(" ")
278281
return res
279282

280283
def _start_dhcrelay_process(self, new_dhcp_interfaces, dhcp_server_ip, force_kill):

0 commit comments

Comments
 (0)