Skip to content

Commit

Permalink
Rework DHCP relay to support routed and/or unnumbered interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasstockner committed Aug 2, 2023
1 parent f6ae0f7 commit c087ff9
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 130 deletions.
27 changes: 10 additions & 17 deletions dockers/docker-dhcp-relay/dhcp-relay.programs.j2
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
[group:dhcp-relay]
programs=
{%- set relay_for_ipv6 = { 'flag': False } %}
{%- set add_preceding_comma = { 'flag': False } %}
{% for vlan_name in VLAN_INTERFACE %}
{# Append DHCPv4 agents #}
{% 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 %}
{% if DHCP_RELAY and vlan_name in DHCP_RELAY and DHCP_RELAY[vlan_name]['dhcpv6_servers']|length > 0 %}
{% set _dummy = relay_for_ipv6.update({'flag': True}) %}
{%- endif %}
{% endfor %}
{# Append DHCPv6 agents #}
{% if relay_for_ipv6.flag %}
{% if add_preceding_comma.flag %},{% endif %}
{% set _dummy = add_preceding_comma.update({'flag': True}) %}
{% for server in v4_relays %}
{% if add_preceding_comma.flag %},{% endif %}
{% set _dummy = add_preceding_comma.update({'flag': True}) %}
isc-dhcpv4-relay-{{ server | replace(".", "-") }}
{%- endfor %}
{# Append DHCPv6 agents #}
{% if ipv6_num_relays.count > 0 %}
{% if add_preceding_comma.flag %},{% endif %}
{% set _dummy = add_preceding_comma.update({'flag': True}) %}
dhcp6relay
{% endif %}
{% endif %}
38 changes: 7 additions & 31 deletions dockers/docker-dhcp-relay/dhcpv4-relay.agents.j2
Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
{# Append DHCPv4 agents #}
{% 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 %}
{% set _dummy = relay_for_ipv4.update({'flag': True}) %}
{% endif %}
[program:isc-dhcpv4-relay-{{ server | replace(".", "-") }}]
command=/usr/sbin/dhcrelay -d -m discard
{%- for iface in downstreams %} -i {{ iface }}{% endfor %}
{% for iface in VLAN_INTERFACE|default([])|list + INTERFACE|default([])|list + PORTCHANNEL_INTERFACE|default([])|list %}
{% if iface is string and iface not in downstreams %} -iu {{ iface }}{% endif %}
{% endfor %}
{% if relay_for_ipv4.flag %}
{% set _dummy = relay_for_ipv4.update({'flag': False}) %}
[program:isc-dhcpv4-relay-{{ vlan_name }}]
{# We treat this VLAN as a downstream interface (-id), as we only want to listen for requests #}
command=/usr/sbin/dhcrelay -d -m discard -a %%h:%%p %%P --name-alias-map-file /tmp/port-name-alias-map.txt -id {{ vlan_name }}
{#- Dual ToR Option #}
{% if 'subtype' in DEVICE_METADATA['localhost'] and DEVICE_METADATA['localhost']['subtype'] == 'DualToR' %} -U Loopback0 -dt{% endif -%}
{#- si option to use intf addr in relay #}
{% if DEVICE_METADATA['localhost']['deployment_id'] == '8' %} -si{% endif -%}
{#- We treat all other interfaces as upstream interfaces (-iu), as we only want to listen for replies #}
{% for (name, prefix) in VLAN_INTERFACE|pfx_filter %}
{% if prefix | ipv4 and name != vlan_name %} -iu {{ name }}{% endif -%}
{% endfor %}
{% for (name, prefix) in INTERFACE|pfx_filter %}
{% if prefix | ipv4 %} -iu {{ name }}{% endif -%}
{% endfor %}
{% for (name, prefix) in PORTCHANNEL_INTERFACE|pfx_filter %}
{% if prefix | ipv4 %} -iu {{ name }}{% endif -%}
{% endfor %}
{% for dhcp_server in VLAN[vlan_name]['dhcp_servers'] %}
{%- if dhcp_server | ipv4 %} {{ dhcp_server }}{% endif -%}
{% endfor %}

{{ server }}
priority=3
autostart=false
autorestart=false
Expand All @@ -36,5 +13,4 @@ stderr_logfile=syslog
dependent_startup=true
dependent_startup_wait_for=start:exited

{% endif %}
{% endif %}

61 changes: 41 additions & 20 deletions dockers/docker-dhcp-relay/docker-dhcp-relay.supervisord.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,52 @@ stderr_logfile=syslog
dependent_startup=true
dependent_startup_wait_for=rsyslogd:running

{# If our configuration has VLANs... #}
{% if VLAN_INTERFACE %}
{# Count how many VLANs require a DHCP relay agent... #}
{% set ipv4_num_relays = { 'count': 0 } %}
{% set v4_relays = {} %}
{% set ipv6_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 %}
{% set _dummy = ipv4_num_relays.update({'count': ipv4_num_relays.count + 1}) %}
{% endif %}
{% if DHCP_RELAY and vlan_name in DHCP_RELAY and DHCP_RELAY[vlan_name]['dhcpv6_servers']|length > 0 %}
{% set _dummy = ipv6_num_relays.update({'count': ipv6_num_relays.count + 1}) %}
{% if VLAN_INTERFACE %}
{% 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 %}
{% if dhcp_server in v4_relays %}
{% set _dummy = v4_relays[dhcp_server].append(vlan_name) %}
{% else %}
{% set _dummy = v4_relays.update({dhcp_server: [vlan_name]}) %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% if DHCP_RELAY and vlan_name in DHCP_RELAY and DHCP_RELAY[vlan_name]['dhcpv6_servers']|length > 0 %}
{% set _dummy = ipv6_num_relays.update({'count': ipv6_num_relays.count + 1}) %}
{% endif %}
{% endfor %}
{% endif %}

{% for iface_name, iface in (INTERFACE | default({})).items()|list + (PORTCHANNEL_INTERFACE | default({})).items()|list %}
{% if iface_name is string and 'dhcp_servers' in iface and iface['dhcp_servers']|length > 0 %}
{% for dhcp_server in iface['dhcp_servers'] %}
{% if dhcp_server | ipv4 %}
{% if dhcp_server in v4_relays %}
{% set _dummy = v4_relays[dhcp_server].append(iface_name) %}
{% else %}
{% set _dummy = v4_relays.update({dhcp_server: [iface_name]}) %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}


{# If one or more of the VLANs require a DHCP relay agent... #}
{% if ipv4_num_relays.count > 0 or ipv6_num_relays.count > 0 %}
{% include 'dhcp-relay.programs.j2' %}
{% if v4_relays|length > 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 %}
{% include 'dhcpv4-relay.agents.j2' %}
{% endfor %}
{# Create a program entry for each DHCP relay agent instance #}
{% set relay_for_ipv6 = { 'flag': False } %}
{% for server, downstreams in v4_relays.items() %}
{% include 'dhcpv4-relay.agents.j2' %}
{% endfor %}

{% include 'dhcpv6-relay.agents.j2' %}
{% endif %}
{% include 'dhcpv6-relay.agents.j2' %}
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ stderr_logfile=syslog
dependent_startup=true
dependent_startup_wait_for=rsyslogd:running




[group:dhcp-relay]
programs=isc-dhcpv4-relay-Vlan1000,dhcp6relay
programs=isc-dhcpv4-relay-192-0-0-2,isc-dhcpv4-relay-192-0-0-1,dhcp6relay


[program:isc-dhcpv4-relay-Vlan1000]
command=/usr/sbin/dhcrelay -d -m discard -a %%h:%%p %%P --name-alias-map-file /tmp/port-name-alias-map.txt -id Vlan1000 -iu Vlan2000 -iu PortChannel02 -iu PortChannel03 -iu PortChannel04 -iu PortChannel01 192.0.0.1 192.0.0.2
[program:isc-dhcpv4-relay-192-0-0-2]
command=/usr/sbin/dhcrelay -d -m discard -i Vlan1000 -iu Vlan2000 -iu PortChannel03 -iu PortChannel02 -iu PortChannel01 -iu PortChannel04 192.0.0.2
priority=3
autostart=false
autorestart=false
Expand All @@ -53,9 +56,8 @@ stderr_logfile=syslog
dependent_startup=true
dependent_startup_wait_for=start:exited


[program:dhcp6relay]
command=/usr/sbin/dhcp6relay
[program:isc-dhcpv4-relay-192-0-0-1]
command=/usr/sbin/dhcrelay -d -m discard -i Vlan1000 -iu Vlan2000 -iu PortChannel03 -iu PortChannel02 -iu PortChannel01 -iu PortChannel04 192.0.0.1
priority=3
autostart=false
autorestart=false
Expand All @@ -64,17 +66,15 @@ stderr_logfile=syslog
dependent_startup=true
dependent_startup_wait_for=start:exited

[group:dhcpmon]
programs=dhcpmon-Vlan1000

[program:dhcpmon-Vlan1000]
command=/usr/sbin/dhcpmon -id Vlan1000 -iu Vlan2000 -iu PortChannel02 -iu PortChannel03 -iu PortChannel04 -iu PortChannel01 -im eth0
priority=4
[program:dhcp6relay]
command=/usr/sbin/dhcp6relay
priority=3
autostart=false
autorestart=false
stdout_logfile=syslog
stderr_logfile=syslog
dependent_startup=true
dependent_startup_wait_for=isc-dhcpv4-relay-Vlan1000:running
dependent_startup_wait_for=start:exited


Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ stderr_logfile=syslog
dependent_startup=true
dependent_startup_wait_for=rsyslogd:running




[group:dhcp-relay]
programs=isc-dhcpv4-relay-Vlan1000,isc-dhcpv4-relay-Vlan2000,dhcp6relay
programs=isc-dhcpv4-relay-192-0-0-4,isc-dhcpv4-relay-192-0-0-3,isc-dhcpv4-relay-192-0-0-2,isc-dhcpv4-relay-192-0-0-1,dhcp6relay


[program:isc-dhcpv4-relay-Vlan1000]
command=/usr/sbin/dhcrelay -d -m discard -a %%h:%%p %%P --name-alias-map-file /tmp/port-name-alias-map.txt -id Vlan1000 -iu Vlan2000 -iu PortChannel02 -iu PortChannel03 -iu PortChannel04 -iu PortChannel01 192.0.0.1 192.0.0.2
[program:isc-dhcpv4-relay-192-0-0-4]
command=/usr/sbin/dhcrelay -d -m discard -i Vlan2000 -iu Vlan1000 -iu PortChannel03 -iu PortChannel02 -iu PortChannel01 -iu PortChannel04 192.0.0.4
priority=3
autostart=false
autorestart=false
Expand All @@ -53,8 +56,8 @@ stderr_logfile=syslog
dependent_startup=true
dependent_startup_wait_for=start:exited

[program:isc-dhcpv4-relay-Vlan2000]
command=/usr/sbin/dhcrelay -d -m discard -a %%h:%%p %%P --name-alias-map-file /tmp/port-name-alias-map.txt -id Vlan2000 -iu Vlan1000 -iu PortChannel02 -iu PortChannel03 -iu PortChannel04 -iu PortChannel01 192.0.0.3 192.0.0.4
[program:isc-dhcpv4-relay-192-0-0-3]
command=/usr/sbin/dhcrelay -d -m discard -i Vlan2000 -iu Vlan1000 -iu PortChannel03 -iu PortChannel02 -iu PortChannel01 -iu PortChannel04 192.0.0.3
priority=3
autostart=false
autorestart=false
Expand All @@ -63,9 +66,8 @@ stderr_logfile=syslog
dependent_startup=true
dependent_startup_wait_for=start:exited


[program:dhcp6relay]
command=/usr/sbin/dhcp6relay
[program:isc-dhcpv4-relay-192-0-0-2]
command=/usr/sbin/dhcrelay -d -m discard -i Vlan1000 -iu Vlan2000 -iu PortChannel03 -iu PortChannel02 -iu PortChannel01 -iu PortChannel04 192.0.0.2
priority=3
autostart=false
autorestart=false
Expand All @@ -74,27 +76,25 @@ stderr_logfile=syslog
dependent_startup=true
dependent_startup_wait_for=start:exited

[group:dhcpmon]
programs=dhcpmon-Vlan1000,dhcpmon-Vlan2000

[program:dhcpmon-Vlan1000]
command=/usr/sbin/dhcpmon -id Vlan1000 -iu Vlan2000 -iu PortChannel02 -iu PortChannel03 -iu PortChannel04 -iu PortChannel01 -im eth0
priority=4
[program:isc-dhcpv4-relay-192-0-0-1]
command=/usr/sbin/dhcrelay -d -m discard -i Vlan1000 -iu Vlan2000 -iu PortChannel03 -iu PortChannel02 -iu PortChannel01 -iu PortChannel04 192.0.0.1
priority=3
autostart=false
autorestart=false
stdout_logfile=syslog
stderr_logfile=syslog
dependent_startup=true
dependent_startup_wait_for=isc-dhcpv4-relay-Vlan1000:running
dependent_startup_wait_for=start:exited

[program:dhcpmon-Vlan2000]
command=/usr/sbin/dhcpmon -id Vlan2000 -iu Vlan1000 -iu PortChannel02 -iu PortChannel03 -iu PortChannel04 -iu PortChannel01 -im eth0
priority=4

[program:dhcp6relay]
command=/usr/sbin/dhcp6relay
priority=3
autostart=false
autorestart=false
stdout_logfile=syslog
stderr_logfile=syslog
dependent_startup=true
dependent_startup_wait_for=isc-dhcpv4-relay-Vlan2000:running
dependent_startup_wait_for=start:exited


Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ stderr_logfile=syslog
dependent_startup=true
dependent_startup_wait_for=rsyslogd:running




[group:dhcp-relay]
programs=isc-dhcpv4-relay-Vlan1000,dhcp6relay
programs=isc-dhcpv4-relay-192-0-0-1,isc-dhcpv4-relay-192-0-0-2,dhcp6relay


[program:isc-dhcpv4-relay-Vlan1000]
command=/usr/sbin/dhcrelay -d -m discard -a %%h:%%p %%P --name-alias-map-file /tmp/port-name-alias-map.txt -id Vlan1000 -iu Vlan2000 -iu PortChannel01 -iu PortChannel02 -iu PortChannel03 -iu PortChannel04 192.0.0.1 192.0.0.2
[program:isc-dhcpv4-relay-192-0-0-1]
command=/usr/sbin/dhcrelay -d -m discard -i Vlan1000 -iu Vlan2000 -iu PortChannel01 -iu PortChannel02 -iu PortChannel03 -iu PortChannel04 192.0.0.1
priority=3
autostart=false
autorestart=false
Expand All @@ -53,9 +56,8 @@ stderr_logfile=syslog
dependent_startup=true
dependent_startup_wait_for=start:exited


[program:dhcp6relay]
command=/usr/sbin/dhcp6relay
[program:isc-dhcpv4-relay-192-0-0-2]
command=/usr/sbin/dhcrelay -d -m discard -i Vlan1000 -iu Vlan2000 -iu PortChannel01 -iu PortChannel02 -iu PortChannel03 -iu PortChannel04 192.0.0.2
priority=3
autostart=false
autorestart=false
Expand All @@ -64,17 +66,15 @@ stderr_logfile=syslog
dependent_startup=true
dependent_startup_wait_for=start:exited

[group:dhcpmon]
programs=dhcpmon-Vlan1000

[program:dhcpmon-Vlan1000]
command=/usr/sbin/dhcpmon -id Vlan1000 -iu Vlan2000 -iu PortChannel01 -iu PortChannel02 -iu PortChannel03 -iu PortChannel04 -im eth0
priority=4
[program:dhcp6relay]
command=/usr/sbin/dhcp6relay
priority=3
autostart=false
autorestart=false
stdout_logfile=syslog
stderr_logfile=syslog
dependent_startup=true
dependent_startup_wait_for=isc-dhcpv4-relay-Vlan1000:running
dependent_startup_wait_for=start:exited


Loading

0 comments on commit c087ff9

Please sign in to comment.