2f7022138773d272049deb856524715620164c0c
[openvswitch] / 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  save-datapaths         Outputs a shell script on stdout that will restore
31                         the datapaths with the same port numbers as before.
32
33 This script is meant as a helper for the Open vSwitch init script commands.
34 EOF
35 }
36
37 PATH=/sbin:/bin:/usr/sbin:/usr/bin
38
39 missing_program () {
40     save_IFS=$IFS
41     IFS=:
42     for dir in $PATH; do
43         IFS=$save_IFS
44         if test -x $dir/$1; then
45             return 1
46         fi
47     done
48     IFS=$save_IFS
49     return 0
50 }
51
52 save_interfaces () {
53     if missing_program ip; then
54         echo "$0: ip not found in $PATH" >&2
55         exit 1
56     fi
57
58     if test "$#" = 0; then
59         exit 0
60     fi
61
62     devs="$@"
63     for dev in $devs; do
64         state=`ip link show dev $dev` || continue
65
66         echo "# $dev"
67         # Link state (Ethernet addresses, up/down, ...)
68         linkcmd=
69         case $state in
70             *"state UP"* | *[,\<]"UP"[,\>]* )
71                 linkcmd="$linkcmd up"
72                 ;;
73             *"state DOWN"*)
74                 linkcmd="$linkcmd down"
75                 ;;
76         esac
77         if expr "$state" : '.*\bdynamic\b' > /dev/null; then
78             linkcmd="$linkcmd dynamic"
79         fi
80         if qlen=`expr "$state" : '.*qlen \([0-9]+\)'`; then
81             linkcmd="$linkcmd txqueuelen $qlen"
82         fi
83         if hwaddr=`expr "$state" : '.*link/ether \([^ ]*\)'`; then
84             linkcmd="$linkcmd address $hwaddr"
85         fi
86         if brd=`expr "$state" : '.*brd \([^ ]*\)'`; then
87             linkcmd="$linkcmd broadcast $brd"
88         fi
89         if mtu=`expr "$state" : '.*mtu \([0-9]+\)'`; then
90             linkcmd="$linkcmd mtu $mtu"
91         fi
92         if test -n "$linkcmd"; then
93             echo ip link set dev $dev down # Required to change hwaddr.
94             echo ip link set dev $dev $linkcmd
95         fi
96
97         # IP addresses (including IPv6).
98         echo "ip addr flush dev $dev 2>/dev/null" # Suppresses "Nothing to flush".
99         ip addr show dev $dev | while read addr; do
100             set -- $addr
101
102             # Check and trim family.
103             family=$1
104             shift
105             case $family in
106                 inet | inet6) ;;
107                 *) continue ;;
108             esac
109
110             # Trim device off the end--"ip" insists on having "dev" precede it.
111             addrcmd=
112             while test $# != 0; do
113                 case $1 in
114                     dynamic)
115                         # Omit kernel-maintained route.
116                         continue 2
117                         ;;
118                     scope)
119                         if test "$2" = link; then
120                             # Omit route derived from IP address, e.g.
121                             # 172.16.0.0/16 derived from 172.16.12.34.
122                             continue 2
123                         fi
124                         ;;
125                     "$dev"|"$dev:"*)
126                         # Address label string
127                         addrcmd="$addrcmd label $1"
128                         shift
129                         continue
130                         ;;
131                 esac
132                 addrcmd="$addrcmd $1"
133                 shift
134             done
135             if test "$1" != "$dev"; then
136                 addrcmd="$addrcmd $1"
137             fi
138
139             echo ip -f $family addr add $addrcmd dev $dev
140         done
141
142         # Routes.
143         echo "ip route flush dev $dev proto boot 2>/dev/null" # Suppresses "Nothing to flush".
144         ip route show dev $dev | while read route; do
145             # "proto kernel" routes are installed by the kernel automatically.
146             case $route in
147                 *" proto kernel "*) continue ;;
148             esac
149
150             echo "ip route add $route dev $dev"
151         done
152
153         echo
154     done
155
156     if missing_program iptables-save; then
157         echo "# iptables-save not found in $PATH, not saving iptables state"
158     else
159         echo "# global"
160         echo "iptables-restore <<'EOF'"
161         iptables-save
162         echo "EOF"
163     fi
164 }
165
166 save_flows () {
167     if missing_program ovs-ofctl; then
168         echo "$0: ovs-ofctl not found in $PATH" >&2
169         exit 1
170     fi
171
172     for bridge in "$@"; do
173         echo "ovs-ofctl add-flows ${bridge} - << EOF"
174         ovs-ofctl dump-flows "${bridge}" | sed -e '/NXST_FLOW/d' \
175             -e 's/\(idle\|hard\)_age=[^,]*,//g'
176         echo "EOF"
177     done
178 }
179
180 ovs_vsctl () {
181     ovs-vsctl --no-wait --timeout=5 "$@"
182 }
183
184 save_datapaths () {
185     if missing_program ovs-dpctl; then
186         echo "$0: ovs-dpctl not found in $PATH" >&2
187         exit 1
188     fi
189     if missing_program ovs-vsctl; then
190         echo "$0: ovs-vsctl not found in $PATH" >&2
191         exit 1
192     fi
193
194     for dp in "$@"; do
195         echo "ovs-dpctl add-dp ${dp}"
196         ovs-dpctl show $dp | while read line; do
197             # An example 'ovs-dpctl show' output looks like this:
198             # system@br1:
199             # lookups: hit:0 missed:0 lost:0
200             # flows: 0
201             # port 0: br1 (internal)
202             # port 2: gre2886795521 (ipsec_gre: key=flow, remote_ip=172.17.1.1, tos=inherit)
203             # port 3: gre1 (ipsec_gre: remote_ip=192.168.113.1)
204             # port 14: gre2 (gre: remote_ip=192.168.115.1)
205             # port 15: gre3 (gre64: remote_ip=192.168.116.1)
206             # port 16: eth0
207             # port 17: br1- (patch: peer=br1+)
208
209             # Skip lines which do not have 'port'
210             if port_no=`expr "${line}" : '.*port \([0-9]\+\):'`; then :; else
211                 continue
212             fi
213
214             netdev=`echo ${line} | awk '{print $3}'`
215
216             # Do not add port that has the same name as the datapath. It gets
217             # added by default.
218             [ "${dp#system@}" = "${netdev}" ] && continue
219
220             type=`echo ${line} | awk '{print $4}' | sed 's/[:)(]//g'`
221             [ ! -n "${type}" ] && type="system"
222
223             command="ovs-dpctl add-if ${dp}\
224                         ${netdev},type=${type},port_no=${port_no}"
225
226             options=`echo ${line} | awk -F: '{print $3}' | sed 's/[) ]//g'`
227             [ -n "${options}" ] && command="${command},${options}"
228
229             # For ipsec, ovs-dpctl does not show the key value pairs related
230             # to certificates. Get that information from ovs-vsctl.
231             if [ "${type}" = "ipsec_gre" ] ; then
232                 if peer_cert=`ovs_vsctl get interface \
233                                 "${netdev}" options:peer_cert 2>/dev/null`; then
234                     # The option peer_cert comes with an accompanying
235                     # "certificate" or "use_ssl_cert"
236                     if certificate=`ovs_vsctl get interface "${netdev}" \
237                             options:certificate 2>/dev/null` ; then
238                         command="${command},peer_cert=${peer_cert},certificate=${certificate}"
239                     else
240                         use_ssl_cert=`ovs_vsctl get interface "${netdev}" \
241                                         options:use_ssl_cert 2>/dev/null`
242                         command="${command},peer_cert=${peer_cert},use_ssl_cert=${use_ssl_cert}"
243                     fi
244                 else
245                     psk=`ovs_vsctl get interface "${netdev}" \
246                             options:psk 2>/dev/null`
247                     command="${command},psk=${psk}"
248                 fi
249             fi
250             echo ${command}
251         done
252     done
253 }
254
255 while [ $# -ne 0 ]
256 do
257     case $1 in
258         "save-datapaths")
259             shift
260             save_datapaths "$@"
261             exit 0
262             ;;
263         "save-flows")
264             shift
265             save_flows "$@"
266             exit 0
267             ;;
268         "save-interfaces")
269             shift
270             save_interfaces "$@"
271             exit 0
272             ;;
273         -h | --help)
274             usage
275             exit 0
276             ;;
277         *)
278             echo >&2 "$0: unknown command \"$1\" (use --help for help)"
279             exit 1
280             ;;
281     esac
282 done
283
284 exit 0