2ab9d086a023c0d800aa845ed02bd978055bf077
[sliver-openvswitch.git] / utilities / ovs-save
1 #! /bin/sh
2
3 # Copyright (c) 2011 Nicira, Inc.
4 #
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:
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 usage() {
18     UTIL=$(basename $0)
19     cat <<EOF
20 ${UTIL}: Provides helper functions to save Open vSwitch's configuration.
21 usage: $0 COMMAND
22
23 Commands:
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
27                         configuration.
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.
31 EOF
32 }
33
34 PATH=/sbin:/bin:/usr/sbin:/usr/bin
35
36 missing_program () {
37     save_IFS=$IFS
38     IFS=:
39     for dir in $PATH; do
40         IFS=$save_IFS
41         if test -x $dir/$1; then
42             return 1
43         fi
44     done
45     IFS=$save_IFS
46     return 0
47 }
48
49 save_interfaces () {
50     if missing_program ip; then
51         echo "$0: ip not found in $PATH" >&2
52         exit 1
53     fi
54
55     if test "$#" = 0; then
56         exit 0
57     fi
58
59     devs="$@"
60     for dev in $devs; do
61         state=`ip link show dev $dev` || continue
62
63         echo "# $dev"
64         # Link state (Ethernet addresses, up/down, ...)
65         linkcmd=
66         case $state in
67             *"state UP"* | *[,\<]"UP"[,\>]* )
68                 linkcmd="$linkcmd up"
69                 ;;
70             *"state DOWN"*)
71                 linkcmd="$linkcmd down"
72                 ;;
73         esac
74         if expr "$state" : '.*\bdynamic\b' > /dev/null; then
75             linkcmd="$linkcmd dynamic"
76         fi
77         if qlen=`expr "$state" : '.*qlen \([0-9]+\)'`; then
78             linkcmd="$linkcmd txqueuelen $qlen"
79         fi
80         if hwaddr=`expr "$state" : '.*link/ether \([^ ]*\)'`; then
81             linkcmd="$linkcmd address $hwaddr"
82         fi
83         if brd=`expr "$state" : '.*brd \([^ ]*\)'`; then
84             linkcmd="$linkcmd broadcast $brd"
85         fi
86         if mtu=`expr "$state" : '.*mtu \([0-9]+\)'`; then
87             linkcmd="$linkcmd mtu $mtu"
88         fi
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
92         fi
93
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
97             set -- $addr
98
99             # Check and trim family.
100             family=$1
101             shift
102             case $family in
103                 inet | inet6) ;;
104                 *) continue ;;
105             esac
106
107             # Trim device off the end--"ip" insists on having "dev" precede it.
108             addrcmd=
109             while test $# != 0; do
110                 case $1 in
111                     dynamic)
112                         # Omit kernel-maintained route.
113                         continue 2
114                         ;;
115                     scope)
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.
119                             continue 2
120                         fi
121                         ;;
122                     "$dev"|"$dev:"*)
123                         # Address label string
124                         addrcmd="$addrcmd label $1"
125                         shift
126                         continue
127                         ;;
128                 esac
129                 addrcmd="$addrcmd $1"
130                 shift
131             done
132             if test "$1" != "$dev"; then
133                 addrcmd="$addrcmd $1"
134             fi
135
136             echo ip -f $family addr add $addrcmd dev $dev
137         done
138
139         # Routes.
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.
143             case $route in
144                 *" proto kernel "*) continue ;;
145             esac
146
147             echo "ip route add $route dev $dev"
148         done
149
150         echo
151     done
152
153     if missing_program iptables-save; then
154         echo "# iptables-save not found in $PATH, not saving iptables state"
155     else
156         echo "# global"
157         echo "iptables-restore <<'EOF'"
158         iptables-save
159         echo "EOF"
160     fi
161 }
162
163 save_flows () {
164     if missing_program ovs-ofctl; then
165         echo "$0: ovs-ofctl not found in $PATH" >&2
166         exit 1
167     fi
168
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'
173         echo "EOF"
174     done
175 }
176
177
178 while [ $# -ne 0 ]
179 do
180     case $1 in
181         "save-flows")
182             shift
183             save_flows "$@"
184             exit 0
185             ;;
186         "save-interfaces")
187             shift
188             save_interfaces "$@"
189             exit 0
190             ;;
191         -h | --help)
192             usage
193             exit 0
194             ;;
195         *)
196             echo >&2 "$0: unknown command \"$1\" (use --help for help)"
197             exit 1
198             ;;
199     esac
200 done
201
202 exit 0