diff options
author | Jason M. Bills <jason.m.bills@linux.intel.com> | 2021-05-24 22:35:24 +0300 |
---|---|---|
committer | Jason M. Bills <jason.m.bills@linux.intel.com> | 2021-05-24 22:35:24 +0300 |
commit | 0e0df451ae365f09d5c0c766b253f23de26901f2 (patch) | |
tree | db4d7d3ce85e02ee01ad58a86ede02ac876aae77 /meta-google/recipes-google/networking/files/gbmc-ip-monitor.sh | |
parent | e370fd750e2821620ec427f26f8efab0069824ff (diff) | |
parent | 7e10dee74964afa47859704886128dd256acf854 (diff) | |
download | openbmc-0e0df451ae365f09d5c0c766b253f23de26901f2.tar.xz |
Merge tag '0.52' of ssh://git-amr-1.devtools.intel.com:29418/openbmc-openbmc into update
Diffstat (limited to 'meta-google/recipes-google/networking/files/gbmc-ip-monitor.sh')
-rwxr-xr-x | meta-google/recipes-google/networking/files/gbmc-ip-monitor.sh | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/meta-google/recipes-google/networking/files/gbmc-ip-monitor.sh b/meta-google/recipes-google/networking/files/gbmc-ip-monitor.sh new file mode 100755 index 000000000..baeff9a85 --- /dev/null +++ b/meta-google/recipes-google/networking/files/gbmc-ip-monitor.sh @@ -0,0 +1,122 @@ +#!/bin/bash +# Copyright 2021 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. + +# A list of functions which get executed for each netlink event received. +# These are configured by the files included below. +GBMC_IP_MONITOR_HOOKS=() + +# Load configurations from a known location in the filesystem to populate +# hooks that are executed after each event. +shopt -s nullglob +for conf in /usr/share/gbmc-ip-monitor/*.sh; do + source "$conf" +done + +gbmc_ip_monitor_run_hooks() { + local hook + for hook in "${GBMC_IP_MONITOR_HOOKS[@]}"; do + "$hook" || continue + done +} + +gbmc_ip_monitor_generate_init() { + ip link | sed 's,^[^ ],[LINK]\0,' + local intf= + local line + while read line; do + [[ "$line" =~ ^([0-9]+:[[:space:]][^:]+) ]] && intf="${BASH_REMATCH[1]}" + [[ "$line" =~ ^[[:space:]]*inet ]] && echo "[ADDR]$intf $line" + done < <(ip addr) + ip -4 route | sed 's,^,[ROUTE],' + ip -6 route | sed 's,^,[ROUTE],' + echo '[INIT]' +} + +gbmc_ip_monitor_parse_line() { + local line="$1" + if [[ "$line" == '[INIT]'* ]]; then + change=init + echo "Initialized" >&2 + elif [[ "$line" == '[ADDR]'* ]]; then + change=addr + action=add + pfx_re='^\[ADDR\](Deleted )?[0-9]+:[[:space:]]*' + intf_re='([^ ]+)[[:space:]]+' + fam_re='([^ ]+)[[:space:]]+' + addr_re='([^/]+)/[0-9]+[[:space:]]+(brd[[:space:]]+[^ ]+[[:space:]]+)?' + scope_re='scope[[:space:]]+([^ ]+)[[:space:]]*(.*)' + combined_re="${pfx_re}${intf_re}${fam_re}${addr_re}${scope_re}" + if ! [[ "$line" =~ ${combined_re} ]]; then + echo "Failed to parse addr: $line" >&2 + return 1 + fi + if [ -n "${BASH_REMATCH[1]}" ]; then + action=del + fi + intf="${BASH_REMATCH[2]}" + fam="${BASH_REMATCH[3]}" + ip="${BASH_REMATCH[4]}" + scope="${BASH_REMATCH[6]}" + flags="${BASH_REMATCH[7]}" + elif [[ "$line" == '[ROUTE]'* ]]; then + line="${line#[ROUTE]}" + change=route + action=add + if ! [[ "$line" =~ ^\[ROUTE\](Deleted )?(.*)$ ]]; then + echo "Failed to parse link: $line" >&2 + return 1 + fi + if [ -n "${BASH_REMATCH[1]}" ]; then + action=del + fi + route="${BASH_REMATCH[2]}" + elif [[ "$line" == '[LINK]'* ]]; then + change=link + action=add + pfx_re='^\[LINK\](Deleted )?[0-9]+:[[:space:]]*' + intf_re='([^:]+):[[:space:]]+' + if ! [[ "$line" =~ ${pfx_re}${intf_re} ]]; then + echo "Failed to parse link: $line" >&2 + return 1 + fi + if [ -n "${BASH_REMATCH[1]}" ]; then + action=del + fi + intf="${BASH_REMATCH[2]}" + read line || break + data=($line) + mac="${data[1]}" + else + return 2 + fi +} + +cleanup() { + local st="$?" + trap - HUP INT QUIT ABRT TERM EXIT + jobs -l -p | xargs -r kill || true + exit $st +} +trap cleanup HUP INT QUIT ABRT TERM EXIT + +return 0 2>/dev/null + +while read line; do + gbmc_ip_monitor_parse_line || continue + gbmc_ip_monitor_run_hooks || continue + if [ "$change" = 'init' ]; then + systemd-notify --ready + fi +done < <(gbmc_ip_monitor_generate_init; exec ip monitor link addr route label) |