2011-02-08 16:43:40 -08:00
|
|
|
#! /bin/sh
|
|
|
|
|
2016-03-16 20:10:40 -07:00
|
|
|
# Copyright (c) 2011, 2013, 2016 Nicira, Inc.
|
2011-02-08 16:43:40 -08:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2015-06-21 08:02:15 -07:00
|
|
|
case $0 in
|
|
|
|
*/*) dir0=`echo "$0" | sed 's,/[^/]*$,,'` ;;
|
|
|
|
*) dir0=./ ;;
|
|
|
|
esac
|
|
|
|
. "$dir0/ovs-lib" || exit 1
|
|
|
|
|
2012-10-18 15:44:37 -07:00
|
|
|
usage() {
|
|
|
|
UTIL=$(basename $0)
|
2011-02-08 16:43:40 -08:00
|
|
|
cat <<EOF
|
2012-10-18 15:44:37 -07:00
|
|
|
${UTIL}: Provides helper functions to save Open vSwitch's configuration.
|
|
|
|
usage: $0 COMMAND
|
2011-02-08 16:43:40 -08:00
|
|
|
|
2012-10-18 15:44:37 -07:00
|
|
|
Commands:
|
|
|
|
save-interfaces Outputs a shell script on stdout that will restore
|
|
|
|
the current kernel configuration of the specified
|
|
|
|
network interfaces, as well as the system iptables
|
|
|
|
configuration.
|
|
|
|
save-flows Outputs a shell script on stdout that will restore
|
2014-08-14 13:12:12 -07:00
|
|
|
OpenFlow flows of each Open vSwitch bridge.
|
2012-10-18 15:44:37 -07:00
|
|
|
This script is meant as a helper for the Open vSwitch init script commands.
|
2011-02-08 16:43:40 -08:00
|
|
|
EOF
|
2012-10-18 15:44:37 -07:00
|
|
|
}
|
2011-02-08 16:43:40 -08:00
|
|
|
|
2012-10-18 15:44:37 -07:00
|
|
|
save_interfaces () {
|
2018-11-30 07:30:03 +00:00
|
|
|
if (ip link show) > /dev/null 2>&1; then :; else
|
2012-10-18 15:44:37 -07:00
|
|
|
echo "$0: ip not found in $PATH" >&2
|
|
|
|
exit 1
|
2011-02-08 16:43:40 -08:00
|
|
|
fi
|
2012-10-18 15:44:37 -07:00
|
|
|
|
|
|
|
if test "$#" = 0; then
|
|
|
|
exit 0
|
2011-02-08 16:43:40 -08:00
|
|
|
fi
|
|
|
|
|
2012-10-18 15:44:37 -07:00
|
|
|
devs="$@"
|
|
|
|
for dev in $devs; do
|
|
|
|
state=`ip link show dev $dev` || continue
|
|
|
|
|
|
|
|
echo "# $dev"
|
|
|
|
# Link state (Ethernet addresses, up/down, ...)
|
|
|
|
linkcmd=
|
|
|
|
case $state in
|
|
|
|
*"state UP"* | *[,\<]"UP"[,\>]* )
|
|
|
|
linkcmd="$linkcmd up"
|
|
|
|
;;
|
|
|
|
*"state DOWN"*)
|
|
|
|
linkcmd="$linkcmd down"
|
|
|
|
;;
|
2011-02-08 16:43:40 -08:00
|
|
|
esac
|
2012-10-18 15:44:37 -07:00
|
|
|
if expr "$state" : '.*\bdynamic\b' > /dev/null; then
|
|
|
|
linkcmd="$linkcmd dynamic"
|
|
|
|
fi
|
2016-04-11 08:23:43 -07:00
|
|
|
if qlen=`expr "$state" : '.*qlen \([0-9]\+\)'`; then
|
2012-10-18 15:44:37 -07:00
|
|
|
linkcmd="$linkcmd txqueuelen $qlen"
|
|
|
|
fi
|
|
|
|
if hwaddr=`expr "$state" : '.*link/ether \([^ ]*\)'`; then
|
|
|
|
linkcmd="$linkcmd address $hwaddr"
|
|
|
|
fi
|
|
|
|
if brd=`expr "$state" : '.*brd \([^ ]*\)'`; then
|
|
|
|
linkcmd="$linkcmd broadcast $brd"
|
|
|
|
fi
|
2016-04-11 08:23:43 -07:00
|
|
|
if mtu=`expr "$state" : '.*mtu \([0-9]\+\)'`; then
|
2012-10-18 15:44:37 -07:00
|
|
|
linkcmd="$linkcmd mtu $mtu"
|
|
|
|
fi
|
|
|
|
if test -n "$linkcmd"; then
|
|
|
|
echo ip link set dev $dev down # Required to change hwaddr.
|
|
|
|
echo ip link set dev $dev $linkcmd
|
|
|
|
fi
|
|
|
|
|
2015-06-21 08:39:04 -07:00
|
|
|
move_ip_address $dev $dev
|
2015-06-21 08:02:15 -07:00
|
|
|
|
2015-06-21 08:39:04 -07:00
|
|
|
move_ip_routes $dev $dev
|
2011-02-08 16:43:40 -08:00
|
|
|
|
2012-10-18 15:44:37 -07:00
|
|
|
echo
|
2011-02-08 16:43:40 -08:00
|
|
|
done
|
|
|
|
|
2013-08-26 10:44:18 -07:00
|
|
|
if (iptables-save) > /dev/null 2>&1; then
|
2012-10-18 15:44:37 -07:00
|
|
|
echo "# global"
|
|
|
|
echo "iptables-restore <<'EOF'"
|
|
|
|
iptables-save
|
|
|
|
echo "EOF"
|
2013-08-26 10:44:18 -07:00
|
|
|
else
|
|
|
|
echo "# iptables-save not found in $PATH, not saving iptables state"
|
2012-10-18 15:44:37 -07:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2017-09-25 16:44:04 +02:00
|
|
|
get_highest_ofp_version() {
|
|
|
|
ovs-vsctl get bridge "$1" protocols | \
|
2021-06-01 12:47:19 +00:00
|
|
|
sed 's/[][]//g' | sed 's/\ //g' | \
|
2022-05-11 16:53:37 +08:00
|
|
|
awk -F ',' '{ print (NF>0)? $(NF) : "OpenFlow14" }'
|
2017-09-25 16:44:04 +02:00
|
|
|
}
|
|
|
|
|
2012-10-18 15:44:37 -07:00
|
|
|
save_flows () {
|
2013-08-26 10:44:18 -07:00
|
|
|
if (ovs-ofctl --version) > /dev/null 2>&1; then :; else
|
2012-10-18 15:44:37 -07:00
|
|
|
echo "$0: ovs-ofctl not found in $PATH" >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
2011-02-08 16:43:40 -08:00
|
|
|
|
2019-05-08 06:55:27 -07:00
|
|
|
# OVS 2.7 and earlier do not enable OpenFlow 1.4 (by default) and lack
|
|
|
|
# other features needed to save and restore flows. Don't try.
|
|
|
|
case `ovs-appctl version | sed 1q` in
|
|
|
|
"ovs-vswitchd (Open vSwitch) 1."*.*)
|
|
|
|
return
|
|
|
|
;;
|
|
|
|
"ovs-vswitchd (Open vSwitch) 2."[0-7].*)
|
|
|
|
return
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
2017-09-25 16:44:05 +02:00
|
|
|
workdir=$(mktemp -d "${TMPDIR:-/tmp}/ovs-save.XXXXXXXXXX")
|
2012-10-18 15:44:37 -07:00
|
|
|
for bridge in "$@"; do
|
2017-09-25 16:44:04 +02:00
|
|
|
# Get the highest enabled OpenFlow version
|
|
|
|
ofp_version=$(get_highest_ofp_version "$bridge")
|
|
|
|
|
2022-07-01 18:52:40 +08:00
|
|
|
printf "%s" "ovs-ofctl -O $ofp_version add-tlv-map ${bridge} '"
|
2021-07-01 12:34:04 +00:00
|
|
|
ovs-ofctl dump-tlv-map ${bridge} -O $ofp_version | \
|
2018-10-07 21:50:53 -07:00
|
|
|
awk '/^ *0x/ {if (cnt != 0) printf ","; \
|
2016-08-30 14:04:12 -07:00
|
|
|
cnt++;printf "{class="$1",type="$2",len="$3"}->"$4}'
|
|
|
|
echo "'"
|
|
|
|
|
2018-09-24 11:18:03 -07:00
|
|
|
# If possible use OpenFlow 1.4 atomic bundle txn for flows and groups
|
|
|
|
[ ${ofp_version#OpenFlow} -ge 14 ] && bundle=" --bundle" || bundle=""
|
2017-09-25 16:44:04 +02:00
|
|
|
|
2018-09-24 11:18:03 -07:00
|
|
|
echo "ovs-ofctl -O $ofp_version add-groups ${bridge} \
|
|
|
|
\"$workdir/$bridge.groups.dump\" ${bundle}"
|
|
|
|
|
|
|
|
echo "ovs-ofctl -O $ofp_version replace-flows ${bridge} \
|
|
|
|
\"$workdir/$bridge.flows.dump\" ${bundle}"
|
|
|
|
|
|
|
|
ovs-ofctl -O $ofp_version dump-groups "$bridge" | \
|
|
|
|
sed -e '/^OFPST_GROUP_DESC/d' \
|
|
|
|
-e '/^NXST_GROUP_DESC/d' > \
|
|
|
|
"$workdir/$bridge.groups.dump"
|
2017-09-25 16:44:04 +02:00
|
|
|
|
|
|
|
ovs-ofctl -O $ofp_version dump-flows --no-names --no-stats "$bridge" | \
|
|
|
|
sed -e '/NXST_FLOW/d' \
|
|
|
|
-e '/OFPST_FLOW/d' \
|
2021-11-04 15:39:45 -04:00
|
|
|
-e 's/\(idle\|hard\)_age=[^,]*,//g' \
|
|
|
|
-e 's/igmp_type/tp_src/g' \
|
|
|
|
-e 's/igmp_code/tp_dst/g' \
|
|
|
|
-e 's/igmp/ip,nw_proto=2/g' > \
|
2017-09-25 16:44:05 +02:00
|
|
|
"$workdir/$bridge.flows.dump"
|
2011-02-08 16:43:40 -08:00
|
|
|
done
|
2017-09-25 16:44:05 +02:00
|
|
|
echo "rm -rf \"$workdir\""
|
2012-10-18 15:44:37 -07:00
|
|
|
}
|
2011-02-08 16:43:40 -08:00
|
|
|
|
2012-10-18 15:44:37 -07:00
|
|
|
while [ $# -ne 0 ]
|
|
|
|
do
|
|
|
|
case $1 in
|
|
|
|
"save-flows")
|
|
|
|
shift
|
|
|
|
save_flows "$@"
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
"save-interfaces")
|
|
|
|
shift
|
|
|
|
save_interfaces "$@"
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
-h | --help)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo >&2 "$0: unknown command \"$1\" (use --help for help)"
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2011-02-08 16:43:40 -08:00
|
|
|
|
|
|
|
exit 0
|