diff --git a/dockers/docker-dhcp-relay/dhcp-relay.programs.j2 b/dockers/docker-dhcp-relay/dhcp-relay.programs.j2 index d6c0d805a4..f28bc39789 100644 --- a/dockers/docker-dhcp-relay/dhcp-relay.programs.j2 +++ b/dockers/docker-dhcp-relay/dhcp-relay.programs.j2 @@ -1,17 +1,23 @@ [group:isc-dhcp-relay] programs= {%- set add_preceding_comma = { 'flag': False } %} -{% for vlan_name in VLAN_INTERFACE %} {# Append DHCPv4 agents #} +{% if ipv4_num_relays.count > 0 %} +{% for vlan_name in VLAN_INTERFACE %} {% if VLAN and vlan_name in VLAN and 'dhcp_servers' in VLAN[vlan_name] and VLAN[vlan_name]['dhcp_servers']|length > 0 %} {% if add_preceding_comma.flag %},{% endif %} {% set _dummy = add_preceding_comma.update({'flag': True}) %} isc-dhcpv4-relay-{{ vlan_name }} {%- endif %} -{# Append DHCPv6 agents #} +{% endfor %} +{% endif %} +{# Append DHCPv6 agent #} +{% if ipv6_num_relays.count > 0 %} +{% for vlan_name in VLAN_INTERFACE %} {% if VLAN and vlan_name in VLAN and 'dhcpv6_servers' in VLAN[vlan_name] and VLAN[vlan_name]['dhcpv6_servers']|length > 0 %} -{% if add_preceding_comma.flag %},{% endif %} -{% set _dummy = add_preceding_comma.update({'flag': True}) %} -isc-dhcpv6-relay-{{ vlan_name }} +{% set _dummy = v6_vlan_list.append( vlan_name ) %} {%- endif %} {% endfor %} +{% if add_preceding_comma.flag %},{% endif %} +isc-dhcpv6-relay-{{ v6_vlan_list|join("-") }} +{% endif %} \ No newline at end of file diff --git a/dockers/docker-dhcp-relay/dhcpv4-relay.agents.j2 b/dockers/docker-dhcp-relay/dhcpv4-relay.agents.j2 index bf756ff638..69478cae3a 100644 --- a/dockers/docker-dhcp-relay/dhcpv4-relay.agents.j2 +++ b/dockers/docker-dhcp-relay/dhcpv4-relay.agents.j2 @@ -1,4 +1,6 @@ {# Append DHCPv4 agents #} +{% set relay_for_ipv4 = { 'flag': False } %} +{% for vlan_name in VLAN_INTERFACE %} {% if VLAN and vlan_name in VLAN and 'dhcp_servers' in VLAN[vlan_name] and VLAN[vlan_name]['dhcp_servers']|length > 0 %} {% for dhcp_server in VLAN[vlan_name]['dhcp_servers'] %} {% if dhcp_server | ipv4 %} @@ -38,3 +40,4 @@ dependent_startup_wait_for=start:exited {% endif %} {% endif %} +{% endfor %} \ No newline at end of file diff --git a/dockers/docker-dhcp-relay/dhcpv6-relay.agents.j2 b/dockers/docker-dhcp-relay/dhcpv6-relay.agents.j2 index 2d95b383c0..114f8eb55c 100644 --- a/dockers/docker-dhcp-relay/dhcpv6-relay.agents.j2 +++ b/dockers/docker-dhcp-relay/dhcpv6-relay.agents.j2 @@ -1,20 +1,23 @@ -{# Append DHCPv6 agents #} -{% if VLAN and vlan_name in VLAN and 'dhcpv6_servers' in VLAN[vlan_name] and VLAN[vlan_name]['dhcpv6_servers']|length > 0 %} -{% for dhcpv6_server in VLAN[vlan_name]['dhcpv6_servers'] %} -{% if dhcpv6_server | ipv6 %} -{% set _dummy = relay_for_ipv6.update({'flag': True}) %} -{% endif %} -{% endfor %} -{% if relay_for_ipv6.flag %} -{% set _dummy = relay_for_ipv6.update({'flag': False}) %} -[program:isc-dhcpv6-relay-{{ vlan_name }}] -{# We treat this VLAN as a downstream interface (-l), as we only want to listen for requests #} -command=/usr/sbin/dhcrelay -d -6 --name-alias-map-file /tmp/port-name-alias-map.txt -l {{ vlan_name }} -{#- We treat all other interfaces as upstream interfaces (-u), as we only want to listen for replies #} +{# Append DHCPv6 agent #} +{% set relay_for_ipv6 = { 'flag': False } %} +[program:isc-dhcpv6-relay-{{ v6_vlan_list|join("-") }}] +command=/usr/sbin/dhcrelay -d -6 --name-alias-map-file /tmp/port-name-alias-map.txt +{#- We get all DHCPv6 servers only once #} +{%- set dhcpv6_server_list = [] %} +{%- for vlan_name in v6_vlan_list %} {%- for dhcpv6_server in VLAN[vlan_name]['dhcpv6_servers'] %} +{%- if dhcpv6_server | ipv6 and dhcpv6_server not in dhcpv6_server_list %} +{%- set _dummy = dhcpv6_server_list.append( dhcpv6_server ) %} +{%- endif %} +{%- endfor %} +{%- endfor %} +{#- We treat each VLAN as a downstream interface (-l), as we only want to listen for requests #} +{%- for vlan_name in v6_vlan_list %} -l {{ vlan_name }}{% endfor -%} +{#- We treat all other interfaces as upstream interfaces (-u), as we only want to listen for replies #} +{%- for dhcpv6_server in dhcpv6_server_list %} {%- if dhcpv6_server | ipv6 %} {%- for (name, prefix) in VLAN_INTERFACE|pfx_filter %} -{%- if prefix | ipv6 and name != vlan_name %} -u {{ dhcpv6_server }}%%{{ name }} {% endif -%} +{%- if prefix | ipv6 and name not in v6_vlan_list%} -u {{ dhcpv6_server }}%%{{ name }} {% endif -%} {% endfor %} {% for (name, prefix) in INTERFACE|pfx_filter %} {% if prefix | ipv6 %} -u {{ dhcpv6_server }}%%{{ name }} {% endif -%} @@ -22,7 +25,7 @@ command=/usr/sbin/dhcrelay -d -6 --name-alias-map-file /tmp/port-name-alias-map. {% for (name, prefix) in PORTCHANNEL_INTERFACE|pfx_filter %} {% if prefix | ipv6 %} -u {{ dhcpv6_server }}%%{{ name }} {% endif -%} {% endfor %} -{% endif -%} +{% endif %} {% endfor %} priority=3 @@ -33,5 +36,3 @@ stderr_logfile=syslog dependent_startup=true dependent_startup_wait_for=start:exited -{% endif %} -{% endif %} diff --git a/dockers/docker-dhcp-relay/dhcpv6-relay.monitors.j2 b/dockers/docker-dhcp-relay/dhcpv6-relay.monitors.j2 index 8bb9e5ad81..9e2e6d6650 100644 --- a/dockers/docker-dhcp-relay/dhcpv6-relay.monitors.j2 +++ b/dockers/docker-dhcp-relay/dhcpv6-relay.monitors.j2 @@ -70,7 +70,7 @@ stderr_logfile=syslog dependent_startup=true dependent_startup_wait_for= {%- if relay_for_ipv4.flag %}isc-dhcpv4-relay-{{ vlan_name }}:running {% endif %} -{% if relay_for_ipv6.flag %}isc-dhcpv6-relay-{{ vlan_name }}:running{% endif %} +{% if relay_for_ipv6.flag %}isc-dhcpv6-relay-{{ v6_vlan_list|join("-") }}:running{% endif %} {% set _dummy = relay_for_ipv4.update({'flag': False}) %} diff --git a/dockers/docker-dhcp-relay/docker-dhcp-relay.supervisord.conf.j2 b/dockers/docker-dhcp-relay/docker-dhcp-relay.supervisord.conf.j2 index 664e1f36c0..ad3aa34034 100644 --- a/dockers/docker-dhcp-relay/docker-dhcp-relay.supervisord.conf.j2 +++ b/dockers/docker-dhcp-relay/docker-dhcp-relay.supervisord.conf.j2 @@ -39,9 +39,11 @@ stderr_logfile=syslog dependent_startup=true dependent_startup_wait_for=rsyslogd:running +{% block dhcp_relay_block %} {# If our configuration has VLANs... #} {% if VLAN_INTERFACE %} {# Count how many VLANs require a DHCP relay agent... #} +{% set v6_vlan_list = [] %} {% set ipv4_num_relays = { 'count': 0 } %} {% set ipv6_num_relays = { 'count': 0 } %} {% for vlan_name in VLAN_INTERFACE %} @@ -56,15 +58,15 @@ dependent_startup_wait_for=rsyslogd:running {% if ipv4_num_relays.count > 0 or ipv6_num_relays.count > 0 %} {% include 'dhcp-relay.programs.j2' %} - {# Create a program entry for each DHCP relay agent instance #} -{% set relay_for_ipv4 = { 'flag': False } %} -{% set relay_for_ipv6 = { 'flag': False } %} -{% for vlan_name in VLAN_INTERFACE %} +{% if ipv4_num_relays.count > 0 %} {% include 'dhcpv4-relay.agents.j2' %} +{% endif %} +{% if ipv6_num_relays.count > 0 %} {% include 'dhcpv6-relay.agents.j2' %} -{% endfor %} +{% endif %} {% include 'dhcpv6-relay.monitors.j2' %} {% endif %} {% endif %} +{% endblock %} \ No newline at end of file diff --git a/src/sonic-config-engine/tests/sample_output/py2/docker-dhcp-relay.supervisord.conf b/src/sonic-config-engine/tests/sample_output/py2/docker-dhcp-relay.supervisord.conf index 35dfe5ea4b..09006e6bb5 100644 --- a/src/sonic-config-engine/tests/sample_output/py2/docker-dhcp-relay.supervisord.conf +++ b/src/sonic-config-engine/tests/sample_output/py2/docker-dhcp-relay.supervisord.conf @@ -62,7 +62,6 @@ stderr_logfile=syslog dependent_startup=true dependent_startup_wait_for=start:exited - [group:dhcpmon] programs=dhcpmon-Vlan1000 diff --git a/src/sonic-config-engine/tests/sample_output/py3/docker-dhcp-relay.supervisord.conf b/src/sonic-config-engine/tests/sample_output/py3/docker-dhcp-relay.supervisord.conf index bc24433a14..f7a644afa2 100644 --- a/src/sonic-config-engine/tests/sample_output/py3/docker-dhcp-relay.supervisord.conf +++ b/src/sonic-config-engine/tests/sample_output/py3/docker-dhcp-relay.supervisord.conf @@ -62,7 +62,6 @@ stderr_logfile=syslog dependent_startup=true dependent_startup_wait_for=start:exited - [group:dhcpmon] programs=dhcpmon-Vlan1000