3 # Copyright (c) 2011 Nicira, Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at:
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
20 ${UTIL}: Provides helper functions to save Open vSwitch's configuration.
24 save-interfaces Outputs a shell script on stdout that will restore
25 the current kernel configuration of the specified
26 network interfaces, as well as the system iptables
28 save-flows Outputs a shell script on stdout that will restore
29 Openflow flows of each Open vSwitch bridge.
30 This script is meant as a helper for the Open vSwitch init script commands.
34 PATH=/sbin:/bin:/usr/sbin:/usr/bin
41 if test -x $dir/$1; then
50 if missing_program ip; then
51 echo "$0: ip not found in $PATH" >&2
55 if test "$#" = 0; then
61 state=`ip link show dev $dev` || continue
64 # Link state (Ethernet addresses, up/down, ...)
67 *"state UP"* | *[,\<]"UP"[,\>]* )
71 linkcmd="$linkcmd down"
74 if expr "$state" : '.*\bdynamic\b' > /dev/null; then
75 linkcmd="$linkcmd dynamic"
77 if qlen=`expr "$state" : '.*qlen \([0-9]+\)'`; then
78 linkcmd="$linkcmd txqueuelen $qlen"
80 if hwaddr=`expr "$state" : '.*link/ether \([^ ]*\)'`; then
81 linkcmd="$linkcmd address $hwaddr"
83 if brd=`expr "$state" : '.*brd \([^ ]*\)'`; then
84 linkcmd="$linkcmd broadcast $brd"
86 if mtu=`expr "$state" : '.*mtu \([0-9]+\)'`; then
87 linkcmd="$linkcmd mtu $mtu"
89 if test -n "$linkcmd"; then
90 echo ip link set dev $dev down # Required to change hwaddr.
91 echo ip link set dev $dev $linkcmd
94 # IP addresses (including IPv6).
95 echo "ip addr flush dev $dev 2>/dev/null" # Suppresses "Nothing to flush".
96 ip addr show dev $dev | while read addr; do
99 # Check and trim family.
107 # Trim device off the end--"ip" insists on having "dev" precede it.
109 while test $# != 0; do
112 # Omit kernel-maintained route.
116 if test "$2" = link; then
117 # Omit route derived from IP address, e.g.
118 # 172.16.0.0/16 derived from 172.16.12.34.
123 # Address label string
124 addrcmd="$addrcmd label $1"
129 addrcmd="$addrcmd $1"
132 if test "$1" != "$dev"; then
133 addrcmd="$addrcmd $1"
136 echo ip -f $family addr add $addrcmd dev $dev
140 echo "ip route flush dev $dev proto boot 2>/dev/null" # Suppresses "Nothing to flush".
141 ip route show dev $dev | while read route; do
142 # "proto kernel" routes are installed by the kernel automatically.
144 *" proto kernel "*) continue ;;
147 echo "ip route add $route dev $dev"
153 if missing_program iptables-save; then
154 echo "# iptables-save not found in $PATH, not saving iptables state"
157 echo "iptables-restore <<'EOF'"
164 if missing_program ovs-ofctl; then
165 echo "$0: ovs-ofctl not found in $PATH" >&2
169 for bridge in "$@"; do
170 echo "ovs-ofctl add-flows ${bridge} - << EOF"
171 ovs-ofctl dump-flows "${bridge}" | sed -e '/NXST_FLOW/d' \
172 -e 's/\(idle\|hard\)_age=[^,]*,//g'
196 echo >&2 "$0: unknown command \"$1\" (use --help for help)"