meta-google: Refactor networkd reloads to one place

We want to have common logging and conditional routines when we talk to
networkd.

Change-Id: I44c83c8137399d2126c565a1f753520603a9f97b
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/meta-google/recipes-google/ncsi/files/gbmc-ncsi-br-deprecated-ips.sh.in b/meta-google/recipes-google/ncsi/files/gbmc-ncsi-br-deprecated-ips.sh.in
index 93c6b27..c2783be 100644
--- a/meta-google/recipes-google/ncsi/files/gbmc-ncsi-br-deprecated-ips.sh.in
+++ b/meta-google/recipes-google/ncsi/files/gbmc-ncsi-br-deprecated-ips.sh.in
@@ -15,6 +15,7 @@
 [ -n "${gbmc_ncsi_br_deprecated_ips_lib-}" ] && return
 
 source /usr/share/network/lib.sh || exit
+source /usr/share/gbmc-net-lib.sh || exit
 
 gbmc_ncsi_br_deprecated_ips_init=
 gbmc_ncsi_br_deprecated_ips_confip=
@@ -115,9 +116,7 @@
       lookup $GBMC_NCSI_ROUTE_TABLE || st=$?
   fi
 
-  if (( st != 0 )); then
-    networkctl reload && networkctl reconfigure @NCSI_IF@
-  fi
+  (( st == 0 )) || gbmc_net_networkd_reload @NCSI_IF@
 
   local rfile=/run/nftables/40-gbmc-ncsi-br.rules
   mkdir -p -m 755 "$(dirname "$rfile")"
diff --git a/meta-google/recipes-google/ncsi/files/gbmc-ncsi-ra.sh b/meta-google/recipes-google/ncsi/files/gbmc-ncsi-ra.sh
index 34c59ab..ca8fa05 100644
--- a/meta-google/recipes-google/ncsi/files/gbmc-ncsi-ra.sh
+++ b/meta-google/recipes-google/ncsi/files/gbmc-ncsi-ra.sh
@@ -13,6 +13,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+# shellcheck source=meta-google/recipes-google/networking/gbmc-net-common/gbmc-net-lib.sh
+source /usr/share/gbmc-net-lib.sh || exit
+
 RA_IF=$1
 IP_OFFSET=1
 # NCSI is known to be closer to the ToR than bridge routes. Prefer over bridge routes.
@@ -33,7 +36,7 @@
   done
 
   ip -6 route replace default via "$rtr" onlink dev "$RA_IF" table "$ROUTE_TABLE" || \
-    networkctl reload && networkctl reconfigure "$RA_IF"
+    gbmc_net_networkd_reload "$RA_IF"
 }
 
 ncsi_is_active() {
@@ -78,7 +81,7 @@
     ip -6 addr del "$old_ncsi_pfx/128" dev gbmcbr || true
   fi
   ip -6 addr replace "$pfx/128" dev gbmcbr || \
-    (networkctl reload && networkctl reconfigure gbmcbr) || true
+    gbmc_net_networkd_reload gbmcbr || true
   old_ncsi_pfx=$pfx
 
   echo "Set NCSI addr $pfx on gbmcbr" >&2
diff --git a/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-gw-src.sh b/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-gw-src.sh
index 56dd840..5865946 100644
--- a/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-gw-src.sh
+++ b/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-gw-src.sh
@@ -17,6 +17,8 @@
 
 # shellcheck source=meta-google/recipes-google/networking/network-sh/lib.sh
 source /usr/share/network/lib.sh || exit
+# shellcheck source=meta-google/recipes-google/networking/gbmc-net-common/gbmc-net-lib.sh
+source /usr/share/gbmc-net-lib.sh || exit
 
 declare -A gbmc_br_gw_src_ips=()
 declare -A gbmc_br_gw_src_routes=()
@@ -50,9 +52,8 @@
     rm -f "${files[@]}"
   fi
 
-  if [[ $(systemctl is-active systemd-networkd) != inactive ]]; then
-    networkctl reload && networkctl reconfigure gbmcbr
-  fi
+  # shellcheck disable=SC2119
+  gbmc_net_networkd_reload
 }
 
 gbmc_br_gw_src_update() {
diff --git a/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-hostname.sh b/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-hostname.sh
index 408ffb6..d680e14 100755
--- a/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-hostname.sh
+++ b/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-hostname.sh
@@ -12,7 +12,10 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-#
+
+# shellcheck source=meta-google/recipes-google/networking/gbmc-net-common/gbmc-net-lib.sh
+source /usr/share/gbmc-net-lib.sh || exit
+
 oldname=
 while read -r _; do
   # Don't bother parsing the output, just read the final hostname
@@ -27,5 +30,6 @@
     printf '%s' "$contents" >"$netfile"
    done
 
-  networkctl reload && networkctl reconfigure gbmcbr
+  # shellcheck disable=SC2119
+  gbmc_net_networkd_reload
 done < <(dbus-monitor --system "type='signal',interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',arg0='org.freedesktop.hostname1'")
diff --git a/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-lib.sh b/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-lib.sh
index 31596ca..912a3c1 100644
--- a/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-lib.sh
+++ b/meta-google/recipes-google/networking/gbmc-bridge/gbmc-br-lib.sh
@@ -15,9 +15,10 @@
 
 [ -n "${gbmc_br_lib_init-}" ] && return
 
-# SC can't find this path during repotest
-# shellcheck disable=SC1091
+# shellcheck source=meta-google/recipes-google/networking/network-sh/lib.sh
 source /usr/share/network/lib.sh || exit
+# shellcheck source=meta-google/recipes-google/networking/gbmc-net-common/gbmc-net-lib.sh
+source /usr/share/gbmc-net-lib.sh || exit
 
 # A list of functions which get executed for each configured IP.
 # These are configured by the files included below.
@@ -49,16 +50,10 @@
   done
 }
 
-gbmc_br_reload() {
-  if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
-    networkctl reload && networkctl reconfigure gbmcbr
-  fi
-}
-
 gbmc_br_no_ip() {
   echo "Runtime removing gbmcbr IP" >&2
   rm -f /run/systemd/network/{00,}-bmc-gbmcbr.network.d/50-public.conf
-  gbmc_br_reload
+  gbmc_net_networkd_reload gbmcbr
 }
 
 gbmc_br_reload_ip() {
@@ -109,7 +104,7 @@
     printf '%s' "$contents" >"$file"
   done
 
-  gbmc_br_reload
+  gbmc_net_networkd_reload gbmcbr
 }
 
 gbmc_br_set_ip() {
diff --git a/meta-google/recipes-google/networking/gbmc-net-common.bb b/meta-google/recipes-google/networking/gbmc-net-common.bb
index 1f709b4..1a3ca77 100644
--- a/meta-google/recipes-google/networking/gbmc-net-common.bb
+++ b/meta-google/recipes-google/networking/gbmc-net-common.bb
@@ -4,6 +4,7 @@
 LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
 
 SRC_URI += " \
+  file://gbmc-net-lib.sh \
   file://gbmc-ra.sh \
   "
 S = "${WORKDIR}"
@@ -20,6 +21,7 @@
 
 do_install() {
   install -d -m0755 ${D}${datadir}
+  install -m0644 ${S}/gbmc-net-lib.sh ${D}${datadir}/
   install -m0644 ${S}/gbmc-ra.sh ${D}${datadir}/
 }
 
diff --git a/meta-google/recipes-google/networking/gbmc-net-common/gbmc-net-lib.sh b/meta-google/recipes-google/networking/gbmc-net-common/gbmc-net-lib.sh
new file mode 100644
index 0000000..6e4ba36
--- /dev/null
+++ b/meta-google/recipes-google/networking/gbmc-net-common/gbmc-net-lib.sh
@@ -0,0 +1,31 @@
+#!/bin/bash
+# Copyright 2024 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+[ -n "${gbmc_net_lib_init-}" ] && return
+
+gbmc_net_networkd_reload() {
+  if [ "$(systemctl is-active systemd-networkd)" != 'inactive' ]; then
+    echo "Reloading networkd + reconfiguring ($*) from $(caller 0)" >&2
+    networkctl reload || return
+    local st=0
+    local intf
+    for intf in "$@"; do
+      networkctl reconfigure "$intf" || st=$?
+    done
+    return $st
+  fi
+}
+
+gbmc_net_lib_init=1
diff --git a/meta-google/recipes-google/networking/gbmc-net-common/gbmc-ra.sh b/meta-google/recipes-google/networking/gbmc-net-common/gbmc-ra.sh
index d2e92a5..280083a 100644
--- a/meta-google/recipes-google/networking/gbmc-net-common/gbmc-ra.sh
+++ b/meta-google/recipes-google/networking/gbmc-net-common/gbmc-ra.sh
@@ -15,6 +15,8 @@
 
 # shellcheck source=meta-google/recipes-google/networking/network-sh/lib.sh
 source /usr/share/network/lib.sh || exit
+# shellcheck source=meta-google/recipes-google/networking/gbmc-net-common/gbmc-net-lib.sh
+source /usr/share/gbmc-net-lib.sh || exit
 
 : "${RA_IF:?No RA interface set}"
 : "${IP_OFFSET=?1}"
@@ -55,7 +57,7 @@
   # Fall back to reload only if ip link commands fail
   (ip -6 route replace default via "$rtr" onlink dev "$RA_IF" metric "$ROUTE_METRIC" && \
     ip -6 neigh replace "$rtr" dev "$RA_IF" lladdr "$mac") || \
-    (networkctl reload && networkctl reconfigure "$RA_IF") || true
+    gbmc_net_networkd_reload "$RA_IF" || true
 
   echo "Set router $rtr on $RA_IF" >&2
 }
diff --git a/meta-google/recipes-google/networking/gbmc-nic-config/gbmc-nic-neigh.sh.in b/meta-google/recipes-google/networking/gbmc-nic-config/gbmc-nic-neigh.sh.in
index 5f3f47f..d7c0f02 100644
--- a/meta-google/recipes-google/networking/gbmc-nic-config/gbmc-nic-neigh.sh.in
+++ b/meta-google/recipes-google/networking/gbmc-nic-config/gbmc-nic-neigh.sh.in
@@ -17,6 +17,8 @@
 
 # shellcheck source=meta-google/recipes-google/networking/network-sh/lib.sh
 source /usr/share/network/lib.sh || exit
+# shellcheck source=meta-google/recipes-google/networking/gbmc-net-common/gbmc-net-lib.sh
+source /usr/share/gbmc-net-lib.sh || exit
 
 gbmc_nic_neigh_intfs=(@IFS@)
 gbmc_nic_neigh_addr=
@@ -55,11 +57,8 @@
   done
   [[ "$act" == del ]] && return
   if (( "${#failed_intfs[@]}" > 0 )); then
-    networkctl reload || true
+    gbmc_net_networkd_reload "${failed_intfs[@]}"
   fi
-  for intf in "${failed_intfs[@]}"; do
-    networkctl reconfigure "$intf" || true
-  done
 }
 
 gbmc_nic_neigh_hook() {