| #!/bin/sh |
| # Controls configuration knobs for ptp devices. |
| |
| . /etc/utils.sh |
| |
| CONFIG_DIR="/config/settings" |
| |
| usage() { |
| echo "Usage:" |
| echo |
| echo " -h This help screen" |
| echo " -g config_option Get config_option" |
| echo " -i config_option Do any initialization for config_option" |
| echo " -s config_option value Set config_option to value" |
| echo " -d config_option value Set default (set if not set)" |
| echo " -c config_option Clear config_option" |
| echo |
| echo "Some example invocations:" |
| echo |
| echo " Set the craft port ip address to 192.168.1.1 on subnet 255.255.255.0:" |
| echo " $0 -s craft_ipaddr 192.168.1.1/24" |
| echo |
| echo " Get the craft port ip address:" |
| echo " $0 -g craft_ipaddr" |
| echo " 192.168.1.1/24" |
| echo |
| echo " Initialize the craft port with the provided ip address:" |
| echo " $0 -i craft_ipaddr" |
| echo |
| echo " Clear the craft_ipaddr config setting:" |
| echo " $0 -c craft_ipaddr" |
| echo |
| exit 99 |
| } |
| |
| die() { |
| echo "Fatal: $@" >&2 |
| exit 1 |
| } |
| |
| do_get() { |
| local config_file="$CONFIG_DIR/$1" |
| [ -e "$config_file" ] || exit 2 |
| read opt <"$config_file" |
| [ -n "$opt" ] || exit 2 |
| echo "$opt" |
| } |
| |
| do_vlan() { |
| local file="$CONFIG_DIR/$1" if="$2" vname="$3" |
| local vif="$if.$vname" vlan |
| [ -r "$file" ] || die "Failed to initialize $1: Cannot open $file" |
| read vlan <"$file" |
| [ -n "$vlan" ] || die "Cannot init $1: Need to set a value first." |
| |
| # clear existing |
| if [ -e "/sys/class/net/$vif" ]; then |
| retval=$(ip link delete "$vif" 2>&1) |
| [ "$?" -eq "0" ] || die "Failed to delete $vif: $retval." |
| fi |
| |
| # add vlan interface, bring up |
| retval=$(ip link add link $if name $vif type vlan id $vlan 2>&1) |
| [ "$?" -eq "0" ] || die "Failed to add $vif: $retval." |
| retval=$(ifup $vif 2>&1) |
| [ "$?" -eq "0" ] || die "Failed to ifup $vif: $retval." |
| |
| # restart cpss if running to pick up new vlans |
| pgrep -x cpss && restart cpss |
| } |
| |
| do_ipaddr() { |
| local file="$CONFIG_DIR/$1" if="$2" |
| local ipaddr |
| [ -r "$file" ] || die "Failed to initialize $1: Cannot open $file" |
| read ipaddr <"$file" |
| [ -n "$ipaddr" ] || die "Cannot init $1: Need to set a value first." |
| |
| # Clear any existing values on this interface so we don't get an error. |
| retval=$(ip addr flush dev $if 2>&1) |
| [ "$?" -eq "0" ] || die "Failed to flush $if: $retval." |
| |
| # Set the new ip address from the config file. |
| retval=$(ip addr add $ipaddr dev $if 2>&1) |
| [ "$?" -eq "0" ] || die "Failed to set $ipaddr on $if: $retval" |
| } |
| |
| do_init() { |
| case "$1" in |
| craft_ipaddr) |
| do_ipaddr "$1" craft0 |
| ;; |
| local_ipaddr) |
| do_ipaddr "$1" sw0.peer |
| ;; |
| vlan_inband) |
| do_vlan "$1" sw0 inband |
| ;; |
| vlan_ooband) |
| do_vlan "$1" sw0 ooband |
| ;; |
| vlan_peer) |
| do_vlan "$1" sw0 peer |
| ;; |
| *) |
| echo "Nothing to initialize for $1" && exit 2 |
| ;; |
| esac |
| } |
| |
| do_set() { |
| local file="$CONFIG_DIR/$1" |
| local value="$2" |
| if [ -z "$file" ] || [ -z "$value" ]; then |
| die "Missing 'value' for set command." |
| fi |
| atomic $file $value || die "Failed to write config value to $file: $retval" |
| } |
| |
| do_set_if_not_set() { |
| local file="$CONFIG_DIR/$1" |
| if [ ! -f "$file" ]; then |
| do_set "$1" "$2" |
| fi |
| } |
| |
| do_clear() { |
| rm -f "$CONFIG_DIR/$1" |
| } |
| |
| if [ "$#" -eq "0" ]; then |
| usage |
| fi |
| |
| # If the config settings directory structure does not exist, we need to try to |
| # create it. |
| if [ ! -d "$CONFIG_DIR" ]; then |
| mkdir -p "$CONFIG_DIR" |
| fi |
| |
| while getopts ":cg:hi:s:d:" option |
| do |
| case "$option" in |
| g) |
| option_found=1 && do_get "$2" |
| ;; |
| i) |
| option_found=1 && do_init "$2" |
| ;; |
| s) |
| option_found=1 && do_set "$2" "$3" |
| ;; |
| d) |
| option_found=1 && do_set_if_not_set "$2" "$3" |
| ;; |
| c) |
| option_found=1 && do_clear "$2" |
| ;; |
| h) |
| usage |
| ;; |
| \?|:|*) |
| echo "$0: Missing option for -$OPTARG" && echo && usage |
| ;; |
| esac |
| done |
| |
| if [ -z "$option_found" ]; then |
| echo "$0: Unknown options: $@" |
| usage |
| fi |