ncsid: Fix update_ra_gw for multi-RA
Change-Id: I2a3e796b39fe3dd4a3de0b347277d846896c010a
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/subprojects/ncsid/src/update_ra_gw.sh b/subprojects/ncsid/src/update_ra_gw.sh
index 58fdc52..47e7f27 100644
--- a/subprojects/ncsid/src/update_ra_gw.sh
+++ b/subprojects/ncsid/src/update_ra_gw.sh
@@ -15,45 +15,55 @@
source "$(dirname "${BASH_SOURCE[0]}")"/ncsid_lib.sh
-UpdateRAGW() {
- local netdev="$1"
+NCSI_IF="$1"
- local reqs=5
- while ! ShouldTerm; do
- local st=0
- local disc
- disc="$(RunInterruptible DiscoverRouter6 "$1" "$reqs" 360000)" || st=$?
- if (( st != 0 )); then
- echo "Failed to discover router: $st" >&2
- continue
+old_rtr=
+old_mac=
+
+set_rtr() {
+ [ -n "$rtr" -a -n "$lifetime" ] || return
+ [ "$rtr" != "$old_rtr" -a "$mac" != "$old_mac" ] || return
+ # Only valid default routers can be considered, 0 lifetime implies
+ # a non-default router
+ (( lifetime > 0 )) || return
+
+ echo "Setting default router: $rtr at $mac" >&2
+ UpdateGateway xyz.openbmc_project.Network "$rtr" || return
+ UpdateNeighbor xyz.openbmc_project.Network "$NCSI_IF" "$rtr" "$mac" || return
+
+ retries=-1
+ old_mac="$mac"
+ old_rtr="$rtr"
+}
+
+retries=1
+w=60
+while true; do
+ start=$SECONDS
+ args=(-m "$NCSI_IF" -w $(( w * 1000 )))
+ if (( retries > 0 )); then
+ args+=(-r "$retries")
+ else
+ args+=(-d)
+ fi
+ while read line; do
+ if [ -z "$line" ]; then
+ lifetime=
+ mac=
+ elif [[ "$line" =~ ^Router' 'lifetime' '*:' '*([0-9]*) ]]; then
+ lifetime="${BASH_REMATCH[1]}"
+ elif [[ "$line" =~ ^Source' 'link-layer' 'address' '*:' '*([a-fA-F0-9:]*)$ ]]; then
+ mac="${BASH_REMATCH[1]}"
+ elif [[ "$line" =~ ^from' '(.*)$ ]]; then
+ rtr="${BASH_REMATCH[1]}"
+ set_rtr || true
+ lifetime=
+ mac=
+ rtr=
fi
- # We don't want to send any requests after the initial finding
- # Just passively listen now
- reqs=-1
-
- local vars
- vars="$(echo "$disc" | JSONToVars)" || return
- eval "$vars" || return
- [ -n "$stateful_address" ] || continue
- echo "GW($netdev) $router_ip MAC: $router_mac" >&2
-
- SuppressTerm
- local service='xyz.openbmc_project.Network'
- local rc=0
- UpdateGateway "$service" "$router_ip" && \
- UpdateNeighbor "$service" "$netdev" "$router_ip" "$router_mac" || rc=$?
- UnsuppressTerm
- done
-}
-
-Main() {
- set -o nounset
- set -o errexit
- set -o pipefail
-
- InitTerm
- UpdateRAGW "$@"
-}
-
-return 0 2>/dev/null
-Main "$@"
+ done < <(exec rdisc6 "${args[@]}" 2>/dev/null)
+ # If rdisc6 exits early we still want to wait the full `w` time before
+ # starting again.
+ (( timeout = start + w - SECONDS ))
+ sleep $(( timeout < 0 ? 0 : timeout ))
+done