From 95c7e7251d6e938691e2984bd70bcb80f590650d Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Tue, 21 Nov 2017 01:25:38 +0000 Subject: [PATCH 1/2] [DHCP relay]: Wait for all interfaces to be assigned IPv4 addresses before starting relay agent --- dockers/docker-dhcp-relay/wait_for_intf.sh.j2 | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/dockers/docker-dhcp-relay/wait_for_intf.sh.j2 b/dockers/docker-dhcp-relay/wait_for_intf.sh.j2 index 1524b322131..80e75fd037b 100644 --- a/dockers/docker-dhcp-relay/wait_for_intf.sh.j2 +++ b/dockers/docker-dhcp-relay/wait_for_intf.sh.j2 @@ -1,27 +1,42 @@ #!/usr/bin/env bash -function wait_until_iface_exists +function wait_until_iface_ready { IFACE=$1 - echo "Waiting for interface ${IFACE}..." + echo "Waiting until interface $IFACE is up..." # Wait for the interface to come up (i.e., 'ip link show' returns 0) - until ip link show $IFACE > /dev/null 2>&1; do + until ip link show dev $IFACE up > /dev/null 2>&1; do sleep 1 done - echo "Interface ${IFACE} is created" + echo "Interface $IFACE is up" + + echo "Waiting until interface $IFACE has an IPv4 address..." + + # Wait until the interface gets assigned an IPv4 address + while true; do + IP=$(ip -4 addr show dev $IFACE | grep "inet " | awk '{ print $2 }' | cut -d '/' -f1) + + if [ -n "$IP" ]; then + break + fi + + sleep 1 + done + + echo "Interface $IFACE is configured with IP $IP" } # Wait for all interfaces to come up before starting the DHCP relay {% for (name, prefix) in INTERFACE %} -wait_until_iface_exists {{ name }} +wait_until_iface_ready {{ name }} {% endfor %} {% for (name, prefix) in VLAN_INTERFACE %} -wait_until_iface_exists {{ name }} +wait_until_iface_ready {{ name }} {% endfor %} {% for (name, prefix) in PORTCHANNEL_INTERFACE %} -wait_until_iface_exists {{ name }} +wait_until_iface_ready {{ name }} {% endfor %} From d13e14b53d75d98def45c57deab94ad7fa533b21 Mon Sep 17 00:00:00 2001 From: Joe LeVeque Date: Tue, 21 Nov 2017 01:57:56 +0000 Subject: [PATCH 2/2] Add comments --- dockers/docker-dhcp-relay/start.sh | 7 ++++++- dockers/docker-dhcp-relay/wait_for_intf.sh.j2 | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/dockers/docker-dhcp-relay/start.sh b/dockers/docker-dhcp-relay/start.sh index b53d7e4e238..2ee80c4e338 100755 --- a/dockers/docker-dhcp-relay/start.sh +++ b/dockers/docker-dhcp-relay/start.sh @@ -6,7 +6,12 @@ rm -f /var/run/rsyslogd.pid # Start rsyslog supervisorctl start rsyslogd -# Wait for all interfaces to come up before starting the DHCP relay agent(s) +# Wait for all interfaces to come up and be assigned IPv4 addresses before +# starting the DHCP relay agent(s). If an interface the relay should listen +# on is down, the relay agent will not start. If an interface the relay should +# listen on is up but does not have an IP address assigned when the relay +# agent starts, it will not listen or send on that interface for the lifetime +# of the process. /usr/bin/wait_for_intf.sh # Start the DHCP relay agent(s) diff --git a/dockers/docker-dhcp-relay/wait_for_intf.sh.j2 b/dockers/docker-dhcp-relay/wait_for_intf.sh.j2 index 80e75fd037b..037dc66ead6 100644 --- a/dockers/docker-dhcp-relay/wait_for_intf.sh.j2 +++ b/dockers/docker-dhcp-relay/wait_for_intf.sh.j2 @@ -30,7 +30,7 @@ function wait_until_iface_ready } -# Wait for all interfaces to come up before starting the DHCP relay +# Wait for all interfaces to come up and have IPv4 addresses assigned {% for (name, prefix) in INTERFACE %} wait_until_iface_ready {{ name }} {% endfor %}