Skip to content

Commit

Permalink
[dhcp-relay] Reduce Calls to SONiC Cfggen (#5175)
Browse files Browse the repository at this point in the history
Calls to sonic-cfggen is CPU expensive. This PR reduces calls to
sonic-cfggen to one call during startup when starting dhcp-relay
service.

singed-off-by: Tamer Ahmed <tamer.ahmed@microsoft.com>
  • Loading branch information
tahmed-dev authored and abdosi committed Oct 7, 2020
1 parent df2794c commit f9c1df7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dockers/docker-dhcp-relay/Dockerfile.j2
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ RUN apt-get clean -y && \
rm -rf /debs

COPY ["docker_init.sh", "start.sh", "/usr/bin/"]
COPY ["docker-dhcp-relay.supervisord.conf.j2", "wait_for_intf.sh.j2", "/usr/share/sonic/templates/"]
COPY ["docker-dhcp-relay.supervisord.conf.j2", "port-name-alias-map.txt.j2", "wait_for_intf.sh.j2", "/usr/share/sonic/templates/"]
COPY ["files/supervisor-proc-exit-listener", "/usr/bin"]
COPY ["critical_processes", "/etc/supervisor"]

Expand Down
20 changes: 13 additions & 7 deletions dockers/docker-dhcp-relay/docker_init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@

# Generate supervisord config file
mkdir -p /etc/supervisor/conf.d/
sonic-cfggen -d -t /usr/share/sonic/templates/docker-dhcp-relay.supervisord.conf.j2 > /etc/supervisor/conf.d/docker-dhcp-relay.supervisord.conf

# Generate the script that waits for all interfaces to come up and make it executable
sonic-cfggen -d -t /usr/share/sonic/templates/wait_for_intf.sh.j2 > /usr/bin/wait_for_intf.sh
chmod +x /usr/bin/wait_for_intf.sh
# Generate the following files from templates:
# 1. supervisord configuration
# 2. wait_for_intf.sh, which waits for all interfaces to come up
# 3. port-to-alias name map
CFGGEN_PARAMS=" \
-d \
-t /usr/share/sonic/templates/docker-dhcp-relay.supervisord.conf.j2,/etc/supervisor/conf.d/docker-dhcp-relay.supervisord.conf \
-t /usr/share/sonic/templates/wait_for_intf.sh.j2,/usr/bin/wait_for_intf.sh \
-t /usr/share/sonic/templates/port-name-alias-map.txt.j2,/tmp/port-name-alias-map.txt \
"
sonic-cfggen $CFGGEN_PARAMS

# Generate port name-alias map for isc-dhcp-relay to parse. Each line contains one
# name-alias pair of the form "<name> <alias>"
sonic-cfggen -d --var-json "PORT" | python -c "import sys, json, os; [sys.stdout.write('%s %s\n' % (k, v['alias'] if 'alias' in v else k)) for (k, v) in json.load(sys.stdin).iteritems()]" > /tmp/port-name-alias-map.txt
# Make the script that waits for all interfaces to come up executable
chmod +x /usr/bin/wait_for_intf.sh

# The docker container should start this script as PID 1, so now that supervisord is
# properly configured, we exec supervisord so that it runs as PID 1 for the
Expand Down
5 changes: 5 additions & 0 deletions dockers/docker-dhcp-relay/port-name-alias-map.txt.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{# Generate port name-alias map for isc-dhcp-relay to parse. Each line contains one #}
{# name-alias pair of the form "<name> <alias>" #}
{% for port, config in PORT.items() %}
{{- port }} {% if "alias" in config %}{{ config["alias"] }}{% else %}{{ port }}{% endif %} {{- "\n" -}}
{% endfor -%}

0 comments on commit f9c1df7

Please sign in to comment.