3 # Copyright (c) 2011, 2013 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 save-ofports Outputs a shell script on stdout that will restore
31 the ofport value across a force-reload-kmod.
32 This script is meant as a helper for the Open vSwitch init script commands.
37 if (ip -V) > /dev/null 2>&1; then :; else
38 echo "$0: ip not found in $PATH" >&2
42 if test "$#" = 0; then
48 state=`ip link show dev $dev` || continue
51 # Link state (Ethernet addresses, up/down, ...)
54 *"state UP"* | *[,\<]"UP"[,\>]* )
58 linkcmd="$linkcmd down"
61 if expr "$state" : '.*\bdynamic\b' > /dev/null; then
62 linkcmd="$linkcmd dynamic"
64 if qlen=`expr "$state" : '.*qlen \([0-9]+\)'`; then
65 linkcmd="$linkcmd txqueuelen $qlen"
67 if hwaddr=`expr "$state" : '.*link/ether \([^ ]*\)'`; then
68 linkcmd="$linkcmd address $hwaddr"
70 if brd=`expr "$state" : '.*brd \([^ ]*\)'`; then
71 linkcmd="$linkcmd broadcast $brd"
73 if mtu=`expr "$state" : '.*mtu \([0-9]+\)'`; then
74 linkcmd="$linkcmd mtu $mtu"
76 if test -n "$linkcmd"; then
77 echo ip link set dev $dev down # Required to change hwaddr.
78 echo ip link set dev $dev $linkcmd
81 # IP addresses (including IPv6).
82 echo "ip addr flush dev $dev 2>/dev/null" # Suppresses "Nothing to flush".
83 ip addr show dev $dev | while read addr; do
86 # Check and trim family.
94 # Trim device off the end--"ip" insists on having "dev" precede it.
96 while test $# != 0; do
99 # Omit kernel-maintained route.
103 if test "$2" = link -a "$family" != inet6; then
104 # Omit route derived from IP address, e.g.
105 # 172.16.0.0/16 derived from 172.16.12.34,
106 # but preserve IPv6 link-local address.
111 # Address label string
112 addrcmd="$addrcmd label $1"
117 addrcmd="$addrcmd $1"
120 if test "$1" != "$dev"; then
121 addrcmd="$addrcmd $1"
124 echo ip -f $family addr add $addrcmd dev $dev
128 echo "ip route flush dev $dev proto boot 2>/dev/null" # Suppresses "Nothing to flush".
129 ip route show dev $dev | while read route; do
130 # "proto kernel" routes are installed by the kernel automatically.
132 *" proto kernel "*) continue ;;
135 echo "ip route add $route dev $dev"
141 if (iptables-save) > /dev/null 2>&1; then
143 echo "iptables-restore <<'EOF'"
147 echo "# iptables-save not found in $PATH, not saving iptables state"
152 if (ovs-ofctl --version) > /dev/null 2>&1; then :; else
153 echo "$0: ovs-ofctl not found in $PATH" >&2
157 for bridge in "$@"; do
158 echo "ovs-ofctl add-flows ${bridge} - << EOF"
159 ovs-ofctl dump-flows "${bridge}" | sed -e '/NXST_FLOW/d' \
160 -e 's/\(idle\|hard\)_age=[^,]*,//g'
166 ovs-vsctl --no-wait "$@"
171 if (ovs-vsctl --version) > /dev/null 2>&1; then :; else
172 echo "$0: ovs-vsctl not found in $PATH" >&2
176 for bridge in "$@"; do
178 for iface in `ovs_vsctl list-ifaces ${bridge}`; do
179 ofport=`ovs_vsctl get interface ${iface} ofport`
180 [ "${count}" -eq 0 ] && cmd="ovs-vsctl --no-wait"
181 cmd="${cmd} -- --if-exists set interface "${iface}" \
182 ofport_request="${ofport}""
184 # Run set interface command on 50 ports at a time.
185 count=`expr ${count} + 1`
186 [ "${count}" -eq 50 ] && count=0 && echo "${cmd}" && cmd=""
215 echo >&2 "$0: unknown command \"$1\" (use --help for help)"